blob: 9ac3feb8aab3f6659fd3468c287cbe4a120c862c [file] [log] [blame]
Marco Varlese191a5942017-10-30 18:17:21 +01001/*
2 * Copyright (c) 2017 SUSE LLC.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <vnet/sctp/sctp.h>
16#include <vnet/sctp/sctp_debug.h>
17#include <vppinfra/random.h>
Marco Varlese91389ac2018-01-31 11:00:01 +010018#include <openssl/hmac.h>
Marco Varlese191a5942017-10-30 18:17:21 +010019
20vlib_node_registration_t sctp4_output_node;
21vlib_node_registration_t sctp6_output_node;
22
23typedef enum _sctp_output_next
24{
25 SCTP_OUTPUT_NEXT_DROP,
26 SCTP_OUTPUT_NEXT_IP_LOOKUP,
27 SCTP_OUTPUT_N_NEXT
28} sctp_output_next_t;
29
30#define foreach_sctp4_output_next \
31 _ (DROP, "error-drop") \
32 _ (IP_LOOKUP, "ip4-lookup")
33
34#define foreach_sctp6_output_next \
35 _ (DROP, "error-drop") \
36 _ (IP_LOOKUP, "ip6-lookup")
37
38static char *sctp_error_strings[] = {
39#define sctp_error(n,s) s,
40#include <vnet/sctp/sctp_error.def>
41#undef sctp_error
42};
43
44typedef struct
45{
46 sctp_header_t sctp_header;
47 sctp_connection_t sctp_connection;
48} sctp_tx_trace_t;
49
50/**
51 * Flush tx frame populated by retransmits and timer pops
52 */
53void
54sctp_flush_frame_to_output (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
55{
56 if (sctp_main.tx_frames[!is_ip4][thread_index])
57 {
58 u32 next_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;
63 }
64}
65
66/**
67 * Flush ip lookup tx frames populated by timer pops
68 */
69always_inline void
70sctp_flush_frame_to_ip_lookup (vlib_main_t * vm, u8 thread_index, u8 is_ip4)
71{
72 if (sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index])
73 {
74 u32 next_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]
78 [thread_index]);
79 sctp_main.ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
80 }
81}
82
83/**
84 * Flush v4 and v6 sctp and ip-lookup tx frames for thread index
85 */
86void
87sctp_flush_frames_to_output (u8 thread_index)
88{
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);
94}
95
96u32
97ip4_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
98 ip4_header_t * ip0)
99{
100 ip_csum_t checksum;
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;
104
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;
109 checksum =
110 clib_host_to_net_u32 (payload_length_host_byte_order +
111 (ip0->protocol << 16));
112
113 if (BITS (uword) == 32)
114 {
115 checksum =
116 ip_csum_with_carry (checksum,
117 clib_mem_unaligned (&ip0->src_address, u32));
118 checksum =
119 ip_csum_with_carry (checksum,
120 clib_mem_unaligned (&ip0->dst_address, u32));
121 }
122 else
123 checksum =
124 ip_csum_with_carry (checksum,
125 clib_mem_unaligned (&ip0->src_address, u64));
126
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)
132 {
133 n_this_buffer = n_ip_bytes_this_buffer > ip_header_length ?
134 n_ip_bytes_this_buffer - ip_header_length : 0;
135 }
136 while (1)
137 {
138 checksum =
139 ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
140 n_bytes_left -= n_this_buffer;
141 if (n_bytes_left == 0)
142 break;
143
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;
148 }
149
150 return checksum;
151}
152
153u32
154ip6_sctp_compute_checksum (vlib_main_t * vm, vlib_buffer_t * p0,
155 ip6_header_t * ip0, int *bogus_lengthp)
156{
157 ip_csum_t checksum;
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;
162
163 ASSERT (bogus_lengthp);
164 *bogus_lengthp = 0;
165
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);
170
171 for (i = 0; i < ARRAY_LEN (ip0->src_address.as_uword); i++)
172 {
173 checksum = ip_csum_with_carry (checksum,
174 clib_mem_unaligned (&ip0->
175 src_address.as_uword
176 [i], uword));
177 checksum =
178 ip_csum_with_carry (checksum,
179 clib_mem_unaligned (&ip0->dst_address.as_uword[i],
180 uword));
181 }
182
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))
186 {
187 u32 skip_bytes;
188 ip6_hop_by_hop_ext_t *ext_hdr =
189 (ip6_hop_by_hop_ext_t *) data_this_buffer;
190
191 /* validate really icmp6 next */
192 ASSERT ((ext_hdr->next_hdr == IP_PROTOCOL_SCTP));
193
194 skip_bytes = 8 * (1 + ext_hdr->n_data_u64s);
195 data_this_buffer = (void *) ((u8 *) data_this_buffer + skip_bytes);
196
197 payload_length_host_byte_order -= skip_bytes;
198 headers_size += skip_bytes;
199 }
200
201 n_bytes_left = n_this_buffer = payload_length_host_byte_order;
202 if (p0 && n_this_buffer + headers_size > p0->current_length)
203 n_this_buffer =
204 p0->current_length >
205 headers_size ? p0->current_length - headers_size : 0;
206 while (1)
207 {
208 checksum =
209 ip_incremental_checksum (checksum, data_this_buffer, n_this_buffer);
210 n_bytes_left -= n_this_buffer;
211 if (n_bytes_left == 0)
212 break;
213
214 if (!(p0->flags & VLIB_BUFFER_NEXT_PRESENT))
215 {
216 *bogus_lengthp = 1;
217 return 0xfefe;
218 }
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;
222 }
223
224 return checksum;
225}
226
227void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100228sctp_push_ip_hdr (sctp_main_t * tm, sctp_sub_connection_t * sctp_sub_conn,
Marco Varlese191a5942017-10-30 18:17:21 +0100229 vlib_buffer_t * b)
230{
231 sctp_header_t *th = vlib_buffer_get_current (b);
232 vlib_main_t *vm = vlib_get_main ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100233 if (sctp_sub_conn->c_is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100234 {
235 ip4_header_t *ih;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100236 ih = vlib_buffer_push_ip4 (vm, b, &sctp_sub_conn->c_lcl_ip4,
237 &sctp_sub_conn->c_rmt_ip4, IP_PROTOCOL_SCTP,
238 1);
Marco Varlese191a5942017-10-30 18:17:21 +0100239 th->checksum = ip4_sctp_compute_checksum (vm, b, ih);
240 }
241 else
242 {
243 ip6_header_t *ih;
244 int bogus = ~0;
245
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100246 ih = vlib_buffer_push_ip6 (vm, b, &sctp_sub_conn->c_lcl_ip6,
247 &sctp_sub_conn->c_rmt_ip6, IP_PROTOCOL_SCTP);
Marco Varlese191a5942017-10-30 18:17:21 +0100248 th->checksum = ip6_sctp_compute_checksum (vm, b, ih, &bogus);
249 ASSERT (!bogus);
250 }
251}
252
253always_inline void *
254sctp_reuse_buffer (vlib_main_t * vm, vlib_buffer_t * b)
255{
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;
260 b->current_data = 0;
261 b->current_length = 0;
262 b->total_length_not_including_first_buffer = 0;
263 vnet_buffer (b)->sctp.flags = 0;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100264 vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
Marco Varlese191a5942017-10-30 18:17:21 +0100265
266 /* Leave enough space for headers */
267 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
268}
269
270always_inline void *
271sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
272{
273 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Mariondac03522018-02-01 15:30:13 +0100274 b->flags &= VLIB_BUFFER_NON_DEFAULT_FREELIST;
Marco Varlese191a5942017-10-30 18:17:21 +0100275 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
276 b->total_length_not_including_first_buffer = 0;
277 vnet_buffer (b)->sctp.flags = 0;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100278 vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
Marco Varlese191a5942017-10-30 18:17:21 +0100279 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
280 /* Leave enough space for headers */
281 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
282}
283
284always_inline int
285sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
286{
287 vlib_main_t *vm = vlib_get_main ();
288 u32 current_length = vec_len (tm->tx_buffers[thread_index]);
289 u32 n_allocated;
290
291 vec_validate (tm->tx_buffers[thread_index],
292 current_length + n_free_buffers - 1);
293 n_allocated =
294 vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
295 n_free_buffers);
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)
299 {
300 clib_warning ("out of buffers");
301 return -1;
302 }
303 return 0;
304}
305
306always_inline int
307sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
308{
309 u32 *my_tx_buffers;
310 u32 thread_index = vlib_get_thread_index ();
311 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
312 {
313 if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
314 return -1;
315 }
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;
319 return 0;
320}
321
322always_inline void
323sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
324 u8 is_ip4, u8 flush)
325{
326 sctp_main_t *tm = vnet_get_sctp_main ();
327 u32 thread_index = vlib_get_thread_index ();
328 u32 *to_next, next_index;
329 vlib_frame_t *f;
330
331 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
332 b->error = 0;
333
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);
337
338 /* Get frame to v4/6 output node */
339 f = tm->tx_frames[!is_ip4][thread_index];
340 if (!f)
341 {
342 f = vlib_get_frame_to_node (vm, next_index);
343 ASSERT (f);
344 tm->tx_frames[!is_ip4][thread_index] = f;
345 }
346 to_next = vlib_frame_vector_args (f);
347 to_next[f->n_vectors] = bi;
348 f->n_vectors += 1;
349 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
350 {
351 vlib_put_frame_to_node (vm, next_index, f);
352 tm->tx_frames[!is_ip4][thread_index] = 0;
353 }
354}
355
356always_inline void
357sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
358 u8 is_ip4)
359{
360 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
361}
362
363always_inline void
364sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
365 u8 is_ip4, u8 flush)
366{
367 sctp_main_t *tm = vnet_get_sctp_main ();
368 u32 thread_index = vlib_get_thread_index ();
369 u32 *to_next, next_index;
370 vlib_frame_t *f;
371
372 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
373 b->error = 0;
374
375 /* Default FIB for now */
376 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
377
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)
381 {
382 b->pre_data[0] = 2;
383 b->pre_data[1] = next_index;
384 }
385
386 f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
387 if (!f)
388 {
389 f = vlib_get_frame_to_node (vm, next_index);
390 ASSERT (f);
391 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
392 }
393
394 to_next = vlib_frame_vector_args (f);
395 to_next[f->n_vectors] = bi;
396 f->n_vectors += 1;
397 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
398 {
399 vlib_put_frame_to_node (vm, next_index, f);
400 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
401 }
402}
403
404always_inline void
405sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
406 u8 is_ip4)
407{
408 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
409}
410
Marco Varlese191a5942017-10-30 18:17:21 +0100411/**
412 * Convert buffer to INIT
413 */
414void
Marco Varlese54432f82018-02-15 17:01:56 +0100415sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, u8 idx,
416 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100417{
418 u32 random_seed = random_default_seed ();
419 u16 alloc_bytes = sizeof (sctp_init_chunk_t);
Marco Varlese54432f82018-02-15 17:01:56 +0100420 sctp_sub_connection_t *sub_conn = &sctp_conn->sub_conn[idx];
Marco Varlese191a5942017-10-30 18:17:21 +0100421
422 sctp_ipv4_addr_param_t *ip4_param = 0;
423 sctp_ipv6_addr_param_t *ip6_param = 0;
424
425 if (sub_conn->c_is_ip4)
426 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
427 else
428 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
429
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
432 */
433 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
434
435 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
436
437 sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
438
439 u16 pointer_offset = sizeof (init_chunk);
440 if (sub_conn->c_is_ip4)
441 {
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;
444
445 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
446 }
447 else
448 {
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];
452
453 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
454 }
455
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;
461
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);
465
Marco Varlese6e4d4a32018-03-12 12:36:59 +0100466 sctp_init_cwnd (sctp_conn);
467
Marco Varlesef3ab4892018-02-19 15:23:13 +0100468 init_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
Marco Varlese191a5942017-10-30 18:17:21 +0100469 init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
470 init_chunk->inboud_streams_count =
471 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
472 init_chunk->outbound_streams_count =
473 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
474
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100475 init_chunk->initial_tsn =
476 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
477 SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
478 sctp_conn->local_initial_tsn);
479
Marco Varlese191a5942017-10-30 18:17:21 +0100480 sctp_conn->local_tag = init_chunk->initiate_tag;
481
482 vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100483 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100484
485 SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
486 "CHUNK_TYPE = %s, "
487 "SRC_PORT = %u, DST_PORT = %u",
488 sub_conn->connection.c_index,
489 sctp_conn->state,
490 sctp_state_to_string (sctp_conn->state),
491 sctp_chunk_to_string (INIT),
492 init_chunk->sctp_hdr.src_port,
493 init_chunk->sctp_hdr.dst_port);
494}
495
Marco Varlese91389ac2018-01-31 11:00:01 +0100496void
497sctp_compute_mac (sctp_connection_t * sctp_conn,
498 sctp_state_cookie_param_t * state_cookie)
Marco Varlese191a5942017-10-30 18:17:21 +0100499{
Marco Varlese91389ac2018-01-31 11:00:01 +0100500#if OPENSSL_VERSION_NUMBER >= 0x10100000L
501 HMAC_CTX *ctx;
502#else
503 HMAC_CTX ctx;
Marco Varlese91389ac2018-01-31 11:00:01 +0100504#endif
505 unsigned int len = 0;
Marco Varlese18b63152018-02-12 09:08:21 +0100506 const EVP_MD *md = EVP_sha1 ();
Marco Varlese91389ac2018-01-31 11:00:01 +0100507#if OPENSSL_VERSION_NUMBER >= 0x10100000L
508 ctx = HMAC_CTX_new ();
Marco Varlese18b63152018-02-12 09:08:21 +0100509 HMAC_Init_ex (ctx, &state_cookie->creation_time,
Marco Varlese91389ac2018-01-31 11:00:01 +0100510 sizeof (state_cookie->creation_time), md, NULL);
511 HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
512 HMAC_Final (ctx, state_cookie->mac, &len);
513#else
514 HMAC_CTX_init (&ctx);
515 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
516 sizeof (state_cookie->creation_time), md, NULL);
Marco Varlese91389ac2018-01-31 11:00:01 +0100517 HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
518 HMAC_Final (&ctx, state_cookie->mac, &len);
519 HMAC_CTX_cleanup (&ctx);
520#endif
521
522 ENDIANESS_SWAP (state_cookie->mac);
Marco Varlese191a5942017-10-30 18:17:21 +0100523}
524
525void
Marco Varlese54432f82018-02-15 17:01:56 +0100526sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100527 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100528{
529 vlib_main_t *vm = vlib_get_main ();
Marco Varlese191a5942017-10-30 18:17:21 +0100530
531 sctp_reuse_buffer (vm, b);
532
533 u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
534
535 /* As per RFC 4960 the chunk_length value does NOT contemplate
536 * the size of the first header (see sctp_header_t) and any padding
537 */
538 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
539
540 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
541
542 sctp_cookie_ack_chunk_t *cookie_ack_chunk =
543 vlib_buffer_push_uninit (b, alloc_bytes);
544
545 cookie_ack_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100546 cookie_ack_chunk->sctp_hdr.src_port =
547 sctp_conn->sub_conn[idx].connection.lcl_port;
548 cookie_ack_chunk->sctp_hdr.dst_port =
549 sctp_conn->sub_conn[idx].connection.rmt_port;
550 cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100551 vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
552 vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
553
554 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100555 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100556 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100557}
558
559void
Marco Varlese54432f82018-02-15 17:01:56 +0100560sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese9e09ff32018-03-05 12:31:45 +0100561 vlib_buffer_t * b, u8 reuse_buffer)
Marco Varlese191a5942017-10-30 18:17:21 +0100562{
563 vlib_main_t *vm = vlib_get_main ();
Marco Varlese191a5942017-10-30 18:17:21 +0100564
Marco Varlese9e09ff32018-03-05 12:31:45 +0100565 if (reuse_buffer)
566 sctp_reuse_buffer (vm, b);
Marco Varlese191a5942017-10-30 18:17:21 +0100567
568 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
569 u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
570 /* As per RFC 4960 the chunk_length value does NOT contemplate
571 * the size of the first header (see sctp_header_t) and any padding
572 */
573 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
574 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
575 sctp_cookie_echo_chunk_t *cookie_echo_chunk =
576 vlib_buffer_push_uninit (b, alloc_bytes);
577 cookie_echo_chunk->sctp_hdr.checksum = 0;
578 cookie_echo_chunk->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100579 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +0100580 cookie_echo_chunk->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100581 sctp_conn->sub_conn[idx].connection.rmt_port;
582 cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100583 vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
584 vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
Marco Varlese9e09ff32018-03-05 12:31:45 +0100585 clib_memcpy (&(cookie_echo_chunk->cookie), &sctp_conn->cookie_param,
Marco Varlese191a5942017-10-30 18:17:21 +0100586 sizeof (sctp_state_cookie_param_t));
Marco Varlese21c8baf2018-02-02 17:17:51 +0100587
Marco Varlese191a5942017-10-30 18:17:21 +0100588 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100589 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100590 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100591}
592
Marco Varlese9e09ff32018-03-05 12:31:45 +0100593
594/*
595 * Send COOKIE_ECHO
596 */
597void
598sctp_send_cookie_echo (sctp_connection_t * sctp_conn)
599{
600 vlib_buffer_t *b;
601 u32 bi;
602 sctp_main_t *tm = vnet_get_sctp_main ();
603 vlib_main_t *vm = vlib_get_main ();
604
605 if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS))
606 {
607 clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection.");
608
609 session_stream_connect_notify (&sctp_conn->sub_conn
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100610 [SCTP_PRIMARY_PATH_IDX].connection, 1);
Marco Varlese9e09ff32018-03-05 12:31:45 +0100611
612 sctp_connection_timers_reset (sctp_conn);
613
614 sctp_connection_cleanup (sctp_conn);
615 }
616
617 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
618 return;
619
620 b = vlib_get_buffer (vm, bi);
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100621 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese9e09ff32018-03-05 12:31:45 +0100622
623 sctp_init_buffer (vm, b);
624 sctp_prepare_cookie_echo_chunk (sctp_conn, idx, b, 0);
625 sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
626
627 /* Start the T1_INIT timer */
628 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
629 sctp_conn->sub_conn[idx].RTO);
630
631 /* Change state to COOKIE_WAIT */
632 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
633
634 /* Measure RTT with this */
635 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
636}
637
638
Marco Varlese191a5942017-10-30 18:17:21 +0100639/**
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100640 * Convert buffer to ERROR
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100641 */
Marco Varlese200fa322018-02-26 16:33:54 +0100642void
643sctp_prepare_operation_error (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100644 vlib_buffer_t * b, u8 err_cause)
Marco Varlese200fa322018-02-26 16:33:54 +0100645{
646 vlib_main_t *vm = vlib_get_main ();
647
648 sctp_reuse_buffer (vm, b);
649
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100650 /* The minimum size of the message is given by the sctp_operation_error_t */
651 u16 alloc_bytes =
652 sizeof (sctp_operation_error_t) + sizeof (sctp_err_cause_param_t);
Marco Varlese200fa322018-02-26 16:33:54 +0100653
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100654 /* As per RFC 4960 the chunk_length value does NOT contemplate
655 * the size of the first header (see sctp_header_t) and any padding
656 */
Marco Varlese200fa322018-02-26 16:33:54 +0100657 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
658
659 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
660
661 sctp_operation_error_t *err_chunk =
662 vlib_buffer_push_uninit (b, alloc_bytes);
663
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100664 /* src_port & dst_port are already in network byte-order */
Marco Varlese200fa322018-02-26 16:33:54 +0100665 err_chunk->sctp_hdr.checksum = 0;
666 err_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
667 err_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100668 /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
Marco Varlese200fa322018-02-26 16:33:54 +0100669 err_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
670
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100671 err_chunk->err_causes[0].param_hdr.length =
672 clib_host_to_net_u16 (sizeof (err_chunk->err_causes[0].param_hdr.type) +
673 sizeof (err_chunk->err_causes[0].param_hdr.length));
674 err_chunk->err_causes[0].param_hdr.type = clib_host_to_net_u16 (err_cause);
675
Marco Varlese200fa322018-02-26 16:33:54 +0100676 vnet_sctp_set_chunk_type (&err_chunk->chunk_hdr, OPERATION_ERROR);
677 vnet_sctp_set_chunk_length (&err_chunk->chunk_hdr, chunk_len);
678
679 vnet_buffer (b)->sctp.connection_index =
680 sctp_conn->sub_conn[idx].connection.c_index;
681 vnet_buffer (b)->sctp.subconn_idx = idx;
682}
Marco Varlese200fa322018-02-26 16:33:54 +0100683
684/**
685 * Convert buffer to ABORT
686 */
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100687void
688sctp_prepare_abort_for_collision (sctp_connection_t * sctp_conn, u8 idx,
689 vlib_buffer_t * b, ip4_address_t * ip4_addr,
690 ip6_address_t * ip6_addr)
691{
692 vlib_main_t *vm = vlib_get_main ();
693
694 sctp_reuse_buffer (vm, b);
695
696 /* The minimum size of the message is given by the sctp_abort_chunk_t */
697 u16 alloc_bytes = sizeof (sctp_abort_chunk_t);
698
699 /* As per RFC 4960 the chunk_length value does NOT contemplate
700 * the size of the first header (see sctp_header_t) and any padding
701 */
702 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
703
704 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
705
706 sctp_abort_chunk_t *abort_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
707
708 /* src_port & dst_port are already in network byte-order */
709 abort_chunk->sctp_hdr.checksum = 0;
710 abort_chunk->sctp_hdr.src_port =
711 sctp_conn->sub_conn[idx].connection.lcl_port;
712 abort_chunk->sctp_hdr.dst_port =
713 sctp_conn->sub_conn[idx].connection.rmt_port;
714 /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
715 abort_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
716
717 vnet_sctp_set_chunk_type (&abort_chunk->chunk_hdr, ABORT);
718 vnet_sctp_set_chunk_length (&abort_chunk->chunk_hdr, chunk_len);
719
720 vnet_buffer (b)->sctp.connection_index =
721 sctp_conn->sub_conn[idx].connection.c_index;
722 vnet_buffer (b)->sctp.subconn_idx = idx;
723}
724
725/**
726 * Convert buffer to INIT-ACK
727 */
728void
729sctp_prepare_initack_chunk_for_collision (sctp_connection_t * sctp_conn,
730 u8 idx, vlib_buffer_t * b,
731 ip4_address_t * ip4_addr,
732 ip6_address_t * ip6_addr)
733{
734 vlib_main_t *vm = vlib_get_main ();
735 sctp_ipv4_addr_param_t *ip4_param = 0;
736 sctp_ipv6_addr_param_t *ip6_param = 0;
737
738 sctp_reuse_buffer (vm, b);
739
740 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
741 u16 alloc_bytes =
742 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
743
744 if (PREDICT_TRUE (ip4_addr != NULL))
745 {
746 /* Create room for variable-length fields in the INIT_ACK chunk */
747 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
748 }
749 if (PREDICT_TRUE (ip6_addr != NULL))
750 {
751 /* Create room for variable-length fields in the INIT_ACK chunk */
752 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
753 }
754
755 if (sctp_conn->sub_conn[idx].connection.is_ip4)
756 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
757 else
758 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
759
760 /* As per RFC 4960 the chunk_length value does NOT contemplate
761 * the size of the first header (see sctp_header_t) and any padding
762 */
763 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
764
765 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
766
767 sctp_init_ack_chunk_t *init_ack_chunk =
768 vlib_buffer_push_uninit (b, alloc_bytes);
769
770 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
771
772 /* Create State Cookie parameter */
773 sctp_state_cookie_param_t *state_cookie_param =
774 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
775
776 state_cookie_param->param_hdr.type =
777 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
778 state_cookie_param->param_hdr.length =
779 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
780 state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
781 state_cookie_param->cookie_lifespan =
782 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
783
784 sctp_compute_mac (sctp_conn, state_cookie_param);
785
786 pointer_offset += sizeof (sctp_state_cookie_param_t);
787
788 if (PREDICT_TRUE (ip4_addr != NULL))
789 {
790 sctp_ipv4_addr_param_t *ipv4_addr =
791 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
792
793 ipv4_addr->param_hdr.type =
794 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
795 ipv4_addr->param_hdr.length =
796 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
797 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
798
799 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
800 }
801 if (PREDICT_TRUE (ip6_addr != NULL))
802 {
803 sctp_ipv6_addr_param_t *ipv6_addr =
804 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
805
806 ipv6_addr->param_hdr.type =
807 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
808 ipv6_addr->param_hdr.length =
809 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
810 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
811 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
812
813 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
814 }
815
816 if (sctp_conn->sub_conn[idx].connection.is_ip4)
817 {
818 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
819 ip4_param->address.as_u32 =
820 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
821
822 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
823 }
824 else
825 {
826 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
827 ip6_param->address.as_u64[0] =
828 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
829 ip6_param->address.as_u64[1] =
830 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
831
832 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
833 }
834
835 /* src_port & dst_port are already in network byte-order */
836 init_ack_chunk->sctp_hdr.checksum = 0;
837 init_ack_chunk->sctp_hdr.src_port =
838 sctp_conn->sub_conn[idx].connection.lcl_port;
839 init_ack_chunk->sctp_hdr.dst_port =
840 sctp_conn->sub_conn[idx].connection.rmt_port;
841 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
842 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
843 init_ack_chunk->initial_tsn =
844 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
845 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
846 init_ack_chunk->initial_tsn);
847
848 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
849 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
850
851 init_ack_chunk->initiate_tag = sctp_conn->local_tag;
852
853 init_ack_chunk->a_rwnd =
854 clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
855 init_ack_chunk->inboud_streams_count =
856 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
857 init_ack_chunk->outbound_streams_count =
858 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
859
860 vnet_buffer (b)->sctp.connection_index =
861 sctp_conn->sub_conn[idx].connection.c_index;
862 vnet_buffer (b)->sctp.subconn_idx = idx;
863}
864
865/**
Marco Varlese191a5942017-10-30 18:17:21 +0100866 * Convert buffer to INIT-ACK
867 */
868void
Marco Varlese54432f82018-02-15 17:01:56 +0100869sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, u8 idx,
870 vlib_buffer_t * b, ip4_address_t * ip4_addr,
Marco Varlese191a5942017-10-30 18:17:21 +0100871 ip6_address_t * ip6_addr)
872{
873 vlib_main_t *vm = vlib_get_main ();
874 sctp_ipv4_addr_param_t *ip4_param = 0;
875 sctp_ipv6_addr_param_t *ip6_param = 0;
Marco Varlese191a5942017-10-30 18:17:21 +0100876 u32 random_seed = random_default_seed ();
877
878 sctp_reuse_buffer (vm, b);
879
880 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
881 u16 alloc_bytes =
882 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
883
884 if (PREDICT_TRUE (ip4_addr != NULL))
885 {
886 /* Create room for variable-length fields in the INIT_ACK chunk */
887 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
888 }
889 if (PREDICT_TRUE (ip6_addr != NULL))
890 {
891 /* Create room for variable-length fields in the INIT_ACK chunk */
892 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
893 }
894
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100895 if (sctp_conn->sub_conn[idx].connection.is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100896 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
897 else
898 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
899
900 /* As per RFC 4960 the chunk_length value does NOT contemplate
901 * the size of the first header (see sctp_header_t) and any padding
902 */
903 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
904
905 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
906
907 sctp_init_ack_chunk_t *init_ack_chunk =
908 vlib_buffer_push_uninit (b, alloc_bytes);
909
910 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
911
912 /* Create State Cookie parameter */
913 sctp_state_cookie_param_t *state_cookie_param =
914 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
915
916 state_cookie_param->param_hdr.type =
917 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
918 state_cookie_param->param_hdr.length =
919 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
920 state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
921 state_cookie_param->cookie_lifespan =
922 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
Marco Varlese91389ac2018-01-31 11:00:01 +0100923
924 sctp_compute_mac (sctp_conn, state_cookie_param);
Marco Varlese191a5942017-10-30 18:17:21 +0100925
926 pointer_offset += sizeof (sctp_state_cookie_param_t);
927
928 if (PREDICT_TRUE (ip4_addr != NULL))
929 {
930 sctp_ipv4_addr_param_t *ipv4_addr =
931 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
932
933 ipv4_addr->param_hdr.type =
934 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
935 ipv4_addr->param_hdr.length =
936 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
937 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
938
939 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
940 }
941 if (PREDICT_TRUE (ip6_addr != NULL))
942 {
943 sctp_ipv6_addr_param_t *ipv6_addr =
Marco Varlesef429a932018-02-06 17:31:06 +0100944 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
Marco Varlese191a5942017-10-30 18:17:21 +0100945
946 ipv6_addr->param_hdr.type =
947 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
948 ipv6_addr->param_hdr.length =
949 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
950 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
951 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
952
953 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
954 }
955
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100956 if (sctp_conn->sub_conn[idx].connection.is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100957 {
958 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
959 ip4_param->address.as_u32 =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100960 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
Marco Varlese191a5942017-10-30 18:17:21 +0100961
962 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
963 }
964 else
965 {
966 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
967 ip6_param->address.as_u64[0] =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100968 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
Marco Varlese191a5942017-10-30 18:17:21 +0100969 ip6_param->address.as_u64[1] =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100970 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
Marco Varlese191a5942017-10-30 18:17:21 +0100971
972 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
973 }
974
975 /* src_port & dst_port are already in network byte-order */
976 init_ack_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100977 init_ack_chunk->sctp_hdr.src_port =
978 sctp_conn->sub_conn[idx].connection.lcl_port;
979 init_ack_chunk->sctp_hdr.dst_port =
980 sctp_conn->sub_conn[idx].connection.rmt_port;
981 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
982 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
983 init_ack_chunk->initial_tsn =
984 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
985 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
986 init_ack_chunk->initial_tsn);
Marco Varlese191a5942017-10-30 18:17:21 +0100987
988 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
989 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
990
991 init_ack_chunk->initiate_tag =
992 clib_host_to_net_u32 (random_u32 (&random_seed));
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100993
Marco Varlesef3ab4892018-02-19 15:23:13 +0100994 init_ack_chunk->a_rwnd =
995 clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
Marco Varlese191a5942017-10-30 18:17:21 +0100996 init_ack_chunk->inboud_streams_count =
997 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
998 init_ack_chunk->outbound_streams_count =
999 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
1000
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001001 sctp_conn->local_tag = init_ack_chunk->initiate_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001002
1003 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001004 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001005 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001006}
1007
1008/**
1009 * Convert buffer to SHUTDOWN
1010 */
1011void
Marco Varlese54432f82018-02-15 17:01:56 +01001012sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, u8 idx,
1013 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001014{
Marco Varlese191a5942017-10-30 18:17:21 +01001015 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1016
Marco Varlese191a5942017-10-30 18:17:21 +01001017 /* As per RFC 4960 the chunk_length value does NOT contemplate
1018 * the size of the first header (see sctp_header_t) and any padding
1019 */
1020 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1021
1022 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1023
1024 sctp_shutdown_association_chunk_t *shutdown_chunk =
1025 vlib_buffer_push_uninit (b, alloc_bytes);
1026
1027 shutdown_chunk->sctp_hdr.checksum = 0;
1028 /* No need of host_to_net conversion, already in net-byte order */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001029 shutdown_chunk->sctp_hdr.src_port =
1030 sctp_conn->sub_conn[idx].connection.lcl_port;
1031 shutdown_chunk->sctp_hdr.dst_port =
1032 sctp_conn->sub_conn[idx].connection.rmt_port;
1033 shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001034 vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
1035 vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
1036
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001037 shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
Marco Varlese191a5942017-10-30 18:17:21 +01001038
1039 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001040 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001041 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001042}
1043
1044/*
1045 * Send SHUTDOWN
1046 */
1047void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001048sctp_send_shutdown (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +01001049{
1050 vlib_buffer_t *b;
1051 u32 bi;
1052 sctp_main_t *tm = vnet_get_sctp_main ();
1053 vlib_main_t *vm = vlib_get_main ();
1054
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001055 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001056 return;
1057
1058 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1059 return;
1060
Marco Varlesec7fe4f32018-03-05 15:12:29 +01001061 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese54432f82018-02-15 17:01:56 +01001062
Marco Varlese191a5942017-10-30 18:17:21 +01001063 b = vlib_get_buffer (vm, bi);
1064 sctp_init_buffer (vm, b);
Marco Varlese54432f82018-02-15 17:01:56 +01001065 sctp_prepare_shutdown_chunk (sctp_conn, idx, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001066
Marco Varlesebe2251b2018-02-07 12:22:41 +01001067 sctp_enqueue_to_output_now (vm, b, bi,
1068 sctp_conn->sub_conn[idx].connection.is_ip4);
Marco Varlese191a5942017-10-30 18:17:21 +01001069}
1070
1071/**
1072 * Convert buffer to SHUTDOWN_ACK
1073 */
1074void
Marco Varlese54432f82018-02-15 17:01:56 +01001075sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001076 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001077{
Marco Varlese191a5942017-10-30 18:17:21 +01001078 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1079 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1080
1081 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1082
1083 sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
1084 vlib_buffer_push_uninit (b, alloc_bytes);
1085
1086 shutdown_ack_chunk->sctp_hdr.checksum = 0;
1087 /* No need of host_to_net conversion, already in net-byte order */
1088 shutdown_ack_chunk->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001089 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +01001090 shutdown_ack_chunk->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001091 sctp_conn->sub_conn[idx].connection.rmt_port;
1092 shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001093
1094 vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
1095 vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
1096
1097 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001098 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001099 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001100}
1101
1102/*
1103 * Send SHUTDOWN_ACK
1104 */
1105void
Marco Varlese54432f82018-02-15 17:01:56 +01001106sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, u8 idx,
1107 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001108{
Marco Varlese191a5942017-10-30 18:17:21 +01001109 vlib_main_t *vm = vlib_get_main ();
1110
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001111 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001112 return;
1113
Marco Varlesebe2251b2018-02-07 12:22:41 +01001114 sctp_reuse_buffer (vm, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001115
Marco Varlese54432f82018-02-15 17:01:56 +01001116 sctp_prepare_shutdown_ack_chunk (sctp_conn, idx, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001117}
1118
1119/**
1120 * Convert buffer to SACK
1121 */
1122void
Marco Varlese54432f82018-02-15 17:01:56 +01001123sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1124 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001125{
1126 vlib_main_t *vm = vlib_get_main ();
Marco Varlese191a5942017-10-30 18:17:21 +01001127
1128 sctp_reuse_buffer (vm, b);
1129
1130 u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
1131
1132 /* As per RFC 4960 the chunk_length value does NOT contemplate
1133 * the size of the first header (see sctp_header_t) and any padding
1134 */
1135 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1136
1137 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1138
1139 sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
1140
1141 sack->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001142 sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1143 sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1144 sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001145 vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
1146 vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
1147
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001148 sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
1149
1150 sctp_conn->ack_state = 0;
1151
Marco Varlese191a5942017-10-30 18:17:21 +01001152 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001153 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001154 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001155}
1156
1157/**
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001158 * Convert buffer to HEARTBEAT_ACK
1159 */
1160void
Marco Varlese54432f82018-02-15 17:01:56 +01001161sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001162 vlib_buffer_t * b)
1163{
1164 vlib_main_t *vm = vlib_get_main ();
1165
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001166 u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
1167
1168 sctp_reuse_buffer (vm, b);
1169
1170 /* As per RFC 4960 the chunk_length value does NOT contemplate
1171 * the size of the first header (see sctp_header_t) and any padding
1172 */
1173 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1174
1175 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1176
1177 sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
1178
1179 hb_ack->sctp_hdr.checksum = 0;
1180 /* No need of host_to_net conversion, already in net-byte order */
1181 hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1182 hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1183 hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1184 hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1185 hb_ack->hb_info.param_hdr.length =
1186 clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
1187
1188 vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
1189 vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
1190
1191 vnet_buffer (b)->sctp.connection_index =
1192 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001193 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001194}
1195
1196/**
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001197 * Convert buffer to HEARTBEAT
1198 */
1199void
Marco Varlese54432f82018-02-15 17:01:56 +01001200sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001201 vlib_buffer_t * b)
1202{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001203 u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
1204
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001205 /* As per RFC 4960 the chunk_length value does NOT contemplate
1206 * the size of the first header (see sctp_header_t) and any padding
1207 */
1208 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1209
1210 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1211
1212 sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
1213
1214 hb_req->sctp_hdr.checksum = 0;
1215 /* No need of host_to_net conversion, already in net-byte order */
1216 hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1217 hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1218 hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1219 hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1220 hb_req->hb_info.param_hdr.length =
1221 clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
1222
1223 vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
1224 vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
1225
1226 vnet_buffer (b)->sctp.connection_index =
1227 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001228 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001229}
1230
1231void
1232sctp_send_heartbeat (sctp_connection_t * sctp_conn)
1233{
1234 vlib_buffer_t *b;
1235 u32 bi;
1236 sctp_main_t *tm = vnet_get_sctp_main ();
1237 vlib_main_t *vm = vlib_get_main ();
1238
Marco Varlese54432f82018-02-15 17:01:56 +01001239 u8 i;
1240 u32 now = sctp_time_now ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001241
Marco Varlese54432f82018-02-15 17:01:56 +01001242 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
1243 {
1244 if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
1245 continue;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001246
Marco Varlese54432f82018-02-15 17:01:56 +01001247 if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
1248 {
1249 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1250 return;
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001251
Marco Varlese54432f82018-02-15 17:01:56 +01001252 b = vlib_get_buffer (vm, bi);
1253 sctp_init_buffer (vm, b);
1254 sctp_prepare_heartbeat_chunk (sctp_conn, i, b);
1255
1256 sctp_enqueue_to_output_now (vm, b, bi,
1257 sctp_conn->sub_conn[i].
1258 connection.is_ip4);
1259
1260 sctp_conn->sub_conn[i].unacknowledged_hb += 1;
1261 }
1262 }
Marco Varlese191a5942017-10-30 18:17:21 +01001263}
1264
1265/**
1266 * Convert buffer to SHUTDOWN_COMPLETE
1267 */
1268void
Marco Varlese54432f82018-02-15 17:01:56 +01001269sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese191a5942017-10-30 18:17:21 +01001270 vlib_buffer_t * b)
1271{
Marco Varlese191a5942017-10-30 18:17:21 +01001272 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1273 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1274
1275 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1276
1277 sctp_shutdown_complete_chunk_t *shutdown_complete =
1278 vlib_buffer_push_uninit (b, alloc_bytes);
1279
1280 shutdown_complete->sctp_hdr.checksum = 0;
1281 /* No need of host_to_net conversion, already in net-byte order */
1282 shutdown_complete->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001283 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +01001284 shutdown_complete->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001285 sctp_conn->sub_conn[idx].connection.rmt_port;
1286 shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001287
1288 vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1289 vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1290
1291 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001292 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001293 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001294}
1295
1296void
Marco Varlese54432f82018-02-15 17:01:56 +01001297sctp_send_shutdown_complete (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlesefae40392018-02-14 15:38:35 +01001298 vlib_buffer_t * b0)
Marco Varlese191a5942017-10-30 18:17:21 +01001299{
Marco Varlese191a5942017-10-30 18:17:21 +01001300 vlib_main_t *vm = vlib_get_main ();
1301
Marco Varlesefae40392018-02-14 15:38:35 +01001302 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001303 return;
1304
Marco Varlesefae40392018-02-14 15:38:35 +01001305 sctp_reuse_buffer (vm, b0);
Marco Varlese191a5942017-10-30 18:17:21 +01001306
Marco Varlese54432f82018-02-15 17:01:56 +01001307 sctp_prepare_shutdown_complete_chunk (sctp_conn, idx, b0);
Marco Varlese191a5942017-10-30 18:17:21 +01001308}
1309
Marco Varlese191a5942017-10-30 18:17:21 +01001310/*
1311 * Send INIT
1312 */
1313void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001314sctp_send_init (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +01001315{
1316 vlib_buffer_t *b;
1317 u32 bi;
1318 sctp_main_t *tm = vnet_get_sctp_main ();
1319 vlib_main_t *vm = vlib_get_main ();
1320
Marco Varlese9e09ff32018-03-05 12:31:45 +01001321 if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS))
1322 {
1323 clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection.");
1324
1325 session_stream_connect_notify (&sctp_conn->sub_conn
Marco Varlesec7fe4f32018-03-05 15:12:29 +01001326 [SCTP_PRIMARY_PATH_IDX].connection, 1);
Marco Varlese9e09ff32018-03-05 12:31:45 +01001327
1328 sctp_connection_timers_reset (sctp_conn);
1329
1330 sctp_connection_cleanup (sctp_conn);
1331
1332 return;
1333 }
1334
Marco Varlese191a5942017-10-30 18:17:21 +01001335 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1336 return;
1337
1338 b = vlib_get_buffer (vm, bi);
Marco Varlesec7fe4f32018-03-05 15:12:29 +01001339 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese191a5942017-10-30 18:17:21 +01001340
1341 sctp_init_buffer (vm, b);
Marco Varlese54432f82018-02-15 17:01:56 +01001342 sctp_prepare_init_chunk (sctp_conn, idx, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001343
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001344 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001345 sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
Marco Varlese191a5942017-10-30 18:17:21 +01001346
1347 /* Start the T1_INIT timer */
Marco Varlese21c8baf2018-02-02 17:17:51 +01001348 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1349 sctp_conn->sub_conn[idx].RTO);
1350
Marco Varlese191a5942017-10-30 18:17:21 +01001351 /* Change state to COOKIE_WAIT */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001352 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
Marco Varlesea38783e2018-02-13 12:38:52 +01001353
1354 /* Measure RTT with this */
1355 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +01001356}
1357
Marco Varlese191a5942017-10-30 18:17:21 +01001358/**
1359 * Push SCTP header and update connection variables
1360 */
1361static void
Marco Varlesef3ab4892018-02-19 15:23:13 +01001362sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
Marco Varlese191a5942017-10-30 18:17:21 +01001363 sctp_state_t next_state)
1364{
Marco Varlese191a5942017-10-30 18:17:21 +01001365 u16 data_len =
1366 b->current_length + b->total_length_not_including_first_buffer;
1367 ASSERT (!b->total_length_not_including_first_buffer
1368 || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1369
1370 SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1371 "b->current_data = %p "
1372 "data_len = %u",
1373 b->current_length, b->current_data, data_len);
1374
1375 u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1376 u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1377
1378 bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1379
1380 sctp_payload_data_chunk_t *data_chunk =
1381 vlib_buffer_push_uninit (b, bytes_to_add);
1382
Marco Varlesef3ab4892018-02-19 15:23:13 +01001383 u8 idx = sctp_data_subconn_select (sctp_conn);
Marco Varlese15cc6a82018-02-21 12:39:52 +01001384 SCTP_DBG_OUTPUT
Marco Varlese04e5d642018-02-23 17:43:06 +01001385 ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
Marco Varlese15cc6a82018-02-21 12:39:52 +01001386 sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1387 sctp_conn->sub_conn[idx].connection.c_index,
1388 sctp_conn->sub_conn[idx].connection.lcl_port,
1389 sctp_conn->sub_conn[idx].connection.rmt_port);
Marco Varlese191a5942017-10-30 18:17:21 +01001390 data_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001391 data_chunk->sctp_hdr.src_port =
1392 sctp_conn->sub_conn[idx].connection.lcl_port;
1393 data_chunk->sctp_hdr.dst_port =
1394 sctp_conn->sub_conn[idx].connection.rmt_port;
1395 data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001396
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001397 data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
Marco Varlese191a5942017-10-30 18:17:21 +01001398 data_chunk->stream_id = clib_host_to_net_u16 (0);
1399 data_chunk->stream_seq = clib_host_to_net_u16 (0);
1400
1401 vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1402 vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1403
Marco Varlese91389ac2018-01-31 11:00:01 +01001404 vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1405 vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1406
Marco Varlese191a5942017-10-30 18:17:21 +01001407 SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1408 b->data, b->current_data);
1409
Marco Varlese6e4d4a32018-03-12 12:36:59 +01001410 if (sctp_conn->sub_conn[idx].state != SCTP_SUBCONN_AWAITING_SACK)
1411 {
1412 sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_AWAITING_SACK;
1413 sctp_conn->last_unacked_tsn = sctp_conn->next_tsn;
1414 }
1415
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001416 sctp_conn->next_tsn += data_len;
1417
Marco Varlesef3ab4892018-02-19 15:23:13 +01001418 u32 inflight = sctp_conn->next_tsn - sctp_conn->last_unacked_tsn;
1419 /* Section 7.2.2; point (3) */
1420 if (sctp_conn->sub_conn[idx].partially_acked_bytes >=
1421 sctp_conn->sub_conn[idx].cwnd
1422 && inflight >= sctp_conn->sub_conn[idx].cwnd)
1423 {
1424 sctp_conn->sub_conn[idx].cwnd += sctp_conn->sub_conn[idx].PMTU;
1425 sctp_conn->sub_conn[idx].partially_acked_bytes -=
1426 sctp_conn->sub_conn[idx].cwnd;
1427 }
1428
1429 sctp_conn->sub_conn[idx].last_data_ts = sctp_time_now ();
1430
Marco Varlese191a5942017-10-30 18:17:21 +01001431 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001432 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese54432f82018-02-15 17:01:56 +01001433
Marco Varlese15cc6a82018-02-21 12:39:52 +01001434 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001435}
1436
1437u32
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001438sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001439{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001440 sctp_connection_t *sctp_conn =
1441 sctp_get_connection_from_transport (trans_conn);
Marco Varlese191a5942017-10-30 18:17:21 +01001442
Marco Varlese15cc6a82018-02-21 12:39:52 +01001443 SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, "
1444 "S_INDEX = %u, C_INDEX = %u,"
Marco Varlese04e5d642018-02-23 17:43:06 +01001445 "trans_conn->LCL_PORT = %u, trans_conn->RMT_PORT = %u",
Marco Varlese15cc6a82018-02-21 12:39:52 +01001446 trans_conn,
1447 sctp_conn,
1448 trans_conn->s_index,
1449 trans_conn->c_index,
1450 trans_conn->lcl_port, trans_conn->rmt_port);
1451
Marco Varlesef3ab4892018-02-19 15:23:13 +01001452 sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
Marco Varlese21c8baf2018-02-02 17:17:51 +01001453
Stevenb95bc052018-02-27 10:29:32 -08001454 sctp_trajectory_add_start (b, 3);
Marco Varlese191a5942017-10-30 18:17:21 +01001455
1456 return 0;
Marco Varlese191a5942017-10-30 18:17:21 +01001457}
1458
Marco Varlese9e09ff32018-03-05 12:31:45 +01001459void
1460sctp_data_retransmit (sctp_connection_t * sctp_conn)
1461{
1462 /* TODO: requires use of PEEK/SEND */
1463 SCTP_DBG_OUTPUT
1464 ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
1465 sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1466 sctp_conn->sub_conn[idx].connection.c_index,
1467 sctp_conn->sub_conn[idx].connection.lcl_port,
1468 sctp_conn->sub_conn[idx].connection.rmt_port);
1469
1470 return;
1471}
1472
Marco Varlesefae40392018-02-14 15:38:35 +01001473#if SCTP_DEBUG_STATE_MACHINE
Marco Varlesea38783e2018-02-13 12:38:52 +01001474always_inline u8
1475sctp_validate_output_state_machine (sctp_connection_t * sctp_conn,
1476 u8 chunk_type)
1477{
1478 u8 result = 0;
1479 switch (sctp_conn->state)
1480 {
1481 case SCTP_STATE_CLOSED:
1482 if (chunk_type != INIT && chunk_type != INIT_ACK)
1483 result = 1;
1484 break;
1485 case SCTP_STATE_ESTABLISHED:
1486 if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1487 chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1488 chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1489 result = 1;
1490 break;
1491 case SCTP_STATE_COOKIE_WAIT:
1492 if (chunk_type != COOKIE_ECHO)
1493 result = 1;
1494 break;
1495 case SCTP_STATE_SHUTDOWN_SENT:
1496 if (chunk_type != SHUTDOWN_COMPLETE)
1497 result = 1;
1498 break;
1499 case SCTP_STATE_SHUTDOWN_RECEIVED:
1500 if (chunk_type != SHUTDOWN_ACK)
1501 result = 1;
1502 break;
1503 }
1504 return result;
1505}
Marco Varlesefae40392018-02-14 15:38:35 +01001506#endif
Marco Varlesea38783e2018-02-13 12:38:52 +01001507
1508always_inline u8
1509sctp_is_retransmitting (sctp_connection_t * sctp_conn, u8 idx)
1510{
1511 return sctp_conn->sub_conn[idx].is_retransmitting;
1512}
1513
Marco Varlese191a5942017-10-30 18:17:21 +01001514always_inline uword
1515sctp46_output_inline (vlib_main_t * vm,
1516 vlib_node_runtime_t * node,
1517 vlib_frame_t * from_frame, int is_ip4)
1518{
1519 u32 n_left_from, next_index, *from, *to_next;
1520 u32 my_thread_index = vm->thread_index;
1521
1522 from = vlib_frame_vector_args (from_frame);
1523 n_left_from = from_frame->n_vectors;
1524 next_index = node->cached_next_index;
1525 sctp_set_time_now (my_thread_index);
1526
1527 while (n_left_from > 0)
1528 {
1529 u32 n_left_to_next;
1530
1531 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1532
1533 while (n_left_from > 0 && n_left_to_next > 0)
1534 {
1535 u32 bi0;
1536 vlib_buffer_t *b0;
1537 sctp_header_t *sctp_hdr = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001538 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +01001539 sctp_tx_trace_t *t0;
1540 sctp_header_t *th0 = 0;
1541 u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1542 SCTP_OUTPUT_NEXT_IP_LOOKUP;
1543
1544#if SCTP_DEBUG_STATE_MACHINE
1545 u16 packet_length = 0;
1546#endif
1547
1548 bi0 = from[0];
1549 to_next[0] = bi0;
1550 from += 1;
1551 to_next += 1;
1552 n_left_from -= 1;
1553 n_left_to_next -= 1;
1554
1555 b0 = vlib_get_buffer (vm, bi0);
Marco Varlesebe2251b2018-02-07 12:22:41 +01001556
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001557 sctp_conn =
1558 sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1559 my_thread_index);
Marco Varlese191a5942017-10-30 18:17:21 +01001560
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001561 if (PREDICT_FALSE (sctp_conn == 0))
Marco Varlese191a5942017-10-30 18:17:21 +01001562 {
1563 error0 = SCTP_ERROR_INVALID_CONNECTION;
1564 next0 = SCTP_OUTPUT_NEXT_DROP;
1565 goto done;
1566 }
1567
Marco Varlese15cc6a82018-02-21 12:39:52 +01001568 u8 idx = vnet_buffer (b0)->sctp.subconn_idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001569
1570 th0 = vlib_buffer_get_current (b0);
1571
1572 if (is_ip4)
1573 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001574 ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1575 b0,
1576 &sctp_conn->sub_conn
1577 [idx].connection.
1578 lcl_ip.ip4,
1579 &sctp_conn->
1580 sub_conn
1581 [idx].connection.
1582 rmt_ip.ip4,
1583 IP_PROTOCOL_SCTP, 1);
Marco Varlese191a5942017-10-30 18:17:21 +01001584
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001585 u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
Marco Varlese191a5942017-10-30 18:17:21 +01001586
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001587 sctp_hdr = ip4_next_header (iph4);
Marco Varlese191a5942017-10-30 18:17:21 +01001588 sctp_hdr->checksum = checksum;
1589
1590 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001591
1592#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001593 packet_length = clib_net_to_host_u16 (iph4->length);
Marco Varlese191a5942017-10-30 18:17:21 +01001594#endif
1595 }
1596 else
1597 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001598 ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1599 b0,
1600 &sctp_conn->sub_conn
1601 [idx].
1602 connection.lcl_ip.
1603 ip6,
1604 &sctp_conn->sub_conn
1605 [idx].
1606 connection.rmt_ip.
1607 ip6,
1608 IP_PROTOCOL_SCTP);
Marco Varlese191a5942017-10-30 18:17:21 +01001609
1610 int bogus = ~0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001611 u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
Marco Varlese191a5942017-10-30 18:17:21 +01001612 ASSERT (!bogus);
1613
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001614 sctp_hdr = ip6_next_header (iph6);
Marco Varlese191a5942017-10-30 18:17:21 +01001615 sctp_hdr->checksum = checksum;
1616
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001617 vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001618 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001619
1620#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001621 packet_length = clib_net_to_host_u16 (iph6->payload_length);
Marco Varlese191a5942017-10-30 18:17:21 +01001622#endif
1623 }
1624
Marco Varlesefae40392018-02-14 15:38:35 +01001625 sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1626 u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1627 if (chunk_type >= UNKNOWN)
1628 {
1629 clib_warning
1630 ("Trying to send an unrecognized chunk... something is really bad.");
1631 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1632 next0 = SCTP_OUTPUT_NEXT_DROP;
1633 goto done;
1634 }
1635
1636#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese191a5942017-10-30 18:17:21 +01001637 u8 is_valid =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001638 (sctp_conn->sub_conn[idx].connection.lcl_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001639 sctp_hdr->src_port
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001640 || sctp_conn->sub_conn[idx].connection.lcl_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001641 sctp_hdr->dst_port)
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001642 && (sctp_conn->sub_conn[idx].connection.rmt_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001643 sctp_hdr->dst_port
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001644 || sctp_conn->sub_conn[idx].connection.rmt_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001645 sctp_hdr->src_port);
1646
Marco Varlese191a5942017-10-30 18:17:21 +01001647 if (!is_valid)
1648 {
1649 SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1650 "packet_length = %u, "
1651 "chunk_type = %u [%s], "
1652 "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1653 "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
Marco Varlesefae40392018-02-14 15:38:35 +01001654 sctp_conn->sub_conn[idx].
1655 connection.c_index, packet_length,
Marco Varlese191a5942017-10-30 18:17:21 +01001656 chunk_type,
1657 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001658 sctp_conn->sub_conn[idx].
1659 connection.lcl_port, sctp_hdr->src_port,
1660 sctp_conn->sub_conn[idx].
1661 connection.rmt_port,
Marco Varlese191a5942017-10-30 18:17:21 +01001662 sctp_hdr->dst_port);
1663
1664 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1665 next0 = SCTP_OUTPUT_NEXT_DROP;
1666 goto done;
1667 }
Marco Varlesefae40392018-02-14 15:38:35 +01001668#endif
Marco Varlese191a5942017-10-30 18:17:21 +01001669 SCTP_DBG_STATE_MACHINE
Marco Varlesef3ab4892018-02-19 15:23:13 +01001670 ("SESSION_INDEX = %u, CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
Marco Varlese191a5942017-10-30 18:17:21 +01001671 "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
Marco Varlesef3ab4892018-02-19 15:23:13 +01001672 sctp_conn->sub_conn[idx].connection.s_index,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001673 sctp_conn->sub_conn[idx].connection.c_index,
1674 sctp_conn->state, sctp_state_to_string (sctp_conn->state),
Marco Varlese191a5942017-10-30 18:17:21 +01001675 sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1676 full_hdr->hdr.dst_port);
1677
Marco Varlese191a5942017-10-30 18:17:21 +01001678 /* Let's make sure the state-machine does not send anything crazy */
Marco Varlesea38783e2018-02-13 12:38:52 +01001679#if SCTP_DEBUG_STATE_MACHINE
1680 if (sctp_validate_output_state_machine (sctp_conn, chunk_type) != 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001681 {
Marco Varlese191a5942017-10-30 18:17:21 +01001682 SCTP_DBG_STATE_MACHINE
Marco Varlesea38783e2018-02-13 12:38:52 +01001683 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
Marco Varlese191a5942017-10-30 18:17:21 +01001684 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001685 sctp_state_to_string (sctp_conn->state));
Marco Varlesea38783e2018-02-13 12:38:52 +01001686
1687 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1688 next0 = SCTP_OUTPUT_NEXT_DROP;
1689 goto done;
Marco Varlesef3ab4892018-02-19 15:23:13 +01001690
Marco Varlesea38783e2018-02-13 12:38:52 +01001691 }
1692#endif
1693
1694 /* Karn's algorithm: RTT measurements MUST NOT be made using
1695 * packets that were retransmitted
1696 */
1697 if (!sctp_is_retransmitting (sctp_conn, idx))
1698 {
1699 /* Measure RTT with this */
1700 if (chunk_type == DATA
1701 && sctp_conn->sub_conn[idx].RTO_pending == 0)
1702 {
1703 sctp_conn->sub_conn[idx].RTO_pending = 1;
1704 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1705 }
1706 else
1707 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +01001708 }
1709
Marco Varlesea38783e2018-02-13 12:38:52 +01001710 /* Let's take care of TIMERS */
Marco Varlesebe2251b2018-02-07 12:22:41 +01001711 switch (chunk_type)
Marco Varlese191a5942017-10-30 18:17:21 +01001712 {
Marco Varlesea38783e2018-02-13 12:38:52 +01001713 case COOKIE_ECHO:
1714 {
1715 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1716 break;
1717 }
Marco Varlesebe2251b2018-02-07 12:22:41 +01001718 case DATA:
1719 {
Marco Varlesea38783e2018-02-13 12:38:52 +01001720 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1721
Marco Varlesebe2251b2018-02-07 12:22:41 +01001722 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1723 sctp_conn->sub_conn[idx].RTO);
1724 break;
1725 }
1726 case SHUTDOWN:
1727 {
1728 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1729 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1730 sctp_conn->sub_conn[idx].RTO);
1731 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1732 break;
1733 }
1734 case SHUTDOWN_ACK:
1735 {
1736 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1737 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1738 sctp_conn->sub_conn[idx].RTO);
1739 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1740 break;
1741 }
Marco Varlesea38783e2018-02-13 12:38:52 +01001742 case SHUTDOWN_COMPLETE:
1743 {
1744 sctp_conn->state = SCTP_STATE_CLOSED;
1745 break;
1746 }
Marco Varlese191a5942017-10-30 18:17:21 +01001747 }
1748
1749 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1750 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1751
1752 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1753
Marco Varlesef3ab4892018-02-19 15:23:13 +01001754 SCTP_DBG_STATE_MACHINE
1755 ("SESSION_INDEX = %u, CONNECTION_INDEX = %u, " "NEW_STATE = %s, "
1756 "CHUNK_SENT = %s", sctp_conn->sub_conn[idx].connection.s_index,
1757 sctp_conn->sub_conn[idx].connection.c_index,
1758 sctp_state_to_string (sctp_conn->state),
1759 sctp_chunk_to_string (chunk_type));
Marco Varlese191a5942017-10-30 18:17:21 +01001760
1761 vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1762
1763 done:
1764 b0->error = node->errors[error0];
1765 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1766 {
1767 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1768 if (th0)
1769 {
1770 clib_memcpy (&t0->sctp_header, th0,
1771 sizeof (t0->sctp_header));
1772 }
1773 else
1774 {
1775 memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1776 }
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001777 clib_memcpy (&t0->sctp_connection, sctp_conn,
Marco Varlese191a5942017-10-30 18:17:21 +01001778 sizeof (t0->sctp_connection));
1779 }
1780
1781 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1782 n_left_to_next, bi0, next0);
1783 }
1784
1785 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1786 }
1787
1788 return from_frame->n_vectors;
1789}
1790
1791static uword
1792sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1793 vlib_frame_t * from_frame)
1794{
1795 return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1796}
1797
1798static uword
1799sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1800 vlib_frame_t * from_frame)
1801{
1802 return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1803}
1804
1805/* *INDENT-OFF* */
1806VLIB_REGISTER_NODE (sctp4_output_node) =
1807{
1808 .function = sctp4_output,.name = "sctp4-output",
1809 /* Takes a vector of packets. */
1810 .vector_size = sizeof (u32),
1811 .n_errors = SCTP_N_ERROR,
1812 .error_strings = sctp_error_strings,
1813 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1814 .next_nodes = {
1815#define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1816 foreach_sctp4_output_next
1817#undef _
1818 },
1819 .format_buffer = format_sctp_header,
1820 .format_trace = format_sctp_tx_trace,
1821};
1822/* *INDENT-ON* */
1823
1824VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1825
1826/* *INDENT-OFF* */
1827VLIB_REGISTER_NODE (sctp6_output_node) =
1828{
1829 .function = sctp6_output,
1830 .name = "sctp6-output",
1831 /* Takes a vector of packets. */
1832 .vector_size = sizeof (u32),
1833 .n_errors = SCTP_N_ERROR,
1834 .error_strings = sctp_error_strings,
1835 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1836 .next_nodes = {
1837#define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1838 foreach_sctp6_output_next
1839#undef _
1840 },
1841 .format_buffer = format_sctp_header,
1842 .format_trace = format_sctp_tx_trace,
1843};
1844/* *INDENT-ON* */
1845
1846VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1847
1848/*
1849 * fd.io coding-style-patch-verification: ON
1850 *
1851 * Local Variables:
1852 * eval: (c-set-style "gnu")
1853 * End:
1854 */