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>
18 #include <openssl/hmac.h>
20 vlib_node_registration_t sctp4_output_node;
21 vlib_node_registration_t sctp6_output_node;
23 typedef enum _sctp_output_next
25 SCTP_OUTPUT_NEXT_DROP,
26 SCTP_OUTPUT_NEXT_IP_LOOKUP,
30 #define foreach_sctp4_output_next \
31 _ (DROP, "error-drop") \
32 _ (IP_LOOKUP, "ip4-lookup")
34 #define foreach_sctp6_output_next \
35 _ (DROP, "error-drop") \
36 _ (IP_LOOKUP, "ip6-lookup")
38 static char *sctp_error_strings[] = {
39 #define sctp_error(n,s) s,
40 #include <vnet/sctp/sctp_error.def>
46 sctp_header_t sctp_header;
47 sctp_connection_t sctp_connection;
51 * Flush tx frame populated by retransmits and timer pops
54 sctp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
56 if (sctp_main.tx_frames[!is_ip4][thread_index])
59 next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
60 vlib_put_frame_to_node (vm, next_index,
61 sctp_main.tx_frames[!is_ip4][thread_index]);
62 sctp_main.tx_frames[!is_ip4][thread_index] = 0;
67 * Flush ip lookup tx frames populated by timer pops
70 sctp_flush_frame_to_ip_lookup (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
72 if (sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index])
75 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
76 vlib_put_frame_to_node (vm, next_index,
77 sctp_main.ip_lookup_tx_frames[!is_ip4]
79 sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
84 * Flush v4 and v6 sctp and ip-lookup tx frames for thread index
87 sctp_flush_frames_to_output (u8 thread_index)
89 vlib_main_t *vm = vlib_get_main ();
90 sctp_flush_frame_to_output (vm, thread_index, 1);
91 sctp_flush_frame_to_output (vm, thread_index, 0);
92 sctp_flush_frame_to_ip_lookup (vm, thread_index, 1);
93 sctp_flush_frame_to_ip_lookup (vm, thread_index, 0);
97 ip4_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
101 u32 ip_header_length, payload_length_host_byte_order;
102 u32 n_this_buffer, n_bytes_left, n_ip_bytes_this_buffer;
103 void *data_this_buffer;
105 /* Initialize checksum with ip header. */
106 ip_header_length = ip4_header_bytes (ip0);
107 payload_length_host_byte_order =
108 clib_net_to_host_u16 (ip0->length) - ip_header_length;
110 clib_host_to_net_u32 (payload_length_host_byte_order +
111 (ip0->protocol << 16));
113 if (BITS (uword) == 32)
116 ip_csum_with_carry (checksum,
117 clib_mem_unaligned (&ip0->src_address, u32));
119 ip_csum_with_carry (checksum,
120 clib_mem_unaligned (&ip0->dst_address, u32));
124 ip_csum_with_carry (checksum,
125 clib_mem_unaligned (&ip0->src_address, u64));
127 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
128 data_this_buffer = (void *) ip0 + ip_header_length;
129 n_ip_bytes_this_buffer =
130 p0->current_length - (((u8 *) ip0 - p0->data) - p0->current_data);
131 if (n_this_buffer + ip_header_length > n_ip_bytes_this_buffer)
133 n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
134 n_ip_bytes_this_buffer - ip_header_length : 0;
139 ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
140 n_bytes_left -= n_this_buffer;
141 if (n_bytes_left == 0)
144 ASSERT (p0->flags & VLIB_BUFFER_NEXT_PRESENT);
145 p0 = vlib_get_buffer (vm, p0->next_buffer);
146 data_this_buffer = vlib_buffer_get_current (p0);
147 n_this_buffer = p0->current_length;
154 ip6_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
155 ip6_header_t * ip0, int *bogus_lengthp)
158 u16 payload_length_host_byte_order;
159 u32 i, n_this_buffer, n_bytes_left;
160 u32 headers_size = sizeof (ip0[0]);
161 void *data_this_buffer;
163 ASSERT (bogus_lengthp);
166 /* Initialize checksum with ip header. */
167 checksum = ip0->payload_length + clib_host_to_net_u16 (ip0->protocol);
168 payload_length_host_byte_order = clib_net_to_host_u16 (ip0->payload_length);
169 data_this_buffer = (void *) (ip0 + 1);
171 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
173 checksum = ip_csum_with_carry (checksum,
174 clib_mem_unaligned (&ip0->
178 ip_csum_with_carry (checksum,
179 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
183 /* some icmp packets may come with a "router alert" hop-by-hop extension header (e.g., mldv2 packets)
184 * or UDP-Ping packets */
185 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS))
188 ip6_hop_by_hop_ext_t *ext_hdr =
189 (ip6_hop_by_hop_ext_t *) data_this_buffer;
191 /* validate really icmp6 next */
192 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_SCTP));
194 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
195 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
197 payload_length_host_byte_order -= skip_bytes;
198 headers_size += skip_bytes;
201 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
202 if (p0 && n_this_buffer + headers_size > p0->current_length)
205 headers_size ? p0->current_length - headers_size : 0;
209 ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
210 n_bytes_left -= n_this_buffer;
211 if (n_bytes_left == 0)
214 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
219 p0 = vlib_get_buffer (vm, p0->next_buffer);
220 data_this_buffer = vlib_buffer_get_current (p0);
221 n_this_buffer = p0->current_length;
228 sctp_push_ip_hdr (sctp_main_t * tm, sctp_sub_connection_t * sctp_sub_conn,
231 sctp_header_t *th = vlib_buffer_get_current (b);
232 vlib_main_t *vm = vlib_get_main ();
233 if (sctp_sub_conn->c_is_ip4)
236 ih = vlib_buffer_push_ip4 (vm, b, &sctp_sub_conn->c_lcl_ip4,
237 &sctp_sub_conn->c_rmt_ip4, IP_PROTOCOL_SCTP,
239 th->checksum = ip4_sctp_compute_checksum (vm, b, ih);
246 ih = vlib_buffer_push_ip6 (vm, b, &sctp_sub_conn->c_lcl_ip6,
247 &sctp_sub_conn->c_rmt_ip6, IP_PROTOCOL_SCTP);
248 th->checksum = ip6_sctp_compute_checksum (vm, b, ih, &bogus);
254 sctp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
256 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
257 vlib_buffer_free_one (vm, b->next_buffer);
258 /* Zero all flags but free list index and trace flag */
259 b->flags &= VLIB_BUFFER_NEXT_PRESENT - 1;
261 b->current_length = 0;
262 b->total_length_not_including_first_buffer = 0;
263 vnet_buffer (b)->sctp.flags = 0;
265 /* Leave enough space for headers */
266 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
270 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
272 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
273 b->flags &= VLIB_BUFFER_FREE_LIST_INDEX_MASK;
274 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
275 b->total_length_not_including_first_buffer = 0;
276 vnet_buffer (b)->sctp.flags = 0;
277 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
278 /* Leave enough space for headers */
279 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
283 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
285 vlib_main_t *vm = vlib_get_main ();
286 u32 current_length = vec_len (tm->tx_buffers[thread_index]);
289 vec_validate (tm->tx_buffers[thread_index],
290 current_length + n_free_buffers - 1);
292 vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
294 _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
295 /* buffer shortage, report failure */
296 if (vec_len (tm->tx_buffers[thread_index]) == 0)
298 clib_warning ("out of buffers");
305 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
308 u32 thread_index = vlib_get_thread_index ();
309 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
311 if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
314 my_tx_buffers = tm->tx_buffers[thread_index];
315 *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
316 _vec_len (my_tx_buffers) -= 1;
321 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
324 sctp_main_t *tm = vnet_get_sctp_main ();
325 u32 thread_index = vlib_get_thread_index ();
326 u32 *to_next, next_index;
329 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
332 /* Decide where to send the packet */
333 next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
334 sctp_trajectory_add_start (b, 2);
336 /* Get frame to v4/6 output node */
337 f = tm->tx_frames[!is_ip4][thread_index];
340 f = vlib_get_frame_to_node (vm, next_index);
342 tm->tx_frames[!is_ip4][thread_index] = f;
344 to_next = vlib_frame_vector_args (f);
345 to_next[f->n_vectors] = bi;
347 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
349 vlib_put_frame_to_node (vm, next_index, f);
350 tm->tx_frames[!is_ip4][thread_index] = 0;
355 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
358 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
362 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
365 sctp_main_t *tm = vnet_get_sctp_main ();
366 u32 thread_index = vlib_get_thread_index ();
367 u32 *to_next, next_index;
370 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
373 /* Default FIB for now */
374 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
376 /* Send to IP lookup */
377 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
378 if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
381 b->pre_data[1] = next_index;
384 f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
387 f = vlib_get_frame_to_node (vm, next_index);
389 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
392 to_next = vlib_frame_vector_args (f);
393 to_next[f->n_vectors] = bi;
395 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
397 vlib_put_frame_to_node (vm, next_index, f);
398 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
403 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
406 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
410 sctp_enqueue_to_ip_lookup_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
413 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 1);
417 * Convert buffer to INIT
420 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
422 u32 random_seed = random_default_seed ();
423 u16 alloc_bytes = sizeof (sctp_init_chunk_t);
424 sctp_sub_connection_t *sub_conn =
425 &sctp_conn->sub_conn[sctp_pick_conn_idx_on_chunk (INIT)];
427 sctp_ipv4_addr_param_t *ip4_param = 0;
428 sctp_ipv6_addr_param_t *ip6_param = 0;
430 if (sub_conn->c_is_ip4)
431 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
433 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
435 /* As per RFC 4960 the chunk_length value does NOT contemplate
436 * the size of the first header (see sctp_header_t) and any padding
438 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
440 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
442 sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
444 u16 pointer_offset = sizeof (init_chunk);
445 if (sub_conn->c_is_ip4)
447 ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
448 ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
450 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
454 ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
455 ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
456 ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
458 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
461 init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
462 init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
463 init_chunk->sctp_hdr.checksum = 0;
464 /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
465 init_chunk->sctp_hdr.verification_tag = 0x0;
467 vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
468 vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
469 vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
471 init_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
472 init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
473 init_chunk->inboud_streams_count =
474 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
475 init_chunk->outbound_streams_count =
476 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
478 init_chunk->initial_tsn =
479 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
480 SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
481 sctp_conn->local_initial_tsn);
483 sctp_conn->local_tag = init_chunk->initiate_tag;
485 vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
487 SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
489 "SRC_PORT = %u, DST_PORT = %u",
490 sub_conn->connection.c_index,
492 sctp_state_to_string (sctp_conn->state),
493 sctp_chunk_to_string (INIT),
494 init_chunk->sctp_hdr.src_port,
495 init_chunk->sctp_hdr.dst_port);
499 sctp_compute_mac (sctp_connection_t * sctp_conn,
500 sctp_state_cookie_param_t * state_cookie)
502 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
506 const EVP_MD *md = EVP_sha1 ();
508 unsigned int len = 0;
510 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
511 ctx = HMAC_CTX_new ();
512 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
513 sizeof (state_cookie->creation_time), md, NULL);
514 HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
515 HMAC_Final (ctx, state_cookie->mac, &len);
517 HMAC_CTX_init (&ctx);
518 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
519 sizeof (state_cookie->creation_time), md, NULL);
521 HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
522 HMAC_Final (&ctx, state_cookie->mac, &len);
523 HMAC_CTX_cleanup (&ctx);
526 ENDIANESS_SWAP (state_cookie->mac);
530 sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn,
533 vlib_main_t *vm = vlib_get_main ();
534 u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ACK);
536 sctp_reuse_buffer (vm, b);
538 u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
540 /* As per RFC 4960 the chunk_length value does NOT contemplate
541 * the size of the first header (see sctp_header_t) and any padding
543 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
545 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
547 sctp_cookie_ack_chunk_t *cookie_ack_chunk =
548 vlib_buffer_push_uninit (b, alloc_bytes);
550 cookie_ack_chunk->sctp_hdr.checksum = 0;
551 cookie_ack_chunk->sctp_hdr.src_port =
552 sctp_conn->sub_conn[idx].connection.lcl_port;
553 cookie_ack_chunk->sctp_hdr.dst_port =
554 sctp_conn->sub_conn[idx].connection.rmt_port;
555 cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
556 vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
557 vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
559 /* Measure RTT with this */
560 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
562 vnet_buffer (b)->sctp.connection_index =
563 sctp_conn->sub_conn[idx].connection.c_index;
567 sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn,
569 sctp_state_cookie_param_t * sc)
571 vlib_main_t *vm = vlib_get_main ();
572 u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ECHO);
574 sctp_reuse_buffer (vm, b);
576 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
577 u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
578 /* As per RFC 4960 the chunk_length value does NOT contemplate
579 * the size of the first header (see sctp_header_t) and any padding
581 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
582 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
583 sctp_cookie_echo_chunk_t *cookie_echo_chunk =
584 vlib_buffer_push_uninit (b, alloc_bytes);
585 cookie_echo_chunk->sctp_hdr.checksum = 0;
586 cookie_echo_chunk->sctp_hdr.src_port =
587 sctp_conn->sub_conn[idx].connection.lcl_port;
588 cookie_echo_chunk->sctp_hdr.dst_port =
589 sctp_conn->sub_conn[idx].connection.rmt_port;
590 cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
591 vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
592 vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
593 clib_memcpy (&(cookie_echo_chunk->cookie), sc,
594 sizeof (sctp_state_cookie_param_t));
596 /* Measure RTT with this */
597 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
599 vnet_buffer (b)->sctp.connection_index =
600 sctp_conn->sub_conn[idx].connection.c_index;
604 * Convert buffer to INIT-ACK
607 sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
608 ip4_address_t * ip4_addr,
609 ip6_address_t * ip6_addr)
611 vlib_main_t *vm = vlib_get_main ();
612 sctp_ipv4_addr_param_t *ip4_param = 0;
613 sctp_ipv6_addr_param_t *ip6_param = 0;
614 u8 idx = sctp_pick_conn_idx_on_chunk (INIT_ACK);
615 u32 random_seed = random_default_seed ();
617 sctp_reuse_buffer (vm, b);
619 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
621 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
623 if (PREDICT_TRUE (ip4_addr != NULL))
625 /* Create room for variable-length fields in the INIT_ACK chunk */
626 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
628 if (PREDICT_TRUE (ip6_addr != NULL))
630 /* Create room for variable-length fields in the INIT_ACK chunk */
631 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
634 if (sctp_conn->sub_conn[idx].connection.is_ip4)
635 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
637 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
639 /* As per RFC 4960 the chunk_length value does NOT contemplate
640 * the size of the first header (see sctp_header_t) and any padding
642 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
644 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
646 sctp_init_ack_chunk_t *init_ack_chunk =
647 vlib_buffer_push_uninit (b, alloc_bytes);
649 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
651 /* Create State Cookie parameter */
652 sctp_state_cookie_param_t *state_cookie_param =
653 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
655 state_cookie_param->param_hdr.type =
656 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
657 state_cookie_param->param_hdr.length =
658 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
659 state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
660 state_cookie_param->cookie_lifespan =
661 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
663 sctp_compute_mac (sctp_conn, state_cookie_param);
665 pointer_offset += sizeof (sctp_state_cookie_param_t);
667 if (PREDICT_TRUE (ip4_addr != NULL))
669 sctp_ipv4_addr_param_t *ipv4_addr =
670 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
672 ipv4_addr->param_hdr.type =
673 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
674 ipv4_addr->param_hdr.length =
675 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
676 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
678 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
680 if (PREDICT_TRUE (ip6_addr != NULL))
682 sctp_ipv6_addr_param_t *ipv6_addr =
683 (sctp_ipv6_addr_param_t *) init_ack_chunk +
684 sizeof (sctp_init_chunk_t) + pointer_offset;
686 ipv6_addr->param_hdr.type =
687 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
688 ipv6_addr->param_hdr.length =
689 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
690 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
691 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
693 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
696 if (sctp_conn->sub_conn[idx].connection.is_ip4)
698 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
699 ip4_param->address.as_u32 =
700 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
702 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
706 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
707 ip6_param->address.as_u64[0] =
708 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
709 ip6_param->address.as_u64[1] =
710 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
712 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
715 /* src_port & dst_port are already in network byte-order */
716 init_ack_chunk->sctp_hdr.checksum = 0;
717 init_ack_chunk->sctp_hdr.src_port =
718 sctp_conn->sub_conn[idx].connection.lcl_port;
719 init_ack_chunk->sctp_hdr.dst_port =
720 sctp_conn->sub_conn[idx].connection.rmt_port;
721 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
722 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
723 init_ack_chunk->initial_tsn =
724 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
725 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
726 init_ack_chunk->initial_tsn);
728 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
729 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
731 init_ack_chunk->initiate_tag =
732 clib_host_to_net_u32 (random_u32 (&random_seed));
734 init_ack_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
735 init_ack_chunk->inboud_streams_count =
736 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
737 init_ack_chunk->outbound_streams_count =
738 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
740 sctp_conn->local_tag = init_ack_chunk->initiate_tag;
742 /* Measure RTT with this */
743 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
745 vnet_buffer (b)->sctp.connection_index =
746 sctp_conn->sub_conn[idx].connection.c_index;
750 * Convert buffer to SHUTDOWN
753 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
755 vlib_main_t *vm = vlib_get_main ();
756 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
757 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
759 b = sctp_reuse_buffer (vm, b);
761 /* As per RFC 4960 the chunk_length value does NOT contemplate
762 * the size of the first header (see sctp_header_t) and any padding
764 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
766 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
768 sctp_shutdown_association_chunk_t *shutdown_chunk =
769 vlib_buffer_push_uninit (b, alloc_bytes);
771 shutdown_chunk->sctp_hdr.checksum = 0;
772 /* No need of host_to_net conversion, already in net-byte order */
773 shutdown_chunk->sctp_hdr.src_port =
774 sctp_conn->sub_conn[idx].connection.lcl_port;
775 shutdown_chunk->sctp_hdr.dst_port =
776 sctp_conn->sub_conn[idx].connection.rmt_port;
777 shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
778 vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
779 vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
781 shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
783 vnet_buffer (b)->sctp.connection_index =
784 sctp_conn->sub_conn[idx].connection.c_index;
791 sctp_send_shutdown (sctp_connection_t * sctp_conn)
795 sctp_main_t *tm = vnet_get_sctp_main ();
796 vlib_main_t *vm = vlib_get_main ();
798 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
801 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
804 b = vlib_get_buffer (vm, bi);
805 sctp_init_buffer (vm, b);
806 sctp_prepare_shutdown_chunk (sctp_conn, b);
808 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
809 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
810 sctp_enqueue_to_output_now (vm, b, bi,
811 sctp_conn->sub_conn[idx].connection.is_ip4);
813 /* Measure RTT with this */
814 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
818 * Convert buffer to SHUTDOWN_ACK
821 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn,
824 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
825 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
826 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
828 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
830 sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
831 vlib_buffer_push_uninit (b, alloc_bytes);
833 shutdown_ack_chunk->sctp_hdr.checksum = 0;
834 /* No need of host_to_net conversion, already in net-byte order */
835 shutdown_ack_chunk->sctp_hdr.src_port =
836 sctp_conn->sub_conn[idx].connection.lcl_port;
837 shutdown_ack_chunk->sctp_hdr.dst_port =
838 sctp_conn->sub_conn[idx].connection.rmt_port;
839 shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
841 vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
842 vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
844 vnet_buffer (b)->sctp.connection_index =
845 sctp_conn->sub_conn[idx].connection.c_index;
852 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn)
856 sctp_main_t *tm = vnet_get_sctp_main ();
857 vlib_main_t *vm = vlib_get_main ();
859 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
862 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
865 b = vlib_get_buffer (vm, bi);
866 sctp_init_buffer (vm, b);
867 sctp_prepare_shutdown_ack_chunk (sctp_conn, b);
869 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
870 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
871 sctp_enqueue_to_ip_lookup (vm, b, bi,
872 sctp_conn->sub_conn[idx].connection.is_ip4);
874 /* Measure RTT with this */
875 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
877 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
878 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
879 sctp_conn->sub_conn[idx].RTO);
880 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
884 * Convert buffer to SACK
887 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
889 vlib_main_t *vm = vlib_get_main ();
890 u8 idx = sctp_pick_conn_idx_on_chunk (SACK);
892 sctp_reuse_buffer (vm, b);
894 u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
896 /* As per RFC 4960 the chunk_length value does NOT contemplate
897 * the size of the first header (see sctp_header_t) and any padding
899 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
901 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
903 sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
905 sack->sctp_hdr.checksum = 0;
906 sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
907 sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
908 sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
909 vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
910 vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
912 sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
914 sctp_conn->ack_state = 0;
916 vnet_buffer (b)->sctp.connection_index =
917 sctp_conn->sub_conn[idx].connection.c_index;
921 * Convert buffer to HEARTBEAT
924 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn,
927 vlib_main_t *vm = vlib_get_main ();
928 u8 idx = sctp_pick_conn_idx_on_chunk (HEARTBEAT);
929 u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
931 b = sctp_reuse_buffer (vm, b);
933 /* As per RFC 4960 the chunk_length value does NOT contemplate
934 * the size of the first header (see sctp_header_t) and any padding
936 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
938 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
940 sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
942 hb_req->sctp_hdr.checksum = 0;
943 /* No need of host_to_net conversion, already in net-byte order */
944 hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
945 hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
946 hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
947 hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
948 hb_req->hb_info.param_hdr.length =
949 clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
951 vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
952 vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
954 vnet_buffer (b)->sctp.connection_index =
955 sctp_conn->sub_conn[idx].connection.c_index;
959 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
963 sctp_main_t *tm = vnet_get_sctp_main ();
964 vlib_main_t *vm = vlib_get_main ();
966 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
969 b = vlib_get_buffer (vm, bi);
970 sctp_init_buffer (vm, b);
971 sctp_prepare_heartbeat_chunk (sctp_conn, b);
973 u8 idx = sctp_pick_conn_idx_on_state (SCTP_STATE_ESTABLISHED);
974 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
975 sctp_enqueue_to_ip_lookup (vm, b, bi,
976 sctp_conn->sub_conn[idx].connection.is_ip4);
980 * Convert buffer to SHUTDOWN_COMPLETE
983 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn,
986 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
987 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
988 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
990 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
992 sctp_shutdown_complete_chunk_t *shutdown_complete =
993 vlib_buffer_push_uninit (b, alloc_bytes);
995 shutdown_complete->sctp_hdr.checksum = 0;
996 /* No need of host_to_net conversion, already in net-byte order */
997 shutdown_complete->sctp_hdr.src_port =
998 sctp_conn->sub_conn[idx].connection.lcl_port;
999 shutdown_complete->sctp_hdr.dst_port =
1000 sctp_conn->sub_conn[idx].connection.rmt_port;
1001 shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1003 vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1004 vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1006 vnet_buffer (b)->sctp.connection_index =
1007 sctp_conn->sub_conn[idx].connection.c_index;
1011 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn)
1015 sctp_main_t *tm = vnet_get_sctp_main ();
1016 vlib_main_t *vm = vlib_get_main ();
1018 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1021 b = vlib_get_buffer (vm, bi);
1022 sctp_init_buffer (vm, b);
1023 sctp_prepare_shutdown_complete_chunk (sctp_conn, b);
1025 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
1026 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1027 sctp_enqueue_to_ip_lookup (vm, b, bi,
1028 sctp_conn->sub_conn[idx].connection.is_ip4);
1030 sctp_conn->state = SCTP_STATE_CLOSED;
1038 sctp_send_init (sctp_connection_t * sctp_conn)
1042 sctp_main_t *tm = vnet_get_sctp_main ();
1043 vlib_main_t *vm = vlib_get_main ();
1045 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1048 b = vlib_get_buffer (vm, bi);
1049 u8 idx = sctp_pick_conn_idx_on_chunk (INIT);
1051 sctp_init_buffer (vm, b);
1052 sctp_prepare_init_chunk (sctp_conn, b);
1054 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1055 sctp_enqueue_to_ip_lookup_now (vm, b, bi,
1056 sctp_conn->sub_conn[idx].c_is_ip4);
1058 /* Measure RTT with this */
1059 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1061 /* Start the T1_INIT timer */
1062 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1063 sctp_conn->sub_conn[idx].RTO);
1065 /* Change state to COOKIE_WAIT */
1066 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
1070 * Push SCTP header and update connection variables
1073 sctp_push_hdr_i (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b,
1074 sctp_state_t next_state)
1077 b->current_length + b->total_length_not_including_first_buffer;
1078 ASSERT (!b->total_length_not_including_first_buffer
1079 || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1081 SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1082 "b->current_data = %p "
1084 b->current_length, b->current_data, data_len);
1086 u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1087 u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1089 bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1091 sctp_payload_data_chunk_t *data_chunk =
1092 vlib_buffer_push_uninit (b, bytes_to_add);
1094 data_chunk->sctp_hdr.checksum = 0;
1095 data_chunk->sctp_hdr.src_port =
1096 sctp_conn->sub_conn[idx].connection.lcl_port;
1097 data_chunk->sctp_hdr.dst_port =
1098 sctp_conn->sub_conn[idx].connection.rmt_port;
1099 data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1101 data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
1102 data_chunk->stream_id = clib_host_to_net_u16 (0);
1103 data_chunk->stream_seq = clib_host_to_net_u16 (0);
1105 vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1106 vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1108 vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1109 vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1111 SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1112 b->data, b->current_data);
1114 sctp_conn->next_tsn += data_len;
1116 vnet_buffer (b)->sctp.connection_index =
1117 sctp_conn->sub_conn[idx].connection.c_index;
1121 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
1123 sctp_connection_t *sctp_conn =
1124 sctp_get_connection_from_transport (trans_conn);
1126 u8 idx = sctp_pick_conn_idx_on_chunk (DATA);
1128 sctp_push_hdr_i (sctp_conn, idx, b, SCTP_STATE_ESTABLISHED);
1130 if (sctp_conn->sub_conn[idx].RTO_pending == 0)
1132 sctp_conn->sub_conn[idx].RTO_pending = 1;
1133 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1136 sctp_trajectory_add_start (b0, 3);
1143 sctp46_output_inline (vlib_main_t * vm,
1144 vlib_node_runtime_t * node,
1145 vlib_frame_t * from_frame, int is_ip4)
1147 u32 n_left_from, next_index, *from, *to_next;
1148 u32 my_thread_index = vm->thread_index;
1150 from = vlib_frame_vector_args (from_frame);
1151 n_left_from = from_frame->n_vectors;
1152 next_index = node->cached_next_index;
1153 sctp_set_time_now (my_thread_index);
1155 while (n_left_from > 0)
1159 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1161 while (n_left_from > 0 && n_left_to_next > 0)
1165 sctp_header_t *sctp_hdr = 0;
1166 sctp_connection_t *sctp_conn;
1167 sctp_tx_trace_t *t0;
1168 sctp_header_t *th0 = 0;
1169 u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1170 SCTP_OUTPUT_NEXT_IP_LOOKUP;
1172 #if SCTP_DEBUG_STATE_MACHINE
1173 u16 packet_length = 0;
1181 n_left_to_next -= 1;
1183 b0 = vlib_get_buffer (vm, bi0);
1185 sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1188 if (PREDICT_FALSE (sctp_conn == 0))
1190 error0 = SCTP_ERROR_INVALID_CONNECTION;
1191 next0 = SCTP_OUTPUT_NEXT_DROP;
1195 u8 idx = sctp_pick_conn_idx_on_state (sctp_conn->state);
1197 th0 = vlib_buffer_get_current (b0);
1201 ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1203 &sctp_conn->sub_conn
1210 IP_PROTOCOL_SCTP, 1);
1212 u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1214 sctp_hdr = ip4_next_header (iph4);
1215 sctp_hdr->checksum = checksum;
1217 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1219 #if SCTP_DEBUG_STATE_MACHINE
1220 packet_length = clib_net_to_host_u16 (iph4->length);
1225 ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1227 &sctp_conn->sub_conn
1231 &sctp_conn->sub_conn
1238 u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1241 sctp_hdr = ip6_next_header (iph6);
1242 sctp_hdr->checksum = checksum;
1244 vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1245 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1247 #if SCTP_DEBUG_STATE_MACHINE
1248 packet_length = clib_net_to_host_u16 (iph6->payload_length);
1253 (sctp_conn->sub_conn[idx].connection.lcl_port ==
1255 || sctp_conn->sub_conn[idx].connection.lcl_port ==
1257 && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1259 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1260 sctp_hdr->src_port);
1262 sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1263 u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1267 SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1268 "packet_length = %u, "
1269 "chunk_type = %u [%s], "
1270 "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1271 "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1273 [idx].connection.c_index, packet_length,
1275 sctp_chunk_to_string (chunk_type),
1276 sctp_conn->sub_conn[idx].
1277 connection.lcl_port, sctp_hdr->src_port,
1278 sctp_conn->sub_conn[idx].
1279 connection.rmt_port,
1280 sctp_hdr->dst_port);
1282 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1283 next0 = SCTP_OUTPUT_NEXT_DROP;
1287 SCTP_DBG_STATE_MACHINE
1288 ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1289 "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1290 sctp_conn->sub_conn[idx].connection.c_index,
1291 sctp_conn->state, sctp_state_to_string (sctp_conn->state),
1292 sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1293 full_hdr->hdr.dst_port);
1295 if (chunk_type == DATA)
1296 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1298 /* Let's make sure the state-machine does not send anything crazy */
1299 switch (sctp_conn->state)
1301 case SCTP_STATE_CLOSED:
1303 if (chunk_type != INIT && chunk_type != INIT_ACK)
1305 SCTP_DBG_STATE_MACHINE
1306 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1307 sctp_chunk_to_string (chunk_type),
1308 sctp_state_to_string (sctp_conn->state));
1310 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1311 next0 = SCTP_OUTPUT_NEXT_DROP;
1316 case SCTP_STATE_ESTABLISHED:
1317 if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1318 chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1319 chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1321 SCTP_DBG_STATE_MACHINE
1322 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1323 sctp_chunk_to_string (chunk_type),
1324 sctp_state_to_string (sctp_conn->state));
1326 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1327 next0 = SCTP_OUTPUT_NEXT_DROP;
1331 case SCTP_STATE_COOKIE_WAIT:
1332 if (chunk_type != COOKIE_ECHO)
1334 SCTP_DBG_STATE_MACHINE
1335 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1336 sctp_chunk_to_string (chunk_type),
1337 sctp_state_to_string (sctp_conn->state));
1339 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1340 next0 = SCTP_OUTPUT_NEXT_DROP;
1344 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1347 SCTP_DBG_STATE_MACHINE
1348 ("Sending chunk (%s) based on state-machine status (%s)",
1349 sctp_chunk_to_string (chunk_type),
1350 sctp_state_to_string (sctp_conn->state));
1354 if (chunk_type == SHUTDOWN)
1356 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1357 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1358 sctp_conn->sub_conn[idx].RTO);
1359 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1362 if (chunk_type == DATA)
1364 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1365 sctp_conn->sub_conn[idx].RTO);
1368 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1369 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1371 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1373 SCTP_DBG_STATE_MACHINE ("CONNECTION_INDEX = %u, "
1376 sctp_conn->sub_conn[idx].connection.c_index,
1377 sctp_state_to_string (sctp_conn->state),
1378 sctp_chunk_to_string (chunk_type));
1380 vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1383 b0->error = node->errors[error0];
1384 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1386 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1389 clib_memcpy (&t0->sctp_header, th0,
1390 sizeof (t0->sctp_header));
1394 memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1396 clib_memcpy (&t0->sctp_connection, sctp_conn,
1397 sizeof (t0->sctp_connection));
1400 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1401 n_left_to_next, bi0, next0);
1404 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1407 return from_frame->n_vectors;
1411 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1412 vlib_frame_t * from_frame)
1414 return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1418 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1419 vlib_frame_t * from_frame)
1421 return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1425 VLIB_REGISTER_NODE (sctp4_output_node) =
1427 .function = sctp4_output,.name = "sctp4-output",
1428 /* Takes a vector of packets. */
1429 .vector_size = sizeof (u32),
1430 .n_errors = SCTP_N_ERROR,
1431 .error_strings = sctp_error_strings,
1432 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1434 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1435 foreach_sctp4_output_next
1438 .format_buffer = format_sctp_header,
1439 .format_trace = format_sctp_tx_trace,
1443 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1446 VLIB_REGISTER_NODE (sctp6_output_node) =
1448 .function = sctp6_output,
1449 .name = "sctp6-output",
1450 /* Takes a vector of packets. */
1451 .vector_size = sizeof (u32),
1452 .n_errors = SCTP_N_ERROR,
1453 .error_strings = sctp_error_strings,
1454 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1456 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1457 foreach_sctp6_output_next
1460 .format_buffer = format_sctp_header,
1461 .format_trace = format_sctp_tx_trace,
1465 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1468 * fd.io coding-style-patch-verification: ON
1471 * eval: (c-set-style "gnu")