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