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;
677 snat_user_key_t u_key;
679 dlist_elt_t * head, * elt;
680 u32 elt_index, head_index;
685 /* If the external address is a specific interface address */
686 if (sw_if_index != ~0)
688 ip4_address_t * first_int_addr;
690 /* Might be already set... */
691 first_int_addr = ip4_interface_first_address
692 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
694 /* DHCP resolution required? */
695 if (first_int_addr == 0)
697 snat_add_static_mapping_when_resolved
698 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
699 addr_only, is_add, tag);
704 e_addr.as_u32 = first_int_addr->as_u32;
705 /* Identity mapping? */
706 if (l_addr.as_u32 == 0)
707 l_addr.as_u32 = e_addr.as_u32;
712 m_key.port = addr_only ? 0 : e_port;
713 m_key.protocol = addr_only ? 0 : proto;
714 m_key.fib_index = sm->outside_fib_index;
715 kv.key = m_key.as_u64;
716 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
719 m = pool_elt_at_index (sm->static_mappings, value.value);
724 return VNET_API_ERROR_VALUE_EXIST;
726 if (twice_nat && addr_only)
727 return VNET_API_ERROR_UNSUPPORTED;
729 /* Convert VRF id to FIB index */
732 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
734 return VNET_API_ERROR_NO_SUCH_FIB;
737 /* If not specified use inside VRF id from SNAT plugin startup config */
740 fib_index = sm->inside_fib_index;
741 vrf_id = sm->inside_vrf_id;
744 /* Find external address in allocated addresses and reserve port for
745 address and port pair mapping when dynamic translations enabled */
746 if (!(addr_only || sm->static_mapping_only || out2in_only))
748 for (i = 0; i < vec_len (sm->addresses); i++)
750 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
752 a = sm->addresses + i;
753 /* External port must be unused */
756 #define _(N, j, n, s) \
757 case SNAT_PROTOCOL_##N: \
758 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
759 return VNET_API_ERROR_INVALID_VALUE; \
760 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
763 a->busy_##n##_ports++; \
764 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
767 foreach_snat_protocol
770 clib_warning("unknown_protocol");
771 return VNET_API_ERROR_INVALID_VALUE_2;
776 /* External address must be allocated */
777 if (!a && (l_addr.as_u32 != e_addr.as_u32))
778 return VNET_API_ERROR_NO_SUCH_ENTRY;
781 pool_get (sm->static_mappings, m);
782 memset (m, 0, sizeof (*m));
783 m->tag = vec_dup (tag);
784 m->local_addr = l_addr;
785 m->external_addr = e_addr;
786 m->addr_only = addr_only;
788 m->fib_index = fib_index;
789 m->twice_nat = twice_nat;
790 m->out2in_only = out2in_only;
793 m->local_port = l_port;
794 m->external_port = e_port;
801 .src_address = m->local_addr,
803 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
804 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
807 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
809 m_key.addr = m->local_addr;
810 m_key.port = m->local_port;
811 m_key.protocol = m->proto;
812 m_key.fib_index = m->fib_index;
813 kv.key = m_key.as_u64;
814 kv.value = m - sm->static_mappings;
816 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
817 if (twice_nat || out2in_only)
819 m_key.port = clib_host_to_net_u16 (l_port);
820 kv.key = m_key.as_u64;
822 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
823 clib_warning ("in2out key add failed");
826 m_key.addr = m->external_addr;
827 m_key.port = m->external_port;
828 m_key.fib_index = sm->outside_fib_index;
829 kv.key = m_key.as_u64;
830 kv.value = m - sm->static_mappings;
831 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
832 if (twice_nat || out2in_only)
834 m_key.port = clib_host_to_net_u16 (e_port);
835 kv.key = m_key.as_u64;
837 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
838 clib_warning ("out2in key add failed");
841 /* Delete dynamic sessions matching local address (+ local port) */
842 if (!(sm->static_mapping_only))
844 u_key.addr = m->local_addr;
845 u_key.fib_index = m->fib_index;
846 kv.key = u_key.as_u64;
847 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
849 user_index = value.value;
850 u = pool_elt_at_index (tsm->users, user_index);
853 head_index = u->sessions_per_user_list_head_index;
854 head = pool_elt_at_index (tsm->list_pool, head_index);
855 elt_index = head->next;
856 elt = pool_elt_at_index (tsm->list_pool, elt_index);
857 ses_index = elt->value;
858 while (ses_index != ~0)
860 s = pool_elt_at_index (tsm->sessions, ses_index);
861 elt = pool_elt_at_index (tsm->list_pool, elt->next);
862 ses_index = elt->value;
864 if (snat_is_session_static (s))
869 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
870 (clib_net_to_host_u16 (s->out2in.port) != e_port))
874 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
875 clib_dlist_remove (tsm->list_pool, s->per_user_index);
876 pool_put_index (tsm->list_pool, s->per_user_index);
877 pool_put (tsm->sessions, s);
890 return VNET_API_ERROR_NO_SUCH_ENTRY;
892 /* Free external address port */
893 if (!(addr_only || sm->static_mapping_only || out2in_only))
895 for (i = 0; i < vec_len (sm->addresses); i++)
897 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
899 a = sm->addresses + i;
902 #define _(N, j, n, s) \
903 case SNAT_PROTOCOL_##N: \
904 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
907 a->busy_##n##_ports--; \
908 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
911 foreach_snat_protocol
914 clib_warning("unknown_protocol");
915 return VNET_API_ERROR_INVALID_VALUE_2;
922 if (sm->num_workers > 1)
923 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
925 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
927 m_key.addr = m->local_addr;
928 m_key.port = m->local_port;
929 m_key.protocol = m->proto;
930 m_key.fib_index = m->fib_index;
931 kv.key = m_key.as_u64;
933 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
934 if (twice_nat || out2in_only)
936 m_key.port = clib_host_to_net_u16 (m->local_port);
937 kv.key = m_key.as_u64;
939 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
940 clib_warning ("in2out key del failed");
943 m_key.addr = m->external_addr;
944 m_key.port = m->external_port;
945 m_key.fib_index = sm->outside_fib_index;
946 kv.key = m_key.as_u64;
947 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
948 if (twice_nat || out2in_only)
950 m_key.port = clib_host_to_net_u16 (m->external_port);
951 kv.key = m_key.as_u64;
953 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
954 clib_warning ("in2out key del failed");
957 /* Delete session(s) for static mapping if exist */
958 if (!(sm->static_mapping_only) ||
959 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
961 u_key.addr = m->local_addr;
962 u_key.fib_index = m->fib_index;
963 kv.key = u_key.as_u64;
964 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
966 user_index = value.value;
967 u = pool_elt_at_index (tsm->users, user_index);
968 if (u->nstaticsessions)
970 head_index = u->sessions_per_user_list_head_index;
971 head = pool_elt_at_index (tsm->list_pool, head_index);
972 elt_index = head->next;
973 elt = pool_elt_at_index (tsm->list_pool, elt_index);
974 ses_index = elt->value;
975 while (ses_index != ~0)
977 s = pool_elt_at_index (tsm->sessions, ses_index);
978 elt = pool_elt_at_index (tsm->list_pool, elt->next);
979 ses_index = elt->value;
983 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
984 (clib_net_to_host_u16 (s->out2in.port) != e_port))
988 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
989 clib_dlist_remove (tsm->list_pool, s->per_user_index);
990 pool_put_index (tsm->list_pool, s->per_user_index);
991 pool_put (tsm->sessions, s);
992 u->nstaticsessions--;
999 pool_put (tsm->users, u);
1000 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
1007 /* Delete static mapping from pool */
1008 pool_put (sm->static_mappings, m);
1011 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
1014 /* Add/delete external address to FIB */
1015 pool_foreach (interface, sm->interfaces,
1017 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1020 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1023 pool_foreach (interface, sm->output_feature_interfaces,
1025 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1028 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
1035 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
1036 snat_protocol_t proto, u32 vrf_id,
1037 nat44_lb_addr_port_t *locals, u8 is_add,
1038 u8 twice_nat, u8 out2in_only, u8 *tag)
1040 snat_main_t * sm = &snat_main;
1041 snat_static_mapping_t *m;
1042 snat_session_key_t m_key;
1043 clib_bihash_kv_8_8_t kv, value;
1045 snat_address_t *a = 0;
1047 nat44_lb_addr_port_t *local;
1048 u32 worker_index = 0, elt_index, head_index, ses_index;
1049 snat_main_per_thread_data_t *tsm;
1050 snat_user_key_t u_key;
1053 dlist_elt_t * head, * elt;
1055 m_key.addr = e_addr;
1056 m_key.port = e_port;
1057 m_key.protocol = proto;
1058 m_key.fib_index = sm->outside_fib_index;
1059 kv.key = m_key.as_u64;
1060 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1063 m = pool_elt_at_index (sm->static_mappings, value.value);
1068 return VNET_API_ERROR_VALUE_EXIST;
1070 if (vec_len (locals) < 2)
1071 return VNET_API_ERROR_INVALID_VALUE;
1073 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1075 FIB_SOURCE_PLUGIN_HI);
1077 /* Find external address in allocated addresses and reserve port for
1078 address and port pair mapping when dynamic translations enabled */
1079 if (!(sm->static_mapping_only || out2in_only))
1081 for (i = 0; i < vec_len (sm->addresses); i++)
1083 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1085 a = sm->addresses + i;
1086 /* External port must be unused */
1089 #define _(N, j, n, s) \
1090 case SNAT_PROTOCOL_##N: \
1091 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1092 return VNET_API_ERROR_INVALID_VALUE; \
1093 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1094 if (e_port > 1024) \
1096 a->busy_##n##_ports++; \
1097 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1100 foreach_snat_protocol
1103 clib_warning("unknown_protocol");
1104 return VNET_API_ERROR_INVALID_VALUE_2;
1109 /* External address must be allocated */
1111 return VNET_API_ERROR_NO_SUCH_ENTRY;
1114 pool_get (sm->static_mappings, m);
1115 memset (m, 0, sizeof (*m));
1116 m->tag = vec_dup (tag);
1117 m->external_addr = e_addr;
1120 m->fib_index = fib_index;
1121 m->external_port = e_port;
1123 m->twice_nat = twice_nat;
1124 m->out2in_only = out2in_only;
1126 m_key.addr = m->external_addr;
1127 m_key.port = m->external_port;
1128 m_key.protocol = m->proto;
1129 m_key.fib_index = sm->outside_fib_index;
1130 kv.key = m_key.as_u64;
1131 kv.value = m - sm->static_mappings;
1132 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1134 clib_warning ("static_mapping_by_external key add failed");
1135 return VNET_API_ERROR_UNSPECIFIED;
1141 worker_index = sm->first_worker_index +
1142 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1143 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
1144 m->worker_index = worker_index;
1147 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1149 m_key.port = clib_host_to_net_u16 (m->external_port);
1150 kv.key = m_key.as_u64;
1152 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
1154 clib_warning ("out2in key add failed");
1155 return VNET_API_ERROR_UNSPECIFIED;
1158 m_key.fib_index = m->fib_index;
1159 for (i = 0; i < vec_len (locals); i++)
1161 m_key.addr = locals[i].addr;
1164 m_key.port = locals[i].port;
1165 kv.key = m_key.as_u64;
1166 kv.value = m - sm->static_mappings;
1167 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1169 locals[i].prefix = (i == 0) ? locals[i].probability :\
1170 (locals[i - 1].prefix + locals[i].probability);
1171 vec_add1 (m->locals, locals[i]);
1173 m_key.port = clib_host_to_net_u16 (locals[i].port);
1174 kv.key = m_key.as_u64;
1176 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
1178 clib_warning ("in2out key add failed");
1179 return VNET_API_ERROR_UNSPECIFIED;
1186 return VNET_API_ERROR_NO_SUCH_ENTRY;
1188 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI);
1190 /* Free external address port */
1191 if (!(sm->static_mapping_only || out2in_only))
1193 for (i = 0; i < vec_len (sm->addresses); i++)
1195 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1197 a = sm->addresses + i;
1200 #define _(N, j, n, s) \
1201 case SNAT_PROTOCOL_##N: \
1202 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1203 if (e_port > 1024) \
1205 a->busy_##n##_ports--; \
1206 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1209 foreach_snat_protocol
1212 clib_warning("unknown_protocol");
1213 return VNET_API_ERROR_INVALID_VALUE_2;
1220 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1221 m_key.addr = m->external_addr;
1222 m_key.port = m->external_port;
1223 m_key.protocol = m->proto;
1224 m_key.fib_index = sm->outside_fib_index;
1225 kv.key = m_key.as_u64;
1226 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1228 clib_warning ("static_mapping_by_external key del failed");
1229 return VNET_API_ERROR_UNSPECIFIED;
1232 m_key.port = clib_host_to_net_u16 (m->external_port);
1233 kv.key = m_key.as_u64;
1234 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1236 clib_warning ("outi2in key del failed");
1237 return VNET_API_ERROR_UNSPECIFIED;
1240 vec_foreach (local, m->locals)
1242 m_key.addr = local->addr;
1245 m_key.port = local->port;
1246 m_key.fib_index = m->fib_index;
1247 kv.key = m_key.as_u64;
1248 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1250 clib_warning ("static_mapping_by_local key del failed");
1251 return VNET_API_ERROR_UNSPECIFIED;
1255 m_key.port = clib_host_to_net_u16 (local->port);
1256 kv.key = m_key.as_u64;
1257 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1259 clib_warning ("in2out key del failed");
1260 return VNET_API_ERROR_UNSPECIFIED;
1262 /* Delete sessions */
1263 u_key.addr = local->addr;
1264 u_key.fib_index = m->fib_index;
1265 kv.key = u_key.as_u64;
1266 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1268 u = pool_elt_at_index (tsm->users, value.value);
1269 if (u->nstaticsessions)
1271 head_index = u->sessions_per_user_list_head_index;
1272 head = pool_elt_at_index (tsm->list_pool, head_index);
1273 elt_index = head->next;
1274 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1275 ses_index = elt->value;
1276 while (ses_index != ~0)
1278 s = pool_elt_at_index (tsm->sessions, ses_index);
1279 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1280 ses_index = elt->value;
1282 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1283 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1286 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1287 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1288 pool_put_index (tsm->list_pool, s->per_user_index);
1289 pool_put (tsm->sessions, s);
1290 u->nstaticsessions--;
1295 vec_free(m->locals);
1298 pool_put (sm->static_mappings, m);
1305 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1308 snat_address_t *a = 0;
1309 snat_session_t *ses;
1310 u32 *ses_to_be_removed = 0, *ses_index;
1311 clib_bihash_kv_8_8_t kv, value;
1312 snat_user_key_t user_key;
1314 snat_main_per_thread_data_t *tsm;
1315 snat_static_mapping_t *m;
1316 snat_interface_t *interface;
1318 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1320 /* Find SNAT address */
1321 for (i=0; i < vec_len (addresses); i++)
1323 if (addresses[i].addr.as_u32 == addr.as_u32)
1330 return VNET_API_ERROR_NO_SUCH_ENTRY;
1334 pool_foreach (m, sm->static_mappings,
1336 if (m->external_addr.as_u32 == addr.as_u32)
1337 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1338 m->local_port, m->external_port,
1339 m->vrf_id, m->addr_only, ~0,
1340 m->proto, 0, m->twice_nat,
1341 m->out2in_only, m->tag);
1346 /* Check if address is used in some static mapping */
1347 if (is_snat_address_used_in_static_mapping(sm, addr))
1349 clib_warning ("address used in static mapping");
1350 return VNET_API_ERROR_UNSPECIFIED;
1354 if (a->fib_index != ~0)
1355 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1356 FIB_SOURCE_PLUGIN_HI);
1358 /* Delete sessions using address */
1359 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1361 vec_foreach (tsm, sm->per_thread_data)
1363 pool_foreach (ses, tsm->sessions, ({
1364 if (ses->out2in.addr.as_u32 == addr.as_u32)
1366 ses->outside_address_index = ~0;
1367 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1368 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1369 pool_put_index (tsm->list_pool, ses->per_user_index);
1370 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1371 user_key.addr = ses->in2out.addr;
1372 user_key.fib_index = ses->in2out.fib_index;
1373 kv.key = user_key.as_u64;
1374 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1376 u = pool_elt_at_index (tsm->users, value.value);
1382 vec_foreach (ses_index, ses_to_be_removed)
1383 pool_put_index (tsm->sessions, ses_index[0]);
1385 vec_free (ses_to_be_removed);
1391 vec_del1 (sm->twice_nat_addresses, i);
1395 vec_del1 (sm->addresses, i);
1397 /* Delete external address from FIB */
1398 pool_foreach (interface, sm->interfaces,
1400 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1403 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1406 pool_foreach (interface, sm->output_feature_interfaces,
1408 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1411 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1418 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1420 snat_main_t *sm = &snat_main;
1421 snat_interface_t *i;
1422 const char * feature_name, *del_feature_name;
1423 snat_address_t * ap;
1424 snat_static_mapping_t * m;
1425 snat_det_map_t * dm;
1427 if (sm->out2in_dpo && !is_inside)
1428 return VNET_API_ERROR_UNSUPPORTED;
1430 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1431 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1434 if (sm->num_workers > 1 && !sm->deterministic)
1435 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1436 else if (sm->deterministic)
1437 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1439 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1442 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1443 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1445 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1446 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1448 pool_foreach (i, sm->interfaces,
1450 if (i->sw_if_index == sw_if_index)
1454 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1457 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1459 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1461 if (sm->num_workers > 1 && !sm->deterministic)
1463 del_feature_name = "nat44-handoff-classify";
1464 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1465 "nat44-out2in-worker-handoff";
1467 else if (sm->deterministic)
1469 del_feature_name = "nat44-det-classify";
1470 feature_name = !is_inside ? "nat44-det-in2out" :
1475 del_feature_name = "nat44-classify";
1476 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1479 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1480 sw_if_index, 0, 0, 0);
1481 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1482 sw_if_index, 1, 0, 0);
1486 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1487 sw_if_index, 0, 0, 0);
1488 pool_put (sm->interfaces, i);
1493 if ((nat_interface_is_inside(i) && is_inside) ||
1494 (nat_interface_is_outside(i) && !is_inside))
1497 if (sm->num_workers > 1 && !sm->deterministic)
1499 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1500 "nat44-out2in-worker-handoff";
1501 feature_name = "nat44-handoff-classify";
1503 else if (sm->deterministic)
1505 del_feature_name = !is_inside ? "nat44-det-in2out" :
1507 feature_name = "nat44-det-classify";
1511 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1512 feature_name = "nat44-classify";
1515 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1516 sw_if_index, 0, 0, 0);
1517 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1518 sw_if_index, 1, 0, 0);
1527 return VNET_API_ERROR_NO_SUCH_ENTRY;
1529 pool_get (sm->interfaces, i);
1530 i->sw_if_index = sw_if_index;
1532 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1536 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1538 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1540 /* Add/delete external addresses to FIB */
1542 if (is_inside && !sm->out2in_dpo)
1544 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1545 sw_if_index, !is_del, 0, 0);
1549 vec_foreach (ap, sm->addresses)
1550 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1552 pool_foreach (m, sm->static_mappings,
1554 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1557 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1560 pool_foreach (dm, sm->det_maps,
1562 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1568 int snat_interface_add_del_output_feature (u32 sw_if_index,
1572 snat_main_t *sm = &snat_main;
1573 snat_interface_t *i;
1574 snat_address_t * ap;
1575 snat_static_mapping_t * m;
1577 if (sm->deterministic ||
1578 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1579 return VNET_API_ERROR_UNSUPPORTED;
1583 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1584 sw_if_index, !is_del, 0, 0);
1585 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1586 sw_if_index, !is_del, 0, 0);
1590 if (sm->num_workers > 1)
1592 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1593 sw_if_index, !is_del, 0, 0);
1594 vnet_feature_enable_disable ("ip4-output",
1595 "nat44-in2out-output-worker-handoff",
1596 sw_if_index, !is_del, 0, 0);
1600 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1602 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1603 sw_if_index, !is_del, 0, 0);
1607 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1608 sm->fq_in2out_output_index =
1609 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1611 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1612 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1614 pool_foreach (i, sm->output_feature_interfaces,
1616 if (i->sw_if_index == sw_if_index)
1619 pool_put (sm->output_feature_interfaces, i);
1621 return VNET_API_ERROR_VALUE_EXIST;
1628 return VNET_API_ERROR_NO_SUCH_ENTRY;
1630 pool_get (sm->output_feature_interfaces, i);
1631 i->sw_if_index = sw_if_index;
1634 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1636 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1638 /* Add/delete external addresses to FIB */
1643 vec_foreach (ap, sm->addresses)
1644 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1646 pool_foreach (m, sm->static_mappings,
1648 if (!(m->addr_only))
1651 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1657 int snat_set_workers (uword * bitmap)
1659 snat_main_t *sm = &snat_main;
1662 if (sm->num_workers < 2)
1663 return VNET_API_ERROR_FEATURE_DISABLED;
1665 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1666 return VNET_API_ERROR_INVALID_WORKER;
1668 vec_free (sm->workers);
1669 clib_bitmap_foreach (i, bitmap,
1671 vec_add1(sm->workers, i);
1672 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1676 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1677 sm->num_snat_thread = _vec_len (sm->workers);
1684 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1687 ip4_address_t * address,
1689 u32 if_address_index,
1693 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1696 snat_session_key_t * k,
1697 u32 * address_indexp,
1698 u16 port_per_thread,
1699 u32 snat_thread_index);
1701 static clib_error_t * snat_init (vlib_main_t * vm)
1703 snat_main_t * sm = &snat_main;
1704 clib_error_t * error = 0;
1705 ip4_main_t * im = &ip4_main;
1706 ip_lookup_main_t * lm = &im->lookup_main;
1708 vlib_thread_registration_t *tr;
1709 vlib_thread_main_t *tm = vlib_get_thread_main ();
1712 ip4_add_del_interface_address_callback_t cb4;
1715 sm->vnet_main = vnet_get_main();
1717 sm->ip4_lookup_main = lm;
1718 sm->api_main = &api_main;
1719 sm->first_worker_index = 0;
1720 sm->next_worker = 0;
1721 sm->num_workers = 0;
1722 sm->num_snat_thread = 1;
1724 sm->port_per_thread = 0xffff - 1024;
1725 sm->fq_in2out_index = ~0;
1726 sm->fq_out2in_index = ~0;
1727 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1728 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1729 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1730 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1731 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1732 sm->forwarding_enabled = 0;
1734 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1737 tr = (vlib_thread_registration_t *) p[0];
1740 sm->num_workers = tr->count;
1741 sm->first_worker_index = tr->first_index;
1745 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1747 /* Use all available workers by default */
1748 if (sm->num_workers > 1)
1750 for (i=0; i < sm->num_workers; i++)
1751 bitmap = clib_bitmap_set (bitmap, i, 1);
1752 snat_set_workers(bitmap);
1753 clib_bitmap_free (bitmap);
1757 sm->per_thread_data[0].snat_thread_index = 0;
1760 error = snat_api_init(vm, sm);
1764 /* Set up the interface address add/del callback */
1765 cb4.function = snat_ip4_add_del_interface_address_cb;
1766 cb4.function_opaque = 0;
1768 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1770 nat_dpo_module_init ();
1772 /* Init IPFIX logging */
1773 snat_ipfix_logging_init(vm);
1776 error = nat64_init(vm);
1782 /* Init virtual fragmenentation reassembly */
1783 return nat_reass_init(vm);
1786 VLIB_INIT_FUNCTION (snat_init);
1788 void snat_free_outside_address_and_port (snat_address_t * addresses,
1790 snat_session_key_t * k,
1794 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1796 ASSERT (address_index < vec_len (addresses));
1798 a = addresses + address_index;
1800 switch (k->protocol)
1802 #define _(N, i, n, s) \
1803 case SNAT_PROTOCOL_##N: \
1804 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1805 port_host_byte_order) == 1); \
1806 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1807 port_host_byte_order, 0); \
1808 a->busy_##n##_ports--; \
1809 a->busy_##n##_ports_per_thread[thread_index]--; \
1811 foreach_snat_protocol
1814 clib_warning("unknown_protocol");
1820 * @brief Match NAT44 static mapping.
1822 * @param sm NAT main.
1823 * @param match Address and port to match.
1824 * @param mapping External or local address and port of the matched mapping.
1825 * @param by_external If 0 match by local address otherwise match by external
1827 * @param is_addr_only If matched mapping is address only
1828 * @param twice_nat If matched mapping is twice NAT.
1830 * @returns 0 if match found otherwise 1.
1832 int snat_static_mapping_match (snat_main_t * sm,
1833 snat_session_key_t match,
1834 snat_session_key_t * mapping,
1839 clib_bihash_kv_8_8_t kv, value;
1840 snat_static_mapping_t *m;
1841 snat_session_key_t m_key;
1842 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1843 u32 rand, lo = 0, hi, mid;
1846 mapping_hash = &sm->static_mapping_by_external;
1848 m_key.addr = match.addr;
1849 m_key.port = clib_net_to_host_u16 (match.port);
1850 m_key.protocol = match.protocol;
1851 m_key.fib_index = match.fib_index;
1853 kv.key = m_key.as_u64;
1855 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1857 /* Try address only mapping */
1860 kv.key = m_key.as_u64;
1861 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1865 m = pool_elt_at_index (sm->static_mappings, value.value);
1869 if (vec_len (m->locals))
1871 hi = vec_len (m->locals) - 1;
1872 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
1875 mid = ((hi - lo) >> 1) + lo;
1876 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
1878 if (!(m->locals[lo].prefix >= rand))
1880 mapping->addr = m->locals[lo].addr;
1881 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
1885 mapping->addr = m->local_addr;
1886 /* Address only mapping doesn't change port */
1887 mapping->port = m->addr_only ? match.port
1888 : clib_host_to_net_u16 (m->local_port);
1890 mapping->fib_index = m->fib_index;
1891 mapping->protocol = m->proto;
1895 mapping->addr = m->external_addr;
1896 /* Address only mapping doesn't change port */
1897 mapping->port = m->addr_only ? match.port
1898 : clib_host_to_net_u16 (m->external_port);
1899 mapping->fib_index = sm->outside_fib_index;
1902 if (PREDICT_FALSE(is_addr_only != 0))
1903 *is_addr_only = m->addr_only;
1905 if (PREDICT_FALSE(twice_nat != 0))
1906 *twice_nat = m->twice_nat;
1911 static_always_inline u16
1912 snat_random_port (u16 min, u16 max)
1914 snat_main_t *sm = &snat_main;
1915 return min + random_u32 (&sm->random_seed) /
1916 (random_u32_max() / (max - min + 1) + 1);
1920 snat_alloc_outside_address_and_port (snat_address_t * addresses,
1923 snat_session_key_t * k,
1924 u32 * address_indexp,
1925 u16 port_per_thread,
1926 u32 snat_thread_index)
1928 snat_main_t *sm = &snat_main;
1930 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
1931 address_indexp, port_per_thread,
1936 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1939 snat_session_key_t * k,
1940 u32 * address_indexp,
1941 u16 port_per_thread,
1942 u32 snat_thread_index)
1945 snat_address_t *a, *ga = 0;
1948 for (i = 0; i < vec_len (addresses); i++)
1951 switch (k->protocol)
1953 #define _(N, j, n, s) \
1954 case SNAT_PROTOCOL_##N: \
1955 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1957 if (a->fib_index == fib_index) \
1961 portnum = (port_per_thread * \
1962 snat_thread_index) + \
1963 snat_random_port(1, port_per_thread) + 1024; \
1964 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1966 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1967 a->busy_##n##_ports_per_thread[thread_index]++; \
1968 a->busy_##n##_ports++; \
1969 k->addr = a->addr; \
1970 k->port = clib_host_to_net_u16(portnum); \
1971 *address_indexp = i; \
1975 else if (a->fib_index == ~0) \
1982 foreach_snat_protocol
1985 clib_warning("unknown protocol");
1994 switch (k->protocol)
1996 #define _(N, j, n, s) \
1997 case SNAT_PROTOCOL_##N: \
2000 portnum = (port_per_thread * \
2001 snat_thread_index) + \
2002 snat_random_port(1, port_per_thread) + 1024; \
2003 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2005 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2006 a->busy_##n##_ports_per_thread[thread_index]++; \
2007 a->busy_##n##_ports++; \
2008 k->addr = a->addr; \
2009 k->port = clib_host_to_net_u16(portnum); \
2010 *address_indexp = gi; \
2014 foreach_snat_protocol
2017 clib_warning ("unknown protocol");
2022 /* Totally out of translations to use... */
2023 snat_ipfix_logging_addresses_exhausted(0);
2028 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
2031 snat_session_key_t * k,
2032 u32 * address_indexp,
2033 u16 port_per_thread,
2034 u32 snat_thread_index)
2036 snat_main_t *sm = &snat_main;
2037 snat_address_t *a = addresses;
2038 u16 m, ports, portnum, A, j;
2039 m = 16 - (sm->psid_offset + sm->psid_length);
2040 ports = (1 << (16 - sm->psid_length)) - (1 << m);
2042 if (!vec_len (addresses))
2045 switch (k->protocol)
2047 #define _(N, i, n, s) \
2048 case SNAT_PROTOCOL_##N: \
2049 if (a->busy_##n##_ports < ports) \
2053 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2054 j = snat_random_port(0, pow2_mask(m)); \
2055 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2056 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2058 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2059 a->busy_##n##_ports++; \
2060 k->addr = a->addr; \
2061 k->port = clib_host_to_net_u16 (portnum); \
2062 *address_indexp = i; \
2067 foreach_snat_protocol
2070 clib_warning("unknown protocol");
2075 /* Totally out of translations to use... */
2076 snat_ipfix_logging_addresses_exhausted(0);
2081 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2083 dpo_id_t dpo_v4 = DPO_INVALID;
2084 fib_prefix_t pfx = {
2085 .fp_proto = FIB_PROTOCOL_IP4,
2087 .fp_addr.ip4.as_u32 = addr.as_u32,
2092 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2093 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2094 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2095 dpo_reset (&dpo_v4);
2099 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2104 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2106 u32 *r = va_arg (*args, u32 *);
2109 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2110 foreach_snat_protocol
2118 format_snat_protocol (u8 * s, va_list * args)
2120 u32 i = va_arg (*args, u32);
2125 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2126 foreach_snat_protocol
2129 s = format (s, "unknown");
2132 s = format (s, "%s", t);
2137 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2139 snat_main_t *sm = &snat_main;
2140 u32 next_worker_index = 0;
2143 next_worker_index = sm->first_worker_index;
2144 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2145 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2147 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2148 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2150 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2152 return next_worker_index;
2156 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2158 snat_main_t *sm = &snat_main;
2161 snat_session_key_t m_key;
2162 clib_bihash_kv_8_8_t kv, value;
2163 snat_static_mapping_t *m;
2164 nat_ed_ses_key_t key;
2165 clib_bihash_kv_16_8_t s_kv, s_value;
2166 snat_main_per_thread_data_t *tsm;
2170 u32 next_worker_index = 0;
2172 /* first try static mappings without port */
2173 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2175 m_key.addr = ip0->dst_address;
2178 m_key.fib_index = rx_fib_index0;
2179 kv.key = m_key.as_u64;
2180 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2182 m = pool_elt_at_index (sm->static_mappings, value.value);
2183 return m->worker_index;
2187 proto = ip_proto_to_snat_proto (ip0->protocol);
2188 udp = ip4_next_header (ip0);
2189 port = udp->dst_port;
2191 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2193 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2194 return vlib_get_thread_index ();
2196 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2198 nat_reass_ip4_t *reass;
2200 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2201 ip0->fragment_id, ip0->protocol);
2203 if (reass && (reass->thread_index != (u32) ~ 0))
2204 return reass->thread_index;
2206 return vlib_get_thread_index ();
2210 /* unknown protocol */
2211 if (PREDICT_FALSE (proto == ~0))
2213 key.l_addr = ip0->dst_address;
2214 key.r_addr = ip0->src_address;
2215 key.fib_index = rx_fib_index0;
2216 key.proto = ip0->protocol;
2219 s_kv.key[0] = key.as_u64[0];
2220 s_kv.key[1] = key.as_u64[1];
2222 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
2224 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
2226 tsm = vec_elt_at_index (sm->per_thread_data, i);
2227 if (!pool_is_free_index(tsm->sessions, s_value.value))
2229 s = pool_elt_at_index (tsm->sessions, s_value.value);
2230 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2231 s->out2in.port == ip0->protocol &&
2232 snat_is_unk_proto_session (s))
2238 /* if no session use current thread */
2239 return vlib_get_thread_index ();
2242 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2244 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2245 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2246 if (!icmp_is_error_message (icmp))
2247 port = echo->identifier;
2250 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2251 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2252 void *l4_header = ip4_next_header (inner_ip);
2255 case SNAT_PROTOCOL_ICMP:
2256 icmp = (icmp46_header_t*)l4_header;
2257 echo = (icmp_echo_header_t *)(icmp + 1);
2258 port = echo->identifier;
2260 case SNAT_PROTOCOL_UDP:
2261 case SNAT_PROTOCOL_TCP:
2262 port = ((tcp_udp_header_t*)l4_header)->src_port;
2265 return vlib_get_thread_index ();
2270 /* try static mappings with port */
2271 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2273 m_key.addr = ip0->dst_address;
2274 m_key.port = clib_net_to_host_u16 (port);
2275 m_key.protocol = proto;
2276 m_key.fib_index = rx_fib_index0;
2277 kv.key = m_key.as_u64;
2278 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2280 m = pool_elt_at_index (sm->static_mappings, value.value);
2281 return m->worker_index;
2285 /* worker by outside port */
2286 next_worker_index = sm->first_worker_index;
2287 next_worker_index +=
2288 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2289 return next_worker_index;
2292 static clib_error_t *
2293 snat_config (vlib_main_t * vm, unformat_input_t * input)
2295 snat_main_t * sm = &snat_main;
2296 u32 translation_buckets = 1024;
2297 u32 translation_memory_size = 128<<20;
2298 u32 user_buckets = 128;
2299 u32 user_memory_size = 64<<20;
2300 u32 max_translations_per_user = 100;
2301 u32 outside_vrf_id = 0;
2302 u32 inside_vrf_id = 0;
2303 u32 static_mapping_buckets = 1024;
2304 u32 static_mapping_memory_size = 64<<20;
2305 u32 nat64_bib_buckets = 1024;
2306 u32 nat64_bib_memory_size = 128 << 20;
2307 u32 nat64_st_buckets = 2048;
2308 u32 nat64_st_memory_size = 256 << 20;
2309 u8 static_mapping_only = 0;
2310 u8 static_mapping_connection_tracking = 0;
2311 snat_main_per_thread_data_t *tsm;
2312 dslite_main_t * dm = &dslite_main;
2314 sm->deterministic = 0;
2317 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2319 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2321 else if (unformat (input, "translation hash memory %d",
2322 &translation_memory_size));
2323 else if (unformat (input, "user hash buckets %d", &user_buckets))
2325 else if (unformat (input, "user hash memory %d",
2328 else if (unformat (input, "max translations per user %d",
2329 &max_translations_per_user))
2331 else if (unformat (input, "outside VRF id %d",
2334 else if (unformat (input, "inside VRF id %d",
2337 else if (unformat (input, "static mapping only"))
2339 static_mapping_only = 1;
2340 if (unformat (input, "connection tracking"))
2341 static_mapping_connection_tracking = 1;
2343 else if (unformat (input, "deterministic"))
2344 sm->deterministic = 1;
2345 else if (unformat (input, "nat64 bib hash buckets %d",
2346 &nat64_bib_buckets))
2348 else if (unformat (input, "nat64 bib hash memory %d",
2349 &nat64_bib_memory_size))
2351 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2353 else if (unformat (input, "nat64 st hash memory %d",
2354 &nat64_st_memory_size))
2356 else if (unformat (input, "out2in dpo"))
2358 else if (unformat (input, "dslite ce"))
2359 dslite_set_ce(dm, 1);
2361 return clib_error_return (0, "unknown input '%U'",
2362 format_unformat_error, input);
2365 /* for show commands, etc. */
2366 sm->translation_buckets = translation_buckets;
2367 sm->translation_memory_size = translation_memory_size;
2368 /* do not exceed load factor 10 */
2369 sm->max_translations = 10 * translation_buckets;
2370 sm->user_buckets = user_buckets;
2371 sm->user_memory_size = user_memory_size;
2372 sm->max_translations_per_user = max_translations_per_user;
2373 sm->outside_vrf_id = outside_vrf_id;
2374 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2376 FIB_SOURCE_PLUGIN_HI);
2377 sm->inside_vrf_id = inside_vrf_id;
2378 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2380 FIB_SOURCE_PLUGIN_HI);
2381 sm->static_mapping_only = static_mapping_only;
2382 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2384 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
2385 nat64_st_memory_size);
2387 if (sm->deterministic)
2389 sm->in2out_node_index = snat_det_in2out_node.index;
2390 sm->in2out_output_node_index = ~0;
2391 sm->out2in_node_index = snat_det_out2in_node.index;
2392 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2393 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2397 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2398 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2399 sm->in2out_node_index = snat_in2out_node.index;
2400 sm->in2out_output_node_index = snat_in2out_output_node.index;
2401 sm->out2in_node_index = snat_out2in_node.index;
2402 if (!static_mapping_only ||
2403 (static_mapping_only && static_mapping_connection_tracking))
2405 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2406 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2408 vec_foreach (tsm, sm->per_thread_data)
2410 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2411 translation_memory_size);
2413 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2414 translation_memory_size);
2416 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2420 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
2421 translation_buckets, translation_memory_size);
2423 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2424 translation_buckets, translation_memory_size);
2428 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2429 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2431 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2432 "static_mapping_by_local", static_mapping_buckets,
2433 static_mapping_memory_size);
2435 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2436 "static_mapping_by_external", static_mapping_buckets,
2437 static_mapping_memory_size);
2443 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2445 u8 * format_snat_session_state (u8 * s, va_list * args)
2447 u32 i = va_arg (*args, u32);
2452 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2453 foreach_snat_session_state
2456 t = format (t, "unknown");
2458 s = format (s, "%s", t);
2462 u8 * format_snat_key (u8 * s, va_list * args)
2464 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2466 s = format (s, "%U proto %U port %d fib %d",
2467 format_ip4_address, &key->addr,
2468 format_snat_protocol, key->protocol,
2469 clib_net_to_host_u16 (key->port), key->fib_index);
2473 u8 * format_snat_session (u8 * s, va_list * args)
2475 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2476 snat_session_t * sess = va_arg (*args, snat_session_t *);
2478 if (snat_is_unk_proto_session (sess))
2480 s = format (s, " i2o %U proto %u fib %u\n",
2481 format_ip4_address, &sess->in2out.addr,
2482 clib_net_to_host_u16 (sess->in2out.port),
2483 sess->in2out.fib_index);
2484 s = format (s, " o2i %U proto %u fib %u\n",
2485 format_ip4_address, &sess->out2in.addr,
2486 clib_net_to_host_u16 (sess->out2in.port),
2487 sess->out2in.fib_index);
2491 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2492 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2494 if (is_twice_nat_session (sess))
2496 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
2497 format_ip4_address, &sess->ext_host_addr,
2498 clib_net_to_host_u16 (sess->ext_host_port),
2499 format_ip4_address, &sess->ext_host_nat_addr,
2500 clib_net_to_host_u16 (sess->ext_host_nat_port));
2504 if (sess->ext_host_addr.as_u32)
2505 s = format (s, " external host %U\n",
2506 format_ip4_address, &sess->ext_host_addr);
2508 s = format (s, " last heard %.2f\n", sess->last_heard);
2509 s = format (s, " total pkts %d, total bytes %lld\n",
2510 sess->total_pkts, sess->total_bytes);
2511 if (snat_is_session_static (sess))
2512 s = format (s, " static translation\n");
2514 s = format (s, " dynamic translation\n");
2515 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2516 s = format (s, " load-balancing\n");
2517 if (is_twice_nat_session (sess))
2518 s = format (s, " twice-nat\n");
2523 u8 * format_snat_user (u8 * s, va_list * args)
2525 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2526 snat_user_t * u = va_arg (*args, snat_user_t *);
2527 int verbose = va_arg (*args, int);
2528 dlist_elt_t * head, * elt;
2529 u32 elt_index, head_index;
2531 snat_session_t * sess;
2533 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2534 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2539 if (u->nsessions || u->nstaticsessions)
2541 head_index = u->sessions_per_user_list_head_index;
2542 head = pool_elt_at_index (sm->list_pool, head_index);
2544 elt_index = head->next;
2545 elt = pool_elt_at_index (sm->list_pool, elt_index);
2546 session_index = elt->value;
2548 while (session_index != ~0)
2550 sess = pool_elt_at_index (sm->sessions, session_index);
2552 s = format (s, " %U\n", format_snat_session, sm, sess);
2554 elt_index = elt->next;
2555 elt = pool_elt_at_index (sm->list_pool, elt_index);
2556 session_index = elt->value;
2563 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2565 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2566 nat44_lb_addr_port_t *local;
2569 s = format (s, "local %U external %U vrf %d %s",
2570 format_ip4_address, &m->local_addr,
2571 format_ip4_address, &m->external_addr,
2572 m->vrf_id, m->twice_nat ? "twice-nat" : "");
2575 if (vec_len (m->locals))
2577 s = format (s, "%U vrf %d external %U:%d %s %s",
2578 format_snat_protocol, m->proto,
2580 format_ip4_address, &m->external_addr, m->external_port,
2581 m->twice_nat ? "twice-nat" : "",
2582 m->out2in_only ? "out2in-only" : "");
2583 vec_foreach (local, m->locals)
2584 s = format (s, "\n local %U:%d probability %d\%",
2585 format_ip4_address, &local->addr, local->port,
2586 local->probability);
2589 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
2590 format_snat_protocol, m->proto,
2591 format_ip4_address, &m->local_addr, m->local_port,
2592 format_ip4_address, &m->external_addr, m->external_port,
2593 m->vrf_id, m->twice_nat ? "twice-nat" : "",
2594 m->out2in_only ? "out2in-only" : "");
2599 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2601 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2602 vnet_main_t *vnm = vnet_get_main();
2605 s = format (s, "local %U external %U vrf %d",
2606 format_ip4_address, &m->l_addr,
2607 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2610 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2611 format_snat_protocol, m->proto,
2612 format_ip4_address, &m->l_addr, m->l_port,
2613 format_vnet_sw_if_index_name, vnm, m->sw_if_index,
2614 m->e_port, m->vrf_id);
2619 u8 * format_det_map_ses (u8 * s, va_list * args)
2621 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2622 ip4_address_t in_addr, out_addr;
2623 u32 in_offset, out_offset;
2624 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2625 u32 * i = va_arg (*args, u32 *);
2627 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2628 in_addr.as_u32 = clib_host_to_net_u32 (
2629 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2630 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2631 clib_net_to_host_u32(det_map->in_addr.as_u32);
2632 out_offset = in_offset / det_map->sharing_ratio;
2633 out_addr.as_u32 = clib_host_to_net_u32(
2634 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2635 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2636 format_ip4_address, &in_addr,
2637 clib_net_to_host_u16 (ses->in_port),
2638 format_ip4_address, &out_addr,
2639 clib_net_to_host_u16 (ses->out.out_port),
2640 format_ip4_address, &ses->out.ext_host_addr,
2641 clib_net_to_host_u16 (ses->out.ext_host_port),
2642 format_snat_session_state, ses->state,
2649 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2652 ip4_address_t * address,
2654 u32 if_address_index,
2657 snat_main_t *sm = &snat_main;
2658 snat_static_map_resolve_t *rp;
2659 u32 *indices_to_delete = 0;
2660 ip4_address_t l_addr;
2664 snat_address_t *addresses = sm->addresses;
2666 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2668 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2672 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
2675 addresses = sm->twice_nat_addresses;
2676 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
2685 /* Don't trip over lease renewal, static config */
2686 for (j = 0; j < vec_len(addresses); j++)
2687 if (addresses[j].addr.as_u32 == address->as_u32)
2690 snat_add_address (sm, address, ~0, twice_nat);
2691 /* Scan static map resolution vector */
2692 for (j = 0; j < vec_len (sm->to_resolve); j++)
2694 rp = sm->to_resolve + j;
2695 /* On this interface? */
2696 if (rp->sw_if_index == sw_if_index)
2698 /* Indetity mapping? */
2699 if (rp->l_addr.as_u32 == 0)
2700 l_addr.as_u32 = address[0].as_u32;
2702 l_addr.as_u32 = rp->l_addr.as_u32;
2703 /* Add the static mapping */
2704 rv = snat_add_static_mapping (l_addr,
2710 ~0 /* sw_if_index */,
2715 clib_warning ("snat_add_static_mapping returned %d",
2718 vec_add1 (indices_to_delete, j);
2721 /* If we resolved any of the outstanding static mappings */
2722 if (vec_len(indices_to_delete))
2725 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2726 vec_delete(sm->to_resolve, 1, j);
2727 vec_free(indices_to_delete);
2733 (void) snat_del_address(sm, address[0], 1, twice_nat);
2739 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
2742 ip4_main_t * ip4_main = sm->ip4_main;
2743 ip4_address_t * first_int_addr;
2744 snat_static_map_resolve_t *rp;
2745 u32 *indices_to_delete = 0;
2747 u32 *auto_add_sw_if_indices =
2748 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
2750 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2751 0 /* just want the address*/);
2753 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
2755 if (auto_add_sw_if_indices[i] == sw_if_index)
2759 /* if have address remove it */
2761 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
2764 for (j = 0; j < vec_len (sm->to_resolve); j++)
2766 rp = sm->to_resolve + j;
2767 if (rp->sw_if_index == sw_if_index)
2768 vec_add1 (indices_to_delete, j);
2770 if (vec_len(indices_to_delete))
2772 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2773 vec_del1(sm->to_resolve, j);
2774 vec_free(indices_to_delete);
2778 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
2780 vec_del1(sm->auto_add_sw_if_indices, i);
2783 return VNET_API_ERROR_VALUE_EXIST;
2790 return VNET_API_ERROR_NO_SUCH_ENTRY;
2792 /* add to the auto-address list */
2794 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
2796 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2798 /* If the address is already bound - or static - add it now */
2800 snat_add_address (sm, first_int_addr, ~0, twice_nat);
2806 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
2807 snat_protocol_t proto, u32 vrf_id, int is_in)
2809 snat_main_per_thread_data_t *tsm;
2810 clib_bihash_kv_8_8_t kv, value;
2812 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
2813 snat_session_key_t key;
2815 clib_bihash_8_8_t *t;
2816 snat_user_key_t u_key;
2819 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
2820 if (sm->num_workers)
2822 vec_elt_at_index (sm->per_thread_data,
2823 sm->worker_in2out_cb (&ip, fib_index));
2825 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
2827 key.addr.as_u32 = addr->as_u32;
2828 key.port = clib_host_to_net_u16 (port);
2829 key.protocol = proto;
2830 key.fib_index = fib_index;
2831 kv.key = key.as_u64;
2832 t = is_in ? &tsm->in2out : &tsm->out2in;
2833 if (!clib_bihash_search_8_8 (t, &kv, &value))
2835 s = pool_elt_at_index (tsm->sessions, value.value);
2836 kv.key = s->in2out.as_u64;
2837 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
2838 kv.key = s->out2in.as_u64;
2839 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
2840 u_key.addr = s->in2out.addr;
2841 u_key.fib_index = s->in2out.fib_index;
2842 kv.key = u_key.as_u64;
2843 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
2845 u = pool_elt_at_index (tsm->users, value.value);
2848 clib_dlist_remove (tsm->list_pool, s->per_user_index);
2849 pool_put (tsm->sessions, s);
2853 return VNET_API_ERROR_NO_SUCH_ENTRY;
2857 nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
2859 snat_main_t *sm = &snat_main;
2861 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
2863 sm->psid_offset = psid_offset;
2864 sm->psid_length = psid_length;
2868 nat_set_alloc_addr_and_port_default (void)
2870 snat_main_t *sm = &snat_main;
2872 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;