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