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/ip6_fib.h>
20 #include <vnet/adj/adj.h>
21 #include <vnet/map/map_dpo.h>
27 crc_u32 (u32 data, u32 value)
29 __asm__ volatile ("crc32l %[data], %[value];":[value] "+r" (value):[data]
34 #include <vppinfra/xxhash.h>
37 crc_u32 (u32 data, u32 value)
39 u64 tmp = ((u64) data << 32) | (u64) value;
40 return (u32) clib_xxhash (tmp);
46 * This code supports the following MAP modes:
48 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
49 * ea_bits_len + ip4_prefix > 32
50 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
51 * Algorithmic Full IPv4 address (ea_bits_len > 0):
52 * ea_bits_len + ip4_prefix = 32
53 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
54 * Algorithmic IPv4 prefix (ea_bits_len > 0):
55 * ea_bits_len + ip4_prefix < 32
56 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
58 * Independent Shared IPv4 address (ea_bits_len = 0):
61 * Rule IPv6 address = 128, Rule PSID Set
62 * Independent Full IPv4 address (ea_bits_len = 0):
64 * psid_length = 0, ip6_prefix = 128
65 * Independent IPv4 prefix (ea_bits_len = 0):
67 * psid_length = 0, ip6_prefix = 128
72 * This code supports MAP-T:
74 * With DMR prefix length equal to 96.
80 ip4_get_port (ip4_header_t * ip, map_dir_e dir, u16 buffer_len)
82 //TODO: use buffer length
83 if (ip->ip_version_and_header_length != 0x45 ||
84 ip4_get_fragment_offset (ip))
87 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
88 (ip->protocol == IP_PROTOCOL_UDP)))
90 udp_header_t *udp = (void *) (ip + 1);
91 return (dir == MAP_SENDER) ? udp->src_port : udp->dst_port;
93 else if (ip->protocol == IP_PROTOCOL_ICMP)
95 icmp46_header_t *icmp = (void *) (ip + 1);
96 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
98 return *((u16 *) (icmp + 1));
100 else if (clib_net_to_host_u16 (ip->length) >= 64)
102 ip = (ip4_header_t *) (icmp + 2);
103 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
104 (ip->protocol == IP_PROTOCOL_UDP)))
106 udp_header_t *udp = (void *) (ip + 1);
107 return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
109 else if (ip->protocol == IP_PROTOCOL_ICMP)
111 icmp46_header_t *icmp = (void *) (ip + 1);
112 if (icmp->type == ICMP4_echo_request ||
113 icmp->type == ICMP4_echo_reply)
115 return *((u16 *) (icmp + 1));
124 ip6_get_port (ip6_header_t * ip6, map_dir_e dir, u16 buffer_len)
131 if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
134 //TODO: Use buffer length
137 ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
138 u8_ptr_add (ip6, frag_offset))))
139 return -1; //Can't deal with non-first fragment for now
141 l4 = u8_ptr_add (ip6, l4_offset);
142 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
145 MAP_SENDER) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t
149 else if (l4_protocol == IP_PROTOCOL_ICMP6)
151 icmp46_header_t *icmp = (icmp46_header_t *) (l4);
152 if (icmp->type == ICMP6_echo_request)
154 return (dir == MAP_SENDER) ? ((u16 *) (icmp))[2] : -1;
156 else if (icmp->type == ICMP6_echo_reply)
158 return (dir == MAP_SENDER) ? -1 : ((u16 *) (icmp))[2];
166 map_create_domain (ip4_address_t * ip4_prefix,
168 ip6_address_t * ip6_prefix,
170 ip6_address_t * ip6_src,
174 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
176 u8 suffix_len, suffix_shift;
177 map_main_t *mm = &map_main;
178 dpo_id_t dpo_v4 = DPO_INVALID;
179 dpo_id_t dpo_v6 = DPO_INVALID;
180 fib_node_index_t fei;
183 /* Sanity check on the src prefix length */
184 if (flags & MAP_DOMAIN_TRANSLATION)
186 if (ip6_src_len != 96)
188 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
194 if (ip6_src_len != 128)
197 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
203 /* How many, and which bits to grab from the IPv4 DA */
204 if (ip4_prefix_len + ea_bits_len < 32)
206 flags |= MAP_DOMAIN_PREFIX;
207 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
208 suffix_len = ea_bits_len;
213 suffix_len = 32 - ip4_prefix_len;
216 /* EA bits must be within the first 64 bits */
217 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
218 ip6_prefix_len + suffix_len + psid_length > 64))
221 ("Embedded Address bits must be within the first 64 bits of "
226 /* Get domain index */
227 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
228 memset (d, 0, sizeof (*d));
229 *map_domain_index = d - mm->domains;
231 /* Init domain struct */
232 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
233 d->ip4_prefix_len = ip4_prefix_len;
234 d->ip6_prefix = *ip6_prefix;
235 d->ip6_prefix_len = ip6_prefix_len;
236 d->ip6_src = *ip6_src;
237 d->ip6_src_len = ip6_src_len;
238 d->ea_bits_len = ea_bits_len;
239 d->psid_offset = psid_offset;
240 d->psid_length = psid_length;
243 d->suffix_shift = suffix_shift;
244 d->suffix_mask = (1 << suffix_len) - 1;
246 d->psid_shift = 16 - psid_length - psid_offset;
247 d->psid_mask = (1 << d->psid_length) - 1;
248 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
250 /* MAP data-plane object */
251 if (d->flags & MAP_DOMAIN_TRANSLATION)
252 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
254 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
256 /* Create ip4 route */
258 .fp_proto = FIB_PROTOCOL_IP4,
259 .fp_len = d->ip4_prefix_len,
261 .ip4 = d->ip4_prefix,
265 fib_table_entry_special_dpo_add (0, &pfx,
267 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
271 * Multiple MAP domains may share same source IPv6 TEP.
272 * In this case the route will exist and be MAP sourced.
273 * Find the adj (if any) already contributed and modify it
275 fib_prefix_t pfx6 = {
276 .fp_proto = FIB_PROTOCOL_IP6,
277 .fp_len = d->ip6_src_len,
283 fei = fib_table_lookup_exact_match (0, &pfx6);
285 if (FIB_NODE_INDEX_INVALID != fei)
287 dpo_id_t dpo = DPO_INVALID;
289 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_MAP, &dpo))
292 * modify the existing MAP to indicate it's shared
295 const dpo_id_t *md_dpo;
298 ASSERT (DPO_LOAD_BALANCE == dpo.dpoi_type);
300 md_dpo = load_balance_get_bucket (dpo.dpoi_index, 0);
301 md = map_dpo_get (md_dpo->dpoi_index);
304 dpo_copy (&dpo_v6, md_dpo);
311 if (d->flags & MAP_DOMAIN_TRANSLATION)
312 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
314 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
318 * Create ip6 route. This is a reference counted add. If the prefix
319 * already exists and is MAP sourced, it is now MAP source n+1 times
320 * and will need to be removed n+1 times.
322 fib_table_entry_special_dpo_add (0, &pfx6,
324 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
327 /* Validate packet/byte counters */
328 map_domain_counter_lock (mm);
330 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
332 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
334 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
337 for (i = 0; i < vec_len (mm->domain_counters); i++)
339 vlib_validate_combined_counter (&mm->domain_counters[i],
341 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
343 map_domain_counter_unlock (mm);
352 map_delete_domain (u32 map_domain_index)
354 map_main_t *mm = &map_main;
357 if (pool_is_free_index (mm->domains, map_domain_index))
359 clib_warning ("MAP domain delete: domain does not exist: %d",
364 d = pool_elt_at_index (mm->domains, map_domain_index);
367 .fp_proto = FIB_PROTOCOL_IP4,
368 .fp_len = d->ip4_prefix_len,
370 .ip4 = d->ip4_prefix,
374 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
376 fib_prefix_t pfx6 = {
377 .fp_proto = FIB_PROTOCOL_IP6,
378 .fp_len = d->ip6_src_len,
384 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
388 clib_mem_free (d->rules);
390 pool_put (mm->domains, d);
396 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
400 map_main_t *mm = &map_main;
402 if (pool_is_free_index (mm->domains, map_domain_index))
404 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
407 d = pool_elt_at_index (mm->domains, map_domain_index);
409 /* Rules are only used in 1:1 independent case */
410 if (d->ea_bits_len > 0)
415 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
416 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
419 memset (d->rules, 0, l);
422 if (psid >= (0x1 << d->psid_length))
424 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
425 0x1 << d->psid_length);
431 d->rules[psid] = *tep;
435 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
440 #ifdef MAP_SKIP_IP6_LOOKUP
442 * Pre-resolvd per-protocol global next-hops
444 map_main_pre_resolved_t pre_resolved[FIB_PROTOCOL_MAX];
447 map_pre_resolve_init (map_main_pre_resolved_t * pr)
449 pr->fei = FIB_NODE_INDEX_INVALID;
450 fib_node_init (&pr->node, FIB_NODE_TYPE_MAP_E);
454 format_map_pre_resolve (u8 * s, va_list ap)
456 map_main_pre_resolved_t *pr = va_arg (ap, map_main_pre_resolved_t *);
458 if (FIB_NODE_INDEX_INVALID != pr->fei)
462 fib_entry_get_prefix (pr->fei, &pfx);
464 return (format (s, "%U (%u)",
465 format_ip46_address, &pfx.fp_addr, IP46_TYPE_ANY,
466 pr->dpo.dpoi_index));
470 return (format (s, "un-set"));
476 * Function definition to inform the FIB node that its last lock has gone.
479 map_last_lock_gone (fib_node_t * node)
482 * The MAP is a root of the graph. As such
483 * it never has children and thus is never locked.
488 static map_main_pre_resolved_t *
489 map_from_fib_node (fib_node_t * node)
492 ASSERT (FIB_NODE_TYPE_MAP_E == node->fn_type);
494 return ((map_main_pre_resolved_t *)
496 STRUCT_OFFSET_OF (map_main_pre_resolved_t, node)));
500 map_stack (map_main_pre_resolved_t * pr)
504 dpo = fib_entry_contribute_ip_forwarding (pr->fei);
506 dpo_copy (&pr->dpo, dpo);
510 * Function definition to backwalk a FIB node
512 static fib_node_back_walk_rc_t
513 map_back_walk (fib_node_t * node, fib_node_back_walk_ctx_t * ctx)
515 map_stack (map_from_fib_node (node));
517 return (FIB_NODE_BACK_WALK_CONTINUE);
521 * Function definition to get a FIB node from its index
524 map_fib_node_get (fib_node_index_t index)
526 return (&pre_resolved[index].node);
530 * Virtual function table registered by MPLS GRE tunnels
531 * for participation in the FIB object graph.
533 const static fib_node_vft_t map_vft = {
534 .fnv_get = map_fib_node_get,
535 .fnv_last_lock = map_last_lock_gone,
536 .fnv_back_walk = map_back_walk,
540 map_fib_resolve (map_main_pre_resolved_t * pr,
541 fib_protocol_t proto, u8 len, const ip46_address_t * addr)
549 pr->fei = fib_table_entry_special_add (0, // default fib
554 pr->sibling = fib_entry_child_add (pr->fei, FIB_NODE_TYPE_MAP_E, proto);
559 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6)
561 if (ip6 && (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0))
563 ip46_address_t addr = {
566 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP6],
567 FIB_PROTOCOL_IP6, 128, &addr);
569 if (ip4 && (ip4->as_u32 != 0))
571 ip46_address_t addr = {
574 map_fib_resolve (&pre_resolved[FIB_PROTOCOL_IP4],
575 FIB_PROTOCOL_IP4, 32, &addr);
580 static clib_error_t *
581 map_security_check_command_fn (vlib_main_t * vm,
582 unformat_input_t * input,
583 vlib_cli_command_t * cmd)
585 unformat_input_t _line_input, *line_input = &_line_input;
586 map_main_t *mm = &map_main;
587 clib_error_t *error = NULL;
589 /* Get a line of input. */
590 if (!unformat_user (input, unformat_line_input, line_input))
593 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
595 if (unformat (line_input, "off"))
596 mm->sec_check = false;
597 else if (unformat (line_input, "on"))
598 mm->sec_check = true;
601 error = clib_error_return (0, "unknown input `%U'",
602 format_unformat_error, line_input);
608 unformat_free (line_input);
613 static clib_error_t *
614 map_security_check_frag_command_fn (vlib_main_t * vm,
615 unformat_input_t * input,
616 vlib_cli_command_t * cmd)
618 unformat_input_t _line_input, *line_input = &_line_input;
619 map_main_t *mm = &map_main;
620 clib_error_t *error = NULL;
622 /* Get a line of input. */
623 if (!unformat_user (input, unformat_line_input, line_input))
626 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
628 if (unformat (line_input, "off"))
629 mm->sec_check_frag = false;
630 else if (unformat (line_input, "on"))
631 mm->sec_check_frag = true;
634 error = clib_error_return (0, "unknown input `%U'",
635 format_unformat_error, line_input);
641 unformat_free (line_input);
646 static clib_error_t *
647 map_add_domain_command_fn (vlib_main_t * vm,
648 unformat_input_t * input, vlib_cli_command_t * cmd)
650 unformat_input_t _line_input, *line_input = &_line_input;
651 ip4_address_t ip4_prefix;
652 ip6_address_t ip6_prefix;
653 ip6_address_t ip6_src;
654 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
656 /* Optional arguments */
657 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
661 clib_error_t *error = NULL;
663 /* Get a line of input. */
664 if (!unformat_user (input, unformat_line_input, line_input))
667 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
670 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
675 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
680 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
685 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
687 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
689 else if (unformat (line_input, "psid-offset %d", &psid_offset))
691 else if (unformat (line_input, "psid-len %d", &psid_length))
693 else if (unformat (line_input, "mtu %d", &mtu))
695 else if (unformat (line_input, "map-t"))
696 flags |= MAP_DOMAIN_TRANSLATION;
699 error = clib_error_return (0, "unknown input `%U'",
700 format_unformat_error, line_input);
707 error = clib_error_return (0, "mandatory argument(s) missing");
711 map_create_domain (&ip4_prefix, ip4_prefix_len,
712 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
713 ea_bits_len, psid_offset, psid_length, &map_domain_index,
717 unformat_free (line_input);
722 static clib_error_t *
723 map_del_domain_command_fn (vlib_main_t * vm,
724 unformat_input_t * input, vlib_cli_command_t * cmd)
726 unformat_input_t _line_input, *line_input = &_line_input;
728 u32 map_domain_index;
729 clib_error_t *error = NULL;
731 /* Get a line of input. */
732 if (!unformat_user (input, unformat_line_input, line_input))
735 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
737 if (unformat (line_input, "index %d", &map_domain_index))
741 error = clib_error_return (0, "unknown input `%U'",
742 format_unformat_error, line_input);
749 error = clib_error_return (0, "mandatory argument(s) missing");
753 map_delete_domain (map_domain_index);
756 unformat_free (line_input);
761 static clib_error_t *
762 map_add_rule_command_fn (vlib_main_t * vm,
763 unformat_input_t * input, vlib_cli_command_t * cmd)
765 unformat_input_t _line_input, *line_input = &_line_input;
768 u32 psid = 0, map_domain_index;
769 clib_error_t *error = NULL;
771 /* Get a line of input. */
772 if (!unformat_user (input, unformat_line_input, line_input))
775 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
777 if (unformat (line_input, "index %d", &map_domain_index))
779 else if (unformat (line_input, "psid %d", &psid))
782 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
786 error = clib_error_return (0, "unknown input `%U'",
787 format_unformat_error, line_input);
794 error = clib_error_return (0, "mandatory argument(s) missing");
798 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
800 error = clib_error_return (0, "Failing to add Mapping Rule");
805 unformat_free (line_input);
810 #if MAP_SKIP_IP6_LOOKUP
811 static clib_error_t *
812 map_pre_resolve_command_fn (vlib_main_t * vm,
813 unformat_input_t * input,
814 vlib_cli_command_t * cmd)
816 unformat_input_t _line_input, *line_input = &_line_input;
817 ip4_address_t ip4nh, *p_v4 = NULL;
818 ip6_address_t ip6nh, *p_v6 = NULL;
819 clib_error_t *error = NULL;
821 memset (&ip4nh, 0, sizeof (ip4nh));
822 memset (&ip6nh, 0, sizeof (ip6nh));
824 /* Get a line of input. */
825 if (!unformat_user (input, unformat_line_input, line_input))
828 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
830 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
833 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
837 error = clib_error_return (0, "unknown input `%U'",
838 format_unformat_error, line_input);
843 map_pre_resolve (p_v4, p_v6);
846 unformat_free (line_input);
852 static clib_error_t *
853 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
854 unformat_input_t * input,
855 vlib_cli_command_t * cmd)
857 unformat_input_t _line_input, *line_input = &_line_input;
858 ip4_address_t icmp_src_address;
859 map_main_t *mm = &map_main;
860 clib_error_t *error = NULL;
862 mm->icmp4_src_address.as_u32 = 0;
864 /* Get a line of input. */
865 if (!unformat_user (input, unformat_line_input, line_input))
868 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
871 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
872 mm->icmp4_src_address = icmp_src_address;
875 error = clib_error_return (0, "unknown input `%U'",
876 format_unformat_error, line_input);
882 unformat_free (line_input);
887 static clib_error_t *
888 map_icmp_unreachables_command_fn (vlib_main_t * vm,
889 unformat_input_t * input,
890 vlib_cli_command_t * cmd)
892 unformat_input_t _line_input, *line_input = &_line_input;
893 map_main_t *mm = &map_main;
895 clib_error_t *error = NULL;
897 /* Get a line of input. */
898 if (!unformat_user (input, unformat_line_input, line_input))
901 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
904 if (unformat (line_input, "on"))
905 mm->icmp6_enabled = true;
906 else if (unformat (line_input, "off"))
907 mm->icmp6_enabled = false;
910 error = clib_error_return (0, "unknown input `%U'",
911 format_unformat_error, line_input);
918 error = clib_error_return (0, "mandatory argument(s) missing");
921 unformat_free (line_input);
926 static clib_error_t *
927 map_fragment_command_fn (vlib_main_t * vm,
928 unformat_input_t * input, vlib_cli_command_t * cmd)
930 unformat_input_t _line_input, *line_input = &_line_input;
931 map_main_t *mm = &map_main;
932 clib_error_t *error = NULL;
934 /* Get a line of input. */
935 if (!unformat_user (input, unformat_line_input, line_input))
938 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
940 if (unformat (line_input, "inner"))
941 mm->frag_inner = true;
942 else if (unformat (line_input, "outer"))
943 mm->frag_inner = false;
946 error = clib_error_return (0, "unknown input `%U'",
947 format_unformat_error, line_input);
953 unformat_free (line_input);
958 static clib_error_t *
959 map_fragment_df_command_fn (vlib_main_t * vm,
960 unformat_input_t * input,
961 vlib_cli_command_t * cmd)
963 unformat_input_t _line_input, *line_input = &_line_input;
964 map_main_t *mm = &map_main;
965 clib_error_t *error = NULL;
967 /* Get a line of input. */
968 if (!unformat_user (input, unformat_line_input, line_input))
971 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
973 if (unformat (line_input, "on"))
974 mm->frag_ignore_df = true;
975 else if (unformat (line_input, "off"))
976 mm->frag_ignore_df = false;
979 error = clib_error_return (0, "unknown input `%U'",
980 format_unformat_error, line_input);
986 unformat_free (line_input);
991 static clib_error_t *
992 map_traffic_class_command_fn (vlib_main_t * vm,
993 unformat_input_t * input,
994 vlib_cli_command_t * cmd)
996 unformat_input_t _line_input, *line_input = &_line_input;
997 map_main_t *mm = &map_main;
999 clib_error_t *error = NULL;
1001 mm->tc_copy = false;
1003 /* Get a line of input. */
1004 if (!unformat_user (input, unformat_line_input, line_input))
1007 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1009 if (unformat (line_input, "copy"))
1011 else if (unformat (line_input, "%x", &tc))
1015 error = clib_error_return (0, "unknown input `%U'",
1016 format_unformat_error, line_input);
1022 unformat_free (line_input);
1028 format_map_domain (u8 * s, va_list * args)
1030 map_domain_t *d = va_arg (*args, map_domain_t *);
1031 bool counters = va_arg (*args, int);
1032 map_main_t *mm = &map_main;
1033 ip6_address_t ip6_prefix;
1036 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
1038 ip6_prefix = d->ip6_prefix;
1041 "[%d] ip4-pfx %U/%d ip6-pfx %U/%d ip6-src %U/%d ea_bits_len %d psid-offset %d psid-len %d mtu %d %s",
1043 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
1044 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
1045 format_ip6_address, &d->ip6_src, d->ip6_src_len,
1046 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
1047 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
1051 map_domain_counter_lock (mm);
1053 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
1054 d - mm->domains, &v);
1055 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
1056 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
1057 d - mm->domains, &v);
1058 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
1059 map_domain_counter_unlock (mm);
1061 s = format (s, "\n");
1067 for (i = 0; i < (0x1 << d->psid_length); i++)
1070 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
1073 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
1081 format_map_ip4_reass (u8 * s, va_list * args)
1083 map_main_t *mm = &map_main;
1084 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
1085 map_ip4_reass_key_t *k = &r->key;
1086 f64 now = vlib_time_now (mm->vlib_main);
1087 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
1088 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1090 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
1091 format_ip4_address, &k->src.as_u8, format_ip4_address,
1092 &k->dst.as_u8, k->protocol,
1093 clib_net_to_host_u16 (k->fragment_id),
1094 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
1099 format_map_ip6_reass (u8 * s, va_list * args)
1101 map_main_t *mm = &map_main;
1102 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
1103 map_ip6_reass_key_t *k = &r->key;
1104 f64 now = vlib_time_now (mm->vlib_main);
1105 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
1106 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
1108 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
1109 format_ip6_address, &k->src.as_u8, format_ip6_address,
1110 &k->dst.as_u8, k->protocol,
1111 clib_net_to_host_u32 (k->fragment_id), dt);
1115 static clib_error_t *
1116 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
1117 vlib_cli_command_t * cmd)
1119 unformat_input_t _line_input, *line_input = &_line_input;
1120 map_main_t *mm = &map_main;
1122 bool counters = false;
1123 u32 map_domain_index = ~0;
1124 clib_error_t *error = NULL;
1126 /* Get a line of input. */
1127 if (!unformat_user (input, unformat_line_input, line_input))
1130 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1132 if (unformat (line_input, "counters"))
1134 else if (unformat (line_input, "index %d", &map_domain_index))
1138 error = clib_error_return (0, "unknown input `%U'",
1139 format_unformat_error, line_input);
1144 if (pool_elts (mm->domains) == 0)
1145 vlib_cli_output (vm, "No MAP domains are configured...");
1147 if (map_domain_index == ~0)
1150 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
1155 if (pool_is_free_index (mm->domains, map_domain_index))
1157 error = clib_error_return (0, "MAP domain does not exists %d",
1162 d = pool_elt_at_index (mm->domains, map_domain_index);
1163 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
1167 unformat_free (line_input);
1172 static clib_error_t *
1173 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
1174 vlib_cli_command_t * cmd)
1176 map_main_t *mm = &map_main;
1177 map_ip4_reass_t *f4;
1178 map_ip6_reass_t *f6;
1181 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
1184 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
1190 map_error_counter_get (u32 node_index, map_error_t map_error)
1192 vlib_main_t *vm = vlib_get_main ();
1193 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
1194 vlib_error_main_t *em = &vm->error_main;
1195 vlib_error_t e = error_node->errors[map_error];
1196 vlib_node_t *n = vlib_get_node (vm, node_index);
1199 ci = vlib_error_get_code (e);
1200 ASSERT (ci < n->n_errors);
1201 ci += n->error_heap_index;
1203 return (em->counters[ci]);
1206 static clib_error_t *
1207 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1208 vlib_cli_command_t * cmd)
1210 map_main_t *mm = &map_main;
1212 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1213 if (pool_elts (mm->domains) == 0)
1214 vlib_cli_output (vm, "No MAP domains are configured...");
1217 pool_foreach(d, mm->domains, ({
1219 rulecount+= 0x1 << d->psid_length;
1220 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1222 domains += sizeof(*d);
1227 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1228 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1229 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1230 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1232 #if MAP_SKIP_IP6_LOOKUP
1233 vlib_cli_output (vm,
1234 "MAP pre-resolve: IP6 next-hop: %U, IP4 next-hop: %U\n",
1235 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP6],
1236 format_map_pre_resolve, &pre_resolved[FIB_PROTOCOL_IP4]);
1241 vlib_cli_output (vm, "MAP traffic-class: copy");
1243 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1245 vlib_cli_output (vm,
1246 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1247 mm->sec_check ? "enabled" : "disabled",
1248 mm->sec_check_frag ? "enabled" : "disabled");
1250 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1251 format_ip4_address, &mm->icmp4_src_address);
1252 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1253 mm->icmp6_enabled ? "enabled" : "disabled");
1254 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1255 mm->frag_inner ? "enabled" : "disabled");
1256 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1257 mm->frag_ignore_df ? "enabled" : "disabled");
1262 vlib_combined_counter_main_t *cm = mm->domain_counters;
1263 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1264 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1268 memset (total_pkts, 0, sizeof (total_pkts));
1269 memset (total_bytes, 0, sizeof (total_bytes));
1271 map_domain_counter_lock (mm);
1272 vec_foreach (cm, mm->domain_counters)
1274 which = cm - mm->domain_counters;
1276 for (i = 0; i < vec_len (cm->maxi); i++)
1278 vlib_get_combined_counter (cm, i, &v);
1279 total_pkts[which] += v.packets;
1280 total_bytes[which] += v.bytes;
1283 map_domain_counter_unlock (mm);
1285 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1286 total_pkts[MAP_DOMAIN_COUNTER_TX],
1287 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1288 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1289 total_pkts[MAP_DOMAIN_COUNTER_RX],
1290 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1292 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1293 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1298 static clib_error_t *
1299 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1300 vlib_cli_command_t * cmd)
1302 unformat_input_t _line_input, *line_input = &_line_input;
1304 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1306 u64 buffers = ~(0ull);
1307 u8 ip4 = 0, ip6 = 0;
1309 if (!unformat_user (input, unformat_line_input, line_input))
1312 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1314 if (unformat (line_input, "lifetime %u", &lifetime))
1316 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1318 else if (unformat (line_input, "pool-size %u", &pool_size))
1320 else if (unformat (line_input, "buffers %llu", &buffers))
1322 else if (unformat (line_input, "ip4"))
1324 else if (unformat (line_input, "ip6"))
1328 unformat_free (line_input);
1329 return clib_error_return (0, "invalid input");
1332 unformat_free (line_input);
1335 return clib_error_return (0, "must specify ip4 and/or ip6");
1339 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1340 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1341 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1342 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1343 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1344 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1345 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1346 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1347 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1348 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1349 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1350 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1351 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1356 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1357 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1358 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1359 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1360 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1361 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1362 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1363 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1364 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1365 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1366 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1367 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1368 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1373 u32 reass = 0, packets = 0;
1374 if (pool_size != ~0)
1376 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1378 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1382 vlib_cli_output (vm,
1383 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1387 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1389 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1391 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1395 vlib_cli_output (vm,
1396 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1402 if (map_ip4_reass_conf_lifetime (lifetime))
1403 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1405 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1407 if (buffers != ~(0ull))
1409 if (map_ip4_reass_conf_buffers (buffers))
1410 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1412 vlib_cli_output (vm, "Setting ip4-reass buffers");
1415 if (map_main.ip4_reass_conf_buffers >
1416 map_main.ip4_reass_conf_pool_size *
1417 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1419 vlib_cli_output (vm,
1420 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1426 u32 reass = 0, packets = 0;
1427 if (pool_size != ~0)
1429 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1431 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1435 vlib_cli_output (vm,
1436 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1440 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1442 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1444 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1448 vlib_cli_output (vm,
1449 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1455 if (map_ip6_reass_conf_lifetime (lifetime))
1456 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1458 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1460 if (buffers != ~(0ull))
1462 if (map_ip6_reass_conf_buffers (buffers))
1463 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1465 vlib_cli_output (vm, "Setting ip6-reass buffers");
1468 if (map_main.ip6_reass_conf_buffers >
1469 map_main.ip6_reass_conf_pool_size *
1470 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1472 vlib_cli_output (vm,
1473 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1482 * packet trace format function
1485 format_map_trace (u8 * s, va_list * args)
1487 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1488 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1489 map_trace_t *t = va_arg (*args, map_trace_t *);
1490 u32 map_domain_index = t->map_domain_index;
1494 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1495 clib_net_to_host_u16 (port));
1500 static_always_inline map_ip4_reass_t *
1501 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1503 map_main_t *mm = &map_main;
1504 u32 ri = mm->ip4_reass_hash_table[bucket];
1505 while (ri != MAP_REASS_INDEX_NONE)
1507 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1508 if (r->key.as_u64[0] == k->as_u64[0] &&
1509 r->key.as_u64[1] == k->as_u64[1] &&
1510 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1514 ri = r->bucket_next;
1519 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1522 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1524 map_main_t *mm = &map_main;
1525 map_ip4_reass_get_fragments (r, pi_to_drop);
1527 // Unlink in hash bucket
1528 map_ip4_reass_t *r2 = NULL;
1529 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1530 while (r2i != map_ip4_reass_pool_index (r))
1532 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1533 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1534 r2i = r2->bucket_next;
1538 r2->bucket_next = r->bucket_next;
1542 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1546 if (r->fifo_next == map_ip4_reass_pool_index (r))
1548 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1552 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1553 mm->ip4_reass_fifo_last = r->fifo_prev;
1554 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1556 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1560 pool_put (mm->ip4_reass_pool, r);
1561 mm->ip4_reass_allocated--;
1565 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1566 u8 protocol, u32 ** pi_to_drop)
1569 map_main_t *mm = &map_main;
1570 map_ip4_reass_key_t k = {.src.data_u32 = src,
1571 .dst.data_u32 = dst,
1572 .fragment_id = fragment_id,
1573 .protocol = protocol
1577 h = crc_u32 (k.as_u32[0], h);
1578 h = crc_u32 (k.as_u32[1], h);
1579 h = crc_u32 (k.as_u32[2], h);
1580 h = crc_u32 (k.as_u32[3], h);
1581 h = h >> (32 - mm->ip4_reass_ht_log2len);
1583 f64 now = vlib_time_now (mm->vlib_main);
1585 //Cache garbage collection
1586 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1588 map_ip4_reass_t *last =
1589 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1590 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1591 map_ip4_reass_free (last, pi_to_drop);
1596 if ((r = map_ip4_reass_lookup (&k, h, now)))
1599 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1602 pool_get (mm->ip4_reass_pool, r);
1603 mm->ip4_reass_allocated++;
1605 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1606 r->fragments[i] = ~0;
1608 u32 ri = map_ip4_reass_pool_index (r);
1610 //Link in new bucket
1612 r->bucket_next = mm->ip4_reass_hash_table[h];
1613 mm->ip4_reass_hash_table[h] = ri;
1616 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1619 pool_elt_at_index (mm->ip4_reass_pool,
1620 mm->ip4_reass_fifo_last)->fifo_next;
1621 r->fifo_prev = mm->ip4_reass_fifo_last;
1622 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1623 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1627 r->fifo_next = r->fifo_prev = ri;
1628 mm->ip4_reass_fifo_last = ri;
1635 #ifdef MAP_IP4_REASS_COUNT_BYTES
1636 r->expected_total = 0xffff;
1644 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1646 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1650 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1651 if (r->fragments[i] == ~0)
1653 r->fragments[i] = pi;
1654 map_main.ip4_reass_buffered_counter++;
1660 static_always_inline map_ip6_reass_t *
1661 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1663 map_main_t *mm = &map_main;
1664 u32 ri = mm->ip6_reass_hash_table[bucket];
1665 while (ri != MAP_REASS_INDEX_NONE)
1667 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1668 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1669 r->key.as_u64[0] == k->as_u64[0] &&
1670 r->key.as_u64[1] == k->as_u64[1] &&
1671 r->key.as_u64[2] == k->as_u64[2] &&
1672 r->key.as_u64[3] == k->as_u64[3] &&
1673 r->key.as_u64[4] == k->as_u64[4])
1675 ri = r->bucket_next;
1680 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1683 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1685 map_main_t *mm = &map_main;
1687 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1688 if (r->fragments[i].pi != ~0)
1690 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1691 r->fragments[i].pi = ~0;
1692 map_main.ip6_reass_buffered_counter--;
1695 // Unlink in hash bucket
1696 map_ip6_reass_t *r2 = NULL;
1697 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1698 while (r2i != map_ip6_reass_pool_index (r))
1700 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1701 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1702 r2i = r2->bucket_next;
1706 r2->bucket_next = r->bucket_next;
1710 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1714 if (r->fifo_next == map_ip6_reass_pool_index (r))
1716 //Single element in the list, list is now empty
1717 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1721 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1722 mm->ip6_reass_fifo_last = r->fifo_prev;
1723 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1725 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1729 // Free from pool if necessary
1730 pool_put (mm->ip6_reass_pool, r);
1731 mm->ip6_reass_allocated--;
1735 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1736 u8 protocol, u32 ** pi_to_drop)
1739 map_main_t *mm = &map_main;
1740 map_ip6_reass_key_t k = {
1743 .fragment_id = fragment_id,
1744 .protocol = protocol
1749 for (i = 0; i < 10; i++)
1750 h = crc_u32 (k.as_u32[i], h);
1751 h = h >> (32 - mm->ip6_reass_ht_log2len);
1753 f64 now = vlib_time_now (mm->vlib_main);
1755 //Cache garbage collection
1756 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1758 map_ip6_reass_t *last =
1759 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1760 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1761 map_ip6_reass_free (last, pi_to_drop);
1766 if ((r = map_ip6_reass_lookup (&k, h, now)))
1769 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1772 pool_get (mm->ip6_reass_pool, r);
1773 mm->ip6_reass_allocated++;
1774 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1776 r->fragments[i].pi = ~0;
1777 r->fragments[i].next_data_len = 0;
1778 r->fragments[i].next_data_offset = 0;
1781 u32 ri = map_ip6_reass_pool_index (r);
1783 //Link in new bucket
1785 r->bucket_next = mm->ip6_reass_hash_table[h];
1786 mm->ip6_reass_hash_table[h] = ri;
1789 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1792 pool_elt_at_index (mm->ip6_reass_pool,
1793 mm->ip6_reass_fifo_last)->fifo_next;
1794 r->fifo_prev = mm->ip6_reass_fifo_last;
1795 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1796 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1800 r->fifo_next = r->fifo_prev = ri;
1801 mm->ip6_reass_fifo_last = ri;
1807 r->ip4_header.ip_version_and_header_length = 0;
1808 #ifdef MAP_IP6_REASS_COUNT_BYTES
1809 r->expected_total = 0xffff;
1816 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1817 u16 data_offset, u16 next_data_offset,
1818 u8 * data_start, u16 data_len)
1820 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1821 u16 copied_len = (data_len > 20) ? 20 : data_len;
1823 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1826 //Lookup for fragments for the current buffer
1827 //and the one before that
1829 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1831 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1833 prev_f = &r->fragments[i]; // This is buffer for previous packet
1835 else if (r->fragments[i].next_data_offset == next_data_offset)
1837 f = &r->fragments[i]; // This is a buffer for the current packet
1839 else if (r->fragments[i].next_data_offset == 0)
1842 f = &r->fragments[i];
1843 else if (prev_f == NULL)
1844 prev_f = &r->fragments[i];
1848 if (!f || f->pi != ~0)
1856 clib_memcpy (prev_f->next_data, data_start, copied_len);
1857 prev_f->next_data_len = copied_len;
1858 prev_f->next_data_offset = data_offset;
1862 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1865 if (r->ip4_header.ip_version_and_header_length == 0)
1866 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1871 f->next_data_offset = next_data_offset;
1873 map_main.ip6_reass_buffered_counter++;
1879 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1881 map_main_t *mm = &map_main;
1884 if (dropped_packets)
1885 *dropped_packets = mm->ip4_reass_buffered_counter;
1887 *trashed_reass = mm->ip4_reass_allocated;
1888 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1890 u16 ri = mm->ip4_reass_fifo_last;
1893 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1894 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1895 if (r->fragments[i] != ~0)
1896 map_ip4_drop_pi (r->fragments[i]);
1899 pool_put (mm->ip4_reass_pool, r);
1901 while (ri != mm->ip4_reass_fifo_last);
1904 vec_free (mm->ip4_reass_hash_table);
1905 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1906 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1907 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1908 pool_free (mm->ip4_reass_pool);
1909 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1911 mm->ip4_reass_allocated = 0;
1912 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1913 mm->ip4_reass_buffered_counter = 0;
1917 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1919 u32 desired_size = (u32) (pool_size * ht_ratio);
1921 for (i = 1; i < 31; i++)
1922 if ((1 << i) >= desired_size)
1928 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1929 u32 * dropped_packets)
1931 map_main_t *mm = &map_main;
1932 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1935 map_ip4_reass_lock ();
1936 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1937 mm->ip4_reass_ht_log2len =
1938 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1939 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1940 map_ip4_reass_unlock ();
1945 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1946 u32 * dropped_packets)
1948 map_main_t *mm = &map_main;
1949 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1952 map_ip4_reass_lock ();
1953 mm->ip4_reass_conf_pool_size = pool_size;
1954 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1955 map_ip4_reass_unlock ();
1960 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1962 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1967 map_ip4_reass_conf_buffers (u32 buffers)
1969 map_main.ip4_reass_conf_buffers = buffers;
1974 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1976 map_main_t *mm = &map_main;
1977 if (dropped_packets)
1978 *dropped_packets = mm->ip6_reass_buffered_counter;
1980 *trashed_reass = mm->ip6_reass_allocated;
1982 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1984 u16 ri = mm->ip6_reass_fifo_last;
1987 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1988 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1989 if (r->fragments[i].pi != ~0)
1990 map_ip6_drop_pi (r->fragments[i].pi);
1993 pool_put (mm->ip6_reass_pool, r);
1995 while (ri != mm->ip6_reass_fifo_last);
1996 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1999 vec_free (mm->ip6_reass_hash_table);
2000 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
2001 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
2002 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
2003 pool_free (mm->ip6_reass_pool);
2004 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
2006 mm->ip6_reass_allocated = 0;
2007 mm->ip6_reass_buffered_counter = 0;
2011 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
2012 u32 * dropped_packets)
2014 map_main_t *mm = &map_main;
2015 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
2018 map_ip6_reass_lock ();
2019 mm->ip6_reass_conf_ht_ratio = ht_ratio;
2020 mm->ip6_reass_ht_log2len =
2021 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
2022 map_ip6_reass_reinit (trashed_reass, dropped_packets);
2023 map_ip6_reass_unlock ();
2028 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
2029 u32 * dropped_packets)
2031 map_main_t *mm = &map_main;
2032 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
2035 map_ip6_reass_lock ();
2036 mm->ip6_reass_conf_pool_size = pool_size;
2037 map_ip6_reass_reinit (trashed_reass, dropped_packets);
2038 map_ip6_reass_unlock ();
2043 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
2045 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
2050 map_ip6_reass_conf_buffers (u32 buffers)
2052 map_main.ip6_reass_conf_buffers = buffers;
2059 * Configure MAP reassembly behaviour
2062 * @cliexstart{map params reassembly}
2065 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
2066 .path = "map params reassembly",
2067 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
2068 "[pool-size <pool-size>] [buffers <buffers>] "
2069 "[ht-ratio <ht-ratio>]",
2070 .function = map_params_reass_command_fn,
2074 * Set or copy the IP TOS/Traffic Class field
2077 * @cliexstart{map params traffic-class}
2079 * This command is used to set the traffic-class field in translated
2080 * or encapsulated packets. If copy is specifed (the default) then the
2081 * traffic-class/TOS field is copied from the original packet to the
2082 * translated / encapsulating header.
2085 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
2086 .path = "map params traffic-class",
2087 .short_help = "map params traffic-class {0x0-0xff | copy}",
2088 .function = map_traffic_class_command_fn,
2092 * Bypass IP4/IP6 lookup
2095 * @cliexstart{map params pre-resolve}
2097 * Bypass a second FIB lookup of the translated or encapsulated
2098 * packet, and forward the packet directly to the specified
2099 * next-hop. This optimization trades forwarding flexibility for
2103 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
2104 .path = "map params pre-resolve",
2105 .short_help = " map params pre-resolve {ip4-nh <address>} "
2106 "| {ip6-nh <address>}",
2107 .function = map_pre_resolve_command_fn,
2111 * Enable or disable the MAP-E inbound security check
2114 * @cliexstart{map params security-check}
2116 * By default, a decapsulated packet's IPv4 source address will be
2117 * verified against the outer header's IPv6 source address. Disabling
2118 * this feature will allow IPv4 source address spoofing.
2121 VLIB_CLI_COMMAND(map_security_check_command, static) = {
2122 .path = "map params security-check",
2123 .short_help = "map params security-check on|off",
2124 .function = map_security_check_command_fn,
2128 * Specifiy the IPv4 source address used for relayed ICMP error messages
2131 * @cliexstart{map params icmp source-address}
2133 * This command specifies which IPv4 source address (must be local to
2134 * the system), that is used for relayed received IPv6 ICMP error
2138 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
2139 .path = "map params icmp source-address",
2140 .short_help = "map params icmp source-address <ip4-address>",
2141 .function = map_icmp_relay_source_address_command_fn,
2145 * Send IPv6 ICMP unreachables
2148 * @cliexstart{map params icmp6 unreachables}
2150 * Send IPv6 ICMP unreachable messages back if security check fails or
2151 * no MAP domain exists.
2154 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
2155 .path = "map params icmp6 unreachables",
2156 .short_help = "map params icmp6 unreachables {on|off}",
2157 .function = map_icmp_unreachables_command_fn,
2161 * Configure MAP fragmentation behaviour
2164 * @cliexstart{map params fragment}
2167 VLIB_CLI_COMMAND(map_fragment_command, static) = {
2168 .path = "map params fragment",
2169 .short_help = "map params fragment inner|outer",
2170 .function = map_fragment_command_fn,
2174 * Ignore the IPv4 Don't fragment bit
2177 * @cliexstart{map params fragment ignore-df}
2179 * Allows fragmentation of the IPv4 packet even if the DF bit is
2180 * set. The choice between inner or outer fragmentation of tunnel
2181 * packets is complicated. The benefit of inner fragmentation is that
2182 * the ultimate endpoint must reassemble, instead of the tunnel
2186 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
2187 .path = "map params fragment ignore-df",
2188 .short_help = "map params fragment ignore-df on|off",
2189 .function = map_fragment_df_command_fn,
2193 * Specifiy if the inbound security check should be done on fragments
2196 * @cliexstart{map params security-check fragments}
2198 * Typically the inbound on-decapsulation security check is only done
2199 * on the first packet. The packet that contains the L4
2200 * information. While a security check on every fragment is possible,
2201 * it has a cost. State must be created on the first fragment.
2204 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
2205 .path = "map params security-check fragments",
2206 .short_help = "map params security-check fragments on|off",
2207 .function = map_security_check_frag_command_fn,
2214 * @cliexstart{map add domain}
2217 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2218 .path = "map add domain",
2219 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2220 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2221 "[map-t] [mtu <mtu>]",
2222 .function = map_add_domain_command_fn,
2226 * Add MAP rule to a domain
2229 * @cliexstart{map add rule}
2232 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2233 .path = "map add rule",
2234 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2235 .function = map_add_rule_command_fn,
2242 * @cliexstart{map del domain}
2245 VLIB_CLI_COMMAND(map_del_command, static) = {
2246 .path = "map del domain",
2247 .short_help = "map del domain index <domain>",
2248 .function = map_del_domain_command_fn,
2255 * @cliexstart{show map domain}
2258 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2259 .path = "show map domain",
2260 .short_help = "show map domain index <n> [counters]",
2261 .function = show_map_domain_command_fn,
2265 * Show MAP statistics
2268 * @cliexstart{show map stats}
2271 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2272 .path = "show map stats",
2273 .short_help = "show map stats",
2274 .function = show_map_stats_command_fn,
2278 * Show MAP fragmentation information
2281 * @cliexstart{show map fragments}
2284 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2285 .path = "show map fragments",
2286 .short_help = "show map fragments",
2287 .function = show_map_fragments_command_fn,
2295 map_init (vlib_main_t * vm)
2297 map_main_t *mm = &map_main;
2298 mm->vnet_main = vnet_get_main ();
2301 #ifdef MAP_SKIP_IP6_LOOKUP
2302 fib_protocol_t proto;
2304 FOR_EACH_FIB_PROTOCOL (proto)
2306 map_pre_resolve_init (&pre_resolved[proto]);
2314 /* Inbound security check */
2315 mm->sec_check = true;
2316 mm->sec_check_frag = false;
2318 /* ICMP6 Type 1, Code 5 for security check failure */
2319 mm->icmp6_enabled = false;
2321 /* Inner or outer fragmentation */
2322 mm->frag_inner = false;
2323 mm->frag_ignore_df = false;
2325 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2326 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2327 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2329 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2330 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2332 /* IP4 virtual reassembly */
2333 mm->ip4_reass_hash_table = 0;
2334 mm->ip4_reass_pool = 0;
2335 mm->ip4_reass_lock =
2336 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2337 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2338 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2339 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2340 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2341 mm->ip4_reass_ht_log2len =
2342 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2343 mm->ip4_reass_conf_pool_size);
2344 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2345 map_ip4_reass_reinit (NULL, NULL);
2347 /* IP6 virtual reassembly */
2348 mm->ip6_reass_hash_table = 0;
2349 mm->ip6_reass_pool = 0;
2350 mm->ip6_reass_lock =
2351 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2352 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2353 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2354 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2355 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2356 mm->ip6_reass_ht_log2len =
2357 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2358 mm->ip6_reass_conf_pool_size);
2359 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2360 map_ip6_reass_reinit (NULL, NULL);
2362 #ifdef MAP_SKIP_IP6_LOOKUP
2363 fib_node_register_type (FIB_NODE_TYPE_MAP_E, &map_vft);
2365 map_dpo_module_init ();
2370 VLIB_INIT_FUNCTION (map_init);
2373 * fd.io coding-style-patch-verification: ON
2376 * eval: (c-set-style "gnu")