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 <nat/nat64.h>
21 #include <nat/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 FIB_SOURCE_PLUGIN_HI);
112 #define _(N, i, n, s) \
113 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535);
114 foreach_snat_protocol
120 return VNET_API_ERROR_NO_SUCH_ENTRY;
123 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6,
124 FIB_SOURCE_PLUGIN_HI);
126 #define _(N, id, n, s) \
127 clib_bitmap_free (a->busy_##n##_port_bitmap);
128 foreach_snat_protocol
130 /* Delete sessions using address */
131 nat64_db_free_out_addr (&nm->db, &a->addr);
132 vec_del1 (nm->addr_pool, i);
135 /* Add/del external address to FIB */
137 pool_foreach (interface, nm->interfaces,
139 if (nat_interface_is_inside(interface))
142 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
151 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
153 nat64_main_t *nm = &nat64_main;
154 snat_address_t *a = 0;
157 vec_foreach (a, nm->addr_pool)
166 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
168 nat64_main_t *nm = &nat64_main;
169 snat_interface_t *interface = 0, *i;
171 const char *feature_name, *arc_name;
173 /* Check if interface already exists */
175 pool_foreach (i, nm->interfaces,
177 if (i->sw_if_index == sw_if_index)
190 pool_get (nm->interfaces, interface);
191 interface->sw_if_index = sw_if_index;
192 interface->flags = 0;
195 interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
197 interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
202 return VNET_API_ERROR_NO_SUCH_ENTRY;
204 if ((nat_interface_is_inside (interface)
205 && nat_interface_is_outside (interface)))
207 is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE :
208 ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
210 pool_put (nm->interfaces, interface);
216 vec_foreach (ap, nm->addr_pool)
217 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
221 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
222 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
224 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
229 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
231 nat64_main_t *nm = &nat64_main;
232 snat_interface_t *i = 0;
235 pool_foreach (i, nm->interfaces,
244 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
245 ip4_address_t * addr, u16 * port)
247 nat64_main_t *nm = &nat64_main;
248 snat_main_t *sm = &snat_main;
250 snat_address_t *a, *ga = 0;
253 for (i = 0; i < vec_len (nm->addr_pool); i++)
255 a = nm->addr_pool + i;
258 #define _(N, j, n, s) \
259 case SNAT_PROTOCOL_##N: \
260 if (a->busy_##n##_ports < (65535-1024)) \
262 if (a->fib_index == fib_index) \
266 portnum = random_u32 (&sm->random_seed); \
268 if (portnum < 1024) \
270 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
273 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
275 a->busy_##n##_ports++; \
277 addr->as_u32 = a->addr.as_u32; \
281 else if (a->fib_index == 0) \
285 foreach_snat_protocol
288 clib_warning ("unknown protocol");
297 #define _(N, j, n, s) \
298 case SNAT_PROTOCOL_##N: \
301 portnum = random_u32 (&sm->random_seed); \
303 if (portnum < 1024) \
305 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
308 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
310 a->busy_##n##_ports++; \
312 addr->as_u32 = a->addr.as_u32; \
316 foreach_snat_protocol
319 clib_warning ("unknown protocol");
324 /* Totally out of translations to use... */
330 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
331 snat_protocol_t proto)
333 nat64_main_t *nm = &nat64_main;
337 for (i = 0; i < vec_len (nm->addr_pool); i++)
339 a = nm->addr_pool + i;
340 if (addr->as_u32 != a->addr.as_u32)
344 #define _(N, j, n, s) \
345 case SNAT_PROTOCOL_##N: \
346 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
348 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
349 a->busy_##n##_ports--; \
351 foreach_snat_protocol
354 clib_warning ("unknown protocol");
362 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
363 ip4_address_t * out_addr, u16 in_port,
364 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
366 nat64_main_t *nm = &nat64_main;
367 nat64_db_bib_entry_t *bibe;
368 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
369 FIB_SOURCE_PLUGIN_HI);
370 snat_protocol_t p = ip_proto_to_snat_proto (proto);
375 addr.as_u64[0] = in_addr->as_u64[0];
376 addr.as_u64[1] = in_addr->as_u64[1];
378 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
379 proto, fib_index, 1);
384 return VNET_API_ERROR_VALUE_EXIST;
386 for (i = 0; i < vec_len (nm->addr_pool); i++)
388 a = nm->addr_pool + i;
389 if (out_addr->as_u32 != a->addr.as_u32)
393 #define _(N, j, n, s) \
394 case SNAT_PROTOCOL_##N: \
395 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
397 return VNET_API_ERROR_INVALID_VALUE; \
398 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
400 if (out_port > 1024) \
401 a->busy_##n##_ports++; \
403 foreach_snat_protocol
406 memset (&addr, 0, sizeof (addr));
407 addr.ip4.as_u32 = out_addr->as_u32;
408 if (nat64_db_bib_entry_find
409 (&nm->db, &addr, 0, proto, fib_index, 0))
410 return VNET_API_ERROR_INVALID_VALUE;
415 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
416 clib_host_to_net_u16 (in_port),
417 clib_host_to_net_u16 (out_port), fib_index,
420 return VNET_API_ERROR_UNSPECIFIED;
425 return VNET_API_ERROR_NO_SUCH_ENTRY;
427 nat64_free_out_addr_and_port (out_addr, out_port, p);
428 nat64_db_bib_entry_free (&nm->db, bibe);
435 nat64_set_udp_timeout (u32 timeout)
437 nat64_main_t *nm = &nat64_main;
440 nm->udp_timeout = SNAT_UDP_TIMEOUT;
441 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
442 return VNET_API_ERROR_INVALID_VALUE;
444 nm->udp_timeout = timeout;
450 nat64_get_udp_timeout (void)
452 nat64_main_t *nm = &nat64_main;
454 return nm->udp_timeout;
458 nat64_set_icmp_timeout (u32 timeout)
460 nat64_main_t *nm = &nat64_main;
463 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
465 nm->icmp_timeout = timeout;
471 nat64_get_icmp_timeout (void)
473 nat64_main_t *nm = &nat64_main;
475 return nm->icmp_timeout;
479 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
481 nat64_main_t *nm = &nat64_main;
484 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
486 nm->tcp_trans_timeout = trans;
489 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
491 nm->tcp_est_timeout = est;
493 if (incoming_syn == 0)
494 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
496 nm->tcp_incoming_syn_timeout = incoming_syn;
502 nat64_get_tcp_trans_timeout (void)
504 nat64_main_t *nm = &nat64_main;
506 return nm->tcp_trans_timeout;
510 nat64_get_tcp_est_timeout (void)
512 nat64_main_t *nm = &nat64_main;
514 return nm->tcp_est_timeout;
518 nat64_get_tcp_incoming_syn_timeout (void)
520 nat64_main_t *nm = &nat64_main;
522 return nm->tcp_incoming_syn_timeout;
526 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
528 nat64_main_t *nm = &nat64_main;
529 u32 now = (u32) vlib_time_now (vm);
531 switch (ip_proto_to_snat_proto (ste->proto))
533 case SNAT_PROTOCOL_ICMP:
534 ste->expire = now + nm->icmp_timeout;
536 case SNAT_PROTOCOL_TCP:
538 switch (ste->tcp_state)
540 case NAT64_TCP_STATE_V4_INIT:
541 case NAT64_TCP_STATE_V6_INIT:
542 case NAT64_TCP_STATE_V4_FIN_RCV:
543 case NAT64_TCP_STATE_V6_FIN_RCV:
544 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
545 case NAT64_TCP_STATE_TRANS:
546 ste->expire = now + nm->tcp_trans_timeout;
548 case NAT64_TCP_STATE_ESTABLISHED:
549 ste->expire = now + nm->tcp_est_timeout;
555 case SNAT_PROTOCOL_UDP:
556 ste->expire = now + nm->udp_timeout;
559 ste->expire = now + nm->udp_timeout;
565 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
568 switch (ste->tcp_state)
570 case NAT64_TCP_STATE_CLOSED:
572 if (tcp->flags & TCP_FLAG_SYN)
575 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
577 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
581 case NAT64_TCP_STATE_V4_INIT:
583 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
584 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
587 case NAT64_TCP_STATE_V6_INIT:
589 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
590 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
593 case NAT64_TCP_STATE_ESTABLISHED:
595 if (tcp->flags & TCP_FLAG_FIN)
598 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
600 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
602 else if (tcp->flags & TCP_FLAG_RST)
604 ste->tcp_state = NAT64_TCP_STATE_TRANS;
608 case NAT64_TCP_STATE_V4_FIN_RCV:
610 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
611 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
614 case NAT64_TCP_STATE_V6_FIN_RCV:
616 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
617 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
620 case NAT64_TCP_STATE_TRANS:
622 if (!(tcp->flags & TCP_FLAG_RST))
623 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
632 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
634 nat64_main_t *nm = &nat64_main;
635 nat64_prefix_t *p = 0;
638 /* Verify prefix length */
639 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
641 return VNET_API_ERROR_INVALID_VALUE;
643 /* Check if tenant already have prefix */
644 for (i = 0; i < vec_len (nm->pref64); i++)
646 if (nm->pref64[i].vrf_id == vrf_id)
657 vec_add2 (nm->pref64, p, 1);
659 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
660 FIB_SOURCE_PLUGIN_HI);
664 p->prefix.as_u64[0] = prefix->as_u64[0];
665 p->prefix.as_u64[1] = prefix->as_u64[1];
671 return VNET_API_ERROR_NO_SUCH_ENTRY;
673 vec_del1 (nm->pref64, i);
680 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
682 nat64_main_t *nm = &nat64_main;
683 nat64_prefix_t *p = 0;
686 vec_foreach (p, nm->pref64)
695 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
697 nat64_main_t *nm = &nat64_main;
698 nat64_prefix_t *p, *gp = 0, *prefix = 0;
701 vec_foreach (p, nm->pref64)
703 if (p->fib_index == fib_index)
709 if (p->fib_index == 0)
720 memcpy (ip6, &p->prefix, p->plen);
724 ip6->as_u32[1] = ip4->as_u32;
727 ip6->as_u8[5] = ip4->as_u8[0];
728 ip6->as_u8[6] = ip4->as_u8[1];
729 ip6->as_u8[7] = ip4->as_u8[2];
730 ip6->as_u8[9] = ip4->as_u8[3];
733 ip6->as_u8[6] = ip4->as_u8[0];
734 ip6->as_u8[7] = ip4->as_u8[1];
735 ip6->as_u8[9] = ip4->as_u8[2];
736 ip6->as_u8[10] = ip4->as_u8[3];
739 ip6->as_u8[7] = ip4->as_u8[0];
740 ip6->as_u8[9] = ip4->as_u8[1];
741 ip6->as_u8[10] = ip4->as_u8[2];
742 ip6->as_u8[11] = ip4->as_u8[3];
745 ip6->as_u8[9] = ip4->as_u8[0];
746 ip6->as_u8[10] = ip4->as_u8[1];
747 ip6->as_u8[11] = ip4->as_u8[2];
748 ip6->as_u8[12] = ip4->as_u8[3];
751 ip6->as_u32[3] = ip4->as_u32;
754 clib_warning ("invalid prefix length");
760 memcpy (ip6, well_known_prefix, 16);
761 ip6->as_u32[3] = ip4->as_u32;
766 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
768 nat64_main_t *nm = &nat64_main;
769 nat64_prefix_t *p, *gp = 0;
773 vec_foreach (p, nm->pref64)
775 if (p->fib_index == fib_index)
797 ip4->as_u32 = ip6->as_u32[1];
800 ip4->as_u8[0] = ip6->as_u8[5];
801 ip4->as_u8[1] = ip6->as_u8[6];
802 ip4->as_u8[2] = ip6->as_u8[7];
803 ip4->as_u8[3] = ip6->as_u8[9];
806 ip4->as_u8[0] = ip6->as_u8[6];
807 ip4->as_u8[1] = ip6->as_u8[7];
808 ip4->as_u8[2] = ip6->as_u8[9];
809 ip4->as_u8[3] = ip6->as_u8[10];
812 ip4->as_u8[0] = ip6->as_u8[7];
813 ip4->as_u8[1] = ip6->as_u8[9];
814 ip4->as_u8[2] = ip6->as_u8[10];
815 ip4->as_u8[3] = ip6->as_u8[11];
818 ip4->as_u8[0] = ip6->as_u8[9];
819 ip4->as_u8[1] = ip6->as_u8[10];
820 ip4->as_u8[2] = ip6->as_u8[11];
821 ip4->as_u8[3] = ip6->as_u8[12];
824 ip4->as_u32 = ip6->as_u32[3];
827 clib_warning ("invalid prefix length");
833 * @brief The 'nat64-expire-walk' process's main loop.
835 * Check expire time for NAT64 sessions.
838 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
841 nat64_main_t *nm = &nat64_main;
843 while (!nm->is_disabled)
845 vlib_process_wait_for_event_or_clock (vm, 10.0);
846 vlib_process_get_events (vm, NULL);
847 u32 now = (u32) vlib_time_now (vm);
849 nad64_db_st_free_expired (&nm->db, now);
855 static vlib_node_registration_t nat64_expire_walk_node;
858 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
859 .function = nat64_expire_walk_fn,
860 .type = VLIB_NODE_TYPE_PROCESS,
861 .name = "nat64-expire-walk",
866 * fd.io coding-style-patch-verification: ON
869 * eval: (c-set-style "gnu")