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/plugin/plugin.h>
20 #include <vlibapi/api.h>
21 #include <snat/snat.h>
23 #include <vlibapi/api.h>
24 #include <vlibmemory/api.h>
25 #include <vlibsocket/api.h>
27 snat_main_t snat_main;
29 /* define message IDs */
30 #include <snat/snat_msg_enum.h>
32 /* define message structures */
34 #include <snat/snat_all_api_h.h>
37 /* define generated endian-swappers */
39 #include <snat/snat_all_api_h.h>
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
44 /* Get the API version number */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <snat/snat_all_api_h.h>
49 /* Macro to finish up custom dump fns */
52 vl_print (handle, (char *)s); \
57 * A handy macro to set up a message reply.
58 * Assumes that the following variables are available:
59 * mp - pointer to request message
60 * rmp - pointer to reply message type
64 #define REPLY_MACRO(t) \
66 unix_shared_memory_queue_t * q = \
67 vl_api_client_index_to_input_queue (mp->client_index); \
71 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
72 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
73 rmp->context = mp->context; \
74 rmp->retval = ntohl(rv); \
76 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
79 #define REPLY_MACRO2(t, body) \
81 unix_shared_memory_queue_t * q = \
82 vl_api_client_index_to_input_queue (mp->client_index); \
86 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
87 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
88 rmp->context = mp->context; \
89 rmp->retval = ntohl(rv); \
90 do {body;} while (0); \
91 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
95 /* Hook up input features */
96 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_in2out, static) = {
97 .node_name = "snat-in2out",
98 .runs_before = (char *[]){"snat-out2in", 0},
99 .feature_index = &snat_main.rx_feature_in2out,
101 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_out2in, static) = {
102 .node_name = "snat-out2in",
103 .runs_before = (char *[]){"ip4-lookup", 0},
104 .feature_index = &snat_main.rx_feature_out2in,
106 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
107 .node_name = "snat-in2out-fast",
108 .runs_before = (char *[]){"snat-out2in-fast", 0},
109 .feature_index = &snat_main.rx_feature_in2out_fast,
111 VNET_IP4_UNICAST_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
112 .node_name = "snat-out2in-fast",
113 .runs_before = (char *[]){"ip4-lookup", 0},
114 .feature_index = &snat_main.rx_feature_out2in_fast,
119 * This routine exists to convince the vlib plugin framework that
120 * we haven't accidentally copied a random .dll into the plugin directory.
122 * Also collects global variable pointers passed from the vpp engine
126 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
129 snat_main_t * sm = &snat_main;
130 clib_error_t * error = 0;
133 sm->vnet_main = h->vnet_main;
134 sm->ethernet_main = h->ethernet_main;
139 /*$$$$$ move to an installed header file */
140 #if (1 || CLIB_DEBUG > 0) /* "trust, but verify" */
142 #define VALIDATE_SW_IF_INDEX(mp) \
143 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
144 vnet_main_t *__vnm = vnet_get_main(); \
145 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
147 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
148 goto bad_sw_if_index; \
152 #define BAD_SW_IF_INDEX_LABEL \
158 #define VALIDATE_RX_SW_IF_INDEX(mp) \
159 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
160 vnet_main_t *__vnm = vnet_get_main(); \
161 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
162 __rx_sw_if_index)) { \
163 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
164 goto bad_rx_sw_if_index; \
168 #define BAD_RX_SW_IF_INDEX_LABEL \
170 bad_rx_sw_if_index: \
174 #define VALIDATE_TX_SW_IF_INDEX(mp) \
175 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
176 vnet_main_t *__vnm = vnet_get_main(); \
177 if (pool_is_free_index(__vnm->interface_main.sw_interfaces, \
178 __tx_sw_if_index)) { \
179 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
180 goto bad_tx_sw_if_index; \
184 #define BAD_TX_SW_IF_INDEX_LABEL \
186 bad_tx_sw_if_index: \
192 #define VALIDATE_SW_IF_INDEX(mp)
193 #define BAD_SW_IF_INDEX_LABEL
194 #define VALIDATE_RX_SW_IF_INDEX(mp)
195 #define BAD_RX_SW_IF_INDEX_LABEL
196 #define VALIDATE_TX_SW_IF_INDEX(mp)
197 #define BAD_TX_SW_IF_INDEX_LABEL
199 #endif /* CLIB_DEBUG > 0 */
201 void snat_add_address (snat_main_t *sm, ip4_address_t *addr)
205 vec_add2 (sm->addresses, ap, 1);
210 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
213 snat_static_mapping_t *m;
214 pool_foreach (m, sm->static_mappings,
216 if (m->external_addr.as_u32 == addr.as_u32)
223 int snat_del_address (snat_main_t *sm, ip4_address_t addr)
225 clib_warning("%U", format_ip4_address, &addr);
226 snat_address_t *a = 0;
228 u32 *ses_to_be_removed = 0, *ses_index;
229 clib_bihash_kv_8_8_t kv, value;
230 snat_user_key_t user_key;
235 /* Find SNAT address */
236 for (i=0; i < vec_len (sm->addresses); i++)
238 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
240 a = sm->addresses + i;
245 return VNET_API_ERROR_NO_SUCH_ENTRY;
247 /* Check if address is used in some static mapping */
248 if (is_snat_address_used_in_static_mapping(sm, addr))
250 clib_warning ("address used in static mapping");
251 return VNET_API_ERROR_UNSPECIFIED;
254 /* Delete sessions using address */
257 pool_foreach (ses, sm->sessions, ({
258 if (ses->out2in.addr.as_u32 == addr.as_u32)
260 vec_add1 (ses_to_be_removed, ses - sm->sessions);
261 kv.key = ses->in2out.as_u64;
262 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
263 kv.key = ses->out2in.as_u64;
264 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
265 clib_dlist_remove (sm->list_pool, ses->per_user_index);
266 user_key.addr = ses->in2out.addr;
267 user_key.fib_index = ses->in2out.fib_index;
268 kv.key = user_key.as_u64;
269 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
271 u = pool_elt_at_index (sm->users, value.value);
277 vec_foreach (ses_index, ses_to_be_removed)
278 pool_put_index (sm->sessions, ses_index[0]);
280 vec_free (ses_to_be_removed);
283 vec_del1 (sm->addresses, i);
288 static void increment_v4_address (ip4_address_t * a)
292 v = clib_net_to_host_u32(a->as_u32) + 1;
293 a->as_u32 = clib_host_to_net_u32(v);
297 * @brief Add static mapping.
299 * Create static mapping between local addr+port and external addr+port.
301 * @param l_addr Local IPv4 address.
302 * @param e_addr External IPv4 address.
303 * @param l_port Local port number.
304 * @param e_port External port number.
305 * @param vrf_id VRF ID.
306 * @param addr_only If 0 address port and pair mapping, otherwise address only.
307 * @param is_add If 0 delete static mapping, otherwise add.
311 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
312 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
315 snat_main_t * sm = &snat_main;
316 snat_static_mapping_t *m;
317 snat_static_mapping_key_t m_key;
318 clib_bihash_kv_8_8_t kv, value;
319 snat_address_t *a = 0;
324 /* If outside FIB index is not resolved yet */
325 if (sm->outside_fib_index == ~0)
327 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
329 return VNET_API_ERROR_NO_SUCH_FIB;
330 sm->outside_fib_index = p[0];
334 m_key.port = addr_only ? 0 : e_port;
335 m_key.fib_index = sm->outside_fib_index;
336 kv.key = m_key.as_u64;
337 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
340 m = pool_elt_at_index (sm->static_mappings, value.value);
345 return VNET_API_ERROR_VALUE_EXIST;
347 /* Convert VRF id to FIB index */
350 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
352 return VNET_API_ERROR_NO_SUCH_FIB;
355 /* If not specified use inside VRF id from SNAT plugin startup config */
358 if (sm->inside_fib_index == ~0)
360 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->inside_vrf_id);
362 return VNET_API_ERROR_NO_SUCH_FIB;
364 sm->inside_fib_index = fib_index;
367 fib_index = sm->inside_fib_index;
369 vrf_id = sm->inside_vrf_id;
372 /* Find external address in allocated addresses and reserve port for
373 address and port pair mapping when dynamic translations enabled */
374 if (!addr_only && !(sm->static_mapping_only))
376 for (i = 0; i < vec_len (sm->addresses); i++)
378 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
380 a = sm->addresses + i;
381 /* External port must be unused */
382 if (clib_bitmap_get (a->busy_port_bitmap, e_port))
383 return VNET_API_ERROR_INVALID_VALUE;
384 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
392 /* External address must be allocated */
394 return VNET_API_ERROR_NO_SUCH_ENTRY;
397 pool_get (sm->static_mappings, m);
398 memset (m, 0, sizeof (*m));
399 m->local_addr = l_addr;
400 m->external_addr = e_addr;
401 m->addr_only = addr_only;
403 m->fib_index = fib_index;
406 m->local_port = l_port;
407 m->external_port = e_port;
410 m_key.addr = m->local_addr;
411 m_key.port = m->local_port;
412 m_key.fib_index = m->fib_index;
413 kv.key = m_key.as_u64;
414 kv.value = m - sm->static_mappings;
415 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 1);
417 m_key.addr = m->external_addr;
418 m_key.port = m->external_port;
419 m_key.fib_index = sm->outside_fib_index;
420 kv.key = m_key.as_u64;
421 kv.value = m - sm->static_mappings;
422 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
427 return VNET_API_ERROR_NO_SUCH_ENTRY;
429 /* Free external address port */
430 if (!addr_only && !(sm->static_mapping_only))
432 for (i = 0; i < vec_len (sm->addresses); i++)
434 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
436 a = sm->addresses + i;
437 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
446 m_key.addr = m->local_addr;
447 m_key.port = m->local_port;
448 m_key.fib_index = m->fib_index;
449 kv.key = m_key.as_u64;
450 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
452 m_key.addr = m->external_addr;
453 m_key.port = m->external_port;
454 m_key.fib_index = sm->outside_fib_index;
455 kv.key = m_key.as_u64;
456 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
458 /* Delete session(s) for static mapping if exist */
459 if (!(sm->static_mapping_only) ||
460 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
462 snat_user_key_t u_key;
464 dlist_elt_t * head, * elt;
465 u32 elt_index, head_index;
469 u_key.addr = m->local_addr;
470 u_key.fib_index = m->fib_index;
471 kv.key = u_key.as_u64;
472 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
474 u = pool_elt_at_index (sm->users, value.value);
475 if (u->nstaticsessions)
477 head_index = u->sessions_per_user_list_head_index;
478 head = pool_elt_at_index (sm->list_pool, head_index);
479 elt_index = head->next;
480 elt = pool_elt_at_index (sm->list_pool, elt_index);
481 ses_index = elt->value;
482 while (ses_index != ~0)
484 s = pool_elt_at_index (sm->sessions, ses_index);
488 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
489 (clib_net_to_host_u16 (s->out2in.port) != e_port))
492 value.key = s->in2out.as_u64;
493 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
494 value.key = s->out2in.as_u64;
495 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
496 pool_put (sm->sessions, s);
501 elt_index = elt->next;
502 elt = pool_elt_at_index (sm->list_pool, elt_index);
503 ses_index = elt->value;
507 while ((elt_index = clib_dlist_remove_head(sm->list_pool, head_index)) != ~0)
508 pool_put_index (sm->list_pool, elt_index);
509 pool_put (sm->users, u);
510 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
516 clib_dlist_remove (sm->list_pool, elt_index);
517 pool_put (sm->list_pool, elt);
518 u->nstaticsessions--;
525 /* Delete static mapping from pool */
526 pool_put (sm->static_mappings, m);
533 vl_api_snat_add_address_range_t_handler
534 (vl_api_snat_add_address_range_t * mp)
536 snat_main_t * sm = &snat_main;
537 vl_api_snat_add_address_range_reply_t * rmp;
538 ip4_address_t this_addr;
539 u32 start_host_order, end_host_order;
546 rv = VNET_API_ERROR_UNIMPLEMENTED;
550 if (sm->static_mapping_only)
552 rv = VNET_API_ERROR_FEATURE_DISABLED;
556 tmp = (u32 *) mp->first_ip_address;
557 start_host_order = clib_host_to_net_u32 (tmp[0]);
558 tmp = (u32 *) mp->last_ip_address;
559 end_host_order = clib_host_to_net_u32 (tmp[0]);
561 count = (end_host_order - start_host_order) + 1;
564 clib_warning ("%U - %U, %d addresses...",
565 format_ip4_address, mp->first_ip_address,
566 format_ip4_address, mp->last_ip_address,
569 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
571 for (i = 0; i < count; i++)
574 snat_add_address (sm, &this_addr);
576 rv = snat_del_address (sm, this_addr);
581 increment_v4_address (&this_addr);
585 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
588 static void *vl_api_snat_add_address_range_t_print
589 (vl_api_snat_add_address_range_t *mp, void * handle)
593 s = format (0, "SCRIPT: snat_add_address_range ");
594 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
595 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
597 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
603 send_snat_address_details
604 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
606 vl_api_snat_address_details_t *rmp;
607 snat_main_t * sm = &snat_main;
609 rmp = vl_msg_api_alloc (sizeof (*rmp));
610 memset (rmp, 0, sizeof (*rmp));
611 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
613 clib_memcpy (rmp->ip_address, &(a->addr), 4);
614 rmp->context = context;
616 vl_msg_api_send_shmem (q, (u8 *) & rmp);
620 vl_api_snat_address_dump_t_handler
621 (vl_api_snat_address_dump_t * mp)
623 unix_shared_memory_queue_t *q;
624 snat_main_t * sm = &snat_main;
627 q = vl_api_client_index_to_input_queue (mp->client_index);
631 vec_foreach (a, sm->addresses)
632 send_snat_address_details (a, q, mp->context);
635 static void *vl_api_snat_address_dump_t_print
636 (vl_api_snat_address_dump_t *mp, void * handle)
640 s = format (0, "SCRIPT: snat_address_dump ");
646 vl_api_snat_interface_add_del_feature_t_handler
647 (vl_api_snat_interface_add_del_feature_t * mp)
649 snat_main_t * sm = &snat_main;
650 vl_api_snat_interface_add_del_feature_reply_t * rmp;
651 u8 is_del = mp->is_add == 0;
652 u32 sw_if_index = ntohl(mp->sw_if_index);
654 ip4_main_t * im = &ip4_main;
655 ip_lookup_main_t * lm = &im->lookup_main;
656 ip_config_main_t * rx_cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
660 VALIDATE_SW_IF_INDEX(mp);
662 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
663 feature_index = mp->is_inside ? sm->rx_feature_in2out_fast
664 : sm->rx_feature_out2in_fast;
666 feature_index = mp->is_inside ? sm->rx_feature_in2out
667 : sm->rx_feature_out2in;
669 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
671 ? vnet_config_del_feature
672 : vnet_config_add_feature)
673 (sm->vlib_main, &rx_cm->config_main,
676 0 /* config struct */,
677 0 /* sizeof config struct*/);
678 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
680 BAD_SW_IF_INDEX_LABEL;
682 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
685 static void *vl_api_snat_interface_add_del_feature_t_print
686 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
690 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
691 s = format (s, "sw_if_index %d %s %s",
692 clib_host_to_net_u32(mp->sw_if_index),
693 mp->is_inside ? "in":"out",
694 mp->is_add ? "" : "del");
700 vl_api_snat_add_static_mapping_t_handler
701 (vl_api_snat_add_static_mapping_t * mp)
703 snat_main_t * sm = &snat_main;
704 vl_api_snat_add_static_mapping_reply_t * rmp;
705 ip4_address_t local_addr, external_addr;
706 u16 local_port = 0, external_port = 0;
712 rv = VNET_API_ERROR_UNIMPLEMENTED;
716 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
717 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
718 if (mp->addr_only == 0)
720 local_port = clib_net_to_host_u16 (mp->local_port);
721 external_port = clib_net_to_host_u16 (mp->external_port);
723 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
725 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
726 external_port, vrf_id, mp->addr_only,
730 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
733 static void *vl_api_snat_add_static_mapping_t_print
734 (vl_api_snat_add_static_mapping_t *mp, void * handle)
738 s = format (0, "SCRIPT: snat_add_static_mapping ");
739 s = format (s, "local_addr %U external_addr %U ",
740 format_ip4_address, mp->local_ip_address,
741 format_ip4_address, mp->external_ip_address);
743 if (mp->addr_only == 0)
744 s = format (s, "local_port %d external_port %d ",
745 clib_net_to_host_u16 (mp->local_port),
746 clib_net_to_host_u16 (mp->external_port));
748 if (mp->vrf_id != ~0)
749 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
755 send_snat_static_mapping_details
756 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
758 vl_api_snat_static_mapping_details_t *rmp;
759 snat_main_t * sm = &snat_main;
761 rmp = vl_msg_api_alloc (sizeof (*rmp));
762 memset (rmp, 0, sizeof (*rmp));
763 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
765 rmp->addr_only = m->addr_only;
766 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
767 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
768 rmp->local_port = htons (m->local_port);
769 rmp->external_port = htons (m->external_port);
770 rmp->vrf_id = htonl (m->vrf_id);
771 rmp->context = context;
773 vl_msg_api_send_shmem (q, (u8 *) & rmp);
777 vl_api_snat_static_mapping_dump_t_handler
778 (vl_api_snat_static_mapping_dump_t * mp)
780 unix_shared_memory_queue_t *q;
781 snat_main_t * sm = &snat_main;
782 snat_static_mapping_t * m;
784 q = vl_api_client_index_to_input_queue (mp->client_index);
788 pool_foreach (m, sm->static_mappings,
790 send_snat_static_mapping_details (m, q, mp->context);
794 static void *vl_api_snat_static_mapping_dump_t_print
795 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
799 s = format (0, "SCRIPT: snat_static_mapping_dump ");
805 vl_api_snat_control_ping_t_handler
806 (vl_api_snat_control_ping_t * mp)
808 vl_api_snat_control_ping_reply_t *rmp;
809 snat_main_t * sm = &snat_main;
812 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
814 rmp->vpe_pid = ntohl (getpid());
818 static void *vl_api_snat_control_ping_t_print
819 (vl_api_snat_control_ping_t *mp, void * handle)
823 s = format (0, "SCRIPT: snat_control_ping ");
829 vl_api_snat_show_config_t_handler
830 (vl_api_snat_show_config_t * mp)
832 vl_api_snat_show_config_reply_t *rmp;
833 snat_main_t * sm = &snat_main;
836 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
838 rmp->translation_buckets = htons (sm->translation_buckets);
839 rmp->translation_memory_size = htons (sm->translation_memory_size);
840 rmp->user_buckets = htons (sm->user_buckets);
841 rmp->user_memory_size = htons (sm->user_memory_size);
842 rmp->max_translations_per_user = htons (sm->max_translations_per_user);
843 rmp->outside_vrf_id = htons (sm->outside_vrf_id);
844 rmp->inside_vrf_id = htons (sm->inside_vrf_id);
845 rmp->static_mapping_only = sm->static_mapping_only;
846 rmp->static_mapping_connection_tracking =
847 sm->static_mapping_connection_tracking;
851 static void *vl_api_snat_show_config_t_print
852 (vl_api_snat_show_config_t *mp, void * handle)
856 s = format (0, "SCRIPT: snat_show_config ");
861 /* List of message types that this plugin understands */
862 #define foreach_snat_plugin_api_msg \
863 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
864 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
865 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
866 _(SNAT_CONTROL_PING, snat_control_ping) \
867 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
868 _(SNAT_SHOW_CONFIG, snat_show_config) \
869 _(SNAT_ADDRESS_DUMP, snat_address_dump)
871 /* Set up the API message handling tables */
872 static clib_error_t *
873 snat_plugin_api_hookup (vlib_main_t *vm)
875 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
877 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
879 vl_api_##n##_t_handler, \
881 vl_api_##n##_t_endian, \
882 vl_api_##n##_t_print, \
883 sizeof(vl_api_##n##_t), 1);
884 foreach_snat_plugin_api_msg;
890 static void plugin_custom_dump_configure (snat_main_t * sm)
892 #define _(n,f) sm->api_main->msg_print_handlers \
893 [VL_API_##n + sm->msg_id_base] \
894 = (void *) vl_api_##f##_t_print;
895 foreach_snat_plugin_api_msg;
899 static clib_error_t * snat_init (vlib_main_t * vm)
901 snat_main_t * sm = &snat_main;
902 clib_error_t * error = 0;
903 ip4_main_t * im = &ip4_main;
904 ip_lookup_main_t * lm = &im->lookup_main;
907 name = format (0, "snat_%08x%c", api_version, 0);
909 /* Ask for a correctly-sized block of API message decode slots */
910 sm->msg_id_base = vl_msg_api_get_msg_ids
911 ((char *) name, VL_MSG_FIRST_AVAILABLE);
914 sm->vnet_main = vnet_get_main();
916 sm->ip4_lookup_main = lm;
917 sm->api_main = &api_main;
919 error = snat_plugin_api_hookup (vm);
920 plugin_custom_dump_configure (sm);
926 VLIB_INIT_FUNCTION (snat_init);
928 void snat_free_outside_address_and_port (snat_main_t * sm,
929 snat_session_key_t * k,
933 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
935 ASSERT (address_index < vec_len (sm->addresses));
937 a = sm->addresses + address_index;
939 ASSERT (clib_bitmap_get (a->busy_port_bitmap, port_host_byte_order) == 1);
941 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
942 port_host_byte_order, 0);
947 * @brief Match SNAT static mapping.
949 * @param sm SNAT main.
950 * @param match Address and port to match.
951 * @param mapping External or local address and port of the matched mapping.
952 * @param by_external If 0 match by local address otherwise match by external
955 * @returns 0 if match found otherwise 1.
957 int snat_static_mapping_match (snat_main_t * sm,
958 snat_session_key_t match,
959 snat_session_key_t * mapping,
962 clib_bihash_kv_8_8_t kv, value;
963 snat_static_mapping_t *m;
964 snat_static_mapping_key_t m_key;
965 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
968 mapping_hash = &sm->static_mapping_by_external;
970 m_key.addr = match.addr;
971 m_key.port = clib_net_to_host_u16 (match.port);
972 m_key.fib_index = match.fib_index;
974 kv.key = m_key.as_u64;
976 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
978 /* Try address only mapping */
980 kv.key = m_key.as_u64;
981 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
985 m = pool_elt_at_index (sm->static_mappings, value.value);
989 mapping->addr = m->local_addr;
990 /* Address only mapping doesn't change port */
991 mapping->port = m->addr_only ? match.port
992 : clib_host_to_net_u16 (m->local_port);
993 mapping->fib_index = m->fib_index;
997 mapping->addr = m->external_addr;
998 /* Address only mapping doesn't change port */
999 mapping->port = m->addr_only ? match.port
1000 : clib_host_to_net_u16 (m->external_port);
1001 mapping->fib_index = sm->outside_fib_index;
1007 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1008 snat_session_key_t * k,
1009 u32 * address_indexp)
1015 for (i = 0; i < vec_len (sm->addresses); i++)
1017 if (sm->addresses[i].busy_ports < (65535-1024))
1019 a = sm->addresses + i;
1023 portnum = random_u32 (&sm->random_seed);
1027 if (clib_bitmap_get (a->busy_port_bitmap, portnum))
1029 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
1032 /* Caller sets protocol and fib index */
1034 k->port = clib_host_to_net_u16(portnum);
1035 *address_indexp = i;
1040 /* Totally out of translations to use... */
1045 static clib_error_t *
1046 add_address_command_fn (vlib_main_t * vm,
1047 unformat_input_t * input,
1048 vlib_cli_command_t * cmd)
1050 unformat_input_t _line_input, *line_input = &_line_input;
1051 snat_main_t * sm = &snat_main;
1052 ip4_address_t start_addr, end_addr, this_addr;
1053 u32 start_host_order, end_host_order;
1058 /* Get a line of input. */
1059 if (!unformat_user (input, unformat_line_input, line_input))
1062 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1064 if (unformat (line_input, "%U - %U",
1065 unformat_ip4_address, &start_addr,
1066 unformat_ip4_address, &end_addr))
1068 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1069 end_addr = start_addr;
1070 else if (unformat (line_input, "del"))
1073 return clib_error_return (0, "unknown input '%U'",
1074 format_unformat_error, input);
1076 unformat_free (line_input);
1078 if (sm->static_mapping_only)
1079 return clib_error_return (0, "static mapping only mode");
1081 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1082 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1084 if (end_host_order < start_host_order)
1085 return clib_error_return (0, "end address less than start address");
1087 count = (end_host_order - start_host_order) + 1;
1090 clib_warning ("%U - %U, %d addresses...",
1091 format_ip4_address, &start_addr,
1092 format_ip4_address, &end_addr,
1095 this_addr = start_addr;
1097 for (i = 0; i < count; i++)
1100 snat_add_address (sm, &this_addr);
1102 rv = snat_del_address (sm, this_addr);
1106 case VNET_API_ERROR_NO_SUCH_ENTRY:
1107 return clib_error_return (0, "S-NAT address not exist.");
1109 case VNET_API_ERROR_UNSPECIFIED:
1110 return clib_error_return (0, "S-NAT address used in static mapping.");
1116 increment_v4_address (&this_addr);
1122 VLIB_CLI_COMMAND (add_address_command, static) = {
1123 .path = "snat add address",
1124 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1125 .function = add_address_command_fn,
1128 static clib_error_t *
1129 snat_feature_command_fn (vlib_main_t * vm,
1130 unformat_input_t * input,
1131 vlib_cli_command_t * cmd)
1133 unformat_input_t _line_input, *line_input = &_line_input;
1134 vnet_main_t * vnm = vnet_get_main();
1135 snat_main_t * sm = &snat_main;
1136 ip4_main_t * im = &ip4_main;
1137 ip_lookup_main_t * lm = &im->lookup_main;
1138 ip_config_main_t * rx_cm = &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
1139 clib_error_t * error = 0;
1140 u32 sw_if_index, ci;
1142 u32 * inside_sw_if_indices = 0;
1143 u32 * outside_sw_if_indices = 0;
1149 /* Get a line of input. */
1150 if (!unformat_user (input, unformat_line_input, line_input))
1153 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1155 if (unformat (line_input, "in %U", unformat_vnet_sw_interface,
1157 vec_add1 (inside_sw_if_indices, sw_if_index);
1158 else if (unformat (line_input, "out %U", unformat_vnet_sw_interface,
1160 vec_add1 (outside_sw_if_indices, sw_if_index);
1161 else if (unformat (line_input, "del"))
1164 return clib_error_return (0, "unknown input '%U'",
1165 format_unformat_error, input);
1167 unformat_free (line_input);
1169 if (vec_len (inside_sw_if_indices))
1171 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1172 feature_index = sm->rx_feature_in2out_fast;
1174 feature_index = sm->rx_feature_in2out;
1176 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1178 sw_if_index = inside_sw_if_indices[i];
1179 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
1181 ? vnet_config_del_feature
1182 : vnet_config_add_feature)
1183 (vm, &rx_cm->config_main,
1186 0 /* config struct */,
1187 0 /* sizeof config struct*/);
1188 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
1192 if (vec_len (outside_sw_if_indices))
1194 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1195 feature_index = sm->rx_feature_out2in_fast;
1197 feature_index = sm->rx_feature_out2in;
1199 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1201 sw_if_index = outside_sw_if_indices[i];
1202 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
1204 ? vnet_config_del_feature
1205 : vnet_config_add_feature)
1206 (vm, &rx_cm->config_main,
1209 0 /* config struct */,
1210 0 /* sizeof config struct*/);
1211 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
1215 vec_free (inside_sw_if_indices);
1216 vec_free (outside_sw_if_indices);
1221 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1222 .path = "set interface snat",
1223 .function = snat_feature_command_fn,
1224 .short_help = "set interface snat in <intfc> out <intfc> [del]",
1227 static clib_error_t *
1228 add_static_mapping_command_fn (vlib_main_t * vm,
1229 unformat_input_t * input,
1230 vlib_cli_command_t * cmd)
1232 unformat_input_t _line_input, *line_input = &_line_input;
1233 clib_error_t * error = 0;
1234 ip4_address_t l_addr, e_addr;
1235 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1240 /* Get a line of input. */
1241 if (!unformat_user (input, unformat_line_input, line_input))
1244 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1246 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1249 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1251 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1254 else if (unformat (line_input, "external %U", unformat_ip4_address,
1257 else if (unformat (line_input, "vrf %u", &vrf_id))
1259 else if (unformat (line_input, "del"))
1262 return clib_error_return (0, "unknown input: '%U'",
1263 format_unformat_error, line_input);
1265 unformat_free (line_input);
1267 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1268 vrf_id, addr_only, is_add);
1272 case VNET_API_ERROR_INVALID_VALUE:
1273 return clib_error_return (0, "External port already in use.");
1275 case VNET_API_ERROR_NO_SUCH_ENTRY:
1277 return clib_error_return (0, "External addres must be allocated.");
1279 return clib_error_return (0, "Mapping not exist.");
1281 case VNET_API_ERROR_NO_SUCH_FIB:
1282 return clib_error_return (0, "No such VRF id.");
1283 case VNET_API_ERROR_VALUE_EXIST:
1284 return clib_error_return (0, "Mapping already exist.");
1294 * @cliexstart{snat add static mapping}
1295 * Static mapping allows hosts on the external network to initiate connection
1296 * to to the local network host.
1297 * To create static mapping between local host address 10.0.0.3 port 6303 and
1298 * external address 4.4.4.4 port 3606 use:
1299 * vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1300 * If not runnig "static mapping only" S-NAT plugin mode use before:
1301 * vpp# snat add address 4.4.4.4
1302 * To create static mapping between local and external address use:
1303 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1306 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1307 .path = "snat add static mapping",
1308 .function = add_static_mapping_command_fn,
1310 "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1313 static clib_error_t *
1314 snat_config (vlib_main_t * vm, unformat_input_t * input)
1316 snat_main_t * sm = &snat_main;
1317 u32 translation_buckets = 1024;
1318 u32 translation_memory_size = 128<<20;
1319 u32 user_buckets = 128;
1320 u32 user_memory_size = 64<<20;
1321 u32 max_translations_per_user = 100;
1322 u32 outside_vrf_id = 0;
1323 u32 inside_vrf_id = 0;
1324 u32 static_mapping_buckets = 1024;
1325 u32 static_mapping_memory_size = 64<<20;
1326 u8 static_mapping_only = 0;
1327 u8 static_mapping_connection_tracking = 0;
1329 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1331 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1333 else if (unformat (input, "translation hash memory %d",
1334 &translation_memory_size));
1335 else if (unformat (input, "user hash buckets %d", &user_buckets))
1337 else if (unformat (input, "user hash memory %d",
1340 else if (unformat (input, "max translations per user %d",
1341 &max_translations_per_user))
1343 else if (unformat (input, "outside VRF id %d",
1346 else if (unformat (input, "inside VRF id %d",
1349 else if (unformat (input, "static mapping only"))
1351 static_mapping_only = 1;
1352 if (unformat (input, "connection tracking"))
1353 static_mapping_connection_tracking = 1;
1356 return clib_error_return (0, "unknown input '%U'",
1357 format_unformat_error, input);
1360 /* for show commands, etc. */
1361 sm->translation_buckets = translation_buckets;
1362 sm->translation_memory_size = translation_memory_size;
1363 sm->user_buckets = user_buckets;
1364 sm->user_memory_size = user_memory_size;
1365 sm->max_translations_per_user = max_translations_per_user;
1366 sm->outside_vrf_id = outside_vrf_id;
1367 sm->outside_fib_index = ~0;
1368 sm->inside_vrf_id = inside_vrf_id;
1369 sm->inside_fib_index = ~0;
1370 sm->static_mapping_only = static_mapping_only;
1371 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1373 if (!static_mapping_only ||
1374 (static_mapping_only && static_mapping_connection_tracking))
1376 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1377 translation_memory_size);
1379 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1380 translation_memory_size);
1382 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1385 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1386 "static_mapping_by_local", static_mapping_buckets,
1387 static_mapping_memory_size);
1389 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1390 "static_mapping_by_external", static_mapping_buckets,
1391 static_mapping_memory_size);
1395 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1397 u8 * format_snat_key (u8 * s, va_list * args)
1399 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1400 char * protocol_string = "unknown";
1401 static char *protocol_strings[] = {
1407 if (key->protocol < ARRAY_LEN(protocol_strings))
1408 protocol_string = protocol_strings[key->protocol];
1410 s = format (s, "%U proto %s port %d fib %d",
1411 format_ip4_address, &key->addr, protocol_string,
1412 clib_net_to_host_u16 (key->port), key->fib_index);
1416 u8 * format_snat_session (u8 * s, va_list * args)
1418 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1419 snat_session_t * sess = va_arg (*args, snat_session_t *);
1421 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1422 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1423 s = format (s, " last heard %.2f\n", sess->last_heard);
1424 s = format (s, " total pkts %d, total bytes %lld\n",
1425 sess->total_pkts, sess->total_bytes);
1426 if (snat_is_session_static (sess))
1427 s = format (s, " static translation\n");
1429 s = format (s, " dynamic translation\n");
1434 u8 * format_snat_user (u8 * s, va_list * args)
1436 snat_main_t * sm = va_arg (*args, snat_main_t *);
1437 snat_user_t * u = va_arg (*args, snat_user_t *);
1438 int verbose = va_arg (*args, int);
1439 dlist_elt_t * head, * elt;
1440 u32 elt_index, head_index;
1442 snat_session_t * sess;
1444 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1445 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1450 if (u->nsessions || u->nstaticsessions)
1452 head_index = u->sessions_per_user_list_head_index;
1453 head = pool_elt_at_index (sm->list_pool, head_index);
1455 elt_index = head->next;
1456 elt = pool_elt_at_index (sm->list_pool, elt_index);
1457 session_index = elt->value;
1459 while (session_index != ~0)
1461 sess = pool_elt_at_index (sm->sessions, session_index);
1463 s = format (s, " %U\n", format_snat_session, sm, sess);
1465 elt_index = elt->next;
1466 elt = pool_elt_at_index (sm->list_pool, elt_index);
1467 session_index = elt->value;
1474 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1476 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1479 s = format (s, "local %U external %U vrf %d",
1480 format_ip4_address, &m->local_addr,
1481 format_ip4_address, &m->external_addr,
1484 s = format (s, "local %U:%d external %U:%d vrf %d",
1485 format_ip4_address, &m->local_addr, m->local_port,
1486 format_ip4_address, &m->external_addr, m->external_port,
1492 static clib_error_t *
1493 show_snat_command_fn (vlib_main_t * vm,
1494 unformat_input_t * input,
1495 vlib_cli_command_t * cmd)
1498 snat_main_t * sm = &snat_main;
1500 snat_static_mapping_t *m;
1502 if (unformat (input, "detail"))
1504 else if (unformat (input, "verbose"))
1507 if (sm->static_mapping_only)
1509 if (sm->static_mapping_connection_tracking)
1510 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
1513 vlib_cli_output (vm, "SNAT mode: static mapping only");
1517 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
1520 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1522 vlib_cli_output (vm, "%d static mappings",
1523 pool_elts (sm->static_mappings));
1527 pool_foreach (m, sm->static_mappings,
1529 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1535 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
1536 " %d static mappings",
1537 pool_elts (sm->users),
1538 vec_len (sm->addresses),
1539 pool_elts (sm->sessions),
1540 pool_elts (sm->static_mappings));
1544 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
1546 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
1548 vlib_cli_output (vm, "%d list pool elements",
1549 pool_elts (sm->list_pool));
1551 pool_foreach (u, sm->users,
1553 vlib_cli_output (vm, "%U", format_snat_user, sm, u, verbose - 1);
1556 if (pool_elts (sm->static_mappings))
1558 vlib_cli_output (vm, "static mappings:");
1559 pool_foreach (m, sm->static_mappings,
1561 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1570 VLIB_CLI_COMMAND (show_snat_command, static) = {
1571 .path = "show snat",
1572 .short_help = "show snat",
1573 .function = show_snat_command_fn,