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