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,
629 snat_static_map_resolve_t *rp;
631 vec_add2 (sm->to_resolve, rp, 1);
632 rp->l_addr.as_u32 = l_addr.as_u32;
634 rp->sw_if_index = sw_if_index;
638 rp->addr_only = addr_only;
640 rp->tag = vec_dup (tag);
644 * @brief Add static mapping.
646 * Create static mapping between local addr+port and external addr+port.
648 * @param l_addr Local IPv4 address.
649 * @param e_addr External IPv4 address.
650 * @param l_port Local port number.
651 * @param e_port External port number.
652 * @param vrf_id VRF ID.
653 * @param addr_only If 0 address port and pair mapping, otherwise address only.
654 * @param sw_if_index External port instead of specific IP address.
655 * @param is_add If 0 delete static mapping, otherwise add.
656 * @param twice_nat If 1 translate external host address and port.
657 * @param out2in_only If 1 rule match only out2in direction
658 * @param tag - opaque string tag
662 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
663 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
664 u32 sw_if_index, snat_protocol_t proto, int is_add,
665 u8 twice_nat, u8 out2in_only, u8 * tag)
667 snat_main_t * sm = &snat_main;
668 snat_static_mapping_t *m;
669 snat_session_key_t m_key;
670 clib_bihash_kv_8_8_t kv, value;
671 snat_address_t *a = 0;
674 snat_interface_t *interface;
676 snat_main_per_thread_data_t *tsm;
678 /* If the external address is a specific interface address */
679 if (sw_if_index != ~0)
681 ip4_address_t * first_int_addr;
683 /* Might be already set... */
684 first_int_addr = ip4_interface_first_address
685 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
687 /* DHCP resolution required? */
688 if (first_int_addr == 0)
690 snat_add_static_mapping_when_resolved
691 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
692 addr_only, is_add, tag);
697 e_addr.as_u32 = first_int_addr->as_u32;
698 /* Identity mapping? */
699 if (l_addr.as_u32 == 0)
700 l_addr.as_u32 = e_addr.as_u32;
705 m_key.port = addr_only ? 0 : e_port;
706 m_key.protocol = addr_only ? 0 : proto;
707 m_key.fib_index = sm->outside_fib_index;
708 kv.key = m_key.as_u64;
709 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
712 m = pool_elt_at_index (sm->static_mappings, value.value);
717 return VNET_API_ERROR_VALUE_EXIST;
719 if (twice_nat && addr_only)
720 return VNET_API_ERROR_UNSUPPORTED;
722 /* Convert VRF id to FIB index */
725 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
727 return VNET_API_ERROR_NO_SUCH_FIB;
730 /* If not specified use inside VRF id from SNAT plugin startup config */
733 fib_index = sm->inside_fib_index;
734 vrf_id = sm->inside_vrf_id;
737 /* Find external address in allocated addresses and reserve port for
738 address and port pair mapping when dynamic translations enabled */
739 if (!(addr_only || sm->static_mapping_only || out2in_only))
741 for (i = 0; i < vec_len (sm->addresses); i++)
743 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
745 a = sm->addresses + i;
746 /* External port must be unused */
749 #define _(N, j, n, s) \
750 case SNAT_PROTOCOL_##N: \
751 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
752 return VNET_API_ERROR_INVALID_VALUE; \
753 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
756 a->busy_##n##_ports++; \
757 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
760 foreach_snat_protocol
763 clib_warning("unknown_protocol");
764 return VNET_API_ERROR_INVALID_VALUE_2;
769 /* External address must be allocated */
771 return VNET_API_ERROR_NO_SUCH_ENTRY;
774 pool_get (sm->static_mappings, m);
775 memset (m, 0, sizeof (*m));
776 m->tag = vec_dup (tag);
777 m->local_addr = l_addr;
778 m->external_addr = e_addr;
779 m->addr_only = addr_only;
781 m->fib_index = fib_index;
782 m->twice_nat = twice_nat;
783 m->out2in_only = out2in_only;
786 m->local_port = l_port;
787 m->external_port = e_port;
794 .src_address = m->local_addr,
796 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
797 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
800 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
802 m_key.addr = m->local_addr;
803 m_key.port = m->local_port;
804 m_key.protocol = m->proto;
805 m_key.fib_index = m->fib_index;
806 kv.key = m_key.as_u64;
807 kv.value = m - sm->static_mappings;
809 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
810 if (twice_nat || out2in_only)
812 m_key.port = clib_host_to_net_u16 (l_port);
813 kv.key = m_key.as_u64;
815 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
816 clib_warning ("in2out key add failed");
819 m_key.addr = m->external_addr;
820 m_key.port = m->external_port;
821 m_key.fib_index = sm->outside_fib_index;
822 kv.key = m_key.as_u64;
823 kv.value = m - sm->static_mappings;
824 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
825 if (twice_nat || out2in_only)
827 m_key.port = clib_host_to_net_u16 (e_port);
828 kv.key = m_key.as_u64;
830 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
831 clib_warning ("out2in key add failed");
838 return VNET_API_ERROR_NO_SUCH_ENTRY;
840 /* Free external address port */
841 if (!(addr_only || sm->static_mapping_only || out2in_only))
843 for (i = 0; i < vec_len (sm->addresses); i++)
845 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
847 a = sm->addresses + i;
850 #define _(N, j, n, s) \
851 case SNAT_PROTOCOL_##N: \
852 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
855 a->busy_##n##_ports--; \
856 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
859 foreach_snat_protocol
862 clib_warning("unknown_protocol");
863 return VNET_API_ERROR_INVALID_VALUE_2;
870 if (sm->num_workers > 1)
871 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
873 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
875 m_key.addr = m->local_addr;
876 m_key.port = m->local_port;
877 m_key.protocol = m->proto;
878 m_key.fib_index = m->fib_index;
879 kv.key = m_key.as_u64;
881 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
882 if (twice_nat || out2in_only)
884 m_key.port = clib_host_to_net_u16 (m->local_port);
885 kv.key = m_key.as_u64;
887 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
888 clib_warning ("in2out key del failed");
891 m_key.addr = m->external_addr;
892 m_key.port = m->external_port;
893 m_key.fib_index = sm->outside_fib_index;
894 kv.key = m_key.as_u64;
895 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
896 if (twice_nat || out2in_only)
898 m_key.port = clib_host_to_net_u16 (m->external_port);
899 kv.key = m_key.as_u64;
901 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
902 clib_warning ("in2out key del failed");
905 /* Delete session(s) for static mapping if exist */
906 if (!(sm->static_mapping_only) ||
907 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
909 snat_user_key_t u_key;
911 dlist_elt_t * head, * elt;
912 u32 elt_index, head_index;
917 u_key.addr = m->local_addr;
918 u_key.fib_index = m->fib_index;
919 kv.key = u_key.as_u64;
920 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
922 user_index = value.value;
923 u = pool_elt_at_index (tsm->users, user_index);
924 if (u->nstaticsessions)
926 head_index = u->sessions_per_user_list_head_index;
927 head = pool_elt_at_index (tsm->list_pool, head_index);
928 elt_index = head->next;
929 elt = pool_elt_at_index (tsm->list_pool, elt_index);
930 ses_index = elt->value;
931 while (ses_index != ~0)
933 s = pool_elt_at_index (tsm->sessions, ses_index);
934 elt = pool_elt_at_index (tsm->list_pool, elt->next);
935 ses_index = elt->value;
939 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
940 (clib_net_to_host_u16 (s->out2in.port) != e_port))
944 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
945 clib_dlist_remove (tsm->list_pool, s->per_user_index);
946 pool_put_index (tsm->list_pool, s->per_user_index);
947 pool_put (tsm->sessions, s);
948 u->nstaticsessions--;
955 pool_put (tsm->users, u);
956 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
963 /* Delete static mapping from pool */
964 pool_put (sm->static_mappings, m);
967 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
970 /* Add/delete external address to FIB */
971 pool_foreach (interface, sm->interfaces,
973 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
976 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
979 pool_foreach (interface, sm->output_feature_interfaces,
981 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
984 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
991 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
992 snat_protocol_t proto, u32 vrf_id,
993 nat44_lb_addr_port_t *locals, u8 is_add,
994 u8 twice_nat, u8 out2in_only, u8 *tag)
996 snat_main_t * sm = &snat_main;
997 snat_static_mapping_t *m;
998 snat_session_key_t m_key;
999 clib_bihash_kv_8_8_t kv, value;
1001 snat_address_t *a = 0;
1003 nat44_lb_addr_port_t *local;
1004 u32 worker_index = 0, elt_index, head_index, ses_index;
1005 snat_main_per_thread_data_t *tsm;
1006 snat_user_key_t u_key;
1009 dlist_elt_t * head, * elt;
1011 m_key.addr = e_addr;
1012 m_key.port = e_port;
1013 m_key.protocol = proto;
1014 m_key.fib_index = sm->outside_fib_index;
1015 kv.key = m_key.as_u64;
1016 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1019 m = pool_elt_at_index (sm->static_mappings, value.value);
1024 return VNET_API_ERROR_VALUE_EXIST;
1026 if (vec_len (locals) < 2)
1027 return VNET_API_ERROR_INVALID_VALUE;
1029 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1031 FIB_SOURCE_PLUGIN_HI);
1033 /* Find external address in allocated addresses and reserve port for
1034 address and port pair mapping when dynamic translations enabled */
1035 if (!(sm->static_mapping_only || out2in_only))
1037 for (i = 0; i < vec_len (sm->addresses); i++)
1039 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1041 a = sm->addresses + i;
1042 /* External port must be unused */
1045 #define _(N, j, n, s) \
1046 case SNAT_PROTOCOL_##N: \
1047 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1048 return VNET_API_ERROR_INVALID_VALUE; \
1049 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1050 if (e_port > 1024) \
1052 a->busy_##n##_ports++; \
1053 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1056 foreach_snat_protocol
1059 clib_warning("unknown_protocol");
1060 return VNET_API_ERROR_INVALID_VALUE_2;
1065 /* External address must be allocated */
1067 return VNET_API_ERROR_NO_SUCH_ENTRY;
1070 pool_get (sm->static_mappings, m);
1071 memset (m, 0, sizeof (*m));
1072 m->tag = vec_dup (tag);
1073 m->external_addr = e_addr;
1076 m->fib_index = fib_index;
1077 m->external_port = e_port;
1079 m->twice_nat = twice_nat;
1080 m->out2in_only = out2in_only;
1082 m_key.addr = m->external_addr;
1083 m_key.port = m->external_port;
1084 m_key.protocol = m->proto;
1085 m_key.fib_index = sm->outside_fib_index;
1086 kv.key = m_key.as_u64;
1087 kv.value = m - sm->static_mappings;
1088 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1090 clib_warning ("static_mapping_by_external key add failed");
1091 return VNET_API_ERROR_UNSPECIFIED;
1097 worker_index = sm->first_worker_index +
1098 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1099 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
1100 m->worker_index = worker_index;
1103 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1105 m_key.port = clib_host_to_net_u16 (m->external_port);
1106 kv.key = m_key.as_u64;
1108 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
1110 clib_warning ("out2in key add failed");
1111 return VNET_API_ERROR_UNSPECIFIED;
1114 m_key.fib_index = m->fib_index;
1115 for (i = 0; i < vec_len (locals); i++)
1117 m_key.addr = locals[i].addr;
1120 m_key.port = locals[i].port;
1121 kv.key = m_key.as_u64;
1122 kv.value = m - sm->static_mappings;
1123 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1125 locals[i].prefix = (i == 0) ? locals[i].probability :\
1126 (locals[i - 1].prefix + locals[i].probability);
1127 vec_add1 (m->locals, locals[i]);
1129 m_key.port = clib_host_to_net_u16 (locals[i].port);
1130 kv.key = m_key.as_u64;
1132 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
1134 clib_warning ("in2out key add failed");
1135 return VNET_API_ERROR_UNSPECIFIED;
1142 return VNET_API_ERROR_NO_SUCH_ENTRY;
1144 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI);
1146 /* Free external address port */
1147 if (!(sm->static_mapping_only || out2in_only))
1149 for (i = 0; i < vec_len (sm->addresses); i++)
1151 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1153 a = sm->addresses + i;
1156 #define _(N, j, n, s) \
1157 case SNAT_PROTOCOL_##N: \
1158 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1159 if (e_port > 1024) \
1161 a->busy_##n##_ports--; \
1162 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1165 foreach_snat_protocol
1168 clib_warning("unknown_protocol");
1169 return VNET_API_ERROR_INVALID_VALUE_2;
1176 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1177 m_key.addr = m->external_addr;
1178 m_key.port = m->external_port;
1179 m_key.protocol = m->proto;
1180 m_key.fib_index = sm->outside_fib_index;
1181 kv.key = m_key.as_u64;
1182 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1184 clib_warning ("static_mapping_by_external key del failed");
1185 return VNET_API_ERROR_UNSPECIFIED;
1188 m_key.port = clib_host_to_net_u16 (m->external_port);
1189 kv.key = m_key.as_u64;
1190 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1192 clib_warning ("outi2in key del failed");
1193 return VNET_API_ERROR_UNSPECIFIED;
1196 vec_foreach (local, m->locals)
1198 m_key.addr = local->addr;
1201 m_key.port = local->port;
1202 m_key.fib_index = m->fib_index;
1203 kv.key = m_key.as_u64;
1204 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1206 clib_warning ("static_mapping_by_local key del failed");
1207 return VNET_API_ERROR_UNSPECIFIED;
1211 m_key.port = clib_host_to_net_u16 (local->port);
1212 kv.key = m_key.as_u64;
1213 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1215 clib_warning ("in2out key del failed");
1216 return VNET_API_ERROR_UNSPECIFIED;
1218 /* Delete sessions */
1219 u_key.addr = local->addr;
1220 u_key.fib_index = m->fib_index;
1221 kv.key = u_key.as_u64;
1222 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1224 u = pool_elt_at_index (tsm->users, value.value);
1225 if (u->nstaticsessions)
1227 head_index = u->sessions_per_user_list_head_index;
1228 head = pool_elt_at_index (tsm->list_pool, head_index);
1229 elt_index = head->next;
1230 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1231 ses_index = elt->value;
1232 while (ses_index != ~0)
1234 s = pool_elt_at_index (tsm->sessions, ses_index);
1235 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1236 ses_index = elt->value;
1238 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1239 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1242 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1243 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1244 pool_put_index (tsm->list_pool, s->per_user_index);
1245 pool_put (tsm->sessions, s);
1246 u->nstaticsessions--;
1251 vec_free(m->locals);
1254 pool_put (sm->static_mappings, m);
1261 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1264 snat_address_t *a = 0;
1265 snat_session_t *ses;
1266 u32 *ses_to_be_removed = 0, *ses_index;
1267 clib_bihash_kv_8_8_t kv, value;
1268 snat_user_key_t user_key;
1270 snat_main_per_thread_data_t *tsm;
1271 snat_static_mapping_t *m;
1272 snat_interface_t *interface;
1274 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1276 /* Find SNAT address */
1277 for (i=0; i < vec_len (addresses); i++)
1279 if (addresses[i].addr.as_u32 == addr.as_u32)
1286 return VNET_API_ERROR_NO_SUCH_ENTRY;
1290 pool_foreach (m, sm->static_mappings,
1292 if (m->external_addr.as_u32 == addr.as_u32)
1293 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1294 m->local_port, m->external_port,
1295 m->vrf_id, m->addr_only, ~0,
1296 m->proto, 0, m->twice_nat,
1297 m->out2in_only, m->tag);
1302 /* Check if address is used in some static mapping */
1303 if (is_snat_address_used_in_static_mapping(sm, addr))
1305 clib_warning ("address used in static mapping");
1306 return VNET_API_ERROR_UNSPECIFIED;
1310 if (a->fib_index != ~0)
1311 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1312 FIB_SOURCE_PLUGIN_HI);
1314 /* Delete sessions using address */
1315 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1317 vec_foreach (tsm, sm->per_thread_data)
1319 pool_foreach (ses, tsm->sessions, ({
1320 if (ses->out2in.addr.as_u32 == addr.as_u32)
1322 ses->outside_address_index = ~0;
1323 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1324 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1325 pool_put_index (tsm->list_pool, ses->per_user_index);
1326 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1327 user_key.addr = ses->in2out.addr;
1328 user_key.fib_index = ses->in2out.fib_index;
1329 kv.key = user_key.as_u64;
1330 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1332 u = pool_elt_at_index (tsm->users, value.value);
1338 vec_foreach (ses_index, ses_to_be_removed)
1339 pool_put_index (tsm->sessions, ses_index[0]);
1341 vec_free (ses_to_be_removed);
1347 vec_del1 (sm->twice_nat_addresses, i);
1351 vec_del1 (sm->addresses, i);
1353 /* Delete external address from FIB */
1354 pool_foreach (interface, sm->interfaces,
1356 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1359 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1362 pool_foreach (interface, sm->output_feature_interfaces,
1364 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1367 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1374 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1376 snat_main_t *sm = &snat_main;
1377 snat_interface_t *i;
1378 const char * feature_name, *del_feature_name;
1379 snat_address_t * ap;
1380 snat_static_mapping_t * m;
1381 snat_det_map_t * dm;
1383 if (sm->out2in_dpo && !is_inside)
1384 return VNET_API_ERROR_UNSUPPORTED;
1386 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1387 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1390 if (sm->num_workers > 1 && !sm->deterministic)
1391 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1392 else if (sm->deterministic)
1393 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1395 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1398 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1399 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1401 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1402 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1404 pool_foreach (i, sm->interfaces,
1406 if (i->sw_if_index == sw_if_index)
1410 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1413 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1415 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1417 if (sm->num_workers > 1 && !sm->deterministic)
1419 del_feature_name = "nat44-handoff-classify";
1420 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1421 "nat44-out2in-worker-handoff";
1423 else if (sm->deterministic)
1425 del_feature_name = "nat44-det-classify";
1426 feature_name = !is_inside ? "nat44-det-in2out" :
1431 del_feature_name = "nat44-classify";
1432 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1435 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1436 sw_if_index, 0, 0, 0);
1437 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1438 sw_if_index, 1, 0, 0);
1442 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1443 sw_if_index, 0, 0, 0);
1444 pool_put (sm->interfaces, i);
1449 if ((nat_interface_is_inside(i) && is_inside) ||
1450 (nat_interface_is_outside(i) && !is_inside))
1453 if (sm->num_workers > 1 && !sm->deterministic)
1455 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1456 "nat44-out2in-worker-handoff";
1457 feature_name = "nat44-handoff-classify";
1459 else if (sm->deterministic)
1461 del_feature_name = !is_inside ? "nat44-det-in2out" :
1463 feature_name = "nat44-det-classify";
1467 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1468 feature_name = "nat44-classify";
1471 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1472 sw_if_index, 0, 0, 0);
1473 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1474 sw_if_index, 1, 0, 0);
1483 return VNET_API_ERROR_NO_SUCH_ENTRY;
1485 pool_get (sm->interfaces, i);
1486 i->sw_if_index = sw_if_index;
1488 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1492 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1494 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1496 /* Add/delete external addresses to FIB */
1498 if (is_inside && !sm->out2in_dpo)
1500 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1501 sw_if_index, !is_del, 0, 0);
1505 vec_foreach (ap, sm->addresses)
1506 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1508 pool_foreach (m, sm->static_mappings,
1510 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1513 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1516 pool_foreach (dm, sm->det_maps,
1518 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1524 int snat_interface_add_del_output_feature (u32 sw_if_index,
1528 snat_main_t *sm = &snat_main;
1529 snat_interface_t *i;
1530 snat_address_t * ap;
1531 snat_static_mapping_t * m;
1533 if (sm->deterministic ||
1534 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1535 return VNET_API_ERROR_UNSUPPORTED;
1539 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1540 sw_if_index, !is_del, 0, 0);
1541 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1542 sw_if_index, !is_del, 0, 0);
1546 if (sm->num_workers > 1)
1548 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1549 sw_if_index, !is_del, 0, 0);
1550 vnet_feature_enable_disable ("ip4-output",
1551 "nat44-in2out-output-worker-handoff",
1552 sw_if_index, !is_del, 0, 0);
1556 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1558 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1559 sw_if_index, !is_del, 0, 0);
1563 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1564 sm->fq_in2out_output_index =
1565 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1567 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1568 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1570 pool_foreach (i, sm->output_feature_interfaces,
1572 if (i->sw_if_index == sw_if_index)
1575 pool_put (sm->output_feature_interfaces, i);
1577 return VNET_API_ERROR_VALUE_EXIST;
1584 return VNET_API_ERROR_NO_SUCH_ENTRY;
1586 pool_get (sm->output_feature_interfaces, i);
1587 i->sw_if_index = sw_if_index;
1590 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1592 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1594 /* Add/delete external addresses to FIB */
1599 vec_foreach (ap, sm->addresses)
1600 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1602 pool_foreach (m, sm->static_mappings,
1604 if (!(m->addr_only))
1607 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1613 int snat_set_workers (uword * bitmap)
1615 snat_main_t *sm = &snat_main;
1618 if (sm->num_workers < 2)
1619 return VNET_API_ERROR_FEATURE_DISABLED;
1621 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1622 return VNET_API_ERROR_INVALID_WORKER;
1624 vec_free (sm->workers);
1625 clib_bitmap_foreach (i, bitmap,
1627 vec_add1(sm->workers, i);
1628 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1632 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1633 sm->num_snat_thread = _vec_len (sm->workers);
1640 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1643 ip4_address_t * address,
1645 u32 if_address_index,
1649 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1652 snat_session_key_t * k,
1653 u32 * address_indexp,
1654 u16 port_per_thread,
1655 u32 snat_thread_index);
1657 static clib_error_t * snat_init (vlib_main_t * vm)
1659 snat_main_t * sm = &snat_main;
1660 clib_error_t * error = 0;
1661 ip4_main_t * im = &ip4_main;
1662 ip_lookup_main_t * lm = &im->lookup_main;
1664 vlib_thread_registration_t *tr;
1665 vlib_thread_main_t *tm = vlib_get_thread_main ();
1668 ip4_add_del_interface_address_callback_t cb4;
1671 sm->vnet_main = vnet_get_main();
1673 sm->ip4_lookup_main = lm;
1674 sm->api_main = &api_main;
1675 sm->first_worker_index = 0;
1676 sm->next_worker = 0;
1677 sm->num_workers = 0;
1678 sm->num_snat_thread = 1;
1680 sm->port_per_thread = 0xffff - 1024;
1681 sm->fq_in2out_index = ~0;
1682 sm->fq_out2in_index = ~0;
1683 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1684 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1685 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1686 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1687 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1688 sm->forwarding_enabled = 0;
1690 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1693 tr = (vlib_thread_registration_t *) p[0];
1696 sm->num_workers = tr->count;
1697 sm->first_worker_index = tr->first_index;
1701 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1703 /* Use all available workers by default */
1704 if (sm->num_workers > 1)
1706 for (i=0; i < sm->num_workers; i++)
1707 bitmap = clib_bitmap_set (bitmap, i, 1);
1708 snat_set_workers(bitmap);
1709 clib_bitmap_free (bitmap);
1713 sm->per_thread_data[0].snat_thread_index = 0;
1716 error = snat_api_init(vm, sm);
1720 /* Set up the interface address add/del callback */
1721 cb4.function = snat_ip4_add_del_interface_address_cb;
1722 cb4.function_opaque = 0;
1724 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1726 nat_dpo_module_init ();
1728 /* Init IPFIX logging */
1729 snat_ipfix_logging_init(vm);
1732 error = nat64_init(vm);
1738 /* Init virtual fragmenentation reassembly */
1739 return nat_reass_init(vm);
1742 VLIB_INIT_FUNCTION (snat_init);
1744 void snat_free_outside_address_and_port (snat_address_t * addresses,
1746 snat_session_key_t * k,
1750 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1752 ASSERT (address_index < vec_len (addresses));
1754 a = addresses + address_index;
1756 switch (k->protocol)
1758 #define _(N, i, n, s) \
1759 case SNAT_PROTOCOL_##N: \
1760 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1761 port_host_byte_order) == 1); \
1762 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1763 port_host_byte_order, 0); \
1764 a->busy_##n##_ports--; \
1765 a->busy_##n##_ports_per_thread[thread_index]--; \
1767 foreach_snat_protocol
1770 clib_warning("unknown_protocol");
1776 * @brief Match NAT44 static mapping.
1778 * @param sm NAT main.
1779 * @param match Address and port to match.
1780 * @param mapping External or local address and port of the matched mapping.
1781 * @param by_external If 0 match by local address otherwise match by external
1783 * @param is_addr_only If matched mapping is address only
1784 * @param twice_nat If matched mapping is twice NAT.
1786 * @returns 0 if match found otherwise 1.
1788 int snat_static_mapping_match (snat_main_t * sm,
1789 snat_session_key_t match,
1790 snat_session_key_t * mapping,
1795 clib_bihash_kv_8_8_t kv, value;
1796 snat_static_mapping_t *m;
1797 snat_session_key_t m_key;
1798 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1799 u32 rand, lo = 0, hi, mid;
1802 mapping_hash = &sm->static_mapping_by_external;
1804 m_key.addr = match.addr;
1805 m_key.port = clib_net_to_host_u16 (match.port);
1806 m_key.protocol = match.protocol;
1807 m_key.fib_index = match.fib_index;
1809 kv.key = m_key.as_u64;
1811 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1813 /* Try address only mapping */
1816 kv.key = m_key.as_u64;
1817 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1821 m = pool_elt_at_index (sm->static_mappings, value.value);
1825 if (vec_len (m->locals))
1827 hi = vec_len (m->locals) - 1;
1828 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
1831 mid = ((hi - lo) >> 1) + lo;
1832 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
1834 if (!(m->locals[lo].prefix >= rand))
1836 mapping->addr = m->locals[lo].addr;
1837 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
1841 mapping->addr = m->local_addr;
1842 /* Address only mapping doesn't change port */
1843 mapping->port = m->addr_only ? match.port
1844 : clib_host_to_net_u16 (m->local_port);
1846 mapping->fib_index = m->fib_index;
1847 mapping->protocol = m->proto;
1851 mapping->addr = m->external_addr;
1852 /* Address only mapping doesn't change port */
1853 mapping->port = m->addr_only ? match.port
1854 : clib_host_to_net_u16 (m->external_port);
1855 mapping->fib_index = sm->outside_fib_index;
1858 if (PREDICT_FALSE(is_addr_only != 0))
1859 *is_addr_only = m->addr_only;
1861 if (PREDICT_FALSE(twice_nat != 0))
1862 *twice_nat = m->twice_nat;
1867 static_always_inline u16
1868 snat_random_port (u16 min, u16 max)
1870 snat_main_t *sm = &snat_main;
1871 return min + random_u32 (&sm->random_seed) /
1872 (random_u32_max() / (max - min + 1) + 1);
1876 snat_alloc_outside_address_and_port (snat_address_t * addresses,
1879 snat_session_key_t * k,
1880 u32 * address_indexp,
1881 u16 port_per_thread,
1882 u32 snat_thread_index)
1884 snat_main_t *sm = &snat_main;
1886 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
1887 address_indexp, port_per_thread,
1892 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1895 snat_session_key_t * k,
1896 u32 * address_indexp,
1897 u16 port_per_thread,
1898 u32 snat_thread_index)
1901 snat_address_t *a, *ga = 0;
1904 for (i = 0; i < vec_len (addresses); i++)
1907 switch (k->protocol)
1909 #define _(N, j, n, s) \
1910 case SNAT_PROTOCOL_##N: \
1911 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1913 if (a->fib_index == fib_index) \
1917 portnum = (port_per_thread * \
1918 snat_thread_index) + \
1919 snat_random_port(1, port_per_thread) + 1024; \
1920 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1922 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1923 a->busy_##n##_ports_per_thread[thread_index]++; \
1924 a->busy_##n##_ports++; \
1925 k->addr = a->addr; \
1926 k->port = clib_host_to_net_u16(portnum); \
1927 *address_indexp = i; \
1931 else if (a->fib_index == ~0) \
1938 foreach_snat_protocol
1941 clib_warning("unknown protocol");
1950 switch (k->protocol)
1952 #define _(N, j, n, s) \
1953 case SNAT_PROTOCOL_##N: \
1956 portnum = (port_per_thread * \
1957 snat_thread_index) + \
1958 snat_random_port(1, port_per_thread) + 1024; \
1959 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1961 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1962 a->busy_##n##_ports_per_thread[thread_index]++; \
1963 a->busy_##n##_ports++; \
1964 k->addr = a->addr; \
1965 k->port = clib_host_to_net_u16(portnum); \
1966 *address_indexp = gi; \
1970 foreach_snat_protocol
1973 clib_warning ("unknown protocol");
1978 /* Totally out of translations to use... */
1979 snat_ipfix_logging_addresses_exhausted(0);
1984 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
1987 snat_session_key_t * k,
1988 u32 * address_indexp,
1989 u16 port_per_thread,
1990 u32 snat_thread_index)
1992 snat_main_t *sm = &snat_main;
1993 snat_address_t *a = addresses;
1994 u16 m, ports, portnum, A, j;
1995 m = 16 - (sm->psid_offset + sm->psid_length);
1996 ports = (1 << (16 - sm->psid_length)) - (1 << m);
1998 if (!vec_len (addresses))
2001 switch (k->protocol)
2003 #define _(N, i, n, s) \
2004 case SNAT_PROTOCOL_##N: \
2005 if (a->busy_##n##_ports < ports) \
2009 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2010 j = snat_random_port(0, pow2_mask(m)); \
2011 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2012 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2014 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2015 a->busy_##n##_ports++; \
2016 k->addr = a->addr; \
2017 k->port = clib_host_to_net_u16 (portnum); \
2018 *address_indexp = i; \
2023 foreach_snat_protocol
2026 clib_warning("unknown protocol");
2031 /* Totally out of translations to use... */
2032 snat_ipfix_logging_addresses_exhausted(0);
2037 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2039 dpo_id_t dpo_v4 = DPO_INVALID;
2040 fib_prefix_t pfx = {
2041 .fp_proto = FIB_PROTOCOL_IP4,
2043 .fp_addr.ip4.as_u32 = addr.as_u32,
2048 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2049 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2050 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2051 dpo_reset (&dpo_v4);
2055 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2060 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2062 u32 *r = va_arg (*args, u32 *);
2065 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2066 foreach_snat_protocol
2074 format_snat_protocol (u8 * s, va_list * args)
2076 u32 i = va_arg (*args, u32);
2081 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2082 foreach_snat_protocol
2085 s = format (s, "unknown");
2088 s = format (s, "%s", t);
2093 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2095 snat_main_t *sm = &snat_main;
2096 u32 next_worker_index = 0;
2099 next_worker_index = sm->first_worker_index;
2100 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2101 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2103 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2104 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2106 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2108 return next_worker_index;
2112 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2114 snat_main_t *sm = &snat_main;
2117 snat_session_key_t m_key;
2118 clib_bihash_kv_8_8_t kv, value;
2119 snat_static_mapping_t *m;
2120 nat_ed_ses_key_t key;
2121 clib_bihash_kv_16_8_t s_kv, s_value;
2122 snat_main_per_thread_data_t *tsm;
2126 u32 next_worker_index = 0;
2128 /* first try static mappings without port */
2129 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2131 m_key.addr = ip0->dst_address;
2134 m_key.fib_index = rx_fib_index0;
2135 kv.key = m_key.as_u64;
2136 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2138 m = pool_elt_at_index (sm->static_mappings, value.value);
2139 return m->worker_index;
2143 proto = ip_proto_to_snat_proto (ip0->protocol);
2144 udp = ip4_next_header (ip0);
2145 port = udp->dst_port;
2147 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2149 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2150 return vlib_get_thread_index ();
2152 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2154 nat_reass_ip4_t *reass;
2156 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2157 ip0->fragment_id, ip0->protocol);
2159 if (reass && (reass->thread_index != (u32) ~ 0))
2160 return reass->thread_index;
2162 return vlib_get_thread_index ();
2166 /* unknown protocol */
2167 if (PREDICT_FALSE (proto == ~0))
2169 key.l_addr = ip0->dst_address;
2170 key.r_addr = ip0->src_address;
2171 key.fib_index = rx_fib_index0;
2172 key.proto = ip0->protocol;
2175 s_kv.key[0] = key.as_u64[0];
2176 s_kv.key[1] = key.as_u64[1];
2178 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
2180 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
2182 tsm = vec_elt_at_index (sm->per_thread_data, i);
2183 if (!pool_is_free_index(tsm->sessions, s_value.value))
2185 s = pool_elt_at_index (tsm->sessions, s_value.value);
2186 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2187 s->out2in.port == ip0->protocol &&
2188 snat_is_unk_proto_session (s))
2194 /* if no session use current thread */
2195 return vlib_get_thread_index ();
2198 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2200 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2201 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2202 if (!icmp_is_error_message (icmp))
2203 port = echo->identifier;
2206 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2207 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2208 void *l4_header = ip4_next_header (inner_ip);
2211 case SNAT_PROTOCOL_ICMP:
2212 icmp = (icmp46_header_t*)l4_header;
2213 echo = (icmp_echo_header_t *)(icmp + 1);
2214 port = echo->identifier;
2216 case SNAT_PROTOCOL_UDP:
2217 case SNAT_PROTOCOL_TCP:
2218 port = ((tcp_udp_header_t*)l4_header)->src_port;
2221 return vlib_get_thread_index ();
2226 /* try static mappings with port */
2227 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2229 m_key.addr = ip0->dst_address;
2230 m_key.port = clib_net_to_host_u16 (port);
2231 m_key.protocol = proto;
2232 m_key.fib_index = rx_fib_index0;
2233 kv.key = m_key.as_u64;
2234 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2236 m = pool_elt_at_index (sm->static_mappings, value.value);
2237 return m->worker_index;
2241 /* worker by outside port */
2242 next_worker_index = sm->first_worker_index;
2243 next_worker_index +=
2244 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2245 return next_worker_index;
2248 static clib_error_t *
2249 snat_config (vlib_main_t * vm, unformat_input_t * input)
2251 snat_main_t * sm = &snat_main;
2252 u32 translation_buckets = 1024;
2253 u32 translation_memory_size = 128<<20;
2254 u32 user_buckets = 128;
2255 u32 user_memory_size = 64<<20;
2256 u32 max_translations_per_user = 100;
2257 u32 outside_vrf_id = 0;
2258 u32 inside_vrf_id = 0;
2259 u32 static_mapping_buckets = 1024;
2260 u32 static_mapping_memory_size = 64<<20;
2261 u32 nat64_bib_buckets = 1024;
2262 u32 nat64_bib_memory_size = 128 << 20;
2263 u32 nat64_st_buckets = 2048;
2264 u32 nat64_st_memory_size = 256 << 20;
2265 u8 static_mapping_only = 0;
2266 u8 static_mapping_connection_tracking = 0;
2267 snat_main_per_thread_data_t *tsm;
2268 dslite_main_t * dm = &dslite_main;
2270 sm->deterministic = 0;
2273 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2275 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2277 else if (unformat (input, "translation hash memory %d",
2278 &translation_memory_size));
2279 else if (unformat (input, "user hash buckets %d", &user_buckets))
2281 else if (unformat (input, "user hash memory %d",
2284 else if (unformat (input, "max translations per user %d",
2285 &max_translations_per_user))
2287 else if (unformat (input, "outside VRF id %d",
2290 else if (unformat (input, "inside VRF id %d",
2293 else if (unformat (input, "static mapping only"))
2295 static_mapping_only = 1;
2296 if (unformat (input, "connection tracking"))
2297 static_mapping_connection_tracking = 1;
2299 else if (unformat (input, "deterministic"))
2300 sm->deterministic = 1;
2301 else if (unformat (input, "nat64 bib hash buckets %d",
2302 &nat64_bib_buckets))
2304 else if (unformat (input, "nat64 bib hash memory %d",
2305 &nat64_bib_memory_size))
2307 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2309 else if (unformat (input, "nat64 st hash memory %d",
2310 &nat64_st_memory_size))
2312 else if (unformat (input, "out2in dpo"))
2314 else if (unformat (input, "dslite ce"))
2315 dslite_set_ce(dm, 1);
2317 return clib_error_return (0, "unknown input '%U'",
2318 format_unformat_error, input);
2321 /* for show commands, etc. */
2322 sm->translation_buckets = translation_buckets;
2323 sm->translation_memory_size = translation_memory_size;
2324 /* do not exceed load factor 10 */
2325 sm->max_translations = 10 * translation_buckets;
2326 sm->user_buckets = user_buckets;
2327 sm->user_memory_size = user_memory_size;
2328 sm->max_translations_per_user = max_translations_per_user;
2329 sm->outside_vrf_id = outside_vrf_id;
2330 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2332 FIB_SOURCE_PLUGIN_HI);
2333 sm->inside_vrf_id = inside_vrf_id;
2334 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2336 FIB_SOURCE_PLUGIN_HI);
2337 sm->static_mapping_only = static_mapping_only;
2338 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2340 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2341 nat64_st_memory_size);
2343 if (sm->deterministic)
2345 sm->in2out_node_index = snat_det_in2out_node.index;
2346 sm->in2out_output_node_index = ~0;
2347 sm->out2in_node_index = snat_det_out2in_node.index;
2348 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2349 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2353 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2354 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2355 sm->in2out_node_index = snat_in2out_node.index;
2356 sm->in2out_output_node_index = snat_in2out_output_node.index;
2357 sm->out2in_node_index = snat_out2in_node.index;
2358 if (!static_mapping_only ||
2359 (static_mapping_only && static_mapping_connection_tracking))
2361 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2362 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2364 vec_foreach (tsm, sm->per_thread_data)
2366 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2367 translation_memory_size);
2369 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2370 translation_memory_size);
2372 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2376 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
2377 translation_buckets, translation_memory_size);
2379 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2380 translation_buckets, translation_memory_size);
2384 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2385 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2387 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2388 "static_mapping_by_local", static_mapping_buckets,
2389 static_mapping_memory_size);
2391 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2392 "static_mapping_by_external", static_mapping_buckets,
2393 static_mapping_memory_size);
2399 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2401 u8 * format_snat_session_state (u8 * s, va_list * args)
2403 u32 i = va_arg (*args, u32);
2408 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2409 foreach_snat_session_state
2412 t = format (t, "unknown");
2414 s = format (s, "%s", t);
2418 u8 * format_snat_key (u8 * s, va_list * args)
2420 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2422 s = format (s, "%U proto %U port %d fib %d",
2423 format_ip4_address, &key->addr,
2424 format_snat_protocol, key->protocol,
2425 clib_net_to_host_u16 (key->port), key->fib_index);
2429 u8 * format_snat_session (u8 * s, va_list * args)
2431 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2432 snat_session_t * sess = va_arg (*args, snat_session_t *);
2434 if (snat_is_unk_proto_session (sess))
2436 s = format (s, " i2o %U proto %u fib %u\n",
2437 format_ip4_address, &sess->in2out.addr,
2438 clib_net_to_host_u16 (sess->in2out.port),
2439 sess->in2out.fib_index);
2440 s = format (s, " o2i %U proto %u fib %u\n",
2441 format_ip4_address, &sess->out2in.addr,
2442 clib_net_to_host_u16 (sess->out2in.port),
2443 sess->out2in.fib_index);
2447 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2448 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2450 if (is_twice_nat_session (sess))
2452 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
2453 format_ip4_address, &sess->ext_host_addr,
2454 clib_net_to_host_u16 (sess->ext_host_port),
2455 format_ip4_address, &sess->ext_host_nat_addr,
2456 clib_net_to_host_u16 (sess->ext_host_nat_port));
2460 if (sess->ext_host_addr.as_u32)
2461 s = format (s, " external host %U\n",
2462 format_ip4_address, &sess->ext_host_addr);
2464 s = format (s, " last heard %.2f\n", sess->last_heard);
2465 s = format (s, " total pkts %d, total bytes %lld\n",
2466 sess->total_pkts, sess->total_bytes);
2467 if (snat_is_session_static (sess))
2468 s = format (s, " static translation\n");
2470 s = format (s, " dynamic translation\n");
2471 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2472 s = format (s, " load-balancing\n");
2473 if (is_twice_nat_session (sess))
2474 s = format (s, " twice-nat\n");
2479 u8 * format_snat_user (u8 * s, va_list * args)
2481 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2482 snat_user_t * u = va_arg (*args, snat_user_t *);
2483 int verbose = va_arg (*args, int);
2484 dlist_elt_t * head, * elt;
2485 u32 elt_index, head_index;
2487 snat_session_t * sess;
2489 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2490 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2495 if (u->nsessions || u->nstaticsessions)
2497 head_index = u->sessions_per_user_list_head_index;
2498 head = pool_elt_at_index (sm->list_pool, head_index);
2500 elt_index = head->next;
2501 elt = pool_elt_at_index (sm->list_pool, elt_index);
2502 session_index = elt->value;
2504 while (session_index != ~0)
2506 sess = pool_elt_at_index (sm->sessions, session_index);
2508 s = format (s, " %U\n", format_snat_session, sm, sess);
2510 elt_index = elt->next;
2511 elt = pool_elt_at_index (sm->list_pool, elt_index);
2512 session_index = elt->value;
2519 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2521 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2522 nat44_lb_addr_port_t *local;
2525 s = format (s, "local %U external %U vrf %d %s",
2526 format_ip4_address, &m->local_addr,
2527 format_ip4_address, &m->external_addr,
2528 m->vrf_id, m->twice_nat ? "twice-nat" : "");
2531 if (vec_len (m->locals))
2533 s = format (s, "%U vrf %d external %U:%d %s %s",
2534 format_snat_protocol, m->proto,
2536 format_ip4_address, &m->external_addr, m->external_port,
2537 m->twice_nat ? "twice-nat" : "",
2538 m->out2in_only ? "out2in-only" : "");
2539 vec_foreach (local, m->locals)
2540 s = format (s, "\n local %U:%d probability %d\%",
2541 format_ip4_address, &local->addr, local->port,
2542 local->probability);
2545 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
2546 format_snat_protocol, m->proto,
2547 format_ip4_address, &m->local_addr, m->local_port,
2548 format_ip4_address, &m->external_addr, m->external_port,
2549 m->vrf_id, m->twice_nat ? "twice-nat" : "",
2550 m->out2in_only ? "out2in-only" : "");
2555 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2557 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2558 vnet_main_t *vnm = vnet_get_main();
2561 s = format (s, "local %U external %U vrf %d",
2562 format_ip4_address, &m->l_addr,
2563 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2566 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2567 format_snat_protocol, m->proto,
2568 format_ip4_address, &m->l_addr, m->l_port,
2569 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2570 m->e_port, m->vrf_id);
2575 u8 * format_det_map_ses (u8 * s, va_list * args)
2577 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2578 ip4_address_t in_addr, out_addr;
2579 u32 in_offset, out_offset;
2580 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2581 u32 * i = va_arg (*args, u32 *);
2583 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2584 in_addr.as_u32 = clib_host_to_net_u32 (
2585 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2586 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2587 clib_net_to_host_u32(det_map->in_addr.as_u32);
2588 out_offset = in_offset / det_map->sharing_ratio;
2589 out_addr.as_u32 = clib_host_to_net_u32(
2590 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2591 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2592 format_ip4_address, &in_addr,
2593 clib_net_to_host_u16 (ses->in_port),
2594 format_ip4_address, &out_addr,
2595 clib_net_to_host_u16 (ses->out.out_port),
2596 format_ip4_address, &ses->out.ext_host_addr,
2597 clib_net_to_host_u16 (ses->out.ext_host_port),
2598 format_snat_session_state, ses->state,
2605 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2608 ip4_address_t * address,
2610 u32 if_address_index,
2613 snat_main_t *sm = &snat_main;
2614 snat_static_map_resolve_t *rp;
2615 u32 *indices_to_delete = 0;
2616 ip4_address_t l_addr;
2620 snat_address_t *addresses = sm->addresses;
2622 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2624 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2628 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
2631 addresses = sm->twice_nat_addresses;
2632 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
2641 /* Don't trip over lease renewal, static config */
2642 for (j = 0; j < vec_len(addresses); j++)
2643 if (addresses[j].addr.as_u32 == address->as_u32)
2646 snat_add_address (sm, address, ~0, twice_nat);
2647 /* Scan static map resolution vector */
2648 for (j = 0; j < vec_len (sm->to_resolve); j++)
2650 rp = sm->to_resolve + j;
2651 /* On this interface? */
2652 if (rp->sw_if_index == sw_if_index)
2654 /* Indetity mapping? */
2655 if (rp->l_addr.as_u32 == 0)
2656 l_addr.as_u32 = address[0].as_u32;
2658 l_addr.as_u32 = rp->l_addr.as_u32;
2659 /* Add the static mapping */
2660 rv = snat_add_static_mapping (l_addr,
2666 ~0 /* sw_if_index */,
2671 clib_warning ("snat_add_static_mapping returned %d",
2674 vec_add1 (indices_to_delete, j);
2677 /* If we resolved any of the outstanding static mappings */
2678 if (vec_len(indices_to_delete))
2681 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2682 vec_delete(sm->to_resolve, 1, j);
2683 vec_free(indices_to_delete);
2689 (void) snat_del_address(sm, address[0], 1, twice_nat);
2695 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
2698 ip4_main_t * ip4_main = sm->ip4_main;
2699 ip4_address_t * first_int_addr;
2700 snat_static_map_resolve_t *rp;
2701 u32 *indices_to_delete = 0;
2703 u32 *auto_add_sw_if_indices =
2704 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
2706 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2707 0 /* just want the address*/);
2709 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
2711 if (auto_add_sw_if_indices[i] == sw_if_index)
2715 /* if have address remove it */
2717 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
2720 for (j = 0; j < vec_len (sm->to_resolve); j++)
2722 rp = sm->to_resolve + j;
2723 if (rp->sw_if_index == sw_if_index)
2724 vec_add1 (indices_to_delete, j);
2726 if (vec_len(indices_to_delete))
2728 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2729 vec_del1(sm->to_resolve, j);
2730 vec_free(indices_to_delete);
2734 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
2736 vec_del1(sm->auto_add_sw_if_indices, i);
2739 return VNET_API_ERROR_VALUE_EXIST;
2746 return VNET_API_ERROR_NO_SUCH_ENTRY;
2748 /* add to the auto-address list */
2750 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
2752 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2754 /* If the address is already bound - or static - add it now */
2756 snat_add_address (sm, first_int_addr, ~0, twice_nat);
2762 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
2763 snat_protocol_t proto, u32 vrf_id, int is_in)
2765 snat_main_per_thread_data_t *tsm;
2766 clib_bihash_kv_8_8_t kv, value;
2768 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2769 snat_session_key_t key;
2771 clib_bihash_8_8_t *t;
2772 snat_user_key_t u_key;
2775 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
2776 if (sm->num_workers)
2778 vec_elt_at_index (sm->per_thread_data,
2779 sm->worker_in2out_cb (&ip, fib_index));
2781 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
2783 key.addr.as_u32 = addr->as_u32;
2784 key.port = clib_host_to_net_u16 (port);
2785 key.protocol = proto;
2786 key.fib_index = fib_index;
2787 kv.key = key.as_u64;
2788 t = is_in ? &tsm->in2out : &tsm->out2in;
2789 if (!clib_bihash_search_8_8 (t, &kv, &value))
2791 s = pool_elt_at_index (tsm->sessions, value.value);
2792 kv.key = s->in2out.as_u64;
2793 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
2794 kv.key = s->out2in.as_u64;
2795 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
2796 u_key.addr = s->in2out.addr;
2797 u_key.fib_index = s->in2out.fib_index;
2798 kv.key = u_key.as_u64;
2799 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
2801 u = pool_elt_at_index (tsm->users, value.value);
2804 clib_dlist_remove (tsm->list_pool, s->per_user_index);
2805 pool_put (tsm->sessions, s);
2809 return VNET_API_ERROR_NO_SUCH_ENTRY;
2813 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
2815 snat_main_t *sm = &snat_main;
2817 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
2819 sm->psid_offset = psid_offset;
2820 sm->psid_length = psid_length;
2824 nat_set_alloc_addr_and_port_default (void)
2826 snat_main_t *sm = &snat_main;
2828 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;