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/dslite.h>
28 #include <nat/nat_reass.h>
29 #include <vnet/fib/fib_table.h>
30 #include <vnet/fib/ip4_fib.h>
32 #include <vpp/app/version.h>
34 snat_main_t snat_main;
37 /* Hook up input features */
38 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
39 .arc_name = "ip4-unicast",
40 .node_name = "nat44-in2out",
41 .runs_before = VNET_FEATURES ("nat44-out2in"),
43 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
44 .arc_name = "ip4-unicast",
45 .node_name = "nat44-out2in",
46 .runs_before = VNET_FEATURES ("ip4-lookup"),
48 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
49 .arc_name = "ip4-unicast",
50 .node_name = "nat44-classify",
51 .runs_before = VNET_FEATURES ("ip4-lookup"),
53 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
54 .arc_name = "ip4-unicast",
55 .node_name = "nat44-det-in2out",
56 .runs_before = VNET_FEATURES ("nat44-det-out2in"),
58 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
59 .arc_name = "ip4-unicast",
60 .node_name = "nat44-det-out2in",
61 .runs_before = VNET_FEATURES ("ip4-lookup"),
63 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
64 .arc_name = "ip4-unicast",
65 .node_name = "nat44-det-classify",
66 .runs_before = VNET_FEATURES ("ip4-lookup"),
68 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
69 .arc_name = "ip4-unicast",
70 .node_name = "nat44-in2out-worker-handoff",
71 .runs_before = VNET_FEATURES ("nat44-out2in-worker-handoff"),
73 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
74 .arc_name = "ip4-unicast",
75 .node_name = "nat44-out2in-worker-handoff",
76 .runs_before = VNET_FEATURES ("ip4-lookup"),
78 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
79 .arc_name = "ip4-unicast",
80 .node_name = "nat44-handoff-classify",
81 .runs_before = VNET_FEATURES ("ip4-lookup"),
83 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
84 .arc_name = "ip4-unicast",
85 .node_name = "nat44-in2out-fast",
86 .runs_before = VNET_FEATURES ("nat44-out2in-fast"),
88 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
89 .arc_name = "ip4-unicast",
90 .node_name = "nat44-out2in-fast",
91 .runs_before = VNET_FEATURES ("ip4-lookup"),
93 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
94 .arc_name = "ip4-unicast",
95 .node_name = "nat44-hairpin-dst",
96 .runs_before = VNET_FEATURES ("ip4-lookup"),
99 /* Hook up output features */
100 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
101 .arc_name = "ip4-output",
102 .node_name = "nat44-in2out-output",
103 .runs_before = VNET_FEATURES ("interface-output"),
105 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
106 .arc_name = "ip4-output",
107 .node_name = "nat44-in2out-output-worker-handoff",
108 .runs_before = VNET_FEATURES ("interface-output"),
110 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
111 .arc_name = "ip4-output",
112 .node_name = "nat44-hairpin-src",
113 .runs_before = VNET_FEATURES ("interface-output"),
116 /* Hook up ip4-local features */
117 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
119 .arc_name = "ip4-local",
120 .node_name = "nat44-hairpinning",
121 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
126 VLIB_PLUGIN_REGISTER () = {
127 .version = VPP_BUILD_VER,
128 .description = "Network Address Translation",
132 vlib_node_registration_t nat44_classify_node;
133 vlib_node_registration_t nat44_det_classify_node;
134 vlib_node_registration_t nat44_handoff_classify_node;
137 NAT44_CLASSIFY_NEXT_IN2OUT,
138 NAT44_CLASSIFY_NEXT_OUT2IN,
139 NAT44_CLASSIFY_N_NEXT,
140 } nat44_classify_next_t;
143 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
145 snat_session_key_t key;
146 clib_bihash_kv_8_8_t kv;
147 nat_ed_ses_key_t ed_key;
148 clib_bihash_kv_16_8_t ed_kv;
151 snat_main_per_thread_data_t *tsm =
152 vec_elt_at_index (sm->per_thread_data, thread_index);
154 /* Endpoint dependent session lookup tables */
155 if (is_ed_session (s))
157 ed_key.l_addr = s->out2in.addr;
158 ed_key.r_addr = s->ext_host_addr;
159 ed_key.fib_index = s->out2in.fib_index;
160 if (snat_is_unk_proto_session (s))
162 ed_key.proto = s->in2out.port;
168 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
169 ed_key.l_port = s->out2in.port;
170 ed_key.r_port = s->ext_host_port;
172 ed_kv.key[0] = ed_key.as_u64[0];
173 ed_kv.key[1] = ed_key.as_u64[1];
174 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
175 clib_warning ("out2in_ed key del failed");
177 ed_key.l_addr = s->in2out.addr;
178 ed_key.fib_index = s->in2out.fib_index;
179 if (!snat_is_unk_proto_session (s))
180 ed_key.l_port = s->in2out.port;
181 if (is_twice_nat_session (s))
183 ed_key.r_addr = s->ext_host_nat_addr;
184 ed_key.r_port = s->ext_host_nat_port;
186 ed_kv.key[0] = ed_key.as_u64[0];
187 ed_kv.key[1] = ed_key.as_u64[1];
188 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &ed_kv, 0))
189 clib_warning ("in2out_ed key del failed");
192 if (snat_is_unk_proto_session (s))
196 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
197 s->out2in.addr.as_u32,
201 s->in2out.fib_index);
203 /* Twice NAT address and port for external host */
204 if (is_twice_nat_session (s))
206 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
208 key.protocol = s->in2out.protocol;
209 key.port = s->ext_host_nat_port;
210 a = sm->twice_nat_addresses + i;
211 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
213 snat_free_outside_address_and_port (sm->twice_nat_addresses,
214 thread_index, &key, i);
220 if (is_ed_session (s))
223 /* Session lookup tables */
224 kv.key = s->in2out.as_u64;
225 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
226 clib_warning ("in2out key del failed");
227 kv.key = s->out2in.as_u64;
228 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
229 clib_warning ("out2in key del failed");
231 if (snat_is_session_static (s))
234 if (s->outside_address_index != ~0)
235 snat_free_outside_address_and_port (sm->addresses, thread_index,
236 &s->out2in, s->outside_address_index);
240 nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
244 snat_user_key_t user_key;
245 clib_bihash_kv_8_8_t kv, value;
246 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
247 dlist_elt_t * per_user_list_head_elt;
249 user_key.addr.as_u32 = addr->as_u32;
250 user_key.fib_index = fib_index;
251 kv.key = user_key.as_u64;
253 /* Ever heard of the "user" = src ip4 address before? */
254 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
256 /* no, make a new one */
257 pool_get (tsm->users, u);
258 memset (u, 0, sizeof (*u));
259 u->addr.as_u32 = addr->as_u32;
260 u->fib_index = fib_index;
262 pool_get (tsm->list_pool, per_user_list_head_elt);
264 u->sessions_per_user_list_head_index = per_user_list_head_elt -
267 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
269 kv.value = u - tsm->users;
272 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
273 clib_warning ("user_hash keay add failed");
277 u = pool_elt_at_index (tsm->users, value.value);
284 nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u, u32 thread_index)
287 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
288 u32 oldest_per_user_translation_list_index, session_index;
289 dlist_elt_t * oldest_per_user_translation_list_elt;
290 dlist_elt_t * per_user_translation_list_elt;
292 /* Over quota? Recycle the least recently used translation */
293 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
295 oldest_per_user_translation_list_index =
296 clib_dlist_remove_head (tsm->list_pool,
297 u->sessions_per_user_list_head_index);
299 ASSERT (oldest_per_user_translation_list_index != ~0);
301 /* Add it back to the end of the LRU list */
302 clib_dlist_addtail (tsm->list_pool,
303 u->sessions_per_user_list_head_index,
304 oldest_per_user_translation_list_index);
305 /* Get the list element */
306 oldest_per_user_translation_list_elt =
307 pool_elt_at_index (tsm->list_pool,
308 oldest_per_user_translation_list_index);
310 /* Get the session index from the list element */
311 session_index = oldest_per_user_translation_list_elt->value;
313 /* Get the session */
314 s = pool_elt_at_index (tsm->sessions, session_index);
315 nat_free_session_data (sm, s, thread_index);
316 s->outside_address_index = ~0;
323 pool_get (tsm->sessions, s);
324 memset (s, 0, sizeof (*s));
325 s->outside_address_index = ~0;
327 /* Create list elts */
328 pool_get (tsm->list_pool, per_user_translation_list_elt);
329 clib_dlist_init (tsm->list_pool,
330 per_user_translation_list_elt - tsm->list_pool);
332 per_user_translation_list_elt->value = s - tsm->sessions;
333 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
334 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
336 clib_dlist_addtail (tsm->list_pool,
337 s->per_user_list_head_index,
338 per_user_translation_list_elt - tsm->list_pool);
345 nat44_classify_node_fn_inline (vlib_main_t * vm,
346 vlib_node_runtime_t * node,
347 vlib_frame_t * frame)
349 u32 n_left_from, * from, * to_next;
350 nat44_classify_next_t next_index;
351 snat_main_t *sm = &snat_main;
353 from = vlib_frame_vector_args (frame);
354 n_left_from = frame->n_vectors;
355 next_index = node->cached_next_index;
357 while (n_left_from > 0)
361 vlib_get_next_frame (vm, node, next_index,
362 to_next, n_left_to_next);
364 while (n_left_from > 0 && n_left_to_next > 0)
368 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT;
371 snat_session_key_t m_key0;
372 clib_bihash_kv_8_8_t kv0, value0;
374 /* speculatively enqueue b0 to the current next frame */
382 b0 = vlib_get_buffer (vm, bi0);
383 ip0 = vlib_buffer_get_current (b0);
385 vec_foreach (ap, sm->addresses)
387 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
389 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
394 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
396 m_key0.addr = ip0->dst_address;
399 m_key0.fib_index = sm->outside_fib_index;
400 kv0.key = m_key0.as_u64;
401 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
403 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
406 udp_header_t * udp0 = ip4_next_header (ip0);
407 m_key0.port = clib_net_to_host_u16 (udp0->dst_port);
408 m_key0.protocol = ip_proto_to_snat_proto (ip0->protocol);
409 kv0.key = m_key0.as_u64;
410 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
411 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
415 /* verify speculative enqueue, maybe switch current next frame */
416 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
417 to_next, n_left_to_next,
421 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
424 return frame->n_vectors;
428 nat44_classify_node_fn (vlib_main_t * vm,
429 vlib_node_runtime_t * node,
430 vlib_frame_t * frame)
432 return nat44_classify_node_fn_inline (vm, node, frame);
435 VLIB_REGISTER_NODE (nat44_classify_node) = {
436 .function = nat44_classify_node_fn,
437 .name = "nat44-classify",
438 .vector_size = sizeof (u32),
439 .type = VLIB_NODE_TYPE_INTERNAL,
440 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
442 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
443 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
447 VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
448 nat44_classify_node_fn);
451 nat44_det_classify_node_fn (vlib_main_t * vm,
452 vlib_node_runtime_t * node,
453 vlib_frame_t * frame)
455 return nat44_classify_node_fn_inline (vm, node, frame);
458 VLIB_REGISTER_NODE (nat44_det_classify_node) = {
459 .function = nat44_det_classify_node_fn,
460 .name = "nat44-det-classify",
461 .vector_size = sizeof (u32),
462 .type = VLIB_NODE_TYPE_INTERNAL,
463 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
465 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
466 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
470 VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
471 nat44_det_classify_node_fn);
474 nat44_handoff_classify_node_fn (vlib_main_t * vm,
475 vlib_node_runtime_t * node,
476 vlib_frame_t * frame)
478 return nat44_classify_node_fn_inline (vm, node, frame);
481 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
482 .function = nat44_handoff_classify_node_fn,
483 .name = "nat44-handoff-classify",
484 .vector_size = sizeof (u32),
485 .type = VLIB_NODE_TYPE_INTERNAL,
486 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
488 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
489 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
493 VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
494 nat44_handoff_classify_node_fn);
497 * @brief Add/del NAT address to FIB.
499 * Add the external NAT address to the FIB as receive entries. This ensures
500 * that VPP will reply to ARP for this address and we don't need to enable
501 * proxy ARP on the outside interface.
503 * @param addr IPv4 address.
504 * @param plen address prefix length
505 * @param sw_if_index Interface.
506 * @param is_add If 0 delete, otherwise add.
509 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
512 fib_prefix_t prefix = {
514 .fp_proto = FIB_PROTOCOL_IP4,
516 .ip4.as_u32 = addr->as_u32,
519 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
522 fib_table_entry_update_one_path(fib_index,
524 FIB_SOURCE_PLUGIN_HI,
525 (FIB_ENTRY_FLAG_CONNECTED |
526 FIB_ENTRY_FLAG_LOCAL |
527 FIB_ENTRY_FLAG_EXCLUSIVE),
534 FIB_ROUTE_PATH_FLAG_NONE);
536 fib_table_entry_delete(fib_index,
538 FIB_SOURCE_PLUGIN_HI);
541 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
546 vlib_thread_main_t *tm = vlib_get_thread_main ();
548 /* Check if address already exists */
549 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
551 if (ap->addr.as_u32 == addr->as_u32)
556 vec_add2 (sm->twice_nat_addresses, ap, 1);
558 vec_add2 (sm->addresses, ap, 1);
563 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
564 FIB_SOURCE_PLUGIN_HI);
567 #define _(N, i, n, s) \
568 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
569 ap->busy_##n##_ports = 0; \
570 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
571 foreach_snat_protocol
577 /* Add external address to FIB */
578 pool_foreach (i, sm->interfaces,
580 if (nat_interface_is_inside(i) || sm->out2in_dpo)
583 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
586 pool_foreach (i, sm->output_feature_interfaces,
588 if (nat_interface_is_inside(i) || sm->out2in_dpo)
591 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
596 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
599 snat_static_mapping_t *m;
600 pool_foreach (m, sm->static_mappings,
602 if (m->external_addr.as_u32 == addr.as_u32)
609 void increment_v4_address (ip4_address_t * a)
613 v = clib_net_to_host_u32(a->as_u32) + 1;
614 a->as_u32 = clib_host_to_net_u32(v);
618 snat_add_static_mapping_when_resolved (snat_main_t * sm,
619 ip4_address_t l_addr,
624 snat_protocol_t proto,
628 snat_static_map_resolve_t *rp;
630 vec_add2 (sm->to_resolve, rp, 1);
631 rp->l_addr.as_u32 = l_addr.as_u32;
633 rp->sw_if_index = sw_if_index;
637 rp->addr_only = addr_only;
642 * @brief Add static mapping.
644 * Create static mapping between local addr+port and external addr+port.
646 * @param l_addr Local IPv4 address.
647 * @param e_addr External IPv4 address.
648 * @param l_port Local port number.
649 * @param e_port External port number.
650 * @param vrf_id VRF ID.
651 * @param addr_only If 0 address port and pair mapping, otherwise address only.
652 * @param sw_if_index External port instead of specific IP address.
653 * @param is_add If 0 delete static mapping, otherwise add.
654 * @param twice_nat If 1 translate external host address and port.
655 * @param out2in_only If 1 rule match only out2in direction
659 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
660 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
661 u32 sw_if_index, snat_protocol_t proto, int is_add,
662 u8 twice_nat, u8 out2in_only)
664 snat_main_t * sm = &snat_main;
665 snat_static_mapping_t *m;
666 snat_session_key_t m_key;
667 clib_bihash_kv_8_8_t kv, value;
668 snat_address_t *a = 0;
671 snat_interface_t *interface;
673 snat_main_per_thread_data_t *tsm;
675 /* If the external address is a specific interface address */
676 if (sw_if_index != ~0)
678 ip4_address_t * first_int_addr;
680 /* Might be already set... */
681 first_int_addr = ip4_interface_first_address
682 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
684 /* DHCP resolution required? */
685 if (first_int_addr == 0)
687 snat_add_static_mapping_when_resolved
688 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
694 e_addr.as_u32 = first_int_addr->as_u32;
695 /* Identity mapping? */
696 if (l_addr.as_u32 == 0)
697 l_addr.as_u32 = e_addr.as_u32;
702 m_key.port = addr_only ? 0 : e_port;
703 m_key.protocol = addr_only ? 0 : proto;
704 m_key.fib_index = sm->outside_fib_index;
705 kv.key = m_key.as_u64;
706 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
709 m = pool_elt_at_index (sm->static_mappings, value.value);
714 return VNET_API_ERROR_VALUE_EXIST;
716 if (twice_nat && addr_only)
717 return VNET_API_ERROR_UNSUPPORTED;
719 /* Convert VRF id to FIB index */
722 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
724 return VNET_API_ERROR_NO_SUCH_FIB;
727 /* If not specified use inside VRF id from SNAT plugin startup config */
730 fib_index = sm->inside_fib_index;
731 vrf_id = sm->inside_vrf_id;
734 /* Find external address in allocated addresses and reserve port for
735 address and port pair mapping when dynamic translations enabled */
736 if (!(addr_only || sm->static_mapping_only || out2in_only))
738 for (i = 0; i < vec_len (sm->addresses); i++)
740 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
742 a = sm->addresses + i;
743 /* External port must be unused */
746 #define _(N, j, n, s) \
747 case SNAT_PROTOCOL_##N: \
748 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
749 return VNET_API_ERROR_INVALID_VALUE; \
750 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
753 a->busy_##n##_ports++; \
754 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
757 foreach_snat_protocol
760 clib_warning("unknown_protocol");
761 return VNET_API_ERROR_INVALID_VALUE_2;
766 /* External address must be allocated */
768 return VNET_API_ERROR_NO_SUCH_ENTRY;
771 pool_get (sm->static_mappings, m);
772 memset (m, 0, sizeof (*m));
773 m->local_addr = l_addr;
774 m->external_addr = e_addr;
775 m->addr_only = addr_only;
777 m->fib_index = fib_index;
778 m->twice_nat = twice_nat;
779 m->out2in_only = out2in_only;
782 m->local_port = l_port;
783 m->external_port = e_port;
790 .src_address = m->local_addr,
792 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
793 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
796 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
798 m_key.addr = m->local_addr;
799 m_key.port = m->local_port;
800 m_key.protocol = m->proto;
801 m_key.fib_index = m->fib_index;
802 kv.key = m_key.as_u64;
803 kv.value = m - sm->static_mappings;
805 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
806 if (twice_nat || out2in_only)
808 m_key.port = clib_host_to_net_u16 (l_port);
809 kv.key = m_key.as_u64;
811 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
812 clib_warning ("in2out key add failed");
815 m_key.addr = m->external_addr;
816 m_key.port = m->external_port;
817 m_key.fib_index = sm->outside_fib_index;
818 kv.key = m_key.as_u64;
819 kv.value = m - sm->static_mappings;
820 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
821 if (twice_nat || out2in_only)
823 m_key.port = clib_host_to_net_u16 (e_port);
824 kv.key = m_key.as_u64;
826 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
827 clib_warning ("out2in key add failed");
834 return VNET_API_ERROR_NO_SUCH_ENTRY;
836 /* Free external address port */
837 if (!(addr_only || sm->static_mapping_only || out2in_only))
839 for (i = 0; i < vec_len (sm->addresses); i++)
841 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
843 a = sm->addresses + i;
846 #define _(N, j, n, s) \
847 case SNAT_PROTOCOL_##N: \
848 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
851 a->busy_##n##_ports--; \
852 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
855 foreach_snat_protocol
858 clib_warning("unknown_protocol");
859 return VNET_API_ERROR_INVALID_VALUE_2;
866 if (sm->num_workers > 1)
867 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
869 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
871 m_key.addr = m->local_addr;
872 m_key.port = m->local_port;
873 m_key.protocol = m->proto;
874 m_key.fib_index = m->fib_index;
875 kv.key = m_key.as_u64;
877 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
878 if (twice_nat || out2in_only)
880 m_key.port = clib_host_to_net_u16 (m->local_port);
881 kv.key = m_key.as_u64;
883 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
884 clib_warning ("in2out key del failed");
887 m_key.addr = m->external_addr;
888 m_key.port = m->external_port;
889 m_key.fib_index = sm->outside_fib_index;
890 kv.key = m_key.as_u64;
891 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
892 if (twice_nat || out2in_only)
894 m_key.port = clib_host_to_net_u16 (m->external_port);
895 kv.key = m_key.as_u64;
897 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
898 clib_warning ("in2out key del failed");
901 /* Delete session(s) for static mapping if exist */
902 if (!(sm->static_mapping_only) ||
903 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
905 snat_user_key_t u_key;
907 dlist_elt_t * head, * elt;
908 u32 elt_index, head_index;
913 u_key.addr = m->local_addr;
914 u_key.fib_index = m->fib_index;
915 kv.key = u_key.as_u64;
916 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
918 user_index = value.value;
919 u = pool_elt_at_index (tsm->users, user_index);
920 if (u->nstaticsessions)
922 head_index = u->sessions_per_user_list_head_index;
923 head = pool_elt_at_index (tsm->list_pool, head_index);
924 elt_index = head->next;
925 elt = pool_elt_at_index (tsm->list_pool, elt_index);
926 ses_index = elt->value;
927 while (ses_index != ~0)
929 s = pool_elt_at_index (tsm->sessions, ses_index);
930 elt = pool_elt_at_index (tsm->list_pool, elt->next);
931 ses_index = elt->value;
935 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
936 (clib_net_to_host_u16 (s->out2in.port) != e_port))
940 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
941 clib_dlist_remove (tsm->list_pool, s->per_user_index);
942 pool_put_index (tsm->list_pool, s->per_user_index);
943 pool_put (tsm->sessions, s);
944 u->nstaticsessions--;
951 pool_put (tsm->users, u);
952 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
958 /* Delete static mapping from pool */
959 pool_put (sm->static_mappings, m);
962 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
965 /* Add/delete external address to FIB */
966 pool_foreach (interface, sm->interfaces,
968 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
971 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
974 pool_foreach (interface, sm->output_feature_interfaces,
976 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
979 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
986 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
987 snat_protocol_t proto, u32 vrf_id,
988 nat44_lb_addr_port_t *locals, u8 is_add,
989 u8 twice_nat, u8 out2in_only)
991 snat_main_t * sm = &snat_main;
992 snat_static_mapping_t *m;
993 snat_session_key_t m_key;
994 clib_bihash_kv_8_8_t kv, value;
996 snat_address_t *a = 0;
998 nat44_lb_addr_port_t *local;
999 u32 worker_index = 0, elt_index, head_index, ses_index;
1000 snat_main_per_thread_data_t *tsm;
1001 snat_user_key_t u_key;
1004 dlist_elt_t * head, * elt;
1006 m_key.addr = e_addr;
1007 m_key.port = e_port;
1008 m_key.protocol = proto;
1009 m_key.fib_index = sm->outside_fib_index;
1010 kv.key = m_key.as_u64;
1011 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1014 m = pool_elt_at_index (sm->static_mappings, value.value);
1019 return VNET_API_ERROR_VALUE_EXIST;
1021 if (vec_len (locals) < 2)
1022 return VNET_API_ERROR_INVALID_VALUE;
1024 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1026 FIB_SOURCE_PLUGIN_HI);
1028 /* Find external address in allocated addresses and reserve port for
1029 address and port pair mapping when dynamic translations enabled */
1030 if (!(sm->static_mapping_only || out2in_only))
1032 for (i = 0; i < vec_len (sm->addresses); i++)
1034 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1036 a = sm->addresses + i;
1037 /* External port must be unused */
1040 #define _(N, j, n, s) \
1041 case SNAT_PROTOCOL_##N: \
1042 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1043 return VNET_API_ERROR_INVALID_VALUE; \
1044 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1045 if (e_port > 1024) \
1047 a->busy_##n##_ports++; \
1048 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1051 foreach_snat_protocol
1054 clib_warning("unknown_protocol");
1055 return VNET_API_ERROR_INVALID_VALUE_2;
1060 /* External address must be allocated */
1062 return VNET_API_ERROR_NO_SUCH_ENTRY;
1065 pool_get (sm->static_mappings, m);
1066 memset (m, 0, sizeof (*m));
1067 m->external_addr = e_addr;
1070 m->fib_index = fib_index;
1071 m->external_port = e_port;
1073 m->twice_nat = twice_nat;
1074 m->out2in_only = out2in_only;
1076 m_key.addr = m->external_addr;
1077 m_key.port = m->external_port;
1078 m_key.protocol = m->proto;
1079 m_key.fib_index = sm->outside_fib_index;
1080 kv.key = m_key.as_u64;
1081 kv.value = m - sm->static_mappings;
1082 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1084 clib_warning ("static_mapping_by_external key add failed");
1085 return VNET_API_ERROR_UNSPECIFIED;
1091 worker_index = sm->first_worker_index +
1092 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1093 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
1094 m->worker_index = worker_index;
1097 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1099 m_key.port = clib_host_to_net_u16 (m->external_port);
1100 kv.key = m_key.as_u64;
1102 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
1104 clib_warning ("out2in key add failed");
1105 return VNET_API_ERROR_UNSPECIFIED;
1108 m_key.fib_index = m->fib_index;
1109 for (i = 0; i < vec_len (locals); i++)
1111 m_key.addr = locals[i].addr;
1114 m_key.port = locals[i].port;
1115 kv.key = m_key.as_u64;
1116 kv.value = m - sm->static_mappings;
1117 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1119 locals[i].prefix = (i == 0) ? locals[i].probability :\
1120 (locals[i - 1].prefix + locals[i].probability);
1121 vec_add1 (m->locals, locals[i]);
1123 m_key.port = clib_host_to_net_u16 (locals[i].port);
1124 kv.key = m_key.as_u64;
1126 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
1128 clib_warning ("in2out key add failed");
1129 return VNET_API_ERROR_UNSPECIFIED;
1136 return VNET_API_ERROR_NO_SUCH_ENTRY;
1138 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI);
1140 /* Free external address port */
1141 if (!(sm->static_mapping_only || out2in_only))
1143 for (i = 0; i < vec_len (sm->addresses); i++)
1145 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1147 a = sm->addresses + i;
1150 #define _(N, j, n, s) \
1151 case SNAT_PROTOCOL_##N: \
1152 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1153 if (e_port > 1024) \
1155 a->busy_##n##_ports--; \
1156 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1159 foreach_snat_protocol
1162 clib_warning("unknown_protocol");
1163 return VNET_API_ERROR_INVALID_VALUE_2;
1170 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1171 m_key.addr = m->external_addr;
1172 m_key.port = m->external_port;
1173 m_key.protocol = m->proto;
1174 m_key.fib_index = sm->outside_fib_index;
1175 kv.key = m_key.as_u64;
1176 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1178 clib_warning ("static_mapping_by_external key del failed");
1179 return VNET_API_ERROR_UNSPECIFIED;
1182 m_key.port = clib_host_to_net_u16 (m->external_port);
1183 kv.key = m_key.as_u64;
1184 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1186 clib_warning ("outi2in key del failed");
1187 return VNET_API_ERROR_UNSPECIFIED;
1190 vec_foreach (local, m->locals)
1192 m_key.addr = local->addr;
1195 m_key.port = local->port;
1196 m_key.fib_index = m->fib_index;
1197 kv.key = m_key.as_u64;
1198 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1200 clib_warning ("static_mapping_by_local key del failed");
1201 return VNET_API_ERROR_UNSPECIFIED;
1205 m_key.port = clib_host_to_net_u16 (local->port);
1206 kv.key = m_key.as_u64;
1207 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1209 clib_warning ("in2out key del failed");
1210 return VNET_API_ERROR_UNSPECIFIED;
1212 /* Delete sessions */
1213 u_key.addr = local->addr;
1214 u_key.fib_index = m->fib_index;
1215 kv.key = u_key.as_u64;
1216 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1218 u = pool_elt_at_index (tsm->users, value.value);
1219 if (u->nstaticsessions)
1221 head_index = u->sessions_per_user_list_head_index;
1222 head = pool_elt_at_index (tsm->list_pool, head_index);
1223 elt_index = head->next;
1224 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1225 ses_index = elt->value;
1226 while (ses_index != ~0)
1228 s = pool_elt_at_index (tsm->sessions, ses_index);
1229 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1230 ses_index = elt->value;
1232 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1233 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1236 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1237 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1238 pool_put_index (tsm->list_pool, s->per_user_index);
1239 pool_put (tsm->sessions, s);
1240 u->nstaticsessions--;
1245 vec_free(m->locals);
1247 pool_put (sm->static_mappings, m);
1254 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1257 snat_address_t *a = 0;
1258 snat_session_t *ses;
1259 u32 *ses_to_be_removed = 0, *ses_index;
1260 clib_bihash_kv_8_8_t kv, value;
1261 snat_user_key_t user_key;
1263 snat_main_per_thread_data_t *tsm;
1264 snat_static_mapping_t *m;
1265 snat_interface_t *interface;
1267 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1269 /* Find SNAT address */
1270 for (i=0; i < vec_len (addresses); i++)
1272 if (addresses[i].addr.as_u32 == addr.as_u32)
1279 return VNET_API_ERROR_NO_SUCH_ENTRY;
1283 pool_foreach (m, sm->static_mappings,
1285 if (m->external_addr.as_u32 == addr.as_u32)
1286 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1287 m->local_port, m->external_port,
1288 m->vrf_id, m->addr_only, ~0,
1289 m->proto, 0, m->twice_nat,
1295 /* Check if address is used in some static mapping */
1296 if (is_snat_address_used_in_static_mapping(sm, addr))
1298 clib_warning ("address used in static mapping");
1299 return VNET_API_ERROR_UNSPECIFIED;
1303 if (a->fib_index != ~0)
1304 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1305 FIB_SOURCE_PLUGIN_HI);
1307 /* Delete sessions using address */
1308 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1310 vec_foreach (tsm, sm->per_thread_data)
1312 pool_foreach (ses, tsm->sessions, ({
1313 if (ses->out2in.addr.as_u32 == addr.as_u32)
1315 ses->outside_address_index = ~0;
1316 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1317 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1318 pool_put_index (tsm->list_pool, ses->per_user_index);
1319 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1320 user_key.addr = ses->in2out.addr;
1321 user_key.fib_index = ses->in2out.fib_index;
1322 kv.key = user_key.as_u64;
1323 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1325 u = pool_elt_at_index (tsm->users, value.value);
1331 vec_foreach (ses_index, ses_to_be_removed)
1332 pool_put_index (tsm->sessions, ses_index[0]);
1334 vec_free (ses_to_be_removed);
1340 vec_del1 (sm->twice_nat_addresses, i);
1344 vec_del1 (sm->addresses, i);
1346 /* Delete external address from FIB */
1347 pool_foreach (interface, sm->interfaces,
1349 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1352 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1355 pool_foreach (interface, sm->output_feature_interfaces,
1357 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1360 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1367 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1369 snat_main_t *sm = &snat_main;
1370 snat_interface_t *i;
1371 const char * feature_name, *del_feature_name;
1372 snat_address_t * ap;
1373 snat_static_mapping_t * m;
1374 snat_det_map_t * dm;
1376 if (sm->out2in_dpo && !is_inside)
1377 return VNET_API_ERROR_UNSUPPORTED;
1379 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1380 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1383 if (sm->num_workers > 1 && !sm->deterministic)
1384 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1385 else if (sm->deterministic)
1386 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1388 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1391 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1392 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1394 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1395 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1397 pool_foreach (i, sm->interfaces,
1399 if (i->sw_if_index == sw_if_index)
1403 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1406 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1408 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1410 if (sm->num_workers > 1 && !sm->deterministic)
1412 del_feature_name = "nat44-handoff-classify";
1413 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1414 "nat44-out2in-worker-handoff";
1416 else if (sm->deterministic)
1418 del_feature_name = "nat44-det-classify";
1419 feature_name = !is_inside ? "nat44-det-in2out" :
1424 del_feature_name = "nat44-classify";
1425 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1428 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1429 sw_if_index, 0, 0, 0);
1430 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1431 sw_if_index, 1, 0, 0);
1435 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1436 sw_if_index, 0, 0, 0);
1437 pool_put (sm->interfaces, i);
1442 if ((nat_interface_is_inside(i) && is_inside) ||
1443 (nat_interface_is_outside(i) && !is_inside))
1446 if (sm->num_workers > 1 && !sm->deterministic)
1448 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1449 "nat44-out2in-worker-handoff";
1450 feature_name = "nat44-handoff-classify";
1452 else if (sm->deterministic)
1454 del_feature_name = !is_inside ? "nat44-det-in2out" :
1456 feature_name = "nat44-det-classify";
1460 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1461 feature_name = "nat44-classify";
1464 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1465 sw_if_index, 0, 0, 0);
1466 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1467 sw_if_index, 1, 0, 0);
1476 return VNET_API_ERROR_NO_SUCH_ENTRY;
1478 pool_get (sm->interfaces, i);
1479 i->sw_if_index = sw_if_index;
1481 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1485 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1487 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1489 /* Add/delete external addresses to FIB */
1491 if (is_inside && !sm->out2in_dpo)
1493 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1494 sw_if_index, !is_del, 0, 0);
1498 vec_foreach (ap, sm->addresses)
1499 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1501 pool_foreach (m, sm->static_mappings,
1503 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1506 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1509 pool_foreach (dm, sm->det_maps,
1511 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1517 int snat_interface_add_del_output_feature (u32 sw_if_index,
1521 snat_main_t *sm = &snat_main;
1522 snat_interface_t *i;
1523 snat_address_t * ap;
1524 snat_static_mapping_t * m;
1526 if (sm->deterministic ||
1527 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1528 return VNET_API_ERROR_UNSUPPORTED;
1532 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1533 sw_if_index, !is_del, 0, 0);
1534 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1535 sw_if_index, !is_del, 0, 0);
1539 if (sm->num_workers > 1)
1541 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1542 sw_if_index, !is_del, 0, 0);
1543 vnet_feature_enable_disable ("ip4-output",
1544 "nat44-in2out-output-worker-handoff",
1545 sw_if_index, !is_del, 0, 0);
1549 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1551 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1552 sw_if_index, !is_del, 0, 0);
1556 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1557 sm->fq_in2out_output_index =
1558 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1560 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1561 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1563 pool_foreach (i, sm->output_feature_interfaces,
1565 if (i->sw_if_index == sw_if_index)
1568 pool_put (sm->output_feature_interfaces, i);
1570 return VNET_API_ERROR_VALUE_EXIST;
1577 return VNET_API_ERROR_NO_SUCH_ENTRY;
1579 pool_get (sm->output_feature_interfaces, i);
1580 i->sw_if_index = sw_if_index;
1583 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1585 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1587 /* Add/delete external addresses to FIB */
1592 vec_foreach (ap, sm->addresses)
1593 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1595 pool_foreach (m, sm->static_mappings,
1597 if (!(m->addr_only))
1600 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1606 int snat_set_workers (uword * bitmap)
1608 snat_main_t *sm = &snat_main;
1611 if (sm->num_workers < 2)
1612 return VNET_API_ERROR_FEATURE_DISABLED;
1614 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1615 return VNET_API_ERROR_INVALID_WORKER;
1617 vec_free (sm->workers);
1618 clib_bitmap_foreach (i, bitmap,
1620 vec_add1(sm->workers, i);
1621 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1625 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1626 sm->num_snat_thread = _vec_len (sm->workers);
1633 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1636 ip4_address_t * address,
1638 u32 if_address_index,
1642 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1645 snat_session_key_t * k,
1646 u32 * address_indexp,
1647 u16 port_per_thread,
1648 u32 snat_thread_index);
1650 static clib_error_t * snat_init (vlib_main_t * vm)
1652 snat_main_t * sm = &snat_main;
1653 clib_error_t * error = 0;
1654 ip4_main_t * im = &ip4_main;
1655 ip_lookup_main_t * lm = &im->lookup_main;
1657 vlib_thread_registration_t *tr;
1658 vlib_thread_main_t *tm = vlib_get_thread_main ();
1661 ip4_add_del_interface_address_callback_t cb4;
1664 sm->vnet_main = vnet_get_main();
1666 sm->ip4_lookup_main = lm;
1667 sm->api_main = &api_main;
1668 sm->first_worker_index = 0;
1669 sm->next_worker = 0;
1670 sm->num_workers = 0;
1671 sm->num_snat_thread = 1;
1673 sm->port_per_thread = 0xffff - 1024;
1674 sm->fq_in2out_index = ~0;
1675 sm->fq_out2in_index = ~0;
1676 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1677 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1678 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1679 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1680 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1681 sm->forwarding_enabled = 0;
1683 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1686 tr = (vlib_thread_registration_t *) p[0];
1689 sm->num_workers = tr->count;
1690 sm->first_worker_index = tr->first_index;
1694 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1696 /* Use all available workers by default */
1697 if (sm->num_workers > 1)
1699 for (i=0; i < sm->num_workers; i++)
1700 bitmap = clib_bitmap_set (bitmap, i, 1);
1701 snat_set_workers(bitmap);
1702 clib_bitmap_free (bitmap);
1706 sm->per_thread_data[0].snat_thread_index = 0;
1709 error = snat_api_init(vm, sm);
1713 /* Set up the interface address add/del callback */
1714 cb4.function = snat_ip4_add_del_interface_address_cb;
1715 cb4.function_opaque = 0;
1717 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1719 nat_dpo_module_init ();
1721 /* Init IPFIX logging */
1722 snat_ipfix_logging_init(vm);
1725 error = nat64_init(vm);
1731 /* Init virtual fragmenentation reassembly */
1732 return nat_reass_init(vm);
1735 VLIB_INIT_FUNCTION (snat_init);
1737 void snat_free_outside_address_and_port (snat_address_t * addresses,
1739 snat_session_key_t * k,
1743 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1745 ASSERT (address_index < vec_len (addresses));
1747 a = addresses + address_index;
1749 switch (k->protocol)
1751 #define _(N, i, n, s) \
1752 case SNAT_PROTOCOL_##N: \
1753 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1754 port_host_byte_order) == 1); \
1755 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1756 port_host_byte_order, 0); \
1757 a->busy_##n##_ports--; \
1758 a->busy_##n##_ports_per_thread[thread_index]--; \
1760 foreach_snat_protocol
1763 clib_warning("unknown_protocol");
1769 * @brief Match NAT44 static mapping.
1771 * @param sm NAT main.
1772 * @param match Address and port to match.
1773 * @param mapping External or local address and port of the matched mapping.
1774 * @param by_external If 0 match by local address otherwise match by external
1776 * @param is_addr_only If matched mapping is address only
1777 * @param twice_nat If matched mapping is twice NAT.
1779 * @returns 0 if match found otherwise 1.
1781 int snat_static_mapping_match (snat_main_t * sm,
1782 snat_session_key_t match,
1783 snat_session_key_t * mapping,
1788 clib_bihash_kv_8_8_t kv, value;
1789 snat_static_mapping_t *m;
1790 snat_session_key_t m_key;
1791 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1792 u32 rand, lo = 0, hi, mid;
1795 mapping_hash = &sm->static_mapping_by_external;
1797 m_key.addr = match.addr;
1798 m_key.port = clib_net_to_host_u16 (match.port);
1799 m_key.protocol = match.protocol;
1800 m_key.fib_index = match.fib_index;
1802 kv.key = m_key.as_u64;
1804 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1806 /* Try address only mapping */
1809 kv.key = m_key.as_u64;
1810 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1814 m = pool_elt_at_index (sm->static_mappings, value.value);
1818 if (vec_len (m->locals))
1820 hi = vec_len (m->locals) - 1;
1821 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
1824 mid = ((hi - lo) >> 1) + lo;
1825 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
1827 if (!(m->locals[lo].prefix >= rand))
1829 mapping->addr = m->locals[lo].addr;
1830 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
1834 mapping->addr = m->local_addr;
1835 /* Address only mapping doesn't change port */
1836 mapping->port = m->addr_only ? match.port
1837 : clib_host_to_net_u16 (m->local_port);
1839 mapping->fib_index = m->fib_index;
1840 mapping->protocol = m->proto;
1844 mapping->addr = m->external_addr;
1845 /* Address only mapping doesn't change port */
1846 mapping->port = m->addr_only ? match.port
1847 : clib_host_to_net_u16 (m->external_port);
1848 mapping->fib_index = sm->outside_fib_index;
1851 if (PREDICT_FALSE(is_addr_only != 0))
1852 *is_addr_only = m->addr_only;
1854 if (PREDICT_FALSE(twice_nat != 0))
1855 *twice_nat = m->twice_nat;
1860 static_always_inline u16
1861 snat_random_port (u16 min, u16 max)
1863 snat_main_t *sm = &snat_main;
1864 return min + random_u32 (&sm->random_seed) /
1865 (random_u32_max() / (max - min + 1) + 1);
1869 snat_alloc_outside_address_and_port (snat_address_t * addresses,
1872 snat_session_key_t * k,
1873 u32 * address_indexp,
1874 u16 port_per_thread,
1875 u32 snat_thread_index)
1877 snat_main_t *sm = &snat_main;
1879 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
1880 address_indexp, port_per_thread,
1885 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1888 snat_session_key_t * k,
1889 u32 * address_indexp,
1890 u16 port_per_thread,
1891 u32 snat_thread_index)
1894 snat_address_t *a, *ga = 0;
1897 for (i = 0; i < vec_len (addresses); i++)
1900 switch (k->protocol)
1902 #define _(N, j, n, s) \
1903 case SNAT_PROTOCOL_##N: \
1904 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1906 if (a->fib_index == fib_index) \
1910 portnum = (port_per_thread * \
1911 snat_thread_index) + \
1912 snat_random_port(1, port_per_thread) + 1024; \
1913 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1915 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1916 a->busy_##n##_ports_per_thread[thread_index]++; \
1917 a->busy_##n##_ports++; \
1918 k->addr = a->addr; \
1919 k->port = clib_host_to_net_u16(portnum); \
1920 *address_indexp = i; \
1924 else if (a->fib_index == ~0) \
1931 foreach_snat_protocol
1934 clib_warning("unknown protocol");
1943 switch (k->protocol)
1945 #define _(N, j, n, s) \
1946 case SNAT_PROTOCOL_##N: \
1949 portnum = (port_per_thread * \
1950 snat_thread_index) + \
1951 snat_random_port(1, port_per_thread) + 1024; \
1952 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1954 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1955 a->busy_##n##_ports_per_thread[thread_index]++; \
1956 a->busy_##n##_ports++; \
1957 k->addr = a->addr; \
1958 k->port = clib_host_to_net_u16(portnum); \
1959 *address_indexp = gi; \
1963 foreach_snat_protocol
1966 clib_warning ("unknown protocol");
1971 /* Totally out of translations to use... */
1972 snat_ipfix_logging_addresses_exhausted(0);
1977 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
1980 snat_session_key_t * k,
1981 u32 * address_indexp,
1982 u16 port_per_thread,
1983 u32 snat_thread_index)
1985 snat_main_t *sm = &snat_main;
1986 snat_address_t *a = addresses;
1987 u16 m, ports, portnum, A, j;
1988 m = 16 - (sm->psid_offset + sm->psid_length);
1989 ports = (1 << (16 - sm->psid_length)) - (1 << m);
1991 if (!vec_len (addresses))
1994 switch (k->protocol)
1996 #define _(N, i, n, s) \
1997 case SNAT_PROTOCOL_##N: \
1998 if (a->busy_##n##_ports < ports) \
2002 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2003 j = snat_random_port(0, pow2_mask(m)); \
2004 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2005 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2007 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2008 a->busy_##n##_ports++; \
2009 k->addr = a->addr; \
2010 k->port = clib_host_to_net_u16 (portnum); \
2011 *address_indexp = i; \
2016 foreach_snat_protocol
2019 clib_warning("unknown protocol");
2024 /* Totally out of translations to use... */
2025 snat_ipfix_logging_addresses_exhausted(0);
2030 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2032 dpo_id_t dpo_v4 = DPO_INVALID;
2033 fib_prefix_t pfx = {
2034 .fp_proto = FIB_PROTOCOL_IP4,
2036 .fp_addr.ip4.as_u32 = addr.as_u32,
2041 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2042 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2043 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2044 dpo_reset (&dpo_v4);
2048 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2053 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2055 u32 *r = va_arg (*args, u32 *);
2058 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2059 foreach_snat_protocol
2067 format_snat_protocol (u8 * s, va_list * args)
2069 u32 i = va_arg (*args, u32);
2074 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2075 foreach_snat_protocol
2078 s = format (s, "unknown");
2081 s = format (s, "%s", t);
2086 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2088 snat_main_t *sm = &snat_main;
2089 u32 next_worker_index = 0;
2092 next_worker_index = sm->first_worker_index;
2093 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2094 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2096 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2097 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2099 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2101 return next_worker_index;
2105 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2107 snat_main_t *sm = &snat_main;
2110 snat_session_key_t m_key;
2111 clib_bihash_kv_8_8_t kv, value;
2112 snat_static_mapping_t *m;
2113 nat_ed_ses_key_t key;
2114 clib_bihash_kv_16_8_t s_kv, s_value;
2115 snat_main_per_thread_data_t *tsm;
2119 u32 next_worker_index = 0;
2121 /* first try static mappings without port */
2122 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2124 m_key.addr = ip0->dst_address;
2127 m_key.fib_index = rx_fib_index0;
2128 kv.key = m_key.as_u64;
2129 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2131 m = pool_elt_at_index (sm->static_mappings, value.value);
2132 return m->worker_index;
2136 proto = ip_proto_to_snat_proto (ip0->protocol);
2137 udp = ip4_next_header (ip0);
2138 port = udp->dst_port;
2140 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2142 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2143 return vlib_get_thread_index ();
2145 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2147 nat_reass_ip4_t *reass;
2149 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2150 ip0->fragment_id, ip0->protocol);
2152 if (reass && (reass->thread_index != (u32) ~ 0))
2153 return reass->thread_index;
2155 return vlib_get_thread_index ();
2159 /* unknown protocol */
2160 if (PREDICT_FALSE (proto == ~0))
2162 key.l_addr = ip0->dst_address;
2163 key.r_addr = ip0->src_address;
2164 key.fib_index = rx_fib_index0;
2165 key.proto = ip0->protocol;
2168 s_kv.key[0] = key.as_u64[0];
2169 s_kv.key[1] = key.as_u64[1];
2171 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
2173 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
2175 tsm = vec_elt_at_index (sm->per_thread_data, i);
2176 if (!pool_is_free_index(tsm->sessions, s_value.value))
2178 s = pool_elt_at_index (tsm->sessions, s_value.value);
2179 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2180 s->out2in.port == ip0->protocol &&
2181 snat_is_unk_proto_session (s))
2187 /* if no session use current thread */
2188 return vlib_get_thread_index ();
2191 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2193 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2194 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2195 if (!icmp_is_error_message (icmp))
2196 port = echo->identifier;
2199 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2200 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2201 void *l4_header = ip4_next_header (inner_ip);
2204 case SNAT_PROTOCOL_ICMP:
2205 icmp = (icmp46_header_t*)l4_header;
2206 echo = (icmp_echo_header_t *)(icmp + 1);
2207 port = echo->identifier;
2209 case SNAT_PROTOCOL_UDP:
2210 case SNAT_PROTOCOL_TCP:
2211 port = ((tcp_udp_header_t*)l4_header)->src_port;
2214 return vlib_get_thread_index ();
2219 /* try static mappings with port */
2220 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2222 m_key.addr = ip0->dst_address;
2223 m_key.port = clib_net_to_host_u16 (port);
2224 m_key.protocol = proto;
2225 m_key.fib_index = rx_fib_index0;
2226 kv.key = m_key.as_u64;
2227 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2229 m = pool_elt_at_index (sm->static_mappings, value.value);
2230 return m->worker_index;
2234 /* worker by outside port */
2235 next_worker_index = sm->first_worker_index;
2236 next_worker_index +=
2237 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2238 return next_worker_index;
2241 static clib_error_t *
2242 snat_config (vlib_main_t * vm, unformat_input_t * input)
2244 snat_main_t * sm = &snat_main;
2245 u32 translation_buckets = 1024;
2246 u32 translation_memory_size = 128<<20;
2247 u32 user_buckets = 128;
2248 u32 user_memory_size = 64<<20;
2249 u32 max_translations_per_user = 100;
2250 u32 outside_vrf_id = 0;
2251 u32 inside_vrf_id = 0;
2252 u32 static_mapping_buckets = 1024;
2253 u32 static_mapping_memory_size = 64<<20;
2254 u32 nat64_bib_buckets = 1024;
2255 u32 nat64_bib_memory_size = 128 << 20;
2256 u32 nat64_st_buckets = 2048;
2257 u32 nat64_st_memory_size = 256 << 20;
2258 u8 static_mapping_only = 0;
2259 u8 static_mapping_connection_tracking = 0;
2260 snat_main_per_thread_data_t *tsm;
2261 dslite_main_t * dm = &dslite_main;
2263 sm->deterministic = 0;
2266 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2268 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2270 else if (unformat (input, "translation hash memory %d",
2271 &translation_memory_size));
2272 else if (unformat (input, "user hash buckets %d", &user_buckets))
2274 else if (unformat (input, "user hash memory %d",
2277 else if (unformat (input, "max translations per user %d",
2278 &max_translations_per_user))
2280 else if (unformat (input, "outside VRF id %d",
2283 else if (unformat (input, "inside VRF id %d",
2286 else if (unformat (input, "static mapping only"))
2288 static_mapping_only = 1;
2289 if (unformat (input, "connection tracking"))
2290 static_mapping_connection_tracking = 1;
2292 else if (unformat (input, "deterministic"))
2293 sm->deterministic = 1;
2294 else if (unformat (input, "nat64 bib hash buckets %d",
2295 &nat64_bib_buckets))
2297 else if (unformat (input, "nat64 bib hash memory %d",
2298 &nat64_bib_memory_size))
2300 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2302 else if (unformat (input, "nat64 st hash memory %d",
2303 &nat64_st_memory_size))
2305 else if (unformat (input, "out2in dpo"))
2307 else if (unformat (input, "dslite ce"))
2308 dslite_set_ce(dm, 1);
2310 return clib_error_return (0, "unknown input '%U'",
2311 format_unformat_error, input);
2314 /* for show commands, etc. */
2315 sm->translation_buckets = translation_buckets;
2316 sm->translation_memory_size = translation_memory_size;
2317 /* do not exceed load factor 10 */
2318 sm->max_translations = 10 * translation_buckets;
2319 sm->user_buckets = user_buckets;
2320 sm->user_memory_size = user_memory_size;
2321 sm->max_translations_per_user = max_translations_per_user;
2322 sm->outside_vrf_id = outside_vrf_id;
2323 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2325 FIB_SOURCE_PLUGIN_HI);
2326 sm->inside_vrf_id = inside_vrf_id;
2327 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2329 FIB_SOURCE_PLUGIN_HI);
2330 sm->static_mapping_only = static_mapping_only;
2331 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2333 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2334 nat64_st_memory_size);
2336 if (sm->deterministic)
2338 sm->in2out_node_index = snat_det_in2out_node.index;
2339 sm->in2out_output_node_index = ~0;
2340 sm->out2in_node_index = snat_det_out2in_node.index;
2341 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2342 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2346 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2347 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2348 sm->in2out_node_index = snat_in2out_node.index;
2349 sm->in2out_output_node_index = snat_in2out_output_node.index;
2350 sm->out2in_node_index = snat_out2in_node.index;
2351 if (!static_mapping_only ||
2352 (static_mapping_only && static_mapping_connection_tracking))
2354 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2355 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2357 vec_foreach (tsm, sm->per_thread_data)
2359 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2360 translation_memory_size);
2362 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2363 translation_memory_size);
2365 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2369 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
2370 translation_buckets, translation_memory_size);
2372 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2373 translation_buckets, translation_memory_size);
2377 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2378 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2380 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2381 "static_mapping_by_local", static_mapping_buckets,
2382 static_mapping_memory_size);
2384 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2385 "static_mapping_by_external", static_mapping_buckets,
2386 static_mapping_memory_size);
2392 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2394 u8 * format_snat_session_state (u8 * s, va_list * args)
2396 u32 i = va_arg (*args, u32);
2401 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2402 foreach_snat_session_state
2405 t = format (t, "unknown");
2407 s = format (s, "%s", t);
2411 u8 * format_snat_key (u8 * s, va_list * args)
2413 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2415 s = format (s, "%U proto %U port %d fib %d",
2416 format_ip4_address, &key->addr,
2417 format_snat_protocol, key->protocol,
2418 clib_net_to_host_u16 (key->port), key->fib_index);
2422 u8 * format_snat_session (u8 * s, va_list * args)
2424 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2425 snat_session_t * sess = va_arg (*args, snat_session_t *);
2427 if (snat_is_unk_proto_session (sess))
2429 s = format (s, " i2o %U proto %u fib %u\n",
2430 format_ip4_address, &sess->in2out.addr,
2431 clib_net_to_host_u16 (sess->in2out.port),
2432 sess->in2out.fib_index);
2433 s = format (s, " o2i %U proto %u fib %u\n",
2434 format_ip4_address, &sess->out2in.addr,
2435 clib_net_to_host_u16 (sess->out2in.port),
2436 sess->out2in.fib_index);
2440 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2441 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2443 if (is_twice_nat_session (sess))
2445 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
2446 format_ip4_address, &sess->ext_host_addr,
2447 clib_net_to_host_u16 (sess->ext_host_port),
2448 format_ip4_address, &sess->ext_host_nat_addr,
2449 clib_net_to_host_u16 (sess->ext_host_nat_port));
2453 if (sess->ext_host_addr.as_u32)
2454 s = format (s, " external host %U\n",
2455 format_ip4_address, &sess->ext_host_addr);
2457 s = format (s, " last heard %.2f\n", sess->last_heard);
2458 s = format (s, " total pkts %d, total bytes %lld\n",
2459 sess->total_pkts, sess->total_bytes);
2460 if (snat_is_session_static (sess))
2461 s = format (s, " static translation\n");
2463 s = format (s, " dynamic translation\n");
2464 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2465 s = format (s, " load-balancing\n");
2466 if (is_twice_nat_session (sess))
2467 s = format (s, " twice-nat\n");
2472 u8 * format_snat_user (u8 * s, va_list * args)
2474 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2475 snat_user_t * u = va_arg (*args, snat_user_t *);
2476 int verbose = va_arg (*args, int);
2477 dlist_elt_t * head, * elt;
2478 u32 elt_index, head_index;
2480 snat_session_t * sess;
2482 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2483 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2488 if (u->nsessions || u->nstaticsessions)
2490 head_index = u->sessions_per_user_list_head_index;
2491 head = pool_elt_at_index (sm->list_pool, head_index);
2493 elt_index = head->next;
2494 elt = pool_elt_at_index (sm->list_pool, elt_index);
2495 session_index = elt->value;
2497 while (session_index != ~0)
2499 sess = pool_elt_at_index (sm->sessions, session_index);
2501 s = format (s, " %U\n", format_snat_session, sm, sess);
2503 elt_index = elt->next;
2504 elt = pool_elt_at_index (sm->list_pool, elt_index);
2505 session_index = elt->value;
2512 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2514 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2515 nat44_lb_addr_port_t *local;
2518 s = format (s, "local %U external %U vrf %d %s",
2519 format_ip4_address, &m->local_addr,
2520 format_ip4_address, &m->external_addr,
2521 m->vrf_id, m->twice_nat ? "twice-nat" : "");
2524 if (vec_len (m->locals))
2526 s = format (s, "%U vrf %d external %U:%d %s %s",
2527 format_snat_protocol, m->proto,
2529 format_ip4_address, &m->external_addr, m->external_port,
2530 m->twice_nat ? "twice-nat" : "",
2531 m->out2in_only ? "out2in-only" : "");
2532 vec_foreach (local, m->locals)
2533 s = format (s, "\n local %U:%d probability %d\%",
2534 format_ip4_address, &local->addr, local->port,
2535 local->probability);
2538 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
2539 format_snat_protocol, m->proto,
2540 format_ip4_address, &m->local_addr, m->local_port,
2541 format_ip4_address, &m->external_addr, m->external_port,
2542 m->vrf_id, m->twice_nat ? "twice-nat" : "",
2543 m->out2in_only ? "out2in-only" : "");
2548 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2550 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2551 vnet_main_t *vnm = vnet_get_main();
2554 s = format (s, "local %U external %U vrf %d",
2555 format_ip4_address, &m->l_addr,
2556 format_vnet_sw_interface_name, vnm,
2557 vnet_get_sw_interface (vnm, m->sw_if_index),
2560 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2561 format_snat_protocol, m->proto,
2562 format_ip4_address, &m->l_addr, m->l_port,
2563 format_vnet_sw_interface_name, vnm,
2564 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2570 u8 * format_det_map_ses (u8 * s, va_list * args)
2572 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2573 ip4_address_t in_addr, out_addr;
2574 u32 in_offset, out_offset;
2575 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2576 u32 * i = va_arg (*args, u32 *);
2578 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2579 in_addr.as_u32 = clib_host_to_net_u32 (
2580 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2581 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2582 clib_net_to_host_u32(det_map->in_addr.as_u32);
2583 out_offset = in_offset / det_map->sharing_ratio;
2584 out_addr.as_u32 = clib_host_to_net_u32(
2585 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2586 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2587 format_ip4_address, &in_addr,
2588 clib_net_to_host_u16 (ses->in_port),
2589 format_ip4_address, &out_addr,
2590 clib_net_to_host_u16 (ses->out.out_port),
2591 format_ip4_address, &ses->out.ext_host_addr,
2592 clib_net_to_host_u16 (ses->out.ext_host_port),
2593 format_snat_session_state, ses->state,
2600 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2603 ip4_address_t * address,
2605 u32 if_address_index,
2608 snat_main_t *sm = &snat_main;
2609 snat_static_map_resolve_t *rp;
2610 u32 *indices_to_delete = 0;
2611 ip4_address_t l_addr;
2615 snat_address_t *addresses = sm->addresses;
2617 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2619 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2623 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
2626 addresses = sm->twice_nat_addresses;
2627 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
2636 /* Don't trip over lease renewal, static config */
2637 for (j = 0; j < vec_len(addresses); j++)
2638 if (addresses[j].addr.as_u32 == address->as_u32)
2641 snat_add_address (sm, address, ~0, twice_nat);
2642 /* Scan static map resolution vector */
2643 for (j = 0; j < vec_len (sm->to_resolve); j++)
2645 rp = sm->to_resolve + j;
2646 /* On this interface? */
2647 if (rp->sw_if_index == sw_if_index)
2649 /* Indetity mapping? */
2650 if (rp->l_addr.as_u32 == 0)
2651 l_addr.as_u32 = address[0].as_u32;
2653 l_addr.as_u32 = rp->l_addr.as_u32;
2654 /* Add the static mapping */
2655 rv = snat_add_static_mapping (l_addr,
2661 ~0 /* sw_if_index */,
2666 clib_warning ("snat_add_static_mapping returned %d",
2668 vec_add1 (indices_to_delete, j);
2671 /* If we resolved any of the outstanding static mappings */
2672 if (vec_len(indices_to_delete))
2675 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2676 vec_delete(sm->to_resolve, 1, j);
2677 vec_free(indices_to_delete);
2683 (void) snat_del_address(sm, address[0], 1, twice_nat);
2689 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
2692 ip4_main_t * ip4_main = sm->ip4_main;
2693 ip4_address_t * first_int_addr;
2694 snat_static_map_resolve_t *rp;
2695 u32 *indices_to_delete = 0;
2697 u32 *auto_add_sw_if_indices =
2698 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
2700 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2701 0 /* just want the address*/);
2703 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
2705 if (auto_add_sw_if_indices[i] == sw_if_index)
2709 /* if have address remove it */
2711 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
2714 for (j = 0; j < vec_len (sm->to_resolve); j++)
2716 rp = sm->to_resolve + j;
2717 if (rp->sw_if_index == sw_if_index)
2718 vec_add1 (indices_to_delete, j);
2720 if (vec_len(indices_to_delete))
2722 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2723 vec_del1(sm->to_resolve, j);
2724 vec_free(indices_to_delete);
2728 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
2730 vec_del1(sm->auto_add_sw_if_indices, i);
2733 return VNET_API_ERROR_VALUE_EXIST;
2740 return VNET_API_ERROR_NO_SUCH_ENTRY;
2742 /* add to the auto-address list */
2744 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
2746 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2748 /* If the address is already bound - or static - add it now */
2750 snat_add_address (sm, first_int_addr, ~0, twice_nat);
2756 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
2757 snat_protocol_t proto, u32 vrf_id, int is_in)
2759 snat_main_per_thread_data_t *tsm;
2760 clib_bihash_kv_8_8_t kv, value;
2762 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2763 snat_session_key_t key;
2765 clib_bihash_8_8_t *t;
2766 snat_user_key_t u_key;
2769 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
2770 if (sm->num_workers)
2772 vec_elt_at_index (sm->per_thread_data,
2773 sm->worker_in2out_cb (&ip, fib_index));
2775 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
2777 key.addr.as_u32 = addr->as_u32;
2778 key.port = clib_host_to_net_u16 (port);
2779 key.protocol = proto;
2780 key.fib_index = fib_index;
2781 kv.key = key.as_u64;
2782 t = is_in ? &tsm->in2out : &tsm->out2in;
2783 if (!clib_bihash_search_8_8 (t, &kv, &value))
2785 s = pool_elt_at_index (tsm->sessions, value.value);
2786 kv.key = s->in2out.as_u64;
2787 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
2788 kv.key = s->out2in.as_u64;
2789 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
2790 u_key.addr = s->in2out.addr;
2791 u_key.fib_index = s->in2out.fib_index;
2792 kv.key = u_key.as_u64;
2793 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
2795 u = pool_elt_at_index (tsm->users, value.value);
2798 clib_dlist_remove (tsm->list_pool, s->per_user_index);
2799 pool_put (tsm->sessions, s);
2803 return VNET_API_ERROR_NO_SUCH_ENTRY;
2807 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
2809 snat_main_t *sm = &snat_main;
2811 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
2813 sm->psid_offset = psid_offset;
2814 sm->psid_length = psid_length;
2818 nat_set_alloc_addr_and_port_default (void)
2820 snat_main_t *sm = &snat_main;
2822 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;