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.
21 * This code supports the following MAP modes:
23 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
24 * ea_bits_len + ip4_prefix > 32
25 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
26 * Algorithmic Full IPv4 address (ea_bits_len > 0):
27 * ea_bits_len + ip4_prefix = 32
28 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
29 * Algorithmic IPv4 prefix (ea_bits_len > 0):
30 * ea_bits_len + ip4_prefix < 32
31 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
33 * Independent Shared IPv4 address (ea_bits_len = 0):
36 * Rule IPv6 address = 128, Rule PSID Set
37 * Independent Full IPv4 address (ea_bits_len = 0):
39 * psid_length = 0, ip6_prefix = 128
40 * Independent IPv4 prefix (ea_bits_len = 0):
42 * psid_length = 0, ip6_prefix = 128
47 * This code supports MAP-T:
49 * With DMR prefix length equal to 96.
55 ip4_get_port (ip4_header_t *ip, map_dir_e dir, u16 buffer_len)
57 //TODO: use buffer length
58 if (ip->ip_version_and_header_length != 0x45 ||
59 ip4_get_fragment_offset(ip))
62 if (PREDICT_TRUE((ip->protocol == IP_PROTOCOL_TCP) ||
63 (ip->protocol == IP_PROTOCOL_UDP))) {
64 udp_header_t *udp = (void *)(ip + 1);
65 return (dir == MAP_SENDER) ? udp->src_port : udp->dst_port;
66 } else if (ip->protocol == IP_PROTOCOL_ICMP) {
67 icmp46_header_t *icmp = (void *)(ip + 1);
68 if (icmp->type == ICMP4_echo_request ||
69 icmp->type == ICMP4_echo_reply) {
70 return *((u16 *)(icmp + 1));
71 } else if (clib_net_to_host_u16(ip->length) >= 64) {
72 ip = (ip4_header_t *)(icmp + 2);
73 if (PREDICT_TRUE((ip->protocol == IP_PROTOCOL_TCP) ||
74 (ip->protocol == IP_PROTOCOL_UDP))) {
75 udp_header_t *udp = (void *)(ip + 1);
76 return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
77 } else if (ip->protocol == IP_PROTOCOL_ICMP) {
78 icmp46_header_t *icmp = (void *)(ip + 1);
79 if (icmp->type == ICMP4_echo_request ||
80 icmp->type == ICMP4_echo_reply) {
81 return *((u16 *)(icmp + 1));
90 ip6_get_port (ip6_header_t *ip6, map_dir_e dir, u16 buffer_len)
97 if (ip6_parse(ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
100 //TODO: Use buffer length
103 ip6_frag_hdr_offset(((ip6_frag_hdr_t *)u8_ptr_add(ip6, frag_offset))))
104 return -1; //Can't deal with non-first fragment for now
106 l4 = u8_ptr_add(ip6, l4_offset);
107 if (l4_protocol == IP_PROTOCOL_TCP ||
108 l4_protocol == IP_PROTOCOL_UDP) {
109 return (dir == MAP_SENDER) ? ((udp_header_t *)(l4))->src_port : ((udp_header_t *)(l4))->dst_port;
110 } else if (l4_protocol == IP_PROTOCOL_ICMP6) {
111 icmp46_header_t *icmp = (icmp46_header_t *)(l4);
112 if (icmp->type == ICMP6_echo_request) {
113 return (dir == MAP_SENDER) ? ((u16*)(icmp))[2] : -1;
114 } else if (icmp->type == ICMP6_echo_reply) {
115 return (dir == MAP_SENDER) ? -1 : ((u16*)(icmp))[2];
123 map_create_domain (ip4_address_t *ip4_prefix,
125 ip6_address_t *ip6_prefix,
127 ip6_address_t *ip6_src,
132 u32 *map_domain_index,
136 map_main_t *mm = &map_main;
137 ip4_main_t *im4 = &ip4_main;
138 ip6_main_t *im6 = &ip6_main;
141 ip4_add_del_route_args_t args4;
142 ip6_add_del_route_args_t args6;
146 /* EA bits must be within the first 64 bits */
147 if (ea_bits_len > 0 && (ip6_prefix_len + ea_bits_len) > 64)
150 /* Sanity check on the src prefix length */
151 if (flags & MAP_DOMAIN_TRANSLATION) {
152 if (ip6_src_len != 96) {
153 clib_warning("MAP-T only supports ip6_src_len = 96 for now.");
157 if (ip6_src_len != 128) {
158 clib_warning("MAP-E requires a BR address, not a prefix (ip6_src_len should be 128).");
163 /* Get domain index */
164 pool_get_aligned(mm->domains, d, CLIB_CACHE_LINE_BYTES);
165 memset(d, 0, sizeof (*d));
166 *map_domain_index = d - mm->domains;
168 /* Init domain struct */
169 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
170 d->ip4_prefix_len = ip4_prefix_len;
171 d->ip6_prefix = *ip6_prefix;
172 d->ip6_prefix_len = ip6_prefix_len;
173 d->ip6_src = *ip6_src;
174 d->ip6_src_len = ip6_src_len;
175 d->ea_bits_len = ea_bits_len;
176 d->psid_offset = psid_offset;
177 d->psid_length = psid_length;
181 /* How many, and which bits to grab from the IPv4 DA */
182 if (ip4_prefix_len + ea_bits_len < 32) {
183 d->flags |= MAP_DOMAIN_PREFIX;
184 suffix_len = d->suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
187 suffix_len = 32 - ip4_prefix_len;
189 d->suffix_mask = (1<<suffix_len) - 1;
191 d->psid_shift = 16 - psid_length - psid_offset;
192 d->psid_mask = (1 << d->psid_length) - 1;
193 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
195 /* Init IP adjacency */
196 memset(&adj, 0, sizeof(adj));
197 adj.explicit_fib_index = ~0;
198 adj.lookup_next_index = (d->flags & MAP_DOMAIN_TRANSLATION) ? IP_LOOKUP_NEXT_MAP_T : IP_LOOKUP_NEXT_MAP;
199 p = (uword *)&adj.rewrite_data[0];
200 *p = (uword) (*map_domain_index);
202 if (ip4_get_route(im4, 0, 0, (u8 *)ip4_prefix, ip4_prefix_len)) {
203 clib_warning("IPv4 route already defined: %U/%d", format_ip4_address, ip4_prefix, ip4_prefix_len);
204 pool_put(mm->domains, d);
208 /* Create ip4 adjacency */
209 memset(&args4, 0, sizeof(args4));
210 args4.table_index_or_table_id = 0;
211 args4.flags = IP4_ROUTE_FLAG_ADD;
212 args4.dst_address.as_u32 = ip4_prefix->as_u32;
213 args4.dst_address_length = ip4_prefix_len;
215 args4.adj_index = ~0;
216 args4.add_adj = &adj;
218 ip4_add_del_route(im4, &args4);
220 /* Multiple MAP domains may share same source IPv6 TEP */
221 u32 ai = ip6_get_route(im6, 0, 0, ip6_src, ip6_src_len);
223 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
224 ip_adjacency_t *adj6 = ip_get_adjacency(lm6, ai);
225 if (adj6->lookup_next_index != IP_LOOKUP_NEXT_MAP &&
226 adj6->lookup_next_index != IP_LOOKUP_NEXT_MAP_T) {
227 clib_warning("BR source address already assigned: %U", format_ip6_address, ip6_src);
228 pool_put(mm->domains, d);
232 p = (uword *)&adj6->rewrite_data[0];
235 /* Add refcount, so we don't accidentially delete the route underneath someone */
238 /* Create ip6 adjacency. */
239 memset(&args6, 0, sizeof(args6));
240 args6.table_index_or_table_id = 0;
241 args6.flags = IP6_ROUTE_FLAG_ADD;
242 args6.dst_address.as_u64[0] = ip6_src->as_u64[0];
243 args6.dst_address.as_u64[1] = ip6_src->as_u64[1];
244 args6.dst_address_length = ip6_src_len;
245 args6.adj_index = ~0;
246 args6.add_adj = &adj;
248 ip6_add_del_route(im6, &args6);
251 /* Validate packet/byte counters */
252 map_domain_counter_lock(mm);
254 for (i = 0; i < vec_len(mm->simple_domain_counters); i++) {
255 vlib_validate_simple_counter(&mm->simple_domain_counters[i], *map_domain_index);
256 vlib_zero_simple_counter(&mm->simple_domain_counters[i], *map_domain_index);
258 for (i = 0; i < vec_len(mm->domain_counters); i++) {
259 vlib_validate_combined_counter(&mm->domain_counters[i], *map_domain_index);
260 vlib_zero_combined_counter(&mm->domain_counters[i], *map_domain_index);
262 map_domain_counter_unlock(mm);
271 map_delete_domain (u32 map_domain_index)
273 map_main_t *mm = &map_main;
274 ip4_main_t *im4 = &ip4_main;
275 ip6_main_t *im6 = &ip6_main;
278 ip4_add_del_route_args_t args4;
279 ip6_add_del_route_args_t args6;
281 if (pool_is_free_index(mm->domains, map_domain_index)) {
282 clib_warning("MAP domain delete: domain does not exist: %d", map_domain_index);
286 d = pool_elt_at_index(mm->domains, map_domain_index);
288 memset(&adj, 0, sizeof(adj));
289 adj.explicit_fib_index = ~0;
290 adj.lookup_next_index = (d->flags & MAP_DOMAIN_TRANSLATION) ? IP_LOOKUP_NEXT_MAP_T : IP_LOOKUP_NEXT_MAP;
292 /* Delete ip4 adjacency */
293 memset(&args4, 0, sizeof(args4));
294 args4.table_index_or_table_id = 0;
295 args4.flags = IP4_ROUTE_FLAG_DEL;
296 args4.dst_address.as_u32 = d->ip4_prefix.as_u32;
297 args4.dst_address_length = d->ip4_prefix_len;
299 args4.add_adj = &adj;
301 ip4_add_del_route(im4, &args4);
303 /* Delete ip6 adjacency */
304 u32 ai = ip6_get_route(im6, 0, 0, &d->ip6_src, d->ip6_src_len);
306 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
307 ip_adjacency_t *adj6 = ip_get_adjacency(lm6, ai);
309 uword *p = (uword *)&adj6->rewrite_data[0];
310 /* Delete route when no other domains use this source */
312 memset(&args6, 0, sizeof (args6));
313 args6.table_index_or_table_id = 0;
314 args6.flags = IP6_ROUTE_FLAG_DEL;
315 args6.dst_address.as_u64[0] = d->ip6_src.as_u64[0];
316 args6.dst_address.as_u64[1] = d->ip6_src.as_u64[1];
317 args6.dst_address_length = d->ip6_src_len;
319 args6.add_adj = &adj;
321 ip6_add_del_route(im6, &args6);
327 clib_mem_free(d->rules);
329 pool_put(mm->domains, d);
335 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t *tep,
339 map_main_t *mm = &map_main;
341 if (pool_is_free_index(mm->domains, map_domain_index)) {
342 clib_warning("MAP rule: domain does not exist: %d", map_domain_index);
345 d = pool_elt_at_index(mm->domains, map_domain_index);
347 /* Rules are only used in 1:1 independent case */
348 if (d->ea_bits_len > 0)
352 u32 l = (0x1 << d->psid_length) * sizeof(ip6_address_t);
353 d->rules = clib_mem_alloc_aligned(l, CLIB_CACHE_LINE_BYTES);
354 if (!d->rules) return -1;
355 memset(d->rules, 0, l);
358 if (psid >= (0x1 << d->psid_length)) {
359 clib_warning("MAP rule: PSID outside bounds: %d [%d]", psid, 0x1 << d->psid_length);
364 d->rules[psid] = *tep;
366 memset(&d->rules[psid], 0, sizeof(ip6_address_t));
371 #ifdef MAP_SKIP_IP6_LOOKUP
373 map_pre_resolve (ip4_address_t *ip4, ip6_address_t *ip6)
375 map_main_t *mm = &map_main;
376 ip4_main_t *im4 = &ip4_main;
377 ip6_main_t *im6 = &ip6_main;
379 if (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0) {
380 mm->adj6_index = ip6_fib_lookup_with_table(im6, 0, ip6);
381 clib_warning("FIB lookup results in: %u", mm->adj6_index);
383 if (ip4->as_u32 != 0) {
384 mm->adj4_index = ip4_fib_lookup_with_table(im4, 0, ip4, 0);
385 clib_warning("FIB lookup results in: %u", mm->adj4_index);
390 static clib_error_t *
391 map_security_check_command_fn (vlib_main_t *vm,
392 unformat_input_t *input,
393 vlib_cli_command_t *cmd)
395 unformat_input_t _line_input, *line_input = &_line_input;
396 map_main_t *mm = &map_main;
397 /* Get a line of input. */
398 if (!unformat_user(input, unformat_line_input, line_input))
401 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
402 if (unformat(line_input, "off"))
403 mm->sec_check = false;
404 else if (unformat(line_input, "on"))
405 mm->sec_check = true;
407 return clib_error_return(0, "unknown input `%U'",
408 format_unformat_error, input);
410 unformat_free(line_input);
414 static clib_error_t *
415 map_security_check_frag_command_fn (vlib_main_t *vm,
416 unformat_input_t *input,
417 vlib_cli_command_t *cmd)
419 unformat_input_t _line_input, *line_input = &_line_input;
420 map_main_t *mm = &map_main;
421 /* Get a line of input. */
422 if (!unformat_user(input, unformat_line_input, line_input))
425 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
426 if (unformat(line_input, "off"))
427 mm->sec_check_frag = false;
428 else if (unformat(line_input, "on"))
429 mm->sec_check_frag = true;
431 return clib_error_return(0, "unknown input `%U'",
432 format_unformat_error, input);
434 unformat_free(line_input);
438 static clib_error_t *
439 map_add_domain_command_fn (vlib_main_t *vm,
440 unformat_input_t *input,
441 vlib_cli_command_t *cmd)
443 unformat_input_t _line_input, *line_input = &_line_input;
444 ip4_address_t ip4_prefix;
445 ip6_address_t ip6_prefix;
446 ip6_address_t ip6_src;
447 u32 ip6_prefix_len, ip4_prefix_len, map_domain_index, ip6_src_len;
449 /* Optional arguments */
450 u32 ea_bits_len, psid_offset = 0, psid_length = 0;
455 /* Get a line of input. */
456 if (!unformat_user(input, unformat_line_input, line_input))
459 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
460 if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
462 else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix, &ip6_prefix_len))
464 else if (unformat(line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src, &ip6_src_len))
466 else if (unformat(line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
468 else if (unformat(line_input, "ea-bits-len %d", &ea_bits_len))
470 else if (unformat(line_input, "psid-offset %d", &psid_offset))
472 else if (unformat(line_input, "psid-len %d", &psid_length))
474 else if (unformat(line_input, "mtu %d", &mtu))
476 else if (unformat(line_input, "map-t"))
477 flags |= MAP_DOMAIN_TRANSLATION;
479 return clib_error_return(0, "unknown input `%U'",
480 format_unformat_error, input);
482 unformat_free(line_input);
485 return clib_error_return(0, "mandatory argument(s) missing");
487 map_create_domain(&ip4_prefix, ip4_prefix_len,
488 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
489 ea_bits_len, psid_offset, psid_length, &map_domain_index,
495 static clib_error_t *
496 map_del_domain_command_fn (vlib_main_t *vm,
497 unformat_input_t *input,
498 vlib_cli_command_t *cmd)
500 unformat_input_t _line_input, *line_input = &_line_input;
502 u32 map_domain_index;
504 /* Get a line of input. */
505 if (! unformat_user(input, unformat_line_input, line_input))
508 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
509 if (unformat(line_input, "index %d", &map_domain_index))
512 return clib_error_return(0, "unknown input `%U'",
513 format_unformat_error, input);
515 unformat_free(line_input);
518 return clib_error_return(0, "mandatory argument(s) missing");
520 map_delete_domain(map_domain_index);
525 static clib_error_t *
526 map_add_rule_command_fn (vlib_main_t *vm,
527 unformat_input_t *input,
528 vlib_cli_command_t *cmd)
530 unformat_input_t _line_input, *line_input = &_line_input;
533 u32 psid, map_domain_index;
535 /* Get a line of input. */
536 if (! unformat_user(input, unformat_line_input, line_input))
539 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
540 if (unformat(line_input, "index %d", &map_domain_index))
542 else if (unformat(line_input, "psid %d", &psid))
544 else if (unformat(line_input, "ip6-dst %U", unformat_ip6_address, &tep))
547 return clib_error_return(0, "unknown input `%U'",
548 format_unformat_error, input);
550 unformat_free(line_input);
553 return clib_error_return(0, "mandatory argument(s) missing");
555 if (map_add_del_psid(map_domain_index, psid, &tep, 1) != 0) {
556 return clib_error_return(0, "Failing to add Mapping Rule");
561 #if MAP_SKIP_IP6_LOOKUP
562 static clib_error_t *
563 map_pre_resolve_command_fn (vlib_main_t *vm,
564 unformat_input_t *input,
565 vlib_cli_command_t *cmd)
567 unformat_input_t _line_input, *line_input = &_line_input;
570 map_main_t *mm = &map_main;
572 memset(&ip4nh, 0, sizeof(ip4nh));
573 memset(&ip6nh, 0, sizeof(ip6nh));
575 /* Get a line of input. */
576 if (!unformat_user(input, unformat_line_input, line_input))
579 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
580 if (unformat(line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
581 mm->preresolve_ip4 = ip4nh;
582 else if (unformat(line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
583 mm->preresolve_ip6 = ip6nh;
585 return clib_error_return(0, "unknown input `%U'",
586 format_unformat_error, input);
588 unformat_free(line_input);
590 map_pre_resolve(&ip4nh, &ip6nh);
596 static clib_error_t *
597 map_icmp_relay_source_address_command_fn (vlib_main_t *vm,
598 unformat_input_t *input,
599 vlib_cli_command_t *cmd)
601 unformat_input_t _line_input, *line_input = &_line_input;
602 ip4_address_t icmp_src_address;
603 map_main_t *mm = &map_main;
605 mm->icmp4_src_address.as_u32 = 0;
607 /* Get a line of input. */
608 if (!unformat_user(input, unformat_line_input, line_input))
611 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
612 if (unformat(line_input, "%U", unformat_ip4_address, &icmp_src_address))
613 mm->icmp4_src_address = icmp_src_address;
615 return clib_error_return(0, "unknown input `%U'",
616 format_unformat_error, input);
618 unformat_free(line_input);
623 static clib_error_t *
624 map_icmp_unreachables_command_fn (vlib_main_t *vm,
625 unformat_input_t *input,
626 vlib_cli_command_t *cmd)
628 unformat_input_t _line_input, *line_input = &_line_input;
629 map_main_t *mm = &map_main;
632 /* Get a line of input. */
633 if (!unformat_user(input, unformat_line_input, line_input))
636 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
638 if (unformat(line_input, "on"))
639 mm->icmp6_enabled = true;
640 else if (unformat(line_input, "off"))
641 mm->icmp6_enabled = false;
643 return clib_error_return(0, "unknown input `%U'",
644 format_unformat_error, input);
646 unformat_free(line_input);
650 return clib_error_return(0, "mandatory argument(s) missing");
655 static clib_error_t *
656 map_fragment_command_fn (vlib_main_t *vm,
657 unformat_input_t *input,
658 vlib_cli_command_t *cmd)
660 unformat_input_t _line_input, *line_input = &_line_input;
661 map_main_t *mm = &map_main;
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) {
668 if (unformat(line_input, "inner"))
669 mm->frag_inner = true;
670 else if (unformat(line_input, "outer"))
671 mm->frag_inner = false;
673 return clib_error_return(0, "unknown input `%U'",
674 format_unformat_error, input);
676 unformat_free(line_input);
681 static clib_error_t *
682 map_fragment_df_command_fn (vlib_main_t *vm,
683 unformat_input_t *input,
684 vlib_cli_command_t *cmd)
686 unformat_input_t _line_input, *line_input = &_line_input;
687 map_main_t *mm = &map_main;
689 /* Get a line of input. */
690 if (!unformat_user(input, unformat_line_input, line_input))
693 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
694 if (unformat(line_input, "on"))
695 mm->frag_ignore_df = true;
696 else if (unformat(line_input, "off"))
697 mm->frag_ignore_df = false;
699 return clib_error_return(0, "unknown input `%U'",
700 format_unformat_error, input);
702 unformat_free(line_input);
707 static clib_error_t *
708 map_traffic_class_command_fn (vlib_main_t *vm,
709 unformat_input_t *input,
710 vlib_cli_command_t *cmd)
712 unformat_input_t _line_input, *line_input = &_line_input;
713 map_main_t *mm = &map_main;
718 /* Get a line of input. */
719 if (!unformat_user(input, unformat_line_input, line_input))
722 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
723 if (unformat(line_input, "copy"))
725 else if (unformat(line_input, "%x", &tc))
728 return clib_error_return(0, "unknown input `%U'",
729 format_unformat_error, input);
731 unformat_free(line_input);
737 format_map_domain (u8 *s, va_list *args)
739 map_domain_t *d = va_arg(*args, map_domain_t *);
740 bool counters = va_arg(*args, int);
741 map_main_t *mm = &map_main;
742 ip6_address_t ip6_prefix;
745 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
747 ip6_prefix = d->ip6_prefix;
750 "[%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",
752 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
753 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
754 format_ip6_address, &d->ip6_src, d->ip6_src_len,
755 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
756 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
759 map_domain_counter_lock(mm);
761 vlib_get_combined_counter(&mm->domain_counters[MAP_DOMAIN_COUNTER_TX], d - mm->domains, &v);
762 s = format(s, " TX: %lld/%lld", v.packets, v.bytes);
763 vlib_get_combined_counter(&mm->domain_counters[MAP_DOMAIN_COUNTER_RX], d - mm->domains, &v);
764 s = format(s, " RX: %lld/%lld", v.packets, v.bytes);
765 map_domain_counter_unlock(mm);
772 for (i = 0; i < (0x1 << d->psid_length); i++) {
774 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0 )
777 " rule psid: %d ip6-dst %U\n", i, format_ip6_address, &dst);
784 format_map_ip4_reass (u8 *s, va_list *args)
786 map_main_t *mm = &map_main;
787 map_ip4_reass_t *r = va_arg(*args, map_ip4_reass_t *);
788 map_ip4_reass_key_t *k = &r->key;
789 f64 now = vlib_time_now(mm->vlib_main);
790 f64 lifetime = (((f64)mm->ip4_reass_conf_lifetime_ms) / 1000);
791 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
793 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
794 format_ip4_address, &k->src.as_u8, format_ip4_address, &k->dst.as_u8,
795 k->protocol, clib_net_to_host_u16(k->fragment_id), (r->port >= 0)?clib_net_to_host_u16(r->port):-1, dt);
800 format_map_ip6_reass (u8 *s, va_list *args)
802 map_main_t *mm = &map_main;
803 map_ip6_reass_t *r = va_arg(*args, map_ip6_reass_t *);
804 map_ip6_reass_key_t *k = &r->key;
805 f64 now = vlib_time_now(mm->vlib_main);
806 f64 lifetime = (((f64)mm->ip6_reass_conf_lifetime_ms) / 1000);
807 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
809 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
810 format_ip6_address, &k->src.as_u8, format_ip6_address, &k->dst.as_u8,
811 k->protocol, clib_net_to_host_u32(k->fragment_id), dt);
815 static clib_error_t *
816 show_map_domain_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
818 unformat_input_t _line_input, *line_input = &_line_input;
819 map_main_t *mm = &map_main;
821 bool counters = false;
822 u32 map_domain_index = ~0;
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) {
829 if (unformat(line_input, "counters"))
831 else if (unformat(line_input, "index %d", &map_domain_index))
834 return clib_error_return(0, "unknown input `%U'",
835 format_unformat_error, input);
837 unformat_free(line_input);
839 if (pool_elts(mm->domains) == 0)
840 vlib_cli_output(vm, "No MAP domains are configured...");
842 if (map_domain_index == ~0) {
843 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
845 if (pool_is_free_index(mm->domains, map_domain_index)) {
846 return clib_error_return(0, "MAP domain does not exists %d", map_domain_index);
849 d = pool_elt_at_index(mm->domains, map_domain_index);
850 vlib_cli_output(vm, "%U", format_map_domain, d, counters);
856 static clib_error_t *
857 show_map_fragments_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
859 map_main_t *mm = &map_main;
863 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
864 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
869 map_error_counter_get (u32 node_index, map_error_t map_error)
871 vlib_main_t *vm = vlib_get_main();
872 vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, node_index);
873 vlib_error_main_t *em = &vm->error_main;
874 vlib_error_t e = error_node->errors[map_error];
875 vlib_node_t *n = vlib_get_node(vm, node_index);
878 ci = vlib_error_get_code(e);
879 ASSERT (ci < n->n_errors);
880 ci += n->error_heap_index;
882 return (em->counters[ci]);
885 static clib_error_t *
886 show_map_stats_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
888 map_main_t *mm = &map_main;
890 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
891 if (pool_elts (mm->domains) == 0)
892 vlib_cli_output(vm, "No MAP domains are configured...");
894 pool_foreach(d, mm->domains, ({
896 rulecount+= 0x1 << d->psid_length;
897 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
899 domains += sizeof(*d);
903 vlib_cli_output(vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
904 vlib_cli_output(vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
905 vlib_cli_output(vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
906 vlib_cli_output(vm, "Total: %d bytes)\n", rules + domains);
908 #if MAP_SKIP_IP6_LOOKUP
909 vlib_cli_output(vm, "MAP pre-resolve: IP6 next-hop: %U (%u), IP4 next-hop: %U (%u)\n",
910 format_ip6_address, &mm->preresolve_ip6, mm->adj6_index,
911 format_ip4_address, &mm->preresolve_ip4, mm->adj4_index);
915 vlib_cli_output(vm, "MAP traffic-class: copy");
917 vlib_cli_output(vm, "MAP traffic-class: %x", mm->tc);
919 vlib_cli_output(vm, "MAP IPv6 inbound security check: %s, fragmented packet security check: %s", mm->sec_check ? "enabled" : "disabled",
920 mm->sec_check_frag ? "enabled" : "disabled");
922 vlib_cli_output(vm, "ICMP-relay IPv4 source address: %U\n", format_ip4_address, &mm->icmp4_src_address);
923 vlib_cli_output(vm, "ICMP6 unreachables sent for unmatched packets: %s\n", mm->icmp6_enabled ? "enabled" : "disabled");
924 vlib_cli_output(vm, "Inner fragmentation: %s\n", mm->frag_inner ? "enabled" : "disabled");
925 vlib_cli_output(vm, "Fragment packets regardless of DF flag: %s\n", mm->frag_ignore_df ? "enabled" : "disabled");
930 vlib_combined_counter_main_t *cm = mm->domain_counters;
931 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
932 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
936 memset (total_pkts, 0, sizeof (total_pkts));
937 memset (total_bytes, 0, sizeof (total_bytes));
939 map_domain_counter_lock (mm);
940 vec_foreach (cm, mm->domain_counters) {
941 which = cm - mm->domain_counters;
943 for (i = 0; i < vec_len (cm->maxi); i++) {
944 vlib_get_combined_counter (cm, i, &v);
945 total_pkts[which] += v.packets;
946 total_bytes[which] += v.bytes;
949 map_domain_counter_unlock (mm);
951 vlib_cli_output(vm, "Encapsulated packets: %lld bytes: %lld\n", total_pkts[MAP_DOMAIN_COUNTER_TX],
952 total_bytes[MAP_DOMAIN_COUNTER_TX]);
953 vlib_cli_output(vm, "Decapsulated packets: %lld bytes: %lld\n", total_pkts[MAP_DOMAIN_COUNTER_RX],
954 total_bytes[MAP_DOMAIN_COUNTER_RX]);
956 vlib_cli_output(vm, "ICMP relayed packets: %d\n", vlib_get_simple_counter(&mm->icmp_relayed, 0));
961 static clib_error_t *
962 map_params_reass_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
964 unformat_input_t _line_input, *line_input = &_line_input;
966 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1);
968 u64 buffers = ~(0ull);
971 if (!unformat_user(input, unformat_line_input, line_input))
974 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
975 if (!unformat(line_input, "lifetime %u", &lifetime) &&
976 !unformat(line_input, "ht-ratio %lf", &ht_ratio) &&
977 !unformat(line_input, "pool-size %u", &pool_size) &&
978 !unformat(line_input, "buffers %llu", &buffers) &&
979 !((unformat(line_input, "ip4")) && (ip4 = 1)) &&
980 !((unformat(line_input, "ip6")) && (ip6 = 1))) {
981 unformat_free(line_input);
982 return clib_error_return(0, "invalid input");
985 unformat_free(line_input);
988 return clib_error_return(0, "must specify ip4 and/or ip6");
991 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
992 return clib_error_return(0, "invalid ip4-reass pool-size ( > %d)", MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
993 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1) && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
994 return clib_error_return(0, "invalid ip4-reass ht-ratio ( > %d)", MAP_IP4_REASS_CONF_HT_RATIO_MAX);
995 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
996 return clib_error_return(0, "invalid ip4-reass lifetime ( > %d)", MAP_IP4_REASS_CONF_LIFETIME_MAX);
997 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
998 return clib_error_return(0, "invalid ip4-reass buffers ( > %ld)", MAP_IP4_REASS_CONF_BUFFERS_MAX);
1002 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1003 return clib_error_return(0, "invalid ip6-reass pool-size ( > %d)", MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1004 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1) && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1005 return clib_error_return(0, "invalid ip6-reass ht-log2len ( > %d)", MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1006 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1007 return clib_error_return(0, "invalid ip6-reass lifetime ( > %d)", MAP_IP6_REASS_CONF_LIFETIME_MAX);
1008 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1009 return clib_error_return(0, "invalid ip6-reass buffers ( > %ld)", MAP_IP6_REASS_CONF_BUFFERS_MAX);
1013 u32 reass = 0, packets = 0;
1014 if (pool_size != ~0) {
1015 if (map_ip4_reass_conf_pool_size(pool_size, &reass, &packets)) {
1016 vlib_cli_output(vm, "Could not set ip4-reass pool-size");
1018 vlib_cli_output(vm, "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1021 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1)) {
1022 if (map_ip4_reass_conf_ht_ratio(ht_ratio, &reass, &packets)) {
1023 vlib_cli_output(vm, "Could not set ip4-reass ht-log2len");
1025 vlib_cli_output(vm, "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1028 if (lifetime != ~0) {
1029 if (map_ip4_reass_conf_lifetime(lifetime))
1030 vlib_cli_output(vm, "Could not set ip4-reass lifetime");
1032 vlib_cli_output(vm, "Setting ip4-reass lifetime");
1034 if (buffers != ~(0ull)) {
1035 if (map_ip4_reass_conf_buffers(buffers))
1036 vlib_cli_output(vm, "Could not set ip4-reass buffers");
1038 vlib_cli_output(vm, "Setting ip4-reass buffers");
1041 if (map_main.ip4_reass_conf_buffers >
1042 map_main.ip4_reass_conf_pool_size * MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY) {
1043 vlib_cli_output(vm, "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1048 u32 reass = 0, packets = 0;
1049 if (pool_size != ~0) {
1050 if (map_ip6_reass_conf_pool_size(pool_size, &reass, &packets)) {
1051 vlib_cli_output(vm, "Could not set ip6-reass pool-size");
1053 vlib_cli_output(vm, "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1056 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1)) {
1057 if (map_ip6_reass_conf_ht_ratio(ht_ratio, &reass, &packets)) {
1058 vlib_cli_output(vm, "Could not set ip6-reass ht-log2len");
1060 vlib_cli_output(vm, "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1063 if (lifetime != ~0) {
1064 if (map_ip6_reass_conf_lifetime(lifetime))
1065 vlib_cli_output(vm, "Could not set ip6-reass lifetime");
1067 vlib_cli_output(vm, "Setting ip6-reass lifetime");
1069 if (buffers != ~(0ull)) {
1070 if (map_ip6_reass_conf_buffers(buffers))
1071 vlib_cli_output(vm, "Could not set ip6-reass buffers");
1073 vlib_cli_output(vm, "Setting ip6-reass buffers");
1076 if (map_main.ip6_reass_conf_buffers >
1077 map_main.ip6_reass_conf_pool_size * MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY) {
1078 vlib_cli_output(vm, "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1087 * packet trace format function
1090 format_map_trace (u8 *s, va_list *args)
1092 CLIB_UNUSED(vlib_main_t *vm) = va_arg (*args, vlib_main_t *);
1093 CLIB_UNUSED(vlib_node_t *node) = va_arg (*args, vlib_node_t *);
1094 map_trace_t *t = va_arg (*args, map_trace_t *);
1095 u32 map_domain_index = t->map_domain_index;
1098 s = format(s, "MAP domain index: %d L4 port: %u", map_domain_index, clib_net_to_host_u16(port));
1103 static_always_inline map_ip4_reass_t *
1104 map_ip4_reass_lookup(map_ip4_reass_key_t *k, u32 bucket, f64 now)
1106 map_main_t *mm = &map_main;
1107 u32 ri = mm->ip4_reass_hash_table[bucket];
1108 while(ri != MAP_REASS_INDEX_NONE) {
1109 map_ip4_reass_t * r = pool_elt_at_index(mm->ip4_reass_pool, ri);
1110 if (r->key.as_u64[0] == k->as_u64[0] &&
1111 r->key.as_u64[1] == k->as_u64[1] &&
1112 now < r->ts + (((f64)mm->ip4_reass_conf_lifetime_ms) / 1000)) {
1115 ri = r->bucket_next;
1120 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1123 map_ip4_reass_free(map_ip4_reass_t *r, u32 **pi_to_drop)
1125 map_main_t *mm = &map_main;
1126 map_ip4_reass_get_fragments(r, pi_to_drop);
1128 // Unlink in hash bucket
1129 map_ip4_reass_t *r2 = NULL;
1130 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1131 while (r2i != map_ip4_reass_pool_index(r)) {
1132 ASSERT(r2i != MAP_REASS_INDEX_NONE);
1133 r2 = pool_elt_at_index(mm->ip4_reass_pool, r2i);
1134 r2i = r2->bucket_next;
1137 r2->bucket_next = r->bucket_next;
1139 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1143 if (r->fifo_next == map_ip4_reass_pool_index(r)) {
1144 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1146 if(mm->ip4_reass_fifo_last == map_ip4_reass_pool_index(r))
1147 mm->ip4_reass_fifo_last = r->fifo_prev;
1148 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_prev)->fifo_next = r->fifo_next;
1149 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_next)->fifo_prev = r->fifo_prev;
1152 pool_put(mm->ip4_reass_pool, r);
1153 mm->ip4_reass_allocated--;
1157 map_ip4_reass_get(u32 src, u32 dst, u16 fragment_id,
1158 u8 protocol, u32 **pi_to_drop)
1160 map_ip4_reass_t * r;
1161 map_main_t *mm = &map_main;
1162 map_ip4_reass_key_t k = {.src.data_u32 = src,
1163 .dst.data_u32 = dst,
1164 .fragment_id = fragment_id,
1165 .protocol = protocol };
1168 h = crc_u32(k.as_u32[0], h);
1169 h = crc_u32(k.as_u32[1], h);
1170 h = crc_u32(k.as_u32[2], h);
1171 h = crc_u32(k.as_u32[3], h);
1172 h = h >> (32 - mm->ip4_reass_ht_log2len);
1174 f64 now = vlib_time_now(mm->vlib_main);
1176 //Cache garbage collection
1177 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1178 map_ip4_reass_t *last = pool_elt_at_index(mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1179 if (last->ts + (((f64)mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1180 map_ip4_reass_free(last, pi_to_drop);
1185 if ((r = map_ip4_reass_lookup(&k, h, now)))
1188 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1191 pool_get(mm->ip4_reass_pool, r);
1192 mm->ip4_reass_allocated++;
1194 for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1195 r->fragments[i] = ~0;
1197 u32 ri = map_ip4_reass_pool_index(r);
1199 //Link in new bucket
1201 r->bucket_next = mm->ip4_reass_hash_table[h];
1202 mm->ip4_reass_hash_table[h] = ri;
1205 if(mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1206 r->fifo_next = pool_elt_at_index(mm->ip4_reass_pool, mm->ip4_reass_fifo_last)->fifo_next;
1207 r->fifo_prev = mm->ip4_reass_fifo_last;
1208 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1209 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1211 r->fifo_next = r->fifo_prev = ri;
1212 mm->ip4_reass_fifo_last = ri;
1219 #ifdef MAP_IP4_REASS_COUNT_BYTES
1220 r->expected_total = 0xffff;
1228 map_ip4_reass_add_fragment(map_ip4_reass_t *r, u32 pi)
1230 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1234 for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1235 if(r->fragments[i] == ~0) {
1236 r->fragments[i] = pi;
1237 map_main.ip4_reass_buffered_counter++;
1243 static_always_inline map_ip6_reass_t *
1244 map_ip6_reass_lookup(map_ip6_reass_key_t *k, u32 bucket, f64 now)
1246 map_main_t *mm = &map_main;
1247 u32 ri = mm->ip6_reass_hash_table[bucket];
1248 while(ri != MAP_REASS_INDEX_NONE) {
1249 map_ip6_reass_t * r = pool_elt_at_index(mm->ip6_reass_pool, ri);
1250 if(now < r->ts + (((f64)mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1251 r->key.as_u64[0] == k->as_u64[0] &&
1252 r->key.as_u64[1] == k->as_u64[1] &&
1253 r->key.as_u64[2] == k->as_u64[2] &&
1254 r->key.as_u64[3] == k->as_u64[3] &&
1255 r->key.as_u64[4] == k->as_u64[4])
1257 ri = r->bucket_next;
1262 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1265 map_ip6_reass_free(map_ip6_reass_t *r, u32 **pi_to_drop)
1267 map_main_t *mm = &map_main;
1269 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1270 if(r->fragments[i].pi != ~0) {
1271 vec_add1(*pi_to_drop, r->fragments[i].pi);
1272 r->fragments[i].pi = ~0;
1273 map_main.ip6_reass_buffered_counter--;
1276 // Unlink in hash bucket
1277 map_ip6_reass_t *r2 = NULL;
1278 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1279 while (r2i != map_ip6_reass_pool_index(r)) {
1280 ASSERT(r2i != MAP_REASS_INDEX_NONE);
1281 r2 = pool_elt_at_index(mm->ip6_reass_pool, r2i);
1282 r2i = r2->bucket_next;
1285 r2->bucket_next = r->bucket_next;
1287 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1291 if (r->fifo_next == map_ip6_reass_pool_index(r)) {
1292 //Single element in the list, list is now empty
1293 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1295 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index(r)) //First element
1296 mm->ip6_reass_fifo_last = r->fifo_prev;
1297 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_prev)->fifo_next = r->fifo_next;
1298 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_next)->fifo_prev = r->fifo_prev;
1301 // Free from pool if necessary
1302 pool_put(mm->ip6_reass_pool, r);
1303 mm->ip6_reass_allocated--;
1307 map_ip6_reass_get(ip6_address_t *src, ip6_address_t *dst, u32 fragment_id,
1308 u8 protocol, u32 **pi_to_drop)
1310 map_ip6_reass_t * r;
1311 map_main_t *mm = &map_main;
1312 map_ip6_reass_key_t k = {
1315 .fragment_id = fragment_id,
1316 .protocol = protocol };
1320 for (i=0; i<10; i++)
1321 h = crc_u32(k.as_u32[i], h);
1322 h = h >> (32 - mm->ip6_reass_ht_log2len);
1324 f64 now = vlib_time_now(mm->vlib_main);
1326 //Cache garbage collection
1327 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1328 map_ip6_reass_t *last = pool_elt_at_index(mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1329 if (last->ts + (((f64)mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1330 map_ip6_reass_free(last, pi_to_drop);
1335 if ((r = map_ip6_reass_lookup(&k, h, now)))
1338 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1341 pool_get(mm->ip6_reass_pool, r);
1342 mm->ip6_reass_allocated++;
1343 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++) {
1344 r->fragments[i].pi = ~0;
1345 r->fragments[i].next_data_len = 0;
1346 r->fragments[i].next_data_offset = 0;
1349 u32 ri = map_ip6_reass_pool_index(r);
1351 //Link in new bucket
1353 r->bucket_next = mm->ip6_reass_hash_table[h];
1354 mm->ip6_reass_hash_table[h] = ri;
1357 if(mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1358 r->fifo_next = pool_elt_at_index(mm->ip6_reass_pool, mm->ip6_reass_fifo_last)->fifo_next;
1359 r->fifo_prev = mm->ip6_reass_fifo_last;
1360 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1361 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1363 r->fifo_next = r->fifo_prev = ri;
1364 mm->ip6_reass_fifo_last = ri;
1370 r->ip4_header.ip_version_and_header_length = 0;
1371 #ifdef MAP_IP6_REASS_COUNT_BYTES
1372 r->expected_total = 0xffff;
1379 map_ip6_reass_add_fragment(map_ip6_reass_t *r, u32 pi,
1380 u16 data_offset, u16 next_data_offset,
1381 u8 *data_start, u16 data_len)
1383 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1384 u16 copied_len = (data_len > 20) ? 20 : data_len;
1386 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1389 //Lookup for fragments for the current buffer
1390 //and the one before that
1392 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++) {
1393 if (data_offset && r->fragments[i].next_data_offset == data_offset) {
1394 prev_f = &r->fragments[i]; // This is buffer for previous packet
1395 } else if (r->fragments[i].next_data_offset == next_data_offset) {
1396 f = &r->fragments[i]; // This is a buffer for the current packet
1397 } else if (r->fragments[i].next_data_offset == 0) { //Available
1399 f = &r->fragments[i];
1400 else if (prev_f == NULL)
1401 prev_f = &r->fragments[i];
1405 if (!f || f->pi != ~0)
1412 memcpy(prev_f->next_data, data_start, copied_len);
1413 prev_f->next_data_len = copied_len;
1414 prev_f->next_data_offset = data_offset;
1416 if (((ip4_header_t *)data_start)->ip_version_and_header_length != 0x45)
1419 if (r->ip4_header.ip_version_and_header_length == 0)
1420 memcpy(&r->ip4_header, data_start, sizeof(ip4_header_t));
1424 f->next_data_offset = next_data_offset;
1426 map_main.ip6_reass_buffered_counter++;
1431 void map_ip4_reass_reinit(u32 *trashed_reass, u32 *dropped_packets)
1433 map_main_t *mm = &map_main;
1437 *dropped_packets = mm->ip4_reass_buffered_counter;
1439 *trashed_reass = mm->ip4_reass_allocated;
1440 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1441 u16 ri = mm->ip4_reass_fifo_last;
1443 map_ip4_reass_t *r = pool_elt_at_index(mm->ip4_reass_pool, ri);
1444 for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1445 if (r->fragments[i] != ~0)
1446 map_ip4_drop_pi(r->fragments[i]);
1449 pool_put(mm->ip4_reass_pool, r);
1450 } while (ri != mm->ip4_reass_fifo_last);
1453 vec_free(mm->ip4_reass_hash_table);
1454 vec_resize(mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1455 for (i=0; i<(1 << mm->ip4_reass_ht_log2len); i++)
1456 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1457 pool_free(mm->ip4_reass_pool);
1458 pool_alloc(mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1460 mm->ip4_reass_allocated = 0;
1461 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1462 mm->ip4_reass_buffered_counter = 0;
1465 u8 map_get_ht_log2len(f32 ht_ratio, u16 pool_size)
1467 u32 desired_size = (u32)(pool_size * ht_ratio);
1469 for (i=1; i<31; i++)
1470 if ((1 << i) >= desired_size)
1475 int map_ip4_reass_conf_ht_ratio(f32 ht_ratio, u32 *trashed_reass, u32 *dropped_packets)
1477 map_main_t *mm = &map_main;
1478 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1481 map_ip4_reass_lock();
1482 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1483 mm->ip4_reass_ht_log2len = map_get_ht_log2len(ht_ratio, mm->ip4_reass_conf_pool_size);
1484 map_ip4_reass_reinit(trashed_reass, dropped_packets);
1485 map_ip4_reass_unlock();
1489 int map_ip4_reass_conf_pool_size(u16 pool_size, u32 *trashed_reass, u32 *dropped_packets)
1491 map_main_t *mm = &map_main;
1492 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1495 map_ip4_reass_lock();
1496 mm->ip4_reass_conf_pool_size = pool_size;
1497 map_ip4_reass_reinit(trashed_reass, dropped_packets);
1498 map_ip4_reass_unlock();
1502 int map_ip4_reass_conf_lifetime(u16 lifetime_ms)
1504 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1508 int map_ip4_reass_conf_buffers(u32 buffers)
1510 map_main.ip4_reass_conf_buffers = buffers;
1514 void map_ip6_reass_reinit(u32 *trashed_reass, u32 *dropped_packets)
1516 map_main_t *mm = &map_main;
1518 *dropped_packets = mm->ip6_reass_buffered_counter;
1520 *trashed_reass = mm->ip6_reass_allocated;
1522 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1523 u16 ri = mm->ip6_reass_fifo_last;
1525 map_ip6_reass_t *r = pool_elt_at_index(mm->ip6_reass_pool, ri);
1526 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1527 if (r->fragments[i].pi != ~0)
1528 map_ip6_drop_pi(r->fragments[i].pi);
1531 pool_put(mm->ip6_reass_pool, r);
1532 } while (ri != mm->ip6_reass_fifo_last);
1533 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1536 vec_free(mm->ip6_reass_hash_table);
1537 vec_resize(mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1538 for(i=0; i<(1 << mm->ip6_reass_ht_log2len); i++)
1539 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1540 pool_free(mm->ip6_reass_pool);
1541 pool_alloc(mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1543 mm->ip6_reass_allocated = 0;
1544 mm->ip6_reass_buffered_counter = 0;
1547 int map_ip6_reass_conf_ht_ratio(f32 ht_ratio, u32 *trashed_reass, u32 *dropped_packets)
1549 map_main_t *mm = &map_main;
1550 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1553 map_ip6_reass_lock();
1554 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1555 mm->ip6_reass_ht_log2len = map_get_ht_log2len(ht_ratio, mm->ip6_reass_conf_pool_size);
1556 map_ip6_reass_reinit(trashed_reass, dropped_packets);
1557 map_ip6_reass_unlock();
1561 int map_ip6_reass_conf_pool_size(u16 pool_size, u32 *trashed_reass, u32 *dropped_packets)
1563 map_main_t *mm = &map_main;
1564 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1567 map_ip6_reass_lock();
1568 mm->ip6_reass_conf_pool_size = pool_size;
1569 map_ip6_reass_reinit(trashed_reass, dropped_packets);
1570 map_ip6_reass_unlock();
1574 int map_ip6_reass_conf_lifetime(u16 lifetime_ms)
1576 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1580 int map_ip6_reass_conf_buffers(u32 buffers)
1582 map_main.ip6_reass_conf_buffers = buffers;
1586 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1587 .path = "map params reassembly",
1588 .short_help = "[ip4 | ip6] [lifetime <lifetime-ms>] [pool-size <pool-size>] [buffers <buffers>] [ht-ratio <ht-ratio>]",
1589 .function = map_params_reass_command_fn,
1592 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1593 .path = "map params traffic-class",
1595 "traffic-class {0x0-0xff | copy}",
1596 .function = map_traffic_class_command_fn,
1599 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1600 .path = "map params pre-resolve",
1602 "pre-resolve {ip4-nh <address>} | {ip6-nh <address>}",
1603 .function = map_pre_resolve_command_fn,
1606 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1607 .path = "map params security-check",
1609 "security-check on|off",
1610 .function = map_security_check_command_fn,
1613 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
1614 .path = "map params icmp source-address",
1615 .short_help = "source-address <ip4-address>",
1616 .function = map_icmp_relay_source_address_command_fn,
1619 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
1620 .path = "map params icmp6 unreachables",
1621 .short_help = "unreachables {on|off}",
1622 .function = map_icmp_unreachables_command_fn,
1625 VLIB_CLI_COMMAND(map_fragment_command, static) = {
1626 .path = "map params fragment",
1627 .short_help = "[inner|outer] [ignore-df [on|off]]",
1628 .function = map_fragment_command_fn,
1631 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
1632 .path = "map params fragment ignore-df",
1633 .short_help = "on|off",
1634 .function = map_fragment_df_command_fn,
1637 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
1638 .path = "map params security-check fragments",
1641 .function = map_security_check_frag_command_fn,
1644 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
1645 .path = "map add domain",
1647 "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> ip6-src <ip6-pfx> "
1648 "ea-bits-len <n> psid-offset <n> psid-len <n> [map-t] [mtu <mtu>]",
1649 .function = map_add_domain_command_fn,
1652 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
1653 .path = "map add rule",
1655 "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
1656 .function = map_add_rule_command_fn,
1659 VLIB_CLI_COMMAND(map_del_command, static) = {
1660 .path = "map del domain",
1662 "map del domain index <domain>",
1663 .function = map_del_domain_command_fn,
1666 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
1667 .path = "show map domain",
1668 .function = show_map_domain_command_fn,
1671 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
1672 .path = "show map stats",
1673 .function = show_map_stats_command_fn,
1676 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
1677 .path = "show map fragments",
1678 .function = show_map_fragments_command_fn,
1684 clib_error_t *map_init (vlib_main_t *vm)
1686 map_main_t *mm = &map_main;
1687 mm->vnet_main = vnet_get_main();
1690 #ifdef MAP_SKIP_IP6_LOOKUP
1691 memset(&mm->preresolve_ip4, 0, sizeof(mm->preresolve_ip4));
1692 memset(&mm->preresolve_ip6, 0, sizeof(mm->preresolve_ip6));
1701 /* Inbound security check */
1702 mm->sec_check = true;
1703 mm->sec_check_frag = false;
1705 /* ICMP6 Type 1, Code 5 for security check failure */
1706 mm->icmp6_enabled = false;
1708 /* Inner or outer fragmentation */
1709 mm->frag_inner = false;
1710 mm->frag_ignore_df = false;
1712 vec_validate(mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
1713 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
1714 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
1716 vlib_validate_simple_counter(&mm->icmp_relayed, 0);
1717 vlib_zero_simple_counter(&mm->icmp_relayed, 0);
1719 /* IP4 virtual reassembly */
1720 mm->ip4_reass_hash_table = 0;
1721 mm->ip4_reass_pool = 0;
1722 mm->ip4_reass_lock = clib_mem_alloc_aligned(CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
1723 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
1724 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
1725 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
1726 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
1727 mm->ip4_reass_ht_log2len = map_get_ht_log2len(mm->ip4_reass_conf_ht_ratio, mm->ip4_reass_conf_pool_size);
1728 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1729 map_ip4_reass_reinit(NULL, NULL);
1731 /* IP6 virtual reassembly */
1732 mm->ip6_reass_hash_table = 0;
1733 mm->ip6_reass_pool = 0;
1734 mm->ip6_reass_lock = clib_mem_alloc_aligned(CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
1735 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
1736 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
1737 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
1738 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
1739 mm->ip6_reass_ht_log2len = map_get_ht_log2len(mm->ip6_reass_conf_ht_ratio, mm->ip6_reass_conf_pool_size);
1740 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1741 map_ip6_reass_reinit(NULL, NULL);
1746 VLIB_INIT_FUNCTION(map_init);