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