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_lb_addr_port_t_endian vl_noop_handler
37 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
38 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
40 /* define message structures */
42 #include <nat/nat_all_api_h.h>
45 /* define generated endian-swappers */
47 #include <nat/nat_all_api_h.h>
50 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
52 #define REPLY_MSG_ID_BASE sm->msg_id_base
53 #include <vlibapi/api_helper_macros.h>
55 /* Get the API version number */
56 #define vl_api_version(n,v) static u32 api_version=(v);
57 #include <nat/nat_all_api_h.h>
60 /* Macro to finish up custom dump fns */
63 vl_print (handle, (char *)s); \
67 /******************************/
68 /*** Common NAT plugin APIs ***/
69 /******************************/
72 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
74 vl_api_nat_control_ping_reply_t *rmp;
75 snat_main_t *sm = &snat_main;
79 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
81 rmp->vpe_pid = ntohl (getpid ());
87 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
91 s = format (0, "SCRIPT: nat_control_ping ");
97 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
99 vl_api_nat_show_config_reply_t *rmp;
100 snat_main_t *sm = &snat_main;
101 dslite_main_t *dm = &dslite_main;
102 nat64_main_t *n64m = &nat64_main;
106 REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
108 rmp->translation_buckets = htonl (sm->translation_buckets);
109 rmp->translation_memory_size = htonl (sm->translation_memory_size);
110 rmp->user_buckets = htonl (sm->user_buckets);
111 rmp->user_memory_size = htonl (sm->user_memory_size);
112 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
113 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
114 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
115 rmp->static_mapping_only = sm->static_mapping_only;
116 rmp->static_mapping_connection_tracking =
117 sm->static_mapping_connection_tracking;
118 rmp->deterministic = sm->deterministic;
119 rmp->endpoint_dependent = sm->endpoint_dependent;
120 rmp->out2in_dpo = sm->out2in_dpo;
121 rmp->dslite_ce = dm->is_ce;
122 rmp->nat64_bib_buckets = n64m->bib_buckets;
123 rmp->nat64_bib_memory_size = n64m->bib_memory_size;
124 rmp->nat64_st_buckets = n64m->st_buckets;
125 rmp->nat64_st_memory_size = n64m->st_memory_size;
131 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
135 s = format (0, "SCRIPT: nat_show_config ");
141 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
143 snat_main_t *sm = &snat_main;
144 vl_api_nat_set_workers_reply_t *rmp;
149 if (sm->deterministic)
151 rv = VNET_API_ERROR_UNSUPPORTED;
155 mask = clib_net_to_host_u64 (mp->worker_mask);
157 if (sm->num_workers < 2)
159 rv = VNET_API_ERROR_FEATURE_DISABLED;
163 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
164 rv = snat_set_workers (bitmap);
165 clib_bitmap_free (bitmap);
168 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
172 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
178 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
180 s = format (0, "SCRIPT: nat_set_workers ");
181 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
183 clib_bitmap_foreach (i, bitmap,
186 s = format (s, "%d", i);
188 s = format (s, ",%d", i);
192 clib_bitmap_free (bitmap);
197 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
200 vl_api_nat_worker_details_t *rmp;
201 snat_main_t *sm = &snat_main;
202 vlib_worker_thread_t *w =
203 vlib_worker_threads + worker_index + sm->first_worker_index;
204 u32 len = vec_len (w->name);
209 rmp = vl_msg_api_alloc (sizeof (*rmp) + len);
210 clib_memset (rmp, 0, sizeof (*rmp) + len);
211 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
212 rmp->context = context;
213 rmp->worker_index = htonl (worker_index);
214 rmp->lcore_id = htonl (w->cpu_id);
217 vl_api_to_api_string (len, (char *) w->name, &rmp->name);
219 vl_api_send_msg (reg, (u8 *) rmp);
223 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
225 vl_api_registration_t *reg;
226 snat_main_t *sm = &snat_main;
229 if (sm->deterministic)
232 reg = vl_api_client_index_to_registration (mp->client_index);
237 vec_foreach (worker_index, sm->workers)
238 send_nat_worker_details(*worker_index, reg, mp->context);
243 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
247 s = format (0, "SCRIPT: nat_worker_dump ");
253 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
255 snat_main_t *sm = &snat_main;
256 vl_api_nat_set_log_level_reply_t *rmp;
259 if (sm->log_level > NAT_LOG_DEBUG)
260 rv = VNET_API_ERROR_UNSUPPORTED;
262 sm->log_level = mp->log_level;
264 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
268 vl_api_nat_set_log_level_t_print (vl_api_nat_set_log_level_t *
273 s = format (0, "SCRIPT: nat_set_log_level ");
274 s = format (s, "log_level %d", mp->log_level);
280 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
283 snat_main_t *sm = &snat_main;
284 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
287 rv = snat_ipfix_logging_enable_disable (mp->enable,
293 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
297 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
302 s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
304 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
306 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
308 s = format (s, "disable ");
314 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
316 snat_main_t *sm = &snat_main;
317 vl_api_nat_set_reass_reply_t *rmp;
321 nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
322 mp->drop_frag, mp->is_ip6);
324 REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
328 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
332 s = format (0, "SCRIPT: nat_set_reass ");
333 s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
334 clib_host_to_net_u32 (mp->timeout),
335 clib_host_to_net_u16 (mp->max_reass),
336 mp->max_frag, mp->drop_frag, mp->is_ip6);
342 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
344 snat_main_t *sm = &snat_main;
345 vl_api_nat_get_reass_reply_t *rmp;
349 REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
351 rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
352 rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
353 rmp->ip4_max_frag = nat_reass_get_max_frag(0);
354 rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
355 rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
356 rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
357 rmp->ip6_max_frag = nat_reass_get_max_frag(1);
358 rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
364 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
368 s = format (0, "SCRIPT: nat_get_reass");
373 typedef struct nat_api_walk_ctx_t_
375 vl_api_registration_t *reg;
377 } nat_api_walk_ctx_t;
380 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
382 vl_api_nat_reass_details_t *rmp;
383 snat_main_t *sm = &snat_main;
384 nat_api_walk_ctx_t *ctx = arg;
385 ip46_address_t ip_address;
387 rmp = vl_msg_api_alloc (sizeof (*rmp));
388 clib_memset (rmp, 0, sizeof (*rmp));
389 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
390 rmp->context = ctx->context;
392 clib_memcpy (&ip_address.ip4, &reass->key.src, 4);
393 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->src_addr);
395 clib_memcpy (&ip_address.ip4, &reass->key.dst, 4);
396 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->dst_addr);
398 rmp->proto = reass->key.proto;
399 rmp->frag_id = ntohl (reass->key.frag_id);
400 rmp->frag_n = reass->frag_n;
402 vl_api_send_msg (ctx->reg, (u8 *) rmp);
408 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
410 vl_api_nat_reass_details_t *rmp;
411 snat_main_t *sm = &snat_main;
412 nat_api_walk_ctx_t *ctx = arg;
413 ip46_address_t ip_address;
415 rmp = vl_msg_api_alloc (sizeof (*rmp));
416 clib_memset (rmp, 0, sizeof (*rmp));
417 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
418 rmp->context = ctx->context;
420 clib_memcpy (&ip_address.ip6, &reass->key.src, 16);
421 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->src_addr);
423 clib_memcpy (&ip_address.ip6, &reass->key.dst, 16);
424 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->dst_addr);
426 rmp->proto = reass->key.proto;
427 rmp->frag_id = ntohl (reass->key.frag_id);
428 rmp->frag_n = reass->frag_n;
430 vl_api_send_msg (ctx->reg, (u8 *) rmp);
436 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
438 vl_api_registration_t *reg;
440 reg = vl_api_client_index_to_registration (mp->client_index);
444 nat_api_walk_ctx_t ctx = {
446 .context = mp->context,
449 nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
450 nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
454 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
458 s = format (0, "SCRIPT: nat_reass_dump");
464 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
466 snat_main_t *sm = &snat_main;
467 vl_api_nat_set_timeouts_reply_t *rmp;
470 sm->udp_timeout = ntohl (mp->udp);
471 sm->tcp_established_timeout = ntohl (mp->tcp_established);
472 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
473 sm->icmp_timeout = ntohl (mp->icmp);
475 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
478 rv = nat64_set_udp_timeout (ntohl (mp->udp));
482 nat64_set_tcp_timeouts (ntohl (mp->tcp_transitory),
483 ntohl (mp->tcp_established));
486 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
490 vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle)
494 s = format (0, "SCRIPT: nat_set_timeouts ");
495 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
497 ntohl (mp->tcp_established),
498 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
504 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
506 snat_main_t *sm = &snat_main;
507 vl_api_nat_get_timeouts_reply_t *rmp;
511 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
513 rmp->udp = htonl (sm->udp_timeout);
514 rmp->tcp_established = htonl (sm->tcp_established_timeout);
515 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
516 rmp->icmp = htonl (sm->icmp_timeout);
522 vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle)
526 s = format (0, "SCRIPT: nat_get_timeouts");
532 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
533 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
535 snat_main_t *sm = &snat_main;
536 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
538 u16 port_start, port_end;
540 if (sm->deterministic)
542 rv = VNET_API_ERROR_UNSUPPORTED;
548 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
549 nat_set_alloc_addr_and_port_default ();
551 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
552 nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
555 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
556 port_start = ntohs (mp->start_port);
557 port_end = ntohs (mp->end_port);
558 if (port_end <= port_start)
560 rv = VNET_API_ERROR_INVALID_VALUE;
563 nat_set_alloc_addr_and_port_range (port_start, port_end);
566 rv = VNET_API_ERROR_INVALID_VALUE;
571 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
574 static void *vl_api_nat_set_addr_and_port_alloc_alg_t_print
575 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp, void *handle)
579 s = format (0, "SCRIPT: nat_set_addr_and_port_alloc_alg ");
580 s = format (s, "alg %d psid_offset %d psid_length %d psid %d start_port %d "
582 ntohl (mp->alg), ntohl (mp->psid_offset),
583 ntohl (mp->psid_length), ntohs (mp->psid),
584 ntohs (mp->start_port), ntohs (mp->end_port));
590 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
591 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
593 snat_main_t *sm = &snat_main;
594 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
598 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
600 rmp->alg = sm->addr_and_port_alloc_alg;
601 rmp->psid_offset = sm->psid_offset;
602 rmp->psid_length = sm->psid_length;
603 rmp->psid = htons (sm->psid);
604 rmp->start_port = htons (sm->start_port);
605 rmp->end_port = htons (sm->end_port);
610 static void *vl_api_nat_get_addr_and_port_alloc_alg_t_print
611 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp, void *handle)
615 s = format (0, "SCRIPT: nat_get_addr_and_port_alloc_alg");
621 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
623 snat_main_t *sm = &snat_main;
624 vl_api_nat_set_mss_clamping_reply_t *rmp;
629 sm->mss_clamping = ntohs (mp->mss_value);
630 sm->mss_value_net = mp->mss_value;
633 sm->mss_clamping = 0;
635 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
639 vl_api_nat_set_mss_clamping_t_print (vl_api_nat_set_mss_clamping_t * mp,
644 s = format (0, "SCRIPT: nat_set_mss_clamping enable %d mss_value %d\n",
645 mp->enable, ntohs (mp->mss_value));
651 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
653 snat_main_t *sm = &snat_main;
654 vl_api_nat_get_mss_clamping_reply_t *rmp;
658 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
660 rmp->enable = sm->mss_clamping ? 1 : 0;
661 rmp->mss_value = htons (sm->mss_clamping);
667 vl_api_nat_get_mss_clamping_t_print (vl_api_nat_get_mss_clamping_t * mp,
672 s = format (0, "SCRIPT: nat_get_mss_clamping");
678 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
680 snat_main_t *sm = &snat_main;
681 vl_api_nat_ha_set_listener_reply_t *rmp;
685 memcpy (&addr, &mp->ip_address, sizeof (addr));
687 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
688 clib_net_to_host_u32 (mp->path_mtu));
690 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
694 vl_api_nat_ha_set_listener_t_print (vl_api_nat_ha_set_listener_t * mp,
699 s = format (0, "SCRIPT: nat_ha_set_listener ");
700 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
701 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
702 s = format (s, "path_mtu %d", clib_net_to_host_u32 (mp->path_mtu));
708 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
710 snat_main_t *sm = &snat_main;
711 vl_api_nat_ha_get_listener_reply_t *rmp;
717 nat_ha_get_listener (&addr, &port, &path_mtu);
720 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
722 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
723 rmp->port = clib_host_to_net_u16 (port);
724 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
730 vl_api_nat_ha_get_listener_t_print (vl_api_nat_ha_get_listener_t * mp,
735 s = format (0, "SCRIPT: nat_ha_get_listener");
741 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
743 snat_main_t *sm = &snat_main;
744 vl_api_nat_ha_set_failover_reply_t *rmp;
748 memcpy (&addr, &mp->ip_address, sizeof (addr));
750 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
751 clib_net_to_host_u32 (mp->session_refresh_interval));
753 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
757 vl_api_nat_ha_set_failover_t_print (vl_api_nat_ha_set_failover_t * mp,
762 s = format (0, "SCRIPT: nat_ha_set_failover ");
763 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
764 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
770 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
772 snat_main_t *sm = &snat_main;
773 vl_api_nat_ha_get_failover_reply_t *rmp;
777 u32 session_refresh_interval;
779 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
782 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
784 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
785 rmp->port = clib_host_to_net_u16 (port);
786 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
792 vl_api_nat_ha_get_failover_t_print (vl_api_nat_ha_get_failover_t * mp,
797 s = format (0, "SCRIPT: nat_ha_get_failover");
803 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
805 snat_main_t *sm = &snat_main;
806 vl_api_nat_ha_flush_reply_t *rmp;
811 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
815 vl_api_nat_ha_flush_t_print (vl_api_nat_ha_flush_t * mp, void *handle)
819 s = format (0, "SCRIPT: nat_ha_flush ");
825 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
827 snat_main_t *sm = &snat_main;
828 vl_api_registration_t *reg;
829 vl_api_nat_ha_resync_completed_event_t *mp;
831 reg = vl_api_client_index_to_registration (client_index);
835 mp = vl_msg_api_alloc (sizeof (*mp));
836 clib_memset (mp, 0, sizeof (*mp));
837 mp->client_index = client_index;
839 mp->missed_count = clib_host_to_net_u32 (missed_count);
841 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
843 vl_api_send_msg (reg, (u8 *) mp);
847 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
849 snat_main_t *sm = &snat_main;
850 vl_api_nat_ha_resync_reply_t *rmp;
854 nat_ha_resync (mp->client_index, mp->pid,
855 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
858 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
862 vl_api_nat_ha_resync_t_print (vl_api_nat_ha_resync_t * mp, void *handle)
866 s = format (0, "SCRIPT: nat_ha_resync ");
868 format (s, "want_resync_event %d pid %d", mp->want_resync_event,
869 clib_host_to_net_u32 (mp->pid));
878 vl_api_nat44_add_del_address_range_t_handler
879 (vl_api_nat44_add_del_address_range_t * mp)
881 snat_main_t *sm = &snat_main;
882 vl_api_nat44_add_del_address_range_reply_t *rmp;
883 ip4_address_t this_addr;
884 u8 is_add, twice_nat;
885 u32 start_host_order, end_host_order;
891 if (sm->deterministic)
893 rv = VNET_API_ERROR_UNSUPPORTED;
897 if (sm->static_mapping_only)
899 rv = VNET_API_ERROR_FEATURE_DISABLED;
904 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
906 tmp = (u32 *) mp->first_ip_address;
907 start_host_order = clib_host_to_net_u32 (tmp[0]);
908 tmp = (u32 *) mp->last_ip_address;
909 end_host_order = clib_host_to_net_u32 (tmp[0]);
911 count = (end_host_order - start_host_order) + 1;
913 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
916 nat_log_info ("%U - %U, %d addresses...",
917 format_ip4_address, mp->first_ip_address,
918 format_ip4_address, mp->last_ip_address, count);
920 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
922 for (i = 0; i < count; i++)
925 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
927 rv = snat_del_address (sm, this_addr, 0, twice_nat);
933 nat44_add_del_address_dpo (this_addr, is_add);
935 increment_v4_address (&this_addr);
939 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
942 static void *vl_api_nat44_add_del_address_range_t_print
943 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
947 s = format (0, "SCRIPT: nat44_add_address_range ");
948 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
949 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
951 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
953 s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
958 send_nat44_address_details (snat_address_t * a,
959 vl_api_registration_t * reg, u32 context,
962 vl_api_nat44_address_details_t *rmp;
963 snat_main_t *sm = &snat_main;
965 rmp = vl_msg_api_alloc (sizeof (*rmp));
966 clib_memset (rmp, 0, sizeof (*rmp));
967 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
968 clib_memcpy (rmp->ip_address, &(a->addr), 4);
969 if (a->fib_index != ~0)
971 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
972 rmp->vrf_id = ntohl (fib->ft_table_id);
977 rmp->flags |= NAT_API_IS_TWICE_NAT;
978 rmp->context = context;
980 vl_api_send_msg (reg, (u8 *) rmp);
984 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
986 vl_api_registration_t *reg;
987 snat_main_t *sm = &snat_main;
990 if (sm->deterministic)
993 reg = vl_api_client_index_to_registration (mp->client_index);
998 vec_foreach (a, sm->addresses)
999 send_nat44_address_details (a, reg, mp->context, 0);
1000 vec_foreach (a, sm->twice_nat_addresses)
1001 send_nat44_address_details (a, reg, mp->context, 1);
1006 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
1011 s = format (0, "SCRIPT: nat44_address_dump ");
1017 vl_api_nat44_interface_add_del_feature_t_handler
1018 (vl_api_nat44_interface_add_del_feature_t * mp)
1020 snat_main_t *sm = &snat_main;
1021 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
1022 u32 sw_if_index = ntohl (mp->sw_if_index);
1026 is_del = !mp->is_add;
1028 VALIDATE_SW_IF_INDEX (mp);
1031 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
1034 BAD_SW_IF_INDEX_LABEL;
1036 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
1039 static void *vl_api_nat44_interface_add_del_feature_t_print
1040 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
1044 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
1045 s = format (s, "sw_if_index %d %s %s",
1046 clib_host_to_net_u32 (mp->sw_if_index),
1047 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1048 mp->is_add ? "" : "del");
1054 send_nat44_interface_details (snat_interface_t * i,
1055 vl_api_registration_t * reg, u32 context)
1057 vl_api_nat44_interface_details_t *rmp;
1058 snat_main_t *sm = &snat_main;
1060 rmp = vl_msg_api_alloc (sizeof (*rmp));
1061 clib_memset (rmp, 0, sizeof (*rmp));
1062 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
1063 rmp->sw_if_index = ntohl (i->sw_if_index);
1065 if (nat_interface_is_inside (i))
1066 rmp->flags |= NAT_API_IS_INSIDE;
1067 if (nat_interface_is_outside (i))
1068 rmp->flags |= NAT_API_IS_OUTSIDE;
1070 rmp->context = context;
1072 vl_api_send_msg (reg, (u8 *) rmp);
1076 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
1078 vl_api_registration_t *reg;
1079 snat_main_t *sm = &snat_main;
1080 snat_interface_t *i;
1082 reg = vl_api_client_index_to_registration (mp->client_index);
1087 pool_foreach (i, sm->interfaces,
1089 send_nat44_interface_details(i, reg, mp->context);
1095 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
1100 s = format (0, "SCRIPT: nat44_interface_dump ");
1106 vl_api_nat44_interface_add_del_output_feature_t_handler
1107 (vl_api_nat44_interface_add_del_output_feature_t * mp)
1109 snat_main_t *sm = &snat_main;
1110 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
1111 u32 sw_if_index = ntohl (mp->sw_if_index);
1114 if (sm->deterministic)
1116 rv = VNET_API_ERROR_UNSUPPORTED;
1120 VALIDATE_SW_IF_INDEX (mp);
1122 rv = snat_interface_add_del_output_feature (sw_if_index,
1123 mp->flags & NAT_API_IS_INSIDE,
1126 BAD_SW_IF_INDEX_LABEL;
1128 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
1131 static void *vl_api_nat44_interface_add_del_output_feature_t_print
1132 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
1136 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
1137 s = format (s, "sw_if_index %d %s %s",
1138 clib_host_to_net_u32 (mp->sw_if_index),
1139 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1140 mp->is_add ? "" : "del");
1146 send_nat44_interface_output_feature_details (snat_interface_t * i,
1147 vl_api_registration_t * reg,
1150 vl_api_nat44_interface_output_feature_details_t *rmp;
1151 snat_main_t *sm = &snat_main;
1153 rmp = vl_msg_api_alloc (sizeof (*rmp));
1154 clib_memset (rmp, 0, sizeof (*rmp));
1156 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
1157 rmp->sw_if_index = ntohl (i->sw_if_index);
1158 rmp->context = context;
1160 if (nat_interface_is_inside (i))
1161 rmp->flags |= NAT_API_IS_INSIDE;
1163 vl_api_send_msg (reg, (u8 *) rmp);
1167 vl_api_nat44_interface_output_feature_dump_t_handler
1168 (vl_api_nat44_interface_output_feature_dump_t * mp)
1170 vl_api_registration_t *reg;
1171 snat_main_t *sm = &snat_main;
1172 snat_interface_t *i;
1174 if (sm->deterministic)
1177 reg = vl_api_client_index_to_registration (mp->client_index);
1182 pool_foreach (i, sm->output_feature_interfaces,
1184 send_nat44_interface_output_feature_details(i, reg, mp->context);
1189 static void *vl_api_nat44_interface_output_feature_dump_t_print
1190 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1194 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1200 vl_api_nat44_add_del_static_mapping_t_handler
1201 (vl_api_nat44_add_del_static_mapping_t * mp)
1203 snat_main_t *sm = &snat_main;
1204 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1205 ip4_address_t local_addr, external_addr;
1206 u16 local_port = 0, external_port = 0;
1207 u32 vrf_id, external_sw_if_index;
1208 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1210 snat_protocol_t proto;
1214 if (sm->deterministic)
1216 rv = VNET_API_ERROR_UNSUPPORTED;
1220 len = vl_api_string_len (&mp->tag);
1224 rv = VNET_API_ERROR_INVALID_VALUE;
1228 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1229 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1231 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1233 local_port = clib_net_to_host_u16 (mp->local_port);
1234 external_port = clib_net_to_host_u16 (mp->external_port);
1237 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1238 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1239 proto = ip_proto_to_snat_proto (mp->protocol);
1241 if (mp->flags & NAT_API_IS_TWICE_NAT)
1242 twice_nat = TWICE_NAT;
1243 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1244 twice_nat = TWICE_NAT_SELF;
1246 tag = vec_new (u8, len);
1248 memcpy (tag, mp->tag.buf, len);
1249 vec_terminate_c_string (tag);
1251 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1252 external_port, vrf_id,
1253 mp->flags & NAT_API_IS_ADDR_ONLY,
1254 external_sw_if_index, proto,
1255 mp->is_add, twice_nat,
1256 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0);
1260 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1263 static void *vl_api_nat44_add_del_static_mapping_t_print
1264 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1268 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1269 s = format (s, "protocol %d local_addr %U external_addr %U ",
1271 format_ip4_address, mp->local_ip_address,
1272 format_ip4_address, mp->external_ip_address);
1274 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1275 s = format (s, "local_port %d external_port %d ",
1276 clib_net_to_host_u16 (mp->local_port),
1277 clib_net_to_host_u16 (mp->external_port));
1279 s = format (s, "twice_nat %d out2in_only %d ",
1280 mp->flags & NAT_API_IS_TWICE_NAT,
1281 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1283 if (mp->vrf_id != ~0)
1284 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1286 if (mp->external_sw_if_index != ~0)
1287 s = format (s, "external_sw_if_index %d",
1288 clib_net_to_host_u32 (mp->external_sw_if_index));
1293 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1294 vl_api_registration_t * reg, u32 context)
1296 vl_api_nat44_static_mapping_details_t *rmp;
1297 snat_main_t *sm = &snat_main;
1298 u32 len = sizeof (*rmp);
1302 len += vec_len (m->tag);
1303 rmp = vl_msg_api_alloc (len);
1304 clib_memset (rmp, 0, len);
1305 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1309 rmp = vl_msg_api_alloc (len);
1310 clib_memset (rmp, 0, len);
1314 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1316 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1317 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1318 rmp->external_sw_if_index = ~0;
1319 rmp->vrf_id = htonl (m->vrf_id);
1320 rmp->context = context;
1322 if (m->twice_nat == TWICE_NAT)
1323 rmp->flags |= NAT_API_IS_TWICE_NAT;
1324 else if (m->twice_nat == TWICE_NAT_SELF)
1325 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1327 if (is_out2in_only_static_mapping (m))
1328 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1330 if (is_addr_only_static_mapping (m))
1332 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1336 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1337 rmp->external_port = htons (m->external_port);
1338 rmp->local_port = htons (m->local_port);
1341 vl_api_send_msg (reg, (u8 *) rmp);
1345 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1346 vl_api_registration_t * reg,
1349 vl_api_nat44_static_mapping_details_t *rmp;
1350 snat_main_t *sm = &snat_main;
1351 u32 len = sizeof (*rmp);
1355 len += vec_len (m->tag);
1356 rmp = vl_msg_api_alloc (len);
1357 clib_memset (rmp, 0, len);
1358 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1362 rmp = vl_msg_api_alloc (len);
1363 clib_memset (rmp, 0, len);
1367 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1368 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1369 rmp->external_sw_if_index = htonl (m->sw_if_index);
1370 rmp->vrf_id = htonl (m->vrf_id);
1371 rmp->context = context;
1374 rmp->flags |= NAT_API_IS_TWICE_NAT;
1378 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1382 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1383 rmp->external_port = htons (m->e_port);
1384 rmp->local_port = htons (m->l_port);
1387 vl_api_send_msg (reg, (u8 *) rmp);
1391 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1394 vl_api_registration_t *reg;
1395 snat_main_t *sm = &snat_main;
1396 snat_static_mapping_t *m;
1397 snat_static_map_resolve_t *rp;
1400 if (sm->deterministic)
1403 reg = vl_api_client_index_to_registration (mp->client_index);
1408 pool_foreach (m, sm->static_mappings,
1410 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1411 send_nat44_static_mapping_details (m, reg, mp->context);
1415 for (j = 0; j < vec_len (sm->to_resolve); j++)
1417 rp = sm->to_resolve + j;
1418 if (!rp->identity_nat)
1419 send_nat44_static_map_resolve_details (rp, reg, mp->context);
1424 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1429 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1435 vl_api_nat44_add_del_identity_mapping_t_handler
1436 (vl_api_nat44_add_del_identity_mapping_t * mp)
1438 snat_main_t *sm = &snat_main;
1439 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1442 u32 vrf_id, sw_if_index;
1444 snat_protocol_t proto = ~0;
1448 if (sm->deterministic)
1450 rv = VNET_API_ERROR_UNSUPPORTED;
1454 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1456 port = clib_net_to_host_u16 (mp->port);
1457 proto = ip_proto_to_snat_proto (mp->protocol);
1459 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1460 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1461 if (sw_if_index != ~0)
1464 memcpy (&addr.as_u8, mp->ip_address, 4);
1466 len = vl_api_string_len (&mp->tag);
1468 tag = vec_new (u8, len);
1469 memcpy (tag, mp->tag.buf, len);
1470 vec_terminate_c_string (tag);
1473 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1474 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1475 proto, mp->is_add, 0, 0, tag, 1);
1479 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1482 static void *vl_api_nat44_add_del_identity_mapping_t_print
1483 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1487 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1488 if (mp->sw_if_index != ~0)
1489 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1491 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1493 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1495 format (s, " protocol %d port %d", mp->protocol,
1496 clib_net_to_host_u16 (mp->port));
1498 if (mp->vrf_id != ~0)
1499 s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1505 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1506 vl_api_registration_t * reg, u32 context)
1508 vl_api_nat44_identity_mapping_details_t *rmp;
1509 snat_main_t *sm = &snat_main;
1510 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1511 u32 len = sizeof (*rmp);
1515 len += vec_len (m->tag);
1516 rmp = vl_msg_api_alloc (len);
1517 clib_memset (rmp, 0, len);
1518 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1522 rmp = vl_msg_api_alloc (len);
1523 clib_memset (rmp, 0, len);
1527 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1529 if (is_addr_only_static_mapping (m))
1530 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1532 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1533 rmp->port = htons (m->local_port);
1534 rmp->sw_if_index = ~0;
1535 rmp->vrf_id = htonl (local->vrf_id);
1536 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1537 rmp->context = context;
1539 vl_api_send_msg (reg, (u8 *) rmp);
1543 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1544 vl_api_registration_t * reg,
1547 vl_api_nat44_identity_mapping_details_t *rmp;
1548 snat_main_t *sm = &snat_main;
1549 u32 len = sizeof (*rmp);
1553 len += vec_len (m->tag);
1554 rmp = vl_msg_api_alloc (len);
1555 clib_memset (rmp, 0, len);
1556 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1560 rmp = vl_msg_api_alloc (len);
1561 clib_memset (rmp, 0, len);
1565 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1568 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1570 rmp->port = htons (m->l_port);
1571 rmp->sw_if_index = htonl (m->sw_if_index);
1572 rmp->vrf_id = htonl (m->vrf_id);
1573 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1574 rmp->context = context;
1576 vl_api_send_msg (reg, (u8 *) rmp);
1580 vl_api_nat44_identity_mapping_dump_t_handler
1581 (vl_api_nat44_identity_mapping_dump_t * mp)
1583 vl_api_registration_t *reg;
1584 snat_main_t *sm = &snat_main;
1585 snat_static_mapping_t *m;
1586 snat_static_map_resolve_t *rp;
1589 if (sm->deterministic)
1592 reg = vl_api_client_index_to_registration (mp->client_index);
1597 pool_foreach (m, sm->static_mappings,
1599 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1601 pool_foreach_index (j, m->locals,
1603 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1609 for (j = 0; j < vec_len (sm->to_resolve); j++)
1611 rp = sm->to_resolve + j;
1612 if (rp->identity_nat)
1613 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1617 static void *vl_api_nat44_identity_mapping_dump_t_print
1618 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1622 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1628 vl_api_nat44_add_del_interface_addr_t_handler
1629 (vl_api_nat44_add_del_interface_addr_t * mp)
1631 snat_main_t *sm = &snat_main;
1632 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1633 u32 sw_if_index = ntohl (mp->sw_if_index);
1637 if (sm->deterministic)
1639 rv = VNET_API_ERROR_UNSUPPORTED;
1643 is_del = !mp->is_add;
1645 VALIDATE_SW_IF_INDEX (mp);
1647 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1648 mp->flags & NAT_API_IS_TWICE_NAT);
1650 BAD_SW_IF_INDEX_LABEL;
1652 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1655 static void *vl_api_nat44_add_del_interface_addr_t_print
1656 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1660 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1661 s = format (s, "sw_if_index %d twice_nat %d %s",
1662 clib_host_to_net_u32 (mp->sw_if_index),
1663 mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1669 send_nat44_interface_addr_details (u32 sw_if_index,
1670 vl_api_registration_t * reg, u32 context,
1673 vl_api_nat44_interface_addr_details_t *rmp;
1674 snat_main_t *sm = &snat_main;
1676 rmp = vl_msg_api_alloc (sizeof (*rmp));
1677 clib_memset (rmp, 0, sizeof (*rmp));
1679 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1680 rmp->sw_if_index = ntohl (sw_if_index);
1683 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1684 rmp->context = context;
1686 vl_api_send_msg (reg, (u8 *) rmp);
1690 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1693 vl_api_registration_t *reg;
1694 snat_main_t *sm = &snat_main;
1697 if (sm->deterministic)
1700 reg = vl_api_client_index_to_registration (mp->client_index);
1705 vec_foreach (i, sm->auto_add_sw_if_indices)
1706 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1707 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1708 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1713 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1718 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1724 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1727 vl_api_nat44_user_details_t *rmp;
1728 snat_main_t *sm = &snat_main;
1729 ip4_main_t *im = &ip4_main;
1731 rmp = vl_msg_api_alloc (sizeof (*rmp));
1732 clib_memset (rmp, 0, sizeof (*rmp));
1733 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1735 if (!pool_is_free_index (im->fibs, u->fib_index))
1737 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1738 rmp->vrf_id = ntohl (fib->ft_table_id);
1741 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1742 rmp->nsessions = ntohl (u->nsessions);
1743 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1744 rmp->context = context;
1746 vl_api_send_msg (reg, (u8 *) rmp);
1750 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1752 vl_api_registration_t *reg;
1753 snat_main_t *sm = &snat_main;
1754 snat_main_per_thread_data_t *tsm;
1757 if (sm->deterministic)
1760 reg = vl_api_client_index_to_registration (mp->client_index);
1765 vec_foreach (tsm, sm->per_thread_data)
1767 pool_foreach (u, tsm->users,
1769 send_nat44_user_details (u, reg, mp->context);
1776 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1780 s = format (0, "SCRIPT: nat44_user_dump ");
1786 send_nat44_user_session_details (snat_session_t * s,
1787 vl_api_registration_t * reg, u32 context)
1789 vl_api_nat44_user_session_details_t *rmp;
1790 snat_main_t *sm = &snat_main;
1792 rmp = vl_msg_api_alloc (sizeof (*rmp));
1793 clib_memset (rmp, 0, sizeof (*rmp));
1795 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1796 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1797 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1799 if (snat_is_session_static (s))
1800 rmp->flags |= NAT_API_IS_STATIC;
1802 if (is_twice_nat_session (s))
1803 rmp->flags |= NAT_API_IS_TWICE_NAT;
1805 if (is_ed_session (s) || is_fwd_bypass_session (s))
1806 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1808 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1809 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1810 rmp->total_pkts = ntohl (s->total_pkts);
1811 rmp->context = context;
1812 if (snat_is_unk_proto_session (s))
1814 rmp->outside_port = 0;
1815 rmp->inside_port = 0;
1816 rmp->protocol = ntohs (s->in2out.port);
1820 rmp->outside_port = s->out2in.port;
1821 rmp->inside_port = s->in2out.port;
1822 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1824 if (is_ed_session (s) || is_fwd_bypass_session (s))
1826 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1827 rmp->ext_host_port = s->ext_host_port;
1828 if (is_twice_nat_session (s))
1830 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1831 rmp->ext_host_nat_port = s->ext_host_nat_port;
1835 vl_api_send_msg (reg, (u8 *) rmp);
1839 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1842 vl_api_registration_t *reg;
1843 snat_main_t *sm = &snat_main;
1844 snat_main_per_thread_data_t *tsm;
1846 clib_bihash_kv_8_8_t key, value;
1847 snat_user_key_t ukey;
1849 u32 session_index, head_index, elt_index;
1850 dlist_elt_t *head, *elt;
1853 if (sm->deterministic)
1856 reg = vl_api_client_index_to_registration (mp->client_index);
1860 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1861 ip.src_address.as_u32 = ukey.addr.as_u32;
1862 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1863 key.key = ukey.as_u64;
1864 if (sm->num_workers > 1)
1866 vec_elt_at_index (sm->per_thread_data,
1867 sm->worker_in2out_cb (&ip, ukey.fib_index));
1869 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1870 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1872 u = pool_elt_at_index (tsm->users, value.value);
1873 if (!u->nsessions && !u->nstaticsessions)
1876 head_index = u->sessions_per_user_list_head_index;
1877 head = pool_elt_at_index (tsm->list_pool, head_index);
1878 elt_index = head->next;
1879 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1880 session_index = elt->value;
1881 while (session_index != ~0)
1883 s = pool_elt_at_index (tsm->sessions, session_index);
1885 send_nat44_user_session_details (s, reg, mp->context);
1887 elt_index = elt->next;
1888 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1889 session_index = elt->value;
1894 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1899 s = format (0, "SCRIPT: nat44_user_session_dump ");
1900 s = format (s, "ip_address %U vrf_id %d\n",
1901 format_ip4_address, mp->ip_address,
1902 clib_net_to_host_u32 (mp->vrf_id));
1907 static nat44_lb_addr_port_t *
1908 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1909 u32 addr_port_pair_num)
1912 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1913 vl_api_nat44_lb_addr_port_t *ap;
1915 for (i = 0; i < addr_port_pair_num; i++)
1917 ap = &addr_port_pairs[i];
1918 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1919 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1920 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1921 lb_addr_port.probability = ap->probability;
1922 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1923 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1926 return lb_addr_port_pairs;
1930 vl_api_nat44_add_del_lb_static_mapping_t_handler
1931 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1933 snat_main_t *sm = &snat_main;
1934 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1935 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1937 nat44_lb_addr_port_t *locals = 0;
1938 ip4_address_t e_addr;
1939 snat_protocol_t proto;
1940 vl_api_string_t *sp;
1944 if (!sm->endpoint_dependent)
1946 rv = VNET_API_ERROR_UNSUPPORTED;
1951 unformat_nat44_lb_addr_port (mp->locals,
1952 clib_net_to_host_u32 (mp->local_num));
1953 clib_memcpy (&e_addr, mp->external_addr, 4);
1954 proto = ip_proto_to_snat_proto (mp->protocol);
1956 if (mp->flags & NAT_API_IS_TWICE_NAT)
1957 twice_nat = TWICE_NAT;
1958 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1959 twice_nat = TWICE_NAT_SELF;
1961 sp = (void *) &mp->locals +
1962 sizeof (vl_api_nat44_lb_addr_port_t) *
1963 clib_net_to_host_u32 (mp->local_num);
1965 len = vl_api_string_len (sp);
1967 tag = vec_new (u8, len);
1968 memcpy (tag, sp->buf, len);
1969 vec_terminate_c_string (tag);
1972 nat44_add_del_lb_static_mapping (e_addr,
1973 clib_net_to_host_u16 (mp->external_port),
1974 proto, locals, mp->is_add,
1976 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1977 clib_net_to_host_u32 (mp->affinity));
1983 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1986 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1987 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1991 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1992 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1994 mp->flags & NAT_API_IS_TWICE_NAT,
1995 mp->flags & NAT_API_IS_OUT2IN_ONLY);
2001 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
2002 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
2004 snat_main_t *sm = &snat_main;
2005 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
2007 ip4_address_t e_addr, l_addr;
2008 snat_protocol_t proto;
2010 if (!sm->endpoint_dependent)
2012 rv = VNET_API_ERROR_UNSUPPORTED;
2016 clib_memcpy (&e_addr, mp->external_addr, 4);
2017 clib_memcpy (&l_addr, mp->local.addr, 4);
2018 proto = ip_proto_to_snat_proto (mp->protocol);
2021 nat44_lb_static_mapping_add_del_local (e_addr,
2022 clib_net_to_host_u16
2023 (mp->external_port), l_addr,
2024 clib_net_to_host_u16 (mp->
2027 clib_net_to_host_u32 (mp->
2029 mp->local.probability, mp->is_add);
2032 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
2035 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
2036 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
2040 s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
2041 s = format (s, "is_add %d", mp->is_add);
2047 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
2048 vl_api_registration_t * reg,
2051 vl_api_nat44_lb_static_mapping_details_t *rmp;
2052 snat_main_t *sm = &snat_main;
2053 nat44_lb_addr_port_t *ap;
2054 vl_api_nat44_lb_addr_port_t *locals;
2055 vl_api_string_t *sp;
2057 u32 len = sizeof (*rmp);
2061 len += pool_elts (m->locals) *
2062 sizeof (nat44_lb_addr_port_t) + vec_len (m->tag);
2063 rmp = vl_msg_api_alloc (len);
2064 clib_memset (rmp, 0, len);
2066 sp = (void *) &rmp->locals +
2067 sizeof (vl_api_nat44_lb_addr_port_t) * pool_elts (m->locals);
2068 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, sp);
2072 len += pool_elts (m->locals) * sizeof (nat44_lb_addr_port_t);
2073 rmp = vl_msg_api_alloc (len);
2074 clib_memset (rmp, 0, len);
2078 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2080 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
2081 rmp->external_port = ntohs (m->external_port);
2082 rmp->protocol = snat_proto_to_ip_proto (m->proto);
2083 rmp->context = context;
2085 if (m->twice_nat == TWICE_NAT)
2086 rmp->flags |= NAT_API_IS_TWICE_NAT;
2087 else if (m->twice_nat == TWICE_NAT_SELF)
2088 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
2089 if (is_out2in_only_static_mapping (m))
2090 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
2092 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
2094 pool_foreach (ap, m->locals,
2096 clib_memcpy (locals->addr, &(ap->addr), 4);
2097 locals->port = htons (ap->port);
2098 locals->probability = ap->probability;
2099 locals->vrf_id = ntohl (ap->vrf_id);
2104 rmp->local_num = ntohl (local_num);
2106 vl_api_send_msg (reg, (u8 *) rmp);
2110 vl_api_nat44_lb_static_mapping_dump_t_handler
2111 (vl_api_nat44_lb_static_mapping_dump_t * mp)
2113 vl_api_registration_t *reg;
2114 snat_main_t *sm = &snat_main;
2115 snat_static_mapping_t *m;
2117 if (!sm->endpoint_dependent)
2120 reg = vl_api_client_index_to_registration (mp->client_index);
2125 pool_foreach (m, sm->static_mappings,
2127 if (is_lb_static_mapping(m))
2128 send_nat44_lb_static_mapping_details (m, reg, mp->context);
2133 static void *vl_api_nat44_lb_static_mapping_dump_t_print
2134 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
2138 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
2144 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
2146 snat_main_t *sm = &snat_main;
2147 vl_api_nat44_del_session_reply_t *rmp;
2148 ip4_address_t addr, eh_addr;
2153 snat_protocol_t proto;
2155 if (sm->deterministic)
2157 rv = VNET_API_ERROR_UNSUPPORTED;
2161 memcpy (&addr.as_u8, mp->address, 4);
2162 port = clib_net_to_host_u16 (mp->port);
2163 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
2164 proto = ip_proto_to_snat_proto (mp->protocol);
2165 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
2166 eh_port = clib_net_to_host_u16 (mp->ext_host_port);
2168 is_in = mp->flags & NAT_API_IS_INSIDE;
2170 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2172 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
2175 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
2178 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
2182 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
2187 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
2188 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
2189 format_ip4_address, mp->address,
2190 clib_net_to_host_u16 (mp->port),
2191 mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
2192 mp->flags & NAT_API_IS_INSIDE);
2193 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2194 s = format (s, "ext_host_address %U ext_host_port %d",
2195 format_ip4_address, mp->ext_host_address,
2196 clib_net_to_host_u16 (mp->ext_host_port));
2202 vl_api_nat44_forwarding_enable_disable_t_handler
2203 (vl_api_nat44_forwarding_enable_disable_t * mp)
2205 snat_main_t *sm = &snat_main;
2206 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
2208 u32 *ses_to_be_removed = 0, *ses_index;
2209 snat_main_per_thread_data_t *tsm;
2212 sm->forwarding_enabled = mp->enable != 0;
2214 if (mp->enable == 0)
2217 vec_foreach (tsm, sm->per_thread_data)
2219 pool_foreach (s, tsm->sessions,
2221 if (is_fwd_bypass_session(s))
2223 vec_add1 (ses_to_be_removed, s - tsm->sessions);
2226 vec_foreach (ses_index, ses_to_be_removed)
2228 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2229 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2230 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2232 vec_free (ses_to_be_removed);
2237 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2240 static void *vl_api_nat44_forwarding_enable_disable_t_print
2241 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2245 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2246 s = format (s, "enable %d", mp->enable != 0);
2252 vl_api_nat44_forwarding_is_enabled_t_handler
2253 (vl_api_nat44_forwarding_is_enabled_t * mp)
2255 vl_api_registration_t *reg;
2256 snat_main_t *sm = &snat_main;
2257 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2259 reg = vl_api_client_index_to_registration (mp->client_index);
2263 rmp = vl_msg_api_alloc (sizeof (*rmp));
2264 clib_memset (rmp, 0, sizeof (*rmp));
2266 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2267 rmp->context = mp->context;
2269 rmp->enabled = sm->forwarding_enabled;
2271 vl_api_send_msg (reg, (u8 *) rmp);
2274 static void *vl_api_nat44_forwarding_is_enabled_t_print
2275 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2279 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2284 /*******************************/
2285 /*** Deterministic NAT (CGN) ***/
2286 /*******************************/
2289 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
2291 snat_main_t *sm = &snat_main;
2292 vl_api_nat_det_add_del_map_reply_t *rmp;
2294 ip4_address_t in_addr, out_addr;
2296 if (!sm->deterministic)
2298 rv = VNET_API_ERROR_UNSUPPORTED;
2302 clib_memcpy (&in_addr, mp->in_addr, 4);
2303 clib_memcpy (&out_addr, mp->out_addr, 4);
2304 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
2305 mp->out_plen, mp->is_add);
2308 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
2312 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
2317 s = format (0, "SCRIPT: nat_det_add_del_map ");
2318 s = format (s, "inside address %U/%d outside address %U/%d\n",
2319 format_ip4_address, mp->in_addr, mp->in_plen,
2320 format_ip4_address, mp->out_addr, mp->out_plen);
2326 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
2328 snat_main_t *sm = &snat_main;
2329 vl_api_nat_det_forward_reply_t *rmp;
2331 u16 lo_port = 0, hi_port = 0;
2333 ip4_address_t in_addr, out_addr;
2335 if (!sm->deterministic)
2337 rv = VNET_API_ERROR_UNSUPPORTED;
2338 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
2342 out_addr.as_u32 = 0;
2343 clib_memcpy (&in_addr, mp->in_addr, 4);
2344 dm = snat_det_map_by_user (sm, &in_addr);
2347 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2351 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2352 hi_port = lo_port + dm->ports_per_host - 1;
2356 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
2358 rmp->out_port_lo = ntohs (lo_port);
2359 rmp->out_port_hi = ntohs (hi_port);
2360 clib_memcpy (rmp->out_addr, &out_addr, 4);
2366 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
2370 s = format (0, "SCRIPT: nat_det_forward");
2371 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
2377 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
2379 snat_main_t *sm = &snat_main;
2380 vl_api_nat_det_reverse_reply_t *rmp;
2382 ip4_address_t out_addr, in_addr;
2385 if (!sm->deterministic)
2387 rv = VNET_API_ERROR_UNSUPPORTED;
2388 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
2393 clib_memcpy (&out_addr, mp->out_addr, 4);
2394 dm = snat_det_map_by_out (sm, &out_addr);
2397 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2401 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
2405 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
2407 clib_memcpy (rmp->in_addr, &in_addr, 4);
2413 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
2417 s = format (0, "SCRIPT: nat_det_reverse");
2418 s = format (s, "outside ip address %U outside port %d",
2419 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
2425 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
2428 vl_api_nat_det_map_details_t *rmp;
2429 snat_main_t *sm = &snat_main;
2431 rmp = vl_msg_api_alloc (sizeof (*rmp));
2432 clib_memset (rmp, 0, sizeof (*rmp));
2433 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
2434 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
2435 rmp->in_plen = m->in_plen;
2436 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
2437 rmp->out_plen = m->out_plen;
2438 rmp->sharing_ratio = htonl (m->sharing_ratio);
2439 rmp->ports_per_host = htons (m->ports_per_host);
2440 rmp->ses_num = htonl (m->ses_num);
2441 rmp->context = context;
2443 vl_api_send_msg (reg, (u8 *) rmp);
2447 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
2449 vl_api_registration_t *reg;
2450 snat_main_t *sm = &snat_main;
2453 if (!sm->deterministic)
2456 reg = vl_api_client_index_to_registration (mp->client_index);
2461 vec_foreach(m, sm->det_maps)
2462 sent_nat_det_map_details(m, reg, mp->context);
2467 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
2471 s = format (0, "SCRIPT: nat_det_map_dump ");
2477 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
2480 snat_main_t *sm = &snat_main;
2481 vl_api_nat_det_close_session_out_reply_t *rmp;
2482 ip4_address_t out_addr, ext_addr, in_addr;
2483 snat_det_out_key_t key;
2485 snat_det_session_t *ses;
2488 if (!sm->deterministic)
2490 rv = VNET_API_ERROR_UNSUPPORTED;
2494 clib_memcpy (&out_addr, mp->out_addr, 4);
2495 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2497 dm = snat_det_map_by_out (sm, &out_addr);
2500 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2503 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
2504 key.ext_host_addr = ext_addr;
2505 key.ext_host_port = mp->ext_port;
2506 key.out_port = mp->out_port;
2507 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
2510 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2513 snat_det_ses_close (dm, ses);
2516 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2520 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
2525 s = format (0, "SCRIPT: nat_det_close_session_out ");
2526 s = format (s, "out_addr %U out_port %d "
2527 "ext_addr %U ext_port %d\n",
2528 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
2529 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2535 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
2538 snat_main_t *sm = &snat_main;
2539 vl_api_nat_det_close_session_in_reply_t *rmp;
2540 ip4_address_t in_addr, ext_addr;
2541 snat_det_out_key_t key;
2543 snat_det_session_t *ses;
2546 if (!sm->deterministic)
2548 rv = VNET_API_ERROR_UNSUPPORTED;
2552 clib_memcpy (&in_addr, mp->in_addr, 4);
2553 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2555 dm = snat_det_map_by_user (sm, &in_addr);
2558 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2561 key.ext_host_addr = ext_addr;
2562 key.ext_host_port = mp->ext_port;
2563 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
2566 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2569 snat_det_ses_close (dm, ses);
2572 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2576 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
2580 s = format (0, "SCRIPT: nat_det_close_session_in ");
2581 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
2582 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
2583 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2589 send_nat_det_session_details (snat_det_session_t * s,
2590 vl_api_registration_t * reg, u32 context)
2592 vl_api_nat_det_session_details_t *rmp;
2593 snat_main_t *sm = &snat_main;
2595 rmp = vl_msg_api_alloc (sizeof (*rmp));
2596 clib_memset (rmp, 0, sizeof (*rmp));
2597 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
2598 rmp->in_port = s->in_port;
2599 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
2600 rmp->ext_port = s->out.ext_host_port;
2601 rmp->out_port = s->out.out_port;
2602 rmp->state = s->state;
2603 rmp->expire = ntohl (s->expire);
2604 rmp->context = context;
2606 vl_api_send_msg (reg, (u8 *) rmp);
2610 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
2612 vl_api_registration_t *reg;
2613 snat_main_t *sm = &snat_main;
2614 ip4_address_t user_addr;
2616 snat_det_session_t *s, empty_ses;
2619 if (!sm->deterministic)
2622 reg = vl_api_client_index_to_registration (mp->client_index);
2626 clib_memset (&empty_ses, 0, sizeof (empty_ses));
2627 clib_memcpy (&user_addr, mp->user_addr, 4);
2628 dm = snat_det_map_by_user (sm, &user_addr);
2632 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2633 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2636 send_nat_det_session_details (s, reg, mp->context);
2642 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2647 s = format (0, "SCRIPT: nat_det_session_dump ");
2648 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2658 vl_api_nat64_add_del_pool_addr_range_t_handler
2659 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2661 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2662 snat_main_t *sm = &snat_main;
2664 ip4_address_t this_addr;
2665 u32 start_host_order, end_host_order;
2670 tmp = (u32 *) mp->start_addr;
2671 start_host_order = clib_host_to_net_u32 (tmp[0]);
2672 tmp = (u32 *) mp->end_addr;
2673 end_host_order = clib_host_to_net_u32 (tmp[0]);
2675 count = (end_host_order - start_host_order) + 1;
2677 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2679 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2681 for (i = 0; i < count; i++)
2683 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2686 increment_v4_address (&this_addr);
2690 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2693 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2694 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2698 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2699 s = format (s, "%U - %U vrf_id %u %s\n",
2700 format_ip4_address, mp->start_addr,
2701 format_ip4_address, mp->end_addr,
2702 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2707 typedef struct nat64_api_walk_ctx_t_
2709 vl_api_registration_t *reg;
2712 } nat64_api_walk_ctx_t;
2715 nat64_api_pool_walk (snat_address_t * a, void *arg)
2717 vl_api_nat64_pool_addr_details_t *rmp;
2718 snat_main_t *sm = &snat_main;
2719 nat64_api_walk_ctx_t *ctx = arg;
2721 rmp = vl_msg_api_alloc (sizeof (*rmp));
2722 clib_memset (rmp, 0, sizeof (*rmp));
2723 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2724 clib_memcpy (rmp->address, &(a->addr), 4);
2725 if (a->fib_index != ~0)
2727 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2730 rmp->vrf_id = ntohl (fib->ft_table_id);
2734 rmp->context = ctx->context;
2736 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2742 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2744 vl_api_registration_t *reg;
2746 reg = vl_api_client_index_to_registration (mp->client_index);
2750 nat64_api_walk_ctx_t ctx = {
2752 .context = mp->context,
2755 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2759 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2764 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2770 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2773 snat_main_t *sm = &snat_main;
2774 vl_api_nat64_add_del_interface_reply_t *rmp;
2777 VALIDATE_SW_IF_INDEX (mp);
2780 nat64_add_del_interface (ntohl (mp->sw_if_index),
2781 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2783 BAD_SW_IF_INDEX_LABEL;
2785 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2789 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2794 s = format (0, "SCRIPT: nat64_add_del_interface ");
2795 s = format (s, "sw_if_index %d %s %s",
2796 clib_host_to_net_u32 (mp->sw_if_index),
2797 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2798 mp->is_add ? "" : "del");
2804 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2806 vl_api_nat64_interface_details_t *rmp;
2807 snat_main_t *sm = &snat_main;
2808 nat64_api_walk_ctx_t *ctx = arg;
2810 rmp = vl_msg_api_alloc (sizeof (*rmp));
2811 clib_memset (rmp, 0, sizeof (*rmp));
2812 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2813 rmp->sw_if_index = ntohl (i->sw_if_index);
2815 if (nat_interface_is_inside (i))
2816 rmp->flags |= NAT_API_IS_INSIDE;
2817 if (nat_interface_is_outside (i))
2818 rmp->flags |= NAT_API_IS_OUTSIDE;
2820 rmp->context = ctx->context;
2822 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2828 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2830 vl_api_registration_t *reg;
2832 reg = vl_api_client_index_to_registration (mp->client_index);
2836 nat64_api_walk_ctx_t ctx = {
2838 .context = mp->context,
2841 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2845 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2850 s = format (0, "SCRIPT: snat_interface_dump ");
2856 vl_api_nat64_add_del_static_bib_t_handler
2857 (vl_api_nat64_add_del_static_bib_t * mp)
2859 snat_main_t *sm = &snat_main;
2860 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2861 ip6_address_t in_addr;
2862 ip4_address_t out_addr;
2865 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2866 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2869 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2870 clib_net_to_host_u16 (mp->i_port),
2871 clib_net_to_host_u16 (mp->o_port),
2873 clib_net_to_host_u32 (mp->vrf_id),
2876 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2879 static void *vl_api_nat64_add_del_static_bib_t_print
2880 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2884 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2885 s = format (s, "protocol %d i_addr %U o_addr %U ",
2887 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2889 if (mp->vrf_id != ~0)
2890 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2896 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2898 vl_api_nat64_bib_details_t *rmp;
2899 snat_main_t *sm = &snat_main;
2900 nat64_api_walk_ctx_t *ctx = arg;
2903 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2907 rmp = vl_msg_api_alloc (sizeof (*rmp));
2908 clib_memset (rmp, 0, sizeof (*rmp));
2909 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2910 rmp->context = ctx->context;
2911 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2912 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2913 rmp->i_port = bibe->in_port;
2914 rmp->o_port = bibe->out_port;
2915 rmp->vrf_id = ntohl (fib->ft_table_id);
2916 rmp->proto = bibe->proto;
2917 if (bibe->is_static)
2918 rmp->flags |= NAT_API_IS_STATIC;
2919 rmp->ses_num = ntohl (bibe->ses_num);
2921 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2927 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2929 vl_api_registration_t *reg;
2930 nat64_main_t *nm = &nat64_main;
2933 reg = vl_api_client_index_to_registration (mp->client_index);
2937 nat64_api_walk_ctx_t ctx = {
2939 .context = mp->context,
2943 vec_foreach (db, nm->db)
2944 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2949 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2953 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2959 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2961 vl_api_nat64_st_details_t *rmp;
2962 snat_main_t *sm = &snat_main;
2963 nat64_api_walk_ctx_t *ctx = arg;
2964 nat64_db_bib_entry_t *bibe;
2967 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2971 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2975 rmp = vl_msg_api_alloc (sizeof (*rmp));
2976 clib_memset (rmp, 0, sizeof (*rmp));
2977 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2978 rmp->context = ctx->context;
2979 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2980 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2981 rmp->il_port = bibe->in_port;
2982 rmp->ol_port = bibe->out_port;
2983 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2984 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2985 rmp->il_port = ste->r_port;
2986 rmp->vrf_id = ntohl (fib->ft_table_id);
2987 rmp->proto = ste->proto;
2989 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2995 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2997 vl_api_registration_t *reg;
2998 nat64_main_t *nm = &nat64_main;
3001 reg = vl_api_client_index_to_registration (mp->client_index);
3005 nat64_api_walk_ctx_t ctx = {
3007 .context = mp->context,
3011 vec_foreach (db, nm->db)
3014 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
3020 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
3024 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
3030 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
3032 vl_api_nat64_add_del_prefix_reply_t *rmp;
3033 snat_main_t *sm = &snat_main;
3034 ip6_address_t prefix;
3037 memcpy (&prefix.as_u8, mp->prefix.address, 16);
3040 nat64_add_del_prefix (&prefix, mp->prefix.len,
3041 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
3042 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
3046 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
3051 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
3052 format_ip6_address, mp->prefix.address, mp->prefix.len,
3053 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
3059 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
3061 vl_api_nat64_prefix_details_t *rmp;
3062 snat_main_t *sm = &snat_main;
3063 nat64_api_walk_ctx_t *ctx = arg;
3065 rmp = vl_msg_api_alloc (sizeof (*rmp));
3066 clib_memset (rmp, 0, sizeof (*rmp));
3067 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
3068 clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
3069 rmp->prefix.len = p->plen;
3070 rmp->vrf_id = ntohl (p->vrf_id);
3071 rmp->context = ctx->context;
3073 vl_api_send_msg (ctx->reg, (u8 *) rmp);
3079 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
3081 vl_api_registration_t *reg;
3083 reg = vl_api_client_index_to_registration (mp->client_index);
3087 nat64_api_walk_ctx_t ctx = {
3089 .context = mp->context,
3092 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
3096 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
3101 s = format (0, "SCRIPT: nat64_prefix_dump\n");
3107 vl_api_nat64_add_del_interface_addr_t_handler
3108 (vl_api_nat64_add_del_interface_addr_t * mp)
3110 snat_main_t *sm = &snat_main;
3111 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
3112 u32 sw_if_index = ntohl (mp->sw_if_index);
3115 VALIDATE_SW_IF_INDEX (mp);
3117 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
3119 BAD_SW_IF_INDEX_LABEL;
3121 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
3124 static void *vl_api_nat64_add_del_interface_addr_t_print
3125 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
3129 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
3130 s = format (s, "sw_if_index %d %s",
3131 clib_host_to_net_u32 (mp->sw_if_index),
3132 mp->is_add ? "" : "del");
3142 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
3144 vl_api_dslite_set_aftr_addr_reply_t *rmp;
3145 snat_main_t *sm = &snat_main;
3146 dslite_main_t *dm = &dslite_main;
3148 ip6_address_t ip6_addr;
3149 ip4_address_t ip4_addr;
3151 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3152 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3154 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
3156 rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
3158 REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
3162 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
3167 s = format (0, "SCRIPT: dslite_set_aftr_addr ");
3168 s = format (s, "ip6_addr %U ip4_addr %U\n",
3169 format_ip6_address, mp->ip6_addr,
3170 format_ip4_address, mp->ip4_addr);
3176 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
3178 snat_main_t *sm = &snat_main;
3179 vl_api_dslite_get_aftr_addr_reply_t *rmp;
3180 dslite_main_t *dm = &dslite_main;
3184 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
3186 memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
3187 memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
3193 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
3198 s = format (0, "SCRIPT: dslite_get_aftr_addr");
3204 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
3206 vl_api_dslite_set_b4_addr_reply_t *rmp;
3207 snat_main_t *sm = &snat_main;
3208 dslite_main_t *dm = &dslite_main;
3210 ip6_address_t ip6_addr;
3211 ip4_address_t ip4_addr;
3213 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3214 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3216 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
3218 rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
3220 REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
3224 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
3229 s = format (0, "SCRIPT: dslite_set_b4_addr ");
3230 s = format (s, "ip6_addr %U ip4_addr %U\n",
3231 format_ip6_address, mp->ip6_addr,
3232 format_ip6_address, mp->ip4_addr);
3238 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
3240 snat_main_t *sm = &snat_main;
3241 vl_api_dslite_get_b4_addr_reply_t *rmp;
3242 dslite_main_t *dm = &dslite_main;
3246 REPLY_MACRO2 (VL_API_DSLITE_GET_B4_ADDR_REPLY,
3248 memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
3249 memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
3255 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
3260 s = format (0, "SCRIPT: dslite_get_b4_addr");
3266 vl_api_dslite_add_del_pool_addr_range_t_handler
3267 (vl_api_dslite_add_del_pool_addr_range_t * mp)
3269 vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
3270 snat_main_t *sm = &snat_main;
3271 dslite_main_t *dm = &dslite_main;
3273 ip4_address_t this_addr;
3274 u32 start_host_order, end_host_order;
3278 tmp = (u32 *) mp->start_addr;
3279 start_host_order = clib_host_to_net_u32 (tmp[0]);
3280 tmp = (u32 *) mp->end_addr;
3281 end_host_order = clib_host_to_net_u32 (tmp[0]);
3283 count = (end_host_order - start_host_order) + 1;
3284 memcpy (&this_addr.as_u8, mp->start_addr, 4);
3286 for (i = 0; i < count; i++)
3288 if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
3291 increment_v4_address (&this_addr);
3295 REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
3299 send_dslite_address_details (snat_address_t * ap,
3300 vl_api_registration_t * reg, u32 context)
3302 vl_api_dslite_address_details_t *rmp;
3303 snat_main_t *sm = &snat_main;
3305 rmp = vl_msg_api_alloc (sizeof (*rmp));
3307 clib_memset (rmp, 0, sizeof (*rmp));
3309 rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
3310 clib_memcpy (rmp->ip_address, &(ap->addr), 4);
3311 rmp->context = context;
3313 vl_api_send_msg (reg, (u8 *) rmp);
3317 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
3319 vl_api_registration_t *reg;
3320 dslite_main_t *dm = &dslite_main;
3323 reg = vl_api_client_index_to_registration (mp->client_index);
3328 vec_foreach (ap, dm->addr_pool)
3330 send_dslite_address_details (ap, reg, mp->context);
3336 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
3341 s = format (0, "SCRIPT: dslite_address_dump ");
3346 static void *vl_api_dslite_add_del_pool_addr_range_t_print
3347 (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
3351 s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
3352 s = format (s, "%U - %U\n",
3353 format_ip4_address, mp->start_addr,
3354 format_ip4_address, mp->end_addr);
3365 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
3368 snat_main_t *sm = &snat_main;
3369 vl_api_nat66_add_del_interface_reply_t *rmp;
3372 VALIDATE_SW_IF_INDEX (mp);
3375 nat66_interface_add_del (ntohl (mp->sw_if_index),
3376 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
3378 BAD_SW_IF_INDEX_LABEL;
3380 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
3384 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
3389 s = format (0, "SCRIPT: nat66_add_del_interface ");
3390 s = format (s, "sw_if_index %d %s %s",
3391 clib_host_to_net_u32 (mp->sw_if_index),
3392 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
3393 mp->is_add ? "" : "del");
3399 vl_api_nat66_add_del_static_mapping_t_handler
3400 (vl_api_nat66_add_del_static_mapping_t * mp)
3402 snat_main_t *sm = &snat_main;
3403 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
3404 ip6_address_t l_addr, e_addr;
3407 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
3408 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
3411 nat66_static_mapping_add_del (&l_addr, &e_addr,
3412 clib_net_to_host_u32 (mp->vrf_id),
3415 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
3418 static void *vl_api_nat66_add_del_static_mapping_t_print
3419 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
3423 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
3424 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
3425 format_ip6_address, mp->local_ip_address,
3426 format_ip6_address, mp->external_ip_address,
3427 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
3432 typedef struct nat66_api_walk_ctx_t_
3434 vl_api_registration_t *rp;
3436 } nat66_api_walk_ctx_t;
3439 nat66_api_interface_walk (snat_interface_t * i, void *arg)
3441 vl_api_nat66_interface_details_t *rmp;
3442 snat_main_t *sm = &snat_main;
3443 nat66_api_walk_ctx_t *ctx = arg;
3445 rmp = vl_msg_api_alloc (sizeof (*rmp));
3446 clib_memset (rmp, 0, sizeof (*rmp));
3447 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
3448 rmp->sw_if_index = ntohl (i->sw_if_index);
3449 if (nat_interface_is_inside (i))
3450 rmp->flags |= NAT_API_IS_INSIDE;
3451 rmp->context = ctx->context;
3453 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3459 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
3461 vl_api_registration_t *rp;
3463 rp = vl_api_client_index_to_registration (mp->client_index);
3467 nat66_api_walk_ctx_t ctx = {
3469 .context = mp->context,
3472 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
3476 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
3481 s = format (0, "SCRIPT: nat66_interface_dump ");
3487 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
3489 vl_api_nat66_static_mapping_details_t *rmp;
3490 nat66_main_t *nm = &nat66_main;
3491 snat_main_t *sm = &snat_main;
3492 nat66_api_walk_ctx_t *ctx = arg;
3496 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
3500 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
3502 rmp = vl_msg_api_alloc (sizeof (*rmp));
3503 clib_memset (rmp, 0, sizeof (*rmp));
3505 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
3506 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
3507 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
3508 rmp->vrf_id = ntohl (fib->ft_table_id);
3509 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
3510 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
3511 rmp->context = ctx->context;
3513 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3519 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
3522 vl_api_registration_t *rp;
3524 rp = vl_api_client_index_to_registration (mp->client_index);
3528 nat66_api_walk_ctx_t ctx = {
3530 .context = mp->context,
3533 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
3537 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
3542 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
3548 /* List of message types that this plugin understands */
3549 #define foreach_snat_plugin_api_msg \
3550 _(NAT_CONTROL_PING, nat_control_ping) \
3551 _(NAT_SHOW_CONFIG, nat_show_config) \
3552 _(NAT_SET_WORKERS, nat_set_workers) \
3553 _(NAT_WORKER_DUMP, nat_worker_dump) \
3554 _(NAT_SET_LOG_LEVEL, nat_set_log_level) \
3555 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
3556 _(NAT_SET_REASS, nat_set_reass) \
3557 _(NAT_GET_REASS, nat_get_reass) \
3558 _(NAT_REASS_DUMP, nat_reass_dump) \
3559 _(NAT_SET_TIMEOUTS, nat_set_timeouts) \
3560 _(NAT_GET_TIMEOUTS, nat_get_timeouts) \
3561 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg) \
3562 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg) \
3563 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping) \
3564 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping) \
3565 _(NAT_HA_SET_LISTENER, nat_ha_set_listener) \
3566 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover) \
3567 _(NAT_HA_GET_LISTENER, nat_ha_get_listener) \
3568 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover) \
3569 _(NAT_HA_FLUSH, nat_ha_flush) \
3570 _(NAT_HA_RESYNC, nat_ha_resync) \
3571 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
3572 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
3573 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
3574 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3575 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3576 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3577 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3578 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3579 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3580 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3581 _(NAT44_USER_DUMP, nat44_user_dump) \
3582 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3583 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3584 nat44_interface_add_del_output_feature) \
3585 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3586 nat44_interface_output_feature_dump) \
3587 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3588 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL, \
3589 nat44_lb_static_mapping_add_del_local) \
3590 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3591 _(NAT44_DEL_SESSION, nat44_del_session) \
3592 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3593 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3594 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3595 _(NAT_DET_FORWARD, nat_det_forward) \
3596 _(NAT_DET_REVERSE, nat_det_reverse) \
3597 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3598 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3599 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3600 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3601 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3602 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3603 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3604 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3605 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3606 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3607 _(NAT64_ST_DUMP, nat64_st_dump) \
3608 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3609 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3610 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3611 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range) \
3612 _(DSLITE_ADDRESS_DUMP, dslite_address_dump) \
3613 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr) \
3614 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr) \
3615 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr) \
3616 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr) \
3617 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3618 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3619 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3620 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3622 /* Set up the API message handling tables */
3623 static clib_error_t *
3624 snat_plugin_api_hookup (vlib_main_t * vm)
3626 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3628 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3630 vl_api_##n##_t_handler, \
3632 vl_api_##n##_t_endian, \
3633 vl_api_##n##_t_print, \
3634 sizeof(vl_api_##n##_t), 1);
3635 foreach_snat_plugin_api_msg;
3641 #define vl_msg_name_crc_list
3642 #include <nat/nat_all_api_h.h>
3643 #undef vl_msg_name_crc_list
3646 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3648 #define _(id,n,crc) \
3649 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3650 foreach_vl_msg_name_crc_nat;
3655 plugin_custom_dump_configure (snat_main_t * sm)
3657 #define _(n,f) sm->api_main->msg_print_handlers \
3658 [VL_API_##n + sm->msg_id_base] \
3659 = (void *) vl_api_##f##_t_print;
3660 foreach_snat_plugin_api_msg;
3665 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3668 clib_error_t *error = 0;
3670 name = format (0, "nat_%08x%c", api_version, 0);
3672 /* Ask for a correctly-sized block of API message decode slots */
3674 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3676 error = snat_plugin_api_hookup (vm);
3678 /* Add our API messages to the global name_crc hash table */
3679 setup_message_id_table (sm, sm->api_main);
3681 plugin_custom_dump_configure (sm);
3689 * fd.io coding-style-patch-verification: ON
3692 * eval: (c-set-style "gnu")