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