2 * snat.c - simple nat plugin
4 * Copyright (c) 2016 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.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
23 #include <nat/nat_dpo.h>
24 #include <nat/lib/ipfix_logging.h>
25 #include <nat/lib/nat_syslog.h>
26 #include <nat/nat_inlines.h>
27 #include <nat/nat44/inlines.h>
28 #include <nat/nat_affinity.h>
29 #include <nat/nat_ha.h>
30 #include <vnet/fib/fib_table.h>
31 #include <vnet/fib/ip4_fib.h>
32 #include <vnet/ip/reass/ip4_sv_reass.h>
33 #include <vppinfra/bihash_16_8.h>
34 #include <nat/nat44/ed_inlines.h>
35 #include <vnet/ip/ip_table.h>
37 #include <vpp/app/version.h>
39 snat_main_t snat_main;
42 /* Hook up input features */
43 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
44 .arc_name = "ip4-unicast",
45 .node_name = "nat-pre-in2out",
46 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
47 "ip4-sv-reassembly-feature"),
49 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
50 .arc_name = "ip4-unicast",
51 .node_name = "nat-pre-out2in",
52 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
53 "ip4-dhcp-client-detect",
54 "ip4-sv-reassembly-feature"),
56 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
57 .arc_name = "ip4-unicast",
58 .node_name = "nat44-in2out-worker-handoff",
59 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
61 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
62 .arc_name = "ip4-unicast",
63 .node_name = "nat44-out2in-worker-handoff",
64 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
65 "ip4-dhcp-client-detect"),
67 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
68 .arc_name = "ip4-unicast",
69 .node_name = "nat44-in2out",
70 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
72 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
73 .arc_name = "ip4-unicast",
74 .node_name = "nat44-out2in",
75 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
76 "ip4-dhcp-client-detect"),
78 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
79 .arc_name = "ip4-unicast",
80 .node_name = "nat44-classify",
81 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
83 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
84 .arc_name = "ip4-unicast",
85 .node_name = "nat44-ed-in2out",
86 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
88 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
89 .arc_name = "ip4-unicast",
90 .node_name = "nat44-ed-out2in",
91 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
92 "ip4-dhcp-client-detect"),
94 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
95 .arc_name = "ip4-unicast",
96 .node_name = "nat44-ed-classify",
97 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
99 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
100 .arc_name = "ip4-unicast",
101 .node_name = "nat44-handoff-classify",
102 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
104 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
105 .arc_name = "ip4-unicast",
106 .node_name = "nat44-in2out-fast",
107 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
109 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
110 .arc_name = "ip4-unicast",
111 .node_name = "nat44-out2in-fast",
112 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
113 "ip4-dhcp-client-detect"),
115 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
116 .arc_name = "ip4-unicast",
117 .node_name = "nat44-hairpin-dst",
118 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
120 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
121 .arc_name = "ip4-unicast",
122 .node_name = "nat44-ed-hairpin-dst",
123 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
126 /* Hook up output features */
127 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
128 .arc_name = "ip4-output",
129 .node_name = "nat44-in2out-output",
130 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
132 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
133 .arc_name = "ip4-output",
134 .node_name = "nat44-in2out-output-worker-handoff",
135 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
137 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
138 .arc_name = "ip4-output",
139 .node_name = "nat44-hairpin-src",
140 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
142 VNET_FEATURE_INIT (nat_pre_in2out_output, static) = {
143 .arc_name = "ip4-output",
144 .node_name = "nat-pre-in2out-output",
145 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
146 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
148 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
149 .arc_name = "ip4-output",
150 .node_name = "nat44-ed-in2out-output",
151 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
152 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
154 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
155 .arc_name = "ip4-output",
156 .node_name = "nat44-ed-hairpin-src",
157 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
158 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
161 /* Hook up ip4-local features */
162 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
164 .arc_name = "ip4-local",
165 .node_name = "nat44-hairpinning",
166 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
168 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
170 .arc_name = "ip4-local",
171 .node_name = "nat44-ed-hairpinning",
172 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
176 VLIB_PLUGIN_REGISTER () = {
177 .version = VPP_BUILD_VER,
178 .description = "Network Address Translation (NAT)",
183 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
184 u32 rx_fib_index, u8 is_output);
187 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
191 snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
192 u32 rx_fib_index0, u8 is_output);
195 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
198 static u32 nat_calc_bihash_buckets (u32 n_elts);
201 format_session_kvp (u8 * s, va_list * args)
203 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
205 s = format (s, "%U session-index %llu", format_snat_key, v->key, v->value);
211 format_static_mapping_kvp (u8 * s, va_list * args)
213 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
215 s = format (s, "%U static-mapping-index %llu",
216 format_snat_key, v->key, v->value);
222 format_user_kvp (u8 * s, va_list * args)
224 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
229 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
230 k.fib_index, v->value);
236 format_ed_session_kvp (u8 * s, va_list * args)
238 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
242 ip4_address_t l_addr, r_addr;
245 split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
248 "local %U:%d remote %U:%d proto %U fib %d thread-index %u session-index %u",
249 format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
250 format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
251 format_ip_protocol, proto, fib_index,
252 ed_value_get_session_index (v), ed_value_get_thread_index (v));
258 nat44_ei_free_session_data (snat_main_t * sm, snat_session_t * s,
259 u32 thread_index, u8 is_ha)
261 clib_bihash_kv_8_8_t kv;
263 snat_main_per_thread_data_t *tsm =
264 vec_elt_at_index (sm->per_thread_data, thread_index);
266 init_nat_i2o_k (&kv, s);
267 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
268 nat_elog_warn ("in2out key del failed");
270 init_nat_o2i_k (&kv, s);
271 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
272 nat_elog_warn ("out2in key del failed");
276 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
277 &s->in2out.addr, s->in2out.port,
278 &s->out2in.addr, s->out2in.port, s->nat_proto);
280 nat_ipfix_logging_nat44_ses_delete (thread_index,
281 s->in2out.addr.as_u32,
282 s->out2in.addr.as_u32,
286 s->in2out.fib_index);
288 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
289 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
294 if (snat_is_session_static (s))
297 snat_free_outside_address_and_port (sm->addresses, thread_index,
298 &s->out2in.addr, s->out2in.port,
302 static_always_inline void
303 nat44_ei_user_del_sessions (snat_user_t * u, u32 thread_index)
308 snat_main_t *sm = &snat_main;
309 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
312 elt = pool_elt_at_index (tsm->list_pool,
313 u->sessions_per_user_list_head_index);
315 elt = pool_elt_at_index (tsm->list_pool, elt->next);
317 while (elt->value != ~0)
319 s = pool_elt_at_index (tsm->sessions, elt->value);
320 elt = pool_elt_at_index (tsm->list_pool, elt->next);
322 nat44_ei_free_session_data (sm, s, thread_index, 0);
323 nat44_delete_session (sm, s, thread_index);
328 nat44_ei_user_del (ip4_address_t * addr, u32 fib_index)
332 snat_main_t *sm = &snat_main;
333 snat_main_per_thread_data_t *tsm;
335 snat_user_key_t user_key;
336 clib_bihash_kv_8_8_t kv, value;
338 if (sm->endpoint_dependent)
341 user_key.addr.as_u32 = addr->as_u32;
342 user_key.fib_index = fib_index;
343 kv.key = user_key.as_u64;
345 if (sm->num_workers > 1)
348 vec_foreach (tsm, sm->per_thread_data)
350 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
352 nat44_ei_user_del_sessions (
353 pool_elt_at_index (tsm->users, value.value),
363 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
364 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
366 nat44_ei_user_del_sessions (pool_elt_at_index
367 (tsm->users, value.value),
376 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
379 clib_bihash_kv_8_8_t kv;
382 ip4_address_t *l_addr, *r_addr;
384 clib_bihash_kv_16_8_t ed_kv;
385 snat_main_per_thread_data_t *tsm =
386 vec_elt_at_index (sm->per_thread_data, thread_index);
388 if (is_ed_session (s))
390 per_vrf_sessions_unregister_session (s, thread_index);
393 if (is_fwd_bypass_session (s))
395 if (snat_is_unk_proto_session (s))
397 init_ed_k (&ed_kv, s->in2out.addr, 0, s->ext_host_addr, 0, 0,
402 l_port = s->in2out.port;
403 r_port = s->ext_host_port;
404 l_addr = &s->in2out.addr;
405 r_addr = &s->ext_host_addr;
406 proto = nat_proto_to_ip_proto (s->nat_proto);
407 fib_index = s->in2out.fib_index;
408 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index,
411 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
412 nat_elog_warn ("in2out_ed key del failed");
416 /* session lookup tables */
417 if (is_ed_session (s))
419 if (is_affinity_sessions (s))
420 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
421 s->nat_proto, s->out2in.port);
422 l_addr = &s->out2in.addr;
423 r_addr = &s->ext_host_addr;
424 fib_index = s->out2in.fib_index;
425 if (snat_is_unk_proto_session (s))
427 proto = s->in2out.port;
433 proto = nat_proto_to_ip_proto (s->nat_proto);
434 l_port = s->out2in.port;
435 r_port = s->ext_host_port;
437 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
438 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
439 nat_elog_warn ("out2in_ed key del failed");
440 l_addr = &s->in2out.addr;
441 fib_index = s->in2out.fib_index;
442 if (!snat_is_unk_proto_session (s))
443 l_port = s->in2out.port;
444 if (is_twice_nat_session (s))
446 r_addr = &s->ext_host_nat_addr;
447 r_port = s->ext_host_nat_port;
449 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
450 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
451 nat_elog_warn ("in2out_ed key del failed");
454 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
455 &s->in2out.addr, s->in2out.port,
456 &s->ext_host_nat_addr, s->ext_host_nat_port,
457 &s->out2in.addr, s->out2in.port,
458 &s->ext_host_addr, s->ext_host_port,
459 s->nat_proto, is_twice_nat_session (s));
463 init_nat_i2o_k (&kv, s);
464 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
465 nat_elog_warn ("in2out key del failed");
466 init_nat_o2i_k (&kv, s);
467 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
468 nat_elog_warn ("out2in key del failed");
471 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
472 &s->in2out.addr, s->in2out.port,
473 &s->out2in.addr, s->out2in.port,
477 if (snat_is_unk_proto_session (s))
483 nat_ipfix_logging_nat44_ses_delete (thread_index,
484 s->in2out.addr.as_u32,
485 s->out2in.addr.as_u32,
489 s->in2out.fib_index);
491 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
492 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
496 /* Twice NAT address and port for external host */
497 if (is_twice_nat_session (s))
499 snat_free_outside_address_and_port (sm->twice_nat_addresses,
501 &s->ext_host_nat_addr,
502 s->ext_host_nat_port, s->nat_proto);
505 if (snat_is_session_static (s))
508 snat_free_outside_address_and_port (sm->addresses, thread_index,
509 &s->out2in.addr, s->out2in.port,
514 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
518 snat_user_key_t user_key;
519 clib_bihash_kv_8_8_t kv, value;
520 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
521 dlist_elt_t *per_user_list_head_elt;
523 user_key.addr.as_u32 = addr->as_u32;
524 user_key.fib_index = fib_index;
525 kv.key = user_key.as_u64;
527 /* Ever heard of the "user" = src ip4 address before? */
528 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
530 if (pool_elts (tsm->users) >= sm->max_users_per_thread)
532 vlib_increment_simple_counter (&sm->user_limit_reached,
534 nat_elog_warn ("maximum user limit reached");
537 /* no, make a new one */
538 pool_get (tsm->users, u);
539 clib_memset (u, 0, sizeof (*u));
541 u->addr.as_u32 = addr->as_u32;
542 u->fib_index = fib_index;
544 pool_get (tsm->list_pool, per_user_list_head_elt);
546 u->sessions_per_user_list_head_index = per_user_list_head_elt -
549 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
551 kv.value = u - tsm->users;
554 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
556 nat_elog_warn ("user_hash key add failed");
557 nat44_delete_user_with_no_session (sm, u, thread_index);
561 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
562 pool_elts (tsm->users));
566 u = pool_elt_at_index (tsm->users, value.value);
574 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
575 u32 thread_index, f64 now)
578 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
579 u32 oldest_per_user_translation_list_index, session_index;
580 dlist_elt_t *oldest_per_user_translation_list_elt;
581 dlist_elt_t *per_user_translation_list_elt;
583 /* Over quota? Recycle the least recently used translation */
584 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
586 oldest_per_user_translation_list_index =
587 clib_dlist_remove_head (tsm->list_pool,
588 u->sessions_per_user_list_head_index);
590 ASSERT (oldest_per_user_translation_list_index != ~0);
592 /* Add it back to the end of the LRU list */
593 clib_dlist_addtail (tsm->list_pool,
594 u->sessions_per_user_list_head_index,
595 oldest_per_user_translation_list_index);
596 /* Get the list element */
597 oldest_per_user_translation_list_elt =
598 pool_elt_at_index (tsm->list_pool,
599 oldest_per_user_translation_list_index);
601 /* Get the session index from the list element */
602 session_index = oldest_per_user_translation_list_elt->value;
604 /* Get the session */
605 s = pool_elt_at_index (tsm->sessions, session_index);
607 // TODO: ONLY EI version should be called
608 nat_free_session_data (sm, s, thread_index, 0);
609 if (snat_is_session_static (s))
610 u->nstaticsessions--;
617 s->ext_host_addr.as_u32 = 0;
618 s->ext_host_port = 0;
619 s->ext_host_nat_addr.as_u32 = 0;
620 s->ext_host_nat_port = 0;
624 pool_get (tsm->sessions, s);
625 clib_memset (s, 0, sizeof (*s));
627 /* Create list elts */
628 pool_get (tsm->list_pool, per_user_translation_list_elt);
629 clib_dlist_init (tsm->list_pool,
630 per_user_translation_list_elt - tsm->list_pool);
632 per_user_translation_list_elt->value = s - tsm->sessions;
633 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
634 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
636 clib_dlist_addtail (tsm->list_pool,
637 s->per_user_list_head_index,
638 per_user_translation_list_elt - tsm->list_pool);
640 s->user_index = u - tsm->users;
641 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
642 pool_elts (tsm->sessions));
645 s->ha_last_refreshed = now;
651 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
654 snat_main_t *sm = &snat_main;
655 fib_prefix_t prefix = {
657 .fp_proto = FIB_PROTOCOL_IP4,
659 .ip4.as_u32 = addr->as_u32,
662 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
665 fib_table_entry_update_one_path (fib_index,
668 (FIB_ENTRY_FLAG_CONNECTED |
669 FIB_ENTRY_FLAG_LOCAL |
670 FIB_ENTRY_FLAG_EXCLUSIVE),
674 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
676 fib_table_entry_delete (fib_index, &prefix, sm->fib_src_low);
680 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
685 vlib_thread_main_t *tm = vlib_get_thread_main ();
687 if (twice_nat && !sm->endpoint_dependent)
689 nat_log_err ("unsupported");
690 return VNET_API_ERROR_UNSUPPORTED;
693 /* Check if address already exists */
695 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
697 if (ap->addr.as_u32 == addr->as_u32)
699 nat_log_err ("address exist");
700 return VNET_API_ERROR_VALUE_EXIST;
706 vec_add2 (sm->twice_nat_addresses, ap, 1);
708 vec_add2 (sm->addresses, ap, 1);
713 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
719 #define _(N, i, n, s) \
720 clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
721 ap->busy_##n##_ports = 0; \
722 ap->busy_##n##_ports_per_thread = 0;\
723 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
731 /* Add external address to FIB */
733 pool_foreach (i, sm->interfaces,
735 if (nat_interface_is_inside(i) || sm->out2in_dpo)
738 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
741 pool_foreach (i, sm->output_feature_interfaces,
743 if (nat_interface_is_inside(i) || sm->out2in_dpo)
746 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
755 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
757 snat_static_mapping_t *m;
759 pool_foreach (m, sm->static_mappings,
761 if (is_addr_only_static_mapping (m) ||
762 is_out2in_only_static_mapping (m) ||
763 is_identity_static_mapping (m))
765 if (m->external_addr.as_u32 == addr.as_u32)
774 snat_add_static_mapping_when_resolved (snat_main_t * sm,
775 ip4_address_t l_addr,
780 nat_protocol_t proto,
781 int addr_only, int is_add, u8 * tag,
782 int twice_nat, int out2in_only,
784 ip4_address_t pool_addr, int exact)
786 snat_static_map_resolve_t *rp;
788 vec_add2 (sm->to_resolve, rp, 1);
789 rp->l_addr.as_u32 = l_addr.as_u32;
791 rp->sw_if_index = sw_if_index;
795 rp->addr_only = addr_only;
797 rp->twice_nat = twice_nat;
798 rp->out2in_only = out2in_only;
799 rp->identity_nat = identity_nat;
800 rp->tag = vec_dup (tag);
801 rp->pool_addr = pool_addr;
806 get_thread_idx_by_port (u16 e_port)
808 snat_main_t *sm = &snat_main;
809 u32 thread_idx = sm->num_workers;
810 if (sm->num_workers > 1)
813 sm->first_worker_index +
814 sm->workers[(e_port - 1024) / sm->port_per_thread];
820 nat_ei_static_mapping_del_sessions (snat_main_t * sm,
821 snat_main_per_thread_data_t * tsm,
822 snat_user_key_t u_key, int addr_only,
823 ip4_address_t e_addr, u16 e_port)
825 clib_bihash_kv_8_8_t kv, value;
826 kv.key = u_key.as_u64;
828 dlist_elt_t *head, *elt;
831 u32 elt_index, head_index, ses_index;
833 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
835 user_index = value.value;
836 u = pool_elt_at_index (tsm->users, user_index);
837 if (u->nstaticsessions)
839 head_index = u->sessions_per_user_list_head_index;
840 head = pool_elt_at_index (tsm->list_pool, head_index);
841 elt_index = head->next;
842 elt = pool_elt_at_index (tsm->list_pool, elt_index);
843 ses_index = elt->value;
844 while (ses_index != ~0)
846 s = pool_elt_at_index (tsm->sessions, ses_index);
847 elt = pool_elt_at_index (tsm->list_pool, elt->next);
848 ses_index = elt->value;
852 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
853 (s->out2in.port != e_port))
857 if (is_lb_session (s))
860 if (!snat_is_session_static (s))
863 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
864 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
874 nat_ed_static_mapping_del_sessions (snat_main_t * sm,
875 snat_main_per_thread_data_t * tsm,
876 ip4_address_t l_addr,
879 u32 fib_index, int addr_only,
880 ip4_address_t e_addr, u16 e_port)
883 u32 *indexes_to_free = NULL;
885 pool_foreach (s, tsm->sessions, {
886 if (s->in2out.fib_index != fib_index ||
887 s->in2out.addr.as_u32 != l_addr.as_u32)
893 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
894 s->out2in.port != e_port ||
895 s->in2out.port != l_port ||
896 s->nat_proto != protocol)
900 if (is_lb_session (s))
902 if (!snat_is_session_static (s))
904 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
905 vec_add1 (indexes_to_free, s - tsm->sessions);
911 vec_foreach (ses_index, indexes_to_free)
913 s = pool_elt_at_index (tsm->sessions, *ses_index);
914 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
916 vec_free (indexes_to_free);
920 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
921 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
922 u32 sw_if_index, nat_protocol_t proto, int is_add,
923 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
924 u8 identity_nat, ip4_address_t pool_addr, int exact)
926 snat_main_t *sm = &snat_main;
927 snat_static_mapping_t *m;
928 clib_bihash_kv_8_8_t kv, value;
929 snat_address_t *a = 0;
931 snat_interface_t *interface;
933 snat_main_per_thread_data_t *tsm;
934 snat_user_key_t u_key;
936 dlist_elt_t *head, *elt;
937 u32 elt_index, head_index;
941 snat_static_map_resolve_t *rp, *rp_match = 0;
942 nat44_lb_addr_port_t *local;
945 if (!sm->endpoint_dependent)
947 if (twice_nat || out2in_only)
948 return VNET_API_ERROR_UNSUPPORTED;
951 /* If the external address is a specific interface address */
952 if (sw_if_index != ~0)
954 ip4_address_t *first_int_addr;
956 for (i = 0; i < vec_len (sm->to_resolve); i++)
958 rp = sm->to_resolve + i;
959 if (rp->sw_if_index != sw_if_index ||
960 rp->l_addr.as_u32 != l_addr.as_u32 ||
961 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
966 if ((rp->l_port != l_port && rp->e_port != e_port)
967 || rp->proto != proto)
975 /* Might be already set... */
976 first_int_addr = ip4_interface_first_address
977 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
982 return VNET_API_ERROR_VALUE_EXIST;
984 snat_add_static_mapping_when_resolved
985 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
986 addr_only, is_add, tag, twice_nat, out2in_only,
987 identity_nat, pool_addr, exact);
989 /* DHCP resolution required? */
990 if (first_int_addr == 0)
996 e_addr.as_u32 = first_int_addr->as_u32;
997 /* Identity mapping? */
998 if (l_addr.as_u32 == 0)
999 l_addr.as_u32 = e_addr.as_u32;
1005 return VNET_API_ERROR_NO_SUCH_ENTRY;
1007 vec_del1 (sm->to_resolve, i);
1011 e_addr.as_u32 = first_int_addr->as_u32;
1012 /* Identity mapping? */
1013 if (l_addr.as_u32 == 0)
1014 l_addr.as_u32 = e_addr.as_u32;
1021 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
1022 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1025 m = pool_elt_at_index (sm->static_mappings, value.value);
1031 if (is_identity_static_mapping (m))
1034 pool_foreach (local, m->locals,
1036 if (local->vrf_id == vrf_id)
1037 return VNET_API_ERROR_VALUE_EXIST;
1040 pool_get (m->locals, local);
1041 local->vrf_id = vrf_id;
1043 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1045 init_nat_kv (&kv, m->local_addr, m->local_port,
1046 local->fib_index, m->proto,
1047 m - sm->static_mappings);
1048 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1052 return VNET_API_ERROR_VALUE_EXIST;
1055 if (twice_nat && addr_only)
1056 return VNET_API_ERROR_UNSUPPORTED;
1058 /* Convert VRF id to FIB index */
1061 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1063 /* If not specified use inside VRF id from SNAT plugin startup config */
1066 fib_index = sm->inside_fib_index;
1067 vrf_id = sm->inside_vrf_id;
1068 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1071 if (!(out2in_only || identity_nat))
1073 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
1074 addr_only ? 0 : proto);
1075 if (!clib_bihash_search_8_8
1076 (&sm->static_mapping_by_local, &kv, &value))
1077 return VNET_API_ERROR_VALUE_EXIST;
1080 /* Find external address in allocated addresses and reserve port for
1081 address and port pair mapping when dynamic translations enabled */
1082 if (!(addr_only || sm->static_mapping_only || out2in_only))
1084 for (i = 0; i < vec_len (sm->addresses); i++)
1086 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1088 a = sm->addresses + i;
1089 /* External port must be unused */
1092 #define _(N, j, n, s) \
1093 case NAT_PROTOCOL_##N: \
1094 if (a->busy_##n##_port_refcounts[e_port]) \
1095 return VNET_API_ERROR_INVALID_VALUE; \
1096 ++a->busy_##n##_port_refcounts[e_port]; \
1097 if (e_port > 1024) \
1099 a->busy_##n##_ports++; \
1100 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1103 foreach_nat_protocol
1106 nat_elog_info ("unknown protocol");
1107 return VNET_API_ERROR_INVALID_VALUE_2;
1112 /* External address must be allocated */
1113 if (!a && (l_addr.as_u32 != e_addr.as_u32))
1115 if (sw_if_index != ~0)
1117 for (i = 0; i < vec_len (sm->to_resolve); i++)
1119 rp = sm->to_resolve + i;
1122 if (rp->sw_if_index != sw_if_index &&
1123 rp->l_addr.as_u32 != l_addr.as_u32 &&
1124 rp->vrf_id != vrf_id && rp->l_port != l_port &&
1125 rp->e_port != e_port && rp->proto != proto)
1128 vec_del1 (sm->to_resolve, i);
1132 return VNET_API_ERROR_NO_SUCH_ENTRY;
1136 pool_get (sm->static_mappings, m);
1137 clib_memset (m, 0, sizeof (*m));
1138 m->tag = vec_dup (tag);
1139 m->local_addr = l_addr;
1140 m->external_addr = e_addr;
1141 m->twice_nat = twice_nat;
1143 if (twice_nat == TWICE_NAT && exact)
1145 m->flags |= NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS;
1146 m->pool_addr = pool_addr;
1150 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1152 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
1155 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
1156 pool_get (m->locals, local);
1157 local->vrf_id = vrf_id;
1158 local->fib_index = fib_index;
1163 m->fib_index = fib_index;
1167 m->local_port = l_port;
1168 m->external_port = e_port;
1172 if (sm->num_workers > 1)
1175 .src_address = m->local_addr,
1177 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
1178 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1181 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1183 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto,
1184 m - sm->static_mappings);
1186 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1188 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1189 m - sm->static_mappings);
1190 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
1192 /* Delete dynamic sessions matching local address (+ local port) */
1193 // TODO: based on type of NAT EI/ED
1194 if (!(sm->static_mapping_only))
1196 u_key.addr = m->local_addr;
1197 u_key.fib_index = m->fib_index;
1198 kv.key = u_key.as_u64;
1199 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1201 user_index = value.value;
1202 u = pool_elt_at_index (tsm->users, user_index);
1205 head_index = u->sessions_per_user_list_head_index;
1206 head = pool_elt_at_index (tsm->list_pool, head_index);
1207 elt_index = head->next;
1208 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1209 ses_index = elt->value;
1210 while (ses_index != ~0)
1212 s = pool_elt_at_index (tsm->sessions, ses_index);
1213 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1214 ses_index = elt->value;
1216 if (snat_is_session_static (s))
1219 if (!addr_only && s->in2out.port != m->local_port)
1222 nat_free_session_data (sm, s,
1223 tsm - sm->per_thread_data, 0);
1224 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1226 if (!addr_only && !sm->endpoint_dependent)
1237 if (sw_if_index != ~0)
1240 return VNET_API_ERROR_NO_SUCH_ENTRY;
1246 vrf_id = sm->inside_vrf_id;
1249 pool_foreach (local, m->locals,
1251 if (local->vrf_id == vrf_id)
1252 find = local - m->locals;
1256 return VNET_API_ERROR_NO_SUCH_ENTRY;
1258 local = pool_elt_at_index (m->locals, find);
1259 fib_index = local->fib_index;
1260 pool_put (m->locals, local);
1263 fib_index = m->fib_index;
1265 /* Free external address port */
1266 if (!(addr_only || sm->static_mapping_only || out2in_only))
1268 for (i = 0; i < vec_len (sm->addresses); i++)
1270 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1272 a = sm->addresses + i;
1275 #define _(N, j, n, s) \
1276 case NAT_PROTOCOL_##N: \
1277 --a->busy_##n##_port_refcounts[e_port]; \
1278 if (e_port > 1024) \
1280 a->busy_##n##_ports--; \
1281 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1284 foreach_nat_protocol
1287 nat_elog_info ("unknown protocol");
1288 return VNET_API_ERROR_INVALID_VALUE_2;
1295 if (sm->num_workers > 1)
1296 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1298 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1300 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
1302 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1304 /* Delete session(s) for static mapping if exist */
1305 if (!(sm->static_mapping_only) ||
1306 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1308 if (sm->endpoint_dependent)
1310 nat_ed_static_mapping_del_sessions (sm, tsm, m->local_addr,
1311 m->local_port, m->proto,
1312 fib_index, addr_only,
1317 u_key.addr = m->local_addr;
1318 u_key.fib_index = fib_index;
1319 kv.key = u_key.as_u64;
1320 nat_ei_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
1325 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1326 if (pool_elts (m->locals))
1329 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1330 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1333 vec_free (m->workers);
1334 /* Delete static mapping from pool */
1335 pool_put (sm->static_mappings, m);
1338 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1341 /* Add/delete external address to FIB */
1343 pool_foreach (interface, sm->interfaces,
1345 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1348 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1351 pool_foreach (interface, sm->output_feature_interfaces,
1353 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1356 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1365 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1366 nat_protocol_t proto,
1367 nat44_lb_addr_port_t * locals, u8 is_add,
1368 twice_nat_type_t twice_nat, u8 out2in_only,
1369 u8 * tag, u32 affinity)
1371 snat_main_t *sm = &snat_main;
1372 snat_static_mapping_t *m;
1373 clib_bihash_kv_8_8_t kv, value;
1374 snat_address_t *a = 0;
1376 nat44_lb_addr_port_t *local;
1377 snat_main_per_thread_data_t *tsm;
1381 if (!sm->endpoint_dependent)
1382 return VNET_API_ERROR_UNSUPPORTED;
1384 init_nat_k (&kv, e_addr, e_port, 0, proto);
1385 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1388 m = pool_elt_at_index (sm->static_mappings, value.value);
1393 return VNET_API_ERROR_VALUE_EXIST;
1395 if (vec_len (locals) < 2)
1396 return VNET_API_ERROR_INVALID_VALUE;
1398 /* Find external address in allocated addresses and reserve port for
1399 address and port pair mapping when dynamic translations enabled */
1400 if (!(sm->static_mapping_only || out2in_only))
1402 for (i = 0; i < vec_len (sm->addresses); i++)
1404 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1406 a = sm->addresses + i;
1407 /* External port must be unused */
1410 #define _(N, j, n, s) \
1411 case NAT_PROTOCOL_##N: \
1412 if (a->busy_##n##_port_refcounts[e_port]) \
1413 return VNET_API_ERROR_INVALID_VALUE; \
1414 ++a->busy_##n##_port_refcounts[e_port]; \
1415 if (e_port > 1024) \
1417 a->busy_##n##_ports++; \
1418 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1421 foreach_nat_protocol
1424 nat_elog_info ("unknown protocol");
1425 return VNET_API_ERROR_INVALID_VALUE_2;
1430 /* External address must be allocated */
1432 return VNET_API_ERROR_NO_SUCH_ENTRY;
1435 pool_get (sm->static_mappings, m);
1436 clib_memset (m, 0, sizeof (*m));
1437 m->tag = vec_dup (tag);
1438 m->external_addr = e_addr;
1439 m->external_port = e_port;
1441 m->twice_nat = twice_nat;
1442 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1444 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1445 m->affinity = affinity;
1448 m->affinity_per_service_list_head_index =
1449 nat_affinity_get_per_service_list_head_index ();
1451 m->affinity_per_service_list_head_index = ~0;
1453 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1454 m - sm->static_mappings);
1455 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1457 nat_elog_err ("static_mapping_by_external key add failed");
1458 return VNET_API_ERROR_UNSPECIFIED;
1461 for (i = 0; i < vec_len (locals); i++)
1463 locals[i].fib_index =
1464 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1469 init_nat_kv (&kv, locals[i].addr, locals[i].port,
1470 locals[i].fib_index, m->proto,
1471 m - sm->static_mappings);
1472 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1474 locals[i].prefix = (i == 0) ? locals[i].probability :
1475 (locals[i - 1].prefix + locals[i].probability);
1476 pool_get (m->locals, local);
1478 if (sm->num_workers > 1)
1481 .src_address = locals[i].addr,
1484 clib_bitmap_set (bitmap,
1485 sm->worker_in2out_cb (&ip, m->fib_index, 0),
1490 /* Assign workers */
1491 if (sm->num_workers > 1)
1494 clib_bitmap_foreach (i, bitmap,
1496 vec_add1(m->workers, i);
1504 return VNET_API_ERROR_NO_SUCH_ENTRY;
1506 if (!is_lb_static_mapping (m))
1507 return VNET_API_ERROR_INVALID_VALUE;
1509 /* Free external address port */
1510 if (!(sm->static_mapping_only || out2in_only))
1512 for (i = 0; i < vec_len (sm->addresses); i++)
1514 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1516 a = sm->addresses + i;
1519 #define _(N, j, n, s) \
1520 case NAT_PROTOCOL_##N: \
1521 --a->busy_##n##_port_refcounts[e_port]; \
1522 if (e_port > 1024) \
1524 a->busy_##n##_ports--; \
1525 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1528 foreach_nat_protocol
1531 nat_elog_info ("unknown protocol");
1532 return VNET_API_ERROR_INVALID_VALUE_2;
1539 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1540 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1542 nat_elog_err ("static_mapping_by_external key del failed");
1543 return VNET_API_ERROR_UNSPECIFIED;
1547 pool_foreach (local, m->locals,
1549 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1553 init_nat_k(& kv, local->addr, local->port, local->fib_index, m->proto);
1554 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1556 nat_elog_err ("static_mapping_by_local key del failed");
1557 return VNET_API_ERROR_UNSPECIFIED;
1561 if (sm->num_workers > 1)
1564 .src_address = local->addr,
1566 tsm = vec_elt_at_index (sm->per_thread_data,
1567 sm->worker_in2out_cb (&ip, m->fib_index, 0));
1570 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1572 /* Delete sessions */
1573 pool_foreach (s, tsm->sessions, {
1574 if (!(is_lb_session (s)))
1577 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1578 s->in2out.port != local->port)
1581 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1582 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1587 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1588 pool_free (m->locals);
1590 vec_free (m->workers);
1592 pool_put (sm->static_mappings, m);
1599 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1600 ip4_address_t l_addr, u16 l_port,
1601 nat_protocol_t proto, u32 vrf_id,
1602 u8 probability, u8 is_add)
1604 snat_main_t *sm = &snat_main;
1605 snat_static_mapping_t *m = 0;
1606 clib_bihash_kv_8_8_t kv, value;
1607 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1608 snat_main_per_thread_data_t *tsm;
1614 if (!sm->endpoint_dependent)
1615 return VNET_API_ERROR_FEATURE_DISABLED;
1617 init_nat_k (&kv, e_addr, e_port, 0, proto);
1618 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1619 m = pool_elt_at_index (sm->static_mappings, value.value);
1622 return VNET_API_ERROR_NO_SUCH_ENTRY;
1624 if (!is_lb_static_mapping (m))
1625 return VNET_API_ERROR_INVALID_VALUE;
1628 pool_foreach (local, m->locals,
1630 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1631 (local->vrf_id == vrf_id))
1633 match_local = local;
1642 return VNET_API_ERROR_VALUE_EXIST;
1644 pool_get (m->locals, local);
1645 clib_memset (local, 0, sizeof (*local));
1646 local->addr.as_u32 = l_addr.as_u32;
1647 local->port = l_port;
1648 local->probability = probability;
1649 local->vrf_id = vrf_id;
1651 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1654 if (!is_out2in_only_static_mapping (m))
1656 init_nat_kv (&kv, l_addr, l_port, local->fib_index, proto,
1657 m - sm->static_mappings);
1658 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1659 nat_elog_err ("static_mapping_by_local key add failed");
1665 return VNET_API_ERROR_NO_SUCH_ENTRY;
1667 if (pool_elts (m->locals) < 3)
1668 return VNET_API_ERROR_UNSPECIFIED;
1670 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1673 if (!is_out2in_only_static_mapping (m))
1675 init_nat_k (&kv, l_addr, l_port, match_local->fib_index, proto);
1676 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1677 nat_elog_err ("static_mapping_by_local key del failed");
1680 if (sm->num_workers > 1)
1683 .src_address = local->addr,
1685 tsm = vec_elt_at_index (sm->per_thread_data,
1686 sm->worker_in2out_cb (&ip, m->fib_index,
1690 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1692 /* Delete sessions */
1694 pool_foreach (s, tsm->sessions, {
1695 if (!(is_lb_session (s)))
1698 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1699 s->in2out.port != match_local->port)
1702 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1703 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1707 pool_put (m->locals, match_local);
1710 vec_free (m->workers);
1713 pool_foreach (local, m->locals,
1715 vec_add1 (locals, local - m->locals);
1716 if (sm->num_workers > 1)
1719 ip.src_address.as_u32 = local->addr.as_u32,
1720 bitmap = clib_bitmap_set (bitmap,
1721 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1727 ASSERT (vec_len (locals) > 1);
1729 local = pool_elt_at_index (m->locals, locals[0]);
1730 local->prefix = local->probability;
1731 for (i = 1; i < vec_len (locals); i++)
1733 local = pool_elt_at_index (m->locals, locals[i]);
1734 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1735 local->prefix = local->probability + prev_local->prefix;
1738 /* Assign workers */
1739 if (sm->num_workers > 1)
1742 clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); }));
1750 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1753 snat_address_t *a = 0;
1754 snat_session_t *ses;
1755 u32 *ses_to_be_removed = 0, *ses_index;
1756 snat_main_per_thread_data_t *tsm;
1757 snat_static_mapping_t *m;
1758 snat_interface_t *interface;
1760 snat_address_t *addresses =
1761 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1763 /* Find SNAT address */
1764 for (i = 0; i < vec_len (addresses); i++)
1766 if (addresses[i].addr.as_u32 == addr.as_u32)
1774 nat_log_err ("no such address");
1775 return VNET_API_ERROR_NO_SUCH_ENTRY;
1780 ip4_address_t pool_addr = { 0 };
1782 pool_foreach (m, sm->static_mappings,
1784 if (m->external_addr.as_u32 == addr.as_u32)
1785 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1786 m->local_port, m->external_port,
1788 is_addr_only_static_mapping(m), ~0,
1789 m->proto, 0 /* is_add */,
1791 is_out2in_only_static_mapping(m),
1793 is_identity_static_mapping(m),
1800 /* Check if address is used in some static mapping */
1801 if (is_snat_address_used_in_static_mapping (sm, addr))
1803 nat_log_err ("address used in static mapping");
1804 return VNET_API_ERROR_UNSPECIFIED;
1808 if (a->fib_index != ~0)
1809 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1811 /* Delete sessions using address */
1812 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1814 vec_foreach (tsm, sm->per_thread_data)
1817 pool_foreach (ses, tsm->sessions, ({
1818 if (ses->out2in.addr.as_u32 == addr.as_u32)
1820 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1821 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1826 if (sm->endpoint_dependent)
1828 vec_foreach (ses_index, ses_to_be_removed)
1830 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1831 nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
1836 vec_foreach (ses_index, ses_to_be_removed)
1838 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1839 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1843 vec_free (ses_to_be_removed);
1847 #define _(N, i, n, s) \
1848 vec_free (a->busy_##n##_ports_per_thread);
1849 foreach_nat_protocol
1853 vec_del1 (sm->twice_nat_addresses, i);
1857 vec_del1 (sm->addresses, i);
1859 /* Delete external address from FIB */
1861 pool_foreach (interface, sm->interfaces,
1863 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1866 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1869 pool_foreach (interface, sm->output_feature_interfaces,
1871 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1874 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1883 nat_validate_counters (snat_main_t * sm, u32 sw_if_index)
1886 vlib_validate_simple_counter (&sm->counters.fastpath.in2out.x, \
1888 vlib_zero_simple_counter (&sm->counters.fastpath.in2out.x, sw_if_index); \
1889 vlib_validate_simple_counter (&sm->counters.fastpath.out2in.x, \
1891 vlib_zero_simple_counter (&sm->counters.fastpath.out2in.x, sw_if_index); \
1892 vlib_validate_simple_counter (&sm->counters.slowpath.in2out.x, \
1894 vlib_zero_simple_counter (&sm->counters.slowpath.in2out.x, sw_if_index); \
1895 vlib_validate_simple_counter (&sm->counters.slowpath.out2in.x, \
1897 vlib_zero_simple_counter (&sm->counters.slowpath.out2in.x, sw_if_index); \
1898 vlib_validate_simple_counter (&sm->counters.fastpath.in2out_ed.x, \
1900 vlib_zero_simple_counter (&sm->counters.fastpath.in2out_ed.x, sw_if_index); \
1901 vlib_validate_simple_counter (&sm->counters.fastpath.out2in_ed.x, \
1903 vlib_zero_simple_counter (&sm->counters.fastpath.out2in_ed.x, sw_if_index); \
1904 vlib_validate_simple_counter (&sm->counters.slowpath.in2out_ed.x, \
1906 vlib_zero_simple_counter (&sm->counters.slowpath.in2out_ed.x, sw_if_index); \
1907 vlib_validate_simple_counter (&sm->counters.slowpath.out2in_ed.x, \
1909 vlib_zero_simple_counter (&sm->counters.slowpath.out2in_ed.x, sw_if_index);
1910 foreach_nat_counter;
1912 vlib_validate_simple_counter (&sm->counters.hairpinning, sw_if_index);
1913 vlib_zero_simple_counter (&sm->counters.hairpinning, sw_if_index);
1917 expire_per_vrf_sessions (u32 fib_index)
1919 per_vrf_sessions_t *per_vrf_sessions;
1920 snat_main_per_thread_data_t *tsm;
1921 snat_main_t *sm = &snat_main;
1924 vec_foreach (tsm, sm->per_thread_data)
1926 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1928 if ((per_vrf_sessions->rx_fib_index == fib_index) ||
1929 (per_vrf_sessions->tx_fib_index == fib_index))
1931 per_vrf_sessions->expired = 1;
1939 update_per_vrf_sessions_vec (u32 fib_index, int is_del)
1941 snat_main_t *sm = &snat_main;
1944 // we don't care if it is outside/inside fib
1945 // we just care about their ref_count
1946 // if it reaches 0 sessions should expire
1947 // because the fib isn't valid for NAT anymore
1949 vec_foreach (fib, sm->fibs)
1951 if (fib->fib_index == fib_index)
1956 if (!fib->ref_count)
1958 vec_del1 (sm->fibs, fib - sm->fibs);
1959 expire_per_vrf_sessions (fib_index);
1969 vec_add2 (sm->fibs, fib, 1);
1971 fib->fib_index = fib_index;
1976 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1978 snat_main_t *sm = &snat_main;
1979 snat_interface_t *i;
1980 const char *feature_name, *del_feature_name;
1982 snat_static_mapping_t *m;
1983 nat_outside_fib_t *outside_fib;
1984 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1989 nat_log_err ("nat44 is disabled");
1990 return VNET_API_ERROR_UNSUPPORTED;
1993 if (sm->out2in_dpo && !is_inside)
1995 nat_log_err ("error unsupported");
1996 return VNET_API_ERROR_UNSUPPORTED;
2000 pool_foreach (i, sm->output_feature_interfaces,
2002 if (i->sw_if_index == sw_if_index)
2004 nat_log_err ("error interface already configured");
2005 return VNET_API_ERROR_VALUE_EXIST;
2010 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2011 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
2014 if (sm->num_workers > 1)
2016 is_inside ? "nat44-in2out-worker-handoff" :
2017 "nat44-out2in-worker-handoff";
2018 else if (sm->endpoint_dependent)
2020 feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
2023 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
2026 if (sm->fq_in2out_index == ~0 && sm->num_workers > 1)
2027 sm->fq_in2out_index =
2028 vlib_frame_queue_main_init (sm->in2out_node_index, NAT_FQ_NELTS);
2030 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2031 sm->fq_out2in_index =
2032 vlib_frame_queue_main_init (sm->out2in_node_index, NAT_FQ_NELTS);
2034 if (sm->endpoint_dependent)
2035 update_per_vrf_sessions_vec (fib_index, is_del);
2040 vec_foreach (outside_fib, sm->outside_fibs)
2042 if (outside_fib->fib_index == fib_index)
2046 outside_fib->refcount--;
2047 if (!outside_fib->refcount)
2048 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2051 outside_fib->refcount++;
2058 vec_add2 (sm->outside_fibs, outside_fib, 1);
2059 outside_fib->refcount = 1;
2060 outside_fib->fib_index = fib_index;
2066 pool_foreach (i, sm->interfaces,
2068 if (i->sw_if_index == sw_if_index)
2072 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
2075 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
2077 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
2079 if (sm->num_workers > 1)
2081 del_feature_name = "nat44-handoff-classify";
2082 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
2083 "nat44-out2in-worker-handoff";
2085 else if (sm->endpoint_dependent)
2087 del_feature_name = "nat44-ed-classify";
2088 feature_name = !is_inside ? "nat-pre-in2out" :
2093 del_feature_name = "nat44-classify";
2094 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
2097 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
2100 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
2101 sw_if_index, 0, 0, 0);
2102 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2103 sw_if_index, 1, 0, 0);
2106 if (sm->endpoint_dependent)
2107 vnet_feature_enable_disable ("ip4-local",
2108 "nat44-ed-hairpinning",
2109 sw_if_index, 1, 0, 0);
2111 vnet_feature_enable_disable ("ip4-local",
2112 "nat44-hairpinning",
2113 sw_if_index, 1, 0, 0);
2118 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
2121 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2122 sw_if_index, 0, 0, 0);
2123 pool_put (sm->interfaces, i);
2126 if (sm->endpoint_dependent)
2127 vnet_feature_enable_disable ("ip4-local",
2128 "nat44-ed-hairpinning",
2129 sw_if_index, 0, 0, 0);
2131 vnet_feature_enable_disable ("ip4-local",
2132 "nat44-hairpinning",
2133 sw_if_index, 0, 0, 0);
2139 if ((nat_interface_is_inside(i) && is_inside) ||
2140 (nat_interface_is_outside(i) && !is_inside))
2143 if (sm->num_workers > 1)
2145 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
2146 "nat44-out2in-worker-handoff";
2147 feature_name = "nat44-handoff-classify";
2149 else if (sm->endpoint_dependent)
2151 del_feature_name = !is_inside ? "nat-pre-in2out" :
2154 feature_name = "nat44-ed-classify";
2158 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
2159 feature_name = "nat44-classify";
2162 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2165 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
2166 sw_if_index, 0, 0, 0);
2167 vnet_feature_enable_disable ("ip4-unicast", feature_name,
2168 sw_if_index, 1, 0, 0);
2171 if (sm->endpoint_dependent)
2172 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2173 sw_if_index, 0, 0, 0);
2175 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2176 sw_if_index, 0, 0, 0);
2188 nat_log_err ("error interface couldn't be found");
2189 return VNET_API_ERROR_NO_SUCH_ENTRY;
2192 pool_get (sm->interfaces, i);
2193 i->sw_if_index = sw_if_index;
2195 nat_validate_counters (sm, sw_if_index);
2197 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
2200 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2204 if (is_inside && !sm->out2in_dpo)
2206 if (sm->endpoint_dependent)
2207 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2208 sw_if_index, 1, 0, 0);
2210 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2211 sw_if_index, 1, 0, 0);
2217 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2221 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2223 /* Add/delete external addresses to FIB */
2226 vec_foreach (ap, sm->addresses)
2227 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2229 pool_foreach (m, sm->static_mappings,
2231 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2234 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2242 snat_interface_add_del_output_feature (u32 sw_if_index,
2243 u8 is_inside, int is_del)
2245 snat_main_t *sm = &snat_main;
2246 snat_interface_t *i;
2248 snat_static_mapping_t *m;
2249 nat_outside_fib_t *outside_fib;
2250 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2255 nat_log_err ("nat44 is disabled");
2256 return VNET_API_ERROR_UNSUPPORTED;
2259 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2261 nat_log_err ("error unsupported");
2262 return VNET_API_ERROR_UNSUPPORTED;
2266 pool_foreach (i, sm->interfaces,
2268 if (i->sw_if_index == sw_if_index)
2270 nat_log_err ("error interface already configured");
2271 return VNET_API_ERROR_VALUE_EXIST;
2276 if (sm->endpoint_dependent)
2277 update_per_vrf_sessions_vec (fib_index, is_del);
2282 vec_foreach (outside_fib, sm->outside_fibs)
2284 if (outside_fib->fib_index == fib_index)
2288 outside_fib->refcount--;
2289 if (!outside_fib->refcount)
2290 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2293 outside_fib->refcount++;
2300 vec_add2 (sm->outside_fibs, outside_fib, 1);
2301 outside_fib->refcount = 1;
2302 outside_fib->fib_index = fib_index;
2309 if (sm->endpoint_dependent)
2312 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2316 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2320 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2321 sw_if_index, !is_del, 0, 0);
2322 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2323 sw_if_index, !is_del, 0, 0);
2328 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2332 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2336 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2337 sw_if_index, !is_del, 0, 0);
2338 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2339 sw_if_index, !is_del, 0, 0);
2344 if (sm->num_workers > 1)
2346 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2350 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
2353 vnet_feature_enable_disable ("ip4-unicast",
2354 "nat44-out2in-worker-handoff",
2355 sw_if_index, !is_del, 0, 0);
2356 vnet_feature_enable_disable ("ip4-output",
2357 "nat44-in2out-output-worker-handoff",
2358 sw_if_index, !is_del, 0, 0);
2362 if (sm->endpoint_dependent)
2365 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2369 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2373 vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
2374 sw_if_index, !is_del, 0, 0);
2375 vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
2376 sw_if_index, !is_del, 0, 0);
2381 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2385 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2389 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2390 sw_if_index, !is_del, 0, 0);
2391 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2392 sw_if_index, !is_del, 0, 0);
2397 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2398 sm->fq_in2out_output_index =
2399 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2401 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2402 sm->fq_out2in_index =
2403 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2406 pool_foreach (i, sm->output_feature_interfaces,
2408 if (i->sw_if_index == sw_if_index)
2411 pool_put (sm->output_feature_interfaces, i);
2413 return VNET_API_ERROR_VALUE_EXIST;
2422 nat_log_err ("error interface couldn't be found");
2423 return VNET_API_ERROR_NO_SUCH_ENTRY;
2426 pool_get (sm->output_feature_interfaces, i);
2427 i->sw_if_index = sw_if_index;
2429 nat_validate_counters (sm, sw_if_index);
2431 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2433 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2435 /* Add/delete external addresses to FIB */
2441 vec_foreach (ap, sm->addresses)
2442 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2444 pool_foreach (m, sm->static_mappings,
2446 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2449 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2457 snat_set_workers (uword * bitmap)
2459 snat_main_t *sm = &snat_main;
2462 if (sm->num_workers < 2)
2463 return VNET_API_ERROR_FEATURE_DISABLED;
2465 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2466 return VNET_API_ERROR_INVALID_WORKER;
2468 vec_free (sm->workers);
2470 clib_bitmap_foreach (i, bitmap,
2472 vec_add1(sm->workers, i);
2473 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2474 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2479 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2485 snat_update_outside_fib (ip4_main_t * im, uword opaque,
2486 u32 sw_if_index, u32 new_fib_index,
2489 snat_main_t *sm = &snat_main;
2490 nat_outside_fib_t *outside_fib;
2491 snat_interface_t *i;
2495 if (!sm->enabled || (new_fib_index == old_fib_index)
2496 || (!vec_len (sm->outside_fibs)))
2502 pool_foreach (i, sm->interfaces,
2504 if (i->sw_if_index == sw_if_index)
2506 if (!(nat_interface_is_outside (i)))
2512 pool_foreach (i, sm->output_feature_interfaces,
2514 if (i->sw_if_index == sw_if_index)
2516 if (!(nat_interface_is_outside (i)))
2526 vec_foreach (outside_fib, sm->outside_fibs)
2528 if (outside_fib->fib_index == old_fib_index)
2530 outside_fib->refcount--;
2531 if (!outside_fib->refcount)
2532 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2537 vec_foreach (outside_fib, sm->outside_fibs)
2539 if (outside_fib->fib_index == new_fib_index)
2541 outside_fib->refcount++;
2549 vec_add2 (sm->outside_fibs, outside_fib, 1);
2550 outside_fib->refcount = 1;
2551 outside_fib->fib_index = new_fib_index;
2556 snat_update_outside_fib (ip4_main_t * im, uword opaque,
2557 u32 sw_if_index, u32 new_fib_index,
2561 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2564 ip4_address_t * address,
2566 u32 if_address_index, u32 is_delete);
2569 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2572 ip4_address_t * address,
2574 u32 if_address_index, u32 is_delete);
2577 nat_alloc_addr_and_port_default (snat_address_t * addresses, u32 fib_index,
2578 u32 thread_index, nat_protocol_t proto,
2579 ip4_address_t * addr, u16 * port,
2580 u16 port_per_thread, u32 snat_thread_index);
2583 test_key_calc_split ()
2585 ip4_address_t l_addr;
2586 l_addr.as_u8[0] = 1;
2587 l_addr.as_u8[1] = 1;
2588 l_addr.as_u8[2] = 1;
2589 l_addr.as_u8[3] = 1;
2590 ip4_address_t r_addr;
2591 r_addr.as_u8[0] = 2;
2592 r_addr.as_u8[1] = 2;
2593 r_addr.as_u8[2] = 2;
2594 r_addr.as_u8[3] = 2;
2598 u32 fib_index = 9000001;
2599 u32 thread_index = 3000000001;
2600 u32 session_index = 3000000221;
2601 clib_bihash_kv_16_8_t kv;
2602 init_ed_kv (&kv, l_addr, l_port, r_addr, r_port, fib_index, proto,
2603 thread_index, session_index);
2604 ip4_address_t l_addr2;
2605 ip4_address_t r_addr2;
2606 clib_memset (&l_addr2, 0, sizeof (l_addr2));
2607 clib_memset (&r_addr2, 0, sizeof (r_addr2));
2612 split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
2614 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2615 ASSERT (r_addr.as_u32 == r_addr2.as_u32);
2616 ASSERT (l_port == l_port2);
2617 ASSERT (r_port == r_port2);
2618 ASSERT (proto == proto2);
2619 ASSERT (fib_index == fib_index2);
2620 ASSERT (thread_index == ed_value_get_thread_index (&kv));
2621 ASSERT (session_index == ed_value_get_session_index (&kv));
2625 nat_protocol_t proto3 = ~0;
2626 u64 key = calc_nat_key (l_addr, l_port, fib_index, proto);
2627 split_nat_key (key, &l_addr2, &l_port2, &fib_index2, &proto3);
2628 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2629 ASSERT (l_port == l_port2);
2630 ASSERT (proto == proto3);
2631 ASSERT (fib_index == fib_index2);
2634 static clib_error_t *
2635 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
2637 snat_main_t *sm = &snat_main;
2640 if (sm->endpoint_dependent)
2642 // TODO: consider removing all NAT interfaces
2645 fib_index = ip4_fib_index_from_table_id (table_id);
2646 if (fib_index != ~0)
2647 expire_per_vrf_sessions (fib_index);
2653 VNET_IP_TABLE_ADD_DEL_FUNCTION (nat_ip_table_add_del);
2656 nat44_set_node_indexes (snat_main_t * sm, vlib_main_t * vm)
2660 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2661 sm->ei_out2in_node_index = node->index;
2662 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2663 sm->ei_in2out_node_index = node->index;
2664 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2665 sm->ei_in2out_output_node_index = node->index;
2667 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2668 sm->ed_out2in_node_index = node->index;
2669 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2670 sm->ed_in2out_node_index = node->index;
2671 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-output");
2672 sm->ed_in2out_output_node_index = node->index;
2674 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2675 sm->error_node_index = node->index;
2676 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2677 sm->pre_in2out_node_index = node->index;
2678 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2679 sm->pre_out2in_node_index = node->index;
2680 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2681 sm->pre_in2out_node_index = node->index;
2682 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2683 sm->pre_out2in_node_index = node->index;
2684 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2685 sm->in2out_fast_node_index = node->index;
2686 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2687 sm->in2out_slowpath_node_index = node->index;
2688 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2689 sm->in2out_slowpath_output_node_index = node->index;
2690 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2691 sm->ed_in2out_slowpath_node_index = node->index;
2692 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2693 sm->out2in_fast_node_index = node->index;
2694 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2695 sm->ed_out2in_slowpath_node_index = node->index;
2696 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2697 sm->hairpinning_node_index = node->index;
2698 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2699 sm->hairpin_dst_node_index = node->index;
2700 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2701 sm->hairpin_src_node_index = node->index;
2702 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpinning");
2703 sm->ed_hairpinning_node_index = node->index;
2704 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-dst");
2705 sm->ed_hairpin_dst_node_index = node->index;
2706 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-src");
2707 sm->ed_hairpin_src_node_index = node->index;
2710 #define nat_init_simple_counter(c, n, sn) \
2714 c.stat_segment_name = sn; \
2715 vlib_validate_simple_counter (&c, 0); \
2716 vlib_zero_simple_counter (&c, 0); \
2719 static clib_error_t *
2720 nat_init (vlib_main_t * vm)
2722 snat_main_t *sm = &snat_main;
2723 vlib_thread_main_t *tm = vlib_get_thread_main ();
2724 vlib_thread_registration_t *tr;
2725 ip4_add_del_interface_address_callback_t cbi = { 0 };
2726 ip4_table_bind_callback_t cbt = { 0 };
2727 u32 i, num_threads = 0;
2728 uword *p, *bitmap = 0;
2730 clib_memset (sm, 0, sizeof (*sm));
2733 sm->vnet_main = vnet_get_main ();
2735 sm->ip4_main = &ip4_main;
2736 sm->api_main = vlibapi_get_main ();
2737 sm->ip4_lookup_main = &ip4_main.lookup_main;
2739 // frame queue indices used for handoff
2740 sm->fq_out2in_index = ~0;
2741 sm->fq_in2out_index = ~0;
2742 sm->fq_in2out_output_index = ~0;
2744 sm->log_level = SNAT_LOG_ERROR;
2746 nat44_set_node_indexes (sm, vm);
2747 sm->log_class = vlib_log_register_class ("nat", 0);
2748 nat_ipfix_logging_init (vm);
2750 nat_init_simple_counter (sm->total_users, "total-users",
2751 "/nat44/total-users");
2752 nat_init_simple_counter (sm->total_sessions, "total-sessions",
2753 "/nat44/total-sessions");
2754 nat_init_simple_counter (sm->user_limit_reached, "user-limit-reached",
2755 "/nat44/user-limit-reached");
2758 sm->counters.fastpath.in2out.x.name = #x; \
2759 sm->counters.fastpath.in2out.x.stat_segment_name = \
2760 "/nat44/in2out/fastpath/" #x; \
2761 sm->counters.slowpath.in2out.x.name = #x; \
2762 sm->counters.slowpath.in2out.x.stat_segment_name = \
2763 "/nat44/in2out/slowpath/" #x; \
2764 sm->counters.fastpath.out2in.x.name = #x; \
2765 sm->counters.fastpath.out2in.x.stat_segment_name = \
2766 "/nat44/out2in/fastpath/" #x; \
2767 sm->counters.slowpath.out2in.x.name = #x; \
2768 sm->counters.slowpath.out2in.x.stat_segment_name = \
2769 "/nat44/out2in/slowpath/" #x; \
2770 sm->counters.fastpath.in2out_ed.x.name = #x; \
2771 sm->counters.fastpath.in2out_ed.x.stat_segment_name = \
2772 "/nat44/ed/in2out/fastpath/" #x; \
2773 sm->counters.slowpath.in2out_ed.x.name = #x; \
2774 sm->counters.slowpath.in2out_ed.x.stat_segment_name = \
2775 "/nat44/ed/in2out/slowpath/" #x; \
2776 sm->counters.fastpath.out2in_ed.x.name = #x; \
2777 sm->counters.fastpath.out2in_ed.x.stat_segment_name = \
2778 "/nat44/ed/out2in/fastpath/" #x; \
2779 sm->counters.slowpath.out2in_ed.x.name = #x; \
2780 sm->counters.slowpath.out2in_ed.x.stat_segment_name = \
2781 "/nat44/ed/out2in/slowpath/" #x;
2782 foreach_nat_counter;
2784 sm->counters.hairpinning.name = "hairpinning";
2785 sm->counters.hairpinning.stat_segment_name = "/nat44/hairpinning";
2787 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2790 tr = (vlib_thread_registration_t *) p[0];
2793 sm->num_workers = tr->count;
2794 sm->first_worker_index = tr->first_index;
2797 num_threads = tm->n_vlib_mains - 1;
2798 sm->port_per_thread = 0xffff - 1024;
2799 vec_validate (sm->per_thread_data, num_threads);
2801 /* Use all available workers by default */
2802 if (sm->num_workers > 1)
2805 for (i = 0; i < sm->num_workers; i++)
2806 bitmap = clib_bitmap_set (bitmap, i, 1);
2807 snat_set_workers (bitmap);
2808 clib_bitmap_free (bitmap);
2811 sm->per_thread_data[0].snat_thread_index = 0;
2813 /* callbacks to call when interface address changes. */
2814 cbi.function = snat_ip4_add_del_interface_address_cb;
2815 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2816 cbi.function = nat_ip4_add_del_addr_only_sm_cb;
2817 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2819 /* callbacks to call when interface to table biding changes */
2820 cbt.function = snat_update_outside_fib;
2821 vec_add1 (sm->ip4_main->table_bind_callbacks, cbt);
2824 fib_source_allocate ("nat-low", FIB_SOURCE_PRIORITY_LOW,
2825 FIB_SOURCE_BH_SIMPLE);
2827 fib_source_allocate ("nat-hi", FIB_SOURCE_PRIORITY_HI,
2828 FIB_SOURCE_BH_SIMPLE);
2830 /* used only by out2in-dpo feature */
2831 nat_dpo_module_init ();
2833 nat_affinity_init (vm);
2834 nat_ha_init (vm, sm->num_workers, num_threads);
2836 test_key_calc_split ();
2837 return nat44_api_hookup (vm);
2840 VLIB_INIT_FUNCTION (nat_init);
2843 nat44_plugin_enable (nat44_config_t c)
2845 snat_main_t *sm = &snat_main;
2846 u32 static_mapping_buckets = 1024;
2847 u32 static_mapping_memory_size = 64 << 20;
2851 nat_log_err ("nat44 is enabled");
2855 // c.static_mapping_only + c.connection_tracking
2856 // - supported in NAT EI & NAT ED
2857 // c.out2in_dpo, c.static_mapping_only
2858 // - supported in NAT EI
2860 if (c.endpoint_dependent)
2862 if ((c.static_mapping_only && !c.connection_tracking) || c.out2in_dpo)
2864 nat_log_err ("unsupported combination of configuration");
2867 if (c.users || c.user_sessions)
2869 nat_log_err ("unsupported combination of configuration");
2874 // reset to defaults:
2875 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2876 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2878 nat_reset_timeouts (&sm->timeouts);
2880 // nat44 feature configuration
2881 sm->endpoint_dependent = c.endpoint_dependent;
2882 sm->static_mapping_only = c.static_mapping_only;
2883 sm->static_mapping_connection_tracking = c.connection_tracking;
2884 sm->forwarding_enabled = 0;
2885 sm->mss_clamping = 0;
2890 sm->max_users_per_thread = c.users;
2891 sm->user_buckets = nat_calc_bihash_buckets (c.users);
2894 c.sessions = 10 * 1024;
2896 sm->max_translations_per_thread = c.sessions;
2897 sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
2899 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
2900 sm->max_translations_per_user
2901 = c.user_sessions ? c.user_sessions : sm->max_translations_per_thread;
2903 sm->outside_vrf_id = c.outside_vrf;
2904 sm->outside_fib_index =
2905 fib_table_find_or_create_and_lock
2906 (FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi);
2908 sm->inside_vrf_id = c.inside_vrf;
2909 sm->inside_fib_index =
2910 fib_table_find_or_create_and_lock
2911 (FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi);
2913 if (c.endpoint_dependent)
2915 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2916 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
2917 sm->out2in_node_index = sm->ed_out2in_node_index;
2918 sm->in2out_node_index = sm->ed_in2out_node_index;
2919 sm->in2out_output_node_index = sm->ed_in2out_output_node_index;
2920 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
2921 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
2923 // try to move it into nat44_db_init,
2924 // consider static mapping requirements
2925 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2926 sm->translation_buckets, 0);
2927 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
2928 format_ed_session_kvp);
2931 nat_affinity_enable ();
2933 nat_ha_enable (nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb, nat_ha_sref_ed_cb);
2937 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2938 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2939 sm->out2in_node_index = sm->ei_out2in_node_index;
2940 sm->in2out_node_index = sm->ei_in2out_node_index;
2941 sm->in2out_output_node_index = sm->ei_in2out_output_node_index;
2942 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2943 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2945 nat_ha_enable (nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
2948 // c.static_mapping & c.connection_tracking require
2950 if (!c.static_mapping_only
2951 || (c.static_mapping_only && c.connection_tracking))
2953 snat_main_per_thread_data_t *tsm;
2955 vec_foreach (tsm, sm->per_thread_data)
2957 nat44_db_init (tsm);
2963 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2964 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2967 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2968 "static_mapping_by_local", static_mapping_buckets,
2969 static_mapping_memory_size);
2970 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2971 format_static_mapping_kvp);
2973 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2974 "static_mapping_by_external",
2975 static_mapping_buckets, static_mapping_memory_size);
2976 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2977 format_static_mapping_kvp);
2979 // last: reset counters
2980 vlib_zero_simple_counter (&sm->total_users, 0);
2981 vlib_zero_simple_counter (&sm->total_sessions, 0);
2982 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
2991 nat44_addresses_free (snat_address_t ** addresses)
2995 vec_foreach (ap, *addresses)
2997 #define _(N, i, n, s) \
2998 vec_free (ap->busy_##n##_ports_per_thread);
2999 foreach_nat_protocol
3003 vec_free (*addresses);
3008 nat44_plugin_disable ()
3010 snat_main_t *sm = &snat_main;
3011 snat_interface_t *i, *vec;
3016 nat_log_err ("nat44 is disabled");
3020 // first unregister all nodes from interfaces
3021 vec = vec_dup (sm->interfaces);
3023 vec_foreach (i, vec)
3025 if (nat_interface_is_inside(i))
3026 error = snat_interface_add_del (i->sw_if_index, 1, 1);
3027 if (nat_interface_is_outside(i))
3028 error = snat_interface_add_del (i->sw_if_index, 0, 1);
3032 nat_log_err ("error occurred while removing interface %u",
3040 vec = vec_dup (sm->output_feature_interfaces);
3042 vec_foreach (i, vec)
3044 if (nat_interface_is_inside(i))
3045 error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1);
3046 if (nat_interface_is_outside(i))
3047 error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1);
3051 nat_log_err ("error occurred while removing interface %u",
3057 sm->output_feature_interfaces = 0;
3059 vec_free (sm->max_translations_per_fib);
3061 if (sm->endpoint_dependent)
3063 nat_affinity_disable ();
3064 clib_bihash_free_16_8 (&sm->out2in_ed);
3067 clib_bihash_free_8_8 (&sm->static_mapping_by_local);
3068 clib_bihash_free_8_8 (&sm->static_mapping_by_external);
3070 if (!sm->static_mapping_only ||
3071 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
3073 snat_main_per_thread_data_t *tsm;
3075 vec_foreach (tsm, sm->per_thread_data)
3077 nat44_db_free (tsm);
3082 pool_free (sm->static_mappings);
3084 nat44_addresses_free (&sm->addresses);
3085 nat44_addresses_free (&sm->twice_nat_addresses);
3088 vec_free (sm->to_resolve);
3089 vec_free (sm->auto_add_sw_if_indices);
3090 vec_free (sm->auto_add_sw_if_indices_twice_nat);
3093 sm->auto_add_sw_if_indices = 0;
3094 sm->auto_add_sw_if_indices_twice_nat = 0;
3096 sm->forwarding_enabled = 0;
3099 clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig));
3105 snat_free_outside_address_and_port (snat_address_t * addresses,
3107 ip4_address_t * addr,
3108 u16 port, nat_protocol_t protocol)
3112 u16 port_host_byte_order = clib_net_to_host_u16 (port);
3114 for (address_index = 0; address_index < vec_len (addresses);
3117 if (addresses[address_index].addr.as_u32 == addr->as_u32)
3121 ASSERT (address_index < vec_len (addresses));
3123 a = addresses + address_index;
3127 #define _(N, i, n, s) \
3128 case NAT_PROTOCOL_##N: \
3129 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
3130 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
3131 a->busy_##n##_ports--; \
3132 a->busy_##n##_ports_per_thread[thread_index]--; \
3134 foreach_nat_protocol
3137 nat_elog_info ("unknown protocol");
3143 nat_set_outside_address_and_port (snat_address_t * addresses,
3144 u32 thread_index, ip4_address_t addr,
3145 u16 port, nat_protocol_t protocol)
3147 snat_address_t *a = 0;
3149 u16 port_host_byte_order = clib_net_to_host_u16 (port);
3151 for (address_index = 0; address_index < vec_len (addresses);
3154 if (addresses[address_index].addr.as_u32 != addr.as_u32)
3157 a = addresses + address_index;
3160 #define _(N, j, n, s) \
3161 case NAT_PROTOCOL_##N: \
3162 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
3163 return VNET_API_ERROR_INSTANCE_IN_USE; \
3164 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
3165 a->busy_##n##_ports_per_thread[thread_index]++; \
3166 a->busy_##n##_ports++; \
3168 foreach_nat_protocol
3171 nat_elog_info ("unknown protocol");
3176 return VNET_API_ERROR_NO_SUCH_ENTRY;
3180 snat_static_mapping_match (snat_main_t * sm,
3181 ip4_address_t match_addr,
3183 u32 match_fib_index,
3184 nat_protocol_t match_protocol,
3185 ip4_address_t * mapping_addr,
3187 u32 * mapping_fib_index,
3190 twice_nat_type_t * twice_nat,
3191 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
3192 u8 * is_identity_nat, snat_static_mapping_t ** out)
3194 clib_bihash_kv_8_8_t kv, value;
3195 clib_bihash_8_8_t *mapping_hash;
3196 snat_static_mapping_t *m;
3197 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
3198 nat44_lb_addr_port_t *local;
3203 mapping_hash = &sm->static_mapping_by_local;
3204 init_nat_k (&kv, match_addr, match_port, match_fib_index,
3206 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3208 /* Try address only mapping */
3209 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
3210 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3216 mapping_hash = &sm->static_mapping_by_external;
3217 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
3218 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3220 /* Try address only mapping */
3221 init_nat_k (&kv, match_addr, 0, 0, 0);
3222 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3227 m = pool_elt_at_index (sm->static_mappings, value.value);
3231 if (is_lb_static_mapping (m))
3233 if (PREDICT_FALSE (lb != 0))
3234 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
3235 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
3241 local = pool_elt_at_index (m->locals, backend_index);
3242 *mapping_addr = local->addr;
3243 *mapping_port = local->port;
3244 *mapping_fib_index = local->fib_index;
3247 // pick locals matching this worker
3248 if (PREDICT_FALSE (sm->num_workers > 1))
3250 u32 thread_index = vlib_get_thread_index ();
3252 pool_foreach_index (i, m->locals,
3254 local = pool_elt_at_index (m->locals, i);
3257 .src_address = local->addr,
3260 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
3267 ASSERT (vec_len (tmp) != 0);
3272 pool_foreach_index (i, m->locals,
3278 hi = vec_len (tmp) - 1;
3279 local = pool_elt_at_index (m->locals, tmp[hi]);
3280 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
3283 mid = ((hi - lo) >> 1) + lo;
3284 local = pool_elt_at_index (m->locals, tmp[mid]);
3285 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
3287 local = pool_elt_at_index (m->locals, tmp[lo]);
3288 if (!(local->prefix >= rand))
3290 *mapping_addr = local->addr;
3291 *mapping_port = local->port;
3292 *mapping_fib_index = local->fib_index;
3295 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
3296 match_protocol, match_port,
3297 tmp[lo], m->affinity,
3298 m->affinity_per_service_list_head_index))
3299 nat_elog_info ("create affinity record failed");
3305 if (PREDICT_FALSE (lb != 0))
3307 *mapping_fib_index = m->fib_index;
3308 *mapping_addr = m->local_addr;
3309 /* Address only mapping doesn't change port */
3310 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3316 *mapping_addr = m->external_addr;
3317 /* Address only mapping doesn't change port */
3318 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3320 *mapping_fib_index = sm->outside_fib_index;
3324 if (PREDICT_FALSE (is_addr_only != 0))
3325 *is_addr_only = is_addr_only_static_mapping (m);
3327 if (PREDICT_FALSE (twice_nat != 0))
3328 *twice_nat = m->twice_nat;
3330 if (PREDICT_FALSE (is_identity_nat != 0))
3331 *is_identity_nat = is_identity_static_mapping (m);
3340 snat_alloc_outside_address_and_port (snat_address_t * addresses,
3343 nat_protocol_t proto,
3344 ip4_address_t * addr,
3346 u16 port_per_thread,
3347 u32 snat_thread_index)
3349 snat_main_t *sm = &snat_main;
3351 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, proto,
3352 addr, port, port_per_thread,
3357 nat_alloc_addr_and_port_default (snat_address_t * addresses,
3360 nat_protocol_t proto,
3361 ip4_address_t * addr,
3363 u16 port_per_thread, u32 snat_thread_index)
3366 snat_address_t *a, *ga = 0;
3369 for (i = 0; i < vec_len (addresses); i++)
3374 #define _(N, j, n, s) \
3375 case NAT_PROTOCOL_##N: \
3376 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
3378 if (a->fib_index == fib_index) \
3382 portnum = (port_per_thread * \
3383 snat_thread_index) + \
3384 snat_random_port(0, port_per_thread - 1) + 1024; \
3385 if (a->busy_##n##_port_refcounts[portnum]) \
3387 --a->busy_##n##_port_refcounts[portnum]; \
3388 a->busy_##n##_ports_per_thread[thread_index]++; \
3389 a->busy_##n##_ports++; \
3391 *port = clib_host_to_net_u16(portnum); \
3395 else if (a->fib_index == ~0) \
3401 foreach_nat_protocol
3404 nat_elog_info ("unknown protocol");
3415 #define _(N, j, n, s) \
3416 case NAT_PROTOCOL_##N: \
3419 portnum = (port_per_thread * \
3420 snat_thread_index) + \
3421 snat_random_port(0, port_per_thread - 1) + 1024; \
3422 if (a->busy_##n##_port_refcounts[portnum]) \
3424 ++a->busy_##n##_port_refcounts[portnum]; \
3425 a->busy_##n##_ports_per_thread[thread_index]++; \
3426 a->busy_##n##_ports++; \
3428 *port = clib_host_to_net_u16(portnum); \
3432 foreach_nat_protocol
3435 nat_elog_info ("unknown protocol");
3440 /* Totally out of translations to use... */
3441 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3446 nat_alloc_addr_and_port_mape (snat_address_t * addresses, u32 fib_index,
3447 u32 thread_index, nat_protocol_t proto,
3448 ip4_address_t * addr, u16 * port,
3449 u16 port_per_thread, u32 snat_thread_index)
3451 snat_main_t *sm = &snat_main;
3452 snat_address_t *a = addresses;
3453 u16 m, ports, portnum, A, j;
3454 m = 16 - (sm->psid_offset + sm->psid_length);
3455 ports = (1 << (16 - sm->psid_length)) - (1 << m);
3457 if (!vec_len (addresses))
3462 #define _(N, i, n, s) \
3463 case NAT_PROTOCOL_##N: \
3464 if (a->busy_##n##_ports < ports) \
3468 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
3469 j = snat_random_port(0, pow2_mask(m)); \
3470 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
3471 if (a->busy_##n##_port_refcounts[portnum]) \
3473 ++a->busy_##n##_port_refcounts[portnum]; \
3474 a->busy_##n##_ports++; \
3476 *port = clib_host_to_net_u16 (portnum); \
3481 foreach_nat_protocol
3484 nat_elog_info ("unknown protocol");
3489 /* Totally out of translations to use... */
3490 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3495 nat_alloc_addr_and_port_range (snat_address_t * addresses, u32 fib_index,
3496 u32 thread_index, nat_protocol_t proto,
3497 ip4_address_t * addr, u16 * port,
3498 u16 port_per_thread, u32 snat_thread_index)
3500 snat_main_t *sm = &snat_main;
3501 snat_address_t *a = addresses;
3504 ports = sm->end_port - sm->start_port + 1;
3506 if (!vec_len (addresses))
3511 #define _(N, i, n, s) \
3512 case NAT_PROTOCOL_##N: \
3513 if (a->busy_##n##_ports < ports) \
3517 portnum = snat_random_port(sm->start_port, sm->end_port); \
3518 if (a->busy_##n##_port_refcounts[portnum]) \
3520 ++a->busy_##n##_port_refcounts[portnum]; \
3521 a->busy_##n##_ports++; \
3523 *port = clib_host_to_net_u16 (portnum); \
3528 foreach_nat_protocol
3531 nat_elog_info ("unknown protocol");
3536 /* Totally out of translations to use... */
3537 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3542 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3544 snat_main_t *sm = &snat_main;
3545 dpo_id_t dpo_v4 = DPO_INVALID;
3546 fib_prefix_t pfx = {
3547 .fp_proto = FIB_PROTOCOL_IP4,
3549 .fp_addr.ip4.as_u32 = addr.as_u32,
3554 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3555 fib_table_entry_special_dpo_add (0, &pfx, sm->fib_src_hi,
3556 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3557 dpo_reset (&dpo_v4);
3561 fib_table_entry_special_remove (0, &pfx, sm->fib_src_hi);
3566 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
3569 snat_main_t *sm = &snat_main;
3570 u32 next_worker_index = 0;
3573 next_worker_index = sm->first_worker_index;
3574 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
3575 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
3577 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3578 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3580 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3582 return next_worker_index;
3586 snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
3587 u32 rx_fib_index0, u8 is_output)
3589 snat_main_t *sm = &snat_main;
3592 clib_bihash_kv_8_8_t kv, value;
3593 snat_static_mapping_t *m;
3595 u32 next_worker_index = 0;
3597 /* first try static mappings without port */
3598 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3600 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
3601 if (!clib_bihash_search_8_8
3602 (&sm->static_mapping_by_external, &kv, &value))
3604 m = pool_elt_at_index (sm->static_mappings, value.value);
3605 return m->workers[0];
3609 proto = ip_proto_to_nat_proto (ip0->protocol);
3610 udp = ip4_next_header (ip0);
3611 port = udp->dst_port;
3613 /* unknown protocol */
3614 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3616 /* use current thread */
3617 return vlib_get_thread_index ();
3620 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
3622 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3623 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3624 if (!icmp_type_is_error_message
3625 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3626 port = vnet_buffer (b)->ip.reass.l4_src_port;
3629 /* if error message, then it's not fragmented and we can access it */
3630 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3631 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3632 void *l4_header = ip4_next_header (inner_ip);
3635 case NAT_PROTOCOL_ICMP:
3636 icmp = (icmp46_header_t *) l4_header;
3637 echo = (icmp_echo_header_t *) (icmp + 1);
3638 port = echo->identifier;
3640 case NAT_PROTOCOL_UDP:
3641 case NAT_PROTOCOL_TCP:
3642 port = ((tcp_udp_header_t *) l4_header)->src_port;
3645 return vlib_get_thread_index ();
3650 /* try static mappings with port */
3651 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3653 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
3654 if (!clib_bihash_search_8_8
3655 (&sm->static_mapping_by_external, &kv, &value))
3657 m = pool_elt_at_index (sm->static_mappings, value.value);
3658 return m->workers[0];
3662 /* worker by outside port */
3663 next_worker_index = sm->first_worker_index;
3664 next_worker_index +=
3665 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3666 return next_worker_index;
3670 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3673 snat_main_t *sm = &snat_main;
3674 u32 next_worker_index = sm->first_worker_index;
3677 clib_bihash_kv_16_8_t kv16, value16;
3678 snat_main_per_thread_data_t *tsm;
3681 if (PREDICT_FALSE (is_output))
3683 u32 fib_index = sm->outside_fib_index;
3684 nat_outside_fib_t *outside_fib;
3685 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3686 fib_prefix_t pfx = {
3687 .fp_proto = FIB_PROTOCOL_IP4,
3690 .ip4.as_u32 = ip->dst_address.as_u32,
3695 udp = ip4_next_header (ip);
3697 switch (vec_len (sm->outside_fibs))
3700 fib_index = sm->outside_fib_index;
3703 fib_index = sm->outside_fibs[0].fib_index;
3707 vec_foreach (outside_fib, sm->outside_fibs)
3709 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3710 if (FIB_NODE_INDEX_INVALID != fei)
3712 if (fib_entry_get_resolving_interface (fei) != ~0)
3714 fib_index = outside_fib->fib_index;
3723 init_ed_k (&kv16, ip->src_address, udp->src_port, ip->dst_address,
3724 udp->dst_port, fib_index, ip->protocol);
3726 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3730 vec_elt_at_index (sm->per_thread_data,
3731 ed_value_get_thread_index (&value16));
3732 next_worker_index += tsm->thread_index;
3734 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3735 next_worker_index, fib_index,
3736 clib_net_to_host_u32 (ip->
3737 src_address.as_u32),
3738 clib_net_to_host_u32 (ip->
3739 dst_address.as_u32));
3741 return next_worker_index;
3745 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3746 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3748 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3749 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3751 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3753 if (PREDICT_TRUE (!is_output))
3755 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3756 next_worker_index, rx_fib_index,
3757 clib_net_to_host_u32 (ip->src_address.as_u32),
3758 clib_net_to_host_u32 (ip->dst_address.as_u32));
3762 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3763 next_worker_index, rx_fib_index,
3764 clib_net_to_host_u32 (ip->src_address.as_u32),
3765 clib_net_to_host_u32 (ip->dst_address.as_u32));
3768 return next_worker_index;
3772 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3773 u32 rx_fib_index, u8 is_output)
3775 snat_main_t *sm = &snat_main;
3776 clib_bihash_kv_8_8_t kv, value;
3777 clib_bihash_kv_16_8_t kv16, value16;
3778 snat_main_per_thread_data_t *tsm;
3780 u32 proto, next_worker_index = 0;
3783 snat_static_mapping_t *m;
3786 proto = ip_proto_to_nat_proto (ip->protocol);
3788 if (PREDICT_TRUE (proto == NAT_PROTOCOL_UDP || proto == NAT_PROTOCOL_TCP))
3790 udp = ip4_next_header (ip);
3792 init_ed_k (&kv16, ip->dst_address, udp->dst_port, ip->src_address,
3793 udp->src_port, rx_fib_index, ip->protocol);
3795 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3799 vec_elt_at_index (sm->per_thread_data,
3800 ed_value_get_thread_index (&value16));
3801 vnet_buffer2 (b)->nat.ed_out2in_nat_session_index =
3802 ed_value_get_session_index (&value16);
3803 next_worker_index = sm->first_worker_index + tsm->thread_index;
3804 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3805 next_worker_index, rx_fib_index,
3806 clib_net_to_host_u32 (ip->
3807 src_address.as_u32),
3808 clib_net_to_host_u32 (ip->
3809 dst_address.as_u32));
3810 return next_worker_index;
3813 else if (proto == NAT_PROTOCOL_ICMP)
3815 if (!get_icmp_o2i_ed_key (b, ip, rx_fib_index, ~0, ~0, 0, 0, 0, &kv16))
3817 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3821 vec_elt_at_index (sm->per_thread_data,
3822 ed_value_get_thread_index (&value16));
3823 next_worker_index = sm->first_worker_index + tsm->thread_index;
3824 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3825 next_worker_index, rx_fib_index,
3826 clib_net_to_host_u32 (ip->
3827 src_address.as_u32),
3828 clib_net_to_host_u32 (ip->
3829 dst_address.as_u32));
3830 return next_worker_index;
3835 /* first try static mappings without port */
3836 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3838 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
3839 if (!clib_bihash_search_8_8
3840 (&sm->static_mapping_by_external, &kv, &value))
3842 m = pool_elt_at_index (sm->static_mappings, value.value);
3843 next_worker_index = m->workers[0];
3848 /* unknown protocol */
3849 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3851 /* use current thread */
3852 next_worker_index = vlib_get_thread_index ();
3856 udp = ip4_next_header (ip);
3857 port = udp->dst_port;
3859 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3861 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3862 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3863 if (!icmp_type_is_error_message
3864 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3865 port = vnet_buffer (b)->ip.reass.l4_src_port;
3868 /* if error message, then it's not fragmented and we can access it */
3869 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3870 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3871 void *l4_header = ip4_next_header (inner_ip);
3874 case NAT_PROTOCOL_ICMP:
3875 icmp = (icmp46_header_t *) l4_header;
3876 echo = (icmp_echo_header_t *) (icmp + 1);
3877 port = echo->identifier;
3879 case NAT_PROTOCOL_UDP:
3880 case NAT_PROTOCOL_TCP:
3881 port = ((tcp_udp_header_t *) l4_header)->src_port;
3884 next_worker_index = vlib_get_thread_index ();
3890 /* try static mappings with port */
3891 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3893 init_nat_k (&kv, ip->dst_address, port, 0, proto);
3894 if (!clib_bihash_search_8_8
3895 (&sm->static_mapping_by_external, &kv, &value))
3897 m = pool_elt_at_index (sm->static_mappings, value.value);
3898 if (!is_lb_static_mapping (m))
3900 next_worker_index = m->workers[0];
3904 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3905 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3907 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3909 m->workers[hash & (_vec_len (m->workers) - 1)];
3911 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3916 /* worker by outside port */
3917 next_worker_index = sm->first_worker_index;
3918 next_worker_index +=
3919 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3922 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3923 clib_net_to_host_u32 (ip->src_address.as_u32),
3924 clib_net_to_host_u32 (ip->dst_address.as_u32));
3925 return next_worker_index;
3929 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
3930 ip4_address_t * out_addr, u16 out_port,
3931 ip4_address_t * eh_addr, u16 eh_port,
3932 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3933 u32 fib_index, u16 flags, u32 thread_index)
3935 snat_main_t *sm = &snat_main;
3936 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3939 clib_bihash_kv_8_8_t kv;
3940 vlib_main_t *vm = vlib_get_main ();
3941 f64 now = vlib_time_now (vm);
3942 nat_outside_fib_t *outside_fib;
3943 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3944 fib_prefix_t pfx = {
3945 .fp_proto = FIB_PROTOCOL_IP4,
3948 .ip4.as_u32 = eh_addr->as_u32,
3952 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3954 if (nat_set_outside_address_and_port
3955 (sm->addresses, thread_index, *out_addr, out_port, proto))
3959 u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3963 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
3967 if (sm->endpoint_dependent)
3969 nat_ed_lru_insert (tsm, s, now, nat_proto_to_ip_proto (proto));
3972 s->out2in.addr.as_u32 = out_addr->as_u32;
3973 s->out2in.port = out_port;
3974 s->nat_proto = proto;
3975 s->last_heard = now;
3977 s->ext_host_addr.as_u32 = eh_addr->as_u32;
3978 s->ext_host_port = eh_port;
3979 user_session_increment (sm, u, snat_is_session_static (s));
3980 switch (vec_len (sm->outside_fibs))
3983 s->out2in.fib_index = sm->outside_fib_index;
3986 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
3990 vec_foreach (outside_fib, sm->outside_fibs)
3992 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3993 if (FIB_NODE_INDEX_INVALID != fei)
3995 if (fib_entry_get_resolving_interface (fei) != ~0)
3997 s->out2in.fib_index = outside_fib->fib_index;
4005 init_nat_o2i_kv (&kv, s, s - tsm->sessions);
4006 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
4007 nat_elog_warn ("out2in key add failed");
4009 s->in2out.addr.as_u32 = in_addr->as_u32;
4010 s->in2out.port = in_port;
4011 s->in2out.fib_index = fib_index;
4012 init_nat_i2o_kv (&kv, s, s - tsm->sessions);
4013 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
4014 nat_elog_warn ("in2out key add failed");
4018 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
4019 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
4022 snat_main_t *sm = &snat_main;
4023 clib_bihash_kv_8_8_t kv, value;
4026 snat_main_per_thread_data_t *tsm;
4028 if (sm->num_workers > 1)
4030 sm->first_worker_index +
4031 (sm->workers[(clib_net_to_host_u16 (out_port) -
4032 1024) / sm->port_per_thread]);
4034 thread_index = sm->num_workers;
4035 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4037 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
4038 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
4041 s = pool_elt_at_index (tsm->sessions, value.value);
4042 nat_free_session_data (sm, s, thread_index, 1);
4043 nat44_delete_session (sm, s, thread_index);
4047 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
4048 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
4049 u32 total_pkts, u64 total_bytes, u32 thread_index)
4051 snat_main_t *sm = &snat_main;
4052 clib_bihash_kv_8_8_t kv, value;
4054 snat_main_per_thread_data_t *tsm;
4056 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4058 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
4059 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
4062 s = pool_elt_at_index (tsm->sessions, value.value);
4063 s->total_pkts = total_pkts;
4064 s->total_bytes = total_bytes;
4068 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
4069 ip4_address_t * out_addr, u16 out_port,
4070 ip4_address_t * eh_addr, u16 eh_port,
4071 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
4072 u32 fib_index, u16 flags, u32 thread_index)
4074 snat_main_t *sm = &snat_main;
4075 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
4077 clib_bihash_kv_16_8_t kv;
4078 vlib_main_t *vm = vlib_get_main ();
4079 f64 now = vlib_time_now (vm);
4080 nat_outside_fib_t *outside_fib;
4081 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
4082 fib_prefix_t pfx = {
4083 .fp_proto = FIB_PROTOCOL_IP4,
4086 .ip4.as_u32 = eh_addr->as_u32,
4091 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
4093 if (nat_set_outside_address_and_port
4094 (sm->addresses, thread_index, *out_addr, out_port, proto))
4098 if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
4100 if (nat_set_outside_address_and_port
4101 (sm->addresses, thread_index, *ehn_addr, ehn_port, proto))
4105 s = nat_ed_session_alloc (sm, thread_index, now, proto);
4109 s->last_heard = now;
4111 s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
4112 s->ext_host_nat_port = s->ext_host_port = eh_port;
4113 if (is_twice_nat_session (s))
4115 s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
4116 s->ext_host_nat_port = ehn_port;
4118 switch (vec_len (sm->outside_fibs))
4121 s->out2in.fib_index = sm->outside_fib_index;
4124 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
4128 vec_foreach (outside_fib, sm->outside_fibs)
4130 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
4131 if (FIB_NODE_INDEX_INVALID != fei)
4133 if (fib_entry_get_resolving_interface (fei) != ~0)
4135 s->out2in.fib_index = outside_fib->fib_index;
4143 s->nat_proto = proto;
4144 s->out2in.addr.as_u32 = out_addr->as_u32;
4145 s->out2in.port = out_port;
4147 s->in2out.addr.as_u32 = in_addr->as_u32;
4148 s->in2out.port = in_port;
4149 s->in2out.fib_index = fib_index;
4151 init_ed_kv (&kv, *in_addr, in_port, s->ext_host_nat_addr,
4152 s->ext_host_nat_port, fib_index, nat_proto_to_ip_proto (proto),
4153 thread_index, s - tsm->sessions);
4154 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
4155 nat_elog_warn ("in2out key add failed");
4157 init_ed_kv (&kv, *out_addr, out_port, *eh_addr, eh_port,
4158 s->out2in.fib_index, nat_proto_to_ip_proto (proto),
4159 thread_index, s - tsm->sessions);
4160 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 1))
4161 nat_elog_warn ("out2in key add failed");
4165 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
4166 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4167 u32 fib_index, u32 ti)
4169 snat_main_t *sm = &snat_main;
4170 clib_bihash_kv_16_8_t kv, value;
4173 snat_main_per_thread_data_t *tsm;
4175 if (sm->num_workers > 1)
4177 sm->first_worker_index +
4178 (sm->workers[(clib_net_to_host_u16 (out_port) -
4179 1024) / sm->port_per_thread]);
4181 thread_index = sm->num_workers;
4182 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4184 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
4185 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
4188 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
4189 nat_free_session_data (sm, s, thread_index, 1);
4190 nat44_delete_session (sm, s, thread_index);
4194 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
4195 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4196 u32 fib_index, u32 total_pkts, u64 total_bytes,
4199 snat_main_t *sm = &snat_main;
4200 clib_bihash_kv_16_8_t kv, value;
4202 snat_main_per_thread_data_t *tsm;
4204 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4206 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
4207 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
4210 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
4211 s->total_pkts = total_pkts;
4212 s->total_bytes = total_bytes;
4216 nat_calc_bihash_buckets (u32 n_elts)
4218 n_elts = n_elts / 2.5;
4220 while (lower_pow2 * 2 < n_elts)
4222 lower_pow2 = 2 * lower_pow2;
4224 u64 upper_pow2 = 2 * lower_pow2;
4225 if ((upper_pow2 - n_elts) < (n_elts - lower_pow2))
4227 if (upper_pow2 <= UINT32_MAX)
4236 nat44_get_max_session_limit ()
4238 snat_main_t *sm = &snat_main;
4239 u32 max_limit = 0, len = 0;
4241 for (; len < vec_len (sm->max_translations_per_fib); len++)
4243 if (max_limit < sm->max_translations_per_fib[len])
4244 max_limit = sm->max_translations_per_fib[len];
4250 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
4252 snat_main_t *sm = &snat_main;
4253 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4254 u32 len = vec_len (sm->max_translations_per_fib);
4256 if (len <= fib_index)
4258 vec_validate (sm->max_translations_per_fib, fib_index + 1);
4260 for (; len < vec_len (sm->max_translations_per_fib); len++)
4261 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
4264 sm->max_translations_per_fib[fib_index] = session_limit;
4269 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
4271 snat_main_t *sm = &snat_main;
4273 if (nat44_set_session_limit (session_limit, vrf_id))
4275 sm->max_translations_per_thread = nat44_get_max_session_limit ();
4277 sm->translation_buckets =
4278 nat_calc_bihash_buckets (sm->max_translations_per_thread);
4280 nat44_sessions_clear ();
4285 nat44_db_init (snat_main_per_thread_data_t * tsm)
4287 snat_main_t *sm = &snat_main;
4289 pool_alloc (tsm->sessions, sm->max_translations_per_thread);
4290 pool_alloc (tsm->lru_pool, sm->max_translations_per_thread);
4294 pool_get (tsm->lru_pool, head);
4295 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
4296 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
4298 pool_get (tsm->lru_pool, head);
4299 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
4300 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
4302 pool_get (tsm->lru_pool, head);
4303 tsm->udp_lru_head_index = head - tsm->lru_pool;
4304 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
4306 pool_get (tsm->lru_pool, head);
4307 tsm->icmp_lru_head_index = head - tsm->lru_pool;
4308 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
4310 pool_get (tsm->lru_pool, head);
4311 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
4312 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
4314 if (sm->endpoint_dependent)
4316 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
4317 sm->translation_buckets, 0);
4318 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
4319 format_ed_session_kvp);
4321 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4322 sm->translation_buckets, 0);
4323 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4324 format_ed_session_kvp); */
4328 clib_bihash_init_8_8 (&tsm->in2out, "in2out", sm->translation_buckets,
4330 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out, format_session_kvp);
4331 clib_bihash_init_8_8 (&tsm->out2in, "out2in", sm->translation_buckets,
4333 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in, format_session_kvp);
4336 // TODO: ED nat is not using these
4337 // before removal large refactor required
4338 pool_alloc (tsm->list_pool, sm->max_translations_per_thread);
4339 clib_bihash_init_8_8 (&tsm->user_hash, "users", sm->user_buckets, 0);
4340 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
4344 nat44_db_free (snat_main_per_thread_data_t * tsm)
4346 snat_main_t *sm = &snat_main;
4348 pool_free (tsm->sessions);
4349 pool_free (tsm->lru_pool);
4351 if (sm->endpoint_dependent)
4353 clib_bihash_free_16_8 (&tsm->in2out_ed);
4354 vec_free (tsm->per_vrf_sessions_vec);
4358 clib_bihash_free_8_8 (&tsm->in2out);
4359 clib_bihash_free_8_8 (&tsm->out2in);
4362 // TODO: resolve static mappings (put only to !ED)
4363 pool_free (tsm->users);
4364 pool_free (tsm->list_pool);
4365 clib_bihash_free_8_8 (&tsm->user_hash);
4369 nat44_sessions_clear ()
4371 snat_main_t *sm = &snat_main;
4372 snat_main_per_thread_data_t *tsm;
4374 if (sm->endpoint_dependent)
4376 clib_bihash_free_16_8 (&sm->out2in_ed);
4377 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4380 sm->translation_buckets, 0);
4381 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4382 format_ed_session_kvp);
4386 vec_foreach (tsm, sm->per_thread_data)
4390 nat44_db_free (tsm);
4391 nat44_db_init (tsm);
4393 ti = tsm->snat_thread_index;
4394 vlib_set_simple_counter (&sm->total_users, ti, 0, 0);
4395 vlib_set_simple_counter (&sm->total_sessions, ti, 0, 0);
4401 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
4404 ip4_address_t * address,
4406 u32 if_address_index, u32 is_delete)
4408 snat_main_t *sm = &snat_main;
4409 snat_static_map_resolve_t *rp;
4410 snat_static_mapping_t *m;
4411 clib_bihash_kv_8_8_t kv, value;
4413 ip4_address_t l_addr;
4418 for (i = 0; i < vec_len (sm->to_resolve); i++)
4420 rp = sm->to_resolve + i;
4421 if (rp->addr_only == 0)
4423 if (rp->sw_if_index == sw_if_index)
4430 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
4431 sm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
4432 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
4435 m = pool_elt_at_index (sm->static_mappings, value.value);
4439 /* Don't trip over lease renewal, static config */
4449 /* Indetity mapping? */
4450 if (rp->l_addr.as_u32 == 0)
4451 l_addr.as_u32 = address[0].as_u32;
4453 l_addr.as_u32 = rp->l_addr.as_u32;
4454 /* Add the static mapping */
4455 rv = snat_add_static_mapping (l_addr,
4460 rp->addr_only, ~0 /* sw_if_index */ ,
4461 rp->proto, !is_delete, rp->twice_nat,
4462 rp->out2in_only, rp->tag, rp->identity_nat,
4463 rp->pool_addr, rp->exact);
4465 nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
4469 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
4472 ip4_address_t * address,
4474 u32 if_address_index, u32 is_delete)
4476 snat_main_t *sm = &snat_main;
4477 snat_static_map_resolve_t *rp;
4478 ip4_address_t l_addr;
4482 snat_address_t *addresses = sm->addresses;
4487 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
4489 if (sw_if_index == sm->auto_add_sw_if_indices[i])
4493 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
4496 addresses = sm->twice_nat_addresses;
4497 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
4506 /* Don't trip over lease renewal, static config */
4507 for (j = 0; j < vec_len (addresses); j++)
4508 if (addresses[j].addr.as_u32 == address->as_u32)
4511 (void) snat_add_address (sm, address, ~0, twice_nat);
4512 /* Scan static map resolution vector */
4513 for (j = 0; j < vec_len (sm->to_resolve); j++)
4515 rp = sm->to_resolve + j;
4518 /* On this interface? */
4519 if (rp->sw_if_index == sw_if_index)
4521 /* Indetity mapping? */
4522 if (rp->l_addr.as_u32 == 0)
4523 l_addr.as_u32 = address[0].as_u32;
4525 l_addr.as_u32 = rp->l_addr.as_u32;
4526 /* Add the static mapping */
4527 rv = snat_add_static_mapping (l_addr,
4533 ~0 /* sw_if_index */ ,
4535 rp->is_add, rp->twice_nat,
4536 rp->out2in_only, rp->tag,
4538 rp->pool_addr, rp->exact);
4540 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
4548 (void) snat_del_address (sm, address[0], 1, twice_nat);
4554 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
4557 ip4_main_t *ip4_main = sm->ip4_main;
4558 ip4_address_t *first_int_addr;
4559 snat_static_map_resolve_t *rp;
4560 u32 *indices_to_delete = 0;
4562 u32 *auto_add_sw_if_indices =
4564 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
4566 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
4569 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
4571 if (auto_add_sw_if_indices[i] == sw_if_index)
4575 /* if have address remove it */
4577 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
4580 for (j = 0; j < vec_len (sm->to_resolve); j++)
4582 rp = sm->to_resolve + j;
4583 if (rp->sw_if_index == sw_if_index)
4584 vec_add1 (indices_to_delete, j);
4586 if (vec_len (indices_to_delete))
4588 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
4589 vec_del1 (sm->to_resolve, j);
4590 vec_free (indices_to_delete);
4594 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
4596 vec_del1 (sm->auto_add_sw_if_indices, i);
4599 return VNET_API_ERROR_VALUE_EXIST;
4606 return VNET_API_ERROR_NO_SUCH_ENTRY;
4608 /* add to the auto-address list */
4610 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
4612 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
4614 /* If the address is already bound - or static - add it now */
4616 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
4622 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4623 nat_protocol_t proto, u32 vrf_id, int is_in)
4625 snat_main_per_thread_data_t *tsm;
4626 clib_bihash_kv_8_8_t kv, value;
4628 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4630 clib_bihash_8_8_t *t;
4632 if (sm->endpoint_dependent)
4633 return VNET_API_ERROR_UNSUPPORTED;
4635 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4636 if (sm->num_workers > 1)
4638 vec_elt_at_index (sm->per_thread_data,
4639 sm->worker_in2out_cb (&ip, fib_index, 0));
4641 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4643 init_nat_k (&kv, *addr, port, fib_index, proto);
4644 t = is_in ? &tsm->in2out : &tsm->out2in;
4645 if (!clib_bihash_search_8_8 (t, &kv, &value))
4647 if (pool_is_free_index (tsm->sessions, value.value))
4648 return VNET_API_ERROR_UNSPECIFIED;
4650 s = pool_elt_at_index (tsm->sessions, value.value);
4651 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4652 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4656 return VNET_API_ERROR_NO_SUCH_ENTRY;
4660 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4661 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4662 u32 vrf_id, int is_in)
4665 clib_bihash_16_8_t *t;
4666 clib_bihash_kv_16_8_t kv, value;
4667 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4669 snat_main_per_thread_data_t *tsm;
4671 if (!sm->endpoint_dependent)
4672 return VNET_API_ERROR_FEATURE_DISABLED;
4674 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4675 if (sm->num_workers > 1)
4677 vec_elt_at_index (sm->per_thread_data,
4678 sm->worker_in2out_cb (&ip, fib_index, 0));
4680 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4682 t = is_in ? &tsm->in2out_ed : &sm->out2in_ed;
4683 init_ed_k (&kv, *addr, port, *eh_addr, eh_port, fib_index, proto);
4684 if (clib_bihash_search_16_8 (t, &kv, &value))
4686 return VNET_API_ERROR_NO_SUCH_ENTRY;
4689 if (pool_is_free_index (tsm->sessions, value.value))
4690 return VNET_API_ERROR_UNSPECIFIED;
4691 s = pool_elt_at_index (tsm->sessions, value.value);
4692 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4693 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
4698 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
4700 snat_main_t *sm = &snat_main;
4702 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
4703 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
4705 sm->psid_offset = psid_offset;
4706 sm->psid_length = psid_length;
4710 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
4712 snat_main_t *sm = &snat_main;
4714 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
4715 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
4716 sm->start_port = start_port;
4717 sm->end_port = end_port;
4721 nat_set_alloc_addr_and_port_default (void)
4723 snat_main_t *sm = &snat_main;
4725 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
4726 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
4729 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
4730 vlib_node_runtime_t * node,
4731 vlib_frame_t * frame)
4737 VLIB_REGISTER_NODE (nat_default_node) = {
4738 .name = "nat-default",
4739 .vector_size = sizeof (u32),
4741 .type = VLIB_NODE_TYPE_INTERNAL,
4743 .n_next_nodes = NAT_N_NEXT,
4745 [NAT_NEXT_DROP] = "error-drop",
4746 [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
4747 [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
4748 [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
4749 [NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH] = "nat44-ed-in2out-output",
4750 [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
4751 [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
4752 [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
4753 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
4754 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
4760 * fd.io coding-style-patch-verification: ON
4763 * eval: (c-set-style "gnu")