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;
122 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6);
124 #define _(N, id, n, s) \
125 clib_bitmap_free (a->busy_##n##_port_bitmap);
126 foreach_snat_protocol
128 vec_del1 (nm->addr_pool, i);
131 /* Add/del external address to FIB */
133 pool_foreach (interface, nm->interfaces,
135 if (interface->is_inside)
138 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
147 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
149 nat64_main_t *nm = &nat64_main;
150 snat_address_t *a = 0;
153 vec_foreach (a, nm->addr_pool)
162 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
164 nat64_main_t *nm = &nat64_main;
165 snat_interface_t *interface = 0, *i;
167 const char *feature_name, *arc_name;
169 /* Check if address already exists */
171 pool_foreach (i, nm->interfaces,
173 if (i->sw_if_index == sw_if_index)
184 return VNET_API_ERROR_VALUE_EXIST;
186 pool_get (nm->interfaces, interface);
187 interface->sw_if_index = sw_if_index;
188 interface->is_inside = is_inside;
194 return VNET_API_ERROR_NO_SUCH_ENTRY;
196 pool_put (nm->interfaces, interface);
202 vec_foreach (ap, nm->addr_pool)
203 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
207 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
208 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
210 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
215 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
217 nat64_main_t *nm = &nat64_main;
218 snat_interface_t *i = 0;
221 pool_foreach (i, nm->interfaces,
230 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
231 ip4_address_t * addr, u16 * port)
233 nat64_main_t *nm = &nat64_main;
234 snat_main_t *sm = &snat_main;
236 snat_address_t *a, *ga = 0;
239 for (i = 0; i < vec_len (nm->addr_pool); i++)
241 a = nm->addr_pool + i;
244 #define _(N, j, n, s) \
245 case SNAT_PROTOCOL_##N: \
246 if (a->busy_##n##_ports < (65535-1024)) \
248 if (a->fib_index == fib_index) \
252 portnum = random_u32 (&sm->random_seed); \
254 if (portnum < 1024) \
256 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
259 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
261 a->busy_##n##_ports++; \
263 addr->as_u32 = a->addr.as_u32; \
267 else if (a->fib_index == 0) \
271 foreach_snat_protocol
274 clib_warning ("unknown protocol");
283 #define _(N, j, n, s) \
284 case SNAT_PROTOCOL_##N: \
287 portnum = random_u32 (&sm->random_seed); \
289 if (portnum < 1024) \
291 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
294 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
296 a->busy_##n##_ports++; \
298 addr->as_u32 = a->addr.as_u32; \
302 foreach_snat_protocol
305 clib_warning ("unknown protocol");
310 /* Totally out of translations to use... */
316 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
317 snat_protocol_t proto)
319 nat64_main_t *nm = &nat64_main;
323 for (i = 0; i < vec_len (nm->addr_pool); i++)
325 a = nm->addr_pool + i;
326 if (addr->as_u32 != a->addr.as_u32)
330 #define _(N, j, n, s) \
331 case SNAT_PROTOCOL_##N: \
332 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
334 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
335 a->busy_##n##_ports--; \
337 foreach_snat_protocol
340 clib_warning ("unknown protocol");
348 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
349 ip4_address_t * out_addr, u16 in_port,
350 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
352 nat64_main_t *nm = &nat64_main;
353 nat64_db_bib_entry_t *bibe;
355 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
356 snat_protocol_t p = ip_proto_to_snat_proto (proto);
361 addr.as_u64[0] = in_addr->as_u64[0];
362 addr.as_u64[1] = in_addr->as_u64[1];
364 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
370 return VNET_API_ERROR_VALUE_EXIST;
372 for (i = 0; i < vec_len (nm->addr_pool); i++)
374 a = nm->addr_pool + i;
375 if (out_addr->as_u32 != a->addr.as_u32)
379 #define _(N, j, n, s) \
380 case SNAT_PROTOCOL_##N: \
381 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
383 return VNET_API_ERROR_INVALID_VALUE; \
384 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
386 if (out_port > 1024) \
387 a->busy_##n##_ports++; \
389 foreach_snat_protocol
392 clib_warning ("unknown protocol");
393 return VNET_API_ERROR_INVALID_VALUE_2;
398 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
399 clib_host_to_net_u16 (in_port),
400 clib_host_to_net_u16 (out_port), fib_index,
403 return VNET_API_ERROR_UNSPECIFIED;
408 return VNET_API_ERROR_NO_SUCH_ENTRY;
410 nat64_free_out_addr_and_port (out_addr, out_port, p);
411 nat64_db_bib_entry_free (&nm->db, bibe);
418 nat64_set_udp_timeout (u32 timeout)
420 nat64_main_t *nm = &nat64_main;
423 nm->udp_timeout = SNAT_UDP_TIMEOUT;
424 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
425 return VNET_API_ERROR_INVALID_VALUE;
427 nm->udp_timeout = timeout;
433 nat64_get_udp_timeout (void)
435 nat64_main_t *nm = &nat64_main;
437 return nm->udp_timeout;
441 nat64_set_icmp_timeout (u32 timeout)
443 nat64_main_t *nm = &nat64_main;
446 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
448 nm->icmp_timeout = timeout;
454 nat64_get_icmp_timeout (void)
456 nat64_main_t *nm = &nat64_main;
458 return nm->icmp_timeout;
462 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
464 nat64_main_t *nm = &nat64_main;
467 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
469 nm->tcp_trans_timeout = trans;
472 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
474 nm->tcp_est_timeout = est;
476 if (incoming_syn == 0)
477 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
479 nm->tcp_incoming_syn_timeout = incoming_syn;
485 nat64_get_tcp_trans_timeout (void)
487 nat64_main_t *nm = &nat64_main;
489 return nm->tcp_trans_timeout;
493 nat64_get_tcp_est_timeout (void)
495 nat64_main_t *nm = &nat64_main;
497 return nm->tcp_est_timeout;
501 nat64_get_tcp_incoming_syn_timeout (void)
503 nat64_main_t *nm = &nat64_main;
505 return nm->tcp_incoming_syn_timeout;
509 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
511 nat64_main_t *nm = &nat64_main;
512 u32 now = (u32) vlib_time_now (vm);
516 case SNAT_PROTOCOL_ICMP:
517 ste->expire = now + nm->icmp_timeout;
519 case SNAT_PROTOCOL_TCP:
521 switch (ste->tcp_state)
523 case NAT64_TCP_STATE_V4_INIT:
524 case NAT64_TCP_STATE_V6_INIT:
525 case NAT64_TCP_STATE_V4_FIN_RCV:
526 case NAT64_TCP_STATE_V6_FIN_RCV:
527 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
528 case NAT64_TCP_STATE_TRANS:
529 ste->expire = now + nm->tcp_trans_timeout;
531 case NAT64_TCP_STATE_ESTABLISHED:
532 ste->expire = now + nm->tcp_est_timeout;
538 case SNAT_PROTOCOL_UDP:
539 ste->expire = now + nm->udp_timeout;
547 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
550 switch (ste->tcp_state)
552 case NAT64_TCP_STATE_CLOSED:
554 if (tcp->flags & TCP_FLAG_SYN)
557 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
559 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
563 case NAT64_TCP_STATE_V4_INIT:
565 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
566 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
569 case NAT64_TCP_STATE_V6_INIT:
571 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
572 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
575 case NAT64_TCP_STATE_ESTABLISHED:
577 if (tcp->flags & TCP_FLAG_FIN)
580 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
582 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
584 else if (tcp->flags & TCP_FLAG_RST)
586 ste->tcp_state = NAT64_TCP_STATE_TRANS;
590 case NAT64_TCP_STATE_V4_FIN_RCV:
592 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
593 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
596 case NAT64_TCP_STATE_V6_FIN_RCV:
598 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
599 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
602 case NAT64_TCP_STATE_TRANS:
604 if (!(tcp->flags & TCP_FLAG_RST))
605 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
614 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
616 nat64_main_t *nm = &nat64_main;
617 nat64_prefix_t *p = 0;
620 /* Verify prefix length */
621 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
623 return VNET_API_ERROR_INVALID_VALUE;
625 /* Check if tenant already have prefix */
626 for (i = 0; i < vec_len (nm->pref64); i++)
628 if (nm->pref64[i].vrf_id == vrf_id)
639 vec_add2 (nm->pref64, p, 1);
641 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
645 p->prefix.as_u64[0] = prefix->as_u64[0];
646 p->prefix.as_u64[1] = prefix->as_u64[1];
652 return VNET_API_ERROR_NO_SUCH_ENTRY;
654 vec_del1 (nm->pref64, i);
661 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
663 nat64_main_t *nm = &nat64_main;
664 nat64_prefix_t *p = 0;
667 vec_foreach (p, nm->pref64)
676 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
678 nat64_main_t *nm = &nat64_main;
679 nat64_prefix_t *p, *gp = 0, *prefix = 0;
682 vec_foreach (p, nm->pref64)
684 if (p->fib_index == fib_index)
690 if (p->fib_index == 0)
701 memcpy (ip6, &p->prefix, p->plen);
705 ip6->as_u32[1] = ip4->as_u32;
708 ip6->as_u8[5] = ip4->as_u8[0];
709 ip6->as_u8[6] = ip4->as_u8[1];
710 ip6->as_u8[7] = ip4->as_u8[2];
711 ip6->as_u8[9] = ip4->as_u8[3];
714 ip6->as_u8[6] = ip4->as_u8[0];
715 ip6->as_u8[7] = ip4->as_u8[1];
716 ip6->as_u8[9] = ip4->as_u8[2];
717 ip6->as_u8[10] = ip4->as_u8[3];
720 ip6->as_u8[7] = ip4->as_u8[0];
721 ip6->as_u8[9] = ip4->as_u8[1];
722 ip6->as_u8[10] = ip4->as_u8[2];
723 ip6->as_u8[11] = ip4->as_u8[3];
726 ip6->as_u8[9] = ip4->as_u8[0];
727 ip6->as_u8[10] = ip4->as_u8[1];
728 ip6->as_u8[11] = ip4->as_u8[2];
729 ip6->as_u8[12] = ip4->as_u8[3];
732 ip6->as_u32[3] = ip4->as_u32;
735 clib_warning ("invalid prefix length");
741 memcpy (ip6, well_known_prefix, 16);
742 ip6->as_u32[3] = ip4->as_u32;
747 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
749 nat64_main_t *nm = &nat64_main;
750 nat64_prefix_t *p, *gp = 0;
754 vec_foreach (p, nm->pref64)
756 if (p->fib_index == fib_index)
778 ip4->as_u32 = ip6->as_u32[1];
781 ip4->as_u8[0] = ip6->as_u8[5];
782 ip4->as_u8[1] = ip6->as_u8[6];
783 ip4->as_u8[2] = ip6->as_u8[7];
784 ip4->as_u8[3] = ip6->as_u8[9];
787 ip4->as_u8[0] = ip6->as_u8[6];
788 ip4->as_u8[1] = ip6->as_u8[7];
789 ip4->as_u8[2] = ip6->as_u8[9];
790 ip4->as_u8[3] = ip6->as_u8[10];
793 ip4->as_u8[0] = ip6->as_u8[7];
794 ip4->as_u8[1] = ip6->as_u8[9];
795 ip4->as_u8[2] = ip6->as_u8[10];
796 ip4->as_u8[3] = ip6->as_u8[11];
799 ip4->as_u8[0] = ip6->as_u8[9];
800 ip4->as_u8[1] = ip6->as_u8[10];
801 ip4->as_u8[2] = ip6->as_u8[11];
802 ip4->as_u8[3] = ip6->as_u8[12];
805 ip4->as_u32 = ip6->as_u32[3];
808 clib_warning ("invalid prefix length");
812 clib_warning ("%U %U plen %u", format_ip6_address, ip6, format_ip4_address,
817 * @brief The 'nat64-expire-walk' process's main loop.
819 * Check expire time for NAT64 sessions.
822 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
825 nat64_main_t *nm = &nat64_main;
827 while (!nm->is_disabled)
829 vlib_process_wait_for_event_or_clock (vm, 10.0);
830 vlib_process_get_events (vm, NULL);
831 u32 now = (u32) vlib_time_now (vm);
833 nad64_db_st_free_expired (&nm->db, now);
839 static vlib_node_registration_t nat64_expire_walk_node;
842 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
843 .function = nat64_expire_walk_fn,
844 .type = VLIB_NODE_TYPE_PROCESS,
845 .name = "nat64-expire-walk",
850 * fd.io coding-style-patch-verification: ON
853 * eval: (c-set-style "gnu")