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