vcl: allow more rx events on peek
[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/fib_entry_track.h>
20 #include <vnet/fib/ip6_fib.h>
21 #include <vnet/adj/adj.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  * Save user-assigned MAP domain names ("tags") in a vector of
65  * extra domain information.
66  */
67 static void
68 map_save_extras (u32 map_domain_index, u8 * tag)
69 {
70   map_main_t *mm = &map_main;
71   map_domain_extra_t *de;
72
73   if (map_domain_index == ~0)
74     return;
75
76   vec_validate (mm->domain_extras, map_domain_index);
77   de = vec_elt_at_index (mm->domain_extras, map_domain_index);
78   clib_memset (de, 0, sizeof (*de));
79
80   if (!tag)
81     return;
82
83   vec_validate_init_c_string (de->tag, tag, strlen ((char *) tag));
84 }
85
86
87 static void
88 map_free_extras (u32 map_domain_index)
89 {
90   map_main_t *mm = &map_main;
91   map_domain_extra_t *de;
92
93   if (map_domain_index == ~0)
94     return;
95
96   if (map_domain_index >= vec_len (mm->domain_extras))
97     return;
98
99   de = vec_elt_at_index (mm->domain_extras, map_domain_index);
100   vec_free (de->tag);
101 }
102
103
104 int
105 map_create_domain (ip4_address_t * ip4_prefix,
106                    u8 ip4_prefix_len,
107                    ip6_address_t * ip6_prefix,
108                    u8 ip6_prefix_len,
109                    ip6_address_t * ip6_src,
110                    u8 ip6_src_len,
111                    u8 ea_bits_len,
112                    u8 psid_offset,
113                    u8 psid_length,
114                    u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag)
115 {
116   u8 suffix_len, suffix_shift;
117   map_main_t *mm = &map_main;
118   map_domain_t *d;
119
120   /* How many, and which bits to grab from the IPv4 DA */
121   if (ip4_prefix_len + ea_bits_len < 32)
122     {
123       flags |= MAP_DOMAIN_PREFIX;
124       suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
125       suffix_len = ea_bits_len;
126     }
127   else
128     {
129       suffix_shift = 0;
130       suffix_len = 32 - ip4_prefix_len;
131     }
132
133   /* EA bits must be within the first 64 bits */
134   if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
135                           ip6_prefix_len + suffix_len + psid_length > 64))
136     {
137       clib_warning
138         ("Embedded Address bits must be within the first 64 bits of "
139          "the IPv6 prefix");
140       return -1;
141     }
142
143   /* Get domain index */
144   pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
145   clib_memset (d, 0, sizeof (*d));
146   *map_domain_index = d - mm->domains;
147
148   /* Init domain struct */
149   d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
150   d->ip4_prefix_len = ip4_prefix_len;
151   d->ip6_prefix = *ip6_prefix;
152   d->ip6_prefix_len = ip6_prefix_len;
153   d->ip6_src = *ip6_src;
154   d->ip6_src_len = ip6_src_len;
155   d->ea_bits_len = ea_bits_len;
156   d->psid_offset = psid_offset;
157   d->psid_length = psid_length;
158   d->mtu = mtu;
159   d->flags = flags;
160   d->suffix_shift = suffix_shift;
161   d->suffix_mask = (1 << suffix_len) - 1;
162
163   d->psid_shift = 16 - psid_length - psid_offset;
164   d->psid_mask = (1 << d->psid_length) - 1;
165   d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
166
167   /* Save a user-assigned MAP domain name if provided. */
168   if (tag)
169     map_save_extras (*map_domain_index, tag);
170
171   /* MAP longest match lookup table (input feature / FIB) */
172   mm->ip4_prefix_tbl->add (mm->ip4_prefix_tbl, &d->ip4_prefix,
173                            d->ip4_prefix_len, *map_domain_index);
174
175   /* Really needed? Or always use FIB? */
176   mm->ip6_src_prefix_tbl->add (mm->ip6_src_prefix_tbl, &d->ip6_src,
177                                d->ip6_src_len, *map_domain_index);
178
179   /* Let's build a table with the MAP rule ip6 prefixes as well [dgeist] */
180   mm->ip6_prefix_tbl->add (mm->ip6_prefix_tbl, &d->ip6_prefix,
181                            d->ip6_prefix_len, *map_domain_index);
182
183   /* Validate packet/byte counters */
184   map_domain_counter_lock (mm);
185   int i;
186   for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
187     {
188       vlib_validate_simple_counter (&mm->simple_domain_counters[i],
189                                     *map_domain_index);
190       vlib_zero_simple_counter (&mm->simple_domain_counters[i],
191                                 *map_domain_index);
192     }
193   for (i = 0; i < vec_len (mm->domain_counters); i++)
194     {
195       vlib_validate_combined_counter (&mm->domain_counters[i],
196                                       *map_domain_index);
197       vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
198     }
199   map_domain_counter_unlock (mm);
200
201   return 0;
202 }
203
204 /*
205  * map_delete_domain
206  */
207 int
208 map_delete_domain (u32 map_domain_index)
209 {
210   map_main_t *mm = &map_main;
211   map_domain_t *d;
212
213   if (pool_is_free_index (mm->domains, map_domain_index))
214     {
215       clib_warning ("MAP domain delete: domain does not exist: %d",
216                     map_domain_index);
217       return -1;
218     }
219
220   d = pool_elt_at_index (mm->domains, map_domain_index);
221   mm->ip4_prefix_tbl->delete (mm->ip4_prefix_tbl, &d->ip4_prefix,
222                               d->ip4_prefix_len);
223   mm->ip6_src_prefix_tbl->delete (mm->ip6_src_prefix_tbl, &d->ip6_src,
224                                   d->ip6_src_len);
225   /* Addition to remove the new table [dgeist] */
226   mm->ip6_prefix_tbl->delete (mm->ip6_prefix_tbl, &d->ip6_prefix,
227                               d->ip6_prefix_len);
228
229   /* Release user-assigned MAP domain name. */
230   map_free_extras (map_domain_index);
231
232   /* Deleting rules */
233   if (d->rules)
234     clib_mem_free (d->rules);
235
236   pool_put (mm->domains, d);
237
238   return 0;
239 }
240
241 int
242 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
243                   bool is_add)
244 {
245   map_domain_t *d;
246   map_main_t *mm = &map_main;
247
248   if (pool_is_free_index (mm->domains, map_domain_index))
249     {
250       clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
251       return -1;
252     }
253   d = pool_elt_at_index (mm->domains, map_domain_index);
254
255   /* Rules are only used in 1:1 independent case */
256   if (d->ea_bits_len > 0)
257     return (-1);
258
259   if (!d->rules)
260     {
261       u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
262       d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
263       if (!d->rules)
264         return -1;
265       clib_memset (d->rules, 0, l);
266     }
267
268   if (psid >= (0x1 << d->psid_length))
269     {
270       clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
271                     0x1 << d->psid_length);
272       return -1;
273     }
274
275   if (is_add)
276     {
277       d->rules[psid] = *tep;
278     }
279   else
280     {
281       clib_memset (&d->rules[psid], 0, sizeof (ip6_address_t));
282     }
283   return 0;
284 }
285
286 #ifdef MAP_SKIP_IP6_LOOKUP
287 /**
288  * Pre-resolved per-protocol global next-hops
289  */
290 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
291
292 static void
293 map_pre_resolve_init (map_main_pre_resolved_t * pr)
294 {
295   pr->fei = FIB_NODE_INDEX_INVALID;
296   fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
297 }
298
299 static u8 *
300 format_map_pre_resolve (u8 * s, va_list * ap)
301 {
302   map_main_pre_resolved_t *pr = va_arg (*ap, map_main_pre_resolved_t *);
303
304   if (FIB_NODE_INDEX_INVALID != pr->fei)
305     {
306       const fib_prefix_t *pfx;
307
308       pfx = fib_entry_get_prefix (pr->fei);
309
310       return (format (s, "%U (%u)",
311                       format_ip46_address, &pfx->fp_addr, IP46_TYPE_ANY,
312                       pr->dpo.dpoi_index));
313     }
314   else
315     {
316       return (format (s, "un-set"));
317     }
318 }
319
320
321 /**
322  * Function definition to inform the FIB node that its last lock has gone.
323  */
324 static void
325 map_last_lock_gone (fib_node_t * node)
326 {
327   /*
328    * The MAP is a root of the graph. As such
329    * it never has children and thus is never locked.
330    */
331   ASSERT (0);
332 }
333
334 static map_main_pre_resolved_t *
335 map_from_fib_node (fib_node_t * node)
336 {
337   ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
338   return ((map_main_pre_resolved_t *)
339           (((char *) node) -
340            STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
341 }
342
343 static void
344 map_stack (map_main_pre_resolved_t * pr)
345 {
346   const dpo_id_t *dpo;
347
348   dpo = fib_entry_contribute_ip_forwarding (pr->fei);
349
350   dpo_copy (&pr->dpo, dpo);
351 }
352
353 /**
354  * Function definition to backwalk a FIB node
355  */
356 static fib_node_back_walk_rc_t
357 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
358 {
359   map_stack (map_from_fib_node (node));
360
361   return (FIB_NODE_BACK_WALK_CONTINUE);
362 }
363
364 /**
365  * Function definition to get a FIB node from its index
366  */
367 static fib_node_t *
368 map_fib_node_get (fib_node_index_t index)
369 {
370   return (&pre_resolved[index].node);
371 }
372
373 /*
374  * Virtual function table registered by MPLS GRE tunnels
375  * for participation in the FIB object graph.
376  */
377 const static fib_node_vft_t map_vft = {
378   .fnv_get = map_fib_node_get,
379   .fnv_last_lock = map_last_lock_gone,
380   .fnv_back_walk = map_back_walk,
381 };
382
383 static void
384 map_fib_resolve (map_main_pre_resolved_t * pr,
385                  fib_protocol_t proto, u8 len, const ip46_address_t * addr)
386 {
387   fib_prefix_t pfx = {
388     .fp_proto = proto,
389     .fp_len = len,
390     .fp_addr = *addr,
391   };
392
393   pr->fei = fib_entry_track (0, // default fib
394                              &pfx, FIB_NODE_TYPE_MAP_E, proto, &pr->sibling);
395   map_stack (pr);
396 }
397
398 static void
399 map_fib_unresolve (map_main_pre_resolved_t * pr,
400                    fib_protocol_t proto, u8 len, const ip46_address_t * addr)
401 {
402   if (pr->fei != FIB_NODE_INDEX_INVALID)
403     {
404       fib_entry_untrack (pr->fei, pr->sibling);
405
406       dpo_reset (&pr->dpo);
407
408       pr->fei = FIB_NODE_INDEX_INVALID;
409       pr->sibling = FIB_NODE_INDEX_INVALID;
410     }
411 }
412
413 void
414 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del)
415 {
416   if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
417     {
418       ip46_address_t addr = {
419         .ip6 = *ip6,
420       };
421       if (is_del)
422         map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
423                            FIB_PROTOCOL_IP6, 128, &addr);
424       else
425         map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
426                          FIB_PROTOCOL_IP6, 128, &addr);
427     }
428   if (ip4 && (ip4->as_u32 != 0))
429     {
430       ip46_address_t addr = {
431         .ip4 = *ip4,
432       };
433       if (is_del)
434         map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
435                            FIB_PROTOCOL_IP4, 32, &addr);
436       else
437         map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
438                          FIB_PROTOCOL_IP4, 32, &addr);
439     }
440 }
441 #endif
442
443 static clib_error_t *
444 map_security_check_command_fn (vlib_main_t * vm,
445                                unformat_input_t * input,
446                                vlib_cli_command_t * cmd)
447 {
448   unformat_input_t _line_input, *line_input = &_line_input;
449   clib_error_t *error = NULL;
450   bool enable = false;
451   bool check_frag = false;
452   bool saw_enable = false;
453   bool saw_frag = false;
454
455   /* Get a line of input. */
456   if (!unformat_user (input, unformat_line_input, line_input))
457     return 0;
458
459   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
460     {
461       if (unformat (line_input, "enable"))
462         {
463           enable = true;
464           saw_enable = true;
465         }
466       else if (unformat (line_input, "disable"))
467         {
468           enable = false;
469           saw_enable = true;
470         }
471       else if (unformat (line_input, "fragments on"))
472         {
473           check_frag = true;
474           saw_frag = true;
475         }
476       else if (unformat (line_input, "fragments off"))
477         {
478           check_frag = false;
479           saw_frag = true;
480         }
481       else
482         {
483           error = clib_error_return (0, "unknown input `%U'",
484                                      format_unformat_error, line_input);
485           goto done;
486         }
487     }
488
489   if (!saw_enable)
490     {
491       error = clib_error_return (0,
492                                  "Must specify enable 'enable' or 'disable'");
493       goto done;
494     }
495
496   if (!saw_frag)
497     {
498       error = clib_error_return (0, "Must specify fragments 'on' or 'off'");
499       goto done;
500     }
501
502   map_param_set_security_check (enable, check_frag);
503
504 done:
505   unformat_free (line_input);
506
507   return error;
508 }
509
510
511 static clib_error_t *
512 map_add_domain_command_fn (vlib_main_t * vm,
513                            unformat_input_t * input, vlib_cli_command_t * cmd)
514 {
515   unformat_input_t _line_input, *line_input = &_line_input;
516   ip4_address_t ip4_prefix;
517   ip6_address_t ip6_prefix;
518   ip6_address_t ip6_src;
519   u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
520   u32 num_m_args = 0;
521   /* Optional arguments */
522   u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
523   u32 mtu = 0;
524   u8 flags = 0;
525   u8 *tag = 0;
526   ip6_src_len = 128;
527   clib_error_t *error = NULL;
528
529   /* Get a line of input. */
530   if (!unformat_user (input, unformat_line_input, line_input))
531     return 0;
532
533   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
534     {
535       if (unformat
536           (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
537            &ip4_prefix_len))
538         num_m_args++;
539       else
540         if (unformat
541             (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
542              &ip6_prefix_len))
543         num_m_args++;
544       else
545         if (unformat
546             (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
547              &ip6_src_len))
548         num_m_args++;
549       else
550         if (unformat
551             (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
552         num_m_args++;
553       else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
554         num_m_args++;
555       else if (unformat (line_input, "psid-offset %d", &psid_offset))
556         num_m_args++;
557       else if (unformat (line_input, "psid-len %d", &psid_length))
558         num_m_args++;
559       else if (unformat (line_input, "mtu %d", &mtu))
560         num_m_args++;
561       else if (unformat (line_input, "tag %s", &tag))
562         ;
563       else
564         {
565           error = clib_error_return (0, "unknown input `%U'",
566                                      format_unformat_error, line_input);
567           goto done;
568         }
569     }
570
571   if (num_m_args < 3)
572     {
573       error = clib_error_return (0, "mandatory argument(s) missing");
574       goto done;
575     }
576
577   map_create_domain (&ip4_prefix, ip4_prefix_len,
578                      &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
579                      ea_bits_len, psid_offset, psid_length, &map_domain_index,
580                      mtu, flags, tag);
581
582 done:
583   vec_free (tag);
584   unformat_free (line_input);
585
586   return error;
587 }
588
589 static clib_error_t *
590 map_del_domain_command_fn (vlib_main_t * vm,
591                            unformat_input_t * input, vlib_cli_command_t * cmd)
592 {
593   unformat_input_t _line_input, *line_input = &_line_input;
594   u32 num_m_args = 0;
595   u32 map_domain_index;
596   clib_error_t *error = NULL;
597
598   /* Get a line of input. */
599   if (!unformat_user (input, unformat_line_input, line_input))
600     return 0;
601
602   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
603     {
604       if (unformat (line_input, "index %d", &map_domain_index))
605         num_m_args++;
606       else
607         {
608           error = clib_error_return (0, "unknown input `%U'",
609                                      format_unformat_error, line_input);
610           goto done;
611         }
612     }
613
614   if (num_m_args != 1)
615     {
616       error = clib_error_return (0, "mandatory argument(s) missing");
617       goto done;
618     }
619
620   map_delete_domain (map_domain_index);
621
622 done:
623   unformat_free (line_input);
624
625   return error;
626 }
627
628 static clib_error_t *
629 map_add_rule_command_fn (vlib_main_t * vm,
630                          unformat_input_t * input, vlib_cli_command_t * cmd)
631 {
632   unformat_input_t _line_input, *line_input = &_line_input;
633   ip6_address_t tep;
634   u32 num_m_args = 0;
635   u32 psid = 0, map_domain_index;
636   clib_error_t *error = NULL;
637
638   /* Get a line of input. */
639   if (!unformat_user (input, unformat_line_input, line_input))
640     return 0;
641
642   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
643     {
644       if (unformat (line_input, "index %d", &map_domain_index))
645         num_m_args++;
646       else if (unformat (line_input, "psid %d", &psid))
647         num_m_args++;
648       else
649         if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
650         num_m_args++;
651       else
652         {
653           error = clib_error_return (0, "unknown input `%U'",
654                                      format_unformat_error, line_input);
655           goto done;
656         }
657     }
658
659   if (num_m_args != 3)
660     {
661       error = clib_error_return (0, "mandatory argument(s) missing");
662       goto done;
663     }
664
665   if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
666     {
667       error = clib_error_return (0, "Failing to add Mapping Rule");
668       goto done;
669     }
670
671 done:
672   unformat_free (line_input);
673
674   return error;
675 }
676
677 #if MAP_SKIP_IP6_LOOKUP
678 static clib_error_t *
679 map_pre_resolve_command_fn (vlib_main_t * vm,
680                             unformat_input_t * input,
681                             vlib_cli_command_t * cmd)
682 {
683   unformat_input_t _line_input, *line_input = &_line_input;
684   ip4_address_t ip4nh, *p_v4 = NULL;
685   ip6_address_t ip6nh, *p_v6 = NULL;
686   clib_error_t *error = NULL;
687   bool is_del = false;
688
689   clib_memset (&ip4nh, 0, sizeof (ip4nh));
690   clib_memset (&ip6nh, 0, sizeof (ip6nh));
691
692   /* Get a line of input. */
693   if (!unformat_user (input, unformat_line_input, line_input))
694     return 0;
695
696   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
697     {
698       if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
699         p_v4 = &ip4nh;
700       else
701         if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
702         p_v6 = &ip6nh;
703       else if (unformat (line_input, "del"))
704         is_del = true;
705       else
706         {
707           error = clib_error_return (0, "unknown input `%U'",
708                                      format_unformat_error, line_input);
709           goto done;
710         }
711     }
712
713   map_pre_resolve (p_v4, p_v6, is_del);
714
715 done:
716   unformat_free (line_input);
717
718   return error;
719 }
720 #endif
721
722 static clib_error_t *
723 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
724                                           unformat_input_t * input,
725                                           vlib_cli_command_t * cmd)
726 {
727   unformat_input_t _line_input, *line_input = &_line_input;
728   ip4_address_t icmp_src_address;
729   ip4_address_t *p_icmp_addr = 0;
730   map_main_t *mm = &map_main;
731   clib_error_t *error = NULL;
732
733   mm->icmp4_src_address.as_u32 = 0;
734
735   /* Get a line of input. */
736   if (!unformat_user (input, unformat_line_input, line_input))
737     return 0;
738
739   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
740     {
741       if (unformat
742           (line_input, "%U", unformat_ip4_address, &icmp_src_address))
743         {
744           mm->icmp4_src_address = icmp_src_address;
745           p_icmp_addr = &icmp_src_address;
746         }
747       else
748         {
749           error = clib_error_return (0, "unknown input `%U'",
750                                      format_unformat_error, line_input);
751           goto done;
752         }
753     }
754
755   map_param_set_icmp (p_icmp_addr);
756
757 done:
758   unformat_free (line_input);
759
760   return error;
761 }
762
763 static clib_error_t *
764 map_icmp_unreachables_command_fn (vlib_main_t * vm,
765                                   unformat_input_t * input,
766                                   vlib_cli_command_t * cmd)
767 {
768   unformat_input_t _line_input, *line_input = &_line_input;
769   int num_m_args = 0;
770   clib_error_t *error = NULL;
771   bool enabled = false;
772
773   /* Get a line of input. */
774   if (!unformat_user (input, unformat_line_input, line_input))
775     return 0;
776
777   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
778     {
779       num_m_args++;
780       if (unformat (line_input, "on"))
781         enabled = true;
782       else if (unformat (line_input, "off"))
783         enabled = false;
784       else
785         {
786           error = clib_error_return (0, "unknown input `%U'",
787                                      format_unformat_error, line_input);
788           goto done;
789         }
790     }
791
792
793   if (num_m_args != 1)
794     error = clib_error_return (0, "mandatory argument(s) missing");
795
796
797   map_param_set_icmp6 (enabled);
798
799 done:
800   unformat_free (line_input);
801
802   return error;
803 }
804
805
806 static clib_error_t *
807 map_fragment_command_fn (vlib_main_t * vm,
808                          unformat_input_t * input, vlib_cli_command_t * cmd)
809 {
810   unformat_input_t _line_input, *line_input = &_line_input;
811   clib_error_t *error = NULL;
812   bool frag_inner = false;
813   bool frag_ignore_df = false;
814   bool saw_in_out = false;
815   bool saw_df = false;
816
817   /* Get a line of input. */
818   if (!unformat_user (input, unformat_line_input, line_input))
819     return 0;
820
821   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
822     {
823       if (unformat (line_input, "inner"))
824         {
825           frag_inner = true;
826           saw_in_out = true;
827         }
828       else if (unformat (line_input, "outer"))
829         {
830           frag_inner = false;
831           saw_in_out = true;
832         }
833       else if (unformat (line_input, "ignore-df"))
834         {
835           frag_ignore_df = true;
836           saw_df = true;
837         }
838       else if (unformat (line_input, "honor-df"))
839         {
840           frag_ignore_df = false;
841           saw_df = true;
842         }
843       else
844         {
845           error = clib_error_return (0, "unknown input `%U'",
846                                      format_unformat_error, line_input);
847           goto done;
848         }
849     }
850
851   if (!saw_in_out)
852     {
853       error = clib_error_return (0, "Must specify 'inner' or 'outer'");
854       goto done;
855     }
856
857   if (!saw_df)
858     {
859       error = clib_error_return (0, "Must specify 'ignore-df' or 'honor-df'");
860       goto done;
861     }
862
863   map_param_set_fragmentation (frag_inner, frag_ignore_df);
864
865 done:
866   unformat_free (line_input);
867
868   return error;
869 }
870
871 static clib_error_t *
872 map_traffic_class_command_fn (vlib_main_t * vm,
873                               unformat_input_t * input,
874                               vlib_cli_command_t * cmd)
875 {
876   unformat_input_t _line_input, *line_input = &_line_input;
877   u32 tc = 0;
878   clib_error_t *error = NULL;
879   bool tc_copy = false;
880
881
882   /* Get a line of input. */
883   if (!unformat_user (input, unformat_line_input, line_input))
884     return 0;
885
886   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
887     {
888       if (unformat (line_input, "copy"))
889         tc_copy = true;
890       else if (unformat (line_input, "%x", &tc))
891         tc = tc & 0xff;
892       else
893         {
894           error = clib_error_return (0, "unknown input `%U'",
895                                      format_unformat_error, line_input);
896           goto done;
897         }
898     }
899
900   map_param_set_traffic_class (tc_copy, tc);
901
902 done:
903   unformat_free (line_input);
904
905   return error;
906 }
907
908 static char *
909 map_flags_to_string (u32 flags)
910 {
911   if (flags & MAP_DOMAIN_PREFIX)
912     return "prefix";
913   return "";
914 }
915
916 static u8 *
917 format_map_domain (u8 * s, va_list * args)
918 {
919   map_domain_t *d = va_arg (*args, map_domain_t *);
920   bool counters = va_arg (*args, int);
921   map_main_t *mm = &map_main;
922   ip6_address_t ip6_prefix;
923   u32 map_domain_index = d - mm->domains;
924   map_domain_extra_t *de = 0;
925
926   if (d->rules)
927     clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
928   else
929     ip6_prefix = d->ip6_prefix;
930
931   if (map_domain_index < vec_len (mm->domain_extras))
932     de = vec_elt_at_index (mm->domain_extras, map_domain_index);
933
934   s = format (s,
935               "[%d] tag {%s} ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d "
936               "ea-bits-len %d psid-offset %d psid-len %d mtu %d %s",
937               map_domain_index, (de && de->tag) ? de->tag : (u8 *) "[no-tag]",
938               format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
939               format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
940               format_ip6_address, &d->ip6_src, d->ip6_src_len,
941               d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
942               map_flags_to_string (d->flags));
943
944   if (counters)
945     {
946       map_domain_counter_lock (mm);
947       vlib_counter_t v;
948       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
949                                  map_domain_index, &v);
950       s = format (s, "  TX: %lld/%lld", v.packets, v.bytes);
951       vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
952                                  map_domain_index, &v);
953       s = format (s, "  RX: %lld/%lld", v.packets, v.bytes);
954       map_domain_counter_unlock (mm);
955     }
956   s = format (s, "\n");
957
958   if (d->rules)
959     {
960       int i;
961       ip6_address_t dst;
962       for (i = 0; i < (0x1 << d->psid_length); i++)
963         {
964           dst = d->rules[i];
965           if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
966             continue;
967           s = format (s,
968                       " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
969                       &dst);
970         }
971     }
972   return s;
973 }
974
975 static clib_error_t *
976 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
977                             vlib_cli_command_t * cmd)
978 {
979   unformat_input_t _line_input, *line_input = &_line_input;
980   map_main_t *mm = &map_main;
981   map_domain_t *d;
982   bool counters = false;
983   u32 map_domain_index = ~0;
984   clib_error_t *error = NULL;
985
986   /* Get a line of input. */
987   if (!unformat_user (input, unformat_line_input, line_input))
988     {
989       pool_foreach (d, mm->domains)
990          {vlib_cli_output(vm, "%U", format_map_domain, d, counters);}
991       return 0;
992     }
993
994   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
995     {
996       if (unformat (line_input, "counters"))
997         counters = true;
998       else if (unformat (line_input, "index %d", &map_domain_index))
999         ;
1000       else
1001         {
1002           error = clib_error_return (0, "unknown input `%U'",
1003                                      format_unformat_error, line_input);
1004           goto done;
1005         }
1006     }
1007
1008   if (pool_elts (mm->domains) == 0)
1009     {
1010       vlib_cli_output (vm, "No MAP domains are configured...");
1011       goto done;
1012     }
1013
1014   if (map_domain_index == ~0)
1015     {
1016       pool_foreach (d, mm->domains)
1017          {vlib_cli_output(vm, "%U", format_map_domain, d, counters);}
1018     }
1019   else
1020     {
1021       if (pool_is_free_index (mm->domains, map_domain_index))
1022         {
1023           error = clib_error_return (0, "MAP domain does not exists %d",
1024                                      map_domain_index);
1025           goto done;
1026         }
1027
1028       d = pool_elt_at_index (mm->domains, map_domain_index);
1029       vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1030     }
1031
1032 done:
1033   unformat_free (line_input);
1034
1035   return error;
1036 }
1037
1038 u64
1039 map_error_counter_get (u32 node_index, map_error_t map_error)
1040 {
1041   vlib_main_t *vm = vlib_get_main ();
1042   vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1043   vlib_error_main_t *em = &vm->error_main;
1044   vlib_error_t e = error_node->errors[map_error];
1045   vlib_node_t *n = vlib_get_node (vm, node_index);
1046   u32 ci;
1047
1048   ci = vlib_error_get_code (&vm->node_main, e);
1049   ASSERT (ci < n->n_errors);
1050   ci += n->error_heap_index;
1051
1052   return (em->counters[ci]);
1053 }
1054
1055 static clib_error_t *
1056 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1057                            vlib_cli_command_t * cmd)
1058 {
1059   map_main_t *mm = &map_main;
1060   map_domain_t *d;
1061   int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1062   if (pool_elts (mm->domains) == 0)
1063     {
1064       vlib_cli_output (vm, "No MAP domains are configured...");
1065       return 0;
1066     }
1067
1068   pool_foreach (d, mm->domains)  {
1069     if (d->rules) {
1070       rulecount+= 0x1 << d->psid_length;
1071       rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1072     }
1073     domains += sizeof(*d);
1074     domaincount++;
1075   }
1076
1077   vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1078   vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1079   vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1080   vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1081
1082 #if MAP_SKIP_IP6_LOOKUP
1083   vlib_cli_output (vm,
1084                    "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1085                    format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1086                    format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1087
1088 #endif
1089
1090   if (mm->tc_copy)
1091     vlib_cli_output (vm, "MAP traffic-class: copy");
1092   else
1093     vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1094
1095   if (mm->tcp_mss)
1096     vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
1097
1098   vlib_cli_output (vm,
1099                    "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1100                    mm->sec_check ? "enabled" : "disabled",
1101                    mm->sec_check_frag ? "enabled" : "disabled");
1102
1103   vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1104                    format_ip4_address, &mm->icmp4_src_address);
1105   vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1106                    mm->icmp6_enabled ? "enabled" : "disabled");
1107   vlib_cli_output (vm, "Inner fragmentation: %s\n",
1108                    mm->frag_inner ? "enabled" : "disabled");
1109   vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1110                    mm->frag_ignore_df ? "enabled" : "disabled");
1111
1112   /*
1113    * Counters
1114    */
1115   vlib_combined_counter_main_t *cm = mm->domain_counters;
1116   u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1117   u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1118   int which, i;
1119   vlib_counter_t v;
1120
1121   clib_memset (total_pkts, 0, sizeof (total_pkts));
1122   clib_memset (total_bytes, 0, sizeof (total_bytes));
1123
1124   map_domain_counter_lock (mm);
1125   vec_foreach (cm, mm->domain_counters)
1126   {
1127     which = cm - mm->domain_counters;
1128
1129     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1130       {
1131         vlib_get_combined_counter (cm, i, &v);
1132         total_pkts[which] += v.packets;
1133         total_bytes[which] += v.bytes;
1134       }
1135   }
1136   map_domain_counter_unlock (mm);
1137
1138   vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1139                    total_pkts[MAP_DOMAIN_COUNTER_TX],
1140                    total_bytes[MAP_DOMAIN_COUNTER_TX]);
1141   vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1142                    total_pkts[MAP_DOMAIN_COUNTER_RX],
1143                    total_bytes[MAP_DOMAIN_COUNTER_RX]);
1144
1145   vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1146                    vlib_get_simple_counter (&mm->icmp_relayed, 0));
1147
1148   return 0;
1149 }
1150
1151 static clib_error_t *
1152 map_if_command_fn (vlib_main_t * vm,
1153                    unformat_input_t * input, vlib_cli_command_t * cmd)
1154 {
1155   unformat_input_t _line_input, *line_input = &_line_input;
1156   clib_error_t *error = NULL;
1157   bool is_enable = true, is_translation = false;
1158   vnet_main_t *vnm = vnet_get_main ();
1159   u32 sw_if_index = ~0;
1160
1161   /* Get a line of input. */
1162   if (!unformat_user (input, unformat_line_input, line_input))
1163     return 0;
1164
1165   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1166     {
1167       if (unformat
1168           (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1169         ;
1170       else if (unformat (line_input, "del"))
1171         is_enable = false;
1172       else if (unformat (line_input, "map-t"))
1173         is_translation = true;
1174       else
1175         {
1176           error = clib_error_return (0, "unknown input `%U'",
1177                                      format_unformat_error, line_input);
1178           goto done;
1179         }
1180     }
1181
1182 done:
1183   unformat_free (line_input);
1184
1185   if (sw_if_index == ~0)
1186     {
1187       error = clib_error_return (0, "unknown interface");
1188       return error;
1189     }
1190
1191   int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
1192   if (rv)
1193     {
1194       error = clib_error_return (0, "failure enabling MAP on interface");
1195     }
1196
1197   return error;
1198 }
1199
1200
1201 /*
1202  * packet trace format function
1203  */
1204 u8 *
1205 format_map_trace (u8 * s, va_list * args)
1206 {
1207   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1208   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1209   map_trace_t *t = va_arg (*args, map_trace_t *);
1210   u32 map_domain_index = t->map_domain_index;
1211   u16 port = t->port;
1212
1213   s =
1214     format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1215             clib_net_to_host_u16 (port));
1216
1217   return s;
1218 }
1219
1220 static clib_error_t *
1221 map_tcp_mss_command_fn (vlib_main_t * vm,
1222                         unformat_input_t * input, vlib_cli_command_t * cmd)
1223 {
1224   unformat_input_t _line_input, *line_input = &_line_input;
1225   clib_error_t *error = NULL;
1226   u32 tcp_mss = 0;
1227
1228   /* Get a line of input. */
1229   if (!unformat_user (input, unformat_line_input, line_input))
1230     return 0;
1231
1232   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1233     {
1234       if (unformat (line_input, "%u", &tcp_mss))
1235         ;
1236       else
1237         {
1238           error = clib_error_return (0, "unknown input `%U'",
1239                                      format_unformat_error, line_input);
1240           goto done;
1241         }
1242     }
1243
1244   if (tcp_mss >= (0x1 << 16))
1245     {
1246       error = clib_error_return (0, "invalid value `%u'", tcp_mss);
1247       goto done;
1248     }
1249
1250   map_param_set_tcp (tcp_mss);
1251
1252 done:
1253   unformat_free (line_input);
1254
1255   return error;
1256 }
1257
1258
1259
1260 /*?
1261  * Set or copy the IP TOS/Traffic Class field
1262  *
1263  * @cliexpar
1264  * @cliexstart{map params traffic-class}
1265  *
1266  * This command is used to set the traffic-class field in translated
1267  * or encapsulated packets. If copy is specifed (the default) then the
1268  * traffic-class/TOS field is copied from the original packet to the
1269  * translated / encapsulating header.
1270  * @cliexend
1271  ?*/
1272 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1273   .path = "map params traffic-class",
1274   .short_help = "map params traffic-class {0x0-0xff | copy}",
1275   .function = map_traffic_class_command_fn,
1276 };
1277
1278 /*?
1279  * TCP MSS clamping
1280  *
1281  * @cliexpar
1282  * @cliexstart{map params tcp-mss}
1283  *
1284  * This command is used to set the TCP MSS in translated
1285  * or encapsulated packets.
1286  * @cliexend
1287  ?*/
1288 VLIB_CLI_COMMAND(map_tcp_mss_command, static) = {
1289   .path = "map params tcp-mss",
1290   .short_help = "map params tcp-mss <value>",
1291   .function = map_tcp_mss_command_fn,
1292 };
1293
1294 /*?
1295  * Bypass IP4/IP6 lookup
1296  *
1297  * @cliexpar
1298  * @cliexstart{map params pre-resolve}
1299  *
1300  * Bypass a second FIB lookup of the translated or encapsulated
1301  * packet, and forward the packet directly to the specified
1302  * next-hop. This optimization trades forwarding flexibility for
1303  * performance.
1304  * @cliexend
1305  ?*/
1306 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1307   .path = "map params pre-resolve",
1308   .short_help = " map params pre-resolve {ip4-nh <address>} "
1309                 "| {ip6-nh <address>}",
1310   .function = map_pre_resolve_command_fn,
1311 };
1312
1313 /*?
1314  * Enable or disable the MAP-E inbound security check
1315  * Specify if the inbound security check should be done on fragments
1316  *
1317  * @cliexpar
1318  * @cliexstart{map params security-check}
1319  *
1320  * By default, a decapsulated packet's IPv4 source address will be
1321  * verified against the outer header's IPv6 source address. Disabling
1322  * this feature will allow IPv4 source address spoofing.
1323  *
1324  * Typically the inbound on-decapsulation security check is only done
1325  * on the first packet. The packet that contains the L4
1326  * information. While a security check on every fragment is possible,
1327  * it has a cost. State must be created on the first fragment.
1328  * @cliexend
1329  ?*/
1330 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1331   .path = "map params security-check",
1332   .short_help = "map params security-check enable|disable fragments on|off",
1333   .function = map_security_check_command_fn,
1334 };
1335
1336 /*?
1337  * Specify the IPv4 source address used for relayed ICMP error messages
1338  *
1339  * @cliexpar
1340  * @cliexstart{map params icmp source-address}
1341  *
1342  * This command specifies which IPv4 source address (must be local to
1343  * the system), that is used for relayed received IPv6 ICMP error
1344  * messages.
1345  * @cliexend
1346  ?*/
1347 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
1348   .path = "map params icmp source-address",
1349   .short_help = "map params icmp source-address <ip4-address>",
1350   .function = map_icmp_relay_source_address_command_fn,
1351 };
1352
1353 /*?
1354  * Send IPv6 ICMP unreachables
1355  *
1356  * @cliexpar
1357  * @cliexstart{map params icmp6 unreachables}
1358  *
1359  * Send IPv6 ICMP unreachable messages back if security check fails or
1360  * no MAP domain exists.
1361  * @cliexend
1362  ?*/
1363 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
1364   .path = "map params icmp6 unreachables",
1365   .short_help = "map params icmp6 unreachables {on|off}",
1366   .function = map_icmp_unreachables_command_fn,
1367 };
1368
1369 /*?
1370  * Configure MAP fragmentation behaviour
1371  *
1372  * @cliexpar
1373  * @cliexstart{map params fragment}
1374  *
1375  * Allows fragmentation of the IPv4 packet even if the DF bit is
1376  * set. The choice between inner or outer fragmentation of tunnel
1377  * packets is complicated. The benefit of inner fragmentation is that
1378  * the ultimate endpoint must reassemble, instead of the tunnel
1379  * endpoint.
1380  * @cliexend
1381  ?*/
1382 VLIB_CLI_COMMAND(map_fragment_command, static) = {
1383   .path = "map params fragment",
1384   .short_help = "map params fragment inner|outer ignore-df|honor-df",
1385   .function = map_fragment_command_fn,
1386 };
1387
1388
1389 /*?
1390  * Add MAP domain
1391  *
1392  * @cliexpar
1393  * @cliexstart{map add domain}
1394  * @cliexend
1395  ?*/
1396 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
1397   .path = "map add domain",
1398   .short_help = "map add domain [tag <tag>] ip4-pfx <ip4-pfx> "
1399       "ip6-pfx <ip6-pfx> "
1400       "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
1401       "[map-t] [mtu <mtu>]",
1402   .function = map_add_domain_command_fn,
1403 };
1404
1405 /*?
1406  * Add MAP rule to a domain
1407  *
1408  * @cliexpar
1409  * @cliexstart{map add rule}
1410  * @cliexend
1411  ?*/
1412 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
1413   .path = "map add rule",
1414   .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
1415   .function = map_add_rule_command_fn,
1416 };
1417
1418 /*?
1419  * Delete MAP domain
1420  *
1421  * @cliexpar
1422  * @cliexstart{map del domain}
1423  * @cliexend
1424  ?*/
1425 VLIB_CLI_COMMAND(map_del_command, static) = {
1426   .path = "map del domain",
1427   .short_help = "map del domain index <domain>",
1428   .function = map_del_domain_command_fn,
1429 };
1430
1431 /*?
1432  * Show MAP domains
1433  *
1434  * @cliexpar
1435  * @cliexstart{show map domain}
1436  * @cliexend
1437  ?*/
1438 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
1439   .path = "show map domain",
1440   .short_help = "show map domain index <n> [counters]",
1441   .function = show_map_domain_command_fn,
1442 };
1443
1444 /*?
1445  * Show MAP statistics
1446  *
1447  * @cliexpar
1448  * @cliexstart{show map stats}
1449  * @cliexend
1450  ?*/
1451 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
1452   .path = "show map stats",
1453   .short_help = "show map stats",
1454   .function = show_map_stats_command_fn,
1455 };
1456
1457 /*?
1458  * Enable MAP processing on interface (input feature)
1459  *
1460  ?*/
1461 VLIB_CLI_COMMAND(map_if_command, static) = {
1462   .path = "map interface",
1463   .short_help = "map interface <interface-name> [map-t] [del]",
1464   .function = map_if_command_fn,
1465 };
1466
1467 VLIB_PLUGIN_REGISTER() = {
1468   .version = VPP_BUILD_VER,
1469   .description = "Mapping of Address and Port (MAP)",
1470 };
1471
1472
1473 /*
1474  * map_init
1475  */
1476 clib_error_t *
1477 map_init (vlib_main_t * vm)
1478 {
1479   map_main_t *mm = &map_main;
1480   clib_error_t *error = 0;
1481
1482   memset (mm, 0, sizeof (*mm));
1483
1484   mm->vnet_main = vnet_get_main ();
1485   mm->vlib_main = vm;
1486
1487 #ifdef MAP_SKIP_IP6_LOOKUP
1488   fib_protocol_t proto;
1489
1490   FOR_EACH_FIB_PROTOCOL (proto)
1491   {
1492     map_pre_resolve_init (&pre_resolved[proto]);
1493   }
1494 #endif
1495
1496   /* traffic class */
1497   mm->tc = 0;
1498   mm->tc_copy = true;
1499
1500   /* Inbound security check */
1501   mm->sec_check = true;
1502   mm->sec_check_frag = false;
1503
1504   /* ICMP6 Type 1, Code 5 for security check failure */
1505   mm->icmp6_enabled = false;
1506
1507   /* Inner or outer fragmentation */
1508   mm->frag_inner = false;
1509   mm->frag_ignore_df = false;
1510
1511   vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
1512   mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
1513   mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
1514
1515   vlib_validate_simple_counter (&mm->icmp_relayed, 0);
1516   vlib_zero_simple_counter (&mm->icmp_relayed, 0);
1517   mm->icmp_relayed.stat_segment_name = "/map/icmp-relayed";
1518
1519   /* IP6 virtual reassembly */
1520
1521 #ifdef MAP_SKIP_IP6_LOOKUP
1522   fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
1523 #endif
1524
1525   /* LPM lookup tables */
1526   mm->ip4_prefix_tbl = lpm_table_init (LPM_TYPE_KEY32);
1527   mm->ip6_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
1528   mm->ip6_src_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
1529
1530   mm->bm_trans_enabled_by_sw_if = 0;
1531   mm->bm_encap_enabled_by_sw_if = 0;
1532
1533   error = map_plugin_api_hookup (vm);
1534
1535   return error;
1536 }
1537
1538 VLIB_INIT_FUNCTION (map_init);
1539
1540 /*
1541  * fd.io coding-style-patch-verification: ON
1542  *
1543  * Local Variables:
1544  * eval: (c-set-style "gnu")
1545  * End:
1546  */