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_FEATURE_INIT (ip4_snat_in2out, static) = {
97 .arc_name = "ip4-unicast",
98 .node_name = "snat-in2out",
99 .runs_before = VNET_FEATURES ("snat-out2in"),
101 VNET_FEATURE_INIT (ip4_snat_out2in, static) = {
102 .arc_name = "ip4-unicast",
103 .node_name = "snat-out2in",
104 .runs_before = VNET_FEATURES ("ip4-lookup"),
106 VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = {
107 .arc_name = "ip4-unicast",
108 .node_name = "snat-in2out-fast",
109 .runs_before = VNET_FEATURES ("snat-out2in-fast"),
111 VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = {
112 .arc_name = "ip4-unicast",
113 .node_name = "snat-out2in-fast",
114 .runs_before = VNET_FEATURES ("ip4-lookup"),
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 /* Check if address already exists */
206 vec_foreach (ap, sm->addresses)
208 if (ap->addr.as_u32 == addr->as_u32)
212 vec_add2 (sm->addresses, ap, 1);
217 static int is_snat_address_used_in_static_mapping (snat_main_t *sm,
220 snat_static_mapping_t *m;
221 pool_foreach (m, sm->static_mappings,
223 if (m->external_addr.as_u32 == addr.as_u32)
230 int snat_del_address (snat_main_t *sm, ip4_address_t addr)
232 clib_warning("%U", format_ip4_address, &addr);
233 snat_address_t *a = 0;
235 u32 *ses_to_be_removed = 0, *ses_index;
236 clib_bihash_kv_8_8_t kv, value;
237 snat_user_key_t user_key;
242 /* Find SNAT address */
243 for (i=0; i < vec_len (sm->addresses); i++)
245 if (sm->addresses[i].addr.as_u32 == addr.as_u32)
247 a = sm->addresses + i;
252 return VNET_API_ERROR_NO_SUCH_ENTRY;
254 /* Check if address is used in some static mapping */
255 if (is_snat_address_used_in_static_mapping(sm, addr))
257 clib_warning ("address used in static mapping");
258 return VNET_API_ERROR_UNSPECIFIED;
261 /* Delete sessions using address */
264 pool_foreach (ses, sm->sessions, ({
265 if (ses->out2in.addr.as_u32 == addr.as_u32)
267 vec_add1 (ses_to_be_removed, ses - sm->sessions);
268 kv.key = ses->in2out.as_u64;
269 clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0);
270 kv.key = ses->out2in.as_u64;
271 clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0);
272 clib_dlist_remove (sm->list_pool, ses->per_user_index);
273 user_key.addr = ses->in2out.addr;
274 user_key.fib_index = ses->in2out.fib_index;
275 kv.key = user_key.as_u64;
276 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
278 u = pool_elt_at_index (sm->users, value.value);
284 vec_foreach (ses_index, ses_to_be_removed)
285 pool_put_index (sm->sessions, ses_index[0]);
287 vec_free (ses_to_be_removed);
290 vec_del1 (sm->addresses, i);
295 static void increment_v4_address (ip4_address_t * a)
299 v = clib_net_to_host_u32(a->as_u32) + 1;
300 a->as_u32 = clib_host_to_net_u32(v);
304 * @brief Add static mapping.
306 * Create static mapping between local addr+port and external addr+port.
308 * @param l_addr Local IPv4 address.
309 * @param e_addr External IPv4 address.
310 * @param l_port Local port number.
311 * @param e_port External port number.
312 * @param vrf_id VRF ID.
313 * @param addr_only If 0 address port and pair mapping, otherwise address only.
314 * @param is_add If 0 delete static mapping, otherwise add.
318 int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
319 u16 l_port, u16 e_port, u32 vrf_id, int addr_only,
322 snat_main_t * sm = &snat_main;
323 snat_static_mapping_t *m;
324 snat_static_mapping_key_t m_key;
325 clib_bihash_kv_8_8_t kv, value;
326 snat_address_t *a = 0;
331 /* If outside FIB index is not resolved yet */
332 if (sm->outside_fib_index == ~0)
334 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
336 return VNET_API_ERROR_NO_SUCH_FIB;
337 sm->outside_fib_index = p[0];
341 m_key.port = addr_only ? 0 : e_port;
342 m_key.fib_index = sm->outside_fib_index;
343 kv.key = m_key.as_u64;
344 if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value))
347 m = pool_elt_at_index (sm->static_mappings, value.value);
352 return VNET_API_ERROR_VALUE_EXIST;
354 /* Convert VRF id to FIB index */
357 p = hash_get (sm->ip4_main->fib_index_by_table_id, vrf_id);
359 return VNET_API_ERROR_NO_SUCH_FIB;
362 /* If not specified use inside VRF id from SNAT plugin startup config */
365 if (sm->inside_fib_index == ~0)
367 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->inside_vrf_id);
369 return VNET_API_ERROR_NO_SUCH_FIB;
371 sm->inside_fib_index = fib_index;
374 fib_index = sm->inside_fib_index;
376 vrf_id = sm->inside_vrf_id;
379 /* Find external address in allocated addresses and reserve port for
380 address and port pair mapping when dynamic translations enabled */
381 if (!addr_only && !(sm->static_mapping_only))
383 for (i = 0; i < vec_len (sm->addresses); i++)
385 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
387 a = sm->addresses + i;
388 /* External port must be unused */
389 if (clib_bitmap_get (a->busy_port_bitmap, e_port))
390 return VNET_API_ERROR_INVALID_VALUE;
391 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
399 /* External address must be allocated */
401 return VNET_API_ERROR_NO_SUCH_ENTRY;
404 pool_get (sm->static_mappings, m);
405 memset (m, 0, sizeof (*m));
406 m->local_addr = l_addr;
407 m->external_addr = e_addr;
408 m->addr_only = addr_only;
410 m->fib_index = fib_index;
413 m->local_port = l_port;
414 m->external_port = e_port;
417 m_key.addr = m->local_addr;
418 m_key.port = m->local_port;
419 m_key.fib_index = m->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_local, &kv, 1);
424 m_key.addr = m->external_addr;
425 m_key.port = m->external_port;
426 m_key.fib_index = sm->outside_fib_index;
427 kv.key = m_key.as_u64;
428 kv.value = m - sm->static_mappings;
429 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 1);
434 return VNET_API_ERROR_NO_SUCH_ENTRY;
436 /* Free external address port */
437 if (!addr_only && !(sm->static_mapping_only))
439 for (i = 0; i < vec_len (sm->addresses); i++)
441 if (sm->addresses[i].addr.as_u32 == e_addr.as_u32)
443 a = sm->addresses + i;
444 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
453 m_key.addr = m->local_addr;
454 m_key.port = m->local_port;
455 m_key.fib_index = m->fib_index;
456 kv.key = m_key.as_u64;
457 clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0);
459 m_key.addr = m->external_addr;
460 m_key.port = m->external_port;
461 m_key.fib_index = sm->outside_fib_index;
462 kv.key = m_key.as_u64;
463 clib_bihash_add_del_8_8(&sm->static_mapping_by_external, &kv, 0);
465 /* Delete session(s) for static mapping if exist */
466 if (!(sm->static_mapping_only) ||
467 (sm->static_mapping_only && sm->static_mapping_connection_tracking))
469 snat_user_key_t u_key;
471 dlist_elt_t * head, * elt;
472 u32 elt_index, head_index;
476 u_key.addr = m->local_addr;
477 u_key.fib_index = m->fib_index;
478 kv.key = u_key.as_u64;
479 if (!clib_bihash_search_8_8 (&sm->user_hash, &kv, &value))
481 u = pool_elt_at_index (sm->users, value.value);
482 if (u->nstaticsessions)
484 head_index = u->sessions_per_user_list_head_index;
485 head = pool_elt_at_index (sm->list_pool, head_index);
486 elt_index = head->next;
487 elt = pool_elt_at_index (sm->list_pool, elt_index);
488 ses_index = elt->value;
489 while (ses_index != ~0)
491 s = pool_elt_at_index (sm->sessions, ses_index);
495 if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
496 (clib_net_to_host_u16 (s->out2in.port) != e_port))
499 value.key = s->in2out.as_u64;
500 clib_bihash_add_del_8_8 (&sm->in2out, &value, 0);
501 value.key = s->out2in.as_u64;
502 clib_bihash_add_del_8_8 (&sm->out2in, &value, 0);
503 pool_put (sm->sessions, s);
508 elt_index = elt->next;
509 elt = pool_elt_at_index (sm->list_pool, elt_index);
510 ses_index = elt->value;
514 while ((elt_index = clib_dlist_remove_head(sm->list_pool, head_index)) != ~0)
515 pool_put_index (sm->list_pool, elt_index);
516 pool_put (sm->users, u);
517 clib_bihash_add_del_8_8 (&sm->user_hash, &kv, 0);
523 clib_dlist_remove (sm->list_pool, elt_index);
524 pool_put (sm->list_pool, elt);
525 u->nstaticsessions--;
532 /* Delete static mapping from pool */
533 pool_put (sm->static_mappings, m);
539 static int snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del)
541 snat_main_t *sm = &snat_main;
543 const char * feature_name;
545 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
546 feature_name = is_inside ? "snat-in2out-fast" : "snat-out2in-fast";
548 feature_name = is_inside ? "snat-in2out" : "snat-out2in";
550 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
553 pool_foreach (i, sm->interfaces,
555 if (i->sw_if_index == sw_if_index)
558 pool_put (sm->interfaces, i);
560 return VNET_API_ERROR_VALUE_EXIST;
567 return VNET_API_ERROR_NO_SUCH_ENTRY;
569 pool_get (sm->interfaces, i);
570 i->sw_if_index = sw_if_index;
571 i->is_inside = is_inside;
577 vl_api_snat_add_address_range_t_handler
578 (vl_api_snat_add_address_range_t * mp)
580 snat_main_t * sm = &snat_main;
581 vl_api_snat_add_address_range_reply_t * rmp;
582 ip4_address_t this_addr;
583 u32 start_host_order, end_host_order;
590 rv = VNET_API_ERROR_UNIMPLEMENTED;
594 if (sm->static_mapping_only)
596 rv = VNET_API_ERROR_FEATURE_DISABLED;
600 tmp = (u32 *) mp->first_ip_address;
601 start_host_order = clib_host_to_net_u32 (tmp[0]);
602 tmp = (u32 *) mp->last_ip_address;
603 end_host_order = clib_host_to_net_u32 (tmp[0]);
605 count = (end_host_order - start_host_order) + 1;
608 clib_warning ("%U - %U, %d addresses...",
609 format_ip4_address, mp->first_ip_address,
610 format_ip4_address, mp->last_ip_address,
613 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
615 for (i = 0; i < count; i++)
618 snat_add_address (sm, &this_addr);
620 rv = snat_del_address (sm, this_addr);
625 increment_v4_address (&this_addr);
629 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
632 static void *vl_api_snat_add_address_range_t_print
633 (vl_api_snat_add_address_range_t *mp, void * handle)
637 s = format (0, "SCRIPT: snat_add_address_range ");
638 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
639 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
641 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
647 send_snat_address_details
648 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
650 vl_api_snat_address_details_t *rmp;
651 snat_main_t * sm = &snat_main;
653 rmp = vl_msg_api_alloc (sizeof (*rmp));
654 memset (rmp, 0, sizeof (*rmp));
655 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS+sm->msg_id_base);
657 clib_memcpy (rmp->ip_address, &(a->addr), 4);
658 rmp->context = context;
660 vl_msg_api_send_shmem (q, (u8 *) & rmp);
664 vl_api_snat_address_dump_t_handler
665 (vl_api_snat_address_dump_t * mp)
667 unix_shared_memory_queue_t *q;
668 snat_main_t * sm = &snat_main;
671 q = vl_api_client_index_to_input_queue (mp->client_index);
675 vec_foreach (a, sm->addresses)
676 send_snat_address_details (a, q, mp->context);
679 static void *vl_api_snat_address_dump_t_print
680 (vl_api_snat_address_dump_t *mp, void * handle)
684 s = format (0, "SCRIPT: snat_address_dump ");
690 vl_api_snat_interface_add_del_feature_t_handler
691 (vl_api_snat_interface_add_del_feature_t * mp)
693 snat_main_t * sm = &snat_main;
694 vl_api_snat_interface_add_del_feature_reply_t * rmp;
695 u8 is_del = mp->is_add == 0;
696 u32 sw_if_index = ntohl(mp->sw_if_index);
699 VALIDATE_SW_IF_INDEX(mp);
701 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
703 BAD_SW_IF_INDEX_LABEL;
705 REPLY_MACRO(VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
708 static void *vl_api_snat_interface_add_del_feature_t_print
709 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
713 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
714 s = format (s, "sw_if_index %d %s %s",
715 clib_host_to_net_u32(mp->sw_if_index),
716 mp->is_inside ? "in":"out",
717 mp->is_add ? "" : "del");
723 send_snat_interface_details
724 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
726 vl_api_snat_interface_details_t *rmp;
727 snat_main_t * sm = &snat_main;
729 rmp = vl_msg_api_alloc (sizeof (*rmp));
730 memset (rmp, 0, sizeof (*rmp));
731 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS+sm->msg_id_base);
732 rmp->sw_if_index = ntohl (i->sw_if_index);
733 rmp->is_inside = i->is_inside;
734 rmp->context = context;
736 vl_msg_api_send_shmem (q, (u8 *) & rmp);
740 vl_api_snat_interface_dump_t_handler
741 (vl_api_snat_interface_dump_t * mp)
743 unix_shared_memory_queue_t *q;
744 snat_main_t * sm = &snat_main;
745 snat_interface_t * i;
747 q = vl_api_client_index_to_input_queue (mp->client_index);
751 pool_foreach (i, sm->interfaces,
753 send_snat_interface_details(i, q, mp->context);
757 static void *vl_api_snat_interface_dump_t_print
758 (vl_api_snat_interface_dump_t *mp, void * handle)
762 s = format (0, "SCRIPT: snat_interface_dump ");
767 vl_api_snat_add_static_mapping_t_handler
768 (vl_api_snat_add_static_mapping_t * mp)
770 snat_main_t * sm = &snat_main;
771 vl_api_snat_add_static_mapping_reply_t * rmp;
772 ip4_address_t local_addr, external_addr;
773 u16 local_port = 0, external_port = 0;
779 rv = VNET_API_ERROR_UNIMPLEMENTED;
783 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
784 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
785 if (mp->addr_only == 0)
787 local_port = clib_net_to_host_u16 (mp->local_port);
788 external_port = clib_net_to_host_u16 (mp->external_port);
790 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
792 rv = snat_add_static_mapping(local_addr, external_addr, local_port,
793 external_port, vrf_id, mp->addr_only,
797 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
800 static void *vl_api_snat_add_static_mapping_t_print
801 (vl_api_snat_add_static_mapping_t *mp, void * handle)
805 s = format (0, "SCRIPT: snat_add_static_mapping ");
806 s = format (s, "local_addr %U external_addr %U ",
807 format_ip4_address, mp->local_ip_address,
808 format_ip4_address, mp->external_ip_address);
810 if (mp->addr_only == 0)
811 s = format (s, "local_port %d external_port %d ",
812 clib_net_to_host_u16 (mp->local_port),
813 clib_net_to_host_u16 (mp->external_port));
815 if (mp->vrf_id != ~0)
816 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
822 send_snat_static_mapping_details
823 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
825 vl_api_snat_static_mapping_details_t *rmp;
826 snat_main_t * sm = &snat_main;
828 rmp = vl_msg_api_alloc (sizeof (*rmp));
829 memset (rmp, 0, sizeof (*rmp));
830 rmp->_vl_msg_id = ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS+sm->msg_id_base);
832 rmp->addr_only = m->addr_only;
833 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
834 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
835 rmp->local_port = htons (m->local_port);
836 rmp->external_port = htons (m->external_port);
837 rmp->vrf_id = htonl (m->vrf_id);
838 rmp->context = context;
840 vl_msg_api_send_shmem (q, (u8 *) & rmp);
844 vl_api_snat_static_mapping_dump_t_handler
845 (vl_api_snat_static_mapping_dump_t * mp)
847 unix_shared_memory_queue_t *q;
848 snat_main_t * sm = &snat_main;
849 snat_static_mapping_t * m;
851 q = vl_api_client_index_to_input_queue (mp->client_index);
855 pool_foreach (m, sm->static_mappings,
857 send_snat_static_mapping_details (m, q, mp->context);
861 static void *vl_api_snat_static_mapping_dump_t_print
862 (vl_api_snat_static_mapping_dump_t *mp, void * handle)
866 s = format (0, "SCRIPT: snat_static_mapping_dump ");
872 vl_api_snat_control_ping_t_handler
873 (vl_api_snat_control_ping_t * mp)
875 vl_api_snat_control_ping_reply_t *rmp;
876 snat_main_t * sm = &snat_main;
879 REPLY_MACRO2(VL_API_SNAT_CONTROL_PING_REPLY,
881 rmp->vpe_pid = ntohl (getpid());
885 static void *vl_api_snat_control_ping_t_print
886 (vl_api_snat_control_ping_t *mp, void * handle)
890 s = format (0, "SCRIPT: snat_control_ping ");
896 vl_api_snat_show_config_t_handler
897 (vl_api_snat_show_config_t * mp)
899 vl_api_snat_show_config_reply_t *rmp;
900 snat_main_t * sm = &snat_main;
903 REPLY_MACRO2(VL_API_SNAT_SHOW_CONFIG_REPLY,
905 rmp->translation_buckets = htons (sm->translation_buckets);
906 rmp->translation_memory_size = htons (sm->translation_memory_size);
907 rmp->user_buckets = htons (sm->user_buckets);
908 rmp->user_memory_size = htons (sm->user_memory_size);
909 rmp->max_translations_per_user = htons (sm->max_translations_per_user);
910 rmp->outside_vrf_id = htons (sm->outside_vrf_id);
911 rmp->inside_vrf_id = htons (sm->inside_vrf_id);
912 rmp->static_mapping_only = sm->static_mapping_only;
913 rmp->static_mapping_connection_tracking =
914 sm->static_mapping_connection_tracking;
918 static void *vl_api_snat_show_config_t_print
919 (vl_api_snat_show_config_t *mp, void * handle)
923 s = format (0, "SCRIPT: snat_show_config ");
928 /* List of message types that this plugin understands */
929 #define foreach_snat_plugin_api_msg \
930 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
931 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
932 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
933 _(SNAT_CONTROL_PING, snat_control_ping) \
934 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
935 _(SNAT_SHOW_CONFIG, snat_show_config) \
936 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
937 _(SNAT_INTERFACE_DUMP, snat_interface_dump)
939 /* Set up the API message handling tables */
940 static clib_error_t *
941 snat_plugin_api_hookup (vlib_main_t *vm)
943 snat_main_t * sm __attribute__ ((unused)) = &snat_main;
945 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
947 vl_api_##n##_t_handler, \
949 vl_api_##n##_t_endian, \
950 vl_api_##n##_t_print, \
951 sizeof(vl_api_##n##_t), 1);
952 foreach_snat_plugin_api_msg;
958 #define vl_msg_name_crc_list
959 #include <snat/snat_all_api_h.h>
960 #undef vl_msg_name_crc_list
963 setup_message_id_table (snat_main_t * sm, api_main_t * am)
965 #define _(id,n,crc) \
966 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
967 foreach_vl_msg_name_crc_snat;
971 static void plugin_custom_dump_configure (snat_main_t * sm)
973 #define _(n,f) sm->api_main->msg_print_handlers \
974 [VL_API_##n + sm->msg_id_base] \
975 = (void *) vl_api_##f##_t_print;
976 foreach_snat_plugin_api_msg;
980 static clib_error_t * snat_init (vlib_main_t * vm)
982 snat_main_t * sm = &snat_main;
983 clib_error_t * error = 0;
984 ip4_main_t * im = &ip4_main;
985 ip_lookup_main_t * lm = &im->lookup_main;
988 name = format (0, "snat_%08x%c", api_version, 0);
990 /* Ask for a correctly-sized block of API message decode slots */
991 sm->msg_id_base = vl_msg_api_get_msg_ids
992 ((char *) name, VL_MSG_FIRST_AVAILABLE);
995 sm->vnet_main = vnet_get_main();
997 sm->ip4_lookup_main = lm;
998 sm->api_main = &api_main;
1000 error = snat_plugin_api_hookup (vm);
1002 /* Add our API messages to the global name_crc hash table */
1003 setup_message_id_table (sm, &api_main);
1005 plugin_custom_dump_configure (sm);
1011 VLIB_INIT_FUNCTION (snat_init);
1013 void snat_free_outside_address_and_port (snat_main_t * sm,
1014 snat_session_key_t * k,
1018 u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
1020 ASSERT (address_index < vec_len (sm->addresses));
1022 a = sm->addresses + address_index;
1024 ASSERT (clib_bitmap_get (a->busy_port_bitmap, port_host_byte_order) == 1);
1026 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
1027 port_host_byte_order, 0);
1032 * @brief Match SNAT static mapping.
1034 * @param sm SNAT main.
1035 * @param match Address and port to match.
1036 * @param mapping External or local address and port of the matched mapping.
1037 * @param by_external If 0 match by local address otherwise match by external
1040 * @returns 0 if match found otherwise 1.
1042 int snat_static_mapping_match (snat_main_t * sm,
1043 snat_session_key_t match,
1044 snat_session_key_t * mapping,
1047 clib_bihash_kv_8_8_t kv, value;
1048 snat_static_mapping_t *m;
1049 snat_static_mapping_key_t m_key;
1050 clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local;
1053 mapping_hash = &sm->static_mapping_by_external;
1055 m_key.addr = match.addr;
1056 m_key.port = clib_net_to_host_u16 (match.port);
1057 m_key.fib_index = match.fib_index;
1059 kv.key = m_key.as_u64;
1061 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1063 /* Try address only mapping */
1065 kv.key = m_key.as_u64;
1066 if (clib_bihash_search_8_8 (mapping_hash, &kv, &value))
1070 m = pool_elt_at_index (sm->static_mappings, value.value);
1074 mapping->addr = m->local_addr;
1075 /* Address only mapping doesn't change port */
1076 mapping->port = m->addr_only ? match.port
1077 : clib_host_to_net_u16 (m->local_port);
1078 mapping->fib_index = m->fib_index;
1082 mapping->addr = m->external_addr;
1083 /* Address only mapping doesn't change port */
1084 mapping->port = m->addr_only ? match.port
1085 : clib_host_to_net_u16 (m->external_port);
1086 mapping->fib_index = sm->outside_fib_index;
1092 int snat_alloc_outside_address_and_port (snat_main_t * sm,
1093 snat_session_key_t * k,
1094 u32 * address_indexp)
1100 for (i = 0; i < vec_len (sm->addresses); i++)
1102 if (sm->addresses[i].busy_ports < (65535-1024))
1104 a = sm->addresses + i;
1108 portnum = random_u32 (&sm->random_seed);
1112 if (clib_bitmap_get (a->busy_port_bitmap, portnum))
1114 a->busy_port_bitmap = clib_bitmap_set (a->busy_port_bitmap,
1117 /* Caller sets protocol and fib index */
1119 k->port = clib_host_to_net_u16(portnum);
1120 *address_indexp = i;
1125 /* Totally out of translations to use... */
1130 static clib_error_t *
1131 add_address_command_fn (vlib_main_t * vm,
1132 unformat_input_t * input,
1133 vlib_cli_command_t * cmd)
1135 unformat_input_t _line_input, *line_input = &_line_input;
1136 snat_main_t * sm = &snat_main;
1137 ip4_address_t start_addr, end_addr, this_addr;
1138 u32 start_host_order, end_host_order;
1143 /* Get a line of input. */
1144 if (!unformat_user (input, unformat_line_input, line_input))
1147 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1149 if (unformat (line_input, "%U - %U",
1150 unformat_ip4_address, &start_addr,
1151 unformat_ip4_address, &end_addr))
1153 else if (unformat (line_input, "%U", unformat_ip4_address, &start_addr))
1154 end_addr = start_addr;
1155 else if (unformat (line_input, "del"))
1158 return clib_error_return (0, "unknown input '%U'",
1159 format_unformat_error, input);
1161 unformat_free (line_input);
1163 if (sm->static_mapping_only)
1164 return clib_error_return (0, "static mapping only mode");
1166 start_host_order = clib_host_to_net_u32 (start_addr.as_u32);
1167 end_host_order = clib_host_to_net_u32 (end_addr.as_u32);
1169 if (end_host_order < start_host_order)
1170 return clib_error_return (0, "end address less than start address");
1172 count = (end_host_order - start_host_order) + 1;
1175 clib_warning ("%U - %U, %d addresses...",
1176 format_ip4_address, &start_addr,
1177 format_ip4_address, &end_addr,
1180 this_addr = start_addr;
1182 for (i = 0; i < count; i++)
1185 snat_add_address (sm, &this_addr);
1187 rv = snat_del_address (sm, this_addr);
1191 case VNET_API_ERROR_NO_SUCH_ENTRY:
1192 return clib_error_return (0, "S-NAT address not exist.");
1194 case VNET_API_ERROR_UNSPECIFIED:
1195 return clib_error_return (0, "S-NAT address used in static mapping.");
1201 increment_v4_address (&this_addr);
1207 VLIB_CLI_COMMAND (add_address_command, static) = {
1208 .path = "snat add address",
1209 .short_help = "snat add addresses <ip4-range-start> [- <ip4-range-end>] [del]",
1210 .function = add_address_command_fn,
1213 static clib_error_t *
1214 snat_feature_command_fn (vlib_main_t * vm,
1215 unformat_input_t * input,
1216 vlib_cli_command_t * cmd)
1218 unformat_input_t _line_input, *line_input = &_line_input;
1219 vnet_main_t * vnm = vnet_get_main();
1220 clib_error_t * error = 0;
1222 u32 * inside_sw_if_indices = 0;
1223 u32 * outside_sw_if_indices = 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, "del"))
1244 return clib_error_return (0, "unknown input '%U'",
1245 format_unformat_error, input);
1247 unformat_free (line_input);
1249 if (vec_len (inside_sw_if_indices))
1251 for (i = 0; i < vec_len(inside_sw_if_indices); i++)
1253 sw_if_index = inside_sw_if_indices[i];
1254 snat_interface_add_del (sw_if_index, 1, is_del);
1258 if (vec_len (outside_sw_if_indices))
1260 for (i = 0; i < vec_len(outside_sw_if_indices); i++)
1262 sw_if_index = outside_sw_if_indices[i];
1263 snat_interface_add_del (sw_if_index, 0, is_del);
1267 vec_free (inside_sw_if_indices);
1268 vec_free (outside_sw_if_indices);
1273 VLIB_CLI_COMMAND (set_interface_snat_command, static) = {
1274 .path = "set interface snat",
1275 .function = snat_feature_command_fn,
1276 .short_help = "set interface snat in <intfc> out <intfc> [del]",
1279 static clib_error_t *
1280 add_static_mapping_command_fn (vlib_main_t * vm,
1281 unformat_input_t * input,
1282 vlib_cli_command_t * cmd)
1284 unformat_input_t _line_input, *line_input = &_line_input;
1285 clib_error_t * error = 0;
1286 ip4_address_t l_addr, e_addr;
1287 u32 l_port = 0, e_port = 0, vrf_id = ~0;
1292 /* Get a line of input. */
1293 if (!unformat_user (input, unformat_line_input, line_input))
1296 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1298 if (unformat (line_input, "local %U %u", unformat_ip4_address, &l_addr,
1301 else if (unformat (line_input, "local %U", unformat_ip4_address, &l_addr))
1303 else if (unformat (line_input, "external %U %u", unformat_ip4_address,
1306 else if (unformat (line_input, "external %U", unformat_ip4_address,
1309 else if (unformat (line_input, "vrf %u", &vrf_id))
1311 else if (unformat (line_input, "del"))
1314 return clib_error_return (0, "unknown input: '%U'",
1315 format_unformat_error, line_input);
1317 unformat_free (line_input);
1319 rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
1320 vrf_id, addr_only, is_add);
1324 case VNET_API_ERROR_INVALID_VALUE:
1325 return clib_error_return (0, "External port already in use.");
1327 case VNET_API_ERROR_NO_SUCH_ENTRY:
1329 return clib_error_return (0, "External addres must be allocated.");
1331 return clib_error_return (0, "Mapping not exist.");
1333 case VNET_API_ERROR_NO_SUCH_FIB:
1334 return clib_error_return (0, "No such VRF id.");
1335 case VNET_API_ERROR_VALUE_EXIST:
1336 return clib_error_return (0, "Mapping already exist.");
1346 * @cliexstart{snat add static mapping}
1347 * Static mapping allows hosts on the external network to initiate connection
1348 * to to the local network host.
1349 * To create static mapping between local host address 10.0.0.3 port 6303 and
1350 * external address 4.4.4.4 port 3606 use:
1351 * vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
1352 * If not runnig "static mapping only" S-NAT plugin mode use before:
1353 * vpp# snat add address 4.4.4.4
1354 * To create static mapping between local and external address use:
1355 * vpp# snat add static mapping local 10.0.0.3 external 4.4.4.4
1358 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
1359 .path = "snat add static mapping",
1360 .function = add_static_mapping_command_fn,
1362 "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
1365 static clib_error_t *
1366 snat_config (vlib_main_t * vm, unformat_input_t * input)
1368 snat_main_t * sm = &snat_main;
1369 u32 translation_buckets = 1024;
1370 u32 translation_memory_size = 128<<20;
1371 u32 user_buckets = 128;
1372 u32 user_memory_size = 64<<20;
1373 u32 max_translations_per_user = 100;
1374 u32 outside_vrf_id = 0;
1375 u32 inside_vrf_id = 0;
1376 u32 static_mapping_buckets = 1024;
1377 u32 static_mapping_memory_size = 64<<20;
1378 u8 static_mapping_only = 0;
1379 u8 static_mapping_connection_tracking = 0;
1381 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1383 if (unformat (input, "translation hash buckets %d", &translation_buckets))
1385 else if (unformat (input, "translation hash memory %d",
1386 &translation_memory_size));
1387 else if (unformat (input, "user hash buckets %d", &user_buckets))
1389 else if (unformat (input, "user hash memory %d",
1392 else if (unformat (input, "max translations per user %d",
1393 &max_translations_per_user))
1395 else if (unformat (input, "outside VRF id %d",
1398 else if (unformat (input, "inside VRF id %d",
1401 else if (unformat (input, "static mapping only"))
1403 static_mapping_only = 1;
1404 if (unformat (input, "connection tracking"))
1405 static_mapping_connection_tracking = 1;
1408 return clib_error_return (0, "unknown input '%U'",
1409 format_unformat_error, input);
1412 /* for show commands, etc. */
1413 sm->translation_buckets = translation_buckets;
1414 sm->translation_memory_size = translation_memory_size;
1415 sm->user_buckets = user_buckets;
1416 sm->user_memory_size = user_memory_size;
1417 sm->max_translations_per_user = max_translations_per_user;
1418 sm->outside_vrf_id = outside_vrf_id;
1419 sm->outside_fib_index = ~0;
1420 sm->inside_vrf_id = inside_vrf_id;
1421 sm->inside_fib_index = ~0;
1422 sm->static_mapping_only = static_mapping_only;
1423 sm->static_mapping_connection_tracking = static_mapping_connection_tracking;
1425 if (!static_mapping_only ||
1426 (static_mapping_only && static_mapping_connection_tracking))
1428 clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets,
1429 translation_memory_size);
1431 clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets,
1432 translation_memory_size);
1434 clib_bihash_init_8_8 (&sm->user_hash, "users", user_buckets,
1437 clib_bihash_init_8_8 (&sm->static_mapping_by_local,
1438 "static_mapping_by_local", static_mapping_buckets,
1439 static_mapping_memory_size);
1441 clib_bihash_init_8_8 (&sm->static_mapping_by_external,
1442 "static_mapping_by_external", static_mapping_buckets,
1443 static_mapping_memory_size);
1447 VLIB_CONFIG_FUNCTION (snat_config, "snat");
1449 u8 * format_snat_key (u8 * s, va_list * args)
1451 snat_session_key_t * key = va_arg (*args, snat_session_key_t *);
1452 char * protocol_string = "unknown";
1453 static char *protocol_strings[] = {
1459 if (key->protocol < ARRAY_LEN(protocol_strings))
1460 protocol_string = protocol_strings[key->protocol];
1462 s = format (s, "%U proto %s port %d fib %d",
1463 format_ip4_address, &key->addr, protocol_string,
1464 clib_net_to_host_u16 (key->port), key->fib_index);
1468 u8 * format_snat_session (u8 * s, va_list * args)
1470 snat_main_t * sm __attribute__((unused)) = va_arg (*args, snat_main_t *);
1471 snat_session_t * sess = va_arg (*args, snat_session_t *);
1473 s = format (s, " i2o %U\n", format_snat_key, &sess->in2out);
1474 s = format (s, " o2i %U\n", format_snat_key, &sess->out2in);
1475 s = format (s, " last heard %.2f\n", sess->last_heard);
1476 s = format (s, " total pkts %d, total bytes %lld\n",
1477 sess->total_pkts, sess->total_bytes);
1478 if (snat_is_session_static (sess))
1479 s = format (s, " static translation\n");
1481 s = format (s, " dynamic translation\n");
1486 u8 * format_snat_user (u8 * s, va_list * args)
1488 snat_main_t * sm = va_arg (*args, snat_main_t *);
1489 snat_user_t * u = va_arg (*args, snat_user_t *);
1490 int verbose = va_arg (*args, int);
1491 dlist_elt_t * head, * elt;
1492 u32 elt_index, head_index;
1494 snat_session_t * sess;
1496 s = format (s, "%U: %d dynamic translations, %d static translations\n",
1497 format_ip4_address, &u->addr, u->nsessions, u->nstaticsessions);
1502 if (u->nsessions || u->nstaticsessions)
1504 head_index = u->sessions_per_user_list_head_index;
1505 head = pool_elt_at_index (sm->list_pool, head_index);
1507 elt_index = head->next;
1508 elt = pool_elt_at_index (sm->list_pool, elt_index);
1509 session_index = elt->value;
1511 while (session_index != ~0)
1513 sess = pool_elt_at_index (sm->sessions, session_index);
1515 s = format (s, " %U\n", format_snat_session, sm, sess);
1517 elt_index = elt->next;
1518 elt = pool_elt_at_index (sm->list_pool, elt_index);
1519 session_index = elt->value;
1526 u8 * format_snat_static_mapping (u8 * s, va_list * args)
1528 snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
1531 s = format (s, "local %U external %U vrf %d",
1532 format_ip4_address, &m->local_addr,
1533 format_ip4_address, &m->external_addr,
1536 s = format (s, "local %U:%d external %U:%d vrf %d",
1537 format_ip4_address, &m->local_addr, m->local_port,
1538 format_ip4_address, &m->external_addr, m->external_port,
1544 static clib_error_t *
1545 show_snat_command_fn (vlib_main_t * vm,
1546 unformat_input_t * input,
1547 vlib_cli_command_t * cmd)
1550 snat_main_t * sm = &snat_main;
1552 snat_static_mapping_t *m;
1553 snat_interface_t *i;
1554 vnet_main_t *vnm = vnet_get_main();
1556 if (unformat (input, "detail"))
1558 else if (unformat (input, "verbose"))
1561 if (sm->static_mapping_only)
1563 if (sm->static_mapping_connection_tracking)
1564 vlib_cli_output (vm, "SNAT mode: static mapping only connection "
1567 vlib_cli_output (vm, "SNAT mode: static mapping only");
1571 vlib_cli_output (vm, "SNAT mode: dynamic translations enabled");
1576 pool_foreach (i, sm->interfaces,
1578 vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
1579 vnet_get_sw_interface (vnm, i->sw_if_index),
1580 i->is_inside ? "in" : "out");
1584 if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
1586 vlib_cli_output (vm, "%d static mappings",
1587 pool_elts (sm->static_mappings));
1591 pool_foreach (m, sm->static_mappings,
1593 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1599 vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
1600 " %d static mappings",
1601 pool_elts (sm->users),
1602 vec_len (sm->addresses),
1603 pool_elts (sm->sessions),
1604 pool_elts (sm->static_mappings));
1608 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
1610 vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
1612 vlib_cli_output (vm, "%d list pool elements",
1613 pool_elts (sm->list_pool));
1615 pool_foreach (u, sm->users,
1617 vlib_cli_output (vm, "%U", format_snat_user, sm, u, verbose - 1);
1620 if (pool_elts (sm->static_mappings))
1622 vlib_cli_output (vm, "static mappings:");
1623 pool_foreach (m, sm->static_mappings,
1625 vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
1634 VLIB_CLI_COMMAND (show_snat_command, static) = {
1635 .path = "show snat",
1636 .short_help = "show snat",
1637 .function = show_snat_command_fn,