2 * nat44_ei.c - nat44 endpoint dependent plugin
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, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
18 #include <vnet/plugin/plugin.h>
19 #include <vpp/app/version.h>
21 #include <vnet/vnet.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/ip/ip4.h>
24 #include <vnet/ip/ip_table.h>
25 #include <vnet/ip/reass/ip4_sv_reass.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
28 #include <vnet/plugin/plugin.h>
31 #include <nat/lib/log.h>
32 #include <nat/lib/nat_syslog.h>
33 #include <nat/lib/nat_inlines.h>
34 #include <nat/lib/ipfix_logging.h>
36 #include <nat/nat44-ei/nat44_ei_dpo.h>
37 #include <nat/nat44-ei/nat44_ei_inlines.h>
38 #include <nat/nat44-ei/nat44_ei.h>
40 nat44_ei_main_t nat44_ei_main;
42 extern vlib_node_registration_t nat44_ei_hairpinning_node;
43 extern vlib_node_registration_t nat44_ei_hairpin_dst_node;
44 extern vlib_node_registration_t
45 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node;
46 extern vlib_node_registration_t
47 nat44_ei_in2out_hairpinning_finish_interface_output_node;
49 #define skip_if_disabled() \
52 nat44_ei_main_t *nm = &nat44_ei_main; \
53 if (PREDICT_FALSE (!nm->enabled)) \
58 #define fail_if_enabled() \
61 nat44_ei_main_t *nm = &nat44_ei_main; \
62 if (PREDICT_FALSE (nm->enabled)) \
64 nat44_ei_log_err ("plugin enabled"); \
70 #define fail_if_disabled() \
73 nat44_ei_main_t *nm = &nat44_ei_main; \
74 if (PREDICT_FALSE (!nm->enabled)) \
76 nat44_ei_log_err ("plugin disabled"); \
82 /* Hook up input features */
83 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
84 .arc_name = "ip4-unicast",
85 .node_name = "nat44-ei-classify",
86 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
87 "ip4-sv-reassembly-feature"),
89 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
90 .arc_name = "ip4-unicast",
91 .node_name = "nat44-ei-handoff-classify",
92 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
93 "ip4-sv-reassembly-feature"),
95 VNET_FEATURE_INIT (ip4_nat44_ei_in2out, static) = {
96 .arc_name = "ip4-unicast",
97 .node_name = "nat44-ei-in2out",
98 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
99 "ip4-sv-reassembly-feature"),
101 VNET_FEATURE_INIT (ip4_nat44_ei_out2in, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "nat44-ei-out2in",
104 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
105 "ip4-sv-reassembly-feature",
106 "ip4-dhcp-client-detect"),
108 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output, static) = {
109 .arc_name = "ip4-output",
110 .node_name = "nat44-ei-in2out-output",
111 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
112 "ip4-sv-reassembly-output-feature"),
114 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_fast, static) = {
115 .arc_name = "ip4-unicast",
116 .node_name = "nat44-ei-in2out-fast",
117 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
118 "ip4-sv-reassembly-feature"),
120 VNET_FEATURE_INIT (ip4_nat44_ei_out2in_fast, static) = {
121 .arc_name = "ip4-unicast",
122 .node_name = "nat44-ei-out2in-fast",
123 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
124 "ip4-sv-reassembly-feature",
125 "ip4-dhcp-client-detect"),
127 VNET_FEATURE_INIT (ip4_nat44_ei_hairpin_dst, static) = {
128 .arc_name = "ip4-unicast",
129 .node_name = "nat44-ei-hairpin-dst",
130 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
131 "ip4-sv-reassembly-feature"),
133 VNET_FEATURE_INIT (ip4_nat44_ei_hairpin_src, static) = {
134 .arc_name = "ip4-output",
135 .node_name = "nat44-ei-hairpin-src",
136 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
137 "ip4-sv-reassembly-output-feature"),
139 VNET_FEATURE_INIT (ip4_nat44_ei_hairpinning, static) = {
140 .arc_name = "ip4-local",
141 .node_name = "nat44-ei-hairpinning",
142 .runs_before = VNET_FEATURES ("ip4-local-end-of-arc"),
144 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_worker_handoff, static) = {
145 .arc_name = "ip4-unicast",
146 .node_name = "nat44-ei-in2out-worker-handoff",
147 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
149 VNET_FEATURE_INIT (ip4_nat44_ei_out2in_worker_handoff, static) = {
150 .arc_name = "ip4-unicast",
151 .node_name = "nat44-ei-out2in-worker-handoff",
152 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
153 "ip4-dhcp-client-detect"),
155 VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output_worker_handoff, static) = {
156 .arc_name = "ip4-output",
157 .node_name = "nat44-ei-in2out-output-worker-handoff",
158 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa",
159 "ip4-sv-reassembly-output-feature"),
162 VLIB_PLUGIN_REGISTER () = {
163 .version = VPP_BUILD_VER,
164 .description = "IPv4 Endpoint-Independent NAT (NAT44 EI)",
167 #define foreach_nat44_ei_classify_error \
168 _ (NEXT_IN2OUT, "next in2out") \
169 _ (NEXT_OUT2IN, "next out2in") \
170 _ (FRAG_CACHED, "fragment cached")
174 #define _(sym, str) NAT44_EI_CLASSIFY_ERROR_##sym,
175 foreach_nat44_ei_classify_error
177 NAT44_EI_CLASSIFY_N_ERROR,
178 } nat44_ei_classify_error_t;
180 static char *nat44_ei_classify_error_strings[] = {
181 #define _(sym, string) string,
182 foreach_nat44_ei_classify_error
188 NAT44_EI_CLASSIFY_NEXT_IN2OUT,
189 NAT44_EI_CLASSIFY_NEXT_OUT2IN,
190 NAT44_EI_CLASSIFY_NEXT_DROP,
191 NAT44_EI_CLASSIFY_N_NEXT,
192 } nat44_ei_classify_next_t;
198 } nat44_ei_classify_trace_t;
200 void nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len,
201 u32 sw_if_index, int is_add);
204 format_nat44_ei_classify_trace (u8 *s, va_list *args)
206 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
207 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
208 nat44_ei_classify_trace_t *t = va_arg (*args, nat44_ei_classify_trace_t *);
212 s = format (s, "nat44-ei-classify: fragment cached");
215 next = t->next_in2out ? "nat44-ei-in2out" : "nat44-ei-out2in";
216 s = format (s, "nat44-ei-classify: next %s", next);
222 static void nat44_ei_db_free ();
224 static void nat44_ei_db_init (u32 translations, u32 translation_buckets,
227 static void nat44_ei_ip4_add_del_interface_address_cb (
228 ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
229 u32 address_length, u32 if_address_index, u32 is_delete);
231 static void nat44_ei_ip4_add_del_addr_only_sm_cb (
232 ip4_main_t *im, uword opaque, u32 sw_if_index, ip4_address_t *address,
233 u32 address_length, u32 if_address_index, u32 is_delete);
235 static void nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque,
236 u32 sw_if_index, u32 new_fib_index,
240 nat44_ei_set_node_indexes (nat44_ei_main_t *nm, vlib_main_t *vm)
243 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-out2in");
244 nm->out2in_node_index = node->index;
245 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out");
246 nm->in2out_node_index = node->index;
247 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out-output");
248 nm->in2out_output_node_index = node->index;
252 nat44_ei_set_workers (uword *bitmap)
254 nat44_ei_main_t *nm = &nat44_ei_main;
257 if (nm->num_workers < 2)
258 return VNET_API_ERROR_FEATURE_DISABLED;
260 if (clib_bitmap_last_set (bitmap) >= nm->num_workers)
261 return VNET_API_ERROR_INVALID_WORKER;
263 vec_free (nm->workers);
264 clib_bitmap_foreach (i, bitmap)
266 vec_add1 (nm->workers, i);
267 nm->per_thread_data[nm->first_worker_index + i].snat_thread_index = j;
268 nm->per_thread_data[nm->first_worker_index + i].thread_index = i;
272 nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers);
277 #define nat_validate_simple_counter(c, i) \
280 vlib_validate_simple_counter (&c, i); \
281 vlib_zero_simple_counter (&c, i); \
285 #define nat_init_simple_counter(c, n, sn) \
289 c.stat_segment_name = sn; \
290 nat_validate_simple_counter (c, 0); \
294 static_always_inline void
295 nat_validate_interface_counters (nat44_ei_main_t *nm, u32 sw_if_index)
298 nat_validate_simple_counter (nm->counters.fastpath.in2out.x, sw_if_index); \
299 nat_validate_simple_counter (nm->counters.fastpath.out2in.x, sw_if_index); \
300 nat_validate_simple_counter (nm->counters.slowpath.in2out.x, sw_if_index); \
301 nat_validate_simple_counter (nm->counters.slowpath.out2in.x, sw_if_index);
304 nat_validate_simple_counter (nm->counters.hairpinning, sw_if_index);
308 nat44_ei_init (vlib_main_t *vm)
310 nat44_ei_main_t *nm = &nat44_ei_main;
311 vlib_thread_main_t *tm = vlib_get_thread_main ();
312 vlib_thread_registration_t *tr;
313 ip4_add_del_interface_address_callback_t cbi = { 0 };
314 ip4_table_bind_callback_t cbt = { 0 };
315 u32 i, num_threads = 0;
316 uword *p, *bitmap = 0;
318 clib_memset (nm, 0, sizeof (*nm));
321 nm->vnet_main = vnet_get_main ();
323 nm->ip4_main = &ip4_main;
324 nm->api_main = vlibapi_get_main ();
325 nm->ip4_lookup_main = &ip4_main.lookup_main;
328 nm->fq_out2in_index = ~0;
329 nm->fq_in2out_index = ~0;
330 nm->fq_in2out_output_index = ~0;
332 nm->log_level = NAT_LOG_ERROR;
334 nat44_ei_set_node_indexes (nm, vm);
335 nm->log_class = vlib_log_register_class ("nat44-ei", 0);
337 nat_init_simple_counter (nm->total_users, "total-users",
338 "/nat44-ei/total-users");
339 nat_init_simple_counter (nm->total_sessions, "total-sessions",
340 "/nat44-ei/total-sessions");
341 nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached",
342 "/nat44-ei/user-limit-reached");
345 nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x, \
346 "/nat44-ei/in2out/fastpath/" #x); \
347 nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x, \
348 "/nat44-ei/out2in/fastpath/" #x); \
349 nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x, \
350 "/nat44-ei/in2out/slowpath/" #x); \
351 nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x, \
352 "/nat44-ei/out2in/slowpath/" #x);
355 nat_init_simple_counter (nm->counters.hairpinning, "hairpinning",
356 "/nat44-ei/hairpinning");
358 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
361 tr = (vlib_thread_registration_t *) p[0];
364 nm->num_workers = tr->count;
365 nm->first_worker_index = tr->first_index;
368 num_threads = tm->n_vlib_mains - 1;
369 nm->port_per_thread = 0xffff - 1024;
370 vec_validate (nm->per_thread_data, num_threads);
372 /* Use all available workers by default */
373 if (nm->num_workers > 1)
376 for (i = 0; i < nm->num_workers; i++)
377 bitmap = clib_bitmap_set (bitmap, i, 1);
378 nat44_ei_set_workers (bitmap);
379 clib_bitmap_free (bitmap);
382 nm->per_thread_data[0].snat_thread_index = 0;
384 /* callbacks to call when interface address changes. */
385 cbi.function = nat44_ei_ip4_add_del_interface_address_cb;
386 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
387 cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb;
388 vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi);
390 /* callbacks to call when interface to table biding changes */
391 cbt.function = nat44_ei_update_outside_fib;
392 vec_add1 (nm->ip4_main->table_bind_callbacks, cbt);
394 nm->fib_src_low = fib_source_allocate (
395 "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
396 nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI,
397 FIB_SOURCE_BH_SIMPLE);
399 // used only by out2in-dpo feature
400 nat_dpo_module_init ();
401 nat_ha_init (vm, nm->num_workers, num_threads);
403 nm->hairpinning_fq_index =
404 vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0);
405 nm->hairpin_dst_fq_index =
406 vlib_frame_queue_main_init (nat44_ei_hairpin_dst_node.index, 0);
407 nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index =
408 vlib_frame_queue_main_init (
409 nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0);
410 nm->in2out_hairpinning_finish_interface_output_node_fq_index =
411 vlib_frame_queue_main_init (
412 nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0);
413 return nat44_ei_api_hookup (vm);
416 VLIB_INIT_FUNCTION (nat44_ei_init);
419 nat44_ei_plugin_enable (nat44_ei_config_t c)
421 nat44_ei_main_t *nm = &nat44_ei_main;
429 c.sessions = 10 * 1024;
431 if (!c.user_sessions)
432 c.user_sessions = c.sessions;
436 if (!nm->frame_queue_nelts)
437 nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT;
439 nm->translations = c.sessions;
440 nm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
441 nm->user_buckets = nat_calc_bihash_buckets (c.users);
443 nm->pat = (!c.static_mapping_only ||
444 (c.static_mapping_only && c.connection_tracking));
446 nm->static_mapping_only = c.static_mapping_only;
447 nm->static_mapping_connection_tracking = c.connection_tracking;
448 nm->out2in_dpo = c.out2in_dpo;
449 nm->forwarding_enabled = 0;
450 nm->mss_clamping = 0;
452 nm->max_users_per_thread = c.users;
453 nm->max_translations_per_thread = c.sessions;
454 nm->max_translations_per_user = c.user_sessions;
456 nm->inside_vrf_id = c.inside_vrf;
457 nm->inside_fib_index = fib_table_find_or_create_and_lock (
458 FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi);
460 nm->outside_vrf_id = c.outside_vrf;
461 nm->outside_fib_index = fib_table_find_or_create_and_lock (
462 FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi);
464 nat_reset_timeouts (&nm->timeouts);
465 nat44_ei_db_init (nm->translations, nm->translation_buckets,
467 nat44_ei_set_alloc_default ();
469 // TODO: zero simple counter for all counters missing
471 vlib_zero_simple_counter (&nm->total_users, 0);
472 vlib_zero_simple_counter (&nm->total_sessions, 0);
473 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
482 nat44_ei_addresses_free (nat44_ei_address_t **addresses)
484 nat44_ei_address_t *ap;
485 vec_foreach (ap, *addresses)
487 #define _(N, i, n, s) vec_free (ap->busy_##n##_ports_per_thread);
491 vec_free (*addresses);
496 nat44_ei_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
498 const char *feature_name, *del_feature_name;
499 nat44_ei_main_t *nm = &nat44_ei_main;
500 nat44_ei_interface_t *i;
501 nat44_ei_address_t *ap;
502 nat44_ei_static_mapping_t *m;
503 nat44_ei_outside_fib_t *outside_fib;
505 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
509 if (nm->out2in_dpo && !is_inside)
511 nat44_ei_log_err ("error unsupported");
512 return VNET_API_ERROR_UNSUPPORTED;
515 pool_foreach (i, nm->output_feature_interfaces)
517 if (i->sw_if_index == sw_if_index)
519 nat44_ei_log_err ("error interface already configured");
520 return VNET_API_ERROR_VALUE_EXIST;
524 if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking))
525 feature_name = is_inside ? "nat44-ei-in2out-fast" : "nat44-ei-out2in-fast";
528 if (nm->num_workers > 1)
529 feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" :
530 "nat44-ei-out2in-worker-handoff";
532 feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
535 if (nm->fq_in2out_index == ~0 && nm->num_workers > 1)
536 nm->fq_in2out_index = vlib_frame_queue_main_init (nm->in2out_node_index,
537 nm->frame_queue_nelts);
539 if (nm->fq_out2in_index == ~0 && nm->num_workers > 1)
540 nm->fq_out2in_index = vlib_frame_queue_main_init (nm->out2in_node_index,
541 nm->frame_queue_nelts);
545 vec_foreach (outside_fib, nm->outside_fibs)
547 if (outside_fib->fib_index == fib_index)
551 outside_fib->refcount--;
552 if (!outside_fib->refcount)
553 vec_del1 (nm->outside_fibs,
554 outside_fib - nm->outside_fibs);
557 outside_fib->refcount++;
563 vec_add2 (nm->outside_fibs, outside_fib, 1);
564 outside_fib->refcount = 1;
565 outside_fib->fib_index = fib_index;
570 pool_foreach (i, nm->interfaces)
572 if (i->sw_if_index == sw_if_index)
576 if (nat44_ei_interface_is_inside (i) &&
577 nat44_ei_interface_is_outside (i))
580 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
582 i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
584 if (nm->num_workers > 1)
586 del_feature_name = "nat44-ei-handoff-classify";
588 "del_feature_name = nat44-ei-handoff-classify");
589 feature_name = !is_inside ?
590 "nat44-ei-in2out-worker-handoff" :
591 "nat44-ei-out2in-worker-handoff";
595 del_feature_name = "nat44-ei-classify";
596 clib_warning ("del_feature_name = nat44-ei-classify");
598 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
602 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
605 rv = vnet_feature_enable_disable (
606 "ip4-unicast", del_feature_name, sw_if_index, 0, 0, 0);
609 rv = vnet_feature_enable_disable (
610 "ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
615 rv = vnet_feature_enable_disable ("ip4-local",
616 "nat44-ei-hairpinning",
617 sw_if_index, 1, 0, 0);
625 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
628 rv = vnet_feature_enable_disable (
629 "ip4-unicast", feature_name, sw_if_index, 0, 0, 0);
632 pool_put (nm->interfaces, i);
635 rv = vnet_feature_enable_disable ("ip4-local",
636 "nat44-ei-hairpinning",
637 sw_if_index, 0, 0, 0);
645 if ((nat44_ei_interface_is_inside (i) && is_inside) ||
646 (nat44_ei_interface_is_outside (i) && !is_inside))
649 if (nm->num_workers > 1)
651 del_feature_name = !is_inside ?
652 "nat44-ei-in2out-worker-handoff" :
653 "nat44-ei-out2in-worker-handoff";
654 feature_name = "nat44-ei-handoff-classify";
655 clib_warning ("feature_name = nat44-ei-handoff-classify");
660 !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in";
661 feature_name = "nat44-ei-classify";
662 clib_warning ("feature_name = nat44-ei-classify");
666 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
669 rv = vnet_feature_enable_disable (
670 "ip4-unicast", del_feature_name, sw_if_index, 0, 0, 0);
673 rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
674 sw_if_index, 1, 0, 0);
679 rv = vnet_feature_enable_disable (
680 "ip4-local", "nat44-ei-hairpinning", sw_if_index, 0, 0, 0);
693 nat44_ei_log_err ("error interface couldn't be found");
694 return VNET_API_ERROR_NO_SUCH_ENTRY;
697 pool_get (nm->interfaces, i);
698 i->sw_if_index = sw_if_index;
700 nat_validate_interface_counters (nm, sw_if_index);
702 int rv = vnet_feature_enable_disable ("ip4-unicast", feature_name,
703 sw_if_index, 1, 0, 0);
707 rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
711 if (is_inside && !nm->out2in_dpo)
713 rv = vnet_feature_enable_disable ("ip4-local", "nat44-ei-hairpinning",
714 sw_if_index, 1, 0, 0);
722 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
726 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
728 /* Add/delete external addresses to FIB */
730 vec_foreach (ap, nm->addresses)
731 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del);
733 pool_foreach (m, nm->static_mappings)
735 if (!(nat44_ei_is_addr_only_static_mapping (m)) ||
736 (m->local_addr.as_u32 == m->external_addr.as_u32))
739 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
747 nat44_ei_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside,
750 nat44_ei_main_t *nm = &nat44_ei_main;
751 nat44_ei_interface_t *i;
752 nat44_ei_address_t *ap;
753 nat44_ei_static_mapping_t *m;
754 nat44_ei_outside_fib_t *outside_fib;
756 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
760 if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking))
762 nat44_ei_log_err ("error unsupported");
763 return VNET_API_ERROR_UNSUPPORTED;
766 pool_foreach (i, nm->interfaces)
768 if (i->sw_if_index == sw_if_index)
770 nat44_ei_log_err ("error interface already configured");
771 return VNET_API_ERROR_VALUE_EXIST;
777 vec_foreach (outside_fib, nm->outside_fibs)
779 if (outside_fib->fib_index == fib_index)
783 outside_fib->refcount--;
784 if (!outside_fib->refcount)
785 vec_del1 (nm->outside_fibs,
786 outside_fib - nm->outside_fibs);
789 outside_fib->refcount++;
795 vec_add2 (nm->outside_fibs, outside_fib, 1);
796 outside_fib->refcount = 1;
797 outside_fib->fib_index = fib_index;
804 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
808 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
811 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-hairpin-dst",
812 sw_if_index, !is_del, 0, 0);
815 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-hairpin-src",
816 sw_if_index, !is_del, 0, 0);
822 if (nm->num_workers > 1)
824 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
828 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
831 rv = vnet_feature_enable_disable ("ip4-unicast",
832 "nat44-ei-out2in-worker-handoff",
833 sw_if_index, !is_del, 0, 0);
836 rv = vnet_feature_enable_disable (
837 "ip4-output", "nat44-ei-in2out-output-worker-handoff", sw_if_index,
844 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
848 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
851 rv = vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in",
852 sw_if_index, !is_del, 0, 0);
855 rv = vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output",
856 sw_if_index, !is_del, 0, 0);
862 if (nm->fq_in2out_output_index == ~0 && nm->num_workers > 1)
863 nm->fq_in2out_output_index =
864 vlib_frame_queue_main_init (nm->in2out_output_node_index, 0);
866 if (nm->fq_out2in_index == ~0 && nm->num_workers > 1)
867 nm->fq_out2in_index =
868 vlib_frame_queue_main_init (nm->out2in_node_index, 0);
870 pool_foreach (i, nm->output_feature_interfaces)
872 if (i->sw_if_index == sw_if_index)
875 pool_put (nm->output_feature_interfaces, i);
877 return VNET_API_ERROR_VALUE_EXIST;
885 nat44_ei_log_err ("error interface couldn't be found");
886 return VNET_API_ERROR_NO_SUCH_ENTRY;
889 pool_get (nm->output_feature_interfaces, i);
890 i->sw_if_index = sw_if_index;
892 nat_validate_interface_counters (nm, sw_if_index);
894 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
896 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
898 /* Add/delete external addresses to FIB */
903 vec_foreach (ap, nm->addresses)
904 nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del);
906 pool_foreach (m, nm->static_mappings)
908 if (!((nat44_ei_is_addr_only_static_mapping (m))) ||
909 (m->local_addr.as_u32 == m->external_addr.as_u32))
912 nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index,
920 nat44_ei_plugin_disable ()
922 nat44_ei_main_t *nm = &nat44_ei_main;
923 nat44_ei_interface_t *i, *pool;
926 // first unregister all nodes from interfaces
927 pool = pool_dup (nm->interfaces);
928 pool_foreach (i, pool)
930 if (nat44_ei_interface_is_inside (i))
931 error = nat44_ei_interface_add_del (i->sw_if_index, 1, 1);
932 if (nat44_ei_interface_is_outside (i))
933 error = nat44_ei_interface_add_del (i->sw_if_index, 0, 1);
937 nat44_ei_log_err ("error occurred while removing interface %u",
942 pool_free (nm->interfaces);
944 pool = pool_dup (nm->output_feature_interfaces);
945 pool_foreach (i, pool)
947 if (nat44_ei_interface_is_inside (i))
949 nat44_ei_interface_add_del_output_feature (i->sw_if_index, 1, 1);
950 if (nat44_ei_interface_is_outside (i))
952 nat44_ei_interface_add_del_output_feature (i->sw_if_index, 0, 1);
956 nat44_ei_log_err ("error occurred while removing interface %u",
961 pool_free (nm->output_feature_interfaces);
966 nat44_ei_addresses_free (&nm->addresses);
968 vec_free (nm->to_resolve);
969 vec_free (nm->auto_add_sw_if_indices);
972 nm->auto_add_sw_if_indices = 0;
974 nm->forwarding_enabled = 0;
977 clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig));
983 nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses,
984 u32 thread_index, ip4_address_t addr,
985 u16 port, nat_protocol_t protocol)
987 nat44_ei_main_t *nm = &nat44_ei_main;
988 nat44_ei_address_t *a = 0;
990 u16 port_host_byte_order = clib_net_to_host_u16 (port);
992 for (address_index = 0; address_index < vec_len (addresses); address_index++)
994 if (addresses[address_index].addr.as_u32 != addr.as_u32)
997 a = addresses + address_index;
1000 #define _(N, j, n, s) \
1001 case NAT_PROTOCOL_##N: \
1002 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
1003 return VNET_API_ERROR_INSTANCE_IN_USE; \
1004 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
1005 a->busy_##n##_ports_per_thread[thread_index]++; \
1006 a->busy_##n##_ports++; \
1008 foreach_nat_protocol
1010 default : nat_elog_info (nm, "unknown protocol");
1015 return VNET_API_ERROR_NO_SUCH_ENTRY;
1019 nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add)
1021 nat44_ei_main_t *nm = &nat44_ei_main;
1022 dpo_id_t dpo_v4 = DPO_INVALID;
1023 fib_prefix_t pfx = {
1024 .fp_proto = FIB_PROTOCOL_IP4,
1026 .fp_addr.ip4.as_u32 = addr.as_u32,
1031 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
1032 fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi,
1033 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
1034 dpo_reset (&dpo_v4);
1038 fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi);
1043 nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses,
1044 u32 thread_index, ip4_address_t *addr,
1045 u16 port, nat_protocol_t protocol)
1047 nat44_ei_main_t *nm = &nat44_ei_main;
1048 nat44_ei_address_t *a;
1050 u16 port_host_byte_order = clib_net_to_host_u16 (port);
1052 for (address_index = 0; address_index < vec_len (addresses); address_index++)
1054 if (addresses[address_index].addr.as_u32 == addr->as_u32)
1058 ASSERT (address_index < vec_len (addresses));
1060 a = addresses + address_index;
1064 #define _(N, i, n, s) \
1065 case NAT_PROTOCOL_##N: \
1066 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
1067 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
1068 a->busy_##n##_ports--; \
1069 a->busy_##n##_ports_per_thread[thread_index]--; \
1071 foreach_nat_protocol
1073 default : nat_elog_info (nm, "unknown protocol");
1079 nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1080 u32 thread_index, u8 is_ha)
1082 clib_bihash_kv_8_8_t kv;
1084 /* session lookup tables */
1085 init_nat_i2o_k (&kv, s);
1086 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1087 nat_elog_warn (nm, "in2out key del failed");
1088 init_nat_o2i_k (&kv, s);
1089 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1090 nat_elog_warn (nm, "out2in key del failed");
1093 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1094 &s->in2out.addr, s->in2out.port, &s->out2in.addr,
1095 s->out2in.port, s->nat_proto);
1097 if (nat44_ei_is_unk_proto_session (s))
1103 nat_ipfix_logging_nat44_ses_delete (
1104 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1105 s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index);
1107 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1108 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1112 if (nat44_ei_is_session_static (s))
1115 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1116 &s->out2in.addr, s->out2in.port,
1121 nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr,
1122 u32 fib_index, u32 thread_index)
1124 nat44_ei_user_t *u = 0;
1125 nat44_ei_user_key_t user_key;
1126 clib_bihash_kv_8_8_t kv, value;
1127 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1128 dlist_elt_t *per_user_list_head_elt;
1130 user_key.addr.as_u32 = addr->as_u32;
1131 user_key.fib_index = fib_index;
1132 kv.key = user_key.as_u64;
1134 /* Ever heard of the "user" = src ip4 address before? */
1135 if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1137 if (pool_elts (tnm->users) >= nm->max_users_per_thread)
1139 vlib_increment_simple_counter (&nm->user_limit_reached, thread_index,
1141 nat_elog_warn (nm, "maximum user limit reached");
1144 /* no, make a new one */
1145 pool_get (tnm->users, u);
1146 clib_memset (u, 0, sizeof (*u));
1148 u->addr.as_u32 = addr->as_u32;
1149 u->fib_index = fib_index;
1151 pool_get (tnm->list_pool, per_user_list_head_elt);
1153 u->sessions_per_user_list_head_index =
1154 per_user_list_head_elt - tnm->list_pool;
1156 clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index);
1158 kv.value = u - tnm->users;
1161 if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1))
1163 nat_elog_warn (nm, "user_hash key add failed");
1164 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1168 vlib_set_simple_counter (&nm->total_users, thread_index, 0,
1169 pool_elts (tnm->users));
1173 u = pool_elt_at_index (tnm->users, value.value);
1179 nat44_ei_session_t *
1180 nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u,
1181 u32 thread_index, f64 now)
1183 nat44_ei_session_t *s;
1184 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1185 u32 oldest_per_user_translation_list_index, session_index;
1186 dlist_elt_t *oldest_per_user_translation_list_elt;
1187 dlist_elt_t *per_user_translation_list_elt;
1189 /* Over quota? Recycle the least recently used translation */
1190 if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user)
1192 oldest_per_user_translation_list_index = clib_dlist_remove_head (
1193 tnm->list_pool, u->sessions_per_user_list_head_index);
1195 ASSERT (oldest_per_user_translation_list_index != ~0);
1197 /* Add it back to the end of the LRU list */
1198 clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index,
1199 oldest_per_user_translation_list_index);
1200 /* Get the list element */
1201 oldest_per_user_translation_list_elt = pool_elt_at_index (
1202 tnm->list_pool, oldest_per_user_translation_list_index);
1204 /* Get the session index from the list element */
1205 session_index = oldest_per_user_translation_list_elt->value;
1207 /* Get the session */
1208 s = pool_elt_at_index (tnm->sessions, session_index);
1210 nat44_ei_free_session_data_v2 (nm, s, thread_index, 0);
1211 if (nat44_ei_is_session_static (s))
1212 u->nstaticsessions--;
1219 s->ext_host_addr.as_u32 = 0;
1220 s->ext_host_port = 0;
1221 s->ext_host_nat_addr.as_u32 = 0;
1222 s->ext_host_nat_port = 0;
1226 pool_get (tnm->sessions, s);
1227 clib_memset (s, 0, sizeof (*s));
1229 /* Create list elts */
1230 pool_get (tnm->list_pool, per_user_translation_list_elt);
1231 clib_dlist_init (tnm->list_pool,
1232 per_user_translation_list_elt - tnm->list_pool);
1234 per_user_translation_list_elt->value = s - tnm->sessions;
1235 s->per_user_index = per_user_translation_list_elt - tnm->list_pool;
1236 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
1238 clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index,
1239 per_user_translation_list_elt - tnm->list_pool);
1241 s->user_index = u - tnm->users;
1242 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1243 pool_elts (tnm->sessions));
1246 s->ha_last_refreshed = now;
1252 nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s,
1253 u32 thread_index, u8 is_ha)
1255 clib_bihash_kv_8_8_t kv;
1257 init_nat_i2o_k (&kv, s);
1258 if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0))
1259 nat_elog_warn (nm, "in2out key del failed");
1261 init_nat_o2i_k (&kv, s);
1262 if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0))
1263 nat_elog_warn (nm, "out2in key del failed");
1267 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
1268 &s->in2out.addr, s->in2out.port,
1269 &s->out2in.addr, s->out2in.port, s->nat_proto);
1271 nat_ipfix_logging_nat44_ses_delete (
1272 thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32,
1273 s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index);
1275 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
1276 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
1280 if (nat44_ei_is_session_static (s))
1283 nat44_ei_free_outside_address_and_port (nm->addresses, thread_index,
1284 &s->out2in.addr, s->out2in.port,
1288 static_always_inline void
1289 nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index)
1292 nat44_ei_session_t *s;
1294 nat44_ei_main_t *nm = &nat44_ei_main;
1295 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
1299 pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index);
1300 // get first element
1301 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1303 while (elt->value != ~0)
1305 s = pool_elt_at_index (tnm->sessions, elt->value);
1306 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1308 nat44_ei_free_session_data (nm, s, thread_index, 0);
1309 nat44_ei_delete_session (nm, s, thread_index);
1314 nat44_ei_user_del (ip4_address_t *addr, u32 fib_index)
1318 nat44_ei_main_t *nm = &nat44_ei_main;
1319 nat44_ei_main_per_thread_data_t *tnm;
1321 nat44_ei_user_key_t user_key;
1322 clib_bihash_kv_8_8_t kv, value;
1324 user_key.addr.as_u32 = addr->as_u32;
1325 user_key.fib_index = fib_index;
1326 kv.key = user_key.as_u64;
1328 if (nm->num_workers > 1)
1330 vec_foreach (tnm, nm->per_thread_data)
1332 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1334 nat44_ei_user_del_sessions (
1335 pool_elt_at_index (tnm->users, value.value),
1344 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1345 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1347 nat44_ei_user_del_sessions (
1348 pool_elt_at_index (tnm->users, value.value), tnm->thread_index);
1356 nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm,
1357 nat44_ei_main_per_thread_data_t *tnm,
1358 nat44_ei_user_key_t u_key, int addr_only,
1359 ip4_address_t e_addr, u16 e_port)
1361 clib_bihash_kv_8_8_t kv, value;
1362 kv.key = u_key.as_u64;
1364 dlist_elt_t *head, *elt;
1366 nat44_ei_session_t *s;
1367 u32 elt_index, head_index, ses_index;
1369 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1371 user_index = value.value;
1372 u = pool_elt_at_index (tnm->users, user_index);
1373 if (u->nstaticsessions)
1375 head_index = u->sessions_per_user_list_head_index;
1376 head = pool_elt_at_index (tnm->list_pool, head_index);
1377 elt_index = head->next;
1378 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1379 ses_index = elt->value;
1380 while (ses_index != ~0)
1382 s = pool_elt_at_index (tnm->sessions, ses_index);
1383 elt = pool_elt_at_index (tnm->list_pool, elt->next);
1384 ses_index = elt->value;
1388 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1389 (s->out2in.port != e_port))
1393 if (!nat44_ei_is_session_static (s))
1396 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data,
1398 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1408 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
1411 nat44_ei_main_t *nm = &nat44_ei_main;
1412 u32 next_worker_index = 0;
1415 next_worker_index = nm->first_worker_index;
1416 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
1417 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
1419 if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers))))
1420 next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)];
1422 next_worker_index += nm->workers[hash % _vec_len (nm->workers)];
1424 return next_worker_index;
1428 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
1429 u32 rx_fib_index0, u8 is_output)
1431 nat44_ei_main_t *nm = &nat44_ei_main;
1434 clib_bihash_kv_8_8_t kv, value;
1435 nat44_ei_static_mapping_t *m;
1437 u32 next_worker_index = 0;
1439 /* first try static mappings without port */
1440 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1442 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
1443 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1446 m = pool_elt_at_index (nm->static_mappings, value.value);
1447 return m->workers[0];
1451 proto = ip_proto_to_nat_proto (ip0->protocol);
1452 udp = ip4_next_header (ip0);
1453 port = vnet_buffer (b)->ip.reass.l4_dst_port;
1455 /* unknown protocol */
1456 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
1458 /* use current thread */
1459 return vlib_get_thread_index ();
1462 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
1464 icmp46_header_t *icmp = (icmp46_header_t *) udp;
1465 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
1466 if (!icmp_type_is_error_message (
1467 vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
1468 port = vnet_buffer (b)->ip.reass.l4_src_port;
1471 /* if error message, then it's not fragmented and we can access it */
1472 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
1473 proto = ip_proto_to_nat_proto (inner_ip->protocol);
1474 void *l4_header = ip4_next_header (inner_ip);
1477 case NAT_PROTOCOL_ICMP:
1478 icmp = (icmp46_header_t *) l4_header;
1479 echo = (icmp_echo_header_t *) (icmp + 1);
1480 port = echo->identifier;
1482 case NAT_PROTOCOL_UDP:
1483 case NAT_PROTOCOL_TCP:
1484 port = ((tcp_udp_header_t *) l4_header)->src_port;
1487 return vlib_get_thread_index ();
1492 /* try static mappings with port */
1493 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
1495 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
1496 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
1499 m = pool_elt_at_index (nm->static_mappings, value.value);
1500 return m->workers[0];
1504 /* worker by outside port */
1505 next_worker_index = nm->first_worker_index;
1506 next_worker_index +=
1507 nm->workers[(clib_net_to_host_u16 (port) - 1024) / nm->port_per_thread];
1508 return next_worker_index;
1512 nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index,
1513 u32 thread_index, nat_protocol_t proto,
1514 ip4_address_t s_addr, ip4_address_t *addr,
1515 u16 *port, u16 port_per_thread,
1516 u32 snat_thread_index)
1518 nat44_ei_main_t *nm = &nat44_ei_main;
1519 nat44_ei_address_t *a, *ga = 0;
1523 if (vec_len (addresses) > 0)
1526 int s_addr_offset = s_addr.as_u32 % vec_len (addresses);
1528 for (i = s_addr_offset; i < vec_len (addresses); ++i)
1533 #define _(N, j, n, s) \
1534 case NAT_PROTOCOL_##N: \
1535 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1537 if (a->fib_index == fib_index) \
1541 portnum = (port_per_thread * snat_thread_index) + \
1542 nat_random_port (&nm->random_seed, 0, \
1543 port_per_thread - 1) + \
1545 if (a->busy_##n##_port_refcounts[portnum]) \
1547 --a->busy_##n##_port_refcounts[portnum]; \
1548 a->busy_##n##_ports_per_thread[thread_index]++; \
1549 a->busy_##n##_ports++; \
1551 *port = clib_host_to_net_u16 (portnum); \
1555 else if (a->fib_index == ~0) \
1561 foreach_nat_protocol;
1563 nat_elog_info (nm, "unknown protocol");
1568 for (i = 0; i < s_addr_offset; ++i)
1573 foreach_nat_protocol;
1575 nat_elog_info (nm, "unknown protocol");
1582 // fake fib index to reuse macro
1586 foreach_nat_protocol;
1587 default : nat_elog_info (nm, "unknown protocol");
1595 /* Totally out of translations to use... */
1596 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1601 nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index,
1602 u32 thread_index, nat_protocol_t proto,
1603 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1604 u16 port_per_thread, u32 snat_thread_index)
1606 nat44_ei_main_t *nm = &nat44_ei_main;
1607 nat44_ei_address_t *a = addresses;
1610 ports = nm->end_port - nm->start_port + 1;
1612 if (!vec_len (addresses))
1617 #define _(N, i, n, s) \
1618 case NAT_PROTOCOL_##N: \
1619 if (a->busy_##n##_ports < ports) \
1623 portnum = nat_random_port (&nm->random_seed, nm->start_port, \
1625 if (a->busy_##n##_port_refcounts[portnum]) \
1627 ++a->busy_##n##_port_refcounts[portnum]; \
1628 a->busy_##n##_ports++; \
1630 *port = clib_host_to_net_u16 (portnum); \
1635 foreach_nat_protocol
1637 default : nat_elog_info (nm, "unknown protocol");
1642 /* Totally out of translations to use... */
1643 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1648 nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index,
1649 u32 thread_index, nat_protocol_t proto,
1650 ip4_address_t s_addr, ip4_address_t *addr, u16 *port,
1651 u16 port_per_thread, u32 snat_thread_index)
1653 nat44_ei_main_t *nm = &nat44_ei_main;
1654 nat44_ei_address_t *a = addresses;
1655 u16 m, ports, portnum, A, j;
1656 m = 16 - (nm->psid_offset + nm->psid_length);
1657 ports = (1 << (16 - nm->psid_length)) - (1 << m);
1659 if (!vec_len (addresses))
1664 #define _(N, i, n, s) \
1665 case NAT_PROTOCOL_##N: \
1666 if (a->busy_##n##_ports < ports) \
1670 A = nat_random_port (&nm->random_seed, 1, \
1671 pow2_mask (nm->psid_offset)); \
1672 j = nat_random_port (&nm->random_seed, 0, pow2_mask (m)); \
1673 portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m)); \
1674 if (a->busy_##n##_port_refcounts[portnum]) \
1676 ++a->busy_##n##_port_refcounts[portnum]; \
1677 a->busy_##n##_ports++; \
1679 *port = clib_host_to_net_u16 (portnum); \
1684 foreach_nat_protocol
1686 default : nat_elog_info (nm, "unknown protocol");
1691 /* Totally out of translations to use... */
1692 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
1697 nat44_ei_set_alloc_default ()
1699 nat44_ei_main_t *nm = &nat44_ei_main;
1701 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1702 nm->alloc_addr_and_port = nat44_ei_alloc_default_cb;
1706 nat44_ei_set_alloc_range (u16 start_port, u16 end_port)
1708 nat44_ei_main_t *nm = &nat44_ei_main;
1710 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE;
1711 nm->alloc_addr_and_port = nat44_ei_alloc_range_cb;
1712 nm->start_port = start_port;
1713 nm->end_port = end_port;
1717 nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
1719 nat44_ei_main_t *nm = &nat44_ei_main;
1721 nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE;
1722 nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb;
1724 nm->psid_offset = psid_offset;
1725 nm->psid_length = psid_length;
1729 nat44_ei_add_static_mapping_when_resolved (ip4_address_t l_addr, u16 l_port,
1730 u16 e_port, nat_protocol_t proto,
1731 u32 sw_if_index, u32 vrf_id,
1732 int addr_only, int identity_nat,
1735 nat44_ei_main_t *nm = &nat44_ei_main;
1736 nat44_ei_static_map_resolve_t *rp;
1738 vec_add2 (nm->to_resolve, rp, 1);
1739 clib_memset (rp, 0, sizeof (*rp));
1741 rp->l_addr.as_u32 = l_addr.as_u32;
1742 rp->l_port = l_port;
1743 rp->e_port = e_port;
1744 rp->sw_if_index = sw_if_index;
1745 rp->vrf_id = vrf_id;
1747 rp->addr_only = addr_only;
1748 rp->identity_nat = identity_nat;
1749 rp->tag = vec_dup (tag);
1753 nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses,
1756 nat44_ei_main_per_thread_data_t *tnm =
1757 vec_elt_at_index (nm->per_thread_data, thread_index);
1758 clib_bihash_kv_8_8_t kv, value;
1760 const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr,
1761 .fib_index = ses->in2out.fib_index };
1762 const u8 u_static = nat44_ei_is_session_static (ses);
1764 clib_dlist_remove (tnm->list_pool, ses->per_user_index);
1765 pool_put_index (tnm->list_pool, ses->per_user_index);
1767 pool_put (tnm->sessions, ses);
1768 vlib_set_simple_counter (&nm->total_sessions, thread_index, 0,
1769 pool_elts (tnm->sessions));
1771 kv.key = u_key.as_u64;
1772 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
1774 u = pool_elt_at_index (tnm->users, value.value);
1776 u->nstaticsessions--;
1780 nat44_ei_delete_user_with_no_session (nm, u, thread_index);
1785 nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port,
1786 nat_protocol_t proto, u32 vrf_id, int is_in)
1788 nat44_ei_main_per_thread_data_t *tnm;
1789 clib_bihash_kv_8_8_t kv, value;
1790 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1791 nat44_ei_session_t *s;
1792 clib_bihash_8_8_t *t;
1794 init_nat_k (&kv, *addr, port, fib_index, proto);
1795 t = is_in ? &nm->in2out : &nm->out2in;
1796 if (!clib_bihash_search_8_8 (t, &kv, &value))
1798 // this is called from API/CLI, so the world is stopped here
1799 // it's safe to manipulate arbitrary per-thread data
1800 u32 thread_index = nat_value_get_thread_index (&value);
1801 tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
1802 u32 session_index = nat_value_get_session_index (&value);
1803 if (pool_is_free_index (tnm->sessions, session_index))
1804 return VNET_API_ERROR_UNSPECIFIED;
1806 s = pool_elt_at_index (tnm->sessions, session_index);
1807 nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0);
1808 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1812 return VNET_API_ERROR_NO_SUCH_ENTRY;
1816 nat44_ei_get_thread_idx_by_port (u16 e_port)
1818 nat44_ei_main_t *nm = &nat44_ei_main;
1819 u32 thread_idx = nm->num_workers;
1820 if (nm->num_workers > 1)
1822 thread_idx = nm->first_worker_index +
1823 nm->workers[(e_port - 1024) / nm->port_per_thread];
1829 nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index,
1832 nat44_ei_main_t *nm = &nat44_ei_main;
1833 fib_prefix_t prefix = {
1835 .fp_proto = FIB_PROTOCOL_IP4,
1837 .ip4.as_u32 = addr->as_u32,
1840 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
1843 fib_table_entry_update_one_path (
1844 fib_index, &prefix, nm->fib_src_low,
1845 (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL |
1846 FIB_ENTRY_FLAG_EXCLUSIVE),
1847 DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
1849 fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low);
1853 nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
1854 u16 l_port, u16 e_port, nat_protocol_t proto,
1855 u32 sw_if_index, u32 vrf_id, u8 addr_only,
1856 u8 identity_nat, u8 *tag, u8 is_add)
1858 nat44_ei_main_t *nm = &nat44_ei_main;
1859 nat44_ei_static_mapping_t *m = 0;
1860 clib_bihash_kv_8_8_t kv, value;
1861 nat44_ei_address_t *a = 0;
1863 nat44_ei_interface_t *interface;
1864 nat44_ei_main_per_thread_data_t *tnm;
1865 nat44_ei_user_key_t u_key;
1867 dlist_elt_t *head, *elt;
1868 u32 elt_index, head_index;
1871 nat44_ei_session_t *s;
1872 nat44_ei_static_map_resolve_t *rp, *rp_match = 0;
1873 nat44_ei_lb_addr_port_t *local;
1877 if (sw_if_index != ~0)
1879 ip4_address_t *first_int_addr;
1881 for (i = 0; i < vec_len (nm->to_resolve); i++)
1883 rp = nm->to_resolve + i;
1884 if (rp->sw_if_index != sw_if_index ||
1885 rp->l_addr.as_u32 != l_addr.as_u32 || rp->vrf_id != vrf_id ||
1886 rp->addr_only != addr_only)
1891 if ((rp->l_port != l_port && rp->e_port != e_port) ||
1900 /* Might be already set... */
1901 first_int_addr = ip4_interface_first_address (
1902 nm->ip4_main, sw_if_index, 0 /* just want the address */);
1907 return VNET_API_ERROR_VALUE_EXIST;
1909 nat44_ei_add_static_mapping_when_resolved (
1910 l_addr, l_port, e_port, proto, sw_if_index, vrf_id, addr_only,
1913 /* DHCP resolution required? */
1914 if (!first_int_addr)
1917 e_addr.as_u32 = first_int_addr->as_u32;
1918 /* Identity mapping? */
1919 if (l_addr.as_u32 == 0)
1920 l_addr.as_u32 = e_addr.as_u32;
1925 return VNET_API_ERROR_NO_SUCH_ENTRY;
1927 vec_del1 (nm->to_resolve, i);
1929 if (!first_int_addr)
1932 e_addr.as_u32 = first_int_addr->as_u32;
1933 /* Identity mapping? */
1934 if (l_addr.as_u32 == 0)
1935 l_addr.as_u32 = e_addr.as_u32;
1939 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
1940 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
1941 m = pool_elt_at_index (nm->static_mappings, value.value);
1947 // identity mapping for second vrf
1948 if (nat44_ei_is_identity_static_mapping (m))
1950 pool_foreach (local, m->locals)
1952 if (local->vrf_id == vrf_id)
1953 return VNET_API_ERROR_VALUE_EXIST;
1955 pool_get (m->locals, local);
1956 local->vrf_id = vrf_id;
1957 local->fib_index = fib_table_find_or_create_and_lock (
1958 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
1959 init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index,
1960 m->proto, 0, m - nm->static_mappings);
1961 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
1964 return VNET_API_ERROR_VALUE_EXIST;
1967 /* Convert VRF id to FIB index */
1970 fib_index = fib_table_find_or_create_and_lock (
1971 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
1973 /* If not specified use inside VRF id from NAT44 plugin config */
1976 fib_index = nm->inside_fib_index;
1977 vrf_id = nm->inside_vrf_id;
1978 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
1983 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
1984 addr_only ? 0 : proto);
1985 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
1987 return VNET_API_ERROR_VALUE_EXIST;
1990 /* Find external address in allocated addresses and reserve port for
1991 address and port pair mapping when dynamic translations enabled */
1992 if (!(addr_only || nm->static_mapping_only))
1994 for (i = 0; i < vec_len (nm->addresses); i++)
1996 if (nm->addresses[i].addr.as_u32 == e_addr.as_u32)
1998 a = nm->addresses + i;
1999 /* External port must be unused */
2002 #define _(N, j, n, s) \
2003 case NAT_PROTOCOL_##N: \
2004 if (a->busy_##n##_port_refcounts[e_port]) \
2005 return VNET_API_ERROR_INVALID_VALUE; \
2006 ++a->busy_##n##_port_refcounts[e_port]; \
2007 if (e_port > 1024) \
2009 a->busy_##n##_ports++; \
2010 a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port ( \
2014 foreach_nat_protocol
2016 default : nat_elog_info (nm, "unknown protocol");
2017 return VNET_API_ERROR_INVALID_VALUE_2;
2022 /* External address must be allocated */
2023 if (!a && (l_addr.as_u32 != e_addr.as_u32))
2025 if (sw_if_index != ~0)
2027 for (i = 0; i < vec_len (nm->to_resolve); i++)
2029 rp = nm->to_resolve + i;
2032 if (rp->sw_if_index != sw_if_index &&
2033 rp->l_addr.as_u32 != l_addr.as_u32 &&
2034 rp->vrf_id != vrf_id && rp->l_port != l_port &&
2035 rp->e_port != e_port && rp->proto != proto)
2038 vec_del1 (nm->to_resolve, i);
2042 return VNET_API_ERROR_NO_SUCH_ENTRY;
2046 pool_get (nm->static_mappings, m);
2047 clib_memset (m, 0, sizeof (*m));
2048 m->tag = vec_dup (tag);
2049 m->local_addr = l_addr;
2050 m->external_addr = e_addr;
2053 m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_ADDR_ONLY;
2056 m->local_port = l_port;
2057 m->external_port = e_port;
2063 m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_IDENTITY_NAT;
2064 pool_get (m->locals, local);
2065 local->vrf_id = vrf_id;
2066 local->fib_index = fib_index;
2071 m->fib_index = fib_index;
2074 if (nm->num_workers > 1)
2077 .src_address = m->local_addr,
2079 vec_add1 (m->workers,
2080 nat44_ei_get_in2out_worker_index (&ip, m->fib_index, 0));
2081 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2084 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2086 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0,
2087 m - nm->static_mappings);
2088 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1);
2090 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0,
2091 m - nm->static_mappings);
2092 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1);
2094 /* Delete dynamic sessions matching local address (+ local port) */
2095 // TODO: based on type of NAT EI/ED
2096 if (!(nm->static_mapping_only))
2098 u_key.addr = m->local_addr;
2099 u_key.fib_index = m->fib_index;
2100 kv.key = u_key.as_u64;
2101 if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value))
2103 user_index = value.value;
2104 u = pool_elt_at_index (tnm->users, user_index);
2107 head_index = u->sessions_per_user_list_head_index;
2108 head = pool_elt_at_index (tnm->list_pool, head_index);
2109 elt_index = head->next;
2110 elt = pool_elt_at_index (tnm->list_pool, elt_index);
2111 ses_index = elt->value;
2112 while (ses_index != ~0)
2114 s = pool_elt_at_index (tnm->sessions, ses_index);
2115 elt = pool_elt_at_index (tnm->list_pool, elt->next);
2116 ses_index = elt->value;
2118 if (nat44_ei_is_session_static (s))
2121 if (!addr_only && s->in2out.port != m->local_port)
2124 nat44_ei_free_session_data_v2 (
2125 nm, s, tnm - nm->per_thread_data, 0);
2126 nat44_ei_delete_session (nm, s,
2127 tnm - nm->per_thread_data);
2140 if (sw_if_index != ~0)
2143 return VNET_API_ERROR_NO_SUCH_ENTRY;
2149 vrf_id = nm->inside_vrf_id;
2151 pool_foreach (local, m->locals)
2153 if (local->vrf_id == vrf_id)
2154 find = local - m->locals;
2157 return VNET_API_ERROR_NO_SUCH_ENTRY;
2159 local = pool_elt_at_index (m->locals, find);
2160 fib_index = local->fib_index;
2161 pool_put (m->locals, local);
2164 fib_index = m->fib_index;
2166 /* Free external address port */
2167 if (!(addr_only || nm->static_mapping_only))
2169 for (i = 0; i < vec_len (nm->addresses); i++)
2171 if (nm->addresses[i].addr.as_u32 == e_addr.as_u32)
2173 a = nm->addresses + i;
2176 #define _(N, j, n, s) \
2177 case NAT_PROTOCOL_##N: \
2178 --a->busy_##n##_port_refcounts[e_port]; \
2179 if (e_port > 1024) \
2181 a->busy_##n##_ports--; \
2182 a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port ( \
2186 foreach_nat_protocol
2188 default : return VNET_API_ERROR_INVALID_VALUE_2;
2195 if (nm->num_workers > 1)
2196 tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]);
2198 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
2200 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
2201 clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0);
2203 /* Delete session(s) for static mapping if exist */
2204 if (!(nm->static_mapping_only) ||
2205 (nm->static_mapping_only && nm->static_mapping_connection_tracking))
2207 u_key.addr = m->local_addr;
2208 u_key.fib_index = fib_index;
2209 kv.key = u_key.as_u64;
2210 nat44_ei_static_mapping_del_sessions (nm, tnm, u_key, addr_only,
2214 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2215 if (pool_elts (m->locals))
2218 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
2219 clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0);
2222 vec_free (m->workers);
2223 /* Delete static mapping from pool */
2224 pool_put (nm->static_mappings, m);
2227 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
2230 /* Add/delete external address to FIB */
2231 pool_foreach (interface, nm->interfaces)
2233 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2236 nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index,
2240 pool_foreach (interface, nm->output_feature_interfaces)
2242 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2245 nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index,
2253 nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port,
2254 u32 match_fib_index,
2255 nat_protocol_t match_protocol,
2256 ip4_address_t *mapping_addr, u16 *mapping_port,
2257 u32 *mapping_fib_index, u8 by_external,
2258 u8 *is_addr_only, u8 *is_identity_nat)
2260 nat44_ei_main_t *nm = &nat44_ei_main;
2261 clib_bihash_kv_8_8_t kv, value;
2262 nat44_ei_static_mapping_t *m;
2267 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2268 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2271 /* Try address only mapping */
2272 init_nat_k (&kv, match_addr, 0, 0, 0);
2273 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv,
2277 m = pool_elt_at_index (nm->static_mappings, value.value);
2279 *mapping_fib_index = m->fib_index;
2280 *mapping_addr = m->local_addr;
2281 port = m->local_port;
2285 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2287 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value))
2289 /* Try address only mapping */
2290 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2291 if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv,
2295 m = pool_elt_at_index (nm->static_mappings, value.value);
2297 *mapping_fib_index = nm->outside_fib_index;
2298 *mapping_addr = m->external_addr;
2299 port = m->external_port;
2302 /* Address only mapping doesn't change port */
2303 if (nat44_ei_is_addr_only_static_mapping (m))
2304 *mapping_port = match_port;
2306 *mapping_port = port;
2308 if (PREDICT_FALSE (is_addr_only != 0))
2309 *is_addr_only = nat44_ei_is_addr_only_static_mapping (m);
2311 if (PREDICT_FALSE (is_identity_nat != 0))
2312 *is_identity_nat = nat44_ei_is_identity_static_mapping (m);
2318 nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm)
2320 pool_free (tnm->list_pool);
2321 pool_free (tnm->lru_pool);
2322 pool_free (tnm->sessions);
2323 pool_free (tnm->users);
2325 clib_bihash_free_8_8 (&tnm->user_hash);
2329 format_nat44_ei_key (u8 *s, va_list *args)
2331 u64 key = va_arg (*args, u64);
2335 nat_protocol_t protocol;
2338 split_nat_key (key, &addr, &port, &fib_index, &protocol);
2340 s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr,
2341 format_nat_protocol, protocol, clib_net_to_host_u16 (port),
2347 format_nat44_ei_user_kvp (u8 *s, va_list *args)
2349 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2350 nat44_ei_user_key_t k;
2354 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2355 k.fib_index, v->value);
2361 format_nat44_ei_session_kvp (u8 *s, va_list *args)
2363 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2365 s = format (s, "%U thread-index %llu session-index %llu",
2366 format_nat44_ei_key, v->key, nat_value_get_thread_index (v),
2367 nat_value_get_session_index (v));
2373 format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args)
2375 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2377 s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key,
2384 nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm,
2385 u32 translations, u32 translation_buckets,
2390 pool_alloc (tnm->list_pool, translations);
2391 pool_alloc (tnm->lru_pool, translations);
2392 pool_alloc (tnm->sessions, translations);
2394 clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0);
2396 clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash,
2397 format_nat44_ei_user_kvp);
2399 pool_get (tnm->lru_pool, head);
2400 tnm->tcp_trans_lru_head_index = head - tnm->lru_pool;
2401 clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index);
2403 pool_get (tnm->lru_pool, head);
2404 tnm->tcp_estab_lru_head_index = head - tnm->lru_pool;
2405 clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index);
2407 pool_get (tnm->lru_pool, head);
2408 tnm->udp_lru_head_index = head - tnm->lru_pool;
2409 clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index);
2411 pool_get (tnm->lru_pool, head);
2412 tnm->icmp_lru_head_index = head - tnm->lru_pool;
2413 clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index);
2415 pool_get (tnm->lru_pool, head);
2416 tnm->unk_proto_lru_head_index = head - tnm->lru_pool;
2417 clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index);
2423 nat44_ei_main_t *nm = &nat44_ei_main;
2424 nat44_ei_main_per_thread_data_t *tnm;
2426 pool_free (nm->static_mappings);
2427 clib_bihash_free_8_8 (&nm->static_mapping_by_local);
2428 clib_bihash_free_8_8 (&nm->static_mapping_by_external);
2432 clib_bihash_free_8_8 (&nm->in2out);
2433 clib_bihash_free_8_8 (&nm->out2in);
2434 vec_foreach (tnm, nm->per_thread_data)
2436 nat44_ei_worker_db_free (tnm);
2442 nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets)
2444 nat44_ei_main_t *nm = &nat44_ei_main;
2445 nat44_ei_main_per_thread_data_t *tnm;
2447 u32 static_mapping_buckets = 1024;
2448 u32 static_mapping_memory_size = 64 << 20;
2450 clib_bihash_init_8_8 (&nm->static_mapping_by_local,
2451 "static_mapping_by_local", static_mapping_buckets,
2452 static_mapping_memory_size);
2453 clib_bihash_init_8_8 (&nm->static_mapping_by_external,
2454 "static_mapping_by_external", static_mapping_buckets,
2455 static_mapping_memory_size);
2456 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local,
2457 format_nat44_ei_static_mapping_kvp);
2458 clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external,
2459 format_nat44_ei_static_mapping_kvp);
2463 clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0);
2464 clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0);
2465 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2466 format_nat44_ei_session_kvp);
2467 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2468 format_nat44_ei_session_kvp);
2469 vec_foreach (tnm, nm->per_thread_data)
2471 nat44_ei_worker_db_init (tnm, translations, translation_buckets,
2478 nat44_ei_sessions_clear ()
2480 nat44_ei_main_t *nm = &nat44_ei_main;
2481 nat44_ei_main_per_thread_data_t *tnm;
2485 clib_bihash_free_8_8 (&nm->in2out);
2486 clib_bihash_free_8_8 (&nm->out2in);
2487 clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0);
2488 clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0);
2489 clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out,
2490 format_nat44_ei_session_kvp);
2491 clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in,
2492 format_nat44_ei_session_kvp);
2493 vec_foreach (tnm, nm->per_thread_data)
2495 nat44_ei_worker_db_free (tnm);
2496 nat44_ei_worker_db_init (tnm, nm->translations,
2497 nm->translation_buckets, nm->user_buckets);
2501 vlib_zero_simple_counter (&nm->total_users, 0);
2502 vlib_zero_simple_counter (&nm->total_sessions, 0);
2503 vlib_zero_simple_counter (&nm->user_limit_reached, 0);
2507 nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index,
2508 u32 new_fib_index, u32 old_fib_index)
2510 nat44_ei_main_t *nm = &nat44_ei_main;
2511 nat44_ei_outside_fib_t *outside_fib;
2512 nat44_ei_interface_t *i;
2516 if (!nm->enabled || (new_fib_index == old_fib_index) ||
2517 (!vec_len (nm->outside_fibs)))
2522 pool_foreach (i, nm->interfaces)
2524 if (i->sw_if_index == sw_if_index)
2526 if (!(nat44_ei_interface_is_outside (i)))
2532 pool_foreach (i, nm->output_feature_interfaces)
2534 if (i->sw_if_index == sw_if_index)
2536 if (!(nat44_ei_interface_is_outside (i)))
2545 vec_foreach (outside_fib, nm->outside_fibs)
2547 if (outside_fib->fib_index == old_fib_index)
2549 outside_fib->refcount--;
2550 if (!outside_fib->refcount)
2551 vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs);
2556 vec_foreach (outside_fib, nm->outside_fibs)
2558 if (outside_fib->fib_index == new_fib_index)
2560 outside_fib->refcount++;
2568 vec_add2 (nm->outside_fibs, outside_fib, 1);
2569 outside_fib->refcount = 1;
2570 outside_fib->fib_index = new_fib_index;
2575 nat44_ei_add_address (nat44_ei_main_t *nm, ip4_address_t *addr, u32 vrf_id)
2577 nat44_ei_address_t *ap;
2578 nat44_ei_interface_t *i;
2579 vlib_thread_main_t *tm = vlib_get_thread_main ();
2581 /* Check if address already exists */
2582 vec_foreach (ap, nm->addresses)
2584 if (ap->addr.as_u32 == addr->as_u32)
2586 nat44_ei_log_err ("address exist");
2587 return VNET_API_ERROR_VALUE_EXIST;
2591 vec_add2 (nm->addresses, ap, 1);
2595 ap->fib_index = fib_table_find_or_create_and_lock (
2596 FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low);
2600 #define _(N, i, n, s) \
2601 clib_memset (ap->busy_##n##_port_refcounts, 0, \
2602 sizeof (ap->busy_##n##_port_refcounts)); \
2603 ap->busy_##n##_ports = 0; \
2604 ap->busy_##n##_ports_per_thread = 0; \
2605 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, \
2606 tm->n_vlib_mains - 1, 0);
2607 foreach_nat_protocol
2610 /* Add external address to FIB */
2611 pool_foreach (i, nm->interfaces)
2613 if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo)
2616 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
2619 pool_foreach (i, nm->output_feature_interfaces)
2621 if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo)
2624 nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1);
2632 nat44_ei_add_interface_address (nat44_ei_main_t *nm, u32 sw_if_index,
2635 ip4_main_t *ip4_main = nm->ip4_main;
2636 ip4_address_t *first_int_addr;
2637 nat44_ei_static_map_resolve_t *rp;
2638 u32 *indices_to_delete = 0;
2640 u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices;
2642 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2643 0 /* just want the address */);
2645 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
2647 if (auto_add_sw_if_indices[i] == sw_if_index)
2651 /* if have address remove it */
2653 (void) nat44_ei_del_address (nm, first_int_addr[0], 1);
2656 for (j = 0; j < vec_len (nm->to_resolve); j++)
2658 rp = nm->to_resolve + j;
2659 if (rp->sw_if_index == sw_if_index)
2660 vec_add1 (indices_to_delete, j);
2662 if (vec_len (indices_to_delete))
2664 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
2665 vec_del1 (nm->to_resolve, j);
2666 vec_free (indices_to_delete);
2669 vec_del1 (nm->auto_add_sw_if_indices, i);
2672 return VNET_API_ERROR_VALUE_EXIST;
2679 return VNET_API_ERROR_NO_SUCH_ENTRY;
2681 /* add to the auto-address list */
2682 vec_add1 (nm->auto_add_sw_if_indices, sw_if_index);
2684 /* If the address is already bound - or static - add it now */
2686 (void) nat44_ei_add_address (nm, first_int_addr, ~0);
2692 nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr)
2694 nat44_ei_main_t *nm = &nat44_ei_main;
2695 nat44_ei_static_mapping_t *m;
2696 pool_foreach (m, nm->static_mappings)
2698 if (nat44_ei_is_addr_only_static_mapping (m) ||
2699 nat44_ei_is_identity_static_mapping (m))
2701 if (m->external_addr.as_u32 == addr.as_u32)
2708 nat44_ei_del_address (nat44_ei_main_t *nm, ip4_address_t addr, u8 delete_sm)
2710 nat44_ei_address_t *a = 0;
2711 nat44_ei_session_t *ses;
2712 u32 *ses_to_be_removed = 0, *ses_index;
2713 nat44_ei_main_per_thread_data_t *tnm;
2714 nat44_ei_interface_t *interface;
2715 nat44_ei_static_mapping_t *m;
2718 /* Find SNAT address */
2719 for (i = 0; i < vec_len (nm->addresses); i++)
2721 if (nm->addresses[i].addr.as_u32 == addr.as_u32)
2723 a = nm->addresses + i;
2729 nat44_ei_log_err ("no such address");
2730 return VNET_API_ERROR_NO_SUCH_ENTRY;
2735 pool_foreach (m, nm->static_mappings)
2737 if (m->external_addr.as_u32 == addr.as_u32)
2738 (void) nat44_ei_add_del_static_mapping (
2739 m->local_addr, m->external_addr, m->local_port, m->external_port,
2740 m->proto, ~0 /* sw_if_index */, m->vrf_id,
2741 nat44_ei_is_addr_only_static_mapping (m),
2742 nat44_ei_is_identity_static_mapping (m), m->tag, 0);
2747 /* Check if address is used in some static mapping */
2748 if (nat44_ei_is_address_used_in_static_mapping (addr))
2750 nat44_ei_log_err ("address used in static mapping");
2751 return VNET_API_ERROR_UNSPECIFIED;
2755 if (a->fib_index != ~0)
2756 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low);
2758 /* Delete sessions using address */
2759 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
2761 vec_foreach (tnm, nm->per_thread_data)
2763 pool_foreach (ses, tnm->sessions)
2765 if (ses->out2in.addr.as_u32 == addr.as_u32)
2767 nat44_ei_free_session_data (nm, ses,
2768 tnm - nm->per_thread_data, 0);
2769 vec_add1 (ses_to_be_removed, ses - tnm->sessions);
2772 vec_foreach (ses_index, ses_to_be_removed)
2774 ses = pool_elt_at_index (tnm->sessions, ses_index[0]);
2775 nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data);
2777 vec_free (ses_to_be_removed);
2781 #define _(N, i, n, s) vec_free (a->busy_##n##_ports_per_thread);
2782 foreach_nat_protocol
2784 vec_del1 (nm->addresses, i);
2786 /* Delete external address from FIB */
2787 pool_foreach (interface, nm->interfaces)
2789 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2791 nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
2795 pool_foreach (interface, nm->output_feature_interfaces)
2797 if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo)
2799 nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0);
2807 nat44_ei_ip4_add_del_interface_address_cb (ip4_main_t *im, uword opaque,
2809 ip4_address_t *address,
2811 u32 if_address_index, u32 is_delete)
2813 nat44_ei_main_t *nm = &nat44_ei_main;
2814 nat44_ei_static_map_resolve_t *rp;
2815 ip4_address_t l_addr;
2818 nat44_ei_address_t *addresses = nm->addresses;
2823 for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++)
2825 if (sw_if_index == nm->auto_add_sw_if_indices[i])
2834 /* Don't trip over lease renewal, static config */
2835 for (j = 0; j < vec_len (addresses); j++)
2836 if (addresses[j].addr.as_u32 == address->as_u32)
2839 (void) nat44_ei_add_address (nm, address, ~0);
2840 /* Scan static map resolution vector */
2841 for (j = 0; j < vec_len (nm->to_resolve); j++)
2843 rp = nm->to_resolve + j;
2846 /* On this interface? */
2847 if (rp->sw_if_index == sw_if_index)
2849 /* Indetity mapping? */
2850 if (rp->l_addr.as_u32 == 0)
2851 l_addr.as_u32 = address[0].as_u32;
2853 l_addr.as_u32 = rp->l_addr.as_u32;
2854 /* Add the static mapping */
2855 rv = nat44_ei_add_del_static_mapping (
2856 l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
2857 ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only,
2858 rp->identity_nat, rp->tag, 1);
2860 nat_elog_notice_X1 (
2861 nm, "nat44_ei_add_del_static_mapping returned %d", "i4", rv);
2868 (void) nat44_ei_del_address (nm, address[0], 1);
2874 nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts)
2877 nat44_ei_main_t *nm = &nat44_ei_main;
2878 nm->frame_queue_nelts = frame_queue_nelts;
2883 nat44_ei_ip4_add_del_addr_only_sm_cb (ip4_main_t *im, uword opaque,
2884 u32 sw_if_index, ip4_address_t *address,
2885 u32 address_length, u32 if_address_index,
2888 nat44_ei_main_t *nm = &nat44_ei_main;
2889 nat44_ei_static_map_resolve_t *rp;
2890 nat44_ei_static_mapping_t *m;
2891 clib_bihash_kv_8_8_t kv, value;
2893 ip4_address_t l_addr;
2898 for (i = 0; i < vec_len (nm->to_resolve); i++)
2900 rp = nm->to_resolve + i;
2901 if (rp->addr_only == 0)
2903 if (rp->sw_if_index == sw_if_index)
2910 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
2911 nm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
2912 if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value))
2915 m = pool_elt_at_index (nm->static_mappings, value.value);
2919 /* Don't trip over lease renewal, static config */
2929 /* Indetity mapping? */
2930 if (rp->l_addr.as_u32 == 0)
2931 l_addr.as_u32 = address[0].as_u32;
2933 l_addr.as_u32 = rp->l_addr.as_u32;
2934 /* Add the static mapping */
2936 rv = nat44_ei_add_del_static_mapping (
2937 l_addr, address[0], rp->l_port, rp->e_port, rp->proto,
2938 ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only, rp->identity_nat, rp->tag,
2941 nat_elog_notice_X1 (nm, "nat44_ei_add_del_static_mapping returned %d",
2945 static_always_inline uword
2946 nat44_ei_classify_inline_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
2947 vlib_frame_t *frame)
2949 u32 n_left_from, *from, *to_next;
2950 nat44_ei_classify_next_t next_index;
2951 nat44_ei_main_t *nm = &nat44_ei_main;
2952 nat44_ei_static_mapping_t *m;
2953 u32 next_in2out = 0, next_out2in = 0;
2955 from = vlib_frame_vector_args (frame);
2956 n_left_from = frame->n_vectors;
2957 next_index = node->cached_next_index;
2959 while (n_left_from > 0)
2963 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
2965 while (n_left_from > 0 && n_left_to_next > 0)
2969 u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT;
2971 nat44_ei_address_t *ap;
2972 clib_bihash_kv_8_8_t kv0, value0;
2974 /* speculatively enqueue b0 to the current next frame */
2980 n_left_to_next -= 1;
2982 b0 = vlib_get_buffer (vm, bi0);
2983 ip0 = vlib_buffer_get_current (b0);
2985 vec_foreach (ap, nm->addresses)
2987 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
2989 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
2994 if (PREDICT_FALSE (pool_elts (nm->static_mappings)))
2996 init_nat_k (&kv0, ip0->dst_address, 0, 0, 0);
2997 /* try to classify the fragment based on IP header alone */
2998 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3001 m = pool_elt_at_index (nm->static_mappings, value0.value);
3002 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3003 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3006 init_nat_k (&kv0, ip0->dst_address,
3007 vnet_buffer (b0)->ip.reass.l4_dst_port, 0,
3008 ip_proto_to_nat_proto (ip0->protocol));
3009 if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external,
3012 m = pool_elt_at_index (nm->static_mappings, value0.value);
3013 if (m->local_addr.as_u32 != m->external_addr.as_u32)
3014 next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3019 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3020 (b0->flags & VLIB_BUFFER_IS_TRACED)))
3022 nat44_ei_classify_trace_t *t =
3023 vlib_add_trace (vm, node, b0, sizeof (*t));
3025 t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0;
3028 next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT;
3029 next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN;
3031 /* verify speculative enqueue, maybe switch current next frame */
3032 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
3033 n_left_to_next, bi0, next0);
3036 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
3039 vlib_node_increment_counter (
3040 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out);
3041 vlib_node_increment_counter (
3042 vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in);
3043 return frame->n_vectors;
3046 VLIB_NODE_FN (nat44_ei_classify_node)
3047 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3049 return nat44_ei_classify_inline_fn (vm, node, frame);
3052 VLIB_REGISTER_NODE (nat44_ei_classify_node) = {
3053 .name = "nat44-ei-classify",
3054 .vector_size = sizeof (u32),
3055 .format_trace = format_nat44_ei_classify_trace,
3056 .type = VLIB_NODE_TYPE_INTERNAL,
3057 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3058 .error_strings = nat44_ei_classify_error_strings,
3059 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3061 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out",
3062 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in",
3063 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3067 VLIB_NODE_FN (nat44_ei_handoff_classify_node)
3068 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
3070 return nat44_ei_classify_inline_fn (vm, node, frame);
3073 VLIB_REGISTER_NODE (nat44_ei_handoff_classify_node) = {
3074 .name = "nat44-ei-handoff-classify",
3075 .vector_size = sizeof (u32),
3076 .format_trace = format_nat44_ei_classify_trace,
3077 .type = VLIB_NODE_TYPE_INTERNAL,
3078 .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings),
3079 .error_strings = nat44_ei_classify_error_strings,
3080 .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT,
3082 [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out-worker-handoff",
3083 [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in-worker-handoff",
3084 [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop",
3089 * fd.io coding-style-patch-verification: ON
3092 * eval: (c-set-style "gnu")