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>
28 } nat64_out2in_trace_t;
31 format_nat64_out2in_trace (u8 * s, va_list * args)
33 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35 nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
38 format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
44 vlib_node_registration_t nat64_out2in_node;
46 #define foreach_nat64_out2in_error \
47 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
48 _(OUT2IN_PACKETS, "Good out2in packets processed") \
49 _(NO_TRANSLATION, "No translation") \
54 #define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
55 foreach_nat64_out2in_error
58 } nat64_out2in_error_t;
60 static char *nat64_out2in_error_strings[] = {
61 #define _(sym,string) string,
62 foreach_nat64_out2in_error
68 NAT64_OUT2IN_NEXT_LOOKUP,
69 NAT64_OUT2IN_NEXT_DROP,
71 } nat64_out2in_next_t;
73 typedef struct nat64_out2in_set_ctx_t_
77 } nat64_out2in_set_ctx_t;
80 nat64_out2in_tcp_udp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
83 nat64_main_t *nm = &nat64_main;
84 nat64_out2in_set_ctx_t *ctx = arg;
85 nat64_db_bib_entry_t *bibe;
86 nat64_db_st_entry_t *ste;
87 ip46_address_t saddr, daddr;
88 ip6_address_t ip6_saddr;
89 udp_header_t *udp = ip4_next_header (ip4);
90 tcp_header_t *tcp = ip4_next_header (ip4);
91 snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
92 u16 dport = udp->dst_port;
93 u16 sport = udp->src_port;
94 u32 sw_if_index, fib_index;
98 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
99 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
101 memset (&saddr, 0, sizeof (saddr));
102 saddr.ip4.as_u32 = ip4->src_address.as_u32;
103 memset (&daddr, 0, sizeof (daddr));
104 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
107 nat64_db_st_entry_find (&nm->db, &daddr, &saddr, dport, sport, proto,
111 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
118 nat64_db_bib_entry_find (&nm->db, &daddr, dport, proto, fib_index, 0);
123 nat64_compose_ip6 (&ip6_saddr, &ip4->src_address, bibe->fib_index);
125 nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
129 nat64_session_reset_timeout (ste, ctx->vm);
131 ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
132 ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
134 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
135 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
136 udp->dst_port = bibe->in_port;
138 if (proto == SNAT_PROTOCOL_UDP)
139 checksum = &udp->checksum;
141 checksum = &tcp->checksum;
142 csum = ip_csum_sub_even (*checksum, dport);
143 csum = ip_csum_add_even (csum, udp->dst_port);
144 *checksum = ip_csum_fold (csum);
146 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
152 nat64_out2in_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg)
154 nat64_main_t *nm = &nat64_main;
155 nat64_out2in_set_ctx_t *ctx = arg;
156 nat64_db_bib_entry_t *bibe;
157 nat64_db_st_entry_t *ste;
158 ip46_address_t saddr, daddr;
159 ip6_address_t ip6_saddr;
160 u32 sw_if_index, fib_index;
161 icmp46_header_t *icmp = ip4_next_header (ip4);
163 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
164 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
166 memset (&saddr, 0, sizeof (saddr));
167 saddr.ip4.as_u32 = ip4->src_address.as_u32;
168 memset (&daddr, 0, sizeof (daddr));
169 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
171 if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
173 u16 out_id = ((u16 *) (icmp))[2];
175 nat64_db_st_entry_find (&nm->db, &daddr, &saddr, out_id, 0,
176 SNAT_PROTOCOL_ICMP, fib_index, 0);
181 nat64_db_bib_entry_by_index (&nm->db, SNAT_PROTOCOL_ICMP,
189 nat64_db_bib_entry_find (&nm->db, &daddr, out_id,
190 SNAT_PROTOCOL_ICMP, fib_index, 0);
194 nat64_compose_ip6 (&ip6_saddr, &ip4->src_address, bibe->fib_index);
196 nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
200 nat64_session_reset_timeout (ste, ctx->vm);
202 ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
203 ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
205 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
206 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
207 ((u16 *) (icmp))[2] = bibe->in_port;
209 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
213 ip6_header_t *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
215 nat64_compose_ip6 (&ip6->src_address, &ip4->src_address,
216 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX]);
217 ip6->dst_address.as_u64[0] = inner_ip6->src_address.as_u64[0];
218 ip6->dst_address.as_u64[1] = inner_ip6->src_address.as_u64[1];
225 nat64_out2in_inner_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
228 nat64_main_t *nm = &nat64_main;
229 nat64_out2in_set_ctx_t *ctx = arg;
230 nat64_db_bib_entry_t *bibe;
231 nat64_db_st_entry_t *ste;
232 ip46_address_t saddr, daddr;
233 u32 sw_if_index, fib_index;
234 snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
236 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
238 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
240 memset (&saddr, 0, sizeof (saddr));
241 saddr.ip4.as_u32 = ip4->src_address.as_u32;
242 memset (&daddr, 0, sizeof (daddr));
243 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
245 if (proto == SNAT_PROTOCOL_ICMP)
247 icmp46_header_t *icmp = ip4_next_header (ip4);
248 u16 out_id = ((u16 *) (icmp))[2];
251 (icmp->type == ICMP6_echo_request
252 || icmp->type == ICMP6_echo_reply))
256 nat64_db_st_entry_find (&nm->db, &saddr, &daddr, out_id, 0, proto,
261 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
265 ip6->dst_address.as_u64[0] = ste->in_r_addr.as_u64[0];
266 ip6->dst_address.as_u64[1] = ste->in_r_addr.as_u64[1];
267 ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
268 ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
269 ((u16 *) (icmp))[2] = bibe->in_port;
271 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
275 udp_header_t *udp = ip4_next_header (ip4);
276 tcp_header_t *tcp = ip4_next_header (ip4);
277 u16 dport = udp->dst_port;
278 u16 sport = udp->src_port;
283 nat64_db_st_entry_find (&nm->db, &saddr, &daddr, sport, dport, proto,
288 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
292 nat64_compose_ip6 (&ip6->dst_address, &daddr.ip4, bibe->fib_index);
293 ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
294 ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
295 udp->src_port = bibe->in_port;
297 if (proto == SNAT_PROTOCOL_UDP)
298 checksum = &udp->checksum;
300 checksum = &tcp->checksum;
303 csum = ip_csum_sub_even (*checksum, sport);
304 csum = ip_csum_add_even (csum, udp->src_port);
305 *checksum = ip_csum_fold (csum);
308 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
315 nat64_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
316 vlib_frame_t * frame)
318 u32 n_left_from, *from, *to_next;
319 nat64_out2in_next_t next_index;
320 u32 pkts_processed = 0;
322 from = vlib_frame_vector_args (frame);
323 n_left_from = frame->n_vectors;
324 next_index = node->cached_next_index;
325 while (n_left_from > 0)
329 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
331 while (n_left_from > 0 && n_left_to_next > 0)
338 nat64_out2in_set_ctx_t ctx0;
340 /* speculatively enqueue b0 to the current next frame */
348 b0 = vlib_get_buffer (vm, bi0);
349 ip40 = vlib_buffer_get_current (b0);
354 next0 = NAT64_OUT2IN_NEXT_LOOKUP;
356 proto0 = ip_proto_to_snat_proto (ip40->protocol);
357 if (PREDICT_FALSE (proto0 == ~0))
359 next0 = NAT64_OUT2IN_NEXT_DROP;
361 node->errors[NAT64_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
365 if (proto0 == SNAT_PROTOCOL_ICMP)
368 (b0, nat64_out2in_icmp_set_cb, &ctx0,
369 nat64_out2in_inner_icmp_set_cb, &ctx0))
371 next0 = NAT64_OUT2IN_NEXT_DROP;
372 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
378 if (ip4_to_ip6_tcp_udp (b0, nat64_out2in_tcp_udp_set_cb, &ctx0))
380 next0 = NAT64_OUT2IN_NEXT_DROP;
381 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
387 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
388 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
390 nat64_out2in_trace_t *t =
391 vlib_add_trace (vm, node, b0, sizeof (*t));
392 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
393 t->next_index = next0;
396 pkts_processed += next0 != NAT64_OUT2IN_NEXT_DROP;
398 /* verify speculative enqueue, maybe switch current next frame */
399 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
400 n_left_to_next, bi0, next0);
402 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
404 vlib_node_increment_counter (vm, nat64_out2in_node.index,
405 NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
407 return frame->n_vectors;
411 VLIB_REGISTER_NODE (nat64_out2in_node) = {
412 .function = nat64_out2in_node_fn,
413 .name = "nat64-out2in",
414 .vector_size = sizeof (u32),
415 .format_trace = format_nat64_out2in_trace,
416 .type = VLIB_NODE_TYPE_INTERNAL,
417 .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
418 .error_strings = nat64_out2in_error_strings,.n_next_nodes = 2,
419 /* edit / add dispositions here */
421 [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
422 [NAT64_OUT2IN_NEXT_LOOKUP] = "ip6-lookup",
427 VLIB_NODE_FUNCTION_MULTIARCH (nat64_out2in_node, nat64_out2in_node_fn);
430 * fd.io coding-style-patch-verification: ON
433 * eval: (c-set-style "gnu")