blob: 276d7e70213c24fa350fe74778091c8f338b222f [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;
264
265 /* Leave enough space for headers */
266 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
267}
268
269always_inline void *
270sctp_init_buffer (vlib_main_t * vm, vlib_buffer_t * b)
271{
272 ASSERT ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0);
Damjan Mariondac03522018-02-01 15:30:13 +0100273 b->flags &= VLIB_BUFFER_NON_DEFAULT_FREELIST;
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;
277 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
278 /* Leave enough space for headers */
279 return vlib_buffer_make_headroom (b, MAX_HDRS_LEN);
280}
281
282always_inline int
283sctp_alloc_tx_buffers (sctp_main_t * tm, u8 thread_index, u32 n_free_buffers)
284{
285 vlib_main_t *vm = vlib_get_main ();
286 u32 current_length = vec_len (tm->tx_buffers[thread_index]);
287 u32 n_allocated;
288
289 vec_validate (tm->tx_buffers[thread_index],
290 current_length + n_free_buffers - 1);
291 n_allocated =
292 vlib_buffer_alloc (vm, &tm->tx_buffers[thread_index][current_length],
293 n_free_buffers);
294 _vec_len (tm->tx_buffers[thread_index]) = current_length + n_allocated;
295 /* buffer shortage, report failure */
296 if (vec_len (tm->tx_buffers[thread_index]) == 0)
297 {
298 clib_warning ("out of buffers");
299 return -1;
300 }
301 return 0;
302}
303
304always_inline int
305sctp_get_free_buffer_index (sctp_main_t * tm, u32 * bidx)
306{
307 u32 *my_tx_buffers;
308 u32 thread_index = vlib_get_thread_index ();
309 if (PREDICT_FALSE (vec_len (tm->tx_buffers[thread_index]) == 0))
310 {
311 if (sctp_alloc_tx_buffers (tm, thread_index, VLIB_FRAME_SIZE))
312 return -1;
313 }
314 my_tx_buffers = tm->tx_buffers[thread_index];
315 *bidx = my_tx_buffers[vec_len (my_tx_buffers) - 1];
316 _vec_len (my_tx_buffers) -= 1;
317 return 0;
318}
319
320always_inline void
321sctp_enqueue_to_output_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
322 u8 is_ip4, u8 flush)
323{
324 sctp_main_t *tm = vnet_get_sctp_main ();
325 u32 thread_index = vlib_get_thread_index ();
326 u32 *to_next, next_index;
327 vlib_frame_t *f;
328
329 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
330 b->error = 0;
331
332 /* Decide where to send the packet */
333 next_index = is_ip4 ? sctp4_output_node.index : sctp6_output_node.index;
334 sctp_trajectory_add_start (b, 2);
335
336 /* Get frame to v4/6 output node */
337 f = tm->tx_frames[!is_ip4][thread_index];
338 if (!f)
339 {
340 f = vlib_get_frame_to_node (vm, next_index);
341 ASSERT (f);
342 tm->tx_frames[!is_ip4][thread_index] = f;
343 }
344 to_next = vlib_frame_vector_args (f);
345 to_next[f->n_vectors] = bi;
346 f->n_vectors += 1;
347 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
348 {
349 vlib_put_frame_to_node (vm, next_index, f);
350 tm->tx_frames[!is_ip4][thread_index] = 0;
351 }
352}
353
354always_inline void
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100355sctp_enqueue_to_output (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
356 u8 is_ip4)
357{
358 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 0);
359}
360
361always_inline void
Marco Varlese191a5942017-10-30 18:17:21 +0100362sctp_enqueue_to_output_now (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
363 u8 is_ip4)
364{
365 sctp_enqueue_to_output_i (vm, b, bi, is_ip4, 1);
366}
367
368always_inline void
369sctp_enqueue_to_ip_lookup_i (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
370 u8 is_ip4, u8 flush)
371{
372 sctp_main_t *tm = vnet_get_sctp_main ();
373 u32 thread_index = vlib_get_thread_index ();
374 u32 *to_next, next_index;
375 vlib_frame_t *f;
376
377 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
378 b->error = 0;
379
380 /* Default FIB for now */
381 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
382
383 /* Send to IP lookup */
384 next_index = is_ip4 ? ip4_lookup_node.index : ip6_lookup_node.index;
385 if (VLIB_BUFFER_TRACE_TRAJECTORY > 0)
386 {
387 b->pre_data[0] = 2;
388 b->pre_data[1] = next_index;
389 }
390
391 f = tm->ip_lookup_tx_frames[!is_ip4][thread_index];
392 if (!f)
393 {
394 f = vlib_get_frame_to_node (vm, next_index);
395 ASSERT (f);
396 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = f;
397 }
398
399 to_next = vlib_frame_vector_args (f);
400 to_next[f->n_vectors] = bi;
401 f->n_vectors += 1;
402 if (flush || f->n_vectors == VLIB_FRAME_SIZE)
403 {
404 vlib_put_frame_to_node (vm, next_index, f);
405 tm->ip_lookup_tx_frames[!is_ip4][thread_index] = 0;
406 }
407}
408
409always_inline void
410sctp_enqueue_to_ip_lookup (vlib_main_t * vm, vlib_buffer_t * b, u32 bi,
411 u8 is_ip4)
412{
413 sctp_enqueue_to_ip_lookup_i (vm, b, bi, is_ip4, 0);
414}
415
Marco Varlese191a5942017-10-30 18:17:21 +0100416/**
417 * Convert buffer to INIT
418 */
419void
420sctp_prepare_init_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
421{
422 u32 random_seed = random_default_seed ();
423 u16 alloc_bytes = sizeof (sctp_init_chunk_t);
424 sctp_sub_connection_t *sub_conn =
425 &sctp_conn->sub_conn[sctp_pick_conn_idx_on_chunk (INIT)];
426
427 sctp_ipv4_addr_param_t *ip4_param = 0;
428 sctp_ipv6_addr_param_t *ip6_param = 0;
429
430 if (sub_conn->c_is_ip4)
431 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
432 else
433 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
434
435 /* As per RFC 4960 the chunk_length value does NOT contemplate
436 * the size of the first header (see sctp_header_t) and any padding
437 */
438 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
439
440 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
441
442 sctp_init_chunk_t *init_chunk = vlib_buffer_push_uninit (b, alloc_bytes);
443
444 u16 pointer_offset = sizeof (init_chunk);
445 if (sub_conn->c_is_ip4)
446 {
447 ip4_param = (sctp_ipv4_addr_param_t *) init_chunk + pointer_offset;
448 ip4_param->address.as_u32 = sub_conn->c_lcl_ip.ip4.as_u32;
449
450 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
451 }
452 else
453 {
454 ip6_param = (sctp_ipv6_addr_param_t *) init_chunk + pointer_offset;
455 ip6_param->address.as_u64[0] = sub_conn->c_lcl_ip.ip6.as_u64[0];
456 ip6_param->address.as_u64[1] = sub_conn->c_lcl_ip.ip6.as_u64[1];
457
458 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
459 }
460
461 init_chunk->sctp_hdr.src_port = sub_conn->c_lcl_port; /* No need of host_to_net conversion, already in net-byte order */
462 init_chunk->sctp_hdr.dst_port = sub_conn->c_rmt_port; /* No need of host_to_net conversion, already in net-byte order */
463 init_chunk->sctp_hdr.checksum = 0;
464 /* The sender of an INIT must set the VERIFICATION_TAG to 0 as per RFC 4960 Section 8.5.1 */
465 init_chunk->sctp_hdr.verification_tag = 0x0;
466
467 vnet_sctp_set_chunk_type (&init_chunk->chunk_hdr, INIT);
468 vnet_sctp_set_chunk_length (&init_chunk->chunk_hdr, chunk_len);
469 vnet_sctp_common_hdr_params_host_to_net (&init_chunk->chunk_hdr);
470
471 init_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
472 init_chunk->initiate_tag = clib_host_to_net_u32 (random_u32 (&random_seed));
473 init_chunk->inboud_streams_count =
474 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
475 init_chunk->outbound_streams_count =
476 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
477
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100478 init_chunk->initial_tsn =
479 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
480 SCTP_CONN_TRACKING_DBG ("sctp_conn->local_initial_tsn = %u",
481 sctp_conn->local_initial_tsn);
482
Marco Varlese191a5942017-10-30 18:17:21 +0100483 sctp_conn->local_tag = init_chunk->initiate_tag;
484
485 vnet_buffer (b)->sctp.connection_index = sub_conn->c_c_index;
486
487 SCTP_DBG_STATE_MACHINE ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
488 "CHUNK_TYPE = %s, "
489 "SRC_PORT = %u, DST_PORT = %u",
490 sub_conn->connection.c_index,
491 sctp_conn->state,
492 sctp_state_to_string (sctp_conn->state),
493 sctp_chunk_to_string (INIT),
494 init_chunk->sctp_hdr.src_port,
495 init_chunk->sctp_hdr.dst_port);
496}
497
Marco Varlese91389ac2018-01-31 11:00:01 +0100498void
499sctp_compute_mac (sctp_connection_t * sctp_conn,
500 sctp_state_cookie_param_t * state_cookie)
Marco Varlese191a5942017-10-30 18:17:21 +0100501{
Marco Varlese91389ac2018-01-31 11:00:01 +0100502#if OPENSSL_VERSION_NUMBER >= 0x10100000L
503 HMAC_CTX *ctx;
504#else
505 HMAC_CTX ctx;
506 const EVP_MD *md = EVP_sha1 ();
507#endif
508 unsigned int len = 0;
509
510#if OPENSSL_VERSION_NUMBER >= 0x10100000L
511 ctx = HMAC_CTX_new ();
512 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
513 sizeof (state_cookie->creation_time), md, NULL);
514 HMAC_Update (ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
515 HMAC_Final (ctx, state_cookie->mac, &len);
516#else
517 HMAC_CTX_init (&ctx);
518 HMAC_Init_ex (&ctx, &state_cookie->creation_time,
519 sizeof (state_cookie->creation_time), md, NULL);
520
521 HMAC_Update (&ctx, (const unsigned char *) &sctp_conn, sizeof (sctp_conn));
522 HMAC_Final (&ctx, state_cookie->mac, &len);
523 HMAC_CTX_cleanup (&ctx);
524#endif
525
526 ENDIANESS_SWAP (state_cookie->mac);
Marco Varlese191a5942017-10-30 18:17:21 +0100527}
528
529void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100530sctp_prepare_cookie_ack_chunk (sctp_connection_t * sctp_conn,
531 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100532{
533 vlib_main_t *vm = vlib_get_main ();
534 u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ACK);
535
536 sctp_reuse_buffer (vm, b);
537
538 u16 alloc_bytes = sizeof (sctp_cookie_ack_chunk_t);
539
540 /* As per RFC 4960 the chunk_length value does NOT contemplate
541 * the size of the first header (see sctp_header_t) and any padding
542 */
543 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
544
545 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
546
547 sctp_cookie_ack_chunk_t *cookie_ack_chunk =
548 vlib_buffer_push_uninit (b, alloc_bytes);
549
550 cookie_ack_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100551 cookie_ack_chunk->sctp_hdr.src_port =
552 sctp_conn->sub_conn[idx].connection.lcl_port;
553 cookie_ack_chunk->sctp_hdr.dst_port =
554 sctp_conn->sub_conn[idx].connection.rmt_port;
555 cookie_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100556 vnet_sctp_set_chunk_type (&cookie_ack_chunk->chunk_hdr, COOKIE_ACK);
557 vnet_sctp_set_chunk_length (&cookie_ack_chunk->chunk_hdr, chunk_len);
558
Marco Varlese21c8baf2018-02-02 17:17:51 +0100559 /* Measure RTT with this */
560 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
561
Marco Varlese191a5942017-10-30 18:17:21 +0100562 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100563 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100564}
565
566void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100567sctp_prepare_cookie_echo_chunk (sctp_connection_t * sctp_conn,
568 vlib_buffer_t * b,
Marco Varlese191a5942017-10-30 18:17:21 +0100569 sctp_state_cookie_param_t * sc)
570{
571 vlib_main_t *vm = vlib_get_main ();
572 u8 idx = sctp_pick_conn_idx_on_chunk (COOKIE_ECHO);
573
574 sctp_reuse_buffer (vm, b);
575
576 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
577 u16 alloc_bytes = sizeof (sctp_cookie_echo_chunk_t);
578 /* As per RFC 4960 the chunk_length value does NOT contemplate
579 * the size of the first header (see sctp_header_t) and any padding
580 */
581 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
582 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
583 sctp_cookie_echo_chunk_t *cookie_echo_chunk =
584 vlib_buffer_push_uninit (b, alloc_bytes);
585 cookie_echo_chunk->sctp_hdr.checksum = 0;
586 cookie_echo_chunk->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100587 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +0100588 cookie_echo_chunk->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100589 sctp_conn->sub_conn[idx].connection.rmt_port;
590 cookie_echo_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100591 vnet_sctp_set_chunk_type (&cookie_echo_chunk->chunk_hdr, COOKIE_ECHO);
592 vnet_sctp_set_chunk_length (&cookie_echo_chunk->chunk_hdr, chunk_len);
593 clib_memcpy (&(cookie_echo_chunk->cookie), sc,
594 sizeof (sctp_state_cookie_param_t));
Marco Varlese21c8baf2018-02-02 17:17:51 +0100595
596 /* Measure RTT with this */
597 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
598
Marco Varlese191a5942017-10-30 18:17:21 +0100599 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100600 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100601}
602
603/**
604 * Convert buffer to INIT-ACK
605 */
606void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100607sctp_prepare_initack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b,
Marco Varlese191a5942017-10-30 18:17:21 +0100608 ip4_address_t * ip4_addr,
609 ip6_address_t * ip6_addr)
610{
611 vlib_main_t *vm = vlib_get_main ();
612 sctp_ipv4_addr_param_t *ip4_param = 0;
613 sctp_ipv6_addr_param_t *ip6_param = 0;
614 u8 idx = sctp_pick_conn_idx_on_chunk (INIT_ACK);
615 u32 random_seed = random_default_seed ();
616
617 sctp_reuse_buffer (vm, b);
618
619 /* The minimum size of the message is given by the sctp_init_ack_chunk_t */
620 u16 alloc_bytes =
621 sizeof (sctp_init_ack_chunk_t) + sizeof (sctp_state_cookie_param_t);
622
623 if (PREDICT_TRUE (ip4_addr != NULL))
624 {
625 /* Create room for variable-length fields in the INIT_ACK chunk */
626 alloc_bytes += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
627 }
628 if (PREDICT_TRUE (ip6_addr != NULL))
629 {
630 /* Create room for variable-length fields in the INIT_ACK chunk */
631 alloc_bytes += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
632 }
633
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100634 if (sctp_conn->sub_conn[idx].connection.is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100635 alloc_bytes += sizeof (sctp_ipv4_addr_param_t);
636 else
637 alloc_bytes += sizeof (sctp_ipv6_addr_param_t);
638
639 /* As per RFC 4960 the chunk_length value does NOT contemplate
640 * the size of the first header (see sctp_header_t) and any padding
641 */
642 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
643
644 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
645
646 sctp_init_ack_chunk_t *init_ack_chunk =
647 vlib_buffer_push_uninit (b, alloc_bytes);
648
649 u16 pointer_offset = sizeof (sctp_init_ack_chunk_t);
650
651 /* Create State Cookie parameter */
652 sctp_state_cookie_param_t *state_cookie_param =
653 (sctp_state_cookie_param_t *) ((char *) init_ack_chunk + pointer_offset);
654
655 state_cookie_param->param_hdr.type =
656 clib_host_to_net_u16 (SCTP_STATE_COOKIE_TYPE);
657 state_cookie_param->param_hdr.length =
658 clib_host_to_net_u16 (sizeof (sctp_state_cookie_param_t));
659 state_cookie_param->creation_time = clib_host_to_net_u32 (sctp_time_now ());
660 state_cookie_param->cookie_lifespan =
661 clib_host_to_net_u32 (SCTP_VALID_COOKIE_LIFE);
Marco Varlese91389ac2018-01-31 11:00:01 +0100662
663 sctp_compute_mac (sctp_conn, state_cookie_param);
Marco Varlese191a5942017-10-30 18:17:21 +0100664
665 pointer_offset += sizeof (sctp_state_cookie_param_t);
666
667 if (PREDICT_TRUE (ip4_addr != NULL))
668 {
669 sctp_ipv4_addr_param_t *ipv4_addr =
670 (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
671
672 ipv4_addr->param_hdr.type =
673 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE);
674 ipv4_addr->param_hdr.length =
675 clib_host_to_net_u16 (SCTP_IPV4_ADDRESS_TYPE_LENGTH);
676 ipv4_addr->address.as_u32 = ip4_addr->as_u32;
677
678 pointer_offset += SCTP_IPV4_ADDRESS_TYPE_LENGTH;
679 }
680 if (PREDICT_TRUE (ip6_addr != NULL))
681 {
682 sctp_ipv6_addr_param_t *ipv6_addr =
Marco Varlesef429a932018-02-06 17:31:06 +0100683 (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
Marco Varlese191a5942017-10-30 18:17:21 +0100684
685 ipv6_addr->param_hdr.type =
686 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE);
687 ipv6_addr->param_hdr.length =
688 clib_host_to_net_u16 (SCTP_IPV6_ADDRESS_TYPE_LENGTH);
689 ipv6_addr->address.as_u64[0] = ip6_addr->as_u64[0];
690 ipv6_addr->address.as_u64[1] = ip6_addr->as_u64[1];
691
692 pointer_offset += SCTP_IPV6_ADDRESS_TYPE_LENGTH;
693 }
694
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100695 if (sctp_conn->sub_conn[idx].connection.is_ip4)
Marco Varlese191a5942017-10-30 18:17:21 +0100696 {
697 ip4_param = (sctp_ipv4_addr_param_t *) init_ack_chunk + pointer_offset;
698 ip4_param->address.as_u32 =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100699 sctp_conn->sub_conn[idx].connection.lcl_ip.ip4.as_u32;
Marco Varlese191a5942017-10-30 18:17:21 +0100700
701 pointer_offset += sizeof (sctp_ipv4_addr_param_t);
702 }
703 else
704 {
705 ip6_param = (sctp_ipv6_addr_param_t *) init_ack_chunk + pointer_offset;
706 ip6_param->address.as_u64[0] =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100707 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[0];
Marco Varlese191a5942017-10-30 18:17:21 +0100708 ip6_param->address.as_u64[1] =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100709 sctp_conn->sub_conn[idx].connection.lcl_ip.ip6.as_u64[1];
Marco Varlese191a5942017-10-30 18:17:21 +0100710
711 pointer_offset += sizeof (sctp_ipv6_addr_param_t);
712 }
713
714 /* src_port & dst_port are already in network byte-order */
715 init_ack_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100716 init_ack_chunk->sctp_hdr.src_port =
717 sctp_conn->sub_conn[idx].connection.lcl_port;
718 init_ack_chunk->sctp_hdr.dst_port =
719 sctp_conn->sub_conn[idx].connection.rmt_port;
720 /* the sctp_conn->verification_tag is already in network byte-order (being a copy of the init_tag coming with the INIT chunk) */
721 init_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
722 init_ack_chunk->initial_tsn =
723 clib_host_to_net_u32 (sctp_conn->local_initial_tsn);
724 SCTP_CONN_TRACKING_DBG ("init_ack_chunk->initial_tsn = %u",
725 init_ack_chunk->initial_tsn);
Marco Varlese191a5942017-10-30 18:17:21 +0100726
727 vnet_sctp_set_chunk_type (&init_ack_chunk->chunk_hdr, INIT_ACK);
728 vnet_sctp_set_chunk_length (&init_ack_chunk->chunk_hdr, chunk_len);
729
730 init_ack_chunk->initiate_tag =
731 clib_host_to_net_u32 (random_u32 (&random_seed));
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100732
Marco Varlese191a5942017-10-30 18:17:21 +0100733 init_ack_chunk->a_rwnd = clib_host_to_net_u32 (DEFAULT_A_RWND);
734 init_ack_chunk->inboud_streams_count =
735 clib_host_to_net_u16 (INBOUND_STREAMS_COUNT);
736 init_ack_chunk->outbound_streams_count =
737 clib_host_to_net_u16 (OUTBOUND_STREAMS_COUNT);
738
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100739 sctp_conn->local_tag = init_ack_chunk->initiate_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100740
Marco Varlese21c8baf2018-02-02 17:17:51 +0100741 /* Measure RTT with this */
742 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
743
Marco Varlese191a5942017-10-30 18:17:21 +0100744 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100745 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100746}
747
748/**
749 * Convert buffer to SHUTDOWN
750 */
751void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100752sctp_prepare_shutdown_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100753{
Marco Varlese191a5942017-10-30 18:17:21 +0100754 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
755 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
756
Marco Varlese191a5942017-10-30 18:17:21 +0100757 /* As per RFC 4960 the chunk_length value does NOT contemplate
758 * the size of the first header (see sctp_header_t) and any padding
759 */
760 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
761
762 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
763
764 sctp_shutdown_association_chunk_t *shutdown_chunk =
765 vlib_buffer_push_uninit (b, alloc_bytes);
766
767 shutdown_chunk->sctp_hdr.checksum = 0;
768 /* No need of host_to_net conversion, already in net-byte order */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100769 shutdown_chunk->sctp_hdr.src_port =
770 sctp_conn->sub_conn[idx].connection.lcl_port;
771 shutdown_chunk->sctp_hdr.dst_port =
772 sctp_conn->sub_conn[idx].connection.rmt_port;
773 shutdown_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100774 vnet_sctp_set_chunk_type (&shutdown_chunk->chunk_hdr, SHUTDOWN);
775 vnet_sctp_set_chunk_length (&shutdown_chunk->chunk_hdr, chunk_len);
776
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100777 shutdown_chunk->cumulative_tsn_ack = sctp_conn->last_rcvd_tsn;
Marco Varlese191a5942017-10-30 18:17:21 +0100778
779 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100780 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100781}
782
783/*
784 * Send SHUTDOWN
785 */
786void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100787sctp_send_shutdown (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +0100788{
789 vlib_buffer_t *b;
790 u32 bi;
791 sctp_main_t *tm = vnet_get_sctp_main ();
792 vlib_main_t *vm = vlib_get_main ();
793
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100794 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +0100795 return;
796
797 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
798 return;
799
800 b = vlib_get_buffer (vm, bi);
801 sctp_init_buffer (vm, b);
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100802 sctp_prepare_shutdown_chunk (sctp_conn, b);
Marco Varlese191a5942017-10-30 18:17:21 +0100803
804 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN);
Marco Varlesebe2251b2018-02-07 12:22:41 +0100805 sctp_enqueue_to_output_now (vm, b, bi,
806 sctp_conn->sub_conn[idx].connection.is_ip4);
Marco Varlese21c8baf2018-02-02 17:17:51 +0100807
808 /* Measure RTT with this */
809 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +0100810}
811
812/**
813 * Convert buffer to SHUTDOWN_ACK
814 */
815void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100816sctp_prepare_shutdown_ack_chunk (sctp_connection_t * sctp_conn,
817 vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100818{
819 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
820 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
821 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
822
823 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
824
825 sctp_shutdown_ack_chunk_t *shutdown_ack_chunk =
826 vlib_buffer_push_uninit (b, alloc_bytes);
827
828 shutdown_ack_chunk->sctp_hdr.checksum = 0;
829 /* No need of host_to_net conversion, already in net-byte order */
830 shutdown_ack_chunk->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100831 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +0100832 shutdown_ack_chunk->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100833 sctp_conn->sub_conn[idx].connection.rmt_port;
834 shutdown_ack_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100835
836 vnet_sctp_set_chunk_type (&shutdown_ack_chunk->chunk_hdr, SHUTDOWN_ACK);
837 vnet_sctp_set_chunk_length (&shutdown_ack_chunk->chunk_hdr, chunk_len);
838
839 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100840 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +0100841}
842
843/*
844 * Send SHUTDOWN_ACK
845 */
846void
Marco Varlesebe2251b2018-02-07 12:22:41 +0100847sctp_send_shutdown_ack (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100848{
Marco Varlese191a5942017-10-30 18:17:21 +0100849 vlib_main_t *vm = vlib_get_main ();
850
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100851 if (sctp_check_outstanding_data_chunks (sctp_conn) > 0)
Marco Varlese191a5942017-10-30 18:17:21 +0100852 return;
853
Marco Varlesebe2251b2018-02-07 12:22:41 +0100854 sctp_reuse_buffer (vm, b);
Marco Varlese191a5942017-10-30 18:17:21 +0100855
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100856 sctp_prepare_shutdown_ack_chunk (sctp_conn, b);
Marco Varlese191a5942017-10-30 18:17:21 +0100857
858 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_ACK);
Marco Varlese191a5942017-10-30 18:17:21 +0100859
Marco Varlese21c8baf2018-02-02 17:17:51 +0100860 /* Measure RTT with this */
861 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +0100862}
863
864/**
865 * Convert buffer to SACK
866 */
867void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100868sctp_prepare_sack_chunk (sctp_connection_t * sctp_conn, vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +0100869{
870 vlib_main_t *vm = vlib_get_main ();
871 u8 idx = sctp_pick_conn_idx_on_chunk (SACK);
872
873 sctp_reuse_buffer (vm, b);
874
875 u16 alloc_bytes = sizeof (sctp_selective_ack_chunk_t);
876
877 /* As per RFC 4960 the chunk_length value does NOT contemplate
878 * the size of the first header (see sctp_header_t) and any padding
879 */
880 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
881
882 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
883
884 sctp_selective_ack_chunk_t *sack = vlib_buffer_push_uninit (b, alloc_bytes);
885
886 sack->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100887 sack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
888 sack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
889 sack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +0100890 vnet_sctp_set_chunk_type (&sack->chunk_hdr, SACK);
891 vnet_sctp_set_chunk_length (&sack->chunk_hdr, chunk_len);
892
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100893 sack->cumulative_tsn_ack = sctp_conn->next_tsn_expected;
894
895 sctp_conn->ack_state = 0;
896
Marco Varlese191a5942017-10-30 18:17:21 +0100897 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100898 sctp_conn->sub_conn[idx].connection.c_index;
899}
900
901/**
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100902 * Convert buffer to HEARTBEAT_ACK
903 */
904void
905sctp_prepare_heartbeat_ack_chunk (sctp_connection_t * sctp_conn,
906 vlib_buffer_t * b)
907{
908 vlib_main_t *vm = vlib_get_main ();
909
910 u8 idx = sctp_pick_conn_idx_on_chunk (HEARTBEAT_ACK);
911 u16 alloc_bytes = sizeof (sctp_hb_ack_chunk_t);
912
913 sctp_reuse_buffer (vm, b);
914
915 /* As per RFC 4960 the chunk_length value does NOT contemplate
916 * the size of the first header (see sctp_header_t) and any padding
917 */
918 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
919
920 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
921
922 sctp_hb_ack_chunk_t *hb_ack = vlib_buffer_push_uninit (b, alloc_bytes);
923
924 hb_ack->sctp_hdr.checksum = 0;
925 /* No need of host_to_net conversion, already in net-byte order */
926 hb_ack->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
927 hb_ack->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
928 hb_ack->sctp_hdr.verification_tag = sctp_conn->remote_tag;
929 hb_ack->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
930 hb_ack->hb_info.param_hdr.length =
931 clib_host_to_net_u16 (sizeof (hb_ack->hb_info.hb_info));
932
933 vnet_sctp_set_chunk_type (&hb_ack->chunk_hdr, HEARTBEAT_ACK);
934 vnet_sctp_set_chunk_length (&hb_ack->chunk_hdr, chunk_len);
935
936 vnet_buffer (b)->sctp.connection_index =
937 sctp_conn->sub_conn[idx].connection.c_index;
938}
939
940/**
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100941 * Convert buffer to HEARTBEAT
942 */
943void
944sctp_prepare_heartbeat_chunk (sctp_connection_t * sctp_conn,
945 vlib_buffer_t * b)
946{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100947 u8 idx = sctp_pick_conn_idx_on_chunk (HEARTBEAT);
948 u16 alloc_bytes = sizeof (sctp_hb_req_chunk_t);
949
Marco Varlese8ad6a2d2018-01-26 16:50:01 +0100950 /* As per RFC 4960 the chunk_length value does NOT contemplate
951 * the size of the first header (see sctp_header_t) and any padding
952 */
953 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
954
955 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
956
957 sctp_hb_req_chunk_t *hb_req = vlib_buffer_push_uninit (b, alloc_bytes);
958
959 hb_req->sctp_hdr.checksum = 0;
960 /* No need of host_to_net conversion, already in net-byte order */
961 hb_req->sctp_hdr.src_port = sctp_conn->sub_conn[idx].connection.lcl_port;
962 hb_req->sctp_hdr.dst_port = sctp_conn->sub_conn[idx].connection.rmt_port;
963 hb_req->sctp_hdr.verification_tag = sctp_conn->remote_tag;
964 hb_req->hb_info.param_hdr.type = clib_host_to_net_u16 (1);
965 hb_req->hb_info.param_hdr.length =
966 clib_host_to_net_u16 (sizeof (hb_req->hb_info.hb_info));
967
968 vnet_sctp_set_chunk_type (&hb_req->chunk_hdr, HEARTBEAT);
969 vnet_sctp_set_chunk_length (&hb_req->chunk_hdr, chunk_len);
970
971 vnet_buffer (b)->sctp.connection_index =
972 sctp_conn->sub_conn[idx].connection.c_index;
973}
974
975void
976sctp_send_heartbeat (sctp_connection_t * sctp_conn)
977{
978 vlib_buffer_t *b;
979 u32 bi;
980 sctp_main_t *tm = vnet_get_sctp_main ();
981 vlib_main_t *vm = vlib_get_main ();
982
983 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
984 return;
985
986 b = vlib_get_buffer (vm, bi);
987 sctp_init_buffer (vm, b);
988 sctp_prepare_heartbeat_chunk (sctp_conn, b);
989
990 u8 idx = sctp_pick_conn_idx_on_state (SCTP_STATE_ESTABLISHED);
Marco Varlesedf5a99c2018-02-06 13:48:30 +0100991 sctp_enqueue_to_output_now (vm, b, bi,
992 sctp_conn->sub_conn[idx].connection.is_ip4);
993
994 sctp_conn->sub_conn[idx].unacknowledged_hb += 1;
Marco Varlese191a5942017-10-30 18:17:21 +0100995}
996
997/**
998 * Convert buffer to SHUTDOWN_COMPLETE
999 */
1000void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001001sctp_prepare_shutdown_complete_chunk (sctp_connection_t * sctp_conn,
Marco Varlese191a5942017-10-30 18:17:21 +01001002 vlib_buffer_t * b)
1003{
1004 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
1005 u16 alloc_bytes = sizeof (sctp_shutdown_association_chunk_t);
1006 alloc_bytes += vnet_sctp_calculate_padding (alloc_bytes);
1007
1008 u16 chunk_len = alloc_bytes - sizeof (sctp_header_t);
1009
1010 sctp_shutdown_complete_chunk_t *shutdown_complete =
1011 vlib_buffer_push_uninit (b, alloc_bytes);
1012
1013 shutdown_complete->sctp_hdr.checksum = 0;
1014 /* No need of host_to_net conversion, already in net-byte order */
1015 shutdown_complete->sctp_hdr.src_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001016 sctp_conn->sub_conn[idx].connection.lcl_port;
Marco Varlese191a5942017-10-30 18:17:21 +01001017 shutdown_complete->sctp_hdr.dst_port =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001018 sctp_conn->sub_conn[idx].connection.rmt_port;
1019 shutdown_complete->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001020
1021 vnet_sctp_set_chunk_type (&shutdown_complete->chunk_hdr, SHUTDOWN_COMPLETE);
1022 vnet_sctp_set_chunk_length (&shutdown_complete->chunk_hdr, chunk_len);
1023
1024 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001025 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +01001026}
1027
1028void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001029sctp_send_shutdown_complete (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +01001030{
1031 vlib_buffer_t *b;
1032 u32 bi;
1033 sctp_main_t *tm = vnet_get_sctp_main ();
1034 vlib_main_t *vm = vlib_get_main ();
1035
1036 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1037 return;
1038
1039 b = vlib_get_buffer (vm, bi);
1040 sctp_init_buffer (vm, b);
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001041 sctp_prepare_shutdown_complete_chunk (sctp_conn, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001042
1043 u8 idx = sctp_pick_conn_idx_on_chunk (SHUTDOWN_COMPLETE);
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001044 sctp_enqueue_to_output (vm, b, bi,
1045 sctp_conn->sub_conn[idx].connection.is_ip4);
Marco Varlese191a5942017-10-30 18:17:21 +01001046
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001047 sctp_conn->state = SCTP_STATE_CLOSED;
Marco Varlese191a5942017-10-30 18:17:21 +01001048}
1049
1050
1051/*
1052 * Send INIT
1053 */
1054void
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001055sctp_send_init (sctp_connection_t * sctp_conn)
Marco Varlese191a5942017-10-30 18:17:21 +01001056{
1057 vlib_buffer_t *b;
1058 u32 bi;
1059 sctp_main_t *tm = vnet_get_sctp_main ();
1060 vlib_main_t *vm = vlib_get_main ();
1061
1062 if (PREDICT_FALSE (sctp_get_free_buffer_index (tm, &bi)))
1063 return;
1064
1065 b = vlib_get_buffer (vm, bi);
1066 u8 idx = sctp_pick_conn_idx_on_chunk (INIT);
1067
1068 sctp_init_buffer (vm, b);
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001069 sctp_prepare_init_chunk (sctp_conn, b);
Marco Varlese191a5942017-10-30 18:17:21 +01001070
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001071 sctp_push_ip_hdr (tm, &sctp_conn->sub_conn[idx], b);
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001072 sctp_enqueue_to_ip_lookup (vm, b, bi, sctp_conn->sub_conn[idx].c_is_ip4);
Marco Varlese191a5942017-10-30 18:17:21 +01001073
Marco Varlese21c8baf2018-02-02 17:17:51 +01001074 /* Measure RTT with this */
1075 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
1076
Marco Varlese191a5942017-10-30 18:17:21 +01001077 /* Start the T1_INIT timer */
Marco Varlese21c8baf2018-02-02 17:17:51 +01001078 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T1_INIT,
1079 sctp_conn->sub_conn[idx].RTO);
1080
Marco Varlese191a5942017-10-30 18:17:21 +01001081 /* Change state to COOKIE_WAIT */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001082 sctp_conn->state = SCTP_STATE_COOKIE_WAIT;
Marco Varlese191a5942017-10-30 18:17:21 +01001083}
1084
Marco Varlese191a5942017-10-30 18:17:21 +01001085/**
1086 * Push SCTP header and update connection variables
1087 */
1088static void
Marco Varlese21c8baf2018-02-02 17:17:51 +01001089sctp_push_hdr_i (sctp_connection_t * sctp_conn, u8 idx, vlib_buffer_t * b,
Marco Varlese191a5942017-10-30 18:17:21 +01001090 sctp_state_t next_state)
1091{
Marco Varlese191a5942017-10-30 18:17:21 +01001092 u16 data_len =
1093 b->current_length + b->total_length_not_including_first_buffer;
1094 ASSERT (!b->total_length_not_including_first_buffer
1095 || (b->flags & VLIB_BUFFER_NEXT_PRESENT));
1096
1097 SCTP_ADV_DBG_OUTPUT ("b->current_length = %u, "
1098 "b->current_data = %p "
1099 "data_len = %u",
1100 b->current_length, b->current_data, data_len);
1101
1102 u16 bytes_to_add = sizeof (sctp_payload_data_chunk_t);
1103 u16 chunk_length = data_len + bytes_to_add - sizeof (sctp_header_t);
1104
1105 bytes_to_add += vnet_sctp_calculate_padding (bytes_to_add + data_len);
1106
1107 sctp_payload_data_chunk_t *data_chunk =
1108 vlib_buffer_push_uninit (b, bytes_to_add);
1109
1110 data_chunk->sctp_hdr.checksum = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001111 data_chunk->sctp_hdr.src_port =
1112 sctp_conn->sub_conn[idx].connection.lcl_port;
1113 data_chunk->sctp_hdr.dst_port =
1114 sctp_conn->sub_conn[idx].connection.rmt_port;
1115 data_chunk->sctp_hdr.verification_tag = sctp_conn->remote_tag;
Marco Varlese191a5942017-10-30 18:17:21 +01001116
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001117 data_chunk->tsn = clib_host_to_net_u32 (sctp_conn->next_tsn);
Marco Varlese191a5942017-10-30 18:17:21 +01001118 data_chunk->stream_id = clib_host_to_net_u16 (0);
1119 data_chunk->stream_seq = clib_host_to_net_u16 (0);
1120
1121 vnet_sctp_set_chunk_type (&data_chunk->chunk_hdr, DATA);
1122 vnet_sctp_set_chunk_length (&data_chunk->chunk_hdr, chunk_length);
1123
Marco Varlese91389ac2018-01-31 11:00:01 +01001124 vnet_sctp_set_bbit (&data_chunk->chunk_hdr);
1125 vnet_sctp_set_ebit (&data_chunk->chunk_hdr);
1126
Marco Varlese191a5942017-10-30 18:17:21 +01001127 SCTP_ADV_DBG_OUTPUT ("POINTER_WITH_DATA = %p, DATA_OFFSET = %u",
1128 b->data, b->current_data);
1129
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001130 sctp_conn->next_tsn += data_len;
1131
Marco Varlese191a5942017-10-30 18:17:21 +01001132 vnet_buffer (b)->sctp.connection_index =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001133 sctp_conn->sub_conn[idx].connection.c_index;
Marco Varlese191a5942017-10-30 18:17:21 +01001134}
1135
1136u32
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001137sctp_push_header (transport_connection_t * trans_conn, vlib_buffer_t * b)
Marco Varlese191a5942017-10-30 18:17:21 +01001138{
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001139 sctp_connection_t *sctp_conn =
1140 sctp_get_connection_from_transport (trans_conn);
Marco Varlese191a5942017-10-30 18:17:21 +01001141
Marco Varlese21c8baf2018-02-02 17:17:51 +01001142 u8 idx = sctp_pick_conn_idx_on_chunk (DATA);
1143
Marco Varlesedf5a99c2018-02-06 13:48:30 +01001144 if (sctp_conn->sub_conn[idx].unacknowledged_hb >
1145 SCTP_ASSOCIATION_MAX_RETRANS)
1146 {
1147 // The remote-peer is considered to be unreachable hence shutting down
1148
1149 /* Start cleanup. App wasn't notified yet so use delete notify as
1150 * opposed to delete to cleanup session layer state. */
1151 stream_session_delete_notify (&sctp_conn->sub_conn
1152 [MAIN_SCTP_SUB_CONN_IDX].connection);
1153
1154 sctp_connection_timers_reset (sctp_conn);
1155
1156 sctp_connection_cleanup (sctp_conn);
1157 }
1158
Marco Varlese21c8baf2018-02-02 17:17:51 +01001159 sctp_push_hdr_i (sctp_conn, idx, b, SCTP_STATE_ESTABLISHED);
1160
1161 if (sctp_conn->sub_conn[idx].RTO_pending == 0)
Marco Varlese191a5942017-10-30 18:17:21 +01001162 {
Marco Varlese21c8baf2018-02-02 17:17:51 +01001163 sctp_conn->sub_conn[idx].RTO_pending = 1;
1164 sctp_conn->sub_conn[idx].rtt_ts = sctp_time_now ();
Marco Varlese191a5942017-10-30 18:17:21 +01001165 }
Marco Varlese21c8baf2018-02-02 17:17:51 +01001166
Marco Varlese191a5942017-10-30 18:17:21 +01001167 sctp_trajectory_add_start (b0, 3);
1168
1169 return 0;
1170
1171}
1172
1173always_inline uword
1174sctp46_output_inline (vlib_main_t * vm,
1175 vlib_node_runtime_t * node,
1176 vlib_frame_t * from_frame, int is_ip4)
1177{
1178 u32 n_left_from, next_index, *from, *to_next;
1179 u32 my_thread_index = vm->thread_index;
1180
1181 from = vlib_frame_vector_args (from_frame);
1182 n_left_from = from_frame->n_vectors;
1183 next_index = node->cached_next_index;
1184 sctp_set_time_now (my_thread_index);
1185
1186 while (n_left_from > 0)
1187 {
1188 u32 n_left_to_next;
1189
1190 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1191
1192 while (n_left_from > 0 && n_left_to_next > 0)
1193 {
1194 u32 bi0;
1195 vlib_buffer_t *b0;
1196 sctp_header_t *sctp_hdr = 0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001197 sctp_connection_t *sctp_conn;
Marco Varlese191a5942017-10-30 18:17:21 +01001198 sctp_tx_trace_t *t0;
1199 sctp_header_t *th0 = 0;
1200 u32 error0 = SCTP_ERROR_PKTS_SENT, next0 =
1201 SCTP_OUTPUT_NEXT_IP_LOOKUP;
1202
1203#if SCTP_DEBUG_STATE_MACHINE
1204 u16 packet_length = 0;
1205#endif
1206
1207 bi0 = from[0];
1208 to_next[0] = bi0;
1209 from += 1;
1210 to_next += 1;
1211 n_left_from -= 1;
1212 n_left_to_next -= 1;
1213
1214 b0 = vlib_get_buffer (vm, bi0);
Marco Varlesebe2251b2018-02-07 12:22:41 +01001215
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001216 sctp_conn =
1217 sctp_connection_get (vnet_buffer (b0)->sctp.connection_index,
1218 my_thread_index);
Marco Varlese191a5942017-10-30 18:17:21 +01001219
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001220 if (PREDICT_FALSE (sctp_conn == 0))
Marco Varlese191a5942017-10-30 18:17:21 +01001221 {
1222 error0 = SCTP_ERROR_INVALID_CONNECTION;
1223 next0 = SCTP_OUTPUT_NEXT_DROP;
1224 goto done;
1225 }
1226
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001227 u8 idx = sctp_pick_conn_idx_on_state (sctp_conn->state);
Marco Varlese191a5942017-10-30 18:17:21 +01001228
1229 th0 = vlib_buffer_get_current (b0);
1230
1231 if (is_ip4)
1232 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001233 ip4_header_t *iph4 = vlib_buffer_push_ip4 (vm,
1234 b0,
1235 &sctp_conn->sub_conn
1236 [idx].connection.
1237 lcl_ip.ip4,
1238 &sctp_conn->
1239 sub_conn
1240 [idx].connection.
1241 rmt_ip.ip4,
1242 IP_PROTOCOL_SCTP, 1);
Marco Varlese191a5942017-10-30 18:17:21 +01001243
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001244 u32 checksum = ip4_sctp_compute_checksum (vm, b0, iph4);
Marco Varlese191a5942017-10-30 18:17:21 +01001245
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001246 sctp_hdr = ip4_next_header (iph4);
Marco Varlese191a5942017-10-30 18:17:21 +01001247 sctp_hdr->checksum = checksum;
1248
1249 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001250
1251#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001252 packet_length = clib_net_to_host_u16 (iph4->length);
Marco Varlese191a5942017-10-30 18:17:21 +01001253#endif
1254 }
1255 else
1256 {
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001257 ip6_header_t *iph6 = vlib_buffer_push_ip6 (vm,
1258 b0,
1259 &sctp_conn->sub_conn
1260 [idx].
1261 connection.lcl_ip.
1262 ip6,
1263 &sctp_conn->sub_conn
1264 [idx].
1265 connection.rmt_ip.
1266 ip6,
1267 IP_PROTOCOL_SCTP);
Marco Varlese191a5942017-10-30 18:17:21 +01001268
1269 int bogus = ~0;
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001270 u32 checksum = ip6_sctp_compute_checksum (vm, b0, iph6, &bogus);
Marco Varlese191a5942017-10-30 18:17:21 +01001271 ASSERT (!bogus);
1272
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001273 sctp_hdr = ip6_next_header (iph6);
Marco Varlese191a5942017-10-30 18:17:21 +01001274 sctp_hdr->checksum = checksum;
1275
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001276 vnet_buffer (b0)->l3_hdr_offset = (u8 *) iph6 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001277 vnet_buffer (b0)->l4_hdr_offset = (u8 *) th0 - b0->data;
Marco Varlese191a5942017-10-30 18:17:21 +01001278
1279#if SCTP_DEBUG_STATE_MACHINE
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001280 packet_length = clib_net_to_host_u16 (iph6->payload_length);
Marco Varlese191a5942017-10-30 18:17:21 +01001281#endif
1282 }
1283
1284 u8 is_valid =
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001285 (sctp_conn->sub_conn[idx].connection.lcl_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001286 sctp_hdr->src_port
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001287 || sctp_conn->sub_conn[idx].connection.lcl_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001288 sctp_hdr->dst_port)
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001289 && (sctp_conn->sub_conn[idx].connection.rmt_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001290 sctp_hdr->dst_port
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001291 || sctp_conn->sub_conn[idx].connection.rmt_port ==
Marco Varlese191a5942017-10-30 18:17:21 +01001292 sctp_hdr->src_port);
1293
1294 sctp_full_hdr_t *full_hdr = (sctp_full_hdr_t *) sctp_hdr;
1295 u8 chunk_type = vnet_sctp_get_chunk_type (&full_hdr->common_hdr);
1296
1297 if (!is_valid)
1298 {
1299 SCTP_DBG_STATE_MACHINE ("BUFFER IS INCORRECT: conn_index = %u, "
1300 "packet_length = %u, "
1301 "chunk_type = %u [%s], "
1302 "connection.lcl_port = %u, sctp_hdr->src_port = %u, "
1303 "connection.rmt_port = %u, sctp_hdr->dst_port = %u",
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001304 sctp_conn->sub_conn
Marco Varlese191a5942017-10-30 18:17:21 +01001305 [idx].connection.c_index, packet_length,
1306 chunk_type,
1307 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001308 sctp_conn->sub_conn[idx].
1309 connection.lcl_port, sctp_hdr->src_port,
1310 sctp_conn->sub_conn[idx].
1311 connection.rmt_port,
Marco Varlese191a5942017-10-30 18:17:21 +01001312 sctp_hdr->dst_port);
1313
1314 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1315 next0 = SCTP_OUTPUT_NEXT_DROP;
1316 goto done;
1317 }
1318
1319 SCTP_DBG_STATE_MACHINE
1320 ("CONN_INDEX = %u, CURR_CONN_STATE = %u (%s), "
1321 "CHUNK_TYPE = %s, " "SRC_PORT = %u, DST_PORT = %u",
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001322 sctp_conn->sub_conn[idx].connection.c_index,
1323 sctp_conn->state, sctp_state_to_string (sctp_conn->state),
Marco Varlese191a5942017-10-30 18:17:21 +01001324 sctp_chunk_to_string (chunk_type), full_hdr->hdr.src_port,
1325 full_hdr->hdr.dst_port);
1326
1327 if (chunk_type == DATA)
1328 SCTP_ADV_DBG_OUTPUT ("PACKET_LENGTH = %u", packet_length);
1329
1330 /* Let's make sure the state-machine does not send anything crazy */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001331 switch (sctp_conn->state)
Marco Varlese191a5942017-10-30 18:17:21 +01001332 {
1333 case SCTP_STATE_CLOSED:
1334 {
1335 if (chunk_type != INIT && chunk_type != INIT_ACK)
1336 {
1337 SCTP_DBG_STATE_MACHINE
1338 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1339 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001340 sctp_state_to_string (sctp_conn->state));
Marco Varlese191a5942017-10-30 18:17:21 +01001341
1342 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1343 next0 = SCTP_OUTPUT_NEXT_DROP;
1344 goto done;
1345 }
1346 break;
1347 }
1348 case SCTP_STATE_ESTABLISHED:
1349 if (chunk_type != DATA && chunk_type != HEARTBEAT &&
1350 chunk_type != HEARTBEAT_ACK && chunk_type != SACK &&
1351 chunk_type != COOKIE_ACK && chunk_type != SHUTDOWN)
1352 {
1353 SCTP_DBG_STATE_MACHINE
1354 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1355 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001356 sctp_state_to_string (sctp_conn->state));
Marco Varlese191a5942017-10-30 18:17:21 +01001357
1358 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1359 next0 = SCTP_OUTPUT_NEXT_DROP;
1360 goto done;
1361 }
1362 break;
1363 case SCTP_STATE_COOKIE_WAIT:
1364 if (chunk_type != COOKIE_ECHO)
1365 {
1366 SCTP_DBG_STATE_MACHINE
1367 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1368 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001369 sctp_state_to_string (sctp_conn->state));
Marco Varlese191a5942017-10-30 18:17:21 +01001370
1371 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1372 next0 = SCTP_OUTPUT_NEXT_DROP;
1373 goto done;
1374 }
1375 /* Change state */
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001376 sctp_conn->state = SCTP_STATE_COOKIE_ECHOED;
Marco Varlese191a5942017-10-30 18:17:21 +01001377 break;
Marco Varlesebe2251b2018-02-07 12:22:41 +01001378 case SCTP_STATE_SHUTDOWN_SENT:
1379 if (chunk_type != SHUTDOWN_COMPLETE)
1380 {
1381 SCTP_DBG_STATE_MACHINE
1382 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1383 sctp_chunk_to_string (chunk_type),
1384 sctp_state_to_string (sctp_conn->state));
1385
1386 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1387 next0 = SCTP_OUTPUT_NEXT_DROP;
1388 goto done;
1389 }
1390 case SCTP_STATE_SHUTDOWN_RECEIVED:
1391 if (chunk_type != SHUTDOWN_ACK)
1392 {
1393 SCTP_DBG_STATE_MACHINE
1394 ("Sending the wrong chunk (%s) based on state-machine status (%s)",
1395 sctp_chunk_to_string (chunk_type),
1396 sctp_state_to_string (sctp_conn->state));
1397
1398 error0 = SCTP_ERROR_UNKOWN_CHUNK;
1399 next0 = SCTP_OUTPUT_NEXT_DROP;
1400 goto done;
1401 }
Marco Varlese191a5942017-10-30 18:17:21 +01001402 default:
1403 SCTP_DBG_STATE_MACHINE
1404 ("Sending chunk (%s) based on state-machine status (%s)",
1405 sctp_chunk_to_string (chunk_type),
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001406 sctp_state_to_string (sctp_conn->state));
Marco Varlese191a5942017-10-30 18:17:21 +01001407 break;
1408 }
1409
Marco Varlesebe2251b2018-02-07 12:22:41 +01001410 switch (chunk_type)
Marco Varlese191a5942017-10-30 18:17:21 +01001411 {
Marco Varlesebe2251b2018-02-07 12:22:41 +01001412 case DATA:
1413 {
1414 sctp_timer_update (sctp_conn, idx, SCTP_TIMER_T3_RXTX,
1415 sctp_conn->sub_conn[idx].RTO);
1416 break;
1417 }
1418 case SHUTDOWN:
1419 {
1420 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1421 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1422 sctp_conn->sub_conn[idx].RTO);
1423 sctp_conn->state = SCTP_STATE_SHUTDOWN_SENT;
1424 break;
1425 }
1426 case SHUTDOWN_ACK:
1427 {
1428 /* Start the SCTP_TIMER_T2_SHUTDOWN timer */
1429 sctp_timer_set (sctp_conn, idx, SCTP_TIMER_T2_SHUTDOWN,
1430 sctp_conn->sub_conn[idx].RTO);
1431 sctp_conn->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
1432 break;
1433 }
Marco Varlese191a5942017-10-30 18:17:21 +01001434 }
1435
1436 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
1437 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
1438
1439 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
1440
1441 SCTP_DBG_STATE_MACHINE ("CONNECTION_INDEX = %u, "
1442 "NEW_STATE = %s, "
1443 "CHUNK_SENT = %s",
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001444 sctp_conn->sub_conn[idx].connection.c_index,
1445 sctp_state_to_string (sctp_conn->state),
Marco Varlese191a5942017-10-30 18:17:21 +01001446 sctp_chunk_to_string (chunk_type));
1447
1448 vnet_sctp_common_hdr_params_host_to_net (&full_hdr->common_hdr);
1449
1450 done:
1451 b0->error = node->errors[error0];
1452 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1453 {
1454 t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
1455 if (th0)
1456 {
1457 clib_memcpy (&t0->sctp_header, th0,
1458 sizeof (t0->sctp_header));
1459 }
1460 else
1461 {
1462 memset (&t0->sctp_header, 0, sizeof (t0->sctp_header));
1463 }
Marco Varlese8ad6a2d2018-01-26 16:50:01 +01001464 clib_memcpy (&t0->sctp_connection, sctp_conn,
Marco Varlese191a5942017-10-30 18:17:21 +01001465 sizeof (t0->sctp_connection));
1466 }
1467
1468 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1469 n_left_to_next, bi0, next0);
1470 }
1471
1472 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1473 }
1474
1475 return from_frame->n_vectors;
1476}
1477
1478static uword
1479sctp4_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1480 vlib_frame_t * from_frame)
1481{
1482 return sctp46_output_inline (vm, node, from_frame, 1 /* is_ip4 */ );
1483}
1484
1485static uword
1486sctp6_output (vlib_main_t * vm, vlib_node_runtime_t * node,
1487 vlib_frame_t * from_frame)
1488{
1489 return sctp46_output_inline (vm, node, from_frame, 0 /* is_ip4 */ );
1490}
1491
1492/* *INDENT-OFF* */
1493VLIB_REGISTER_NODE (sctp4_output_node) =
1494{
1495 .function = sctp4_output,.name = "sctp4-output",
1496 /* Takes a vector of packets. */
1497 .vector_size = sizeof (u32),
1498 .n_errors = SCTP_N_ERROR,
1499 .error_strings = sctp_error_strings,
1500 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1501 .next_nodes = {
1502#define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1503 foreach_sctp4_output_next
1504#undef _
1505 },
1506 .format_buffer = format_sctp_header,
1507 .format_trace = format_sctp_tx_trace,
1508};
1509/* *INDENT-ON* */
1510
1511VLIB_NODE_FUNCTION_MULTIARCH (sctp4_output_node, sctp4_output);
1512
1513/* *INDENT-OFF* */
1514VLIB_REGISTER_NODE (sctp6_output_node) =
1515{
1516 .function = sctp6_output,
1517 .name = "sctp6-output",
1518 /* Takes a vector of packets. */
1519 .vector_size = sizeof (u32),
1520 .n_errors = SCTP_N_ERROR,
1521 .error_strings = sctp_error_strings,
1522 .n_next_nodes = SCTP_OUTPUT_N_NEXT,
1523 .next_nodes = {
1524#define _(s,n) [SCTP_OUTPUT_NEXT_##s] = n,
1525 foreach_sctp6_output_next
1526#undef _
1527 },
1528 .format_buffer = format_sctp_header,
1529 .format_trace = format_sctp_tx_trace,
1530};
1531/* *INDENT-ON* */
1532
1533VLIB_NODE_FUNCTION_MULTIARCH (sctp6_output_node, sctp6_output);
1534
1535/*
1536 * fd.io coding-style-patch-verification: ON
1537 *
1538 * Local Variables:
1539 * eval: (c-set-style "gnu")
1540 * End:
1541 */