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 implementation
20 #include <snat/nat64.h>
21 #include <snat/nat64_db.h>
22 #include <vnet/fib/ip4_fib.h>
25 nat64_main_t nat64_main;
29 /* Hook up input features */
30 VNET_FEATURE_INIT (nat64_in2out, static) = {
31 .arc_name = "ip6-unicast",
32 .node_name = "nat64-in2out",
33 .runs_before = VNET_FEATURES ("ip6-lookup"),
35 VNET_FEATURE_INIT (nat64_out2in, static) = {
36 .arc_name = "ip4-unicast",
37 .node_name = "nat64-out2in",
38 .runs_before = VNET_FEATURES ("ip4-lookup"),
44 nat64_init (vlib_main_t * vm)
46 nat64_main_t *nm = &nat64_main;
47 clib_error_t *error = 0;
48 vlib_thread_main_t *tm = vlib_get_thread_main ();
50 if (tm->n_vlib_mains > 1)
52 error = clib_error_return (0, "multi thread not supported");
56 if (nat64_db_init (&nm->db))
58 error = clib_error_return (0, "NAT64 DB init failed");
62 /* set session timeouts to default values */
63 nm->udp_timeout = SNAT_UDP_TIMEOUT;
64 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
65 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
66 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
67 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
74 nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
76 nat64_main_t *nm = &nat64_main;
77 snat_address_t *a = 0;
78 snat_interface_t *interface;
81 /* Check if address already exists */
82 for (i = 0; i < vec_len (nm->addr_pool); i++)
84 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
86 a = nm->addr_pool + i;
94 return VNET_API_ERROR_VALUE_EXIST;
96 vec_add2 (nm->addr_pool, a, 1);
98 a->fib_index = ip4_fib_index_from_table_id (vrf_id);
99 #define _(N, i, n, s) \
100 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535);
101 foreach_snat_protocol
107 return VNET_API_ERROR_NO_SUCH_ENTRY;
109 #define _(N, id, n, s) \
110 clib_bitmap_free (a->busy_##n##_port_bitmap);
111 foreach_snat_protocol
113 vec_del1 (nm->addr_pool, i);
116 /* Add/del external address to FIB */
118 pool_foreach (interface, nm->interfaces,
120 if (interface->is_inside)
123 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
132 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
134 nat64_main_t *nm = &nat64_main;
135 snat_address_t *a = 0;
138 vec_foreach (a, nm->addr_pool)
147 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
149 nat64_main_t *nm = &nat64_main;
150 snat_interface_t *interface = 0, *i;
152 const char *feature_name, *arc_name;
154 /* Check if address already exists */
156 pool_foreach (i, nm->interfaces,
158 if (i->sw_if_index == sw_if_index)
169 return VNET_API_ERROR_VALUE_EXIST;
171 pool_get (nm->interfaces, interface);
172 interface->sw_if_index = sw_if_index;
173 interface->is_inside = is_inside;
179 return VNET_API_ERROR_NO_SUCH_ENTRY;
181 pool_put (nm->interfaces, interface);
187 vec_foreach (ap, nm->addr_pool)
188 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
192 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
193 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
195 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
200 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
202 nat64_main_t *nm = &nat64_main;
203 snat_interface_t *i = 0;
206 pool_foreach (i, nm->interfaces,
215 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
216 ip4_address_t * addr, u16 * port)
218 nat64_main_t *nm = &nat64_main;
219 snat_main_t *sm = &snat_main;
224 for (i = 0; i < vec_len (nm->addr_pool); i++)
226 a = nm->addr_pool + i;
229 #define _(N, j, n, s) \
230 case SNAT_PROTOCOL_##N: \
231 if (a->busy_##n##_ports < (65535-1024)) \
235 portnum = random_u32 (&sm->random_seed); \
237 if (portnum < 1024) \
239 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
242 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
244 a->busy_##n##_ports++; \
246 addr->as_u32 = a->addr.as_u32; \
251 foreach_snat_protocol
254 clib_warning ("unknown protocol");
259 /* Totally out of translations to use... */
265 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
266 snat_protocol_t proto)
268 nat64_main_t *nm = &nat64_main;
272 for (i = 0; i < vec_len (nm->addr_pool); i++)
274 a = nm->addr_pool + i;
275 if (addr->as_u32 != a->addr.as_u32)
279 #define _(N, j, n, s) \
280 case SNAT_PROTOCOL_##N: \
281 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
283 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
284 a->busy_##n##_ports--; \
286 foreach_snat_protocol
289 clib_warning ("unknown protocol");
297 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
298 ip4_address_t * out_addr, u16 in_port,
299 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
301 nat64_main_t *nm = &nat64_main;
302 nat64_db_bib_entry_t *bibe;
304 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
305 snat_protocol_t p = ip_proto_to_snat_proto (proto);
310 addr.as_u64[0] = in_addr->as_u64[0];
311 addr.as_u64[1] = in_addr->as_u64[1];
313 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
319 return VNET_API_ERROR_VALUE_EXIST;
321 for (i = 0; i < vec_len (nm->addr_pool); i++)
323 a = nm->addr_pool + i;
324 if (out_addr->as_u32 != a->addr.as_u32)
328 #define _(N, j, n, s) \
329 case SNAT_PROTOCOL_##N: \
330 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
332 return VNET_API_ERROR_INVALID_VALUE; \
333 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
335 if (out_port > 1024) \
336 a->busy_##n##_ports++; \
338 foreach_snat_protocol
341 clib_warning ("unknown protocol");
342 return VNET_API_ERROR_INVALID_VALUE_2;
347 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
348 clib_host_to_net_u16 (in_port),
349 clib_host_to_net_u16 (out_port), fib_index,
352 return VNET_API_ERROR_UNSPECIFIED;
357 return VNET_API_ERROR_NO_SUCH_ENTRY;
359 nat64_free_out_addr_and_port (out_addr, out_port, p);
360 nat64_db_bib_entry_free (&nm->db, bibe);
367 nat64_set_udp_timeout (u32 timeout)
369 nat64_main_t *nm = &nat64_main;
372 nm->udp_timeout = SNAT_UDP_TIMEOUT;
373 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
374 return VNET_API_ERROR_INVALID_VALUE;
376 nm->udp_timeout = timeout;
382 nat64_get_udp_timeout (void)
384 nat64_main_t *nm = &nat64_main;
386 return nm->udp_timeout;
390 nat64_set_icmp_timeout (u32 timeout)
392 nat64_main_t *nm = &nat64_main;
395 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
397 nm->icmp_timeout = timeout;
403 nat64_get_icmp_timeout (void)
405 nat64_main_t *nm = &nat64_main;
407 return nm->icmp_timeout;
411 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
413 nat64_main_t *nm = &nat64_main;
416 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
418 nm->tcp_trans_timeout = trans;
421 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
423 nm->tcp_est_timeout = est;
425 if (incoming_syn == 0)
426 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
428 nm->tcp_incoming_syn_timeout = incoming_syn;
434 nat64_get_tcp_trans_timeout (void)
436 nat64_main_t *nm = &nat64_main;
438 return nm->tcp_trans_timeout;
442 nat64_get_tcp_est_timeout (void)
444 nat64_main_t *nm = &nat64_main;
446 return nm->tcp_est_timeout;
450 nat64_get_tcp_incoming_syn_timeout (void)
452 nat64_main_t *nm = &nat64_main;
454 return nm->tcp_incoming_syn_timeout;
458 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
460 nat64_main_t *nm = &nat64_main;
461 u32 now = (u32) vlib_time_now (vm);
465 case SNAT_PROTOCOL_ICMP:
466 ste->expire = now + nm->icmp_timeout;
468 case SNAT_PROTOCOL_TCP:
470 switch (ste->tcp_state)
472 case NAT64_TCP_STATE_V4_INIT:
473 case NAT64_TCP_STATE_V6_INIT:
474 case NAT64_TCP_STATE_V4_FIN_RCV:
475 case NAT64_TCP_STATE_V6_FIN_RCV:
476 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
477 case NAT64_TCP_STATE_TRANS:
478 ste->expire = now + nm->tcp_trans_timeout;
480 case NAT64_TCP_STATE_ESTABLISHED:
481 ste->expire = now + nm->tcp_est_timeout;
487 case SNAT_PROTOCOL_UDP:
488 ste->expire = now + nm->udp_timeout;
496 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
499 switch (ste->tcp_state)
501 case NAT64_TCP_STATE_CLOSED:
503 if (tcp->flags & TCP_FLAG_SYN)
506 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
508 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
512 case NAT64_TCP_STATE_V4_INIT:
514 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
515 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
518 case NAT64_TCP_STATE_V6_INIT:
520 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
521 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
524 case NAT64_TCP_STATE_ESTABLISHED:
526 if (tcp->flags & TCP_FLAG_FIN)
529 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
531 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
533 else if (tcp->flags & TCP_FLAG_RST)
535 ste->tcp_state = NAT64_TCP_STATE_TRANS;
539 case NAT64_TCP_STATE_V4_FIN_RCV:
541 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
542 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
545 case NAT64_TCP_STATE_V6_FIN_RCV:
547 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
548 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
551 case NAT64_TCP_STATE_TRANS:
553 if (!(tcp->flags & TCP_FLAG_RST))
554 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
563 * @brief The 'nat64-expire-walk' process's main loop.
565 * Check expire time for NAT64 sessions.
568 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
571 nat64_main_t *nm = &nat64_main;
575 vlib_process_wait_for_event_or_clock (vm, 10.0);
576 vlib_process_get_events (vm, NULL);
577 u32 now = (u32) vlib_time_now (vm);
579 nad64_db_st_free_expired (&nm->db, now);
585 static vlib_node_registration_t nat64_expire_walk_node;
588 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
589 .function = nat64_expire_walk_fn,
590 .type = VLIB_NODE_TYPE_PROCESS,
591 .name = "nat64-expire-walk",
596 * fd.io coding-style-patch-verification: ON
599 * eval: (c-set-style "gnu")