2 * Copyright (c) 2017 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 * @brief NAT64 IPv4 to IPv6 translation (otside to inside network)
20 #include <snat/nat64.h>
21 #include <vnet/ip/ip4_to_ip6.h>
22 #include <vnet/fib/ip4_fib.h>
25 static u8 well_known_prefix[] = {
26 0x00, 0x64, 0xff, 0x9b,
27 0x00, 0x00, 0x00, 0x00,
28 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00
37 } nat64_out2in_trace_t;
40 format_nat64_out2in_trace (u8 * s, va_list * args)
42 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44 nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
47 format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
53 vlib_node_registration_t nat64_out2in_node;
55 #define foreach_nat64_out2in_error \
56 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
57 _(OUT2IN_PACKETS, "Good out2in packets processed") \
58 _(NO_TRANSLATION, "No translation") \
63 #define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
64 foreach_nat64_out2in_error
67 } nat64_out2in_error_t;
69 static char *nat64_out2in_error_strings[] = {
70 #define _(sym,string) string,
71 foreach_nat64_out2in_error
77 NAT64_OUT2IN_NEXT_LOOKUP,
78 NAT64_OUT2IN_NEXT_DROP,
80 } nat64_out2in_next_t;
82 typedef struct nat64_out2in_set_ctx_t_
86 } nat64_out2in_set_ctx_t;
89 nat64_out2in_tcp_udp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
92 nat64_main_t *nm = &nat64_main;
93 nat64_out2in_set_ctx_t *ctx = arg;
94 nat64_db_bib_entry_t *bibe;
95 nat64_db_st_entry_t *ste;
96 ip46_address_t saddr, daddr;
97 ip6_address_t ip6_saddr;
98 udp_header_t *udp = ip4_next_header (ip4);
99 tcp_header_t *tcp = ip4_next_header (ip4);
100 snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
101 u16 dport = udp->dst_port;
102 u16 sport = udp->src_port;
103 u32 sw_if_index, fib_index;
107 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
108 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
110 memset (&saddr, 0, sizeof (saddr));
111 saddr.ip4.as_u32 = ip4->src_address.as_u32;
112 memset (&daddr, 0, sizeof (daddr));
113 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
115 memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
116 ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
119 nat64_db_st_entry_find (&nm->db, &daddr, &saddr, dport, sport, proto,
123 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
130 nat64_db_bib_entry_find (&nm->db, &daddr, dport, proto, fib_index, 0);
136 nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
140 nat64_session_reset_timeout (ste, ctx->vm);
142 ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
143 ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
145 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
146 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
147 udp->dst_port = bibe->in_port;
149 if (proto == SNAT_PROTOCOL_UDP)
150 checksum = &udp->checksum;
152 checksum = &tcp->checksum;
153 csum = ip_csum_sub_even (*checksum, dport);
154 csum = ip_csum_add_even (csum, udp->dst_port);
155 *checksum = ip_csum_fold (csum);
157 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
163 nat64_out2in_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg)
165 nat64_main_t *nm = &nat64_main;
166 nat64_out2in_set_ctx_t *ctx = arg;
167 nat64_db_bib_entry_t *bibe;
168 nat64_db_st_entry_t *ste;
169 ip46_address_t saddr, daddr;
170 ip6_address_t ip6_saddr;
171 u32 sw_if_index, fib_index;
172 icmp46_header_t *icmp = ip4_next_header (ip4);
174 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
175 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
177 memset (&saddr, 0, sizeof (saddr));
178 saddr.ip4.as_u32 = ip4->src_address.as_u32;
179 memset (&daddr, 0, sizeof (daddr));
180 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
182 memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
183 ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
185 if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
187 u16 out_id = ((u16 *) (icmp))[2];
189 nat64_db_st_entry_find (&nm->db, &daddr, &saddr, out_id, 0,
190 SNAT_PROTOCOL_ICMP, fib_index, 0);
195 nat64_db_bib_entry_by_index (&nm->db, SNAT_PROTOCOL_ICMP,
203 nat64_db_bib_entry_find (&nm->db, &daddr, out_id,
204 SNAT_PROTOCOL_ICMP, fib_index, 0);
209 nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
213 nat64_session_reset_timeout (ste, ctx->vm);
215 ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
216 ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
218 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
219 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
220 ((u16 *) (icmp))[2] = bibe->in_port;
222 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
226 ip6_header_t *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
228 ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
229 ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
230 ip6->dst_address.as_u64[0] = inner_ip6->src_address.as_u64[0];
231 ip6->dst_address.as_u64[1] = inner_ip6->src_address.as_u64[1];
238 nat64_out2in_inner_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
241 nat64_main_t *nm = &nat64_main;
242 nat64_out2in_set_ctx_t *ctx = arg;
243 nat64_db_bib_entry_t *bibe;
244 nat64_db_st_entry_t *ste;
245 ip46_address_t saddr, daddr;
246 ip6_address_t ip6_daddr;
247 u32 sw_if_index, fib_index;
248 snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
250 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
252 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
254 memset (&saddr, 0, sizeof (saddr));
255 saddr.ip4.as_u32 = ip4->src_address.as_u32;
256 memset (&daddr, 0, sizeof (daddr));
257 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
259 memcpy (&ip6_daddr, well_known_prefix, sizeof (ip6_daddr));
260 ip6_daddr.as_u32[3] = ip4->dst_address.as_u32;
262 if (proto == SNAT_PROTOCOL_ICMP)
264 icmp46_header_t *icmp = ip4_next_header (ip4);
265 u16 out_id = ((u16 *) (icmp))[2];
268 (icmp->type == ICMP6_echo_request
269 || icmp->type == ICMP6_echo_reply))
273 nat64_db_st_entry_find (&nm->db, &saddr, &daddr, out_id, 0, proto,
278 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
282 ip6->dst_address.as_u64[0] = ip6_daddr.as_u64[0];
283 ip6->dst_address.as_u64[1] = ip6_daddr.as_u64[1];
284 ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
285 ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
286 ((u16 *) (icmp))[2] = bibe->in_port;
288 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
292 udp_header_t *udp = ip4_next_header (ip4);
293 tcp_header_t *tcp = ip4_next_header (ip4);
294 u16 dport = udp->dst_port;
295 u16 sport = udp->src_port;
300 nat64_db_st_entry_find (&nm->db, &saddr, &daddr, sport, dport, proto,
305 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
309 ip6->dst_address.as_u64[0] = ip6_daddr.as_u64[0];
310 ip6->dst_address.as_u64[1] = ip6_daddr.as_u64[1];
311 ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
312 ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
313 udp->src_port = bibe->in_port;
315 if (proto == SNAT_PROTOCOL_UDP)
316 checksum = &udp->checksum;
318 checksum = &tcp->checksum;
321 csum = ip_csum_sub_even (*checksum, sport);
322 csum = ip_csum_add_even (csum, udp->src_port);
323 *checksum = ip_csum_fold (csum);
326 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
333 nat64_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
334 vlib_frame_t * frame)
336 u32 n_left_from, *from, *to_next;
337 nat64_out2in_next_t next_index;
338 u32 pkts_processed = 0;
340 from = vlib_frame_vector_args (frame);
341 n_left_from = frame->n_vectors;
342 next_index = node->cached_next_index;
343 while (n_left_from > 0)
347 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
349 while (n_left_from > 0 && n_left_to_next > 0)
356 nat64_out2in_set_ctx_t ctx0;
358 /* speculatively enqueue b0 to the current next frame */
366 b0 = vlib_get_buffer (vm, bi0);
367 ip40 = vlib_buffer_get_current (b0);
372 next0 = NAT64_OUT2IN_NEXT_LOOKUP;
374 proto0 = ip_proto_to_snat_proto (ip40->protocol);
375 if (PREDICT_FALSE (proto0 == ~0))
377 next0 = NAT64_OUT2IN_NEXT_DROP;
379 node->errors[NAT64_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
383 if (proto0 == SNAT_PROTOCOL_ICMP)
386 (b0, nat64_out2in_icmp_set_cb, &ctx0,
387 nat64_out2in_inner_icmp_set_cb, &ctx0))
389 next0 = NAT64_OUT2IN_NEXT_DROP;
390 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
396 if (ip4_to_ip6_tcp_udp (b0, nat64_out2in_tcp_udp_set_cb, &ctx0))
398 next0 = NAT64_OUT2IN_NEXT_DROP;
399 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
405 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
406 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
408 nat64_out2in_trace_t *t =
409 vlib_add_trace (vm, node, b0, sizeof (*t));
410 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
411 t->next_index = next0;
414 pkts_processed += next0 != NAT64_OUT2IN_NEXT_DROP;
416 /* verify speculative enqueue, maybe switch current next frame */
417 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
418 n_left_to_next, bi0, next0);
420 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
422 vlib_node_increment_counter (vm, nat64_out2in_node.index,
423 NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
425 return frame->n_vectors;
429 VLIB_REGISTER_NODE (nat64_out2in_node) = {
430 .function = nat64_out2in_node_fn,
431 .name = "nat64-out2in",
432 .vector_size = sizeof (u32),
433 .format_trace = format_nat64_out2in_trace,
434 .type = VLIB_NODE_TYPE_INTERNAL,
435 .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
436 .error_strings = nat64_out2in_error_strings,.n_next_nodes = 2,
437 /* edit / add dispositions here */
439 [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
440 [NAT64_OUT2IN_NEXT_LOOKUP] = "ip6-lookup",
445 VLIB_NODE_FUNCTION_MULTIARCH (nat64_out2in_node, nat64_out2in_node_fn);
448 * fd.io coding-style-patch-verification: ON
451 * eval: (c-set-style "gnu")