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