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_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
256 snat_main_t *sm = &snat_main;
257 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
260 rv = snat_ipfix_logging_enable_disable (mp->enable,
266 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
270 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
275 s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
277 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
279 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
281 s = format (s, "disable ");
287 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
289 snat_main_t *sm = &snat_main;
290 vl_api_nat_set_reass_reply_t *rmp;
294 nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
295 mp->drop_frag, mp->is_ip6);
297 REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
301 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
305 s = format (0, "SCRIPT: nat_set_reass ");
306 s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
307 clib_host_to_net_u32 (mp->timeout),
308 clib_host_to_net_u16 (mp->max_reass),
309 mp->max_frag, mp->drop_frag, mp->is_ip6);
315 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
317 snat_main_t *sm = &snat_main;
318 vl_api_nat_get_reass_reply_t *rmp;
322 REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
324 rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
325 rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
326 rmp->ip4_max_frag = nat_reass_get_max_frag(0);
327 rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
328 rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
329 rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
330 rmp->ip6_max_frag = nat_reass_get_max_frag(1);
331 rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
337 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
341 s = format (0, "SCRIPT: nat_get_reass");
346 typedef struct nat_api_walk_ctx_t_
348 vl_api_registration_t *reg;
350 } nat_api_walk_ctx_t;
353 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
355 vl_api_nat_reass_details_t *rmp;
356 snat_main_t *sm = &snat_main;
357 nat_api_walk_ctx_t *ctx = arg;
358 ip46_address_t ip_address;
360 rmp = vl_msg_api_alloc (sizeof (*rmp));
361 clib_memset (rmp, 0, sizeof (*rmp));
362 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
363 rmp->context = ctx->context;
365 clib_memcpy (&ip_address.ip4, &reass->key.src, 4);
366 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->src_addr);
368 clib_memcpy (&ip_address.ip4, &reass->key.dst, 4);
369 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->dst_addr);
371 rmp->proto = reass->key.proto;
372 rmp->frag_id = ntohl (reass->key.frag_id);
373 rmp->frag_n = reass->frag_n;
375 vl_api_send_msg (ctx->reg, (u8 *) rmp);
381 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
383 vl_api_nat_reass_details_t *rmp;
384 snat_main_t *sm = &snat_main;
385 nat_api_walk_ctx_t *ctx = arg;
386 ip46_address_t ip_address;
388 rmp = vl_msg_api_alloc (sizeof (*rmp));
389 clib_memset (rmp, 0, sizeof (*rmp));
390 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
391 rmp->context = ctx->context;
393 clib_memcpy (&ip_address.ip6, &reass->key.src, 16);
394 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->src_addr);
396 clib_memcpy (&ip_address.ip6, &reass->key.dst, 16);
397 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->dst_addr);
399 rmp->proto = reass->key.proto;
400 rmp->frag_id = ntohl (reass->key.frag_id);
401 rmp->frag_n = reass->frag_n;
403 vl_api_send_msg (ctx->reg, (u8 *) rmp);
409 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
411 vl_api_registration_t *reg;
413 reg = vl_api_client_index_to_registration (mp->client_index);
417 nat_api_walk_ctx_t ctx = {
419 .context = mp->context,
422 nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
423 nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
427 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
431 s = format (0, "SCRIPT: nat_reass_dump");
437 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
439 snat_main_t *sm = &snat_main;
440 vl_api_nat_set_timeouts_reply_t *rmp;
443 sm->udp_timeout = ntohl (mp->udp);
444 sm->tcp_established_timeout = ntohl (mp->tcp_established);
445 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
446 sm->icmp_timeout = ntohl (mp->icmp);
448 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
451 rv = nat64_set_udp_timeout (ntohl (mp->udp));
455 nat64_set_tcp_timeouts (ntohl (mp->tcp_transitory),
456 ntohl (mp->tcp_established));
459 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
463 vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle)
467 s = format (0, "SCRIPT: nat_set_timeouts ");
468 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
470 ntohl (mp->tcp_established),
471 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
477 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
479 snat_main_t *sm = &snat_main;
480 vl_api_nat_get_timeouts_reply_t *rmp;
484 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
486 rmp->udp = htonl (sm->udp_timeout);
487 rmp->tcp_established = htonl (sm->tcp_established_timeout);
488 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
489 rmp->icmp = htonl (sm->icmp_timeout);
495 vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle)
499 s = format (0, "SCRIPT: nat_get_timeouts");
505 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
506 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
508 snat_main_t *sm = &snat_main;
509 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
511 u16 port_start, port_end;
513 if (sm->deterministic)
515 rv = VNET_API_ERROR_UNSUPPORTED;
521 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
522 nat_set_alloc_addr_and_port_default ();
524 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
525 nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
528 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
529 port_start = ntohs (mp->start_port);
530 port_end = ntohs (mp->end_port);
531 if (port_end <= port_start)
533 rv = VNET_API_ERROR_INVALID_VALUE;
536 nat_set_alloc_addr_and_port_range (port_start, port_end);
539 rv = VNET_API_ERROR_INVALID_VALUE;
544 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
547 static void *vl_api_nat_set_addr_and_port_alloc_alg_t_print
548 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp, void *handle)
552 s = format (0, "SCRIPT: nat_set_addr_and_port_alloc_alg ");
553 s = format (s, "alg %d psid_offset %d psid_length %d psid %d start_port %d "
555 ntohl (mp->alg), ntohl (mp->psid_offset),
556 ntohl (mp->psid_length), ntohs (mp->psid),
557 ntohs (mp->start_port), ntohs (mp->end_port));
563 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
564 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
566 snat_main_t *sm = &snat_main;
567 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
571 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
573 rmp->alg = sm->addr_and_port_alloc_alg;
574 rmp->psid_offset = sm->psid_offset;
575 rmp->psid_length = sm->psid_length;
576 rmp->psid = htons (sm->psid);
577 rmp->start_port = htons (sm->start_port);
578 rmp->end_port = htons (sm->end_port);
583 static void *vl_api_nat_get_addr_and_port_alloc_alg_t_print
584 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp, void *handle)
588 s = format (0, "SCRIPT: nat_get_addr_and_port_alloc_alg");
594 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
596 snat_main_t *sm = &snat_main;
597 vl_api_nat_set_mss_clamping_reply_t *rmp;
602 sm->mss_clamping = ntohs (mp->mss_value);
603 sm->mss_value_net = mp->mss_value;
606 sm->mss_clamping = 0;
608 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
612 vl_api_nat_set_mss_clamping_t_print (vl_api_nat_set_mss_clamping_t * mp,
617 s = format (0, "SCRIPT: nat_set_mss_clamping enable %d mss_value %d\n",
618 mp->enable, ntohs (mp->mss_value));
624 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
626 snat_main_t *sm = &snat_main;
627 vl_api_nat_get_mss_clamping_reply_t *rmp;
631 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
633 rmp->enable = sm->mss_clamping ? 1 : 0;
634 rmp->mss_value = htons (sm->mss_clamping);
640 vl_api_nat_get_mss_clamping_t_print (vl_api_nat_get_mss_clamping_t * mp,
645 s = format (0, "SCRIPT: nat_get_mss_clamping");
651 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
653 snat_main_t *sm = &snat_main;
654 vl_api_nat_ha_set_listener_reply_t *rmp;
658 memcpy (&addr, &mp->ip_address, sizeof (addr));
660 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
661 clib_net_to_host_u32 (mp->path_mtu));
663 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
667 vl_api_nat_ha_set_listener_t_print (vl_api_nat_ha_set_listener_t * mp,
672 s = format (0, "SCRIPT: nat_ha_set_listener ");
673 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
674 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
675 s = format (s, "path_mtu %d", clib_net_to_host_u32 (mp->path_mtu));
681 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
683 snat_main_t *sm = &snat_main;
684 vl_api_nat_ha_get_listener_reply_t *rmp;
690 nat_ha_get_listener (&addr, &port, &path_mtu);
693 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
695 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
696 rmp->port = clib_host_to_net_u16 (port);
697 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
703 vl_api_nat_ha_get_listener_t_print (vl_api_nat_ha_get_listener_t * mp,
708 s = format (0, "SCRIPT: nat_ha_get_listener");
714 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
716 snat_main_t *sm = &snat_main;
717 vl_api_nat_ha_set_failover_reply_t *rmp;
721 memcpy (&addr, &mp->ip_address, sizeof (addr));
723 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
724 clib_net_to_host_u32 (mp->session_refresh_interval));
726 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
730 vl_api_nat_ha_set_failover_t_print (vl_api_nat_ha_set_failover_t * mp,
735 s = format (0, "SCRIPT: nat_ha_set_failover ");
736 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
737 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
743 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
745 snat_main_t *sm = &snat_main;
746 vl_api_nat_ha_get_failover_reply_t *rmp;
750 u32 session_refresh_interval;
752 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
755 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
757 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
758 rmp->port = clib_host_to_net_u16 (port);
759 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
765 vl_api_nat_ha_get_failover_t_print (vl_api_nat_ha_get_failover_t * mp,
770 s = format (0, "SCRIPT: nat_ha_get_failover");
776 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
778 snat_main_t *sm = &snat_main;
779 vl_api_nat_ha_flush_reply_t *rmp;
784 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
788 vl_api_nat_ha_flush_t_print (vl_api_nat_ha_flush_t * mp, void *handle)
792 s = format (0, "SCRIPT: nat_ha_flush ");
798 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
800 snat_main_t *sm = &snat_main;
801 vl_api_registration_t *reg;
802 vl_api_nat_ha_resync_completed_event_t *mp;
804 reg = vl_api_client_index_to_registration (client_index);
808 mp = vl_msg_api_alloc (sizeof (*mp));
809 clib_memset (mp, 0, sizeof (*mp));
810 mp->client_index = client_index;
812 mp->missed_count = clib_host_to_net_u32 (missed_count);
814 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
816 vl_api_send_msg (reg, (u8 *) mp);
820 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
822 snat_main_t *sm = &snat_main;
823 vl_api_nat_ha_resync_reply_t *rmp;
827 nat_ha_resync (mp->client_index, mp->pid,
828 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
831 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
835 vl_api_nat_ha_resync_t_print (vl_api_nat_ha_resync_t * mp, void *handle)
839 s = format (0, "SCRIPT: nat_ha_resync ");
841 format (s, "want_resync_event %d pid %d", mp->want_resync_event,
842 clib_host_to_net_u32 (mp->pid));
851 vl_api_nat44_add_del_address_range_t_handler
852 (vl_api_nat44_add_del_address_range_t * mp)
854 snat_main_t *sm = &snat_main;
855 vl_api_nat44_add_del_address_range_reply_t *rmp;
856 ip4_address_t this_addr;
857 u8 is_add, twice_nat;
858 u32 start_host_order, end_host_order;
864 if (sm->deterministic)
866 rv = VNET_API_ERROR_UNSUPPORTED;
870 if (sm->static_mapping_only)
872 rv = VNET_API_ERROR_FEATURE_DISABLED;
877 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
879 tmp = (u32 *) mp->first_ip_address;
880 start_host_order = clib_host_to_net_u32 (tmp[0]);
881 tmp = (u32 *) mp->last_ip_address;
882 end_host_order = clib_host_to_net_u32 (tmp[0]);
884 count = (end_host_order - start_host_order) + 1;
886 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
889 nat_log_info ("%U - %U, %d addresses...",
890 format_ip4_address, mp->first_ip_address,
891 format_ip4_address, mp->last_ip_address, count);
893 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
895 for (i = 0; i < count; i++)
898 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
900 rv = snat_del_address (sm, this_addr, 0, twice_nat);
906 nat44_add_del_address_dpo (this_addr, is_add);
908 increment_v4_address (&this_addr);
912 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
915 static void *vl_api_nat44_add_del_address_range_t_print
916 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
920 s = format (0, "SCRIPT: nat44_add_address_range ");
921 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
922 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
924 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
926 s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
931 send_nat44_address_details (snat_address_t * a,
932 vl_api_registration_t * reg, u32 context,
935 vl_api_nat44_address_details_t *rmp;
936 snat_main_t *sm = &snat_main;
938 rmp = vl_msg_api_alloc (sizeof (*rmp));
939 clib_memset (rmp, 0, sizeof (*rmp));
940 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
941 clib_memcpy (rmp->ip_address, &(a->addr), 4);
942 if (a->fib_index != ~0)
944 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
945 rmp->vrf_id = ntohl (fib->ft_table_id);
950 rmp->flags |= NAT_API_IS_TWICE_NAT;
951 rmp->context = context;
953 vl_api_send_msg (reg, (u8 *) rmp);
957 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
959 vl_api_registration_t *reg;
960 snat_main_t *sm = &snat_main;
963 if (sm->deterministic)
966 reg = vl_api_client_index_to_registration (mp->client_index);
971 vec_foreach (a, sm->addresses)
972 send_nat44_address_details (a, reg, mp->context, 0);
973 vec_foreach (a, sm->twice_nat_addresses)
974 send_nat44_address_details (a, reg, mp->context, 1);
979 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
984 s = format (0, "SCRIPT: nat44_address_dump ");
990 vl_api_nat44_interface_add_del_feature_t_handler
991 (vl_api_nat44_interface_add_del_feature_t * mp)
993 snat_main_t *sm = &snat_main;
994 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
995 u32 sw_if_index = ntohl (mp->sw_if_index);
999 is_del = !mp->is_add;
1001 VALIDATE_SW_IF_INDEX (mp);
1004 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
1007 BAD_SW_IF_INDEX_LABEL;
1009 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
1012 static void *vl_api_nat44_interface_add_del_feature_t_print
1013 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
1017 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
1018 s = format (s, "sw_if_index %d %s %s",
1019 clib_host_to_net_u32 (mp->sw_if_index),
1020 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1021 mp->is_add ? "" : "del");
1027 send_nat44_interface_details (snat_interface_t * i,
1028 vl_api_registration_t * reg, u32 context)
1030 vl_api_nat44_interface_details_t *rmp;
1031 snat_main_t *sm = &snat_main;
1033 rmp = vl_msg_api_alloc (sizeof (*rmp));
1034 clib_memset (rmp, 0, sizeof (*rmp));
1035 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
1036 rmp->sw_if_index = ntohl (i->sw_if_index);
1038 if (nat_interface_is_inside (i))
1039 rmp->flags |= NAT_API_IS_INSIDE;
1040 if (nat_interface_is_outside (i))
1041 rmp->flags |= NAT_API_IS_OUTSIDE;
1043 rmp->context = context;
1045 vl_api_send_msg (reg, (u8 *) rmp);
1049 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
1051 vl_api_registration_t *reg;
1052 snat_main_t *sm = &snat_main;
1053 snat_interface_t *i;
1055 reg = vl_api_client_index_to_registration (mp->client_index);
1060 pool_foreach (i, sm->interfaces,
1062 send_nat44_interface_details(i, reg, mp->context);
1068 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
1073 s = format (0, "SCRIPT: nat44_interface_dump ");
1079 vl_api_nat44_interface_add_del_output_feature_t_handler
1080 (vl_api_nat44_interface_add_del_output_feature_t * mp)
1082 snat_main_t *sm = &snat_main;
1083 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
1084 u32 sw_if_index = ntohl (mp->sw_if_index);
1087 if (sm->deterministic)
1089 rv = VNET_API_ERROR_UNSUPPORTED;
1093 VALIDATE_SW_IF_INDEX (mp);
1095 rv = snat_interface_add_del_output_feature (sw_if_index,
1096 mp->flags & NAT_API_IS_INSIDE,
1099 BAD_SW_IF_INDEX_LABEL;
1101 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
1104 static void *vl_api_nat44_interface_add_del_output_feature_t_print
1105 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
1109 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
1110 s = format (s, "sw_if_index %d %s %s",
1111 clib_host_to_net_u32 (mp->sw_if_index),
1112 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1113 mp->is_add ? "" : "del");
1119 send_nat44_interface_output_feature_details (snat_interface_t * i,
1120 vl_api_registration_t * reg,
1123 vl_api_nat44_interface_output_feature_details_t *rmp;
1124 snat_main_t *sm = &snat_main;
1126 rmp = vl_msg_api_alloc (sizeof (*rmp));
1127 clib_memset (rmp, 0, sizeof (*rmp));
1129 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
1130 rmp->sw_if_index = ntohl (i->sw_if_index);
1131 rmp->context = context;
1133 if (nat_interface_is_inside (i))
1134 rmp->flags |= NAT_API_IS_INSIDE;
1136 vl_api_send_msg (reg, (u8 *) rmp);
1140 vl_api_nat44_interface_output_feature_dump_t_handler
1141 (vl_api_nat44_interface_output_feature_dump_t * mp)
1143 vl_api_registration_t *reg;
1144 snat_main_t *sm = &snat_main;
1145 snat_interface_t *i;
1147 if (sm->deterministic)
1150 reg = vl_api_client_index_to_registration (mp->client_index);
1155 pool_foreach (i, sm->output_feature_interfaces,
1157 send_nat44_interface_output_feature_details(i, reg, mp->context);
1162 static void *vl_api_nat44_interface_output_feature_dump_t_print
1163 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1167 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1173 vl_api_nat44_add_del_static_mapping_t_handler
1174 (vl_api_nat44_add_del_static_mapping_t * mp)
1176 snat_main_t *sm = &snat_main;
1177 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1178 ip4_address_t local_addr, external_addr;
1179 u16 local_port = 0, external_port = 0;
1180 u32 vrf_id, external_sw_if_index;
1181 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1183 snat_protocol_t proto;
1187 if (sm->deterministic)
1189 rv = VNET_API_ERROR_UNSUPPORTED;
1193 len = vl_api_string_len (&mp->tag);
1197 rv = VNET_API_ERROR_INVALID_VALUE;
1201 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1202 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1204 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1206 local_port = clib_net_to_host_u16 (mp->local_port);
1207 external_port = clib_net_to_host_u16 (mp->external_port);
1210 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1211 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1212 proto = ip_proto_to_snat_proto (mp->protocol);
1214 if (mp->flags & NAT_API_IS_TWICE_NAT)
1215 twice_nat = TWICE_NAT;
1216 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1217 twice_nat = TWICE_NAT_SELF;
1219 tag = vec_new (u8, len);
1221 memcpy (tag, mp->tag.buf, len);
1222 vec_terminate_c_string (tag);
1224 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1225 external_port, vrf_id,
1226 mp->flags & NAT_API_IS_ADDR_ONLY,
1227 external_sw_if_index, proto,
1228 mp->is_add, twice_nat,
1229 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0);
1233 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1236 static void *vl_api_nat44_add_del_static_mapping_t_print
1237 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1241 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1242 s = format (s, "protocol %d local_addr %U external_addr %U ",
1244 format_ip4_address, mp->local_ip_address,
1245 format_ip4_address, mp->external_ip_address);
1247 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1248 s = format (s, "local_port %d external_port %d ",
1249 clib_net_to_host_u16 (mp->local_port),
1250 clib_net_to_host_u16 (mp->external_port));
1252 s = format (s, "twice_nat %d out2in_only %d ",
1253 mp->flags & NAT_API_IS_TWICE_NAT,
1254 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1256 if (mp->vrf_id != ~0)
1257 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1259 if (mp->external_sw_if_index != ~0)
1260 s = format (s, "external_sw_if_index %d",
1261 clib_net_to_host_u32 (mp->external_sw_if_index));
1266 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1267 vl_api_registration_t * reg, u32 context)
1269 vl_api_nat44_static_mapping_details_t *rmp;
1270 snat_main_t *sm = &snat_main;
1271 u32 len = sizeof (*rmp);
1275 len += vec_len (m->tag);
1276 rmp = vl_msg_api_alloc (len);
1277 clib_memset (rmp, 0, len);
1278 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1282 rmp = vl_msg_api_alloc (len);
1283 clib_memset (rmp, 0, len);
1287 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1289 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1290 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1291 rmp->external_sw_if_index = ~0;
1292 rmp->vrf_id = htonl (m->vrf_id);
1293 rmp->context = context;
1295 if (m->twice_nat == TWICE_NAT)
1296 rmp->flags |= NAT_API_IS_TWICE_NAT;
1297 else if (m->twice_nat == TWICE_NAT_SELF)
1298 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1300 if (is_out2in_only_static_mapping (m))
1301 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1303 if (is_addr_only_static_mapping (m))
1305 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1309 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1310 rmp->external_port = htons (m->external_port);
1311 rmp->local_port = htons (m->local_port);
1314 vl_api_send_msg (reg, (u8 *) rmp);
1318 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1319 vl_api_registration_t * reg,
1322 vl_api_nat44_static_mapping_details_t *rmp;
1323 snat_main_t *sm = &snat_main;
1324 u32 len = sizeof (*rmp);
1328 len += vec_len (m->tag);
1329 rmp = vl_msg_api_alloc (len);
1330 clib_memset (rmp, 0, len);
1331 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1335 rmp = vl_msg_api_alloc (len);
1336 clib_memset (rmp, 0, len);
1340 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1341 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1342 rmp->external_sw_if_index = htonl (m->sw_if_index);
1343 rmp->vrf_id = htonl (m->vrf_id);
1344 rmp->context = context;
1347 rmp->flags |= NAT_API_IS_TWICE_NAT;
1351 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1355 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1356 rmp->external_port = htons (m->e_port);
1357 rmp->local_port = htons (m->l_port);
1360 vl_api_send_msg (reg, (u8 *) rmp);
1364 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1367 vl_api_registration_t *reg;
1368 snat_main_t *sm = &snat_main;
1369 snat_static_mapping_t *m;
1370 snat_static_map_resolve_t *rp;
1373 if (sm->deterministic)
1376 reg = vl_api_client_index_to_registration (mp->client_index);
1381 pool_foreach (m, sm->static_mappings,
1383 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1384 send_nat44_static_mapping_details (m, reg, mp->context);
1388 for (j = 0; j < vec_len (sm->to_resolve); j++)
1390 rp = sm->to_resolve + j;
1391 if (!rp->identity_nat)
1392 send_nat44_static_map_resolve_details (rp, reg, mp->context);
1397 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1402 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1408 vl_api_nat44_add_del_identity_mapping_t_handler
1409 (vl_api_nat44_add_del_identity_mapping_t * mp)
1411 snat_main_t *sm = &snat_main;
1412 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1415 u32 vrf_id, sw_if_index;
1417 snat_protocol_t proto = ~0;
1421 if (sm->deterministic)
1423 rv = VNET_API_ERROR_UNSUPPORTED;
1427 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1429 port = clib_net_to_host_u16 (mp->port);
1430 proto = ip_proto_to_snat_proto (mp->protocol);
1432 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1433 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1434 if (sw_if_index != ~0)
1437 memcpy (&addr.as_u8, mp->ip_address, 4);
1439 len = vl_api_string_len (&mp->tag);
1441 tag = vec_new (u8, len);
1442 memcpy (tag, mp->tag.buf, len);
1443 vec_terminate_c_string (tag);
1446 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1447 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1448 proto, mp->is_add, 0, 0, tag, 1);
1452 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1455 static void *vl_api_nat44_add_del_identity_mapping_t_print
1456 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1460 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1461 if (mp->sw_if_index != ~0)
1462 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1464 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1466 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1468 format (s, " protocol %d port %d", mp->protocol,
1469 clib_net_to_host_u16 (mp->port));
1471 if (mp->vrf_id != ~0)
1472 s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1478 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1479 vl_api_registration_t * reg, u32 context)
1481 vl_api_nat44_identity_mapping_details_t *rmp;
1482 snat_main_t *sm = &snat_main;
1483 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1484 u32 len = sizeof (*rmp);
1488 len += vec_len (m->tag);
1489 rmp = vl_msg_api_alloc (len);
1490 clib_memset (rmp, 0, len);
1491 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1495 rmp = vl_msg_api_alloc (len);
1496 clib_memset (rmp, 0, len);
1500 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1502 if (is_addr_only_static_mapping (m))
1503 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1505 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1506 rmp->port = htons (m->local_port);
1507 rmp->sw_if_index = ~0;
1508 rmp->vrf_id = htonl (local->vrf_id);
1509 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1510 rmp->context = context;
1512 vl_api_send_msg (reg, (u8 *) rmp);
1516 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1517 vl_api_registration_t * reg,
1520 vl_api_nat44_identity_mapping_details_t *rmp;
1521 snat_main_t *sm = &snat_main;
1522 u32 len = sizeof (*rmp);
1526 len += vec_len (m->tag);
1527 rmp = vl_msg_api_alloc (len);
1528 clib_memset (rmp, 0, len);
1529 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1533 rmp = vl_msg_api_alloc (len);
1534 clib_memset (rmp, 0, len);
1538 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1541 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1543 rmp->port = htons (m->l_port);
1544 rmp->sw_if_index = htonl (m->sw_if_index);
1545 rmp->vrf_id = htonl (m->vrf_id);
1546 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1547 rmp->context = context;
1549 vl_api_send_msg (reg, (u8 *) rmp);
1553 vl_api_nat44_identity_mapping_dump_t_handler
1554 (vl_api_nat44_identity_mapping_dump_t * mp)
1556 vl_api_registration_t *reg;
1557 snat_main_t *sm = &snat_main;
1558 snat_static_mapping_t *m;
1559 snat_static_map_resolve_t *rp;
1562 if (sm->deterministic)
1565 reg = vl_api_client_index_to_registration (mp->client_index);
1570 pool_foreach (m, sm->static_mappings,
1572 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1574 pool_foreach_index (j, m->locals,
1576 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1582 for (j = 0; j < vec_len (sm->to_resolve); j++)
1584 rp = sm->to_resolve + j;
1585 if (rp->identity_nat)
1586 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1590 static void *vl_api_nat44_identity_mapping_dump_t_print
1591 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1595 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1601 vl_api_nat44_add_del_interface_addr_t_handler
1602 (vl_api_nat44_add_del_interface_addr_t * mp)
1604 snat_main_t *sm = &snat_main;
1605 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1606 u32 sw_if_index = ntohl (mp->sw_if_index);
1610 if (sm->deterministic)
1612 rv = VNET_API_ERROR_UNSUPPORTED;
1616 is_del = !mp->is_add;
1618 VALIDATE_SW_IF_INDEX (mp);
1620 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1621 mp->flags & NAT_API_IS_TWICE_NAT);
1623 BAD_SW_IF_INDEX_LABEL;
1625 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1628 static void *vl_api_nat44_add_del_interface_addr_t_print
1629 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1633 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1634 s = format (s, "sw_if_index %d twice_nat %d %s",
1635 clib_host_to_net_u32 (mp->sw_if_index),
1636 mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1642 send_nat44_interface_addr_details (u32 sw_if_index,
1643 vl_api_registration_t * reg, u32 context,
1646 vl_api_nat44_interface_addr_details_t *rmp;
1647 snat_main_t *sm = &snat_main;
1649 rmp = vl_msg_api_alloc (sizeof (*rmp));
1650 clib_memset (rmp, 0, sizeof (*rmp));
1652 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1653 rmp->sw_if_index = ntohl (sw_if_index);
1656 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1657 rmp->context = context;
1659 vl_api_send_msg (reg, (u8 *) rmp);
1663 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1666 vl_api_registration_t *reg;
1667 snat_main_t *sm = &snat_main;
1670 if (sm->deterministic)
1673 reg = vl_api_client_index_to_registration (mp->client_index);
1678 vec_foreach (i, sm->auto_add_sw_if_indices)
1679 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1680 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1681 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1686 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1691 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1697 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1700 vl_api_nat44_user_details_t *rmp;
1701 snat_main_t *sm = &snat_main;
1702 ip4_main_t *im = &ip4_main;
1704 rmp = vl_msg_api_alloc (sizeof (*rmp));
1705 clib_memset (rmp, 0, sizeof (*rmp));
1706 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1708 if (!pool_is_free_index (im->fibs, u->fib_index))
1710 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1711 rmp->vrf_id = ntohl (fib->ft_table_id);
1714 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1715 rmp->nsessions = ntohl (u->nsessions);
1716 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1717 rmp->context = context;
1719 vl_api_send_msg (reg, (u8 *) rmp);
1723 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1725 vl_api_registration_t *reg;
1726 snat_main_t *sm = &snat_main;
1727 snat_main_per_thread_data_t *tsm;
1730 if (sm->deterministic)
1733 reg = vl_api_client_index_to_registration (mp->client_index);
1738 vec_foreach (tsm, sm->per_thread_data)
1740 pool_foreach (u, tsm->users,
1742 send_nat44_user_details (u, reg, mp->context);
1749 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1753 s = format (0, "SCRIPT: nat44_user_dump ");
1759 send_nat44_user_session_details (snat_session_t * s,
1760 vl_api_registration_t * reg, u32 context)
1762 vl_api_nat44_user_session_details_t *rmp;
1763 snat_main_t *sm = &snat_main;
1765 rmp = vl_msg_api_alloc (sizeof (*rmp));
1766 clib_memset (rmp, 0, sizeof (*rmp));
1768 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1769 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1770 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1772 if (snat_is_session_static (s))
1773 rmp->flags |= NAT_API_IS_STATIC;
1775 if (is_twice_nat_session (s))
1776 rmp->flags |= NAT_API_IS_TWICE_NAT;
1778 if (is_ed_session (s) || is_fwd_bypass_session (s))
1779 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1781 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1782 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1783 rmp->total_pkts = ntohl (s->total_pkts);
1784 rmp->context = context;
1785 if (snat_is_unk_proto_session (s))
1787 rmp->outside_port = 0;
1788 rmp->inside_port = 0;
1789 rmp->protocol = ntohs (s->in2out.port);
1793 rmp->outside_port = s->out2in.port;
1794 rmp->inside_port = s->in2out.port;
1795 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1797 if (is_ed_session (s) || is_fwd_bypass_session (s))
1799 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1800 rmp->ext_host_port = s->ext_host_port;
1801 if (is_twice_nat_session (s))
1803 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1804 rmp->ext_host_nat_port = s->ext_host_nat_port;
1808 vl_api_send_msg (reg, (u8 *) rmp);
1812 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1815 vl_api_registration_t *reg;
1816 snat_main_t *sm = &snat_main;
1817 snat_main_per_thread_data_t *tsm;
1819 clib_bihash_kv_8_8_t key, value;
1820 snat_user_key_t ukey;
1822 u32 session_index, head_index, elt_index;
1823 dlist_elt_t *head, *elt;
1826 if (sm->deterministic)
1829 reg = vl_api_client_index_to_registration (mp->client_index);
1833 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1834 ip.src_address.as_u32 = ukey.addr.as_u32;
1835 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1836 key.key = ukey.as_u64;
1837 if (sm->num_workers > 1)
1839 vec_elt_at_index (sm->per_thread_data,
1840 sm->worker_in2out_cb (&ip, ukey.fib_index));
1842 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1843 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1845 u = pool_elt_at_index (tsm->users, value.value);
1846 if (!u->nsessions && !u->nstaticsessions)
1849 head_index = u->sessions_per_user_list_head_index;
1850 head = pool_elt_at_index (tsm->list_pool, head_index);
1851 elt_index = head->next;
1852 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1853 session_index = elt->value;
1854 while (session_index != ~0)
1856 s = pool_elt_at_index (tsm->sessions, session_index);
1858 send_nat44_user_session_details (s, reg, mp->context);
1860 elt_index = elt->next;
1861 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1862 session_index = elt->value;
1867 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1872 s = format (0, "SCRIPT: nat44_user_session_dump ");
1873 s = format (s, "ip_address %U vrf_id %d\n",
1874 format_ip4_address, mp->ip_address,
1875 clib_net_to_host_u32 (mp->vrf_id));
1880 static nat44_lb_addr_port_t *
1881 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1882 u32 addr_port_pair_num)
1885 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1886 vl_api_nat44_lb_addr_port_t *ap;
1888 for (i = 0; i < addr_port_pair_num; i++)
1890 ap = &addr_port_pairs[i];
1891 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1892 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1893 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1894 lb_addr_port.probability = ap->probability;
1895 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1896 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1899 return lb_addr_port_pairs;
1903 vl_api_nat44_add_del_lb_static_mapping_t_handler
1904 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1906 snat_main_t *sm = &snat_main;
1907 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1908 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1910 nat44_lb_addr_port_t *locals = 0;
1911 ip4_address_t e_addr;
1912 snat_protocol_t proto;
1913 vl_api_string_t *sp;
1917 if (!sm->endpoint_dependent)
1919 rv = VNET_API_ERROR_UNSUPPORTED;
1924 unformat_nat44_lb_addr_port (mp->locals,
1925 clib_net_to_host_u32 (mp->local_num));
1926 clib_memcpy (&e_addr, mp->external_addr, 4);
1927 proto = ip_proto_to_snat_proto (mp->protocol);
1929 if (mp->flags & NAT_API_IS_TWICE_NAT)
1930 twice_nat = TWICE_NAT;
1931 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1932 twice_nat = TWICE_NAT_SELF;
1934 sp = (void *) &mp->locals +
1935 sizeof (vl_api_nat44_lb_addr_port_t) *
1936 clib_net_to_host_u32 (mp->local_num);
1938 len = vl_api_string_len (sp);
1940 tag = vec_new (u8, len);
1941 memcpy (tag, sp->buf, len);
1942 vec_terminate_c_string (tag);
1945 nat44_add_del_lb_static_mapping (e_addr,
1946 clib_net_to_host_u16 (mp->external_port),
1947 proto, locals, mp->is_add,
1949 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1950 clib_net_to_host_u32 (mp->affinity));
1956 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1959 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1960 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1964 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1965 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1967 mp->flags & NAT_API_IS_TWICE_NAT,
1968 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1974 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1975 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1977 snat_main_t *sm = &snat_main;
1978 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1980 ip4_address_t e_addr, l_addr;
1981 snat_protocol_t proto;
1983 if (!sm->endpoint_dependent)
1985 rv = VNET_API_ERROR_UNSUPPORTED;
1989 clib_memcpy (&e_addr, mp->external_addr, 4);
1990 clib_memcpy (&l_addr, mp->local.addr, 4);
1991 proto = ip_proto_to_snat_proto (mp->protocol);
1994 nat44_lb_static_mapping_add_del_local (e_addr,
1995 clib_net_to_host_u16
1996 (mp->external_port), l_addr,
1997 clib_net_to_host_u16 (mp->
2000 clib_net_to_host_u32 (mp->
2002 mp->local.probability, mp->is_add);
2005 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
2008 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
2009 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
2013 s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
2014 s = format (s, "is_add %d", mp->is_add);
2020 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
2021 vl_api_registration_t * reg,
2024 vl_api_nat44_lb_static_mapping_details_t *rmp;
2025 snat_main_t *sm = &snat_main;
2026 nat44_lb_addr_port_t *ap;
2027 vl_api_nat44_lb_addr_port_t *locals;
2028 vl_api_string_t *sp;
2030 u32 len = sizeof (*rmp);
2034 len += pool_elts (m->locals) *
2035 sizeof (nat44_lb_addr_port_t) + vec_len (m->tag);
2036 rmp = vl_msg_api_alloc (len);
2037 clib_memset (rmp, 0, len);
2039 sp = (void *) &m->locals +
2040 sizeof (vl_api_nat44_lb_addr_port_t) * pool_elts (m->locals);
2041 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, sp);
2045 len += pool_elts (m->locals) * sizeof (nat44_lb_addr_port_t);
2046 rmp = vl_msg_api_alloc (len);
2047 clib_memset (rmp, 0, len);
2051 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2053 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
2054 rmp->external_port = ntohs (m->external_port);
2055 rmp->protocol = snat_proto_to_ip_proto (m->proto);
2056 rmp->context = context;
2058 if (m->twice_nat == TWICE_NAT)
2059 rmp->flags |= NAT_API_IS_TWICE_NAT;
2060 else if (m->twice_nat == TWICE_NAT_SELF)
2061 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
2062 if (is_out2in_only_static_mapping (m))
2063 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
2065 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
2067 pool_foreach (ap, m->locals,
2069 clib_memcpy (locals->addr, &(ap->addr), 4);
2070 locals->port = htons (ap->port);
2071 locals->probability = ap->probability;
2072 locals->vrf_id = ntohl (ap->vrf_id);
2077 rmp->local_num = ntohl (local_num);
2079 vl_api_send_msg (reg, (u8 *) rmp);
2083 vl_api_nat44_lb_static_mapping_dump_t_handler
2084 (vl_api_nat44_lb_static_mapping_dump_t * mp)
2086 vl_api_registration_t *reg;
2087 snat_main_t *sm = &snat_main;
2088 snat_static_mapping_t *m;
2090 if (!sm->endpoint_dependent)
2093 reg = vl_api_client_index_to_registration (mp->client_index);
2098 pool_foreach (m, sm->static_mappings,
2100 if (is_lb_static_mapping(m))
2101 send_nat44_lb_static_mapping_details (m, reg, mp->context);
2106 static void *vl_api_nat44_lb_static_mapping_dump_t_print
2107 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
2111 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
2117 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
2119 snat_main_t *sm = &snat_main;
2120 vl_api_nat44_del_session_reply_t *rmp;
2121 ip4_address_t addr, eh_addr;
2126 snat_protocol_t proto;
2128 if (sm->deterministic)
2130 rv = VNET_API_ERROR_UNSUPPORTED;
2134 memcpy (&addr.as_u8, mp->address, 4);
2135 port = clib_net_to_host_u16 (mp->port);
2136 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
2137 proto = ip_proto_to_snat_proto (mp->protocol);
2138 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
2139 eh_port = clib_net_to_host_u16 (mp->ext_host_port);
2141 is_in = mp->flags & NAT_API_IS_INSIDE;
2143 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2145 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
2148 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
2151 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
2155 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
2160 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
2161 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
2162 format_ip4_address, mp->address,
2163 clib_net_to_host_u16 (mp->port),
2164 mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
2165 mp->flags & NAT_API_IS_INSIDE);
2166 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2167 s = format (s, "ext_host_address %U ext_host_port %d",
2168 format_ip4_address, mp->ext_host_address,
2169 clib_net_to_host_u16 (mp->ext_host_port));
2175 vl_api_nat44_forwarding_enable_disable_t_handler
2176 (vl_api_nat44_forwarding_enable_disable_t * mp)
2178 snat_main_t *sm = &snat_main;
2179 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
2181 u32 *ses_to_be_removed = 0, *ses_index;
2182 snat_main_per_thread_data_t *tsm;
2185 sm->forwarding_enabled = mp->enable != 0;
2187 if (mp->enable == 0)
2190 vec_foreach (tsm, sm->per_thread_data)
2192 pool_foreach (s, tsm->sessions,
2194 if (is_fwd_bypass_session(s))
2196 vec_add1 (ses_to_be_removed, s - tsm->sessions);
2199 vec_foreach (ses_index, ses_to_be_removed)
2201 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2202 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2203 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2205 vec_free (ses_to_be_removed);
2210 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2213 static void *vl_api_nat44_forwarding_enable_disable_t_print
2214 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2218 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2219 s = format (s, "enable %d", mp->enable != 0);
2225 vl_api_nat44_forwarding_is_enabled_t_handler
2226 (vl_api_nat44_forwarding_is_enabled_t * mp)
2228 vl_api_registration_t *reg;
2229 snat_main_t *sm = &snat_main;
2230 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2232 reg = vl_api_client_index_to_registration (mp->client_index);
2236 rmp = vl_msg_api_alloc (sizeof (*rmp));
2237 clib_memset (rmp, 0, sizeof (*rmp));
2239 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2240 rmp->context = mp->context;
2242 rmp->enabled = sm->forwarding_enabled;
2244 vl_api_send_msg (reg, (u8 *) rmp);
2247 static void *vl_api_nat44_forwarding_is_enabled_t_print
2248 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2252 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2257 /*******************************/
2258 /*** Deterministic NAT (CGN) ***/
2259 /*******************************/
2262 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
2264 snat_main_t *sm = &snat_main;
2265 vl_api_nat_det_add_del_map_reply_t *rmp;
2267 ip4_address_t in_addr, out_addr;
2269 if (!sm->deterministic)
2271 rv = VNET_API_ERROR_UNSUPPORTED;
2275 clib_memcpy (&in_addr, mp->in_addr, 4);
2276 clib_memcpy (&out_addr, mp->out_addr, 4);
2277 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
2278 mp->out_plen, mp->is_add);
2281 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
2285 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
2290 s = format (0, "SCRIPT: nat_det_add_del_map ");
2291 s = format (s, "inside address %U/%d outside address %U/%d\n",
2292 format_ip4_address, mp->in_addr, mp->in_plen,
2293 format_ip4_address, mp->out_addr, mp->out_plen);
2299 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
2301 snat_main_t *sm = &snat_main;
2302 vl_api_nat_det_forward_reply_t *rmp;
2304 u16 lo_port = 0, hi_port = 0;
2306 ip4_address_t in_addr, out_addr;
2308 if (!sm->deterministic)
2310 rv = VNET_API_ERROR_UNSUPPORTED;
2311 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
2315 out_addr.as_u32 = 0;
2316 clib_memcpy (&in_addr, mp->in_addr, 4);
2317 dm = snat_det_map_by_user (sm, &in_addr);
2320 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2324 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2325 hi_port = lo_port + dm->ports_per_host - 1;
2329 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
2331 rmp->out_port_lo = ntohs (lo_port);
2332 rmp->out_port_hi = ntohs (hi_port);
2333 clib_memcpy (rmp->out_addr, &out_addr, 4);
2339 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
2343 s = format (0, "SCRIPT: nat_det_forward");
2344 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
2350 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
2352 snat_main_t *sm = &snat_main;
2353 vl_api_nat_det_reverse_reply_t *rmp;
2355 ip4_address_t out_addr, in_addr;
2358 if (!sm->deterministic)
2360 rv = VNET_API_ERROR_UNSUPPORTED;
2361 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
2366 clib_memcpy (&out_addr, mp->out_addr, 4);
2367 dm = snat_det_map_by_out (sm, &out_addr);
2370 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2374 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
2378 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
2380 clib_memcpy (rmp->in_addr, &in_addr, 4);
2386 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
2390 s = format (0, "SCRIPT: nat_det_reverse");
2391 s = format (s, "outside ip address %U outside port %d",
2392 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
2398 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
2401 vl_api_nat_det_map_details_t *rmp;
2402 snat_main_t *sm = &snat_main;
2404 rmp = vl_msg_api_alloc (sizeof (*rmp));
2405 clib_memset (rmp, 0, sizeof (*rmp));
2406 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
2407 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
2408 rmp->in_plen = m->in_plen;
2409 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
2410 rmp->out_plen = m->out_plen;
2411 rmp->sharing_ratio = htonl (m->sharing_ratio);
2412 rmp->ports_per_host = htons (m->ports_per_host);
2413 rmp->ses_num = htonl (m->ses_num);
2414 rmp->context = context;
2416 vl_api_send_msg (reg, (u8 *) rmp);
2420 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
2422 vl_api_registration_t *reg;
2423 snat_main_t *sm = &snat_main;
2426 if (!sm->deterministic)
2429 reg = vl_api_client_index_to_registration (mp->client_index);
2434 vec_foreach(m, sm->det_maps)
2435 sent_nat_det_map_details(m, reg, mp->context);
2440 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
2444 s = format (0, "SCRIPT: nat_det_map_dump ");
2450 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
2453 snat_main_t *sm = &snat_main;
2454 vl_api_nat_det_close_session_out_reply_t *rmp;
2455 ip4_address_t out_addr, ext_addr, in_addr;
2456 snat_det_out_key_t key;
2458 snat_det_session_t *ses;
2461 if (!sm->deterministic)
2463 rv = VNET_API_ERROR_UNSUPPORTED;
2467 clib_memcpy (&out_addr, mp->out_addr, 4);
2468 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2470 dm = snat_det_map_by_out (sm, &out_addr);
2473 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2476 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
2477 key.ext_host_addr = ext_addr;
2478 key.ext_host_port = mp->ext_port;
2479 key.out_port = mp->out_port;
2480 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
2483 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2486 snat_det_ses_close (dm, ses);
2489 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2493 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
2498 s = format (0, "SCRIPT: nat_det_close_session_out ");
2499 s = format (s, "out_addr %U out_port %d "
2500 "ext_addr %U ext_port %d\n",
2501 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
2502 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2508 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
2511 snat_main_t *sm = &snat_main;
2512 vl_api_nat_det_close_session_in_reply_t *rmp;
2513 ip4_address_t in_addr, ext_addr;
2514 snat_det_out_key_t key;
2516 snat_det_session_t *ses;
2519 if (!sm->deterministic)
2521 rv = VNET_API_ERROR_UNSUPPORTED;
2525 clib_memcpy (&in_addr, mp->in_addr, 4);
2526 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2528 dm = snat_det_map_by_user (sm, &in_addr);
2531 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2534 key.ext_host_addr = ext_addr;
2535 key.ext_host_port = mp->ext_port;
2536 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
2539 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2542 snat_det_ses_close (dm, ses);
2545 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2549 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
2553 s = format (0, "SCRIPT: nat_det_close_session_in ");
2554 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
2555 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
2556 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2562 send_nat_det_session_details (snat_det_session_t * s,
2563 vl_api_registration_t * reg, u32 context)
2565 vl_api_nat_det_session_details_t *rmp;
2566 snat_main_t *sm = &snat_main;
2568 rmp = vl_msg_api_alloc (sizeof (*rmp));
2569 clib_memset (rmp, 0, sizeof (*rmp));
2570 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
2571 rmp->in_port = s->in_port;
2572 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
2573 rmp->ext_port = s->out.ext_host_port;
2574 rmp->out_port = s->out.out_port;
2575 rmp->state = s->state;
2576 rmp->expire = ntohl (s->expire);
2577 rmp->context = context;
2579 vl_api_send_msg (reg, (u8 *) rmp);
2583 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
2585 vl_api_registration_t *reg;
2586 snat_main_t *sm = &snat_main;
2587 ip4_address_t user_addr;
2589 snat_det_session_t *s, empty_ses;
2592 if (!sm->deterministic)
2595 reg = vl_api_client_index_to_registration (mp->client_index);
2599 clib_memset (&empty_ses, 0, sizeof (empty_ses));
2600 clib_memcpy (&user_addr, mp->user_addr, 4);
2601 dm = snat_det_map_by_user (sm, &user_addr);
2605 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2606 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2609 send_nat_det_session_details (s, reg, mp->context);
2615 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2620 s = format (0, "SCRIPT: nat_det_session_dump ");
2621 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2631 vl_api_nat64_add_del_pool_addr_range_t_handler
2632 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2634 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2635 snat_main_t *sm = &snat_main;
2637 ip4_address_t this_addr;
2638 u32 start_host_order, end_host_order;
2643 tmp = (u32 *) mp->start_addr;
2644 start_host_order = clib_host_to_net_u32 (tmp[0]);
2645 tmp = (u32 *) mp->end_addr;
2646 end_host_order = clib_host_to_net_u32 (tmp[0]);
2648 count = (end_host_order - start_host_order) + 1;
2650 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2652 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2654 for (i = 0; i < count; i++)
2656 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2659 increment_v4_address (&this_addr);
2663 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2666 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2667 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2671 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2672 s = format (s, "%U - %U vrf_id %u %s\n",
2673 format_ip4_address, mp->start_addr,
2674 format_ip4_address, mp->end_addr,
2675 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2680 typedef struct nat64_api_walk_ctx_t_
2682 vl_api_registration_t *reg;
2685 } nat64_api_walk_ctx_t;
2688 nat64_api_pool_walk (snat_address_t * a, void *arg)
2690 vl_api_nat64_pool_addr_details_t *rmp;
2691 snat_main_t *sm = &snat_main;
2692 nat64_api_walk_ctx_t *ctx = arg;
2694 rmp = vl_msg_api_alloc (sizeof (*rmp));
2695 clib_memset (rmp, 0, sizeof (*rmp));
2696 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2697 clib_memcpy (rmp->address, &(a->addr), 4);
2698 if (a->fib_index != ~0)
2700 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2703 rmp->vrf_id = ntohl (fib->ft_table_id);
2707 rmp->context = ctx->context;
2709 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2715 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2717 vl_api_registration_t *reg;
2719 reg = vl_api_client_index_to_registration (mp->client_index);
2723 nat64_api_walk_ctx_t ctx = {
2725 .context = mp->context,
2728 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2732 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2737 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2743 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2746 snat_main_t *sm = &snat_main;
2747 vl_api_nat64_add_del_interface_reply_t *rmp;
2750 VALIDATE_SW_IF_INDEX (mp);
2753 nat64_add_del_interface (ntohl (mp->sw_if_index),
2754 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2756 BAD_SW_IF_INDEX_LABEL;
2758 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2762 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2767 s = format (0, "SCRIPT: nat64_add_del_interface ");
2768 s = format (s, "sw_if_index %d %s %s",
2769 clib_host_to_net_u32 (mp->sw_if_index),
2770 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2771 mp->is_add ? "" : "del");
2777 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2779 vl_api_nat64_interface_details_t *rmp;
2780 snat_main_t *sm = &snat_main;
2781 nat64_api_walk_ctx_t *ctx = arg;
2783 rmp = vl_msg_api_alloc (sizeof (*rmp));
2784 clib_memset (rmp, 0, sizeof (*rmp));
2785 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2786 rmp->sw_if_index = ntohl (i->sw_if_index);
2788 if (nat_interface_is_inside (i))
2789 rmp->flags |= NAT_API_IS_INSIDE;
2790 if (nat_interface_is_outside (i))
2791 rmp->flags |= NAT_API_IS_OUTSIDE;
2793 rmp->context = ctx->context;
2795 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2801 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2803 vl_api_registration_t *reg;
2805 reg = vl_api_client_index_to_registration (mp->client_index);
2809 nat64_api_walk_ctx_t ctx = {
2811 .context = mp->context,
2814 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2818 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2823 s = format (0, "SCRIPT: snat_interface_dump ");
2829 vl_api_nat64_add_del_static_bib_t_handler
2830 (vl_api_nat64_add_del_static_bib_t * mp)
2832 snat_main_t *sm = &snat_main;
2833 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2834 ip6_address_t in_addr;
2835 ip4_address_t out_addr;
2838 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2839 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2842 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2843 clib_net_to_host_u16 (mp->i_port),
2844 clib_net_to_host_u16 (mp->o_port),
2846 clib_net_to_host_u32 (mp->vrf_id),
2849 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2852 static void *vl_api_nat64_add_del_static_bib_t_print
2853 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2857 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2858 s = format (s, "protocol %d i_addr %U o_addr %U ",
2860 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2862 if (mp->vrf_id != ~0)
2863 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2869 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2871 vl_api_nat64_bib_details_t *rmp;
2872 snat_main_t *sm = &snat_main;
2873 nat64_api_walk_ctx_t *ctx = arg;
2876 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2880 rmp = vl_msg_api_alloc (sizeof (*rmp));
2881 clib_memset (rmp, 0, sizeof (*rmp));
2882 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2883 rmp->context = ctx->context;
2884 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2885 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2886 rmp->i_port = bibe->in_port;
2887 rmp->o_port = bibe->out_port;
2888 rmp->vrf_id = ntohl (fib->ft_table_id);
2889 rmp->proto = bibe->proto;
2890 if (bibe->is_static)
2891 rmp->flags |= NAT_API_IS_STATIC;
2892 rmp->ses_num = ntohl (bibe->ses_num);
2894 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2900 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2902 vl_api_registration_t *reg;
2903 nat64_main_t *nm = &nat64_main;
2906 reg = vl_api_client_index_to_registration (mp->client_index);
2910 nat64_api_walk_ctx_t ctx = {
2912 .context = mp->context,
2916 vec_foreach (db, nm->db)
2917 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2922 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2926 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2932 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2934 vl_api_nat64_st_details_t *rmp;
2935 snat_main_t *sm = &snat_main;
2936 nat64_api_walk_ctx_t *ctx = arg;
2937 nat64_db_bib_entry_t *bibe;
2940 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2944 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2948 rmp = vl_msg_api_alloc (sizeof (*rmp));
2949 clib_memset (rmp, 0, sizeof (*rmp));
2950 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2951 rmp->context = ctx->context;
2952 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2953 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2954 rmp->il_port = bibe->in_port;
2955 rmp->ol_port = bibe->out_port;
2956 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2957 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2958 rmp->il_port = ste->r_port;
2959 rmp->vrf_id = ntohl (fib->ft_table_id);
2960 rmp->proto = ste->proto;
2962 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2968 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2970 vl_api_registration_t *reg;
2971 nat64_main_t *nm = &nat64_main;
2974 reg = vl_api_client_index_to_registration (mp->client_index);
2978 nat64_api_walk_ctx_t ctx = {
2980 .context = mp->context,
2984 vec_foreach (db, nm->db)
2987 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2993 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2997 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
3003 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
3005 vl_api_nat64_add_del_prefix_reply_t *rmp;
3006 snat_main_t *sm = &snat_main;
3007 ip6_address_t prefix;
3010 memcpy (&prefix.as_u8, mp->prefix.address, 16);
3013 nat64_add_del_prefix (&prefix, mp->prefix.len,
3014 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
3015 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
3019 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
3024 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
3025 format_ip6_address, mp->prefix.address, mp->prefix.len,
3026 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
3032 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
3034 vl_api_nat64_prefix_details_t *rmp;
3035 snat_main_t *sm = &snat_main;
3036 nat64_api_walk_ctx_t *ctx = arg;
3038 rmp = vl_msg_api_alloc (sizeof (*rmp));
3039 clib_memset (rmp, 0, sizeof (*rmp));
3040 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
3041 clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
3042 rmp->prefix.len = p->plen;
3043 rmp->vrf_id = ntohl (p->vrf_id);
3044 rmp->context = ctx->context;
3046 vl_api_send_msg (ctx->reg, (u8 *) rmp);
3052 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
3054 vl_api_registration_t *reg;
3056 reg = vl_api_client_index_to_registration (mp->client_index);
3060 nat64_api_walk_ctx_t ctx = {
3062 .context = mp->context,
3065 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
3069 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
3074 s = format (0, "SCRIPT: nat64_prefix_dump\n");
3080 vl_api_nat64_add_del_interface_addr_t_handler
3081 (vl_api_nat64_add_del_interface_addr_t * mp)
3083 snat_main_t *sm = &snat_main;
3084 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
3085 u32 sw_if_index = ntohl (mp->sw_if_index);
3088 VALIDATE_SW_IF_INDEX (mp);
3090 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
3092 BAD_SW_IF_INDEX_LABEL;
3094 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
3097 static void *vl_api_nat64_add_del_interface_addr_t_print
3098 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
3102 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
3103 s = format (s, "sw_if_index %d %s",
3104 clib_host_to_net_u32 (mp->sw_if_index),
3105 mp->is_add ? "" : "del");
3115 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
3117 vl_api_dslite_set_aftr_addr_reply_t *rmp;
3118 snat_main_t *sm = &snat_main;
3119 dslite_main_t *dm = &dslite_main;
3121 ip6_address_t ip6_addr;
3122 ip4_address_t ip4_addr;
3124 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3125 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3127 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
3129 rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
3131 REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
3135 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
3140 s = format (0, "SCRIPT: dslite_set_aftr_addr ");
3141 s = format (s, "ip6_addr %U ip4_addr %U\n",
3142 format_ip6_address, mp->ip6_addr,
3143 format_ip4_address, mp->ip4_addr);
3149 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
3151 snat_main_t *sm = &snat_main;
3152 vl_api_dslite_get_aftr_addr_reply_t *rmp;
3153 dslite_main_t *dm = &dslite_main;
3157 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
3159 memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
3160 memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
3166 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
3171 s = format (0, "SCRIPT: dslite_get_aftr_addr");
3177 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
3179 vl_api_dslite_set_b4_addr_reply_t *rmp;
3180 snat_main_t *sm = &snat_main;
3181 dslite_main_t *dm = &dslite_main;
3183 ip6_address_t ip6_addr;
3184 ip4_address_t ip4_addr;
3186 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3187 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3189 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
3191 rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
3193 REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
3197 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
3202 s = format (0, "SCRIPT: dslite_set_b4_addr ");
3203 s = format (s, "ip6_addr %U ip4_addr %U\n",
3204 format_ip6_address, mp->ip6_addr,
3205 format_ip6_address, mp->ip4_addr);
3211 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
3213 snat_main_t *sm = &snat_main;
3214 vl_api_dslite_get_b4_addr_reply_t *rmp;
3215 dslite_main_t *dm = &dslite_main;
3219 REPLY_MACRO2 (VL_API_DSLITE_GET_B4_ADDR_REPLY,
3221 memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
3222 memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
3228 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
3233 s = format (0, "SCRIPT: dslite_get_b4_addr");
3239 vl_api_dslite_add_del_pool_addr_range_t_handler
3240 (vl_api_dslite_add_del_pool_addr_range_t * mp)
3242 vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
3243 snat_main_t *sm = &snat_main;
3244 dslite_main_t *dm = &dslite_main;
3246 ip4_address_t this_addr;
3247 u32 start_host_order, end_host_order;
3251 tmp = (u32 *) mp->start_addr;
3252 start_host_order = clib_host_to_net_u32 (tmp[0]);
3253 tmp = (u32 *) mp->end_addr;
3254 end_host_order = clib_host_to_net_u32 (tmp[0]);
3256 count = (end_host_order - start_host_order) + 1;
3257 memcpy (&this_addr.as_u8, mp->start_addr, 4);
3259 for (i = 0; i < count; i++)
3261 if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
3264 increment_v4_address (&this_addr);
3268 REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
3272 send_dslite_address_details (snat_address_t * ap,
3273 vl_api_registration_t * reg, u32 context)
3275 vl_api_dslite_address_details_t *rmp;
3276 snat_main_t *sm = &snat_main;
3278 rmp = vl_msg_api_alloc (sizeof (*rmp));
3280 clib_memset (rmp, 0, sizeof (*rmp));
3282 rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
3283 clib_memcpy (rmp->ip_address, &(ap->addr), 4);
3284 rmp->context = context;
3286 vl_api_send_msg (reg, (u8 *) rmp);
3290 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
3292 vl_api_registration_t *reg;
3293 dslite_main_t *dm = &dslite_main;
3296 reg = vl_api_client_index_to_registration (mp->client_index);
3301 vec_foreach (ap, dm->addr_pool)
3303 send_dslite_address_details (ap, reg, mp->context);
3309 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
3314 s = format (0, "SCRIPT: dslite_address_dump ");
3319 static void *vl_api_dslite_add_del_pool_addr_range_t_print
3320 (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
3324 s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
3325 s = format (s, "%U - %U\n",
3326 format_ip4_address, mp->start_addr,
3327 format_ip4_address, mp->end_addr);
3338 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
3341 snat_main_t *sm = &snat_main;
3342 vl_api_nat66_add_del_interface_reply_t *rmp;
3345 VALIDATE_SW_IF_INDEX (mp);
3348 nat66_interface_add_del (ntohl (mp->sw_if_index),
3349 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
3351 BAD_SW_IF_INDEX_LABEL;
3353 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
3357 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
3362 s = format (0, "SCRIPT: nat66_add_del_interface ");
3363 s = format (s, "sw_if_index %d %s %s",
3364 clib_host_to_net_u32 (mp->sw_if_index),
3365 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
3366 mp->is_add ? "" : "del");
3372 vl_api_nat66_add_del_static_mapping_t_handler
3373 (vl_api_nat66_add_del_static_mapping_t * mp)
3375 snat_main_t *sm = &snat_main;
3376 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
3377 ip6_address_t l_addr, e_addr;
3380 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
3381 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
3384 nat66_static_mapping_add_del (&l_addr, &e_addr,
3385 clib_net_to_host_u32 (mp->vrf_id),
3388 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
3391 static void *vl_api_nat66_add_del_static_mapping_t_print
3392 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
3396 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
3397 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
3398 format_ip6_address, mp->local_ip_address,
3399 format_ip6_address, mp->external_ip_address,
3400 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
3405 typedef struct nat66_api_walk_ctx_t_
3407 vl_api_registration_t *rp;
3409 } nat66_api_walk_ctx_t;
3412 nat66_api_interface_walk (snat_interface_t * i, void *arg)
3414 vl_api_nat66_interface_details_t *rmp;
3415 snat_main_t *sm = &snat_main;
3416 nat66_api_walk_ctx_t *ctx = arg;
3418 rmp = vl_msg_api_alloc (sizeof (*rmp));
3419 clib_memset (rmp, 0, sizeof (*rmp));
3420 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
3421 rmp->sw_if_index = ntohl (i->sw_if_index);
3422 if (nat_interface_is_inside (i))
3423 rmp->flags |= NAT_API_IS_INSIDE;
3424 rmp->context = ctx->context;
3426 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3432 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
3434 vl_api_registration_t *rp;
3436 rp = vl_api_client_index_to_registration (mp->client_index);
3440 nat66_api_walk_ctx_t ctx = {
3442 .context = mp->context,
3445 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
3449 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
3454 s = format (0, "SCRIPT: nat66_interface_dump ");
3460 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
3462 vl_api_nat66_static_mapping_details_t *rmp;
3463 nat66_main_t *nm = &nat66_main;
3464 snat_main_t *sm = &snat_main;
3465 nat66_api_walk_ctx_t *ctx = arg;
3469 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
3473 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
3475 rmp = vl_msg_api_alloc (sizeof (*rmp));
3476 clib_memset (rmp, 0, sizeof (*rmp));
3478 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
3479 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
3480 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
3481 rmp->vrf_id = ntohl (fib->ft_table_id);
3482 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
3483 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
3484 rmp->context = ctx->context;
3486 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3492 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
3495 vl_api_registration_t *rp;
3497 rp = vl_api_client_index_to_registration (mp->client_index);
3501 nat66_api_walk_ctx_t ctx = {
3503 .context = mp->context,
3506 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
3510 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
3515 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
3521 /* List of message types that this plugin understands */
3522 #define foreach_snat_plugin_api_msg \
3523 _(NAT_CONTROL_PING, nat_control_ping) \
3524 _(NAT_SHOW_CONFIG, nat_show_config) \
3525 _(NAT_SET_WORKERS, nat_set_workers) \
3526 _(NAT_WORKER_DUMP, nat_worker_dump) \
3527 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
3528 _(NAT_SET_REASS, nat_set_reass) \
3529 _(NAT_GET_REASS, nat_get_reass) \
3530 _(NAT_REASS_DUMP, nat_reass_dump) \
3531 _(NAT_SET_TIMEOUTS, nat_set_timeouts) \
3532 _(NAT_GET_TIMEOUTS, nat_get_timeouts) \
3533 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg) \
3534 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg) \
3535 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping) \
3536 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping) \
3537 _(NAT_HA_SET_LISTENER, nat_ha_set_listener) \
3538 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover) \
3539 _(NAT_HA_GET_LISTENER, nat_ha_get_listener) \
3540 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover) \
3541 _(NAT_HA_FLUSH, nat_ha_flush) \
3542 _(NAT_HA_RESYNC, nat_ha_resync) \
3543 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
3544 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
3545 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
3546 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3547 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3548 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3549 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3550 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3551 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3552 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3553 _(NAT44_USER_DUMP, nat44_user_dump) \
3554 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3555 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3556 nat44_interface_add_del_output_feature) \
3557 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3558 nat44_interface_output_feature_dump) \
3559 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3560 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL, \
3561 nat44_lb_static_mapping_add_del_local) \
3562 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3563 _(NAT44_DEL_SESSION, nat44_del_session) \
3564 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3565 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3566 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3567 _(NAT_DET_FORWARD, nat_det_forward) \
3568 _(NAT_DET_REVERSE, nat_det_reverse) \
3569 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3570 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3571 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3572 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3573 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3574 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3575 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3576 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3577 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3578 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3579 _(NAT64_ST_DUMP, nat64_st_dump) \
3580 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3581 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3582 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3583 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range) \
3584 _(DSLITE_ADDRESS_DUMP, dslite_address_dump) \
3585 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr) \
3586 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr) \
3587 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr) \
3588 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr) \
3589 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3590 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3591 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3592 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3594 /* Set up the API message handling tables */
3595 static clib_error_t *
3596 snat_plugin_api_hookup (vlib_main_t * vm)
3598 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3600 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3602 vl_api_##n##_t_handler, \
3604 vl_api_##n##_t_endian, \
3605 vl_api_##n##_t_print, \
3606 sizeof(vl_api_##n##_t), 1);
3607 foreach_snat_plugin_api_msg;
3613 #define vl_msg_name_crc_list
3614 #include <nat/nat_all_api_h.h>
3615 #undef vl_msg_name_crc_list
3618 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3620 #define _(id,n,crc) \
3621 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3622 foreach_vl_msg_name_crc_nat;
3627 plugin_custom_dump_configure (snat_main_t * sm)
3629 #define _(n,f) sm->api_main->msg_print_handlers \
3630 [VL_API_##n + sm->msg_id_base] \
3631 = (void *) vl_api_##f##_t_print;
3632 foreach_snat_plugin_api_msg;
3637 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3640 clib_error_t *error = 0;
3642 name = format (0, "nat_%08x%c", api_version, 0);
3644 /* Ask for a correctly-sized block of API message decode slots */
3646 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3648 error = snat_plugin_api_hookup (vm);
3650 /* Add our API messages to the global name_crc hash table */
3651 setup_message_id_table (sm, sm->api_main);
3653 plugin_custom_dump_configure (sm);
3661 * fd.io coding-style-patch-verification: ON
3664 * eval: (c-set-style "gnu")