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 NAT plugin API implementation
22 #include <nat/nat_det.h>
23 #include <nat/nat64.h>
24 #include <nat/nat66.h>
25 #include <nat/dslite.h>
26 #include <nat/nat_reass.h>
27 #include <vlibapi/api.h>
28 #include <vlibmemory/api.h>
30 #include <nat/nat_msg_enum.h>
31 #include <vnet/fib/fib_table.h>
33 #define vl_api_nat44_lb_addr_port_t_endian vl_noop_handler
34 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
35 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
37 /* define message structures */
39 #include <nat/nat_all_api_h.h>
42 /* define generated endian-swappers */
44 #include <nat/nat_all_api_h.h>
47 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49 #define REPLY_MSG_ID_BASE sm->msg_id_base
50 #include <vlibapi/api_helper_macros.h>
52 /* Get the API version number */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <nat/nat_all_api_h.h>
57 /* Macro to finish up custom dump fns */
60 vl_print (handle, (char *)s); \
65 /******************************/
66 /*** Common NAT plugin APIs ***/
67 /******************************/
70 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
72 vl_api_nat_control_ping_reply_t *rmp;
73 snat_main_t *sm = &snat_main;
77 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
79 rmp->vpe_pid = ntohl (getpid ());
85 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
89 s = format (0, "SCRIPT: nat_control_ping ");
95 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
97 vl_api_nat_show_config_reply_t *rmp;
98 snat_main_t *sm = &snat_main;
102 REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
104 rmp->translation_buckets = htonl (sm->translation_buckets);
105 rmp->translation_memory_size = htonl (sm->translation_memory_size);
106 rmp->user_buckets = htonl (sm->user_buckets);
107 rmp->user_memory_size = htonl (sm->user_memory_size);
108 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
109 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
110 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
111 rmp->static_mapping_only = sm->static_mapping_only;
112 rmp->static_mapping_connection_tracking =
113 sm->static_mapping_connection_tracking;
114 rmp->deterministic = sm->deterministic;
120 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
124 s = format (0, "SCRIPT: nat_show_config ");
130 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
132 snat_main_t *sm = &snat_main;
133 vl_api_nat_set_workers_reply_t *rmp;
138 if (sm->deterministic)
140 rv = VNET_API_ERROR_UNSUPPORTED;
144 mask = clib_net_to_host_u64 (mp->worker_mask);
146 if (sm->num_workers < 2)
148 rv = VNET_API_ERROR_FEATURE_DISABLED;
152 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
153 rv = snat_set_workers (bitmap);
154 clib_bitmap_free (bitmap);
157 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
161 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
167 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
169 s = format (0, "SCRIPT: nat_set_workers ");
170 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
172 clib_bitmap_foreach (i, bitmap,
175 s = format (s, "%d", i);
177 s = format (s, ",%d", i);
181 clib_bitmap_free (bitmap);
186 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
189 vl_api_nat_worker_details_t *rmp;
190 snat_main_t *sm = &snat_main;
191 vlib_worker_thread_t *w =
192 vlib_worker_threads + worker_index + sm->first_worker_index;
194 rmp = vl_msg_api_alloc (sizeof (*rmp));
195 memset (rmp, 0, sizeof (*rmp));
196 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
197 rmp->context = context;
198 rmp->worker_index = htonl (worker_index);
199 rmp->lcore_id = htonl (w->lcore_id);
200 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
202 vl_api_send_msg (reg, (u8 *) rmp);
206 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
208 vl_api_registration_t *reg;
209 snat_main_t *sm = &snat_main;
212 if (sm->deterministic)
215 reg = vl_api_client_index_to_registration (mp->client_index);
220 vec_foreach (worker_index, sm->workers)
221 send_nat_worker_details(*worker_index, reg, mp->context);
226 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
230 s = format (0, "SCRIPT: nat_worker_dump ");
236 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
239 snat_main_t *sm = &snat_main;
240 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
243 rv = snat_ipfix_logging_enable_disable (mp->enable,
249 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
253 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
258 s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
260 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
262 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
264 s = format (s, "disable ");
270 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
272 snat_main_t *sm = &snat_main;
273 vl_api_nat_set_reass_reply_t *rmp;
277 nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
278 mp->drop_frag, mp->is_ip6);
280 REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
284 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
288 s = format (0, "SCRIPT: nat_set_reass ");
289 s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
290 clib_host_to_net_u32 (mp->timeout),
291 clib_host_to_net_u16 (mp->max_reass),
292 mp->max_frag, mp->drop_frag, mp->is_ip6);
298 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
300 snat_main_t *sm = &snat_main;
301 vl_api_nat_get_reass_reply_t *rmp;
305 REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
307 rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
308 rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
309 rmp->ip4_max_frag = nat_reass_get_max_frag(0);
310 rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
311 rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
312 rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
313 rmp->ip6_max_frag = nat_reass_get_max_frag(1);
314 rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
320 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
324 s = format (0, "SCRIPT: nat_get_reass");
329 typedef struct nat_api_walk_ctx_t_
331 vl_api_registration_t *reg;
333 } nat_api_walk_ctx_t;
336 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
338 vl_api_nat_reass_details_t *rmp;
339 snat_main_t *sm = &snat_main;
340 nat_api_walk_ctx_t *ctx = arg;
342 rmp = vl_msg_api_alloc (sizeof (*rmp));
343 memset (rmp, 0, sizeof (*rmp));
344 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
345 rmp->context = ctx->context;
346 clib_memcpy (rmp->src_addr, &(reass->key.src), 4);
347 clib_memcpy (rmp->dst_addr, &(reass->key.dst), 4);
348 rmp->proto = reass->key.proto;
349 rmp->frag_id = ntohl (reass->key.frag_id);
350 rmp->frag_n = reass->frag_n;
353 vl_api_send_msg (ctx->reg, (u8 *) rmp);
359 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
361 vl_api_nat_reass_details_t *rmp;
362 snat_main_t *sm = &snat_main;
363 nat_api_walk_ctx_t *ctx = arg;
365 rmp = vl_msg_api_alloc (sizeof (*rmp));
366 memset (rmp, 0, sizeof (*rmp));
367 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
368 rmp->context = ctx->context;
369 clib_memcpy (rmp->src_addr, &(reass->key.src), 16);
370 clib_memcpy (rmp->dst_addr, &(reass->key.dst), 16);
371 rmp->proto = reass->key.proto;
372 rmp->frag_id = ntohl (reass->key.frag_id);
373 rmp->frag_n = reass->frag_n;
376 vl_api_send_msg (ctx->reg, (u8 *) rmp);
382 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
384 vl_api_registration_t *reg;
386 reg = vl_api_client_index_to_registration (mp->client_index);
390 nat_api_walk_ctx_t ctx = {
392 .context = mp->context,
395 nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
396 nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
400 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
404 s = format (0, "SCRIPT: nat_reass_dump");
413 vl_api_nat44_add_del_address_range_t_handler
414 (vl_api_nat44_add_del_address_range_t * mp)
416 snat_main_t *sm = &snat_main;
417 vl_api_nat44_add_del_address_range_reply_t *rmp;
418 ip4_address_t this_addr;
419 u32 start_host_order, end_host_order;
425 if (sm->deterministic)
427 rv = VNET_API_ERROR_UNSUPPORTED;
431 if (sm->static_mapping_only)
433 rv = VNET_API_ERROR_FEATURE_DISABLED;
437 tmp = (u32 *) mp->first_ip_address;
438 start_host_order = clib_host_to_net_u32 (tmp[0]);
439 tmp = (u32 *) mp->last_ip_address;
440 end_host_order = clib_host_to_net_u32 (tmp[0]);
442 count = (end_host_order - start_host_order) + 1;
444 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
447 clib_warning ("%U - %U, %d addresses...",
448 format_ip4_address, mp->first_ip_address,
449 format_ip4_address, mp->last_ip_address, count);
451 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
453 for (i = 0; i < count; i++)
456 snat_add_address (sm, &this_addr, vrf_id, mp->twice_nat);
458 rv = snat_del_address (sm, this_addr, 0, mp->twice_nat);
464 nat44_add_del_address_dpo (this_addr, mp->is_add);
466 increment_v4_address (&this_addr);
470 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
473 static void *vl_api_nat44_add_del_address_range_t_print
474 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
478 s = format (0, "SCRIPT: nat44_add_address_range ");
479 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
480 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
482 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
484 s = format (s, "twice_nat %d ", mp->twice_nat);
489 send_nat44_address_details (snat_address_t * a,
490 vl_api_registration_t * reg, u32 context,
493 vl_api_nat44_address_details_t *rmp;
494 snat_main_t *sm = &snat_main;
496 rmp = vl_msg_api_alloc (sizeof (*rmp));
497 memset (rmp, 0, sizeof (*rmp));
498 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
499 clib_memcpy (rmp->ip_address, &(a->addr), 4);
500 if (a->fib_index != ~0)
502 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
503 rmp->vrf_id = ntohl (fib->ft_table_id);
507 rmp->twice_nat = twice_nat;
508 rmp->context = context;
510 vl_api_send_msg (reg, (u8 *) rmp);
514 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
516 vl_api_registration_t *reg;
517 snat_main_t *sm = &snat_main;
520 if (sm->deterministic)
523 reg = vl_api_client_index_to_registration (mp->client_index);
528 vec_foreach (a, sm->addresses)
529 send_nat44_address_details (a, reg, mp->context, 0);
530 vec_foreach (a, sm->twice_nat_addresses)
531 send_nat44_address_details (a, reg, mp->context, 1);
536 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
541 s = format (0, "SCRIPT: nat44_address_dump ");
547 vl_api_nat44_interface_add_del_feature_t_handler
548 (vl_api_nat44_interface_add_del_feature_t * mp)
550 snat_main_t *sm = &snat_main;
551 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
552 u8 is_del = mp->is_add == 0;
553 u32 sw_if_index = ntohl (mp->sw_if_index);
556 VALIDATE_SW_IF_INDEX (mp);
558 rv = snat_interface_add_del (sw_if_index, mp->is_inside, is_del);
560 BAD_SW_IF_INDEX_LABEL;
562 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
565 static void *vl_api_nat44_interface_add_del_feature_t_print
566 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
570 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
571 s = format (s, "sw_if_index %d %s %s",
572 clib_host_to_net_u32 (mp->sw_if_index),
573 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
579 send_nat44_interface_details (snat_interface_t * i,
580 vl_api_registration_t * reg, u32 context)
582 vl_api_nat44_interface_details_t *rmp;
583 snat_main_t *sm = &snat_main;
585 rmp = vl_msg_api_alloc (sizeof (*rmp));
586 memset (rmp, 0, sizeof (*rmp));
587 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
588 rmp->sw_if_index = ntohl (i->sw_if_index);
589 rmp->is_inside = (nat_interface_is_inside (i)
590 && nat_interface_is_outside (i)) ? 2 :
591 nat_interface_is_inside (i);
592 rmp->context = context;
594 vl_api_send_msg (reg, (u8 *) rmp);
598 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
600 vl_api_registration_t *reg;
601 snat_main_t *sm = &snat_main;
604 reg = vl_api_client_index_to_registration (mp->client_index);
609 pool_foreach (i, sm->interfaces,
611 send_nat44_interface_details(i, reg, mp->context);
617 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
622 s = format (0, "SCRIPT: nat44_interface_dump ");
628 vl_api_nat44_interface_add_del_output_feature_t_handler
629 (vl_api_nat44_interface_add_del_output_feature_t * mp)
631 snat_main_t *sm = &snat_main;
632 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
633 u8 is_del = mp->is_add == 0;
634 u32 sw_if_index = ntohl (mp->sw_if_index);
637 if (sm->deterministic)
639 rv = VNET_API_ERROR_UNSUPPORTED;
643 VALIDATE_SW_IF_INDEX (mp);
645 rv = snat_interface_add_del_output_feature (sw_if_index, mp->is_inside,
648 BAD_SW_IF_INDEX_LABEL;
650 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
653 static void *vl_api_nat44_interface_add_del_output_feature_t_print
654 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
658 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
659 s = format (s, "sw_if_index %d %s %s",
660 clib_host_to_net_u32 (mp->sw_if_index),
661 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
667 send_nat44_interface_output_feature_details (snat_interface_t * i,
668 vl_api_registration_t * reg,
671 vl_api_nat44_interface_output_feature_details_t *rmp;
672 snat_main_t *sm = &snat_main;
674 rmp = vl_msg_api_alloc (sizeof (*rmp));
675 memset (rmp, 0, sizeof (*rmp));
677 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
678 rmp->sw_if_index = ntohl (i->sw_if_index);
679 rmp->context = context;
680 rmp->is_inside = nat_interface_is_inside (i);
682 vl_api_send_msg (reg, (u8 *) rmp);
686 vl_api_nat44_interface_output_feature_dump_t_handler
687 (vl_api_nat44_interface_output_feature_dump_t * mp)
689 vl_api_registration_t *reg;
690 snat_main_t *sm = &snat_main;
693 if (sm->deterministic)
696 reg = vl_api_client_index_to_registration (mp->client_index);
701 pool_foreach (i, sm->output_feature_interfaces,
703 send_nat44_interface_output_feature_details(i, reg, mp->context);
708 static void *vl_api_nat44_interface_output_feature_dump_t_print
709 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
713 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
719 vl_api_nat44_add_del_static_mapping_t_handler
720 (vl_api_nat44_add_del_static_mapping_t * mp)
722 snat_main_t *sm = &snat_main;
723 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
724 ip4_address_t local_addr, external_addr;
725 u16 local_port = 0, external_port = 0;
726 u32 vrf_id, external_sw_if_index;
727 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
729 snat_protocol_t proto;
732 if (sm->deterministic)
734 rv = VNET_API_ERROR_UNSUPPORTED;
738 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
739 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
740 if (mp->addr_only == 0)
742 local_port = clib_net_to_host_u16 (mp->local_port);
743 external_port = clib_net_to_host_u16 (mp->external_port);
745 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
746 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
747 proto = ip_proto_to_snat_proto (mp->protocol);
749 twice_nat = TWICE_NAT;
750 else if (mp->self_twice_nat)
751 twice_nat = TWICE_NAT_SELF;
752 mp->tag[sizeof (mp->tag) - 1] = 0;
753 tag = format (0, "%s", mp->tag);
754 vec_terminate_c_string (tag);
756 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
757 external_port, vrf_id, mp->addr_only,
758 external_sw_if_index, proto, mp->is_add,
759 twice_nat, mp->out2in_only, tag);
764 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
767 static void *vl_api_nat44_add_del_static_mapping_t_print
768 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
772 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
773 s = format (s, "protocol %d local_addr %U external_addr %U ",
775 format_ip4_address, mp->local_ip_address,
776 format_ip4_address, mp->external_ip_address);
778 if (mp->addr_only == 0)
779 s = format (s, "local_port %d external_port %d ",
780 clib_net_to_host_u16 (mp->local_port),
781 clib_net_to_host_u16 (mp->external_port));
783 s = format (s, "twice_nat %d out2in_only %d ",
784 mp->twice_nat, mp->out2in_only);
786 if (mp->vrf_id != ~0)
787 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
789 if (mp->external_sw_if_index != ~0)
790 s = format (s, "external_sw_if_index %d",
791 clib_net_to_host_u32 (mp->external_sw_if_index));
796 send_nat44_static_mapping_details (snat_static_mapping_t * m,
797 vl_api_registration_t * reg, u32 context)
799 vl_api_nat44_static_mapping_details_t *rmp;
800 snat_main_t *sm = &snat_main;
802 rmp = vl_msg_api_alloc (sizeof (*rmp));
803 memset (rmp, 0, sizeof (*rmp));
805 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
806 rmp->addr_only = m->addr_only;
807 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
808 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
809 rmp->external_sw_if_index = ~0;
810 rmp->vrf_id = htonl (m->vrf_id);
811 rmp->context = context;
812 if (m->twice_nat == TWICE_NAT)
814 else if (m->twice_nat == TWICE_NAT_SELF)
815 rmp->self_twice_nat = 1;
816 rmp->out2in_only = m->out2in_only;
817 if (m->addr_only == 0)
819 rmp->protocol = snat_proto_to_ip_proto (m->proto);
820 rmp->external_port = htons (m->external_port);
821 rmp->local_port = htons (m->local_port);
824 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
826 vl_api_send_msg (reg, (u8 *) rmp);
830 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
831 vl_api_registration_t * reg,
834 vl_api_nat44_static_mapping_details_t *rmp;
835 snat_main_t *sm = &snat_main;
837 rmp = vl_msg_api_alloc (sizeof (*rmp));
838 memset (rmp, 0, sizeof (*rmp));
840 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
841 rmp->addr_only = m->addr_only;
842 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
843 rmp->external_sw_if_index = htonl (m->sw_if_index);
844 rmp->vrf_id = htonl (m->vrf_id);
845 rmp->context = context;
846 rmp->twice_nat = m->twice_nat;
847 if (m->addr_only == 0)
849 rmp->protocol = snat_proto_to_ip_proto (m->proto);
850 rmp->external_port = htons (m->e_port);
851 rmp->local_port = htons (m->l_port);
854 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
856 vl_api_send_msg (reg, (u8 *) rmp);
860 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
863 vl_api_registration_t *reg;
864 snat_main_t *sm = &snat_main;
865 snat_static_mapping_t *m;
866 snat_static_map_resolve_t *rp;
869 if (sm->deterministic)
872 reg = vl_api_client_index_to_registration (mp->client_index);
877 pool_foreach (m, sm->static_mappings,
879 if (!vec_len (m->locals) &&
880 ((m->local_port != m->external_port)
881 || (m->local_addr.as_u32 != m->external_addr.as_u32)))
882 send_nat44_static_mapping_details (m, reg, mp->context);
886 for (j = 0; j < vec_len (sm->to_resolve); j++)
888 rp = sm->to_resolve + j;
889 if (rp->l_addr.as_u32 != 0)
890 send_nat44_static_map_resolve_details (rp, reg, mp->context);
895 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
900 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
906 vl_api_nat44_add_del_identity_mapping_t_handler
907 (vl_api_nat44_add_del_identity_mapping_t * mp)
909 snat_main_t *sm = &snat_main;
910 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
913 u32 vrf_id, sw_if_index;
915 snat_protocol_t proto = ~0;
918 if (sm->deterministic)
920 rv = VNET_API_ERROR_UNSUPPORTED;
924 if (mp->addr_only == 0)
926 port = clib_net_to_host_u16 (mp->port);
927 proto = ip_proto_to_snat_proto (mp->protocol);
929 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
930 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
931 if (sw_if_index != ~0)
934 memcpy (&addr.as_u8, mp->ip_address, 4);
935 mp->tag[sizeof (mp->tag) - 1] = 0;
936 tag = format (0, "%s", mp->tag);
937 vec_terminate_c_string (tag);
940 snat_add_static_mapping (addr, addr, port, port, vrf_id, mp->addr_only,
941 sw_if_index, proto, mp->is_add, 0, 0, tag);
946 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
949 static void *vl_api_nat44_add_del_identity_mapping_t_print
950 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
954 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
955 if (mp->sw_if_index != ~0)
956 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
958 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
960 if (mp->addr_only == 0)
962 format (s, "protocol %d port %d", mp->protocol,
963 clib_net_to_host_u16 (mp->port));
965 if (mp->vrf_id != ~0)
966 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
972 send_nat44_identity_mapping_details (snat_static_mapping_t * m,
973 vl_api_registration_t * reg, u32 context)
975 vl_api_nat44_identity_mapping_details_t *rmp;
976 snat_main_t *sm = &snat_main;
978 rmp = vl_msg_api_alloc (sizeof (*rmp));
979 memset (rmp, 0, sizeof (*rmp));
981 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
982 rmp->addr_only = m->addr_only;
983 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
984 rmp->port = htons (m->local_port);
985 rmp->sw_if_index = ~0;
986 rmp->vrf_id = htonl (m->vrf_id);
987 rmp->protocol = snat_proto_to_ip_proto (m->proto);
988 rmp->context = context;
990 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
992 vl_api_send_msg (reg, (u8 *) rmp);
996 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
997 vl_api_registration_t * reg,
1000 vl_api_nat44_identity_mapping_details_t *rmp;
1001 snat_main_t *sm = &snat_main;
1003 rmp = vl_msg_api_alloc (sizeof (*rmp));
1004 memset (rmp, 0, sizeof (*rmp));
1006 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1007 rmp->addr_only = m->addr_only;
1008 rmp->port = htons (m->l_port);
1009 rmp->sw_if_index = htonl (m->sw_if_index);
1010 rmp->vrf_id = htonl (m->vrf_id);
1011 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1012 rmp->context = context;
1014 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1016 vl_api_send_msg (reg, (u8 *) rmp);
1020 vl_api_nat44_identity_mapping_dump_t_handler
1021 (vl_api_nat44_identity_mapping_dump_t * mp)
1023 vl_api_registration_t *reg;
1024 snat_main_t *sm = &snat_main;
1025 snat_static_mapping_t *m;
1026 snat_static_map_resolve_t *rp;
1029 if (sm->deterministic)
1032 reg = vl_api_client_index_to_registration (mp->client_index);
1037 pool_foreach (m, sm->static_mappings,
1039 if (!vec_len (m->locals) && (m->local_port == m->external_port)
1040 && (m->local_addr.as_u32 == m->external_addr.as_u32))
1041 send_nat44_identity_mapping_details (m, reg, mp->context);
1045 for (j = 0; j < vec_len (sm->to_resolve); j++)
1047 rp = sm->to_resolve + j;
1048 if (rp->l_addr.as_u32 == 0)
1049 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1053 static void *vl_api_nat44_identity_mapping_dump_t_print
1054 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1058 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1064 vl_api_nat44_add_del_interface_addr_t_handler
1065 (vl_api_nat44_add_del_interface_addr_t * mp)
1067 snat_main_t *sm = &snat_main;
1068 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1069 u8 is_del = mp->is_add == 0;
1070 u32 sw_if_index = ntohl (mp->sw_if_index);
1073 if (sm->deterministic)
1075 rv = VNET_API_ERROR_UNSUPPORTED;
1079 VALIDATE_SW_IF_INDEX (mp);
1081 rv = snat_add_interface_address (sm, sw_if_index, is_del, mp->twice_nat);
1083 BAD_SW_IF_INDEX_LABEL;
1085 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1088 static void *vl_api_nat44_add_del_interface_addr_t_print
1089 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1093 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1094 s = format (s, "sw_if_index %d twice_nat %d %s",
1095 clib_host_to_net_u32 (mp->sw_if_index),
1096 mp->twice_nat, mp->is_add ? "" : "del");
1102 send_nat44_interface_addr_details (u32 sw_if_index,
1103 vl_api_registration_t * reg, u32 context,
1106 vl_api_nat44_interface_addr_details_t *rmp;
1107 snat_main_t *sm = &snat_main;
1109 rmp = vl_msg_api_alloc (sizeof (*rmp));
1110 memset (rmp, 0, sizeof (*rmp));
1112 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1113 rmp->sw_if_index = ntohl (sw_if_index);
1114 rmp->twice_nat = twice_nat;
1115 rmp->context = context;
1117 vl_api_send_msg (reg, (u8 *) rmp);
1121 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1124 vl_api_registration_t *reg;
1125 snat_main_t *sm = &snat_main;
1128 if (sm->deterministic)
1131 reg = vl_api_client_index_to_registration (mp->client_index);
1136 vec_foreach (i, sm->auto_add_sw_if_indices)
1137 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1138 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1139 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1144 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1149 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1155 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1158 vl_api_nat44_user_details_t *rmp;
1159 snat_main_t *sm = &snat_main;
1160 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1162 rmp = vl_msg_api_alloc (sizeof (*rmp));
1163 memset (rmp, 0, sizeof (*rmp));
1164 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1166 rmp->vrf_id = ntohl (fib->ft_table_id);
1168 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1169 rmp->nsessions = ntohl (u->nsessions);
1170 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1171 rmp->context = context;
1173 vl_api_send_msg (reg, (u8 *) rmp);
1177 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1179 vl_api_registration_t *reg;
1180 snat_main_t *sm = &snat_main;
1181 snat_main_per_thread_data_t *tsm;
1184 if (sm->deterministic)
1187 reg = vl_api_client_index_to_registration (mp->client_index);
1192 vec_foreach (tsm, sm->per_thread_data)
1193 vec_foreach (u, tsm->users)
1194 send_nat44_user_details (u, reg, mp->context);
1199 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1203 s = format (0, "SCRIPT: nat44_user_dump ");
1209 send_nat44_user_session_details (snat_session_t * s,
1210 vl_api_registration_t * reg, u32 context)
1212 vl_api_nat44_user_session_details_t *rmp;
1213 snat_main_t *sm = &snat_main;
1215 rmp = vl_msg_api_alloc (sizeof (*rmp));
1216 memset (rmp, 0, sizeof (*rmp));
1218 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1219 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1220 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1221 rmp->is_static = s->flags & SNAT_SESSION_FLAG_STATIC_MAPPING ? 1 : 0;
1222 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1223 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1224 rmp->total_pkts = ntohl (s->total_pkts);
1225 rmp->context = context;
1226 if (snat_is_unk_proto_session (s))
1228 rmp->outside_port = 0;
1229 rmp->inside_port = 0;
1230 rmp->protocol = ntohs (s->in2out.port);
1234 rmp->outside_port = s->out2in.port;
1235 rmp->inside_port = s->in2out.port;
1236 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1238 if (s->in2out.protocol == SNAT_PROTOCOL_TCP)
1239 rmp->is_closed = s->state == SNAT_SESSION_TCP_CLOSED ? 1 : 0;
1241 vl_api_send_msg (reg, (u8 *) rmp);
1245 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1248 vl_api_registration_t *reg;
1249 snat_main_t *sm = &snat_main;
1250 snat_main_per_thread_data_t *tsm;
1252 clib_bihash_kv_8_8_t key, value;
1253 snat_user_key_t ukey;
1255 u32 session_index, head_index, elt_index;
1256 dlist_elt_t *head, *elt;
1259 if (sm->deterministic)
1262 reg = vl_api_client_index_to_registration (mp->client_index);
1266 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1267 ip.src_address.as_u32 = ukey.addr.as_u32;
1268 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1269 key.key = ukey.as_u64;
1270 if (sm->num_workers > 1)
1272 vec_elt_at_index (sm->per_thread_data,
1273 sm->worker_in2out_cb (&ip, ukey.fib_index));
1275 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1276 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1278 u = pool_elt_at_index (tsm->users, value.value);
1279 if (!u->nsessions && !u->nstaticsessions)
1282 head_index = u->sessions_per_user_list_head_index;
1283 head = pool_elt_at_index (tsm->list_pool, head_index);
1284 elt_index = head->next;
1285 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1286 session_index = elt->value;
1287 while (session_index != ~0)
1289 s = pool_elt_at_index (tsm->sessions, session_index);
1291 send_nat44_user_session_details (s, reg, mp->context);
1293 elt_index = elt->next;
1294 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1295 session_index = elt->value;
1300 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1305 s = format (0, "SCRIPT: nat44_user_session_dump ");
1306 s = format (s, "ip_address %U vrf_id %d\n",
1307 format_ip4_address, mp->ip_address,
1308 clib_net_to_host_u32 (mp->vrf_id));
1313 static nat44_lb_addr_port_t *
1314 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1315 u8 addr_port_pair_num)
1318 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1319 vl_api_nat44_lb_addr_port_t *ap;
1321 for (i = 0; i < addr_port_pair_num; i++)
1323 ap = &addr_port_pairs[i];
1324 memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1325 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1326 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1327 lb_addr_port.probability = ap->probability;
1328 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1331 return lb_addr_port_pairs;
1335 vl_api_nat44_add_del_lb_static_mapping_t_handler
1336 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1338 snat_main_t *sm = &snat_main;
1339 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1340 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1342 nat44_lb_addr_port_t *locals = 0;
1343 ip4_address_t e_addr;
1344 snat_protocol_t proto;
1347 if (sm->deterministic)
1349 rv = VNET_API_ERROR_UNSUPPORTED;
1353 locals = unformat_nat44_lb_addr_port (mp->locals, mp->local_num);
1354 clib_memcpy (&e_addr, mp->external_addr, 4);
1355 proto = ip_proto_to_snat_proto (mp->protocol);
1357 twice_nat = TWICE_NAT;
1358 else if (mp->self_twice_nat)
1359 twice_nat = TWICE_NAT_SELF;
1360 mp->tag[sizeof (mp->tag) - 1] = 0;
1361 tag = format (0, "%s", mp->tag);
1362 vec_terminate_c_string (tag);
1365 nat44_add_del_lb_static_mapping (e_addr,
1366 clib_net_to_host_u16 (mp->external_port),
1367 proto, clib_net_to_host_u32 (mp->vrf_id),
1368 locals, mp->is_add, twice_nat,
1369 mp->out2in_only, tag);
1375 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1378 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1379 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1383 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1384 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1385 mp->is_add, mp->twice_nat, mp->out2in_only);
1391 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1392 vl_api_registration_t * reg,
1395 vl_api_nat44_lb_static_mapping_details_t *rmp;
1396 snat_main_t *sm = &snat_main;
1397 nat44_lb_addr_port_t *ap;
1398 vl_api_nat44_lb_addr_port_t *locals;
1401 vl_msg_api_alloc (sizeof (*rmp) +
1402 (vec_len (m->locals) * sizeof (nat44_lb_addr_port_t)));
1403 memset (rmp, 0, sizeof (*rmp));
1405 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1407 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1408 rmp->external_port = ntohs (m->external_port);
1409 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1410 rmp->vrf_id = ntohl (m->vrf_id);
1411 rmp->context = context;
1412 if (m->twice_nat == TWICE_NAT)
1414 else if (m->twice_nat == TWICE_NAT_SELF)
1415 rmp->self_twice_nat = 1;
1416 rmp->out2in_only = m->out2in_only;
1418 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1420 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1421 vec_foreach (ap, m->locals)
1423 clib_memcpy (locals->addr, &(ap->addr), 4);
1424 locals->port = htons (ap->port);
1425 locals->probability = ap->probability;
1430 vl_api_send_msg (reg, (u8 *) rmp);
1434 vl_api_nat44_lb_static_mapping_dump_t_handler
1435 (vl_api_nat44_lb_static_mapping_dump_t * mp)
1437 vl_api_registration_t *reg;
1438 snat_main_t *sm = &snat_main;
1439 snat_static_mapping_t *m;
1441 if (sm->deterministic)
1444 reg = vl_api_client_index_to_registration (mp->client_index);
1449 pool_foreach (m, sm->static_mappings,
1451 if (vec_len(m->locals))
1452 send_nat44_lb_static_mapping_details (m, reg, mp->context);
1457 static void *vl_api_nat44_lb_static_mapping_dump_t_print
1458 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
1462 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
1468 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1470 snat_main_t *sm = &snat_main;
1471 vl_api_nat44_del_session_reply_t *rmp;
1476 snat_protocol_t proto;
1478 if (sm->deterministic)
1480 rv = VNET_API_ERROR_UNSUPPORTED;
1484 memcpy (&addr.as_u8, mp->address, 4);
1485 port = clib_net_to_host_u16 (mp->port);
1486 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1487 proto = ip_proto_to_snat_proto (mp->protocol);
1489 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, mp->is_in);
1492 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1496 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
1501 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1502 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
1503 format_ip4_address, mp->address,
1504 clib_net_to_host_u16 (mp->port),
1505 mp->protocol, clib_net_to_host_u32 (mp->vrf_id), mp->is_in);
1511 vl_api_nat44_forwarding_enable_disable_t_handler
1512 (vl_api_nat44_forwarding_enable_disable_t * mp)
1514 snat_main_t *sm = &snat_main;
1515 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1518 sm->forwarding_enabled = mp->enable != 0;
1520 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1523 static void *vl_api_nat44_forwarding_enable_disable_t_print
1524 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
1528 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
1529 s = format (s, "enable %d", mp->enable != 0);
1535 vl_api_nat44_forwarding_is_enabled_t_handler
1536 (vl_api_nat44_forwarding_is_enabled_t * mp)
1538 vl_api_registration_t *reg;
1539 snat_main_t *sm = &snat_main;
1540 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1542 reg = vl_api_client_index_to_registration (mp->client_index);
1546 rmp = vl_msg_api_alloc (sizeof (*rmp));
1547 memset (rmp, 0, sizeof (*rmp));
1549 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1550 rmp->context = mp->context;
1552 rmp->enabled = sm->forwarding_enabled;
1554 vl_api_send_msg (reg, (u8 *) rmp);
1557 static void *vl_api_nat44_forwarding_is_enabled_t_print
1558 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
1562 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
1567 /*******************************/
1568 /*** Deterministic NAT (CGN) ***/
1569 /*******************************/
1572 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
1574 snat_main_t *sm = &snat_main;
1575 vl_api_nat_det_add_del_map_reply_t *rmp;
1577 ip4_address_t in_addr, out_addr;
1579 if (!sm->deterministic)
1581 rv = VNET_API_ERROR_UNSUPPORTED;
1587 rv = VNET_API_ERROR_UNIMPLEMENTED;
1591 clib_memcpy (&in_addr, mp->in_addr, 4);
1592 clib_memcpy (&out_addr, mp->out_addr, 4);
1593 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
1594 mp->out_plen, mp->is_add);
1597 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
1601 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
1606 s = format (0, "SCRIPT: nat_det_add_del_map ");
1607 s = format (s, "inside address %U/%d outside address %U/%d\n",
1608 format_ip4_address, mp->in_addr, mp->in_plen,
1609 format_ip4_address, mp->out_addr, mp->out_plen);
1615 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
1617 snat_main_t *sm = &snat_main;
1618 vl_api_nat_det_forward_reply_t *rmp;
1620 u16 lo_port = 0, hi_port = 0;
1622 ip4_address_t in_addr, out_addr;
1624 if (!sm->deterministic)
1626 rv = VNET_API_ERROR_UNSUPPORTED;
1627 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
1633 out_addr.as_u32 = 0;
1634 rv = VNET_API_ERROR_UNIMPLEMENTED;
1638 out_addr.as_u32 = 0;
1639 clib_memcpy (&in_addr, mp->in_addr, 4);
1640 dm = snat_det_map_by_user (sm, &in_addr);
1643 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1647 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
1648 hi_port = lo_port + dm->ports_per_host - 1;
1652 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
1654 rmp->out_port_lo = ntohs (lo_port);
1655 rmp->out_port_hi = ntohs (hi_port);
1656 clib_memcpy (rmp->out_addr, &out_addr, 4);
1662 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
1666 s = format (0, "SCRIPT: nat_det_forward");
1667 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
1673 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
1675 snat_main_t *sm = &snat_main;
1676 vl_api_nat_det_reverse_reply_t *rmp;
1678 ip4_address_t out_addr, in_addr;
1681 if (!sm->deterministic)
1683 rv = VNET_API_ERROR_UNSUPPORTED;
1684 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
1689 clib_memcpy (&out_addr, mp->out_addr, 4);
1690 dm = snat_det_map_by_out (sm, &out_addr);
1693 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1697 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
1701 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
1704 memset (rmp->in_addr, 0, 16);
1705 clib_memcpy (rmp->in_addr, &in_addr, 4);
1711 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
1715 s = format (0, "SCRIPT: nat_det_reverse");
1716 s = format (s, "outside ip address %U outside port %d",
1717 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
1723 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
1726 vl_api_nat_det_map_details_t *rmp;
1727 snat_main_t *sm = &snat_main;
1729 rmp = vl_msg_api_alloc (sizeof (*rmp));
1730 memset (rmp, 0, sizeof (*rmp));
1731 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
1733 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
1734 rmp->in_plen = m->in_plen;
1735 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
1736 rmp->out_plen = m->out_plen;
1737 rmp->sharing_ratio = htonl (m->sharing_ratio);
1738 rmp->ports_per_host = htons (m->ports_per_host);
1739 rmp->ses_num = htonl (m->ses_num);
1740 rmp->context = context;
1742 vl_api_send_msg (reg, (u8 *) rmp);
1746 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
1748 vl_api_registration_t *reg;
1749 snat_main_t *sm = &snat_main;
1752 if (!sm->deterministic)
1755 reg = vl_api_client_index_to_registration (mp->client_index);
1760 vec_foreach(m, sm->det_maps)
1761 sent_nat_det_map_details(m, reg, mp->context);
1766 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
1770 s = format (0, "SCRIPT: nat_det_map_dump ");
1776 vl_api_nat_det_set_timeouts_t_handler (vl_api_nat_det_set_timeouts_t * mp)
1778 snat_main_t *sm = &snat_main;
1779 vl_api_nat_det_set_timeouts_reply_t *rmp;
1782 if (!sm->deterministic)
1784 rv = VNET_API_ERROR_UNSUPPORTED;
1788 sm->udp_timeout = ntohl (mp->udp);
1789 sm->tcp_established_timeout = ntohl (mp->tcp_established);
1790 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
1791 sm->icmp_timeout = ntohl (mp->icmp);
1794 REPLY_MACRO (VL_API_NAT_DET_SET_TIMEOUTS_REPLY);
1798 vl_api_nat_det_set_timeouts_t_print (vl_api_nat_det_set_timeouts_t * mp,
1803 s = format (0, "SCRIPT: nat_det_set_timeouts ");
1804 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
1806 ntohl (mp->tcp_established),
1807 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
1813 vl_api_nat_det_get_timeouts_t_handler (vl_api_nat_det_get_timeouts_t * mp)
1815 snat_main_t *sm = &snat_main;
1816 vl_api_nat_det_get_timeouts_reply_t *rmp;
1819 if (!sm->deterministic)
1821 rv = VNET_API_ERROR_UNSUPPORTED;
1822 REPLY_MACRO (VL_API_NAT_DET_GET_TIMEOUTS_REPLY);
1827 REPLY_MACRO2 (VL_API_NAT_DET_GET_TIMEOUTS_REPLY,
1829 rmp->udp = htonl (sm->udp_timeout);
1830 rmp->tcp_established = htonl (sm->tcp_established_timeout);
1831 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
1832 rmp->icmp = htonl (sm->icmp_timeout);
1838 vl_api_nat_det_get_timeouts_t_print (vl_api_nat_det_get_timeouts_t * mp,
1843 s = format (0, "SCRIPT: nat_det_get_timeouts");
1849 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
1852 snat_main_t *sm = &snat_main;
1853 vl_api_nat_det_close_session_out_reply_t *rmp;
1854 ip4_address_t out_addr, ext_addr, in_addr;
1855 snat_det_out_key_t key;
1857 snat_det_session_t *ses;
1860 if (!sm->deterministic)
1862 rv = VNET_API_ERROR_UNSUPPORTED;
1866 clib_memcpy (&out_addr, mp->out_addr, 4);
1867 clib_memcpy (&ext_addr, mp->ext_addr, 4);
1869 dm = snat_det_map_by_out (sm, &out_addr);
1872 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1875 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
1876 key.ext_host_addr = ext_addr;
1877 key.ext_host_port = mp->ext_port;
1878 key.out_port = mp->out_port;
1879 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
1882 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1885 snat_det_ses_close (dm, ses);
1888 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1892 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
1897 s = format (0, "SCRIPT: nat_det_close_session_out ");
1898 s = format (s, "out_addr %U out_port %d "
1899 "ext_addr %U ext_port %d\n",
1900 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
1901 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1907 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
1910 snat_main_t *sm = &snat_main;
1911 vl_api_nat_det_close_session_in_reply_t *rmp;
1912 ip4_address_t in_addr, ext_addr;
1913 snat_det_out_key_t key;
1915 snat_det_session_t *ses;
1918 if (!sm->deterministic)
1920 rv = VNET_API_ERROR_UNSUPPORTED;
1926 rv = VNET_API_ERROR_UNIMPLEMENTED;
1930 clib_memcpy (&in_addr, mp->in_addr, 4);
1931 clib_memcpy (&ext_addr, mp->ext_addr, 4);
1933 dm = snat_det_map_by_user (sm, &in_addr);
1936 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1939 key.ext_host_addr = ext_addr;
1940 key.ext_host_port = mp->ext_port;
1941 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
1944 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
1947 snat_det_ses_close (dm, ses);
1950 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
1954 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
1958 s = format (0, "SCRIPT: nat_det_close_session_in ");
1959 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
1960 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
1961 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
1967 send_nat_det_session_details (snat_det_session_t * s,
1968 vl_api_registration_t * reg, u32 context)
1970 vl_api_nat_det_session_details_t *rmp;
1971 snat_main_t *sm = &snat_main;
1973 rmp = vl_msg_api_alloc (sizeof (*rmp));
1974 memset (rmp, 0, sizeof (*rmp));
1975 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
1976 rmp->in_port = s->in_port;
1977 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
1978 rmp->ext_port = s->out.ext_host_port;
1979 rmp->out_port = s->out.out_port;
1980 rmp->state = s->state;
1981 rmp->expire = ntohl (s->expire);
1982 rmp->context = context;
1984 vl_api_send_msg (reg, (u8 *) rmp);
1988 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
1990 vl_api_registration_t *reg;
1991 snat_main_t *sm = &snat_main;
1992 ip4_address_t user_addr;
1994 snat_det_session_t *s, empty_ses;
1997 if (!sm->deterministic)
2000 reg = vl_api_client_index_to_registration (mp->client_index);
2006 memset (&empty_ses, 0, sizeof (empty_ses));
2007 clib_memcpy (&user_addr, mp->user_addr, 4);
2008 dm = snat_det_map_by_user (sm, &user_addr);
2012 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2013 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2016 send_nat_det_session_details (s, reg, mp->context);
2022 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2027 s = format (0, "SCRIPT: nat_det_session_dump ");
2028 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2038 vl_api_nat64_add_del_pool_addr_range_t_handler
2039 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2041 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2042 snat_main_t *sm = &snat_main;
2044 ip4_address_t this_addr;
2045 u32 start_host_order, end_host_order;
2050 tmp = (u32 *) mp->start_addr;
2051 start_host_order = clib_host_to_net_u32 (tmp[0]);
2052 tmp = (u32 *) mp->end_addr;
2053 end_host_order = clib_host_to_net_u32 (tmp[0]);
2055 count = (end_host_order - start_host_order) + 1;
2057 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2059 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2061 for (i = 0; i < count; i++)
2063 if ((rv = nat64_add_del_pool_addr (&this_addr, vrf_id, mp->is_add)))
2066 increment_v4_address (&this_addr);
2070 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2073 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2074 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2078 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2079 s = format (s, "%U - %U vrf_id %u %s\n",
2080 format_ip4_address, mp->start_addr,
2081 format_ip4_address, mp->end_addr,
2082 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2087 typedef struct nat64_api_walk_ctx_t_
2089 vl_api_registration_t *reg;
2092 } nat64_api_walk_ctx_t;
2095 nat64_api_pool_walk (snat_address_t * a, void *arg)
2097 vl_api_nat64_pool_addr_details_t *rmp;
2098 snat_main_t *sm = &snat_main;
2099 nat64_api_walk_ctx_t *ctx = arg;
2101 rmp = vl_msg_api_alloc (sizeof (*rmp));
2102 memset (rmp, 0, sizeof (*rmp));
2103 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2104 clib_memcpy (rmp->address, &(a->addr), 4);
2105 if (a->fib_index != ~0)
2107 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2110 rmp->vrf_id = ntohl (fib->ft_table_id);
2114 rmp->context = ctx->context;
2116 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2122 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2124 vl_api_registration_t *reg;
2126 reg = vl_api_client_index_to_registration (mp->client_index);
2130 nat64_api_walk_ctx_t ctx = {
2132 .context = mp->context,
2135 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2139 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2144 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2150 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2153 snat_main_t *sm = &snat_main;
2154 vl_api_nat64_add_del_interface_reply_t *rmp;
2157 VALIDATE_SW_IF_INDEX (mp);
2160 nat64_add_del_interface (ntohl (mp->sw_if_index), mp->is_inside,
2163 BAD_SW_IF_INDEX_LABEL;
2165 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2169 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2174 s = format (0, "SCRIPT: nat64_add_del_interface ");
2175 s = format (s, "sw_if_index %d %s %s",
2176 clib_host_to_net_u32 (mp->sw_if_index),
2177 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
2183 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2185 vl_api_nat64_interface_details_t *rmp;
2186 snat_main_t *sm = &snat_main;
2187 nat64_api_walk_ctx_t *ctx = arg;
2189 rmp = vl_msg_api_alloc (sizeof (*rmp));
2190 memset (rmp, 0, sizeof (*rmp));
2191 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2192 rmp->sw_if_index = ntohl (i->sw_if_index);
2193 rmp->is_inside = (nat_interface_is_inside (i)
2194 && nat_interface_is_outside (i)) ? 2 :
2195 nat_interface_is_inside (i);
2196 rmp->context = ctx->context;
2198 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2204 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2206 vl_api_registration_t *reg;
2208 reg = vl_api_client_index_to_registration (mp->client_index);
2212 nat64_api_walk_ctx_t ctx = {
2214 .context = mp->context,
2217 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2221 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2226 s = format (0, "SCRIPT: snat_interface_dump ");
2232 vl_api_nat64_add_del_static_bib_t_handler
2233 (vl_api_nat64_add_del_static_bib_t * mp)
2235 snat_main_t *sm = &snat_main;
2236 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2237 ip6_address_t in_addr;
2238 ip4_address_t out_addr;
2241 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2242 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2245 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2246 clib_net_to_host_u16 (mp->i_port),
2247 clib_net_to_host_u16 (mp->o_port),
2249 clib_net_to_host_u32 (mp->vrf_id),
2252 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2255 static void *vl_api_nat64_add_del_static_bib_t_print
2256 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2260 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2261 s = format (s, "protocol %d i_addr %U o_addr %U ",
2263 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2265 if (mp->vrf_id != ~0)
2266 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2272 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2274 vl_api_nat64_bib_details_t *rmp;
2275 snat_main_t *sm = &snat_main;
2276 nat64_api_walk_ctx_t *ctx = arg;
2279 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2283 rmp = vl_msg_api_alloc (sizeof (*rmp));
2284 memset (rmp, 0, sizeof (*rmp));
2285 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2286 rmp->context = ctx->context;
2287 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2288 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2289 rmp->i_port = bibe->in_port;
2290 rmp->o_port = bibe->out_port;
2291 rmp->vrf_id = ntohl (fib->ft_table_id);
2292 rmp->proto = bibe->proto;
2293 rmp->is_static = bibe->is_static;
2294 rmp->ses_num = ntohl (bibe->ses_num);
2296 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2302 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2304 vl_api_registration_t *reg;
2305 nat64_main_t *nm = &nat64_main;
2308 reg = vl_api_client_index_to_registration (mp->client_index);
2312 nat64_api_walk_ctx_t ctx = {
2314 .context = mp->context,
2318 vec_foreach (db, nm->db)
2319 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2324 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2328 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2334 vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp)
2336 snat_main_t *sm = &snat_main;
2337 vl_api_nat64_set_timeouts_reply_t *rmp;
2340 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
2343 rv = nat64_set_udp_timeout (ntohl (mp->udp));
2347 nat64_set_tcp_timeouts (ntohl (mp->tcp_trans), ntohl (mp->tcp_est),
2348 ntohl (mp->tcp_incoming_syn));
2351 REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY);
2354 static void *vl_api_nat64_set_timeouts_t_print
2355 (vl_api_nat64_set_timeouts_t * mp, void *handle)
2359 s = format (0, "SCRIPT: nat64_set_timeouts ");
2362 "udp %d icmp %d, tcp_trans %d, tcp_est %d, tcp_incoming_syn %d\n",
2363 ntohl (mp->udp), ntohl (mp->icmp), ntohl (mp->tcp_trans),
2364 ntohl (mp->tcp_est), ntohl (mp->tcp_incoming_syn));
2370 vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp)
2372 snat_main_t *sm = &snat_main;
2373 vl_api_nat64_get_timeouts_reply_t *rmp;
2377 REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY,
2379 rmp->udp = htonl (nat64_get_udp_timeout());
2380 rmp->icmp = htonl (nat64_get_icmp_timeout());
2381 rmp->tcp_trans = htonl (nat64_get_tcp_trans_timeout());
2382 rmp->tcp_est = htonl (nat64_get_tcp_est_timeout());
2383 rmp->tcp_incoming_syn = htonl (nat64_get_tcp_incoming_syn_timeout());
2388 static void *vl_api_nat64_get_timeouts_t_print
2389 (vl_api_nat64_get_timeouts_t * mp, void *handle)
2393 s = format (0, "SCRIPT: nat64_get_timeouts");
2399 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2401 vl_api_nat64_st_details_t *rmp;
2402 snat_main_t *sm = &snat_main;
2403 nat64_api_walk_ctx_t *ctx = arg;
2404 nat64_db_bib_entry_t *bibe;
2407 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2411 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2415 rmp = vl_msg_api_alloc (sizeof (*rmp));
2416 memset (rmp, 0, sizeof (*rmp));
2417 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2418 rmp->context = ctx->context;
2419 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2420 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2421 rmp->il_port = bibe->in_port;
2422 rmp->ol_port = bibe->out_port;
2423 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2424 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2425 rmp->il_port = ste->r_port;
2426 rmp->vrf_id = ntohl (fib->ft_table_id);
2427 rmp->proto = ste->proto;
2429 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2435 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2437 vl_api_registration_t *reg;
2438 nat64_main_t *nm = &nat64_main;
2441 reg = vl_api_client_index_to_registration (mp->client_index);
2445 nat64_api_walk_ctx_t ctx = {
2447 .context = mp->context,
2451 vec_foreach (db, nm->db)
2454 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2460 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2464 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2470 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2472 vl_api_nat64_add_del_prefix_reply_t *rmp;
2473 snat_main_t *sm = &snat_main;
2474 ip6_address_t prefix;
2477 memcpy (&prefix.as_u8, mp->prefix, 16);
2480 nat64_add_del_prefix (&prefix, mp->prefix_len,
2481 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2482 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2486 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2491 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2492 format_ip6_address, mp->prefix, mp->prefix_len,
2493 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2499 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2501 vl_api_nat64_prefix_details_t *rmp;
2502 snat_main_t *sm = &snat_main;
2503 nat64_api_walk_ctx_t *ctx = arg;
2505 rmp = vl_msg_api_alloc (sizeof (*rmp));
2506 memset (rmp, 0, sizeof (*rmp));
2507 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2508 clib_memcpy (rmp->prefix, &(p->prefix), 16);
2509 rmp->prefix_len = p->plen;
2510 rmp->vrf_id = ntohl (p->vrf_id);
2511 rmp->context = ctx->context;
2513 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2519 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2521 vl_api_registration_t *reg;
2523 reg = vl_api_client_index_to_registration (mp->client_index);
2527 nat64_api_walk_ctx_t ctx = {
2529 .context = mp->context,
2532 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2536 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2541 s = format (0, "SCRIPT: nat64_prefix_dump\n");
2547 vl_api_nat64_add_del_interface_addr_t_handler
2548 (vl_api_nat64_add_del_interface_addr_t * mp)
2550 snat_main_t *sm = &snat_main;
2551 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2552 u32 sw_if_index = ntohl (mp->sw_if_index);
2555 VALIDATE_SW_IF_INDEX (mp);
2557 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2559 BAD_SW_IF_INDEX_LABEL;
2561 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2564 static void *vl_api_nat64_add_del_interface_addr_t_print
2565 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2569 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2570 s = format (s, "sw_if_index %d %s",
2571 clib_host_to_net_u32 (mp->sw_if_index),
2572 mp->is_add ? "" : "del");
2582 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
2584 vl_api_dslite_set_aftr_addr_reply_t *rmp;
2585 snat_main_t *sm = &snat_main;
2586 dslite_main_t *dm = &dslite_main;
2588 ip6_address_t ip6_addr;
2589 ip4_address_t ip4_addr;
2591 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2592 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2594 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
2596 rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
2598 REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
2602 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
2607 s = format (0, "SCRIPT: dslite_set_aftr_addr ");
2608 s = format (s, "ip6_addr %U ip4_addr %U\n",
2609 format_ip6_address, mp->ip6_addr,
2610 format_ip4_address, mp->ip4_addr);
2616 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
2618 snat_main_t *sm = &snat_main;
2619 vl_api_dslite_get_aftr_addr_reply_t *rmp;
2620 dslite_main_t *dm = &dslite_main;
2624 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2626 memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
2627 memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
2633 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
2638 s = format (0, "SCRIPT: dslite_get_aftr_addr");
2644 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
2646 vl_api_dslite_set_b4_addr_reply_t *rmp;
2647 snat_main_t *sm = &snat_main;
2648 dslite_main_t *dm = &dslite_main;
2650 ip6_address_t ip6_addr;
2651 ip4_address_t ip4_addr;
2653 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2654 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2656 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
2658 rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
2660 REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
2664 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
2669 s = format (0, "SCRIPT: dslite_set_b4_addr ");
2670 s = format (s, "ip6_addr %U ip4_addr %U\n",
2671 format_ip6_address, mp->ip6_addr,
2672 format_ip6_address, mp->ip4_addr);
2678 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
2680 snat_main_t *sm = &snat_main;
2681 vl_api_dslite_get_b4_addr_reply_t *rmp;
2682 dslite_main_t *dm = &dslite_main;
2686 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2688 memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
2689 memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
2695 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
2700 s = format (0, "SCRIPT: dslite_get_b4_addr");
2706 vl_api_dslite_add_del_pool_addr_range_t_handler
2707 (vl_api_dslite_add_del_pool_addr_range_t * mp)
2709 vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
2710 snat_main_t *sm = &snat_main;
2711 dslite_main_t *dm = &dslite_main;
2713 ip4_address_t this_addr;
2714 u32 start_host_order, end_host_order;
2718 tmp = (u32 *) mp->start_addr;
2719 start_host_order = clib_host_to_net_u32 (tmp[0]);
2720 tmp = (u32 *) mp->end_addr;
2721 end_host_order = clib_host_to_net_u32 (tmp[0]);
2723 count = (end_host_order - start_host_order) + 1;
2724 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2726 for (i = 0; i < count; i++)
2728 if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
2731 increment_v4_address (&this_addr);
2735 REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2739 send_dslite_address_details (snat_address_t * ap,
2740 vl_api_registration_t * reg, u32 context)
2742 vl_api_dslite_address_details_t *rmp;
2743 snat_main_t *sm = &snat_main;
2745 rmp = vl_msg_api_alloc (sizeof (*rmp));
2747 memset (rmp, 0, sizeof (*rmp));
2749 rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
2750 clib_memcpy (rmp->ip_address, &(ap->addr), 4);
2751 rmp->context = context;
2753 vl_api_send_msg (reg, (u8 *) rmp);
2757 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
2759 vl_api_registration_t *reg;
2760 dslite_main_t *dm = &dslite_main;
2763 reg = vl_api_client_index_to_registration (mp->client_index);
2768 vec_foreach (ap, dm->addr_pool)
2770 send_dslite_address_details (ap, reg, mp->context);
2776 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
2781 s = format (0, "SCRIPT: dslite_address_dump ");
2786 static void *vl_api_dslite_add_del_pool_addr_range_t_print
2787 (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
2791 s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
2792 s = format (s, "%U - %U\n",
2793 format_ip4_address, mp->start_addr,
2794 format_ip4_address, mp->end_addr);
2805 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
2808 snat_main_t *sm = &snat_main;
2809 vl_api_nat66_add_del_interface_reply_t *rmp;
2812 VALIDATE_SW_IF_INDEX (mp);
2815 nat66_interface_add_del (ntohl (mp->sw_if_index), mp->is_inside,
2818 BAD_SW_IF_INDEX_LABEL;
2820 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
2824 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
2829 s = format (0, "SCRIPT: nat66_add_del_interface ");
2830 s = format (s, "sw_if_index %d %s %s",
2831 clib_host_to_net_u32 (mp->sw_if_index),
2832 mp->is_inside ? "in" : "out", mp->is_add ? "" : "del");
2838 vl_api_nat66_add_del_static_mapping_t_handler
2839 (vl_api_nat66_add_del_static_mapping_t * mp)
2841 snat_main_t *sm = &snat_main;
2842 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
2843 ip6_address_t l_addr, e_addr;
2846 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
2847 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
2850 nat66_static_mapping_add_del (&l_addr, &e_addr,
2851 clib_net_to_host_u32 (mp->vrf_id),
2854 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
2857 static void *vl_api_nat66_add_del_static_mapping_t_print
2858 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
2862 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
2863 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
2864 format_ip6_address, mp->local_ip_address,
2865 format_ip6_address, mp->external_ip_address,
2866 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
2871 typedef struct nat66_api_walk_ctx_t_
2875 } nat66_api_walk_ctx_t;
2878 nat66_api_interface_walk (snat_interface_t * i, void *arg)
2880 vl_api_nat66_interface_details_t *rmp;
2881 snat_main_t *sm = &snat_main;
2882 nat66_api_walk_ctx_t *ctx = arg;
2884 rmp = vl_msg_api_alloc (sizeof (*rmp));
2885 memset (rmp, 0, sizeof (*rmp));
2886 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
2887 rmp->sw_if_index = ntohl (i->sw_if_index);
2888 rmp->is_inside = nat_interface_is_inside (i);
2889 rmp->context = ctx->context;
2891 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2897 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
2901 q = vl_api_client_index_to_input_queue (mp->client_index);
2905 nat66_api_walk_ctx_t ctx = {
2907 .context = mp->context,
2910 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
2914 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
2919 s = format (0, "SCRIPT: nat66_interface_dump ");
2925 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
2927 vl_api_nat66_static_mapping_details_t *rmp;
2928 nat66_main_t *nm = &nat66_main;
2929 snat_main_t *sm = &snat_main;
2930 nat66_api_walk_ctx_t *ctx = arg;
2934 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
2938 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
2940 rmp = vl_msg_api_alloc (sizeof (*rmp));
2941 memset (rmp, 0, sizeof (*rmp));
2943 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2944 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
2945 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
2946 rmp->vrf_id = ntohl (fib->ft_table_id);
2947 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
2948 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
2949 rmp->context = ctx->context;
2951 vl_msg_api_send_shmem (ctx->q, (u8 *) & rmp);
2957 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
2962 q = vl_api_client_index_to_input_queue (mp->client_index);
2966 nat66_api_walk_ctx_t ctx = {
2968 .context = mp->context,
2971 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
2975 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
2980 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
2986 /* List of message types that this plugin understands */
2987 #define foreach_snat_plugin_api_msg \
2988 _(NAT_CONTROL_PING, nat_control_ping) \
2989 _(NAT_SHOW_CONFIG, nat_show_config) \
2990 _(NAT_SET_WORKERS, nat_set_workers) \
2991 _(NAT_WORKER_DUMP, nat_worker_dump) \
2992 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
2993 _(NAT_SET_REASS, nat_set_reass) \
2994 _(NAT_GET_REASS, nat_get_reass) \
2995 _(NAT_REASS_DUMP, nat_reass_dump) \
2996 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
2997 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
2998 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
2999 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3000 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3001 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3002 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3003 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3004 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3005 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3006 _(NAT44_USER_DUMP, nat44_user_dump) \
3007 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3008 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3009 nat44_interface_add_del_output_feature) \
3010 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3011 nat44_interface_output_feature_dump) \
3012 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3013 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3014 _(NAT44_DEL_SESSION, nat44_del_session) \
3015 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3016 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3017 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3018 _(NAT_DET_FORWARD, nat_det_forward) \
3019 _(NAT_DET_REVERSE, nat_det_reverse) \
3020 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3021 _(NAT_DET_SET_TIMEOUTS, nat_det_set_timeouts) \
3022 _(NAT_DET_GET_TIMEOUTS, nat_det_get_timeouts) \
3023 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3024 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3025 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3026 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3027 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3028 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3029 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3030 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3031 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3032 _(NAT64_SET_TIMEOUTS, nat64_set_timeouts) \
3033 _(NAT64_GET_TIMEOUTS, nat64_get_timeouts) \
3034 _(NAT64_ST_DUMP, nat64_st_dump) \
3035 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3036 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3037 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3038 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range) \
3039 _(DSLITE_ADDRESS_DUMP, dslite_address_dump) \
3040 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr) \
3041 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr) \
3042 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr) \
3043 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr) \
3044 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3045 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3046 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3047 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3049 /* Set up the API message handling tables */
3050 static clib_error_t *
3051 snat_plugin_api_hookup (vlib_main_t * vm)
3053 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3055 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3057 vl_api_##n##_t_handler, \
3059 vl_api_##n##_t_endian, \
3060 vl_api_##n##_t_print, \
3061 sizeof(vl_api_##n##_t), 1);
3062 foreach_snat_plugin_api_msg;
3068 #define vl_msg_name_crc_list
3069 #include <nat/nat_all_api_h.h>
3070 #undef vl_msg_name_crc_list
3073 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3075 #define _(id,n,crc) \
3076 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3077 foreach_vl_msg_name_crc_nat;
3082 plugin_custom_dump_configure (snat_main_t * sm)
3084 #define _(n,f) sm->api_main->msg_print_handlers \
3085 [VL_API_##n + sm->msg_id_base] \
3086 = (void *) vl_api_##f##_t_print;
3087 foreach_snat_plugin_api_msg;
3092 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3095 clib_error_t *error = 0;
3097 name = format (0, "nat_%08x%c", api_version, 0);
3099 /* Ask for a correctly-sized block of API message decode slots */
3101 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3103 error = snat_plugin_api_hookup (vm);
3105 /* Add our API messages to the global name_crc hash table */
3106 setup_message_id_table (sm, sm->api_main);
3108 plugin_custom_dump_configure (sm);
3116 * fd.io coding-style-patch-verification: ON
3119 * eval: (c-set-style "gnu")