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_inlines.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vppinfra/crc32.h>
25 #include <vnet/ip/reass/ip4_sv_reass.h>
26 #include <vnet/ip/reass/ip6_sv_reass.h>
29 nat64_main_t nat64_main;
33 /* Hook up input features */
34 VNET_FEATURE_INIT (nat64_in2out, static) = {
35 .arc_name = "ip6-unicast",
36 .node_name = "nat64-in2out",
37 .runs_before = VNET_FEATURES ("ip6-lookup"),
38 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
40 VNET_FEATURE_INIT (nat64_out2in, static) = {
41 .arc_name = "ip4-unicast",
42 .node_name = "nat64-out2in",
43 .runs_before = VNET_FEATURES ("ip4-lookup"),
44 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
46 VNET_FEATURE_INIT (nat64_in2out_handoff, static) = {
47 .arc_name = "ip6-unicast",
48 .node_name = "nat64-in2out-handoff",
49 .runs_before = VNET_FEATURES ("ip6-lookup"),
50 .runs_after = VNET_FEATURES ("ip6-sv-reassembly-feature"),
52 VNET_FEATURE_INIT (nat64_out2in_handoff, static) = {
53 .arc_name = "ip4-unicast",
54 .node_name = "nat64-out2in-handoff",
55 .runs_before = VNET_FEATURES ("ip4-lookup"),
56 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
60 static u8 well_known_prefix[] = {
61 0x00, 0x64, 0xff, 0x9b,
62 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00
70 nat64_ip4_add_del_interface_address_cb (ip4_main_t * im, uword opaque,
72 ip4_address_t * address,
74 u32 if_address_index, u32 is_delete)
76 nat64_main_t *nm = &nat64_main;
79 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
81 if (sw_if_index == nm->auto_add_sw_if_indices[i])
85 /* Don't trip over lease renewal, static config */
86 for (j = 0; j < vec_len (nm->addr_pool); j++)
87 if (nm->addr_pool[j].addr.as_u32 == address->as_u32)
90 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
96 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
105 nat64_get_worker_in2out (ip6_address_t * addr)
107 nat64_main_t *nm = &nat64_main;
108 snat_main_t *sm = nm->sm;
109 u32 next_worker_index = nm->sm->first_worker_index;
112 #ifdef clib_crc32c_uses_intrinsics
113 hash = clib_crc32c ((u8 *) addr->as_u32, 16);
115 u64 tmp = addr->as_u64[0] ^ addr->as_u64[1];
116 hash = clib_xxhash (tmp);
119 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
120 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
122 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
124 return next_worker_index;
128 nat64_get_worker_out2in (vlib_buffer_t * b, ip4_header_t * ip)
130 nat64_main_t *nm = &nat64_main;
131 snat_main_t *sm = nm->sm;
136 proto = ip_proto_to_snat_proto (ip->protocol);
137 udp = ip4_next_header (ip);
138 port = udp->dst_port;
140 /* unknown protocol */
141 if (PREDICT_FALSE (proto == ~0))
144 ip46_address_t daddr;
145 nat64_db_bib_entry_t *bibe;
147 clib_memset (&daddr, 0, sizeof (daddr));
148 daddr.ip4.as_u32 = ip->dst_address.as_u32;
151 vec_foreach (db, nm->db)
153 bibe = nat64_db_bib_entry_find (db, &daddr, 0, ip->protocol, 0, 0);
155 return (u32) (db - nm->db);
158 return vlib_get_thread_index ();
162 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
164 icmp46_header_t *icmp = (icmp46_header_t *) udp;
165 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
166 if (!icmp_type_is_error_message
167 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
168 port = vnet_buffer (b)->ip.reass.l4_src_port;
171 /* if error message, then it's not fragmented and we can access it */
172 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
173 proto = ip_proto_to_snat_proto (inner_ip->protocol);
174 void *l4_header = ip4_next_header (inner_ip);
177 case SNAT_PROTOCOL_ICMP:
178 icmp = (icmp46_header_t *) l4_header;
179 echo = (icmp_echo_header_t *) (icmp + 1);
180 port = echo->identifier;
182 case SNAT_PROTOCOL_UDP:
183 case SNAT_PROTOCOL_TCP:
184 port = ((tcp_udp_header_t *) l4_header)->src_port;
187 return vlib_get_thread_index ();
192 /* worker by outside port (TCP/UDP) */
193 port = clib_net_to_host_u16 (port);
195 return nm->sm->first_worker_index + ((port - 1024) / sm->port_per_thread);
197 return vlib_get_thread_index ();
201 nat64_init (vlib_main_t * vm)
203 nat64_main_t *nm = &nat64_main;
204 vlib_thread_main_t *tm = vlib_get_thread_main ();
205 ip4_add_del_interface_address_callback_t cb4;
206 ip4_main_t *im = &ip4_main;
210 vec_validate (nm->db, tm->n_vlib_mains - 1);
212 nm->fq_in2out_index = ~0;
213 nm->fq_out2in_index = ~0;
215 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
216 nm->error_node_index = node->index;
218 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out");
219 nm->in2out_node_index = node->index;
221 node = vlib_get_node_by_name (vm, (u8 *) "nat64-in2out-slowpath");
222 nm->in2out_slowpath_node_index = node->index;
224 node = vlib_get_node_by_name (vm, (u8 *) "nat64-out2in");
225 nm->out2in_node_index = node->index;
227 /* set session timeouts to default values */
228 nm->udp_timeout = SNAT_UDP_TIMEOUT;
229 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
230 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
231 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
233 nm->total_enabled_count = 0;
235 /* Set up the interface address add/del callback */
236 cb4.function = nat64_ip4_add_del_interface_address_cb;
237 cb4.function_opaque = 0;
238 vec_add1 (im->add_del_interface_address_callbacks, cb4);
242 nm->total_bibs.name = "total-bibs";
243 nm->total_bibs.stat_segment_name = "/nat64/total-bibs";
244 vlib_validate_simple_counter (&nm->total_bibs, 0);
245 vlib_zero_simple_counter (&nm->total_bibs, 0);
246 nm->total_sessions.name = "total-sessions";
247 nm->total_sessions.stat_segment_name = "/nat64/total-sessions";
248 vlib_validate_simple_counter (&nm->total_sessions, 0);
249 vlib_zero_simple_counter (&nm->total_sessions, 0);
254 static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
255 ip4_address_t * addr, u16 port,
259 nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
262 nat64_main_t *nm = &nat64_main;
265 nm->bib_buckets = bib_buckets;
266 nm->bib_memory_size = bib_memory_size;
267 nm->st_buckets = st_buckets;
268 nm->st_memory_size = st_memory_size;
271 vec_foreach (db, nm->db)
273 if (nat64_db_init (db, bib_buckets, bib_memory_size, st_buckets,
274 st_memory_size, nat64_free_out_addr_and_port))
275 nat_elog_err ("NAT64 DB init failed");
281 nat64_add_del_pool_addr (u32 thread_index,
282 ip4_address_t * addr, u32 vrf_id, u8 is_add)
284 nat64_main_t *nm = &nat64_main;
285 snat_address_t *a = 0;
286 snat_interface_t *interface;
289 vlib_thread_main_t *tm = vlib_get_thread_main ();
291 /* Check if address already exists */
292 for (i = 0; i < vec_len (nm->addr_pool); i++)
294 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
296 a = nm->addr_pool + i;
304 return VNET_API_ERROR_VALUE_EXIST;
306 vec_add2 (nm->addr_pool, a, 1);
311 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
313 #define _(N, id, n, s) \
314 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535); \
315 a->busy_##n##_ports = 0; \
316 vec_validate_init_empty (a->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
317 foreach_snat_protocol
323 return VNET_API_ERROR_NO_SUCH_ENTRY;
325 if (a->fib_index != ~0)
326 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6, nat_fib_src_hi);
327 /* Delete sessions using address */
329 vec_foreach (db, nm->db)
331 nat64_db_free_out_addr (thread_index, db, &a->addr);
332 vlib_set_simple_counter (&nm->total_bibs, db - nm->db, 0,
333 db->bib.bib_entries_num);
334 vlib_set_simple_counter (&nm->total_sessions, db - nm->db, 0,
335 db->st.st_entries_num);
337 #define _(N, id, n, s) \
338 clib_bitmap_free (a->busy_##n##_port_bitmap);
339 foreach_snat_protocol
342 vec_del1 (nm->addr_pool, i);
345 /* Add/del external address to FIB */
347 pool_foreach (interface, nm->interfaces,
349 if (nat_interface_is_inside(interface))
352 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
361 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
363 nat64_main_t *nm = &nat64_main;
364 snat_address_t *a = 0;
367 vec_foreach (a, nm->addr_pool)
376 nat64_add_interface_address (u32 sw_if_index, int is_add)
378 nat64_main_t *nm = &nat64_main;
379 ip4_main_t *ip4_main = nm->ip4_main;
380 ip4_address_t *first_int_addr;
383 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0);
385 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
387 if (nm->auto_add_sw_if_indices[i] == sw_if_index)
390 return VNET_API_ERROR_VALUE_EXIST;
393 /* if have address remove it */
395 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
396 first_int_addr, ~0, 0);
397 vec_del1 (nm->auto_add_sw_if_indices, i);
404 return VNET_API_ERROR_NO_SUCH_ENTRY;
406 /* add to the auto-address list */
407 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
409 /* If the address is already bound - or static - add it now */
411 (void) nat64_add_del_pool_addr (vlib_get_thread_index (),
412 first_int_addr, ~0, 1);
418 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
420 nat64_main_t *nm = &nat64_main;
421 snat_interface_t *interface = 0, *i;
423 const char *feature_name, *arc_name;
425 /* Check if interface already exists */
427 pool_foreach (i, nm->interfaces,
429 if (i->sw_if_index == sw_if_index)
442 pool_get (nm->interfaces, interface);
443 interface->sw_if_index = sw_if_index;
444 interface->flags = 0;
447 interface->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
449 interface->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
451 nm->total_enabled_count++;
452 vlib_process_signal_event (nm->sm->vlib_main,
453 nm->nat64_expire_walk_node_index,
454 NAT64_CLEANER_RESCHEDULE, 0);
460 return VNET_API_ERROR_NO_SUCH_ENTRY;
462 if ((nat_interface_is_inside (interface)
463 && nat_interface_is_outside (interface)))
465 is_inside ? ~NAT_INTERFACE_FLAG_IS_INSIDE :
466 ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
468 pool_put (nm->interfaces, interface);
470 nm->total_enabled_count--;
476 vec_foreach (ap, nm->addr_pool)
477 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
481 if (nm->sm->num_workers > 1)
484 is_inside ? "nat64-in2out-handoff" : "nat64-out2in-handoff";
485 if (nm->fq_in2out_index == ~0)
486 nm->fq_in2out_index =
487 vlib_frame_queue_main_init (nat64_in2out_node.index, 0);
488 if (nm->fq_out2in_index == ~0)
489 nm->fq_out2in_index =
490 vlib_frame_queue_main_init (nat64_out2in_node.index, 0);
493 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
495 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
499 int rv = ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
505 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_add);
510 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
515 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
517 nat64_main_t *nm = &nat64_main;
518 snat_interface_t *i = 0;
521 pool_foreach (i, nm->interfaces,
530 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
531 ip4_address_t * addr, u16 * port,
534 nat64_main_t *nm = &nat64_main;
535 snat_main_t *sm = nm->sm;
536 snat_session_key_t k;
537 u32 worker_index = 0;
542 if (sm->num_workers > 1)
543 worker_index = thread_index - sm->first_worker_index;
546 sm->alloc_addr_and_port (nm->addr_pool, fib_index, thread_index, &k,
547 sm->port_per_thread, worker_index);
552 addr->as_u32 = k.addr.as_u32;
559 nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
560 u16 port, u8 protocol)
562 nat64_main_t *nm = &nat64_main;
565 u32 thread_index = db - nm->db;
566 snat_protocol_t proto = ip_proto_to_snat_proto (protocol);
567 u16 port_host_byte_order = clib_net_to_host_u16 (port);
569 for (i = 0; i < vec_len (nm->addr_pool); i++)
571 a = nm->addr_pool + i;
572 if (addr->as_u32 != a->addr.as_u32)
576 #define _(N, j, n, s) \
577 case SNAT_PROTOCOL_##N: \
578 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
579 port_host_byte_order) == 1); \
580 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port_host_byte_order, 0); \
581 a->busy_##n##_ports--; \
582 a->busy_##n##_ports_per_thread[thread_index]--; \
584 foreach_snat_protocol
587 nat_elog_notice ("unknown protocol");
595 * @brief Add/delete static BIB entry in worker thread.
598 nat64_static_bib_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
601 nat64_main_t *nm = &nat64_main;
602 u32 thread_index = vm->thread_index;
603 nat64_db_t *db = &nm->db[thread_index];
604 nat64_static_bib_to_update_t *static_bib;
605 nat64_db_bib_entry_t *bibe;
609 pool_foreach (static_bib, nm->static_bibs,
611 if ((static_bib->thread_index != thread_index) || (static_bib->done))
614 if (static_bib->is_add)
616 (void) nat64_db_bib_entry_create (thread_index, db,
617 &static_bib->in_addr,
618 &static_bib->out_addr,
620 static_bib->out_port,
621 static_bib->fib_index,
622 static_bib->proto, 1);
623 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
624 db->bib.bib_entries_num);
628 addr.as_u64[0] = static_bib->in_addr.as_u64[0];
629 addr.as_u64[1] = static_bib->in_addr.as_u64[1];
630 bibe = nat64_db_bib_entry_find (db, &addr, static_bib->in_port,
632 static_bib->fib_index, 1);
635 nat64_db_bib_entry_free (thread_index, db, bibe);
636 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
637 db->bib.bib_entries_num);
638 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
639 db->st.st_entries_num);
643 static_bib->done = 1;
650 static vlib_node_registration_t nat64_static_bib_worker_node;
653 VLIB_REGISTER_NODE (nat64_static_bib_worker_node, static) = {
654 .function = nat64_static_bib_worker_fn,
655 .type = VLIB_NODE_TYPE_INPUT,
656 .state = VLIB_NODE_STATE_INTERRUPT,
657 .name = "nat64-static-bib-worker",
662 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
663 ip4_address_t * out_addr, u16 in_port,
664 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
666 nat64_main_t *nm = &nat64_main;
667 nat64_db_bib_entry_t *bibe;
668 u32 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
670 snat_protocol_t p = ip_proto_to_snat_proto (proto);
674 u32 thread_index = 0;
676 nat64_static_bib_to_update_t *static_bib;
677 vlib_main_t *worker_vm;
678 u32 *to_be_free = 0, *index;
680 if (nm->sm->num_workers > 1)
682 thread_index = nat64_get_worker_in2out (in_addr);
683 db = &nm->db[thread_index];
686 db = &nm->db[nm->sm->num_workers];
688 addr.as_u64[0] = in_addr->as_u64[0];
689 addr.as_u64[1] = in_addr->as_u64[1];
691 nat64_db_bib_entry_find (db, &addr, clib_host_to_net_u16 (in_port),
692 proto, fib_index, 1);
697 return VNET_API_ERROR_VALUE_EXIST;
699 /* outside port must be assigned to same thread as internall address */
700 if ((out_port > 1024) && (nm->sm->num_workers > 1))
702 if (thread_index != ((out_port - 1024) / nm->sm->port_per_thread))
703 return VNET_API_ERROR_INVALID_VALUE_2;
706 for (i = 0; i < vec_len (nm->addr_pool); i++)
708 a = nm->addr_pool + i;
709 if (out_addr->as_u32 != a->addr.as_u32)
713 #define _(N, j, n, s) \
714 case SNAT_PROTOCOL_##N: \
715 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
717 return VNET_API_ERROR_INVALID_VALUE; \
718 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
720 if (out_port > 1024) \
722 a->busy_##n##_ports++; \
723 a->busy_##n##_ports_per_thread[thread_index]++; \
726 foreach_snat_protocol
729 clib_memset (&addr, 0, sizeof (addr));
730 addr.ip4.as_u32 = out_addr->as_u32;
731 if (nat64_db_bib_entry_find (db, &addr, 0, proto, fib_index, 0))
732 return VNET_API_ERROR_INVALID_VALUE;
736 if (!nm->sm->num_workers)
739 nat64_db_bib_entry_create (thread_index, db, in_addr, out_addr,
740 clib_host_to_net_u16 (in_port),
741 clib_host_to_net_u16 (out_port),
742 fib_index, proto, 1);
744 return VNET_API_ERROR_UNSPECIFIED;
746 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
747 db->bib.bib_entries_num);
753 return VNET_API_ERROR_NO_SUCH_ENTRY;
755 if (!nm->sm->num_workers)
757 nat64_db_bib_entry_free (thread_index, db, bibe);
758 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
759 db->bib.bib_entries_num);
763 if (nm->sm->num_workers)
766 pool_foreach (static_bib, nm->static_bibs,
768 if (static_bib->done)
769 vec_add1 (to_be_free, static_bib - nm->static_bibs);
771 vec_foreach (index, to_be_free)
772 pool_put_index (nm->static_bibs, index[0]);
774 vec_free (to_be_free);
775 pool_get (nm->static_bibs, static_bib);
776 static_bib->in_addr.as_u64[0] = in_addr->as_u64[0];
777 static_bib->in_addr.as_u64[1] = in_addr->as_u64[1];
778 static_bib->in_port = clib_host_to_net_u16 (in_port);
779 static_bib->out_addr.as_u32 = out_addr->as_u32;
780 static_bib->out_port = clib_host_to_net_u16 (out_port);
781 static_bib->fib_index = fib_index;
782 static_bib->proto = proto;
783 static_bib->is_add = is_add;
784 static_bib->thread_index = thread_index;
785 static_bib->done = 0;
786 worker_vm = vlib_mains[thread_index];
788 vlib_node_set_interrupt_pending (worker_vm,
789 nat64_static_bib_worker_node.index);
791 return VNET_API_ERROR_UNSPECIFIED;
798 nat64_set_udp_timeout (u32 timeout)
800 nat64_main_t *nm = &nat64_main;
803 nm->udp_timeout = SNAT_UDP_TIMEOUT;
805 nm->udp_timeout = timeout;
811 nat64_get_udp_timeout (void)
813 nat64_main_t *nm = &nat64_main;
815 return nm->udp_timeout;
819 nat64_set_icmp_timeout (u32 timeout)
821 nat64_main_t *nm = &nat64_main;
824 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
826 nm->icmp_timeout = timeout;
832 nat64_get_icmp_timeout (void)
834 nat64_main_t *nm = &nat64_main;
836 return nm->icmp_timeout;
840 nat64_set_tcp_timeouts (u32 trans, u32 est)
842 nat64_main_t *nm = &nat64_main;
845 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
847 nm->tcp_trans_timeout = trans;
850 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
852 nm->tcp_est_timeout = est;
858 nat64_get_tcp_trans_timeout (void)
860 nat64_main_t *nm = &nat64_main;
862 return nm->tcp_trans_timeout;
866 nat64_get_tcp_est_timeout (void)
868 nat64_main_t *nm = &nat64_main;
870 return nm->tcp_est_timeout;
874 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
876 nat64_main_t *nm = &nat64_main;
877 u32 now = (u32) vlib_time_now (vm);
879 switch (ip_proto_to_snat_proto (ste->proto))
881 case SNAT_PROTOCOL_ICMP:
882 ste->expire = now + nm->icmp_timeout;
884 case SNAT_PROTOCOL_TCP:
886 switch (ste->tcp_state)
888 case NAT64_TCP_STATE_V4_INIT:
889 case NAT64_TCP_STATE_V6_INIT:
890 case NAT64_TCP_STATE_V4_FIN_RCV:
891 case NAT64_TCP_STATE_V6_FIN_RCV:
892 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
893 case NAT64_TCP_STATE_TRANS:
894 ste->expire = now + nm->tcp_trans_timeout;
896 case NAT64_TCP_STATE_ESTABLISHED:
897 ste->expire = now + nm->tcp_est_timeout;
903 case SNAT_PROTOCOL_UDP:
904 ste->expire = now + nm->udp_timeout;
907 ste->expire = now + nm->udp_timeout;
913 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
916 switch (ste->tcp_state)
918 case NAT64_TCP_STATE_CLOSED:
920 if (tcp->flags & TCP_FLAG_SYN)
923 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
925 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
929 case NAT64_TCP_STATE_V4_INIT:
931 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
932 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
935 case NAT64_TCP_STATE_V6_INIT:
937 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
938 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
941 case NAT64_TCP_STATE_ESTABLISHED:
943 if (tcp->flags & TCP_FLAG_FIN)
946 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
948 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
950 else if (tcp->flags & TCP_FLAG_RST)
952 ste->tcp_state = NAT64_TCP_STATE_TRANS;
956 case NAT64_TCP_STATE_V4_FIN_RCV:
958 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
959 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
962 case NAT64_TCP_STATE_V6_FIN_RCV:
964 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
965 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
968 case NAT64_TCP_STATE_TRANS:
970 if (!(tcp->flags & TCP_FLAG_RST))
971 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
980 nat64_add_del_prefix (ip6_address_t * prefix, u8 plen, u32 vrf_id, u8 is_add)
982 nat64_main_t *nm = &nat64_main;
983 nat64_prefix_t *p = 0;
986 /* Verify prefix length */
987 if (plen != 32 && plen != 40 && plen != 48 && plen != 56 && plen != 64
989 return VNET_API_ERROR_INVALID_VALUE;
991 /* Check if tenant already have prefix */
992 for (i = 0; i < vec_len (nm->pref64); i++)
994 if (nm->pref64[i].vrf_id == vrf_id)
1005 vec_add2 (nm->pref64, p, 1);
1007 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id,
1012 p->prefix.as_u64[0] = prefix->as_u64[0];
1013 p->prefix.as_u64[1] = prefix->as_u64[1];
1019 return VNET_API_ERROR_NO_SUCH_ENTRY;
1021 vec_del1 (nm->pref64, i);
1028 nat64_prefix_walk (nat64_prefix_walk_fn_t fn, void *ctx)
1030 nat64_main_t *nm = &nat64_main;
1031 nat64_prefix_t *p = 0;
1034 vec_foreach (p, nm->pref64)
1043 nat64_compose_ip6 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1045 nat64_main_t *nm = &nat64_main;
1046 nat64_prefix_t *p, *gp = 0, *prefix = 0;
1049 vec_foreach (p, nm->pref64)
1051 if (p->fib_index == fib_index)
1057 if (p->fib_index == 0)
1067 clib_memcpy_fast (ip6, &p->prefix, sizeof (ip6_address_t));
1071 ip6->as_u32[1] = ip4->as_u32;
1074 ip6->as_u8[5] = ip4->as_u8[0];
1075 ip6->as_u8[6] = ip4->as_u8[1];
1076 ip6->as_u8[7] = ip4->as_u8[2];
1077 ip6->as_u8[9] = ip4->as_u8[3];
1080 ip6->as_u8[6] = ip4->as_u8[0];
1081 ip6->as_u8[7] = ip4->as_u8[1];
1082 ip6->as_u8[9] = ip4->as_u8[2];
1083 ip6->as_u8[10] = ip4->as_u8[3];
1086 ip6->as_u8[7] = ip4->as_u8[0];
1087 ip6->as_u8[9] = ip4->as_u8[1];
1088 ip6->as_u8[10] = ip4->as_u8[2];
1089 ip6->as_u8[11] = ip4->as_u8[3];
1092 ip6->as_u8[9] = ip4->as_u8[0];
1093 ip6->as_u8[10] = ip4->as_u8[1];
1094 ip6->as_u8[11] = ip4->as_u8[2];
1095 ip6->as_u8[12] = ip4->as_u8[3];
1098 ip6->as_u32[3] = ip4->as_u32;
1101 nat_elog_notice ("invalid prefix length");
1107 clib_memcpy_fast (ip6, well_known_prefix, sizeof (ip6_address_t));
1108 ip6->as_u32[3] = ip4->as_u32;
1113 nat64_extract_ip4 (ip6_address_t * ip6, ip4_address_t * ip4, u32 fib_index)
1115 nat64_main_t *nm = &nat64_main;
1116 nat64_prefix_t *p, *gp = 0;
1120 vec_foreach (p, nm->pref64)
1122 if (p->fib_index == fib_index)
1144 ip4->as_u32 = ip6->as_u32[1];
1147 ip4->as_u8[0] = ip6->as_u8[5];
1148 ip4->as_u8[1] = ip6->as_u8[6];
1149 ip4->as_u8[2] = ip6->as_u8[7];
1150 ip4->as_u8[3] = ip6->as_u8[9];
1153 ip4->as_u8[0] = ip6->as_u8[6];
1154 ip4->as_u8[1] = ip6->as_u8[7];
1155 ip4->as_u8[2] = ip6->as_u8[9];
1156 ip4->as_u8[3] = ip6->as_u8[10];
1159 ip4->as_u8[0] = ip6->as_u8[7];
1160 ip4->as_u8[1] = ip6->as_u8[9];
1161 ip4->as_u8[2] = ip6->as_u8[10];
1162 ip4->as_u8[3] = ip6->as_u8[11];
1165 ip4->as_u8[0] = ip6->as_u8[9];
1166 ip4->as_u8[1] = ip6->as_u8[10];
1167 ip4->as_u8[2] = ip6->as_u8[11];
1168 ip4->as_u8[3] = ip6->as_u8[12];
1171 ip4->as_u32 = ip6->as_u32[3];
1174 nat_elog_notice ("invalid prefix length");
1180 * @brief Per worker process checking expire time for NAT64 sessions.
1183 nat64_expire_worker_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1186 nat64_main_t *nm = &nat64_main;
1187 u32 thread_index = vm->thread_index;
1188 nat64_db_t *db = &nm->db[thread_index];
1189 u32 now = (u32) vlib_time_now (vm);
1191 nad64_db_st_free_expired (thread_index, db, now);
1192 vlib_set_simple_counter (&nm->total_bibs, thread_index, 0,
1193 db->bib.bib_entries_num);
1194 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1195 db->st.st_entries_num);
1200 static vlib_node_registration_t nat64_expire_worker_walk_node;
1203 VLIB_REGISTER_NODE (nat64_expire_worker_walk_node, static) = {
1204 .function = nat64_expire_worker_walk_fn,
1205 .type = VLIB_NODE_TYPE_INPUT,
1206 .state = VLIB_NODE_STATE_INTERRUPT,
1207 .name = "nat64-expire-worker-walk",
1211 static vlib_node_registration_t nat64_expire_walk_node;
1214 * @brief Centralized process to drive per worker expire walk.
1217 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
1220 nat64_main_t *nm = &nat64_main;
1221 vlib_main_t **worker_vms = 0, *worker_vm;
1223 uword event_type, *event_data = 0;
1225 nm->nat64_expire_walk_node_index = nat64_expire_walk_node.index;
1227 if (vec_len (vlib_mains) == 0)
1228 vec_add1 (worker_vms, vm);
1231 for (i = 0; i < vec_len (vlib_mains); i++)
1233 worker_vm = vlib_mains[i];
1235 vec_add1 (worker_vms, worker_vm);
1241 if (nm->total_enabled_count)
1243 vlib_process_wait_for_event_or_clock (vm, 10.0);
1244 event_type = vlib_process_get_events (vm, &event_data);
1248 vlib_process_wait_for_event (vm);
1249 event_type = vlib_process_get_events (vm, &event_data);
1256 case NAT64_CLEANER_RESCHEDULE:
1259 nat_elog_notice_X1 ("unknown event %d", "i4", event_type);
1263 for (i = 0; i < vec_len (worker_vms); i++)
1265 worker_vm = worker_vms[i];
1266 vlib_node_set_interrupt_pending (worker_vm,
1267 nat64_expire_worker_walk_node.index);
1275 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
1276 .function = nat64_expire_walk_fn,
1277 .type = VLIB_NODE_TYPE_PROCESS,
1278 .name = "nat64-expire-walk",
1283 * fd.io coding-style-patch-verification: ON
1286 * eval: (c-set-style "gnu")