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 (interface->is_inside)
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 address already exists */
175 pool_foreach (i, nm->interfaces,
177 if (i->sw_if_index == sw_if_index)
188 return VNET_API_ERROR_VALUE_EXIST;
190 pool_get (nm->interfaces, interface);
191 interface->sw_if_index = sw_if_index;
192 interface->is_inside = is_inside;
198 return VNET_API_ERROR_NO_SUCH_ENTRY;
200 pool_put (nm->interfaces, interface);
206 vec_foreach (ap, nm->addr_pool)
207 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
211 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
212 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
214 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
219 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
221 nat64_main_t *nm = &nat64_main;
222 snat_interface_t *i = 0;
225 pool_foreach (i, nm->interfaces,
234 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
235 ip4_address_t * addr, u16 * port)
237 nat64_main_t *nm = &nat64_main;
238 snat_main_t *sm = &snat_main;
240 snat_address_t *a, *ga = 0;
243 for (i = 0; i < vec_len (nm->addr_pool); i++)
245 a = nm->addr_pool + i;
248 #define _(N, j, n, s) \
249 case SNAT_PROTOCOL_##N: \
250 if (a->busy_##n##_ports < (65535-1024)) \
252 if (a->fib_index == fib_index) \
256 portnum = random_u32 (&sm->random_seed); \
258 if (portnum < 1024) \
260 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
263 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
265 a->busy_##n##_ports++; \
267 addr->as_u32 = a->addr.as_u32; \
271 else if (a->fib_index == 0) \
275 foreach_snat_protocol
278 clib_warning ("unknown protocol");
287 #define _(N, j, n, s) \
288 case SNAT_PROTOCOL_##N: \
291 portnum = random_u32 (&sm->random_seed); \
293 if (portnum < 1024) \
295 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
298 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
300 a->busy_##n##_ports++; \
302 addr->as_u32 = a->addr.as_u32; \
306 foreach_snat_protocol
309 clib_warning ("unknown protocol");
314 /* Totally out of translations to use... */
320 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
321 snat_protocol_t proto)
323 nat64_main_t *nm = &nat64_main;
327 for (i = 0; i < vec_len (nm->addr_pool); i++)
329 a = nm->addr_pool + i;
330 if (addr->as_u32 != a->addr.as_u32)
334 #define _(N, j, n, s) \
335 case SNAT_PROTOCOL_##N: \
336 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
338 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
339 a->busy_##n##_ports--; \
341 foreach_snat_protocol
344 clib_warning ("unknown protocol");
352 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
353 ip4_address_t * out_addr, u16 in_port,
354 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
356 nat64_main_t *nm = &nat64_main;
357 nat64_db_bib_entry_t *bibe;
358 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
359 FIB_SOURCE_PLUGIN_HI);
360 snat_protocol_t p = ip_proto_to_snat_proto (proto);
365 addr.as_u64[0] = in_addr->as_u64[0];
366 addr.as_u64[1] = in_addr->as_u64[1];
368 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
369 proto, fib_index, 1);
374 return VNET_API_ERROR_VALUE_EXIST;
376 for (i = 0; i < vec_len (nm->addr_pool); i++)
378 a = nm->addr_pool + i;
379 if (out_addr->as_u32 != a->addr.as_u32)
383 #define _(N, j, n, s) \
384 case SNAT_PROTOCOL_##N: \
385 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
387 return VNET_API_ERROR_INVALID_VALUE; \
388 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
390 if (out_port > 1024) \
391 a->busy_##n##_ports++; \
393 foreach_snat_protocol
396 memset (&addr, 0, sizeof (addr));
397 addr.ip4.as_u32 = out_addr->as_u32;
398 if (nat64_db_bib_entry_find
399 (&nm->db, &addr, 0, proto, fib_index, 0))
400 return VNET_API_ERROR_INVALID_VALUE;
405 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
406 clib_host_to_net_u16 (in_port),
407 clib_host_to_net_u16 (out_port), fib_index,
410 return VNET_API_ERROR_UNSPECIFIED;
415 return VNET_API_ERROR_NO_SUCH_ENTRY;
417 nat64_free_out_addr_and_port (out_addr, out_port, p);
418 nat64_db_bib_entry_free (&nm->db, bibe);
425 nat64_set_udp_timeout (u32 timeout)
427 nat64_main_t *nm = &nat64_main;
430 nm->udp_timeout = SNAT_UDP_TIMEOUT;
431 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
432 return VNET_API_ERROR_INVALID_VALUE;
434 nm->udp_timeout = timeout;
440 nat64_get_udp_timeout (void)
442 nat64_main_t *nm = &nat64_main;
444 return nm->udp_timeout;
448 nat64_set_icmp_timeout (u32 timeout)
450 nat64_main_t *nm = &nat64_main;
453 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
455 nm->icmp_timeout = timeout;
461 nat64_get_icmp_timeout (void)
463 nat64_main_t *nm = &nat64_main;
465 return nm->icmp_timeout;
469 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
471 nat64_main_t *nm = &nat64_main;
474 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
476 nm->tcp_trans_timeout = trans;
479 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
481 nm->tcp_est_timeout = est;
483 if (incoming_syn == 0)
484 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
486 nm->tcp_incoming_syn_timeout = incoming_syn;
492 nat64_get_tcp_trans_timeout (void)
494 nat64_main_t *nm = &nat64_main;
496 return nm->tcp_trans_timeout;
500 nat64_get_tcp_est_timeout (void)
502 nat64_main_t *nm = &nat64_main;
504 return nm->tcp_est_timeout;
508 nat64_get_tcp_incoming_syn_timeout (void)
510 nat64_main_t *nm = &nat64_main;
512 return nm->tcp_incoming_syn_timeout;
516 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
518 nat64_main_t *nm = &nat64_main;
519 u32 now = (u32) vlib_time_now (vm);
521 switch (ip_proto_to_snat_proto (ste->proto))
523 case SNAT_PROTOCOL_ICMP:
524 ste->expire = now + nm->icmp_timeout;
526 case SNAT_PROTOCOL_TCP:
528 switch (ste->tcp_state)
530 case NAT64_TCP_STATE_V4_INIT:
531 case NAT64_TCP_STATE_V6_INIT:
532 case NAT64_TCP_STATE_V4_FIN_RCV:
533 case NAT64_TCP_STATE_V6_FIN_RCV:
534 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
535 case NAT64_TCP_STATE_TRANS:
536 ste->expire = now + nm->tcp_trans_timeout;
538 case NAT64_TCP_STATE_ESTABLISHED:
539 ste->expire = now + nm->tcp_est_timeout;
545 case SNAT_PROTOCOL_UDP:
546 ste->expire = now + nm->udp_timeout;
549 ste->expire = now + nm->udp_timeout;
555 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
558 switch (ste->tcp_state)
560 case NAT64_TCP_STATE_CLOSED:
562 if (tcp->flags & TCP_FLAG_SYN)
565 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
567 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
571 case NAT64_TCP_STATE_V4_INIT:
573 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
574 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
577 case NAT64_TCP_STATE_V6_INIT:
579 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
580 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
583 case NAT64_TCP_STATE_ESTABLISHED:
585 if (tcp->flags & TCP_FLAG_FIN)
588 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
590 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
592 else if (tcp->flags & TCP_FLAG_RST)
594 ste->tcp_state = NAT64_TCP_STATE_TRANS;
598 case NAT64_TCP_STATE_V4_FIN_RCV:
600 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
601 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
604 case NAT64_TCP_STATE_V6_FIN_RCV:
606 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
607 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
610 case NAT64_TCP_STATE_TRANS:
612 if (!(tcp->flags & TCP_FLAG_RST))
613 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
622 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
624 nat64_main_t *nm = &nat64_main;
625 nat64_prefix_t *p = 0;
628 /* Verify prefix length */
629 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
631 return VNET_API_ERROR_INVALID_VALUE;
633 /* Check if tenant already have prefix */
634 for (i = 0; i < vec_len (nm->pref64); i++)
636 if (nm->pref64[i].vrf_id == vrf_id)
647 vec_add2 (nm->pref64, p, 1);
649 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
650 FIB_SOURCE_PLUGIN_HI);
654 p->prefix.as_u64[0] = prefix->as_u64[0];
655 p->prefix.as_u64[1] = prefix->as_u64[1];
661 return VNET_API_ERROR_NO_SUCH_ENTRY;
663 vec_del1 (nm->pref64, i);
670 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
672 nat64_main_t *nm = &nat64_main;
673 nat64_prefix_t *p = 0;
676 vec_foreach (p, nm->pref64)
685 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
687 nat64_main_t *nm = &nat64_main;
688 nat64_prefix_t *p, *gp = 0, *prefix = 0;
691 vec_foreach (p, nm->pref64)
693 if (p->fib_index == fib_index)
699 if (p->fib_index == 0)
710 memcpy (ip6, &p->prefix, p->plen);
714 ip6->as_u32[1] = ip4->as_u32;
717 ip6->as_u8[5] = ip4->as_u8[0];
718 ip6->as_u8[6] = ip4->as_u8[1];
719 ip6->as_u8[7] = ip4->as_u8[2];
720 ip6->as_u8[9] = ip4->as_u8[3];
723 ip6->as_u8[6] = ip4->as_u8[0];
724 ip6->as_u8[7] = ip4->as_u8[1];
725 ip6->as_u8[9] = ip4->as_u8[2];
726 ip6->as_u8[10] = ip4->as_u8[3];
729 ip6->as_u8[7] = ip4->as_u8[0];
730 ip6->as_u8[9] = ip4->as_u8[1];
731 ip6->as_u8[10] = ip4->as_u8[2];
732 ip6->as_u8[11] = ip4->as_u8[3];
735 ip6->as_u8[9] = ip4->as_u8[0];
736 ip6->as_u8[10] = ip4->as_u8[1];
737 ip6->as_u8[11] = ip4->as_u8[2];
738 ip6->as_u8[12] = ip4->as_u8[3];
741 ip6->as_u32[3] = ip4->as_u32;
744 clib_warning ("invalid prefix length");
750 memcpy (ip6, well_known_prefix, 16);
751 ip6->as_u32[3] = ip4->as_u32;
756 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
758 nat64_main_t *nm = &nat64_main;
759 nat64_prefix_t *p, *gp = 0;
763 vec_foreach (p, nm->pref64)
765 if (p->fib_index == fib_index)
787 ip4->as_u32 = ip6->as_u32[1];
790 ip4->as_u8[0] = ip6->as_u8[5];
791 ip4->as_u8[1] = ip6->as_u8[6];
792 ip4->as_u8[2] = ip6->as_u8[7];
793 ip4->as_u8[3] = ip6->as_u8[9];
796 ip4->as_u8[0] = ip6->as_u8[6];
797 ip4->as_u8[1] = ip6->as_u8[7];
798 ip4->as_u8[2] = ip6->as_u8[9];
799 ip4->as_u8[3] = ip6->as_u8[10];
802 ip4->as_u8[0] = ip6->as_u8[7];
803 ip4->as_u8[1] = ip6->as_u8[9];
804 ip4->as_u8[2] = ip6->as_u8[10];
805 ip4->as_u8[3] = ip6->as_u8[11];
808 ip4->as_u8[0] = ip6->as_u8[9];
809 ip4->as_u8[1] = ip6->as_u8[10];
810 ip4->as_u8[2] = ip6->as_u8[11];
811 ip4->as_u8[3] = ip6->as_u8[12];
814 ip4->as_u32 = ip6->as_u32[3];
817 clib_warning ("invalid prefix length");
823 * @brief The 'nat64-expire-walk' process's main loop.
825 * Check expire time for NAT64 sessions.
828 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
831 nat64_main_t *nm = &nat64_main;
833 while (!nm->is_disabled)
835 vlib_process_wait_for_event_or_clock (vm, 10.0);
836 vlib_process_get_events (vm, NULL);
837 u32 now = (u32) vlib_time_now (vm);
839 nad64_db_st_free_expired (&nm->db, now);
845 static vlib_node_registration_t nat64_expire_walk_node;
848 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
849 .function = nat64_expire_walk_fn,
850 .type = VLIB_NODE_TYPE_PROCESS,
851 .name = "nat64-expire-walk",
856 * fd.io coding-style-patch-verification: ON
859 * eval: (c-set-style "gnu")