SCTP: retransmission in INIT/SHUTDOWN phase
[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                                                     MAIN_SCTP_SUB_CONN_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                                                       MAIN_SCTP_SUB_CONN_IDX,
349                                                       b0, ip4_addr, ip6_addr);
350           else
351             sctp_prepare_abort_for_collision (sctp_conn,
352                                               MAIN_SCTP_SUB_CONN_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                                              [MAIN_SCTP_SUB_CONN_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                                              [MAIN_SCTP_SUB_CONN_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, MAIN_SCTP_SUB_CONN_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                                              [MAIN_SCTP_SUB_CONN_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                                              [MAIN_SCTP_SUB_CONN_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   /* Section 4.4 of the RFC4960 */
715   if (sctp_conn->state == SCTP_STATE_SHUTDOWN_SENT)
716     {
717       SCTP_CONN_TRACKING_DBG ("sctp_conn->state = %s; SACK not delayable",
718                               sctp_state_to_string (sctp_conn->state));
719       return 0;
720     }
721
722   if (is_gapping != 0)
723     {
724       SCTP_CONN_TRACKING_DBG
725         ("gapping != 0: CONN_INDEX = %u, sctp_conn->ack_state = %u",
726          sctp_conn->sub_conn[idx].connection.c_index, sctp_conn->ack_state);
727       return 0;
728     }
729
730   if (sctp_conn->ack_state >= MAX_ENQUEABLE_SACKS)
731     {
732       SCTP_CONN_TRACKING_DBG
733         ("sctp_conn->ack_state >= MAX_ENQUEABLE_SACKS: CONN_INDEX = %u, sctp_conn->ack_state = %u",
734          sctp_conn->sub_conn[idx].connection.c_index, sctp_conn->ack_state);
735       return 0;
736     }
737
738   sctp_conn->ack_state += 1;
739
740   return 1;
741 }
742
743 always_inline void
744 sctp_is_connection_gapping (sctp_connection_t * sctp_conn, u32 tsn,
745                             u8 * gapping)
746 {
747   if (sctp_conn->next_tsn_expected != tsn)      // It means data transmission is GAPPING
748     {
749       SCTP_CONN_TRACKING_DBG
750         ("GAPPING: CONN_INDEX = %u, sctp_conn->next_tsn_expected = %u, tsn = %u, diff = %u",
751          sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.c_index,
752          sctp_conn->next_tsn_expected, tsn,
753          sctp_conn->next_tsn_expected - tsn);
754
755       *gapping = 1;
756     }
757 }
758
759 always_inline u16
760 sctp_handle_data (sctp_payload_data_chunk_t * sctp_data_chunk,
761                   sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b,
762                   u16 * next0)
763 {
764   u32 error = 0, n_data_bytes;
765   u8 is_gapping = 0;
766
767   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
768   if (sctp_conn->local_tag != sctp_data_chunk->sctp_hdr.verification_tag)
769     {
770       return SCTP_ERROR_INVALID_TAG;
771     }
772
773   vnet_buffer (b)->sctp.sid = sctp_data_chunk->stream_id;
774   vnet_buffer (b)->sctp.ssn = sctp_data_chunk->stream_seq;
775
776   u32 tsn = clib_net_to_host_u32 (sctp_data_chunk->tsn);
777
778   vlib_buffer_advance (b, vnet_buffer (b)->sctp.data_offset);
779   n_data_bytes = vnet_buffer (b)->sctp.data_len;
780   ASSERT (n_data_bytes);
781
782   sctp_is_connection_gapping (sctp_conn, tsn, &is_gapping);
783
784   sctp_conn->last_rcvd_tsn = tsn;
785
786   SCTP_ADV_DBG ("POINTER_WITH_DATA = %p", b->data);
787
788   u8 bbit = vnet_sctp_get_bbit (&sctp_data_chunk->chunk_hdr);
789   u8 ebit = vnet_sctp_get_ebit (&sctp_data_chunk->chunk_hdr);
790
791   if (bbit == 1 && ebit == 1)   /* Unfragmented message */
792     {
793       /* In order data, enqueue. Fifo figures out by itself if any out-of-order
794        * segments can be enqueued after fifo tail offset changes. */
795       if (PREDICT_FALSE (is_gapping == 1))
796         error =
797           sctp_session_enqueue_data_ooo (sctp_conn, b, n_data_bytes, idx);
798       else
799         error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
800     }
801   else if (bbit == 1 && ebit == 0)      /* First piece of a fragmented user message */
802     {
803       error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
804     }
805   else if (bbit == 0 && ebit == 1)      /* Last piece of a fragmented user message */
806     {
807       if (PREDICT_FALSE (is_gapping == 1))
808         error =
809           sctp_session_enqueue_data_ooo (sctp_conn, b, n_data_bytes, idx);
810       else
811         error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
812     }
813   else                          /* Middle piece of a fragmented user message */
814     {
815       if (PREDICT_FALSE (is_gapping == 1))
816         error =
817           sctp_session_enqueue_data_ooo (sctp_conn, b, n_data_bytes, idx);
818       else
819         error = sctp_session_enqueue_data (sctp_conn, b, n_data_bytes, idx);
820     }
821   sctp_conn->last_rcvd_tsn = tsn;
822
823   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
824
825   SCTP_ADV_DBG ("POINTER_WITH_DATA = %p", b->data);
826
827   if (!sctp_is_sack_delayable (sctp_conn, idx, is_gapping))
828     sctp_prepare_sack_chunk (sctp_conn, idx, b);
829
830   sctp_conn->sub_conn[idx].enqueue_state = error;
831
832   return error;
833 }
834
835 always_inline u16
836 sctp_handle_cookie_echo (sctp_header_t * sctp_hdr,
837                          sctp_chunks_common_hdr_t * sctp_chunk_hdr,
838                          sctp_connection_t * sctp_conn, u8 idx,
839                          vlib_buffer_t * b0, u16 * next0)
840 {
841   u32 now = sctp_time_now ();
842
843   sctp_cookie_echo_chunk_t *cookie_echo =
844     (sctp_cookie_echo_chunk_t *) sctp_hdr;
845
846   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
847   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
848     {
849       return SCTP_ERROR_INVALID_TAG;
850     }
851
852   sctp_calculate_rto (sctp_conn, idx);
853
854   u32 creation_time =
855     clib_net_to_host_u32 (cookie_echo->cookie.creation_time);
856   u32 cookie_lifespan =
857     clib_net_to_host_u32 (cookie_echo->cookie.cookie_lifespan);
858   if (now > creation_time + cookie_lifespan)
859     {
860       SCTP_DBG ("now (%u) > creation_time (%u) + cookie_lifespan (%u)",
861                 now, creation_time, cookie_lifespan);
862       return SCTP_ERROR_COOKIE_ECHO_VIOLATION;
863     }
864
865   sctp_prepare_cookie_ack_chunk (sctp_conn, idx, b0);
866
867   /* Change state */
868   sctp_conn->state = SCTP_STATE_ESTABLISHED;
869   sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_STATE_UP;
870   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].c_is_ip4);
871
872   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
873                   sctp_conn->sub_conn[idx].RTO);
874
875   stream_session_accept_notify (&sctp_conn->sub_conn[idx].connection);
876
877   return SCTP_ERROR_NONE;
878
879 }
880
881 always_inline u16
882 sctp_handle_cookie_ack (sctp_header_t * sctp_hdr,
883                         sctp_chunks_common_hdr_t * sctp_chunk_hdr,
884                         sctp_connection_t * sctp_conn, u8 idx,
885                         vlib_buffer_t * b0, u16 * next0)
886 {
887   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
888   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
889     {
890       return SCTP_ERROR_INVALID_TAG;
891     }
892
893   sctp_calculate_rto (sctp_conn, idx);
894
895   sctp_timer_reset (sctp_conn, idx, SCTP_TIMER_T1_COOKIE);
896   /* Change state */
897   sctp_conn->state = SCTP_STATE_ESTABLISHED;
898   sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_STATE_UP;
899
900   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
901
902   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
903                   sctp_conn->sub_conn[idx].RTO);
904
905   stream_session_accept_notify (&sctp_conn->sub_conn[idx].connection);
906
907   return SCTP_ERROR_NONE;
908
909 }
910
911 always_inline uword
912 sctp46_rcv_phase_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
913                          vlib_frame_t * from_frame, int is_ip4)
914 {
915   sctp_main_t *tm = vnet_get_sctp_main ();
916
917   u32 n_left_from, next_index, *from, *to_next;
918   u32 my_thread_index = vm->thread_index;
919
920   from = vlib_frame_vector_args (from_frame);
921   n_left_from = from_frame->n_vectors;
922
923   next_index = node->cached_next_index;
924
925   while (n_left_from > 0)
926     {
927       u32 n_left_to_next;
928
929       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
930
931       while (n_left_from > 0 && n_left_to_next > 0)
932         {
933           u32 bi0;
934           vlib_buffer_t *b0;
935           sctp_header_t *sctp_hdr = 0;
936           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
937           ip4_header_t *ip4_hdr = 0;
938           ip6_header_t *ip6_hdr = 0;
939           sctp_connection_t *sctp_conn, *new_sctp_conn;
940           u16 sctp_implied_length = 0;
941           u16 error0 = SCTP_ERROR_NONE, next0 = sctp_next_drop (is_ip4);
942           u8 idx;
943
944           bi0 = from[0];
945           to_next[0] = bi0;
946           from += 1;
947           to_next += 1;
948           n_left_from -= 1;
949           n_left_to_next -= 1;
950
951           b0 = vlib_get_buffer (vm, bi0);
952
953           /* If we are in SCTP_COOKIE_WAIT_STATE then the connection
954            * will come from the half-open connections pool.
955            */
956           sctp_conn =
957             sctp_half_open_connection_get (vnet_buffer (b0)->
958                                            sctp.connection_index);
959
960           if (PREDICT_FALSE (sctp_conn == 0))
961             {
962               SCTP_ADV_DBG
963                 ("sctp_conn == NULL; return SCTP_ERROR_INVALID_CONNECTION");
964               error0 = SCTP_ERROR_INVALID_CONNECTION;
965               goto drop;
966             }
967           if (is_ip4)
968             {
969               ip4_hdr = vlib_buffer_get_current (b0);
970               sctp_hdr = ip4_next_header (ip4_hdr);
971               idx = sctp_sub_conn_id_via_ip4h (sctp_conn, ip4_hdr);
972             }
973           else
974             {
975               ip6_hdr = vlib_buffer_get_current (b0);
976               sctp_hdr = ip6_next_header (ip6_hdr);
977               idx = sctp_sub_conn_id_via_ip6h (sctp_conn, ip6_hdr);
978             }
979
980           sctp_conn->sub_conn[idx].subconn_idx = idx;
981           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
982
983           sctp_chunk_hdr =
984             (sctp_chunks_common_hdr_t *) (&full_hdr->common_hdr);
985
986           sctp_implied_length =
987             sctp_calculate_implied_length (ip4_hdr, ip6_hdr, is_ip4);
988
989           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
990
991           switch (chunk_type)
992             {
993             case INIT_ACK:
994               error0 =
995                 sctp_is_valid_init_ack (sctp_hdr, sctp_chunk_hdr, sctp_conn,
996                                         b0, sctp_implied_length);
997
998               if (error0 == SCTP_ERROR_NONE)
999                 {
1000                   pool_get (tm->connections[my_thread_index], new_sctp_conn);
1001                   clib_memcpy (new_sctp_conn, sctp_conn,
1002                                sizeof (*new_sctp_conn));
1003                   new_sctp_conn->sub_conn[idx].c_c_index =
1004                     new_sctp_conn - tm->connections[my_thread_index];
1005                   new_sctp_conn->sub_conn[idx].c_thread_index =
1006                     my_thread_index;
1007                   new_sctp_conn->sub_conn[idx].PMTU =
1008                     sctp_conn->sub_conn[idx].PMTU;
1009                   new_sctp_conn->sub_conn[idx].subconn_idx = idx;
1010
1011                   if (sctp_half_open_connection_cleanup (sctp_conn))
1012                     {
1013                       SCTP_DBG
1014                         ("Cannot cleanup half-open connection; not the owning thread");
1015                     }
1016
1017                   sctp_connection_timers_init (new_sctp_conn);
1018
1019                   error0 =
1020                     sctp_handle_init_ack (sctp_hdr, sctp_chunk_hdr,
1021                                           new_sctp_conn, idx, b0,
1022                                           sctp_implied_length);
1023
1024                   sctp_init_cwnd (new_sctp_conn);
1025
1026                   if (session_stream_connect_notify
1027                       (&new_sctp_conn->sub_conn[idx].connection, 0))
1028                     {
1029                       SCTP_DBG
1030                         ("conn_index = %u: session_stream_connect_notify error; cleaning up connection",
1031                          new_sctp_conn->sub_conn[idx].connection.c_index);
1032                       sctp_connection_cleanup (new_sctp_conn);
1033                       goto drop;
1034                     }
1035                   next0 = sctp_next_output (is_ip4);
1036                 }
1037               break;
1038
1039             case OPERATION_ERROR:
1040               error0 =
1041                 sctp_handle_operation_err (sctp_hdr, sctp_conn, idx, b0,
1042                                            &next0);
1043               break;
1044
1045               /* All UNEXPECTED scenarios (wrong chunk received per state-machine)
1046                * are handled by the input-dispatcher function using the table-lookup
1047                * hence we should never get to the "default" case below.
1048                */
1049             default:
1050               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1051               next0 = sctp_next_drop (is_ip4);
1052               goto drop;
1053             }
1054
1055           if (error0 != SCTP_ERROR_NONE)
1056             {
1057               clib_warning ("error while parsing chunk");
1058               sctp_connection_cleanup (sctp_conn);
1059               next0 = sctp_next_drop (is_ip4);
1060               goto drop;
1061             }
1062
1063         drop:
1064           b0->error = node->errors[error0];
1065           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1066             {
1067               sctp_rx_trace_t *t0 =
1068                 vlib_add_trace (vm, node, b0, sizeof (*t0));
1069               sctp_set_rx_trace_data (t0, sctp_conn, sctp_hdr, b0, is_ip4);
1070             }
1071
1072           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1073                                            n_left_to_next, bi0, next0);
1074         }
1075
1076       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1077     }
1078   return from_frame->n_vectors;
1079 }
1080
1081 static uword
1082 sctp4_rcv_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1083                  vlib_frame_t * from_frame)
1084 {
1085   return sctp46_rcv_phase_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1086 }
1087
1088 static uword
1089 sctp6_rcv_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1090                  vlib_frame_t * from_frame)
1091 {
1092   return sctp46_rcv_phase_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1093 }
1094
1095 u8 *
1096 format_sctp_rx_trace_short (u8 * s, va_list * args)
1097 {
1098   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1099   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1100   sctp_rx_trace_t *t = va_arg (*args, sctp_rx_trace_t *);
1101
1102   s = format (s, "%d -> %d (%U)",
1103               clib_net_to_host_u16 (t->sctp_header.src_port),
1104               clib_net_to_host_u16 (t->sctp_header.dst_port),
1105               format_sctp_state, t->sctp_connection.state);
1106
1107   return s;
1108 }
1109
1110 /* *INDENT-OFF* */
1111 VLIB_REGISTER_NODE (sctp4_rcv_phase_node) =
1112 {
1113   .function = sctp4_rcv_phase,
1114   .name = "sctp4-rcv",
1115   /* Takes a vector of packets. */
1116   .vector_size = sizeof (u32),
1117   .n_errors = SCTP_N_ERROR,
1118   .error_strings = sctp_error_strings,
1119   .n_next_nodes = SCTP_RCV_PHASE_N_NEXT,
1120   .next_nodes =
1121   {
1122 #define _(s,n) [SCTP_RCV_PHASE_NEXT_##s] = n,
1123     foreach_sctp_state_next
1124 #undef _
1125   },
1126   .format_trace = format_sctp_rx_trace_short,
1127 };
1128 /* *INDENT-ON* */
1129
1130 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_rcv_phase_node, sctp4_rcv_phase);
1131
1132 /* *INDENT-OFF* */
1133 VLIB_REGISTER_NODE (sctp6_init_phase_node) =
1134 {
1135   .function = sctp6_rcv_phase,
1136   .name = "sctp6-rcv",
1137   /* Takes a vector of packets. */
1138   .vector_size = sizeof (u32),
1139   .n_errors = SCTP_N_ERROR,
1140   .error_strings = sctp_error_strings,
1141   .n_next_nodes = SCTP_RCV_PHASE_N_NEXT,
1142   .next_nodes =
1143   {
1144 #define _(s,n) [SCTP_RCV_PHASE_NEXT_##s] = n,
1145     foreach_sctp_state_next
1146 #undef _
1147   },
1148   .format_trace = format_sctp_rx_trace_short,
1149 };
1150 /* *INDENT-ON* */
1151
1152 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_init_phase_node, sctp6_rcv_phase);
1153
1154 vlib_node_registration_t sctp4_shutdown_phase_node;
1155 vlib_node_registration_t sctp6_shutdown_phase_node;
1156
1157 always_inline u16
1158 sctp_handle_shutdown (sctp_header_t * sctp_hdr,
1159                       sctp_chunks_common_hdr_t * sctp_chunk_hdr,
1160                       sctp_connection_t * sctp_conn, u8 idx,
1161                       vlib_buffer_t * b0, u16 sctp_implied_length,
1162                       u16 * next0)
1163 {
1164   sctp_shutdown_association_chunk_t *shutdown_chunk =
1165     (sctp_shutdown_association_chunk_t *) (sctp_hdr);
1166
1167   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1168   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
1169     {
1170       return SCTP_ERROR_INVALID_TAG;
1171     }
1172
1173   /*
1174    * It is not possible to bundle any other CHUNK with the SHUTDOWN chunk
1175    */
1176   if (sctp_is_bundling (sctp_implied_length, &shutdown_chunk->chunk_hdr))
1177     return SCTP_ERROR_BUNDLING_VIOLATION;
1178
1179   switch (sctp_conn->state)
1180     {
1181     case SCTP_STATE_ESTABLISHED:
1182       if (sctp_check_outstanding_data_chunks (sctp_conn) == 0)
1183         sctp_conn->state = SCTP_STATE_SHUTDOWN_RECEIVED;
1184       sctp_send_shutdown_ack (sctp_conn, idx, b0);
1185       break;
1186
1187     case SCTP_STATE_SHUTDOWN_SENT:
1188       sctp_send_shutdown_ack (sctp_conn, idx, b0);
1189       break;
1190     }
1191
1192   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].c_is_ip4);
1193
1194   return SCTP_ERROR_NONE;
1195 }
1196
1197 always_inline u16
1198 sctp_handle_shutdown_ack (sctp_header_t * sctp_hdr,
1199                           sctp_chunks_common_hdr_t * sctp_chunk_hdr,
1200                           sctp_connection_t * sctp_conn, u8 idx,
1201                           vlib_buffer_t * b0, u16 sctp_implied_length,
1202                           u16 * next0)
1203 {
1204   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
1205     (sctp_shutdown_ack_chunk_t *) (sctp_hdr);
1206
1207   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1208   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
1209     {
1210       return SCTP_ERROR_INVALID_TAG;
1211     }
1212
1213   /*
1214    * It is not possible to bundle any other CHUNK with the SHUTDOWN chunk
1215    */
1216   if (sctp_is_bundling (sctp_implied_length, &shutdown_ack_chunk->chunk_hdr))
1217     return SCTP_ERROR_BUNDLING_VIOLATION;
1218
1219   /* Whether we are in SCTP_STATE_SHUTDOWN_SENT or SCTP_STATE_SHUTDOWN_ACK_SENT
1220    * the reception of a SHUTDOWN_ACK chunk leads to the same actions:
1221    * - STOP T2_SHUTDOWN timer
1222    * - SEND SHUTDOWN_COMPLETE chunk
1223    */
1224   sctp_timer_reset (sctp_conn, MAIN_SCTP_SUB_CONN_IDX,
1225                     SCTP_TIMER_T2_SHUTDOWN);
1226
1227   sctp_send_shutdown_complete (sctp_conn, idx, b0);
1228
1229   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].c_is_ip4);
1230
1231   return SCTP_ERROR_NONE;
1232 }
1233
1234 always_inline u16
1235 sctp_handle_shutdown_complete (sctp_header_t * sctp_hdr,
1236                                sctp_chunks_common_hdr_t * sctp_chunk_hdr,
1237                                sctp_connection_t * sctp_conn, u8 idx,
1238                                vlib_buffer_t * b0, u16 sctp_implied_length,
1239                                u16 * next0)
1240 {
1241   sctp_shutdown_complete_chunk_t *shutdown_complete =
1242     (sctp_shutdown_complete_chunk_t *) (sctp_hdr);
1243
1244   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1245   if (sctp_conn->local_tag != sctp_hdr->verification_tag)
1246     {
1247       return SCTP_ERROR_INVALID_TAG;
1248     }
1249
1250   /*
1251    * It is not possible to bundle any other CHUNK with the SHUTDOWN chunk
1252    */
1253   if (sctp_is_bundling (sctp_implied_length, &shutdown_complete->chunk_hdr))
1254     return SCTP_ERROR_BUNDLING_VIOLATION;
1255
1256   sctp_timer_reset (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN);
1257
1258   stream_session_disconnect_notify (&sctp_conn->sub_conn[idx].connection);
1259
1260   sctp_conn->state = SCTP_STATE_CLOSED;
1261
1262   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
1263
1264   return SCTP_ERROR_NONE;
1265 }
1266
1267 always_inline uword
1268 sctp46_shutdown_phase_inline (vlib_main_t * vm,
1269                               vlib_node_runtime_t * node,
1270                               vlib_frame_t * from_frame, int is_ip4)
1271 {
1272   u32 n_left_from, next_index, *from, *to_next;
1273   u32 my_thread_index = vm->thread_index;
1274
1275   from = vlib_frame_vector_args (from_frame);
1276   n_left_from = from_frame->n_vectors;
1277
1278   next_index = node->cached_next_index;
1279
1280   while (n_left_from > 0)
1281     {
1282       u32 n_left_to_next;
1283
1284       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1285
1286       while (n_left_from > 0 && n_left_to_next > 0)
1287         {
1288           u32 bi0;
1289           vlib_buffer_t *b0;
1290           sctp_rx_trace_t *sctp_trace;
1291           sctp_header_t *sctp_hdr = 0;
1292           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
1293           ip4_header_t *ip4_hdr = 0;
1294           ip6_header_t *ip6_hdr = 0;
1295           sctp_connection_t *sctp_conn;
1296           u16 sctp_implied_length = 0;
1297           u16 error0 = SCTP_ERROR_NONE, next0 = SCTP_RCV_PHASE_N_NEXT;
1298           u8 idx = 0;
1299
1300           bi0 = from[0];
1301           to_next[0] = bi0;
1302           from += 1;
1303           to_next += 1;
1304           n_left_from -= 1;
1305           n_left_to_next -= 1;
1306
1307           b0 = vlib_get_buffer (vm, bi0);
1308           sctp_conn =
1309             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1310                                  my_thread_index);
1311
1312           if (PREDICT_FALSE (sctp_conn == 0))
1313             {
1314               SCTP_DBG
1315                 ("sctp_conn == NULL; return SCTP_ERROR_INVALID_CONNECTION");
1316               error0 = SCTP_ERROR_INVALID_CONNECTION;
1317               goto drop;
1318             }
1319
1320           if (is_ip4)
1321             {
1322               ip4_hdr = vlib_buffer_get_current (b0);
1323               sctp_hdr = ip4_next_header (ip4_hdr);
1324               idx = sctp_sub_conn_id_via_ip4h (sctp_conn, ip4_hdr);
1325             }
1326           else
1327             {
1328               ip6_hdr = vlib_buffer_get_current (b0);
1329               sctp_hdr = ip6_next_header (ip6_hdr);
1330               idx = sctp_sub_conn_id_via_ip6h (sctp_conn, ip6_hdr);
1331             }
1332
1333           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1334           sctp_chunk_hdr = &full_hdr->common_hdr;
1335
1336           sctp_implied_length =
1337             sctp_calculate_implied_length (ip4_hdr, ip6_hdr, is_ip4);
1338
1339           u8 chunk_type = vnet_sctp_get_chunk_type (sctp_chunk_hdr);
1340           switch (chunk_type)
1341             {
1342             case SHUTDOWN:
1343               error0 =
1344                 sctp_handle_shutdown (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1345                                       idx, b0, sctp_implied_length, &next0);
1346               break;
1347
1348             case SHUTDOWN_ACK:
1349               error0 =
1350                 sctp_handle_shutdown_ack (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1351                                           idx, b0, sctp_implied_length,
1352                                           &next0);
1353               break;
1354
1355             case SHUTDOWN_COMPLETE:
1356               error0 =
1357                 sctp_handle_shutdown_complete (sctp_hdr, sctp_chunk_hdr,
1358                                                sctp_conn, idx, b0,
1359                                                sctp_implied_length, &next0);
1360
1361               sctp_connection_cleanup (sctp_conn);
1362               break;
1363
1364               /*
1365                * DATA chunks can still be transmitted/received in the SHUTDOWN-PENDING
1366                * and SHUTDOWN-SENT states (as per RFC4960 Section 6)
1367                */
1368             case DATA:
1369               error0 =
1370                 sctp_handle_data ((sctp_payload_data_chunk_t *) sctp_hdr,
1371                                   sctp_conn, idx, b0, &next0);
1372               break;
1373
1374             case OPERATION_ERROR:
1375               error0 =
1376                 sctp_handle_operation_err (sctp_hdr, sctp_conn, idx, b0,
1377                                            &next0);
1378               break;
1379
1380             case COOKIE_ECHO:   /* Cookie Received While Shutting Down */
1381               sctp_prepare_operation_error (sctp_conn, idx, b0,
1382                                             COOKIE_RECEIVED_WHILE_SHUTTING_DOWN);
1383               error0 = SCTP_ERROR_NONE;
1384               next0 = sctp_next_output (is_ip4);
1385               break;
1386               /* All UNEXPECTED scenarios (wrong chunk received per state-machine)
1387                * are handled by the input-dispatcher function using the table-lookup
1388                * hence we should never get to the "default" case below.
1389                */
1390             default:
1391               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1392               next0 = sctp_next_drop (is_ip4);
1393               goto drop;
1394             }
1395
1396           if (error0 != SCTP_ERROR_NONE)
1397             {
1398               clib_warning ("error while parsing chunk");
1399               sctp_connection_cleanup (sctp_conn);
1400               next0 = sctp_next_drop (is_ip4);
1401               goto drop;
1402             }
1403
1404         drop:
1405           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1406             {
1407               sctp_trace =
1408                 vlib_add_trace (vm, node, b0, sizeof (*sctp_trace));
1409
1410               if (sctp_hdr != NULL)
1411                 clib_memcpy (&sctp_trace->sctp_header, sctp_hdr,
1412                              sizeof (sctp_trace->sctp_header));
1413
1414               if (sctp_conn != NULL)
1415                 clib_memcpy (&sctp_trace->sctp_connection, sctp_conn,
1416                              sizeof (sctp_trace->sctp_connection));
1417             }
1418
1419           b0->error = node->errors[error0];
1420
1421           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1422                                            n_left_to_next, bi0, next0);
1423         }
1424
1425       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1426     }
1427
1428   return from_frame->n_vectors;
1429
1430 }
1431
1432 static uword
1433 sctp4_shutdown_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1434                       vlib_frame_t * from_frame)
1435 {
1436   return sctp46_shutdown_phase_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1437 }
1438
1439 static uword
1440 sctp6_shutdown_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1441                       vlib_frame_t * from_frame)
1442 {
1443   return sctp46_shutdown_phase_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1444 }
1445
1446 /* *INDENT-OFF* */
1447 VLIB_REGISTER_NODE (sctp4_shutdown_phase_node) =
1448 {
1449   .function = sctp4_shutdown_phase,
1450   .name = "sctp4-shutdown",
1451   /* Takes a vector of packets. */
1452   .vector_size = sizeof (u32),
1453   .n_errors = SCTP_N_ERROR,
1454   .error_strings = sctp_error_strings,
1455   .n_next_nodes = SCTP_SHUTDOWN_PHASE_N_NEXT,
1456   .next_nodes =
1457   {
1458 #define _(s,n) [SCTP_SHUTDOWN_PHASE_NEXT_##s] = n,
1459     foreach_sctp_state_next
1460 #undef _
1461   },
1462   .format_trace = format_sctp_rx_trace_short,
1463 };
1464 /* *INDENT-ON* */
1465
1466 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_shutdown_phase_node,
1467                               sctp4_shutdown_phase);
1468
1469 /* *INDENT-OFF* */
1470 VLIB_REGISTER_NODE (sctp6_shutdown_phase_node) =
1471 {
1472   .function = sctp6_shutdown_phase,
1473   .name = "sctp6-shutdown",
1474   /* Takes a vector of packets. */
1475   .vector_size = sizeof (u32),
1476   .n_errors = SCTP_N_ERROR,
1477   .error_strings = sctp_error_strings,
1478   .n_next_nodes = SCTP_SHUTDOWN_PHASE_N_NEXT,
1479   .next_nodes =
1480   {
1481 #define _(s,n) [SCTP_SHUTDOWN_PHASE_NEXT_##s] = n,
1482     foreach_sctp_state_next
1483 #undef _
1484   },
1485   .format_trace = format_sctp_rx_trace_short,
1486 };
1487 /* *INDENT-ON* */
1488
1489 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_shutdown_phase_node,
1490                               sctp6_shutdown_phase);
1491
1492 vlib_node_registration_t sctp4_listen_phase_node;
1493 vlib_node_registration_t sctp6_listen_phase_node;
1494
1495 vlib_node_registration_t sctp4_established_phase_node;
1496 vlib_node_registration_t sctp6_established_phase_node;
1497
1498 always_inline u16
1499 sctp_handle_sack (sctp_selective_ack_chunk_t * sack_chunk,
1500                   sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b0,
1501                   u16 * next0)
1502 {
1503   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1504   if (sctp_conn->local_tag != sack_chunk->sctp_hdr.verification_tag)
1505     {
1506       return SCTP_ERROR_INVALID_TAG;
1507     }
1508
1509   sctp_conn->sub_conn[idx].last_seen = sctp_time_now ();
1510
1511   /* Section 7.2.2; point (2) */
1512   if (sctp_conn->sub_conn[idx].cwnd > sctp_conn->sub_conn[idx].ssthresh)
1513     sctp_conn->sub_conn[idx].partially_acked_bytes =
1514       sctp_conn->next_tsn - sack_chunk->cumulative_tsn_ack;
1515
1516   /* Section 7.2.2; point (5) */
1517   if (sctp_conn->next_tsn - sack_chunk->cumulative_tsn_ack == 0)
1518     sctp_conn->sub_conn[idx].partially_acked_bytes = 0;
1519
1520   sctp_conn->last_unacked_tsn = sack_chunk->cumulative_tsn_ack;
1521
1522   sctp_calculate_rto (sctp_conn, idx);
1523
1524   sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1525                      sctp_conn->sub_conn[idx].RTO);
1526
1527   sctp_conn->sub_conn[idx].RTO_pending = 0;
1528
1529   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
1530
1531   return SCTP_ERROR_NONE;
1532 }
1533
1534 always_inline u16
1535 sctp_handle_heartbeat (sctp_hb_req_chunk_t * sctp_hb_chunk,
1536                        sctp_connection_t * sctp_conn, u8 idx,
1537                        vlib_buffer_t * b0, u16 * next0)
1538 {
1539   /* Check that the LOCALLY generated tag is being used by the REMOTE peer as the verification tag */
1540   if (sctp_conn->local_tag != sctp_hb_chunk->sctp_hdr.verification_tag)
1541     {
1542       return SCTP_ERROR_INVALID_TAG;
1543     }
1544
1545   sctp_prepare_heartbeat_ack_chunk (sctp_conn, idx, b0);
1546
1547   *next0 = sctp_next_output (sctp_conn->sub_conn[idx].connection.is_ip4);
1548
1549   return SCTP_ERROR_NONE;
1550 }
1551
1552 always_inline u16
1553 sctp_handle_heartbeat_ack (sctp_hb_ack_chunk_t * sctp_hb_ack_chunk,
1554                            sctp_connection_t * sctp_conn, u8 idx,
1555                            vlib_buffer_t * b0, u16 * next0)
1556 {
1557   sctp_conn->sub_conn[idx].last_seen = sctp_time_now ();
1558
1559   sctp_conn->sub_conn[idx].unacknowledged_hb -= 1;
1560
1561   sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T4_HEARTBEAT,
1562                      sctp_conn->sub_conn[idx].RTO);
1563
1564   *next0 = sctp_next_drop (sctp_conn->sub_conn[idx].c_is_ip4);
1565
1566   return SCTP_ERROR_NONE;
1567 }
1568
1569 always_inline void
1570 sctp_node_inc_counter (vlib_main_t * vm, u32 sctp4_node, u32 sctp6_node,
1571                        u8 is_ip4, u8 evt, u8 val)
1572 {
1573   if (PREDICT_TRUE (!val))
1574     return;
1575
1576   if (is_ip4)
1577     vlib_node_increment_counter (vm, sctp4_node, evt, val);
1578   else
1579     vlib_node_increment_counter (vm, sctp6_node, evt, val);
1580 }
1581
1582 always_inline uword
1583 sctp46_listen_process_inline (vlib_main_t * vm,
1584                               vlib_node_runtime_t * node,
1585                               vlib_frame_t * from_frame, int is_ip4)
1586 {
1587   u32 n_left_from, next_index, *from, *to_next;
1588   u32 my_thread_index = vm->thread_index;
1589
1590   from = vlib_frame_vector_args (from_frame);
1591   n_left_from = from_frame->n_vectors;
1592
1593   next_index = node->cached_next_index;
1594
1595   while (n_left_from > 0)
1596     {
1597       u32 n_left_to_next;
1598
1599       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1600
1601       while (n_left_from > 0 && n_left_to_next > 0)
1602         {
1603           u32 bi0;
1604           vlib_buffer_t *b0;
1605           sctp_header_t *sctp_hdr = 0;
1606           ip4_header_t *ip4_hdr;
1607           ip6_header_t *ip6_hdr;
1608           sctp_connection_t *child_conn;
1609           sctp_connection_t *sctp_listener;
1610           u16 next0 = sctp_next_drop (is_ip4), error0 = SCTP_ERROR_ENQUEUED;
1611
1612           bi0 = from[0];
1613           to_next[0] = bi0;
1614           from += 1;
1615           to_next += 1;
1616           n_left_from -= 1;
1617           n_left_to_next -= 1;
1618
1619           b0 = vlib_get_buffer (vm, bi0);
1620           sctp_listener =
1621             sctp_listener_get (vnet_buffer (b0)->sctp.connection_index);
1622
1623           if (is_ip4)
1624             {
1625               ip4_hdr = vlib_buffer_get_current (b0);
1626               sctp_hdr = ip4_next_header (ip4_hdr);
1627             }
1628           else
1629             {
1630               ip6_hdr = vlib_buffer_get_current (b0);
1631               sctp_hdr = ip6_next_header (ip6_hdr);
1632             }
1633
1634           child_conn =
1635             sctp_lookup_connection (sctp_listener->sub_conn
1636                                     [MAIN_SCTP_SUB_CONN_IDX].c_fib_index, b0,
1637                                     my_thread_index, is_ip4);
1638
1639           if (PREDICT_FALSE (child_conn->state != SCTP_STATE_CLOSED))
1640             {
1641               SCTP_DBG
1642                 ("conn_index = %u: child_conn->state != SCTP_STATE_CLOSED.... STATE=%s",
1643                  child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
1644                  connection.c_index,
1645                  sctp_state_to_string (child_conn->state));
1646               error0 = SCTP_ERROR_CREATE_EXISTS;
1647               goto drop;
1648             }
1649
1650           /* Create child session and send SYN-ACK */
1651           child_conn = sctp_connection_new (my_thread_index);
1652           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].subconn_idx =
1653             MAIN_SCTP_SUB_CONN_IDX;
1654           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_lcl_port =
1655             sctp_hdr->dst_port;
1656           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_rmt_port =
1657             sctp_hdr->src_port;
1658           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_is_ip4 = is_ip4;
1659           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.proto =
1660             sctp_listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection.proto;
1661           child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].PMTU =
1662             sctp_listener->sub_conn[MAIN_SCTP_SUB_CONN_IDX].PMTU;
1663           child_conn->state = SCTP_STATE_CLOSED;
1664
1665           if (is_ip4)
1666             {
1667               child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_lcl_ip4.as_u32 =
1668                 ip4_hdr->dst_address.as_u32;
1669               child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_rmt_ip4.as_u32 =
1670                 ip4_hdr->src_address.as_u32;
1671             }
1672           else
1673             {
1674               clib_memcpy (&child_conn->
1675                            sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_lcl_ip6,
1676                            &ip6_hdr->dst_address, sizeof (ip6_address_t));
1677               clib_memcpy (&child_conn->
1678                            sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_rmt_ip6,
1679                            &ip6_hdr->src_address, sizeof (ip6_address_t));
1680             }
1681
1682           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1683           sctp_chunks_common_hdr_t *sctp_chunk_hdr = &full_hdr->common_hdr;
1684
1685           u8 chunk_type = vnet_sctp_get_chunk_type (sctp_chunk_hdr);
1686           if (chunk_type != INIT)
1687             {
1688               SCTP_DBG
1689                 ("conn_index = %u: chunk_type != INIT... chunk_type=%s",
1690                  child_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
1691                  connection.c_index, sctp_chunk_to_string (chunk_type));
1692
1693               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1694               next0 = sctp_next_drop (is_ip4);
1695               goto drop;
1696             }
1697
1698           u16 sctp_implied_length =
1699             sctp_calculate_implied_length (ip4_hdr, ip6_hdr, is_ip4);
1700
1701           switch (chunk_type)
1702             {
1703             case INIT:
1704               sctp_connection_timers_init (child_conn);
1705
1706               sctp_init_snd_vars (child_conn);
1707
1708               error0 =
1709                 sctp_handle_init (sctp_hdr, sctp_chunk_hdr, child_conn, b0,
1710                                   sctp_implied_length);
1711
1712               sctp_init_cwnd (child_conn);
1713
1714               if (error0 == SCTP_ERROR_NONE)
1715                 {
1716                   if (stream_session_accept
1717                       (&child_conn->
1718                        sub_conn[MAIN_SCTP_SUB_CONN_IDX].connection,
1719                        sctp_listener->
1720                        sub_conn[MAIN_SCTP_SUB_CONN_IDX].c_s_index, 0))
1721                     {
1722                       clib_warning ("session accept fail");
1723                       sctp_connection_cleanup (child_conn);
1724                       error0 = SCTP_ERROR_CREATE_SESSION_FAIL;
1725                       goto drop;
1726                     }
1727                   next0 = sctp_next_output (is_ip4);
1728                 }
1729               break;
1730
1731               /* Reception of a DATA chunk whilst in the CLOSED state is called
1732                * "Out of the Blue" packet and handling of the chunk needs special treatment
1733                * as per RFC4960 section 8.4
1734                */
1735             case DATA:
1736               break;
1737
1738             case OPERATION_ERROR:
1739               error0 =
1740                 sctp_handle_operation_err (sctp_hdr, child_conn,
1741                                            MAIN_SCTP_SUB_CONN_IDX, b0,
1742                                            &next0);
1743               break;
1744             }
1745
1746         drop:
1747           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1748             {
1749               sctp_rx_trace_t *t0 =
1750                 vlib_add_trace (vm, node, b0, sizeof (*t0));
1751               clib_memcpy (&t0->sctp_header, sctp_hdr,
1752                            sizeof (t0->sctp_header));
1753               clib_memcpy (&t0->sctp_connection, sctp_listener,
1754                            sizeof (t0->sctp_connection));
1755             }
1756
1757           b0->error = node->errors[error0];
1758
1759           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1760                                            n_left_to_next, bi0, next0);
1761         }
1762       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1763
1764     }
1765   return from_frame->n_vectors;
1766 }
1767
1768 static uword
1769 sctp4_listen_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1770                     vlib_frame_t * from_frame)
1771 {
1772   return sctp46_listen_process_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1773 }
1774
1775 static uword
1776 sctp6_listen_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1777                     vlib_frame_t * from_frame)
1778 {
1779   return sctp46_listen_process_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1780 }
1781
1782 always_inline uword
1783 sctp46_established_phase_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1784                                  vlib_frame_t * from_frame, int is_ip4)
1785 {
1786   u32 n_left_from, next_index, *from, *to_next;
1787   u32 my_thread_index = vm->thread_index, errors = 0;
1788
1789   from = vlib_frame_vector_args (from_frame);
1790   n_left_from = from_frame->n_vectors;
1791
1792   next_index = node->cached_next_index;
1793
1794   while (n_left_from > 0)
1795     {
1796       u32 n_left_to_next;
1797
1798       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1799
1800       while (n_left_from > 0 && n_left_to_next > 0)
1801         {
1802           u32 bi0;
1803           vlib_buffer_t *b0;
1804           sctp_header_t *sctp_hdr = 0;
1805           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
1806           ip4_header_t *ip4_hdr = 0;
1807           ip6_header_t *ip6_hdr = 0;
1808           sctp_connection_t *sctp_conn;
1809           u16 error0 = SCTP_ERROR_ENQUEUED, next0 =
1810             SCTP_ESTABLISHED_PHASE_N_NEXT;
1811           u8 idx;
1812
1813           bi0 = from[0];
1814           to_next[0] = bi0;
1815           from += 1;
1816           to_next += 1;
1817           n_left_from -= 1;
1818           n_left_to_next -= 1;
1819
1820           b0 = vlib_get_buffer (vm, bi0);
1821           sctp_conn =
1822             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1823                                  my_thread_index);
1824
1825           if (PREDICT_FALSE (sctp_conn == 0))
1826             {
1827               SCTP_DBG
1828                 ("sctp_conn == NULL; return SCTP_ERROR_INVALID_CONNECTION");
1829               error0 = SCTP_ERROR_INVALID_CONNECTION;
1830               goto done;
1831             }
1832           if (is_ip4)
1833             {
1834               ip4_hdr = vlib_buffer_get_current (b0);
1835               sctp_hdr = ip4_next_header (ip4_hdr);
1836               idx = sctp_sub_conn_id_via_ip4h (sctp_conn, ip4_hdr);
1837             }
1838           else
1839             {
1840               ip6_hdr = vlib_buffer_get_current (b0);
1841               sctp_hdr = ip6_next_header (ip6_hdr);
1842               idx = sctp_sub_conn_id_via_ip6h (sctp_conn, ip6_hdr);
1843             }
1844
1845           sctp_conn->sub_conn[idx].subconn_idx = idx;
1846
1847           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1848           sctp_chunk_hdr =
1849             (sctp_chunks_common_hdr_t *) (&full_hdr->common_hdr);
1850
1851           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1852
1853           switch (chunk_type)
1854             {
1855             case COOKIE_ECHO:
1856               error0 =
1857                 sctp_handle_cookie_echo (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1858                                          idx, b0, &next0);
1859               break;
1860
1861             case COOKIE_ACK:
1862               error0 =
1863                 sctp_handle_cookie_ack (sctp_hdr, sctp_chunk_hdr, sctp_conn,
1864                                         idx, b0, &next0);
1865               break;
1866
1867             case SACK:
1868               error0 =
1869                 sctp_handle_sack ((sctp_selective_ack_chunk_t *) sctp_hdr,
1870                                   sctp_conn, idx, b0, &next0);
1871               break;
1872
1873             case HEARTBEAT:
1874               error0 =
1875                 sctp_handle_heartbeat ((sctp_hb_req_chunk_t *) sctp_hdr,
1876                                        sctp_conn, idx, b0, &next0);
1877               break;
1878
1879             case HEARTBEAT_ACK:
1880               error0 =
1881                 sctp_handle_heartbeat_ack ((sctp_hb_ack_chunk_t *) sctp_hdr,
1882                                            sctp_conn, idx, b0, &next0);
1883               break;
1884
1885             case DATA:
1886               error0 =
1887                 sctp_handle_data ((sctp_payload_data_chunk_t *) sctp_hdr,
1888                                   sctp_conn, idx, b0, &next0);
1889               break;
1890
1891             case OPERATION_ERROR:
1892               error0 =
1893                 sctp_handle_operation_err (sctp_hdr, sctp_conn, idx, b0,
1894                                            &next0);
1895               break;
1896
1897               /* All UNEXPECTED scenarios (wrong chunk received per state-machine)
1898                * are handled by the input-dispatcher function using the table-lookup
1899                * hence we should never get to the "default" case below.
1900                */
1901             default:
1902               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1903               next0 = sctp_next_drop (is_ip4);
1904               goto done;
1905             }
1906
1907         done:
1908           b0->error = node->errors[error0];
1909           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1910             {
1911               sctp_rx_trace_t *t0 =
1912                 vlib_add_trace (vm, node, b0, sizeof (*t0));
1913               sctp_set_rx_trace_data (t0, sctp_conn, sctp_hdr, b0, is_ip4);
1914             }
1915
1916           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1917                                            n_left_to_next, bi0, next0);
1918         }
1919
1920       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1921     }
1922
1923   errors = session_manager_flush_enqueue_events (TRANSPORT_PROTO_SCTP,
1924                                                  my_thread_index);
1925
1926   sctp_node_inc_counter (vm, is_ip4, sctp4_established_phase_node.index,
1927                          sctp6_established_phase_node.index,
1928                          SCTP_ERROR_EVENT_FIFO_FULL, errors);
1929   sctp_flush_frame_to_output (vm, my_thread_index, is_ip4);
1930
1931   return from_frame->n_vectors;
1932 }
1933
1934 static uword
1935 sctp4_established_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1936                          vlib_frame_t * from_frame)
1937 {
1938   return sctp46_established_phase_inline (vm, node, from_frame,
1939                                           1 /* is_ip4 */ );
1940 }
1941
1942 static uword
1943 sctp6_established_phase (vlib_main_t * vm, vlib_node_runtime_t * node,
1944                          vlib_frame_t * from_frame)
1945 {
1946   return sctp46_established_phase_inline (vm, node, from_frame,
1947                                           0 /* is_ip4 */ );
1948 }
1949
1950 u8 *
1951 format_sctp_rx_trace (u8 * s, va_list * args)
1952 {
1953   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1954   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1955   sctp_rx_trace_t *t = va_arg (*args, sctp_rx_trace_t *);
1956   u32 indent = format_get_indent (s);
1957
1958   s = format (s, "%U\n%U%U",
1959               format_sctp_header, &t->sctp_header, 128,
1960               format_white_space, indent,
1961               format_sctp_connection, &t->sctp_connection, 1);
1962
1963   return s;
1964 }
1965
1966 /* *INDENT-OFF* */
1967 VLIB_REGISTER_NODE (sctp4_listen_phase_node) =
1968 {
1969   .function = sctp4_listen_phase,
1970   .name = "sctp4-listen",
1971   /* Takes a vector of packets. */
1972   .vector_size = sizeof (u32),
1973   .n_errors = SCTP_N_ERROR,
1974   .error_strings = sctp_error_strings,
1975   .n_next_nodes = SCTP_LISTEN_PHASE_N_NEXT,
1976   .next_nodes =
1977   {
1978 #define _(s,n) [SCTP_LISTEN_PHASE_NEXT_##s] = n,
1979     foreach_sctp_state_next
1980 #undef _
1981   },
1982   .format_trace = format_sctp_rx_trace_short,
1983 };
1984 /* *INDENT-ON* */
1985
1986 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_listen_phase_node, sctp4_listen_phase);
1987
1988 /* *INDENT-OFF* */
1989 VLIB_REGISTER_NODE (sctp6_listen_phase_node) =
1990 {
1991   .function = sctp6_listen_phase,
1992   .name = "sctp6-listen",
1993   /* Takes a vector of packets. */
1994   .vector_size = sizeof (u32),
1995   .n_errors = SCTP_N_ERROR,
1996   .error_strings = sctp_error_strings,
1997   .n_next_nodes = SCTP_LISTEN_PHASE_N_NEXT,
1998   .next_nodes =
1999   {
2000 #define _(s,n) [SCTP_LISTEN_PHASE_NEXT_##s] = n,
2001     foreach_sctp_state_next
2002 #undef _
2003   },
2004   .format_trace = format_sctp_rx_trace_short,
2005 };
2006 /* *INDENT-ON* */
2007
2008 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_listen_phase_node, sctp6_listen_phase);
2009
2010 /* *INDENT-OFF* */
2011 VLIB_REGISTER_NODE (sctp4_established_phase_node) =
2012 {
2013   .function = sctp4_established_phase,
2014   .name = "sctp4-established",
2015   /* Takes a vector of packets. */
2016   .vector_size = sizeof (u32),
2017   .n_errors = SCTP_N_ERROR,
2018   .error_strings = sctp_error_strings,
2019   .n_next_nodes = SCTP_ESTABLISHED_PHASE_N_NEXT,
2020   .next_nodes =
2021   {
2022 #define _(s,n) [SCTP_ESTABLISHED_PHASE_NEXT_##s] = n,
2023     foreach_sctp_state_next
2024 #undef _
2025   },
2026   .format_trace = format_sctp_rx_trace_short,
2027 };
2028 /* *INDENT-ON* */
2029
2030 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_established_phase_node,
2031                               sctp4_established_phase);
2032
2033 /* *INDENT-OFF* */
2034 VLIB_REGISTER_NODE (sctp6_established_phase_node) =
2035 {
2036   .function = sctp6_established_phase,
2037   .name = "sctp6-established",
2038   /* Takes a vector of packets. */
2039   .vector_size = sizeof (u32),
2040   .n_errors = SCTP_N_ERROR,
2041   .error_strings = sctp_error_strings,
2042   .n_next_nodes = SCTP_LISTEN_PHASE_N_NEXT,
2043   .next_nodes =
2044   {
2045 #define _(s,n) [SCTP_LISTEN_PHASE_NEXT_##s] = n,
2046     foreach_sctp_state_next
2047 #undef _
2048   },
2049   .format_trace = format_sctp_rx_trace_short,
2050 };
2051 /* *INDENT-ON* */
2052
2053 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_established_phase_node,
2054                               sctp6_established_phase);
2055
2056 /*
2057  * This is the function executed first for the SCTP graph.
2058  * It takes care of doing the initial message parsing and
2059  * dispatch to the specialized function.
2060  */
2061 always_inline uword
2062 sctp46_input_dispatcher (vlib_main_t * vm, vlib_node_runtime_t * node,
2063                          vlib_frame_t * from_frame, int is_ip4)
2064 {
2065   u32 n_left_from, next_index, *from, *to_next;
2066   u32 my_thread_index = vm->thread_index;
2067   u8 is_filtered;
2068   sctp_main_t *tm = vnet_get_sctp_main ();
2069
2070   from = vlib_frame_vector_args (from_frame);
2071   n_left_from = from_frame->n_vectors;
2072   next_index = node->cached_next_index;
2073   sctp_set_time_now (my_thread_index);
2074
2075   while (n_left_from > 0)
2076     {
2077       u32 n_left_to_next;
2078
2079       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2080
2081       while (n_left_from > 0 && n_left_to_next > 0)
2082         {
2083           int n_advance_bytes0, n_data_bytes0;
2084           u32 bi0, fib_index0;
2085           vlib_buffer_t *b0;
2086           sctp_header_t *sctp_hdr = 0;
2087           sctp_chunks_common_hdr_t *sctp_chunk_hdr = 0;
2088           sctp_connection_t *sctp_conn;
2089           transport_connection_t *trans_conn;
2090           ip4_header_t *ip4_hdr;
2091           ip6_header_t *ip6_hdr;
2092           u32 error0 = SCTP_ERROR_NO_LISTENER, next0 = SCTP_INPUT_NEXT_DROP;
2093
2094           bi0 = from[0];
2095           to_next[0] = bi0;
2096           from += 1;
2097           to_next += 1;
2098           n_left_from -= 1;
2099           n_left_to_next -= 1;
2100
2101           b0 = vlib_get_buffer (vm, bi0);
2102           vnet_buffer (b0)->sctp.flags = 0;
2103           fib_index0 = vnet_buffer (b0)->ip.fib_index;
2104
2105           /* Checksum computed by ipx_local no need to compute again */
2106
2107           if (is_ip4)
2108             {
2109               ip4_hdr = vlib_buffer_get_current (b0);
2110               sctp_hdr = ip4_next_header (ip4_hdr);
2111
2112               sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
2113               sctp_chunk_hdr = &full_hdr->common_hdr;
2114
2115               n_advance_bytes0 =
2116                 (ip4_header_bytes (ip4_hdr) +
2117                  sizeof (sctp_payload_data_chunk_t));
2118               n_data_bytes0 =
2119                 clib_net_to_host_u16 (ip4_hdr->length) - n_advance_bytes0;
2120
2121               trans_conn = session_lookup_connection_wt4 (fib_index0,
2122                                                           &ip4_hdr->dst_address,
2123                                                           &ip4_hdr->src_address,
2124                                                           sctp_hdr->dst_port,
2125                                                           sctp_hdr->src_port,
2126                                                           TRANSPORT_PROTO_SCTP,
2127                                                           my_thread_index,
2128                                                           &is_filtered);
2129             }
2130           else
2131             {
2132               ip6_hdr = vlib_buffer_get_current (b0);
2133               sctp_hdr = ip6_next_header (ip6_hdr);
2134
2135               sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
2136               sctp_chunk_hdr = &full_hdr->common_hdr;
2137
2138               n_advance_bytes0 = sctp_header_bytes ();
2139               n_data_bytes0 =
2140                 clib_net_to_host_u16 (ip6_hdr->payload_length) -
2141                 n_advance_bytes0;
2142               n_advance_bytes0 += sizeof (ip6_hdr[0]);
2143
2144               trans_conn = session_lookup_connection_wt6 (fib_index0,
2145                                                           &ip6_hdr->dst_address,
2146                                                           &ip6_hdr->src_address,
2147                                                           sctp_hdr->dst_port,
2148                                                           sctp_hdr->src_port,
2149                                                           TRANSPORT_PROTO_SCTP,
2150                                                           my_thread_index,
2151                                                           &is_filtered);
2152             }
2153
2154           /* Length check */
2155           if (PREDICT_FALSE (n_advance_bytes0 < 0))
2156             {
2157               error0 = SCTP_ERROR_LENGTH;
2158               goto done;
2159             }
2160
2161           sctp_conn = sctp_get_connection_from_transport (trans_conn);
2162           vnet_sctp_common_hdr_params_net_to_host (sctp_chunk_hdr);
2163
2164           u8 chunk_type = vnet_sctp_get_chunk_type (sctp_chunk_hdr);
2165           if (chunk_type >= UNKNOWN)
2166             {
2167               clib_warning
2168                 ("Received an unrecognized chunk; sending back OPERATION_ERROR chunk");
2169
2170               sctp_prepare_operation_error (sctp_conn, MAIN_SCTP_SUB_CONN_IDX,
2171                                             b0, UNRECOGNIZED_CHUNK_TYPE);
2172
2173               error0 = SCTP_ERROR_UNKOWN_CHUNK;
2174               next0 = sctp_next_output (is_ip4);
2175               goto done;
2176             }
2177
2178           vnet_buffer (b0)->sctp.hdr_offset =
2179             (u8 *) sctp_hdr - (u8 *) vlib_buffer_get_current (b0);
2180
2181           /* Session exists */
2182           if (PREDICT_TRUE (0 != sctp_conn))
2183             {
2184               /* Save connection index */
2185               vnet_buffer (b0)->sctp.connection_index = trans_conn->c_index;
2186               vnet_buffer (b0)->sctp.data_offset = n_advance_bytes0;
2187               vnet_buffer (b0)->sctp.data_len = n_data_bytes0;
2188
2189               next0 = tm->dispatch_table[sctp_conn->state][chunk_type].next;
2190               error0 = tm->dispatch_table[sctp_conn->state][chunk_type].error;
2191
2192               SCTP_DBG_STATE_MACHINE
2193                 ("S_INDEX = %u, C_INDEX = %u, TRANS_CONN = %p, SCTP_CONN = %p, CURRENT_CONNECTION_STATE = %s,"
2194                  "CHUNK_TYPE_RECEIVED = %s " "NEXT_PHASE = %s",
2195                  sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
2196                  connection.s_index,
2197                  sctp_conn->sub_conn[MAIN_SCTP_SUB_CONN_IDX].
2198                  connection.c_index, trans_conn, sctp_conn,
2199                  sctp_state_to_string (sctp_conn->state),
2200                  sctp_chunk_to_string (chunk_type), phase_to_string (next0));
2201
2202               if (chunk_type == DATA)
2203                 SCTP_ADV_DBG ("n_advance_bytes0 = %u, n_data_bytes0 = %u",
2204                               n_advance_bytes0, n_data_bytes0);
2205
2206             }
2207           else
2208             {
2209               if (is_filtered)
2210                 {
2211                   next0 = SCTP_INPUT_NEXT_DROP;
2212                   error0 = SCTP_ERROR_FILTERED;
2213                 }
2214               else if ((is_ip4 && tm->punt_unknown4) ||
2215                        (!is_ip4 && tm->punt_unknown6))
2216                 {
2217                   next0 = SCTP_INPUT_NEXT_PUNT_PHASE;
2218                   error0 = SCTP_ERROR_PUNT;
2219                 }
2220               else
2221                 {
2222                   next0 = SCTP_INPUT_NEXT_DROP;
2223                   error0 = SCTP_ERROR_NO_LISTENER;
2224                 }
2225               SCTP_DBG_STATE_MACHINE ("sctp_conn == NULL, NEXT_PHASE = %s",
2226                                       phase_to_string (next0));
2227               sctp_conn = 0;
2228             }
2229
2230         done:
2231           b0->error = error0 ? node->errors[error0] : 0;
2232
2233           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2234             {
2235               sctp_rx_trace_t *t0 =
2236                 vlib_add_trace (vm, node, b0, sizeof (*t0));
2237               sctp_set_rx_trace_data (t0, sctp_conn, sctp_hdr, b0, is_ip4);
2238             }
2239           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
2240                                            n_left_to_next, bi0, next0);
2241         }
2242
2243       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2244     }
2245   return from_frame->n_vectors;
2246 }
2247
2248 static uword
2249 sctp4_input_dispatcher (vlib_main_t * vm, vlib_node_runtime_t * node,
2250                         vlib_frame_t * from_frame)
2251 {
2252   return sctp46_input_dispatcher (vm, node, from_frame, 1 /* is_ip4 */ );
2253 }
2254
2255 static uword
2256 sctp6_input_dispatcher (vlib_main_t * vm, vlib_node_runtime_t * node,
2257                         vlib_frame_t * from_frame)
2258 {
2259   return sctp46_input_dispatcher (vm, node, from_frame, 0 /* is_ip4 */ );
2260 }
2261
2262 /* *INDENT-OFF* */
2263 VLIB_REGISTER_NODE (sctp4_input_node) =
2264 {
2265   .function = sctp4_input_dispatcher,
2266   .name = "sctp4-input",
2267   /* Takes a vector of packets. */
2268   .vector_size = sizeof (u32),
2269   .n_errors = SCTP_N_ERROR,
2270   .error_strings = sctp_error_strings,
2271   .n_next_nodes = SCTP_INPUT_N_NEXT,
2272   .next_nodes =
2273   {
2274 #define _(s,n) [SCTP_INPUT_NEXT_##s] = n,
2275     foreach_sctp4_input_next
2276 #undef _
2277   },
2278   .format_buffer = format_sctp_header,
2279   .format_trace = format_sctp_rx_trace,
2280 };
2281 /* *INDENT-ON* */
2282
2283 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_input_node, sctp4_input_dispatcher);
2284
2285 /* *INDENT-OFF* */
2286 VLIB_REGISTER_NODE (sctp6_input_node) =
2287 {
2288   .function = sctp6_input_dispatcher,
2289   .name = "sctp6-input",
2290   /* Takes a vector of packets. */
2291   .vector_size = sizeof (u32),
2292   .n_errors = SCTP_N_ERROR,
2293   .error_strings = sctp_error_strings,
2294   .n_next_nodes = SCTP_INPUT_N_NEXT,
2295   .next_nodes =
2296   {
2297 #define _(s,n) [SCTP_INPUT_NEXT_##s] = n,
2298     foreach_sctp6_input_next
2299 #undef _
2300   },
2301   .format_buffer = format_sctp_header,
2302   .format_trace = format_sctp_rx_trace,
2303 };
2304 /* *INDENT-ON* */
2305
2306 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_input_node, sctp6_input_dispatcher);
2307
2308 vlib_node_registration_t sctp4_input_node;
2309 vlib_node_registration_t sctp6_input_node;
2310
2311 static void
2312 sctp_dispatch_table_init (sctp_main_t * tm)
2313 {
2314   int i, j;
2315   for (i = 0; i < ARRAY_LEN (tm->dispatch_table); i++)
2316     for (j = 0; j < ARRAY_LEN (tm->dispatch_table[i]); j++)
2317       {
2318         tm->dispatch_table[i][j].next = SCTP_INPUT_NEXT_DROP;
2319         tm->dispatch_table[i][j].error = SCTP_ERROR_DISPATCH;
2320       }
2321
2322 #define _(t,f,n,e)                                              \
2323 do {                                                            \
2324     tm->dispatch_table[SCTP_STATE_##t][f].next = (n);           \
2325     tm->dispatch_table[SCTP_STATE_##t][f].error = (e);          \
2326 } while (0)
2327
2328   /*
2329    * SCTP STATE-MACHINE states:
2330    *
2331    * _(CLOSED, "CLOSED")                         \
2332    * _(COOKIE_WAIT, "COOKIE_WAIT")               \
2333    * _(COOKIE_ECHOED, "COOKIE_ECHOED")           \
2334    * _(ESTABLISHED, "ESTABLISHED")               \
2335    * _(SHUTDOWN_PENDING, "SHUTDOWN_PENDING")     \
2336    * _(SHUTDOWN_SENT, "SHUTDOWN_SENT")           \
2337    * _(SHUTDOWN_RECEIVED, "SHUTDOWN_RECEIVED")   \
2338    * _(SHUTDOWN_ACK_SENT, "SHUTDOWN_ACK_SENT")
2339    */
2340   //_(CLOSED, DATA, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);     /* UNEXPECTED DATA chunk which requires special handling */
2341   _(CLOSED, INIT, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);
2342   _(CLOSED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);        /* UNEXPECTED INIT_ACK chunk */
2343   _(CLOSED, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);       /* UNEXPECTED SACK chunk */
2344   _(CLOSED, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);     /* UNEXPECTED HEARTBEAT chunk */
2345   _(CLOSED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);     /* UNEXPECTED HEARTBEAT_ACK chunk */
2346   _(CLOSED, ABORT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);
2347   _(CLOSED, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);       /* UNEXPECTED SHUTDOWN chunk */
2348   _(CLOSED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);       /* UNEXPECTED SHUTDOWN_ACK chunk */
2349   _(CLOSED, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);       /* UNEXPECTED OPERATION_ERROR chunk */
2350   _(CLOSED, COOKIE_ECHO, SCTP_INPUT_NEXT_ESTABLISHED_PHASE, SCTP_ERROR_NONE);
2351   _(CLOSED, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);      /* UNEXPECTED COOKIE_ACK chunk */
2352   _(CLOSED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);     /* UNEXPECTED ECNE chunk */
2353   _(CLOSED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);       /* UNEXPECTED CWR chunk */
2354   _(CLOSED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);   /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2355   _(CLOSED, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);
2356
2357   _(COOKIE_WAIT, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_NONE);  /* UNEXPECTED DATA chunk which requires special handling */
2358   _(COOKIE_WAIT, INIT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);     /* UNEXPECTED INIT chunk which requires special handling */
2359   _(COOKIE_WAIT, INIT_ACK, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);
2360   _(COOKIE_WAIT, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);  /* UNEXPECTED SACK chunk */
2361   _(COOKIE_WAIT, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);        /* UNEXPECTED HEARTBEAT chunk */
2362   _(COOKIE_WAIT, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);        /* UNEXPECTED HEARTBEAT_ACK chunk */
2363   _(COOKIE_WAIT, ABORT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);
2364   _(COOKIE_WAIT, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);  /* UNEXPECTED SHUTDOWN chunk */
2365   _(COOKIE_WAIT, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);  /* UNEXPECTED SHUTDOWN_ACK chunk */
2366   _(COOKIE_WAIT, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);  /* UNEXPECTED OPERATION_ERROR chunk */
2367   _(COOKIE_WAIT, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);  /* UNEXPECTED COOKIE_ECHO chunk */
2368   _(COOKIE_WAIT, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED COOKIE_ACK chunk */
2369   _(COOKIE_WAIT, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);        /* UNEXPECTED ECNE chunk */
2370   _(COOKIE_WAIT, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);  /* UNEXPECTED CWR chunk */
2371   _(COOKIE_WAIT, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);      /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2372   _(COOKIE_WAIT, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2373     SCTP_ERROR_NONE);
2374
2375   _(COOKIE_ECHOED, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_NONE);
2376   _(COOKIE_ECHOED, INIT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);   /* UNEXPECTED INIT chunk which requires special handling */
2377   _(COOKIE_ECHOED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED INIT_ACK chunk */
2378   _(COOKIE_ECHOED, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);        /* UNEXPECTED SACK chunk */
2379   _(COOKIE_ECHOED, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT chunk */
2380   _(COOKIE_ECHOED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT_ACK chunk */
2381   _(COOKIE_ECHOED, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);      /* UNEXPECTED ABORT chunk */
2382   _(COOKIE_ECHOED, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);        /* UNEXPECTED SHUTDOWN chunk */
2383   _(COOKIE_ECHOED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);        /* UNEXPECTED SHUTDOWN_ACK chunk */
2384   _(COOKIE_ECHOED, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);        /* UNEXPECTED OPERATION_ERROR chunk */
2385   _(COOKIE_ECHOED, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);        /* UNEXPECTED COOKIE_ECHO chunk */
2386   _(COOKIE_ECHOED, COOKIE_ACK, SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
2387     SCTP_ERROR_NONE);
2388   _(COOKIE_ECHOED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);      /* UNEXPECTED ECNE chunk */
2389   _(COOKIE_ECHOED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);        /* UNEXPECTED CWR chunk */
2390   _(COOKIE_ECHOED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);    /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2391   _(COOKIE_ECHOED, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2392     SCTP_ERROR_NONE);
2393
2394   _(ESTABLISHED, DATA, SCTP_INPUT_NEXT_ESTABLISHED_PHASE, SCTP_ERROR_NONE);
2395   _(ESTABLISHED, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);  /* UNEXPECTED INIT chunk */
2396   _(ESTABLISHED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);   /* UNEXPECTED INIT_ACK chunk */
2397   _(ESTABLISHED, SACK, SCTP_INPUT_NEXT_ESTABLISHED_PHASE, SCTP_ERROR_NONE);
2398   _(ESTABLISHED, HEARTBEAT, SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
2399     SCTP_ERROR_NONE);
2400   _(ESTABLISHED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_ESTABLISHED_PHASE,
2401     SCTP_ERROR_NONE);
2402   _(ESTABLISHED, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);        /* UNEXPECTED ABORT chunk */
2403   _(ESTABLISHED, SHUTDOWN, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2404   _(ESTABLISHED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);  /* UNEXPECTED SHUTDOWN_ACK chunk */
2405   _(ESTABLISHED, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);  /* UNEXPECTED OPERATION_ERROR chunk */
2406   _(ESTABLISHED, COOKIE_ECHO, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_COOKIE_ECHO_VIOLATION);  /* UNEXPECTED COOKIE_ECHO chunk */
2407   _(ESTABLISHED, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED COOKIE_ACK chunk */
2408   _(ESTABLISHED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);        /* UNEXPECTED ECNE chunk */
2409   _(ESTABLISHED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);  /* UNEXPECTED CWR chunk */
2410   _(ESTABLISHED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);      /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2411   _(ESTABLISHED, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2412     SCTP_ERROR_NONE);
2413
2414   _(SHUTDOWN_PENDING, DATA, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2415   _(SHUTDOWN_PENDING, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);     /* UNEXPECTED INIT chunk */
2416   _(SHUTDOWN_PENDING, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);      /* UNEXPECTED INIT_ACK chunk */
2417   _(SHUTDOWN_PENDING, SACK, SCTP_INPUT_NEXT_LISTEN_PHASE, SCTP_ERROR_NONE);
2418   _(SHUTDOWN_PENDING, HEARTBEAT, SCTP_INPUT_NEXT_LISTEN_PHASE,
2419     SCTP_ERROR_NONE);
2420   _(SHUTDOWN_PENDING, HEARTBEAT_ACK, SCTP_INPUT_NEXT_LISTEN_PHASE,
2421     SCTP_ERROR_NONE);
2422   _(SHUTDOWN_PENDING, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);   /* UNEXPECTED ABORT chunk */
2423   _(SHUTDOWN_PENDING, SHUTDOWN, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2424     SCTP_ERROR_NONE);
2425   _(SHUTDOWN_PENDING, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);     /* UNEXPECTED SHUTDOWN_ACK chunk */
2426   _(SHUTDOWN_PENDING, OPERATION_ERROR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_OPERATION_ERROR_VIOLATION);     /* UNEXPECTED OPERATION_ERROR chunk */
2427   _(SHUTDOWN_PENDING, COOKIE_ECHO, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2428     SCTP_ERROR_NONE);
2429   _(SHUTDOWN_PENDING, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);    /* UNEXPECTED COOKIE_ACK chunk */
2430   _(SHUTDOWN_PENDING, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);   /* UNEXPECTED ECNE chunk */
2431   _(SHUTDOWN_PENDING, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);     /* UNEXPECTED CWR chunk */
2432   _(SHUTDOWN_PENDING, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION); /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2433   _(SHUTDOWN_PENDING, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2434     SCTP_ERROR_NONE);
2435
2436   _(SHUTDOWN_SENT, DATA, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2437   _(SHUTDOWN_SENT, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);        /* UNEXPECTED INIT chunk */
2438   _(SHUTDOWN_SENT, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP); /* UNEXPECTED INIT_ACK chunk */
2439   _(SHUTDOWN_SENT, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);        /* UNEXPECTED SACK chunk */
2440   _(SHUTDOWN_SENT, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT chunk */
2441   _(SHUTDOWN_SENT, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);      /* UNEXPECTED HEARTBEAT_ACK chunk */
2442   _(SHUTDOWN_SENT, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);      /* UNEXPECTED ABORT chunk */
2443   _(SHUTDOWN_SENT, SHUTDOWN, SCTP_INPUT_NEXT_SHUTDOWN_PHASE, SCTP_ERROR_NONE);
2444   _(SHUTDOWN_SENT, SHUTDOWN_ACK, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2445     SCTP_ERROR_NONE);
2446   _(SHUTDOWN_SENT, COOKIE_ECHO, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2447     SCTP_ERROR_NONE);
2448   _(SHUTDOWN_SENT, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);       /* UNEXPECTED COOKIE_ACK chunk */
2449   _(SHUTDOWN_SENT, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);      /* UNEXPECTED ECNE chunk */
2450   _(SHUTDOWN_SENT, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);        /* UNEXPECTED CWR chunk */
2451   _(SHUTDOWN_SENT, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);    /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2452   _(SHUTDOWN_SENT, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2453     SCTP_ERROR_NONE);
2454
2455   _(SHUTDOWN_RECEIVED, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_DATA_CHUNK_VIOLATION);    /* UNEXPECTED DATA chunk */
2456   _(SHUTDOWN_RECEIVED, INIT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_INIT_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2457   _(SHUTDOWN_RECEIVED, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);     /* UNEXPECTED INIT_ACK chunk */
2458   _(SHUTDOWN_RECEIVED, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2459   _(SHUTDOWN_RECEIVED, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT chunk */
2460   _(SHUTDOWN_RECEIVED, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT_ACK chunk */
2461   _(SHUTDOWN_RECEIVED, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);  /* UNEXPECTED ABORT chunk */
2462   _(SHUTDOWN_RECEIVED, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);    /* UNEXPECTED SHUTDOWN chunk */
2463   _(SHUTDOWN_RECEIVED, SHUTDOWN_ACK, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2464     SCTP_ERROR_NONE);
2465   _(SHUTDOWN_RECEIVED, COOKIE_ECHO, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2466     SCTP_ERROR_NONE);
2467   _(SHUTDOWN_RECEIVED, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);   /* UNEXPECTED COOKIE_ACK chunk */
2468   _(SHUTDOWN_RECEIVED, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);  /* UNEXPECTED ECNE chunk */
2469   _(SHUTDOWN_RECEIVED, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);    /* UNEXPECTED CWR chunk */
2470   _(SHUTDOWN_RECEIVED, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_COMPLETE_VIOLATION);        /* UNEXPECTED SHUTDOWN_COMPLETE chunk */
2471   _(SHUTDOWN_RECEIVED, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2472     SCTP_ERROR_NONE);
2473
2474   _(SHUTDOWN_ACK_SENT, DATA, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_DATA_CHUNK_VIOLATION);    /* UNEXPECTED DATA chunk */
2475   _(SHUTDOWN_ACK_SENT, INIT, SCTP_INPUT_NEXT_RCV_PHASE, SCTP_ERROR_NONE);       /* UNEXPECTED INIT chunk */
2476   _(SHUTDOWN_ACK_SENT, INIT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);     /* UNEXPECTED INIT_ACK chunk */
2477   _(SHUTDOWN_ACK_SENT, SACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SACK_CHUNK_VIOLATION);    /* UNEXPECTED INIT chunk */
2478   _(SHUTDOWN_ACK_SENT, HEARTBEAT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT chunk */
2479   _(SHUTDOWN_ACK_SENT, HEARTBEAT_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_HEARTBEAT_ACK_CHUNK_VIOLATION);  /* UNEXPECTED HEARTBEAT_ACK chunk */
2480   _(SHUTDOWN_ACK_SENT, ABORT, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ABORT_CHUNK_VIOLATION);  /* UNEXPECTED ABORT chunk */
2481   _(SHUTDOWN_ACK_SENT, SHUTDOWN, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_CHUNK_VIOLATION);    /* UNEXPECTED SHUTDOWN chunk */
2482   _(SHUTDOWN_ACK_SENT, SHUTDOWN_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_SHUTDOWN_ACK_CHUNK_VIOLATION);    /* UNEXPECTED SHUTDOWN_ACK chunk */
2483   _(SHUTDOWN_ACK_SENT, COOKIE_ECHO, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2484     SCTP_ERROR_NONE);
2485   _(SHUTDOWN_ACK_SENT, COOKIE_ACK, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ACK_DUP);   /* UNEXPECTED COOKIE_ACK chunk */
2486   _(SHUTDOWN_ACK_SENT, ECNE, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_ECNE_VIOLATION);  /* UNEXPECTED ECNE chunk */
2487   _(SHUTDOWN_ACK_SENT, CWR, SCTP_INPUT_NEXT_DROP, SCTP_ERROR_CWR_VIOLATION);    /* UNEXPECTED CWR chunk */
2488   _(SHUTDOWN_ACK_SENT, SHUTDOWN_COMPLETE, SCTP_INPUT_NEXT_SHUTDOWN_PHASE,
2489     SCTP_ERROR_NONE);
2490   _(SHUTDOWN_ACK_SENT, OPERATION_ERROR, SCTP_INPUT_NEXT_LISTEN_PHASE,
2491     SCTP_ERROR_NONE);
2492
2493   /* TODO: Handle COOKIE ECHO when a TCB Exists */
2494
2495 #undef _
2496 }
2497
2498 clib_error_t *
2499 sctp_input_init (vlib_main_t * vm)
2500 {
2501   clib_error_t *error = 0;
2502   sctp_main_t *tm = vnet_get_sctp_main ();
2503
2504   if ((error = vlib_call_init_function (vm, sctp_init)))
2505     return error;
2506
2507   /* Initialize dispatch table. */
2508   sctp_dispatch_table_init (tm);
2509
2510   return error;
2511 }
2512
2513 VLIB_INIT_FUNCTION (sctp_input_init);
2514
2515 /*
2516  * fd.io coding-style-patch-verification: ON
2517  *
2518  * Local Variables:
2519  * eval: (c-set-style "gnu")
2520  * End:
2521  */