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>
22 #include <snat/snat.h>
23 #include <snat/snat_ipfix_logging.h>
24 #include <snat/snat_det.h>
25 #include <snat/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 = "snat-in2out",
38 .runs_before = VNET_FEATURES ("snat-out2in"),
40 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
41 .arc_name = "ip4-unicast",
42 .node_name = "snat-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 = "snat-det-in2out",
48 .runs_before = VNET_FEATURES ("snat-det-out2in"),
50 VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = {
51 .arc_name = "ip4-unicast",
52 .node_name = "snat-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 = "snat-in2out-worker-handoff",
58 .runs_before = VNET_FEATURES ("snat-out2in-worker-handoff"),
60 VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = {
61 .arc_name = "ip4-unicast",
62 .node_name = "snat-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 = "snat-in2out-fast",
68 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
70 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
71 .arc_name = "ip4-unicast",
72 .node_name = "snat-out2in-fast",
73 .runs_before = VNET_FEATURES ("ip4-lookup"),
76 /* Hook up output features */
77 VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = {
78 .arc_name = "ip4-output",
79 .node_name = "snat-in2out-output",
80 .runs_before = VNET_FEATURES ("interface-output"),
83 VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = {
84 .arc_name = "ip4-output",
85 .node_name = "snat-in2out-output-worker-handoff",
86 .runs_before = VNET_FEATURES ("interface-output"),
91 VLIB_PLUGIN_REGISTER () = {
92 .version = VPP_BUILD_VER,
93 .description = "Network Address Translation",
98 * @brief Add/del NAT address to FIB.
100 * Add the external NAT address to the FIB as receive entries. This ensures
101 * that VPP will reply to ARP for this address and we don't need to enable
102 * proxy ARP on the outside interface.
104 * @param addr IPv4 address.
105 * @param plen address prefix length
106 * @param sw_if_index Interface.
107 * @param is_add If 0 delete, otherwise add.
110 snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index,
113 fib_prefix_t prefix = {
115 .fp_proto = FIB_PROTOCOL_IP4,
117 .ip4.as_u32 = addr->as_u32,
120 u32 fib_index = ip4_fib_table_get_index_for_sw_if_index(sw_if_index);
123 fib_table_entry_update_one_path(fib_index,
125 FIB_SOURCE_PLUGIN_HI,
126 (FIB_ENTRY_FLAG_CONNECTED |
127 FIB_ENTRY_FLAG_LOCAL |
128 FIB_ENTRY_FLAG_EXCLUSIVE),
135 FIB_ROUTE_PATH_FLAG_NONE);
137 fib_table_entry_delete(fib_index,
139 FIB_SOURCE_PLUGIN_HI);
142 void snat_add_address (snat_main_t *sm, ip4_address_t *addr, u32 vrf_id)
150 /* Check if address already exists */
151 vec_foreach (ap, sm->addresses)
153 if (ap->addr.as_u32 == addr->as_u32)
157 vec_add2 (sm->addresses, ap, 1);
159 ap->fib_index = ip4_fib_index_from_table_id(vrf_id);
160 #define _(N, i, n, s) \
161 clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535);
162 foreach_snat_protocol
165 /* Add external address to FIB */
166 pool_foreach (i, sm->interfaces,
171 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
174 pool_foreach (i, sm->output_feature_interfaces,
179 snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1);
184 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
187 snat_static_mapping_t *m;
188 pool_foreach (m, sm->static_mappings,
190 if (m->external_addr.as_u32 == addr.as_u32)
197 void increment_v4_address (ip4_address_t * a)
201 v = clib_net_to_host_u32(a->as_u32) + 1;
202 a->as_u32 = clib_host_to_net_u32(v);
206 snat_add_static_mapping_when_resolved (snat_main_t * sm,
207 ip4_address_t l_addr,
212 snat_protocol_t proto,
216 snat_static_map_resolve_t *rp;
218 vec_add2 (sm->to_resolve, rp, 1);
219 rp->l_addr.as_u32 = l_addr.as_u32;
221 rp->sw_if_index = sw_if_index;
225 rp->addr_only = addr_only;
230 * @brief Add static mapping.
232 * Create static mapping between local addr+port and external addr+port.
234 * @param l_addr Local IPv4 address.
235 * @param e_addr External IPv4 address.
236 * @param l_port Local port number.
237 * @param e_port External port number.
238 * @param vrf_id VRF ID.
239 * @param addr_only If 0 address port and pair mapping, otherwise address only.
240 * @param sw_if_index External port instead of specific IP address.
241 * @param is_add If 0 delete static mapping, otherwise add.
245 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
246 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
247 u32 sw_if_index, snat_protocol_t proto, int is_add)
249 snat_main_t * sm = &snat_main;
250 snat_static_mapping_t *m;
251 snat_session_key_t m_key;
252 clib_bihash_kv_8_8_t kv, value;
253 snat_address_t *a = 0;
256 snat_interface_t *interface;
259 /* If the external address is a specific interface address */
260 if (sw_if_index != ~0)
262 ip4_address_t * first_int_addr;
264 /* Might be already set... */
265 first_int_addr = ip4_interface_first_address
266 (sm->ip4_main, sw_if_index, 0 /* just want the address*/);
268 /* DHCP resolution required? */
269 if (first_int_addr == 0)
271 snat_add_static_mapping_when_resolved
272 (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto,
277 e_addr.as_u32 = first_int_addr->as_u32;
281 m_key.port = addr_only ? 0 : e_port;
282 m_key.protocol = addr_only ? 0 : proto;
283 m_key.fib_index = sm->outside_fib_index;
284 kv.key = m_key.as_u64;
285 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
288 m = pool_elt_at_index (sm->static_mappings, value.value);
293 return VNET_API_ERROR_VALUE_EXIST;
295 /* Convert VRF id to FIB index */
298 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
300 return VNET_API_ERROR_NO_SUCH_FIB;
303 /* If not specified use inside VRF id from SNAT plugin startup config */
306 fib_index = sm->inside_fib_index;
307 vrf_id = sm->inside_vrf_id;
310 /* Find external address in allocated addresses and reserve port for
311 address and port pair mapping when dynamic translations enabled */
312 if (!addr_only && !(sm->static_mapping_only))
314 for (i = 0; i < vec_len (sm->addresses); i++)
316 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
318 a = sm->addresses + i;
319 /* External port must be unused */
322 #define _(N, j, n, s) \
323 case SNAT_PROTOCOL_##N: \
324 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \
325 return VNET_API_ERROR_INVALID_VALUE; \
326 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \
328 a->busy_##n##_ports++; \
330 foreach_snat_protocol
333 clib_warning("unknown_protocol");
334 return VNET_API_ERROR_INVALID_VALUE_2;
339 /* External address must be allocated */
341 return VNET_API_ERROR_NO_SUCH_ENTRY;
344 pool_get (sm->static_mappings, m);
345 memset (m, 0, sizeof (*m));
346 m->local_addr = l_addr;
347 m->external_addr = e_addr;
348 m->addr_only = addr_only;
350 m->fib_index = fib_index;
353 m->local_port = l_port;
354 m->external_port = e_port;
358 m_key.addr = m->local_addr;
359 m_key.port = m->local_port;
360 m_key.protocol = m->proto;
361 m_key.fib_index = m->fib_index;
362 kv.key = m_key.as_u64;
363 kv.value = m - sm->static_mappings;
364 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
366 m_key.addr = m->external_addr;
367 m_key.port = m->external_port;
368 m_key.fib_index = sm->outside_fib_index;
369 kv.key = m_key.as_u64;
370 kv.value = m - sm->static_mappings;
371 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
376 snat_user_key_t w_key0;
377 snat_worker_key_t w_key1;
379 w_key0.addr = m->local_addr;
380 w_key0.fib_index = m->fib_index;
381 kv.key = w_key0.as_u64;
383 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
385 kv.value = sm->first_worker_index +
386 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
388 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv, 1);
392 kv.value = value.value;
395 w_key1.addr = m->external_addr;
396 w_key1.port = clib_host_to_net_u16 (m->external_port);
397 w_key1.fib_index = sm->outside_fib_index;
398 kv.key = w_key1.as_u64;
399 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv, 1);
405 return VNET_API_ERROR_NO_SUCH_ENTRY;
407 /* Free external address port */
408 if (!addr_only && !(sm->static_mapping_only))
410 for (i = 0; i < vec_len (sm->addresses); i++)
412 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
414 a = sm->addresses + i;
417 #define _(N, j, n, s) \
418 case SNAT_PROTOCOL_##N: \
419 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \
421 a->busy_##n##_ports--; \
423 foreach_snat_protocol
426 clib_warning("unknown_protocol");
427 return VNET_API_ERROR_INVALID_VALUE_2;
434 m_key.addr = m->local_addr;
435 m_key.port = m->local_port;
436 m_key.protocol = m->proto;
437 m_key.fib_index = m->fib_index;
438 kv.key = m_key.as_u64;
439 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
441 m_key.addr = m->external_addr;
442 m_key.port = m->external_port;
443 m_key.fib_index = sm->outside_fib_index;
444 kv.key = m_key.as_u64;
445 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
447 /* Delete session(s) for static mapping if exist */
448 if (!(sm->static_mapping_only) ||
449 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
451 snat_user_key_t u_key;
453 dlist_elt_t * head, * elt;
454 u32 elt_index, head_index, del_elt_index;
458 snat_main_per_thread_data_t *tsm;
460 u_key.addr = m->local_addr;
461 u_key.fib_index = m->fib_index;
462 kv.key = u_key.as_u64;
463 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
465 user_index = value.value;
466 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &kv, &value))
467 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
469 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
470 u = pool_elt_at_index (tsm->users, user_index);
471 if (u->nstaticsessions)
473 head_index = u->sessions_per_user_list_head_index;
474 head = pool_elt_at_index (tsm->list_pool, head_index);
475 elt_index = head->next;
476 elt = pool_elt_at_index (tsm->list_pool, elt_index);
477 ses_index = elt->value;
478 while (ses_index != ~0)
480 s = pool_elt_at_index (tsm->sessions, ses_index);
481 del_elt_index = elt_index;
482 elt_index = elt->next;
483 elt = pool_elt_at_index (tsm->list_pool, elt_index);
484 ses_index = elt->value;
488 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
489 (clib_net_to_host_u16 (s->out2in.port) != e_port))
493 if (snat_is_unk_proto_session (s))
495 clib_bihash_kv_16_8_t up_kv;
496 snat_unk_proto_ses_key_t up_key;
497 up_key.l_addr = s->in2out.addr;
498 up_key.r_addr = s->ext_host_addr;
499 up_key.fib_index = s->in2out.fib_index;
500 up_key.proto = s->in2out.port;
501 up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
502 up_kv.key[0] = up_key.as_u64[0];
503 up_kv.key[1] = up_key.as_u64[1];
504 if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
506 clib_warning ("in2out key del failed");
508 up_key.l_addr = s->out2in.addr;
509 up_key.fib_index = s->out2in.fib_index;
510 up_kv.key[0] = up_key.as_u64[0];
511 up_kv.key[1] = up_key.as_u64[1];
512 if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
514 clib_warning ("out2in key del failed");
519 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
520 s->out2in.addr.as_u32,
524 s->in2out.fib_index);
526 value.key = s->in2out.as_u64;
527 if (clib_bihash_add_del_8_8 (&sm->in2out, &value, 0))
528 clib_warning ("in2out key del failed");
529 value.key = s->out2in.as_u64;
530 if (clib_bihash_add_del_8_8 (&sm->out2in, &value, 0))
531 clib_warning ("out2in key del failed");
533 pool_put (tsm->sessions, s);
535 clib_dlist_remove (tsm->list_pool, del_elt_index);
536 pool_put_index (tsm->list_pool, del_elt_index);
537 u->nstaticsessions--;
544 pool_put (tsm->users, u);
545 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
551 /* Delete static mapping from pool */
552 pool_put (sm->static_mappings, m);
558 /* Add/delete external address to FIB */
559 pool_foreach (interface, sm->interfaces,
561 if (interface->is_inside)
564 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
567 pool_foreach (interface, sm->output_feature_interfaces,
569 if (interface->is_inside)
572 snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add);
579 int snat_del_address (snat_main_t *sm, ip4_address_t addr, u8 delete_sm)
581 snat_address_t *a = 0;
583 u32 *ses_to_be_removed = 0, *ses_index;
584 clib_bihash_kv_8_8_t kv, value;
585 snat_user_key_t user_key;
587 snat_main_per_thread_data_t *tsm;
588 snat_static_mapping_t *m;
589 snat_interface_t *interface;
592 /* Find SNAT address */
593 for (i=0; i < vec_len (sm->addresses); i++)
595 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
597 a = sm->addresses + i;
602 return VNET_API_ERROR_NO_SUCH_ENTRY;
606 pool_foreach (m, sm->static_mappings,
608 if (m->external_addr.as_u32 == addr.as_u32)
609 (void) snat_add_static_mapping (m->local_addr, m->external_addr,
610 m->local_port, m->external_port,
611 m->vrf_id, m->addr_only, ~0,
617 /* Check if address is used in some static mapping */
618 if (is_snat_address_used_in_static_mapping(sm, addr))
620 clib_warning ("address used in static mapping");
621 return VNET_API_ERROR_UNSPECIFIED;
625 /* Delete sessions using address */
626 if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports)
628 vec_foreach (tsm, sm->per_thread_data)
630 pool_foreach (ses, tsm->sessions, ({
631 if (ses->out2in.addr.as_u32 == addr.as_u32)
633 if (snat_is_unk_proto_session (ses))
635 clib_bihash_kv_16_8_t up_kv;
636 snat_unk_proto_ses_key_t up_key;
637 up_key.l_addr = ses->in2out.addr;
638 up_key.r_addr = ses->ext_host_addr;
639 up_key.fib_index = ses->in2out.fib_index;
640 up_key.proto = ses->in2out.port;
641 up_key.rsvd[0] = up_key.rsvd[1] = up_key.rsvd[2] = 0;
642 up_kv.key[0] = up_key.as_u64[0];
643 up_kv.key[1] = up_key.as_u64[1];
644 if (clib_bihash_add_del_16_8 (&sm->in2out_unk_proto,
646 clib_warning ("in2out key del failed");
648 up_key.l_addr = ses->out2in.addr;
649 up_key.fib_index = ses->out2in.fib_index;
650 up_kv.key[0] = up_key.as_u64[0];
651 up_kv.key[1] = up_key.as_u64[1];
652 if (clib_bihash_add_del_16_8 (&sm->out2in_unk_proto,
654 clib_warning ("out2in key del failed");
659 snat_ipfix_logging_nat44_ses_delete(ses->in2out.addr.as_u32,
660 ses->out2in.addr.as_u32,
661 ses->in2out.protocol,
664 ses->in2out.fib_index);
665 kv.key = ses->in2out.as_u64;
666 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
667 kv.key = ses->out2in.as_u64;
668 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
670 vec_add1 (ses_to_be_removed, ses - tsm->sessions);
671 clib_dlist_remove (tsm->list_pool, ses->per_user_index);
672 user_key.addr = ses->in2out.addr;
673 user_key.fib_index = ses->in2out.fib_index;
674 kv.key = user_key.as_u64;
675 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
677 u = pool_elt_at_index (tsm->users, value.value);
683 vec_foreach (ses_index, ses_to_be_removed)
684 pool_put_index (tsm->sessions, ses_index[0]);
686 vec_free (ses_to_be_removed);
690 vec_del1 (sm->addresses, i);
692 /* Delete external address from FIB */
693 pool_foreach (interface, sm->interfaces,
695 if (interface->is_inside)
698 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
701 pool_foreach (interface, sm->output_feature_interfaces,
703 if (interface->is_inside)
706 snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0);
713 int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
715 snat_main_t *sm = &snat_main;
717 const char * feature_name;
719 snat_static_mapping_t * m;
722 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
723 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
726 if (sm->num_workers > 1 && !sm->deterministic)
727 feature_name = is_inside ? "snat-in2out-worker-handoff" : "snat-out2in-worker-handoff";
728 else if (sm->deterministic)
729 feature_name = is_inside ? "snat-det-in2out" : "snat-det-out2in";
731 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
734 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
737 if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1)
738 sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, 0);
740 if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1)
741 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
743 pool_foreach (i, sm->interfaces,
745 if (i->sw_if_index == sw_if_index)
748 pool_put (sm->interfaces, i);
750 return VNET_API_ERROR_VALUE_EXIST;
757 return VNET_API_ERROR_NO_SUCH_ENTRY;
759 pool_get (sm->interfaces, i);
760 i->sw_if_index = sw_if_index;
761 i->is_inside = is_inside;
763 /* Add/delete external addresses to FIB */
768 vec_foreach (ap, sm->addresses)
769 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
771 pool_foreach (m, sm->static_mappings,
776 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
779 pool_foreach (dm, sm->det_maps,
781 snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del);
787 int snat_interface_add_del_output_feature (u32 sw_if_index,
791 snat_main_t *sm = &snat_main;
794 snat_static_mapping_t * m;
796 if (sm->deterministic ||
797 (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)))
798 return VNET_API_ERROR_UNSUPPORTED;
803 if (sm->num_workers > 1)
805 vnet_feature_enable_disable ("ip4-unicast", "snat-out2in-worker-handoff",
806 sw_if_index, !is_del, 0, 0);
807 vnet_feature_enable_disable ("ip4-output",
808 "snat-in2out-output-worker-handoff",
809 sw_if_index, !is_del, 0, 0);
813 vnet_feature_enable_disable ("ip4-unicast", "snat-out2in", sw_if_index,
815 vnet_feature_enable_disable ("ip4-output", "snat-in2out-output",
816 sw_if_index, !is_del, 0, 0);
819 if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1)
820 sm->fq_in2out_output_index =
821 vlib_frame_queue_main_init (sm->in2out_output_node_index, 0);
823 if (sm->fq_out2in_index == ~0 && sm->num_workers > 1)
824 sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, 0);
827 pool_foreach (i, sm->output_feature_interfaces,
829 if (i->sw_if_index == sw_if_index)
832 pool_put (sm->output_feature_interfaces, i);
834 return VNET_API_ERROR_VALUE_EXIST;
841 return VNET_API_ERROR_NO_SUCH_ENTRY;
843 pool_get (sm->output_feature_interfaces, i);
844 i->sw_if_index = sw_if_index;
845 i->is_inside = is_inside;
847 /* Add/delete external addresses to FIB */
852 vec_foreach (ap, sm->addresses)
853 snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del);
855 pool_foreach (m, sm->static_mappings,
860 snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del);
866 int snat_set_workers (uword * bitmap)
868 snat_main_t *sm = &snat_main;
871 if (sm->num_workers < 2)
872 return VNET_API_ERROR_FEATURE_DISABLED;
874 if (clib_bitmap_last_set (bitmap) >= sm->num_workers)
875 return VNET_API_ERROR_INVALID_WORKER;
877 vec_free (sm->workers);
878 clib_bitmap_foreach (i, bitmap,
880 vec_add1(sm->workers, i);
888 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
891 ip4_address_t * address,
893 u32 if_address_index,
896 static clib_error_t * snat_init (vlib_main_t * vm)
898 snat_main_t * sm = &snat_main;
899 clib_error_t * error = 0;
900 ip4_main_t * im = &ip4_main;
901 ip_lookup_main_t * lm = &im->lookup_main;
903 vlib_thread_registration_t *tr;
904 vlib_thread_main_t *tm = vlib_get_thread_main ();
907 ip4_add_del_interface_address_callback_t cb4;
910 sm->vnet_main = vnet_get_main();
912 sm->ip4_lookup_main = lm;
913 sm->api_main = &api_main;
914 sm->first_worker_index = 0;
918 sm->fq_in2out_index = ~0;
919 sm->fq_out2in_index = ~0;
920 sm->udp_timeout = SNAT_UDP_TIMEOUT;
921 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
922 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
923 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
925 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
928 tr = (vlib_thread_registration_t *) p[0];
931 sm->num_workers = tr->count;
932 sm->first_worker_index = tr->first_index;
936 /* Use all available workers by default */
937 if (sm->num_workers > 1)
939 for (i=0; i < sm->num_workers; i++)
940 bitmap = clib_bitmap_set (bitmap, i, 1);
941 snat_set_workers(bitmap);
942 clib_bitmap_free (bitmap);
945 error = snat_api_init(vm, sm);
949 /* Set up the interface address add/del callback */
950 cb4.function = snat_ip4_add_del_interface_address_cb;
951 cb4.function_opaque = 0;
953 vec_add1 (im->add_del_interface_address_callbacks, cb4);
955 /* Init IPFIX logging */
956 snat_ipfix_logging_init(vm);
958 error = nat64_init(vm);
963 VLIB_INIT_FUNCTION (snat_init);
965 void snat_free_outside_address_and_port (snat_main_t * sm,
966 snat_session_key_t * k,
970 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
972 ASSERT (address_index < vec_len (sm->addresses));
974 a = sm->addresses + address_index;
978 #define _(N, i, n, s) \
979 case SNAT_PROTOCOL_##N: \
980 ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
981 port_host_byte_order) == 1); \
982 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \
983 port_host_byte_order, 0); \
984 a->busy_##n##_ports--; \
986 foreach_snat_protocol
989 clib_warning("unknown_protocol");
995 * @brief Match SNAT static mapping.
997 * @param sm SNAT main.
998 * @param match Address and port to match.
999 * @param mapping External or local address and port of the matched mapping.
1000 * @param by_external If 0 match by local address otherwise match by external
1002 * @param is_addr_only If matched mapping is address only
1004 * @returns 0 if match found otherwise 1.
1006 int snat_static_mapping_match (snat_main_t * sm,
1007 snat_session_key_t match,
1008 snat_session_key_t * mapping,
1012 clib_bihash_kv_8_8_t kv, value;
1013 snat_static_mapping_t *m;
1014 snat_session_key_t m_key;
1015 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1018 mapping_hash = &sm->static_mapping_by_external;
1020 m_key.addr = match.addr;
1021 m_key.port = clib_net_to_host_u16 (match.port);
1022 m_key.protocol = match.protocol;
1023 m_key.fib_index = match.fib_index;
1025 kv.key = m_key.as_u64;
1027 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1029 /* Try address only mapping */
1032 kv.key = m_key.as_u64;
1033 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1037 m = pool_elt_at_index (sm->static_mappings, value.value);
1041 mapping->addr = m->local_addr;
1042 /* Address only mapping doesn't change port */
1043 mapping->port = m->addr_only ? match.port
1044 : clib_host_to_net_u16 (m->local_port);
1045 mapping->fib_index = m->fib_index;
1049 mapping->addr = m->external_addr;
1050 /* Address only mapping doesn't change port */
1051 mapping->port = m->addr_only ? match.port
1052 : clib_host_to_net_u16 (m->external_port);
1053 mapping->fib_index = sm->outside_fib_index;
1056 if (PREDICT_FALSE(is_addr_only != 0))
1057 *is_addr_only = m->addr_only;
1062 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1064 snat_session_key_t * k,
1065 u32 * address_indexp)
1071 for (i = 0; i < vec_len (sm->addresses); i++)
1073 a = sm->addresses + i;
1074 if (sm->vrf_mode && a->fib_index != ~0 && a->fib_index != fib_index)
1076 switch (k->protocol)
1078 #define _(N, j, n, s) \
1079 case SNAT_PROTOCOL_##N: \
1080 if (a->busy_##n##_ports < (65535-1024)) \
1084 portnum = random_u32 (&sm->random_seed); \
1085 portnum &= 0xFFFF; \
1086 if (portnum < 1024) \
1088 if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \
1090 clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \
1091 a->busy_##n##_ports++; \
1092 k->addr = a->addr; \
1093 k->port = clib_host_to_net_u16(portnum); \
1094 *address_indexp = i; \
1099 foreach_snat_protocol
1102 clib_warning("unknown protocol");
1107 /* Totally out of translations to use... */
1108 snat_ipfix_logging_addresses_exhausted(0);
1113 static clib_error_t *
1114 add_address_command_fn (vlib_main_t * vm,
1115 unformat_input_t * input,
1116 vlib_cli_command_t * cmd)
1118 unformat_input_t _line_input, *line_input = &_line_input;
1119 snat_main_t * sm = &snat_main;
1120 ip4_address_t start_addr, end_addr, this_addr;
1121 u32 start_host_order, end_host_order;
1126 clib_error_t *error = 0;
1128 /* Get a line of input. */
1129 if (!unformat_user (input, unformat_line_input, line_input))
1132 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1134 if (unformat (line_input, "%U - %U",
1135 unformat_ip4_address, &start_addr,
1136 unformat_ip4_address, &end_addr))
1138 else if (unformat (line_input, "tenant-vrf %u", &vrf_id))
1140 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1141 end_addr = start_addr;
1142 else if (unformat (line_input, "del"))
1146 error = clib_error_return (0, "unknown input '%U'",
1147 format_unformat_error, line_input);
1152 if (sm->static_mapping_only)
1154 error = clib_error_return (0, "static mapping only mode");
1158 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1159 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1161 if (end_host_order < start_host_order)
1163 error = clib_error_return (0, "end address less than start address");
1167 count = (end_host_order - start_host_order) + 1;
1170 clib_warning ("%U - %U, %d addresses...",
1171 format_ip4_address, &start_addr,
1172 format_ip4_address, &end_addr,
1175 this_addr = start_addr;
1177 for (i = 0; i < count; i++)
1180 snat_add_address (sm, &this_addr, vrf_id);
1182 rv = snat_del_address (sm, this_addr, 0);
1186 case VNET_API_ERROR_NO_SUCH_ENTRY:
1187 error = clib_error_return (0, "S-NAT address not exist.");
1189 case VNET_API_ERROR_UNSPECIFIED:
1190 error = clib_error_return (0, "S-NAT address used in static mapping.");
1196 increment_v4_address (&this_addr);
1200 unformat_free (line_input);
1205 VLIB_CLI_COMMAND (add_address_command, static) = {
1206 .path = "snat add address",
1207 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] "
1208 "[tenant-vrf <vrf-id>] [del]",
1209 .function = add_address_command_fn,
1212 static clib_error_t *
1213 snat_feature_command_fn (vlib_main_t * vm,
1214 unformat_input_t * input,
1215 vlib_cli_command_t * cmd)
1217 unformat_input_t _line_input, *line_input = &_line_input;
1218 vnet_main_t * vnm = vnet_get_main();
1219 clib_error_t * error = 0;
1221 u32 * inside_sw_if_indices = 0;
1222 u32 * outside_sw_if_indices = 0;
1223 u8 is_output_feature = 0;
1229 /* Get a line of input. */
1230 if (!unformat_user (input, unformat_line_input, line_input))
1233 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1235 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1237 vec_add1 (inside_sw_if_indices, sw_if_index);
1238 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1240 vec_add1 (outside_sw_if_indices, sw_if_index);
1241 else if (unformat (line_input, "output-feature"))
1242 is_output_feature = 1;
1243 else if (unformat (line_input, "del"))
1247 error = clib_error_return (0, "unknown input '%U'",
1248 format_unformat_error, line_input);
1253 if (vec_len (inside_sw_if_indices))
1255 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1257 sw_if_index = inside_sw_if_indices[i];
1258 if (is_output_feature)
1260 if (snat_interface_add_del_output_feature (sw_if_index, 1, is_del))
1262 error = clib_error_return (0, "%s %U failed",
1263 is_del ? "del" : "add",
1264 format_vnet_sw_interface_name, vnm,
1265 vnet_get_sw_interface (vnm,
1272 if (snat_interface_add_del (sw_if_index, 1, is_del))
1274 error = clib_error_return (0, "%s %U failed",
1275 is_del ? "del" : "add",
1276 format_vnet_sw_interface_name, vnm,
1277 vnet_get_sw_interface (vnm,
1285 if (vec_len (outside_sw_if_indices))
1287 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1289 sw_if_index = outside_sw_if_indices[i];
1290 if (is_output_feature)
1292 if (snat_interface_add_del_output_feature (sw_if_index, 0, is_del))
1294 error = clib_error_return (0, "%s %U failed",
1295 is_del ? "del" : "add",
1296 format_vnet_sw_interface_name, vnm,
1297 vnet_get_sw_interface (vnm,
1304 if (snat_interface_add_del (sw_if_index, 0, is_del))
1306 error = clib_error_return (0, "%s %U failed",
1307 is_del ? "del" : "add",
1308 format_vnet_sw_interface_name, vnm,
1309 vnet_get_sw_interface (vnm,
1318 unformat_free (line_input);
1319 vec_free (inside_sw_if_indices);
1320 vec_free (outside_sw_if_indices);
1325 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1326 .path = "set interface snat",
1327 .function = snat_feature_command_fn,
1328 .short_help = "set interface snat in <intfc> out <intfc> [output-feature] "
1333 unformat_snat_protocol (unformat_input_t * input, va_list * args)
1335 u32 *r = va_arg (*args, u32 *);
1338 #define _(N, i, n, s) else if (unformat (input, s)) *r = SNAT_PROTOCOL_##N;
1339 foreach_snat_protocol
1347 format_snat_protocol (u8 * s, va_list * args)
1349 u32 i = va_arg (*args, u32);
1354 #define _(N, j, n, str) case SNAT_PROTOCOL_##N: t = (u8 *) str; break;
1355 foreach_snat_protocol
1358 s = format (s, "unknown");
1360 s = format (s, "%s", t);
1364 static clib_error_t *
1365 add_static_mapping_command_fn (vlib_main_t * vm,
1366 unformat_input_t * input,
1367 vlib_cli_command_t * cmd)
1369 unformat_input_t _line_input, *line_input = &_line_input;
1370 clib_error_t * error = 0;
1371 ip4_address_t l_addr, e_addr;
1372 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1375 u32 sw_if_index = ~0;
1376 vnet_main_t * vnm = vnet_get_main();
1378 snat_protocol_t proto;
1381 /* Get a line of input. */
1382 if (!unformat_user (input, unformat_line_input, line_input))
1385 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1387 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1390 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1392 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1395 else if (unformat (line_input, "external %U", unformat_ip4_address,
1398 else if (unformat (line_input, "external %U %u",
1399 unformat_vnet_sw_interface, vnm, &sw_if_index,
1403 else if (unformat (line_input, "external %U",
1404 unformat_vnet_sw_interface, vnm, &sw_if_index))
1406 else if (unformat (line_input, "vrf %u", &vrf_id))
1408 else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
1410 else if (unformat (line_input, "del"))
1414 error = clib_error_return (0, "unknown input: '%U'",
1415 format_unformat_error, line_input);
1420 if (!addr_only && !proto_set)
1422 error = clib_error_return (0, "missing protocol");
1426 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1427 vrf_id, addr_only, sw_if_index, proto, is_add);
1431 case VNET_API_ERROR_INVALID_VALUE:
1432 error = clib_error_return (0, "External port already in use.");
1434 case VNET_API_ERROR_NO_SUCH_ENTRY:
1436 error = clib_error_return (0, "External addres must be allocated.");
1438 error = clib_error_return (0, "Mapping not exist.");
1440 case VNET_API_ERROR_NO_SUCH_FIB:
1441 error = clib_error_return (0, "No such VRF id.");
1443 case VNET_API_ERROR_VALUE_EXIST:
1444 error = clib_error_return (0, "Mapping already exist.");
1451 unformat_free (line_input);
1458 * @cliexstart{snat add static mapping}
1459 * Static mapping allows hosts on the external network to initiate connection
1460 * to to the local network host.
1461 * To create static mapping between local host address 10.0.0.3 port 6303 and
1462 * external address 4.4.4.4 port 3606 for TCP protocol use:
1463 * vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
1464 * If not runnig "static mapping only" S-NAT plugin mode use before:
1465 * vpp# snat add address 4.4.4.4
1466 * To create static mapping between local and external address use:
1467 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1470 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1471 .path = "snat add static mapping",
1472 .function = add_static_mapping_command_fn,
1474 "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1477 static clib_error_t *
1478 set_workers_command_fn (vlib_main_t * vm,
1479 unformat_input_t * input,
1480 vlib_cli_command_t * cmd)
1482 unformat_input_t _line_input, *line_input = &_line_input;
1485 clib_error_t *error = 0;
1487 /* Get a line of input. */
1488 if (!unformat_user (input, unformat_line_input, line_input))
1491 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1493 if (unformat (line_input, "%U", unformat_bitmap_list, &bitmap))
1497 error = clib_error_return (0, "unknown input '%U'",
1498 format_unformat_error, line_input);
1505 error = clib_error_return (0, "List of workers must be specified.");
1509 rv = snat_set_workers(bitmap);
1511 clib_bitmap_free (bitmap);
1515 case VNET_API_ERROR_INVALID_WORKER:
1516 error = clib_error_return (0, "Invalid worker(s).");
1518 case VNET_API_ERROR_FEATURE_DISABLED:
1519 error = clib_error_return (0,
1520 "Supported only if 2 or more workes available.");
1527 unformat_free (line_input);
1534 * @cliexstart{set snat workers}
1535 * Set SNAT workers if 2 or more workers available, use:
1536 * vpp# set snat workers 0-2,5
1539 VLIB_CLI_COMMAND (set_workers_command, static) = {
1540 .path = "set snat workers",
1541 .function = set_workers_command_fn,
1543 "set snat workers <workers-list>",
1546 static clib_error_t *
1547 snat_ipfix_logging_enable_disable_command_fn (vlib_main_t * vm,
1548 unformat_input_t * input,
1549 vlib_cli_command_t * cmd)
1551 unformat_input_t _line_input, *line_input = &_line_input;
1556 clib_error_t *error = 0;
1558 /* Get a line of input. */
1559 if (!unformat_user (input, unformat_line_input, line_input))
1562 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1564 if (unformat (line_input, "domain %d", &domain_id))
1566 else if (unformat (line_input, "src-port %d", &src_port))
1568 else if (unformat (line_input, "disable"))
1572 error = clib_error_return (0, "unknown input '%U'",
1573 format_unformat_error, line_input);
1578 rv = snat_ipfix_logging_enable_disable (enable, domain_id, (u16) src_port);
1582 error = clib_error_return (0, "ipfix logging enable failed");
1587 unformat_free (line_input);
1594 * @cliexstart{snat ipfix logging}
1595 * To enable SNAT IPFIX logging use:
1596 * vpp# snat ipfix logging
1597 * To set IPFIX exporter use:
1598 * vpp# set ipfix exporter collector 10.10.10.3 src 10.10.10.1
1601 VLIB_CLI_COMMAND (snat_ipfix_logging_enable_disable_command, static) = {
1602 .path = "snat ipfix logging",
1603 .function = snat_ipfix_logging_enable_disable_command_fn,
1604 .short_help = "snat ipfix logging [domain <domain-id>] [src-port <port>] [disable]",
1608 snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1610 snat_main_t *sm = &snat_main;
1611 snat_user_key_t key0;
1612 clib_bihash_kv_8_8_t kv0, value0;
1613 u32 next_worker_index = 0;
1615 key0.addr = ip0->src_address;
1616 key0.fib_index = rx_fib_index0;
1618 kv0.key = key0.as_u64;
1620 /* Ever heard of of the "user" before? */
1621 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1623 /* No, assign next available worker (RR) */
1624 next_worker_index = sm->first_worker_index;
1625 if (vec_len (sm->workers))
1627 next_worker_index +=
1628 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1631 /* add non-traslated packets worker lookup */
1632 kv0.value = next_worker_index;
1633 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1636 next_worker_index = value0.value;
1638 return next_worker_index;
1642 snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0)
1644 snat_main_t *sm = &snat_main;
1645 snat_worker_key_t key0;
1646 clib_bihash_kv_8_8_t kv0, value0;
1647 udp_header_t * udp0;
1648 u32 next_worker_index = 0;
1650 udp0 = ip4_next_header (ip0);
1652 key0.addr = ip0->dst_address;
1653 key0.port = udp0->dst_port;
1654 key0.fib_index = rx_fib_index0;
1656 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
1658 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
1659 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
1660 key0.port = echo0->identifier;
1663 kv0.key = key0.as_u64;
1665 /* Ever heard of of the "user" before? */
1666 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1669 kv0.key = key0.as_u64;
1671 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
1673 /* No, assign next available worker (RR) */
1674 next_worker_index = sm->first_worker_index;
1675 if (vec_len (sm->workers))
1677 next_worker_index +=
1678 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
1683 /* Static mapping without port */
1684 next_worker_index = value0.value;
1687 /* Add to translated packets worker lookup */
1688 kv0.value = next_worker_index;
1689 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
1692 next_worker_index = value0.value;
1694 return next_worker_index;
1697 static clib_error_t *
1698 snat_config (vlib_main_t * vm, unformat_input_t * input)
1700 snat_main_t * sm = &snat_main;
1701 u32 translation_buckets = 1024;
1702 u32 translation_memory_size = 128<<20;
1703 u32 user_buckets = 128;
1704 u32 user_memory_size = 64<<20;
1705 u32 max_translations_per_user = 100;
1706 u32 outside_vrf_id = 0;
1707 u32 inside_vrf_id = 0;
1708 u32 static_mapping_buckets = 1024;
1709 u32 static_mapping_memory_size = 64<<20;
1710 u8 static_mapping_only = 0;
1711 u8 static_mapping_connection_tracking = 0;
1712 vlib_thread_main_t *tm = vlib_get_thread_main ();
1714 sm->deterministic = 0;
1716 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1718 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1720 else if (unformat (input, "translation hash memory %d",
1721 &translation_memory_size));
1722 else if (unformat (input, "user hash buckets %d", &user_buckets))
1724 else if (unformat (input, "user hash memory %d",
1727 else if (unformat (input, "max translations per user %d",
1728 &max_translations_per_user))
1730 else if (unformat (input, "outside VRF id %d",
1733 else if (unformat (input, "inside VRF id %d",
1736 else if (unformat (input, "static mapping only"))
1738 static_mapping_only = 1;
1739 if (unformat (input, "connection tracking"))
1740 static_mapping_connection_tracking = 1;
1742 else if (unformat (input, "deterministic"))
1743 sm->deterministic = 1;
1745 return clib_error_return (0, "unknown input '%U'",
1746 format_unformat_error, input);
1749 /* for show commands, etc. */
1750 sm->translation_buckets = translation_buckets;
1751 sm->translation_memory_size = translation_memory_size;
1752 sm->user_buckets = user_buckets;
1753 sm->user_memory_size = user_memory_size;
1754 sm->max_translations_per_user = max_translations_per_user;
1755 sm->outside_vrf_id = outside_vrf_id;
1756 sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1758 sm->inside_vrf_id = inside_vrf_id;
1759 sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4,
1761 sm->static_mapping_only = static_mapping_only;
1762 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1764 if (sm->deterministic)
1766 sm->in2out_node_index = snat_det_in2out_node.index;
1767 sm->in2out_output_node_index = ~0;
1768 sm->out2in_node_index = snat_det_out2in_node.index;
1769 sm->icmp_match_in2out_cb = icmp_match_in2out_det;
1770 sm->icmp_match_out2in_cb = icmp_match_out2in_det;
1774 sm->worker_in2out_cb = snat_get_worker_in2out_cb;
1775 sm->worker_out2in_cb = snat_get_worker_out2in_cb;
1776 sm->in2out_node_index = snat_in2out_node.index;
1777 sm->in2out_output_node_index = snat_in2out_output_node.index;
1778 sm->out2in_node_index = snat_out2in_node.index;
1779 if (!static_mapping_only ||
1780 (static_mapping_only && static_mapping_connection_tracking))
1782 sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
1783 sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
1785 clib_bihash_init_8_8 (&sm->worker_by_in, "worker-by-in", user_buckets,
1788 clib_bihash_init_8_8 (&sm->worker_by_out, "worker-by-out", user_buckets,
1791 vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1);
1793 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1794 translation_memory_size);
1796 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1797 translation_memory_size);
1799 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1802 clib_bihash_init_16_8 (&sm->in2out_unk_proto, "in2out-unk-proto",
1803 translation_buckets, translation_memory_size);
1805 clib_bihash_init_16_8 (&sm->out2in_unk_proto, "out2in-unk-proto",
1806 translation_buckets, translation_memory_size);
1810 sm->icmp_match_in2out_cb = icmp_match_in2out_fast;
1811 sm->icmp_match_out2in_cb = icmp_match_out2in_fast;
1813 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1814 "static_mapping_by_local", static_mapping_buckets,
1815 static_mapping_memory_size);
1817 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1818 "static_mapping_by_external", static_mapping_buckets,
1819 static_mapping_memory_size);
1825 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1827 u8 * format_snat_session_state (u8 * s, va_list * args)
1829 u32 i = va_arg (*args, u32);
1834 #define _(v, N, str) case SNAT_SESSION_##N: t = (u8 *) str; break;
1835 foreach_snat_session_state
1838 t = format (t, "unknown");
1840 s = format (s, "%s", t);
1844 u8 * format_snat_key (u8 * s, va_list * args)
1846 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1847 char * protocol_string = "unknown";
1848 static char *protocol_strings[] = {
1854 if (key->protocol < ARRAY_LEN(protocol_strings))
1855 protocol_string = protocol_strings[key->protocol];
1857 s = format (s, "%U proto %s port %d fib %d",
1858 format_ip4_address, &key->addr, protocol_string,
1859 clib_net_to_host_u16 (key->port), key->fib_index);
1863 u8 * format_snat_session (u8 * s, va_list * args)
1865 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1866 snat_session_t * sess = va_arg (*args, snat_session_t *);
1868 if (snat_is_unk_proto_session (sess))
1870 s = format (s, " i2o %U proto %u fib %u\n",
1871 format_ip4_address, &sess->in2out.addr, sess->in2out.port,
1872 sess->in2out.fib_index);
1873 s = format (s, " o2i %U proto %u fib %u\n",
1874 format_ip4_address, &sess->out2in.addr, sess->out2in.port,
1875 sess->out2in.fib_index);
1879 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1880 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1882 s = format (s, " last heard %.2f\n", sess->last_heard);
1883 s = format (s, " total pkts %d, total bytes %lld\n",
1884 sess->total_pkts, sess->total_bytes);
1885 if (snat_is_session_static (sess))
1886 s = format (s, " static translation\n");
1888 s = format (s, " dynamic translation\n");
1893 u8 * format_snat_user (u8 * s, va_list * args)
1895 snat_main_per_thread_data_t * sm = va_arg (*args, snat_main_per_thread_data_t *);
1896 snat_user_t * u = va_arg (*args, snat_user_t *);
1897 int verbose = va_arg (*args, int);
1898 dlist_elt_t * head, * elt;
1899 u32 elt_index, head_index;
1901 snat_session_t * sess;
1903 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1904 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1909 if (u->nsessions || u->nstaticsessions)
1911 head_index = u->sessions_per_user_list_head_index;
1912 head = pool_elt_at_index (sm->list_pool, head_index);
1914 elt_index = head->next;
1915 elt = pool_elt_at_index (sm->list_pool, elt_index);
1916 session_index = elt->value;
1918 while (session_index != ~0)
1920 sess = pool_elt_at_index (sm->sessions, session_index);
1922 s = format (s, " %U\n", format_snat_session, sm, sess);
1924 elt_index = elt->next;
1925 elt = pool_elt_at_index (sm->list_pool, elt_index);
1926 session_index = elt->value;
1933 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1935 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1938 s = format (s, "local %U external %U vrf %d",
1939 format_ip4_address, &m->local_addr,
1940 format_ip4_address, &m->external_addr,
1943 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1944 format_snat_protocol, m->proto,
1945 format_ip4_address, &m->local_addr, m->local_port,
1946 format_ip4_address, &m->external_addr, m->external_port,
1952 u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
1954 snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
1955 vnet_main_t *vnm = vnet_get_main();
1958 s = format (s, "local %U external %U vrf %d",
1959 format_ip4_address, &m->l_addr,
1960 format_vnet_sw_interface_name, vnm,
1961 vnet_get_sw_interface (vnm, m->sw_if_index),
1964 s = format (s, "%U local %U:%d external %U:%d vrf %d",
1965 format_snat_protocol, m->proto,
1966 format_ip4_address, &m->l_addr, m->l_port,
1967 format_vnet_sw_interface_name, vnm,
1968 vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
1974 u8 * format_det_map_ses (u8 * s, va_list * args)
1976 snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
1977 ip4_address_t in_addr, out_addr;
1978 u32 in_offset, out_offset;
1979 snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
1980 u32 * i = va_arg (*args, u32 *);
1982 u32 user_index = *i / SNAT_DET_SES_PER_USER;
1983 in_addr.as_u32 = clib_host_to_net_u32 (
1984 clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
1985 in_offset = clib_net_to_host_u32(in_addr.as_u32) -
1986 clib_net_to_host_u32(det_map->in_addr.as_u32);
1987 out_offset = in_offset / det_map->sharing_ratio;
1988 out_addr.as_u32 = clib_host_to_net_u32(
1989 clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
1990 s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
1991 format_ip4_address, &in_addr,
1992 clib_net_to_host_u16 (ses->in_port),
1993 format_ip4_address, &out_addr,
1994 clib_net_to_host_u16 (ses->out.out_port),
1995 format_ip4_address, &ses->out.ext_host_addr,
1996 clib_net_to_host_u16 (ses->out.ext_host_port),
1997 format_snat_session_state, ses->state,
2003 static clib_error_t *
2004 show_snat_command_fn (vlib_main_t * vm,
2005 unformat_input_t * input,
2006 vlib_cli_command_t * cmd)
2009 snat_main_t * sm = &snat_main;
2011 snat_static_mapping_t *m;
2012 snat_interface_t *i;
2013 snat_address_t * ap;
2014 vnet_main_t *vnm = vnet_get_main();
2015 snat_main_per_thread_data_t *tsm;
2016 u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
2018 snat_static_map_resolve_t *rp;
2019 snat_det_map_t * dm;
2020 snat_det_session_t * ses;
2022 if (unformat (input, "detail"))
2024 else if (unformat (input, "verbose"))
2027 if (sm->static_mapping_only)
2029 if (sm->static_mapping_connection_tracking)
2030 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
2033 vlib_cli_output (vm, "SNAT mode: static mapping only");
2035 else if (sm->deterministic)
2037 vlib_cli_output (vm, "SNAT mode: deterministic mapping");
2041 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
2046 pool_foreach (i, sm->interfaces,
2048 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
2049 vnet_get_sw_interface (vnm, i->sw_if_index),
2050 i->is_inside ? "in" : "out");
2053 pool_foreach (i, sm->output_feature_interfaces,
2055 vlib_cli_output (vm, "%U output-feature %s",
2056 format_vnet_sw_interface_name, vnm,
2057 vnet_get_sw_interface (vnm, i->sw_if_index),
2058 i->is_inside ? "in" : "out");
2061 if (vec_len (sm->auto_add_sw_if_indices))
2063 vlib_cli_output (vm, "SNAT pool addresses interfaces:");
2064 vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
2066 vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
2067 vnet_get_sw_interface (vnm, *sw_if_index));
2071 vec_foreach (ap, sm->addresses)
2073 vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
2074 if (ap->fib_index != ~0)
2075 vlib_cli_output (vm, " tenant VRF: %u",
2076 ip4_fib_get(ap->fib_index)->table_id);
2078 vlib_cli_output (vm, " tenant VRF independent");
2079 #define _(N, i, n, s) \
2080 vlib_cli_output (vm, " %d busy %s ports", ap->busy_##n##_ports, s);
2081 foreach_snat_protocol
2086 if (sm->num_workers > 1)
2088 vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
2091 vec_foreach (worker, sm->workers)
2093 vlib_worker_thread_t *w =
2094 vlib_worker_threads + *worker + sm->first_worker_index;
2095 vlib_cli_output (vm, " %s", w->name);
2100 if (sm->deterministic)
2102 vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
2103 vlib_cli_output (vm, "tcp-established timeout: %dsec",
2104 sm->tcp_established_timeout);
2105 vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
2106 sm->tcp_transitory_timeout);
2107 vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
2108 vlib_cli_output (vm, "%d deterministic mappings",
2109 pool_elts (sm->det_maps));
2112 pool_foreach (dm, sm->det_maps,
2114 vlib_cli_output (vm, "in %U/%d out %U/%d\n",
2115 format_ip4_address, &dm->in_addr, dm->in_plen,
2116 format_ip4_address, &dm->out_addr, dm->out_plen);
2117 vlib_cli_output (vm, " outside address sharing ratio: %d\n",
2119 vlib_cli_output (vm, " number of ports per inside host: %d\n",
2120 dm->ports_per_host);
2121 vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
2124 vec_foreach_index (j, dm->sessions)
2126 ses = vec_elt_at_index (dm->sessions, j);
2128 vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
2137 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
2139 vlib_cli_output (vm, "%d static mappings",
2140 pool_elts (sm->static_mappings));
2144 pool_foreach (m, sm->static_mappings,
2146 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2152 vec_foreach (tsm, sm->per_thread_data)
2154 users_num += pool_elts (tsm->users);
2155 sessions_num += pool_elts (tsm->sessions);
2158 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
2159 " %d static mappings",
2161 vec_len (sm->addresses),
2163 pool_elts (sm->static_mappings));
2167 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
2169 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
2171 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
2173 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
2175 vec_foreach_index (j, sm->per_thread_data)
2177 tsm = vec_elt_at_index (sm->per_thread_data, j);
2179 if (pool_elts (tsm->users) == 0)
2182 vlib_worker_thread_t *w = vlib_worker_threads + j;
2183 vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
2185 vlib_cli_output (vm, " %d list pool elements",
2186 pool_elts (tsm->list_pool));
2188 pool_foreach (u, tsm->users,
2190 vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
2195 if (pool_elts (sm->static_mappings))
2197 vlib_cli_output (vm, "static mappings:");
2198 pool_foreach (m, sm->static_mappings,
2200 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
2202 for (j = 0; j < vec_len (sm->to_resolve); j++)
2204 rp = sm->to_resolve + j;
2205 vlib_cli_output (vm, "%U",
2206 format_snat_static_map_to_resolve, rp);
2215 VLIB_CLI_COMMAND (show_snat_command, static) = {
2216 .path = "show snat",
2217 .short_help = "show snat",
2218 .function = show_snat_command_fn,
2223 snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
2226 ip4_address_t * address,
2228 u32 if_address_index,
2231 snat_main_t *sm = &snat_main;
2232 snat_static_map_resolve_t *rp;
2233 u32 *indices_to_delete = 0;
2237 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2239 if (sw_if_index == sm->auto_add_sw_if_indices[i])
2243 /* Don't trip over lease renewal, static config */
2244 for (j = 0; j < vec_len(sm->addresses); j++)
2245 if (sm->addresses[j].addr.as_u32 == address->as_u32)
2248 snat_add_address (sm, address, ~0);
2249 /* Scan static map resolution vector */
2250 for (j = 0; j < vec_len (sm->to_resolve); j++)
2252 rp = sm->to_resolve + j;
2253 /* On this interface? */
2254 if (rp->sw_if_index == sw_if_index)
2256 /* Add the static mapping */
2257 rv = snat_add_static_mapping (rp->l_addr,
2263 ~0 /* sw_if_index */,
2267 clib_warning ("snat_add_static_mapping returned %d",
2269 vec_add1 (indices_to_delete, j);
2272 /* If we resolved any of the outstanding static mappings */
2273 if (vec_len(indices_to_delete))
2276 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2277 vec_delete(sm->to_resolve, 1, j);
2278 vec_free(indices_to_delete);
2284 (void) snat_del_address(sm, address[0], 1);
2292 int snat_add_interface_address (snat_main_t *sm, u32 sw_if_index, int is_del)
2294 ip4_main_t * ip4_main = sm->ip4_main;
2295 ip4_address_t * first_int_addr;
2296 snat_static_map_resolve_t *rp;
2297 u32 *indices_to_delete = 0;
2300 first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index,
2301 0 /* just want the address*/);
2303 for (i = 0; i < vec_len(sm->auto_add_sw_if_indices); i++)
2305 if (sm->auto_add_sw_if_indices[i] == sw_if_index)
2309 /* if have address remove it */
2311 (void) snat_del_address (sm, first_int_addr[0], 1);
2314 for (j = 0; j < vec_len (sm->to_resolve); j++)
2316 rp = sm->to_resolve + j;
2317 if (rp->sw_if_index == sw_if_index)
2318 vec_add1 (indices_to_delete, j);
2320 if (vec_len(indices_to_delete))
2322 for (j = vec_len(indices_to_delete)-1; j >= 0; j--)
2323 vec_del1(sm->to_resolve, j);
2324 vec_free(indices_to_delete);
2327 vec_del1(sm->auto_add_sw_if_indices, i);
2330 return VNET_API_ERROR_VALUE_EXIST;
2337 return VNET_API_ERROR_NO_SUCH_ENTRY;
2339 /* add to the auto-address list */
2340 vec_add1(sm->auto_add_sw_if_indices, sw_if_index);
2342 /* If the address is already bound - or static - add it now */
2344 snat_add_address (sm, first_int_addr, ~0);
2349 static clib_error_t *
2350 snat_add_interface_address_command_fn (vlib_main_t * vm,
2351 unformat_input_t * input,
2352 vlib_cli_command_t * cmd)
2354 snat_main_t *sm = &snat_main;
2355 unformat_input_t _line_input, *line_input = &_line_input;
2359 clib_error_t *error = 0;
2361 /* Get a line of input. */
2362 if (!unformat_user (input, unformat_line_input, line_input))
2365 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2367 if (unformat (line_input, "%U", unformat_vnet_sw_interface,
2368 sm->vnet_main, &sw_if_index))
2370 else if (unformat (line_input, "del"))
2374 error = clib_error_return (0, "unknown input '%U'",
2375 format_unformat_error, line_input);
2380 rv = snat_add_interface_address (sm, sw_if_index, is_del);
2388 error = clib_error_return (0, "snat_add_interface_address returned %d",
2394 unformat_free (line_input);
2399 VLIB_CLI_COMMAND (snat_add_interface_address_command, static) = {
2400 .path = "snat add interface address",
2401 .short_help = "snat add interface address <interface> [del]",
2402 .function = snat_add_interface_address_command_fn,
2405 static clib_error_t *
2406 snat_det_map_command_fn (vlib_main_t * vm,
2407 unformat_input_t * input,
2408 vlib_cli_command_t * cmd)
2410 snat_main_t *sm = &snat_main;
2411 unformat_input_t _line_input, *line_input = &_line_input;
2412 ip4_address_t in_addr, out_addr;
2413 u32 in_plen, out_plen;
2415 clib_error_t *error = 0;
2417 /* Get a line of input. */
2418 if (!unformat_user (input, unformat_line_input, line_input))
2421 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2423 if (unformat (line_input, "in %U/%u", unformat_ip4_address, &in_addr, &in_plen))
2425 else if (unformat (line_input, "out %U/%u", unformat_ip4_address, &out_addr, &out_plen))
2427 else if (unformat (line_input, "del"))
2431 error = clib_error_return (0, "unknown input '%U'",
2432 format_unformat_error, line_input);
2437 unformat_free (line_input);
2439 rv = snat_det_add_map(sm, &in_addr, (u8) in_plen, &out_addr, (u8)out_plen,
2444 error = clib_error_return (0, "snat_det_add_map return %d", rv);
2449 unformat_free (line_input);
2456 * @cliexstart{snat deterministic add}
2457 * Create bijective mapping of inside address to outside address and port range
2458 * pairs, with the purpose of enabling deterministic NAT to reduce logging in
2460 * To create deterministic mapping between inside network 10.0.0.0/18 and
2461 * outside network 1.1.1.0/30 use:
2462 * # vpp# snat deterministic add in 10.0.0.0/18 out 1.1.1.0/30
2465 VLIB_CLI_COMMAND (snat_det_map_command, static) = {
2466 .path = "snat deterministic add",
2467 .short_help = "snat deterministic add in <addr>/<plen> out <addr>/<plen> [del]",
2468 .function = snat_det_map_command_fn,
2471 static clib_error_t *
2472 snat_det_forward_command_fn (vlib_main_t * vm,
2473 unformat_input_t * input,
2474 vlib_cli_command_t * cmd)
2476 snat_main_t *sm = &snat_main;
2477 unformat_input_t _line_input, *line_input = &_line_input;
2478 ip4_address_t in_addr, out_addr;
2480 snat_det_map_t * dm;
2481 clib_error_t *error = 0;
2483 /* Get a line of input. */
2484 if (!unformat_user (input, unformat_line_input, line_input))
2487 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2489 if (unformat (line_input, "%U", unformat_ip4_address, &in_addr))
2493 error = clib_error_return (0, "unknown input '%U'",
2494 format_unformat_error, line_input);
2499 unformat_free (line_input);
2501 dm = snat_det_map_by_user(sm, &in_addr);
2503 vlib_cli_output (vm, "no match");
2506 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2507 vlib_cli_output (vm, "%U:<%d-%d>", format_ip4_address, &out_addr,
2508 lo_port, lo_port + dm->ports_per_host - 1);
2512 unformat_free (line_input);
2519 * @cliexstart{snat deterministic forward}
2520 * Return outside address and port range from inside address for deterministic
2522 * To obtain outside address and port of inside host use:
2523 * vpp# snat deterministic forward 10.0.0.2
2524 * 1.1.1.0:<1054-1068>
2527 VLIB_CLI_COMMAND (snat_det_forward_command, static) = {
2528 .path = "snat deterministic forward",
2529 .short_help = "snat deterministic forward <addr>",
2530 .function = snat_det_forward_command_fn,
2533 static clib_error_t *
2534 snat_det_reverse_command_fn (vlib_main_t * vm,
2535 unformat_input_t * input,
2536 vlib_cli_command_t * cmd)
2538 snat_main_t *sm = &snat_main;
2539 unformat_input_t _line_input, *line_input = &_line_input;
2540 ip4_address_t in_addr, out_addr;
2542 snat_det_map_t * dm;
2543 clib_error_t *error = 0;
2545 /* Get a line of input. */
2546 if (!unformat_user (input, unformat_line_input, line_input))
2549 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2551 if (unformat (line_input, "%U:%d", unformat_ip4_address, &out_addr, &out_port))
2555 error = clib_error_return (0, "unknown input '%U'",
2556 format_unformat_error, line_input);
2560 unformat_free (line_input);
2562 if (out_port < 1024 || out_port > 65535)
2564 error = clib_error_return (0, "wrong port, must be <1024-65535>");
2568 dm = snat_det_map_by_out(sm, &out_addr);
2570 vlib_cli_output (vm, "no match");
2573 snat_det_reverse (dm, &out_addr, (u16) out_port, &in_addr);
2574 vlib_cli_output (vm, "%U", format_ip4_address, &in_addr);
2578 unformat_free (line_input);
2585 * @cliexstart{snat deterministic reverse}
2586 * Return inside address from outside address and port for deterministic NAT.
2587 * To obtain inside host address from outside address and port use:
2588 * #vpp snat deterministic reverse 1.1.1.1:1276
2592 VLIB_CLI_COMMAND (snat_det_reverse_command, static) = {
2593 .path = "snat deterministic reverse",
2594 .short_help = "snat deterministic reverse <addr>:<port>",
2595 .function = snat_det_reverse_command_fn,
2598 static clib_error_t *
2599 set_timeout_command_fn (vlib_main_t * vm,
2600 unformat_input_t * input,
2601 vlib_cli_command_t * cmd)
2603 snat_main_t *sm = &snat_main;
2604 unformat_input_t _line_input, *line_input = &_line_input;
2605 clib_error_t *error = 0;
2607 /* Get a line of input. */
2608 if (!unformat_user (input, unformat_line_input, line_input))
2611 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2613 if (unformat (line_input, "udp %u", &sm->udp_timeout))
2615 else if (unformat (line_input, "tcp-established %u",
2616 &sm->tcp_established_timeout))
2618 else if (unformat (line_input, "tcp-transitory %u",
2619 &sm->tcp_transitory_timeout))
2621 else if (unformat (line_input, "icmp %u", &sm->icmp_timeout))
2623 else if (unformat (line_input, "reset"))
2625 sm->udp_timeout = SNAT_UDP_TIMEOUT;
2626 sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT;
2627 sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT;
2628 sm->icmp_timeout = SNAT_ICMP_TIMEOUT;
2632 error = clib_error_return (0, "unknown input '%U'",
2633 format_unformat_error, line_input);
2638 unformat_free (line_input);
2641 unformat_free (line_input);
2648 * @cliexstart{set snat deterministic timeout}
2649 * Set values of timeouts for deterministic NAT (in seconds), use:
2650 * vpp# set snat deterministic timeout udp 120 tcp-established 7500
2651 * tcp-transitory 250 icmp 90
2652 * To reset default values use:
2653 * vpp# set snat deterministic timeout reset
2656 VLIB_CLI_COMMAND (set_timeout_command, static) = {
2657 .path = "set snat deterministic timeout",
2658 .function = set_timeout_command_fn,
2660 "set snat deterministic timeout [udp <sec> | tcp-established <sec> "
2661 "tcp-transitory <sec> | icmp <sec> | reset]",
2664 static clib_error_t *
2665 snat_det_close_session_out_fn (vlib_main_t *vm,
2666 unformat_input_t * input,
2667 vlib_cli_command_t * cmd)
2669 snat_main_t *sm = &snat_main;
2670 unformat_input_t _line_input, *line_input = &_line_input;
2671 ip4_address_t out_addr, ext_addr, in_addr;
2672 u16 out_port, ext_port;
2673 snat_det_map_t * dm;
2674 snat_det_session_t * ses;
2675 snat_det_out_key_t key;
2676 clib_error_t *error = 0;
2678 /* Get a line of input. */
2679 if (!unformat_user (input, unformat_line_input, line_input))
2682 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2684 if (unformat (line_input, "%U:%d %U:%d",
2685 unformat_ip4_address, &out_addr, &out_port,
2686 unformat_ip4_address, &ext_addr, &ext_port))
2690 error = clib_error_return (0, "unknown input '%U'",
2691 format_unformat_error, line_input);
2696 unformat_free (line_input);
2698 dm = snat_det_map_by_out(sm, &out_addr);
2700 vlib_cli_output (vm, "no match");
2703 snat_det_reverse(dm, &ext_addr, out_port, &in_addr);
2704 key.ext_host_addr = out_addr;
2705 key.ext_host_port = ntohs(ext_port);
2706 key.out_port = ntohs(out_port);
2707 ses = snat_det_get_ses_by_out(dm, &out_addr, key.as_u64);
2709 vlib_cli_output (vm, "no match");
2711 snat_det_ses_close(dm, ses);
2715 unformat_free (line_input);
2722 * @cliexstart{snat deterministic close session out}
2723 * Close session using outside ip address and port
2724 * and external ip address and port, use:
2725 * vpp# snat deterministic close session out 1.1.1.1:1276 2.2.2.2:2387
2728 VLIB_CLI_COMMAND (snat_det_close_sesion_out_command, static) = {
2729 .path = "snat deterministic close session out",
2730 .short_help = "snat deterministic close session out "
2731 "<out_addr>:<out_port> <ext_addr>:<ext_port>",
2732 .function = snat_det_close_session_out_fn,
2735 static clib_error_t *
2736 snat_det_close_session_in_fn (vlib_main_t *vm,
2737 unformat_input_t * input,
2738 vlib_cli_command_t * cmd)
2740 snat_main_t *sm = &snat_main;
2741 unformat_input_t _line_input, *line_input = &_line_input;
2742 ip4_address_t in_addr, ext_addr;
2743 u16 in_port, ext_port;
2744 snat_det_map_t * dm;
2745 snat_det_session_t * ses;
2746 snat_det_out_key_t key;
2747 clib_error_t *error = 0;
2749 /* Get a line of input. */
2750 if (!unformat_user (input, unformat_line_input, line_input))
2753 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2755 if (unformat (line_input, "%U:%d %U:%d",
2756 unformat_ip4_address, &in_addr, &in_port,
2757 unformat_ip4_address, &ext_addr, &ext_port))
2761 error = clib_error_return (0, "unknown input '%U'",
2762 format_unformat_error, line_input);
2767 unformat_free (line_input);
2769 dm = snat_det_map_by_user (sm, &in_addr);
2771 vlib_cli_output (vm, "no match");
2774 key.ext_host_addr = ext_addr;
2775 key.ext_host_port = ntohs (ext_port);
2776 ses = snat_det_find_ses_by_in (dm, &in_addr, ntohs(in_port), key);
2778 vlib_cli_output (vm, "no match");
2780 snat_det_ses_close(dm, ses);
2784 unformat_free(line_input);
2791 * @cliexstart{snat deterministic close_session_in}
2792 * Close session using inside ip address and port
2793 * and external ip address and port, use:
2794 * vpp# snat deterministic close session in 3.3.3.3:3487 2.2.2.2:2387
2797 VLIB_CLI_COMMAND (snat_det_close_session_in_command, static) = {
2798 .path = "snat deterministic close session in",
2799 .short_help = "snat deterministic close session in "
2800 "<in_addr>:<in_port> <ext_addr>:<ext_port>",
2801 .function = snat_det_close_session_in_fn,