SCTP: fix corrupted buffers seen in output node
[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
265   /* Leave enough space for headers */
266   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
267 }
268
269 always_inline void *
270 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
271 {
272   ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
273   b->flags &= VLIB_BUFFER_NON_DEFAULT_FREELIST;
274   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
275   b->total_length_not_including_first_buffer = 0;
276   vnet_buffer (b)->sctp.flags = 0;
277   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
278   /* Leave enough space for headers */
279   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
280 }
281
282 always_inline int
283 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
284 {
285   vlib_main_t *vm = vlib_get_main ();
286   u32 current_length = vec_len (tm->tx_buffers[thread_index]);
287   u32 n_allocated;
288
289   vec_validate (tm->tx_buffers[thread_index],
290                 current_length + n_free_buffers - 1);
291   n_allocated =
292     vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
293                        n_free_buffers);
294   _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
295   /* buffer shortage, report failure */
296   if (vec_len (tm->tx_buffers[thread_index]) == 0)
297     {
298       clib_warning ("out of buffers");
299       return -1;
300     }
301   return 0;
302 }
303
304 always_inline int
305 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
306 {
307   u32 *my_tx_buffers;
308   u32 thread_index = vlib_get_thread_index ();
309   if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
310     {
311       if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
312         return -1;
313     }
314   my_tx_buffers = tm->tx_buffers[thread_index];
315   *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
316   _vec_len (my_tx_buffers) -= 1;
317   return 0;
318 }
319
320 always_inline void
321 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
322                           u8 is_ip4, u8 flush)
323 {
324   sctp_main_t *tm = vnet_get_sctp_main ();
325   u32 thread_index = vlib_get_thread_index ();
326   u32 *to_next, next_index;
327   vlib_frame_t *f;
328
329   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
330   b->error = 0;
331
332   /* Decide where to send the packet */
333   next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
334   sctp_trajectory_add_start (b, 2);
335
336   /* Get frame to v4/6 output node */
337   f = tm->tx_frames[!is_ip4][thread_index];
338   if (!f)
339     {
340       f = vlib_get_frame_to_node (vm, next_index);
341       ASSERT (f);
342       tm->tx_frames[!is_ip4][thread_index] = f;
343     }
344   to_next = vlib_frame_vector_args (f);
345   to_next[f->n_vectors] = bi;
346   f->n_vectors += 1;
347   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
348     {
349       vlib_put_frame_to_node (vm, next_index, f);
350       tm->tx_frames[!is_ip4][thread_index] = 0;
351     }
352 }
353
354 always_inline void
355 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
356                             u8 is_ip4)
357 {
358   sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
359 }
360
361 always_inline void
362 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
363                              u8 is_ip4, u8 flush)
364 {
365   sctp_main_t *tm = vnet_get_sctp_main ();
366   u32 thread_index = vlib_get_thread_index ();
367   u32 *to_next, next_index;
368   vlib_frame_t *f;
369
370   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
371   b->error = 0;
372
373   /* Default FIB for now */
374   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
375
376   /* Send to IP lookup */
377   next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
378   if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
379     {
380       b->pre_data[0] = 2;
381       b->pre_data[1] = next_index;
382     }
383
384   f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
385   if (!f)
386     {
387       f = vlib_get_frame_to_node (vm, next_index);
388       ASSERT (f);
389       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
390     }
391
392   to_next = vlib_frame_vector_args (f);
393   to_next[f->n_vectors] = bi;
394   f->n_vectors += 1;
395   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
396     {
397       vlib_put_frame_to_node (vm, next_index, f);
398       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
399     }
400 }
401
402 always_inline void
403 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
404                            u8 is_ip4)
405 {
406   sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
407 }
408
409 /**
410  * Convert buffer to INIT
411  */
412 void
413 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
414 {
415   u32 random_seed = random_default_seed ();
416   u16 alloc_bytes = sizeof (sctp_init_chunk_t);
417   sctp_sub_connection_t *sub_conn =
418     &sctp_conn->sub_conn[sctp_pick_conn_idx_on_chunk (INIT)];
419
420   sctp_ipv4_addr_param_t *ip4_param = 0;
421   sctp_ipv6_addr_param_t *ip6_param = 0;
422
423   if (sub_conn->c_is_ip4)
424     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
425   else
426     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
427
428   /* As per RFC 4960 the chunk_length value does NOT contemplate
429    * the size of the first header (see sctp_header_t) and any padding
430    */
431   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
432
433   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
434
435   sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
436
437   u16 pointer_offset = sizeof (init_chunk);
438   if (sub_conn->c_is_ip4)
439     {
440       ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
441       ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
442
443       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
444     }
445   else
446     {
447       ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
448       ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
449       ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
450
451       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
452     }
453
454   init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
455   init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
456   init_chunk->sctp_hdr.checksum = 0;
457   /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
458   init_chunk->sctp_hdr.verification_tag = 0x0;
459
460   vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
461   vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
462   vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
463
464   init_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
465   init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
466   init_chunk->inboud_streams_count =
467     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
468   init_chunk->outbound_streams_count =
469     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
470
471   init_chunk->initial_tsn =
472     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
473   SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
474                           sctp_conn->local_initial_tsn);
475
476   sctp_conn->local_tag = init_chunk->initiate_tag;
477
478   vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
479
480   SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
481                           "CHUNK_TYPE = %s, "
482                           "SRC_PORT = %u, DST_PORT = %u",
483                           sub_conn->connection.c_index,
484                           sctp_conn->state,
485                           sctp_state_to_string (sctp_conn->state),
486                           sctp_chunk_to_string (INIT),
487                           init_chunk->sctp_hdr.src_port,
488                           init_chunk->sctp_hdr.dst_port);
489 }
490
491 void
492 sctp_compute_mac (sctp_connection_t * sctp_conn,
493                   sctp_state_cookie_param_t * state_cookie)
494 {
495 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
496   HMAC_CTX *ctx;
497 #else
498   HMAC_CTX ctx;
499 #endif
500   unsigned int len = 0;
501   const EVP_MD *md = EVP_sha1 ();
502 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
503   ctx = HMAC_CTX_new ();
504   HMAC_Init_ex (ctx, &state_cookie->creation_time,
505                 sizeof (state_cookie->creation_time), md, NULL);
506   HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
507   HMAC_Final (ctx, state_cookie->mac, &len);
508 #else
509   HMAC_CTX_init (&ctx);
510   HMAC_Init_ex (&ctx, &state_cookie->creation_time,
511                 sizeof (state_cookie->creation_time), md, NULL);
512   HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
513   HMAC_Final (&ctx, state_cookie->mac, &len);
514   HMAC_CTX_cleanup (&ctx);
515 #endif
516
517   ENDIANESS_SWAP (state_cookie->mac);
518 }
519
520 void
521 sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn,
522                                vlib_buffer_t * b)
523 {
524   vlib_main_t *vm = vlib_get_main ();
525   u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ACK);
526
527   sctp_reuse_buffer (vm, b);
528
529   u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
530
531   /* As per RFC 4960 the chunk_length value does NOT contemplate
532    * the size of the first header (see sctp_header_t) and any padding
533    */
534   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
535
536   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
537
538   sctp_cookie_ack_chunk_t *cookie_ack_chunk =
539     vlib_buffer_push_uninit (b, alloc_bytes);
540
541   cookie_ack_chunk->sctp_hdr.checksum = 0;
542   cookie_ack_chunk->sctp_hdr.src_port =
543     sctp_conn->sub_conn[idx].connection.lcl_port;
544   cookie_ack_chunk->sctp_hdr.dst_port =
545     sctp_conn->sub_conn[idx].connection.rmt_port;
546   cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
547   vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
548   vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
549
550   vnet_buffer (b)->sctp.connection_index =
551     sctp_conn->sub_conn[idx].connection.c_index;
552 }
553
554 void
555 sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn,
556                                 vlib_buffer_t * b,
557                                 sctp_state_cookie_param_t * sc)
558 {
559   vlib_main_t *vm = vlib_get_main ();
560   u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ECHO);
561
562   sctp_reuse_buffer (vm, b);
563
564   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
565   u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
566   /* As per RFC 4960 the chunk_length value does NOT contemplate
567    * the size of the first header (see sctp_header_t) and any padding
568    */
569   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
570   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
571   sctp_cookie_echo_chunk_t *cookie_echo_chunk =
572     vlib_buffer_push_uninit (b, alloc_bytes);
573   cookie_echo_chunk->sctp_hdr.checksum = 0;
574   cookie_echo_chunk->sctp_hdr.src_port =
575     sctp_conn->sub_conn[idx].connection.lcl_port;
576   cookie_echo_chunk->sctp_hdr.dst_port =
577     sctp_conn->sub_conn[idx].connection.rmt_port;
578   cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
579   vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
580   vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
581   clib_memcpy (&(cookie_echo_chunk->cookie), sc,
582                sizeof (sctp_state_cookie_param_t));
583
584   vnet_buffer (b)->sctp.connection_index =
585     sctp_conn->sub_conn[idx].connection.c_index;
586 }
587
588 /**
589  * Convert buffer to INIT-ACK
590  */
591 void
592 sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
593                             ip4_address_t * ip4_addr,
594                             ip6_address_t * ip6_addr)
595 {
596   vlib_main_t *vm = vlib_get_main ();
597   sctp_ipv4_addr_param_t *ip4_param = 0;
598   sctp_ipv6_addr_param_t *ip6_param = 0;
599   u8 idx = sctp_pick_conn_idx_on_chunk (INIT_ACK);
600   u32 random_seed = random_default_seed ();
601
602   sctp_reuse_buffer (vm, b);
603
604   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
605   u16 alloc_bytes =
606     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
607
608   if (PREDICT_TRUE (ip4_addr != NULL))
609     {
610       /* Create room for variable-length fields in the INIT_ACK chunk */
611       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
612     }
613   if (PREDICT_TRUE (ip6_addr != NULL))
614     {
615       /* Create room for variable-length fields in the INIT_ACK chunk */
616       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
617     }
618
619   if (sctp_conn->sub_conn[idx].connection.is_ip4)
620     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
621   else
622     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
623
624   /* As per RFC 4960 the chunk_length value does NOT contemplate
625    * the size of the first header (see sctp_header_t) and any padding
626    */
627   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
628
629   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
630
631   sctp_init_ack_chunk_t *init_ack_chunk =
632     vlib_buffer_push_uninit (b, alloc_bytes);
633
634   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
635
636   /* Create State Cookie parameter */
637   sctp_state_cookie_param_t *state_cookie_param =
638     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
639
640   state_cookie_param->param_hdr.type =
641     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
642   state_cookie_param->param_hdr.length =
643     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
644   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
645   state_cookie_param->cookie_lifespan =
646     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
647
648   sctp_compute_mac (sctp_conn, state_cookie_param);
649
650   pointer_offset += sizeof (sctp_state_cookie_param_t);
651
652   if (PREDICT_TRUE (ip4_addr != NULL))
653     {
654       sctp_ipv4_addr_param_t *ipv4_addr =
655         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
656
657       ipv4_addr->param_hdr.type =
658         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
659       ipv4_addr->param_hdr.length =
660         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
661       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
662
663       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
664     }
665   if (PREDICT_TRUE (ip6_addr != NULL))
666     {
667       sctp_ipv6_addr_param_t *ipv6_addr =
668         (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
669
670       ipv6_addr->param_hdr.type =
671         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
672       ipv6_addr->param_hdr.length =
673         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
674       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
675       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
676
677       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
678     }
679
680   if (sctp_conn->sub_conn[idx].connection.is_ip4)
681     {
682       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
683       ip4_param->address.as_u32 =
684         sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
685
686       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
687     }
688   else
689     {
690       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
691       ip6_param->address.as_u64[0] =
692         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
693       ip6_param->address.as_u64[1] =
694         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
695
696       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
697     }
698
699   /* src_port & dst_port are already in network byte-order */
700   init_ack_chunk->sctp_hdr.checksum = 0;
701   init_ack_chunk->sctp_hdr.src_port =
702     sctp_conn->sub_conn[idx].connection.lcl_port;
703   init_ack_chunk->sctp_hdr.dst_port =
704     sctp_conn->sub_conn[idx].connection.rmt_port;
705   /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
706   init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
707   init_ack_chunk->initial_tsn =
708     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
709   SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
710                           init_ack_chunk->initial_tsn);
711
712   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
713   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
714
715   init_ack_chunk->initiate_tag =
716     clib_host_to_net_u32 (random_u32 (&random_seed));
717
718   init_ack_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
719   init_ack_chunk->inboud_streams_count =
720     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
721   init_ack_chunk->outbound_streams_count =
722     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
723
724   sctp_conn->local_tag = init_ack_chunk->initiate_tag;
725
726   vnet_buffer (b)->sctp.connection_index =
727     sctp_conn->sub_conn[idx].connection.c_index;
728 }
729
730 /**
731  * Convert buffer to SHUTDOWN
732  */
733 void
734 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
735 {
736   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
737   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
738
739   /* As per RFC 4960 the chunk_length value does NOT contemplate
740    * the size of the first header (see sctp_header_t) and any padding
741    */
742   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
743
744   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
745
746   sctp_shutdown_association_chunk_t *shutdown_chunk =
747     vlib_buffer_push_uninit (b, alloc_bytes);
748
749   shutdown_chunk->sctp_hdr.checksum = 0;
750   /* No need of host_to_net conversion, already in net-byte order */
751   shutdown_chunk->sctp_hdr.src_port =
752     sctp_conn->sub_conn[idx].connection.lcl_port;
753   shutdown_chunk->sctp_hdr.dst_port =
754     sctp_conn->sub_conn[idx].connection.rmt_port;
755   shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
756   vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
757   vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
758
759   shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
760
761   vnet_buffer (b)->sctp.connection_index =
762     sctp_conn->sub_conn[idx].connection.c_index;
763 }
764
765 /*
766  * Send SHUTDOWN
767  */
768 void
769 sctp_send_shutdown (sctp_connection_t * sctp_conn)
770 {
771   vlib_buffer_t *b;
772   u32 bi;
773   sctp_main_t *tm = vnet_get_sctp_main ();
774   vlib_main_t *vm = vlib_get_main ();
775
776   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
777     return;
778
779   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
780     return;
781
782   b = vlib_get_buffer (vm, bi);
783   sctp_init_buffer (vm, b);
784   sctp_prepare_shutdown_chunk (sctp_conn, b);
785
786   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
787   sctp_enqueue_to_output_now (vm, b, bi,
788                               sctp_conn->sub_conn[idx].connection.is_ip4);
789 }
790
791 /**
792  * Convert buffer to SHUTDOWN_ACK
793  */
794 void
795 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn,
796                                  vlib_buffer_t * b)
797 {
798   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
799   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
800   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
801
802   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
803
804   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
805     vlib_buffer_push_uninit (b, alloc_bytes);
806
807   shutdown_ack_chunk->sctp_hdr.checksum = 0;
808   /* No need of host_to_net conversion, already in net-byte order */
809   shutdown_ack_chunk->sctp_hdr.src_port =
810     sctp_conn->sub_conn[idx].connection.lcl_port;
811   shutdown_ack_chunk->sctp_hdr.dst_port =
812     sctp_conn->sub_conn[idx].connection.rmt_port;
813   shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
814
815   vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
816   vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
817
818   vnet_buffer (b)->sctp.connection_index =
819     sctp_conn->sub_conn[idx].connection.c_index;
820 }
821
822 /*
823  * Send SHUTDOWN_ACK
824  */
825 void
826 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
827 {
828   vlib_main_t *vm = vlib_get_main ();
829
830   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
831     return;
832
833   sctp_reuse_buffer (vm, b);
834
835   sctp_prepare_shutdown_ack_chunk (sctp_conn, b);
836 }
837
838 /**
839  * Convert buffer to SACK
840  */
841 void
842 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
843 {
844   vlib_main_t *vm = vlib_get_main ();
845   u8 idx = sctp_pick_conn_idx_on_chunk (SACK);
846
847   sctp_reuse_buffer (vm, b);
848
849   u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
850
851   /* As per RFC 4960 the chunk_length value does NOT contemplate
852    * the size of the first header (see sctp_header_t) and any padding
853    */
854   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
855
856   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
857
858   sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
859
860   sack->sctp_hdr.checksum = 0;
861   sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
862   sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
863   sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
864   vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
865   vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
866
867   sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
868
869   sctp_conn->ack_state = 0;
870
871   vnet_buffer (b)->sctp.connection_index =
872     sctp_conn->sub_conn[idx].connection.c_index;
873 }
874
875 /**
876  * Convert buffer to HEARTBEAT_ACK
877  */
878 void
879 sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn,
880                                   vlib_buffer_t * b)
881 {
882   vlib_main_t *vm = vlib_get_main ();
883
884   u8 idx = sctp_pick_conn_idx_on_chunk (HEARTBEAT_ACK);
885   u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
886
887   sctp_reuse_buffer (vm, b);
888
889   /* As per RFC 4960 the chunk_length value does NOT contemplate
890    * the size of the first header (see sctp_header_t) and any padding
891    */
892   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
893
894   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
895
896   sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
897
898   hb_ack->sctp_hdr.checksum = 0;
899   /* No need of host_to_net conversion, already in net-byte order */
900   hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
901   hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
902   hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
903   hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
904   hb_ack->hb_info.param_hdr.length =
905     clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
906
907   vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
908   vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
909
910   vnet_buffer (b)->sctp.connection_index =
911     sctp_conn->sub_conn[idx].connection.c_index;
912 }
913
914 /**
915  * Convert buffer to HEARTBEAT
916  */
917 void
918 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn,
919                               vlib_buffer_t * b)
920 {
921   u8 idx = sctp_pick_conn_idx_on_chunk (HEARTBEAT);
922   u16 alloc_bytes = sizeof (sctp_hb_req_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_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
932
933   hb_req->sctp_hdr.checksum = 0;
934   /* No need of host_to_net conversion, already in net-byte order */
935   hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
936   hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
937   hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
938   hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
939   hb_req->hb_info.param_hdr.length =
940     clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
941
942   vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
943   vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
944
945   vnet_buffer (b)->sctp.connection_index =
946     sctp_conn->sub_conn[idx].connection.c_index;
947 }
948
949 void
950 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
951 {
952   vlib_buffer_t *b;
953   u32 bi;
954   sctp_main_t *tm = vnet_get_sctp_main ();
955   vlib_main_t *vm = vlib_get_main ();
956
957   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
958     return;
959
960   b = vlib_get_buffer (vm, bi);
961   sctp_init_buffer (vm, b);
962   sctp_prepare_heartbeat_chunk (sctp_conn, b);
963
964   u8 idx = sctp_pick_conn_idx_on_state (SCTP_STATE_ESTABLISHED);
965   sctp_enqueue_to_output_now (vm, b, bi,
966                               sctp_conn->sub_conn[idx].connection.is_ip4);
967
968   sctp_conn->sub_conn[idx].unacknowledged_hb += 1;
969 }
970
971 /**
972  * Convert buffer to SHUTDOWN_COMPLETE
973  */
974 void
975 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn,
976                                       vlib_buffer_t * b)
977 {
978   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
979   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
980   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
981
982   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
983
984   sctp_shutdown_complete_chunk_t *shutdown_complete =
985     vlib_buffer_push_uninit (b, alloc_bytes);
986
987   shutdown_complete->sctp_hdr.checksum = 0;
988   /* No need of host_to_net conversion, already in net-byte order */
989   shutdown_complete->sctp_hdr.src_port =
990     sctp_conn->sub_conn[idx].connection.lcl_port;
991   shutdown_complete->sctp_hdr.dst_port =
992     sctp_conn->sub_conn[idx].connection.rmt_port;
993   shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
994
995   vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
996   vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
997
998   vnet_buffer (b)->sctp.connection_index =
999     sctp_conn->sub_conn[idx].connection.c_index;
1000 }
1001
1002 void
1003 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn,
1004                              vlib_buffer_t * b0)
1005 {
1006   vlib_main_t *vm = vlib_get_main ();
1007
1008   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1009     return;
1010
1011   sctp_reuse_buffer (vm, b0);
1012
1013   sctp_prepare_shutdown_complete_chunk (sctp_conn, b0);
1014
1015   sctp_conn->state = SCTP_STATE_CLOSED;
1016 }
1017
1018
1019 /*
1020  *  Send INIT
1021  */
1022 void
1023 sctp_send_init (sctp_connection_t * sctp_conn)
1024 {
1025   vlib_buffer_t *b;
1026   u32 bi;
1027   sctp_main_t *tm = vnet_get_sctp_main ();
1028   vlib_main_t *vm = vlib_get_main ();
1029
1030   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1031     return;
1032
1033   b = vlib_get_buffer (vm, bi);
1034   u8 idx = sctp_pick_conn_idx_on_chunk (INIT);
1035
1036   sctp_init_buffer (vm, b);
1037   sctp_prepare_init_chunk (sctp_conn, b);
1038
1039   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1040   sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
1041
1042   /* Start the T1_INIT timer */
1043   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1044                   sctp_conn->sub_conn[idx].RTO);
1045
1046   /* Change state to COOKIE_WAIT */
1047   sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
1048
1049   /* Measure RTT with this */
1050   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1051 }
1052
1053 /**
1054  * Push SCTP header and update connection variables
1055  */
1056 static void
1057 sctp_push_hdr_i (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b,
1058                  sctp_state_t next_state)
1059 {
1060   u16 data_len =
1061     b->current_length + b->total_length_not_including_first_buffer;
1062   ASSERT (!b->total_length_not_including_first_buffer
1063           || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1064
1065   SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1066                        "b->current_data = %p "
1067                        "data_len = %u",
1068                        b->current_length, b->current_data, data_len);
1069
1070   u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1071   u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1072
1073   bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1074
1075   sctp_payload_data_chunk_t *data_chunk =
1076     vlib_buffer_push_uninit (b, bytes_to_add);
1077
1078   data_chunk->sctp_hdr.checksum = 0;
1079   data_chunk->sctp_hdr.src_port =
1080     sctp_conn->sub_conn[idx].connection.lcl_port;
1081   data_chunk->sctp_hdr.dst_port =
1082     sctp_conn->sub_conn[idx].connection.rmt_port;
1083   data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1084
1085   data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
1086   data_chunk->stream_id = clib_host_to_net_u16 (0);
1087   data_chunk->stream_seq = clib_host_to_net_u16 (0);
1088
1089   vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1090   vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1091
1092   vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1093   vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1094
1095   SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1096                        b->data, b->current_data);
1097
1098   sctp_conn->next_tsn += data_len;
1099
1100   vnet_buffer (b)->sctp.connection_index =
1101     sctp_conn->sub_conn[idx].connection.c_index;
1102 }
1103
1104 u32
1105 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
1106 {
1107   sctp_connection_t *sctp_conn =
1108     sctp_get_connection_from_transport (trans_conn);
1109
1110   u8 idx = sctp_pick_conn_idx_on_chunk (DATA);
1111
1112   if (sctp_conn->sub_conn[idx].unacknowledged_hb >
1113       SCTP_ASSOCIATION_MAX_RETRANS)
1114     {
1115       // The remote-peer is considered to be unreachable hence shutting down
1116
1117       /* Start cleanup. App wasn't notified yet so use delete notify as
1118        * opposed to delete to cleanup session layer state. */
1119       stream_session_delete_notify (&sctp_conn->sub_conn
1120                                     [MAIN_SCTP_SUB_CONN_IDX].connection);
1121
1122       sctp_connection_timers_reset (sctp_conn);
1123
1124       sctp_connection_cleanup (sctp_conn);
1125     }
1126
1127   sctp_push_hdr_i (sctp_conn, idx, b, SCTP_STATE_ESTABLISHED);
1128
1129   sctp_trajectory_add_start (b0, 3);
1130
1131   return 0;
1132
1133 }
1134
1135 #if SCTP_DEBUG_STATE_MACHINE
1136 always_inline u8
1137 sctp_validate_output_state_machine (sctp_connection_t * sctp_conn,
1138                                     u8 chunk_type)
1139 {
1140   u8 result = 0;
1141   switch (sctp_conn->state)
1142     {
1143     case SCTP_STATE_CLOSED:
1144       if (chunk_type != INIT && chunk_type != INIT_ACK)
1145         result = 1;
1146       break;
1147     case SCTP_STATE_ESTABLISHED:
1148       if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1149           chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1150           chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1151         result = 1;
1152       break;
1153     case SCTP_STATE_COOKIE_WAIT:
1154       if (chunk_type != COOKIE_ECHO)
1155         result = 1;
1156       break;
1157     case SCTP_STATE_SHUTDOWN_SENT:
1158       if (chunk_type != SHUTDOWN_COMPLETE)
1159         result = 1;
1160       break;
1161     case SCTP_STATE_SHUTDOWN_RECEIVED:
1162       if (chunk_type != SHUTDOWN_ACK)
1163         result = 1;
1164       break;
1165     }
1166   return result;
1167 }
1168 #endif
1169
1170 always_inline u8
1171 sctp_is_retransmitting (sctp_connection_t * sctp_conn, u8 idx)
1172 {
1173   return sctp_conn->sub_conn[idx].is_retransmitting;
1174 }
1175
1176 always_inline uword
1177 sctp46_output_inline (vlib_main_t * vm,
1178                       vlib_node_runtime_t * node,
1179                       vlib_frame_t * from_frame, int is_ip4)
1180 {
1181   u32 n_left_from, next_index, *from, *to_next;
1182   u32 my_thread_index = vm->thread_index;
1183
1184   from = vlib_frame_vector_args (from_frame);
1185   n_left_from = from_frame->n_vectors;
1186   next_index = node->cached_next_index;
1187   sctp_set_time_now (my_thread_index);
1188
1189   while (n_left_from > 0)
1190     {
1191       u32 n_left_to_next;
1192
1193       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1194
1195       while (n_left_from > 0 && n_left_to_next > 0)
1196         {
1197           u32 bi0;
1198           vlib_buffer_t *b0;
1199           sctp_header_t *sctp_hdr = 0;
1200           sctp_connection_t *sctp_conn;
1201           sctp_tx_trace_t *t0;
1202           sctp_header_t *th0 = 0;
1203           u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1204             SCTP_OUTPUT_NEXT_IP_LOOKUP;
1205
1206 #if SCTP_DEBUG_STATE_MACHINE
1207           u16 packet_length = 0;
1208 #endif
1209
1210           bi0 = from[0];
1211           to_next[0] = bi0;
1212           from += 1;
1213           to_next += 1;
1214           n_left_from -= 1;
1215           n_left_to_next -= 1;
1216
1217           b0 = vlib_get_buffer (vm, bi0);
1218
1219           sctp_conn =
1220             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1221                                  my_thread_index);
1222
1223           if (PREDICT_FALSE (sctp_conn == 0))
1224             {
1225               error0 = SCTP_ERROR_INVALID_CONNECTION;
1226               next0 = SCTP_OUTPUT_NEXT_DROP;
1227               goto done;
1228             }
1229
1230           u8 idx = sctp_pick_conn_idx_on_state (sctp_conn->state);
1231
1232           th0 = vlib_buffer_get_current (b0);
1233
1234           if (is_ip4)
1235             {
1236               ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1237                                                          b0,
1238                                                          &sctp_conn->sub_conn
1239                                                          [idx].connection.
1240                                                          lcl_ip.ip4,
1241                                                          &sctp_conn->
1242                                                          sub_conn
1243                                                          [idx].connection.
1244                                                          rmt_ip.ip4,
1245                                                          IP_PROTOCOL_SCTP, 1);
1246
1247               u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1248
1249               sctp_hdr = ip4_next_header (iph4);
1250               sctp_hdr->checksum = checksum;
1251
1252               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1253
1254 #if SCTP_DEBUG_STATE_MACHINE
1255               packet_length = clib_net_to_host_u16 (iph4->length);
1256 #endif
1257             }
1258           else
1259             {
1260               ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1261                                                          b0,
1262                                                          &sctp_conn->sub_conn
1263                                                          [idx].
1264                                                          connection.lcl_ip.
1265                                                          ip6,
1266                                                          &sctp_conn->sub_conn
1267                                                          [idx].
1268                                                          connection.rmt_ip.
1269                                                          ip6,
1270                                                          IP_PROTOCOL_SCTP);
1271
1272               int bogus = ~0;
1273               u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1274               ASSERT (!bogus);
1275
1276               sctp_hdr = ip6_next_header (iph6);
1277               sctp_hdr->checksum = checksum;
1278
1279               vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1280               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1281
1282 #if SCTP_DEBUG_STATE_MACHINE
1283               packet_length = clib_net_to_host_u16 (iph6->payload_length);
1284 #endif
1285             }
1286
1287           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1288           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1289           if (chunk_type >= UNKNOWN)
1290             {
1291               clib_warning
1292                 ("Trying to send an unrecognized chunk... something is really bad.");
1293               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1294               next0 = SCTP_OUTPUT_NEXT_DROP;
1295               goto done;
1296             }
1297
1298 #if SCTP_DEBUG_STATE_MACHINE
1299           u8 is_valid =
1300             (sctp_conn->sub_conn[idx].connection.lcl_port ==
1301              sctp_hdr->src_port
1302              || sctp_conn->sub_conn[idx].connection.lcl_port ==
1303              sctp_hdr->dst_port)
1304             && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1305                 sctp_hdr->dst_port
1306                 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1307                 sctp_hdr->src_port);
1308
1309           if (!is_valid)
1310             {
1311               SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1312                                       "packet_length = %u, "
1313                                       "chunk_type = %u [%s], "
1314                                       "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1315                                       "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1316                                       sctp_conn->sub_conn[idx].
1317                                       connection.c_index, packet_length,
1318                                       chunk_type,
1319                                       sctp_chunk_to_string (chunk_type),
1320                                       sctp_conn->sub_conn[idx].
1321                                       connection.lcl_port, sctp_hdr->src_port,
1322                                       sctp_conn->sub_conn[idx].
1323                                       connection.rmt_port,
1324                                       sctp_hdr->dst_port);
1325
1326               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1327               next0 = SCTP_OUTPUT_NEXT_DROP;
1328               goto done;
1329             }
1330 #endif
1331           SCTP_DBG_STATE_MACHINE
1332             ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1333              "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1334              sctp_conn->sub_conn[idx].connection.c_index,
1335              sctp_conn->state, sctp_state_to_string (sctp_conn->state),
1336              sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1337              full_hdr->hdr.dst_port);
1338
1339           /* Let's make sure the state-machine does not send anything crazy */
1340 #if SCTP_DEBUG_STATE_MACHINE
1341           if (sctp_validate_output_state_machine (sctp_conn, chunk_type) != 0)
1342             {
1343               SCTP_DBG_STATE_MACHINE
1344                 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1345                  sctp_chunk_to_string (chunk_type),
1346                  sctp_state_to_string (sctp_conn->state));
1347
1348               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1349               next0 = SCTP_OUTPUT_NEXT_DROP;
1350               goto done;
1351             }
1352 #endif
1353
1354           /* Karn's algorithm: RTT measurements MUST NOT be made using
1355            * packets that were retransmitted
1356            */
1357           if (!sctp_is_retransmitting (sctp_conn, idx))
1358             {
1359               /* Measure RTT with this */
1360               if (chunk_type == DATA
1361                   && sctp_conn->sub_conn[idx].RTO_pending == 0)
1362                 {
1363                   sctp_conn->sub_conn[idx].RTO_pending = 1;
1364                   sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1365                 }
1366               else
1367                 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1368             }
1369
1370           /* Let's take care of TIMERS */
1371           switch (chunk_type)
1372             {
1373             case COOKIE_ECHO:
1374               {
1375                 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1376                 break;
1377               }
1378             case DATA:
1379               {
1380                 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1381
1382                 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1383                                    sctp_conn->sub_conn[idx].RTO);
1384                 break;
1385               }
1386             case SHUTDOWN:
1387               {
1388                 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1389                 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1390                                 sctp_conn->sub_conn[idx].RTO);
1391                 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1392                 break;
1393               }
1394             case SHUTDOWN_ACK:
1395               {
1396                 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1397                 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1398                                 sctp_conn->sub_conn[idx].RTO);
1399                 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1400                 break;
1401               }
1402             case SHUTDOWN_COMPLETE:
1403               {
1404                 sctp_conn->state = SCTP_STATE_CLOSED;
1405                 break;
1406               }
1407             }
1408
1409           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1410           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1411
1412           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1413
1414           SCTP_DBG_STATE_MACHINE ("CONNECTION_INDEX = %u, "
1415                                   "NEW_STATE = %s, "
1416                                   "CHUNK_SENT = %s",
1417                                   sctp_conn->sub_conn[idx].connection.c_index,
1418                                   sctp_state_to_string (sctp_conn->state),
1419                                   sctp_chunk_to_string (chunk_type));
1420
1421           vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1422
1423         done:
1424           b0->error = node->errors[error0];
1425           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1426             {
1427               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1428               if (th0)
1429                 {
1430                   clib_memcpy (&t0->sctp_header, th0,
1431                                sizeof (t0->sctp_header));
1432                 }
1433               else
1434                 {
1435                   memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1436                 }
1437               clib_memcpy (&t0->sctp_connection, sctp_conn,
1438                            sizeof (t0->sctp_connection));
1439             }
1440
1441           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1442                                            n_left_to_next, bi0, next0);
1443         }
1444
1445       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1446     }
1447
1448   return from_frame->n_vectors;
1449 }
1450
1451 static uword
1452 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1453               vlib_frame_t * from_frame)
1454 {
1455   return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1456 }
1457
1458 static uword
1459 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1460               vlib_frame_t * from_frame)
1461 {
1462   return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1463 }
1464
1465 /* *INDENT-OFF* */
1466 VLIB_REGISTER_NODE (sctp4_output_node) =
1467 {
1468   .function = sctp4_output,.name = "sctp4-output",
1469     /* Takes a vector of packets. */
1470     .vector_size = sizeof (u32),
1471     .n_errors = SCTP_N_ERROR,
1472     .error_strings = sctp_error_strings,
1473     .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1474     .next_nodes = {
1475 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1476     foreach_sctp4_output_next
1477 #undef _
1478     },
1479     .format_buffer = format_sctp_header,
1480     .format_trace = format_sctp_tx_trace,
1481 };
1482 /* *INDENT-ON* */
1483
1484 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1485
1486 /* *INDENT-OFF* */
1487 VLIB_REGISTER_NODE (sctp6_output_node) =
1488 {
1489   .function = sctp6_output,
1490   .name = "sctp6-output",
1491     /* Takes a vector of packets. */
1492   .vector_size = sizeof (u32),
1493   .n_errors = SCTP_N_ERROR,
1494   .error_strings = sctp_error_strings,
1495   .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1496   .next_nodes = {
1497 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1498     foreach_sctp6_output_next
1499 #undef _
1500   },
1501   .format_buffer = format_sctp_header,
1502   .format_trace = format_sctp_tx_trace,
1503 };
1504 /* *INDENT-ON* */
1505
1506 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1507
1508 /*
1509  * fd.io coding-style-patch-verification: ON
1510  *
1511  * Local Variables:
1512  * eval: (c-set-style "gnu")
1513  * End:
1514  */