blob: 17b28a86492708e95041950fc78498ca6ff35cac [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);
Marco Varlese191a5942017-10-30 18:17:21 +0100274 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
275 b->total_length_not_including_first_buffer = 0;
276 vnet_buffer (b)->sctp.flags = 0;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100277 vnet_buffer (b)->sctp.subconn_idx = MAX_SCTP_CONNECTIONS;
Marco Varlese191a5942017-10-30 18:17:21 +0100278 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
279 /* Leave enough space for headers */
280 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
281}
282
283always_inline int
284sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
285{
286 vlib_main_t *vm = vlib_get_main ();
287 u32 current_length = vec_len (tm->tx_buffers[thread_index]);
288 u32 n_allocated;
289
290 vec_validate (tm->tx_buffers[thread_index],
291 current_length + n_free_buffers - 1);
292 n_allocated =
293 vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
294 n_free_buffers);
295 _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
296 /* buffer shortage, report failure */
297 if (vec_len (tm->tx_buffers[thread_index]) == 0)
298 {
299 clib_warning ("out of buffers");
300 return -1;
301 }
302 return 0;
303}
304
305always_inline int
306sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
307{
308 u32 *my_tx_buffers;
309 u32 thread_index = vlib_get_thread_index ();
310 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
311 {
312 if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
313 return -1;
314 }
315 my_tx_buffers = tm->tx_buffers[thread_index];
316 *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
317 _vec_len (my_tx_buffers) -= 1;
318 return 0;
319}
320
321always_inline void
322sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
323 u8 is_ip4, u8 flush)
324{
325 sctp_main_t *tm = vnet_get_sctp_main ();
326 u32 thread_index = vlib_get_thread_index ();
327 u32 *to_next, next_index;
328 vlib_frame_t *f;
329
330 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
331 b->error = 0;
332
333 /* Decide where to send the packet */
334 next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
335 sctp_trajectory_add_start (b, 2);
336
337 /* Get frame to v4/6 output node */
338 f = tm->tx_frames[!is_ip4][thread_index];
339 if (!f)
340 {
341 f = vlib_get_frame_to_node (vm, next_index);
342 ASSERT (f);
343 tm->tx_frames[!is_ip4][thread_index] = f;
344 }
345 to_next = vlib_frame_vector_args (f);
346 to_next[f->n_vectors] = bi;
347 f->n_vectors += 1;
348 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
349 {
350 vlib_put_frame_to_node (vm, next_index, f);
351 tm->tx_frames[!is_ip4][thread_index] = 0;
352 }
353}
354
355always_inline void
356sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
357 u8 is_ip4)
358{
359 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
360}
361
362always_inline void
363sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
Marco Varlesee17bb712018-03-28 12:06:10 +0200364 u8 is_ip4, u32 fib_index, u8 flush)
Marco Varlese191a5942017-10-30 18:17:21 +0100365{
366 sctp_main_t *tm = vnet_get_sctp_main ();
367 u32 thread_index = vlib_get_thread_index ();
368 u32 *to_next, next_index;
369 vlib_frame_t *f;
370
371 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
372 b->error = 0;
373
Marco Varlesee17bb712018-03-28 12:06:10 +0200374 vnet_buffer (b)->sw_if_index[VLIB_TX] = fib_index;
375 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
Marco Varlese191a5942017-10-30 18:17:21 +0100376
377 /* Send to IP lookup */
378 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
379 if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
380 {
381 b->pre_data[0] = 2;
382 b->pre_data[1] = next_index;
383 }
384
385 f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
386 if (!f)
387 {
388 f = vlib_get_frame_to_node (vm, next_index);
389 ASSERT (f);
390 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
391 }
392
393 to_next = vlib_frame_vector_args (f);
394 to_next[f->n_vectors] = bi;
395 f->n_vectors += 1;
396 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
397 {
398 vlib_put_frame_to_node (vm, next_index, f);
399 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
400 }
401}
402
403always_inline void
404sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
Marco Varlesee17bb712018-03-28 12:06:10 +0200405 u8 is_ip4, u32 fib_index)
Marco Varlese191a5942017-10-30 18:17:21 +0100406{
Marco Varlesee17bb712018-03-28 12:06:10 +0200407 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, fib_index, 0);
Florin Corasfd542f12018-05-16 19:28:24 -0700408 if (vm->thread_index == 0 && vlib_num_workers ())
409 session_flush_frames_main_thread (vm);
Marco Varlese191a5942017-10-30 18:17:21 +0100410}
411
Marco Varlese191a5942017-10-30 18:17:21 +0100412/**
413 * Convert buffer to INIT
414 */
415void
Marco Varlese54432f82018-02-15 17:01:56 +0100416sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, u8 idx,
417 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100418{
419 u32 random_seed = random_default_seed ();
420 u16 alloc_bytes = sizeof (sctp_init_chunk_t);
Marco Varlese54432f82018-02-15 17:01:56 +0100421 sctp_sub_connection_t *sub_conn = &sctp_conn->sub_conn[idx];
Marco Varlese191a5942017-10-30 18:17:21 +0100422
423 sctp_ipv4_addr_param_t *ip4_param = 0;
424 sctp_ipv6_addr_param_t *ip6_param = 0;
425
426 if (sub_conn->c_is_ip4)
427 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
428 else
429 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
430
431 /* As per RFC 4960 the chunk_length value does NOT contemplate
432 * the size of the first header (see sctp_header_t) and any padding
433 */
434 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
435
436 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
437
438 sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
439
440 u16 pointer_offset = sizeof (init_chunk);
441 if (sub_conn->c_is_ip4)
442 {
443 ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
444 ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
445
446 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
447 }
448 else
449 {
450 ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
451 ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
452 ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
453
454 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
455 }
456
457 init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
458 init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
459 init_chunk->sctp_hdr.checksum = 0;
460 /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
461 init_chunk->sctp_hdr.verification_tag = 0x0;
462
463 vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
464 vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
465 vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
466
Marco Varlese6e4d4a32018-03-12 12:36:59 +0100467 sctp_init_cwnd (sctp_conn);
468
Marco Varlesef3ab4892018-02-19 15:23:13 +0100469 init_chunk->a_rwnd = clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
Marco Varlese191a5942017-10-30 18:17:21 +0100470 init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
471 init_chunk->inboud_streams_count =
472 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
473 init_chunk->outbound_streams_count =
474 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
475
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100476 init_chunk->initial_tsn =
477 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
478 SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
479 sctp_conn->local_initial_tsn);
480
Marco Varlese191a5942017-10-30 18:17:21 +0100481 sctp_conn->local_tag = init_chunk->initiate_tag;
482
483 vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100484 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100485
486 SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
487 "CHUNK_TYPE = %s, "
488 "SRC_PORT = %u, DST_PORT = %u",
489 sub_conn->connection.c_index,
490 sctp_conn->state,
491 sctp_state_to_string (sctp_conn->state),
492 sctp_chunk_to_string (INIT),
493 init_chunk->sctp_hdr.src_port,
494 init_chunk->sctp_hdr.dst_port);
495}
496
Marco Varlese91389ac2018-01-31 11:00:01 +0100497void
498sctp_compute_mac (sctp_connection_t * sctp_conn,
499 sctp_state_cookie_param_t * state_cookie)
Marco Varlese191a5942017-10-30 18:17:21 +0100500{
Marco Varlese91389ac2018-01-31 11:00:01 +0100501#if OPENSSL_VERSION_NUMBER >= 0x10100000L
502 HMAC_CTX *ctx;
503#else
504 HMAC_CTX ctx;
Marco Varlese91389ac2018-01-31 11:00:01 +0100505#endif
506 unsigned int len = 0;
Marco Varlese18b63152018-02-12 09:08:21 +0100507 const EVP_MD *md = EVP_sha1 ();
Marco Varlese91389ac2018-01-31 11:00:01 +0100508#if OPENSSL_VERSION_NUMBER >= 0x10100000L
509 ctx = HMAC_CTX_new ();
Marco Varlese18b63152018-02-12 09:08:21 +0100510 HMAC_Init_ex (ctx, &state_cookie->creation_time,
Marco Varlese91389ac2018-01-31 11:00:01 +0100511 sizeof (state_cookie->creation_time), md, NULL);
512 HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
513 HMAC_Final (ctx, state_cookie->mac, &len);
514#else
515 HMAC_CTX_init (&ctx);
516 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
517 sizeof (state_cookie->creation_time), md, NULL);
Marco Varlese91389ac2018-01-31 11:00:01 +0100518 HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
519 HMAC_Final (&ctx, state_cookie->mac, &len);
520 HMAC_CTX_cleanup (&ctx);
521#endif
522
523 ENDIANESS_SWAP (state_cookie->mac);
Marco Varlese191a5942017-10-30 18:17:21 +0100524}
525
526void
Marco Varlese54432f82018-02-15 17:01:56 +0100527sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100528 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100529{
530 vlib_main_t *vm = vlib_get_main ();
Marco Varlese191a5942017-10-30 18:17:21 +0100531
532 sctp_reuse_buffer (vm, b);
533
534 u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
535
536 /* As per RFC 4960 the chunk_length value does NOT contemplate
537 * the size of the first header (see sctp_header_t) and any padding
538 */
539 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
540
541 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
542
543 sctp_cookie_ack_chunk_t *cookie_ack_chunk =
544 vlib_buffer_push_uninit (b, alloc_bytes);
545
546 cookie_ack_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100547 cookie_ack_chunk->sctp_hdr.src_port =
548 sctp_conn->sub_conn[idx].connection.lcl_port;
549 cookie_ack_chunk->sctp_hdr.dst_port =
550 sctp_conn->sub_conn[idx].connection.rmt_port;
551 cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100552 vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
553 vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
554
555 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100556 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100557 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100558}
559
560void
Marco Varlese54432f82018-02-15 17:01:56 +0100561sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese9e09ff32018-03-05 12:31:45 +0100562 vlib_buffer_t * b, u8 reuse_buffer)
Marco Varlese191a5942017-10-30 18:17:21 +0100563{
564 vlib_main_t *vm = vlib_get_main ();
Marco Varlese191a5942017-10-30 18:17:21 +0100565
Marco Varlese9e09ff32018-03-05 12:31:45 +0100566 if (reuse_buffer)
567 sctp_reuse_buffer (vm, b);
Marco Varlese191a5942017-10-30 18:17:21 +0100568
569 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
570 u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
571 /* As per RFC 4960 the chunk_length value does NOT contemplate
572 * the size of the first header (see sctp_header_t) and any padding
573 */
574 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
575 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
576 sctp_cookie_echo_chunk_t *cookie_echo_chunk =
577 vlib_buffer_push_uninit (b, alloc_bytes);
578 cookie_echo_chunk->sctp_hdr.checksum = 0;
579 cookie_echo_chunk->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100580 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +0100581 cookie_echo_chunk->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100582 sctp_conn->sub_conn[idx].connection.rmt_port;
583 cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100584 vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
585 vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
Dave Barach178cf492018-11-13 16:34:13 -0500586 clib_memcpy_fast (&(cookie_echo_chunk->cookie), &sctp_conn->cookie_param,
587 sizeof (sctp_state_cookie_param_t));
Marco Varlese21c8baf2018-02-02 17:17:51 +0100588
Marco Varlese191a5942017-10-30 18:17:21 +0100589 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100590 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +0100591 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +0100592}
593
Marco Varlese9e09ff32018-03-05 12:31:45 +0100594
595/*
596 * Send COOKIE_ECHO
597 */
598void
599sctp_send_cookie_echo (sctp_connection_t * sctp_conn)
600{
601 vlib_buffer_t *b;
602 u32 bi;
603 sctp_main_t *tm = vnet_get_sctp_main ();
604 vlib_main_t *vm = vlib_get_main ();
605
606 if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS))
607 {
608 clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection.");
609
610 session_stream_connect_notify (&sctp_conn->sub_conn
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100611 [SCTP_PRIMARY_PATH_IDX].connection, 1);
Marco Varlese9e09ff32018-03-05 12:31:45 +0100612
613 sctp_connection_timers_reset (sctp_conn);
614
615 sctp_connection_cleanup (sctp_conn);
616 }
617
618 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
619 return;
620
621 b = vlib_get_buffer (vm, bi);
Marco Varlesec7fe4f32018-03-05 15:12:29 +0100622 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese9e09ff32018-03-05 12:31:45 +0100623
624 sctp_init_buffer (vm, b);
625 sctp_prepare_cookie_echo_chunk (sctp_conn, idx, b, 0);
626 sctp_enqueue_to_output_now (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
627
628 /* Start the T1_INIT timer */
629 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
630 sctp_conn->sub_conn[idx].RTO);
631
632 /* Change state to COOKIE_WAIT */
633 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
634
635 /* Measure RTT with this */
636 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
637}
638
639
Marco Varlese191a5942017-10-30 18:17:21 +0100640/**
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100641 * Convert buffer to ERROR
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100642 */
Marco Varlese200fa322018-02-26 16:33:54 +0100643void
644sctp_prepare_operation_error (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100645 vlib_buffer_t * b, u8 err_cause)
Marco Varlese200fa322018-02-26 16:33:54 +0100646{
647 vlib_main_t *vm = vlib_get_main ();
648
649 sctp_reuse_buffer (vm, b);
650
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100651 /* The minimum size of the message is given by the sctp_operation_error_t */
652 u16 alloc_bytes =
653 sizeof (sctp_operation_error_t) + sizeof (sctp_err_cause_param_t);
Marco Varlese200fa322018-02-26 16:33:54 +0100654
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100655 /* As per RFC 4960 the chunk_length value does NOT contemplate
656 * the size of the first header (see sctp_header_t) and any padding
657 */
Marco Varlese200fa322018-02-26 16:33:54 +0100658 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
659
660 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
661
662 sctp_operation_error_t *err_chunk =
663 vlib_buffer_push_uninit (b, alloc_bytes);
664
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100665 /* src_port & dst_port are already in network byte-order */
Marco Varlese200fa322018-02-26 16:33:54 +0100666 err_chunk->sctp_hdr.checksum = 0;
667 err_chunk->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
668 err_chunk->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100669 /* 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 +0100670 err_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
671
Marco Varlese8c5f67f2018-02-27 09:38:31 +0100672 err_chunk->err_causes[0].param_hdr.length =
673 clib_host_to_net_u16 (sizeof (err_chunk->err_causes[0].param_hdr.type) +
674 sizeof (err_chunk->err_causes[0].param_hdr.length));
675 err_chunk->err_causes[0].param_hdr.type = clib_host_to_net_u16 (err_cause);
676
Marco Varlese200fa322018-02-26 16:33:54 +0100677 vnet_sctp_set_chunk_type (&err_chunk->chunk_hdr, OPERATION_ERROR);
678 vnet_sctp_set_chunk_length (&err_chunk->chunk_hdr, chunk_len);
679
680 vnet_buffer (b)->sctp.connection_index =
681 sctp_conn->sub_conn[idx].connection.c_index;
682 vnet_buffer (b)->sctp.subconn_idx = idx;
683}
Marco Varlese200fa322018-02-26 16:33:54 +0100684
685/**
686 * Convert buffer to ABORT
687 */
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100688void
689sctp_prepare_abort_for_collision (sctp_connection_t * sctp_conn, u8 idx,
690 vlib_buffer_t * b, ip4_address_t * ip4_addr,
691 ip6_address_t * ip6_addr)
692{
693 vlib_main_t *vm = vlib_get_main ();
694
695 sctp_reuse_buffer (vm, b);
696
697 /* The minimum size of the message is given by the sctp_abort_chunk_t */
698 u16 alloc_bytes = sizeof (sctp_abort_chunk_t);
699
700 /* As per RFC 4960 the chunk_length value does NOT contemplate
701 * the size of the first header (see sctp_header_t) and any padding
702 */
703 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
704
705 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
706
707 sctp_abort_chunk_t *abort_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
708
709 /* src_port & dst_port are already in network byte-order */
710 abort_chunk->sctp_hdr.checksum = 0;
711 abort_chunk->sctp_hdr.src_port =
712 sctp_conn->sub_conn[idx].connection.lcl_port;
713 abort_chunk->sctp_hdr.dst_port =
714 sctp_conn->sub_conn[idx].connection.rmt_port;
715 /* As per RFC4960 Section 5.2.2: copy the INITIATE_TAG into the VERIFICATION_TAG of the ABORT chunk */
716 abort_chunk->sctp_hdr.verification_tag = sctp_conn->local_tag;
717
718 vnet_sctp_set_chunk_type (&abort_chunk->chunk_hdr, ABORT);
719 vnet_sctp_set_chunk_length (&abort_chunk->chunk_hdr, chunk_len);
720
721 vnet_buffer (b)->sctp.connection_index =
722 sctp_conn->sub_conn[idx].connection.c_index;
723 vnet_buffer (b)->sctp.subconn_idx = idx;
724}
725
726/**
727 * Convert buffer to INIT-ACK
728 */
729void
730sctp_prepare_initack_chunk_for_collision (sctp_connection_t * sctp_conn,
731 u8 idx, vlib_buffer_t * b,
732 ip4_address_t * ip4_addr,
733 ip6_address_t * ip6_addr)
734{
735 vlib_main_t *vm = vlib_get_main ();
736 sctp_ipv4_addr_param_t *ip4_param = 0;
737 sctp_ipv6_addr_param_t *ip6_param = 0;
738
739 sctp_reuse_buffer (vm, b);
740
741 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
742 u16 alloc_bytes =
743 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
744
745 if (PREDICT_TRUE (ip4_addr != NULL))
746 {
747 /* Create room for variable-length fields in the INIT_ACK chunk */
748 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
749 }
750 if (PREDICT_TRUE (ip6_addr != NULL))
751 {
752 /* Create room for variable-length fields in the INIT_ACK chunk */
753 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
754 }
755
756 if (sctp_conn->sub_conn[idx].connection.is_ip4)
757 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
758 else
759 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
760
761 /* As per RFC 4960 the chunk_length value does NOT contemplate
762 * the size of the first header (see sctp_header_t) and any padding
763 */
764 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
765
766 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
767
768 sctp_init_ack_chunk_t *init_ack_chunk =
769 vlib_buffer_push_uninit (b, alloc_bytes);
770
771 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
772
773 /* Create State Cookie parameter */
774 sctp_state_cookie_param_t *state_cookie_param =
775 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
776
777 state_cookie_param->param_hdr.type =
778 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
779 state_cookie_param->param_hdr.length =
780 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
Marco Varlese93826732018-09-27 16:43:57 +0200781 state_cookie_param->creation_time = clib_host_to_net_u64 (sctp_time_now ());
Marco Varleseeacf3cf2018-02-26 14:52:25 +0100782 state_cookie_param->cookie_lifespan =
783 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
784
785 sctp_compute_mac (sctp_conn, state_cookie_param);
786
787 pointer_offset += sizeof (sctp_state_cookie_param_t);
788
789 if (PREDICT_TRUE (ip4_addr != NULL))
790 {
791 sctp_ipv4_addr_param_t *ipv4_addr =
792 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
793
794 ipv4_addr->param_hdr.type =
795 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
796 ipv4_addr->param_hdr.length =
797 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
798 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
799
800 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
801 }
802 if (PREDICT_TRUE (ip6_addr != NULL))
803 {
804 sctp_ipv6_addr_param_t *ipv6_addr =
805 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
806
807 ipv6_addr->param_hdr.type =
808 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
809 ipv6_addr->param_hdr.length =
810 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
811 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
812 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
813
814 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
815 }
816
817 if (sctp_conn->sub_conn[idx].connection.is_ip4)
818 {
819 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
820 ip4_param->address.as_u32 =
821 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
822
823 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
824 }
825 else
826 {
827 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
828 ip6_param->address.as_u64[0] =
829 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
830 ip6_param->address.as_u64[1] =
831 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
832
833 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
834 }
835
836 /* src_port & dst_port are already in network byte-order */
837 init_ack_chunk->sctp_hdr.checksum = 0;
838 init_ack_chunk->sctp_hdr.src_port =
839 sctp_conn->sub_conn[idx].connection.lcl_port;
840 init_ack_chunk->sctp_hdr.dst_port =
841 sctp_conn->sub_conn[idx].connection.rmt_port;
842 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
843 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
844 init_ack_chunk->initial_tsn =
845 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
846 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
847 init_ack_chunk->initial_tsn);
848
849 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
850 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
851
852 init_ack_chunk->initiate_tag = sctp_conn->local_tag;
853
854 init_ack_chunk->a_rwnd =
855 clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
856 init_ack_chunk->inboud_streams_count =
857 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
858 init_ack_chunk->outbound_streams_count =
859 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
860
861 vnet_buffer (b)->sctp.connection_index =
862 sctp_conn->sub_conn[idx].connection.c_index;
863 vnet_buffer (b)->sctp.subconn_idx = idx;
864}
865
866/**
Marco Varlese191a5942017-10-30 18:17:21 +0100867 * Convert buffer to INIT-ACK
868 */
869void
Marco Varlese54432f82018-02-15 17:01:56 +0100870sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, u8 idx,
871 vlib_buffer_t * b, ip4_address_t * ip4_addr,
Marco Varlese216c35b2018-04-17 16:41:51 +0200872 u8 add_ip4, ip6_address_t * ip6_addr, u8 add_ip6)
Marco Varlese191a5942017-10-30 18:17:21 +0100873{
874 vlib_main_t *vm = vlib_get_main ();
875 sctp_ipv4_addr_param_t *ip4_param = 0;
876 sctp_ipv6_addr_param_t *ip6_param = 0;
Marco Varlese191a5942017-10-30 18:17:21 +0100877 u32 random_seed = random_default_seed ();
878
879 sctp_reuse_buffer (vm, b);
880
881 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
882 u16 alloc_bytes =
883 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
884
Marco Varlese216c35b2018-04-17 16:41:51 +0200885 if (PREDICT_FALSE (add_ip4 == 1))
Marco Varlese191a5942017-10-30 18:17:21 +0100886 {
887 /* Create room for variable-length fields in the INIT_ACK chunk */
888 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
889 }
Marco Varlese216c35b2018-04-17 16:41:51 +0200890 if (PREDICT_FALSE (add_ip6 == 1))
Marco Varlese191a5942017-10-30 18:17:21 +0100891 {
892 /* Create room for variable-length fields in the INIT_ACK chunk */
893 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
894 }
895
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100896 if (sctp_conn->sub_conn[idx].connection.is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100897 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
898 else
899 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
900
901 /* As per RFC 4960 the chunk_length value does NOT contemplate
902 * the size of the first header (see sctp_header_t) and any padding
903 */
904 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
905
906 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
907
908 sctp_init_ack_chunk_t *init_ack_chunk =
909 vlib_buffer_push_uninit (b, alloc_bytes);
910
911 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
912
913 /* Create State Cookie parameter */
914 sctp_state_cookie_param_t *state_cookie_param =
915 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
916
917 state_cookie_param->param_hdr.type =
918 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
919 state_cookie_param->param_hdr.length =
920 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
Marco Varlese93826732018-09-27 16:43:57 +0200921 state_cookie_param->creation_time = clib_host_to_net_u64 (sctp_time_now ());
Marco Varlese191a5942017-10-30 18:17:21 +0100922 state_cookie_param->cookie_lifespan =
923 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
Marco Varlese91389ac2018-01-31 11:00:01 +0100924
925 sctp_compute_mac (sctp_conn, state_cookie_param);
Marco Varlese191a5942017-10-30 18:17:21 +0100926
927 pointer_offset += sizeof (sctp_state_cookie_param_t);
928
929 if (PREDICT_TRUE (ip4_addr != NULL))
930 {
931 sctp_ipv4_addr_param_t *ipv4_addr =
932 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
933
934 ipv4_addr->param_hdr.type =
935 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
936 ipv4_addr->param_hdr.length =
937 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
938 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
939
940 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
941 }
942 if (PREDICT_TRUE (ip6_addr != NULL))
943 {
944 sctp_ipv6_addr_param_t *ipv6_addr =
Marco Varlesef429a932018-02-06 17:31:06 +0100945 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
Marco Varlese191a5942017-10-30 18:17:21 +0100946
947 ipv6_addr->param_hdr.type =
948 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
949 ipv6_addr->param_hdr.length =
950 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
951 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
952 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
953
954 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
955 }
956
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100957 if (sctp_conn->sub_conn[idx].connection.is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100958 {
959 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
960 ip4_param->address.as_u32 =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100961 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
Marco Varlese191a5942017-10-30 18:17:21 +0100962
963 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
964 }
965 else
966 {
967 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
968 ip6_param->address.as_u64[0] =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100969 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
Marco Varlese191a5942017-10-30 18:17:21 +0100970 ip6_param->address.as_u64[1] =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100971 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
Marco Varlese191a5942017-10-30 18:17:21 +0100972
973 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
974 }
975
976 /* src_port & dst_port are already in network byte-order */
977 init_ack_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100978 init_ack_chunk->sctp_hdr.src_port =
979 sctp_conn->sub_conn[idx].connection.lcl_port;
980 init_ack_chunk->sctp_hdr.dst_port =
981 sctp_conn->sub_conn[idx].connection.rmt_port;
982 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
983 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
984 init_ack_chunk->initial_tsn =
985 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
986 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
987 init_ack_chunk->initial_tsn);
Marco Varlese191a5942017-10-30 18:17:21 +0100988
989 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
990 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
991
992 init_ack_chunk->initiate_tag =
993 clib_host_to_net_u32 (random_u32 (&random_seed));
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100994
Marco Varlesef3ab4892018-02-19 15:23:13 +0100995 init_ack_chunk->a_rwnd =
996 clib_host_to_net_u32 (sctp_conn->sub_conn[idx].cwnd);
Marco Varlese191a5942017-10-30 18:17:21 +0100997 init_ack_chunk->inboud_streams_count =
998 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
999 init_ack_chunk->outbound_streams_count =
1000 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
1001
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001002 sctp_conn->local_tag = init_ack_chunk->initiate_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001003
1004 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001005 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001006 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001007}
1008
1009/**
1010 * Convert buffer to SHUTDOWN
1011 */
1012void
Marco Varlese54432f82018-02-15 17:01:56 +01001013sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, u8 idx,
1014 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001015{
Marco Varlese191a5942017-10-30 18:17:21 +01001016 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1017
Marco Varlese191a5942017-10-30 18:17:21 +01001018 /* As per RFC 4960 the chunk_length value does NOT contemplate
1019 * the size of the first header (see sctp_header_t) and any padding
1020 */
1021 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1022
1023 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1024
1025 sctp_shutdown_association_chunk_t *shutdown_chunk =
1026 vlib_buffer_push_uninit (b, alloc_bytes);
1027
1028 shutdown_chunk->sctp_hdr.checksum = 0;
1029 /* No need of host_to_net conversion, already in net-byte order */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001030 shutdown_chunk->sctp_hdr.src_port =
1031 sctp_conn->sub_conn[idx].connection.lcl_port;
1032 shutdown_chunk->sctp_hdr.dst_port =
1033 sctp_conn->sub_conn[idx].connection.rmt_port;
1034 shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001035 vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
1036 vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
1037
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001038 shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
Marco Varlese191a5942017-10-30 18:17:21 +01001039
1040 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001041 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001042 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001043}
1044
1045/*
1046 * Send SHUTDOWN
1047 */
1048void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001049sctp_send_shutdown (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +01001050{
1051 vlib_buffer_t *b;
1052 u32 bi;
1053 sctp_main_t *tm = vnet_get_sctp_main ();
1054 vlib_main_t *vm = vlib_get_main ();
1055
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001056 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001057 return;
1058
1059 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1060 return;
1061
Marco Varlesec7fe4f32018-03-05 15:12:29 +01001062 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese54432f82018-02-15 17:01:56 +01001063
Marco Varlese191a5942017-10-30 18:17:21 +01001064 b = vlib_get_buffer (vm, bi);
1065 sctp_init_buffer (vm, b);
Marco Varlese54432f82018-02-15 17:01:56 +01001066 sctp_prepare_shutdown_chunk (sctp_conn, idx, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001067
Marco Varlesebe2251b2018-02-07 12:22:41 +01001068 sctp_enqueue_to_output_now (vm, b, bi,
1069 sctp_conn->sub_conn[idx].connection.is_ip4);
Marco Varlese191a5942017-10-30 18:17:21 +01001070}
1071
1072/**
1073 * Convert buffer to SHUTDOWN_ACK
1074 */
1075void
Marco Varlese54432f82018-02-15 17:01:56 +01001076sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001077 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001078{
Marco Varlese191a5942017-10-30 18:17:21 +01001079 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1080 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1081
1082 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1083
1084 sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
1085 vlib_buffer_push_uninit (b, alloc_bytes);
1086
1087 shutdown_ack_chunk->sctp_hdr.checksum = 0;
1088 /* No need of host_to_net conversion, already in net-byte order */
1089 shutdown_ack_chunk->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001090 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +01001091 shutdown_ack_chunk->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001092 sctp_conn->sub_conn[idx].connection.rmt_port;
1093 shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001094
1095 vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
1096 vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
1097
1098 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001099 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001100 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001101}
1102
1103/*
1104 * Send SHUTDOWN_ACK
1105 */
1106void
Marco Varlese54432f82018-02-15 17:01:56 +01001107sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, u8 idx,
1108 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001109{
Marco Varlese191a5942017-10-30 18:17:21 +01001110 vlib_main_t *vm = vlib_get_main ();
1111
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001112 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001113 return;
1114
Marco Varlesebe2251b2018-02-07 12:22:41 +01001115 sctp_reuse_buffer (vm, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001116
Marco Varlese54432f82018-02-15 17:01:56 +01001117 sctp_prepare_shutdown_ack_chunk (sctp_conn, idx, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001118}
1119
1120/**
1121 * Convert buffer to SACK
1122 */
1123void
Marco Varlese54432f82018-02-15 17:01:56 +01001124sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, u8 idx,
1125 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001126{
1127 vlib_main_t *vm = vlib_get_main ();
Marco Varlese191a5942017-10-30 18:17:21 +01001128
1129 sctp_reuse_buffer (vm, b);
1130
1131 u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
1132
1133 /* As per RFC 4960 the chunk_length value does NOT contemplate
1134 * the size of the first header (see sctp_header_t) and any padding
1135 */
1136 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1137
1138 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1139
1140 sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
1141
1142 sack->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001143 sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1144 sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1145 sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001146 vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
1147 vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
1148
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001149 sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
1150
1151 sctp_conn->ack_state = 0;
1152
Marco Varlese191a5942017-10-30 18:17:21 +01001153 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001154 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001155 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001156}
1157
1158/**
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001159 * Convert buffer to HEARTBEAT_ACK
1160 */
1161void
Marco Varlese54432f82018-02-15 17:01:56 +01001162sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001163 vlib_buffer_t * b)
1164{
1165 vlib_main_t *vm = vlib_get_main ();
1166
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001167 u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
1168
1169 sctp_reuse_buffer (vm, b);
1170
1171 /* As per RFC 4960 the chunk_length value does NOT contemplate
1172 * the size of the first header (see sctp_header_t) and any padding
1173 */
1174 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1175
1176 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1177
1178 sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
1179
1180 hb_ack->sctp_hdr.checksum = 0;
1181 /* No need of host_to_net conversion, already in net-byte order */
1182 hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1183 hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1184 hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1185 hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1186 hb_ack->hb_info.param_hdr.length =
1187 clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
1188
1189 vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
1190 vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
1191
1192 vnet_buffer (b)->sctp.connection_index =
1193 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001194 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001195}
1196
1197/**
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001198 * Convert buffer to HEARTBEAT
1199 */
1200void
Marco Varlese54432f82018-02-15 17:01:56 +01001201sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001202 vlib_buffer_t * b)
1203{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001204 u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
1205
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001206 /* As per RFC 4960 the chunk_length value does NOT contemplate
1207 * the size of the first header (see sctp_header_t) and any padding
1208 */
1209 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1210
1211 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1212
1213 sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
1214
1215 hb_req->sctp_hdr.checksum = 0;
1216 /* No need of host_to_net conversion, already in net-byte order */
1217 hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
1218 hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
1219 hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
1220 hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
1221 hb_req->hb_info.param_hdr.length =
1222 clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
1223
1224 vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
1225 vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
1226
1227 vnet_buffer (b)->sctp.connection_index =
1228 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001229 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001230}
1231
1232void
1233sctp_send_heartbeat (sctp_connection_t * sctp_conn)
1234{
1235 vlib_buffer_t *b;
1236 u32 bi;
1237 sctp_main_t *tm = vnet_get_sctp_main ();
1238 vlib_main_t *vm = vlib_get_main ();
1239
Marco Varlese54432f82018-02-15 17:01:56 +01001240 u8 i;
Marco Varlese93826732018-09-27 16:43:57 +02001241 u64 now = sctp_time_now ();
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001242
Marco Varlese54432f82018-02-15 17:01:56 +01001243 for (i = 0; i < MAX_SCTP_CONNECTIONS; i++)
1244 {
1245 if (sctp_conn->sub_conn[i].state == SCTP_SUBCONN_STATE_DOWN)
1246 continue;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001247
Marco Varlese54432f82018-02-15 17:01:56 +01001248 if (now > (sctp_conn->sub_conn[i].last_seen + SCTP_HB_INTERVAL))
1249 {
1250 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1251 return;
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001252
Marco Varlese54432f82018-02-15 17:01:56 +01001253 b = vlib_get_buffer (vm, bi);
1254 sctp_init_buffer (vm, b);
1255 sctp_prepare_heartbeat_chunk (sctp_conn, i, b);
1256
1257 sctp_enqueue_to_output_now (vm, b, bi,
1258 sctp_conn->sub_conn[i].
1259 connection.is_ip4);
1260
1261 sctp_conn->sub_conn[i].unacknowledged_hb += 1;
1262 }
1263 }
Marco Varlese191a5942017-10-30 18:17:21 +01001264}
1265
1266/**
1267 * Convert buffer to SHUTDOWN_COMPLETE
1268 */
1269void
Marco Varlese54432f82018-02-15 17:01:56 +01001270sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlese191a5942017-10-30 18:17:21 +01001271 vlib_buffer_t * b)
1272{
Marco Varlese191a5942017-10-30 18:17:21 +01001273 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1274 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1275
1276 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1277
1278 sctp_shutdown_complete_chunk_t *shutdown_complete =
1279 vlib_buffer_push_uninit (b, alloc_bytes);
1280
1281 shutdown_complete->sctp_hdr.checksum = 0;
1282 /* No need of host_to_net conversion, already in net-byte order */
1283 shutdown_complete->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001284 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +01001285 shutdown_complete->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001286 sctp_conn->sub_conn[idx].connection.rmt_port;
1287 shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001288
1289 vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1290 vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1291
1292 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001293 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese15cc6a82018-02-21 12:39:52 +01001294 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001295}
1296
1297void
Marco Varlese54432f82018-02-15 17:01:56 +01001298sctp_send_shutdown_complete (sctp_connection_t * sctp_conn, u8 idx,
Marco Varlesefae40392018-02-14 15:38:35 +01001299 vlib_buffer_t * b0)
Marco Varlese191a5942017-10-30 18:17:21 +01001300{
Marco Varlese191a5942017-10-30 18:17:21 +01001301 vlib_main_t *vm = vlib_get_main ();
1302
Marco Varlesefae40392018-02-14 15:38:35 +01001303 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001304 return;
1305
Marco Varlesefae40392018-02-14 15:38:35 +01001306 sctp_reuse_buffer (vm, b0);
Marco Varlese191a5942017-10-30 18:17:21 +01001307
Marco Varlese54432f82018-02-15 17:01:56 +01001308 sctp_prepare_shutdown_complete_chunk (sctp_conn, idx, b0);
Marco Varlese191a5942017-10-30 18:17:21 +01001309}
1310
Marco Varlese191a5942017-10-30 18:17:21 +01001311/*
1312 * Send INIT
1313 */
1314void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001315sctp_send_init (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +01001316{
1317 vlib_buffer_t *b;
1318 u32 bi;
1319 sctp_main_t *tm = vnet_get_sctp_main ();
1320 vlib_main_t *vm = vlib_get_main ();
1321
Marco Varlese9e09ff32018-03-05 12:31:45 +01001322 if (PREDICT_FALSE (sctp_conn->init_retransmit_err > SCTP_MAX_INIT_RETRANS))
1323 {
1324 clib_warning ("Reached MAX_INIT_RETRANS times. Aborting connection.");
1325
1326 session_stream_connect_notify (&sctp_conn->sub_conn
Marco Varlesec7fe4f32018-03-05 15:12:29 +01001327 [SCTP_PRIMARY_PATH_IDX].connection, 1);
Marco Varlese9e09ff32018-03-05 12:31:45 +01001328
1329 sctp_connection_timers_reset (sctp_conn);
1330
1331 sctp_connection_cleanup (sctp_conn);
1332
1333 return;
1334 }
1335
Marco Varlese191a5942017-10-30 18:17:21 +01001336 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1337 return;
1338
1339 b = vlib_get_buffer (vm, bi);
Marco Varlesec7fe4f32018-03-05 15:12:29 +01001340 u8 idx = SCTP_PRIMARY_PATH_IDX;
Marco Varlese191a5942017-10-30 18:17:21 +01001341
1342 sctp_init_buffer (vm, b);
Marco Varlese54432f82018-02-15 17:01:56 +01001343 sctp_prepare_init_chunk (sctp_conn, idx, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001344
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001345 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
Marco Varlesee17bb712018-03-28 12:06:10 +02001346 sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4,
1347 sctp_conn->sub_conn[idx].c_fib_index);
Marco Varlese191a5942017-10-30 18:17:21 +01001348
1349 /* Start the T1_INIT timer */
Marco Varlese21c8baf2018-02-02 17:17:51 +01001350 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1351 sctp_conn->sub_conn[idx].RTO);
1352
Marco Varlese191a5942017-10-30 18:17:21 +01001353 /* Change state to COOKIE_WAIT */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001354 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
Marco Varlesea38783e2018-02-13 12:38:52 +01001355
1356 /* Measure RTT with this */
1357 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +01001358}
1359
Marco Varlese191a5942017-10-30 18:17:21 +01001360/**
1361 * Push SCTP header and update connection variables
1362 */
1363static void
Marco Varlesef3ab4892018-02-19 15:23:13 +01001364sctp_push_hdr_i (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
Marco Varlese191a5942017-10-30 18:17:21 +01001365 sctp_state_t next_state)
1366{
Marco Varlese191a5942017-10-30 18:17:21 +01001367 u16 data_len =
1368 b->current_length + b->total_length_not_including_first_buffer;
Marco Varlese87971682018-10-04 15:46:05 +02001369
Marco Varlese191a5942017-10-30 18:17:21 +01001370 ASSERT (!b->total_length_not_including_first_buffer
1371 || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1372
1373 SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1374 "b->current_data = %p "
1375 "data_len = %u",
1376 b->current_length, b->current_data, data_len);
1377
Marco Varlese87971682018-10-04 15:46:05 +02001378 u16 data_padding = vnet_sctp_calculate_padding (b->current_length);
1379 if (data_padding > 0)
1380 {
1381 u8 *p_tail = vlib_buffer_put_uninit (b, data_padding);
1382 clib_memset_u8 (p_tail, 0, data_padding);
1383 }
1384
Marco Varlese191a5942017-10-30 18:17:21 +01001385 u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1386 u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1387
Marco Varlese191a5942017-10-30 18:17:21 +01001388 sctp_payload_data_chunk_t *data_chunk =
1389 vlib_buffer_push_uninit (b, bytes_to_add);
1390
Marco Varlesef3ab4892018-02-19 15:23:13 +01001391 u8 idx = sctp_data_subconn_select (sctp_conn);
Marco Varlese15cc6a82018-02-21 12:39:52 +01001392 SCTP_DBG_OUTPUT
Marco Varlese04e5d642018-02-23 17:43:06 +01001393 ("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 +01001394 sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1395 sctp_conn->sub_conn[idx].connection.c_index,
1396 sctp_conn->sub_conn[idx].connection.lcl_port,
1397 sctp_conn->sub_conn[idx].connection.rmt_port);
Marco Varlese191a5942017-10-30 18:17:21 +01001398 data_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001399 data_chunk->sctp_hdr.src_port =
1400 sctp_conn->sub_conn[idx].connection.lcl_port;
1401 data_chunk->sctp_hdr.dst_port =
1402 sctp_conn->sub_conn[idx].connection.rmt_port;
1403 data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001404
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001405 data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
Marco Varlese191a5942017-10-30 18:17:21 +01001406 data_chunk->stream_id = clib_host_to_net_u16 (0);
1407 data_chunk->stream_seq = clib_host_to_net_u16 (0);
1408
1409 vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1410 vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1411
Marco Varlese91389ac2018-01-31 11:00:01 +01001412 vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1413 vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1414
Marco Varlese191a5942017-10-30 18:17:21 +01001415 SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1416 b->data, b->current_data);
1417
Marco Varlese6e4d4a32018-03-12 12:36:59 +01001418 if (sctp_conn->sub_conn[idx].state != SCTP_SUBCONN_AWAITING_SACK)
1419 {
1420 sctp_conn->sub_conn[idx].state = SCTP_SUBCONN_AWAITING_SACK;
1421 sctp_conn->last_unacked_tsn = sctp_conn->next_tsn;
1422 }
1423
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001424 sctp_conn->next_tsn += data_len;
1425
Marco Varlesef3ab4892018-02-19 15:23:13 +01001426 u32 inflight = sctp_conn->next_tsn - sctp_conn->last_unacked_tsn;
1427 /* Section 7.2.2; point (3) */
1428 if (sctp_conn->sub_conn[idx].partially_acked_bytes >=
1429 sctp_conn->sub_conn[idx].cwnd
1430 && inflight >= sctp_conn->sub_conn[idx].cwnd)
1431 {
1432 sctp_conn->sub_conn[idx].cwnd += sctp_conn->sub_conn[idx].PMTU;
1433 sctp_conn->sub_conn[idx].partially_acked_bytes -=
1434 sctp_conn->sub_conn[idx].cwnd;
1435 }
1436
1437 sctp_conn->sub_conn[idx].last_data_ts = sctp_time_now ();
1438
Marco Varlese191a5942017-10-30 18:17:21 +01001439 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001440 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese54432f82018-02-15 17:01:56 +01001441
Marco Varlese15cc6a82018-02-21 12:39:52 +01001442 vnet_buffer (b)->sctp.subconn_idx = idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001443}
1444
1445u32
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001446sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001447{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001448 sctp_connection_t *sctp_conn =
1449 sctp_get_connection_from_transport (trans_conn);
Marco Varlese191a5942017-10-30 18:17:21 +01001450
Marco Varlese15cc6a82018-02-21 12:39:52 +01001451 SCTP_DBG_OUTPUT ("TRANS_CONN = %p, SCTP_CONN = %p, "
1452 "S_INDEX = %u, C_INDEX = %u,"
Marco Varlese04e5d642018-02-23 17:43:06 +01001453 "trans_conn->LCL_PORT = %u, trans_conn->RMT_PORT = %u",
Marco Varlese15cc6a82018-02-21 12:39:52 +01001454 trans_conn,
1455 sctp_conn,
1456 trans_conn->s_index,
1457 trans_conn->c_index,
1458 trans_conn->lcl_port, trans_conn->rmt_port);
1459
Marco Varlesef3ab4892018-02-19 15:23:13 +01001460 sctp_push_hdr_i (sctp_conn, b, SCTP_STATE_ESTABLISHED);
Marco Varlese21c8baf2018-02-02 17:17:51 +01001461
Stevenb95bc052018-02-27 10:29:32 -08001462 sctp_trajectory_add_start (b, 3);
Marco Varlese191a5942017-10-30 18:17:21 +01001463
1464 return 0;
Marco Varlese191a5942017-10-30 18:17:21 +01001465}
1466
Marco Varlese3e9b4652018-03-13 15:44:56 +01001467u32
1468sctp_prepare_data_retransmit (sctp_connection_t * sctp_conn,
1469 u8 idx,
1470 u32 offset,
1471 u32 max_deq_bytes, vlib_buffer_t ** b)
1472{
1473 sctp_main_t *tm = vnet_get_sctp_main ();
1474 vlib_main_t *vm = vlib_get_main ();
1475 int n_bytes = 0;
1476 u32 bi, available_bytes, seg_size;
1477 u8 *data;
1478
1479 ASSERT (sctp_conn->state >= SCTP_STATE_ESTABLISHED);
1480 ASSERT (max_deq_bytes != 0);
1481
1482 /*
1483 * Make sure we can retransmit something
1484 */
1485 available_bytes =
Florin Coras25579b42018-06-06 17:55:02 -07001486 session_tx_fifo_max_dequeue (&sctp_conn->sub_conn[idx].connection);
Marco Varlese3e9b4652018-03-13 15:44:56 +01001487 ASSERT (available_bytes >= offset);
1488 available_bytes -= offset;
1489 if (!available_bytes)
1490 return 0;
1491 max_deq_bytes = clib_min (sctp_conn->sub_conn[idx].cwnd, max_deq_bytes);
1492 max_deq_bytes = clib_min (available_bytes, max_deq_bytes);
1493
1494 seg_size = max_deq_bytes;
1495
1496 /*
1497 * Allocate and fill in buffer(s)
1498 */
1499
1500 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1501 return 0;
1502 *b = vlib_get_buffer (vm, bi);
1503 data = sctp_init_buffer (vm, *b);
1504
1505 /* Easy case, buffer size greater than mss */
1506 if (PREDICT_TRUE (seg_size <= tm->bytes_per_buffer))
1507 {
1508 n_bytes =
1509 stream_session_peek_bytes (&sctp_conn->sub_conn[idx].connection, data,
1510 offset, max_deq_bytes);
1511 ASSERT (n_bytes == max_deq_bytes);
1512 b[0]->current_length = n_bytes;
1513 sctp_push_hdr_i (sctp_conn, *b, sctp_conn->state);
1514 }
1515
1516 return n_bytes;
1517}
1518
Marco Varlese9e09ff32018-03-05 12:31:45 +01001519void
1520sctp_data_retransmit (sctp_connection_t * sctp_conn)
1521{
Marco Varlese3e9b4652018-03-13 15:44:56 +01001522 vlib_main_t *vm = vlib_get_main ();
1523 vlib_buffer_t *b = 0;
1524 u32 bi, n_bytes = 0;
1525
Marco Varlesebaf74052018-09-27 11:58:34 +02001526 u8 idx = sctp_data_subconn_select (sctp_conn);
1527
Marco Varlese9e09ff32018-03-05 12:31:45 +01001528 SCTP_DBG_OUTPUT
1529 ("SCTP_CONN = %p, IDX = %u, S_INDEX = %u, C_INDEX = %u, sctp_conn->[...].LCL_PORT = %u, sctp_conn->[...].RMT_PORT = %u",
1530 sctp_conn, idx, sctp_conn->sub_conn[idx].connection.s_index,
1531 sctp_conn->sub_conn[idx].connection.c_index,
1532 sctp_conn->sub_conn[idx].connection.lcl_port,
1533 sctp_conn->sub_conn[idx].connection.rmt_port);
1534
Marco Varlese3e9b4652018-03-13 15:44:56 +01001535 if (sctp_conn->state >= SCTP_STATE_ESTABLISHED)
1536 {
1537 return;
1538 }
1539
Marco Varlese3e9b4652018-03-13 15:44:56 +01001540 n_bytes =
1541 sctp_prepare_data_retransmit (sctp_conn, idx, 0,
1542 sctp_conn->sub_conn[idx].cwnd, &b);
1543 if (n_bytes > 0)
1544 SCTP_DBG_OUTPUT ("We have data (%u bytes) to retransmit", n_bytes);
1545
1546 bi = vlib_get_buffer_index (vm, b);
1547
1548 sctp_enqueue_to_output_now (vm, b, bi,
1549 sctp_conn->sub_conn[idx].connection.is_ip4);
1550
Marco Varlese9e09ff32018-03-05 12:31:45 +01001551 return;
1552}
1553
Marco Varlesefae40392018-02-14 15:38:35 +01001554#if SCTP_DEBUG_STATE_MACHINE
Marco Varlesea38783e2018-02-13 12:38:52 +01001555always_inline u8
1556sctp_validate_output_state_machine (sctp_connection_t * sctp_conn,
1557 u8 chunk_type)
1558{
1559 u8 result = 0;
1560 switch (sctp_conn->state)
1561 {
1562 case SCTP_STATE_CLOSED:
1563 if (chunk_type != INIT && chunk_type != INIT_ACK)
1564 result = 1;
1565 break;
1566 case SCTP_STATE_ESTABLISHED:
1567 if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1568 chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1569 chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1570 result = 1;
1571 break;
1572 case SCTP_STATE_COOKIE_WAIT:
1573 if (chunk_type != COOKIE_ECHO)
1574 result = 1;
1575 break;
1576 case SCTP_STATE_SHUTDOWN_SENT:
1577 if (chunk_type != SHUTDOWN_COMPLETE)
1578 result = 1;
1579 break;
1580 case SCTP_STATE_SHUTDOWN_RECEIVED:
1581 if (chunk_type != SHUTDOWN_ACK)
1582 result = 1;
1583 break;
1584 }
1585 return result;
1586}
Marco Varlesefae40392018-02-14 15:38:35 +01001587#endif
Marco Varlesea38783e2018-02-13 12:38:52 +01001588
1589always_inline u8
1590sctp_is_retransmitting (sctp_connection_t * sctp_conn, u8 idx)
1591{
1592 return sctp_conn->sub_conn[idx].is_retransmitting;
1593}
1594
Marco Varlese191a5942017-10-30 18:17:21 +01001595always_inline uword
1596sctp46_output_inline (vlib_main_t * vm,
1597 vlib_node_runtime_t * node,
1598 vlib_frame_t * from_frame, int is_ip4)
1599{
1600 u32 n_left_from, next_index, *from, *to_next;
1601 u32 my_thread_index = vm->thread_index;
1602
1603 from = vlib_frame_vector_args (from_frame);
1604 n_left_from = from_frame->n_vectors;
1605 next_index = node->cached_next_index;
1606 sctp_set_time_now (my_thread_index);
1607
1608 while (n_left_from > 0)
1609 {
1610 u32 n_left_to_next;
1611
1612 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1613
1614 while (n_left_from > 0 && n_left_to_next > 0)
1615 {
1616 u32 bi0;
1617 vlib_buffer_t *b0;
1618 sctp_header_t *sctp_hdr = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001619 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +01001620 sctp_tx_trace_t *t0;
1621 sctp_header_t *th0 = 0;
1622 u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1623 SCTP_OUTPUT_NEXT_IP_LOOKUP;
1624
1625#if SCTP_DEBUG_STATE_MACHINE
1626 u16 packet_length = 0;
1627#endif
1628
1629 bi0 = from[0];
1630 to_next[0] = bi0;
1631 from += 1;
1632 to_next += 1;
1633 n_left_from -= 1;
1634 n_left_to_next -= 1;
1635
1636 b0 = vlib_get_buffer (vm, bi0);
Marco Varlesebe2251b2018-02-07 12:22:41 +01001637
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001638 sctp_conn =
1639 sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1640 my_thread_index);
Marco Varlese191a5942017-10-30 18:17:21 +01001641
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001642 if (PREDICT_FALSE (sctp_conn == 0))
Marco Varlese191a5942017-10-30 18:17:21 +01001643 {
1644 error0 = SCTP_ERROR_INVALID_CONNECTION;
1645 next0 = SCTP_OUTPUT_NEXT_DROP;
1646 goto done;
1647 }
1648
Marco Varlese15cc6a82018-02-21 12:39:52 +01001649 u8 idx = vnet_buffer (b0)->sctp.subconn_idx;
Marco Varlese191a5942017-10-30 18:17:21 +01001650
1651 th0 = vlib_buffer_get_current (b0);
1652
1653 if (is_ip4)
1654 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001655 ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1656 b0,
1657 &sctp_conn->sub_conn
1658 [idx].connection.
1659 lcl_ip.ip4,
1660 &sctp_conn->
1661 sub_conn
1662 [idx].connection.
1663 rmt_ip.ip4,
1664 IP_PROTOCOL_SCTP, 1);
Marco Varlese191a5942017-10-30 18:17:21 +01001665
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001666 u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
Marco Varlese191a5942017-10-30 18:17:21 +01001667
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001668 sctp_hdr = ip4_next_header (iph4);
Marco Varlese191a5942017-10-30 18:17:21 +01001669 sctp_hdr->checksum = checksum;
1670
1671 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001672
1673#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001674 packet_length = clib_net_to_host_u16 (iph4->length);
Marco Varlese191a5942017-10-30 18:17:21 +01001675#endif
1676 }
1677 else
1678 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001679 ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1680 b0,
1681 &sctp_conn->sub_conn
1682 [idx].
1683 connection.lcl_ip.
1684 ip6,
1685 &sctp_conn->sub_conn
1686 [idx].
1687 connection.rmt_ip.
1688 ip6,
1689 IP_PROTOCOL_SCTP);
Marco Varlese191a5942017-10-30 18:17:21 +01001690
1691 int bogus = ~0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001692 u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
Marco Varlese191a5942017-10-30 18:17:21 +01001693 ASSERT (!bogus);
1694
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001695 sctp_hdr = ip6_next_header (iph6);
Marco Varlese191a5942017-10-30 18:17:21 +01001696 sctp_hdr->checksum = checksum;
1697
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001698 vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001699 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001700
1701#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001702 packet_length = clib_net_to_host_u16 (iph6->payload_length);
Marco Varlese191a5942017-10-30 18:17:21 +01001703#endif
1704 }
1705
Marco Varlesefae40392018-02-14 15:38:35 +01001706 sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1707 u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1708 if (chunk_type >= UNKNOWN)
1709 {
1710 clib_warning
1711 ("Trying to send an unrecognized chunk... something is really bad.");
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +03001712 error0 = SCTP_ERROR_UNKNOWN_CHUNK;
Marco Varlesefae40392018-02-14 15:38:35 +01001713 next0 = SCTP_OUTPUT_NEXT_DROP;
1714 goto done;
1715 }
1716
1717#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese191a5942017-10-30 18:17:21 +01001718 u8 is_valid =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001719 (sctp_conn->sub_conn[idx].connection.lcl_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001720 sctp_hdr->src_port
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001721 || sctp_conn->sub_conn[idx].connection.lcl_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001722 sctp_hdr->dst_port)
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001723 && (sctp_conn->sub_conn[idx].connection.rmt_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001724 sctp_hdr->dst_port
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001725 || sctp_conn->sub_conn[idx].connection.rmt_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001726 sctp_hdr->src_port);
1727
Marco Varlese191a5942017-10-30 18:17:21 +01001728 if (!is_valid)
1729 {
1730 SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1731 "packet_length = %u, "
1732 "chunk_type = %u [%s], "
1733 "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1734 "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
Marco Varlesefae40392018-02-14 15:38:35 +01001735 sctp_conn->sub_conn[idx].
1736 connection.c_index, packet_length,
Marco Varlese191a5942017-10-30 18:17:21 +01001737 chunk_type,
1738 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001739 sctp_conn->sub_conn[idx].
1740 connection.lcl_port, sctp_hdr->src_port,
1741 sctp_conn->sub_conn[idx].
1742 connection.rmt_port,
Marco Varlese191a5942017-10-30 18:17:21 +01001743 sctp_hdr->dst_port);
1744
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +03001745 error0 = SCTP_ERROR_UNKNOWN_CHUNK;
Marco Varlese191a5942017-10-30 18:17:21 +01001746 next0 = SCTP_OUTPUT_NEXT_DROP;
1747 goto done;
1748 }
Marco Varlesefae40392018-02-14 15:38:35 +01001749#endif
Marco Varlese191a5942017-10-30 18:17:21 +01001750 SCTP_DBG_STATE_MACHINE
Marco Varlesef3ab4892018-02-19 15:23:13 +01001751 ("SESSION_INDEX = %u, CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
Marco Varlese191a5942017-10-30 18:17:21 +01001752 "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
Marco Varlesef3ab4892018-02-19 15:23:13 +01001753 sctp_conn->sub_conn[idx].connection.s_index,
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001754 sctp_conn->sub_conn[idx].connection.c_index,
1755 sctp_conn->state, sctp_state_to_string (sctp_conn->state),
Marco Varlese191a5942017-10-30 18:17:21 +01001756 sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1757 full_hdr->hdr.dst_port);
1758
Marco Varlese191a5942017-10-30 18:17:21 +01001759 /* Let's make sure the state-machine does not send anything crazy */
Marco Varlesea38783e2018-02-13 12:38:52 +01001760#if SCTP_DEBUG_STATE_MACHINE
1761 if (sctp_validate_output_state_machine (sctp_conn, chunk_type) != 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001762 {
Marco Varlese191a5942017-10-30 18:17:21 +01001763 SCTP_DBG_STATE_MACHINE
Marco Varlesea38783e2018-02-13 12:38:52 +01001764 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
Marco Varlese191a5942017-10-30 18:17:21 +01001765 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001766 sctp_state_to_string (sctp_conn->state));
Marco Varlesea38783e2018-02-13 12:38:52 +01001767
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +03001768 error0 = SCTP_ERROR_UNKNOWN_CHUNK;
Marco Varlesea38783e2018-02-13 12:38:52 +01001769 next0 = SCTP_OUTPUT_NEXT_DROP;
1770 goto done;
Marco Varlesef3ab4892018-02-19 15:23:13 +01001771
Marco Varlesea38783e2018-02-13 12:38:52 +01001772 }
1773#endif
1774
1775 /* Karn's algorithm: RTT measurements MUST NOT be made using
1776 * packets that were retransmitted
1777 */
1778 if (!sctp_is_retransmitting (sctp_conn, idx))
1779 {
1780 /* Measure RTT with this */
1781 if (chunk_type == DATA
1782 && sctp_conn->sub_conn[idx].RTO_pending == 0)
1783 {
1784 sctp_conn->sub_conn[idx].RTO_pending = 1;
1785 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1786 }
1787 else
1788 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +01001789 }
1790
Marco Varlesea38783e2018-02-13 12:38:52 +01001791 /* Let's take care of TIMERS */
Marco Varlesebe2251b2018-02-07 12:22:41 +01001792 switch (chunk_type)
Marco Varlese191a5942017-10-30 18:17:21 +01001793 {
Marco Varlesea38783e2018-02-13 12:38:52 +01001794 case COOKIE_ECHO:
1795 {
1796 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
1797 break;
1798 }
Marco Varlesebe2251b2018-02-07 12:22:41 +01001799 case DATA:
1800 {
Marco Varlesea38783e2018-02-13 12:38:52 +01001801 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1802
Marco Varlesebe2251b2018-02-07 12:22:41 +01001803 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1804 sctp_conn->sub_conn[idx].RTO);
1805 break;
1806 }
1807 case SHUTDOWN:
1808 {
1809 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1810 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1811 sctp_conn->sub_conn[idx].RTO);
1812 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1813 break;
1814 }
1815 case SHUTDOWN_ACK:
1816 {
1817 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1818 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1819 sctp_conn->sub_conn[idx].RTO);
1820 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1821 break;
1822 }
Marco Varlesea38783e2018-02-13 12:38:52 +01001823 case SHUTDOWN_COMPLETE:
1824 {
1825 sctp_conn->state = SCTP_STATE_CLOSED;
1826 break;
1827 }
Marco Varlese191a5942017-10-30 18:17:21 +01001828 }
1829
1830 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
Marco Varlesee17bb712018-03-28 12:06:10 +02001831 vnet_buffer (b0)->sw_if_index[VLIB_TX] =
1832 sctp_conn->sub_conn[idx].c_fib_index;
Marco Varlese191a5942017-10-30 18:17:21 +01001833
1834 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1835
Marco Varlesef3ab4892018-02-19 15:23:13 +01001836 SCTP_DBG_STATE_MACHINE
1837 ("SESSION_INDEX = %u, CONNECTION_INDEX = %u, " "NEW_STATE = %s, "
1838 "CHUNK_SENT = %s", sctp_conn->sub_conn[idx].connection.s_index,
1839 sctp_conn->sub_conn[idx].connection.c_index,
1840 sctp_state_to_string (sctp_conn->state),
1841 sctp_chunk_to_string (chunk_type));
Marco Varlese191a5942017-10-30 18:17:21 +01001842
1843 vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1844
1845 done:
1846 b0->error = node->errors[error0];
1847 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1848 {
1849 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1850 if (th0)
1851 {
Dave Barach178cf492018-11-13 16:34:13 -05001852 clib_memcpy_fast (&t0->sctp_header, th0,
1853 sizeof (t0->sctp_header));
Marco Varlese191a5942017-10-30 18:17:21 +01001854 }
1855 else
1856 {
Dave Barachb7b92992018-10-17 10:38:51 -04001857 clib_memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
Marco Varlese191a5942017-10-30 18:17:21 +01001858 }
Dave Barach178cf492018-11-13 16:34:13 -05001859 clib_memcpy_fast (&t0->sctp_connection, sctp_conn,
1860 sizeof (t0->sctp_connection));
Marco Varlese191a5942017-10-30 18:17:21 +01001861 }
1862
1863 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1864 n_left_to_next, bi0, next0);
1865 }
1866
1867 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1868 }
1869
1870 return from_frame->n_vectors;
1871}
1872
1873static uword
1874sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1875 vlib_frame_t * from_frame)
1876{
1877 return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1878}
1879
1880static uword
1881sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1882 vlib_frame_t * from_frame)
1883{
1884 return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1885}
1886
1887/* *INDENT-OFF* */
1888VLIB_REGISTER_NODE (sctp4_output_node) =
1889{
1890 .function = sctp4_output,.name = "sctp4-output",
1891 /* Takes a vector of packets. */
1892 .vector_size = sizeof (u32),
1893 .n_errors = SCTP_N_ERROR,
1894 .error_strings = sctp_error_strings,
1895 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1896 .next_nodes = {
1897#define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1898 foreach_sctp4_output_next
1899#undef _
1900 },
1901 .format_buffer = format_sctp_header,
1902 .format_trace = format_sctp_tx_trace,
1903};
1904/* *INDENT-ON* */
1905
1906VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1907
1908/* *INDENT-OFF* */
1909VLIB_REGISTER_NODE (sctp6_output_node) =
1910{
1911 .function = sctp6_output,
1912 .name = "sctp6-output",
1913 /* Takes a vector of packets. */
1914 .vector_size = sizeof (u32),
1915 .n_errors = SCTP_N_ERROR,
1916 .error_strings = sctp_error_strings,
1917 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1918 .next_nodes = {
1919#define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1920 foreach_sctp6_output_next
1921#undef _
1922 },
1923 .format_buffer = format_sctp_header,
1924 .format_trace = format_sctp_tx_trace,
1925};
1926/* *INDENT-ON* */
1927
1928VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1929
1930/*
1931 * fd.io coding-style-patch-verification: ON
1932 *
1933 * Local Variables:
1934 * eval: (c-set-style "gnu")
1935 * End:
1936 */