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 %s", &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,
577 unformat_free (line_input);
582 static clib_error_t *
583 map_del_domain_command_fn (vlib_main_t * vm,
584 unformat_input_t * input, vlib_cli_command_t * cmd)
586 unformat_input_t _line_input, *line_input = &_line_input;
588 u32 map_domain_index;
589 clib_error_t *error = NULL;
591 /* Get a line of input. */
592 if (!unformat_user (input, unformat_line_input, line_input))
595 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
597 if (unformat (line_input, "index %d", &map_domain_index))
601 error = clib_error_return (0, "unknown input `%U'",
602 format_unformat_error, line_input);
609 error = clib_error_return (0, "mandatory argument(s) missing");
613 map_delete_domain (map_domain_index);
616 unformat_free (line_input);
621 static clib_error_t *
622 map_add_rule_command_fn (vlib_main_t * vm,
623 unformat_input_t * input, vlib_cli_command_t * cmd)
625 unformat_input_t _line_input, *line_input = &_line_input;
628 u32 psid = 0, map_domain_index;
629 clib_error_t *error = NULL;
631 /* Get a line of input. */
632 if (!unformat_user (input, unformat_line_input, line_input))
635 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
637 if (unformat (line_input, "index %d", &map_domain_index))
639 else if (unformat (line_input, "psid %d", &psid))
642 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
646 error = clib_error_return (0, "unknown input `%U'",
647 format_unformat_error, line_input);
654 error = clib_error_return (0, "mandatory argument(s) missing");
658 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
660 error = clib_error_return (0, "Failing to add Mapping Rule");
665 unformat_free (line_input);
670 #if MAP_SKIP_IP6_LOOKUP
671 static clib_error_t *
672 map_pre_resolve_command_fn (vlib_main_t * vm,
673 unformat_input_t * input,
674 vlib_cli_command_t * cmd)
676 unformat_input_t _line_input, *line_input = &_line_input;
677 ip4_address_t ip4nh, *p_v4 = NULL;
678 ip6_address_t ip6nh, *p_v6 = NULL;
679 clib_error_t *error = NULL;
682 clib_memset (&ip4nh, 0, sizeof (ip4nh));
683 clib_memset (&ip6nh, 0, sizeof (ip6nh));
685 /* Get a line of input. */
686 if (!unformat_user (input, unformat_line_input, line_input))
689 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
691 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
694 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
696 else if (unformat (line_input, "del"))
700 error = clib_error_return (0, "unknown input `%U'",
701 format_unformat_error, line_input);
706 map_pre_resolve (p_v4, p_v6, is_del);
709 unformat_free (line_input);
715 static clib_error_t *
716 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
717 unformat_input_t * input,
718 vlib_cli_command_t * cmd)
720 unformat_input_t _line_input, *line_input = &_line_input;
721 ip4_address_t icmp_src_address;
722 ip4_address_t *p_icmp_addr = 0;
723 map_main_t *mm = &map_main;
724 clib_error_t *error = NULL;
726 mm->icmp4_src_address.as_u32 = 0;
728 /* Get a line of input. */
729 if (!unformat_user (input, unformat_line_input, line_input))
732 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
735 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
737 mm->icmp4_src_address = icmp_src_address;
738 p_icmp_addr = &icmp_src_address;
742 error = clib_error_return (0, "unknown input `%U'",
743 format_unformat_error, line_input);
748 map_param_set_icmp (p_icmp_addr);
751 unformat_free (line_input);
756 static clib_error_t *
757 map_icmp_unreachables_command_fn (vlib_main_t * vm,
758 unformat_input_t * input,
759 vlib_cli_command_t * cmd)
761 unformat_input_t _line_input, *line_input = &_line_input;
763 clib_error_t *error = NULL;
764 bool enabled = false;
766 /* Get a line of input. */
767 if (!unformat_user (input, unformat_line_input, line_input))
770 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
773 if (unformat (line_input, "on"))
775 else if (unformat (line_input, "off"))
779 error = clib_error_return (0, "unknown input `%U'",
780 format_unformat_error, line_input);
787 error = clib_error_return (0, "mandatory argument(s) missing");
790 map_param_set_icmp6 (enabled);
793 unformat_free (line_input);
799 static clib_error_t *
800 map_fragment_command_fn (vlib_main_t * vm,
801 unformat_input_t * input, vlib_cli_command_t * cmd)
803 unformat_input_t _line_input, *line_input = &_line_input;
804 clib_error_t *error = NULL;
805 bool frag_inner = false;
806 bool frag_ignore_df = false;
807 bool saw_in_out = false;
810 /* Get a line of input. */
811 if (!unformat_user (input, unformat_line_input, line_input))
814 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
816 if (unformat (line_input, "inner"))
821 else if (unformat (line_input, "outer"))
826 else if (unformat (line_input, "ignore-df"))
828 frag_ignore_df = true;
831 else if (unformat (line_input, "honor-df"))
833 frag_ignore_df = false;
838 error = clib_error_return (0, "unknown input `%U'",
839 format_unformat_error, line_input);
846 error = clib_error_return (0, "Must specify 'inner' or 'outer'");
852 error = clib_error_return (0, "Must specify 'ignore-df' or 'honor-df'");
856 map_param_set_fragmentation (frag_inner, frag_ignore_df);
859 unformat_free (line_input);
864 static clib_error_t *
865 map_traffic_class_command_fn (vlib_main_t * vm,
866 unformat_input_t * input,
867 vlib_cli_command_t * cmd)
869 unformat_input_t _line_input, *line_input = &_line_input;
871 clib_error_t *error = NULL;
872 bool tc_copy = false;
875 /* Get a line of input. */
876 if (!unformat_user (input, unformat_line_input, line_input))
879 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
881 if (unformat (line_input, "copy"))
883 else if (unformat (line_input, "%x", &tc))
887 error = clib_error_return (0, "unknown input `%U'",
888 format_unformat_error, line_input);
893 map_param_set_traffic_class (tc_copy, tc);
896 unformat_free (line_input);
902 map_flags_to_string (u32 flags)
904 if (flags & MAP_DOMAIN_PREFIX)
910 format_map_domain (u8 * s, va_list * args)
912 map_domain_t *d = va_arg (*args, map_domain_t *);
913 bool counters = va_arg (*args, int);
914 map_main_t *mm = &map_main;
915 ip6_address_t ip6_prefix;
916 u32 map_domain_index = d - mm->domains;
917 map_domain_extra_t *de = 0;
920 clib_memset (&ip6_prefix, 0, sizeof (ip6_prefix));
922 ip6_prefix = d->ip6_prefix;
924 if (map_domain_index < vec_len (mm->domain_extras))
925 de = vec_elt_at_index (mm->domain_extras, map_domain_index);
928 "[%d] tag {%s} ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d "
929 "ea-bits-len %d psid-offset %d psid-len %d mtu %d %s",
930 map_domain_index, (de && de->tag) ? de->tag : (u8 *) "[no-tag]",
931 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
932 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
933 format_ip6_address, &d->ip6_src, d->ip6_src_len,
934 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
935 map_flags_to_string (d->flags));
939 map_domain_counter_lock (mm);
941 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
942 map_domain_index, &v);
943 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
944 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
945 map_domain_index, &v);
946 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
947 map_domain_counter_unlock (mm);
949 s = format (s, "\n");
955 for (i = 0; i < (0x1 << d->psid_length); i++)
958 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
961 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
968 static clib_error_t *
969 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
970 vlib_cli_command_t * cmd)
972 unformat_input_t _line_input, *line_input = &_line_input;
973 map_main_t *mm = &map_main;
975 bool counters = false;
976 u32 map_domain_index = ~0;
977 clib_error_t *error = NULL;
979 /* Get a line of input. */
980 if (!unformat_user (input, unformat_line_input, line_input))
983 pool_foreach(d, mm->domains,
984 ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
989 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
991 if (unformat (line_input, "counters"))
993 else if (unformat (line_input, "index %d", &map_domain_index))
997 error = clib_error_return (0, "unknown input `%U'",
998 format_unformat_error, line_input);
1003 if (pool_elts (mm->domains) == 0)
1005 vlib_cli_output (vm, "No MAP domains are configured...");
1009 if (map_domain_index == ~0)
1012 pool_foreach(d, mm->domains,
1013 ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1018 if (pool_is_free_index (mm->domains, map_domain_index))
1020 error = clib_error_return (0, "MAP domain does not exists %d",
1025 d = pool_elt_at_index (mm->domains, map_domain_index);
1026 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1030 unformat_free (line_input);
1036 map_error_counter_get (u32 node_index, map_error_t map_error)
1038 vlib_main_t *vm = vlib_get_main ();
1039 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1040 vlib_error_main_t *em = &vm->error_main;
1041 vlib_error_t e = error_node->errors[map_error];
1042 vlib_node_t *n = vlib_get_node (vm, node_index);
1045 ci = vlib_error_get_code (&vm->node_main, e);
1046 ASSERT (ci < n->n_errors);
1047 ci += n->error_heap_index;
1049 return (em->counters[ci]);
1052 static clib_error_t *
1053 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1054 vlib_cli_command_t * cmd)
1056 map_main_t *mm = &map_main;
1058 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1059 if (pool_elts (mm->domains) == 0)
1061 vlib_cli_output (vm, "No MAP domains are configured...");
1066 pool_foreach(d, mm->domains, ({
1068 rulecount+= 0x1 << d->psid_length;
1069 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1071 domains += sizeof(*d);
1076 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1077 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1078 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1079 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1081 #if MAP_SKIP_IP6_LOOKUP
1082 vlib_cli_output (vm,
1083 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1084 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1085 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1090 vlib_cli_output (vm, "MAP traffic-class: copy");
1092 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1095 vlib_cli_output (vm, "MAP TCP MSS clamping: %u", mm->tcp_mss);
1097 vlib_cli_output (vm,
1098 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1099 mm->sec_check ? "enabled" : "disabled",
1100 mm->sec_check_frag ? "enabled" : "disabled");
1102 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1103 format_ip4_address, &mm->icmp4_src_address);
1104 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1105 mm->icmp6_enabled ? "enabled" : "disabled");
1106 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1107 mm->frag_inner ? "enabled" : "disabled");
1108 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1109 mm->frag_ignore_df ? "enabled" : "disabled");
1114 vlib_combined_counter_main_t *cm = mm->domain_counters;
1115 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1116 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1120 clib_memset (total_pkts, 0, sizeof (total_pkts));
1121 clib_memset (total_bytes, 0, sizeof (total_bytes));
1123 map_domain_counter_lock (mm);
1124 vec_foreach (cm, mm->domain_counters)
1126 which = cm - mm->domain_counters;
1128 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
1130 vlib_get_combined_counter (cm, i, &v);
1131 total_pkts[which] += v.packets;
1132 total_bytes[which] += v.bytes;
1135 map_domain_counter_unlock (mm);
1137 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1138 total_pkts[MAP_DOMAIN_COUNTER_TX],
1139 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1140 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1141 total_pkts[MAP_DOMAIN_COUNTER_RX],
1142 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1144 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1145 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1150 static clib_error_t *
1151 map_if_command_fn (vlib_main_t * vm,
1152 unformat_input_t * input, vlib_cli_command_t * cmd)
1154 unformat_input_t _line_input, *line_input = &_line_input;
1155 clib_error_t *error = NULL;
1156 bool is_enable = true, is_translation = false;
1157 vnet_main_t *vnm = vnet_get_main ();
1158 u32 sw_if_index = ~0;
1160 /* Get a line of input. */
1161 if (!unformat_user (input, unformat_line_input, line_input))
1164 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1167 (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1169 else if (unformat (line_input, "del"))
1171 else if (unformat (line_input, "map-t"))
1172 is_translation = true;
1175 error = clib_error_return (0, "unknown input `%U'",
1176 format_unformat_error, line_input);
1182 unformat_free (line_input);
1184 if (sw_if_index == ~0)
1186 error = clib_error_return (0, "unknown interface");
1190 int rv = map_if_enable_disable (is_enable, sw_if_index, is_translation);
1193 error = clib_error_return (0, "failure enabling MAP on interface");
1201 * packet trace format function
1204 format_map_trace (u8 * s, va_list * args)
1206 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1207 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1208 map_trace_t *t = va_arg (*args, map_trace_t *);
1209 u32 map_domain_index = t->map_domain_index;
1213 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1214 clib_net_to_host_u16 (port));
1219 static clib_error_t *
1220 map_tcp_mss_command_fn (vlib_main_t * vm,
1221 unformat_input_t * input, vlib_cli_command_t * cmd)
1223 unformat_input_t _line_input, *line_input = &_line_input;
1224 clib_error_t *error = NULL;
1227 /* Get a line of input. */
1228 if (!unformat_user (input, unformat_line_input, line_input))
1231 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1233 if (unformat (line_input, "%u", &tcp_mss))
1237 error = clib_error_return (0, "unknown input `%U'",
1238 format_unformat_error, line_input);
1243 if (tcp_mss >= (0x1 << 16))
1245 error = clib_error_return (0, "invalid value `%u'", tcp_mss);
1249 map_param_set_tcp (tcp_mss);
1252 unformat_free (line_input);
1261 * Set or copy the IP TOS/Traffic Class field
1264 * @cliexstart{map params traffic-class}
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.
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,
1282 * @cliexstart{map params tcp-mss}
1284 * This command is used to set the TCP MSS in translated
1285 * or encapsulated packets.
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,
1295 * Bypass IP4/IP6 lookup
1298 * @cliexstart{map params pre-resolve}
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
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,
1314 * Enable or disable the MAP-E inbound security check
1315 * Specifiy if the inbound security check should be done on fragments
1318 * @cliexstart{map params security-check}
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.
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.
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,
1338 * Specifiy the IPv4 source address used for relayed ICMP error messages
1341 * @cliexstart{map params icmp source-address}
1343 * This command specifies which IPv4 source address (must be local to
1344 * the system), that is used for relayed received IPv6 ICMP error
1348 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
1349 .path = "map params icmp source-address",
1350 .short_help = "map params icmp source-address <ip4-address>",
1351 .function = map_icmp_relay_source_address_command_fn,
1355 * Send IPv6 ICMP unreachables
1358 * @cliexstart{map params icmp6 unreachables}
1360 * Send IPv6 ICMP unreachable messages back if security check fails or
1361 * no MAP domain exists.
1364 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
1365 .path = "map params icmp6 unreachables",
1366 .short_help = "map params icmp6 unreachables {on|off}",
1367 .function = map_icmp_unreachables_command_fn,
1371 * Configure MAP fragmentation behaviour
1374 * @cliexstart{map params fragment}
1376 * Allows fragmentation of the IPv4 packet even if the DF bit is
1377 * set. The choice between inner or outer fragmentation of tunnel
1378 * packets is complicated. The benefit of inner fragmentation is that
1379 * the ultimate endpoint must reassemble, instead of the tunnel
1383 VLIB_CLI_COMMAND(map_fragment_command, static) = {
1384 .path = "map params fragment",
1385 .short_help = "map params fragment inner|outer ignore-df|honor-df",
1386 .function = map_fragment_command_fn,
1394 * @cliexstart{map add domain}
1397 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
1398 .path = "map add domain",
1399 .short_help = "map add domain [tag <tag>] ip4-pfx <ip4-pfx> "
1400 "ip6-pfx <ip6-pfx> "
1401 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
1402 "[map-t] [mtu <mtu>]",
1403 .function = map_add_domain_command_fn,
1407 * Add MAP rule to a domain
1410 * @cliexstart{map add rule}
1413 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
1414 .path = "map add rule",
1415 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
1416 .function = map_add_rule_command_fn,
1423 * @cliexstart{map del domain}
1426 VLIB_CLI_COMMAND(map_del_command, static) = {
1427 .path = "map del domain",
1428 .short_help = "map del domain index <domain>",
1429 .function = map_del_domain_command_fn,
1436 * @cliexstart{show map domain}
1439 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
1440 .path = "show map domain",
1441 .short_help = "show map domain index <n> [counters]",
1442 .function = show_map_domain_command_fn,
1446 * Show MAP statistics
1449 * @cliexstart{show map stats}
1452 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
1453 .path = "show map stats",
1454 .short_help = "show map stats",
1455 .function = show_map_stats_command_fn,
1459 * Enable MAP processing on interface (input feature)
1462 VLIB_CLI_COMMAND(map_if_command, static) = {
1463 .path = "map interface",
1464 .short_help = "map interface <interface-name> [map-t] [del]",
1465 .function = map_if_command_fn,
1468 VLIB_PLUGIN_REGISTER() = {
1469 .version = VPP_BUILD_VER,
1470 .description = "Mapping of Address and Port (MAP)",
1479 map_init (vlib_main_t * vm)
1481 map_main_t *mm = &map_main;
1482 clib_error_t *error = 0;
1484 memset (mm, 0, sizeof (*mm));
1486 mm->vnet_main = vnet_get_main ();
1489 #ifdef MAP_SKIP_IP6_LOOKUP
1490 fib_protocol_t proto;
1492 FOR_EACH_FIB_PROTOCOL (proto)
1494 map_pre_resolve_init (&pre_resolved[proto]);
1502 /* Inbound security check */
1503 mm->sec_check = true;
1504 mm->sec_check_frag = false;
1506 /* ICMP6 Type 1, Code 5 for security check failure */
1507 mm->icmp6_enabled = false;
1509 /* Inner or outer fragmentation */
1510 mm->frag_inner = false;
1511 mm->frag_ignore_df = false;
1513 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
1514 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "/map/rx";
1515 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "/map/tx";
1517 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
1518 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
1519 mm->icmp_relayed.stat_segment_name = "/map/icmp-relayed";
1521 /* IP6 virtual reassembly */
1523 #ifdef MAP_SKIP_IP6_LOOKUP
1524 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
1527 /* LPM lookup tables */
1528 mm->ip4_prefix_tbl = lpm_table_init (LPM_TYPE_KEY32);
1529 mm->ip6_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
1530 mm->ip6_src_prefix_tbl = lpm_table_init (LPM_TYPE_KEY128);
1532 mm->bm_trans_enabled_by_sw_if = 0;
1533 mm->bm_encap_enabled_by_sw_if = 0;
1535 error = map_plugin_api_hookup (vm);
1540 VLIB_INIT_FUNCTION (map_init);
1543 * fd.io coding-style-patch-verification: ON
1546 * eval: (c-set-style "gnu")