2 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @brief SNAT plugin API implementation
21 #include <snat/snat.h>
22 #include <snat/snat_det.h>
23 #include <snat/nat64.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26 #include <vlibsocket/api.h>
27 #include <snat/snat_msg_enum.h>
28 #include <vnet/fib/fib_table.h>
30 /* define message structures */
32 #include <snat/snat_all_api_h.h>
35 /* define generated endian-swappers */
37 #include <snat/snat_all_api_h.h>
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define REPLY_MSG_ID_BASE sm->msg_id_base
43 #include <vlibapi/api_helper_macros.h>
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <snat/snat_all_api_h.h>
50 /* Macro to finish up custom dump fns */
53 vl_print (handle, (char *)s); \
58 vl_api_snat_add_address_range_t_handler
59 (vl_api_snat_add_address_range_t * mp)
61 snat_main_t *sm = &snat_main;
62 vl_api_snat_add_address_range_reply_t *rmp;
63 ip4_address_t this_addr;
64 u32 start_host_order, end_host_order;
72 rv = VNET_API_ERROR_UNIMPLEMENTED;
76 if (sm->static_mapping_only)
78 rv = VNET_API_ERROR_FEATURE_DISABLED;
82 tmp = (u32 *) mp->first_ip_address;
83 start_host_order = clib_host_to_net_u32 (tmp[0]);
84 tmp = (u32 *) mp->last_ip_address;
85 end_host_order = clib_host_to_net_u32 (tmp[0]);
87 count = (end_host_order - start_host_order) + 1;
89 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
92 clib_warning ("%U - %U, %d addresses...",
93 format_ip4_address, mp->first_ip_address,
94 format_ip4_address, mp->last_ip_address, count);
96 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
98 for (i = 0; i < count; i++)
101 snat_add_address (sm, &this_addr, vrf_id);
103 rv = snat_del_address (sm, this_addr, 0);
108 increment_v4_address (&this_addr);
112 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
115 static void *vl_api_snat_add_address_range_t_print
116 (vl_api_snat_add_address_range_t * mp, void *handle)
120 s = format (0, "SCRIPT: snat_add_address_range ");
121 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
122 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
124 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
130 send_snat_address_details
131 (snat_address_t * a, unix_shared_memory_queue_t * q, u32 context)
133 vl_api_snat_address_details_t *rmp;
134 snat_main_t *sm = &snat_main;
136 rmp = vl_msg_api_alloc (sizeof (*rmp));
137 memset (rmp, 0, sizeof (*rmp));
138 rmp->_vl_msg_id = ntohs (VL_API_SNAT_ADDRESS_DETAILS + sm->msg_id_base);
140 clib_memcpy (rmp->ip_address, &(a->addr), 4);
141 if (a->fib_index != ~0)
143 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
144 rmp->vrf_id = ntohl (fib->ft_table_id);
148 rmp->context = context;
150 vl_msg_api_send_shmem (q, (u8 *) & rmp);
154 vl_api_snat_address_dump_t_handler (vl_api_snat_address_dump_t * mp)
156 unix_shared_memory_queue_t *q;
157 snat_main_t *sm = &snat_main;
160 q = vl_api_client_index_to_input_queue (mp->client_index);
165 vec_foreach (a, sm->addresses)
166 send_snat_address_details (a, q, mp->context);
170 static void *vl_api_snat_address_dump_t_print
171 (vl_api_snat_address_dump_t * mp, void *handle)
175 s = format (0, "SCRIPT: snat_address_dump ");
181 vl_api_snat_interface_add_del_feature_t_handler
182 (vl_api_snat_interface_add_del_feature_t * mp)
184 snat_main_t *sm = &snat_main;
185 vl_api_snat_interface_add_del_feature_reply_t *rmp;
186 u8 is_del = mp->is_add == 0;
187 u32 sw_if_index = ntohl (mp->sw_if_index);
190 VALIDATE_SW_IF_INDEX (mp);
192 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
194 BAD_SW_IF_INDEX_LABEL;
196 REPLY_MACRO (VL_API_SNAT_INTERFACE_ADD_DEL_FEATURE_REPLY);
199 static void *vl_api_snat_interface_add_del_feature_t_print
200 (vl_api_snat_interface_add_del_feature_t * mp, void *handle)
204 s = format (0, "SCRIPT: snat_interface_add_del_feature ");
205 s = format (s, "sw_if_index %d %s %s",
206 clib_host_to_net_u32 (mp->sw_if_index),
207 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
213 send_snat_interface_details
214 (snat_interface_t * i, unix_shared_memory_queue_t * q, u32 context)
216 vl_api_snat_interface_details_t *rmp;
217 snat_main_t *sm = &snat_main;
219 rmp = vl_msg_api_alloc (sizeof (*rmp));
220 memset (rmp, 0, sizeof (*rmp));
221 rmp->_vl_msg_id = ntohs (VL_API_SNAT_INTERFACE_DETAILS + sm->msg_id_base);
222 rmp->sw_if_index = ntohl (i->sw_if_index);
223 rmp->is_inside = i->is_inside;
224 rmp->context = context;
226 vl_msg_api_send_shmem (q, (u8 *) & rmp);
230 vl_api_snat_interface_dump_t_handler (vl_api_snat_interface_dump_t * mp)
232 unix_shared_memory_queue_t *q;
233 snat_main_t *sm = &snat_main;
236 q = vl_api_client_index_to_input_queue (mp->client_index);
241 pool_foreach (i, sm->interfaces,
243 send_snat_interface_details(i, q, mp->context);
248 static void *vl_api_snat_interface_dump_t_print
249 (vl_api_snat_interface_dump_t * mp, void *handle)
253 s = format (0, "SCRIPT: snat_interface_dump ");
259 vl_api_snat_interface_add_del_output_feature_t_handler
260 (vl_api_snat_interface_add_del_output_feature_t * mp)
262 snat_main_t *sm = &snat_main;
263 vl_api_snat_interface_add_del_output_feature_reply_t *rmp;
264 u8 is_del = mp->is_add == 0;
265 u32 sw_if_index = ntohl (mp->sw_if_index);
268 VALIDATE_SW_IF_INDEX (mp);
270 rv = snat_interface_add_del_output_feature (sw_if_index, mp->is_inside,
273 BAD_SW_IF_INDEX_LABEL;
275 REPLY_MACRO (VL_API_SNAT_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
278 static void *vl_api_snat_interface_add_del_output_feature_t_print
279 (vl_api_snat_interface_add_del_output_feature_t * mp, void *handle)
283 s = format (0, "SCRIPT: snat_interface_add_del_output_feature ");
284 s = format (s, "sw_if_index %d %s %s",
285 clib_host_to_net_u32 (mp->sw_if_index),
286 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
292 send_snat_interface_output_feature_details (snat_interface_t * i,
293 unix_shared_memory_queue_t * q,
296 vl_api_snat_interface_output_feature_details_t *rmp;
297 snat_main_t *sm = &snat_main;
299 rmp = vl_msg_api_alloc (sizeof (*rmp));
300 memset (rmp, 0, sizeof (*rmp));
302 ntohs (VL_API_SNAT_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
303 rmp->sw_if_index = ntohl (i->sw_if_index);
304 rmp->context = context;
305 rmp->is_inside = i->is_inside;
307 vl_msg_api_send_shmem (q, (u8 *) & rmp);
311 vl_api_snat_interface_output_feature_dump_t_handler
312 (vl_api_snat_interface_output_feature_dump_t * mp)
314 unix_shared_memory_queue_t *q;
315 snat_main_t *sm = &snat_main;
318 q = vl_api_client_index_to_input_queue (mp->client_index);
323 pool_foreach (i, sm->output_feature_interfaces,
325 send_snat_interface_output_feature_details(i, q, mp->context);
330 static void *vl_api_snat_interface_output_feature_dump_t_print
331 (vl_api_snat_interface_output_feature_dump_t * mp, void *handle)
335 s = format (0, "SCRIPT: snat_interface_output_feature_dump ");
341 vl_api_snat_add_static_mapping_t_handler
342 (vl_api_snat_add_static_mapping_t * mp)
344 snat_main_t *sm = &snat_main;
345 vl_api_snat_add_static_mapping_reply_t *rmp;
346 ip4_address_t local_addr, external_addr;
347 u16 local_port = 0, external_port = 0;
348 u32 vrf_id, external_sw_if_index;
350 snat_protocol_t proto;
354 rv = VNET_API_ERROR_UNIMPLEMENTED;
358 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
359 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
360 if (mp->addr_only == 0)
362 local_port = clib_net_to_host_u16 (mp->local_port);
363 external_port = clib_net_to_host_u16 (mp->external_port);
365 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
366 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
367 proto = ip_proto_to_snat_proto (mp->protocol);
369 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
370 external_port, vrf_id, mp->addr_only,
371 external_sw_if_index, proto, mp->is_add);
374 REPLY_MACRO (VL_API_SNAT_ADD_ADDRESS_RANGE_REPLY);
377 static void *vl_api_snat_add_static_mapping_t_print
378 (vl_api_snat_add_static_mapping_t * mp, void *handle)
382 s = format (0, "SCRIPT: snat_add_static_mapping ");
383 s = format (s, "protocol %d local_addr %U external_addr %U ",
385 format_ip4_address, mp->local_ip_address,
386 format_ip4_address, mp->external_ip_address);
388 if (mp->addr_only == 0)
389 s = format (s, "local_port %d external_port %d ",
390 clib_net_to_host_u16 (mp->local_port),
391 clib_net_to_host_u16 (mp->external_port));
393 if (mp->vrf_id != ~0)
394 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
396 if (mp->external_sw_if_index != ~0)
397 s = format (s, "external_sw_if_index %d",
398 clib_net_to_host_u32 (mp->external_sw_if_index));
403 send_snat_static_mapping_details
404 (snat_static_mapping_t * m, unix_shared_memory_queue_t * q, u32 context)
406 vl_api_snat_static_mapping_details_t *rmp;
407 snat_main_t *sm = &snat_main;
409 rmp = vl_msg_api_alloc (sizeof (*rmp));
410 memset (rmp, 0, sizeof (*rmp));
412 ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS + sm->msg_id_base);
414 rmp->addr_only = m->addr_only;
415 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
416 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
417 rmp->local_port = htons (m->local_port);
418 rmp->external_port = htons (m->external_port);
419 rmp->external_sw_if_index = ~0;
420 rmp->vrf_id = htonl (m->vrf_id);
421 rmp->protocol = snat_proto_to_ip_proto (m->proto);
422 rmp->context = context;
424 vl_msg_api_send_shmem (q, (u8 *) & rmp);
428 send_snat_static_map_resolve_details
429 (snat_static_map_resolve_t * m, unix_shared_memory_queue_t * q, u32 context)
431 vl_api_snat_static_mapping_details_t *rmp;
432 snat_main_t *sm = &snat_main;
434 rmp = vl_msg_api_alloc (sizeof (*rmp));
435 memset (rmp, 0, sizeof (*rmp));
437 ntohs (VL_API_SNAT_STATIC_MAPPING_DETAILS + sm->msg_id_base);
439 rmp->addr_only = m->addr_only;
440 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
441 rmp->local_port = htons (m->l_port);
442 rmp->external_port = htons (m->e_port);
443 rmp->external_sw_if_index = htonl (m->sw_if_index);
444 rmp->vrf_id = htonl (m->vrf_id);
445 rmp->protocol = snat_proto_to_ip_proto (m->proto);
446 rmp->context = context;
448 vl_msg_api_send_shmem (q, (u8 *) & rmp);
452 vl_api_snat_static_mapping_dump_t_handler
453 (vl_api_snat_static_mapping_dump_t * mp)
455 unix_shared_memory_queue_t *q;
456 snat_main_t *sm = &snat_main;
457 snat_static_mapping_t *m;
458 snat_static_map_resolve_t *rp;
461 q = vl_api_client_index_to_input_queue (mp->client_index);
466 pool_foreach (m, sm->static_mappings,
468 send_snat_static_mapping_details (m, q, mp->context);
472 for (j = 0; j < vec_len (sm->to_resolve); j++)
474 rp = sm->to_resolve + j;
475 send_snat_static_map_resolve_details (rp, q, mp->context);
479 static void *vl_api_snat_static_mapping_dump_t_print
480 (vl_api_snat_static_mapping_dump_t * mp, void *handle)
484 s = format (0, "SCRIPT: snat_static_mapping_dump ");
490 vl_api_snat_control_ping_t_handler (vl_api_snat_control_ping_t * mp)
492 vl_api_snat_control_ping_reply_t *rmp;
493 snat_main_t *sm = &snat_main;
497 REPLY_MACRO2 (VL_API_SNAT_CONTROL_PING_REPLY,
499 rmp->vpe_pid = ntohl (getpid ());
504 static void *vl_api_snat_control_ping_t_print
505 (vl_api_snat_control_ping_t * mp, void *handle)
509 s = format (0, "SCRIPT: snat_control_ping ");
515 vl_api_snat_show_config_t_handler (vl_api_snat_show_config_t * mp)
517 vl_api_snat_show_config_reply_t *rmp;
518 snat_main_t *sm = &snat_main;
522 REPLY_MACRO2 (VL_API_SNAT_SHOW_CONFIG_REPLY,
524 rmp->translation_buckets = htonl (sm->translation_buckets);
525 rmp->translation_memory_size = htonl (sm->translation_memory_size);
526 rmp->user_buckets = htonl (sm->user_buckets);
527 rmp->user_memory_size = htonl (sm->user_memory_size);
528 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
529 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
530 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
531 rmp->static_mapping_only = sm->static_mapping_only;
532 rmp->static_mapping_connection_tracking =
533 sm->static_mapping_connection_tracking;
534 rmp->deterministic = sm->deterministic;
539 static void *vl_api_snat_show_config_t_print
540 (vl_api_snat_show_config_t * mp, void *handle)
544 s = format (0, "SCRIPT: snat_show_config ");
550 vl_api_snat_set_workers_t_handler (vl_api_snat_set_workers_t * mp)
552 snat_main_t *sm = &snat_main;
553 vl_api_snat_set_workers_reply_t *rmp;
556 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
558 if (sm->num_workers < 2)
560 rv = VNET_API_ERROR_FEATURE_DISABLED;
564 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
565 rv = snat_set_workers (bitmap);
566 clib_bitmap_free (bitmap);
569 REPLY_MACRO (VL_API_SNAT_SET_WORKERS_REPLY);
572 static void *vl_api_snat_set_workers_t_print
573 (vl_api_snat_set_workers_t * mp, void *handle)
579 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
581 s = format (0, "SCRIPT: snat_set_workers ");
582 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
584 clib_bitmap_foreach (i, bitmap,
587 s = format (s, "%d", i);
589 s = format (s, ",%d", i);
593 clib_bitmap_free (bitmap);
598 send_snat_worker_details
599 (u32 worker_index, unix_shared_memory_queue_t * q, u32 context)
601 vl_api_snat_worker_details_t *rmp;
602 snat_main_t *sm = &snat_main;
603 vlib_worker_thread_t *w =
604 vlib_worker_threads + worker_index + sm->first_worker_index;
606 rmp = vl_msg_api_alloc (sizeof (*rmp));
607 memset (rmp, 0, sizeof (*rmp));
608 rmp->_vl_msg_id = ntohs (VL_API_SNAT_WORKER_DETAILS + sm->msg_id_base);
609 rmp->context = context;
610 rmp->worker_index = htonl (worker_index);
611 rmp->lcore_id = htonl (w->lcore_id);
612 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
614 vl_msg_api_send_shmem (q, (u8 *) & rmp);
618 vl_api_snat_worker_dump_t_handler (vl_api_snat_worker_dump_t * mp)
620 unix_shared_memory_queue_t *q;
621 snat_main_t *sm = &snat_main;
624 q = vl_api_client_index_to_input_queue (mp->client_index);
629 vec_foreach (worker_index, sm->workers)
630 send_snat_worker_details(*worker_index, q, mp->context);
634 static void *vl_api_snat_worker_dump_t_print
635 (vl_api_snat_worker_dump_t * mp, void *handle)
639 s = format (0, "SCRIPT: snat_worker_dump ");
645 vl_api_snat_add_del_interface_addr_t_handler
646 (vl_api_snat_add_del_interface_addr_t * mp)
648 snat_main_t *sm = &snat_main;
649 vl_api_snat_add_del_interface_addr_reply_t *rmp;
650 u8 is_del = mp->is_add == 0;
651 u32 sw_if_index = ntohl (mp->sw_if_index);
654 VALIDATE_SW_IF_INDEX (mp);
656 rv = snat_add_interface_address (sm, sw_if_index, is_del);
658 BAD_SW_IF_INDEX_LABEL;
660 REPLY_MACRO (VL_API_SNAT_ADD_DEL_INTERFACE_ADDR_REPLY);
663 static void *vl_api_snat_add_del_interface_addr_t_print
664 (vl_api_snat_add_del_interface_addr_t * mp, void *handle)
668 s = format (0, "SCRIPT: snat_add_del_interface_addr ");
669 s = format (s, "sw_if_index %d %s",
670 clib_host_to_net_u32 (mp->sw_if_index),
671 mp->is_add ? "" : "del");
677 send_snat_interface_addr_details
678 (u32 sw_if_index, unix_shared_memory_queue_t * q, u32 context)
680 vl_api_snat_interface_addr_details_t *rmp;
681 snat_main_t *sm = &snat_main;
683 rmp = vl_msg_api_alloc (sizeof (*rmp));
684 memset (rmp, 0, sizeof (*rmp));
686 ntohs (VL_API_SNAT_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
687 rmp->sw_if_index = ntohl (sw_if_index);
688 rmp->context = context;
690 vl_msg_api_send_shmem (q, (u8 *) & rmp);
694 vl_api_snat_interface_addr_dump_t_handler
695 (vl_api_snat_interface_addr_dump_t * mp)
697 unix_shared_memory_queue_t *q;
698 snat_main_t *sm = &snat_main;
701 q = vl_api_client_index_to_input_queue (mp->client_index);
706 vec_foreach (i, sm->auto_add_sw_if_indices)
707 send_snat_interface_addr_details(*i, q, mp->context);
711 static void *vl_api_snat_interface_addr_dump_t_print
712 (vl_api_snat_interface_addr_dump_t * mp, void *handle)
716 s = format (0, "SCRIPT: snat_interface_addr_dump ");
722 vl_api_snat_ipfix_enable_disable_t_handler
723 (vl_api_snat_ipfix_enable_disable_t * mp)
725 snat_main_t *sm = &snat_main;
726 vl_api_snat_ipfix_enable_disable_reply_t *rmp;
729 rv = snat_ipfix_logging_enable_disable (mp->enable,
735 REPLY_MACRO (VL_API_SNAT_IPFIX_ENABLE_DISABLE_REPLY);
738 static void *vl_api_snat_ipfix_enable_disable_t_print
739 (vl_api_snat_ipfix_enable_disable_t * mp, void *handle)
743 s = format (0, "SCRIPT: snat_ipfix_enable_disable ");
745 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
747 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
749 s = format (s, "disable ");
755 send_snat_user_details
756 (snat_user_t * u, unix_shared_memory_queue_t * q, u32 context)
758 vl_api_snat_user_details_t *rmp;
759 snat_main_t *sm = &snat_main;
760 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
762 rmp = vl_msg_api_alloc (sizeof (*rmp));
763 memset (rmp, 0, sizeof (*rmp));
764 rmp->_vl_msg_id = ntohs (VL_API_SNAT_USER_DETAILS + sm->msg_id_base);
766 rmp->vrf_id = ntohl (fib->ft_table_id);
769 clib_memcpy (rmp->ip_address, &(u->addr), 4);
770 rmp->nsessions = ntohl (u->nsessions);
771 rmp->nstaticsessions = ntohl (u->nstaticsessions);
772 rmp->context = context;
774 vl_msg_api_send_shmem (q, (u8 *) & rmp);
778 vl_api_snat_user_dump_t_handler (vl_api_snat_user_dump_t * mp)
780 unix_shared_memory_queue_t *q;
781 snat_main_t *sm = &snat_main;
782 snat_main_per_thread_data_t *tsm;
785 q = vl_api_client_index_to_input_queue (mp->client_index);
790 vec_foreach (tsm, sm->per_thread_data)
791 vec_foreach (u, tsm->users)
792 send_snat_user_details (u, q, mp->context);
796 static void *vl_api_snat_user_dump_t_print
797 (vl_api_snat_user_dump_t * mp, void *handle)
801 s = format (0, "SCRIPT: snat_user_dump ");
807 send_snat_user_session_details
808 (snat_session_t * s, unix_shared_memory_queue_t * q, u32 context)
810 vl_api_snat_user_session_details_t *rmp;
811 snat_main_t *sm = &snat_main;
813 rmp = vl_msg_api_alloc (sizeof (*rmp));
814 memset (rmp, 0, sizeof (*rmp));
816 ntohs (VL_API_SNAT_USER_SESSION_DETAILS + sm->msg_id_base);
818 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
819 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
820 rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
821 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
822 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
823 rmp->total_pkts = ntohl (s->total_pkts);
824 rmp->context = context;
825 if (snat_is_unk_proto_session (s))
827 rmp->outside_port = 0;
828 rmp->inside_port = 0;
829 rmp->protocol = ntohs (s->in2out.port);
833 rmp->outside_port = s->out2in.port;
834 rmp->inside_port = s->in2out.port;
835 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
838 vl_msg_api_send_shmem (q, (u8 *) & rmp);
842 vl_api_snat_user_session_dump_t_handler
843 (vl_api_snat_user_session_dump_t * mp)
845 unix_shared_memory_queue_t *q;
846 snat_main_t *sm = &snat_main;
847 snat_main_per_thread_data_t *tsm;
849 clib_bihash_kv_8_8_t key, value;
850 snat_user_key_t ukey;
852 u32 session_index, head_index, elt_index;
853 dlist_elt_t *head, *elt;
855 q = vl_api_client_index_to_input_queue (mp->client_index);
861 clib_memcpy (&ukey.addr, mp->ip_address, 4);
862 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
863 key.key = ukey.as_u64;
864 if (!clib_bihash_search_8_8 (&sm->worker_by_in, &key, &value))
865 tsm = vec_elt_at_index (sm->per_thread_data, value.value);
867 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
868 if (clib_bihash_search_8_8 (&sm->user_hash, &key, &value))
870 u = pool_elt_at_index (tsm->users, value.value);
871 if (!u->nsessions && !u->nstaticsessions)
874 head_index = u->sessions_per_user_list_head_index;
875 head = pool_elt_at_index (tsm->list_pool, head_index);
876 elt_index = head->next;
877 elt = pool_elt_at_index (tsm->list_pool, elt_index);
878 session_index = elt->value;
879 while (session_index != ~0)
881 s = pool_elt_at_index (tsm->sessions, session_index);
883 send_snat_user_session_details (s, q, mp->context);
885 elt_index = elt->next;
886 elt = pool_elt_at_index (tsm->list_pool, elt_index);
887 session_index = elt->value;
891 static void *vl_api_snat_user_session_dump_t_print
892 (vl_api_snat_user_session_dump_t * mp, void *handle)
896 s = format (0, "SCRIPT: snat_user_session_dump ");
897 s = format (s, "ip_address %U vrf_id %d\n",
898 format_ip4_address, mp->ip_address,
899 clib_net_to_host_u32 (mp->vrf_id));
904 /****************************/
905 /*** detrministic NAT/CGN ***/
906 /****************************/
909 vl_api_snat_add_det_map_t_handler (vl_api_snat_add_det_map_t * mp)
911 snat_main_t *sm = &snat_main;
912 vl_api_snat_add_det_map_reply_t *rmp;
914 ip4_address_t in_addr, out_addr;
916 clib_memcpy (&in_addr, mp->in_addr, 4);
917 clib_memcpy (&out_addr, mp->out_addr, 4);
918 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
919 mp->out_plen, mp->is_add);
921 REPLY_MACRO (VL_API_SNAT_ADD_DET_MAP_REPLY);
924 static void *vl_api_snat_add_det_map_t_print
925 (vl_api_snat_add_det_map_t * mp, void *handle)
929 s = format (0, "SCRIPT: snat_add_det_map ");
930 s = format (s, "inside address %U/%d outside address %U/%d\n",
931 format_ip4_address, mp->in_addr, mp->in_plen,
932 format_ip4_address, mp->out_addr, mp->out_plen);
938 vl_api_snat_det_forward_t_handler (vl_api_snat_det_forward_t * mp)
940 snat_main_t *sm = &snat_main;
941 vl_api_snat_det_forward_reply_t *rmp;
943 u16 lo_port = 0, hi_port = 0;
945 ip4_address_t in_addr, out_addr;
948 clib_memcpy (&in_addr, mp->in_addr, 4);
949 dm = snat_det_map_by_user (sm, &in_addr);
952 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
956 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
957 hi_port = lo_port + dm->ports_per_host - 1;
961 REPLY_MACRO2 (VL_API_SNAT_DET_FORWARD_REPLY,
963 rmp->out_port_lo = ntohs (lo_port);
964 rmp->out_port_hi = ntohs (hi_port);
966 memset (rmp->out_addr, 0, 16);
967 clib_memcpy (rmp->out_addr, &out_addr, 4);
972 static void *vl_api_snat_det_forward_t_print
973 (vl_api_snat_det_forward_t * mp, void *handle)
977 s = format (0, "SCRIPT: smat_det_forward_t");
978 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
984 vl_api_snat_det_reverse_t_handler (vl_api_snat_det_reverse_t * mp)
986 snat_main_t *sm = &snat_main;
987 vl_api_snat_det_reverse_reply_t *rmp;
989 ip4_address_t out_addr, in_addr;
993 clib_memcpy (&out_addr, mp->out_addr, 4);
994 dm = snat_det_map_by_out (sm, &out_addr);
997 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1001 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
1005 REPLY_MACRO2 (VL_API_SNAT_DET_REVERSE_REPLY,
1008 memset (rmp->in_addr, 0, 16);
1009 clib_memcpy (rmp->in_addr, &in_addr, 4);
1014 static void *vl_api_snat_det_reverse_t_print
1015 (vl_api_snat_det_reverse_t * mp, void *handle)
1019 s = format (0, "SCRIPT: smat_det_reverse_t");
1020 s = format (s, "outside ip address %U outside port %d",
1021 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
1027 sent_snat_det_map_details
1028 (snat_det_map_t * m, unix_shared_memory_queue_t * q, u32 context)
1030 vl_api_snat_det_map_details_t *rmp;
1031 snat_main_t *sm = &snat_main;
1033 rmp = vl_msg_api_alloc (sizeof (*rmp));
1034 memset (rmp, 0, sizeof (*rmp));
1035 rmp->_vl_msg_id = ntohs (VL_API_SNAT_DET_MAP_DETAILS + sm->msg_id_base);
1037 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
1038 rmp->in_plen = m->in_plen;
1039 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
1040 rmp->out_plen = m->out_plen;
1041 rmp->sharing_ratio = htonl (m->sharing_ratio);
1042 rmp->ports_per_host = htons (m->ports_per_host);
1043 rmp->ses_num = htonl (m->ses_num);
1044 rmp->context = context;
1046 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1050 vl_api_snat_det_map_dump_t_handler (vl_api_snat_det_map_dump_t * mp)
1052 unix_shared_memory_queue_t *q;
1053 snat_main_t *sm = &snat_main;
1056 q = vl_api_client_index_to_input_queue (mp->client_index);
1061 vec_foreach(m, sm->det_maps)
1062 sent_snat_det_map_details(m, q, mp->context);
1066 static void *vl_api_snat_det_map_dump_t_print
1067 (vl_api_snat_det_map_dump_t * mp, void *handle)
1071 s = format (0, "SCRIPT: snat_det_map_dump ");
1077 vl_api_snat_det_set_timeouts_t_handler (vl_api_snat_det_set_timeouts_t * mp)
1079 snat_main_t *sm = &snat_main;
1080 vl_api_snat_det_set_timeouts_reply_t *rmp;
1083 sm->udp_timeout = ntohl (mp->udp);
1084 sm->tcp_established_timeout = ntohl (mp->tcp_established);
1085 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
1086 sm->icmp_timeout = ntohl (mp->icmp);
1088 REPLY_MACRO (VL_API_SNAT_DET_SET_TIMEOUTS_REPLY);
1091 static void *vl_api_snat_det_set_timeouts_t_print
1092 (vl_api_snat_det_set_timeouts_t * mp, void *handle)
1096 s = format (0, "SCRIPT: snat_det_set_timeouts ");
1097 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
1099 ntohl (mp->tcp_established),
1100 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
1106 vl_api_snat_det_get_timeouts_t_handler (vl_api_snat_det_get_timeouts_t * mp)
1108 snat_main_t *sm = &snat_main;
1109 vl_api_snat_det_get_timeouts_reply_t *rmp;
1113 REPLY_MACRO2 (VL_API_SNAT_DET_GET_TIMEOUTS_REPLY,
1115 rmp->udp = htonl (sm->udp_timeout);
1116 rmp->tcp_established = htonl (sm->tcp_established_timeout);
1117 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
1118 rmp->icmp = htonl (sm->icmp_timeout);
1123 static void *vl_api_snat_det_get_timeouts_t_print
1124 (vl_api_snat_det_get_timeouts_t * mp, void *handle)
1128 s = format (0, "SCRIPT: snat_det_get_timeouts");
1134 vl_api_snat_det_close_session_out_t_handler
1135 (vl_api_snat_det_close_session_out_t * mp)
1137 snat_main_t *sm = &snat_main;
1138 vl_api_snat_det_close_session_out_reply_t *rmp;
1139 ip4_address_t out_addr, ext_addr, in_addr;
1140 snat_det_out_key_t key;
1142 snat_det_session_t *ses;
1145 clib_memcpy (&out_addr, mp->out_addr, 4);
1146 clib_memcpy (&ext_addr, mp->ext_addr, 4);
1148 dm = snat_det_map_by_out (sm, &out_addr);
1151 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1154 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
1155 key.ext_host_addr = ext_addr;
1156 key.ext_host_port = mp->ext_port;
1157 key.out_port = mp->out_port;
1158 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
1161 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1164 snat_det_ses_close (dm, ses);
1167 REPLY_MACRO (VL_API_SNAT_DET_CLOSE_SESSION_OUT_REPLY);
1170 static void *vl_api_snat_det_close_session_out_t_print
1171 (vl_api_snat_det_close_session_out_t * mp, void *handle)
1175 s = format (0, "SCRIPT: snat_det_close_session_out ");
1176 s = format (s, "out_addr %U out_port %d "
1177 "ext_addr %U ext_port %d\n",
1178 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
1179 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1185 vl_api_snat_det_close_session_in_t_handler
1186 (vl_api_snat_det_close_session_in_t * mp)
1188 snat_main_t *sm = &snat_main;
1189 vl_api_snat_det_close_session_in_reply_t *rmp;
1190 ip4_address_t in_addr, ext_addr;
1191 snat_det_out_key_t key;
1193 snat_det_session_t *ses;
1196 clib_memcpy (&in_addr, mp->in_addr, 4);
1197 clib_memcpy (&ext_addr, mp->ext_addr, 4);
1199 dm = snat_det_map_by_user (sm, &in_addr);
1202 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1205 key.ext_host_addr = ext_addr;
1206 key.ext_host_port = mp->ext_port;
1207 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
1210 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1213 snat_det_ses_close (dm, ses);
1216 REPLY_MACRO (VL_API_SNAT_DET_CLOSE_SESSION_OUT_REPLY);
1219 static void *vl_api_snat_det_close_session_in_t_print
1220 (vl_api_snat_det_close_session_in_t * mp, void *handle)
1223 s = format (0, "SCRIPT: snat_det_close_session_in ");
1224 s = format (s, "in_addr %U in_port %d "
1225 "ext_addr %U ext_port %d\n",
1226 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
1227 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1233 send_snat_det_session_details
1234 (snat_det_session_t * s, unix_shared_memory_queue_t * q, u32 context)
1236 vl_api_snat_det_session_details_t *rmp;
1237 snat_main_t *sm = &snat_main;
1239 rmp = vl_msg_api_alloc (sizeof (*rmp));
1240 memset (rmp, 0, sizeof (*rmp));
1241 rmp->_vl_msg_id = ntohs (VL_API_SNAT_DET_SESSION_DETAILS + sm->msg_id_base);
1243 rmp->in_port = s->in_port;
1244 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
1245 rmp->ext_port = s->out.ext_host_port;
1246 rmp->out_port = s->out.out_port;
1247 rmp->state = s->state;
1248 rmp->expire = ntohl (s->expire);
1249 rmp->context = context;
1251 vl_msg_api_send_shmem (q, (u8 *) & rmp);
1255 vl_api_snat_det_session_dump_t_handler (vl_api_snat_det_session_dump_t * mp)
1257 unix_shared_memory_queue_t *q;
1258 snat_main_t *sm = &snat_main;
1259 ip4_address_t user_addr;
1261 snat_det_session_t *s, empty_ses;
1264 q = vl_api_client_index_to_input_queue (mp->client_index);
1270 memset (&empty_ses, 0, sizeof (empty_ses));
1271 clib_memcpy (&user_addr, mp->user_addr, 4);
1272 dm = snat_det_map_by_user (sm, &user_addr);
1276 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
1277 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
1280 send_snat_det_session_details (s, q, mp->context);
1285 static void *vl_api_snat_det_session_dump_t_print
1286 (vl_api_snat_det_session_dump_t * mp, void *handle)
1290 s = format (0, "SCRIPT: snat_det_session_dump ");
1291 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
1301 vl_api_nat64_add_del_pool_addr_range_t_handler
1302 (vl_api_nat64_add_del_pool_addr_range_t * mp)
1304 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
1305 snat_main_t *sm = &snat_main;
1306 nat64_main_t *nm = &nat64_main;
1308 ip4_address_t this_addr;
1309 u32 start_host_order, end_host_order;
1314 if (nm->is_disabled)
1316 rv = VNET_API_ERROR_FEATURE_DISABLED;
1320 tmp = (u32 *) mp->start_addr;
1321 start_host_order = clib_host_to_net_u32 (tmp[0]);
1322 tmp = (u32 *) mp->end_addr;
1323 end_host_order = clib_host_to_net_u32 (tmp[0]);
1325 count = (end_host_order - start_host_order) + 1;
1327 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
1329 memcpy (&this_addr.as_u8, mp->start_addr, 4);
1331 for (i = 0; i < count; i++)
1333 if ((rv = nat64_add_del_pool_addr (&this_addr, vrf_id, mp->is_add)))
1336 increment_v4_address (&this_addr);
1340 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
1343 static void *vl_api_nat64_add_del_pool_addr_range_t_print
1344 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
1348 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
1349 s = format (s, "%U - %U vrf_id %u %s\n",
1350 format_ip4_address, mp->start_addr,
1351 format_ip4_address, mp->end_addr,
1352 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
1357 typedef struct nat64_api_walk_ctx_t_
1359 unix_shared_memory_queue_t *q;
1361 } nat64_api_walk_ctx_t;
1364 nat64_api_pool_walk (snat_address_t * a, void *arg)
1366 vl_api_nat64_pool_addr_details_t *rmp;
1367 snat_main_t *sm = &snat_main;
1368 nat64_api_walk_ctx_t *ctx = arg;
1370 rmp = vl_msg_api_alloc (sizeof (*rmp));
1371 memset (rmp, 0, sizeof (*rmp));
1372 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
1373 clib_memcpy (rmp->address, &(a->addr), 4);
1374 if (a->fib_index != ~0)
1376 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
1379 rmp->vrf_id = ntohl (fib->ft_table_id);
1383 rmp->context = ctx->context;
1385 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
1391 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
1393 unix_shared_memory_queue_t *q;
1394 nat64_main_t *nm = &nat64_main;
1396 if (nm->is_disabled)
1399 q = vl_api_client_index_to_input_queue (mp->client_index);
1403 nat64_api_walk_ctx_t ctx = {
1405 .context = mp->context,
1408 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
1412 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
1417 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
1423 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
1426 snat_main_t *sm = &snat_main;
1427 nat64_main_t *nm = &nat64_main;
1428 vl_api_nat64_add_del_interface_reply_t *rmp;
1431 if (nm->is_disabled)
1433 rv = VNET_API_ERROR_FEATURE_DISABLED;
1437 VALIDATE_SW_IF_INDEX (mp);
1440 nat64_add_del_interface (ntohl (mp->sw_if_index), mp->is_inside,
1443 BAD_SW_IF_INDEX_LABEL;
1446 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
1450 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
1455 s = format (0, "SCRIPT: nat64_add_del_interface ");
1456 s = format (s, "sw_if_index %d %s %s",
1457 clib_host_to_net_u32 (mp->sw_if_index),
1458 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
1464 nat64_api_interface_walk (snat_interface_t * i, void *arg)
1466 vl_api_nat64_interface_details_t *rmp;
1467 snat_main_t *sm = &snat_main;
1468 nat64_api_walk_ctx_t *ctx = arg;
1470 rmp = vl_msg_api_alloc (sizeof (*rmp));
1471 memset (rmp, 0, sizeof (*rmp));
1472 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
1473 rmp->sw_if_index = ntohl (i->sw_if_index);
1474 rmp->is_inside = i->is_inside;
1475 rmp->context = ctx->context;
1477 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
1483 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
1485 unix_shared_memory_queue_t *q;
1486 nat64_main_t *nm = &nat64_main;
1488 if (nm->is_disabled)
1491 q = vl_api_client_index_to_input_queue (mp->client_index);
1495 nat64_api_walk_ctx_t ctx = {
1497 .context = mp->context,
1500 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
1504 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
1509 s = format (0, "SCRIPT: snat_interface_dump ");
1515 vl_api_nat64_add_del_static_bib_t_handler
1516 (vl_api_nat64_add_del_static_bib_t * mp)
1518 snat_main_t *sm = &snat_main;
1519 nat64_main_t *nm = &nat64_main;
1520 vl_api_nat64_add_del_static_bib_reply_t *rmp;
1521 ip6_address_t in_addr;
1522 ip4_address_t out_addr;
1525 if (nm->is_disabled)
1527 rv = VNET_API_ERROR_FEATURE_DISABLED;
1531 memcpy (&in_addr.as_u8, mp->i_addr, 16);
1532 memcpy (&out_addr.as_u8, mp->o_addr, 4);
1535 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
1536 clib_net_to_host_u16 (mp->i_port),
1537 clib_net_to_host_u16 (mp->o_port),
1539 clib_net_to_host_u32 (mp->vrf_id),
1543 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
1546 static void *vl_api_nat64_add_del_static_bib_t_print
1547 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
1551 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
1552 s = format (s, "protocol %d i_addr %U o_addr %U ",
1554 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
1556 if (mp->vrf_id != ~0)
1557 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1563 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
1565 vl_api_nat64_bib_details_t *rmp;
1566 snat_main_t *sm = &snat_main;
1567 nat64_api_walk_ctx_t *ctx = arg;
1570 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
1574 rmp = vl_msg_api_alloc (sizeof (*rmp));
1575 memset (rmp, 0, sizeof (*rmp));
1576 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
1577 rmp->context = ctx->context;
1578 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
1579 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
1580 rmp->i_port = bibe->in_port;
1581 rmp->o_port = bibe->out_port;
1582 rmp->vrf_id = ntohl (fib->ft_table_id);
1583 rmp->proto = snat_proto_to_ip_proto (bibe->proto);
1584 rmp->is_static = bibe->is_static;
1585 rmp->ses_num = ntohl (bibe->ses_num);
1587 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
1593 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
1595 unix_shared_memory_queue_t *q;
1596 nat64_main_t *nm = &nat64_main;
1597 snat_protocol_t proto;
1599 if (nm->is_disabled)
1602 q = vl_api_client_index_to_input_queue (mp->client_index);
1606 nat64_api_walk_ctx_t ctx = {
1608 .context = mp->context,
1611 proto = ip_proto_to_snat_proto (mp->proto);
1613 nat64_db_bib_walk (&nm->db, proto, nat64_api_bib_walk, &ctx);
1617 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
1621 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
1627 vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp)
1629 snat_main_t *sm = &snat_main;
1630 nat64_main_t *nm = &nat64_main;
1631 vl_api_nat64_set_timeouts_reply_t *rmp;
1634 if (nm->is_disabled)
1636 rv = VNET_API_ERROR_FEATURE_DISABLED;
1640 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
1643 rv = nat64_set_udp_timeout (ntohl (mp->udp));
1647 nat64_set_tcp_timeouts (ntohl (mp->tcp_trans), ntohl (mp->tcp_est),
1648 ntohl (mp->tcp_incoming_syn));
1651 REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY);
1654 static void *vl_api_nat64_set_timeouts_t_print
1655 (vl_api_nat64_set_timeouts_t * mp, void *handle)
1659 s = format (0, "SCRIPT: nat64_set_timeouts ");
1662 "udp %d icmp %d, tcp_trans %d, tcp_est %d, tcp_incoming_syn %d\n",
1663 ntohl (mp->udp), ntohl (mp->icmp), ntohl (mp->tcp_trans),
1664 ntohl (mp->tcp_est), ntohl (mp->tcp_incoming_syn));
1670 vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp)
1672 snat_main_t *sm = &snat_main;
1673 nat64_main_t *nm = &nat64_main;
1674 vl_api_nat64_get_timeouts_reply_t *rmp;
1677 if (nm->is_disabled)
1681 REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY,
1683 rmp->udp = htonl (nat64_get_udp_timeout());
1684 rmp->icmp = htonl (nat64_get_icmp_timeout());
1685 rmp->tcp_trans = htonl (nat64_get_tcp_trans_timeout());
1686 rmp->tcp_est = htonl (nat64_get_tcp_est_timeout());
1687 rmp->tcp_incoming_syn = htonl (nat64_get_tcp_incoming_syn_timeout());
1692 static void *vl_api_nat64_get_timeouts_t_print
1693 (vl_api_nat64_get_timeouts_t * mp, void *handle)
1697 s = format (0, "SCRIPT: nat64_get_timeouts");
1703 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
1705 vl_api_nat64_st_details_t *rmp;
1706 snat_main_t *sm = &snat_main;
1707 nat64_api_walk_ctx_t *ctx = arg;
1708 nat64_main_t *nm = &nat64_main;
1709 nat64_db_bib_entry_t *bibe;
1712 bibe = nat64_db_bib_entry_by_index (&nm->db, ste->proto, ste->bibe_index);
1716 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
1720 rmp = vl_msg_api_alloc (sizeof (*rmp));
1721 memset (rmp, 0, sizeof (*rmp));
1722 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
1723 rmp->context = ctx->context;
1724 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
1725 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
1726 rmp->il_port = bibe->in_port;
1727 rmp->ol_port = bibe->out_port;
1728 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
1729 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
1730 rmp->il_port = ste->r_port;
1731 rmp->vrf_id = ntohl (fib->ft_table_id);
1732 rmp->proto = snat_proto_to_ip_proto (ste->proto);
1734 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
1740 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
1742 unix_shared_memory_queue_t *q;
1743 nat64_main_t *nm = &nat64_main;
1744 snat_protocol_t proto;
1746 if (nm->is_disabled)
1749 q = vl_api_client_index_to_input_queue (mp->client_index);
1753 nat64_api_walk_ctx_t ctx = {
1755 .context = mp->context,
1758 proto = ip_proto_to_snat_proto (mp->proto);
1760 nat64_db_st_walk (&nm->db, proto, nat64_api_st_walk, &ctx);
1764 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
1768 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
1774 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
1776 vl_api_nat64_add_del_prefix_reply_t *rmp;
1777 snat_main_t *sm = &snat_main;
1778 nat64_main_t *nm = &nat64_main;
1779 ip6_address_t prefix;
1782 if (nm->is_disabled)
1784 rv = VNET_API_ERROR_FEATURE_DISABLED;
1788 memcpy (&prefix.as_u8, mp->prefix, 16);
1791 nat64_add_del_prefix (&prefix, mp->prefix_len,
1792 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
1794 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
1798 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
1803 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
1804 format_ip6_address, mp->prefix, mp->prefix_len,
1805 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
1811 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
1813 vl_api_nat64_prefix_details_t *rmp;
1814 snat_main_t *sm = &snat_main;
1815 nat64_api_walk_ctx_t *ctx = arg;
1817 rmp = vl_msg_api_alloc (sizeof (*rmp));
1818 memset (rmp, 0, sizeof (*rmp));
1819 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
1820 clib_memcpy (rmp->prefix, &(p->prefix), 16);
1821 rmp->prefix_len = p->plen;
1822 rmp->vrf_id = ntohl (p->vrf_id);
1823 rmp->context = ctx->context;
1825 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
1831 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
1833 unix_shared_memory_queue_t *q;
1834 nat64_main_t *nm = &nat64_main;
1836 if (nm->is_disabled)
1839 q = vl_api_client_index_to_input_queue (mp->client_index);
1843 nat64_api_walk_ctx_t ctx = {
1845 .context = mp->context,
1848 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
1852 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
1857 s = format (0, "SCRIPT: nat64_prefix_dump\n");
1862 /* List of message types that this plugin understands */
1863 #define foreach_snat_plugin_api_msg \
1864 _(SNAT_ADD_ADDRESS_RANGE, snat_add_address_range) \
1865 _(SNAT_INTERFACE_ADD_DEL_FEATURE, snat_interface_add_del_feature) \
1866 _(SNAT_ADD_STATIC_MAPPING, snat_add_static_mapping) \
1867 _(SNAT_CONTROL_PING, snat_control_ping) \
1868 _(SNAT_STATIC_MAPPING_DUMP, snat_static_mapping_dump) \
1869 _(SNAT_SHOW_CONFIG, snat_show_config) \
1870 _(SNAT_ADDRESS_DUMP, snat_address_dump) \
1871 _(SNAT_INTERFACE_DUMP, snat_interface_dump) \
1872 _(SNAT_SET_WORKERS, snat_set_workers) \
1873 _(SNAT_WORKER_DUMP, snat_worker_dump) \
1874 _(SNAT_ADD_DEL_INTERFACE_ADDR, snat_add_del_interface_addr) \
1875 _(SNAT_INTERFACE_ADDR_DUMP, snat_interface_addr_dump) \
1876 _(SNAT_IPFIX_ENABLE_DISABLE, snat_ipfix_enable_disable) \
1877 _(SNAT_USER_DUMP, snat_user_dump) \
1878 _(SNAT_USER_SESSION_DUMP, snat_user_session_dump) \
1879 _(SNAT_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
1880 snat_interface_add_del_output_feature) \
1881 _(SNAT_INTERFACE_OUTPUT_FEATURE_DUMP, \
1882 snat_interface_output_feature_dump) \
1883 _(SNAT_ADD_DET_MAP, snat_add_det_map) \
1884 _(SNAT_DET_FORWARD, snat_det_forward) \
1885 _(SNAT_DET_REVERSE, snat_det_reverse) \
1886 _(SNAT_DET_MAP_DUMP, snat_det_map_dump) \
1887 _(SNAT_DET_SET_TIMEOUTS, snat_det_set_timeouts) \
1888 _(SNAT_DET_GET_TIMEOUTS, snat_det_get_timeouts) \
1889 _(SNAT_DET_CLOSE_SESSION_OUT, snat_det_close_session_out) \
1890 _(SNAT_DET_CLOSE_SESSION_IN, snat_det_close_session_in) \
1891 _(SNAT_DET_SESSION_DUMP, snat_det_session_dump) \
1892 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
1893 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
1894 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
1895 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
1896 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
1897 _(NAT64_BIB_DUMP, nat64_bib_dump) \
1898 _(NAT64_SET_TIMEOUTS, nat64_set_timeouts) \
1899 _(NAT64_GET_TIMEOUTS, nat64_get_timeouts) \
1900 _(NAT64_ST_DUMP, nat64_st_dump) \
1901 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
1902 _(NAT64_PREFIX_DUMP, nat64_prefix_dump)
1904 /* Set up the API message handling tables */
1905 static clib_error_t *
1906 snat_plugin_api_hookup (vlib_main_t * vm)
1908 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
1910 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
1912 vl_api_##n##_t_handler, \
1914 vl_api_##n##_t_endian, \
1915 vl_api_##n##_t_print, \
1916 sizeof(vl_api_##n##_t), 1);
1917 foreach_snat_plugin_api_msg;
1923 #define vl_msg_name_crc_list
1924 #include <snat/snat_all_api_h.h>
1925 #undef vl_msg_name_crc_list
1928 setup_message_id_table (snat_main_t * sm, api_main_t * am)
1930 #define _(id,n,crc) \
1931 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
1932 foreach_vl_msg_name_crc_snat;
1937 plugin_custom_dump_configure (snat_main_t * sm)
1939 #define _(n,f) sm->api_main->msg_print_handlers \
1940 [VL_API_##n + sm->msg_id_base] \
1941 = (void *) vl_api_##f##_t_print;
1942 foreach_snat_plugin_api_msg;
1947 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
1950 clib_error_t *error = 0;
1952 name = format (0, "snat_%08x%c", api_version, 0);
1954 /* Ask for a correctly-sized block of API message decode slots */
1956 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
1958 error = snat_plugin_api_hookup (vm);
1960 /* Add our API messages to the global name_crc hash table */
1961 setup_message_id_table (sm, sm->api_main);
1963 plugin_custom_dump_configure (sm);
1971 * fd.io coding-style-patch-verification: ON
1974 * eval: (c-set-style "gnu")