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 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 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 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 (m->external_addr.as_u32 == addr.as_u32)
570 increment_v4_address (ip4_address_t * a)
574 v = clib_net_to_host_u32 (a->as_u32) + 1;
575 a->as_u32 = clib_host_to_net_u32 (v);
579 snat_add_static_mapping_when_resolved (snat_main_t * sm,
580 ip4_address_t l_addr,
585 snat_protocol_t proto,
586 int addr_only, int is_add, u8 * tag,
587 int twice_nat, int out2in_only,
590 snat_static_map_resolve_t *rp;
592 vec_add2 (sm->to_resolve, rp, 1);
593 rp->l_addr.as_u32 = l_addr.as_u32;
595 rp->sw_if_index = sw_if_index;
599 rp->addr_only = addr_only;
601 rp->twice_nat = twice_nat;
602 rp->out2in_only = out2in_only;
603 rp->identity_nat = identity_nat;
604 rp->tag = vec_dup (tag);
608 get_thread_idx_by_port (u16 e_port)
610 snat_main_t *sm = &snat_main;
611 u32 thread_idx = sm->num_workers;
612 if (sm->num_workers > 1)
615 sm->first_worker_index +
616 sm->workers[(e_port - 1024) / sm->port_per_thread];
622 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
623 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
624 u32 sw_if_index, snat_protocol_t proto, int is_add,
625 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
628 snat_main_t *sm = &snat_main;
629 snat_static_mapping_t *m;
630 snat_session_key_t m_key;
631 clib_bihash_kv_8_8_t kv, value;
632 snat_address_t *a = 0;
634 snat_interface_t *interface;
636 snat_main_per_thread_data_t *tsm;
637 snat_user_key_t u_key;
639 dlist_elt_t *head, *elt;
640 u32 elt_index, head_index;
644 snat_static_map_resolve_t *rp, *rp_match = 0;
645 nat44_lb_addr_port_t *local;
648 if (!sm->endpoint_dependent)
650 if (twice_nat || out2in_only)
651 return VNET_API_ERROR_FEATURE_DISABLED;
654 /* If the external address is a specific interface address */
655 if (sw_if_index != ~0)
657 ip4_address_t *first_int_addr;
659 for (i = 0; i < vec_len (sm->to_resolve); i++)
661 rp = sm->to_resolve + i;
662 if (rp->sw_if_index != sw_if_index ||
663 rp->l_addr.as_u32 != l_addr.as_u32 ||
664 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
669 if (rp->l_port != l_port || rp->e_port != e_port
670 || rp->proto != proto)
678 /* Might be already set... */
679 first_int_addr = ip4_interface_first_address
680 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
685 return VNET_API_ERROR_VALUE_EXIST;
687 snat_add_static_mapping_when_resolved
688 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
689 addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
691 /* DHCP resolution required? */
692 if (first_int_addr == 0)
698 e_addr.as_u32 = first_int_addr->as_u32;
699 /* Identity mapping? */
700 if (l_addr.as_u32 == 0)
701 l_addr.as_u32 = e_addr.as_u32;
707 return VNET_API_ERROR_NO_SUCH_ENTRY;
709 vec_del1 (sm->to_resolve, i);
713 e_addr.as_u32 = first_int_addr->as_u32;
714 /* Identity mapping? */
715 if (l_addr.as_u32 == 0)
716 l_addr.as_u32 = e_addr.as_u32;
724 m_key.port = addr_only ? 0 : e_port;
725 m_key.protocol = addr_only ? 0 : proto;
727 kv.key = m_key.as_u64;
728 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
731 m = pool_elt_at_index (sm->static_mappings, value.value);
737 if (is_identity_static_mapping (m))
740 vec_foreach (local, m->locals)
742 if (local->vrf_id == vrf_id)
743 return VNET_API_ERROR_VALUE_EXIST;
746 vec_add2 (m->locals, local, 1);
747 local->vrf_id = vrf_id;
749 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
750 FIB_SOURCE_PLUGIN_LOW);
751 m_key.addr = m->local_addr;
752 m_key.port = m->local_port;
753 m_key.protocol = m->proto;
754 m_key.fib_index = local->fib_index;
755 kv.key = m_key.as_u64;
756 kv.value = m - sm->static_mappings;
757 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
761 return VNET_API_ERROR_VALUE_EXIST;
764 if (twice_nat && addr_only)
765 return VNET_API_ERROR_UNSUPPORTED;
767 /* Convert VRF id to FIB index */
770 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
771 FIB_SOURCE_PLUGIN_LOW);
772 /* If not specified use inside VRF id from SNAT plugin startup config */
775 fib_index = sm->inside_fib_index;
776 vrf_id = sm->inside_vrf_id;
779 if (!(out2in_only || identity_nat))
782 m_key.port = addr_only ? 0 : l_port;
783 m_key.protocol = addr_only ? 0 : proto;
784 m_key.fib_index = fib_index;
785 kv.key = m_key.as_u64;
786 if (!clib_bihash_search_8_8
787 (&sm->static_mapping_by_local, &kv, &value))
788 return VNET_API_ERROR_VALUE_EXIST;
791 /* Find external address in allocated addresses and reserve port for
792 address and port pair mapping when dynamic translations enabled */
793 if (!(addr_only || sm->static_mapping_only || out2in_only))
795 for (i = 0; i < vec_len (sm->addresses); i++)
797 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
799 a = sm->addresses + i;
800 /* External port must be unused */
803 #define _(N, j, n, s) \
804 case SNAT_PROTOCOL_##N: \
805 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
806 return VNET_API_ERROR_INVALID_VALUE; \
807 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
810 a->busy_##n##_ports++; \
811 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
814 foreach_snat_protocol
817 nat_log_info ("unknown protocol");
818 return VNET_API_ERROR_INVALID_VALUE_2;
823 /* External address must be allocated */
824 if (!a && (l_addr.as_u32 != e_addr.as_u32))
826 if (sw_if_index != ~0)
828 for (i = 0; i < vec_len (sm->to_resolve); i++)
830 rp = sm->to_resolve + i;
833 if (rp->sw_if_index != sw_if_index &&
834 rp->l_addr.as_u32 != l_addr.as_u32 &&
835 rp->vrf_id != vrf_id && rp->l_port != l_port &&
836 rp->e_port != e_port && rp->proto != proto)
839 vec_del1 (sm->to_resolve, i);
843 return VNET_API_ERROR_NO_SUCH_ENTRY;
847 pool_get (sm->static_mappings, m);
848 memset (m, 0, sizeof (*m));
849 m->tag = vec_dup (tag);
850 m->local_addr = l_addr;
851 m->external_addr = e_addr;
852 m->twice_nat = twice_nat;
854 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
856 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
859 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
860 vec_add2 (m->locals, local, 1);
861 local->vrf_id = vrf_id;
862 local->fib_index = fib_index;
867 m->fib_index = fib_index;
871 m->local_port = l_port;
872 m->external_port = e_port;
876 if (sm->num_workers > 1)
879 .src_address = m->local_addr,
881 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index));
882 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
885 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
887 m_key.addr = m->local_addr;
888 m_key.port = m->local_port;
889 m_key.protocol = m->proto;
890 m_key.fib_index = fib_index;
891 kv.key = m_key.as_u64;
892 kv.value = m - sm->static_mappings;
894 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
896 m_key.addr = m->external_addr;
897 m_key.port = m->external_port;
899 kv.key = m_key.as_u64;
900 kv.value = m - sm->static_mappings;
901 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
903 /* Delete dynamic sessions matching local address (+ local port) */
904 if (!(sm->static_mapping_only))
906 u_key.addr = m->local_addr;
907 u_key.fib_index = m->fib_index;
908 kv.key = u_key.as_u64;
909 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
911 user_index = value.value;
912 u = pool_elt_at_index (tsm->users, user_index);
915 head_index = u->sessions_per_user_list_head_index;
916 head = pool_elt_at_index (tsm->list_pool, head_index);
917 elt_index = head->next;
918 elt = pool_elt_at_index (tsm->list_pool, elt_index);
919 ses_index = elt->value;
920 while (ses_index != ~0)
922 s = pool_elt_at_index (tsm->sessions, ses_index);
923 elt = pool_elt_at_index (tsm->list_pool, elt->next);
924 ses_index = elt->value;
926 if (snat_is_session_static (s))
930 && (clib_net_to_host_u16 (s->in2out.port) !=
934 nat_free_session_data (sm, s,
935 tsm - sm->per_thread_data);
936 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
938 if (!addr_only && !sm->endpoint_dependent)
949 if (sw_if_index != ~0)
952 return VNET_API_ERROR_NO_SUCH_ENTRY;
957 for (i = 0; i < vec_len (m->locals); i++)
959 if (m->locals[i].vrf_id == vrf_id)
966 return VNET_API_ERROR_NO_SUCH_ENTRY;
968 fib_index = m->locals[i].fib_index;
969 vec_del1 (m->locals, i);
972 fib_index = m->fib_index;
974 /* Free external address port */
975 if (!(addr_only || sm->static_mapping_only || out2in_only))
977 for (i = 0; i < vec_len (sm->addresses); i++)
979 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
981 a = sm->addresses + i;
984 #define _(N, j, n, s) \
985 case SNAT_PROTOCOL_##N: \
986 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
989 a->busy_##n##_ports--; \
990 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
993 foreach_snat_protocol
996 nat_log_info ("unknown protocol");
997 return VNET_API_ERROR_INVALID_VALUE_2;
1004 if (sm->num_workers > 1)
1005 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1007 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1009 m_key.addr = m->local_addr;
1010 m_key.port = m->local_port;
1011 m_key.protocol = m->proto;
1012 m_key.fib_index = fib_index;
1013 kv.key = m_key.as_u64;
1015 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1017 /* Delete session(s) for static mapping if exist */
1018 if (!(sm->static_mapping_only) ||
1019 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1021 u_key.addr = m->local_addr;
1022 u_key.fib_index = fib_index;
1023 kv.key = u_key.as_u64;
1024 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1026 user_index = value.value;
1027 u = pool_elt_at_index (tsm->users, user_index);
1028 if (u->nstaticsessions)
1030 head_index = u->sessions_per_user_list_head_index;
1031 head = pool_elt_at_index (tsm->list_pool, head_index);
1032 elt_index = head->next;
1033 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1034 ses_index = elt->value;
1035 while (ses_index != ~0)
1037 s = pool_elt_at_index (tsm->sessions, ses_index);
1038 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1039 ses_index = elt->value;
1043 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1044 (clib_net_to_host_u16 (s->out2in.port) !=
1049 if (is_lb_session (s))
1052 if (!snat_is_session_static (s))
1055 nat_free_session_data (sm, s,
1056 tsm - sm->per_thread_data);
1057 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1059 if (!addr_only && !sm->endpoint_dependent)
1066 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1067 if (vec_len (m->locals))
1070 m_key.addr = m->external_addr;
1071 m_key.port = m->external_port;
1072 m_key.fib_index = 0;
1073 kv.key = m_key.as_u64;
1074 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1077 vec_free (m->workers);
1078 /* Delete static mapping from pool */
1079 pool_put (sm->static_mappings, m);
1082 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1085 /* Add/delete external address to FIB */
1087 pool_foreach (interface, sm->interfaces,
1089 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1092 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1095 pool_foreach (interface, sm->output_feature_interfaces,
1097 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1100 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1109 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1110 snat_protocol_t proto,
1111 nat44_lb_addr_port_t * locals, u8 is_add,
1112 twice_nat_type_t twice_nat, u8 out2in_only,
1113 u8 * tag, u32 affinity)
1115 snat_main_t *sm = &snat_main;
1116 snat_static_mapping_t *m;
1117 snat_session_key_t m_key;
1118 clib_bihash_kv_8_8_t kv, value;
1119 snat_address_t *a = 0;
1121 nat44_lb_addr_port_t *local;
1122 u32 elt_index, head_index, ses_index;
1123 snat_main_per_thread_data_t *tsm;
1124 snat_user_key_t u_key;
1127 dlist_elt_t *head, *elt;
1130 if (!sm->endpoint_dependent)
1131 return VNET_API_ERROR_FEATURE_DISABLED;
1133 m_key.addr = e_addr;
1134 m_key.port = e_port;
1135 m_key.protocol = proto;
1136 m_key.fib_index = 0;
1137 kv.key = m_key.as_u64;
1138 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1141 m = pool_elt_at_index (sm->static_mappings, value.value);
1146 return VNET_API_ERROR_VALUE_EXIST;
1148 if (vec_len (locals) < 2)
1149 return VNET_API_ERROR_INVALID_VALUE;
1151 /* Find external address in allocated addresses and reserve port for
1152 address and port pair mapping when dynamic translations enabled */
1153 if (!(sm->static_mapping_only || out2in_only))
1155 for (i = 0; i < vec_len (sm->addresses); i++)
1157 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1159 a = sm->addresses + i;
1160 /* External port must be unused */
1163 #define _(N, j, n, s) \
1164 case SNAT_PROTOCOL_##N: \
1165 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1166 return VNET_API_ERROR_INVALID_VALUE; \
1167 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1168 if (e_port > 1024) \
1170 a->busy_##n##_ports++; \
1171 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1174 foreach_snat_protocol
1177 nat_log_info ("unknown protocol");
1178 return VNET_API_ERROR_INVALID_VALUE_2;
1183 /* External address must be allocated */
1185 return VNET_API_ERROR_NO_SUCH_ENTRY;
1188 pool_get (sm->static_mappings, m);
1189 memset (m, 0, sizeof (*m));
1190 m->tag = vec_dup (tag);
1191 m->external_addr = e_addr;
1192 m->external_port = e_port;
1194 m->twice_nat = twice_nat;
1195 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1197 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1198 m->affinity = affinity;
1201 m->affinity_per_service_list_head_index =
1202 nat_affinity_get_per_service_list_head_index ();
1204 m->affinity_per_service_list_head_index = ~0;
1206 m_key.addr = m->external_addr;
1207 m_key.port = m->external_port;
1208 m_key.protocol = m->proto;
1209 m_key.fib_index = 0;
1210 kv.key = m_key.as_u64;
1211 kv.value = m - sm->static_mappings;
1212 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1214 nat_log_err ("static_mapping_by_external key add failed");
1215 return VNET_API_ERROR_UNSPECIFIED;
1218 m_key.fib_index = m->fib_index;
1219 for (i = 0; i < vec_len (locals); i++)
1221 locals[i].fib_index =
1222 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1224 FIB_SOURCE_PLUGIN_LOW);
1225 m_key.addr = locals[i].addr;
1226 m_key.fib_index = locals[i].fib_index;
1229 m_key.port = locals[i].port;
1230 kv.key = m_key.as_u64;
1231 kv.value = m - sm->static_mappings;
1232 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1234 locals[i].prefix = (i == 0) ? locals[i].probability :
1235 (locals[i - 1].prefix + locals[i].probability);
1236 vec_add1 (m->locals, locals[i]);
1237 if (sm->num_workers > 1)
1240 .src_address = locals[i].addr,
1243 clib_bitmap_set (bitmap,
1244 sm->worker_in2out_cb (&ip, m->fib_index), 1);
1248 /* Assign workers */
1249 if (sm->num_workers > 1)
1252 clib_bitmap_foreach (i, bitmap,
1254 vec_add1(m->workers, i);
1262 return VNET_API_ERROR_NO_SUCH_ENTRY;
1264 if (!is_lb_static_mapping (m))
1265 return VNET_API_ERROR_INVALID_VALUE;
1267 /* Free external address port */
1268 if (!(sm->static_mapping_only || out2in_only))
1270 for (i = 0; i < vec_len (sm->addresses); i++)
1272 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1274 a = sm->addresses + i;
1277 #define _(N, j, n, s) \
1278 case SNAT_PROTOCOL_##N: \
1279 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1280 if (e_port > 1024) \
1282 a->busy_##n##_ports--; \
1283 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1286 foreach_snat_protocol
1289 nat_log_info ("unknown protocol");
1290 return VNET_API_ERROR_INVALID_VALUE_2;
1297 m_key.addr = m->external_addr;
1298 m_key.port = m->external_port;
1299 m_key.protocol = m->proto;
1300 m_key.fib_index = 0;
1301 kv.key = m_key.as_u64;
1302 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1304 nat_log_err ("static_mapping_by_external key del failed");
1305 return VNET_API_ERROR_UNSPECIFIED;
1309 vec_foreach (local, m->locals)
1311 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1312 FIB_SOURCE_PLUGIN_LOW);
1313 m_key.addr = local->addr;
1316 m_key.port = local->port;
1317 m_key.fib_index = local->fib_index;
1318 kv.key = m_key.as_u64;
1319 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1321 nat_log_err ("static_mapping_by_local key del failed");
1322 return VNET_API_ERROR_UNSPECIFIED;
1326 if (sm->num_workers > 1)
1329 .src_address = local->addr,
1331 tsm = vec_elt_at_index (sm->per_thread_data,
1332 sm->worker_in2out_cb (&ip, m->fib_index));
1335 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1337 /* Delete sessions */
1338 u_key.addr = local->addr;
1339 u_key.fib_index = m->fib_index;
1340 kv.key = u_key.as_u64;
1341 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1343 u = pool_elt_at_index (tsm->users, value.value);
1344 if (u->nstaticsessions)
1346 head_index = u->sessions_per_user_list_head_index;
1347 head = pool_elt_at_index (tsm->list_pool, head_index);
1348 elt_index = head->next;
1349 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1350 ses_index = elt->value;
1351 while (ses_index != ~0)
1353 s = pool_elt_at_index (tsm->sessions, ses_index);
1354 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1355 ses_index = elt->value;
1357 if (!(is_lb_session (s)))
1360 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1361 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1364 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1365 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1372 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1373 vec_free (m->locals);
1375 vec_free (m->workers);
1377 pool_put (sm->static_mappings, m);
1384 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1387 snat_address_t *a = 0;
1388 snat_session_t *ses;
1389 u32 *ses_to_be_removed = 0, *ses_index;
1390 snat_main_per_thread_data_t *tsm;
1391 snat_static_mapping_t *m;
1392 snat_interface_t *interface;
1394 snat_address_t *addresses =
1395 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1397 /* Find SNAT address */
1398 for (i = 0; i < vec_len (addresses); i++)
1400 if (addresses[i].addr.as_u32 == addr.as_u32)
1407 return VNET_API_ERROR_NO_SUCH_ENTRY;
1412 pool_foreach (m, sm->static_mappings,
1414 if (m->external_addr.as_u32 == addr.as_u32)
1415 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1416 m->local_port, m->external_port,
1417 m->vrf_id, is_addr_only_static_mapping(m), ~0,
1418 m->proto, 0, m->twice_nat,
1419 is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1425 /* Check if address is used in some static mapping */
1426 if (is_snat_address_used_in_static_mapping (sm, addr))
1428 nat_log_notice ("address used in static mapping");
1429 return VNET_API_ERROR_UNSPECIFIED;
1433 if (a->fib_index != ~0)
1434 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1436 /* Delete sessions using address */
1437 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1440 vec_foreach (tsm, sm->per_thread_data)
1442 pool_foreach (ses, tsm->sessions, ({
1443 if (ses->out2in.addr.as_u32 == addr.as_u32)
1445 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1446 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1450 vec_foreach (ses_index, ses_to_be_removed)
1452 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1453 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1456 vec_free (ses_to_be_removed);
1461 #define _(N, i, n, s) \
1462 clib_bitmap_free (a->busy_##n##_port_bitmap); \
1463 vec_free (a->busy_##n##_ports_per_thread);
1464 foreach_snat_protocol
1468 vec_del1 (sm->twice_nat_addresses, i);
1472 vec_del1 (sm->addresses, i);
1474 /* Delete external address from FIB */
1476 pool_foreach (interface, sm->interfaces,
1478 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1481 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1484 pool_foreach (interface, sm->output_feature_interfaces,
1486 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1489 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1498 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1500 snat_main_t *sm = &snat_main;
1501 snat_interface_t *i;
1502 const char *feature_name, *del_feature_name;
1504 snat_static_mapping_t *m;
1506 nat_outside_fib_t *outside_fib;
1507 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1510 if (sm->out2in_dpo && !is_inside)
1511 return VNET_API_ERROR_UNSUPPORTED;
1514 pool_foreach (i, sm->output_feature_interfaces,
1516 if (i->sw_if_index == sw_if_index)
1517 return VNET_API_ERROR_VALUE_EXIST;
1521 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1522 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1525 if (sm->num_workers > 1 && !sm->deterministic)
1527 is_inside ? "nat44-in2out-worker-handoff" :
1528 "nat44-out2in-worker-handoff";
1529 else if (sm->deterministic)
1530 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1531 else if (sm->endpoint_dependent)
1532 feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in";
1534 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1537 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1538 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1541 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1542 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1548 vec_foreach (outside_fib, sm->outside_fibs)
1550 if (outside_fib->fib_index == fib_index)
1554 outside_fib->refcount--;
1555 if (!outside_fib->refcount)
1556 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1559 outside_fib->refcount++;
1566 vec_add2 (sm->outside_fibs, outside_fib, 1);
1567 outside_fib->refcount = 1;
1568 outside_fib->fib_index = fib_index;
1573 pool_foreach (i, sm->interfaces,
1575 if (i->sw_if_index == sw_if_index)
1579 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1582 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1584 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1586 if (sm->num_workers > 1 && !sm->deterministic)
1588 del_feature_name = "nat44-handoff-classify";
1589 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1590 "nat44-out2in-worker-handoff";
1592 else if (sm->deterministic)
1594 del_feature_name = "nat44-det-classify";
1595 feature_name = !is_inside ? "nat44-det-in2out" :
1598 else if (sm->endpoint_dependent)
1600 del_feature_name = "nat44-ed-classify";
1601 feature_name = !is_inside ? "nat44-ed-in2out" :
1606 del_feature_name = "nat44-classify";
1607 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1610 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1611 sw_if_index, 0, 0, 0);
1612 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1613 sw_if_index, 1, 0, 0);
1616 if (sm->endpoint_dependent)
1617 vnet_feature_enable_disable ("ip4-local",
1618 "nat44-ed-hairpinning",
1619 sw_if_index, 1, 0, 0);
1620 else if (!sm->deterministic)
1621 vnet_feature_enable_disable ("ip4-local",
1622 "nat44-hairpinning",
1623 sw_if_index, 1, 0, 0);
1628 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1629 sw_if_index, 0, 0, 0);
1630 pool_put (sm->interfaces, i);
1633 if (sm->endpoint_dependent)
1634 vnet_feature_enable_disable ("ip4-local",
1635 "nat44-ed-hairpinning",
1636 sw_if_index, 0, 0, 0);
1637 else if (!sm->deterministic)
1638 vnet_feature_enable_disable ("ip4-local",
1639 "nat44-hairpinning",
1640 sw_if_index, 0, 0, 0);
1646 if ((nat_interface_is_inside(i) && is_inside) ||
1647 (nat_interface_is_outside(i) && !is_inside))
1650 if (sm->num_workers > 1 && !sm->deterministic)
1652 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1653 "nat44-out2in-worker-handoff";
1654 feature_name = "nat44-handoff-classify";
1656 else if (sm->deterministic)
1658 del_feature_name = !is_inside ? "nat44-det-in2out" :
1660 feature_name = "nat44-det-classify";
1662 else if (sm->endpoint_dependent)
1664 del_feature_name = !is_inside ? "nat44-ed-in2out" :
1666 feature_name = "nat44-ed-classify";
1670 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1671 feature_name = "nat44-classify";
1674 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1675 sw_if_index, 0, 0, 0);
1676 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1677 sw_if_index, 1, 0, 0);
1680 if (sm->endpoint_dependent)
1681 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1682 sw_if_index, 0, 0, 0);
1683 else if (!sm->deterministic)
1684 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1685 sw_if_index, 0, 0, 0);
1696 return VNET_API_ERROR_NO_SUCH_ENTRY;
1698 pool_get (sm->interfaces, i);
1699 i->sw_if_index = sw_if_index;
1701 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
1704 if (is_inside && !sm->out2in_dpo)
1706 if (sm->endpoint_dependent)
1707 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1708 sw_if_index, 1, 0, 0);
1709 else if (!sm->deterministic)
1710 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1711 sw_if_index, 1, 0, 0);
1717 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1721 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1723 /* Add/delete external addresses to FIB */
1726 vec_foreach (ap, sm->addresses)
1727 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1729 pool_foreach (m, sm->static_mappings,
1731 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1734 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1737 pool_foreach (dm, sm->det_maps,
1739 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1747 snat_interface_add_del_output_feature (u32 sw_if_index,
1748 u8 is_inside, int is_del)
1750 snat_main_t *sm = &snat_main;
1751 snat_interface_t *i;
1753 snat_static_mapping_t *m;
1755 if (sm->deterministic ||
1756 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1757 return VNET_API_ERROR_UNSUPPORTED;
1760 pool_foreach (i, sm->interfaces,
1762 if (i->sw_if_index == sw_if_index)
1763 return VNET_API_ERROR_VALUE_EXIST;
1769 if (sm->endpoint_dependent)
1771 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
1772 sw_if_index, !is_del, 0, 0);
1773 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
1774 sw_if_index, !is_del, 0, 0);
1778 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1779 sw_if_index, !is_del, 0, 0);
1780 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1781 sw_if_index, !is_del, 0, 0);
1786 if (sm->num_workers > 1)
1788 vnet_feature_enable_disable ("ip4-unicast",
1789 "nat44-out2in-worker-handoff",
1790 sw_if_index, !is_del, 0, 0);
1791 vnet_feature_enable_disable ("ip4-output",
1792 "nat44-in2out-output-worker-handoff",
1793 sw_if_index, !is_del, 0, 0);
1797 if (sm->endpoint_dependent)
1799 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in",
1800 sw_if_index, !is_del, 0, 0);
1801 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
1802 sw_if_index, !is_del, 0, 0);
1806 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
1807 sw_if_index, !is_del, 0, 0);
1808 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1809 sw_if_index, !is_del, 0, 0);
1814 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1815 sm->fq_in2out_output_index =
1816 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1818 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1819 sm->fq_out2in_index =
1820 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1823 pool_foreach (i, sm->output_feature_interfaces,
1825 if (i->sw_if_index == sw_if_index)
1828 pool_put (sm->output_feature_interfaces, i);
1830 return VNET_API_ERROR_VALUE_EXIST;
1838 return VNET_API_ERROR_NO_SUCH_ENTRY;
1840 pool_get (sm->output_feature_interfaces, i);
1841 i->sw_if_index = sw_if_index;
1844 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1846 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1848 /* Add/delete external addresses to FIB */
1854 vec_foreach (ap, sm->addresses)
1855 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1857 pool_foreach (m, sm->static_mappings,
1859 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1862 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1870 snat_set_workers (uword * bitmap)
1872 snat_main_t *sm = &snat_main;
1875 if (sm->num_workers < 2)
1876 return VNET_API_ERROR_FEATURE_DISABLED;
1878 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1879 return VNET_API_ERROR_INVALID_WORKER;
1881 vec_free (sm->workers);
1883 clib_bitmap_foreach (i, bitmap,
1885 vec_add1(sm->workers, i);
1886 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1891 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1892 sm->num_snat_thread = _vec_len (sm->workers);
1899 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1902 ip4_address_t * address,
1904 u32 if_address_index, u32 is_delete);
1907 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
1910 ip4_address_t * address,
1912 u32 if_address_index, u32 is_delete);
1915 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1918 snat_session_key_t * k,
1919 u16 port_per_thread, u32 snat_thread_index);
1921 static clib_error_t *
1922 snat_init (vlib_main_t * vm)
1924 snat_main_t *sm = &snat_main;
1925 clib_error_t *error = 0;
1926 ip4_main_t *im = &ip4_main;
1927 ip_lookup_main_t *lm = &im->lookup_main;
1929 vlib_thread_registration_t *tr;
1930 vlib_thread_main_t *tm = vlib_get_thread_main ();
1933 ip4_add_del_interface_address_callback_t cb4;
1934 vlib_node_t *error_drop_node;
1937 sm->vnet_main = vnet_get_main ();
1939 sm->ip4_lookup_main = lm;
1940 sm->api_main = &api_main;
1941 sm->first_worker_index = 0;
1942 sm->num_workers = 0;
1943 sm->num_snat_thread = 1;
1945 sm->port_per_thread = 0xffff - 1024;
1946 sm->fq_in2out_index = ~0;
1947 sm->fq_out2in_index = ~0;
1948 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1949 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1950 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1951 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1952 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1953 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
1954 sm->forwarding_enabled = 0;
1955 sm->log_class = vlib_log_register_class ("nat", 0);
1956 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
1957 sm->error_node_index = error_drop_node->index;
1958 sm->mss_clamping = 0;
1960 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1963 tr = (vlib_thread_registration_t *) p[0];
1966 sm->num_workers = tr->count;
1967 sm->first_worker_index = tr->first_index;
1971 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1973 /* Use all available workers by default */
1974 if (sm->num_workers > 1)
1976 for (i = 0; i < sm->num_workers; i++)
1977 bitmap = clib_bitmap_set (bitmap, i, 1);
1978 snat_set_workers (bitmap);
1979 clib_bitmap_free (bitmap);
1983 sm->per_thread_data[0].snat_thread_index = 0;
1986 error = snat_api_init (vm, sm);
1990 /* Set up the interface address add/del callback */
1991 cb4.function = snat_ip4_add_del_interface_address_cb;
1992 cb4.function_opaque = 0;
1994 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1996 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
1997 cb4.function_opaque = 0;
1999 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2001 nat_dpo_module_init ();
2003 /* Init IPFIX logging */
2004 snat_ipfix_logging_init (vm);
2007 error = nat64_init (vm);
2015 /* Init virtual fragmenentation reassembly */
2016 return nat_reass_init (vm);
2019 VLIB_INIT_FUNCTION (snat_init);
2022 snat_free_outside_address_and_port (snat_address_t * addresses,
2023 u32 thread_index, snat_session_key_t * k)
2027 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2029 for (address_index = 0; address_index < vec_len (addresses);
2032 if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
2036 ASSERT (address_index < vec_len (addresses));
2038 a = addresses + address_index;
2040 switch (k->protocol)
2042 #define _(N, i, n, s) \
2043 case SNAT_PROTOCOL_##N: \
2044 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
2045 port_host_byte_order) == 1); \
2046 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
2047 port_host_byte_order, 0); \
2048 a->busy_##n##_ports--; \
2049 a->busy_##n##_ports_per_thread[thread_index]--; \
2051 foreach_snat_protocol
2054 nat_log_info ("unknown protocol");
2060 snat_static_mapping_match (snat_main_t * sm,
2061 snat_session_key_t match,
2062 snat_session_key_t * mapping,
2065 twice_nat_type_t * twice_nat,
2066 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2067 u8 * is_identity_nat)
2069 clib_bihash_kv_8_8_t kv, value;
2070 snat_static_mapping_t *m;
2071 snat_session_key_t m_key;
2072 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2073 u32 rand, lo = 0, hi, mid;
2076 m_key.fib_index = match.fib_index;
2079 mapping_hash = &sm->static_mapping_by_external;
2080 m_key.fib_index = 0;
2083 m_key.addr = match.addr;
2084 m_key.port = clib_net_to_host_u16 (match.port);
2085 m_key.protocol = match.protocol;
2087 kv.key = m_key.as_u64;
2089 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2091 /* Try address only mapping */
2094 kv.key = m_key.as_u64;
2095 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2099 m = pool_elt_at_index (sm->static_mappings, value.value);
2103 if (is_lb_static_mapping (m))
2105 if (PREDICT_FALSE (lb != 0))
2106 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2109 if (nat_affinity_find_and_lock (ext_host_addr[0], match.addr,
2110 match.protocol, match.port,
2114 mapping->addr = m->locals[backend_index].addr;
2116 clib_host_to_net_u16 (m->locals[backend_index].port);
2117 mapping->fib_index = m->locals[backend_index].fib_index;
2121 hi = vec_len (m->locals) - 1;
2122 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
2125 mid = ((hi - lo) >> 1) + lo;
2126 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
2128 if (!(m->locals[lo].prefix >= rand))
2130 if (PREDICT_FALSE (sm->num_workers > 1))
2133 .src_address = m->locals[lo].addr,
2135 if (sm->worker_in2out_cb (&ip, m->fib_index) !=
2136 vlib_get_thread_index ())
2139 mapping->addr = m->locals[lo].addr;
2140 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
2141 mapping->fib_index = m->locals[lo].fib_index;
2144 if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr,
2145 match.protocol, match.port,
2147 m->affinity_per_service_list_head_index))
2148 nat_log_info ("create affinity record failed");
2153 if (PREDICT_FALSE (lb != 0))
2155 mapping->fib_index = m->fib_index;
2156 mapping->addr = m->local_addr;
2157 /* Address only mapping doesn't change port */
2158 mapping->port = is_addr_only_static_mapping (m) ? match.port
2159 : clib_host_to_net_u16 (m->local_port);
2161 mapping->protocol = m->proto;
2165 mapping->addr = m->external_addr;
2166 /* Address only mapping doesn't change port */
2167 mapping->port = is_addr_only_static_mapping (m) ? match.port
2168 : clib_host_to_net_u16 (m->external_port);
2169 mapping->fib_index = sm->outside_fib_index;
2173 if (PREDICT_FALSE (is_addr_only != 0))
2174 *is_addr_only = is_addr_only_static_mapping (m);
2176 if (PREDICT_FALSE (twice_nat != 0))
2177 *twice_nat = m->twice_nat;
2179 if (PREDICT_FALSE (is_identity_nat != 0))
2180 *is_identity_nat = is_identity_static_mapping (m);
2185 static_always_inline u16
2186 snat_random_port (u16 min, u16 max)
2188 snat_main_t *sm = &snat_main;
2189 return min + random_u32 (&sm->random_seed) /
2190 (random_u32_max () / (max - min + 1) + 1);
2194 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2197 snat_session_key_t * k,
2198 u16 port_per_thread,
2199 u32 snat_thread_index)
2201 snat_main_t *sm = &snat_main;
2203 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, k,
2204 port_per_thread, snat_thread_index);
2208 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2211 snat_session_key_t * k,
2212 u16 port_per_thread, u32 snat_thread_index)
2215 snat_address_t *a, *ga = 0;
2218 for (i = 0; i < vec_len (addresses); i++)
2221 switch (k->protocol)
2223 #define _(N, j, n, s) \
2224 case SNAT_PROTOCOL_##N: \
2225 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2227 if (a->fib_index == fib_index) \
2231 portnum = (port_per_thread * \
2232 snat_thread_index) + \
2233 snat_random_port(1, port_per_thread) + 1024; \
2234 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2236 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2237 a->busy_##n##_ports_per_thread[thread_index]++; \
2238 a->busy_##n##_ports++; \
2239 k->addr = a->addr; \
2240 k->port = clib_host_to_net_u16(portnum); \
2244 else if (a->fib_index == ~0) \
2250 foreach_snat_protocol
2253 nat_log_info ("unknown protocol");
2262 switch (k->protocol)
2264 #define _(N, j, n, s) \
2265 case SNAT_PROTOCOL_##N: \
2268 portnum = (port_per_thread * \
2269 snat_thread_index) + \
2270 snat_random_port(1, port_per_thread) + 1024; \
2271 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2273 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2274 a->busy_##n##_ports_per_thread[thread_index]++; \
2275 a->busy_##n##_ports++; \
2276 k->addr = a->addr; \
2277 k->port = clib_host_to_net_u16(portnum); \
2281 foreach_snat_protocol
2284 nat_log_info ("unknown protocol");
2289 /* Totally out of translations to use... */
2290 snat_ipfix_logging_addresses_exhausted (0);
2295 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2298 snat_session_key_t * k,
2299 u16 port_per_thread, u32 snat_thread_index)
2301 snat_main_t *sm = &snat_main;
2302 snat_address_t *a = addresses;
2303 u16 m, ports, portnum, A, j;
2304 m = 16 - (sm->psid_offset + sm->psid_length);
2305 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2307 if (!vec_len (addresses))
2310 switch (k->protocol)
2312 #define _(N, i, n, s) \
2313 case SNAT_PROTOCOL_##N: \
2314 if (a->busy_##n##_ports < ports) \
2318 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2319 j = snat_random_port(0, pow2_mask(m)); \
2320 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2321 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2323 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2324 a->busy_##n##_ports++; \
2325 k->addr = a->addr; \
2326 k->port = clib_host_to_net_u16 (portnum); \
2331 foreach_snat_protocol
2334 nat_log_info ("unknown protocol");
2339 /* Totally out of translations to use... */
2340 snat_ipfix_logging_addresses_exhausted (0);
2345 nat_alloc_addr_and_port_range (snat_address_t * addresses,
2348 snat_session_key_t * k,
2349 u16 port_per_thread, u32 snat_thread_index)
2351 snat_main_t *sm = &snat_main;
2352 snat_address_t *a = addresses;
2355 ports = sm->end_port - sm->start_port + 1;
2357 if (!vec_len (addresses))
2360 switch (k->protocol)
2362 #define _(N, i, n, s) \
2363 case SNAT_PROTOCOL_##N: \
2364 if (a->busy_##n##_ports < ports) \
2368 portnum = snat_random_port(sm->start_port, sm->end_port); \
2369 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2371 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2372 a->busy_##n##_ports++; \
2373 k->addr = a->addr; \
2374 k->port = clib_host_to_net_u16 (portnum); \
2379 foreach_snat_protocol
2382 nat_log_info ("unknown protocol");
2387 /* Totally out of translations to use... */
2388 snat_ipfix_logging_addresses_exhausted (0);
2393 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2395 dpo_id_t dpo_v4 = DPO_INVALID;
2396 fib_prefix_t pfx = {
2397 .fp_proto = FIB_PROTOCOL_IP4,
2399 .fp_addr.ip4.as_u32 = addr.as_u32,
2404 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2405 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2406 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2407 dpo_reset (&dpo_v4);
2411 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2416 format_session_kvp (u8 * s, va_list * args)
2418 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2419 snat_session_key_t k;
2423 s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
2429 format_static_mapping_kvp (u8 * s, va_list * args)
2431 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2432 snat_session_key_t k;
2436 s = format (s, "%U static-mapping-index %llu",
2437 format_static_mapping_key, &k, v->value);
2443 format_user_kvp (u8 * s, va_list * args)
2445 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2450 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2451 k.fib_index, v->value);
2457 format_ed_session_kvp (u8 * s, va_list * args)
2459 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
2462 k.as_u64[0] = v->key[0];
2463 k.as_u64[1] = v->key[1];
2466 format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2467 format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
2468 format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
2469 format_ip_protocol, k.proto, k.fib_index, v->value);
2475 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2477 snat_main_t *sm = &snat_main;
2478 u32 next_worker_index = 0;
2481 next_worker_index = sm->first_worker_index;
2482 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2483 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
2485 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2486 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2488 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2490 return next_worker_index;
2494 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2496 snat_main_t *sm = &snat_main;
2499 snat_session_key_t m_key;
2500 clib_bihash_kv_8_8_t kv, value;
2501 snat_static_mapping_t *m;
2503 u32 next_worker_index = 0;
2505 /* first try static mappings without port */
2506 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2508 m_key.addr = ip0->dst_address;
2511 m_key.fib_index = rx_fib_index0;
2512 kv.key = m_key.as_u64;
2513 if (!clib_bihash_search_8_8
2514 (&sm->static_mapping_by_external, &kv, &value))
2516 m = pool_elt_at_index (sm->static_mappings, value.value);
2517 return m->workers[0];
2521 proto = ip_proto_to_snat_proto (ip0->protocol);
2522 udp = ip4_next_header (ip0);
2523 port = udp->dst_port;
2525 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2527 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2528 return vlib_get_thread_index ();
2530 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2532 nat_reass_ip4_t *reass;
2534 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2535 ip0->fragment_id, ip0->protocol);
2537 if (reass && (reass->thread_index != (u32) ~ 0))
2538 return reass->thread_index;
2540 return vlib_get_thread_index ();
2544 /* unknown protocol */
2545 if (PREDICT_FALSE (proto == ~0))
2547 /* use current thread */
2548 return vlib_get_thread_index ();
2551 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2553 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2554 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2555 if (!icmp_is_error_message (icmp))
2556 port = echo->identifier;
2559 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2560 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2561 void *l4_header = ip4_next_header (inner_ip);
2564 case SNAT_PROTOCOL_ICMP:
2565 icmp = (icmp46_header_t *) l4_header;
2566 echo = (icmp_echo_header_t *) (icmp + 1);
2567 port = echo->identifier;
2569 case SNAT_PROTOCOL_UDP:
2570 case SNAT_PROTOCOL_TCP:
2571 port = ((tcp_udp_header_t *) l4_header)->src_port;
2574 return vlib_get_thread_index ();
2579 /* try static mappings with port */
2580 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2582 m_key.addr = ip0->dst_address;
2583 m_key.port = clib_net_to_host_u16 (port);
2584 m_key.protocol = proto;
2585 m_key.fib_index = rx_fib_index0;
2586 kv.key = m_key.as_u64;
2587 if (!clib_bihash_search_8_8
2588 (&sm->static_mapping_by_external, &kv, &value))
2590 m = pool_elt_at_index (sm->static_mappings, value.value);
2591 return m->workers[0];
2595 /* worker by outside port */
2596 next_worker_index = sm->first_worker_index;
2597 next_worker_index +=
2598 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2599 return next_worker_index;
2603 nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index)
2605 snat_main_t *sm = &snat_main;
2606 clib_bihash_kv_8_8_t kv, value;
2607 u32 proto, next_worker_index = 0;
2610 snat_static_mapping_t *m;
2613 /* first try static mappings without port */
2614 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2616 make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0);
2617 if (!clib_bihash_search_8_8
2618 (&sm->static_mapping_by_external, &kv, &value))
2620 m = pool_elt_at_index (sm->static_mappings, value.value);
2621 return m->workers[0];
2625 proto = ip_proto_to_snat_proto (ip->protocol);
2627 /* unknown protocol */
2628 if (PREDICT_FALSE (proto == ~0))
2630 /* use current thread */
2631 return vlib_get_thread_index ();
2634 udp = ip4_next_header (ip);
2635 port = udp->dst_port;
2637 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2639 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2640 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2641 if (!icmp_is_error_message (icmp))
2642 port = echo->identifier;
2645 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2646 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2647 void *l4_header = ip4_next_header (inner_ip);
2650 case SNAT_PROTOCOL_ICMP:
2651 icmp = (icmp46_header_t *) l4_header;
2652 echo = (icmp_echo_header_t *) (icmp + 1);
2653 port = echo->identifier;
2655 case SNAT_PROTOCOL_UDP:
2656 case SNAT_PROTOCOL_TCP:
2657 port = ((tcp_udp_header_t *) l4_header)->src_port;
2660 return vlib_get_thread_index ();
2665 /* try static mappings with port */
2666 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2668 make_sm_kv (&kv, &ip->dst_address, proto, rx_fib_index,
2669 clib_net_to_host_u16 (port));
2670 if (!clib_bihash_search_8_8
2671 (&sm->static_mapping_by_external, &kv, &value))
2673 m = pool_elt_at_index (sm->static_mappings, value.value);
2674 if (!is_lb_static_mapping (m))
2675 return m->workers[0];
2677 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2678 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2680 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2681 return m->workers[hash & (_vec_len (m->workers) - 1)];
2683 return m->workers[hash % _vec_len (m->workers)];
2687 /* worker by outside port */
2688 next_worker_index = sm->first_worker_index;
2689 next_worker_index +=
2690 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2692 return next_worker_index;
2695 static clib_error_t *
2696 snat_config (vlib_main_t * vm, unformat_input_t * input)
2698 snat_main_t *sm = &snat_main;
2699 nat66_main_t *nm = &nat66_main;
2700 u32 translation_buckets = 1024;
2701 u32 translation_memory_size = 128 << 20;
2702 u32 user_buckets = 128;
2703 u32 user_memory_size = 64 << 20;
2704 u32 max_translations_per_user = 100;
2705 u32 outside_vrf_id = 0;
2706 u32 outside_ip6_vrf_id = 0;
2707 u32 inside_vrf_id = 0;
2708 u32 static_mapping_buckets = 1024;
2709 u32 static_mapping_memory_size = 64 << 20;
2710 u32 nat64_bib_buckets = 1024;
2711 u32 nat64_bib_memory_size = 128 << 20;
2712 u32 nat64_st_buckets = 2048;
2713 u32 nat64_st_memory_size = 256 << 20;
2714 u8 static_mapping_only = 0;
2715 u8 static_mapping_connection_tracking = 0;
2716 snat_main_per_thread_data_t *tsm;
2717 dslite_main_t *dm = &dslite_main;
2719 sm->deterministic = 0;
2721 sm->endpoint_dependent = 0;
2723 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2726 (input, "translation hash buckets %d", &translation_buckets))
2728 else if (unformat (input, "translation hash memory %d",
2729 &translation_memory_size));
2730 else if (unformat (input, "user hash buckets %d", &user_buckets))
2732 else if (unformat (input, "user hash memory %d", &user_memory_size))
2734 else if (unformat (input, "max translations per user %d",
2735 &max_translations_per_user))
2737 else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
2739 else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
2741 else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
2743 else if (unformat (input, "static mapping only"))
2745 static_mapping_only = 1;
2746 if (unformat (input, "connection tracking"))
2747 static_mapping_connection_tracking = 1;
2749 else if (unformat (input, "deterministic"))
2750 sm->deterministic = 1;
2751 else if (unformat (input, "nat64 bib hash buckets %d",
2752 &nat64_bib_buckets))
2754 else if (unformat (input, "nat64 bib hash memory %d",
2755 &nat64_bib_memory_size))
2758 if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2760 else if (unformat (input, "nat64 st hash memory %d",
2761 &nat64_st_memory_size))
2763 else if (unformat (input, "out2in dpo"))
2765 else if (unformat (input, "dslite ce"))
2766 dslite_set_ce (dm, 1);
2767 else if (unformat (input, "endpoint-dependent"))
2768 sm->endpoint_dependent = 1;
2770 return clib_error_return (0, "unknown input '%U'",
2771 format_unformat_error, input);
2774 if (sm->deterministic && sm->endpoint_dependent)
2775 return clib_error_return (0,
2776 "deterministic and endpoint-dependent modes are mutually exclusive");
2778 if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
2779 return clib_error_return (0,
2780 "static mapping only mode available only for simple nat");
2782 if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
2783 return clib_error_return (0,
2784 "out2in dpo mode available only for simple nat");
2786 /* for show commands, etc. */
2787 sm->translation_buckets = translation_buckets;
2788 sm->translation_memory_size = translation_memory_size;
2789 /* do not exceed load factor 10 */
2790 sm->max_translations = 10 * translation_buckets;
2791 sm->user_buckets = user_buckets;
2792 sm->user_memory_size = user_memory_size;
2793 sm->max_translations_per_user = max_translations_per_user;
2794 sm->outside_vrf_id = outside_vrf_id;
2795 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2797 FIB_SOURCE_PLUGIN_HI);
2798 nm->outside_vrf_id = outside_ip6_vrf_id;
2799 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
2801 FIB_SOURCE_PLUGIN_HI);
2802 sm->inside_vrf_id = inside_vrf_id;
2803 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2805 FIB_SOURCE_PLUGIN_HI);
2806 sm->static_mapping_only = static_mapping_only;
2807 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2809 nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2810 nat64_st_memory_size);
2812 if (sm->deterministic)
2814 sm->in2out_node_index = snat_det_in2out_node.index;
2815 sm->in2out_output_node_index = ~0;
2816 sm->out2in_node_index = snat_det_out2in_node.index;
2817 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2818 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2822 if (sm->endpoint_dependent)
2824 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2825 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
2826 sm->in2out_node_index = nat44_ed_in2out_node.index;
2827 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
2828 sm->out2in_node_index = nat44_ed_out2in_node.index;
2829 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
2830 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
2831 nat_affinity_init (vm);
2835 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2836 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2837 sm->in2out_node_index = snat_in2out_node.index;
2838 sm->in2out_output_node_index = snat_in2out_output_node.index;
2839 sm->out2in_node_index = snat_out2in_node.index;
2840 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2841 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2843 if (!static_mapping_only ||
2844 (static_mapping_only && static_mapping_connection_tracking))
2847 vec_foreach (tsm, sm->per_thread_data)
2849 if (sm->endpoint_dependent)
2851 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
2852 translation_buckets,
2853 translation_memory_size);
2854 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
2855 format_ed_session_kvp);
2857 clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
2858 translation_buckets,
2859 translation_memory_size);
2860 clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
2861 format_ed_session_kvp);
2865 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
2866 translation_buckets,
2867 translation_memory_size);
2868 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
2869 format_session_kvp);
2871 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
2872 translation_buckets,
2873 translation_memory_size);
2874 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
2875 format_session_kvp);
2878 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2880 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
2888 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2889 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2891 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2892 "static_mapping_by_local", static_mapping_buckets,
2893 static_mapping_memory_size);
2894 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
2895 format_static_mapping_kvp);
2897 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2898 "static_mapping_by_external",
2899 static_mapping_buckets,
2900 static_mapping_memory_size);
2901 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
2902 format_static_mapping_kvp);
2908 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2911 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2914 ip4_address_t * address,
2916 u32 if_address_index, u32 is_delete)
2918 snat_main_t *sm = &snat_main;
2919 snat_static_map_resolve_t *rp;
2920 snat_static_mapping_t *m;
2921 snat_session_key_t m_key;
2922 clib_bihash_kv_8_8_t kv, value;
2924 ip4_address_t l_addr;
2926 for (i = 0; i < vec_len (sm->to_resolve); i++)
2928 rp = sm->to_resolve + i;
2929 if (rp->addr_only == 0)
2931 if (rp->sw_if_index == sw_if_index)
2938 m_key.addr.as_u32 = address->as_u32;
2939 m_key.port = rp->addr_only ? 0 : rp->e_port;
2940 m_key.protocol = rp->addr_only ? 0 : rp->proto;
2941 m_key.fib_index = sm->outside_fib_index;
2942 kv.key = m_key.as_u64;
2943 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2946 m = pool_elt_at_index (sm->static_mappings, value.value);
2950 /* Don't trip over lease renewal, static config */
2960 /* Indetity mapping? */
2961 if (rp->l_addr.as_u32 == 0)
2962 l_addr.as_u32 = address[0].as_u32;
2964 l_addr.as_u32 = rp->l_addr.as_u32;
2965 /* Add the static mapping */
2966 rv = snat_add_static_mapping (l_addr,
2971 rp->addr_only, ~0 /* sw_if_index */ ,
2972 rp->proto, !is_delete, rp->twice_nat,
2973 rp->out2in_only, rp->tag, rp->identity_nat);
2975 nat_log_notice ("snat_add_static_mapping returned %d", rv);
2979 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2982 ip4_address_t * address,
2984 u32 if_address_index, u32 is_delete)
2986 snat_main_t *sm = &snat_main;
2987 snat_static_map_resolve_t *rp;
2988 ip4_address_t l_addr;
2992 snat_address_t *addresses = sm->addresses;
2994 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
2996 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3000 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
3003 addresses = sm->twice_nat_addresses;
3004 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3013 /* Don't trip over lease renewal, static config */
3014 for (j = 0; j < vec_len (addresses); j++)
3015 if (addresses[j].addr.as_u32 == address->as_u32)
3018 (void) snat_add_address (sm, address, ~0, twice_nat);
3019 /* Scan static map resolution vector */
3020 for (j = 0; j < vec_len (sm->to_resolve); j++)
3022 rp = sm->to_resolve + j;
3025 /* On this interface? */
3026 if (rp->sw_if_index == sw_if_index)
3028 /* Indetity mapping? */
3029 if (rp->l_addr.as_u32 == 0)
3030 l_addr.as_u32 = address[0].as_u32;
3032 l_addr.as_u32 = rp->l_addr.as_u32;
3033 /* Add the static mapping */
3034 rv = snat_add_static_mapping (l_addr,
3040 ~0 /* sw_if_index */ ,
3042 rp->is_add, rp->twice_nat,
3043 rp->out2in_only, rp->tag,
3046 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3053 (void) snat_del_address (sm, address[0], 1, twice_nat);
3060 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
3063 ip4_main_t *ip4_main = sm->ip4_main;
3064 ip4_address_t *first_int_addr;
3065 snat_static_map_resolve_t *rp;
3066 u32 *indices_to_delete = 0;
3068 u32 *auto_add_sw_if_indices =
3070 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3072 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
3075 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3077 if (auto_add_sw_if_indices[i] == sw_if_index)
3081 /* if have address remove it */
3083 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3086 for (j = 0; j < vec_len (sm->to_resolve); j++)
3088 rp = sm->to_resolve + j;
3089 if (rp->sw_if_index == sw_if_index)
3090 vec_add1 (indices_to_delete, j);
3092 if (vec_len (indices_to_delete))
3094 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3095 vec_del1 (sm->to_resolve, j);
3096 vec_free (indices_to_delete);
3100 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3102 vec_del1 (sm->auto_add_sw_if_indices, i);
3105 return VNET_API_ERROR_VALUE_EXIST;
3112 return VNET_API_ERROR_NO_SUCH_ENTRY;
3114 /* add to the auto-address list */
3116 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3118 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3120 /* If the address is already bound - or static - add it now */
3122 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3128 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3129 snat_protocol_t proto, u32 vrf_id, int is_in)
3131 snat_main_per_thread_data_t *tsm;
3132 clib_bihash_kv_8_8_t kv, value;
3134 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3135 snat_session_key_t key;
3137 clib_bihash_8_8_t *t;
3139 if (sm->endpoint_dependent)
3140 return VNET_API_ERROR_UNSUPPORTED;
3142 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3143 if (sm->num_workers > 1)
3145 vec_elt_at_index (sm->per_thread_data,
3146 sm->worker_in2out_cb (&ip, fib_index));
3148 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3150 key.addr.as_u32 = addr->as_u32;
3151 key.port = clib_host_to_net_u16 (port);
3152 key.protocol = proto;
3153 key.fib_index = fib_index;
3154 kv.key = key.as_u64;
3155 t = is_in ? &tsm->in2out : &tsm->out2in;
3156 if (!clib_bihash_search_8_8 (t, &kv, &value))
3158 if (pool_is_free_index (tsm->sessions, value.value))
3159 return VNET_API_ERROR_UNSPECIFIED;
3161 s = pool_elt_at_index (tsm->sessions, value.value);
3162 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3163 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3167 return VNET_API_ERROR_NO_SUCH_ENTRY;
3171 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3172 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3173 u32 vrf_id, int is_in)
3176 clib_bihash_16_8_t *t;
3177 nat_ed_ses_key_t key;
3178 clib_bihash_kv_16_8_t kv, value;
3179 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3181 snat_main_per_thread_data_t *tsm;
3183 if (!sm->endpoint_dependent)
3184 return VNET_API_ERROR_FEATURE_DISABLED;
3186 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3187 if (sm->num_workers > 1)
3189 vec_elt_at_index (sm->per_thread_data,
3190 sm->worker_in2out_cb (&ip, fib_index));
3192 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3194 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
3195 key.l_addr.as_u32 = addr->as_u32;
3196 key.r_addr.as_u32 = eh_addr->as_u32;
3197 key.l_port = clib_host_to_net_u16 (port);
3198 key.r_port = clib_host_to_net_u16 (eh_port);
3200 key.fib_index = fib_index;
3201 kv.key[0] = key.as_u64[0];
3202 kv.key[1] = key.as_u64[1];
3203 if (clib_bihash_search_16_8 (t, &kv, &value))
3204 return VNET_API_ERROR_NO_SUCH_ENTRY;
3206 if (pool_is_free_index (tsm->sessions, value.value))
3207 return VNET_API_ERROR_UNSPECIFIED;
3208 s = pool_elt_at_index (tsm->sessions, value.value);
3209 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3210 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3215 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3217 snat_main_t *sm = &snat_main;
3219 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
3220 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3222 sm->psid_offset = psid_offset;
3223 sm->psid_length = psid_length;
3227 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
3229 snat_main_t *sm = &snat_main;
3231 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
3232 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
3233 sm->start_port = start_port;
3234 sm->end_port = end_port;
3238 nat_set_alloc_addr_and_port_default (void)
3240 snat_main_t *sm = &snat_main;
3242 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
3243 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
3247 * fd.io coding-style-patch-verification: ON
3250 * eval: (c-set-style "gnu")