41fa1b31709c187d05dea14c70ffa06fae94e033
[vpp.git] / src / vnet / sctp / sctp_output.c
1 /*
2  * Copyright (c) 2017 SUSE LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/sctp/sctp.h>
16 #include <vnet/sctp/sctp_debug.h>
17 #include <vppinfra/random.h>
18 #include <openssl/hmac.h>
19
20 vlib_node_registration_t sctp4_output_node;
21 vlib_node_registration_t sctp6_output_node;
22
23 typedef enum _sctp_output_next
24 {
25   SCTP_OUTPUT_NEXT_DROP,
26   SCTP_OUTPUT_NEXT_IP_LOOKUP,
27   SCTP_OUTPUT_N_NEXT
28 } sctp_output_next_t;
29
30 #define foreach_sctp4_output_next               \
31   _ (DROP, "error-drop")                        \
32   _ (IP_LOOKUP, "ip4-lookup")
33
34 #define foreach_sctp6_output_next               \
35   _ (DROP, "error-drop")                        \
36   _ (IP_LOOKUP, "ip6-lookup")
37
38 static char *sctp_error_strings[] = {
39 #define sctp_error(n,s) s,
40 #include <vnet/sctp/sctp_error.def>
41 #undef sctp_error
42 };
43
44 typedef struct
45 {
46   sctp_header_t sctp_header;
47   sctp_connection_t sctp_connection;
48 } sctp_tx_trace_t;
49
50 /**
51  * Flush tx frame populated by retransmits and timer pops
52  */
53 void
54 sctp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
55 {
56   if (sctp_main.tx_frames[!is_ip4][thread_index])
57     {
58       u32 next_index;
59       next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
60       vlib_put_frame_to_node (vm, next_index,
61                               sctp_main.tx_frames[!is_ip4][thread_index]);
62       sctp_main.tx_frames[!is_ip4][thread_index] = 0;
63     }
64 }
65
66 /**
67  * Flush ip lookup tx frames populated by timer pops
68  */
69 always_inline void
70 sctp_flush_frame_to_ip_lookup (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
71 {
72   if (sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index])
73     {
74       u32 next_index;
75       next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
76       vlib_put_frame_to_node (vm, next_index,
77                               sctp_main.ip_lookup_tx_frames[!is_ip4]
78                               [thread_index]);
79       sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
80     }
81 }
82
83 /**
84  * Flush v4 and v6 sctp and ip-lookup tx frames for thread index
85  */
86 void
87 sctp_flush_frames_to_output (u8 thread_index)
88 {
89   vlib_main_t *vm = vlib_get_main ();
90   sctp_flush_frame_to_output (vm, thread_index, 1);
91   sctp_flush_frame_to_output (vm, thread_index, 0);
92   sctp_flush_frame_to_ip_lookup (vm, thread_index, 1);
93   sctp_flush_frame_to_ip_lookup (vm, thread_index, 0);
94 }
95
96 u32
97 ip4_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
98                            ip4_header_t * ip0)
99 {
100   ip_csum_t checksum;
101   u32 ip_header_length, payload_length_host_byte_order;
102   u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
103   void *data_this_buffer;
104
105   /* Initialize checksum with ip header. */
106   ip_header_length = ip4_header_bytes (ip0);
107   payload_length_host_byte_order =
108     clib_net_to_host_u16 (ip0->length) - ip_header_length;
109   checksum =
110     clib_host_to_net_u32 (payload_length_host_byte_order +
111                           (ip0->protocol << 16));
112
113   if (BITS (uword) == 32)
114     {
115       checksum =
116         ip_csum_with_carry (checksum,
117                             clib_mem_unaligned (&ip0->src_address, u32));
118       checksum =
119         ip_csum_with_carry (checksum,
120                             clib_mem_unaligned (&ip0->dst_address, u32));
121     }
122   else
123     checksum =
124       ip_csum_with_carry (checksum,
125                           clib_mem_unaligned (&ip0->src_address, u64));
126
127   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
128   data_this_buffer = (void *) ip0 + ip_header_length;
129   n_ip_bytes_this_buffer =
130     p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
131   if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
132     {
133       n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
134         n_ip_bytes_this_buffer - ip_header_length : 0;
135     }
136   while (1)
137     {
138       checksum =
139         ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
140       n_bytes_left -= n_this_buffer;
141       if (n_bytes_left == 0)
142         break;
143
144       ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
145       p0 = vlib_get_buffer (vm, p0->next_buffer);
146       data_this_buffer = vlib_buffer_get_current (p0);
147       n_this_buffer = p0->current_length;
148     }
149
150   return checksum;
151 }
152
153 u32
154 ip6_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
155                            ip6_header_t * ip0, int *bogus_lengthp)
156 {
157   ip_csum_t checksum;
158   u16 payload_length_host_byte_order;
159   u32 i, n_this_buffer, n_bytes_left;
160   u32 headers_size = sizeof (ip0[0]);
161   void *data_this_buffer;
162
163   ASSERT (bogus_lengthp);
164   *bogus_lengthp = 0;
165
166   /* Initialize checksum with ip header. */
167   checksum = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
168   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
169   data_this_buffer = (void *) (ip0 + 1);
170
171   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
172     {
173       checksum = ip_csum_with_carry (checksum,
174                                      clib_mem_unaligned (&ip0->
175                                                          src_address.as_uword
176                                                          [i], uword));
177       checksum =
178         ip_csum_with_carry (checksum,
179                             clib_mem_unaligned (&ip0->dst_address.as_uword[i],
180                                                 uword));
181     }
182
183   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
184    * or UDP-Ping packets */
185   if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
186     {
187       u32 skip_bytes;
188       ip6_hop_by_hop_ext_t *ext_hdr =
189         (ip6_hop_by_hop_ext_t *) data_this_buffer;
190
191       /* validate really icmp6 next */
192       ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_SCTP));
193
194       skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
195       data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
196
197       payload_length_host_byte_order -= skip_bytes;
198       headers_size += skip_bytes;
199     }
200
201   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
202   if (p0 && n_this_buffer + headers_size > p0->current_length)
203     n_this_buffer =
204       p0->current_length >
205       headers_size ? p0->current_length - headers_size : 0;
206   while (1)
207     {
208       checksum =
209         ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
210       n_bytes_left -= n_this_buffer;
211       if (n_bytes_left == 0)
212         break;
213
214       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
215         {
216           *bogus_lengthp = 1;
217           return 0xfefe;
218         }
219       p0 = vlib_get_buffer (vm, p0->next_buffer);
220       data_this_buffer = vlib_buffer_get_current (p0);
221       n_this_buffer = p0->current_length;
222     }
223
224   return checksum;
225 }
226
227 void
228 sctp_push_ip_hdr (sctp_main_t * tm, sctp_sub_connection_t * sctp_sub_conn,
229                   vlib_buffer_t * b)
230 {
231   sctp_header_t *th = vlib_buffer_get_current (b);
232   vlib_main_t *vm = vlib_get_main ();
233   if (sctp_sub_conn->c_is_ip4)
234     {
235       ip4_header_t *ih;
236       ih = vlib_buffer_push_ip4 (vm, b, &sctp_sub_conn->c_lcl_ip4,
237                                  &sctp_sub_conn->c_rmt_ip4, IP_PROTOCOL_SCTP,
238                                  1);
239       th->checksum = ip4_sctp_compute_checksum (vm, b, ih);
240     }
241   else
242     {
243       ip6_header_t *ih;
244       int bogus = ~0;
245
246       ih = vlib_buffer_push_ip6 (vm, b, &sctp_sub_conn->c_lcl_ip6,
247                                  &sctp_sub_conn->c_rmt_ip6, IP_PROTOCOL_SCTP);
248       th->checksum = ip6_sctp_compute_checksum (vm, b, ih, &bogus);
249       ASSERT (!bogus);
250     }
251 }
252
253 always_inline void *
254 sctp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
255 {
256   if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
257     vlib_buffer_free_one (vm, b->next_buffer);
258   /* Zero all flags but free list index and trace flag */
259   b->flags &= VLIB_BUFFER_NEXT_PRESENT - 1;
260   b->current_data = 0;
261   b->current_length = 0;
262   b->total_length_not_including_first_buffer = 0;
263   vnet_buffer (b)->sctp.flags = 0;
264   vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
265
266   /* Leave enough space for headers */
267   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
268 }
269
270 always_inline void *
271 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
272 {
273   ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
274   b->flags &= VLIB_BUFFER_NON_DEFAULT_FREELIST;
275   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
276   b->total_length_not_including_first_buffer = 0;
277   vnet_buffer (b)->sctp.flags = 0;
278   vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
279   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
280   /* Leave enough space for headers */
281   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
282 }
283
284 always_inline int
285 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
286 {
287   vlib_main_t *vm = vlib_get_main ();
288   u32 current_length = vec_len (tm->tx_buffers[thread_index]);
289   u32 n_allocated;
290
291   vec_validate (tm->tx_buffers[thread_index],
292                 current_length + n_free_buffers - 1);
293   n_allocated =
294     vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
295                        n_free_buffers);
296   _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
297   /* buffer shortage, report failure */
298   if (vec_len (tm->tx_buffers[thread_index]) == 0)
299     {
300       clib_warning ("out of buffers");
301       return -1;
302     }
303   return 0;
304 }
305
306 always_inline int
307 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
308 {
309   u32 *my_tx_buffers;
310   u32 thread_index = vlib_get_thread_index ();
311   if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
312     {
313       if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
314         return -1;
315     }
316   my_tx_buffers = tm->tx_buffers[thread_index];
317   *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
318   _vec_len (my_tx_buffers) -= 1;
319   return 0;
320 }
321
322 always_inline void
323 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
324                           u8 is_ip4, u8 flush)
325 {
326   sctp_main_t *tm = vnet_get_sctp_main ();
327   u32 thread_index = vlib_get_thread_index ();
328   u32 *to_next, next_index;
329   vlib_frame_t *f;
330
331   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
332   b->error = 0;
333
334   /* Decide where to send the packet */
335   next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
336   sctp_trajectory_add_start (b, 2);
337
338   /* Get frame to v4/6 output node */
339   f = tm->tx_frames[!is_ip4][thread_index];
340   if (!f)
341     {
342       f = vlib_get_frame_to_node (vm, next_index);
343       ASSERT (f);
344       tm->tx_frames[!is_ip4][thread_index] = f;
345     }
346   to_next = vlib_frame_vector_args (f);
347   to_next[f->n_vectors] = bi;
348   f->n_vectors += 1;
349   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
350     {
351       vlib_put_frame_to_node (vm, next_index, f);
352       tm->tx_frames[!is_ip4][thread_index] = 0;
353     }
354 }
355
356 always_inline void
357 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
358                             u8 is_ip4)
359 {
360   sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
361 }
362
363 always_inline void
364 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
365                              u8 is_ip4, u8 flush)
366 {
367   sctp_main_t *tm = vnet_get_sctp_main ();
368   u32 thread_index = vlib_get_thread_index ();
369   u32 *to_next, next_index;
370   vlib_frame_t *f;
371
372   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
373   b->error = 0;
374
375   /* Default FIB for now */
376   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
377
378   /* Send to IP lookup */
379   next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
380   if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
381     {
382       b->pre_data[0] = 2;
383       b->pre_data[1] = next_index;
384     }
385
386   f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
387   if (!f)
388     {
389       f = vlib_get_frame_to_node (vm, next_index);
390       ASSERT (f);
391       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
392     }
393
394   to_next = vlib_frame_vector_args (f);
395   to_next[f->n_vectors] = bi;
396   f->n_vectors += 1;
397   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
398     {
399       vlib_put_frame_to_node (vm, next_index, f);
400       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
401     }
402 }
403
404 always_inline void
405 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
406                            u8 is_ip4)
407 {
408   sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
409 }
410
411 /**
412  * Convert buffer to INIT
413  */
414 void
415 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, u8 idx,
416                          vlib_buffer_t * b)
417 {
418   u32 random_seed = random_default_seed ();
419   u16 alloc_bytes = sizeof (sctp_init_chunk_t);
420   sctp_sub_connection_t *sub_conn = &sctp_conn->sub_conn[idx];
421
422   sctp_ipv4_addr_param_t *ip4_param = 0;
423   sctp_ipv6_addr_param_t *ip6_param = 0;
424
425   if (sub_conn->c_is_ip4)
426     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
427   else
428     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
429
430   /* As per RFC 4960 the chunk_length value does NOT contemplate
431    * the size of the first header (see sctp_header_t) and any padding
432    */
433   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
434
435   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
436
437   sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
438
439   u16 pointer_offset = sizeof (init_chunk);
440   if (sub_conn->c_is_ip4)
441     {
442       ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
443       ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
444
445       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
446     }
447   else
448     {
449       ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
450       ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
451       ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
452
453       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
454     }
455
456   init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
457   init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
458   init_chunk->sctp_hdr.checksum = 0;
459   /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
460   init_chunk->sctp_hdr.verification_tag = 0x0;
461
462   vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
463   vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
464   vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
465
466   init_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
467   init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
468   init_chunk->inboud_streams_count =
469     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
470   init_chunk->outbound_streams_count =
471     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
472
473   init_chunk->initial_tsn =
474     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
475   SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
476                           sctp_conn->local_initial_tsn);
477
478   sctp_conn->local_tag = init_chunk->initiate_tag;
479
480   vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
481   vnet_buffer (b)->sctp.subconn_idx = idx;
482
483   SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
484                           "CHUNK_TYPE = %s, "
485                           "SRC_PORT = %u, DST_PORT = %u",
486                           sub_conn->connection.c_index,
487                           sctp_conn->state,
488                           sctp_state_to_string (sctp_conn->state),
489                           sctp_chunk_to_string (INIT),
490                           init_chunk->sctp_hdr.src_port,
491                           init_chunk->sctp_hdr.dst_port);
492 }
493
494 void
495 sctp_compute_mac (sctp_connection_t * sctp_conn,
496                   sctp_state_cookie_param_t * state_cookie)
497 {
498 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
499   HMAC_CTX *ctx;
500 #else
501   HMAC_CTX ctx;
502 #endif
503   unsigned int len = 0;
504   const EVP_MD *md = EVP_sha1 ();
505 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
506   ctx = HMAC_CTX_new ();
507   HMAC_Init_ex (ctx, &state_cookie->creation_time,
508                 sizeof (state_cookie->creation_time), md, NULL);
509   HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
510   HMAC_Final (ctx, state_cookie->mac, &len);
511 #else
512   HMAC_CTX_init (&ctx);
513   HMAC_Init_ex (&ctx, &state_cookie->creation_time,
514                 sizeof (state_cookie->creation_time), md, NULL);
515   HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
516   HMAC_Final (&ctx, state_cookie->mac, &len);
517   HMAC_CTX_cleanup (&ctx);
518 #endif
519
520   ENDIANESS_SWAP (state_cookie->mac);
521 }
522
523 void
524 sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
525                                vlib_buffer_t * b)
526 {
527   vlib_main_t *vm = vlib_get_main ();
528
529   sctp_reuse_buffer (vm, b);
530
531   u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
532
533   /* As per RFC 4960 the chunk_length value does NOT contemplate
534    * the size of the first header (see sctp_header_t) and any padding
535    */
536   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
537
538   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
539
540   sctp_cookie_ack_chunk_t *cookie_ack_chunk =
541     vlib_buffer_push_uninit (b, alloc_bytes);
542
543   cookie_ack_chunk->sctp_hdr.checksum = 0;
544   cookie_ack_chunk->sctp_hdr.src_port =
545     sctp_conn->sub_conn[idx].connection.lcl_port;
546   cookie_ack_chunk->sctp_hdr.dst_port =
547     sctp_conn->sub_conn[idx].connection.rmt_port;
548   cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
549   vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
550   vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
551
552   vnet_buffer (b)->sctp.connection_index =
553     sctp_conn->sub_conn[idx].connection.c_index;
554   vnet_buffer (b)->sctp.subconn_idx = idx;
555 }
556
557 void
558 sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn, u8 idx,
559                                 vlib_buffer_t * b, u8 reuse_buffer)
560 {
561   vlib_main_t *vm = vlib_get_main ();
562
563   if (reuse_buffer)
564     sctp_reuse_buffer (vm, b);
565
566   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
567   u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
568   /* As per RFC 4960 the chunk_length value does NOT contemplate
569    * the size of the first header (see sctp_header_t) and any padding
570    */
571   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
572   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
573   sctp_cookie_echo_chunk_t *cookie_echo_chunk =
574     vlib_buffer_push_uninit (b, alloc_bytes);
575   cookie_echo_chunk->sctp_hdr.checksum = 0;
576   cookie_echo_chunk->sctp_hdr.src_port =
577     sctp_conn->sub_conn[idx].connection.lcl_port;
578   cookie_echo_chunk->sctp_hdr.dst_port =
579     sctp_conn->sub_conn[idx].connection.rmt_port;
580   cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
581   vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
582   vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
583   clib_memcpy (&(cookie_echo_chunk->cookie), &sctp_conn->cookie_param,
584                sizeof (sctp_state_cookie_param_t));
585
586   vnet_buffer (b)->sctp.connection_index =
587     sctp_conn->sub_conn[idx].connection.c_index;
588   vnet_buffer (b)->sctp.subconn_idx = idx;
589 }
590
591
592 /*
593  *  Send COOKIE_ECHO
594  */
595 void
596 sctp_send_cookie_echo (sctp_connection_t * sctp_conn)
597 {
598   vlib_buffer_t *b;
599   u32 bi;
600   sctp_main_t *tm = vnet_get_sctp_main ();
601   vlib_main_t *vm = vlib_get_main ();
602
603   if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS))
604     {
605       clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection.");
606
607       session_stream_connect_notify (&sctp_conn->sub_conn
608                                      [MAIN_SCTP_SUB_CONN_IDX].connection, 1);
609
610       sctp_connection_timers_reset (sctp_conn);
611
612       sctp_connection_cleanup (sctp_conn);
613     }
614
615   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
616     return;
617
618   b = vlib_get_buffer (vm, bi);
619   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
620
621   sctp_init_buffer (vm, b);
622   sctp_prepare_cookie_echo_chunk (sctp_conn, idx, b, 0);
623   sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
624
625   /* Start the T1_INIT timer */
626   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
627                   sctp_conn->sub_conn[idx].RTO);
628
629   /* Change state to COOKIE_WAIT */
630   sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
631
632   /* Measure RTT with this */
633   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
634 }
635
636
637 /**
638  * Convert buffer to ERROR
639  */
640 void
641 sctp_prepare_operation_error (sctp_connection_t * sctp_conn, u8 idx,
642                               vlib_buffer_t * b, u8 err_cause)
643 {
644   vlib_main_t *vm = vlib_get_main ();
645
646   sctp_reuse_buffer (vm, b);
647
648   /* The minimum size of the message is given by the sctp_operation_error_t */
649   u16 alloc_bytes =
650     sizeof (sctp_operation_error_t) + sizeof (sctp_err_cause_param_t);
651
652   /* As per RFC 4960 the chunk_length value does NOT contemplate
653    * the size of the first header (see sctp_header_t) and any padding
654    */
655   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
656
657   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
658
659   sctp_operation_error_t *err_chunk =
660     vlib_buffer_push_uninit (b, alloc_bytes);
661
662   /* src_port & dst_port are already in network byte-order */
663   err_chunk->sctp_hdr.checksum = 0;
664   err_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
665   err_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
666   /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
667   err_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
668
669   err_chunk->err_causes[0].param_hdr.length =
670     clib_host_to_net_u16 (sizeof (err_chunk->err_causes[0].param_hdr.type) +
671                           sizeof (err_chunk->err_causes[0].param_hdr.length));
672   err_chunk->err_causes[0].param_hdr.type = clib_host_to_net_u16 (err_cause);
673
674   vnet_sctp_set_chunk_type (&err_chunk->chunk_hdr, OPERATION_ERROR);
675   vnet_sctp_set_chunk_length (&err_chunk->chunk_hdr, chunk_len);
676
677   vnet_buffer (b)->sctp.connection_index =
678     sctp_conn->sub_conn[idx].connection.c_index;
679   vnet_buffer (b)->sctp.subconn_idx = idx;
680 }
681
682 /**
683  * Convert buffer to ABORT
684  */
685 void
686 sctp_prepare_abort_for_collision (sctp_connection_t * sctp_conn, u8 idx,
687                                   vlib_buffer_t * b, ip4_address_t * ip4_addr,
688                                   ip6_address_t * ip6_addr)
689 {
690   vlib_main_t *vm = vlib_get_main ();
691
692   sctp_reuse_buffer (vm, b);
693
694   /* The minimum size of the message is given by the sctp_abort_chunk_t */
695   u16 alloc_bytes = sizeof (sctp_abort_chunk_t);
696
697   /* As per RFC 4960 the chunk_length value does NOT contemplate
698    * the size of the first header (see sctp_header_t) and any padding
699    */
700   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
701
702   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
703
704   sctp_abort_chunk_t *abort_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
705
706   /* src_port & dst_port are already in network byte-order */
707   abort_chunk->sctp_hdr.checksum = 0;
708   abort_chunk->sctp_hdr.src_port =
709     sctp_conn->sub_conn[idx].connection.lcl_port;
710   abort_chunk->sctp_hdr.dst_port =
711     sctp_conn->sub_conn[idx].connection.rmt_port;
712   /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
713   abort_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
714
715   vnet_sctp_set_chunk_type (&abort_chunk->chunk_hdr, ABORT);
716   vnet_sctp_set_chunk_length (&abort_chunk->chunk_hdr, chunk_len);
717
718   vnet_buffer (b)->sctp.connection_index =
719     sctp_conn->sub_conn[idx].connection.c_index;
720   vnet_buffer (b)->sctp.subconn_idx = idx;
721 }
722
723 /**
724  * Convert buffer to INIT-ACK
725  */
726 void
727 sctp_prepare_initack_chunk_for_collision (sctp_connection_t * sctp_conn,
728                                           u8 idx, vlib_buffer_t * b,
729                                           ip4_address_t * ip4_addr,
730                                           ip6_address_t * ip6_addr)
731 {
732   vlib_main_t *vm = vlib_get_main ();
733   sctp_ipv4_addr_param_t *ip4_param = 0;
734   sctp_ipv6_addr_param_t *ip6_param = 0;
735
736   sctp_reuse_buffer (vm, b);
737
738   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
739   u16 alloc_bytes =
740     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
741
742   if (PREDICT_TRUE (ip4_addr != NULL))
743     {
744       /* Create room for variable-length fields in the INIT_ACK chunk */
745       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
746     }
747   if (PREDICT_TRUE (ip6_addr != NULL))
748     {
749       /* Create room for variable-length fields in the INIT_ACK chunk */
750       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
751     }
752
753   if (sctp_conn->sub_conn[idx].connection.is_ip4)
754     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
755   else
756     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
757
758   /* As per RFC 4960 the chunk_length value does NOT contemplate
759    * the size of the first header (see sctp_header_t) and any padding
760    */
761   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
762
763   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
764
765   sctp_init_ack_chunk_t *init_ack_chunk =
766     vlib_buffer_push_uninit (b, alloc_bytes);
767
768   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
769
770   /* Create State Cookie parameter */
771   sctp_state_cookie_param_t *state_cookie_param =
772     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
773
774   state_cookie_param->param_hdr.type =
775     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
776   state_cookie_param->param_hdr.length =
777     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
778   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
779   state_cookie_param->cookie_lifespan =
780     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
781
782   sctp_compute_mac (sctp_conn, state_cookie_param);
783
784   pointer_offset += sizeof (sctp_state_cookie_param_t);
785
786   if (PREDICT_TRUE (ip4_addr != NULL))
787     {
788       sctp_ipv4_addr_param_t *ipv4_addr =
789         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
790
791       ipv4_addr->param_hdr.type =
792         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
793       ipv4_addr->param_hdr.length =
794         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
795       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
796
797       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
798     }
799   if (PREDICT_TRUE (ip6_addr != NULL))
800     {
801       sctp_ipv6_addr_param_t *ipv6_addr =
802         (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
803
804       ipv6_addr->param_hdr.type =
805         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
806       ipv6_addr->param_hdr.length =
807         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
808       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
809       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
810
811       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
812     }
813
814   if (sctp_conn->sub_conn[idx].connection.is_ip4)
815     {
816       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
817       ip4_param->address.as_u32 =
818         sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
819
820       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
821     }
822   else
823     {
824       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
825       ip6_param->address.as_u64[0] =
826         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
827       ip6_param->address.as_u64[1] =
828         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
829
830       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
831     }
832
833   /* src_port & dst_port are already in network byte-order */
834   init_ack_chunk->sctp_hdr.checksum = 0;
835   init_ack_chunk->sctp_hdr.src_port =
836     sctp_conn->sub_conn[idx].connection.lcl_port;
837   init_ack_chunk->sctp_hdr.dst_port =
838     sctp_conn->sub_conn[idx].connection.rmt_port;
839   /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
840   init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
841   init_ack_chunk->initial_tsn =
842     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
843   SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
844                           init_ack_chunk->initial_tsn);
845
846   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
847   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
848
849   init_ack_chunk->initiate_tag = sctp_conn->local_tag;
850
851   init_ack_chunk->a_rwnd =
852     clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
853   init_ack_chunk->inboud_streams_count =
854     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
855   init_ack_chunk->outbound_streams_count =
856     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
857
858   vnet_buffer (b)->sctp.connection_index =
859     sctp_conn->sub_conn[idx].connection.c_index;
860   vnet_buffer (b)->sctp.subconn_idx = idx;
861 }
862
863 /**
864  * Convert buffer to INIT-ACK
865  */
866 void
867 sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, u8 idx,
868                             vlib_buffer_t * b, ip4_address_t * ip4_addr,
869                             ip6_address_t * ip6_addr)
870 {
871   vlib_main_t *vm = vlib_get_main ();
872   sctp_ipv4_addr_param_t *ip4_param = 0;
873   sctp_ipv6_addr_param_t *ip6_param = 0;
874   u32 random_seed = random_default_seed ();
875
876   sctp_reuse_buffer (vm, b);
877
878   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
879   u16 alloc_bytes =
880     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
881
882   if (PREDICT_TRUE (ip4_addr != NULL))
883     {
884       /* Create room for variable-length fields in the INIT_ACK chunk */
885       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
886     }
887   if (PREDICT_TRUE (ip6_addr != NULL))
888     {
889       /* Create room for variable-length fields in the INIT_ACK chunk */
890       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
891     }
892
893   if (sctp_conn->sub_conn[idx].connection.is_ip4)
894     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
895   else
896     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
897
898   /* As per RFC 4960 the chunk_length value does NOT contemplate
899    * the size of the first header (see sctp_header_t) and any padding
900    */
901   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
902
903   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
904
905   sctp_init_ack_chunk_t *init_ack_chunk =
906     vlib_buffer_push_uninit (b, alloc_bytes);
907
908   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
909
910   /* Create State Cookie parameter */
911   sctp_state_cookie_param_t *state_cookie_param =
912     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
913
914   state_cookie_param->param_hdr.type =
915     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
916   state_cookie_param->param_hdr.length =
917     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
918   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
919   state_cookie_param->cookie_lifespan =
920     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
921
922   sctp_compute_mac (sctp_conn, state_cookie_param);
923
924   pointer_offset += sizeof (sctp_state_cookie_param_t);
925
926   if (PREDICT_TRUE (ip4_addr != NULL))
927     {
928       sctp_ipv4_addr_param_t *ipv4_addr =
929         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
930
931       ipv4_addr->param_hdr.type =
932         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
933       ipv4_addr->param_hdr.length =
934         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
935       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
936
937       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
938     }
939   if (PREDICT_TRUE (ip6_addr != NULL))
940     {
941       sctp_ipv6_addr_param_t *ipv6_addr =
942         (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
943
944       ipv6_addr->param_hdr.type =
945         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
946       ipv6_addr->param_hdr.length =
947         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
948       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
949       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
950
951       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
952     }
953
954   if (sctp_conn->sub_conn[idx].connection.is_ip4)
955     {
956       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
957       ip4_param->address.as_u32 =
958         sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
959
960       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
961     }
962   else
963     {
964       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
965       ip6_param->address.as_u64[0] =
966         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
967       ip6_param->address.as_u64[1] =
968         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
969
970       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
971     }
972
973   /* src_port & dst_port are already in network byte-order */
974   init_ack_chunk->sctp_hdr.checksum = 0;
975   init_ack_chunk->sctp_hdr.src_port =
976     sctp_conn->sub_conn[idx].connection.lcl_port;
977   init_ack_chunk->sctp_hdr.dst_port =
978     sctp_conn->sub_conn[idx].connection.rmt_port;
979   /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
980   init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
981   init_ack_chunk->initial_tsn =
982     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
983   SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
984                           init_ack_chunk->initial_tsn);
985
986   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
987   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
988
989   init_ack_chunk->initiate_tag =
990     clib_host_to_net_u32 (random_u32 (&random_seed));
991
992   init_ack_chunk->a_rwnd =
993     clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
994   init_ack_chunk->inboud_streams_count =
995     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
996   init_ack_chunk->outbound_streams_count =
997     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
998
999   sctp_conn->local_tag = init_ack_chunk->initiate_tag;
1000
1001   vnet_buffer (b)->sctp.connection_index =
1002     sctp_conn->sub_conn[idx].connection.c_index;
1003   vnet_buffer (b)->sctp.subconn_idx = idx;
1004 }
1005
1006 /**
1007  * Convert buffer to SHUTDOWN
1008  */
1009 void
1010 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, u8 idx,
1011                              vlib_buffer_t * b)
1012 {
1013   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1014
1015   /* As per RFC 4960 the chunk_length value does NOT contemplate
1016    * the size of the first header (see sctp_header_t) and any padding
1017    */
1018   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1019
1020   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1021
1022   sctp_shutdown_association_chunk_t *shutdown_chunk =
1023     vlib_buffer_push_uninit (b, alloc_bytes);
1024
1025   shutdown_chunk->sctp_hdr.checksum = 0;
1026   /* No need of host_to_net conversion, already in net-byte order */
1027   shutdown_chunk->sctp_hdr.src_port =
1028     sctp_conn->sub_conn[idx].connection.lcl_port;
1029   shutdown_chunk->sctp_hdr.dst_port =
1030     sctp_conn->sub_conn[idx].connection.rmt_port;
1031   shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1032   vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
1033   vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
1034
1035   shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
1036
1037   vnet_buffer (b)->sctp.connection_index =
1038     sctp_conn->sub_conn[idx].connection.c_index;
1039   vnet_buffer (b)->sctp.subconn_idx = idx;
1040 }
1041
1042 /*
1043  * Send SHUTDOWN
1044  */
1045 void
1046 sctp_send_shutdown (sctp_connection_t * sctp_conn)
1047 {
1048   vlib_buffer_t *b;
1049   u32 bi;
1050   sctp_main_t *tm = vnet_get_sctp_main ();
1051   vlib_main_t *vm = vlib_get_main ();
1052
1053   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1054     return;
1055
1056   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1057     return;
1058
1059   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
1060
1061   b = vlib_get_buffer (vm, bi);
1062   sctp_init_buffer (vm, b);
1063   sctp_prepare_shutdown_chunk (sctp_conn, idx, b);
1064
1065   sctp_enqueue_to_output_now (vm, b, bi,
1066                               sctp_conn->sub_conn[idx].connection.is_ip4);
1067 }
1068
1069 /**
1070  * Convert buffer to SHUTDOWN_ACK
1071  */
1072 void
1073 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1074                                  vlib_buffer_t * b)
1075 {
1076   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1077   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1078
1079   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1080
1081   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
1082     vlib_buffer_push_uninit (b, alloc_bytes);
1083
1084   shutdown_ack_chunk->sctp_hdr.checksum = 0;
1085   /* No need of host_to_net conversion, already in net-byte order */
1086   shutdown_ack_chunk->sctp_hdr.src_port =
1087     sctp_conn->sub_conn[idx].connection.lcl_port;
1088   shutdown_ack_chunk->sctp_hdr.dst_port =
1089     sctp_conn->sub_conn[idx].connection.rmt_port;
1090   shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1091
1092   vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
1093   vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
1094
1095   vnet_buffer (b)->sctp.connection_index =
1096     sctp_conn->sub_conn[idx].connection.c_index;
1097   vnet_buffer (b)->sctp.subconn_idx = idx;
1098 }
1099
1100 /*
1101  * Send SHUTDOWN_ACK
1102  */
1103 void
1104 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, u8 idx,
1105                         vlib_buffer_t * b)
1106 {
1107   vlib_main_t *vm = vlib_get_main ();
1108
1109   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1110     return;
1111
1112   sctp_reuse_buffer (vm, b);
1113
1114   sctp_prepare_shutdown_ack_chunk (sctp_conn, idx, b);
1115 }
1116
1117 /**
1118  * Convert buffer to SACK
1119  */
1120 void
1121 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1122                          vlib_buffer_t * b)
1123 {
1124   vlib_main_t *vm = vlib_get_main ();
1125
1126   sctp_reuse_buffer (vm, b);
1127
1128   u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
1129
1130   /* As per RFC 4960 the chunk_length value does NOT contemplate
1131    * the size of the first header (see sctp_header_t) and any padding
1132    */
1133   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1134
1135   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1136
1137   sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
1138
1139   sack->sctp_hdr.checksum = 0;
1140   sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1141   sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1142   sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1143   vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
1144   vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
1145
1146   sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
1147
1148   sctp_conn->ack_state = 0;
1149
1150   vnet_buffer (b)->sctp.connection_index =
1151     sctp_conn->sub_conn[idx].connection.c_index;
1152   vnet_buffer (b)->sctp.subconn_idx = idx;
1153 }
1154
1155 /**
1156  * Convert buffer to HEARTBEAT_ACK
1157  */
1158 void
1159 sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1160                                   vlib_buffer_t * b)
1161 {
1162   vlib_main_t *vm = vlib_get_main ();
1163
1164   u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
1165
1166   sctp_reuse_buffer (vm, b);
1167
1168   /* As per RFC 4960 the chunk_length value does NOT contemplate
1169    * the size of the first header (see sctp_header_t) and any padding
1170    */
1171   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1172
1173   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1174
1175   sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
1176
1177   hb_ack->sctp_hdr.checksum = 0;
1178   /* No need of host_to_net conversion, already in net-byte order */
1179   hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1180   hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1181   hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1182   hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1183   hb_ack->hb_info.param_hdr.length =
1184     clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
1185
1186   vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
1187   vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
1188
1189   vnet_buffer (b)->sctp.connection_index =
1190     sctp_conn->sub_conn[idx].connection.c_index;
1191   vnet_buffer (b)->sctp.subconn_idx = idx;
1192 }
1193
1194 /**
1195  * Convert buffer to HEARTBEAT
1196  */
1197 void
1198 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn, u8 idx,
1199                               vlib_buffer_t * b)
1200 {
1201   u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
1202
1203   /* As per RFC 4960 the chunk_length value does NOT contemplate
1204    * the size of the first header (see sctp_header_t) and any padding
1205    */
1206   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1207
1208   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1209
1210   sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
1211
1212   hb_req->sctp_hdr.checksum = 0;
1213   /* No need of host_to_net conversion, already in net-byte order */
1214   hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1215   hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1216   hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1217   hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1218   hb_req->hb_info.param_hdr.length =
1219     clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
1220
1221   vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
1222   vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
1223
1224   vnet_buffer (b)->sctp.connection_index =
1225     sctp_conn->sub_conn[idx].connection.c_index;
1226   vnet_buffer (b)->sctp.subconn_idx = idx;
1227 }
1228
1229 void
1230 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
1231 {
1232   vlib_buffer_t *b;
1233   u32 bi;
1234   sctp_main_t *tm = vnet_get_sctp_main ();
1235   vlib_main_t *vm = vlib_get_main ();
1236
1237   u8 i;
1238   u32 now = sctp_time_now ();
1239
1240   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
1241     {
1242       if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
1243         continue;
1244
1245       if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
1246         {
1247           if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1248             return;
1249
1250           b = vlib_get_buffer (vm, bi);
1251           sctp_init_buffer (vm, b);
1252           sctp_prepare_heartbeat_chunk (sctp_conn, i, b);
1253
1254           sctp_enqueue_to_output_now (vm, b, bi,
1255                                       sctp_conn->sub_conn[i].
1256                                       connection.is_ip4);
1257
1258           sctp_conn->sub_conn[i].unacknowledged_hb += 1;
1259         }
1260     }
1261 }
1262
1263 /**
1264  * Convert buffer to SHUTDOWN_COMPLETE
1265  */
1266 void
1267 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn, u8 idx,
1268                                       vlib_buffer_t * b)
1269 {
1270   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1271   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1272
1273   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1274
1275   sctp_shutdown_complete_chunk_t *shutdown_complete =
1276     vlib_buffer_push_uninit (b, alloc_bytes);
1277
1278   shutdown_complete->sctp_hdr.checksum = 0;
1279   /* No need of host_to_net conversion, already in net-byte order */
1280   shutdown_complete->sctp_hdr.src_port =
1281     sctp_conn->sub_conn[idx].connection.lcl_port;
1282   shutdown_complete->sctp_hdr.dst_port =
1283     sctp_conn->sub_conn[idx].connection.rmt_port;
1284   shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1285
1286   vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1287   vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1288
1289   vnet_buffer (b)->sctp.connection_index =
1290     sctp_conn->sub_conn[idx].connection.c_index;
1291   vnet_buffer (b)->sctp.subconn_idx = idx;
1292 }
1293
1294 void
1295 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn, u8 idx,
1296                              vlib_buffer_t * b0)
1297 {
1298   vlib_main_t *vm = vlib_get_main ();
1299
1300   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1301     return;
1302
1303   sctp_reuse_buffer (vm, b0);
1304
1305   sctp_prepare_shutdown_complete_chunk (sctp_conn, idx, b0);
1306 }
1307
1308 /*
1309  *  Send INIT
1310  */
1311 void
1312 sctp_send_init (sctp_connection_t * sctp_conn)
1313 {
1314   vlib_buffer_t *b;
1315   u32 bi;
1316   sctp_main_t *tm = vnet_get_sctp_main ();
1317   vlib_main_t *vm = vlib_get_main ();
1318
1319   if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS))
1320     {
1321       clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection.");
1322
1323       session_stream_connect_notify (&sctp_conn->sub_conn
1324                                      [MAIN_SCTP_SUB_CONN_IDX].connection, 1);
1325
1326       sctp_connection_timers_reset (sctp_conn);
1327
1328       sctp_connection_cleanup (sctp_conn);
1329
1330       return;
1331     }
1332
1333   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1334     return;
1335
1336   b = vlib_get_buffer (vm, bi);
1337   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
1338
1339   sctp_init_buffer (vm, b);
1340   sctp_prepare_init_chunk (sctp_conn, idx, b);
1341
1342   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1343   sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
1344
1345   /* Start the T1_INIT timer */
1346   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1347                   sctp_conn->sub_conn[idx].RTO);
1348
1349   /* Change state to COOKIE_WAIT */
1350   sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
1351
1352   /* Measure RTT with this */
1353   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1354 }
1355
1356 /**
1357  * Push SCTP header and update connection variables
1358  */
1359 static void
1360 sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
1361                  sctp_state_t next_state)
1362 {
1363   u16 data_len =
1364     b->current_length + b->total_length_not_including_first_buffer;
1365   ASSERT (!b->total_length_not_including_first_buffer
1366           || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1367
1368   SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1369                        "b->current_data = %p "
1370                        "data_len = %u",
1371                        b->current_length, b->current_data, data_len);
1372
1373   u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1374   u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1375
1376   bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1377
1378   sctp_payload_data_chunk_t *data_chunk =
1379     vlib_buffer_push_uninit (b, bytes_to_add);
1380
1381   u8 idx = sctp_data_subconn_select (sctp_conn);
1382   SCTP_DBG_OUTPUT
1383     ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
1384      sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1385      sctp_conn->sub_conn[idx].connection.c_index,
1386      sctp_conn->sub_conn[idx].connection.lcl_port,
1387      sctp_conn->sub_conn[idx].connection.rmt_port);
1388   data_chunk->sctp_hdr.checksum = 0;
1389   data_chunk->sctp_hdr.src_port =
1390     sctp_conn->sub_conn[idx].connection.lcl_port;
1391   data_chunk->sctp_hdr.dst_port =
1392     sctp_conn->sub_conn[idx].connection.rmt_port;
1393   data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1394
1395   data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
1396   data_chunk->stream_id = clib_host_to_net_u16 (0);
1397   data_chunk->stream_seq = clib_host_to_net_u16 (0);
1398
1399   vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1400   vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1401
1402   vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1403   vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1404
1405   SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1406                        b->data, b->current_data);
1407
1408   sctp_conn->last_unacked_tsn = sctp_conn->next_tsn;
1409   sctp_conn->next_tsn += data_len;
1410
1411   u32 inflight = sctp_conn->next_tsn - sctp_conn->last_unacked_tsn;
1412   /* Section 7.2.2; point (3) */
1413   if (sctp_conn->sub_conn[idx].partially_acked_bytes >=
1414       sctp_conn->sub_conn[idx].cwnd
1415       && inflight >= sctp_conn->sub_conn[idx].cwnd)
1416     {
1417       sctp_conn->sub_conn[idx].cwnd += sctp_conn->sub_conn[idx].PMTU;
1418       sctp_conn->sub_conn[idx].partially_acked_bytes -=
1419         sctp_conn->sub_conn[idx].cwnd;
1420     }
1421
1422   sctp_conn->sub_conn[idx].last_data_ts = sctp_time_now ();
1423
1424   vnet_buffer (b)->sctp.connection_index =
1425     sctp_conn->sub_conn[idx].connection.c_index;
1426
1427   vnet_buffer (b)->sctp.subconn_idx = idx;
1428 }
1429
1430 u32
1431 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
1432 {
1433   sctp_connection_t *sctp_conn =
1434     sctp_get_connection_from_transport (trans_conn);
1435
1436   SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, "
1437                    "S_INDEX = %u, C_INDEX = %u,"
1438                    "trans_conn->LCL_PORT = %u, trans_conn->RMT_PORT = %u",
1439                    trans_conn,
1440                    sctp_conn,
1441                    trans_conn->s_index,
1442                    trans_conn->c_index,
1443                    trans_conn->lcl_port, trans_conn->rmt_port);
1444
1445   sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
1446
1447   sctp_trajectory_add_start (b, 3);
1448
1449   return 0;
1450 }
1451
1452 void
1453 sctp_data_retransmit (sctp_connection_t * sctp_conn)
1454 {
1455   /* TODO: requires use of PEEK/SEND */
1456   SCTP_DBG_OUTPUT
1457     ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
1458      sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1459      sctp_conn->sub_conn[idx].connection.c_index,
1460      sctp_conn->sub_conn[idx].connection.lcl_port,
1461      sctp_conn->sub_conn[idx].connection.rmt_port);
1462
1463   return;
1464 }
1465
1466 #if SCTP_DEBUG_STATE_MACHINE
1467 always_inline u8
1468 sctp_validate_output_state_machine (sctp_connection_t * sctp_conn,
1469                                     u8 chunk_type)
1470 {
1471   u8 result = 0;
1472   switch (sctp_conn->state)
1473     {
1474     case SCTP_STATE_CLOSED:
1475       if (chunk_type != INIT && chunk_type != INIT_ACK)
1476         result = 1;
1477       break;
1478     case SCTP_STATE_ESTABLISHED:
1479       if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1480           chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1481           chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1482         result = 1;
1483       break;
1484     case SCTP_STATE_COOKIE_WAIT:
1485       if (chunk_type != COOKIE_ECHO)
1486         result = 1;
1487       break;
1488     case SCTP_STATE_SHUTDOWN_SENT:
1489       if (chunk_type != SHUTDOWN_COMPLETE)
1490         result = 1;
1491       break;
1492     case SCTP_STATE_SHUTDOWN_RECEIVED:
1493       if (chunk_type != SHUTDOWN_ACK)
1494         result = 1;
1495       break;
1496     }
1497   return result;
1498 }
1499 #endif
1500
1501 always_inline u8
1502 sctp_is_retransmitting (sctp_connection_t * sctp_conn, u8 idx)
1503 {
1504   return sctp_conn->sub_conn[idx].is_retransmitting;
1505 }
1506
1507 always_inline uword
1508 sctp46_output_inline (vlib_main_t * vm,
1509                       vlib_node_runtime_t * node,
1510                       vlib_frame_t * from_frame, int is_ip4)
1511 {
1512   u32 n_left_from, next_index, *from, *to_next;
1513   u32 my_thread_index = vm->thread_index;
1514
1515   from = vlib_frame_vector_args (from_frame);
1516   n_left_from = from_frame->n_vectors;
1517   next_index = node->cached_next_index;
1518   sctp_set_time_now (my_thread_index);
1519
1520   while (n_left_from > 0)
1521     {
1522       u32 n_left_to_next;
1523
1524       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1525
1526       while (n_left_from > 0 && n_left_to_next > 0)
1527         {
1528           u32 bi0;
1529           vlib_buffer_t *b0;
1530           sctp_header_t *sctp_hdr = 0;
1531           sctp_connection_t *sctp_conn;
1532           sctp_tx_trace_t *t0;
1533           sctp_header_t *th0 = 0;
1534           u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1535             SCTP_OUTPUT_NEXT_IP_LOOKUP;
1536
1537 #if SCTP_DEBUG_STATE_MACHINE
1538           u16 packet_length = 0;
1539 #endif
1540
1541           bi0 = from[0];
1542           to_next[0] = bi0;
1543           from += 1;
1544           to_next += 1;
1545           n_left_from -= 1;
1546           n_left_to_next -= 1;
1547
1548           b0 = vlib_get_buffer (vm, bi0);
1549
1550           sctp_conn =
1551             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1552                                  my_thread_index);
1553
1554           if (PREDICT_FALSE (sctp_conn == 0))
1555             {
1556               error0 = SCTP_ERROR_INVALID_CONNECTION;
1557               next0 = SCTP_OUTPUT_NEXT_DROP;
1558               goto done;
1559             }
1560
1561           u8 idx = vnet_buffer (b0)->sctp.subconn_idx;
1562
1563           th0 = vlib_buffer_get_current (b0);
1564
1565           if (is_ip4)
1566             {
1567               ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1568                                                          b0,
1569                                                          &sctp_conn->sub_conn
1570                                                          [idx].connection.
1571                                                          lcl_ip.ip4,
1572                                                          &sctp_conn->
1573                                                          sub_conn
1574                                                          [idx].connection.
1575                                                          rmt_ip.ip4,
1576                                                          IP_PROTOCOL_SCTP, 1);
1577
1578               u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1579
1580               sctp_hdr = ip4_next_header (iph4);
1581               sctp_hdr->checksum = checksum;
1582
1583               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1584
1585 #if SCTP_DEBUG_STATE_MACHINE
1586               packet_length = clib_net_to_host_u16 (iph4->length);
1587 #endif
1588             }
1589           else
1590             {
1591               ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1592                                                          b0,
1593                                                          &sctp_conn->sub_conn
1594                                                          [idx].
1595                                                          connection.lcl_ip.
1596                                                          ip6,
1597                                                          &sctp_conn->sub_conn
1598                                                          [idx].
1599                                                          connection.rmt_ip.
1600                                                          ip6,
1601                                                          IP_PROTOCOL_SCTP);
1602
1603               int bogus = ~0;
1604               u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1605               ASSERT (!bogus);
1606
1607               sctp_hdr = ip6_next_header (iph6);
1608               sctp_hdr->checksum = checksum;
1609
1610               vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1611               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1612
1613 #if SCTP_DEBUG_STATE_MACHINE
1614               packet_length = clib_net_to_host_u16 (iph6->payload_length);
1615 #endif
1616             }
1617
1618           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1619           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1620           if (chunk_type >= UNKNOWN)
1621             {
1622               clib_warning
1623                 ("Trying to send an unrecognized chunk... something is really bad.");
1624               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1625               next0 = SCTP_OUTPUT_NEXT_DROP;
1626               goto done;
1627             }
1628
1629 #if SCTP_DEBUG_STATE_MACHINE
1630           u8 is_valid =
1631             (sctp_conn->sub_conn[idx].connection.lcl_port ==
1632              sctp_hdr->src_port
1633              || sctp_conn->sub_conn[idx].connection.lcl_port ==
1634              sctp_hdr->dst_port)
1635             && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1636                 sctp_hdr->dst_port
1637                 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1638                 sctp_hdr->src_port);
1639
1640           if (!is_valid)
1641             {
1642               SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1643                                       "packet_length = %u, "
1644                                       "chunk_type = %u [%s], "
1645                                       "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1646                                       "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1647                                       sctp_conn->sub_conn[idx].
1648                                       connection.c_index, packet_length,
1649                                       chunk_type,
1650                                       sctp_chunk_to_string (chunk_type),
1651                                       sctp_conn->sub_conn[idx].
1652                                       connection.lcl_port, sctp_hdr->src_port,
1653                                       sctp_conn->sub_conn[idx].
1654                                       connection.rmt_port,
1655                                       sctp_hdr->dst_port);
1656
1657               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1658               next0 = SCTP_OUTPUT_NEXT_DROP;
1659               goto done;
1660             }
1661 #endif
1662           SCTP_DBG_STATE_MACHINE
1663             ("SESSION_INDEX = %u, CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1664              "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1665              sctp_conn->sub_conn[idx].connection.s_index,
1666              sctp_conn->sub_conn[idx].connection.c_index,
1667              sctp_conn->state, sctp_state_to_string (sctp_conn->state),
1668              sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1669              full_hdr->hdr.dst_port);
1670
1671           /* Let's make sure the state-machine does not send anything crazy */
1672 #if SCTP_DEBUG_STATE_MACHINE
1673           if (sctp_validate_output_state_machine (sctp_conn, chunk_type) != 0)
1674             {
1675               SCTP_DBG_STATE_MACHINE
1676                 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1677                  sctp_chunk_to_string (chunk_type),
1678                  sctp_state_to_string (sctp_conn->state));
1679
1680               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1681               next0 = SCTP_OUTPUT_NEXT_DROP;
1682               goto done;
1683
1684             }
1685 #endif
1686
1687           /* Karn's algorithm: RTT measurements MUST NOT be made using
1688            * packets that were retransmitted
1689            */
1690           if (!sctp_is_retransmitting (sctp_conn, idx))
1691             {
1692               /* Measure RTT with this */
1693               if (chunk_type == DATA
1694                   && sctp_conn->sub_conn[idx].RTO_pending == 0)
1695                 {
1696                   sctp_conn->sub_conn[idx].RTO_pending = 1;
1697                   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1698                 }
1699               else
1700                 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1701             }
1702
1703           /* Let's take care of TIMERS */
1704           switch (chunk_type)
1705             {
1706             case COOKIE_ECHO:
1707               {
1708                 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1709                 break;
1710               }
1711             case DATA:
1712               {
1713                 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1714
1715                 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1716                                    sctp_conn->sub_conn[idx].RTO);
1717                 break;
1718               }
1719             case SHUTDOWN:
1720               {
1721                 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1722                 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1723                                 sctp_conn->sub_conn[idx].RTO);
1724                 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1725                 break;
1726               }
1727             case SHUTDOWN_ACK:
1728               {
1729                 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1730                 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1731                                 sctp_conn->sub_conn[idx].RTO);
1732                 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1733                 break;
1734               }
1735             case SHUTDOWN_COMPLETE:
1736               {
1737                 sctp_conn->state = SCTP_STATE_CLOSED;
1738                 break;
1739               }
1740             }
1741
1742           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1743           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1744
1745           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1746
1747           SCTP_DBG_STATE_MACHINE
1748             ("SESSION_INDEX = %u, CONNECTION_INDEX = %u, " "NEW_STATE = %s, "
1749              "CHUNK_SENT = %s", sctp_conn->sub_conn[idx].connection.s_index,
1750              sctp_conn->sub_conn[idx].connection.c_index,
1751              sctp_state_to_string (sctp_conn->state),
1752              sctp_chunk_to_string (chunk_type));
1753
1754           vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1755
1756         done:
1757           b0->error = node->errors[error0];
1758           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1759             {
1760               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1761               if (th0)
1762                 {
1763                   clib_memcpy (&t0->sctp_header, th0,
1764                                sizeof (t0->sctp_header));
1765                 }
1766               else
1767                 {
1768                   memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1769                 }
1770               clib_memcpy (&t0->sctp_connection, sctp_conn,
1771                            sizeof (t0->sctp_connection));
1772             }
1773
1774           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1775                                            n_left_to_next, bi0, next0);
1776         }
1777
1778       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1779     }
1780
1781   return from_frame->n_vectors;
1782 }
1783
1784 static uword
1785 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1786               vlib_frame_t * from_frame)
1787 {
1788   return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1789 }
1790
1791 static uword
1792 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1793               vlib_frame_t * from_frame)
1794 {
1795   return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1796 }
1797
1798 /* *INDENT-OFF* */
1799 VLIB_REGISTER_NODE (sctp4_output_node) =
1800 {
1801   .function = sctp4_output,.name = "sctp4-output",
1802     /* Takes a vector of packets. */
1803     .vector_size = sizeof (u32),
1804     .n_errors = SCTP_N_ERROR,
1805     .error_strings = sctp_error_strings,
1806     .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1807     .next_nodes = {
1808 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1809     foreach_sctp4_output_next
1810 #undef _
1811     },
1812     .format_buffer = format_sctp_header,
1813     .format_trace = format_sctp_tx_trace,
1814 };
1815 /* *INDENT-ON* */
1816
1817 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1818
1819 /* *INDENT-OFF* */
1820 VLIB_REGISTER_NODE (sctp6_output_node) =
1821 {
1822   .function = sctp6_output,
1823   .name = "sctp6-output",
1824     /* Takes a vector of packets. */
1825   .vector_size = sizeof (u32),
1826   .n_errors = SCTP_N_ERROR,
1827   .error_strings = sctp_error_strings,
1828   .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1829   .next_nodes = {
1830 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1831     foreach_sctp6_output_next
1832 #undef _
1833   },
1834   .format_buffer = format_sctp_header,
1835   .format_trace = format_sctp_tx_trace,
1836 };
1837 /* *INDENT-ON* */
1838
1839 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1840
1841 /*
1842  * fd.io coding-style-patch-verification: ON
1843  *
1844  * Local Variables:
1845  * eval: (c-set-style "gnu")
1846  * End:
1847  */