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/ip_frag.h>
18 #include <vnet/ip/ip6_to_ip4.h>
19 #include <vnet/ip/ip4_to_ip6.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;
54 static_always_inline int
55 ip6_map_fragment_cache (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
56 map_domain_t * d, u16 port)
59 map_ip4_reass_lock ();
60 map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address,
62 ip6_map_t_embedded_address (d,
65 frag_id_6to4 (frag->identification),
68 IP_PROTOCOL_ICMP : ip6->protocol,
73 map_ip4_reass_unlock ();
77 /* Returns the associated port or -1 */
78 static_always_inline i32
79 ip6_map_fragment_get (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
83 map_ip4_reass_lock ();
84 map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address,
86 ip6_map_t_embedded_address (d,
89 frag_id_6to4 (frag->identification),
92 IP_PROTOCOL_ICMP : ip6->protocol,
94 i32 ret = r ? r->port : -1;
95 map_ip4_reass_unlock ();
103 } icmp6_to_icmp_ctx_t;
106 ip6_to_ip4_set_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
108 icmp6_to_icmp_ctx_t *ctx = arg;
112 //Note that this prevents an intermediate IPv6 router from answering the request
113 ip4_sadr = map_get_ip4 (&ip6->src_address, ctx->d->flags);
114 if (ip6->src_address.as_u64[0] !=
115 map_get_pfx_net (ctx->d, ip4_sadr, ctx->sender_port)
116 || ip6->src_address.as_u64[1] != map_get_sfx_net (ctx->d, ip4_sadr,
120 ip4->dst_address.as_u32 =
121 ip6_map_t_embedded_address (ctx->d, &ip6->dst_address);
122 ip4->src_address.as_u32 = ip4_sadr;
128 ip6_to_ip4_set_inner_icmp_cb (ip6_header_t * ip6, ip4_header_t * ip4,
131 icmp6_to_icmp_ctx_t *ctx = arg;
134 //Security check of inner packet
135 inner_ip4_dadr = map_get_ip4 (&ip6->dst_address, ctx->d->flags);
136 if (ip6->dst_address.as_u64[0] !=
137 map_get_pfx_net (ctx->d, inner_ip4_dadr, ctx->sender_port)
138 || ip6->dst_address.as_u64[1] != map_get_sfx_net (ctx->d,
143 ip4->dst_address.as_u32 = inner_ip4_dadr;
144 ip4->src_address.as_u32 =
145 ip6_map_t_embedded_address (ctx->d, &ip6->src_address);
151 ip6_map_t_icmp (vlib_main_t * vm,
152 vlib_node_runtime_t * node, vlib_frame_t * frame)
154 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
155 vlib_node_runtime_t *error_node =
156 vlib_node_get_runtime (vm, ip6_map_t_icmp_node.index);
157 from = vlib_frame_vector_args (frame);
158 n_left_from = frame->n_vectors;
159 next_index = node->cached_next_index;
160 vlib_combined_counter_main_t *cm = map_main.domain_counters;
161 u32 thread_index = vm->thread_index;
163 while (n_left_from > 0)
165 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
167 while (n_left_from > 0 && n_left_to_next > 0)
172 ip6_mapt_icmp_next_t next0;
175 icmp6_to_icmp_ctx_t ctx0;
178 pi0 = to_next[0] = from[0];
183 error0 = MAP_ERROR_NONE;
184 next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP;
186 p0 = vlib_get_buffer (vm, pi0);
187 ip60 = vlib_buffer_get_current (p0);
188 len0 = clib_net_to_host_u16 (ip60->payload_length);
190 pool_elt_at_index (map_main.domains,
191 vnet_buffer (p0)->map_t.map_domain_index);
192 ctx0.sender_port = ip6_get_port (ip60, 0, p0->current_length);
194 if (ctx0.sender_port == 0)
196 // In case of 1:1 mapping, we don't care about the port
197 if (!(d0->ea_bits_len == 0 && d0->rules))
199 error0 = MAP_ERROR_ICMP;
205 (p0, ip6_to_ip4_set_icmp_cb, &ctx0,
206 ip6_to_ip4_set_inner_icmp_cb, &ctx0))
208 error0 = MAP_ERROR_ICMP;
212 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
214 //Send to fragmentation node if necessary
215 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
216 vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
217 next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
220 if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
222 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
225 map_t.map_domain_index, 1,
230 next0 = IP6_MAPT_ICMP_NEXT_DROP;
233 p0->error = error_node->errors[error0];
234 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
235 to_next, n_left_to_next, pi0,
238 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
240 return frame->n_vectors;
244 ip6_to_ip4_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *ctx)
246 vlib_buffer_t *p = ctx;
248 ip4->dst_address.as_u32 = vnet_buffer (p)->map_t.v6.daddr;
249 ip4->src_address.as_u32 = vnet_buffer (p)->map_t.v6.saddr;
255 ip6_map_t_fragmented (vlib_main_t * vm,
256 vlib_node_runtime_t * node, vlib_frame_t * frame)
258 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
259 from = vlib_frame_vector_args (frame);
260 n_left_from = frame->n_vectors;
261 next_index = node->cached_next_index;
262 vlib_node_runtime_t *error_node =
263 vlib_node_get_runtime (vm, ip6_map_t_fragmented_node.index);
265 while (n_left_from > 0)
267 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
269 while (n_left_from > 0 && n_left_to_next > 0)
275 pi0 = to_next[0] = from[0];
281 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
282 p0 = vlib_get_buffer (vm, pi0);
284 if (ip6_to_ip4_fragmented (p0, ip6_to_ip4_set_cb, p0))
286 p0->error = error_node->errors[MAP_ERROR_FRAGMENT_DROPPED];
287 next0 = IP6_MAPT_FRAGMENTED_NEXT_DROP;
291 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
293 //Send to fragmentation node if necessary
294 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
295 vnet_buffer (p0)->ip_frag.next_index =
296 IP4_FRAG_NEXT_IP4_LOOKUP;
297 next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
301 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
302 to_next, n_left_to_next, pi0,
305 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307 return frame->n_vectors;
311 ip6_map_t_tcp_udp (vlib_main_t * vm,
312 vlib_node_runtime_t * node, vlib_frame_t * frame)
314 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
315 vlib_node_runtime_t *error_node =
316 vlib_node_get_runtime (vm, ip6_map_t_tcp_udp_node.index);
318 from = vlib_frame_vector_args (frame);
319 n_left_from = frame->n_vectors;
320 next_index = node->cached_next_index;
321 while (n_left_from > 0)
323 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
325 while (n_left_from > 0 && n_left_to_next > 0)
329 ip6_mapt_tcp_udp_next_t next0;
331 pi0 = to_next[0] = from[0];
336 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
338 p0 = vlib_get_buffer (vm, pi0);
340 if (ip6_to_ip4_tcp_udp (p0, ip6_to_ip4_set_cb, p0, 1))
342 p0->error = error_node->errors[MAP_ERROR_UNKNOWN];
343 next0 = IP6_MAPT_TCP_UDP_NEXT_DROP;
347 if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
349 //Send to fragmentation node if necessary
350 vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
351 vnet_buffer (p0)->ip_frag.next_index =
352 IP4_FRAG_NEXT_IP4_LOOKUP;
353 next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
357 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
358 to_next, n_left_to_next, pi0,
361 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
363 return frame->n_vectors;
367 ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
369 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
370 vlib_node_runtime_t *error_node =
371 vlib_node_get_runtime (vm, ip6_map_t_node.index);
372 vlib_combined_counter_main_t *cm = map_main.domain_counters;
373 u32 thread_index = vm->thread_index;
375 from = vlib_frame_vector_args (frame);
376 n_left_from = frame->n_vectors;
377 next_index = node->cached_next_index;
378 while (n_left_from > 0)
380 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
382 while (n_left_from > 0 && n_left_to_next > 0)
391 ip6_frag_hdr_t *frag0;
392 ip6_mapt_next_t next0 = 0;
395 pi0 = to_next[0] = from[0];
400 error0 = MAP_ERROR_NONE;
402 p0 = vlib_get_buffer (vm, pi0);
403 ip60 = vlib_buffer_get_current (p0);
405 //Save saddr in a different variable to not overwrite ip.adj_index
406 saddr = 0; /* TODO */
407 /* NOTE: ip6_map_get_domain currently doesn't utilize second argument */
409 d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
410 (ip4_address_t *) & saddr,
411 &vnet_buffer (p0)->map_t.map_domain_index,
414 saddr = map_get_ip4 (&ip60->src_address, d0->flags);
416 //FIXME: What if d0 is null
417 vnet_buffer (p0)->map_t.v6.saddr = saddr;
418 vnet_buffer (p0)->map_t.v6.daddr =
419 ip6_map_t_embedded_address (d0, &ip60->dst_address);
420 vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
422 if (PREDICT_FALSE (ip6_parse (ip60, p0->current_length,
423 &(vnet_buffer (p0)->map_t.
425 &(vnet_buffer (p0)->map_t.
427 &(vnet_buffer (p0)->map_t.
430 error0 = MAP_ERROR_MALFORMED;
431 next0 = IP6_MAPT_NEXT_DROP;
435 l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
436 sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
438 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
439 vnet_buffer (p0)->map_t.
443 if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
444 ip6_frag_hdr_offset (frag0)))
446 src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
447 error0 = (src_port0 != -1) ? error0 : MAP_ERROR_FRAGMENT_MEMORY;
448 next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
452 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
456 sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : error0;
457 vnet_buffer (p0)->map_t.checksum_offset =
458 vnet_buffer (p0)->map_t.v6.l4_offset + 16;
459 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
463 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
467 (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
471 sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : error0;
472 vnet_buffer (p0)->map_t.checksum_offset =
473 vnet_buffer (p0)->map_t.v6.l4_offset + 6;
474 next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
478 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
480 else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
485 sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : error0;
486 next0 = IP6_MAPT_NEXT_MAPT_ICMP;
487 if (((icmp46_header_t *)
489 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
491 || ((icmp46_header_t *)
493 vnet_buffer (p0)->map_t.v6.
494 l4_offset))->code == ICMP6_echo_request)
499 vnet_buffer (p0)->map_t.v6.l4_offset + 6));
503 //TODO: In case of 1:1 mapping, it might be possible to do something with those packets.
504 error0 = MAP_ERROR_BAD_PROTOCOL;
510 && (ip60->src_address.as_u64[0] !=
511 map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
513 || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
515 (p0)->map_t.v6.saddr,
518 //Security check when src_port0 is not zero (non-first fragment, UDP or TCP)
519 error0 = MAP_ERROR_SEC_CHECK;
522 //Fragmented first packet needs to be cached for following packets
523 if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
524 !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
529 && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
530 && (error0 == MAP_ERROR_NONE))
532 ip6_map_fragment_cache (ip60,
533 (ip6_frag_hdr_t *) u8_ptr_add (ip60,
541 (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
543 vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
546 map_t.map_domain_index, 1,
548 (ip60->payload_length));
551 next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
552 p0->error = error_node->errors[error0];
553 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
554 to_next, n_left_to_next, pi0,
557 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
559 return frame->n_vectors;
562 static char *map_t_error_strings[] = {
563 #define _(sym,string) string,
569 VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = {
570 .function = ip6_map_t_fragmented,
571 .name = "ip6-map-t-fragmented",
572 .vector_size = sizeof (u32),
573 .format_trace = format_map_trace,
574 .type = VLIB_NODE_TYPE_INTERNAL,
576 .n_errors = MAP_N_ERROR,
577 .error_strings = map_t_error_strings,
579 .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT,
581 [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup",
582 [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
583 [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop",
589 VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = {
590 .function = ip6_map_t_icmp,
591 .name = "ip6-map-t-icmp",
592 .vector_size = sizeof (u32),
593 .format_trace = format_map_trace,
594 .type = VLIB_NODE_TYPE_INTERNAL,
596 .n_errors = MAP_N_ERROR,
597 .error_strings = map_t_error_strings,
599 .n_next_nodes = IP6_MAPT_ICMP_N_NEXT,
601 [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup",
602 [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
603 [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop",
609 VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = {
610 .function = ip6_map_t_tcp_udp,
611 .name = "ip6-map-t-tcp-udp",
612 .vector_size = sizeof (u32),
613 .format_trace = format_map_trace,
614 .type = VLIB_NODE_TYPE_INTERNAL,
616 .n_errors = MAP_N_ERROR,
617 .error_strings = map_t_error_strings,
619 .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT,
621 [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup",
622 [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
623 [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop",
629 VLIB_REGISTER_NODE(ip6_map_t_node) = {
630 .function = ip6_map_t,
632 .vector_size = sizeof(u32),
633 .format_trace = format_map_trace,
634 .type = VLIB_NODE_TYPE_INTERNAL,
636 .n_errors = MAP_N_ERROR,
637 .error_strings = map_t_error_strings,
639 .n_next_nodes = IP6_MAPT_N_NEXT,
641 [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp",
642 [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp",
643 [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented",
644 [IP6_MAPT_NEXT_DROP] = "error-drop",
650 * fd.io coding-style-patch-verification: ON
653 * eval: (c-set-style "gnu")