blob: 10fc6f1f5595d212e0a8d620701c61dd3cfea786 [file] [log] [blame]
Klement Sekera75e7d132017-09-20 08:26:30 +02001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
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
16/**
17 * @file
18 * @brief IPv6 Reassembly.
19 *
20 * This file contains the source code for IPv6 reassembly.
21 */
22
23#include <vppinfra/vec.h>
24#include <vnet/vnet.h>
25#include <vnet/ip/ip.h>
26#include <vppinfra/bihash_48_8.h>
27#include <vnet/ip/ip6_reassembly.h>
28
29#define MSEC_PER_SEC 1000
30#define IP6_REASS_TIMEOUT_DEFAULT_MS 100
31#define IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 10000 // 10 seconds default
Klement Sekera4c533132018-02-22 11:41:12 +010032#define IP6_REASS_MAX_REASSEMBLIES_DEFAULT 1024
Klement Sekera3a343d42019-05-16 14:35:46 +020033#define IP6_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
Klement Sekera75e7d132017-09-20 08:26:30 +020034#define IP6_REASS_HT_LOAD_FACTOR (0.75)
35
Klement Sekerad0f70a32018-12-14 17:24:13 +010036typedef enum
37{
38 IP6_REASS_RC_OK,
39 IP6_REASS_RC_INTERNAL_ERROR,
Klement Sekera3a343d42019-05-16 14:35:46 +020040 IP6_REASS_RC_TOO_MANY_FRAGMENTS,
Klement Sekeraf883f6a2019-02-13 11:01:32 +010041 IP6_REASS_RC_NO_BUF,
Klement Sekerad0f70a32018-12-14 17:24:13 +010042} ip6_reass_rc_t;
Klement Sekera75e7d132017-09-20 08:26:30 +020043
44typedef struct
45{
46 union
47 {
48 struct
49 {
50 ip6_address_t src;
51 ip6_address_t dst;
Klement Sekera75e7d132017-09-20 08:26:30 +020052 u32 xx_id;
Klement Sekera75e7d132017-09-20 08:26:30 +020053 u32 frag_id;
Klement Sekera8dcfed52018-06-28 11:16:15 +020054 u8 unused[7];
55 u8 proto;
Klement Sekera75e7d132017-09-20 08:26:30 +020056 };
57 u64 as_u64[6];
58 };
59} ip6_reass_key_t;
60
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -080061typedef union
62{
63 struct
64 {
65 u32 reass_index;
66 u32 thread_index;
67 };
68 u64 as_u64;
69} ip6_reass_val_t;
70
71typedef union
72{
73 struct
74 {
75 ip6_reass_key_t k;
76 ip6_reass_val_t v;
77 };
78 clib_bihash_kv_48_8_t kv;
79} ip6_reass_kv_t;
80
81
Klement Sekera75e7d132017-09-20 08:26:30 +020082always_inline u32
Klement Sekera75e7d132017-09-20 08:26:30 +020083ip6_reass_buffer_get_data_offset (vlib_buffer_t * b)
84{
85 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010086 return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
Klement Sekera75e7d132017-09-20 08:26:30 +020087}
88
89always_inline u16
90ip6_reass_buffer_get_data_len (vlib_buffer_t * b)
91{
92 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekerad0f70a32018-12-14 17:24:13 +010093 return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
94 (vnb->ip.reass.fragment_first + ip6_reass_buffer_get_data_offset (b)) + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +020095}
96
97typedef struct
98{
99 // hash table key
100 ip6_reass_key_t key;
Klement Sekera75e7d132017-09-20 08:26:30 +0200101 // time when last packet was received
102 f64 last_heard;
103 // internal id of this reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100104 u64 id;
Klement Sekera75e7d132017-09-20 08:26:30 +0200105 // buffer index of first buffer in this reassembly context
106 u32 first_bi;
107 // last octet of packet, ~0 until fragment without more_fragments arrives
108 u32 last_packet_octet;
109 // length of data collected so far
110 u32 data_len;
111 // trace operation counter
112 u32 trace_op_counter;
Klement Sekerae8498652019-06-17 12:23:15 +0000113 // next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200114 u32 next_index;
Klement Sekerae8498652019-06-17 12:23:15 +0000115 // error next index - used by custom apps (~0 if not set)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200116 u32 error_next_index;
Klement Sekera4c533132018-02-22 11:41:12 +0100117 // minimum fragment length for this reassembly - used to estimate MTU
118 u16 min_fragment_length;
Klement Sekera3a343d42019-05-16 14:35:46 +0200119 // number of fragments for this reassembly
120 u32 fragments_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200121} ip6_reass_t;
122
123typedef struct
124{
Klement Sekera4c533132018-02-22 11:41:12 +0100125 ip6_reass_t *pool;
126 u32 reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +0100127 u32 id_counter;
128 clib_spinlock_t lock;
129} ip6_reass_per_thread_t;
130
131typedef struct
132{
Klement Sekera75e7d132017-09-20 08:26:30 +0200133 // IPv6 config
134 u32 timeout_ms;
135 f64 timeout;
136 u32 expire_walk_interval_ms;
Klement Sekera3a343d42019-05-16 14:35:46 +0200137 // maximum number of fragments in one reassembly
138 u32 max_reass_len;
139 // maximum number of reassemblies
Klement Sekera75e7d132017-09-20 08:26:30 +0200140 u32 max_reass_n;
141
142 // IPv6 runtime
Klement Sekera75e7d132017-09-20 08:26:30 +0200143 clib_bihash_48_8_t hash;
Klement Sekera4c533132018-02-22 11:41:12 +0100144
145 // per-thread data
146 ip6_reass_per_thread_t *per_thread_data;
Klement Sekera75e7d132017-09-20 08:26:30 +0200147
148 // convenience
149 vlib_main_t *vlib_main;
150 vnet_main_t *vnet_main;
151
152 // node index of ip6-drop node
153 u32 ip6_drop_idx;
154 u32 ip6_icmp_error_idx;
155 u32 ip6_reass_expire_node_idx;
156
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800157 /** Worker handoff */
158 u32 fq_index;
159 u32 fq_feature_index;
160
Klement Sekera75e7d132017-09-20 08:26:30 +0200161} ip6_reass_main_t;
162
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700163extern ip6_reass_main_t ip6_reass_main;
164
165#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +0200166ip6_reass_main_t ip6_reass_main;
Filip Tehlar26ea14e2019-03-11 05:30:21 -0700167#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +0200168
169typedef enum
170{
171 IP6_REASSEMBLY_NEXT_INPUT,
172 IP6_REASSEMBLY_NEXT_DROP,
173 IP6_REASSEMBLY_NEXT_ICMP_ERROR,
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800174 IP6_REASSEMBLY_NEXT_HANDOFF,
Klement Sekera75e7d132017-09-20 08:26:30 +0200175 IP6_REASSEMBLY_N_NEXT,
176} ip6_reass_next_t;
177
178typedef enum
179{
180 RANGE_NEW,
181 RANGE_OVERLAP,
182 ICMP_ERROR_RT_EXCEEDED,
183 ICMP_ERROR_FL_TOO_BIG,
184 ICMP_ERROR_FL_NOT_MULT_8,
185 FINALIZE,
186} ip6_reass_trace_operation_e;
187
188typedef struct
189{
190 u16 range_first;
191 u16 range_last;
192 u32 range_bi;
193 i32 data_offset;
194 u32 data_len;
195 u32 first_bi;
196} ip6_reass_range_trace_t;
197
198typedef struct
199{
200 ip6_reass_trace_operation_e action;
Klement Sekera75e7d132017-09-20 08:26:30 +0200201 u32 reass_id;
202 ip6_reass_range_trace_t trace_range;
203 u32 size_diff;
204 u32 op_id;
205 u32 fragment_first;
206 u32 fragment_last;
207 u32 total_data_len;
208} ip6_reass_trace_t;
209
210static void
211ip6_reass_trace_details (vlib_main_t * vm, u32 bi,
212 ip6_reass_range_trace_t * trace)
213{
214 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
215 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
216 trace->range_first = vnb->ip.reass.range_first;
217 trace->range_last = vnb->ip.reass.range_last;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100218 trace->data_offset = ip6_reass_buffer_get_data_offset (b);
219 trace->data_len = ip6_reass_buffer_get_data_len (b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200220 trace->range_bi = bi;
221}
222
Klement Sekera4c533132018-02-22 11:41:12 +0100223static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200224format_ip6_reass_range_trace (u8 * s, va_list * args)
225{
226 ip6_reass_range_trace_t *trace = va_arg (*args, ip6_reass_range_trace_t *);
227 s = format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
228 trace->range_last, trace->data_offset, trace->data_len,
229 trace->range_bi);
230 return s;
231}
232
Klement Sekera4c533132018-02-22 11:41:12 +0100233static u8 *
Klement Sekera75e7d132017-09-20 08:26:30 +0200234format_ip6_reass_trace (u8 * s, va_list * args)
235{
236 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
237 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
238 ip6_reass_trace_t *t = va_arg (*args, ip6_reass_trace_t *);
239 s = format (s, "reass id: %u, op id: %u ", t->reass_id, t->op_id);
240 u32 indent = format_get_indent (s);
241 s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
242 t->trace_range.first_bi, t->total_data_len, t->fragment_first,
243 t->fragment_last);
244 switch (t->action)
245 {
246 case RANGE_NEW:
247 s = format (s, "\n%Unew %U", format_white_space, indent,
248 format_ip6_reass_range_trace, &t->trace_range);
249 break;
250 case RANGE_OVERLAP:
251 s = format (s, "\n%Uoverlap %U", format_white_space, indent,
252 format_ip6_reass_range_trace, &t->trace_range);
253 break;
254 case ICMP_ERROR_FL_TOO_BIG:
255 s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
256 format_white_space, indent, format_ip6_reass_range_trace,
257 &t->trace_range);
258 break;
259 case ICMP_ERROR_FL_NOT_MULT_8:
260 s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
261 format_white_space, indent, format_ip6_reass_range_trace,
262 &t->trace_range);
263 break;
264 case ICMP_ERROR_RT_EXCEEDED:
265 s = format (s, "\n%Uicmp-error - reassembly time exceeded",
266 format_white_space, indent);
267 break;
268 case FINALIZE:
269 s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
270 break;
271 }
272 return s;
273}
274
275static void
276ip6_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
277 ip6_reass_main_t * rm, ip6_reass_t * reass,
278 u32 bi, ip6_reass_trace_operation_e action,
279 u32 size_diff)
280{
281 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
282 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
Klement Sekera4c533132018-02-22 11:41:12 +0100283 if (pool_is_free_index (vm->trace_main.trace_buffer_pool, b->trace_index))
284 {
285 // this buffer's trace is gone
286 b->flags &= ~VLIB_BUFFER_IS_TRACED;
287 return;
288 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200289 ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200290 t->reass_id = reass->id;
291 t->action = action;
292 ip6_reass_trace_details (vm, bi, &t->trace_range);
293 t->size_diff = size_diff;
294 t->op_id = reass->trace_op_counter;
295 ++reass->trace_op_counter;
296 t->fragment_first = vnb->ip.reass.fragment_first;
297 t->fragment_last = vnb->ip.reass.fragment_last;
298 t->trace_range.first_bi = reass->first_bi;
299 t->total_data_len = reass->data_len;
300#if 0
301 static u8 *s = NULL;
302 s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
303 printf ("%.*s\n", vec_len (s), s);
304 fflush (stdout);
305 vec_reset_length (s);
306#endif
307}
308
Klement Sekera4c533132018-02-22 11:41:12 +0100309always_inline void
310ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
311 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200312{
313 clib_bihash_kv_48_8_t kv;
314 kv.key[0] = reass->key.as_u64[0];
315 kv.key[1] = reass->key.as_u64[1];
316 kv.key[2] = reass->key.as_u64[2];
317 kv.key[3] = reass->key.as_u64[3];
318 kv.key[4] = reass->key.as_u64[4];
319 kv.key[5] = reass->key.as_u64[5];
320 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100321 pool_put (rt->pool, reass);
322 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200323}
324
Klement Sekera4c533132018-02-22 11:41:12 +0100325always_inline void
Klement Sekera21aa8f12019-05-20 12:27:33 +0200326ip6_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerae8498652019-06-17 12:23:15 +0000327 ip6_reass_main_t * rm, ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200328{
329 u32 range_bi = reass->first_bi;
330 vlib_buffer_t *range_b;
331 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100332 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200333 while (~0 != range_bi)
334 {
335 range_b = vlib_get_buffer (vm, range_bi);
336 range_vnb = vnet_buffer (range_b);
337 u32 bi = range_bi;
338 while (~0 != bi)
339 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100340 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200341 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
342 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
343 {
344 bi = b->next_buffer;
345 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
346 }
347 else
348 {
349 bi = ~0;
350 }
351 }
352 range_bi = range_vnb->ip.reass.next_range_bi;
353 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200354 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000355 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200356 {
357 u32 n_left_to_next, *to_next, next_index;
358
359 next_index = reass->error_next_index;
360 u32 bi = ~0;
361
362 while (vec_len (to_free) > 0)
363 {
364 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
365
366 while (vec_len (to_free) > 0 && n_left_to_next > 0)
367 {
368 bi = vec_pop (to_free);
369
370 if (~0 != bi)
371 {
372 to_next[0] = bi;
373 to_next += 1;
374 n_left_to_next -= 1;
375 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
376 to_next, n_left_to_next,
377 bi, next_index);
378 }
379 }
380 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
381 }
382 }
383 else
384 {
385 vlib_buffer_free (vm, to_free, vec_len (to_free));
386 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100387 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200388}
389
Klement Sekera4c533132018-02-22 11:41:12 +0100390always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200391ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100392 ip6_reass_main_t * rm, ip6_reass_t * reass,
Klement Sekerae8498652019-06-17 12:23:15 +0000393 u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200394{
395 if (~0 == reass->first_bi)
396 {
397 return;
398 }
Klement Sekerae8498652019-06-17 12:23:15 +0000399 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200400 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200401 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
402 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200403 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200404 *icmp_bi = reass->first_bi;
405 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
406 {
407 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
408 ICMP_ERROR_RT_EXCEEDED, 0);
409 }
410 // fragment with offset zero received - send icmp message back
411 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
412 {
413 // separate first buffer from chain and steer it towards icmp node
414 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
415 reass->first_bi = b->next_buffer;
416 }
417 else
418 {
419 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
420 }
421 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
422 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
423 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200424 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200425 }
Klement Sekerae8498652019-06-17 12:23:15 +0000426 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200427}
428
Klement Sekera4c533132018-02-22 11:41:12 +0100429always_inline ip6_reass_t *
430ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
431 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekerae8498652019-06-17 12:23:15 +0000432 ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200433{
434 ip6_reass_t *reass = NULL;
435 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera75e7d132017-09-20 08:26:30 +0200436
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800437 if (!clib_bihash_search_48_8
438 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200439 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800440 if (vm->thread_index != kv->v.thread_index)
441 {
442 *do_handoff = 1;
443 return NULL;
444 }
445 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
446
Klement Sekera75e7d132017-09-20 08:26:30 +0200447 if (now > reass->last_heard + rm->timeout)
448 {
Klement Sekerae8498652019-06-17 12:23:15 +0000449 ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +0100450 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200451 reass = NULL;
452 }
453 }
454
455 if (reass)
456 {
457 reass->last_heard = now;
458 return reass;
459 }
460
Klement Sekera4c533132018-02-22 11:41:12 +0100461 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200462 {
463 reass = NULL;
464 return reass;
465 }
466 else
467 {
Klement Sekera4c533132018-02-22 11:41:12 +0100468 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400469 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800470 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100471 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200472 reass->first_bi = ~0;
473 reass->last_packet_octet = ~0;
474 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000475 reass->next_index = ~0;
476 reass->error_next_index = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100477 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200478 }
479
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800480 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
481 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
482 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
483 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
484 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
485 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
486 kv->v.reass_index = (reass - rt->pool);
487 kv->v.thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200488 reass->last_heard = now;
489
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800490 if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200491 {
Klement Sekera4c533132018-02-22 11:41:12 +0100492 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200493 reass = NULL;
494 }
495
496 return reass;
497}
498
Klement Sekerad0f70a32018-12-14 17:24:13 +0100499always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200500ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100501 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100502 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000503 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200504{
Klement Sekera75e7d132017-09-20 08:26:30 +0200505 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200506 *error0 = IP6_ERROR_NONE;
507 ip6_frag_hdr_t *frag_hdr;
508 vlib_buffer_t *last_b = NULL;
509 u32 sub_chain_bi = reass->first_bi;
510 u32 total_length = 0;
511 u32 buf_cnt = 0;
512 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100513 u32 *vec_drop_compress = NULL;
514 ip6_reass_rc_t rv = IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200515 do
516 {
517 u32 tmp_bi = sub_chain_bi;
518 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100519 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
520 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
521 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
522 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100523 rv = IP6_REASS_RC_INTERNAL_ERROR;
524 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100525 }
526
Klement Sekera75e7d132017-09-20 08:26:30 +0200527 u32 data_len = ip6_reass_buffer_get_data_len (tmp);
528 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
529 sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
530 u32 trim_end =
531 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
532 if (tmp_bi == reass->first_bi)
533 {
534 /* first buffer - keep ip6 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100535 if (0 != ip6_reass_buffer_get_data_offset (tmp))
536 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100537 rv = IP6_REASS_RC_INTERNAL_ERROR;
538 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100539 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200540 trim_front = 0;
541 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
542 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
543 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100544 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
545 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100546 rv = IP6_REASS_RC_INTERNAL_ERROR;
547 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100548 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200549 }
550 u32 keep_data =
551 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
552 while (1)
553 {
554 ++buf_cnt;
555 if (trim_front)
556 {
557 if (trim_front > tmp->current_length)
558 {
559 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100560 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200561 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100562 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
563 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100564 rv = IP6_REASS_RC_INTERNAL_ERROR;
565 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100566 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200567 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
568 tmp_bi = tmp->next_buffer;
569 tmp = vlib_get_buffer (vm, tmp_bi);
570 continue;
571 }
572 else
573 {
574 vlib_buffer_advance (tmp, trim_front);
575 trim_front = 0;
576 }
577 }
578 if (keep_data)
579 {
580 if (last_b)
581 {
582 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
583 last_b->next_buffer = tmp_bi;
584 }
585 last_b = tmp;
586 if (keep_data <= tmp->current_length)
587 {
588 tmp->current_length = keep_data;
589 keep_data = 0;
590 }
591 else
592 {
593 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100594 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
595 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100596 rv = IP6_REASS_RC_INTERNAL_ERROR;
597 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100598 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200599 }
600 total_length += tmp->current_length;
601 }
602 else
603 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100604 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100605 if (reass->first_bi == tmp_bi)
606 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100607 rv = IP6_REASS_RC_INTERNAL_ERROR;
608 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100609 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200610 ++dropped_cnt;
611 }
612 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
613 {
614 tmp_bi = tmp->next_buffer;
615 tmp = vlib_get_buffer (vm, tmp->next_buffer);
616 }
617 else
618 {
619 break;
620 }
621 }
622 sub_chain_bi =
623 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
624 reass.next_range_bi;
625 }
626 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700627
Klement Sekerad0f70a32018-12-14 17:24:13 +0100628 if (!last_b)
629 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100630 rv = IP6_REASS_RC_INTERNAL_ERROR;
631 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100632 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200633 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
634 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100635 if (total_length < first_b->current_length)
636 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100637 rv = IP6_REASS_RC_INTERNAL_ERROR;
638 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100639 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200640 total_length -= first_b->current_length;
641 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
642 first_b->total_length_not_including_first_buffer = total_length;
643 // drop fragment header
644 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
645 ip6_header_t *ip = vlib_buffer_get_current (first_b);
646 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
647 ip6_ext_header_t *prev_hdr;
648 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
649 IP_PROTOCOL_IPV6_FRAGMENTATION);
650 if (prev_hdr)
651 {
652 prev_hdr->next_hdr = frag_hdr->next_hdr;
653 }
654 else
655 {
656 ip->protocol = frag_hdr->next_hdr;
657 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100658 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
659 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100660 rv = IP6_REASS_RC_INTERNAL_ERROR;
661 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100662 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200663 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
664 first_b->current_length - ip6_frag_hdr_offset -
665 sizeof (ip6_frag_hdr_t));
666 first_b->current_length -= sizeof (*frag_hdr);
667 ip->payload_length =
668 clib_host_to_net_u16 (total_length + first_b->current_length -
669 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100670 if (!vlib_buffer_chain_linearize (vm, first_b))
671 {
672 rv = IP6_REASS_RC_NO_BUF;
673 goto free_buffers_and_return;
674 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700675 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200676 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
677 {
678 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
679#if 0
680 // following code does a hexdump of packet fragments to stdout ...
681 do
682 {
683 u32 bi = reass->first_bi;
684 u8 *s = NULL;
685 while (~0 != bi)
686 {
687 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
688 s = format (s, "%u: %U\n", bi, format_hexdump,
689 vlib_buffer_get_current (b), b->current_length);
690 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
691 {
692 bi = b->next_buffer;
693 }
694 else
695 {
696 break;
697 }
698 }
699 printf ("%.*s\n", vec_len (s), s);
700 fflush (stdout);
701 vec_free (s);
702 }
703 while (0);
704#endif
705 }
Klement Sekerae8498652019-06-17 12:23:15 +0000706 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100707 {
708 *next0 = IP6_REASSEMBLY_NEXT_INPUT;
709 }
710 else
711 {
712 *next0 = reass->next_index;
713 }
714 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
715 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200716 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100717free_buffers_and_return:
718 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
719 vec_free (vec_drop_compress);
720 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200721}
722
Klement Sekera4c533132018-02-22 11:41:12 +0100723always_inline void
724ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm,
725 ip6_reass_per_thread_t * rt,
726 ip6_reass_t * reass, u32 prev_range_bi,
727 u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200728{
729
730 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
731 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
732 if (~0 != prev_range_bi)
733 {
734 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
735 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
736 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
737 prev_vnb->ip.reass.next_range_bi = new_next_bi;
738 }
739 else
740 {
741 if (~0 != reass->first_bi)
742 {
743 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
744 }
745 reass->first_bi = new_next_bi;
746 }
747 reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200748}
749
Klement Sekerad0f70a32018-12-14 17:24:13 +0100750always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200751ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100752 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100753 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000754 ip6_frag_hdr_t * frag_hdr, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200755{
756 int consumed = 0;
757 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
758 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000759 if (is_custom_app)
760 {
761 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
762 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
763 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200764
Klement Sekera75e7d132017-09-20 08:26:30 +0200765 fvnb->ip.reass.ip6_frag_hdr_offset =
766 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
767 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100768 if (fb->current_length < sizeof (*fip) ||
769 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
770 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
771 {
772 return IP6_REASS_RC_INTERNAL_ERROR;
773 }
774
Klement Sekera75e7d132017-09-20 08:26:30 +0200775 u32 fragment_first = fvnb->ip.reass.fragment_first =
776 ip6_frag_hdr_offset_bytes (frag_hdr);
777 u32 fragment_length =
778 vlib_buffer_length_in_chain (vm, fb) -
779 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
780 u32 fragment_last = fvnb->ip.reass.fragment_last =
781 fragment_first + fragment_length - 1;
782 int more_fragments = ip6_frag_hdr_more (frag_hdr);
783 u32 candidate_range_bi = reass->first_bi;
784 u32 prev_range_bi = ~0;
785 fvnb->ip.reass.range_first = fragment_first;
786 fvnb->ip.reass.range_last = fragment_last;
787 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200788 if (!more_fragments)
789 {
790 reass->last_packet_octet = fragment_last;
791 }
792 if (~0 == reass->first_bi)
793 {
794 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100795 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
796 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100797 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100798 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200799 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100800 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200801 }
Klement Sekera4c533132018-02-22 11:41:12 +0100802 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200803 clib_min (clib_net_to_host_u16 (fip->payload_length),
804 fvnb->ip.reass.estimated_mtu);
805 while (~0 != candidate_range_bi)
806 {
807 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
808 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
809 if (fragment_first > candidate_vnb->ip.reass.range_last)
810 {
811 // this fragments starts after candidate range
812 prev_range_bi = candidate_range_bi;
813 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
814 if (candidate_vnb->ip.reass.range_last < fragment_last &&
815 ~0 == candidate_range_bi)
816 {
817 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100818 ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
819 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200820 consumed = 1;
821 break;
822 }
823 continue;
824 }
825 if (fragment_last < candidate_vnb->ip.reass.range_first)
826 {
827 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100828 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200829 *bi0);
830 consumed = 1;
831 }
832 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
833 fragment_last == candidate_vnb->ip.reass.range_last)
834 {
835 // duplicate fragment - ignore
836 }
837 else
838 {
839 // overlapping fragment - not allowed by RFC 8200
Klement Sekerae8498652019-06-17 12:23:15 +0000840 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100841 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200842 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
843 {
844 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
845 0);
846 }
Klement Sekera4c533132018-02-22 11:41:12 +0100847 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200848 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekerabe30fea2019-02-19 11:28:47 +0100849 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200850 }
851 break;
852 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200853 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100854check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200855 if (consumed)
856 {
857 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
858 {
859 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
860 }
861 }
862 if (~0 != reass->last_packet_octet &&
863 reass->data_len == reass->last_packet_octet + 1)
864 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100865 return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000866 is_custom_app);
Klement Sekera75e7d132017-09-20 08:26:30 +0200867 }
868 else
869 {
870 if (consumed)
871 {
872 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200873 if (reass->fragments_n > rm->max_reass_len)
874 {
875 return IP6_REASS_RC_TOO_MANY_FRAGMENTS;
876 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200877 }
878 else
879 {
Klement Sekera4c533132018-02-22 11:41:12 +0100880 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200881 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
882 }
883 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100884 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200885}
886
Klement Sekera4c533132018-02-22 11:41:12 +0100887always_inline bool
888ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
889 vlib_buffer_t * b,
890 ip6_frag_hdr_t * frag_hdr)
891{
892 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
893 while (ip6_ext_hdr (tmp->next_hdr))
894 {
895 tmp = ip6_ext_next_header (tmp);
896 }
897 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
898 {
899 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
900 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
901 0);
902 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
903
904 return false;
905 }
906 return true;
907}
908
909always_inline bool
910ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
911 vlib_node_runtime_t * node,
912 vlib_buffer_t * b,
913 ip6_frag_hdr_t * frag_hdr)
914{
915 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
916 ip6_header_t *ip = vlib_buffer_get_current (b);
917 int more_fragments = ip6_frag_hdr_more (frag_hdr);
918 u32 fragment_length =
919 vlib_buffer_length_in_chain (vm, b) -
920 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
921 if (more_fragments && 0 != fragment_length % 8)
922 {
923 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
924 ICMP6_parameter_problem_erroneous_header_field,
925 (u8 *) & ip->payload_length - (u8 *) ip);
926 return false;
927 }
928 return true;
929}
930
931always_inline bool
932ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
933 vlib_node_runtime_t * node,
934 vlib_buffer_t * b,
935 ip6_frag_hdr_t * frag_hdr)
936{
937 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
938 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
939 u32 fragment_length =
940 vlib_buffer_length_in_chain (vm, b) -
941 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
942 if (fragment_first + fragment_length > 65535)
943 {
944 ip6_header_t *ip0 = vlib_buffer_get_current (b);
945 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
946 ICMP6_parameter_problem_erroneous_header_field,
947 (u8 *) & frag_hdr->fragment_offset_and_more
948 - (u8 *) ip0);
949 return false;
950 }
951 return true;
952}
953
Klement Sekera75e7d132017-09-20 08:26:30 +0200954always_inline uword
Klement Sekerae8498652019-06-17 12:23:15 +0000955ip6_reassembly_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
956 vlib_frame_t * frame, bool is_feature,
957 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200958{
959 u32 *from = vlib_frame_vector_args (frame);
960 u32 n_left_from, n_left_to_next, *to_next, next_index;
961 ip6_reass_main_t *rm = &ip6_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800962 ip6_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +0100963 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200964
965 n_left_from = frame->n_vectors;
966 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100967 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200968 {
969 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
970
Klement Sekera75e7d132017-09-20 08:26:30 +0200971 while (n_left_from > 0 && n_left_to_next > 0)
972 {
973 u32 bi0;
974 vlib_buffer_t *b0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100975 u32 next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +0100976 u32 error0 = IP6_ERROR_NONE;
977 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200978
979 bi0 = from[0];
980 b0 = vlib_get_buffer (vm, bi0);
981
982 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +0200983 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200984 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +0200985 if (ip6_ext_hdr (ip0->protocol))
986 {
987 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
988 IP_PROTOCOL_IPV6_FRAGMENTATION);
989 }
Klement Sekera4c533132018-02-22 11:41:12 +0100990 if (!frag_hdr)
991 {
992 // this is a regular packet - no fragmentation
993 next0 = IP6_REASSEMBLY_NEXT_INPUT;
994 goto skip_reass;
995 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200996 if (0 == ip6_frag_hdr_offset (frag_hdr))
997 {
998 // first fragment - verify upper-layer is present
Klement Sekera4c533132018-02-22 11:41:12 +0100999 if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +02001000 {
Klement Sekera4c533132018-02-22 11:41:12 +01001001 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1002 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001003 }
Klement Sekera4c533132018-02-22 11:41:12 +01001004 }
1005 if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
1006 || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
1007 frag_hdr))
1008 {
1009 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1010 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001011 }
1012 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1013 (u8 *) frag_hdr - (u8 *) ip0;
1014
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001015 ip6_reass_kv_t kv;
1016 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001017
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001018 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1019 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1020 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1021 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1022 kv.k.as_u64[4] =
1023 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1024 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
1025 (u64) frag_hdr->identification;
1026 kv.k.as_u64[5] = ip0->protocol;
1027
1028 ip6_reass_t *reass =
1029 ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
Klement Sekerae8498652019-06-17 12:23:15 +00001030 &do_handoff);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001031
1032 if (PREDICT_FALSE (do_handoff))
1033 {
1034 next0 = IP6_REASSEMBLY_NEXT_HANDOFF;
1035 if (is_feature)
1036 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
1037 kv.v.thread_index;
1038 else
1039 vnet_buffer (b0)->ip.reass.owner_thread_index =
1040 kv.v.thread_index;
1041 }
1042 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001043 {
Klement Sekerad0f70a32018-12-14 17:24:13 +01001044 switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekerae8498652019-06-17 12:23:15 +00001045 &error0, frag_hdr, is_custom_app))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001046 {
1047 case IP6_REASS_RC_OK:
1048 /* nothing to do here */
1049 break;
Klement Sekera3a343d42019-05-16 14:35:46 +02001050 case IP6_REASS_RC_TOO_MANY_FRAGMENTS:
1051 vlib_node_increment_counter (vm, node->node_index,
1052 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1053 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001054 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001055 ip6_reass_free (rm, rt, reass);
1056 goto next_packet;
1057 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001058 case IP6_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001059 vlib_node_increment_counter (vm, node->node_index,
1060 IP6_ERROR_REASS_NO_BUF, 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001061 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001062 ip6_reass_free (rm, rt, reass);
1063 goto next_packet;
1064 break;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001065 case IP6_REASS_RC_INTERNAL_ERROR:
Klement Sekera21aa8f12019-05-20 12:27:33 +02001066 /* drop everything and start with a clean slate */
Klement Sekera3a343d42019-05-16 14:35:46 +02001067 vlib_node_increment_counter (vm, node->node_index,
1068 IP6_ERROR_REASS_INTERNAL_ERROR,
1069 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001070 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001071 ip6_reass_free (rm, rt, reass);
1072 goto next_packet;
1073 break;
1074 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001075 }
1076 else
1077 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001078 if (is_feature)
1079 {
1080 next0 = IP6_REASSEMBLY_NEXT_DROP;
1081 }
1082 else
1083 {
1084 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1085 next0 = fvnb->ip.reass.error_next_index;
1086 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001087 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1088 }
1089
1090 b0->error = node->errors[error0];
1091
1092 if (~0 != bi0)
1093 {
Klement Sekera4c533132018-02-22 11:41:12 +01001094 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001095 to_next[0] = bi0;
1096 to_next += 1;
1097 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001098 if (is_feature && IP6_ERROR_NONE == error0)
1099 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001100 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001101 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001102 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001103 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1104 n_left_to_next, bi0, next0);
1105 }
1106
1107 if (~0 != icmp_bi)
1108 {
1109 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1110 to_next[0] = icmp_bi;
1111 to_next += 1;
1112 n_left_to_next -= 1;
1113 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1114 n_left_to_next, icmp_bi,
1115 next0);
1116 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001117 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001118 from += 1;
1119 n_left_from -= 1;
1120 }
1121
1122 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1123 }
1124
Klement Sekera4c533132018-02-22 11:41:12 +01001125 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001126 return frame->n_vectors;
1127}
1128
1129static char *ip6_reassembly_error_strings[] = {
1130#define _(sym, string) string,
1131 foreach_ip6_error
1132#undef _
1133};
1134
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001135VLIB_NODE_FN (ip6_reass_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1136 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001137{
Klement Sekerae8498652019-06-17 12:23:15 +00001138 return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1139 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001140}
1141
Klement Sekera75e7d132017-09-20 08:26:30 +02001142/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001143VLIB_REGISTER_NODE (ip6_reass_node) = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001144 .name = "ip6-reassembly",
1145 .vector_size = sizeof (u32),
1146 .format_trace = format_ip6_reass_trace,
1147 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1148 .error_strings = ip6_reassembly_error_strings,
1149 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1150 .next_nodes =
1151 {
1152 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1153 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1154 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001155 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001156 },
1157};
1158/* *INDENT-ON* */
1159
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001160VLIB_NODE_FN (ip6_reass_node_feature) (vlib_main_t * vm,
1161 vlib_node_runtime_t * node,
1162 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001163{
Klement Sekerae8498652019-06-17 12:23:15 +00001164 return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1165 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001166}
1167
1168/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001169VLIB_REGISTER_NODE (ip6_reass_node_feature) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001170 .name = "ip6-reassembly-feature",
1171 .vector_size = sizeof (u32),
1172 .format_trace = format_ip6_reass_trace,
1173 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1174 .error_strings = ip6_reassembly_error_strings,
1175 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1176 .next_nodes =
1177 {
1178 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1179 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1180 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001181 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001182 },
1183};
1184/* *INDENT-ON* */
1185
Klement Sekera4c533132018-02-22 11:41:12 +01001186/* *INDENT-OFF* */
1187VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1188 .arc_name = "ip6-unicast",
1189 .node_name = "ip6-reassembly-feature",
1190 .runs_before = VNET_FEATURES ("ip6-lookup"),
1191 .runs_after = 0,
1192};
1193/* *INDENT-ON* */
1194
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001195#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001196static u32
1197ip6_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001198{
1199 ip6_reass_main_t *rm = &ip6_reass_main;
1200 u32 nbuckets;
1201 u8 i;
1202
1203 nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1204
1205 for (i = 0; i < 31; i++)
1206 if ((1 << i) >= nbuckets)
1207 break;
1208 nbuckets = 1 << i;
1209
1210 return nbuckets;
1211}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001212#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001213
1214typedef enum
1215{
1216 IP6_EVENT_CONFIG_CHANGED = 1,
1217} ip6_reass_event_t;
1218
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001219#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001220typedef struct
1221{
1222 int failure;
1223 clib_bihash_48_8_t *new_hash;
1224} ip6_rehash_cb_ctx;
1225
Klement Sekera4c533132018-02-22 11:41:12 +01001226static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001227ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1228{
1229 ip6_rehash_cb_ctx *ctx = _ctx;
1230 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1231 {
1232 ctx->failure = 1;
1233 }
1234}
1235
Klement Sekera4c533132018-02-22 11:41:12 +01001236static void
1237ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001238 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001239{
1240 ip6_reass_main.timeout_ms = timeout_ms;
1241 ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1242 ip6_reass_main.max_reass_n = max_reassemblies;
Klement Sekera3a343d42019-05-16 14:35:46 +02001243 ip6_reass_main.max_reass_len = max_reassembly_length;
Klement Sekera4c533132018-02-22 11:41:12 +01001244 ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1245}
1246
Klement Sekera75e7d132017-09-20 08:26:30 +02001247vnet_api_error_t
1248ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001249 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001250{
1251 u32 old_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3a343d42019-05-16 14:35:46 +02001252 ip6_reass_set_params (timeout_ms, max_reassemblies, max_reassembly_length,
Klement Sekera4c533132018-02-22 11:41:12 +01001253 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001254 vlib_process_signal_event (ip6_reass_main.vlib_main,
1255 ip6_reass_main.ip6_reass_expire_node_idx,
1256 IP6_EVENT_CONFIG_CHANGED, 0);
1257 u32 new_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001258 if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001259 {
1260 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001261 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001262 ip6_rehash_cb_ctx ctx;
1263 ctx.failure = 0;
1264 ctx.new_hash = &new_hash;
1265 clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1266 new_nbuckets * 1024);
1267 clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1268 ip6_rehash_cb, &ctx);
1269 if (ctx.failure)
1270 {
1271 clib_bihash_free_48_8 (&new_hash);
1272 return -1;
1273 }
1274 else
1275 {
1276 clib_bihash_free_48_8 (&ip6_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001277 clib_memcpy_fast (&ip6_reass_main.hash, &new_hash,
1278 sizeof (ip6_reass_main.hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001279 }
1280 }
1281 return 0;
1282}
1283
1284vnet_api_error_t
1285ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1286 u32 * expire_walk_interval_ms)
1287{
1288 *timeout_ms = ip6_reass_main.timeout_ms;
1289 *max_reassemblies = ip6_reass_main.max_reass_n;
1290 *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1291 return 0;
1292}
1293
Klement Sekera4c533132018-02-22 11:41:12 +01001294static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001295ip6_reass_init_function (vlib_main_t * vm)
1296{
1297 ip6_reass_main_t *rm = &ip6_reass_main;
1298 clib_error_t *error = 0;
1299 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001300 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001301
1302 rm->vlib_main = vm;
1303 rm->vnet_main = vnet_get_main ();
1304
Juraj Slobodacd806922018-10-10 10:15:54 +02001305 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001306 ip6_reass_per_thread_t *rt;
1307 vec_foreach (rt, rm->per_thread_data)
1308 {
1309 clib_spinlock_init (&rt->lock);
1310 pool_alloc (rt->pool, rm->max_reass_n);
1311 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001312
1313 node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1314 ASSERT (node);
1315 rm->ip6_reass_expire_node_idx = node->index;
1316
Klement Sekera3ecc2212018-03-27 10:34:43 +02001317 ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS,
1318 IP6_REASS_MAX_REASSEMBLIES_DEFAULT,
Klement Sekera3a343d42019-05-16 14:35:46 +02001319 IP6_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
Klement Sekera3ecc2212018-03-27 10:34:43 +02001320 IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1321
Klement Sekera75e7d132017-09-20 08:26:30 +02001322 nbuckets = ip6_reass_get_nbuckets ();
1323 clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1324
Dave Barach1403fcd2018-02-05 09:45:43 -05001325 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001326 ASSERT (node);
1327 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001328 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1329 ASSERT (node);
1330 rm->ip6_icmp_error_idx = node->index;
1331
1332 if ((error = vlib_call_init_function (vm, ip_main_init)))
1333 return error;
1334 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1335 ip6_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001336
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001337 rm->fq_index = vlib_frame_queue_main_init (ip6_reass_node.index, 0);
1338 rm->fq_feature_index =
1339 vlib_frame_queue_main_init (ip6_reass_node_feature.index, 0);
1340
Klement Sekera75e7d132017-09-20 08:26:30 +02001341 return error;
1342}
1343
1344VLIB_INIT_FUNCTION (ip6_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001345#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001346
1347static uword
1348ip6_reass_walk_expired (vlib_main_t * vm,
1349 vlib_node_runtime_t * node, vlib_frame_t * f)
1350{
1351 ip6_reass_main_t *rm = &ip6_reass_main;
1352 uword event_type, *event_data = 0;
1353
1354 while (true)
1355 {
1356 vlib_process_wait_for_event_or_clock (vm,
1357 (f64) rm->expire_walk_interval_ms
1358 / (f64) MSEC_PER_SEC);
1359 event_type = vlib_process_get_events (vm, &event_data);
1360
1361 switch (event_type)
1362 {
1363 case ~0: /* no events => timeout */
1364 /* nothing to do here */
1365 break;
1366 case IP6_EVENT_CONFIG_CHANGED:
1367 break;
1368 default:
1369 clib_warning ("BUG: event type 0x%wx", event_type);
1370 break;
1371 }
1372 f64 now = vlib_time_now (vm);
1373
1374 ip6_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001375 int *pool_indexes_to_free = NULL;
1376
Klement Sekera4c533132018-02-22 11:41:12 +01001377 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001378 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001379 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001380 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001381 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1382 {
1383 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1384 clib_spinlock_lock (&rt->lock);
1385
1386 vec_reset_length (pool_indexes_to_free);
1387 /* *INDENT-OFF* */
1388 pool_foreach_index (index, rt->pool, ({
1389 reass = pool_elt_at_index (rt->pool, index);
1390 if (now > reass->last_heard + rm->timeout)
1391 {
1392 vec_add1 (pool_indexes_to_free, index);
1393 }
1394 }));
1395 /* *INDENT-ON* */
1396 int *i;
1397 /* *INDENT-OFF* */
1398 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001399 {
Klement Sekera4c533132018-02-22 11:41:12 +01001400 ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1401 u32 icmp_bi = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +01001402 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
1403 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1404 {
1405 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1406 b->trace_index))
1407 {
1408 /* the trace is gone, don't trace this buffer anymore */
1409 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1410 }
1411 }
Klement Sekerae8498652019-06-17 12:23:15 +00001412 ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001413 if (~0 != icmp_bi)
1414 {
1415 vec_add1 (vec_icmp_bi, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001416 }
1417 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001418 }
Klement Sekera4c533132018-02-22 11:41:12 +01001419 /* *INDENT-ON* */
1420
1421 clib_spinlock_unlock (&rt->lock);
1422 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001423
Klement Sekera75e7d132017-09-20 08:26:30 +02001424 while (vec_len (vec_icmp_bi) > 0)
1425 {
1426 vlib_frame_t *f =
1427 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1428 u32 *to_next = vlib_frame_vector_args (f);
1429 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001430 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001431 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1432 {
1433 u32 bi = vec_pop (vec_icmp_bi);
1434 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1435 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1436 {
1437 if (pool_is_free_index (vm->trace_main.trace_buffer_pool,
1438 b->trace_index))
1439 {
1440 /* the trace is gone, don't trace this buffer anymore */
1441 b->flags &= ~VLIB_BUFFER_IS_TRACED;
1442 }
1443 else
1444 {
Klement Sekera4c533132018-02-22 11:41:12 +01001445 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001446 }
1447 }
1448 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1449 to_next[0] = bi;
1450 ++f->n_vectors;
1451 to_next += 1;
1452 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001453 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001454 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001455 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1456 }
1457
1458 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001459 vec_free (vec_icmp_bi);
1460 if (event_data)
1461 {
1462 _vec_len (event_data) = 0;
1463 }
1464 }
1465
1466 return 0;
1467}
1468
Klement Sekera75e7d132017-09-20 08:26:30 +02001469/* *INDENT-OFF* */
1470VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = {
1471 .function = ip6_reass_walk_expired,
1472 .format_trace = format_ip6_reass_trace,
1473 .type = VLIB_NODE_TYPE_PROCESS,
1474 .name = "ip6-reassembly-expire-walk",
1475
1476 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1477 .error_strings = ip6_reassembly_error_strings,
1478
1479};
1480/* *INDENT-ON* */
1481
1482static u8 *
1483format_ip6_reass_key (u8 * s, va_list * args)
1484{
1485 ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1486 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1487 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1488 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1489 return s;
1490}
1491
1492static u8 *
1493format_ip6_reass (u8 * s, va_list * args)
1494{
1495 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1496 ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1497
Klement Sekera4c533132018-02-22 11:41:12 +01001498 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001499 "last_packet_octet: %u, trace_op_counter: %u\n",
1500 reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1501 reass->data_len, reass->last_packet_octet,
1502 reass->trace_op_counter);
1503 u32 bi = reass->first_bi;
1504 u32 counter = 0;
1505 while (~0 != bi)
1506 {
1507 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1508 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1509 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1510 "fragment[%u, %u]\n",
1511 counter, vnb->ip.reass.range_first,
1512 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001513 ip6_reass_buffer_get_data_offset (b),
1514 ip6_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001515 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1516 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1517 {
1518 bi = b->next_buffer;
1519 }
1520 else
1521 {
1522 bi = ~0;
1523 }
1524 }
1525 return s;
1526}
1527
1528static clib_error_t *
1529show_ip6_reass (vlib_main_t * vm, unformat_input_t * input,
1530 CLIB_UNUSED (vlib_cli_command_t * lmd))
1531{
1532 ip6_reass_main_t *rm = &ip6_reass_main;
1533
1534 vlib_cli_output (vm, "---------------------");
1535 vlib_cli_output (vm, "IP6 reassembly status");
1536 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001537 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001538 if (unformat (input, "details"))
1539 {
Klement Sekera4c533132018-02-22 11:41:12 +01001540 details = true;
1541 }
1542
1543 u32 sum_reass_n = 0;
1544 u64 sum_buffers_n = 0;
1545 ip6_reass_t *reass;
1546 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001547 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001548 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1549 {
1550 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1551 clib_spinlock_lock (&rt->lock);
1552 if (details)
1553 {
1554 /* *INDENT-OFF* */
1555 pool_foreach (reass, rt->pool, {
1556 vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1557 });
1558 /* *INDENT-ON* */
1559 }
1560 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001561 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001562 }
1563 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001564 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1565 (long unsigned) sum_reass_n);
1566 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1567 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001568 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001569 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001570 return 0;
1571}
1572
1573/* *INDENT-OFF* */
1574VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = {
1575 .path = "show ip6-reassembly",
1576 .short_help = "show ip6-reassembly [details]",
1577 .function = show_ip6_reass,
1578};
1579/* *INDENT-ON* */
1580
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001581#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001582vnet_api_error_t
1583ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1584{
1585 return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1586 sw_if_index, enable_disable, 0, 0);
1587}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001588#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001589
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001590#define foreach_ip6_reassembly_handoff_error \
1591_(CONGESTION_DROP, "congestion drop")
1592
1593
1594typedef enum
1595{
1596#define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym,
1597 foreach_ip6_reassembly_handoff_error
1598#undef _
1599 IP6_REASSEMBLY_HANDOFF_N_ERROR,
1600} ip6_reassembly_handoff_error_t;
1601
1602static char *ip6_reassembly_handoff_error_strings[] = {
1603#define _(sym,string) string,
1604 foreach_ip6_reassembly_handoff_error
1605#undef _
1606};
1607
1608typedef struct
1609{
1610 u32 next_worker_index;
1611} ip6_reassembly_handoff_trace_t;
1612
1613static u8 *
1614format_ip6_reassembly_handoff_trace (u8 * s, va_list * args)
1615{
1616 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1617 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1618 ip6_reassembly_handoff_trace_t *t =
1619 va_arg (*args, ip6_reassembly_handoff_trace_t *);
1620
1621 s =
1622 format (s, "ip6-reassembly-handoff: next-worker %d",
1623 t->next_worker_index);
1624
1625 return s;
1626}
1627
1628always_inline uword
1629ip6_reassembly_handoff_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1630 vlib_frame_t * frame, bool is_feature)
1631{
1632 ip6_reass_main_t *rm = &ip6_reass_main;
1633
1634 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1635 u32 n_enq, n_left_from, *from;
1636 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1637 u32 fq_index;
1638
1639 from = vlib_frame_vector_args (frame);
1640 n_left_from = frame->n_vectors;
1641 vlib_get_buffers (vm, from, bufs, n_left_from);
1642
1643 b = bufs;
1644 ti = thread_indices;
1645
1646 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1647
1648 while (n_left_from > 0)
1649 {
1650 ti[0] =
1651 (is_feature) ? vnet_buffer (b[0])->ip.
1652 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1653 reass.owner_thread_index;
1654
1655 if (PREDICT_FALSE
1656 ((node->flags & VLIB_NODE_FLAG_TRACE)
1657 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1658 {
1659 ip6_reassembly_handoff_trace_t *t =
1660 vlib_add_trace (vm, node, b[0], sizeof (*t));
1661 t->next_worker_index = ti[0];
1662 }
1663
1664 n_left_from -= 1;
1665 ti += 1;
1666 b += 1;
1667 }
1668 n_enq =
1669 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1670 frame->n_vectors, 1);
1671
1672 if (n_enq < frame->n_vectors)
1673 vlib_node_increment_counter (vm, node->node_index,
1674 IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1675 frame->n_vectors - n_enq);
1676 return frame->n_vectors;
1677}
1678
1679VLIB_NODE_FN (ip6_reassembly_handoff_node) (vlib_main_t * vm,
1680 vlib_node_runtime_t * node,
1681 vlib_frame_t * frame)
1682{
1683 return ip6_reassembly_handoff_inline (vm, node, frame,
1684 false /* is_feature */ );
1685}
1686
1687/* *INDENT-OFF* */
1688VLIB_REGISTER_NODE (ip6_reassembly_handoff_node) = {
1689 .name = "ip6-reassembly-handoff",
1690 .vector_size = sizeof (u32),
1691 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1692 .error_strings = ip6_reassembly_handoff_error_strings,
1693 .format_trace = format_ip6_reassembly_handoff_trace,
1694
1695 .n_next_nodes = 1,
1696
1697 .next_nodes = {
1698 [0] = "error-drop",
1699 },
1700};
1701
1702
1703VLIB_NODE_FN (ip6_reassembly_feature_handoff_node) (vlib_main_t * vm,
1704 vlib_node_runtime_t * node, vlib_frame_t * frame)
1705{
1706 return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1707}
1708
1709
1710/* *INDENT-OFF* */
1711VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node) = {
1712 .name = "ip6-reass-feature-hoff",
1713 .vector_size = sizeof (u32),
1714 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1715 .error_strings = ip6_reassembly_handoff_error_strings,
1716 .format_trace = format_ip6_reassembly_handoff_trace,
1717
1718 .n_next_nodes = 1,
1719
1720 .next_nodes = {
1721 [0] = "error-drop",
1722 },
1723};
1724/* *INDENT-ON* */
1725
Klement Sekera75e7d132017-09-20 08:26:30 +02001726/*
1727 * fd.io coding-style-patch-verification: ON
1728 *
1729 * Local Variables:
1730 * eval: (c-set-style "gnu")
1731 * End:
1732 */