blob: 7b0d84de289477ed00874a62739a82e8f09a898a [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);
283 ip6_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
Klement Sekera75e7d132017-09-20 08:26:30 +0200284 t->reass_id = reass->id;
285 t->action = action;
286 ip6_reass_trace_details (vm, bi, &t->trace_range);
287 t->size_diff = size_diff;
288 t->op_id = reass->trace_op_counter;
289 ++reass->trace_op_counter;
290 t->fragment_first = vnb->ip.reass.fragment_first;
291 t->fragment_last = vnb->ip.reass.fragment_last;
292 t->trace_range.first_bi = reass->first_bi;
293 t->total_data_len = reass->data_len;
294#if 0
295 static u8 *s = NULL;
296 s = format (s, "%U", format_ip6_reass_trace, NULL, NULL, t);
297 printf ("%.*s\n", vec_len (s), s);
298 fflush (stdout);
299 vec_reset_length (s);
300#endif
301}
302
Klement Sekera4c533132018-02-22 11:41:12 +0100303always_inline void
304ip6_reass_free (ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
305 ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200306{
307 clib_bihash_kv_48_8_t kv;
308 kv.key[0] = reass->key.as_u64[0];
309 kv.key[1] = reass->key.as_u64[1];
310 kv.key[2] = reass->key.as_u64[2];
311 kv.key[3] = reass->key.as_u64[3];
312 kv.key[4] = reass->key.as_u64[4];
313 kv.key[5] = reass->key.as_u64[5];
314 clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
Klement Sekera4c533132018-02-22 11:41:12 +0100315 pool_put (rt->pool, reass);
316 --rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200317}
318
Klement Sekera4c533132018-02-22 11:41:12 +0100319always_inline void
Klement Sekera21aa8f12019-05-20 12:27:33 +0200320ip6_reass_drop_all (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekerae8498652019-06-17 12:23:15 +0000321 ip6_reass_main_t * rm, ip6_reass_t * reass)
Klement Sekera75e7d132017-09-20 08:26:30 +0200322{
323 u32 range_bi = reass->first_bi;
324 vlib_buffer_t *range_b;
325 vnet_buffer_opaque_t *range_vnb;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100326 u32 *to_free = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200327 while (~0 != range_bi)
328 {
329 range_b = vlib_get_buffer (vm, range_bi);
330 range_vnb = vnet_buffer (range_b);
331 u32 bi = range_bi;
332 while (~0 != bi)
333 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100334 vec_add1 (to_free, bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200335 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
336 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
337 {
338 bi = b->next_buffer;
339 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
340 }
341 else
342 {
343 bi = ~0;
344 }
345 }
346 range_bi = range_vnb->ip.reass.next_range_bi;
347 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200348 /* send to next_error_index */
Klement Sekerae8498652019-06-17 12:23:15 +0000349 if (~0 != reass->error_next_index)
Klement Sekera21aa8f12019-05-20 12:27:33 +0200350 {
351 u32 n_left_to_next, *to_next, next_index;
352
353 next_index = reass->error_next_index;
354 u32 bi = ~0;
355
356 while (vec_len (to_free) > 0)
357 {
358 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
359
360 while (vec_len (to_free) > 0 && n_left_to_next > 0)
361 {
362 bi = vec_pop (to_free);
363
364 if (~0 != bi)
365 {
366 to_next[0] = bi;
367 to_next += 1;
368 n_left_to_next -= 1;
369 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
370 to_next, n_left_to_next,
371 bi, next_index);
372 }
373 }
374 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
375 }
376 }
377 else
378 {
379 vlib_buffer_free (vm, to_free, vec_len (to_free));
380 }
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100381 vec_free (to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +0200382}
383
Klement Sekera4c533132018-02-22 11:41:12 +0100384always_inline void
Klement Sekera75e7d132017-09-20 08:26:30 +0200385ip6_reass_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100386 ip6_reass_main_t * rm, ip6_reass_t * reass,
Klement Sekerae8498652019-06-17 12:23:15 +0000387 u32 * icmp_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200388{
389 if (~0 == reass->first_bi)
390 {
391 return;
392 }
Klement Sekerae8498652019-06-17 12:23:15 +0000393 if (~0 == reass->next_index) // custom apps don't want icmp
Klement Sekera75e7d132017-09-20 08:26:30 +0200394 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200395 vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
396 if (0 == vnet_buffer (b)->ip.reass.fragment_first)
Klement Sekera75e7d132017-09-20 08:26:30 +0200397 {
Klement Sekera21aa8f12019-05-20 12:27:33 +0200398 *icmp_bi = reass->first_bi;
399 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
400 {
401 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi,
402 ICMP_ERROR_RT_EXCEEDED, 0);
403 }
404 // fragment with offset zero received - send icmp message back
405 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
406 {
407 // separate first buffer from chain and steer it towards icmp node
408 b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
409 reass->first_bi = b->next_buffer;
410 }
411 else
412 {
413 reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
414 }
415 icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
416 ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
417 0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200418 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200419 }
Klement Sekerae8498652019-06-17 12:23:15 +0000420 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200421}
422
Klement Sekera4c533132018-02-22 11:41:12 +0100423always_inline ip6_reass_t *
424ip6_reass_find_or_create (vlib_main_t * vm, vlib_node_runtime_t * node,
425 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekerae8498652019-06-17 12:23:15 +0000426 ip6_reass_kv_t * kv, u32 * icmp_bi, u8 * do_handoff)
Klement Sekera75e7d132017-09-20 08:26:30 +0200427{
428 ip6_reass_t *reass = NULL;
429 f64 now = vlib_time_now (rm->vlib_main);
Klement Sekera75e7d132017-09-20 08:26:30 +0200430
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800431 if (!clib_bihash_search_48_8
432 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, (clib_bihash_kv_48_8_t *) kv))
Klement Sekera75e7d132017-09-20 08:26:30 +0200433 {
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800434 if (vm->thread_index != kv->v.thread_index)
435 {
436 *do_handoff = 1;
437 return NULL;
438 }
439 reass = pool_elt_at_index (rt->pool, kv->v.reass_index);
440
Klement Sekera75e7d132017-09-20 08:26:30 +0200441 if (now > reass->last_heard + rm->timeout)
442 {
Klement Sekerae8498652019-06-17 12:23:15 +0000443 ip6_reass_on_timeout (vm, node, rm, reass, icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +0100444 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200445 reass = NULL;
446 }
447 }
448
449 if (reass)
450 {
451 reass->last_heard = now;
452 return reass;
453 }
454
Klement Sekera4c533132018-02-22 11:41:12 +0100455 if (rt->reass_n >= rm->max_reass_n)
Klement Sekera75e7d132017-09-20 08:26:30 +0200456 {
457 reass = NULL;
458 return reass;
459 }
460 else
461 {
Klement Sekera4c533132018-02-22 11:41:12 +0100462 pool_get (rt->pool, reass);
Dave Barachb7b92992018-10-17 10:38:51 -0400463 clib_memset (reass, 0, sizeof (*reass));
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800464 reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
Klement Sekera4c533132018-02-22 11:41:12 +0100465 ++rt->id_counter;
Klement Sekera75e7d132017-09-20 08:26:30 +0200466 reass->first_bi = ~0;
467 reass->last_packet_octet = ~0;
468 reass->data_len = 0;
Klement Sekerae8498652019-06-17 12:23:15 +0000469 reass->next_index = ~0;
470 reass->error_next_index = ~0;
Klement Sekera4c533132018-02-22 11:41:12 +0100471 ++rt->reass_n;
Klement Sekera75e7d132017-09-20 08:26:30 +0200472 }
473
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800474 reass->key.as_u64[0] = ((clib_bihash_kv_48_8_t *) kv)->key[0];
475 reass->key.as_u64[1] = ((clib_bihash_kv_48_8_t *) kv)->key[1];
476 reass->key.as_u64[2] = ((clib_bihash_kv_48_8_t *) kv)->key[2];
477 reass->key.as_u64[3] = ((clib_bihash_kv_48_8_t *) kv)->key[3];
478 reass->key.as_u64[4] = ((clib_bihash_kv_48_8_t *) kv)->key[4];
479 reass->key.as_u64[5] = ((clib_bihash_kv_48_8_t *) kv)->key[5];
480 kv->v.reass_index = (reass - rt->pool);
481 kv->v.thread_index = vm->thread_index;
Klement Sekera75e7d132017-09-20 08:26:30 +0200482 reass->last_heard = now;
483
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800484 if (clib_bihash_add_del_48_8 (&rm->hash, (clib_bihash_kv_48_8_t *) kv, 1))
Klement Sekera75e7d132017-09-20 08:26:30 +0200485 {
Klement Sekera4c533132018-02-22 11:41:12 +0100486 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200487 reass = NULL;
488 }
489
490 return reass;
491}
492
Klement Sekerad0f70a32018-12-14 17:24:13 +0100493always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200494ip6_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100495 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100496 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000497 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200498{
Klement Sekera75e7d132017-09-20 08:26:30 +0200499 *bi0 = reass->first_bi;
Klement Sekera75e7d132017-09-20 08:26:30 +0200500 *error0 = IP6_ERROR_NONE;
501 ip6_frag_hdr_t *frag_hdr;
502 vlib_buffer_t *last_b = NULL;
503 u32 sub_chain_bi = reass->first_bi;
504 u32 total_length = 0;
505 u32 buf_cnt = 0;
506 u32 dropped_cnt = 0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100507 u32 *vec_drop_compress = NULL;
508 ip6_reass_rc_t rv = IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200509 do
510 {
511 u32 tmp_bi = sub_chain_bi;
512 vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100513 vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
514 if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
515 !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
516 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100517 rv = IP6_REASS_RC_INTERNAL_ERROR;
518 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100519 }
520
Klement Sekera75e7d132017-09-20 08:26:30 +0200521 u32 data_len = ip6_reass_buffer_get_data_len (tmp);
522 u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
523 sizeof (*frag_hdr) + ip6_reass_buffer_get_data_offset (tmp);
524 u32 trim_end =
525 vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
526 if (tmp_bi == reass->first_bi)
527 {
528 /* first buffer - keep ip6 header */
Klement Sekerad0f70a32018-12-14 17:24:13 +0100529 if (0 != ip6_reass_buffer_get_data_offset (tmp))
530 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100531 rv = IP6_REASS_RC_INTERNAL_ERROR;
532 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100533 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200534 trim_front = 0;
535 trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
536 (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
537 sizeof (*frag_hdr));
Klement Sekerad0f70a32018-12-14 17:24:13 +0100538 if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
539 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100540 rv = IP6_REASS_RC_INTERNAL_ERROR;
541 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100542 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200543 }
544 u32 keep_data =
545 vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
546 while (1)
547 {
548 ++buf_cnt;
549 if (trim_front)
550 {
551 if (trim_front > tmp->current_length)
552 {
553 /* drop whole buffer */
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100554 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekera75e7d132017-09-20 08:26:30 +0200555 trim_front -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100556 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
557 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100558 rv = IP6_REASS_RC_INTERNAL_ERROR;
559 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100560 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200561 tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
562 tmp_bi = tmp->next_buffer;
563 tmp = vlib_get_buffer (vm, tmp_bi);
564 continue;
565 }
566 else
567 {
568 vlib_buffer_advance (tmp, trim_front);
569 trim_front = 0;
570 }
571 }
572 if (keep_data)
573 {
574 if (last_b)
575 {
576 last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
577 last_b->next_buffer = tmp_bi;
578 }
579 last_b = tmp;
580 if (keep_data <= tmp->current_length)
581 {
582 tmp->current_length = keep_data;
583 keep_data = 0;
584 }
585 else
586 {
587 keep_data -= tmp->current_length;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100588 if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
589 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100590 rv = IP6_REASS_RC_INTERNAL_ERROR;
591 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100592 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200593 }
594 total_length += tmp->current_length;
595 }
596 else
597 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100598 vec_add1 (vec_drop_compress, tmp_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100599 if (reass->first_bi == tmp_bi)
600 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100601 rv = IP6_REASS_RC_INTERNAL_ERROR;
602 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100603 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200604 ++dropped_cnt;
605 }
606 if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
607 {
608 tmp_bi = tmp->next_buffer;
609 tmp = vlib_get_buffer (vm, tmp->next_buffer);
610 }
611 else
612 {
613 break;
614 }
615 }
616 sub_chain_bi =
617 vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
618 reass.next_range_bi;
619 }
620 while (~0 != sub_chain_bi);
Chris Luke30684ac2018-03-29 12:56:58 -0700621
Klement Sekerad0f70a32018-12-14 17:24:13 +0100622 if (!last_b)
623 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100624 rv = IP6_REASS_RC_INTERNAL_ERROR;
625 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100626 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200627 last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
628 vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100629 if (total_length < first_b->current_length)
630 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100631 rv = IP6_REASS_RC_INTERNAL_ERROR;
632 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100633 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200634 total_length -= first_b->current_length;
635 first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
636 first_b->total_length_not_including_first_buffer = total_length;
637 // drop fragment header
638 vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
639 ip6_header_t *ip = vlib_buffer_get_current (first_b);
640 u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
641 ip6_ext_header_t *prev_hdr;
642 ip6_ext_header_find_t (ip, prev_hdr, frag_hdr,
643 IP_PROTOCOL_IPV6_FRAGMENTATION);
644 if (prev_hdr)
645 {
646 prev_hdr->next_hdr = frag_hdr->next_hdr;
647 }
648 else
649 {
650 ip->protocol = frag_hdr->next_hdr;
651 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100652 if (!((u8 *) frag_hdr - (u8 *) ip == ip6_frag_hdr_offset))
653 {
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100654 rv = IP6_REASS_RC_INTERNAL_ERROR;
655 goto free_buffers_and_return;
Klement Sekerad0f70a32018-12-14 17:24:13 +0100656 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200657 memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
658 first_b->current_length - ip6_frag_hdr_offset -
659 sizeof (ip6_frag_hdr_t));
660 first_b->current_length -= sizeof (*frag_hdr);
661 ip->payload_length =
662 clib_host_to_net_u16 (total_length + first_b->current_length -
663 sizeof (*ip));
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100664 if (!vlib_buffer_chain_linearize (vm, first_b))
665 {
666 rv = IP6_REASS_RC_NO_BUF;
667 goto free_buffers_and_return;
668 }
Vijayabhaskar Katamreddy90556d62019-05-23 13:02:28 -0700669 first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
Klement Sekera75e7d132017-09-20 08:26:30 +0200670 if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
671 {
672 ip6_reass_add_trace (vm, node, rm, reass, reass->first_bi, FINALIZE, 0);
673#if 0
674 // following code does a hexdump of packet fragments to stdout ...
675 do
676 {
677 u32 bi = reass->first_bi;
678 u8 *s = NULL;
679 while (~0 != bi)
680 {
681 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
682 s = format (s, "%u: %U\n", bi, format_hexdump,
683 vlib_buffer_get_current (b), b->current_length);
684 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
685 {
686 bi = b->next_buffer;
687 }
688 else
689 {
690 break;
691 }
692 }
693 printf ("%.*s\n", vec_len (s), s);
694 fflush (stdout);
695 vec_free (s);
696 }
697 while (0);
698#endif
699 }
Klement Sekerae8498652019-06-17 12:23:15 +0000700 if (!is_custom_app)
Klement Sekera4c533132018-02-22 11:41:12 +0100701 {
702 *next0 = IP6_REASSEMBLY_NEXT_INPUT;
703 }
704 else
705 {
706 *next0 = reass->next_index;
707 }
708 vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
709 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200710 reass = NULL;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100711free_buffers_and_return:
712 vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
713 vec_free (vec_drop_compress);
714 return rv;
Klement Sekera75e7d132017-09-20 08:26:30 +0200715}
716
Klement Sekera4c533132018-02-22 11:41:12 +0100717always_inline void
718ip6_reass_insert_range_in_chain (vlib_main_t * vm, ip6_reass_main_t * rm,
719 ip6_reass_per_thread_t * rt,
720 ip6_reass_t * reass, u32 prev_range_bi,
721 u32 new_next_bi)
Klement Sekera75e7d132017-09-20 08:26:30 +0200722{
723
724 vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
725 vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
726 if (~0 != prev_range_bi)
727 {
728 vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
729 vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
730 new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
731 prev_vnb->ip.reass.next_range_bi = new_next_bi;
732 }
733 else
734 {
735 if (~0 != reass->first_bi)
736 {
737 new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
738 }
739 reass->first_bi = new_next_bi;
740 }
741 reass->data_len += ip6_reass_buffer_get_data_len (new_next_b);
Klement Sekera75e7d132017-09-20 08:26:30 +0200742}
743
Klement Sekerad0f70a32018-12-14 17:24:13 +0100744always_inline ip6_reass_rc_t
Klement Sekera75e7d132017-09-20 08:26:30 +0200745ip6_reass_update (vlib_main_t * vm, vlib_node_runtime_t * node,
Klement Sekera4c533132018-02-22 11:41:12 +0100746 ip6_reass_main_t * rm, ip6_reass_per_thread_t * rt,
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100747 ip6_reass_t * reass, u32 * bi0, u32 * next0, u32 * error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000748 ip6_frag_hdr_t * frag_hdr, bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200749{
750 int consumed = 0;
751 vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
752 vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
Klement Sekerae8498652019-06-17 12:23:15 +0000753 if (is_custom_app)
754 {
755 reass->next_index = fvnb->ip.reass.next_index; // store next_index before it's overwritten
756 reass->error_next_index = fvnb->ip.reass.error_next_index; // store error_next_index before it is overwritten
757 }
Klement Sekera21aa8f12019-05-20 12:27:33 +0200758
Klement Sekera75e7d132017-09-20 08:26:30 +0200759 fvnb->ip.reass.ip6_frag_hdr_offset =
760 (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
761 ip6_header_t *fip = vlib_buffer_get_current (fb);
Klement Sekerad0f70a32018-12-14 17:24:13 +0100762 if (fb->current_length < sizeof (*fip) ||
763 fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
764 fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
765 {
766 return IP6_REASS_RC_INTERNAL_ERROR;
767 }
768
Klement Sekera75e7d132017-09-20 08:26:30 +0200769 u32 fragment_first = fvnb->ip.reass.fragment_first =
770 ip6_frag_hdr_offset_bytes (frag_hdr);
771 u32 fragment_length =
772 vlib_buffer_length_in_chain (vm, fb) -
773 (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
774 u32 fragment_last = fvnb->ip.reass.fragment_last =
775 fragment_first + fragment_length - 1;
776 int more_fragments = ip6_frag_hdr_more (frag_hdr);
777 u32 candidate_range_bi = reass->first_bi;
778 u32 prev_range_bi = ~0;
779 fvnb->ip.reass.range_first = fragment_first;
780 fvnb->ip.reass.range_last = fragment_last;
781 fvnb->ip.reass.next_range_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200782 if (!more_fragments)
783 {
784 reass->last_packet_octet = fragment_last;
785 }
786 if (~0 == reass->first_bi)
787 {
788 // starting a new reassembly
Klement Sekera4c533132018-02-22 11:41:12 +0100789 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
790 *bi0);
Klement Sekera4c533132018-02-22 11:41:12 +0100791 reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100792 consumed = 1;
Klement Sekera3a343d42019-05-16 14:35:46 +0200793 reass->fragments_n = 1;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100794 goto check_if_done_maybe;
Klement Sekera75e7d132017-09-20 08:26:30 +0200795 }
Klement Sekera4c533132018-02-22 11:41:12 +0100796 reass->min_fragment_length =
Klement Sekera75e7d132017-09-20 08:26:30 +0200797 clib_min (clib_net_to_host_u16 (fip->payload_length),
798 fvnb->ip.reass.estimated_mtu);
799 while (~0 != candidate_range_bi)
800 {
801 vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
802 vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
803 if (fragment_first > candidate_vnb->ip.reass.range_last)
804 {
805 // this fragments starts after candidate range
806 prev_range_bi = candidate_range_bi;
807 candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
808 if (candidate_vnb->ip.reass.range_last < fragment_last &&
809 ~0 == candidate_range_bi)
810 {
811 // special case - this fragment falls beyond all known ranges
Klement Sekera4c533132018-02-22 11:41:12 +0100812 ip6_reass_insert_range_in_chain (vm, rm, rt, reass,
813 prev_range_bi, *bi0);
Klement Sekera75e7d132017-09-20 08:26:30 +0200814 consumed = 1;
815 break;
816 }
817 continue;
818 }
819 if (fragment_last < candidate_vnb->ip.reass.range_first)
820 {
821 // this fragment ends before candidate range without any overlap
Klement Sekera4c533132018-02-22 11:41:12 +0100822 ip6_reass_insert_range_in_chain (vm, rm, rt, reass, prev_range_bi,
Klement Sekera75e7d132017-09-20 08:26:30 +0200823 *bi0);
824 consumed = 1;
825 }
826 else if (fragment_first == candidate_vnb->ip.reass.range_first &&
827 fragment_last == candidate_vnb->ip.reass.range_last)
828 {
829 // duplicate fragment - ignore
830 }
831 else
832 {
833 // overlapping fragment - not allowed by RFC 8200
Klement Sekerae8498652019-06-17 12:23:15 +0000834 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera4c533132018-02-22 11:41:12 +0100835 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +0200836 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
837 {
838 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_OVERLAP,
839 0);
840 }
Klement Sekera4c533132018-02-22 11:41:12 +0100841 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200842 *error0 = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
Klement Sekerabe30fea2019-02-19 11:28:47 +0100843 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200844 }
845 break;
846 }
Klement Sekera3a343d42019-05-16 14:35:46 +0200847 ++reass->fragments_n;
Klement Sekeraf1b4e522019-02-19 14:47:25 +0100848check_if_done_maybe:
Klement Sekera75e7d132017-09-20 08:26:30 +0200849 if (consumed)
850 {
851 if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
852 {
853 ip6_reass_add_trace (vm, node, rm, reass, *bi0, RANGE_NEW, 0);
854 }
855 }
856 if (~0 != reass->last_packet_octet &&
857 reass->data_len == reass->last_packet_octet + 1)
858 {
Klement Sekerad0f70a32018-12-14 17:24:13 +0100859 return ip6_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
Klement Sekerae8498652019-06-17 12:23:15 +0000860 is_custom_app);
Klement Sekera75e7d132017-09-20 08:26:30 +0200861 }
862 else
863 {
864 if (consumed)
865 {
866 *bi0 = ~0;
Klement Sekera3a343d42019-05-16 14:35:46 +0200867 if (reass->fragments_n > rm->max_reass_len)
868 {
869 return IP6_REASS_RC_TOO_MANY_FRAGMENTS;
870 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200871 }
872 else
873 {
Klement Sekera4c533132018-02-22 11:41:12 +0100874 *next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera75e7d132017-09-20 08:26:30 +0200875 *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
876 }
877 }
Klement Sekerad0f70a32018-12-14 17:24:13 +0100878 return IP6_REASS_RC_OK;
Klement Sekera75e7d132017-09-20 08:26:30 +0200879}
880
Klement Sekera4c533132018-02-22 11:41:12 +0100881always_inline bool
882ip6_reass_verify_upper_layer_present (vlib_node_runtime_t * node,
883 vlib_buffer_t * b,
884 ip6_frag_hdr_t * frag_hdr)
885{
886 ip6_ext_header_t *tmp = (ip6_ext_header_t *) frag_hdr;
887 while (ip6_ext_hdr (tmp->next_hdr))
888 {
889 tmp = ip6_ext_next_header (tmp);
890 }
891 if (IP_PROTOCOL_IP6_NONXT == tmp->next_hdr)
892 {
893 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
894 ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain,
895 0);
896 b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
897
898 return false;
899 }
900 return true;
901}
902
903always_inline bool
904ip6_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
905 vlib_node_runtime_t * node,
906 vlib_buffer_t * b,
907 ip6_frag_hdr_t * frag_hdr)
908{
909 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
910 ip6_header_t *ip = vlib_buffer_get_current (b);
911 int more_fragments = ip6_frag_hdr_more (frag_hdr);
912 u32 fragment_length =
913 vlib_buffer_length_in_chain (vm, b) -
914 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
915 if (more_fragments && 0 != fragment_length % 8)
916 {
917 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
918 ICMP6_parameter_problem_erroneous_header_field,
919 (u8 *) & ip->payload_length - (u8 *) ip);
920 return false;
921 }
922 return true;
923}
924
925always_inline bool
926ip6_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
927 vlib_node_runtime_t * node,
928 vlib_buffer_t * b,
929 ip6_frag_hdr_t * frag_hdr)
930{
931 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
932 u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
933 u32 fragment_length =
934 vlib_buffer_length_in_chain (vm, b) -
935 (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
936 if (fragment_first + fragment_length > 65535)
937 {
938 ip6_header_t *ip0 = vlib_buffer_get_current (b);
939 icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
940 ICMP6_parameter_problem_erroneous_header_field,
941 (u8 *) & frag_hdr->fragment_offset_and_more
942 - (u8 *) ip0);
943 return false;
944 }
945 return true;
946}
947
Klement Sekera75e7d132017-09-20 08:26:30 +0200948always_inline uword
Klement Sekerae8498652019-06-17 12:23:15 +0000949ip6_reassembly_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
950 vlib_frame_t * frame, bool is_feature,
951 bool is_custom_app)
Klement Sekera75e7d132017-09-20 08:26:30 +0200952{
953 u32 *from = vlib_frame_vector_args (frame);
954 u32 n_left_from, n_left_to_next, *to_next, next_index;
955 ip6_reass_main_t *rm = &ip6_reass_main;
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -0800956 ip6_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
Klement Sekera4c533132018-02-22 11:41:12 +0100957 clib_spinlock_lock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +0200958
959 n_left_from = frame->n_vectors;
960 next_index = node->cached_next_index;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100961 while (n_left_from > 0)
Klement Sekera75e7d132017-09-20 08:26:30 +0200962 {
963 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
964
Klement Sekera75e7d132017-09-20 08:26:30 +0200965 while (n_left_from > 0 && n_left_to_next > 0)
966 {
967 u32 bi0;
968 vlib_buffer_t *b0;
Klement Sekeraf883f6a2019-02-13 11:01:32 +0100969 u32 next0 = IP6_REASSEMBLY_NEXT_DROP;
Klement Sekera4c533132018-02-22 11:41:12 +0100970 u32 error0 = IP6_ERROR_NONE;
971 u32 icmp_bi = ~0;
Klement Sekera75e7d132017-09-20 08:26:30 +0200972
973 bi0 = from[0];
974 b0 = vlib_get_buffer (vm, bi0);
975
976 ip6_header_t *ip0 = vlib_buffer_get_current (b0);
Klement Sekera3ecc2212018-03-27 10:34:43 +0200977 ip6_frag_hdr_t *frag_hdr = NULL;
Klement Sekera75e7d132017-09-20 08:26:30 +0200978 ip6_ext_header_t *prev_hdr;
Klement Sekera3ecc2212018-03-27 10:34:43 +0200979 if (ip6_ext_hdr (ip0->protocol))
980 {
981 ip6_ext_header_find_t (ip0, prev_hdr, frag_hdr,
982 IP_PROTOCOL_IPV6_FRAGMENTATION);
983 }
Klement Sekera4c533132018-02-22 11:41:12 +0100984 if (!frag_hdr)
985 {
986 // this is a regular packet - no fragmentation
987 next0 = IP6_REASSEMBLY_NEXT_INPUT;
988 goto skip_reass;
989 }
Klement Sekera75e7d132017-09-20 08:26:30 +0200990 if (0 == ip6_frag_hdr_offset (frag_hdr))
991 {
992 // first fragment - verify upper-layer is present
Klement Sekera4c533132018-02-22 11:41:12 +0100993 if (!ip6_reass_verify_upper_layer_present (node, b0, frag_hdr))
Klement Sekera75e7d132017-09-20 08:26:30 +0200994 {
Klement Sekera4c533132018-02-22 11:41:12 +0100995 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
996 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +0200997 }
Klement Sekera4c533132018-02-22 11:41:12 +0100998 }
999 if (!ip6_reass_verify_fragment_multiple_8 (vm, node, b0, frag_hdr)
1000 || !ip6_reass_verify_packet_size_lt_64k (vm, node, b0,
1001 frag_hdr))
1002 {
1003 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1004 goto skip_reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001005 }
1006 vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1007 (u8 *) frag_hdr - (u8 *) ip0;
1008
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001009 ip6_reass_kv_t kv;
1010 u8 do_handoff = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001011
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001012 kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1013 kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1014 kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1015 kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1016 kv.k.as_u64[4] =
1017 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1018 vnet_buffer (b0)->sw_if_index[VLIB_RX])) << 32 |
1019 (u64) frag_hdr->identification;
1020 kv.k.as_u64[5] = ip0->protocol;
1021
1022 ip6_reass_t *reass =
1023 ip6_reass_find_or_create (vm, node, rm, rt, &kv, &icmp_bi,
Klement Sekerae8498652019-06-17 12:23:15 +00001024 &do_handoff);
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001025
1026 if (PREDICT_FALSE (do_handoff))
1027 {
1028 next0 = IP6_REASSEMBLY_NEXT_HANDOFF;
1029 if (is_feature)
1030 vnet_buffer (b0)->ip.reass.owner_feature_thread_index =
1031 kv.v.thread_index;
1032 else
1033 vnet_buffer (b0)->ip.reass.owner_thread_index =
1034 kv.v.thread_index;
1035 }
1036 else if (reass)
Klement Sekera75e7d132017-09-20 08:26:30 +02001037 {
Klement Sekerad0f70a32018-12-14 17:24:13 +01001038 switch (ip6_reass_update (vm, node, rm, rt, reass, &bi0, &next0,
Klement Sekerae8498652019-06-17 12:23:15 +00001039 &error0, frag_hdr, is_custom_app))
Klement Sekerad0f70a32018-12-14 17:24:13 +01001040 {
1041 case IP6_REASS_RC_OK:
1042 /* nothing to do here */
1043 break;
Klement Sekera3a343d42019-05-16 14:35:46 +02001044 case IP6_REASS_RC_TOO_MANY_FRAGMENTS:
1045 vlib_node_increment_counter (vm, node->node_index,
1046 IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG,
1047 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001048 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001049 ip6_reass_free (rm, rt, reass);
1050 goto next_packet;
1051 break;
Klement Sekeraf883f6a2019-02-13 11:01:32 +01001052 case IP6_REASS_RC_NO_BUF:
Klement Sekera3a343d42019-05-16 14:35:46 +02001053 vlib_node_increment_counter (vm, node->node_index,
1054 IP6_ERROR_REASS_NO_BUF, 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001055 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekera3a343d42019-05-16 14:35:46 +02001056 ip6_reass_free (rm, rt, reass);
1057 goto next_packet;
1058 break;
Klement Sekerad0f70a32018-12-14 17:24:13 +01001059 case IP6_REASS_RC_INTERNAL_ERROR:
Klement Sekera21aa8f12019-05-20 12:27:33 +02001060 /* drop everything and start with a clean slate */
Klement Sekera3a343d42019-05-16 14:35:46 +02001061 vlib_node_increment_counter (vm, node->node_index,
1062 IP6_ERROR_REASS_INTERNAL_ERROR,
1063 1);
Klement Sekerae8498652019-06-17 12:23:15 +00001064 ip6_reass_drop_all (vm, node, rm, reass);
Klement Sekerad0f70a32018-12-14 17:24:13 +01001065 ip6_reass_free (rm, rt, reass);
1066 goto next_packet;
1067 break;
1068 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001069 }
1070 else
1071 {
Klement Sekera21aa8f12019-05-20 12:27:33 +02001072 if (is_feature)
1073 {
1074 next0 = IP6_REASSEMBLY_NEXT_DROP;
1075 }
1076 else
1077 {
1078 vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1079 next0 = fvnb->ip.reass.error_next_index;
1080 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001081 error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1082 }
1083
1084 b0->error = node->errors[error0];
1085
1086 if (~0 != bi0)
1087 {
Klement Sekera4c533132018-02-22 11:41:12 +01001088 skip_reass:
Klement Sekera75e7d132017-09-20 08:26:30 +02001089 to_next[0] = bi0;
1090 to_next += 1;
1091 n_left_to_next -= 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001092 if (is_feature && IP6_ERROR_NONE == error0)
1093 {
Kingwel Xiea0060652018-09-26 04:59:52 -04001094 b0 = vlib_get_buffer (vm, bi0);
Damjan Marion7d98a122018-07-19 20:42:08 +02001095 vnet_feature_next (&next0, b0);
Klement Sekera4c533132018-02-22 11:41:12 +01001096 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001097 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1098 n_left_to_next, bi0, next0);
1099 }
1100
1101 if (~0 != icmp_bi)
1102 {
1103 next0 = IP6_REASSEMBLY_NEXT_ICMP_ERROR;
1104 to_next[0] = icmp_bi;
1105 to_next += 1;
1106 n_left_to_next -= 1;
1107 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1108 n_left_to_next, icmp_bi,
1109 next0);
1110 }
Klement Sekerad0f70a32018-12-14 17:24:13 +01001111 next_packet:
Klement Sekera75e7d132017-09-20 08:26:30 +02001112 from += 1;
1113 n_left_from -= 1;
1114 }
1115
1116 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1117 }
1118
Klement Sekera4c533132018-02-22 11:41:12 +01001119 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001120 return frame->n_vectors;
1121}
1122
1123static char *ip6_reassembly_error_strings[] = {
1124#define _(sym, string) string,
1125 foreach_ip6_error
1126#undef _
1127};
1128
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001129VLIB_NODE_FN (ip6_reass_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
1130 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001131{
Klement Sekerae8498652019-06-17 12:23:15 +00001132 return ip6_reassembly_inline (vm, node, frame, false /* is_feature */ ,
1133 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001134}
1135
Klement Sekera75e7d132017-09-20 08:26:30 +02001136/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001137VLIB_REGISTER_NODE (ip6_reass_node) = {
Klement Sekera75e7d132017-09-20 08:26:30 +02001138 .name = "ip6-reassembly",
1139 .vector_size = sizeof (u32),
1140 .format_trace = format_ip6_reass_trace,
1141 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1142 .error_strings = ip6_reassembly_error_strings,
1143 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1144 .next_nodes =
1145 {
1146 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1147 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1148 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001149 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reassembly-handoff",
Klement Sekera75e7d132017-09-20 08:26:30 +02001150 },
1151};
1152/* *INDENT-ON* */
1153
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001154VLIB_NODE_FN (ip6_reass_node_feature) (vlib_main_t * vm,
1155 vlib_node_runtime_t * node,
1156 vlib_frame_t * frame)
Klement Sekera4c533132018-02-22 11:41:12 +01001157{
Klement Sekerae8498652019-06-17 12:23:15 +00001158 return ip6_reassembly_inline (vm, node, frame, true /* is_feature */ ,
1159 false /* is_custom_app */ );
Klement Sekera4c533132018-02-22 11:41:12 +01001160}
1161
1162/* *INDENT-OFF* */
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001163VLIB_REGISTER_NODE (ip6_reass_node_feature) = {
Klement Sekera4c533132018-02-22 11:41:12 +01001164 .name = "ip6-reassembly-feature",
1165 .vector_size = sizeof (u32),
1166 .format_trace = format_ip6_reass_trace,
1167 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1168 .error_strings = ip6_reassembly_error_strings,
1169 .n_next_nodes = IP6_REASSEMBLY_N_NEXT,
1170 .next_nodes =
1171 {
1172 [IP6_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1173 [IP6_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1174 [IP6_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001175 [IP6_REASSEMBLY_NEXT_HANDOFF] = "ip6-reass-feature-hoff",
Klement Sekera4c533132018-02-22 11:41:12 +01001176 },
1177};
1178/* *INDENT-ON* */
1179
Klement Sekera4c533132018-02-22 11:41:12 +01001180/* *INDENT-OFF* */
1181VNET_FEATURE_INIT (ip6_reassembly_feature, static) = {
1182 .arc_name = "ip6-unicast",
1183 .node_name = "ip6-reassembly-feature",
1184 .runs_before = VNET_FEATURES ("ip6-lookup"),
1185 .runs_after = 0,
1186};
1187/* *INDENT-ON* */
1188
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001189#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001190static u32
1191ip6_reass_get_nbuckets ()
Klement Sekera75e7d132017-09-20 08:26:30 +02001192{
1193 ip6_reass_main_t *rm = &ip6_reass_main;
1194 u32 nbuckets;
1195 u8 i;
1196
1197 nbuckets = (u32) (rm->max_reass_n / IP6_REASS_HT_LOAD_FACTOR);
1198
1199 for (i = 0; i < 31; i++)
1200 if ((1 << i) >= nbuckets)
1201 break;
1202 nbuckets = 1 << i;
1203
1204 return nbuckets;
1205}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001206#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001207
1208typedef enum
1209{
1210 IP6_EVENT_CONFIG_CHANGED = 1,
1211} ip6_reass_event_t;
1212
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001213#ifndef CLIB_MARCH_VARIANT
Klement Sekera75e7d132017-09-20 08:26:30 +02001214typedef struct
1215{
1216 int failure;
1217 clib_bihash_48_8_t *new_hash;
1218} ip6_rehash_cb_ctx;
1219
Klement Sekera4c533132018-02-22 11:41:12 +01001220static void
Klement Sekera75e7d132017-09-20 08:26:30 +02001221ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1222{
1223 ip6_rehash_cb_ctx *ctx = _ctx;
1224 if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1225 {
1226 ctx->failure = 1;
1227 }
1228}
1229
Klement Sekera4c533132018-02-22 11:41:12 +01001230static void
1231ip6_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001232 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera4c533132018-02-22 11:41:12 +01001233{
1234 ip6_reass_main.timeout_ms = timeout_ms;
1235 ip6_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1236 ip6_reass_main.max_reass_n = max_reassemblies;
Klement Sekera3a343d42019-05-16 14:35:46 +02001237 ip6_reass_main.max_reass_len = max_reassembly_length;
Klement Sekera4c533132018-02-22 11:41:12 +01001238 ip6_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1239}
1240
Klement Sekera75e7d132017-09-20 08:26:30 +02001241vnet_api_error_t
1242ip6_reass_set (u32 timeout_ms, u32 max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02001243 u32 max_reassembly_length, u32 expire_walk_interval_ms)
Klement Sekera75e7d132017-09-20 08:26:30 +02001244{
1245 u32 old_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3a343d42019-05-16 14:35:46 +02001246 ip6_reass_set_params (timeout_ms, max_reassemblies, max_reassembly_length,
Klement Sekera4c533132018-02-22 11:41:12 +01001247 expire_walk_interval_ms);
Klement Sekera75e7d132017-09-20 08:26:30 +02001248 vlib_process_signal_event (ip6_reass_main.vlib_main,
1249 ip6_reass_main.ip6_reass_expire_node_idx,
1250 IP6_EVENT_CONFIG_CHANGED, 0);
1251 u32 new_nbuckets = ip6_reass_get_nbuckets ();
Klement Sekera3ecc2212018-03-27 10:34:43 +02001252 if (ip6_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
Klement Sekera75e7d132017-09-20 08:26:30 +02001253 {
1254 clib_bihash_48_8_t new_hash;
Dave Barachb7b92992018-10-17 10:38:51 -04001255 clib_memset (&new_hash, 0, sizeof (new_hash));
Klement Sekera75e7d132017-09-20 08:26:30 +02001256 ip6_rehash_cb_ctx ctx;
1257 ctx.failure = 0;
1258 ctx.new_hash = &new_hash;
1259 clib_bihash_init_48_8 (&new_hash, "ip6-reass", new_nbuckets,
1260 new_nbuckets * 1024);
1261 clib_bihash_foreach_key_value_pair_48_8 (&ip6_reass_main.hash,
1262 ip6_rehash_cb, &ctx);
1263 if (ctx.failure)
1264 {
1265 clib_bihash_free_48_8 (&new_hash);
1266 return -1;
1267 }
1268 else
1269 {
1270 clib_bihash_free_48_8 (&ip6_reass_main.hash);
Dave Barach178cf492018-11-13 16:34:13 -05001271 clib_memcpy_fast (&ip6_reass_main.hash, &new_hash,
1272 sizeof (ip6_reass_main.hash));
Dave Barach32dcd3b2019-07-08 12:25:38 -04001273 clib_bihash_copied (&ip6_reass_main.hash, &new_hash);
Klement Sekera75e7d132017-09-20 08:26:30 +02001274 }
1275 }
1276 return 0;
1277}
1278
1279vnet_api_error_t
1280ip6_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1281 u32 * expire_walk_interval_ms)
1282{
1283 *timeout_ms = ip6_reass_main.timeout_ms;
1284 *max_reassemblies = ip6_reass_main.max_reass_n;
1285 *expire_walk_interval_ms = ip6_reass_main.expire_walk_interval_ms;
1286 return 0;
1287}
1288
Klement Sekera4c533132018-02-22 11:41:12 +01001289static clib_error_t *
Klement Sekera75e7d132017-09-20 08:26:30 +02001290ip6_reass_init_function (vlib_main_t * vm)
1291{
1292 ip6_reass_main_t *rm = &ip6_reass_main;
1293 clib_error_t *error = 0;
1294 u32 nbuckets;
Dave Barach1403fcd2018-02-05 09:45:43 -05001295 vlib_node_t *node;
Klement Sekera75e7d132017-09-20 08:26:30 +02001296
1297 rm->vlib_main = vm;
1298 rm->vnet_main = vnet_get_main ();
1299
Juraj Slobodacd806922018-10-10 10:15:54 +02001300 vec_validate (rm->per_thread_data, vlib_num_workers ());
Klement Sekera4c533132018-02-22 11:41:12 +01001301 ip6_reass_per_thread_t *rt;
1302 vec_foreach (rt, rm->per_thread_data)
1303 {
1304 clib_spinlock_init (&rt->lock);
1305 pool_alloc (rt->pool, rm->max_reass_n);
1306 }
Dave Barach1403fcd2018-02-05 09:45:43 -05001307
1308 node = vlib_get_node_by_name (vm, (u8 *) "ip6-reassembly-expire-walk");
1309 ASSERT (node);
1310 rm->ip6_reass_expire_node_idx = node->index;
1311
Klement Sekera3ecc2212018-03-27 10:34:43 +02001312 ip6_reass_set_params (IP6_REASS_TIMEOUT_DEFAULT_MS,
1313 IP6_REASS_MAX_REASSEMBLIES_DEFAULT,
Klement Sekera3a343d42019-05-16 14:35:46 +02001314 IP6_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
Klement Sekera3ecc2212018-03-27 10:34:43 +02001315 IP6_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1316
Klement Sekera75e7d132017-09-20 08:26:30 +02001317 nbuckets = ip6_reass_get_nbuckets ();
1318 clib_bihash_init_48_8 (&rm->hash, "ip6-reass", nbuckets, nbuckets * 1024);
1319
Dave Barach1403fcd2018-02-05 09:45:43 -05001320 node = vlib_get_node_by_name (vm, (u8 *) "ip6-drop");
Klement Sekera75e7d132017-09-20 08:26:30 +02001321 ASSERT (node);
1322 rm->ip6_drop_idx = node->index;
Klement Sekera75e7d132017-09-20 08:26:30 +02001323 node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1324 ASSERT (node);
1325 rm->ip6_icmp_error_idx = node->index;
1326
1327 if ((error = vlib_call_init_function (vm, ip_main_init)))
1328 return error;
1329 ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1330 ip6_reass_node.index);
Klement Sekera4c533132018-02-22 11:41:12 +01001331
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001332 rm->fq_index = vlib_frame_queue_main_init (ip6_reass_node.index, 0);
1333 rm->fq_feature_index =
1334 vlib_frame_queue_main_init (ip6_reass_node_feature.index, 0);
1335
Klement Sekera75e7d132017-09-20 08:26:30 +02001336 return error;
1337}
1338
1339VLIB_INIT_FUNCTION (ip6_reass_init_function);
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001340#endif /* CLIB_MARCH_VARIANT */
Klement Sekera75e7d132017-09-20 08:26:30 +02001341
1342static uword
1343ip6_reass_walk_expired (vlib_main_t * vm,
1344 vlib_node_runtime_t * node, vlib_frame_t * f)
1345{
1346 ip6_reass_main_t *rm = &ip6_reass_main;
1347 uword event_type, *event_data = 0;
1348
1349 while (true)
1350 {
1351 vlib_process_wait_for_event_or_clock (vm,
1352 (f64) rm->expire_walk_interval_ms
1353 / (f64) MSEC_PER_SEC);
1354 event_type = vlib_process_get_events (vm, &event_data);
1355
1356 switch (event_type)
1357 {
1358 case ~0: /* no events => timeout */
1359 /* nothing to do here */
1360 break;
1361 case IP6_EVENT_CONFIG_CHANGED:
1362 break;
1363 default:
1364 clib_warning ("BUG: event type 0x%wx", event_type);
1365 break;
1366 }
1367 f64 now = vlib_time_now (vm);
1368
1369 ip6_reass_t *reass;
Klement Sekera75e7d132017-09-20 08:26:30 +02001370 int *pool_indexes_to_free = NULL;
1371
Klement Sekera4c533132018-02-22 11:41:12 +01001372 uword thread_index = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001373 int index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001374 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001375 u32 *vec_icmp_bi = NULL;
Klement Sekera4c533132018-02-22 11:41:12 +01001376 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1377 {
1378 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1379 clib_spinlock_lock (&rt->lock);
1380
1381 vec_reset_length (pool_indexes_to_free);
1382 /* *INDENT-OFF* */
1383 pool_foreach_index (index, rt->pool, ({
1384 reass = pool_elt_at_index (rt->pool, index);
1385 if (now > reass->last_heard + rm->timeout)
1386 {
1387 vec_add1 (pool_indexes_to_free, index);
1388 }
1389 }));
1390 /* *INDENT-ON* */
1391 int *i;
1392 /* *INDENT-OFF* */
1393 vec_foreach (i, pool_indexes_to_free)
Klement Sekera75e7d132017-09-20 08:26:30 +02001394 {
Klement Sekera4c533132018-02-22 11:41:12 +01001395 ip6_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1396 u32 icmp_bi = ~0;
Klement Sekerae8498652019-06-17 12:23:15 +00001397 ip6_reass_on_timeout (vm, node, rm, reass, &icmp_bi);
Klement Sekera4c533132018-02-22 11:41:12 +01001398 if (~0 != icmp_bi)
Dave Baracha638c182019-06-21 18:24:07 -04001399 vec_add1 (vec_icmp_bi, icmp_bi);
1400
Klement Sekera4c533132018-02-22 11:41:12 +01001401 ip6_reass_free (rm, rt, reass);
Klement Sekera75e7d132017-09-20 08:26:30 +02001402 }
Klement Sekera4c533132018-02-22 11:41:12 +01001403 /* *INDENT-ON* */
1404
1405 clib_spinlock_unlock (&rt->lock);
1406 }
Klement Sekera75e7d132017-09-20 08:26:30 +02001407
Klement Sekera75e7d132017-09-20 08:26:30 +02001408 while (vec_len (vec_icmp_bi) > 0)
1409 {
1410 vlib_frame_t *f =
1411 vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1412 u32 *to_next = vlib_frame_vector_args (f);
1413 u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
Klement Sekera4c533132018-02-22 11:41:12 +01001414 int trace_frame = 0;
Klement Sekera75e7d132017-09-20 08:26:30 +02001415 while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1416 {
1417 u32 bi = vec_pop (vec_icmp_bi);
1418 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1419 if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
Dave Baracha638c182019-06-21 18:24:07 -04001420 trace_frame = 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001421 b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1422 to_next[0] = bi;
1423 ++f->n_vectors;
1424 to_next += 1;
1425 n_left_to_next -= 1;
Klement Sekera75e7d132017-09-20 08:26:30 +02001426 }
Damjan Marion633b6fd2018-09-14 14:38:53 +02001427 f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
Klement Sekera75e7d132017-09-20 08:26:30 +02001428 vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1429 }
1430
1431 vec_free (pool_indexes_to_free);
Klement Sekera75e7d132017-09-20 08:26:30 +02001432 vec_free (vec_icmp_bi);
1433 if (event_data)
1434 {
1435 _vec_len (event_data) = 0;
1436 }
1437 }
1438
1439 return 0;
1440}
1441
Klement Sekera75e7d132017-09-20 08:26:30 +02001442/* *INDENT-OFF* */
1443VLIB_REGISTER_NODE (ip6_reass_expire_node, static) = {
1444 .function = ip6_reass_walk_expired,
1445 .format_trace = format_ip6_reass_trace,
1446 .type = VLIB_NODE_TYPE_PROCESS,
1447 .name = "ip6-reassembly-expire-walk",
1448
1449 .n_errors = ARRAY_LEN (ip6_reassembly_error_strings),
1450 .error_strings = ip6_reassembly_error_strings,
1451
1452};
1453/* *INDENT-ON* */
1454
1455static u8 *
1456format_ip6_reass_key (u8 * s, va_list * args)
1457{
1458 ip6_reass_key_t *key = va_arg (*args, ip6_reass_key_t *);
1459 s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1460 key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1461 &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1462 return s;
1463}
1464
1465static u8 *
1466format_ip6_reass (u8 * s, va_list * args)
1467{
1468 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1469 ip6_reass_t *reass = va_arg (*args, ip6_reass_t *);
1470
Klement Sekera4c533132018-02-22 11:41:12 +01001471 s = format (s, "ID: %lu, key: %U\n first_bi: %u, data_len: %u, "
Klement Sekera75e7d132017-09-20 08:26:30 +02001472 "last_packet_octet: %u, trace_op_counter: %u\n",
1473 reass->id, format_ip6_reass_key, &reass->key, reass->first_bi,
1474 reass->data_len, reass->last_packet_octet,
1475 reass->trace_op_counter);
1476 u32 bi = reass->first_bi;
1477 u32 counter = 0;
1478 while (~0 != bi)
1479 {
1480 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1481 vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1482 s = format (s, " #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1483 "fragment[%u, %u]\n",
1484 counter, vnb->ip.reass.range_first,
1485 vnb->ip.reass.range_last, bi,
Klement Sekerad0f70a32018-12-14 17:24:13 +01001486 ip6_reass_buffer_get_data_offset (b),
1487 ip6_reass_buffer_get_data_len (b),
Klement Sekera75e7d132017-09-20 08:26:30 +02001488 vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1489 if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1490 {
1491 bi = b->next_buffer;
1492 }
1493 else
1494 {
1495 bi = ~0;
1496 }
1497 }
1498 return s;
1499}
1500
1501static clib_error_t *
1502show_ip6_reass (vlib_main_t * vm, unformat_input_t * input,
1503 CLIB_UNUSED (vlib_cli_command_t * lmd))
1504{
1505 ip6_reass_main_t *rm = &ip6_reass_main;
1506
1507 vlib_cli_output (vm, "---------------------");
1508 vlib_cli_output (vm, "IP6 reassembly status");
1509 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001510 bool details = false;
Klement Sekera75e7d132017-09-20 08:26:30 +02001511 if (unformat (input, "details"))
1512 {
Klement Sekera4c533132018-02-22 11:41:12 +01001513 details = true;
1514 }
1515
1516 u32 sum_reass_n = 0;
1517 u64 sum_buffers_n = 0;
1518 ip6_reass_t *reass;
1519 uword thread_index;
Juraj Slobodacd806922018-10-10 10:15:54 +02001520 const uword nthreads = vlib_num_workers () + 1;
Klement Sekera4c533132018-02-22 11:41:12 +01001521 for (thread_index = 0; thread_index < nthreads; ++thread_index)
1522 {
1523 ip6_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1524 clib_spinlock_lock (&rt->lock);
1525 if (details)
1526 {
1527 /* *INDENT-OFF* */
1528 pool_foreach (reass, rt->pool, {
1529 vlib_cli_output (vm, "%U", format_ip6_reass, vm, reass);
1530 });
1531 /* *INDENT-ON* */
1532 }
1533 sum_reass_n += rt->reass_n;
Klement Sekera4c533132018-02-22 11:41:12 +01001534 clib_spinlock_unlock (&rt->lock);
Klement Sekera75e7d132017-09-20 08:26:30 +02001535 }
1536 vlib_cli_output (vm, "---------------------");
Klement Sekera4c533132018-02-22 11:41:12 +01001537 vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1538 (long unsigned) sum_reass_n);
1539 vlib_cli_output (vm, "Maximum configured concurrent IP6 reassemblies per "
1540 "worker-thread: %lu\n", (long unsigned) rm->max_reass_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001541 vlib_cli_output (vm, "Buffers in use: %lu\n",
Klement Sekera4c533132018-02-22 11:41:12 +01001542 (long unsigned) sum_buffers_n);
Klement Sekera75e7d132017-09-20 08:26:30 +02001543 return 0;
1544}
1545
1546/* *INDENT-OFF* */
1547VLIB_CLI_COMMAND (show_ip6_reassembly_cmd, static) = {
1548 .path = "show ip6-reassembly",
1549 .short_help = "show ip6-reassembly [details]",
1550 .function = show_ip6_reass,
1551};
1552/* *INDENT-ON* */
1553
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001554#ifndef CLIB_MARCH_VARIANT
Klement Sekera4c533132018-02-22 11:41:12 +01001555vnet_api_error_t
1556ip6_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1557{
1558 return vnet_feature_enable_disable ("ip6-unicast", "ip6-reassembly-feature",
1559 sw_if_index, enable_disable, 0, 0);
1560}
Filip Tehlar26ea14e2019-03-11 05:30:21 -07001561#endif /* CLIB_MARCH_VARIANT */
Klement Sekera4c533132018-02-22 11:41:12 +01001562
Vijayabhaskar Katamreddy470a3702019-03-01 19:57:06 -08001563#define foreach_ip6_reassembly_handoff_error \
1564_(CONGESTION_DROP, "congestion drop")
1565
1566
1567typedef enum
1568{
1569#define _(sym,str) IP6_REASSEMBLY_HANDOFF_ERROR_##sym,
1570 foreach_ip6_reassembly_handoff_error
1571#undef _
1572 IP6_REASSEMBLY_HANDOFF_N_ERROR,
1573} ip6_reassembly_handoff_error_t;
1574
1575static char *ip6_reassembly_handoff_error_strings[] = {
1576#define _(sym,string) string,
1577 foreach_ip6_reassembly_handoff_error
1578#undef _
1579};
1580
1581typedef struct
1582{
1583 u32 next_worker_index;
1584} ip6_reassembly_handoff_trace_t;
1585
1586static u8 *
1587format_ip6_reassembly_handoff_trace (u8 * s, va_list * args)
1588{
1589 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1590 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1591 ip6_reassembly_handoff_trace_t *t =
1592 va_arg (*args, ip6_reassembly_handoff_trace_t *);
1593
1594 s =
1595 format (s, "ip6-reassembly-handoff: next-worker %d",
1596 t->next_worker_index);
1597
1598 return s;
1599}
1600
1601always_inline uword
1602ip6_reassembly_handoff_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
1603 vlib_frame_t * frame, bool is_feature)
1604{
1605 ip6_reass_main_t *rm = &ip6_reass_main;
1606
1607 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1608 u32 n_enq, n_left_from, *from;
1609 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1610 u32 fq_index;
1611
1612 from = vlib_frame_vector_args (frame);
1613 n_left_from = frame->n_vectors;
1614 vlib_get_buffers (vm, from, bufs, n_left_from);
1615
1616 b = bufs;
1617 ti = thread_indices;
1618
1619 fq_index = (is_feature) ? rm->fq_feature_index : rm->fq_index;
1620
1621 while (n_left_from > 0)
1622 {
1623 ti[0] =
1624 (is_feature) ? vnet_buffer (b[0])->ip.
1625 reass.owner_feature_thread_index : vnet_buffer (b[0])->ip.
1626 reass.owner_thread_index;
1627
1628 if (PREDICT_FALSE
1629 ((node->flags & VLIB_NODE_FLAG_TRACE)
1630 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1631 {
1632 ip6_reassembly_handoff_trace_t *t =
1633 vlib_add_trace (vm, node, b[0], sizeof (*t));
1634 t->next_worker_index = ti[0];
1635 }
1636
1637 n_left_from -= 1;
1638 ti += 1;
1639 b += 1;
1640 }
1641 n_enq =
1642 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
1643 frame->n_vectors, 1);
1644
1645 if (n_enq < frame->n_vectors)
1646 vlib_node_increment_counter (vm, node->node_index,
1647 IP6_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
1648 frame->n_vectors - n_enq);
1649 return frame->n_vectors;
1650}
1651
1652VLIB_NODE_FN (ip6_reassembly_handoff_node) (vlib_main_t * vm,
1653 vlib_node_runtime_t * node,
1654 vlib_frame_t * frame)
1655{
1656 return ip6_reassembly_handoff_inline (vm, node, frame,
1657 false /* is_feature */ );
1658}
1659
1660/* *INDENT-OFF* */
1661VLIB_REGISTER_NODE (ip6_reassembly_handoff_node) = {
1662 .name = "ip6-reassembly-handoff",
1663 .vector_size = sizeof (u32),
1664 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1665 .error_strings = ip6_reassembly_handoff_error_strings,
1666 .format_trace = format_ip6_reassembly_handoff_trace,
1667
1668 .n_next_nodes = 1,
1669
1670 .next_nodes = {
1671 [0] = "error-drop",
1672 },
1673};
1674
1675
1676VLIB_NODE_FN (ip6_reassembly_feature_handoff_node) (vlib_main_t * vm,
1677 vlib_node_runtime_t * node, vlib_frame_t * frame)
1678{
1679 return ip6_reassembly_handoff_inline (vm, node, frame, true /* is_feature */ );
1680}
1681
1682
1683/* *INDENT-OFF* */
1684VLIB_REGISTER_NODE (ip6_reassembly_feature_handoff_node) = {
1685 .name = "ip6-reass-feature-hoff",
1686 .vector_size = sizeof (u32),
1687 .n_errors = ARRAY_LEN(ip6_reassembly_handoff_error_strings),
1688 .error_strings = ip6_reassembly_handoff_error_strings,
1689 .format_trace = format_ip6_reassembly_handoff_trace,
1690
1691 .n_next_nodes = 1,
1692
1693 .next_nodes = {
1694 [0] = "error-drop",
1695 },
1696};
1697/* *INDENT-ON* */
1698
Klement Sekera75e7d132017-09-20 08:26:30 +02001699/*
1700 * fd.io coding-style-patch-verification: ON
1701 *
1702 * Local Variables:
1703 * eval: (c-set-style "gnu")
1704 * End:
1705 */