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