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);
45 * This code supports the following MAP modes:
47 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
48 * ea_bits_len + ip4_prefix > 32
49 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
50 * Algorithmic Full IPv4 address (ea_bits_len > 0):
51 * ea_bits_len + ip4_prefix = 32
52 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
53 * Algorithmic IPv4 prefix (ea_bits_len > 0):
54 * ea_bits_len + ip4_prefix < 32
55 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
57 * Independent Shared IPv4 address (ea_bits_len = 0):
60 * Rule IPv6 address = 128, Rule PSID Set
61 * Independent Full IPv4 address (ea_bits_len = 0):
63 * psid_length = 0, ip6_prefix = 128
64 * Independent IPv4 prefix (ea_bits_len = 0):
66 * psid_length = 0, ip6_prefix = 128
71 * This code supports MAP-T:
73 * With DMR prefix length equal to 96.
79 ip4_get_port (ip4_header_t * ip, map_dir_e dir, u16 buffer_len)
81 //TODO: use buffer length
82 if (ip->ip_version_and_header_length != 0x45 ||
83 ip4_get_fragment_offset (ip))
86 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
87 (ip->protocol == IP_PROTOCOL_UDP)))
89 udp_header_t *udp = (void *) (ip + 1);
90 return (dir == MAP_SENDER) ? udp->src_port : udp->dst_port;
92 else if (ip->protocol == IP_PROTOCOL_ICMP)
94 icmp46_header_t *icmp = (void *) (ip + 1);
95 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
97 return *((u16 *) (icmp + 1));
99 else if (clib_net_to_host_u16 (ip->length) >= 64)
101 ip = (ip4_header_t *) (icmp + 2);
102 if (PREDICT_TRUE ((ip->protocol == IP_PROTOCOL_TCP) ||
103 (ip->protocol == IP_PROTOCOL_UDP)))
105 udp_header_t *udp = (void *) (ip + 1);
106 return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
108 else if (ip->protocol == IP_PROTOCOL_ICMP)
110 icmp46_header_t *icmp = (void *) (ip + 1);
111 if (icmp->type == ICMP4_echo_request ||
112 icmp->type == ICMP4_echo_reply)
114 return *((u16 *) (icmp + 1));
123 ip6_get_port (ip6_header_t * ip6, map_dir_e dir, u16 buffer_len)
130 if (ip6_parse (ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
133 //TODO: Use buffer length
136 ip6_frag_hdr_offset (((ip6_frag_hdr_t *)
137 u8_ptr_add (ip6, frag_offset))))
138 return -1; //Can't deal with non-first fragment for now
140 l4 = u8_ptr_add (ip6, l4_offset);
141 if (l4_protocol == IP_PROTOCOL_TCP || l4_protocol == IP_PROTOCOL_UDP)
144 MAP_SENDER) ? ((udp_header_t *) (l4))->src_port : ((udp_header_t
148 else if (l4_protocol == IP_PROTOCOL_ICMP6)
150 icmp46_header_t *icmp = (icmp46_header_t *) (l4);
151 if (icmp->type == ICMP6_echo_request)
153 return (dir == MAP_SENDER) ? ((u16 *) (icmp))[2] : -1;
155 else if (icmp->type == ICMP6_echo_reply)
157 return (dir == MAP_SENDER) ? -1 : ((u16 *) (icmp))[2];
165 map_create_domain (ip4_address_t * ip4_prefix,
167 ip6_address_t * ip6_prefix,
169 ip6_address_t * ip6_src,
173 u8 psid_length, u32 * map_domain_index, u16 mtu, u8 flags)
175 u8 suffix_len, suffix_shift;
176 map_main_t *mm = &map_main;
177 dpo_id_t dpo_v4 = DPO_INVALID;
178 dpo_id_t dpo_v6 = DPO_INVALID;
179 fib_node_index_t fei;
182 /* Sanity check on the src prefix length */
183 if (flags & MAP_DOMAIN_TRANSLATION)
185 if (ip6_src_len != 96)
187 clib_warning ("MAP-T only supports ip6_src_len = 96 for now.");
193 if (ip6_src_len != 128)
196 ("MAP-E requires a BR address, not a prefix (ip6_src_len should "
202 /* How many, and which bits to grab from the IPv4 DA */
203 if (ip4_prefix_len + ea_bits_len < 32)
205 flags |= MAP_DOMAIN_PREFIX;
206 suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
207 suffix_len = ea_bits_len;
212 suffix_len = 32 - ip4_prefix_len;
215 /* EA bits must be within the first 64 bits */
216 if (ea_bits_len > 0 && ((ip6_prefix_len + ea_bits_len) > 64 ||
217 ip6_prefix_len + suffix_len + psid_length > 64))
220 ("Embedded Address bits must be within the first 64 bits of "
225 /* Get domain index */
226 pool_get_aligned (mm->domains, d, CLIB_CACHE_LINE_BYTES);
227 memset (d, 0, sizeof (*d));
228 *map_domain_index = d - mm->domains;
230 /* Init domain struct */
231 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
232 d->ip4_prefix_len = ip4_prefix_len;
233 d->ip6_prefix = *ip6_prefix;
234 d->ip6_prefix_len = ip6_prefix_len;
235 d->ip6_src = *ip6_src;
236 d->ip6_src_len = ip6_src_len;
237 d->ea_bits_len = ea_bits_len;
238 d->psid_offset = psid_offset;
239 d->psid_length = psid_length;
242 d->suffix_shift = suffix_shift;
243 d->suffix_mask = (1 << suffix_len) - 1;
245 d->psid_shift = 16 - psid_length - psid_offset;
246 d->psid_mask = (1 << d->psid_length) - 1;
247 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
249 /* MAP data-plane object */
250 if (d->flags & MAP_DOMAIN_TRANSLATION)
251 map_t_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
253 map_dpo_create (DPO_PROTO_IP4, *map_domain_index, &dpo_v4);
255 /* Create ip4 route */
257 .fp_proto = FIB_PROTOCOL_IP4,
258 .fp_len = d->ip4_prefix_len,
260 .ip4 = d->ip4_prefix,
264 fib_table_entry_special_dpo_add (0, &pfx,
266 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
270 * Multiple MAP domains may share same source IPv6 TEP.
271 * In this case the route will exist and be MAP sourced.
272 * Find the adj (if any) already contributed and modify it
274 fib_prefix_t pfx6 = {
275 .fp_proto = FIB_PROTOCOL_IP6,
276 .fp_len = d->ip6_src_len,
282 fei = fib_table_lookup_exact_match (0, &pfx6);
284 if (FIB_NODE_INDEX_INVALID != fei)
286 dpo_id_t dpo = DPO_INVALID;
288 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_MAP, &dpo))
291 * modify the existing MAP to indicate it's shared
294 const dpo_id_t *md_dpo;
297 ASSERT (DPO_LOAD_BALANCE == dpo.dpoi_type);
299 md_dpo = load_balance_get_bucket (dpo.dpoi_index, 0);
300 md = map_dpo_get (md_dpo->dpoi_index);
303 dpo_copy (&dpo_v6, md_dpo);
310 if (d->flags & MAP_DOMAIN_TRANSLATION)
311 map_t_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
313 map_dpo_create (DPO_PROTO_IP6, *map_domain_index, &dpo_v6);
317 * Create ip6 route. This is a reference counted add. If the prefix
318 * already exists and is MAP sourced, it is now MAP source n+1 times
319 * and will need to be removed n+1 times.
321 fib_table_entry_special_dpo_add (0, &pfx6,
323 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v6);
326 /* Validate packet/byte counters */
327 map_domain_counter_lock (mm);
329 for (i = 0; i < vec_len (mm->simple_domain_counters); i++)
331 vlib_validate_simple_counter (&mm->simple_domain_counters[i],
333 vlib_zero_simple_counter (&mm->simple_domain_counters[i],
336 for (i = 0; i < vec_len (mm->domain_counters); i++)
338 vlib_validate_combined_counter (&mm->domain_counters[i],
340 vlib_zero_combined_counter (&mm->domain_counters[i], *map_domain_index);
342 map_domain_counter_unlock (mm);
351 map_delete_domain (u32 map_domain_index)
353 map_main_t *mm = &map_main;
356 if (pool_is_free_index (mm->domains, map_domain_index))
358 clib_warning ("MAP domain delete: domain does not exist: %d",
363 d = pool_elt_at_index (mm->domains, map_domain_index);
366 .fp_proto = FIB_PROTOCOL_IP4,
367 .fp_len = d->ip4_prefix_len,
369 .ip4 = d->ip4_prefix,
373 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_MAP);
375 fib_prefix_t pfx6 = {
376 .fp_proto = FIB_PROTOCOL_IP6,
377 .fp_len = d->ip6_src_len,
383 fib_table_entry_special_remove (0, &pfx6, FIB_SOURCE_MAP);
387 clib_mem_free (d->rules);
389 pool_put (mm->domains, d);
395 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t * tep,
399 map_main_t *mm = &map_main;
401 if (pool_is_free_index (mm->domains, map_domain_index))
403 clib_warning ("MAP rule: domain does not exist: %d", map_domain_index);
406 d = pool_elt_at_index (mm->domains, map_domain_index);
408 /* Rules are only used in 1:1 independent case */
409 if (d->ea_bits_len > 0)
414 u32 l = (0x1 << d->psid_length) * sizeof (ip6_address_t);
415 d->rules = clib_mem_alloc_aligned (l, CLIB_CACHE_LINE_BYTES);
418 memset (d->rules, 0, l);
421 if (psid >= (0x1 << d->psid_length))
423 clib_warning ("MAP rule: PSID outside bounds: %d [%d]", psid,
424 0x1 << d->psid_length);
430 d->rules[psid] = *tep;
434 memset (&d->rules[psid], 0, sizeof (ip6_address_t));
439 #ifdef MAP_SKIP_IP6_LOOKUP
441 map_pre_resolve (ip4_address_t * ip4, ip6_address_t * ip6)
443 map_main_t *mm = &map_main;
444 ip6_main_t *im6 = &ip6_main;
446 if (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0)
449 mm->adj6_index = ip6_fib_table_fwding_lookup (im6, 0, ip6);
450 clib_warning ("FIB lookup results in: %u", mm->adj6_index);
452 if (ip4->as_u32 != 0)
455 mm->adj4_index = ip4_fib_table_lookup_lb (0, ip4);
456 clib_warning ("FIB lookup results in: %u", mm->adj4_index);
461 static clib_error_t *
462 map_security_check_command_fn (vlib_main_t * vm,
463 unformat_input_t * input,
464 vlib_cli_command_t * cmd)
466 unformat_input_t _line_input, *line_input = &_line_input;
467 map_main_t *mm = &map_main;
468 /* Get a line of input. */
469 if (!unformat_user (input, unformat_line_input, line_input))
472 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
474 if (unformat (line_input, "off"))
475 mm->sec_check = false;
476 else if (unformat (line_input, "on"))
477 mm->sec_check = true;
479 return clib_error_return (0, "unknown input `%U'",
480 format_unformat_error, input);
482 unformat_free (line_input);
486 static clib_error_t *
487 map_security_check_frag_command_fn (vlib_main_t * vm,
488 unformat_input_t * input,
489 vlib_cli_command_t * cmd)
491 unformat_input_t _line_input, *line_input = &_line_input;
492 map_main_t *mm = &map_main;
493 /* Get a line of input. */
494 if (!unformat_user (input, unformat_line_input, line_input))
497 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
499 if (unformat (line_input, "off"))
500 mm->sec_check_frag = false;
501 else if (unformat (line_input, "on"))
502 mm->sec_check_frag = true;
504 return clib_error_return (0, "unknown input `%U'",
505 format_unformat_error, input);
507 unformat_free (line_input);
511 static clib_error_t *
512 map_add_domain_command_fn (vlib_main_t * vm,
513 unformat_input_t * input, vlib_cli_command_t * cmd)
515 unformat_input_t _line_input, *line_input = &_line_input;
516 ip4_address_t ip4_prefix;
517 ip6_address_t ip6_prefix;
518 ip6_address_t ip6_src;
519 u32 ip6_prefix_len = 0, ip4_prefix_len = 0, map_domain_index, ip6_src_len;
521 /* Optional arguments */
522 u32 ea_bits_len = 0, psid_offset = 0, psid_length = 0;
527 /* Get a line of input. */
528 if (!unformat_user (input, unformat_line_input, line_input))
531 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
534 (line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix,
539 (line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix,
544 (line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src,
549 (line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
551 else if (unformat (line_input, "ea-bits-len %d", &ea_bits_len))
553 else if (unformat (line_input, "psid-offset %d", &psid_offset))
555 else if (unformat (line_input, "psid-len %d", &psid_length))
557 else if (unformat (line_input, "mtu %d", &mtu))
559 else if (unformat (line_input, "map-t"))
560 flags |= MAP_DOMAIN_TRANSLATION;
562 return clib_error_return (0, "unknown input `%U'",
563 format_unformat_error, input);
565 unformat_free (line_input);
568 return 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,
578 static clib_error_t *
579 map_del_domain_command_fn (vlib_main_t * vm,
580 unformat_input_t * input, vlib_cli_command_t * cmd)
582 unformat_input_t _line_input, *line_input = &_line_input;
584 u32 map_domain_index;
586 /* Get a line of input. */
587 if (!unformat_user (input, unformat_line_input, line_input))
590 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
592 if (unformat (line_input, "index %d", &map_domain_index))
595 return clib_error_return (0, "unknown input `%U'",
596 format_unformat_error, input);
598 unformat_free (line_input);
601 return clib_error_return (0, "mandatory argument(s) missing");
603 map_delete_domain (map_domain_index);
608 static clib_error_t *
609 map_add_rule_command_fn (vlib_main_t * vm,
610 unformat_input_t * input, vlib_cli_command_t * cmd)
612 unformat_input_t _line_input, *line_input = &_line_input;
615 u32 psid = 0, map_domain_index;
617 /* Get a line of input. */
618 if (!unformat_user (input, unformat_line_input, line_input))
621 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
623 if (unformat (line_input, "index %d", &map_domain_index))
625 else if (unformat (line_input, "psid %d", &psid))
628 if (unformat (line_input, "ip6-dst %U", unformat_ip6_address, &tep))
631 return clib_error_return (0, "unknown input `%U'",
632 format_unformat_error, input);
634 unformat_free (line_input);
637 return clib_error_return (0, "mandatory argument(s) missing");
639 if (map_add_del_psid (map_domain_index, psid, &tep, 1) != 0)
641 return clib_error_return (0, "Failing to add Mapping Rule");
646 #if MAP_SKIP_IP6_LOOKUP
647 static clib_error_t *
648 map_pre_resolve_command_fn (vlib_main_t * vm,
649 unformat_input_t * input,
650 vlib_cli_command_t * cmd)
652 unformat_input_t _line_input, *line_input = &_line_input;
655 map_main_t *mm = &map_main;
657 memset (&ip4nh, 0, sizeof (ip4nh));
658 memset (&ip6nh, 0, sizeof (ip6nh));
660 /* Get a line of input. */
661 if (!unformat_user (input, unformat_line_input, line_input))
664 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
666 if (unformat (line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
667 mm->preresolve_ip4 = ip4nh;
669 if (unformat (line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
670 mm->preresolve_ip6 = ip6nh;
672 return clib_error_return (0, "unknown input `%U'",
673 format_unformat_error, input);
675 unformat_free (line_input);
677 map_pre_resolve (&ip4nh, &ip6nh);
683 static clib_error_t *
684 map_icmp_relay_source_address_command_fn (vlib_main_t * vm,
685 unformat_input_t * input,
686 vlib_cli_command_t * cmd)
688 unformat_input_t _line_input, *line_input = &_line_input;
689 ip4_address_t icmp_src_address;
690 map_main_t *mm = &map_main;
692 mm->icmp4_src_address.as_u32 = 0;
694 /* Get a line of input. */
695 if (!unformat_user (input, unformat_line_input, line_input))
698 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
701 (line_input, "%U", unformat_ip4_address, &icmp_src_address))
702 mm->icmp4_src_address = icmp_src_address;
704 return clib_error_return (0, "unknown input `%U'",
705 format_unformat_error, input);
707 unformat_free (line_input);
712 static clib_error_t *
713 map_icmp_unreachables_command_fn (vlib_main_t * vm,
714 unformat_input_t * input,
715 vlib_cli_command_t * cmd)
717 unformat_input_t _line_input, *line_input = &_line_input;
718 map_main_t *mm = &map_main;
721 /* Get a line of input. */
722 if (!unformat_user (input, unformat_line_input, line_input))
725 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
728 if (unformat (line_input, "on"))
729 mm->icmp6_enabled = true;
730 else if (unformat (line_input, "off"))
731 mm->icmp6_enabled = false;
733 return clib_error_return (0, "unknown input `%U'",
734 format_unformat_error, input);
736 unformat_free (line_input);
740 return clib_error_return (0, "mandatory argument(s) missing");
745 static clib_error_t *
746 map_fragment_command_fn (vlib_main_t * vm,
747 unformat_input_t * input, vlib_cli_command_t * cmd)
749 unformat_input_t _line_input, *line_input = &_line_input;
750 map_main_t *mm = &map_main;
752 /* Get a line of input. */
753 if (!unformat_user (input, unformat_line_input, line_input))
756 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
758 if (unformat (line_input, "inner"))
759 mm->frag_inner = true;
760 else if (unformat (line_input, "outer"))
761 mm->frag_inner = false;
763 return clib_error_return (0, "unknown input `%U'",
764 format_unformat_error, input);
766 unformat_free (line_input);
771 static clib_error_t *
772 map_fragment_df_command_fn (vlib_main_t * vm,
773 unformat_input_t * input,
774 vlib_cli_command_t * cmd)
776 unformat_input_t _line_input, *line_input = &_line_input;
777 map_main_t *mm = &map_main;
779 /* Get a line of input. */
780 if (!unformat_user (input, unformat_line_input, line_input))
783 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
785 if (unformat (line_input, "on"))
786 mm->frag_ignore_df = true;
787 else if (unformat (line_input, "off"))
788 mm->frag_ignore_df = false;
790 return clib_error_return (0, "unknown input `%U'",
791 format_unformat_error, input);
793 unformat_free (line_input);
798 static clib_error_t *
799 map_traffic_class_command_fn (vlib_main_t * vm,
800 unformat_input_t * input,
801 vlib_cli_command_t * cmd)
803 unformat_input_t _line_input, *line_input = &_line_input;
804 map_main_t *mm = &map_main;
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, "copy"))
817 else if (unformat (line_input, "%x", &tc))
820 return clib_error_return (0, "unknown input `%U'",
821 format_unformat_error, input);
823 unformat_free (line_input);
829 format_map_domain (u8 * s, va_list * args)
831 map_domain_t *d = va_arg (*args, map_domain_t *);
832 bool counters = va_arg (*args, int);
833 map_main_t *mm = &map_main;
834 ip6_address_t ip6_prefix;
837 memset (&ip6_prefix, 0, sizeof (ip6_prefix));
839 ip6_prefix = d->ip6_prefix;
842 "[%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",
844 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
845 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
846 format_ip6_address, &d->ip6_src, d->ip6_src_len,
847 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
848 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
852 map_domain_counter_lock (mm);
854 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_TX],
855 d - mm->domains, &v);
856 s = format (s, " TX: %lld/%lld", v.packets, v.bytes);
857 vlib_get_combined_counter (&mm->domain_counters[MAP_DOMAIN_COUNTER_RX],
858 d - mm->domains, &v);
859 s = format (s, " RX: %lld/%lld", v.packets, v.bytes);
860 map_domain_counter_unlock (mm);
862 s = format (s, "\n");
868 for (i = 0; i < (0x1 << d->psid_length); i++)
871 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
874 " rule psid: %d ip6-dst %U\n", i, format_ip6_address,
882 format_map_ip4_reass (u8 * s, va_list * args)
884 map_main_t *mm = &map_main;
885 map_ip4_reass_t *r = va_arg (*args, map_ip4_reass_t *);
886 map_ip4_reass_key_t *k = &r->key;
887 f64 now = vlib_time_now (mm->vlib_main);
888 f64 lifetime = (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000);
889 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
891 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
892 format_ip4_address, &k->src.as_u8, format_ip4_address,
893 &k->dst.as_u8, k->protocol,
894 clib_net_to_host_u16 (k->fragment_id),
895 (r->port >= 0) ? clib_net_to_host_u16 (r->port) : -1, dt);
900 format_map_ip6_reass (u8 * s, va_list * args)
902 map_main_t *mm = &map_main;
903 map_ip6_reass_t *r = va_arg (*args, map_ip6_reass_t *);
904 map_ip6_reass_key_t *k = &r->key;
905 f64 now = vlib_time_now (mm->vlib_main);
906 f64 lifetime = (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000);
907 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
909 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
910 format_ip6_address, &k->src.as_u8, format_ip6_address,
911 &k->dst.as_u8, k->protocol,
912 clib_net_to_host_u32 (k->fragment_id), dt);
916 static clib_error_t *
917 show_map_domain_command_fn (vlib_main_t * vm, unformat_input_t * input,
918 vlib_cli_command_t * cmd)
920 unformat_input_t _line_input, *line_input = &_line_input;
921 map_main_t *mm = &map_main;
923 bool counters = false;
924 u32 map_domain_index = ~0;
926 /* Get a line of input. */
927 if (!unformat_user (input, unformat_line_input, line_input))
930 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
932 if (unformat (line_input, "counters"))
934 else if (unformat (line_input, "index %d", &map_domain_index))
937 return clib_error_return (0, "unknown input `%U'",
938 format_unformat_error, input);
940 unformat_free (line_input);
942 if (pool_elts (mm->domains) == 0)
943 vlib_cli_output (vm, "No MAP domains are configured...");
945 if (map_domain_index == ~0)
948 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
953 if (pool_is_free_index (mm->domains, map_domain_index))
955 return clib_error_return (0, "MAP domain does not exists %d",
959 d = pool_elt_at_index (mm->domains, map_domain_index);
960 vlib_cli_output (vm, "%U", format_map_domain, d, counters);
966 static clib_error_t *
967 show_map_fragments_command_fn (vlib_main_t * vm, unformat_input_t * input,
968 vlib_cli_command_t * cmd)
970 map_main_t *mm = &map_main;
975 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
978 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
984 map_error_counter_get (u32 node_index, map_error_t map_error)
986 vlib_main_t *vm = vlib_get_main ();
987 vlib_node_runtime_t *error_node = vlib_node_get_runtime (vm, node_index);
988 vlib_error_main_t *em = &vm->error_main;
989 vlib_error_t e = error_node->errors[map_error];
990 vlib_node_t *n = vlib_get_node (vm, node_index);
993 ci = vlib_error_get_code (e);
994 ASSERT (ci < n->n_errors);
995 ci += n->error_heap_index;
997 return (em->counters[ci]);
1000 static clib_error_t *
1001 show_map_stats_command_fn (vlib_main_t * vm, unformat_input_t * input,
1002 vlib_cli_command_t * cmd)
1004 map_main_t *mm = &map_main;
1006 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
1007 if (pool_elts (mm->domains) == 0)
1008 vlib_cli_output (vm, "No MAP domains are configured...");
1011 pool_foreach(d, mm->domains, ({
1013 rulecount+= 0x1 << d->psid_length;
1014 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
1016 domains += sizeof(*d);
1021 vlib_cli_output (vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
1022 vlib_cli_output (vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
1023 vlib_cli_output (vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
1024 vlib_cli_output (vm, "Total: %d bytes)\n", rules + domains);
1026 #if MAP_SKIP_IP6_LOOKUP
1027 vlib_cli_output (vm,
1028 "MAP pre-resolve: IP6 next-hop: %U (%u), IP4 next-hop: %U (%u)\n",
1029 format_ip6_address, &mm->preresolve_ip6, mm->adj6_index,
1030 format_ip4_address, &mm->preresolve_ip4, mm->adj4_index);
1034 vlib_cli_output (vm, "MAP traffic-class: copy");
1036 vlib_cli_output (vm, "MAP traffic-class: %x", mm->tc);
1038 vlib_cli_output (vm,
1039 "MAP IPv6 inbound security check: %s, fragmented packet security check: %s",
1040 mm->sec_check ? "enabled" : "disabled",
1041 mm->sec_check_frag ? "enabled" : "disabled");
1043 vlib_cli_output (vm, "ICMP-relay IPv4 source address: %U\n",
1044 format_ip4_address, &mm->icmp4_src_address);
1045 vlib_cli_output (vm, "ICMP6 unreachables sent for unmatched packets: %s\n",
1046 mm->icmp6_enabled ? "enabled" : "disabled");
1047 vlib_cli_output (vm, "Inner fragmentation: %s\n",
1048 mm->frag_inner ? "enabled" : "disabled");
1049 vlib_cli_output (vm, "Fragment packets regardless of DF flag: %s\n",
1050 mm->frag_ignore_df ? "enabled" : "disabled");
1055 vlib_combined_counter_main_t *cm = mm->domain_counters;
1056 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
1057 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
1061 memset (total_pkts, 0, sizeof (total_pkts));
1062 memset (total_bytes, 0, sizeof (total_bytes));
1064 map_domain_counter_lock (mm);
1065 vec_foreach (cm, mm->domain_counters)
1067 which = cm - mm->domain_counters;
1069 for (i = 0; i < vec_len (cm->maxi); i++)
1071 vlib_get_combined_counter (cm, i, &v);
1072 total_pkts[which] += v.packets;
1073 total_bytes[which] += v.bytes;
1076 map_domain_counter_unlock (mm);
1078 vlib_cli_output (vm, "Encapsulated packets: %lld bytes: %lld\n",
1079 total_pkts[MAP_DOMAIN_COUNTER_TX],
1080 total_bytes[MAP_DOMAIN_COUNTER_TX]);
1081 vlib_cli_output (vm, "Decapsulated packets: %lld bytes: %lld\n",
1082 total_pkts[MAP_DOMAIN_COUNTER_RX],
1083 total_bytes[MAP_DOMAIN_COUNTER_RX]);
1085 vlib_cli_output (vm, "ICMP relayed packets: %d\n",
1086 vlib_get_simple_counter (&mm->icmp_relayed, 0));
1091 static clib_error_t *
1092 map_params_reass_command_fn (vlib_main_t * vm, unformat_input_t * input,
1093 vlib_cli_command_t * cmd)
1095 unformat_input_t _line_input, *line_input = &_line_input;
1097 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1);
1099 u64 buffers = ~(0ull);
1100 u8 ip4 = 0, ip6 = 0;
1102 if (!unformat_user (input, unformat_line_input, line_input))
1105 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1107 if (unformat (line_input, "lifetime %u", &lifetime))
1109 else if (unformat (line_input, "ht-ratio %lf", &ht_ratio))
1111 else if (unformat (line_input, "pool-size %u", &pool_size))
1113 else if (unformat (line_input, "buffers %llu", &buffers))
1115 else if (unformat (line_input, "ip4"))
1117 else if (unformat (line_input, "ip6"))
1121 unformat_free (line_input);
1122 return clib_error_return (0, "invalid input");
1125 unformat_free (line_input);
1128 return clib_error_return (0, "must specify ip4 and/or ip6");
1132 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1133 return clib_error_return (0, "invalid ip4-reass pool-size ( > %d)",
1134 MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1135 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1136 && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1137 return clib_error_return (0, "invalid ip4-reass ht-ratio ( > %d)",
1138 MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1139 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1140 return clib_error_return (0, "invalid ip4-reass lifetime ( > %d)",
1141 MAP_IP4_REASS_CONF_LIFETIME_MAX);
1142 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1143 return clib_error_return (0, "invalid ip4-reass buffers ( > %ld)",
1144 MAP_IP4_REASS_CONF_BUFFERS_MAX);
1149 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1150 return clib_error_return (0, "invalid ip6-reass pool-size ( > %d)",
1151 MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1152 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1)
1153 && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1154 return clib_error_return (0, "invalid ip6-reass ht-log2len ( > %d)",
1155 MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1156 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1157 return clib_error_return (0, "invalid ip6-reass lifetime ( > %d)",
1158 MAP_IP6_REASS_CONF_LIFETIME_MAX);
1159 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1160 return clib_error_return (0, "invalid ip6-reass buffers ( > %ld)",
1161 MAP_IP6_REASS_CONF_BUFFERS_MAX);
1166 u32 reass = 0, packets = 0;
1167 if (pool_size != ~0)
1169 if (map_ip4_reass_conf_pool_size (pool_size, &reass, &packets))
1171 vlib_cli_output (vm, "Could not set ip4-reass pool-size");
1175 vlib_cli_output (vm,
1176 "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1180 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1182 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1184 vlib_cli_output (vm, "Could not set ip4-reass ht-log2len");
1188 vlib_cli_output (vm,
1189 "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1195 if (map_ip4_reass_conf_lifetime (lifetime))
1196 vlib_cli_output (vm, "Could not set ip4-reass lifetime");
1198 vlib_cli_output (vm, "Setting ip4-reass lifetime");
1200 if (buffers != ~(0ull))
1202 if (map_ip4_reass_conf_buffers (buffers))
1203 vlib_cli_output (vm, "Could not set ip4-reass buffers");
1205 vlib_cli_output (vm, "Setting ip4-reass buffers");
1208 if (map_main.ip4_reass_conf_buffers >
1209 map_main.ip4_reass_conf_pool_size *
1210 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1212 vlib_cli_output (vm,
1213 "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1219 u32 reass = 0, packets = 0;
1220 if (pool_size != ~0)
1222 if (map_ip6_reass_conf_pool_size (pool_size, &reass, &packets))
1224 vlib_cli_output (vm, "Could not set ip6-reass pool-size");
1228 vlib_cli_output (vm,
1229 "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)",
1233 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
1235 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &reass, &packets))
1237 vlib_cli_output (vm, "Could not set ip6-reass ht-log2len");
1241 vlib_cli_output (vm,
1242 "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)",
1248 if (map_ip6_reass_conf_lifetime (lifetime))
1249 vlib_cli_output (vm, "Could not set ip6-reass lifetime");
1251 vlib_cli_output (vm, "Setting ip6-reass lifetime");
1253 if (buffers != ~(0ull))
1255 if (map_ip6_reass_conf_buffers (buffers))
1256 vlib_cli_output (vm, "Could not set ip6-reass buffers");
1258 vlib_cli_output (vm, "Setting ip6-reass buffers");
1261 if (map_main.ip6_reass_conf_buffers >
1262 map_main.ip6_reass_conf_pool_size *
1263 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
1265 vlib_cli_output (vm,
1266 "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1275 * packet trace format function
1278 format_map_trace (u8 * s, va_list * args)
1280 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1281 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1282 map_trace_t *t = va_arg (*args, map_trace_t *);
1283 u32 map_domain_index = t->map_domain_index;
1287 format (s, "MAP domain index: %d L4 port: %u", map_domain_index,
1288 clib_net_to_host_u16 (port));
1293 static_always_inline map_ip4_reass_t *
1294 map_ip4_reass_lookup (map_ip4_reass_key_t * k, u32 bucket, f64 now)
1296 map_main_t *mm = &map_main;
1297 u32 ri = mm->ip4_reass_hash_table[bucket];
1298 while (ri != MAP_REASS_INDEX_NONE)
1300 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1301 if (r->key.as_u64[0] == k->as_u64[0] &&
1302 r->key.as_u64[1] == k->as_u64[1] &&
1303 now < r->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000))
1307 ri = r->bucket_next;
1312 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1315 map_ip4_reass_free (map_ip4_reass_t * r, u32 ** pi_to_drop)
1317 map_main_t *mm = &map_main;
1318 map_ip4_reass_get_fragments (r, pi_to_drop);
1320 // Unlink in hash bucket
1321 map_ip4_reass_t *r2 = NULL;
1322 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1323 while (r2i != map_ip4_reass_pool_index (r))
1325 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1326 r2 = pool_elt_at_index (mm->ip4_reass_pool, r2i);
1327 r2i = r2->bucket_next;
1331 r2->bucket_next = r->bucket_next;
1335 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1339 if (r->fifo_next == map_ip4_reass_pool_index (r))
1341 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1345 if (mm->ip4_reass_fifo_last == map_ip4_reass_pool_index (r))
1346 mm->ip4_reass_fifo_last = r->fifo_prev;
1347 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next =
1349 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev =
1353 pool_put (mm->ip4_reass_pool, r);
1354 mm->ip4_reass_allocated--;
1358 map_ip4_reass_get (u32 src, u32 dst, u16 fragment_id,
1359 u8 protocol, u32 ** pi_to_drop)
1362 map_main_t *mm = &map_main;
1363 map_ip4_reass_key_t k = {.src.data_u32 = src,
1364 .dst.data_u32 = dst,
1365 .fragment_id = fragment_id,
1366 .protocol = protocol
1370 h = crc_u32 (k.as_u32[0], h);
1371 h = crc_u32 (k.as_u32[1], h);
1372 h = crc_u32 (k.as_u32[2], h);
1373 h = crc_u32 (k.as_u32[3], h);
1374 h = h >> (32 - mm->ip4_reass_ht_log2len);
1376 f64 now = vlib_time_now (mm->vlib_main);
1378 //Cache garbage collection
1379 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1381 map_ip4_reass_t *last =
1382 pool_elt_at_index (mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1383 if (last->ts + (((f64) mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1384 map_ip4_reass_free (last, pi_to_drop);
1389 if ((r = map_ip4_reass_lookup (&k, h, now)))
1392 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1395 pool_get (mm->ip4_reass_pool, r);
1396 mm->ip4_reass_allocated++;
1398 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1399 r->fragments[i] = ~0;
1401 u32 ri = map_ip4_reass_pool_index (r);
1403 //Link in new bucket
1405 r->bucket_next = mm->ip4_reass_hash_table[h];
1406 mm->ip4_reass_hash_table[h] = ri;
1409 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1412 pool_elt_at_index (mm->ip4_reass_pool,
1413 mm->ip4_reass_fifo_last)->fifo_next;
1414 r->fifo_prev = mm->ip4_reass_fifo_last;
1415 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1416 pool_elt_at_index (mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1420 r->fifo_next = r->fifo_prev = ri;
1421 mm->ip4_reass_fifo_last = ri;
1428 #ifdef MAP_IP4_REASS_COUNT_BYTES
1429 r->expected_total = 0xffff;
1437 map_ip4_reass_add_fragment (map_ip4_reass_t * r, u32 pi)
1439 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1443 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1444 if (r->fragments[i] == ~0)
1446 r->fragments[i] = pi;
1447 map_main.ip4_reass_buffered_counter++;
1453 static_always_inline map_ip6_reass_t *
1454 map_ip6_reass_lookup (map_ip6_reass_key_t * k, u32 bucket, f64 now)
1456 map_main_t *mm = &map_main;
1457 u32 ri = mm->ip6_reass_hash_table[bucket];
1458 while (ri != MAP_REASS_INDEX_NONE)
1460 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1461 if (now < r->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1462 r->key.as_u64[0] == k->as_u64[0] &&
1463 r->key.as_u64[1] == k->as_u64[1] &&
1464 r->key.as_u64[2] == k->as_u64[2] &&
1465 r->key.as_u64[3] == k->as_u64[3] &&
1466 r->key.as_u64[4] == k->as_u64[4])
1468 ri = r->bucket_next;
1473 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1476 map_ip6_reass_free (map_ip6_reass_t * r, u32 ** pi_to_drop)
1478 map_main_t *mm = &map_main;
1480 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1481 if (r->fragments[i].pi != ~0)
1483 vec_add1 (*pi_to_drop, r->fragments[i].pi);
1484 r->fragments[i].pi = ~0;
1485 map_main.ip6_reass_buffered_counter--;
1488 // Unlink in hash bucket
1489 map_ip6_reass_t *r2 = NULL;
1490 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1491 while (r2i != map_ip6_reass_pool_index (r))
1493 ASSERT (r2i != MAP_REASS_INDEX_NONE);
1494 r2 = pool_elt_at_index (mm->ip6_reass_pool, r2i);
1495 r2i = r2->bucket_next;
1499 r2->bucket_next = r->bucket_next;
1503 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1507 if (r->fifo_next == map_ip6_reass_pool_index (r))
1509 //Single element in the list, list is now empty
1510 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1514 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index (r)) //First element
1515 mm->ip6_reass_fifo_last = r->fifo_prev;
1516 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next =
1518 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev =
1522 // Free from pool if necessary
1523 pool_put (mm->ip6_reass_pool, r);
1524 mm->ip6_reass_allocated--;
1528 map_ip6_reass_get (ip6_address_t * src, ip6_address_t * dst, u32 fragment_id,
1529 u8 protocol, u32 ** pi_to_drop)
1532 map_main_t *mm = &map_main;
1533 map_ip6_reass_key_t k = {
1536 .fragment_id = fragment_id,
1537 .protocol = protocol
1542 for (i = 0; i < 10; i++)
1543 h = crc_u32 (k.as_u32[i], h);
1544 h = h >> (32 - mm->ip6_reass_ht_log2len);
1546 f64 now = vlib_time_now (mm->vlib_main);
1548 //Cache garbage collection
1549 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1551 map_ip6_reass_t *last =
1552 pool_elt_at_index (mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1553 if (last->ts + (((f64) mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1554 map_ip6_reass_free (last, pi_to_drop);
1559 if ((r = map_ip6_reass_lookup (&k, h, now)))
1562 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1565 pool_get (mm->ip6_reass_pool, r);
1566 mm->ip6_reass_allocated++;
1567 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1569 r->fragments[i].pi = ~0;
1570 r->fragments[i].next_data_len = 0;
1571 r->fragments[i].next_data_offset = 0;
1574 u32 ri = map_ip6_reass_pool_index (r);
1576 //Link in new bucket
1578 r->bucket_next = mm->ip6_reass_hash_table[h];
1579 mm->ip6_reass_hash_table[h] = ri;
1582 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1585 pool_elt_at_index (mm->ip6_reass_pool,
1586 mm->ip6_reass_fifo_last)->fifo_next;
1587 r->fifo_prev = mm->ip6_reass_fifo_last;
1588 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1589 pool_elt_at_index (mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1593 r->fifo_next = r->fifo_prev = ri;
1594 mm->ip6_reass_fifo_last = ri;
1600 r->ip4_header.ip_version_and_header_length = 0;
1601 #ifdef MAP_IP6_REASS_COUNT_BYTES
1602 r->expected_total = 0xffff;
1609 map_ip6_reass_add_fragment (map_ip6_reass_t * r, u32 pi,
1610 u16 data_offset, u16 next_data_offset,
1611 u8 * data_start, u16 data_len)
1613 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1614 u16 copied_len = (data_len > 20) ? 20 : data_len;
1616 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1619 //Lookup for fragments for the current buffer
1620 //and the one before that
1622 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1624 if (data_offset && r->fragments[i].next_data_offset == data_offset)
1626 prev_f = &r->fragments[i]; // This is buffer for previous packet
1628 else if (r->fragments[i].next_data_offset == next_data_offset)
1630 f = &r->fragments[i]; // This is a buffer for the current packet
1632 else if (r->fragments[i].next_data_offset == 0)
1635 f = &r->fragments[i];
1636 else if (prev_f == NULL)
1637 prev_f = &r->fragments[i];
1641 if (!f || f->pi != ~0)
1649 clib_memcpy (prev_f->next_data, data_start, copied_len);
1650 prev_f->next_data_len = copied_len;
1651 prev_f->next_data_offset = data_offset;
1655 if (((ip4_header_t *) data_start)->ip_version_and_header_length != 0x45)
1658 if (r->ip4_header.ip_version_and_header_length == 0)
1659 clib_memcpy (&r->ip4_header, data_start, sizeof (ip4_header_t));
1664 f->next_data_offset = next_data_offset;
1666 map_main.ip6_reass_buffered_counter++;
1672 map_ip4_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1674 map_main_t *mm = &map_main;
1677 if (dropped_packets)
1678 *dropped_packets = mm->ip4_reass_buffered_counter;
1680 *trashed_reass = mm->ip4_reass_allocated;
1681 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE)
1683 u16 ri = mm->ip4_reass_fifo_last;
1686 map_ip4_reass_t *r = pool_elt_at_index (mm->ip4_reass_pool, ri);
1687 for (i = 0; i < MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1688 if (r->fragments[i] != ~0)
1689 map_ip4_drop_pi (r->fragments[i]);
1692 pool_put (mm->ip4_reass_pool, r);
1694 while (ri != mm->ip4_reass_fifo_last);
1697 vec_free (mm->ip4_reass_hash_table);
1698 vec_resize (mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1699 for (i = 0; i < (1 << mm->ip4_reass_ht_log2len); i++)
1700 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1701 pool_free (mm->ip4_reass_pool);
1702 pool_alloc (mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1704 mm->ip4_reass_allocated = 0;
1705 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1706 mm->ip4_reass_buffered_counter = 0;
1710 map_get_ht_log2len (f32 ht_ratio, u16 pool_size)
1712 u32 desired_size = (u32) (pool_size * ht_ratio);
1714 for (i = 1; i < 31; i++)
1715 if ((1 << i) >= desired_size)
1721 map_ip4_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1722 u32 * dropped_packets)
1724 map_main_t *mm = &map_main;
1725 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1728 map_ip4_reass_lock ();
1729 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1730 mm->ip4_reass_ht_log2len =
1731 map_get_ht_log2len (ht_ratio, mm->ip4_reass_conf_pool_size);
1732 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1733 map_ip4_reass_unlock ();
1738 map_ip4_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1739 u32 * dropped_packets)
1741 map_main_t *mm = &map_main;
1742 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1745 map_ip4_reass_lock ();
1746 mm->ip4_reass_conf_pool_size = pool_size;
1747 map_ip4_reass_reinit (trashed_reass, dropped_packets);
1748 map_ip4_reass_unlock ();
1753 map_ip4_reass_conf_lifetime (u16 lifetime_ms)
1755 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1760 map_ip4_reass_conf_buffers (u32 buffers)
1762 map_main.ip4_reass_conf_buffers = buffers;
1767 map_ip6_reass_reinit (u32 * trashed_reass, u32 * dropped_packets)
1769 map_main_t *mm = &map_main;
1770 if (dropped_packets)
1771 *dropped_packets = mm->ip6_reass_buffered_counter;
1773 *trashed_reass = mm->ip6_reass_allocated;
1775 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE)
1777 u16 ri = mm->ip6_reass_fifo_last;
1780 map_ip6_reass_t *r = pool_elt_at_index (mm->ip6_reass_pool, ri);
1781 for (i = 0; i < MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1782 if (r->fragments[i].pi != ~0)
1783 map_ip6_drop_pi (r->fragments[i].pi);
1786 pool_put (mm->ip6_reass_pool, r);
1788 while (ri != mm->ip6_reass_fifo_last);
1789 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1792 vec_free (mm->ip6_reass_hash_table);
1793 vec_resize (mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1794 for (i = 0; i < (1 << mm->ip6_reass_ht_log2len); i++)
1795 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1796 pool_free (mm->ip6_reass_pool);
1797 pool_alloc (mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1799 mm->ip6_reass_allocated = 0;
1800 mm->ip6_reass_buffered_counter = 0;
1804 map_ip6_reass_conf_ht_ratio (f32 ht_ratio, u32 * trashed_reass,
1805 u32 * dropped_packets)
1807 map_main_t *mm = &map_main;
1808 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1811 map_ip6_reass_lock ();
1812 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1813 mm->ip6_reass_ht_log2len =
1814 map_get_ht_log2len (ht_ratio, mm->ip6_reass_conf_pool_size);
1815 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1816 map_ip6_reass_unlock ();
1821 map_ip6_reass_conf_pool_size (u16 pool_size, u32 * trashed_reass,
1822 u32 * dropped_packets)
1824 map_main_t *mm = &map_main;
1825 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1828 map_ip6_reass_lock ();
1829 mm->ip6_reass_conf_pool_size = pool_size;
1830 map_ip6_reass_reinit (trashed_reass, dropped_packets);
1831 map_ip6_reass_unlock ();
1836 map_ip6_reass_conf_lifetime (u16 lifetime_ms)
1838 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1843 map_ip6_reass_conf_buffers (u32 buffers)
1845 map_main.ip6_reass_conf_buffers = buffers;
1852 * Configure MAP reassembly behaviour
1855 * @cliexstart{map params reassembly}
1858 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1859 .path = "map params reassembly",
1860 .short_help = "map params reassembly [ip4 | ip6] [lifetime <lifetime-ms>] "
1861 "[pool-size <pool-size>] [buffers <buffers>] "
1862 "[ht-ratio <ht-ratio>]",
1863 .function = map_params_reass_command_fn,
1867 * Set or copy the IP TOS/Traffic Class field
1870 * @cliexstart{map params traffic-class}
1872 * This command is used to set the traffic-class field in translated
1873 * or encapsulated packets. If copy is specifed (the default) then the
1874 * traffic-class/TOS field is copied from the original packet to the
1875 * translated / encapsulating header.
1878 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1879 .path = "map params traffic-class",
1880 .short_help = "map params traffic-class {0x0-0xff | copy}",
1881 .function = map_traffic_class_command_fn,
1885 * Bypass IP4/IP6 lookup
1888 * @cliexstart{map params pre-resolve}
1890 * Bypass a second FIB lookup of the translated or encapsulated
1891 * packet, and forward the packet directly to the specified
1892 * next-hop. This optimization trades forwarding flexibility for
1896 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1897 .path = "map params pre-resolve",
1898 .short_help = " map params pre-resolve {ip4-nh <address>} "
1899 "| {ip6-nh <address>}",
1900 .function = map_pre_resolve_command_fn,
1904 * Enable or disable the MAP-E inbound security check
1907 * @cliexstart{map params security-check}
1909 * By default, a decapsulated packet's IPv4 source address will be
1910 * verified against the outer header's IPv6 source address. Disabling
1911 * this feature will allow IPv4 source address spoofing.
1914 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1915 .path = "map params security-check",
1916 .short_help = "map params security-check on|off",
1917 .function = map_security_check_command_fn,
1921 * Specifiy the IPv4 source address used for relayed ICMP error messages
1924 * @cliexstart{map params icmp source-address}
1926 * This command specifies which IPv4 source address (must be local to
1927 * the system), that is used for relayed received IPv6 ICMP error
1931 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
1932 .path = "map params icmp source-address",
1933 .short_help = "map params icmp source-address <ip4-address>",
1934 .function = map_icmp_relay_source_address_command_fn,
1938 * Send IPv6 ICMP unreachables
1941 * @cliexstart{map params icmp6 unreachables}
1943 * Send IPv6 ICMP unreachable messages back if security check fails or
1944 * no MAP domain exists.
1947 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
1948 .path = "map params icmp6 unreachables",
1949 .short_help = "map params icmp6 unreachables {on|off}",
1950 .function = map_icmp_unreachables_command_fn,
1954 * Configure MAP fragmentation behaviour
1957 * @cliexstart{map params fragment}
1960 VLIB_CLI_COMMAND(map_fragment_command, static) = {
1961 .path = "map params fragment",
1962 .short_help = "map params fragment inner|outer",
1963 .function = map_fragment_command_fn,
1967 * Ignore the IPv4 Don't fragment bit
1970 * @cliexstart{map params fragment ignore-df}
1972 * Allows fragmentation of the IPv4 packet even if the DF bit is
1973 * set. The choice between inner or outer fragmentation of tunnel
1974 * packets is complicated. The benefit of inner fragmentation is that
1975 * the ultimate endpoint must reassemble, instead of the tunnel
1979 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
1980 .path = "map params fragment ignore-df",
1981 .short_help = "map params fragment ignore-df on|off",
1982 .function = map_fragment_df_command_fn,
1986 * Specifiy if the inbound security check should be done on fragments
1989 * @cliexstart{map params security-check fragments}
1991 * Typically the inbound on-decapsulation security check is only done
1992 * on the first packet. The packet that contains the L4
1993 * information. While a security check on every fragment is possible,
1994 * it has a cost. State must be created on the first fragment.
1997 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
1998 .path = "map params security-check fragments",
1999 .short_help = "map params security-check fragments on|off",
2000 .function = map_security_check_frag_command_fn,
2007 * @cliexstart{map add domain}
2010 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
2011 .path = "map add domain",
2012 .short_help = "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> "
2013 "ip6-src <ip6-pfx> ea-bits-len <n> psid-offset <n> psid-len <n> "
2014 "[map-t] [mtu <mtu>]",
2015 .function = map_add_domain_command_fn,
2019 * Add MAP rule to a domain
2022 * @cliexstart{map add rule}
2025 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
2026 .path = "map add rule",
2027 .short_help = "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
2028 .function = map_add_rule_command_fn,
2035 * @cliexstart{map del domain}
2038 VLIB_CLI_COMMAND(map_del_command, static) = {
2039 .path = "map del domain",
2040 .short_help = "map del domain index <domain>",
2041 .function = map_del_domain_command_fn,
2048 * @cliexstart{show map domain}
2051 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
2052 .path = "show map domain",
2053 .short_help = "show map domain index <n> [counters]",
2054 .function = show_map_domain_command_fn,
2058 * Show MAP statistics
2061 * @cliexstart{show map stats}
2064 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
2065 .path = "show map stats",
2066 .short_help = "show map stats",
2067 .function = show_map_stats_command_fn,
2071 * Show MAP fragmentation information
2074 * @cliexstart{show map fragments}
2077 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
2078 .path = "show map fragments",
2079 .short_help = "show map fragments",
2080 .function = show_map_fragments_command_fn,
2088 map_init (vlib_main_t * vm)
2090 map_main_t *mm = &map_main;
2091 mm->vnet_main = vnet_get_main ();
2094 #ifdef MAP_SKIP_IP6_LOOKUP
2095 memset (&mm->preresolve_ip4, 0, sizeof (mm->preresolve_ip4));
2096 memset (&mm->preresolve_ip6, 0, sizeof (mm->preresolve_ip6));
2105 /* Inbound security check */
2106 mm->sec_check = true;
2107 mm->sec_check_frag = false;
2109 /* ICMP6 Type 1, Code 5 for security check failure */
2110 mm->icmp6_enabled = false;
2112 /* Inner or outer fragmentation */
2113 mm->frag_inner = false;
2114 mm->frag_ignore_df = false;
2116 vec_validate (mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
2117 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
2118 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
2120 vlib_validate_simple_counter (&mm->icmp_relayed, 0);
2121 vlib_zero_simple_counter (&mm->icmp_relayed, 0);
2123 /* IP4 virtual reassembly */
2124 mm->ip4_reass_hash_table = 0;
2125 mm->ip4_reass_pool = 0;
2126 mm->ip4_reass_lock =
2127 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2128 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
2129 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
2130 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
2131 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
2132 mm->ip4_reass_ht_log2len =
2133 map_get_ht_log2len (mm->ip4_reass_conf_ht_ratio,
2134 mm->ip4_reass_conf_pool_size);
2135 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
2136 map_ip4_reass_reinit (NULL, NULL);
2138 /* IP6 virtual reassembly */
2139 mm->ip6_reass_hash_table = 0;
2140 mm->ip6_reass_pool = 0;
2141 mm->ip6_reass_lock =
2142 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
2143 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
2144 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
2145 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
2146 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
2147 mm->ip6_reass_ht_log2len =
2148 map_get_ht_log2len (mm->ip6_reass_conf_ht_ratio,
2149 mm->ip6_reass_conf_pool_size);
2150 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
2151 map_ip6_reass_reinit (NULL, NULL);
2153 map_dpo_module_init ();
2158 VLIB_INIT_FUNCTION (map_init);
2161 * fd.io coding-style-patch-verification: ON
2164 * eval: (c-set-style "gnu")