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);
101 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
102 #define _(N, i, n, s) \
103 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535);
104 foreach_snat_protocol
110 return VNET_API_ERROR_NO_SUCH_ENTRY;
112 #define _(N, id, n, s) \
113 clib_bitmap_free (a->busy_##n##_port_bitmap);
114 foreach_snat_protocol
116 vec_del1 (nm->addr_pool, i);
119 /* Add/del external address to FIB */
121 pool_foreach (interface, nm->interfaces,
123 if (interface->is_inside)
126 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
135 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
137 nat64_main_t *nm = &nat64_main;
138 snat_address_t *a = 0;
141 vec_foreach (a, nm->addr_pool)
150 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
152 nat64_main_t *nm = &nat64_main;
153 snat_interface_t *interface = 0, *i;
155 const char *feature_name, *arc_name;
157 /* Check if address already exists */
159 pool_foreach (i, nm->interfaces,
161 if (i->sw_if_index == sw_if_index)
172 return VNET_API_ERROR_VALUE_EXIST;
174 pool_get (nm->interfaces, interface);
175 interface->sw_if_index = sw_if_index;
176 interface->is_inside = is_inside;
182 return VNET_API_ERROR_NO_SUCH_ENTRY;
184 pool_put (nm->interfaces, interface);
190 vec_foreach (ap, nm->addr_pool)
191 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
195 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
196 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
198 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
203 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
205 nat64_main_t *nm = &nat64_main;
206 snat_interface_t *i = 0;
209 pool_foreach (i, nm->interfaces,
218 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
219 ip4_address_t * addr, u16 * port)
221 nat64_main_t *nm = &nat64_main;
222 snat_main_t *sm = &snat_main;
224 snat_address_t *a, *ga = 0;
227 for (i = 0; i < vec_len (nm->addr_pool); i++)
229 a = nm->addr_pool + i;
232 #define _(N, j, n, s) \
233 case SNAT_PROTOCOL_##N: \
234 if (a->busy_##n##_ports < (65535-1024)) \
236 if (a->fib_index == fib_index) \
240 portnum = random_u32 (&sm->random_seed); \
242 if (portnum < 1024) \
244 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
247 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
249 a->busy_##n##_ports++; \
251 addr->as_u32 = a->addr.as_u32; \
255 else if (a->fib_index == 0) \
259 foreach_snat_protocol
262 clib_warning ("unknown protocol");
271 #define _(N, j, n, s) \
272 case SNAT_PROTOCOL_##N: \
275 portnum = random_u32 (&sm->random_seed); \
277 if (portnum < 1024) \
279 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
282 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
284 a->busy_##n##_ports++; \
286 addr->as_u32 = a->addr.as_u32; \
290 foreach_snat_protocol
293 clib_warning ("unknown protocol");
298 /* Totally out of translations to use... */
304 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
305 snat_protocol_t proto)
307 nat64_main_t *nm = &nat64_main;
311 for (i = 0; i < vec_len (nm->addr_pool); i++)
313 a = nm->addr_pool + i;
314 if (addr->as_u32 != a->addr.as_u32)
318 #define _(N, j, n, s) \
319 case SNAT_PROTOCOL_##N: \
320 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
322 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
323 a->busy_##n##_ports--; \
325 foreach_snat_protocol
328 clib_warning ("unknown protocol");
336 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
337 ip4_address_t * out_addr, u16 in_port,
338 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
340 nat64_main_t *nm = &nat64_main;
341 nat64_db_bib_entry_t *bibe;
343 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
344 snat_protocol_t p = ip_proto_to_snat_proto (proto);
349 addr.as_u64[0] = in_addr->as_u64[0];
350 addr.as_u64[1] = in_addr->as_u64[1];
352 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
358 return VNET_API_ERROR_VALUE_EXIST;
360 for (i = 0; i < vec_len (nm->addr_pool); i++)
362 a = nm->addr_pool + i;
363 if (out_addr->as_u32 != a->addr.as_u32)
367 #define _(N, j, n, s) \
368 case SNAT_PROTOCOL_##N: \
369 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
371 return VNET_API_ERROR_INVALID_VALUE; \
372 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
374 if (out_port > 1024) \
375 a->busy_##n##_ports++; \
377 foreach_snat_protocol
380 clib_warning ("unknown protocol");
381 return VNET_API_ERROR_INVALID_VALUE_2;
386 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
387 clib_host_to_net_u16 (in_port),
388 clib_host_to_net_u16 (out_port), fib_index,
391 return VNET_API_ERROR_UNSPECIFIED;
396 return VNET_API_ERROR_NO_SUCH_ENTRY;
398 nat64_free_out_addr_and_port (out_addr, out_port, p);
399 nat64_db_bib_entry_free (&nm->db, bibe);
406 nat64_set_udp_timeout (u32 timeout)
408 nat64_main_t *nm = &nat64_main;
411 nm->udp_timeout = SNAT_UDP_TIMEOUT;
412 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
413 return VNET_API_ERROR_INVALID_VALUE;
415 nm->udp_timeout = timeout;
421 nat64_get_udp_timeout (void)
423 nat64_main_t *nm = &nat64_main;
425 return nm->udp_timeout;
429 nat64_set_icmp_timeout (u32 timeout)
431 nat64_main_t *nm = &nat64_main;
434 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
436 nm->icmp_timeout = timeout;
442 nat64_get_icmp_timeout (void)
444 nat64_main_t *nm = &nat64_main;
446 return nm->icmp_timeout;
450 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
452 nat64_main_t *nm = &nat64_main;
455 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
457 nm->tcp_trans_timeout = trans;
460 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
462 nm->tcp_est_timeout = est;
464 if (incoming_syn == 0)
465 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
467 nm->tcp_incoming_syn_timeout = incoming_syn;
473 nat64_get_tcp_trans_timeout (void)
475 nat64_main_t *nm = &nat64_main;
477 return nm->tcp_trans_timeout;
481 nat64_get_tcp_est_timeout (void)
483 nat64_main_t *nm = &nat64_main;
485 return nm->tcp_est_timeout;
489 nat64_get_tcp_incoming_syn_timeout (void)
491 nat64_main_t *nm = &nat64_main;
493 return nm->tcp_incoming_syn_timeout;
497 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
499 nat64_main_t *nm = &nat64_main;
500 u32 now = (u32) vlib_time_now (vm);
504 case SNAT_PROTOCOL_ICMP:
505 ste->expire = now + nm->icmp_timeout;
507 case SNAT_PROTOCOL_TCP:
509 switch (ste->tcp_state)
511 case NAT64_TCP_STATE_V4_INIT:
512 case NAT64_TCP_STATE_V6_INIT:
513 case NAT64_TCP_STATE_V4_FIN_RCV:
514 case NAT64_TCP_STATE_V6_FIN_RCV:
515 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
516 case NAT64_TCP_STATE_TRANS:
517 ste->expire = now + nm->tcp_trans_timeout;
519 case NAT64_TCP_STATE_ESTABLISHED:
520 ste->expire = now + nm->tcp_est_timeout;
526 case SNAT_PROTOCOL_UDP:
527 ste->expire = now + nm->udp_timeout;
535 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
538 switch (ste->tcp_state)
540 case NAT64_TCP_STATE_CLOSED:
542 if (tcp->flags & TCP_FLAG_SYN)
545 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
547 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
551 case NAT64_TCP_STATE_V4_INIT:
553 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
554 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
557 case NAT64_TCP_STATE_V6_INIT:
559 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
560 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
563 case NAT64_TCP_STATE_ESTABLISHED:
565 if (tcp->flags & TCP_FLAG_FIN)
568 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
570 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
572 else if (tcp->flags & TCP_FLAG_RST)
574 ste->tcp_state = NAT64_TCP_STATE_TRANS;
578 case NAT64_TCP_STATE_V4_FIN_RCV:
580 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
581 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
584 case NAT64_TCP_STATE_V6_FIN_RCV:
586 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
587 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
590 case NAT64_TCP_STATE_TRANS:
592 if (!(tcp->flags & TCP_FLAG_RST))
593 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
602 * @brief The 'nat64-expire-walk' process's main loop.
604 * Check expire time for NAT64 sessions.
607 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
610 nat64_main_t *nm = &nat64_main;
614 vlib_process_wait_for_event_or_clock (vm, 10.0);
615 vlib_process_get_events (vm, NULL);
616 u32 now = (u32) vlib_time_now (vm);
618 nad64_db_st_free_expired (&nm->db, now);
624 static vlib_node_registration_t nat64_expire_walk_node;
627 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
628 .function = nat64_expire_walk_fn,
629 .type = VLIB_NODE_TYPE_PROCESS,
630 .name = "nat64-expire-walk",
635 * fd.io coding-style-patch-verification: ON
638 * eval: (c-set-style "gnu")