2 * snat.c - simple nat plugin
4 * Copyright (c) 2016 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/ip4.h>
21 #include <vnet/plugin/plugin.h>
23 #include <nat/nat_dpo.h>
24 #include <nat/nat_ipfix_logging.h>
25 #include <nat/nat_det.h>
26 #include <nat/nat64.h>
27 #include <nat/dslite.h>
28 #include <nat/nat_reass.h>
29 #include <vnet/fib/fib_table.h>
30 #include <vnet/fib/ip4_fib.h>
32 #include <vpp/app/version.h>
34 snat_main_t snat_main;
37 /* Hook up input features */
38 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
39 .arc_name = "ip4-unicast",
40 .node_name = "nat44-in2out",
41 .runs_before = VNET_FEATURES ("nat44-out2in"),
43 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
44 .arc_name = "ip4-unicast",
45 .node_name = "nat44-out2in",
46 .runs_before = VNET_FEATURES ("ip4-lookup"),
48 VNET_FEATURE_INIT (ip4_nat_classify, static) = {
49 .arc_name = "ip4-unicast",
50 .node_name = "nat44-classify",
51 .runs_before = VNET_FEATURES ("ip4-lookup"),
53 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
54 .arc_name = "ip4-unicast",
55 .node_name = "nat44-det-in2out",
56 .runs_before = VNET_FEATURES ("nat44-det-out2in"),
58 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
59 .arc_name = "ip4-unicast",
60 .node_name = "nat44-det-out2in",
61 .runs_before = VNET_FEATURES ("ip4-lookup"),
63 VNET_FEATURE_INIT (ip4_nat_det_classify, static) = {
64 .arc_name = "ip4-unicast",
65 .node_name = "nat44-det-classify",
66 .runs_before = VNET_FEATURES ("ip4-lookup"),
68 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
69 .arc_name = "ip4-unicast",
70 .node_name = "nat44-in2out-worker-handoff",
71 .runs_before = VNET_FEATURES ("nat44-out2in-worker-handoff"),
73 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
74 .arc_name = "ip4-unicast",
75 .node_name = "nat44-out2in-worker-handoff",
76 .runs_before = VNET_FEATURES ("ip4-lookup"),
78 VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = {
79 .arc_name = "ip4-unicast",
80 .node_name = "nat44-handoff-classify",
81 .runs_before = VNET_FEATURES ("ip4-lookup"),
83 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
84 .arc_name = "ip4-unicast",
85 .node_name = "nat44-in2out-fast",
86 .runs_before = VNET_FEATURES ("nat44-out2in-fast"),
88 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
89 .arc_name = "ip4-unicast",
90 .node_name = "nat44-out2in-fast",
91 .runs_before = VNET_FEATURES ("ip4-lookup"),
93 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
94 .arc_name = "ip4-unicast",
95 .node_name = "nat44-hairpin-dst",
96 .runs_before = VNET_FEATURES ("ip4-lookup"),
99 /* Hook up output features */
100 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
101 .arc_name = "ip4-output",
102 .node_name = "nat44-in2out-output",
103 .runs_before = VNET_FEATURES ("interface-output"),
105 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
106 .arc_name = "ip4-output",
107 .node_name = "nat44-in2out-output-worker-handoff",
108 .runs_before = VNET_FEATURES ("interface-output"),
110 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
111 .arc_name = "ip4-output",
112 .node_name = "nat44-hairpin-src",
113 .runs_before = VNET_FEATURES ("interface-output"),
116 /* Hook up ip4-local features */
117 VNET_FEATURE_INIT (ip4_nat_hairpinning, static) =
119 .arc_name = "ip4-local",
120 .node_name = "nat44-hairpinning",
121 .runs_before = VNET_FEATURES("ip4-local-end-of-arc"),
126 VLIB_PLUGIN_REGISTER () = {
127 .version = VPP_BUILD_VER,
128 .description = "Network Address Translation",
132 vlib_node_registration_t nat44_classify_node;
133 vlib_node_registration_t nat44_det_classify_node;
134 vlib_node_registration_t nat44_handoff_classify_node;
137 NAT44_CLASSIFY_NEXT_IN2OUT,
138 NAT44_CLASSIFY_NEXT_OUT2IN,
139 NAT44_CLASSIFY_N_NEXT,
140 } nat44_classify_next_t;
143 nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
145 snat_session_key_t key;
146 clib_bihash_kv_8_8_t kv;
147 nat_ed_ses_key_t ed_key;
148 clib_bihash_kv_16_8_t ed_kv;
151 snat_main_per_thread_data_t *tsm =
152 vec_elt_at_index (sm->per_thread_data, thread_index);
154 /* Endpoint dependent session lookup tables */
155 if (is_ed_session (s))
157 ed_key.l_addr = s->out2in.addr;
158 ed_key.r_addr = s->ext_host_addr;
159 ed_key.fib_index = s->out2in.fib_index;
160 if (snat_is_unk_proto_session (s))
162 ed_key.proto = s->in2out.port;
168 ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
169 ed_key.l_port = s->out2in.port;
170 ed_key.r_port = s->ext_host_port;
172 ed_kv.key[0] = ed_key.as_u64[0];
173 ed_kv.key[1] = ed_key.as_u64[1];
174 if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &ed_kv, 0))
175 clib_warning ("out2in_ed key del failed");
177 ed_key.l_addr = s->in2out.addr;
178 ed_key.fib_index = s->in2out.fib_index;
179 if (!snat_is_unk_proto_session (s))
180 ed_key.l_port = s->in2out.port;
181 if (is_twice_nat_session (s))
183 ed_key.r_addr = s->ext_host_nat_addr;
184 ed_key.r_port = s->ext_host_nat_port;
186 ed_kv.key[0] = ed_key.as_u64[0];
187 ed_kv.key[1] = ed_key.as_u64[1];
188 if (clib_bihash_add_del_16_8 (&sm->in2out_ed, &ed_kv, 0))
189 clib_warning ("in2out_ed key del failed");
192 if (snat_is_unk_proto_session (s))
196 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
197 s->out2in.addr.as_u32,
201 s->in2out.fib_index);
203 /* Twice NAT address and port for external host */
204 if (is_twice_nat_session (s))
206 for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
208 key.protocol = s->in2out.protocol;
209 key.port = s->ext_host_nat_port;
210 a = sm->twice_nat_addresses + i;
211 if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
213 snat_free_outside_address_and_port (sm->twice_nat_addresses,
214 thread_index, &key, i);
220 if (is_ed_session (s))
223 /* Session lookup tables */
224 kv.key = s->in2out.as_u64;
225 if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
226 clib_warning ("in2out key del failed");
227 kv.key = s->out2in.as_u64;
228 if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
229 clib_warning ("out2in key del failed");
231 if (snat_is_session_static (s))
234 if (s->outside_address_index != ~0)
235 snat_free_outside_address_and_port (sm->addresses, thread_index,
236 &s->out2in, s->outside_address_index);
240 nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
244 snat_user_key_t user_key;
245 clib_bihash_kv_8_8_t kv, value;
246 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
247 dlist_elt_t * per_user_list_head_elt;
249 user_key.addr.as_u32 = addr->as_u32;
250 user_key.fib_index = fib_index;
251 kv.key = user_key.as_u64;
253 /* Ever heard of the "user" = src ip4 address before? */
254 if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
256 /* no, make a new one */
257 pool_get (tsm->users, u);
258 memset (u, 0, sizeof (*u));
259 u->addr.as_u32 = addr->as_u32;
260 u->fib_index = fib_index;
262 pool_get (tsm->list_pool, per_user_list_head_elt);
264 u->sessions_per_user_list_head_index = per_user_list_head_elt -
267 clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index);
269 kv.value = u - tsm->users;
272 if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
273 clib_warning ("user_hash keay add failed");
277 u = pool_elt_at_index (tsm->users, value.value);
284 nat_session_alloc_or_recycle (snat_main_t *sm, snat_user_t *u, u32 thread_index)
287 snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
288 u32 oldest_per_user_translation_list_index, session_index;
289 dlist_elt_t * oldest_per_user_translation_list_elt;
290 dlist_elt_t * per_user_translation_list_elt;
292 /* Over quota? Recycle the least recently used translation */
293 if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user)
295 oldest_per_user_translation_list_index =
296 clib_dlist_remove_head (tsm->list_pool,
297 u->sessions_per_user_list_head_index);
299 ASSERT (oldest_per_user_translation_list_index != ~0);
301 /* Add it back to the end of the LRU list */
302 clib_dlist_addtail (tsm->list_pool,
303 u->sessions_per_user_list_head_index,
304 oldest_per_user_translation_list_index);
305 /* Get the list element */
306 oldest_per_user_translation_list_elt =
307 pool_elt_at_index (tsm->list_pool,
308 oldest_per_user_translation_list_index);
310 /* Get the session index from the list element */
311 session_index = oldest_per_user_translation_list_elt->value;
313 /* Get the session */
314 s = pool_elt_at_index (tsm->sessions, session_index);
315 nat_free_session_data (sm, s, thread_index);
316 s->outside_address_index = ~0;
323 pool_get (tsm->sessions, s);
324 memset (s, 0, sizeof (*s));
325 s->outside_address_index = ~0;
327 /* Create list elts */
328 pool_get (tsm->list_pool, per_user_translation_list_elt);
329 clib_dlist_init (tsm->list_pool,
330 per_user_translation_list_elt - tsm->list_pool);
332 per_user_translation_list_elt->value = s - tsm->sessions;
333 s->per_user_index = per_user_translation_list_elt - tsm->list_pool;
334 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
336 clib_dlist_addtail (tsm->list_pool,
337 s->per_user_list_head_index,
338 per_user_translation_list_elt - tsm->list_pool);
345 nat44_classify_node_fn_inline (vlib_main_t * vm,
346 vlib_node_runtime_t * node,
347 vlib_frame_t * frame)
349 u32 n_left_from, * from, * to_next;
350 nat44_classify_next_t next_index;
351 snat_main_t *sm = &snat_main;
353 from = vlib_frame_vector_args (frame);
354 n_left_from = frame->n_vectors;
355 next_index = node->cached_next_index;
357 while (n_left_from > 0)
361 vlib_get_next_frame (vm, node, next_index,
362 to_next, n_left_to_next);
364 while (n_left_from > 0 && n_left_to_next > 0)
368 u32 next0 = NAT44_CLASSIFY_NEXT_IN2OUT;
371 snat_session_key_t m_key0;
372 clib_bihash_kv_8_8_t kv0, value0;
374 /* speculatively enqueue b0 to the current next frame */
382 b0 = vlib_get_buffer (vm, bi0);
383 ip0 = vlib_buffer_get_current (b0);
385 vec_foreach (ap, sm->addresses)
387 if (ip0->dst_address.as_u32 == ap->addr.as_u32)
389 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
394 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
396 m_key0.addr = ip0->dst_address;
399 m_key0.fib_index = sm->outside_fib_index;
400 kv0.key = m_key0.as_u64;
401 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
403 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
406 udp_header_t * udp0 = ip4_next_header (ip0);
407 m_key0.port = clib_net_to_host_u16 (udp0->dst_port);
408 m_key0.protocol = ip_proto_to_snat_proto (ip0->protocol);
409 kv0.key = m_key0.as_u64;
410 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv0, &value0))
411 next0 = NAT44_CLASSIFY_NEXT_OUT2IN;
415 /* verify speculative enqueue, maybe switch current next frame */
416 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
417 to_next, n_left_to_next,
421 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
424 return frame->n_vectors;
428 nat44_classify_node_fn (vlib_main_t * vm,
429 vlib_node_runtime_t * node,
430 vlib_frame_t * frame)
432 return nat44_classify_node_fn_inline (vm, node, frame);
435 VLIB_REGISTER_NODE (nat44_classify_node) = {
436 .function = nat44_classify_node_fn,
437 .name = "nat44-classify",
438 .vector_size = sizeof (u32),
439 .type = VLIB_NODE_TYPE_INTERNAL,
440 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
442 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out",
443 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in",
447 VLIB_NODE_FUNCTION_MULTIARCH (nat44_classify_node,
448 nat44_classify_node_fn);
451 nat44_det_classify_node_fn (vlib_main_t * vm,
452 vlib_node_runtime_t * node,
453 vlib_frame_t * frame)
455 return nat44_classify_node_fn_inline (vm, node, frame);
458 VLIB_REGISTER_NODE (nat44_det_classify_node) = {
459 .function = nat44_det_classify_node_fn,
460 .name = "nat44-det-classify",
461 .vector_size = sizeof (u32),
462 .type = VLIB_NODE_TYPE_INTERNAL,
463 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
465 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-det-in2out",
466 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-det-out2in",
470 VLIB_NODE_FUNCTION_MULTIARCH (nat44_det_classify_node,
471 nat44_det_classify_node_fn);
474 nat44_handoff_classify_node_fn (vlib_main_t * vm,
475 vlib_node_runtime_t * node,
476 vlib_frame_t * frame)
478 return nat44_classify_node_fn_inline (vm, node, frame);
481 VLIB_REGISTER_NODE (nat44_handoff_classify_node) = {
482 .function = nat44_handoff_classify_node_fn,
483 .name = "nat44-handoff-classify",
484 .vector_size = sizeof (u32),
485 .type = VLIB_NODE_TYPE_INTERNAL,
486 .n_next_nodes = NAT44_CLASSIFY_N_NEXT,
488 [NAT44_CLASSIFY_NEXT_IN2OUT] = "nat44-in2out-worker-handoff",
489 [NAT44_CLASSIFY_NEXT_OUT2IN] = "nat44-out2in-worker-handoff",
493 VLIB_NODE_FUNCTION_MULTIARCH (nat44_handoff_classify_node,
494 nat44_handoff_classify_node_fn);
497 * @brief Add/del NAT address to FIB.
499 * Add the external NAT address to the FIB as receive entries. This ensures
500 * that VPP will reply to ARP for this address and we don't need to enable
501 * proxy ARP on the outside interface.
503 * @param addr IPv4 address.
504 * @param plen address prefix length
505 * @param sw_if_index Interface.
506 * @param is_add If 0 delete, otherwise add.
509 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
512 fib_prefix_t prefix = {
514 .fp_proto = FIB_PROTOCOL_IP4,
516 .ip4.as_u32 = addr->as_u32,
519 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
522 fib_table_entry_update_one_path(fib_index,
524 FIB_SOURCE_PLUGIN_HI,
525 (FIB_ENTRY_FLAG_CONNECTED |
526 FIB_ENTRY_FLAG_LOCAL |
527 FIB_ENTRY_FLAG_EXCLUSIVE),
534 FIB_ROUTE_PATH_FLAG_NONE);
536 fib_table_entry_delete(fib_index,
538 FIB_SOURCE_PLUGIN_HI);
541 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id,
546 vlib_thread_main_t *tm = vlib_get_thread_main ();
548 /* Check if address already exists */
549 vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses)
551 if (ap->addr.as_u32 == addr->as_u32)
556 vec_add2 (sm->twice_nat_addresses, ap, 1);
558 vec_add2 (sm->addresses, ap, 1);
563 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
564 FIB_SOURCE_PLUGIN_HI);
567 #define _(N, i, n, s) \
568 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
569 ap->busy_##n##_ports = 0; \
570 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
571 foreach_snat_protocol
577 /* Add external address to FIB */
578 pool_foreach (i, sm->interfaces,
580 if (nat_interface_is_inside(i) || sm->out2in_dpo)
583 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
586 pool_foreach (i, sm->output_feature_interfaces,
588 if (nat_interface_is_inside(i) || sm->out2in_dpo)
591 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
596 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
599 snat_static_mapping_t *m;
600 pool_foreach (m, sm->static_mappings,
602 if (m->external_addr.as_u32 == addr.as_u32)
609 void increment_v4_address (ip4_address_t * a)
613 v = clib_net_to_host_u32(a->as_u32) + 1;
614 a->as_u32 = clib_host_to_net_u32(v);
618 snat_add_static_mapping_when_resolved (snat_main_t * sm,
619 ip4_address_t l_addr,
624 snat_protocol_t proto,
628 snat_static_map_resolve_t *rp;
630 vec_add2 (sm->to_resolve, rp, 1);
631 rp->l_addr.as_u32 = l_addr.as_u32;
633 rp->sw_if_index = sw_if_index;
637 rp->addr_only = addr_only;
642 * @brief Add static mapping.
644 * Create static mapping between local addr+port and external addr+port.
646 * @param l_addr Local IPv4 address.
647 * @param e_addr External IPv4 address.
648 * @param l_port Local port number.
649 * @param e_port External port number.
650 * @param vrf_id VRF ID.
651 * @param addr_only If 0 address port and pair mapping, otherwise address only.
652 * @param sw_if_index External port instead of specific IP address.
653 * @param is_add If 0 delete static mapping, otherwise add.
654 * @param twice_nat If 1 translate external host address and port.
655 * @param out2in_only If 1 rule match only out2in direction
659 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
660 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
661 u32 sw_if_index, snat_protocol_t proto, int is_add,
662 u8 twice_nat, u8 out2in_only)
664 snat_main_t * sm = &snat_main;
665 snat_static_mapping_t *m;
666 snat_session_key_t m_key;
667 clib_bihash_kv_8_8_t kv, value;
668 snat_address_t *a = 0;
671 snat_interface_t *interface;
673 snat_main_per_thread_data_t *tsm;
675 /* If the external address is a specific interface address */
676 if (sw_if_index != ~0)
678 ip4_address_t * first_int_addr;
680 /* Might be already set... */
681 first_int_addr = ip4_interface_first_address
682 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
684 /* DHCP resolution required? */
685 if (first_int_addr == 0)
687 snat_add_static_mapping_when_resolved
688 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
694 e_addr.as_u32 = first_int_addr->as_u32;
695 /* Identity mapping? */
696 if (l_addr.as_u32 == 0)
697 l_addr.as_u32 = e_addr.as_u32;
702 m_key.port = addr_only ? 0 : e_port;
703 m_key.protocol = addr_only ? 0 : proto;
704 m_key.fib_index = sm->outside_fib_index;
705 kv.key = m_key.as_u64;
706 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
709 m = pool_elt_at_index (sm->static_mappings, value.value);
714 return VNET_API_ERROR_VALUE_EXIST;
716 if (twice_nat && addr_only)
717 return VNET_API_ERROR_UNSUPPORTED;
719 /* Convert VRF id to FIB index */
722 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
724 return VNET_API_ERROR_NO_SUCH_FIB;
727 /* If not specified use inside VRF id from SNAT plugin startup config */
730 fib_index = sm->inside_fib_index;
731 vrf_id = sm->inside_vrf_id;
734 /* Find external address in allocated addresses and reserve port for
735 address and port pair mapping when dynamic translations enabled */
736 if (!(addr_only || sm->static_mapping_only || out2in_only))
738 for (i = 0; i < vec_len (sm->addresses); i++)
740 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
742 a = sm->addresses + i;
743 /* External port must be unused */
746 #define _(N, j, n, s) \
747 case SNAT_PROTOCOL_##N: \
748 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
749 return VNET_API_ERROR_INVALID_VALUE; \
750 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
753 a->busy_##n##_ports++; \
754 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
757 foreach_snat_protocol
760 clib_warning("unknown_protocol");
761 return VNET_API_ERROR_INVALID_VALUE_2;
766 /* External address must be allocated */
768 return VNET_API_ERROR_NO_SUCH_ENTRY;
771 pool_get (sm->static_mappings, m);
772 memset (m, 0, sizeof (*m));
773 m->local_addr = l_addr;
774 m->external_addr = e_addr;
775 m->addr_only = addr_only;
777 m->fib_index = fib_index;
778 m->twice_nat = twice_nat;
779 m->out2in_only = out2in_only;
782 m->local_port = l_port;
783 m->external_port = e_port;
790 .src_address = m->local_addr,
792 m->worker_index = sm->worker_in2out_cb (&ip, m->fib_index);
793 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
796 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
798 m_key.addr = m->local_addr;
799 m_key.port = m->local_port;
800 m_key.protocol = m->proto;
801 m_key.fib_index = m->fib_index;
802 kv.key = m_key.as_u64;
803 kv.value = m - sm->static_mappings;
805 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
806 if (twice_nat || out2in_only)
808 m_key.port = clib_host_to_net_u16 (l_port);
809 kv.key = m_key.as_u64;
811 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
812 clib_warning ("in2out key add failed");
815 m_key.addr = m->external_addr;
816 m_key.port = m->external_port;
817 m_key.fib_index = sm->outside_fib_index;
818 kv.key = m_key.as_u64;
819 kv.value = m - sm->static_mappings;
820 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
821 if (twice_nat || out2in_only)
823 m_key.port = clib_host_to_net_u16 (e_port);
824 kv.key = m_key.as_u64;
826 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
827 clib_warning ("out2in key add failed");
834 return VNET_API_ERROR_NO_SUCH_ENTRY;
836 /* Free external address port */
837 if (!(addr_only || sm->static_mapping_only || out2in_only))
839 for (i = 0; i < vec_len (sm->addresses); i++)
841 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
843 a = sm->addresses + i;
846 #define _(N, j, n, s) \
847 case SNAT_PROTOCOL_##N: \
848 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
851 a->busy_##n##_ports--; \
852 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
855 foreach_snat_protocol
858 clib_warning("unknown_protocol");
859 return VNET_API_ERROR_INVALID_VALUE_2;
866 if (sm->num_workers > 1)
867 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
869 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
871 m_key.addr = m->local_addr;
872 m_key.port = m->local_port;
873 m_key.protocol = m->proto;
874 m_key.fib_index = m->fib_index;
875 kv.key = m_key.as_u64;
877 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
878 if (twice_nat || out2in_only)
880 m_key.port = clib_host_to_net_u16 (m->local_port);
881 kv.key = m_key.as_u64;
883 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
884 clib_warning ("in2out key del failed");
887 m_key.addr = m->external_addr;
888 m_key.port = m->external_port;
889 m_key.fib_index = sm->outside_fib_index;
890 kv.key = m_key.as_u64;
891 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
892 if (twice_nat || out2in_only)
894 m_key.port = clib_host_to_net_u16 (m->external_port);
895 kv.key = m_key.as_u64;
897 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
898 clib_warning ("in2out key del failed");
901 /* Delete session(s) for static mapping if exist */
902 if (!(sm->static_mapping_only) ||
903 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
905 snat_user_key_t u_key;
907 dlist_elt_t * head, * elt;
908 u32 elt_index, head_index;
913 u_key.addr = m->local_addr;
914 u_key.fib_index = m->fib_index;
915 kv.key = u_key.as_u64;
916 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
918 user_index = value.value;
919 u = pool_elt_at_index (tsm->users, user_index);
920 if (u->nstaticsessions)
922 head_index = u->sessions_per_user_list_head_index;
923 head = pool_elt_at_index (tsm->list_pool, head_index);
924 elt_index = head->next;
925 elt = pool_elt_at_index (tsm->list_pool, elt_index);
926 ses_index = elt->value;
927 while (ses_index != ~0)
929 s = pool_elt_at_index (tsm->sessions, ses_index);
930 elt = pool_elt_at_index (tsm->list_pool, elt->next);
931 ses_index = elt->value;
935 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
936 (clib_net_to_host_u16 (s->out2in.port) != e_port))
940 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
941 clib_dlist_remove (tsm->list_pool, s->per_user_index);
942 pool_put_index (tsm->list_pool, s->per_user_index);
943 pool_put (tsm->sessions, s);
944 u->nstaticsessions--;
951 pool_put (tsm->users, u);
952 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
958 /* Delete static mapping from pool */
959 pool_put (sm->static_mappings, m);
962 if (!addr_only || (l_addr.as_u32 == e_addr.as_u32))
965 /* Add/delete external address to FIB */
966 pool_foreach (interface, sm->interfaces,
968 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
971 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
974 pool_foreach (interface, sm->output_feature_interfaces,
976 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
979 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
986 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
987 snat_protocol_t proto, u32 vrf_id,
988 nat44_lb_addr_port_t *locals, u8 is_add,
989 u8 twice_nat, u8 out2in_only)
991 snat_main_t * sm = &snat_main;
992 snat_static_mapping_t *m;
993 snat_session_key_t m_key;
994 clib_bihash_kv_8_8_t kv, value;
996 snat_address_t *a = 0;
998 nat44_lb_addr_port_t *local;
999 u32 worker_index = 0, elt_index, head_index, ses_index;
1000 snat_main_per_thread_data_t *tsm;
1001 snat_user_key_t u_key;
1004 dlist_elt_t * head, * elt;
1006 m_key.addr = e_addr;
1007 m_key.port = e_port;
1008 m_key.protocol = proto;
1009 m_key.fib_index = sm->outside_fib_index;
1010 kv.key = m_key.as_u64;
1011 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
1014 m = pool_elt_at_index (sm->static_mappings, value.value);
1019 return VNET_API_ERROR_VALUE_EXIST;
1021 if (vec_len (locals) < 2)
1022 return VNET_API_ERROR_INVALID_VALUE;
1024 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1026 FIB_SOURCE_PLUGIN_HI);
1028 /* Find external address in allocated addresses and reserve port for
1029 address and port pair mapping when dynamic translations enabled */
1030 if (!(sm->static_mapping_only || out2in_only))
1032 for (i = 0; i < vec_len (sm->addresses); i++)
1034 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1036 a = sm->addresses + i;
1037 /* External port must be unused */
1040 #define _(N, j, n, s) \
1041 case SNAT_PROTOCOL_##N: \
1042 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
1043 return VNET_API_ERROR_INVALID_VALUE; \
1044 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
1045 if (e_port > 1024) \
1047 a->busy_##n##_ports++; \
1048 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]++; \
1051 foreach_snat_protocol
1054 clib_warning("unknown_protocol");
1055 return VNET_API_ERROR_INVALID_VALUE_2;
1060 /* External address must be allocated */
1062 return VNET_API_ERROR_NO_SUCH_ENTRY;
1065 pool_get (sm->static_mappings, m);
1066 memset (m, 0, sizeof (*m));
1067 m->external_addr = e_addr;
1070 m->fib_index = fib_index;
1071 m->external_port = e_port;
1073 m->twice_nat = twice_nat;
1074 m->out2in_only = out2in_only;
1076 m_key.addr = m->external_addr;
1077 m_key.port = m->external_port;
1078 m_key.protocol = m->proto;
1079 m_key.fib_index = sm->outside_fib_index;
1080 kv.key = m_key.as_u64;
1081 kv.value = m - sm->static_mappings;
1082 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
1084 clib_warning ("static_mapping_by_external key add failed");
1085 return VNET_API_ERROR_UNSPECIFIED;
1091 worker_index = sm->first_worker_index +
1092 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1093 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
1094 m->worker_index = worker_index;
1097 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1099 m_key.port = clib_host_to_net_u16 (m->external_port);
1100 kv.key = m_key.as_u64;
1102 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
1104 clib_warning ("out2in key add failed");
1105 return VNET_API_ERROR_UNSPECIFIED;
1108 m_key.fib_index = m->fib_index;
1109 for (i = 0; i < vec_len (locals); i++)
1111 m_key.addr = locals[i].addr;
1114 m_key.port = locals[i].port;
1115 kv.key = m_key.as_u64;
1116 kv.value = m - sm->static_mappings;
1117 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
1119 locals[i].prefix = (i == 0) ? locals[i].probability :\
1120 (locals[i - 1].prefix + locals[i].probability);
1121 vec_add1 (m->locals, locals[i]);
1123 m_key.port = clib_host_to_net_u16 (locals[i].port);
1124 kv.key = m_key.as_u64;
1126 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
1128 clib_warning ("in2out key add failed");
1129 return VNET_API_ERROR_UNSPECIFIED;
1136 return VNET_API_ERROR_NO_SUCH_ENTRY;
1138 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI);
1140 /* Free external address port */
1141 if (!(sm->static_mapping_only || out2in_only))
1143 for (i = 0; i < vec_len (sm->addresses); i++)
1145 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
1147 a = sm->addresses + i;
1150 #define _(N, j, n, s) \
1151 case SNAT_PROTOCOL_##N: \
1152 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
1153 if (e_port > 1024) \
1155 a->busy_##n##_ports--; \
1156 a->busy_##n##_ports_per_thread[(e_port - 1024) / sm->port_per_thread]--; \
1159 foreach_snat_protocol
1162 clib_warning("unknown_protocol");
1163 return VNET_API_ERROR_INVALID_VALUE_2;
1170 tsm = vec_elt_at_index (sm->per_thread_data, m->worker_index);
1171 m_key.addr = m->external_addr;
1172 m_key.port = m->external_port;
1173 m_key.protocol = m->proto;
1174 m_key.fib_index = sm->outside_fib_index;
1175 kv.key = m_key.as_u64;
1176 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
1178 clib_warning ("static_mapping_by_external key del failed");
1179 return VNET_API_ERROR_UNSPECIFIED;
1182 m_key.port = clib_host_to_net_u16 (m->external_port);
1183 kv.key = m_key.as_u64;
1184 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
1186 clib_warning ("outi2in key del failed");
1187 return VNET_API_ERROR_UNSPECIFIED;
1190 vec_foreach (local, m->locals)
1192 m_key.addr = local->addr;
1195 m_key.port = local->port;
1196 m_key.fib_index = m->fib_index;
1197 kv.key = m_key.as_u64;
1198 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
1200 clib_warning ("static_mapping_by_local key del failed");
1201 return VNET_API_ERROR_UNSPECIFIED;
1205 m_key.port = clib_host_to_net_u16 (local->port);
1206 kv.key = m_key.as_u64;
1207 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
1209 clib_warning ("in2out key del failed");
1210 return VNET_API_ERROR_UNSPECIFIED;
1212 /* Delete sessions */
1213 u_key.addr = local->addr;
1214 u_key.fib_index = m->fib_index;
1215 kv.key = u_key.as_u64;
1216 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1218 u = pool_elt_at_index (tsm->users, value.value);
1219 if (u->nstaticsessions)
1221 head_index = u->sessions_per_user_list_head_index;
1222 head = pool_elt_at_index (tsm->list_pool, head_index);
1223 elt_index = head->next;
1224 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1225 ses_index = elt->value;
1226 while (ses_index != ~0)
1228 s = pool_elt_at_index (tsm->sessions, ses_index);
1229 elt = pool_elt_at_index (tsm->list_pool, elt->next);
1230 ses_index = elt->value;
1232 if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
1233 (clib_net_to_host_u16 (s->in2out.port) != local->port))
1236 nat_free_session_data (sm, s, tsm - sm->per_thread_data);
1237 clib_dlist_remove (tsm->list_pool, s->per_user_index);
1238 pool_put_index (tsm->list_pool, s->per_user_index);
1239 pool_put (tsm->sessions, s);
1240 u->nstaticsessions--;
1245 vec_free(m->locals);
1247 pool_put (sm->static_mappings, m);
1254 snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm,
1257 snat_address_t *a = 0;
1258 snat_session_t *ses;
1259 u32 *ses_to_be_removed = 0, *ses_index;
1260 clib_bihash_kv_8_8_t kv, value;
1261 snat_user_key_t user_key;
1263 snat_main_per_thread_data_t *tsm;
1264 snat_static_mapping_t *m;
1265 snat_interface_t *interface;
1267 snat_address_t *addresses = twice_nat ? sm->twice_nat_addresses : sm->addresses;
1269 /* Find SNAT address */
1270 for (i=0; i < vec_len (addresses); i++)
1272 if (addresses[i].addr.as_u32 == addr.as_u32)
1279 return VNET_API_ERROR_NO_SUCH_ENTRY;
1283 pool_foreach (m, sm->static_mappings,
1285 if (m->external_addr.as_u32 == addr.as_u32)
1286 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
1287 m->local_port, m->external_port,
1288 m->vrf_id, m->addr_only, ~0,
1289 m->proto, 0, m->twice_nat,
1295 /* Check if address is used in some static mapping */
1296 if (is_snat_address_used_in_static_mapping(sm, addr))
1298 clib_warning ("address used in static mapping");
1299 return VNET_API_ERROR_UNSPECIFIED;
1303 if (a->fib_index != ~0)
1304 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
1305 FIB_SOURCE_PLUGIN_HI);
1307 /* Delete sessions using address */
1308 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
1310 vec_foreach (tsm, sm->per_thread_data)
1312 pool_foreach (ses, tsm->sessions, ({
1313 if (ses->out2in.addr.as_u32 == addr.as_u32)
1315 ses->outside_address_index = ~0;
1316 nat_free_session_data (sm, ses, tsm - sm->per_thread_data);
1317 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
1318 pool_put_index (tsm->list_pool, ses->per_user_index);
1319 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
1320 user_key.addr = ses->in2out.addr;
1321 user_key.fib_index = ses->in2out.fib_index;
1322 kv.key = user_key.as_u64;
1323 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
1325 u = pool_elt_at_index (tsm->users, value.value);
1331 vec_foreach (ses_index, ses_to_be_removed)
1332 pool_put_index (tsm->sessions, ses_index[0]);
1334 vec_free (ses_to_be_removed);
1340 vec_del1 (sm->twice_nat_addresses, i);
1344 vec_del1 (sm->addresses, i);
1346 /* Delete external address from FIB */
1347 pool_foreach (interface, sm->interfaces,
1349 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1352 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1355 pool_foreach (interface, sm->output_feature_interfaces,
1357 if (nat_interface_is_inside(interface) || sm->out2in_dpo)
1360 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1367 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1369 snat_main_t *sm = &snat_main;
1370 snat_interface_t *i;
1371 const char * feature_name, *del_feature_name;
1372 snat_address_t * ap;
1373 snat_static_mapping_t * m;
1374 snat_det_map_t * dm;
1376 if (sm->out2in_dpo && !is_inside)
1377 return VNET_API_ERROR_UNSUPPORTED;
1379 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1380 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1383 if (sm->num_workers > 1 && !sm->deterministic)
1384 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1385 else if (sm->deterministic)
1386 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1388 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1391 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1392 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1394 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1395 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1397 pool_foreach (i, sm->interfaces,
1399 if (i->sw_if_index == sw_if_index)
1403 if (nat_interface_is_inside(i) && nat_interface_is_outside(i))
1406 i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE;
1408 i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE;
1410 if (sm->num_workers > 1 && !sm->deterministic)
1412 del_feature_name = "nat44-handoff-classify";
1413 feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1414 "nat44-out2in-worker-handoff";
1416 else if (sm->deterministic)
1418 del_feature_name = "nat44-det-classify";
1419 feature_name = !is_inside ? "nat44-det-in2out" :
1424 del_feature_name = "nat44-classify";
1425 feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1428 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1429 sw_if_index, 0, 0, 0);
1430 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1431 sw_if_index, 1, 0, 0);
1435 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1436 sw_if_index, 0, 0, 0);
1437 pool_put (sm->interfaces, i);
1442 if ((nat_interface_is_inside(i) && is_inside) ||
1443 (nat_interface_is_outside(i) && !is_inside))
1446 if (sm->num_workers > 1 && !sm->deterministic)
1448 del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" :
1449 "nat44-out2in-worker-handoff";
1450 feature_name = "nat44-handoff-classify";
1452 else if (sm->deterministic)
1454 del_feature_name = !is_inside ? "nat44-det-in2out" :
1456 feature_name = "nat44-det-classify";
1460 del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in";
1461 feature_name = "nat44-classify";
1464 vnet_feature_enable_disable ("ip4-unicast", del_feature_name,
1465 sw_if_index, 0, 0, 0);
1466 vnet_feature_enable_disable ("ip4-unicast", feature_name,
1467 sw_if_index, 1, 0, 0);
1476 return VNET_API_ERROR_NO_SUCH_ENTRY;
1478 pool_get (sm->interfaces, i);
1479 i->sw_if_index = sw_if_index;
1481 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, 0);
1485 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1487 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1489 /* Add/delete external addresses to FIB */
1491 if (is_inside && !sm->out2in_dpo)
1493 vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
1494 sw_if_index, !is_del, 0, 0);
1498 vec_foreach (ap, sm->addresses)
1499 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1501 pool_foreach (m, sm->static_mappings,
1503 if (!(m->addr_only) || (m->local_addr.as_u32 == m->external_addr.as_u32))
1506 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1509 pool_foreach (dm, sm->det_maps,
1511 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1517 int snat_interface_add_del_output_feature (u32 sw_if_index,
1521 snat_main_t *sm = &snat_main;
1522 snat_interface_t *i;
1523 snat_address_t * ap;
1524 snat_static_mapping_t * m;
1526 if (sm->deterministic ||
1527 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1528 return VNET_API_ERROR_UNSUPPORTED;
1532 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1533 sw_if_index, !is_del, 0, 0);
1534 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1535 sw_if_index, !is_del, 0, 0);
1539 if (sm->num_workers > 1)
1541 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1542 sw_if_index, !is_del, 0, 0);
1543 vnet_feature_enable_disable ("ip4-output",
1544 "nat44-in2out-output-worker-handoff",
1545 sw_if_index, !is_del, 0, 0);
1549 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1551 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1552 sw_if_index, !is_del, 0, 0);
1556 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1557 sm->fq_in2out_output_index =
1558 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1560 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1561 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1563 pool_foreach (i, sm->output_feature_interfaces,
1565 if (i->sw_if_index == sw_if_index)
1568 pool_put (sm->output_feature_interfaces, i);
1570 return VNET_API_ERROR_VALUE_EXIST;
1577 return VNET_API_ERROR_NO_SUCH_ENTRY;
1579 pool_get (sm->output_feature_interfaces, i);
1580 i->sw_if_index = sw_if_index;
1583 i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE;
1585 i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE;
1587 /* Add/delete external addresses to FIB */
1592 vec_foreach (ap, sm->addresses)
1593 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1595 pool_foreach (m, sm->static_mappings,
1597 if (!(m->addr_only))
1600 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1606 int snat_set_workers (uword * bitmap)
1608 snat_main_t *sm = &snat_main;
1611 if (sm->num_workers < 2)
1612 return VNET_API_ERROR_FEATURE_DISABLED;
1614 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1615 return VNET_API_ERROR_INVALID_WORKER;
1617 vec_free (sm->workers);
1618 clib_bitmap_foreach (i, bitmap,
1620 vec_add1(sm->workers, i);
1621 sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j;
1625 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1626 sm->num_snat_thread = _vec_len (sm->workers);
1633 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1636 ip4_address_t * address,
1638 u32 if_address_index,
1642 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1645 snat_session_key_t * k,
1646 u32 * address_indexp,
1647 u16 port_per_thread,
1648 u32 snat_thread_index);
1650 static clib_error_t * snat_init (vlib_main_t * vm)
1652 snat_main_t * sm = &snat_main;
1653 clib_error_t * error = 0;
1654 ip4_main_t * im = &ip4_main;
1655 ip_lookup_main_t * lm = &im->lookup_main;
1657 vlib_thread_registration_t *tr;
1658 vlib_thread_main_t *tm = vlib_get_thread_main ();
1661 ip4_add_del_interface_address_callback_t cb4;
1664 sm->vnet_main = vnet_get_main();
1666 sm->ip4_lookup_main = lm;
1667 sm->api_main = &api_main;
1668 sm->first_worker_index = 0;
1669 sm->next_worker = 0;
1670 sm->num_workers = 0;
1671 sm->num_snat_thread = 1;
1673 sm->port_per_thread = 0xffff - 1024;
1674 sm->fq_in2out_index = ~0;
1675 sm->fq_out2in_index = ~0;
1676 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1677 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1678 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1679 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1680 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
1681 sm->forwarding_enabled = 0;
1683 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1686 tr = (vlib_thread_registration_t *) p[0];
1689 sm->num_workers = tr->count;
1690 sm->first_worker_index = tr->first_index;
1694 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1696 /* Use all available workers by default */
1697 if (sm->num_workers > 1)
1699 for (i=0; i < sm->num_workers; i++)
1700 bitmap = clib_bitmap_set (bitmap, i, 1);
1701 snat_set_workers(bitmap);
1702 clib_bitmap_free (bitmap);
1706 sm->per_thread_data[0].snat_thread_index = 0;
1709 error = snat_api_init(vm, sm);
1713 /* Set up the interface address add/del callback */
1714 cb4.function = snat_ip4_add_del_interface_address_cb;
1715 cb4.function_opaque = 0;
1717 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1719 nat_dpo_module_init ();
1721 /* Init IPFIX logging */
1722 snat_ipfix_logging_init(vm);
1725 error = nat64_init(vm);
1731 /* Init virtual fragmenentation reassembly */
1732 return nat_reass_init(vm);
1735 VLIB_INIT_FUNCTION (snat_init);
1737 void snat_free_outside_address_and_port (snat_address_t * addresses,
1739 snat_session_key_t * k,
1743 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1745 ASSERT (address_index < vec_len (addresses));
1747 a = addresses + address_index;
1749 switch (k->protocol)
1751 #define _(N, i, n, s) \
1752 case SNAT_PROTOCOL_##N: \
1753 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1754 port_host_byte_order) == 1); \
1755 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1756 port_host_byte_order, 0); \
1757 a->busy_##n##_ports--; \
1758 a->busy_##n##_ports_per_thread[thread_index]--; \
1760 foreach_snat_protocol
1763 clib_warning("unknown_protocol");
1769 * @brief Match NAT44 static mapping.
1771 * @param sm NAT main.
1772 * @param match Address and port to match.
1773 * @param mapping External or local address and port of the matched mapping.
1774 * @param by_external If 0 match by local address otherwise match by external
1776 * @param is_addr_only If matched mapping is address only
1777 * @param twice_nat If matched mapping is twice NAT.
1779 * @returns 0 if match found otherwise 1.
1781 int snat_static_mapping_match (snat_main_t * sm,
1782 snat_session_key_t match,
1783 snat_session_key_t * mapping,
1788 clib_bihash_kv_8_8_t kv, value;
1789 snat_static_mapping_t *m;
1790 snat_session_key_t m_key;
1791 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1792 u32 rand, lo = 0, hi, mid;
1795 mapping_hash = &sm->static_mapping_by_external;
1797 m_key.addr = match.addr;
1798 m_key.port = clib_net_to_host_u16 (match.port);
1799 m_key.protocol = match.protocol;
1800 m_key.fib_index = match.fib_index;
1802 kv.key = m_key.as_u64;
1804 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1806 /* Try address only mapping */
1809 kv.key = m_key.as_u64;
1810 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1814 m = pool_elt_at_index (sm->static_mappings, value.value);
1818 if (vec_len (m->locals))
1820 hi = vec_len (m->locals) - 1;
1821 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
1824 mid = ((hi - lo) >> 1) + lo;
1825 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
1827 if (!(m->locals[lo].prefix >= rand))
1829 mapping->addr = m->locals[lo].addr;
1830 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
1834 mapping->addr = m->local_addr;
1835 /* Address only mapping doesn't change port */
1836 mapping->port = m->addr_only ? match.port
1837 : clib_host_to_net_u16 (m->local_port);
1839 mapping->fib_index = m->fib_index;
1840 mapping->protocol = m->proto;
1844 mapping->addr = m->external_addr;
1845 /* Address only mapping doesn't change port */
1846 mapping->port = m->addr_only ? match.port
1847 : clib_host_to_net_u16 (m->external_port);
1848 mapping->fib_index = sm->outside_fib_index;
1851 if (PREDICT_FALSE(is_addr_only != 0))
1852 *is_addr_only = m->addr_only;
1854 if (PREDICT_FALSE(twice_nat != 0))
1855 *twice_nat = m->twice_nat;
1860 static_always_inline u16
1861 snat_random_port (u16 min, u16 max)
1863 snat_main_t *sm = &snat_main;
1864 return min + random_u32 (&sm->random_seed) /
1865 (random_u32_max() / (max - min + 1) + 1);
1869 snat_alloc_outside_address_and_port (snat_address_t * addresses,
1872 snat_session_key_t * k,
1873 u32 * address_indexp,
1874 u16 port_per_thread,
1875 u32 snat_thread_index)
1877 snat_main_t *sm = &snat_main;
1879 return sm->alloc_addr_and_port(addresses, fib_index, thread_index, k,
1880 address_indexp, port_per_thread,
1885 nat_alloc_addr_and_port_default (snat_address_t * addresses,
1888 snat_session_key_t * k,
1889 u32 * address_indexp,
1890 u16 port_per_thread,
1891 u32 snat_thread_index)
1894 snat_address_t *a, *ga = 0;
1897 for (i = 0; i < vec_len (addresses); i++)
1900 switch (k->protocol)
1902 #define _(N, j, n, s) \
1903 case SNAT_PROTOCOL_##N: \
1904 if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
1906 if (a->fib_index == fib_index) \
1910 portnum = (port_per_thread * \
1911 snat_thread_index) + \
1912 snat_random_port(1, port_per_thread) + 1024; \
1913 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1915 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1916 a->busy_##n##_ports_per_thread[thread_index]++; \
1917 a->busy_##n##_ports++; \
1918 k->addr = a->addr; \
1919 k->port = clib_host_to_net_u16(portnum); \
1920 *address_indexp = i; \
1924 else if (a->fib_index == ~0) \
1931 foreach_snat_protocol
1934 clib_warning("unknown protocol");
1943 switch (k->protocol)
1945 #define _(N, j, n, s) \
1946 case SNAT_PROTOCOL_##N: \
1949 portnum = (port_per_thread * \
1950 snat_thread_index) + \
1951 snat_random_port(1, port_per_thread) + 1024; \
1952 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1954 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1955 a->busy_##n##_ports_per_thread[thread_index]++; \
1956 a->busy_##n##_ports++; \
1957 k->addr = a->addr; \
1958 k->port = clib_host_to_net_u16(portnum); \
1959 *address_indexp = gi; \
1963 foreach_snat_protocol
1966 clib_warning ("unknown protocol");
1971 /* Totally out of translations to use... */
1972 snat_ipfix_logging_addresses_exhausted(0);
1977 nat_alloc_addr_and_port_mape (snat_address_t * addresses,
1980 snat_session_key_t * k,
1981 u32 * address_indexp,
1982 u16 port_per_thread,
1983 u32 snat_thread_index)
1985 snat_main_t *sm = &snat_main;
1986 snat_address_t *a = addresses;
1987 u16 m, ports, portnum, A, j;
1988 m = 16 - (sm->psid_offset + sm->psid_length);
1989 ports = (1 << (16 - sm->psid_length)) - (1 << m);
1991 if (!vec_len (addresses))
1994 switch (k->protocol)
1996 #define _(N, i, n, s) \
1997 case SNAT_PROTOCOL_##N: \
1998 if (a->busy_##n##_ports < ports) \
2002 A = snat_random_port(1, pow2_mask(sm->psid_offset)); \
2003 j = snat_random_port(0, pow2_mask(m)); \
2004 portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \
2005 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
2007 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
2008 a->busy_##n##_ports++; \
2009 k->addr = a->addr; \
2010 k->port = clib_host_to_net_u16 (portnum); \
2011 *address_indexp = i; \
2016 foreach_snat_protocol
2019 clib_warning("unknown protocol");
2024 /* Totally out of translations to use... */
2025 snat_ipfix_logging_addresses_exhausted(0);
2030 nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
2032 dpo_id_t dpo_v4 = DPO_INVALID;
2033 fib_prefix_t pfx = {
2034 .fp_proto = FIB_PROTOCOL_IP4,
2036 .fp_addr.ip4.as_u32 = addr.as_u32,
2041 nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4);
2042 fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI,
2043 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4);
2044 dpo_reset (&dpo_v4);
2048 fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI);
2052 static clib_error_t *
2053 add_address_command_fn (vlib_main_t * vm,
2054 unformat_input_t * input,
2055 vlib_cli_command_t * cmd)
2057 unformat_input_t _line_input, *line_input = &_line_input;
2058 snat_main_t * sm = &snat_main;
2059 ip4_address_t start_addr, end_addr, this_addr;
2060 u32 start_host_order, end_host_order;
2065 clib_error_t *error = 0;
2068 /* Get a line of input. */
2069 if (!unformat_user (input, unformat_line_input, line_input))
2072 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2074 if (unformat (line_input, "%U - %U",
2075 unformat_ip4_address, &start_addr,
2076 unformat_ip4_address, &end_addr))
2078 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
2080 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
2081 end_addr = start_addr;
2082 else if (unformat (line_input, "twice-nat"))
2084 else if (unformat (line_input, "del"))
2088 error = clib_error_return (0, "unknown input '%U'",
2089 format_unformat_error, line_input);
2094 if (sm->static_mapping_only)
2096 error = clib_error_return (0, "static mapping only mode");
2100 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
2101 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
2103 if (end_host_order < start_host_order)
2105 error = clib_error_return (0, "end address less than start address");
2109 count = (end_host_order - start_host_order) + 1;
2112 clib_warning ("%U - %U, %d addresses...",
2113 format_ip4_address, &start_addr,
2114 format_ip4_address, &end_addr,
2117 this_addr = start_addr;
2119 for (i = 0; i < count; i++)
2122 snat_add_address (sm, &this_addr, vrf_id, twice_nat);
2124 rv = snat_del_address (sm, this_addr, 0, twice_nat);
2128 case VNET_API_ERROR_NO_SUCH_ENTRY:
2129 error = clib_error_return (0, "S-NAT address not exist.");
2131 case VNET_API_ERROR_UNSPECIFIED:
2132 error = clib_error_return (0, "S-NAT address used in static mapping.");
2139 nat44_add_del_address_dpo (this_addr, is_add);
2141 increment_v4_address (&this_addr);
2145 unformat_free (line_input);
2150 VLIB_CLI_COMMAND (add_address_command, static) = {
2151 .path = "nat44 add address",
2152 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
2153 "[tenant-vrf <vrf-id>] [twice-nat] [del]",
2154 .function = add_address_command_fn,
2157 static clib_error_t *
2158 snat_feature_command_fn (vlib_main_t * vm,
2159 unformat_input_t * input,
2160 vlib_cli_command_t * cmd)
2162 unformat_input_t _line_input, *line_input = &_line_input;
2163 vnet_main_t * vnm = vnet_get_main();
2164 clib_error_t * error = 0;
2166 u32 * inside_sw_if_indices = 0;
2167 u32 * outside_sw_if_indices = 0;
2168 u8 is_output_feature = 0;
2174 /* Get a line of input. */
2175 if (!unformat_user (input, unformat_line_input, line_input))
2178 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2180 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
2182 vec_add1 (inside_sw_if_indices, sw_if_index);
2183 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
2185 vec_add1 (outside_sw_if_indices, sw_if_index);
2186 else if (unformat (line_input, "output-feature"))
2187 is_output_feature = 1;
2188 else if (unformat (line_input, "del"))
2192 error = clib_error_return (0, "unknown input '%U'",
2193 format_unformat_error, line_input);
2198 if (vec_len (inside_sw_if_indices))
2200 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
2202 sw_if_index = inside_sw_if_indices[i];
2203 if (is_output_feature)
2205 if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
2207 error = clib_error_return (0, "%s %U failed",
2208 is_del ? "del" : "add",
2209 format_vnet_sw_interface_name, vnm,
2210 vnet_get_sw_interface (vnm,
2217 if (snat_interface_add_del (sw_if_index, 1, is_del))
2219 error = clib_error_return (0, "%s %U failed",
2220 is_del ? "del" : "add",
2221 format_vnet_sw_interface_name, vnm,
2222 vnet_get_sw_interface (vnm,
2230 if (vec_len (outside_sw_if_indices))
2232 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
2234 sw_if_index = outside_sw_if_indices[i];
2235 if (is_output_feature)
2237 if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
2239 error = clib_error_return (0, "%s %U failed",
2240 is_del ? "del" : "add",
2241 format_vnet_sw_interface_name, vnm,
2242 vnet_get_sw_interface (vnm,
2249 if (snat_interface_add_del (sw_if_index, 0, is_del))
2251 error = clib_error_return (0, "%s %U failed",
2252 is_del ? "del" : "add",
2253 format_vnet_sw_interface_name, vnm,
2254 vnet_get_sw_interface (vnm,
2263 unformat_free (line_input);
2264 vec_free (inside_sw_if_indices);
2265 vec_free (outside_sw_if_indices);
2270 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
2271 .path = "set interface nat44",
2272 .function = snat_feature_command_fn,
2273 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
2278 unformat_snat_protocol (unformat_input_t * input, va_list * args)
2280 u32 *r = va_arg (*args, u32 *);
2283 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
2284 foreach_snat_protocol
2292 format_snat_protocol (u8 * s, va_list * args)
2294 u32 i = va_arg (*args, u32);
2299 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
2300 foreach_snat_protocol
2303 s = format (s, "unknown");
2306 s = format (s, "%s", t);
2310 static clib_error_t *
2311 add_static_mapping_command_fn (vlib_main_t * vm,
2312 unformat_input_t * input,
2313 vlib_cli_command_t * cmd)
2315 unformat_input_t _line_input, *line_input = &_line_input;
2316 clib_error_t * error = 0;
2317 ip4_address_t l_addr, e_addr;
2318 u32 l_port = 0, e_port = 0, vrf_id = ~0;
2321 u32 sw_if_index = ~0;
2322 vnet_main_t * vnm = vnet_get_main();
2324 snat_protocol_t proto = ~0;
2329 /* Get a line of input. */
2330 if (!unformat_user (input, unformat_line_input, line_input))
2333 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2335 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
2338 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
2340 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
2343 else if (unformat (line_input, "external %U", unformat_ip4_address,
2346 else if (unformat (line_input, "external %U %u",
2347 unformat_vnet_sw_interface, vnm, &sw_if_index,
2351 else if (unformat (line_input, "external %U",
2352 unformat_vnet_sw_interface, vnm, &sw_if_index))
2354 else if (unformat (line_input, "vrf %u", &vrf_id))
2356 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
2358 else if (unformat (line_input, "twice-nat"))
2360 else if (unformat (line_input, "out2in-only"))
2362 else if (unformat (line_input, "del"))
2366 error = clib_error_return (0, "unknown input: '%U'",
2367 format_unformat_error, line_input);
2372 if (twice_nat && addr_only)
2374 error = clib_error_return (0, "twice NAT only for 1:1 NAPT");
2378 if (!addr_only && !proto_set)
2380 error = clib_error_return (0, "missing protocol");
2384 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
2385 vrf_id, addr_only, sw_if_index, proto, is_add,
2386 twice_nat, out2in_only);
2390 case VNET_API_ERROR_INVALID_VALUE:
2391 error = clib_error_return (0, "External port already in use.");
2393 case VNET_API_ERROR_NO_SUCH_ENTRY:
2395 error = clib_error_return (0, "External addres must be allocated.");
2397 error = clib_error_return (0, "Mapping not exist.");
2399 case VNET_API_ERROR_NO_SUCH_FIB:
2400 error = clib_error_return (0, "No such VRF id.");
2402 case VNET_API_ERROR_VALUE_EXIST:
2403 error = clib_error_return (0, "Mapping already exist.");
2410 unformat_free (line_input);
2417 * @cliexstart{snat add static mapping}
2418 * Static mapping allows hosts on the external network to initiate connection
2419 * to to the local network host.
2420 * To create static mapping between local host address 10.0.0.3 port 6303 and
2421 * external address 4.4.4.4 port 3606 for TCP protocol use:
2422 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
2423 * If not runnig "static mapping only" NAT plugin mode use before:
2424 * vpp# nat44 add address 4.4.4.4
2425 * To create static mapping between local and external address use:
2426 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
2429 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
2430 .path = "nat44 add static mapping",
2431 .function = add_static_mapping_command_fn,
2433 "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
2434 "external <addr> [<port>] [vrf <table-id>] [twice-nat] [out2in-only] [del]",
2437 static clib_error_t *
2438 add_identity_mapping_command_fn (vlib_main_t * vm,
2439 unformat_input_t * input,
2440 vlib_cli_command_t * cmd)
2442 unformat_input_t _line_input, *line_input = &_line_input;
2443 clib_error_t * error = 0;
2445 u32 port = 0, vrf_id = ~0;
2448 u32 sw_if_index = ~0;
2449 vnet_main_t * vnm = vnet_get_main();
2451 snat_protocol_t proto;
2455 /* Get a line of input. */
2456 if (!unformat_user (input, unformat_line_input, line_input))
2459 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2461 if (unformat (line_input, "%U", unformat_ip4_address, &addr))
2463 else if (unformat (line_input, "external %U",
2464 unformat_vnet_sw_interface, vnm, &sw_if_index))
2466 else if (unformat (line_input, "vrf %u", &vrf_id))
2468 else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
2471 else if (unformat (line_input, "del"))
2475 error = clib_error_return (0, "unknown input: '%U'",
2476 format_unformat_error, line_input);
2481 rv = snat_add_static_mapping(addr, addr, (u16) port, (u16) port,
2482 vrf_id, addr_only, sw_if_index, proto, is_add,
2487 case VNET_API_ERROR_INVALID_VALUE:
2488 error = clib_error_return (0, "External port already in use.");
2490 case VNET_API_ERROR_NO_SUCH_ENTRY:
2492 error = clib_error_return (0, "External addres must be allocated.");
2494 error = clib_error_return (0, "Mapping not exist.");
2496 case VNET_API_ERROR_NO_SUCH_FIB:
2497 error = clib_error_return (0, "No such VRF id.");
2499 case VNET_API_ERROR_VALUE_EXIST:
2500 error = clib_error_return (0, "Mapping already exist.");
2507 unformat_free (line_input);
2514 * @cliexstart{snat add identity mapping}
2515 * Identity mapping translate an IP address to itself.
2516 * To create identity mapping for address 10.0.0.3 port 6303 for TCP protocol
2518 * vpp# nat44 add identity mapping 10.0.0.3 tcp 6303
2519 * To create identity mapping for address 10.0.0.3 use:
2520 * vpp# nat44 add identity mapping 10.0.0.3
2521 * To create identity mapping for DHCP addressed interface use:
2522 * vpp# nat44 add identity mapping GigabitEthernet0/a/0 tcp 3606
2525 VLIB_CLI_COMMAND (add_identity_mapping_command, static) = {
2526 .path = "nat44 add identity mapping",
2527 .function = add_identity_mapping_command_fn,
2528 .short_help = "nat44 add identity mapping <interface>|<ip4-addr> "
2529 "[<protocol> <port>] [vrf <table-id>] [del]",
2532 static clib_error_t *
2533 add_lb_static_mapping_command_fn (vlib_main_t * vm,
2534 unformat_input_t * input,
2535 vlib_cli_command_t * cmd)
2537 unformat_input_t _line_input, *line_input = &_line_input;
2538 clib_error_t * error = 0;
2539 ip4_address_t l_addr, e_addr;
2540 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
2543 snat_protocol_t proto;
2545 nat44_lb_addr_port_t *locals = 0, local;
2549 /* Get a line of input. */
2550 if (!unformat_user (input, unformat_line_input, line_input))
2553 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2555 if (unformat (line_input, "local %U:%u probability %u",
2556 unformat_ip4_address, &l_addr, &l_port, &probability))
2558 memset (&local, 0, sizeof (local));
2559 local.addr = l_addr;
2560 local.port = (u16) l_port;
2561 local.probability = (u8) probability;
2562 vec_add1 (locals, local);
2564 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
2567 else if (unformat (line_input, "vrf %u", &vrf_id))
2569 else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
2572 else if (unformat (line_input, "twice-nat"))
2574 else if (unformat (line_input, "out2in-only"))
2576 else if (unformat (line_input, "del"))
2580 error = clib_error_return (0, "unknown input: '%U'",
2581 format_unformat_error, line_input);
2586 if (vec_len (locals) < 2)
2588 error = clib_error_return (0, "at least two local must be set");
2594 error = clib_error_return (0, "missing protocol");
2598 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
2599 locals, is_add, twice_nat, out2in_only);
2603 case VNET_API_ERROR_INVALID_VALUE:
2604 error = clib_error_return (0, "External port already in use.");
2606 case VNET_API_ERROR_NO_SUCH_ENTRY:
2608 error = clib_error_return (0, "External addres must be allocated.");
2610 error = clib_error_return (0, "Mapping not exist.");
2612 case VNET_API_ERROR_VALUE_EXIST:
2613 error = clib_error_return (0, "Mapping already exist.");
2620 unformat_free (line_input);
2626 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
2627 .path = "nat44 add load-balancing static mapping",
2628 .function = add_lb_static_mapping_command_fn,
2630 "nat44 add load-balancing static mapping protocol tcp|udp "
2631 "external <addr>:<port> local <addr>:<port> probability <n> [twice-nat] "
2632 "[vrf <table-id>] [out2in-only] [del]",
2635 static clib_error_t *
2636 set_workers_command_fn (vlib_main_t * vm,
2637 unformat_input_t * input,
2638 vlib_cli_command_t * cmd)
2640 unformat_input_t _line_input, *line_input = &_line_input;
2643 clib_error_t *error = 0;
2645 /* Get a line of input. */
2646 if (!unformat_user (input, unformat_line_input, line_input))
2649 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2651 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
2655 error = clib_error_return (0, "unknown input '%U'",
2656 format_unformat_error, line_input);
2663 error = clib_error_return (0, "List of workers must be specified.");
2667 rv = snat_set_workers(bitmap);
2669 clib_bitmap_free (bitmap);
2673 case VNET_API_ERROR_INVALID_WORKER:
2674 error = clib_error_return (0, "Invalid worker(s).");
2676 case VNET_API_ERROR_FEATURE_DISABLED:
2677 error = clib_error_return (0,
2678 "Supported only if 2 or more workes available.");
2685 unformat_free (line_input);
2692 * @cliexstart{set snat workers}
2693 * Set NAT workers if 2 or more workers available, use:
2694 * vpp# set snat workers 0-2,5
2697 VLIB_CLI_COMMAND (set_workers_command, static) = {
2698 .path = "set nat workers",
2699 .function = set_workers_command_fn,
2701 "set nat workers <workers-list>",
2704 static clib_error_t *
2705 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
2706 unformat_input_t * input,
2707 vlib_cli_command_t * cmd)
2709 unformat_input_t _line_input, *line_input = &_line_input;
2714 clib_error_t *error = 0;
2716 /* Get a line of input. */
2717 if (!unformat_user (input, unformat_line_input, line_input))
2720 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2722 if (unformat (line_input, "domain %d", &domain_id))
2724 else if (unformat (line_input, "src-port %d", &src_port))
2726 else if (unformat (line_input, "disable"))
2730 error = clib_error_return (0, "unknown input '%U'",
2731 format_unformat_error, line_input);
2736 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
2740 error = clib_error_return (0, "ipfix logging enable failed");
2745 unformat_free (line_input);
2752 * @cliexstart{snat ipfix logging}
2753 * To enable NAT IPFIX logging use:
2754 * vpp# nat ipfix logging
2755 * To set IPFIX exporter use:
2756 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2759 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2760 .path = "nat ipfix logging",
2761 .function = snat_ipfix_logging_enable_disable_command_fn,
2762 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2766 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2768 snat_main_t *sm = &snat_main;
2769 u32 next_worker_index = 0;
2772 next_worker_index = sm->first_worker_index;
2773 hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) +
2774 (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >>24);
2776 if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers))))
2777 next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)];
2779 next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
2781 return next_worker_index;
2785 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2787 snat_main_t *sm = &snat_main;
2790 snat_session_key_t m_key;
2791 clib_bihash_kv_8_8_t kv, value;
2792 snat_static_mapping_t *m;
2793 nat_ed_ses_key_t key;
2794 clib_bihash_kv_16_8_t s_kv, s_value;
2795 snat_main_per_thread_data_t *tsm;
2799 u32 next_worker_index = 0;
2801 /* first try static mappings without port */
2802 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2804 m_key.addr = ip0->dst_address;
2807 m_key.fib_index = rx_fib_index0;
2808 kv.key = m_key.as_u64;
2809 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2811 m = pool_elt_at_index (sm->static_mappings, value.value);
2812 return m->worker_index;
2816 proto = ip_proto_to_snat_proto (ip0->protocol);
2817 udp = ip4_next_header (ip0);
2818 port = udp->dst_port;
2820 if (PREDICT_FALSE (ip4_is_fragment (ip0)))
2822 if (PREDICT_FALSE (nat_reass_is_drop_frag (0)))
2823 return vlib_get_thread_index ();
2825 if (PREDICT_TRUE (!ip4_is_first_fragment (ip0)))
2827 nat_reass_ip4_t *reass;
2829 reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address,
2830 ip0->fragment_id, ip0->protocol);
2832 if (reass && (reass->thread_index != (u32) ~ 0))
2833 return reass->thread_index;
2835 return vlib_get_thread_index ();
2839 /* unknown protocol */
2840 if (PREDICT_FALSE (proto == ~0))
2842 key.l_addr = ip0->dst_address;
2843 key.r_addr = ip0->src_address;
2844 key.fib_index = rx_fib_index0;
2845 key.proto = ip0->protocol;
2848 s_kv.key[0] = key.as_u64[0];
2849 s_kv.key[1] = key.as_u64[1];
2851 if (!clib_bihash_search_16_8 (&sm->out2in_ed, &s_kv, &s_value))
2853 for (i = 0; i < _vec_len (sm->per_thread_data); i++)
2855 tsm = vec_elt_at_index (sm->per_thread_data, i);
2856 if (!pool_is_free_index(tsm->sessions, s_value.value))
2858 s = pool_elt_at_index (tsm->sessions, s_value.value);
2859 if (s->out2in.addr.as_u32 == ip0->dst_address.as_u32 &&
2860 s->out2in.port == ip0->protocol &&
2861 snat_is_unk_proto_session (s))
2867 /* if no session use current thread */
2868 return vlib_get_thread_index ();
2871 if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
2873 icmp46_header_t * icmp = (icmp46_header_t *) udp;
2874 icmp_echo_header_t *echo = (icmp_echo_header_t *)(icmp + 1);
2875 if (!icmp_is_error_message (icmp))
2876 port = echo->identifier;
2879 ip4_header_t *inner_ip = (ip4_header_t *)(echo + 1);
2880 proto = ip_proto_to_snat_proto (inner_ip->protocol);
2881 void *l4_header = ip4_next_header (inner_ip);
2884 case SNAT_PROTOCOL_ICMP:
2885 icmp = (icmp46_header_t*)l4_header;
2886 echo = (icmp_echo_header_t *)(icmp + 1);
2887 port = echo->identifier;
2889 case SNAT_PROTOCOL_UDP:
2890 case SNAT_PROTOCOL_TCP:
2891 port = ((tcp_udp_header_t*)l4_header)->src_port;
2894 return vlib_get_thread_index ();
2899 /* try static mappings with port */
2900 if (PREDICT_FALSE (pool_elts (sm->static_mappings)))
2902 m_key.addr = ip0->dst_address;
2903 m_key.port = clib_net_to_host_u16 (port);
2904 m_key.protocol = proto;
2905 m_key.fib_index = rx_fib_index0;
2906 kv.key = m_key.as_u64;
2907 if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
2909 m = pool_elt_at_index (sm->static_mappings, value.value);
2910 return m->worker_index;
2914 /* worker by outside port */
2915 next_worker_index = sm->first_worker_index;
2916 next_worker_index +=
2917 sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
2918 return next_worker_index;
2921 static clib_error_t *
2922 snat_config (vlib_main_t * vm, unformat_input_t * input)
2924 snat_main_t * sm = &snat_main;
2925 u32 translation_buckets = 1024;
2926 u32 translation_memory_size = 128<<20;
2927 u32 user_buckets = 128;
2928 u32 user_memory_size = 64<<20;
2929 u32 max_translations_per_user = 100;
2930 u32 outside_vrf_id = 0;
2931 u32 inside_vrf_id = 0;
2932 u32 static_mapping_buckets = 1024;
2933 u32 static_mapping_memory_size = 64<<20;
2934 u32 nat64_bib_buckets = 1024;
2935 u32 nat64_bib_memory_size = 128 << 20;
2936 u32 nat64_st_buckets = 2048;
2937 u32 nat64_st_memory_size = 256 << 20;
2938 u8 static_mapping_only = 0;
2939 u8 static_mapping_connection_tracking = 0;
2940 snat_main_per_thread_data_t *tsm;
2941 dslite_main_t * dm = &dslite_main;
2943 sm->deterministic = 0;
2946 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2948 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2950 else if (unformat (input, "translation hash memory %d",
2951 &translation_memory_size));
2952 else if (unformat (input, "user hash buckets %d", &user_buckets))
2954 else if (unformat (input, "user hash memory %d",
2957 else if (unformat (input, "max translations per user %d",
2958 &max_translations_per_user))
2960 else if (unformat (input, "outside VRF id %d",
2963 else if (unformat (input, "inside VRF id %d",
2966 else if (unformat (input, "static mapping only"))
2968 static_mapping_only = 1;
2969 if (unformat (input, "connection tracking"))
2970 static_mapping_connection_tracking = 1;
2972 else if (unformat (input, "deterministic"))
2973 sm->deterministic = 1;
2974 else if (unformat (input, "nat64 bib hash buckets %d",
2975 &nat64_bib_buckets))
2977 else if (unformat (input, "nat64 bib hash memory %d",
2978 &nat64_bib_memory_size))
2980 else if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets))
2982 else if (unformat (input, "nat64 st hash memory %d",
2983 &nat64_st_memory_size))
2985 else if (unformat (input, "out2in dpo"))
2987 else if (unformat (input, "dslite ce"))
2988 dslite_set_ce(dm, 1);
2990 return clib_error_return (0, "unknown input '%U'",
2991 format_unformat_error, input);
2994 /* for show commands, etc. */
2995 sm->translation_buckets = translation_buckets;
2996 sm->translation_memory_size = translation_memory_size;
2997 /* do not exceed load factor 10 */
2998 sm->max_translations = 10 * translation_buckets;
2999 sm->user_buckets = user_buckets;
3000 sm->user_memory_size = user_memory_size;
3001 sm->max_translations_per_user = max_translations_per_user;
3002 sm->outside_vrf_id = outside_vrf_id;
3003 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
3005 FIB_SOURCE_PLUGIN_HI);
3006 sm->inside_vrf_id = inside_vrf_id;
3007 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
3009 FIB_SOURCE_PLUGIN_HI);
3010 sm->static_mapping_only = static_mapping_only;
3011 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
3013 nat64_set_hash(nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets,
3014 nat64_st_memory_size);
3016 if (sm->deterministic)
3018 sm->in2out_node_index = snat_det_in2out_node.index;
3019 sm->in2out_output_node_index = ~0;
3020 sm->out2in_node_index = snat_det_out2in_node.index;
3021 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
3022 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
3026 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
3027 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
3028 sm->in2out_node_index = snat_in2out_node.index;
3029 sm->in2out_output_node_index = snat_in2out_output_node.index;
3030 sm->out2in_node_index = snat_out2in_node.index;
3031 if (!static_mapping_only ||
3032 (static_mapping_only && static_mapping_connection_tracking))
3034 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
3035 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
3037 vec_foreach (tsm, sm->per_thread_data)
3039 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
3040 translation_memory_size);
3042 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
3043 translation_memory_size);
3045 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
3049 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
3050 translation_buckets, translation_memory_size);
3052 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
3053 translation_buckets, translation_memory_size);
3057 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
3058 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
3060 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
3061 "static_mapping_by_local", static_mapping_buckets,
3062 static_mapping_memory_size);
3064 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
3065 "static_mapping_by_external", static_mapping_buckets,
3066 static_mapping_memory_size);
3072 VLIB_CONFIG_FUNCTION (snat_config, "nat");
3074 u8 * format_snat_session_state (u8 * s, va_list * args)
3076 u32 i = va_arg (*args, u32);
3081 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
3082 foreach_snat_session_state
3085 t = format (t, "unknown");
3087 s = format (s, "%s", t);
3091 u8 * format_snat_key (u8 * s, va_list * args)
3093 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
3095 s = format (s, "%U proto %U port %d fib %d",
3096 format_ip4_address, &key->addr,
3097 format_snat_protocol, key->protocol,
3098 clib_net_to_host_u16 (key->port), key->fib_index);
3102 u8 * format_snat_session (u8 * s, va_list * args)
3104 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
3105 snat_session_t * sess = va_arg (*args, snat_session_t *);
3107 if (snat_is_unk_proto_session (sess))
3109 s = format (s, " i2o %U proto %u fib %u\n",
3110 format_ip4_address, &sess->in2out.addr,
3111 clib_net_to_host_u16 (sess->in2out.port),
3112 sess->in2out.fib_index);
3113 s = format (s, " o2i %U proto %u fib %u\n",
3114 format_ip4_address, &sess->out2in.addr,
3115 clib_net_to_host_u16 (sess->out2in.port),
3116 sess->out2in.fib_index);
3120 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
3121 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
3123 if (is_twice_nat_session (sess))
3125 s = format (s, " external host o2i %U:%d i2o %U:%d\n",
3126 format_ip4_address, &sess->ext_host_addr,
3127 clib_net_to_host_u16 (sess->ext_host_port),
3128 format_ip4_address, &sess->ext_host_nat_addr,
3129 clib_net_to_host_u16 (sess->ext_host_nat_port));
3133 if (sess->ext_host_addr.as_u32)
3134 s = format (s, " external host %U\n",
3135 format_ip4_address, &sess->ext_host_addr);
3137 s = format (s, " last heard %.2f\n", sess->last_heard);
3138 s = format (s, " total pkts %d, total bytes %lld\n",
3139 sess->total_pkts, sess->total_bytes);
3140 if (snat_is_session_static (sess))
3141 s = format (s, " static translation\n");
3143 s = format (s, " dynamic translation\n");
3144 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
3145 s = format (s, " load-balancing\n");
3146 if (is_twice_nat_session (sess))
3147 s = format (s, " twice-nat\n");
3152 u8 * format_snat_user (u8 * s, va_list * args)
3154 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
3155 snat_user_t * u = va_arg (*args, snat_user_t *);
3156 int verbose = va_arg (*args, int);
3157 dlist_elt_t * head, * elt;
3158 u32 elt_index, head_index;
3160 snat_session_t * sess;
3162 s = format (s, "%U: %d dynamic translations, %d static translations\n",
3163 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
3168 if (u->nsessions || u->nstaticsessions)
3170 head_index = u->sessions_per_user_list_head_index;
3171 head = pool_elt_at_index (sm->list_pool, head_index);
3173 elt_index = head->next;
3174 elt = pool_elt_at_index (sm->list_pool, elt_index);
3175 session_index = elt->value;
3177 while (session_index != ~0)
3179 sess = pool_elt_at_index (sm->sessions, session_index);
3181 s = format (s, " %U\n", format_snat_session, sm, sess);
3183 elt_index = elt->next;
3184 elt = pool_elt_at_index (sm->list_pool, elt_index);
3185 session_index = elt->value;
3192 u8 * format_snat_static_mapping (u8 * s, va_list * args)
3194 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
3195 nat44_lb_addr_port_t *local;
3198 s = format (s, "local %U external %U vrf %d %s",
3199 format_ip4_address, &m->local_addr,
3200 format_ip4_address, &m->external_addr,
3201 m->vrf_id, m->twice_nat ? "twice-nat" : "");
3204 if (vec_len (m->locals))
3206 s = format (s, "%U vrf %d external %U:%d %s %s",
3207 format_snat_protocol, m->proto,
3209 format_ip4_address, &m->external_addr, m->external_port,
3210 m->twice_nat ? "twice-nat" : "",
3211 m->out2in_only ? "out2in-only" : "");
3212 vec_foreach (local, m->locals)
3213 s = format (s, "\n local %U:%d probability %d\%",
3214 format_ip4_address, &local->addr, local->port,
3215 local->probability);
3218 s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
3219 format_snat_protocol, m->proto,
3220 format_ip4_address, &m->local_addr, m->local_port,
3221 format_ip4_address, &m->external_addr, m->external_port,
3222 m->vrf_id, m->twice_nat ? "twice-nat" : "",
3223 m->out2in_only ? "out2in-only" : "");
3228 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
3230 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
3231 vnet_main_t *vnm = vnet_get_main();
3234 s = format (s, "local %U external %U vrf %d",
3235 format_ip4_address, &m->l_addr,
3236 format_vnet_sw_interface_name, vnm,
3237 vnet_get_sw_interface (vnm, m->sw_if_index),
3240 s = format (s, "%U local %U:%d external %U:%d vrf %d",
3241 format_snat_protocol, m->proto,
3242 format_ip4_address, &m->l_addr, m->l_port,
3243 format_vnet_sw_interface_name, vnm,
3244 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
3250 u8 * format_det_map_ses (u8 * s, va_list * args)
3252 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
3253 ip4_address_t in_addr, out_addr;
3254 u32 in_offset, out_offset;
3255 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
3256 u32 * i = va_arg (*args, u32 *);
3258 u32 user_index = *i / SNAT_DET_SES_PER_USER;
3259 in_addr.as_u32 = clib_host_to_net_u32 (
3260 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
3261 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
3262 clib_net_to_host_u32(det_map->in_addr.as_u32);
3263 out_offset = in_offset / det_map->sharing_ratio;
3264 out_addr.as_u32 = clib_host_to_net_u32(
3265 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
3266 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
3267 format_ip4_address, &in_addr,
3268 clib_net_to_host_u16 (ses->in_port),
3269 format_ip4_address, &out_addr,
3270 clib_net_to_host_u16 (ses->out.out_port),
3271 format_ip4_address, &ses->out.ext_host_addr,
3272 clib_net_to_host_u16 (ses->out.ext_host_port),
3273 format_snat_session_state, ses->state,
3279 static clib_error_t *
3280 show_snat_command_fn (vlib_main_t * vm,
3281 unformat_input_t * input,
3282 vlib_cli_command_t * cmd)
3285 snat_main_t * sm = &snat_main;
3287 snat_static_mapping_t *m;
3288 snat_interface_t *i;
3289 snat_address_t * ap;
3290 vnet_main_t *vnm = vnet_get_main();
3291 snat_main_per_thread_data_t *tsm;
3292 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
3294 snat_static_map_resolve_t *rp;
3295 snat_det_map_t * dm;
3296 snat_det_session_t * ses;
3298 if (unformat (input, "detail"))
3300 else if (unformat (input, "verbose"))
3303 if (sm->static_mapping_only)
3305 if (sm->static_mapping_connection_tracking)
3306 vlib_cli_output (vm, "NAT plugin mode: static mapping only connection "
3309 vlib_cli_output (vm, "NAT plugin mode: static mapping only");
3311 else if (sm->deterministic)
3313 vlib_cli_output (vm, "NAT plugin mode: deterministic mapping");
3317 vlib_cli_output (vm, "NAT plugin mode: dynamic translations enabled");
3322 pool_foreach (i, sm->interfaces,
3324 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
3325 vnet_get_sw_interface (vnm, i->sw_if_index),
3326 (nat_interface_is_inside(i) &&
3327 nat_interface_is_outside(i)) ? "in out" :
3328 (nat_interface_is_inside(i) ? "in" : "out"));
3331 pool_foreach (i, sm->output_feature_interfaces,
3333 vlib_cli_output (vm, "%U output-feature %s",
3334 format_vnet_sw_interface_name, vnm,
3335 vnet_get_sw_interface (vnm, i->sw_if_index),
3336 (nat_interface_is_inside(i) &&
3337 nat_interface_is_outside(i)) ? "in out" :
3338 (nat_interface_is_inside(i) ? "in" : "out"));
3341 if (vec_len (sm->auto_add_sw_if_indices))
3343 vlib_cli_output (vm, "NAT44 pool addresses interfaces:");
3344 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
3346 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
3347 vnet_get_sw_interface (vnm, *sw_if_index));
3351 if (vec_len (sm->auto_add_sw_if_indices_twice_nat))
3353 vlib_cli_output (vm, "NAT44 twice-nat pool addresses interfaces:");
3354 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices_twice_nat)
3356 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
3357 vnet_get_sw_interface (vnm, *sw_if_index));
3361 vlib_cli_output (vm, "NAT44 pool addresses:");
3362 vec_foreach (ap, sm->addresses)
3364 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
3365 if (ap->fib_index != ~0)
3366 vlib_cli_output (vm, " tenant VRF: %u",
3367 ip4_fib_get(ap->fib_index)->table_id);
3369 vlib_cli_output (vm, " tenant VRF independent");
3370 #define _(N, i, n, s) \
3371 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
3372 foreach_snat_protocol
3376 vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
3377 vec_foreach (ap, sm->twice_nat_addresses)
3379 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
3380 if (ap->fib_index != ~0)
3381 vlib_cli_output (vm, " tenant VRF: %u",
3382 ip4_fib_get(ap->fib_index)->table_id);
3384 vlib_cli_output (vm, " tenant VRF independent");
3385 #define _(N, i, n, s) \
3386 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
3387 foreach_snat_protocol
3392 if (sm->num_workers > 1)
3394 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
3397 vec_foreach (worker, sm->workers)
3399 vlib_worker_thread_t *w =
3400 vlib_worker_threads + *worker + sm->first_worker_index;
3401 vlib_cli_output (vm, " %s", w->name);
3406 if (sm->deterministic)
3408 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
3409 vlib_cli_output (vm, "tcp-established timeout: %dsec",
3410 sm->tcp_established_timeout);
3411 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
3412 sm->tcp_transitory_timeout);
3413 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
3414 vlib_cli_output (vm, "%d deterministic mappings",
3415 pool_elts (sm->det_maps));
3418 pool_foreach (dm, sm->det_maps,
3420 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
3421 format_ip4_address, &dm->in_addr, dm->in_plen,
3422 format_ip4_address, &dm->out_addr, dm->out_plen);
3423 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
3425 vlib_cli_output (vm, " number of ports per inside host: %d\n",
3426 dm->ports_per_host);
3427 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
3430 vec_foreach_index (j, dm->sessions)
3432 ses = vec_elt_at_index (dm->sessions, j);
3434 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
3443 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
3445 vlib_cli_output (vm, "%d static mappings",
3446 pool_elts (sm->static_mappings));
3450 pool_foreach (m, sm->static_mappings,
3452 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
3458 vec_foreach (tsm, sm->per_thread_data)
3460 users_num += pool_elts (tsm->users);
3461 sessions_num += pool_elts (tsm->sessions);
3464 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
3465 " %d static mappings, %d twice-nat addresses",
3467 vec_len (sm->addresses),
3469 pool_elts (sm->static_mappings),
3470 vec_len (sm->twice_nat_addresses));
3474 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->in2out_ed,
3476 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed,
3478 vec_foreach_index (j, sm->per_thread_data)
3480 tsm = vec_elt_at_index (sm->per_thread_data, j);
3482 if (pool_elts (tsm->users) == 0)
3485 vlib_worker_thread_t *w = vlib_worker_threads + j;
3486 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
3488 vlib_cli_output (vm, " %U", format_bihash_8_8, &tsm->in2out,
3490 vlib_cli_output (vm, " %U", format_bihash_8_8, &tsm->out2in,
3492 vlib_cli_output (vm, " %d list pool elements",
3493 pool_elts (tsm->list_pool));
3495 pool_foreach (u, tsm->users,
3497 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
3502 if (pool_elts (sm->static_mappings))
3504 vlib_cli_output (vm, "static mappings:");
3505 pool_foreach (m, sm->static_mappings,
3507 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
3509 for (j = 0; j < vec_len (sm->to_resolve); j++)
3511 rp = sm->to_resolve + j;
3512 vlib_cli_output (vm, "%U",
3513 format_snat_static_map_to_resolve, rp);
3523 VLIB_CLI_COMMAND (show_snat_command, static) = {
3524 .path = "show nat44",
3525 .short_help = "show nat44",
3526 .function = show_snat_command_fn,
3531 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
3534 ip4_address_t * address,
3536 u32 if_address_index,
3539 snat_main_t *sm = &snat_main;
3540 snat_static_map_resolve_t *rp;
3541 u32 *indices_to_delete = 0;
3542 ip4_address_t l_addr;
3546 snat_address_t *addresses = sm->addresses;
3548 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
3550 if (sw_if_index == sm->auto_add_sw_if_indices[i])
3554 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices_twice_nat); i++)
3557 addresses = sm->twice_nat_addresses;
3558 if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i])
3567 /* Don't trip over lease renewal, static config */
3568 for (j = 0; j < vec_len(addresses); j++)
3569 if (addresses[j].addr.as_u32 == address->as_u32)
3572 snat_add_address (sm, address, ~0, twice_nat);
3573 /* Scan static map resolution vector */
3574 for (j = 0; j < vec_len (sm->to_resolve); j++)
3576 rp = sm->to_resolve + j;
3577 /* On this interface? */
3578 if (rp->sw_if_index == sw_if_index)
3580 /* Indetity mapping? */
3581 if (rp->l_addr.as_u32 == 0)
3582 l_addr.as_u32 = address[0].as_u32;
3584 l_addr.as_u32 = rp->l_addr.as_u32;
3585 /* Add the static mapping */
3586 rv = snat_add_static_mapping (l_addr,
3592 ~0 /* sw_if_index */,
3597 clib_warning ("snat_add_static_mapping returned %d",
3599 vec_add1 (indices_to_delete, j);
3602 /* If we resolved any of the outstanding static mappings */
3603 if (vec_len(indices_to_delete))
3606 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3607 vec_delete(sm->to_resolve, 1, j);
3608 vec_free(indices_to_delete);
3614 (void) snat_del_address(sm, address[0], 1, twice_nat);
3620 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del,
3623 ip4_main_t * ip4_main = sm->ip4_main;
3624 ip4_address_t * first_int_addr;
3625 snat_static_map_resolve_t *rp;
3626 u32 *indices_to_delete = 0;
3628 u32 *auto_add_sw_if_indices =
3629 twice_nat ? sm->auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices;
3631 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
3632 0 /* just want the address*/);
3634 for (i = 0; i < vec_len(auto_add_sw_if_indices); i++)
3636 if (auto_add_sw_if_indices[i] == sw_if_index)
3640 /* if have address remove it */
3642 (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat);
3645 for (j = 0; j < vec_len (sm->to_resolve); j++)
3647 rp = sm->to_resolve + j;
3648 if (rp->sw_if_index == sw_if_index)
3649 vec_add1 (indices_to_delete, j);
3651 if (vec_len(indices_to_delete))
3653 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
3654 vec_del1(sm->to_resolve, j);
3655 vec_free(indices_to_delete);
3659 vec_del1(sm->auto_add_sw_if_indices_twice_nat, i);
3661 vec_del1(sm->auto_add_sw_if_indices, i);
3664 return VNET_API_ERROR_VALUE_EXIST;
3671 return VNET_API_ERROR_NO_SUCH_ENTRY;
3673 /* add to the auto-address list */
3675 vec_add1(sm->auto_add_sw_if_indices_twice_nat, sw_if_index);
3677 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
3679 /* If the address is already bound - or static - add it now */
3681 snat_add_address (sm, first_int_addr, ~0, twice_nat);
3686 static clib_error_t *
3687 snat_add_interface_address_command_fn (vlib_main_t * vm,
3688 unformat_input_t * input,
3689 vlib_cli_command_t * cmd)
3691 snat_main_t *sm = &snat_main;
3692 unformat_input_t _line_input, *line_input = &_line_input;
3696 clib_error_t *error = 0;
3699 /* Get a line of input. */
3700 if (!unformat_user (input, unformat_line_input, line_input))
3703 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3705 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
3706 sm->vnet_main, &sw_if_index))
3708 else if (unformat (line_input, "twice-nat"))
3710 else if (unformat (line_input, "del"))
3714 error = clib_error_return (0, "unknown input '%U'",
3715 format_unformat_error, line_input);
3720 rv = snat_add_interface_address (sm, sw_if_index, is_del, twice_nat);
3728 error = clib_error_return (0, "snat_add_interface_address returned %d",
3734 unformat_free (line_input);
3739 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
3740 .path = "nat44 add interface address",
3741 .short_help = "nat44 add interface address <interface> [twice-nat] [del]",
3742 .function = snat_add_interface_address_command_fn,
3746 nat44_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port,
3747 snat_protocol_t proto, u32 vrf_id, int is_in)
3749 snat_main_per_thread_data_t *tsm;
3750 clib_bihash_kv_8_8_t kv, value;
3752 u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
3753 snat_session_key_t key;
3755 clib_bihash_8_8_t *t;
3756 snat_user_key_t u_key;
3759 ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32;
3760 if (sm->num_workers)
3762 vec_elt_at_index (sm->per_thread_data,
3763 sm->worker_in2out_cb (&ip, fib_index));
3765 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
3767 key.addr.as_u32 = addr->as_u32;
3768 key.port = clib_host_to_net_u16 (port);
3769 key.protocol = proto;
3770 key.fib_index = fib_index;
3771 kv.key = key.as_u64;
3772 t = is_in ? &tsm->in2out : &tsm->out2in;
3773 if (!clib_bihash_search_8_8 (t, &kv, &value))
3775 s = pool_elt_at_index (tsm->sessions, value.value);
3776 kv.key = s->in2out.as_u64;
3777 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
3778 kv.key = s->out2in.as_u64;
3779 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
3780 u_key.addr = s->in2out.addr;
3781 u_key.fib_index = s->in2out.fib_index;
3782 kv.key = u_key.as_u64;
3783 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
3785 u = pool_elt_at_index (tsm->users, value.value);
3788 clib_dlist_remove (tsm->list_pool, s->per_user_index);
3789 pool_put (tsm->sessions, s);
3793 return VNET_API_ERROR_NO_SUCH_ENTRY;
3796 static clib_error_t *
3797 nat44_del_session_command_fn (vlib_main_t * vm,
3798 unformat_input_t * input,
3799 vlib_cli_command_t * cmd)
3801 snat_main_t *sm = &snat_main;
3802 unformat_input_t _line_input, *line_input = &_line_input;
3804 clib_error_t *error = 0;
3806 u32 port = 0, vrf_id = sm->outside_vrf_id;
3807 snat_protocol_t proto;
3810 /* Get a line of input. */
3811 if (!unformat_user (input, unformat_line_input, line_input))
3814 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3816 if (unformat (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
3817 unformat_snat_protocol, &proto))
3819 else if (unformat (line_input, "in"))
3822 vrf_id = sm->inside_vrf_id;
3824 else if (unformat (line_input, "vrf %u", &vrf_id))
3828 error = clib_error_return (0, "unknown input '%U'",
3829 format_unformat_error, line_input);
3834 rv = nat44_del_session(sm, &addr, port, proto, vrf_id, is_in);
3842 error = clib_error_return (0, "nat44_del_session returned %d", rv);
3847 unformat_free (line_input);
3852 VLIB_CLI_COMMAND (nat44_del_session_command, static) = {
3853 .path = "nat44 del session",
3854 .short_help = "nat44 del session in|out <addr>:<port> tcp|udp|icmp [vrf <id>]",
3855 .function = nat44_del_session_command_fn,
3858 static clib_error_t *
3859 nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
3860 unformat_input_t * input,
3861 vlib_cli_command_t * cmd)
3863 snat_main_t *sm = &snat_main;
3864 unformat_input_t _line_input, *line_input = &_line_input;
3865 clib_error_t *error = 0;
3866 u32 psid, psid_offset, psid_length;
3868 /* Get a line of input. */
3869 if (!unformat_user (input, unformat_line_input, line_input))
3872 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3874 if (unformat (line_input, "default"))
3875 sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
3876 else if (unformat (line_input, "map-e psid %d psid-offset %d psid-len %d",
3877 &psid, &psid_offset, &psid_length))
3879 sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
3880 sm->psid = (u16) psid;
3881 sm->psid_offset = (u16) psid_offset;
3882 sm->psid_length = (u16) psid_length;
3886 error = clib_error_return (0, "unknown input '%U'",
3887 format_unformat_error, line_input);
3893 unformat_free (line_input);
3898 VLIB_CLI_COMMAND (nat44_set_alloc_addr_and_port_alg_command, static) = {
3899 .path = "nat addr-port-assignment-alg",
3900 .short_help = "nat addr-port-assignment-alg <alg-name> [<alg-params>]",
3901 .function = nat44_set_alloc_addr_and_port_alg_command_fn,
3904 static clib_error_t *
3905 snat_det_map_command_fn (vlib_main_t * vm,
3906 unformat_input_t * input,
3907 vlib_cli_command_t * cmd)
3909 snat_main_t *sm = &snat_main;
3910 unformat_input_t _line_input, *line_input = &_line_input;
3911 ip4_address_t in_addr, out_addr;
3912 u32 in_plen, out_plen;
3914 clib_error_t *error = 0;
3916 /* Get a line of input. */
3917 if (!unformat_user (input, unformat_line_input, line_input))
3920 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3922 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
3924 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
3926 else if (unformat (line_input, "del"))
3930 error = clib_error_return (0, "unknown input '%U'",
3931 format_unformat_error, line_input);
3936 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
3941 error = clib_error_return (0, "snat_det_add_map return %d", rv);
3946 unformat_free (line_input);
3953 * @cliexstart{snat deterministic add}
3954 * Create bijective mapping of inside address to outside address and port range
3955 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
3957 * To create deterministic mapping between inside network 10.0.0.0/18 and
3958 * outside network 1.1.1.0/30 use:
3959 * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
3962 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
3963 .path = "nat44 deterministic add",
3964 .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
3965 .function = snat_det_map_command_fn,
3968 static clib_error_t *
3969 snat_det_forward_command_fn (vlib_main_t * vm,
3970 unformat_input_t * input,
3971 vlib_cli_command_t * cmd)
3973 snat_main_t *sm = &snat_main;
3974 unformat_input_t _line_input, *line_input = &_line_input;
3975 ip4_address_t in_addr, out_addr;
3977 snat_det_map_t * dm;
3978 clib_error_t *error = 0;
3980 /* Get a line of input. */
3981 if (!unformat_user (input, unformat_line_input, line_input))
3984 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3986 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
3990 error = clib_error_return (0, "unknown input '%U'",
3991 format_unformat_error, line_input);
3996 dm = snat_det_map_by_user(sm, &in_addr);
3998 vlib_cli_output (vm, "no match");
4001 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
4002 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
4003 lo_port, lo_port + dm->ports_per_host - 1);
4007 unformat_free (line_input);
4014 * @cliexstart{snat deterministic forward}
4015 * Return outside address and port range from inside address for deterministic
4017 * To obtain outside address and port of inside host use:
4018 * vpp# nat44 deterministic forward 10.0.0.2
4019 * 1.1.1.0:<1054-1068>
4022 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
4023 .path = "nat44 deterministic forward",
4024 .short_help = "nat44 deterministic forward <addr>",
4025 .function = snat_det_forward_command_fn,
4028 static clib_error_t *
4029 snat_det_reverse_command_fn (vlib_main_t * vm,
4030 unformat_input_t * input,
4031 vlib_cli_command_t * cmd)
4033 snat_main_t *sm = &snat_main;
4034 unformat_input_t _line_input, *line_input = &_line_input;
4035 ip4_address_t in_addr, out_addr;
4037 snat_det_map_t * dm;
4038 clib_error_t *error = 0;
4040 /* Get a line of input. */
4041 if (!unformat_user (input, unformat_line_input, line_input))
4044 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
4046 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
4050 error = clib_error_return (0, "unknown input '%U'",
4051 format_unformat_error, line_input);
4056 if (out_port < 1024 || out_port > 65535)
4058 error = clib_error_return (0, "wrong port, must be <1024-65535>");
4062 dm = snat_det_map_by_out(sm, &out_addr);
4064 vlib_cli_output (vm, "no match");
4067 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
4068 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
4072 unformat_free (line_input);
4079 * @cliexstart{snat deterministic reverse}
4080 * Return inside address from outside address and port for deterministic NAT.
4081 * To obtain inside host address from outside address and port use:
4082 * #vpp nat44 deterministic reverse 1.1.1.1:1276
4086 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
4087 .path = "nat44 deterministic reverse",
4088 .short_help = "nat44 deterministic reverse <addr>:<port>",
4089 .function = snat_det_reverse_command_fn,
4092 static clib_error_t *
4093 set_timeout_command_fn (vlib_main_t * vm,
4094 unformat_input_t * input,
4095 vlib_cli_command_t * cmd)
4097 snat_main_t *sm = &snat_main;
4098 unformat_input_t _line_input, *line_input = &_line_input;
4099 clib_error_t *error = 0;
4101 /* Get a line of input. */
4102 if (!unformat_user (input, unformat_line_input, line_input))
4105 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
4107 if (unformat (line_input, "udp %u", &sm->udp_timeout))
4109 else if (unformat (line_input, "tcp-established %u",
4110 &sm->tcp_established_timeout))
4112 else if (unformat (line_input, "tcp-transitory %u",
4113 &sm->tcp_transitory_timeout))
4115 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
4117 else if (unformat (line_input, "reset"))
4119 sm->udp_timeout = SNAT_UDP_TIMEOUT;
4120 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
4121 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
4122 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
4126 error = clib_error_return (0, "unknown input '%U'",
4127 format_unformat_error, line_input);
4133 unformat_free (line_input);
4140 * @cliexstart{set snat deterministic timeout}
4141 * Set values of timeouts for deterministic NAT (in seconds), use:
4142 * vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
4143 * tcp-transitory 250 icmp 90
4144 * To reset default values use:
4145 * vpp# set nat44 deterministic timeout reset
4148 VLIB_CLI_COMMAND (set_timeout_command, static) = {
4149 .path = "set nat44 deterministic timeout",
4150 .function = set_timeout_command_fn,
4152 "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
4153 "tcp-transitory <sec> | icmp <sec> | reset]",
4156 static clib_error_t *
4157 snat_det_close_session_out_fn (vlib_main_t *vm,
4158 unformat_input_t * input,
4159 vlib_cli_command_t * cmd)
4161 snat_main_t *sm = &snat_main;
4162 unformat_input_t _line_input, *line_input = &_line_input;
4163 ip4_address_t out_addr, ext_addr, in_addr;
4164 u32 out_port, ext_port;
4165 snat_det_map_t * dm;
4166 snat_det_session_t * ses;
4167 snat_det_out_key_t key;
4168 clib_error_t *error = 0;
4170 /* Get a line of input. */
4171 if (!unformat_user (input, unformat_line_input, line_input))
4174 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
4176 if (unformat (line_input, "%U:%d %U:%d",
4177 unformat_ip4_address, &out_addr, &out_port,
4178 unformat_ip4_address, &ext_addr, &ext_port))
4182 error = clib_error_return (0, "unknown input '%U'",
4183 format_unformat_error, line_input);
4188 unformat_free (line_input);
4190 dm = snat_det_map_by_out(sm, &out_addr);
4192 vlib_cli_output (vm, "no match");
4195 snat_det_reverse(dm, &ext_addr, (u16)out_port, &in_addr);
4196 key.ext_host_addr = out_addr;
4197 key.ext_host_port = ntohs((u16)ext_port);
4198 key.out_port = ntohs((u16)out_port);
4199 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
4201 vlib_cli_output (vm, "no match");
4203 snat_det_ses_close(dm, ses);
4207 unformat_free (line_input);
4214 * @cliexstart{snat deterministic close session out}
4215 * Close session using outside ip address and port
4216 * and external ip address and port, use:
4217 * vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
4220 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
4221 .path = "nat44 deterministic close session out",
4222 .short_help = "nat44 deterministic close session out "
4223 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
4224 .function = snat_det_close_session_out_fn,
4227 static clib_error_t *
4228 snat_det_close_session_in_fn (vlib_main_t *vm,
4229 unformat_input_t * input,
4230 vlib_cli_command_t * cmd)
4232 snat_main_t *sm = &snat_main;
4233 unformat_input_t _line_input, *line_input = &_line_input;
4234 ip4_address_t in_addr, ext_addr;
4235 u32 in_port, ext_port;
4236 snat_det_map_t * dm;
4237 snat_det_session_t * ses;
4238 snat_det_out_key_t key;
4239 clib_error_t *error = 0;
4241 /* Get a line of input. */
4242 if (!unformat_user (input, unformat_line_input, line_input))
4245 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
4247 if (unformat (line_input, "%U:%d %U:%d",
4248 unformat_ip4_address, &in_addr, &in_port,
4249 unformat_ip4_address, &ext_addr, &ext_port))
4253 error = clib_error_return (0, "unknown input '%U'",
4254 format_unformat_error, line_input);
4259 unformat_free (line_input);
4261 dm = snat_det_map_by_user (sm, &in_addr);
4263 vlib_cli_output (vm, "no match");
4266 key.ext_host_addr = ext_addr;
4267 key.ext_host_port = ntohs ((u16)ext_port);
4268 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs((u16)in_port), key);
4270 vlib_cli_output (vm, "no match");
4272 snat_det_ses_close(dm, ses);
4276 unformat_free(line_input);
4283 * @cliexstart{snat deterministic close_session_in}
4284 * Close session using inside ip address and port
4285 * and external ip address and port, use:
4286 * vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
4289 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
4290 .path = "nat44 deterministic close session in",
4291 .short_help = "nat44 deterministic close session in "
4292 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
4293 .function = snat_det_close_session_in_fn,
4296 static clib_error_t *
4297 snat_forwarding_set_command_fn (vlib_main_t *vm,
4298 unformat_input_t * input,
4299 vlib_cli_command_t * cmd)
4301 snat_main_t *sm = &snat_main;
4302 unformat_input_t _line_input, *line_input = &_line_input;
4303 u8 forwarding_enable;
4304 u8 forwarding_enable_set = 0;
4305 clib_error_t *error = 0;
4307 /* Get a line of input. */
4308 if (!unformat_user (input, unformat_line_input, line_input))
4309 return clib_error_return (0, "'enable' or 'disable' expected");
4311 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
4313 if (!forwarding_enable_set && unformat (line_input, "enable"))
4315 forwarding_enable = 1;
4316 forwarding_enable_set = 1;
4318 else if (!forwarding_enable_set && unformat (line_input, "disable"))
4320 forwarding_enable = 0;
4321 forwarding_enable_set = 1;
4325 error = clib_error_return (0, "unknown input '%U'",
4326 format_unformat_error, line_input);
4331 if (!forwarding_enable_set)
4333 error = clib_error_return (0, "'enable' or 'disable' expected");
4337 sm->forwarding_enabled = forwarding_enable;
4340 unformat_free(line_input);
4347 * @cliexstart{nat44 forwarding}
4348 * Enable or disable forwarding
4349 * Forward packets which don't match existing translation
4350 * or static mapping instead of dropping them.
4351 * To enable forwarding, use:
4352 * vpp# nat44 forwarding enable
4353 * To disable forwarding, use:
4354 * vpp# nat44 forwarding disable
4357 VLIB_CLI_COMMAND (snat_forwarding_set_command, static) = {
4358 .path = "nat44 forwarding",
4359 .short_help = "nat44 forwarding enable|disable",
4360 .function = snat_forwarding_set_command_fn,