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
36 } nat64_out2in_trace_t;
39 format_nat64_out2in_trace (u8 * s, va_list * args)
41 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
42 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
43 nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
46 format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
52 vlib_node_registration_t nat64_out2in_node;
54 #define foreach_nat64_out2in_error \
55 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
56 _(OUT2IN_PACKETS, "Good out2in packets processed") \
57 _(NO_TRANSLATION, "No translation") \
62 #define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
63 foreach_nat64_out2in_error
66 } nat64_out2in_error_t;
68 static char *nat64_out2in_error_strings[] = {
69 #define _(sym,string) string,
70 foreach_nat64_out2in_error
76 NAT64_OUT2IN_NEXT_LOOKUP,
77 NAT64_OUT2IN_NEXT_DROP,
79 } nat64_out2in_next_t;
81 typedef struct nat64_out2in_set_ctx_t_
85 } nat64_out2in_set_ctx_t;
88 nat64_out2in_tcp_udp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
91 nat64_main_t *nm = &nat64_main;
92 nat64_out2in_set_ctx_t *ctx = arg;
93 nat64_db_bib_entry_t *bibe;
94 nat64_db_st_entry_t *ste;
95 ip46_address_t saddr, daddr;
96 ip6_address_t ip6_saddr;
97 udp_header_t *udp = ip4_next_header (ip4);
98 snat_protocol_t proto = ip_proto_to_snat_proto (ip4->protocol);
99 u16 dport = udp->dst_port;
100 u16 sport = udp->src_port;
101 u32 sw_if_index, fib_index;
103 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
104 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
106 memset (&saddr, 0, sizeof (saddr));
107 saddr.ip4.as_u32 = ip4->src_address.as_u32;
108 memset (&daddr, 0, sizeof (daddr));
109 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
111 memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
112 ip6_saddr.as_u32[3] = ip4->src_address.as_u32;
115 nat64_db_st_entry_find (&nm->db, &daddr, &saddr, dport, sport, proto,
119 bibe = nat64_db_bib_entry_by_index (&nm->db, proto, ste->bibe_index);
126 nat64_db_bib_entry_find (&nm->db, &daddr, dport, proto, fib_index, 0);
132 nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
136 nat64_session_reset_timeout (ste, ctx->vm);
138 ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
139 ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
141 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
142 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
143 udp->dst_port = bibe->in_port;
149 nat64_out2in_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6, void *arg)
151 nat64_main_t *nm = &nat64_main;
152 nat64_out2in_set_ctx_t *ctx = arg;
153 nat64_db_bib_entry_t *bibe;
154 nat64_db_st_entry_t *ste;
155 ip46_address_t saddr, daddr;
156 ip6_address_t ip6_saddr;
157 u32 sw_if_index, fib_index;
158 icmp46_header_t *icmp = ip4_next_header (ip4);
160 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
161 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
163 memset (&saddr, 0, sizeof (saddr));
164 saddr.ip4.as_u32 = ip4->src_address.as_u32;
165 memset (&daddr, 0, sizeof (daddr));
166 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
168 memcpy (&ip6_saddr, well_known_prefix, sizeof (ip6_saddr));
169 ip6_saddr.as_u32[3] = ip4->src_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);
195 nat64_db_st_entry_create (&nm->db, bibe, &ip6_saddr, &saddr.ip4,
199 nat64_session_reset_timeout (ste, ctx->vm);
201 ip6->src_address.as_u64[0] = ip6_saddr.as_u64[0];
202 ip6->src_address.as_u64[1] = ip6_saddr.as_u64[1];
204 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
205 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
206 ((u16 *) (icmp))[2] = bibe->in_port;
212 clib_warning ("not ICMP echo request/reply, %u", icmp->type);
220 nat64_out2in_inner_icmp_set_cb (ip4_header_t * ip4, ip6_header_t * ip6,
228 nat64_out2in_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
229 vlib_frame_t * frame)
231 u32 n_left_from, *from, *to_next;
232 nat64_out2in_next_t next_index;
233 u32 pkts_processed = 0;
235 from = vlib_frame_vector_args (frame);
236 n_left_from = frame->n_vectors;
237 next_index = node->cached_next_index;
238 while (n_left_from > 0)
242 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
244 while (n_left_from > 0 && n_left_to_next > 0)
251 nat64_out2in_set_ctx_t ctx0;
253 /* speculatively enqueue b0 to the current next frame */
261 b0 = vlib_get_buffer (vm, bi0);
262 ip40 = vlib_buffer_get_current (b0);
267 next0 = NAT64_OUT2IN_NEXT_LOOKUP;
269 proto0 = ip_proto_to_snat_proto (ip40->protocol);
270 if (PREDICT_FALSE (proto0 == ~0))
272 next0 = NAT64_OUT2IN_NEXT_DROP;
274 node->errors[NAT64_OUT2IN_ERROR_UNSUPPORTED_PROTOCOL];
278 if (proto0 == SNAT_PROTOCOL_ICMP)
281 (b0, nat64_out2in_icmp_set_cb, &ctx0,
282 nat64_out2in_inner_icmp_set_cb, &ctx0))
284 next0 = NAT64_OUT2IN_NEXT_DROP;
285 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
291 if (ip4_to_ip6_tcp_udp (b0, nat64_out2in_tcp_udp_set_cb, &ctx0))
293 next0 = NAT64_OUT2IN_NEXT_DROP;
294 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
300 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
301 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
303 nat64_out2in_trace_t *t =
304 vlib_add_trace (vm, node, b0, sizeof (*t));
305 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
306 t->next_index = next0;
309 pkts_processed += next0 != NAT64_OUT2IN_NEXT_DROP;
311 /* verify speculative enqueue, maybe switch current next frame */
312 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
313 n_left_to_next, bi0, next0);
315 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
317 vlib_node_increment_counter (vm, nat64_out2in_node.index,
318 NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
320 return frame->n_vectors;
324 VLIB_REGISTER_NODE (nat64_out2in_node) = {
325 .function = nat64_out2in_node_fn,
326 .name = "nat64-out2in",
327 .vector_size = sizeof (u32),
328 .format_trace = format_nat64_out2in_trace,
329 .type = VLIB_NODE_TYPE_INTERNAL,
330 .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
331 .error_strings = nat64_out2in_error_strings,.n_next_nodes = 2,
332 /* edit / add dispositions here */
334 [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
335 [NAT64_OUT2IN_NEXT_LOOKUP] = "ip6-lookup",
340 VLIB_NODE_FUNCTION_MULTIARCH (nat64_out2in_node, nat64_out2in_node_fn);
343 * fd.io coding-style-patch-verification: ON
346 * eval: (c-set-style "gnu")