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 <vnet/fib/fib_table.h>
30 #include <vnet/fib/ip4_fib.h>
31 #include <vnet/ip/reass/ip4_sv_reass.h>
32 #include <vppinfra/bihash_16_8.h>
33 #include <nat/nat44/ed_inlines.h>
34 #include <vnet/ip/ip_table.h>
36 #include <nat/nat44-ei/nat44_ei_ha.h>
37 #include <nat/nat44-ei/nat44_ei.h>
39 #include <vpp/app/version.h>
41 snat_main_t snat_main;
44 /* Hook up input features */
45 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "nat-pre-in2out",
48 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
49 "ip4-sv-reassembly-feature"),
51 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
52 .arc_name = "ip4-unicast",
53 .node_name = "nat-pre-out2in",
54 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
55 "ip4-dhcp-client-detect",
56 "ip4-sv-reassembly-feature"),
58 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
59 .arc_name = "ip4-unicast",
60 .node_name = "nat44-in2out-worker-handoff",
61 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
63 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
64 .arc_name = "ip4-unicast",
65 .node_name = "nat44-out2in-worker-handoff",
66 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
67 "ip4-dhcp-client-detect"),
69 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
70 .arc_name = "ip4-unicast",
71 .node_name = "nat44-in2out",
72 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
74 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
75 .arc_name = "ip4-unicast",
76 .node_name = "nat44-out2in",
77 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
78 "ip4-dhcp-client-detect"),
80 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
81 .arc_name = "ip4-unicast",
82 .node_name = "nat44-classify",
83 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
85 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
86 .arc_name = "ip4-unicast",
87 .node_name = "nat44-ed-in2out",
88 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
90 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
91 .arc_name = "ip4-unicast",
92 .node_name = "nat44-ed-out2in",
93 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
94 "ip4-dhcp-client-detect"),
96 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
97 .arc_name = "ip4-unicast",
98 .node_name = "nat44-ed-classify",
99 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
101 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "nat44-handoff-classify",
104 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
106 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
107 .arc_name = "ip4-unicast",
108 .node_name = "nat44-in2out-fast",
109 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
111 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
112 .arc_name = "ip4-unicast",
113 .node_name = "nat44-out2in-fast",
114 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
115 "ip4-dhcp-client-detect"),
117 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
118 .arc_name = "ip4-unicast",
119 .node_name = "nat44-hairpin-dst",
120 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
122 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
123 .arc_name = "ip4-unicast",
124 .node_name = "nat44-ed-hairpin-dst",
125 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
128 /* Hook up output features */
129 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
130 .arc_name = "ip4-output",
131 .node_name = "nat44-in2out-output",
132 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
134 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
135 .arc_name = "ip4-output",
136 .node_name = "nat44-in2out-output-worker-handoff",
137 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
139 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
140 .arc_name = "ip4-output",
141 .node_name = "nat44-hairpin-src",
142 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
144 VNET_FEATURE_INIT (nat_pre_in2out_output, static) = {
145 .arc_name = "ip4-output",
146 .node_name = "nat-pre-in2out-output",
147 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
148 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
150 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
151 .arc_name = "ip4-output",
152 .node_name = "nat44-ed-in2out-output",
153 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
154 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
156 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
157 .arc_name = "ip4-output",
158 .node_name = "nat44-ed-hairpin-src",
159 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
160 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
163 /* Hook up ip4-local features */
164 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
166 .arc_name = "ip4-local",
167 .node_name = "nat44-hairpinning",
168 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
170 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
172 .arc_name = "ip4-local",
173 .node_name = "nat44-ed-hairpinning",
174 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
178 VLIB_PLUGIN_REGISTER () = {
179 .version = VPP_BUILD_VER,
180 .description = "Network Address Translation (NAT)",
185 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
186 u32 rx_fib_index, u8 is_output);
189 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
192 static u32 nat_calc_bihash_buckets (u32 n_elts);
195 format_session_kvp (u8 * s, va_list * args)
197 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
199 s = format (s, "%U session-index %llu", format_snat_key, v->key, v->value);
205 format_static_mapping_kvp (u8 * s, va_list * args)
207 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
209 s = format (s, "%U static-mapping-index %llu",
210 format_snat_key, v->key, v->value);
216 format_user_kvp (u8 * s, va_list * args)
218 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
223 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
224 k.fib_index, v->value);
230 format_ed_session_kvp (u8 * s, va_list * args)
232 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
236 ip4_address_t l_addr, r_addr;
239 split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
242 "local %U:%d remote %U:%d proto %U fib %d thread-index %u session-index %u",
243 format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
244 format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
245 format_ip_protocol, proto, fib_index,
246 ed_value_get_session_index (v), ed_value_get_thread_index (v));
252 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
255 clib_bihash_kv_8_8_t kv;
258 ip4_address_t *l_addr, *r_addr;
260 clib_bihash_kv_16_8_t ed_kv;
261 snat_main_per_thread_data_t *tsm =
262 vec_elt_at_index (sm->per_thread_data, thread_index);
264 if (is_ed_session (s))
266 per_vrf_sessions_unregister_session (s, thread_index);
269 if (is_fwd_bypass_session (s))
271 if (snat_is_unk_proto_session (s))
273 init_ed_k (&ed_kv, s->in2out.addr, 0, s->ext_host_addr, 0, 0,
278 l_port = s->in2out.port;
279 r_port = s->ext_host_port;
280 l_addr = &s->in2out.addr;
281 r_addr = &s->ext_host_addr;
282 proto = nat_proto_to_ip_proto (s->nat_proto);
283 fib_index = s->in2out.fib_index;
284 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index,
287 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
288 nat_elog_warn ("in2out_ed key del failed");
292 /* session lookup tables */
293 if (is_ed_session (s))
295 if (is_affinity_sessions (s))
296 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
297 s->nat_proto, s->out2in.port);
298 l_addr = &s->out2in.addr;
299 r_addr = &s->ext_host_addr;
300 fib_index = s->out2in.fib_index;
301 if (snat_is_unk_proto_session (s))
303 proto = s->in2out.port;
309 proto = nat_proto_to_ip_proto (s->nat_proto);
310 l_port = s->out2in.port;
311 r_port = s->ext_host_port;
313 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
314 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
315 nat_elog_warn ("out2in_ed key del failed");
316 l_addr = &s->in2out.addr;
317 fib_index = s->in2out.fib_index;
318 if (!snat_is_unk_proto_session (s))
319 l_port = s->in2out.port;
320 if (is_twice_nat_session (s))
322 r_addr = &s->ext_host_nat_addr;
323 r_port = s->ext_host_nat_port;
325 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
326 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
327 nat_elog_warn ("in2out_ed key del failed");
330 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
331 &s->in2out.addr, s->in2out.port,
332 &s->ext_host_nat_addr, s->ext_host_nat_port,
333 &s->out2in.addr, s->out2in.port,
334 &s->ext_host_addr, s->ext_host_port,
335 s->nat_proto, is_twice_nat_session (s));
339 init_nat_i2o_k (&kv, s);
340 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
341 nat_elog_warn ("in2out key del failed");
342 init_nat_o2i_k (&kv, s);
343 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
344 nat_elog_warn ("out2in key del failed");
347 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
348 &s->in2out.addr, s->in2out.port,
349 &s->out2in.addr, s->out2in.port,
353 if (snat_is_unk_proto_session (s))
359 nat_ipfix_logging_nat44_ses_delete (thread_index,
360 s->in2out.addr.as_u32,
361 s->out2in.addr.as_u32,
365 s->in2out.fib_index);
367 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
368 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
372 /* Twice NAT address and port for external host */
373 if (is_twice_nat_session (s))
375 snat_free_outside_address_and_port (sm->twice_nat_addresses,
377 &s->ext_host_nat_addr,
378 s->ext_host_nat_port, s->nat_proto);
381 if (snat_is_session_static (s))
384 snat_free_outside_address_and_port (sm->addresses, thread_index,
385 &s->out2in.addr, s->out2in.port,
390 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
394 snat_user_key_t user_key;
395 clib_bihash_kv_8_8_t kv, value;
396 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
397 dlist_elt_t *per_user_list_head_elt;
399 user_key.addr.as_u32 = addr->as_u32;
400 user_key.fib_index = fib_index;
401 kv.key = user_key.as_u64;
403 /* Ever heard of the "user" = src ip4 address before? */
404 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
406 if (pool_elts (tsm->users) >= sm->max_users_per_thread)
408 vlib_increment_simple_counter (&sm->user_limit_reached,
410 nat_elog_warn ("maximum user limit reached");
413 /* no, make a new one */
414 pool_get (tsm->users, u);
415 clib_memset (u, 0, sizeof (*u));
417 u->addr.as_u32 = addr->as_u32;
418 u->fib_index = fib_index;
420 pool_get (tsm->list_pool, per_user_list_head_elt);
422 u->sessions_per_user_list_head_index = per_user_list_head_elt -
425 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
427 kv.value = u - tsm->users;
430 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
432 nat_elog_warn ("user_hash key add failed");
433 nat44_delete_user_with_no_session (sm, u, thread_index);
437 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
438 pool_elts (tsm->users));
442 u = pool_elt_at_index (tsm->users, value.value);
450 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
451 u32 thread_index, f64 now)
454 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
455 u32 oldest_per_user_translation_list_index, session_index;
456 dlist_elt_t *oldest_per_user_translation_list_elt;
457 dlist_elt_t *per_user_translation_list_elt;
459 /* Over quota? Recycle the least recently used translation */
460 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
462 oldest_per_user_translation_list_index =
463 clib_dlist_remove_head (tsm->list_pool,
464 u->sessions_per_user_list_head_index);
466 ASSERT (oldest_per_user_translation_list_index != ~0);
468 /* Add it back to the end of the LRU list */
469 clib_dlist_addtail (tsm->list_pool,
470 u->sessions_per_user_list_head_index,
471 oldest_per_user_translation_list_index);
472 /* Get the list element */
473 oldest_per_user_translation_list_elt =
474 pool_elt_at_index (tsm->list_pool,
475 oldest_per_user_translation_list_index);
477 /* Get the session index from the list element */
478 session_index = oldest_per_user_translation_list_elt->value;
480 /* Get the session */
481 s = pool_elt_at_index (tsm->sessions, session_index);
483 // TODO: ONLY EI version should be called
484 nat_free_session_data (sm, s, thread_index, 0);
485 if (snat_is_session_static (s))
486 u->nstaticsessions--;
493 s->ext_host_addr.as_u32 = 0;
494 s->ext_host_port = 0;
495 s->ext_host_nat_addr.as_u32 = 0;
496 s->ext_host_nat_port = 0;
500 pool_get (tsm->sessions, s);
501 clib_memset (s, 0, sizeof (*s));
503 /* Create list elts */
504 pool_get (tsm->list_pool, per_user_translation_list_elt);
505 clib_dlist_init (tsm->list_pool,
506 per_user_translation_list_elt - tsm->list_pool);
508 per_user_translation_list_elt->value = s - tsm->sessions;
509 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
510 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
512 clib_dlist_addtail (tsm->list_pool,
513 s->per_user_list_head_index,
514 per_user_translation_list_elt - tsm->list_pool);
516 s->user_index = u - tsm->users;
517 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
518 pool_elts (tsm->sessions));
521 s->ha_last_refreshed = now;
527 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
530 snat_main_t *sm = &snat_main;
531 fib_prefix_t prefix = {
533 .fp_proto = FIB_PROTOCOL_IP4,
535 .ip4.as_u32 = addr->as_u32,
538 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
541 fib_table_entry_update_one_path (fib_index,
544 (FIB_ENTRY_FLAG_CONNECTED |
545 FIB_ENTRY_FLAG_LOCAL |
546 FIB_ENTRY_FLAG_EXCLUSIVE),
550 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
552 fib_table_entry_delete (fib_index, &prefix, sm->fib_src_low);
556 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
561 vlib_thread_main_t *tm = vlib_get_thread_main ();
563 if (twice_nat && !sm->endpoint_dependent)
565 nat_log_err ("unsupported");
566 return VNET_API_ERROR_UNSUPPORTED;
569 /* Check if address already exists */
571 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
573 if (ap->addr.as_u32 == addr->as_u32)
575 nat_log_err ("address exist");
576 return VNET_API_ERROR_VALUE_EXIST;
582 vec_add2 (sm->twice_nat_addresses, ap, 1);
584 vec_add2 (sm->addresses, ap, 1);
589 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
595 #define _(N, i, n, s) \
596 clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
597 ap->busy_##n##_ports = 0; \
598 ap->busy_##n##_ports_per_thread = 0;\
599 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
607 /* Add external address to FIB */
609 pool_foreach (i, sm->interfaces)
611 if (nat_interface_is_inside(i) || sm->out2in_dpo)
614 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
617 pool_foreach (i, sm->output_feature_interfaces)
619 if (nat_interface_is_inside(i) || sm->out2in_dpo)
622 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
631 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
633 snat_static_mapping_t *m;
635 pool_foreach (m, sm->static_mappings)
637 if (is_addr_only_static_mapping (m) ||
638 is_out2in_only_static_mapping (m) ||
639 is_identity_static_mapping (m))
641 if (m->external_addr.as_u32 == addr.as_u32)
650 snat_add_static_mapping_when_resolved (snat_main_t * sm,
651 ip4_address_t l_addr,
656 nat_protocol_t proto,
657 int addr_only, int is_add, u8 * tag,
658 int twice_nat, int out2in_only,
660 ip4_address_t pool_addr, int exact)
662 snat_static_map_resolve_t *rp;
664 vec_add2 (sm->to_resolve, rp, 1);
665 rp->l_addr.as_u32 = l_addr.as_u32;
667 rp->sw_if_index = sw_if_index;
671 rp->addr_only = addr_only;
673 rp->twice_nat = twice_nat;
674 rp->out2in_only = out2in_only;
675 rp->identity_nat = identity_nat;
676 rp->tag = vec_dup (tag);
677 rp->pool_addr = pool_addr;
682 get_thread_idx_by_port (u16 e_port)
684 snat_main_t *sm = &snat_main;
685 u32 thread_idx = sm->num_workers;
686 if (sm->num_workers > 1)
689 sm->first_worker_index +
690 sm->workers[(e_port - 1024) / sm->port_per_thread];
696 nat_ed_static_mapping_del_sessions (snat_main_t * sm,
697 snat_main_per_thread_data_t * tsm,
698 ip4_address_t l_addr,
701 u32 fib_index, int addr_only,
702 ip4_address_t e_addr, u16 e_port)
705 u32 *indexes_to_free = NULL;
707 pool_foreach (s, tsm->sessions) {
708 if (s->in2out.fib_index != fib_index ||
709 s->in2out.addr.as_u32 != l_addr.as_u32)
715 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
716 s->out2in.port != e_port ||
717 s->in2out.port != l_port ||
718 s->nat_proto != protocol)
722 if (is_lb_session (s))
724 if (!snat_is_session_static (s))
726 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
727 vec_add1 (indexes_to_free, s - tsm->sessions);
733 vec_foreach (ses_index, indexes_to_free)
735 s = pool_elt_at_index (tsm->sessions, *ses_index);
736 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
738 vec_free (indexes_to_free);
742 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
743 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
744 u32 sw_if_index, nat_protocol_t proto, int is_add,
745 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
746 u8 identity_nat, ip4_address_t pool_addr, int exact)
748 snat_main_t *sm = &snat_main;
749 snat_static_mapping_t *m;
750 clib_bihash_kv_8_8_t kv, value;
751 snat_address_t *a = 0;
753 snat_interface_t *interface;
755 snat_main_per_thread_data_t *tsm;
756 snat_user_key_t u_key;
758 dlist_elt_t *head, *elt;
759 u32 elt_index, head_index;
763 snat_static_map_resolve_t *rp, *rp_match = 0;
764 nat44_lb_addr_port_t *local;
767 if (!sm->endpoint_dependent)
769 if (twice_nat || out2in_only)
770 return VNET_API_ERROR_UNSUPPORTED;
773 /* If the external address is a specific interface address */
774 if (sw_if_index != ~0)
776 ip4_address_t *first_int_addr;
778 for (i = 0; i < vec_len (sm->to_resolve); i++)
780 rp = sm->to_resolve + i;
781 if (rp->sw_if_index != sw_if_index ||
782 rp->l_addr.as_u32 != l_addr.as_u32 ||
783 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
788 if ((rp->l_port != l_port && rp->e_port != e_port)
789 || rp->proto != proto)
797 /* Might be already set... */
798 first_int_addr = ip4_interface_first_address
799 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
804 return VNET_API_ERROR_VALUE_EXIST;
806 snat_add_static_mapping_when_resolved
807 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
808 addr_only, is_add, tag, twice_nat, out2in_only,
809 identity_nat, pool_addr, exact);
811 /* DHCP resolution required? */
812 if (first_int_addr == 0)
818 e_addr.as_u32 = first_int_addr->as_u32;
819 /* Identity mapping? */
820 if (l_addr.as_u32 == 0)
821 l_addr.as_u32 = e_addr.as_u32;
827 return VNET_API_ERROR_NO_SUCH_ENTRY;
829 vec_del1 (sm->to_resolve, i);
833 e_addr.as_u32 = first_int_addr->as_u32;
834 /* Identity mapping? */
835 if (l_addr.as_u32 == 0)
836 l_addr.as_u32 = e_addr.as_u32;
843 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
844 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
847 m = pool_elt_at_index (sm->static_mappings, value.value);
853 if (is_identity_static_mapping (m))
856 pool_foreach (local, m->locals)
858 if (local->vrf_id == vrf_id)
859 return VNET_API_ERROR_VALUE_EXIST;
862 pool_get (m->locals, local);
863 local->vrf_id = vrf_id;
865 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
867 init_nat_kv (&kv, m->local_addr, m->local_port,
868 local->fib_index, m->proto,
869 m - sm->static_mappings);
870 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
874 return VNET_API_ERROR_VALUE_EXIST;
877 if (twice_nat && addr_only)
878 return VNET_API_ERROR_UNSUPPORTED;
880 /* Convert VRF id to FIB index */
883 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
885 /* If not specified use inside VRF id from SNAT plugin startup config */
888 fib_index = sm->inside_fib_index;
889 vrf_id = sm->inside_vrf_id;
890 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
893 if (!(out2in_only || identity_nat))
895 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
896 addr_only ? 0 : proto);
897 if (!clib_bihash_search_8_8
898 (&sm->static_mapping_by_local, &kv, &value))
899 return VNET_API_ERROR_VALUE_EXIST;
902 /* Find external address in allocated addresses and reserve port for
903 address and port pair mapping when dynamic translations enabled */
904 if (!(addr_only || sm->static_mapping_only || out2in_only))
906 for (i = 0; i < vec_len (sm->addresses); i++)
908 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
910 a = sm->addresses + i;
911 /* External port must be unused */
914 #define _(N, j, n, s) \
915 case NAT_PROTOCOL_##N: \
916 if (a->busy_##n##_port_refcounts[e_port]) \
917 return VNET_API_ERROR_INVALID_VALUE; \
918 ++a->busy_##n##_port_refcounts[e_port]; \
921 a->busy_##n##_ports++; \
922 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
928 nat_elog_info ("unknown protocol");
929 return VNET_API_ERROR_INVALID_VALUE_2;
934 /* External address must be allocated */
935 if (!a && (l_addr.as_u32 != e_addr.as_u32))
937 if (sw_if_index != ~0)
939 for (i = 0; i < vec_len (sm->to_resolve); i++)
941 rp = sm->to_resolve + i;
944 if (rp->sw_if_index != sw_if_index &&
945 rp->l_addr.as_u32 != l_addr.as_u32 &&
946 rp->vrf_id != vrf_id && rp->l_port != l_port &&
947 rp->e_port != e_port && rp->proto != proto)
950 vec_del1 (sm->to_resolve, i);
954 return VNET_API_ERROR_NO_SUCH_ENTRY;
958 pool_get (sm->static_mappings, m);
959 clib_memset (m, 0, sizeof (*m));
960 m->tag = vec_dup (tag);
961 m->local_addr = l_addr;
962 m->external_addr = e_addr;
963 m->twice_nat = twice_nat;
965 if (twice_nat == TWICE_NAT && exact)
967 m->flags |= NAT_STATIC_MAPPING_FLAG_EXACT_ADDRESS;
968 m->pool_addr = pool_addr;
972 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
974 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
977 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
978 pool_get (m->locals, local);
979 local->vrf_id = vrf_id;
980 local->fib_index = fib_index;
985 m->fib_index = fib_index;
989 m->local_port = l_port;
990 m->external_port = e_port;
994 if (sm->num_workers > 1)
997 .src_address = m->local_addr,
999 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
1000 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1003 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1005 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto,
1006 m - sm->static_mappings);
1008 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1010 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1011 m - sm->static_mappings);
1012 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
1014 /* Delete dynamic sessions matching local address (+ local port) */
1015 // TODO: based on type of NAT EI/ED
1016 if (!(sm->static_mapping_only))
1018 u_key.addr = m->local_addr;
1019 u_key.fib_index = m->fib_index;
1020 kv.key = u_key.as_u64;
1021 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1023 user_index = value.value;
1024 u = pool_elt_at_index (tsm->users, user_index);
1027 head_index = u->sessions_per_user_list_head_index;
1028 head = pool_elt_at_index (tsm->list_pool, head_index);
1029 elt_index = head->next;
1030 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1031 ses_index = elt->value;
1032 while (ses_index != ~0)
1034 s = pool_elt_at_index (tsm->sessions, ses_index);
1035 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1036 ses_index = elt->value;
1038 if (snat_is_session_static (s))
1041 if (!addr_only && s->in2out.port != m->local_port)
1044 nat_free_session_data (sm, s,
1045 tsm - sm->per_thread_data, 0);
1046 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1048 if (!addr_only && !sm->endpoint_dependent)
1059 if (sw_if_index != ~0)
1062 return VNET_API_ERROR_NO_SUCH_ENTRY;
1068 vrf_id = sm->inside_vrf_id;
1071 pool_foreach (local, m->locals)
1073 if (local->vrf_id == vrf_id)
1074 find = local - m->locals;
1078 return VNET_API_ERROR_NO_SUCH_ENTRY;
1080 local = pool_elt_at_index (m->locals, find);
1081 fib_index = local->fib_index;
1082 pool_put (m->locals, local);
1085 fib_index = m->fib_index;
1087 /* Free external address port */
1088 if (!(addr_only || sm->static_mapping_only || out2in_only))
1090 for (i = 0; i < vec_len (sm->addresses); i++)
1092 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1094 a = sm->addresses + i;
1097 #define _(N, j, n, s) \
1098 case NAT_PROTOCOL_##N: \
1099 --a->busy_##n##_port_refcounts[e_port]; \
1100 if (e_port > 1024) \
1102 a->busy_##n##_ports--; \
1103 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1106 foreach_nat_protocol
1109 nat_elog_info ("unknown protocol");
1110 return VNET_API_ERROR_INVALID_VALUE_2;
1117 if (sm->num_workers > 1)
1118 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1120 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1122 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
1124 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1126 /* Delete session(s) for static mapping if exist */
1127 if (!(sm->static_mapping_only) ||
1128 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1130 if (sm->endpoint_dependent)
1132 nat_ed_static_mapping_del_sessions (sm, tsm, m->local_addr,
1133 m->local_port, m->proto,
1134 fib_index, addr_only,
1139 u_key.addr = m->local_addr;
1140 u_key.fib_index = fib_index;
1141 kv.key = u_key.as_u64;
1142 nat44_ei_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
1147 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1148 if (pool_elts (m->locals))
1151 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1152 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1155 vec_free (m->workers);
1156 /* Delete static mapping from pool */
1157 pool_put (sm->static_mappings, m);
1160 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1163 /* Add/delete external address to FIB */
1165 pool_foreach (interface, sm->interfaces)
1167 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1170 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1173 pool_foreach (interface, sm->output_feature_interfaces)
1175 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1178 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1187 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1188 nat_protocol_t proto,
1189 nat44_lb_addr_port_t * locals, u8 is_add,
1190 twice_nat_type_t twice_nat, u8 out2in_only,
1191 u8 * tag, u32 affinity)
1193 snat_main_t *sm = &snat_main;
1194 snat_static_mapping_t *m;
1195 clib_bihash_kv_8_8_t kv, value;
1196 snat_address_t *a = 0;
1198 nat44_lb_addr_port_t *local;
1199 snat_main_per_thread_data_t *tsm;
1203 if (!sm->endpoint_dependent)
1204 return VNET_API_ERROR_UNSUPPORTED;
1206 init_nat_k (&kv, e_addr, e_port, 0, proto);
1207 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1210 m = pool_elt_at_index (sm->static_mappings, value.value);
1215 return VNET_API_ERROR_VALUE_EXIST;
1217 if (vec_len (locals) < 2)
1218 return VNET_API_ERROR_INVALID_VALUE;
1220 /* Find external address in allocated addresses and reserve port for
1221 address and port pair mapping when dynamic translations enabled */
1222 if (!(sm->static_mapping_only || out2in_only))
1224 for (i = 0; i < vec_len (sm->addresses); i++)
1226 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1228 a = sm->addresses + i;
1229 /* External port must be unused */
1232 #define _(N, j, n, s) \
1233 case NAT_PROTOCOL_##N: \
1234 if (a->busy_##n##_port_refcounts[e_port]) \
1235 return VNET_API_ERROR_INVALID_VALUE; \
1236 ++a->busy_##n##_port_refcounts[e_port]; \
1237 if (e_port > 1024) \
1239 a->busy_##n##_ports++; \
1240 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1243 foreach_nat_protocol
1246 nat_elog_info ("unknown protocol");
1247 return VNET_API_ERROR_INVALID_VALUE_2;
1252 /* External address must be allocated */
1254 return VNET_API_ERROR_NO_SUCH_ENTRY;
1257 pool_get (sm->static_mappings, m);
1258 clib_memset (m, 0, sizeof (*m));
1259 m->tag = vec_dup (tag);
1260 m->external_addr = e_addr;
1261 m->external_port = e_port;
1263 m->twice_nat = twice_nat;
1264 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1266 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1267 m->affinity = affinity;
1270 m->affinity_per_service_list_head_index =
1271 nat_affinity_get_per_service_list_head_index ();
1273 m->affinity_per_service_list_head_index = ~0;
1275 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1276 m - sm->static_mappings);
1277 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1279 nat_elog_err ("static_mapping_by_external key add failed");
1280 return VNET_API_ERROR_UNSPECIFIED;
1283 for (i = 0; i < vec_len (locals); i++)
1285 locals[i].fib_index =
1286 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1291 init_nat_kv (&kv, locals[i].addr, locals[i].port,
1292 locals[i].fib_index, m->proto,
1293 m - sm->static_mappings);
1294 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1296 locals[i].prefix = (i == 0) ? locals[i].probability :
1297 (locals[i - 1].prefix + locals[i].probability);
1298 pool_get (m->locals, local);
1300 if (sm->num_workers > 1)
1303 .src_address = locals[i].addr,
1306 clib_bitmap_set (bitmap,
1307 sm->worker_in2out_cb (&ip, m->fib_index, 0),
1312 /* Assign workers */
1313 if (sm->num_workers > 1)
1316 clib_bitmap_foreach (i, bitmap)
1318 vec_add1(m->workers, i);
1326 return VNET_API_ERROR_NO_SUCH_ENTRY;
1328 if (!is_lb_static_mapping (m))
1329 return VNET_API_ERROR_INVALID_VALUE;
1331 /* Free external address port */
1332 if (!(sm->static_mapping_only || out2in_only))
1334 for (i = 0; i < vec_len (sm->addresses); i++)
1336 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1338 a = sm->addresses + i;
1341 #define _(N, j, n, s) \
1342 case NAT_PROTOCOL_##N: \
1343 --a->busy_##n##_port_refcounts[e_port]; \
1344 if (e_port > 1024) \
1346 a->busy_##n##_ports--; \
1347 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1350 foreach_nat_protocol
1353 nat_elog_info ("unknown protocol");
1354 return VNET_API_ERROR_INVALID_VALUE_2;
1361 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1362 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1364 nat_elog_err ("static_mapping_by_external key del failed");
1365 return VNET_API_ERROR_UNSPECIFIED;
1369 pool_foreach (local, m->locals)
1371 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1375 init_nat_k(& kv, local->addr, local->port, local->fib_index, m->proto);
1376 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1378 nat_elog_err ("static_mapping_by_local key del failed");
1379 return VNET_API_ERROR_UNSPECIFIED;
1383 if (sm->num_workers > 1)
1386 .src_address = local->addr,
1388 tsm = vec_elt_at_index (sm->per_thread_data,
1389 sm->worker_in2out_cb (&ip, m->fib_index, 0));
1392 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1394 /* Delete sessions */
1395 pool_foreach (s, tsm->sessions) {
1396 if (!(is_lb_session (s)))
1399 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1400 s->in2out.port != local->port)
1403 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1404 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1409 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1410 pool_free (m->locals);
1412 vec_free (m->workers);
1414 pool_put (sm->static_mappings, m);
1421 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1422 ip4_address_t l_addr, u16 l_port,
1423 nat_protocol_t proto, u32 vrf_id,
1424 u8 probability, u8 is_add)
1426 snat_main_t *sm = &snat_main;
1427 snat_static_mapping_t *m = 0;
1428 clib_bihash_kv_8_8_t kv, value;
1429 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1430 snat_main_per_thread_data_t *tsm;
1436 if (!sm->endpoint_dependent)
1437 return VNET_API_ERROR_FEATURE_DISABLED;
1439 init_nat_k (&kv, e_addr, e_port, 0, proto);
1440 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1441 m = pool_elt_at_index (sm->static_mappings, value.value);
1444 return VNET_API_ERROR_NO_SUCH_ENTRY;
1446 if (!is_lb_static_mapping (m))
1447 return VNET_API_ERROR_INVALID_VALUE;
1450 pool_foreach (local, m->locals)
1452 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1453 (local->vrf_id == vrf_id))
1455 match_local = local;
1464 return VNET_API_ERROR_VALUE_EXIST;
1466 pool_get (m->locals, local);
1467 clib_memset (local, 0, sizeof (*local));
1468 local->addr.as_u32 = l_addr.as_u32;
1469 local->port = l_port;
1470 local->probability = probability;
1471 local->vrf_id = vrf_id;
1473 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1476 if (!is_out2in_only_static_mapping (m))
1478 init_nat_kv (&kv, l_addr, l_port, local->fib_index, proto,
1479 m - sm->static_mappings);
1480 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1481 nat_elog_err ("static_mapping_by_local key add failed");
1487 return VNET_API_ERROR_NO_SUCH_ENTRY;
1489 if (pool_elts (m->locals) < 3)
1490 return VNET_API_ERROR_UNSPECIFIED;
1492 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1495 if (!is_out2in_only_static_mapping (m))
1497 init_nat_k (&kv, l_addr, l_port, match_local->fib_index, proto);
1498 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1499 nat_elog_err ("static_mapping_by_local key del failed");
1502 if (sm->num_workers > 1)
1505 .src_address = local->addr,
1507 tsm = vec_elt_at_index (sm->per_thread_data,
1508 sm->worker_in2out_cb (&ip, m->fib_index,
1512 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1514 /* Delete sessions */
1516 pool_foreach (s, tsm->sessions) {
1517 if (!(is_lb_session (s)))
1520 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1521 s->in2out.port != match_local->port)
1524 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1525 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1529 pool_put (m->locals, match_local);
1532 vec_free (m->workers);
1535 pool_foreach (local, m->locals)
1537 vec_add1 (locals, local - m->locals);
1538 if (sm->num_workers > 1)
1541 ip.src_address.as_u32 = local->addr.as_u32,
1542 bitmap = clib_bitmap_set (bitmap,
1543 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1549 ASSERT (vec_len (locals) > 1);
1551 local = pool_elt_at_index (m->locals, locals[0]);
1552 local->prefix = local->probability;
1553 for (i = 1; i < vec_len (locals); i++)
1555 local = pool_elt_at_index (m->locals, locals[i]);
1556 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1557 local->prefix = local->probability + prev_local->prefix;
1560 /* Assign workers */
1561 if (sm->num_workers > 1)
1564 clib_bitmap_foreach (i, bitmap) { vec_add1(m->workers, i); }
1572 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1575 snat_address_t *a = 0;
1576 snat_session_t *ses;
1577 u32 *ses_to_be_removed = 0, *ses_index;
1578 snat_main_per_thread_data_t *tsm;
1579 snat_static_mapping_t *m;
1580 snat_interface_t *interface;
1582 snat_address_t *addresses =
1583 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1585 /* Find SNAT address */
1586 for (i = 0; i < vec_len (addresses); i++)
1588 if (addresses[i].addr.as_u32 == addr.as_u32)
1596 nat_log_err ("no such address");
1597 return VNET_API_ERROR_NO_SUCH_ENTRY;
1602 ip4_address_t pool_addr = { 0 };
1604 pool_foreach (m, sm->static_mappings)
1606 if (m->external_addr.as_u32 == addr.as_u32)
1607 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1608 m->local_port, m->external_port,
1610 is_addr_only_static_mapping(m), ~0,
1611 m->proto, 0 /* is_add */,
1613 is_out2in_only_static_mapping(m),
1615 is_identity_static_mapping(m),
1622 /* Check if address is used in some static mapping */
1623 if (is_snat_address_used_in_static_mapping (sm, addr))
1625 nat_log_err ("address used in static mapping");
1626 return VNET_API_ERROR_UNSPECIFIED;
1630 if (a->fib_index != ~0)
1631 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low);
1633 /* Delete sessions using address */
1634 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1636 vec_foreach (tsm, sm->per_thread_data)
1639 pool_foreach (ses, tsm->sessions) {
1640 if (ses->out2in.addr.as_u32 == addr.as_u32)
1642 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1643 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1648 if (sm->endpoint_dependent)
1650 vec_foreach (ses_index, ses_to_be_removed)
1652 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1653 nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
1658 vec_foreach (ses_index, ses_to_be_removed)
1660 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1661 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1665 vec_free (ses_to_be_removed);
1669 #define _(N, i, n, s) \
1670 vec_free (a->busy_##n##_ports_per_thread);
1671 foreach_nat_protocol
1675 vec_del1 (sm->twice_nat_addresses, i);
1679 vec_del1 (sm->addresses, i);
1681 /* Delete external address from FIB */
1683 pool_foreach (interface, sm->interfaces)
1685 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1688 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1691 pool_foreach (interface, sm->output_feature_interfaces)
1693 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1696 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1705 nat_validate_counters (snat_main_t * sm, u32 sw_if_index)
1708 vlib_validate_simple_counter (&sm->counters.fastpath.in2out.x, \
1710 vlib_zero_simple_counter (&sm->counters.fastpath.in2out.x, sw_if_index); \
1711 vlib_validate_simple_counter (&sm->counters.fastpath.out2in.x, \
1713 vlib_zero_simple_counter (&sm->counters.fastpath.out2in.x, sw_if_index); \
1714 vlib_validate_simple_counter (&sm->counters.slowpath.in2out.x, \
1716 vlib_zero_simple_counter (&sm->counters.slowpath.in2out.x, sw_if_index); \
1717 vlib_validate_simple_counter (&sm->counters.slowpath.out2in.x, \
1719 vlib_zero_simple_counter (&sm->counters.slowpath.out2in.x, sw_if_index); \
1720 vlib_validate_simple_counter (&sm->counters.fastpath.in2out_ed.x, \
1722 vlib_zero_simple_counter (&sm->counters.fastpath.in2out_ed.x, sw_if_index); \
1723 vlib_validate_simple_counter (&sm->counters.fastpath.out2in_ed.x, \
1725 vlib_zero_simple_counter (&sm->counters.fastpath.out2in_ed.x, sw_if_index); \
1726 vlib_validate_simple_counter (&sm->counters.slowpath.in2out_ed.x, \
1728 vlib_zero_simple_counter (&sm->counters.slowpath.in2out_ed.x, sw_if_index); \
1729 vlib_validate_simple_counter (&sm->counters.slowpath.out2in_ed.x, \
1731 vlib_zero_simple_counter (&sm->counters.slowpath.out2in_ed.x, sw_if_index);
1732 foreach_nat_counter;
1734 vlib_validate_simple_counter (&sm->counters.hairpinning, sw_if_index);
1735 vlib_zero_simple_counter (&sm->counters.hairpinning, sw_if_index);
1739 expire_per_vrf_sessions (u32 fib_index)
1741 per_vrf_sessions_t *per_vrf_sessions;
1742 snat_main_per_thread_data_t *tsm;
1743 snat_main_t *sm = &snat_main;
1746 vec_foreach (tsm, sm->per_thread_data)
1748 vec_foreach (per_vrf_sessions, tsm->per_vrf_sessions_vec)
1750 if ((per_vrf_sessions->rx_fib_index == fib_index) ||
1751 (per_vrf_sessions->tx_fib_index == fib_index))
1753 per_vrf_sessions->expired = 1;
1761 update_per_vrf_sessions_vec (u32 fib_index, int is_del)
1763 snat_main_t *sm = &snat_main;
1766 // we don't care if it is outside/inside fib
1767 // we just care about their ref_count
1768 // if it reaches 0 sessions should expire
1769 // because the fib isn't valid for NAT anymore
1771 vec_foreach (fib, sm->fibs)
1773 if (fib->fib_index == fib_index)
1778 if (!fib->ref_count)
1780 vec_del1 (sm->fibs, fib - sm->fibs);
1781 expire_per_vrf_sessions (fib_index);
1791 vec_add2 (sm->fibs, fib, 1);
1793 fib->fib_index = fib_index;
1798 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1800 snat_main_t *sm = &snat_main;
1801 snat_interface_t *i;
1802 const char *feature_name, *del_feature_name;
1804 snat_static_mapping_t *m;
1805 nat_outside_fib_t *outside_fib;
1806 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1811 nat_log_err ("nat44 is disabled");
1812 return VNET_API_ERROR_UNSUPPORTED;
1815 if (sm->out2in_dpo && !is_inside)
1817 nat_log_err ("error unsupported");
1818 return VNET_API_ERROR_UNSUPPORTED;
1822 pool_foreach (i, sm->output_feature_interfaces)
1824 if (i->sw_if_index == sw_if_index)
1826 nat_log_err ("error interface already configured");
1827 return VNET_API_ERROR_VALUE_EXIST;
1832 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1833 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1836 if (sm->num_workers > 1)
1838 is_inside ? "nat44-in2out-worker-handoff" :
1839 "nat44-out2in-worker-handoff";
1840 else if (sm->endpoint_dependent)
1842 feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1845 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1848 if (sm->fq_in2out_index == ~0 && sm->num_workers > 1)
1849 sm->fq_in2out_index =
1850 vlib_frame_queue_main_init (sm->in2out_node_index, NAT_FQ_NELTS);
1852 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1853 sm->fq_out2in_index =
1854 vlib_frame_queue_main_init (sm->out2in_node_index, NAT_FQ_NELTS);
1856 if (sm->endpoint_dependent)
1857 update_per_vrf_sessions_vec (fib_index, is_del);
1862 vec_foreach (outside_fib, sm->outside_fibs)
1864 if (outside_fib->fib_index == fib_index)
1868 outside_fib->refcount--;
1869 if (!outside_fib->refcount)
1870 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1873 outside_fib->refcount++;
1880 vec_add2 (sm->outside_fibs, outside_fib, 1);
1881 outside_fib->refcount = 1;
1882 outside_fib->fib_index = fib_index;
1888 pool_foreach (i, sm->interfaces)
1890 if (i->sw_if_index == sw_if_index)
1894 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1897 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1899 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1901 if (sm->num_workers > 1)
1903 del_feature_name = "nat44-handoff-classify";
1904 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1905 "nat44-out2in-worker-handoff";
1907 else if (sm->endpoint_dependent)
1909 del_feature_name = "nat44-ed-classify";
1910 feature_name = !is_inside ? "nat-pre-in2out" :
1915 del_feature_name = "nat44-classify";
1916 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1919 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1922 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1923 sw_if_index, 0, 0, 0);
1924 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1925 sw_if_index, 1, 0, 0);
1928 if (sm->endpoint_dependent)
1929 vnet_feature_enable_disable ("ip4-local",
1930 "nat44-ed-hairpinning",
1931 sw_if_index, 1, 0, 0);
1933 vnet_feature_enable_disable ("ip4-local",
1934 "nat44-hairpinning",
1935 sw_if_index, 1, 0, 0);
1940 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1943 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1944 sw_if_index, 0, 0, 0);
1945 pool_put (sm->interfaces, i);
1948 if (sm->endpoint_dependent)
1949 vnet_feature_enable_disable ("ip4-local",
1950 "nat44-ed-hairpinning",
1951 sw_if_index, 0, 0, 0);
1953 vnet_feature_enable_disable ("ip4-local",
1954 "nat44-hairpinning",
1955 sw_if_index, 0, 0, 0);
1961 if ((nat_interface_is_inside(i) && is_inside) ||
1962 (nat_interface_is_outside(i) && !is_inside))
1965 if (sm->num_workers > 1)
1967 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1968 "nat44-out2in-worker-handoff";
1969 feature_name = "nat44-handoff-classify";
1971 else if (sm->endpoint_dependent)
1973 del_feature_name = !is_inside ? "nat-pre-in2out" :
1976 feature_name = "nat44-ed-classify";
1980 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1981 feature_name = "nat44-classify";
1984 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1987 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1988 sw_if_index, 0, 0, 0);
1989 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1990 sw_if_index, 1, 0, 0);
1993 if (sm->endpoint_dependent)
1994 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1995 sw_if_index, 0, 0, 0);
1997 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1998 sw_if_index, 0, 0, 0);
2010 nat_log_err ("error interface couldn't be found");
2011 return VNET_API_ERROR_NO_SUCH_ENTRY;
2014 pool_get (sm->interfaces, i);
2015 i->sw_if_index = sw_if_index;
2017 nat_validate_counters (sm, sw_if_index);
2019 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
2022 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2026 if (is_inside && !sm->out2in_dpo)
2028 if (sm->endpoint_dependent)
2029 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2030 sw_if_index, 1, 0, 0);
2032 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2033 sw_if_index, 1, 0, 0);
2039 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2043 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2045 /* Add/delete external addresses to FIB */
2048 vec_foreach (ap, sm->addresses)
2049 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2051 pool_foreach (m, sm->static_mappings)
2053 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2056 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2064 snat_interface_add_del_output_feature (u32 sw_if_index,
2065 u8 is_inside, int is_del)
2067 snat_main_t *sm = &snat_main;
2068 snat_interface_t *i;
2070 snat_static_mapping_t *m;
2071 nat_outside_fib_t *outside_fib;
2072 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2077 nat_log_err ("nat44 is disabled");
2078 return VNET_API_ERROR_UNSUPPORTED;
2081 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2083 nat_log_err ("error unsupported");
2084 return VNET_API_ERROR_UNSUPPORTED;
2088 pool_foreach (i, sm->interfaces)
2090 if (i->sw_if_index == sw_if_index)
2092 nat_log_err ("error interface already configured");
2093 return VNET_API_ERROR_VALUE_EXIST;
2098 if (sm->endpoint_dependent)
2099 update_per_vrf_sessions_vec (fib_index, is_del);
2104 vec_foreach (outside_fib, sm->outside_fibs)
2106 if (outside_fib->fib_index == fib_index)
2110 outside_fib->refcount--;
2111 if (!outside_fib->refcount)
2112 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2115 outside_fib->refcount++;
2122 vec_add2 (sm->outside_fibs, outside_fib, 1);
2123 outside_fib->refcount = 1;
2124 outside_fib->fib_index = fib_index;
2131 if (sm->endpoint_dependent)
2134 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2138 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2142 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2143 sw_if_index, !is_del, 0, 0);
2144 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2145 sw_if_index, !is_del, 0, 0);
2150 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2154 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2158 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2159 sw_if_index, !is_del, 0, 0);
2160 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2161 sw_if_index, !is_del, 0, 0);
2166 if (sm->num_workers > 1)
2168 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2172 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
2175 vnet_feature_enable_disable ("ip4-unicast",
2176 "nat44-out2in-worker-handoff",
2177 sw_if_index, !is_del, 0, 0);
2178 vnet_feature_enable_disable ("ip4-output",
2179 "nat44-in2out-output-worker-handoff",
2180 sw_if_index, !is_del, 0, 0);
2184 if (sm->endpoint_dependent)
2187 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2191 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2195 vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
2196 sw_if_index, !is_del, 0, 0);
2197 vnet_feature_enable_disable ("ip4-output", "nat-pre-in2out-output",
2198 sw_if_index, !is_del, 0, 0);
2203 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2207 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2211 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2212 sw_if_index, !is_del, 0, 0);
2213 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2214 sw_if_index, !is_del, 0, 0);
2219 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2220 sm->fq_in2out_output_index =
2221 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2223 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2224 sm->fq_out2in_index =
2225 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2228 pool_foreach (i, sm->output_feature_interfaces)
2230 if (i->sw_if_index == sw_if_index)
2233 pool_put (sm->output_feature_interfaces, i);
2235 return VNET_API_ERROR_VALUE_EXIST;
2244 nat_log_err ("error interface couldn't be found");
2245 return VNET_API_ERROR_NO_SUCH_ENTRY;
2248 pool_get (sm->output_feature_interfaces, i);
2249 i->sw_if_index = sw_if_index;
2251 nat_validate_counters (sm, sw_if_index);
2253 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2255 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2257 /* Add/delete external addresses to FIB */
2263 vec_foreach (ap, sm->addresses)
2264 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2266 pool_foreach (m, sm->static_mappings)
2268 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2271 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2279 snat_set_workers (uword * bitmap)
2281 snat_main_t *sm = &snat_main;
2284 if (sm->num_workers < 2)
2285 return VNET_API_ERROR_FEATURE_DISABLED;
2287 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2288 return VNET_API_ERROR_INVALID_WORKER;
2290 vec_free (sm->workers);
2292 clib_bitmap_foreach (i, bitmap)
2294 vec_add1(sm->workers, i);
2295 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2296 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2301 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2307 snat_update_outside_fib (ip4_main_t * im, uword opaque,
2308 u32 sw_if_index, u32 new_fib_index,
2311 snat_main_t *sm = &snat_main;
2312 nat_outside_fib_t *outside_fib;
2313 snat_interface_t *i;
2317 if (!sm->enabled || (new_fib_index == old_fib_index)
2318 || (!vec_len (sm->outside_fibs)))
2324 pool_foreach (i, sm->interfaces)
2326 if (i->sw_if_index == sw_if_index)
2328 if (!(nat_interface_is_outside (i)))
2334 pool_foreach (i, sm->output_feature_interfaces)
2336 if (i->sw_if_index == sw_if_index)
2338 if (!(nat_interface_is_outside (i)))
2348 vec_foreach (outside_fib, sm->outside_fibs)
2350 if (outside_fib->fib_index == old_fib_index)
2352 outside_fib->refcount--;
2353 if (!outside_fib->refcount)
2354 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2359 vec_foreach (outside_fib, sm->outside_fibs)
2361 if (outside_fib->fib_index == new_fib_index)
2363 outside_fib->refcount++;
2371 vec_add2 (sm->outside_fibs, outside_fib, 1);
2372 outside_fib->refcount = 1;
2373 outside_fib->fib_index = new_fib_index;
2378 snat_update_outside_fib (ip4_main_t * im, uword opaque,
2379 u32 sw_if_index, u32 new_fib_index,
2383 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2386 ip4_address_t * address,
2388 u32 if_address_index, u32 is_delete);
2391 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2394 ip4_address_t * address,
2396 u32 if_address_index, u32 is_delete);
2399 test_key_calc_split ()
2401 ip4_address_t l_addr;
2402 l_addr.as_u8[0] = 1;
2403 l_addr.as_u8[1] = 1;
2404 l_addr.as_u8[2] = 1;
2405 l_addr.as_u8[3] = 1;
2406 ip4_address_t r_addr;
2407 r_addr.as_u8[0] = 2;
2408 r_addr.as_u8[1] = 2;
2409 r_addr.as_u8[2] = 2;
2410 r_addr.as_u8[3] = 2;
2414 u32 fib_index = 9000001;
2415 u32 thread_index = 3000000001;
2416 u32 session_index = 3000000221;
2417 clib_bihash_kv_16_8_t kv;
2418 init_ed_kv (&kv, l_addr, l_port, r_addr, r_port, fib_index, proto,
2419 thread_index, session_index);
2420 ip4_address_t l_addr2;
2421 ip4_address_t r_addr2;
2422 clib_memset (&l_addr2, 0, sizeof (l_addr2));
2423 clib_memset (&r_addr2, 0, sizeof (r_addr2));
2428 split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
2430 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2431 ASSERT (r_addr.as_u32 == r_addr2.as_u32);
2432 ASSERT (l_port == l_port2);
2433 ASSERT (r_port == r_port2);
2434 ASSERT (proto == proto2);
2435 ASSERT (fib_index == fib_index2);
2436 ASSERT (thread_index == ed_value_get_thread_index (&kv));
2437 ASSERT (session_index == ed_value_get_session_index (&kv));
2441 nat_protocol_t proto3 = ~0;
2442 u64 key = calc_nat_key (l_addr, l_port, fib_index, proto);
2443 split_nat_key (key, &l_addr2, &l_port2, &fib_index2, &proto3);
2444 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2445 ASSERT (l_port == l_port2);
2446 ASSERT (proto == proto3);
2447 ASSERT (fib_index == fib_index2);
2450 static clib_error_t *
2451 nat_ip_table_add_del (vnet_main_t * vnm, u32 table_id, u32 is_add)
2453 snat_main_t *sm = &snat_main;
2456 if (sm->endpoint_dependent)
2458 // TODO: consider removing all NAT interfaces
2461 fib_index = ip4_fib_index_from_table_id (table_id);
2462 if (fib_index != ~0)
2463 expire_per_vrf_sessions (fib_index);
2469 VNET_IP_TABLE_ADD_DEL_FUNCTION (nat_ip_table_add_del);
2472 nat44_set_node_indexes (snat_main_t * sm, vlib_main_t * vm)
2476 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2477 sm->ei_out2in_node_index = node->index;
2478 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2479 sm->ei_in2out_node_index = node->index;
2480 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2481 sm->ei_in2out_output_node_index = node->index;
2483 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2484 sm->ed_out2in_node_index = node->index;
2485 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2486 sm->ed_in2out_node_index = node->index;
2487 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-output");
2488 sm->ed_in2out_output_node_index = node->index;
2490 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2491 sm->error_node_index = node->index;
2492 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2493 sm->pre_in2out_node_index = node->index;
2494 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2495 sm->pre_out2in_node_index = node->index;
2496 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2497 sm->pre_in2out_node_index = node->index;
2498 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2499 sm->pre_out2in_node_index = node->index;
2500 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2501 sm->in2out_fast_node_index = node->index;
2502 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2503 sm->in2out_slowpath_node_index = node->index;
2504 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2505 sm->in2out_slowpath_output_node_index = node->index;
2506 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2507 sm->ed_in2out_slowpath_node_index = node->index;
2508 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2509 sm->out2in_fast_node_index = node->index;
2510 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2511 sm->ed_out2in_slowpath_node_index = node->index;
2512 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2513 sm->hairpinning_node_index = node->index;
2514 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2515 sm->hairpin_dst_node_index = node->index;
2516 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2517 sm->hairpin_src_node_index = node->index;
2518 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpinning");
2519 sm->ed_hairpinning_node_index = node->index;
2520 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-dst");
2521 sm->ed_hairpin_dst_node_index = node->index;
2522 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-src");
2523 sm->ed_hairpin_src_node_index = node->index;
2526 #define nat_init_simple_counter(c, n, sn) \
2530 c.stat_segment_name = sn; \
2531 vlib_validate_simple_counter (&c, 0); \
2532 vlib_zero_simple_counter (&c, 0); \
2535 static clib_error_t *
2536 nat_init (vlib_main_t * vm)
2538 snat_main_t *sm = &snat_main;
2539 vlib_thread_main_t *tm = vlib_get_thread_main ();
2540 vlib_thread_registration_t *tr;
2541 ip4_add_del_interface_address_callback_t cbi = { 0 };
2542 ip4_table_bind_callback_t cbt = { 0 };
2543 u32 i, num_threads = 0;
2544 uword *p, *bitmap = 0;
2546 clib_memset (sm, 0, sizeof (*sm));
2549 sm->vnet_main = vnet_get_main ();
2551 sm->ip4_main = &ip4_main;
2552 sm->api_main = vlibapi_get_main ();
2553 sm->ip4_lookup_main = &ip4_main.lookup_main;
2555 // frame queue indices used for handoff
2556 sm->fq_out2in_index = ~0;
2557 sm->fq_in2out_index = ~0;
2558 sm->fq_in2out_output_index = ~0;
2560 sm->log_level = SNAT_LOG_ERROR;
2562 nat44_set_node_indexes (sm, vm);
2563 sm->log_class = vlib_log_register_class ("nat", 0);
2564 nat_ipfix_logging_init (vm);
2566 nat_init_simple_counter (sm->total_users, "total-users",
2567 "/nat44/total-users");
2568 nat_init_simple_counter (sm->total_sessions, "total-sessions",
2569 "/nat44/total-sessions");
2570 nat_init_simple_counter (sm->user_limit_reached, "user-limit-reached",
2571 "/nat44/user-limit-reached");
2574 sm->counters.fastpath.in2out.x.name = #x; \
2575 sm->counters.fastpath.in2out.x.stat_segment_name = \
2576 "/nat44/in2out/fastpath/" #x; \
2577 sm->counters.slowpath.in2out.x.name = #x; \
2578 sm->counters.slowpath.in2out.x.stat_segment_name = \
2579 "/nat44/in2out/slowpath/" #x; \
2580 sm->counters.fastpath.out2in.x.name = #x; \
2581 sm->counters.fastpath.out2in.x.stat_segment_name = \
2582 "/nat44/out2in/fastpath/" #x; \
2583 sm->counters.slowpath.out2in.x.name = #x; \
2584 sm->counters.slowpath.out2in.x.stat_segment_name = \
2585 "/nat44/out2in/slowpath/" #x; \
2586 sm->counters.fastpath.in2out_ed.x.name = #x; \
2587 sm->counters.fastpath.in2out_ed.x.stat_segment_name = \
2588 "/nat44/ed/in2out/fastpath/" #x; \
2589 sm->counters.slowpath.in2out_ed.x.name = #x; \
2590 sm->counters.slowpath.in2out_ed.x.stat_segment_name = \
2591 "/nat44/ed/in2out/slowpath/" #x; \
2592 sm->counters.fastpath.out2in_ed.x.name = #x; \
2593 sm->counters.fastpath.out2in_ed.x.stat_segment_name = \
2594 "/nat44/ed/out2in/fastpath/" #x; \
2595 sm->counters.slowpath.out2in_ed.x.name = #x; \
2596 sm->counters.slowpath.out2in_ed.x.stat_segment_name = \
2597 "/nat44/ed/out2in/slowpath/" #x;
2598 foreach_nat_counter;
2600 sm->counters.hairpinning.name = "hairpinning";
2601 sm->counters.hairpinning.stat_segment_name = "/nat44/hairpinning";
2603 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2606 tr = (vlib_thread_registration_t *) p[0];
2609 sm->num_workers = tr->count;
2610 sm->first_worker_index = tr->first_index;
2613 num_threads = tm->n_vlib_mains - 1;
2614 sm->port_per_thread = 0xffff - 1024;
2615 vec_validate (sm->per_thread_data, num_threads);
2617 /* Use all available workers by default */
2618 if (sm->num_workers > 1)
2621 for (i = 0; i < sm->num_workers; i++)
2622 bitmap = clib_bitmap_set (bitmap, i, 1);
2623 snat_set_workers (bitmap);
2624 clib_bitmap_free (bitmap);
2627 sm->per_thread_data[0].snat_thread_index = 0;
2629 /* callbacks to call when interface address changes. */
2630 cbi.function = snat_ip4_add_del_interface_address_cb;
2631 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2632 cbi.function = nat_ip4_add_del_addr_only_sm_cb;
2633 vec_add1 (sm->ip4_main->add_del_interface_address_callbacks, cbi);
2635 /* callbacks to call when interface to table biding changes */
2636 cbt.function = snat_update_outside_fib;
2637 vec_add1 (sm->ip4_main->table_bind_callbacks, cbt);
2640 fib_source_allocate ("nat-low", FIB_SOURCE_PRIORITY_LOW,
2641 FIB_SOURCE_BH_SIMPLE);
2643 fib_source_allocate ("nat-hi", FIB_SOURCE_PRIORITY_HI,
2644 FIB_SOURCE_BH_SIMPLE);
2646 /* used only by out2in-dpo feature */
2647 nat_dpo_module_init ();
2649 nat_affinity_init (vm);
2650 nat_ha_init (vm, sm->num_workers, num_threads);
2652 test_key_calc_split ();
2653 return nat44_api_hookup (vm);
2656 VLIB_INIT_FUNCTION (nat_init);
2659 nat44_plugin_enable (nat44_config_t c)
2661 snat_main_t *sm = &snat_main;
2662 u32 static_mapping_buckets = 1024;
2663 u32 static_mapping_memory_size = 64 << 20;
2667 nat_log_err ("nat44 is enabled");
2671 // c.static_mapping_only + c.connection_tracking
2672 // - supported in NAT EI & NAT ED
2673 // c.out2in_dpo, c.static_mapping_only
2674 // - supported in NAT EI
2676 if (c.endpoint_dependent)
2678 if ((c.static_mapping_only && !c.connection_tracking) || c.out2in_dpo)
2680 nat_log_err ("unsupported combination of configuration");
2683 if (c.users || c.user_sessions)
2685 nat_log_err ("unsupported combination of configuration");
2690 nat_reset_timeouts (&sm->timeouts);
2692 // nat44 feature configuration
2693 sm->endpoint_dependent = c.endpoint_dependent;
2694 sm->static_mapping_only = c.static_mapping_only;
2695 sm->static_mapping_connection_tracking = c.connection_tracking;
2696 sm->out2in_dpo = c.out2in_dpo;
2697 sm->forwarding_enabled = 0;
2698 sm->mss_clamping = 0;
2703 sm->max_users_per_thread = c.users;
2704 sm->user_buckets = nat_calc_bihash_buckets (c.users);
2707 c.sessions = 10 * 1024;
2709 sm->max_translations_per_thread = c.sessions;
2710 sm->translation_buckets = nat_calc_bihash_buckets (c.sessions);
2712 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
2713 sm->max_translations_per_user
2714 = c.user_sessions ? c.user_sessions : sm->max_translations_per_thread;
2716 sm->outside_vrf_id = c.outside_vrf;
2717 sm->outside_fib_index =
2718 fib_table_find_or_create_and_lock
2719 (FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi);
2721 sm->inside_vrf_id = c.inside_vrf;
2722 sm->inside_fib_index =
2723 fib_table_find_or_create_and_lock
2724 (FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi);
2726 if (c.endpoint_dependent)
2728 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2729 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
2730 sm->out2in_node_index = sm->ed_out2in_node_index;
2731 sm->in2out_node_index = sm->ed_in2out_node_index;
2732 sm->in2out_output_node_index = sm->ed_in2out_output_node_index;
2733 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
2734 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
2736 // try to move it into nat44_db_init,
2737 // consider static mapping requirements
2738 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2739 sm->translation_buckets, 0);
2740 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
2741 format_ed_session_kvp);
2743 nat_affinity_enable ();
2747 sm->worker_out2in_cb = nat44_ei_get_out2in_worker_index;
2748 sm->worker_in2out_cb = nat44_ei_get_in2out_worker_index;
2750 sm->out2in_node_index = sm->ei_out2in_node_index;
2751 sm->in2out_node_index = sm->ei_in2out_node_index;
2752 sm->in2out_output_node_index = sm->ei_in2out_output_node_index;
2753 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2754 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2756 nat44_ei_plugin_enable ();
2759 // c.static_mapping & c.connection_tracking require
2761 if (!c.static_mapping_only
2762 || (c.static_mapping_only && c.connection_tracking))
2764 snat_main_per_thread_data_t *tsm;
2766 vec_foreach (tsm, sm->per_thread_data)
2768 nat44_db_init (tsm);
2774 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2775 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2778 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2779 "static_mapping_by_local", static_mapping_buckets,
2780 static_mapping_memory_size);
2781 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2782 format_static_mapping_kvp);
2784 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2785 "static_mapping_by_external",
2786 static_mapping_buckets, static_mapping_memory_size);
2787 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2788 format_static_mapping_kvp);
2790 // last: reset counters
2791 vlib_zero_simple_counter (&sm->total_users, 0);
2792 vlib_zero_simple_counter (&sm->total_sessions, 0);
2793 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
2802 nat44_addresses_free (snat_address_t ** addresses)
2806 vec_foreach (ap, *addresses)
2808 #define _(N, i, n, s) \
2809 vec_free (ap->busy_##n##_ports_per_thread);
2810 foreach_nat_protocol
2814 vec_free (*addresses);
2819 nat44_plugin_disable ()
2821 snat_main_t *sm = &snat_main;
2822 snat_interface_t *i, *vec;
2827 nat_log_err ("nat44 is disabled");
2831 if (!sm->endpoint_dependent)
2833 nat44_ei_plugin_disable ();
2836 // first unregister all nodes from interfaces
2837 vec = vec_dup (sm->interfaces);
2839 vec_foreach (i, vec)
2841 if (nat_interface_is_inside(i))
2842 error = snat_interface_add_del (i->sw_if_index, 1, 1);
2843 if (nat_interface_is_outside(i))
2844 error = snat_interface_add_del (i->sw_if_index, 0, 1);
2848 nat_log_err ("error occurred while removing interface %u",
2856 vec = vec_dup (sm->output_feature_interfaces);
2858 vec_foreach (i, vec)
2860 if (nat_interface_is_inside(i))
2861 error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1);
2862 if (nat_interface_is_outside(i))
2863 error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1);
2867 nat_log_err ("error occurred while removing interface %u",
2873 sm->output_feature_interfaces = 0;
2875 vec_free (sm->max_translations_per_fib);
2877 if (sm->endpoint_dependent)
2879 nat_affinity_disable ();
2880 clib_bihash_free_16_8 (&sm->out2in_ed);
2883 clib_bihash_free_8_8 (&sm->static_mapping_by_local);
2884 clib_bihash_free_8_8 (&sm->static_mapping_by_external);
2886 if (!sm->static_mapping_only ||
2887 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
2889 snat_main_per_thread_data_t *tsm;
2891 vec_foreach (tsm, sm->per_thread_data)
2893 nat44_db_free (tsm);
2898 pool_free (sm->static_mappings);
2900 nat44_addresses_free (&sm->addresses);
2901 nat44_addresses_free (&sm->twice_nat_addresses);
2904 vec_free (sm->to_resolve);
2905 vec_free (sm->auto_add_sw_if_indices);
2906 vec_free (sm->auto_add_sw_if_indices_twice_nat);
2909 sm->auto_add_sw_if_indices = 0;
2910 sm->auto_add_sw_if_indices_twice_nat = 0;
2912 sm->forwarding_enabled = 0;
2915 clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig));
2921 snat_free_outside_address_and_port (snat_address_t * addresses,
2923 ip4_address_t * addr,
2924 u16 port, nat_protocol_t protocol)
2928 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2930 for (address_index = 0; address_index < vec_len (addresses);
2933 if (addresses[address_index].addr.as_u32 == addr->as_u32)
2937 ASSERT (address_index < vec_len (addresses));
2939 a = addresses + address_index;
2943 #define _(N, i, n, s) \
2944 case NAT_PROTOCOL_##N: \
2945 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
2946 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
2947 a->busy_##n##_ports--; \
2948 a->busy_##n##_ports_per_thread[thread_index]--; \
2950 foreach_nat_protocol
2953 nat_elog_info ("unknown protocol");
2959 nat_set_outside_address_and_port (snat_address_t *addresses, u32 thread_index,
2960 ip4_address_t addr, u16 port,
2961 nat_protocol_t protocol)
2963 snat_address_t *a = 0;
2965 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2967 for (address_index = 0; address_index < vec_len (addresses);
2970 if (addresses[address_index].addr.as_u32 != addr.as_u32)
2973 a = addresses + address_index;
2976 #define _(N, j, n, s) \
2977 case NAT_PROTOCOL_##N: \
2978 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
2979 return VNET_API_ERROR_INSTANCE_IN_USE; \
2980 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
2981 a->busy_##n##_ports_per_thread[thread_index]++; \
2982 a->busy_##n##_ports++; \
2984 foreach_nat_protocol
2987 nat_elog_info ("unknown protocol");
2992 return VNET_API_ERROR_NO_SUCH_ENTRY;
2996 snat_static_mapping_match (snat_main_t * sm,
2997 ip4_address_t match_addr,
2999 u32 match_fib_index,
3000 nat_protocol_t match_protocol,
3001 ip4_address_t * mapping_addr,
3003 u32 * mapping_fib_index,
3006 twice_nat_type_t * twice_nat,
3007 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
3008 u8 * is_identity_nat, snat_static_mapping_t ** out)
3010 clib_bihash_kv_8_8_t kv, value;
3011 clib_bihash_8_8_t *mapping_hash;
3012 snat_static_mapping_t *m;
3013 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
3014 nat44_lb_addr_port_t *local;
3019 mapping_hash = &sm->static_mapping_by_local;
3020 init_nat_k (&kv, match_addr, match_port, match_fib_index,
3022 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3024 /* Try address only mapping */
3025 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
3026 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3032 mapping_hash = &sm->static_mapping_by_external;
3033 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
3034 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3036 /* Try address only mapping */
3037 init_nat_k (&kv, match_addr, 0, 0, 0);
3038 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
3043 m = pool_elt_at_index (sm->static_mappings, value.value);
3047 if (is_lb_static_mapping (m))
3049 if (PREDICT_FALSE (lb != 0))
3050 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
3051 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
3057 local = pool_elt_at_index (m->locals, backend_index);
3058 *mapping_addr = local->addr;
3059 *mapping_port = local->port;
3060 *mapping_fib_index = local->fib_index;
3063 // pick locals matching this worker
3064 if (PREDICT_FALSE (sm->num_workers > 1))
3066 u32 thread_index = vlib_get_thread_index ();
3068 pool_foreach_index (i, m->locals)
3070 local = pool_elt_at_index (m->locals, i);
3073 .src_address = local->addr,
3076 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
3083 ASSERT (vec_len (tmp) != 0);
3088 pool_foreach_index (i, m->locals)
3094 hi = vec_len (tmp) - 1;
3095 local = pool_elt_at_index (m->locals, tmp[hi]);
3096 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
3099 mid = ((hi - lo) >> 1) + lo;
3100 local = pool_elt_at_index (m->locals, tmp[mid]);
3101 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
3103 local = pool_elt_at_index (m->locals, tmp[lo]);
3104 if (!(local->prefix >= rand))
3106 *mapping_addr = local->addr;
3107 *mapping_port = local->port;
3108 *mapping_fib_index = local->fib_index;
3111 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
3112 match_protocol, match_port,
3113 tmp[lo], m->affinity,
3114 m->affinity_per_service_list_head_index))
3115 nat_elog_info ("create affinity record failed");
3121 if (PREDICT_FALSE (lb != 0))
3123 *mapping_fib_index = m->fib_index;
3124 *mapping_addr = m->local_addr;
3125 /* Address only mapping doesn't change port */
3126 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3132 *mapping_addr = m->external_addr;
3133 /* Address only mapping doesn't change port */
3134 *mapping_port = is_addr_only_static_mapping (m) ? match_port
3136 *mapping_fib_index = sm->outside_fib_index;
3140 if (PREDICT_FALSE (is_addr_only != 0))
3141 *is_addr_only = is_addr_only_static_mapping (m);
3143 if (PREDICT_FALSE (twice_nat != 0))
3144 *twice_nat = m->twice_nat;
3146 if (PREDICT_FALSE (is_identity_nat != 0))
3147 *is_identity_nat = is_identity_static_mapping (m);
3156 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3158 snat_main_t *sm = &snat_main;
3159 dpo_id_t dpo_v4 = DPO_INVALID;
3160 fib_prefix_t pfx = {
3161 .fp_proto = FIB_PROTOCOL_IP4,
3163 .fp_addr.ip4.as_u32 = addr.as_u32,
3168 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3169 fib_table_entry_special_dpo_add (0, &pfx, sm->fib_src_hi,
3170 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3171 dpo_reset (&dpo_v4);
3175 fib_table_entry_special_remove (0, &pfx, sm->fib_src_hi);
3180 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3183 snat_main_t *sm = &snat_main;
3184 u32 next_worker_index = sm->first_worker_index;
3187 clib_bihash_kv_16_8_t kv16, value16;
3188 snat_main_per_thread_data_t *tsm;
3191 if (PREDICT_FALSE (is_output))
3193 u32 fib_index = sm->outside_fib_index;
3194 nat_outside_fib_t *outside_fib;
3195 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3196 fib_prefix_t pfx = {
3197 .fp_proto = FIB_PROTOCOL_IP4,
3200 .ip4.as_u32 = ip->dst_address.as_u32,
3205 udp = ip4_next_header (ip);
3207 switch (vec_len (sm->outside_fibs))
3210 fib_index = sm->outside_fib_index;
3213 fib_index = sm->outside_fibs[0].fib_index;
3217 vec_foreach (outside_fib, sm->outside_fibs)
3219 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3220 if (FIB_NODE_INDEX_INVALID != fei)
3222 if (fib_entry_get_resolving_interface (fei) != ~0)
3224 fib_index = outside_fib->fib_index;
3233 init_ed_k (&kv16, ip->src_address, udp->src_port, ip->dst_address,
3234 udp->dst_port, fib_index, ip->protocol);
3236 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3240 vec_elt_at_index (sm->per_thread_data,
3241 ed_value_get_thread_index (&value16));
3242 next_worker_index += tsm->thread_index;
3244 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3245 next_worker_index, fib_index,
3246 clib_net_to_host_u32 (ip->
3247 src_address.as_u32),
3248 clib_net_to_host_u32 (ip->
3249 dst_address.as_u32));
3251 return next_worker_index;
3255 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3256 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3258 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3259 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3261 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3263 if (PREDICT_TRUE (!is_output))
3265 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3266 next_worker_index, rx_fib_index,
3267 clib_net_to_host_u32 (ip->src_address.as_u32),
3268 clib_net_to_host_u32 (ip->dst_address.as_u32));
3272 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3273 next_worker_index, rx_fib_index,
3274 clib_net_to_host_u32 (ip->src_address.as_u32),
3275 clib_net_to_host_u32 (ip->dst_address.as_u32));
3278 return next_worker_index;
3282 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3283 u32 rx_fib_index, u8 is_output)
3285 snat_main_t *sm = &snat_main;
3286 clib_bihash_kv_8_8_t kv, value;
3287 clib_bihash_kv_16_8_t kv16, value16;
3288 snat_main_per_thread_data_t *tsm;
3290 u32 proto, next_worker_index = 0;
3293 snat_static_mapping_t *m;
3296 proto = ip_proto_to_nat_proto (ip->protocol);
3298 if (PREDICT_TRUE (proto == NAT_PROTOCOL_UDP || proto == NAT_PROTOCOL_TCP))
3300 udp = ip4_next_header (ip);
3302 init_ed_k (&kv16, ip->dst_address, udp->dst_port, ip->src_address,
3303 udp->src_port, rx_fib_index, ip->protocol);
3305 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3309 vec_elt_at_index (sm->per_thread_data,
3310 ed_value_get_thread_index (&value16));
3311 vnet_buffer2 (b)->nat.ed_out2in_nat_session_index =
3312 ed_value_get_session_index (&value16);
3313 next_worker_index = sm->first_worker_index + tsm->thread_index;
3314 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3315 next_worker_index, rx_fib_index,
3316 clib_net_to_host_u32 (ip->
3317 src_address.as_u32),
3318 clib_net_to_host_u32 (ip->
3319 dst_address.as_u32));
3320 return next_worker_index;
3323 else if (proto == NAT_PROTOCOL_ICMP)
3325 if (!get_icmp_o2i_ed_key (b, ip, rx_fib_index, ~0, ~0, 0, 0, 0, &kv16))
3327 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3331 vec_elt_at_index (sm->per_thread_data,
3332 ed_value_get_thread_index (&value16));
3333 next_worker_index = sm->first_worker_index + tsm->thread_index;
3334 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3335 next_worker_index, rx_fib_index,
3336 clib_net_to_host_u32 (ip->
3337 src_address.as_u32),
3338 clib_net_to_host_u32 (ip->
3339 dst_address.as_u32));
3340 return next_worker_index;
3345 /* first try static mappings without port */
3346 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3348 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
3349 if (!clib_bihash_search_8_8
3350 (&sm->static_mapping_by_external, &kv, &value))
3352 m = pool_elt_at_index (sm->static_mappings, value.value);
3353 next_worker_index = m->workers[0];
3358 /* unknown protocol */
3359 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3361 /* use current thread */
3362 next_worker_index = vlib_get_thread_index ();
3366 udp = ip4_next_header (ip);
3367 port = udp->dst_port;
3369 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3371 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3372 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3373 if (!icmp_type_is_error_message
3374 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3375 port = vnet_buffer (b)->ip.reass.l4_src_port;
3378 /* if error message, then it's not fragmented and we can access it */
3379 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3380 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3381 void *l4_header = ip4_next_header (inner_ip);
3384 case NAT_PROTOCOL_ICMP:
3385 icmp = (icmp46_header_t *) l4_header;
3386 echo = (icmp_echo_header_t *) (icmp + 1);
3387 port = echo->identifier;
3389 case NAT_PROTOCOL_UDP:
3390 case NAT_PROTOCOL_TCP:
3391 port = ((tcp_udp_header_t *) l4_header)->src_port;
3394 next_worker_index = vlib_get_thread_index ();
3400 /* try static mappings with port */
3401 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3403 init_nat_k (&kv, ip->dst_address, port, 0, proto);
3404 if (!clib_bihash_search_8_8
3405 (&sm->static_mapping_by_external, &kv, &value))
3407 m = pool_elt_at_index (sm->static_mappings, value.value);
3408 if (!is_lb_static_mapping (m))
3410 next_worker_index = m->workers[0];
3414 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3415 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3417 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3419 m->workers[hash & (_vec_len (m->workers) - 1)];
3421 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3426 /* worker by outside port */
3427 next_worker_index = sm->first_worker_index;
3428 next_worker_index +=
3429 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3432 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3433 clib_net_to_host_u32 (ip->src_address.as_u32),
3434 clib_net_to_host_u32 (ip->dst_address.as_u32));
3435 return next_worker_index;
3439 nat_calc_bihash_buckets (u32 n_elts)
3441 n_elts = n_elts / 2.5;
3443 while (lower_pow2 * 2 < n_elts)
3445 lower_pow2 = 2 * lower_pow2;
3447 u64 upper_pow2 = 2 * lower_pow2;
3448 if ((upper_pow2 - n_elts) < (n_elts - lower_pow2))
3450 if (upper_pow2 <= UINT32_MAX)
3459 nat44_get_max_session_limit ()
3461 snat_main_t *sm = &snat_main;
3462 u32 max_limit = 0, len = 0;
3464 for (; len < vec_len (sm->max_translations_per_fib); len++)
3466 if (max_limit < sm->max_translations_per_fib[len])
3467 max_limit = sm->max_translations_per_fib[len];
3473 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
3475 snat_main_t *sm = &snat_main;
3476 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3477 u32 len = vec_len (sm->max_translations_per_fib);
3479 if (len <= fib_index)
3481 vec_validate (sm->max_translations_per_fib, fib_index + 1);
3483 for (; len < vec_len (sm->max_translations_per_fib); len++)
3484 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
3487 sm->max_translations_per_fib[fib_index] = session_limit;
3492 nat44_update_session_limit (u32 session_limit, u32 vrf_id)
3494 snat_main_t *sm = &snat_main;
3496 if (nat44_set_session_limit (session_limit, vrf_id))
3498 sm->max_translations_per_thread = nat44_get_max_session_limit ();
3500 sm->translation_buckets =
3501 nat_calc_bihash_buckets (sm->max_translations_per_thread);
3503 nat44_sessions_clear ();
3508 nat44_db_init (snat_main_per_thread_data_t * tsm)
3510 snat_main_t *sm = &snat_main;
3512 pool_alloc (tsm->sessions, sm->max_translations_per_thread);
3513 pool_alloc (tsm->lru_pool, sm->max_translations_per_thread);
3517 pool_get (tsm->lru_pool, head);
3518 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
3519 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
3521 pool_get (tsm->lru_pool, head);
3522 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
3523 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
3525 pool_get (tsm->lru_pool, head);
3526 tsm->udp_lru_head_index = head - tsm->lru_pool;
3527 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
3529 pool_get (tsm->lru_pool, head);
3530 tsm->icmp_lru_head_index = head - tsm->lru_pool;
3531 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
3533 pool_get (tsm->lru_pool, head);
3534 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
3535 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
3537 if (sm->endpoint_dependent)
3539 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
3540 sm->translation_buckets, 0);
3541 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
3542 format_ed_session_kvp);
3544 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
3545 sm->translation_buckets, 0);
3546 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
3547 format_ed_session_kvp); */
3551 clib_bihash_init_8_8 (&tsm->in2out, "in2out", sm->translation_buckets,
3553 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out, format_session_kvp);
3554 clib_bihash_init_8_8 (&tsm->out2in, "out2in", sm->translation_buckets,
3556 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in, format_session_kvp);
3559 // TODO: ED nat is not using these
3560 // before removal large refactor required
3561 pool_alloc (tsm->list_pool, sm->max_translations_per_thread);
3562 clib_bihash_init_8_8 (&tsm->user_hash, "users", sm->user_buckets, 0);
3563 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
3567 nat44_db_free (snat_main_per_thread_data_t * tsm)
3569 snat_main_t *sm = &snat_main;
3571 pool_free (tsm->sessions);
3572 pool_free (tsm->lru_pool);
3574 if (sm->endpoint_dependent)
3576 clib_bihash_free_16_8 (&tsm->in2out_ed);
3577 vec_free (tsm->per_vrf_sessions_vec);
3581 clib_bihash_free_8_8 (&tsm->in2out);
3582 clib_bihash_free_8_8 (&tsm->out2in);
3585 // TODO: resolve static mappings (put only to !ED)
3586 pool_free (tsm->users);
3587 pool_free (tsm->list_pool);
3588 clib_bihash_free_8_8 (&tsm->user_hash);
3592 nat44_sessions_clear ()
3594 snat_main_t *sm = &snat_main;
3595 snat_main_per_thread_data_t *tsm;
3597 if (sm->endpoint_dependent)
3599 clib_bihash_free_16_8 (&sm->out2in_ed);
3600 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
3603 sm->translation_buckets, 0);
3604 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
3605 format_ed_session_kvp);
3609 vec_foreach (tsm, sm->per_thread_data)
3611 nat44_db_free (tsm);
3612 nat44_db_init (tsm);
3616 vlib_zero_simple_counter (&sm->total_users, 0);
3617 vlib_zero_simple_counter (&sm->total_sessions, 0);
3621 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
3624 ip4_address_t * address,
3626 u32 if_address_index, u32 is_delete)
3628 snat_main_t *sm = &snat_main;
3629 snat_static_map_resolve_t *rp;
3630 snat_static_mapping_t *m;
3631 clib_bihash_kv_8_8_t kv, value;
3633 ip4_address_t l_addr;
3638 for (i = 0; i < vec_len (sm->to_resolve); i++)
3640 rp = sm->to_resolve + i;
3641 if (rp->addr_only == 0)
3643 if (rp->sw_if_index == sw_if_index)
3650 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
3651 sm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
3652 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3655 m = pool_elt_at_index (sm->static_mappings, value.value);
3659 /* Don't trip over lease renewal, static config */
3669 /* Indetity mapping? */
3670 if (rp->l_addr.as_u32 == 0)
3671 l_addr.as_u32 = address[0].as_u32;
3673 l_addr.as_u32 = rp->l_addr.as_u32;
3674 /* Add the static mapping */
3675 rv = snat_add_static_mapping (l_addr,
3680 rp->addr_only, ~0 /* sw_if_index */ ,
3681 rp->proto, !is_delete, rp->twice_nat,
3682 rp->out2in_only, rp->tag, rp->identity_nat,
3683 rp->pool_addr, rp->exact);
3685 nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
3689 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3692 ip4_address_t * address,
3694 u32 if_address_index, u32 is_delete)
3696 snat_main_t *sm = &snat_main;
3697 snat_static_map_resolve_t *rp;
3698 ip4_address_t l_addr;
3702 snat_address_t *addresses = sm->addresses;
3707 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
3709 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3713 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
3716 addresses = sm->twice_nat_addresses;
3717 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3726 /* Don't trip over lease renewal, static config */
3727 for (j = 0; j < vec_len (addresses); j++)
3728 if (addresses[j].addr.as_u32 == address->as_u32)
3731 (void) snat_add_address (sm, address, ~0, twice_nat);
3732 /* Scan static map resolution vector */
3733 for (j = 0; j < vec_len (sm->to_resolve); j++)
3735 rp = sm->to_resolve + j;
3738 /* On this interface? */
3739 if (rp->sw_if_index == sw_if_index)
3741 /* Indetity mapping? */
3742 if (rp->l_addr.as_u32 == 0)
3743 l_addr.as_u32 = address[0].as_u32;
3745 l_addr.as_u32 = rp->l_addr.as_u32;
3746 /* Add the static mapping */
3747 rv = snat_add_static_mapping (l_addr,
3753 ~0 /* sw_if_index */ ,
3755 rp->is_add, rp->twice_nat,
3756 rp->out2in_only, rp->tag,
3758 rp->pool_addr, rp->exact);
3760 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
3768 (void) snat_del_address (sm, address[0], 1, twice_nat);
3774 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
3777 ip4_main_t *ip4_main = sm->ip4_main;
3778 ip4_address_t *first_int_addr;
3779 snat_static_map_resolve_t *rp;
3780 u32 *indices_to_delete = 0;
3782 u32 *auto_add_sw_if_indices =
3784 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3786 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
3789 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3791 if (auto_add_sw_if_indices[i] == sw_if_index)
3795 /* if have address remove it */
3797 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3800 for (j = 0; j < vec_len (sm->to_resolve); j++)
3802 rp = sm->to_resolve + j;
3803 if (rp->sw_if_index == sw_if_index)
3804 vec_add1 (indices_to_delete, j);
3806 if (vec_len (indices_to_delete))
3808 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3809 vec_del1 (sm->to_resolve, j);
3810 vec_free (indices_to_delete);
3814 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3816 vec_del1 (sm->auto_add_sw_if_indices, i);
3819 return VNET_API_ERROR_VALUE_EXIST;
3826 return VNET_API_ERROR_NO_SUCH_ENTRY;
3828 /* add to the auto-address list */
3830 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3832 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3834 /* If the address is already bound - or static - add it now */
3836 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3842 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3843 nat_protocol_t proto, u32 vrf_id, int is_in)
3845 snat_main_per_thread_data_t *tsm;
3846 clib_bihash_kv_8_8_t kv, value;
3848 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3850 clib_bihash_8_8_t *t;
3852 if (sm->endpoint_dependent)
3853 return VNET_API_ERROR_UNSUPPORTED;
3855 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3856 if (sm->num_workers > 1)
3858 vec_elt_at_index (sm->per_thread_data,
3859 sm->worker_in2out_cb (&ip, fib_index, 0));
3861 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3863 init_nat_k (&kv, *addr, port, fib_index, proto);
3864 t = is_in ? &tsm->in2out : &tsm->out2in;
3865 if (!clib_bihash_search_8_8 (t, &kv, &value))
3867 if (pool_is_free_index (tsm->sessions, value.value))
3868 return VNET_API_ERROR_UNSPECIFIED;
3870 s = pool_elt_at_index (tsm->sessions, value.value);
3871 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
3872 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3876 return VNET_API_ERROR_NO_SUCH_ENTRY;
3880 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3881 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3882 u32 vrf_id, int is_in)
3885 clib_bihash_16_8_t *t;
3886 clib_bihash_kv_16_8_t kv, value;
3887 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3889 snat_main_per_thread_data_t *tsm;
3891 if (!sm->endpoint_dependent)
3892 return VNET_API_ERROR_FEATURE_DISABLED;
3894 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3895 if (sm->num_workers > 1)
3897 vec_elt_at_index (sm->per_thread_data,
3898 sm->worker_in2out_cb (&ip, fib_index, 0));
3900 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3902 t = is_in ? &tsm->in2out_ed : &sm->out2in_ed;
3903 init_ed_k (&kv, *addr, port, *eh_addr, eh_port, fib_index, proto);
3904 if (clib_bihash_search_16_8 (t, &kv, &value))
3906 return VNET_API_ERROR_NO_SUCH_ENTRY;
3909 if (pool_is_free_index (tsm->sessions, value.value))
3910 return VNET_API_ERROR_UNSPECIFIED;
3911 s = pool_elt_at_index (tsm->sessions, value.value);
3912 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
3913 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
3917 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
3918 vlib_node_runtime_t * node,
3919 vlib_frame_t * frame)
3925 VLIB_REGISTER_NODE (nat_default_node) = {
3926 .name = "nat-default",
3927 .vector_size = sizeof (u32),
3929 .type = VLIB_NODE_TYPE_INTERNAL,
3931 .n_next_nodes = NAT_N_NEXT,
3933 [NAT_NEXT_DROP] = "error-drop",
3934 [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
3935 [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
3936 [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
3937 [NAT_NEXT_IN2OUT_ED_OUTPUT_FAST_PATH] = "nat44-ed-in2out-output",
3938 [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
3939 [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
3940 [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
3941 [NAT_NEXT_OUT2IN_ED_HANDOFF] = "nat44-ed-out2in-handoff",
3942 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
3943 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
3949 * fd.io coding-style-patch-verification: ON
3952 * eval: (c-set-style "gnu")