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 <nat/nat_reass.h>
23 #include <nat/nat_inlines.h>
24 #include <vnet/fib/ip4_fib.h>
25 #include <vppinfra/crc32.h>
28 nat64_main_t nat64_main;
32 /* Hook up input features */
33 VNET_FEATURE_INIT (nat64_in2out, static) = {
34 .arc_name = "ip6-unicast",
35 .node_name = "nat64-in2out",
36 .runs_before = VNET_FEATURES ("ip6-lookup"),
38 VNET_FEATURE_INIT (nat64_out2in, static) = {
39 .arc_name = "ip4-unicast",
40 .node_name = "nat64-out2in",
41 .runs_before = VNET_FEATURES ("ip4-lookup"),
43 VNET_FEATURE_INIT (nat64_in2out_handoff, static) = {
44 .arc_name = "ip6-unicast",
45 .node_name = "nat64-in2out-handoff",
46 .runs_before = VNET_FEATURES ("ip6-lookup"),
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"),
55 static u8 well_known_prefix[] = {
56 0x00, 0x64, 0xff, 0x9b,
57 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00
65 nat64_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque,
67 ip4_address_t * address,
69 u32 if_address_index, u32 is_delete)
71 nat64_main_t *nm = &nat64_main;
74 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
76 if (sw_if_index == nm->auto_add_sw_if_indices[i])
80 /* Don't trip over lease renewal, static config */
81 for (j = 0; j < vec_len (nm->addr_pool); j++)
82 if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
85 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
91 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
100 nat64_get_worker_in2out (ip6_address_t * addr)
102 nat64_main_t *nm = &nat64_main;
103 snat_main_t *sm = nm->sm;
104 u32 next_worker_index = nm->sm->first_worker_index;
107 #ifdef clib_crc32c_uses_intrinsics
108 hash = clib_crc32c ((u8 *) addr->as_u32, 16);
110 u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
111 hash = clib_xxhash (tmp);
114 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
115 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
117 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
119 return next_worker_index;
123 nat64_get_worker_out2in (ip4_header_t * ip)
125 nat64_main_t *nm = &nat64_main;
126 snat_main_t *sm = nm->sm;
131 proto = ip_proto_to_snat_proto (ip->protocol);
132 udp = ip4_next_header (ip);
133 port = udp->dst_port;
136 if (PREDICT_FALSE (ip4_is_fragment (ip)))
138 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
139 return vlib_get_thread_index ();
141 nat_reass_ip4_t *reass;
142 reass = nat_ip4_reass_find (ip->src_address, ip->dst_address,
143 ip->fragment_id, ip->protocol);
145 if (reass && (reass->thread_index != (u32) ~ 0))
146 return reass->thread_index;
148 if (ip4_is_first_fragment (ip))
151 nat_ip4_reass_create (ip->src_address, ip->dst_address,
152 ip->fragment_id, ip->protocol);
156 port = clib_net_to_host_u16 (port);
158 reass->thread_index =
159 nm->sm->first_worker_index +
160 ((port - 1024) / sm->port_per_thread);
162 reass->thread_index = vlib_get_thread_index ();
163 return reass->thread_index;
166 return vlib_get_thread_index ();
170 /* unknown protocol */
171 if (PREDICT_FALSE (proto == ~0))
174 ip46_address_t daddr;
175 nat64_db_bib_entry_t *bibe;
177 clib_memset (&daddr, 0, sizeof (daddr));
178 daddr.ip4.as_u32 = ip->dst_address.as_u32;
181 vec_foreach (db, nm->db)
183 bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
185 return (u32) (db - nm->db);
188 return vlib_get_thread_index ();
192 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
194 icmp46_header_t *icmp = (icmp46_header_t *) udp;
195 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
196 if (!icmp_is_error_message (icmp))
197 port = echo->identifier;
200 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
201 proto = ip_proto_to_snat_proto (inner_ip->protocol);
202 void *l4_header = ip4_next_header (inner_ip);
205 case SNAT_PROTOCOL_ICMP:
206 icmp = (icmp46_header_t *) l4_header;
207 echo = (icmp_echo_header_t *) (icmp + 1);
208 port = echo->identifier;
210 case SNAT_PROTOCOL_UDP:
211 case SNAT_PROTOCOL_TCP:
212 port = ((tcp_udp_header_t *) l4_header)->src_port;
215 return vlib_get_thread_index ();
220 /* worker by outside port (TCP/UDP) */
221 port = clib_net_to_host_u16 (port);
223 return nm->sm->first_worker_index + ((port - 1024) / sm->port_per_thread);
225 return vlib_get_thread_index ();
229 nat64_init (vlib_main_t * vm)
231 nat64_main_t *nm = &nat64_main;
232 vlib_thread_main_t *tm = vlib_get_thread_main ();
233 ip4_add_del_interface_address_callback_t cb4;
234 ip4_main_t *im = &ip4_main;
238 vec_validate (nm->db, tm->n_vlib_mains - 1);
240 nm->fq_in2out_index = ~0;
241 nm->fq_out2in_index = ~0;
243 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
244 nm->error_node_index = node->index;
246 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out");
247 nm->in2out_node_index = node->index;
249 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-slowpath");
250 nm->in2out_slowpath_node_index = node->index;
252 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-reass");
253 nm->in2out_reass_node_index = node->index;
255 node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in");
256 nm->out2in_node_index = node->index;
258 node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in-reass");
259 nm->out2in_reass_node_index = node->index;
261 /* set session timeouts to default values */
262 nm->udp_timeout = SNAT_UDP_TIMEOUT;
263 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
264 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
265 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
267 nm->total_enabled_count = 0;
269 /* Set up the interface address add/del callback */
270 cb4.function = nat64_ip4_add_del_interface_address_cb;
271 cb4.function_opaque = 0;
272 vec_add1 (im->add_del_interface_address_callbacks, cb4);
276 nm->total_bibs.name = "total-bibs";
277 nm->total_bibs.stat_segment_name = "/nat64/total-bibs";
278 vlib_validate_simple_counter (&nm->total_bibs, 0);
279 vlib_zero_simple_counter (&nm->total_bibs, 0);
280 nm->total_sessions.name = "total-sessions";
281 nm->total_sessions.stat_segment_name = "/nat64/total-sessions";
282 vlib_validate_simple_counter (&nm->total_sessions, 0);
283 vlib_zero_simple_counter (&nm->total_sessions, 0);
288 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
289 ip4_address_t * addr, u16 port,
293 nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
296 nat64_main_t *nm = &nat64_main;
299 nm->bib_buckets = bib_buckets;
300 nm->bib_memory_size = bib_memory_size;
301 nm->st_buckets = st_buckets;
302 nm->st_memory_size = st_memory_size;
305 vec_foreach (db, nm->db)
307 if (nat64_db_init (db, bib_buckets, bib_memory_size, st_buckets,
308 st_memory_size, nat64_free_out_addr_and_port))
309 nat_log_err ("NAT64 DB init failed");
315 nat64_add_del_pool_addr (u32 thread_index,
316 ip4_address_t * addr, u32 vrf_id, u8 is_add)
318 nat64_main_t *nm = &nat64_main;
319 snat_address_t *a = 0;
320 snat_interface_t *interface;
323 vlib_thread_main_t *tm = vlib_get_thread_main ();
325 /* Check if address already exists */
326 for (i = 0; i < vec_len (nm->addr_pool); i++)
328 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
330 a = nm->addr_pool + i;
338 return VNET_API_ERROR_VALUE_EXIST;
340 vec_add2 (nm->addr_pool, a, 1);
345 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
346 FIB_SOURCE_PLUGIN_HI);
347 #define _(N, id, n, s) \
348 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
349 a->busy_##n##_ports = 0; \
350 vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
351 foreach_snat_protocol
357 return VNET_API_ERROR_NO_SUCH_ENTRY;
359 if (a->fib_index != ~0)
360 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6,
361 FIB_SOURCE_PLUGIN_HI);
362 /* Delete sessions using address */
364 vec_foreach (db, nm->db)
366 nat64_db_free_out_addr (thread_index, db, &a->addr);
367 vlib_set_simple_counter (&nm->total_bibs, db - nm->db, 0,
368 db->bib.bib_entries_num);
369 vlib_set_simple_counter (&nm->total_sessions, db - nm->db, 0,
370 db->st.st_entries_num);
372 #define _(N, id, n, s) \
373 clib_bitmap_free (a->busy_##n##_port_bitmap);
374 foreach_snat_protocol
377 vec_del1 (nm->addr_pool, i);
380 /* Add/del external address to FIB */
382 pool_foreach (interface, nm->interfaces,
384 if (nat_interface_is_inside(interface))
387 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
396 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
398 nat64_main_t *nm = &nat64_main;
399 snat_address_t *a = 0;
402 vec_foreach (a, nm->addr_pool)
411 nat64_add_interface_address (u32 sw_if_index, int is_add)
413 nat64_main_t *nm = &nat64_main;
414 ip4_main_t *ip4_main = nm->ip4_main;
415 ip4_address_t *first_int_addr;
418 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
420 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
422 if (nm->auto_add_sw_if_indices[i] == sw_if_index)
425 return VNET_API_ERROR_VALUE_EXIST;
428 /* if have address remove it */
430 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
431 first_int_addr, ~0, 0);
432 vec_del1 (nm->auto_add_sw_if_indices, i);
439 return VNET_API_ERROR_NO_SUCH_ENTRY;
441 /* add to the auto-address list */
442 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
444 /* If the address is already bound - or static - add it now */
446 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
447 first_int_addr, ~0, 1);
453 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
455 nat64_main_t *nm = &nat64_main;
456 snat_interface_t *interface = 0, *i;
458 const char *feature_name, *arc_name;
460 /* Check if interface already exists */
462 pool_foreach (i, nm->interfaces,
464 if (i->sw_if_index == sw_if_index)
477 pool_get (nm->interfaces, interface);
478 interface->sw_if_index = sw_if_index;
479 interface->flags = 0;
482 interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
484 interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
486 nm->total_enabled_count++;
487 vlib_process_signal_event (nm->sm->vlib_main,
488 nm->nat64_expire_walk_node_index,
489 NAT64_CLEANER_RESCHEDULE, 0);
495 return VNET_API_ERROR_NO_SUCH_ENTRY;
497 if ((nat_interface_is_inside (interface)
498 && nat_interface_is_outside (interface)))
500 is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE :
501 ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
503 pool_put (nm->interfaces, interface);
505 nm->total_enabled_count--;
511 vec_foreach (ap, nm->addr_pool)
512 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
516 if (nm->sm->num_workers > 1)
519 is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
520 if (nm->fq_in2out_index == ~0)
521 nm->fq_in2out_index =
522 vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
523 if (nm->fq_out2in_index == ~0)
524 nm->fq_out2in_index =
525 vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
528 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
530 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
532 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
537 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
539 nat64_main_t *nm = &nat64_main;
540 snat_interface_t *i = 0;
543 pool_foreach (i, nm->interfaces,
552 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
553 ip4_address_t * addr, u16 * port,
556 nat64_main_t *nm = &nat64_main;
557 snat_main_t *sm = nm->sm;
558 snat_session_key_t k;
559 u32 worker_index = 0;
564 if (sm->num_workers > 1)
565 worker_index = thread_index - sm->first_worker_index;
568 sm->alloc_addr_and_port (nm->addr_pool, fib_index, thread_index, &k,
569 sm->port_per_thread, worker_index);
574 addr->as_u32 = k.addr.as_u32;
581 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
582 u16 port, u8 protocol)
584 nat64_main_t *nm = &nat64_main;
587 u32 thread_index = db - nm->db;
588 snat_protocol_t proto = ip_proto_to_snat_proto (protocol);
589 u16 port_host_byte_order = clib_net_to_host_u16 (port);
591 for (i = 0; i < vec_len (nm->addr_pool); i++)
593 a = nm->addr_pool + i;
594 if (addr->as_u32 != a->addr.as_u32)
598 #define _(N, j, n, s) \
599 case SNAT_PROTOCOL_##N: \
600 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
601 port_host_byte_order) == 1); \
602 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
603 a->busy_##n##_ports--; \
604 a->busy_##n##_ports_per_thread[thread_index]--; \
606 foreach_snat_protocol
609 nat_log_notice ("unknown protocol");
617 * @brief Add/delete static BIB entry in worker thread.
620 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
623 nat64_main_t *nm = &nat64_main;
624 u32 thread_index = vm->thread_index;
625 nat64_db_t *db = &nm->db[thread_index];
626 nat64_static_bib_to_update_t *static_bib;
627 nat64_db_bib_entry_t *bibe;
631 pool_foreach (static_bib, nm->static_bibs,
633 if ((static_bib->thread_index != thread_index) || (static_bib->done))
636 if (static_bib->is_add)
638 (void) nat64_db_bib_entry_create (thread_index, db,
639 &static_bib->in_addr,
640 &static_bib->out_addr,
642 static_bib->out_port,
643 static_bib->fib_index,
644 static_bib->proto, 1);
645 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
646 db->bib.bib_entries_num);
650 addr.as_u64[0] = static_bib->in_addr.as_u64[0];
651 addr.as_u64[1] = static_bib->in_addr.as_u64[1];
652 bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
654 static_bib->fib_index, 1);
657 nat64_db_bib_entry_free (thread_index, db, bibe);
658 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
659 db->bib.bib_entries_num);
660 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
661 db->st.st_entries_num);
665 static_bib->done = 1;
672 static vlib_node_registration_t nat64_static_bib_worker_node;
675 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
676 .function = nat64_static_bib_worker_fn,
677 .type = VLIB_NODE_TYPE_INPUT,
678 .state = VLIB_NODE_STATE_INTERRUPT,
679 .name = "nat64-static-bib-worker",
684 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
685 ip4_address_t * out_addr, u16 in_port,
686 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
688 nat64_main_t *nm = &nat64_main;
689 nat64_db_bib_entry_t *bibe;
690 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
691 FIB_SOURCE_PLUGIN_HI);
692 snat_protocol_t p = ip_proto_to_snat_proto (proto);
696 u32 thread_index = 0;
698 nat64_static_bib_to_update_t *static_bib;
699 vlib_main_t *worker_vm;
700 u32 *to_be_free = 0, *index;
702 if (nm->sm->num_workers > 1)
704 thread_index = nat64_get_worker_in2out (in_addr);
705 db = &nm->db[thread_index];
708 db = &nm->db[nm->sm->num_workers];
710 addr.as_u64[0] = in_addr->as_u64[0];
711 addr.as_u64[1] = in_addr->as_u64[1];
713 nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
714 proto, fib_index, 1);
719 return VNET_API_ERROR_VALUE_EXIST;
721 /* outside port must be assigned to same thread as internall address */
722 if ((out_port > 1024) && (nm->sm->num_workers > 1))
724 if (thread_index != ((out_port - 1024) / nm->sm->port_per_thread))
725 return VNET_API_ERROR_INVALID_VALUE_2;
728 for (i = 0; i < vec_len (nm->addr_pool); i++)
730 a = nm->addr_pool + i;
731 if (out_addr->as_u32 != a->addr.as_u32)
735 #define _(N, j, n, s) \
736 case SNAT_PROTOCOL_##N: \
737 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
739 return VNET_API_ERROR_INVALID_VALUE; \
740 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
742 if (out_port > 1024) \
744 a->busy_##n##_ports++; \
745 a->busy_##n##_ports_per_thread[thread_index]++; \
748 foreach_snat_protocol
751 clib_memset (&addr, 0, sizeof (addr));
752 addr.ip4.as_u32 = out_addr->as_u32;
753 if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
754 return VNET_API_ERROR_INVALID_VALUE;
758 if (!nm->sm->num_workers)
761 nat64_db_bib_entry_create (thread_index, db, in_addr, out_addr,
762 clib_host_to_net_u16 (in_port),
763 clib_host_to_net_u16 (out_port),
764 fib_index, proto, 1);
766 return VNET_API_ERROR_UNSPECIFIED;
768 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
769 db->bib.bib_entries_num);
775 return VNET_API_ERROR_NO_SUCH_ENTRY;
777 if (!nm->sm->num_workers)
779 nat64_db_bib_entry_free (thread_index, db, bibe);
780 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
781 db->bib.bib_entries_num);
785 if (nm->sm->num_workers)
788 pool_foreach (static_bib, nm->static_bibs,
790 if (static_bib->done)
791 vec_add1 (to_be_free, static_bib - nm->static_bibs);
793 vec_foreach (index, to_be_free)
794 pool_put_index (nm->static_bibs, index[0]);
796 vec_free (to_be_free);
797 pool_get (nm->static_bibs, static_bib);
798 static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
799 static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
800 static_bib->in_port = clib_host_to_net_u16 (in_port);
801 static_bib->out_addr.as_u32 = out_addr->as_u32;
802 static_bib->out_port = clib_host_to_net_u16 (out_port);
803 static_bib->fib_index = fib_index;
804 static_bib->proto = proto;
805 static_bib->is_add = is_add;
806 static_bib->thread_index = thread_index;
807 static_bib->done = 0;
808 worker_vm = vlib_mains[thread_index];
810 vlib_node_set_interrupt_pending (worker_vm,
811 nat64_static_bib_worker_node.index);
813 return VNET_API_ERROR_UNSPECIFIED;
820 nat64_set_udp_timeout (u32 timeout)
822 nat64_main_t *nm = &nat64_main;
825 nm->udp_timeout = SNAT_UDP_TIMEOUT;
827 nm->udp_timeout = timeout;
833 nat64_get_udp_timeout (void)
835 nat64_main_t *nm = &nat64_main;
837 return nm->udp_timeout;
841 nat64_set_icmp_timeout (u32 timeout)
843 nat64_main_t *nm = &nat64_main;
846 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
848 nm->icmp_timeout = timeout;
854 nat64_get_icmp_timeout (void)
856 nat64_main_t *nm = &nat64_main;
858 return nm->icmp_timeout;
862 nat64_set_tcp_timeouts (u32 trans, u32 est)
864 nat64_main_t *nm = &nat64_main;
867 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
869 nm->tcp_trans_timeout = trans;
872 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
874 nm->tcp_est_timeout = est;
880 nat64_get_tcp_trans_timeout (void)
882 nat64_main_t *nm = &nat64_main;
884 return nm->tcp_trans_timeout;
888 nat64_get_tcp_est_timeout (void)
890 nat64_main_t *nm = &nat64_main;
892 return nm->tcp_est_timeout;
896 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
898 nat64_main_t *nm = &nat64_main;
899 u32 now = (u32) vlib_time_now (vm);
901 switch (ip_proto_to_snat_proto (ste->proto))
903 case SNAT_PROTOCOL_ICMP:
904 ste->expire = now + nm->icmp_timeout;
906 case SNAT_PROTOCOL_TCP:
908 switch (ste->tcp_state)
910 case NAT64_TCP_STATE_V4_INIT:
911 case NAT64_TCP_STATE_V6_INIT:
912 case NAT64_TCP_STATE_V4_FIN_RCV:
913 case NAT64_TCP_STATE_V6_FIN_RCV:
914 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
915 case NAT64_TCP_STATE_TRANS:
916 ste->expire = now + nm->tcp_trans_timeout;
918 case NAT64_TCP_STATE_ESTABLISHED:
919 ste->expire = now + nm->tcp_est_timeout;
925 case SNAT_PROTOCOL_UDP:
926 ste->expire = now + nm->udp_timeout;
929 ste->expire = now + nm->udp_timeout;
935 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
938 switch (ste->tcp_state)
940 case NAT64_TCP_STATE_CLOSED:
942 if (tcp->flags & TCP_FLAG_SYN)
945 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
947 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
951 case NAT64_TCP_STATE_V4_INIT:
953 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
954 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
957 case NAT64_TCP_STATE_V6_INIT:
959 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
960 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
963 case NAT64_TCP_STATE_ESTABLISHED:
965 if (tcp->flags & TCP_FLAG_FIN)
968 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
970 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
972 else if (tcp->flags & TCP_FLAG_RST)
974 ste->tcp_state = NAT64_TCP_STATE_TRANS;
978 case NAT64_TCP_STATE_V4_FIN_RCV:
980 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
981 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
984 case NAT64_TCP_STATE_V6_FIN_RCV:
986 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
987 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
990 case NAT64_TCP_STATE_TRANS:
992 if (!(tcp->flags & TCP_FLAG_RST))
993 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
1002 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
1004 nat64_main_t *nm = &nat64_main;
1005 nat64_prefix_t *p = 0;
1008 /* Verify prefix length */
1009 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
1011 return VNET_API_ERROR_INVALID_VALUE;
1013 /* Check if tenant already have prefix */
1014 for (i = 0; i < vec_len (nm->pref64); i++)
1016 if (nm->pref64[i].vrf_id == vrf_id)
1027 vec_add2 (nm->pref64, p, 1);
1029 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
1030 FIB_SOURCE_PLUGIN_HI);
1034 p->prefix.as_u64[0] = prefix->as_u64[0];
1035 p->prefix.as_u64[1] = prefix->as_u64[1];
1041 return VNET_API_ERROR_NO_SUCH_ENTRY;
1043 vec_del1 (nm->pref64, i);
1050 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
1052 nat64_main_t *nm = &nat64_main;
1053 nat64_prefix_t *p = 0;
1056 vec_foreach (p, nm->pref64)
1065 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1067 nat64_main_t *nm = &nat64_main;
1068 nat64_prefix_t *p, *gp = 0, *prefix = 0;
1071 vec_foreach (p, nm->pref64)
1073 if (p->fib_index == fib_index)
1079 if (p->fib_index == 0)
1089 clib_memcpy_fast (ip6, &p->prefix, sizeof (ip6_address_t));
1093 ip6->as_u32[1] = ip4->as_u32;
1096 ip6->as_u8[5] = ip4->as_u8[0];
1097 ip6->as_u8[6] = ip4->as_u8[1];
1098 ip6->as_u8[7] = ip4->as_u8[2];
1099 ip6->as_u8[9] = ip4->as_u8[3];
1102 ip6->as_u8[6] = ip4->as_u8[0];
1103 ip6->as_u8[7] = ip4->as_u8[1];
1104 ip6->as_u8[9] = ip4->as_u8[2];
1105 ip6->as_u8[10] = ip4->as_u8[3];
1108 ip6->as_u8[7] = ip4->as_u8[0];
1109 ip6->as_u8[9] = ip4->as_u8[1];
1110 ip6->as_u8[10] = ip4->as_u8[2];
1111 ip6->as_u8[11] = ip4->as_u8[3];
1114 ip6->as_u8[9] = ip4->as_u8[0];
1115 ip6->as_u8[10] = ip4->as_u8[1];
1116 ip6->as_u8[11] = ip4->as_u8[2];
1117 ip6->as_u8[12] = ip4->as_u8[3];
1120 ip6->as_u32[3] = ip4->as_u32;
1123 nat_log_notice ("invalid prefix length");
1129 clib_memcpy_fast (ip6, well_known_prefix, sizeof (ip6_address_t));
1130 ip6->as_u32[3] = ip4->as_u32;
1135 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1137 nat64_main_t *nm = &nat64_main;
1138 nat64_prefix_t *p, *gp = 0;
1142 vec_foreach (p, nm->pref64)
1144 if (p->fib_index == fib_index)
1166 ip4->as_u32 = ip6->as_u32[1];
1169 ip4->as_u8[0] = ip6->as_u8[5];
1170 ip4->as_u8[1] = ip6->as_u8[6];
1171 ip4->as_u8[2] = ip6->as_u8[7];
1172 ip4->as_u8[3] = ip6->as_u8[9];
1175 ip4->as_u8[0] = ip6->as_u8[6];
1176 ip4->as_u8[1] = ip6->as_u8[7];
1177 ip4->as_u8[2] = ip6->as_u8[9];
1178 ip4->as_u8[3] = ip6->as_u8[10];
1181 ip4->as_u8[0] = ip6->as_u8[7];
1182 ip4->as_u8[1] = ip6->as_u8[9];
1183 ip4->as_u8[2] = ip6->as_u8[10];
1184 ip4->as_u8[3] = ip6->as_u8[11];
1187 ip4->as_u8[0] = ip6->as_u8[9];
1188 ip4->as_u8[1] = ip6->as_u8[10];
1189 ip4->as_u8[2] = ip6->as_u8[11];
1190 ip4->as_u8[3] = ip6->as_u8[12];
1193 ip4->as_u32 = ip6->as_u32[3];
1196 nat_log_notice ("invalid prefix length");
1202 * @brief Per worker process checking expire time for NAT64 sessions.
1205 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1208 nat64_main_t *nm = &nat64_main;
1209 u32 thread_index = vm->thread_index;
1210 nat64_db_t *db = &nm->db[thread_index];
1211 u32 now = (u32) vlib_time_now (vm);
1213 nad64_db_st_free_expired (thread_index, db, now);
1214 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
1215 db->bib.bib_entries_num);
1216 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1217 db->st.st_entries_num);
1222 static vlib_node_registration_t nat64_expire_worker_walk_node;
1225 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1226 .function = nat64_expire_worker_walk_fn,
1227 .type = VLIB_NODE_TYPE_INPUT,
1228 .state = VLIB_NODE_STATE_INTERRUPT,
1229 .name = "nat64-expire-worker-walk",
1233 static vlib_node_registration_t nat64_expire_walk_node;
1236 * @brief Centralized process to drive per worker expire walk.
1239 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1242 nat64_main_t *nm = &nat64_main;
1243 vlib_main_t **worker_vms = 0, *worker_vm;
1245 uword event_type, *event_data = 0;
1247 nm->nat64_expire_walk_node_index = nat64_expire_walk_node.index;
1249 if (vec_len (vlib_mains) == 0)
1250 vec_add1 (worker_vms, vm);
1253 for (i = 0; i < vec_len (vlib_mains); i++)
1255 worker_vm = vlib_mains[i];
1257 vec_add1 (worker_vms, worker_vm);
1263 if (nm->total_enabled_count)
1265 vlib_process_wait_for_event_or_clock (vm, 10.0);
1266 event_type = vlib_process_get_events (vm, &event_data);
1270 vlib_process_wait_for_event (vm);
1271 event_type = vlib_process_get_events (vm, &event_data);
1278 case NAT64_CLEANER_RESCHEDULE:
1281 nat_log_notice ("unknown event %u", event_type);
1285 for (i = 0; i < vec_len (worker_vms); i++)
1287 worker_vm = worker_vms[i];
1288 vlib_node_set_interrupt_pending (worker_vm,
1289 nat64_expire_worker_walk_node.index);
1297 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
1298 .function = nat64_expire_walk_fn,
1299 .type = VLIB_NODE_TYPE_PROCESS,
1300 .name = "nat64-expire-walk",
1305 * fd.io coding-style-patch-verification: ON
1308 * eval: (c-set-style "gnu")