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 #include <vppinfra/xxhash.h>
24 crc_u32(u32 data, u32 value)
26 u64 tmp = ((u64)data<<32) | (u64) value;
27 return (u32) clib_xxhash(tmp);
32 * This code supports the following MAP modes:
34 * Algorithmic Shared IPv4 address (ea_bits_len > 0):
35 * ea_bits_len + ip4_prefix > 32
36 * psid_length > 0, ip6_prefix < 64, ip4_prefix <= 32
37 * Algorithmic Full IPv4 address (ea_bits_len > 0):
38 * ea_bits_len + ip4_prefix = 32
39 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
40 * Algorithmic IPv4 prefix (ea_bits_len > 0):
41 * ea_bits_len + ip4_prefix < 32
42 * psid_length = 0, ip6_prefix < 64, ip4_prefix <= 32
44 * Independent Shared IPv4 address (ea_bits_len = 0):
47 * Rule IPv6 address = 128, Rule PSID Set
48 * Independent Full IPv4 address (ea_bits_len = 0):
50 * psid_length = 0, ip6_prefix = 128
51 * Independent IPv4 prefix (ea_bits_len = 0):
53 * psid_length = 0, ip6_prefix = 128
58 * This code supports MAP-T:
60 * With DMR prefix length equal to 96.
66 ip4_get_port (ip4_header_t *ip, map_dir_e dir, u16 buffer_len)
68 //TODO: use buffer length
69 if (ip->ip_version_and_header_length != 0x45 ||
70 ip4_get_fragment_offset(ip))
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->src_port : udp->dst_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));
82 } else if (clib_net_to_host_u16(ip->length) >= 64) {
83 ip = (ip4_header_t *)(icmp + 2);
84 if (PREDICT_TRUE((ip->protocol == IP_PROTOCOL_TCP) ||
85 (ip->protocol == IP_PROTOCOL_UDP))) {
86 udp_header_t *udp = (void *)(ip + 1);
87 return (dir == MAP_SENDER) ? udp->dst_port : udp->src_port;
88 } else if (ip->protocol == IP_PROTOCOL_ICMP) {
89 icmp46_header_t *icmp = (void *)(ip + 1);
90 if (icmp->type == ICMP4_echo_request ||
91 icmp->type == ICMP4_echo_reply) {
92 return *((u16 *)(icmp + 1));
101 ip6_get_port (ip6_header_t *ip6, map_dir_e dir, u16 buffer_len)
108 if (ip6_parse(ip6, buffer_len, &l4_protocol, &l4_offset, &frag_offset))
111 //TODO: Use buffer length
114 ip6_frag_hdr_offset(((ip6_frag_hdr_t *)u8_ptr_add(ip6, frag_offset))))
115 return -1; //Can't deal with non-first fragment for now
117 l4 = u8_ptr_add(ip6, l4_offset);
118 if (l4_protocol == IP_PROTOCOL_TCP ||
119 l4_protocol == IP_PROTOCOL_UDP) {
120 return (dir == MAP_SENDER) ? ((udp_header_t *)(l4))->src_port : ((udp_header_t *)(l4))->dst_port;
121 } else if (l4_protocol == IP_PROTOCOL_ICMP6) {
122 icmp46_header_t *icmp = (icmp46_header_t *)(l4);
123 if (icmp->type == ICMP6_echo_request) {
124 return (dir == MAP_SENDER) ? ((u16*)(icmp))[2] : -1;
125 } else if (icmp->type == ICMP6_echo_reply) {
126 return (dir == MAP_SENDER) ? -1 : ((u16*)(icmp))[2];
134 map_create_domain (ip4_address_t *ip4_prefix,
136 ip6_address_t *ip6_prefix,
138 ip6_address_t *ip6_src,
143 u32 *map_domain_index,
147 map_main_t *mm = &map_main;
148 ip4_main_t *im4 = &ip4_main;
149 ip6_main_t *im6 = &ip6_main;
152 ip4_add_del_route_args_t args4;
153 ip6_add_del_route_args_t args6;
157 /* EA bits must be within the first 64 bits */
158 if (ea_bits_len > 0 && (ip6_prefix_len + ea_bits_len) > 64)
161 /* Sanity check on the src prefix length */
162 if (flags & MAP_DOMAIN_TRANSLATION) {
163 if (ip6_src_len != 96) {
164 clib_warning("MAP-T only supports ip6_src_len = 96 for now.");
168 if (ip6_src_len != 128) {
169 clib_warning("MAP-E requires a BR address, not a prefix (ip6_src_len should be 128).");
174 /* Get domain index */
175 pool_get_aligned(mm->domains, d, CLIB_CACHE_LINE_BYTES);
176 memset(d, 0, sizeof (*d));
177 *map_domain_index = d - mm->domains;
179 /* Init domain struct */
180 d->ip4_prefix.as_u32 = ip4_prefix->as_u32;
181 d->ip4_prefix_len = ip4_prefix_len;
182 d->ip6_prefix = *ip6_prefix;
183 d->ip6_prefix_len = ip6_prefix_len;
184 d->ip6_src = *ip6_src;
185 d->ip6_src_len = ip6_src_len;
186 d->ea_bits_len = ea_bits_len;
187 d->psid_offset = psid_offset;
188 d->psid_length = psid_length;
192 /* How many, and which bits to grab from the IPv4 DA */
193 if (ip4_prefix_len + ea_bits_len < 32) {
194 d->flags |= MAP_DOMAIN_PREFIX;
195 suffix_len = d->suffix_shift = 32 - ip4_prefix_len - ea_bits_len;
198 suffix_len = 32 - ip4_prefix_len;
200 d->suffix_mask = (1<<suffix_len) - 1;
202 d->psid_shift = 16 - psid_length - psid_offset;
203 d->psid_mask = (1 << d->psid_length) - 1;
204 d->ea_shift = 64 - ip6_prefix_len - suffix_len - d->psid_length;
206 /* Init IP adjacency */
207 memset(&adj, 0, sizeof(adj));
208 adj.explicit_fib_index = ~0;
209 adj.lookup_next_index = (d->flags & MAP_DOMAIN_TRANSLATION) ? IP_LOOKUP_NEXT_MAP_T : IP_LOOKUP_NEXT_MAP;
210 p = (uword *)&adj.rewrite_data[0];
211 *p = (uword) (*map_domain_index);
213 if (ip4_get_route(im4, 0, 0, (u8 *)ip4_prefix, ip4_prefix_len)) {
214 clib_warning("IPv4 route already defined: %U/%d", format_ip4_address, ip4_prefix, ip4_prefix_len);
215 pool_put(mm->domains, d);
219 /* Create ip4 adjacency */
220 memset(&args4, 0, sizeof(args4));
221 args4.table_index_or_table_id = 0;
222 args4.flags = IP4_ROUTE_FLAG_ADD;
223 args4.dst_address.as_u32 = ip4_prefix->as_u32;
224 args4.dst_address_length = ip4_prefix_len;
226 args4.adj_index = ~0;
227 args4.add_adj = &adj;
229 ip4_add_del_route(im4, &args4);
231 /* Multiple MAP domains may share same source IPv6 TEP */
232 u32 ai = ip6_get_route(im6, 0, 0, ip6_src, ip6_src_len);
234 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
235 ip_adjacency_t *adj6 = ip_get_adjacency(lm6, ai);
236 if (adj6->lookup_next_index != IP_LOOKUP_NEXT_MAP &&
237 adj6->lookup_next_index != IP_LOOKUP_NEXT_MAP_T) {
238 clib_warning("BR source address already assigned: %U", format_ip6_address, ip6_src);
239 pool_put(mm->domains, d);
243 p = (uword *)&adj6->rewrite_data[0];
246 /* Add refcount, so we don't accidentially delete the route underneath someone */
249 /* Create ip6 adjacency. */
250 memset(&args6, 0, sizeof(args6));
251 args6.table_index_or_table_id = 0;
252 args6.flags = IP6_ROUTE_FLAG_ADD;
253 args6.dst_address.as_u64[0] = ip6_src->as_u64[0];
254 args6.dst_address.as_u64[1] = ip6_src->as_u64[1];
255 args6.dst_address_length = ip6_src_len;
256 args6.adj_index = ~0;
257 args6.add_adj = &adj;
259 ip6_add_del_route(im6, &args6);
262 /* Validate packet/byte counters */
263 map_domain_counter_lock(mm);
265 for (i = 0; i < vec_len(mm->simple_domain_counters); i++) {
266 vlib_validate_simple_counter(&mm->simple_domain_counters[i], *map_domain_index);
267 vlib_zero_simple_counter(&mm->simple_domain_counters[i], *map_domain_index);
269 for (i = 0; i < vec_len(mm->domain_counters); i++) {
270 vlib_validate_combined_counter(&mm->domain_counters[i], *map_domain_index);
271 vlib_zero_combined_counter(&mm->domain_counters[i], *map_domain_index);
273 map_domain_counter_unlock(mm);
282 map_delete_domain (u32 map_domain_index)
284 map_main_t *mm = &map_main;
285 ip4_main_t *im4 = &ip4_main;
286 ip6_main_t *im6 = &ip6_main;
289 ip4_add_del_route_args_t args4;
290 ip6_add_del_route_args_t args6;
292 if (pool_is_free_index(mm->domains, map_domain_index)) {
293 clib_warning("MAP domain delete: domain does not exist: %d", map_domain_index);
297 d = pool_elt_at_index(mm->domains, map_domain_index);
299 memset(&adj, 0, sizeof(adj));
300 adj.explicit_fib_index = ~0;
301 adj.lookup_next_index = (d->flags & MAP_DOMAIN_TRANSLATION) ? IP_LOOKUP_NEXT_MAP_T : IP_LOOKUP_NEXT_MAP;
303 /* Delete ip4 adjacency */
304 memset(&args4, 0, sizeof(args4));
305 args4.table_index_or_table_id = 0;
306 args4.flags = IP4_ROUTE_FLAG_DEL;
307 args4.dst_address.as_u32 = d->ip4_prefix.as_u32;
308 args4.dst_address_length = d->ip4_prefix_len;
310 args4.add_adj = &adj;
312 ip4_add_del_route(im4, &args4);
314 /* Delete ip6 adjacency */
315 u32 ai = ip6_get_route(im6, 0, 0, &d->ip6_src, d->ip6_src_len);
317 ip_lookup_main_t *lm6 = &ip6_main.lookup_main;
318 ip_adjacency_t *adj6 = ip_get_adjacency(lm6, ai);
320 uword *p = (uword *)&adj6->rewrite_data[0];
321 /* Delete route when no other domains use this source */
323 memset(&args6, 0, sizeof (args6));
324 args6.table_index_or_table_id = 0;
325 args6.flags = IP6_ROUTE_FLAG_DEL;
326 args6.dst_address.as_u64[0] = d->ip6_src.as_u64[0];
327 args6.dst_address.as_u64[1] = d->ip6_src.as_u64[1];
328 args6.dst_address_length = d->ip6_src_len;
330 args6.add_adj = &adj;
332 ip6_add_del_route(im6, &args6);
338 clib_mem_free(d->rules);
340 pool_put(mm->domains, d);
346 map_add_del_psid (u32 map_domain_index, u16 psid, ip6_address_t *tep,
350 map_main_t *mm = &map_main;
352 if (pool_is_free_index(mm->domains, map_domain_index)) {
353 clib_warning("MAP rule: domain does not exist: %d", map_domain_index);
356 d = pool_elt_at_index(mm->domains, map_domain_index);
358 /* Rules are only used in 1:1 independent case */
359 if (d->ea_bits_len > 0)
363 u32 l = (0x1 << d->psid_length) * sizeof(ip6_address_t);
364 d->rules = clib_mem_alloc_aligned(l, CLIB_CACHE_LINE_BYTES);
365 if (!d->rules) return -1;
366 memset(d->rules, 0, l);
369 if (psid >= (0x1 << d->psid_length)) {
370 clib_warning("MAP rule: PSID outside bounds: %d [%d]", psid, 0x1 << d->psid_length);
375 d->rules[psid] = *tep;
377 memset(&d->rules[psid], 0, sizeof(ip6_address_t));
382 #ifdef MAP_SKIP_IP6_LOOKUP
384 map_pre_resolve (ip4_address_t *ip4, ip6_address_t *ip6)
386 map_main_t *mm = &map_main;
387 ip4_main_t *im4 = &ip4_main;
388 ip6_main_t *im6 = &ip6_main;
390 if (ip6->as_u64[0] != 0 || ip6->as_u64[1] != 0) {
391 mm->adj6_index = ip6_fib_lookup_with_table(im6, 0, ip6);
392 clib_warning("FIB lookup results in: %u", mm->adj6_index);
394 if (ip4->as_u32 != 0) {
395 mm->adj4_index = ip4_fib_lookup_with_table(im4, 0, ip4, 0);
396 clib_warning("FIB lookup results in: %u", mm->adj4_index);
401 static clib_error_t *
402 map_security_check_command_fn (vlib_main_t *vm,
403 unformat_input_t *input,
404 vlib_cli_command_t *cmd)
406 unformat_input_t _line_input, *line_input = &_line_input;
407 map_main_t *mm = &map_main;
408 /* Get a line of input. */
409 if (!unformat_user(input, unformat_line_input, line_input))
412 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
413 if (unformat(line_input, "off"))
414 mm->sec_check = false;
415 else if (unformat(line_input, "on"))
416 mm->sec_check = true;
418 return clib_error_return(0, "unknown input `%U'",
419 format_unformat_error, input);
421 unformat_free(line_input);
425 static clib_error_t *
426 map_security_check_frag_command_fn (vlib_main_t *vm,
427 unformat_input_t *input,
428 vlib_cli_command_t *cmd)
430 unformat_input_t _line_input, *line_input = &_line_input;
431 map_main_t *mm = &map_main;
432 /* Get a line of input. */
433 if (!unformat_user(input, unformat_line_input, line_input))
436 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
437 if (unformat(line_input, "off"))
438 mm->sec_check_frag = false;
439 else if (unformat(line_input, "on"))
440 mm->sec_check_frag = true;
442 return clib_error_return(0, "unknown input `%U'",
443 format_unformat_error, input);
445 unformat_free(line_input);
449 static clib_error_t *
450 map_add_domain_command_fn (vlib_main_t *vm,
451 unformat_input_t *input,
452 vlib_cli_command_t *cmd)
454 unformat_input_t _line_input, *line_input = &_line_input;
455 ip4_address_t ip4_prefix;
456 ip6_address_t ip6_prefix;
457 ip6_address_t ip6_src;
458 u32 ip6_prefix_len, ip4_prefix_len, map_domain_index, ip6_src_len;
460 /* Optional arguments */
461 u32 ea_bits_len, psid_offset = 0, psid_length = 0;
466 /* Get a line of input. */
467 if (!unformat_user(input, unformat_line_input, line_input))
470 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
471 if (unformat(line_input, "ip4-pfx %U/%d", unformat_ip4_address, &ip4_prefix, &ip4_prefix_len))
473 else if (unformat(line_input, "ip6-pfx %U/%d", unformat_ip6_address, &ip6_prefix, &ip6_prefix_len))
475 else if (unformat(line_input, "ip6-src %U/%d", unformat_ip6_address, &ip6_src, &ip6_src_len))
477 else if (unformat(line_input, "ip6-src %U", unformat_ip6_address, &ip6_src))
479 else if (unformat(line_input, "ea-bits-len %d", &ea_bits_len))
481 else if (unformat(line_input, "psid-offset %d", &psid_offset))
483 else if (unformat(line_input, "psid-len %d", &psid_length))
485 else if (unformat(line_input, "mtu %d", &mtu))
487 else if (unformat(line_input, "map-t"))
488 flags |= MAP_DOMAIN_TRANSLATION;
490 return clib_error_return(0, "unknown input `%U'",
491 format_unformat_error, input);
493 unformat_free(line_input);
496 return clib_error_return(0, "mandatory argument(s) missing");
498 map_create_domain(&ip4_prefix, ip4_prefix_len,
499 &ip6_prefix, ip6_prefix_len, &ip6_src, ip6_src_len,
500 ea_bits_len, psid_offset, psid_length, &map_domain_index,
506 static clib_error_t *
507 map_del_domain_command_fn (vlib_main_t *vm,
508 unformat_input_t *input,
509 vlib_cli_command_t *cmd)
511 unformat_input_t _line_input, *line_input = &_line_input;
513 u32 map_domain_index;
515 /* Get a line of input. */
516 if (! unformat_user(input, unformat_line_input, line_input))
519 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
520 if (unformat(line_input, "index %d", &map_domain_index))
523 return clib_error_return(0, "unknown input `%U'",
524 format_unformat_error, input);
526 unformat_free(line_input);
529 return clib_error_return(0, "mandatory argument(s) missing");
531 map_delete_domain(map_domain_index);
536 static clib_error_t *
537 map_add_rule_command_fn (vlib_main_t *vm,
538 unformat_input_t *input,
539 vlib_cli_command_t *cmd)
541 unformat_input_t _line_input, *line_input = &_line_input;
544 u32 psid, map_domain_index;
546 /* Get a line of input. */
547 if (! unformat_user(input, unformat_line_input, line_input))
550 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
551 if (unformat(line_input, "index %d", &map_domain_index))
553 else if (unformat(line_input, "psid %d", &psid))
555 else if (unformat(line_input, "ip6-dst %U", unformat_ip6_address, &tep))
558 return clib_error_return(0, "unknown input `%U'",
559 format_unformat_error, input);
561 unformat_free(line_input);
564 return clib_error_return(0, "mandatory argument(s) missing");
566 if (map_add_del_psid(map_domain_index, psid, &tep, 1) != 0) {
567 return clib_error_return(0, "Failing to add Mapping Rule");
572 #if MAP_SKIP_IP6_LOOKUP
573 static clib_error_t *
574 map_pre_resolve_command_fn (vlib_main_t *vm,
575 unformat_input_t *input,
576 vlib_cli_command_t *cmd)
578 unformat_input_t _line_input, *line_input = &_line_input;
581 map_main_t *mm = &map_main;
583 memset(&ip4nh, 0, sizeof(ip4nh));
584 memset(&ip6nh, 0, sizeof(ip6nh));
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) {
591 if (unformat(line_input, "ip4-nh %U", unformat_ip4_address, &ip4nh))
592 mm->preresolve_ip4 = ip4nh;
593 else if (unformat(line_input, "ip6-nh %U", unformat_ip6_address, &ip6nh))
594 mm->preresolve_ip6 = ip6nh;
596 return clib_error_return(0, "unknown input `%U'",
597 format_unformat_error, input);
599 unformat_free(line_input);
601 map_pre_resolve(&ip4nh, &ip6nh);
607 static clib_error_t *
608 map_icmp_relay_source_address_command_fn (vlib_main_t *vm,
609 unformat_input_t *input,
610 vlib_cli_command_t *cmd)
612 unformat_input_t _line_input, *line_input = &_line_input;
613 ip4_address_t icmp_src_address;
614 map_main_t *mm = &map_main;
616 mm->icmp4_src_address.as_u32 = 0;
618 /* Get a line of input. */
619 if (!unformat_user(input, unformat_line_input, line_input))
622 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
623 if (unformat(line_input, "%U", unformat_ip4_address, &icmp_src_address))
624 mm->icmp4_src_address = icmp_src_address;
626 return clib_error_return(0, "unknown input `%U'",
627 format_unformat_error, input);
629 unformat_free(line_input);
634 static clib_error_t *
635 map_icmp_unreachables_command_fn (vlib_main_t *vm,
636 unformat_input_t *input,
637 vlib_cli_command_t *cmd)
639 unformat_input_t _line_input, *line_input = &_line_input;
640 map_main_t *mm = &map_main;
643 /* Get a line of input. */
644 if (!unformat_user(input, unformat_line_input, line_input))
647 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
649 if (unformat(line_input, "on"))
650 mm->icmp6_enabled = true;
651 else if (unformat(line_input, "off"))
652 mm->icmp6_enabled = false;
654 return clib_error_return(0, "unknown input `%U'",
655 format_unformat_error, input);
657 unformat_free(line_input);
661 return clib_error_return(0, "mandatory argument(s) missing");
666 static clib_error_t *
667 map_fragment_command_fn (vlib_main_t *vm,
668 unformat_input_t *input,
669 vlib_cli_command_t *cmd)
671 unformat_input_t _line_input, *line_input = &_line_input;
672 map_main_t *mm = &map_main;
674 /* Get a line of input. */
675 if (!unformat_user(input, unformat_line_input, line_input))
678 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
679 if (unformat(line_input, "inner"))
680 mm->frag_inner = true;
681 else if (unformat(line_input, "outer"))
682 mm->frag_inner = false;
684 return clib_error_return(0, "unknown input `%U'",
685 format_unformat_error, input);
687 unformat_free(line_input);
692 static clib_error_t *
693 map_fragment_df_command_fn (vlib_main_t *vm,
694 unformat_input_t *input,
695 vlib_cli_command_t *cmd)
697 unformat_input_t _line_input, *line_input = &_line_input;
698 map_main_t *mm = &map_main;
700 /* Get a line of input. */
701 if (!unformat_user(input, unformat_line_input, line_input))
704 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
705 if (unformat(line_input, "on"))
706 mm->frag_ignore_df = true;
707 else if (unformat(line_input, "off"))
708 mm->frag_ignore_df = false;
710 return clib_error_return(0, "unknown input `%U'",
711 format_unformat_error, input);
713 unformat_free(line_input);
718 static clib_error_t *
719 map_traffic_class_command_fn (vlib_main_t *vm,
720 unformat_input_t *input,
721 vlib_cli_command_t *cmd)
723 unformat_input_t _line_input, *line_input = &_line_input;
724 map_main_t *mm = &map_main;
729 /* Get a line of input. */
730 if (!unformat_user(input, unformat_line_input, line_input))
733 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
734 if (unformat(line_input, "copy"))
736 else if (unformat(line_input, "%x", &tc))
739 return clib_error_return(0, "unknown input `%U'",
740 format_unformat_error, input);
742 unformat_free(line_input);
748 format_map_domain (u8 *s, va_list *args)
750 map_domain_t *d = va_arg(*args, map_domain_t *);
751 bool counters = va_arg(*args, int);
752 map_main_t *mm = &map_main;
753 ip6_address_t ip6_prefix;
756 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
758 ip6_prefix = d->ip6_prefix;
761 "[%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",
763 format_ip4_address, &d->ip4_prefix, d->ip4_prefix_len,
764 format_ip6_address, &ip6_prefix, d->ip6_prefix_len,
765 format_ip6_address, &d->ip6_src, d->ip6_src_len,
766 d->ea_bits_len, d->psid_offset, d->psid_length, d->mtu,
767 (d->flags & MAP_DOMAIN_TRANSLATION) ? "map-t" : "");
770 map_domain_counter_lock(mm);
772 vlib_get_combined_counter(&mm->domain_counters[MAP_DOMAIN_COUNTER_TX], d - mm->domains, &v);
773 s = format(s, " TX: %lld/%lld", v.packets, v.bytes);
774 vlib_get_combined_counter(&mm->domain_counters[MAP_DOMAIN_COUNTER_RX], d - mm->domains, &v);
775 s = format(s, " RX: %lld/%lld", v.packets, v.bytes);
776 map_domain_counter_unlock(mm);
783 for (i = 0; i < (0x1 << d->psid_length); i++) {
785 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0 )
788 " rule psid: %d ip6-dst %U\n", i, format_ip6_address, &dst);
795 format_map_ip4_reass (u8 *s, va_list *args)
797 map_main_t *mm = &map_main;
798 map_ip4_reass_t *r = va_arg(*args, map_ip4_reass_t *);
799 map_ip4_reass_key_t *k = &r->key;
800 f64 now = vlib_time_now(mm->vlib_main);
801 f64 lifetime = (((f64)mm->ip4_reass_conf_lifetime_ms) / 1000);
802 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
804 "ip4-reass src=%U dst=%U protocol=%d identifier=%d port=%d lifetime=%.3lf\n",
805 format_ip4_address, &k->src.as_u8, format_ip4_address, &k->dst.as_u8,
806 k->protocol, clib_net_to_host_u16(k->fragment_id), (r->port >= 0)?clib_net_to_host_u16(r->port):-1, dt);
811 format_map_ip6_reass (u8 *s, va_list *args)
813 map_main_t *mm = &map_main;
814 map_ip6_reass_t *r = va_arg(*args, map_ip6_reass_t *);
815 map_ip6_reass_key_t *k = &r->key;
816 f64 now = vlib_time_now(mm->vlib_main);
817 f64 lifetime = (((f64)mm->ip6_reass_conf_lifetime_ms) / 1000);
818 f64 dt = (r->ts + lifetime > now) ? (r->ts + lifetime - now) : -1;
820 "ip6-reass src=%U dst=%U protocol=%d identifier=%d lifetime=%.3lf\n",
821 format_ip6_address, &k->src.as_u8, format_ip6_address, &k->dst.as_u8,
822 k->protocol, clib_net_to_host_u32(k->fragment_id), dt);
826 static clib_error_t *
827 show_map_domain_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
829 unformat_input_t _line_input, *line_input = &_line_input;
830 map_main_t *mm = &map_main;
832 bool counters = false;
833 u32 map_domain_index = ~0;
835 /* Get a line of input. */
836 if (!unformat_user(input, unformat_line_input, line_input))
839 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
840 if (unformat(line_input, "counters"))
842 else if (unformat(line_input, "index %d", &map_domain_index))
845 return clib_error_return(0, "unknown input `%U'",
846 format_unformat_error, input);
848 unformat_free(line_input);
850 if (pool_elts(mm->domains) == 0)
851 vlib_cli_output(vm, "No MAP domains are configured...");
853 if (map_domain_index == ~0) {
854 pool_foreach(d, mm->domains, ({vlib_cli_output(vm, "%U", format_map_domain, d, counters);}));
856 if (pool_is_free_index(mm->domains, map_domain_index)) {
857 return clib_error_return(0, "MAP domain does not exists %d", map_domain_index);
860 d = pool_elt_at_index(mm->domains, map_domain_index);
861 vlib_cli_output(vm, "%U", format_map_domain, d, counters);
867 static clib_error_t *
868 show_map_fragments_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
870 map_main_t *mm = &map_main;
874 pool_foreach(f4, mm->ip4_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip4_reass, f4);}));
875 pool_foreach(f6, mm->ip6_reass_pool, ({vlib_cli_output (vm, "%U", format_map_ip6_reass, f6);}));
880 map_error_counter_get (u32 node_index, map_error_t map_error)
882 vlib_main_t *vm = vlib_get_main();
883 vlib_node_runtime_t *error_node = vlib_node_get_runtime(vm, node_index);
884 vlib_error_main_t *em = &vm->error_main;
885 vlib_error_t e = error_node->errors[map_error];
886 vlib_node_t *n = vlib_get_node(vm, node_index);
889 ci = vlib_error_get_code(e);
890 ASSERT (ci < n->n_errors);
891 ci += n->error_heap_index;
893 return (em->counters[ci]);
896 static clib_error_t *
897 show_map_stats_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
899 map_main_t *mm = &map_main;
901 int domains = 0, rules = 0, domaincount = 0, rulecount = 0;
902 if (pool_elts (mm->domains) == 0)
903 vlib_cli_output(vm, "No MAP domains are configured...");
905 pool_foreach(d, mm->domains, ({
907 rulecount+= 0x1 << d->psid_length;
908 rules += sizeof(ip6_address_t) * 0x1 << d->psid_length;
910 domains += sizeof(*d);
914 vlib_cli_output(vm, "MAP domains structure: %d\n", sizeof (map_domain_t));
915 vlib_cli_output(vm, "MAP domains: %d (%d bytes)\n", domaincount, domains);
916 vlib_cli_output(vm, "MAP rules: %d (%d bytes)\n", rulecount, rules);
917 vlib_cli_output(vm, "Total: %d bytes)\n", rules + domains);
919 #if MAP_SKIP_IP6_LOOKUP
920 vlib_cli_output(vm, "MAP pre-resolve: IP6 next-hop: %U (%u), IP4 next-hop: %U (%u)\n",
921 format_ip6_address, &mm->preresolve_ip6, mm->adj6_index,
922 format_ip4_address, &mm->preresolve_ip4, mm->adj4_index);
926 vlib_cli_output(vm, "MAP traffic-class: copy");
928 vlib_cli_output(vm, "MAP traffic-class: %x", mm->tc);
930 vlib_cli_output(vm, "MAP IPv6 inbound security check: %s, fragmented packet security check: %s", mm->sec_check ? "enabled" : "disabled",
931 mm->sec_check_frag ? "enabled" : "disabled");
933 vlib_cli_output(vm, "ICMP-relay IPv4 source address: %U\n", format_ip4_address, &mm->icmp4_src_address);
934 vlib_cli_output(vm, "ICMP6 unreachables sent for unmatched packets: %s\n", mm->icmp6_enabled ? "enabled" : "disabled");
935 vlib_cli_output(vm, "Inner fragmentation: %s\n", mm->frag_inner ? "enabled" : "disabled");
936 vlib_cli_output(vm, "Fragment packets regardless of DF flag: %s\n", mm->frag_ignore_df ? "enabled" : "disabled");
941 vlib_combined_counter_main_t *cm = mm->domain_counters;
942 u64 total_pkts[MAP_N_DOMAIN_COUNTER];
943 u64 total_bytes[MAP_N_DOMAIN_COUNTER];
947 memset (total_pkts, 0, sizeof (total_pkts));
948 memset (total_bytes, 0, sizeof (total_bytes));
950 map_domain_counter_lock (mm);
951 vec_foreach (cm, mm->domain_counters) {
952 which = cm - mm->domain_counters;
954 for (i = 0; i < vec_len (cm->maxi); i++) {
955 vlib_get_combined_counter (cm, i, &v);
956 total_pkts[which] += v.packets;
957 total_bytes[which] += v.bytes;
960 map_domain_counter_unlock (mm);
962 vlib_cli_output(vm, "Encapsulated packets: %lld bytes: %lld\n", total_pkts[MAP_DOMAIN_COUNTER_TX],
963 total_bytes[MAP_DOMAIN_COUNTER_TX]);
964 vlib_cli_output(vm, "Decapsulated packets: %lld bytes: %lld\n", total_pkts[MAP_DOMAIN_COUNTER_RX],
965 total_bytes[MAP_DOMAIN_COUNTER_RX]);
967 vlib_cli_output(vm, "ICMP relayed packets: %d\n", vlib_get_simple_counter(&mm->icmp_relayed, 0));
972 static clib_error_t *
973 map_params_reass_command_fn (vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
975 unformat_input_t _line_input, *line_input = &_line_input;
977 f64 ht_ratio = (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1);
979 u64 buffers = ~(0ull);
982 if (!unformat_user(input, unformat_line_input, line_input))
985 while (unformat_check_input(line_input) != UNFORMAT_END_OF_INPUT) {
986 if (!unformat(line_input, "lifetime %u", &lifetime) &&
987 !unformat(line_input, "ht-ratio %lf", &ht_ratio) &&
988 !unformat(line_input, "pool-size %u", &pool_size) &&
989 !unformat(line_input, "buffers %llu", &buffers) &&
990 !((unformat(line_input, "ip4")) && (ip4 = 1)) &&
991 !((unformat(line_input, "ip6")) && (ip6 = 1))) {
992 unformat_free(line_input);
993 return clib_error_return(0, "invalid input");
996 unformat_free(line_input);
999 return clib_error_return(0, "must specify ip4 and/or ip6");
1002 if (pool_size != ~0 && pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1003 return clib_error_return(0, "invalid ip4-reass pool-size ( > %d)", MAP_IP4_REASS_CONF_POOL_SIZE_MAX);
1004 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1) && ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1005 return clib_error_return(0, "invalid ip4-reass ht-ratio ( > %d)", MAP_IP4_REASS_CONF_HT_RATIO_MAX);
1006 if (lifetime != ~0 && lifetime > MAP_IP4_REASS_CONF_LIFETIME_MAX)
1007 return clib_error_return(0, "invalid ip4-reass lifetime ( > %d)", MAP_IP4_REASS_CONF_LIFETIME_MAX);
1008 if (buffers != ~(0ull) && buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
1009 return clib_error_return(0, "invalid ip4-reass buffers ( > %ld)", MAP_IP4_REASS_CONF_BUFFERS_MAX);
1013 if (pool_size != ~0 && pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1014 return clib_error_return(0, "invalid ip6-reass pool-size ( > %d)", MAP_IP6_REASS_CONF_POOL_SIZE_MAX);
1015 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1) && ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1016 return clib_error_return(0, "invalid ip6-reass ht-log2len ( > %d)", MAP_IP6_REASS_CONF_HT_RATIO_MAX);
1017 if (lifetime != ~0 && lifetime > MAP_IP6_REASS_CONF_LIFETIME_MAX)
1018 return clib_error_return(0, "invalid ip6-reass lifetime ( > %d)", MAP_IP6_REASS_CONF_LIFETIME_MAX);
1019 if (buffers != ~(0ull) && buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
1020 return clib_error_return(0, "invalid ip6-reass buffers ( > %ld)", MAP_IP6_REASS_CONF_BUFFERS_MAX);
1024 u32 reass = 0, packets = 0;
1025 if (pool_size != ~0) {
1026 if (map_ip4_reass_conf_pool_size(pool_size, &reass, &packets)) {
1027 vlib_cli_output(vm, "Could not set ip4-reass pool-size");
1029 vlib_cli_output(vm, "Setting ip4-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1032 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1)) {
1033 if (map_ip4_reass_conf_ht_ratio(ht_ratio, &reass, &packets)) {
1034 vlib_cli_output(vm, "Could not set ip4-reass ht-log2len");
1036 vlib_cli_output(vm, "Setting ip4-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1039 if (lifetime != ~0) {
1040 if (map_ip4_reass_conf_lifetime(lifetime))
1041 vlib_cli_output(vm, "Could not set ip4-reass lifetime");
1043 vlib_cli_output(vm, "Setting ip4-reass lifetime");
1045 if (buffers != ~(0ull)) {
1046 if (map_ip4_reass_conf_buffers(buffers))
1047 vlib_cli_output(vm, "Could not set ip4-reass buffers");
1049 vlib_cli_output(vm, "Setting ip4-reass buffers");
1052 if (map_main.ip4_reass_conf_buffers >
1053 map_main.ip4_reass_conf_pool_size * MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY) {
1054 vlib_cli_output(vm, "Note: 'ip4-reass buffers' > pool-size * max-fragments-per-reassembly.");
1059 u32 reass = 0, packets = 0;
1060 if (pool_size != ~0) {
1061 if (map_ip6_reass_conf_pool_size(pool_size, &reass, &packets)) {
1062 vlib_cli_output(vm, "Could not set ip6-reass pool-size");
1064 vlib_cli_output(vm, "Setting ip6-reass pool-size (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1067 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX+1)) {
1068 if (map_ip6_reass_conf_ht_ratio(ht_ratio, &reass, &packets)) {
1069 vlib_cli_output(vm, "Could not set ip6-reass ht-log2len");
1071 vlib_cli_output(vm, "Setting ip6-reass ht-log2len (destroyed-reassembly=%u , dropped-fragments=%u)", reass, packets);
1074 if (lifetime != ~0) {
1075 if (map_ip6_reass_conf_lifetime(lifetime))
1076 vlib_cli_output(vm, "Could not set ip6-reass lifetime");
1078 vlib_cli_output(vm, "Setting ip6-reass lifetime");
1080 if (buffers != ~(0ull)) {
1081 if (map_ip6_reass_conf_buffers(buffers))
1082 vlib_cli_output(vm, "Could not set ip6-reass buffers");
1084 vlib_cli_output(vm, "Setting ip6-reass buffers");
1087 if (map_main.ip6_reass_conf_buffers >
1088 map_main.ip6_reass_conf_pool_size * MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY) {
1089 vlib_cli_output(vm, "Note: 'ip6-reass buffers' > pool-size * max-fragments-per-reassembly.");
1098 * packet trace format function
1101 format_map_trace (u8 *s, va_list *args)
1103 CLIB_UNUSED(vlib_main_t *vm) = va_arg (*args, vlib_main_t *);
1104 CLIB_UNUSED(vlib_node_t *node) = va_arg (*args, vlib_node_t *);
1105 map_trace_t *t = va_arg (*args, map_trace_t *);
1106 u32 map_domain_index = t->map_domain_index;
1109 s = format(s, "MAP domain index: %d L4 port: %u", map_domain_index, clib_net_to_host_u16(port));
1114 static_always_inline map_ip4_reass_t *
1115 map_ip4_reass_lookup(map_ip4_reass_key_t *k, u32 bucket, f64 now)
1117 map_main_t *mm = &map_main;
1118 u32 ri = mm->ip4_reass_hash_table[bucket];
1119 while(ri != MAP_REASS_INDEX_NONE) {
1120 map_ip4_reass_t * r = pool_elt_at_index(mm->ip4_reass_pool, ri);
1121 if (r->key.as_u64[0] == k->as_u64[0] &&
1122 r->key.as_u64[1] == k->as_u64[1] &&
1123 now < r->ts + (((f64)mm->ip4_reass_conf_lifetime_ms) / 1000)) {
1126 ri = r->bucket_next;
1131 #define map_ip4_reass_pool_index(r) (r - map_main.ip4_reass_pool)
1134 map_ip4_reass_free(map_ip4_reass_t *r, u32 **pi_to_drop)
1136 map_main_t *mm = &map_main;
1137 map_ip4_reass_get_fragments(r, pi_to_drop);
1139 // Unlink in hash bucket
1140 map_ip4_reass_t *r2 = NULL;
1141 u32 r2i = mm->ip4_reass_hash_table[r->bucket];
1142 while (r2i != map_ip4_reass_pool_index(r)) {
1143 ASSERT(r2i != MAP_REASS_INDEX_NONE);
1144 r2 = pool_elt_at_index(mm->ip4_reass_pool, r2i);
1145 r2i = r2->bucket_next;
1148 r2->bucket_next = r->bucket_next;
1150 mm->ip4_reass_hash_table[r->bucket] = r->bucket_next;
1154 if (r->fifo_next == map_ip4_reass_pool_index(r)) {
1155 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1157 if(mm->ip4_reass_fifo_last == map_ip4_reass_pool_index(r))
1158 mm->ip4_reass_fifo_last = r->fifo_prev;
1159 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_prev)->fifo_next = r->fifo_next;
1160 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_next)->fifo_prev = r->fifo_prev;
1163 pool_put(mm->ip4_reass_pool, r);
1164 mm->ip4_reass_allocated--;
1168 map_ip4_reass_get(u32 src, u32 dst, u16 fragment_id,
1169 u8 protocol, u32 **pi_to_drop)
1171 map_ip4_reass_t * r;
1172 map_main_t *mm = &map_main;
1173 map_ip4_reass_key_t k = {.src.data_u32 = src,
1174 .dst.data_u32 = dst,
1175 .fragment_id = fragment_id,
1176 .protocol = protocol };
1179 h = crc_u32(k.as_u32[0], h);
1180 h = crc_u32(k.as_u32[1], h);
1181 h = crc_u32(k.as_u32[2], h);
1182 h = crc_u32(k.as_u32[3], h);
1183 h = h >> (32 - mm->ip4_reass_ht_log2len);
1185 f64 now = vlib_time_now(mm->vlib_main);
1187 //Cache garbage collection
1188 while (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1189 map_ip4_reass_t *last = pool_elt_at_index(mm->ip4_reass_pool, mm->ip4_reass_fifo_last);
1190 if (last->ts + (((f64)mm->ip4_reass_conf_lifetime_ms) / 1000) < now)
1191 map_ip4_reass_free(last, pi_to_drop);
1196 if ((r = map_ip4_reass_lookup(&k, h, now)))
1199 if (mm->ip4_reass_allocated >= mm->ip4_reass_conf_pool_size)
1202 pool_get(mm->ip4_reass_pool, r);
1203 mm->ip4_reass_allocated++;
1205 for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1206 r->fragments[i] = ~0;
1208 u32 ri = map_ip4_reass_pool_index(r);
1210 //Link in new bucket
1212 r->bucket_next = mm->ip4_reass_hash_table[h];
1213 mm->ip4_reass_hash_table[h] = ri;
1216 if(mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1217 r->fifo_next = pool_elt_at_index(mm->ip4_reass_pool, mm->ip4_reass_fifo_last)->fifo_next;
1218 r->fifo_prev = mm->ip4_reass_fifo_last;
1219 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_prev)->fifo_next = ri;
1220 pool_elt_at_index(mm->ip4_reass_pool, r->fifo_next)->fifo_prev = ri;
1222 r->fifo_next = r->fifo_prev = ri;
1223 mm->ip4_reass_fifo_last = ri;
1230 #ifdef MAP_IP4_REASS_COUNT_BYTES
1231 r->expected_total = 0xffff;
1239 map_ip4_reass_add_fragment(map_ip4_reass_t *r, u32 pi)
1241 if (map_main.ip4_reass_buffered_counter >= map_main.ip4_reass_conf_buffers)
1245 for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1246 if(r->fragments[i] == ~0) {
1247 r->fragments[i] = pi;
1248 map_main.ip4_reass_buffered_counter++;
1254 static_always_inline map_ip6_reass_t *
1255 map_ip6_reass_lookup(map_ip6_reass_key_t *k, u32 bucket, f64 now)
1257 map_main_t *mm = &map_main;
1258 u32 ri = mm->ip6_reass_hash_table[bucket];
1259 while(ri != MAP_REASS_INDEX_NONE) {
1260 map_ip6_reass_t * r = pool_elt_at_index(mm->ip6_reass_pool, ri);
1261 if(now < r->ts + (((f64)mm->ip6_reass_conf_lifetime_ms) / 1000) &&
1262 r->key.as_u64[0] == k->as_u64[0] &&
1263 r->key.as_u64[1] == k->as_u64[1] &&
1264 r->key.as_u64[2] == k->as_u64[2] &&
1265 r->key.as_u64[3] == k->as_u64[3] &&
1266 r->key.as_u64[4] == k->as_u64[4])
1268 ri = r->bucket_next;
1273 #define map_ip6_reass_pool_index(r) (r - map_main.ip6_reass_pool)
1276 map_ip6_reass_free(map_ip6_reass_t *r, u32 **pi_to_drop)
1278 map_main_t *mm = &map_main;
1280 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1281 if(r->fragments[i].pi != ~0) {
1282 vec_add1(*pi_to_drop, r->fragments[i].pi);
1283 r->fragments[i].pi = ~0;
1284 map_main.ip6_reass_buffered_counter--;
1287 // Unlink in hash bucket
1288 map_ip6_reass_t *r2 = NULL;
1289 u32 r2i = mm->ip6_reass_hash_table[r->bucket];
1290 while (r2i != map_ip6_reass_pool_index(r)) {
1291 ASSERT(r2i != MAP_REASS_INDEX_NONE);
1292 r2 = pool_elt_at_index(mm->ip6_reass_pool, r2i);
1293 r2i = r2->bucket_next;
1296 r2->bucket_next = r->bucket_next;
1298 mm->ip6_reass_hash_table[r->bucket] = r->bucket_next;
1302 if (r->fifo_next == map_ip6_reass_pool_index(r)) {
1303 //Single element in the list, list is now empty
1304 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1306 if (mm->ip6_reass_fifo_last == map_ip6_reass_pool_index(r)) //First element
1307 mm->ip6_reass_fifo_last = r->fifo_prev;
1308 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_prev)->fifo_next = r->fifo_next;
1309 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_next)->fifo_prev = r->fifo_prev;
1312 // Free from pool if necessary
1313 pool_put(mm->ip6_reass_pool, r);
1314 mm->ip6_reass_allocated--;
1318 map_ip6_reass_get(ip6_address_t *src, ip6_address_t *dst, u32 fragment_id,
1319 u8 protocol, u32 **pi_to_drop)
1321 map_ip6_reass_t * r;
1322 map_main_t *mm = &map_main;
1323 map_ip6_reass_key_t k = {
1326 .fragment_id = fragment_id,
1327 .protocol = protocol };
1331 for (i=0; i<10; i++)
1332 h = crc_u32(k.as_u32[i], h);
1333 h = h >> (32 - mm->ip6_reass_ht_log2len);
1335 f64 now = vlib_time_now(mm->vlib_main);
1337 //Cache garbage collection
1338 while (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1339 map_ip6_reass_t *last = pool_elt_at_index(mm->ip6_reass_pool, mm->ip6_reass_fifo_last);
1340 if (last->ts + (((f64)mm->ip6_reass_conf_lifetime_ms) / 1000) < now)
1341 map_ip6_reass_free(last, pi_to_drop);
1346 if ((r = map_ip6_reass_lookup(&k, h, now)))
1349 if (mm->ip6_reass_allocated >= mm->ip6_reass_conf_pool_size)
1352 pool_get(mm->ip6_reass_pool, r);
1353 mm->ip6_reass_allocated++;
1354 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++) {
1355 r->fragments[i].pi = ~0;
1356 r->fragments[i].next_data_len = 0;
1357 r->fragments[i].next_data_offset = 0;
1360 u32 ri = map_ip6_reass_pool_index(r);
1362 //Link in new bucket
1364 r->bucket_next = mm->ip6_reass_hash_table[h];
1365 mm->ip6_reass_hash_table[h] = ri;
1368 if(mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1369 r->fifo_next = pool_elt_at_index(mm->ip6_reass_pool, mm->ip6_reass_fifo_last)->fifo_next;
1370 r->fifo_prev = mm->ip6_reass_fifo_last;
1371 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_prev)->fifo_next = ri;
1372 pool_elt_at_index(mm->ip6_reass_pool, r->fifo_next)->fifo_prev = ri;
1374 r->fifo_next = r->fifo_prev = ri;
1375 mm->ip6_reass_fifo_last = ri;
1381 r->ip4_header.ip_version_and_header_length = 0;
1382 #ifdef MAP_IP6_REASS_COUNT_BYTES
1383 r->expected_total = 0xffff;
1390 map_ip6_reass_add_fragment(map_ip6_reass_t *r, u32 pi,
1391 u16 data_offset, u16 next_data_offset,
1392 u8 *data_start, u16 data_len)
1394 map_ip6_fragment_t *f = NULL, *prev_f = NULL;
1395 u16 copied_len = (data_len > 20) ? 20 : data_len;
1397 if (map_main.ip6_reass_buffered_counter >= map_main.ip6_reass_conf_buffers)
1400 //Lookup for fragments for the current buffer
1401 //and the one before that
1403 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++) {
1404 if (data_offset && r->fragments[i].next_data_offset == data_offset) {
1405 prev_f = &r->fragments[i]; // This is buffer for previous packet
1406 } else if (r->fragments[i].next_data_offset == next_data_offset) {
1407 f = &r->fragments[i]; // This is a buffer for the current packet
1408 } else if (r->fragments[i].next_data_offset == 0) { //Available
1410 f = &r->fragments[i];
1411 else if (prev_f == NULL)
1412 prev_f = &r->fragments[i];
1416 if (!f || f->pi != ~0)
1423 clib_memcpy(prev_f->next_data, data_start, copied_len);
1424 prev_f->next_data_len = copied_len;
1425 prev_f->next_data_offset = data_offset;
1427 if (((ip4_header_t *)data_start)->ip_version_and_header_length != 0x45)
1430 if (r->ip4_header.ip_version_and_header_length == 0)
1431 clib_memcpy(&r->ip4_header, data_start, sizeof(ip4_header_t));
1435 f->next_data_offset = next_data_offset;
1437 map_main.ip6_reass_buffered_counter++;
1442 void map_ip4_reass_reinit(u32 *trashed_reass, u32 *dropped_packets)
1444 map_main_t *mm = &map_main;
1448 *dropped_packets = mm->ip4_reass_buffered_counter;
1450 *trashed_reass = mm->ip4_reass_allocated;
1451 if (mm->ip4_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1452 u16 ri = mm->ip4_reass_fifo_last;
1454 map_ip4_reass_t *r = pool_elt_at_index(mm->ip4_reass_pool, ri);
1455 for (i=0; i<MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1456 if (r->fragments[i] != ~0)
1457 map_ip4_drop_pi(r->fragments[i]);
1460 pool_put(mm->ip4_reass_pool, r);
1461 } while (ri != mm->ip4_reass_fifo_last);
1464 vec_free(mm->ip4_reass_hash_table);
1465 vec_resize(mm->ip4_reass_hash_table, 1 << mm->ip4_reass_ht_log2len);
1466 for (i=0; i<(1 << mm->ip4_reass_ht_log2len); i++)
1467 mm->ip4_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1468 pool_free(mm->ip4_reass_pool);
1469 pool_alloc(mm->ip4_reass_pool, mm->ip4_reass_conf_pool_size);
1471 mm->ip4_reass_allocated = 0;
1472 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1473 mm->ip4_reass_buffered_counter = 0;
1476 u8 map_get_ht_log2len(f32 ht_ratio, u16 pool_size)
1478 u32 desired_size = (u32)(pool_size * ht_ratio);
1480 for (i=1; i<31; i++)
1481 if ((1 << i) >= desired_size)
1486 int map_ip4_reass_conf_ht_ratio(f32 ht_ratio, u32 *trashed_reass, u32 *dropped_packets)
1488 map_main_t *mm = &map_main;
1489 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
1492 map_ip4_reass_lock();
1493 mm->ip4_reass_conf_ht_ratio = ht_ratio;
1494 mm->ip4_reass_ht_log2len = map_get_ht_log2len(ht_ratio, mm->ip4_reass_conf_pool_size);
1495 map_ip4_reass_reinit(trashed_reass, dropped_packets);
1496 map_ip4_reass_unlock();
1500 int map_ip4_reass_conf_pool_size(u16 pool_size, u32 *trashed_reass, u32 *dropped_packets)
1502 map_main_t *mm = &map_main;
1503 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
1506 map_ip4_reass_lock();
1507 mm->ip4_reass_conf_pool_size = pool_size;
1508 map_ip4_reass_reinit(trashed_reass, dropped_packets);
1509 map_ip4_reass_unlock();
1513 int map_ip4_reass_conf_lifetime(u16 lifetime_ms)
1515 map_main.ip4_reass_conf_lifetime_ms = lifetime_ms;
1519 int map_ip4_reass_conf_buffers(u32 buffers)
1521 map_main.ip4_reass_conf_buffers = buffers;
1525 void map_ip6_reass_reinit(u32 *trashed_reass, u32 *dropped_packets)
1527 map_main_t *mm = &map_main;
1529 *dropped_packets = mm->ip6_reass_buffered_counter;
1531 *trashed_reass = mm->ip6_reass_allocated;
1533 if (mm->ip6_reass_fifo_last != MAP_REASS_INDEX_NONE) {
1534 u16 ri = mm->ip6_reass_fifo_last;
1536 map_ip6_reass_t *r = pool_elt_at_index(mm->ip6_reass_pool, ri);
1537 for (i=0; i<MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY; i++)
1538 if (r->fragments[i].pi != ~0)
1539 map_ip6_drop_pi(r->fragments[i].pi);
1542 pool_put(mm->ip6_reass_pool, r);
1543 } while (ri != mm->ip6_reass_fifo_last);
1544 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1547 vec_free(mm->ip6_reass_hash_table);
1548 vec_resize(mm->ip6_reass_hash_table, 1 << mm->ip6_reass_ht_log2len);
1549 for(i=0; i<(1 << mm->ip6_reass_ht_log2len); i++)
1550 mm->ip6_reass_hash_table[i] = MAP_REASS_INDEX_NONE;
1551 pool_free(mm->ip6_reass_pool);
1552 pool_alloc(mm->ip6_reass_pool, mm->ip4_reass_conf_pool_size);
1554 mm->ip6_reass_allocated = 0;
1555 mm->ip6_reass_buffered_counter = 0;
1558 int map_ip6_reass_conf_ht_ratio(f32 ht_ratio, u32 *trashed_reass, u32 *dropped_packets)
1560 map_main_t *mm = &map_main;
1561 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
1564 map_ip6_reass_lock();
1565 mm->ip6_reass_conf_ht_ratio = ht_ratio;
1566 mm->ip6_reass_ht_log2len = map_get_ht_log2len(ht_ratio, mm->ip6_reass_conf_pool_size);
1567 map_ip6_reass_reinit(trashed_reass, dropped_packets);
1568 map_ip6_reass_unlock();
1572 int map_ip6_reass_conf_pool_size(u16 pool_size, u32 *trashed_reass, u32 *dropped_packets)
1574 map_main_t *mm = &map_main;
1575 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
1578 map_ip6_reass_lock();
1579 mm->ip6_reass_conf_pool_size = pool_size;
1580 map_ip6_reass_reinit(trashed_reass, dropped_packets);
1581 map_ip6_reass_unlock();
1585 int map_ip6_reass_conf_lifetime(u16 lifetime_ms)
1587 map_main.ip6_reass_conf_lifetime_ms = lifetime_ms;
1591 int map_ip6_reass_conf_buffers(u32 buffers)
1593 map_main.ip6_reass_conf_buffers = buffers;
1597 VLIB_CLI_COMMAND(map_ip4_reass_lifetime_command, static) = {
1598 .path = "map params reassembly",
1599 .short_help = "[ip4 | ip6] [lifetime <lifetime-ms>] [pool-size <pool-size>] [buffers <buffers>] [ht-ratio <ht-ratio>]",
1600 .function = map_params_reass_command_fn,
1603 VLIB_CLI_COMMAND(map_traffic_class_command, static) = {
1604 .path = "map params traffic-class",
1606 "traffic-class {0x0-0xff | copy}",
1607 .function = map_traffic_class_command_fn,
1610 VLIB_CLI_COMMAND(map_pre_resolve_command, static) = {
1611 .path = "map params pre-resolve",
1613 "pre-resolve {ip4-nh <address>} | {ip6-nh <address>}",
1614 .function = map_pre_resolve_command_fn,
1617 VLIB_CLI_COMMAND(map_security_check_command, static) = {
1618 .path = "map params security-check",
1620 "security-check on|off",
1621 .function = map_security_check_command_fn,
1624 VLIB_CLI_COMMAND(map_icmp_relay_source_address_command, static) = {
1625 .path = "map params icmp source-address",
1626 .short_help = "source-address <ip4-address>",
1627 .function = map_icmp_relay_source_address_command_fn,
1630 VLIB_CLI_COMMAND(map_icmp_unreachables_command, static) = {
1631 .path = "map params icmp6 unreachables",
1632 .short_help = "unreachables {on|off}",
1633 .function = map_icmp_unreachables_command_fn,
1636 VLIB_CLI_COMMAND(map_fragment_command, static) = {
1637 .path = "map params fragment",
1638 .short_help = "[inner|outer] [ignore-df [on|off]]",
1639 .function = map_fragment_command_fn,
1642 VLIB_CLI_COMMAND(map_fragment_df_command, static) = {
1643 .path = "map params fragment ignore-df",
1644 .short_help = "on|off",
1645 .function = map_fragment_df_command_fn,
1648 VLIB_CLI_COMMAND(map_security_check_frag_command, static) = {
1649 .path = "map params security-check fragments",
1652 .function = map_security_check_frag_command_fn,
1655 VLIB_CLI_COMMAND(map_add_domain_command, static) = {
1656 .path = "map add domain",
1658 "map add domain ip4-pfx <ip4-pfx> ip6-pfx <ip6-pfx> ip6-src <ip6-pfx> "
1659 "ea-bits-len <n> psid-offset <n> psid-len <n> [map-t] [mtu <mtu>]",
1660 .function = map_add_domain_command_fn,
1663 VLIB_CLI_COMMAND(map_add_rule_command, static) = {
1664 .path = "map add rule",
1666 "map add rule index <domain> psid <psid> ip6-dst <ip6-addr>",
1667 .function = map_add_rule_command_fn,
1670 VLIB_CLI_COMMAND(map_del_command, static) = {
1671 .path = "map del domain",
1673 "map del domain index <domain>",
1674 .function = map_del_domain_command_fn,
1677 VLIB_CLI_COMMAND(show_map_domain_command, static) = {
1678 .path = "show map domain",
1679 .function = show_map_domain_command_fn,
1682 VLIB_CLI_COMMAND(show_map_stats_command, static) = {
1683 .path = "show map stats",
1684 .function = show_map_stats_command_fn,
1687 VLIB_CLI_COMMAND(show_map_fragments_command, static) = {
1688 .path = "show map fragments",
1689 .function = show_map_fragments_command_fn,
1695 clib_error_t *map_init (vlib_main_t *vm)
1697 map_main_t *mm = &map_main;
1698 mm->vnet_main = vnet_get_main();
1701 #ifdef MAP_SKIP_IP6_LOOKUP
1702 memset(&mm->preresolve_ip4, 0, sizeof(mm->preresolve_ip4));
1703 memset(&mm->preresolve_ip6, 0, sizeof(mm->preresolve_ip6));
1712 /* Inbound security check */
1713 mm->sec_check = true;
1714 mm->sec_check_frag = false;
1716 /* ICMP6 Type 1, Code 5 for security check failure */
1717 mm->icmp6_enabled = false;
1719 /* Inner or outer fragmentation */
1720 mm->frag_inner = false;
1721 mm->frag_ignore_df = false;
1723 vec_validate(mm->domain_counters, MAP_N_DOMAIN_COUNTER - 1);
1724 mm->domain_counters[MAP_DOMAIN_COUNTER_RX].name = "rx";
1725 mm->domain_counters[MAP_DOMAIN_COUNTER_TX].name = "tx";
1727 vlib_validate_simple_counter(&mm->icmp_relayed, 0);
1728 vlib_zero_simple_counter(&mm->icmp_relayed, 0);
1730 /* IP4 virtual reassembly */
1731 mm->ip4_reass_hash_table = 0;
1732 mm->ip4_reass_pool = 0;
1733 mm->ip4_reass_lock = clib_mem_alloc_aligned(CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
1734 mm->ip4_reass_conf_ht_ratio = MAP_IP4_REASS_HT_RATIO_DEFAULT;
1735 mm->ip4_reass_conf_lifetime_ms = MAP_IP4_REASS_LIFETIME_DEFAULT;
1736 mm->ip4_reass_conf_pool_size = MAP_IP4_REASS_POOL_SIZE_DEFAULT;
1737 mm->ip4_reass_conf_buffers = MAP_IP4_REASS_BUFFERS_DEFAULT;
1738 mm->ip4_reass_ht_log2len = map_get_ht_log2len(mm->ip4_reass_conf_ht_ratio, mm->ip4_reass_conf_pool_size);
1739 mm->ip4_reass_fifo_last = MAP_REASS_INDEX_NONE;
1740 map_ip4_reass_reinit(NULL, NULL);
1742 /* IP6 virtual reassembly */
1743 mm->ip6_reass_hash_table = 0;
1744 mm->ip6_reass_pool = 0;
1745 mm->ip6_reass_lock = clib_mem_alloc_aligned(CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
1746 mm->ip6_reass_conf_ht_ratio = MAP_IP6_REASS_HT_RATIO_DEFAULT;
1747 mm->ip6_reass_conf_lifetime_ms = MAP_IP6_REASS_LIFETIME_DEFAULT;
1748 mm->ip6_reass_conf_pool_size = MAP_IP6_REASS_POOL_SIZE_DEFAULT;
1749 mm->ip6_reass_conf_buffers = MAP_IP6_REASS_BUFFERS_DEFAULT;
1750 mm->ip6_reass_ht_log2len = map_get_ht_log2len(mm->ip6_reass_conf_ht_ratio, mm->ip6_reass_conf_pool_size);
1751 mm->ip6_reass_fifo_last = MAP_REASS_INDEX_NONE;
1752 map_ip6_reass_reinit(NULL, NULL);
1757 VLIB_INIT_FUNCTION(map_init);