2 * Copyright (c) 2020 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.
16 #include <vppinfra/crc32.h>
17 #include <vnet/fib/ip4_fib.h>
19 #include <vnet/ip/reass/ip4_sv_reass.h>
20 #include <vnet/ip/reass/ip6_sv_reass.h>
21 #include <vnet/plugin/plugin.h>
22 #include <vpp/app/version.h>
24 #include <nat/nat64/nat64.h>
26 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"),
34 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
36 VNET_FEATURE_INIT (nat64_out2in, static) = {
37 .arc_name = "ip4-unicast",
38 .node_name = "nat64-out2in",
39 .runs_before = VNET_FEATURES ("ip4-lookup"),
40 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
42 VNET_FEATURE_INIT (nat64_in2out_handoff, static) = {
43 .arc_name = "ip6-unicast",
44 .node_name = "nat64-in2out-handoff",
45 .runs_before = VNET_FEATURES ("ip6-lookup"),
46 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
48 VNET_FEATURE_INIT (nat64_out2in_handoff, static) = {
49 .arc_name = "ip4-unicast",
50 .node_name = "nat64-out2in-handoff",
51 .runs_before = VNET_FEATURES ("ip4-lookup"),
52 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
54 VLIB_PLUGIN_REGISTER () = {
55 .version = VPP_BUILD_VER,
56 .description = "NAT64",
58 static u8 well_known_prefix[] = {
59 0x00, 0x64, 0xff, 0x9b,
60 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00
66 #define nat_elog_str(_str) \
69 ELOG_TYPE_DECLARE (e) = \
71 .format = "nat-msg " _str, \
74 ELOG_DATA (&vlib_global_main.elog_main, e); \
78 nat64_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque,
80 ip4_address_t * address,
82 u32 if_address_index, u32 is_delete)
84 nat64_main_t *nm = &nat64_main;
87 if (plugin_enabled () == 0)
90 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
92 if (sw_if_index == nm->auto_add_sw_if_indices[i])
96 /* Don't trip over lease renewal, static config */
97 for (j = 0; j < vec_len (nm->addr_pool); j++)
98 if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
101 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
107 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
116 nat64_get_worker_in2out (ip6_address_t * addr)
118 nat64_main_t *nm = &nat64_main;
119 u32 next_worker_index = nm->first_worker_index;
122 #ifdef clib_crc32c_uses_intrinsics
123 hash = clib_crc32c ((u8 *) addr->as_u32, 16);
125 u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
126 hash = clib_xxhash (tmp);
129 if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
130 next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
132 next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
134 return next_worker_index;
138 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip)
140 nat64_main_t *nm = &nat64_main;
145 proto = ip_proto_to_nat_proto (ip->protocol);
146 udp = ip4_next_header (ip);
147 port = udp->dst_port;
149 /* unknown protocol */
150 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
153 ip46_address_t daddr;
154 nat64_db_bib_entry_t *bibe;
156 clib_memset (&daddr, 0, sizeof (daddr));
157 daddr.ip4.as_u32 = ip->dst_address.as_u32;
160 vec_foreach (db, nm->db)
162 bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
164 return (u32) (db - nm->db);
167 return vlib_get_thread_index ();
171 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
173 icmp46_header_t *icmp = (icmp46_header_t *) udp;
174 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
175 if (!icmp_type_is_error_message
176 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
177 port = vnet_buffer (b)->ip.reass.l4_src_port;
180 /* if error message, then it's not fragmented and we can access it */
181 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
182 proto = ip_proto_to_nat_proto (inner_ip->protocol);
183 void *l4_header = ip4_next_header (inner_ip);
186 case NAT_PROTOCOL_ICMP:
187 icmp = (icmp46_header_t *) l4_header;
188 echo = (icmp_echo_header_t *) (icmp + 1);
189 port = echo->identifier;
191 case NAT_PROTOCOL_UDP:
192 case NAT_PROTOCOL_TCP:
193 port = ((tcp_udp_header_t *) l4_header)->src_port;
196 return vlib_get_thread_index ();
201 /* worker by outside port (TCP/UDP) */
202 port = clib_net_to_host_u16 (port);
204 return nm->first_worker_index + ((port - 1024) / nm->port_per_thread);
206 return vlib_get_thread_index ();
210 nat64_init (vlib_main_t * vm)
212 nat64_main_t *nm = &nat64_main;
213 vlib_thread_main_t *tm = vlib_get_thread_main ();
214 ip4_add_del_interface_address_callback_t cb4;
217 clib_memset (nm, 0, sizeof (*nm));
219 nm->ip4_main = &ip4_main;
220 nm->log_class = vlib_log_register_class ("nat64", 0);
222 nm->port_per_thread = 0xffff - 1024;
224 nm->fq_in2out_index = ~0;
225 nm->fq_out2in_index = ~0;
227 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
228 nm->error_node_index = node->index;
229 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out");
230 nm->in2out_node_index = node->index;
231 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-slowpath");
232 nm->in2out_slowpath_node_index = node->index;
233 node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in");
234 nm->out2in_node_index = node->index;
236 node = vlib_get_node_by_name (vm, (u8 *) "nat64-expire-worker-walk");
237 nm->expire_worker_walk_node_index = node->index;
239 nm->fib_src_hi = fib_source_allocate ("nat64-hi",
240 FIB_SOURCE_PRIORITY_HI,
241 FIB_SOURCE_BH_SIMPLE);
242 nm->fib_src_low = fib_source_allocate ("nat64-low",
243 FIB_SOURCE_PRIORITY_LOW,
244 FIB_SOURCE_BH_SIMPLE);
246 // set protocol timeouts to defaults
247 nat64_reset_timeouts ();
249 /* Set up the interface address add/del callback */
250 cb4.function = nat64_ip4_add_del_interface_address_cb;
251 cb4.function_opaque = 0;
252 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cb4);
255 nm->total_bibs.name = "total-bibs";
256 nm->total_bibs.stat_segment_name = "/nat64/total-bibs";
257 vlib_validate_simple_counter (&nm->total_bibs, 0);
258 vlib_zero_simple_counter (&nm->total_bibs, 0);
259 nm->total_sessions.name = "total-sessions";
260 nm->total_sessions.stat_segment_name = "/nat64/total-sessions";
261 vlib_validate_simple_counter (&nm->total_sessions, 0);
262 vlib_zero_simple_counter (&nm->total_sessions, 0);
264 uword *p = hash_get_mem (tm->thread_registrations_by_name, "workers");
267 vlib_thread_registration_t *tr;
268 tr = (vlib_thread_registration_t *) p[0];
271 nm->num_workers = tr->count;
272 nm->first_worker_index = tr->first_index;
276 if (nm->num_workers > 1)
281 for (i = 0; i < nm->num_workers; i++)
282 bitmap = clib_bitmap_set (bitmap, i, 1);
285 clib_bitmap_foreach (i, bitmap,
287 vec_add1(nm->workers, i);
291 clib_bitmap_free (bitmap);
293 nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
296 // TODO: ipfix needs to be separated from NAT base plugin
297 /* Init IPFIX logging */
298 //snat_ipfix_logging_init (vm);
301 nm->counters.in2out.x.name = #x; \
302 nm->counters.in2out.x.stat_segment_name = "/nat64/in2out/" #x; \
303 nm->counters.out2in.x.name = #x; \
304 nm->counters.out2in.x.stat_segment_name = "/nat64/out2in/" #x;
307 return nat64_api_hookup (vm);
310 VLIB_INIT_FUNCTION (nat64_init);
312 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
313 ip4_address_t * addr, u16 port,
317 nat64_init_hash (nat64_config_t c)
319 vlib_thread_main_t *tm = vlib_get_thread_main ();
320 nat64_main_t *nm = &nat64_main;
324 vec_validate (nm->db, tm->n_vlib_mains - 1);
327 vec_foreach (db, nm->db)
329 if (nat64_db_init (db, c, nat64_free_out_addr_and_port))
331 nat64_log_err ("NAT64 DB init failed");
343 nat64_main_t *nm = &nat64_main;
348 vec_foreach (db, nm->db)
350 if (nat64_db_free (db))
352 nat64_log_err ("NAT64 DB free failed");
364 nat64_add_del_pool_addr (u32 thread_index,
365 ip4_address_t * addr, u32 vrf_id, u8 is_add)
367 nat64_main_t *nm = &nat64_main;
368 nat64_address_t *a = 0;
369 nat64_interface_t *interface;
372 vlib_thread_main_t *tm = vlib_get_thread_main ();
374 /* Check if address already exists */
375 for (i = 0; i < vec_len (nm->addr_pool); i++)
377 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
379 a = nm->addr_pool + i;
387 return VNET_API_ERROR_VALUE_EXIST;
389 vec_add2 (nm->addr_pool, a, 1);
394 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
396 #define _(N, id, n, s) \
397 clib_memset (a->busy_##n##_port_refcounts, 0, sizeof(a->busy_##n##_port_refcounts)); \
398 a->busy_##n##_ports = 0; \
399 vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
406 return VNET_API_ERROR_NO_SUCH_ENTRY;
408 if (a->fib_index != ~0)
409 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6, nm->fib_src_hi);
410 /* Delete sessions using address */
412 vec_foreach (db, nm->db)
414 nat64_db_free_out_addr (thread_index, db, &a->addr);
415 vlib_set_simple_counter (&nm->total_bibs, db - nm->db, 0,
416 db->bib.bib_entries_num);
417 vlib_set_simple_counter (&nm->total_sessions, db - nm->db, 0,
418 db->st.st_entries_num);
421 vec_del1 (nm->addr_pool, i);
424 /* Add/del external address to FIB */
426 pool_foreach (interface, nm->interfaces,
428 if (nat64_interface_is_inside(interface))
431 nat64_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
440 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
442 nat64_main_t *nm = &nat64_main;
443 nat64_address_t *a = 0;
446 vec_foreach (a, nm->addr_pool)
455 nat64_add_interface_address (u32 sw_if_index, int is_add)
457 nat64_main_t *nm = &nat64_main;
458 ip4_main_t *ip4_main = nm->ip4_main;
459 ip4_address_t *first_int_addr;
462 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
464 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
466 if (nm->auto_add_sw_if_indices[i] == sw_if_index)
469 return VNET_API_ERROR_VALUE_EXIST;
472 /* if have address remove it */
474 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
475 first_int_addr, ~0, 0);
476 vec_del1 (nm->auto_add_sw_if_indices, i);
483 return VNET_API_ERROR_NO_SUCH_ENTRY;
485 /* add to the auto-address list */
486 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
488 /* If the address is already bound - or static - add it now */
490 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
491 first_int_addr, ~0, 1);
497 nat64_validate_counters (nat64_main_t * nm, u32 sw_if_index)
500 vlib_validate_simple_counter (&nm->counters.in2out.x, sw_if_index); \
501 vlib_zero_simple_counter (&nm->counters.in2out.x, sw_if_index); \
502 vlib_validate_simple_counter (&nm->counters.out2in.x, sw_if_index); \
503 vlib_zero_simple_counter (&nm->counters.out2in.x, sw_if_index);
509 nat64_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
512 nat64_main_t *nm = &nat64_main;
513 fib_prefix_t prefix = {
515 .fp_proto = FIB_PROTOCOL_IP4,
517 .ip4.as_u32 = addr->as_u32,
520 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
523 fib_table_entry_update_one_path (fib_index,
526 (FIB_ENTRY_FLAG_CONNECTED |
527 FIB_ENTRY_FLAG_LOCAL |
528 FIB_ENTRY_FLAG_EXCLUSIVE),
532 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
534 fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
538 nat64_interface_add_del (u32 sw_if_index, u8 is_inside, u8 is_add)
540 vlib_main_t *vm = vlib_get_main ();
541 nat64_main_t *nm = &nat64_main;
542 nat64_interface_t *interface = 0, *i;
544 const char *feature_name, *arc_name;
546 // TODO: is enabled ? we can't signal if it is not
548 /* Check if interface already exists */
550 pool_foreach (i, nm->interfaces,
552 if (i->sw_if_index == sw_if_index)
565 pool_get (nm->interfaces, interface);
566 interface->sw_if_index = sw_if_index;
567 interface->flags = 0;
568 nat64_validate_counters (nm, sw_if_index);
571 interface->flags |= NAT64_INTERFACE_FLAG_IS_INSIDE;
573 interface->flags |= NAT64_INTERFACE_FLAG_IS_OUTSIDE;
575 nm->total_enabled_count++;
576 vlib_process_signal_event (vm,
577 nm->expire_walk_node_index,
578 NAT64_CLEANER_RESCHEDULE, 0);
584 return VNET_API_ERROR_NO_SUCH_ENTRY;
586 if ((nat64_interface_is_inside (interface)
587 && nat64_interface_is_outside (interface)))
589 is_inside ? ~NAT64_INTERFACE_FLAG_IS_INSIDE :
590 ~NAT64_INTERFACE_FLAG_IS_OUTSIDE;
592 pool_put (nm->interfaces, interface);
594 nm->total_enabled_count--;
600 vec_foreach (ap, nm->addr_pool)
601 nat64_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, is_add);
605 if (nm->num_workers > 1)
608 is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
609 if (nm->fq_in2out_index == ~0)
610 nm->fq_in2out_index =
611 vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
612 if (nm->fq_out2in_index == ~0)
613 nm->fq_out2in_index =
614 vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
617 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
619 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
623 int rv = ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
629 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
634 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
639 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
641 nat64_main_t *nm = &nat64_main;
642 nat64_interface_t *i = 0;
645 pool_foreach (i, nm->interfaces,
653 // TODO: plugin independent
654 static_always_inline u16
655 nat64_random_port (u16 min, u16 max)
657 nat64_main_t *nm = &nat64_main;
661 rwide = random_u32 (&nm->random_seed);
663 if (r >= min && r <= max)
666 return min + (rwide % (max - min + 1));
669 static_always_inline int
670 nat64_alloc_addr_and_port_default (nat64_address_t * addresses,
673 nat_protocol_t proto,
674 ip4_address_t * addr,
676 u16 port_per_thread, u32 nat_thread_index)
679 nat64_address_t *a, *ga = 0;
682 for (i = 0; i < vec_len (addresses); i++)
687 #define _(N, j, n, s) \
688 case NAT_PROTOCOL_##N: \
689 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
691 if (a->fib_index == fib_index) \
695 portnum = (port_per_thread * \
696 nat_thread_index) + \
697 nat64_random_port(0, port_per_thread - 1) + 1024; \
698 if (a->busy_##n##_port_refcounts[portnum]) \
700 --a->busy_##n##_port_refcounts[portnum]; \
701 a->busy_##n##_ports_per_thread[thread_index]++; \
702 a->busy_##n##_ports++; \
704 *port = clib_host_to_net_u16(portnum); \
708 else if (a->fib_index == ~0) \
727 #define _(N, j, n, s) \
728 case NAT_PROTOCOL_##N: \
731 portnum = (port_per_thread * \
732 nat_thread_index) + \
733 nat64_random_port(0, port_per_thread - 1) + 1024; \
734 if (a->busy_##n##_port_refcounts[portnum]) \
736 ++a->busy_##n##_port_refcounts[portnum]; \
737 a->busy_##n##_ports_per_thread[thread_index]++; \
738 a->busy_##n##_ports++; \
740 *port = clib_host_to_net_u16(portnum); \
751 /* Totally out of translations to use... */
752 //snat_ipfix_logging_addresses_exhausted (thread_index, 0);
757 nat64_alloc_out_addr_and_port (u32 fib_index, nat_protocol_t proto,
758 ip4_address_t * addr, u16 * port,
761 nat64_main_t *nm = &nat64_main;
762 u32 worker_index = 0;
765 if (nm->num_workers > 1)
766 worker_index = thread_index - nm->first_worker_index;
768 rv = nat64_alloc_addr_and_port_default (nm->addr_pool, fib_index,
771 nm->port_per_thread, worker_index);
777 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
778 u16 port, u8 protocol)
780 nat64_main_t *nm = &nat64_main;
781 u32 thread_index = db - nm->db;
782 nat_protocol_t proto = ip_proto_to_nat_proto (protocol);
783 u16 port_host_byte_order = clib_net_to_host_u16 (port);
787 for (i = 0; i < vec_len (nm->addr_pool); i++)
789 a = nm->addr_pool + i;
790 if (addr->as_u32 != a->addr.as_u32)
794 #define _(N, j, n, s) \
795 case NAT_PROTOCOL_##N: \
796 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
797 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
798 a->busy_##n##_ports--; \
799 a->busy_##n##_ports_per_thread[thread_index]--; \
804 nat_elog_str ("unknown protocol");
812 * @brief Add/delete static BIB entry in worker thread.
815 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
818 nat64_main_t *nm = &nat64_main;
819 u32 thread_index = vm->thread_index;
820 nat64_db_t *db = &nm->db[thread_index];
821 nat64_static_bib_to_update_t *static_bib;
822 nat64_db_bib_entry_t *bibe;
826 pool_foreach (static_bib, nm->static_bibs,
828 if ((static_bib->thread_index != thread_index) || (static_bib->done))
831 if (static_bib->is_add)
833 (void) nat64_db_bib_entry_create (thread_index, db,
834 &static_bib->in_addr,
835 &static_bib->out_addr,
837 static_bib->out_port,
838 static_bib->fib_index,
839 static_bib->proto, 1);
840 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
841 db->bib.bib_entries_num);
845 addr.as_u64[0] = static_bib->in_addr.as_u64[0];
846 addr.as_u64[1] = static_bib->in_addr.as_u64[1];
847 bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
849 static_bib->fib_index, 1);
852 nat64_db_bib_entry_free (thread_index, db, bibe);
853 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
854 db->bib.bib_entries_num);
855 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
856 db->st.st_entries_num);
860 static_bib->done = 1;
867 static vlib_node_registration_t nat64_static_bib_worker_node;
870 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
871 .function = nat64_static_bib_worker_fn,
872 .type = VLIB_NODE_TYPE_INPUT,
873 .state = VLIB_NODE_STATE_INTERRUPT,
874 .name = "nat64-static-bib-worker",
879 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
880 ip4_address_t * out_addr, u16 in_port,
881 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
883 nat64_main_t *nm = &nat64_main;
884 nat64_db_bib_entry_t *bibe;
885 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
887 nat_protocol_t p = ip_proto_to_nat_proto (proto);
891 u32 thread_index = 0;
893 nat64_static_bib_to_update_t *static_bib;
894 vlib_main_t *worker_vm;
895 u32 *to_be_free = 0, *index;
897 if (nm->num_workers > 1)
899 thread_index = nat64_get_worker_in2out (in_addr);
900 db = &nm->db[thread_index];
903 db = &nm->db[nm->num_workers];
905 addr.as_u64[0] = in_addr->as_u64[0];
906 addr.as_u64[1] = in_addr->as_u64[1];
908 nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
909 proto, fib_index, 1);
914 return VNET_API_ERROR_VALUE_EXIST;
916 /* outside port must be assigned to same thread as internall address */
917 if ((out_port > 1024) && (nm->num_workers > 1))
919 if (thread_index != ((out_port - 1024) / nm->port_per_thread))
920 return VNET_API_ERROR_INVALID_VALUE_2;
923 for (i = 0; i < vec_len (nm->addr_pool); i++)
925 a = nm->addr_pool + i;
926 if (out_addr->as_u32 != a->addr.as_u32)
930 #define _(N, j, n, s) \
931 case NAT_PROTOCOL_##N: \
932 if (a->busy_##n##_port_refcounts[out_port]) \
933 return VNET_API_ERROR_INVALID_VALUE; \
934 ++a->busy_##n##_port_refcounts[out_port]; \
935 if (out_port > 1024) \
937 a->busy_##n##_ports++; \
938 a->busy_##n##_ports_per_thread[thread_index]++; \
944 clib_memset (&addr, 0, sizeof (addr));
945 addr.ip4.as_u32 = out_addr->as_u32;
946 if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
947 return VNET_API_ERROR_INVALID_VALUE;
951 if (!nm->num_workers)
954 nat64_db_bib_entry_create (thread_index, db, in_addr, out_addr,
955 clib_host_to_net_u16 (in_port),
956 clib_host_to_net_u16 (out_port),
957 fib_index, proto, 1);
959 return VNET_API_ERROR_UNSPECIFIED;
961 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
962 db->bib.bib_entries_num);
968 return VNET_API_ERROR_NO_SUCH_ENTRY;
970 if (!nm->num_workers)
972 nat64_db_bib_entry_free (thread_index, db, bibe);
973 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
974 db->bib.bib_entries_num);
981 pool_foreach (static_bib, nm->static_bibs,
983 if (static_bib->done)
984 vec_add1 (to_be_free, static_bib - nm->static_bibs);
986 vec_foreach (index, to_be_free)
987 pool_put_index (nm->static_bibs, index[0]);
989 vec_free (to_be_free);
990 pool_get (nm->static_bibs, static_bib);
991 static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
992 static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
993 static_bib->in_port = clib_host_to_net_u16 (in_port);
994 static_bib->out_addr.as_u32 = out_addr->as_u32;
995 static_bib->out_port = clib_host_to_net_u16 (out_port);
996 static_bib->fib_index = fib_index;
997 static_bib->proto = proto;
998 static_bib->is_add = is_add;
999 static_bib->thread_index = thread_index;
1000 static_bib->done = 0;
1001 worker_vm = vlib_mains[thread_index];
1003 vlib_node_set_interrupt_pending (worker_vm,
1004 nat64_static_bib_worker_node.index);
1006 return VNET_API_ERROR_UNSPECIFIED;
1013 nat64_set_udp_timeout (u32 timeout)
1015 nat64_main_t *nm = &nat64_main;
1018 nm->udp_timeout = NAT_UDP_TIMEOUT;
1020 nm->udp_timeout = timeout;
1026 nat64_get_udp_timeout (void)
1028 nat64_main_t *nm = &nat64_main;
1030 return nm->udp_timeout;
1034 nat64_set_icmp_timeout (u32 timeout)
1036 nat64_main_t *nm = &nat64_main;
1039 nm->icmp_timeout = NAT_ICMP_TIMEOUT;
1041 nm->icmp_timeout = timeout;
1047 nat64_reset_timeouts ()
1049 nat64_main_t *nm = &nat64_main;
1051 nm->udp_timeout = NAT_UDP_TIMEOUT;
1052 nm->icmp_timeout = NAT_ICMP_TIMEOUT;
1053 nm->tcp_est_timeout = NAT_TCP_ESTABLISHED_TIMEOUT;
1054 nm->tcp_trans_timeout = NAT_TCP_TRANSITORY_TIMEOUT;
1058 nat64_get_icmp_timeout (void)
1060 nat64_main_t *nm = &nat64_main;
1062 return nm->icmp_timeout;
1066 nat64_set_tcp_timeouts (u32 trans, u32 est)
1068 nat64_main_t *nm = &nat64_main;
1071 nm->tcp_trans_timeout = NAT_TCP_TRANSITORY_TIMEOUT;
1073 nm->tcp_trans_timeout = trans;
1076 nm->tcp_est_timeout = NAT_TCP_ESTABLISHED_TIMEOUT;
1078 nm->tcp_est_timeout = est;
1084 nat64_get_tcp_trans_timeout (void)
1086 nat64_main_t *nm = &nat64_main;
1088 return nm->tcp_trans_timeout;
1092 nat64_get_tcp_est_timeout (void)
1094 nat64_main_t *nm = &nat64_main;
1096 return nm->tcp_est_timeout;
1100 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
1102 nat64_main_t *nm = &nat64_main;
1103 u32 now = (u32) vlib_time_now (vm);
1105 switch (ip_proto_to_nat_proto (ste->proto))
1107 case NAT_PROTOCOL_ICMP:
1108 ste->expire = now + nm->icmp_timeout;
1110 case NAT_PROTOCOL_TCP:
1112 switch (ste->tcp_state)
1114 case NAT64_TCP_STATE_V4_INIT:
1115 case NAT64_TCP_STATE_V6_INIT:
1116 case NAT64_TCP_STATE_V4_FIN_RCV:
1117 case NAT64_TCP_STATE_V6_FIN_RCV:
1118 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
1119 case NAT64_TCP_STATE_TRANS:
1120 ste->expire = now + nm->tcp_trans_timeout;
1122 case NAT64_TCP_STATE_ESTABLISHED:
1123 ste->expire = now + nm->tcp_est_timeout;
1129 case NAT_PROTOCOL_UDP:
1130 ste->expire = now + nm->udp_timeout;
1133 ste->expire = now + nm->udp_timeout;
1139 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
1142 switch (ste->tcp_state)
1144 case NAT64_TCP_STATE_CLOSED:
1146 if (tcp->flags & TCP_FLAG_SYN)
1149 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
1151 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
1155 case NAT64_TCP_STATE_V4_INIT:
1157 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
1158 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1161 case NAT64_TCP_STATE_V6_INIT:
1163 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
1164 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1167 case NAT64_TCP_STATE_ESTABLISHED:
1169 if (tcp->flags & TCP_FLAG_FIN)
1172 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
1174 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
1176 else if (tcp->flags & TCP_FLAG_RST)
1178 ste->tcp_state = NAT64_TCP_STATE_TRANS;
1182 case NAT64_TCP_STATE_V4_FIN_RCV:
1184 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
1185 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
1188 case NAT64_TCP_STATE_V6_FIN_RCV:
1190 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
1191 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
1194 case NAT64_TCP_STATE_TRANS:
1196 if (!(tcp->flags & TCP_FLAG_RST))
1197 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1206 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
1208 nat64_main_t *nm = &nat64_main;
1209 nat64_prefix_t *p = 0;
1212 /* Verify prefix length */
1213 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
1215 return VNET_API_ERROR_INVALID_VALUE;
1217 /* Check if tenant already have prefix */
1218 for (i = 0; i < vec_len (nm->pref64); i++)
1220 if (nm->pref64[i].vrf_id == vrf_id)
1231 vec_add2 (nm->pref64, p, 1);
1233 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
1238 p->prefix.as_u64[0] = prefix->as_u64[0];
1239 p->prefix.as_u64[1] = prefix->as_u64[1];
1245 return VNET_API_ERROR_NO_SUCH_ENTRY;
1247 // TODO: missing fib_table_unlock ?
1249 vec_del1 (nm->pref64, i);
1256 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
1258 nat64_main_t *nm = &nat64_main;
1259 nat64_prefix_t *p = 0;
1262 vec_foreach (p, nm->pref64)
1271 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1273 nat64_main_t *nm = &nat64_main;
1274 nat64_prefix_t *p, *gp = 0, *prefix = 0;
1277 vec_foreach (p, nm->pref64)
1279 if (p->fib_index == fib_index)
1285 if (p->fib_index == 0)
1295 clib_memcpy_fast (ip6, &p->prefix, sizeof (ip6_address_t));
1299 ip6->as_u32[1] = ip4->as_u32;
1302 ip6->as_u8[5] = ip4->as_u8[0];
1303 ip6->as_u8[6] = ip4->as_u8[1];
1304 ip6->as_u8[7] = ip4->as_u8[2];
1305 ip6->as_u8[9] = ip4->as_u8[3];
1308 ip6->as_u8[6] = ip4->as_u8[0];
1309 ip6->as_u8[7] = ip4->as_u8[1];
1310 ip6->as_u8[9] = ip4->as_u8[2];
1311 ip6->as_u8[10] = ip4->as_u8[3];
1314 ip6->as_u8[7] = ip4->as_u8[0];
1315 ip6->as_u8[9] = ip4->as_u8[1];
1316 ip6->as_u8[10] = ip4->as_u8[2];
1317 ip6->as_u8[11] = ip4->as_u8[3];
1320 ip6->as_u8[9] = ip4->as_u8[0];
1321 ip6->as_u8[10] = ip4->as_u8[1];
1322 ip6->as_u8[11] = ip4->as_u8[2];
1323 ip6->as_u8[12] = ip4->as_u8[3];
1326 ip6->as_u32[3] = ip4->as_u32;
1329 nat_elog_str ("invalid prefix length");
1335 clib_memcpy_fast (ip6, well_known_prefix, sizeof (ip6_address_t));
1336 ip6->as_u32[3] = ip4->as_u32;
1341 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1343 nat64_main_t *nm = &nat64_main;
1344 nat64_prefix_t *p, *gp = 0;
1348 vec_foreach (p, nm->pref64)
1350 if (p->fib_index == fib_index)
1372 ip4->as_u32 = ip6->as_u32[1];
1375 ip4->as_u8[0] = ip6->as_u8[5];
1376 ip4->as_u8[1] = ip6->as_u8[6];
1377 ip4->as_u8[2] = ip6->as_u8[7];
1378 ip4->as_u8[3] = ip6->as_u8[9];
1381 ip4->as_u8[0] = ip6->as_u8[6];
1382 ip4->as_u8[1] = ip6->as_u8[7];
1383 ip4->as_u8[2] = ip6->as_u8[9];
1384 ip4->as_u8[3] = ip6->as_u8[10];
1387 ip4->as_u8[0] = ip6->as_u8[7];
1388 ip4->as_u8[1] = ip6->as_u8[9];
1389 ip4->as_u8[2] = ip6->as_u8[10];
1390 ip4->as_u8[3] = ip6->as_u8[11];
1393 ip4->as_u8[0] = ip6->as_u8[9];
1394 ip4->as_u8[1] = ip6->as_u8[10];
1395 ip4->as_u8[2] = ip6->as_u8[11];
1396 ip4->as_u8[3] = ip6->as_u8[12];
1399 ip4->as_u32 = ip6->as_u32[3];
1402 nat_elog_str ("invalid prefix length");
1408 * @brief Per worker process checking expire time for NAT64 sessions.
1411 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1414 nat64_main_t *nm = &nat64_main;
1415 u32 thread_index = vm->thread_index;
1419 // TODO: barier sync on plugin enabled
1420 if (plugin_enabled () == 0)
1423 db = &nm->db[thread_index];
1424 now = (u32) vlib_time_now (vm);
1426 nad64_db_st_free_expired (thread_index, db, now);
1427 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
1428 db->bib.bib_entries_num);
1429 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1430 db->st.st_entries_num);
1435 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1436 .function = nat64_expire_worker_walk_fn,
1437 .type = VLIB_NODE_TYPE_INPUT,
1438 .state = VLIB_NODE_STATE_INTERRUPT,
1439 .name = "nat64-expire-worker-walk",
1444 * @brief Centralized process to drive per worker expire walk.
1447 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1450 nat64_main_t *nm = &nat64_main;
1451 vlib_main_t **worker_vms = 0, *worker_vm;
1453 uword event_type, *event_data = 0;
1455 if (vec_len (vlib_mains) == 0)
1456 vec_add1 (worker_vms, vm);
1459 for (i = 0; i < vec_len (vlib_mains); i++)
1461 worker_vm = vlib_mains[i];
1463 vec_add1 (worker_vms, worker_vm);
1469 if (nm->total_enabled_count)
1471 vlib_process_wait_for_event_or_clock (vm, 10.0);
1472 event_type = vlib_process_get_events (vm, &event_data);
1476 vlib_process_wait_for_event (vm);
1477 event_type = vlib_process_get_events (vm, &event_data);
1484 case NAT64_CLEANER_RESCHEDULE:
1487 nat64_log_err ("unknown event %u", event_type);
1491 for (i = 0; i < vec_len (worker_vms); i++)
1493 worker_vm = worker_vms[i];
1494 vlib_node_set_interrupt_pending (worker_vm,
1495 nm->expire_worker_walk_node_index);
1503 nat64_create_expire_walk_process ()
1505 nat64_main_t *nm = &nat64_main;
1507 if (nm->expire_walk_node_index)
1509 nm->expire_walk_node_index = vlib_process_create (vlib_get_main (),
1510 "nat64-expire-walk",
1511 nat64_expire_walk_fn,
1512 16 /* stack_bytes */ );
1516 nat64_plugin_enable (nat64_config_t c)
1518 nat64_main_t *nm = &nat64_main;
1520 if (plugin_enabled () == 1)
1522 nat64_log_err ("plugin already enabled!");
1527 c.bib_buckets = 1024;
1529 if (!c.bib_memory_size)
1530 c.bib_memory_size = 128 << 20;
1533 c.st_buckets = 2048;
1535 if (!c.st_memory_size)
1536 c.st_memory_size = 256 << 20;
1540 if (nat64_init_hash (c))
1542 nat64_log_err ("initializing hashes failed!");
1546 nat64_create_expire_walk_process ();
1553 nat64_plugin_disable ()
1555 nat64_main_t *nm = &nat64_main;
1556 vnet_main_t *vnm = vnet_get_main ();
1560 nat64_interface_t *i, *interfaces;
1562 if (plugin_enabled () == 0)
1564 nat64_log_err ("plugin already disabled!");
1569 interfaces = vec_dup (nm->interfaces);
1570 vec_foreach (i, interfaces)
1572 rv = nat64_interface_add_del (i->sw_if_index, i->flags, 1);
1575 nat64_log_err ("%U %s interface del failed",
1576 unformat_vnet_sw_interface,
1577 i->flags & NAT64_INTERFACE_FLAG_IS_INSIDE ?
1578 "inside" : "outside", vnm, i->sw_if_index);
1581 vec_free (interfaces);
1582 pool_free (nm->interfaces);
1584 nat64_reset_timeouts ();
1586 if (nat64_free_hash ())
1589 nat64_log_err ("freeing hashes failed!");
1592 // TODO: based on nat64_add_del_prefix fib_table_unlock is not called
1593 vec_free (nm->pref64);
1595 if (vec_len (nm->addr_pool))
1597 vec_foreach (a, nm->addr_pool)
1599 if (a->fib_index != ~0)
1600 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6, nm->fib_src_hi);
1602 vec_free (nm->addr_pool);
1608 unformat_nat_protocol (unformat_input_t * input, va_list * args)
1610 u32 *r = va_arg (*args, u32 *);
1613 #define _(N, i, n, s) else if (unformat (input, s)) *r = NAT_PROTOCOL_##N;
1614 foreach_nat_protocol
1622 format_nat_protocol (u8 * s, va_list * args)
1624 u32 i = va_arg (*args, u32);
1629 #define _(N, j, n, str) case NAT_PROTOCOL_##N: t = (u8 *) str; break;
1630 foreach_nat_protocol
1633 s = format (s, "unknown");
1636 s = format (s, "%s", t);
1641 * fd.io coding-style-patch-verification: ON
1644 * eval: (c-set-style "gnu")