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