2 * det44.c - deterministic NAT
4 * Copyright (c) 2020 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @brief deterministic NAT (CGN)
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ip/ip4.h>
25 #include <vpp/app/version.h>
26 #include <vnet/plugin/plugin.h>
28 #include <nat/det44/det44.h>
30 det44_main_t det44_main;
33 VNET_FEATURE_INIT (ip4_det44_in2out, static) = {
34 .arc_name = "ip4-unicast",
35 .node_name = "det44-in2out",
36 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
37 "ip4-sv-reassembly-feature"),
39 VNET_FEATURE_INIT (ip4_det44_out2in, static) = {
40 .arc_name = "ip4-unicast",
41 .node_name = "det44-out2in",
42 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
43 "ip4-sv-reassembly-feature",
44 "ip4-dhcp-client-detect"),
46 VLIB_PLUGIN_REGISTER () = {
47 .version = VPP_BUILD_VER,
48 .description = "Deterministic NAT (CGN)",
53 det44_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
56 det44_main_t *dm = &det44_main;
57 fib_prefix_t prefix = {
59 .fp_proto = FIB_PROTOCOL_IP4,
61 .ip4.as_u32 = addr->as_u32,
64 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
68 fib_table_entry_update_one_path (fib_index,
71 (FIB_ENTRY_FLAG_CONNECTED |
72 FIB_ENTRY_FLAG_LOCAL |
73 FIB_ENTRY_FLAG_EXCLUSIVE),
77 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
81 fib_table_entry_delete (fib_index, &prefix, dm->fib_src_low);
86 * @brief Add/delete deterministic NAT mapping.
88 * Create bijective mapping of inside address to outside address and port range
89 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
92 * @param in_addr Inside network address.
93 * @param in_plen Inside network prefix length.
94 * @param out_addr Outside network address.
95 * @param out_plen Outside network prefix length.
96 * @param is_add If 0 delete, otherwise add.
99 snat_det_add_map (ip4_address_t * in_addr, u8 in_plen,
100 ip4_address_t * out_addr, u8 out_plen, int is_add)
102 static snat_det_session_t empty_snat_det_session = { 0 };
103 det44_main_t *dm = &det44_main;
104 ip4_address_t in_cmp, out_cmp;
105 det44_interface_t *i;
109 in_cmp.as_u32 = in_addr->as_u32 & ip4_main.fib_masks[in_plen];
110 out_cmp.as_u32 = out_addr->as_u32 & ip4_main.fib_masks[out_plen];
111 vec_foreach (mp, dm->det_maps)
113 /* Checking for overlapping addresses to be added here */
114 if (mp->in_addr.as_u32 == in_cmp.as_u32 &&
115 mp->in_plen == in_plen &&
116 mp->out_addr.as_u32 == out_cmp.as_u32 && mp->out_plen == out_plen)
123 /* If found, don't add again */
125 return VNET_API_ERROR_VALUE_EXIST;
127 /* If not found, don't delete */
128 if (!found && !is_add)
129 return VNET_API_ERROR_NO_SUCH_ENTRY;
133 pool_get (dm->det_maps, mp);
134 clib_memset (mp, 0, sizeof (*mp));
135 mp->in_addr.as_u32 = in_cmp.as_u32;
136 mp->in_plen = in_plen;
137 mp->out_addr.as_u32 = out_cmp.as_u32;
138 mp->out_plen = out_plen;
139 mp->sharing_ratio = (1 << (32 - in_plen)) / (1 << (32 - out_plen));
140 mp->ports_per_host = (65535 - 1023) / mp->sharing_ratio;
142 vec_validate_init_empty (mp->sessions,
143 DET44_SES_PER_USER * (1 << (32 - in_plen)) -
144 1, empty_snat_det_session);
148 vec_free (mp->sessions);
149 vec_del1 (dm->det_maps, mp - dm->det_maps);
152 /* Add/del external address range to FIB */
154 pool_foreach (i, dm->interfaces) {
155 if (det44_interface_is_inside(i))
157 det44_add_del_addr_to_fib(out_addr, out_plen, i->sw_if_index, is_add);
166 det44_set_timeouts (nat_timeouts_t * timeouts)
168 det44_main_t *dm = &det44_main;
170 dm->timeouts.udp = timeouts->udp;
171 if (timeouts->tcp.established)
172 dm->timeouts.tcp.established = timeouts->tcp.established;
173 if (timeouts->tcp.transitory)
174 dm->timeouts.tcp.transitory = timeouts->tcp.transitory;
176 dm->timeouts.icmp = timeouts->icmp;
181 det44_get_timeouts ()
183 det44_main_t *dm = &det44_main;
188 det44_reset_timeouts ()
190 det44_main_t *dm = &det44_main;
191 nat_reset_timeouts (&dm->timeouts);
195 det44_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
197 det44_main_t *dm = &det44_main;
198 det44_interface_t *tmp, *i = 0;
199 const char *feature_name;
202 // TODO: if plugin is not enabled do not register nodes on interfaces
203 // rather make a structure and when enable call is used
204 // then register nodes
207 pool_foreach (tmp, dm->interfaces) {
208 if (tmp->sw_if_index == sw_if_index)
217 feature_name = is_inside ? "det44-in2out" : "det44-out2in";
223 det44_log_err ("det44 is not enabled on this interface");
224 return VNET_API_ERROR_INVALID_VALUE;
227 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
231 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
232 sw_if_index, 1, 0, 0);
236 pool_put (dm->interfaces, i);
242 det44_log_err ("det44 is already enabled on this interface");
243 return VNET_API_ERROR_INVALID_VALUE;
246 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
250 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
251 sw_if_index, 1, 0, 0);
255 pool_get (dm->interfaces, i);
256 clib_memset (i, 0, sizeof (*i));
258 i->sw_if_index = sw_if_index;
261 i->flags |= DET44_INTERFACE_FLAG_IS_INSIDE;
263 i->flags |= DET44_INTERFACE_FLAG_IS_OUTSIDE;
268 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
270 // add/del outside interface fib to registry
272 det44_fib_t *outside_fib;
274 vec_foreach (outside_fib, dm->outside_fibs)
276 if (outside_fib->fib_index == fib_index)
280 outside_fib->refcount++;
284 outside_fib->refcount--;
285 if (!outside_fib->refcount)
287 vec_del1 (dm->outside_fibs,
288 outside_fib - dm->outside_fibs);
296 if (!is_del && !found)
298 vec_add2 (dm->outside_fibs, outside_fib, 1);
299 outside_fib->fib_index = fib_index;
300 outside_fib->refcount = 1;
302 // add/del outside address to FIB
305 pool_foreach (mp, dm->det_maps) {
306 det44_add_del_addr_to_fib(&mp->out_addr,
307 mp->out_plen, sw_if_index, !is_del);
315 * @brief The 'det44-expire-walk' process's main loop.
317 * Check expire time for active sessions.
320 det44_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
323 det44_main_t *dm = &det44_main;
324 snat_det_session_t *ses;
329 vlib_process_wait_for_event_or_clock (vm, 10.0);
330 vlib_process_get_events (vm, NULL);
331 u32 now = (u32) vlib_time_now (vm);
333 if (!plugin_enabled ())
338 pool_foreach (mp, dm->det_maps)
340 vec_foreach (ses, mp->sessions)
342 // close expired sessions
343 if (ses->in_port && (ses->expire < now))
345 snat_det_ses_close (mp, ses);
354 det44_create_expire_walk_process ()
356 det44_main_t *dm = &det44_main;
358 if (dm->expire_walk_node_index)
361 dm->expire_walk_node_index = vlib_process_create (vlib_get_main (),
363 det44_expire_walk_fn,
364 16 /* stack_bytes */ );
368 det44_plugin_enable (det44_config_t c)
370 det44_main_t *dm = &det44_main;
372 if (plugin_enabled () == 1)
374 det44_log_err ("plugin already enabled!");
378 det44_log_err ("inside %u, outside %u", c.inside_vrf_id, c.outside_vrf_id);
380 dm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
383 dm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
387 dm->mss_clamping = 0;
391 det44_create_expire_walk_process ();
396 det44_plugin_disable ()
398 det44_main_t *dm = &det44_main;
399 det44_interface_t *i, *interfaces;
403 if (plugin_enabled () == 0)
405 det44_log_err ("plugin already disabled!");
411 // DET44 cleanup (order dependent)
412 // 1) remove interfaces (det44_interface_add_del) removes map ranges from fib
416 interfaces = vec_dup (dm->interfaces);
417 vec_foreach (i, interfaces)
419 vnet_main_t *vnm = vnet_get_main ();
421 if (i->flags & DET44_INTERFACE_FLAG_IS_INSIDE)
423 rv = det44_interface_add_del (i->sw_if_index, i->flags, 1);
426 det44_log_err ("inside interface %U del failed",
427 unformat_vnet_sw_interface, vnm, i->sw_if_index);
431 if (i->flags & DET44_INTERFACE_FLAG_IS_OUTSIDE)
433 rv = det44_interface_add_del (i->sw_if_index, i->flags, 1);
436 det44_log_err ("outside interface %U del failed",
437 unformat_vnet_sw_interface, vnm, i->sw_if_index);
442 vec_free (interfaces);
445 pool_foreach (mp, dm->det_maps)
447 vec_free (mp->sessions);
451 det44_reset_timeouts ();
453 pool_free (dm->interfaces);
454 pool_free (dm->det_maps);
460 det44_update_outside_fib (ip4_main_t * im,
462 u32 sw_if_index, u32 new_fib_index,
465 det44_main_t *dm = &det44_main;
467 det44_fib_t *outside_fib;
468 det44_interface_t *i;
473 if (plugin_enabled () == 0)
476 if (new_fib_index == old_fib_index)
479 if (!vec_len (dm->outside_fibs))
483 pool_foreach (i, dm->interfaces)
485 if (i->sw_if_index == sw_if_index)
487 if (!(det44_interface_is_outside (i)))
497 vec_foreach (outside_fib, dm->outside_fibs)
499 if (outside_fib->fib_index == old_fib_index)
501 outside_fib->refcount--;
502 if (!outside_fib->refcount)
503 vec_del1 (dm->outside_fibs, outside_fib - dm->outside_fibs);
508 vec_foreach (outside_fib, dm->outside_fibs)
510 if (outside_fib->fib_index == new_fib_index)
512 outside_fib->refcount++;
520 vec_add2 (dm->outside_fibs, outside_fib, 1);
521 outside_fib->refcount = 1;
522 outside_fib->fib_index = new_fib_index;
526 static clib_error_t *
527 det44_init (vlib_main_t * vm)
529 det44_main_t *dm = &det44_main;
530 ip4_table_bind_callback_t cb;
533 clib_memset (dm, 0, sizeof (*dm));
535 dm->ip4_main = &ip4_main;
536 dm->log_class = vlib_log_register_class ("det44", 0);
538 node = vlib_get_node_by_name (vm, (u8 *) "det44-in2out");
539 dm->in2out_node_index = node->index;
540 node = vlib_get_node_by_name (vm, (u8 *) "det44-out2in");
541 dm->out2in_node_index = node->index;
543 dm->fib_src_hi = fib_source_allocate ("det44-hi",
544 FIB_SOURCE_PRIORITY_HI,
545 FIB_SOURCE_BH_SIMPLE);
546 dm->fib_src_low = fib_source_allocate ("det44-low",
547 FIB_SOURCE_PRIORITY_LOW,
548 FIB_SOURCE_BH_SIMPLE);
550 cb.function = det44_update_outside_fib;
551 cb.function_opaque = 0;
552 vec_add1 (dm->ip4_main->table_bind_callbacks, cb);
554 det44_reset_timeouts ();
555 return det44_api_hookup (vm);
558 VLIB_INIT_FUNCTION (det44_init);
561 format_det44_session_state (u8 * s, va_list * args)
563 u32 i = va_arg (*args, u32);
568 #define _(v, N, str) case DET44_SESSION_##N: t = (u8 *) str; break;
569 foreach_det44_session_state
572 t = format (t, "unknown");
574 s = format (s, "%s", t);
579 format_det_map_ses (u8 * s, va_list * args)
581 snat_det_map_t *det_map = va_arg (*args, snat_det_map_t *);
582 ip4_address_t in_addr, out_addr;
583 u32 in_offset, out_offset;
584 snat_det_session_t *ses = va_arg (*args, snat_det_session_t *);
585 u32 *i = va_arg (*args, u32 *);
587 u32 user_index = *i / DET44_SES_PER_USER;
589 clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->in_addr.as_u32) +
592 clib_net_to_host_u32 (in_addr.as_u32) -
593 clib_net_to_host_u32 (det_map->in_addr.as_u32);
594 out_offset = in_offset / det_map->sharing_ratio;
596 clib_host_to_net_u32 (clib_net_to_host_u32 (det_map->out_addr.as_u32) +
600 "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
601 format_ip4_address, &in_addr, clib_net_to_host_u16 (ses->in_port),
602 format_ip4_address, &out_addr,
603 clib_net_to_host_u16 (ses->out.out_port), format_ip4_address,
604 &ses->out.ext_host_addr,
605 clib_net_to_host_u16 (ses->out.ext_host_port),
606 format_det44_session_state, ses->state, ses->expire);
612 * fd.io coding-style-patch-verification: ON
615 * eval: (c-set-style "gnu")