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 <snat/nat64.h>
21 #include <snat/nat64_db.h>
22 #include <vnet/fib/ip4_fib.h>
25 nat64_main_t nat64_main;
29 /* Hook up input features */
30 VNET_FEATURE_INIT (nat64_in2out, static) = {
31 .arc_name = "ip6-unicast",
32 .node_name = "nat64-in2out",
33 .runs_before = VNET_FEATURES ("ip6-lookup"),
35 VNET_FEATURE_INIT (nat64_out2in, static) = {
36 .arc_name = "ip4-unicast",
37 .node_name = "nat64-out2in",
38 .runs_before = VNET_FEATURES ("ip4-lookup"),
44 nat64_init (vlib_main_t * vm)
46 nat64_main_t *nm = &nat64_main;
47 clib_error_t *error = 0;
49 if (nat64_db_init (&nm->db))
50 error = clib_error_return (0, "NAT64 DB init failed");
52 /* set session timeouts to default values */
53 nm->udp_timeout = SNAT_UDP_TIMEOUT;
54 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
55 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
56 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
57 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
63 nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
65 nat64_main_t *nm = &nat64_main;
66 snat_address_t *a = 0;
67 snat_interface_t *interface;
70 /* Check if address already exists */
71 for (i = 0; i < vec_len (nm->addr_pool); i++)
73 if (nm->addr_pool[i].addr.as_u32 == addr->as_u32)
75 a = nm->addr_pool + i;
83 return VNET_API_ERROR_VALUE_EXIST;
85 vec_add2 (nm->addr_pool, a, 1);
87 a->fib_index = ip4_fib_index_from_table_id (vrf_id);
88 #define _(N, i, n, s) \
89 clib_bitmap_alloc (a->busy_##n##_port_bitmap, 65535);
96 return VNET_API_ERROR_NO_SUCH_ENTRY;
98 #define _(N, id, n, s) \
99 clib_bitmap_free (a->busy_##n##_port_bitmap);
100 foreach_snat_protocol
102 vec_del1 (nm->addr_pool, i);
105 /* Add/del external address to FIB */
107 pool_foreach (interface, nm->interfaces,
109 if (interface->is_inside)
112 snat_add_del_addr_to_fib (addr, 32, interface->sw_if_index, is_add);
121 nat64_pool_addr_walk (nat64_pool_addr_walk_fn_t fn, void *ctx)
123 nat64_main_t *nm = &nat64_main;
124 snat_address_t *a = 0;
127 vec_foreach (a, nm->addr_pool)
136 nat64_add_del_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
138 nat64_main_t *nm = &nat64_main;
139 snat_interface_t *interface = 0, *i;
141 const char *feature_name, *arc_name;
143 /* Check if address already exists */
145 pool_foreach (i, nm->interfaces,
147 if (i->sw_if_index == sw_if_index)
158 return VNET_API_ERROR_VALUE_EXIST;
160 pool_get (nm->interfaces, interface);
161 interface->sw_if_index = sw_if_index;
162 interface->is_inside = is_inside;
168 return VNET_API_ERROR_NO_SUCH_ENTRY;
170 pool_put (nm->interfaces, interface);
176 vec_foreach (ap, nm->addr_pool)
177 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, is_add);
181 arc_name = is_inside ? "ip6-unicast" : "ip4-unicast";
182 feature_name = is_inside ? "nat64-in2out" : "nat64-out2in";
184 return vnet_feature_enable_disable (arc_name, feature_name, sw_if_index,
189 nat64_interfaces_walk (nat64_interface_walk_fn_t fn, void *ctx)
191 nat64_main_t *nm = &nat64_main;
192 snat_interface_t *i = 0;
195 pool_foreach (i, nm->interfaces,
204 nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
205 ip4_address_t * addr, u16 * port)
207 nat64_main_t *nm = &nat64_main;
208 snat_main_t *sm = &snat_main;
213 for (i = 0; i < vec_len (nm->addr_pool); i++)
215 a = nm->addr_pool + i;
218 #define _(N, j, n, s) \
219 case SNAT_PROTOCOL_##N: \
220 if (a->busy_##n##_ports < (65535-1024)) \
224 portnum = random_u32 (&sm->random_seed); \
226 if (portnum < 1024) \
228 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
231 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
233 a->busy_##n##_ports++; \
235 addr->as_u32 = a->addr.as_u32; \
240 foreach_snat_protocol
243 clib_warning ("unknown protocol");
248 /* Totally out of translations to use... */
254 nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
255 snat_protocol_t proto)
257 nat64_main_t *nm = &nat64_main;
261 for (i = 0; i < vec_len (nm->addr_pool); i++)
263 a = nm->addr_pool + i;
264 if (addr->as_u32 != a->addr.as_u32)
268 #define _(N, j, n, s) \
269 case SNAT_PROTOCOL_##N: \
270 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
272 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
273 a->busy_##n##_ports--; \
275 foreach_snat_protocol
278 clib_warning ("unknown protocol");
286 nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
287 ip4_address_t * out_addr, u16 in_port,
288 u16 out_port, u8 proto, u32 vrf_id, u8 is_add)
290 nat64_main_t *nm = &nat64_main;
291 nat64_db_bib_entry_t *bibe;
293 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, vrf_id);
294 snat_protocol_t p = ip_proto_to_snat_proto (proto);
299 addr.as_u64[0] = in_addr->as_u64[0];
300 addr.as_u64[1] = in_addr->as_u64[1];
302 nat64_db_bib_entry_find (&nm->db, &addr, clib_host_to_net_u16 (in_port),
308 return VNET_API_ERROR_VALUE_EXIST;
310 for (i = 0; i < vec_len (nm->addr_pool); i++)
312 a = nm->addr_pool + i;
313 if (out_addr->as_u32 != a->addr.as_u32)
317 #define _(N, j, n, s) \
318 case SNAT_PROTOCOL_##N: \
319 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
321 return VNET_API_ERROR_INVALID_VALUE; \
322 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
324 if (out_port > 1024) \
325 a->busy_##n##_ports++; \
327 foreach_snat_protocol
330 clib_warning ("unknown protocol");
331 return VNET_API_ERROR_INVALID_VALUE_2;
336 nat64_db_bib_entry_create (&nm->db, in_addr, out_addr,
337 clib_host_to_net_u16 (in_port),
338 clib_host_to_net_u16 (out_port), fib_index,
341 return VNET_API_ERROR_UNSPECIFIED;
346 return VNET_API_ERROR_NO_SUCH_ENTRY;
348 nat64_free_out_addr_and_port (out_addr, out_port, p);
349 nat64_db_bib_entry_free (&nm->db, bibe);
356 nat64_set_udp_timeout (u32 timeout)
358 nat64_main_t *nm = &nat64_main;
361 nm->udp_timeout = SNAT_UDP_TIMEOUT;
362 else if (timeout < SNAT_UDP_TIMEOUT_MIN)
363 return VNET_API_ERROR_INVALID_VALUE;
365 nm->udp_timeout = timeout;
371 nat64_get_udp_timeout (void)
373 nat64_main_t *nm = &nat64_main;
375 return nm->udp_timeout;
379 nat64_set_icmp_timeout (u32 timeout)
381 nat64_main_t *nm = &nat64_main;
384 nm->icmp_timeout = SNAT_ICMP_TIMEOUT;
386 nm->icmp_timeout = timeout;
392 nat64_get_icmp_timeout (void)
394 nat64_main_t *nm = &nat64_main;
396 return nm->icmp_timeout;
400 nat64_set_tcp_timeouts (u32 trans, u32 est, u32 incoming_syn)
402 nat64_main_t *nm = &nat64_main;
405 nm->tcp_trans_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
407 nm->tcp_trans_timeout = trans;
410 nm->tcp_est_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
412 nm->tcp_est_timeout = est;
414 if (incoming_syn == 0)
415 nm->tcp_incoming_syn_timeout = SNAT_TCP_INCOMING_SYN;
417 nm->tcp_incoming_syn_timeout = incoming_syn;
423 nat64_get_tcp_trans_timeout (void)
425 nat64_main_t *nm = &nat64_main;
427 return nm->tcp_trans_timeout;
431 nat64_get_tcp_est_timeout (void)
433 nat64_main_t *nm = &nat64_main;
435 return nm->tcp_est_timeout;
439 nat64_get_tcp_incoming_syn_timeout (void)
441 nat64_main_t *nm = &nat64_main;
443 return nm->tcp_incoming_syn_timeout;
447 nat64_session_reset_timeout (nat64_db_st_entry_t * ste, vlib_main_t * vm)
449 nat64_main_t *nm = &nat64_main;
450 u32 now = (u32) vlib_time_now (vm);
454 case SNAT_PROTOCOL_ICMP:
455 ste->expire = now + nm->icmp_timeout;
457 case SNAT_PROTOCOL_TCP:
459 switch (ste->tcp_state)
461 case NAT64_TCP_STATE_V4_INIT:
462 case NAT64_TCP_STATE_V6_INIT:
463 case NAT64_TCP_STATE_V4_FIN_RCV:
464 case NAT64_TCP_STATE_V6_FIN_RCV:
465 case NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV:
466 case NAT64_TCP_STATE_TRANS:
467 ste->expire = now + nm->tcp_trans_timeout;
469 case NAT64_TCP_STATE_ESTABLISHED:
470 ste->expire = now + nm->tcp_est_timeout;
476 case SNAT_PROTOCOL_UDP:
477 ste->expire = now + nm->udp_timeout;
485 nat64_tcp_session_set_state (nat64_db_st_entry_t * ste, tcp_header_t * tcp,
488 switch (ste->tcp_state)
490 case NAT64_TCP_STATE_CLOSED:
492 if (tcp->flags & TCP_FLAG_SYN)
495 ste->tcp_state = NAT64_TCP_STATE_V6_INIT;
497 ste->tcp_state = NAT64_TCP_STATE_V4_INIT;
501 case NAT64_TCP_STATE_V4_INIT:
503 if (is_ip6 && (tcp->flags & TCP_FLAG_SYN))
504 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
507 case NAT64_TCP_STATE_V6_INIT:
509 if (!is_ip6 && (tcp->flags & TCP_FLAG_SYN))
510 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
513 case NAT64_TCP_STATE_ESTABLISHED:
515 if (tcp->flags & TCP_FLAG_FIN)
518 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_RCV;
520 ste->tcp_state = NAT64_TCP_STATE_V4_FIN_RCV;
522 else if (tcp->flags & TCP_FLAG_RST)
524 ste->tcp_state = NAT64_TCP_STATE_TRANS;
528 case NAT64_TCP_STATE_V4_FIN_RCV:
530 if (is_ip6 && (tcp->flags & TCP_FLAG_FIN))
531 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
534 case NAT64_TCP_STATE_V6_FIN_RCV:
536 if (!is_ip6 && (tcp->flags & TCP_FLAG_FIN))
537 ste->tcp_state = NAT64_TCP_STATE_V6_FIN_V4_FIN_RCV;
540 case NAT64_TCP_STATE_TRANS:
542 if (!(tcp->flags & TCP_FLAG_RST))
543 ste->tcp_state = NAT64_TCP_STATE_ESTABLISHED;
552 * @brief The 'nat64-expire-walk' process's main loop.
554 * Check expire time for NAT64 sessions.
557 nat64_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
560 nat64_main_t *nm = &nat64_main;
564 vlib_process_wait_for_event_or_clock (vm, 10.0);
565 vlib_process_get_events (vm, NULL);
566 u32 now = (u32) vlib_time_now (vm);
568 nad64_db_st_free_expired (&nm->db, now);
574 static vlib_node_registration_t nat64_expire_walk_node;
577 VLIB_REGISTER_NODE (nat64_expire_walk_node, static) = {
578 .function = nat64_expire_walk_fn,
579 .type = VLIB_NODE_TYPE_PROCESS,
580 .name = "nat64-expire-walk",
585 * fd.io coding-style-patch-verification: ON
588 * eval: (c-set-style "gnu")