SCTP: congestion control
[vpp.git] / src / vnet / sctp / sctp_input.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 <vppinfra/sparse_vec.h>
16 #include <vnet/sctp/sctp.h>
17 #include <vnet/sctp/sctp_packet.h>
18 #include <vnet/sctp/sctp_debug.h>
19 #include <vnet/session/session.h>
20 #include <math.h>
21
22 static char *sctp_error_strings[] = {
23 #define sctp_error(n,s) s,
24 #include <vnet/sctp/sctp_error.def>
25 #undef sctp_error
26 };
27
28 /* All SCTP nodes have the same outgoing arcs */
29 #define foreach_sctp_state_next                  \
30   _ (DROP4, "ip4-drop")                         \
31   _ (DROP6, "ip6-drop")                         \
32   _ (SCTP4_OUTPUT, "sctp4-output")                \
33   _ (SCTP6_OUTPUT, "sctp6-output")
34
35 typedef enum _sctp_established_phase_next
36 {
37 #define _(s,n) SCTP_ESTABLISHED_PHASE_NEXT_##s,
38   foreach_sctp_state_next
39 #undef _
40     SCTP_ESTABLISHED_PHASE_N_NEXT,
41 } sctp_established_phase_next_t;
42
43 typedef enum _sctp_rcv_phase_next
44 {
45 #define _(s,n) SCTP_RCV_PHASE_NEXT_##s,
46   foreach_sctp_state_next
47 #undef _
48     SCTP_RCV_PHASE_N_NEXT,
49 } sctp_rcv_phase_next_t;
50
51 typedef enum _sctp_listen_phase_next
52 {
53 #define _(s,n) SCTP_LISTEN_PHASE_NEXT_##s,
54   foreach_sctp_state_next
55 #undef _
56     SCTP_LISTEN_PHASE_N_NEXT,
57 } sctp_listen_phase_next_t;
58
59 typedef enum _sctp_shutdown_phase_next
60 {
61 #define _(s,n) SCTP_SHUTDOWN_PHASE_NEXT_##s,
62   foreach_sctp_state_next
63 #undef _
64     SCTP_SHUTDOWN_PHASE_N_NEXT,
65 } sctp_shutdown_phase_next_t;
66
67 /* Generic, state independent indices */
68 typedef enum _sctp_state_next
69 {
70 #define _(s,n) SCTP_NEXT_##s,
71   foreach_sctp_state_next
72 #undef _
73     SCTP_STATE_N_NEXT,
74 } sctp_state_next_t;
75
76 typedef enum _sctp_input_next
77 {
78   SCTP_INPUT_NEXT_DROP,
79   SCTP_INPUT_NEXT_LISTEN_PHASE,
80   SCTP_INPUT_NEXT_RCV_PHASE,
81   SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
82   SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
83   SCTP_INPUT_NEXT_PUNT_PHASE,
84   SCTP_INPUT_N_NEXT
85 } sctp_input_next_t;
86
87 char *
88 phase_to_string (u8 phase)
89 {
90   switch (phase)
91     {
92     case SCTP_INPUT_NEXT_DROP:
93       return "SCTP_INPUT_NEXT_DROP";
94     case SCTP_INPUT_NEXT_LISTEN_PHASE:
95       return "SCTP_INPUT_NEXT_LISTEN_PHASE";
96     case SCTP_INPUT_NEXT_RCV_PHASE:
97       return "SCTP_INPUT_NEXT_RCV_PHASE";
98     case SCTP_INPUT_NEXT_ESTABLISHED_PHASE:
99       return "SCTP_INPUT_NEXT_ESTABLISHED_PHASE";
100     case SCTP_INPUT_NEXT_SHUTDOWN_PHASE:
101       return "SCTP_INPUT_NEXT_SHUTDOWN_PHASE";
102     case SCTP_INPUT_NEXT_PUNT_PHASE:
103       return "SCTP_INPUT_NEXT_PUNT_PHASE";
104     }
105   return NULL;
106 }
107
108 #define foreach_sctp4_input_next                 \
109   _ (DROP, "error-drop")                         \
110   _ (RCV_PHASE, "sctp4-rcv")                    \
111   _ (LISTEN_PHASE, "sctp4-listen")       \
112   _ (ESTABLISHED_PHASE, "sctp4-established")             \
113   _ (SHUTDOWN_PHASE, "sctp4-shutdown")  \
114   _ (PUNT_PHASE, "ip4-punt")
115
116
117 #define foreach_sctp6_input_next                 \
118   _ (DROP, "error-drop")                         \
119   _ (RCV_PHASE, "sctp6-rcv")                    \
120   _ (LISTEN_PHASE, "sctp6-listen")       \
121   _ (ESTABLISHED_PHASE, "sctp6-established")             \
122   _ (SHUTDOWN_PHASE, "sctp6-shutdown")          \
123   _ (PUNT_PHASE, "ip6-punt")
124
125 static u8
126 sctp_lookup_is_valid (transport_connection_t * trans_conn,
127                       sctp_header_t * sctp_hdr)
128 {
129   sctp_connection_t *sctp_conn =
130     sctp_get_connection_from_transport (trans_conn);
131
132   if (!sctp_conn)
133     return 1;
134
135   u8 is_valid = (trans_conn->lcl_port == sctp_hdr->dst_port
136                  && (sctp_conn->state == SCTP_STATE_CLOSED
137                      || trans_conn->rmt_port == sctp_hdr->src_port));
138
139   return is_valid;
140 }
141
142 /**
143  * Lookup transport connection
144  */
145 static sctp_connection_t *
146 sctp_lookup_connection (u32 fib_index, vlib_buffer_t * b, u8 thread_index,
147                         u8 is_ip4)
148 {
149   sctp_main_t *tm = vnet_get_sctp_main ();
150   sctp_header_t *sctp_hdr;
151   transport_connection_t *trans_conn;
152   sctp_connection_t *sctp_conn;
153   u8 is_filtered, i;
154   if (is_ip4)
155     {
156       ip4_header_t *ip4_hdr;
157       ip4_hdr = vlib_buffer_get_current (b);
158       sctp_hdr = ip4_next_header (ip4_hdr);
159       trans_conn = session_lookup_connection_wt4 (fib_index,
160                                                   &ip4_hdr->dst_address,
161                                                   &ip4_hdr->src_address,
162                                                   sctp_hdr->dst_port,
163                                                   sctp_hdr->src_port,
164                                                   TRANSPORT_PROTO_SCTP,
165                                                   thread_index, &is_filtered);
166       if (trans_conn == 0)      /* Not primary connection */
167         {
168           for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
169             {
170               if ((tm->connections[thread_index]->sub_conn[i].
171                    connection.lcl_ip.ip4.as_u32 ==
172                    ip4_hdr->dst_address.as_u32)
173                   && (tm->connections[thread_index]->sub_conn[i].
174                       connection.rmt_ip.ip4.as_u32 ==
175                       ip4_hdr->src_address.as_u32))
176                 {
177                   trans_conn =
178                     &tm->connections[thread_index]->sub_conn[i].connection;
179                   break;
180                 }
181             }
182         }
183       ASSERT (trans_conn != 0);
184       ASSERT (sctp_lookup_is_valid (trans_conn, sctp_hdr));
185     }
186   else
187     {
188       ip6_header_t *ip6_hdr;
189       ip6_hdr = vlib_buffer_get_current (b);
190       sctp_hdr = ip6_next_header (ip6_hdr);
191       trans_conn = session_lookup_connection_wt6 (fib_index,
192                                                   &ip6_hdr->dst_address,
193                                                   &ip6_hdr->src_address,
194                                                   sctp_hdr->dst_port,
195                                                   sctp_hdr->src_port,
196                                                   TRANSPORT_PROTO_SCTP,
197                                                   thread_index, &is_filtered);
198       if (trans_conn == 0)      /* Not primary connection */
199         {
200           for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
201             {
202               if ((tm->connections[thread_index]->sub_conn[i].
203                    connection.lcl_ip.ip6.as_u64[0] ==
204                    ip6_hdr->dst_address.as_u64[0]
205                    && tm->connections[thread_index]->sub_conn[i].
206                    connection.lcl_ip.ip6.as_u64[1] ==
207                    ip6_hdr->dst_address.as_u64[1])
208                   && (tm->connections[thread_index]->sub_conn[i].
209                       connection.rmt_ip.ip6.as_u64[0] ==
210                       ip6_hdr->src_address.as_u64[0]
211                       && tm->connections[thread_index]->
212                       sub_conn[i].connection.rmt_ip.ip6.as_u64[1] ==
213                       ip6_hdr->src_address.as_u64[1]))
214                 {
215                   trans_conn =
216                     &tm->connections[thread_index]->sub_conn[i].connection;
217                   break;
218                 }
219             }
220         }
221       ASSERT (trans_conn != 0);
222       ASSERT (sctp_lookup_is_valid (trans_conn, sctp_hdr));
223     }
224   sctp_conn = sctp_get_connection_from_transport (trans_conn);
225   return sctp_conn;
226 }
227
228 typedef struct
229 {
230   sctp_header_t sctp_header;
231   sctp_connection_t sctp_connection;
232 } sctp_rx_trace_t;
233
234 #define sctp_next_output(is_ip4) (is_ip4 ? SCTP_NEXT_SCTP4_OUTPUT          \
235                                         : SCTP_NEXT_SCTP6_OUTPUT)
236
237 #define sctp_next_drop(is_ip4) (is_ip4 ? SCTP_NEXT_DROP4                  \
238                                       : SCTP_NEXT_DROP6)
239
240 void
241 sctp_set_rx_trace_data (sctp_rx_trace_t * rx_trace,
242                         sctp_connection_t * sctp_conn,
243                         sctp_header_t * sctp_hdr, vlib_buffer_t * b0,
244                         u8 is_ip4)
245 {
246   if (sctp_conn)
247     {
248       clib_memcpy (&rx_trace->sctp_connection, sctp_conn,
249                    sizeof (rx_trace->sctp_connection));
250     }
251   else
252     {
253       sctp_hdr = sctp_buffer_hdr (b0);
254     }
255   clib_memcpy (&rx_trace->sctp_header, sctp_hdr,
256                sizeof (rx_trace->sctp_header));
257 }
258
259 always_inline u16
260 sctp_calculate_implied_length (ip4_header_t * ip4_hdr, ip6_header_t * ip6_hdr,
261                                int is_ip4)
262 {
263   u16 sctp_implied_packet_length = 0;
264
265   if (is_ip4)
266     sctp_implied_packet_length =
267       clib_net_to_host_u16 (ip4_hdr->length) - ip4_header_bytes (ip4_hdr);
268   else
269     sctp_implied_packet_length =
270       clib_net_to_host_u16 (ip6_hdr->payload_length) - sizeof (ip6_hdr);
271
272   return sctp_implied_packet_length;
273 }
274
275 always_inline u8
276 sctp_is_bundling (u16 sctp_implied_length,
277                   sctp_chunks_common_hdr_t * sctp_common_hdr)
278 {
279   if (sctp_implied_length !=
280       sizeof (sctp_header_t) + vnet_sctp_get_chunk_length (sctp_common_hdr))
281     return 1;
282   return 0;
283 }
284
285 always_inline u16
286 sctp_handle_init (sctp_header_t * sctp_hdr,
287                   sctp_chunks_common_hdr_t * sctp_chunk_hdr,
288                   sctp_connection_t * sctp_conn, vlib_buffer_t * b0,
289                   u16 sctp_implied_length)
290 {
291   sctp_init_chunk_t *init_chunk = (sctp_init_chunk_t *) (sctp_hdr);
292   ip4_address_t *ip4_addr = 0;
293   ip6_address_t *ip6_addr = 0;
294   char hostname[FQDN_MAX_LENGTH];
295
296   /* Check the current state of the connection
297    *
298    * The logic required by the RFC4960 Section 5.2.2 is already taken care of
299    * in the code below and by the "sctp_prepare_initack_chunk" function.
300    * However, for debugging purposes it is nice to have a message printed out
301    * for these corner-case scenarios.
302    */
303   if (sctp_conn->state != SCTP_STATE_CLOSED)
304     {                           /* UNEXPECTED scenario */
305       switch (sctp_conn->state)
306         {
307         case SCTP_STATE_COOKIE_WAIT:    /* TODO */
308           SCTP_ADV_DBG ("Received INIT chunk while in COOKIE_WAIT state");
309           break;
310         case SCTP_STATE_COOKIE_ECHOED:  /* TODO */
311           SCTP_ADV_DBG ("Received INIT chunk while in COOKIE_ECHOED state");
312           break;
313         }
314     }
315
316   if (sctp_hdr->verification_tag != 0x0)
317     return SCTP_ERROR_INVALID_TAG_FOR_INIT;
318
319   /*
320    * It is not possible to bundle any other CHUNK with the INIT chunk
321    */
322   if (sctp_is_bundling (sctp_implied_length, &init_chunk->chunk_hdr))
323     return SCTP_ERROR_BUNDLING_VIOLATION;
324
325   /* Save the INITIATE_TAG of the remote peer for this connection:
326    * it MUST be used for the VERIFICATION_TAG parameter in the SCTP HEADER */
327   sctp_conn->remote_tag = init_chunk->initiate_tag;
328   sctp_conn->remote_initial_tsn =
329     clib_net_to_host_u32 (init_chunk->initial_tsn);
330   sctp_conn->last_rcvd_tsn = sctp_conn->remote_initial_tsn;
331   sctp_conn->next_tsn_expected = sctp_conn->remote_initial_tsn + 1;
332   SCTP_CONN_TRACKING_DBG ("sctp_conn->remote_initial_tsn = %u",
333                           sctp_conn->remote_initial_tsn);
334
335   sctp_conn->snd_opts.a_rwnd = clib_net_to_host_u32 (init_chunk->a_rwnd);
336
337   /*
338    * If the length specified in the INIT message is bigger than the size in bytes of our structure it means that
339    * optional parameters have been sent with the INIT chunk and we need to parse them.
340    */
341   u16 length = vnet_sctp_get_chunk_length (sctp_chunk_hdr);
342   if (length > sizeof (sctp_init_chunk_t))
343     {
344       /* There are optional parameters in the INIT chunk */
345       u16 pointer_offset = sizeof (sctp_init_chunk_t);
346       while (pointer_offset < length)
347         {
348           sctp_opt_params_hdr_t *opt_params_hdr =
349             (sctp_opt_params_hdr_t *) init_chunk + pointer_offset;
350
351           switch (clib_net_to_host_u16 (opt_params_hdr->type))
352             {
353             case SCTP_IPV4_ADDRESS_TYPE:
354               {
355                 sctp_ipv4_addr_param_t *ipv4 =
356                   (sctp_ipv4_addr_param_t *) opt_params_hdr;
357                 clib_memcpy (ip4_addr, &ipv4->address,
358                              sizeof (ip4_address_t));
359
360                 sctp_sub_connection_add_ip4 (vlib_get_thread_index (), ipv4);
361
362                 break;
363               }
364             case SCTP_IPV6_ADDRESS_TYPE:
365               {
366                 sctp_ipv6_addr_param_t *ipv6 =
367                   (sctp_ipv6_addr_param_t *) opt_params_hdr;
368                 clib_memcpy (ip6_addr, &ipv6->address,
369                              sizeof (ip6_address_t));
370
371                 sctp_sub_connection_add_ip6 (vlib_get_thread_index (), ipv6);
372
373                 break;
374               }
375             case SCTP_COOKIE_PRESERVATIVE_TYPE:
376               {
377                 sctp_cookie_preservative_param_t *cookie_pres =
378                   (sctp_cookie_preservative_param_t *) opt_params_hdr;
379                 sctp_conn->peer_cookie_life_span_increment =
380                   cookie_pres->life_span_inc;
381                 break;
382               }
383             case SCTP_HOSTNAME_ADDRESS_TYPE:
384               {
385                 sctp_hostname_param_t *hostname_addr =
386                   (sctp_hostname_param_t *) opt_params_hdr;
387                 clib_memcpy (hostname, hostname_addr->hostname,
388                              FQDN_MAX_LENGTH);
389                 break;
390               }
391             case SCTP_SUPPORTED_ADDRESS_TYPES:
392               {
393                 /* TODO */
394                 break;
395               }
396             }
397           pointer_offset += clib_net_to_host_u16 (opt_params_hdr->length);
398         }
399     }
400
401   /* Reuse buffer to make init-ack and send */
402   sctp_prepare_initack_chunk (sctp_conn, MAIN_SCTP_SUB_CONN_IDX, b0, ip4_addr,
403                               ip6_addr);
404   return SCTP_ERROR_NONE;
405 }
406
407 always_inline u16
408 sctp_is_valid_init_ack (sctp_header_t * sctp_hdr,
409                         sctp_chunks_common_hdr_t * sctp_chunk_hdr,
410                         sctp_connection_t * sctp_conn, vlib_buffer_t * b0,
411                         u16 sctp_implied_length)
412 {
413   sctp_init_ack_chunk_t *init_ack_chunk =
414     (sctp_init_ack_chunk_t *) (sctp_hdr);
415
416   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
417   if (sctp_conn->local_tag != init_ack_chunk->sctp_hdr.verification_tag)
418     {
419       return SCTP_ERROR_INVALID_TAG;
420     }
421
422   /*
423    * It is not possible to bundle any other CHUNK with the INIT_ACK chunk
424    */
425   if (sctp_is_bundling (sctp_implied_length, &init_ack_chunk->chunk_hdr))
426     return SCTP_ERROR_BUNDLING_VIOLATION;
427
428   return SCTP_ERROR_NONE;
429 }
430
431 always_inline u16
432 sctp_handle_init_ack (sctp_header_t * sctp_hdr,
433                       sctp_chunks_common_hdr_t * sctp_chunk_hdr,
434                       sctp_connection_t * sctp_conn, u8 idx,
435                       vlib_buffer_t * b0, u16 sctp_implied_length)
436 {
437   sctp_init_ack_chunk_t *init_ack_chunk =
438     (sctp_init_ack_chunk_t *) (sctp_hdr);
439   sctp_state_cookie_param_t state_cookie;
440
441   char hostname[FQDN_MAX_LENGTH];
442
443   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
444   if (sctp_conn->local_tag != init_ack_chunk->sctp_hdr.verification_tag)
445     {
446       return SCTP_ERROR_INVALID_TAG;
447     }
448
449   /*
450    * It is not possible to bundle any other CHUNK with the INIT chunk
451    */
452   if (sctp_is_bundling (sctp_implied_length, &init_ack_chunk->chunk_hdr))
453     return SCTP_ERROR_BUNDLING_VIOLATION;
454
455   sctp_calculate_rto (sctp_conn, idx);
456
457   /* remote_tag to be placed in the VERIFICATION_TAG field of the COOKIE_ECHO chunk */
458   sctp_conn->remote_tag = init_ack_chunk->initiate_tag;
459   sctp_conn->remote_initial_tsn =
460     clib_net_to_host_u32 (init_ack_chunk->initial_tsn);
461   sctp_conn->last_rcvd_tsn = sctp_conn->remote_initial_tsn;
462   sctp_conn->next_tsn_expected = sctp_conn->remote_initial_tsn + 1;
463   SCTP_CONN_TRACKING_DBG ("sctp_conn->remote_initial_tsn = %u",
464                           sctp_conn->remote_initial_tsn);
465   sctp_conn->snd_opts.a_rwnd = clib_net_to_host_u32 (init_ack_chunk->a_rwnd);
466
467   u16 length = vnet_sctp_get_chunk_length (sctp_chunk_hdr);
468
469   if (length > sizeof (sctp_init_ack_chunk_t))
470     /*
471      * There are optional parameters in the INIT ACK chunk
472      */
473     {
474       u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
475
476       while (pointer_offset < length)
477         {
478           sctp_opt_params_hdr_t *opt_params_hdr =
479             (sctp_opt_params_hdr_t *) ((char *) init_ack_chunk +
480                                        pointer_offset);
481
482           switch (clib_net_to_host_u16 (opt_params_hdr->type))
483             {
484             case SCTP_IPV4_ADDRESS_TYPE:
485               {
486                 sctp_ipv4_addr_param_t *ipv4 =
487                   (sctp_ipv4_addr_param_t *) opt_params_hdr;
488
489                 sctp_sub_connection_add_ip4 (vlib_get_thread_index (), ipv4);
490
491                 break;
492               }
493             case SCTP_IPV6_ADDRESS_TYPE:
494               {
495                 sctp_ipv6_addr_param_t *ipv6 =
496                   (sctp_ipv6_addr_param_t *) opt_params_hdr;
497
498                 sctp_sub_connection_add_ip6 (vlib_get_thread_index (), ipv6);
499
500                 break;
501               }
502             case SCTP_STATE_COOKIE_TYPE:
503               {
504                 sctp_state_cookie_param_t *state_cookie_param =
505                   (sctp_state_cookie_param_t *) opt_params_hdr;
506
507                 clib_memcpy (&state_cookie, state_cookie_param,
508                              sizeof (sctp_state_cookie_param_t));
509                 break;
510               }
511             case SCTP_HOSTNAME_ADDRESS_TYPE:
512               {
513                 sctp_hostname_param_t *hostname_addr =
514                   (sctp_hostname_param_t *) opt_params_hdr;
515                 clib_memcpy (hostname, hostname_addr->hostname,
516                              FQDN_MAX_LENGTH);
517                 break;
518               }
519             case SCTP_UNRECOGNIZED_TYPE:
520               {
521                 break;
522               }
523             }
524           u16 increment = clib_net_to_host_u16 (opt_params_hdr->length);
525           /* This indicates something really bad happened */
526           if (increment == 0)
527             {
528               return SCTP_ERROR_INVALID_TAG;
529             }
530           pointer_offset += increment;
531         }
532     }
533
534   sctp_prepare_cookie_echo_chunk (sctp_conn, idx, b0, &state_cookie);
535
536   /* Start the T1_COOKIE timer */
537   sctp_timer_set (sctp_conn, idx,
538                   SCTP_TIMER_T1_COOKIE, sctp_conn->sub_conn[idx].RTO);
539
540   stream_session_accept_notify (&sctp_conn->sub_conn[idx].connection);
541
542   return SCTP_ERROR_NONE;
543 }
544
545 /** Enqueue data out-of-order for delivery to application */
546 always_inline int
547 sctp_session_enqueue_data_ooo (sctp_connection_t * sctp_conn,
548                                vlib_buffer_t * b, u16 data_len, u8 conn_idx)
549 {
550   int written, error = SCTP_ERROR_ENQUEUED;
551
552   written =
553     session_enqueue_stream_connection (&sctp_conn->
554                                        sub_conn[conn_idx].connection, b, 0,
555                                        1 /* queue event */ ,
556                                        0);
557
558   /* Update next_tsn_expected */
559   if (PREDICT_TRUE (written == data_len))
560     {
561       sctp_conn->next_tsn_expected += written;
562
563       SCTP_ADV_DBG ("CONN = %u, WRITTEN [%u] == DATA_LEN [%d]",
564                     sctp_conn->sub_conn[conn_idx].connection.c_index,
565                     written, data_len);
566     }
567   /* If more data written than expected, account for out-of-order bytes. */
568   else if (written > data_len)
569     {
570       sctp_conn->next_tsn_expected += written;
571
572       SCTP_ADV_DBG ("CONN = %u, WRITTEN [%u] > DATA_LEN [%d]",
573                     sctp_conn->sub_conn[conn_idx].connection.c_index,
574                     written, data_len);
575     }
576   else if (written > 0)
577     {
578       /* We've written something but FIFO is probably full now */
579       sctp_conn->next_tsn_expected += written;
580
581       error = SCTP_ERROR_PARTIALLY_ENQUEUED;
582
583       SCTP_ADV_DBG
584         ("CONN = %u, WRITTEN [%u] > 0 (SCTP_ERROR_PARTIALLY_ENQUEUED)",
585          sctp_conn->sub_conn[conn_idx].connection.c_index, written);
586     }
587   else
588     {
589       SCTP_ADV_DBG ("CONN = %u, WRITTEN == 0 (SCTP_ERROR_FIFO_FULL)",
590                     sctp_conn->sub_conn[conn_idx].connection.c_index);
591
592       return SCTP_ERROR_FIFO_FULL;
593     }
594
595   /* TODO: Update out_of_order_map & SACK list */
596
597   return error;
598 }
599
600 /** Enqueue data for delivery to application */
601 always_inline int
602 sctp_session_enqueue_data (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
603                            u16 data_len, u8 conn_idx)
604 {
605   int written, error = SCTP_ERROR_ENQUEUED;
606
607   written =
608     session_enqueue_stream_connection (&sctp_conn->
609                                        sub_conn[conn_idx].connection, b, 0,
610                                        1 /* queue event */ ,
611                                        1);
612
613   /* Update next_tsn_expected */
614   if (PREDICT_TRUE (written == data_len))
615     {
616       sctp_conn->next_tsn_expected += written;
617
618       SCTP_ADV_DBG ("CONN = %u, WRITTEN [%u] == DATA_LEN [%d]",
619                     sctp_conn->sub_conn[conn_idx].connection.c_index,
620                     written, data_len);
621     }
622   /* If more data written than expected, account for out-of-order bytes. */
623   else if (written > data_len)
624     {
625       sctp_conn->next_tsn_expected += written;
626
627       SCTP_ADV_DBG ("CONN = %u, WRITTEN [%u] > DATA_LEN [%d]",
628                     sctp_conn->sub_conn[conn_idx].connection.c_index,
629                     written, data_len);
630     }
631   else if (written > 0)
632     {
633       /* We've written something but FIFO is probably full now */
634       sctp_conn->next_tsn_expected += written;
635
636       error = SCTP_ERROR_PARTIALLY_ENQUEUED;
637
638       SCTP_ADV_DBG
639         ("CONN = %u, WRITTEN [%u] > 0 (SCTP_ERROR_PARTIALLY_ENQUEUED)",
640          sctp_conn->sub_conn[conn_idx].connection.c_index, written);
641     }
642   else
643     {
644       SCTP_ADV_DBG ("CONN = %u, WRITTEN == 0 (SCTP_ERROR_FIFO_FULL)",
645                     sctp_conn->sub_conn[conn_idx].connection.c_index);
646
647       return SCTP_ERROR_FIFO_FULL;
648     }
649
650   return error;
651 }
652
653 always_inline u8
654 sctp_is_sack_delayable (sctp_connection_t * sctp_conn, u8 idx, u8 is_gapping)
655 {
656   if (is_gapping != 0)
657     {
658       SCTP_CONN_TRACKING_DBG
659         ("gapping != 0: CONN_INDEX = %u, sctp_conn->ack_state = %u",
660          sctp_conn->sub_conn[idx].connection.c_index, sctp_conn->ack_state);
661       return 0;
662     }
663
664   if (sctp_conn->ack_state >= MAX_ENQUEABLE_SACKS)
665     {
666       SCTP_CONN_TRACKING_DBG
667         ("sctp_conn->ack_state >= MAX_ENQUEABLE_SACKS: CONN_INDEX = %u, sctp_conn->ack_state = %u",
668          sctp_conn->sub_conn[idx].connection.c_index, sctp_conn->ack_state);
669       return 0;
670     }
671
672   sctp_conn->ack_state += 1;
673
674   return 1;
675 }
676
677 always_inline void
678 sctp_is_connection_gapping (sctp_connection_t * sctp_conn, u32 tsn,
679                             u8 * gapping)
680 {
681   if (sctp_conn->next_tsn_expected != tsn)      // It means data transmission is GAPPING
682     {
683       SCTP_CONN_TRACKING_DBG
684         ("GAPPING: CONN_INDEX = %u, sctp_conn->next_tsn_expected = %u, tsn = %u, diff = %u",
685          sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index,
686          sctp_conn->next_tsn_expected, tsn,
687          sctp_conn->next_tsn_expected - tsn);
688
689       *gapping = 1;
690     }
691 }
692
693 always_inline u16
694 sctp_handle_data (sctp_payload_data_chunk_t * sctp_data_chunk,
695                   sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b,
696                   u16 * next0)
697 {
698   u32 error = 0, n_data_bytes;
699   u8 is_gapping = 0;
700
701   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
702   if (sctp_conn->local_tag != sctp_data_chunk->sctp_hdr.verification_tag)
703     {
704       return SCTP_ERROR_INVALID_TAG;
705     }
706
707   vnet_buffer (b)->sctp.sid = sctp_data_chunk->stream_id;
708   vnet_buffer (b)->sctp.ssn = sctp_data_chunk->stream_seq;
709
710   u32 tsn = clib_net_to_host_u32 (sctp_data_chunk->tsn);
711
712   vlib_buffer_advance (b, vnet_buffer (b)->sctp.data_offset);
713   n_data_bytes = vnet_buffer (b)->sctp.data_len;
714   ASSERT (n_data_bytes);
715
716   sctp_is_connection_gapping (sctp_conn, tsn, &is_gapping);
717
718   sctp_conn->last_rcvd_tsn = tsn;
719
720   SCTP_ADV_DBG ("POINTER_WITH_DATA = %p", b->data);
721
722   u8 bbit = vnet_sctp_get_bbit (&sctp_data_chunk->chunk_hdr);
723   u8 ebit = vnet_sctp_get_ebit (&sctp_data_chunk->chunk_hdr);
724
725   if (bbit == 1 && ebit == 1)   /* Unfragmented message */
726     {
727       /* In order data, enqueue. Fifo figures out by itself if any out-of-order
728        * segments can be enqueued after fifo tail offset changes. */
729       if (PREDICT_FALSE (is_gapping == 1))
730         error =
731           sctp_session_enqueue_data_ooo (sctp_conn, b, n_data_bytes, idx);
732       else
733         error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
734     }
735   else if (bbit == 1 && ebit == 0)      /* First piece of a fragmented user message */
736     {
737       error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
738     }
739   else if (bbit == 0 && ebit == 1)      /* Last piece of a fragmented user message */
740     {
741       if (PREDICT_FALSE (is_gapping == 1))
742         error =
743           sctp_session_enqueue_data_ooo (sctp_conn, b, n_data_bytes, idx);
744       else
745         error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
746     }
747   else                          /* Middle piece of a fragmented user message */
748     {
749       if (PREDICT_FALSE (is_gapping == 1))
750         error =
751           sctp_session_enqueue_data_ooo (sctp_conn, b, n_data_bytes, idx);
752       else
753         error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
754     }
755   sctp_conn->last_rcvd_tsn = tsn;
756
757   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
758
759   SCTP_ADV_DBG ("POINTER_WITH_DATA = %p", b->data);
760
761   if (!sctp_is_sack_delayable (sctp_conn, idx, is_gapping))
762     sctp_prepare_sack_chunk (sctp_conn, idx, b);
763
764   sctp_conn->sub_conn[idx].enqueue_state = error;
765
766   return error;
767 }
768
769 always_inline u16
770 sctp_handle_cookie_echo (sctp_header_t * sctp_hdr,
771                          sctp_chunks_common_hdr_t * sctp_chunk_hdr,
772                          sctp_connection_t * sctp_conn, u8 idx,
773                          vlib_buffer_t * b0, u16 * next0)
774 {
775   u32 now = sctp_time_now ();
776
777   sctp_cookie_echo_chunk_t *cookie_echo =
778     (sctp_cookie_echo_chunk_t *) sctp_hdr;
779
780   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
781   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
782     {
783       return SCTP_ERROR_INVALID_TAG;
784     }
785
786   sctp_calculate_rto (sctp_conn, idx);
787
788   u32 creation_time =
789     clib_net_to_host_u32 (cookie_echo->cookie.creation_time);
790   u32 cookie_lifespan =
791     clib_net_to_host_u32 (cookie_echo->cookie.cookie_lifespan);
792   if (now > creation_time + cookie_lifespan)
793     {
794       SCTP_DBG ("now (%u) > creation_time (%u) + cookie_lifespan (%u)",
795                 now, creation_time, cookie_lifespan);
796       return SCTP_ERROR_COOKIE_ECHO_VIOLATION;
797     }
798
799   sctp_prepare_cookie_ack_chunk (sctp_conn, idx, b0);
800
801   /* Change state */
802   sctp_conn->state = SCTP_STATE_ESTABLISHED;
803   sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_STATE_UP;
804   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].c_is_ip4);
805
806   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
807                   sctp_conn->sub_conn[idx].RTO);
808
809   return SCTP_ERROR_NONE;
810
811 }
812
813 always_inline u16
814 sctp_handle_cookie_ack (sctp_header_t * sctp_hdr,
815                         sctp_chunks_common_hdr_t * sctp_chunk_hdr,
816                         sctp_connection_t * sctp_conn, u8 idx,
817                         vlib_buffer_t * b0, u16 * next0)
818 {
819   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
820   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
821     {
822       return SCTP_ERROR_INVALID_TAG;
823     }
824
825   sctp_calculate_rto (sctp_conn, idx);
826
827   sctp_timer_reset (sctp_conn, idx, SCTP_TIMER_T1_COOKIE);
828   /* Change state */
829   sctp_conn->state = SCTP_STATE_ESTABLISHED;
830   sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_STATE_UP;
831
832   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
833
834   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
835                   sctp_conn->sub_conn[idx].RTO);
836
837   return SCTP_ERROR_NONE;
838
839 }
840
841 always_inline uword
842 sctp46_rcv_phase_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
843                          vlib_frame_t * from_frame, int is_ip4)
844 {
845   sctp_main_t *tm = vnet_get_sctp_main ();
846
847   u32 n_left_from, next_index, *from, *to_next;
848   u32 my_thread_index = vm->thread_index;
849
850   from = vlib_frame_vector_args (from_frame);
851   n_left_from = from_frame->n_vectors;
852
853   next_index = node->cached_next_index;
854
855   while (n_left_from > 0)
856     {
857       u32 n_left_to_next;
858
859       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
860
861       while (n_left_from > 0 && n_left_to_next > 0)
862         {
863           u32 bi0;
864           vlib_buffer_t *b0;
865           sctp_header_t *sctp_hdr = 0;
866           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
867           ip4_header_t *ip4_hdr = 0;
868           ip6_header_t *ip6_hdr = 0;
869           sctp_connection_t *sctp_conn, *new_sctp_conn;
870           u16 sctp_implied_length = 0;
871           u16 error0 = SCTP_ERROR_NONE, next0 = sctp_next_drop (is_ip4);
872           u8 idx;
873
874           bi0 = from[0];
875           to_next[0] = bi0;
876           from += 1;
877           to_next += 1;
878           n_left_from -= 1;
879           n_left_to_next -= 1;
880
881           b0 = vlib_get_buffer (vm, bi0);
882
883           /* If we are in SCTP_COOKIE_WAIT_STATE then the connection
884            * will come from the half-open connections pool.
885            */
886           sctp_conn =
887             sctp_half_open_connection_get (vnet_buffer (b0)->
888                                            sctp.connection_index);
889
890           if (PREDICT_FALSE (sctp_conn == 0))
891             {
892               SCTP_ADV_DBG
893                 ("sctp_conn == NULL; return SCTP_ERROR_INVALID_CONNECTION");
894               error0 = SCTP_ERROR_INVALID_CONNECTION;
895               goto drop;
896             }
897           if (is_ip4)
898             {
899               ip4_hdr = vlib_buffer_get_current (b0);
900               sctp_hdr = ip4_next_header (ip4_hdr);
901               idx = sctp_sub_conn_id_via_ip4h (sctp_conn, ip4_hdr);
902             }
903           else
904             {
905               ip6_hdr = vlib_buffer_get_current (b0);
906               sctp_hdr = ip6_next_header (ip6_hdr);
907               idx = sctp_sub_conn_id_via_ip6h (sctp_conn, ip6_hdr);
908             }
909
910           sctp_conn->sub_conn[idx].parent = sctp_conn;
911           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
912
913           sctp_chunk_hdr =
914             (sctp_chunks_common_hdr_t *) (&full_hdr->common_hdr);
915
916           sctp_implied_length =
917             sctp_calculate_implied_length (ip4_hdr, ip6_hdr, is_ip4);
918
919           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
920
921           switch (chunk_type)
922             {
923             case INIT_ACK:
924               error0 =
925                 sctp_is_valid_init_ack (sctp_hdr, sctp_chunk_hdr, sctp_conn,
926                                         b0, sctp_implied_length);
927
928               if (error0 == SCTP_ERROR_NONE)
929                 {
930                   pool_get (tm->connections[my_thread_index], new_sctp_conn);
931                   clib_memcpy (new_sctp_conn, sctp_conn,
932                                sizeof (*new_sctp_conn));
933                   new_sctp_conn->sub_conn[idx].c_c_index =
934                     new_sctp_conn - tm->connections[my_thread_index];
935                   new_sctp_conn->sub_conn[idx].c_thread_index =
936                     my_thread_index;
937                   new_sctp_conn->sub_conn[idx].PMTU =
938                     sctp_conn->sub_conn[idx].PMTU;
939                   new_sctp_conn->sub_conn[idx].parent = new_sctp_conn;
940
941                   if (sctp_half_open_connection_cleanup (sctp_conn))
942                     {
943                       SCTP_DBG
944                         ("Cannot cleanup half-open connection; not the owning thread");
945                     }
946
947                   sctp_connection_timers_init (new_sctp_conn);
948
949                   error0 =
950                     sctp_handle_init_ack (sctp_hdr, sctp_chunk_hdr,
951                                           new_sctp_conn, idx, b0,
952                                           sctp_implied_length);
953
954                   sctp_init_cwnd (new_sctp_conn);
955
956                   if (session_stream_connect_notify
957                       (&new_sctp_conn->sub_conn[idx].connection, 0))
958                     {
959                       SCTP_DBG
960                         ("conn_index = %u: session_stream_connect_notify error; cleaning up connection",
961                          new_sctp_conn->sub_conn[idx].connection.c_index);
962                       sctp_connection_cleanup (new_sctp_conn);
963                       goto drop;
964                     }
965                   next0 = sctp_next_output (is_ip4);
966                 }
967               break;
968
969               /* All UNEXPECTED scenarios (wrong chunk received per state-machine)
970                * are handled by the input-dispatcher function using the table-lookup
971                * hence we should never get to the "default" case below.
972                */
973             default:
974               error0 = SCTP_ERROR_UNKOWN_CHUNK;
975               next0 = sctp_next_drop (is_ip4);
976               goto drop;
977             }
978
979           if (error0 != SCTP_ERROR_NONE)
980             {
981               clib_warning ("error while parsing chunk");
982               sctp_connection_cleanup (sctp_conn);
983               next0 = sctp_next_drop (is_ip4);
984               goto drop;
985             }
986
987         drop:
988           b0->error = node->errors[error0];
989           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
990             {
991               sctp_rx_trace_t *t0 =
992                 vlib_add_trace (vm, node, b0, sizeof (*t0));
993               sctp_set_rx_trace_data (t0, sctp_conn, sctp_hdr, b0, is_ip4);
994             }
995
996           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
997                                            n_left_to_next, bi0, next0);
998         }
999
1000       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1001     }
1002   return from_frame->n_vectors;
1003 }
1004
1005 static uword
1006 sctp4_rcv_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1007                  vlib_frame_t * from_frame)
1008 {
1009   return sctp46_rcv_phase_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1010 }
1011
1012 static uword
1013 sctp6_rcv_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1014                  vlib_frame_t * from_frame)
1015 {
1016   return sctp46_rcv_phase_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1017 }
1018
1019 u8 *
1020 format_sctp_rx_trace_short (u8 * s, va_list * args)
1021 {
1022   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1023   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1024   sctp_rx_trace_t *t = va_arg (*args, sctp_rx_trace_t *);
1025
1026   s = format (s, "%d -> %d (%U)",
1027               clib_net_to_host_u16 (t->sctp_header.src_port),
1028               clib_net_to_host_u16 (t->sctp_header.dst_port),
1029               format_sctp_state, t->sctp_connection.state);
1030
1031   return s;
1032 }
1033
1034 /* *INDENT-OFF* */
1035 VLIB_REGISTER_NODE (sctp4_rcv_phase_node) =
1036 {
1037   .function = sctp4_rcv_phase,
1038   .name = "sctp4-rcv",
1039   /* Takes a vector of packets. */
1040   .vector_size = sizeof (u32),
1041   .n_errors = SCTP_N_ERROR,
1042   .error_strings = sctp_error_strings,
1043   .n_next_nodes = SCTP_RCV_PHASE_N_NEXT,
1044   .next_nodes =
1045   {
1046 #define _(s,n) [SCTP_RCV_PHASE_NEXT_##s] = n,
1047     foreach_sctp_state_next
1048 #undef _
1049   },
1050   .format_trace = format_sctp_rx_trace_short,
1051 };
1052 /* *INDENT-ON* */
1053
1054 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_rcv_phase_node, sctp4_rcv_phase);
1055
1056 /* *INDENT-OFF* */
1057 VLIB_REGISTER_NODE (sctp6_init_phase_node) =
1058 {
1059   .function = sctp6_rcv_phase,
1060   .name = "sctp6-rcv",
1061   /* Takes a vector of packets. */
1062   .vector_size = sizeof (u32),
1063   .n_errors = SCTP_N_ERROR,
1064   .error_strings = sctp_error_strings,
1065   .n_next_nodes = SCTP_RCV_PHASE_N_NEXT,
1066   .next_nodes =
1067   {
1068 #define _(s,n) [SCTP_RCV_PHASE_NEXT_##s] = n,
1069     foreach_sctp_state_next
1070 #undef _
1071   },
1072   .format_trace = format_sctp_rx_trace_short,
1073 };
1074 /* *INDENT-ON* */
1075
1076 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_init_phase_node, sctp6_rcv_phase);
1077
1078 vlib_node_registration_t sctp4_shutdown_phase_node;
1079 vlib_node_registration_t sctp6_shutdown_phase_node;
1080
1081 always_inline u16
1082 sctp_handle_shutdown (sctp_header_t * sctp_hdr,
1083                       sctp_chunks_common_hdr_t * sctp_chunk_hdr,
1084                       sctp_connection_t * sctp_conn, u8 idx,
1085                       vlib_buffer_t * b0, u16 sctp_implied_length,
1086                       u16 * next0)
1087 {
1088   sctp_shutdown_association_chunk_t *shutdown_chunk =
1089     (sctp_shutdown_association_chunk_t *) (sctp_hdr);
1090
1091   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1092   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
1093     {
1094       return SCTP_ERROR_INVALID_TAG;
1095     }
1096
1097   /*
1098    * It is not possible to bundle any other CHUNK with the SHUTDOWN chunk
1099    */
1100   if (sctp_is_bundling (sctp_implied_length, &shutdown_chunk->chunk_hdr))
1101     return SCTP_ERROR_BUNDLING_VIOLATION;
1102
1103   switch (sctp_conn->state)
1104     {
1105     case SCTP_STATE_ESTABLISHED:
1106       if (sctp_check_outstanding_data_chunks (sctp_conn) == 0)
1107         sctp_conn->state = SCTP_STATE_SHUTDOWN_RECEIVED;
1108       sctp_send_shutdown_ack (sctp_conn, idx, b0);
1109       break;
1110
1111     case SCTP_STATE_SHUTDOWN_SENT:
1112       sctp_send_shutdown_ack (sctp_conn, idx, b0);
1113       break;
1114     }
1115
1116   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].c_is_ip4);
1117
1118   return SCTP_ERROR_NONE;
1119 }
1120
1121 always_inline u16
1122 sctp_handle_shutdown_ack (sctp_header_t * sctp_hdr,
1123                           sctp_chunks_common_hdr_t * sctp_chunk_hdr,
1124                           sctp_connection_t * sctp_conn, u8 idx,
1125                           vlib_buffer_t * b0, u16 sctp_implied_length,
1126                           u16 * next0)
1127 {
1128   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
1129     (sctp_shutdown_ack_chunk_t *) (sctp_hdr);
1130
1131   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1132   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
1133     {
1134       return SCTP_ERROR_INVALID_TAG;
1135     }
1136
1137   /*
1138    * It is not possible to bundle any other CHUNK with the SHUTDOWN chunk
1139    */
1140   if (sctp_is_bundling (sctp_implied_length, &shutdown_ack_chunk->chunk_hdr))
1141     return SCTP_ERROR_BUNDLING_VIOLATION;
1142
1143   /* Whether we are in SCTP_STATE_SHUTDOWN_SENT or SCTP_STATE_SHUTDOWN_ACK_SENT
1144    * the reception of a SHUTDOWN_ACK chunk leads to the same actions:
1145    * - STOP T2_SHUTDOWN timer
1146    * - SEND SHUTDOWN_COMPLETE chunk
1147    */
1148   sctp_timer_reset (sctp_conn, MAIN_SCTP_SUB_CONN_IDX,
1149                     SCTP_TIMER_T2_SHUTDOWN);
1150
1151   sctp_send_shutdown_complete (sctp_conn, idx, b0);
1152
1153   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].c_is_ip4);
1154
1155   return SCTP_ERROR_NONE;
1156 }
1157
1158 always_inline u16
1159 sctp_handle_shutdown_complete (sctp_header_t * sctp_hdr,
1160                                sctp_chunks_common_hdr_t * sctp_chunk_hdr,
1161                                sctp_connection_t * sctp_conn, u8 idx,
1162                                vlib_buffer_t * b0, u16 sctp_implied_length,
1163                                u16 * next0)
1164 {
1165   sctp_shutdown_complete_chunk_t *shutdown_complete =
1166     (sctp_shutdown_complete_chunk_t *) (sctp_hdr);
1167
1168   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1169   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
1170     {
1171       return SCTP_ERROR_INVALID_TAG;
1172     }
1173
1174   /*
1175    * It is not possible to bundle any other CHUNK with the SHUTDOWN chunk
1176    */
1177   if (sctp_is_bundling (sctp_implied_length, &shutdown_complete->chunk_hdr))
1178     return SCTP_ERROR_BUNDLING_VIOLATION;
1179
1180   sctp_timer_reset (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN);
1181
1182   stream_session_disconnect_notify (&sctp_conn->sub_conn[idx].connection);
1183
1184   sctp_conn->state = SCTP_STATE_CLOSED;
1185
1186   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
1187
1188   return SCTP_ERROR_NONE;
1189 }
1190
1191 always_inline uword
1192 sctp46_shutdown_phase_inline (vlib_main_t * vm,
1193                               vlib_node_runtime_t * node,
1194                               vlib_frame_t * from_frame, int is_ip4)
1195 {
1196   u32 n_left_from, next_index, *from, *to_next;
1197   u32 my_thread_index = vm->thread_index;
1198
1199   from = vlib_frame_vector_args (from_frame);
1200   n_left_from = from_frame->n_vectors;
1201
1202   next_index = node->cached_next_index;
1203
1204   while (n_left_from > 0)
1205     {
1206       u32 n_left_to_next;
1207
1208       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1209
1210       while (n_left_from > 0 && n_left_to_next > 0)
1211         {
1212           u32 bi0;
1213           vlib_buffer_t *b0;
1214           sctp_rx_trace_t *sctp_trace;
1215           sctp_header_t *sctp_hdr = 0;
1216           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
1217           ip4_header_t *ip4_hdr = 0;
1218           ip6_header_t *ip6_hdr = 0;
1219           sctp_connection_t *sctp_conn;
1220           u16 sctp_implied_length = 0;
1221           u16 error0 = SCTP_ERROR_NONE, next0 = SCTP_RCV_PHASE_N_NEXT;
1222           u8 idx = 0;
1223
1224           bi0 = from[0];
1225           to_next[0] = bi0;
1226           from += 1;
1227           to_next += 1;
1228           n_left_from -= 1;
1229           n_left_to_next -= 1;
1230
1231           b0 = vlib_get_buffer (vm, bi0);
1232           sctp_conn =
1233             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1234                                  my_thread_index);
1235
1236           if (PREDICT_FALSE (sctp_conn == 0))
1237             {
1238               SCTP_DBG
1239                 ("sctp_conn == NULL; return SCTP_ERROR_INVALID_CONNECTION");
1240               error0 = SCTP_ERROR_INVALID_CONNECTION;
1241               goto drop;
1242             }
1243
1244           if (is_ip4)
1245             {
1246               ip4_hdr = vlib_buffer_get_current (b0);
1247               sctp_hdr = ip4_next_header (ip4_hdr);
1248               idx = sctp_sub_conn_id_via_ip4h (sctp_conn, ip4_hdr);
1249             }
1250           else
1251             {
1252               ip6_hdr = vlib_buffer_get_current (b0);
1253               sctp_hdr = ip6_next_header (ip6_hdr);
1254               idx = sctp_sub_conn_id_via_ip6h (sctp_conn, ip6_hdr);
1255             }
1256
1257           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1258           sctp_chunk_hdr = &full_hdr->common_hdr;
1259
1260           sctp_implied_length =
1261             sctp_calculate_implied_length (ip4_hdr, ip6_hdr, is_ip4);
1262
1263           u8 chunk_type = vnet_sctp_get_chunk_type (sctp_chunk_hdr);
1264           switch (chunk_type)
1265             {
1266             case SHUTDOWN:
1267               error0 =
1268                 sctp_handle_shutdown (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1269                                       idx, b0, sctp_implied_length, &next0);
1270               break;
1271
1272             case SHUTDOWN_ACK:
1273               error0 =
1274                 sctp_handle_shutdown_ack (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1275                                           idx, b0, sctp_implied_length,
1276                                           &next0);
1277               break;
1278
1279             case SHUTDOWN_COMPLETE:
1280               error0 =
1281                 sctp_handle_shutdown_complete (sctp_hdr, sctp_chunk_hdr,
1282                                                sctp_conn, idx, b0,
1283                                                sctp_implied_length, &next0);
1284
1285               sctp_connection_cleanup (sctp_conn);
1286               break;
1287
1288               /*
1289                * DATA chunks can still be transmitted/received in the SHUTDOWN-PENDING
1290                * and SHUTDOWN-SENT states (as per RFC4960 Section 6)
1291                */
1292             case DATA:
1293               error0 =
1294                 sctp_handle_data ((sctp_payload_data_chunk_t *) sctp_hdr,
1295                                   sctp_conn, idx, b0, &next0);
1296               break;
1297
1298               /* All UNEXPECTED scenarios (wrong chunk received per state-machine)
1299                * are handled by the input-dispatcher function using the table-lookup
1300                * hence we should never get to the "default" case below.
1301                */
1302             default:
1303               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1304               next0 = sctp_next_drop (is_ip4);
1305               goto drop;
1306             }
1307
1308           if (error0 != SCTP_ERROR_NONE)
1309             {
1310               clib_warning ("error while parsing chunk");
1311               sctp_connection_cleanup (sctp_conn);
1312               next0 = sctp_next_drop (is_ip4);
1313               goto drop;
1314             }
1315
1316         drop:
1317           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1318             {
1319               sctp_trace =
1320                 vlib_add_trace (vm, node, b0, sizeof (*sctp_trace));
1321
1322               if (sctp_hdr != NULL)
1323                 clib_memcpy (&sctp_trace->sctp_header, sctp_hdr,
1324                              sizeof (sctp_trace->sctp_header));
1325
1326               if (sctp_conn != NULL)
1327                 clib_memcpy (&sctp_trace->sctp_connection, sctp_conn,
1328                              sizeof (sctp_trace->sctp_connection));
1329             }
1330
1331           b0->error = node->errors[error0];
1332
1333           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1334                                            n_left_to_next, bi0, next0);
1335         }
1336
1337       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1338     }
1339
1340   return from_frame->n_vectors;
1341
1342 }
1343
1344 static uword
1345 sctp4_shutdown_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1346                       vlib_frame_t * from_frame)
1347 {
1348   return sctp46_shutdown_phase_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1349 }
1350
1351 static uword
1352 sctp6_shutdown_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1353                       vlib_frame_t * from_frame)
1354 {
1355   return sctp46_shutdown_phase_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1356 }
1357
1358 /* *INDENT-OFF* */
1359 VLIB_REGISTER_NODE (sctp4_shutdown_phase_node) =
1360 {
1361   .function = sctp4_shutdown_phase,
1362   .name = "sctp4-shutdown",
1363   /* Takes a vector of packets. */
1364   .vector_size = sizeof (u32),
1365   .n_errors = SCTP_N_ERROR,
1366   .error_strings = sctp_error_strings,
1367   .n_next_nodes = SCTP_SHUTDOWN_PHASE_N_NEXT,
1368   .next_nodes =
1369   {
1370 #define _(s,n) [SCTP_SHUTDOWN_PHASE_NEXT_##s] = n,
1371     foreach_sctp_state_next
1372 #undef _
1373   },
1374   .format_trace = format_sctp_rx_trace_short,
1375 };
1376 /* *INDENT-ON* */
1377
1378 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_shutdown_phase_node,
1379                               sctp4_shutdown_phase);
1380
1381 /* *INDENT-OFF* */
1382 VLIB_REGISTER_NODE (sctp6_shutdown_phase_node) =
1383 {
1384   .function = sctp6_shutdown_phase,
1385   .name = "sctp6-shutdown",
1386   /* Takes a vector of packets. */
1387   .vector_size = sizeof (u32),
1388   .n_errors = SCTP_N_ERROR,
1389   .error_strings = sctp_error_strings,
1390   .n_next_nodes = SCTP_SHUTDOWN_PHASE_N_NEXT,
1391   .next_nodes =
1392   {
1393 #define _(s,n) [SCTP_SHUTDOWN_PHASE_NEXT_##s] = n,
1394     foreach_sctp_state_next
1395 #undef _
1396   },
1397   .format_trace = format_sctp_rx_trace_short,
1398 };
1399 /* *INDENT-ON* */
1400
1401 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_shutdown_phase_node,
1402                               sctp6_shutdown_phase);
1403
1404 vlib_node_registration_t sctp4_listen_phase_node;
1405 vlib_node_registration_t sctp6_listen_phase_node;
1406
1407 vlib_node_registration_t sctp4_established_phase_node;
1408 vlib_node_registration_t sctp6_established_phase_node;
1409
1410 always_inline u16
1411 sctp_handle_sack (sctp_selective_ack_chunk_t * sack_chunk,
1412                   sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b0,
1413                   u16 * next0)
1414 {
1415   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1416   if (sctp_conn->local_tag != sack_chunk->sctp_hdr.verification_tag)
1417     {
1418       return SCTP_ERROR_INVALID_TAG;
1419     }
1420
1421   sctp_conn->sub_conn[idx].last_seen = sctp_time_now ();
1422
1423   /* Section 7.2.2; point (2) */
1424   if (sctp_conn->sub_conn[idx].cwnd > sctp_conn->sub_conn[idx].ssthresh)
1425     sctp_conn->sub_conn[idx].partially_acked_bytes =
1426       sctp_conn->next_tsn - sack_chunk->cumulative_tsn_ack;
1427
1428   /* Section 7.2.2; point (5) */
1429   if (sctp_conn->next_tsn - sack_chunk->cumulative_tsn_ack == 0)
1430     sctp_conn->sub_conn[idx].partially_acked_bytes = 0;
1431
1432   sctp_conn->last_unacked_tsn = sack_chunk->cumulative_tsn_ack;
1433
1434   sctp_calculate_rto (sctp_conn, idx);
1435
1436   sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1437                      sctp_conn->sub_conn[idx].RTO);
1438
1439   sctp_conn->sub_conn[idx].RTO_pending = 0;
1440
1441   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
1442
1443   return SCTP_ERROR_NONE;
1444 }
1445
1446 always_inline u16
1447 sctp_handle_heartbeat (sctp_hb_req_chunk_t * sctp_hb_chunk,
1448                        sctp_connection_t * sctp_conn, u8 idx,
1449                        vlib_buffer_t * b0, u16 * next0)
1450 {
1451   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1452   if (sctp_conn->local_tag != sctp_hb_chunk->sctp_hdr.verification_tag)
1453     {
1454       return SCTP_ERROR_INVALID_TAG;
1455     }
1456
1457   sctp_prepare_heartbeat_ack_chunk (sctp_conn, idx, b0);
1458
1459   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].connection.is_ip4);
1460
1461   return SCTP_ERROR_NONE;
1462 }
1463
1464 always_inline u16
1465 sctp_handle_heartbeat_ack (sctp_hb_ack_chunk_t * sctp_hb_ack_chunk,
1466                            sctp_connection_t * sctp_conn, u8 idx,
1467                            vlib_buffer_t * b0, u16 * next0)
1468 {
1469   sctp_conn->sub_conn[idx].last_seen = sctp_time_now ();
1470
1471   sctp_conn->sub_conn[idx].unacknowledged_hb -= 1;
1472
1473   sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
1474                      sctp_conn->sub_conn[idx].RTO);
1475
1476   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
1477
1478   return SCTP_ERROR_NONE;
1479 }
1480
1481 always_inline void
1482 sctp_node_inc_counter (vlib_main_t * vm, u32 tcp4_node, u32 tcp6_node,
1483                        u8 is_ip4, u8 evt, u8 val)
1484 {
1485   if (PREDICT_TRUE (!val))
1486     return;
1487
1488   if (is_ip4)
1489     vlib_node_increment_counter (vm, tcp4_node, evt, val);
1490   else
1491     vlib_node_increment_counter (vm, tcp6_node, evt, val);
1492 }
1493
1494 always_inline uword
1495 sctp46_listen_process_inline (vlib_main_t * vm,
1496                               vlib_node_runtime_t * node,
1497                               vlib_frame_t * from_frame, int is_ip4)
1498 {
1499   u32 n_left_from, next_index, *from, *to_next;
1500   u32 my_thread_index = vm->thread_index;
1501
1502   from = vlib_frame_vector_args (from_frame);
1503   n_left_from = from_frame->n_vectors;
1504
1505   next_index = node->cached_next_index;
1506
1507   while (n_left_from > 0)
1508     {
1509       u32 n_left_to_next;
1510
1511       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1512
1513       while (n_left_from > 0 && n_left_to_next > 0)
1514         {
1515           u32 bi0;
1516           vlib_buffer_t *b0;
1517           sctp_header_t *sctp_hdr = 0;
1518           ip4_header_t *ip4_hdr;
1519           ip6_header_t *ip6_hdr;
1520           sctp_connection_t *child_conn;
1521           sctp_connection_t *sctp_listener;
1522           u16 next0 = sctp_next_drop (is_ip4), error0 = SCTP_ERROR_ENQUEUED;
1523
1524           bi0 = from[0];
1525           to_next[0] = bi0;
1526           from += 1;
1527           to_next += 1;
1528           n_left_from -= 1;
1529           n_left_to_next -= 1;
1530
1531           b0 = vlib_get_buffer (vm, bi0);
1532           sctp_listener =
1533             sctp_listener_get (vnet_buffer (b0)->sctp.connection_index);
1534
1535           if (is_ip4)
1536             {
1537               ip4_hdr = vlib_buffer_get_current (b0);
1538               sctp_hdr = ip4_next_header (ip4_hdr);
1539             }
1540           else
1541             {
1542               ip6_hdr = vlib_buffer_get_current (b0);
1543               sctp_hdr = ip6_next_header (ip6_hdr);
1544             }
1545
1546           child_conn =
1547             sctp_lookup_connection (sctp_listener->sub_conn
1548                                     [MAIN_SCTP_SUB_CONN_IDX].c_fib_index, b0,
1549                                     my_thread_index, is_ip4);
1550
1551           if (PREDICT_FALSE (child_conn->state != SCTP_STATE_CLOSED))
1552             {
1553               SCTP_DBG
1554                 ("conn_index = %u: child_conn->state != SCTP_STATE_CLOSED.... STATE=%s",
1555                  child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
1556                  connection.c_index,
1557                  sctp_state_to_string (child_conn->state));
1558               error0 = SCTP_ERROR_CREATE_EXISTS;
1559               goto drop;
1560             }
1561
1562           /* Create child session and send SYN-ACK */
1563           child_conn = sctp_connection_new (my_thread_index);
1564           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].parent = child_conn;
1565           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_lcl_port =
1566             sctp_hdr->dst_port;
1567           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_rmt_port =
1568             sctp_hdr->src_port;
1569           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_is_ip4 = is_ip4;
1570           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.proto =
1571             sctp_listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.proto;
1572           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].PMTU =
1573             sctp_listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].PMTU;
1574           child_conn->state = SCTP_STATE_CLOSED;
1575
1576           if (is_ip4)
1577             {
1578               child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_lcl_ip4.as_u32 =
1579                 ip4_hdr->dst_address.as_u32;
1580               child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_rmt_ip4.as_u32 =
1581                 ip4_hdr->src_address.as_u32;
1582             }
1583           else
1584             {
1585               clib_memcpy (&child_conn->
1586                            sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_lcl_ip6,
1587                            &ip6_hdr->dst_address, sizeof (ip6_address_t));
1588               clib_memcpy (&child_conn->
1589                            sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_rmt_ip6,
1590                            &ip6_hdr->src_address, sizeof (ip6_address_t));
1591             }
1592
1593           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1594           sctp_chunks_common_hdr_t *sctp_chunk_hdr = &full_hdr->common_hdr;
1595
1596           u8 chunk_type = vnet_sctp_get_chunk_type (sctp_chunk_hdr);
1597           if (chunk_type != INIT)
1598             {
1599               SCTP_DBG
1600                 ("conn_index = %u: chunk_type != INIT... chunk_type=%s",
1601                  child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
1602                  connection.c_index, sctp_chunk_to_string (chunk_type));
1603
1604               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1605               next0 = sctp_next_drop (is_ip4);
1606               goto drop;
1607             }
1608
1609           u16 sctp_implied_length =
1610             sctp_calculate_implied_length (ip4_hdr, ip6_hdr, is_ip4);
1611
1612           switch (chunk_type)
1613             {
1614             case INIT:
1615               sctp_connection_timers_init (child_conn);
1616
1617               sctp_init_snd_vars (child_conn);
1618
1619               error0 =
1620                 sctp_handle_init (sctp_hdr, sctp_chunk_hdr, child_conn, b0,
1621                                   sctp_implied_length);
1622
1623               sctp_init_cwnd (child_conn);
1624
1625               if (error0 == SCTP_ERROR_NONE)
1626                 {
1627                   if (stream_session_accept
1628                       (&child_conn->
1629                        sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection,
1630                        sctp_listener->
1631                        sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_s_index, 0))
1632                     {
1633                       clib_warning ("session accept fail");
1634                       sctp_connection_cleanup (child_conn);
1635                       error0 = SCTP_ERROR_CREATE_SESSION_FAIL;
1636                       goto drop;
1637                     }
1638                   next0 = sctp_next_output (is_ip4);
1639                 }
1640               break;
1641
1642               /* Reception of a DATA chunk whilst in the CLOSED state is called
1643                * "Out of the Blue" packet and handling of the chunk needs special treatment
1644                * as per RFC4960 section 8.4
1645                */
1646             case DATA:
1647               break;
1648             }
1649
1650         drop:
1651           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1652             {
1653               sctp_rx_trace_t *t0 =
1654                 vlib_add_trace (vm, node, b0, sizeof (*t0));
1655               clib_memcpy (&t0->sctp_header, sctp_hdr,
1656                            sizeof (t0->sctp_header));
1657               clib_memcpy (&t0->sctp_connection, sctp_listener,
1658                            sizeof (t0->sctp_connection));
1659             }
1660
1661           b0->error = node->errors[error0];
1662
1663           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1664                                            n_left_to_next, bi0, next0);
1665         }
1666       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1667
1668     }
1669   return from_frame->n_vectors;
1670 }
1671
1672 static uword
1673 sctp4_listen_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1674                     vlib_frame_t * from_frame)
1675 {
1676   return sctp46_listen_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1677 }
1678
1679 static uword
1680 sctp6_listen_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1681                     vlib_frame_t * from_frame)
1682 {
1683   return sctp46_listen_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1684 }
1685
1686 always_inline uword
1687 sctp46_established_phase_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1688                                  vlib_frame_t * from_frame, int is_ip4)
1689 {
1690   u32 n_left_from, next_index, *from, *to_next;
1691   u32 my_thread_index = vm->thread_index, errors = 0;
1692
1693   from = vlib_frame_vector_args (from_frame);
1694   n_left_from = from_frame->n_vectors;
1695
1696   next_index = node->cached_next_index;
1697
1698   while (n_left_from > 0)
1699     {
1700       u32 n_left_to_next;
1701
1702       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1703
1704       while (n_left_from > 0 && n_left_to_next > 0)
1705         {
1706           u32 bi0;
1707           vlib_buffer_t *b0;
1708           sctp_header_t *sctp_hdr = 0;
1709           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
1710           ip4_header_t *ip4_hdr = 0;
1711           ip6_header_t *ip6_hdr = 0;
1712           sctp_connection_t *sctp_conn;
1713           u16 error0 = SCTP_ERROR_ENQUEUED, next0 =
1714             SCTP_ESTABLISHED_PHASE_N_NEXT;
1715           u8 idx;
1716
1717           bi0 = from[0];
1718           to_next[0] = bi0;
1719           from += 1;
1720           to_next += 1;
1721           n_left_from -= 1;
1722           n_left_to_next -= 1;
1723
1724           b0 = vlib_get_buffer (vm, bi0);
1725           sctp_conn =
1726             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1727                                  my_thread_index);
1728
1729           if (PREDICT_FALSE (sctp_conn == 0))
1730             {
1731               SCTP_DBG
1732                 ("sctp_conn == NULL; return SCTP_ERROR_INVALID_CONNECTION");
1733               error0 = SCTP_ERROR_INVALID_CONNECTION;
1734               goto done;
1735             }
1736           if (is_ip4)
1737             {
1738               ip4_hdr = vlib_buffer_get_current (b0);
1739               sctp_hdr = ip4_next_header (ip4_hdr);
1740               idx = sctp_sub_conn_id_via_ip4h (sctp_conn, ip4_hdr);
1741             }
1742           else
1743             {
1744               ip6_hdr = vlib_buffer_get_current (b0);
1745               sctp_hdr = ip6_next_header (ip6_hdr);
1746               idx = sctp_sub_conn_id_via_ip6h (sctp_conn, ip6_hdr);
1747             }
1748
1749           sctp_conn->sub_conn[idx].parent = sctp_conn;
1750
1751           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1752           sctp_chunk_hdr =
1753             (sctp_chunks_common_hdr_t *) (&full_hdr->common_hdr);
1754
1755           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1756
1757           switch (chunk_type)
1758             {
1759             case COOKIE_ECHO:
1760               error0 =
1761                 sctp_handle_cookie_echo (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1762                                          idx, b0, &next0);
1763               break;
1764
1765             case COOKIE_ACK:
1766               error0 =
1767                 sctp_handle_cookie_ack (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1768                                         idx, b0, &next0);
1769               break;
1770
1771             case SACK:
1772               error0 =
1773                 sctp_handle_sack ((sctp_selective_ack_chunk_t *) sctp_hdr,
1774                                   sctp_conn, idx, b0, &next0);
1775               break;
1776
1777             case HEARTBEAT:
1778               error0 =
1779                 sctp_handle_heartbeat ((sctp_hb_req_chunk_t *) sctp_hdr,
1780                                        sctp_conn, idx, b0, &next0);
1781               break;
1782
1783             case HEARTBEAT_ACK:
1784               error0 =
1785                 sctp_handle_heartbeat_ack ((sctp_hb_ack_chunk_t *) sctp_hdr,
1786                                            sctp_conn, idx, b0, &next0);
1787               break;
1788
1789             case DATA:
1790               error0 =
1791                 sctp_handle_data ((sctp_payload_data_chunk_t *) sctp_hdr,
1792                                   sctp_conn, idx, b0, &next0);
1793               break;
1794
1795               /* All UNEXPECTED scenarios (wrong chunk received per state-machine)
1796                * are handled by the input-dispatcher function using the table-lookup
1797                * hence we should never get to the "default" case below.
1798                */
1799             default:
1800               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1801               next0 = sctp_next_drop (is_ip4);
1802               goto done;
1803             }
1804
1805         done:
1806           b0->error = node->errors[error0];
1807           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1808             {
1809               sctp_rx_trace_t *t0 =
1810                 vlib_add_trace (vm, node, b0, sizeof (*t0));
1811               sctp_set_rx_trace_data (t0, sctp_conn, sctp_hdr, b0, is_ip4);
1812             }
1813
1814           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1815                                            n_left_to_next, bi0, next0);
1816         }
1817
1818       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1819     }
1820
1821   errors = session_manager_flush_enqueue_events (TRANSPORT_PROTO_SCTP,
1822                                                  my_thread_index);
1823
1824   sctp_node_inc_counter (vm, is_ip4, sctp4_established_phase_node.index,
1825                          sctp6_established_phase_node.index,
1826                          SCTP_ERROR_EVENT_FIFO_FULL, errors);
1827   sctp_flush_frame_to_output (vm, my_thread_index, is_ip4);
1828
1829   return from_frame->n_vectors;
1830 }
1831
1832 static uword
1833 sctp4_established_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1834                          vlib_frame_t * from_frame)
1835 {
1836   return sctp46_established_phase_inline (vm, node, from_frame,
1837                                           1 /* is_ip4 */ );
1838 }
1839
1840 static uword
1841 sctp6_established_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1842                          vlib_frame_t * from_frame)
1843 {
1844   return sctp46_established_phase_inline (vm, node, from_frame,
1845                                           0 /* is_ip4 */ );
1846 }
1847
1848 u8 *
1849 format_sctp_rx_trace (u8 * s, va_list * args)
1850 {
1851   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1852   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1853   sctp_rx_trace_t *t = va_arg (*args, sctp_rx_trace_t *);
1854   u32 indent = format_get_indent (s);
1855
1856   s = format (s, "%U\n%U%U",
1857               format_sctp_header, &t->sctp_header, 128,
1858               format_white_space, indent,
1859               format_sctp_connection, &t->sctp_connection, 1);
1860
1861   return s;
1862 }
1863
1864 /* *INDENT-OFF* */
1865 VLIB_REGISTER_NODE (sctp4_listen_phase_node) =
1866 {
1867   .function = sctp4_listen_phase,
1868   .name = "sctp4-listen",
1869   /* Takes a vector of packets. */
1870   .vector_size = sizeof (u32),
1871   .n_errors = SCTP_N_ERROR,
1872   .error_strings = sctp_error_strings,
1873   .n_next_nodes = SCTP_LISTEN_PHASE_N_NEXT,
1874   .next_nodes =
1875   {
1876 #define _(s,n) [SCTP_LISTEN_PHASE_NEXT_##s] = n,
1877     foreach_sctp_state_next
1878 #undef _
1879   },
1880   .format_trace = format_sctp_rx_trace_short,
1881 };
1882 /* *INDENT-ON* */
1883
1884 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_listen_phase_node, sctp4_listen_phase);
1885
1886 /* *INDENT-OFF* */
1887 VLIB_REGISTER_NODE (sctp6_listen_phase_node) =
1888 {
1889   .function = sctp6_listen_phase,
1890   .name = "sctp6-listen",
1891   /* Takes a vector of packets. */
1892   .vector_size = sizeof (u32),
1893   .n_errors = SCTP_N_ERROR,
1894   .error_strings = sctp_error_strings,
1895   .n_next_nodes = SCTP_LISTEN_PHASE_N_NEXT,
1896   .next_nodes =
1897   {
1898 #define _(s,n) [SCTP_LISTEN_PHASE_NEXT_##s] = n,
1899     foreach_sctp_state_next
1900 #undef _
1901   },
1902   .format_trace = format_sctp_rx_trace_short,
1903 };
1904 /* *INDENT-ON* */
1905
1906 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_listen_phase_node, sctp6_listen_phase);
1907
1908 /* *INDENT-OFF* */
1909 VLIB_REGISTER_NODE (sctp4_established_phase_node) =
1910 {
1911   .function = sctp4_established_phase,
1912   .name = "sctp4-established",
1913   /* Takes a vector of packets. */
1914   .vector_size = sizeof (u32),
1915   .n_errors = SCTP_N_ERROR,
1916   .error_strings = sctp_error_strings,
1917   .n_next_nodes = SCTP_ESTABLISHED_PHASE_N_NEXT,
1918   .next_nodes =
1919   {
1920 #define _(s,n) [SCTP_ESTABLISHED_PHASE_NEXT_##s] = n,
1921     foreach_sctp_state_next
1922 #undef _
1923   },
1924   .format_trace = format_sctp_rx_trace_short,
1925 };
1926 /* *INDENT-ON* */
1927
1928 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_established_phase_node,
1929                               sctp4_established_phase);
1930
1931 /* *INDENT-OFF* */
1932 VLIB_REGISTER_NODE (sctp6_established_phase_node) =
1933 {
1934   .function = sctp6_established_phase,
1935   .name = "sctp6-established",
1936   /* Takes a vector of packets. */
1937   .vector_size = sizeof (u32),
1938   .n_errors = SCTP_N_ERROR,
1939   .error_strings = sctp_error_strings,
1940   .n_next_nodes = SCTP_LISTEN_PHASE_N_NEXT,
1941   .next_nodes =
1942   {
1943 #define _(s,n) [SCTP_LISTEN_PHASE_NEXT_##s] = n,
1944     foreach_sctp_state_next
1945 #undef _
1946   },
1947   .format_trace = format_sctp_rx_trace_short,
1948 };
1949 /* *INDENT-ON* */
1950
1951 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_established_phase_node,
1952                               sctp6_established_phase);
1953
1954 /*
1955  * This is the function executed first for the SCTP graph.
1956  * It takes care of doing the initial message parsing and
1957  * dispatch to the specialized function.
1958  */
1959 always_inline uword
1960 sctp46_input_dispatcher (vlib_main_t * vm, vlib_node_runtime_t * node,
1961                          vlib_frame_t * from_frame, int is_ip4)
1962 {
1963   u32 n_left_from, next_index, *from, *to_next;
1964   u32 my_thread_index = vm->thread_index;
1965   u8 is_filtered;
1966   sctp_main_t *tm = vnet_get_sctp_main ();
1967
1968   from = vlib_frame_vector_args (from_frame);
1969   n_left_from = from_frame->n_vectors;
1970   next_index = node->cached_next_index;
1971   sctp_set_time_now (my_thread_index);
1972
1973   while (n_left_from > 0)
1974     {
1975       u32 n_left_to_next;
1976
1977       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1978
1979       while (n_left_from > 0 && n_left_to_next > 0)
1980         {
1981           int n_advance_bytes0, n_data_bytes0;
1982           u32 bi0, fib_index0;
1983           vlib_buffer_t *b0;
1984           sctp_header_t *sctp_hdr = 0;
1985           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
1986           sctp_connection_t *sctp_conn;
1987           transport_connection_t *trans_conn;
1988           ip4_header_t *ip4_hdr;
1989           ip6_header_t *ip6_hdr;
1990           u32 error0 = SCTP_ERROR_NO_LISTENER, next0 = SCTP_INPUT_NEXT_DROP;
1991
1992           bi0 = from[0];
1993           to_next[0] = bi0;
1994           from += 1;
1995           to_next += 1;
1996           n_left_from -= 1;
1997           n_left_to_next -= 1;
1998
1999           b0 = vlib_get_buffer (vm, bi0);
2000           vnet_buffer (b0)->tcp.flags = 0;
2001           fib_index0 = vnet_buffer (b0)->ip.fib_index;
2002
2003           /* Checksum computed by ipx_local no need to compute again */
2004
2005           if (is_ip4)
2006             {
2007               ip4_hdr = vlib_buffer_get_current (b0);
2008               sctp_hdr = ip4_next_header (ip4_hdr);
2009
2010               sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
2011               sctp_chunk_hdr = &full_hdr->common_hdr;
2012
2013               n_advance_bytes0 =
2014                 (ip4_header_bytes (ip4_hdr) +
2015                  sizeof (sctp_payload_data_chunk_t));
2016               n_data_bytes0 =
2017                 clib_net_to_host_u16 (ip4_hdr->length) - n_advance_bytes0;
2018
2019               trans_conn = session_lookup_connection_wt4 (fib_index0,
2020                                                           &ip4_hdr->dst_address,
2021                                                           &ip4_hdr->src_address,
2022                                                           sctp_hdr->dst_port,
2023                                                           sctp_hdr->src_port,
2024                                                           TRANSPORT_PROTO_SCTP,
2025                                                           my_thread_index,
2026                                                           &is_filtered);
2027             }
2028           else
2029             {
2030               ip6_hdr = vlib_buffer_get_current (b0);
2031               sctp_hdr = ip6_next_header (ip6_hdr);
2032
2033               sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
2034               sctp_chunk_hdr = &full_hdr->common_hdr;
2035
2036               n_advance_bytes0 = sctp_header_bytes ();
2037               n_data_bytes0 =
2038                 clib_net_to_host_u16 (ip6_hdr->payload_length) -
2039                 n_advance_bytes0;
2040               n_advance_bytes0 += sizeof (ip6_hdr[0]);
2041
2042               trans_conn = session_lookup_connection_wt6 (fib_index0,
2043                                                           &ip6_hdr->dst_address,
2044                                                           &ip6_hdr->src_address,
2045                                                           sctp_hdr->dst_port,
2046                                                           sctp_hdr->src_port,
2047                                                           TRANSPORT_PROTO_SCTP,
2048                                                           my_thread_index,
2049                                                           &is_filtered);
2050             }
2051
2052           /* Length check */
2053           if (PREDICT_FALSE (n_advance_bytes0 < 0))
2054             {
2055               error0 = SCTP_ERROR_LENGTH;
2056               goto done;
2057             }
2058
2059           sctp_conn = sctp_get_connection_from_transport (trans_conn);
2060           vnet_sctp_common_hdr_params_net_to_host (sctp_chunk_hdr);
2061
2062           u8 chunk_type = vnet_sctp_get_chunk_type (sctp_chunk_hdr);
2063           if (chunk_type >= UNKNOWN)
2064             {
2065               clib_warning
2066                 ("Received an unrecognized chunk... something is really bad.");
2067               error0 = SCTP_ERROR_UNKOWN_CHUNK;
2068               next0 = SCTP_INPUT_NEXT_DROP;
2069               goto done;
2070             }
2071
2072           vnet_buffer (b0)->sctp.hdr_offset =
2073             (u8 *) sctp_hdr - (u8 *) vlib_buffer_get_current (b0);
2074
2075           /* Session exists */
2076           if (PREDICT_TRUE (0 != sctp_conn))
2077             {
2078               /* Save connection index */
2079               vnet_buffer (b0)->sctp.connection_index = trans_conn->c_index;
2080               vnet_buffer (b0)->sctp.data_offset = n_advance_bytes0;
2081               vnet_buffer (b0)->sctp.data_len = n_data_bytes0;
2082
2083               next0 = tm->dispatch_table[sctp_conn->state][chunk_type].next;
2084               error0 = tm->dispatch_table[sctp_conn->state][chunk_type].error;
2085
2086               SCTP_DBG_STATE_MACHINE
2087                 ("SESSION_INDEX = %u, CURRENT_CONNECTION_STATE = %s,"
2088                  "CHUNK_TYPE_RECEIVED = %s " "NEXT_PHASE = %s",
2089                  sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
2090                  connection.s_index, sctp_state_to_string (sctp_conn->state),
2091                  sctp_chunk_to_string (chunk_type), phase_to_string (next0));
2092
2093               if (chunk_type == DATA)
2094                 SCTP_ADV_DBG ("n_advance_bytes0 = %u, n_data_bytes0 = %u",
2095                               n_advance_bytes0, n_data_bytes0);
2096
2097             }
2098           else
2099             {
2100               if (is_filtered)
2101                 {
2102                   next0 = SCTP_INPUT_NEXT_DROP;
2103                   error0 = SCTP_ERROR_FILTERED;
2104                 }
2105               else if ((is_ip4 && tm->punt_unknown4) ||
2106                        (!is_ip4 && tm->punt_unknown6))
2107                 {
2108                   next0 = SCTP_INPUT_NEXT_PUNT_PHASE;
2109                   error0 = SCTP_ERROR_PUNT;
2110                 }
2111               else
2112                 {
2113                   next0 = SCTP_INPUT_NEXT_DROP;
2114                   error0 = SCTP_ERROR_NO_LISTENER;
2115                 }
2116               SCTP_DBG_STATE_MACHINE ("sctp_conn == NULL, NEXT_PHASE = %s",
2117                                       phase_to_string (next0));
2118               sctp_conn = 0;
2119             }
2120
2121         done:
2122           b0->error = error0 ? node->errors[error0] : 0;
2123
2124           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2125             {
2126               sctp_rx_trace_t *t0 =
2127                 vlib_add_trace (vm, node, b0, sizeof (*t0));
2128               sctp_set_rx_trace_data (t0, sctp_conn, sctp_hdr, b0, is_ip4);
2129             }
2130           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2131                                            n_left_to_next, bi0, next0);
2132         }
2133
2134       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2135     }
2136   return from_frame->n_vectors;
2137 }
2138
2139 static uword
2140 sctp4_input_dispatcher (vlib_main_t * vm, vlib_node_runtime_t * node,
2141                         vlib_frame_t * from_frame)
2142 {
2143   return sctp46_input_dispatcher (vm, node, from_frame, 1 /* is_ip4 */ );
2144 }
2145
2146 static uword
2147 sctp6_input_dispatcher (vlib_main_t * vm, vlib_node_runtime_t * node,
2148                         vlib_frame_t * from_frame)
2149 {
2150   return sctp46_input_dispatcher (vm, node, from_frame, 0 /* is_ip4 */ );
2151 }
2152
2153 /* *INDENT-OFF* */
2154 VLIB_REGISTER_NODE (sctp4_input_node) =
2155 {
2156   .function = sctp4_input_dispatcher,
2157   .name = "sctp4-input",
2158   /* Takes a vector of packets. */
2159   .vector_size = sizeof (u32),
2160   .n_errors = SCTP_N_ERROR,
2161   .error_strings = sctp_error_strings,
2162   .n_next_nodes = SCTP_INPUT_N_NEXT,
2163   .next_nodes =
2164   {
2165 #define _(s,n) [SCTP_INPUT_NEXT_##s] = n,
2166     foreach_sctp4_input_next
2167 #undef _
2168   },
2169   .format_buffer = format_sctp_header,
2170   .format_trace = format_sctp_rx_trace,
2171 };
2172 /* *INDENT-ON* */
2173
2174 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_input_node, sctp4_input_dispatcher);
2175
2176 /* *INDENT-OFF* */
2177 VLIB_REGISTER_NODE (sctp6_input_node) =
2178 {
2179   .function = sctp6_input_dispatcher,
2180   .name = "sctp6-input",
2181   /* Takes a vector of packets. */
2182   .vector_size = sizeof (u32),
2183   .n_errors = SCTP_N_ERROR,
2184   .error_strings = sctp_error_strings,
2185   .n_next_nodes = SCTP_INPUT_N_NEXT,
2186   .next_nodes =
2187   {
2188 #define _(s,n) [SCTP_INPUT_NEXT_##s] = n,
2189     foreach_sctp6_input_next
2190 #undef _
2191   },
2192   .format_buffer = format_sctp_header,
2193   .format_trace = format_sctp_rx_trace,
2194 };
2195 /* *INDENT-ON* */
2196
2197 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_input_node, sctp6_input_dispatcher);
2198
2199 vlib_node_registration_t sctp4_input_node;
2200 vlib_node_registration_t sctp6_input_node;
2201
2202 static void
2203 sctp_dispatch_table_init (sctp_main_t * tm)
2204 {
2205   int i, j;
2206   for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2207     for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2208       {
2209         tm->dispatch_table[i][j].next = SCTP_INPUT_NEXT_DROP;
2210         tm->dispatch_table[i][j].error = SCTP_ERROR_DISPATCH;
2211       }
2212
2213 #define _(t,f,n,e)                                              \
2214 do {                                                            \
2215     tm->dispatch_table[SCTP_STATE_##t][f].next = (n);           \
2216     tm->dispatch_table[SCTP_STATE_##t][f].error = (e);          \
2217 } while (0)
2218
2219   /*
2220    * SCTP STATE-MACHINE states:
2221    *
2222    * _(CLOSED, "CLOSED")                         \
2223    * _(COOKIE_WAIT, "COOKIE_WAIT")               \
2224    * _(COOKIE_ECHOED, "COOKIE_ECHOED")           \
2225    * _(ESTABLISHED, "ESTABLISHED")               \
2226    * _(SHUTDOWN_PENDING, "SHUTDOWN_PENDING")     \
2227    * _(SHUTDOWN_SENT, "SHUTDOWN_SENT")           \
2228    * _(SHUTDOWN_RECEIVED, "SHUTDOWN_RECEIVED")   \
2229    * _(SHUTDOWN_ACK_SENT, "SHUTDOWN_ACK_SENT")
2230    */
2231   //_(CLOSED, DATA, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);     /* UNEXPECTED DATA chunk which requires special handling */
2232   _(CLOSED, INIT, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);
2233   _(CLOSED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);        /* UNEXPECTED INIT_ACK chunk */
2234   _(CLOSED, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);       /* UNEXPECTED SACK chunk */
2235   _(CLOSED, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);     /* UNEXPECTED HEARTBEAT chunk */
2236   _(CLOSED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);     /* UNEXPECTED HEARTBEAT_ACK chunk */
2237   _(CLOSED, ABORT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);
2238   _(CLOSED, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);       /* UNEXPECTED SHUTDOWN chunk */
2239   _(CLOSED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);       /* UNEXPECTED SHUTDOWN_ACK chunk */
2240   _(CLOSED, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);       /* UNEXPECTED OPERATION_ERROR chunk */
2241   _(CLOSED, COOKIE_ECHO, SCTP_INPUT_NEXT_ESTABLISHED_PHASE, SCTP_ERROR_NONE);
2242   _(CLOSED, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);      /* UNEXPECTED COOKIE_ACK chunk */
2243   _(CLOSED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);     /* UNEXPECTED ECNE chunk */
2244   _(CLOSED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);       /* UNEXPECTED CWR chunk */
2245   _(CLOSED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);   /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2246
2247   _(COOKIE_WAIT, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_NONE);
2248   _(COOKIE_WAIT, INIT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);     /* UNEXPECTED INIT chunk which requires special handling */
2249   _(COOKIE_WAIT, INIT_ACK, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);
2250   _(COOKIE_WAIT, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);  /* UNEXPECTED SACK chunk */
2251   _(COOKIE_WAIT, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);        /* UNEXPECTED HEARTBEAT chunk */
2252   _(COOKIE_WAIT, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);        /* UNEXPECTED HEARTBEAT_ACK chunk */
2253   _(COOKIE_WAIT, ABORT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);
2254   _(COOKIE_WAIT, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);  /* UNEXPECTED SHUTDOWN chunk */
2255   _(COOKIE_WAIT, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);  /* UNEXPECTED SHUTDOWN_ACK chunk */
2256   _(COOKIE_WAIT, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);  /* UNEXPECTED OPERATION_ERROR chunk */
2257   _(COOKIE_WAIT, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);  /* UNEXPECTED COOKIE_ECHO chunk */
2258   _(COOKIE_WAIT, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED COOKIE_ACK chunk */
2259   _(COOKIE_WAIT, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);        /* UNEXPECTED ECNE chunk */
2260   _(COOKIE_WAIT, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);  /* UNEXPECTED CWR chunk */
2261   _(COOKIE_WAIT, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);      /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2262
2263   _(COOKIE_ECHOED, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_NONE);
2264   _(COOKIE_ECHOED, INIT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);   /* UNEXPECTED INIT chunk which requires special handling */
2265   _(COOKIE_ECHOED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED INIT_ACK chunk */
2266   _(COOKIE_ECHOED, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);        /* UNEXPECTED SACK chunk */
2267   _(COOKIE_ECHOED, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT chunk */
2268   _(COOKIE_ECHOED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT_ACK chunk */
2269   _(COOKIE_ECHOED, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);      /* UNEXPECTED ABORT chunk */
2270   _(COOKIE_ECHOED, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);        /* UNEXPECTED SHUTDOWN chunk */
2271   _(COOKIE_ECHOED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);        /* UNEXPECTED SHUTDOWN_ACK chunk */
2272   _(COOKIE_ECHOED, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);        /* UNEXPECTED OPERATION_ERROR chunk */
2273   _(COOKIE_ECHOED, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);        /* UNEXPECTED COOKIE_ECHO chunk */
2274   _(COOKIE_ECHOED, COOKIE_ACK, SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
2275     SCTP_ERROR_NONE);
2276   _(COOKIE_ECHOED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);      /* UNEXPECTED ECNE chunk */
2277   _(COOKIE_ECHOED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);        /* UNEXPECTED CWR chunk */
2278   _(COOKIE_ECHOED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);    /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2279
2280   _(ESTABLISHED, DATA, SCTP_INPUT_NEXT_ESTABLISHED_PHASE, SCTP_ERROR_NONE);
2281   _(ESTABLISHED, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);  /* UNEXPECTED INIT chunk */
2282   _(ESTABLISHED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);   /* UNEXPECTED INIT_ACK chunk */
2283   _(ESTABLISHED, SACK, SCTP_INPUT_NEXT_ESTABLISHED_PHASE, SCTP_ERROR_NONE);
2284   _(ESTABLISHED, HEARTBEAT, SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
2285     SCTP_ERROR_NONE);
2286   _(ESTABLISHED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
2287     SCTP_ERROR_NONE);
2288   _(ESTABLISHED, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);        /* UNEXPECTED ABORT chunk */
2289   _(ESTABLISHED, SHUTDOWN, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2290   _(ESTABLISHED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);  /* UNEXPECTED SHUTDOWN_ACK chunk */
2291   _(ESTABLISHED, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);  /* UNEXPECTED OPERATION_ERROR chunk */
2292   _(ESTABLISHED, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);  /* UNEXPECTED COOKIE_ECHO chunk */
2293   _(ESTABLISHED, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED COOKIE_ACK chunk */
2294   _(ESTABLISHED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);        /* UNEXPECTED ECNE chunk */
2295   _(ESTABLISHED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);  /* UNEXPECTED CWR chunk */
2296   _(ESTABLISHED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);      /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2297
2298   _(SHUTDOWN_PENDING, DATA, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2299   _(SHUTDOWN_PENDING, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);     /* UNEXPECTED INIT chunk */
2300   _(SHUTDOWN_PENDING, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);      /* UNEXPECTED INIT_ACK chunk */
2301   _(SHUTDOWN_PENDING, SACK, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);
2302   _(SHUTDOWN_PENDING, HEARTBEAT, SCTP_INPUT_NEXT_LISTEN_PHASE,
2303     SCTP_ERROR_NONE);
2304   _(SHUTDOWN_PENDING, HEARTBEAT_ACK, SCTP_INPUT_NEXT_LISTEN_PHASE,
2305     SCTP_ERROR_NONE);
2306   _(SHUTDOWN_PENDING, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);   /* UNEXPECTED ABORT chunk */
2307   _(SHUTDOWN_PENDING, SHUTDOWN, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2308     SCTP_ERROR_NONE);
2309   _(SHUTDOWN_PENDING, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);     /* UNEXPECTED SHUTDOWN_ACK chunk */
2310   _(SHUTDOWN_PENDING, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);     /* UNEXPECTED OPERATION_ERROR chunk */
2311   _(SHUTDOWN_PENDING, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);     /* UNEXPECTED COOKIE_ECHO chunk */
2312   _(SHUTDOWN_PENDING, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);    /* UNEXPECTED COOKIE_ACK chunk */
2313   _(SHUTDOWN_PENDING, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);   /* UNEXPECTED ECNE chunk */
2314   _(SHUTDOWN_PENDING, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);     /* UNEXPECTED CWR chunk */
2315   _(SHUTDOWN_PENDING, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION); /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2316
2317   _(SHUTDOWN_SENT, DATA, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2318   _(SHUTDOWN_SENT, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);        /* UNEXPECTED INIT chunk */
2319   _(SHUTDOWN_SENT, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED INIT_ACK chunk */
2320   _(SHUTDOWN_SENT, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);        /* UNEXPECTED SACK chunk */
2321   _(SHUTDOWN_SENT, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT chunk */
2322   _(SHUTDOWN_SENT, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT_ACK chunk */
2323   _(SHUTDOWN_SENT, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);      /* UNEXPECTED ABORT chunk */
2324   _(SHUTDOWN_SENT, SHUTDOWN, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2325   _(SHUTDOWN_SENT, SHUTDOWN_ACK, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2326     SCTP_ERROR_NONE);
2327   _(SHUTDOWN_SENT, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);        /* UNEXPECTED COOKIE_ECHO chunk */
2328   _(SHUTDOWN_SENT, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);       /* UNEXPECTED COOKIE_ACK chunk */
2329   _(SHUTDOWN_SENT, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);      /* UNEXPECTED ECNE chunk */
2330   _(SHUTDOWN_SENT, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);        /* UNEXPECTED CWR chunk */
2331   _(SHUTDOWN_SENT, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);    /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2332
2333   _(SHUTDOWN_RECEIVED, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_DATA_CHUNK_VIOLATION);    /* UNEXPECTED DATA chunk */
2334   _(SHUTDOWN_RECEIVED, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2335   _(SHUTDOWN_RECEIVED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);     /* UNEXPECTED INIT_ACK chunk */
2336   _(SHUTDOWN_RECEIVED, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2337   _(SHUTDOWN_RECEIVED, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT chunk */
2338   _(SHUTDOWN_RECEIVED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT_ACK chunk */
2339   _(SHUTDOWN_RECEIVED, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);  /* UNEXPECTED ABORT chunk */
2340   _(SHUTDOWN_RECEIVED, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);    /* UNEXPECTED SHUTDOWN chunk */
2341   _(SHUTDOWN_RECEIVED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2342     SCTP_ERROR_NONE);
2343   _(SHUTDOWN_RECEIVED, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);    /* UNEXPECTED COOKIE_ECHO chunk */
2344   _(SHUTDOWN_RECEIVED, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);   /* UNEXPECTED COOKIE_ACK chunk */
2345   _(SHUTDOWN_RECEIVED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);  /* UNEXPECTED ECNE chunk */
2346   _(SHUTDOWN_RECEIVED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);    /* UNEXPECTED CWR chunk */
2347   _(SHUTDOWN_RECEIVED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);        /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2348
2349   _(SHUTDOWN_ACK_SENT, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_DATA_CHUNK_VIOLATION);    /* UNEXPECTED DATA chunk */
2350   _(SHUTDOWN_ACK_SENT, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2351   _(SHUTDOWN_ACK_SENT, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);     /* UNEXPECTED INIT_ACK chunk */
2352   _(SHUTDOWN_ACK_SENT, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2353   _(SHUTDOWN_ACK_SENT, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT chunk */
2354   _(SHUTDOWN_ACK_SENT, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT_ACK chunk */
2355   _(SHUTDOWN_ACK_SENT, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);  /* UNEXPECTED ABORT chunk */
2356   _(SHUTDOWN_ACK_SENT, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);    /* UNEXPECTED SHUTDOWN chunk */
2357   _(SHUTDOWN_ACK_SENT, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);    /* UNEXPECTED SHUTDOWN_ACK chunk */
2358   _(SHUTDOWN_ACK_SENT, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);    /* UNEXPECTED COOKIE_ECHO chunk */
2359   _(SHUTDOWN_ACK_SENT, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);   /* UNEXPECTED COOKIE_ACK chunk */
2360   _(SHUTDOWN_ACK_SENT, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);  /* UNEXPECTED ECNE chunk */
2361   _(SHUTDOWN_ACK_SENT, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);    /* UNEXPECTED CWR chunk */
2362   _(SHUTDOWN_ACK_SENT, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2363     SCTP_ERROR_NONE);
2364
2365   /* TODO: Handle COOKIE ECHO when a TCB Exists */
2366
2367 #undef _
2368 }
2369
2370 clib_error_t *
2371 sctp_input_init (vlib_main_t * vm)
2372 {
2373   clib_error_t *error = 0;
2374   sctp_main_t *tm = vnet_get_sctp_main ();
2375
2376   if ((error = vlib_call_init_function (vm, sctp_init)))
2377     return error;
2378
2379   /* Initialize dispatch table. */
2380   sctp_dispatch_table_init (tm);
2381
2382   return error;
2383 }
2384
2385 VLIB_INIT_FUNCTION (sctp_input_init);
2386
2387 /*
2388  * fd.io coding-style-patch-verification: ON
2389  *
2390  * Local Variables:
2391  * eval: (c-set-style "gnu")
2392  * End:
2393  */