MAP: Add API support for MAP input feature.
[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   if (mm->tcp_mss)
1173     vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
1174
1175   vlib_cli_output (vm,
1176                    "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1177                    mm->sec_check ? "enabled" : "disabled",
1178                    mm->sec_check_frag ? "enabled" : "disabled");
1179
1180   vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1181                    format_ip4_address, &mm->icmp4_src_address);
1182   vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1183                    mm->icmp6_enabled ? "enabled" : "disabled");
1184   vlib_cli_output (vm, "Inner fragmentation: %s\n",
1185                    mm->frag_inner ? "enabled" : "disabled");
1186   vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1187                    mm->frag_ignore_df ? "enabled" : "disabled");
1188
1189   /*
1190    * Counters
1191    */
1192   vlib_combined_counter_main_t *cm = mm->domain_counters;
1193   u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1194   u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1195   int which, i;
1196   vlib_counter_t v;
1197
1198   clib_memset (total_pkts, 0, sizeof (total_pkts));
1199   clib_memset (total_bytes, 0, sizeof (total_bytes));
1200
1201   map_domain_counter_lock (mm);
1202   vec_foreach (cm, mm->domain_counters)
1203   {
1204     which = cm - mm->domain_counters;
1205
1206     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1207       {
1208         vlib_get_combined_counter (cm, i, &v);
1209         total_pkts[which] += v.packets;
1210         total_bytes[which] += v.bytes;
1211       }
1212   }
1213   map_domain_counter_unlock (mm);
1214
1215   vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1216                    total_pkts[MAP_DOMAIN_COUNTER_TX],
1217                    total_bytes[MAP_DOMAIN_COUNTER_TX]);
1218   vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1219                    total_pkts[MAP_DOMAIN_COUNTER_RX],
1220                    total_bytes[MAP_DOMAIN_COUNTER_RX]);
1221
1222   vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1223                    vlib_get_simple_counter (&mm->icmp_relayed, 0));
1224
1225   return 0;
1226 }
1227
1228 static clib_error_t *
1229 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1230                              vlib_cli_command_t * cmd)
1231 {
1232   unformat_input_t _line_input, *line_input = &_line_input;
1233   u32 lifetime = ~0;
1234   f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1235   u32 pool_size = ~0;
1236   u64 buffers = ~(0ull);
1237   u8 ip4 = 0, ip6 = 0;
1238
1239   if (!unformat_user (input, unformat_line_input, line_input))
1240     return 0;
1241
1242   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1243     {
1244       if (unformat (line_input, "lifetime %u", &lifetime))
1245         ;
1246       else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1247         ;
1248       else if (unformat (line_input, "pool-size %u", &pool_size))
1249         ;
1250       else if (unformat (line_input, "buffers %llu", &buffers))
1251         ;
1252       else if (unformat (line_input, "ip4"))
1253         ip4 = 1;
1254       else if (unformat (line_input, "ip6"))
1255         ip6 = 1;
1256       else
1257         {
1258           unformat_free (line_input);
1259           return clib_error_return (0, "invalid input");
1260         }
1261     }
1262   unformat_free (line_input);
1263
1264   if (!ip4 && !ip6)
1265     return clib_error_return (0, "must specify ip4 and/or ip6");
1266
1267   if (ip4)
1268     {
1269       if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1270         return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1271                                   MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1272       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1273           && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1274         return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1275                                   MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1276       if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1277         return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1278                                   MAP_IP4_REASS_CONF_LIFETIME_MAX);
1279       if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1280         return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1281                                   MAP_IP4_REASS_CONF_BUFFERS_MAX);
1282     }
1283
1284   if (ip6)
1285     {
1286       if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1287         return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1288                                   MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1289       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1290           && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1291         return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1292                                   MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1293       if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1294         return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1295                                   MAP_IP6_REASS_CONF_LIFETIME_MAX);
1296       if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1297         return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1298                                   MAP_IP6_REASS_CONF_BUFFERS_MAX);
1299     }
1300
1301   int rv;
1302   u32 reass = 0, packets = 0;
1303   rv = map_param_set_reassembly (!ip4, lifetime, pool_size, buffers, ht_ratio,
1304                                  &reass, &packets);
1305
1306   switch (rv)
1307     {
1308     case 0:
1309       vlib_cli_output (vm,
1310                        "Note: destroyed-reassembly=%u , dropped-fragments=%u",
1311                        reass, packets);
1312       break;
1313
1314     case MAP_ERR_BAD_POOL_SIZE:
1315       return clib_error_return (0, "Could not set reass pool-size");
1316
1317     case MAP_ERR_BAD_HT_RATIO:
1318       return clib_error_return (0, "Could not set reass ht-log2len");
1319
1320     case MAP_ERR_BAD_LIFETIME:
1321       return clib_error_return (0, "Could not set ip6-reass lifetime");
1322
1323     case MAP_ERR_BAD_BUFFERS:
1324       return clib_error_return (0, "Could not set ip6-reass buffers");
1325
1326     case MAP_ERR_BAD_BUFFERS_TOO_LARGE:
1327       return clib_error_return (0,
1328                                 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1329     }
1330
1331   return 0;
1332 }
1333
1334
1335 static clib_error_t *
1336 map_if_command_fn (vlib_main_t * vm,
1337                    unformat_input_t * input, vlib_cli_command_t * cmd)
1338 {
1339   unformat_input_t _line_input, *line_input = &_line_input;
1340   clib_error_t *error = NULL;
1341   bool is_enable = true, is_translation = false;
1342   vnet_main_t *vnm = vnet_get_main ();
1343   u32 sw_if_index = ~0;
1344
1345   /* Get a line of input. */
1346   if (!unformat_user (input, unformat_line_input, line_input))
1347     return 0;
1348
1349   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1350     {
1351       if (unformat
1352           (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1353         ;
1354       else if (unformat (line_input, "del"))
1355         is_enable = false;
1356       else if (unformat (line_input, "map-t"))
1357         is_translation = true;
1358       else
1359         {
1360           error = clib_error_return (0, "unknown input `%U'",
1361                                      format_unformat_error, line_input);
1362           goto done;
1363         }
1364     }
1365
1366 done:
1367   unformat_free (line_input);
1368
1369   if (sw_if_index == ~0)
1370     {
1371       error = clib_error_return (0, "unknown interface");
1372       return error;
1373     }
1374
1375   int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
1376   if (rv)
1377     {
1378       error = clib_error_return (0, "failure enabling MAP on interface");
1379     }
1380
1381   return error;
1382 }
1383
1384
1385 /*
1386  * packet trace format function
1387  */
1388 u8 *
1389 format_map_trace (u8 * s, va_list * args)
1390 {
1391   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1392   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1393   map_trace_t *t = va_arg (*args, map_trace_t *);
1394   u32 map_domain_index = t->map_domain_index;
1395   u16 port = t->port;
1396
1397   s =
1398     format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1399             clib_net_to_host_u16 (port));
1400
1401   return s;
1402 }
1403
1404 static_always_inline map_ip4_reass_t *
1405 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1406 {
1407   map_main_t *mm = &map_main;
1408   u32 ri = mm->ip4_reass_hash_table[bucket];
1409   while (ri != MAP_REASS_INDEX_NONE)
1410     {
1411       map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1412       if (r->key.as_u64[0] == k->as_u64[0] &&
1413           r->key.as_u64[1] == k->as_u64[1] &&
1414           now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1415         {
1416           return r;
1417         }
1418       ri = r->bucket_next;
1419     }
1420   return NULL;
1421 }
1422
1423 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1424
1425 void
1426 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1427 {
1428   map_main_t *mm = &map_main;
1429   map_ip4_reass_get_fragments (r, pi_to_drop);
1430
1431   // Unlink in hash bucket
1432   map_ip4_reass_t *r2 = NULL;
1433   u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1434   while (r2i != map_ip4_reass_pool_index (r))
1435     {
1436       ASSERT (r2i != MAP_REASS_INDEX_NONE);
1437       r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1438       r2i = r2->bucket_next;
1439     }
1440   if (r2)
1441     {
1442       r2->bucket_next = r->bucket_next;
1443     }
1444   else
1445     {
1446       mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1447     }
1448
1449   // Unlink in list
1450   if (r->fifo_next == map_ip4_reass_pool_index (r))
1451     {
1452       mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1453     }
1454   else
1455     {
1456       if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1457         mm->ip4_reass_fifo_last = r->fifo_prev;
1458       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1459         r->fifo_next;
1460       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1461         r->fifo_prev;
1462     }
1463
1464   pool_put (mm->ip4_reass_pool, r);
1465   mm->ip4_reass_allocated--;
1466 }
1467
1468 map_ip4_reass_t *
1469 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1470                    u8 protocol, u32 ** pi_to_drop)
1471 {
1472   map_ip4_reass_t *r;
1473   map_main_t *mm = &map_main;
1474   map_ip4_reass_key_t k = {.src.data_u32 = src,
1475     .dst.data_u32 = dst,
1476     .fragment_id = fragment_id,
1477     .protocol = protocol
1478   };
1479
1480   u32 h = 0;
1481 #ifdef clib_crc32c_uses_intrinsics
1482   h = clib_crc32c ((u8 *) k.as_u32, 16);
1483 #else
1484   u64 tmp = k.as_u32[0] ^ k.as_u32[1] ^ k.as_u32[2] ^ k.as_u32[3];
1485   h = clib_xxhash (tmp);
1486 #endif
1487   h = h >> (32 - mm->ip4_reass_ht_log2len);
1488
1489   f64 now = vlib_time_now (mm->vlib_main);
1490
1491   //Cache garbage collection
1492   while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1493     {
1494       map_ip4_reass_t *last =
1495         pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1496       if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1497         map_ip4_reass_free (last, pi_to_drop);
1498       else
1499         break;
1500     }
1501
1502   if ((r = map_ip4_reass_lookup (&k, h, now)))
1503     return r;
1504
1505   if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1506     return NULL;
1507
1508   pool_get (mm->ip4_reass_pool, r);
1509   mm->ip4_reass_allocated++;
1510   int i;
1511   for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1512     r->fragments[i] = ~0;
1513
1514   u32 ri = map_ip4_reass_pool_index (r);
1515
1516   //Link in new bucket
1517   r->bucket = h;
1518   r->bucket_next = mm->ip4_reass_hash_table[h];
1519   mm->ip4_reass_hash_table[h] = ri;
1520
1521   //Link in fifo
1522   if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1523     {
1524       r->fifo_next =
1525         pool_elt_at_index (mm->ip4_reass_pool,
1526                            mm->ip4_reass_fifo_last)->fifo_next;
1527       r->fifo_prev = mm->ip4_reass_fifo_last;
1528       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1529       pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1530     }
1531   else
1532     {
1533       r->fifo_next = r->fifo_prev = ri;
1534       mm->ip4_reass_fifo_last = ri;
1535     }
1536
1537   //Set other fields
1538   r->ts = now;
1539   r->key = k;
1540   r->port = -1;
1541 #ifdef MAP_IP4_REASS_COUNT_BYTES
1542   r->expected_total = 0xffff;
1543   r->forwarded = 0;
1544 #endif
1545
1546   return r;
1547 }
1548
1549 int
1550 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1551 {
1552   if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1553     return -1;
1554
1555   int i;
1556   for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1557     if (r->fragments[i] == ~0)
1558       {
1559         r->fragments[i] = pi;
1560         map_main.ip4_reass_buffered_counter++;
1561         return 0;
1562       }
1563   return -1;
1564 }
1565
1566 static_always_inline map_ip6_reass_t *
1567 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1568 {
1569   map_main_t *mm = &map_main;
1570   u32 ri = mm->ip6_reass_hash_table[bucket];
1571   while (ri != MAP_REASS_INDEX_NONE)
1572     {
1573       map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1574       if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1575           r->key.as_u64[0] == k->as_u64[0] &&
1576           r->key.as_u64[1] == k->as_u64[1] &&
1577           r->key.as_u64[2] == k->as_u64[2] &&
1578           r->key.as_u64[3] == k->as_u64[3] &&
1579           r->key.as_u64[4] == k->as_u64[4])
1580         return r;
1581       ri = r->bucket_next;
1582     }
1583   return NULL;
1584 }
1585
1586 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1587
1588 void
1589 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1590 {
1591   map_main_t *mm = &map_main;
1592   int i;
1593   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1594     if (r->fragments[i].pi != ~0)
1595       {
1596         vec_add1 (*pi_to_drop, r->fragments[i].pi);
1597         r->fragments[i].pi = ~0;
1598         map_main.ip6_reass_buffered_counter--;
1599       }
1600
1601   // Unlink in hash bucket
1602   map_ip6_reass_t *r2 = NULL;
1603   u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1604   while (r2i != map_ip6_reass_pool_index (r))
1605     {
1606       ASSERT (r2i != MAP_REASS_INDEX_NONE);
1607       r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1608       r2i = r2->bucket_next;
1609     }
1610   if (r2)
1611     {
1612       r2->bucket_next = r->bucket_next;
1613     }
1614   else
1615     {
1616       mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1617     }
1618
1619   // Unlink in list
1620   if (r->fifo_next == map_ip6_reass_pool_index (r))
1621     {
1622       //Single element in the list, list is now empty
1623       mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1624     }
1625   else
1626     {
1627       if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r))      //First element
1628         mm->ip6_reass_fifo_last = r->fifo_prev;
1629       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1630         r->fifo_next;
1631       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1632         r->fifo_prev;
1633     }
1634
1635   // Free from pool if necessary
1636   pool_put (mm->ip6_reass_pool, r);
1637   mm->ip6_reass_allocated--;
1638 }
1639
1640 map_ip6_reass_t *
1641 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1642                    u8 protocol, u32 ** pi_to_drop)
1643 {
1644   map_ip6_reass_t *r;
1645   map_main_t *mm = &map_main;
1646   map_ip6_reass_key_t k = {
1647     .src = *src,
1648     .dst = *dst,
1649     .fragment_id = fragment_id,
1650     .protocol = protocol
1651   };
1652
1653   u32 h = 0;
1654   int i;
1655
1656 #ifdef clib_crc32c_uses_intrinsics
1657   h = clib_crc32c ((u8 *) k.as_u32, 40);
1658 #else
1659   u64 tmp =
1660     k.as_u64[0] ^ k.as_u64[1] ^ k.as_u64[2] ^ k.as_u64[3] ^ k.as_u64[4];
1661   h = clib_xxhash (tmp);
1662 #endif
1663
1664   h = h >> (32 - mm->ip6_reass_ht_log2len);
1665
1666   f64 now = vlib_time_now (mm->vlib_main);
1667
1668   //Cache garbage collection
1669   while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1670     {
1671       map_ip6_reass_t *last =
1672         pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1673       if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1674         map_ip6_reass_free (last, pi_to_drop);
1675       else
1676         break;
1677     }
1678
1679   if ((r = map_ip6_reass_lookup (&k, h, now)))
1680     return r;
1681
1682   if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1683     return NULL;
1684
1685   pool_get (mm->ip6_reass_pool, r);
1686   mm->ip6_reass_allocated++;
1687   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1688     {
1689       r->fragments[i].pi = ~0;
1690       r->fragments[i].next_data_len = 0;
1691       r->fragments[i].next_data_offset = 0;
1692     }
1693
1694   u32 ri = map_ip6_reass_pool_index (r);
1695
1696   //Link in new bucket
1697   r->bucket = h;
1698   r->bucket_next = mm->ip6_reass_hash_table[h];
1699   mm->ip6_reass_hash_table[h] = ri;
1700
1701   //Link in fifo
1702   if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1703     {
1704       r->fifo_next =
1705         pool_elt_at_index (mm->ip6_reass_pool,
1706                            mm->ip6_reass_fifo_last)->fifo_next;
1707       r->fifo_prev = mm->ip6_reass_fifo_last;
1708       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1709       pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1710     }
1711   else
1712     {
1713       r->fifo_next = r->fifo_prev = ri;
1714       mm->ip6_reass_fifo_last = ri;
1715     }
1716
1717   //Set other fields
1718   r->ts = now;
1719   r->key = k;
1720   r->ip4_header.ip_version_and_header_length = 0;
1721 #ifdef MAP_IP6_REASS_COUNT_BYTES
1722   r->expected_total = 0xffff;
1723   r->forwarded = 0;
1724 #endif
1725   return r;
1726 }
1727
1728 int
1729 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1730                             u16 data_offset, u16 next_data_offset,
1731                             u8 * data_start, u16 data_len)
1732 {
1733   map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1734   u16 copied_len = (data_len > 20) ? 20 : data_len;
1735
1736   if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1737     return -1;
1738
1739   //Lookup for fragments for the current buffer
1740   //and the one before that
1741   int i;
1742   for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1743     {
1744       if (data_offset && r->fragments[i].next_data_offset == data_offset)
1745         {
1746           prev_f = &r->fragments[i];    // This is buffer for previous packet
1747         }
1748       else if (r->fragments[i].next_data_offset == next_data_offset)
1749         {
1750           f = &r->fragments[i]; // This is a buffer for the current packet
1751         }
1752       else if (r->fragments[i].next_data_offset == 0)
1753         {                       //Available
1754           if (f == NULL)
1755             f = &r->fragments[i];
1756           else if (prev_f == NULL)
1757             prev_f = &r->fragments[i];
1758         }
1759     }
1760
1761   if (!f || f->pi != ~0)
1762     return -1;
1763
1764   if (data_offset)
1765     {
1766       if (!prev_f)
1767         return -1;
1768
1769       clib_memcpy_fast (prev_f->next_data, data_start, copied_len);
1770       prev_f->next_data_len = copied_len;
1771       prev_f->next_data_offset = data_offset;
1772     }
1773   else
1774     {
1775       if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1776         return -1;
1777
1778       if (r->ip4_header.ip_version_and_header_length == 0)
1779         clib_memcpy_fast (&r->ip4_header, data_start, sizeof (ip4_header_t));
1780     }
1781
1782   if (data_len > 20)
1783     {
1784       f->next_data_offset = next_data_offset;
1785       f->pi = pi;
1786       map_main.ip6_reass_buffered_counter++;
1787     }
1788   return 0;
1789 }
1790
1791 void
1792 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1793 {
1794   map_main_t *mm = &map_main;
1795   int i;
1796
1797   if (dropped_packets)
1798     *dropped_packets = mm->ip4_reass_buffered_counter;
1799   if (trashed_reass)
1800     *trashed_reass = mm->ip4_reass_allocated;
1801   if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1802     {
1803       u16 ri = mm->ip4_reass_fifo_last;
1804       do
1805         {
1806           map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1807           for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1808             if (r->fragments[i] != ~0)
1809               map_ip4_drop_pi (r->fragments[i]);
1810
1811           ri = r->fifo_next;
1812           pool_put (mm->ip4_reass_pool, r);
1813         }
1814       while (ri != mm->ip4_reass_fifo_last);
1815     }
1816
1817   vec_free (mm->ip4_reass_hash_table);
1818   vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1819   for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1820     mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1821   pool_free (mm->ip4_reass_pool);
1822   pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1823
1824   mm->ip4_reass_allocated = 0;
1825   mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1826   mm->ip4_reass_buffered_counter = 0;
1827 }
1828
1829 u8
1830 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1831 {
1832   u32 desired_size = (u32) (pool_size * ht_ratio);
1833   u8 i;
1834   for (i = 1; i < 31; i++)
1835     if ((1 << i) >= desired_size)
1836       return i;
1837   return 4;
1838 }
1839
1840 int
1841 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1842                              u32 * dropped_packets)
1843 {
1844   map_main_t *mm = &map_main;
1845   if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1846     return -1;
1847
1848   map_ip4_reass_lock ();
1849   mm->ip4_reass_conf_ht_ratio = ht_ratio;
1850   mm->ip4_reass_ht_log2len =
1851     map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1852   map_ip4_reass_reinit (trashed_reass, dropped_packets);
1853   map_ip4_reass_unlock ();
1854   return 0;
1855 }
1856
1857 int
1858 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1859                               u32 * dropped_packets)
1860 {
1861   map_main_t *mm = &map_main;
1862   if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1863     return -1;
1864
1865   map_ip4_reass_lock ();
1866   mm->ip4_reass_conf_pool_size = pool_size;
1867   map_ip4_reass_reinit (trashed_reass, dropped_packets);
1868   map_ip4_reass_unlock ();
1869   return 0;
1870 }
1871
1872 int
1873 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1874 {
1875   map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1876   return 0;
1877 }
1878
1879 int
1880 map_ip4_reass_conf_buffers (u32 buffers)
1881 {
1882   map_main.ip4_reass_conf_buffers = buffers;
1883   return 0;
1884 }
1885
1886 void
1887 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1888 {
1889   map_main_t *mm = &map_main;
1890   if (dropped_packets)
1891     *dropped_packets = mm->ip6_reass_buffered_counter;
1892   if (trashed_reass)
1893     *trashed_reass = mm->ip6_reass_allocated;
1894   int i;
1895   if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1896     {
1897       u16 ri = mm->ip6_reass_fifo_last;
1898       do
1899         {
1900           map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1901           for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1902             if (r->fragments[i].pi != ~0)
1903               map_ip6_drop_pi (r->fragments[i].pi);
1904
1905           ri = r->fifo_next;
1906           pool_put (mm->ip6_reass_pool, r);
1907         }
1908       while (ri != mm->ip6_reass_fifo_last);
1909       mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1910     }
1911
1912   vec_free (mm->ip6_reass_hash_table);
1913   vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1914   for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1915     mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1916   pool_free (mm->ip6_reass_pool);
1917   pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1918
1919   mm->ip6_reass_allocated = 0;
1920   mm->ip6_reass_buffered_counter = 0;
1921 }
1922
1923 int
1924 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1925                              u32 * dropped_packets)
1926 {
1927   map_main_t *mm = &map_main;
1928   if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1929     return -1;
1930
1931   map_ip6_reass_lock ();
1932   mm->ip6_reass_conf_ht_ratio = ht_ratio;
1933   mm->ip6_reass_ht_log2len =
1934     map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1935   map_ip6_reass_reinit (trashed_reass, dropped_packets);
1936   map_ip6_reass_unlock ();
1937   return 0;
1938 }
1939
1940 int
1941 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1942                               u32 * dropped_packets)
1943 {
1944   map_main_t *mm = &map_main;
1945   if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1946     return -1;
1947
1948   map_ip6_reass_lock ();
1949   mm->ip6_reass_conf_pool_size = pool_size;
1950   map_ip6_reass_reinit (trashed_reass, dropped_packets);
1951   map_ip6_reass_unlock ();
1952   return 0;
1953 }
1954
1955 int
1956 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1957 {
1958   map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1959   return 0;
1960 }
1961
1962 int
1963 map_ip6_reass_conf_buffers (u32 buffers)
1964 {
1965   map_main.ip6_reass_conf_buffers = buffers;
1966   return 0;
1967 }
1968
1969 static clib_error_t *
1970 map_tcp_mss_command_fn (vlib_main_t * vm,
1971                         unformat_input_t * input, vlib_cli_command_t * cmd)
1972 {
1973   unformat_input_t _line_input, *line_input = &_line_input;
1974   clib_error_t *error = NULL;
1975   u32 tcp_mss = 0;
1976
1977   /* Get a line of input. */
1978   if (!unformat_user (input, unformat_line_input, line_input))
1979     return 0;
1980
1981   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1982     {
1983       if (unformat (line_input, "%u", &tcp_mss))
1984         ;
1985       else
1986         {
1987           error = clib_error_return (0, "unknown input `%U'",
1988                                      format_unformat_error, line_input);
1989           goto done;
1990         }
1991     }
1992
1993   if (tcp_mss >= (0x1 << 16))
1994     {
1995       error = clib_error_return (0, "invalid value `%u'", tcp_mss);
1996       goto done;
1997     }
1998
1999   map_param_set_tcp (tcp_mss);
2000
2001 done:
2002   unformat_free (line_input);
2003
2004   return error;
2005 }
2006
2007
2008 /* *INDENT-OFF* */
2009
2010 /*?
2011  * Configure MAP reassembly behaviour
2012  *
2013  * @cliexpar
2014  * @cliexstart{map params reassembly}
2015  * @cliexend
2016  ?*/
2017 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2018   .path = "map params reassembly",
2019   .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2020                 "[pool-size <pool-size>] [buffers <buffers>] "
2021                 "[ht-ratio <ht-ratio>]",
2022   .function = map_params_reass_command_fn,
2023 };
2024
2025 /*?
2026  * Set or copy the IP TOS/Traffic Class field
2027  *
2028  * @cliexpar
2029  * @cliexstart{map params traffic-class}
2030  *
2031  * This command is used to set the traffic-class field in translated
2032  * or encapsulated packets. If copy is specifed (the default) then the
2033  * traffic-class/TOS field is copied from the original packet to the
2034  * translated / encapsulating header.
2035  * @cliexend
2036  ?*/
2037 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2038   .path = "map params traffic-class",
2039   .short_help = "map params traffic-class {0x0-0xff | copy}",
2040   .function = map_traffic_class_command_fn,
2041 };
2042
2043 /*?
2044  * TCP MSS clamping
2045  *
2046  * @cliexpar
2047  * @cliexstart{map params tcp-mss}
2048  *
2049  * This command is used to set the TCP MSS in translated
2050  * or encapsulated packets.
2051  * @cliexend
2052  ?*/
2053 VLIB_CLI_COMMAND(map_tcp_mss_command, static) = {
2054   .path = "map params tcp-mss",
2055   .short_help = "map params tcp-mss <value>",
2056   .function = map_tcp_mss_command_fn,
2057 };
2058
2059 /*?
2060  * Bypass IP4/IP6 lookup
2061  *
2062  * @cliexpar
2063  * @cliexstart{map params pre-resolve}
2064  *
2065  * Bypass a second FIB lookup of the translated or encapsulated
2066  * packet, and forward the packet directly to the specified
2067  * next-hop. This optimization trades forwarding flexibility for
2068  * performance.
2069  * @cliexend
2070  ?*/
2071 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2072   .path = "map params pre-resolve",
2073   .short_help = " map params pre-resolve {ip4-nh <address>} "
2074                 "| {ip6-nh <address>}",
2075   .function = map_pre_resolve_command_fn,
2076 };
2077
2078 /*?
2079  * Enable or disable the MAP-E inbound security check
2080  * Specifiy if the inbound security check should be done on fragments
2081  *
2082  * @cliexpar
2083  * @cliexstart{map params security-check}
2084  *
2085  * By default, a decapsulated packet's IPv4 source address will be
2086  * verified against the outer header's IPv6 source address. Disabling
2087  * this feature will allow IPv4 source address spoofing.
2088  *
2089  * Typically the inbound on-decapsulation security check is only done
2090  * on the first packet. The packet that contains the L4
2091  * information. While a security check on every fragment is possible,
2092  * it has a cost. State must be created on the first fragment.
2093  * @cliexend
2094  ?*/
2095 VLIB_CLI_COMMAND(map_security_check_command, static) = {
2096   .path = "map params security-check",
2097   .short_help = "map params security-check enable|disable fragments on|off",
2098   .function = map_security_check_command_fn,
2099 };
2100
2101
2102 /*?
2103  * Specifiy the IPv4 source address used for relayed ICMP error messages
2104  *
2105  * @cliexpar
2106  * @cliexstart{map params icmp source-address}
2107  *
2108  * This command specifies which IPv4 source address (must be local to
2109  * the system), that is used for relayed received IPv6 ICMP error
2110  * messages.
2111  * @cliexend
2112  ?*/
2113 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2114   .path = "map params icmp source-address",
2115   .short_help = "map params icmp source-address <ip4-address>",
2116   .function = map_icmp_relay_source_address_command_fn,
2117 };
2118
2119 /*?
2120  * Send IPv6 ICMP unreachables
2121  *
2122  * @cliexpar
2123  * @cliexstart{map params icmp6 unreachables}
2124  *
2125  * Send IPv6 ICMP unreachable messages back if security check fails or
2126  * no MAP domain exists.
2127  * @cliexend
2128  ?*/
2129 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2130   .path = "map params icmp6 unreachables",
2131   .short_help = "map params icmp6 unreachables {on|off}",
2132   .function = map_icmp_unreachables_command_fn,
2133 };
2134
2135 /*?
2136  * Configure MAP fragmentation behaviour
2137  *
2138  * @cliexpar
2139  * @cliexstart{map params fragment}
2140  *
2141  * Allows fragmentation of the IPv4 packet even if the DF bit is
2142  * set. The choice between inner or outer fragmentation of tunnel
2143  * packets is complicated. The benefit of inner fragmentation is that
2144  * the ultimate endpoint must reassemble, instead of the tunnel
2145  * endpoint.
2146  * @cliexend
2147  ?*/
2148 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2149   .path = "map params fragment",
2150   .short_help = "map params fragment inner|outer ignore-df|honor-df",
2151   .function = map_fragment_command_fn,
2152 };
2153
2154
2155 /*?
2156  * Add MAP domain
2157  *
2158  * @cliexpar
2159  * @cliexstart{map add domain}
2160  * @cliexend
2161  ?*/
2162 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2163   .path = "map add domain",
2164   .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2165       "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2166       "[map-t] [mtu <mtu>]",
2167   .function = map_add_domain_command_fn,
2168 };
2169
2170 /*?
2171  * Add MAP rule to a domain
2172  *
2173  * @cliexpar
2174  * @cliexstart{map add rule}
2175  * @cliexend
2176  ?*/
2177 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2178   .path = "map add rule",
2179   .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2180   .function = map_add_rule_command_fn,
2181 };
2182
2183 /*?
2184  * Delete MAP domain
2185  *
2186  * @cliexpar
2187  * @cliexstart{map del domain}
2188  * @cliexend
2189  ?*/
2190 VLIB_CLI_COMMAND(map_del_command, static) = {
2191   .path = "map del domain",
2192   .short_help = "map del domain index <domain>",
2193   .function = map_del_domain_command_fn,
2194 };
2195
2196 /*?
2197  * Show MAP domains
2198  *
2199  * @cliexpar
2200  * @cliexstart{show map domain}
2201  * @cliexend
2202  ?*/
2203 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2204   .path = "show map domain",
2205   .short_help = "show map domain index <n> [counters]",
2206   .function = show_map_domain_command_fn,
2207 };
2208
2209 /*?
2210  * Show MAP statistics
2211  *
2212  * @cliexpar
2213  * @cliexstart{show map stats}
2214  * @cliexend
2215  ?*/
2216 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2217   .path = "show map stats",
2218   .short_help = "show map stats",
2219   .function = show_map_stats_command_fn,
2220 };
2221
2222 /*?
2223  * Show MAP fragmentation information
2224  *
2225  * @cliexpar
2226  * @cliexstart{show map fragments}
2227  * @cliexend
2228  ?*/
2229 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2230   .path = "show map fragments",
2231   .short_help = "show map fragments",
2232   .function = show_map_fragments_command_fn,
2233 };
2234
2235 /*?
2236  * Enable MAP processing on interface (input feature)
2237  *
2238  ?*/
2239 VLIB_CLI_COMMAND(map_if_command, static) = {
2240   .path = "map interface",
2241   .short_help = "map interface <interface-name> [map-t] [del]",
2242   .function = map_if_command_fn,
2243 };
2244
2245 VLIB_PLUGIN_REGISTER() = {
2246   .version = VPP_BUILD_VER,
2247   .description = "Mapping of address and port (MAP)",
2248 };
2249
2250 /* *INDENT-ON* */
2251
2252 /*
2253  * map_init
2254  */
2255 clib_error_t *
2256 map_init (vlib_main_t * vm)
2257 {
2258   map_main_t *mm = &map_main;
2259   clib_error_t *error = 0;
2260   mm->vnet_main = vnet_get_main ();
2261   mm->vlib_main = vm;
2262
2263 #ifdef MAP_SKIP_IP6_LOOKUP
2264   fib_protocol_t proto;
2265
2266   FOR_EACH_FIB_PROTOCOL (proto)
2267   {
2268     map_pre_resolve_init (&pre_resolved[proto]);
2269   }
2270 #endif
2271
2272   /* traffic class */
2273   mm->tc = 0;
2274   mm->tc_copy = true;
2275
2276   /* Inbound security check */
2277   mm->sec_check = true;
2278   mm->sec_check_frag = false;
2279
2280   /* ICMP6 Type 1, Code 5 for security check failure */
2281   mm->icmp6_enabled = false;
2282
2283   /* Inner or outer fragmentation */
2284   mm->frag_inner = false;
2285   mm->frag_ignore_df = false;
2286
2287   vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2288   mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
2289   mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
2290
2291   vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2292   vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2293
2294   /* IP4 virtual reassembly */
2295   mm->ip4_reass_hash_table = 0;
2296   mm->ip4_reass_pool = 0;
2297   mm->ip4_reass_lock =
2298     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2299   *mm->ip4_reass_lock = 0;
2300   mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2301   mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2302   mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2303   mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2304   mm->ip4_reass_ht_log2len =
2305     map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2306                         mm->ip4_reass_conf_pool_size);
2307   mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2308   map_ip4_reass_reinit (NULL, NULL);
2309
2310   /* IP6 virtual reassembly */
2311   mm->ip6_reass_hash_table = 0;
2312   mm->ip6_reass_pool = 0;
2313   mm->ip6_reass_lock =
2314     clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2315   *mm->ip6_reass_lock = 0;
2316   mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2317   mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2318   mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2319   mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2320   mm->ip6_reass_ht_log2len =
2321     map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2322                         mm->ip6_reass_conf_pool_size);
2323   mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2324   map_ip6_reass_reinit (NULL, NULL);
2325
2326 #ifdef MAP_SKIP_IP6_LOOKUP
2327   fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2328 #endif
2329   map_dpo_module_init ();
2330
2331   error = map_plugin_api_hookup (vm);
2332
2333   return error;
2334 }
2335
2336 VLIB_INIT_FUNCTION (map_init);
2337
2338 /*
2339  * fd.io coding-style-patch-verification: ON
2340  *
2341  * Local Variables:
2342  * eval: (c-set-style "gnu")
2343  * End:
2344  */