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 <vnet/fib/fib_table.h>
33 #include <vnet/fib/ip4_fib.h>
35 #include <vpp/app/version.h>
37 snat_main_t snat_main;
41 /* Hook up input features */
42 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
43 .arc_name = "ip4-unicast",
44 .node_name = "nat44-in2out",
45 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
47 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
48 .arc_name = "ip4-unicast",
49 .node_name = "nat44-out2in",
50 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
51 "ip4-dhcp-client-detect"),
53 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
54 .arc_name = "ip4-unicast",
55 .node_name = "nat44-classify",
56 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
58 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
59 .arc_name = "ip4-unicast",
60 .node_name = "nat44-det-in2out",
61 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
63 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
64 .arc_name = "ip4-unicast",
65 .node_name = "nat44-det-out2in",
66 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
67 "ip4-dhcp-client-detect"),
69 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
70 .arc_name = "ip4-unicast",
71 .node_name = "nat44-det-classify",
72 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
74 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
75 .arc_name = "ip4-unicast",
76 .node_name = "nat44-ed-in2out",
77 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
79 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
80 .arc_name = "ip4-unicast",
81 .node_name = "nat44-ed-out2in",
82 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
83 "ip4-dhcp-client-detect"),
85 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
86 .arc_name = "ip4-unicast",
87 .node_name = "nat44-ed-classify",
88 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
90 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
91 .arc_name = "ip4-unicast",
92 .node_name = "nat44-in2out-worker-handoff",
93 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
95 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
96 .arc_name = "ip4-unicast",
97 .node_name = "nat44-out2in-worker-handoff",
98 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
99 "ip4-dhcp-client-detect"),
101 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "nat44-handoff-classify",
104 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
106 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
107 .arc_name = "ip4-unicast",
108 .node_name = "nat44-in2out-fast",
109 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
111 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
112 .arc_name = "ip4-unicast",
113 .node_name = "nat44-out2in-fast",
114 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
115 "ip4-dhcp-client-detect"),
117 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
118 .arc_name = "ip4-unicast",
119 .node_name = "nat44-hairpin-dst",
120 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
122 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
123 .arc_name = "ip4-unicast",
124 .node_name = "nat44-ed-hairpin-dst",
125 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
128 /* Hook up output features */
129 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
130 .arc_name = "ip4-output",
131 .node_name = "nat44-in2out-output",
132 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
134 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
135 .arc_name = "ip4-output",
136 .node_name = "nat44-in2out-output-worker-handoff",
137 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
139 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
140 .arc_name = "ip4-output",
141 .node_name = "nat44-hairpin-src",
142 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
144 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
145 .arc_name = "ip4-output",
146 .node_name = "nat44-ed-in2out-output",
147 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
149 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
150 .arc_name = "ip4-output",
151 .node_name = "nat44-ed-hairpin-src",
152 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
155 /* Hook up ip4-local features */
156 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
158 .arc_name = "ip4-local",
159 .node_name = "nat44-hairpinning",
160 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
162 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
164 .arc_name = "ip4-local",
165 .node_name = "nat44-ed-hairpinning",
166 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
170 VLIB_PLUGIN_REGISTER () = {
171 .version = VPP_BUILD_VER,
172 .description = "Network Address Translation",
177 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
179 snat_session_key_t key;
180 clib_bihash_kv_8_8_t kv;
181 nat_ed_ses_key_t ed_key;
182 clib_bihash_kv_16_8_t ed_kv;
183 snat_main_per_thread_data_t *tsm =
184 vec_elt_at_index (sm->per_thread_data, thread_index);
186 if (is_fwd_bypass_session (s))
188 ed_key.l_addr = s->in2out.addr;
189 ed_key.r_addr = s->ext_host_addr;
190 ed_key.l_port = s->in2out.port;
191 ed_key.r_port = s->ext_host_port;
192 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
193 ed_key.fib_index = 0;
194 ed_kv.key[0] = ed_key.as_u64[0];
195 ed_kv.key[1] = ed_key.as_u64[1];
196 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
197 nat_log_warn ("in2out_ed key del failed");
201 /* session lookup tables */
202 if (is_ed_session (s))
204 if (is_affinity_sessions (s))
205 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
206 s->in2out.protocol, s->out2in.port);
207 ed_key.l_addr = s->out2in.addr;
208 ed_key.r_addr = s->ext_host_addr;
209 ed_key.fib_index = s->out2in.fib_index;
210 if (snat_is_unk_proto_session (s))
212 ed_key.proto = s->in2out.port;
218 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
219 ed_key.l_port = s->out2in.port;
220 ed_key.r_port = s->ext_host_port;
222 ed_kv.key[0] = ed_key.as_u64[0];
223 ed_kv.key[1] = ed_key.as_u64[1];
224 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
225 nat_log_warn ("out2in_ed key del failed");
226 ed_key.l_addr = s->in2out.addr;
227 ed_key.fib_index = s->in2out.fib_index;
228 if (!snat_is_unk_proto_session (s))
229 ed_key.l_port = s->in2out.port;
230 if (is_twice_nat_session (s))
232 ed_key.r_addr = s->ext_host_nat_addr;
233 ed_key.r_port = s->ext_host_nat_port;
235 ed_kv.key[0] = ed_key.as_u64[0];
236 ed_kv.key[1] = ed_key.as_u64[1];
237 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
238 nat_log_warn ("in2out_ed key del failed");
242 kv.key = s->in2out.as_u64;
243 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
244 nat_log_warn ("in2out key del failed");
245 kv.key = s->out2in.as_u64;
246 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
247 nat_log_warn ("out2in key del failed");
250 if (snat_is_unk_proto_session (s))
254 snat_ipfix_logging_nat44_ses_delete (s->in2out.addr.as_u32,
255 s->out2in.addr.as_u32,
258 s->out2in.port, s->in2out.fib_index);
260 /* Twice NAT address and port for external host */
261 if (is_twice_nat_session (s))
263 key.protocol = s->in2out.protocol;
264 key.port = s->ext_host_nat_port;
265 key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
266 snat_free_outside_address_and_port (sm->twice_nat_addresses,
270 if (snat_is_session_static (s))
273 snat_free_outside_address_and_port (sm->addresses, thread_index,
278 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
282 snat_user_key_t user_key;
283 clib_bihash_kv_8_8_t kv, value;
284 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
285 dlist_elt_t *per_user_list_head_elt;
287 user_key.addr.as_u32 = addr->as_u32;
288 user_key.fib_index = fib_index;
289 kv.key = user_key.as_u64;
291 /* Ever heard of the "user" = src ip4 address before? */
292 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
294 /* no, make a new one */
295 pool_get (tsm->users, u);
296 clib_memset (u, 0, sizeof (*u));
297 u->addr.as_u32 = addr->as_u32;
298 u->fib_index = fib_index;
300 pool_get (tsm->list_pool, per_user_list_head_elt);
302 u->sessions_per_user_list_head_index = per_user_list_head_elt -
305 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
307 kv.value = u - tsm->users;
310 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
311 nat_log_warn ("user_hash keay add failed");
315 u = pool_elt_at_index (tsm->users, value.value);
322 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
326 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
327 u32 oldest_per_user_translation_list_index, session_index;
328 dlist_elt_t *oldest_per_user_translation_list_elt;
329 dlist_elt_t *per_user_translation_list_elt;
331 /* Over quota? Recycle the least recently used translation */
332 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
334 oldest_per_user_translation_list_index =
335 clib_dlist_remove_head (tsm->list_pool,
336 u->sessions_per_user_list_head_index);
338 ASSERT (oldest_per_user_translation_list_index != ~0);
340 /* Add it back to the end of the LRU list */
341 clib_dlist_addtail (tsm->list_pool,
342 u->sessions_per_user_list_head_index,
343 oldest_per_user_translation_list_index);
344 /* Get the list element */
345 oldest_per_user_translation_list_elt =
346 pool_elt_at_index (tsm->list_pool,
347 oldest_per_user_translation_list_index);
349 /* Get the session index from the list element */
350 session_index = oldest_per_user_translation_list_elt->value;
352 /* Get the session */
353 s = pool_elt_at_index (tsm->sessions, session_index);
354 nat_free_session_data (sm, s, thread_index);
355 if (snat_is_session_static (s))
356 u->nstaticsessions--;
363 s->ext_host_addr.as_u32 = 0;
364 s->ext_host_port = 0;
365 s->ext_host_nat_addr.as_u32 = 0;
366 s->ext_host_nat_port = 0;
370 pool_get (tsm->sessions, s);
371 clib_memset (s, 0, sizeof (*s));
373 /* Create list elts */
374 pool_get (tsm->list_pool, per_user_translation_list_elt);
375 clib_dlist_init (tsm->list_pool,
376 per_user_translation_list_elt - tsm->list_pool);
378 per_user_translation_list_elt->value = s - tsm->sessions;
379 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
380 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
382 clib_dlist_addtail (tsm->list_pool,
383 s->per_user_list_head_index,
384 per_user_translation_list_elt - tsm->list_pool);
391 nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u, u32 thread_index,
395 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
396 dlist_elt_t *per_user_translation_list_elt, *oldest_elt;
398 u64 sess_timeout_time;
400 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
403 clib_dlist_remove_head (tsm->list_pool,
404 u->sessions_per_user_list_head_index);
405 oldest_elt = pool_elt_at_index (tsm->list_pool, oldest_index);
406 s = pool_elt_at_index (tsm->sessions, oldest_elt->value);
408 s->last_heard + (f64) nat44_session_get_timeout (sm, s);
409 if (now >= sess_timeout_time)
411 clib_dlist_addtail (tsm->list_pool,
412 u->sessions_per_user_list_head_index,
414 nat_free_session_data (sm, s, thread_index);
415 if (snat_is_session_static (s))
416 u->nstaticsessions--;
423 s->ext_host_addr.as_u32 = 0;
424 s->ext_host_port = 0;
425 s->ext_host_nat_addr.as_u32 = 0;
426 s->ext_host_nat_port = 0;
430 clib_dlist_addhead (tsm->list_pool,
431 u->sessions_per_user_list_head_index,
433 nat_log_warn ("max translations per user %U", format_ip4_address,
435 snat_ipfix_logging_max_entries_per_user
436 (sm->max_translations_per_user, u->addr.as_u32);
442 pool_get (tsm->sessions, s);
443 clib_memset (s, 0, sizeof (*s));
445 /* Create list elts */
446 pool_get (tsm->list_pool, per_user_translation_list_elt);
447 clib_dlist_init (tsm->list_pool,
448 per_user_translation_list_elt - tsm->list_pool);
450 per_user_translation_list_elt->value = s - tsm->sessions;
451 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
452 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
454 clib_dlist_addtail (tsm->list_pool,
455 s->per_user_list_head_index,
456 per_user_translation_list_elt - tsm->list_pool);
462 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
465 fib_prefix_t prefix = {
467 .fp_proto = FIB_PROTOCOL_IP4,
469 .ip4.as_u32 = addr->as_u32,
472 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
475 fib_table_entry_update_one_path (fib_index,
477 FIB_SOURCE_PLUGIN_LOW,
478 (FIB_ENTRY_FLAG_CONNECTED |
479 FIB_ENTRY_FLAG_LOCAL |
480 FIB_ENTRY_FLAG_EXCLUSIVE),
484 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
486 fib_table_entry_delete (fib_index, &prefix, FIB_SOURCE_PLUGIN_LOW);
490 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
495 vlib_thread_main_t *tm = vlib_get_thread_main ();
497 if (twice_nat && !sm->endpoint_dependent)
498 return VNET_API_ERROR_FEATURE_DISABLED;
500 /* Check if address already exists */
502 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
504 if (ap->addr.as_u32 == addr->as_u32)
505 return VNET_API_ERROR_VALUE_EXIST;
510 vec_add2 (sm->twice_nat_addresses, ap, 1);
512 vec_add2 (sm->addresses, ap, 1);
517 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
518 FIB_SOURCE_PLUGIN_LOW);
521 #define _(N, i, n, s) \
522 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
523 ap->busy_##n##_ports = 0; \
524 ap->busy_##n##_ports_per_thread = 0;\
525 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
526 foreach_snat_protocol
531 /* Add external address to FIB */
533 pool_foreach (i, sm->interfaces,
535 if (nat_interface_is_inside(i) || sm->out2in_dpo)
538 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
541 pool_foreach (i, sm->output_feature_interfaces,
543 if (nat_interface_is_inside(i) || sm->out2in_dpo)
546 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
555 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
557 snat_static_mapping_t *m;
559 pool_foreach (m, sm->static_mappings,
561 if (is_addr_only_static_mapping (m) ||
562 is_out2in_only_static_mapping (m) ||
563 is_identity_static_mapping (m))
565 if (m->external_addr.as_u32 == addr.as_u32)
574 increment_v4_address (ip4_address_t * a)
578 v = clib_net_to_host_u32 (a->as_u32) + 1;
579 a->as_u32 = clib_host_to_net_u32 (v);
583 snat_add_static_mapping_when_resolved (snat_main_t * sm,
584 ip4_address_t l_addr,
589 snat_protocol_t proto,
590 int addr_only, int is_add, u8 * tag,
591 int twice_nat, int out2in_only,
594 snat_static_map_resolve_t *rp;
596 vec_add2 (sm->to_resolve, rp, 1);
597 rp->l_addr.as_u32 = l_addr.as_u32;
599 rp->sw_if_index = sw_if_index;
603 rp->addr_only = addr_only;
605 rp->twice_nat = twice_nat;
606 rp->out2in_only = out2in_only;
607 rp->identity_nat = identity_nat;
608 rp->tag = vec_dup (tag);
612 get_thread_idx_by_port (u16 e_port)
614 snat_main_t *sm = &snat_main;
615 u32 thread_idx = sm->num_workers;
616 if (sm->num_workers > 1)
619 sm->first_worker_index +
620 sm->workers[(e_port - 1024) / sm->port_per_thread];
626 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
627 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
628 u32 sw_if_index, snat_protocol_t proto, int is_add,
629 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
632 snat_main_t *sm = &snat_main;
633 snat_static_mapping_t *m;
634 snat_session_key_t m_key;
635 clib_bihash_kv_8_8_t kv, value;
636 snat_address_t *a = 0;
638 snat_interface_t *interface;
640 snat_main_per_thread_data_t *tsm;
641 snat_user_key_t u_key;
643 dlist_elt_t *head, *elt;
644 u32 elt_index, head_index;
648 snat_static_map_resolve_t *rp, *rp_match = 0;
649 nat44_lb_addr_port_t *local;
652 if (!sm->endpoint_dependent)
654 if (twice_nat || out2in_only)
655 return VNET_API_ERROR_FEATURE_DISABLED;
658 /* If the external address is a specific interface address */
659 if (sw_if_index != ~0)
661 ip4_address_t *first_int_addr;
663 for (i = 0; i < vec_len (sm->to_resolve); i++)
665 rp = sm->to_resolve + i;
666 if (rp->sw_if_index != sw_if_index ||
667 rp->l_addr.as_u32 != l_addr.as_u32 ||
668 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
673 if (rp->l_port != l_port || rp->e_port != e_port
674 || rp->proto != proto)
682 /* Might be already set... */
683 first_int_addr = ip4_interface_first_address
684 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
689 return VNET_API_ERROR_VALUE_EXIST;
691 snat_add_static_mapping_when_resolved
692 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
693 addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
695 /* DHCP resolution required? */
696 if (first_int_addr == 0)
702 e_addr.as_u32 = first_int_addr->as_u32;
703 /* Identity mapping? */
704 if (l_addr.as_u32 == 0)
705 l_addr.as_u32 = e_addr.as_u32;
711 return VNET_API_ERROR_NO_SUCH_ENTRY;
713 vec_del1 (sm->to_resolve, i);
717 e_addr.as_u32 = first_int_addr->as_u32;
718 /* Identity mapping? */
719 if (l_addr.as_u32 == 0)
720 l_addr.as_u32 = e_addr.as_u32;
728 m_key.port = addr_only ? 0 : e_port;
729 m_key.protocol = addr_only ? 0 : proto;
731 kv.key = m_key.as_u64;
732 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
735 m = pool_elt_at_index (sm->static_mappings, value.value);
741 if (is_identity_static_mapping (m))
744 vec_foreach (local, m->locals)
746 if (local->vrf_id == vrf_id)
747 return VNET_API_ERROR_VALUE_EXIST;
750 vec_add2 (m->locals, local, 1);
751 local->vrf_id = vrf_id;
753 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
754 FIB_SOURCE_PLUGIN_LOW);
755 m_key.addr = m->local_addr;
756 m_key.port = m->local_port;
757 m_key.protocol = m->proto;
758 m_key.fib_index = local->fib_index;
759 kv.key = m_key.as_u64;
760 kv.value = m - sm->static_mappings;
761 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
765 return VNET_API_ERROR_VALUE_EXIST;
768 if (twice_nat && addr_only)
769 return VNET_API_ERROR_UNSUPPORTED;
771 /* Convert VRF id to FIB index */
774 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
775 FIB_SOURCE_PLUGIN_LOW);
776 /* If not specified use inside VRF id from SNAT plugin startup config */
779 fib_index = sm->inside_fib_index;
780 vrf_id = sm->inside_vrf_id;
783 if (!(out2in_only || identity_nat))
786 m_key.port = addr_only ? 0 : l_port;
787 m_key.protocol = addr_only ? 0 : proto;
788 m_key.fib_index = fib_index;
789 kv.key = m_key.as_u64;
790 if (!clib_bihash_search_8_8
791 (&sm->static_mapping_by_local, &kv, &value))
792 return VNET_API_ERROR_VALUE_EXIST;
795 /* Find external address in allocated addresses and reserve port for
796 address and port pair mapping when dynamic translations enabled */
797 if (!(addr_only || sm->static_mapping_only || out2in_only))
799 for (i = 0; i < vec_len (sm->addresses); i++)
801 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
803 a = sm->addresses + i;
804 /* External port must be unused */
807 #define _(N, j, n, s) \
808 case SNAT_PROTOCOL_##N: \
809 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
810 return VNET_API_ERROR_INVALID_VALUE; \
811 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
814 a->busy_##n##_ports++; \
815 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
818 foreach_snat_protocol
821 nat_log_info ("unknown protocol");
822 return VNET_API_ERROR_INVALID_VALUE_2;
827 /* External address must be allocated */
828 if (!a && (l_addr.as_u32 != e_addr.as_u32))
830 if (sw_if_index != ~0)
832 for (i = 0; i < vec_len (sm->to_resolve); i++)
834 rp = sm->to_resolve + i;
837 if (rp->sw_if_index != sw_if_index &&
838 rp->l_addr.as_u32 != l_addr.as_u32 &&
839 rp->vrf_id != vrf_id && rp->l_port != l_port &&
840 rp->e_port != e_port && rp->proto != proto)
843 vec_del1 (sm->to_resolve, i);
847 return VNET_API_ERROR_NO_SUCH_ENTRY;
851 pool_get (sm->static_mappings, m);
852 clib_memset (m, 0, sizeof (*m));
853 m->tag = vec_dup (tag);
854 m->local_addr = l_addr;
855 m->external_addr = e_addr;
856 m->twice_nat = twice_nat;
858 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
860 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
863 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
864 vec_add2 (m->locals, local, 1);
865 local->vrf_id = vrf_id;
866 local->fib_index = fib_index;
871 m->fib_index = fib_index;
875 m->local_port = l_port;
876 m->external_port = e_port;
880 if (sm->num_workers > 1)
883 .src_address = m->local_addr,
885 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index));
886 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
889 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
891 m_key.addr = m->local_addr;
892 m_key.port = m->local_port;
893 m_key.protocol = m->proto;
894 m_key.fib_index = fib_index;
895 kv.key = m_key.as_u64;
896 kv.value = m - sm->static_mappings;
898 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
900 m_key.addr = m->external_addr;
901 m_key.port = m->external_port;
903 kv.key = m_key.as_u64;
904 kv.value = m - sm->static_mappings;
905 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
907 /* Delete dynamic sessions matching local address (+ local port) */
908 if (!(sm->static_mapping_only))
910 u_key.addr = m->local_addr;
911 u_key.fib_index = m->fib_index;
912 kv.key = u_key.as_u64;
913 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
915 user_index = value.value;
916 u = pool_elt_at_index (tsm->users, user_index);
919 head_index = u->sessions_per_user_list_head_index;
920 head = pool_elt_at_index (tsm->list_pool, head_index);
921 elt_index = head->next;
922 elt = pool_elt_at_index (tsm->list_pool, elt_index);
923 ses_index = elt->value;
924 while (ses_index != ~0)
926 s = pool_elt_at_index (tsm->sessions, ses_index);
927 elt = pool_elt_at_index (tsm->list_pool, elt->next);
928 ses_index = elt->value;
930 if (snat_is_session_static (s))
934 && (clib_net_to_host_u16 (s->in2out.port) !=
938 nat_free_session_data (sm, s,
939 tsm - sm->per_thread_data);
940 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
942 if (!addr_only && !sm->endpoint_dependent)
953 if (sw_if_index != ~0)
956 return VNET_API_ERROR_NO_SUCH_ENTRY;
962 vrf_id = sm->inside_vrf_id;
964 for (i = 0; i < vec_len (m->locals); i++)
966 if (m->locals[i].vrf_id == vrf_id)
973 return VNET_API_ERROR_NO_SUCH_ENTRY;
975 fib_index = m->locals[i].fib_index;
976 vec_del1 (m->locals, i);
979 fib_index = m->fib_index;
981 /* Free external address port */
982 if (!(addr_only || sm->static_mapping_only || out2in_only))
984 for (i = 0; i < vec_len (sm->addresses); i++)
986 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
988 a = sm->addresses + i;
991 #define _(N, j, n, s) \
992 case SNAT_PROTOCOL_##N: \
993 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
996 a->busy_##n##_ports--; \
997 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1000 foreach_snat_protocol
1003 nat_log_info ("unknown protocol");
1004 return VNET_API_ERROR_INVALID_VALUE_2;
1011 if (sm->num_workers > 1)
1012 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1014 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1016 m_key.addr = m->local_addr;
1017 m_key.port = m->local_port;
1018 m_key.protocol = m->proto;
1019 m_key.fib_index = fib_index;
1020 kv.key = m_key.as_u64;
1022 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1024 /* Delete session(s) for static mapping if exist */
1025 if (!(sm->static_mapping_only) ||
1026 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1028 u_key.addr = m->local_addr;
1029 u_key.fib_index = fib_index;
1030 kv.key = u_key.as_u64;
1031 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1033 user_index = value.value;
1034 u = pool_elt_at_index (tsm->users, user_index);
1035 if (u->nstaticsessions)
1037 head_index = u->sessions_per_user_list_head_index;
1038 head = pool_elt_at_index (tsm->list_pool, head_index);
1039 elt_index = head->next;
1040 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1041 ses_index = elt->value;
1042 while (ses_index != ~0)
1044 s = pool_elt_at_index (tsm->sessions, ses_index);
1045 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1046 ses_index = elt->value;
1050 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1051 (clib_net_to_host_u16 (s->out2in.port) !=
1056 if (is_lb_session (s))
1059 if (!snat_is_session_static (s))
1062 nat_free_session_data (sm, s,
1063 tsm - sm->per_thread_data);
1064 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1066 if (!addr_only && !sm->endpoint_dependent)
1073 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1074 if (vec_len (m->locals))
1077 m_key.addr = m->external_addr;
1078 m_key.port = m->external_port;
1079 m_key.fib_index = 0;
1080 kv.key = m_key.as_u64;
1081 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1084 vec_free (m->workers);
1085 /* Delete static mapping from pool */
1086 pool_put (sm->static_mappings, m);
1089 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1092 /* Add/delete external address to FIB */
1094 pool_foreach (interface, sm->interfaces,
1096 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1099 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1102 pool_foreach (interface, sm->output_feature_interfaces,
1104 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1107 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1116 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1117 snat_protocol_t proto,
1118 nat44_lb_addr_port_t * locals, u8 is_add,
1119 twice_nat_type_t twice_nat, u8 out2in_only,
1120 u8 * tag, u32 affinity)
1122 snat_main_t *sm = &snat_main;
1123 snat_static_mapping_t *m;
1124 snat_session_key_t m_key;
1125 clib_bihash_kv_8_8_t kv, value;
1126 snat_address_t *a = 0;
1128 nat44_lb_addr_port_t *local;
1129 u32 elt_index, head_index, ses_index;
1130 snat_main_per_thread_data_t *tsm;
1131 snat_user_key_t u_key;
1134 dlist_elt_t *head, *elt;
1137 if (!sm->endpoint_dependent)
1138 return VNET_API_ERROR_FEATURE_DISABLED;
1140 m_key.addr = e_addr;
1141 m_key.port = e_port;
1142 m_key.protocol = proto;
1143 m_key.fib_index = 0;
1144 kv.key = m_key.as_u64;
1145 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1148 m = pool_elt_at_index (sm->static_mappings, value.value);
1153 return VNET_API_ERROR_VALUE_EXIST;
1155 if (vec_len (locals) < 2)
1156 return VNET_API_ERROR_INVALID_VALUE;
1158 /* Find external address in allocated addresses and reserve port for
1159 address and port pair mapping when dynamic translations enabled */
1160 if (!(sm->static_mapping_only || out2in_only))
1162 for (i = 0; i < vec_len (sm->addresses); i++)
1164 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1166 a = sm->addresses + i;
1167 /* External port must be unused */
1170 #define _(N, j, n, s) \
1171 case SNAT_PROTOCOL_##N: \
1172 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1173 return VNET_API_ERROR_INVALID_VALUE; \
1174 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1175 if (e_port > 1024) \
1177 a->busy_##n##_ports++; \
1178 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1181 foreach_snat_protocol
1184 nat_log_info ("unknown protocol");
1185 return VNET_API_ERROR_INVALID_VALUE_2;
1190 /* External address must be allocated */
1192 return VNET_API_ERROR_NO_SUCH_ENTRY;
1195 pool_get (sm->static_mappings, m);
1196 clib_memset (m, 0, sizeof (*m));
1197 m->tag = vec_dup (tag);
1198 m->external_addr = e_addr;
1199 m->external_port = e_port;
1201 m->twice_nat = twice_nat;
1202 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1204 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1205 m->affinity = affinity;
1208 m->affinity_per_service_list_head_index =
1209 nat_affinity_get_per_service_list_head_index ();
1211 m->affinity_per_service_list_head_index = ~0;
1213 m_key.addr = m->external_addr;
1214 m_key.port = m->external_port;
1215 m_key.protocol = m->proto;
1216 m_key.fib_index = 0;
1217 kv.key = m_key.as_u64;
1218 kv.value = m - sm->static_mappings;
1219 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1221 nat_log_err ("static_mapping_by_external key add failed");
1222 return VNET_API_ERROR_UNSPECIFIED;
1225 m_key.fib_index = m->fib_index;
1226 for (i = 0; i < vec_len (locals); i++)
1228 locals[i].fib_index =
1229 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1231 FIB_SOURCE_PLUGIN_LOW);
1232 m_key.addr = locals[i].addr;
1233 m_key.fib_index = locals[i].fib_index;
1236 m_key.port = locals[i].port;
1237 kv.key = m_key.as_u64;
1238 kv.value = m - sm->static_mappings;
1239 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1241 locals[i].prefix = (i == 0) ? locals[i].probability :
1242 (locals[i - 1].prefix + locals[i].probability);
1243 vec_add1 (m->locals, locals[i]);
1244 if (sm->num_workers > 1)
1247 .src_address = locals[i].addr,
1250 clib_bitmap_set (bitmap,
1251 sm->worker_in2out_cb (&ip, m->fib_index), 1);
1255 /* Assign workers */
1256 if (sm->num_workers > 1)
1259 clib_bitmap_foreach (i, bitmap,
1261 vec_add1(m->workers, i);
1269 return VNET_API_ERROR_NO_SUCH_ENTRY;
1271 if (!is_lb_static_mapping (m))
1272 return VNET_API_ERROR_INVALID_VALUE;
1274 /* Free external address port */
1275 if (!(sm->static_mapping_only || out2in_only))
1277 for (i = 0; i < vec_len (sm->addresses); i++)
1279 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1281 a = sm->addresses + i;
1284 #define _(N, j, n, s) \
1285 case SNAT_PROTOCOL_##N: \
1286 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1287 if (e_port > 1024) \
1289 a->busy_##n##_ports--; \
1290 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1293 foreach_snat_protocol
1296 nat_log_info ("unknown protocol");
1297 return VNET_API_ERROR_INVALID_VALUE_2;
1304 m_key.addr = m->external_addr;
1305 m_key.port = m->external_port;
1306 m_key.protocol = m->proto;
1307 m_key.fib_index = 0;
1308 kv.key = m_key.as_u64;
1309 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1311 nat_log_err ("static_mapping_by_external key del failed");
1312 return VNET_API_ERROR_UNSPECIFIED;
1316 vec_foreach (local, m->locals)
1318 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1319 FIB_SOURCE_PLUGIN_LOW);
1320 m_key.addr = local->addr;
1323 m_key.port = local->port;
1324 m_key.fib_index = local->fib_index;
1325 kv.key = m_key.as_u64;
1326 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1328 nat_log_err ("static_mapping_by_local key del failed");
1329 return VNET_API_ERROR_UNSPECIFIED;
1333 if (sm->num_workers > 1)
1336 .src_address = local->addr,
1338 tsm = vec_elt_at_index (sm->per_thread_data,
1339 sm->worker_in2out_cb (&ip, m->fib_index));
1342 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1344 /* Delete sessions */
1345 u_key.addr = local->addr;
1346 u_key.fib_index = m->fib_index;
1347 kv.key = u_key.as_u64;
1348 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1350 u = pool_elt_at_index (tsm->users, value.value);
1351 if (u->nstaticsessions)
1353 head_index = u->sessions_per_user_list_head_index;
1354 head = pool_elt_at_index (tsm->list_pool, head_index);
1355 elt_index = head->next;
1356 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1357 ses_index = elt->value;
1358 while (ses_index != ~0)
1360 s = pool_elt_at_index (tsm->sessions, ses_index);
1361 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1362 ses_index = elt->value;
1364 if (!(is_lb_session (s)))
1367 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1368 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1371 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1372 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1379 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1380 vec_free (m->locals);
1382 vec_free (m->workers);
1384 pool_put (sm->static_mappings, m);
1391 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1394 snat_address_t *a = 0;
1395 snat_session_t *ses;
1396 u32 *ses_to_be_removed = 0, *ses_index;
1397 snat_main_per_thread_data_t *tsm;
1398 snat_static_mapping_t *m;
1399 snat_interface_t *interface;
1401 snat_address_t *addresses =
1402 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1404 /* Find SNAT address */
1405 for (i = 0; i < vec_len (addresses); i++)
1407 if (addresses[i].addr.as_u32 == addr.as_u32)
1414 return VNET_API_ERROR_NO_SUCH_ENTRY;
1419 pool_foreach (m, sm->static_mappings,
1421 if (m->external_addr.as_u32 == addr.as_u32)
1422 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1423 m->local_port, m->external_port,
1424 m->vrf_id, is_addr_only_static_mapping(m), ~0,
1425 m->proto, 0, m->twice_nat,
1426 is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1432 /* Check if address is used in some static mapping */
1433 if (is_snat_address_used_in_static_mapping (sm, addr))
1435 nat_log_notice ("address used in static mapping");
1436 return VNET_API_ERROR_UNSPECIFIED;
1440 if (a->fib_index != ~0)
1441 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1443 /* Delete sessions using address */
1444 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1447 vec_foreach (tsm, sm->per_thread_data)
1449 pool_foreach (ses, tsm->sessions, ({
1450 if (ses->out2in.addr.as_u32 == addr.as_u32)
1452 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1453 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1457 vec_foreach (ses_index, ses_to_be_removed)
1459 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1460 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1463 vec_free (ses_to_be_removed);
1468 #define _(N, i, n, s) \
1469 clib_bitmap_free (a->busy_##n##_port_bitmap); \
1470 vec_free (a->busy_##n##_ports_per_thread);
1471 foreach_snat_protocol
1475 vec_del1 (sm->twice_nat_addresses, i);
1479 vec_del1 (sm->addresses, i);
1481 /* Delete external address from FIB */
1483 pool_foreach (interface, sm->interfaces,
1485 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1488 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1491 pool_foreach (interface, sm->output_feature_interfaces,
1493 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1496 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1505 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1507 snat_main_t *sm = &snat_main;
1508 snat_interface_t *i;
1509 const char *feature_name, *del_feature_name;
1511 snat_static_mapping_t *m;
1513 nat_outside_fib_t *outside_fib;
1514 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1517 if (sm->out2in_dpo && !is_inside)
1518 return VNET_API_ERROR_UNSUPPORTED;
1521 pool_foreach (i, sm->output_feature_interfaces,
1523 if (i->sw_if_index == sw_if_index)
1524 return VNET_API_ERROR_VALUE_EXIST;
1528 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1529 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1532 if (sm->num_workers > 1 && !sm->deterministic)
1534 is_inside ? "nat44-in2out-worker-handoff" :
1535 "nat44-out2in-worker-handoff";
1536 else if (sm->deterministic)
1537 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1538 else if (sm->endpoint_dependent)
1539 feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in";
1541 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1544 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1545 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1548 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1549 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1555 vec_foreach (outside_fib, sm->outside_fibs)
1557 if (outside_fib->fib_index == fib_index)
1561 outside_fib->refcount--;
1562 if (!outside_fib->refcount)
1563 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1566 outside_fib->refcount++;
1573 vec_add2 (sm->outside_fibs, outside_fib, 1);
1574 outside_fib->refcount = 1;
1575 outside_fib->fib_index = fib_index;
1580 pool_foreach (i, sm->interfaces,
1582 if (i->sw_if_index == sw_if_index)
1586 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1589 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1591 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1593 if (sm->num_workers > 1 && !sm->deterministic)
1595 del_feature_name = "nat44-handoff-classify";
1596 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1597 "nat44-out2in-worker-handoff";
1599 else if (sm->deterministic)
1601 del_feature_name = "nat44-det-classify";
1602 feature_name = !is_inside ? "nat44-det-in2out" :
1605 else if (sm->endpoint_dependent)
1607 del_feature_name = "nat44-ed-classify";
1608 feature_name = !is_inside ? "nat44-ed-in2out" :
1613 del_feature_name = "nat44-classify";
1614 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1617 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1618 sw_if_index, 0, 0, 0);
1619 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1620 sw_if_index, 1, 0, 0);
1623 if (sm->endpoint_dependent)
1624 vnet_feature_enable_disable ("ip4-local",
1625 "nat44-ed-hairpinning",
1626 sw_if_index, 1, 0, 0);
1627 else if (!sm->deterministic)
1628 vnet_feature_enable_disable ("ip4-local",
1629 "nat44-hairpinning",
1630 sw_if_index, 1, 0, 0);
1635 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1636 sw_if_index, 0, 0, 0);
1637 pool_put (sm->interfaces, i);
1640 if (sm->endpoint_dependent)
1641 vnet_feature_enable_disable ("ip4-local",
1642 "nat44-ed-hairpinning",
1643 sw_if_index, 0, 0, 0);
1644 else if (!sm->deterministic)
1645 vnet_feature_enable_disable ("ip4-local",
1646 "nat44-hairpinning",
1647 sw_if_index, 0, 0, 0);
1653 if ((nat_interface_is_inside(i) && is_inside) ||
1654 (nat_interface_is_outside(i) && !is_inside))
1657 if (sm->num_workers > 1 && !sm->deterministic)
1659 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1660 "nat44-out2in-worker-handoff";
1661 feature_name = "nat44-handoff-classify";
1663 else if (sm->deterministic)
1665 del_feature_name = !is_inside ? "nat44-det-in2out" :
1667 feature_name = "nat44-det-classify";
1669 else if (sm->endpoint_dependent)
1671 del_feature_name = !is_inside ? "nat44-ed-in2out" :
1673 feature_name = "nat44-ed-classify";
1677 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1678 feature_name = "nat44-classify";
1681 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1682 sw_if_index, 0, 0, 0);
1683 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1684 sw_if_index, 1, 0, 0);
1687 if (sm->endpoint_dependent)
1688 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1689 sw_if_index, 0, 0, 0);
1690 else if (!sm->deterministic)
1691 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1692 sw_if_index, 0, 0, 0);
1703 return VNET_API_ERROR_NO_SUCH_ENTRY;
1705 pool_get (sm->interfaces, i);
1706 i->sw_if_index = sw_if_index;
1708 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
1711 if (is_inside && !sm->out2in_dpo)
1713 if (sm->endpoint_dependent)
1714 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1715 sw_if_index, 1, 0, 0);
1716 else if (!sm->deterministic)
1717 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1718 sw_if_index, 1, 0, 0);
1724 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1728 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1730 /* Add/delete external addresses to FIB */
1733 vec_foreach (ap, sm->addresses)
1734 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1736 pool_foreach (m, sm->static_mappings,
1738 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1741 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1744 pool_foreach (dm, sm->det_maps,
1746 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1754 snat_interface_add_del_output_feature (u32 sw_if_index,
1755 u8 is_inside, int is_del)
1757 snat_main_t *sm = &snat_main;
1758 snat_interface_t *i;
1760 snat_static_mapping_t *m;
1762 if (sm->deterministic ||
1763 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1764 return VNET_API_ERROR_UNSUPPORTED;
1767 pool_foreach (i, sm->interfaces,
1769 if (i->sw_if_index == sw_if_index)
1770 return VNET_API_ERROR_VALUE_EXIST;
1776 if (sm->endpoint_dependent)
1778 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
1779 sw_if_index, !is_del, 0, 0);
1780 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
1781 sw_if_index, !is_del, 0, 0);
1785 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1786 sw_if_index, !is_del, 0, 0);
1787 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1788 sw_if_index, !is_del, 0, 0);
1793 if (sm->num_workers > 1)
1795 vnet_feature_enable_disable ("ip4-unicast",
1796 "nat44-out2in-worker-handoff",
1797 sw_if_index, !is_del, 0, 0);
1798 vnet_feature_enable_disable ("ip4-output",
1799 "nat44-in2out-output-worker-handoff",
1800 sw_if_index, !is_del, 0, 0);
1804 if (sm->endpoint_dependent)
1806 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in",
1807 sw_if_index, !is_del, 0, 0);
1808 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
1809 sw_if_index, !is_del, 0, 0);
1813 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
1814 sw_if_index, !is_del, 0, 0);
1815 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1816 sw_if_index, !is_del, 0, 0);
1821 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1822 sm->fq_in2out_output_index =
1823 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1825 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1826 sm->fq_out2in_index =
1827 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1830 pool_foreach (i, sm->output_feature_interfaces,
1832 if (i->sw_if_index == sw_if_index)
1835 pool_put (sm->output_feature_interfaces, i);
1837 return VNET_API_ERROR_VALUE_EXIST;
1845 return VNET_API_ERROR_NO_SUCH_ENTRY;
1847 pool_get (sm->output_feature_interfaces, i);
1848 i->sw_if_index = sw_if_index;
1851 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1853 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1855 /* Add/delete external addresses to FIB */
1861 vec_foreach (ap, sm->addresses)
1862 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1864 pool_foreach (m, sm->static_mappings,
1866 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1869 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1877 snat_set_workers (uword * bitmap)
1879 snat_main_t *sm = &snat_main;
1882 if (sm->num_workers < 2)
1883 return VNET_API_ERROR_FEATURE_DISABLED;
1885 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1886 return VNET_API_ERROR_INVALID_WORKER;
1888 vec_free (sm->workers);
1890 clib_bitmap_foreach (i, bitmap,
1892 vec_add1(sm->workers, i);
1893 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1898 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1899 sm->num_snat_thread = _vec_len (sm->workers);
1906 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1909 ip4_address_t * address,
1911 u32 if_address_index, u32 is_delete);
1914 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
1917 ip4_address_t * address,
1919 u32 if_address_index, u32 is_delete);
1922 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1925 snat_session_key_t * k,
1926 u16 port_per_thread, u32 snat_thread_index);
1928 static clib_error_t *
1929 snat_init (vlib_main_t * vm)
1931 snat_main_t *sm = &snat_main;
1932 clib_error_t *error = 0;
1933 ip4_main_t *im = &ip4_main;
1934 ip_lookup_main_t *lm = &im->lookup_main;
1936 vlib_thread_registration_t *tr;
1937 vlib_thread_main_t *tm = vlib_get_thread_main ();
1940 ip4_add_del_interface_address_callback_t cb4;
1941 vlib_node_t *error_drop_node;
1944 sm->vnet_main = vnet_get_main ();
1946 sm->ip4_lookup_main = lm;
1947 sm->api_main = &api_main;
1948 sm->first_worker_index = 0;
1949 sm->num_workers = 0;
1950 sm->num_snat_thread = 1;
1952 sm->port_per_thread = 0xffff - 1024;
1953 sm->fq_in2out_index = ~0;
1954 sm->fq_out2in_index = ~0;
1955 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1956 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1957 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1958 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1959 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1960 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1961 sm->forwarding_enabled = 0;
1962 sm->log_class = vlib_log_register_class ("nat", 0);
1963 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
1964 sm->error_node_index = error_drop_node->index;
1965 sm->mss_clamping = 0;
1967 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1970 tr = (vlib_thread_registration_t *) p[0];
1973 sm->num_workers = tr->count;
1974 sm->first_worker_index = tr->first_index;
1978 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1980 /* Use all available workers by default */
1981 if (sm->num_workers > 1)
1983 for (i = 0; i < sm->num_workers; i++)
1984 bitmap = clib_bitmap_set (bitmap, i, 1);
1985 snat_set_workers (bitmap);
1986 clib_bitmap_free (bitmap);
1990 sm->per_thread_data[0].snat_thread_index = 0;
1993 error = snat_api_init (vm, sm);
1997 /* Set up the interface address add/del callback */
1998 cb4.function = snat_ip4_add_del_interface_address_cb;
1999 cb4.function_opaque = 0;
2001 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2003 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2004 cb4.function_opaque = 0;
2006 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2008 nat_dpo_module_init ();
2010 /* Init IPFIX logging */
2011 snat_ipfix_logging_init (vm);
2014 error = nat64_init (vm);
2022 /* Init virtual fragmenentation reassembly */
2023 return nat_reass_init (vm);
2026 VLIB_INIT_FUNCTION (snat_init);
2029 snat_free_outside_address_and_port (snat_address_t * addresses,
2030 u32 thread_index, snat_session_key_t * k)
2034 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2036 for (address_index = 0; address_index < vec_len (addresses);
2039 if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
2043 ASSERT (address_index < vec_len (addresses));
2045 a = addresses + address_index;
2047 switch (k->protocol)
2049 #define _(N, i, n, s) \
2050 case SNAT_PROTOCOL_##N: \
2051 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
2052 port_host_byte_order) == 1); \
2053 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
2054 port_host_byte_order, 0); \
2055 a->busy_##n##_ports--; \
2056 a->busy_##n##_ports_per_thread[thread_index]--; \
2058 foreach_snat_protocol
2061 nat_log_info ("unknown protocol");
2067 snat_static_mapping_match (snat_main_t * sm,
2068 snat_session_key_t match,
2069 snat_session_key_t * mapping,
2072 twice_nat_type_t * twice_nat,
2073 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2074 u8 * is_identity_nat)
2076 clib_bihash_kv_8_8_t kv, value;
2077 snat_static_mapping_t *m;
2078 snat_session_key_t m_key;
2079 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2080 u32 rand, lo = 0, hi, mid;
2083 m_key.fib_index = match.fib_index;
2086 mapping_hash = &sm->static_mapping_by_external;
2087 m_key.fib_index = 0;
2090 m_key.addr = match.addr;
2091 m_key.port = clib_net_to_host_u16 (match.port);
2092 m_key.protocol = match.protocol;
2094 kv.key = m_key.as_u64;
2096 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2098 /* Try address only mapping */
2101 kv.key = m_key.as_u64;
2102 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2106 m = pool_elt_at_index (sm->static_mappings, value.value);
2110 if (is_lb_static_mapping (m))
2112 if (PREDICT_FALSE (lb != 0))
2113 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2116 if (nat_affinity_find_and_lock (ext_host_addr[0], match.addr,
2117 match.protocol, match.port,
2121 mapping->addr = m->locals[backend_index].addr;
2123 clib_host_to_net_u16 (m->locals[backend_index].port);
2124 mapping->fib_index = m->locals[backend_index].fib_index;
2128 hi = vec_len (m->locals) - 1;
2129 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
2132 mid = ((hi - lo) >> 1) + lo;
2133 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
2135 if (!(m->locals[lo].prefix >= rand))
2137 if (PREDICT_FALSE (sm->num_workers > 1))
2140 .src_address = m->locals[lo].addr,
2142 if (sm->worker_in2out_cb (&ip, m->fib_index) !=
2143 vlib_get_thread_index ())
2146 mapping->addr = m->locals[lo].addr;
2147 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
2148 mapping->fib_index = m->locals[lo].fib_index;
2151 if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr,
2152 match.protocol, match.port,
2154 m->affinity_per_service_list_head_index))
2155 nat_log_info ("create affinity record failed");
2160 if (PREDICT_FALSE (lb != 0))
2162 mapping->fib_index = m->fib_index;
2163 mapping->addr = m->local_addr;
2164 /* Address only mapping doesn't change port */
2165 mapping->port = is_addr_only_static_mapping (m) ? match.port
2166 : clib_host_to_net_u16 (m->local_port);
2168 mapping->protocol = m->proto;
2172 mapping->addr = m->external_addr;
2173 /* Address only mapping doesn't change port */
2174 mapping->port = is_addr_only_static_mapping (m) ? match.port
2175 : clib_host_to_net_u16 (m->external_port);
2176 mapping->fib_index = sm->outside_fib_index;
2180 if (PREDICT_FALSE (is_addr_only != 0))
2181 *is_addr_only = is_addr_only_static_mapping (m);
2183 if (PREDICT_FALSE (twice_nat != 0))
2184 *twice_nat = m->twice_nat;
2186 if (PREDICT_FALSE (is_identity_nat != 0))
2187 *is_identity_nat = is_identity_static_mapping (m);
2192 static_always_inline u16
2193 snat_random_port (u16 min, u16 max)
2195 snat_main_t *sm = &snat_main;
2196 return min + random_u32 (&sm->random_seed) /
2197 (random_u32_max () / (max - min + 1) + 1);
2201 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2204 snat_session_key_t * k,
2205 u16 port_per_thread,
2206 u32 snat_thread_index)
2208 snat_main_t *sm = &snat_main;
2210 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, k,
2211 port_per_thread, snat_thread_index);
2215 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2218 snat_session_key_t * k,
2219 u16 port_per_thread, u32 snat_thread_index)
2222 snat_address_t *a, *ga = 0;
2225 for (i = 0; i < vec_len (addresses); i++)
2228 switch (k->protocol)
2230 #define _(N, j, n, s) \
2231 case SNAT_PROTOCOL_##N: \
2232 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2234 if (a->fib_index == fib_index) \
2238 portnum = (port_per_thread * \
2239 snat_thread_index) + \
2240 snat_random_port(1, port_per_thread) + 1024; \
2241 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2243 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2244 a->busy_##n##_ports_per_thread[thread_index]++; \
2245 a->busy_##n##_ports++; \
2246 k->addr = a->addr; \
2247 k->port = clib_host_to_net_u16(portnum); \
2251 else if (a->fib_index == ~0) \
2257 foreach_snat_protocol
2260 nat_log_info ("unknown protocol");
2269 switch (k->protocol)
2271 #define _(N, j, n, s) \
2272 case SNAT_PROTOCOL_##N: \
2275 portnum = (port_per_thread * \
2276 snat_thread_index) + \
2277 snat_random_port(1, port_per_thread) + 1024; \
2278 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2280 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2281 a->busy_##n##_ports_per_thread[thread_index]++; \
2282 a->busy_##n##_ports++; \
2283 k->addr = a->addr; \
2284 k->port = clib_host_to_net_u16(portnum); \
2288 foreach_snat_protocol
2291 nat_log_info ("unknown protocol");
2296 /* Totally out of translations to use... */
2297 snat_ipfix_logging_addresses_exhausted (0);
2302 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2305 snat_session_key_t * k,
2306 u16 port_per_thread, u32 snat_thread_index)
2308 snat_main_t *sm = &snat_main;
2309 snat_address_t *a = addresses;
2310 u16 m, ports, portnum, A, j;
2311 m = 16 - (sm->psid_offset + sm->psid_length);
2312 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2314 if (!vec_len (addresses))
2317 switch (k->protocol)
2319 #define _(N, i, n, s) \
2320 case SNAT_PROTOCOL_##N: \
2321 if (a->busy_##n##_ports < ports) \
2325 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2326 j = snat_random_port(0, pow2_mask(m)); \
2327 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2328 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2330 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2331 a->busy_##n##_ports++; \
2332 k->addr = a->addr; \
2333 k->port = clib_host_to_net_u16 (portnum); \
2338 foreach_snat_protocol
2341 nat_log_info ("unknown protocol");
2346 /* Totally out of translations to use... */
2347 snat_ipfix_logging_addresses_exhausted (0);
2352 nat_alloc_addr_and_port_range (snat_address_t * addresses,
2355 snat_session_key_t * k,
2356 u16 port_per_thread, u32 snat_thread_index)
2358 snat_main_t *sm = &snat_main;
2359 snat_address_t *a = addresses;
2362 ports = sm->end_port - sm->start_port + 1;
2364 if (!vec_len (addresses))
2367 switch (k->protocol)
2369 #define _(N, i, n, s) \
2370 case SNAT_PROTOCOL_##N: \
2371 if (a->busy_##n##_ports < ports) \
2375 portnum = snat_random_port(sm->start_port, sm->end_port); \
2376 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2378 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2379 a->busy_##n##_ports++; \
2380 k->addr = a->addr; \
2381 k->port = clib_host_to_net_u16 (portnum); \
2386 foreach_snat_protocol
2389 nat_log_info ("unknown protocol");
2394 /* Totally out of translations to use... */
2395 snat_ipfix_logging_addresses_exhausted (0);
2400 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2402 dpo_id_t dpo_v4 = DPO_INVALID;
2403 fib_prefix_t pfx = {
2404 .fp_proto = FIB_PROTOCOL_IP4,
2406 .fp_addr.ip4.as_u32 = addr.as_u32,
2411 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2412 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2413 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2414 dpo_reset (&dpo_v4);
2418 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2423 format_session_kvp (u8 * s, va_list * args)
2425 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2426 snat_session_key_t k;
2430 s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
2436 format_static_mapping_kvp (u8 * s, va_list * args)
2438 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2439 snat_session_key_t k;
2443 s = format (s, "%U static-mapping-index %llu",
2444 format_static_mapping_key, &k, v->value);
2450 format_user_kvp (u8 * s, va_list * args)
2452 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2457 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2458 k.fib_index, v->value);
2464 format_ed_session_kvp (u8 * s, va_list * args)
2466 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
2469 k.as_u64[0] = v->key[0];
2470 k.as_u64[1] = v->key[1];
2473 format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2474 format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
2475 format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
2476 format_ip_protocol, k.proto, k.fib_index, v->value);
2482 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2484 snat_main_t *sm = &snat_main;
2485 u32 next_worker_index = 0;
2488 next_worker_index = sm->first_worker_index;
2489 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2490 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
2492 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2493 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2495 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2497 return next_worker_index;
2501 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2503 snat_main_t *sm = &snat_main;
2506 snat_session_key_t m_key;
2507 clib_bihash_kv_8_8_t kv, value;
2508 snat_static_mapping_t *m;
2510 u32 next_worker_index = 0;
2512 /* first try static mappings without port */
2513 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2515 m_key.addr = ip0->dst_address;
2518 m_key.fib_index = rx_fib_index0;
2519 kv.key = m_key.as_u64;
2520 if (!clib_bihash_search_8_8
2521 (&sm->static_mapping_by_external, &kv, &value))
2523 m = pool_elt_at_index (sm->static_mappings, value.value);
2524 return m->workers[0];
2528 proto = ip_proto_to_snat_proto (ip0->protocol);
2529 udp = ip4_next_header (ip0);
2530 port = udp->dst_port;
2532 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2534 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2535 return vlib_get_thread_index ();
2537 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2539 nat_reass_ip4_t *reass;
2541 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2542 ip0->fragment_id, ip0->protocol);
2544 if (reass && (reass->thread_index != (u32) ~ 0))
2545 return reass->thread_index;
2547 return vlib_get_thread_index ();
2551 /* unknown protocol */
2552 if (PREDICT_FALSE (proto == ~0))
2554 /* use current thread */
2555 return vlib_get_thread_index ();
2558 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2560 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2561 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2562 if (!icmp_is_error_message (icmp))
2563 port = echo->identifier;
2566 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2567 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2568 void *l4_header = ip4_next_header (inner_ip);
2571 case SNAT_PROTOCOL_ICMP:
2572 icmp = (icmp46_header_t *) l4_header;
2573 echo = (icmp_echo_header_t *) (icmp + 1);
2574 port = echo->identifier;
2576 case SNAT_PROTOCOL_UDP:
2577 case SNAT_PROTOCOL_TCP:
2578 port = ((tcp_udp_header_t *) l4_header)->src_port;
2581 return vlib_get_thread_index ();
2586 /* try static mappings with port */
2587 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2589 m_key.addr = ip0->dst_address;
2590 m_key.port = clib_net_to_host_u16 (port);
2591 m_key.protocol = proto;
2592 m_key.fib_index = rx_fib_index0;
2593 kv.key = m_key.as_u64;
2594 if (!clib_bihash_search_8_8
2595 (&sm->static_mapping_by_external, &kv, &value))
2597 m = pool_elt_at_index (sm->static_mappings, value.value);
2598 return m->workers[0];
2602 /* worker by outside port */
2603 next_worker_index = sm->first_worker_index;
2604 next_worker_index +=
2605 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2606 return next_worker_index;
2610 nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index)
2612 snat_main_t *sm = &snat_main;
2613 clib_bihash_kv_8_8_t kv, value;
2614 u32 proto, next_worker_index = 0;
2617 snat_static_mapping_t *m;
2620 /* first try static mappings without port */
2621 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2623 make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0);
2624 if (!clib_bihash_search_8_8
2625 (&sm->static_mapping_by_external, &kv, &value))
2627 m = pool_elt_at_index (sm->static_mappings, value.value);
2628 return m->workers[0];
2632 proto = ip_proto_to_snat_proto (ip->protocol);
2634 /* unknown protocol */
2635 if (PREDICT_FALSE (proto == ~0))
2637 /* use current thread */
2638 return vlib_get_thread_index ();
2641 udp = ip4_next_header (ip);
2642 port = udp->dst_port;
2644 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2646 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2647 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2648 if (!icmp_is_error_message (icmp))
2649 port = echo->identifier;
2652 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2653 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2654 void *l4_header = ip4_next_header (inner_ip);
2657 case SNAT_PROTOCOL_ICMP:
2658 icmp = (icmp46_header_t *) l4_header;
2659 echo = (icmp_echo_header_t *) (icmp + 1);
2660 port = echo->identifier;
2662 case SNAT_PROTOCOL_UDP:
2663 case SNAT_PROTOCOL_TCP:
2664 port = ((tcp_udp_header_t *) l4_header)->src_port;
2667 return vlib_get_thread_index ();
2672 /* try static mappings with port */
2673 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2675 make_sm_kv (&kv, &ip->dst_address, proto, rx_fib_index,
2676 clib_net_to_host_u16 (port));
2677 if (!clib_bihash_search_8_8
2678 (&sm->static_mapping_by_external, &kv, &value))
2680 m = pool_elt_at_index (sm->static_mappings, value.value);
2681 if (!is_lb_static_mapping (m))
2682 return m->workers[0];
2684 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2685 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2687 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2688 return m->workers[hash & (_vec_len (m->workers) - 1)];
2690 return m->workers[hash % _vec_len (m->workers)];
2694 /* worker by outside port */
2695 next_worker_index = sm->first_worker_index;
2696 next_worker_index +=
2697 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2699 return next_worker_index;
2702 static clib_error_t *
2703 snat_config (vlib_main_t * vm, unformat_input_t * input)
2705 snat_main_t *sm = &snat_main;
2706 nat66_main_t *nm = &nat66_main;
2707 u32 translation_buckets = 1024;
2708 u32 translation_memory_size = 128 << 20;
2709 u32 user_buckets = 128;
2710 u32 user_memory_size = 64 << 20;
2711 u32 max_translations_per_user = 100;
2712 u32 outside_vrf_id = 0;
2713 u32 outside_ip6_vrf_id = 0;
2714 u32 inside_vrf_id = 0;
2715 u32 static_mapping_buckets = 1024;
2716 u32 static_mapping_memory_size = 64 << 20;
2717 u32 nat64_bib_buckets = 1024;
2718 u32 nat64_bib_memory_size = 128 << 20;
2719 u32 nat64_st_buckets = 2048;
2720 u32 nat64_st_memory_size = 256 << 20;
2721 u8 static_mapping_only = 0;
2722 u8 static_mapping_connection_tracking = 0;
2723 snat_main_per_thread_data_t *tsm;
2724 dslite_main_t *dm = &dslite_main;
2726 sm->deterministic = 0;
2728 sm->endpoint_dependent = 0;
2730 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2733 (input, "translation hash buckets %d", &translation_buckets))
2735 else if (unformat (input, "translation hash memory %d",
2736 &translation_memory_size));
2737 else if (unformat (input, "user hash buckets %d", &user_buckets))
2739 else if (unformat (input, "user hash memory %d", &user_memory_size))
2741 else if (unformat (input, "max translations per user %d",
2742 &max_translations_per_user))
2744 else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
2746 else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
2748 else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
2750 else if (unformat (input, "static mapping only"))
2752 static_mapping_only = 1;
2753 if (unformat (input, "connection tracking"))
2754 static_mapping_connection_tracking = 1;
2756 else if (unformat (input, "deterministic"))
2757 sm->deterministic = 1;
2758 else if (unformat (input, "nat64 bib hash buckets %d",
2759 &nat64_bib_buckets))
2761 else if (unformat (input, "nat64 bib hash memory %d",
2762 &nat64_bib_memory_size))
2765 if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2767 else if (unformat (input, "nat64 st hash memory %d",
2768 &nat64_st_memory_size))
2770 else if (unformat (input, "out2in dpo"))
2772 else if (unformat (input, "dslite ce"))
2773 dslite_set_ce (dm, 1);
2774 else if (unformat (input, "endpoint-dependent"))
2775 sm->endpoint_dependent = 1;
2777 return clib_error_return (0, "unknown input '%U'",
2778 format_unformat_error, input);
2781 if (sm->deterministic && sm->endpoint_dependent)
2782 return clib_error_return (0,
2783 "deterministic and endpoint-dependent modes are mutually exclusive");
2785 if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
2786 return clib_error_return (0,
2787 "static mapping only mode available only for simple nat");
2789 if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
2790 return clib_error_return (0,
2791 "out2in dpo mode available only for simple nat");
2793 /* for show commands, etc. */
2794 sm->translation_buckets = translation_buckets;
2795 sm->translation_memory_size = translation_memory_size;
2796 /* do not exceed load factor 10 */
2797 sm->max_translations = 10 * translation_buckets;
2798 sm->user_buckets = user_buckets;
2799 sm->user_memory_size = user_memory_size;
2800 sm->max_translations_per_user = max_translations_per_user;
2801 sm->outside_vrf_id = outside_vrf_id;
2802 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2804 FIB_SOURCE_PLUGIN_HI);
2805 nm->outside_vrf_id = outside_ip6_vrf_id;
2806 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2808 FIB_SOURCE_PLUGIN_HI);
2809 sm->inside_vrf_id = inside_vrf_id;
2810 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2812 FIB_SOURCE_PLUGIN_HI);
2813 sm->static_mapping_only = static_mapping_only;
2814 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2816 nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2817 nat64_st_memory_size);
2819 if (sm->deterministic)
2821 sm->in2out_node_index = snat_det_in2out_node.index;
2822 sm->in2out_output_node_index = ~0;
2823 sm->out2in_node_index = snat_det_out2in_node.index;
2824 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2825 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2829 if (sm->endpoint_dependent)
2831 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2832 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2833 sm->in2out_node_index = nat44_ed_in2out_node.index;
2834 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
2835 sm->out2in_node_index = nat44_ed_out2in_node.index;
2836 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
2837 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
2838 nat_affinity_init (vm);
2842 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2843 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2844 sm->in2out_node_index = snat_in2out_node.index;
2845 sm->in2out_output_node_index = snat_in2out_output_node.index;
2846 sm->out2in_node_index = snat_out2in_node.index;
2847 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2848 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2850 if (!static_mapping_only ||
2851 (static_mapping_only && static_mapping_connection_tracking))
2854 vec_foreach (tsm, sm->per_thread_data)
2856 if (sm->endpoint_dependent)
2858 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
2859 translation_buckets,
2860 translation_memory_size);
2861 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
2862 format_ed_session_kvp);
2864 clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
2865 translation_buckets,
2866 translation_memory_size);
2867 clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
2868 format_ed_session_kvp);
2872 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
2873 translation_buckets,
2874 translation_memory_size);
2875 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
2876 format_session_kvp);
2878 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
2879 translation_buckets,
2880 translation_memory_size);
2881 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
2882 format_session_kvp);
2885 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2887 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
2895 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2896 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2898 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2899 "static_mapping_by_local", static_mapping_buckets,
2900 static_mapping_memory_size);
2901 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2902 format_static_mapping_kvp);
2904 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2905 "static_mapping_by_external",
2906 static_mapping_buckets,
2907 static_mapping_memory_size);
2908 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2909 format_static_mapping_kvp);
2915 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2918 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2921 ip4_address_t * address,
2923 u32 if_address_index, u32 is_delete)
2925 snat_main_t *sm = &snat_main;
2926 snat_static_map_resolve_t *rp;
2927 snat_static_mapping_t *m;
2928 snat_session_key_t m_key;
2929 clib_bihash_kv_8_8_t kv, value;
2931 ip4_address_t l_addr;
2933 for (i = 0; i < vec_len (sm->to_resolve); i++)
2935 rp = sm->to_resolve + i;
2936 if (rp->addr_only == 0)
2938 if (rp->sw_if_index == sw_if_index)
2945 m_key.addr.as_u32 = address->as_u32;
2946 m_key.port = rp->addr_only ? 0 : rp->e_port;
2947 m_key.protocol = rp->addr_only ? 0 : rp->proto;
2948 m_key.fib_index = sm->outside_fib_index;
2949 kv.key = m_key.as_u64;
2950 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2953 m = pool_elt_at_index (sm->static_mappings, value.value);
2957 /* Don't trip over lease renewal, static config */
2967 /* Indetity mapping? */
2968 if (rp->l_addr.as_u32 == 0)
2969 l_addr.as_u32 = address[0].as_u32;
2971 l_addr.as_u32 = rp->l_addr.as_u32;
2972 /* Add the static mapping */
2973 rv = snat_add_static_mapping (l_addr,
2978 rp->addr_only, ~0 /* sw_if_index */ ,
2979 rp->proto, !is_delete, rp->twice_nat,
2980 rp->out2in_only, rp->tag, rp->identity_nat);
2982 nat_log_notice ("snat_add_static_mapping returned %d", rv);
2986 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2989 ip4_address_t * address,
2991 u32 if_address_index, u32 is_delete)
2993 snat_main_t *sm = &snat_main;
2994 snat_static_map_resolve_t *rp;
2995 ip4_address_t l_addr;
2999 snat_address_t *addresses = sm->addresses;
3001 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
3003 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3007 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
3010 addresses = sm->twice_nat_addresses;
3011 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3020 /* Don't trip over lease renewal, static config */
3021 for (j = 0; j < vec_len (addresses); j++)
3022 if (addresses[j].addr.as_u32 == address->as_u32)
3025 (void) snat_add_address (sm, address, ~0, twice_nat);
3026 /* Scan static map resolution vector */
3027 for (j = 0; j < vec_len (sm->to_resolve); j++)
3029 rp = sm->to_resolve + j;
3032 /* On this interface? */
3033 if (rp->sw_if_index == sw_if_index)
3035 /* Indetity mapping? */
3036 if (rp->l_addr.as_u32 == 0)
3037 l_addr.as_u32 = address[0].as_u32;
3039 l_addr.as_u32 = rp->l_addr.as_u32;
3040 /* Add the static mapping */
3041 rv = snat_add_static_mapping (l_addr,
3047 ~0 /* sw_if_index */ ,
3049 rp->is_add, rp->twice_nat,
3050 rp->out2in_only, rp->tag,
3053 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3060 (void) snat_del_address (sm, address[0], 1, twice_nat);
3067 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
3070 ip4_main_t *ip4_main = sm->ip4_main;
3071 ip4_address_t *first_int_addr;
3072 snat_static_map_resolve_t *rp;
3073 u32 *indices_to_delete = 0;
3075 u32 *auto_add_sw_if_indices =
3077 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3079 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
3082 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3084 if (auto_add_sw_if_indices[i] == sw_if_index)
3088 /* if have address remove it */
3090 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3093 for (j = 0; j < vec_len (sm->to_resolve); j++)
3095 rp = sm->to_resolve + j;
3096 if (rp->sw_if_index == sw_if_index)
3097 vec_add1 (indices_to_delete, j);
3099 if (vec_len (indices_to_delete))
3101 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3102 vec_del1 (sm->to_resolve, j);
3103 vec_free (indices_to_delete);
3107 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3109 vec_del1 (sm->auto_add_sw_if_indices, i);
3112 return VNET_API_ERROR_VALUE_EXIST;
3119 return VNET_API_ERROR_NO_SUCH_ENTRY;
3121 /* add to the auto-address list */
3123 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3125 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3127 /* If the address is already bound - or static - add it now */
3129 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3135 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3136 snat_protocol_t proto, u32 vrf_id, int is_in)
3138 snat_main_per_thread_data_t *tsm;
3139 clib_bihash_kv_8_8_t kv, value;
3141 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3142 snat_session_key_t key;
3144 clib_bihash_8_8_t *t;
3146 if (sm->endpoint_dependent)
3147 return VNET_API_ERROR_UNSUPPORTED;
3149 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3150 if (sm->num_workers > 1)
3152 vec_elt_at_index (sm->per_thread_data,
3153 sm->worker_in2out_cb (&ip, fib_index));
3155 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3157 key.addr.as_u32 = addr->as_u32;
3158 key.port = clib_host_to_net_u16 (port);
3159 key.protocol = proto;
3160 key.fib_index = fib_index;
3161 kv.key = key.as_u64;
3162 t = is_in ? &tsm->in2out : &tsm->out2in;
3163 if (!clib_bihash_search_8_8 (t, &kv, &value))
3165 if (pool_is_free_index (tsm->sessions, value.value))
3166 return VNET_API_ERROR_UNSPECIFIED;
3168 s = pool_elt_at_index (tsm->sessions, value.value);
3169 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3170 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3174 return VNET_API_ERROR_NO_SUCH_ENTRY;
3178 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3179 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3180 u32 vrf_id, int is_in)
3183 clib_bihash_16_8_t *t;
3184 nat_ed_ses_key_t key;
3185 clib_bihash_kv_16_8_t kv, value;
3186 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3188 snat_main_per_thread_data_t *tsm;
3190 if (!sm->endpoint_dependent)
3191 return VNET_API_ERROR_FEATURE_DISABLED;
3193 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3194 if (sm->num_workers > 1)
3196 vec_elt_at_index (sm->per_thread_data,
3197 sm->worker_in2out_cb (&ip, fib_index));
3199 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3201 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
3202 key.l_addr.as_u32 = addr->as_u32;
3203 key.r_addr.as_u32 = eh_addr->as_u32;
3204 key.l_port = clib_host_to_net_u16 (port);
3205 key.r_port = clib_host_to_net_u16 (eh_port);
3207 key.fib_index = fib_index;
3208 kv.key[0] = key.as_u64[0];
3209 kv.key[1] = key.as_u64[1];
3210 if (clib_bihash_search_16_8 (t, &kv, &value))
3211 return VNET_API_ERROR_NO_SUCH_ENTRY;
3213 if (pool_is_free_index (tsm->sessions, value.value))
3214 return VNET_API_ERROR_UNSPECIFIED;
3215 s = pool_elt_at_index (tsm->sessions, value.value);
3216 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3217 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3222 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3224 snat_main_t *sm = &snat_main;
3226 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
3227 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3229 sm->psid_offset = psid_offset;
3230 sm->psid_length = psid_length;
3234 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
3236 snat_main_t *sm = &snat_main;
3238 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
3239 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
3240 sm->start_port = start_port;
3241 sm->end_port = end_port;
3245 nat_set_alloc_addr_and_port_default (void)
3247 snat_main_t *sm = &snat_main;
3249 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
3250 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
3254 * fd.io coding-style-patch-verification: ON
3257 * eval: (c-set-style "gnu")