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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/sctp/sctp.h>
16 #include <vnet/sctp/sctp_debug.h>
17 #include <vppinfra/random.h>
19 vlib_node_registration_t sctp4_output_node;
20 vlib_node_registration_t sctp6_output_node;
22 typedef enum _sctp_output_next
24 SCTP_OUTPUT_NEXT_DROP,
25 SCTP_OUTPUT_NEXT_IP_LOOKUP,
29 #define foreach_sctp4_output_next \
30 _ (DROP, "error-drop") \
31 _ (IP_LOOKUP, "ip4-lookup")
33 #define foreach_sctp6_output_next \
34 _ (DROP, "error-drop") \
35 _ (IP_LOOKUP, "ip6-lookup")
37 static char *sctp_error_strings[] = {
38 #define sctp_error(n,s) s,
39 #include <vnet/sctp/sctp_error.def>
45 sctp_header_t sctp_header;
46 sctp_connection_t sctp_connection;
50 * Flush tx frame populated by retransmits and timer pops
53 sctp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
55 if (sctp_main.tx_frames[!is_ip4][thread_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;
66 * Flush ip lookup tx frames populated by timer pops
69 sctp_flush_frame_to_ip_lookup (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
71 if (sctp_main.ip_lookup_tx_frames[!is_ip4][thread_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]
78 sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
83 * Flush v4 and v6 sctp and ip-lookup tx frames for thread index
86 sctp_flush_frames_to_output (u8 thread_index)
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);
96 ip4_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
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;
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;
109 clib_host_to_net_u32 (payload_length_host_byte_order +
110 (ip0->protocol << 16));
112 if (BITS (uword) == 32)
115 ip_csum_with_carry (checksum,
116 clib_mem_unaligned (&ip0->src_address, u32));
118 ip_csum_with_carry (checksum,
119 clib_mem_unaligned (&ip0->dst_address, u32));
123 ip_csum_with_carry (checksum,
124 clib_mem_unaligned (&ip0->src_address, u64));
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)
132 n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
133 n_ip_bytes_this_buffer - ip_header_length : 0;
138 ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
139 n_bytes_left -= n_this_buffer;
140 if (n_bytes_left == 0)
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;
153 ip6_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
154 ip6_header_t * ip0, int *bogus_lengthp)
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;
162 ASSERT (bogus_lengthp);
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);
170 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
172 checksum = ip_csum_with_carry (checksum,
173 clib_mem_unaligned (&ip0->
177 ip_csum_with_carry (checksum,
178 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
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))
187 ip6_hop_by_hop_ext_t *ext_hdr =
188 (ip6_hop_by_hop_ext_t *) data_this_buffer;
190 /* validate really icmp6 next */
191 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_SCTP));
193 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
194 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
196 payload_length_host_byte_order -= skip_bytes;
197 headers_size += skip_bytes;
200 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
201 if (p0 && n_this_buffer + headers_size > p0->current_length)
204 headers_size ? p0->current_length - headers_size : 0;
208 ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
209 n_bytes_left -= n_this_buffer;
210 if (n_bytes_left == 0)
213 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
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;
227 sctp_push_ip_hdr (sctp_main_t * tm, sctp_sub_connection_t * sctp_sub_conn,
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)
235 ih = vlib_buffer_push_ip4 (vm, b, &sctp_sub_conn->c_lcl_ip4,
236 &sctp_sub_conn->c_rmt_ip4, IP_PROTOCOL_SCTP,
238 th->checksum = ip4_sctp_compute_checksum (vm, b, ih);
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);
253 sctp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
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;
260 b->current_length = 0;
261 b->total_length_not_including_first_buffer = 0;
262 vnet_buffer (b)->sctp.flags = 0;
264 /* Leave enough space for headers */
265 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
269 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
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);
282 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
284 vlib_main_t *vm = vlib_get_main ();
285 u32 current_length = vec_len (tm->tx_buffers[thread_index]);
288 vec_validate (tm->tx_buffers[thread_index],
289 current_length + n_free_buffers - 1);
291 vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
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)
297 clib_warning ("out of buffers");
304 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
307 u32 thread_index = vlib_get_thread_index ();
308 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
310 if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
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;
320 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
323 sctp_main_t *tm = vnet_get_sctp_main ();
324 u32 thread_index = vlib_get_thread_index ();
325 u32 *to_next, next_index;
328 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
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);
335 /* Get frame to v4/6 output node */
336 f = tm->tx_frames[!is_ip4][thread_index];
339 f = vlib_get_frame_to_node (vm, next_index);
341 tm->tx_frames[!is_ip4][thread_index] = f;
343 to_next = vlib_frame_vector_args (f);
344 to_next[f->n_vectors] = bi;
346 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
348 vlib_put_frame_to_node (vm, next_index, f);
349 tm->tx_frames[!is_ip4][thread_index] = 0;
354 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
357 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
361 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
364 sctp_main_t *tm = vnet_get_sctp_main ();
365 u32 thread_index = vlib_get_thread_index ();
366 u32 *to_next, next_index;
369 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
372 /* Default FIB for now */
373 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
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)
380 b->pre_data[1] = next_index;
383 f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
386 f = vlib_get_frame_to_node (vm, next_index);
388 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
391 to_next = vlib_frame_vector_args (f);
392 to_next[f->n_vectors] = bi;
394 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
396 vlib_put_frame_to_node (vm, next_index, f);
397 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
402 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
405 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
409 sctp_enqueue_to_ip_lookup_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
412 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 1);
416 * Convert buffer to INIT
419 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
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)];
426 sctp_ipv4_addr_param_t *ip4_param = 0;
427 sctp_ipv6_addr_param_t *ip6_param = 0;
429 if (sub_conn->c_is_ip4)
430 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
432 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
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
437 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
439 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
441 sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
443 u16 pointer_offset = sizeof (init_chunk);
444 if (sub_conn->c_is_ip4)
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;
449 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
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];
457 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
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;
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);
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);
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);
482 sctp_conn->local_tag = init_chunk->initiate_tag;
484 vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
486 SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
488 "SRC_PORT = %u, DST_PORT = %u",
489 sub_conn->connection.c_index,
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);
504 sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn,
507 vlib_main_t *vm = vlib_get_main ();
508 u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ACK);
510 sctp_reuse_buffer (vm, b);
512 u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
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
517 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
519 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
521 sctp_cookie_ack_chunk_t *cookie_ack_chunk =
522 vlib_buffer_push_uninit (b, alloc_bytes);
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);
533 vnet_buffer (b)->sctp.connection_index =
534 sctp_conn->sub_conn[idx].connection.c_index;
538 sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn,
540 sctp_state_cookie_param_t * sc)
542 vlib_main_t *vm = vlib_get_main ();
543 u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ECHO);
545 sctp_reuse_buffer (vm, b);
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
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;
571 * Convert buffer to INIT-ACK
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)
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 ();
584 sctp_reuse_buffer (vm, b);
586 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
588 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
590 if (PREDICT_TRUE (ip4_addr != NULL))
592 /* Create room for variable-length fields in the INIT_ACK chunk */
593 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
595 if (PREDICT_TRUE (ip6_addr != NULL))
597 /* Create room for variable-length fields in the INIT_ACK chunk */
598 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
601 if (sctp_conn->sub_conn[idx].connection.is_ip4)
602 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
604 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
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
609 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
611 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
613 sctp_init_ack_chunk_t *init_ack_chunk =
614 vlib_buffer_push_uninit (b, alloc_bytes);
616 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
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);
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 ());
631 pointer_offset += sizeof (sctp_state_cookie_param_t);
633 if (PREDICT_TRUE (ip4_addr != NULL))
635 sctp_ipv4_addr_param_t *ipv4_addr =
636 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
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;
644 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
646 if (PREDICT_TRUE (ip6_addr != NULL))
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;
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];
659 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
662 if (sctp_conn->sub_conn[idx].connection.is_ip4)
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;
668 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
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];
678 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
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);
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);
697 init_ack_chunk->initiate_tag =
698 clib_host_to_net_u32 (random_u32 (&random_seed));
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);
706 sctp_conn->local_tag = init_ack_chunk->initiate_tag;
708 vnet_buffer (b)->sctp.connection_index =
709 sctp_conn->sub_conn[idx].connection.c_index;
713 * Convert buffer to SHUTDOWN
716 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
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);
722 b = sctp_reuse_buffer (vm, b);
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
727 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
729 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
731 sctp_shutdown_association_chunk_t *shutdown_chunk =
732 vlib_buffer_push_uninit (b, alloc_bytes);
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);
744 shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
746 vnet_buffer (b)->sctp.connection_index =
747 sctp_conn->sub_conn[idx].connection.c_index;
754 sctp_send_shutdown (sctp_connection_t * sctp_conn)
758 sctp_main_t *tm = vnet_get_sctp_main ();
759 vlib_main_t *vm = vlib_get_main ();
761 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
764 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
767 b = vlib_get_buffer (vm, bi);
768 sctp_init_buffer (vm, b);
769 sctp_prepare_shutdown_chunk (sctp_conn, b);
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);
778 * Convert buffer to SHUTDOWN_ACK
781 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn,
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);
788 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
790 sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
791 vlib_buffer_push_uninit (b, alloc_bytes);
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;
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);
804 vnet_buffer (b)->sctp.connection_index =
805 sctp_conn->sub_conn[idx].connection.c_index;
812 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn)
816 sctp_main_t *tm = vnet_get_sctp_main ();
817 vlib_main_t *vm = vlib_get_main ();
819 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
822 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
825 b = vlib_get_buffer (vm, bi);
826 sctp_init_buffer (vm, b);
827 sctp_prepare_shutdown_ack_chunk (sctp_conn, b);
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);
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;
840 * Convert buffer to SACK
843 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
845 vlib_main_t *vm = vlib_get_main ();
846 u8 idx = sctp_pick_conn_idx_on_chunk (SACK);
848 sctp_reuse_buffer (vm, b);
850 u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
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
855 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
857 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
859 sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
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);
868 sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
870 sctp_conn->ack_state = 0;
872 vnet_buffer (b)->sctp.connection_index =
873 sctp_conn->sub_conn[idx].connection.c_index;
877 * Convert buffer to HEARTBEAT
880 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn,
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);
887 b = sctp_reuse_buffer (vm, b);
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
892 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
894 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
896 sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
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));
907 vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
908 vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
910 vnet_buffer (b)->sctp.connection_index =
911 sctp_conn->sub_conn[idx].connection.c_index;
915 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
919 sctp_main_t *tm = vnet_get_sctp_main ();
920 vlib_main_t *vm = vlib_get_main ();
922 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
925 b = vlib_get_buffer (vm, bi);
926 sctp_init_buffer (vm, b);
927 sctp_prepare_heartbeat_chunk (sctp_conn, b);
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);
936 * Convert buffer to SHUTDOWN_COMPLETE
939 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn,
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);
946 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
948 sctp_shutdown_complete_chunk_t *shutdown_complete =
949 vlib_buffer_push_uninit (b, alloc_bytes);
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;
959 vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
960 vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
962 vnet_buffer (b)->sctp.connection_index =
963 sctp_conn->sub_conn[idx].connection.c_index;
967 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn)
971 sctp_main_t *tm = vnet_get_sctp_main ();
972 vlib_main_t *vm = vlib_get_main ();
974 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
977 b = vlib_get_buffer (vm, bi);
978 sctp_init_buffer (vm, b);
979 sctp_prepare_shutdown_complete_chunk (sctp_conn, b);
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);
986 sctp_conn->state = SCTP_STATE_CLOSED;
994 sctp_send_init (sctp_connection_t * sctp_conn)
998 sctp_main_t *tm = vnet_get_sctp_main ();
999 vlib_main_t *vm = vlib_get_main ();
1001 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1004 b = vlib_get_buffer (vm, bi);
1005 u8 idx = sctp_pick_conn_idx_on_chunk (INIT);
1007 sctp_init_buffer (vm, b);
1008 sctp_prepare_init_chunk (sctp_conn, b);
1010 /* Measure RTT with this */
1011 sctp_conn->rtt_ts = sctp_time_now ();
1012 sctp_conn->rtt_seq = sctp_conn->next_tsn;
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);
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;
1025 sctp_in_cong_recovery (sctp_connection_t * sctp_conn)
1031 * Push SCTP header and update connection variables
1034 sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
1035 sctp_state_t next_state)
1037 u8 idx = sctp_pick_conn_idx_on_chunk (DATA);
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));
1044 SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1045 "b->current_data = %p "
1047 b->current_length, b->current_data, data_len);
1049 u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1050 u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1052 bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1054 sctp_payload_data_chunk_t *data_chunk =
1055 vlib_buffer_push_uninit (b, bytes_to_add);
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;
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);
1068 vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1069 vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1071 SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1072 b->data, b->current_data);
1074 sctp_conn->next_tsn += data_len;
1076 vnet_buffer (b)->sctp.connection_index =
1077 sctp_conn->sub_conn[idx].connection.c_index;
1081 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
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);
1087 if (sctp_conn->rtt_ts == 0 && !sctp_in_cong_recovery (sctp_conn))
1089 sctp_conn->rtt_ts = sctp_time_now ();
1090 sctp_conn->rtt_seq = sctp_conn->next_tsn;
1092 sctp_trajectory_add_start (b0, 3);
1099 sctp46_output_inline (vlib_main_t * vm,
1100 vlib_node_runtime_t * node,
1101 vlib_frame_t * from_frame, int is_ip4)
1103 u32 n_left_from, next_index, *from, *to_next;
1104 u32 my_thread_index = vm->thread_index;
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);
1111 while (n_left_from > 0)
1115 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1117 while (n_left_from > 0 && n_left_to_next > 0)
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;
1128 #if SCTP_DEBUG_STATE_MACHINE
1129 u16 packet_length = 0;
1137 n_left_to_next -= 1;
1139 b0 = vlib_get_buffer (vm, bi0);
1141 sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1144 if (PREDICT_FALSE (sctp_conn == 0))
1146 error0 = SCTP_ERROR_INVALID_CONNECTION;
1147 next0 = SCTP_OUTPUT_NEXT_DROP;
1151 u8 idx = sctp_pick_conn_idx_on_state (sctp_conn->state);
1153 th0 = vlib_buffer_get_current (b0);
1157 ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1159 &sctp_conn->sub_conn
1166 IP_PROTOCOL_SCTP, 1);
1168 u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1170 sctp_hdr = ip4_next_header (iph4);
1171 sctp_hdr->checksum = checksum;
1173 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1175 #if SCTP_DEBUG_STATE_MACHINE
1176 packet_length = clib_net_to_host_u16 (iph4->length);
1181 ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1183 &sctp_conn->sub_conn
1187 &sctp_conn->sub_conn
1194 u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1197 sctp_hdr = ip6_next_header (iph6);
1198 sctp_hdr->checksum = checksum;
1200 vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1201 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1203 #if SCTP_DEBUG_STATE_MACHINE
1204 packet_length = clib_net_to_host_u16 (iph6->payload_length);
1209 (sctp_conn->sub_conn[idx].connection.lcl_port ==
1211 || sctp_conn->sub_conn[idx].connection.lcl_port ==
1213 && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1215 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1216 sctp_hdr->src_port);
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);
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",
1229 [idx].connection.c_index, packet_length,
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);
1238 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1239 next0 = SCTP_OUTPUT_NEXT_DROP;
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);
1251 if (chunk_type == DATA)
1252 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1254 /* Let's make sure the state-machine does not send anything crazy */
1255 switch (sctp_conn->state)
1257 case SCTP_STATE_CLOSED:
1259 if (chunk_type != INIT && chunk_type != INIT_ACK)
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));
1266 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1267 next0 = SCTP_OUTPUT_NEXT_DROP;
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)
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));
1282 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1283 next0 = SCTP_OUTPUT_NEXT_DROP;
1287 case SCTP_STATE_COOKIE_WAIT:
1288 if (chunk_type != COOKIE_ECHO)
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));
1295 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1296 next0 = SCTP_OUTPUT_NEXT_DROP;
1300 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
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));
1310 if (chunk_type == SHUTDOWN)
1312 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1313 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1315 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1318 if (chunk_type == DATA)
1320 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1324 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1325 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1327 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1329 SCTP_DBG_STATE_MACHINE ("CONNECTION_INDEX = %u, "
1332 sctp_conn->sub_conn[idx].connection.c_index,
1333 sctp_state_to_string (sctp_conn->state),
1334 sctp_chunk_to_string (chunk_type));
1336 vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1339 b0->error = node->errors[error0];
1340 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1342 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1345 clib_memcpy (&t0->sctp_header, th0,
1346 sizeof (t0->sctp_header));
1350 memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1352 clib_memcpy (&t0->sctp_connection, sctp_conn,
1353 sizeof (t0->sctp_connection));
1356 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1357 n_left_to_next, bi0, next0);
1360 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1363 return from_frame->n_vectors;
1367 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1368 vlib_frame_t * from_frame)
1370 return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1374 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1375 vlib_frame_t * from_frame)
1377 return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1381 VLIB_REGISTER_NODE (sctp4_output_node) =
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,
1390 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1391 foreach_sctp4_output_next
1394 .format_buffer = format_sctp_header,
1395 .format_trace = format_sctp_tx_trace,
1399 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1402 VLIB_REGISTER_NODE (sctp6_output_node) =
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,
1412 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1413 foreach_sctp6_output_next
1416 .format_buffer = format_sctp_header,
1417 .format_trace = format_sctp_tx_trace,
1421 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1424 * fd.io coding-style-patch-verification: ON
1427 * eval: (c-set-style "gnu")