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