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"),
41 static u8 well_known_prefix[] = {
42 0x00, 0x64, 0xff, 0x9b,
43 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00
51 nat64_init (vlib_main_t * vm)
53 nat64_main_t *nm = &nat64_main;
54 clib_error_t *error = 0;
55 vlib_thread_main_t *tm = vlib_get_thread_main ();
59 if (tm->n_vlib_mains > 1)
65 if (nat64_db_init (&nm->db))
67 error = clib_error_return (0, "NAT64 DB init failed");
71 /* set session timeouts to default values */
72 nm->udp_timeout = SNAT_UDP_TIMEOUT;
73 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
74 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
75 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
76 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
83 nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
85 nat64_main_t *nm = &nat64_main;
86 snat_address_t *a = 0;
87 snat_interface_t *interface;
90 /* Check if address already exists */
91 for (i = 0; i < vec_len (nm->addr_pool); i++)
93 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
95 a = nm->addr_pool + i;
103 return VNET_API_ERROR_VALUE_EXIST;
105 vec_add2 (nm->addr_pool, a, 1);
110 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
111 #define _(N, i, n, s) \
112 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535);
113 foreach_snat_protocol
119 return VNET_API_ERROR_NO_SUCH_ENTRY;
121 #define _(N, id, n, s) \
122 clib_bitmap_free (a->busy_##n##_port_bitmap);
123 foreach_snat_protocol
125 vec_del1 (nm->addr_pool, i);
128 /* Add/del external address to FIB */
130 pool_foreach (interface, nm->interfaces,
132 if (interface->is_inside)
135 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
144 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
146 nat64_main_t *nm = &nat64_main;
147 snat_address_t *a = 0;
150 vec_foreach (a, nm->addr_pool)
159 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
161 nat64_main_t *nm = &nat64_main;
162 snat_interface_t *interface = 0, *i;
164 const char *feature_name, *arc_name;
166 /* Check if address already exists */
168 pool_foreach (i, nm->interfaces,
170 if (i->sw_if_index == sw_if_index)
181 return VNET_API_ERROR_VALUE_EXIST;
183 pool_get (nm->interfaces, interface);
184 interface->sw_if_index = sw_if_index;
185 interface->is_inside = is_inside;
191 return VNET_API_ERROR_NO_SUCH_ENTRY;
193 pool_put (nm->interfaces, interface);
199 vec_foreach (ap, nm->addr_pool)
200 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
204 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
205 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
207 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
212 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
214 nat64_main_t *nm = &nat64_main;
215 snat_interface_t *i = 0;
218 pool_foreach (i, nm->interfaces,
227 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
228 ip4_address_t * addr, u16 * port)
230 nat64_main_t *nm = &nat64_main;
231 snat_main_t *sm = &snat_main;
233 snat_address_t *a, *ga = 0;
236 for (i = 0; i < vec_len (nm->addr_pool); i++)
238 a = nm->addr_pool + i;
241 #define _(N, j, n, s) \
242 case SNAT_PROTOCOL_##N: \
243 if (a->busy_##n##_ports < (65535-1024)) \
245 if (a->fib_index == fib_index) \
249 portnum = random_u32 (&sm->random_seed); \
251 if (portnum < 1024) \
253 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
256 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
258 a->busy_##n##_ports++; \
260 addr->as_u32 = a->addr.as_u32; \
264 else if (a->fib_index == 0) \
268 foreach_snat_protocol
271 clib_warning ("unknown protocol");
280 #define _(N, j, n, s) \
281 case SNAT_PROTOCOL_##N: \
284 portnum = random_u32 (&sm->random_seed); \
286 if (portnum < 1024) \
288 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
291 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
293 a->busy_##n##_ports++; \
295 addr->as_u32 = a->addr.as_u32; \
299 foreach_snat_protocol
302 clib_warning ("unknown protocol");
307 /* Totally out of translations to use... */
313 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
314 snat_protocol_t proto)
316 nat64_main_t *nm = &nat64_main;
320 for (i = 0; i < vec_len (nm->addr_pool); i++)
322 a = nm->addr_pool + i;
323 if (addr->as_u32 != a->addr.as_u32)
327 #define _(N, j, n, s) \
328 case SNAT_PROTOCOL_##N: \
329 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
331 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
332 a->busy_##n##_ports--; \
334 foreach_snat_protocol
337 clib_warning ("unknown protocol");
345 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
346 ip4_address_t * out_addr, u16 in_port,
347 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
349 nat64_main_t *nm = &nat64_main;
350 nat64_db_bib_entry_t *bibe;
352 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
353 snat_protocol_t p = ip_proto_to_snat_proto (proto);
358 addr.as_u64[0] = in_addr->as_u64[0];
359 addr.as_u64[1] = in_addr->as_u64[1];
361 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
367 return VNET_API_ERROR_VALUE_EXIST;
369 for (i = 0; i < vec_len (nm->addr_pool); i++)
371 a = nm->addr_pool + i;
372 if (out_addr->as_u32 != a->addr.as_u32)
376 #define _(N, j, n, s) \
377 case SNAT_PROTOCOL_##N: \
378 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
380 return VNET_API_ERROR_INVALID_VALUE; \
381 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
383 if (out_port > 1024) \
384 a->busy_##n##_ports++; \
386 foreach_snat_protocol
389 clib_warning ("unknown protocol");
390 return VNET_API_ERROR_INVALID_VALUE_2;
395 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
396 clib_host_to_net_u16 (in_port),
397 clib_host_to_net_u16 (out_port), fib_index,
400 return VNET_API_ERROR_UNSPECIFIED;
405 return VNET_API_ERROR_NO_SUCH_ENTRY;
407 nat64_free_out_addr_and_port (out_addr, out_port, p);
408 nat64_db_bib_entry_free (&nm->db, bibe);
415 nat64_set_udp_timeout (u32 timeout)
417 nat64_main_t *nm = &nat64_main;
420 nm->udp_timeout = SNAT_UDP_TIMEOUT;
421 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
422 return VNET_API_ERROR_INVALID_VALUE;
424 nm->udp_timeout = timeout;
430 nat64_get_udp_timeout (void)
432 nat64_main_t *nm = &nat64_main;
434 return nm->udp_timeout;
438 nat64_set_icmp_timeout (u32 timeout)
440 nat64_main_t *nm = &nat64_main;
443 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
445 nm->icmp_timeout = timeout;
451 nat64_get_icmp_timeout (void)
453 nat64_main_t *nm = &nat64_main;
455 return nm->icmp_timeout;
459 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
461 nat64_main_t *nm = &nat64_main;
464 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
466 nm->tcp_trans_timeout = trans;
469 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
471 nm->tcp_est_timeout = est;
473 if (incoming_syn == 0)
474 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
476 nm->tcp_incoming_syn_timeout = incoming_syn;
482 nat64_get_tcp_trans_timeout (void)
484 nat64_main_t *nm = &nat64_main;
486 return nm->tcp_trans_timeout;
490 nat64_get_tcp_est_timeout (void)
492 nat64_main_t *nm = &nat64_main;
494 return nm->tcp_est_timeout;
498 nat64_get_tcp_incoming_syn_timeout (void)
500 nat64_main_t *nm = &nat64_main;
502 return nm->tcp_incoming_syn_timeout;
506 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
508 nat64_main_t *nm = &nat64_main;
509 u32 now = (u32) vlib_time_now (vm);
513 case SNAT_PROTOCOL_ICMP:
514 ste->expire = now + nm->icmp_timeout;
516 case SNAT_PROTOCOL_TCP:
518 switch (ste->tcp_state)
520 case NAT64_TCP_STATE_V4_INIT:
521 case NAT64_TCP_STATE_V6_INIT:
522 case NAT64_TCP_STATE_V4_FIN_RCV:
523 case NAT64_TCP_STATE_V6_FIN_RCV:
524 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
525 case NAT64_TCP_STATE_TRANS:
526 ste->expire = now + nm->tcp_trans_timeout;
528 case NAT64_TCP_STATE_ESTABLISHED:
529 ste->expire = now + nm->tcp_est_timeout;
535 case SNAT_PROTOCOL_UDP:
536 ste->expire = now + nm->udp_timeout;
544 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
547 switch (ste->tcp_state)
549 case NAT64_TCP_STATE_CLOSED:
551 if (tcp->flags & TCP_FLAG_SYN)
554 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
556 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
560 case NAT64_TCP_STATE_V4_INIT:
562 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
563 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
566 case NAT64_TCP_STATE_V6_INIT:
568 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
569 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
572 case NAT64_TCP_STATE_ESTABLISHED:
574 if (tcp->flags & TCP_FLAG_FIN)
577 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
579 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
581 else if (tcp->flags & TCP_FLAG_RST)
583 ste->tcp_state = NAT64_TCP_STATE_TRANS;
587 case NAT64_TCP_STATE_V4_FIN_RCV:
589 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
590 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
593 case NAT64_TCP_STATE_V6_FIN_RCV:
595 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
596 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
599 case NAT64_TCP_STATE_TRANS:
601 if (!(tcp->flags & TCP_FLAG_RST))
602 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
611 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
613 nat64_main_t *nm = &nat64_main;
614 nat64_prefix_t *p = 0;
617 /* Verify prefix length */
618 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
620 return VNET_API_ERROR_INVALID_VALUE;
622 /* Check if tenant already have prefix */
623 for (i = 0; i < vec_len (nm->pref64); i++)
625 if (nm->pref64[i].vrf_id == vrf_id)
636 vec_add2 (nm->pref64, p, 1);
638 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
642 p->prefix.as_u64[0] = prefix->as_u64[0];
643 p->prefix.as_u64[1] = prefix->as_u64[1];
649 return VNET_API_ERROR_NO_SUCH_ENTRY;
651 vec_del1 (nm->pref64, i);
658 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
660 nat64_main_t *nm = &nat64_main;
661 nat64_prefix_t *p = 0;
664 vec_foreach (p, nm->pref64)
673 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
675 nat64_main_t *nm = &nat64_main;
676 nat64_prefix_t *p, *gp = 0, *prefix = 0;
679 vec_foreach (p, nm->pref64)
681 if (p->fib_index == fib_index)
687 if (p->fib_index == 0)
698 memcpy (ip6, &p->prefix, p->plen);
702 ip6->as_u32[1] = ip4->as_u32;
705 ip6->as_u8[5] = ip4->as_u8[0];
706 ip6->as_u8[6] = ip4->as_u8[1];
707 ip6->as_u8[7] = ip4->as_u8[2];
708 ip6->as_u8[9] = ip4->as_u8[3];
711 ip6->as_u8[6] = ip4->as_u8[0];
712 ip6->as_u8[7] = ip4->as_u8[1];
713 ip6->as_u8[9] = ip4->as_u8[2];
714 ip6->as_u8[10] = ip4->as_u8[3];
717 ip6->as_u8[7] = ip4->as_u8[0];
718 ip6->as_u8[9] = ip4->as_u8[1];
719 ip6->as_u8[10] = ip4->as_u8[2];
720 ip6->as_u8[11] = ip4->as_u8[3];
723 ip6->as_u8[9] = ip4->as_u8[0];
724 ip6->as_u8[10] = ip4->as_u8[1];
725 ip6->as_u8[11] = ip4->as_u8[2];
726 ip6->as_u8[12] = ip4->as_u8[3];
729 ip6->as_u32[3] = ip4->as_u32;
732 clib_warning ("invalid prefix length");
738 memcpy (ip6, well_known_prefix, 16);
739 ip6->as_u32[3] = ip4->as_u32;
744 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
746 nat64_main_t *nm = &nat64_main;
747 nat64_prefix_t *p, *gp = 0;
751 vec_foreach (p, nm->pref64)
753 if (p->fib_index == fib_index)
775 ip4->as_u32 = ip6->as_u32[1];
778 ip4->as_u8[0] = ip6->as_u8[5];
779 ip4->as_u8[1] = ip6->as_u8[6];
780 ip4->as_u8[2] = ip6->as_u8[7];
781 ip4->as_u8[3] = ip6->as_u8[9];
784 ip4->as_u8[0] = ip6->as_u8[6];
785 ip4->as_u8[1] = ip6->as_u8[7];
786 ip4->as_u8[2] = ip6->as_u8[9];
787 ip4->as_u8[3] = ip6->as_u8[10];
790 ip4->as_u8[0] = ip6->as_u8[7];
791 ip4->as_u8[1] = ip6->as_u8[9];
792 ip4->as_u8[2] = ip6->as_u8[10];
793 ip4->as_u8[3] = ip6->as_u8[11];
796 ip4->as_u8[0] = ip6->as_u8[9];
797 ip4->as_u8[1] = ip6->as_u8[10];
798 ip4->as_u8[2] = ip6->as_u8[11];
799 ip4->as_u8[3] = ip6->as_u8[12];
802 ip4->as_u32 = ip6->as_u32[3];
805 clib_warning ("invalid prefix length");
809 clib_warning ("%U %U plen %u", format_ip6_address, ip6, format_ip4_address,
814 * @brief The 'nat64-expire-walk' process's main loop.
816 * Check expire time for NAT64 sessions.
819 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
822 nat64_main_t *nm = &nat64_main;
824 while (!nm->is_disabled)
826 vlib_process_wait_for_event_or_clock (vm, 10.0);
827 vlib_process_get_events (vm, NULL);
828 u32 now = (u32) vlib_time_now (vm);
830 nad64_db_st_free_expired (&nm->db, now);
836 static vlib_node_registration_t nat64_expire_walk_node;
839 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
840 .function = nat64_expire_walk_fn,
841 .type = VLIB_NODE_TYPE_PROCESS,
842 .name = "nat64-expire-walk",
847 * fd.io coding-style-patch-verification: ON
850 * eval: (c-set-style "gnu")