SCTP: handle INIT corner-case handling
[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,
560                                 sctp_state_cookie_param_t * sc)
561 {
562   vlib_main_t *vm = vlib_get_main ();
563
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), sc,
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  * Convert buffer to ABORT
593  */
594 void
595 sctp_prepare_abort_for_collision (sctp_connection_t * sctp_conn, u8 idx,
596                                   vlib_buffer_t * b, ip4_address_t * ip4_addr,
597                                   ip6_address_t * ip6_addr)
598 {
599   vlib_main_t *vm = vlib_get_main ();
600
601   sctp_reuse_buffer (vm, b);
602
603   /* The minimum size of the message is given by the sctp_abort_chunk_t */
604   u16 alloc_bytes = sizeof (sctp_abort_chunk_t);
605
606   /* As per RFC 4960 the chunk_length value does NOT contemplate
607    * the size of the first header (see sctp_header_t) and any padding
608    */
609   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
610
611   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
612
613   sctp_abort_chunk_t *abort_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
614
615   /* src_port & dst_port are already in network byte-order */
616   abort_chunk->sctp_hdr.checksum = 0;
617   abort_chunk->sctp_hdr.src_port =
618     sctp_conn->sub_conn[idx].connection.lcl_port;
619   abort_chunk->sctp_hdr.dst_port =
620     sctp_conn->sub_conn[idx].connection.rmt_port;
621   /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
622   abort_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
623
624   vnet_sctp_set_chunk_type (&abort_chunk->chunk_hdr, ABORT);
625   vnet_sctp_set_chunk_length (&abort_chunk->chunk_hdr, chunk_len);
626
627   vnet_buffer (b)->sctp.connection_index =
628     sctp_conn->sub_conn[idx].connection.c_index;
629   vnet_buffer (b)->sctp.subconn_idx = idx;
630 }
631
632 /**
633  * Convert buffer to INIT-ACK
634  */
635 void
636 sctp_prepare_initack_chunk_for_collision (sctp_connection_t * sctp_conn,
637                                           u8 idx, vlib_buffer_t * b,
638                                           ip4_address_t * ip4_addr,
639                                           ip6_address_t * ip6_addr)
640 {
641   vlib_main_t *vm = vlib_get_main ();
642   sctp_ipv4_addr_param_t *ip4_param = 0;
643   sctp_ipv6_addr_param_t *ip6_param = 0;
644
645   sctp_reuse_buffer (vm, b);
646
647   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
648   u16 alloc_bytes =
649     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
650
651   if (PREDICT_TRUE (ip4_addr != NULL))
652     {
653       /* Create room for variable-length fields in the INIT_ACK chunk */
654       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
655     }
656   if (PREDICT_TRUE (ip6_addr != NULL))
657     {
658       /* Create room for variable-length fields in the INIT_ACK chunk */
659       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
660     }
661
662   if (sctp_conn->sub_conn[idx].connection.is_ip4)
663     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
664   else
665     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
666
667   /* As per RFC 4960 the chunk_length value does NOT contemplate
668    * the size of the first header (see sctp_header_t) and any padding
669    */
670   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
671
672   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
673
674   sctp_init_ack_chunk_t *init_ack_chunk =
675     vlib_buffer_push_uninit (b, alloc_bytes);
676
677   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
678
679   /* Create State Cookie parameter */
680   sctp_state_cookie_param_t *state_cookie_param =
681     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
682
683   state_cookie_param->param_hdr.type =
684     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
685   state_cookie_param->param_hdr.length =
686     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
687   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
688   state_cookie_param->cookie_lifespan =
689     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
690
691   sctp_compute_mac (sctp_conn, state_cookie_param);
692
693   pointer_offset += sizeof (sctp_state_cookie_param_t);
694
695   if (PREDICT_TRUE (ip4_addr != NULL))
696     {
697       sctp_ipv4_addr_param_t *ipv4_addr =
698         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
699
700       ipv4_addr->param_hdr.type =
701         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
702       ipv4_addr->param_hdr.length =
703         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
704       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
705
706       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
707     }
708   if (PREDICT_TRUE (ip6_addr != NULL))
709     {
710       sctp_ipv6_addr_param_t *ipv6_addr =
711         (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
712
713       ipv6_addr->param_hdr.type =
714         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
715       ipv6_addr->param_hdr.length =
716         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
717       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
718       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
719
720       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
721     }
722
723   if (sctp_conn->sub_conn[idx].connection.is_ip4)
724     {
725       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
726       ip4_param->address.as_u32 =
727         sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
728
729       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
730     }
731   else
732     {
733       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
734       ip6_param->address.as_u64[0] =
735         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
736       ip6_param->address.as_u64[1] =
737         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
738
739       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
740     }
741
742   /* src_port & dst_port are already in network byte-order */
743   init_ack_chunk->sctp_hdr.checksum = 0;
744   init_ack_chunk->sctp_hdr.src_port =
745     sctp_conn->sub_conn[idx].connection.lcl_port;
746   init_ack_chunk->sctp_hdr.dst_port =
747     sctp_conn->sub_conn[idx].connection.rmt_port;
748   /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
749   init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
750   init_ack_chunk->initial_tsn =
751     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
752   SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
753                           init_ack_chunk->initial_tsn);
754
755   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
756   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
757
758   init_ack_chunk->initiate_tag = sctp_conn->local_tag;
759
760   init_ack_chunk->a_rwnd =
761     clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
762   init_ack_chunk->inboud_streams_count =
763     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
764   init_ack_chunk->outbound_streams_count =
765     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
766
767   vnet_buffer (b)->sctp.connection_index =
768     sctp_conn->sub_conn[idx].connection.c_index;
769   vnet_buffer (b)->sctp.subconn_idx = idx;
770 }
771
772 /**
773  * Convert buffer to INIT-ACK
774  */
775 void
776 sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, u8 idx,
777                             vlib_buffer_t * b, ip4_address_t * ip4_addr,
778                             ip6_address_t * ip6_addr)
779 {
780   vlib_main_t *vm = vlib_get_main ();
781   sctp_ipv4_addr_param_t *ip4_param = 0;
782   sctp_ipv6_addr_param_t *ip6_param = 0;
783   u32 random_seed = random_default_seed ();
784
785   sctp_reuse_buffer (vm, b);
786
787   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
788   u16 alloc_bytes =
789     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
790
791   if (PREDICT_TRUE (ip4_addr != NULL))
792     {
793       /* Create room for variable-length fields in the INIT_ACK chunk */
794       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
795     }
796   if (PREDICT_TRUE (ip6_addr != NULL))
797     {
798       /* Create room for variable-length fields in the INIT_ACK chunk */
799       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
800     }
801
802   if (sctp_conn->sub_conn[idx].connection.is_ip4)
803     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
804   else
805     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
806
807   /* As per RFC 4960 the chunk_length value does NOT contemplate
808    * the size of the first header (see sctp_header_t) and any padding
809    */
810   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
811
812   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
813
814   sctp_init_ack_chunk_t *init_ack_chunk =
815     vlib_buffer_push_uninit (b, alloc_bytes);
816
817   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
818
819   /* Create State Cookie parameter */
820   sctp_state_cookie_param_t *state_cookie_param =
821     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
822
823   state_cookie_param->param_hdr.type =
824     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
825   state_cookie_param->param_hdr.length =
826     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
827   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
828   state_cookie_param->cookie_lifespan =
829     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
830
831   sctp_compute_mac (sctp_conn, state_cookie_param);
832
833   pointer_offset += sizeof (sctp_state_cookie_param_t);
834
835   if (PREDICT_TRUE (ip4_addr != NULL))
836     {
837       sctp_ipv4_addr_param_t *ipv4_addr =
838         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
839
840       ipv4_addr->param_hdr.type =
841         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
842       ipv4_addr->param_hdr.length =
843         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
844       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
845
846       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
847     }
848   if (PREDICT_TRUE (ip6_addr != NULL))
849     {
850       sctp_ipv6_addr_param_t *ipv6_addr =
851         (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
852
853       ipv6_addr->param_hdr.type =
854         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
855       ipv6_addr->param_hdr.length =
856         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
857       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
858       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
859
860       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
861     }
862
863   if (sctp_conn->sub_conn[idx].connection.is_ip4)
864     {
865       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
866       ip4_param->address.as_u32 =
867         sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
868
869       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
870     }
871   else
872     {
873       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
874       ip6_param->address.as_u64[0] =
875         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
876       ip6_param->address.as_u64[1] =
877         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
878
879       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
880     }
881
882   /* src_port & dst_port are already in network byte-order */
883   init_ack_chunk->sctp_hdr.checksum = 0;
884   init_ack_chunk->sctp_hdr.src_port =
885     sctp_conn->sub_conn[idx].connection.lcl_port;
886   init_ack_chunk->sctp_hdr.dst_port =
887     sctp_conn->sub_conn[idx].connection.rmt_port;
888   /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
889   init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
890   init_ack_chunk->initial_tsn =
891     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
892   SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
893                           init_ack_chunk->initial_tsn);
894
895   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
896   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
897
898   init_ack_chunk->initiate_tag =
899     clib_host_to_net_u32 (random_u32 (&random_seed));
900
901   init_ack_chunk->a_rwnd =
902     clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
903   init_ack_chunk->inboud_streams_count =
904     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
905   init_ack_chunk->outbound_streams_count =
906     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
907
908   sctp_conn->local_tag = init_ack_chunk->initiate_tag;
909
910   vnet_buffer (b)->sctp.connection_index =
911     sctp_conn->sub_conn[idx].connection.c_index;
912   vnet_buffer (b)->sctp.subconn_idx = idx;
913 }
914
915 /**
916  * Convert buffer to SHUTDOWN
917  */
918 void
919 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, u8 idx,
920                              vlib_buffer_t * b)
921 {
922   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
923
924   /* As per RFC 4960 the chunk_length value does NOT contemplate
925    * the size of the first header (see sctp_header_t) and any padding
926    */
927   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
928
929   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
930
931   sctp_shutdown_association_chunk_t *shutdown_chunk =
932     vlib_buffer_push_uninit (b, alloc_bytes);
933
934   shutdown_chunk->sctp_hdr.checksum = 0;
935   /* No need of host_to_net conversion, already in net-byte order */
936   shutdown_chunk->sctp_hdr.src_port =
937     sctp_conn->sub_conn[idx].connection.lcl_port;
938   shutdown_chunk->sctp_hdr.dst_port =
939     sctp_conn->sub_conn[idx].connection.rmt_port;
940   shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
941   vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
942   vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
943
944   shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
945
946   vnet_buffer (b)->sctp.connection_index =
947     sctp_conn->sub_conn[idx].connection.c_index;
948   vnet_buffer (b)->sctp.subconn_idx = idx;
949 }
950
951 /*
952  * Send SHUTDOWN
953  */
954 void
955 sctp_send_shutdown (sctp_connection_t * sctp_conn)
956 {
957   vlib_buffer_t *b;
958   u32 bi;
959   sctp_main_t *tm = vnet_get_sctp_main ();
960   vlib_main_t *vm = vlib_get_main ();
961
962   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
963     return;
964
965   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
966     return;
967
968   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
969
970   b = vlib_get_buffer (vm, bi);
971   sctp_init_buffer (vm, b);
972   sctp_prepare_shutdown_chunk (sctp_conn, idx, b);
973
974   sctp_enqueue_to_output_now (vm, b, bi,
975                               sctp_conn->sub_conn[idx].connection.is_ip4);
976 }
977
978 /**
979  * Convert buffer to SHUTDOWN_ACK
980  */
981 void
982 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
983                                  vlib_buffer_t * b)
984 {
985   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
986   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
987
988   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
989
990   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
991     vlib_buffer_push_uninit (b, alloc_bytes);
992
993   shutdown_ack_chunk->sctp_hdr.checksum = 0;
994   /* No need of host_to_net conversion, already in net-byte order */
995   shutdown_ack_chunk->sctp_hdr.src_port =
996     sctp_conn->sub_conn[idx].connection.lcl_port;
997   shutdown_ack_chunk->sctp_hdr.dst_port =
998     sctp_conn->sub_conn[idx].connection.rmt_port;
999   shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1000
1001   vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
1002   vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
1003
1004   vnet_buffer (b)->sctp.connection_index =
1005     sctp_conn->sub_conn[idx].connection.c_index;
1006   vnet_buffer (b)->sctp.subconn_idx = idx;
1007 }
1008
1009 /*
1010  * Send SHUTDOWN_ACK
1011  */
1012 void
1013 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, u8 idx,
1014                         vlib_buffer_t * b)
1015 {
1016   vlib_main_t *vm = vlib_get_main ();
1017
1018   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1019     return;
1020
1021   sctp_reuse_buffer (vm, b);
1022
1023   sctp_prepare_shutdown_ack_chunk (sctp_conn, idx, b);
1024 }
1025
1026 /**
1027  * Convert buffer to SACK
1028  */
1029 void
1030 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1031                          vlib_buffer_t * b)
1032 {
1033   vlib_main_t *vm = vlib_get_main ();
1034
1035   sctp_reuse_buffer (vm, b);
1036
1037   u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
1038
1039   /* As per RFC 4960 the chunk_length value does NOT contemplate
1040    * the size of the first header (see sctp_header_t) and any padding
1041    */
1042   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1043
1044   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1045
1046   sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
1047
1048   sack->sctp_hdr.checksum = 0;
1049   sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1050   sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1051   sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1052   vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
1053   vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
1054
1055   sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
1056
1057   sctp_conn->ack_state = 0;
1058
1059   vnet_buffer (b)->sctp.connection_index =
1060     sctp_conn->sub_conn[idx].connection.c_index;
1061   vnet_buffer (b)->sctp.subconn_idx = idx;
1062 }
1063
1064 /**
1065  * Convert buffer to HEARTBEAT_ACK
1066  */
1067 void
1068 sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1069                                   vlib_buffer_t * b)
1070 {
1071   vlib_main_t *vm = vlib_get_main ();
1072
1073   u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
1074
1075   sctp_reuse_buffer (vm, b);
1076
1077   /* As per RFC 4960 the chunk_length value does NOT contemplate
1078    * the size of the first header (see sctp_header_t) and any padding
1079    */
1080   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1081
1082   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1083
1084   sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
1085
1086   hb_ack->sctp_hdr.checksum = 0;
1087   /* No need of host_to_net conversion, already in net-byte order */
1088   hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1089   hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1090   hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1091   hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1092   hb_ack->hb_info.param_hdr.length =
1093     clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
1094
1095   vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
1096   vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
1097
1098   vnet_buffer (b)->sctp.connection_index =
1099     sctp_conn->sub_conn[idx].connection.c_index;
1100   vnet_buffer (b)->sctp.subconn_idx = idx;
1101 }
1102
1103 /**
1104  * Convert buffer to HEARTBEAT
1105  */
1106 void
1107 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn, u8 idx,
1108                               vlib_buffer_t * b)
1109 {
1110   u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
1111
1112   /* As per RFC 4960 the chunk_length value does NOT contemplate
1113    * the size of the first header (see sctp_header_t) and any padding
1114    */
1115   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1116
1117   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1118
1119   sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
1120
1121   hb_req->sctp_hdr.checksum = 0;
1122   /* No need of host_to_net conversion, already in net-byte order */
1123   hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1124   hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1125   hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1126   hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1127   hb_req->hb_info.param_hdr.length =
1128     clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
1129
1130   vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
1131   vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
1132
1133   vnet_buffer (b)->sctp.connection_index =
1134     sctp_conn->sub_conn[idx].connection.c_index;
1135   vnet_buffer (b)->sctp.subconn_idx = idx;
1136 }
1137
1138 void
1139 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
1140 {
1141   vlib_buffer_t *b;
1142   u32 bi;
1143   sctp_main_t *tm = vnet_get_sctp_main ();
1144   vlib_main_t *vm = vlib_get_main ();
1145
1146   u8 i;
1147   u32 now = sctp_time_now ();
1148
1149   for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
1150     {
1151       if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
1152         continue;
1153
1154       if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
1155         {
1156           if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1157             return;
1158
1159           b = vlib_get_buffer (vm, bi);
1160           sctp_init_buffer (vm, b);
1161           sctp_prepare_heartbeat_chunk (sctp_conn, i, b);
1162
1163           sctp_enqueue_to_output_now (vm, b, bi,
1164                                       sctp_conn->sub_conn[i].
1165                                       connection.is_ip4);
1166
1167           sctp_conn->sub_conn[i].unacknowledged_hb += 1;
1168         }
1169     }
1170 }
1171
1172 /**
1173  * Convert buffer to SHUTDOWN_COMPLETE
1174  */
1175 void
1176 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn, u8 idx,
1177                                       vlib_buffer_t * b)
1178 {
1179   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1180   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1181
1182   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1183
1184   sctp_shutdown_complete_chunk_t *shutdown_complete =
1185     vlib_buffer_push_uninit (b, alloc_bytes);
1186
1187   shutdown_complete->sctp_hdr.checksum = 0;
1188   /* No need of host_to_net conversion, already in net-byte order */
1189   shutdown_complete->sctp_hdr.src_port =
1190     sctp_conn->sub_conn[idx].connection.lcl_port;
1191   shutdown_complete->sctp_hdr.dst_port =
1192     sctp_conn->sub_conn[idx].connection.rmt_port;
1193   shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1194
1195   vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1196   vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1197
1198   vnet_buffer (b)->sctp.connection_index =
1199     sctp_conn->sub_conn[idx].connection.c_index;
1200   vnet_buffer (b)->sctp.subconn_idx = idx;
1201 }
1202
1203 void
1204 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn, u8 idx,
1205                              vlib_buffer_t * b0)
1206 {
1207   vlib_main_t *vm = vlib_get_main ();
1208
1209   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1210     return;
1211
1212   sctp_reuse_buffer (vm, b0);
1213
1214   sctp_prepare_shutdown_complete_chunk (sctp_conn, idx, b0);
1215 }
1216
1217 /*
1218  *  Send INIT
1219  */
1220 void
1221 sctp_send_init (sctp_connection_t * sctp_conn)
1222 {
1223   vlib_buffer_t *b;
1224   u32 bi;
1225   sctp_main_t *tm = vnet_get_sctp_main ();
1226   vlib_main_t *vm = vlib_get_main ();
1227
1228   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1229     return;
1230
1231   b = vlib_get_buffer (vm, bi);
1232   u8 idx = MAIN_SCTP_SUB_CONN_IDX;
1233
1234   sctp_init_buffer (vm, b);
1235   sctp_prepare_init_chunk (sctp_conn, idx, b);
1236
1237   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1238   sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
1239
1240   /* Start the T1_INIT timer */
1241   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1242                   sctp_conn->sub_conn[idx].RTO);
1243
1244   /* Change state to COOKIE_WAIT */
1245   sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
1246
1247   /* Measure RTT with this */
1248   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1249 }
1250
1251 /**
1252  * Push SCTP header and update connection variables
1253  */
1254 static void
1255 sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
1256                  sctp_state_t next_state)
1257 {
1258   u16 data_len =
1259     b->current_length + b->total_length_not_including_first_buffer;
1260   ASSERT (!b->total_length_not_including_first_buffer
1261           || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1262
1263   SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1264                        "b->current_data = %p "
1265                        "data_len = %u",
1266                        b->current_length, b->current_data, data_len);
1267
1268   u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1269   u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1270
1271   bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1272
1273   sctp_payload_data_chunk_t *data_chunk =
1274     vlib_buffer_push_uninit (b, bytes_to_add);
1275
1276   u8 idx = sctp_data_subconn_select (sctp_conn);
1277   SCTP_DBG_OUTPUT
1278     ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
1279      sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1280      sctp_conn->sub_conn[idx].connection.c_index,
1281      sctp_conn->sub_conn[idx].connection.lcl_port,
1282      sctp_conn->sub_conn[idx].connection.rmt_port);
1283   data_chunk->sctp_hdr.checksum = 0;
1284   data_chunk->sctp_hdr.src_port =
1285     sctp_conn->sub_conn[idx].connection.lcl_port;
1286   data_chunk->sctp_hdr.dst_port =
1287     sctp_conn->sub_conn[idx].connection.rmt_port;
1288   data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1289
1290   data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
1291   data_chunk->stream_id = clib_host_to_net_u16 (0);
1292   data_chunk->stream_seq = clib_host_to_net_u16 (0);
1293
1294   vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1295   vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1296
1297   vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1298   vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1299
1300   SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1301                        b->data, b->current_data);
1302
1303   sctp_conn->last_unacked_tsn = sctp_conn->next_tsn;
1304   sctp_conn->next_tsn += data_len;
1305
1306   u32 inflight = sctp_conn->next_tsn - sctp_conn->last_unacked_tsn;
1307   /* Section 7.2.2; point (3) */
1308   if (sctp_conn->sub_conn[idx].partially_acked_bytes >=
1309       sctp_conn->sub_conn[idx].cwnd
1310       && inflight >= sctp_conn->sub_conn[idx].cwnd)
1311     {
1312       sctp_conn->sub_conn[idx].cwnd += sctp_conn->sub_conn[idx].PMTU;
1313       sctp_conn->sub_conn[idx].partially_acked_bytes -=
1314         sctp_conn->sub_conn[idx].cwnd;
1315     }
1316
1317   sctp_conn->sub_conn[idx].last_data_ts = sctp_time_now ();
1318
1319   vnet_buffer (b)->sctp.connection_index =
1320     sctp_conn->sub_conn[idx].connection.c_index;
1321
1322   vnet_buffer (b)->sctp.subconn_idx = idx;
1323 }
1324
1325 u32
1326 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
1327 {
1328   sctp_connection_t *sctp_conn =
1329     sctp_get_connection_from_transport (trans_conn);
1330
1331   SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, "
1332                    "S_INDEX = %u, C_INDEX = %u,"
1333                    "trans_conn->LCL_PORT = %u, trans_conn->RMT_PORT = %u",
1334                    trans_conn,
1335                    sctp_conn,
1336                    trans_conn->s_index,
1337                    trans_conn->c_index,
1338                    trans_conn->lcl_port, trans_conn->rmt_port);
1339
1340   sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
1341
1342   sctp_trajectory_add_start (b0, 3);
1343
1344   return 0;
1345 }
1346
1347 #if SCTP_DEBUG_STATE_MACHINE
1348 always_inline u8
1349 sctp_validate_output_state_machine (sctp_connection_t * sctp_conn,
1350                                     u8 chunk_type)
1351 {
1352   u8 result = 0;
1353   switch (sctp_conn->state)
1354     {
1355     case SCTP_STATE_CLOSED:
1356       if (chunk_type != INIT && chunk_type != INIT_ACK)
1357         result = 1;
1358       break;
1359     case SCTP_STATE_ESTABLISHED:
1360       if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1361           chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1362           chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1363         result = 1;
1364       break;
1365     case SCTP_STATE_COOKIE_WAIT:
1366       if (chunk_type != COOKIE_ECHO)
1367         result = 1;
1368       break;
1369     case SCTP_STATE_SHUTDOWN_SENT:
1370       if (chunk_type != SHUTDOWN_COMPLETE)
1371         result = 1;
1372       break;
1373     case SCTP_STATE_SHUTDOWN_RECEIVED:
1374       if (chunk_type != SHUTDOWN_ACK)
1375         result = 1;
1376       break;
1377     }
1378   return result;
1379 }
1380 #endif
1381
1382 always_inline u8
1383 sctp_is_retransmitting (sctp_connection_t * sctp_conn, u8 idx)
1384 {
1385   return sctp_conn->sub_conn[idx].is_retransmitting;
1386 }
1387
1388 always_inline uword
1389 sctp46_output_inline (vlib_main_t * vm,
1390                       vlib_node_runtime_t * node,
1391                       vlib_frame_t * from_frame, int is_ip4)
1392 {
1393   u32 n_left_from, next_index, *from, *to_next;
1394   u32 my_thread_index = vm->thread_index;
1395
1396   from = vlib_frame_vector_args (from_frame);
1397   n_left_from = from_frame->n_vectors;
1398   next_index = node->cached_next_index;
1399   sctp_set_time_now (my_thread_index);
1400
1401   while (n_left_from > 0)
1402     {
1403       u32 n_left_to_next;
1404
1405       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1406
1407       while (n_left_from > 0 && n_left_to_next > 0)
1408         {
1409           u32 bi0;
1410           vlib_buffer_t *b0;
1411           sctp_header_t *sctp_hdr = 0;
1412           sctp_connection_t *sctp_conn;
1413           sctp_tx_trace_t *t0;
1414           sctp_header_t *th0 = 0;
1415           u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1416             SCTP_OUTPUT_NEXT_IP_LOOKUP;
1417
1418 #if SCTP_DEBUG_STATE_MACHINE
1419           u16 packet_length = 0;
1420 #endif
1421
1422           bi0 = from[0];
1423           to_next[0] = bi0;
1424           from += 1;
1425           to_next += 1;
1426           n_left_from -= 1;
1427           n_left_to_next -= 1;
1428
1429           b0 = vlib_get_buffer (vm, bi0);
1430
1431           sctp_conn =
1432             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1433                                  my_thread_index);
1434
1435           if (PREDICT_FALSE (sctp_conn == 0))
1436             {
1437               error0 = SCTP_ERROR_INVALID_CONNECTION;
1438               next0 = SCTP_OUTPUT_NEXT_DROP;
1439               goto done;
1440             }
1441
1442           u8 idx = vnet_buffer (b0)->sctp.subconn_idx;
1443
1444           th0 = vlib_buffer_get_current (b0);
1445
1446           if (is_ip4)
1447             {
1448               ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1449                                                          b0,
1450                                                          &sctp_conn->sub_conn
1451                                                          [idx].connection.
1452                                                          lcl_ip.ip4,
1453                                                          &sctp_conn->
1454                                                          sub_conn
1455                                                          [idx].connection.
1456                                                          rmt_ip.ip4,
1457                                                          IP_PROTOCOL_SCTP, 1);
1458
1459               u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1460
1461               sctp_hdr = ip4_next_header (iph4);
1462               sctp_hdr->checksum = checksum;
1463
1464               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1465
1466 #if SCTP_DEBUG_STATE_MACHINE
1467               packet_length = clib_net_to_host_u16 (iph4->length);
1468 #endif
1469             }
1470           else
1471             {
1472               ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1473                                                          b0,
1474                                                          &sctp_conn->sub_conn
1475                                                          [idx].
1476                                                          connection.lcl_ip.
1477                                                          ip6,
1478                                                          &sctp_conn->sub_conn
1479                                                          [idx].
1480                                                          connection.rmt_ip.
1481                                                          ip6,
1482                                                          IP_PROTOCOL_SCTP);
1483
1484               int bogus = ~0;
1485               u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1486               ASSERT (!bogus);
1487
1488               sctp_hdr = ip6_next_header (iph6);
1489               sctp_hdr->checksum = checksum;
1490
1491               vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1492               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1493
1494 #if SCTP_DEBUG_STATE_MACHINE
1495               packet_length = clib_net_to_host_u16 (iph6->payload_length);
1496 #endif
1497             }
1498
1499           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1500           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1501           if (chunk_type >= UNKNOWN)
1502             {
1503               clib_warning
1504                 ("Trying to send an unrecognized chunk... something is really bad.");
1505               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1506               next0 = SCTP_OUTPUT_NEXT_DROP;
1507               goto done;
1508             }
1509
1510 #if SCTP_DEBUG_STATE_MACHINE
1511           u8 is_valid =
1512             (sctp_conn->sub_conn[idx].connection.lcl_port ==
1513              sctp_hdr->src_port
1514              || sctp_conn->sub_conn[idx].connection.lcl_port ==
1515              sctp_hdr->dst_port)
1516             && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1517                 sctp_hdr->dst_port
1518                 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1519                 sctp_hdr->src_port);
1520
1521           if (!is_valid)
1522             {
1523               SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1524                                       "packet_length = %u, "
1525                                       "chunk_type = %u [%s], "
1526                                       "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1527                                       "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1528                                       sctp_conn->sub_conn[idx].
1529                                       connection.c_index, packet_length,
1530                                       chunk_type,
1531                                       sctp_chunk_to_string (chunk_type),
1532                                       sctp_conn->sub_conn[idx].
1533                                       connection.lcl_port, sctp_hdr->src_port,
1534                                       sctp_conn->sub_conn[idx].
1535                                       connection.rmt_port,
1536                                       sctp_hdr->dst_port);
1537
1538               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1539               next0 = SCTP_OUTPUT_NEXT_DROP;
1540               goto done;
1541             }
1542 #endif
1543           SCTP_DBG_STATE_MACHINE
1544             ("SESSION_INDEX = %u, CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1545              "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1546              sctp_conn->sub_conn[idx].connection.s_index,
1547              sctp_conn->sub_conn[idx].connection.c_index,
1548              sctp_conn->state, sctp_state_to_string (sctp_conn->state),
1549              sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1550              full_hdr->hdr.dst_port);
1551
1552           /* Let's make sure the state-machine does not send anything crazy */
1553 #if SCTP_DEBUG_STATE_MACHINE
1554           if (sctp_validate_output_state_machine (sctp_conn, chunk_type) != 0)
1555             {
1556               SCTP_DBG_STATE_MACHINE
1557                 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1558                  sctp_chunk_to_string (chunk_type),
1559                  sctp_state_to_string (sctp_conn->state));
1560
1561               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1562               next0 = SCTP_OUTPUT_NEXT_DROP;
1563               goto done;
1564
1565             }
1566 #endif
1567
1568           /* Karn's algorithm: RTT measurements MUST NOT be made using
1569            * packets that were retransmitted
1570            */
1571           if (!sctp_is_retransmitting (sctp_conn, idx))
1572             {
1573               /* Measure RTT with this */
1574               if (chunk_type == DATA
1575                   && sctp_conn->sub_conn[idx].RTO_pending == 0)
1576                 {
1577                   sctp_conn->sub_conn[idx].RTO_pending = 1;
1578                   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1579                 }
1580               else
1581                 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1582             }
1583
1584           /* Let's take care of TIMERS */
1585           switch (chunk_type)
1586             {
1587             case COOKIE_ECHO:
1588               {
1589                 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1590                 break;
1591               }
1592             case DATA:
1593               {
1594                 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1595
1596                 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1597                                    sctp_conn->sub_conn[idx].RTO);
1598                 break;
1599               }
1600             case SHUTDOWN:
1601               {
1602                 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1603                 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1604                                 sctp_conn->sub_conn[idx].RTO);
1605                 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1606                 break;
1607               }
1608             case SHUTDOWN_ACK:
1609               {
1610                 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1611                 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1612                                 sctp_conn->sub_conn[idx].RTO);
1613                 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1614                 break;
1615               }
1616             case SHUTDOWN_COMPLETE:
1617               {
1618                 sctp_conn->state = SCTP_STATE_CLOSED;
1619                 break;
1620               }
1621             }
1622
1623           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1624           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1625
1626           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1627
1628           SCTP_DBG_STATE_MACHINE
1629             ("SESSION_INDEX = %u, CONNECTION_INDEX = %u, " "NEW_STATE = %s, "
1630              "CHUNK_SENT = %s", sctp_conn->sub_conn[idx].connection.s_index,
1631              sctp_conn->sub_conn[idx].connection.c_index,
1632              sctp_state_to_string (sctp_conn->state),
1633              sctp_chunk_to_string (chunk_type));
1634
1635           vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1636
1637         done:
1638           b0->error = node->errors[error0];
1639           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1640             {
1641               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1642               if (th0)
1643                 {
1644                   clib_memcpy (&t0->sctp_header, th0,
1645                                sizeof (t0->sctp_header));
1646                 }
1647               else
1648                 {
1649                   memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1650                 }
1651               clib_memcpy (&t0->sctp_connection, sctp_conn,
1652                            sizeof (t0->sctp_connection));
1653             }
1654
1655           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1656                                            n_left_to_next, bi0, next0);
1657         }
1658
1659       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1660     }
1661
1662   return from_frame->n_vectors;
1663 }
1664
1665 static uword
1666 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1667               vlib_frame_t * from_frame)
1668 {
1669   return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1670 }
1671
1672 static uword
1673 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1674               vlib_frame_t * from_frame)
1675 {
1676   return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1677 }
1678
1679 /* *INDENT-OFF* */
1680 VLIB_REGISTER_NODE (sctp4_output_node) =
1681 {
1682   .function = sctp4_output,.name = "sctp4-output",
1683     /* Takes a vector of packets. */
1684     .vector_size = sizeof (u32),
1685     .n_errors = SCTP_N_ERROR,
1686     .error_strings = sctp_error_strings,
1687     .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1688     .next_nodes = {
1689 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1690     foreach_sctp4_output_next
1691 #undef _
1692     },
1693     .format_buffer = format_sctp_header,
1694     .format_trace = format_sctp_tx_trace,
1695 };
1696 /* *INDENT-ON* */
1697
1698 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1699
1700 /* *INDENT-OFF* */
1701 VLIB_REGISTER_NODE (sctp6_output_node) =
1702 {
1703   .function = sctp6_output,
1704   .name = "sctp6-output",
1705     /* Takes a vector of packets. */
1706   .vector_size = sizeof (u32),
1707   .n_errors = SCTP_N_ERROR,
1708   .error_strings = sctp_error_strings,
1709   .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1710   .next_nodes = {
1711 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1712     foreach_sctp6_output_next
1713 #undef _
1714   },
1715   .format_buffer = format_sctp_header,
1716   .format_trace = format_sctp_tx_trace,
1717 };
1718 /* *INDENT-ON* */
1719
1720 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1721
1722 /*
1723  * fd.io coding-style-patch-verification: ON
1724  *
1725  * Local Variables:
1726  * eval: (c-set-style "gnu")
1727  * End:
1728  */