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