2 * snat.c - simple nat plugin
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
23 #include <nat/nat_dpo.h>
24 #include <nat/nat_ipfix_logging.h>
25 #include <nat/nat_det.h>
26 #include <nat/nat64.h>
27 #include <nat/nat66.h>
28 #include <nat/dslite.h>
29 #include <nat/nat_reass.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat_affinity.h>
32 #include <nat/nat_syslog.h>
33 #include <vnet/fib/fib_table.h>
34 #include <vnet/fib/ip4_fib.h>
36 #include <vpp/app/version.h>
38 snat_main_t snat_main;
42 /* Hook up input features */
43 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
44 .arc_name = "ip4-unicast",
45 .node_name = "nat44-in2out",
46 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
48 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
49 .arc_name = "ip4-unicast",
50 .node_name = "nat44-out2in",
51 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
52 "ip4-dhcp-client-detect"),
54 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
55 .arc_name = "ip4-unicast",
56 .node_name = "nat44-classify",
57 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
59 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
60 .arc_name = "ip4-unicast",
61 .node_name = "nat44-det-in2out",
62 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
64 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
65 .arc_name = "ip4-unicast",
66 .node_name = "nat44-det-out2in",
67 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
68 "ip4-dhcp-client-detect"),
70 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
71 .arc_name = "ip4-unicast",
72 .node_name = "nat44-det-classify",
73 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
75 VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = {
76 .arc_name = "ip4-unicast",
77 .node_name = "nat44-ed-in2out",
78 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
80 VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = {
81 .arc_name = "ip4-unicast",
82 .node_name = "nat44-ed-out2in",
83 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
84 "ip4-dhcp-client-detect"),
86 VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = {
87 .arc_name = "ip4-unicast",
88 .node_name = "nat44-ed-classify",
89 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
91 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
92 .arc_name = "ip4-unicast",
93 .node_name = "nat44-in2out-worker-handoff",
94 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
96 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
97 .arc_name = "ip4-unicast",
98 .node_name = "nat44-out2in-worker-handoff",
99 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
100 "ip4-dhcp-client-detect"),
102 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
103 .arc_name = "ip4-unicast",
104 .node_name = "nat44-handoff-classify",
105 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
107 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
108 .arc_name = "ip4-unicast",
109 .node_name = "nat44-in2out-fast",
110 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
112 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
113 .arc_name = "ip4-unicast",
114 .node_name = "nat44-out2in-fast",
115 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa",
116 "ip4-dhcp-client-detect"),
118 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
119 .arc_name = "ip4-unicast",
120 .node_name = "nat44-hairpin-dst",
121 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
123 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = {
124 .arc_name = "ip4-unicast",
125 .node_name = "nat44-ed-hairpin-dst",
126 .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"),
129 /* Hook up output features */
130 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
131 .arc_name = "ip4-output",
132 .node_name = "nat44-in2out-output",
133 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
135 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
136 .arc_name = "ip4-output",
137 .node_name = "nat44-in2out-output-worker-handoff",
138 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
140 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
141 .arc_name = "ip4-output",
142 .node_name = "nat44-hairpin-src",
143 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
145 VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = {
146 .arc_name = "ip4-output",
147 .node_name = "nat44-ed-in2out-output",
148 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
150 VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = {
151 .arc_name = "ip4-output",
152 .node_name = "nat44-ed-hairpin-src",
153 .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"),
156 /* Hook up ip4-local features */
157 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
159 .arc_name = "ip4-local",
160 .node_name = "nat44-hairpinning",
161 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
163 VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) =
165 .arc_name = "ip4-local",
166 .node_name = "nat44-ed-hairpinning",
167 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
171 VLIB_PLUGIN_REGISTER () = {
172 .version = VPP_BUILD_VER,
173 .description = "Network Address Translation",
178 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
180 snat_session_key_t key;
181 clib_bihash_kv_8_8_t kv;
182 nat_ed_ses_key_t ed_key;
183 clib_bihash_kv_16_8_t ed_kv;
184 snat_main_per_thread_data_t *tsm =
185 vec_elt_at_index (sm->per_thread_data, thread_index);
187 if (is_fwd_bypass_session (s))
189 ed_key.l_addr = s->in2out.addr;
190 ed_key.r_addr = s->ext_host_addr;
191 ed_key.l_port = s->in2out.port;
192 ed_key.r_port = s->ext_host_port;
193 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
194 ed_key.fib_index = 0;
195 ed_kv.key[0] = ed_key.as_u64[0];
196 ed_kv.key[1] = ed_key.as_u64[1];
197 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
198 nat_log_warn ("in2out_ed key del failed");
202 /* session lookup tables */
203 if (is_ed_session (s))
205 if (is_affinity_sessions (s))
206 nat_affinity_unlock (s->ext_host_addr, s->out2in.addr,
207 s->in2out.protocol, s->out2in.port);
208 ed_key.l_addr = s->out2in.addr;
209 ed_key.r_addr = s->ext_host_addr;
210 ed_key.fib_index = s->out2in.fib_index;
211 if (snat_is_unk_proto_session (s))
213 ed_key.proto = s->in2out.port;
219 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
220 ed_key.l_port = s->out2in.port;
221 ed_key.r_port = s->ext_host_port;
223 ed_kv.key[0] = ed_key.as_u64[0];
224 ed_kv.key[1] = ed_key.as_u64[1];
225 if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0))
226 nat_log_warn ("out2in_ed key del failed");
227 ed_key.l_addr = s->in2out.addr;
228 ed_key.fib_index = s->in2out.fib_index;
229 if (!snat_is_unk_proto_session (s))
230 ed_key.l_port = s->in2out.port;
231 if (is_twice_nat_session (s))
233 ed_key.r_addr = s->ext_host_nat_addr;
234 ed_key.r_port = s->ext_host_nat_port;
236 ed_kv.key[0] = ed_key.as_u64[0];
237 ed_kv.key[1] = ed_key.as_u64[1];
238 if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
239 nat_log_warn ("in2out_ed key del failed");
241 nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
242 &s->in2out.addr, s->in2out.port,
243 &s->ext_host_nat_addr, s->ext_host_nat_port,
244 &s->out2in.addr, s->out2in.port,
245 &s->ext_host_addr, s->ext_host_port,
246 s->in2out.protocol, is_twice_nat_session (s));
250 kv.key = s->in2out.as_u64;
251 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
252 nat_log_warn ("in2out key del failed");
253 kv.key = s->out2in.as_u64;
254 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
255 nat_log_warn ("out2in key del failed");
257 nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index,
258 &s->in2out.addr, s->in2out.port,
259 &s->out2in.addr, s->out2in.port,
263 if (snat_is_unk_proto_session (s))
267 snat_ipfix_logging_nat44_ses_delete (thread_index,
268 s->in2out.addr.as_u32,
269 s->out2in.addr.as_u32,
272 s->out2in.port, s->in2out.fib_index);
274 /* Twice NAT address and port for external host */
275 if (is_twice_nat_session (s))
277 key.protocol = s->in2out.protocol;
278 key.port = s->ext_host_nat_port;
279 key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
280 snat_free_outside_address_and_port (sm->twice_nat_addresses,
284 if (snat_is_session_static (s))
287 snat_free_outside_address_and_port (sm->addresses, thread_index,
292 nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index,
296 snat_user_key_t user_key;
297 clib_bihash_kv_8_8_t kv, value;
298 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
299 dlist_elt_t *per_user_list_head_elt;
301 user_key.addr.as_u32 = addr->as_u32;
302 user_key.fib_index = fib_index;
303 kv.key = user_key.as_u64;
305 /* Ever heard of the "user" = src ip4 address before? */
306 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
308 /* no, make a new one */
309 pool_get (tsm->users, u);
310 clib_memset (u, 0, sizeof (*u));
311 u->addr.as_u32 = addr->as_u32;
312 u->fib_index = fib_index;
314 pool_get (tsm->list_pool, per_user_list_head_elt);
316 u->sessions_per_user_list_head_index = per_user_list_head_elt -
319 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
321 kv.value = u - tsm->users;
324 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
325 nat_log_warn ("user_hash keay add failed");
327 vlib_set_simple_counter (&sm->total_users, thread_index, 0,
328 pool_elts (tsm->users));
332 u = pool_elt_at_index (tsm->users, value.value);
339 nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u,
343 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
344 u32 oldest_per_user_translation_list_index, session_index;
345 dlist_elt_t *oldest_per_user_translation_list_elt;
346 dlist_elt_t *per_user_translation_list_elt;
348 /* Over quota? Recycle the least recently used translation */
349 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
351 oldest_per_user_translation_list_index =
352 clib_dlist_remove_head (tsm->list_pool,
353 u->sessions_per_user_list_head_index);
355 ASSERT (oldest_per_user_translation_list_index != ~0);
357 /* Add it back to the end of the LRU list */
358 clib_dlist_addtail (tsm->list_pool,
359 u->sessions_per_user_list_head_index,
360 oldest_per_user_translation_list_index);
361 /* Get the list element */
362 oldest_per_user_translation_list_elt =
363 pool_elt_at_index (tsm->list_pool,
364 oldest_per_user_translation_list_index);
366 /* Get the session index from the list element */
367 session_index = oldest_per_user_translation_list_elt->value;
369 /* Get the session */
370 s = pool_elt_at_index (tsm->sessions, session_index);
371 nat_free_session_data (sm, s, thread_index);
372 if (snat_is_session_static (s))
373 u->nstaticsessions--;
380 s->ext_host_addr.as_u32 = 0;
381 s->ext_host_port = 0;
382 s->ext_host_nat_addr.as_u32 = 0;
383 s->ext_host_nat_port = 0;
387 pool_get (tsm->sessions, s);
388 clib_memset (s, 0, sizeof (*s));
390 /* Create list elts */
391 pool_get (tsm->list_pool, per_user_translation_list_elt);
392 clib_dlist_init (tsm->list_pool,
393 per_user_translation_list_elt - tsm->list_pool);
395 per_user_translation_list_elt->value = s - tsm->sessions;
396 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
397 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
399 clib_dlist_addtail (tsm->list_pool,
400 s->per_user_list_head_index,
401 per_user_translation_list_elt - tsm->list_pool);
403 s->user_index = u - tsm->users;
404 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
405 pool_elts (tsm->sessions));
412 nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u, u32 thread_index,
416 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
417 dlist_elt_t *per_user_translation_list_elt, *oldest_elt;
419 u64 sess_timeout_time;
421 if (PREDICT_FALSE (!(u->nsessions) && !(u->nstaticsessions)))
425 clib_dlist_remove_head (tsm->list_pool,
426 u->sessions_per_user_list_head_index);
427 oldest_elt = pool_elt_at_index (tsm->list_pool, oldest_index);
428 s = pool_elt_at_index (tsm->sessions, oldest_elt->value);
429 sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
430 if (now >= sess_timeout_time)
432 clib_dlist_addtail (tsm->list_pool,
433 u->sessions_per_user_list_head_index, oldest_index);
434 nat_free_session_data (sm, s, thread_index);
435 if (snat_is_session_static (s))
436 u->nstaticsessions--;
443 s->ext_host_addr.as_u32 = 0;
444 s->ext_host_port = 0;
445 s->ext_host_nat_addr.as_u32 = 0;
446 s->ext_host_nat_port = 0;
450 clib_dlist_addhead (tsm->list_pool,
451 u->sessions_per_user_list_head_index, oldest_index);
452 if ((u->nsessions + u->nstaticsessions) >=
453 sm->max_translations_per_user)
455 nat_log_warn ("max translations per user %U", format_ip4_address,
457 snat_ipfix_logging_max_entries_per_user
458 (thread_index, sm->max_translations_per_user, u->addr.as_u32);
464 pool_get (tsm->sessions, s);
465 clib_memset (s, 0, sizeof (*s));
467 /* Create list elts */
468 pool_get (tsm->list_pool, per_user_translation_list_elt);
469 clib_dlist_init (tsm->list_pool,
470 per_user_translation_list_elt - tsm->list_pool);
472 per_user_translation_list_elt->value = s - tsm->sessions;
473 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
474 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
476 clib_dlist_addtail (tsm->list_pool,
477 s->per_user_list_head_index,
478 per_user_translation_list_elt - tsm->list_pool);
481 vlib_set_simple_counter (&sm->total_sessions, thread_index, 0,
482 pool_elts (tsm->sessions));
489 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
492 fib_prefix_t prefix = {
494 .fp_proto = FIB_PROTOCOL_IP4,
496 .ip4.as_u32 = addr->as_u32,
499 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
502 fib_table_entry_update_one_path (fib_index,
504 FIB_SOURCE_PLUGIN_LOW,
505 (FIB_ENTRY_FLAG_CONNECTED |
506 FIB_ENTRY_FLAG_LOCAL |
507 FIB_ENTRY_FLAG_EXCLUSIVE),
511 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
513 fib_table_entry_delete (fib_index, &prefix, FIB_SOURCE_PLUGIN_LOW);
517 snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id,
522 vlib_thread_main_t *tm = vlib_get_thread_main ();
524 if (twice_nat && !sm->endpoint_dependent)
525 return VNET_API_ERROR_FEATURE_DISABLED;
527 /* Check if address already exists */
529 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
531 if (ap->addr.as_u32 == addr->as_u32)
532 return VNET_API_ERROR_VALUE_EXIST;
537 vec_add2 (sm->twice_nat_addresses, ap, 1);
539 vec_add2 (sm->addresses, ap, 1);
544 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
545 FIB_SOURCE_PLUGIN_LOW);
548 #define _(N, i, n, s) \
549 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
550 ap->busy_##n##_ports = 0; \
551 ap->busy_##n##_ports_per_thread = 0;\
552 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
553 foreach_snat_protocol
558 /* Add external address to FIB */
560 pool_foreach (i, sm->interfaces,
562 if (nat_interface_is_inside(i) || sm->out2in_dpo)
565 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
568 pool_foreach (i, sm->output_feature_interfaces,
570 if (nat_interface_is_inside(i) || sm->out2in_dpo)
573 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
582 is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr)
584 snat_static_mapping_t *m;
586 pool_foreach (m, sm->static_mappings,
588 if (is_addr_only_static_mapping (m) ||
589 is_out2in_only_static_mapping (m) ||
590 is_identity_static_mapping (m))
592 if (m->external_addr.as_u32 == addr.as_u32)
601 increment_v4_address (ip4_address_t * a)
605 v = clib_net_to_host_u32 (a->as_u32) + 1;
606 a->as_u32 = clib_host_to_net_u32 (v);
610 snat_add_static_mapping_when_resolved (snat_main_t * sm,
611 ip4_address_t l_addr,
616 snat_protocol_t proto,
617 int addr_only, int is_add, u8 * tag,
618 int twice_nat, int out2in_only,
621 snat_static_map_resolve_t *rp;
623 vec_add2 (sm->to_resolve, rp, 1);
624 rp->l_addr.as_u32 = l_addr.as_u32;
626 rp->sw_if_index = sw_if_index;
630 rp->addr_only = addr_only;
632 rp->twice_nat = twice_nat;
633 rp->out2in_only = out2in_only;
634 rp->identity_nat = identity_nat;
635 rp->tag = vec_dup (tag);
639 get_thread_idx_by_port (u16 e_port)
641 snat_main_t *sm = &snat_main;
642 u32 thread_idx = sm->num_workers;
643 if (sm->num_workers > 1)
646 sm->first_worker_index +
647 sm->workers[(e_port - 1024) / sm->port_per_thread];
653 snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
654 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
655 u32 sw_if_index, snat_protocol_t proto, int is_add,
656 twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag,
659 snat_main_t *sm = &snat_main;
660 snat_static_mapping_t *m;
661 snat_session_key_t m_key;
662 clib_bihash_kv_8_8_t kv, value;
663 snat_address_t *a = 0;
665 snat_interface_t *interface;
667 snat_main_per_thread_data_t *tsm;
668 snat_user_key_t u_key;
670 dlist_elt_t *head, *elt;
671 u32 elt_index, head_index;
675 snat_static_map_resolve_t *rp, *rp_match = 0;
676 nat44_lb_addr_port_t *local;
679 if (!sm->endpoint_dependent)
681 if (twice_nat || out2in_only)
682 return VNET_API_ERROR_FEATURE_DISABLED;
685 /* If the external address is a specific interface address */
686 if (sw_if_index != ~0)
688 ip4_address_t *first_int_addr;
690 for (i = 0; i < vec_len (sm->to_resolve); i++)
692 rp = sm->to_resolve + i;
693 if (rp->sw_if_index != sw_if_index ||
694 rp->l_addr.as_u32 != l_addr.as_u32 ||
695 rp->vrf_id != vrf_id || rp->addr_only != addr_only)
700 if (rp->l_port != l_port || rp->e_port != e_port
701 || rp->proto != proto)
709 /* Might be already set... */
710 first_int_addr = ip4_interface_first_address
711 (sm->ip4_main, sw_if_index, 0 /* just want the address */ );
716 return VNET_API_ERROR_VALUE_EXIST;
718 snat_add_static_mapping_when_resolved
719 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
720 addr_only, is_add, tag, twice_nat, out2in_only, identity_nat);
722 /* DHCP resolution required? */
723 if (first_int_addr == 0)
729 e_addr.as_u32 = first_int_addr->as_u32;
730 /* Identity mapping? */
731 if (l_addr.as_u32 == 0)
732 l_addr.as_u32 = e_addr.as_u32;
738 return VNET_API_ERROR_NO_SUCH_ENTRY;
740 vec_del1 (sm->to_resolve, i);
744 e_addr.as_u32 = first_int_addr->as_u32;
745 /* Identity mapping? */
746 if (l_addr.as_u32 == 0)
747 l_addr.as_u32 = e_addr.as_u32;
755 m_key.port = addr_only ? 0 : e_port;
756 m_key.protocol = addr_only ? 0 : proto;
758 kv.key = m_key.as_u64;
759 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
762 m = pool_elt_at_index (sm->static_mappings, value.value);
768 if (is_identity_static_mapping (m))
771 pool_foreach (local, m->locals,
773 if (local->vrf_id == vrf_id)
774 return VNET_API_ERROR_VALUE_EXIST;
777 pool_get (m->locals, local);
778 local->vrf_id = vrf_id;
780 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
781 FIB_SOURCE_PLUGIN_LOW);
782 m_key.addr = m->local_addr;
783 m_key.port = m->local_port;
784 m_key.protocol = m->proto;
785 m_key.fib_index = local->fib_index;
786 kv.key = m_key.as_u64;
787 kv.value = m - sm->static_mappings;
788 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
792 return VNET_API_ERROR_VALUE_EXIST;
795 if (twice_nat && addr_only)
796 return VNET_API_ERROR_UNSUPPORTED;
798 /* Convert VRF id to FIB index */
801 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
802 FIB_SOURCE_PLUGIN_LOW);
803 /* If not specified use inside VRF id from SNAT plugin startup config */
806 fib_index = sm->inside_fib_index;
807 vrf_id = sm->inside_vrf_id;
810 if (!(out2in_only || identity_nat))
813 m_key.port = addr_only ? 0 : l_port;
814 m_key.protocol = addr_only ? 0 : proto;
815 m_key.fib_index = fib_index;
816 kv.key = m_key.as_u64;
817 if (!clib_bihash_search_8_8
818 (&sm->static_mapping_by_local, &kv, &value))
819 return VNET_API_ERROR_VALUE_EXIST;
822 /* Find external address in allocated addresses and reserve port for
823 address and port pair mapping when dynamic translations enabled */
824 if (!(addr_only || sm->static_mapping_only || out2in_only))
826 for (i = 0; i < vec_len (sm->addresses); i++)
828 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
830 a = sm->addresses + i;
831 /* External port must be unused */
834 #define _(N, j, n, s) \
835 case SNAT_PROTOCOL_##N: \
836 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
837 return VNET_API_ERROR_INVALID_VALUE; \
838 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
841 a->busy_##n##_ports++; \
842 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
845 foreach_snat_protocol
848 nat_log_info ("unknown protocol");
849 return VNET_API_ERROR_INVALID_VALUE_2;
854 /* External address must be allocated */
855 if (!a && (l_addr.as_u32 != e_addr.as_u32))
857 if (sw_if_index != ~0)
859 for (i = 0; i < vec_len (sm->to_resolve); i++)
861 rp = sm->to_resolve + i;
864 if (rp->sw_if_index != sw_if_index &&
865 rp->l_addr.as_u32 != l_addr.as_u32 &&
866 rp->vrf_id != vrf_id && rp->l_port != l_port &&
867 rp->e_port != e_port && rp->proto != proto)
870 vec_del1 (sm->to_resolve, i);
874 return VNET_API_ERROR_NO_SUCH_ENTRY;
878 pool_get (sm->static_mappings, m);
879 clib_memset (m, 0, sizeof (*m));
880 m->tag = vec_dup (tag);
881 m->local_addr = l_addr;
882 m->external_addr = e_addr;
883 m->twice_nat = twice_nat;
885 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
887 m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY;
890 m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT;
891 pool_get (m->locals, local);
892 local->vrf_id = vrf_id;
893 local->fib_index = fib_index;
898 m->fib_index = fib_index;
902 m->local_port = l_port;
903 m->external_port = e_port;
907 if (sm->num_workers > 1)
910 .src_address = m->local_addr,
912 vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index));
913 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
916 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
918 m_key.addr = m->local_addr;
919 m_key.port = m->local_port;
920 m_key.protocol = m->proto;
921 m_key.fib_index = fib_index;
922 kv.key = m_key.as_u64;
923 kv.value = m - sm->static_mappings;
925 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
927 m_key.addr = m->external_addr;
928 m_key.port = m->external_port;
930 kv.key = m_key.as_u64;
931 kv.value = m - sm->static_mappings;
932 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1);
934 /* Delete dynamic sessions matching local address (+ local port) */
935 if (!(sm->static_mapping_only))
937 u_key.addr = m->local_addr;
938 u_key.fib_index = m->fib_index;
939 kv.key = u_key.as_u64;
940 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
942 user_index = value.value;
943 u = pool_elt_at_index (tsm->users, user_index);
946 head_index = u->sessions_per_user_list_head_index;
947 head = pool_elt_at_index (tsm->list_pool, head_index);
948 elt_index = head->next;
949 elt = pool_elt_at_index (tsm->list_pool, elt_index);
950 ses_index = elt->value;
951 while (ses_index != ~0)
953 s = pool_elt_at_index (tsm->sessions, ses_index);
954 elt = pool_elt_at_index (tsm->list_pool, elt->next);
955 ses_index = elt->value;
957 if (snat_is_session_static (s))
961 && (clib_net_to_host_u16 (s->in2out.port) !=
965 nat_free_session_data (sm, s,
966 tsm - sm->per_thread_data);
967 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
969 if (!addr_only && !sm->endpoint_dependent)
980 if (sw_if_index != ~0)
983 return VNET_API_ERROR_NO_SUCH_ENTRY;
989 vrf_id = sm->inside_vrf_id;
992 pool_foreach (local, m->locals,
994 if (local->vrf_id == vrf_id)
995 find = local - m->locals;
999 return VNET_API_ERROR_NO_SUCH_ENTRY;
1001 local = pool_elt_at_index (m->locals, find);
1002 fib_index = local->fib_index;
1003 pool_put (m->locals, local);
1006 fib_index = m->fib_index;
1008 /* Free external address port */
1009 if (!(addr_only || sm->static_mapping_only || out2in_only))
1011 for (i = 0; i < vec_len (sm->addresses); i++)
1013 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1015 a = sm->addresses + i;
1018 #define _(N, j, n, s) \
1019 case SNAT_PROTOCOL_##N: \
1020 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1021 if (e_port > 1024) \
1023 a->busy_##n##_ports--; \
1024 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1027 foreach_snat_protocol
1030 nat_log_info ("unknown protocol");
1031 return VNET_API_ERROR_INVALID_VALUE_2;
1038 if (sm->num_workers > 1)
1039 tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]);
1041 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1043 m_key.addr = m->local_addr;
1044 m_key.port = m->local_port;
1045 m_key.protocol = m->proto;
1046 m_key.fib_index = fib_index;
1047 kv.key = m_key.as_u64;
1049 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0);
1051 /* Delete session(s) for static mapping if exist */
1052 if (!(sm->static_mapping_only) ||
1053 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
1055 u_key.addr = m->local_addr;
1056 u_key.fib_index = fib_index;
1057 kv.key = u_key.as_u64;
1058 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1060 user_index = value.value;
1061 u = pool_elt_at_index (tsm->users, user_index);
1062 if (u->nstaticsessions)
1064 head_index = u->sessions_per_user_list_head_index;
1065 head = pool_elt_at_index (tsm->list_pool, head_index);
1066 elt_index = head->next;
1067 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1068 ses_index = elt->value;
1069 while (ses_index != ~0)
1071 s = pool_elt_at_index (tsm->sessions, ses_index);
1072 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1073 ses_index = elt->value;
1077 if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
1078 (clib_net_to_host_u16 (s->out2in.port) !=
1083 if (is_lb_session (s))
1086 if (!snat_is_session_static (s))
1089 nat_free_session_data (sm, s,
1090 tsm - sm->per_thread_data);
1091 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1093 if (!addr_only && !sm->endpoint_dependent)
1100 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1101 if (pool_elts (m->locals))
1104 m_key.addr = m->external_addr;
1105 m_key.port = m->external_port;
1106 m_key.fib_index = 0;
1107 kv.key = m_key.as_u64;
1108 clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0);
1111 vec_free (m->workers);
1112 /* Delete static mapping from pool */
1113 pool_put (sm->static_mappings, m);
1116 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1119 /* Add/delete external address to FIB */
1121 pool_foreach (interface, sm->interfaces,
1123 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1126 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1129 pool_foreach (interface, sm->output_feature_interfaces,
1131 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1134 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1143 nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1144 snat_protocol_t proto,
1145 nat44_lb_addr_port_t * locals, u8 is_add,
1146 twice_nat_type_t twice_nat, u8 out2in_only,
1147 u8 * tag, u32 affinity)
1149 snat_main_t *sm = &snat_main;
1150 snat_static_mapping_t *m;
1151 snat_session_key_t m_key;
1152 clib_bihash_kv_8_8_t kv, value;
1153 snat_address_t *a = 0;
1155 nat44_lb_addr_port_t *local;
1156 u32 elt_index, head_index, ses_index;
1157 snat_main_per_thread_data_t *tsm;
1158 snat_user_key_t u_key;
1161 dlist_elt_t *head, *elt;
1164 if (!sm->endpoint_dependent)
1165 return VNET_API_ERROR_FEATURE_DISABLED;
1167 m_key.addr = e_addr;
1168 m_key.port = e_port;
1169 m_key.protocol = proto;
1170 m_key.fib_index = 0;
1171 kv.key = m_key.as_u64;
1172 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1175 m = pool_elt_at_index (sm->static_mappings, value.value);
1180 return VNET_API_ERROR_VALUE_EXIST;
1182 if (vec_len (locals) < 2)
1183 return VNET_API_ERROR_INVALID_VALUE;
1185 /* Find external address in allocated addresses and reserve port for
1186 address and port pair mapping when dynamic translations enabled */
1187 if (!(sm->static_mapping_only || out2in_only))
1189 for (i = 0; i < vec_len (sm->addresses); i++)
1191 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1193 a = sm->addresses + i;
1194 /* External port must be unused */
1197 #define _(N, j, n, s) \
1198 case SNAT_PROTOCOL_##N: \
1199 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1200 return VNET_API_ERROR_INVALID_VALUE; \
1201 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1202 if (e_port > 1024) \
1204 a->busy_##n##_ports++; \
1205 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \
1208 foreach_snat_protocol
1211 nat_log_info ("unknown protocol");
1212 return VNET_API_ERROR_INVALID_VALUE_2;
1217 /* External address must be allocated */
1219 return VNET_API_ERROR_NO_SUCH_ENTRY;
1222 pool_get (sm->static_mappings, m);
1223 clib_memset (m, 0, sizeof (*m));
1224 m->tag = vec_dup (tag);
1225 m->external_addr = e_addr;
1226 m->external_port = e_port;
1228 m->twice_nat = twice_nat;
1229 m->flags |= NAT_STATIC_MAPPING_FLAG_LB;
1231 m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY;
1232 m->affinity = affinity;
1235 m->affinity_per_service_list_head_index =
1236 nat_affinity_get_per_service_list_head_index ();
1238 m->affinity_per_service_list_head_index = ~0;
1240 m_key.addr = m->external_addr;
1241 m_key.port = m->external_port;
1242 m_key.protocol = m->proto;
1243 m_key.fib_index = 0;
1244 kv.key = m_key.as_u64;
1245 kv.value = m - sm->static_mappings;
1246 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1))
1248 nat_log_err ("static_mapping_by_external key add failed");
1249 return VNET_API_ERROR_UNSPECIFIED;
1252 m_key.fib_index = m->fib_index;
1253 for (i = 0; i < vec_len (locals); i++)
1255 locals[i].fib_index =
1256 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1258 FIB_SOURCE_PLUGIN_LOW);
1259 m_key.addr = locals[i].addr;
1260 m_key.fib_index = locals[i].fib_index;
1263 m_key.port = locals[i].port;
1264 kv.key = m_key.as_u64;
1265 kv.value = m - sm->static_mappings;
1266 clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1);
1268 locals[i].prefix = (i == 0) ? locals[i].probability :
1269 (locals[i - 1].prefix + locals[i].probability);
1270 pool_get (m->locals, local);
1272 if (sm->num_workers > 1)
1275 .src_address = locals[i].addr,
1278 clib_bitmap_set (bitmap,
1279 sm->worker_in2out_cb (&ip, m->fib_index), 1);
1283 /* Assign workers */
1284 if (sm->num_workers > 1)
1287 clib_bitmap_foreach (i, bitmap,
1289 vec_add1(m->workers, i);
1297 return VNET_API_ERROR_NO_SUCH_ENTRY;
1299 if (!is_lb_static_mapping (m))
1300 return VNET_API_ERROR_INVALID_VALUE;
1302 /* Free external address port */
1303 if (!(sm->static_mapping_only || out2in_only))
1305 for (i = 0; i < vec_len (sm->addresses); i++)
1307 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1309 a = sm->addresses + i;
1312 #define _(N, j, n, s) \
1313 case SNAT_PROTOCOL_##N: \
1314 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1315 if (e_port > 1024) \
1317 a->busy_##n##_ports--; \
1318 a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \
1321 foreach_snat_protocol
1324 nat_log_info ("unknown protocol");
1325 return VNET_API_ERROR_INVALID_VALUE_2;
1332 m_key.addr = m->external_addr;
1333 m_key.port = m->external_port;
1334 m_key.protocol = m->proto;
1335 m_key.fib_index = 0;
1336 kv.key = m_key.as_u64;
1337 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0))
1339 nat_log_err ("static_mapping_by_external key del failed");
1340 return VNET_API_ERROR_UNSPECIFIED;
1344 pool_foreach (local, m->locals,
1346 fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4,
1347 FIB_SOURCE_PLUGIN_LOW);
1348 m_key.addr = local->addr;
1351 m_key.port = local->port;
1352 m_key.fib_index = local->fib_index;
1353 kv.key = m_key.as_u64;
1354 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1356 nat_log_err ("static_mapping_by_local key del failed");
1357 return VNET_API_ERROR_UNSPECIFIED;
1361 if (sm->num_workers > 1)
1364 .src_address = local->addr,
1366 tsm = vec_elt_at_index (sm->per_thread_data,
1367 sm->worker_in2out_cb (&ip, m->fib_index));
1370 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1372 /* Delete sessions */
1373 u_key.addr = local->addr;
1374 u_key.fib_index = local->fib_index;
1375 kv.key = u_key.as_u64;
1376 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1378 u = pool_elt_at_index (tsm->users, value.value);
1379 if (u->nstaticsessions)
1381 head_index = u->sessions_per_user_list_head_index;
1382 head = pool_elt_at_index (tsm->list_pool, head_index);
1383 elt_index = head->next;
1384 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1385 ses_index = elt->value;
1386 while (ses_index != ~0)
1388 s = pool_elt_at_index (tsm->sessions, ses_index);
1389 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1390 ses_index = elt->value;
1392 if (!(is_lb_session (s)))
1395 if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
1396 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1399 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1400 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1407 nat_affinity_flush_service (m->affinity_per_service_list_head_index);
1408 pool_free (m->locals);
1410 vec_free (m->workers);
1412 pool_put (sm->static_mappings, m);
1419 nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port,
1420 ip4_address_t l_addr, u16 l_port,
1421 snat_protocol_t proto, u32 vrf_id,
1422 u8 probability, u8 is_add)
1424 snat_main_t *sm = &snat_main;
1425 snat_static_mapping_t *m = 0;
1426 snat_session_key_t m_key;
1427 clib_bihash_kv_8_8_t kv, value;
1428 nat44_lb_addr_port_t *local, *prev_local, *match_local = 0;
1429 snat_main_per_thread_data_t *tsm;
1430 snat_user_key_t u_key;
1433 dlist_elt_t *head, *elt;
1434 u32 elt_index, head_index, ses_index, *locals = 0;
1438 if (!sm->endpoint_dependent)
1439 return VNET_API_ERROR_FEATURE_DISABLED;
1441 m_key.addr = e_addr;
1442 m_key.port = e_port;
1443 m_key.protocol = proto;
1444 m_key.fib_index = 0;
1445 kv.key = m_key.as_u64;
1446 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1447 m = pool_elt_at_index (sm->static_mappings, value.value);
1450 return VNET_API_ERROR_NO_SUCH_ENTRY;
1452 if (!is_lb_static_mapping (m))
1453 return VNET_API_ERROR_INVALID_VALUE;
1456 pool_foreach (local, m->locals,
1458 if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) &&
1459 (local->vrf_id == vrf_id))
1461 match_local = local;
1470 return VNET_API_ERROR_VALUE_EXIST;
1472 pool_get (m->locals, local);
1473 clib_memset (local, 0, sizeof (*local));
1474 local->addr.as_u32 = l_addr.as_u32;
1475 local->port = l_port;
1476 local->probability = probability;
1477 local->vrf_id = vrf_id;
1479 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1480 FIB_SOURCE_PLUGIN_LOW);
1482 if (!is_out2in_only_static_mapping (m))
1484 m_key.addr = l_addr;
1485 m_key.port = l_port;
1486 m_key.fib_index = local->fib_index;
1487 kv.key = m_key.as_u64;
1488 kv.value = m - sm->static_mappings;
1489 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1))
1490 nat_log_err ("static_mapping_by_local key add failed");
1496 return VNET_API_ERROR_NO_SUCH_ENTRY;
1498 if (pool_elts (m->locals) < 3)
1499 return VNET_API_ERROR_UNSPECIFIED;
1501 fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4,
1502 FIB_SOURCE_PLUGIN_LOW);
1504 if (!is_out2in_only_static_mapping (m))
1506 m_key.addr = l_addr;
1507 m_key.port = l_port;
1508 m_key.fib_index = match_local->fib_index;
1509 kv.key = m_key.as_u64;
1510 if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0))
1511 nat_log_err ("static_mapping_by_local key del failed");
1514 if (sm->num_workers > 1)
1517 .src_address = local->addr,
1519 tsm = vec_elt_at_index (sm->per_thread_data,
1520 sm->worker_in2out_cb (&ip, m->fib_index));
1523 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1525 /* Delete sessions */
1526 u_key.addr = match_local->addr;
1527 u_key.fib_index = match_local->fib_index;
1528 kv.key = u_key.as_u64;
1529 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1531 u = pool_elt_at_index (tsm->users, value.value);
1532 if (u->nstaticsessions)
1534 head_index = u->sessions_per_user_list_head_index;
1535 head = pool_elt_at_index (tsm->list_pool, head_index);
1536 elt_index = head->next;
1537 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1538 ses_index = elt->value;
1539 while (ses_index != ~0)
1541 s = pool_elt_at_index (tsm->sessions, ses_index);
1542 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1543 ses_index = elt->value;
1545 if (!(is_lb_session (s)))
1548 if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) ||
1549 (clib_net_to_host_u16 (s->in2out.port) !=
1553 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1554 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1559 pool_put (m->locals, match_local);
1562 vec_free (m->workers);
1565 pool_foreach (local, m->locals,
1567 vec_add1 (locals, local - m->locals);
1568 if (sm->num_workers > 1)
1571 ip.src_address.as_u32 = local->addr.as_u32,
1572 bitmap = clib_bitmap_set (bitmap,
1573 sm->worker_in2out_cb (&ip, local->fib_index),
1579 ASSERT (vec_len (locals) > 1);
1581 local = pool_elt_at_index (m->locals, locals[0]);
1582 local->prefix = local->probability;
1583 for (i = 1; i < vec_len (locals); i++)
1585 local = pool_elt_at_index (m->locals, locals[i]);
1586 prev_local = pool_elt_at_index (m->locals, locals[i - 1]);
1587 local->prefix = local->probability + prev_local->prefix;
1590 /* Assign workers */
1591 if (sm->num_workers > 1)
1594 clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); }));
1602 snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm,
1605 snat_address_t *a = 0;
1606 snat_session_t *ses;
1607 u32 *ses_to_be_removed = 0, *ses_index;
1608 snat_main_per_thread_data_t *tsm;
1609 snat_static_mapping_t *m;
1610 snat_interface_t *interface;
1612 snat_address_t *addresses =
1613 twice_nat ? sm->twice_nat_addresses : sm->addresses;
1615 /* Find SNAT address */
1616 for (i = 0; i < vec_len (addresses); i++)
1618 if (addresses[i].addr.as_u32 == addr.as_u32)
1625 return VNET_API_ERROR_NO_SUCH_ENTRY;
1630 pool_foreach (m, sm->static_mappings,
1632 if (m->external_addr.as_u32 == addr.as_u32)
1633 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1634 m->local_port, m->external_port,
1635 m->vrf_id, is_addr_only_static_mapping(m), ~0,
1636 m->proto, 0, m->twice_nat,
1637 is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m));
1643 /* Check if address is used in some static mapping */
1644 if (is_snat_address_used_in_static_mapping (sm, addr))
1646 nat_log_notice ("address used in static mapping");
1647 return VNET_API_ERROR_UNSPECIFIED;
1651 if (a->fib_index != ~0)
1652 fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW);
1654 /* Delete sessions using address */
1655 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1658 vec_foreach (tsm, sm->per_thread_data)
1660 pool_foreach (ses, tsm->sessions, ({
1661 if (ses->out2in.addr.as_u32 == addr.as_u32)
1663 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1664 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1668 vec_foreach (ses_index, ses_to_be_removed)
1670 ses = pool_elt_at_index (tsm->sessions, ses_index[0]);
1671 nat44_delete_session (sm, ses, tsm - sm->per_thread_data);
1674 vec_free (ses_to_be_removed);
1679 #define _(N, i, n, s) \
1680 clib_bitmap_free (a->busy_##n##_port_bitmap); \
1681 vec_free (a->busy_##n##_ports_per_thread);
1682 foreach_snat_protocol
1686 vec_del1 (sm->twice_nat_addresses, i);
1690 vec_del1 (sm->addresses, i);
1692 /* Delete external address from FIB */
1694 pool_foreach (interface, sm->interfaces,
1696 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1699 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1702 pool_foreach (interface, sm->output_feature_interfaces,
1704 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1707 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1716 snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1718 snat_main_t *sm = &snat_main;
1719 snat_interface_t *i;
1720 const char *feature_name, *del_feature_name;
1722 snat_static_mapping_t *m;
1724 nat_outside_fib_t *outside_fib;
1725 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1728 if (sm->out2in_dpo && !is_inside)
1729 return VNET_API_ERROR_UNSUPPORTED;
1732 pool_foreach (i, sm->output_feature_interfaces,
1734 if (i->sw_if_index == sw_if_index)
1735 return VNET_API_ERROR_VALUE_EXIST;
1739 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1740 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1743 if (sm->num_workers > 1 && !sm->deterministic)
1745 is_inside ? "nat44-in2out-worker-handoff" :
1746 "nat44-out2in-worker-handoff";
1747 else if (sm->deterministic)
1748 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1749 else if (sm->endpoint_dependent)
1750 feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in";
1752 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1755 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1756 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index,
1759 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1760 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index,
1766 vec_foreach (outside_fib, sm->outside_fibs)
1768 if (outside_fib->fib_index == fib_index)
1772 outside_fib->refcount--;
1773 if (!outside_fib->refcount)
1774 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
1777 outside_fib->refcount++;
1784 vec_add2 (sm->outside_fibs, outside_fib, 1);
1785 outside_fib->refcount = 1;
1786 outside_fib->fib_index = fib_index;
1791 pool_foreach (i, sm->interfaces,
1793 if (i->sw_if_index == sw_if_index)
1797 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1800 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1802 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1804 if (sm->num_workers > 1 && !sm->deterministic)
1806 del_feature_name = "nat44-handoff-classify";
1807 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1808 "nat44-out2in-worker-handoff";
1810 else if (sm->deterministic)
1812 del_feature_name = "nat44-det-classify";
1813 feature_name = !is_inside ? "nat44-det-in2out" :
1816 else if (sm->endpoint_dependent)
1818 del_feature_name = "nat44-ed-classify";
1819 feature_name = !is_inside ? "nat44-ed-in2out" :
1824 del_feature_name = "nat44-classify";
1825 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1828 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1829 sw_if_index, 0, 0, 0);
1830 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1831 sw_if_index, 1, 0, 0);
1834 if (sm->endpoint_dependent)
1835 vnet_feature_enable_disable ("ip4-local",
1836 "nat44-ed-hairpinning",
1837 sw_if_index, 1, 0, 0);
1838 else if (!sm->deterministic)
1839 vnet_feature_enable_disable ("ip4-local",
1840 "nat44-hairpinning",
1841 sw_if_index, 1, 0, 0);
1846 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1847 sw_if_index, 0, 0, 0);
1848 pool_put (sm->interfaces, i);
1851 if (sm->endpoint_dependent)
1852 vnet_feature_enable_disable ("ip4-local",
1853 "nat44-ed-hairpinning",
1854 sw_if_index, 0, 0, 0);
1855 else if (!sm->deterministic)
1856 vnet_feature_enable_disable ("ip4-local",
1857 "nat44-hairpinning",
1858 sw_if_index, 0, 0, 0);
1864 if ((nat_interface_is_inside(i) && is_inside) ||
1865 (nat_interface_is_outside(i) && !is_inside))
1868 if (sm->num_workers > 1 && !sm->deterministic)
1870 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1871 "nat44-out2in-worker-handoff";
1872 feature_name = "nat44-handoff-classify";
1874 else if (sm->deterministic)
1876 del_feature_name = !is_inside ? "nat44-det-in2out" :
1878 feature_name = "nat44-det-classify";
1880 else if (sm->endpoint_dependent)
1882 del_feature_name = !is_inside ? "nat44-ed-in2out" :
1884 feature_name = "nat44-ed-classify";
1888 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1889 feature_name = "nat44-classify";
1892 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1893 sw_if_index, 0, 0, 0);
1894 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1895 sw_if_index, 1, 0, 0);
1898 if (sm->endpoint_dependent)
1899 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1900 sw_if_index, 0, 0, 0);
1901 else if (!sm->deterministic)
1902 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1903 sw_if_index, 0, 0, 0);
1914 return VNET_API_ERROR_NO_SUCH_ENTRY;
1916 pool_get (sm->interfaces, i);
1917 i->sw_if_index = sw_if_index;
1919 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0,
1922 if (is_inside && !sm->out2in_dpo)
1924 if (sm->endpoint_dependent)
1925 vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
1926 sw_if_index, 1, 0, 0);
1927 else if (!sm->deterministic)
1928 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1929 sw_if_index, 1, 0, 0);
1935 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1939 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1941 /* Add/delete external addresses to FIB */
1944 vec_foreach (ap, sm->addresses)
1945 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1947 pool_foreach (m, sm->static_mappings,
1949 if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1952 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1955 pool_foreach (dm, sm->det_maps,
1957 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1965 snat_interface_add_del_output_feature (u32 sw_if_index,
1966 u8 is_inside, int is_del)
1968 snat_main_t *sm = &snat_main;
1969 snat_interface_t *i;
1971 snat_static_mapping_t *m;
1972 nat_outside_fib_t *outside_fib;
1973 u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
1977 if (sm->deterministic ||
1978 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1979 return VNET_API_ERROR_UNSUPPORTED;
1982 pool_foreach (i, sm->interfaces,
1984 if (i->sw_if_index == sw_if_index)
1985 return VNET_API_ERROR_VALUE_EXIST;
1992 vec_foreach (outside_fib, sm->outside_fibs)
1994 if (outside_fib->fib_index == fib_index)
1998 outside_fib->refcount--;
1999 if (!outside_fib->refcount)
2000 vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs);
2003 outside_fib->refcount++;
2010 vec_add2 (sm->outside_fibs, outside_fib, 1);
2011 outside_fib->refcount = 1;
2012 outside_fib->fib_index = fib_index;
2019 if (sm->endpoint_dependent)
2021 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst",
2022 sw_if_index, !is_del, 0, 0);
2023 vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src",
2024 sw_if_index, !is_del, 0, 0);
2028 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
2029 sw_if_index, !is_del, 0, 0);
2030 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
2031 sw_if_index, !is_del, 0, 0);
2036 if (sm->num_workers > 1)
2038 vnet_feature_enable_disable ("ip4-unicast",
2039 "nat44-out2in-worker-handoff",
2040 sw_if_index, !is_del, 0, 0);
2041 vnet_feature_enable_disable ("ip4-output",
2042 "nat44-in2out-output-worker-handoff",
2043 sw_if_index, !is_del, 0, 0);
2047 if (sm->endpoint_dependent)
2049 vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in",
2050 sw_if_index, !is_del, 0, 0);
2051 vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output",
2052 sw_if_index, !is_del, 0, 0);
2056 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in",
2057 sw_if_index, !is_del, 0, 0);
2058 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
2059 sw_if_index, !is_del, 0, 0);
2064 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
2065 sm->fq_in2out_output_index =
2066 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
2068 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
2069 sm->fq_out2in_index =
2070 vlib_frame_queue_main_init (sm->out2in_node_index, 0);
2073 pool_foreach (i, sm->output_feature_interfaces,
2075 if (i->sw_if_index == sw_if_index)
2078 pool_put (sm->output_feature_interfaces, i);
2080 return VNET_API_ERROR_VALUE_EXIST;
2088 return VNET_API_ERROR_NO_SUCH_ENTRY;
2090 pool_get (sm->output_feature_interfaces, i);
2091 i->sw_if_index = sw_if_index;
2094 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
2096 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
2098 /* Add/delete external addresses to FIB */
2104 vec_foreach (ap, sm->addresses)
2105 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
2107 pool_foreach (m, sm->static_mappings,
2109 if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32))
2112 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
2120 snat_set_workers (uword * bitmap)
2122 snat_main_t *sm = &snat_main;
2125 if (sm->num_workers < 2)
2126 return VNET_API_ERROR_FEATURE_DISABLED;
2128 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
2129 return VNET_API_ERROR_INVALID_WORKER;
2131 vec_free (sm->workers);
2133 clib_bitmap_foreach (i, bitmap,
2135 vec_add1(sm->workers, i);
2136 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
2141 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
2142 sm->num_snat_thread = _vec_len (sm->workers);
2149 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2152 ip4_address_t * address,
2154 u32 if_address_index, u32 is_delete);
2157 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
2160 ip4_address_t * address,
2162 u32 if_address_index, u32 is_delete);
2165 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2168 snat_session_key_t * k,
2169 u16 port_per_thread, u32 snat_thread_index);
2171 static clib_error_t *
2172 snat_init (vlib_main_t * vm)
2174 snat_main_t *sm = &snat_main;
2175 clib_error_t *error = 0;
2176 ip4_main_t *im = &ip4_main;
2177 ip_lookup_main_t *lm = &im->lookup_main;
2179 vlib_thread_registration_t *tr;
2180 vlib_thread_main_t *tm = vlib_get_thread_main ();
2183 ip4_add_del_interface_address_callback_t cb4;
2184 vlib_node_t *error_drop_node;
2187 sm->vnet_main = vnet_get_main ();
2189 sm->ip4_lookup_main = lm;
2190 sm->api_main = &api_main;
2191 sm->first_worker_index = 0;
2192 sm->num_workers = 0;
2193 sm->num_snat_thread = 1;
2195 sm->port_per_thread = 0xffff - 1024;
2196 sm->fq_in2out_index = ~0;
2197 sm->fq_out2in_index = ~0;
2198 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2199 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2200 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2201 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2202 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
2203 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
2204 sm->forwarding_enabled = 0;
2205 sm->log_class = vlib_log_register_class ("nat", 0);
2206 error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
2207 sm->error_node_index = error_drop_node->index;
2208 sm->mss_clamping = 0;
2210 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
2213 tr = (vlib_thread_registration_t *) p[0];
2216 sm->num_workers = tr->count;
2217 sm->first_worker_index = tr->first_index;
2221 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
2223 /* Use all available workers by default */
2224 if (sm->num_workers > 1)
2226 for (i = 0; i < sm->num_workers; i++)
2227 bitmap = clib_bitmap_set (bitmap, i, 1);
2228 snat_set_workers (bitmap);
2229 clib_bitmap_free (bitmap);
2233 sm->per_thread_data[0].snat_thread_index = 0;
2236 error = snat_api_init (vm, sm);
2240 /* Set up the interface address add/del callback */
2241 cb4.function = snat_ip4_add_del_interface_address_cb;
2242 cb4.function_opaque = 0;
2244 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2246 cb4.function = nat_ip4_add_del_addr_only_sm_cb;
2247 cb4.function_opaque = 0;
2249 vec_add1 (im->add_del_interface_address_callbacks, cb4);
2251 nat_dpo_module_init ();
2254 sm->total_users.name = "total-users";
2255 sm->total_users.stat_segment_name = "/nat44/total-users";
2256 vlib_validate_simple_counter (&sm->total_users, 0);
2257 vlib_zero_simple_counter (&sm->total_users, 0);
2258 sm->total_sessions.name = "total-sessions";
2259 sm->total_sessions.stat_segment_name = "/nat44/total-sessions";
2260 vlib_validate_simple_counter (&sm->total_sessions, 0);
2261 vlib_zero_simple_counter (&sm->total_sessions, 0);
2263 /* Init IPFIX logging */
2264 snat_ipfix_logging_init (vm);
2267 error = nat64_init (vm);
2275 /* Init virtual fragmenentation reassembly */
2276 return nat_reass_init (vm);
2279 VLIB_INIT_FUNCTION (snat_init);
2282 snat_free_outside_address_and_port (snat_address_t * addresses,
2283 u32 thread_index, snat_session_key_t * k)
2287 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
2289 for (address_index = 0; address_index < vec_len (addresses);
2292 if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
2296 ASSERT (address_index < vec_len (addresses));
2298 a = addresses + address_index;
2300 switch (k->protocol)
2302 #define _(N, i, n, s) \
2303 case SNAT_PROTOCOL_##N: \
2304 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
2305 port_host_byte_order) == 1); \
2306 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
2307 port_host_byte_order, 0); \
2308 a->busy_##n##_ports--; \
2309 a->busy_##n##_ports_per_thread[thread_index]--; \
2311 foreach_snat_protocol
2314 nat_log_info ("unknown protocol");
2320 snat_static_mapping_match (snat_main_t * sm,
2321 snat_session_key_t match,
2322 snat_session_key_t * mapping,
2325 twice_nat_type_t * twice_nat,
2326 lb_nat_type_t * lb, ip4_address_t * ext_host_addr,
2327 u8 * is_identity_nat)
2329 clib_bihash_kv_8_8_t kv, value;
2330 snat_static_mapping_t *m;
2331 snat_session_key_t m_key;
2332 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
2333 u32 rand, lo = 0, hi, mid, *tmp = 0, i;
2335 nat44_lb_addr_port_t *local;
2337 m_key.fib_index = match.fib_index;
2340 mapping_hash = &sm->static_mapping_by_external;
2341 m_key.fib_index = 0;
2344 m_key.addr = match.addr;
2345 m_key.port = clib_net_to_host_u16 (match.port);
2346 m_key.protocol = match.protocol;
2348 kv.key = m_key.as_u64;
2350 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2352 /* Try address only mapping */
2355 kv.key = m_key.as_u64;
2356 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
2360 m = pool_elt_at_index (sm->static_mappings, value.value);
2364 if (is_lb_static_mapping (m))
2366 if (PREDICT_FALSE (lb != 0))
2367 *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT;
2370 if (nat_affinity_find_and_lock (ext_host_addr[0], match.addr,
2371 match.protocol, match.port,
2375 local = pool_elt_at_index (m->locals, backend_index);
2376 mapping->addr = local->addr;
2377 mapping->port = clib_host_to_net_u16 (local->port);
2378 mapping->fib_index = local->fib_index;
2383 pool_foreach_index (i, m->locals,
2388 hi = vec_len (tmp) - 1;
2389 local = pool_elt_at_index (m->locals, tmp[hi]);
2390 rand = 1 + (random_u32 (&sm->random_seed) % local->prefix);
2393 mid = ((hi - lo) >> 1) + lo;
2394 local = pool_elt_at_index (m->locals, tmp[mid]);
2395 (rand > local->prefix) ? (lo = mid + 1) : (hi = mid);
2397 local = pool_elt_at_index (m->locals, tmp[lo]);
2398 if (!(local->prefix >= rand))
2400 if (PREDICT_FALSE (sm->num_workers > 1))
2403 .src_address = local->addr,
2405 if (sm->worker_in2out_cb (&ip, m->fib_index) !=
2406 vlib_get_thread_index ())
2409 mapping->addr = local->addr;
2410 mapping->port = clib_host_to_net_u16 (local->port);
2411 mapping->fib_index = local->fib_index;
2414 if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr,
2415 match.protocol, match.port,
2416 tmp[lo], m->affinity,
2417 m->affinity_per_service_list_head_index))
2418 nat_log_info ("create affinity record failed");
2424 if (PREDICT_FALSE (lb != 0))
2426 mapping->fib_index = m->fib_index;
2427 mapping->addr = m->local_addr;
2428 /* Address only mapping doesn't change port */
2429 mapping->port = is_addr_only_static_mapping (m) ? match.port
2430 : clib_host_to_net_u16 (m->local_port);
2432 mapping->protocol = m->proto;
2436 mapping->addr = m->external_addr;
2437 /* Address only mapping doesn't change port */
2438 mapping->port = is_addr_only_static_mapping (m) ? match.port
2439 : clib_host_to_net_u16 (m->external_port);
2440 mapping->fib_index = sm->outside_fib_index;
2444 if (PREDICT_FALSE (is_addr_only != 0))
2445 *is_addr_only = is_addr_only_static_mapping (m);
2447 if (PREDICT_FALSE (twice_nat != 0))
2448 *twice_nat = m->twice_nat;
2450 if (PREDICT_FALSE (is_identity_nat != 0))
2451 *is_identity_nat = is_identity_static_mapping (m);
2456 static_always_inline u16
2457 snat_random_port (u16 min, u16 max)
2459 snat_main_t *sm = &snat_main;
2460 return min + random_u32 (&sm->random_seed) /
2461 (random_u32_max () / (max - min + 1) + 1);
2465 snat_alloc_outside_address_and_port (snat_address_t * addresses,
2468 snat_session_key_t * k,
2469 u16 port_per_thread,
2470 u32 snat_thread_index)
2472 snat_main_t *sm = &snat_main;
2474 return sm->alloc_addr_and_port (addresses, fib_index, thread_index, k,
2475 port_per_thread, snat_thread_index);
2479 nat_alloc_addr_and_port_default (snat_address_t * addresses,
2482 snat_session_key_t * k,
2483 u16 port_per_thread, u32 snat_thread_index)
2486 snat_address_t *a, *ga = 0;
2489 for (i = 0; i < vec_len (addresses); i++)
2492 switch (k->protocol)
2494 #define _(N, j, n, s) \
2495 case SNAT_PROTOCOL_##N: \
2496 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
2498 if (a->fib_index == fib_index) \
2502 portnum = (port_per_thread * \
2503 snat_thread_index) + \
2504 snat_random_port(1, port_per_thread) + 1024; \
2505 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2507 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2508 a->busy_##n##_ports_per_thread[thread_index]++; \
2509 a->busy_##n##_ports++; \
2510 k->addr = a->addr; \
2511 k->port = clib_host_to_net_u16(portnum); \
2515 else if (a->fib_index == ~0) \
2521 foreach_snat_protocol
2524 nat_log_info ("unknown protocol");
2533 switch (k->protocol)
2535 #define _(N, j, n, s) \
2536 case SNAT_PROTOCOL_##N: \
2539 portnum = (port_per_thread * \
2540 snat_thread_index) + \
2541 snat_random_port(1, port_per_thread) + 1024; \
2542 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2544 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2545 a->busy_##n##_ports_per_thread[thread_index]++; \
2546 a->busy_##n##_ports++; \
2547 k->addr = a->addr; \
2548 k->port = clib_host_to_net_u16(portnum); \
2552 foreach_snat_protocol
2555 nat_log_info ("unknown protocol");
2560 /* Totally out of translations to use... */
2561 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2566 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2569 snat_session_key_t * k,
2570 u16 port_per_thread, u32 snat_thread_index)
2572 snat_main_t *sm = &snat_main;
2573 snat_address_t *a = addresses;
2574 u16 m, ports, portnum, A, j;
2575 m = 16 - (sm->psid_offset + sm->psid_length);
2576 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2578 if (!vec_len (addresses))
2581 switch (k->protocol)
2583 #define _(N, i, n, s) \
2584 case SNAT_PROTOCOL_##N: \
2585 if (a->busy_##n##_ports < ports) \
2589 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2590 j = snat_random_port(0, pow2_mask(m)); \
2591 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2592 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2594 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2595 a->busy_##n##_ports++; \
2596 k->addr = a->addr; \
2597 k->port = clib_host_to_net_u16 (portnum); \
2602 foreach_snat_protocol
2605 nat_log_info ("unknown protocol");
2610 /* Totally out of translations to use... */
2611 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2616 nat_alloc_addr_and_port_range (snat_address_t * addresses,
2619 snat_session_key_t * k,
2620 u16 port_per_thread, u32 snat_thread_index)
2622 snat_main_t *sm = &snat_main;
2623 snat_address_t *a = addresses;
2626 ports = sm->end_port - sm->start_port + 1;
2628 if (!vec_len (addresses))
2631 switch (k->protocol)
2633 #define _(N, i, n, s) \
2634 case SNAT_PROTOCOL_##N: \
2635 if (a->busy_##n##_ports < ports) \
2639 portnum = snat_random_port(sm->start_port, sm->end_port); \
2640 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2642 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2643 a->busy_##n##_ports++; \
2644 k->addr = a->addr; \
2645 k->port = clib_host_to_net_u16 (portnum); \
2650 foreach_snat_protocol
2653 nat_log_info ("unknown protocol");
2658 /* Totally out of translations to use... */
2659 snat_ipfix_logging_addresses_exhausted (thread_index, 0);
2664 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2666 dpo_id_t dpo_v4 = DPO_INVALID;
2667 fib_prefix_t pfx = {
2668 .fp_proto = FIB_PROTOCOL_IP4,
2670 .fp_addr.ip4.as_u32 = addr.as_u32,
2675 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2676 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2677 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2678 dpo_reset (&dpo_v4);
2682 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2687 format_session_kvp (u8 * s, va_list * args)
2689 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2690 snat_session_key_t k;
2694 s = format (s, "%U session-index %llu", format_snat_key, &k, v->value);
2700 format_static_mapping_kvp (u8 * s, va_list * args)
2702 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2703 snat_session_key_t k;
2707 s = format (s, "%U static-mapping-index %llu",
2708 format_static_mapping_key, &k, v->value);
2714 format_user_kvp (u8 * s, va_list * args)
2716 clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *);
2721 s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr,
2722 k.fib_index, v->value);
2728 format_ed_session_kvp (u8 * s, va_list * args)
2730 clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
2733 k.as_u64[0] = v->key[0];
2734 k.as_u64[1] = v->key[1];
2737 format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu",
2738 format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port),
2739 format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port),
2740 format_ip_protocol, k.proto, k.fib_index, v->value);
2746 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2748 snat_main_t *sm = &snat_main;
2749 u32 next_worker_index = 0;
2752 next_worker_index = sm->first_worker_index;
2753 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2754 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24);
2756 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2757 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2759 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2761 return next_worker_index;
2765 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2767 snat_main_t *sm = &snat_main;
2770 snat_session_key_t m_key;
2771 clib_bihash_kv_8_8_t kv, value;
2772 snat_static_mapping_t *m;
2774 u32 next_worker_index = 0;
2776 /* first try static mappings without port */
2777 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2779 m_key.addr = ip0->dst_address;
2782 m_key.fib_index = rx_fib_index0;
2783 kv.key = m_key.as_u64;
2784 if (!clib_bihash_search_8_8
2785 (&sm->static_mapping_by_external, &kv, &value))
2787 m = pool_elt_at_index (sm->static_mappings, value.value);
2788 return m->workers[0];
2792 proto = ip_proto_to_snat_proto (ip0->protocol);
2793 udp = ip4_next_header (ip0);
2794 port = udp->dst_port;
2796 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2798 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2799 return vlib_get_thread_index ();
2801 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2803 nat_reass_ip4_t *reass;
2805 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2806 ip0->fragment_id, ip0->protocol);
2808 if (reass && (reass->thread_index != (u32) ~ 0))
2809 return reass->thread_index;
2811 return vlib_get_thread_index ();
2815 /* unknown protocol */
2816 if (PREDICT_FALSE (proto == ~0))
2818 /* use current thread */
2819 return vlib_get_thread_index ();
2822 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2824 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2825 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2826 if (!icmp_is_error_message (icmp))
2827 port = echo->identifier;
2830 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2831 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2832 void *l4_header = ip4_next_header (inner_ip);
2835 case SNAT_PROTOCOL_ICMP:
2836 icmp = (icmp46_header_t *) l4_header;
2837 echo = (icmp_echo_header_t *) (icmp + 1);
2838 port = echo->identifier;
2840 case SNAT_PROTOCOL_UDP:
2841 case SNAT_PROTOCOL_TCP:
2842 port = ((tcp_udp_header_t *) l4_header)->src_port;
2845 return vlib_get_thread_index ();
2850 /* try static mappings with port */
2851 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2853 m_key.addr = ip0->dst_address;
2854 m_key.port = clib_net_to_host_u16 (port);
2855 m_key.protocol = proto;
2856 m_key.fib_index = rx_fib_index0;
2857 kv.key = m_key.as_u64;
2858 if (!clib_bihash_search_8_8
2859 (&sm->static_mapping_by_external, &kv, &value))
2861 m = pool_elt_at_index (sm->static_mappings, value.value);
2862 return m->workers[0];
2866 /* worker by outside port */
2867 next_worker_index = sm->first_worker_index;
2868 next_worker_index +=
2869 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2870 return next_worker_index;
2874 nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index)
2876 snat_main_t *sm = &snat_main;
2877 clib_bihash_kv_8_8_t kv, value;
2878 u32 proto, next_worker_index = 0;
2881 snat_static_mapping_t *m;
2884 /* first try static mappings without port */
2885 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2887 make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0);
2888 if (!clib_bihash_search_8_8
2889 (&sm->static_mapping_by_external, &kv, &value))
2891 m = pool_elt_at_index (sm->static_mappings, value.value);
2892 return m->workers[0];
2896 proto = ip_proto_to_snat_proto (ip->protocol);
2898 /* unknown protocol */
2899 if (PREDICT_FALSE (proto == ~0))
2901 /* use current thread */
2902 return vlib_get_thread_index ();
2905 udp = ip4_next_header (ip);
2906 port = udp->dst_port;
2908 if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP))
2910 icmp46_header_t *icmp = (icmp46_header_t *) udp;
2911 icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
2912 if (!icmp_is_error_message (icmp))
2913 port = echo->identifier;
2916 ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
2917 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2918 void *l4_header = ip4_next_header (inner_ip);
2921 case SNAT_PROTOCOL_ICMP:
2922 icmp = (icmp46_header_t *) l4_header;
2923 echo = (icmp_echo_header_t *) (icmp + 1);
2924 port = echo->identifier;
2926 case SNAT_PROTOCOL_UDP:
2927 case SNAT_PROTOCOL_TCP:
2928 port = ((tcp_udp_header_t *) l4_header)->src_port;
2931 return vlib_get_thread_index ();
2936 /* try static mappings with port */
2937 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2939 make_sm_kv (&kv, &ip->dst_address, proto, rx_fib_index,
2940 clib_net_to_host_u16 (port));
2941 if (!clib_bihash_search_8_8
2942 (&sm->static_mapping_by_external, &kv, &value))
2944 m = pool_elt_at_index (sm->static_mappings, value.value);
2945 if (!is_lb_static_mapping (m))
2946 return m->workers[0];
2948 hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) +
2949 (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24);
2951 if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers))))
2952 return m->workers[hash & (_vec_len (m->workers) - 1)];
2954 return m->workers[hash % _vec_len (m->workers)];
2958 /* worker by outside port */
2959 next_worker_index = sm->first_worker_index;
2960 next_worker_index +=
2961 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2963 return next_worker_index;
2966 static clib_error_t *
2967 snat_config (vlib_main_t * vm, unformat_input_t * input)
2969 snat_main_t *sm = &snat_main;
2970 nat66_main_t *nm = &nat66_main;
2971 u32 translation_buckets = 1024;
2972 u32 translation_memory_size = 128 << 20;
2973 u32 user_buckets = 128;
2974 u32 user_memory_size = 64 << 20;
2975 u32 max_translations_per_user = 100;
2976 u32 outside_vrf_id = 0;
2977 u32 outside_ip6_vrf_id = 0;
2978 u32 inside_vrf_id = 0;
2979 u32 static_mapping_buckets = 1024;
2980 u32 static_mapping_memory_size = 64 << 20;
2981 u32 nat64_bib_buckets = 1024;
2982 u32 nat64_bib_memory_size = 128 << 20;
2983 u32 nat64_st_buckets = 2048;
2984 u32 nat64_st_memory_size = 256 << 20;
2985 u8 static_mapping_only = 0;
2986 u8 static_mapping_connection_tracking = 0;
2987 snat_main_per_thread_data_t *tsm;
2988 dslite_main_t *dm = &dslite_main;
2990 sm->deterministic = 0;
2992 sm->endpoint_dependent = 0;
2994 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2997 (input, "translation hash buckets %d", &translation_buckets))
2999 else if (unformat (input, "translation hash memory %d",
3000 &translation_memory_size));
3001 else if (unformat (input, "user hash buckets %d", &user_buckets))
3003 else if (unformat (input, "user hash memory %d", &user_memory_size))
3005 else if (unformat (input, "max translations per user %d",
3006 &max_translations_per_user))
3008 else if (unformat (input, "outside VRF id %d", &outside_vrf_id))
3010 else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id))
3012 else if (unformat (input, "inside VRF id %d", &inside_vrf_id))
3014 else if (unformat (input, "static mapping only"))
3016 static_mapping_only = 1;
3017 if (unformat (input, "connection tracking"))
3018 static_mapping_connection_tracking = 1;
3020 else if (unformat (input, "deterministic"))
3021 sm->deterministic = 1;
3022 else if (unformat (input, "nat64 bib hash buckets %d",
3023 &nat64_bib_buckets))
3025 else if (unformat (input, "nat64 bib hash memory %d",
3026 &nat64_bib_memory_size))
3029 if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
3031 else if (unformat (input, "nat64 st hash memory %d",
3032 &nat64_st_memory_size))
3034 else if (unformat (input, "out2in dpo"))
3036 else if (unformat (input, "dslite ce"))
3037 dslite_set_ce (dm, 1);
3038 else if (unformat (input, "endpoint-dependent"))
3039 sm->endpoint_dependent = 1;
3041 return clib_error_return (0, "unknown input '%U'",
3042 format_unformat_error, input);
3045 if (sm->deterministic && sm->endpoint_dependent)
3046 return clib_error_return (0,
3047 "deterministic and endpoint-dependent modes are mutually exclusive");
3049 if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
3050 return clib_error_return (0,
3051 "static mapping only mode available only for simple nat");
3053 if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
3054 return clib_error_return (0,
3055 "out2in dpo mode available only for simple nat");
3057 /* for show commands, etc. */
3058 sm->translation_buckets = translation_buckets;
3059 sm->translation_memory_size = translation_memory_size;
3060 /* do not exceed load factor 10 */
3061 sm->max_translations = 10 * translation_buckets;
3062 sm->user_buckets = user_buckets;
3063 sm->user_memory_size = user_memory_size;
3064 sm->max_translations_per_user = max_translations_per_user;
3065 sm->outside_vrf_id = outside_vrf_id;
3066 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
3068 FIB_SOURCE_PLUGIN_HI);
3069 nm->outside_vrf_id = outside_ip6_vrf_id;
3070 nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6,
3072 FIB_SOURCE_PLUGIN_HI);
3073 sm->inside_vrf_id = inside_vrf_id;
3074 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
3076 FIB_SOURCE_PLUGIN_HI);
3077 sm->static_mapping_only = static_mapping_only;
3078 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
3080 nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
3081 nat64_st_memory_size);
3083 if (sm->deterministic)
3085 sm->in2out_node_index = snat_det_in2out_node.index;
3086 sm->in2out_output_node_index = ~0;
3087 sm->out2in_node_index = snat_det_out2in_node.index;
3088 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
3089 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
3093 if (sm->endpoint_dependent)
3095 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
3096 sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb;
3097 sm->in2out_node_index = nat44_ed_in2out_node.index;
3098 sm->in2out_output_node_index = nat44_ed_in2out_output_node.index;
3099 sm->out2in_node_index = nat44_ed_out2in_node.index;
3100 sm->icmp_match_in2out_cb = icmp_match_in2out_ed;
3101 sm->icmp_match_out2in_cb = icmp_match_out2in_ed;
3102 nat_affinity_init (vm);
3106 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
3107 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
3108 sm->in2out_node_index = snat_in2out_node.index;
3109 sm->in2out_output_node_index = snat_in2out_output_node.index;
3110 sm->out2in_node_index = snat_out2in_node.index;
3111 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
3112 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
3114 if (!static_mapping_only ||
3115 (static_mapping_only && static_mapping_connection_tracking))
3118 vec_foreach (tsm, sm->per_thread_data)
3120 if (sm->endpoint_dependent)
3122 clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed",
3123 translation_buckets,
3124 translation_memory_size);
3125 clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed,
3126 format_ed_session_kvp);
3128 clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed",
3129 translation_buckets,
3130 translation_memory_size);
3131 clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed,
3132 format_ed_session_kvp);
3136 clib_bihash_init_8_8 (&tsm->in2out, "in2out",
3137 translation_buckets,
3138 translation_memory_size);
3139 clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out,
3140 format_session_kvp);
3142 clib_bihash_init_8_8 (&tsm->out2in, "out2in",
3143 translation_buckets,
3144 translation_memory_size);
3145 clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in,
3146 format_session_kvp);
3149 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
3151 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash,
3159 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
3160 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
3162 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
3163 "static_mapping_by_local", static_mapping_buckets,
3164 static_mapping_memory_size);
3165 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local,
3166 format_static_mapping_kvp);
3168 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
3169 "static_mapping_by_external",
3170 static_mapping_buckets,
3171 static_mapping_memory_size);
3172 clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external,
3173 format_static_mapping_kvp);
3179 VLIB_CONFIG_FUNCTION (snat_config, "nat");
3182 nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
3185 ip4_address_t * address,
3187 u32 if_address_index, u32 is_delete)
3189 snat_main_t *sm = &snat_main;
3190 snat_static_map_resolve_t *rp;
3191 snat_static_mapping_t *m;
3192 snat_session_key_t m_key;
3193 clib_bihash_kv_8_8_t kv, value;
3195 ip4_address_t l_addr;
3197 for (i = 0; i < vec_len (sm->to_resolve); i++)
3199 rp = sm->to_resolve + i;
3200 if (rp->addr_only == 0)
3202 if (rp->sw_if_index == sw_if_index)
3209 m_key.addr.as_u32 = address->as_u32;
3210 m_key.port = rp->addr_only ? 0 : rp->e_port;
3211 m_key.protocol = rp->addr_only ? 0 : rp->proto;
3212 m_key.fib_index = sm->outside_fib_index;
3213 kv.key = m_key.as_u64;
3214 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
3217 m = pool_elt_at_index (sm->static_mappings, value.value);
3221 /* Don't trip over lease renewal, static config */
3231 /* Indetity mapping? */
3232 if (rp->l_addr.as_u32 == 0)
3233 l_addr.as_u32 = address[0].as_u32;
3235 l_addr.as_u32 = rp->l_addr.as_u32;
3236 /* Add the static mapping */
3237 rv = snat_add_static_mapping (l_addr,
3242 rp->addr_only, ~0 /* sw_if_index */ ,
3243 rp->proto, !is_delete, rp->twice_nat,
3244 rp->out2in_only, rp->tag, rp->identity_nat);
3246 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3250 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3253 ip4_address_t * address,
3255 u32 if_address_index, u32 is_delete)
3257 snat_main_t *sm = &snat_main;
3258 snat_static_map_resolve_t *rp;
3259 ip4_address_t l_addr;
3263 snat_address_t *addresses = sm->addresses;
3265 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++)
3267 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3271 for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++)
3274 addresses = sm->twice_nat_addresses;
3275 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3284 /* Don't trip over lease renewal, static config */
3285 for (j = 0; j < vec_len (addresses); j++)
3286 if (addresses[j].addr.as_u32 == address->as_u32)
3289 (void) snat_add_address (sm, address, ~0, twice_nat);
3290 /* Scan static map resolution vector */
3291 for (j = 0; j < vec_len (sm->to_resolve); j++)
3293 rp = sm->to_resolve + j;
3296 /* On this interface? */
3297 if (rp->sw_if_index == sw_if_index)
3299 /* Indetity mapping? */
3300 if (rp->l_addr.as_u32 == 0)
3301 l_addr.as_u32 = address[0].as_u32;
3303 l_addr.as_u32 = rp->l_addr.as_u32;
3304 /* Add the static mapping */
3305 rv = snat_add_static_mapping (l_addr,
3311 ~0 /* sw_if_index */ ,
3313 rp->is_add, rp->twice_nat,
3314 rp->out2in_only, rp->tag,
3317 nat_log_notice ("snat_add_static_mapping returned %d", rv);
3324 (void) snat_del_address (sm, address[0], 1, twice_nat);
3331 snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del,
3334 ip4_main_t *ip4_main = sm->ip4_main;
3335 ip4_address_t *first_int_addr;
3336 snat_static_map_resolve_t *rp;
3337 u32 *indices_to_delete = 0;
3339 u32 *auto_add_sw_if_indices =
3341 auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3343 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */
3346 for (i = 0; i < vec_len (auto_add_sw_if_indices); i++)
3348 if (auto_add_sw_if_indices[i] == sw_if_index)
3352 /* if have address remove it */
3354 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3357 for (j = 0; j < vec_len (sm->to_resolve); j++)
3359 rp = sm->to_resolve + j;
3360 if (rp->sw_if_index == sw_if_index)
3361 vec_add1 (indices_to_delete, j);
3363 if (vec_len (indices_to_delete))
3365 for (j = vec_len (indices_to_delete) - 1; j >= 0; j--)
3366 vec_del1 (sm->to_resolve, j);
3367 vec_free (indices_to_delete);
3371 vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i);
3373 vec_del1 (sm->auto_add_sw_if_indices, i);
3376 return VNET_API_ERROR_VALUE_EXIST;
3383 return VNET_API_ERROR_NO_SUCH_ENTRY;
3385 /* add to the auto-address list */
3387 vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3389 vec_add1 (sm->auto_add_sw_if_indices, sw_if_index);
3391 /* If the address is already bound - or static - add it now */
3393 (void) snat_add_address (sm, first_int_addr, ~0, twice_nat);
3399 nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3400 snat_protocol_t proto, u32 vrf_id, int is_in)
3402 snat_main_per_thread_data_t *tsm;
3403 clib_bihash_kv_8_8_t kv, value;
3405 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3406 snat_session_key_t key;
3408 clib_bihash_8_8_t *t;
3410 if (sm->endpoint_dependent)
3411 return VNET_API_ERROR_UNSUPPORTED;
3413 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3414 if (sm->num_workers > 1)
3416 vec_elt_at_index (sm->per_thread_data,
3417 sm->worker_in2out_cb (&ip, fib_index));
3419 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3421 key.addr.as_u32 = addr->as_u32;
3422 key.port = clib_host_to_net_u16 (port);
3423 key.protocol = proto;
3424 key.fib_index = fib_index;
3425 kv.key = key.as_u64;
3426 t = is_in ? &tsm->in2out : &tsm->out2in;
3427 if (!clib_bihash_search_8_8 (t, &kv, &value))
3429 if (pool_is_free_index (tsm->sessions, value.value))
3430 return VNET_API_ERROR_UNSPECIFIED;
3432 s = pool_elt_at_index (tsm->sessions, value.value);
3433 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3434 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3438 return VNET_API_ERROR_NO_SUCH_ENTRY;
3442 nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
3443 ip4_address_t * eh_addr, u16 eh_port, u8 proto,
3444 u32 vrf_id, int is_in)
3447 clib_bihash_16_8_t *t;
3448 nat_ed_ses_key_t key;
3449 clib_bihash_kv_16_8_t kv, value;
3450 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3452 snat_main_per_thread_data_t *tsm;
3454 if (!sm->endpoint_dependent)
3455 return VNET_API_ERROR_FEATURE_DISABLED;
3457 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3458 if (sm->num_workers > 1)
3460 vec_elt_at_index (sm->per_thread_data,
3461 sm->worker_in2out_cb (&ip, fib_index));
3463 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3465 t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed;
3466 key.l_addr.as_u32 = addr->as_u32;
3467 key.r_addr.as_u32 = eh_addr->as_u32;
3468 key.l_port = clib_host_to_net_u16 (port);
3469 key.r_port = clib_host_to_net_u16 (eh_port);
3471 key.fib_index = fib_index;
3472 kv.key[0] = key.as_u64[0];
3473 kv.key[1] = key.as_u64[1];
3474 if (clib_bihash_search_16_8 (t, &kv, &value))
3475 return VNET_API_ERROR_NO_SUCH_ENTRY;
3477 if (pool_is_free_index (tsm->sessions, value.value))
3478 return VNET_API_ERROR_UNSPECIFIED;
3479 s = pool_elt_at_index (tsm->sessions, value.value);
3480 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
3481 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
3486 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
3488 snat_main_t *sm = &snat_main;
3490 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
3491 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3493 sm->psid_offset = psid_offset;
3494 sm->psid_length = psid_length;
3498 nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
3500 snat_main_t *sm = &snat_main;
3502 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
3503 sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
3504 sm->start_port = start_port;
3505 sm->end_port = end_port;
3509 nat_set_alloc_addr_and_port_default (void)
3511 snat_main_t *sm = &snat_main;
3513 sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
3514 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
3518 * fd.io coding-style-patch-verification: ON
3521 * eval: (c-set-style "gnu")