ip: reassembly - Fixing buffer leaks, corruption in v6 reasm
[vpp.git] / src / vnet / ip / reass / ip6_full_reass.c
1 /*
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 Full Reassembly.
19  *
20  * This file contains the source code for IPv6 full 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/reass/ip6_full_reass.h>
28 #include <vnet/ip/ip6_inlines.h>
29
30 #define MSEC_PER_SEC 1000
31 #define IP6_FULL_REASS_TIMEOUT_DEFAULT_MS 200
32 /* As there are only 1024 reass context per thread, either the DDOS attacks
33  * or fractions of real timeouts, would consume these contexts quickly and
34  * running out context space and unable to perform reassembly */
35 #define IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS 50 // 50 ms default
36 #define IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT 1024
37 #define IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT 3
38 #define IP6_FULL_REASS_HT_LOAD_FACTOR (0.75)
39
40 typedef enum
41 {
42   IP6_FULL_REASS_RC_OK,
43   IP6_FULL_REASS_RC_INTERNAL_ERROR,
44   IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS,
45   IP6_FULL_REASS_RC_NO_BUF,
46   IP6_FULL_REASS_RC_HANDOFF,
47   IP6_FULL_REASS_RC_INVALID_FRAG_LEN,
48   IP6_FULL_REASS_RC_OVERLAP,
49 } ip6_full_reass_rc_t;
50
51 typedef struct
52 {
53   union
54   {
55     struct
56     {
57       ip6_address_t src;
58       ip6_address_t dst;
59       u32 xx_id;
60       u32 frag_id;
61       u8 unused[7];
62       u8 proto;
63     };
64     u64 as_u64[6];
65   };
66 } ip6_full_reass_key_t;
67
68 typedef union
69 {
70   struct
71   {
72     u32 reass_index;
73     u32 memory_owner_thread_index;
74   };
75   u64 as_u64;
76 } ip6_full_reass_val_t;
77
78 typedef union
79 {
80   struct
81   {
82     ip6_full_reass_key_t k;
83     ip6_full_reass_val_t v;
84   };
85   clib_bihash_kv_48_8_t kv;
86 } ip6_full_reass_kv_t;
87
88
89 always_inline u32
90 ip6_full_reass_buffer_get_data_offset (vlib_buffer_t * b)
91 {
92   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
93   return vnb->ip.reass.range_first - vnb->ip.reass.fragment_first;
94 }
95
96 always_inline u16
97 ip6_full_reass_buffer_get_data_len (vlib_buffer_t * b)
98 {
99   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
100   return clib_min (vnb->ip.reass.range_last, vnb->ip.reass.fragment_last) -
101     (vnb->ip.reass.fragment_first +
102      ip6_full_reass_buffer_get_data_offset (b)) + 1;
103 }
104
105 typedef struct
106 {
107   // hash table key
108   ip6_full_reass_key_t key;
109   // time when last packet was received
110   f64 last_heard;
111   // internal id of this reassembly
112   u64 id;
113   // buffer index of first buffer in this reassembly context
114   u32 first_bi;
115   // last octet of packet, ~0 until fragment without more_fragments arrives
116   u32 last_packet_octet;
117   // length of data collected so far
118   u32 data_len;
119   // trace operation counter
120   u32 trace_op_counter;
121   // next index - used by custom apps (~0 if not set)
122   u32 next_index;
123   // error next index - used by custom apps (~0 if not set)
124   u32 error_next_index;
125   // minimum fragment length for this reassembly - used to estimate MTU
126   u16 min_fragment_length;
127   // number of fragments for this reassembly
128   u32 fragments_n;
129   // thread owning memory for this context (whose pool contains this ctx)
130   u32 memory_owner_thread_index;
131   // thread which received fragment with offset 0 and which sends out the
132   // completed reassembly
133   u32 sendout_thread_index;
134 } ip6_full_reass_t;
135
136 typedef struct
137 {
138   ip6_full_reass_t *pool;
139   u32 reass_n;
140   u32 id_counter;
141   // for pacing the main thread timeouts
142   u32 last_id;
143   clib_spinlock_t lock;
144 } ip6_full_reass_per_thread_t;
145
146 typedef struct
147 {
148   // IPv6 config
149   u32 timeout_ms;
150   f64 timeout;
151   u32 expire_walk_interval_ms;
152   // maximum number of fragments in one reassembly
153   u32 max_reass_len;
154   // maximum number of reassemblies
155   u32 max_reass_n;
156
157   // IPv6 runtime
158   clib_bihash_48_8_t hash;
159
160   // per-thread data
161   ip6_full_reass_per_thread_t *per_thread_data;
162
163   // convenience
164   vlib_main_t *vlib_main;
165
166   u32 ip6_icmp_error_idx;
167   u32 ip6_full_reass_expire_node_idx;
168
169   /** Worker handoff */
170   u32 fq_index;
171   u32 fq_local_index;
172   u32 fq_feature_index;
173   u32 fq_custom_index;
174
175   // reference count for enabling/disabling feature - per interface
176   u32 *feature_use_refcount_per_intf;
177
178   // whether local fragmented packets are reassembled or not
179   int is_local_reass_enabled;
180 } ip6_full_reass_main_t;
181
182 extern ip6_full_reass_main_t ip6_full_reass_main;
183
184 #ifndef CLIB_MARCH_VARIANT
185 ip6_full_reass_main_t ip6_full_reass_main;
186 #endif /* CLIB_MARCH_VARIANT */
187
188 typedef enum
189 {
190   IP6_FULL_REASSEMBLY_NEXT_INPUT,
191   IP6_FULL_REASSEMBLY_NEXT_DROP,
192   IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR,
193   IP6_FULL_REASSEMBLY_NEXT_HANDOFF,
194   IP6_FULL_REASSEMBLY_N_NEXT,
195 } ip6_full_reass_next_t;
196
197 typedef enum
198 {
199   NORMAL,
200   FEATURE,
201   CUSTOM
202 } ip6_full_reass_node_type_t;
203
204 typedef enum
205 {
206   RANGE_NEW,
207   RANGE_DISCARD,
208   RANGE_OVERLAP,
209   ICMP_ERROR_RT_EXCEEDED,
210   ICMP_ERROR_FL_TOO_BIG,
211   ICMP_ERROR_FL_NOT_MULT_8,
212   FINALIZE,
213   HANDOFF,
214   PASSTHROUGH,
215 } ip6_full_reass_trace_operation_e;
216
217 typedef struct
218 {
219   u16 range_first;
220   u16 range_last;
221   u32 range_bi;
222   i32 data_offset;
223   u32 data_len;
224   u32 first_bi;
225 } ip6_full_reass_range_trace_t;
226
227 typedef struct
228 {
229   ip6_full_reass_trace_operation_e action;
230   u32 reass_id;
231   ip6_full_reass_range_trace_t trace_range;
232   u32 op_id;
233   u32 fragment_first;
234   u32 fragment_last;
235   u32 total_data_len;
236   u32 thread_id;
237   u32 thread_id_to;
238   bool is_after_handoff;
239   ip6_header_t ip6_header;
240   ip6_frag_hdr_t ip6_frag_header;
241 } ip6_full_reass_trace_t;
242
243 static void
244 ip6_full_reass_trace_details (vlib_main_t * vm, u32 bi,
245                               ip6_full_reass_range_trace_t * trace)
246 {
247   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
248   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
249   trace->range_first = vnb->ip.reass.range_first;
250   trace->range_last = vnb->ip.reass.range_last;
251   trace->data_offset = ip6_full_reass_buffer_get_data_offset (b);
252   trace->data_len = ip6_full_reass_buffer_get_data_len (b);
253   trace->range_bi = bi;
254 }
255
256 static u8 *
257 format_ip6_full_reass_range_trace (u8 * s, va_list * args)
258 {
259   ip6_full_reass_range_trace_t *trace =
260     va_arg (*args, ip6_full_reass_range_trace_t *);
261   s =
262     format (s, "range: [%u, %u], off %d, len %u, bi %u", trace->range_first,
263             trace->range_last, trace->data_offset, trace->data_len,
264             trace->range_bi);
265   return s;
266 }
267
268 static u8 *
269 format_ip6_full_reass_trace (u8 * s, va_list * args)
270 {
271   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
272   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
273   ip6_full_reass_trace_t *t = va_arg (*args, ip6_full_reass_trace_t *);
274   u32 indent = 0;
275   if (~0 != t->reass_id)
276     {
277       if (t->is_after_handoff)
278         {
279           s =
280             format (s, "%U\n", format_ip6_header, &t->ip6_header,
281                     sizeof (t->ip6_header));
282           s =
283             format (s, "  %U\n", format_ip6_frag_hdr, &t->ip6_frag_header,
284                     sizeof (t->ip6_frag_header));
285           indent = 2;
286         }
287       s =
288         format (s, "%Ureass id: %u, op id: %u, ", format_white_space, indent,
289                 t->reass_id, t->op_id);
290       indent = format_get_indent (s);
291       s = format (s, "first bi: %u, data len: %u, ip/fragment[%u, %u]",
292                   t->trace_range.first_bi, t->total_data_len,
293                   t->fragment_first, t->fragment_last);
294     }
295   switch (t->action)
296     {
297     case RANGE_NEW:
298       s = format (s, "\n%Unew %U", format_white_space, indent,
299                   format_ip6_full_reass_range_trace, &t->trace_range);
300       break;
301     case RANGE_DISCARD:
302       s = format (s, "\n%Udiscard %U", format_white_space, indent,
303                   format_ip6_full_reass_range_trace, &t->trace_range);
304       break;
305     case RANGE_OVERLAP:
306       s = format (s, "\n%Uoverlap %U", format_white_space, indent,
307                   format_ip6_full_reass_range_trace, &t->trace_range);
308       break;
309     case ICMP_ERROR_FL_TOO_BIG:
310       s = format (s, "\n%Uicmp-error - frag_len > 65535 %U",
311                   format_white_space, indent,
312                   format_ip6_full_reass_range_trace, &t->trace_range);
313       break;
314     case ICMP_ERROR_FL_NOT_MULT_8:
315       s = format (s, "\n%Uicmp-error - frag_len mod 8 != 0 %U",
316                   format_white_space, indent,
317                   format_ip6_full_reass_range_trace, &t->trace_range);
318       break;
319     case ICMP_ERROR_RT_EXCEEDED:
320       s = format (s, "\n%Uicmp-error - reassembly time exceeded",
321                   format_white_space, indent);
322       break;
323     case FINALIZE:
324       s = format (s, "\n%Ufinalize reassembly", format_white_space, indent);
325       break;
326     case HANDOFF:
327       s =
328         format (s, "handoff from thread #%u to thread #%u", t->thread_id,
329                 t->thread_id_to);
330       break;
331     case PASSTHROUGH:
332       s = format (s, "passthrough - not a fragment");
333       break;
334     }
335   return s;
336 }
337
338 static void
339 ip6_full_reass_add_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
340                           ip6_full_reass_t * reass, u32 bi,
341                           ip6_frag_hdr_t * ip6_frag_header,
342                           ip6_full_reass_trace_operation_e action,
343                           u32 thread_id_to)
344 {
345   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
346   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
347   bool is_after_handoff = false;
348   if (pool_is_free_index
349       (vm->trace_main.trace_buffer_pool, vlib_buffer_get_trace_index (b)))
350     {
351       // this buffer's trace is gone
352       b->flags &= ~VLIB_BUFFER_IS_TRACED;
353       return;
354     }
355   if (vlib_buffer_get_trace_thread (b) != vm->thread_index)
356     {
357       is_after_handoff = true;
358     }
359   ip6_full_reass_trace_t *t = vlib_add_trace (vm, node, b, sizeof (t[0]));
360   t->is_after_handoff = is_after_handoff;
361   if (t->is_after_handoff)
362     {
363       clib_memcpy (&t->ip6_header, vlib_buffer_get_current (b),
364                    clib_min (sizeof (t->ip6_header), b->current_length));
365       if (ip6_frag_header)
366         {
367           clib_memcpy (&t->ip6_frag_header, ip6_frag_header,
368                        sizeof (t->ip6_frag_header));
369         }
370       else
371         {
372           clib_memset (&t->ip6_frag_header, 0, sizeof (t->ip6_frag_header));
373         }
374     }
375   if (reass)
376     {
377       t->reass_id = reass->id;
378       t->op_id = reass->trace_op_counter;
379       t->trace_range.first_bi = reass->first_bi;
380       t->total_data_len = reass->data_len;
381       ++reass->trace_op_counter;
382     }
383   else
384     {
385       t->reass_id = ~0;
386     }
387   t->action = action;
388   t->thread_id = vm->thread_index;
389   t->thread_id_to = thread_id_to;
390   ip6_full_reass_trace_details (vm, bi, &t->trace_range);
391   t->fragment_first = vnb->ip.reass.fragment_first;
392   t->fragment_last = vnb->ip.reass.fragment_last;
393 #if 0
394   static u8 *s = NULL;
395   s = format (s, "%U", format_ip6_full_reass_trace, NULL, NULL, t);
396   printf ("%.*s\n", vec_len (s), s);
397   fflush (stdout);
398   vec_reset_length (s);
399 #endif
400 }
401
402 always_inline void
403 ip6_full_reass_free_ctx (ip6_full_reass_per_thread_t * rt,
404                          ip6_full_reass_t * reass)
405 {
406   pool_put (rt->pool, reass);
407   --rt->reass_n;
408 }
409
410 always_inline void
411 ip6_full_reass_free (ip6_full_reass_main_t * rm,
412                      ip6_full_reass_per_thread_t * rt,
413                      ip6_full_reass_t * reass)
414 {
415   clib_bihash_kv_48_8_t kv;
416   kv.key[0] = reass->key.as_u64[0];
417   kv.key[1] = reass->key.as_u64[1];
418   kv.key[2] = reass->key.as_u64[2];
419   kv.key[3] = reass->key.as_u64[3];
420   kv.key[4] = reass->key.as_u64[4];
421   kv.key[5] = reass->key.as_u64[5];
422   clib_bihash_add_del_48_8 (&rm->hash, &kv, 0);
423   ip6_full_reass_free_ctx (rt, reass);
424 }
425
426 /* n_left_to_next, and to_next are taken as input params, as this function
427  * could be called from a graphnode, where its managing local copy of these
428  * variables, and ignoring those and still trying to enqueue the buffers
429  * with local variables would cause either buffer leak or corruption */
430 always_inline void
431 ip6_full_reass_drop_all (vlib_main_t *vm, vlib_node_runtime_t *node,
432                          ip6_full_reass_t *reass, u32 *n_left_to_next,
433                          u32 **to_next)
434 {
435   u32 range_bi = reass->first_bi;
436   vlib_buffer_t *range_b;
437   vnet_buffer_opaque_t *range_vnb;
438   u32 *to_free = NULL;
439
440   while (~0 != range_bi)
441     {
442       range_b = vlib_get_buffer (vm, range_bi);
443       range_vnb = vnet_buffer (range_b);
444
445       if (~0 != range_bi)
446         {
447           vec_add1 (to_free, range_bi);
448         }
449       range_bi = range_vnb->ip.reass.next_range_bi;
450     }
451
452   /* send to next_error_index */
453   if (~0 != reass->error_next_index &&
454       reass->error_next_index < node->n_next_nodes)
455     {
456       u32 next_index;
457
458       next_index = reass->error_next_index;
459       u32 bi = ~0;
460
461       while (vec_len (to_free) > 0)
462         {
463           vlib_get_next_frame (vm, node, next_index, *to_next,
464                                (*n_left_to_next));
465
466           while (vec_len (to_free) > 0 && (*n_left_to_next) > 0)
467             {
468               bi = vec_pop (to_free);
469
470               if (~0 != bi)
471                 {
472                   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
473                   if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
474                     {
475                       ip6_full_reass_add_trace (vm, node, reass, bi, NULL,
476                                                 RANGE_DISCARD, ~0);
477                     }
478                   *to_next[0] = bi;
479                   (*to_next) += 1;
480                   (*n_left_to_next) -= 1;
481                 }
482             }
483           vlib_put_next_frame (vm, node, next_index, (*n_left_to_next));
484         }
485     }
486   else
487     {
488       vlib_buffer_free (vm, to_free, vec_len (to_free));
489     }
490   vec_free (to_free);
491 }
492
493 always_inline void
494 sanitize_reass_buffers_add_missing (vlib_main_t *vm, ip6_full_reass_t *reass,
495                                     u32 *bi0)
496 {
497   u32 range_bi = reass->first_bi;
498   vlib_buffer_t *range_b;
499   vnet_buffer_opaque_t *range_vnb;
500
501   while (~0 != range_bi)
502     {
503       range_b = vlib_get_buffer (vm, range_bi);
504       range_vnb = vnet_buffer (range_b);
505       u32 bi = range_bi;
506       if (~0 != bi)
507         {
508           if (bi == *bi0)
509             *bi0 = ~0;
510           if (range_b->flags & VLIB_BUFFER_NEXT_PRESENT)
511             {
512               u32 _bi = bi;
513               vlib_buffer_t *_b = vlib_get_buffer (vm, _bi);
514               while (_b->flags & VLIB_BUFFER_NEXT_PRESENT)
515                 {
516                   if (_b->next_buffer != range_vnb->ip.reass.next_range_bi)
517                     {
518                       _bi = _b->next_buffer;
519                       _b = vlib_get_buffer (vm, _bi);
520                     }
521                   else
522                     {
523                       _b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
524                       break;
525                     }
526                 }
527             }
528           range_bi = range_vnb->ip.reass.next_range_bi;
529         }
530     }
531   if (*bi0 != ~0)
532     {
533       vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
534       vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
535       if (~0 != reass->first_bi)
536         {
537           fvnb->ip.reass.next_range_bi = reass->first_bi;
538           reass->first_bi = *bi0;
539         }
540       else
541         {
542           reass->first_bi = *bi0;
543           fvnb->ip.reass.next_range_bi = ~0;
544         }
545       *bi0 = ~0;
546     }
547 }
548
549 always_inline void
550 ip6_full_reass_on_timeout (vlib_main_t *vm, vlib_node_runtime_t *node,
551                            ip6_full_reass_t *reass, u32 *icmp_bi,
552                            u32 *n_left_to_next, u32 **to_next)
553 {
554   if (~0 == reass->first_bi)
555     {
556       return;
557     }
558   if (~0 == reass->next_index)  // custom apps don't want icmp
559     {
560       vlib_buffer_t *b = vlib_get_buffer (vm, reass->first_bi);
561       if (0 == vnet_buffer (b)->ip.reass.fragment_first)
562         {
563           *icmp_bi = reass->first_bi;
564           if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
565             {
566               ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
567                                         ICMP_ERROR_RT_EXCEEDED, ~0);
568             }
569           // fragment with offset zero received - send icmp message back
570           if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
571             {
572               // separate first buffer from chain and steer it towards icmp node
573               b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
574               reass->first_bi = b->next_buffer;
575             }
576           else
577             {
578               reass->first_bi = vnet_buffer (b)->ip.reass.next_range_bi;
579             }
580           icmp6_error_set_vnet_buffer (b, ICMP6_time_exceeded,
581                                        ICMP6_time_exceeded_fragment_reassembly_time_exceeded,
582                                        0);
583         }
584     }
585   ip6_full_reass_drop_all (vm, node, reass, n_left_to_next, to_next);
586 }
587
588 always_inline ip6_full_reass_t *
589 ip6_full_reass_find_or_create (vlib_main_t *vm, vlib_node_runtime_t *node,
590                                ip6_full_reass_main_t *rm,
591                                ip6_full_reass_per_thread_t *rt,
592                                ip6_full_reass_kv_t *kv, u32 *icmp_bi,
593                                u8 *do_handoff, int skip_bihash,
594                                u32 *n_left_to_next, u32 **to_next)
595 {
596   ip6_full_reass_t *reass;
597   f64 now;
598
599 again:
600
601   reass = NULL;
602   now = vlib_time_now (vm);
603
604   if (!skip_bihash && !clib_bihash_search_48_8 (&rm->hash, &kv->kv, &kv->kv))
605     {
606       if (vm->thread_index != kv->v.memory_owner_thread_index)
607         {
608           *do_handoff = 1;
609           return NULL;
610         }
611
612       reass =
613         pool_elt_at_index (rm->per_thread_data
614                            [kv->v.memory_owner_thread_index].pool,
615                            kv->v.reass_index);
616
617       if (now > reass->last_heard + rm->timeout)
618         {
619           ip6_full_reass_on_timeout (vm, node, reass, icmp_bi, n_left_to_next,
620                                      to_next);
621           ip6_full_reass_free (rm, rt, reass);
622           reass = NULL;
623         }
624     }
625
626   if (reass)
627     {
628       reass->last_heard = now;
629       return reass;
630     }
631
632   if (rt->reass_n >= rm->max_reass_n)
633     {
634       reass = NULL;
635       return reass;
636     }
637   else
638     {
639       pool_get (rt->pool, reass);
640       clib_memset (reass, 0, sizeof (*reass));
641       reass->id = ((u64) vm->thread_index * 1000000000) + rt->id_counter;
642       ++rt->id_counter;
643       reass->first_bi = ~0;
644       reass->last_packet_octet = ~0;
645       reass->data_len = 0;
646       reass->next_index = ~0;
647       reass->error_next_index = ~0;
648       reass->memory_owner_thread_index = vm->thread_index;
649       ++rt->reass_n;
650     }
651
652   kv->v.reass_index = (reass - rt->pool);
653   kv->v.memory_owner_thread_index = vm->thread_index;
654   reass->last_heard = now;
655
656   if (!skip_bihash)
657     {
658       reass->key.as_u64[0] = kv->kv.key[0];
659       reass->key.as_u64[1] = kv->kv.key[1];
660       reass->key.as_u64[2] = kv->kv.key[2];
661       reass->key.as_u64[3] = kv->kv.key[3];
662       reass->key.as_u64[4] = kv->kv.key[4];
663       reass->key.as_u64[5] = kv->kv.key[5];
664
665       int rv = clib_bihash_add_del_48_8 (&rm->hash, &kv->kv, 2);
666       if (rv)
667         {
668           ip6_full_reass_free (rm, rt, reass);
669           reass = NULL;
670           // if other worker created a context already work with the other copy
671           if (-2 == rv)
672             goto again;
673         }
674     }
675   else
676     {
677       reass->key.as_u64[0] = ~0;
678       reass->key.as_u64[1] = ~0;
679       reass->key.as_u64[2] = ~0;
680       reass->key.as_u64[3] = ~0;
681       reass->key.as_u64[4] = ~0;
682       reass->key.as_u64[5] = ~0;
683     }
684
685   return reass;
686 }
687
688 always_inline ip6_full_reass_rc_t
689 ip6_full_reass_finalize (vlib_main_t * vm, vlib_node_runtime_t * node,
690                          ip6_full_reass_main_t * rm,
691                          ip6_full_reass_per_thread_t * rt,
692                          ip6_full_reass_t * reass, u32 * bi0, u32 * next0,
693                          u32 * error0, bool is_custom_app)
694 {
695   *bi0 = reass->first_bi;
696   *error0 = IP6_ERROR_NONE;
697   ip6_frag_hdr_t *frag_hdr;
698   vlib_buffer_t *last_b = NULL;
699   u32 sub_chain_bi = reass->first_bi;
700   u32 total_length = 0;
701   u32 buf_cnt = 0;
702   u32 dropped_cnt = 0;
703   u32 *vec_drop_compress = NULL;
704   ip6_full_reass_rc_t rv = IP6_FULL_REASS_RC_OK;
705   do
706     {
707       u32 tmp_bi = sub_chain_bi;
708       vlib_buffer_t *tmp = vlib_get_buffer (vm, tmp_bi);
709       vnet_buffer_opaque_t *vnb = vnet_buffer (tmp);
710       if (!(vnb->ip.reass.range_first >= vnb->ip.reass.fragment_first) &&
711           !(vnb->ip.reass.range_last > vnb->ip.reass.fragment_first))
712         {
713           rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
714           goto free_buffers_and_return;
715         }
716
717       u32 data_len = ip6_full_reass_buffer_get_data_len (tmp);
718       u32 trim_front = vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
719         sizeof (*frag_hdr) + ip6_full_reass_buffer_get_data_offset (tmp);
720       u32 trim_end =
721         vlib_buffer_length_in_chain (vm, tmp) - trim_front - data_len;
722       if (tmp_bi == reass->first_bi)
723         {
724           /* first buffer - keep ip6 header */
725           if (0 != ip6_full_reass_buffer_get_data_offset (tmp))
726             {
727               rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
728               goto free_buffers_and_return;
729             }
730           trim_front = 0;
731           trim_end = vlib_buffer_length_in_chain (vm, tmp) - data_len -
732             (vnet_buffer (tmp)->ip.reass.ip6_frag_hdr_offset +
733              sizeof (*frag_hdr));
734           if (!(vlib_buffer_length_in_chain (vm, tmp) - trim_end > 0))
735             {
736               rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
737               goto free_buffers_and_return;
738             }
739         }
740       u32 keep_data =
741         vlib_buffer_length_in_chain (vm, tmp) - trim_front - trim_end;
742       while (1)
743         {
744           ++buf_cnt;
745           if (trim_front)
746             {
747               if (trim_front > tmp->current_length)
748                 {
749                   /* drop whole buffer */
750                   if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
751                     {
752                       rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
753                       goto free_buffers_and_return;
754                     }
755                   trim_front -= tmp->current_length;
756                   vec_add1 (vec_drop_compress, tmp_bi);
757                   tmp->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
758                   tmp_bi = tmp->next_buffer;
759                   tmp = vlib_get_buffer (vm, tmp_bi);
760                   continue;
761                 }
762               else
763                 {
764                   vlib_buffer_advance (tmp, trim_front);
765                   trim_front = 0;
766                 }
767             }
768           if (keep_data)
769             {
770               if (last_b)
771                 {
772                   last_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
773                   last_b->next_buffer = tmp_bi;
774                 }
775               last_b = tmp;
776               if (keep_data <= tmp->current_length)
777                 {
778                   tmp->current_length = keep_data;
779                   keep_data = 0;
780                 }
781               else
782                 {
783                   keep_data -= tmp->current_length;
784                   if (!(tmp->flags & VLIB_BUFFER_NEXT_PRESENT))
785                     {
786                       rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
787                       goto free_buffers_and_return;
788                     }
789                 }
790               total_length += tmp->current_length;
791             }
792           else
793             {
794               if (reass->first_bi == tmp_bi)
795                 {
796                   rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
797                   goto free_buffers_and_return;
798                 }
799               vec_add1 (vec_drop_compress, tmp_bi);
800               ++dropped_cnt;
801             }
802           if (tmp->flags & VLIB_BUFFER_NEXT_PRESENT)
803             {
804               tmp_bi = tmp->next_buffer;
805               tmp = vlib_get_buffer (vm, tmp->next_buffer);
806             }
807           else
808             {
809               break;
810             }
811         }
812       sub_chain_bi =
813         vnet_buffer (vlib_get_buffer (vm, sub_chain_bi))->ip.
814         reass.next_range_bi;
815     }
816   while (~0 != sub_chain_bi);
817
818   if (!last_b)
819     {
820       rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
821       goto free_buffers_and_return;
822     }
823   last_b->flags &= ~VLIB_BUFFER_NEXT_PRESENT;
824   vlib_buffer_t *first_b = vlib_get_buffer (vm, reass->first_bi);
825   if (total_length < first_b->current_length)
826     {
827       rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
828       goto free_buffers_and_return;
829     }
830   total_length -= first_b->current_length;
831   first_b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
832   first_b->total_length_not_including_first_buffer = total_length;
833   // drop fragment header
834   vnet_buffer_opaque_t *first_b_vnb = vnet_buffer (first_b);
835   ip6_header_t *ip = vlib_buffer_get_current (first_b);
836   u16 ip6_frag_hdr_offset = first_b_vnb->ip.reass.ip6_frag_hdr_offset;
837   ip6_ext_hdr_chain_t hdr_chain;
838   ip6_ext_header_t *prev_hdr = 0;
839   int res = ip6_ext_header_walk (first_b, ip, IP_PROTOCOL_IPV6_FRAGMENTATION,
840                                  &hdr_chain);
841   if (res < 0 ||
842       (hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION))
843     {
844       rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
845       goto free_buffers_and_return;
846     }
847   frag_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res].offset);
848   if (res > 0)
849     {
850       prev_hdr = ip6_ext_next_header_offset (ip, hdr_chain.eh[res - 1].offset);
851       prev_hdr->next_hdr = frag_hdr->next_hdr;
852     }
853   else
854     {
855       ip->protocol = frag_hdr->next_hdr;
856     }
857   if (hdr_chain.eh[res].offset != ip6_frag_hdr_offset)
858     {
859       rv = IP6_FULL_REASS_RC_INTERNAL_ERROR;
860       goto free_buffers_and_return;
861     }
862   memmove (frag_hdr, (u8 *) frag_hdr + sizeof (*frag_hdr),
863            first_b->current_length - ip6_frag_hdr_offset -
864            sizeof (ip6_frag_hdr_t));
865   first_b->current_length -= sizeof (*frag_hdr);
866   ip->payload_length =
867     clib_host_to_net_u16 (total_length + first_b->current_length -
868                           sizeof (*ip));
869   if (!vlib_buffer_chain_linearize (vm, first_b))
870     {
871       rv = IP6_FULL_REASS_RC_NO_BUF;
872       goto free_buffers_and_return;
873     }
874   first_b->flags &= ~VLIB_BUFFER_EXT_HDR_VALID;
875   if (PREDICT_FALSE (first_b->flags & VLIB_BUFFER_IS_TRACED))
876     {
877       ip6_full_reass_add_trace (vm, node, reass, reass->first_bi, NULL,
878                                 FINALIZE, ~0);
879 #if 0
880       // following code does a hexdump of packet fragments to stdout ...
881       do
882         {
883           u32 bi = reass->first_bi;
884           u8 *s = NULL;
885           while (~0 != bi)
886             {
887               vlib_buffer_t *b = vlib_get_buffer (vm, bi);
888               s = format (s, "%u: %U\n", bi, format_hexdump,
889                           vlib_buffer_get_current (b), b->current_length);
890               if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
891                 {
892                   bi = b->next_buffer;
893                 }
894               else
895                 {
896                   break;
897                 }
898             }
899           printf ("%.*s\n", vec_len (s), s);
900           fflush (stdout);
901           vec_free (s);
902         }
903       while (0);
904 #endif
905     }
906   if (!is_custom_app)
907     {
908       *next0 = IP6_FULL_REASSEMBLY_NEXT_INPUT;
909     }
910   else
911     {
912       *next0 = reass->next_index;
913     }
914   vnet_buffer (first_b)->ip.reass.estimated_mtu = reass->min_fragment_length;
915   ip6_full_reass_free (rm, rt, reass);
916   reass = NULL;
917 free_buffers_and_return:
918   vlib_buffer_free (vm, vec_drop_compress, vec_len (vec_drop_compress));
919   vec_free (vec_drop_compress);
920   return rv;
921 }
922
923 always_inline void
924 ip6_full_reass_insert_range_in_chain (vlib_main_t * vm,
925                                       ip6_full_reass_t * reass,
926                                       u32 prev_range_bi, u32 new_next_bi)
927 {
928
929   vlib_buffer_t *new_next_b = vlib_get_buffer (vm, new_next_bi);
930   vnet_buffer_opaque_t *new_next_vnb = vnet_buffer (new_next_b);
931   if (~0 != prev_range_bi)
932     {
933       vlib_buffer_t *prev_b = vlib_get_buffer (vm, prev_range_bi);
934       vnet_buffer_opaque_t *prev_vnb = vnet_buffer (prev_b);
935       new_next_vnb->ip.reass.next_range_bi = prev_vnb->ip.reass.next_range_bi;
936       prev_vnb->ip.reass.next_range_bi = new_next_bi;
937     }
938   else
939     {
940       if (~0 != reass->first_bi)
941         {
942           new_next_vnb->ip.reass.next_range_bi = reass->first_bi;
943         }
944       reass->first_bi = new_next_bi;
945     }
946   reass->data_len += ip6_full_reass_buffer_get_data_len (new_next_b);
947 }
948
949 always_inline ip6_full_reass_rc_t
950 ip6_full_reass_update (vlib_main_t *vm, vlib_node_runtime_t *node,
951                        ip6_full_reass_main_t *rm,
952                        ip6_full_reass_per_thread_t *rt,
953                        ip6_full_reass_t *reass, u32 *bi0, u32 *next0,
954                        u32 *error0, ip6_frag_hdr_t *frag_hdr,
955                        bool is_custom_app, u32 *handoff_thread_idx,
956                        int skip_bihash)
957 {
958   int consumed = 0;
959   vlib_buffer_t *fb = vlib_get_buffer (vm, *bi0);
960   vnet_buffer_opaque_t *fvnb = vnet_buffer (fb);
961   if (is_custom_app)
962     {
963       reass->next_index = fvnb->ip.reass.next_index;    // store next_index before it's overwritten
964       reass->error_next_index = fvnb->ip.reass.error_next_index;        // store error_next_index before it is overwritten
965     }
966
967   fvnb->ip.reass.ip6_frag_hdr_offset =
968     (u8 *) frag_hdr - (u8 *) vlib_buffer_get_current (fb);
969   ip6_header_t *fip = vlib_buffer_get_current (fb);
970   if (fb->current_length < sizeof (*fip) ||
971       fvnb->ip.reass.ip6_frag_hdr_offset == 0 ||
972       fvnb->ip.reass.ip6_frag_hdr_offset >= fb->current_length)
973     {
974       return IP6_FULL_REASS_RC_INTERNAL_ERROR;
975     }
976
977   u32 fragment_first = fvnb->ip.reass.fragment_first =
978     ip6_frag_hdr_offset_bytes (frag_hdr);
979   u32 fragment_length =
980     vlib_buffer_length_in_chain (vm, fb) -
981     (fvnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
982   if (0 == fragment_length)
983     {
984       return IP6_FULL_REASS_RC_INVALID_FRAG_LEN;
985     }
986   u32 fragment_last = fvnb->ip.reass.fragment_last =
987     fragment_first + fragment_length - 1;
988   int more_fragments = ip6_frag_hdr_more (frag_hdr);
989   u32 candidate_range_bi = reass->first_bi;
990   u32 prev_range_bi = ~0;
991   fvnb->ip.reass.range_first = fragment_first;
992   fvnb->ip.reass.range_last = fragment_last;
993   fvnb->ip.reass.next_range_bi = ~0;
994   if (!more_fragments)
995     {
996       reass->last_packet_octet = fragment_last;
997     }
998   if (~0 == reass->first_bi)
999     {
1000       // starting a new reassembly
1001       ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi, *bi0);
1002       reass->min_fragment_length = clib_net_to_host_u16 (fip->payload_length);
1003       consumed = 1;
1004       reass->fragments_n = 1;
1005       goto check_if_done_maybe;
1006     }
1007   reass->min_fragment_length =
1008     clib_min (clib_net_to_host_u16 (fip->payload_length),
1009               fvnb->ip.reass.estimated_mtu);
1010   while (~0 != candidate_range_bi)
1011     {
1012       vlib_buffer_t *candidate_b = vlib_get_buffer (vm, candidate_range_bi);
1013       vnet_buffer_opaque_t *candidate_vnb = vnet_buffer (candidate_b);
1014       if (fragment_first > candidate_vnb->ip.reass.range_last)
1015         {
1016           // this fragments starts after candidate range
1017           prev_range_bi = candidate_range_bi;
1018           candidate_range_bi = candidate_vnb->ip.reass.next_range_bi;
1019           if (candidate_vnb->ip.reass.range_last < fragment_last &&
1020               ~0 == candidate_range_bi)
1021             {
1022               // special case - this fragment falls beyond all known ranges
1023               ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1024                                                     *bi0);
1025               consumed = 1;
1026               break;
1027             }
1028           continue;
1029         }
1030       if (fragment_last < candidate_vnb->ip.reass.range_first)
1031         {
1032           // this fragment ends before candidate range without any overlap
1033           ip6_full_reass_insert_range_in_chain (vm, reass, prev_range_bi,
1034                                                 *bi0);
1035           consumed = 1;
1036         }
1037       else if (fragment_first == candidate_vnb->ip.reass.range_first &&
1038                fragment_last == candidate_vnb->ip.reass.range_last)
1039         {
1040           // duplicate fragment - ignore
1041         }
1042       else
1043         {
1044           // overlapping fragment - not allowed by RFC 8200
1045           if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1046             {
1047               ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr,
1048                                         RANGE_OVERLAP, ~0);
1049             }
1050           return IP6_FULL_REASS_RC_OVERLAP;
1051         }
1052       break;
1053     }
1054   ++reass->fragments_n;
1055 check_if_done_maybe:
1056   if (consumed)
1057     {
1058       if (PREDICT_FALSE (fb->flags & VLIB_BUFFER_IS_TRACED))
1059         {
1060           ip6_full_reass_add_trace (vm, node, reass, *bi0, frag_hdr, RANGE_NEW,
1061                                     ~0);
1062         }
1063     }
1064   else if (skip_bihash)
1065     {
1066       // if this reassembly is not in bihash, then the packet must have been
1067       // consumed
1068       return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1069     }
1070   if (~0 != reass->last_packet_octet &&
1071       reass->data_len == reass->last_packet_octet + 1)
1072     {
1073       *handoff_thread_idx = reass->sendout_thread_index;
1074       int handoff =
1075         reass->memory_owner_thread_index != reass->sendout_thread_index;
1076       ip6_full_reass_rc_t rc =
1077         ip6_full_reass_finalize (vm, node, rm, rt, reass, bi0, next0, error0,
1078                                  is_custom_app);
1079       if (IP6_FULL_REASS_RC_OK == rc && handoff)
1080         {
1081           return IP6_FULL_REASS_RC_HANDOFF;
1082         }
1083       return rc;
1084     }
1085   else
1086     {
1087       if (skip_bihash)
1088         {
1089           // if this reassembly is not in bihash, it should've been an atomic
1090           // fragment and thus finalized
1091           return IP6_FULL_REASS_RC_INTERNAL_ERROR;
1092         }
1093       if (consumed)
1094         {
1095           *bi0 = ~0;
1096           if (reass->fragments_n > rm->max_reass_len)
1097             {
1098               return IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS;
1099             }
1100         }
1101       else
1102         {
1103           *next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1104           *error0 = IP6_ERROR_REASS_DUPLICATE_FRAGMENT;
1105         }
1106     }
1107   return IP6_FULL_REASS_RC_OK;
1108 }
1109
1110 always_inline bool
1111 ip6_full_reass_verify_upper_layer_present (vlib_node_runtime_t *node,
1112                                            vlib_buffer_t *b,
1113                                            ip6_ext_hdr_chain_t *hc)
1114 {
1115   int nh = hc->eh[hc->length - 1].protocol;
1116   /* Checking to see if it's a terminating header */
1117   if (ip6_ext_hdr (nh))
1118     {
1119       icmp6_error_set_vnet_buffer (
1120         b, ICMP6_parameter_problem,
1121         ICMP6_parameter_problem_first_fragment_has_incomplete_header_chain, 0);
1122       b->error = node->errors[IP6_ERROR_REASS_MISSING_UPPER];
1123       return false;
1124     }
1125   return true;
1126 }
1127
1128 always_inline bool
1129 ip6_full_reass_verify_fragment_multiple_8 (vlib_main_t * vm,
1130                                            vlib_buffer_t * b,
1131                                            ip6_frag_hdr_t * frag_hdr)
1132 {
1133   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1134   ip6_header_t *ip = vlib_buffer_get_current (b);
1135   int more_fragments = ip6_frag_hdr_more (frag_hdr);
1136   u32 fragment_length =
1137     vlib_buffer_length_in_chain (vm, b) -
1138     (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1139   if (more_fragments && 0 != fragment_length % 8)
1140     {
1141       icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1142                                    ICMP6_parameter_problem_erroneous_header_field,
1143                                    (u8 *) & ip->payload_length - (u8 *) ip);
1144       return false;
1145     }
1146   return true;
1147 }
1148
1149 always_inline bool
1150 ip6_full_reass_verify_packet_size_lt_64k (vlib_main_t * vm,
1151                                           vlib_buffer_t * b,
1152                                           ip6_frag_hdr_t * frag_hdr)
1153 {
1154   vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1155   u32 fragment_first = ip6_frag_hdr_offset_bytes (frag_hdr);
1156   u32 fragment_length =
1157     vlib_buffer_length_in_chain (vm, b) -
1158     (vnb->ip.reass.ip6_frag_hdr_offset + sizeof (*frag_hdr));
1159   if (fragment_first + fragment_length > 65535)
1160     {
1161       ip6_header_t *ip0 = vlib_buffer_get_current (b);
1162       icmp6_error_set_vnet_buffer (b, ICMP6_parameter_problem,
1163                                    ICMP6_parameter_problem_erroneous_header_field,
1164                                    (u8 *) & frag_hdr->fragment_offset_and_more
1165                                    - (u8 *) ip0);
1166       return false;
1167     }
1168   return true;
1169 }
1170
1171 always_inline uword
1172 ip6_full_reassembly_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1173                             vlib_frame_t *frame, bool is_feature,
1174                             bool is_custom_app, bool is_local)
1175 {
1176   u32 *from = vlib_frame_vector_args (frame);
1177   u32 n_left_from, n_left_to_next, *to_next, next_index;
1178   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1179   ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[vm->thread_index];
1180   clib_spinlock_lock (&rt->lock);
1181
1182   n_left_from = frame->n_vectors;
1183   next_index = node->cached_next_index;
1184   while (n_left_from > 0)
1185     {
1186       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1187
1188       while (n_left_from > 0 && n_left_to_next > 0)
1189         {
1190           u32 bi0;
1191           vlib_buffer_t *b0;
1192           u32 next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1193           u32 error0 = IP6_ERROR_NONE;
1194           u32 icmp_bi = ~0;
1195
1196           bi0 = from[0];
1197           b0 = vlib_get_buffer (vm, bi0);
1198
1199           ip6_header_t *ip0 = vlib_buffer_get_current (b0);
1200           ip6_frag_hdr_t *frag_hdr;
1201           ip6_ext_hdr_chain_t hdr_chain;
1202           int res = ip6_ext_header_walk (
1203             b0, ip0, IP_PROTOCOL_IPV6_FRAGMENTATION, &hdr_chain);
1204           if (res < 0 ||
1205               hdr_chain.eh[res].protocol != IP_PROTOCOL_IPV6_FRAGMENTATION)
1206             {
1207               // this is a mangled packet - no fragmentation
1208               next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1209               ip6_full_reass_add_trace (vm, node, NULL, bi0, NULL, PASSTHROUGH,
1210                                         ~0);
1211               goto skip_reass;
1212             }
1213           if (is_local && !rm->is_local_reass_enabled)
1214             {
1215               next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1216               goto skip_reass;
1217             }
1218           frag_hdr =
1219             ip6_ext_next_header_offset (ip0, hdr_chain.eh[res].offset);
1220           vnet_buffer (b0)->ip.reass.ip6_frag_hdr_offset =
1221             hdr_chain.eh[res].offset;
1222
1223           if (0 == ip6_frag_hdr_offset (frag_hdr))
1224             {
1225               // first fragment - verify upper-layer is present
1226               if (!ip6_full_reass_verify_upper_layer_present (node, b0,
1227                                                               &hdr_chain))
1228                 {
1229                   next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1230                   goto skip_reass;
1231                 }
1232             }
1233           if (!ip6_full_reass_verify_fragment_multiple_8 (vm, b0, frag_hdr) ||
1234               !ip6_full_reass_verify_packet_size_lt_64k (vm, b0, frag_hdr))
1235             {
1236               next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1237               goto skip_reass;
1238             }
1239
1240           int skip_bihash = 0;
1241           ip6_full_reass_kv_t kv;
1242           u8 do_handoff = 0;
1243
1244           if (0 == ip6_frag_hdr_offset (frag_hdr) &&
1245               !ip6_frag_hdr_more (frag_hdr))
1246             {
1247               // this is atomic fragment and needs to be processed separately
1248               skip_bihash = 1;
1249             }
1250           else
1251             {
1252               kv.k.as_u64[0] = ip0->src_address.as_u64[0];
1253               kv.k.as_u64[1] = ip0->src_address.as_u64[1];
1254               kv.k.as_u64[2] = ip0->dst_address.as_u64[0];
1255               kv.k.as_u64[3] = ip0->dst_address.as_u64[1];
1256               kv.k.as_u64[4] =
1257                 ((u64) vec_elt (ip6_main.fib_index_by_sw_if_index,
1258                                 vnet_buffer (b0)->sw_if_index[VLIB_RX]))
1259                   << 32 |
1260                 (u64) frag_hdr->identification;
1261               kv.k.as_u64[5] = ip0->protocol;
1262             }
1263
1264           ip6_full_reass_t *reass = ip6_full_reass_find_or_create (
1265             vm, node, rm, rt, &kv, &icmp_bi, &do_handoff, skip_bihash,
1266             &n_left_to_next, &to_next);
1267
1268           if (reass)
1269             {
1270               const u32 fragment_first = ip6_frag_hdr_offset (frag_hdr);
1271               if (0 == fragment_first)
1272                 {
1273                   reass->sendout_thread_index = vm->thread_index;
1274                 }
1275             }
1276           if (PREDICT_FALSE (do_handoff))
1277             {
1278               next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1279               vnet_buffer (b0)->ip.reass.owner_thread_index =
1280                 kv.v.memory_owner_thread_index;
1281             }
1282           else if (reass)
1283             {
1284               u32 handoff_thread_idx;
1285               u32 counter = ~0;
1286               switch (ip6_full_reass_update (
1287                 vm, node, rm, rt, reass, &bi0, &next0, &error0, frag_hdr,
1288                 is_custom_app, &handoff_thread_idx, skip_bihash))
1289                 {
1290                 case IP6_FULL_REASS_RC_OK:
1291                   /* nothing to do here */
1292                   break;
1293                 case IP6_FULL_REASS_RC_HANDOFF:
1294                   next0 = IP6_FULL_REASSEMBLY_NEXT_HANDOFF;
1295                   b0 = vlib_get_buffer (vm, bi0);
1296                   vnet_buffer (b0)->ip.reass.owner_thread_index =
1297                     handoff_thread_idx;
1298                   break;
1299                 case IP6_FULL_REASS_RC_TOO_MANY_FRAGMENTS:
1300                   counter = IP6_ERROR_REASS_FRAGMENT_CHAIN_TOO_LONG;
1301                   break;
1302                 case IP6_FULL_REASS_RC_NO_BUF:
1303                   counter = IP6_ERROR_REASS_NO_BUF;
1304                   break;
1305                 case IP6_FULL_REASS_RC_INVALID_FRAG_LEN:
1306                   counter = IP6_ERROR_REASS_INVALID_FRAG_LEN;
1307                   break;
1308                 case IP6_FULL_REASS_RC_OVERLAP:
1309                   counter = IP6_ERROR_REASS_OVERLAPPING_FRAGMENT;
1310                   break;
1311                 case IP6_FULL_REASS_RC_INTERNAL_ERROR:
1312                   counter = IP6_ERROR_REASS_INTERNAL_ERROR;
1313                   /* Sanitization is needed in internal error cases only, as
1314                    * the incoming packet is already dropped in other cases,
1315                    * also adding bi0 back to the reassembly list, fixes the
1316                    * leaking of buffers during internal errors.
1317                    *
1318                    * Also it doesnt make sense to send these buffers custom
1319                    * app, these fragments are with internal errors */
1320                   sanitize_reass_buffers_add_missing (vm, reass, &bi0);
1321                   reass->error_next_index = ~0;
1322                   break;
1323                 }
1324               if (~0 != counter)
1325                 {
1326                   vlib_node_increment_counter (vm, node->node_index, counter,
1327                                                1);
1328                   ip6_full_reass_drop_all (vm, node, reass, &n_left_to_next,
1329                                            &to_next);
1330                   ip6_full_reass_free (rm, rt, reass);
1331                   goto next_packet;
1332                   break;
1333                 }
1334             }
1335           else
1336             {
1337               if (is_feature)
1338                 {
1339                   next0 = IP6_FULL_REASSEMBLY_NEXT_DROP;
1340                 }
1341               else
1342                 {
1343                   vnet_buffer_opaque_t *fvnb = vnet_buffer (b0);
1344                   next0 = fvnb->ip.reass.error_next_index;
1345                 }
1346               error0 = IP6_ERROR_REASS_LIMIT_REACHED;
1347             }
1348
1349           if (~0 != bi0)
1350             {
1351             skip_reass:
1352               to_next[0] = bi0;
1353               to_next += 1;
1354               n_left_to_next -= 1;
1355
1356               /* bi0 might have been updated by reass_finalize, reload */
1357               b0 = vlib_get_buffer (vm, bi0);
1358               if (IP6_ERROR_NONE != error0)
1359                 {
1360                   b0->error = node->errors[error0];
1361                 }
1362
1363               if (next0 == IP6_FULL_REASSEMBLY_NEXT_HANDOFF)
1364                 {
1365                   if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
1366                     {
1367                       ip6_full_reass_add_trace (
1368                         vm, node, NULL, bi0, frag_hdr, HANDOFF,
1369                         vnet_buffer (b0)->ip.reass.owner_thread_index);
1370                     }
1371                 }
1372               else if (is_feature && IP6_ERROR_NONE == error0)
1373                 {
1374                   vnet_feature_next (&next0, b0);
1375                 }
1376               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1377                                                n_left_to_next, bi0, next0);
1378             }
1379
1380           if (~0 != icmp_bi)
1381             {
1382               next0 = IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR;
1383               to_next[0] = icmp_bi;
1384               to_next += 1;
1385               n_left_to_next -= 1;
1386               vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1387                                                n_left_to_next, icmp_bi,
1388                                                next0);
1389             }
1390         next_packet:
1391           from += 1;
1392           n_left_from -= 1;
1393         }
1394
1395       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1396     }
1397
1398   clib_spinlock_unlock (&rt->lock);
1399   return frame->n_vectors;
1400 }
1401
1402 static char *ip6_full_reassembly_error_strings[] = {
1403 #define _(sym, string) string,
1404   foreach_ip6_error
1405 #undef _
1406 };
1407
1408 VLIB_NODE_FN (ip6_full_reass_node) (vlib_main_t * vm,
1409                                     vlib_node_runtime_t * node,
1410                                     vlib_frame_t * frame)
1411 {
1412   return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1413                                      false /* is_custom_app */,
1414                                      false /* is_local */);
1415 }
1416
1417 VLIB_REGISTER_NODE (ip6_full_reass_node) = {
1418     .name = "ip6-full-reassembly",
1419     .vector_size = sizeof (u32),
1420     .format_trace = format_ip6_full_reass_trace,
1421     .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1422     .error_strings = ip6_full_reassembly_error_strings,
1423     .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1424     .next_nodes =
1425         {
1426                 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1427                 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1428                 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1429                 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reassembly-handoff",
1430         },
1431 };
1432
1433 VLIB_NODE_FN (ip6_local_full_reass_node)
1434 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1435 {
1436   return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1437                                      false /* is_custom_app */,
1438                                      true /* is_local */);
1439 }
1440
1441 VLIB_REGISTER_NODE (ip6_local_full_reass_node) = {
1442     .name = "ip6-local-full-reassembly",
1443     .vector_size = sizeof (u32),
1444     .format_trace = format_ip6_full_reass_trace,
1445     .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1446     .error_strings = ip6_full_reassembly_error_strings,
1447     .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1448     .next_nodes =
1449         {
1450                 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1451                 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1452                 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1453                 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-local-full-reassembly-handoff",
1454         },
1455 };
1456
1457 VLIB_NODE_FN (ip6_full_reass_node_feature) (vlib_main_t * vm,
1458                                             vlib_node_runtime_t * node,
1459                                             vlib_frame_t * frame)
1460 {
1461   return ip6_full_reassembly_inline (vm, node, frame, true /* is_feature */,
1462                                      false /* is_custom_app */,
1463                                      false /* is_local */);
1464 }
1465
1466 VLIB_REGISTER_NODE (ip6_full_reass_node_feature) = {
1467     .name = "ip6-full-reassembly-feature",
1468     .vector_size = sizeof (u32),
1469     .format_trace = format_ip6_full_reass_trace,
1470     .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1471     .error_strings = ip6_full_reassembly_error_strings,
1472     .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1473     .next_nodes =
1474         {
1475                 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1476                 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1477                 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1478                 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-feature-hoff",
1479         },
1480 };
1481
1482 VNET_FEATURE_INIT (ip6_full_reassembly_feature, static) = {
1483     .arc_name = "ip6-unicast",
1484     .node_name = "ip6-full-reassembly-feature",
1485     .runs_before = VNET_FEATURES ("ip6-lookup",
1486                                   "ipsec6-input-feature"),
1487     .runs_after = 0,
1488 };
1489
1490 VLIB_NODE_FN (ip6_full_reass_node_custom)
1491 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
1492 {
1493   return ip6_full_reassembly_inline (vm, node, frame, false /* is_feature */,
1494                                      true /* is_custom_app */,
1495                                      false /* is_local */);
1496 }
1497
1498 VLIB_REGISTER_NODE (ip6_full_reass_node_custom) = {
1499     .name = "ip6-full-reassembly-custom",
1500     .vector_size = sizeof (u32),
1501     .format_trace = format_ip6_full_reass_trace,
1502     .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1503     .error_strings = ip6_full_reassembly_error_strings,
1504     .n_next_nodes = IP6_FULL_REASSEMBLY_N_NEXT,
1505     .next_nodes =
1506         {
1507                 [IP6_FULL_REASSEMBLY_NEXT_INPUT] = "ip6-input",
1508                 [IP6_FULL_REASSEMBLY_NEXT_DROP] = "ip6-drop",
1509                 [IP6_FULL_REASSEMBLY_NEXT_ICMP_ERROR] = "ip6-icmp-error",
1510                 [IP6_FULL_REASSEMBLY_NEXT_HANDOFF] = "ip6-full-reass-custom-hoff",
1511         },
1512 };
1513
1514 #ifndef CLIB_MARCH_VARIANT
1515 static u32
1516 ip6_full_reass_get_nbuckets ()
1517 {
1518   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1519   u32 nbuckets;
1520   u8 i;
1521
1522   /* need more mem with more workers */
1523   nbuckets = (u32) (rm->max_reass_n * (vlib_num_workers () + 1) /
1524                     IP6_FULL_REASS_HT_LOAD_FACTOR);
1525
1526   for (i = 0; i < 31; i++)
1527     if ((1 << i) >= nbuckets)
1528       break;
1529   nbuckets = 1 << i;
1530
1531   return nbuckets;
1532 }
1533 #endif /* CLIB_MARCH_VARIANT */
1534
1535 typedef enum
1536 {
1537   IP6_EVENT_CONFIG_CHANGED = 1,
1538 } ip6_full_reass_event_t;
1539
1540 #ifndef CLIB_MARCH_VARIANT
1541 typedef struct
1542 {
1543   int failure;
1544   clib_bihash_48_8_t *new_hash;
1545 } ip6_rehash_cb_ctx;
1546
1547 static int
1548 ip6_rehash_cb (clib_bihash_kv_48_8_t * kv, void *_ctx)
1549 {
1550   ip6_rehash_cb_ctx *ctx = _ctx;
1551   if (clib_bihash_add_del_48_8 (ctx->new_hash, kv, 1))
1552     {
1553       ctx->failure = 1;
1554     }
1555   return (BIHASH_WALK_CONTINUE);
1556 }
1557
1558 static void
1559 ip6_full_reass_set_params (u32 timeout_ms, u32 max_reassemblies,
1560                            u32 max_reassembly_length,
1561                            u32 expire_walk_interval_ms)
1562 {
1563   ip6_full_reass_main.timeout_ms = timeout_ms;
1564   ip6_full_reass_main.timeout = (f64) timeout_ms / (f64) MSEC_PER_SEC;
1565   ip6_full_reass_main.max_reass_n = max_reassemblies;
1566   ip6_full_reass_main.max_reass_len = max_reassembly_length;
1567   ip6_full_reass_main.expire_walk_interval_ms = expire_walk_interval_ms;
1568 }
1569
1570 vnet_api_error_t
1571 ip6_full_reass_set (u32 timeout_ms, u32 max_reassemblies,
1572                     u32 max_reassembly_length, u32 expire_walk_interval_ms)
1573 {
1574   u32 old_nbuckets = ip6_full_reass_get_nbuckets ();
1575   ip6_full_reass_set_params (timeout_ms, max_reassemblies,
1576                              max_reassembly_length, expire_walk_interval_ms);
1577   vlib_process_signal_event (ip6_full_reass_main.vlib_main,
1578                              ip6_full_reass_main.ip6_full_reass_expire_node_idx,
1579                              IP6_EVENT_CONFIG_CHANGED, 0);
1580   u32 new_nbuckets = ip6_full_reass_get_nbuckets ();
1581   if (ip6_full_reass_main.max_reass_n > 0 && new_nbuckets > old_nbuckets)
1582     {
1583       clib_bihash_48_8_t new_hash;
1584       clib_memset (&new_hash, 0, sizeof (new_hash));
1585       ip6_rehash_cb_ctx ctx;
1586       ctx.failure = 0;
1587       ctx.new_hash = &new_hash;
1588       clib_bihash_init_48_8 (&new_hash, "ip6-full-reass", new_nbuckets,
1589                              new_nbuckets * 1024);
1590       clib_bihash_foreach_key_value_pair_48_8 (&ip6_full_reass_main.hash,
1591                                                ip6_rehash_cb, &ctx);
1592       if (ctx.failure)
1593         {
1594           clib_bihash_free_48_8 (&new_hash);
1595           return -1;
1596         }
1597       else
1598         {
1599           clib_bihash_free_48_8 (&ip6_full_reass_main.hash);
1600           clib_memcpy_fast (&ip6_full_reass_main.hash, &new_hash,
1601                             sizeof (ip6_full_reass_main.hash));
1602           clib_bihash_copied (&ip6_full_reass_main.hash, &new_hash);
1603         }
1604     }
1605   return 0;
1606 }
1607
1608 vnet_api_error_t
1609 ip6_full_reass_get (u32 * timeout_ms, u32 * max_reassemblies,
1610                     u32 * max_reassembly_length,
1611                     u32 * expire_walk_interval_ms)
1612 {
1613   *timeout_ms = ip6_full_reass_main.timeout_ms;
1614   *max_reassemblies = ip6_full_reass_main.max_reass_n;
1615   *max_reassembly_length = ip6_full_reass_main.max_reass_len;
1616   *expire_walk_interval_ms = ip6_full_reass_main.expire_walk_interval_ms;
1617   return 0;
1618 }
1619
1620 static clib_error_t *
1621 ip6_full_reass_init_function (vlib_main_t * vm)
1622 {
1623   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1624   clib_error_t *error = 0;
1625   u32 nbuckets;
1626   vlib_node_t *node;
1627
1628   rm->vlib_main = vm;
1629
1630   vec_validate (rm->per_thread_data, vlib_num_workers ());
1631   ip6_full_reass_per_thread_t *rt;
1632   vec_foreach (rt, rm->per_thread_data)
1633   {
1634     clib_spinlock_init (&rt->lock);
1635     pool_alloc (rt->pool, rm->max_reass_n);
1636   }
1637
1638   node = vlib_get_node_by_name (vm, (u8 *) "ip6-full-reassembly-expire-walk");
1639   ASSERT (node);
1640   rm->ip6_full_reass_expire_node_idx = node->index;
1641
1642   ip6_full_reass_set_params (IP6_FULL_REASS_TIMEOUT_DEFAULT_MS,
1643                              IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT,
1644                              IP6_FULL_REASS_MAX_REASSEMBLY_LENGTH_DEFAULT,
1645                              IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS);
1646
1647   nbuckets = ip6_full_reass_get_nbuckets ();
1648   clib_bihash_init_48_8 (&rm->hash, "ip6-full-reass", nbuckets,
1649                          nbuckets * 1024);
1650
1651   node = vlib_get_node_by_name (vm, (u8 *) "ip6-icmp-error");
1652   ASSERT (node);
1653   rm->ip6_icmp_error_idx = node->index;
1654
1655   if ((error = vlib_call_init_function (vm, ip_main_init)))
1656     return error;
1657   ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
1658                          ip6_local_full_reass_node.index);
1659   rm->is_local_reass_enabled = 1;
1660
1661   rm->fq_index = vlib_frame_queue_main_init (ip6_full_reass_node.index, 0);
1662   rm->fq_local_index =
1663     vlib_frame_queue_main_init (ip6_local_full_reass_node.index, 0);
1664   rm->fq_feature_index =
1665     vlib_frame_queue_main_init (ip6_full_reass_node_feature.index, 0);
1666   rm->fq_custom_index =
1667     vlib_frame_queue_main_init (ip6_full_reass_node_custom.index, 0);
1668
1669   rm->feature_use_refcount_per_intf = NULL;
1670   return error;
1671 }
1672
1673 VLIB_INIT_FUNCTION (ip6_full_reass_init_function);
1674 #endif /* CLIB_MARCH_VARIANT */
1675
1676 static uword
1677 ip6_full_reass_walk_expired (vlib_main_t *vm, vlib_node_runtime_t *node,
1678                              CLIB_UNUSED (vlib_frame_t *f))
1679 {
1680   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1681   uword event_type, *event_data = 0;
1682
1683   while (true)
1684     {
1685       vlib_process_wait_for_event_or_clock (vm,
1686                                             (f64) rm->expire_walk_interval_ms
1687                                             / (f64) MSEC_PER_SEC);
1688       event_type = vlib_process_get_events (vm, &event_data);
1689
1690       switch (event_type)
1691         {
1692         case ~0:
1693           /* no events => timeout */
1694           /* fallthrough */
1695         case IP6_EVENT_CONFIG_CHANGED:
1696           /* nothing to do here */
1697           break;
1698         default:
1699           clib_warning ("BUG: event type 0x%wx", event_type);
1700           break;
1701         }
1702       f64 now = vlib_time_now (vm);
1703
1704       ip6_full_reass_t *reass;
1705       int *pool_indexes_to_free = NULL;
1706
1707       uword thread_index = 0;
1708       int index;
1709       const uword nthreads = vlib_num_workers () + 1;
1710       u32 *vec_icmp_bi = NULL;
1711       u32 n_left_to_next, *to_next;
1712
1713       for (thread_index = 0; thread_index < nthreads; ++thread_index)
1714         {
1715           ip6_full_reass_per_thread_t *rt =
1716             &rm->per_thread_data[thread_index];
1717           clib_spinlock_lock (&rt->lock);
1718
1719           vec_reset_length (pool_indexes_to_free);
1720           /* Pace the number of timeouts handled per thread,to avoid barrier
1721            * sync issues in real world scenarios */
1722
1723           u32 beg = rt->last_id;
1724           /* to ensure we walk at least once per sec per context */
1725           u32 end = beg + (IP6_FULL_REASS_MAX_REASSEMBLIES_DEFAULT *
1726                              IP6_FULL_REASS_EXPIRE_WALK_INTERVAL_DEFAULT_MS /
1727                              MSEC_PER_SEC +
1728                            1);
1729           if (end > vec_len (rt->pool))
1730             {
1731               end = vec_len (rt->pool);
1732               rt->last_id = 0;
1733             }
1734           else
1735             {
1736               rt->last_id = end;
1737             }
1738
1739           pool_foreach_stepping_index (index, beg, end, rt->pool)
1740           {
1741             reass = pool_elt_at_index (rt->pool, index);
1742             if (now > reass->last_heard + rm->timeout)
1743               {
1744                 vec_add1 (pool_indexes_to_free, index);
1745               }
1746           }
1747
1748           int *i;
1749           vec_foreach (i, pool_indexes_to_free)
1750           {
1751             ip6_full_reass_t *reass = pool_elt_at_index (rt->pool, i[0]);
1752             u32 icmp_bi = ~0;
1753             ip6_full_reass_on_timeout (vm, node, reass, &icmp_bi,
1754                                        &n_left_to_next, &to_next);
1755             if (~0 != icmp_bi)
1756               vec_add1 (vec_icmp_bi, icmp_bi);
1757
1758             ip6_full_reass_free (rm, rt, reass);
1759           }
1760
1761           clib_spinlock_unlock (&rt->lock);
1762         }
1763
1764       while (vec_len (vec_icmp_bi) > 0)
1765         {
1766           vlib_frame_t *f =
1767             vlib_get_frame_to_node (vm, rm->ip6_icmp_error_idx);
1768           u32 *to_next = vlib_frame_vector_args (f);
1769           u32 n_left_to_next = VLIB_FRAME_SIZE - f->n_vectors;
1770           int trace_frame = 0;
1771           while (vec_len (vec_icmp_bi) > 0 && n_left_to_next > 0)
1772             {
1773               u32 bi = vec_pop (vec_icmp_bi);
1774               vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1775               if (PREDICT_FALSE (b->flags & VLIB_BUFFER_IS_TRACED))
1776                 trace_frame = 1;
1777               b->error = node->errors[IP6_ERROR_REASS_TIMEOUT];
1778               to_next[0] = bi;
1779               ++f->n_vectors;
1780               to_next += 1;
1781               n_left_to_next -= 1;
1782             }
1783           f->frame_flags |= (trace_frame * VLIB_FRAME_TRACE);
1784           vlib_put_frame_to_node (vm, rm->ip6_icmp_error_idx, f);
1785         }
1786
1787       vec_free (pool_indexes_to_free);
1788       vec_free (vec_icmp_bi);
1789       if (event_data)
1790         {
1791           vec_set_len (event_data, 0);
1792         }
1793     }
1794
1795   return 0;
1796 }
1797
1798 VLIB_REGISTER_NODE (ip6_full_reass_expire_node) = {
1799     .function = ip6_full_reass_walk_expired,
1800     .format_trace = format_ip6_full_reass_trace,
1801     .type = VLIB_NODE_TYPE_PROCESS,
1802     .name = "ip6-full-reassembly-expire-walk",
1803
1804     .n_errors = ARRAY_LEN (ip6_full_reassembly_error_strings),
1805     .error_strings = ip6_full_reassembly_error_strings,
1806
1807 };
1808
1809 static u8 *
1810 format_ip6_full_reass_key (u8 * s, va_list * args)
1811 {
1812   ip6_full_reass_key_t *key = va_arg (*args, ip6_full_reass_key_t *);
1813   s = format (s, "xx_id: %u, src: %U, dst: %U, frag_id: %u, proto: %u",
1814               key->xx_id, format_ip6_address, &key->src, format_ip6_address,
1815               &key->dst, clib_net_to_host_u16 (key->frag_id), key->proto);
1816   return s;
1817 }
1818
1819 static u8 *
1820 format_ip6_full_reass (u8 * s, va_list * args)
1821 {
1822   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
1823   ip6_full_reass_t *reass = va_arg (*args, ip6_full_reass_t *);
1824
1825   s = format (s, "ID: %lu, key: %U\n  first_bi: %u, data_len: %u, "
1826               "last_packet_octet: %u, trace_op_counter: %u\n",
1827               reass->id, format_ip6_full_reass_key, &reass->key,
1828               reass->first_bi, reass->data_len, reass->last_packet_octet,
1829               reass->trace_op_counter);
1830   u32 bi = reass->first_bi;
1831   u32 counter = 0;
1832   while (~0 != bi)
1833     {
1834       vlib_buffer_t *b = vlib_get_buffer (vm, bi);
1835       vnet_buffer_opaque_t *vnb = vnet_buffer (b);
1836       s = format (s, "  #%03u: range: [%u, %u], bi: %u, off: %d, len: %u, "
1837                   "fragment[%u, %u]\n",
1838                   counter, vnb->ip.reass.range_first,
1839                   vnb->ip.reass.range_last, bi,
1840                   ip6_full_reass_buffer_get_data_offset (b),
1841                   ip6_full_reass_buffer_get_data_len (b),
1842                   vnb->ip.reass.fragment_first, vnb->ip.reass.fragment_last);
1843       if (b->flags & VLIB_BUFFER_NEXT_PRESENT)
1844         {
1845           bi = b->next_buffer;
1846         }
1847       else
1848         {
1849           bi = ~0;
1850         }
1851     }
1852   return s;
1853 }
1854
1855 static clib_error_t *
1856 show_ip6_full_reass (vlib_main_t * vm, unformat_input_t * input,
1857                      CLIB_UNUSED (vlib_cli_command_t * lmd))
1858 {
1859   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1860
1861   vlib_cli_output (vm, "---------------------");
1862   vlib_cli_output (vm, "IP6 reassembly status");
1863   vlib_cli_output (vm, "---------------------");
1864   bool details = false;
1865   if (unformat (input, "details"))
1866     {
1867       details = true;
1868     }
1869
1870   u32 sum_reass_n = 0;
1871   u64 sum_buffers_n = 0;
1872   ip6_full_reass_t *reass;
1873   uword thread_index;
1874   const uword nthreads = vlib_num_workers () + 1;
1875   for (thread_index = 0; thread_index < nthreads; ++thread_index)
1876     {
1877       ip6_full_reass_per_thread_t *rt = &rm->per_thread_data[thread_index];
1878       clib_spinlock_lock (&rt->lock);
1879       if (details)
1880         {
1881           pool_foreach (reass, rt->pool) {
1882             vlib_cli_output (vm, "%U", format_ip6_full_reass, vm, reass);
1883           }
1884         }
1885       sum_reass_n += rt->reass_n;
1886       clib_spinlock_unlock (&rt->lock);
1887     }
1888   vlib_cli_output (vm, "---------------------");
1889   vlib_cli_output (vm, "Current IP6 reassemblies count: %lu\n",
1890                    (long unsigned) sum_reass_n);
1891   vlib_cli_output (vm,
1892                    "Maximum configured concurrent full IP6 reassemblies per worker-thread: %lu\n",
1893                    (long unsigned) rm->max_reass_n);
1894   vlib_cli_output (vm,
1895                    "Maximum configured amount of fragments "
1896                    "per full IP6 reassembly: %lu\n",
1897                    (long unsigned) rm->max_reass_len);
1898   vlib_cli_output (vm,
1899                    "Maximum configured full IP6 reassembly timeout: %lums\n",
1900                    (long unsigned) rm->timeout_ms);
1901   vlib_cli_output (vm,
1902                    "Maximum configured full IP6 reassembly expire walk interval: %lums\n",
1903                    (long unsigned) rm->expire_walk_interval_ms);
1904   vlib_cli_output (vm, "Buffers in use: %lu\n",
1905                    (long unsigned) sum_buffers_n);
1906   return 0;
1907 }
1908
1909 VLIB_CLI_COMMAND (show_ip6_full_reassembly_cmd, static) = {
1910     .path = "show ip6-full-reassembly",
1911     .short_help = "show ip6-full-reassembly [details]",
1912     .function = show_ip6_full_reass,
1913 };
1914
1915 #ifndef CLIB_MARCH_VARIANT
1916 vnet_api_error_t
1917 ip6_full_reass_enable_disable (u32 sw_if_index, u8 enable_disable)
1918 {
1919   return vnet_feature_enable_disable ("ip6-unicast",
1920                                       "ip6-full-reassembly-feature",
1921                                       sw_if_index, enable_disable, 0, 0);
1922 }
1923 #endif /* CLIB_MARCH_VARIANT */
1924
1925 #define foreach_ip6_full_reassembly_handoff_error                       \
1926 _(CONGESTION_DROP, "congestion drop")
1927
1928
1929 typedef enum
1930 {
1931 #define _(sym,str) IP6_FULL_REASSEMBLY_HANDOFF_ERROR_##sym,
1932   foreach_ip6_full_reassembly_handoff_error
1933 #undef _
1934     IP6_FULL_REASSEMBLY_HANDOFF_N_ERROR,
1935 } ip6_full_reassembly_handoff_error_t;
1936
1937 static char *ip6_full_reassembly_handoff_error_strings[] = {
1938 #define _(sym,string) string,
1939   foreach_ip6_full_reassembly_handoff_error
1940 #undef _
1941 };
1942
1943 typedef struct
1944 {
1945   u32 next_worker_index;
1946 } ip6_full_reassembly_handoff_trace_t;
1947
1948 static u8 *
1949 format_ip6_full_reassembly_handoff_trace (u8 * s, va_list * args)
1950 {
1951   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1952   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1953   ip6_full_reassembly_handoff_trace_t *t =
1954     va_arg (*args, ip6_full_reassembly_handoff_trace_t *);
1955
1956   s =
1957     format (s, "ip6-full-reassembly-handoff: next-worker %d",
1958             t->next_worker_index);
1959
1960   return s;
1961 }
1962
1963 always_inline uword
1964 ip6_full_reassembly_handoff_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
1965                                     vlib_frame_t *frame,
1966                                     ip6_full_reass_node_type_t type,
1967                                     bool is_local)
1968 {
1969   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
1970
1971   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1972   u32 n_enq, n_left_from, *from;
1973   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1974   u32 fq_index;
1975
1976   from = vlib_frame_vector_args (frame);
1977   n_left_from = frame->n_vectors;
1978   vlib_get_buffers (vm, from, bufs, n_left_from);
1979
1980   b = bufs;
1981   ti = thread_indices;
1982
1983   switch (type)
1984     {
1985     case NORMAL:
1986       if (is_local)
1987         {
1988           fq_index = rm->fq_local_index;
1989         }
1990       else
1991         {
1992           fq_index = rm->fq_index;
1993         }
1994       break;
1995     case FEATURE:
1996       fq_index = rm->fq_feature_index;
1997       break;
1998     case CUSTOM:
1999       fq_index = rm->fq_custom_index;
2000       break;
2001     default:
2002       clib_warning ("Unexpected `type' (%d)!", type);
2003       ASSERT (0);
2004     }
2005   while (n_left_from > 0)
2006     {
2007       ti[0] = vnet_buffer (b[0])->ip.reass.owner_thread_index;
2008
2009       if (PREDICT_FALSE
2010           ((node->flags & VLIB_NODE_FLAG_TRACE)
2011            && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
2012         {
2013           ip6_full_reassembly_handoff_trace_t *t =
2014             vlib_add_trace (vm, node, b[0], sizeof (*t));
2015           t->next_worker_index = ti[0];
2016         }
2017
2018       n_left_from -= 1;
2019       ti += 1;
2020       b += 1;
2021     }
2022   n_enq = vlib_buffer_enqueue_to_thread (vm, node, fq_index, from,
2023                                          thread_indices, frame->n_vectors, 1);
2024
2025   if (n_enq < frame->n_vectors)
2026     vlib_node_increment_counter (vm, node->node_index,
2027                                  IP6_FULL_REASSEMBLY_HANDOFF_ERROR_CONGESTION_DROP,
2028                                  frame->n_vectors - n_enq);
2029   return frame->n_vectors;
2030 }
2031
2032 VLIB_NODE_FN (ip6_full_reassembly_handoff_node) (vlib_main_t * vm,
2033                                                  vlib_node_runtime_t * node,
2034                                                  vlib_frame_t * frame)
2035 {
2036   return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
2037                                              false /* is_local */);
2038 }
2039
2040 VLIB_REGISTER_NODE (ip6_full_reassembly_handoff_node) = {
2041   .name = "ip6-full-reassembly-handoff",
2042   .vector_size = sizeof (u32),
2043   .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2044   .error_strings = ip6_full_reassembly_handoff_error_strings,
2045   .format_trace = format_ip6_full_reassembly_handoff_trace,
2046
2047   .n_next_nodes = 1,
2048
2049   .next_nodes = {
2050     [0] = "error-drop",
2051   },
2052 };
2053
2054 VLIB_NODE_FN (ip6_local_full_reassembly_handoff_node)
2055 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2056 {
2057   return ip6_full_reassembly_handoff_inline (vm, node, frame, NORMAL,
2058                                              true /* is_feature */);
2059 }
2060
2061 VLIB_REGISTER_NODE (ip6_local_full_reassembly_handoff_node) = {
2062   .name = "ip6-local-full-reassembly-handoff",
2063   .vector_size = sizeof (u32),
2064   .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2065   .error_strings = ip6_full_reassembly_handoff_error_strings,
2066   .format_trace = format_ip6_full_reassembly_handoff_trace,
2067
2068   .n_next_nodes = 1,
2069
2070   .next_nodes = {
2071     [0] = "error-drop",
2072   },
2073 };
2074
2075 VLIB_NODE_FN (ip6_full_reassembly_feature_handoff_node) (vlib_main_t * vm,
2076                                vlib_node_runtime_t * node, vlib_frame_t * frame)
2077 {
2078   return ip6_full_reassembly_handoff_inline (vm, node, frame, FEATURE,
2079                                              false /* is_local */);
2080 }
2081
2082 VLIB_REGISTER_NODE (ip6_full_reassembly_feature_handoff_node) = {
2083   .name = "ip6-full-reass-feature-hoff",
2084   .vector_size = sizeof (u32),
2085   .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2086   .error_strings = ip6_full_reassembly_handoff_error_strings,
2087   .format_trace = format_ip6_full_reassembly_handoff_trace,
2088
2089   .n_next_nodes = 1,
2090
2091   .next_nodes = {
2092     [0] = "error-drop",
2093   },
2094 };
2095
2096 VLIB_NODE_FN (ip6_full_reassembly_custom_handoff_node)
2097 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
2098 {
2099   return ip6_full_reassembly_handoff_inline (vm, node, frame, CUSTOM,
2100                                              false /* is_local */);
2101 }
2102
2103 VLIB_REGISTER_NODE (ip6_full_reassembly_custom_handoff_node) = {
2104   .name = "ip6-full-reass-custom-hoff",
2105   .vector_size = sizeof (u32),
2106   .n_errors = ARRAY_LEN(ip6_full_reassembly_handoff_error_strings),
2107   .error_strings = ip6_full_reassembly_handoff_error_strings,
2108   .format_trace = format_ip6_full_reassembly_handoff_trace,
2109
2110   .n_next_nodes = 1,
2111
2112   .next_nodes = {
2113     [0] = "error-drop",
2114   },
2115 };
2116
2117 #ifndef CLIB_MARCH_VARIANT
2118 int
2119 ip6_full_reass_enable_disable_with_refcnt (u32 sw_if_index, int is_enable)
2120 {
2121   ip6_full_reass_main_t *rm = &ip6_full_reass_main;
2122   vec_validate (rm->feature_use_refcount_per_intf, sw_if_index);
2123   if (is_enable)
2124     {
2125       if (!rm->feature_use_refcount_per_intf[sw_if_index])
2126         {
2127           ++rm->feature_use_refcount_per_intf[sw_if_index];
2128           return vnet_feature_enable_disable ("ip6-unicast",
2129                                               "ip6-full-reassembly-feature",
2130                                               sw_if_index, 1, 0, 0);
2131         }
2132       ++rm->feature_use_refcount_per_intf[sw_if_index];
2133     }
2134   else
2135     {
2136       --rm->feature_use_refcount_per_intf[sw_if_index];
2137       if (!rm->feature_use_refcount_per_intf[sw_if_index])
2138         return vnet_feature_enable_disable ("ip6-unicast",
2139                                             "ip6-full-reassembly-feature",
2140                                             sw_if_index, 0, 0, 0);
2141     }
2142   return -1;
2143 }
2144
2145 void
2146 ip6_local_full_reass_enable_disable (int enable)
2147 {
2148   if (enable)
2149     {
2150       if (!ip6_full_reass_main.is_local_reass_enabled)
2151         {
2152           ip6_full_reass_main.is_local_reass_enabled = 1;
2153           ip6_register_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION,
2154                                  ip6_local_full_reass_node.index);
2155         }
2156     }
2157   else
2158     {
2159       if (ip6_full_reass_main.is_local_reass_enabled)
2160         {
2161           ip6_full_reass_main.is_local_reass_enabled = 0;
2162           ip6_unregister_protocol (IP_PROTOCOL_IPV6_FRAGMENTATION);
2163         }
2164     }
2165 }
2166
2167 int
2168 ip6_local_full_reass_enabled ()
2169 {
2170   return ip6_full_reass_main.is_local_reass_enabled;
2171 }
2172
2173 #endif
2174
2175 /*
2176  * fd.io coding-style-patch-verification: ON
2177  *
2178  * Local Variables:
2179  * eval: (c-set-style "gnu")
2180  * End:
2181  */