SCTP stack (RFC4960)
[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
19 vlib_node_registration_t sctp4_output_node;
20 vlib_node_registration_t sctp6_output_node;
21
22 typedef enum _sctp_output_next
23 {
24   SCTP_OUTPUT_NEXT_DROP,
25   SCTP_OUTPUT_NEXT_IP_LOOKUP,
26   SCTP_OUTPUT_N_NEXT
27 } sctp_output_next_t;
28
29 #define foreach_sctp4_output_next               \
30   _ (DROP, "error-drop")                        \
31   _ (IP_LOOKUP, "ip4-lookup")
32
33 #define foreach_sctp6_output_next               \
34   _ (DROP, "error-drop")                        \
35   _ (IP_LOOKUP, "ip6-lookup")
36
37 static char *sctp_error_strings[] = {
38 #define sctp_error(n,s) s,
39 #include <vnet/sctp/sctp_error.def>
40 #undef sctp_error
41 };
42
43 typedef struct
44 {
45   sctp_header_t sctp_header;
46   sctp_connection_t sctp_connection;
47 } sctp_tx_trace_t;
48
49 /**
50  * Flush tx frame populated by retransmits and timer pops
51  */
52 void
53 sctp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
54 {
55   if (sctp_main.tx_frames[!is_ip4][thread_index])
56     {
57       u32 next_index;
58       next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
59       vlib_put_frame_to_node (vm, next_index,
60                               sctp_main.tx_frames[!is_ip4][thread_index]);
61       sctp_main.tx_frames[!is_ip4][thread_index] = 0;
62     }
63 }
64
65 /**
66  * Flush ip lookup tx frames populated by timer pops
67  */
68 always_inline void
69 sctp_flush_frame_to_ip_lookup (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
70 {
71   if (sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index])
72     {
73       u32 next_index;
74       next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
75       vlib_put_frame_to_node (vm, next_index,
76                               sctp_main.ip_lookup_tx_frames[!is_ip4]
77                               [thread_index]);
78       sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
79     }
80 }
81
82 /**
83  * Flush v4 and v6 sctp and ip-lookup tx frames for thread index
84  */
85 void
86 sctp_flush_frames_to_output (u8 thread_index)
87 {
88   vlib_main_t *vm = vlib_get_main ();
89   sctp_flush_frame_to_output (vm, thread_index, 1);
90   sctp_flush_frame_to_output (vm, thread_index, 0);
91   sctp_flush_frame_to_ip_lookup (vm, thread_index, 1);
92   sctp_flush_frame_to_ip_lookup (vm, thread_index, 0);
93 }
94
95 u32
96 ip4_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
97                            ip4_header_t * ip0)
98 {
99   ip_csum_t checksum;
100   u32 ip_header_length, payload_length_host_byte_order;
101   u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
102   void *data_this_buffer;
103
104   /* Initialize checksum with ip header. */
105   ip_header_length = ip4_header_bytes (ip0);
106   payload_length_host_byte_order =
107     clib_net_to_host_u16 (ip0->length) - ip_header_length;
108   checksum =
109     clib_host_to_net_u32 (payload_length_host_byte_order +
110                           (ip0->protocol << 16));
111
112   if (BITS (uword) == 32)
113     {
114       checksum =
115         ip_csum_with_carry (checksum,
116                             clib_mem_unaligned (&ip0->src_address, u32));
117       checksum =
118         ip_csum_with_carry (checksum,
119                             clib_mem_unaligned (&ip0->dst_address, u32));
120     }
121   else
122     checksum =
123       ip_csum_with_carry (checksum,
124                           clib_mem_unaligned (&ip0->src_address, u64));
125
126   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
127   data_this_buffer = (void *) ip0 + ip_header_length;
128   n_ip_bytes_this_buffer =
129     p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
130   if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
131     {
132       n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
133         n_ip_bytes_this_buffer - ip_header_length : 0;
134     }
135   while (1)
136     {
137       checksum =
138         ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
139       n_bytes_left -= n_this_buffer;
140       if (n_bytes_left == 0)
141         break;
142
143       ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
144       p0 = vlib_get_buffer (vm, p0->next_buffer);
145       data_this_buffer = vlib_buffer_get_current (p0);
146       n_this_buffer = p0->current_length;
147     }
148
149   return checksum;
150 }
151
152 u32
153 ip6_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
154                            ip6_header_t * ip0, int *bogus_lengthp)
155 {
156   ip_csum_t checksum;
157   u16 payload_length_host_byte_order;
158   u32 i, n_this_buffer, n_bytes_left;
159   u32 headers_size = sizeof (ip0[0]);
160   void *data_this_buffer;
161
162   ASSERT (bogus_lengthp);
163   *bogus_lengthp = 0;
164
165   /* Initialize checksum with ip header. */
166   checksum = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
167   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
168   data_this_buffer = (void *) (ip0 + 1);
169
170   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
171     {
172       checksum = ip_csum_with_carry (checksum,
173                                      clib_mem_unaligned (&ip0->
174                                                          src_address.as_uword
175                                                          [i], uword));
176       checksum =
177         ip_csum_with_carry (checksum,
178                             clib_mem_unaligned (&ip0->dst_address.as_uword[i],
179                                                 uword));
180     }
181
182   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
183    * or UDP-Ping packets */
184   if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
185     {
186       u32 skip_bytes;
187       ip6_hop_by_hop_ext_t *ext_hdr =
188         (ip6_hop_by_hop_ext_t *) data_this_buffer;
189
190       /* validate really icmp6 next */
191       ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_SCTP));
192
193       skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
194       data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
195
196       payload_length_host_byte_order -= skip_bytes;
197       headers_size += skip_bytes;
198     }
199
200   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
201   if (p0 && n_this_buffer + headers_size > p0->current_length)
202     n_this_buffer =
203       p0->current_length >
204       headers_size ? p0->current_length - headers_size : 0;
205   while (1)
206     {
207       checksum =
208         ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
209       n_bytes_left -= n_this_buffer;
210       if (n_bytes_left == 0)
211         break;
212
213       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
214         {
215           *bogus_lengthp = 1;
216           return 0xfefe;
217         }
218       p0 = vlib_get_buffer (vm, p0->next_buffer);
219       data_this_buffer = vlib_buffer_get_current (p0);
220       n_this_buffer = p0->current_length;
221     }
222
223   return checksum;
224 }
225
226 void
227 sctp_push_ip_hdr (sctp_main_t * tm, sctp_sub_connection_t * tc,
228                   vlib_buffer_t * b)
229 {
230   sctp_header_t *th = vlib_buffer_get_current (b);
231   vlib_main_t *vm = vlib_get_main ();
232   if (tc->c_is_ip4)
233     {
234       ip4_header_t *ih;
235       ih = vlib_buffer_push_ip4 (vm, b, &tc->c_lcl_ip4,
236                                  &tc->c_rmt_ip4, IP_PROTOCOL_SCTP, 1);
237       th->checksum = ip4_sctp_compute_checksum (vm, b, ih);
238     }
239   else
240     {
241       ip6_header_t *ih;
242       int bogus = ~0;
243
244       ih = vlib_buffer_push_ip6 (vm, b, &tc->c_lcl_ip6,
245                                  &tc->c_rmt_ip6, IP_PROTOCOL_SCTP);
246       th->checksum = ip6_sctp_compute_checksum (vm, b, ih, &bogus);
247       ASSERT (!bogus);
248     }
249 }
250
251 always_inline void *
252 sctp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
253 {
254   if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
255     vlib_buffer_free_one (vm, b->next_buffer);
256   /* Zero all flags but free list index and trace flag */
257   b->flags &= VLIB_BUFFER_NEXT_PRESENT - 1;
258   b->current_data = 0;
259   b->current_length = 0;
260   b->total_length_not_including_first_buffer = 0;
261   vnet_buffer (b)->sctp.flags = 0;
262
263   /* Leave enough space for headers */
264   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
265 }
266
267 always_inline void *
268 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
269 {
270   ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
271   b->flags &= VLIB_BUFFER_FREE_LIST_INDEX_MASK;
272   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
273   b->total_length_not_including_first_buffer = 0;
274   vnet_buffer (b)->sctp.flags = 0;
275   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
276   /* Leave enough space for headers */
277   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
278 }
279
280 always_inline int
281 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
282 {
283   vlib_main_t *vm = vlib_get_main ();
284   u32 current_length = vec_len (tm->tx_buffers[thread_index]);
285   u32 n_allocated;
286
287   vec_validate (tm->tx_buffers[thread_index],
288                 current_length + n_free_buffers - 1);
289   n_allocated =
290     vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
291                        n_free_buffers);
292   _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
293   /* buffer shortage, report failure */
294   if (vec_len (tm->tx_buffers[thread_index]) == 0)
295     {
296       clib_warning ("out of buffers");
297       return -1;
298     }
299   return 0;
300 }
301
302 always_inline int
303 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
304 {
305   u32 *my_tx_buffers;
306   u32 thread_index = vlib_get_thread_index ();
307   if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
308     {
309       if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
310         return -1;
311     }
312   my_tx_buffers = tm->tx_buffers[thread_index];
313   *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
314   _vec_len (my_tx_buffers) -= 1;
315   return 0;
316 }
317
318 always_inline void
319 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
320                           u8 is_ip4, u8 flush)
321 {
322   sctp_main_t *tm = vnet_get_sctp_main ();
323   u32 thread_index = vlib_get_thread_index ();
324   u32 *to_next, next_index;
325   vlib_frame_t *f;
326
327   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
328   b->error = 0;
329
330   /* Decide where to send the packet */
331   next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
332   sctp_trajectory_add_start (b, 2);
333
334   /* Get frame to v4/6 output node */
335   f = tm->tx_frames[!is_ip4][thread_index];
336   if (!f)
337     {
338       f = vlib_get_frame_to_node (vm, next_index);
339       ASSERT (f);
340       tm->tx_frames[!is_ip4][thread_index] = f;
341     }
342   to_next = vlib_frame_vector_args (f);
343   to_next[f->n_vectors] = bi;
344   f->n_vectors += 1;
345   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
346     {
347       vlib_put_frame_to_node (vm, next_index, f);
348       tm->tx_frames[!is_ip4][thread_index] = 0;
349     }
350 }
351
352 always_inline void
353 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
354                             u8 is_ip4)
355 {
356   sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
357 }
358
359 always_inline void
360 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
361                              u8 is_ip4, u8 flush)
362 {
363   sctp_main_t *tm = vnet_get_sctp_main ();
364   u32 thread_index = vlib_get_thread_index ();
365   u32 *to_next, next_index;
366   vlib_frame_t *f;
367
368   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
369   b->error = 0;
370
371   /* Default FIB for now */
372   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
373
374   /* Send to IP lookup */
375   next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
376   if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
377     {
378       b->pre_data[0] = 2;
379       b->pre_data[1] = next_index;
380     }
381
382   f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
383   if (!f)
384     {
385       f = vlib_get_frame_to_node (vm, next_index);
386       ASSERT (f);
387       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
388     }
389
390   to_next = vlib_frame_vector_args (f);
391   to_next[f->n_vectors] = bi;
392   f->n_vectors += 1;
393   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
394     {
395       vlib_put_frame_to_node (vm, next_index, f);
396       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
397     }
398 }
399
400 always_inline void
401 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
402                            u8 is_ip4)
403 {
404   sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
405 }
406
407 always_inline void
408 sctp_enqueue_to_ip_lookup_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
409                                u8 is_ip4)
410 {
411   sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 1);
412 }
413
414 /**
415  * Convert buffer to INIT
416  */
417 void
418 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
419 {
420   u32 random_seed = random_default_seed ();
421   u16 alloc_bytes = sizeof (sctp_init_chunk_t);
422   sctp_sub_connection_t *sub_conn =
423     &sctp_conn->sub_conn[sctp_pick_conn_idx_on_chunk (INIT)];
424
425   sctp_ipv4_addr_param_t *ip4_param = 0;
426   sctp_ipv6_addr_param_t *ip6_param = 0;
427
428   if (sub_conn->c_is_ip4)
429     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
430   else
431     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
432
433   /* As per RFC 4960 the chunk_length value does NOT contemplate
434    * the size of the first header (see sctp_header_t) and any padding
435    */
436   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
437
438   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
439
440   sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
441
442   u16 pointer_offset = sizeof (init_chunk);
443   if (sub_conn->c_is_ip4)
444     {
445       ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
446       ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
447
448       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
449     }
450   else
451     {
452       ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
453       ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
454       ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
455
456       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
457     }
458
459   init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
460   init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
461   init_chunk->sctp_hdr.checksum = 0;
462   /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
463   init_chunk->sctp_hdr.verification_tag = 0x0;
464
465   vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
466   vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
467   vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
468
469   init_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
470   init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
471   init_chunk->inboud_streams_count =
472     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
473   init_chunk->outbound_streams_count =
474     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
475
476   sctp_conn->local_tag = init_chunk->initiate_tag;
477
478   vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
479
480   SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
481                           "CHUNK_TYPE = %s, "
482                           "SRC_PORT = %u, DST_PORT = %u",
483                           sub_conn->connection.c_index,
484                           sctp_conn->state,
485                           sctp_state_to_string (sctp_conn->state),
486                           sctp_chunk_to_string (INIT),
487                           init_chunk->sctp_hdr.src_port,
488                           init_chunk->sctp_hdr.dst_port);
489 }
490
491 u64
492 sctp_compute_mac ()
493 {
494   return 0x0;
495 }
496
497 void
498 sctp_prepare_cookie_ack_chunk (sctp_connection_t * tc, vlib_buffer_t * b)
499 {
500   vlib_main_t *vm = vlib_get_main ();
501   u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ACK);
502
503   sctp_reuse_buffer (vm, b);
504
505   u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
506
507   /* As per RFC 4960 the chunk_length value does NOT contemplate
508    * the size of the first header (see sctp_header_t) and any padding
509    */
510   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
511
512   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
513
514   sctp_cookie_ack_chunk_t *cookie_ack_chunk =
515     vlib_buffer_push_uninit (b, alloc_bytes);
516
517   cookie_ack_chunk->sctp_hdr.checksum = 0;
518   cookie_ack_chunk->sctp_hdr.src_port = tc->sub_conn[idx].connection.lcl_port;
519   cookie_ack_chunk->sctp_hdr.dst_port = tc->sub_conn[idx].connection.rmt_port;
520   cookie_ack_chunk->sctp_hdr.verification_tag = tc->remote_tag;
521   vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
522   vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
523
524   vnet_buffer (b)->sctp.connection_index =
525     tc->sub_conn[idx].connection.c_index;
526 }
527
528 void
529 sctp_prepare_cookie_echo_chunk (sctp_connection_t * tc, vlib_buffer_t * b,
530                                 sctp_state_cookie_param_t * sc)
531 {
532   vlib_main_t *vm = vlib_get_main ();
533   u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ECHO);
534
535   sctp_reuse_buffer (vm, b);
536
537   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
538   u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
539   /* As per RFC 4960 the chunk_length value does NOT contemplate
540    * the size of the first header (see sctp_header_t) and any padding
541    */
542   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
543   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
544   sctp_cookie_echo_chunk_t *cookie_echo_chunk =
545     vlib_buffer_push_uninit (b, alloc_bytes);
546   cookie_echo_chunk->sctp_hdr.checksum = 0;
547   cookie_echo_chunk->sctp_hdr.src_port =
548     tc->sub_conn[idx].connection.lcl_port;
549   cookie_echo_chunk->sctp_hdr.dst_port =
550     tc->sub_conn[idx].connection.rmt_port;
551   cookie_echo_chunk->sctp_hdr.verification_tag = tc->remote_tag;
552   vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
553   vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
554   clib_memcpy (&(cookie_echo_chunk->cookie), sc,
555                sizeof (sctp_state_cookie_param_t));
556   vnet_buffer (b)->sctp.connection_index =
557     tc->sub_conn[idx].connection.c_index;
558 }
559
560 /**
561  * Convert buffer to INIT-ACK
562  */
563 void
564 sctp_prepare_initack_chunk (sctp_connection_t * tc, vlib_buffer_t * b,
565                             ip4_address_t * ip4_addr,
566                             ip6_address_t * ip6_addr)
567 {
568   vlib_main_t *vm = vlib_get_main ();
569   sctp_ipv4_addr_param_t *ip4_param = 0;
570   sctp_ipv6_addr_param_t *ip6_param = 0;
571   u8 idx = sctp_pick_conn_idx_on_chunk (INIT_ACK);
572   u32 random_seed = random_default_seed ();
573
574   sctp_reuse_buffer (vm, b);
575
576   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
577   u16 alloc_bytes =
578     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
579
580   if (PREDICT_TRUE (ip4_addr != NULL))
581     {
582       /* Create room for variable-length fields in the INIT_ACK chunk */
583       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
584     }
585   if (PREDICT_TRUE (ip6_addr != NULL))
586     {
587       /* Create room for variable-length fields in the INIT_ACK chunk */
588       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
589     }
590
591   if (tc->sub_conn[idx].connection.is_ip4)
592     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
593   else
594     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
595
596   /* As per RFC 4960 the chunk_length value does NOT contemplate
597    * the size of the first header (see sctp_header_t) and any padding
598    */
599   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
600
601   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
602
603   sctp_init_ack_chunk_t *init_ack_chunk =
604     vlib_buffer_push_uninit (b, alloc_bytes);
605
606   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
607
608   /* Create State Cookie parameter */
609   sctp_state_cookie_param_t *state_cookie_param =
610     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
611
612   state_cookie_param->param_hdr.type =
613     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
614   state_cookie_param->param_hdr.length =
615     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
616   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
617   state_cookie_param->cookie_lifespan =
618     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
619   state_cookie_param->mac = clib_host_to_net_u64 (sctp_compute_mac ());
620
621   pointer_offset += sizeof (sctp_state_cookie_param_t);
622
623   if (PREDICT_TRUE (ip4_addr != NULL))
624     {
625       sctp_ipv4_addr_param_t *ipv4_addr =
626         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
627
628       ipv4_addr->param_hdr.type =
629         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
630       ipv4_addr->param_hdr.length =
631         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
632       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
633
634       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
635     }
636   if (PREDICT_TRUE (ip6_addr != NULL))
637     {
638       sctp_ipv6_addr_param_t *ipv6_addr =
639         (sctp_ipv6_addr_param_t *) init_ack_chunk +
640         sizeof (sctp_init_chunk_t) + pointer_offset;
641
642       ipv6_addr->param_hdr.type =
643         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
644       ipv6_addr->param_hdr.length =
645         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
646       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
647       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
648
649       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
650     }
651
652   if (tc->sub_conn[idx].connection.is_ip4)
653     {
654       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
655       ip4_param->address.as_u32 =
656         tc->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
657
658       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
659     }
660   else
661     {
662       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
663       ip6_param->address.as_u64[0] =
664         tc->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
665       ip6_param->address.as_u64[1] =
666         tc->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
667
668       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
669     }
670
671   /* src_port & dst_port are already in network byte-order */
672   init_ack_chunk->sctp_hdr.checksum = 0;
673   init_ack_chunk->sctp_hdr.src_port = tc->sub_conn[idx].connection.lcl_port;
674   init_ack_chunk->sctp_hdr.dst_port = tc->sub_conn[idx].connection.rmt_port;
675   /* the tc->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
676   init_ack_chunk->sctp_hdr.verification_tag = tc->remote_tag;
677
678   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
679   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
680
681   init_ack_chunk->initiate_tag =
682     clib_host_to_net_u32 (random_u32 (&random_seed));
683   /* As per RFC 4960, the initial_tsn may be the same value as the initiate_tag */
684   init_ack_chunk->initial_tsn = init_ack_chunk->initiate_tag;
685   init_ack_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
686   init_ack_chunk->inboud_streams_count =
687     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
688   init_ack_chunk->outbound_streams_count =
689     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
690
691   tc->local_tag = init_ack_chunk->initiate_tag;
692
693   vnet_buffer (b)->sctp.connection_index =
694     tc->sub_conn[idx].connection.c_index;
695 }
696
697 /**
698  * Convert buffer to SHUTDOWN
699  */
700 void
701 sctp_prepare_shutdown_chunk (sctp_connection_t * tc, vlib_buffer_t * b)
702 {
703   vlib_main_t *vm = vlib_get_main ();
704   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
705   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
706
707   b = sctp_reuse_buffer (vm, b);
708
709   /* As per RFC 4960 the chunk_length value does NOT contemplate
710    * the size of the first header (see sctp_header_t) and any padding
711    */
712   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
713
714   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
715
716   sctp_shutdown_association_chunk_t *shutdown_chunk =
717     vlib_buffer_push_uninit (b, alloc_bytes);
718
719   shutdown_chunk->sctp_hdr.checksum = 0;
720   /* No need of host_to_net conversion, already in net-byte order */
721   shutdown_chunk->sctp_hdr.src_port = tc->sub_conn[idx].connection.lcl_port;
722   shutdown_chunk->sctp_hdr.dst_port = tc->sub_conn[idx].connection.rmt_port;
723   shutdown_chunk->sctp_hdr.verification_tag = tc->remote_tag;
724   vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
725   vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
726
727   shutdown_chunk->cumulative_tsn_ack = tc->rcv_las;
728
729   vnet_buffer (b)->sctp.connection_index =
730     tc->sub_conn[idx].connection.c_index;
731 }
732
733 /*
734  * Send SHUTDOWN
735  */
736 void
737 sctp_send_shutdown (sctp_connection_t * tc)
738 {
739   vlib_buffer_t *b;
740   u32 bi;
741   sctp_main_t *tm = vnet_get_sctp_main ();
742   vlib_main_t *vm = vlib_get_main ();
743
744   if (sctp_check_outstanding_data_chunks (tc) > 0)
745     return;
746
747   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
748     return;
749
750   b = vlib_get_buffer (vm, bi);
751   sctp_init_buffer (vm, b);
752   sctp_prepare_shutdown_chunk (tc, b);
753
754   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
755   sctp_push_ip_hdr (tm, &tc->sub_conn[idx], b);
756   sctp_enqueue_to_output_now (vm, b, bi, tc->sub_conn[idx].connection.is_ip4);
757 }
758
759 /**
760  * Convert buffer to SHUTDOWN_ACK
761  */
762 void
763 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * tc, vlib_buffer_t * b)
764 {
765   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
766   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
767   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
768
769   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
770
771   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
772     vlib_buffer_push_uninit (b, alloc_bytes);
773
774   shutdown_ack_chunk->sctp_hdr.checksum = 0;
775   /* No need of host_to_net conversion, already in net-byte order */
776   shutdown_ack_chunk->sctp_hdr.src_port =
777     tc->sub_conn[idx].connection.lcl_port;
778   shutdown_ack_chunk->sctp_hdr.dst_port =
779     tc->sub_conn[idx].connection.rmt_port;
780   shutdown_ack_chunk->sctp_hdr.verification_tag = tc->remote_tag;
781
782   vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
783   vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
784
785   vnet_buffer (b)->sctp.connection_index =
786     tc->sub_conn[idx].connection.c_index;
787 }
788
789 /*
790  * Send SHUTDOWN_ACK
791  */
792 void
793 sctp_send_shutdown_ack (sctp_connection_t * tc)
794 {
795   vlib_buffer_t *b;
796   u32 bi;
797   sctp_main_t *tm = vnet_get_sctp_main ();
798   vlib_main_t *vm = vlib_get_main ();
799
800   if (sctp_check_outstanding_data_chunks (tc) > 0)
801     return;
802
803   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
804     return;
805
806   b = vlib_get_buffer (vm, bi);
807   sctp_init_buffer (vm, b);
808   sctp_prepare_shutdown_ack_chunk (tc, b);
809
810   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
811   sctp_push_ip_hdr (tm, &tc->sub_conn[idx], b);
812   sctp_enqueue_to_ip_lookup (vm, b, bi, tc->sub_conn[idx].connection.is_ip4);
813
814   /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
815   sctp_timer_set (tc, idx, SCTP_TIMER_T2_SHUTDOWN, SCTP_RTO_INIT);
816   tc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
817 }
818
819 /**
820  * Convert buffer to SACK
821  */
822 void
823 sctp_prepare_sack_chunk (sctp_connection_t * tc, vlib_buffer_t * b)
824 {
825   vlib_main_t *vm = vlib_get_main ();
826   u8 idx = sctp_pick_conn_idx_on_chunk (SACK);
827
828   sctp_reuse_buffer (vm, b);
829
830   u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
831
832   /* As per RFC 4960 the chunk_length value does NOT contemplate
833    * the size of the first header (see sctp_header_t) and any padding
834    */
835   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
836
837   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
838
839   sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
840
841   sack->sctp_hdr.checksum = 0;
842   sack->sctp_hdr.src_port = tc->sub_conn[idx].connection.lcl_port;
843   sack->sctp_hdr.dst_port = tc->sub_conn[idx].connection.rmt_port;
844   sack->sctp_hdr.verification_tag = tc->remote_tag;
845   vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
846   vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
847
848   vnet_buffer (b)->sctp.connection_index =
849     tc->sub_conn[idx].connection.c_index;
850 }
851
852 /**
853  * Convert buffer to SHUTDOWN_COMPLETE
854  */
855 void
856 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * tc,
857                                       vlib_buffer_t * b)
858 {
859   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
860   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
861   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
862
863   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
864
865   sctp_shutdown_complete_chunk_t *shutdown_complete =
866     vlib_buffer_push_uninit (b, alloc_bytes);
867
868   shutdown_complete->sctp_hdr.checksum = 0;
869   /* No need of host_to_net conversion, already in net-byte order */
870   shutdown_complete->sctp_hdr.src_port =
871     tc->sub_conn[idx].connection.lcl_port;
872   shutdown_complete->sctp_hdr.dst_port =
873     tc->sub_conn[idx].connection.rmt_port;
874   shutdown_complete->sctp_hdr.verification_tag = tc->remote_tag;
875
876   vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
877   vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
878
879   vnet_buffer (b)->sctp.connection_index =
880     tc->sub_conn[idx].connection.c_index;
881 }
882
883 void
884 sctp_send_shutdown_complete (sctp_connection_t * tc)
885 {
886   vlib_buffer_t *b;
887   u32 bi;
888   sctp_main_t *tm = vnet_get_sctp_main ();
889   vlib_main_t *vm = vlib_get_main ();
890
891   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
892     return;
893
894   b = vlib_get_buffer (vm, bi);
895   sctp_init_buffer (vm, b);
896   sctp_prepare_shutdown_complete_chunk (tc, b);
897
898   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
899   sctp_push_ip_hdr (tm, &tc->sub_conn[idx], b);
900   sctp_enqueue_to_ip_lookup (vm, b, bi, tc->sub_conn[idx].connection.is_ip4);
901
902   tc->state = SCTP_STATE_CLOSED;
903 }
904
905
906 /*
907  *  Send INIT
908  */
909 void
910 sctp_send_init (sctp_connection_t * tc)
911 {
912   vlib_buffer_t *b;
913   u32 bi;
914   sctp_main_t *tm = vnet_get_sctp_main ();
915   vlib_main_t *vm = vlib_get_main ();
916
917   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
918     return;
919
920   b = vlib_get_buffer (vm, bi);
921   u8 idx = sctp_pick_conn_idx_on_chunk (INIT);
922
923   sctp_init_buffer (vm, b);
924   sctp_prepare_init_chunk (tc, b);
925
926   /* Measure RTT with this */
927   tc->rtt_ts = sctp_time_now ();
928   tc->rtt_seq = tc->snd_nxt;
929   tc->rto_boff = 0;
930
931   sctp_push_ip_hdr (tm, &tc->sub_conn[idx], b);
932   sctp_enqueue_to_ip_lookup_now (vm, b, bi, tc->sub_conn[idx].c_is_ip4);
933
934   /* Start the T1_INIT timer */
935   sctp_timer_set (tc, idx, SCTP_TIMER_T1_INIT, SCTP_RTO_INIT);
936   /* Change state to COOKIE_WAIT */
937   tc->state = SCTP_STATE_COOKIE_WAIT;
938 }
939
940 always_inline u8
941 sctp_in_cong_recovery (sctp_connection_t * sctp_conn)
942 {
943   return 0;
944 }
945
946 /**
947  * Push SCTP header and update connection variables
948  */
949 static void
950 sctp_push_hdr_i (sctp_connection_t * tc, vlib_buffer_t * b,
951                  sctp_state_t next_state)
952 {
953   u8 idx = sctp_pick_conn_idx_on_chunk (DATA);
954
955   u16 data_len =
956     b->current_length + b->total_length_not_including_first_buffer;
957   ASSERT (!b->total_length_not_including_first_buffer
958           || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
959
960   SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
961                        "b->current_data = %p "
962                        "data_len = %u",
963                        b->current_length, b->current_data, data_len);
964
965   u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
966   u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
967
968   bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
969
970   sctp_payload_data_chunk_t *data_chunk =
971     vlib_buffer_push_uninit (b, bytes_to_add);
972
973   data_chunk->sctp_hdr.checksum = 0;
974   data_chunk->sctp_hdr.src_port = tc->sub_conn[idx].connection.lcl_port;
975   data_chunk->sctp_hdr.dst_port = tc->sub_conn[idx].connection.rmt_port;
976   data_chunk->sctp_hdr.verification_tag = tc->remote_tag;
977
978   data_chunk->tsn = clib_host_to_net_u32 (0);
979   data_chunk->stream_id = clib_host_to_net_u16 (0);
980   data_chunk->stream_seq = clib_host_to_net_u16 (0);
981
982   vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
983   vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
984
985   SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
986                        b->data, b->current_data);
987
988   vnet_buffer (b)->sctp.connection_index =
989     tc->sub_conn[idx].connection.c_index;
990 }
991
992 u32
993 sctp_push_header (transport_connection_t * tconn, vlib_buffer_t * b)
994 {
995   sctp_connection_t *tc = sctp_get_connection_from_transport (tconn);
996   sctp_push_hdr_i (tc, b, SCTP_STATE_ESTABLISHED);
997
998   if (tc->rtt_ts == 0 && !sctp_in_cong_recovery (tc))
999     {
1000       tc->rtt_ts = sctp_time_now ();
1001       tc->rtt_seq = tc->snd_nxt;
1002     }
1003   sctp_trajectory_add_start (b0, 3);
1004
1005   return 0;
1006
1007 }
1008
1009 always_inline uword
1010 sctp46_output_inline (vlib_main_t * vm,
1011                       vlib_node_runtime_t * node,
1012                       vlib_frame_t * from_frame, int is_ip4)
1013 {
1014   u32 n_left_from, next_index, *from, *to_next;
1015   u32 my_thread_index = vm->thread_index;
1016
1017   from = vlib_frame_vector_args (from_frame);
1018   n_left_from = from_frame->n_vectors;
1019   next_index = node->cached_next_index;
1020   sctp_set_time_now (my_thread_index);
1021
1022   while (n_left_from > 0)
1023     {
1024       u32 n_left_to_next;
1025
1026       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1027
1028       while (n_left_from > 0 && n_left_to_next > 0)
1029         {
1030           u32 bi0;
1031           vlib_buffer_t *b0;
1032           sctp_header_t *sctp_hdr = 0;
1033           sctp_connection_t *tc0;
1034           sctp_tx_trace_t *t0;
1035           sctp_header_t *th0 = 0;
1036           u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1037             SCTP_OUTPUT_NEXT_IP_LOOKUP;
1038
1039 #if SCTP_DEBUG_STATE_MACHINE
1040           u16 packet_length = 0;
1041 #endif
1042
1043           bi0 = from[0];
1044           to_next[0] = bi0;
1045           from += 1;
1046           to_next += 1;
1047           n_left_from -= 1;
1048           n_left_to_next -= 1;
1049
1050           b0 = vlib_get_buffer (vm, bi0);
1051           tc0 = sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1052                                      my_thread_index);
1053
1054           if (PREDICT_FALSE (tc0 == 0))
1055             {
1056               error0 = SCTP_ERROR_INVALID_CONNECTION;
1057               next0 = SCTP_OUTPUT_NEXT_DROP;
1058               goto done;
1059             }
1060
1061           u8 idx = sctp_pick_conn_idx_on_state (tc0->state);
1062
1063           th0 = vlib_buffer_get_current (b0);
1064
1065           if (is_ip4)
1066             {
1067               ip4_header_t *th0 = vlib_buffer_push_ip4 (vm,
1068                                                         b0,
1069                                                         &tc0->sub_conn
1070                                                         [idx].connection.
1071                                                         lcl_ip.ip4,
1072                                                         &tc0->
1073                                                         sub_conn
1074                                                         [idx].connection.
1075                                                         rmt_ip.ip4,
1076                                                         IP_PROTOCOL_SCTP, 1);
1077
1078               u32 checksum = ip4_sctp_compute_checksum (vm, b0, th0);
1079
1080               sctp_hdr = ip4_next_header (th0);
1081               sctp_hdr->checksum = checksum;
1082
1083               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1084               th0->checksum = 0;
1085
1086 #if SCTP_DEBUG_STATE_MACHINE
1087               packet_length = clib_net_to_host_u16 (th0->length);
1088 #endif
1089             }
1090           else
1091             {
1092               ip6_header_t *ih0;
1093               ih0 = vlib_buffer_push_ip6 (vm,
1094                                           b0,
1095                                           &tc0->sub_conn[idx].
1096                                           connection.lcl_ip.ip6,
1097                                           &tc0->sub_conn[idx].
1098                                           connection.rmt_ip.ip6,
1099                                           IP_PROTOCOL_SCTP);
1100
1101               int bogus = ~0;
1102               u32 checksum = ip6_sctp_compute_checksum (vm, b0, ih0, &bogus);
1103               ASSERT (!bogus);
1104
1105               sctp_hdr = ip6_next_header (ih0);
1106               sctp_hdr->checksum = checksum;
1107
1108               vnet_buffer (b0)->l3_hdr_offset = (u8 *) ih0 - b0->data;
1109               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1110               th0->checksum = 0;
1111
1112 #if SCTP_DEBUG_STATE_MACHINE
1113               packet_length = clib_net_to_host_u16 (ih0->payload_length);
1114 #endif
1115             }
1116
1117           u8 is_valid =
1118             (tc0->sub_conn[idx].connection.lcl_port ==
1119              sctp_hdr->src_port
1120              || tc0->sub_conn[idx].connection.lcl_port ==
1121              sctp_hdr->dst_port)
1122             && (tc0->sub_conn[idx].connection.rmt_port ==
1123                 sctp_hdr->dst_port
1124                 || tc0->sub_conn[idx].connection.rmt_port ==
1125                 sctp_hdr->src_port);
1126
1127           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1128           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1129
1130           if (!is_valid)
1131             {
1132               SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1133                                       "packet_length = %u, "
1134                                       "chunk_type = %u [%s], "
1135                                       "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1136                                       "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1137                                       tc0->sub_conn
1138                                       [idx].connection.c_index, packet_length,
1139                                       chunk_type,
1140                                       sctp_chunk_to_string (chunk_type),
1141                                       tc0->sub_conn[idx].connection.lcl_port,
1142                                       sctp_hdr->src_port,
1143                                       tc0->sub_conn[idx].connection.rmt_port,
1144                                       sctp_hdr->dst_port);
1145
1146               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1147               next0 = SCTP_OUTPUT_NEXT_DROP;
1148               goto done;
1149             }
1150
1151           SCTP_DBG_STATE_MACHINE
1152             ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1153              "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1154              tc0->sub_conn[idx].connection.c_index,
1155              tc0->state, sctp_state_to_string (tc0->state),
1156              sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1157              full_hdr->hdr.dst_port);
1158
1159           if (chunk_type == DATA)
1160             SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1161
1162           /* Let's make sure the state-machine does not send anything crazy */
1163           switch (tc0->state)
1164             {
1165             case SCTP_STATE_CLOSED:
1166               {
1167                 if (chunk_type != INIT && chunk_type != INIT_ACK)
1168                   {
1169                     SCTP_DBG_STATE_MACHINE
1170                       ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1171                        sctp_chunk_to_string (chunk_type),
1172                        sctp_state_to_string (tc0->state));
1173
1174                     error0 = SCTP_ERROR_UNKOWN_CHUNK;
1175                     next0 = SCTP_OUTPUT_NEXT_DROP;
1176                     goto done;
1177                   }
1178                 break;
1179               }
1180             case SCTP_STATE_ESTABLISHED:
1181               if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1182                   chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1183                   chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1184                 {
1185                   SCTP_DBG_STATE_MACHINE
1186                     ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1187                      sctp_chunk_to_string (chunk_type),
1188                      sctp_state_to_string (tc0->state));
1189
1190                   error0 = SCTP_ERROR_UNKOWN_CHUNK;
1191                   next0 = SCTP_OUTPUT_NEXT_DROP;
1192                   goto done;
1193                 }
1194               break;
1195             case SCTP_STATE_COOKIE_WAIT:
1196               if (chunk_type != COOKIE_ECHO)
1197                 {
1198                   SCTP_DBG_STATE_MACHINE
1199                     ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1200                      sctp_chunk_to_string (chunk_type),
1201                      sctp_state_to_string (tc0->state));
1202
1203                   error0 = SCTP_ERROR_UNKOWN_CHUNK;
1204                   next0 = SCTP_OUTPUT_NEXT_DROP;
1205                   goto done;
1206                 }
1207               /* Change state */
1208               tc0->state = SCTP_STATE_COOKIE_ECHOED;
1209               break;
1210             default:
1211               SCTP_DBG_STATE_MACHINE
1212                 ("Sending chunk (%s) based on state-machine status (%s)",
1213                  sctp_chunk_to_string (chunk_type),
1214                  sctp_state_to_string (tc0->state));
1215               break;
1216             }
1217
1218           if (chunk_type == SHUTDOWN)
1219             {
1220               /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1221               sctp_timer_set (tc0, idx, SCTP_TIMER_T2_SHUTDOWN,
1222                               SCTP_RTO_INIT);
1223               tc0->state = SCTP_STATE_SHUTDOWN_SENT;
1224             }
1225
1226           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1227           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1228
1229           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1230
1231           SCTP_DBG_STATE_MACHINE ("CONNECTION_INDEX = %u, "
1232                                   "NEW_STATE = %s, "
1233                                   "CHUNK_SENT = %s",
1234                                   tc0->sub_conn[idx].connection.c_index,
1235                                   sctp_state_to_string (tc0->state),
1236                                   sctp_chunk_to_string (chunk_type));
1237
1238           vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1239
1240         done:
1241           b0->error = node->errors[error0];
1242           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1243             {
1244               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1245               if (th0)
1246                 {
1247                   clib_memcpy (&t0->sctp_header, th0,
1248                                sizeof (t0->sctp_header));
1249                 }
1250               else
1251                 {
1252                   memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1253                 }
1254               clib_memcpy (&t0->sctp_connection, tc0,
1255                            sizeof (t0->sctp_connection));
1256             }
1257
1258           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1259                                            n_left_to_next, bi0, next0);
1260         }
1261
1262       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1263     }
1264
1265   return from_frame->n_vectors;
1266 }
1267
1268 static uword
1269 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1270               vlib_frame_t * from_frame)
1271 {
1272   return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1273 }
1274
1275 static uword
1276 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1277               vlib_frame_t * from_frame)
1278 {
1279   return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1280 }
1281
1282 /* *INDENT-OFF* */
1283 VLIB_REGISTER_NODE (sctp4_output_node) =
1284 {
1285   .function = sctp4_output,.name = "sctp4-output",
1286     /* Takes a vector of packets. */
1287     .vector_size = sizeof (u32),
1288     .n_errors = SCTP_N_ERROR,
1289     .error_strings = sctp_error_strings,
1290     .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1291     .next_nodes = {
1292 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1293     foreach_sctp4_output_next
1294 #undef _
1295     },
1296     .format_buffer = format_sctp_header,
1297     .format_trace = format_sctp_tx_trace,
1298 };
1299 /* *INDENT-ON* */
1300
1301 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1302
1303 /* *INDENT-OFF* */
1304 VLIB_REGISTER_NODE (sctp6_output_node) =
1305 {
1306   .function = sctp6_output,
1307   .name = "sctp6-output",
1308     /* Takes a vector of packets. */
1309   .vector_size = sizeof (u32),
1310   .n_errors = SCTP_N_ERROR,
1311   .error_strings = sctp_error_strings,
1312   .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1313   .next_nodes = {
1314 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1315     foreach_sctp6_output_next
1316 #undef _
1317   },
1318   .format_buffer = format_sctp_header,
1319   .format_trace = format_sctp_tx_trace,
1320 };
1321 /* *INDENT-ON* */
1322
1323 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1324
1325 /*
1326  * fd.io coding-style-patch-verification: ON
1327  *
1328  * Local Variables:
1329  * eval: (c-set-style "gnu")
1330  * End:
1331  */