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/nat66.h>
28 #include <nat/dslite.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_affinity.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
34 #include <vnet/fib/fib_table.h>
35 #include <vnet/fib/ip4_fib.h>
37 #include <vpp/app/version.h>
39 snat_main_t snat_main;
43 /* Hook up input features */
44 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
45 .arc_name = "ip4-unicast",
46 .node_name = "nat44-in2out",
47 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
49 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
50 .arc_name = "ip4-unicast",
51 .node_name = "nat44-out2in",
52 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
53 "ip4-dhcp-client-detect"),
55 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
56 .arc_name = "ip4-unicast",
57 .node_name = "nat44-classify",
58 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
60 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "nat44-det-in2out",
63 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
65 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
66 .arc_name = "ip4-unicast",
67 .node_name = "nat44-det-out2in",
68 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
69 "ip4-dhcp-client-detect"),
71 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
72 .arc_name = "ip4-unicast",
73 .node_name = "nat44-det-classify",
74 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
76 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
77 .arc_name = "ip4-unicast",
78 .node_name = "nat44-ed-in2out",
79 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
81 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
82 .arc_name = "ip4-unicast",
83 .node_name = "nat44-ed-out2in",
84 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
85 "ip4-dhcp-client-detect"),
87 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
88 .arc_name = "ip4-unicast",
89 .node_name = "nat44-ed-classify",
90 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
92 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
93 .arc_name = "ip4-unicast",
94 .node_name = "nat44-in2out-worker-handoff",
95 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
97 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
98 .arc_name = "ip4-unicast",
99 .node_name = "nat44-out2in-worker-handoff",
100 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
101 "ip4-dhcp-client-detect"),
103 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
104 .arc_name = "ip4-unicast",
105 .node_name = "nat44-handoff-classify",
106 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
108 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
109 .arc_name = "ip4-unicast",
110 .node_name = "nat44-in2out-fast",
111 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
113 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
114 .arc_name = "ip4-unicast",
115 .node_name = "nat44-out2in-fast",
116 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
117 "ip4-dhcp-client-detect"),
119 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
120 .arc_name = "ip4-unicast",
121 .node_name = "nat44-hairpin-dst",
122 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
124 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
125 .arc_name = "ip4-unicast",
126 .node_name = "nat44-ed-hairpin-dst",
127 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
130 /* Hook up output features */
131 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
132 .arc_name = "ip4-output",
133 .node_name = "nat44-in2out-output",
134 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
136 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
137 .arc_name = "ip4-output",
138 .node_name = "nat44-in2out-output-worker-handoff",
139 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
141 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
142 .arc_name = "ip4-output",
143 .node_name = "nat44-hairpin-src",
144 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
146 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
147 .arc_name = "ip4-output",
148 .node_name = "nat44-ed-in2out-output",
149 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
151 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
152 .arc_name = "ip4-output",
153 .node_name = "nat44-ed-hairpin-src",
154 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
157 /* Hook up ip4-local features */
158 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
160 .arc_name = "ip4-local",
161 .node_name = "nat44-hairpinning",
162 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
164 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
166 .arc_name = "ip4-local",
167 .node_name = "nat44-ed-hairpinning",
168 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
172 VLIB_PLUGIN_REGISTER () = {
173 .version = VPP_BUILD_VER,
174 .description = "Network Address Translation (NAT)",
179 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
182 snat_session_key_t key;
183 clib_bihash_kv_8_8_t kv;
184 nat_ed_ses_key_t ed_key;
185 clib_bihash_kv_16_8_t ed_kv;
186 snat_main_per_thread_data_t *tsm =
187 vec_elt_at_index (sm->per_thread_data, thread_index);
189 if (is_fwd_bypass_session (s))
191 if (snat_is_unk_proto_session (s))
193 ed_key.proto = s->in2out.port;
199 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
200 ed_key.l_port = s->in2out.port;
201 ed_key.r_port = s->ext_host_port;
203 ed_key.l_addr = s->in2out.addr;
204 ed_key.r_addr = s->ext_host_addr;
205 ed_key.fib_index = 0;
206 ed_kv.key[0] = ed_key.as_u64[0];
207 ed_kv.key[1] = ed_key.as_u64[1];
208 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
209 nat_elog_warn ("in2out_ed key del failed");
213 /* session lookup tables */
214 if (is_ed_session (s))
216 if (is_affinity_sessions (s))
217 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
218 s->in2out.protocol, s->out2in.port);
219 ed_key.l_addr = s->out2in.addr;
220 ed_key.r_addr = s->ext_host_addr;
221 ed_key.fib_index = s->out2in.fib_index;
222 if (snat_is_unk_proto_session (s))
224 ed_key.proto = s->in2out.port;
230 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
231 ed_key.l_port = s->out2in.port;
232 ed_key.r_port = s->ext_host_port;
234 ed_kv.key[0] = ed_key.as_u64[0];
235 ed_kv.key[1] = ed_key.as_u64[1];
236 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
237 nat_elog_warn ("out2in_ed key del failed");
238 ed_key.l_addr = s->in2out.addr;
239 ed_key.fib_index = s->in2out.fib_index;
240 if (!snat_is_unk_proto_session (s))
241 ed_key.l_port = s->in2out.port;
242 if (is_twice_nat_session (s))
244 ed_key.r_addr = s->ext_host_nat_addr;
245 ed_key.r_port = s->ext_host_nat_port;
247 ed_kv.key[0] = ed_key.as_u64[0];
248 ed_kv.key[1] = ed_key.as_u64[1];
249 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
250 nat_elog_warn ("in2out_ed key del failed");
253 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
254 &s->in2out.addr, s->in2out.port,
255 &s->ext_host_nat_addr, s->ext_host_nat_port,
256 &s->out2in.addr, s->out2in.port,
257 &s->ext_host_addr, s->ext_host_port,
258 s->in2out.protocol, is_twice_nat_session (s));
262 kv.key = s->in2out.as_u64;
263 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
264 nat_elog_warn ("in2out key del failed");
265 kv.key = s->out2in.as_u64;
266 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
267 nat_elog_warn ("out2in key del failed");
270 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
271 &s->in2out.addr, s->in2out.port,
272 &s->out2in.addr, s->out2in.port,
276 if (snat_is_unk_proto_session (s))
282 snat_ipfix_logging_nat44_ses_delete (thread_index,
283 s->in2out.addr.as_u32,
284 s->out2in.addr.as_u32,
288 s->in2out.fib_index);
290 nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
291 s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
295 /* Twice NAT address and port for external host */
296 if (is_twice_nat_session (s))
298 key.protocol = s->in2out.protocol;
299 key.port = s->ext_host_nat_port;
300 key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
301 snat_free_outside_address_and_port (sm->twice_nat_addresses,
305 if (snat_is_session_static (s))
308 snat_free_outside_address_and_port (sm->addresses, thread_index,
313 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
317 snat_user_key_t user_key;
318 clib_bihash_kv_8_8_t kv, value;
319 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
320 dlist_elt_t *per_user_list_head_elt;
322 user_key.addr.as_u32 = addr->as_u32;
323 user_key.fib_index = fib_index;
324 kv.key = user_key.as_u64;
326 /* Ever heard of the "user" = src ip4 address before? */
327 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
329 /* no, make a new one */
330 pool_get (tsm->users, u);
331 clib_memset (u, 0, sizeof (*u));
332 u->addr.as_u32 = addr->as_u32;
333 u->fib_index = fib_index;
335 pool_get (tsm->list_pool, per_user_list_head_elt);
337 u->sessions_per_user_list_head_index = per_user_list_head_elt -
340 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
342 kv.value = u - tsm->users;
345 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
346 nat_elog_warn ("user_hash keay add failed");
348 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
349 pool_elts (tsm->users));
353 u = pool_elt_at_index (tsm->users, value.value);
360 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
361 u32 thread_index, f64 now)
364 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
365 u32 oldest_per_user_translation_list_index, session_index;
366 dlist_elt_t *oldest_per_user_translation_list_elt;
367 dlist_elt_t *per_user_translation_list_elt;
369 /* Over quota? Recycle the least recently used translation */
370 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
372 oldest_per_user_translation_list_index =
373 clib_dlist_remove_head (tsm->list_pool,
374 u->sessions_per_user_list_head_index);
376 ASSERT (oldest_per_user_translation_list_index != ~0);
378 /* Add it back to the end of the LRU list */
379 clib_dlist_addtail (tsm->list_pool,
380 u->sessions_per_user_list_head_index,
381 oldest_per_user_translation_list_index);
382 /* Get the list element */
383 oldest_per_user_translation_list_elt =
384 pool_elt_at_index (tsm->list_pool,
385 oldest_per_user_translation_list_index);
387 /* Get the session index from the list element */
388 session_index = oldest_per_user_translation_list_elt->value;
390 /* Get the session */
391 s = pool_elt_at_index (tsm->sessions, session_index);
392 nat_free_session_data (sm, s, thread_index, 0);
393 if (snat_is_session_static (s))
394 u->nstaticsessions--;
401 s->ext_host_addr.as_u32 = 0;
402 s->ext_host_port = 0;
403 s->ext_host_nat_addr.as_u32 = 0;
404 s->ext_host_nat_port = 0;
408 pool_get (tsm->sessions, s);
409 clib_memset (s, 0, sizeof (*s));
411 /* Create list elts */
412 pool_get (tsm->list_pool, per_user_translation_list_elt);
413 clib_dlist_init (tsm->list_pool,
414 per_user_translation_list_elt - tsm->list_pool);
416 per_user_translation_list_elt->value = s - tsm->sessions;
417 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
418 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
420 clib_dlist_addtail (tsm->list_pool,
421 s->per_user_list_head_index,
422 per_user_translation_list_elt - tsm->list_pool);
424 s->user_index = u - tsm->users;
425 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
426 pool_elts (tsm->sessions));
429 s->ha_last_refreshed = now;
435 nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u, u32 thread_index,
439 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
440 dlist_elt_t *per_user_translation_list_elt, *oldest_elt;
442 u64 sess_timeout_time;
444 if (PREDICT_FALSE (!(u->nsessions) && !(u->nstaticsessions)))
448 clib_dlist_remove_head (tsm->list_pool,
449 u->sessions_per_user_list_head_index);
450 oldest_elt = pool_elt_at_index (tsm->list_pool, oldest_index);
451 s = pool_elt_at_index (tsm->sessions, oldest_elt->value);
452 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
453 if (now >= sess_timeout_time)
455 clib_dlist_addtail (tsm->list_pool,
456 u->sessions_per_user_list_head_index, oldest_index);
457 nat_free_session_data (sm, s, thread_index, 0);
458 if (snat_is_session_static (s))
459 u->nstaticsessions--;
466 s->ext_host_addr.as_u32 = 0;
467 s->ext_host_port = 0;
468 s->ext_host_nat_addr.as_u32 = 0;
469 s->ext_host_nat_port = 0;
473 clib_dlist_addhead (tsm->list_pool,
474 u->sessions_per_user_list_head_index, oldest_index);
475 if ((u->nsessions + u->nstaticsessions) >=
476 sm->max_translations_per_user)
478 nat_elog_addr (SNAT_LOG_WARNING, "[warn] max translations per user",
479 clib_net_to_host_u32 (u->addr.as_u32));
480 snat_ipfix_logging_max_entries_per_user
481 (thread_index, sm->max_translations_per_user, u->addr.as_u32);
487 pool_get (tsm->sessions, s);
488 clib_memset (s, 0, sizeof (*s));
490 /* Create list elts */
491 pool_get (tsm->list_pool, per_user_translation_list_elt);
492 clib_dlist_init (tsm->list_pool,
493 per_user_translation_list_elt - tsm->list_pool);
495 per_user_translation_list_elt->value = s - tsm->sessions;
496 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
497 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
499 clib_dlist_addtail (tsm->list_pool,
500 s->per_user_list_head_index,
501 per_user_translation_list_elt - tsm->list_pool);
504 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
505 pool_elts (tsm->sessions));
508 s->ha_last_refreshed = now;
514 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
517 fib_prefix_t prefix = {
519 .fp_proto = FIB_PROTOCOL_IP4,
521 .ip4.as_u32 = addr->as_u32,
524 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
527 fib_table_entry_update_one_path (fib_index,
529 FIB_SOURCE_PLUGIN_LOW,
530 (FIB_ENTRY_FLAG_CONNECTED |
531 FIB_ENTRY_FLAG_LOCAL |
532 FIB_ENTRY_FLAG_EXCLUSIVE),
536 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
538 fib_table_entry_delete (fib_index, &prefix, FIB_SOURCE_PLUGIN_LOW);
542 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
547 vlib_thread_main_t *tm = vlib_get_thread_main ();
549 if (twice_nat && !sm->endpoint_dependent)
550 return VNET_API_ERROR_FEATURE_DISABLED;
552 /* Check if address already exists */
554 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
556 if (ap->addr.as_u32 == addr->as_u32)
557 return VNET_API_ERROR_VALUE_EXIST;
562 vec_add2 (sm->twice_nat_addresses, ap, 1);
564 vec_add2 (sm->addresses, ap, 1);
569 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
570 FIB_SOURCE_PLUGIN_LOW);
573 #define _(N, i, n, s) \
574 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
575 ap->busy_##n##_ports = 0; \
576 ap->busy_##n##_ports_per_thread = 0;\
577 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
578 foreach_snat_protocol
583 /* Add external address to FIB */
585 pool_foreach (i, sm->interfaces,
587 if (nat_interface_is_inside(i) || sm->out2in_dpo)
590 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
593 pool_foreach (i, sm->output_feature_interfaces,
595 if (nat_interface_is_inside(i) || sm->out2in_dpo)
598 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
607 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
609 snat_static_mapping_t *m;
611 pool_foreach (m, sm->static_mappings,
613 if (is_addr_only_static_mapping (m) ||
614 is_out2in_only_static_mapping (m) ||
615 is_identity_static_mapping (m))
617 if (m->external_addr.as_u32 == addr.as_u32)
626 increment_v4_address (ip4_address_t * a)
630 v = clib_net_to_host_u32 (a->as_u32) + 1;
631 a->as_u32 = clib_host_to_net_u32 (v);
635 snat_add_static_mapping_when_resolved (snat_main_t * sm,
636 ip4_address_t l_addr,
641 snat_protocol_t proto,
642 int addr_only, int is_add, u8 * tag,
643 int twice_nat, int out2in_only,
646 snat_static_map_resolve_t *rp;
648 vec_add2 (sm->to_resolve, rp, 1);
649 rp->l_addr.as_u32 = l_addr.as_u32;
651 rp->sw_if_index = sw_if_index;
655 rp->addr_only = addr_only;
657 rp->twice_nat = twice_nat;
658 rp->out2in_only = out2in_only;
659 rp->identity_nat = identity_nat;
660 rp->tag = vec_dup (tag);
664 get_thread_idx_by_port (u16 e_port)
666 snat_main_t *sm = &snat_main;
667 u32 thread_idx = sm->num_workers;
668 if (sm->num_workers > 1)
671 sm->first_worker_index +
672 sm->workers[(e_port - 1024) / sm->port_per_thread];
678 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
679 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
680 u32 sw_if_index, snat_protocol_t proto, int is_add,
681 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
684 snat_main_t *sm = &snat_main;
685 snat_static_mapping_t *m;
686 snat_session_key_t m_key;
687 clib_bihash_kv_8_8_t kv, value;
688 snat_address_t *a = 0;
690 snat_interface_t *interface;
692 snat_main_per_thread_data_t *tsm;
693 snat_user_key_t u_key;
695 dlist_elt_t *head, *elt;
696 u32 elt_index, head_index;
700 snat_static_map_resolve_t *rp, *rp_match = 0;
701 nat44_lb_addr_port_t *local;
704 if (!sm->endpoint_dependent)
706 if (twice_nat || out2in_only)
707 return VNET_API_ERROR_FEATURE_DISABLED;
710 /* If the external address is a specific interface address */
711 if (sw_if_index != ~0)
713 ip4_address_t *first_int_addr;
715 for (i = 0; i < vec_len (sm->to_resolve); i++)
717 rp = sm->to_resolve + i;
718 if (rp->sw_if_index != sw_if_index ||
719 rp->l_addr.as_u32 != l_addr.as_u32 ||
720 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
725 if ((rp->l_port != l_port && rp->e_port != e_port)
726 || rp->proto != proto)
734 /* Might be already set... */
735 first_int_addr = ip4_interface_first_address
736 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
741 return VNET_API_ERROR_VALUE_EXIST;
743 snat_add_static_mapping_when_resolved
744 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
745 addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
747 /* DHCP resolution required? */
748 if (first_int_addr == 0)
754 e_addr.as_u32 = first_int_addr->as_u32;
755 /* Identity mapping? */
756 if (l_addr.as_u32 == 0)
757 l_addr.as_u32 = e_addr.as_u32;
763 return VNET_API_ERROR_NO_SUCH_ENTRY;
765 vec_del1 (sm->to_resolve, i);
769 e_addr.as_u32 = first_int_addr->as_u32;
770 /* Identity mapping? */
771 if (l_addr.as_u32 == 0)
772 l_addr.as_u32 = e_addr.as_u32;
780 m_key.port = addr_only ? 0 : e_port;
781 m_key.protocol = addr_only ? 0 : proto;
783 kv.key = m_key.as_u64;
784 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
787 m = pool_elt_at_index (sm->static_mappings, value.value);
793 if (is_identity_static_mapping (m))
796 pool_foreach (local, m->locals,
798 if (local->vrf_id == vrf_id)
799 return VNET_API_ERROR_VALUE_EXIST;
802 pool_get (m->locals, local);
803 local->vrf_id = vrf_id;
805 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
806 FIB_SOURCE_PLUGIN_LOW);
807 m_key.addr = m->local_addr;
808 m_key.port = m->local_port;
809 m_key.protocol = m->proto;
810 m_key.fib_index = local->fib_index;
811 kv.key = m_key.as_u64;
812 kv.value = m - sm->static_mappings;
813 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
817 return VNET_API_ERROR_VALUE_EXIST;
820 if (twice_nat && addr_only)
821 return VNET_API_ERROR_UNSUPPORTED;
823 /* Convert VRF id to FIB index */
826 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
827 FIB_SOURCE_PLUGIN_LOW);
828 /* If not specified use inside VRF id from SNAT plugin startup config */
831 fib_index = sm->inside_fib_index;
832 vrf_id = sm->inside_vrf_id;
833 fib_table_lock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
836 if (!(out2in_only || identity_nat))
839 m_key.port = addr_only ? 0 : l_port;
840 m_key.protocol = addr_only ? 0 : proto;
841 m_key.fib_index = fib_index;
842 kv.key = m_key.as_u64;
843 if (!clib_bihash_search_8_8
844 (&sm->static_mapping_by_local, &kv, &value))
845 return VNET_API_ERROR_VALUE_EXIST;
848 /* Find external address in allocated addresses and reserve port for
849 address and port pair mapping when dynamic translations enabled */
850 if (!(addr_only || sm->static_mapping_only || out2in_only))
852 for (i = 0; i < vec_len (sm->addresses); i++)
854 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
856 a = sm->addresses + i;
857 /* External port must be unused */
860 #define _(N, j, n, s) \
861 case SNAT_PROTOCOL_##N: \
862 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
863 return VNET_API_ERROR_INVALID_VALUE; \
864 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
867 a->busy_##n##_ports++; \
868 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
871 foreach_snat_protocol
874 nat_elog_info ("unknown protocol");
875 return VNET_API_ERROR_INVALID_VALUE_2;
880 /* External address must be allocated */
881 if (!a && (l_addr.as_u32 != e_addr.as_u32))
883 if (sw_if_index != ~0)
885 for (i = 0; i < vec_len (sm->to_resolve); i++)
887 rp = sm->to_resolve + i;
890 if (rp->sw_if_index != sw_if_index &&
891 rp->l_addr.as_u32 != l_addr.as_u32 &&
892 rp->vrf_id != vrf_id && rp->l_port != l_port &&
893 rp->e_port != e_port && rp->proto != proto)
896 vec_del1 (sm->to_resolve, i);
900 return VNET_API_ERROR_NO_SUCH_ENTRY;
904 pool_get (sm->static_mappings, m);
905 clib_memset (m, 0, sizeof (*m));
906 m->tag = vec_dup (tag);
907 m->local_addr = l_addr;
908 m->external_addr = e_addr;
909 m->twice_nat = twice_nat;
911 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
913 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
916 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
917 pool_get (m->locals, local);
918 local->vrf_id = vrf_id;
919 local->fib_index = fib_index;
924 m->fib_index = fib_index;
928 m->local_port = l_port;
929 m->external_port = e_port;
933 if (sm->num_workers > 1)
936 .src_address = m->local_addr,
938 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0));
939 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
942 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
944 m_key.addr = m->local_addr;
945 m_key.port = m->local_port;
946 m_key.protocol = m->proto;
947 m_key.fib_index = fib_index;
948 kv.key = m_key.as_u64;
949 kv.value = m - sm->static_mappings;
951 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
953 m_key.addr = m->external_addr;
954 m_key.port = m->external_port;
956 kv.key = m_key.as_u64;
957 kv.value = m - sm->static_mappings;
958 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
960 /* Delete dynamic sessions matching local address (+ local port) */
961 if (!(sm->static_mapping_only))
963 u_key.addr = m->local_addr;
964 u_key.fib_index = m->fib_index;
965 kv.key = u_key.as_u64;
966 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
968 user_index = value.value;
969 u = pool_elt_at_index (tsm->users, user_index);
972 head_index = u->sessions_per_user_list_head_index;
973 head = pool_elt_at_index (tsm->list_pool, head_index);
974 elt_index = head->next;
975 elt = pool_elt_at_index (tsm->list_pool, elt_index);
976 ses_index = elt->value;
977 while (ses_index != ~0)
979 s = pool_elt_at_index (tsm->sessions, ses_index);
980 elt = pool_elt_at_index (tsm->list_pool, elt->next);
981 ses_index = elt->value;
983 if (snat_is_session_static (s))
987 && (clib_net_to_host_u16 (s->in2out.port) !=
991 nat_free_session_data (sm, s,
992 tsm - sm->per_thread_data, 0);
993 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
995 if (!addr_only && !sm->endpoint_dependent)
1006 if (sw_if_index != ~0)
1009 return VNET_API_ERROR_NO_SUCH_ENTRY;
1015 vrf_id = sm->inside_vrf_id;
1018 pool_foreach (local, m->locals,
1020 if (local->vrf_id == vrf_id)
1021 find = local - m->locals;
1025 return VNET_API_ERROR_NO_SUCH_ENTRY;
1027 local = pool_elt_at_index (m->locals, find);
1028 fib_index = local->fib_index;
1029 pool_put (m->locals, local);
1032 fib_index = m->fib_index;
1034 /* Free external address port */
1035 if (!(addr_only || sm->static_mapping_only || out2in_only))
1037 for (i = 0; i < vec_len (sm->addresses); i++)
1039 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1041 a = sm->addresses + i;
1044 #define _(N, j, n, s) \
1045 case SNAT_PROTOCOL_##N: \
1046 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1047 if (e_port > 1024) \
1049 a->busy_##n##_ports--; \
1050 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1053 foreach_snat_protocol
1056 nat_elog_info ("unknown protocol");
1057 return VNET_API_ERROR_INVALID_VALUE_2;
1064 if (sm->num_workers > 1)
1065 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1067 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1069 m_key.addr = m->local_addr;
1070 m_key.port = m->local_port;
1071 m_key.protocol = m->proto;
1072 m_key.fib_index = fib_index;
1073 kv.key = m_key.as_u64;
1075 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1077 /* Delete session(s) for static mapping if exist */
1078 if (!(sm->static_mapping_only) ||
1079 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1081 u_key.addr = m->local_addr;
1082 u_key.fib_index = fib_index;
1083 kv.key = u_key.as_u64;
1084 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1086 user_index = value.value;
1087 u = pool_elt_at_index (tsm->users, user_index);
1088 if (u->nstaticsessions)
1090 head_index = u->sessions_per_user_list_head_index;
1091 head = pool_elt_at_index (tsm->list_pool, head_index);
1092 elt_index = head->next;
1093 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1094 ses_index = elt->value;
1095 while (ses_index != ~0)
1097 s = pool_elt_at_index (tsm->sessions, ses_index);
1098 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1099 ses_index = elt->value;
1103 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1104 (clib_net_to_host_u16 (s->out2in.port) !=
1109 if (is_lb_session (s))
1112 if (!snat_is_session_static (s))
1115 nat_free_session_data (sm, s,
1116 tsm - sm->per_thread_data, 0);
1117 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1119 if (!addr_only && !sm->endpoint_dependent)
1126 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1127 if (pool_elts (m->locals))
1130 m_key.addr = m->external_addr;
1131 m_key.port = m->external_port;
1132 m_key.fib_index = 0;
1133 kv.key = m_key.as_u64;
1134 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1137 vec_free (m->workers);
1138 /* Delete static mapping from pool */
1139 pool_put (sm->static_mappings, m);
1142 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1145 /* Add/delete external address to FIB */
1147 pool_foreach (interface, sm->interfaces,
1149 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1152 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1155 pool_foreach (interface, sm->output_feature_interfaces,
1157 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1160 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1169 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1170 snat_protocol_t proto,
1171 nat44_lb_addr_port_t * locals, u8 is_add,
1172 twice_nat_type_t twice_nat, u8 out2in_only,
1173 u8 * tag, u32 affinity)
1175 snat_main_t *sm = &snat_main;
1176 snat_static_mapping_t *m;
1177 snat_session_key_t m_key;
1178 clib_bihash_kv_8_8_t kv, value;
1179 snat_address_t *a = 0;
1181 nat44_lb_addr_port_t *local;
1182 u32 elt_index, head_index, ses_index;
1183 snat_main_per_thread_data_t *tsm;
1184 snat_user_key_t u_key;
1187 dlist_elt_t *head, *elt;
1190 if (!sm->endpoint_dependent)
1191 return VNET_API_ERROR_FEATURE_DISABLED;
1193 m_key.addr = e_addr;
1194 m_key.port = e_port;
1195 m_key.protocol = proto;
1196 m_key.fib_index = 0;
1197 kv.key = m_key.as_u64;
1198 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1201 m = pool_elt_at_index (sm->static_mappings, value.value);
1206 return VNET_API_ERROR_VALUE_EXIST;
1208 if (vec_len (locals) < 2)
1209 return VNET_API_ERROR_INVALID_VALUE;
1211 /* Find external address in allocated addresses and reserve port for
1212 address and port pair mapping when dynamic translations enabled */
1213 if (!(sm->static_mapping_only || out2in_only))
1215 for (i = 0; i < vec_len (sm->addresses); i++)
1217 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1219 a = sm->addresses + i;
1220 /* External port must be unused */
1223 #define _(N, j, n, s) \
1224 case SNAT_PROTOCOL_##N: \
1225 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1226 return VNET_API_ERROR_INVALID_VALUE; \
1227 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1228 if (e_port > 1024) \
1230 a->busy_##n##_ports++; \
1231 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1234 foreach_snat_protocol
1237 nat_elog_info ("unknown protocol");
1238 return VNET_API_ERROR_INVALID_VALUE_2;
1243 /* External address must be allocated */
1245 return VNET_API_ERROR_NO_SUCH_ENTRY;
1248 pool_get (sm->static_mappings, m);
1249 clib_memset (m, 0, sizeof (*m));
1250 m->tag = vec_dup (tag);
1251 m->external_addr = e_addr;
1252 m->external_port = e_port;
1254 m->twice_nat = twice_nat;
1255 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1257 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1258 m->affinity = affinity;
1261 m->affinity_per_service_list_head_index =
1262 nat_affinity_get_per_service_list_head_index ();
1264 m->affinity_per_service_list_head_index = ~0;
1266 m_key.addr = m->external_addr;
1267 m_key.port = m->external_port;
1268 m_key.protocol = m->proto;
1269 m_key.fib_index = 0;
1270 kv.key = m_key.as_u64;
1271 kv.value = m - sm->static_mappings;
1272 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1274 nat_elog_err ("static_mapping_by_external key add failed");
1275 return VNET_API_ERROR_UNSPECIFIED;
1278 m_key.fib_index = m->fib_index;
1279 for (i = 0; i < vec_len (locals); i++)
1281 locals[i].fib_index =
1282 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1284 FIB_SOURCE_PLUGIN_LOW);
1285 m_key.addr = locals[i].addr;
1286 m_key.fib_index = locals[i].fib_index;
1289 m_key.port = locals[i].port;
1290 kv.key = m_key.as_u64;
1291 kv.value = m - sm->static_mappings;
1292 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1294 locals[i].prefix = (i == 0) ? locals[i].probability :
1295 (locals[i - 1].prefix + locals[i].probability);
1296 pool_get (m->locals, local);
1298 if (sm->num_workers > 1)
1301 .src_address = locals[i].addr,
1304 clib_bitmap_set (bitmap,
1305 sm->worker_in2out_cb (&ip, m->fib_index, 0),
1310 /* Assign workers */
1311 if (sm->num_workers > 1)
1314 clib_bitmap_foreach (i, bitmap,
1316 vec_add1(m->workers, i);
1324 return VNET_API_ERROR_NO_SUCH_ENTRY;
1326 if (!is_lb_static_mapping (m))
1327 return VNET_API_ERROR_INVALID_VALUE;
1329 /* Free external address port */
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;
1339 #define _(N, j, n, s) \
1340 case SNAT_PROTOCOL_##N: \
1341 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1342 if (e_port > 1024) \
1344 a->busy_##n##_ports--; \
1345 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1348 foreach_snat_protocol
1351 nat_elog_info ("unknown protocol");
1352 return VNET_API_ERROR_INVALID_VALUE_2;
1359 m_key.addr = m->external_addr;
1360 m_key.port = m->external_port;
1361 m_key.protocol = m->proto;
1362 m_key.fib_index = 0;
1363 kv.key = m_key.as_u64;
1364 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1366 nat_elog_err ("static_mapping_by_external key del failed");
1367 return VNET_API_ERROR_UNSPECIFIED;
1371 pool_foreach (local, m->locals,
1373 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1374 FIB_SOURCE_PLUGIN_LOW);
1375 m_key.addr = local->addr;
1378 m_key.port = local->port;
1379 m_key.fib_index = local->fib_index;
1380 kv.key = m_key.as_u64;
1381 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1383 nat_elog_err ("static_mapping_by_local key del failed");
1384 return VNET_API_ERROR_UNSPECIFIED;
1388 if (sm->num_workers > 1)
1391 .src_address = local->addr,
1393 tsm = vec_elt_at_index (sm->per_thread_data,
1394 sm->worker_in2out_cb (&ip, m->fib_index, 0));
1397 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1399 /* Delete sessions */
1400 u_key.addr = local->addr;
1401 u_key.fib_index = local->fib_index;
1402 kv.key = u_key.as_u64;
1403 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1405 u = pool_elt_at_index (tsm->users, value.value);
1406 if (u->nstaticsessions)
1408 head_index = u->sessions_per_user_list_head_index;
1409 head = pool_elt_at_index (tsm->list_pool, head_index);
1410 elt_index = head->next;
1411 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1412 ses_index = elt->value;
1413 while (ses_index != ~0)
1415 s = pool_elt_at_index (tsm->sessions, ses_index);
1416 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1417 ses_index = elt->value;
1419 if (!(is_lb_session (s)))
1422 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1423 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1426 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1427 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1434 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1435 pool_free (m->locals);
1437 vec_free (m->workers);
1439 pool_put (sm->static_mappings, m);
1446 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1447 ip4_address_t l_addr, u16 l_port,
1448 snat_protocol_t proto, u32 vrf_id,
1449 u8 probability, u8 is_add)
1451 snat_main_t *sm = &snat_main;
1452 snat_static_mapping_t *m = 0;
1453 snat_session_key_t m_key;
1454 clib_bihash_kv_8_8_t kv, value;
1455 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1456 snat_main_per_thread_data_t *tsm;
1457 snat_user_key_t u_key;
1460 dlist_elt_t *head, *elt;
1461 u32 elt_index, head_index, ses_index, *locals = 0;
1465 if (!sm->endpoint_dependent)
1466 return VNET_API_ERROR_FEATURE_DISABLED;
1468 m_key.addr = e_addr;
1469 m_key.port = e_port;
1470 m_key.protocol = proto;
1471 m_key.fib_index = 0;
1472 kv.key = m_key.as_u64;
1473 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1474 m = pool_elt_at_index (sm->static_mappings, value.value);
1477 return VNET_API_ERROR_NO_SUCH_ENTRY;
1479 if (!is_lb_static_mapping (m))
1480 return VNET_API_ERROR_INVALID_VALUE;
1483 pool_foreach (local, m->locals,
1485 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1486 (local->vrf_id == vrf_id))
1488 match_local = local;
1497 return VNET_API_ERROR_VALUE_EXIST;
1499 pool_get (m->locals, local);
1500 clib_memset (local, 0, sizeof (*local));
1501 local->addr.as_u32 = l_addr.as_u32;
1502 local->port = l_port;
1503 local->probability = probability;
1504 local->vrf_id = vrf_id;
1506 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1507 FIB_SOURCE_PLUGIN_LOW);
1509 if (!is_out2in_only_static_mapping (m))
1511 m_key.addr = l_addr;
1512 m_key.port = l_port;
1513 m_key.fib_index = local->fib_index;
1514 kv.key = m_key.as_u64;
1515 kv.value = m - sm->static_mappings;
1516 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1517 nat_elog_err ("static_mapping_by_local key add failed");
1523 return VNET_API_ERROR_NO_SUCH_ENTRY;
1525 if (pool_elts (m->locals) < 3)
1526 return VNET_API_ERROR_UNSPECIFIED;
1528 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1529 FIB_SOURCE_PLUGIN_LOW);
1531 if (!is_out2in_only_static_mapping (m))
1533 m_key.addr = l_addr;
1534 m_key.port = l_port;
1535 m_key.fib_index = match_local->fib_index;
1536 kv.key = m_key.as_u64;
1537 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1538 nat_elog_err ("static_mapping_by_local key del failed");
1541 if (sm->num_workers > 1)
1544 .src_address = local->addr,
1546 tsm = vec_elt_at_index (sm->per_thread_data,
1547 sm->worker_in2out_cb (&ip, m->fib_index,
1551 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1553 /* Delete sessions */
1554 u_key.addr = match_local->addr;
1555 u_key.fib_index = match_local->fib_index;
1556 kv.key = u_key.as_u64;
1557 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1559 u = pool_elt_at_index (tsm->users, value.value);
1560 if (u->nstaticsessions)
1562 head_index = u->sessions_per_user_list_head_index;
1563 head = pool_elt_at_index (tsm->list_pool, head_index);
1564 elt_index = head->next;
1565 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1566 ses_index = elt->value;
1567 while (ses_index != ~0)
1569 s = pool_elt_at_index (tsm->sessions, ses_index);
1570 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1571 ses_index = elt->value;
1573 if (!(is_lb_session (s)))
1576 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1577 (clib_net_to_host_u16 (s->in2out.port) !=
1581 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1582 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1587 pool_put (m->locals, match_local);
1590 vec_free (m->workers);
1593 pool_foreach (local, m->locals,
1595 vec_add1 (locals, local - m->locals);
1596 if (sm->num_workers > 1)
1599 ip.src_address.as_u32 = local->addr.as_u32,
1600 bitmap = clib_bitmap_set (bitmap,
1601 sm->worker_in2out_cb (&ip, local->fib_index, 0),
1607 ASSERT (vec_len (locals) > 1);
1609 local = pool_elt_at_index (m->locals, locals[0]);
1610 local->prefix = local->probability;
1611 for (i = 1; i < vec_len (locals); i++)
1613 local = pool_elt_at_index (m->locals, locals[i]);
1614 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1615 local->prefix = local->probability + prev_local->prefix;
1618 /* Assign workers */
1619 if (sm->num_workers > 1)
1622 clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); }));
1630 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1633 snat_address_t *a = 0;
1634 snat_session_t *ses;
1635 u32 *ses_to_be_removed = 0, *ses_index;
1636 snat_main_per_thread_data_t *tsm;
1637 snat_static_mapping_t *m;
1638 snat_interface_t *interface;
1640 snat_address_t *addresses =
1641 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1643 /* Find SNAT address */
1644 for (i = 0; i < vec_len (addresses); i++)
1646 if (addresses[i].addr.as_u32 == addr.as_u32)
1653 return VNET_API_ERROR_NO_SUCH_ENTRY;
1658 pool_foreach (m, sm->static_mappings,
1660 if (m->external_addr.as_u32 == addr.as_u32)
1661 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1662 m->local_port, m->external_port,
1663 m->vrf_id, is_addr_only_static_mapping(m), ~0,
1664 m->proto, 0, m->twice_nat,
1665 is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1671 /* Check if address is used in some static mapping */
1672 if (is_snat_address_used_in_static_mapping (sm, addr))
1674 nat_elog_notice ("address used in static mapping");
1675 return VNET_API_ERROR_UNSPECIFIED;
1679 if (a->fib_index != ~0)
1680 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1682 /* Delete sessions using address */
1683 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1686 vec_foreach (tsm, sm->per_thread_data)
1688 pool_foreach (ses, tsm->sessions, ({
1689 if (ses->out2in.addr.as_u32 == addr.as_u32)
1691 nat_free_session_data (sm, ses, tsm - sm->per_thread_data, 0);
1692 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1696 vec_foreach (ses_index, ses_to_be_removed)
1698 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1699 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1702 vec_free (ses_to_be_removed);
1707 #define _(N, i, n, s) \
1708 clib_bitmap_free (a->busy_##n##_port_bitmap); \
1709 vec_free (a->busy_##n##_ports_per_thread);
1710 foreach_snat_protocol
1714 vec_del1 (sm->twice_nat_addresses, i);
1718 vec_del1 (sm->addresses, i);
1720 /* Delete external address from FIB */
1722 pool_foreach (interface, sm->interfaces,
1724 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1727 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1730 pool_foreach (interface, sm->output_feature_interfaces,
1732 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1735 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1744 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1746 snat_main_t *sm = &snat_main;
1747 snat_interface_t *i;
1748 const char *feature_name, *del_feature_name;
1750 snat_static_mapping_t *m;
1752 nat_outside_fib_t *outside_fib;
1753 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1756 if (sm->out2in_dpo && !is_inside)
1757 return VNET_API_ERROR_UNSUPPORTED;
1760 pool_foreach (i, sm->output_feature_interfaces,
1762 if (i->sw_if_index == sw_if_index)
1763 return VNET_API_ERROR_VALUE_EXIST;
1767 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1768 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1771 if (sm->num_workers > 1 && !sm->deterministic)
1773 is_inside ? "nat44-in2out-worker-handoff" :
1774 "nat44-out2in-worker-handoff";
1775 else if (sm->deterministic)
1776 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1777 else if (sm->endpoint_dependent)
1778 feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in";
1780 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1783 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1784 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1787 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1788 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1794 vec_foreach (outside_fib, sm->outside_fibs)
1796 if (outside_fib->fib_index == fib_index)
1800 outside_fib->refcount--;
1801 if (!outside_fib->refcount)
1802 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1805 outside_fib->refcount++;
1812 vec_add2 (sm->outside_fibs, outside_fib, 1);
1813 outside_fib->refcount = 1;
1814 outside_fib->fib_index = fib_index;
1819 pool_foreach (i, sm->interfaces,
1821 if (i->sw_if_index == sw_if_index)
1825 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1828 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1830 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1832 if (sm->num_workers > 1 && !sm->deterministic)
1834 del_feature_name = "nat44-handoff-classify";
1835 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1836 "nat44-out2in-worker-handoff";
1838 else if (sm->deterministic)
1840 del_feature_name = "nat44-det-classify";
1841 feature_name = !is_inside ? "nat44-det-in2out" :
1844 else if (sm->endpoint_dependent)
1846 del_feature_name = "nat44-ed-classify";
1847 feature_name = !is_inside ? "nat44-ed-in2out" :
1852 del_feature_name = "nat44-classify";
1853 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1856 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1857 sw_if_index, 0, 0, 0);
1858 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1859 sw_if_index, 1, 0, 0);
1862 if (sm->endpoint_dependent)
1863 vnet_feature_enable_disable ("ip4-local",
1864 "nat44-ed-hairpinning",
1865 sw_if_index, 1, 0, 0);
1866 else if (!sm->deterministic)
1867 vnet_feature_enable_disable ("ip4-local",
1868 "nat44-hairpinning",
1869 sw_if_index, 1, 0, 0);
1874 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1875 sw_if_index, 0, 0, 0);
1876 pool_put (sm->interfaces, i);
1879 if (sm->endpoint_dependent)
1880 vnet_feature_enable_disable ("ip4-local",
1881 "nat44-ed-hairpinning",
1882 sw_if_index, 0, 0, 0);
1883 else if (!sm->deterministic)
1884 vnet_feature_enable_disable ("ip4-local",
1885 "nat44-hairpinning",
1886 sw_if_index, 0, 0, 0);
1892 if ((nat_interface_is_inside(i) && is_inside) ||
1893 (nat_interface_is_outside(i) && !is_inside))
1896 if (sm->num_workers > 1 && !sm->deterministic)
1898 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1899 "nat44-out2in-worker-handoff";
1900 feature_name = "nat44-handoff-classify";
1902 else if (sm->deterministic)
1904 del_feature_name = !is_inside ? "nat44-det-in2out" :
1906 feature_name = "nat44-det-classify";
1908 else if (sm->endpoint_dependent)
1910 del_feature_name = !is_inside ? "nat44-ed-in2out" :
1912 feature_name = "nat44-ed-classify";
1916 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1917 feature_name = "nat44-classify";
1920 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1921 sw_if_index, 0, 0, 0);
1922 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1923 sw_if_index, 1, 0, 0);
1926 if (sm->endpoint_dependent)
1927 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1928 sw_if_index, 0, 0, 0);
1929 else if (!sm->deterministic)
1930 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1931 sw_if_index, 0, 0, 0);
1942 return VNET_API_ERROR_NO_SUCH_ENTRY;
1944 pool_get (sm->interfaces, i);
1945 i->sw_if_index = sw_if_index;
1947 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
1950 if (is_inside && !sm->out2in_dpo)
1952 if (sm->endpoint_dependent)
1953 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1954 sw_if_index, 1, 0, 0);
1955 else if (!sm->deterministic)
1956 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1957 sw_if_index, 1, 0, 0);
1963 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1967 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1969 /* Add/delete external addresses to FIB */
1972 vec_foreach (ap, sm->addresses)
1973 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1975 pool_foreach (m, sm->static_mappings,
1977 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1980 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1983 pool_foreach (dm, sm->det_maps,
1985 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1993 snat_interface_add_del_output_feature (u32 sw_if_index,
1994 u8 is_inside, int is_del)
1996 snat_main_t *sm = &snat_main;
1997 snat_interface_t *i;
1999 snat_static_mapping_t *m;
2000 nat_outside_fib_t *outside_fib;
2001 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
2005 if (sm->deterministic ||
2006 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
2007 return VNET_API_ERROR_UNSUPPORTED;
2010 pool_foreach (i, sm->interfaces,
2012 if (i->sw_if_index == sw_if_index)
2013 return VNET_API_ERROR_VALUE_EXIST;
2020 vec_foreach (outside_fib, sm->outside_fibs)
2022 if (outside_fib->fib_index == fib_index)
2026 outside_fib->refcount--;
2027 if (!outside_fib->refcount)
2028 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2031 outside_fib->refcount++;
2038 vec_add2 (sm->outside_fibs, outside_fib, 1);
2039 outside_fib->refcount = 1;
2040 outside_fib->fib_index = fib_index;
2047 if (sm->endpoint_dependent)
2049 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2050 sw_if_index, !is_del, 0, 0);
2051 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2052 sw_if_index, !is_del, 0, 0);
2056 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2057 sw_if_index, !is_del, 0, 0);
2058 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2059 sw_if_index, !is_del, 0, 0);
2064 if (sm->num_workers > 1)
2066 vnet_feature_enable_disable ("ip4-unicast",
2067 "nat44-out2in-worker-handoff",
2068 sw_if_index, !is_del, 0, 0);
2069 vnet_feature_enable_disable ("ip4-output",
2070 "nat44-in2out-output-worker-handoff",
2071 sw_if_index, !is_del, 0, 0);
2075 if (sm->endpoint_dependent)
2077 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in",
2078 sw_if_index, !is_del, 0, 0);
2079 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
2080 sw_if_index, !is_del, 0, 0);
2084 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2085 sw_if_index, !is_del, 0, 0);
2086 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2087 sw_if_index, !is_del, 0, 0);
2092 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2093 sm->fq_in2out_output_index =
2094 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2096 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2097 sm->fq_out2in_index =
2098 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2101 pool_foreach (i, sm->output_feature_interfaces,
2103 if (i->sw_if_index == sw_if_index)
2106 pool_put (sm->output_feature_interfaces, i);
2108 return VNET_API_ERROR_VALUE_EXIST;
2116 return VNET_API_ERROR_NO_SUCH_ENTRY;
2118 pool_get (sm->output_feature_interfaces, i);
2119 i->sw_if_index = sw_if_index;
2122 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2124 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2126 /* Add/delete external addresses to FIB */
2132 vec_foreach (ap, sm->addresses)
2133 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2135 pool_foreach (m, sm->static_mappings,
2137 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2140 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2148 snat_set_workers (uword * bitmap)
2150 snat_main_t *sm = &snat_main;
2153 if (sm->num_workers < 2)
2154 return VNET_API_ERROR_FEATURE_DISABLED;
2156 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2157 return VNET_API_ERROR_INVALID_WORKER;
2159 vec_free (sm->workers);
2161 clib_bitmap_foreach (i, bitmap,
2163 vec_add1(sm->workers, i);
2164 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2165 sm->per_thread_data[sm->first_worker_index + i].thread_index = i;
2170 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2171 sm->num_snat_thread = _vec_len (sm->workers);
2177 snat_update_outside_fib (u32 sw_if_index, u32 new_fib_index,
2180 snat_main_t *sm = &snat_main;
2181 nat_outside_fib_t *outside_fib;
2182 snat_interface_t *i;
2186 if (new_fib_index == old_fib_index)
2189 if (!vec_len (sm->outside_fibs))
2193 pool_foreach (i, sm->interfaces,
2195 if (i->sw_if_index == sw_if_index)
2197 if (!(nat_interface_is_outside (i)))
2207 vec_foreach (outside_fib, sm->outside_fibs)
2209 if (outside_fib->fib_index == old_fib_index)
2211 outside_fib->refcount--;
2212 if (!outside_fib->refcount)
2213 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2218 vec_foreach (outside_fib, sm->outside_fibs)
2220 if (outside_fib->fib_index == new_fib_index)
2222 outside_fib->refcount++;
2230 vec_add2 (sm->outside_fibs, outside_fib, 1);
2231 outside_fib->refcount = 1;
2232 outside_fib->fib_index = new_fib_index;
2237 snat_ip4_table_bind (ip4_main_t * im,
2239 u32 sw_if_index, u32 new_fib_index, u32 old_fib_index)
2241 snat_update_outside_fib (sw_if_index, new_fib_index, old_fib_index);
2245 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2248 ip4_address_t * address,
2250 u32 if_address_index, u32 is_delete);
2253 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2256 ip4_address_t * address,
2258 u32 if_address_index, u32 is_delete);
2261 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2264 snat_session_key_t * k,
2265 u16 port_per_thread, u32 snat_thread_index);
2267 static clib_error_t *
2268 snat_init (vlib_main_t * vm)
2270 snat_main_t *sm = &snat_main;
2271 clib_error_t *error = 0;
2272 ip4_main_t *im = &ip4_main;
2273 ip_lookup_main_t *lm = &im->lookup_main;
2275 vlib_thread_registration_t *tr;
2276 vlib_thread_main_t *tm = vlib_get_thread_main ();
2279 ip4_add_del_interface_address_callback_t cb4;
2283 sm->vnet_main = vnet_get_main ();
2285 sm->ip4_lookup_main = lm;
2286 sm->api_main = &api_main;
2287 sm->first_worker_index = 0;
2288 sm->num_workers = 0;
2289 sm->num_snat_thread = 1;
2291 sm->port_per_thread = 0xffff - 1024;
2292 sm->fq_in2out_index = ~0;
2293 sm->fq_in2out_output_index = ~0;
2294 sm->fq_out2in_index = ~0;
2295 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2296 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2297 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2298 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2299 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2300 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2301 sm->forwarding_enabled = 0;
2302 sm->log_class = vlib_log_register_class ("nat", 0);
2303 sm->log_level = SNAT_LOG_ERROR;
2304 sm->mss_clamping = 0;
2306 node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2307 sm->error_node_index = node->index;
2309 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out");
2310 sm->in2out_node_index = node->index;
2311 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output");
2312 sm->in2out_output_node_index = node->index;
2313 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-fast");
2314 sm->in2out_fast_node_index = node->index;
2315 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-slowpath");
2316 sm->in2out_slowpath_node_index = node->index;
2317 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-output-slowpath");
2318 sm->in2out_slowpath_output_node_index = node->index;
2319 node = vlib_get_node_by_name (vm, (u8 *) "nat44-in2out-reass");
2320 sm->in2out_reass_node_index = node->index;
2322 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out");
2323 sm->ed_in2out_node_index = node->index;
2324 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-slowpath");
2325 sm->ed_in2out_slowpath_node_index = node->index;
2326 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-in2out-reass");
2327 sm->ed_in2out_reass_node_index = node->index;
2329 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in");
2330 sm->out2in_node_index = node->index;
2331 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-fast");
2332 sm->out2in_fast_node_index = node->index;
2333 node = vlib_get_node_by_name (vm, (u8 *) "nat44-out2in-reass");
2334 sm->out2in_reass_node_index = node->index;
2336 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in");
2337 sm->ed_out2in_node_index = node->index;
2338 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-slowpath");
2339 sm->ed_out2in_slowpath_node_index = node->index;
2340 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-out2in-reass");
2341 sm->ed_out2in_reass_node_index = node->index;
2343 node = vlib_get_node_by_name (vm, (u8 *) "nat44-det-in2out");
2344 sm->det_in2out_node_index = node->index;
2345 node = vlib_get_node_by_name (vm, (u8 *) "nat44-det-out2in");
2346 sm->det_out2in_node_index = node->index;
2348 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpinning");
2349 sm->hairpinning_node_index = node->index;
2350 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-dst");
2351 sm->hairpin_dst_node_index = node->index;
2352 node = vlib_get_node_by_name (vm, (u8 *) "nat44-hairpin-src");
2353 sm->hairpin_src_node_index = node->index;
2354 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpinning");
2355 sm->ed_hairpinning_node_index = node->index;
2356 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-dst");
2357 sm->ed_hairpin_dst_node_index = node->index;
2358 node = vlib_get_node_by_name (vm, (u8 *) "nat44-ed-hairpin-src");
2359 sm->ed_hairpin_src_node_index = node->index;
2361 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2364 tr = (vlib_thread_registration_t *) p[0];
2367 sm->num_workers = tr->count;
2368 sm->first_worker_index = tr->first_index;
2372 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2374 /* Use all available workers by default */
2375 if (sm->num_workers > 1)
2377 for (i = 0; i < sm->num_workers; i++)
2378 bitmap = clib_bitmap_set (bitmap, i, 1);
2379 snat_set_workers (bitmap);
2380 clib_bitmap_free (bitmap);
2384 sm->per_thread_data[0].snat_thread_index = 0;
2387 error = snat_api_init (vm, sm);
2391 /* Set up the interface address add/del callback */
2392 cb4.function = snat_ip4_add_del_interface_address_cb;
2393 cb4.function_opaque = 0;
2395 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2397 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2398 cb4.function_opaque = 0;
2400 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2402 nat_dpo_module_init ();
2405 sm->total_users.name = "total-users";
2406 sm->total_users.stat_segment_name = "/nat44/total-users";
2407 vlib_validate_simple_counter (&sm->total_users, 0);
2408 vlib_zero_simple_counter (&sm->total_users, 0);
2409 sm->total_sessions.name = "total-sessions";
2410 sm->total_sessions.stat_segment_name = "/nat44/total-sessions";
2411 vlib_validate_simple_counter (&sm->total_sessions, 0);
2412 vlib_zero_simple_counter (&sm->total_sessions, 0);
2414 /* Init IPFIX logging */
2415 snat_ipfix_logging_init (vm);
2418 error = nat64_init (vm);
2426 ip4_table_bind_callback_t cbt4 = {
2427 .function = snat_ip4_table_bind,
2429 vec_add1 (ip4_main.table_bind_callbacks, cbt4);
2431 /* Init virtual fragmenentation reassembly */
2432 return nat_reass_init (vm);
2435 VLIB_INIT_FUNCTION (snat_init);
2438 snat_free_outside_address_and_port (snat_address_t * addresses,
2439 u32 thread_index, snat_session_key_t * k)
2443 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2445 for (address_index = 0; address_index < vec_len (addresses);
2448 if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
2452 ASSERT (address_index < vec_len (addresses));
2454 a = addresses + address_index;
2456 switch (k->protocol)
2458 #define _(N, i, n, s) \
2459 case SNAT_PROTOCOL_##N: \
2460 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
2461 port_host_byte_order) == 1); \
2462 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
2463 port_host_byte_order, 0); \
2464 a->busy_##n##_ports--; \
2465 a->busy_##n##_ports_per_thread[thread_index]--; \
2467 foreach_snat_protocol
2470 nat_elog_info ("unknown protocol");
2476 nat_set_outside_address_and_port (snat_address_t * addresses,
2477 u32 thread_index, snat_session_key_t * k)
2479 snat_address_t *a = 0;
2481 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2483 for (address_index = 0; address_index < vec_len (addresses);
2486 if (addresses[address_index].addr.as_u32 != k->addr.as_u32)
2489 a = addresses + address_index;
2490 switch (k->protocol)
2492 #define _(N, j, n, s) \
2493 case SNAT_PROTOCOL_##N: \
2494 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, port_host_byte_order)) \
2495 return VNET_API_ERROR_INSTANCE_IN_USE; \
2496 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port_host_byte_order, 1); \
2497 a->busy_##n##_ports_per_thread[thread_index]++; \
2498 a->busy_##n##_ports++; \
2500 foreach_snat_protocol
2503 nat_elog_info ("unknown protocol");
2508 return VNET_API_ERROR_NO_SUCH_ENTRY;
2512 snat_static_mapping_match (snat_main_t * sm,
2513 snat_session_key_t match,
2514 snat_session_key_t * mapping,
2517 twice_nat_type_t * twice_nat,
2518 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2519 u8 * is_identity_nat)
2521 clib_bihash_kv_8_8_t kv, value;
2522 snat_static_mapping_t *m;
2523 snat_session_key_t m_key;
2524 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2525 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2527 nat44_lb_addr_port_t *local;
2529 m_key.fib_index = match.fib_index;
2532 mapping_hash = &sm->static_mapping_by_external;
2533 m_key.fib_index = 0;
2536 m_key.addr = match.addr;
2537 m_key.port = clib_net_to_host_u16 (match.port);
2538 m_key.protocol = match.protocol;
2540 kv.key = m_key.as_u64;
2542 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2544 /* Try address only mapping */
2547 kv.key = m_key.as_u64;
2548 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2552 m = pool_elt_at_index (sm->static_mappings, value.value);
2556 if (is_lb_static_mapping (m))
2558 if (PREDICT_FALSE (lb != 0))
2559 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2560 if (m->affinity && !nat_affinity_find_and_lock (ext_host_addr[0],
2566 local = pool_elt_at_index (m->locals, backend_index);
2567 mapping->addr = local->addr;
2568 mapping->port = clib_host_to_net_u16 (local->port);
2569 mapping->fib_index = local->fib_index;
2572 // pick locals matching this worker
2573 if (PREDICT_FALSE (sm->num_workers > 1))
2575 u32 thread_index = vlib_get_thread_index ();
2577 pool_foreach_index (i, m->locals,
2579 local = pool_elt_at_index (m->locals, i);
2582 .src_address = local->addr,
2585 if (sm->worker_in2out_cb (&ip, m->fib_index, 0) ==
2592 ASSERT (vec_len (tmp) != 0);
2597 pool_foreach_index (i, m->locals,
2603 hi = vec_len (tmp) - 1;
2604 local = pool_elt_at_index (m->locals, tmp[hi]);
2605 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2608 mid = ((hi - lo) >> 1) + lo;
2609 local = pool_elt_at_index (m->locals, tmp[mid]);
2610 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2612 local = pool_elt_at_index (m->locals, tmp[lo]);
2613 if (!(local->prefix >= rand))
2615 mapping->addr = local->addr;
2616 mapping->port = clib_host_to_net_u16 (local->port);
2617 mapping->fib_index = local->fib_index;
2620 if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr,
2621 match.protocol, match.port,
2622 tmp[lo], m->affinity,
2623 m->affinity_per_service_list_head_index))
2624 nat_elog_info ("create affinity record failed");
2630 if (PREDICT_FALSE (lb != 0))
2632 mapping->fib_index = m->fib_index;
2633 mapping->addr = m->local_addr;
2634 /* Address only mapping doesn't change port */
2635 mapping->port = is_addr_only_static_mapping (m) ? match.port
2636 : clib_host_to_net_u16 (m->local_port);
2638 mapping->protocol = m->proto;
2642 mapping->addr = m->external_addr;
2643 /* Address only mapping doesn't change port */
2644 mapping->port = is_addr_only_static_mapping (m) ? match.port
2645 : clib_host_to_net_u16 (m->external_port);
2646 mapping->fib_index = sm->outside_fib_index;
2650 if (PREDICT_FALSE (is_addr_only != 0))
2651 *is_addr_only = is_addr_only_static_mapping (m);
2653 if (PREDICT_FALSE (twice_nat != 0))
2654 *twice_nat = m->twice_nat;
2656 if (PREDICT_FALSE (is_identity_nat != 0))
2657 *is_identity_nat = is_identity_static_mapping (m);
2662 static_always_inline u16
2663 snat_random_port (u16 min, u16 max)
2665 snat_main_t *sm = &snat_main;
2666 return min + random_u32 (&sm->random_seed) /
2667 (random_u32_max () / (max - min + 1) + 1);
2671 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2674 snat_session_key_t * k,
2675 u16 port_per_thread,
2676 u32 snat_thread_index)
2678 snat_main_t *sm = &snat_main;
2680 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, k,
2681 port_per_thread, snat_thread_index);
2685 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2688 snat_session_key_t * k,
2689 u16 port_per_thread, u32 snat_thread_index)
2692 snat_address_t *a, *ga = 0;
2695 for (i = 0; i < vec_len (addresses); i++)
2698 switch (k->protocol)
2700 #define _(N, j, n, s) \
2701 case SNAT_PROTOCOL_##N: \
2702 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2704 if (a->fib_index == fib_index) \
2708 portnum = (port_per_thread * \
2709 snat_thread_index) + \
2710 snat_random_port(1, port_per_thread) + 1024; \
2711 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2713 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2714 a->busy_##n##_ports_per_thread[thread_index]++; \
2715 a->busy_##n##_ports++; \
2716 k->addr = a->addr; \
2717 k->port = clib_host_to_net_u16(portnum); \
2721 else if (a->fib_index == ~0) \
2727 foreach_snat_protocol
2730 nat_elog_info ("unknown protocol");
2739 switch (k->protocol)
2741 #define _(N, j, n, s) \
2742 case SNAT_PROTOCOL_##N: \
2745 portnum = (port_per_thread * \
2746 snat_thread_index) + \
2747 snat_random_port(1, port_per_thread) + 1024; \
2748 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2750 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2751 a->busy_##n##_ports_per_thread[thread_index]++; \
2752 a->busy_##n##_ports++; \
2753 k->addr = a->addr; \
2754 k->port = clib_host_to_net_u16(portnum); \
2758 foreach_snat_protocol
2761 nat_elog_info ("unknown protocol");
2766 /* Totally out of translations to use... */
2767 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2772 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2775 snat_session_key_t * k,
2776 u16 port_per_thread, u32 snat_thread_index)
2778 snat_main_t *sm = &snat_main;
2779 snat_address_t *a = addresses;
2780 u16 m, ports, portnum, A, j;
2781 m = 16 - (sm->psid_offset + sm->psid_length);
2782 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2784 if (!vec_len (addresses))
2787 switch (k->protocol)
2789 #define _(N, i, n, s) \
2790 case SNAT_PROTOCOL_##N: \
2791 if (a->busy_##n##_ports < ports) \
2795 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2796 j = snat_random_port(0, pow2_mask(m)); \
2797 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2798 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2800 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2801 a->busy_##n##_ports++; \
2802 k->addr = a->addr; \
2803 k->port = clib_host_to_net_u16 (portnum); \
2808 foreach_snat_protocol
2811 nat_elog_info ("unknown protocol");
2816 /* Totally out of translations to use... */
2817 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2822 nat_alloc_addr_and_port_range (snat_address_t * addresses,
2825 snat_session_key_t * k,
2826 u16 port_per_thread, u32 snat_thread_index)
2828 snat_main_t *sm = &snat_main;
2829 snat_address_t *a = addresses;
2832 ports = sm->end_port - sm->start_port + 1;
2834 if (!vec_len (addresses))
2837 switch (k->protocol)
2839 #define _(N, i, n, s) \
2840 case SNAT_PROTOCOL_##N: \
2841 if (a->busy_##n##_ports < ports) \
2845 portnum = snat_random_port(sm->start_port, sm->end_port); \
2846 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2848 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2849 a->busy_##n##_ports++; \
2850 k->addr = a->addr; \
2851 k->port = clib_host_to_net_u16 (portnum); \
2856 foreach_snat_protocol
2859 nat_elog_info ("unknown protocol");
2864 /* Totally out of translations to use... */
2865 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2870 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2872 dpo_id_t dpo_v4 = DPO_INVALID;
2873 fib_prefix_t pfx = {
2874 .fp_proto = FIB_PROTOCOL_IP4,
2876 .fp_addr.ip4.as_u32 = addr.as_u32,
2881 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2882 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2883 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2884 dpo_reset (&dpo_v4);
2888 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2893 format_session_kvp (u8 * s, va_list * args)
2895 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2896 snat_session_key_t k;
2900 s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
2906 format_static_mapping_kvp (u8 * s, va_list * args)
2908 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2909 snat_session_key_t k;
2913 s = format (s, "%U static-mapping-index %llu",
2914 format_static_mapping_key, &k, v->value);
2920 format_user_kvp (u8 * s, va_list * args)
2922 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2927 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2928 k.fib_index, v->value);
2934 format_ed_session_kvp (u8 * s, va_list * args)
2936 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
2939 k.as_u64[0] = v->key[0];
2940 k.as_u64[1] = v->key[1];
2943 format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2944 format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
2945 format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
2946 format_ip_protocol, k.proto, k.fib_index, v->value);
2952 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
2955 snat_main_t *sm = &snat_main;
2956 u32 next_worker_index = 0;
2959 next_worker_index = sm->first_worker_index;
2960 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2961 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
2963 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2964 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2966 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2968 return next_worker_index;
2972 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0,
2975 snat_main_t *sm = &snat_main;
2978 snat_session_key_t m_key;
2979 clib_bihash_kv_8_8_t kv, value;
2980 snat_static_mapping_t *m;
2982 u32 next_worker_index = 0;
2984 /* first try static mappings without port */
2985 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2987 m_key.addr = ip0->dst_address;
2990 m_key.fib_index = rx_fib_index0;
2991 kv.key = m_key.as_u64;
2992 if (!clib_bihash_search_8_8
2993 (&sm->static_mapping_by_external, &kv, &value))
2995 m = pool_elt_at_index (sm->static_mappings, value.value);
2996 return m->workers[0];
3000 proto = ip_proto_to_snat_proto (ip0->protocol);
3001 udp = ip4_next_header (ip0);
3002 port = udp->dst_port;
3004 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
3006 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
3007 return vlib_get_thread_index ();
3009 nat_reass_ip4_t *reass;
3010 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
3011 ip0->fragment_id, ip0->protocol);
3013 if (reass && (reass->thread_index != (u32) ~ 0))
3014 return reass->thread_index;
3016 if (ip4_is_first_fragment (ip0))
3019 nat_ip4_reass_create (ip0->src_address, ip0->dst_address,
3020 ip0->fragment_id, ip0->protocol);
3024 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3026 m_key.addr = ip0->dst_address;
3027 m_key.port = clib_net_to_host_u16 (port);
3028 m_key.protocol = proto;
3029 m_key.fib_index = rx_fib_index0;
3030 kv.key = m_key.as_u64;
3031 if (!clib_bihash_search_8_8
3032 (&sm->static_mapping_by_external, &kv, &value))
3034 m = pool_elt_at_index (sm->static_mappings, value.value);
3035 reass->thread_index = m->workers[0];
3036 return reass->thread_index;
3039 reass->thread_index = sm->first_worker_index;
3040 reass->thread_index +=
3041 sm->workers[(clib_net_to_host_u16 (port) - 1024) /
3042 sm->port_per_thread];
3043 return reass->thread_index;
3046 return vlib_get_thread_index ();
3050 /* unknown protocol */
3051 if (PREDICT_FALSE (proto == ~0))
3053 /* use current thread */
3054 return vlib_get_thread_index ();
3057 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
3059 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3060 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3061 if (!icmp_is_error_message (icmp))
3062 port = echo->identifier;
3065 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3066 proto = ip_proto_to_snat_proto (inner_ip->protocol);
3067 void *l4_header = ip4_next_header (inner_ip);
3070 case SNAT_PROTOCOL_ICMP:
3071 icmp = (icmp46_header_t *) l4_header;
3072 echo = (icmp_echo_header_t *) (icmp + 1);
3073 port = echo->identifier;
3075 case SNAT_PROTOCOL_UDP:
3076 case SNAT_PROTOCOL_TCP:
3077 port = ((tcp_udp_header_t *) l4_header)->src_port;
3080 return vlib_get_thread_index ();
3085 /* try static mappings with port */
3086 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3088 m_key.addr = ip0->dst_address;
3089 m_key.port = clib_net_to_host_u16 (port);
3090 m_key.protocol = proto;
3091 m_key.fib_index = rx_fib_index0;
3092 kv.key = m_key.as_u64;
3093 if (!clib_bihash_search_8_8
3094 (&sm->static_mapping_by_external, &kv, &value))
3096 m = pool_elt_at_index (sm->static_mappings, value.value);
3097 return m->workers[0];
3101 /* worker by outside port */
3102 next_worker_index = sm->first_worker_index;
3103 next_worker_index +=
3104 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3105 return next_worker_index;
3109 nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
3112 snat_main_t *sm = &snat_main;
3113 u32 next_worker_index = sm->first_worker_index;
3116 clib_bihash_kv_16_8_t kv16, value16;
3117 snat_main_per_thread_data_t *tsm;
3120 if (PREDICT_FALSE (is_output))
3122 u32 fib_index = sm->outside_fib_index;
3123 nat_outside_fib_t *outside_fib;
3124 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3125 fib_prefix_t pfx = {
3126 .fp_proto = FIB_PROTOCOL_IP4,
3129 .ip4.as_u32 = ip->dst_address.as_u32,
3134 udp = ip4_next_header (ip);
3136 switch (vec_len (sm->outside_fibs))
3139 fib_index = sm->outside_fib_index;
3142 fib_index = sm->outside_fibs[0].fib_index;
3146 vec_foreach (outside_fib, sm->outside_fibs)
3148 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3149 if (FIB_NODE_INDEX_INVALID != fei)
3151 if (fib_entry_get_resolving_interface (fei) != ~0)
3153 fib_index = outside_fib->fib_index;
3162 make_ed_kv (&kv16, &ip->src_address, &ip->dst_address,
3163 ip->protocol, fib_index, udp->src_port, udp->dst_port);
3166 vec_foreach (tsm, sm->per_thread_data)
3168 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&tsm->out2in_ed,
3171 next_worker_index += tsm->thread_index;
3173 nat_elog_debug_handoff (
3174 "HANDOFF IN2OUT-OUTPUT-FEATURE (session)",
3175 next_worker_index, fib_index,
3176 clib_net_to_host_u32 (ip->src_address.as_u32),
3177 clib_net_to_host_u32 (ip->dst_address.as_u32));
3179 return next_worker_index;
3185 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3186 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3188 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
3189 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
3191 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
3193 if (PREDICT_TRUE (!is_output))
3195 nat_elog_debug_handoff ("HANDOFF IN2OUT",
3196 next_worker_index, rx_fib_index,
3197 clib_net_to_host_u32 (ip->src_address.as_u32),
3198 clib_net_to_host_u32 (ip->dst_address.as_u32));
3202 nat_elog_debug_handoff ("HANDOFF IN2OUT-OUTPUT-FEATURE",
3203 next_worker_index, rx_fib_index,
3204 clib_net_to_host_u32 (ip->src_address.as_u32),
3205 clib_net_to_host_u32 (ip->dst_address.as_u32));
3208 return next_worker_index;
3212 nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index,
3215 snat_main_t *sm = &snat_main;
3216 clib_bihash_kv_8_8_t kv, value;
3217 clib_bihash_kv_16_8_t kv16, value16;
3218 snat_main_per_thread_data_t *tsm;
3220 u32 proto, next_worker_index = 0;
3223 snat_static_mapping_t *m;
3226 proto = ip_proto_to_snat_proto (ip->protocol);
3228 if (PREDICT_TRUE (proto == SNAT_PROTOCOL_UDP || proto == SNAT_PROTOCOL_TCP))
3230 udp = ip4_next_header (ip);
3232 make_ed_kv (&kv16, &ip->dst_address, &ip->src_address,
3233 ip->protocol, rx_fib_index, udp->dst_port, udp->src_port);
3236 vec_foreach (tsm, sm->per_thread_data)
3238 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&tsm->out2in_ed,
3241 next_worker_index = sm->first_worker_index + tsm->thread_index;
3242 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3243 next_worker_index, rx_fib_index,
3244 clib_net_to_host_u32 (ip->src_address.as_u32),
3245 clib_net_to_host_u32 (ip->dst_address.as_u32));
3246 return next_worker_index;
3251 else if (proto == SNAT_PROTOCOL_ICMP)
3253 nat_ed_ses_key_t key;
3255 if (!get_icmp_o2i_ed_key (ip, &key))
3258 key.fib_index = rx_fib_index;
3259 kv16.key[0] = key.as_u64[0];
3260 kv16.key[1] = key.as_u64[1];
3263 vec_foreach (tsm, sm->per_thread_data)
3265 if (PREDICT_TRUE (!clib_bihash_search_16_8 (&tsm->out2in_ed,
3268 next_worker_index = sm->first_worker_index +
3270 nat_elog_debug_handoff ("HANDOFF OUT2IN (session)",
3271 next_worker_index, rx_fib_index,
3272 clib_net_to_host_u32 (ip->src_address.as_u32),
3273 clib_net_to_host_u32 (ip->dst_address.as_u32));
3274 return next_worker_index;
3281 /* first try static mappings without port */
3282 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3284 make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
3285 if (!clib_bihash_search_8_8
3286 (&sm->static_mapping_by_external, &kv, &value))
3288 m = pool_elt_at_index (sm->static_mappings, value.value);
3289 next_worker_index = m->workers[0];
3294 /* unknown protocol */
3295 if (PREDICT_FALSE (proto == ~0))
3297 /* use current thread */
3298 next_worker_index = vlib_get_thread_index ();
3302 udp = ip4_next_header (ip);
3303 port = udp->dst_port;
3305 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
3307 icmp46_header_t *icmp = (icmp46_header_t *) udp;
3308 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
3309 if (!icmp_is_error_message (icmp))
3310 port = echo->identifier;
3313 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
3314 proto = ip_proto_to_snat_proto (inner_ip->protocol);
3315 void *l4_header = ip4_next_header (inner_ip);
3318 case SNAT_PROTOCOL_ICMP:
3319 icmp = (icmp46_header_t *) l4_header;
3320 echo = (icmp_echo_header_t *) (icmp + 1);
3321 port = echo->identifier;
3323 case SNAT_PROTOCOL_UDP:
3324 case SNAT_PROTOCOL_TCP:
3325 port = ((tcp_udp_header_t *) l4_header)->src_port;
3328 next_worker_index = vlib_get_thread_index ();
3334 /* try static mappings with port */
3335 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
3337 make_sm_kv (&kv, &ip->dst_address, proto, 0,
3338 clib_net_to_host_u16 (port));
3339 if (!clib_bihash_search_8_8
3340 (&sm->static_mapping_by_external, &kv, &value))
3342 m = pool_elt_at_index (sm->static_mappings, value.value);
3343 if (!is_lb_static_mapping (m))
3345 next_worker_index = m->workers[0];
3349 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
3350 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
3352 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
3354 m->workers[hash & (_vec_len (m->workers) - 1)];
3356 next_worker_index = m->workers[hash % _vec_len (m->workers)];
3361 /* worker by outside port */
3362 next_worker_index = sm->first_worker_index;
3363 next_worker_index +=
3364 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
3367 nat_elog_debug_handoff ("HANDOFF OUT2IN", next_worker_index, rx_fib_index,
3368 clib_net_to_host_u32 (ip->src_address.as_u32),
3369 clib_net_to_host_u32 (ip->dst_address.as_u32));
3370 return next_worker_index;
3374 nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
3375 ip4_address_t * out_addr, u16 out_port,
3376 ip4_address_t * eh_addr, u16 eh_port,
3377 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3378 u32 fib_index, u16 flags, u32 thread_index)
3380 snat_main_t *sm = &snat_main;
3381 snat_session_key_t key;
3384 clib_bihash_kv_8_8_t kv;
3385 f64 now = vlib_time_now (sm->vlib_main);
3386 nat_outside_fib_t *outside_fib;
3387 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3388 snat_main_per_thread_data_t *tsm;
3389 fib_prefix_t pfx = {
3390 .fp_proto = FIB_PROTOCOL_IP4,
3393 .ip4.as_u32 = eh_addr->as_u32,
3397 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3399 key.addr.as_u32 = out_addr->as_u32;
3400 key.port = out_port;
3401 key.protocol = proto;
3403 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3405 if (nat_set_outside_address_and_port
3406 (sm->addresses, thread_index, &key))
3410 u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3414 s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
3418 s->last_heard = now;
3420 s->ext_host_addr.as_u32 = eh_addr->as_u32;
3421 s->ext_host_port = eh_port;
3422 user_session_increment (sm, u, snat_is_session_static (s));
3423 switch (vec_len (sm->outside_fibs))
3426 key.fib_index = sm->outside_fib_index;
3429 key.fib_index = sm->outside_fibs[0].fib_index;
3433 vec_foreach (outside_fib, sm->outside_fibs)
3435 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3436 if (FIB_NODE_INDEX_INVALID != fei)
3438 if (fib_entry_get_resolving_interface (fei) != ~0)
3440 key.fib_index = outside_fib->fib_index;
3449 kv.key = key.as_u64;
3450 kv.value = s - tsm->sessions;
3451 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
3452 nat_elog_warn ("out2in key add failed");
3454 key.addr.as_u32 = in_addr->as_u32;
3456 key.fib_index = fib_index;
3458 kv.key = key.as_u64;
3459 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
3460 nat_elog_warn ("in2out key add failed");
3464 nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
3465 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3468 snat_main_t *sm = &snat_main;
3469 snat_session_key_t key;
3470 clib_bihash_kv_8_8_t kv, value;
3473 snat_main_per_thread_data_t *tsm;
3475 if (sm->num_workers > 1)
3477 sm->first_worker_index +
3478 (sm->workers[(clib_net_to_host_u16 (out_port) -
3479 1024) / sm->port_per_thread]);
3481 thread_index = sm->num_workers;
3482 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3484 key.addr.as_u32 = out_addr->as_u32;
3485 key.port = out_port;
3486 key.protocol = proto;
3487 key.fib_index = fib_index;
3488 kv.key = key.as_u64;
3489 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3492 s = pool_elt_at_index (tsm->sessions, value.value);
3493 nat_free_session_data (sm, s, thread_index, 1);
3494 nat44_delete_session (sm, s, thread_index);
3498 nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
3499 ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
3500 u32 total_pkts, u64 total_bytes, u32 thread_index)
3502 snat_main_t *sm = &snat_main;
3503 snat_session_key_t key;
3504 clib_bihash_kv_8_8_t kv, value;
3506 snat_main_per_thread_data_t *tsm;
3508 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3510 key.addr.as_u32 = out_addr->as_u32;
3511 key.port = out_port;
3512 key.protocol = proto;
3513 key.fib_index = fib_index;
3514 kv.key = key.as_u64;
3515 if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
3518 s = pool_elt_at_index (tsm->sessions, value.value);
3519 s->total_pkts = total_pkts;
3520 s->total_bytes = total_bytes;
3524 nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
3525 ip4_address_t * out_addr, u16 out_port,
3526 ip4_address_t * eh_addr, u16 eh_port,
3527 ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
3528 u32 fib_index, u16 flags, u32 thread_index)
3530 snat_main_t *sm = &snat_main;
3531 snat_session_key_t key;
3534 clib_bihash_kv_16_8_t kv;
3535 f64 now = vlib_time_now (sm->vlib_main);
3536 nat_outside_fib_t *outside_fib;
3537 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
3538 snat_main_per_thread_data_t *tsm;
3539 fib_prefix_t pfx = {
3540 .fp_proto = FIB_PROTOCOL_IP4,
3543 .ip4.as_u32 = eh_addr->as_u32,
3547 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3549 key.addr.as_u32 = out_addr->as_u32;
3550 key.port = out_port;
3551 key.protocol = proto;
3553 if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
3555 if (nat_set_outside_address_and_port
3556 (sm->addresses, thread_index, &key))
3560 key.addr.as_u32 = ehn_addr->as_u32;
3561 key.port = ehn_port;
3562 if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
3564 if (nat_set_outside_address_and_port
3565 (sm->twice_nat_addresses, thread_index, &key))
3569 u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
3573 s = nat_ed_session_alloc (sm, u, thread_index, now);
3577 s->last_heard = now;
3579 s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
3580 s->ext_host_nat_port = s->ext_host_port = eh_port;
3581 if (is_twice_nat_session (s))
3583 s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
3584 s->ext_host_nat_port = ehn_port;
3586 user_session_increment (sm, u, snat_is_session_static (s));
3587 switch (vec_len (sm->outside_fibs))
3590 key.fib_index = sm->outside_fib_index;
3593 key.fib_index = sm->outside_fibs[0].fib_index;
3597 vec_foreach (outside_fib, sm->outside_fibs)
3599 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
3600 if (FIB_NODE_INDEX_INVALID != fei)
3602 if (fib_entry_get_resolving_interface (fei) != ~0)
3604 key.fib_index = outside_fib->fib_index;
3612 key.addr.as_u32 = out_addr->as_u32;
3613 key.port = out_port;
3615 kv.value = s - tsm->sessions;
3617 key.addr.as_u32 = in_addr->as_u32;
3619 key.fib_index = fib_index;
3622 make_ed_kv (&kv, in_addr, &s->ext_host_nat_addr,
3623 snat_proto_to_ip_proto (proto), fib_index, in_port,
3624 s->ext_host_nat_port);
3625 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
3626 nat_elog_warn ("in2out key add failed");
3628 make_ed_kv (&kv, out_addr, eh_addr, snat_proto_to_ip_proto (proto),
3629 s->out2in.fib_index, out_port, eh_port);
3630 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 1))
3631 nat_elog_warn ("out2in key add failed");
3635 nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
3636 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3637 u32 fib_index, u32 ti)
3639 snat_main_t *sm = &snat_main;
3640 nat_ed_ses_key_t key;
3641 clib_bihash_kv_16_8_t kv, value;
3644 snat_main_per_thread_data_t *tsm;
3646 if (sm->num_workers > 1)
3648 sm->first_worker_index +
3649 (sm->workers[(clib_net_to_host_u16 (out_port) -
3650 1024) / sm->port_per_thread]);
3652 thread_index = sm->num_workers;
3653 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3655 key.l_addr.as_u32 = out_addr->as_u32;
3656 key.l_port = out_port;
3657 key.r_addr.as_u32 = eh_addr->as_u32;
3658 key.r_port = eh_port;
3660 key.fib_index = fib_index;
3661 kv.key[0] = key.as_u64[0];
3662 kv.key[1] = key.as_u64[1];
3663 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
3666 s = pool_elt_at_index (tsm->sessions, value.value);
3667 nat_free_session_data (sm, s, thread_index, 1);
3668 nat44_delete_session (sm, s, thread_index);
3672 nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
3673 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3674 u32 fib_index, u32 total_pkts, u64 total_bytes,
3677 snat_main_t *sm = &snat_main;
3678 nat_ed_ses_key_t key;
3679 clib_bihash_kv_16_8_t kv, value;
3681 snat_main_per_thread_data_t *tsm;
3683 tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
3685 key.l_addr.as_u32 = out_addr->as_u32;
3686 key.l_port = out_port;
3687 key.r_addr.as_u32 = eh_addr->as_u32;
3688 key.r_port = eh_port;
3690 key.fib_index = fib_index;
3691 kv.key[0] = key.as_u64[0];
3692 kv.key[1] = key.as_u64[1];
3693 if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
3696 s = pool_elt_at_index (tsm->sessions, value.value);
3697 s->total_pkts = total_pkts;
3698 s->total_bytes = total_bytes;
3701 static clib_error_t *
3702 snat_config (vlib_main_t * vm, unformat_input_t * input)
3704 snat_main_t *sm = &snat_main;
3705 nat66_main_t *nm = &nat66_main;
3706 u32 translation_buckets = 1024;
3707 u32 translation_memory_size = 128 << 20;
3708 u32 user_buckets = 128;
3709 u32 user_memory_size = 64 << 20;
3710 u32 max_translations_per_user = 100;
3711 u32 outside_vrf_id = 0;
3712 u32 outside_ip6_vrf_id = 0;
3713 u32 inside_vrf_id = 0;
3714 u32 static_mapping_buckets = 1024;
3715 u32 static_mapping_memory_size = 64 << 20;
3716 u32 nat64_bib_buckets = 1024;
3717 u32 nat64_bib_memory_size = 128 << 20;
3718 u32 nat64_st_buckets = 2048;
3719 u32 nat64_st_memory_size = 256 << 20;
3720 u8 static_mapping_only = 0;
3721 u8 static_mapping_connection_tracking = 0;
3722 snat_main_per_thread_data_t *tsm;
3723 dslite_main_t *dm = &dslite_main;
3725 sm->deterministic = 0;
3727 sm->endpoint_dependent = 0;
3729 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3732 (input, "translation hash buckets %d", &translation_buckets))
3734 else if (unformat (input, "translation hash memory %d",
3735 &translation_memory_size));
3736 else if (unformat (input, "user hash buckets %d", &user_buckets))
3738 else if (unformat (input, "user hash memory %d", &user_memory_size))
3740 else if (unformat (input, "max translations per user %d",
3741 &max_translations_per_user))
3743 else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
3745 else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
3747 else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
3749 else if (unformat (input, "static mapping only"))
3751 static_mapping_only = 1;
3752 if (unformat (input, "connection tracking"))
3753 static_mapping_connection_tracking = 1;
3755 else if (unformat (input, "deterministic"))
3756 sm->deterministic = 1;
3757 else if (unformat (input, "nat64 bib hash buckets %d",
3758 &nat64_bib_buckets))
3760 else if (unformat (input, "nat64 bib hash memory %d",
3761 &nat64_bib_memory_size))
3764 if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
3766 else if (unformat (input, "nat64 st hash memory %d",
3767 &nat64_st_memory_size))
3769 else if (unformat (input, "out2in dpo"))
3771 else if (unformat (input, "dslite ce"))
3772 dslite_set_ce (dm, 1);
3773 else if (unformat (input, "endpoint-dependent"))
3774 sm->endpoint_dependent = 1;
3776 return clib_error_return (0, "unknown input '%U'",
3777 format_unformat_error, input);
3780 if (sm->deterministic && sm->endpoint_dependent)
3781 return clib_error_return (0,
3782 "deterministic and endpoint-dependent modes are mutually exclusive");
3784 if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
3785 return clib_error_return (0,
3786 "static mapping only mode available only for simple nat");
3788 if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
3789 return clib_error_return (0,
3790 "out2in dpo mode available only for simple nat");
3792 /* for show commands, etc. */
3793 sm->translation_buckets = translation_buckets;
3794 sm->translation_memory_size = translation_memory_size;
3795 /* do not exceed load factor 10 */
3796 sm->max_translations = 10 * translation_buckets;
3797 sm->user_buckets = user_buckets;
3798 sm->user_memory_size = user_memory_size;
3799 sm->max_translations_per_user = max_translations_per_user;
3800 sm->outside_vrf_id = outside_vrf_id;
3801 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
3803 FIB_SOURCE_PLUGIN_HI);
3804 nm->outside_vrf_id = outside_ip6_vrf_id;
3805 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3807 FIB_SOURCE_PLUGIN_HI);
3808 sm->inside_vrf_id = inside_vrf_id;
3809 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
3811 FIB_SOURCE_PLUGIN_HI);
3812 sm->static_mapping_only = static_mapping_only;
3813 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
3815 nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
3816 nat64_st_memory_size);
3818 if (sm->deterministic)
3820 sm->in2out_node_index = snat_det_in2out_node.index;
3821 sm->in2out_output_node_index = ~0;
3822 sm->out2in_node_index = snat_det_out2in_node.index;
3823 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
3824 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
3828 if (sm->endpoint_dependent)
3830 sm->worker_in2out_cb = nat44_ed_get_worker_in2out_cb;
3831 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
3832 sm->in2out_node_index = nat44_ed_in2out_node.index;
3833 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
3834 sm->out2in_node_index = nat44_ed_out2in_node.index;
3835 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
3836 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
3837 nat_affinity_init (vm);
3838 nat_ha_init (vm, nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb,
3843 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
3844 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
3845 sm->in2out_node_index = snat_in2out_node.index;
3846 sm->in2out_output_node_index = snat_in2out_output_node.index;
3847 sm->out2in_node_index = snat_out2in_node.index;
3848 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
3849 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
3850 nat_ha_init (vm, nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
3852 if (!static_mapping_only ||
3853 (static_mapping_only && static_mapping_connection_tracking))
3856 vec_foreach (tsm, sm->per_thread_data)
3858 if (sm->endpoint_dependent)
3860 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
3861 translation_buckets,
3862 translation_memory_size);
3863 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
3864 format_ed_session_kvp);
3866 clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
3867 translation_buckets,
3868 translation_memory_size);
3869 clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
3870 format_ed_session_kvp);
3874 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
3875 translation_buckets,
3876 translation_memory_size);
3877 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
3878 format_session_kvp);
3880 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
3881 translation_buckets,
3882 translation_memory_size);
3883 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
3884 format_session_kvp);
3887 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
3889 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
3897 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
3898 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
3900 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
3901 "static_mapping_by_local", static_mapping_buckets,
3902 static_mapping_memory_size);
3903 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
3904 format_static_mapping_kvp);
3906 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
3907 "static_mapping_by_external",
3908 static_mapping_buckets,
3909 static_mapping_memory_size);
3910 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
3911 format_static_mapping_kvp);
3917 VLIB_CONFIG_FUNCTION (snat_config, "nat");
3920 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
3923 ip4_address_t * address,
3925 u32 if_address_index, u32 is_delete)
3927 snat_main_t *sm = &snat_main;
3928 snat_static_map_resolve_t *rp;
3929 snat_static_mapping_t *m;
3930 snat_session_key_t m_key;
3931 clib_bihash_kv_8_8_t kv, value;
3933 ip4_address_t l_addr;
3935 for (i = 0; i < vec_len (sm->to_resolve); i++)
3937 rp = sm->to_resolve + i;
3938 if (rp->addr_only == 0)
3940 if (rp->sw_if_index == sw_if_index)
3947 m_key.addr.as_u32 = address->as_u32;
3948 m_key.port = rp->addr_only ? 0 : rp->e_port;
3949 m_key.protocol = rp->addr_only ? 0 : rp->proto;
3950 m_key.fib_index = sm->outside_fib_index;
3951 kv.key = m_key.as_u64;
3952 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3955 m = pool_elt_at_index (sm->static_mappings, value.value);
3959 /* Don't trip over lease renewal, static config */
3969 /* Indetity mapping? */
3970 if (rp->l_addr.as_u32 == 0)
3971 l_addr.as_u32 = address[0].as_u32;
3973 l_addr.as_u32 = rp->l_addr.as_u32;
3974 /* Add the static mapping */
3975 rv = snat_add_static_mapping (l_addr,
3980 rp->addr_only, ~0 /* sw_if_index */ ,
3981 rp->proto, !is_delete, rp->twice_nat,
3982 rp->out2in_only, rp->tag, rp->identity_nat);
3984 nat_elog_notice_X1 ("snat_add_static_mapping returned %d", "i4", rv);
3988 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3991 ip4_address_t * address,
3993 u32 if_address_index, u32 is_delete)
3995 snat_main_t *sm = &snat_main;
3996 snat_static_map_resolve_t *rp;
3997 ip4_address_t l_addr;
4001 snat_address_t *addresses = sm->addresses;
4003 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
4005 if (sw_if_index == sm->auto_add_sw_if_indices[i])
4009 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
4012 addresses = sm->twice_nat_addresses;
4013 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
4022 /* Don't trip over lease renewal, static config */
4023 for (j = 0; j < vec_len (addresses); j++)
4024 if (addresses[j].addr.as_u32 == address->as_u32)
4027 (void) snat_add_address (sm, address, ~0, twice_nat);
4028 /* Scan static map resolution vector */
4029 for (j = 0; j < vec_len (sm->to_resolve); j++)
4031 rp = sm->to_resolve + j;
4034 /* On this interface? */
4035 if (rp->sw_if_index == sw_if_index)
4037 /* Indetity mapping? */
4038 if (rp->l_addr.as_u32 == 0)
4039 l_addr.as_u32 = address[0].as_u32;
4041 l_addr.as_u32 = rp->l_addr.as_u32;
4042 /* Add the static mapping */
4043 rv = snat_add_static_mapping (l_addr,
4049 ~0 /* sw_if_index */ ,
4051 rp->is_add, rp->twice_nat,
4052 rp->out2in_only, rp->tag,
4055 nat_elog_notice_X1 ("snat_add_static_mapping returned %d",
4063 (void) snat_del_address (sm, address[0], 1, twice_nat);
4070 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
4073 ip4_main_t *ip4_main = sm->ip4_main;
4074 ip4_address_t *first_int_addr;
4075 snat_static_map_resolve_t *rp;
4076 u32 *indices_to_delete = 0;
4078 u32 *auto_add_sw_if_indices =
4080 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
4082 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
4085 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
4087 if (auto_add_sw_if_indices[i] == sw_if_index)
4091 /* if have address remove it */
4093 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
4096 for (j = 0; j < vec_len (sm->to_resolve); j++)
4098 rp = sm->to_resolve + j;
4099 if (rp->sw_if_index == sw_if_index)
4100 vec_add1 (indices_to_delete, j);
4102 if (vec_len (indices_to_delete))
4104 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
4105 vec_del1 (sm->to_resolve, j);
4106 vec_free (indices_to_delete);
4110 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
4112 vec_del1 (sm->auto_add_sw_if_indices, i);
4115 return VNET_API_ERROR_VALUE_EXIST;
4122 return VNET_API_ERROR_NO_SUCH_ENTRY;
4124 /* add to the auto-address list */
4126 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
4128 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
4130 /* If the address is already bound - or static - add it now */
4132 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
4138 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4139 snat_protocol_t proto, u32 vrf_id, int is_in)
4141 snat_main_per_thread_data_t *tsm;
4142 clib_bihash_kv_8_8_t kv, value;
4144 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4145 snat_session_key_t key;
4147 clib_bihash_8_8_t *t;
4149 if (sm->endpoint_dependent)
4150 return VNET_API_ERROR_UNSUPPORTED;
4152 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4153 if (sm->num_workers > 1)
4155 vec_elt_at_index (sm->per_thread_data,
4156 sm->worker_in2out_cb (&ip, fib_index, 0));
4158 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4160 key.addr.as_u32 = addr->as_u32;
4161 key.port = clib_host_to_net_u16 (port);
4162 key.protocol = proto;
4163 key.fib_index = fib_index;
4164 kv.key = key.as_u64;
4165 t = is_in ? &tsm->in2out : &tsm->out2in;
4166 if (!clib_bihash_search_8_8 (t, &kv, &value))
4168 if (pool_is_free_index (tsm->sessions, value.value))
4169 return VNET_API_ERROR_UNSPECIFIED;
4171 s = pool_elt_at_index (tsm->sessions, value.value);
4172 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4173 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4177 return VNET_API_ERROR_NO_SUCH_ENTRY;
4181 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
4182 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
4183 u32 vrf_id, int is_in)
4186 clib_bihash_16_8_t *t;
4187 nat_ed_ses_key_t key;
4188 clib_bihash_kv_16_8_t kv, value;
4189 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
4191 snat_main_per_thread_data_t *tsm;
4193 if (!sm->endpoint_dependent)
4194 return VNET_API_ERROR_FEATURE_DISABLED;
4196 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
4197 if (sm->num_workers > 1)
4199 vec_elt_at_index (sm->per_thread_data,
4200 sm->worker_in2out_cb (&ip, fib_index, 0));
4202 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
4204 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
4205 key.l_addr.as_u32 = addr->as_u32;
4206 key.r_addr.as_u32 = eh_addr->as_u32;
4207 key.l_port = clib_host_to_net_u16 (port);
4208 key.r_port = clib_host_to_net_u16 (eh_port);
4210 key.fib_index = fib_index;
4211 kv.key[0] = key.as_u64[0];
4212 kv.key[1] = key.as_u64[1];
4213 if (clib_bihash_search_16_8 (t, &kv, &value))
4214 return VNET_API_ERROR_NO_SUCH_ENTRY;
4216 if (pool_is_free_index (tsm->sessions, value.value))
4217 return VNET_API_ERROR_UNSPECIFIED;
4218 s = pool_elt_at_index (tsm->sessions, value.value);
4219 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
4220 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
4225 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
4227 snat_main_t *sm = &snat_main;
4229 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
4230 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
4232 sm->psid_offset = psid_offset;
4233 sm->psid_length = psid_length;
4237 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
4239 snat_main_t *sm = &snat_main;
4241 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
4242 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
4243 sm->start_port = start_port;
4244 sm->end_port = end_port;
4248 nat_set_alloc_addr_and_port_default (void)
4250 snat_main_t *sm = &snat_main;
4252 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
4253 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
4257 * fd.io coding-style-patch-verification: ON
4260 * eval: (c-set-style "gnu")