Prep-work patch for congestion-control
[vpp.git] / src / vnet / sctp / sctp_output.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 <vnet/sctp/sctp.h>
16 #include <vnet/sctp/sctp_debug.h>
17 #include <vppinfra/random.h>
18
19 vlib_node_registration_t sctp4_output_node;
20 vlib_node_registration_t sctp6_output_node;
21
22 typedef enum _sctp_output_next
23 {
24   SCTP_OUTPUT_NEXT_DROP,
25   SCTP_OUTPUT_NEXT_IP_LOOKUP,
26   SCTP_OUTPUT_N_NEXT
27 } sctp_output_next_t;
28
29 #define foreach_sctp4_output_next               \
30   _ (DROP, "error-drop")                        \
31   _ (IP_LOOKUP, "ip4-lookup")
32
33 #define foreach_sctp6_output_next               \
34   _ (DROP, "error-drop")                        \
35   _ (IP_LOOKUP, "ip6-lookup")
36
37 static char *sctp_error_strings[] = {
38 #define sctp_error(n,s) s,
39 #include <vnet/sctp/sctp_error.def>
40 #undef sctp_error
41 };
42
43 typedef struct
44 {
45   sctp_header_t sctp_header;
46   sctp_connection_t sctp_connection;
47 } sctp_tx_trace_t;
48
49 /**
50  * Flush tx frame populated by retransmits and timer pops
51  */
52 void
53 sctp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
54 {
55   if (sctp_main.tx_frames[!is_ip4][thread_index])
56     {
57       u32 next_index;
58       next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
59       vlib_put_frame_to_node (vm, next_index,
60                               sctp_main.tx_frames[!is_ip4][thread_index]);
61       sctp_main.tx_frames[!is_ip4][thread_index] = 0;
62     }
63 }
64
65 /**
66  * Flush ip lookup tx frames populated by timer pops
67  */
68 always_inline void
69 sctp_flush_frame_to_ip_lookup (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
70 {
71   if (sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index])
72     {
73       u32 next_index;
74       next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
75       vlib_put_frame_to_node (vm, next_index,
76                               sctp_main.ip_lookup_tx_frames[!is_ip4]
77                               [thread_index]);
78       sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
79     }
80 }
81
82 /**
83  * Flush v4 and v6 sctp and ip-lookup tx frames for thread index
84  */
85 void
86 sctp_flush_frames_to_output (u8 thread_index)
87 {
88   vlib_main_t *vm = vlib_get_main ();
89   sctp_flush_frame_to_output (vm, thread_index, 1);
90   sctp_flush_frame_to_output (vm, thread_index, 0);
91   sctp_flush_frame_to_ip_lookup (vm, thread_index, 1);
92   sctp_flush_frame_to_ip_lookup (vm, thread_index, 0);
93 }
94
95 u32
96 ip4_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
97                            ip4_header_t * ip0)
98 {
99   ip_csum_t checksum;
100   u32 ip_header_length, payload_length_host_byte_order;
101   u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
102   void *data_this_buffer;
103
104   /* Initialize checksum with ip header. */
105   ip_header_length = ip4_header_bytes (ip0);
106   payload_length_host_byte_order =
107     clib_net_to_host_u16 (ip0->length) - ip_header_length;
108   checksum =
109     clib_host_to_net_u32 (payload_length_host_byte_order +
110                           (ip0->protocol << 16));
111
112   if (BITS (uword) == 32)
113     {
114       checksum =
115         ip_csum_with_carry (checksum,
116                             clib_mem_unaligned (&ip0->src_address, u32));
117       checksum =
118         ip_csum_with_carry (checksum,
119                             clib_mem_unaligned (&ip0->dst_address, u32));
120     }
121   else
122     checksum =
123       ip_csum_with_carry (checksum,
124                           clib_mem_unaligned (&ip0->src_address, u64));
125
126   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
127   data_this_buffer = (void *) ip0 + ip_header_length;
128   n_ip_bytes_this_buffer =
129     p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
130   if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
131     {
132       n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
133         n_ip_bytes_this_buffer - ip_header_length : 0;
134     }
135   while (1)
136     {
137       checksum =
138         ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
139       n_bytes_left -= n_this_buffer;
140       if (n_bytes_left == 0)
141         break;
142
143       ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
144       p0 = vlib_get_buffer (vm, p0->next_buffer);
145       data_this_buffer = vlib_buffer_get_current (p0);
146       n_this_buffer = p0->current_length;
147     }
148
149   return checksum;
150 }
151
152 u32
153 ip6_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
154                            ip6_header_t * ip0, int *bogus_lengthp)
155 {
156   ip_csum_t checksum;
157   u16 payload_length_host_byte_order;
158   u32 i, n_this_buffer, n_bytes_left;
159   u32 headers_size = sizeof (ip0[0]);
160   void *data_this_buffer;
161
162   ASSERT (bogus_lengthp);
163   *bogus_lengthp = 0;
164
165   /* Initialize checksum with ip header. */
166   checksum = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
167   payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
168   data_this_buffer = (void *) (ip0 + 1);
169
170   for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
171     {
172       checksum = ip_csum_with_carry (checksum,
173                                      clib_mem_unaligned (&ip0->
174                                                          src_address.as_uword
175                                                          [i], uword));
176       checksum =
177         ip_csum_with_carry (checksum,
178                             clib_mem_unaligned (&ip0->dst_address.as_uword[i],
179                                                 uword));
180     }
181
182   /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
183    * or UDP-Ping packets */
184   if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
185     {
186       u32 skip_bytes;
187       ip6_hop_by_hop_ext_t *ext_hdr =
188         (ip6_hop_by_hop_ext_t *) data_this_buffer;
189
190       /* validate really icmp6 next */
191       ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_SCTP));
192
193       skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
194       data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
195
196       payload_length_host_byte_order -= skip_bytes;
197       headers_size += skip_bytes;
198     }
199
200   n_bytes_left = n_this_buffer = payload_length_host_byte_order;
201   if (p0 && n_this_buffer + headers_size > p0->current_length)
202     n_this_buffer =
203       p0->current_length >
204       headers_size ? p0->current_length - headers_size : 0;
205   while (1)
206     {
207       checksum =
208         ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
209       n_bytes_left -= n_this_buffer;
210       if (n_bytes_left == 0)
211         break;
212
213       if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
214         {
215           *bogus_lengthp = 1;
216           return 0xfefe;
217         }
218       p0 = vlib_get_buffer (vm, p0->next_buffer);
219       data_this_buffer = vlib_buffer_get_current (p0);
220       n_this_buffer = p0->current_length;
221     }
222
223   return checksum;
224 }
225
226 void
227 sctp_push_ip_hdr (sctp_main_t * tm, sctp_sub_connection_t * sctp_sub_conn,
228                   vlib_buffer_t * b)
229 {
230   sctp_header_t *th = vlib_buffer_get_current (b);
231   vlib_main_t *vm = vlib_get_main ();
232   if (sctp_sub_conn->c_is_ip4)
233     {
234       ip4_header_t *ih;
235       ih = vlib_buffer_push_ip4 (vm, b, &sctp_sub_conn->c_lcl_ip4,
236                                  &sctp_sub_conn->c_rmt_ip4, IP_PROTOCOL_SCTP,
237                                  1);
238       th->checksum = ip4_sctp_compute_checksum (vm, b, ih);
239     }
240   else
241     {
242       ip6_header_t *ih;
243       int bogus = ~0;
244
245       ih = vlib_buffer_push_ip6 (vm, b, &sctp_sub_conn->c_lcl_ip6,
246                                  &sctp_sub_conn->c_rmt_ip6, IP_PROTOCOL_SCTP);
247       th->checksum = ip6_sctp_compute_checksum (vm, b, ih, &bogus);
248       ASSERT (!bogus);
249     }
250 }
251
252 always_inline void *
253 sctp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
254 {
255   if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
256     vlib_buffer_free_one (vm, b->next_buffer);
257   /* Zero all flags but free list index and trace flag */
258   b->flags &= VLIB_BUFFER_NEXT_PRESENT - 1;
259   b->current_data = 0;
260   b->current_length = 0;
261   b->total_length_not_including_first_buffer = 0;
262   vnet_buffer (b)->sctp.flags = 0;
263
264   /* Leave enough space for headers */
265   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
266 }
267
268 always_inline void *
269 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
270 {
271   ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
272   b->flags &= VLIB_BUFFER_FREE_LIST_INDEX_MASK;
273   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
274   b->total_length_not_including_first_buffer = 0;
275   vnet_buffer (b)->sctp.flags = 0;
276   VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
277   /* Leave enough space for headers */
278   return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
279 }
280
281 always_inline int
282 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
283 {
284   vlib_main_t *vm = vlib_get_main ();
285   u32 current_length = vec_len (tm->tx_buffers[thread_index]);
286   u32 n_allocated;
287
288   vec_validate (tm->tx_buffers[thread_index],
289                 current_length + n_free_buffers - 1);
290   n_allocated =
291     vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
292                        n_free_buffers);
293   _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
294   /* buffer shortage, report failure */
295   if (vec_len (tm->tx_buffers[thread_index]) == 0)
296     {
297       clib_warning ("out of buffers");
298       return -1;
299     }
300   return 0;
301 }
302
303 always_inline int
304 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
305 {
306   u32 *my_tx_buffers;
307   u32 thread_index = vlib_get_thread_index ();
308   if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
309     {
310       if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
311         return -1;
312     }
313   my_tx_buffers = tm->tx_buffers[thread_index];
314   *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
315   _vec_len (my_tx_buffers) -= 1;
316   return 0;
317 }
318
319 always_inline void
320 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
321                           u8 is_ip4, u8 flush)
322 {
323   sctp_main_t *tm = vnet_get_sctp_main ();
324   u32 thread_index = vlib_get_thread_index ();
325   u32 *to_next, next_index;
326   vlib_frame_t *f;
327
328   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
329   b->error = 0;
330
331   /* Decide where to send the packet */
332   next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
333   sctp_trajectory_add_start (b, 2);
334
335   /* Get frame to v4/6 output node */
336   f = tm->tx_frames[!is_ip4][thread_index];
337   if (!f)
338     {
339       f = vlib_get_frame_to_node (vm, next_index);
340       ASSERT (f);
341       tm->tx_frames[!is_ip4][thread_index] = f;
342     }
343   to_next = vlib_frame_vector_args (f);
344   to_next[f->n_vectors] = bi;
345   f->n_vectors += 1;
346   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
347     {
348       vlib_put_frame_to_node (vm, next_index, f);
349       tm->tx_frames[!is_ip4][thread_index] = 0;
350     }
351 }
352
353 always_inline void
354 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
355                             u8 is_ip4)
356 {
357   sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
358 }
359
360 always_inline void
361 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
362                              u8 is_ip4, u8 flush)
363 {
364   sctp_main_t *tm = vnet_get_sctp_main ();
365   u32 thread_index = vlib_get_thread_index ();
366   u32 *to_next, next_index;
367   vlib_frame_t *f;
368
369   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
370   b->error = 0;
371
372   /* Default FIB for now */
373   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
374
375   /* Send to IP lookup */
376   next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
377   if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
378     {
379       b->pre_data[0] = 2;
380       b->pre_data[1] = next_index;
381     }
382
383   f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
384   if (!f)
385     {
386       f = vlib_get_frame_to_node (vm, next_index);
387       ASSERT (f);
388       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
389     }
390
391   to_next = vlib_frame_vector_args (f);
392   to_next[f->n_vectors] = bi;
393   f->n_vectors += 1;
394   if (flush || f->n_vectors == VLIB_FRAME_SIZE)
395     {
396       vlib_put_frame_to_node (vm, next_index, f);
397       tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
398     }
399 }
400
401 always_inline void
402 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
403                            u8 is_ip4)
404 {
405   sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
406 }
407
408 always_inline void
409 sctp_enqueue_to_ip_lookup_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
410                                u8 is_ip4)
411 {
412   sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 1);
413 }
414
415 /**
416  * Convert buffer to INIT
417  */
418 void
419 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
420 {
421   u32 random_seed = random_default_seed ();
422   u16 alloc_bytes = sizeof (sctp_init_chunk_t);
423   sctp_sub_connection_t *sub_conn =
424     &sctp_conn->sub_conn[sctp_pick_conn_idx_on_chunk (INIT)];
425
426   sctp_ipv4_addr_param_t *ip4_param = 0;
427   sctp_ipv6_addr_param_t *ip6_param = 0;
428
429   if (sub_conn->c_is_ip4)
430     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
431   else
432     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
433
434   /* As per RFC 4960 the chunk_length value does NOT contemplate
435    * the size of the first header (see sctp_header_t) and any padding
436    */
437   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
438
439   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
440
441   sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
442
443   u16 pointer_offset = sizeof (init_chunk);
444   if (sub_conn->c_is_ip4)
445     {
446       ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
447       ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
448
449       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
450     }
451   else
452     {
453       ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
454       ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
455       ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
456
457       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
458     }
459
460   init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
461   init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
462   init_chunk->sctp_hdr.checksum = 0;
463   /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
464   init_chunk->sctp_hdr.verification_tag = 0x0;
465
466   vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
467   vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
468   vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
469
470   init_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
471   init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
472   init_chunk->inboud_streams_count =
473     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
474   init_chunk->outbound_streams_count =
475     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
476
477   init_chunk->initial_tsn =
478     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
479   SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
480                           sctp_conn->local_initial_tsn);
481
482   sctp_conn->local_tag = init_chunk->initiate_tag;
483
484   vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
485
486   SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
487                           "CHUNK_TYPE = %s, "
488                           "SRC_PORT = %u, DST_PORT = %u",
489                           sub_conn->connection.c_index,
490                           sctp_conn->state,
491                           sctp_state_to_string (sctp_conn->state),
492                           sctp_chunk_to_string (INIT),
493                           init_chunk->sctp_hdr.src_port,
494                           init_chunk->sctp_hdr.dst_port);
495 }
496
497 u64
498 sctp_compute_mac ()
499 {
500   return 0x0;
501 }
502
503 void
504 sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn,
505                                vlib_buffer_t * b)
506 {
507   vlib_main_t *vm = vlib_get_main ();
508   u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ACK);
509
510   sctp_reuse_buffer (vm, b);
511
512   u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
513
514   /* As per RFC 4960 the chunk_length value does NOT contemplate
515    * the size of the first header (see sctp_header_t) and any padding
516    */
517   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
518
519   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
520
521   sctp_cookie_ack_chunk_t *cookie_ack_chunk =
522     vlib_buffer_push_uninit (b, alloc_bytes);
523
524   cookie_ack_chunk->sctp_hdr.checksum = 0;
525   cookie_ack_chunk->sctp_hdr.src_port =
526     sctp_conn->sub_conn[idx].connection.lcl_port;
527   cookie_ack_chunk->sctp_hdr.dst_port =
528     sctp_conn->sub_conn[idx].connection.rmt_port;
529   cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
530   vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
531   vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
532
533   vnet_buffer (b)->sctp.connection_index =
534     sctp_conn->sub_conn[idx].connection.c_index;
535 }
536
537 void
538 sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn,
539                                 vlib_buffer_t * b,
540                                 sctp_state_cookie_param_t * sc)
541 {
542   vlib_main_t *vm = vlib_get_main ();
543   u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ECHO);
544
545   sctp_reuse_buffer (vm, b);
546
547   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
548   u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
549   /* As per RFC 4960 the chunk_length value does NOT contemplate
550    * the size of the first header (see sctp_header_t) and any padding
551    */
552   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
553   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
554   sctp_cookie_echo_chunk_t *cookie_echo_chunk =
555     vlib_buffer_push_uninit (b, alloc_bytes);
556   cookie_echo_chunk->sctp_hdr.checksum = 0;
557   cookie_echo_chunk->sctp_hdr.src_port =
558     sctp_conn->sub_conn[idx].connection.lcl_port;
559   cookie_echo_chunk->sctp_hdr.dst_port =
560     sctp_conn->sub_conn[idx].connection.rmt_port;
561   cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
562   vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
563   vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
564   clib_memcpy (&(cookie_echo_chunk->cookie), sc,
565                sizeof (sctp_state_cookie_param_t));
566   vnet_buffer (b)->sctp.connection_index =
567     sctp_conn->sub_conn[idx].connection.c_index;
568 }
569
570 /**
571  * Convert buffer to INIT-ACK
572  */
573 void
574 sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
575                             ip4_address_t * ip4_addr,
576                             ip6_address_t * ip6_addr)
577 {
578   vlib_main_t *vm = vlib_get_main ();
579   sctp_ipv4_addr_param_t *ip4_param = 0;
580   sctp_ipv6_addr_param_t *ip6_param = 0;
581   u8 idx = sctp_pick_conn_idx_on_chunk (INIT_ACK);
582   u32 random_seed = random_default_seed ();
583
584   sctp_reuse_buffer (vm, b);
585
586   /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
587   u16 alloc_bytes =
588     sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
589
590   if (PREDICT_TRUE (ip4_addr != NULL))
591     {
592       /* Create room for variable-length fields in the INIT_ACK chunk */
593       alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
594     }
595   if (PREDICT_TRUE (ip6_addr != NULL))
596     {
597       /* Create room for variable-length fields in the INIT_ACK chunk */
598       alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
599     }
600
601   if (sctp_conn->sub_conn[idx].connection.is_ip4)
602     alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
603   else
604     alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
605
606   /* As per RFC 4960 the chunk_length value does NOT contemplate
607    * the size of the first header (see sctp_header_t) and any padding
608    */
609   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
610
611   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
612
613   sctp_init_ack_chunk_t *init_ack_chunk =
614     vlib_buffer_push_uninit (b, alloc_bytes);
615
616   u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
617
618   /* Create State Cookie parameter */
619   sctp_state_cookie_param_t *state_cookie_param =
620     (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
621
622   state_cookie_param->param_hdr.type =
623     clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
624   state_cookie_param->param_hdr.length =
625     clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
626   state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
627   state_cookie_param->cookie_lifespan =
628     clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
629   state_cookie_param->mac = clib_host_to_net_u64 (sctp_compute_mac ());
630
631   pointer_offset += sizeof (sctp_state_cookie_param_t);
632
633   if (PREDICT_TRUE (ip4_addr != NULL))
634     {
635       sctp_ipv4_addr_param_t *ipv4_addr =
636         (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
637
638       ipv4_addr->param_hdr.type =
639         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
640       ipv4_addr->param_hdr.length =
641         clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
642       ipv4_addr->address.as_u32 = ip4_addr->as_u32;
643
644       pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
645     }
646   if (PREDICT_TRUE (ip6_addr != NULL))
647     {
648       sctp_ipv6_addr_param_t *ipv6_addr =
649         (sctp_ipv6_addr_param_t *) init_ack_chunk +
650         sizeof (sctp_init_chunk_t) + pointer_offset;
651
652       ipv6_addr->param_hdr.type =
653         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
654       ipv6_addr->param_hdr.length =
655         clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
656       ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
657       ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
658
659       pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
660     }
661
662   if (sctp_conn->sub_conn[idx].connection.is_ip4)
663     {
664       ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
665       ip4_param->address.as_u32 =
666         sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
667
668       pointer_offset += sizeof (sctp_ipv4_addr_param_t);
669     }
670   else
671     {
672       ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
673       ip6_param->address.as_u64[0] =
674         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
675       ip6_param->address.as_u64[1] =
676         sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
677
678       pointer_offset += sizeof (sctp_ipv6_addr_param_t);
679     }
680
681   /* src_port & dst_port are already in network byte-order */
682   init_ack_chunk->sctp_hdr.checksum = 0;
683   init_ack_chunk->sctp_hdr.src_port =
684     sctp_conn->sub_conn[idx].connection.lcl_port;
685   init_ack_chunk->sctp_hdr.dst_port =
686     sctp_conn->sub_conn[idx].connection.rmt_port;
687   /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
688   init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
689   init_ack_chunk->initial_tsn =
690     clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
691   SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
692                           init_ack_chunk->initial_tsn);
693
694   vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
695   vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
696
697   init_ack_chunk->initiate_tag =
698     clib_host_to_net_u32 (random_u32 (&random_seed));
699
700   init_ack_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
701   init_ack_chunk->inboud_streams_count =
702     clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
703   init_ack_chunk->outbound_streams_count =
704     clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
705
706   sctp_conn->local_tag = init_ack_chunk->initiate_tag;
707
708   vnet_buffer (b)->sctp.connection_index =
709     sctp_conn->sub_conn[idx].connection.c_index;
710 }
711
712 /**
713  * Convert buffer to SHUTDOWN
714  */
715 void
716 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
717 {
718   vlib_main_t *vm = vlib_get_main ();
719   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
720   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
721
722   b = sctp_reuse_buffer (vm, b);
723
724   /* As per RFC 4960 the chunk_length value does NOT contemplate
725    * the size of the first header (see sctp_header_t) and any padding
726    */
727   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
728
729   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
730
731   sctp_shutdown_association_chunk_t *shutdown_chunk =
732     vlib_buffer_push_uninit (b, alloc_bytes);
733
734   shutdown_chunk->sctp_hdr.checksum = 0;
735   /* No need of host_to_net conversion, already in net-byte order */
736   shutdown_chunk->sctp_hdr.src_port =
737     sctp_conn->sub_conn[idx].connection.lcl_port;
738   shutdown_chunk->sctp_hdr.dst_port =
739     sctp_conn->sub_conn[idx].connection.rmt_port;
740   shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
741   vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
742   vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
743
744   shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
745
746   vnet_buffer (b)->sctp.connection_index =
747     sctp_conn->sub_conn[idx].connection.c_index;
748 }
749
750 /*
751  * Send SHUTDOWN
752  */
753 void
754 sctp_send_shutdown (sctp_connection_t * sctp_conn)
755 {
756   vlib_buffer_t *b;
757   u32 bi;
758   sctp_main_t *tm = vnet_get_sctp_main ();
759   vlib_main_t *vm = vlib_get_main ();
760
761   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
762     return;
763
764   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
765     return;
766
767   b = vlib_get_buffer (vm, bi);
768   sctp_init_buffer (vm, b);
769   sctp_prepare_shutdown_chunk (sctp_conn, b);
770
771   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
772   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
773   sctp_enqueue_to_output_now (vm, b, bi,
774                               sctp_conn->sub_conn[idx].connection.is_ip4);
775 }
776
777 /**
778  * Convert buffer to SHUTDOWN_ACK
779  */
780 void
781 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn,
782                                  vlib_buffer_t * b)
783 {
784   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
785   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
786   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
787
788   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
789
790   sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
791     vlib_buffer_push_uninit (b, alloc_bytes);
792
793   shutdown_ack_chunk->sctp_hdr.checksum = 0;
794   /* No need of host_to_net conversion, already in net-byte order */
795   shutdown_ack_chunk->sctp_hdr.src_port =
796     sctp_conn->sub_conn[idx].connection.lcl_port;
797   shutdown_ack_chunk->sctp_hdr.dst_port =
798     sctp_conn->sub_conn[idx].connection.rmt_port;
799   shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
800
801   vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
802   vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
803
804   vnet_buffer (b)->sctp.connection_index =
805     sctp_conn->sub_conn[idx].connection.c_index;
806 }
807
808 /*
809  * Send SHUTDOWN_ACK
810  */
811 void
812 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn)
813 {
814   vlib_buffer_t *b;
815   u32 bi;
816   sctp_main_t *tm = vnet_get_sctp_main ();
817   vlib_main_t *vm = vlib_get_main ();
818
819   if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
820     return;
821
822   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
823     return;
824
825   b = vlib_get_buffer (vm, bi);
826   sctp_init_buffer (vm, b);
827   sctp_prepare_shutdown_ack_chunk (sctp_conn, b);
828
829   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
830   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
831   sctp_enqueue_to_ip_lookup (vm, b, bi,
832                              sctp_conn->sub_conn[idx].connection.is_ip4);
833
834   /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
835   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN, SCTP_RTO_INIT);
836   sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
837 }
838
839 /**
840  * Convert buffer to SACK
841  */
842 void
843 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
844 {
845   vlib_main_t *vm = vlib_get_main ();
846   u8 idx = sctp_pick_conn_idx_on_chunk (SACK);
847
848   sctp_reuse_buffer (vm, b);
849
850   u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
851
852   /* As per RFC 4960 the chunk_length value does NOT contemplate
853    * the size of the first header (see sctp_header_t) and any padding
854    */
855   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
856
857   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
858
859   sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
860
861   sack->sctp_hdr.checksum = 0;
862   sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
863   sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
864   sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
865   vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
866   vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
867
868   sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
869
870   sctp_conn->ack_state = 0;
871
872   vnet_buffer (b)->sctp.connection_index =
873     sctp_conn->sub_conn[idx].connection.c_index;
874 }
875
876 /**
877  * Convert buffer to HEARTBEAT
878  */
879 void
880 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn,
881                               vlib_buffer_t * b)
882 {
883   vlib_main_t *vm = vlib_get_main ();
884   u8 idx = sctp_pick_conn_idx_on_chunk (HEARTBEAT);
885   u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
886
887   b = sctp_reuse_buffer (vm, b);
888
889   /* As per RFC 4960 the chunk_length value does NOT contemplate
890    * the size of the first header (see sctp_header_t) and any padding
891    */
892   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
893
894   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
895
896   sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
897
898   hb_req->sctp_hdr.checksum = 0;
899   /* No need of host_to_net conversion, already in net-byte order */
900   hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
901   hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
902   hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
903   hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
904   hb_req->hb_info.param_hdr.length =
905     clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
906
907   vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
908   vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
909
910   vnet_buffer (b)->sctp.connection_index =
911     sctp_conn->sub_conn[idx].connection.c_index;
912 }
913
914 void
915 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
916 {
917   vlib_buffer_t *b;
918   u32 bi;
919   sctp_main_t *tm = vnet_get_sctp_main ();
920   vlib_main_t *vm = vlib_get_main ();
921
922   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
923     return;
924
925   b = vlib_get_buffer (vm, bi);
926   sctp_init_buffer (vm, b);
927   sctp_prepare_heartbeat_chunk (sctp_conn, b);
928
929   u8 idx = sctp_pick_conn_idx_on_state (SCTP_STATE_ESTABLISHED);
930   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
931   sctp_enqueue_to_ip_lookup (vm, b, bi,
932                              sctp_conn->sub_conn[idx].connection.is_ip4);
933 }
934
935 /**
936  * Convert buffer to SHUTDOWN_COMPLETE
937  */
938 void
939 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn,
940                                       vlib_buffer_t * b)
941 {
942   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
943   u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
944   alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
945
946   u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
947
948   sctp_shutdown_complete_chunk_t *shutdown_complete =
949     vlib_buffer_push_uninit (b, alloc_bytes);
950
951   shutdown_complete->sctp_hdr.checksum = 0;
952   /* No need of host_to_net conversion, already in net-byte order */
953   shutdown_complete->sctp_hdr.src_port =
954     sctp_conn->sub_conn[idx].connection.lcl_port;
955   shutdown_complete->sctp_hdr.dst_port =
956     sctp_conn->sub_conn[idx].connection.rmt_port;
957   shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
958
959   vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
960   vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
961
962   vnet_buffer (b)->sctp.connection_index =
963     sctp_conn->sub_conn[idx].connection.c_index;
964 }
965
966 void
967 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn)
968 {
969   vlib_buffer_t *b;
970   u32 bi;
971   sctp_main_t *tm = vnet_get_sctp_main ();
972   vlib_main_t *vm = vlib_get_main ();
973
974   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
975     return;
976
977   b = vlib_get_buffer (vm, bi);
978   sctp_init_buffer (vm, b);
979   sctp_prepare_shutdown_complete_chunk (sctp_conn, b);
980
981   u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
982   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
983   sctp_enqueue_to_ip_lookup (vm, b, bi,
984                              sctp_conn->sub_conn[idx].connection.is_ip4);
985
986   sctp_conn->state = SCTP_STATE_CLOSED;
987 }
988
989
990 /*
991  *  Send INIT
992  */
993 void
994 sctp_send_init (sctp_connection_t * sctp_conn)
995 {
996   vlib_buffer_t *b;
997   u32 bi;
998   sctp_main_t *tm = vnet_get_sctp_main ();
999   vlib_main_t *vm = vlib_get_main ();
1000
1001   if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1002     return;
1003
1004   b = vlib_get_buffer (vm, bi);
1005   u8 idx = sctp_pick_conn_idx_on_chunk (INIT);
1006
1007   sctp_init_buffer (vm, b);
1008   sctp_prepare_init_chunk (sctp_conn, b);
1009
1010   /* Measure RTT with this */
1011   sctp_conn->rtt_ts = sctp_time_now ();
1012   sctp_conn->rtt_seq = sctp_conn->next_tsn;
1013
1014   sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1015   sctp_enqueue_to_ip_lookup_now (vm, b, bi,
1016                                  sctp_conn->sub_conn[idx].c_is_ip4);
1017
1018   /* Start the T1_INIT timer */
1019   sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT, SCTP_RTO_INIT);
1020   /* Change state to COOKIE_WAIT */
1021   sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
1022 }
1023
1024 always_inline u8
1025 sctp_in_cong_recovery (sctp_connection_t * sctp_conn)
1026 {
1027   return 0;
1028 }
1029
1030 /**
1031  * Push SCTP header and update connection variables
1032  */
1033 static void
1034 sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
1035                  sctp_state_t next_state)
1036 {
1037   u8 idx = sctp_pick_conn_idx_on_chunk (DATA);
1038
1039   u16 data_len =
1040     b->current_length + b->total_length_not_including_first_buffer;
1041   ASSERT (!b->total_length_not_including_first_buffer
1042           || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1043
1044   SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1045                        "b->current_data = %p "
1046                        "data_len = %u",
1047                        b->current_length, b->current_data, data_len);
1048
1049   u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1050   u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1051
1052   bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1053
1054   sctp_payload_data_chunk_t *data_chunk =
1055     vlib_buffer_push_uninit (b, bytes_to_add);
1056
1057   data_chunk->sctp_hdr.checksum = 0;
1058   data_chunk->sctp_hdr.src_port =
1059     sctp_conn->sub_conn[idx].connection.lcl_port;
1060   data_chunk->sctp_hdr.dst_port =
1061     sctp_conn->sub_conn[idx].connection.rmt_port;
1062   data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1063
1064   data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
1065   data_chunk->stream_id = clib_host_to_net_u16 (0);
1066   data_chunk->stream_seq = clib_host_to_net_u16 (0);
1067
1068   vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1069   vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1070
1071   SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1072                        b->data, b->current_data);
1073
1074   sctp_conn->next_tsn += data_len;
1075
1076   vnet_buffer (b)->sctp.connection_index =
1077     sctp_conn->sub_conn[idx].connection.c_index;
1078 }
1079
1080 u32
1081 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
1082 {
1083   sctp_connection_t *sctp_conn =
1084     sctp_get_connection_from_transport (trans_conn);
1085   sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
1086
1087   if (sctp_conn->rtt_ts == 0 && !sctp_in_cong_recovery (sctp_conn))
1088     {
1089       sctp_conn->rtt_ts = sctp_time_now ();
1090       sctp_conn->rtt_seq = sctp_conn->next_tsn;
1091     }
1092   sctp_trajectory_add_start (b0, 3);
1093
1094   return 0;
1095
1096 }
1097
1098 always_inline uword
1099 sctp46_output_inline (vlib_main_t * vm,
1100                       vlib_node_runtime_t * node,
1101                       vlib_frame_t * from_frame, int is_ip4)
1102 {
1103   u32 n_left_from, next_index, *from, *to_next;
1104   u32 my_thread_index = vm->thread_index;
1105
1106   from = vlib_frame_vector_args (from_frame);
1107   n_left_from = from_frame->n_vectors;
1108   next_index = node->cached_next_index;
1109   sctp_set_time_now (my_thread_index);
1110
1111   while (n_left_from > 0)
1112     {
1113       u32 n_left_to_next;
1114
1115       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1116
1117       while (n_left_from > 0 && n_left_to_next > 0)
1118         {
1119           u32 bi0;
1120           vlib_buffer_t *b0;
1121           sctp_header_t *sctp_hdr = 0;
1122           sctp_connection_t *sctp_conn;
1123           sctp_tx_trace_t *t0;
1124           sctp_header_t *th0 = 0;
1125           u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1126             SCTP_OUTPUT_NEXT_IP_LOOKUP;
1127
1128 #if SCTP_DEBUG_STATE_MACHINE
1129           u16 packet_length = 0;
1130 #endif
1131
1132           bi0 = from[0];
1133           to_next[0] = bi0;
1134           from += 1;
1135           to_next += 1;
1136           n_left_from -= 1;
1137           n_left_to_next -= 1;
1138
1139           b0 = vlib_get_buffer (vm, bi0);
1140           sctp_conn =
1141             sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1142                                  my_thread_index);
1143
1144           if (PREDICT_FALSE (sctp_conn == 0))
1145             {
1146               error0 = SCTP_ERROR_INVALID_CONNECTION;
1147               next0 = SCTP_OUTPUT_NEXT_DROP;
1148               goto done;
1149             }
1150
1151           u8 idx = sctp_pick_conn_idx_on_state (sctp_conn->state);
1152
1153           th0 = vlib_buffer_get_current (b0);
1154
1155           if (is_ip4)
1156             {
1157               ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1158                                                          b0,
1159                                                          &sctp_conn->sub_conn
1160                                                          [idx].connection.
1161                                                          lcl_ip.ip4,
1162                                                          &sctp_conn->
1163                                                          sub_conn
1164                                                          [idx].connection.
1165                                                          rmt_ip.ip4,
1166                                                          IP_PROTOCOL_SCTP, 1);
1167
1168               u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1169
1170               sctp_hdr = ip4_next_header (iph4);
1171               sctp_hdr->checksum = checksum;
1172
1173               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1174
1175 #if SCTP_DEBUG_STATE_MACHINE
1176               packet_length = clib_net_to_host_u16 (iph4->length);
1177 #endif
1178             }
1179           else
1180             {
1181               ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1182                                                          b0,
1183                                                          &sctp_conn->sub_conn
1184                                                          [idx].
1185                                                          connection.lcl_ip.
1186                                                          ip6,
1187                                                          &sctp_conn->sub_conn
1188                                                          [idx].
1189                                                          connection.rmt_ip.
1190                                                          ip6,
1191                                                          IP_PROTOCOL_SCTP);
1192
1193               int bogus = ~0;
1194               u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1195               ASSERT (!bogus);
1196
1197               sctp_hdr = ip6_next_header (iph6);
1198               sctp_hdr->checksum = checksum;
1199
1200               vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1201               vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1202
1203 #if SCTP_DEBUG_STATE_MACHINE
1204               packet_length = clib_net_to_host_u16 (iph6->payload_length);
1205 #endif
1206             }
1207
1208           u8 is_valid =
1209             (sctp_conn->sub_conn[idx].connection.lcl_port ==
1210              sctp_hdr->src_port
1211              || sctp_conn->sub_conn[idx].connection.lcl_port ==
1212              sctp_hdr->dst_port)
1213             && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1214                 sctp_hdr->dst_port
1215                 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1216                 sctp_hdr->src_port);
1217
1218           sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1219           u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1220
1221           if (!is_valid)
1222             {
1223               SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1224                                       "packet_length = %u, "
1225                                       "chunk_type = %u [%s], "
1226                                       "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1227                                       "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1228                                       sctp_conn->sub_conn
1229                                       [idx].connection.c_index, packet_length,
1230                                       chunk_type,
1231                                       sctp_chunk_to_string (chunk_type),
1232                                       sctp_conn->sub_conn[idx].
1233                                       connection.lcl_port, sctp_hdr->src_port,
1234                                       sctp_conn->sub_conn[idx].
1235                                       connection.rmt_port,
1236                                       sctp_hdr->dst_port);
1237
1238               error0 = SCTP_ERROR_UNKOWN_CHUNK;
1239               next0 = SCTP_OUTPUT_NEXT_DROP;
1240               goto done;
1241             }
1242
1243           SCTP_DBG_STATE_MACHINE
1244             ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1245              "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1246              sctp_conn->sub_conn[idx].connection.c_index,
1247              sctp_conn->state, sctp_state_to_string (sctp_conn->state),
1248              sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1249              full_hdr->hdr.dst_port);
1250
1251           if (chunk_type == DATA)
1252             SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1253
1254           /* Let's make sure the state-machine does not send anything crazy */
1255           switch (sctp_conn->state)
1256             {
1257             case SCTP_STATE_CLOSED:
1258               {
1259                 if (chunk_type != INIT && chunk_type != INIT_ACK)
1260                   {
1261                     SCTP_DBG_STATE_MACHINE
1262                       ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1263                        sctp_chunk_to_string (chunk_type),
1264                        sctp_state_to_string (sctp_conn->state));
1265
1266                     error0 = SCTP_ERROR_UNKOWN_CHUNK;
1267                     next0 = SCTP_OUTPUT_NEXT_DROP;
1268                     goto done;
1269                   }
1270                 break;
1271               }
1272             case SCTP_STATE_ESTABLISHED:
1273               if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1274                   chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1275                   chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1276                 {
1277                   SCTP_DBG_STATE_MACHINE
1278                     ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1279                      sctp_chunk_to_string (chunk_type),
1280                      sctp_state_to_string (sctp_conn->state));
1281
1282                   error0 = SCTP_ERROR_UNKOWN_CHUNK;
1283                   next0 = SCTP_OUTPUT_NEXT_DROP;
1284                   goto done;
1285                 }
1286               break;
1287             case SCTP_STATE_COOKIE_WAIT:
1288               if (chunk_type != COOKIE_ECHO)
1289                 {
1290                   SCTP_DBG_STATE_MACHINE
1291                     ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1292                      sctp_chunk_to_string (chunk_type),
1293                      sctp_state_to_string (sctp_conn->state));
1294
1295                   error0 = SCTP_ERROR_UNKOWN_CHUNK;
1296                   next0 = SCTP_OUTPUT_NEXT_DROP;
1297                   goto done;
1298                 }
1299               /* Change state */
1300               sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1301               break;
1302             default:
1303               SCTP_DBG_STATE_MACHINE
1304                 ("Sending chunk (%s) based on state-machine status (%s)",
1305                  sctp_chunk_to_string (chunk_type),
1306                  sctp_state_to_string (sctp_conn->state));
1307               break;
1308             }
1309
1310           if (chunk_type == SHUTDOWN)
1311             {
1312               /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1313               sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1314                               SCTP_RTO_INIT);
1315               sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1316             }
1317
1318           if (chunk_type == DATA)
1319             {
1320               sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1321                                  SCTP_RTO_INIT);
1322             }
1323
1324           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1325           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1326
1327           b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1328
1329           SCTP_DBG_STATE_MACHINE ("CONNECTION_INDEX = %u, "
1330                                   "NEW_STATE = %s, "
1331                                   "CHUNK_SENT = %s",
1332                                   sctp_conn->sub_conn[idx].connection.c_index,
1333                                   sctp_state_to_string (sctp_conn->state),
1334                                   sctp_chunk_to_string (chunk_type));
1335
1336           vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1337
1338         done:
1339           b0->error = node->errors[error0];
1340           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1341             {
1342               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1343               if (th0)
1344                 {
1345                   clib_memcpy (&t0->sctp_header, th0,
1346                                sizeof (t0->sctp_header));
1347                 }
1348               else
1349                 {
1350                   memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1351                 }
1352               clib_memcpy (&t0->sctp_connection, sctp_conn,
1353                            sizeof (t0->sctp_connection));
1354             }
1355
1356           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1357                                            n_left_to_next, bi0, next0);
1358         }
1359
1360       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1361     }
1362
1363   return from_frame->n_vectors;
1364 }
1365
1366 static uword
1367 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1368               vlib_frame_t * from_frame)
1369 {
1370   return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1371 }
1372
1373 static uword
1374 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1375               vlib_frame_t * from_frame)
1376 {
1377   return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1378 }
1379
1380 /* *INDENT-OFF* */
1381 VLIB_REGISTER_NODE (sctp4_output_node) =
1382 {
1383   .function = sctp4_output,.name = "sctp4-output",
1384     /* Takes a vector of packets. */
1385     .vector_size = sizeof (u32),
1386     .n_errors = SCTP_N_ERROR,
1387     .error_strings = sctp_error_strings,
1388     .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1389     .next_nodes = {
1390 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1391     foreach_sctp4_output_next
1392 #undef _
1393     },
1394     .format_buffer = format_sctp_header,
1395     .format_trace = format_sctp_tx_trace,
1396 };
1397 /* *INDENT-ON* */
1398
1399 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1400
1401 /* *INDENT-OFF* */
1402 VLIB_REGISTER_NODE (sctp6_output_node) =
1403 {
1404   .function = sctp6_output,
1405   .name = "sctp6-output",
1406     /* Takes a vector of packets. */
1407   .vector_size = sizeof (u32),
1408   .n_errors = SCTP_N_ERROR,
1409   .error_strings = sctp_error_strings,
1410   .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1411   .next_nodes = {
1412 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1413     foreach_sctp6_output_next
1414 #undef _
1415   },
1416   .format_buffer = format_sctp_header,
1417   .format_trace = format_sctp_tx_trace,
1418 };
1419 /* *INDENT-ON* */
1420
1421 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1422
1423 /*
1424  * fd.io coding-style-patch-verification: ON
1425  *
1426  * Local Variables:
1427  * eval: (c-set-style "gnu")
1428  * End:
1429  */