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/nat_ipfix_logging.h>
25 #include <nat/nat_det.h>
26 #include <nat/nat64.h>
27 #include <nat/nat_inlines.h>
28 #include <nat/nat44/inlines.h>
29 #include <nat/nat_affinity.h>
30 #include <nat/nat_syslog.h>
31 #include <nat/nat_ha.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vnet/fib/ip4_fib.h>
34 #include <vnet/ip/reass/ip4_sv_reass.h>
35 #include <vppinfra/bihash_16_8.h>
36 #include <nat/nat44/ed_inlines.h>
38 #include <vpp/app/version.h>
40 snat_main_t snat_main;
42 fib_source_t nat_fib_src_hi;
43 fib_source_t nat_fib_src_low;
46 /* Hook up input features */
47 VNET_FEATURE_INIT (nat_pre_in2out, static) = {
48 .arc_name = "ip4-unicast",
49 .node_name = "nat-pre-in2out",
50 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
51 "ip4-sv-reassembly-feature"),
53 VNET_FEATURE_INIT (nat_pre_out2in, static) = {
54 .arc_name = "ip4-unicast",
55 .node_name = "nat-pre-out2in",
56 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
57 "ip4-dhcp-client-detect",
58 "ip4-sv-reassembly-feature"),
60 VNET_FEATURE_INIT (snat_in2out_worker_handoff, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "nat44-in2out-worker-handoff",
63 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
65 VNET_FEATURE_INIT (snat_out2in_worker_handoff, static) = {
66 .arc_name = "ip4-unicast",
67 .node_name = "nat44-out2in-worker-handoff",
68 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
69 "ip4-dhcp-client-detect"),
71 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
72 .arc_name = "ip4-unicast",
73 .node_name = "nat44-in2out",
74 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
76 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
77 .arc_name = "ip4-unicast",
78 .node_name = "nat44-out2in",
79 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
80 "ip4-dhcp-client-detect"),
82 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
83 .arc_name = "ip4-unicast",
84 .node_name = "nat44-classify",
85 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
87 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
88 .arc_name = "ip4-unicast",
89 .node_name = "nat44-det-in2out",
90 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
92 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
93 .arc_name = "ip4-unicast",
94 .node_name = "nat44-det-out2in",
95 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
96 "ip4-dhcp-client-detect"),
98 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
99 .arc_name = "ip4-unicast",
100 .node_name = "nat44-det-classify",
101 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
103 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
104 .arc_name = "ip4-unicast",
105 .node_name = "nat44-ed-in2out",
106 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
108 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
109 .arc_name = "ip4-unicast",
110 .node_name = "nat44-ed-out2in",
111 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
112 "ip4-dhcp-client-detect"),
114 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
115 .arc_name = "ip4-unicast",
116 .node_name = "nat44-ed-classify",
117 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
119 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
120 .arc_name = "ip4-unicast",
121 .node_name = "nat44-handoff-classify",
122 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
124 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
125 .arc_name = "ip4-unicast",
126 .node_name = "nat44-in2out-fast",
127 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
129 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
130 .arc_name = "ip4-unicast",
131 .node_name = "nat44-out2in-fast",
132 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature",
133 "ip4-dhcp-client-detect"),
135 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
136 .arc_name = "ip4-unicast",
137 .node_name = "nat44-hairpin-dst",
138 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
140 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
141 .arc_name = "ip4-unicast",
142 .node_name = "nat44-ed-hairpin-dst",
143 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa","ip4-sv-reassembly-feature"),
146 /* Hook up output features */
147 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
148 .arc_name = "ip4-output",
149 .node_name = "nat44-in2out-output",
150 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
152 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
153 .arc_name = "ip4-output",
154 .node_name = "nat44-in2out-output-worker-handoff",
155 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
157 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
158 .arc_name = "ip4-output",
159 .node_name = "nat44-hairpin-src",
160 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa","ip4-sv-reassembly-output-feature"),
162 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
163 .arc_name = "ip4-output",
164 .node_name = "nat44-ed-in2out-output",
165 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
166 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
168 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
169 .arc_name = "ip4-output",
170 .node_name = "nat44-ed-hairpin-src",
171 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),
172 .runs_before = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
175 /* Hook up ip4-local features */
176 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
178 .arc_name = "ip4-local",
179 .node_name = "nat44-hairpinning",
180 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
182 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
184 .arc_name = "ip4-local",
185 .node_name = "nat44-ed-hairpinning",
186 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
190 VLIB_PLUGIN_REGISTER () = {
191 .version = VPP_BUILD_VER,
192 .description = "Network Address Translation (NAT)",
197 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
200 clib_bihash_kv_8_8_t kv;
203 ip4_address_t *l_addr, *r_addr;
205 clib_bihash_kv_16_8_t ed_kv;
206 snat_main_per_thread_data_t *tsm =
207 vec_elt_at_index (sm->per_thread_data, thread_index);
209 if (is_fwd_bypass_session (s))
211 if (snat_is_unk_proto_session (s))
213 init_ed_k (&ed_kv, s->in2out.addr, 0, s->ext_host_addr, 0, 0,
218 l_port = s->in2out.port;
219 r_port = s->ext_host_port;
220 l_addr = &s->in2out.addr;
221 r_addr = &s->ext_host_addr;
222 proto = nat_proto_to_ip_proto (s->nat_proto);
223 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index,
226 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
227 nat_elog_warn ("in2out_ed key del failed");
231 /* session lookup tables */
232 if (is_ed_session (s))
234 if (is_affinity_sessions (s))
235 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
236 s->nat_proto, s->out2in.port);
237 l_addr = &s->out2in.addr;
238 r_addr = &s->ext_host_addr;
239 fib_index = s->out2in.fib_index;
240 if (snat_is_unk_proto_session (s))
242 proto = s->in2out.port;
248 proto = nat_proto_to_ip_proto (s->nat_proto);
249 l_port = s->out2in.port;
250 r_port = s->ext_host_port;
252 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
253 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
254 nat_elog_warn ("out2in_ed key del failed");
255 l_addr = &s->in2out.addr;
256 fib_index = s->in2out.fib_index;
257 if (!snat_is_unk_proto_session (s))
258 l_port = s->in2out.port;
259 if (is_twice_nat_session (s))
261 r_addr = &s->ext_host_nat_addr;
262 r_port = s->ext_host_nat_port;
264 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
265 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
266 nat_elog_warn ("in2out_ed key del failed");
269 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
270 &s->in2out.addr, s->in2out.port,
271 &s->ext_host_nat_addr, s->ext_host_nat_port,
272 &s->out2in.addr, s->out2in.port,
273 &s->ext_host_addr, s->ext_host_port,
274 s->nat_proto, is_twice_nat_session (s));
278 init_nat_i2o_k (&kv, s);
279 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
280 nat_elog_warn ("in2out key del failed");
281 init_nat_o2i_k (&kv, s);
282 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
283 nat_elog_warn ("out2in key del failed");
286 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
287 &s->in2out.addr, s->in2out.port,
288 &s->out2in.addr, s->out2in.port,
292 if (snat_is_unk_proto_session (s))
298 snat_ipfix_logging_nat44_ses_delete (thread_index,
299 s->in2out.addr.as_u32,
300 s->out2in.addr.as_u32,
304 s->in2out.fib_index);
306 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
307 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
311 /* Twice NAT address and port for external host */
312 if (is_twice_nat_session (s))
314 snat_free_outside_address_and_port (sm->twice_nat_addresses,
316 &s->ext_host_nat_addr,
317 s->ext_host_nat_port, s->nat_proto);
320 if (snat_is_session_static (s))
323 snat_free_outside_address_and_port (sm->addresses, thread_index,
324 &s->out2in.addr, s->out2in.port,
329 nat44_set_session_limit (u32 session_limit, u32 vrf_id)
331 snat_main_t *sm = &snat_main;
332 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
333 u32 len = vec_len (sm->max_translations_per_fib);
335 if (len <= fib_index)
337 vec_validate (sm->max_translations_per_fib, fib_index + 1);
339 for (; len < vec_len (sm->max_translations_per_fib); len++)
340 sm->max_translations_per_fib[len] = sm->max_translations_per_thread;
343 sm->max_translations_per_fib[fib_index] = session_limit;
348 nat44_free_session_data (snat_main_t * sm, snat_session_t * s,
349 u32 thread_index, u8 is_ha)
353 ip4_address_t *l_addr, *r_addr;
355 clib_bihash_kv_16_8_t ed_kv;
356 snat_main_per_thread_data_t *tsm =
357 vec_elt_at_index (sm->per_thread_data, thread_index);
359 if (is_fwd_bypass_session (s))
361 if (snat_is_unk_proto_session (s))
363 proto = s->in2out.port;
369 proto = nat_proto_to_ip_proto (s->nat_proto);
370 l_port = s->in2out.port;
371 r_port = s->ext_host_port;
374 l_addr = &s->in2out.addr;
375 r_addr = &s->ext_host_addr;
377 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
380 (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)))
381 nat_elog_warn ("in2out_ed key del failed");
385 /* session lookup tables */
386 if (is_affinity_sessions (s))
387 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
388 s->nat_proto, s->out2in.port);
389 l_addr = &s->out2in.addr;
390 r_addr = &s->ext_host_addr;
391 fib_index = s->out2in.fib_index;
392 if (snat_is_unk_proto_session (s))
394 proto = s->in2out.port;
400 proto = nat_proto_to_ip_proto (s->nat_proto);
401 l_port = s->out2in.port;
402 r_port = s->ext_host_port;
404 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
406 if (PREDICT_FALSE (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0)))
407 nat_elog_warn ("out2in_ed key del failed");
409 l_addr = &s->in2out.addr;
410 fib_index = s->in2out.fib_index;
412 if (!snat_is_unk_proto_session (s))
413 l_port = s->in2out.port;
415 if (is_twice_nat_session (s))
417 r_addr = &s->ext_host_nat_addr;
418 r_port = s->ext_host_nat_port;
420 init_ed_k (&ed_kv, *l_addr, l_port, *r_addr, r_port, fib_index, proto);
422 if (PREDICT_FALSE (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)))
423 nat_elog_warn ("in2out_ed key del failed");
427 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
428 &s->in2out.addr, s->in2out.port,
429 &s->ext_host_nat_addr, s->ext_host_nat_port,
430 &s->out2in.addr, s->out2in.port,
431 &s->ext_host_addr, s->ext_host_port,
432 s->nat_proto, is_twice_nat_session (s));
435 if (snat_is_unk_proto_session (s))
440 snat_ipfix_logging_nat44_ses_delete (thread_index,
441 s->in2out.addr.as_u32,
442 s->out2in.addr.as_u32,
446 s->in2out.fib_index);
447 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
448 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
452 /* Twice NAT address and port for external host */
453 if (is_twice_nat_session (s))
455 snat_free_outside_address_and_port (sm->twice_nat_addresses,
457 &s->ext_host_nat_addr,
458 s->ext_host_nat_port, s->nat_proto);
461 if (snat_is_session_static (s))
464 snat_free_outside_address_and_port (sm->addresses, thread_index,
465 &s->out2in.addr, s->out2in.port,
471 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
475 snat_user_key_t user_key;
476 clib_bihash_kv_8_8_t kv, value;
477 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
478 dlist_elt_t *per_user_list_head_elt;
480 user_key.addr.as_u32 = addr->as_u32;
481 user_key.fib_index = fib_index;
482 kv.key = user_key.as_u64;
484 /* Ever heard of the "user" = src ip4 address before? */
485 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
487 if (pool_elts (tsm->users) >= sm->max_users_per_thread)
489 vlib_increment_simple_counter (&sm->user_limit_reached,
491 nat_elog_warn ("maximum user limit reached");
494 /* no, make a new one */
495 pool_get (tsm->users, u);
496 clib_memset (u, 0, sizeof (*u));
498 u->addr.as_u32 = addr->as_u32;
499 u->fib_index = fib_index;
501 pool_get (tsm->list_pool, per_user_list_head_elt);
503 u->sessions_per_user_list_head_index = per_user_list_head_elt -
506 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
508 kv.value = u - tsm->users;
511 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
513 nat_elog_warn ("user_hash key add failed");
514 nat44_delete_user_with_no_session (sm, u, thread_index);
518 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
519 pool_elts (tsm->users));
523 u = pool_elt_at_index (tsm->users, value.value);
530 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
531 u32 thread_index, f64 now)
534 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
535 u32 oldest_per_user_translation_list_index, session_index;
536 dlist_elt_t *oldest_per_user_translation_list_elt;
537 dlist_elt_t *per_user_translation_list_elt;
539 /* Over quota? Recycle the least recently used translation */
540 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
542 oldest_per_user_translation_list_index =
543 clib_dlist_remove_head (tsm->list_pool,
544 u->sessions_per_user_list_head_index);
546 ASSERT (oldest_per_user_translation_list_index != ~0);
548 /* Add it back to the end of the LRU list */
549 clib_dlist_addtail (tsm->list_pool,
550 u->sessions_per_user_list_head_index,
551 oldest_per_user_translation_list_index);
552 /* Get the list element */
553 oldest_per_user_translation_list_elt =
554 pool_elt_at_index (tsm->list_pool,
555 oldest_per_user_translation_list_index);
557 /* Get the session index from the list element */
558 session_index = oldest_per_user_translation_list_elt->value;
560 /* Get the session */
561 s = pool_elt_at_index (tsm->sessions, session_index);
562 nat_free_session_data (sm, s, thread_index, 0);
563 if (snat_is_session_static (s))
564 u->nstaticsessions--;
571 s->ext_host_addr.as_u32 = 0;
572 s->ext_host_port = 0;
573 s->ext_host_nat_addr.as_u32 = 0;
574 s->ext_host_nat_port = 0;
578 pool_get (tsm->sessions, s);
579 clib_memset (s, 0, sizeof (*s));
581 /* Create list elts */
582 pool_get (tsm->list_pool, per_user_translation_list_elt);
583 clib_dlist_init (tsm->list_pool,
584 per_user_translation_list_elt - tsm->list_pool);
586 per_user_translation_list_elt->value = s - tsm->sessions;
587 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
588 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
590 clib_dlist_addtail (tsm->list_pool,
591 s->per_user_list_head_index,
592 per_user_translation_list_elt - tsm->list_pool);
594 s->user_index = u - tsm->users;
595 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
596 pool_elts (tsm->sessions));
599 s->ha_last_refreshed = now;
605 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
608 fib_prefix_t prefix = {
610 .fp_proto = FIB_PROTOCOL_IP4,
612 .ip4.as_u32 = addr->as_u32,
615 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
618 fib_table_entry_update_one_path (fib_index,
621 (FIB_ENTRY_FLAG_CONNECTED |
622 FIB_ENTRY_FLAG_LOCAL |
623 FIB_ENTRY_FLAG_EXCLUSIVE),
627 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
629 fib_table_entry_delete (fib_index, &prefix, nat_fib_src_low);
633 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
638 vlib_thread_main_t *tm = vlib_get_thread_main ();
640 if (twice_nat && !sm->endpoint_dependent)
641 return VNET_API_ERROR_FEATURE_DISABLED;
643 /* Check if address already exists */
645 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
647 if (ap->addr.as_u32 == addr->as_u32)
648 return VNET_API_ERROR_VALUE_EXIST;
653 vec_add2 (sm->twice_nat_addresses, ap, 1);
655 vec_add2 (sm->addresses, ap, 1);
660 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
664 #define _(N, i, n, s) \
665 clib_memset(ap->busy_##n##_port_refcounts, 0, sizeof(ap->busy_##n##_port_refcounts));\
666 ap->busy_##n##_ports = 0; \
667 ap->busy_##n##_ports_per_thread = 0;\
668 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
674 /* Add external address to FIB */
676 pool_foreach (i, sm->interfaces,
678 if (nat_interface_is_inside(i) || sm->out2in_dpo)
681 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
684 pool_foreach (i, sm->output_feature_interfaces,
686 if (nat_interface_is_inside(i) || sm->out2in_dpo)
689 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
698 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
700 snat_static_mapping_t *m;
702 pool_foreach (m, sm->static_mappings,
704 if (is_addr_only_static_mapping (m) ||
705 is_out2in_only_static_mapping (m) ||
706 is_identity_static_mapping (m))
708 if (m->external_addr.as_u32 == addr.as_u32)
717 snat_add_static_mapping_when_resolved (snat_main_t * sm,
718 ip4_address_t l_addr,
723 nat_protocol_t proto,
724 int addr_only, int is_add, u8 * tag,
725 int twice_nat, int out2in_only,
728 snat_static_map_resolve_t *rp;
730 vec_add2 (sm->to_resolve, rp, 1);
731 rp->l_addr.as_u32 = l_addr.as_u32;
733 rp->sw_if_index = sw_if_index;
737 rp->addr_only = addr_only;
739 rp->twice_nat = twice_nat;
740 rp->out2in_only = out2in_only;
741 rp->identity_nat = identity_nat;
742 rp->tag = vec_dup (tag);
746 get_thread_idx_by_port (u16 e_port)
748 snat_main_t *sm = &snat_main;
749 u32 thread_idx = sm->num_workers;
750 if (sm->num_workers > 1)
753 sm->first_worker_index +
754 sm->workers[(e_port - 1024) / sm->port_per_thread];
760 snat_static_mapping_del_sessions (snat_main_t * sm,
761 snat_main_per_thread_data_t * tsm,
762 snat_user_key_t u_key, int addr_only,
763 ip4_address_t e_addr, u16 e_port)
765 clib_bihash_kv_8_8_t kv, value;
766 kv.key = u_key.as_u64;
768 dlist_elt_t *head, *elt;
771 u32 elt_index, head_index, ses_index;
772 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
774 user_index = value.value;
775 u = pool_elt_at_index (tsm->users, user_index);
776 if (u->nstaticsessions)
778 head_index = u->sessions_per_user_list_head_index;
779 head = pool_elt_at_index (tsm->list_pool, head_index);
780 elt_index = head->next;
781 elt = pool_elt_at_index (tsm->list_pool, elt_index);
782 ses_index = elt->value;
783 while (ses_index != ~0)
785 s = pool_elt_at_index (tsm->sessions, ses_index);
786 elt = pool_elt_at_index (tsm->list_pool, elt->next);
787 ses_index = elt->value;
791 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
792 (s->out2in.port != e_port))
796 if (is_lb_session (s))
799 if (!snat_is_session_static (s))
802 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
803 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
813 snat_ed_static_mapping_del_sessions (snat_main_t * sm,
814 snat_main_per_thread_data_t * tsm,
815 ip4_address_t l_addr,
818 u32 fib_index, int addr_only,
819 ip4_address_t e_addr, u16 e_port)
822 u32 *indexes_to_free = NULL;
824 pool_foreach (s, tsm->sessions, {
825 if (s->in2out.fib_index != fib_index ||
826 s->in2out.addr.as_u32 != l_addr.as_u32)
832 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
833 s->out2in.port != e_port ||
834 s->in2out.port != l_port ||
835 s->nat_proto != protocol)
839 if (is_lb_session (s))
841 if (!snat_is_session_static (s))
843 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
844 vec_add1 (indexes_to_free, s - tsm->sessions);
850 vec_foreach (ses_index, indexes_to_free)
852 s = pool_elt_at_index (tsm->sessions, *ses_index);
853 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
855 vec_free (indexes_to_free);
859 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
860 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
861 u32 sw_if_index, nat_protocol_t proto, int is_add,
862 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
865 snat_main_t *sm = &snat_main;
866 snat_static_mapping_t *m;
867 clib_bihash_kv_8_8_t kv, value;
868 snat_address_t *a = 0;
870 snat_interface_t *interface;
872 snat_main_per_thread_data_t *tsm;
873 snat_user_key_t u_key;
875 dlist_elt_t *head, *elt;
876 u32 elt_index, head_index;
880 snat_static_map_resolve_t *rp, *rp_match = 0;
881 nat44_lb_addr_port_t *local;
884 if (!sm->endpoint_dependent)
886 if (twice_nat || out2in_only)
887 return VNET_API_ERROR_FEATURE_DISABLED;
890 /* If the external address is a specific interface address */
891 if (sw_if_index != ~0)
893 ip4_address_t *first_int_addr;
895 for (i = 0; i < vec_len (sm->to_resolve); i++)
897 rp = sm->to_resolve + i;
898 if (rp->sw_if_index != sw_if_index ||
899 rp->l_addr.as_u32 != l_addr.as_u32 ||
900 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
905 if ((rp->l_port != l_port && rp->e_port != e_port)
906 || rp->proto != proto)
914 /* Might be already set... */
915 first_int_addr = ip4_interface_first_address
916 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
921 return VNET_API_ERROR_VALUE_EXIST;
923 snat_add_static_mapping_when_resolved
924 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
925 addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
927 /* DHCP resolution required? */
928 if (first_int_addr == 0)
934 e_addr.as_u32 = first_int_addr->as_u32;
935 /* Identity mapping? */
936 if (l_addr.as_u32 == 0)
937 l_addr.as_u32 = e_addr.as_u32;
943 return VNET_API_ERROR_NO_SUCH_ENTRY;
945 vec_del1 (sm->to_resolve, i);
949 e_addr.as_u32 = first_int_addr->as_u32;
950 /* Identity mapping? */
951 if (l_addr.as_u32 == 0)
952 l_addr.as_u32 = e_addr.as_u32;
959 init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto);
960 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
963 m = pool_elt_at_index (sm->static_mappings, value.value);
969 if (is_identity_static_mapping (m))
972 pool_foreach (local, m->locals,
974 if (local->vrf_id == vrf_id)
975 return VNET_API_ERROR_VALUE_EXIST;
978 pool_get (m->locals, local);
979 local->vrf_id = vrf_id;
981 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
983 init_nat_kv (&kv, m->local_addr, m->local_port,
984 local->fib_index, m->proto,
985 m - sm->static_mappings);
986 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
990 return VNET_API_ERROR_VALUE_EXIST;
993 if (twice_nat && addr_only)
994 return VNET_API_ERROR_UNSUPPORTED;
996 /* Convert VRF id to FIB index */
999 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1001 /* If not specified use inside VRF id from SNAT plugin startup config */
1004 fib_index = sm->inside_fib_index;
1005 vrf_id = sm->inside_vrf_id;
1006 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1009 if (!(out2in_only || identity_nat))
1011 init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index,
1012 addr_only ? 0 : proto);
1013 if (!clib_bihash_search_8_8
1014 (&sm->static_mapping_by_local, &kv, &value))
1015 return VNET_API_ERROR_VALUE_EXIST;
1018 /* Find external address in allocated addresses and reserve port for
1019 address and port pair mapping when dynamic translations enabled */
1020 if (!(addr_only || sm->static_mapping_only || out2in_only))
1022 for (i = 0; i < vec_len (sm->addresses); i++)
1024 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1026 a = sm->addresses + i;
1027 /* External port must be unused */
1030 #define _(N, j, n, s) \
1031 case NAT_PROTOCOL_##N: \
1032 if (a->busy_##n##_port_refcounts[e_port]) \
1033 return VNET_API_ERROR_INVALID_VALUE; \
1034 ++a->busy_##n##_port_refcounts[e_port]; \
1035 if (e_port > 1024) \
1037 a->busy_##n##_ports++; \
1038 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1041 foreach_nat_protocol
1044 nat_elog_info ("unknown protocol");
1045 return VNET_API_ERROR_INVALID_VALUE_2;
1050 /* External address must be allocated */
1051 if (!a && (l_addr.as_u32 != e_addr.as_u32))
1053 if (sw_if_index != ~0)
1055 for (i = 0; i < vec_len (sm->to_resolve); i++)
1057 rp = sm->to_resolve + i;
1060 if (rp->sw_if_index != sw_if_index &&
1061 rp->l_addr.as_u32 != l_addr.as_u32 &&
1062 rp->vrf_id != vrf_id && rp->l_port != l_port &&
1063 rp->e_port != e_port && rp->proto != proto)
1066 vec_del1 (sm->to_resolve, i);
1070 return VNET_API_ERROR_NO_SUCH_ENTRY;
1074 pool_get (sm->static_mappings, m);
1075 clib_memset (m, 0, sizeof (*m));
1076 m->tag = vec_dup (tag);
1077 m->local_addr = l_addr;
1078 m->external_addr = e_addr;
1079 m->twice_nat = twice_nat;
1081 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1083 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
1086 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
1087 pool_get (m->locals, local);
1088 local->vrf_id = vrf_id;
1089 local->fib_index = fib_index;
1094 m->fib_index = fib_index;
1098 m->local_port = l_port;
1099 m->external_port = e_port;
1103 if (sm->num_workers > 1)
1106 .src_address = m->local_addr,
1108 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
1109 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1112 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1114 init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto,
1115 m - sm->static_mappings);
1117 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1119 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1120 m - sm->static_mappings);
1121 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
1123 /* Delete dynamic sessions matching local address (+ local port) */
1124 if (!(sm->static_mapping_only))
1126 u_key.addr = m->local_addr;
1127 u_key.fib_index = m->fib_index;
1128 kv.key = u_key.as_u64;
1129 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1131 user_index = value.value;
1132 u = pool_elt_at_index (tsm->users, user_index);
1135 head_index = u->sessions_per_user_list_head_index;
1136 head = pool_elt_at_index (tsm->list_pool, head_index);
1137 elt_index = head->next;
1138 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1139 ses_index = elt->value;
1140 while (ses_index != ~0)
1142 s = pool_elt_at_index (tsm->sessions, ses_index);
1143 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1144 ses_index = elt->value;
1146 if (snat_is_session_static (s))
1149 if (!addr_only && s->in2out.port != m->local_port)
1152 nat_free_session_data (sm, s,
1153 tsm - sm->per_thread_data, 0);
1154 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1156 if (!addr_only && !sm->endpoint_dependent)
1167 if (sw_if_index != ~0)
1170 return VNET_API_ERROR_NO_SUCH_ENTRY;
1176 vrf_id = sm->inside_vrf_id;
1179 pool_foreach (local, m->locals,
1181 if (local->vrf_id == vrf_id)
1182 find = local - m->locals;
1186 return VNET_API_ERROR_NO_SUCH_ENTRY;
1188 local = pool_elt_at_index (m->locals, find);
1189 fib_index = local->fib_index;
1190 pool_put (m->locals, local);
1193 fib_index = m->fib_index;
1195 /* Free external address port */
1196 if (!(addr_only || sm->static_mapping_only || out2in_only))
1198 for (i = 0; i < vec_len (sm->addresses); i++)
1200 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1202 a = sm->addresses + i;
1205 #define _(N, j, n, s) \
1206 case NAT_PROTOCOL_##N: \
1207 --a->busy_##n##_port_refcounts[e_port]; \
1208 if (e_port > 1024) \
1210 a->busy_##n##_ports--; \
1211 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1214 foreach_nat_protocol
1217 nat_elog_info ("unknown protocol");
1218 return VNET_API_ERROR_INVALID_VALUE_2;
1225 if (sm->num_workers > 1)
1226 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1228 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1230 init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto);
1232 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1234 /* Delete session(s) for static mapping if exist */
1235 if (!(sm->static_mapping_only) ||
1236 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1238 if (sm->endpoint_dependent)
1240 snat_ed_static_mapping_del_sessions (sm, tsm, m->local_addr,
1241 m->local_port, m->proto,
1242 fib_index, addr_only,
1247 u_key.addr = m->local_addr;
1248 u_key.fib_index = fib_index;
1249 kv.key = u_key.as_u64;
1250 snat_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
1255 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1256 if (pool_elts (m->locals))
1259 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1260 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1263 vec_free (m->workers);
1264 /* Delete static mapping from pool */
1265 pool_put (sm->static_mappings, m);
1268 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1271 /* Add/delete external address to FIB */
1273 pool_foreach (interface, sm->interfaces,
1275 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1278 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1281 pool_foreach (interface, sm->output_feature_interfaces,
1283 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1286 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1295 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1296 nat_protocol_t proto,
1297 nat44_lb_addr_port_t * locals, u8 is_add,
1298 twice_nat_type_t twice_nat, u8 out2in_only,
1299 u8 * tag, u32 affinity)
1301 snat_main_t *sm = &snat_main;
1302 snat_static_mapping_t *m;
1303 clib_bihash_kv_8_8_t kv, value;
1304 snat_address_t *a = 0;
1306 nat44_lb_addr_port_t *local;
1307 snat_main_per_thread_data_t *tsm;
1311 if (!sm->endpoint_dependent)
1312 return VNET_API_ERROR_FEATURE_DISABLED;
1314 init_nat_k (&kv, e_addr, e_port, 0, proto);
1315 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1318 m = pool_elt_at_index (sm->static_mappings, value.value);
1323 return VNET_API_ERROR_VALUE_EXIST;
1325 if (vec_len (locals) < 2)
1326 return VNET_API_ERROR_INVALID_VALUE;
1328 /* Find external address in allocated addresses and reserve port for
1329 address and port pair mapping when dynamic translations enabled */
1330 if (!(sm->static_mapping_only || out2in_only))
1332 for (i = 0; i < vec_len (sm->addresses); i++)
1334 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1336 a = sm->addresses + i;
1337 /* External port must be unused */
1340 #define _(N, j, n, s) \
1341 case NAT_PROTOCOL_##N: \
1342 if (a->busy_##n##_port_refcounts[e_port]) \
1343 return VNET_API_ERROR_INVALID_VALUE; \
1344 ++a->busy_##n##_port_refcounts[e_port]; \
1345 if (e_port > 1024) \
1347 a->busy_##n##_ports++; \
1348 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1351 foreach_nat_protocol
1354 nat_elog_info ("unknown protocol");
1355 return VNET_API_ERROR_INVALID_VALUE_2;
1360 /* External address must be allocated */
1362 return VNET_API_ERROR_NO_SUCH_ENTRY;
1365 pool_get (sm->static_mappings, m);
1366 clib_memset (m, 0, sizeof (*m));
1367 m->tag = vec_dup (tag);
1368 m->external_addr = e_addr;
1369 m->external_port = e_port;
1371 m->twice_nat = twice_nat;
1372 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1374 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1375 m->affinity = affinity;
1378 m->affinity_per_service_list_head_index =
1379 nat_affinity_get_per_service_list_head_index ();
1381 m->affinity_per_service_list_head_index = ~0;
1383 init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto,
1384 m - sm->static_mappings);
1385 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1387 nat_elog_err ("static_mapping_by_external key add failed");
1388 return VNET_API_ERROR_UNSPECIFIED;
1391 for (i = 0; i < vec_len (locals); i++)
1393 locals[i].fib_index =
1394 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1399 init_nat_kv (&kv, locals[i].addr, locals[i].port,
1400 locals[i].fib_index, m->proto,
1401 m - sm->static_mappings);
1402 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1404 locals[i].prefix = (i == 0) ? locals[i].probability :
1405 (locals[i - 1].prefix + locals[i].probability);
1406 pool_get (m->locals, local);
1408 if (sm->num_workers > 1)
1411 .src_address = locals[i].addr,
1414 clib_bitmap_set (bitmap,
1415 sm->worker_in2out_cb (&ip, m->fib_index, 0),
1420 /* Assign workers */
1421 if (sm->num_workers > 1)
1424 clib_bitmap_foreach (i, bitmap,
1426 vec_add1(m->workers, i);
1434 return VNET_API_ERROR_NO_SUCH_ENTRY;
1436 if (!is_lb_static_mapping (m))
1437 return VNET_API_ERROR_INVALID_VALUE;
1439 /* Free external address port */
1440 if (!(sm->static_mapping_only || out2in_only))
1442 for (i = 0; i < vec_len (sm->addresses); i++)
1444 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1446 a = sm->addresses + i;
1449 #define _(N, j, n, s) \
1450 case NAT_PROTOCOL_##N: \
1451 --a->busy_##n##_port_refcounts[e_port]; \
1452 if (e_port > 1024) \
1454 a->busy_##n##_ports--; \
1455 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1458 foreach_nat_protocol
1461 nat_elog_info ("unknown protocol");
1462 return VNET_API_ERROR_INVALID_VALUE_2;
1469 init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto);
1470 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1472 nat_elog_err ("static_mapping_by_external key del failed");
1473 return VNET_API_ERROR_UNSPECIFIED;
1477 pool_foreach (local, m->locals,
1479 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1483 init_nat_k(& kv, local->addr, local->port, local->fib_index, m->proto);
1484 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1486 nat_elog_err ("static_mapping_by_local key del failed");
1487 return VNET_API_ERROR_UNSPECIFIED;
1491 if (sm->num_workers > 1)
1494 .src_address = local->addr,
1496 tsm = vec_elt_at_index (sm->per_thread_data,
1497 sm->worker_in2out_cb (&ip, m->fib_index, 0));
1500 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1502 /* Delete sessions */
1503 pool_foreach (s, tsm->sessions, {
1504 if (!(is_lb_session (s)))
1507 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1508 s->in2out.port != local->port)
1511 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1512 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1517 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1518 pool_free (m->locals);
1520 vec_free (m->workers);
1522 pool_put (sm->static_mappings, m);
1529 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1530 ip4_address_t l_addr, u16 l_port,
1531 nat_protocol_t proto, u32 vrf_id,
1532 u8 probability, u8 is_add)
1534 snat_main_t *sm = &snat_main;
1535 snat_static_mapping_t *m = 0;
1536 clib_bihash_kv_8_8_t kv, value;
1537 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1538 snat_main_per_thread_data_t *tsm;
1544 if (!sm->endpoint_dependent)
1545 return VNET_API_ERROR_FEATURE_DISABLED;
1547 init_nat_k (&kv, e_addr, e_port, 0, proto);
1548 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1549 m = pool_elt_at_index (sm->static_mappings, value.value);
1552 return VNET_API_ERROR_NO_SUCH_ENTRY;
1554 if (!is_lb_static_mapping (m))
1555 return VNET_API_ERROR_INVALID_VALUE;
1558 pool_foreach (local, m->locals,
1560 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1561 (local->vrf_id == vrf_id))
1563 match_local = local;
1572 return VNET_API_ERROR_VALUE_EXIST;
1574 pool_get (m->locals, local);
1575 clib_memset (local, 0, sizeof (*local));
1576 local->addr.as_u32 = l_addr.as_u32;
1577 local->port = l_port;
1578 local->probability = probability;
1579 local->vrf_id = vrf_id;
1581 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1584 if (!is_out2in_only_static_mapping (m))
1586 init_nat_kv (&kv, l_addr, l_port, local->fib_index, proto,
1587 m - sm->static_mappings);
1588 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1589 nat_elog_err ("static_mapping_by_local key add failed");
1595 return VNET_API_ERROR_NO_SUCH_ENTRY;
1597 if (pool_elts (m->locals) < 3)
1598 return VNET_API_ERROR_UNSPECIFIED;
1600 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1603 if (!is_out2in_only_static_mapping (m))
1605 init_nat_k (&kv, l_addr, l_port, match_local->fib_index, proto);
1606 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1607 nat_elog_err ("static_mapping_by_local key del failed");
1610 if (sm->num_workers > 1)
1613 .src_address = local->addr,
1615 tsm = vec_elt_at_index (sm->per_thread_data,
1616 sm->worker_in2out_cb (&ip, m->fib_index,
1620 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1622 /* Delete sessions */
1624 pool_foreach (s, tsm->sessions, {
1625 if (!(is_lb_session (s)))
1628 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1629 s->in2out.port != match_local->port)
1632 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1633 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1637 pool_put (m->locals, match_local);
1640 vec_free (m->workers);
1643 pool_foreach (local, m->locals,
1645 vec_add1 (locals, local - m->locals);
1646 if (sm->num_workers > 1)
1649 ip.src_address.as_u32 = local->addr.as_u32,
1650 bitmap = clib_bitmap_set (bitmap,
1651 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1657 ASSERT (vec_len (locals) > 1);
1659 local = pool_elt_at_index (m->locals, locals[0]);
1660 local->prefix = local->probability;
1661 for (i = 1; i < vec_len (locals); i++)
1663 local = pool_elt_at_index (m->locals, locals[i]);
1664 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1665 local->prefix = local->probability + prev_local->prefix;
1668 /* Assign workers */
1669 if (sm->num_workers > 1)
1672 clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); }));
1680 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1683 snat_address_t *a = 0;
1684 snat_session_t *ses;
1685 u32 *ses_to_be_removed = 0, *ses_index;
1686 snat_main_per_thread_data_t *tsm;
1687 snat_static_mapping_t *m;
1688 snat_interface_t *interface;
1690 snat_address_t *addresses =
1691 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1693 /* Find SNAT address */
1694 for (i = 0; i < vec_len (addresses); i++)
1696 if (addresses[i].addr.as_u32 == addr.as_u32)
1703 return VNET_API_ERROR_NO_SUCH_ENTRY;
1708 pool_foreach (m, sm->static_mappings,
1710 if (m->external_addr.as_u32 == addr.as_u32)
1711 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1712 m->local_port, m->external_port,
1713 m->vrf_id, is_addr_only_static_mapping(m), ~0,
1714 m->proto, 0, m->twice_nat,
1715 is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1721 /* Check if address is used in some static mapping */
1722 if (is_snat_address_used_in_static_mapping (sm, addr))
1724 nat_elog_notice ("address used in static mapping");
1725 return VNET_API_ERROR_UNSPECIFIED;
1729 if (a->fib_index != ~0)
1730 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nat_fib_src_low);
1732 /* Delete sessions using address */
1733 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1736 vec_foreach (tsm, sm->per_thread_data)
1738 pool_foreach (ses, tsm->sessions, ({
1739 if (ses->out2in.addr.as_u32 == addr.as_u32)
1741 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1742 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1746 if (sm->endpoint_dependent){
1747 vec_foreach (ses_index, ses_to_be_removed)
1749 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1750 nat_ed_session_delete (sm, ses, tsm - sm->per_thread_data, 1);
1753 vec_foreach (ses_index, ses_to_be_removed)
1755 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1756 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1760 vec_free (ses_to_be_removed);
1765 #define _(N, i, n, s) \
1766 vec_free (a->busy_##n##_ports_per_thread);
1767 foreach_nat_protocol
1771 vec_del1 (sm->twice_nat_addresses, i);
1775 vec_del1 (sm->addresses, i);
1777 /* Delete external address from FIB */
1779 pool_foreach (interface, sm->interfaces,
1781 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1784 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1787 pool_foreach (interface, sm->output_feature_interfaces,
1789 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1792 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1801 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1803 snat_main_t *sm = &snat_main;
1804 snat_interface_t *i;
1805 const char *feature_name, *del_feature_name;
1807 snat_static_mapping_t *m;
1809 nat_outside_fib_t *outside_fib;
1810 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1813 if (sm->out2in_dpo && !is_inside)
1814 return VNET_API_ERROR_UNSUPPORTED;
1817 pool_foreach (i, sm->output_feature_interfaces,
1819 if (i->sw_if_index == sw_if_index)
1820 return VNET_API_ERROR_VALUE_EXIST;
1824 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1825 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1828 if (sm->num_workers > 1 && !sm->deterministic)
1830 is_inside ? "nat44-in2out-worker-handoff" :
1831 "nat44-out2in-worker-handoff";
1832 else if (sm->deterministic)
1833 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1834 else if (sm->endpoint_dependent)
1836 feature_name = is_inside ? "nat-pre-in2out" : "nat-pre-out2in";
1839 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1842 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1843 sm->fq_in2out_index =
1844 vlib_frame_queue_main_init (sm->in2out_node_index, NAT_FQ_NELTS);
1846 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1847 sm->fq_out2in_index =
1848 vlib_frame_queue_main_init (sm->out2in_node_index, NAT_FQ_NELTS);
1853 vec_foreach (outside_fib, sm->outside_fibs)
1855 if (outside_fib->fib_index == fib_index)
1859 outside_fib->refcount--;
1860 if (!outside_fib->refcount)
1861 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1864 outside_fib->refcount++;
1871 vec_add2 (sm->outside_fibs, outside_fib, 1);
1872 outside_fib->refcount = 1;
1873 outside_fib->fib_index = fib_index;
1878 pool_foreach (i, sm->interfaces,
1880 if (i->sw_if_index == sw_if_index)
1884 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1887 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1889 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1891 if (sm->num_workers > 1 && !sm->deterministic)
1893 del_feature_name = "nat44-handoff-classify";
1894 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1895 "nat44-out2in-worker-handoff";
1897 else if (sm->deterministic)
1899 del_feature_name = "nat44-det-classify";
1900 feature_name = !is_inside ? "nat44-det-in2out" :
1903 else if (sm->endpoint_dependent)
1905 del_feature_name = "nat44-ed-classify";
1906 feature_name = !is_inside ? "nat-pre-in2out" :
1911 del_feature_name = "nat44-classify";
1912 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1915 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1918 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1919 sw_if_index, 0, 0, 0);
1920 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1921 sw_if_index, 1, 0, 0);
1924 if (sm->endpoint_dependent)
1925 vnet_feature_enable_disable ("ip4-local",
1926 "nat44-ed-hairpinning",
1927 sw_if_index, 1, 0, 0);
1928 else if (!sm->deterministic)
1929 vnet_feature_enable_disable ("ip4-local",
1930 "nat44-hairpinning",
1931 sw_if_index, 1, 0, 0);
1936 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0);
1939 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1940 sw_if_index, 0, 0, 0);
1941 pool_put (sm->interfaces, i);
1944 if (sm->endpoint_dependent)
1945 vnet_feature_enable_disable ("ip4-local",
1946 "nat44-ed-hairpinning",
1947 sw_if_index, 0, 0, 0);
1948 else if (!sm->deterministic)
1949 vnet_feature_enable_disable ("ip4-local",
1950 "nat44-hairpinning",
1951 sw_if_index, 0, 0, 0);
1957 if ((nat_interface_is_inside(i) && is_inside) ||
1958 (nat_interface_is_outside(i) && !is_inside))
1961 if (sm->num_workers > 1 && !sm->deterministic)
1963 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1964 "nat44-out2in-worker-handoff";
1965 feature_name = "nat44-handoff-classify";
1967 else if (sm->deterministic)
1969 del_feature_name = !is_inside ? "nat44-det-in2out" :
1971 feature_name = "nat44-det-classify";
1973 else if (sm->endpoint_dependent)
1975 del_feature_name = !is_inside ? "nat-pre-in2out" :
1978 feature_name = "nat44-ed-classify";
1982 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1983 feature_name = "nat44-classify";
1986 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
1989 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1990 sw_if_index, 0, 0, 0);
1991 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1992 sw_if_index, 1, 0, 0);
1995 if (sm->endpoint_dependent)
1996 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1997 sw_if_index, 0, 0, 0);
1998 else if (!sm->deterministic)
1999 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2000 sw_if_index, 0, 0, 0);
2011 return VNET_API_ERROR_NO_SUCH_ENTRY;
2013 pool_get (sm->interfaces, i);
2014 i->sw_if_index = sw_if_index;
2016 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
2019 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1);
2023 if (is_inside && !sm->out2in_dpo)
2025 if (sm->endpoint_dependent)
2026 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
2027 sw_if_index, 1, 0, 0);
2028 else if (!sm->deterministic)
2029 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
2030 sw_if_index, 1, 0, 0);
2036 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2040 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2042 /* Add/delete external addresses to FIB */
2045 vec_foreach (ap, sm->addresses)
2046 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2048 pool_foreach (m, sm->static_mappings,
2050 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2053 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2056 pool_foreach (dm, sm->det_maps,
2058 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
2066 snat_interface_add_del_output_feature (u32 sw_if_index,
2067 u8 is_inside, int is_del)
2069 snat_main_t *sm = &snat_main;
2070 snat_interface_t *i;
2072 snat_static_mapping_t *m;
2073 nat_outside_fib_t *outside_fib;
2074 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2078 if (sm->deterministic ||
2079 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
2080 return VNET_API_ERROR_UNSUPPORTED;
2083 pool_foreach (i, sm->interfaces,
2085 if (i->sw_if_index == sw_if_index)
2086 return VNET_API_ERROR_VALUE_EXIST;
2093 vec_foreach (outside_fib, sm->outside_fibs)
2095 if (outside_fib->fib_index == fib_index)
2099 outside_fib->refcount--;
2100 if (!outside_fib->refcount)
2101 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2104 outside_fib->refcount++;
2111 vec_add2 (sm->outside_fibs, outside_fib, 1);
2112 outside_fib->refcount = 1;
2113 outside_fib->fib_index = fib_index;
2120 if (sm->endpoint_dependent)
2123 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2127 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2131 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2132 sw_if_index, !is_del, 0, 0);
2133 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2134 sw_if_index, !is_del, 0, 0);
2139 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2143 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2147 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2148 sw_if_index, !is_del, 0, 0);
2149 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2150 sw_if_index, !is_del, 0, 0);
2155 if (sm->num_workers > 1)
2157 int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2161 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del);
2164 vnet_feature_enable_disable ("ip4-unicast",
2165 "nat44-out2in-worker-handoff",
2166 sw_if_index, !is_del, 0, 0);
2167 vnet_feature_enable_disable ("ip4-output",
2168 "nat44-in2out-output-worker-handoff",
2169 sw_if_index, !is_del, 0, 0);
2173 if (sm->endpoint_dependent)
2176 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2180 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2184 vnet_feature_enable_disable ("ip4-unicast", "nat-pre-out2in",
2185 sw_if_index, !is_del, 0, 0);
2186 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
2187 sw_if_index, !is_del, 0, 0);
2192 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del);
2196 ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index,
2200 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2201 sw_if_index, !is_del, 0, 0);
2202 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2203 sw_if_index, !is_del, 0, 0);
2208 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2209 sm->fq_in2out_output_index =
2210 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2212 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2213 sm->fq_out2in_index =
2214 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2217 pool_foreach (i, sm->output_feature_interfaces,
2219 if (i->sw_if_index == sw_if_index)
2222 pool_put (sm->output_feature_interfaces, i);
2224 return VNET_API_ERROR_VALUE_EXIST;
2232 return VNET_API_ERROR_NO_SUCH_ENTRY;
2234 pool_get (sm->output_feature_interfaces, i);
2235 i->sw_if_index = sw_if_index;
2238 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2240 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2242 /* Add/delete external addresses to FIB */
2248 vec_foreach (ap, sm->addresses)
2249 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2251 pool_foreach (m, sm->static_mappings,
2253 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2256 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2264 snat_set_workers (uword * bitmap)
2266 snat_main_t *sm = &snat_main;
2269 if (sm->num_workers < 2)
2270 return VNET_API_ERROR_FEATURE_DISABLED;
2272 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2273 return VNET_API_ERROR_INVALID_WORKER;
2275 vec_free (sm->workers);
2277 clib_bitmap_foreach (i, bitmap,
2279 vec_add1(sm->workers, i);
2280 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2281 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2286 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2292 snat_update_outside_fib (u32 sw_if_index, u32 new_fib_index,
2295 snat_main_t *sm = &snat_main;
2296 nat_outside_fib_t *outside_fib;
2297 snat_interface_t *i;
2301 if (new_fib_index == old_fib_index)
2304 if (!vec_len (sm->outside_fibs))
2308 pool_foreach (i, sm->interfaces,
2310 if (i->sw_if_index == sw_if_index)
2312 if (!(nat_interface_is_outside (i)))
2318 pool_foreach (i, sm->output_feature_interfaces,
2320 if (i->sw_if_index == sw_if_index)
2322 if (!(nat_interface_is_outside (i)))
2332 vec_foreach (outside_fib, sm->outside_fibs)
2334 if (outside_fib->fib_index == old_fib_index)
2336 outside_fib->refcount--;
2337 if (!outside_fib->refcount)
2338 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2343 vec_foreach (outside_fib, sm->outside_fibs)
2345 if (outside_fib->fib_index == new_fib_index)
2347 outside_fib->refcount++;
2355 vec_add2 (sm->outside_fibs, outside_fib, 1);
2356 outside_fib->refcount = 1;
2357 outside_fib->fib_index = new_fib_index;
2362 snat_ip4_table_bind (ip4_main_t * im,
2364 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
2366 snat_update_outside_fib (sw_if_index, new_fib_index, old_fib_index);
2370 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2373 ip4_address_t * address,
2375 u32 if_address_index, u32 is_delete);
2378 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2381 ip4_address_t * address,
2383 u32 if_address_index, u32 is_delete);
2386 nat_alloc_addr_and_port_default (snat_address_t * addresses, u32 fib_index,
2387 u32 thread_index, nat_protocol_t proto,
2388 ip4_address_t * addr, u16 * port,
2389 u16 port_per_thread, u32 snat_thread_index);
2392 test_key_calc_split ()
2394 ip4_address_t l_addr;
2395 l_addr.as_u8[0] = 1;
2396 l_addr.as_u8[1] = 1;
2397 l_addr.as_u8[2] = 1;
2398 l_addr.as_u8[3] = 1;
2399 ip4_address_t r_addr;
2400 r_addr.as_u8[0] = 2;
2401 r_addr.as_u8[1] = 2;
2402 r_addr.as_u8[2] = 2;
2403 r_addr.as_u8[3] = 2;
2407 u32 fib_index = 9000001;
2408 u32 thread_index = 3000000001;
2409 u32 session_index = 3000000221;
2410 clib_bihash_kv_16_8_t kv;
2411 init_ed_kv (&kv, l_addr, l_port, r_addr, r_port, fib_index, proto,
2412 thread_index, session_index);
2413 ip4_address_t l_addr2;
2414 ip4_address_t r_addr2;
2415 clib_memset (&l_addr2, 0, sizeof (l_addr2));
2416 clib_memset (&r_addr2, 0, sizeof (r_addr2));
2421 split_ed_kv (&kv, &l_addr2, &r_addr2, &proto2, &fib_index2, &l_port2,
2423 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2424 ASSERT (r_addr.as_u32 == r_addr2.as_u32);
2425 ASSERT (l_port == l_port2);
2426 ASSERT (r_port == r_port2);
2427 ASSERT (proto == proto2);
2428 ASSERT (fib_index == fib_index2);
2429 ASSERT (thread_index == ed_value_get_thread_index (&kv));
2430 ASSERT (session_index == ed_value_get_session_index (&kv));
2434 nat_protocol_t proto3 = ~0;
2435 u64 key = calc_nat_key (l_addr, l_port, fib_index, proto);
2436 split_nat_key (key, &l_addr2, &l_port2, &fib_index2, &proto3);
2437 ASSERT (l_addr.as_u32 == l_addr2.as_u32);
2438 ASSERT (l_port == l_port2);
2439 ASSERT (proto == proto3);
2440 ASSERT (fib_index == fib_index2);
2443 static clib_error_t *
2444 snat_init (vlib_main_t * vm)
2446 snat_main_t *sm = &snat_main;
2447 clib_error_t *error = 0;
2448 ip4_main_t *im = &ip4_main;
2449 ip_lookup_main_t *lm = &im->lookup_main;
2451 vlib_thread_registration_t *tr;
2452 vlib_thread_main_t *tm = vlib_get_thread_main ();
2455 ip4_add_del_interface_address_callback_t cb4;
2458 sm->vnet_main = vnet_get_main ();
2460 sm->ip4_lookup_main = lm;
2461 sm->api_main = vlibapi_get_main ();
2462 sm->first_worker_index = 0;
2463 sm->num_workers = 0;
2465 sm->port_per_thread = 0xffff - 1024;
2466 sm->fq_in2out_index = ~0;
2467 sm->fq_in2out_output_index = ~0;
2468 sm->fq_out2in_index = ~0;
2470 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2471 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2472 sm->forwarding_enabled = 0;
2473 sm->log_class = vlib_log_register_class ("nat", 0);
2474 sm->log_level = SNAT_LOG_ERROR;
2475 sm->mss_clamping = 0;
2477 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2478 sm->error_node_index = node->index;
2480 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2481 sm->pre_in2out_node_index = node->index;
2482 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2483 sm->pre_out2in_node_index = node->index;
2485 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-in2out");
2486 sm->pre_in2out_node_index = node->index;
2488 node = vlib_get_node_by_name (vm, (u8 *) "nat-pre-out2in");
2489 sm->pre_out2in_node_index = node->index;
2491 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2492 sm->in2out_node_index = node->index;
2493 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2494 sm->in2out_output_node_index = node->index;
2495 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2496 sm->in2out_fast_node_index = node->index;
2497 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2498 sm->in2out_slowpath_node_index = node->index;
2499 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2500 sm->in2out_slowpath_output_node_index = node->index;
2502 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2503 sm->ed_in2out_node_index = node->index;
2504 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2505 sm->ed_in2out_slowpath_node_index = node->index;
2507 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2508 sm->out2in_node_index = node->index;
2509 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2510 sm->out2in_fast_node_index = node->index;
2512 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2513 sm->ed_out2in_node_index = node->index;
2514 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2515 sm->ed_out2in_slowpath_node_index = node->index;
2517 node = vlib_get_node_by_name (vm, (u8 *) "nat44-det-in2out");
2518 sm->det_in2out_node_index = node->index;
2519 node = vlib_get_node_by_name (vm, (u8 *) "nat44-det-out2in");
2520 sm->det_out2in_node_index = node->index;
2522 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2523 sm->hairpinning_node_index = node->index;
2524 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2525 sm->hairpin_dst_node_index = node->index;
2526 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2527 sm->hairpin_src_node_index = node->index;
2528 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpinning");
2529 sm->ed_hairpinning_node_index = node->index;
2530 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-dst");
2531 sm->ed_hairpin_dst_node_index = node->index;
2532 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-src");
2533 sm->ed_hairpin_src_node_index = node->index;
2535 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2538 tr = (vlib_thread_registration_t *) p[0];
2541 sm->num_workers = tr->count;
2542 sm->first_worker_index = tr->first_index;
2546 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2548 /* Use all available workers by default */
2549 if (sm->num_workers > 1)
2551 for (i = 0; i < sm->num_workers; i++)
2552 bitmap = clib_bitmap_set (bitmap, i, 1);
2553 snat_set_workers (bitmap);
2554 clib_bitmap_free (bitmap);
2558 sm->per_thread_data[0].snat_thread_index = 0;
2561 error = snat_api_init (vm, sm);
2565 /* Set up the interface address add/del callback */
2566 cb4.function = snat_ip4_add_del_interface_address_cb;
2567 cb4.function_opaque = 0;
2569 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2571 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2572 cb4.function_opaque = 0;
2574 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2576 nat_dpo_module_init ();
2579 sm->total_users.name = "total-users";
2580 sm->total_users.stat_segment_name = "/nat44/total-users";
2581 vlib_validate_simple_counter (&sm->total_users, 0);
2582 vlib_zero_simple_counter (&sm->total_users, 0);
2583 sm->total_sessions.name = "total-sessions";
2584 sm->total_sessions.stat_segment_name = "/nat44/total-sessions";
2585 vlib_validate_simple_counter (&sm->total_sessions, 0);
2586 vlib_zero_simple_counter (&sm->total_sessions, 0);
2587 sm->user_limit_reached.name = "user-limit-reached";
2588 sm->user_limit_reached.stat_segment_name = "/nat44/user-limit-reached";
2589 vlib_validate_simple_counter (&sm->user_limit_reached, 0);
2590 vlib_zero_simple_counter (&sm->user_limit_reached, 0);
2592 /* Init IPFIX logging */
2593 snat_ipfix_logging_init (vm);
2596 error = nat64_init (vm);
2600 ip4_table_bind_callback_t cbt4 = {
2601 .function = snat_ip4_table_bind,
2603 vec_add1 (ip4_main.table_bind_callbacks, cbt4);
2605 nat_fib_src_hi = fib_source_allocate ("nat-hi",
2606 FIB_SOURCE_PRIORITY_HI,
2607 FIB_SOURCE_BH_SIMPLE);
2608 nat_fib_src_low = fib_source_allocate ("nat-low",
2609 FIB_SOURCE_PRIORITY_LOW,
2610 FIB_SOURCE_BH_SIMPLE);
2612 test_key_calc_split ();
2616 VLIB_INIT_FUNCTION (snat_init);
2619 snat_free_outside_address_and_port (snat_address_t * addresses,
2621 ip4_address_t * addr,
2622 u16 port, nat_protocol_t protocol)
2626 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2628 for (address_index = 0; address_index < vec_len (addresses);
2631 if (addresses[address_index].addr.as_u32 == addr->as_u32)
2635 ASSERT (address_index < vec_len (addresses));
2637 a = addresses + address_index;
2641 #define _(N, i, n, s) \
2642 case NAT_PROTOCOL_##N: \
2643 ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \
2644 --a->busy_##n##_port_refcounts[port_host_byte_order]; \
2645 a->busy_##n##_ports--; \
2646 a->busy_##n##_ports_per_thread[thread_index]--; \
2648 foreach_nat_protocol
2651 nat_elog_info ("unknown protocol");
2657 nat_set_outside_address_and_port (snat_address_t * addresses,
2658 u32 thread_index, ip4_address_t addr,
2659 u16 port, nat_protocol_t protocol)
2661 snat_address_t *a = 0;
2663 u16 port_host_byte_order = clib_net_to_host_u16 (port);
2665 for (address_index = 0; address_index < vec_len (addresses);
2668 if (addresses[address_index].addr.as_u32 != addr.as_u32)
2671 a = addresses + address_index;
2674 #define _(N, j, n, s) \
2675 case NAT_PROTOCOL_##N: \
2676 if (a->busy_##n##_port_refcounts[port_host_byte_order]) \
2677 return VNET_API_ERROR_INSTANCE_IN_USE; \
2678 ++a->busy_##n##_port_refcounts[port_host_byte_order]; \
2679 a->busy_##n##_ports_per_thread[thread_index]++; \
2680 a->busy_##n##_ports++; \
2682 foreach_nat_protocol
2685 nat_elog_info ("unknown protocol");
2690 return VNET_API_ERROR_NO_SUCH_ENTRY;
2694 snat_static_mapping_match (snat_main_t * sm,
2695 ip4_address_t match_addr,
2697 u32 match_fib_index,
2698 nat_protocol_t match_protocol,
2699 ip4_address_t * mapping_addr,
2701 u32 * mapping_fib_index,
2704 twice_nat_type_t * twice_nat,
2705 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2706 u8 * is_identity_nat)
2708 clib_bihash_kv_8_8_t kv, value;
2709 snat_static_mapping_t *m;
2710 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2711 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2713 nat44_lb_addr_port_t *local;
2717 mapping_hash = &sm->static_mapping_by_external;
2718 init_nat_k (&kv, match_addr, match_port, 0, match_protocol);
2719 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2721 /* Try address only mapping */
2722 init_nat_k (&kv, match_addr, 0, 0, 0);
2723 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2730 init_nat_k (&kv, match_addr, match_port, match_fib_index,
2732 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2734 /* Try address only mapping */
2735 init_nat_k (&kv, match_addr, 0, match_fib_index, 0);
2736 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2742 m = pool_elt_at_index (sm->static_mappings, value.value);
2746 if (is_lb_static_mapping (m))
2748 if (PREDICT_FALSE (lb != 0))
2749 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2750 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
2756 local = pool_elt_at_index (m->locals, backend_index);
2757 *mapping_addr = local->addr;
2758 *mapping_port = local->port;
2759 *mapping_fib_index = local->fib_index;
2762 // pick locals matching this worker
2763 if (PREDICT_FALSE (sm->num_workers > 1))
2765 u32 thread_index = vlib_get_thread_index ();
2767 pool_foreach_index (i, m->locals,
2769 local = pool_elt_at_index (m->locals, i);
2772 .src_address = local->addr,
2775 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
2782 ASSERT (vec_len (tmp) != 0);
2787 pool_foreach_index (i, m->locals,
2793 hi = vec_len (tmp) - 1;
2794 local = pool_elt_at_index (m->locals, tmp[hi]);
2795 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2798 mid = ((hi - lo) >> 1) + lo;
2799 local = pool_elt_at_index (m->locals, tmp[mid]);
2800 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2802 local = pool_elt_at_index (m->locals, tmp[lo]);
2803 if (!(local->prefix >= rand))
2805 *mapping_addr = local->addr;
2806 *mapping_port = local->port;
2807 *mapping_fib_index = local->fib_index;
2810 if (nat_affinity_create_and_lock (ext_host_addr[0], match_addr,
2811 match_protocol, match_port,
2812 tmp[lo], m->affinity,
2813 m->affinity_per_service_list_head_index))
2814 nat_elog_info ("create affinity record failed");
2820 if (PREDICT_FALSE (lb != 0))
2822 *mapping_fib_index = m->fib_index;
2823 *mapping_addr = m->local_addr;
2824 /* Address only mapping doesn't change port */
2825 *mapping_port = is_addr_only_static_mapping (m) ? match_port
2831 *mapping_addr = m->external_addr;
2832 /* Address only mapping doesn't change port */
2833 *mapping_port = is_addr_only_static_mapping (m) ? match_port
2835 *mapping_fib_index = sm->outside_fib_index;
2839 if (PREDICT_FALSE (is_addr_only != 0))
2840 *is_addr_only = is_addr_only_static_mapping (m);
2842 if (PREDICT_FALSE (twice_nat != 0))
2843 *twice_nat = m->twice_nat;
2845 if (PREDICT_FALSE (is_identity_nat != 0))
2846 *is_identity_nat = is_identity_static_mapping (m);
2852 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2855 nat_protocol_t proto,
2856 ip4_address_t * addr,
2858 u16 port_per_thread,
2859 u32 snat_thread_index)
2861 snat_main_t *sm = &snat_main;
2863 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, proto,
2864 addr, port, port_per_thread,
2869 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2872 nat_protocol_t proto,
2873 ip4_address_t * addr,
2875 u16 port_per_thread, u32 snat_thread_index)
2878 snat_address_t *a, *ga = 0;
2881 for (i = 0; i < vec_len (addresses); i++)
2886 #define _(N, j, n, s) \
2887 case NAT_PROTOCOL_##N: \
2888 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2890 if (a->fib_index == fib_index) \
2894 portnum = (port_per_thread * \
2895 snat_thread_index) + \
2896 snat_random_port(0, port_per_thread - 1) + 1024; \
2897 if (a->busy_##n##_port_refcounts[portnum]) \
2899 --a->busy_##n##_port_refcounts[portnum]; \
2900 a->busy_##n##_ports_per_thread[thread_index]++; \
2901 a->busy_##n##_ports++; \
2903 *port = clib_host_to_net_u16(portnum); \
2907 else if (a->fib_index == ~0) \
2913 foreach_nat_protocol
2916 nat_elog_info ("unknown protocol");
2927 #define _(N, j, n, s) \
2928 case NAT_PROTOCOL_##N: \
2931 portnum = (port_per_thread * \
2932 snat_thread_index) + \
2933 snat_random_port(0, port_per_thread - 1) + 1024; \
2934 if (a->busy_##n##_port_refcounts[portnum]) \
2936 ++a->busy_##n##_port_refcounts[portnum]; \
2937 a->busy_##n##_ports_per_thread[thread_index]++; \
2938 a->busy_##n##_ports++; \
2940 *port = clib_host_to_net_u16(portnum); \
2944 foreach_nat_protocol
2947 nat_elog_info ("unknown protocol");
2952 /* Totally out of translations to use... */
2953 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2958 nat_alloc_addr_and_port_mape (snat_address_t * addresses, u32 fib_index,
2959 u32 thread_index, nat_protocol_t proto,
2960 ip4_address_t * addr, u16 * port,
2961 u16 port_per_thread, u32 snat_thread_index)
2963 snat_main_t *sm = &snat_main;
2964 snat_address_t *a = addresses;
2965 u16 m, ports, portnum, A, j;
2966 m = 16 - (sm->psid_offset + sm->psid_length);
2967 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2969 if (!vec_len (addresses))
2974 #define _(N, i, n, s) \
2975 case NAT_PROTOCOL_##N: \
2976 if (a->busy_##n##_ports < ports) \
2980 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2981 j = snat_random_port(0, pow2_mask(m)); \
2982 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2983 if (a->busy_##n##_port_refcounts[portnum]) \
2985 ++a->busy_##n##_port_refcounts[portnum]; \
2986 a->busy_##n##_ports++; \
2988 *port = clib_host_to_net_u16 (portnum); \
2993 foreach_nat_protocol
2996 nat_elog_info ("unknown protocol");
3001 /* Totally out of translations to use... */
3002 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
3007 nat_alloc_addr_and_port_range (snat_address_t * addresses, u32 fib_index,
3008 u32 thread_index, nat_protocol_t proto,
3009 ip4_address_t * addr, u16 * port,
3010 u16 port_per_thread, u32 snat_thread_index)
3012 snat_main_t *sm = &snat_main;
3013 snat_address_t *a = addresses;
3016 ports = sm->end_port - sm->start_port + 1;
3018 if (!vec_len (addresses))
3023 #define _(N, i, n, s) \
3024 case NAT_PROTOCOL_##N: \
3025 if (a->busy_##n##_ports < ports) \
3029 portnum = snat_random_port(sm->start_port, sm->end_port); \
3030 if (a->busy_##n##_port_refcounts[portnum]) \
3032 ++a->busy_##n##_port_refcounts[portnum]; \
3033 a->busy_##n##_ports++; \
3035 *port = clib_host_to_net_u16 (portnum); \
3040 foreach_nat_protocol
3043 nat_elog_info ("unknown protocol");
3048 /* Totally out of translations to use... */
3049 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
3054 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
3056 dpo_id_t dpo_v4 = DPO_INVALID;
3057 fib_prefix_t pfx = {
3058 .fp_proto = FIB_PROTOCOL_IP4,
3060 .fp_addr.ip4.as_u32 = addr.as_u32,
3065 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
3066 fib_table_entry_special_dpo_add (0, &pfx, nat_fib_src_hi,
3067 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
3068 dpo_reset (&dpo_v4);
3072 fib_table_entry_special_remove (0, &pfx, nat_fib_src_hi);
3077 format_session_kvp (u8 * s, va_list * args)
3079 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3081 s = format (s, "%U session-index %llu", format_snat_key, v->key, v->value);
3087 format_static_mapping_kvp (u8 * s, va_list * args)
3089 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3091 s = format (s, "%U static-mapping-index %llu",
3092 format_snat_key, v->key, v->value);
3098 format_user_kvp (u8 * s, va_list * args)
3100 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
3105 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
3106 k.fib_index, v->value);
3112 format_ed_session_kvp (u8 * s, va_list * args)
3114 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
3118 ip4_address_t l_addr, r_addr;
3121 split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
3124 "local %U:%d remote %U:%d proto %U fib %d thread-index %u session-index %u",
3125 format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
3126 format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
3127 format_ip_protocol, proto, fib_index,
3128 ed_value_get_session_index (v), ed_value_get_thread_index (v));
3134 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
3137 snat_main_t *sm = &snat_main;
3138 u32 next_worker_index = 0;
3141 next_worker_index = sm->first_worker_index;
3142 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
3143 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
3145 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3146 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3148 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3150 return next_worker_index;
3154 snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
3155 u32 rx_fib_index0, u8 is_output)
3157 snat_main_t *sm = &snat_main;
3160 clib_bihash_kv_8_8_t kv, value;
3161 snat_static_mapping_t *m;
3163 u32 next_worker_index = 0;
3165 /* first try static mappings without port */
3166 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3168 init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
3169 if (!clib_bihash_search_8_8
3170 (&sm->static_mapping_by_external, &kv, &value))
3172 m = pool_elt_at_index (sm->static_mappings, value.value);
3173 return m->workers[0];
3177 proto = ip_proto_to_nat_proto (ip0->protocol);
3178 udp = ip4_next_header (ip0);
3179 port = udp->dst_port;
3181 /* unknown protocol */
3182 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3184 /* use current thread */
3185 return vlib_get_thread_index ();
3188 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
3190 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3191 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3192 if (!icmp_type_is_error_message
3193 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3194 port = vnet_buffer (b)->ip.reass.l4_src_port;
3197 /* if error message, then it's not fragmented and we can access it */
3198 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3199 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3200 void *l4_header = ip4_next_header (inner_ip);
3203 case NAT_PROTOCOL_ICMP:
3204 icmp = (icmp46_header_t *) l4_header;
3205 echo = (icmp_echo_header_t *) (icmp + 1);
3206 port = echo->identifier;
3208 case NAT_PROTOCOL_UDP:
3209 case NAT_PROTOCOL_TCP:
3210 port = ((tcp_udp_header_t *) l4_header)->src_port;
3213 return vlib_get_thread_index ();
3218 /* try static mappings with port */
3219 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3221 init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto);
3222 if (!clib_bihash_search_8_8
3223 (&sm->static_mapping_by_external, &kv, &value))
3225 m = pool_elt_at_index (sm->static_mappings, value.value);
3226 return m->workers[0];
3230 /* worker by outside port */
3231 next_worker_index = sm->first_worker_index;
3232 next_worker_index +=
3233 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3234 return next_worker_index;
3238 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3241 snat_main_t *sm = &snat_main;
3242 u32 next_worker_index = sm->first_worker_index;
3245 clib_bihash_kv_16_8_t kv16, value16;
3246 snat_main_per_thread_data_t *tsm;
3249 if (PREDICT_FALSE (is_output))
3251 u32 fib_index = sm->outside_fib_index;
3252 nat_outside_fib_t *outside_fib;
3253 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3254 fib_prefix_t pfx = {
3255 .fp_proto = FIB_PROTOCOL_IP4,
3258 .ip4.as_u32 = ip->dst_address.as_u32,
3263 udp = ip4_next_header (ip);
3265 switch (vec_len (sm->outside_fibs))
3268 fib_index = sm->outside_fib_index;
3271 fib_index = sm->outside_fibs[0].fib_index;
3275 vec_foreach (outside_fib, sm->outside_fibs)
3277 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3278 if (FIB_NODE_INDEX_INVALID != fei)
3280 if (fib_entry_get_resolving_interface (fei) != ~0)
3282 fib_index = outside_fib->fib_index;
3291 init_ed_k (&kv16, ip->src_address, udp->src_port, ip->dst_address,
3292 udp->dst_port, fib_index, ip->protocol);
3294 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3298 vec_elt_at_index (sm->per_thread_data,
3299 ed_value_get_thread_index (&value16));
3300 next_worker_index += tsm->thread_index;
3302 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3303 next_worker_index, fib_index,
3304 clib_net_to_host_u32 (ip->
3305 src_address.as_u32),
3306 clib_net_to_host_u32 (ip->
3307 dst_address.as_u32));
3309 return next_worker_index;
3313 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3314 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3316 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3317 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3319 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3321 if (PREDICT_TRUE (!is_output))
3323 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3324 next_worker_index, rx_fib_index,
3325 clib_net_to_host_u32 (ip->src_address.as_u32),
3326 clib_net_to_host_u32 (ip->dst_address.as_u32));
3330 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3331 next_worker_index, rx_fib_index,
3332 clib_net_to_host_u32 (ip->src_address.as_u32),
3333 clib_net_to_host_u32 (ip->dst_address.as_u32));
3336 return next_worker_index;
3340 nat44_ed_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip,
3341 u32 rx_fib_index, u8 is_output)
3343 snat_main_t *sm = &snat_main;
3344 clib_bihash_kv_8_8_t kv, value;
3345 clib_bihash_kv_16_8_t kv16, value16;
3346 snat_main_per_thread_data_t *tsm;
3348 u32 proto, next_worker_index = 0;
3351 snat_static_mapping_t *m;
3354 proto = ip_proto_to_nat_proto (ip->protocol);
3356 if (PREDICT_TRUE (proto == NAT_PROTOCOL_UDP || proto == NAT_PROTOCOL_TCP))
3358 udp = ip4_next_header (ip);
3360 init_ed_k (&kv16, ip->dst_address, udp->dst_port, ip->src_address,
3361 udp->src_port, rx_fib_index, ip->protocol);
3363 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3367 vec_elt_at_index (sm->per_thread_data,
3368 ed_value_get_thread_index (&value16));
3369 vnet_buffer2 (b)->nat.ed_out2in_nat_session_index =
3370 ed_value_get_session_index (&value16);
3371 next_worker_index = sm->first_worker_index + tsm->thread_index;
3372 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3373 next_worker_index, rx_fib_index,
3374 clib_net_to_host_u32 (ip->
3375 src_address.as_u32),
3376 clib_net_to_host_u32 (ip->
3377 dst_address.as_u32));
3378 return next_worker_index;
3381 else if (proto == NAT_PROTOCOL_ICMP)
3383 if (!get_icmp_o2i_ed_key (b, ip, rx_fib_index, ~0, ~0, 0, 0, 0, &kv16))
3385 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&sm->out2in_ed,
3389 vec_elt_at_index (sm->per_thread_data,
3390 ed_value_get_thread_index (&value16));
3391 next_worker_index = sm->first_worker_index + tsm->thread_index;
3392 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3393 next_worker_index, rx_fib_index,
3394 clib_net_to_host_u32 (ip->
3395 src_address.as_u32),
3396 clib_net_to_host_u32 (ip->
3397 dst_address.as_u32));
3398 return next_worker_index;
3403 /* first try static mappings without port */
3404 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3406 init_nat_k (&kv, ip->dst_address, 0, 0, 0);
3407 if (!clib_bihash_search_8_8
3408 (&sm->static_mapping_by_external, &kv, &value))
3410 m = pool_elt_at_index (sm->static_mappings, value.value);
3411 next_worker_index = m->workers[0];
3416 /* unknown protocol */
3417 if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
3419 /* use current thread */
3420 next_worker_index = vlib_get_thread_index ();
3424 udp = ip4_next_header (ip);
3425 port = udp->dst_port;
3427 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3429 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3430 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3431 if (!icmp_type_is_error_message
3432 (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
3433 port = vnet_buffer (b)->ip.reass.l4_src_port;
3436 /* if error message, then it's not fragmented and we can access it */
3437 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3438 proto = ip_proto_to_nat_proto (inner_ip->protocol);
3439 void *l4_header = ip4_next_header (inner_ip);
3442 case NAT_PROTOCOL_ICMP:
3443 icmp = (icmp46_header_t *) l4_header;
3444 echo = (icmp_echo_header_t *) (icmp + 1);
3445 port = echo->identifier;
3447 case NAT_PROTOCOL_UDP:
3448 case NAT_PROTOCOL_TCP:
3449 port = ((tcp_udp_header_t *) l4_header)->src_port;
3452 next_worker_index = vlib_get_thread_index ();
3458 /* try static mappings with port */
3459 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3461 init_nat_k (&kv, ip->dst_address, proto, 0, port);
3462 if (!clib_bihash_search_8_8
3463 (&sm->static_mapping_by_external, &kv, &value))
3465 m = pool_elt_at_index (sm->static_mappings, value.value);
3466 if (!is_lb_static_mapping (m))
3468 next_worker_index = m->workers[0];
3472 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3473 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3475 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3477 m->workers[hash & (_vec_len (m->workers) - 1)];
3479 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3484 /* worker by outside port */
3485 next_worker_index = sm->first_worker_index;
3486 next_worker_index +=
3487 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3490 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3491 clib_net_to_host_u32 (ip->src_address.as_u32),
3492 clib_net_to_host_u32 (ip->dst_address.as_u32));
3493 return next_worker_index;
3497 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
3498 ip4_address_t * out_addr, u16 out_port,
3499 ip4_address_t * eh_addr, u16 eh_port,
3500 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3501 u32 fib_index, u16 flags, u32 thread_index)
3503 snat_main_t *sm = &snat_main;
3504 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3507 clib_bihash_kv_8_8_t kv;
3508 vlib_main_t *vm = vlib_get_main ();
3509 f64 now = vlib_time_now (vm);
3510 nat_outside_fib_t *outside_fib;
3511 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3512 fib_prefix_t pfx = {
3513 .fp_proto = FIB_PROTOCOL_IP4,
3516 .ip4.as_u32 = eh_addr->as_u32,
3520 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3522 if (nat_set_outside_address_and_port
3523 (sm->addresses, thread_index, *out_addr, out_port, proto))
3527 u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3531 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
3535 if (sm->endpoint_dependent)
3537 nat_ed_lru_insert (tsm, s, now, nat_proto_to_ip_proto (proto));
3540 s->out2in.addr.as_u32 = out_addr->as_u32;
3541 s->out2in.port = out_port;
3542 s->nat_proto = proto;
3543 s->last_heard = now;
3545 s->ext_host_addr.as_u32 = eh_addr->as_u32;
3546 s->ext_host_port = eh_port;
3547 user_session_increment (sm, u, snat_is_session_static (s));
3548 switch (vec_len (sm->outside_fibs))
3551 s->out2in.fib_index = sm->outside_fib_index;
3554 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
3558 vec_foreach (outside_fib, sm->outside_fibs)
3560 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3561 if (FIB_NODE_INDEX_INVALID != fei)
3563 if (fib_entry_get_resolving_interface (fei) != ~0)
3565 s->out2in.fib_index = outside_fib->fib_index;
3573 init_nat_o2i_kv (&kv, s, s - tsm->sessions);
3574 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
3575 nat_elog_warn ("out2in key add failed");
3577 s->in2out.addr.as_u32 = in_addr->as_u32;
3578 s->in2out.port = in_port;
3579 s->in2out.fib_index = fib_index;
3580 init_nat_i2o_kv (&kv, s, s - tsm->sessions);
3581 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
3582 nat_elog_warn ("in2out key add failed");
3586 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
3587 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3590 snat_main_t *sm = &snat_main;
3591 clib_bihash_kv_8_8_t kv, value;
3594 snat_main_per_thread_data_t *tsm;
3596 if (sm->num_workers > 1)
3598 sm->first_worker_index +
3599 (sm->workers[(clib_net_to_host_u16 (out_port) -
3600 1024) / sm->port_per_thread]);
3602 thread_index = sm->num_workers;
3603 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3605 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
3606 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3609 s = pool_elt_at_index (tsm->sessions, value.value);
3610 nat_free_session_data (sm, s, thread_index, 1);
3611 nat44_delete_session (sm, s, thread_index);
3615 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
3616 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3617 u32 total_pkts, u64 total_bytes, u32 thread_index)
3619 snat_main_t *sm = &snat_main;
3620 clib_bihash_kv_8_8_t kv, value;
3622 snat_main_per_thread_data_t *tsm;
3624 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3626 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
3627 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3630 s = pool_elt_at_index (tsm->sessions, value.value);
3631 s->total_pkts = total_pkts;
3632 s->total_bytes = total_bytes;
3636 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
3637 ip4_address_t * out_addr, u16 out_port,
3638 ip4_address_t * eh_addr, u16 eh_port,
3639 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3640 u32 fib_index, u16 flags, u32 thread_index)
3642 snat_main_t *sm = &snat_main;
3643 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
3645 clib_bihash_kv_16_8_t kv;
3646 vlib_main_t *vm = vlib_get_main ();
3647 f64 now = vlib_time_now (vm);
3648 nat_outside_fib_t *outside_fib;
3649 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3650 fib_prefix_t pfx = {
3651 .fp_proto = FIB_PROTOCOL_IP4,
3654 .ip4.as_u32 = eh_addr->as_u32,
3659 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3661 if (nat_set_outside_address_and_port
3662 (sm->addresses, thread_index, *out_addr, out_port, proto))
3666 if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
3668 if (nat_set_outside_address_and_port
3669 (sm->addresses, thread_index, *ehn_addr, ehn_port, proto))
3673 s = nat_ed_session_alloc (sm, thread_index, now, proto);
3677 s->last_heard = now;
3679 s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
3680 s->ext_host_nat_port = s->ext_host_port = eh_port;
3681 if (is_twice_nat_session (s))
3683 s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
3684 s->ext_host_nat_port = ehn_port;
3686 switch (vec_len (sm->outside_fibs))
3689 s->out2in.fib_index = sm->outside_fib_index;
3692 s->out2in.fib_index = sm->outside_fibs[0].fib_index;
3696 vec_foreach (outside_fib, sm->outside_fibs)
3698 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3699 if (FIB_NODE_INDEX_INVALID != fei)
3701 if (fib_entry_get_resolving_interface (fei) != ~0)
3703 s->out2in.fib_index = outside_fib->fib_index;
3711 s->nat_proto = proto;
3712 s->out2in.addr.as_u32 = out_addr->as_u32;
3713 s->out2in.port = out_port;
3715 s->in2out.addr.as_u32 = in_addr->as_u32;
3716 s->in2out.port = in_port;
3717 s->in2out.fib_index = fib_index;
3719 init_ed_kv (&kv, *in_addr, in_port, s->ext_host_nat_addr,
3720 s->ext_host_nat_port, fib_index, nat_proto_to_ip_proto (proto),
3721 thread_index, s - tsm->sessions);
3722 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
3723 nat_elog_warn ("in2out key add failed");
3725 init_ed_kv (&kv, *out_addr, out_port, *eh_addr, eh_port,
3726 s->out2in.fib_index, nat_proto_to_ip_proto (proto),
3727 thread_index, s - tsm->sessions);
3728 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 1))
3729 nat_elog_warn ("out2in key add failed");
3733 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
3734 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3735 u32 fib_index, u32 ti)
3737 snat_main_t *sm = &snat_main;
3738 clib_bihash_kv_16_8_t kv, value;
3741 snat_main_per_thread_data_t *tsm;
3743 if (sm->num_workers > 1)
3745 sm->first_worker_index +
3746 (sm->workers[(clib_net_to_host_u16 (out_port) -
3747 1024) / sm->port_per_thread]);
3749 thread_index = sm->num_workers;
3750 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3752 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
3753 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
3756 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3757 nat_free_session_data (sm, s, thread_index, 1);
3758 nat44_delete_session (sm, s, thread_index);
3762 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
3763 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3764 u32 fib_index, u32 total_pkts, u64 total_bytes,
3767 snat_main_t *sm = &snat_main;
3768 clib_bihash_kv_16_8_t kv, value;
3770 snat_main_per_thread_data_t *tsm;
3772 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3774 init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
3775 if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
3778 s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
3779 s->total_pkts = total_pkts;
3780 s->total_bytes = total_bytes;
3784 nat_calc_bihash_buckets (u32 n_elts)
3786 return 1 << (max_log2 (n_elts >> 1) + 1);
3790 nat_calc_bihash_memory (u32 n_buckets, uword kv_size)
3792 return n_buckets * (8 + kv_size * 4);
3796 nat44_db_init (snat_main_per_thread_data_t * tsm)
3798 snat_main_t *sm = &snat_main;
3800 pool_alloc (tsm->sessions, sm->max_translations_per_thread);
3801 pool_alloc (tsm->lru_pool, sm->max_translations_per_thread);
3805 pool_get (tsm->lru_pool, head);
3806 tsm->tcp_trans_lru_head_index = head - tsm->lru_pool;
3807 clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index);
3809 pool_get (tsm->lru_pool, head);
3810 tsm->tcp_estab_lru_head_index = head - tsm->lru_pool;
3811 clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index);
3813 pool_get (tsm->lru_pool, head);
3814 tsm->udp_lru_head_index = head - tsm->lru_pool;
3815 clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index);
3817 pool_get (tsm->lru_pool, head);
3818 tsm->icmp_lru_head_index = head - tsm->lru_pool;
3819 clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index);
3821 pool_get (tsm->lru_pool, head);
3822 tsm->unk_proto_lru_head_index = head - tsm->lru_pool;
3823 clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index);
3825 if (sm->endpoint_dependent)
3827 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
3828 sm->translation_buckets,
3829 sm->translation_memory_size);
3830 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
3831 format_ed_session_kvp);
3835 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
3836 sm->translation_buckets,
3837 sm->translation_memory_size);
3838 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out, format_session_kvp);
3839 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
3840 sm->translation_buckets,
3841 sm->translation_memory_size);
3842 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in, format_session_kvp);
3845 // TODO: resolve static mappings (put only to !ED)
3846 pool_alloc (tsm->list_pool, sm->max_translations_per_thread);
3847 clib_bihash_init_8_8 (&tsm->user_hash, "users", sm->user_buckets,
3848 sm->user_memory_size);
3849 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
3853 nat44_db_free (snat_main_per_thread_data_t * tsm)
3855 snat_main_t *sm = &snat_main;
3857 pool_free (tsm->sessions);
3858 pool_free (tsm->lru_pool);
3860 if (sm->endpoint_dependent)
3862 clib_bihash_free_16_8 (&tsm->in2out_ed);
3866 clib_bihash_free_8_8 (&tsm->in2out);
3867 clib_bihash_free_8_8 (&tsm->out2in);
3870 // TODO: resolve static mappings (put only to !ED)
3871 pool_free (tsm->users);
3872 pool_free (tsm->list_pool);
3873 clib_bihash_free_8_8 (&tsm->user_hash);
3877 nat44_sessions_clear ()
3879 snat_main_t *sm = &snat_main;
3880 snat_main_per_thread_data_t *tsm;
3882 if (sm->endpoint_dependent)
3884 clib_bihash_free_16_8 (&sm->out2in_ed);
3885 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
3886 clib_max (1, sm->num_workers) *
3887 sm->translation_buckets,
3888 clib_max (1, sm->num_workers) *
3889 sm->translation_memory_size);
3890 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
3891 format_ed_session_kvp);
3895 vec_foreach (tsm, sm->per_thread_data)
3899 nat44_db_free (tsm);
3900 nat44_db_init (tsm);
3902 ti = tsm->snat_thread_index;
3903 vlib_set_simple_counter (&sm->total_users, ti, 0, 0);
3904 vlib_set_simple_counter (&sm->total_sessions, ti, 0, 0);
3909 static clib_error_t *
3910 snat_config (vlib_main_t * vm, unformat_input_t * input)
3912 snat_main_t *sm = &snat_main;
3913 snat_main_per_thread_data_t *tsm;
3915 u32 static_mapping_buckets = 1024;
3916 uword static_mapping_memory_size = 64 << 20;
3918 u32 nat64_bib_buckets = 1024;
3919 u32 nat64_bib_memory_size = 128 << 20;
3921 u32 nat64_st_buckets = 2048;
3922 uword nat64_st_memory_size = 256 << 20;
3924 u32 max_users_per_thread = 0;
3925 u32 user_memory_size = 0;
3926 u32 max_translations_per_thread = 0;
3927 u32 translation_memory_size = 0;
3929 u32 max_translations_per_user = ~0;
3931 u32 outside_vrf_id = 0;
3932 u32 outside_ip6_vrf_id = 0;
3933 u32 inside_vrf_id = 0;
3934 u8 static_mapping_only = 0;
3935 u8 static_mapping_connection_tracking = 0;
3937 u32 udp_timeout = SNAT_UDP_TIMEOUT;
3938 u32 icmp_timeout = SNAT_ICMP_TIMEOUT;
3939 u32 tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
3940 u32 tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
3942 sm->deterministic = 0;
3944 sm->endpoint_dependent = 0;
3946 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3949 (input, "max translations per thread %d",
3950 &max_translations_per_thread))
3952 else if (unformat (input, "udp timeout %d", &udp_timeout))
3954 else if (unformat (input, "icmp timeout %d", &icmp_timeout))
3956 else if (unformat (input, "tcp transitory timeout %d",
3957 &tcp_transitory_timeout));
3958 else if (unformat (input, "tcp established timeout %d",
3959 &tcp_established_timeout));
3960 else if (unformat (input, "translation hash memory %d",
3961 &translation_memory_size));
3964 (input, "max users per thread %d", &max_users_per_thread))
3966 else if (unformat (input, "user hash memory %d", &user_memory_size))
3968 else if (unformat (input, "max translations per user %d",
3969 &max_translations_per_user))
3971 else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
3973 else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
3975 else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
3977 else if (unformat (input, "static mapping only"))
3979 static_mapping_only = 1;
3980 if (unformat (input, "connection tracking"))
3981 static_mapping_connection_tracking = 1;
3983 else if (unformat (input, "deterministic"))
3984 sm->deterministic = 1;
3985 else if (unformat (input, "nat64 bib hash buckets %d",
3986 &nat64_bib_buckets))
3988 else if (unformat (input, "nat64 bib hash memory %d",
3989 &nat64_bib_memory_size))
3992 if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
3994 else if (unformat (input, "nat64 st hash memory %d",
3995 &nat64_st_memory_size))
3997 else if (unformat (input, "out2in dpo"))
3999 else if (unformat (input, "endpoint-dependent"))
4000 sm->endpoint_dependent = 1;
4002 return clib_error_return (0, "unknown input '%U'",
4003 format_unformat_error, input);
4006 if (sm->deterministic && sm->endpoint_dependent)
4007 return clib_error_return (0,
4008 "deterministic and endpoint-dependent modes are mutually exclusive");
4010 if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
4011 return clib_error_return (0,
4012 "static mapping only mode available only for simple nat");
4014 if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
4015 return clib_error_return (0,
4016 "out2in dpo mode available only for simple nat");
4017 if (sm->endpoint_dependent && max_users_per_thread > 0)
4019 return clib_error_return (0,
4020 "setting 'max users' in endpoint-dependent mode is not supported");
4023 if (sm->endpoint_dependent && max_translations_per_user != ~0)
4025 return clib_error_return (0,
4026 "setting 'max translations per user' in endpoint-dependent mode is not supported");
4029 /* optionally configurable timeouts for testing purposes */
4030 sm->udp_timeout = udp_timeout;
4031 sm->tcp_transitory_timeout = tcp_transitory_timeout;
4032 sm->tcp_established_timeout = tcp_established_timeout;
4033 sm->icmp_timeout = icmp_timeout;
4035 if (0 == max_users_per_thread)
4037 max_users_per_thread = 1024;
4039 sm->max_users_per_thread = max_users_per_thread;
4040 sm->user_buckets = nat_calc_bihash_buckets (sm->max_users_per_thread);
4042 if (0 == max_translations_per_thread)
4044 // default value based on legacy setting of load factor 10 * default
4045 // translation buckets 1024
4046 max_translations_per_thread = 10 * 1024;
4048 sm->max_translations_per_thread = max_translations_per_thread;
4049 sm->translation_buckets =
4050 nat_calc_bihash_buckets (sm->max_translations_per_thread);
4051 if (0 == translation_memory_size)
4053 translation_memory_size =
4054 nat_calc_bihash_memory (sm->translation_buckets,
4055 sizeof (clib_bihash_16_8_t));
4057 sm->translation_memory_size = translation_memory_size;
4058 if (0 == user_memory_size)
4061 nat_calc_bihash_memory (sm->max_users_per_thread,
4062 sizeof (clib_bihash_8_8_t));
4064 sm->user_memory_size = user_memory_size;
4065 vec_add1 (sm->max_translations_per_fib, sm->max_translations_per_thread);
4067 sm->max_translations_per_user = max_translations_per_user == ~0 ?
4068 sm->max_translations_per_thread : max_translations_per_user;
4070 sm->outside_vrf_id = outside_vrf_id;
4071 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
4074 sm->inside_vrf_id = inside_vrf_id;
4075 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
4078 sm->static_mapping_only = static_mapping_only;
4079 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
4081 nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
4082 nat64_st_memory_size);
4084 if (sm->deterministic)
4086 sm->in2out_node_index = snat_det_in2out_node.index;
4087 sm->in2out_output_node_index = ~0;
4088 sm->out2in_node_index = snat_det_out2in_node.index;
4089 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
4090 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
4094 if (sm->endpoint_dependent)
4096 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
4097 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
4099 sm->in2out_node_index = nat44_ed_in2out_node.index;
4100 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
4101 sm->out2in_node_index = nat44_ed_out2in_node.index;
4103 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
4104 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
4105 nat_affinity_init (vm);
4106 nat_ha_init (vm, nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb,
4108 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
4109 clib_max (1, sm->num_workers) *
4110 sm->translation_buckets,
4111 clib_max (1, sm->num_workers) *
4112 sm->translation_memory_size);
4113 clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
4114 format_ed_session_kvp);
4118 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
4119 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
4121 sm->in2out_node_index = snat_in2out_node.index;
4122 sm->in2out_output_node_index = snat_in2out_output_node.index;
4123 sm->out2in_node_index = snat_out2in_node.index;
4125 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
4126 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
4127 nat_ha_init (vm, nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
4129 if (!static_mapping_only ||
4130 (static_mapping_only && static_mapping_connection_tracking))
4133 vec_foreach (tsm, sm->per_thread_data)
4135 nat44_db_init (tsm);
4141 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
4142 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
4144 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
4145 "static_mapping_by_local", static_mapping_buckets,
4146 static_mapping_memory_size);
4147 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
4148 format_static_mapping_kvp);
4150 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
4151 "static_mapping_by_external",
4152 static_mapping_buckets,
4153 static_mapping_memory_size);
4154 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
4155 format_static_mapping_kvp);
4161 VLIB_CONFIG_FUNCTION (snat_config, "nat");
4164 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
4167 ip4_address_t * address,
4169 u32 if_address_index, u32 is_delete)
4171 snat_main_t *sm = &snat_main;
4172 snat_static_map_resolve_t *rp;
4173 snat_static_mapping_t *m;
4174 clib_bihash_kv_8_8_t kv, value;
4176 ip4_address_t l_addr;
4178 for (i = 0; i < vec_len (sm->to_resolve); i++)
4180 rp = sm->to_resolve + i;
4181 if (rp->addr_only == 0)
4183 if (rp->sw_if_index == sw_if_index)
4190 init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port,
4191 sm->outside_fib_index, rp->addr_only ? 0 : rp->proto);
4192 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
4195 m = pool_elt_at_index (sm->static_mappings, value.value);
4199 /* Don't trip over lease renewal, static config */
4209 /* Indetity mapping? */
4210 if (rp->l_addr.as_u32 == 0)
4211 l_addr.as_u32 = address[0].as_u32;
4213 l_addr.as_u32 = rp->l_addr.as_u32;
4214 /* Add the static mapping */
4215 rv = snat_add_static_mapping (l_addr,
4220 rp->addr_only, ~0 /* sw_if_index */ ,
4221 rp->proto, !is_delete, rp->twice_nat,
4222 rp->out2in_only, rp->tag, rp->identity_nat);
4224 nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
4228 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
4231 ip4_address_t * address,
4233 u32 if_address_index, u32 is_delete)
4235 snat_main_t *sm = &snat_main;
4236 snat_static_map_resolve_t *rp;
4237 ip4_address_t l_addr;
4241 snat_address_t *addresses = sm->addresses;
4243 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
4245 if (sw_if_index == sm->auto_add_sw_if_indices[i])
4249 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
4252 addresses = sm->twice_nat_addresses;
4253 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
4262 /* Don't trip over lease renewal, static config */
4263 for (j = 0; j < vec_len (addresses); j++)
4264 if (addresses[j].addr.as_u32 == address->as_u32)
4267 (void) snat_add_address (sm, address, ~0, twice_nat);
4268 /* Scan static map resolution vector */
4269 for (j = 0; j < vec_len (sm->to_resolve); j++)
4271 rp = sm->to_resolve + j;
4274 /* On this interface? */
4275 if (rp->sw_if_index == sw_if_index)
4277 /* Indetity mapping? */
4278 if (rp->l_addr.as_u32 == 0)
4279 l_addr.as_u32 = address[0].as_u32;
4281 l_addr.as_u32 = rp->l_addr.as_u32;
4282 /* Add the static mapping */
4283 rv = snat_add_static_mapping (l_addr,
4289 ~0 /* sw_if_index */ ,
4291 rp->is_add, rp->twice_nat,
4292 rp->out2in_only, rp->tag,
4295 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
4303 (void) snat_del_address (sm, address[0], 1, twice_nat);
4310 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
4313 ip4_main_t *ip4_main = sm->ip4_main;
4314 ip4_address_t *first_int_addr;
4315 snat_static_map_resolve_t *rp;
4316 u32 *indices_to_delete = 0;
4318 u32 *auto_add_sw_if_indices =
4320 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
4322 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
4325 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
4327 if (auto_add_sw_if_indices[i] == sw_if_index)
4331 /* if have address remove it */
4333 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
4336 for (j = 0; j < vec_len (sm->to_resolve); j++)
4338 rp = sm->to_resolve + j;
4339 if (rp->sw_if_index == sw_if_index)
4340 vec_add1 (indices_to_delete, j);
4342 if (vec_len (indices_to_delete))
4344 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
4345 vec_del1 (sm->to_resolve, j);
4346 vec_free (indices_to_delete);
4350 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
4352 vec_del1 (sm->auto_add_sw_if_indices, i);
4355 return VNET_API_ERROR_VALUE_EXIST;
4362 return VNET_API_ERROR_NO_SUCH_ENTRY;
4364 /* add to the auto-address list */
4366 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
4368 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
4370 /* If the address is already bound - or static - add it now */
4372 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
4378 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4379 nat_protocol_t proto, u32 vrf_id, int is_in)
4381 snat_main_per_thread_data_t *tsm;
4382 clib_bihash_kv_8_8_t kv, value;
4384 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4386 clib_bihash_8_8_t *t;
4388 if (sm->endpoint_dependent)
4389 return VNET_API_ERROR_UNSUPPORTED;
4391 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4392 if (sm->num_workers > 1)
4394 vec_elt_at_index (sm->per_thread_data,
4395 sm->worker_in2out_cb (&ip, fib_index, 0));
4397 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4399 init_nat_k (&kv, *addr, port, fib_index, proto);
4400 t = is_in ? &tsm->in2out : &tsm->out2in;
4401 if (!clib_bihash_search_8_8 (t, &kv, &value))
4403 if (pool_is_free_index (tsm->sessions, value.value))
4404 return VNET_API_ERROR_UNSPECIFIED;
4406 s = pool_elt_at_index (tsm->sessions, value.value);
4407 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4408 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4412 return VNET_API_ERROR_NO_SUCH_ENTRY;
4416 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4417 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4418 u32 vrf_id, int is_in)
4421 clib_bihash_16_8_t *t;
4422 clib_bihash_kv_16_8_t kv, value;
4423 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4425 snat_main_per_thread_data_t *tsm;
4427 if (!sm->endpoint_dependent)
4428 return VNET_API_ERROR_FEATURE_DISABLED;
4430 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4431 if (sm->num_workers > 1)
4433 vec_elt_at_index (sm->per_thread_data,
4434 sm->worker_in2out_cb (&ip, fib_index, 0));
4436 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4438 t = is_in ? &tsm->in2out_ed : &sm->out2in_ed;
4439 init_ed_k (&kv, *addr, port, *eh_addr, eh_port, fib_index, proto);
4440 if (clib_bihash_search_16_8 (t, &kv, &value))
4442 return VNET_API_ERROR_NO_SUCH_ENTRY;
4445 if (pool_is_free_index (tsm->sessions, value.value))
4446 return VNET_API_ERROR_UNSPECIFIED;
4447 s = pool_elt_at_index (tsm->sessions, value.value);
4448 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4449 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
4454 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
4456 snat_main_t *sm = &snat_main;
4458 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
4459 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
4461 sm->psid_offset = psid_offset;
4462 sm->psid_length = psid_length;
4466 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
4468 snat_main_t *sm = &snat_main;
4470 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
4471 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
4472 sm->start_port = start_port;
4473 sm->end_port = end_port;
4477 nat_set_alloc_addr_and_port_default (void)
4479 snat_main_t *sm = &snat_main;
4481 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
4482 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
4485 VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
4486 vlib_node_runtime_t * node,
4487 vlib_frame_t * frame)
4493 VLIB_REGISTER_NODE (nat_default_node) = {
4494 .name = "nat-default",
4495 .vector_size = sizeof (u32),
4497 .type = VLIB_NODE_TYPE_INTERNAL,
4499 .n_next_nodes = NAT_N_NEXT,
4501 [NAT_NEXT_DROP] = "error-drop",
4502 [NAT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
4503 [NAT_NEXT_IN2OUT_ED_FAST_PATH] = "nat44-ed-in2out",
4504 [NAT_NEXT_IN2OUT_ED_SLOW_PATH] = "nat44-ed-in2out-slowpath",
4505 [NAT_NEXT_IN2OUT_ED_OUTPUT_SLOW_PATH] = "nat44-ed-in2out-output-slowpath",
4506 [NAT_NEXT_OUT2IN_ED_FAST_PATH] = "nat44-ed-out2in",
4507 [NAT_NEXT_OUT2IN_ED_SLOW_PATH] = "nat44-ed-out2in-slowpath",
4508 [NAT_NEXT_IN2OUT_CLASSIFY] = "nat44-in2out-worker-handoff",
4509 [NAT_NEXT_OUT2IN_CLASSIFY] = "nat44-out2in-worker-handoff",
4515 * fd.io coding-style-patch-verification: ON
4518 * eval: (c-set-style "gnu")