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);
148 ctx0.sender_port = ip6_get_port (ip60, 0, p0->current_length);
150 if (ctx0.sender_port == 0)
152 // In case of 1:1 mapping, we don't care about the port
153 if (!(d0->ea_bits_len == 0 && d0->rules))
155 error0 = MAP_ERROR_ICMP;
161 (p0, ip6_to_ip4_set_icmp_cb, &ctx0,
162 ip6_to_ip4_set_inner_icmp_cb, &ctx0))
164 error0 = MAP_ERROR_ICMP;
168 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
170 // Send to fragmentation node if necessary
171 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
172 vnet_buffer (p0)->ip_frag.next_index = IP_FRAG_NEXT_IP4_LOOKUP;
173 next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
176 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
178 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
181 map_t.map_domain_index, 1,
186 next0 = IP6_MAPT_ICMP_NEXT_DROP;
189 p0->error = error_node->errors[error0];
190 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
191 to_next, n_left_to_next, pi0,
194 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
196 return frame->n_vectors;
200 * Translate IPv6 fragmented packet to IPv4.
203 map_ip6_to_ip4_fragmented (vlib_buffer_t * p)
206 ip6_frag_hdr_t *frag;
214 ip6 = vlib_buffer_get_current (p);
217 (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
220 frag = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
221 ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
222 vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
224 frag_id = frag_id_6to4 (frag->identification);
225 frag_more = ip6_frag_hdr_more (frag);
226 frag_offset = ip6_frag_hdr_offset (frag);
228 ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
229 ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
231 ip4->ip_version_and_header_length =
232 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
233 ip4->tos = ip6_translate_tos (ip6);
235 u16_net_add (ip6->payload_length,
236 sizeof (*ip4) - l4_offset + sizeof (*ip6));
237 ip4->fragment_id = frag_id;
238 ip4->flags_and_fragment_offset =
239 clib_host_to_net_u16 (frag_offset |
240 (frag_more ? IP4_HEADER_FLAG_MORE_FRAGMENTS : 0));
241 ip4->ttl = ip6->hop_limit;
243 (l4_protocol == IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : l4_protocol;
244 ip4->checksum = ip4_header_checksum (ip4);
250 ip6_map_t_fragmented (vlib_main_t * vm,
251 vlib_node_runtime_t * node, vlib_frame_t * frame)
253 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
254 from = vlib_frame_vector_args (frame);
255 n_left_from = frame->n_vectors;
256 next_index = node->cached_next_index;
257 vlib_node_runtime_t *error_node =
258 vlib_node_get_runtime (vm, ip6_map_t_fragmented_node.index);
260 while (n_left_from > 0)
262 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
264 while (n_left_from > 0 && n_left_to_next > 0)
270 pi0 = to_next[0] = from[0];
276 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
277 p0 = vlib_get_buffer (vm, pi0);
279 if (map_ip6_to_ip4_fragmented (p0))
281 p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
282 next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
286 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
288 // Send to fragmentation node if necessary
289 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
290 vnet_buffer (p0)->ip_frag.next_index =
291 IP_FRAG_NEXT_IP4_LOOKUP;
292 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
296 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
297 to_next, n_left_to_next, pi0,
300 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
302 return frame->n_vectors;
306 * Translate IPv6 UDP/TCP packet to IPv4.
309 map_ip6_to_ip4_tcp_udp (vlib_buffer_t * p, bool udp_checksum)
311 map_main_t *mm = &map_main;
321 ip6_address_t old_src, old_dst;
323 ip6 = vlib_buffer_get_current (p);
326 (ip6, p->current_length, &l4_protocol, &l4_offset, &frag_offset))
329 if (l4_protocol == IP_PROTOCOL_TCP)
331 tcp_header_t *tcp = ip6_next_header (ip6);
334 csum = tcp->checksum;
335 map_mss_clamping (tcp, &csum, mm->tcp_mss);
336 tcp->checksum = ip_csum_fold (csum);
338 checksum = &tcp->checksum;
342 udp_header_t *udp = ip6_next_header (ip6);
343 checksum = &udp->checksum;
346 old_src.as_u64[0] = ip6->src_address.as_u64[0];
347 old_src.as_u64[1] = ip6->src_address.as_u64[1];
348 old_dst.as_u64[0] = ip6->dst_address.as_u64[0];
349 old_dst.as_u64[1] = ip6->dst_address.as_u64[1];
351 ip4 = (ip4_header_t *) u8_ptr_add (ip6, l4_offset - sizeof (*ip4));
353 vlib_buffer_advance (p, l4_offset - sizeof (*ip4));
355 if (PREDICT_FALSE (frag_offset))
357 // Only the first fragment
358 ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip6, frag_offset);
359 fragment_id = frag_id_6to4 (hdr->identification);
360 flags = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
368 ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
369 ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
371 ip4->ip_version_and_header_length =
372 IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
373 ip4->tos = ip6_translate_tos (ip6);
375 u16_net_add (ip6->payload_length,
376 sizeof (*ip4) + sizeof (*ip6) - l4_offset);
377 ip4->fragment_id = fragment_id;
378 ip4->flags_and_fragment_offset = flags;
379 ip4->ttl = ip6->hop_limit;
380 ip4->protocol = l4_protocol;
381 ip4->checksum = ip4_header_checksum (ip4);
383 // UDP checksum is optional over IPv4
384 if (!udp_checksum && l4_protocol == IP_PROTOCOL_UDP)
390 csum = ip_csum_sub_even (*checksum, old_src.as_u64[0]);
391 csum = ip_csum_sub_even (csum, old_src.as_u64[1]);
392 csum = ip_csum_sub_even (csum, old_dst.as_u64[0]);
393 csum = ip_csum_sub_even (csum, old_dst.as_u64[1]);
394 csum = ip_csum_add_even (csum, ip4->dst_address.as_u32);
395 csum = ip_csum_add_even (csum, ip4->src_address.as_u32);
396 *checksum = ip_csum_fold (csum);
403 ip6_map_t_tcp_udp (vlib_main_t * vm,
404 vlib_node_runtime_t * node, vlib_frame_t * frame)
406 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
407 vlib_node_runtime_t *error_node =
408 vlib_node_get_runtime (vm, ip6_map_t_tcp_udp_node.index);
410 from = vlib_frame_vector_args (frame);
411 n_left_from = frame->n_vectors;
412 next_index = node->cached_next_index;
413 while (n_left_from > 0)
415 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
417 while (n_left_from > 0 && n_left_to_next > 0)
421 ip6_mapt_tcp_udp_next_t next0;
423 pi0 = to_next[0] = from[0];
428 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
430 p0 = vlib_get_buffer (vm, pi0);
432 if (map_ip6_to_ip4_tcp_udp (p0, true))
434 p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
435 next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
439 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
441 // Send to fragmentation node if necessary
442 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
443 vnet_buffer (p0)->ip_frag.next_index =
444 IP_FRAG_NEXT_IP4_LOOKUP;
445 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
449 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
450 to_next, n_left_to_next, pi0,
453 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
455 return frame->n_vectors;
459 ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
461 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
462 vlib_node_runtime_t *error_node =
463 vlib_node_get_runtime (vm, ip6_map_t_node.index);
464 vlib_combined_counter_main_t *cm = map_main.domain_counters;
465 u32 thread_index = vm->thread_index;
467 from = vlib_frame_vector_args (frame);
468 n_left_from = frame->n_vectors;
469 next_index = node->cached_next_index;
470 while (n_left_from > 0)
472 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
474 while (n_left_from > 0 && n_left_to_next > 0)
483 ip6_frag_hdr_t *frag0;
484 ip6_mapt_next_t next0 = 0;
487 pi0 = to_next[0] = from[0];
492 error0 = MAP_ERROR_NONE;
493 p0 = vlib_get_buffer (vm, pi0);
494 u16 l4_src_port = vnet_buffer (p0)->ip.reass.l4_src_port;
496 ip60 = vlib_buffer_get_current (p0);
499 ip6_map_get_domain (&ip60->dst_address,
500 &vnet_buffer (p0)->map_t.map_domain_index,
503 { /* Guess it wasn't for us */
504 vnet_feature_next (&next0, p0);
508 saddr = map_get_ip4 (&ip60->src_address, d0->ip6_src_len);
509 vnet_buffer (p0)->map_t.v6.saddr = saddr;
510 vnet_buffer (p0)->map_t.v6.daddr =
511 ip6_map_t_embedded_address (d0, &ip60->dst_address);
512 vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
515 (ip6_parse (ip60, p0->current_length,
516 &(vnet_buffer (p0)->map_t.v6.l4_protocol),
517 &(vnet_buffer (p0)->map_t.v6.l4_offset),
518 &(vnet_buffer (p0)->map_t.v6.frag_offset))))
521 error0 == MAP_ERROR_NONE ? MAP_ERROR_MALFORMED : error0;
526 (u32) clib_net_to_host_u16 (ip60->payload_length) +
527 sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
529 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
530 vnet_buffer (p0)->map_t.v6.
534 (vnet_buffer (p0)->map_t.v6.frag_offset
535 && ip6_frag_hdr_offset (frag0)))
537 map_port0 = l4_src_port;
538 next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
542 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
546 sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : error0;
547 vnet_buffer (p0)->map_t.checksum_offset =
548 vnet_buffer (p0)->map_t.v6.l4_offset + 16;
549 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
550 map_port0 = l4_src_port;
554 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
558 sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : error0;
559 vnet_buffer (p0)->map_t.checksum_offset =
560 vnet_buffer (p0)->map_t.v6.l4_offset + 6;
561 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
562 map_port0 = l4_src_port;
564 else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
569 sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : error0;
570 next0 = IP6_MAPT_NEXT_MAPT_ICMP;
571 if (((icmp46_header_t *)
573 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
575 || ((icmp46_header_t *)
577 vnet_buffer (p0)->map_t.v6.l4_offset))->
578 code == ICMP6_echo_request)
579 map_port0 = l4_src_port;
583 // TODO: In case of 1:1 mapping, it might be possible to
584 // do something with those packets.
585 error0 = MAP_ERROR_BAD_PROTOCOL;
588 if (PREDICT_FALSE (map_port0 != -1) &&
589 (ip60->src_address.as_u64[0] !=
590 map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
592 || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
598 // Security check when map_port0 is not zero (non-first
599 // fragment, UDP or TCP)
601 error0 == MAP_ERROR_NONE ? MAP_ERROR_SEC_CHECK : error0;
605 (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
607 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
609 vnet_buffer (p0)->map_t.
611 clib_net_to_host_u16 (ip60->
615 next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
616 p0->error = error_node->errors[error0];
617 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
619 map_add_trace (vm, node, p0,
620 vnet_buffer (p0)->map_t.map_domain_index,
624 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
625 to_next, n_left_to_next, pi0,
628 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
630 return frame->n_vectors;
633 static char *map_t_error_strings[] = {
634 #define _(sym, string) string,
640 VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = {
641 .function = ip6_map_t_fragmented,
642 .name = "ip6-map-t-fragmented",
643 .vector_size = sizeof (u32),
644 .format_trace = format_map_trace,
645 .type = VLIB_NODE_TYPE_INTERNAL,
647 .n_errors = MAP_N_ERROR,
648 .error_strings = map_t_error_strings,
650 .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT,
653 [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup",
654 [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
655 [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop",
661 VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = {
662 .function = ip6_map_t_icmp,
663 .name = "ip6-map-t-icmp",
664 .vector_size = sizeof (u32),
665 .format_trace = format_map_trace,
666 .type = VLIB_NODE_TYPE_INTERNAL,
668 .n_errors = MAP_N_ERROR,
669 .error_strings = map_t_error_strings,
671 .n_next_nodes = IP6_MAPT_ICMP_N_NEXT,
674 [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup",
675 [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
676 [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop",
682 VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = {
683 .function = ip6_map_t_tcp_udp,
684 .name = "ip6-map-t-tcp-udp",
685 .vector_size = sizeof (u32),
686 .format_trace = format_map_trace,
687 .type = VLIB_NODE_TYPE_INTERNAL,
689 .n_errors = MAP_N_ERROR,
690 .error_strings = map_t_error_strings,
692 .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT,
695 [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup",
696 [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
697 [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop",
703 VNET_FEATURE_INIT (ip6_map_t_feature, static) = {
704 .arc_name = "ip6-unicast",
705 .node_name = "ip6-map-t",
706 .runs_before = VNET_FEATURES ("ip6-flow-classify"),
707 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
710 VLIB_REGISTER_NODE(ip6_map_t_node) = {
711 .function = ip6_map_t,
713 .vector_size = sizeof(u32),
714 .format_trace = format_map_trace,
715 .type = VLIB_NODE_TYPE_INTERNAL,
717 .n_errors = MAP_N_ERROR,
718 .error_strings = map_t_error_strings,
720 .n_next_nodes = IP6_MAPT_N_NEXT,
723 [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp",
724 [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp",
725 [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented",
726 [IP6_MAPT_NEXT_DROP] = "error-drop",
732 * fd.io coding-style-patch-verification: ON
735 * eval: (c-set-style "gnu")