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_ipfix_logging.h>
24 #include <nat/nat_det.h>
25 #include <nat/nat64.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/ip4_fib.h>
29 #include <vpp/app/version.h>
31 snat_main_t snat_main;
34 /* Hook up input features */
35 VNET_FEATURE_INIT (ip4_snat_in2out, static) = {
36 .arc_name = "ip4-unicast",
37 .node_name = "nat44-in2out",
38 .runs_before = VNET_FEATURES ("nat44-out2in"),
40 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41 .arc_name = "ip4-unicast",
42 .node_name = "nat44-out2in",
43 .runs_before = VNET_FEATURES ("ip4-lookup"),
45 VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = {
46 .arc_name = "ip4-unicast",
47 .node_name = "nat44-det-in2out",
48 .runs_before = VNET_FEATURES ("nat44-det-out2in"),
50 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51 .arc_name = "ip4-unicast",
52 .node_name = "nat44-det-out2in",
53 .runs_before = VNET_FEATURES ("ip4-lookup"),
55 VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = {
56 .arc_name = "ip4-unicast",
57 .node_name = "nat44-in2out-worker-handoff",
58 .runs_before = VNET_FEATURES ("nat44-out2in-worker-handoff"),
60 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "nat44-out2in-worker-handoff",
63 .runs_before = VNET_FEATURES ("ip4-lookup"),
65 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
66 .arc_name = "ip4-unicast",
67 .node_name = "nat44-in2out-fast",
68 .runs_before = VNET_FEATURES ("nat44-out2in-fast"),
70 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71 .arc_name = "ip4-unicast",
72 .node_name = "nat44-out2in-fast",
73 .runs_before = VNET_FEATURES ("ip4-lookup"),
75 VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = {
76 .arc_name = "ip4-unicast",
77 .node_name = "nat44-hairpin-dst",
78 .runs_before = VNET_FEATURES ("ip4-lookup"),
81 /* Hook up output features */
82 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
83 .arc_name = "ip4-output",
84 .node_name = "nat44-in2out-output",
85 .runs_before = VNET_FEATURES ("interface-output"),
87 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
88 .arc_name = "ip4-output",
89 .node_name = "nat44-in2out-output-worker-handoff",
90 .runs_before = VNET_FEATURES ("interface-output"),
92 VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = {
93 .arc_name = "ip4-output",
94 .node_name = "nat44-hairpin-src",
95 .runs_before = VNET_FEATURES ("interface-output"),
100 VLIB_PLUGIN_REGISTER () = {
101 .version = VPP_BUILD_VER,
102 .description = "Network Address Translation",
107 * @brief Add/del NAT address to FIB.
109 * Add the external NAT address to the FIB as receive entries. This ensures
110 * that VPP will reply to ARP for this address and we don't need to enable
111 * proxy ARP on the outside interface.
113 * @param addr IPv4 address.
114 * @param plen address prefix length
115 * @param sw_if_index Interface.
116 * @param is_add If 0 delete, otherwise add.
119 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
122 fib_prefix_t prefix = {
124 .fp_proto = FIB_PROTOCOL_IP4,
126 .ip4.as_u32 = addr->as_u32,
129 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
132 fib_table_entry_update_one_path(fib_index,
134 FIB_SOURCE_PLUGIN_HI,
135 (FIB_ENTRY_FLAG_CONNECTED |
136 FIB_ENTRY_FLAG_LOCAL |
137 FIB_ENTRY_FLAG_EXCLUSIVE),
144 FIB_ROUTE_PATH_FLAG_NONE);
146 fib_table_entry_delete(fib_index,
148 FIB_SOURCE_PLUGIN_HI);
151 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
155 vlib_thread_main_t *tm = vlib_get_thread_main ();
160 /* Check if address already exists */
161 vec_foreach (ap, sm->addresses)
163 if (ap->addr.as_u32 == addr->as_u32)
167 vec_add2 (sm->addresses, ap, 1);
171 fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
172 FIB_SOURCE_PLUGIN_HI);
175 #define _(N, i, n, s) \
176 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \
177 ap->busy_##n##_ports = 0; \
178 vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0);
179 foreach_snat_protocol
182 /* Add external address to FIB */
183 pool_foreach (i, sm->interfaces,
188 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
191 pool_foreach (i, sm->output_feature_interfaces,
196 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
201 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
204 snat_static_mapping_t *m;
205 pool_foreach (m, sm->static_mappings,
207 if (m->external_addr.as_u32 == addr.as_u32)
214 void increment_v4_address (ip4_address_t * a)
218 v = clib_net_to_host_u32(a->as_u32) + 1;
219 a->as_u32 = clib_host_to_net_u32(v);
223 snat_add_static_mapping_when_resolved (snat_main_t * sm,
224 ip4_address_t l_addr,
229 snat_protocol_t proto,
233 snat_static_map_resolve_t *rp;
235 vec_add2 (sm->to_resolve, rp, 1);
236 rp->l_addr.as_u32 = l_addr.as_u32;
238 rp->sw_if_index = sw_if_index;
242 rp->addr_only = addr_only;
247 * @brief Add static mapping.
249 * Create static mapping between local addr+port and external addr+port.
251 * @param l_addr Local IPv4 address.
252 * @param e_addr External IPv4 address.
253 * @param l_port Local port number.
254 * @param e_port External port number.
255 * @param vrf_id VRF ID.
256 * @param addr_only If 0 address port and pair mapping, otherwise address only.
257 * @param sw_if_index External port instead of specific IP address.
258 * @param is_add If 0 delete static mapping, otherwise add.
262 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
263 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
264 u32 sw_if_index, snat_protocol_t proto, int is_add)
266 snat_main_t * sm = &snat_main;
267 snat_static_mapping_t *m;
268 snat_session_key_t m_key;
269 clib_bihash_kv_8_8_t kv, value;
270 snat_address_t *a = 0;
273 snat_interface_t *interface;
276 /* If the external address is a specific interface address */
277 if (sw_if_index != ~0)
279 ip4_address_t * first_int_addr;
281 /* Might be already set... */
282 first_int_addr = ip4_interface_first_address
283 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
285 /* DHCP resolution required? */
286 if (first_int_addr == 0)
288 snat_add_static_mapping_when_resolved
289 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
294 e_addr.as_u32 = first_int_addr->as_u32;
298 m_key.port = addr_only ? 0 : e_port;
299 m_key.protocol = addr_only ? 0 : proto;
300 m_key.fib_index = sm->outside_fib_index;
301 kv.key = m_key.as_u64;
302 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
305 m = pool_elt_at_index (sm->static_mappings, value.value);
310 return VNET_API_ERROR_VALUE_EXIST;
312 /* Convert VRF id to FIB index */
315 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
317 return VNET_API_ERROR_NO_SUCH_FIB;
320 /* If not specified use inside VRF id from SNAT plugin startup config */
323 fib_index = sm->inside_fib_index;
324 vrf_id = sm->inside_vrf_id;
327 /* Find external address in allocated addresses and reserve port for
328 address and port pair mapping when dynamic translations enabled */
329 if (!addr_only && !(sm->static_mapping_only))
331 for (i = 0; i < vec_len (sm->addresses); i++)
333 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
335 a = sm->addresses + i;
336 /* External port must be unused */
339 #define _(N, j, n, s) \
340 case SNAT_PROTOCOL_##N: \
341 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
342 return VNET_API_ERROR_INVALID_VALUE; \
343 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
346 a->busy_##n##_ports++; \
347 a->busy_##n##_ports_per_thread[e_port / sm->port_per_thread]++; \
350 foreach_snat_protocol
353 clib_warning("unknown_protocol");
354 return VNET_API_ERROR_INVALID_VALUE_2;
359 /* External address must be allocated */
361 return VNET_API_ERROR_NO_SUCH_ENTRY;
364 pool_get (sm->static_mappings, m);
365 memset (m, 0, sizeof (*m));
366 m->local_addr = l_addr;
367 m->external_addr = e_addr;
368 m->addr_only = addr_only;
370 m->fib_index = fib_index;
373 m->local_port = l_port;
374 m->external_port = e_port;
378 m_key.addr = m->local_addr;
379 m_key.port = m->local_port;
380 m_key.protocol = m->proto;
381 m_key.fib_index = m->fib_index;
382 kv.key = m_key.as_u64;
383 kv.value = m - sm->static_mappings;
384 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
386 m_key.addr = m->external_addr;
387 m_key.port = m->external_port;
388 m_key.fib_index = sm->outside_fib_index;
389 kv.key = m_key.as_u64;
390 kv.value = m - sm->static_mappings;
391 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
396 snat_user_key_t w_key0;
397 snat_worker_key_t w_key1;
399 w_key0.addr = m->local_addr;
400 w_key0.fib_index = m->fib_index;
401 kv.key = w_key0.as_u64;
403 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
405 kv.value = sm->first_worker_index +
406 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
408 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
412 kv.value = value.value;
415 w_key1.addr = m->external_addr;
416 w_key1.port = clib_host_to_net_u16 (m->external_port);
417 w_key1.fib_index = sm->outside_fib_index;
418 kv.key = w_key1.as_u64;
419 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
425 return VNET_API_ERROR_NO_SUCH_ENTRY;
427 /* Free external address port */
428 if (!addr_only && !(sm->static_mapping_only))
430 for (i = 0; i < vec_len (sm->addresses); i++)
432 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
434 a = sm->addresses + i;
437 #define _(N, j, n, s) \
438 case SNAT_PROTOCOL_##N: \
439 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
442 a->busy_##n##_ports--; \
443 a->busy_##n##_ports_per_thread[e_port / sm->port_per_thread]--; \
446 foreach_snat_protocol
449 clib_warning("unknown_protocol");
450 return VNET_API_ERROR_INVALID_VALUE_2;
457 m_key.addr = m->local_addr;
458 m_key.port = m->local_port;
459 m_key.protocol = m->proto;
460 m_key.fib_index = m->fib_index;
461 kv.key = m_key.as_u64;
462 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
464 m_key.addr = m->external_addr;
465 m_key.port = m->external_port;
466 m_key.fib_index = sm->outside_fib_index;
467 kv.key = m_key.as_u64;
468 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
470 /* Delete session(s) for static mapping if exist */
471 if (!(sm->static_mapping_only) ||
472 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
474 snat_user_key_t u_key;
476 dlist_elt_t * head, * elt;
477 u32 elt_index, head_index, del_elt_index;
481 snat_main_per_thread_data_t *tsm;
483 u_key.addr = m->local_addr;
484 u_key.fib_index = m->fib_index;
485 kv.key = u_key.as_u64;
486 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
487 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
489 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
490 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
492 user_index = value.value;
493 u = pool_elt_at_index (tsm->users, user_index);
494 if (u->nstaticsessions)
496 head_index = u->sessions_per_user_list_head_index;
497 head = pool_elt_at_index (tsm->list_pool, head_index);
498 elt_index = head->next;
499 elt = pool_elt_at_index (tsm->list_pool, elt_index);
500 ses_index = elt->value;
501 while (ses_index != ~0)
503 s = pool_elt_at_index (tsm->sessions, ses_index);
504 del_elt_index = elt_index;
505 elt_index = elt->next;
506 elt = pool_elt_at_index (tsm->list_pool, elt_index);
507 ses_index = elt->value;
511 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
512 (clib_net_to_host_u16 (s->out2in.port) != e_port))
516 if (snat_is_unk_proto_session (s))
518 clib_bihash_kv_16_8_t up_kv;
519 nat_ed_ses_key_t up_key;
520 up_key.l_addr = s->in2out.addr;
521 up_key.r_addr = s->ext_host_addr;
522 up_key.fib_index = s->in2out.fib_index;
523 up_key.proto = s->in2out.port;
526 up_kv.key[0] = up_key.as_u64[0];
527 up_kv.key[1] = up_key.as_u64[1];
528 if (clib_bihash_add_del_16_8 (&sm->in2out_ed,
530 clib_warning ("in2out key del failed");
532 up_key.l_addr = s->out2in.addr;
533 up_key.fib_index = s->out2in.fib_index;
534 up_kv.key[0] = up_key.as_u64[0];
535 up_kv.key[1] = up_key.as_u64[1];
536 if (clib_bihash_add_del_16_8 (&sm->out2in_ed,
538 clib_warning ("out2in key del failed");
543 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
544 s->out2in.addr.as_u32,
548 s->in2out.fib_index);
550 value.key = s->in2out.as_u64;
551 if (clib_bihash_add_del_8_8 (&tsm->in2out, &value, 0))
552 clib_warning ("in2out key del failed");
553 value.key = s->out2in.as_u64;
554 if (clib_bihash_add_del_8_8 (&tsm->out2in, &value, 0))
555 clib_warning ("out2in key del failed");
557 pool_put (tsm->sessions, s);
559 clib_dlist_remove (tsm->list_pool, del_elt_index);
560 pool_put_index (tsm->list_pool, del_elt_index);
561 u->nstaticsessions--;
568 pool_put (tsm->users, u);
569 clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
575 /* Delete static mapping from pool */
576 pool_put (sm->static_mappings, m);
582 /* Add/delete external address to FIB */
583 pool_foreach (interface, sm->interfaces,
585 if (interface->is_inside)
588 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
591 pool_foreach (interface, sm->output_feature_interfaces,
593 if (interface->is_inside)
596 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
603 int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
604 snat_protocol_t proto, u32 vrf_id,
605 nat44_lb_addr_port_t *locals, u8 is_add)
607 snat_main_t * sm = &snat_main;
608 snat_static_mapping_t *m;
609 snat_session_key_t m_key;
610 clib_bihash_kv_8_8_t kv, value;
612 snat_address_t *a = 0;
614 nat44_lb_addr_port_t *local;
615 snat_user_key_t w_key0;
616 snat_worker_key_t w_key1;
617 u32 worker_index = 0;
618 snat_main_per_thread_data_t *tsm;
622 m_key.protocol = proto;
623 m_key.fib_index = sm->outside_fib_index;
624 kv.key = m_key.as_u64;
625 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
628 m = pool_elt_at_index (sm->static_mappings, value.value);
633 return VNET_API_ERROR_VALUE_EXIST;
635 if (vec_len (locals) < 2)
636 return VNET_API_ERROR_INVALID_VALUE;
638 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
640 FIB_SOURCE_PLUGIN_HI);
642 /* Find external address in allocated addresses and reserve port for
643 address and port pair mapping when dynamic translations enabled */
644 if (!sm->static_mapping_only)
646 for (i = 0; i < vec_len (sm->addresses); i++)
648 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
650 a = sm->addresses + i;
651 /* External port must be unused */
654 #define _(N, j, n, s) \
655 case SNAT_PROTOCOL_##N: \
656 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
657 return VNET_API_ERROR_INVALID_VALUE; \
658 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
661 a->busy_##n##_ports++; \
662 a->busy_##n##_ports_per_thread[e_port / sm->port_per_thread]++; \
665 foreach_snat_protocol
668 clib_warning("unknown_protocol");
669 return VNET_API_ERROR_INVALID_VALUE_2;
674 /* External address must be allocated */
676 return VNET_API_ERROR_NO_SUCH_ENTRY;
679 pool_get (sm->static_mappings, m);
680 memset (m, 0, sizeof (*m));
681 m->external_addr = e_addr;
684 m->fib_index = fib_index;
685 m->external_port = e_port;
688 m_key.addr = m->external_addr;
689 m_key.port = m->external_port;
690 m_key.protocol = m->proto;
691 m_key.fib_index = sm->outside_fib_index;
692 kv.key = m_key.as_u64;
693 kv.value = m - sm->static_mappings;
694 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1))
696 clib_warning ("static_mapping_by_external key add failed");
697 return VNET_API_ERROR_UNSPECIFIED;
703 w_key0.addr = locals[0].addr;
704 w_key0.fib_index = fib_index;
705 kv.key = w_key0.as_u64;
707 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
708 worker_index = sm->first_worker_index +
709 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
711 worker_index = value.value;
713 w_key1.addr = m->external_addr;
714 w_key1.port = clib_host_to_net_u16 (m->external_port);
715 w_key1.fib_index = sm->outside_fib_index;
716 kv.key = w_key1.as_u64;
717 kv.value = worker_index;
718 if (clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1))
720 clib_warning ("worker-by-out add key failed");
721 return VNET_API_ERROR_UNSPECIFIED;
723 tsm = vec_elt_at_index (sm->per_thread_data, worker_index);
726 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
728 m_key.port = clib_host_to_net_u16 (m->external_port);
729 kv.key = m_key.as_u64;
731 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 1))
733 clib_warning ("static_mapping_by_local key add failed");
734 return VNET_API_ERROR_UNSPECIFIED;
737 m_key.fib_index = m->fib_index;
738 for (i = 0; i < vec_len (locals); i++)
740 m_key.addr = locals[i].addr;
741 m_key.port = locals[i].port;
742 kv.key = m_key.as_u64;
743 kv.value = m - sm->static_mappings;
744 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
745 locals[i].prefix = (i == 0) ? locals[i].probability :\
746 (locals[i - 1].prefix + locals[i].probability);
747 vec_add1 (m->locals, locals[i]);
748 m_key.port = clib_host_to_net_u16 (locals[i].port);
749 kv.key = m_key.as_u64;
751 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 1))
753 clib_warning ("in2out key add failed");
754 return VNET_API_ERROR_UNSPECIFIED;
759 w_key0.addr = locals[i].addr;
760 w_key0.fib_index = fib_index;
761 kv.key = w_key0.as_u64;
762 kv.value = worker_index;
763 if (clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1))
765 clib_warning ("worker-by-in key add failed");
766 return VNET_API_ERROR_UNSPECIFIED;
774 return VNET_API_ERROR_NO_SUCH_ENTRY;
776 fib_table_unlock (m->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_HI);
778 /* Free external address port */
779 if (!sm->static_mapping_only)
781 for (i = 0; i < vec_len (sm->addresses); i++)
783 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
785 a = sm->addresses + i;
788 #define _(N, j, n, s) \
789 case SNAT_PROTOCOL_##N: \
790 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
793 a->busy_##n##_ports--; \
794 a->busy_##n##_ports_per_thread[e_port / sm->port_per_thread]--; \
797 foreach_snat_protocol
800 clib_warning("unknown_protocol");
801 return VNET_API_ERROR_INVALID_VALUE_2;
808 w_key1.addr = m->external_addr;
809 w_key1.port = clib_host_to_net_u16 (m->external_port);
810 w_key1.fib_index = sm->outside_fib_index;
811 kv.key = w_key1.as_u64;
812 if (!clib_bihash_search_8_8 (&sm->worker_by_out, &kv, &value))
813 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
815 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
817 m_key.addr = m->external_addr;
818 m_key.port = m->external_port;
819 m_key.protocol = m->proto;
820 m_key.fib_index = sm->outside_fib_index;
821 kv.key = m_key.as_u64;
822 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0))
824 clib_warning ("static_mapping_by_external key del failed");
825 return VNET_API_ERROR_UNSPECIFIED;
827 m_key.port = clib_host_to_net_u16 (m->external_port);
828 kv.key = m_key.as_u64;
829 if (clib_bihash_add_del_8_8(&tsm->out2in, &kv, 0))
831 clib_warning ("outi2in key del failed");
832 return VNET_API_ERROR_UNSPECIFIED;
835 vec_foreach (local, m->locals)
837 m_key.addr = local->addr;
838 m_key.port = local->port;
839 m_key.fib_index = m->fib_index;
840 kv.key = m_key.as_u64;
841 if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0))
843 clib_warning ("static_mapping_by_local key del failed");
844 return VNET_API_ERROR_UNSPECIFIED;
846 m_key.port = clib_host_to_net_u16 (local->port);
847 kv.key = m_key.as_u64;
848 if (clib_bihash_add_del_8_8(&tsm->in2out, &kv, 0))
850 clib_warning ("in2out key del failed");
851 return VNET_API_ERROR_UNSPECIFIED;
856 pool_put (sm->static_mappings, m);
862 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
864 snat_address_t *a = 0;
866 u32 *ses_to_be_removed = 0, *ses_index;
867 clib_bihash_kv_8_8_t kv, value;
868 snat_user_key_t user_key;
870 snat_main_per_thread_data_t *tsm;
871 snat_static_mapping_t *m;
872 snat_interface_t *interface;
875 /* Find SNAT address */
876 for (i=0; i < vec_len (sm->addresses); i++)
878 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
880 a = sm->addresses + i;
885 return VNET_API_ERROR_NO_SUCH_ENTRY;
889 pool_foreach (m, sm->static_mappings,
891 if (m->external_addr.as_u32 == addr.as_u32)
892 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
893 m->local_port, m->external_port,
894 m->vrf_id, m->addr_only, ~0,
900 /* Check if address is used in some static mapping */
901 if (is_snat_address_used_in_static_mapping(sm, addr))
903 clib_warning ("address used in static mapping");
904 return VNET_API_ERROR_UNSPECIFIED;
908 if (a->fib_index != ~0)
909 fib_table_unlock(a->fib_index, FIB_PROTOCOL_IP4,
910 FIB_SOURCE_PLUGIN_HI);
912 /* Delete sessions using address */
913 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
915 vec_foreach (tsm, sm->per_thread_data)
917 pool_foreach (ses, tsm->sessions, ({
918 if (ses->out2in.addr.as_u32 == addr.as_u32)
920 if (snat_is_unk_proto_session (ses))
922 clib_bihash_kv_16_8_t up_kv;
923 nat_ed_ses_key_t up_key;
924 up_key.l_addr = ses->in2out.addr;
925 up_key.r_addr = ses->ext_host_addr;
926 up_key.fib_index = ses->in2out.fib_index;
927 up_key.proto = ses->in2out.port;
930 up_kv.key[0] = up_key.as_u64[0];
931 up_kv.key[1] = up_key.as_u64[1];
932 if (clib_bihash_add_del_16_8 (&sm->in2out_ed,
934 clib_warning ("in2out key del failed");
936 up_key.l_addr = ses->out2in.addr;
937 up_key.fib_index = ses->out2in.fib_index;
938 up_kv.key[0] = up_key.as_u64[0];
939 up_kv.key[1] = up_key.as_u64[1];
940 if (clib_bihash_add_del_16_8 (&sm->out2in_ed,
942 clib_warning ("out2in key del failed");
947 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
948 ses->out2in.addr.as_u32,
949 ses->in2out.protocol,
952 ses->in2out.fib_index);
953 kv.key = ses->in2out.as_u64;
954 clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0);
955 kv.key = ses->out2in.as_u64;
956 clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0);
958 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
959 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
960 user_key.addr = ses->in2out.addr;
961 user_key.fib_index = ses->in2out.fib_index;
962 kv.key = user_key.as_u64;
963 if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
965 u = pool_elt_at_index (tsm->users, value.value);
971 vec_foreach (ses_index, ses_to_be_removed)
972 pool_put_index (tsm->sessions, ses_index[0]);
974 vec_free (ses_to_be_removed);
978 vec_del1 (sm->addresses, i);
980 /* Delete external address from FIB */
981 pool_foreach (interface, sm->interfaces,
983 if (interface->is_inside)
986 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
989 pool_foreach (interface, sm->output_feature_interfaces,
991 if (interface->is_inside)
994 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
1001 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
1003 snat_main_t *sm = &snat_main;
1004 snat_interface_t *i;
1005 const char * feature_name;
1006 snat_address_t * ap;
1007 snat_static_mapping_t * m;
1008 snat_det_map_t * dm;
1010 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1011 feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast";
1014 if (sm->num_workers > 1 && !sm->deterministic)
1015 feature_name = is_inside ? "nat44-in2out-worker-handoff" : "nat44-out2in-worker-handoff";
1016 else if (sm->deterministic)
1017 feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in";
1019 feature_name = is_inside ? "nat44-in2out" : "nat44-out2in";
1022 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
1025 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1026 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
1028 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
1029 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1031 pool_foreach (i, sm->interfaces,
1033 if (i->sw_if_index == sw_if_index)
1036 pool_put (sm->interfaces, i);
1038 return VNET_API_ERROR_VALUE_EXIST;
1045 return VNET_API_ERROR_NO_SUCH_ENTRY;
1047 pool_get (sm->interfaces, i);
1048 i->sw_if_index = sw_if_index;
1049 i->is_inside = is_inside;
1051 /* Add/delete external addresses to FIB */
1056 vec_foreach (ap, sm->addresses)
1057 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1059 pool_foreach (m, sm->static_mappings,
1061 if (!(m->addr_only))
1064 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1067 pool_foreach (dm, sm->det_maps,
1069 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
1075 int snat_interface_add_del_output_feature (u32 sw_if_index,
1079 snat_main_t *sm = &snat_main;
1080 snat_interface_t *i;
1081 snat_address_t * ap;
1082 snat_static_mapping_t * m;
1084 if (sm->deterministic ||
1085 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
1086 return VNET_API_ERROR_UNSUPPORTED;
1090 vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst",
1091 sw_if_index, !is_del, 0, 0);
1092 vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src",
1093 sw_if_index, !is_del, 0, 0);
1097 if (sm->num_workers > 1)
1099 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in-worker-handoff",
1100 sw_if_index, !is_del, 0, 0);
1101 vnet_feature_enable_disable ("ip4-output",
1102 "nat44-in2out-output-worker-handoff",
1103 sw_if_index, !is_del, 0, 0);
1107 vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", sw_if_index,
1109 vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output",
1110 sw_if_index, !is_del, 0, 0);
1114 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
1115 sm->fq_in2out_output_index =
1116 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
1118 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
1119 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
1121 pool_foreach (i, sm->output_feature_interfaces,
1123 if (i->sw_if_index == sw_if_index)
1126 pool_put (sm->output_feature_interfaces, i);
1128 return VNET_API_ERROR_VALUE_EXIST;
1135 return VNET_API_ERROR_NO_SUCH_ENTRY;
1137 pool_get (sm->output_feature_interfaces, i);
1138 i->sw_if_index = sw_if_index;
1139 i->is_inside = is_inside;
1141 /* Add/delete external addresses to FIB */
1146 vec_foreach (ap, sm->addresses)
1147 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
1149 pool_foreach (m, sm->static_mappings,
1151 if (!(m->addr_only))
1154 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
1160 int snat_set_workers (uword * bitmap)
1162 snat_main_t *sm = &snat_main;
1165 if (sm->num_workers < 2)
1166 return VNET_API_ERROR_FEATURE_DISABLED;
1168 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
1169 return VNET_API_ERROR_INVALID_WORKER;
1171 vec_free (sm->workers);
1172 clib_bitmap_foreach (i, bitmap,
1174 vec_add1(sm->workers, i);
1175 sm->per_thread_data[i].snat_thread_index = j;
1179 sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers);
1180 sm->num_snat_thread = _vec_len (sm->workers);
1187 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
1190 ip4_address_t * address,
1192 u32 if_address_index,
1195 static clib_error_t * snat_init (vlib_main_t * vm)
1197 snat_main_t * sm = &snat_main;
1198 clib_error_t * error = 0;
1199 ip4_main_t * im = &ip4_main;
1200 ip_lookup_main_t * lm = &im->lookup_main;
1202 vlib_thread_registration_t *tr;
1203 vlib_thread_main_t *tm = vlib_get_thread_main ();
1206 ip4_add_del_interface_address_callback_t cb4;
1209 sm->vnet_main = vnet_get_main();
1211 sm->ip4_lookup_main = lm;
1212 sm->api_main = &api_main;
1213 sm->first_worker_index = 0;
1214 sm->next_worker = 0;
1215 sm->num_workers = 0;
1216 sm->num_snat_thread = 1;
1218 sm->port_per_thread = 0xffff - 1024;
1219 sm->fq_in2out_index = ~0;
1220 sm->fq_out2in_index = ~0;
1221 sm->udp_timeout = SNAT_UDP_TIMEOUT;
1222 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
1223 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
1224 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
1226 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1229 tr = (vlib_thread_registration_t *) p[0];
1232 sm->num_workers = tr->count;
1233 sm->first_worker_index = tr->first_index;
1237 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1239 /* Use all available workers by default */
1240 if (sm->num_workers > 1)
1242 for (i=0; i < sm->num_workers; i++)
1243 bitmap = clib_bitmap_set (bitmap, i, 1);
1244 snat_set_workers(bitmap);
1245 clib_bitmap_free (bitmap);
1249 sm->per_thread_data[0].snat_thread_index = 0;
1252 error = snat_api_init(vm, sm);
1256 /* Set up the interface address add/del callback */
1257 cb4.function = snat_ip4_add_del_interface_address_cb;
1258 cb4.function_opaque = 0;
1260 vec_add1 (im->add_del_interface_address_callbacks, cb4);
1262 /* Init IPFIX logging */
1263 snat_ipfix_logging_init(vm);
1265 error = nat64_init(vm);
1270 VLIB_INIT_FUNCTION (snat_init);
1272 void snat_free_outside_address_and_port (snat_main_t * sm,
1274 snat_session_key_t * k,
1278 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1280 ASSERT (address_index < vec_len (sm->addresses));
1282 a = sm->addresses + address_index;
1284 switch (k->protocol)
1286 #define _(N, i, n, s) \
1287 case SNAT_PROTOCOL_##N: \
1288 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
1289 port_host_byte_order) == 1); \
1290 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
1291 port_host_byte_order, 0); \
1292 a->busy_##n##_ports--; \
1293 a->busy_##n##_ports_per_thread[thread_index]--; \
1295 foreach_snat_protocol
1298 clib_warning("unknown_protocol");
1304 * @brief Match NAT44 static mapping.
1306 * @param sm NAT main.
1307 * @param match Address and port to match.
1308 * @param mapping External or local address and port of the matched mapping.
1309 * @param by_external If 0 match by local address otherwise match by external
1311 * @param is_addr_only If matched mapping is address only
1313 * @returns 0 if match found otherwise 1.
1315 int snat_static_mapping_match (snat_main_t * sm,
1316 snat_session_key_t match,
1317 snat_session_key_t * mapping,
1321 clib_bihash_kv_8_8_t kv, value;
1322 snat_static_mapping_t *m;
1323 snat_session_key_t m_key;
1324 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1325 u32 rand, lo = 0, hi, mid;
1328 mapping_hash = &sm->static_mapping_by_external;
1330 m_key.addr = match.addr;
1331 m_key.port = clib_net_to_host_u16 (match.port);
1332 m_key.protocol = match.protocol;
1333 m_key.fib_index = match.fib_index;
1335 kv.key = m_key.as_u64;
1337 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1339 /* Try address only mapping */
1342 kv.key = m_key.as_u64;
1343 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1347 m = pool_elt_at_index (sm->static_mappings, value.value);
1351 if (vec_len (m->locals))
1353 hi = vec_len (m->locals) - 1;
1354 rand = 1 + (random_u32 (&sm->random_seed) % m->locals[hi].prefix);
1357 mid = ((hi - lo) >> 1) + lo;
1358 (rand > m->locals[mid].prefix) ? (lo = mid + 1) : (hi = mid);
1360 if (!(m->locals[lo].prefix >= rand))
1362 mapping->addr = m->locals[lo].addr;
1363 mapping->port = clib_host_to_net_u16 (m->locals[lo].port);
1367 mapping->addr = m->local_addr;
1368 /* Address only mapping doesn't change port */
1369 mapping->port = m->addr_only ? match.port
1370 : clib_host_to_net_u16 (m->local_port);
1372 mapping->fib_index = m->fib_index;
1373 mapping->protocol = m->proto;
1377 mapping->addr = m->external_addr;
1378 /* Address only mapping doesn't change port */
1379 mapping->port = m->addr_only ? match.port
1380 : clib_host_to_net_u16 (m->external_port);
1381 mapping->fib_index = sm->outside_fib_index;
1384 if (PREDICT_FALSE(is_addr_only != 0))
1385 *is_addr_only = m->addr_only;
1390 static_always_inline u16
1391 snat_random_port (snat_main_t * sm, u16 min, u16 max)
1393 return min + random_u32 (&sm->random_seed) /
1394 (random_u32_max() / (max - min + 1) + 1);
1397 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1400 snat_session_key_t * k,
1401 u32 * address_indexp)
1407 for (i = 0; i < vec_len (sm->addresses); i++)
1409 a = sm->addresses + i;
1410 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1412 switch (k->protocol)
1414 #define _(N, j, n, s) \
1415 case SNAT_PROTOCOL_##N: \
1416 if (a->busy_##n##_ports_per_thread[thread_index] < sm->port_per_thread) \
1420 portnum = (sm->port_per_thread * \
1421 sm->per_thread_data[thread_index].snat_thread_index) + \
1422 snat_random_port(sm, 1, sm->port_per_thread) + 1024; \
1423 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1425 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1426 a->busy_##n##_ports_per_thread[thread_index]++; \
1427 a->busy_##n##_ports++; \
1428 k->addr = a->addr; \
1429 k->port = clib_host_to_net_u16(portnum); \
1430 *address_indexp = i; \
1435 foreach_snat_protocol
1438 clib_warning("unknown protocol");
1443 /* Totally out of translations to use... */
1444 snat_ipfix_logging_addresses_exhausted(0);
1449 static clib_error_t *
1450 add_address_command_fn (vlib_main_t * vm,
1451 unformat_input_t * input,
1452 vlib_cli_command_t * cmd)
1454 unformat_input_t _line_input, *line_input = &_line_input;
1455 snat_main_t * sm = &snat_main;
1456 ip4_address_t start_addr, end_addr, this_addr;
1457 u32 start_host_order, end_host_order;
1462 clib_error_t *error = 0;
1464 /* Get a line of input. */
1465 if (!unformat_user (input, unformat_line_input, line_input))
1468 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1470 if (unformat (line_input, "%U - %U",
1471 unformat_ip4_address, &start_addr,
1472 unformat_ip4_address, &end_addr))
1474 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1476 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1477 end_addr = start_addr;
1478 else if (unformat (line_input, "del"))
1482 error = clib_error_return (0, "unknown input '%U'",
1483 format_unformat_error, line_input);
1488 if (sm->static_mapping_only)
1490 error = clib_error_return (0, "static mapping only mode");
1494 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1495 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1497 if (end_host_order < start_host_order)
1499 error = clib_error_return (0, "end address less than start address");
1503 count = (end_host_order - start_host_order) + 1;
1506 clib_warning ("%U - %U, %d addresses...",
1507 format_ip4_address, &start_addr,
1508 format_ip4_address, &end_addr,
1511 this_addr = start_addr;
1513 for (i = 0; i < count; i++)
1516 snat_add_address (sm, &this_addr, vrf_id);
1518 rv = snat_del_address (sm, this_addr, 0);
1522 case VNET_API_ERROR_NO_SUCH_ENTRY:
1523 error = clib_error_return (0, "S-NAT address not exist.");
1525 case VNET_API_ERROR_UNSPECIFIED:
1526 error = clib_error_return (0, "S-NAT address used in static mapping.");
1532 increment_v4_address (&this_addr);
1536 unformat_free (line_input);
1541 VLIB_CLI_COMMAND (add_address_command, static) = {
1542 .path = "nat44 add address",
1543 .short_help = "nat44 add address <ip4-range-start> [- <ip4-range-end>] "
1544 "[tenant-vrf <vrf-id>] [del]",
1545 .function = add_address_command_fn,
1548 static clib_error_t *
1549 snat_feature_command_fn (vlib_main_t * vm,
1550 unformat_input_t * input,
1551 vlib_cli_command_t * cmd)
1553 unformat_input_t _line_input, *line_input = &_line_input;
1554 vnet_main_t * vnm = vnet_get_main();
1555 clib_error_t * error = 0;
1557 u32 * inside_sw_if_indices = 0;
1558 u32 * outside_sw_if_indices = 0;
1559 u8 is_output_feature = 0;
1565 /* Get a line of input. */
1566 if (!unformat_user (input, unformat_line_input, line_input))
1569 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1571 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1573 vec_add1 (inside_sw_if_indices, sw_if_index);
1574 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1576 vec_add1 (outside_sw_if_indices, sw_if_index);
1577 else if (unformat (line_input, "output-feature"))
1578 is_output_feature = 1;
1579 else if (unformat (line_input, "del"))
1583 error = clib_error_return (0, "unknown input '%U'",
1584 format_unformat_error, line_input);
1589 if (vec_len (inside_sw_if_indices))
1591 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1593 sw_if_index = inside_sw_if_indices[i];
1594 if (is_output_feature)
1596 if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1598 error = clib_error_return (0, "%s %U failed",
1599 is_del ? "del" : "add",
1600 format_vnet_sw_interface_name, vnm,
1601 vnet_get_sw_interface (vnm,
1608 if (snat_interface_add_del (sw_if_index, 1, is_del))
1610 error = clib_error_return (0, "%s %U failed",
1611 is_del ? "del" : "add",
1612 format_vnet_sw_interface_name, vnm,
1613 vnet_get_sw_interface (vnm,
1621 if (vec_len (outside_sw_if_indices))
1623 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1625 sw_if_index = outside_sw_if_indices[i];
1626 if (is_output_feature)
1628 if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1630 error = clib_error_return (0, "%s %U failed",
1631 is_del ? "del" : "add",
1632 format_vnet_sw_interface_name, vnm,
1633 vnet_get_sw_interface (vnm,
1640 if (snat_interface_add_del (sw_if_index, 0, is_del))
1642 error = clib_error_return (0, "%s %U failed",
1643 is_del ? "del" : "add",
1644 format_vnet_sw_interface_name, vnm,
1645 vnet_get_sw_interface (vnm,
1654 unformat_free (line_input);
1655 vec_free (inside_sw_if_indices);
1656 vec_free (outside_sw_if_indices);
1661 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1662 .path = "set interface nat44",
1663 .function = snat_feature_command_fn,
1664 .short_help = "set interface nat44 in <intfc> out <intfc> [output-feature] "
1669 unformat_snat_protocol (unformat_input_t * input, va_list * args)
1671 u32 *r = va_arg (*args, u32 *);
1674 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1675 foreach_snat_protocol
1683 format_snat_protocol (u8 * s, va_list * args)
1685 u32 i = va_arg (*args, u32);
1690 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1691 foreach_snat_protocol
1694 s = format (s, "unknown");
1697 s = format (s, "%s", t);
1701 static clib_error_t *
1702 add_static_mapping_command_fn (vlib_main_t * vm,
1703 unformat_input_t * input,
1704 vlib_cli_command_t * cmd)
1706 unformat_input_t _line_input, *line_input = &_line_input;
1707 clib_error_t * error = 0;
1708 ip4_address_t l_addr, e_addr;
1709 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1712 u32 sw_if_index = ~0;
1713 vnet_main_t * vnm = vnet_get_main();
1715 snat_protocol_t proto;
1718 /* Get a line of input. */
1719 if (!unformat_user (input, unformat_line_input, line_input))
1722 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1724 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1727 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1729 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1732 else if (unformat (line_input, "external %U", unformat_ip4_address,
1735 else if (unformat (line_input, "external %U %u",
1736 unformat_vnet_sw_interface, vnm, &sw_if_index,
1740 else if (unformat (line_input, "external %U",
1741 unformat_vnet_sw_interface, vnm, &sw_if_index))
1743 else if (unformat (line_input, "vrf %u", &vrf_id))
1745 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1747 else if (unformat (line_input, "del"))
1751 error = clib_error_return (0, "unknown input: '%U'",
1752 format_unformat_error, line_input);
1757 if (!addr_only && !proto_set)
1759 error = clib_error_return (0, "missing protocol");
1763 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1764 vrf_id, addr_only, sw_if_index, proto, is_add);
1768 case VNET_API_ERROR_INVALID_VALUE:
1769 error = clib_error_return (0, "External port already in use.");
1771 case VNET_API_ERROR_NO_SUCH_ENTRY:
1773 error = clib_error_return (0, "External addres must be allocated.");
1775 error = clib_error_return (0, "Mapping not exist.");
1777 case VNET_API_ERROR_NO_SUCH_FIB:
1778 error = clib_error_return (0, "No such VRF id.");
1780 case VNET_API_ERROR_VALUE_EXIST:
1781 error = clib_error_return (0, "Mapping already exist.");
1788 unformat_free (line_input);
1795 * @cliexstart{snat add static mapping}
1796 * Static mapping allows hosts on the external network to initiate connection
1797 * to to the local network host.
1798 * To create static mapping between local host address 10.0.0.3 port 6303 and
1799 * external address 4.4.4.4 port 3606 for TCP protocol use:
1800 * vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
1801 * If not runnig "static mapping only" NAT plugin mode use before:
1802 * vpp# nat44 add address 4.4.4.4
1803 * To create static mapping between local and external address use:
1804 * vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
1807 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1808 .path = "nat44 add static mapping",
1809 .function = add_static_mapping_command_fn,
1811 "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1814 static clib_error_t *
1815 add_lb_static_mapping_command_fn (vlib_main_t * vm,
1816 unformat_input_t * input,
1817 vlib_cli_command_t * cmd)
1819 unformat_input_t _line_input, *line_input = &_line_input;
1820 clib_error_t * error = 0;
1821 ip4_address_t l_addr, e_addr;
1822 u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
1825 snat_protocol_t proto;
1827 nat44_lb_addr_port_t *locals = 0, local;
1829 /* Get a line of input. */
1830 if (!unformat_user (input, unformat_line_input, line_input))
1833 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1835 if (unformat (line_input, "local %U:%u probability %u",
1836 unformat_ip4_address, &l_addr, &l_port, &probability))
1838 memset (&local, 0, sizeof (local));
1839 local.addr = l_addr;
1840 local.port = (u16) l_port;
1841 local.probability = (u8) probability;
1842 vec_add1 (locals, local);
1844 else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
1847 else if (unformat (line_input, "vrf %u", &vrf_id))
1849 else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
1852 else if (unformat (line_input, "del"))
1856 error = clib_error_return (0, "unknown input: '%U'",
1857 format_unformat_error, line_input);
1862 if (vec_len (locals) < 2)
1864 error = clib_error_return (0, "at least two local must be set");
1870 error = clib_error_return (0, "missing protocol");
1874 rv = nat44_add_del_lb_static_mapping (e_addr, (u16) e_port, proto, vrf_id,
1879 case VNET_API_ERROR_INVALID_VALUE:
1880 error = clib_error_return (0, "External port already in use.");
1882 case VNET_API_ERROR_NO_SUCH_ENTRY:
1884 error = clib_error_return (0, "External addres must be allocated.");
1886 error = clib_error_return (0, "Mapping not exist.");
1888 case VNET_API_ERROR_VALUE_EXIST:
1889 error = clib_error_return (0, "Mapping already exist.");
1896 unformat_free (line_input);
1902 VLIB_CLI_COMMAND (add_lb_static_mapping_command, static) = {
1903 .path = "nat44 add load-balancing static mapping",
1904 .function = add_lb_static_mapping_command_fn,
1906 "nat44 add load-balancing static mapping protocol tcp|udp external <addr>:<port> local <addr>:<port> probability <n> [vrf <table-id>] [del]",
1909 static clib_error_t *
1910 set_workers_command_fn (vlib_main_t * vm,
1911 unformat_input_t * input,
1912 vlib_cli_command_t * cmd)
1914 unformat_input_t _line_input, *line_input = &_line_input;
1917 clib_error_t *error = 0;
1919 /* Get a line of input. */
1920 if (!unformat_user (input, unformat_line_input, line_input))
1923 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1925 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1929 error = clib_error_return (0, "unknown input '%U'",
1930 format_unformat_error, line_input);
1937 error = clib_error_return (0, "List of workers must be specified.");
1941 rv = snat_set_workers(bitmap);
1943 clib_bitmap_free (bitmap);
1947 case VNET_API_ERROR_INVALID_WORKER:
1948 error = clib_error_return (0, "Invalid worker(s).");
1950 case VNET_API_ERROR_FEATURE_DISABLED:
1951 error = clib_error_return (0,
1952 "Supported only if 2 or more workes available.");
1959 unformat_free (line_input);
1966 * @cliexstart{set snat workers}
1967 * Set NAT workers if 2 or more workers available, use:
1968 * vpp# set snat workers 0-2,5
1971 VLIB_CLI_COMMAND (set_workers_command, static) = {
1972 .path = "set nat workers",
1973 .function = set_workers_command_fn,
1975 "set nat workers <workers-list>",
1978 static clib_error_t *
1979 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1980 unformat_input_t * input,
1981 vlib_cli_command_t * cmd)
1983 unformat_input_t _line_input, *line_input = &_line_input;
1988 clib_error_t *error = 0;
1990 /* Get a line of input. */
1991 if (!unformat_user (input, unformat_line_input, line_input))
1994 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1996 if (unformat (line_input, "domain %d", &domain_id))
1998 else if (unformat (line_input, "src-port %d", &src_port))
2000 else if (unformat (line_input, "disable"))
2004 error = clib_error_return (0, "unknown input '%U'",
2005 format_unformat_error, line_input);
2010 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
2014 error = clib_error_return (0, "ipfix logging enable failed");
2019 unformat_free (line_input);
2026 * @cliexstart{snat ipfix logging}
2027 * To enable NAT IPFIX logging use:
2028 * vpp# nat ipfix logging
2029 * To set IPFIX exporter use:
2030 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
2033 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
2034 .path = "nat ipfix logging",
2035 .function = snat_ipfix_logging_enable_disable_command_fn,
2036 .short_help = "nat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
2040 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2042 snat_main_t *sm = &snat_main;
2043 snat_user_key_t key0;
2044 clib_bihash_kv_8_8_t kv0, value0;
2045 u32 next_worker_index = 0;
2047 key0.addr = ip0->src_address;
2048 key0.fib_index = rx_fib_index0;
2050 kv0.key = key0.as_u64;
2052 /* Ever heard of of the "user" before? */
2053 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
2055 /* No, assign next available worker (RR) */
2056 next_worker_index = sm->first_worker_index;
2057 if (vec_len (sm->workers))
2059 next_worker_index +=
2060 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
2063 /* add non-traslated packets worker lookup */
2064 kv0.value = next_worker_index;
2065 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
2068 next_worker_index = value0.value;
2070 return next_worker_index;
2074 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
2076 snat_main_t *sm = &snat_main;
2077 snat_worker_key_t key0;
2078 clib_bihash_kv_8_8_t kv0, value0;
2079 udp_header_t * udp0;
2080 u32 next_worker_index = 0;
2082 udp0 = ip4_next_header (ip0);
2084 key0.addr = ip0->dst_address;
2085 key0.port = udp0->dst_port;
2086 key0.fib_index = rx_fib_index0;
2088 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
2090 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
2091 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
2092 key0.port = echo0->identifier;
2095 kv0.key = key0.as_u64;
2097 /* Ever heard of of the "user" before? */
2098 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
2101 kv0.key = key0.as_u64;
2103 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
2105 /* No, assign next available worker (RR) */
2106 next_worker_index = sm->first_worker_index;
2107 if (vec_len (sm->workers))
2109 next_worker_index +=
2110 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
2115 /* Static mapping without port */
2116 next_worker_index = value0.value;
2119 /* Add to translated packets worker lookup */
2120 key0.port = udp0->dst_port;
2121 kv0.key = key0.as_u64;
2122 kv0.value = next_worker_index;
2123 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
2126 next_worker_index = value0.value;
2128 return next_worker_index;
2131 static clib_error_t *
2132 snat_config (vlib_main_t * vm, unformat_input_t * input)
2134 snat_main_t * sm = &snat_main;
2135 u32 translation_buckets = 1024;
2136 u32 translation_memory_size = 128<<20;
2137 u32 user_buckets = 128;
2138 u32 user_memory_size = 64<<20;
2139 u32 max_translations_per_user = 100;
2140 u32 outside_vrf_id = 0;
2141 u32 inside_vrf_id = 0;
2142 u32 static_mapping_buckets = 1024;
2143 u32 static_mapping_memory_size = 64<<20;
2144 u8 static_mapping_only = 0;
2145 u8 static_mapping_connection_tracking = 0;
2146 snat_main_per_thread_data_t *tsm;
2148 sm->deterministic = 0;
2150 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2152 if (unformat (input, "translation hash buckets %d", &translation_buckets))
2154 else if (unformat (input, "translation hash memory %d",
2155 &translation_memory_size));
2156 else if (unformat (input, "user hash buckets %d", &user_buckets))
2158 else if (unformat (input, "user hash memory %d",
2161 else if (unformat (input, "max translations per user %d",
2162 &max_translations_per_user))
2164 else if (unformat (input, "outside VRF id %d",
2167 else if (unformat (input, "inside VRF id %d",
2170 else if (unformat (input, "static mapping only"))
2172 static_mapping_only = 1;
2173 if (unformat (input, "connection tracking"))
2174 static_mapping_connection_tracking = 1;
2176 else if (unformat (input, "deterministic"))
2177 sm->deterministic = 1;
2179 return clib_error_return (0, "unknown input '%U'",
2180 format_unformat_error, input);
2183 /* for show commands, etc. */
2184 sm->translation_buckets = translation_buckets;
2185 sm->translation_memory_size = translation_memory_size;
2186 sm->user_buckets = user_buckets;
2187 sm->user_memory_size = user_memory_size;
2188 sm->max_translations_per_user = max_translations_per_user;
2189 sm->outside_vrf_id = outside_vrf_id;
2190 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2192 FIB_SOURCE_PLUGIN_HI);
2193 sm->inside_vrf_id = inside_vrf_id;
2194 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
2196 FIB_SOURCE_PLUGIN_HI);
2197 sm->static_mapping_only = static_mapping_only;
2198 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
2200 if (sm->deterministic)
2202 sm->in2out_node_index = snat_det_in2out_node.index;
2203 sm->in2out_output_node_index = ~0;
2204 sm->out2in_node_index = snat_det_out2in_node.index;
2205 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
2206 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
2210 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
2211 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
2212 sm->in2out_node_index = snat_in2out_node.index;
2213 sm->in2out_output_node_index = snat_in2out_output_node.index;
2214 sm->out2in_node_index = snat_out2in_node.index;
2215 if (!static_mapping_only ||
2216 (static_mapping_only && static_mapping_connection_tracking))
2218 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
2219 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
2221 vec_foreach (tsm, sm->per_thread_data)
2223 clib_bihash_init_8_8 (&tsm->in2out, "in2out", translation_buckets,
2224 translation_memory_size);
2226 clib_bihash_init_8_8 (&tsm->out2in, "out2in", translation_buckets,
2227 translation_memory_size);
2229 clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets,
2233 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
2236 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out",
2237 translation_buckets, translation_memory_size);
2239 clib_bihash_init_16_8 (&sm->in2out_ed, "in2out-ed",
2240 translation_buckets, translation_memory_size);
2242 clib_bihash_init_16_8 (&sm->out2in_ed, "out2in-ed",
2243 translation_buckets, translation_memory_size);
2247 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
2248 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
2250 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
2251 "static_mapping_by_local", static_mapping_buckets,
2252 static_mapping_memory_size);
2254 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
2255 "static_mapping_by_external", static_mapping_buckets,
2256 static_mapping_memory_size);
2262 VLIB_CONFIG_FUNCTION (snat_config, "nat");
2264 u8 * format_snat_session_state (u8 * s, va_list * args)
2266 u32 i = va_arg (*args, u32);
2271 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
2272 foreach_snat_session_state
2275 t = format (t, "unknown");
2277 s = format (s, "%s", t);
2281 u8 * format_snat_key (u8 * s, va_list * args)
2283 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
2285 s = format (s, "%U proto %U port %d fib %d",
2286 format_ip4_address, &key->addr,
2287 format_snat_protocol, key->protocol,
2288 clib_net_to_host_u16 (key->port), key->fib_index);
2292 u8 * format_snat_session (u8 * s, va_list * args)
2294 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
2295 snat_session_t * sess = va_arg (*args, snat_session_t *);
2297 if (snat_is_unk_proto_session (sess))
2299 s = format (s, " i2o %U proto %u fib %u\n",
2300 format_ip4_address, &sess->in2out.addr, sess->in2out.port,
2301 sess->in2out.fib_index);
2302 s = format (s, " o2i %U proto %u fib %u\n",
2303 format_ip4_address, &sess->out2in.addr, sess->out2in.port,
2304 sess->out2in.fib_index);
2308 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
2309 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
2311 if (sess->ext_host_addr.as_u32)
2312 s = format (s, " external host %U\n",
2313 format_ip4_address, &sess->ext_host_addr);
2314 s = format (s, " last heard %.2f\n", sess->last_heard);
2315 s = format (s, " total pkts %d, total bytes %lld\n",
2316 sess->total_pkts, sess->total_bytes);
2317 if (snat_is_session_static (sess))
2318 s = format (s, " static translation\n");
2320 s = format (s, " dynamic translation\n");
2321 if (sess->flags & SNAT_SESSION_FLAG_LOAD_BALANCING)
2322 s = format (s, " load-balancing\n");
2327 u8 * format_snat_user (u8 * s, va_list * args)
2329 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
2330 snat_user_t * u = va_arg (*args, snat_user_t *);
2331 int verbose = va_arg (*args, int);
2332 dlist_elt_t * head, * elt;
2333 u32 elt_index, head_index;
2335 snat_session_t * sess;
2337 s = format (s, "%U: %d dynamic translations, %d static translations\n",
2338 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
2343 if (u->nsessions || u->nstaticsessions)
2345 head_index = u->sessions_per_user_list_head_index;
2346 head = pool_elt_at_index (sm->list_pool, head_index);
2348 elt_index = head->next;
2349 elt = pool_elt_at_index (sm->list_pool, elt_index);
2350 session_index = elt->value;
2352 while (session_index != ~0)
2354 sess = pool_elt_at_index (sm->sessions, session_index);
2356 s = format (s, " %U\n", format_snat_session, sm, sess);
2358 elt_index = elt->next;
2359 elt = pool_elt_at_index (sm->list_pool, elt_index);
2360 session_index = elt->value;
2367 u8 * format_snat_static_mapping (u8 * s, va_list * args)
2369 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
2370 nat44_lb_addr_port_t *local;
2373 s = format (s, "local %U external %U vrf %d",
2374 format_ip4_address, &m->local_addr,
2375 format_ip4_address, &m->external_addr,
2379 if (vec_len (m->locals))
2381 s = format (s, "%U vrf %d external %U:%d",
2382 format_snat_protocol, m->proto,
2384 format_ip4_address, &m->external_addr, m->external_port);
2385 vec_foreach (local, m->locals)
2386 s = format (s, "\n local %U:%d probability %d\%",
2387 format_ip4_address, &local->addr, local->port,
2388 local->probability);
2391 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2392 format_snat_protocol, m->proto,
2393 format_ip4_address, &m->local_addr, m->local_port,
2394 format_ip4_address, &m->external_addr, m->external_port,
2400 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
2402 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
2403 vnet_main_t *vnm = vnet_get_main();
2406 s = format (s, "local %U external %U vrf %d",
2407 format_ip4_address, &m->l_addr,
2408 format_vnet_sw_interface_name, vnm,
2409 vnet_get_sw_interface (vnm, m->sw_if_index),
2412 s = format (s, "%U local %U:%d external %U:%d vrf %d",
2413 format_snat_protocol, m->proto,
2414 format_ip4_address, &m->l_addr, m->l_port,
2415 format_vnet_sw_interface_name, vnm,
2416 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
2422 u8 * format_det_map_ses (u8 * s, va_list * args)
2424 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
2425 ip4_address_t in_addr, out_addr;
2426 u32 in_offset, out_offset;
2427 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
2428 u32 * i = va_arg (*args, u32 *);
2430 u32 user_index = *i / SNAT_DET_SES_PER_USER;
2431 in_addr.as_u32 = clib_host_to_net_u32 (
2432 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
2433 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
2434 clib_net_to_host_u32(det_map->in_addr.as_u32);
2435 out_offset = in_offset / det_map->sharing_ratio;
2436 out_addr.as_u32 = clib_host_to_net_u32(
2437 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
2438 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
2439 format_ip4_address, &in_addr,
2440 clib_net_to_host_u16 (ses->in_port),
2441 format_ip4_address, &out_addr,
2442 clib_net_to_host_u16 (ses->out.out_port),
2443 format_ip4_address, &ses->out.ext_host_addr,
2444 clib_net_to_host_u16 (ses->out.ext_host_port),
2445 format_snat_session_state, ses->state,
2451 static clib_error_t *
2452 show_snat_command_fn (vlib_main_t * vm,
2453 unformat_input_t * input,
2454 vlib_cli_command_t * cmd)
2457 snat_main_t * sm = &snat_main;
2459 snat_static_mapping_t *m;
2460 snat_interface_t *i;
2461 snat_address_t * ap;
2462 vnet_main_t *vnm = vnet_get_main();
2463 snat_main_per_thread_data_t *tsm;
2464 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2466 snat_static_map_resolve_t *rp;
2467 snat_det_map_t * dm;
2468 snat_det_session_t * ses;
2470 if (unformat (input, "detail"))
2472 else if (unformat (input, "verbose"))
2475 if (sm->static_mapping_only)
2477 if (sm->static_mapping_connection_tracking)
2478 vlib_cli_output (vm, "NAT plugin mode: static mapping only connection "
2481 vlib_cli_output (vm, "NAT plugin mode: static mapping only");
2483 else if (sm->deterministic)
2485 vlib_cli_output (vm, "NAT plugin mode: deterministic mapping");
2489 vlib_cli_output (vm, "NAT plugin mode: dynamic translations enabled");
2494 pool_foreach (i, sm->interfaces,
2496 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2497 vnet_get_sw_interface (vnm, i->sw_if_index),
2498 i->is_inside ? "in" : "out");
2501 pool_foreach (i, sm->output_feature_interfaces,
2503 vlib_cli_output (vm, "%U output-feature %s",
2504 format_vnet_sw_interface_name, vnm,
2505 vnet_get_sw_interface (vnm, i->sw_if_index),
2506 i->is_inside ? "in" : "out");
2509 if (vec_len (sm->auto_add_sw_if_indices))
2511 vlib_cli_output (vm, "NAT44 pool addresses interfaces:");
2512 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2514 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2515 vnet_get_sw_interface (vnm, *sw_if_index));
2519 vec_foreach (ap, sm->addresses)
2521 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2522 if (ap->fib_index != ~0)
2523 vlib_cli_output (vm, " tenant VRF: %u",
2524 ip4_fib_get(ap->fib_index)->table_id);
2526 vlib_cli_output (vm, " tenant VRF independent");
2527 #define _(N, i, n, s) \
2528 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2529 foreach_snat_protocol
2534 if (sm->num_workers > 1)
2536 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2539 vec_foreach (worker, sm->workers)
2541 vlib_worker_thread_t *w =
2542 vlib_worker_threads + *worker + sm->first_worker_index;
2543 vlib_cli_output (vm, " %s", w->name);
2548 if (sm->deterministic)
2550 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2551 vlib_cli_output (vm, "tcp-established timeout: %dsec",
2552 sm->tcp_established_timeout);
2553 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2554 sm->tcp_transitory_timeout);
2555 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2556 vlib_cli_output (vm, "%d deterministic mappings",
2557 pool_elts (sm->det_maps));
2560 pool_foreach (dm, sm->det_maps,
2562 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2563 format_ip4_address, &dm->in_addr, dm->in_plen,
2564 format_ip4_address, &dm->out_addr, dm->out_plen);
2565 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2567 vlib_cli_output (vm, " number of ports per inside host: %d\n",
2568 dm->ports_per_host);
2569 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2572 vec_foreach_index (j, dm->sessions)
2574 ses = vec_elt_at_index (dm->sessions, j);
2576 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
2585 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2587 vlib_cli_output (vm, "%d static mappings",
2588 pool_elts (sm->static_mappings));
2592 pool_foreach (m, sm->static_mappings,
2594 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2600 vec_foreach (tsm, sm->per_thread_data)
2602 users_num += pool_elts (tsm->users);
2603 sessions_num += pool_elts (tsm->sessions);
2606 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2607 " %d static mappings",
2609 vec_len (sm->addresses),
2611 pool_elts (sm->static_mappings));
2615 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->in2out_ed,
2617 vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed,
2619 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2621 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2623 vec_foreach_index (j, sm->per_thread_data)
2625 tsm = vec_elt_at_index (sm->per_thread_data, j);
2627 if (pool_elts (tsm->users) == 0)
2630 vlib_worker_thread_t *w = vlib_worker_threads + j;
2631 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2633 vlib_cli_output (vm, " %U", format_bihash_8_8, &tsm->in2out,
2635 vlib_cli_output (vm, " %U", format_bihash_8_8, &tsm->out2in,
2637 vlib_cli_output (vm, " %d list pool elements",
2638 pool_elts (tsm->list_pool));
2640 pool_foreach (u, tsm->users,
2642 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2647 if (pool_elts (sm->static_mappings))
2649 vlib_cli_output (vm, "static mappings:");
2650 pool_foreach (m, sm->static_mappings,
2652 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2654 for (j = 0; j < vec_len (sm->to_resolve); j++)
2656 rp = sm->to_resolve + j;
2657 vlib_cli_output (vm, "%U",
2658 format_snat_static_map_to_resolve, rp);
2667 VLIB_CLI_COMMAND (show_snat_command, static) = {
2668 .path = "show nat44",
2669 .short_help = "show nat44",
2670 .function = show_snat_command_fn,
2675 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2678 ip4_address_t * address,
2680 u32 if_address_index,
2683 snat_main_t *sm = &snat_main;
2684 snat_static_map_resolve_t *rp;
2685 u32 *indices_to_delete = 0;
2689 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2691 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2695 /* Don't trip over lease renewal, static config */
2696 for (j = 0; j < vec_len(sm->addresses); j++)
2697 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2700 snat_add_address (sm, address, ~0);
2701 /* Scan static map resolution vector */
2702 for (j = 0; j < vec_len (sm->to_resolve); j++)
2704 rp = sm->to_resolve + j;
2705 /* On this interface? */
2706 if (rp->sw_if_index == sw_if_index)
2708 /* Add the static mapping */
2709 rv = snat_add_static_mapping (rp->l_addr,
2715 ~0 /* sw_if_index */,
2719 clib_warning ("snat_add_static_mapping returned %d",
2721 vec_add1 (indices_to_delete, j);
2724 /* If we resolved any of the outstanding static mappings */
2725 if (vec_len(indices_to_delete))
2728 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2729 vec_delete(sm->to_resolve, 1, j);
2730 vec_free(indices_to_delete);
2736 (void) snat_del_address(sm, address[0], 1);
2744 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2746 ip4_main_t * ip4_main = sm->ip4_main;
2747 ip4_address_t * first_int_addr;
2748 snat_static_map_resolve_t *rp;
2749 u32 *indices_to_delete = 0;
2752 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2753 0 /* just want the address*/);
2755 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2757 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2761 /* if have address remove it */
2763 (void) snat_del_address (sm, first_int_addr[0], 1);
2766 for (j = 0; j < vec_len (sm->to_resolve); j++)
2768 rp = sm->to_resolve + j;
2769 if (rp->sw_if_index == sw_if_index)
2770 vec_add1 (indices_to_delete, j);
2772 if (vec_len(indices_to_delete))
2774 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2775 vec_del1(sm->to_resolve, j);
2776 vec_free(indices_to_delete);
2779 vec_del1(sm->auto_add_sw_if_indices, i);
2782 return VNET_API_ERROR_VALUE_EXIST;
2789 return VNET_API_ERROR_NO_SUCH_ENTRY;
2791 /* add to the auto-address list */
2792 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2794 /* If the address is already bound - or static - add it now */
2796 snat_add_address (sm, first_int_addr, ~0);
2801 static clib_error_t *
2802 snat_add_interface_address_command_fn (vlib_main_t * vm,
2803 unformat_input_t * input,
2804 vlib_cli_command_t * cmd)
2806 snat_main_t *sm = &snat_main;
2807 unformat_input_t _line_input, *line_input = &_line_input;
2811 clib_error_t *error = 0;
2813 /* Get a line of input. */
2814 if (!unformat_user (input, unformat_line_input, line_input))
2817 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2819 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2820 sm->vnet_main, &sw_if_index))
2822 else if (unformat (line_input, "del"))
2826 error = clib_error_return (0, "unknown input '%U'",
2827 format_unformat_error, line_input);
2832 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2840 error = clib_error_return (0, "snat_add_interface_address returned %d",
2846 unformat_free (line_input);
2851 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2852 .path = "nat44 add interface address",
2853 .short_help = "nat44 add interface address <interface> [del]",
2854 .function = snat_add_interface_address_command_fn,
2857 static clib_error_t *
2858 snat_det_map_command_fn (vlib_main_t * vm,
2859 unformat_input_t * input,
2860 vlib_cli_command_t * cmd)
2862 snat_main_t *sm = &snat_main;
2863 unformat_input_t _line_input, *line_input = &_line_input;
2864 ip4_address_t in_addr, out_addr;
2865 u32 in_plen, out_plen;
2867 clib_error_t *error = 0;
2869 /* Get a line of input. */
2870 if (!unformat_user (input, unformat_line_input, line_input))
2873 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2875 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2877 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2879 else if (unformat (line_input, "del"))
2883 error = clib_error_return (0, "unknown input '%U'",
2884 format_unformat_error, line_input);
2889 unformat_free (line_input);
2891 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2896 error = clib_error_return (0, "snat_det_add_map return %d", rv);
2901 unformat_free (line_input);
2908 * @cliexstart{snat deterministic add}
2909 * Create bijective mapping of inside address to outside address and port range
2910 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2912 * To create deterministic mapping between inside network 10.0.0.0/18 and
2913 * outside network 1.1.1.0/30 use:
2914 * # vpp# nat44 deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2917 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2918 .path = "nat44 deterministic add",
2919 .short_help = "nat44 deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2920 .function = snat_det_map_command_fn,
2923 static clib_error_t *
2924 snat_det_forward_command_fn (vlib_main_t * vm,
2925 unformat_input_t * input,
2926 vlib_cli_command_t * cmd)
2928 snat_main_t *sm = &snat_main;
2929 unformat_input_t _line_input, *line_input = &_line_input;
2930 ip4_address_t in_addr, out_addr;
2932 snat_det_map_t * dm;
2933 clib_error_t *error = 0;
2935 /* Get a line of input. */
2936 if (!unformat_user (input, unformat_line_input, line_input))
2939 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2941 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2945 error = clib_error_return (0, "unknown input '%U'",
2946 format_unformat_error, line_input);
2951 unformat_free (line_input);
2953 dm = snat_det_map_by_user(sm, &in_addr);
2955 vlib_cli_output (vm, "no match");
2958 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2959 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2960 lo_port, lo_port + dm->ports_per_host - 1);
2964 unformat_free (line_input);
2971 * @cliexstart{snat deterministic forward}
2972 * Return outside address and port range from inside address for deterministic
2974 * To obtain outside address and port of inside host use:
2975 * vpp# nat44 deterministic forward 10.0.0.2
2976 * 1.1.1.0:<1054-1068>
2979 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2980 .path = "nat44 deterministic forward",
2981 .short_help = "nat44 deterministic forward <addr>",
2982 .function = snat_det_forward_command_fn,
2985 static clib_error_t *
2986 snat_det_reverse_command_fn (vlib_main_t * vm,
2987 unformat_input_t * input,
2988 vlib_cli_command_t * cmd)
2990 snat_main_t *sm = &snat_main;
2991 unformat_input_t _line_input, *line_input = &_line_input;
2992 ip4_address_t in_addr, out_addr;
2994 snat_det_map_t * dm;
2995 clib_error_t *error = 0;
2997 /* Get a line of input. */
2998 if (!unformat_user (input, unformat_line_input, line_input))
3001 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3003 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
3007 error = clib_error_return (0, "unknown input '%U'",
3008 format_unformat_error, line_input);
3012 unformat_free (line_input);
3014 if (out_port < 1024 || out_port > 65535)
3016 error = clib_error_return (0, "wrong port, must be <1024-65535>");
3020 dm = snat_det_map_by_out(sm, &out_addr);
3022 vlib_cli_output (vm, "no match");
3025 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
3026 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
3030 unformat_free (line_input);
3037 * @cliexstart{snat deterministic reverse}
3038 * Return inside address from outside address and port for deterministic NAT.
3039 * To obtain inside host address from outside address and port use:
3040 * #vpp nat44 deterministic reverse 1.1.1.1:1276
3044 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
3045 .path = "nat44 deterministic reverse",
3046 .short_help = "nat44 deterministic reverse <addr>:<port>",
3047 .function = snat_det_reverse_command_fn,
3050 static clib_error_t *
3051 set_timeout_command_fn (vlib_main_t * vm,
3052 unformat_input_t * input,
3053 vlib_cli_command_t * cmd)
3055 snat_main_t *sm = &snat_main;
3056 unformat_input_t _line_input, *line_input = &_line_input;
3057 clib_error_t *error = 0;
3059 /* Get a line of input. */
3060 if (!unformat_user (input, unformat_line_input, line_input))
3063 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3065 if (unformat (line_input, "udp %u", &sm->udp_timeout))
3067 else if (unformat (line_input, "tcp-established %u",
3068 &sm->tcp_established_timeout))
3070 else if (unformat (line_input, "tcp-transitory %u",
3071 &sm->tcp_transitory_timeout))
3073 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
3075 else if (unformat (line_input, "reset"))
3077 sm->udp_timeout = SNAT_UDP_TIMEOUT;
3078 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
3079 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
3080 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
3084 error = clib_error_return (0, "unknown input '%U'",
3085 format_unformat_error, line_input);
3090 unformat_free (line_input);
3093 unformat_free (line_input);
3100 * @cliexstart{set snat deterministic timeout}
3101 * Set values of timeouts for deterministic NAT (in seconds), use:
3102 * vpp# set nat44 deterministic timeout udp 120 tcp-established 7500
3103 * tcp-transitory 250 icmp 90
3104 * To reset default values use:
3105 * vpp# set nat44 deterministic timeout reset
3108 VLIB_CLI_COMMAND (set_timeout_command, static) = {
3109 .path = "set nat44 deterministic timeout",
3110 .function = set_timeout_command_fn,
3112 "set nat44 deterministic timeout [udp <sec> | tcp-established <sec> "
3113 "tcp-transitory <sec> | icmp <sec> | reset]",
3116 static clib_error_t *
3117 snat_det_close_session_out_fn (vlib_main_t *vm,
3118 unformat_input_t * input,
3119 vlib_cli_command_t * cmd)
3121 snat_main_t *sm = &snat_main;
3122 unformat_input_t _line_input, *line_input = &_line_input;
3123 ip4_address_t out_addr, ext_addr, in_addr;
3124 u16 out_port, ext_port;
3125 snat_det_map_t * dm;
3126 snat_det_session_t * ses;
3127 snat_det_out_key_t key;
3128 clib_error_t *error = 0;
3130 /* Get a line of input. */
3131 if (!unformat_user (input, unformat_line_input, line_input))
3134 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3136 if (unformat (line_input, "%U:%d %U:%d",
3137 unformat_ip4_address, &out_addr, &out_port,
3138 unformat_ip4_address, &ext_addr, &ext_port))
3142 error = clib_error_return (0, "unknown input '%U'",
3143 format_unformat_error, line_input);
3148 unformat_free (line_input);
3150 dm = snat_det_map_by_out(sm, &out_addr);
3152 vlib_cli_output (vm, "no match");
3155 snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
3156 key.ext_host_addr = out_addr;
3157 key.ext_host_port = ntohs(ext_port);
3158 key.out_port = ntohs(out_port);
3159 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
3161 vlib_cli_output (vm, "no match");
3163 snat_det_ses_close(dm, ses);
3167 unformat_free (line_input);
3174 * @cliexstart{snat deterministic close session out}
3175 * Close session using outside ip address and port
3176 * and external ip address and port, use:
3177 * vpp# nat44 deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
3180 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
3181 .path = "nat44 deterministic close session out",
3182 .short_help = "nat44 deterministic close session out "
3183 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
3184 .function = snat_det_close_session_out_fn,
3187 static clib_error_t *
3188 snat_det_close_session_in_fn (vlib_main_t *vm,
3189 unformat_input_t * input,
3190 vlib_cli_command_t * cmd)
3192 snat_main_t *sm = &snat_main;
3193 unformat_input_t _line_input, *line_input = &_line_input;
3194 ip4_address_t in_addr, ext_addr;
3195 u16 in_port, ext_port;
3196 snat_det_map_t * dm;
3197 snat_det_session_t * ses;
3198 snat_det_out_key_t key;
3199 clib_error_t *error = 0;
3201 /* Get a line of input. */
3202 if (!unformat_user (input, unformat_line_input, line_input))
3205 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3207 if (unformat (line_input, "%U:%d %U:%d",
3208 unformat_ip4_address, &in_addr, &in_port,
3209 unformat_ip4_address, &ext_addr, &ext_port))
3213 error = clib_error_return (0, "unknown input '%U'",
3214 format_unformat_error, line_input);
3219 unformat_free (line_input);
3221 dm = snat_det_map_by_user (sm, &in_addr);
3223 vlib_cli_output (vm, "no match");
3226 key.ext_host_addr = ext_addr;
3227 key.ext_host_port = ntohs (ext_port);
3228 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
3230 vlib_cli_output (vm, "no match");
3232 snat_det_ses_close(dm, ses);
3236 unformat_free(line_input);
3243 * @cliexstart{snat deterministic close_session_in}
3244 * Close session using inside ip address and port
3245 * and external ip address and port, use:
3246 * vpp# nat44 deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
3249 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
3250 .path = "nat44 deterministic close session in",
3251 .short_help = "nat44 deterministic close session in "
3252 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
3253 .function = snat_det_close_session_in_fn,