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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
30 * This code supports the following MAP modes:
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
42 * Independent Shared IPv4 address (ea_bits_len = 0):
45 * Rule IPv6 address = 128, Rule PSID Set
46 * Independent Full IPv4 address (ea_bits_len = 0):
48 * psid_length = 0, ip6_prefix = 128
49 * Independent IPv4 prefix (ea_bits_len = 0):
51 * psid_length = 0, ip6_prefix = 128
56 * This code supports MAP-T:
58 * With a DMR prefix length of 64 or 96 (RFC6052).
64 * Save user-assigned MAP domain names ("tags") in a vector of
65 * extra domain information.
68 map_save_extras (u32 map_domain_index, u8 * tag)
70 map_main_t *mm = &map_main;
71 map_domain_extra_t *de;
73 if (map_domain_index == ~0)
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));
83 vec_validate_init_c_string (de->tag, tag, strlen ((char *) tag));
88 map_free_extras (u32 map_domain_index)
90 map_main_t *mm = &map_main;
91 map_domain_extra_t *de;
93 if (map_domain_index == ~0)
96 if (map_domain_index >= vec_len (mm->domain_extras))
99 de = vec_elt_at_index (mm->domain_extras, map_domain_index);
105 map_create_domain (ip4_address_t * ip4_prefix,
107 ip6_address_t * ip6_prefix,
109 ip6_address_t * ip6_src,
114 u32 * map_domain_index, u16 mtu, u8 flags, u8 * tag)
116 u8 suffix_len, suffix_shift;
117 map_main_t *mm = &map_main;
120 /* How many, and which bits to grab from the IPv4 DA */
121 if (ip4_prefix_len + ea_bits_len < 32)
123 flags |= MAP_DOMAIN_PREFIX;
124 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
125 suffix_len = ea_bits_len;
130 suffix_len = 32 - ip4_prefix_len;
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))
138 ("Embedded Address bits must be within the first 64 bits of "
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;
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;
160 d->suffix_shift = suffix_shift;
161 d->suffix_mask = (1 << suffix_len) - 1;
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;
167 /* Save a user-assigned MAP domain name if provided. */
169 map_save_extras (*map_domain_index, tag);
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);
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);
179 /* Validate packet/byte counters */
180 map_domain_counter_lock (mm);
182 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
184 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
186 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
189 for (i = 0; i < vec_len (mm->domain_counters); i++)
191 vlib_validate_combined_counter (&mm->domain_counters[i],
193 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
195 map_domain_counter_unlock (mm);
204 map_delete_domain (u32 map_domain_index)
206 map_main_t *mm = &map_main;
209 if (pool_is_free_index (mm->domains, map_domain_index))
211 clib_warning ("MAP domain delete: domain does not exist: %d",
216 d = pool_elt_at_index (mm->domains, map_domain_index);
217 mm->ip4_prefix_tbl->delete (mm->ip4_prefix_tbl, &d->ip4_prefix,
219 mm->ip6_src_prefix_tbl->delete (mm->ip6_src_prefix_tbl, &d->ip6_src,
222 /* Release user-assigned MAP domain name. */
223 map_free_extras (map_domain_index);
227 clib_mem_free (d->rules);
229 pool_put (mm->domains, d);
235 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
239 map_main_t *mm = &map_main;
241 if (pool_is_free_index (mm->domains, map_domain_index))
243 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
246 d = pool_elt_at_index (mm->domains, map_domain_index);
248 /* Rules are only used in 1:1 independent case */
249 if (d->ea_bits_len > 0)
254 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
255 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
258 clib_memset (d->rules, 0, l);
261 if (psid >= (0x1 << d->psid_length))
263 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
264 0x1 << d->psid_length);
270 d->rules[psid] = *tep;
274 clib_memset (&d->rules[psid], 0, sizeof (ip6_address_t));
279 #ifdef MAP_SKIP_IP6_LOOKUP
281 * Pre-resolved per-protocol global next-hops
283 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
286 map_pre_resolve_init (map_main_pre_resolved_t * pr)
288 pr->fei = FIB_NODE_INDEX_INVALID;
289 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
293 format_map_pre_resolve (u8 * s, va_list * ap)
295 map_main_pre_resolved_t *pr = va_arg (*ap, map_main_pre_resolved_t *);
297 if (FIB_NODE_INDEX_INVALID != pr->fei)
299 const fib_prefix_t *pfx;
301 pfx = fib_entry_get_prefix (pr->fei);
303 return (format (s, "%U (%u)",
304 format_ip46_address, &pfx->fp_addr, IP46_TYPE_ANY,
305 pr->dpo.dpoi_index));
309 return (format (s, "un-set"));
315 * Function definition to inform the FIB node that its last lock has gone.
318 map_last_lock_gone (fib_node_t * node)
321 * The MAP is a root of the graph. As such
322 * it never has children and thus is never locked.
327 static map_main_pre_resolved_t *
328 map_from_fib_node (fib_node_t * node)
330 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
331 return ((map_main_pre_resolved_t *)
333 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
337 map_stack (map_main_pre_resolved_t * pr)
341 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
343 dpo_copy (&pr->dpo, dpo);
347 * Function definition to backwalk a FIB node
349 static fib_node_back_walk_rc_t
350 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
352 map_stack (map_from_fib_node (node));
354 return (FIB_NODE_BACK_WALK_CONTINUE);
358 * Function definition to get a FIB node from its index
361 map_fib_node_get (fib_node_index_t index)
363 return (&pre_resolved[index].node);
367 * Virtual function table registered by MPLS GRE tunnels
368 * for participation in the FIB object graph.
370 const static fib_node_vft_t map_vft = {
371 .fnv_get = map_fib_node_get,
372 .fnv_last_lock = map_last_lock_gone,
373 .fnv_back_walk = map_back_walk,
377 map_fib_resolve (map_main_pre_resolved_t * pr,
378 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
386 pr->fei = fib_entry_track (0, // default fib
387 &pfx, FIB_NODE_TYPE_MAP_E, proto, &pr->sibling);
392 map_fib_unresolve (map_main_pre_resolved_t * pr,
393 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
395 if (pr->fei != FIB_NODE_INDEX_INVALID)
397 fib_entry_untrack (pr->fei, pr->sibling);
399 dpo_reset (&pr->dpo);
401 pr->fei = FIB_NODE_INDEX_INVALID;
402 pr->sibling = FIB_NODE_INDEX_INVALID;
407 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6, bool is_del)
409 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
411 ip46_address_t addr = {
415 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP6],
416 FIB_PROTOCOL_IP6, 128, &addr);
418 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
419 FIB_PROTOCOL_IP6, 128, &addr);
421 if (ip4 && (ip4->as_u32 != 0))
423 ip46_address_t addr = {
427 map_fib_unresolve (&pre_resolved[FIB_PROTOCOL_IP4],
428 FIB_PROTOCOL_IP4, 32, &addr);
430 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
431 FIB_PROTOCOL_IP4, 32, &addr);
436 static clib_error_t *
437 map_security_check_command_fn (vlib_main_t * vm,
438 unformat_input_t * input,
439 vlib_cli_command_t * cmd)
441 unformat_input_t _line_input, *line_input = &_line_input;
442 clib_error_t *error = NULL;
444 bool check_frag = false;
445 bool saw_enable = false;
446 bool saw_frag = false;
448 /* Get a line of input. */
449 if (!unformat_user (input, unformat_line_input, line_input))
452 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
454 if (unformat (line_input, "enable"))
459 else if (unformat (line_input, "disable"))
464 else if (unformat (line_input, "fragments on"))
469 else if (unformat (line_input, "fragments off"))
476 error = clib_error_return (0, "unknown input `%U'",
477 format_unformat_error, line_input);
484 error = clib_error_return (0,
485 "Must specify enable 'enable' or 'disable'");
491 error = clib_error_return (0, "Must specify fragments 'on' or 'off'");
495 map_param_set_security_check (enable, check_frag);
498 unformat_free (line_input);
504 static clib_error_t *
505 map_add_domain_command_fn (vlib_main_t * vm,
506 unformat_input_t * input, vlib_cli_command_t * cmd)
508 unformat_input_t _line_input, *line_input = &_line_input;
509 ip4_address_t ip4_prefix;
510 ip6_address_t ip6_prefix;
511 ip6_address_t ip6_src;
512 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
514 /* Optional arguments */
515 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
520 clib_error_t *error = NULL;
522 /* Get a line of input. */
523 if (!unformat_user (input, unformat_line_input, line_input))
526 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
529 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
534 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
539 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
544 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
546 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
548 else if (unformat (line_input, "psid-offset %d", &psid_offset))
550 else if (unformat (line_input, "psid-len %d", &psid_length))
552 else if (unformat (line_input, "mtu %d", &mtu))
554 else if (unformat (line_input, "tag %v", &tag))
558 error = clib_error_return (0, "unknown input `%U'",
559 format_unformat_error, line_input);
566 error = clib_error_return (0, "mandatory argument(s) missing");
570 map_create_domain (&ip4_prefix, ip4_prefix_len,
571 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
572 ea_bits_len, psid_offset, psid_length, &map_domain_index,
576 unformat_free (line_input);
581 static clib_error_t *
582 map_del_domain_command_fn (vlib_main_t * vm,
583 unformat_input_t * input, vlib_cli_command_t * cmd)
585 unformat_input_t _line_input, *line_input = &_line_input;
587 u32 map_domain_index;
588 clib_error_t *error = NULL;
590 /* Get a line of input. */
591 if (!unformat_user (input, unformat_line_input, line_input))
594 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
596 if (unformat (line_input, "index %d", &map_domain_index))
600 error = clib_error_return (0, "unknown input `%U'",
601 format_unformat_error, line_input);
608 error = clib_error_return (0, "mandatory argument(s) missing");
612 map_delete_domain (map_domain_index);
615 unformat_free (line_input);
620 static clib_error_t *
621 map_add_rule_command_fn (vlib_main_t * vm,
622 unformat_input_t * input, vlib_cli_command_t * cmd)
624 unformat_input_t _line_input, *line_input = &_line_input;
627 u32 psid = 0, map_domain_index;
628 clib_error_t *error = NULL;
630 /* Get a line of input. */
631 if (!unformat_user (input, unformat_line_input, line_input))
634 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
636 if (unformat (line_input, "index %d", &map_domain_index))
638 else if (unformat (line_input, "psid %d", &psid))
641 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
645 error = clib_error_return (0, "unknown input `%U'",
646 format_unformat_error, line_input);
653 error = clib_error_return (0, "mandatory argument(s) missing");
657 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
659 error = clib_error_return (0, "Failing to add Mapping Rule");
664 unformat_free (line_input);
669 #if MAP_SKIP_IP6_LOOKUP
670 static clib_error_t *
671 map_pre_resolve_command_fn (vlib_main_t * vm,
672 unformat_input_t * input,
673 vlib_cli_command_t * cmd)
675 unformat_input_t _line_input, *line_input = &_line_input;
676 ip4_address_t ip4nh, *p_v4 = NULL;
677 ip6_address_t ip6nh, *p_v6 = NULL;
678 clib_error_t *error = NULL;
681 clib_memset (&ip4nh, 0, sizeof (ip4nh));
682 clib_memset (&ip6nh, 0, sizeof (ip6nh));
684 /* Get a line of input. */
685 if (!unformat_user (input, unformat_line_input, line_input))
688 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
690 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
693 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
695 else if (unformat (line_input, "del"))
699 error = clib_error_return (0, "unknown input `%U'",
700 format_unformat_error, line_input);
705 map_pre_resolve (p_v4, p_v6, is_del);
708 unformat_free (line_input);
714 static clib_error_t *
715 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
716 unformat_input_t * input,
717 vlib_cli_command_t * cmd)
719 unformat_input_t _line_input, *line_input = &_line_input;
720 ip4_address_t icmp_src_address;
721 ip4_address_t *p_icmp_addr = 0;
722 map_main_t *mm = &map_main;
723 clib_error_t *error = NULL;
725 mm->icmp4_src_address.as_u32 = 0;
727 /* Get a line of input. */
728 if (!unformat_user (input, unformat_line_input, line_input))
731 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
734 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
736 mm->icmp4_src_address = icmp_src_address;
737 p_icmp_addr = &icmp_src_address;
741 error = clib_error_return (0, "unknown input `%U'",
742 format_unformat_error, line_input);
747 map_param_set_icmp (p_icmp_addr);
750 unformat_free (line_input);
755 static clib_error_t *
756 map_icmp_unreachables_command_fn (vlib_main_t * vm,
757 unformat_input_t * input,
758 vlib_cli_command_t * cmd)
760 unformat_input_t _line_input, *line_input = &_line_input;
762 clib_error_t *error = NULL;
763 bool enabled = false;
765 /* Get a line of input. */
766 if (!unformat_user (input, unformat_line_input, line_input))
769 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
772 if (unformat (line_input, "on"))
774 else if (unformat (line_input, "off"))
778 error = clib_error_return (0, "unknown input `%U'",
779 format_unformat_error, line_input);
786 error = clib_error_return (0, "mandatory argument(s) missing");
789 map_param_set_icmp6 (enabled);
792 unformat_free (line_input);
798 static clib_error_t *
799 map_fragment_command_fn (vlib_main_t * vm,
800 unformat_input_t * input, vlib_cli_command_t * cmd)
802 unformat_input_t _line_input, *line_input = &_line_input;
803 clib_error_t *error = NULL;
804 bool frag_inner = false;
805 bool frag_ignore_df = false;
806 bool saw_in_out = false;
809 /* Get a line of input. */
810 if (!unformat_user (input, unformat_line_input, line_input))
813 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
815 if (unformat (line_input, "inner"))
820 else if (unformat (line_input, "outer"))
825 else if (unformat (line_input, "ignore-df"))
827 frag_ignore_df = true;
830 else if (unformat (line_input, "honor-df"))
832 frag_ignore_df = false;
837 error = clib_error_return (0, "unknown input `%U'",
838 format_unformat_error, line_input);
845 error = clib_error_return (0, "Must specify 'inner' or 'outer'");
851 error = clib_error_return (0, "Must specify 'ignore-df' or 'honor-df'");
855 map_param_set_fragmentation (frag_inner, frag_ignore_df);
858 unformat_free (line_input);
863 static clib_error_t *
864 map_traffic_class_command_fn (vlib_main_t * vm,
865 unformat_input_t * input,
866 vlib_cli_command_t * cmd)
868 unformat_input_t _line_input, *line_input = &_line_input;
870 clib_error_t *error = NULL;
871 bool tc_copy = false;
874 /* Get a line of input. */
875 if (!unformat_user (input, unformat_line_input, line_input))
878 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
880 if (unformat (line_input, "copy"))
882 else if (unformat (line_input, "%x", &tc))
886 error = clib_error_return (0, "unknown input `%U'",
887 format_unformat_error, line_input);
892 map_param_set_traffic_class (tc_copy, tc);
895 unformat_free (line_input);
901 map_flags_to_string (u32 flags)
903 if (flags & MAP_DOMAIN_PREFIX)
909 format_map_domain (u8 * s, va_list * args)
911 map_domain_t *d = va_arg (*args, map_domain_t *);
912 bool counters = va_arg (*args, int);
913 map_main_t *mm = &map_main;
914 ip6_address_t ip6_prefix;
915 u32 map_domain_index = d - mm->domains;
916 map_domain_extra_t *de = 0;
919 clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
921 ip6_prefix = d->ip6_prefix;
923 if (map_domain_index < vec_len (mm->domain_extras))
924 de = vec_elt_at_index (mm->domain_extras, map_domain_index);
927 "[%d] tag {%s} ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d "
928 "ea-bits-len %d psid-offset %d psid-len %d mtu %d %s",
929 map_domain_index, (de && de->tag) ? de->tag : (u8 *) "[no-tag]",
930 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
931 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
932 format_ip6_address, &d->ip6_src, d->ip6_src_len,
933 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
934 map_flags_to_string (d->flags));
938 map_domain_counter_lock (mm);
940 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
941 map_domain_index, &v);
942 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
943 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
944 map_domain_index, &v);
945 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
946 map_domain_counter_unlock (mm);
948 s = format (s, "\n");
954 for (i = 0; i < (0x1 << d->psid_length); i++)
957 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
960 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
967 static clib_error_t *
968 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
969 vlib_cli_command_t * cmd)
971 unformat_input_t _line_input, *line_input = &_line_input;
972 map_main_t *mm = &map_main;
974 bool counters = false;
975 u32 map_domain_index = ~0;
976 clib_error_t *error = NULL;
978 /* Get a line of input. */
979 if (!unformat_user (input, unformat_line_input, line_input))
982 pool_foreach(d, mm->domains,
983 ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
988 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
990 if (unformat (line_input, "counters"))
992 else if (unformat (line_input, "index %d", &map_domain_index))
996 error = clib_error_return (0, "unknown input `%U'",
997 format_unformat_error, line_input);
1002 if (pool_elts (mm->domains) == 0)
1004 vlib_cli_output (vm, "No MAP domains are configured...");
1008 if (map_domain_index == ~0)
1011 pool_foreach(d, mm->domains,
1012 ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1017 if (pool_is_free_index (mm->domains, map_domain_index))
1019 error = clib_error_return (0, "MAP domain does not exists %d",
1024 d = pool_elt_at_index (mm->domains, map_domain_index);
1025 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1029 unformat_free (line_input);
1035 map_error_counter_get (u32 node_index, map_error_t map_error)
1037 vlib_main_t *vm = vlib_get_main ();
1038 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1039 vlib_error_main_t *em = &vm->error_main;
1040 vlib_error_t e = error_node->errors[map_error];
1041 vlib_node_t *n = vlib_get_node (vm, node_index);
1044 ci = vlib_error_get_code (&vm->node_main, e);
1045 ASSERT (ci < n->n_errors);
1046 ci += n->error_heap_index;
1048 return (em->counters[ci]);
1051 static clib_error_t *
1052 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1053 vlib_cli_command_t * cmd)
1055 map_main_t *mm = &map_main;
1057 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1058 if (pool_elts (mm->domains) == 0)
1060 vlib_cli_output (vm, "No MAP domains are configured...");
1065 pool_foreach(d, mm->domains, ({
1067 rulecount+= 0x1 << d->psid_length;
1068 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1070 domains += sizeof(*d);
1075 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1076 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1077 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1078 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1080 #if MAP_SKIP_IP6_LOOKUP
1081 vlib_cli_output (vm,
1082 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1083 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1084 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1089 vlib_cli_output (vm, "MAP traffic-class: copy");
1091 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1094 vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
1096 vlib_cli_output (vm,
1097 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1098 mm->sec_check ? "enabled" : "disabled",
1099 mm->sec_check_frag ? "enabled" : "disabled");
1101 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1102 format_ip4_address, &mm->icmp4_src_address);
1103 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1104 mm->icmp6_enabled ? "enabled" : "disabled");
1105 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1106 mm->frag_inner ? "enabled" : "disabled");
1107 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1108 mm->frag_ignore_df ? "enabled" : "disabled");
1113 vlib_combined_counter_main_t *cm = mm->domain_counters;
1114 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1115 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1119 clib_memset (total_pkts, 0, sizeof (total_pkts));
1120 clib_memset (total_bytes, 0, sizeof (total_bytes));
1122 map_domain_counter_lock (mm);
1123 vec_foreach (cm, mm->domain_counters)
1125 which = cm - mm->domain_counters;
1127 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1129 vlib_get_combined_counter (cm, i, &v);
1130 total_pkts[which] += v.packets;
1131 total_bytes[which] += v.bytes;
1134 map_domain_counter_unlock (mm);
1136 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1137 total_pkts[MAP_DOMAIN_COUNTER_TX],
1138 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1139 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1140 total_pkts[MAP_DOMAIN_COUNTER_RX],
1141 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1143 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1144 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1149 static clib_error_t *
1150 map_if_command_fn (vlib_main_t * vm,
1151 unformat_input_t * input, vlib_cli_command_t * cmd)
1153 unformat_input_t _line_input, *line_input = &_line_input;
1154 clib_error_t *error = NULL;
1155 bool is_enable = true, is_translation = false;
1156 vnet_main_t *vnm = vnet_get_main ();
1157 u32 sw_if_index = ~0;
1159 /* Get a line of input. */
1160 if (!unformat_user (input, unformat_line_input, line_input))
1163 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1166 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1168 else if (unformat (line_input, "del"))
1170 else if (unformat (line_input, "map-t"))
1171 is_translation = true;
1174 error = clib_error_return (0, "unknown input `%U'",
1175 format_unformat_error, line_input);
1181 unformat_free (line_input);
1183 if (sw_if_index == ~0)
1185 error = clib_error_return (0, "unknown interface");
1189 int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
1192 error = clib_error_return (0, "failure enabling MAP on interface");
1200 * packet trace format function
1203 format_map_trace (u8 * s, va_list * args)
1205 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1206 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1207 map_trace_t *t = va_arg (*args, map_trace_t *);
1208 u32 map_domain_index = t->map_domain_index;
1212 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1213 clib_net_to_host_u16 (port));
1218 static clib_error_t *
1219 map_tcp_mss_command_fn (vlib_main_t * vm,
1220 unformat_input_t * input, vlib_cli_command_t * cmd)
1222 unformat_input_t _line_input, *line_input = &_line_input;
1223 clib_error_t *error = NULL;
1226 /* Get a line of input. */
1227 if (!unformat_user (input, unformat_line_input, line_input))
1230 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1232 if (unformat (line_input, "%u", &tcp_mss))
1236 error = clib_error_return (0, "unknown input `%U'",
1237 format_unformat_error, line_input);
1242 if (tcp_mss >= (0x1 << 16))
1244 error = clib_error_return (0, "invalid value `%u'", tcp_mss);
1248 map_param_set_tcp (tcp_mss);
1251 unformat_free (line_input);
1260 * Set or copy the IP TOS/Traffic Class field
1263 * @cliexstart{map params traffic-class}
1265 * This command is used to set the traffic-class field in translated
1266 * or encapsulated packets. If copy is specifed (the default) then the
1267 * traffic-class/TOS field is copied from the original packet to the
1268 * translated / encapsulating header.
1271 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1272 .path = "map params traffic-class",
1273 .short_help = "map params traffic-class {0x0-0xff | copy}",
1274 .function = map_traffic_class_command_fn,
1281 * @cliexstart{map params tcp-mss}
1283 * This command is used to set the TCP MSS in translated
1284 * or encapsulated packets.
1287 VLIB_CLI_COMMAND(map_tcp_mss_command, static) = {
1288 .path = "map params tcp-mss",
1289 .short_help = "map params tcp-mss <value>",
1290 .function = map_tcp_mss_command_fn,
1294 * Bypass IP4/IP6 lookup
1297 * @cliexstart{map params pre-resolve}
1299 * Bypass a second FIB lookup of the translated or encapsulated
1300 * packet, and forward the packet directly to the specified
1301 * next-hop. This optimization trades forwarding flexibility for
1305 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1306 .path = "map params pre-resolve",
1307 .short_help = " map params pre-resolve {ip4-nh <address>} "
1308 "| {ip6-nh <address>}",
1309 .function = map_pre_resolve_command_fn,
1313 * Enable or disable the MAP-E inbound security check
1314 * Specifiy if the inbound security check should be done on fragments
1317 * @cliexstart{map params security-check}
1319 * By default, a decapsulated packet's IPv4 source address will be
1320 * verified against the outer header's IPv6 source address. Disabling
1321 * this feature will allow IPv4 source address spoofing.
1323 * Typically the inbound on-decapsulation security check is only done
1324 * on the first packet. The packet that contains the L4
1325 * information. While a security check on every fragment is possible,
1326 * it has a cost. State must be created on the first fragment.
1329 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1330 .path = "map params security-check",
1331 .short_help = "map params security-check enable|disable fragments on|off",
1332 .function = map_security_check_command_fn,
1337 * Specifiy the IPv4 source address used for relayed ICMP error messages
1340 * @cliexstart{map params icmp source-address}
1342 * This command specifies which IPv4 source address (must be local to
1343 * the system), that is used for relayed received IPv6 ICMP error
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,
1354 * Send IPv6 ICMP unreachables
1357 * @cliexstart{map params icmp6 unreachables}
1359 * Send IPv6 ICMP unreachable messages back if security check fails or
1360 * no MAP domain exists.
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,
1370 * Configure MAP fragmentation behaviour
1373 * @cliexstart{map params fragment}
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
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,
1393 * @cliexstart{map add domain}
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,
1406 * Add MAP rule to a domain
1409 * @cliexstart{map add rule}
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,
1422 * @cliexstart{map del domain}
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,
1435 * @cliexstart{show map domain}
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,
1445 * Show MAP statistics
1448 * @cliexstart{show map stats}
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,
1458 * Enable MAP processing on interface (input feature)
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,
1467 VLIB_PLUGIN_REGISTER() = {
1468 .version = VPP_BUILD_VER,
1469 .description = "Mapping of Address and Port (MAP)",
1478 map_init (vlib_main_t * vm)
1480 map_main_t *mm = &map_main;
1481 clib_error_t *error = 0;
1483 memset (mm, 0, sizeof (*mm));
1485 mm->vnet_main = vnet_get_main ();
1488 #ifdef MAP_SKIP_IP6_LOOKUP
1489 fib_protocol_t proto;
1491 FOR_EACH_FIB_PROTOCOL (proto)
1493 map_pre_resolve_init (&pre_resolved[proto]);
1501 /* Inbound security check */
1502 mm->sec_check = true;
1503 mm->sec_check_frag = false;
1505 /* ICMP6 Type 1, Code 5 for security check failure */
1506 mm->icmp6_enabled = false;
1508 /* Inner or outer fragmentation */
1509 mm->frag_inner = false;
1510 mm->frag_ignore_df = false;
1512 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
1513 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
1514 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
1516 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
1517 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
1518 mm->icmp_relayed.stat_segment_name = "/map/icmp-relayed";
1520 /* IP6 virtual reassembly */
1522 #ifdef MAP_SKIP_IP6_LOOKUP
1523 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
1526 /* LPM lookup tables */
1527 mm->ip4_prefix_tbl = lpm_table_init (LPM_TYPE_KEY32);
1528 mm->ip6_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
1529 mm->ip6_src_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
1531 mm->bm_trans_enabled_by_sw_if = 0;
1532 mm->bm_encap_enabled_by_sw_if = 0;
1534 error = map_plugin_api_hookup (vm);
1539 VLIB_INIT_FUNCTION (map_init);
1542 * fd.io coding-style-patch-verification: ON
1545 * eval: (c-set-style "gnu")