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 <vnet/fib/ip4_fib.h>
24 #include <vppinfra/crc32.h>
27 nat64_main_t nat64_main;
31 /* Hook up input features */
32 VNET_FEATURE_INIT (nat64_in2out, static) = {
33 .arc_name = "ip6-unicast",
34 .node_name = "nat64-in2out",
35 .runs_before = VNET_FEATURES ("ip6-lookup"),
37 VNET_FEATURE_INIT (nat64_out2in, static) = {
38 .arc_name = "ip4-unicast",
39 .node_name = "nat64-out2in",
40 .runs_before = VNET_FEATURES ("ip4-lookup"),
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"),
47 VNET_FEATURE_INIT (nat64_out2in_handoff, static) = {
48 .arc_name = "ip4-unicast",
49 .node_name = "nat64-out2in-handoff",
50 .runs_before = VNET_FEATURES ("ip4-lookup"),
54 static u8 well_known_prefix[] = {
55 0x00, 0x64, 0xff, 0x9b,
56 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00
64 nat64_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque,
66 ip4_address_t * address,
68 u32 if_address_index, u32 is_delete)
70 nat64_main_t *nm = &nat64_main;
73 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
75 if (sw_if_index == nm->auto_add_sw_if_indices[i])
79 /* Don't trip over lease renewal, static config */
80 for (j = 0; j < vec_len (nm->addr_pool); j++)
81 if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
84 (void) nat64_add_del_pool_addr (address, ~0, 1);
89 (void) nat64_add_del_pool_addr (address, ~0, 0);
97 nat64_get_worker_in2out (ip6_address_t * addr)
99 nat64_main_t *nm = &nat64_main;
100 snat_main_t *sm = nm->sm;
101 u32 next_worker_index = nm->sm->first_worker_index;
104 #ifdef clib_crc32c_uses_intrinsics
105 hash = clib_crc32c ((u8 *) addr->as_u32, 16);
107 u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
108 hash = clib_xxhash (tmp);
111 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
112 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
114 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
116 return next_worker_index;
120 nat64_get_worker_out2in (ip4_header_t * ip)
122 nat64_main_t *nm = &nat64_main;
123 snat_main_t *sm = nm->sm;
128 proto = ip_proto_to_snat_proto (ip->protocol);
129 udp = ip4_next_header (ip);
130 port = udp->dst_port;
133 if (PREDICT_FALSE (ip4_is_fragment (ip)))
135 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
136 return vlib_get_thread_index ();
138 if (PREDICT_TRUE (!ip4_is_first_fragment (ip)))
140 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 return vlib_get_thread_index ();
152 /* unknown protocol */
153 if (PREDICT_FALSE (proto == ~0))
156 ip46_address_t daddr;
157 nat64_db_bib_entry_t *bibe;
159 memset (&daddr, 0, sizeof (daddr));
160 daddr.ip4.as_u32 = ip->dst_address.as_u32;
163 vec_foreach (db, nm->db)
165 bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
167 return (u32) (db - nm->db);
170 return vlib_get_thread_index ();
174 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
176 icmp46_header_t *icmp = (icmp46_header_t *) udp;
177 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
178 if (!icmp_is_error_message (icmp))
179 port = echo->identifier;
182 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
183 proto = ip_proto_to_snat_proto (inner_ip->protocol);
184 void *l4_header = ip4_next_header (inner_ip);
187 case SNAT_PROTOCOL_ICMP:
188 icmp = (icmp46_header_t *) l4_header;
189 echo = (icmp_echo_header_t *) (icmp + 1);
190 port = echo->identifier;
192 case SNAT_PROTOCOL_UDP:
193 case SNAT_PROTOCOL_TCP:
194 port = ((tcp_udp_header_t *) l4_header)->src_port;
197 return vlib_get_thread_index ();
202 /* worker by outside port (TCP/UDP) */
203 port = clib_net_to_host_u16 (port);
205 return nm->sm->first_worker_index + ((port - 1024) / sm->port_per_thread);
207 return vlib_get_thread_index ();
211 nat64_init (vlib_main_t * vm)
213 nat64_main_t *nm = &nat64_main;
214 vlib_thread_main_t *tm = vlib_get_thread_main ();
215 ip4_add_del_interface_address_callback_t cb4;
216 ip4_main_t *im = &ip4_main;
217 vlib_node_t *error_drop_node =
218 vlib_get_node_by_name (vm, (u8 *) "error-drop");
220 vec_validate (nm->db, tm->n_vlib_mains - 1);
224 nm->fq_in2out_index = ~0;
225 nm->fq_out2in_index = ~0;
226 nm->error_node_index = error_drop_node->index;
228 /* set session timeouts to default values */
229 nm->udp_timeout = SNAT_UDP_TIMEOUT;
230 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
231 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
232 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
233 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
235 nm->total_enabled_count = 0;
237 /* Set up the interface address add/del callback */
238 cb4.function = nat64_ip4_add_del_interface_address_cb;
239 cb4.function_opaque = 0;
240 vec_add1 (im->add_del_interface_address_callbacks, cb4);
246 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
247 ip4_address_t * addr, u16 port,
251 nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
254 nat64_main_t *nm = &nat64_main;
257 nm->bib_buckets = bib_buckets;
258 nm->bib_memory_size = bib_memory_size;
259 nm->st_buckets = st_buckets;
260 nm->st_memory_size = st_memory_size;
263 vec_foreach (db, nm->db)
265 if (nat64_db_init (db, bib_buckets, bib_memory_size, st_buckets,
266 st_memory_size, nat64_free_out_addr_and_port))
267 clib_warning ("NAT64 DB init failed");
273 nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
275 nat64_main_t *nm = &nat64_main;
276 snat_address_t *a = 0;
277 snat_interface_t *interface;
280 vlib_thread_main_t *tm = vlib_get_thread_main ();
282 /* Check if address already exists */
283 for (i = 0; i < vec_len (nm->addr_pool); i++)
285 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
287 a = nm->addr_pool + i;
295 return VNET_API_ERROR_VALUE_EXIST;
297 vec_add2 (nm->addr_pool, a, 1);
302 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
303 FIB_SOURCE_PLUGIN_HI);
304 #define _(N, id, n, s) \
305 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
306 a->busy_##n##_ports = 0; \
307 vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
308 foreach_snat_protocol
314 return VNET_API_ERROR_NO_SUCH_ENTRY;
316 if (a->fib_index != ~0)
317 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6,
318 FIB_SOURCE_PLUGIN_HI);
319 /* Delete sessions using address */
321 vec_foreach (db, nm->db)
322 nat64_db_free_out_addr (db, &a->addr);
323 #define _(N, id, n, s) \
324 clib_bitmap_free (a->busy_##n##_port_bitmap);
325 foreach_snat_protocol
328 vec_del1 (nm->addr_pool, i);
331 /* Add/del external address to FIB */
333 pool_foreach (interface, nm->interfaces,
335 if (nat_interface_is_inside(interface))
338 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
347 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
349 nat64_main_t *nm = &nat64_main;
350 snat_address_t *a = 0;
353 vec_foreach (a, nm->addr_pool)
362 nat64_add_interface_address (u32 sw_if_index, int is_add)
364 nat64_main_t *nm = &nat64_main;
365 ip4_main_t *ip4_main = nm->ip4_main;
366 ip4_address_t *first_int_addr;
369 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
371 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
373 if (nm->auto_add_sw_if_indices[i] == sw_if_index)
376 return VNET_API_ERROR_VALUE_EXIST;
379 /* if have address remove it */
381 (void) nat64_add_del_pool_addr (first_int_addr, ~0, 0);
383 vec_del1 (nm->auto_add_sw_if_indices, i);
390 return VNET_API_ERROR_NO_SUCH_ENTRY;
392 /* add to the auto-address list */
393 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
395 /* If the address is already bound - or static - add it now */
397 (void) nat64_add_del_pool_addr (first_int_addr, ~0, 1);
403 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
405 nat64_main_t *nm = &nat64_main;
406 snat_interface_t *interface = 0, *i;
408 const char *feature_name, *arc_name;
410 /* Check if interface already exists */
412 pool_foreach (i, nm->interfaces,
414 if (i->sw_if_index == sw_if_index)
427 pool_get (nm->interfaces, interface);
428 interface->sw_if_index = sw_if_index;
429 interface->flags = 0;
432 interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
434 interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
436 nm->total_enabled_count++;
437 vlib_process_signal_event (nm->sm->vlib_main,
438 nm->nat64_expire_walk_node_index,
439 NAT64_CLEANER_RESCHEDULE, 0);
445 return VNET_API_ERROR_NO_SUCH_ENTRY;
447 if ((nat_interface_is_inside (interface)
448 && nat_interface_is_outside (interface)))
450 is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE :
451 ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
453 pool_put (nm->interfaces, interface);
455 nm->total_enabled_count--;
461 vec_foreach (ap, nm->addr_pool)
462 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
466 if (nm->sm->num_workers > 1)
469 is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
470 if (nm->fq_in2out_index == ~0)
471 nm->fq_in2out_index =
472 vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
473 if (nm->fq_out2in_index == ~0)
474 nm->fq_out2in_index =
475 vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
478 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
480 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
482 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
487 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
489 nat64_main_t *nm = &nat64_main;
490 snat_interface_t *i = 0;
493 pool_foreach (i, nm->interfaces,
502 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
503 ip4_address_t * addr, u16 * port,
506 nat64_main_t *nm = &nat64_main;
507 snat_main_t *sm = nm->sm;
508 snat_session_key_t k;
510 u32 worker_index = 0;
515 if (sm->num_workers > 1)
516 worker_index = thread_index - sm->first_worker_index;
519 sm->alloc_addr_and_port (nm->addr_pool, fib_index, thread_index, &k, &ai,
520 sm->port_per_thread, worker_index);
525 addr->as_u32 = k.addr.as_u32;
532 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
533 u16 port, u8 protocol)
535 nat64_main_t *nm = &nat64_main;
538 u32 thread_index = db - nm->db;
539 snat_protocol_t proto = ip_proto_to_snat_proto (protocol);
540 u16 port_host_byte_order = clib_net_to_host_u16 (port);
542 for (i = 0; i < vec_len (nm->addr_pool); i++)
544 a = nm->addr_pool + i;
545 if (addr->as_u32 != a->addr.as_u32)
549 #define _(N, j, n, s) \
550 case SNAT_PROTOCOL_##N: \
551 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
552 port_host_byte_order) == 1); \
553 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
554 a->busy_##n##_ports--; \
555 a->busy_##n##_ports_per_thread[thread_index]--; \
557 foreach_snat_protocol
560 clib_warning ("unknown protocol");
568 * @brief Add/delete static BIB entry in worker thread.
571 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
574 nat64_main_t *nm = &nat64_main;
575 u32 thread_index = vlib_get_thread_index ();
576 nat64_db_t *db = &nm->db[thread_index];
577 nat64_static_bib_to_update_t *static_bib;
578 nat64_db_bib_entry_t *bibe;
582 pool_foreach (static_bib, nm->static_bibs,
584 if ((static_bib->thread_index != thread_index) || (static_bib->done))
587 if (static_bib->is_add)
588 (void) nat64_db_bib_entry_create (db, &static_bib->in_addr,
589 &static_bib->out_addr,
591 static_bib->out_port,
592 static_bib->fib_index,
593 static_bib->proto, 1);
596 addr.as_u64[0] = static_bib->in_addr.as_u64[0];
597 addr.as_u64[1] = static_bib->in_addr.as_u64[1];
598 bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
600 static_bib->fib_index, 1);
602 nat64_db_bib_entry_free (db, bibe);
605 static_bib->done = 1;
612 static vlib_node_registration_t nat64_static_bib_worker_node;
615 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
616 .function = nat64_static_bib_worker_fn,
617 .type = VLIB_NODE_TYPE_INPUT,
618 .state = VLIB_NODE_STATE_INTERRUPT,
619 .name = "nat64-static-bib-worker",
624 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
625 ip4_address_t * out_addr, u16 in_port,
626 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
628 nat64_main_t *nm = &nat64_main;
629 nat64_db_bib_entry_t *bibe;
630 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
631 FIB_SOURCE_PLUGIN_HI);
632 snat_protocol_t p = ip_proto_to_snat_proto (proto);
636 u32 thread_index = 0;
638 nat64_static_bib_to_update_t *static_bib;
639 vlib_main_t *worker_vm;
640 u32 *to_be_free = 0, *index;
642 if (nm->sm->num_workers > 1)
644 thread_index = nat64_get_worker_in2out (in_addr);
645 db = &nm->db[thread_index];
648 db = &nm->db[nm->sm->num_workers];
650 addr.as_u64[0] = in_addr->as_u64[0];
651 addr.as_u64[1] = in_addr->as_u64[1];
653 nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
654 proto, fib_index, 1);
659 return VNET_API_ERROR_VALUE_EXIST;
661 /* outside port must be assigned to same thread as internall address */
662 if ((out_port > 1024) && (nm->sm->num_workers > 1))
664 if (thread_index != ((out_port - 1024) / nm->sm->port_per_thread))
665 return VNET_API_ERROR_INVALID_VALUE_2;
668 for (i = 0; i < vec_len (nm->addr_pool); i++)
670 a = nm->addr_pool + i;
671 if (out_addr->as_u32 != a->addr.as_u32)
675 #define _(N, j, n, s) \
676 case SNAT_PROTOCOL_##N: \
677 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
679 return VNET_API_ERROR_INVALID_VALUE; \
680 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
682 if (out_port > 1024) \
684 a->busy_##n##_ports++; \
685 a->busy_##n##_ports_per_thread[thread_index]++; \
688 foreach_snat_protocol
691 memset (&addr, 0, sizeof (addr));
692 addr.ip4.as_u32 = out_addr->as_u32;
693 if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
694 return VNET_API_ERROR_INVALID_VALUE;
698 if (!nm->sm->num_workers)
701 nat64_db_bib_entry_create (db, in_addr, out_addr,
702 clib_host_to_net_u16 (in_port),
703 clib_host_to_net_u16 (out_port),
704 fib_index, proto, 1);
706 return VNET_API_ERROR_UNSPECIFIED;
712 return VNET_API_ERROR_NO_SUCH_ENTRY;
714 if (!nm->sm->num_workers)
715 nat64_db_bib_entry_free (db, bibe);
718 if (nm->sm->num_workers)
721 pool_foreach (static_bib, nm->static_bibs,
723 if (static_bib->done)
724 vec_add1 (to_be_free, static_bib - nm->static_bibs);
726 vec_foreach (index, to_be_free)
727 pool_put_index (nm->static_bibs, index[0]);
729 vec_free (to_be_free);
730 pool_get (nm->static_bibs, static_bib);
731 static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
732 static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
733 static_bib->in_port = clib_host_to_net_u16 (in_port);
734 static_bib->out_addr.as_u32 = out_addr->as_u32;
735 static_bib->out_port = clib_host_to_net_u16 (out_port);
736 static_bib->fib_index = fib_index;
737 static_bib->proto = proto;
738 static_bib->is_add = is_add;
739 static_bib->thread_index = thread_index;
740 static_bib->done = 0;
741 worker_vm = vlib_mains[thread_index];
743 vlib_node_set_interrupt_pending (worker_vm,
744 nat64_static_bib_worker_node.index);
746 return VNET_API_ERROR_UNSPECIFIED;
753 nat64_set_udp_timeout (u32 timeout)
755 nat64_main_t *nm = &nat64_main;
758 nm->udp_timeout = SNAT_UDP_TIMEOUT;
759 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
760 return VNET_API_ERROR_INVALID_VALUE;
762 nm->udp_timeout = timeout;
768 nat64_get_udp_timeout (void)
770 nat64_main_t *nm = &nat64_main;
772 return nm->udp_timeout;
776 nat64_set_icmp_timeout (u32 timeout)
778 nat64_main_t *nm = &nat64_main;
781 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
783 nm->icmp_timeout = timeout;
789 nat64_get_icmp_timeout (void)
791 nat64_main_t *nm = &nat64_main;
793 return nm->icmp_timeout;
797 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
799 nat64_main_t *nm = &nat64_main;
802 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
804 nm->tcp_trans_timeout = trans;
807 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
809 nm->tcp_est_timeout = est;
811 if (incoming_syn == 0)
812 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
814 nm->tcp_incoming_syn_timeout = incoming_syn;
820 nat64_get_tcp_trans_timeout (void)
822 nat64_main_t *nm = &nat64_main;
824 return nm->tcp_trans_timeout;
828 nat64_get_tcp_est_timeout (void)
830 nat64_main_t *nm = &nat64_main;
832 return nm->tcp_est_timeout;
836 nat64_get_tcp_incoming_syn_timeout (void)
838 nat64_main_t *nm = &nat64_main;
840 return nm->tcp_incoming_syn_timeout;
844 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
846 nat64_main_t *nm = &nat64_main;
847 u32 now = (u32) vlib_time_now (vm);
849 switch (ip_proto_to_snat_proto (ste->proto))
851 case SNAT_PROTOCOL_ICMP:
852 ste->expire = now + nm->icmp_timeout;
854 case SNAT_PROTOCOL_TCP:
856 switch (ste->tcp_state)
858 case NAT64_TCP_STATE_V4_INIT:
859 case NAT64_TCP_STATE_V6_INIT:
860 case NAT64_TCP_STATE_V4_FIN_RCV:
861 case NAT64_TCP_STATE_V6_FIN_RCV:
862 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
863 case NAT64_TCP_STATE_TRANS:
864 ste->expire = now + nm->tcp_trans_timeout;
866 case NAT64_TCP_STATE_ESTABLISHED:
867 ste->expire = now + nm->tcp_est_timeout;
873 case SNAT_PROTOCOL_UDP:
874 ste->expire = now + nm->udp_timeout;
877 ste->expire = now + nm->udp_timeout;
883 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
886 switch (ste->tcp_state)
888 case NAT64_TCP_STATE_CLOSED:
890 if (tcp->flags & TCP_FLAG_SYN)
893 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
895 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
899 case NAT64_TCP_STATE_V4_INIT:
901 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
902 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
905 case NAT64_TCP_STATE_V6_INIT:
907 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
908 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
911 case NAT64_TCP_STATE_ESTABLISHED:
913 if (tcp->flags & TCP_FLAG_FIN)
916 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
918 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
920 else if (tcp->flags & TCP_FLAG_RST)
922 ste->tcp_state = NAT64_TCP_STATE_TRANS;
926 case NAT64_TCP_STATE_V4_FIN_RCV:
928 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
929 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
932 case NAT64_TCP_STATE_V6_FIN_RCV:
934 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
935 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
938 case NAT64_TCP_STATE_TRANS:
940 if (!(tcp->flags & TCP_FLAG_RST))
941 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
950 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
952 nat64_main_t *nm = &nat64_main;
953 nat64_prefix_t *p = 0;
956 /* Verify prefix length */
957 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
959 return VNET_API_ERROR_INVALID_VALUE;
961 /* Check if tenant already have prefix */
962 for (i = 0; i < vec_len (nm->pref64); i++)
964 if (nm->pref64[i].vrf_id == vrf_id)
975 vec_add2 (nm->pref64, p, 1);
977 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
978 FIB_SOURCE_PLUGIN_HI);
982 p->prefix.as_u64[0] = prefix->as_u64[0];
983 p->prefix.as_u64[1] = prefix->as_u64[1];
989 return VNET_API_ERROR_NO_SUCH_ENTRY;
991 vec_del1 (nm->pref64, i);
998 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
1000 nat64_main_t *nm = &nat64_main;
1001 nat64_prefix_t *p = 0;
1004 vec_foreach (p, nm->pref64)
1013 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1015 nat64_main_t *nm = &nat64_main;
1016 nat64_prefix_t *p, *gp = 0, *prefix = 0;
1019 vec_foreach (p, nm->pref64)
1021 if (p->fib_index == fib_index)
1027 if (p->fib_index == 0)
1037 clib_memcpy (ip6, &p->prefix, sizeof (ip6_address_t));
1041 ip6->as_u32[1] = ip4->as_u32;
1044 ip6->as_u8[5] = ip4->as_u8[0];
1045 ip6->as_u8[6] = ip4->as_u8[1];
1046 ip6->as_u8[7] = ip4->as_u8[2];
1047 ip6->as_u8[9] = ip4->as_u8[3];
1050 ip6->as_u8[6] = ip4->as_u8[0];
1051 ip6->as_u8[7] = ip4->as_u8[1];
1052 ip6->as_u8[9] = ip4->as_u8[2];
1053 ip6->as_u8[10] = ip4->as_u8[3];
1056 ip6->as_u8[7] = ip4->as_u8[0];
1057 ip6->as_u8[9] = ip4->as_u8[1];
1058 ip6->as_u8[10] = ip4->as_u8[2];
1059 ip6->as_u8[11] = ip4->as_u8[3];
1062 ip6->as_u8[9] = ip4->as_u8[0];
1063 ip6->as_u8[10] = ip4->as_u8[1];
1064 ip6->as_u8[11] = ip4->as_u8[2];
1065 ip6->as_u8[12] = ip4->as_u8[3];
1068 ip6->as_u32[3] = ip4->as_u32;
1071 clib_warning ("invalid prefix length");
1077 clib_memcpy (ip6, well_known_prefix, sizeof (ip6_address_t));
1078 ip6->as_u32[3] = ip4->as_u32;
1083 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1085 nat64_main_t *nm = &nat64_main;
1086 nat64_prefix_t *p, *gp = 0;
1090 vec_foreach (p, nm->pref64)
1092 if (p->fib_index == fib_index)
1114 ip4->as_u32 = ip6->as_u32[1];
1117 ip4->as_u8[0] = ip6->as_u8[5];
1118 ip4->as_u8[1] = ip6->as_u8[6];
1119 ip4->as_u8[2] = ip6->as_u8[7];
1120 ip4->as_u8[3] = ip6->as_u8[9];
1123 ip4->as_u8[0] = ip6->as_u8[6];
1124 ip4->as_u8[1] = ip6->as_u8[7];
1125 ip4->as_u8[2] = ip6->as_u8[9];
1126 ip4->as_u8[3] = ip6->as_u8[10];
1129 ip4->as_u8[0] = ip6->as_u8[7];
1130 ip4->as_u8[1] = ip6->as_u8[9];
1131 ip4->as_u8[2] = ip6->as_u8[10];
1132 ip4->as_u8[3] = ip6->as_u8[11];
1135 ip4->as_u8[0] = ip6->as_u8[9];
1136 ip4->as_u8[1] = ip6->as_u8[10];
1137 ip4->as_u8[2] = ip6->as_u8[11];
1138 ip4->as_u8[3] = ip6->as_u8[12];
1141 ip4->as_u32 = ip6->as_u32[3];
1144 clib_warning ("invalid prefix length");
1150 * @brief Per worker process checking expire time for NAT64 sessions.
1153 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1156 nat64_main_t *nm = &nat64_main;
1157 u32 thread_index = vlib_get_thread_index ();
1158 nat64_db_t *db = &nm->db[thread_index];
1159 u32 now = (u32) vlib_time_now (vm);
1161 nad64_db_st_free_expired (db, now);
1166 static vlib_node_registration_t nat64_expire_worker_walk_node;
1169 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1170 .function = nat64_expire_worker_walk_fn,
1171 .type = VLIB_NODE_TYPE_INPUT,
1172 .state = VLIB_NODE_STATE_INTERRUPT,
1173 .name = "nat64-expire-worker-walk",
1177 static vlib_node_registration_t nat64_expire_walk_node;
1180 * @brief Centralized process to drive per worker expire walk.
1183 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1186 nat64_main_t *nm = &nat64_main;
1187 vlib_main_t **worker_vms = 0, *worker_vm;
1189 uword event_type, *event_data = 0;
1191 nm->nat64_expire_walk_node_index = nat64_expire_walk_node.index;
1193 if (vec_len (vlib_mains) == 0)
1194 vec_add1 (worker_vms, vm);
1197 for (i = 0; i < vec_len (vlib_mains); i++)
1199 worker_vm = vlib_mains[i];
1201 vec_add1 (worker_vms, worker_vm);
1207 if (nm->total_enabled_count)
1209 vlib_process_wait_for_event_or_clock (vm, 10.0);
1210 event_type = vlib_process_get_events (vm, &event_data);
1214 vlib_process_wait_for_event (vm);
1215 event_type = vlib_process_get_events (vm, &event_data);
1222 case NAT64_CLEANER_RESCHEDULE:
1225 clib_warning ("unknown event %u", event_type);
1229 for (i = 0; i < vec_len (worker_vms); i++)
1231 worker_vm = worker_vms[i];
1232 vlib_node_set_interrupt_pending (worker_vm,
1233 nat64_expire_worker_walk_node.index);
1241 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
1242 .function = nat64_expire_walk_fn,
1243 .type = VLIB_NODE_TYPE_PROCESS,
1244 .name = "nat64-expire-walk",
1249 * fd.io coding-style-patch-verification: ON
1252 * eval: (c-set-style "gnu")