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 <nat/nat_inlines.h>
28 #include <nat/nat_ha.h>
29 #include <vlibapi/api.h>
30 #include <vlibmemory/api.h>
32 #include <nat/nat_msg_enum.h>
33 #include <vnet/fib/fib_table.h>
34 #include <vnet/ip/ip_types_api.h>
36 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
37 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
39 /* define message structures */
41 #include <nat/nat_all_api_h.h>
44 /* define generated endian-swappers */
46 #include <nat/nat_all_api_h.h>
49 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
51 #define REPLY_MSG_ID_BASE sm->msg_id_base
52 #include <vlibapi/api_helper_macros.h>
54 /* Get the API version number */
55 #define vl_api_version(n,v) static u32 api_version=(v);
56 #include <nat/nat_all_api_h.h>
59 /* Macro to finish up custom dump fns */
62 vl_print (handle, (char *)s); \
66 /******************************/
67 /*** Common NAT plugin APIs ***/
68 /******************************/
71 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
73 vl_api_nat_control_ping_reply_t *rmp;
74 snat_main_t *sm = &snat_main;
78 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
80 rmp->vpe_pid = ntohl (getpid ());
86 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
90 s = format (0, "SCRIPT: nat_control_ping ");
96 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
98 vl_api_nat_show_config_reply_t *rmp;
99 snat_main_t *sm = &snat_main;
100 dslite_main_t *dm = &dslite_main;
101 nat64_main_t *n64m = &nat64_main;
105 REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
107 rmp->translation_buckets = htonl (sm->translation_buckets);
108 rmp->translation_memory_size = htonl (sm->translation_memory_size);
109 rmp->user_buckets = htonl (sm->user_buckets);
110 rmp->user_memory_size = htonl (sm->user_memory_size);
111 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
112 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
113 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
114 rmp->static_mapping_only = sm->static_mapping_only;
115 rmp->static_mapping_connection_tracking =
116 sm->static_mapping_connection_tracking;
117 rmp->deterministic = sm->deterministic;
118 rmp->endpoint_dependent = sm->endpoint_dependent;
119 rmp->out2in_dpo = sm->out2in_dpo;
120 rmp->dslite_ce = dm->is_ce;
121 rmp->nat64_bib_buckets = n64m->bib_buckets;
122 rmp->nat64_bib_memory_size = n64m->bib_memory_size;
123 rmp->nat64_st_buckets = n64m->st_buckets;
124 rmp->nat64_st_memory_size = n64m->st_memory_size;
130 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
134 s = format (0, "SCRIPT: nat_show_config ");
140 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
142 snat_main_t *sm = &snat_main;
143 vl_api_nat_set_workers_reply_t *rmp;
148 if (sm->deterministic)
150 rv = VNET_API_ERROR_UNSUPPORTED;
154 mask = clib_net_to_host_u64 (mp->worker_mask);
156 if (sm->num_workers < 2)
158 rv = VNET_API_ERROR_FEATURE_DISABLED;
162 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
163 rv = snat_set_workers (bitmap);
164 clib_bitmap_free (bitmap);
167 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
171 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
177 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
179 s = format (0, "SCRIPT: nat_set_workers ");
180 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
182 clib_bitmap_foreach (i, bitmap,
185 s = format (s, "%d", i);
187 s = format (s, ",%d", i);
191 clib_bitmap_free (bitmap);
196 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
199 vl_api_nat_worker_details_t *rmp;
200 snat_main_t *sm = &snat_main;
201 vlib_worker_thread_t *w =
202 vlib_worker_threads + worker_index + sm->first_worker_index;
204 rmp = vl_msg_api_alloc (sizeof (*rmp));
205 clib_memset (rmp, 0, sizeof (*rmp));
206 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
207 rmp->context = context;
208 rmp->worker_index = htonl (worker_index);
209 rmp->lcore_id = htonl (w->cpu_id);
210 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
212 vl_api_send_msg (reg, (u8 *) rmp);
216 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
218 vl_api_registration_t *reg;
219 snat_main_t *sm = &snat_main;
222 if (sm->deterministic)
225 reg = vl_api_client_index_to_registration (mp->client_index);
230 vec_foreach (worker_index, sm->workers)
231 send_nat_worker_details(*worker_index, reg, mp->context);
236 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
240 s = format (0, "SCRIPT: nat_worker_dump ");
246 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
248 snat_main_t *sm = &snat_main;
249 vl_api_nat_set_log_level_reply_t *rmp;
252 if (sm->log_level > NAT_LOG_DEBUG)
253 rv = VNET_API_ERROR_UNSUPPORTED;
255 sm->log_level = mp->log_level;
257 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
261 vl_api_nat_set_log_level_t_print (vl_api_nat_set_log_level_t *
266 s = format (0, "SCRIPT: nat_set_log_level ");
267 s = format (s, "log_level %d", mp->log_level);
273 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
276 snat_main_t *sm = &snat_main;
277 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
280 rv = snat_ipfix_logging_enable_disable (mp->enable,
286 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
290 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
295 s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
297 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
299 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
301 s = format (s, "disable ");
307 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
309 snat_main_t *sm = &snat_main;
310 vl_api_nat_set_reass_reply_t *rmp;
314 nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
315 mp->drop_frag, mp->is_ip6);
317 REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
321 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
325 s = format (0, "SCRIPT: nat_set_reass ");
326 s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
327 clib_host_to_net_u32 (mp->timeout),
328 clib_host_to_net_u16 (mp->max_reass),
329 mp->max_frag, mp->drop_frag, mp->is_ip6);
335 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
337 snat_main_t *sm = &snat_main;
338 vl_api_nat_get_reass_reply_t *rmp;
342 REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
344 rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
345 rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
346 rmp->ip4_max_frag = nat_reass_get_max_frag(0);
347 rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
348 rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
349 rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
350 rmp->ip6_max_frag = nat_reass_get_max_frag(1);
351 rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
357 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
361 s = format (0, "SCRIPT: nat_get_reass");
366 typedef struct nat_api_walk_ctx_t_
368 vl_api_registration_t *reg;
370 } nat_api_walk_ctx_t;
373 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
375 vl_api_nat_reass_details_t *rmp;
376 snat_main_t *sm = &snat_main;
377 nat_api_walk_ctx_t *ctx = arg;
378 ip46_address_t ip_address;
380 rmp = vl_msg_api_alloc (sizeof (*rmp));
381 clib_memset (rmp, 0, sizeof (*rmp));
382 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
383 rmp->context = ctx->context;
385 clib_memcpy (&ip_address.ip4, &reass->key.src, 4);
386 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->src_addr);
388 clib_memcpy (&ip_address.ip4, &reass->key.dst, 4);
389 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->dst_addr);
391 rmp->proto = reass->key.proto;
392 rmp->frag_id = ntohl (reass->key.frag_id);
393 rmp->frag_n = reass->frag_n;
395 vl_api_send_msg (ctx->reg, (u8 *) rmp);
401 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
403 vl_api_nat_reass_details_t *rmp;
404 snat_main_t *sm = &snat_main;
405 nat_api_walk_ctx_t *ctx = arg;
406 ip46_address_t ip_address;
408 rmp = vl_msg_api_alloc (sizeof (*rmp));
409 clib_memset (rmp, 0, sizeof (*rmp));
410 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
411 rmp->context = ctx->context;
413 clib_memcpy (&ip_address.ip6, &reass->key.src, 16);
414 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->src_addr);
416 clib_memcpy (&ip_address.ip6, &reass->key.dst, 16);
417 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->dst_addr);
419 rmp->proto = reass->key.proto;
420 rmp->frag_id = ntohl (reass->key.frag_id);
421 rmp->frag_n = reass->frag_n;
423 vl_api_send_msg (ctx->reg, (u8 *) rmp);
429 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
431 vl_api_registration_t *reg;
433 reg = vl_api_client_index_to_registration (mp->client_index);
437 nat_api_walk_ctx_t ctx = {
439 .context = mp->context,
442 nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
443 nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
447 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
451 s = format (0, "SCRIPT: nat_reass_dump");
457 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
459 snat_main_t *sm = &snat_main;
460 vl_api_nat_set_timeouts_reply_t *rmp;
463 sm->udp_timeout = ntohl (mp->udp);
464 sm->tcp_established_timeout = ntohl (mp->tcp_established);
465 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
466 sm->icmp_timeout = ntohl (mp->icmp);
468 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
471 rv = nat64_set_udp_timeout (ntohl (mp->udp));
475 nat64_set_tcp_timeouts (ntohl (mp->tcp_transitory),
476 ntohl (mp->tcp_established));
479 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
483 vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle)
487 s = format (0, "SCRIPT: nat_set_timeouts ");
488 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
490 ntohl (mp->tcp_established),
491 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
497 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
499 snat_main_t *sm = &snat_main;
500 vl_api_nat_get_timeouts_reply_t *rmp;
504 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
506 rmp->udp = htonl (sm->udp_timeout);
507 rmp->tcp_established = htonl (sm->tcp_established_timeout);
508 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
509 rmp->icmp = htonl (sm->icmp_timeout);
515 vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle)
519 s = format (0, "SCRIPT: nat_get_timeouts");
525 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
526 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
528 snat_main_t *sm = &snat_main;
529 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
531 u16 port_start, port_end;
533 if (sm->deterministic)
535 rv = VNET_API_ERROR_UNSUPPORTED;
541 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
542 nat_set_alloc_addr_and_port_default ();
544 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
545 nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
548 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
549 port_start = ntohs (mp->start_port);
550 port_end = ntohs (mp->end_port);
551 if (port_end <= port_start)
553 rv = VNET_API_ERROR_INVALID_VALUE;
556 nat_set_alloc_addr_and_port_range (port_start, port_end);
559 rv = VNET_API_ERROR_INVALID_VALUE;
564 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
567 static void *vl_api_nat_set_addr_and_port_alloc_alg_t_print
568 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp, void *handle)
572 s = format (0, "SCRIPT: nat_set_addr_and_port_alloc_alg ");
573 s = format (s, "alg %d psid_offset %d psid_length %d psid %d start_port %d "
575 ntohl (mp->alg), ntohl (mp->psid_offset),
576 ntohl (mp->psid_length), ntohs (mp->psid),
577 ntohs (mp->start_port), ntohs (mp->end_port));
583 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
584 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
586 snat_main_t *sm = &snat_main;
587 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
591 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
593 rmp->alg = sm->addr_and_port_alloc_alg;
594 rmp->psid_offset = sm->psid_offset;
595 rmp->psid_length = sm->psid_length;
596 rmp->psid = htons (sm->psid);
597 rmp->start_port = htons (sm->start_port);
598 rmp->end_port = htons (sm->end_port);
603 static void *vl_api_nat_get_addr_and_port_alloc_alg_t_print
604 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp, void *handle)
608 s = format (0, "SCRIPT: nat_get_addr_and_port_alloc_alg");
614 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
616 snat_main_t *sm = &snat_main;
617 vl_api_nat_set_mss_clamping_reply_t *rmp;
622 sm->mss_clamping = ntohs (mp->mss_value);
623 sm->mss_value_net = mp->mss_value;
626 sm->mss_clamping = 0;
628 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
632 vl_api_nat_set_mss_clamping_t_print (vl_api_nat_set_mss_clamping_t * mp,
637 s = format (0, "SCRIPT: nat_set_mss_clamping enable %d mss_value %d\n",
638 mp->enable, ntohs (mp->mss_value));
644 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
646 snat_main_t *sm = &snat_main;
647 vl_api_nat_get_mss_clamping_reply_t *rmp;
651 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
653 rmp->enable = sm->mss_clamping ? 1 : 0;
654 rmp->mss_value = htons (sm->mss_clamping);
660 vl_api_nat_get_mss_clamping_t_print (vl_api_nat_get_mss_clamping_t * mp,
665 s = format (0, "SCRIPT: nat_get_mss_clamping");
671 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
673 snat_main_t *sm = &snat_main;
674 vl_api_nat_ha_set_listener_reply_t *rmp;
678 memcpy (&addr, &mp->ip_address, sizeof (addr));
680 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
681 clib_net_to_host_u32 (mp->path_mtu));
683 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
687 vl_api_nat_ha_set_listener_t_print (vl_api_nat_ha_set_listener_t * mp,
692 s = format (0, "SCRIPT: nat_ha_set_listener ");
693 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
694 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
695 s = format (s, "path_mtu %d", clib_net_to_host_u32 (mp->path_mtu));
701 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
703 snat_main_t *sm = &snat_main;
704 vl_api_nat_ha_get_listener_reply_t *rmp;
710 nat_ha_get_listener (&addr, &port, &path_mtu);
713 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
715 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
716 rmp->port = clib_host_to_net_u16 (port);
717 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
723 vl_api_nat_ha_get_listener_t_print (vl_api_nat_ha_get_listener_t * mp,
728 s = format (0, "SCRIPT: nat_ha_get_listener");
734 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
736 snat_main_t *sm = &snat_main;
737 vl_api_nat_ha_set_failover_reply_t *rmp;
741 memcpy (&addr, &mp->ip_address, sizeof (addr));
743 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
744 clib_net_to_host_u32 (mp->session_refresh_interval));
746 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
750 vl_api_nat_ha_set_failover_t_print (vl_api_nat_ha_set_failover_t * mp,
755 s = format (0, "SCRIPT: nat_ha_set_failover ");
756 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
757 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
763 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
765 snat_main_t *sm = &snat_main;
766 vl_api_nat_ha_get_failover_reply_t *rmp;
770 u32 session_refresh_interval;
772 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
775 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
777 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
778 rmp->port = clib_host_to_net_u16 (port);
779 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
785 vl_api_nat_ha_get_failover_t_print (vl_api_nat_ha_get_failover_t * mp,
790 s = format (0, "SCRIPT: nat_ha_get_failover");
796 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
798 snat_main_t *sm = &snat_main;
799 vl_api_nat_ha_flush_reply_t *rmp;
804 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
808 vl_api_nat_ha_flush_t_print (vl_api_nat_ha_flush_t * mp, void *handle)
812 s = format (0, "SCRIPT: nat_ha_flush ");
818 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
820 snat_main_t *sm = &snat_main;
821 vl_api_registration_t *reg;
822 vl_api_nat_ha_resync_completed_event_t *mp;
824 reg = vl_api_client_index_to_registration (client_index);
828 mp = vl_msg_api_alloc (sizeof (*mp));
829 clib_memset (mp, 0, sizeof (*mp));
830 mp->client_index = client_index;
832 mp->missed_count = clib_host_to_net_u32 (missed_count);
834 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
836 vl_api_send_msg (reg, (u8 *) mp);
840 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
842 snat_main_t *sm = &snat_main;
843 vl_api_nat_ha_resync_reply_t *rmp;
847 nat_ha_resync (mp->client_index, mp->pid,
848 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
851 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
855 vl_api_nat_ha_resync_t_print (vl_api_nat_ha_resync_t * mp, void *handle)
859 s = format (0, "SCRIPT: nat_ha_resync ");
861 format (s, "want_resync_event %d pid %d", mp->want_resync_event,
862 clib_host_to_net_u32 (mp->pid));
871 vl_api_nat44_add_del_address_range_t_handler
872 (vl_api_nat44_add_del_address_range_t * mp)
874 snat_main_t *sm = &snat_main;
875 vl_api_nat44_add_del_address_range_reply_t *rmp;
876 ip4_address_t this_addr;
877 u8 is_add, twice_nat;
878 u32 start_host_order, end_host_order;
884 if (sm->deterministic)
886 rv = VNET_API_ERROR_UNSUPPORTED;
890 if (sm->static_mapping_only)
892 rv = VNET_API_ERROR_FEATURE_DISABLED;
897 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
899 tmp = (u32 *) mp->first_ip_address;
900 start_host_order = clib_host_to_net_u32 (tmp[0]);
901 tmp = (u32 *) mp->last_ip_address;
902 end_host_order = clib_host_to_net_u32 (tmp[0]);
904 count = (end_host_order - start_host_order) + 1;
906 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
909 nat_log_info ("%U - %U, %d addresses...",
910 format_ip4_address, mp->first_ip_address,
911 format_ip4_address, mp->last_ip_address, count);
913 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
915 for (i = 0; i < count; i++)
918 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
920 rv = snat_del_address (sm, this_addr, 0, twice_nat);
926 nat44_add_del_address_dpo (this_addr, is_add);
928 increment_v4_address (&this_addr);
932 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
935 static void *vl_api_nat44_add_del_address_range_t_print
936 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
940 s = format (0, "SCRIPT: nat44_add_address_range ");
941 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
942 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
944 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
946 s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
951 send_nat44_address_details (snat_address_t * a,
952 vl_api_registration_t * reg, u32 context,
955 vl_api_nat44_address_details_t *rmp;
956 snat_main_t *sm = &snat_main;
958 rmp = vl_msg_api_alloc (sizeof (*rmp));
959 clib_memset (rmp, 0, sizeof (*rmp));
960 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
961 clib_memcpy (rmp->ip_address, &(a->addr), 4);
962 if (a->fib_index != ~0)
964 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
965 rmp->vrf_id = ntohl (fib->ft_table_id);
970 rmp->flags |= NAT_API_IS_TWICE_NAT;
971 rmp->context = context;
973 vl_api_send_msg (reg, (u8 *) rmp);
977 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
979 vl_api_registration_t *reg;
980 snat_main_t *sm = &snat_main;
983 if (sm->deterministic)
986 reg = vl_api_client_index_to_registration (mp->client_index);
991 vec_foreach (a, sm->addresses)
992 send_nat44_address_details (a, reg, mp->context, 0);
993 vec_foreach (a, sm->twice_nat_addresses)
994 send_nat44_address_details (a, reg, mp->context, 1);
999 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
1004 s = format (0, "SCRIPT: nat44_address_dump ");
1010 vl_api_nat44_interface_add_del_feature_t_handler
1011 (vl_api_nat44_interface_add_del_feature_t * mp)
1013 snat_main_t *sm = &snat_main;
1014 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
1015 u32 sw_if_index = ntohl (mp->sw_if_index);
1019 is_del = !mp->is_add;
1021 VALIDATE_SW_IF_INDEX (mp);
1024 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
1027 BAD_SW_IF_INDEX_LABEL;
1029 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
1032 static void *vl_api_nat44_interface_add_del_feature_t_print
1033 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
1037 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
1038 s = format (s, "sw_if_index %d %s %s",
1039 clib_host_to_net_u32 (mp->sw_if_index),
1040 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1041 mp->is_add ? "" : "del");
1047 send_nat44_interface_details (snat_interface_t * i,
1048 vl_api_registration_t * reg, u32 context)
1050 vl_api_nat44_interface_details_t *rmp;
1051 snat_main_t *sm = &snat_main;
1053 rmp = vl_msg_api_alloc (sizeof (*rmp));
1054 clib_memset (rmp, 0, sizeof (*rmp));
1055 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
1056 rmp->sw_if_index = ntohl (i->sw_if_index);
1058 if (nat_interface_is_inside (i))
1059 rmp->flags |= NAT_API_IS_INSIDE;
1060 if (nat_interface_is_outside (i))
1061 rmp->flags |= NAT_API_IS_OUTSIDE;
1063 rmp->context = context;
1065 vl_api_send_msg (reg, (u8 *) rmp);
1069 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
1071 vl_api_registration_t *reg;
1072 snat_main_t *sm = &snat_main;
1073 snat_interface_t *i;
1075 reg = vl_api_client_index_to_registration (mp->client_index);
1080 pool_foreach (i, sm->interfaces,
1082 send_nat44_interface_details(i, reg, mp->context);
1088 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
1093 s = format (0, "SCRIPT: nat44_interface_dump ");
1099 vl_api_nat44_interface_add_del_output_feature_t_handler
1100 (vl_api_nat44_interface_add_del_output_feature_t * mp)
1102 snat_main_t *sm = &snat_main;
1103 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
1104 u32 sw_if_index = ntohl (mp->sw_if_index);
1107 if (sm->deterministic)
1109 rv = VNET_API_ERROR_UNSUPPORTED;
1113 VALIDATE_SW_IF_INDEX (mp);
1115 rv = snat_interface_add_del_output_feature (sw_if_index,
1116 mp->flags & NAT_API_IS_INSIDE,
1119 BAD_SW_IF_INDEX_LABEL;
1121 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
1124 static void *vl_api_nat44_interface_add_del_output_feature_t_print
1125 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
1129 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
1130 s = format (s, "sw_if_index %d %s %s",
1131 clib_host_to_net_u32 (mp->sw_if_index),
1132 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1133 mp->is_add ? "" : "del");
1139 send_nat44_interface_output_feature_details (snat_interface_t * i,
1140 vl_api_registration_t * reg,
1143 vl_api_nat44_interface_output_feature_details_t *rmp;
1144 snat_main_t *sm = &snat_main;
1146 rmp = vl_msg_api_alloc (sizeof (*rmp));
1147 clib_memset (rmp, 0, sizeof (*rmp));
1149 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
1150 rmp->sw_if_index = ntohl (i->sw_if_index);
1151 rmp->context = context;
1153 if (nat_interface_is_inside (i))
1154 rmp->flags |= NAT_API_IS_INSIDE;
1156 vl_api_send_msg (reg, (u8 *) rmp);
1160 vl_api_nat44_interface_output_feature_dump_t_handler
1161 (vl_api_nat44_interface_output_feature_dump_t * mp)
1163 vl_api_registration_t *reg;
1164 snat_main_t *sm = &snat_main;
1165 snat_interface_t *i;
1167 if (sm->deterministic)
1170 reg = vl_api_client_index_to_registration (mp->client_index);
1175 pool_foreach (i, sm->output_feature_interfaces,
1177 send_nat44_interface_output_feature_details(i, reg, mp->context);
1182 static void *vl_api_nat44_interface_output_feature_dump_t_print
1183 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1187 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1193 vl_api_nat44_add_del_static_mapping_t_handler
1194 (vl_api_nat44_add_del_static_mapping_t * mp)
1196 snat_main_t *sm = &snat_main;
1197 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1198 ip4_address_t local_addr, external_addr;
1199 u16 local_port = 0, external_port = 0;
1200 u32 vrf_id, external_sw_if_index;
1201 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1203 snat_protocol_t proto;
1206 if (sm->deterministic)
1208 rv = VNET_API_ERROR_UNSUPPORTED;
1212 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1213 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1215 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1217 local_port = clib_net_to_host_u16 (mp->local_port);
1218 external_port = clib_net_to_host_u16 (mp->external_port);
1221 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1222 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1223 proto = ip_proto_to_snat_proto (mp->protocol);
1225 if (mp->flags & NAT_API_IS_TWICE_NAT)
1226 twice_nat = TWICE_NAT;
1227 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1228 twice_nat = TWICE_NAT_SELF;
1229 mp->tag[sizeof (mp->tag) - 1] = 0;
1230 tag = format (0, "%s", mp->tag);
1231 vec_terminate_c_string (tag);
1233 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1234 external_port, vrf_id,
1235 mp->flags & NAT_API_IS_ADDR_ONLY,
1236 external_sw_if_index, proto,
1237 mp->is_add, twice_nat,
1238 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0);
1242 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1245 static void *vl_api_nat44_add_del_static_mapping_t_print
1246 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1250 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1251 s = format (s, "protocol %d local_addr %U external_addr %U ",
1253 format_ip4_address, mp->local_ip_address,
1254 format_ip4_address, mp->external_ip_address);
1256 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1257 s = format (s, "local_port %d external_port %d ",
1258 clib_net_to_host_u16 (mp->local_port),
1259 clib_net_to_host_u16 (mp->external_port));
1261 s = format (s, "twice_nat %d out2in_only %d ",
1262 mp->flags & NAT_API_IS_TWICE_NAT,
1263 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1265 if (mp->vrf_id != ~0)
1266 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1268 if (mp->external_sw_if_index != ~0)
1269 s = format (s, "external_sw_if_index %d",
1270 clib_net_to_host_u32 (mp->external_sw_if_index));
1275 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1276 vl_api_registration_t * reg, u32 context)
1278 vl_api_nat44_static_mapping_details_t *rmp;
1279 snat_main_t *sm = &snat_main;
1280 u32 len = sizeof (*rmp);
1282 rmp = vl_msg_api_alloc (len);
1283 clib_memset (rmp, 0, len);
1285 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1287 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1288 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1289 rmp->external_sw_if_index = ~0;
1290 rmp->vrf_id = htonl (m->vrf_id);
1291 rmp->context = context;
1293 if (m->twice_nat == TWICE_NAT)
1294 rmp->flags |= NAT_API_IS_TWICE_NAT;
1295 else if (m->twice_nat == TWICE_NAT_SELF)
1296 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1298 if (is_out2in_only_static_mapping (m))
1299 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1301 if (is_addr_only_static_mapping (m))
1303 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1307 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1308 rmp->external_port = htons (m->external_port);
1309 rmp->local_port = htons (m->local_port);
1313 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1315 vl_api_send_msg (reg, (u8 *) rmp);
1319 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1320 vl_api_registration_t * reg,
1323 vl_api_nat44_static_mapping_details_t *rmp;
1324 snat_main_t *sm = &snat_main;
1326 rmp = vl_msg_api_alloc (sizeof (*rmp));
1327 clib_memset (rmp, 0, sizeof (*rmp));
1329 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1330 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1331 rmp->external_sw_if_index = htonl (m->sw_if_index);
1332 rmp->vrf_id = htonl (m->vrf_id);
1333 rmp->context = context;
1336 rmp->flags |= NAT_API_IS_TWICE_NAT;
1340 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1344 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1345 rmp->external_port = htons (m->e_port);
1346 rmp->local_port = htons (m->l_port);
1349 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1351 vl_api_send_msg (reg, (u8 *) rmp);
1355 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1358 vl_api_registration_t *reg;
1359 snat_main_t *sm = &snat_main;
1360 snat_static_mapping_t *m;
1361 snat_static_map_resolve_t *rp;
1364 if (sm->deterministic)
1367 reg = vl_api_client_index_to_registration (mp->client_index);
1372 pool_foreach (m, sm->static_mappings,
1374 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1375 send_nat44_static_mapping_details (m, reg, mp->context);
1379 for (j = 0; j < vec_len (sm->to_resolve); j++)
1381 rp = sm->to_resolve + j;
1382 if (!rp->identity_nat)
1383 send_nat44_static_map_resolve_details (rp, reg, mp->context);
1388 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1393 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1399 vl_api_nat44_add_del_identity_mapping_t_handler
1400 (vl_api_nat44_add_del_identity_mapping_t * mp)
1402 snat_main_t *sm = &snat_main;
1403 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1406 u32 vrf_id, sw_if_index;
1408 snat_protocol_t proto = ~0;
1411 if (sm->deterministic)
1413 rv = VNET_API_ERROR_UNSUPPORTED;
1417 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1419 port = clib_net_to_host_u16 (mp->port);
1420 proto = ip_proto_to_snat_proto (mp->protocol);
1422 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1423 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1424 if (sw_if_index != ~0)
1427 memcpy (&addr.as_u8, mp->ip_address, 4);
1428 mp->tag[sizeof (mp->tag) - 1] = 0;
1429 tag = format (0, "%s", mp->tag);
1430 vec_terminate_c_string (tag);
1433 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1434 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1435 proto, mp->is_add, 0, 0, tag, 1);
1439 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1442 static void *vl_api_nat44_add_del_identity_mapping_t_print
1443 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1447 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1448 if (mp->sw_if_index != ~0)
1449 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1451 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1453 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1455 format (s, " protocol %d port %d", mp->protocol,
1456 clib_net_to_host_u16 (mp->port));
1458 if (mp->vrf_id != ~0)
1459 s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1465 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1466 vl_api_registration_t * reg, u32 context)
1468 vl_api_nat44_identity_mapping_details_t *rmp;
1469 snat_main_t *sm = &snat_main;
1470 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1472 rmp = vl_msg_api_alloc (sizeof (*rmp));
1473 clib_memset (rmp, 0, sizeof (*rmp));
1475 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1477 if (is_addr_only_static_mapping (m))
1478 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1480 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1481 rmp->port = htons (m->local_port);
1482 rmp->sw_if_index = ~0;
1483 rmp->vrf_id = htonl (local->vrf_id);
1484 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1485 rmp->context = context;
1487 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1489 vl_api_send_msg (reg, (u8 *) rmp);
1493 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1494 vl_api_registration_t * reg,
1497 vl_api_nat44_identity_mapping_details_t *rmp;
1498 snat_main_t *sm = &snat_main;
1500 rmp = vl_msg_api_alloc (sizeof (*rmp));
1501 clib_memset (rmp, 0, sizeof (*rmp));
1503 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1506 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1508 rmp->port = htons (m->l_port);
1509 rmp->sw_if_index = htonl (m->sw_if_index);
1510 rmp->vrf_id = htonl (m->vrf_id);
1511 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1512 rmp->context = context;
1514 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1516 vl_api_send_msg (reg, (u8 *) rmp);
1520 vl_api_nat44_identity_mapping_dump_t_handler
1521 (vl_api_nat44_identity_mapping_dump_t * mp)
1523 vl_api_registration_t *reg;
1524 snat_main_t *sm = &snat_main;
1525 snat_static_mapping_t *m;
1526 snat_static_map_resolve_t *rp;
1529 if (sm->deterministic)
1532 reg = vl_api_client_index_to_registration (mp->client_index);
1537 pool_foreach (m, sm->static_mappings,
1539 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1541 pool_foreach_index (j, m->locals,
1543 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1549 for (j = 0; j < vec_len (sm->to_resolve); j++)
1551 rp = sm->to_resolve + j;
1552 if (rp->identity_nat)
1553 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1557 static void *vl_api_nat44_identity_mapping_dump_t_print
1558 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1562 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1568 vl_api_nat44_add_del_interface_addr_t_handler
1569 (vl_api_nat44_add_del_interface_addr_t * mp)
1571 snat_main_t *sm = &snat_main;
1572 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1573 u32 sw_if_index = ntohl (mp->sw_if_index);
1577 if (sm->deterministic)
1579 rv = VNET_API_ERROR_UNSUPPORTED;
1583 is_del = !mp->is_add;
1585 VALIDATE_SW_IF_INDEX (mp);
1587 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1588 mp->flags & NAT_API_IS_TWICE_NAT);
1590 BAD_SW_IF_INDEX_LABEL;
1592 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1595 static void *vl_api_nat44_add_del_interface_addr_t_print
1596 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1600 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1601 s = format (s, "sw_if_index %d twice_nat %d %s",
1602 clib_host_to_net_u32 (mp->sw_if_index),
1603 mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1609 send_nat44_interface_addr_details (u32 sw_if_index,
1610 vl_api_registration_t * reg, u32 context,
1613 vl_api_nat44_interface_addr_details_t *rmp;
1614 snat_main_t *sm = &snat_main;
1616 rmp = vl_msg_api_alloc (sizeof (*rmp));
1617 clib_memset (rmp, 0, sizeof (*rmp));
1619 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1620 rmp->sw_if_index = ntohl (sw_if_index);
1623 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1624 rmp->context = context;
1626 vl_api_send_msg (reg, (u8 *) rmp);
1630 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1633 vl_api_registration_t *reg;
1634 snat_main_t *sm = &snat_main;
1637 if (sm->deterministic)
1640 reg = vl_api_client_index_to_registration (mp->client_index);
1645 vec_foreach (i, sm->auto_add_sw_if_indices)
1646 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1647 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1648 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1653 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1658 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1664 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1667 vl_api_nat44_user_details_t *rmp;
1668 snat_main_t *sm = &snat_main;
1669 ip4_main_t *im = &ip4_main;
1671 rmp = vl_msg_api_alloc (sizeof (*rmp));
1672 clib_memset (rmp, 0, sizeof (*rmp));
1673 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1675 if (!pool_is_free_index (im->fibs, u->fib_index))
1677 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1678 rmp->vrf_id = ntohl (fib->ft_table_id);
1681 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1682 rmp->nsessions = ntohl (u->nsessions);
1683 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1684 rmp->context = context;
1686 vl_api_send_msg (reg, (u8 *) rmp);
1690 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1692 vl_api_registration_t *reg;
1693 snat_main_t *sm = &snat_main;
1694 snat_main_per_thread_data_t *tsm;
1697 if (sm->deterministic)
1700 reg = vl_api_client_index_to_registration (mp->client_index);
1705 vec_foreach (tsm, sm->per_thread_data)
1707 pool_foreach (u, tsm->users,
1709 send_nat44_user_details (u, reg, mp->context);
1716 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1720 s = format (0, "SCRIPT: nat44_user_dump ");
1726 send_nat44_user_session_details (snat_session_t * s,
1727 vl_api_registration_t * reg, u32 context)
1729 vl_api_nat44_user_session_details_t *rmp;
1730 snat_main_t *sm = &snat_main;
1732 rmp = vl_msg_api_alloc (sizeof (*rmp));
1733 clib_memset (rmp, 0, sizeof (*rmp));
1735 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1736 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1737 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1739 if (snat_is_session_static (s))
1740 rmp->flags |= NAT_API_IS_STATIC;
1742 if (is_twice_nat_session (s))
1743 rmp->flags |= NAT_API_IS_TWICE_NAT;
1745 if (is_ed_session (s) || is_fwd_bypass_session (s))
1746 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1748 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1749 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1750 rmp->total_pkts = ntohl (s->total_pkts);
1751 rmp->context = context;
1752 if (snat_is_unk_proto_session (s))
1754 rmp->outside_port = 0;
1755 rmp->inside_port = 0;
1756 rmp->protocol = ntohs (s->in2out.port);
1760 rmp->outside_port = s->out2in.port;
1761 rmp->inside_port = s->in2out.port;
1762 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1764 if (is_ed_session (s) || is_fwd_bypass_session (s))
1766 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1767 rmp->ext_host_port = s->ext_host_port;
1768 if (is_twice_nat_session (s))
1770 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1771 rmp->ext_host_nat_port = s->ext_host_nat_port;
1775 vl_api_send_msg (reg, (u8 *) rmp);
1779 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1782 vl_api_registration_t *reg;
1783 snat_main_t *sm = &snat_main;
1784 snat_main_per_thread_data_t *tsm;
1786 clib_bihash_kv_8_8_t key, value;
1787 snat_user_key_t ukey;
1789 u32 session_index, head_index, elt_index;
1790 dlist_elt_t *head, *elt;
1793 if (sm->deterministic)
1796 reg = vl_api_client_index_to_registration (mp->client_index);
1800 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1801 ip.src_address.as_u32 = ukey.addr.as_u32;
1802 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1803 key.key = ukey.as_u64;
1804 if (sm->num_workers > 1)
1806 vec_elt_at_index (sm->per_thread_data,
1807 sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1809 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1810 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1812 u = pool_elt_at_index (tsm->users, value.value);
1813 if (!u->nsessions && !u->nstaticsessions)
1816 head_index = u->sessions_per_user_list_head_index;
1817 head = pool_elt_at_index (tsm->list_pool, head_index);
1818 elt_index = head->next;
1819 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1820 session_index = elt->value;
1821 while (session_index != ~0)
1823 s = pool_elt_at_index (tsm->sessions, session_index);
1825 send_nat44_user_session_details (s, reg, mp->context);
1827 elt_index = elt->next;
1828 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1829 session_index = elt->value;
1834 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1839 s = format (0, "SCRIPT: nat44_user_session_dump ");
1840 s = format (s, "ip_address %U vrf_id %d\n",
1841 format_ip4_address, mp->ip_address,
1842 clib_net_to_host_u32 (mp->vrf_id));
1847 static nat44_lb_addr_port_t *
1848 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1849 u32 addr_port_pair_num)
1852 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1853 vl_api_nat44_lb_addr_port_t *ap;
1855 for (i = 0; i < addr_port_pair_num; i++)
1857 ap = &addr_port_pairs[i];
1858 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1859 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1860 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1861 lb_addr_port.probability = ap->probability;
1862 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1863 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1866 return lb_addr_port_pairs;
1870 vl_api_nat44_add_del_lb_static_mapping_t_handler
1871 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1873 snat_main_t *sm = &snat_main;
1874 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1875 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1877 nat44_lb_addr_port_t *locals = 0;
1878 ip4_address_t e_addr;
1879 snat_protocol_t proto;
1882 if (!sm->endpoint_dependent)
1884 rv = VNET_API_ERROR_UNSUPPORTED;
1889 unformat_nat44_lb_addr_port (mp->locals,
1890 clib_net_to_host_u32 (mp->local_num));
1891 clib_memcpy (&e_addr, mp->external_addr, 4);
1892 proto = ip_proto_to_snat_proto (mp->protocol);
1894 if (mp->flags & NAT_API_IS_TWICE_NAT)
1895 twice_nat = TWICE_NAT;
1896 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1897 twice_nat = TWICE_NAT_SELF;
1898 mp->tag[sizeof (mp->tag) - 1] = 0;
1899 tag = format (0, "%s", mp->tag);
1900 vec_terminate_c_string (tag);
1903 nat44_add_del_lb_static_mapping (e_addr,
1904 clib_net_to_host_u16 (mp->external_port),
1905 proto, locals, mp->is_add,
1907 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1908 clib_net_to_host_u32 (mp->affinity));
1914 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1917 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1918 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1922 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1923 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1925 mp->flags & NAT_API_IS_TWICE_NAT,
1926 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1932 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1933 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1935 snat_main_t *sm = &snat_main;
1936 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1938 ip4_address_t e_addr, l_addr;
1939 snat_protocol_t proto;
1941 if (!sm->endpoint_dependent)
1943 rv = VNET_API_ERROR_UNSUPPORTED;
1947 clib_memcpy (&e_addr, mp->external_addr, 4);
1948 clib_memcpy (&l_addr, mp->local.addr, 4);
1949 proto = ip_proto_to_snat_proto (mp->protocol);
1952 nat44_lb_static_mapping_add_del_local (e_addr,
1953 clib_net_to_host_u16
1954 (mp->external_port), l_addr,
1955 clib_net_to_host_u16 (mp->
1958 clib_net_to_host_u32 (mp->
1960 mp->local.probability, mp->is_add);
1963 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1966 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
1967 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
1971 s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
1972 s = format (s, "is_add %d", mp->is_add);
1978 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1979 vl_api_registration_t * reg,
1982 vl_api_nat44_lb_static_mapping_details_t *rmp;
1983 snat_main_t *sm = &snat_main;
1984 nat44_lb_addr_port_t *ap;
1985 vl_api_nat44_lb_addr_port_t *locals;
1989 vl_msg_api_alloc (sizeof (*rmp) +
1990 (pool_elts (m->locals) *
1991 sizeof (nat44_lb_addr_port_t)));
1992 clib_memset (rmp, 0, sizeof (*rmp));
1994 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1996 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1997 rmp->external_port = ntohs (m->external_port);
1998 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1999 rmp->context = context;
2001 if (m->twice_nat == TWICE_NAT)
2002 rmp->flags |= NAT_API_IS_TWICE_NAT;
2003 else if (m->twice_nat == TWICE_NAT_SELF)
2004 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
2005 if (is_out2in_only_static_mapping (m))
2006 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
2008 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
2010 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
2012 pool_foreach (ap, m->locals,
2014 clib_memcpy (locals->addr, &(ap->addr), 4);
2015 locals->port = htons (ap->port);
2016 locals->probability = ap->probability;
2017 locals->vrf_id = ntohl (ap->vrf_id);
2022 rmp->local_num = ntohl (local_num);
2024 vl_api_send_msg (reg, (u8 *) rmp);
2028 vl_api_nat44_lb_static_mapping_dump_t_handler
2029 (vl_api_nat44_lb_static_mapping_dump_t * mp)
2031 vl_api_registration_t *reg;
2032 snat_main_t *sm = &snat_main;
2033 snat_static_mapping_t *m;
2035 if (!sm->endpoint_dependent)
2038 reg = vl_api_client_index_to_registration (mp->client_index);
2043 pool_foreach (m, sm->static_mappings,
2045 if (is_lb_static_mapping(m))
2046 send_nat44_lb_static_mapping_details (m, reg, mp->context);
2051 static void *vl_api_nat44_lb_static_mapping_dump_t_print
2052 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
2056 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
2062 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
2064 snat_main_t *sm = &snat_main;
2065 vl_api_nat44_del_session_reply_t *rmp;
2066 ip4_address_t addr, eh_addr;
2071 snat_protocol_t proto;
2073 if (sm->deterministic)
2075 rv = VNET_API_ERROR_UNSUPPORTED;
2079 memcpy (&addr.as_u8, mp->address, 4);
2080 port = clib_net_to_host_u16 (mp->port);
2081 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
2082 proto = ip_proto_to_snat_proto (mp->protocol);
2083 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
2084 eh_port = clib_net_to_host_u16 (mp->ext_host_port);
2086 is_in = mp->flags & NAT_API_IS_INSIDE;
2088 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2090 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
2093 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
2096 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
2100 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
2105 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
2106 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
2107 format_ip4_address, mp->address,
2108 clib_net_to_host_u16 (mp->port),
2109 mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
2110 mp->flags & NAT_API_IS_INSIDE);
2111 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2112 s = format (s, "ext_host_address %U ext_host_port %d",
2113 format_ip4_address, mp->ext_host_address,
2114 clib_net_to_host_u16 (mp->ext_host_port));
2120 vl_api_nat44_forwarding_enable_disable_t_handler
2121 (vl_api_nat44_forwarding_enable_disable_t * mp)
2123 snat_main_t *sm = &snat_main;
2124 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
2126 u32 *ses_to_be_removed = 0, *ses_index;
2127 snat_main_per_thread_data_t *tsm;
2130 sm->forwarding_enabled = mp->enable != 0;
2132 if (mp->enable == 0)
2135 vec_foreach (tsm, sm->per_thread_data)
2137 pool_foreach (s, tsm->sessions,
2139 if (is_fwd_bypass_session(s))
2141 vec_add1 (ses_to_be_removed, s - tsm->sessions);
2144 vec_foreach (ses_index, ses_to_be_removed)
2146 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2147 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2148 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2150 vec_free (ses_to_be_removed);
2155 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2158 static void *vl_api_nat44_forwarding_enable_disable_t_print
2159 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2163 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2164 s = format (s, "enable %d", mp->enable != 0);
2170 vl_api_nat44_forwarding_is_enabled_t_handler
2171 (vl_api_nat44_forwarding_is_enabled_t * mp)
2173 vl_api_registration_t *reg;
2174 snat_main_t *sm = &snat_main;
2175 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2177 reg = vl_api_client_index_to_registration (mp->client_index);
2181 rmp = vl_msg_api_alloc (sizeof (*rmp));
2182 clib_memset (rmp, 0, sizeof (*rmp));
2184 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2185 rmp->context = mp->context;
2187 rmp->enabled = sm->forwarding_enabled;
2189 vl_api_send_msg (reg, (u8 *) rmp);
2192 static void *vl_api_nat44_forwarding_is_enabled_t_print
2193 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2197 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2202 /*******************************/
2203 /*** Deterministic NAT (CGN) ***/
2204 /*******************************/
2207 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
2209 snat_main_t *sm = &snat_main;
2210 vl_api_nat_det_add_del_map_reply_t *rmp;
2212 ip4_address_t in_addr, out_addr;
2214 if (!sm->deterministic)
2216 rv = VNET_API_ERROR_UNSUPPORTED;
2220 clib_memcpy (&in_addr, mp->in_addr, 4);
2221 clib_memcpy (&out_addr, mp->out_addr, 4);
2222 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
2223 mp->out_plen, mp->is_add);
2226 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
2230 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
2235 s = format (0, "SCRIPT: nat_det_add_del_map ");
2236 s = format (s, "inside address %U/%d outside address %U/%d\n",
2237 format_ip4_address, mp->in_addr, mp->in_plen,
2238 format_ip4_address, mp->out_addr, mp->out_plen);
2244 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
2246 snat_main_t *sm = &snat_main;
2247 vl_api_nat_det_forward_reply_t *rmp;
2249 u16 lo_port = 0, hi_port = 0;
2251 ip4_address_t in_addr, out_addr;
2253 if (!sm->deterministic)
2255 rv = VNET_API_ERROR_UNSUPPORTED;
2256 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
2260 out_addr.as_u32 = 0;
2261 clib_memcpy (&in_addr, mp->in_addr, 4);
2262 dm = snat_det_map_by_user (sm, &in_addr);
2265 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2269 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2270 hi_port = lo_port + dm->ports_per_host - 1;
2274 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
2276 rmp->out_port_lo = ntohs (lo_port);
2277 rmp->out_port_hi = ntohs (hi_port);
2278 clib_memcpy (rmp->out_addr, &out_addr, 4);
2284 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
2288 s = format (0, "SCRIPT: nat_det_forward");
2289 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
2295 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
2297 snat_main_t *sm = &snat_main;
2298 vl_api_nat_det_reverse_reply_t *rmp;
2300 ip4_address_t out_addr, in_addr;
2303 if (!sm->deterministic)
2305 rv = VNET_API_ERROR_UNSUPPORTED;
2306 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
2311 clib_memcpy (&out_addr, mp->out_addr, 4);
2312 dm = snat_det_map_by_out (sm, &out_addr);
2315 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2319 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
2323 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
2325 clib_memcpy (rmp->in_addr, &in_addr, 4);
2331 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
2335 s = format (0, "SCRIPT: nat_det_reverse");
2336 s = format (s, "outside ip address %U outside port %d",
2337 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
2343 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
2346 vl_api_nat_det_map_details_t *rmp;
2347 snat_main_t *sm = &snat_main;
2349 rmp = vl_msg_api_alloc (sizeof (*rmp));
2350 clib_memset (rmp, 0, sizeof (*rmp));
2351 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
2352 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
2353 rmp->in_plen = m->in_plen;
2354 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
2355 rmp->out_plen = m->out_plen;
2356 rmp->sharing_ratio = htonl (m->sharing_ratio);
2357 rmp->ports_per_host = htons (m->ports_per_host);
2358 rmp->ses_num = htonl (m->ses_num);
2359 rmp->context = context;
2361 vl_api_send_msg (reg, (u8 *) rmp);
2365 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
2367 vl_api_registration_t *reg;
2368 snat_main_t *sm = &snat_main;
2371 if (!sm->deterministic)
2374 reg = vl_api_client_index_to_registration (mp->client_index);
2379 vec_foreach(m, sm->det_maps)
2380 sent_nat_det_map_details(m, reg, mp->context);
2385 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
2389 s = format (0, "SCRIPT: nat_det_map_dump ");
2395 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
2398 snat_main_t *sm = &snat_main;
2399 vl_api_nat_det_close_session_out_reply_t *rmp;
2400 ip4_address_t out_addr, ext_addr, in_addr;
2401 snat_det_out_key_t key;
2403 snat_det_session_t *ses;
2406 if (!sm->deterministic)
2408 rv = VNET_API_ERROR_UNSUPPORTED;
2412 clib_memcpy (&out_addr, mp->out_addr, 4);
2413 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2415 dm = snat_det_map_by_out (sm, &out_addr);
2418 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2421 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
2422 key.ext_host_addr = ext_addr;
2423 key.ext_host_port = mp->ext_port;
2424 key.out_port = mp->out_port;
2425 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
2428 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2431 snat_det_ses_close (dm, ses);
2434 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2438 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
2443 s = format (0, "SCRIPT: nat_det_close_session_out ");
2444 s = format (s, "out_addr %U out_port %d "
2445 "ext_addr %U ext_port %d\n",
2446 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
2447 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2453 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
2456 snat_main_t *sm = &snat_main;
2457 vl_api_nat_det_close_session_in_reply_t *rmp;
2458 ip4_address_t in_addr, ext_addr;
2459 snat_det_out_key_t key;
2461 snat_det_session_t *ses;
2464 if (!sm->deterministic)
2466 rv = VNET_API_ERROR_UNSUPPORTED;
2470 clib_memcpy (&in_addr, mp->in_addr, 4);
2471 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2473 dm = snat_det_map_by_user (sm, &in_addr);
2476 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2479 key.ext_host_addr = ext_addr;
2480 key.ext_host_port = mp->ext_port;
2481 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
2484 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2487 snat_det_ses_close (dm, ses);
2490 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2494 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
2498 s = format (0, "SCRIPT: nat_det_close_session_in ");
2499 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
2500 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
2501 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2507 send_nat_det_session_details (snat_det_session_t * s,
2508 vl_api_registration_t * reg, u32 context)
2510 vl_api_nat_det_session_details_t *rmp;
2511 snat_main_t *sm = &snat_main;
2513 rmp = vl_msg_api_alloc (sizeof (*rmp));
2514 clib_memset (rmp, 0, sizeof (*rmp));
2515 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
2516 rmp->in_port = s->in_port;
2517 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
2518 rmp->ext_port = s->out.ext_host_port;
2519 rmp->out_port = s->out.out_port;
2520 rmp->state = s->state;
2521 rmp->expire = ntohl (s->expire);
2522 rmp->context = context;
2524 vl_api_send_msg (reg, (u8 *) rmp);
2528 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
2530 vl_api_registration_t *reg;
2531 snat_main_t *sm = &snat_main;
2532 ip4_address_t user_addr;
2534 snat_det_session_t *s, empty_ses;
2537 if (!sm->deterministic)
2540 reg = vl_api_client_index_to_registration (mp->client_index);
2544 clib_memset (&empty_ses, 0, sizeof (empty_ses));
2545 clib_memcpy (&user_addr, mp->user_addr, 4);
2546 dm = snat_det_map_by_user (sm, &user_addr);
2550 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2551 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2554 send_nat_det_session_details (s, reg, mp->context);
2560 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2565 s = format (0, "SCRIPT: nat_det_session_dump ");
2566 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2576 vl_api_nat64_add_del_pool_addr_range_t_handler
2577 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2579 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2580 snat_main_t *sm = &snat_main;
2582 ip4_address_t this_addr;
2583 u32 start_host_order, end_host_order;
2588 tmp = (u32 *) mp->start_addr;
2589 start_host_order = clib_host_to_net_u32 (tmp[0]);
2590 tmp = (u32 *) mp->end_addr;
2591 end_host_order = clib_host_to_net_u32 (tmp[0]);
2593 count = (end_host_order - start_host_order) + 1;
2595 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2597 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2599 for (i = 0; i < count; i++)
2601 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2604 increment_v4_address (&this_addr);
2608 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2611 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2612 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2616 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2617 s = format (s, "%U - %U vrf_id %u %s\n",
2618 format_ip4_address, mp->start_addr,
2619 format_ip4_address, mp->end_addr,
2620 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2625 typedef struct nat64_api_walk_ctx_t_
2627 vl_api_registration_t *reg;
2630 } nat64_api_walk_ctx_t;
2633 nat64_api_pool_walk (snat_address_t * a, void *arg)
2635 vl_api_nat64_pool_addr_details_t *rmp;
2636 snat_main_t *sm = &snat_main;
2637 nat64_api_walk_ctx_t *ctx = arg;
2639 rmp = vl_msg_api_alloc (sizeof (*rmp));
2640 clib_memset (rmp, 0, sizeof (*rmp));
2641 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2642 clib_memcpy (rmp->address, &(a->addr), 4);
2643 if (a->fib_index != ~0)
2645 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2648 rmp->vrf_id = ntohl (fib->ft_table_id);
2652 rmp->context = ctx->context;
2654 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2660 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2662 vl_api_registration_t *reg;
2664 reg = vl_api_client_index_to_registration (mp->client_index);
2668 nat64_api_walk_ctx_t ctx = {
2670 .context = mp->context,
2673 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2677 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2682 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2688 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2691 snat_main_t *sm = &snat_main;
2692 vl_api_nat64_add_del_interface_reply_t *rmp;
2695 VALIDATE_SW_IF_INDEX (mp);
2698 nat64_add_del_interface (ntohl (mp->sw_if_index),
2699 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2701 BAD_SW_IF_INDEX_LABEL;
2703 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2707 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2712 s = format (0, "SCRIPT: nat64_add_del_interface ");
2713 s = format (s, "sw_if_index %d %s %s",
2714 clib_host_to_net_u32 (mp->sw_if_index),
2715 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2716 mp->is_add ? "" : "del");
2722 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2724 vl_api_nat64_interface_details_t *rmp;
2725 snat_main_t *sm = &snat_main;
2726 nat64_api_walk_ctx_t *ctx = arg;
2728 rmp = vl_msg_api_alloc (sizeof (*rmp));
2729 clib_memset (rmp, 0, sizeof (*rmp));
2730 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2731 rmp->sw_if_index = ntohl (i->sw_if_index);
2733 if (nat_interface_is_inside (i))
2734 rmp->flags |= NAT_API_IS_INSIDE;
2735 if (nat_interface_is_outside (i))
2736 rmp->flags |= NAT_API_IS_OUTSIDE;
2738 rmp->context = ctx->context;
2740 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2746 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2748 vl_api_registration_t *reg;
2750 reg = vl_api_client_index_to_registration (mp->client_index);
2754 nat64_api_walk_ctx_t ctx = {
2756 .context = mp->context,
2759 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2763 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2768 s = format (0, "SCRIPT: snat_interface_dump ");
2774 vl_api_nat64_add_del_static_bib_t_handler
2775 (vl_api_nat64_add_del_static_bib_t * mp)
2777 snat_main_t *sm = &snat_main;
2778 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2779 ip6_address_t in_addr;
2780 ip4_address_t out_addr;
2783 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2784 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2787 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2788 clib_net_to_host_u16 (mp->i_port),
2789 clib_net_to_host_u16 (mp->o_port),
2791 clib_net_to_host_u32 (mp->vrf_id),
2794 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2797 static void *vl_api_nat64_add_del_static_bib_t_print
2798 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2802 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2803 s = format (s, "protocol %d i_addr %U o_addr %U ",
2805 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2807 if (mp->vrf_id != ~0)
2808 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2814 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2816 vl_api_nat64_bib_details_t *rmp;
2817 snat_main_t *sm = &snat_main;
2818 nat64_api_walk_ctx_t *ctx = arg;
2821 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2825 rmp = vl_msg_api_alloc (sizeof (*rmp));
2826 clib_memset (rmp, 0, sizeof (*rmp));
2827 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2828 rmp->context = ctx->context;
2829 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2830 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2831 rmp->i_port = bibe->in_port;
2832 rmp->o_port = bibe->out_port;
2833 rmp->vrf_id = ntohl (fib->ft_table_id);
2834 rmp->proto = bibe->proto;
2835 if (bibe->is_static)
2836 rmp->flags |= NAT_API_IS_STATIC;
2837 rmp->ses_num = ntohl (bibe->ses_num);
2839 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2845 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2847 vl_api_registration_t *reg;
2848 nat64_main_t *nm = &nat64_main;
2851 reg = vl_api_client_index_to_registration (mp->client_index);
2855 nat64_api_walk_ctx_t ctx = {
2857 .context = mp->context,
2861 vec_foreach (db, nm->db)
2862 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2867 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2871 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2877 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2879 vl_api_nat64_st_details_t *rmp;
2880 snat_main_t *sm = &snat_main;
2881 nat64_api_walk_ctx_t *ctx = arg;
2882 nat64_db_bib_entry_t *bibe;
2885 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2889 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2893 rmp = vl_msg_api_alloc (sizeof (*rmp));
2894 clib_memset (rmp, 0, sizeof (*rmp));
2895 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2896 rmp->context = ctx->context;
2897 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2898 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2899 rmp->il_port = bibe->in_port;
2900 rmp->ol_port = bibe->out_port;
2901 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2902 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2903 rmp->il_port = ste->r_port;
2904 rmp->vrf_id = ntohl (fib->ft_table_id);
2905 rmp->proto = ste->proto;
2907 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2913 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2915 vl_api_registration_t *reg;
2916 nat64_main_t *nm = &nat64_main;
2919 reg = vl_api_client_index_to_registration (mp->client_index);
2923 nat64_api_walk_ctx_t ctx = {
2925 .context = mp->context,
2929 vec_foreach (db, nm->db)
2932 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2938 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2942 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2948 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2950 vl_api_nat64_add_del_prefix_reply_t *rmp;
2951 snat_main_t *sm = &snat_main;
2952 ip6_address_t prefix;
2955 memcpy (&prefix.as_u8, mp->prefix.address, 16);
2958 nat64_add_del_prefix (&prefix, mp->prefix.len,
2959 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2960 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2964 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2969 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2970 format_ip6_address, mp->prefix.address, mp->prefix.len,
2971 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2977 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2979 vl_api_nat64_prefix_details_t *rmp;
2980 snat_main_t *sm = &snat_main;
2981 nat64_api_walk_ctx_t *ctx = arg;
2983 rmp = vl_msg_api_alloc (sizeof (*rmp));
2984 clib_memset (rmp, 0, sizeof (*rmp));
2985 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2986 clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
2987 rmp->prefix.len = p->plen;
2988 rmp->vrf_id = ntohl (p->vrf_id);
2989 rmp->context = ctx->context;
2991 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2997 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2999 vl_api_registration_t *reg;
3001 reg = vl_api_client_index_to_registration (mp->client_index);
3005 nat64_api_walk_ctx_t ctx = {
3007 .context = mp->context,
3010 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
3014 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
3019 s = format (0, "SCRIPT: nat64_prefix_dump\n");
3025 vl_api_nat64_add_del_interface_addr_t_handler
3026 (vl_api_nat64_add_del_interface_addr_t * mp)
3028 snat_main_t *sm = &snat_main;
3029 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
3030 u32 sw_if_index = ntohl (mp->sw_if_index);
3033 VALIDATE_SW_IF_INDEX (mp);
3035 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
3037 BAD_SW_IF_INDEX_LABEL;
3039 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
3042 static void *vl_api_nat64_add_del_interface_addr_t_print
3043 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
3047 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
3048 s = format (s, "sw_if_index %d %s",
3049 clib_host_to_net_u32 (mp->sw_if_index),
3050 mp->is_add ? "" : "del");
3060 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
3062 vl_api_dslite_set_aftr_addr_reply_t *rmp;
3063 snat_main_t *sm = &snat_main;
3064 dslite_main_t *dm = &dslite_main;
3066 ip6_address_t ip6_addr;
3067 ip4_address_t ip4_addr;
3069 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3070 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3072 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
3074 rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
3076 REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
3080 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
3085 s = format (0, "SCRIPT: dslite_set_aftr_addr ");
3086 s = format (s, "ip6_addr %U ip4_addr %U\n",
3087 format_ip6_address, mp->ip6_addr,
3088 format_ip4_address, mp->ip4_addr);
3094 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
3096 snat_main_t *sm = &snat_main;
3097 vl_api_dslite_get_aftr_addr_reply_t *rmp;
3098 dslite_main_t *dm = &dslite_main;
3102 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
3104 memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
3105 memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
3111 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
3116 s = format (0, "SCRIPT: dslite_get_aftr_addr");
3122 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
3124 vl_api_dslite_set_b4_addr_reply_t *rmp;
3125 snat_main_t *sm = &snat_main;
3126 dslite_main_t *dm = &dslite_main;
3128 ip6_address_t ip6_addr;
3129 ip4_address_t ip4_addr;
3131 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3132 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3134 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
3136 rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
3138 REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
3142 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
3147 s = format (0, "SCRIPT: dslite_set_b4_addr ");
3148 s = format (s, "ip6_addr %U ip4_addr %U\n",
3149 format_ip6_address, mp->ip6_addr,
3150 format_ip6_address, mp->ip4_addr);
3156 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
3158 snat_main_t *sm = &snat_main;
3159 vl_api_dslite_get_b4_addr_reply_t *rmp;
3160 dslite_main_t *dm = &dslite_main;
3164 REPLY_MACRO2 (VL_API_DSLITE_GET_B4_ADDR_REPLY,
3166 memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
3167 memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
3173 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
3178 s = format (0, "SCRIPT: dslite_get_b4_addr");
3184 vl_api_dslite_add_del_pool_addr_range_t_handler
3185 (vl_api_dslite_add_del_pool_addr_range_t * mp)
3187 vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
3188 snat_main_t *sm = &snat_main;
3189 dslite_main_t *dm = &dslite_main;
3191 ip4_address_t this_addr;
3192 u32 start_host_order, end_host_order;
3196 tmp = (u32 *) mp->start_addr;
3197 start_host_order = clib_host_to_net_u32 (tmp[0]);
3198 tmp = (u32 *) mp->end_addr;
3199 end_host_order = clib_host_to_net_u32 (tmp[0]);
3201 count = (end_host_order - start_host_order) + 1;
3202 memcpy (&this_addr.as_u8, mp->start_addr, 4);
3204 for (i = 0; i < count; i++)
3206 if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
3209 increment_v4_address (&this_addr);
3213 REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
3217 send_dslite_address_details (snat_address_t * ap,
3218 vl_api_registration_t * reg, u32 context)
3220 vl_api_dslite_address_details_t *rmp;
3221 snat_main_t *sm = &snat_main;
3223 rmp = vl_msg_api_alloc (sizeof (*rmp));
3225 clib_memset (rmp, 0, sizeof (*rmp));
3227 rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
3228 clib_memcpy (rmp->ip_address, &(ap->addr), 4);
3229 rmp->context = context;
3231 vl_api_send_msg (reg, (u8 *) rmp);
3235 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
3237 vl_api_registration_t *reg;
3238 dslite_main_t *dm = &dslite_main;
3241 reg = vl_api_client_index_to_registration (mp->client_index);
3246 vec_foreach (ap, dm->addr_pool)
3248 send_dslite_address_details (ap, reg, mp->context);
3254 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
3259 s = format (0, "SCRIPT: dslite_address_dump ");
3264 static void *vl_api_dslite_add_del_pool_addr_range_t_print
3265 (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
3269 s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
3270 s = format (s, "%U - %U\n",
3271 format_ip4_address, mp->start_addr,
3272 format_ip4_address, mp->end_addr);
3283 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
3286 snat_main_t *sm = &snat_main;
3287 vl_api_nat66_add_del_interface_reply_t *rmp;
3290 VALIDATE_SW_IF_INDEX (mp);
3293 nat66_interface_add_del (ntohl (mp->sw_if_index),
3294 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
3296 BAD_SW_IF_INDEX_LABEL;
3298 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
3302 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
3307 s = format (0, "SCRIPT: nat66_add_del_interface ");
3308 s = format (s, "sw_if_index %d %s %s",
3309 clib_host_to_net_u32 (mp->sw_if_index),
3310 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
3311 mp->is_add ? "" : "del");
3317 vl_api_nat66_add_del_static_mapping_t_handler
3318 (vl_api_nat66_add_del_static_mapping_t * mp)
3320 snat_main_t *sm = &snat_main;
3321 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
3322 ip6_address_t l_addr, e_addr;
3325 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
3326 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
3329 nat66_static_mapping_add_del (&l_addr, &e_addr,
3330 clib_net_to_host_u32 (mp->vrf_id),
3333 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
3336 static void *vl_api_nat66_add_del_static_mapping_t_print
3337 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
3341 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
3342 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
3343 format_ip6_address, mp->local_ip_address,
3344 format_ip6_address, mp->external_ip_address,
3345 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
3350 typedef struct nat66_api_walk_ctx_t_
3352 vl_api_registration_t *rp;
3354 } nat66_api_walk_ctx_t;
3357 nat66_api_interface_walk (snat_interface_t * i, void *arg)
3359 vl_api_nat66_interface_details_t *rmp;
3360 snat_main_t *sm = &snat_main;
3361 nat66_api_walk_ctx_t *ctx = arg;
3363 rmp = vl_msg_api_alloc (sizeof (*rmp));
3364 clib_memset (rmp, 0, sizeof (*rmp));
3365 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
3366 rmp->sw_if_index = ntohl (i->sw_if_index);
3367 if (nat_interface_is_inside (i))
3368 rmp->flags |= NAT_API_IS_INSIDE;
3369 rmp->context = ctx->context;
3371 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3377 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
3379 vl_api_registration_t *rp;
3381 rp = vl_api_client_index_to_registration (mp->client_index);
3385 nat66_api_walk_ctx_t ctx = {
3387 .context = mp->context,
3390 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
3394 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
3399 s = format (0, "SCRIPT: nat66_interface_dump ");
3405 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
3407 vl_api_nat66_static_mapping_details_t *rmp;
3408 nat66_main_t *nm = &nat66_main;
3409 snat_main_t *sm = &snat_main;
3410 nat66_api_walk_ctx_t *ctx = arg;
3414 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
3418 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
3420 rmp = vl_msg_api_alloc (sizeof (*rmp));
3421 clib_memset (rmp, 0, sizeof (*rmp));
3423 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
3424 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
3425 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
3426 rmp->vrf_id = ntohl (fib->ft_table_id);
3427 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
3428 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
3429 rmp->context = ctx->context;
3431 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3437 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
3440 vl_api_registration_t *rp;
3442 rp = vl_api_client_index_to_registration (mp->client_index);
3446 nat66_api_walk_ctx_t ctx = {
3448 .context = mp->context,
3451 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
3455 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
3460 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
3466 /* List of message types that this plugin understands */
3467 #define foreach_snat_plugin_api_msg \
3468 _(NAT_CONTROL_PING, nat_control_ping) \
3469 _(NAT_SHOW_CONFIG, nat_show_config) \
3470 _(NAT_SET_WORKERS, nat_set_workers) \
3471 _(NAT_WORKER_DUMP, nat_worker_dump) \
3472 _(NAT_SET_LOG_LEVEL, nat_set_log_level) \
3473 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
3474 _(NAT_SET_REASS, nat_set_reass) \
3475 _(NAT_GET_REASS, nat_get_reass) \
3476 _(NAT_REASS_DUMP, nat_reass_dump) \
3477 _(NAT_SET_TIMEOUTS, nat_set_timeouts) \
3478 _(NAT_GET_TIMEOUTS, nat_get_timeouts) \
3479 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg) \
3480 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg) \
3481 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping) \
3482 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping) \
3483 _(NAT_HA_SET_LISTENER, nat_ha_set_listener) \
3484 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover) \
3485 _(NAT_HA_GET_LISTENER, nat_ha_get_listener) \
3486 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover) \
3487 _(NAT_HA_FLUSH, nat_ha_flush) \
3488 _(NAT_HA_RESYNC, nat_ha_resync) \
3489 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
3490 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
3491 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
3492 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3493 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3494 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3495 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3496 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3497 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3498 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3499 _(NAT44_USER_DUMP, nat44_user_dump) \
3500 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3501 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3502 nat44_interface_add_del_output_feature) \
3503 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3504 nat44_interface_output_feature_dump) \
3505 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3506 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL, \
3507 nat44_lb_static_mapping_add_del_local) \
3508 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3509 _(NAT44_DEL_SESSION, nat44_del_session) \
3510 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3511 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3512 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3513 _(NAT_DET_FORWARD, nat_det_forward) \
3514 _(NAT_DET_REVERSE, nat_det_reverse) \
3515 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3516 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3517 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3518 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3519 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3520 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3521 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3522 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3523 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3524 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3525 _(NAT64_ST_DUMP, nat64_st_dump) \
3526 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3527 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3528 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3529 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range) \
3530 _(DSLITE_ADDRESS_DUMP, dslite_address_dump) \
3531 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr) \
3532 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr) \
3533 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr) \
3534 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr) \
3535 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3536 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3537 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3538 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3540 /* Set up the API message handling tables */
3541 static clib_error_t *
3542 snat_plugin_api_hookup (vlib_main_t * vm)
3544 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3546 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3548 vl_api_##n##_t_handler, \
3550 vl_api_##n##_t_endian, \
3551 vl_api_##n##_t_print, \
3552 sizeof(vl_api_##n##_t), 1);
3553 foreach_snat_plugin_api_msg;
3559 #define vl_msg_name_crc_list
3560 #include <nat/nat_all_api_h.h>
3561 #undef vl_msg_name_crc_list
3564 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3566 #define _(id,n,crc) \
3567 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3568 foreach_vl_msg_name_crc_nat;
3573 plugin_custom_dump_configure (snat_main_t * sm)
3575 #define _(n,f) sm->api_main->msg_print_handlers \
3576 [VL_API_##n + sm->msg_id_base] \
3577 = (void *) vl_api_##f##_t_print;
3578 foreach_snat_plugin_api_msg;
3583 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3586 clib_error_t *error = 0;
3588 name = format (0, "nat_%08x%c", api_version, 0);
3590 /* Ask for a correctly-sized block of API message decode slots */
3592 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3594 error = snat_plugin_api_hookup (vm);
3596 /* Add our API messages to the global name_crc hash table */
3597 setup_message_id_table (sm, sm->api_main);
3599 plugin_custom_dump_configure (sm);
3607 * fd.io coding-style-patch-verification: ON
3610 * eval: (c-set-style "gnu")