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