2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <vnet/ip/ip4_to_ip6.h>
18 #include <vnet/ip/ip6_to_ip4.h>
19 #include <vnet/ip/ip_frag.h>
23 IP6_MAPT_NEXT_MAPT_TCP_UDP,
24 IP6_MAPT_NEXT_MAPT_ICMP,
25 IP6_MAPT_NEXT_MAPT_FRAGMENTED,
32 IP6_MAPT_ICMP_NEXT_IP4_LOOKUP,
33 IP6_MAPT_ICMP_NEXT_IP4_FRAG,
34 IP6_MAPT_ICMP_NEXT_DROP,
36 } ip6_mapt_icmp_next_t;
40 IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP,
41 IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG,
42 IP6_MAPT_TCP_UDP_NEXT_DROP,
43 IP6_MAPT_TCP_UDP_N_NEXT
44 } ip6_mapt_tcp_udp_next_t;
48 IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP,
49 IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG,
50 IP6_MAPT_FRAGMENTED_NEXT_DROP,
51 IP6_MAPT_FRAGMENTED_N_NEXT
52 } ip6_mapt_fragmented_next_t;
58 } icmp6_to_icmp_ctx_t;
61 ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
63 icmp6_to_icmp_ctx_t *ctx = arg;
67 // Note that this prevents an intermediate IPv6 router from answering
69 ip4_sadr = map_get_ip4 (&ip6->src_address, ctx->d->flags);
70 if (ip6->src_address.as_u64[0] !=
71 map_get_pfx_net (ctx->d, ip4_sadr, ctx->sender_port)
72 || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr,
76 ip4->dst_address.as_u32 =
77 ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
78 ip4->src_address.as_u32 = ip4_sadr;
84 ip6_to_ip4_set_inner_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4,
87 icmp6_to_icmp_ctx_t *ctx = arg;
90 //Security check of inner packet
91 inner_ip4_dadr = map_get_ip4 (&ip6->dst_address, ctx->d->flags);
92 if (ip6->dst_address.as_u64[0] !=
93 map_get_pfx_net (ctx->d, inner_ip4_dadr, ctx->sender_port)
94 || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d,
99 ip4->dst_address.as_u32 = inner_ip4_dadr;
100 ip4->src_address.as_u32 =
101 ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
107 ip6_map_t_icmp (vlib_main_t * vm,
108 vlib_node_runtime_t * node, vlib_frame_t * frame)
110 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
111 vlib_node_runtime_t *error_node =
112 vlib_node_get_runtime (vm, ip6_map_t_icmp_node.index);
113 from = vlib_frame_vector_args (frame);
114 n_left_from = frame->n_vectors;
115 next_index = node->cached_next_index;
116 vlib_combined_counter_main_t *cm = map_main.domain_counters;
117 u32 thread_index = vm->thread_index;
119 while (n_left_from > 0)
121 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
123 while (n_left_from > 0 && n_left_to_next > 0)
128 ip6_mapt_icmp_next_t next0;
131 icmp6_to_icmp_ctx_t ctx0;
134 pi0 = to_next[0] = from[0];
139 error0 = MAP_ERROR_NONE;
140 next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP;
142 p0 = vlib_get_buffer (vm, pi0);
143 ip60 = vlib_buffer_get_current (p0);
144 len0 = clib_net_to_host_u16 (ip60->payload_length);
146 pool_elt_at_index (map_main.domains,
147 vnet_buffer (p0)->map_t.map_domain_index);
149 ctx0.sender_port = 0;
151 (vm, p0, ip60, p0->current_length, NULL, &ctx0.sender_port,
152 NULL, NULL, NULL, NULL))
154 // In case of 1:1 mapping, we don't care about the port
155 if (!(d0->ea_bits_len == 0 && d0->rules))
157 error0 = MAP_ERROR_ICMP;
162 if (icmp6_to_icmp (vm, p0, ip6_to_ip4_set_icmp_cb, &ctx0,
163 ip6_to_ip4_set_inner_icmp_cb, &ctx0))
165 error0 = MAP_ERROR_ICMP;
169 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
171 // Send to fragmentation node if necessary
172 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
173 vnet_buffer (p0)->ip_frag.next_index = IP_FRAG_NEXT_IP4_LOOKUP;
174 next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
177 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
179 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
182 map_t.map_domain_index, 1,
187 next0 = IP6_MAPT_ICMP_NEXT_DROP;
190 p0->error = error_node->errors[error0];
191 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
192 to_next, n_left_to_next, pi0,
195 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
197 return frame->n_vectors;
201 * Translate IPv6 fragmented packet to IPv4.
204 map_ip6_to_ip4_fragmented (vlib_main_t * vm, vlib_buffer_t * p)
207 ip6_frag_hdr_t *frag;
215 ip6 = vlib_buffer_get_current (p);
218 (vm, p, ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
221 frag = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
222 ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
223 vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
225 frag_id = frag_id_6to4 (frag->identification);
226 frag_more = ip6_frag_hdr_more (frag);
227 frag_offset = ip6_frag_hdr_offset (frag);
229 ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
230 ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
232 ip4->ip_version_and_header_length =
233 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
234 ip4->tos = ip6_translate_tos (ip6->ip_version_traffic_class_and_flow_label);
236 u16_net_add (ip6->payload_length,
237 sizeof (*ip4) - l4_offset + sizeof (*ip6));
238 ip4->fragment_id = frag_id;
239 ip4->flags_and_fragment_offset =
240 clib_host_to_net_u16 (frag_offset |
241 (frag_more ? IP4_HEADER_FLAG_MORE_FRAGMENTS : 0));
242 ip4->ttl = ip6->hop_limit;
244 (l4_protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : l4_protocol;
245 ip4->checksum = ip4_header_checksum (ip4);
251 ip6_map_t_fragmented (vlib_main_t * vm,
252 vlib_node_runtime_t * node, vlib_frame_t * frame)
254 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
255 from = vlib_frame_vector_args (frame);
256 n_left_from = frame->n_vectors;
257 next_index = node->cached_next_index;
258 vlib_node_runtime_t *error_node =
259 vlib_node_get_runtime (vm, ip6_map_t_fragmented_node.index);
261 while (n_left_from > 0)
263 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
265 while (n_left_from > 0 && n_left_to_next > 0)
271 pi0 = to_next[0] = from[0];
277 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
278 p0 = vlib_get_buffer (vm, pi0);
280 if (map_ip6_to_ip4_fragmented (vm, p0))
282 p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
283 next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
287 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
289 // Send to fragmentation node if necessary
290 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
291 vnet_buffer (p0)->ip_frag.next_index =
292 IP_FRAG_NEXT_IP4_LOOKUP;
293 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
297 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
298 to_next, n_left_to_next, pi0,
301 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
303 return frame->n_vectors;
307 * Translate IPv6 UDP/TCP packet to IPv4.
310 map_ip6_to_ip4_tcp_udp (vlib_main_t * vm, vlib_buffer_t * p,
313 map_main_t *mm = &map_main;
323 ip6_address_t old_src, old_dst;
325 ip6 = vlib_buffer_get_current (p);
328 (vm, p, ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
331 if (l4_protocol == IP_PROTOCOL_TCP)
333 tcp_header_t *tcp = ip6_next_header (ip6);
336 csum = tcp->checksum;
337 map_mss_clamping (tcp, &csum, mm->tcp_mss);
338 tcp->checksum = ip_csum_fold (csum);
340 checksum = &tcp->checksum;
344 udp_header_t *udp = ip6_next_header (ip6);
345 checksum = &udp->checksum;
348 old_src.as_u64[0] = ip6->src_address.as_u64[0];
349 old_src.as_u64[1] = ip6->src_address.as_u64[1];
350 old_dst.as_u64[0] = ip6->dst_address.as_u64[0];
351 old_dst.as_u64[1] = ip6->dst_address.as_u64[1];
353 ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
355 vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
357 if (PREDICT_FALSE (frag_offset))
359 // Only the first fragment
360 ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
361 fragment_id = frag_id_6to4 (hdr->identification);
362 flags = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
370 ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
371 ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
373 ip4->ip_version_and_header_length =
374 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
375 ip4->tos = ip6_translate_tos (ip6->ip_version_traffic_class_and_flow_label);
377 u16_net_add (ip6->payload_length,
378 sizeof (*ip4) + sizeof (*ip6) - l4_offset);
379 ip4->fragment_id = fragment_id;
380 ip4->flags_and_fragment_offset = flags;
381 ip4->ttl = ip6->hop_limit;
382 ip4->protocol = l4_protocol;
383 ip4->checksum = ip4_header_checksum (ip4);
385 // UDP checksum is optional over IPv4
386 if (!udp_checksum && l4_protocol == IP_PROTOCOL_UDP)
392 csum = ip_csum_sub_even (*checksum, old_src.as_u64[0]);
393 csum = ip_csum_sub_even (csum, old_src.as_u64[1]);
394 csum = ip_csum_sub_even (csum, old_dst.as_u64[0]);
395 csum = ip_csum_sub_even (csum, old_dst.as_u64[1]);
396 csum = ip_csum_add_even (csum, ip4->dst_address.as_u32);
397 csum = ip_csum_add_even (csum, ip4->src_address.as_u32);
398 *checksum = ip_csum_fold (csum);
405 ip6_map_t_tcp_udp (vlib_main_t * vm,
406 vlib_node_runtime_t * node, vlib_frame_t * frame)
408 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
409 vlib_node_runtime_t *error_node =
410 vlib_node_get_runtime (vm, ip6_map_t_tcp_udp_node.index);
412 from = vlib_frame_vector_args (frame);
413 n_left_from = frame->n_vectors;
414 next_index = node->cached_next_index;
415 while (n_left_from > 0)
417 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
419 while (n_left_from > 0 && n_left_to_next > 0)
423 ip6_mapt_tcp_udp_next_t next0;
425 pi0 = to_next[0] = from[0];
430 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
432 p0 = vlib_get_buffer (vm, pi0);
434 if (map_ip6_to_ip4_tcp_udp (vm, p0, true))
436 p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
437 next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
441 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
443 // Send to fragmentation node if necessary
444 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
445 vnet_buffer (p0)->ip_frag.next_index =
446 IP_FRAG_NEXT_IP4_LOOKUP;
447 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
451 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
452 to_next, n_left_to_next, pi0,
455 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
457 return frame->n_vectors;
461 ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
463 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
464 vlib_node_runtime_t *error_node =
465 vlib_node_get_runtime (vm, ip6_map_t_node.index);
466 vlib_combined_counter_main_t *cm = map_main.domain_counters;
467 u32 thread_index = vm->thread_index;
469 from = vlib_frame_vector_args (frame);
470 n_left_from = frame->n_vectors;
471 next_index = node->cached_next_index;
472 while (n_left_from > 0)
474 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
476 while (n_left_from > 0 && n_left_to_next > 0)
485 ip6_frag_hdr_t *frag0;
486 ip6_mapt_next_t next0 = 0;
489 pi0 = to_next[0] = from[0];
494 error0 = MAP_ERROR_NONE;
495 p0 = vlib_get_buffer (vm, pi0);
496 u16 l4_src_port = vnet_buffer (p0)->ip.reass.l4_src_port;
498 ip60 = vlib_buffer_get_current (p0);
501 ip6_map_get_domain (&ip60->dst_address,
502 &vnet_buffer (p0)->map_t.map_domain_index,
505 { /* Guess it wasn't for us */
506 vnet_feature_next (&next0, p0);
510 saddr = map_get_ip4 (&ip60->src_address, d0->ip6_src_len);
511 vnet_buffer (p0)->map_t.v6.saddr = saddr;
512 vnet_buffer (p0)->map_t.v6.daddr =
513 ip6_map_t_embedded_address (d0, &ip60->dst_address);
514 vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
517 (ip6_parse (vm, p0, ip60, p0->current_length,
518 &(vnet_buffer (p0)->map_t.v6.l4_protocol),
519 &(vnet_buffer (p0)->map_t.v6.l4_offset),
520 &(vnet_buffer (p0)->map_t.v6.frag_offset))))
523 error0 == MAP_ERROR_NONE ? MAP_ERROR_MALFORMED : error0;
528 (u32) clib_net_to_host_u16 (ip60->payload_length) +
529 sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
531 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
532 vnet_buffer (p0)->map_t.v6.
536 (vnet_buffer (p0)->map_t.v6.frag_offset
537 && ip6_frag_hdr_offset (frag0)))
539 map_port0 = l4_src_port;
540 next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
544 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
548 sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : error0;
549 vnet_buffer (p0)->map_t.checksum_offset =
550 vnet_buffer (p0)->map_t.v6.l4_offset + 16;
551 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
552 map_port0 = l4_src_port;
556 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
560 sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : error0;
561 vnet_buffer (p0)->map_t.checksum_offset =
562 vnet_buffer (p0)->map_t.v6.l4_offset + 6;
563 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
564 map_port0 = l4_src_port;
566 else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
571 sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : error0;
572 next0 = IP6_MAPT_NEXT_MAPT_ICMP;
573 if (((icmp46_header_t *)
575 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
577 || ((icmp46_header_t *)
579 vnet_buffer (p0)->map_t.v6.l4_offset))->
580 code == ICMP6_echo_request)
581 map_port0 = l4_src_port;
585 // TODO: In case of 1:1 mapping, it might be possible to
586 // do something with those packets.
587 error0 = MAP_ERROR_BAD_PROTOCOL;
590 if (PREDICT_FALSE (map_port0 != -1) &&
591 (ip60->src_address.as_u64[0] !=
592 map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
594 || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
600 // Security check when map_port0 is not zero (non-first
601 // fragment, UDP or TCP)
603 error0 == MAP_ERROR_NONE ? MAP_ERROR_SEC_CHECK : error0;
607 (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
609 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
611 vnet_buffer (p0)->map_t.
613 clib_net_to_host_u16 (ip60->
617 next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
618 p0->error = error_node->errors[error0];
619 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
621 map_add_trace (vm, node, p0,
622 vnet_buffer (p0)->map_t.map_domain_index,
626 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
627 to_next, n_left_to_next, pi0,
630 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
632 return frame->n_vectors;
635 static char *map_t_error_strings[] = {
636 #define _(sym, string) string,
642 VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = {
643 .function = ip6_map_t_fragmented,
644 .name = "ip6-map-t-fragmented",
645 .vector_size = sizeof (u32),
646 .format_trace = format_map_trace,
647 .type = VLIB_NODE_TYPE_INTERNAL,
649 .n_errors = MAP_N_ERROR,
650 .error_strings = map_t_error_strings,
652 .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT,
655 [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup",
656 [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
657 [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop",
663 VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = {
664 .function = ip6_map_t_icmp,
665 .name = "ip6-map-t-icmp",
666 .vector_size = sizeof (u32),
667 .format_trace = format_map_trace,
668 .type = VLIB_NODE_TYPE_INTERNAL,
670 .n_errors = MAP_N_ERROR,
671 .error_strings = map_t_error_strings,
673 .n_next_nodes = IP6_MAPT_ICMP_N_NEXT,
676 [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup",
677 [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
678 [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop",
684 VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = {
685 .function = ip6_map_t_tcp_udp,
686 .name = "ip6-map-t-tcp-udp",
687 .vector_size = sizeof (u32),
688 .format_trace = format_map_trace,
689 .type = VLIB_NODE_TYPE_INTERNAL,
691 .n_errors = MAP_N_ERROR,
692 .error_strings = map_t_error_strings,
694 .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT,
697 [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup",
698 [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
699 [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop",
705 VNET_FEATURE_INIT (ip6_map_t_feature, static) = {
706 .arc_name = "ip6-unicast",
707 .node_name = "ip6-map-t",
708 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
709 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
712 VLIB_REGISTER_NODE(ip6_map_t_node) = {
713 .function = ip6_map_t,
715 .vector_size = sizeof(u32),
716 .format_trace = format_map_trace,
717 .type = VLIB_NODE_TYPE_INTERNAL,
719 .n_errors = MAP_N_ERROR,
720 .error_strings = map_t_error_strings,
722 .n_next_nodes = IP6_MAPT_N_NEXT,
725 [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp",
726 [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp",
727 [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented",
728 [IP6_MAPT_NEXT_DROP] = "error-drop",
734 * fd.io coding-style-patch-verification: ON
737 * eval: (c-set-style "gnu")