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->out2in_dpo = c.out2in_dpo;
2885 sm->forwarding_enabled = 0;
2886 sm->mss_clamping = 0;
2891 sm->max_users_per_thread = c.users;
2892 sm->user_buckets = nat_calc_bihash_buckets (c.users);
2895 c.sessions = 10 * 1024;
2897 sm->max_translations_per_thread = c.sessions;
2898 sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
2900 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
2901 sm->max_translations_per_user
2902 = c.user_sessions ? c.user_sessions : sm->max_translations_per_thread;
2904 sm->outside_vrf_id = c.outside_vrf;
2905 sm->outside_fib_index =
2906 fib_table_find_or_create_and_lock
2907 (FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi);
2909 sm->inside_vrf_id = c.inside_vrf;
2910 sm->inside_fib_index =
2911 fib_table_find_or_create_and_lock
2912 (FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi);
2914 if (c.endpoint_dependent)
2916 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2917 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
2918 sm->out2in_node_index = sm->ed_out2in_node_index;
2919 sm->in2out_node_index = sm->ed_in2out_node_index;
2920 sm->in2out_output_node_index = sm->ed_in2out_output_node_index;
2921 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
2922 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
2924 // try to move it into nat44_db_init,
2925 // consider static mapping requirements
2926 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2927 sm->translation_buckets, 0);
2928 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
2929 format_ed_session_kvp);
2932 nat_affinity_enable ();
2934 nat_ha_enable (nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb, nat_ha_sref_ed_cb);
2938 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2939 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2940 sm->out2in_node_index = sm->ei_out2in_node_index;
2941 sm->in2out_node_index = sm->ei_in2out_node_index;
2942 sm->in2out_output_node_index = sm->ei_in2out_output_node_index;
2943 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2944 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2946 nat_ha_enable (nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
2949 // c.static_mapping & c.connection_tracking require
2951 if (!c.static_mapping_only
2952 || (c.static_mapping_only && c.connection_tracking))
2954 snat_main_per_thread_data_t *tsm;
2956 vec_foreach (tsm, sm->per_thread_data)
2958 nat44_db_init (tsm);
2964 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2965 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2968 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2969 "static_mapping_by_local", static_mapping_buckets,
2970 static_mapping_memory_size);
2971 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2972 format_static_mapping_kvp);
2974 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2975 "static_mapping_by_external",
2976 static_mapping_buckets, static_mapping_memory_size);
2977 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2978 format_static_mapping_kvp);
2980 // last: reset counters
2981 vlib_zero_simple_counter (&sm->total_users, 0);
2982 vlib_zero_simple_counter (&sm->total_sessions, 0);
2983 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
2992 nat44_addresses_free (snat_address_t ** addresses)
2996 vec_foreach (ap, *addresses)
2998 #define _(N, i, n, s) \
2999 vec_free (ap->busy_##n##_ports_per_thread);
3000 foreach_nat_protocol
3004 vec_free (*addresses);
3009 nat44_plugin_disable ()
3011 snat_main_t *sm = &snat_main;
3012 snat_interface_t *i, *vec;
3017 nat_log_err ("nat44 is disabled");
3023 // first unregister all nodes from interfaces
3024 vec = vec_dup (sm->interfaces);
3026 vec_foreach (i, vec)
3028 if (nat_interface_is_inside(i))
3029 error = snat_interface_add_del (i->sw_if_index, 1, 1);
3030 if (nat_interface_is_outside(i))
3031 error = snat_interface_add_del (i->sw_if_index, 0, 1);
3035 nat_log_err ("error occurred while removing interface %u",
3043 vec = vec_dup (sm->output_feature_interfaces);
3045 vec_foreach (i, vec)
3047 if (nat_interface_is_inside(i))
3048 error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1);
3049 if (nat_interface_is_outside(i))
3050 error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1);
3054 nat_log_err ("error occurred while removing interface %u",
3060 sm->output_feature_interfaces = 0;
3062 vec_free (sm->max_translations_per_fib);
3064 if (sm->endpoint_dependent)
3066 nat_affinity_disable ();
3067 clib_bihash_free_16_8 (&sm->out2in_ed);
3070 clib_bihash_free_8_8 (&sm->static_mapping_by_local);
3071 clib_bihash_free_8_8 (&sm->static_mapping_by_external);
3073 if (!sm->static_mapping_only ||
3074 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
3076 snat_main_per_thread_data_t *tsm;
3078 vec_foreach (tsm, sm->per_thread_data)
3080 nat44_db_free (tsm);
3085 pool_free (sm->static_mappings);
3087 nat44_addresses_free (&sm->addresses);
3088 nat44_addresses_free (&sm->twice_nat_addresses);
3091 vec_free (sm->to_resolve);
3092 vec_free (sm->auto_add_sw_if_indices);
3093 vec_free (sm->auto_add_sw_if_indices_twice_nat);
3096 sm->auto_add_sw_if_indices = 0;
3097 sm->auto_add_sw_if_indices_twice_nat = 0;
3099 sm->forwarding_enabled = 0;
3102 clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig));
3108 snat_free_outside_address_and_port (snat_address_t * addresses,
3110 ip4_address_t * addr,
3111 u16 port, nat_protocol_t protocol)
3115 u16 port_host_byte_order = clib_net_to_host_u16 (port);
3117 for (address_index = 0; address_index < vec_len (addresses);
3120 if (addresses[address_index].addr.as_u32 == addr->as_u32)
3124 ASSERT (address_index < vec_len (addresses));
3126 a = addresses + address_index;
3130 #define _(N, i, n, s) \
3131 case NAT_PROTOCOL_##N: \
3132 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
3133 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
3134 a->busy_##n##_ports--; \
3135 a->busy_##n##_ports_per_thread[thread_index]--; \
3137 foreach_nat_protocol
3140 nat_elog_info ("unknown protocol");
3146 nat_set_outside_address_and_port (snat_address_t * addresses,
3147 u32 thread_index, ip4_address_t addr,
3148 u16 port, nat_protocol_t protocol)
3150 snat_address_t *a = 0;
3152 u16 port_host_byte_order = clib_net_to_host_u16 (port);
3154 for (address_index = 0; address_index < vec_len (addresses);
3157 if (addresses[address_index].addr.as_u32 != addr.as_u32)
3160 a = addresses + address_index;
3163 #define _(N, j, n, s) \
3164 case NAT_PROTOCOL_##N: \
3165 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
3166 return VNET_API_ERROR_INSTANCE_IN_USE; \
3167 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
3168 a->busy_##n##_ports_per_thread[thread_index]++; \
3169 a->busy_##n##_ports++; \
3171 foreach_nat_protocol
3174 nat_elog_info ("unknown protocol");
3179 return VNET_API_ERROR_NO_SUCH_ENTRY;
3183 snat_static_mapping_match (snat_main_t * sm,
3184 ip4_address_t match_addr,
3186 u32 match_fib_index,
3187 nat_protocol_t match_protocol,
3188 ip4_address_t * mapping_addr,
3190 u32 * mapping_fib_index,
3193 twice_nat_type_t * twice_nat,
3194 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
3195 u8 * is_identity_nat, snat_static_mapping_t ** out)
3197 clib_bihash_kv_8_8_t kv, value;
3198 clib_bihash_8_8_t *mapping_hash;
3199 snat_static_mapping_t *m;
3200 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
3201 nat44_lb_addr_port_t *local;
3206 mapping_hash = &sm->static_mapping_by_local;
3207 init_nat_k (&kv, match_addr, match_port, match_fib_index,
3209 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3211 /* Try address only mapping */
3212 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
3213 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3219 mapping_hash = &sm->static_mapping_by_external;
3220 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
3221 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3223 /* Try address only mapping */
3224 init_nat_k (&kv, match_addr, 0, 0, 0);
3225 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3230 m = pool_elt_at_index (sm->static_mappings, value.value);
3234 if (is_lb_static_mapping (m))
3236 if (PREDICT_FALSE (lb != 0))
3237 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
3238 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
3244 local = pool_elt_at_index (m->locals, backend_index);
3245 *mapping_addr = local->addr;
3246 *mapping_port = local->port;
3247 *mapping_fib_index = local->fib_index;
3250 // pick locals matching this worker
3251 if (PREDICT_FALSE (sm->num_workers > 1))
3253 u32 thread_index = vlib_get_thread_index ();
3255 pool_foreach_index (i, m->locals)
3257 local = pool_elt_at_index (m->locals, i);
3260 .src_address = local->addr,
3263 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
3270 ASSERT (vec_len (tmp) != 0);
3275 pool_foreach_index (i, m->locals)
3281 hi = vec_len (tmp) - 1;
3282 local = pool_elt_at_index (m->locals, tmp[hi]);
3283 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
3286 mid = ((hi - lo) >> 1) + lo;
3287 local = pool_elt_at_index (m->locals, tmp[mid]);
3288 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
3290 local = pool_elt_at_index (m->locals, tmp[lo]);
3291 if (!(local->prefix >= rand))
3293 *mapping_addr = local->addr;
3294 *mapping_port = local->port;
3295 *mapping_fib_index = local->fib_index;
3298 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
3299 match_protocol, match_port,
3300 tmp[lo], m->affinity,
3301 m->affinity_per_service_list_head_index))
3302 nat_elog_info ("create affinity record failed");
3308 if (PREDICT_FALSE (lb != 0))
3310 *mapping_fib_index = m->fib_index;
3311 *mapping_addr = m->local_addr;
3312 /* Address only mapping doesn't change port */
3313 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3319 *mapping_addr = m->external_addr;
3320 /* Address only mapping doesn't change port */
3321 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3323 *mapping_fib_index = sm->outside_fib_index;
3327 if (PREDICT_FALSE (is_addr_only != 0))
3328 *is_addr_only = is_addr_only_static_mapping (m);
3330 if (PREDICT_FALSE (twice_nat != 0))
3331 *twice_nat = m->twice_nat;
3333 if (PREDICT_FALSE (is_identity_nat != 0))
3334 *is_identity_nat = is_identity_static_mapping (m);
3343 snat_alloc_outside_address_and_port (snat_address_t * addresses,
3346 nat_protocol_t proto,
3347 ip4_address_t * addr,
3349 u16 port_per_thread,
3350 u32 snat_thread_index)
3352 snat_main_t *sm = &snat_main;
3354 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, proto,
3355 addr, port, port_per_thread,
3360 nat_alloc_addr_and_port_default (snat_address_t * addresses,
3363 nat_protocol_t proto,
3364 ip4_address_t * addr,
3366 u16 port_per_thread, u32 snat_thread_index)
3369 snat_address_t *a, *ga = 0;
3372 for (i = 0; i < vec_len (addresses); i++)
3377 #define _(N, j, n, s) \
3378 case NAT_PROTOCOL_##N: \
3379 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
3381 if (a->fib_index == fib_index) \
3385 portnum = (port_per_thread * \
3386 snat_thread_index) + \
3387 snat_random_port(0, port_per_thread - 1) + 1024; \
3388 if (a->busy_##n##_port_refcounts[portnum]) \
3390 --a->busy_##n##_port_refcounts[portnum]; \
3391 a->busy_##n##_ports_per_thread[thread_index]++; \
3392 a->busy_##n##_ports++; \
3394 *port = clib_host_to_net_u16(portnum); \
3398 else if (a->fib_index == ~0) \
3404 foreach_nat_protocol
3407 nat_elog_info ("unknown protocol");
3418 #define _(N, j, n, s) \
3419 case NAT_PROTOCOL_##N: \
3422 portnum = (port_per_thread * \
3423 snat_thread_index) + \
3424 snat_random_port(0, port_per_thread - 1) + 1024; \
3425 if (a->busy_##n##_port_refcounts[portnum]) \
3427 ++a->busy_##n##_port_refcounts[portnum]; \
3428 a->busy_##n##_ports_per_thread[thread_index]++; \
3429 a->busy_##n##_ports++; \
3431 *port = clib_host_to_net_u16(portnum); \
3435 foreach_nat_protocol
3438 nat_elog_info ("unknown protocol");
3443 /* Totally out of translations to use... */
3444 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3449 nat_alloc_addr_and_port_mape (snat_address_t * addresses, u32 fib_index,
3450 u32 thread_index, nat_protocol_t proto,
3451 ip4_address_t * addr, u16 * port,
3452 u16 port_per_thread, u32 snat_thread_index)
3454 snat_main_t *sm = &snat_main;
3455 snat_address_t *a = addresses;
3456 u16 m, ports, portnum, A, j;
3457 m = 16 - (sm->psid_offset + sm->psid_length);
3458 ports = (1 << (16 - sm->psid_length)) - (1 << m);
3460 if (!vec_len (addresses))
3465 #define _(N, i, n, s) \
3466 case NAT_PROTOCOL_##N: \
3467 if (a->busy_##n##_ports < ports) \
3471 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
3472 j = snat_random_port(0, pow2_mask(m)); \
3473 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
3474 if (a->busy_##n##_port_refcounts[portnum]) \
3476 ++a->busy_##n##_port_refcounts[portnum]; \
3477 a->busy_##n##_ports++; \
3479 *port = clib_host_to_net_u16 (portnum); \
3484 foreach_nat_protocol
3487 nat_elog_info ("unknown protocol");
3492 /* Totally out of translations to use... */
3493 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3498 nat_alloc_addr_and_port_range (snat_address_t * addresses, u32 fib_index,
3499 u32 thread_index, nat_protocol_t proto,
3500 ip4_address_t * addr, u16 * port,
3501 u16 port_per_thread, u32 snat_thread_index)
3503 snat_main_t *sm = &snat_main;
3504 snat_address_t *a = addresses;
3507 ports = sm->end_port - sm->start_port + 1;
3509 if (!vec_len (addresses))
3514 #define _(N, i, n, s) \
3515 case NAT_PROTOCOL_##N: \
3516 if (a->busy_##n##_ports < ports) \
3520 portnum = snat_random_port(sm->start_port, sm->end_port); \
3521 if (a->busy_##n##_port_refcounts[portnum]) \
3523 ++a->busy_##n##_port_refcounts[portnum]; \
3524 a->busy_##n##_ports++; \
3526 *port = clib_host_to_net_u16 (portnum); \
3531 foreach_nat_protocol
3534 nat_elog_info ("unknown protocol");
3539 /* Totally out of translations to use... */
3540 nat_ipfix_logging_addresses_exhausted (thread_index, 0);
3545 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3547 snat_main_t *sm = &snat_main;
3548 dpo_id_t dpo_v4 = DPO_INVALID;
3549 fib_prefix_t pfx = {
3550 .fp_proto = FIB_PROTOCOL_IP4,
3552 .fp_addr.ip4.as_u32 = addr.as_u32,
3557 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3558 fib_table_entry_special_dpo_add (0, &pfx, sm->fib_src_hi,
3559 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3560 dpo_reset (&dpo_v4);
3564 fib_table_entry_special_remove (0, &pfx, sm->fib_src_hi);
3569 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
3572 snat_main_t *sm = &snat_main;
3573 u32 next_worker_index = 0;
3576 next_worker_index = sm->first_worker_index;
3577 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
3578 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
3580 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3581 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3583 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3585 return next_worker_index;
3589 snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
3590 u32 rx_fib_index0, u8 is_output)
3592 snat_main_t *sm = &snat_main;
3595 clib_bihash_kv_8_8_t kv, value;
3596 snat_static_mapping_t *m;
3598 u32 next_worker_index = 0;
3600 /* first try static mappings without port */
3601 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3603 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
3604 if (!clib_bihash_search_8_8
3605 (&sm->static_mapping_by_external, &kv, &value))
3607 m = pool_elt_at_index (sm->static_mappings, value.value);
3608 return m->workers[0];
3612 proto = ip_proto_to_nat_proto (ip0->protocol);
3613 udp = ip4_next_header (ip0);
3614 port = udp->dst_port;
3616 /* unknown protocol */
3617 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3619 /* use current thread */
3620 return vlib_get_thread_index ();
3623 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
3625 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3626 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3627 if (!icmp_type_is_error_message
3628 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3629 port = vnet_buffer (b)->ip.reass.l4_src_port;
3632 /* if error message, then it's not fragmented and we can access it */
3633 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3634 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3635 void *l4_header = ip4_next_header (inner_ip);
3638 case NAT_PROTOCOL_ICMP:
3639 icmp = (icmp46_header_t *) l4_header;
3640 echo = (icmp_echo_header_t *) (icmp + 1);
3641 port = echo->identifier;
3643 case NAT_PROTOCOL_UDP:
3644 case NAT_PROTOCOL_TCP:
3645 port = ((tcp_udp_header_t *) l4_header)->src_port;
3648 return vlib_get_thread_index ();
3653 /* try static mappings with port */
3654 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3656 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
3657 if (!clib_bihash_search_8_8
3658 (&sm->static_mapping_by_external, &kv, &value))
3660 m = pool_elt_at_index (sm->static_mappings, value.value);
3661 return m->workers[0];
3665 /* worker by outside port */
3666 next_worker_index = sm->first_worker_index;
3667 next_worker_index +=
3668 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3669 return next_worker_index;
3673 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3676 snat_main_t *sm = &snat_main;
3677 u32 next_worker_index = sm->first_worker_index;
3680 clib_bihash_kv_16_8_t kv16, value16;
3681 snat_main_per_thread_data_t *tsm;
3684 if (PREDICT_FALSE (is_output))
3686 u32 fib_index = sm->outside_fib_index;
3687 nat_outside_fib_t *outside_fib;
3688 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3689 fib_prefix_t pfx = {
3690 .fp_proto = FIB_PROTOCOL_IP4,
3693 .ip4.as_u32 = ip->dst_address.as_u32,
3698 udp = ip4_next_header (ip);
3700 switch (vec_len (sm->outside_fibs))
3703 fib_index = sm->outside_fib_index;
3706 fib_index = sm->outside_fibs[0].fib_index;
3710 vec_foreach (outside_fib, sm->outside_fibs)
3712 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3713 if (FIB_NODE_INDEX_INVALID != fei)
3715 if (fib_entry_get_resolving_interface (fei) != ~0)
3717 fib_index = outside_fib->fib_index;
3726 init_ed_k (&kv16, ip->src_address, udp->src_port, ip->dst_address,
3727 udp->dst_port, fib_index, ip->protocol);
3729 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3733 vec_elt_at_index (sm->per_thread_data,
3734 ed_value_get_thread_index (&value16));
3735 next_worker_index += tsm->thread_index;
3737 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3738 next_worker_index, fib_index,
3739 clib_net_to_host_u32 (ip->
3740 src_address.as_u32),
3741 clib_net_to_host_u32 (ip->
3742 dst_address.as_u32));
3744 return next_worker_index;
3748 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3749 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3751 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3752 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3754 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3756 if (PREDICT_TRUE (!is_output))
3758 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3759 next_worker_index, rx_fib_index,
3760 clib_net_to_host_u32 (ip->src_address.as_u32),
3761 clib_net_to_host_u32 (ip->dst_address.as_u32));
3765 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3766 next_worker_index, rx_fib_index,
3767 clib_net_to_host_u32 (ip->src_address.as_u32),
3768 clib_net_to_host_u32 (ip->dst_address.as_u32));
3771 return next_worker_index;
3775 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3776 u32 rx_fib_index, u8 is_output)
3778 snat_main_t *sm = &snat_main;
3779 clib_bihash_kv_8_8_t kv, value;
3780 clib_bihash_kv_16_8_t kv16, value16;
3781 snat_main_per_thread_data_t *tsm;
3783 u32 proto, next_worker_index = 0;
3786 snat_static_mapping_t *m;
3789 proto = ip_proto_to_nat_proto (ip->protocol);
3791 if (PREDICT_TRUE (proto == NAT_PROTOCOL_UDP || proto == NAT_PROTOCOL_TCP))
3793 udp = ip4_next_header (ip);
3795 init_ed_k (&kv16, ip->dst_address, udp->dst_port, ip->src_address,
3796 udp->src_port, rx_fib_index, ip->protocol);
3798 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3802 vec_elt_at_index (sm->per_thread_data,
3803 ed_value_get_thread_index (&value16));
3804 vnet_buffer2 (b)->nat.ed_out2in_nat_session_index =
3805 ed_value_get_session_index (&value16);
3806 next_worker_index = sm->first_worker_index + tsm->thread_index;
3807 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3808 next_worker_index, rx_fib_index,
3809 clib_net_to_host_u32 (ip->
3810 src_address.as_u32),
3811 clib_net_to_host_u32 (ip->
3812 dst_address.as_u32));
3813 return next_worker_index;
3816 else if (proto == NAT_PROTOCOL_ICMP)
3818 if (!get_icmp_o2i_ed_key (b, ip, rx_fib_index, ~0, ~0, 0, 0, 0, &kv16))
3820 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3824 vec_elt_at_index (sm->per_thread_data,
3825 ed_value_get_thread_index (&value16));
3826 next_worker_index = sm->first_worker_index + tsm->thread_index;
3827 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3828 next_worker_index, rx_fib_index,
3829 clib_net_to_host_u32 (ip->
3830 src_address.as_u32),
3831 clib_net_to_host_u32 (ip->
3832 dst_address.as_u32));
3833 return next_worker_index;
3838 /* first try static mappings without port */
3839 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3841 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
3842 if (!clib_bihash_search_8_8
3843 (&sm->static_mapping_by_external, &kv, &value))
3845 m = pool_elt_at_index (sm->static_mappings, value.value);
3846 next_worker_index = m->workers[0];
3851 /* unknown protocol */
3852 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3854 /* use current thread */
3855 next_worker_index = vlib_get_thread_index ();
3859 udp = ip4_next_header (ip);
3860 port = udp->dst_port;
3862 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3864 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3865 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3866 if (!icmp_type_is_error_message
3867 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3868 port = vnet_buffer (b)->ip.reass.l4_src_port;
3871 /* if error message, then it's not fragmented and we can access it */
3872 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3873 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3874 void *l4_header = ip4_next_header (inner_ip);
3877 case NAT_PROTOCOL_ICMP:
3878 icmp = (icmp46_header_t *) l4_header;
3879 echo = (icmp_echo_header_t *) (icmp + 1);
3880 port = echo->identifier;
3882 case NAT_PROTOCOL_UDP:
3883 case NAT_PROTOCOL_TCP:
3884 port = ((tcp_udp_header_t *) l4_header)->src_port;
3887 next_worker_index = vlib_get_thread_index ();
3893 /* try static mappings with port */
3894 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3896 init_nat_k (&kv, ip->dst_address, port, 0, proto);
3897 if (!clib_bihash_search_8_8
3898 (&sm->static_mapping_by_external, &kv, &value))
3900 m = pool_elt_at_index (sm->static_mappings, value.value);
3901 if (!is_lb_static_mapping (m))
3903 next_worker_index = m->workers[0];
3907 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3908 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3910 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3912 m->workers[hash & (_vec_len (m->workers) - 1)];
3914 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3919 /* worker by outside port */
3920 next_worker_index = sm->first_worker_index;
3921 next_worker_index +=
3922 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3925 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3926 clib_net_to_host_u32 (ip->src_address.as_u32),
3927 clib_net_to_host_u32 (ip->dst_address.as_u32));
3928 return next_worker_index;
3932 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
3933 ip4_address_t * out_addr, u16 out_port,
3934 ip4_address_t * eh_addr, u16 eh_port,
3935 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3936 u32 fib_index, u16 flags, u32 thread_index)
3938 snat_main_t *sm = &snat_main;
3939 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3942 clib_bihash_kv_8_8_t kv;
3943 vlib_main_t *vm = vlib_get_main ();
3944 f64 now = vlib_time_now (vm);
3945 nat_outside_fib_t *outside_fib;
3946 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3947 fib_prefix_t pfx = {
3948 .fp_proto = FIB_PROTOCOL_IP4,
3951 .ip4.as_u32 = eh_addr->as_u32,
3955 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3957 if (nat_set_outside_address_and_port
3958 (sm->addresses, thread_index, *out_addr, out_port, proto))
3962 u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3966 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
3970 if (sm->endpoint_dependent)
3972 nat_ed_lru_insert (tsm, s, now, nat_proto_to_ip_proto (proto));
3975 s->out2in.addr.as_u32 = out_addr->as_u32;
3976 s->out2in.port = out_port;
3977 s->nat_proto = proto;
3978 s->last_heard = now;
3980 s->ext_host_addr.as_u32 = eh_addr->as_u32;
3981 s->ext_host_port = eh_port;
3982 user_session_increment (sm, u, snat_is_session_static (s));
3983 switch (vec_len (sm->outside_fibs))
3986 s->out2in.fib_index = sm->outside_fib_index;
3989 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
3993 vec_foreach (outside_fib, sm->outside_fibs)
3995 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3996 if (FIB_NODE_INDEX_INVALID != fei)
3998 if (fib_entry_get_resolving_interface (fei) != ~0)
4000 s->out2in.fib_index = outside_fib->fib_index;
4008 init_nat_o2i_kv (&kv, s, s - tsm->sessions);
4009 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
4010 nat_elog_warn ("out2in key add failed");
4012 s->in2out.addr.as_u32 = in_addr->as_u32;
4013 s->in2out.port = in_port;
4014 s->in2out.fib_index = fib_index;
4015 init_nat_i2o_kv (&kv, s, s - tsm->sessions);
4016 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
4017 nat_elog_warn ("in2out key add failed");
4021 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
4022 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
4025 snat_main_t *sm = &snat_main;
4026 clib_bihash_kv_8_8_t kv, value;
4029 snat_main_per_thread_data_t *tsm;
4031 if (sm->num_workers > 1)
4033 sm->first_worker_index +
4034 (sm->workers[(clib_net_to_host_u16 (out_port) -
4035 1024) / sm->port_per_thread]);
4037 thread_index = sm->num_workers;
4038 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4040 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
4041 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
4044 s = pool_elt_at_index (tsm->sessions, value.value);
4045 nat_free_session_data (sm, s, thread_index, 1);
4046 nat44_delete_session (sm, s, thread_index);
4050 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
4051 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
4052 u32 total_pkts, u64 total_bytes, u32 thread_index)
4054 snat_main_t *sm = &snat_main;
4055 clib_bihash_kv_8_8_t kv, value;
4057 snat_main_per_thread_data_t *tsm;
4059 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4061 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
4062 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
4065 s = pool_elt_at_index (tsm->sessions, value.value);
4066 s->total_pkts = total_pkts;
4067 s->total_bytes = total_bytes;
4071 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
4072 ip4_address_t * out_addr, u16 out_port,
4073 ip4_address_t * eh_addr, u16 eh_port,
4074 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
4075 u32 fib_index, u16 flags, u32 thread_index)
4077 snat_main_t *sm = &snat_main;
4078 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
4080 clib_bihash_kv_16_8_t kv;
4081 vlib_main_t *vm = vlib_get_main ();
4082 f64 now = vlib_time_now (vm);
4083 nat_outside_fib_t *outside_fib;
4084 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
4085 fib_prefix_t pfx = {
4086 .fp_proto = FIB_PROTOCOL_IP4,
4089 .ip4.as_u32 = eh_addr->as_u32,
4094 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
4096 if (nat_set_outside_address_and_port
4097 (sm->addresses, thread_index, *out_addr, out_port, proto))
4101 if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
4103 if (nat_set_outside_address_and_port
4104 (sm->addresses, thread_index, *ehn_addr, ehn_port, proto))
4108 s = nat_ed_session_alloc (sm, thread_index, now, proto);
4112 s->last_heard = now;
4114 s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
4115 s->ext_host_nat_port = s->ext_host_port = eh_port;
4116 if (is_twice_nat_session (s))
4118 s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
4119 s->ext_host_nat_port = ehn_port;
4121 switch (vec_len (sm->outside_fibs))
4124 s->out2in.fib_index = sm->outside_fib_index;
4127 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
4131 vec_foreach (outside_fib, sm->outside_fibs)
4133 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
4134 if (FIB_NODE_INDEX_INVALID != fei)
4136 if (fib_entry_get_resolving_interface (fei) != ~0)
4138 s->out2in.fib_index = outside_fib->fib_index;
4146 s->nat_proto = proto;
4147 s->out2in.addr.as_u32 = out_addr->as_u32;
4148 s->out2in.port = out_port;
4150 s->in2out.addr.as_u32 = in_addr->as_u32;
4151 s->in2out.port = in_port;
4152 s->in2out.fib_index = fib_index;
4154 init_ed_kv (&kv, *in_addr, in_port, s->ext_host_nat_addr,
4155 s->ext_host_nat_port, fib_index, nat_proto_to_ip_proto (proto),
4156 thread_index, s - tsm->sessions);
4157 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
4158 nat_elog_warn ("in2out key add failed");
4160 init_ed_kv (&kv, *out_addr, out_port, *eh_addr, eh_port,
4161 s->out2in.fib_index, nat_proto_to_ip_proto (proto),
4162 thread_index, s - tsm->sessions);
4163 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 1))
4164 nat_elog_warn ("out2in key add failed");
4168 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
4169 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4170 u32 fib_index, u32 ti)
4172 snat_main_t *sm = &snat_main;
4173 clib_bihash_kv_16_8_t kv, value;
4176 snat_main_per_thread_data_t *tsm;
4178 if (sm->num_workers > 1)
4180 sm->first_worker_index +
4181 (sm->workers[(clib_net_to_host_u16 (out_port) -
4182 1024) / sm->port_per_thread]);
4184 thread_index = sm->num_workers;
4185 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4187 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
4188 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
4191 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
4192 nat_free_session_data (sm, s, thread_index, 1);
4193 nat44_delete_session (sm, s, thread_index);
4197 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
4198 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4199 u32 fib_index, u32 total_pkts, u64 total_bytes,
4202 snat_main_t *sm = &snat_main;
4203 clib_bihash_kv_16_8_t kv, value;
4205 snat_main_per_thread_data_t *tsm;
4207 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
4209 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
4210 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
4213 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
4214 s->total_pkts = total_pkts;
4215 s->total_bytes = total_bytes;
4219 nat_calc_bihash_buckets (u32 n_elts)
4221 n_elts = n_elts / 2.5;
4223 while (lower_pow2 * 2 < n_elts)
4225 lower_pow2 = 2 * lower_pow2;
4227 u64 upper_pow2 = 2 * lower_pow2;
4228 if ((upper_pow2 - n_elts) < (n_elts - lower_pow2))
4230 if (upper_pow2 <= UINT32_MAX)
4239 nat44_get_max_session_limit ()
4241 snat_main_t *sm = &snat_main;
4242 u32 max_limit = 0, len = 0;
4244 for (; len < vec_len (sm->max_translations_per_fib); len++)
4246 if (max_limit < sm->max_translations_per_fib[len])
4247 max_limit = sm->max_translations_per_fib[len];
4253 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
4255 snat_main_t *sm = &snat_main;
4256 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4257 u32 len = vec_len (sm->max_translations_per_fib);
4259 if (len <= fib_index)
4261 vec_validate (sm->max_translations_per_fib, fib_index + 1);
4263 for (; len < vec_len (sm->max_translations_per_fib); len++)
4264 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
4267 sm->max_translations_per_fib[fib_index] = session_limit;
4272 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
4274 snat_main_t *sm = &snat_main;
4276 if (nat44_set_session_limit (session_limit, vrf_id))
4278 sm->max_translations_per_thread = nat44_get_max_session_limit ();
4280 sm->translation_buckets =
4281 nat_calc_bihash_buckets (sm->max_translations_per_thread);
4283 nat44_sessions_clear ();
4288 nat44_db_init (snat_main_per_thread_data_t * tsm)
4290 snat_main_t *sm = &snat_main;
4292 pool_alloc (tsm->sessions, sm->max_translations_per_thread);
4293 pool_alloc (tsm->lru_pool, sm->max_translations_per_thread);
4297 pool_get (tsm->lru_pool, head);
4298 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
4299 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
4301 pool_get (tsm->lru_pool, head);
4302 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
4303 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
4305 pool_get (tsm->lru_pool, head);
4306 tsm->udp_lru_head_index = head - tsm->lru_pool;
4307 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
4309 pool_get (tsm->lru_pool, head);
4310 tsm->icmp_lru_head_index = head - tsm->lru_pool;
4311 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
4313 pool_get (tsm->lru_pool, head);
4314 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
4315 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
4317 if (sm->endpoint_dependent)
4319 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
4320 sm->translation_buckets, 0);
4321 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
4322 format_ed_session_kvp);
4324 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4325 sm->translation_buckets, 0);
4326 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4327 format_ed_session_kvp); */
4331 clib_bihash_init_8_8 (&tsm->in2out, "in2out", sm->translation_buckets,
4333 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out, format_session_kvp);
4334 clib_bihash_init_8_8 (&tsm->out2in, "out2in", sm->translation_buckets,
4336 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in, format_session_kvp);
4339 // TODO: ED nat is not using these
4340 // before removal large refactor required
4341 pool_alloc (tsm->list_pool, sm->max_translations_per_thread);
4342 clib_bihash_init_8_8 (&tsm->user_hash, "users", sm->user_buckets, 0);
4343 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
4347 nat44_db_free (snat_main_per_thread_data_t * tsm)
4349 snat_main_t *sm = &snat_main;
4351 pool_free (tsm->sessions);
4352 pool_free (tsm->lru_pool);
4354 if (sm->endpoint_dependent)
4356 clib_bihash_free_16_8 (&tsm->in2out_ed);
4357 vec_free (tsm->per_vrf_sessions_vec);
4361 clib_bihash_free_8_8 (&tsm->in2out);
4362 clib_bihash_free_8_8 (&tsm->out2in);
4365 // TODO: resolve static mappings (put only to !ED)
4366 pool_free (tsm->users);
4367 pool_free (tsm->list_pool);
4368 clib_bihash_free_8_8 (&tsm->user_hash);
4372 nat44_sessions_clear ()
4374 snat_main_t *sm = &snat_main;
4375 snat_main_per_thread_data_t *tsm;
4377 if (sm->endpoint_dependent)
4379 clib_bihash_free_16_8 (&sm->out2in_ed);
4380 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4383 sm->translation_buckets, 0);
4384 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4385 format_ed_session_kvp);
4389 vec_foreach (tsm, sm->per_thread_data)
4391 nat44_db_free (tsm);
4392 nat44_db_init (tsm);
4396 vlib_zero_simple_counter (&sm->total_users, 0);
4397 vlib_zero_simple_counter (&sm->total_sessions, 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_OUT2IN_ED_HANDOFF] = "nat44-ed-out2in-handoff",
4754 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
4755 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
4761 * fd.io coding-style-patch-verification: ON
4764 * eval: (c-set-style "gnu")