MAP: Add API support for setting parameters.
[vpp.git] / src / plugins / map / map.c
1 /*
2  * map.c : MAP support
3  *
4  * Copyright (c) 2015 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/fib/ip6_fib.h>
20 #include <vnet/adj/adj.h>
21 #include <map/map_dpo.h>
22 #include <vppinfra/crc32.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include "map.h"
26
27 map_main_t map_main;
28
29 /*
30  * This code supports the following MAP modes:
31  *
32  * Algorithmic Shared IPv4 address (ea_bits_len > 0):
33  *   ea_bits_len + ip4_prefix > 32
34  *   psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
35  * Algorithmic Full IPv4 address (ea_bits_len > 0):
36  *   ea_bits_len + ip4_prefix = 32
37  *   psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
38  * Algorithmic IPv4 prefix (ea_bits_len > 0):
39  *   ea_bits_len + ip4_prefix < 32
40  *   psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
41  *
42  * Independent Shared IPv4 address (ea_bits_len = 0):
43  *   ip4_prefix = 32
44  *   psid_length > 0
45  *   Rule IPv6 address = 128, Rule PSID Set
46  * Independent Full IPv4 address (ea_bits_len = 0):
47  *   ip4_prefix = 32
48  *   psid_length = 0, ip6_prefix = 128
49  * Independent IPv4 prefix (ea_bits_len = 0):
50  *   ip4_prefix < 32
51  *   psid_length = 0, ip6_prefix = 128
52  *
53  */
54
55 /*
56  * This code supports MAP-T:
57  *
58  * With a DMR prefix length of 64 or 96 (RFC6052).
59  *
60  */
61
62
63
64 int
65 map_create_domain (ip4_address_t * ip4_prefix,
66                    u8 ip4_prefix_len,
67                    ip6_address_t * ip6_prefix,
68                    u8 ip6_prefix_len,
69                    ip6_address_t * ip6_src,
70                    u8 ip6_src_len,
71                    u8 ea_bits_len,
72                    u8 psid_offset,
73                    u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
74 {
75   u8 suffix_len, suffix_shift;
76   map_main_t *mm = &map_main;
77   dpo_id_t dpo_v4 = DPO_INVALID;
78   dpo_id_t dpo_v6 = DPO_INVALID;
79   map_domain_t *d;
80
81   /* Sanity check on the src prefix length */
82   if (flags & MAP_DOMAIN_TRANSLATION)
83     {
84       if (ip6_src_len != 96 && ip6_src_len != 64)
85         {
86           clib_warning ("MAP-T only supports prefix lengths of 64 and 96.");
87           return -1;
88         }
89     }
90   else
91     {
92       if (ip6_src_len != 128)
93         {
94           clib_warning
95             ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
96              "be 128).");
97           return -1;
98         }
99     }
100
101   /* How many, and which bits to grab from the IPv4 DA */
102   if (ip4_prefix_len + ea_bits_len < 32)
103     {
104       flags |= MAP_DOMAIN_PREFIX;
105       suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
106       suffix_len = ea_bits_len;
107     }
108   else
109     {
110       suffix_shift = 0;
111       suffix_len = 32 - ip4_prefix_len;
112     }
113
114   /* EA bits must be within the first 64 bits */
115   if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
116                           ip6_prefix_len + suffix_len + psid_length > 64))
117     {
118       clib_warning
119         ("Embedded Address bits must be within the first 64 bits of "
120          "the IPv6 prefix");
121       return -1;
122     }
123
124   /* Get domain index */
125   pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
126   clib_memset (d, 0, sizeof (*d));
127   *map_domain_index = d - mm->domains;
128
129   /* Init domain struct */
130   d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
131   d->ip4_prefix_len = ip4_prefix_len;
132   d->ip6_prefix = *ip6_prefix;
133   d->ip6_prefix_len = ip6_prefix_len;
134   d->ip6_src = *ip6_src;
135   d->ip6_src_len = ip6_src_len;
136   d->ea_bits_len = ea_bits_len;
137   d->psid_offset = psid_offset;
138   d->psid_length = psid_length;
139   d->mtu = mtu;
140   d->flags = flags;
141   d->suffix_shift = suffix_shift;
142   d->suffix_mask = (1 << suffix_len) - 1;
143
144   d->psid_shift = 16 - psid_length - psid_offset;
145   d->psid_mask = (1 << d->psid_length) - 1;
146   d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
147
148   /* MAP data-plane object */
149   if (d->flags & MAP_DOMAIN_TRANSLATION)
150     map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
151   else
152     map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
153
154   /* Create ip4 route */
155   fib_prefix_t pfx = {
156     .fp_proto = FIB_PROTOCOL_IP4,
157     .fp_len = d->ip4_prefix_len,
158     .fp_addr = {
159                 .ip4 = d->ip4_prefix,
160                 }
161     ,
162   };
163   fib_table_entry_special_dpo_add (0, &pfx,
164                                    FIB_SOURCE_MAP,
165                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
166   dpo_reset (&dpo_v4);
167
168   /*
169    * construct a DPO to use the v6 domain
170    */
171   if (d->flags & MAP_DOMAIN_TRANSLATION)
172     map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
173   else
174     map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
175
176   /*
177    * Multiple MAP domains may share same source IPv6 TEP. Which is just dandy.
178    * We are not tracking the sharing. So a v4 lookup to find the correct
179    * domain post decap/trnaslate is always done
180    *
181    * Create ip6 route. This is a reference counted add. If the prefix
182    * already exists and is MAP sourced, it is now MAP source n+1 times
183    * and will need to be removed n+1 times.
184    */
185   fib_prefix_t pfx6 = {
186     .fp_proto = FIB_PROTOCOL_IP6,
187     .fp_len = d->ip6_src_len,
188     .fp_addr.ip6 = d->ip6_src,
189   };
190
191   fib_table_entry_special_dpo_add (0, &pfx6,
192                                    FIB_SOURCE_MAP,
193                                    FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
194   dpo_reset (&dpo_v6);
195
196   /* Validate packet/byte counters */
197   map_domain_counter_lock (mm);
198   int i;
199   for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
200     {
201       vlib_validate_simple_counter (&mm->simple_domain_counters[i],
202                                     *map_domain_index);
203       vlib_zero_simple_counter (&mm->simple_domain_counters[i],
204                                 *map_domain_index);
205     }
206   for (i = 0; i < vec_len (mm->domain_counters); i++)
207     {
208       vlib_validate_combined_counter (&mm->domain_counters[i],
209                                       *map_domain_index);
210       vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
211     }
212   map_domain_counter_unlock (mm);
213
214   return 0;
215 }
216
217 /*
218  * map_delete_domain
219  */
220 int
221 map_delete_domain (u32 map_domain_index)
222 {
223   map_main_t *mm = &map_main;
224   map_domain_t *d;
225
226   if (pool_is_free_index (mm->domains, map_domain_index))
227     {
228       clib_warning ("MAP domain delete: domain does not exist: %d",
229                     map_domain_index);
230       return -1;
231     }
232
233   d = pool_elt_at_index (mm->domains, map_domain_index);
234
235   fib_prefix_t pfx = {
236     .fp_proto = FIB_PROTOCOL_IP4,
237     .fp_len = d->ip4_prefix_len,
238     .fp_addr = {
239                 .ip4 = d->ip4_prefix,
240                 }
241     ,
242   };
243   fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
244
245   fib_prefix_t pfx6 = {
246     .fp_proto = FIB_PROTOCOL_IP6,
247     .fp_len = d->ip6_src_len,
248     .fp_addr = {
249                 .ip6 = d->ip6_src,
250                 }
251     ,
252   };
253   fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
254
255   /* Deleting rules */
256   if (d->rules)
257     clib_mem_free (d->rules);
258
259   pool_put (mm->domains, d);
260
261   return 0;
262 }
263
264 int
265 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
266                   u8 is_add)
267 {
268   map_domain_t *d;
269   map_main_t *mm = &map_main;
270
271   if (pool_is_free_index (mm->domains, map_domain_index))
272     {
273       clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
274       return -1;
275     }
276   d = pool_elt_at_index (mm->domains, map_domain_index);
277
278   /* Rules are only used in 1:1 independent case */
279   if (d->ea_bits_len > 0)
280     return (-1);
281
282   if (!d->rules)
283     {
284       u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
285       d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
286       if (!d->rules)
287         return -1;
288       clib_memset (d->rules, 0, l);
289     }
290
291   if (psid >= (0x1 << d->psid_length))
292     {
293       clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
294                     0x1 << d->psid_length);
295       return -1;
296     }
297
298   if (is_add)
299     {
300       d->rules[psid] = *tep;
301     }
302   else
303     {
304       clib_memset (&d->rules[psid], 0, sizeof (ip6_address_t));
305     }
306   return 0;
307 }
308
309 #ifdef MAP_SKIP_IP6_LOOKUP
310 /**
311  * Pre-resolvd per-protocol global next-hops
312  */
313 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
314
315 static void
316 map_pre_resolve_init (map_main_pre_resolved_t * pr)
317 {
318   pr->fei = FIB_NODE_INDEX_INVALID;
319   fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
320 }
321
322 static u8 *
323 format_map_pre_resolve (u8 * s, va_list * ap)
324 {
325   map_main_pre_resolved_t *pr = va_arg (*ap, map_main_pre_resolved_t *);
326
327   if (FIB_NODE_INDEX_INVALID != pr->fei)
328     {
329       const fib_prefix_t *pfx;
330
331       pfx = fib_entry_get_prefix (pr->fei);
332
333       return (format (s, "%U (%u)",
334                       format_ip46_address, &pfx->fp_addr, IP46_TYPE_ANY,
335                       pr->dpo.dpoi_index));
336     }
337   else
338     {
339       return (format (s, "un-set"));
340     }
341 }
342
343
344 /**
345  * Function definition to inform the FIB node that its last lock has gone.
346  */
347 static void
348 map_last_lock_gone (fib_node_t * node)
349 {
350   /*
351    * The MAP is a root of the graph. As such
352    * it never has children and thus is never locked.
353    */
354   ASSERT (0);
355 }
356
357 static map_main_pre_resolved_t *
358 map_from_fib_node (fib_node_t * node)
359 {
360   ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
361   return ((map_main_pre_resolved_t *)
362           (((char *) node) -
363            STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
364 }
365
366 static void
367 map_stack (map_main_pre_resolved_t * pr)
368 {
369   const dpo_id_t *dpo;
370
371   dpo = fib_entry_contribute_ip_forwarding (pr->fei);
372
373   dpo_copy (&pr->dpo, dpo);
374 }
375
376 /**
377  * Function definition to backwalk a FIB node
378  */
379 static fib_node_back_walk_rc_t
380 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
381 {
382   map_stack (map_from_fib_node (node));
383
384   return (FIB_NODE_BACK_WALK_CONTINUE);
385 }
386
387 /**
388  * Function definition to get a FIB node from its index
389  */
390 static fib_node_t *
391 map_fib_node_get (fib_node_index_t index)
392 {
393   return (&pre_resolved[index].node);
394 }
395
396 /*
397  * Virtual function table registered by MPLS GRE tunnels
398  * for participation in the FIB object graph.
399  */
400 const static fib_node_vft_t map_vft = {
401   .fnv_get = map_fib_node_get,
402   .fnv_last_lock = map_last_lock_gone,
403   .fnv_back_walk = map_back_walk,
404 };
405
406 static void
407 map_fib_resolve (map_main_pre_resolved_t * pr,
408                  fib_protocol_t proto, u8 len, const ip46_address_t * addr)
409 {
410   fib_prefix_t pfx = {
411     .fp_proto = proto,
412     .fp_len = len,
413     .fp_addr = *addr,
414   };
415
416   pr->fei = fib_table_entry_special_add (0,     // default fib
417                                          &pfx,
418                                          FIB_SOURCE_RR, FIB_ENTRY_FLAG_NONE);
419   pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
420   map_stack (pr);
421 }
422
423 static void
424 map_fib_unresolve (map_main_pre_resolved_t * pr,
425                    fib_protocol_t proto, u8 len, const ip46_address_t * addr)
426 {
427   fib_prefix_t pfx = {
428     .fp_proto = proto,
429     .fp_len = len,
430     .fp_addr = *addr,
431   };
432
433   fib_entry_child_remove (pr->fei, pr->sibling);
434
435   fib_table_entry_special_remove (0,    // default fib
436                                   &pfx, FIB_SOURCE_RR);
437   dpo_reset (&pr->dpo);
438
439   pr->fei = FIB_NODE_INDEX_INVALID;
440   pr->sibling = FIB_NODE_INDEX_INVALID;
441 }
442
443 void
444 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, int is_del)
445 {
446   if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
447     {
448       ip46_address_t addr = {
449         .ip6 = *ip6,
450       };
451       if (is_del)
452         map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
453                            FIB_PROTOCOL_IP6, 128, &addr);
454       else
455         map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
456                          FIB_PROTOCOL_IP6, 128, &addr);
457     }
458   if (ip4 && (ip4->as_u32 != 0))
459     {
460       ip46_address_t addr = {
461         .ip4 = *ip4,
462       };
463       if (is_del)
464         map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
465                            FIB_PROTOCOL_IP4, 32, &addr);
466       else
467         map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
468                          FIB_PROTOCOL_IP4, 32, &addr);
469     }
470 }
471 #endif
472
473 static clib_error_t *
474 map_security_check_command_fn (vlib_main_t * vm,
475                                unformat_input_t * input,
476                                vlib_cli_command_t * cmd)
477 {
478   unformat_input_t _line_input, *line_input = &_line_input;
479   clib_error_t *error = NULL;
480   bool enable = false;
481   bool check_frag = false;
482   bool saw_enable = false;
483   bool saw_frag = false;
484
485   /* Get a line of input. */
486   if (!unformat_user (input, unformat_line_input, line_input))
487     return 0;
488
489   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
490     {
491       if (unformat (line_input, "enable"))
492         {
493           enable = false;
494           saw_enable = true;
495         }
496       else if (unformat (line_input, "disable"))
497         {
498           enable = true;
499           saw_enable = true;
500         }
501       else if (unformat (line_input, "fragments on"))
502         {
503           check_frag = true;
504           saw_frag = true;
505         }
506       else if (unformat (line_input, "fragments off"))
507         {
508           check_frag = false;
509           saw_frag = true;
510         }
511       else
512         {
513           error = clib_error_return (0, "unknown input `%U'",
514                                      format_unformat_error, line_input);
515           goto done;
516         }
517     }
518
519   if (!saw_enable)
520     {
521       error = clib_error_return (0,
522                                  "Must specify enable 'enable' or 'disable'");
523       goto done;
524     }
525
526   if (!saw_frag)
527     {
528       error = clib_error_return (0, "Must specify fragments 'on' or 'off'");
529       goto done;
530     }
531
532   map_param_set_security_check (enable, check_frag);
533
534 done:
535   unformat_free (line_input);
536
537   return error;
538 }
539
540
541 static clib_error_t *
542 map_add_domain_command_fn (vlib_main_t * vm,
543                            unformat_input_t * input, vlib_cli_command_t * cmd)
544 {
545   unformat_input_t _line_input, *line_input = &_line_input;
546   ip4_address_t ip4_prefix;
547   ip6_address_t ip6_prefix;
548   ip6_address_t ip6_src;
549   u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
550   u32 num_m_args = 0;
551   /* Optional arguments */
552   u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
553   u32 mtu = 0;
554   u8 flags = 0;
555   ip6_src_len = 128;
556   clib_error_t *error = NULL;
557
558   /* Get a line of input. */
559   if (!unformat_user (input, unformat_line_input, line_input))
560     return 0;
561
562   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
563     {
564       if (unformat
565           (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
566            &ip4_prefix_len))
567         num_m_args++;
568       else
569         if (unformat
570             (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
571              &ip6_prefix_len))
572         num_m_args++;
573       else
574         if (unformat
575             (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
576              &ip6_src_len))
577         num_m_args++;
578       else
579         if (unformat
580             (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
581         num_m_args++;
582       else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
583         num_m_args++;
584       else if (unformat (line_input, "psid-offset %d", &psid_offset))
585         num_m_args++;
586       else if (unformat (line_input, "psid-len %d", &psid_length))
587         num_m_args++;
588       else if (unformat (line_input, "mtu %d", &mtu))
589         num_m_args++;
590       else if (unformat (line_input, "map-t"))
591         flags |= MAP_DOMAIN_TRANSLATION;
592       else if (unformat (line_input, "rfc6052"))
593         flags |= (MAP_DOMAIN_TRANSLATION | MAP_DOMAIN_RFC6052);
594       else
595         {
596           error = clib_error_return (0, "unknown input `%U'",
597                                      format_unformat_error, line_input);
598           goto done;
599         }
600     }
601
602   if (num_m_args < 3)
603     {
604       error = clib_error_return (0, "mandatory argument(s) missing");
605       goto done;
606     }
607
608   map_create_domain (&ip4_prefix, ip4_prefix_len,
609                      &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
610                      ea_bits_len, psid_offset, psid_length, &map_domain_index,
611                      mtu, flags);
612
613 done:
614   unformat_free (line_input);
615
616   return error;
617 }
618
619 static clib_error_t *
620 map_del_domain_command_fn (vlib_main_t * vm,
621                            unformat_input_t * input, vlib_cli_command_t * cmd)
622 {
623   unformat_input_t _line_input, *line_input = &_line_input;
624   u32 num_m_args = 0;
625   u32 map_domain_index;
626   clib_error_t *error = NULL;
627
628   /* Get a line of input. */
629   if (!unformat_user (input, unformat_line_input, line_input))
630     return 0;
631
632   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
633     {
634       if (unformat (line_input, "index %d", &map_domain_index))
635         num_m_args++;
636       else
637         {
638           error = clib_error_return (0, "unknown input `%U'",
639                                      format_unformat_error, line_input);
640           goto done;
641         }
642     }
643
644   if (num_m_args != 1)
645     {
646       error = clib_error_return (0, "mandatory argument(s) missing");
647       goto done;
648     }
649
650   map_delete_domain (map_domain_index);
651
652 done:
653   unformat_free (line_input);
654
655   return error;
656 }
657
658 static clib_error_t *
659 map_add_rule_command_fn (vlib_main_t * vm,
660                          unformat_input_t * input, vlib_cli_command_t * cmd)
661 {
662   unformat_input_t _line_input, *line_input = &_line_input;
663   ip6_address_t tep;
664   u32 num_m_args = 0;
665   u32 psid = 0, map_domain_index;
666   clib_error_t *error = NULL;
667
668   /* Get a line of input. */
669   if (!unformat_user (input, unformat_line_input, line_input))
670     return 0;
671
672   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
673     {
674       if (unformat (line_input, "index %d", &map_domain_index))
675         num_m_args++;
676       else if (unformat (line_input, "psid %d", &psid))
677         num_m_args++;
678       else
679         if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
680         num_m_args++;
681       else
682         {
683           error = clib_error_return (0, "unknown input `%U'",
684                                      format_unformat_error, line_input);
685           goto done;
686         }
687     }
688
689   if (num_m_args != 3)
690     {
691       error = clib_error_return (0, "mandatory argument(s) missing");
692       goto done;
693     }
694
695   if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
696     {
697       error = clib_error_return (0, "Failing to add Mapping Rule");
698       goto done;
699     }
700
701 done:
702   unformat_free (line_input);
703
704   return error;
705 }
706
707 #if MAP_SKIP_IP6_LOOKUP
708 static clib_error_t *
709 map_pre_resolve_command_fn (vlib_main_t * vm,
710                             unformat_input_t * input,
711                             vlib_cli_command_t * cmd)
712 {
713   unformat_input_t _line_input, *line_input = &_line_input;
714   ip4_address_t ip4nh, *p_v4 = NULL;
715   ip6_address_t ip6nh, *p_v6 = NULL;
716   clib_error_t *error = NULL;
717   int is_del = 0;
718
719   clib_memset (&ip4nh, 0, sizeof (ip4nh));
720   clib_memset (&ip6nh, 0, sizeof (ip6nh));
721
722   /* Get a line of input. */
723   if (!unformat_user (input, unformat_line_input, line_input))
724     return 0;
725
726   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
727     {
728       if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
729         p_v4 = &ip4nh;
730       else
731         if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
732         p_v6 = &ip6nh;
733       else if (unformat (line_input, "del"))
734         is_del = 1;
735       else
736         {
737           error = clib_error_return (0, "unknown input `%U'",
738                                      format_unformat_error, line_input);
739           goto done;
740         }
741     }
742
743   map_pre_resolve (p_v4, p_v6, is_del);
744
745 done:
746   unformat_free (line_input);
747
748   return error;
749 }
750 #endif
751
752 static clib_error_t *
753 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
754                                           unformat_input_t * input,
755                                           vlib_cli_command_t * cmd)
756 {
757   unformat_input_t _line_input, *line_input = &_line_input;
758   ip4_address_t icmp_src_address;
759   ip4_address_t *p_icmp_addr = 0;
760   map_main_t *mm = &map_main;
761   clib_error_t *error = NULL;
762
763   mm->icmp4_src_address.as_u32 = 0;
764
765   /* Get a line of input. */
766   if (!unformat_user (input, unformat_line_input, line_input))
767     return 0;
768
769   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
770     {
771       if (unformat
772           (line_input, "%U", unformat_ip4_address, &icmp_src_address))
773         {
774           mm->icmp4_src_address = icmp_src_address;
775           p_icmp_addr = &icmp_src_address;
776         }
777       else
778         {
779           error = clib_error_return (0, "unknown input `%U'",
780                                      format_unformat_error, line_input);
781           goto done;
782         }
783     }
784
785   map_param_set_icmp (p_icmp_addr);
786
787 done:
788   unformat_free (line_input);
789
790   return error;
791 }
792
793 static clib_error_t *
794 map_icmp_unreachables_command_fn (vlib_main_t * vm,
795                                   unformat_input_t * input,
796                                   vlib_cli_command_t * cmd)
797 {
798   unformat_input_t _line_input, *line_input = &_line_input;
799   int num_m_args = 0;
800   clib_error_t *error = NULL;
801   bool enabled = false;
802
803   /* Get a line of input. */
804   if (!unformat_user (input, unformat_line_input, line_input))
805     return 0;
806
807   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
808     {
809       num_m_args++;
810       if (unformat (line_input, "on"))
811         enabled = true;
812       else if (unformat (line_input, "off"))
813         enabled = false;
814       else
815         {
816           error = clib_error_return (0, "unknown input `%U'",
817                                      format_unformat_error, line_input);
818           goto done;
819         }
820     }
821
822
823   if (num_m_args != 1)
824     error = clib_error_return (0, "mandatory argument(s) missing");
825
826
827   map_param_set_icmp6 (enabled);
828
829 done:
830   unformat_free (line_input);
831
832   return error;
833 }
834
835
836 static clib_error_t *
837 map_fragment_command_fn (vlib_main_t * vm,
838                          unformat_input_t * input, vlib_cli_command_t * cmd)
839 {
840   unformat_input_t _line_input, *line_input = &_line_input;
841   clib_error_t *error = NULL;
842   bool frag_inner = false;
843   bool frag_ignore_df = false;
844   bool saw_in_out = false;
845   bool saw_df = false;
846
847   /* Get a line of input. */
848   if (!unformat_user (input, unformat_line_input, line_input))
849     return 0;
850
851   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
852     {
853       if (unformat (line_input, "inner"))
854         {
855           frag_inner = true;
856           saw_in_out = true;
857         }
858       else if (unformat (line_input, "outer"))
859         {
860           frag_inner = false;
861           saw_in_out = true;
862         }
863       else if (unformat (line_input, "ignore-df"))
864         {
865           frag_ignore_df = true;
866           saw_df = true;
867         }
868       else if (unformat (line_input, "honor-df"))
869         {
870           frag_ignore_df = false;
871           saw_df = true;
872         }
873       else
874         {
875           error = clib_error_return (0, "unknown input `%U'",
876                                      format_unformat_error, line_input);
877           goto done;
878         }
879     }
880
881   if (!saw_in_out)
882     {
883       error = clib_error_return (0, "Must specify 'inner' or 'outer'");
884       goto done;
885     }
886
887   if (!saw_df)
888     {
889       error = clib_error_return (0, "Must specify 'ignore-df' or 'honor-df'");
890       goto done;
891     }
892
893   map_param_set_fragmentation (frag_inner, frag_ignore_df);
894
895 done:
896   unformat_free (line_input);
897
898   return error;
899 }
900
901 static clib_error_t *
902 map_traffic_class_command_fn (vlib_main_t * vm,
903                               unformat_input_t * input,
904                               vlib_cli_command_t * cmd)
905 {
906   unformat_input_t _line_input, *line_input = &_line_input;
907   u32 tc = 0;
908   clib_error_t *error = NULL;
909   bool tc_copy = false;
910
911
912   /* Get a line of input. */
913   if (!unformat_user (input, unformat_line_input, line_input))
914     return 0;
915
916   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
917     {
918       if (unformat (line_input, "copy"))
919         tc_copy = true;
920       else if (unformat (line_input, "%x", &tc))
921         tc = tc & 0xff;
922       else
923         {
924           error = clib_error_return (0, "unknown input `%U'",
925                                      format_unformat_error, line_input);
926           goto done;
927         }
928     }
929
930   map_param_set_traffic_class (tc_copy, tc);
931
932 done:
933   unformat_free (line_input);
934
935   return error;
936 }
937
938 static char *
939 map_flags_to_string (u32 flags)
940 {
941   if (flags & MAP_DOMAIN_RFC6052)
942     return "rfc6052";
943   if (flags & MAP_DOMAIN_PREFIX)
944     return "prefix";
945   if (flags & MAP_DOMAIN_TRANSLATION)
946     return "map-t";
947   return "";
948 }
949
950 static u8 *
951 format_map_domain (u8 * s, va_list * args)
952 {
953   map_domain_t *d = va_arg (*args, map_domain_t *);
954   bool counters = va_arg (*args, int);
955   map_main_t *mm = &map_main;
956   ip6_address_t ip6_prefix;
957
958   if (d->rules)
959     clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
960   else
961     ip6_prefix = d->ip6_prefix;
962
963   s = format (s,
964               "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea-bits-len %d "
965               "psid-offset %d psid-len %d mtu %d %s",
966               d - mm->domains,
967               format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
968               format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
969               format_ip6_address, &d->ip6_src, d->ip6_src_len,
970               d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
971               map_flags_to_string (d->flags));
972
973   if (counters)
974     {
975       map_domain_counter_lock (mm);
976       vlib_counter_t v;
977       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
978                                  d - mm->domains, &v);
979       s = format (s, "  TX: %lld/%lld", v.packets, v.bytes);
980       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
981                                  d - mm->domains, &v);
982       s = format (s, "  RX: %lld/%lld", v.packets, v.bytes);
983       map_domain_counter_unlock (mm);
984     }
985   s = format (s, "\n");
986
987   if (d->rules)
988     {
989       int i;
990       ip6_address_t dst;
991       for (i = 0; i < (0x1 << d->psid_length); i++)
992         {
993           dst = d->rules[i];
994           if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
995             continue;
996           s = format (s,
997                       " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
998                       &dst);
999         }
1000     }
1001   return s;
1002 }
1003
1004 static u8 *
1005 format_map_ip4_reass (u8 * s, va_list * args)
1006 {
1007   map_main_t *mm = &map_main;
1008   map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
1009   map_ip4_reass_key_t *k = &r->key;
1010   f64 now = vlib_time_now (mm->vlib_main);
1011   f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
1012   f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1013   s = format (s,
1014               "ip4-reass src=%U  dst=%U  protocol=%d  identifier=%d  port=%d  lifetime=%.3lf\n",
1015               format_ip4_address, &k->src.as_u8, format_ip4_address,
1016               &k->dst.as_u8, k->protocol,
1017               clib_net_to_host_u16 (k->fragment_id),
1018               (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
1019   return s;
1020 }
1021
1022 static u8 *
1023 format_map_ip6_reass (u8 * s, va_list * args)
1024 {
1025   map_main_t *mm = &map_main;
1026   map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
1027   map_ip6_reass_key_t *k = &r->key;
1028   f64 now = vlib_time_now (mm->vlib_main);
1029   f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
1030   f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1031   s = format (s,
1032               "ip6-reass src=%U  dst=%U  protocol=%d  identifier=%d  lifetime=%.3lf\n",
1033               format_ip6_address, &k->src.as_u8, format_ip6_address,
1034               &k->dst.as_u8, k->protocol,
1035               clib_net_to_host_u32 (k->fragment_id), dt);
1036   return s;
1037 }
1038
1039 static clib_error_t *
1040 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1041                             vlib_cli_command_t * cmd)
1042 {
1043   unformat_input_t _line_input, *line_input = &_line_input;
1044   map_main_t *mm = &map_main;
1045   map_domain_t *d;
1046   bool counters = false;
1047   u32 map_domain_index = ~0;
1048   clib_error_t *error = NULL;
1049
1050   /* Get a line of input. */
1051   if (!unformat_user (input, unformat_line_input, line_input))
1052     return 0;
1053
1054   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1055     {
1056       if (unformat (line_input, "counters"))
1057         counters = true;
1058       else if (unformat (line_input, "index %d", &map_domain_index))
1059         ;
1060       else
1061         {
1062           error = clib_error_return (0, "unknown input `%U'",
1063                                      format_unformat_error, line_input);
1064           goto done;
1065         }
1066     }
1067
1068   if (pool_elts (mm->domains) == 0)
1069     vlib_cli_output (vm, "No MAP domains are configured...");
1070
1071   if (map_domain_index == ~0)
1072     {
1073     /* *INDENT-OFF* */
1074     pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1075     /* *INDENT-ON* */
1076     }
1077   else
1078     {
1079       if (pool_is_free_index (mm->domains, map_domain_index))
1080         {
1081           error = clib_error_return (0, "MAP domain does not exists %d",
1082                                      map_domain_index);
1083           goto done;
1084         }
1085
1086       d = pool_elt_at_index (mm->domains, map_domain_index);
1087       vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1088     }
1089
1090 done:
1091   unformat_free (line_input);
1092
1093   return error;
1094 }
1095
1096 static clib_error_t *
1097 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1098                                vlib_cli_command_t * cmd)
1099 {
1100   map_main_t *mm = &map_main;
1101   map_ip4_reass_t *f4;
1102   map_ip6_reass_t *f6;
1103
1104   /* *INDENT-OFF* */
1105   pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
1106   /* *INDENT-ON* */
1107   /* *INDENT-OFF* */
1108   pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
1109   /* *INDENT-ON* */
1110   return (0);
1111 }
1112
1113 u64
1114 map_error_counter_get (u32 node_index, map_error_t map_error)
1115 {
1116   vlib_main_t *vm = vlib_get_main ();
1117   vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1118   vlib_error_main_t *em = &vm->error_main;
1119   vlib_error_t e = error_node->errors[map_error];
1120   vlib_node_t *n = vlib_get_node (vm, node_index);
1121   u32 ci;
1122
1123   ci = vlib_error_get_code (e);
1124   ASSERT (ci < n->n_errors);
1125   ci += n->error_heap_index;
1126
1127   return (em->counters[ci]);
1128 }
1129
1130 static clib_error_t *
1131 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1132                            vlib_cli_command_t * cmd)
1133 {
1134   map_main_t *mm = &map_main;
1135   map_domain_t *d;
1136   int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1137   if (pool_elts (mm->domains) == 0)
1138     {
1139       vlib_cli_output (vm, "No MAP domains are configured...");
1140       return 0;
1141     }
1142
1143   /* *INDENT-OFF* */
1144   pool_foreach(d, mm->domains, ({
1145     if (d->rules) {
1146       rulecount+= 0x1 << d->psid_length;
1147       rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1148     }
1149     domains += sizeof(*d);
1150     domaincount++;
1151   }));
1152   /* *INDENT-ON* */
1153
1154   vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1155   vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1156   vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1157   vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1158
1159 #if MAP_SKIP_IP6_LOOKUP
1160   vlib_cli_output (vm,
1161                    "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1162                    format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1163                    format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1164
1165 #endif
1166
1167   if (mm->tc_copy)
1168     vlib_cli_output (vm, "MAP traffic-class: copy");
1169   else
1170     vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1171
1172   vlib_cli_output (vm,
1173                    "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1174                    mm->sec_check ? "enabled" : "disabled",
1175                    mm->sec_check_frag ? "enabled" : "disabled");
1176
1177   vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1178                    format_ip4_address, &mm->icmp4_src_address);
1179   vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1180                    mm->icmp6_enabled ? "enabled" : "disabled");
1181   vlib_cli_output (vm, "Inner fragmentation: %s\n",
1182                    mm->frag_inner ? "enabled" : "disabled");
1183   vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1184                    mm->frag_ignore_df ? "enabled" : "disabled");
1185
1186   /*
1187    * Counters
1188    */
1189   vlib_combined_counter_main_t *cm = mm->domain_counters;
1190   u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1191   u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1192   int which, i;
1193   vlib_counter_t v;
1194
1195   clib_memset (total_pkts, 0, sizeof (total_pkts));
1196   clib_memset (total_bytes, 0, sizeof (total_bytes));
1197
1198   map_domain_counter_lock (mm);
1199   vec_foreach (cm, mm->domain_counters)
1200   {
1201     which = cm - mm->domain_counters;
1202
1203     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1204       {
1205         vlib_get_combined_counter (cm, i, &v);
1206         total_pkts[which] += v.packets;
1207         total_bytes[which] += v.bytes;
1208       }
1209   }
1210   map_domain_counter_unlock (mm);
1211
1212   vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1213                    total_pkts[MAP_DOMAIN_COUNTER_TX],
1214                    total_bytes[MAP_DOMAIN_COUNTER_TX]);
1215   vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1216                    total_pkts[MAP_DOMAIN_COUNTER_RX],
1217                    total_bytes[MAP_DOMAIN_COUNTER_RX]);
1218
1219   vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1220                    vlib_get_simple_counter (&mm->icmp_relayed, 0));
1221
1222   return 0;
1223 }
1224
1225 static clib_error_t *
1226 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1227                              vlib_cli_command_t * cmd)
1228 {
1229   unformat_input_t _line_input, *line_input = &_line_input;
1230   u32 lifetime = ~0;
1231   f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1232   u32 pool_size = ~0;
1233   u64 buffers = ~(0ull);
1234   u8 ip4 = 0, ip6 = 0;
1235
1236   if (!unformat_user (input, unformat_line_input, line_input))
1237     return 0;
1238
1239   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1240     {
1241       if (unformat (line_input, "lifetime %u", &lifetime))
1242         ;
1243       else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1244         ;
1245       else if (unformat (line_input, "pool-size %u", &pool_size))
1246         ;
1247       else if (unformat (line_input, "buffers %llu", &buffers))
1248         ;
1249       else if (unformat (line_input, "ip4"))
1250         ip4 = 1;
1251       else if (unformat (line_input, "ip6"))
1252         ip6 = 1;
1253       else
1254         {
1255           unformat_free (line_input);
1256           return clib_error_return (0, "invalid input");
1257         }
1258     }
1259   unformat_free (line_input);
1260
1261   if (!ip4 && !ip6)
1262     return clib_error_return (0, "must specify ip4 and/or ip6");
1263
1264   if (ip4)
1265     {
1266       if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1267         return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1268                                   MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1269       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1270           && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1271         return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1272                                   MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1273       if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1274         return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1275                                   MAP_IP4_REASS_CONF_LIFETIME_MAX);
1276       if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1277         return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1278                                   MAP_IP4_REASS_CONF_BUFFERS_MAX);
1279     }
1280
1281   if (ip6)
1282     {
1283       if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1284         return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1285                                   MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1286       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1287           && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1288         return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1289                                   MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1290       if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1291         return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1292                                   MAP_IP6_REASS_CONF_LIFETIME_MAX);
1293       if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1294         return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1295                                   MAP_IP6_REASS_CONF_BUFFERS_MAX);
1296     }
1297
1298   int rv;
1299   u32 reass = 0, packets = 0;
1300   rv = map_param_set_reassembly (!ip4, lifetime, pool_size, buffers, ht_ratio,
1301                                  &reass, &packets);
1302
1303   switch (rv)
1304     {
1305     case 0:
1306       vlib_cli_output (vm,
1307                        "Note: destroyed-reassembly=%u , dropped-fragments=%u",
1308                        reass, packets);
1309       break;
1310
1311     case MAP_ERR_BAD_POOL_SIZE:
1312       return clib_error_return (0, "Could not set reass pool-size");
1313
1314     case MAP_ERR_BAD_HT_RATIO:
1315       return clib_error_return (0, "Could not set reass ht-log2len");
1316
1317     case MAP_ERR_BAD_LIFETIME:
1318       return clib_error_return (0, "Could not set ip6-reass lifetime");
1319
1320     case MAP_ERR_BAD_BUFFERS:
1321       return clib_error_return (0, "Could not set ip6-reass buffers");
1322
1323     case MAP_ERR_BAD_BUFFERS_TOO_LARGE:
1324       return clib_error_return (0,
1325                                 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1326     }
1327
1328   return 0;
1329 }
1330
1331
1332 /*
1333  * packet trace format function
1334  */
1335 u8 *
1336 format_map_trace (u8 * s, va_list * args)
1337 {
1338   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1339   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1340   map_trace_t *t = va_arg (*args, map_trace_t *);
1341   u32 map_domain_index = t->map_domain_index;
1342   u16 port = t->port;
1343
1344   s =
1345     format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1346             clib_net_to_host_u16 (port));
1347
1348   return s;
1349 }
1350
1351 static_always_inline map_ip4_reass_t *
1352 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1353 {
1354   map_main_t *mm = &map_main;
1355   u32 ri = mm->ip4_reass_hash_table[bucket];
1356   while (ri != MAP_REASS_INDEX_NONE)
1357     {
1358       map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1359       if (r->key.as_u64[0] == k->as_u64[0] &&
1360           r->key.as_u64[1] == k->as_u64[1] &&
1361           now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1362         {
1363           return r;
1364         }
1365       ri = r->bucket_next;
1366     }
1367   return NULL;
1368 }
1369
1370 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1371
1372 void
1373 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1374 {
1375   map_main_t *mm = &map_main;
1376   map_ip4_reass_get_fragments (r, pi_to_drop);
1377
1378   // Unlink in hash bucket
1379   map_ip4_reass_t *r2 = NULL;
1380   u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1381   while (r2i != map_ip4_reass_pool_index (r))
1382     {
1383       ASSERT (r2i != MAP_REASS_INDEX_NONE);
1384       r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1385       r2i = r2->bucket_next;
1386     }
1387   if (r2)
1388     {
1389       r2->bucket_next = r->bucket_next;
1390     }
1391   else
1392     {
1393       mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1394     }
1395
1396   // Unlink in list
1397   if (r->fifo_next == map_ip4_reass_pool_index (r))
1398     {
1399       mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1400     }
1401   else
1402     {
1403       if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1404         mm->ip4_reass_fifo_last = r->fifo_prev;
1405       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1406         r->fifo_next;
1407       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1408         r->fifo_prev;
1409     }
1410
1411   pool_put (mm->ip4_reass_pool, r);
1412   mm->ip4_reass_allocated--;
1413 }
1414
1415 map_ip4_reass_t *
1416 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1417                    u8 protocol, u32 ** pi_to_drop)
1418 {
1419   map_ip4_reass_t *r;
1420   map_main_t *mm = &map_main;
1421   map_ip4_reass_key_t k = {.src.data_u32 = src,
1422     .dst.data_u32 = dst,
1423     .fragment_id = fragment_id,
1424     .protocol = protocol
1425   };
1426
1427   u32 h = 0;
1428 #ifdef clib_crc32c_uses_intrinsics
1429   h = clib_crc32c ((u8 *) k.as_u32, 16);
1430 #else
1431   u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1432   h = clib_xxhash (tmp);
1433 #endif
1434   h = h >> (32 - mm->ip4_reass_ht_log2len);
1435
1436   f64 now = vlib_time_now (mm->vlib_main);
1437
1438   //Cache garbage collection
1439   while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1440     {
1441       map_ip4_reass_t *last =
1442         pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1443       if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1444         map_ip4_reass_free (last, pi_to_drop);
1445       else
1446         break;
1447     }
1448
1449   if ((r = map_ip4_reass_lookup (&k, h, now)))
1450     return r;
1451
1452   if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1453     return NULL;
1454
1455   pool_get (mm->ip4_reass_pool, r);
1456   mm->ip4_reass_allocated++;
1457   int i;
1458   for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1459     r->fragments[i] = ~0;
1460
1461   u32 ri = map_ip4_reass_pool_index (r);
1462
1463   //Link in new bucket
1464   r->bucket = h;
1465   r->bucket_next = mm->ip4_reass_hash_table[h];
1466   mm->ip4_reass_hash_table[h] = ri;
1467
1468   //Link in fifo
1469   if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1470     {
1471       r->fifo_next =
1472         pool_elt_at_index (mm->ip4_reass_pool,
1473                            mm->ip4_reass_fifo_last)->fifo_next;
1474       r->fifo_prev = mm->ip4_reass_fifo_last;
1475       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1476       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1477     }
1478   else
1479     {
1480       r->fifo_next = r->fifo_prev = ri;
1481       mm->ip4_reass_fifo_last = ri;
1482     }
1483
1484   //Set other fields
1485   r->ts = now;
1486   r->key = k;
1487   r->port = -1;
1488 #ifdef MAP_IP4_REASS_COUNT_BYTES
1489   r->expected_total = 0xffff;
1490   r->forwarded = 0;
1491 #endif
1492
1493   return r;
1494 }
1495
1496 int
1497 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1498 {
1499   if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1500     return -1;
1501
1502   int i;
1503   for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1504     if (r->fragments[i] == ~0)
1505       {
1506         r->fragments[i] = pi;
1507         map_main.ip4_reass_buffered_counter++;
1508         return 0;
1509       }
1510   return -1;
1511 }
1512
1513 static_always_inline map_ip6_reass_t *
1514 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1515 {
1516   map_main_t *mm = &map_main;
1517   u32 ri = mm->ip6_reass_hash_table[bucket];
1518   while (ri != MAP_REASS_INDEX_NONE)
1519     {
1520       map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1521       if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1522           r->key.as_u64[0] == k->as_u64[0] &&
1523           r->key.as_u64[1] == k->as_u64[1] &&
1524           r->key.as_u64[2] == k->as_u64[2] &&
1525           r->key.as_u64[3] == k->as_u64[3] &&
1526           r->key.as_u64[4] == k->as_u64[4])
1527         return r;
1528       ri = r->bucket_next;
1529     }
1530   return NULL;
1531 }
1532
1533 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1534
1535 void
1536 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1537 {
1538   map_main_t *mm = &map_main;
1539   int i;
1540   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1541     if (r->fragments[i].pi != ~0)
1542       {
1543         vec_add1 (*pi_to_drop, r->fragments[i].pi);
1544         r->fragments[i].pi = ~0;
1545         map_main.ip6_reass_buffered_counter--;
1546       }
1547
1548   // Unlink in hash bucket
1549   map_ip6_reass_t *r2 = NULL;
1550   u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1551   while (r2i != map_ip6_reass_pool_index (r))
1552     {
1553       ASSERT (r2i != MAP_REASS_INDEX_NONE);
1554       r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1555       r2i = r2->bucket_next;
1556     }
1557   if (r2)
1558     {
1559       r2->bucket_next = r->bucket_next;
1560     }
1561   else
1562     {
1563       mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1564     }
1565
1566   // Unlink in list
1567   if (r->fifo_next == map_ip6_reass_pool_index (r))
1568     {
1569       //Single element in the list, list is now empty
1570       mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1571     }
1572   else
1573     {
1574       if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r))      //First element
1575         mm->ip6_reass_fifo_last = r->fifo_prev;
1576       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1577         r->fifo_next;
1578       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1579         r->fifo_prev;
1580     }
1581
1582   // Free from pool if necessary
1583   pool_put (mm->ip6_reass_pool, r);
1584   mm->ip6_reass_allocated--;
1585 }
1586
1587 map_ip6_reass_t *
1588 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1589                    u8 protocol, u32 ** pi_to_drop)
1590 {
1591   map_ip6_reass_t *r;
1592   map_main_t *mm = &map_main;
1593   map_ip6_reass_key_t k = {
1594     .src = *src,
1595     .dst = *dst,
1596     .fragment_id = fragment_id,
1597     .protocol = protocol
1598   };
1599
1600   u32 h = 0;
1601   int i;
1602
1603 #ifdef clib_crc32c_uses_intrinsics
1604   h = clib_crc32c ((u8 *) k.as_u32, 40);
1605 #else
1606   u64 tmp =
1607     k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1608   h = clib_xxhash (tmp);
1609 #endif
1610
1611   h = h >> (32 - mm->ip6_reass_ht_log2len);
1612
1613   f64 now = vlib_time_now (mm->vlib_main);
1614
1615   //Cache garbage collection
1616   while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1617     {
1618       map_ip6_reass_t *last =
1619         pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1620       if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1621         map_ip6_reass_free (last, pi_to_drop);
1622       else
1623         break;
1624     }
1625
1626   if ((r = map_ip6_reass_lookup (&k, h, now)))
1627     return r;
1628
1629   if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1630     return NULL;
1631
1632   pool_get (mm->ip6_reass_pool, r);
1633   mm->ip6_reass_allocated++;
1634   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1635     {
1636       r->fragments[i].pi = ~0;
1637       r->fragments[i].next_data_len = 0;
1638       r->fragments[i].next_data_offset = 0;
1639     }
1640
1641   u32 ri = map_ip6_reass_pool_index (r);
1642
1643   //Link in new bucket
1644   r->bucket = h;
1645   r->bucket_next = mm->ip6_reass_hash_table[h];
1646   mm->ip6_reass_hash_table[h] = ri;
1647
1648   //Link in fifo
1649   if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1650     {
1651       r->fifo_next =
1652         pool_elt_at_index (mm->ip6_reass_pool,
1653                            mm->ip6_reass_fifo_last)->fifo_next;
1654       r->fifo_prev = mm->ip6_reass_fifo_last;
1655       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1656       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1657     }
1658   else
1659     {
1660       r->fifo_next = r->fifo_prev = ri;
1661       mm->ip6_reass_fifo_last = ri;
1662     }
1663
1664   //Set other fields
1665   r->ts = now;
1666   r->key = k;
1667   r->ip4_header.ip_version_and_header_length = 0;
1668 #ifdef MAP_IP6_REASS_COUNT_BYTES
1669   r->expected_total = 0xffff;
1670   r->forwarded = 0;
1671 #endif
1672   return r;
1673 }
1674
1675 int
1676 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1677                             u16 data_offset, u16 next_data_offset,
1678                             u8 * data_start, u16 data_len)
1679 {
1680   map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1681   u16 copied_len = (data_len > 20) ? 20 : data_len;
1682
1683   if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1684     return -1;
1685
1686   //Lookup for fragments for the current buffer
1687   //and the one before that
1688   int i;
1689   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1690     {
1691       if (data_offset && r->fragments[i].next_data_offset == data_offset)
1692         {
1693           prev_f = &r->fragments[i];    // This is buffer for previous packet
1694         }
1695       else if (r->fragments[i].next_data_offset == next_data_offset)
1696         {
1697           f = &r->fragments[i]; // This is a buffer for the current packet
1698         }
1699       else if (r->fragments[i].next_data_offset == 0)
1700         {                       //Available
1701           if (f == NULL)
1702             f = &r->fragments[i];
1703           else if (prev_f == NULL)
1704             prev_f = &r->fragments[i];
1705         }
1706     }
1707
1708   if (!f || f->pi != ~0)
1709     return -1;
1710
1711   if (data_offset)
1712     {
1713       if (!prev_f)
1714         return -1;
1715
1716       clib_memcpy_fast (prev_f->next_data, data_start, copied_len);
1717       prev_f->next_data_len = copied_len;
1718       prev_f->next_data_offset = data_offset;
1719     }
1720   else
1721     {
1722       if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1723         return -1;
1724
1725       if (r->ip4_header.ip_version_and_header_length == 0)
1726         clib_memcpy_fast (&r->ip4_header, data_start, sizeof (ip4_header_t));
1727     }
1728
1729   if (data_len > 20)
1730     {
1731       f->next_data_offset = next_data_offset;
1732       f->pi = pi;
1733       map_main.ip6_reass_buffered_counter++;
1734     }
1735   return 0;
1736 }
1737
1738 void
1739 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1740 {
1741   map_main_t *mm = &map_main;
1742   int i;
1743
1744   if (dropped_packets)
1745     *dropped_packets = mm->ip4_reass_buffered_counter;
1746   if (trashed_reass)
1747     *trashed_reass = mm->ip4_reass_allocated;
1748   if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1749     {
1750       u16 ri = mm->ip4_reass_fifo_last;
1751       do
1752         {
1753           map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1754           for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1755             if (r->fragments[i] != ~0)
1756               map_ip4_drop_pi (r->fragments[i]);
1757
1758           ri = r->fifo_next;
1759           pool_put (mm->ip4_reass_pool, r);
1760         }
1761       while (ri != mm->ip4_reass_fifo_last);
1762     }
1763
1764   vec_free (mm->ip4_reass_hash_table);
1765   vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1766   for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1767     mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1768   pool_free (mm->ip4_reass_pool);
1769   pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1770
1771   mm->ip4_reass_allocated = 0;
1772   mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1773   mm->ip4_reass_buffered_counter = 0;
1774 }
1775
1776 u8
1777 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1778 {
1779   u32 desired_size = (u32) (pool_size * ht_ratio);
1780   u8 i;
1781   for (i = 1; i < 31; i++)
1782     if ((1 << i) >= desired_size)
1783       return i;
1784   return 4;
1785 }
1786
1787 int
1788 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1789                              u32 * dropped_packets)
1790 {
1791   map_main_t *mm = &map_main;
1792   if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1793     return -1;
1794
1795   map_ip4_reass_lock ();
1796   mm->ip4_reass_conf_ht_ratio = ht_ratio;
1797   mm->ip4_reass_ht_log2len =
1798     map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1799   map_ip4_reass_reinit (trashed_reass, dropped_packets);
1800   map_ip4_reass_unlock ();
1801   return 0;
1802 }
1803
1804 int
1805 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1806                               u32 * dropped_packets)
1807 {
1808   map_main_t *mm = &map_main;
1809   if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1810     return -1;
1811
1812   map_ip4_reass_lock ();
1813   mm->ip4_reass_conf_pool_size = pool_size;
1814   map_ip4_reass_reinit (trashed_reass, dropped_packets);
1815   map_ip4_reass_unlock ();
1816   return 0;
1817 }
1818
1819 int
1820 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1821 {
1822   map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1823   return 0;
1824 }
1825
1826 int
1827 map_ip4_reass_conf_buffers (u32 buffers)
1828 {
1829   map_main.ip4_reass_conf_buffers = buffers;
1830   return 0;
1831 }
1832
1833 void
1834 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1835 {
1836   map_main_t *mm = &map_main;
1837   if (dropped_packets)
1838     *dropped_packets = mm->ip6_reass_buffered_counter;
1839   if (trashed_reass)
1840     *trashed_reass = mm->ip6_reass_allocated;
1841   int i;
1842   if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1843     {
1844       u16 ri = mm->ip6_reass_fifo_last;
1845       do
1846         {
1847           map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1848           for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1849             if (r->fragments[i].pi != ~0)
1850               map_ip6_drop_pi (r->fragments[i].pi);
1851
1852           ri = r->fifo_next;
1853           pool_put (mm->ip6_reass_pool, r);
1854         }
1855       while (ri != mm->ip6_reass_fifo_last);
1856       mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1857     }
1858
1859   vec_free (mm->ip6_reass_hash_table);
1860   vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1861   for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1862     mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1863   pool_free (mm->ip6_reass_pool);
1864   pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1865
1866   mm->ip6_reass_allocated = 0;
1867   mm->ip6_reass_buffered_counter = 0;
1868 }
1869
1870 int
1871 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1872                              u32 * dropped_packets)
1873 {
1874   map_main_t *mm = &map_main;
1875   if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1876     return -1;
1877
1878   map_ip6_reass_lock ();
1879   mm->ip6_reass_conf_ht_ratio = ht_ratio;
1880   mm->ip6_reass_ht_log2len =
1881     map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1882   map_ip6_reass_reinit (trashed_reass, dropped_packets);
1883   map_ip6_reass_unlock ();
1884   return 0;
1885 }
1886
1887 int
1888 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1889                               u32 * dropped_packets)
1890 {
1891   map_main_t *mm = &map_main;
1892   if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1893     return -1;
1894
1895   map_ip6_reass_lock ();
1896   mm->ip6_reass_conf_pool_size = pool_size;
1897   map_ip6_reass_reinit (trashed_reass, dropped_packets);
1898   map_ip6_reass_unlock ();
1899   return 0;
1900 }
1901
1902 int
1903 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1904 {
1905   map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1906   return 0;
1907 }
1908
1909 int
1910 map_ip6_reass_conf_buffers (u32 buffers)
1911 {
1912   map_main.ip6_reass_conf_buffers = buffers;
1913   return 0;
1914 }
1915
1916 /* *INDENT-OFF* */
1917
1918 /*?
1919  * Configure MAP reassembly behaviour
1920  *
1921  * @cliexpar
1922  * @cliexstart{map params reassembly}
1923  * @cliexend
1924  ?*/
1925 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1926   .path = "map params reassembly",
1927   .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
1928                 "[pool-size <pool-size>] [buffers <buffers>] "
1929                 "[ht-ratio <ht-ratio>]",
1930   .function = map_params_reass_command_fn,
1931 };
1932
1933 /*?
1934  * Set or copy the IP TOS/Traffic Class field
1935  *
1936  * @cliexpar
1937  * @cliexstart{map params traffic-class}
1938  *
1939  * This command is used to set the traffic-class field in translated
1940  * or encapsulated packets. If copy is specifed (the default) then the
1941  * traffic-class/TOS field is copied from the original packet to the
1942  * translated / encapsulating header.
1943  * @cliexend
1944  ?*/
1945 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1946   .path = "map params traffic-class",
1947   .short_help = "map params traffic-class {0x0-0xff | copy}",
1948   .function = map_traffic_class_command_fn,
1949 };
1950
1951 /*?
1952  * Bypass IP4/IP6 lookup
1953  *
1954  * @cliexpar
1955  * @cliexstart{map params pre-resolve}
1956  *
1957  * Bypass a second FIB lookup of the translated or encapsulated
1958  * packet, and forward the packet directly to the specified
1959  * next-hop. This optimization trades forwarding flexibility for
1960  * performance.
1961  * @cliexend
1962  ?*/
1963 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1964   .path = "map params pre-resolve",
1965   .short_help = " map params pre-resolve {ip4-nh <address>} "
1966                 "| {ip6-nh <address>}",
1967   .function = map_pre_resolve_command_fn,
1968 };
1969
1970 /*?
1971  * Enable or disable the MAP-E inbound security check
1972  * Specifiy if the inbound security check should be done on fragments
1973  *
1974  * @cliexpar
1975  * @cliexstart{map params security-check}
1976  *
1977  * By default, a decapsulated packet's IPv4 source address will be
1978  * verified against the outer header's IPv6 source address. Disabling
1979  * this feature will allow IPv4 source address spoofing.
1980  *
1981  * Typically the inbound on-decapsulation security check is only done
1982  * on the first packet. The packet that contains the L4
1983  * information. While a security check on every fragment is possible,
1984  * it has a cost. State must be created on the first fragment.
1985  * @cliexend
1986  ?*/
1987 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1988   .path = "map params security-check",
1989   .short_help = "map params security-check enable|disable fragments on|off",
1990   .function = map_security_check_command_fn,
1991 };
1992
1993
1994 /*?
1995  * Specifiy the IPv4 source address used for relayed ICMP error messages
1996  *
1997  * @cliexpar
1998  * @cliexstart{map params icmp source-address}
1999  *
2000  * This command specifies which IPv4 source address (must be local to
2001  * the system), that is used for relayed received IPv6 ICMP error
2002  * messages.
2003  * @cliexend
2004  ?*/
2005 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2006   .path = "map params icmp source-address",
2007   .short_help = "map params icmp source-address <ip4-address>",
2008   .function = map_icmp_relay_source_address_command_fn,
2009 };
2010
2011 /*?
2012  * Send IPv6 ICMP unreachables
2013  *
2014  * @cliexpar
2015  * @cliexstart{map params icmp6 unreachables}
2016  *
2017  * Send IPv6 ICMP unreachable messages back if security check fails or
2018  * no MAP domain exists.
2019  * @cliexend
2020  ?*/
2021 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2022   .path = "map params icmp6 unreachables",
2023   .short_help = "map params icmp6 unreachables {on|off}",
2024   .function = map_icmp_unreachables_command_fn,
2025 };
2026
2027 /*?
2028  * Configure MAP fragmentation behaviour
2029  *
2030  * @cliexpar
2031  * @cliexstart{map params fragment}
2032  *
2033  * Allows fragmentation of the IPv4 packet even if the DF bit is
2034  * set. The choice between inner or outer fragmentation of tunnel
2035  * packets is complicated. The benefit of inner fragmentation is that
2036  * the ultimate endpoint must reassemble, instead of the tunnel
2037  * endpoint.
2038  * @cliexend
2039  ?*/
2040 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2041   .path = "map params fragment",
2042   .short_help = "map params fragment inner|outer ignore-df|honor-df",
2043   .function = map_fragment_command_fn,
2044 };
2045
2046
2047 /*?
2048  * Add MAP domain
2049  *
2050  * @cliexpar
2051  * @cliexstart{map add domain}
2052  * @cliexend
2053  ?*/
2054 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2055   .path = "map add domain",
2056   .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2057       "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2058       "[map-t] [mtu <mtu>]",
2059   .function = map_add_domain_command_fn,
2060 };
2061
2062 /*?
2063  * Add MAP rule to a domain
2064  *
2065  * @cliexpar
2066  * @cliexstart{map add rule}
2067  * @cliexend
2068  ?*/
2069 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2070   .path = "map add rule",
2071   .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2072   .function = map_add_rule_command_fn,
2073 };
2074
2075 /*?
2076  * Delete MAP domain
2077  *
2078  * @cliexpar
2079  * @cliexstart{map del domain}
2080  * @cliexend
2081  ?*/
2082 VLIB_CLI_COMMAND(map_del_command, static) = {
2083   .path = "map del domain",
2084   .short_help = "map del domain index <domain>",
2085   .function = map_del_domain_command_fn,
2086 };
2087
2088 /*?
2089  * Show MAP domains
2090  *
2091  * @cliexpar
2092  * @cliexstart{show map domain}
2093  * @cliexend
2094  ?*/
2095 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2096   .path = "show map domain",
2097   .short_help = "show map domain index <n> [counters]",
2098   .function = show_map_domain_command_fn,
2099 };
2100
2101 /*?
2102  * Show MAP statistics
2103  *
2104  * @cliexpar
2105  * @cliexstart{show map stats}
2106  * @cliexend
2107  ?*/
2108 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2109   .path = "show map stats",
2110   .short_help = "show map stats",
2111   .function = show_map_stats_command_fn,
2112 };
2113
2114 /*?
2115  * Show MAP fragmentation information
2116  *
2117  * @cliexpar
2118  * @cliexstart{show map fragments}
2119  * @cliexend
2120  ?*/
2121 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2122   .path = "show map fragments",
2123   .short_help = "show map fragments",
2124   .function = show_map_fragments_command_fn,
2125 };
2126
2127 VLIB_PLUGIN_REGISTER() = {
2128   .version = VPP_BUILD_VER,
2129   .description = "Mapping of address and port (MAP)",
2130 };
2131
2132 /* *INDENT-ON* */
2133
2134 /*
2135  * map_init
2136  */
2137 clib_error_t *
2138 map_init (vlib_main_t * vm)
2139 {
2140   map_main_t *mm = &map_main;
2141   clib_error_t *error = 0;
2142   mm->vnet_main = vnet_get_main ();
2143   mm->vlib_main = vm;
2144
2145 #ifdef MAP_SKIP_IP6_LOOKUP
2146   fib_protocol_t proto;
2147
2148   FOR_EACH_FIB_PROTOCOL (proto)
2149   {
2150     map_pre_resolve_init (&pre_resolved[proto]);
2151   }
2152 #endif
2153
2154   /* traffic class */
2155   mm->tc = 0;
2156   mm->tc_copy = true;
2157
2158   /* Inbound security check */
2159   mm->sec_check = true;
2160   mm->sec_check_frag = false;
2161
2162   /* ICMP6 Type 1, Code 5 for security check failure */
2163   mm->icmp6_enabled = false;
2164
2165   /* Inner or outer fragmentation */
2166   mm->frag_inner = false;
2167   mm->frag_ignore_df = false;
2168
2169   vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2170   mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
2171   mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
2172
2173   vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2174   vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2175
2176   /* IP4 virtual reassembly */
2177   mm->ip4_reass_hash_table = 0;
2178   mm->ip4_reass_pool = 0;
2179   mm->ip4_reass_lock =
2180     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2181   *mm->ip4_reass_lock = 0;
2182   mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2183   mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2184   mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2185   mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2186   mm->ip4_reass_ht_log2len =
2187     map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2188                         mm->ip4_reass_conf_pool_size);
2189   mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2190   map_ip4_reass_reinit (NULL, NULL);
2191
2192   /* IP6 virtual reassembly */
2193   mm->ip6_reass_hash_table = 0;
2194   mm->ip6_reass_pool = 0;
2195   mm->ip6_reass_lock =
2196     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2197   *mm->ip6_reass_lock = 0;
2198   mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2199   mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2200   mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2201   mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2202   mm->ip6_reass_ht_log2len =
2203     map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2204                         mm->ip6_reass_conf_pool_size);
2205   mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2206   map_ip6_reass_reinit (NULL, NULL);
2207
2208 #ifdef MAP_SKIP_IP6_LOOKUP
2209   fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2210 #endif
2211   map_dpo_module_init ();
2212
2213   error = map_plugin_api_hookup (vm);
2214
2215   return error;
2216 }
2217
2218 VLIB_INIT_FUNCTION (map_init);
2219
2220 /*
2221  * fd.io coding-style-patch-verification: ON
2222  *
2223  * Local Variables:
2224  * eval: (c-set-style "gnu")
2225  * End:
2226  */