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;
264 vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
266 /* Leave enough space for headers */
267 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
271 sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
273 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
274 b->flags &= VLIB_BUFFER_NON_DEFAULT_FREELIST;
275 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
276 b->total_length_not_including_first_buffer = 0;
277 vnet_buffer (b)->sctp.flags = 0;
278 vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
279 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
280 /* Leave enough space for headers */
281 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
285 sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
287 vlib_main_t *vm = vlib_get_main ();
288 u32 current_length = vec_len (tm->tx_buffers[thread_index]);
291 vec_validate (tm->tx_buffers[thread_index],
292 current_length + n_free_buffers - 1);
294 vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
296 _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
297 /* buffer shortage, report failure */
298 if (vec_len (tm->tx_buffers[thread_index]) == 0)
300 clib_warning ("out of buffers");
307 sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
310 u32 thread_index = vlib_get_thread_index ();
311 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
313 if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
316 my_tx_buffers = tm->tx_buffers[thread_index];
317 *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
318 _vec_len (my_tx_buffers) -= 1;
323 sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
326 sctp_main_t *tm = vnet_get_sctp_main ();
327 u32 thread_index = vlib_get_thread_index ();
328 u32 *to_next, next_index;
331 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
334 /* Decide where to send the packet */
335 next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
336 sctp_trajectory_add_start (b, 2);
338 /* Get frame to v4/6 output node */
339 f = tm->tx_frames[!is_ip4][thread_index];
342 f = vlib_get_frame_to_node (vm, next_index);
344 tm->tx_frames[!is_ip4][thread_index] = f;
346 to_next = vlib_frame_vector_args (f);
347 to_next[f->n_vectors] = bi;
349 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
351 vlib_put_frame_to_node (vm, next_index, f);
352 tm->tx_frames[!is_ip4][thread_index] = 0;
357 sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
360 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
364 sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
367 sctp_main_t *tm = vnet_get_sctp_main ();
368 u32 thread_index = vlib_get_thread_index ();
369 u32 *to_next, next_index;
372 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
375 /* Default FIB for now */
376 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
378 /* Send to IP lookup */
379 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
380 if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
383 b->pre_data[1] = next_index;
386 f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
389 f = vlib_get_frame_to_node (vm, next_index);
391 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
394 to_next = vlib_frame_vector_args (f);
395 to_next[f->n_vectors] = bi;
397 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
399 vlib_put_frame_to_node (vm, next_index, f);
400 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
405 sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
408 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
412 * Convert buffer to INIT
415 sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, u8 idx,
418 u32 random_seed = random_default_seed ();
419 u16 alloc_bytes = sizeof (sctp_init_chunk_t);
420 sctp_sub_connection_t *sub_conn = &sctp_conn->sub_conn[idx];
422 sctp_ipv4_addr_param_t *ip4_param = 0;
423 sctp_ipv6_addr_param_t *ip6_param = 0;
425 if (sub_conn->c_is_ip4)
426 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
428 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
430 /* As per RFC 4960 the chunk_length value does NOT contemplate
431 * the size of the first header (see sctp_header_t) and any padding
433 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
435 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
437 sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
439 u16 pointer_offset = sizeof (init_chunk);
440 if (sub_conn->c_is_ip4)
442 ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
443 ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
445 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
449 ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
450 ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
451 ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
453 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
456 init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
457 init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
458 init_chunk->sctp_hdr.checksum = 0;
459 /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
460 init_chunk->sctp_hdr.verification_tag = 0x0;
462 vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
463 vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
464 vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
466 init_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
467 init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
468 init_chunk->inboud_streams_count =
469 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
470 init_chunk->outbound_streams_count =
471 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
473 init_chunk->initial_tsn =
474 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
475 SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
476 sctp_conn->local_initial_tsn);
478 sctp_conn->local_tag = init_chunk->initiate_tag;
480 vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
481 vnet_buffer (b)->sctp.subconn_idx = idx;
483 SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
485 "SRC_PORT = %u, DST_PORT = %u",
486 sub_conn->connection.c_index,
488 sctp_state_to_string (sctp_conn->state),
489 sctp_chunk_to_string (INIT),
490 init_chunk->sctp_hdr.src_port,
491 init_chunk->sctp_hdr.dst_port);
495 sctp_compute_mac (sctp_connection_t * sctp_conn,
496 sctp_state_cookie_param_t * state_cookie)
498 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
503 unsigned int len = 0;
504 const EVP_MD *md = EVP_sha1 ();
505 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
506 ctx = HMAC_CTX_new ();
507 HMAC_Init_ex (ctx, &state_cookie->creation_time,
508 sizeof (state_cookie->creation_time), md, NULL);
509 HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
510 HMAC_Final (ctx, state_cookie->mac, &len);
512 HMAC_CTX_init (&ctx);
513 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
514 sizeof (state_cookie->creation_time), md, NULL);
515 HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
516 HMAC_Final (&ctx, state_cookie->mac, &len);
517 HMAC_CTX_cleanup (&ctx);
520 ENDIANESS_SWAP (state_cookie->mac);
524 sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
527 vlib_main_t *vm = vlib_get_main ();
529 sctp_reuse_buffer (vm, b);
531 u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
533 /* As per RFC 4960 the chunk_length value does NOT contemplate
534 * the size of the first header (see sctp_header_t) and any padding
536 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
538 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
540 sctp_cookie_ack_chunk_t *cookie_ack_chunk =
541 vlib_buffer_push_uninit (b, alloc_bytes);
543 cookie_ack_chunk->sctp_hdr.checksum = 0;
544 cookie_ack_chunk->sctp_hdr.src_port =
545 sctp_conn->sub_conn[idx].connection.lcl_port;
546 cookie_ack_chunk->sctp_hdr.dst_port =
547 sctp_conn->sub_conn[idx].connection.rmt_port;
548 cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
549 vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
550 vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
552 vnet_buffer (b)->sctp.connection_index =
553 sctp_conn->sub_conn[idx].connection.c_index;
554 vnet_buffer (b)->sctp.subconn_idx = idx;
558 sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn, u8 idx,
560 sctp_state_cookie_param_t * sc)
562 vlib_main_t *vm = vlib_get_main ();
564 sctp_reuse_buffer (vm, b);
566 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
567 u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
568 /* As per RFC 4960 the chunk_length value does NOT contemplate
569 * the size of the first header (see sctp_header_t) and any padding
571 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
572 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
573 sctp_cookie_echo_chunk_t *cookie_echo_chunk =
574 vlib_buffer_push_uninit (b, alloc_bytes);
575 cookie_echo_chunk->sctp_hdr.checksum = 0;
576 cookie_echo_chunk->sctp_hdr.src_port =
577 sctp_conn->sub_conn[idx].connection.lcl_port;
578 cookie_echo_chunk->sctp_hdr.dst_port =
579 sctp_conn->sub_conn[idx].connection.rmt_port;
580 cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
581 vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
582 vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
583 clib_memcpy (&(cookie_echo_chunk->cookie), sc,
584 sizeof (sctp_state_cookie_param_t));
586 vnet_buffer (b)->sctp.connection_index =
587 sctp_conn->sub_conn[idx].connection.c_index;
588 vnet_buffer (b)->sctp.subconn_idx = idx;
592 * Convert buffer to ABORT
596 sctp_prepare_operation_error (sctp_connection_t * sctp_conn, u8 idx,
597 vlib_buffer_t * b, ip4_address_t * ip4_addr,
598 ip6_address_t * ip6_addr)
600 vlib_main_t *vm = vlib_get_main ();
602 sctp_reuse_buffer (vm, b);
604 // The minimum size of the message is given by the sctp_operation_error_t
605 u16 alloc_bytes = sizeof (sctp_operation_error_t);
607 // As per RFC 4960 the chunk_length value does NOT contemplate
608 // the size of the first header (see sctp_header_t) and any padding
610 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
612 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
614 sctp_operation_error_t *err_chunk =
615 vlib_buffer_push_uninit (b, alloc_bytes);
617 // src_port & dst_port are already in network byte-order
618 err_chunk->sctp_hdr.checksum = 0;
619 err_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
620 err_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
621 // As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk
622 err_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
624 vnet_sctp_set_chunk_type (&err_chunk->chunk_hdr, OPERATION_ERROR);
625 vnet_sctp_set_chunk_length (&err_chunk->chunk_hdr, chunk_len);
627 vnet_buffer (b)->sctp.connection_index =
628 sctp_conn->sub_conn[idx].connection.c_index;
629 vnet_buffer (b)->sctp.subconn_idx = idx;
634 * Convert buffer to ABORT
637 sctp_prepare_abort_for_collision (sctp_connection_t * sctp_conn, u8 idx,
638 vlib_buffer_t * b, ip4_address_t * ip4_addr,
639 ip6_address_t * ip6_addr)
641 vlib_main_t *vm = vlib_get_main ();
643 sctp_reuse_buffer (vm, b);
645 /* The minimum size of the message is given by the sctp_abort_chunk_t */
646 u16 alloc_bytes = sizeof (sctp_abort_chunk_t);
648 /* As per RFC 4960 the chunk_length value does NOT contemplate
649 * the size of the first header (see sctp_header_t) and any padding
651 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
653 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
655 sctp_abort_chunk_t *abort_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
657 /* src_port & dst_port are already in network byte-order */
658 abort_chunk->sctp_hdr.checksum = 0;
659 abort_chunk->sctp_hdr.src_port =
660 sctp_conn->sub_conn[idx].connection.lcl_port;
661 abort_chunk->sctp_hdr.dst_port =
662 sctp_conn->sub_conn[idx].connection.rmt_port;
663 /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
664 abort_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
666 vnet_sctp_set_chunk_type (&abort_chunk->chunk_hdr, ABORT);
667 vnet_sctp_set_chunk_length (&abort_chunk->chunk_hdr, chunk_len);
669 vnet_buffer (b)->sctp.connection_index =
670 sctp_conn->sub_conn[idx].connection.c_index;
671 vnet_buffer (b)->sctp.subconn_idx = idx;
675 * Convert buffer to INIT-ACK
678 sctp_prepare_initack_chunk_for_collision (sctp_connection_t * sctp_conn,
679 u8 idx, vlib_buffer_t * b,
680 ip4_address_t * ip4_addr,
681 ip6_address_t * ip6_addr)
683 vlib_main_t *vm = vlib_get_main ();
684 sctp_ipv4_addr_param_t *ip4_param = 0;
685 sctp_ipv6_addr_param_t *ip6_param = 0;
687 sctp_reuse_buffer (vm, b);
689 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
691 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
693 if (PREDICT_TRUE (ip4_addr != NULL))
695 /* Create room for variable-length fields in the INIT_ACK chunk */
696 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
698 if (PREDICT_TRUE (ip6_addr != NULL))
700 /* Create room for variable-length fields in the INIT_ACK chunk */
701 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
704 if (sctp_conn->sub_conn[idx].connection.is_ip4)
705 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
707 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
709 /* As per RFC 4960 the chunk_length value does NOT contemplate
710 * the size of the first header (see sctp_header_t) and any padding
712 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
714 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
716 sctp_init_ack_chunk_t *init_ack_chunk =
717 vlib_buffer_push_uninit (b, alloc_bytes);
719 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
721 /* Create State Cookie parameter */
722 sctp_state_cookie_param_t *state_cookie_param =
723 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
725 state_cookie_param->param_hdr.type =
726 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
727 state_cookie_param->param_hdr.length =
728 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
729 state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
730 state_cookie_param->cookie_lifespan =
731 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
733 sctp_compute_mac (sctp_conn, state_cookie_param);
735 pointer_offset += sizeof (sctp_state_cookie_param_t);
737 if (PREDICT_TRUE (ip4_addr != NULL))
739 sctp_ipv4_addr_param_t *ipv4_addr =
740 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
742 ipv4_addr->param_hdr.type =
743 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
744 ipv4_addr->param_hdr.length =
745 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
746 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
748 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
750 if (PREDICT_TRUE (ip6_addr != NULL))
752 sctp_ipv6_addr_param_t *ipv6_addr =
753 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
755 ipv6_addr->param_hdr.type =
756 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
757 ipv6_addr->param_hdr.length =
758 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
759 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
760 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
762 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
765 if (sctp_conn->sub_conn[idx].connection.is_ip4)
767 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
768 ip4_param->address.as_u32 =
769 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
771 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
775 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
776 ip6_param->address.as_u64[0] =
777 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
778 ip6_param->address.as_u64[1] =
779 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
781 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
784 /* src_port & dst_port are already in network byte-order */
785 init_ack_chunk->sctp_hdr.checksum = 0;
786 init_ack_chunk->sctp_hdr.src_port =
787 sctp_conn->sub_conn[idx].connection.lcl_port;
788 init_ack_chunk->sctp_hdr.dst_port =
789 sctp_conn->sub_conn[idx].connection.rmt_port;
790 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
791 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
792 init_ack_chunk->initial_tsn =
793 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
794 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
795 init_ack_chunk->initial_tsn);
797 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
798 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
800 init_ack_chunk->initiate_tag = sctp_conn->local_tag;
802 init_ack_chunk->a_rwnd =
803 clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
804 init_ack_chunk->inboud_streams_count =
805 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
806 init_ack_chunk->outbound_streams_count =
807 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
809 vnet_buffer (b)->sctp.connection_index =
810 sctp_conn->sub_conn[idx].connection.c_index;
811 vnet_buffer (b)->sctp.subconn_idx = idx;
815 * Convert buffer to INIT-ACK
818 sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, u8 idx,
819 vlib_buffer_t * b, ip4_address_t * ip4_addr,
820 ip6_address_t * ip6_addr)
822 vlib_main_t *vm = vlib_get_main ();
823 sctp_ipv4_addr_param_t *ip4_param = 0;
824 sctp_ipv6_addr_param_t *ip6_param = 0;
825 u32 random_seed = random_default_seed ();
827 sctp_reuse_buffer (vm, b);
829 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
831 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
833 if (PREDICT_TRUE (ip4_addr != NULL))
835 /* Create room for variable-length fields in the INIT_ACK chunk */
836 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
838 if (PREDICT_TRUE (ip6_addr != NULL))
840 /* Create room for variable-length fields in the INIT_ACK chunk */
841 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
844 if (sctp_conn->sub_conn[idx].connection.is_ip4)
845 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
847 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
849 /* As per RFC 4960 the chunk_length value does NOT contemplate
850 * the size of the first header (see sctp_header_t) and any padding
852 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
854 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
856 sctp_init_ack_chunk_t *init_ack_chunk =
857 vlib_buffer_push_uninit (b, alloc_bytes);
859 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
861 /* Create State Cookie parameter */
862 sctp_state_cookie_param_t *state_cookie_param =
863 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
865 state_cookie_param->param_hdr.type =
866 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
867 state_cookie_param->param_hdr.length =
868 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
869 state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
870 state_cookie_param->cookie_lifespan =
871 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
873 sctp_compute_mac (sctp_conn, state_cookie_param);
875 pointer_offset += sizeof (sctp_state_cookie_param_t);
877 if (PREDICT_TRUE (ip4_addr != NULL))
879 sctp_ipv4_addr_param_t *ipv4_addr =
880 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
882 ipv4_addr->param_hdr.type =
883 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
884 ipv4_addr->param_hdr.length =
885 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
886 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
888 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
890 if (PREDICT_TRUE (ip6_addr != NULL))
892 sctp_ipv6_addr_param_t *ipv6_addr =
893 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
895 ipv6_addr->param_hdr.type =
896 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
897 ipv6_addr->param_hdr.length =
898 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
899 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
900 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
902 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
905 if (sctp_conn->sub_conn[idx].connection.is_ip4)
907 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
908 ip4_param->address.as_u32 =
909 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
911 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
915 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
916 ip6_param->address.as_u64[0] =
917 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
918 ip6_param->address.as_u64[1] =
919 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
921 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
924 /* src_port & dst_port are already in network byte-order */
925 init_ack_chunk->sctp_hdr.checksum = 0;
926 init_ack_chunk->sctp_hdr.src_port =
927 sctp_conn->sub_conn[idx].connection.lcl_port;
928 init_ack_chunk->sctp_hdr.dst_port =
929 sctp_conn->sub_conn[idx].connection.rmt_port;
930 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
931 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
932 init_ack_chunk->initial_tsn =
933 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
934 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
935 init_ack_chunk->initial_tsn);
937 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
938 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
940 init_ack_chunk->initiate_tag =
941 clib_host_to_net_u32 (random_u32 (&random_seed));
943 init_ack_chunk->a_rwnd =
944 clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
945 init_ack_chunk->inboud_streams_count =
946 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
947 init_ack_chunk->outbound_streams_count =
948 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
950 sctp_conn->local_tag = init_ack_chunk->initiate_tag;
952 vnet_buffer (b)->sctp.connection_index =
953 sctp_conn->sub_conn[idx].connection.c_index;
954 vnet_buffer (b)->sctp.subconn_idx = idx;
958 * Convert buffer to SHUTDOWN
961 sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, u8 idx,
964 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
966 /* As per RFC 4960 the chunk_length value does NOT contemplate
967 * the size of the first header (see sctp_header_t) and any padding
969 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
971 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
973 sctp_shutdown_association_chunk_t *shutdown_chunk =
974 vlib_buffer_push_uninit (b, alloc_bytes);
976 shutdown_chunk->sctp_hdr.checksum = 0;
977 /* No need of host_to_net conversion, already in net-byte order */
978 shutdown_chunk->sctp_hdr.src_port =
979 sctp_conn->sub_conn[idx].connection.lcl_port;
980 shutdown_chunk->sctp_hdr.dst_port =
981 sctp_conn->sub_conn[idx].connection.rmt_port;
982 shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
983 vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
984 vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
986 shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
988 vnet_buffer (b)->sctp.connection_index =
989 sctp_conn->sub_conn[idx].connection.c_index;
990 vnet_buffer (b)->sctp.subconn_idx = idx;
997 sctp_send_shutdown (sctp_connection_t * sctp_conn)
1001 sctp_main_t *tm = vnet_get_sctp_main ();
1002 vlib_main_t *vm = vlib_get_main ();
1004 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1007 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1010 u8 idx = MAIN_SCTP_SUB_CONN_IDX;
1012 b = vlib_get_buffer (vm, bi);
1013 sctp_init_buffer (vm, b);
1014 sctp_prepare_shutdown_chunk (sctp_conn, idx, b);
1016 sctp_enqueue_to_output_now (vm, b, bi,
1017 sctp_conn->sub_conn[idx].connection.is_ip4);
1021 * Convert buffer to SHUTDOWN_ACK
1024 sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1027 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1028 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1030 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1032 sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
1033 vlib_buffer_push_uninit (b, alloc_bytes);
1035 shutdown_ack_chunk->sctp_hdr.checksum = 0;
1036 /* No need of host_to_net conversion, already in net-byte order */
1037 shutdown_ack_chunk->sctp_hdr.src_port =
1038 sctp_conn->sub_conn[idx].connection.lcl_port;
1039 shutdown_ack_chunk->sctp_hdr.dst_port =
1040 sctp_conn->sub_conn[idx].connection.rmt_port;
1041 shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1043 vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
1044 vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
1046 vnet_buffer (b)->sctp.connection_index =
1047 sctp_conn->sub_conn[idx].connection.c_index;
1048 vnet_buffer (b)->sctp.subconn_idx = idx;
1055 sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, u8 idx,
1058 vlib_main_t *vm = vlib_get_main ();
1060 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1063 sctp_reuse_buffer (vm, b);
1065 sctp_prepare_shutdown_ack_chunk (sctp_conn, idx, b);
1069 * Convert buffer to SACK
1072 sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1075 vlib_main_t *vm = vlib_get_main ();
1077 sctp_reuse_buffer (vm, b);
1079 u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
1081 /* As per RFC 4960 the chunk_length value does NOT contemplate
1082 * the size of the first header (see sctp_header_t) and any padding
1084 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1086 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1088 sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
1090 sack->sctp_hdr.checksum = 0;
1091 sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1092 sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1093 sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1094 vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
1095 vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
1097 sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
1099 sctp_conn->ack_state = 0;
1101 vnet_buffer (b)->sctp.connection_index =
1102 sctp_conn->sub_conn[idx].connection.c_index;
1103 vnet_buffer (b)->sctp.subconn_idx = idx;
1107 * Convert buffer to HEARTBEAT_ACK
1110 sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1113 vlib_main_t *vm = vlib_get_main ();
1115 u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
1117 sctp_reuse_buffer (vm, b);
1119 /* As per RFC 4960 the chunk_length value does NOT contemplate
1120 * the size of the first header (see sctp_header_t) and any padding
1122 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1124 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1126 sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
1128 hb_ack->sctp_hdr.checksum = 0;
1129 /* No need of host_to_net conversion, already in net-byte order */
1130 hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1131 hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1132 hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1133 hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1134 hb_ack->hb_info.param_hdr.length =
1135 clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
1137 vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
1138 vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
1140 vnet_buffer (b)->sctp.connection_index =
1141 sctp_conn->sub_conn[idx].connection.c_index;
1142 vnet_buffer (b)->sctp.subconn_idx = idx;
1146 * Convert buffer to HEARTBEAT
1149 sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn, u8 idx,
1152 u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
1154 /* As per RFC 4960 the chunk_length value does NOT contemplate
1155 * the size of the first header (see sctp_header_t) and any padding
1157 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1159 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1161 sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
1163 hb_req->sctp_hdr.checksum = 0;
1164 /* No need of host_to_net conversion, already in net-byte order */
1165 hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1166 hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1167 hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1168 hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1169 hb_req->hb_info.param_hdr.length =
1170 clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
1172 vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
1173 vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
1175 vnet_buffer (b)->sctp.connection_index =
1176 sctp_conn->sub_conn[idx].connection.c_index;
1177 vnet_buffer (b)->sctp.subconn_idx = idx;
1181 sctp_send_heartbeat (sctp_connection_t * sctp_conn)
1185 sctp_main_t *tm = vnet_get_sctp_main ();
1186 vlib_main_t *vm = vlib_get_main ();
1189 u32 now = sctp_time_now ();
1191 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
1193 if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
1196 if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
1198 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1201 b = vlib_get_buffer (vm, bi);
1202 sctp_init_buffer (vm, b);
1203 sctp_prepare_heartbeat_chunk (sctp_conn, i, b);
1205 sctp_enqueue_to_output_now (vm, b, bi,
1206 sctp_conn->sub_conn[i].
1209 sctp_conn->sub_conn[i].unacknowledged_hb += 1;
1215 * Convert buffer to SHUTDOWN_COMPLETE
1218 sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn, u8 idx,
1221 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1222 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1224 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1226 sctp_shutdown_complete_chunk_t *shutdown_complete =
1227 vlib_buffer_push_uninit (b, alloc_bytes);
1229 shutdown_complete->sctp_hdr.checksum = 0;
1230 /* No need of host_to_net conversion, already in net-byte order */
1231 shutdown_complete->sctp_hdr.src_port =
1232 sctp_conn->sub_conn[idx].connection.lcl_port;
1233 shutdown_complete->sctp_hdr.dst_port =
1234 sctp_conn->sub_conn[idx].connection.rmt_port;
1235 shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1237 vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1238 vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1240 vnet_buffer (b)->sctp.connection_index =
1241 sctp_conn->sub_conn[idx].connection.c_index;
1242 vnet_buffer (b)->sctp.subconn_idx = idx;
1246 sctp_send_shutdown_complete (sctp_connection_t * sctp_conn, u8 idx,
1249 vlib_main_t *vm = vlib_get_main ();
1251 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
1254 sctp_reuse_buffer (vm, b0);
1256 sctp_prepare_shutdown_complete_chunk (sctp_conn, idx, b0);
1263 sctp_send_init (sctp_connection_t * sctp_conn)
1267 sctp_main_t *tm = vnet_get_sctp_main ();
1268 vlib_main_t *vm = vlib_get_main ();
1270 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1273 b = vlib_get_buffer (vm, bi);
1274 u8 idx = MAIN_SCTP_SUB_CONN_IDX;
1276 sctp_init_buffer (vm, b);
1277 sctp_prepare_init_chunk (sctp_conn, idx, b);
1279 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
1280 sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
1282 /* Start the T1_INIT timer */
1283 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1284 sctp_conn->sub_conn[idx].RTO);
1286 /* Change state to COOKIE_WAIT */
1287 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
1289 /* Measure RTT with this */
1290 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1294 * Push SCTP header and update connection variables
1297 sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
1298 sctp_state_t next_state)
1301 b->current_length + b->total_length_not_including_first_buffer;
1302 ASSERT (!b->total_length_not_including_first_buffer
1303 || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1305 SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1306 "b->current_data = %p "
1308 b->current_length, b->current_data, data_len);
1310 u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1311 u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1313 bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1315 sctp_payload_data_chunk_t *data_chunk =
1316 vlib_buffer_push_uninit (b, bytes_to_add);
1318 u8 idx = sctp_data_subconn_select (sctp_conn);
1320 ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
1321 sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1322 sctp_conn->sub_conn[idx].connection.c_index,
1323 sctp_conn->sub_conn[idx].connection.lcl_port,
1324 sctp_conn->sub_conn[idx].connection.rmt_port);
1325 data_chunk->sctp_hdr.checksum = 0;
1326 data_chunk->sctp_hdr.src_port =
1327 sctp_conn->sub_conn[idx].connection.lcl_port;
1328 data_chunk->sctp_hdr.dst_port =
1329 sctp_conn->sub_conn[idx].connection.rmt_port;
1330 data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1332 data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
1333 data_chunk->stream_id = clib_host_to_net_u16 (0);
1334 data_chunk->stream_seq = clib_host_to_net_u16 (0);
1336 vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1337 vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1339 vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1340 vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1342 SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1343 b->data, b->current_data);
1345 sctp_conn->last_unacked_tsn = sctp_conn->next_tsn;
1346 sctp_conn->next_tsn += data_len;
1348 u32 inflight = sctp_conn->next_tsn - sctp_conn->last_unacked_tsn;
1349 /* Section 7.2.2; point (3) */
1350 if (sctp_conn->sub_conn[idx].partially_acked_bytes >=
1351 sctp_conn->sub_conn[idx].cwnd
1352 && inflight >= sctp_conn->sub_conn[idx].cwnd)
1354 sctp_conn->sub_conn[idx].cwnd += sctp_conn->sub_conn[idx].PMTU;
1355 sctp_conn->sub_conn[idx].partially_acked_bytes -=
1356 sctp_conn->sub_conn[idx].cwnd;
1359 sctp_conn->sub_conn[idx].last_data_ts = sctp_time_now ();
1361 vnet_buffer (b)->sctp.connection_index =
1362 sctp_conn->sub_conn[idx].connection.c_index;
1364 vnet_buffer (b)->sctp.subconn_idx = idx;
1368 sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
1370 sctp_connection_t *sctp_conn =
1371 sctp_get_connection_from_transport (trans_conn);
1373 SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, "
1374 "S_INDEX = %u, C_INDEX = %u,"
1375 "trans_conn->LCL_PORT = %u, trans_conn->RMT_PORT = %u",
1378 trans_conn->s_index,
1379 trans_conn->c_index,
1380 trans_conn->lcl_port, trans_conn->rmt_port);
1382 sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
1384 sctp_trajectory_add_start (b0, 3);
1389 #if SCTP_DEBUG_STATE_MACHINE
1391 sctp_validate_output_state_machine (sctp_connection_t * sctp_conn,
1395 switch (sctp_conn->state)
1397 case SCTP_STATE_CLOSED:
1398 if (chunk_type != INIT && chunk_type != INIT_ACK)
1401 case SCTP_STATE_ESTABLISHED:
1402 if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1403 chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1404 chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1407 case SCTP_STATE_COOKIE_WAIT:
1408 if (chunk_type != COOKIE_ECHO)
1411 case SCTP_STATE_SHUTDOWN_SENT:
1412 if (chunk_type != SHUTDOWN_COMPLETE)
1415 case SCTP_STATE_SHUTDOWN_RECEIVED:
1416 if (chunk_type != SHUTDOWN_ACK)
1425 sctp_is_retransmitting (sctp_connection_t * sctp_conn, u8 idx)
1427 return sctp_conn->sub_conn[idx].is_retransmitting;
1431 sctp46_output_inline (vlib_main_t * vm,
1432 vlib_node_runtime_t * node,
1433 vlib_frame_t * from_frame, int is_ip4)
1435 u32 n_left_from, next_index, *from, *to_next;
1436 u32 my_thread_index = vm->thread_index;
1438 from = vlib_frame_vector_args (from_frame);
1439 n_left_from = from_frame->n_vectors;
1440 next_index = node->cached_next_index;
1441 sctp_set_time_now (my_thread_index);
1443 while (n_left_from > 0)
1447 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1449 while (n_left_from > 0 && n_left_to_next > 0)
1453 sctp_header_t *sctp_hdr = 0;
1454 sctp_connection_t *sctp_conn;
1455 sctp_tx_trace_t *t0;
1456 sctp_header_t *th0 = 0;
1457 u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1458 SCTP_OUTPUT_NEXT_IP_LOOKUP;
1460 #if SCTP_DEBUG_STATE_MACHINE
1461 u16 packet_length = 0;
1469 n_left_to_next -= 1;
1471 b0 = vlib_get_buffer (vm, bi0);
1474 sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1477 if (PREDICT_FALSE (sctp_conn == 0))
1479 error0 = SCTP_ERROR_INVALID_CONNECTION;
1480 next0 = SCTP_OUTPUT_NEXT_DROP;
1484 u8 idx = vnet_buffer (b0)->sctp.subconn_idx;
1486 th0 = vlib_buffer_get_current (b0);
1490 ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1492 &sctp_conn->sub_conn
1499 IP_PROTOCOL_SCTP, 1);
1501 u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
1503 sctp_hdr = ip4_next_header (iph4);
1504 sctp_hdr->checksum = checksum;
1506 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1508 #if SCTP_DEBUG_STATE_MACHINE
1509 packet_length = clib_net_to_host_u16 (iph4->length);
1514 ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1516 &sctp_conn->sub_conn
1520 &sctp_conn->sub_conn
1527 u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
1530 sctp_hdr = ip6_next_header (iph6);
1531 sctp_hdr->checksum = checksum;
1533 vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
1534 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
1536 #if SCTP_DEBUG_STATE_MACHINE
1537 packet_length = clib_net_to_host_u16 (iph6->payload_length);
1541 sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1542 u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1543 if (chunk_type >= UNKNOWN)
1546 ("Trying to send an unrecognized chunk... something is really bad.");
1547 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1548 next0 = SCTP_OUTPUT_NEXT_DROP;
1552 #if SCTP_DEBUG_STATE_MACHINE
1554 (sctp_conn->sub_conn[idx].connection.lcl_port ==
1556 || sctp_conn->sub_conn[idx].connection.lcl_port ==
1558 && (sctp_conn->sub_conn[idx].connection.rmt_port ==
1560 || sctp_conn->sub_conn[idx].connection.rmt_port ==
1561 sctp_hdr->src_port);
1565 SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1566 "packet_length = %u, "
1567 "chunk_type = %u [%s], "
1568 "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1569 "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
1570 sctp_conn->sub_conn[idx].
1571 connection.c_index, packet_length,
1573 sctp_chunk_to_string (chunk_type),
1574 sctp_conn->sub_conn[idx].
1575 connection.lcl_port, sctp_hdr->src_port,
1576 sctp_conn->sub_conn[idx].
1577 connection.rmt_port,
1578 sctp_hdr->dst_port);
1580 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1581 next0 = SCTP_OUTPUT_NEXT_DROP;
1585 SCTP_DBG_STATE_MACHINE
1586 ("SESSION_INDEX = %u, CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1587 "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
1588 sctp_conn->sub_conn[idx].connection.s_index,
1589 sctp_conn->sub_conn[idx].connection.c_index,
1590 sctp_conn->state, sctp_state_to_string (sctp_conn->state),
1591 sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1592 full_hdr->hdr.dst_port);
1594 /* Let's make sure the state-machine does not send anything crazy */
1595 #if SCTP_DEBUG_STATE_MACHINE
1596 if (sctp_validate_output_state_machine (sctp_conn, chunk_type) != 0)
1598 SCTP_DBG_STATE_MACHINE
1599 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1600 sctp_chunk_to_string (chunk_type),
1601 sctp_state_to_string (sctp_conn->state));
1603 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1604 next0 = SCTP_OUTPUT_NEXT_DROP;
1610 /* Karn's algorithm: RTT measurements MUST NOT be made using
1611 * packets that were retransmitted
1613 if (!sctp_is_retransmitting (sctp_conn, idx))
1615 /* Measure RTT with this */
1616 if (chunk_type == DATA
1617 && sctp_conn->sub_conn[idx].RTO_pending == 0)
1619 sctp_conn->sub_conn[idx].RTO_pending = 1;
1620 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1623 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1626 /* Let's take care of TIMERS */
1631 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1636 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1638 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1639 sctp_conn->sub_conn[idx].RTO);
1644 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1645 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1646 sctp_conn->sub_conn[idx].RTO);
1647 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1652 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1653 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1654 sctp_conn->sub_conn[idx].RTO);
1655 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1658 case SHUTDOWN_COMPLETE:
1660 sctp_conn->state = SCTP_STATE_CLOSED;
1665 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1666 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1668 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1670 SCTP_DBG_STATE_MACHINE
1671 ("SESSION_INDEX = %u, CONNECTION_INDEX = %u, " "NEW_STATE = %s, "
1672 "CHUNK_SENT = %s", sctp_conn->sub_conn[idx].connection.s_index,
1673 sctp_conn->sub_conn[idx].connection.c_index,
1674 sctp_state_to_string (sctp_conn->state),
1675 sctp_chunk_to_string (chunk_type));
1677 vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1680 b0->error = node->errors[error0];
1681 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1683 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1686 clib_memcpy (&t0->sctp_header, th0,
1687 sizeof (t0->sctp_header));
1691 memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1693 clib_memcpy (&t0->sctp_connection, sctp_conn,
1694 sizeof (t0->sctp_connection));
1697 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1698 n_left_to_next, bi0, next0);
1701 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1704 return from_frame->n_vectors;
1708 sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1709 vlib_frame_t * from_frame)
1711 return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1715 sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1716 vlib_frame_t * from_frame)
1718 return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1722 VLIB_REGISTER_NODE (sctp4_output_node) =
1724 .function = sctp4_output,.name = "sctp4-output",
1725 /* Takes a vector of packets. */
1726 .vector_size = sizeof (u32),
1727 .n_errors = SCTP_N_ERROR,
1728 .error_strings = sctp_error_strings,
1729 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1731 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1732 foreach_sctp4_output_next
1735 .format_buffer = format_sctp_header,
1736 .format_trace = format_sctp_tx_trace,
1740 VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1743 VLIB_REGISTER_NODE (sctp6_output_node) =
1745 .function = sctp6_output,
1746 .name = "sctp6-output",
1747 /* Takes a vector of packets. */
1748 .vector_size = sizeof (u32),
1749 .n_errors = SCTP_N_ERROR,
1750 .error_strings = sctp_error_strings,
1751 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1753 #define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1754 foreach_sctp6_output_next
1757 .format_buffer = format_sctp_header,
1758 .format_trace = format_sctp_tx_trace,
1762 VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1765 * fd.io coding-style-patch-verification: ON
1768 * eval: (c-set-style "gnu")