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>
35 #include <vlibapi/api_types_inlines.h>
37 #define vl_api_nat44_lb_addr_port_t_endian vl_noop_handler
38 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
39 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
41 /* define message structures */
43 #include <nat/nat_all_api_h.h>
46 /* define generated endian-swappers */
48 #include <nat/nat_all_api_h.h>
51 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
53 #define REPLY_MSG_ID_BASE sm->msg_id_base
54 #include <vlibapi/api_helper_macros.h>
56 /* Get the API version number */
57 #define vl_api_version(n,v) static u32 api_version=(v);
58 #include <nat/nat_all_api_h.h>
61 /* Macro to finish up custom dump fns */
64 vl_print (handle, (char *)s); \
68 /******************************/
69 /*** Common NAT plugin APIs ***/
70 /******************************/
73 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
75 vl_api_nat_control_ping_reply_t *rmp;
76 snat_main_t *sm = &snat_main;
80 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
82 rmp->vpe_pid = ntohl (getpid ());
88 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
92 s = format (0, "SCRIPT: nat_control_ping ");
98 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
100 vl_api_nat_show_config_reply_t *rmp;
101 snat_main_t *sm = &snat_main;
102 dslite_main_t *dm = &dslite_main;
103 nat64_main_t *n64m = &nat64_main;
107 REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
109 rmp->translation_buckets = htonl (sm->translation_buckets);
110 rmp->translation_memory_size = htonl (sm->translation_memory_size);
111 rmp->user_buckets = htonl (sm->user_buckets);
112 rmp->user_memory_size = htonl (sm->user_memory_size);
113 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
114 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
115 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
116 rmp->static_mapping_only = sm->static_mapping_only;
117 rmp->static_mapping_connection_tracking =
118 sm->static_mapping_connection_tracking;
119 rmp->deterministic = sm->deterministic;
120 rmp->endpoint_dependent = sm->endpoint_dependent;
121 rmp->out2in_dpo = sm->out2in_dpo;
122 rmp->dslite_ce = dm->is_ce;
123 rmp->nat64_bib_buckets = n64m->bib_buckets;
124 rmp->nat64_bib_memory_size = n64m->bib_memory_size;
125 rmp->nat64_st_buckets = n64m->st_buckets;
126 rmp->nat64_st_memory_size = n64m->st_memory_size;
132 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
136 s = format (0, "SCRIPT: nat_show_config ");
142 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
144 snat_main_t *sm = &snat_main;
145 vl_api_nat_set_workers_reply_t *rmp;
150 if (sm->deterministic)
152 rv = VNET_API_ERROR_UNSUPPORTED;
156 mask = clib_net_to_host_u64 (mp->worker_mask);
158 if (sm->num_workers < 2)
160 rv = VNET_API_ERROR_FEATURE_DISABLED;
164 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
165 rv = snat_set_workers (bitmap);
166 clib_bitmap_free (bitmap);
169 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
173 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
179 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
181 s = format (0, "SCRIPT: nat_set_workers ");
182 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
184 clib_bitmap_foreach (i, bitmap,
187 s = format (s, "%d", i);
189 s = format (s, ",%d", i);
193 clib_bitmap_free (bitmap);
198 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
201 vl_api_nat_worker_details_t *rmp;
202 snat_main_t *sm = &snat_main;
203 vlib_worker_thread_t *w =
204 vlib_worker_threads + worker_index + sm->first_worker_index;
205 u32 len = vec_len (w->name);
210 rmp = vl_msg_api_alloc (sizeof (*rmp) + len);
211 clib_memset (rmp, 0, sizeof (*rmp) + len);
212 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
213 rmp->context = context;
214 rmp->worker_index = htonl (worker_index);
215 rmp->lcore_id = htonl (w->cpu_id);
218 vl_api_to_api_string (len, (char *) w->name, &rmp->name);
220 vl_api_send_msg (reg, (u8 *) rmp);
224 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
226 vl_api_registration_t *reg;
227 snat_main_t *sm = &snat_main;
230 if (sm->deterministic)
233 reg = vl_api_client_index_to_registration (mp->client_index);
238 vec_foreach (worker_index, sm->workers)
239 send_nat_worker_details(*worker_index, reg, mp->context);
244 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
248 s = format (0, "SCRIPT: nat_worker_dump ");
254 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
257 snat_main_t *sm = &snat_main;
258 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
261 rv = snat_ipfix_logging_enable_disable (mp->enable,
267 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
271 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
276 s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
278 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
280 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
282 s = format (s, "disable ");
288 vl_api_nat_set_reass_t_handler (vl_api_nat_set_reass_t * mp)
290 snat_main_t *sm = &snat_main;
291 vl_api_nat_set_reass_reply_t *rmp;
295 nat_reass_set (ntohl (mp->timeout), ntohs (mp->max_reass), mp->max_frag,
296 mp->drop_frag, mp->is_ip6);
298 REPLY_MACRO (VL_API_NAT_SET_REASS_REPLY);
302 vl_api_nat_set_reass_t_print (vl_api_nat_set_reass_t * mp, void *handle)
306 s = format (0, "SCRIPT: nat_set_reass ");
307 s = format (s, "timeout %d max_reass %d max_frag %d drop_frag %d is_ip6 %d",
308 clib_host_to_net_u32 (mp->timeout),
309 clib_host_to_net_u16 (mp->max_reass),
310 mp->max_frag, mp->drop_frag, mp->is_ip6);
316 vl_api_nat_get_reass_t_handler (vl_api_nat_get_reass_t * mp)
318 snat_main_t *sm = &snat_main;
319 vl_api_nat_get_reass_reply_t *rmp;
323 REPLY_MACRO2 (VL_API_NAT_GET_REASS_REPLY,
325 rmp->ip4_timeout = htonl (nat_reass_get_timeout(0));
326 rmp->ip4_max_reass = htons (nat_reass_get_max_reass(0));
327 rmp->ip4_max_frag = nat_reass_get_max_frag(0);
328 rmp->ip4_drop_frag = nat_reass_is_drop_frag(0);
329 rmp->ip6_timeout = htonl (nat_reass_get_timeout(1));
330 rmp->ip6_max_reass = htons (nat_reass_get_max_reass(1));
331 rmp->ip6_max_frag = nat_reass_get_max_frag(1);
332 rmp->ip6_drop_frag = nat_reass_is_drop_frag(1);
338 vl_api_nat_get_reass_t_print (vl_api_nat_get_reass_t * mp, void *handle)
342 s = format (0, "SCRIPT: nat_get_reass");
347 typedef struct nat_api_walk_ctx_t_
349 vl_api_registration_t *reg;
351 } nat_api_walk_ctx_t;
354 nat_ip4_reass_walk_api (nat_reass_ip4_t * reass, void *arg)
356 vl_api_nat_reass_details_t *rmp;
357 snat_main_t *sm = &snat_main;
358 nat_api_walk_ctx_t *ctx = arg;
359 ip46_address_t ip_address;
361 rmp = vl_msg_api_alloc (sizeof (*rmp));
362 clib_memset (rmp, 0, sizeof (*rmp));
363 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
364 rmp->context = ctx->context;
366 clib_memcpy (&ip_address.ip4, &reass->key.src, 4);
367 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->src_addr);
369 clib_memcpy (&ip_address.ip4, &reass->key.dst, 4);
370 ip_address_encode (&ip_address, IP46_TYPE_IP4, &rmp->dst_addr);
372 rmp->proto = reass->key.proto;
373 rmp->frag_id = ntohl (reass->key.frag_id);
374 rmp->frag_n = reass->frag_n;
376 vl_api_send_msg (ctx->reg, (u8 *) rmp);
382 nat_ip6_reass_walk_api (nat_reass_ip6_t * reass, void *arg)
384 vl_api_nat_reass_details_t *rmp;
385 snat_main_t *sm = &snat_main;
386 nat_api_walk_ctx_t *ctx = arg;
387 ip46_address_t ip_address;
389 rmp = vl_msg_api_alloc (sizeof (*rmp));
390 clib_memset (rmp, 0, sizeof (*rmp));
391 rmp->_vl_msg_id = ntohs (VL_API_NAT_REASS_DETAILS + sm->msg_id_base);
392 rmp->context = ctx->context;
394 clib_memcpy (&ip_address.ip6, &reass->key.src, 16);
395 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->src_addr);
397 clib_memcpy (&ip_address.ip6, &reass->key.dst, 16);
398 ip_address_encode (&ip_address, IP46_TYPE_IP6, &rmp->dst_addr);
400 rmp->proto = reass->key.proto;
401 rmp->frag_id = ntohl (reass->key.frag_id);
402 rmp->frag_n = reass->frag_n;
404 vl_api_send_msg (ctx->reg, (u8 *) rmp);
410 vl_api_nat_reass_dump_t_handler (vl_api_nat_reass_dump_t * mp)
412 vl_api_registration_t *reg;
414 reg = vl_api_client_index_to_registration (mp->client_index);
418 nat_api_walk_ctx_t ctx = {
420 .context = mp->context,
423 nat_ip4_reass_walk (nat_ip4_reass_walk_api, &ctx);
424 nat_ip6_reass_walk (nat_ip6_reass_walk_api, &ctx);
428 vl_api_nat_reass_dump_t_print (vl_api_nat_reass_dump_t * mp, void *handle)
432 s = format (0, "SCRIPT: nat_reass_dump");
438 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
440 snat_main_t *sm = &snat_main;
441 vl_api_nat_set_timeouts_reply_t *rmp;
444 sm->udp_timeout = ntohl (mp->udp);
445 sm->tcp_established_timeout = ntohl (mp->tcp_established);
446 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
447 sm->icmp_timeout = ntohl (mp->icmp);
449 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
452 rv = nat64_set_udp_timeout (ntohl (mp->udp));
456 nat64_set_tcp_timeouts (ntohl (mp->tcp_transitory),
457 ntohl (mp->tcp_established));
460 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
464 vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle)
468 s = format (0, "SCRIPT: nat_set_timeouts ");
469 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
471 ntohl (mp->tcp_established),
472 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
478 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
480 snat_main_t *sm = &snat_main;
481 vl_api_nat_get_timeouts_reply_t *rmp;
485 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
487 rmp->udp = htonl (sm->udp_timeout);
488 rmp->tcp_established = htonl (sm->tcp_established_timeout);
489 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
490 rmp->icmp = htonl (sm->icmp_timeout);
496 vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle)
500 s = format (0, "SCRIPT: nat_get_timeouts");
506 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
507 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
509 snat_main_t *sm = &snat_main;
510 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
512 u16 port_start, port_end;
514 if (sm->deterministic)
516 rv = VNET_API_ERROR_UNSUPPORTED;
522 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
523 nat_set_alloc_addr_and_port_default ();
525 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
526 nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
529 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
530 port_start = ntohs (mp->start_port);
531 port_end = ntohs (mp->end_port);
532 if (port_end <= port_start)
534 rv = VNET_API_ERROR_INVALID_VALUE;
537 nat_set_alloc_addr_and_port_range (port_start, port_end);
540 rv = VNET_API_ERROR_INVALID_VALUE;
545 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
548 static void *vl_api_nat_set_addr_and_port_alloc_alg_t_print
549 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp, void *handle)
553 s = format (0, "SCRIPT: nat_set_addr_and_port_alloc_alg ");
554 s = format (s, "alg %d psid_offset %d psid_length %d psid %d start_port %d "
556 ntohl (mp->alg), ntohl (mp->psid_offset),
557 ntohl (mp->psid_length), ntohs (mp->psid),
558 ntohs (mp->start_port), ntohs (mp->end_port));
564 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
565 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
567 snat_main_t *sm = &snat_main;
568 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
572 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
574 rmp->alg = sm->addr_and_port_alloc_alg;
575 rmp->psid_offset = sm->psid_offset;
576 rmp->psid_length = sm->psid_length;
577 rmp->psid = htons (sm->psid);
578 rmp->start_port = htons (sm->start_port);
579 rmp->end_port = htons (sm->end_port);
584 static void *vl_api_nat_get_addr_and_port_alloc_alg_t_print
585 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp, void *handle)
589 s = format (0, "SCRIPT: nat_get_addr_and_port_alloc_alg");
595 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
597 snat_main_t *sm = &snat_main;
598 vl_api_nat_set_mss_clamping_reply_t *rmp;
603 sm->mss_clamping = ntohs (mp->mss_value);
604 sm->mss_value_net = mp->mss_value;
607 sm->mss_clamping = 0;
609 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
613 vl_api_nat_set_mss_clamping_t_print (vl_api_nat_set_mss_clamping_t * mp,
618 s = format (0, "SCRIPT: nat_set_mss_clamping enable %d mss_value %d\n",
619 mp->enable, ntohs (mp->mss_value));
625 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
627 snat_main_t *sm = &snat_main;
628 vl_api_nat_get_mss_clamping_reply_t *rmp;
632 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
634 rmp->enable = sm->mss_clamping ? 1 : 0;
635 rmp->mss_value = htons (sm->mss_clamping);
641 vl_api_nat_get_mss_clamping_t_print (vl_api_nat_get_mss_clamping_t * mp,
646 s = format (0, "SCRIPT: nat_get_mss_clamping");
652 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
654 snat_main_t *sm = &snat_main;
655 vl_api_nat_ha_set_listener_reply_t *rmp;
659 memcpy (&addr, &mp->ip_address, sizeof (addr));
661 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
662 clib_net_to_host_u32 (mp->path_mtu));
664 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
668 vl_api_nat_ha_set_listener_t_print (vl_api_nat_ha_set_listener_t * mp,
673 s = format (0, "SCRIPT: nat_ha_set_listener ");
674 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
675 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
676 s = format (s, "path_mtu %d", clib_net_to_host_u32 (mp->path_mtu));
682 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
684 snat_main_t *sm = &snat_main;
685 vl_api_nat_ha_get_listener_reply_t *rmp;
691 nat_ha_get_listener (&addr, &port, &path_mtu);
694 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
696 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
697 rmp->port = clib_host_to_net_u16 (port);
698 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
704 vl_api_nat_ha_get_listener_t_print (vl_api_nat_ha_get_listener_t * mp,
709 s = format (0, "SCRIPT: nat_ha_get_listener");
715 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
717 snat_main_t *sm = &snat_main;
718 vl_api_nat_ha_set_failover_reply_t *rmp;
722 memcpy (&addr, &mp->ip_address, sizeof (addr));
724 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
725 clib_net_to_host_u32 (mp->session_refresh_interval));
727 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
731 vl_api_nat_ha_set_failover_t_print (vl_api_nat_ha_set_failover_t * mp,
736 s = format (0, "SCRIPT: nat_ha_set_failover ");
737 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
738 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
744 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
746 snat_main_t *sm = &snat_main;
747 vl_api_nat_ha_get_failover_reply_t *rmp;
751 u32 session_refresh_interval;
753 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
756 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
758 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
759 rmp->port = clib_host_to_net_u16 (port);
760 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
766 vl_api_nat_ha_get_failover_t_print (vl_api_nat_ha_get_failover_t * mp,
771 s = format (0, "SCRIPT: nat_ha_get_failover");
777 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
779 snat_main_t *sm = &snat_main;
780 vl_api_nat_ha_flush_reply_t *rmp;
785 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
789 vl_api_nat_ha_flush_t_print (vl_api_nat_ha_flush_t * mp, void *handle)
793 s = format (0, "SCRIPT: nat_ha_flush ");
799 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
801 snat_main_t *sm = &snat_main;
802 vl_api_registration_t *reg;
803 vl_api_nat_ha_resync_completed_event_t *mp;
805 reg = vl_api_client_index_to_registration (client_index);
809 mp = vl_msg_api_alloc (sizeof (*mp));
810 clib_memset (mp, 0, sizeof (*mp));
811 mp->client_index = client_index;
813 mp->missed_count = clib_host_to_net_u32 (missed_count);
815 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
817 vl_api_send_msg (reg, (u8 *) mp);
821 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
823 snat_main_t *sm = &snat_main;
824 vl_api_nat_ha_resync_reply_t *rmp;
828 nat_ha_resync (mp->client_index, mp->pid,
829 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
832 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
836 vl_api_nat_ha_resync_t_print (vl_api_nat_ha_resync_t * mp, void *handle)
840 s = format (0, "SCRIPT: nat_ha_resync ");
842 format (s, "want_resync_event %d pid %d", mp->want_resync_event,
843 clib_host_to_net_u32 (mp->pid));
852 vl_api_nat44_add_del_address_range_t_handler
853 (vl_api_nat44_add_del_address_range_t * mp)
855 snat_main_t *sm = &snat_main;
856 vl_api_nat44_add_del_address_range_reply_t *rmp;
857 ip4_address_t this_addr;
858 u8 is_add, twice_nat;
859 u32 start_host_order, end_host_order;
865 if (sm->deterministic)
867 rv = VNET_API_ERROR_UNSUPPORTED;
871 if (sm->static_mapping_only)
873 rv = VNET_API_ERROR_FEATURE_DISABLED;
878 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
880 tmp = (u32 *) mp->first_ip_address;
881 start_host_order = clib_host_to_net_u32 (tmp[0]);
882 tmp = (u32 *) mp->last_ip_address;
883 end_host_order = clib_host_to_net_u32 (tmp[0]);
885 count = (end_host_order - start_host_order) + 1;
887 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
890 nat_log_info ("%U - %U, %d addresses...",
891 format_ip4_address, mp->first_ip_address,
892 format_ip4_address, mp->last_ip_address, count);
894 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
896 for (i = 0; i < count; i++)
899 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
901 rv = snat_del_address (sm, this_addr, 0, twice_nat);
907 nat44_add_del_address_dpo (this_addr, is_add);
909 increment_v4_address (&this_addr);
913 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
916 static void *vl_api_nat44_add_del_address_range_t_print
917 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
921 s = format (0, "SCRIPT: nat44_add_address_range ");
922 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
923 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
925 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
927 s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
932 send_nat44_address_details (snat_address_t * a,
933 vl_api_registration_t * reg, u32 context,
936 vl_api_nat44_address_details_t *rmp;
937 snat_main_t *sm = &snat_main;
939 rmp = vl_msg_api_alloc (sizeof (*rmp));
940 clib_memset (rmp, 0, sizeof (*rmp));
941 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
942 clib_memcpy (rmp->ip_address, &(a->addr), 4);
943 if (a->fib_index != ~0)
945 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
946 rmp->vrf_id = ntohl (fib->ft_table_id);
951 rmp->flags |= NAT_API_IS_TWICE_NAT;
952 rmp->context = context;
954 vl_api_send_msg (reg, (u8 *) rmp);
958 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
960 vl_api_registration_t *reg;
961 snat_main_t *sm = &snat_main;
964 if (sm->deterministic)
967 reg = vl_api_client_index_to_registration (mp->client_index);
972 vec_foreach (a, sm->addresses)
973 send_nat44_address_details (a, reg, mp->context, 0);
974 vec_foreach (a, sm->twice_nat_addresses)
975 send_nat44_address_details (a, reg, mp->context, 1);
980 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
985 s = format (0, "SCRIPT: nat44_address_dump ");
991 vl_api_nat44_interface_add_del_feature_t_handler
992 (vl_api_nat44_interface_add_del_feature_t * mp)
994 snat_main_t *sm = &snat_main;
995 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
996 u32 sw_if_index = ntohl (mp->sw_if_index);
1000 is_del = !mp->is_add;
1002 VALIDATE_SW_IF_INDEX (mp);
1005 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
1008 BAD_SW_IF_INDEX_LABEL;
1010 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
1013 static void *vl_api_nat44_interface_add_del_feature_t_print
1014 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
1018 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
1019 s = format (s, "sw_if_index %d %s %s",
1020 clib_host_to_net_u32 (mp->sw_if_index),
1021 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1022 mp->is_add ? "" : "del");
1028 send_nat44_interface_details (snat_interface_t * i,
1029 vl_api_registration_t * reg, u32 context)
1031 vl_api_nat44_interface_details_t *rmp;
1032 snat_main_t *sm = &snat_main;
1034 rmp = vl_msg_api_alloc (sizeof (*rmp));
1035 clib_memset (rmp, 0, sizeof (*rmp));
1036 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
1037 rmp->sw_if_index = ntohl (i->sw_if_index);
1039 if (nat_interface_is_inside (i))
1040 rmp->flags |= NAT_API_IS_INSIDE;
1041 if (nat_interface_is_outside (i))
1042 rmp->flags |= NAT_API_IS_OUTSIDE;
1044 rmp->context = context;
1046 vl_api_send_msg (reg, (u8 *) rmp);
1050 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
1052 vl_api_registration_t *reg;
1053 snat_main_t *sm = &snat_main;
1054 snat_interface_t *i;
1056 reg = vl_api_client_index_to_registration (mp->client_index);
1061 pool_foreach (i, sm->interfaces,
1063 send_nat44_interface_details(i, reg, mp->context);
1069 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
1074 s = format (0, "SCRIPT: nat44_interface_dump ");
1080 vl_api_nat44_interface_add_del_output_feature_t_handler
1081 (vl_api_nat44_interface_add_del_output_feature_t * mp)
1083 snat_main_t *sm = &snat_main;
1084 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
1085 u32 sw_if_index = ntohl (mp->sw_if_index);
1088 if (sm->deterministic)
1090 rv = VNET_API_ERROR_UNSUPPORTED;
1094 VALIDATE_SW_IF_INDEX (mp);
1096 rv = snat_interface_add_del_output_feature (sw_if_index,
1097 mp->flags & NAT_API_IS_INSIDE,
1100 BAD_SW_IF_INDEX_LABEL;
1102 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
1105 static void *vl_api_nat44_interface_add_del_output_feature_t_print
1106 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
1110 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
1111 s = format (s, "sw_if_index %d %s %s",
1112 clib_host_to_net_u32 (mp->sw_if_index),
1113 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1114 mp->is_add ? "" : "del");
1120 send_nat44_interface_output_feature_details (snat_interface_t * i,
1121 vl_api_registration_t * reg,
1124 vl_api_nat44_interface_output_feature_details_t *rmp;
1125 snat_main_t *sm = &snat_main;
1127 rmp = vl_msg_api_alloc (sizeof (*rmp));
1128 clib_memset (rmp, 0, sizeof (*rmp));
1130 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
1131 rmp->sw_if_index = ntohl (i->sw_if_index);
1132 rmp->context = context;
1134 if (nat_interface_is_inside (i))
1135 rmp->flags |= NAT_API_IS_INSIDE;
1137 vl_api_send_msg (reg, (u8 *) rmp);
1141 vl_api_nat44_interface_output_feature_dump_t_handler
1142 (vl_api_nat44_interface_output_feature_dump_t * mp)
1144 vl_api_registration_t *reg;
1145 snat_main_t *sm = &snat_main;
1146 snat_interface_t *i;
1148 if (sm->deterministic)
1151 reg = vl_api_client_index_to_registration (mp->client_index);
1156 pool_foreach (i, sm->output_feature_interfaces,
1158 send_nat44_interface_output_feature_details(i, reg, mp->context);
1163 static void *vl_api_nat44_interface_output_feature_dump_t_print
1164 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1168 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1174 vl_api_nat44_add_del_static_mapping_t_handler
1175 (vl_api_nat44_add_del_static_mapping_t * mp)
1177 snat_main_t *sm = &snat_main;
1178 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1179 ip4_address_t local_addr, external_addr;
1180 u16 local_port = 0, external_port = 0;
1181 u32 vrf_id, external_sw_if_index;
1182 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1184 snat_protocol_t proto;
1188 if (sm->deterministic)
1190 rv = VNET_API_ERROR_UNSUPPORTED;
1194 len = vl_api_string_len (&mp->tag);
1198 rv = VNET_API_ERROR_INVALID_VALUE;
1202 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1203 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1205 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1207 local_port = clib_net_to_host_u16 (mp->local_port);
1208 external_port = clib_net_to_host_u16 (mp->external_port);
1211 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1212 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1213 proto = ip_proto_to_snat_proto (mp->protocol);
1215 if (mp->flags & NAT_API_IS_TWICE_NAT)
1216 twice_nat = TWICE_NAT;
1217 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1218 twice_nat = TWICE_NAT_SELF;
1220 tag = vec_new (u8, len);
1222 memcpy (tag, mp->tag.buf, len);
1223 vec_terminate_c_string (tag);
1225 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1226 external_port, vrf_id,
1227 mp->flags & NAT_API_IS_ADDR_ONLY,
1228 external_sw_if_index, proto,
1229 mp->is_add, twice_nat,
1230 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0);
1234 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1237 static void *vl_api_nat44_add_del_static_mapping_t_print
1238 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1242 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1243 s = format (s, "protocol %d local_addr %U external_addr %U ",
1245 format_ip4_address, mp->local_ip_address,
1246 format_ip4_address, mp->external_ip_address);
1248 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1249 s = format (s, "local_port %d external_port %d ",
1250 clib_net_to_host_u16 (mp->local_port),
1251 clib_net_to_host_u16 (mp->external_port));
1253 s = format (s, "twice_nat %d out2in_only %d ",
1254 mp->flags & NAT_API_IS_TWICE_NAT,
1255 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1257 if (mp->vrf_id != ~0)
1258 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1260 if (mp->external_sw_if_index != ~0)
1261 s = format (s, "external_sw_if_index %d",
1262 clib_net_to_host_u32 (mp->external_sw_if_index));
1267 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1268 vl_api_registration_t * reg, u32 context)
1270 vl_api_nat44_static_mapping_details_t *rmp;
1271 snat_main_t *sm = &snat_main;
1272 u32 len = sizeof (*rmp);
1276 len += vec_len (m->tag);
1277 rmp = vl_msg_api_alloc (len);
1278 clib_memset (rmp, 0, len);
1279 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1283 rmp = vl_msg_api_alloc (len);
1284 clib_memset (rmp, 0, len);
1288 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1290 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1291 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1292 rmp->external_sw_if_index = ~0;
1293 rmp->vrf_id = htonl (m->vrf_id);
1294 rmp->context = context;
1296 if (m->twice_nat == TWICE_NAT)
1297 rmp->flags |= NAT_API_IS_TWICE_NAT;
1298 else if (m->twice_nat == TWICE_NAT_SELF)
1299 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1301 if (is_out2in_only_static_mapping (m))
1302 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1304 if (is_addr_only_static_mapping (m))
1306 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1310 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1311 rmp->external_port = htons (m->external_port);
1312 rmp->local_port = htons (m->local_port);
1315 vl_api_send_msg (reg, (u8 *) rmp);
1319 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1320 vl_api_registration_t * reg,
1323 vl_api_nat44_static_mapping_details_t *rmp;
1324 snat_main_t *sm = &snat_main;
1325 u32 len = sizeof (*rmp);
1329 len += vec_len (m->tag);
1330 rmp = vl_msg_api_alloc (len);
1331 clib_memset (rmp, 0, len);
1332 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1336 rmp = vl_msg_api_alloc (len);
1337 clib_memset (rmp, 0, len);
1341 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1342 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1343 rmp->external_sw_if_index = htonl (m->sw_if_index);
1344 rmp->vrf_id = htonl (m->vrf_id);
1345 rmp->context = context;
1348 rmp->flags |= NAT_API_IS_TWICE_NAT;
1352 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1356 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1357 rmp->external_port = htons (m->e_port);
1358 rmp->local_port = htons (m->l_port);
1361 vl_api_send_msg (reg, (u8 *) rmp);
1365 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1368 vl_api_registration_t *reg;
1369 snat_main_t *sm = &snat_main;
1370 snat_static_mapping_t *m;
1371 snat_static_map_resolve_t *rp;
1374 if (sm->deterministic)
1377 reg = vl_api_client_index_to_registration (mp->client_index);
1382 pool_foreach (m, sm->static_mappings,
1384 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1385 send_nat44_static_mapping_details (m, reg, mp->context);
1389 for (j = 0; j < vec_len (sm->to_resolve); j++)
1391 rp = sm->to_resolve + j;
1392 if (!rp->identity_nat)
1393 send_nat44_static_map_resolve_details (rp, reg, mp->context);
1398 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1403 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1409 vl_api_nat44_add_del_identity_mapping_t_handler
1410 (vl_api_nat44_add_del_identity_mapping_t * mp)
1412 snat_main_t *sm = &snat_main;
1413 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1416 u32 vrf_id, sw_if_index;
1418 snat_protocol_t proto = ~0;
1422 if (sm->deterministic)
1424 rv = VNET_API_ERROR_UNSUPPORTED;
1428 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1430 port = clib_net_to_host_u16 (mp->port);
1431 proto = ip_proto_to_snat_proto (mp->protocol);
1433 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1434 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1435 if (sw_if_index != ~0)
1438 memcpy (&addr.as_u8, mp->ip_address, 4);
1440 len = vl_api_string_len (&mp->tag);
1442 tag = vec_new (u8, len);
1443 memcpy (tag, mp->tag.buf, len);
1444 vec_terminate_c_string (tag);
1447 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1448 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1449 proto, mp->is_add, 0, 0, tag, 1);
1453 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1456 static void *vl_api_nat44_add_del_identity_mapping_t_print
1457 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1461 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1462 if (mp->sw_if_index != ~0)
1463 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1465 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1467 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1469 format (s, " protocol %d port %d", mp->protocol,
1470 clib_net_to_host_u16 (mp->port));
1472 if (mp->vrf_id != ~0)
1473 s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1479 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1480 vl_api_registration_t * reg, u32 context)
1482 vl_api_nat44_identity_mapping_details_t *rmp;
1483 snat_main_t *sm = &snat_main;
1484 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1485 u32 len = sizeof (*rmp);
1489 len += vec_len (m->tag);
1490 rmp = vl_msg_api_alloc (len);
1491 clib_memset (rmp, 0, len);
1492 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1496 rmp = vl_msg_api_alloc (len);
1497 clib_memset (rmp, 0, len);
1501 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1503 if (is_addr_only_static_mapping (m))
1504 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1506 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1507 rmp->port = htons (m->local_port);
1508 rmp->sw_if_index = ~0;
1509 rmp->vrf_id = htonl (local->vrf_id);
1510 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1511 rmp->context = context;
1513 vl_api_send_msg (reg, (u8 *) rmp);
1517 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1518 vl_api_registration_t * reg,
1521 vl_api_nat44_identity_mapping_details_t *rmp;
1522 snat_main_t *sm = &snat_main;
1523 u32 len = sizeof (*rmp);
1527 len += vec_len (m->tag);
1528 rmp = vl_msg_api_alloc (len);
1529 clib_memset (rmp, 0, len);
1530 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, &rmp->tag);
1534 rmp = vl_msg_api_alloc (len);
1535 clib_memset (rmp, 0, len);
1539 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1542 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1544 rmp->port = htons (m->l_port);
1545 rmp->sw_if_index = htonl (m->sw_if_index);
1546 rmp->vrf_id = htonl (m->vrf_id);
1547 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1548 rmp->context = context;
1550 vl_api_send_msg (reg, (u8 *) rmp);
1554 vl_api_nat44_identity_mapping_dump_t_handler
1555 (vl_api_nat44_identity_mapping_dump_t * mp)
1557 vl_api_registration_t *reg;
1558 snat_main_t *sm = &snat_main;
1559 snat_static_mapping_t *m;
1560 snat_static_map_resolve_t *rp;
1563 if (sm->deterministic)
1566 reg = vl_api_client_index_to_registration (mp->client_index);
1571 pool_foreach (m, sm->static_mappings,
1573 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1575 pool_foreach_index (j, m->locals,
1577 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1583 for (j = 0; j < vec_len (sm->to_resolve); j++)
1585 rp = sm->to_resolve + j;
1586 if (rp->identity_nat)
1587 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1591 static void *vl_api_nat44_identity_mapping_dump_t_print
1592 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1596 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1602 vl_api_nat44_add_del_interface_addr_t_handler
1603 (vl_api_nat44_add_del_interface_addr_t * mp)
1605 snat_main_t *sm = &snat_main;
1606 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1607 u32 sw_if_index = ntohl (mp->sw_if_index);
1611 if (sm->deterministic)
1613 rv = VNET_API_ERROR_UNSUPPORTED;
1617 is_del = !mp->is_add;
1619 VALIDATE_SW_IF_INDEX (mp);
1621 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1622 mp->flags & NAT_API_IS_TWICE_NAT);
1624 BAD_SW_IF_INDEX_LABEL;
1626 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1629 static void *vl_api_nat44_add_del_interface_addr_t_print
1630 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1634 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1635 s = format (s, "sw_if_index %d twice_nat %d %s",
1636 clib_host_to_net_u32 (mp->sw_if_index),
1637 mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1643 send_nat44_interface_addr_details (u32 sw_if_index,
1644 vl_api_registration_t * reg, u32 context,
1647 vl_api_nat44_interface_addr_details_t *rmp;
1648 snat_main_t *sm = &snat_main;
1650 rmp = vl_msg_api_alloc (sizeof (*rmp));
1651 clib_memset (rmp, 0, sizeof (*rmp));
1653 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1654 rmp->sw_if_index = ntohl (sw_if_index);
1657 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1658 rmp->context = context;
1660 vl_api_send_msg (reg, (u8 *) rmp);
1664 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1667 vl_api_registration_t *reg;
1668 snat_main_t *sm = &snat_main;
1671 if (sm->deterministic)
1674 reg = vl_api_client_index_to_registration (mp->client_index);
1679 vec_foreach (i, sm->auto_add_sw_if_indices)
1680 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1681 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1682 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1687 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1692 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1698 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1701 vl_api_nat44_user_details_t *rmp;
1702 snat_main_t *sm = &snat_main;
1703 ip4_main_t *im = &ip4_main;
1705 rmp = vl_msg_api_alloc (sizeof (*rmp));
1706 clib_memset (rmp, 0, sizeof (*rmp));
1707 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1709 if (!pool_is_free_index (im->fibs, u->fib_index))
1711 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1712 rmp->vrf_id = ntohl (fib->ft_table_id);
1715 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1716 rmp->nsessions = ntohl (u->nsessions);
1717 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1718 rmp->context = context;
1720 vl_api_send_msg (reg, (u8 *) rmp);
1724 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1726 vl_api_registration_t *reg;
1727 snat_main_t *sm = &snat_main;
1728 snat_main_per_thread_data_t *tsm;
1731 if (sm->deterministic)
1734 reg = vl_api_client_index_to_registration (mp->client_index);
1739 vec_foreach (tsm, sm->per_thread_data)
1741 pool_foreach (u, tsm->users,
1743 send_nat44_user_details (u, reg, mp->context);
1750 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1754 s = format (0, "SCRIPT: nat44_user_dump ");
1760 send_nat44_user_session_details (snat_session_t * s,
1761 vl_api_registration_t * reg, u32 context)
1763 vl_api_nat44_user_session_details_t *rmp;
1764 snat_main_t *sm = &snat_main;
1766 rmp = vl_msg_api_alloc (sizeof (*rmp));
1767 clib_memset (rmp, 0, sizeof (*rmp));
1769 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1770 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1771 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1773 if (snat_is_session_static (s))
1774 rmp->flags |= NAT_API_IS_STATIC;
1776 if (is_twice_nat_session (s))
1777 rmp->flags |= NAT_API_IS_TWICE_NAT;
1779 if (is_ed_session (s) || is_fwd_bypass_session (s))
1780 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1782 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1783 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1784 rmp->total_pkts = ntohl (s->total_pkts);
1785 rmp->context = context;
1786 if (snat_is_unk_proto_session (s))
1788 rmp->outside_port = 0;
1789 rmp->inside_port = 0;
1790 rmp->protocol = ntohs (s->in2out.port);
1794 rmp->outside_port = s->out2in.port;
1795 rmp->inside_port = s->in2out.port;
1796 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1798 if (is_ed_session (s) || is_fwd_bypass_session (s))
1800 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1801 rmp->ext_host_port = s->ext_host_port;
1802 if (is_twice_nat_session (s))
1804 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1805 rmp->ext_host_nat_port = s->ext_host_nat_port;
1809 vl_api_send_msg (reg, (u8 *) rmp);
1813 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1816 vl_api_registration_t *reg;
1817 snat_main_t *sm = &snat_main;
1818 snat_main_per_thread_data_t *tsm;
1820 clib_bihash_kv_8_8_t key, value;
1821 snat_user_key_t ukey;
1823 u32 session_index, head_index, elt_index;
1824 dlist_elt_t *head, *elt;
1827 if (sm->deterministic)
1830 reg = vl_api_client_index_to_registration (mp->client_index);
1834 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1835 ip.src_address.as_u32 = ukey.addr.as_u32;
1836 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1837 key.key = ukey.as_u64;
1838 if (sm->num_workers > 1)
1840 vec_elt_at_index (sm->per_thread_data,
1841 sm->worker_in2out_cb (&ip, ukey.fib_index));
1843 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1844 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1846 u = pool_elt_at_index (tsm->users, value.value);
1847 if (!u->nsessions && !u->nstaticsessions)
1850 head_index = u->sessions_per_user_list_head_index;
1851 head = pool_elt_at_index (tsm->list_pool, head_index);
1852 elt_index = head->next;
1853 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1854 session_index = elt->value;
1855 while (session_index != ~0)
1857 s = pool_elt_at_index (tsm->sessions, session_index);
1859 send_nat44_user_session_details (s, reg, mp->context);
1861 elt_index = elt->next;
1862 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1863 session_index = elt->value;
1868 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1873 s = format (0, "SCRIPT: nat44_user_session_dump ");
1874 s = format (s, "ip_address %U vrf_id %d\n",
1875 format_ip4_address, mp->ip_address,
1876 clib_net_to_host_u32 (mp->vrf_id));
1881 static nat44_lb_addr_port_t *
1882 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1883 u32 addr_port_pair_num)
1886 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1887 vl_api_nat44_lb_addr_port_t *ap;
1889 for (i = 0; i < addr_port_pair_num; i++)
1891 ap = &addr_port_pairs[i];
1892 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1893 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1894 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1895 lb_addr_port.probability = ap->probability;
1896 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1897 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1900 return lb_addr_port_pairs;
1904 vl_api_nat44_add_del_lb_static_mapping_t_handler
1905 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1907 snat_main_t *sm = &snat_main;
1908 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1909 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1911 nat44_lb_addr_port_t *locals = 0;
1912 ip4_address_t e_addr;
1913 snat_protocol_t proto;
1914 vl_api_string_t *sp;
1918 if (!sm->endpoint_dependent)
1920 rv = VNET_API_ERROR_UNSUPPORTED;
1925 unformat_nat44_lb_addr_port (mp->locals,
1926 clib_net_to_host_u32 (mp->local_num));
1927 clib_memcpy (&e_addr, mp->external_addr, 4);
1928 proto = ip_proto_to_snat_proto (mp->protocol);
1930 if (mp->flags & NAT_API_IS_TWICE_NAT)
1931 twice_nat = TWICE_NAT;
1932 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1933 twice_nat = TWICE_NAT_SELF;
1935 sp = (void *) &mp->locals +
1936 sizeof (vl_api_nat44_lb_addr_port_t) *
1937 clib_net_to_host_u32 (mp->local_num);
1939 len = vl_api_string_len (sp);
1941 tag = vec_new (u8, len);
1942 memcpy (tag, sp->buf, len);
1943 vec_terminate_c_string (tag);
1946 nat44_add_del_lb_static_mapping (e_addr,
1947 clib_net_to_host_u16 (mp->external_port),
1948 proto, locals, mp->is_add,
1950 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1951 clib_net_to_host_u32 (mp->affinity));
1957 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1960 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1961 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1965 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1966 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1968 mp->flags & NAT_API_IS_TWICE_NAT,
1969 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1975 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1976 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1978 snat_main_t *sm = &snat_main;
1979 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1981 ip4_address_t e_addr, l_addr;
1982 snat_protocol_t proto;
1984 if (!sm->endpoint_dependent)
1986 rv = VNET_API_ERROR_UNSUPPORTED;
1990 clib_memcpy (&e_addr, mp->external_addr, 4);
1991 clib_memcpy (&l_addr, mp->local.addr, 4);
1992 proto = ip_proto_to_snat_proto (mp->protocol);
1995 nat44_lb_static_mapping_add_del_local (e_addr,
1996 clib_net_to_host_u16
1997 (mp->external_port), l_addr,
1998 clib_net_to_host_u16 (mp->
2001 clib_net_to_host_u32 (mp->
2003 mp->local.probability, mp->is_add);
2006 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
2009 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
2010 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
2014 s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
2015 s = format (s, "is_add %d", mp->is_add);
2021 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
2022 vl_api_registration_t * reg,
2025 vl_api_nat44_lb_static_mapping_details_t *rmp;
2026 snat_main_t *sm = &snat_main;
2027 nat44_lb_addr_port_t *ap;
2028 vl_api_nat44_lb_addr_port_t *locals;
2029 vl_api_string_t *sp;
2031 u32 len = sizeof (*rmp);
2035 len += pool_elts (m->locals) *
2036 sizeof (nat44_lb_addr_port_t) + vec_len (m->tag);
2037 rmp = vl_msg_api_alloc (len);
2038 clib_memset (rmp, 0, len);
2040 sp = (void *) &m->locals +
2041 sizeof (vl_api_nat44_lb_addr_port_t) * pool_elts (m->locals);
2042 vl_api_to_api_string (vec_len (m->tag), (char *) m->tag, sp);
2046 len += pool_elts (m->locals) * sizeof (nat44_lb_addr_port_t);
2047 rmp = vl_msg_api_alloc (len);
2048 clib_memset (rmp, 0, len);
2052 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
2054 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
2055 rmp->external_port = ntohs (m->external_port);
2056 rmp->protocol = snat_proto_to_ip_proto (m->proto);
2057 rmp->context = context;
2059 if (m->twice_nat == TWICE_NAT)
2060 rmp->flags |= NAT_API_IS_TWICE_NAT;
2061 else if (m->twice_nat == TWICE_NAT_SELF)
2062 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
2063 if (is_out2in_only_static_mapping (m))
2064 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
2066 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
2068 pool_foreach (ap, m->locals,
2070 clib_memcpy (locals->addr, &(ap->addr), 4);
2071 locals->port = htons (ap->port);
2072 locals->probability = ap->probability;
2073 locals->vrf_id = ntohl (ap->vrf_id);
2078 rmp->local_num = ntohl (local_num);
2080 vl_api_send_msg (reg, (u8 *) rmp);
2084 vl_api_nat44_lb_static_mapping_dump_t_handler
2085 (vl_api_nat44_lb_static_mapping_dump_t * mp)
2087 vl_api_registration_t *reg;
2088 snat_main_t *sm = &snat_main;
2089 snat_static_mapping_t *m;
2091 if (!sm->endpoint_dependent)
2094 reg = vl_api_client_index_to_registration (mp->client_index);
2099 pool_foreach (m, sm->static_mappings,
2101 if (is_lb_static_mapping(m))
2102 send_nat44_lb_static_mapping_details (m, reg, mp->context);
2107 static void *vl_api_nat44_lb_static_mapping_dump_t_print
2108 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
2112 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
2118 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
2120 snat_main_t *sm = &snat_main;
2121 vl_api_nat44_del_session_reply_t *rmp;
2122 ip4_address_t addr, eh_addr;
2127 snat_protocol_t proto;
2129 if (sm->deterministic)
2131 rv = VNET_API_ERROR_UNSUPPORTED;
2135 memcpy (&addr.as_u8, mp->address, 4);
2136 port = clib_net_to_host_u16 (mp->port);
2137 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
2138 proto = ip_proto_to_snat_proto (mp->protocol);
2139 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
2140 eh_port = clib_net_to_host_u16 (mp->ext_host_port);
2142 is_in = mp->flags & NAT_API_IS_INSIDE;
2144 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2146 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
2149 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
2152 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
2156 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
2161 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
2162 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
2163 format_ip4_address, mp->address,
2164 clib_net_to_host_u16 (mp->port),
2165 mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
2166 mp->flags & NAT_API_IS_INSIDE);
2167 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
2168 s = format (s, "ext_host_address %U ext_host_port %d",
2169 format_ip4_address, mp->ext_host_address,
2170 clib_net_to_host_u16 (mp->ext_host_port));
2176 vl_api_nat44_forwarding_enable_disable_t_handler
2177 (vl_api_nat44_forwarding_enable_disable_t * mp)
2179 snat_main_t *sm = &snat_main;
2180 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
2182 u32 *ses_to_be_removed = 0, *ses_index;
2183 snat_main_per_thread_data_t *tsm;
2186 sm->forwarding_enabled = mp->enable != 0;
2188 if (mp->enable == 0)
2191 vec_foreach (tsm, sm->per_thread_data)
2193 pool_foreach (s, tsm->sessions,
2195 if (is_fwd_bypass_session(s))
2197 vec_add1 (ses_to_be_removed, s - tsm->sessions);
2200 vec_foreach (ses_index, ses_to_be_removed)
2202 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2203 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2204 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2206 vec_free (ses_to_be_removed);
2211 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2214 static void *vl_api_nat44_forwarding_enable_disable_t_print
2215 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2219 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2220 s = format (s, "enable %d", mp->enable != 0);
2226 vl_api_nat44_forwarding_is_enabled_t_handler
2227 (vl_api_nat44_forwarding_is_enabled_t * mp)
2229 vl_api_registration_t *reg;
2230 snat_main_t *sm = &snat_main;
2231 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2233 reg = vl_api_client_index_to_registration (mp->client_index);
2237 rmp = vl_msg_api_alloc (sizeof (*rmp));
2238 clib_memset (rmp, 0, sizeof (*rmp));
2240 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2241 rmp->context = mp->context;
2243 rmp->enabled = sm->forwarding_enabled;
2245 vl_api_send_msg (reg, (u8 *) rmp);
2248 static void *vl_api_nat44_forwarding_is_enabled_t_print
2249 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2253 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2258 /*******************************/
2259 /*** Deterministic NAT (CGN) ***/
2260 /*******************************/
2263 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
2265 snat_main_t *sm = &snat_main;
2266 vl_api_nat_det_add_del_map_reply_t *rmp;
2268 ip4_address_t in_addr, out_addr;
2270 if (!sm->deterministic)
2272 rv = VNET_API_ERROR_UNSUPPORTED;
2276 clib_memcpy (&in_addr, mp->in_addr, 4);
2277 clib_memcpy (&out_addr, mp->out_addr, 4);
2278 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
2279 mp->out_plen, mp->is_add);
2282 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
2286 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
2291 s = format (0, "SCRIPT: nat_det_add_del_map ");
2292 s = format (s, "inside address %U/%d outside address %U/%d\n",
2293 format_ip4_address, mp->in_addr, mp->in_plen,
2294 format_ip4_address, mp->out_addr, mp->out_plen);
2300 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
2302 snat_main_t *sm = &snat_main;
2303 vl_api_nat_det_forward_reply_t *rmp;
2305 u16 lo_port = 0, hi_port = 0;
2307 ip4_address_t in_addr, out_addr;
2309 if (!sm->deterministic)
2311 rv = VNET_API_ERROR_UNSUPPORTED;
2312 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
2316 out_addr.as_u32 = 0;
2317 clib_memcpy (&in_addr, mp->in_addr, 4);
2318 dm = snat_det_map_by_user (sm, &in_addr);
2321 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2325 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2326 hi_port = lo_port + dm->ports_per_host - 1;
2330 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
2332 rmp->out_port_lo = ntohs (lo_port);
2333 rmp->out_port_hi = ntohs (hi_port);
2334 clib_memcpy (rmp->out_addr, &out_addr, 4);
2340 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
2344 s = format (0, "SCRIPT: nat_det_forward");
2345 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
2351 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
2353 snat_main_t *sm = &snat_main;
2354 vl_api_nat_det_reverse_reply_t *rmp;
2356 ip4_address_t out_addr, in_addr;
2359 if (!sm->deterministic)
2361 rv = VNET_API_ERROR_UNSUPPORTED;
2362 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
2367 clib_memcpy (&out_addr, mp->out_addr, 4);
2368 dm = snat_det_map_by_out (sm, &out_addr);
2371 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2375 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
2379 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
2381 clib_memcpy (rmp->in_addr, &in_addr, 4);
2387 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
2391 s = format (0, "SCRIPT: nat_det_reverse");
2392 s = format (s, "outside ip address %U outside port %d",
2393 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
2399 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
2402 vl_api_nat_det_map_details_t *rmp;
2403 snat_main_t *sm = &snat_main;
2405 rmp = vl_msg_api_alloc (sizeof (*rmp));
2406 clib_memset (rmp, 0, sizeof (*rmp));
2407 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
2408 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
2409 rmp->in_plen = m->in_plen;
2410 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
2411 rmp->out_plen = m->out_plen;
2412 rmp->sharing_ratio = htonl (m->sharing_ratio);
2413 rmp->ports_per_host = htons (m->ports_per_host);
2414 rmp->ses_num = htonl (m->ses_num);
2415 rmp->context = context;
2417 vl_api_send_msg (reg, (u8 *) rmp);
2421 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
2423 vl_api_registration_t *reg;
2424 snat_main_t *sm = &snat_main;
2427 if (!sm->deterministic)
2430 reg = vl_api_client_index_to_registration (mp->client_index);
2435 vec_foreach(m, sm->det_maps)
2436 sent_nat_det_map_details(m, reg, mp->context);
2441 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
2445 s = format (0, "SCRIPT: nat_det_map_dump ");
2451 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
2454 snat_main_t *sm = &snat_main;
2455 vl_api_nat_det_close_session_out_reply_t *rmp;
2456 ip4_address_t out_addr, ext_addr, in_addr;
2457 snat_det_out_key_t key;
2459 snat_det_session_t *ses;
2462 if (!sm->deterministic)
2464 rv = VNET_API_ERROR_UNSUPPORTED;
2468 clib_memcpy (&out_addr, mp->out_addr, 4);
2469 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2471 dm = snat_det_map_by_out (sm, &out_addr);
2474 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2477 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
2478 key.ext_host_addr = ext_addr;
2479 key.ext_host_port = mp->ext_port;
2480 key.out_port = mp->out_port;
2481 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
2484 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2487 snat_det_ses_close (dm, ses);
2490 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2494 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
2499 s = format (0, "SCRIPT: nat_det_close_session_out ");
2500 s = format (s, "out_addr %U out_port %d "
2501 "ext_addr %U ext_port %d\n",
2502 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
2503 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2509 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
2512 snat_main_t *sm = &snat_main;
2513 vl_api_nat_det_close_session_in_reply_t *rmp;
2514 ip4_address_t in_addr, ext_addr;
2515 snat_det_out_key_t key;
2517 snat_det_session_t *ses;
2520 if (!sm->deterministic)
2522 rv = VNET_API_ERROR_UNSUPPORTED;
2526 clib_memcpy (&in_addr, mp->in_addr, 4);
2527 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2529 dm = snat_det_map_by_user (sm, &in_addr);
2532 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2535 key.ext_host_addr = ext_addr;
2536 key.ext_host_port = mp->ext_port;
2537 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
2540 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2543 snat_det_ses_close (dm, ses);
2546 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2550 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
2554 s = format (0, "SCRIPT: nat_det_close_session_in ");
2555 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
2556 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
2557 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2563 send_nat_det_session_details (snat_det_session_t * s,
2564 vl_api_registration_t * reg, u32 context)
2566 vl_api_nat_det_session_details_t *rmp;
2567 snat_main_t *sm = &snat_main;
2569 rmp = vl_msg_api_alloc (sizeof (*rmp));
2570 clib_memset (rmp, 0, sizeof (*rmp));
2571 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
2572 rmp->in_port = s->in_port;
2573 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
2574 rmp->ext_port = s->out.ext_host_port;
2575 rmp->out_port = s->out.out_port;
2576 rmp->state = s->state;
2577 rmp->expire = ntohl (s->expire);
2578 rmp->context = context;
2580 vl_api_send_msg (reg, (u8 *) rmp);
2584 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
2586 vl_api_registration_t *reg;
2587 snat_main_t *sm = &snat_main;
2588 ip4_address_t user_addr;
2590 snat_det_session_t *s, empty_ses;
2593 if (!sm->deterministic)
2596 reg = vl_api_client_index_to_registration (mp->client_index);
2600 clib_memset (&empty_ses, 0, sizeof (empty_ses));
2601 clib_memcpy (&user_addr, mp->user_addr, 4);
2602 dm = snat_det_map_by_user (sm, &user_addr);
2606 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2607 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2610 send_nat_det_session_details (s, reg, mp->context);
2616 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2621 s = format (0, "SCRIPT: nat_det_session_dump ");
2622 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2632 vl_api_nat64_add_del_pool_addr_range_t_handler
2633 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2635 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2636 snat_main_t *sm = &snat_main;
2638 ip4_address_t this_addr;
2639 u32 start_host_order, end_host_order;
2644 tmp = (u32 *) mp->start_addr;
2645 start_host_order = clib_host_to_net_u32 (tmp[0]);
2646 tmp = (u32 *) mp->end_addr;
2647 end_host_order = clib_host_to_net_u32 (tmp[0]);
2649 count = (end_host_order - start_host_order) + 1;
2651 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2653 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2655 for (i = 0; i < count; i++)
2657 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2660 increment_v4_address (&this_addr);
2664 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2667 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2668 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2672 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2673 s = format (s, "%U - %U vrf_id %u %s\n",
2674 format_ip4_address, mp->start_addr,
2675 format_ip4_address, mp->end_addr,
2676 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2681 typedef struct nat64_api_walk_ctx_t_
2683 vl_api_registration_t *reg;
2686 } nat64_api_walk_ctx_t;
2689 nat64_api_pool_walk (snat_address_t * a, void *arg)
2691 vl_api_nat64_pool_addr_details_t *rmp;
2692 snat_main_t *sm = &snat_main;
2693 nat64_api_walk_ctx_t *ctx = arg;
2695 rmp = vl_msg_api_alloc (sizeof (*rmp));
2696 clib_memset (rmp, 0, sizeof (*rmp));
2697 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2698 clib_memcpy (rmp->address, &(a->addr), 4);
2699 if (a->fib_index != ~0)
2701 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2704 rmp->vrf_id = ntohl (fib->ft_table_id);
2708 rmp->context = ctx->context;
2710 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2716 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2718 vl_api_registration_t *reg;
2720 reg = vl_api_client_index_to_registration (mp->client_index);
2724 nat64_api_walk_ctx_t ctx = {
2726 .context = mp->context,
2729 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2733 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2738 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2744 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2747 snat_main_t *sm = &snat_main;
2748 vl_api_nat64_add_del_interface_reply_t *rmp;
2751 VALIDATE_SW_IF_INDEX (mp);
2754 nat64_add_del_interface (ntohl (mp->sw_if_index),
2755 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2757 BAD_SW_IF_INDEX_LABEL;
2759 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2763 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2768 s = format (0, "SCRIPT: nat64_add_del_interface ");
2769 s = format (s, "sw_if_index %d %s %s",
2770 clib_host_to_net_u32 (mp->sw_if_index),
2771 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2772 mp->is_add ? "" : "del");
2778 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2780 vl_api_nat64_interface_details_t *rmp;
2781 snat_main_t *sm = &snat_main;
2782 nat64_api_walk_ctx_t *ctx = arg;
2784 rmp = vl_msg_api_alloc (sizeof (*rmp));
2785 clib_memset (rmp, 0, sizeof (*rmp));
2786 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2787 rmp->sw_if_index = ntohl (i->sw_if_index);
2789 if (nat_interface_is_inside (i))
2790 rmp->flags |= NAT_API_IS_INSIDE;
2791 if (nat_interface_is_outside (i))
2792 rmp->flags |= NAT_API_IS_OUTSIDE;
2794 rmp->context = ctx->context;
2796 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2802 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2804 vl_api_registration_t *reg;
2806 reg = vl_api_client_index_to_registration (mp->client_index);
2810 nat64_api_walk_ctx_t ctx = {
2812 .context = mp->context,
2815 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2819 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2824 s = format (0, "SCRIPT: snat_interface_dump ");
2830 vl_api_nat64_add_del_static_bib_t_handler
2831 (vl_api_nat64_add_del_static_bib_t * mp)
2833 snat_main_t *sm = &snat_main;
2834 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2835 ip6_address_t in_addr;
2836 ip4_address_t out_addr;
2839 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2840 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2843 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2844 clib_net_to_host_u16 (mp->i_port),
2845 clib_net_to_host_u16 (mp->o_port),
2847 clib_net_to_host_u32 (mp->vrf_id),
2850 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2853 static void *vl_api_nat64_add_del_static_bib_t_print
2854 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2858 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2859 s = format (s, "protocol %d i_addr %U o_addr %U ",
2861 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2863 if (mp->vrf_id != ~0)
2864 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2870 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2872 vl_api_nat64_bib_details_t *rmp;
2873 snat_main_t *sm = &snat_main;
2874 nat64_api_walk_ctx_t *ctx = arg;
2877 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2881 rmp = vl_msg_api_alloc (sizeof (*rmp));
2882 clib_memset (rmp, 0, sizeof (*rmp));
2883 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2884 rmp->context = ctx->context;
2885 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2886 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2887 rmp->i_port = bibe->in_port;
2888 rmp->o_port = bibe->out_port;
2889 rmp->vrf_id = ntohl (fib->ft_table_id);
2890 rmp->proto = bibe->proto;
2891 if (bibe->is_static)
2892 rmp->flags |= NAT_API_IS_STATIC;
2893 rmp->ses_num = ntohl (bibe->ses_num);
2895 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2901 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2903 vl_api_registration_t *reg;
2904 nat64_main_t *nm = &nat64_main;
2907 reg = vl_api_client_index_to_registration (mp->client_index);
2911 nat64_api_walk_ctx_t ctx = {
2913 .context = mp->context,
2917 vec_foreach (db, nm->db)
2918 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2923 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2927 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2933 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2935 vl_api_nat64_st_details_t *rmp;
2936 snat_main_t *sm = &snat_main;
2937 nat64_api_walk_ctx_t *ctx = arg;
2938 nat64_db_bib_entry_t *bibe;
2941 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2945 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2949 rmp = vl_msg_api_alloc (sizeof (*rmp));
2950 clib_memset (rmp, 0, sizeof (*rmp));
2951 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2952 rmp->context = ctx->context;
2953 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2954 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2955 rmp->il_port = bibe->in_port;
2956 rmp->ol_port = bibe->out_port;
2957 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2958 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2959 rmp->il_port = ste->r_port;
2960 rmp->vrf_id = ntohl (fib->ft_table_id);
2961 rmp->proto = ste->proto;
2963 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2969 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2971 vl_api_registration_t *reg;
2972 nat64_main_t *nm = &nat64_main;
2975 reg = vl_api_client_index_to_registration (mp->client_index);
2979 nat64_api_walk_ctx_t ctx = {
2981 .context = mp->context,
2985 vec_foreach (db, nm->db)
2988 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2994 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2998 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
3004 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
3006 vl_api_nat64_add_del_prefix_reply_t *rmp;
3007 snat_main_t *sm = &snat_main;
3008 ip6_address_t prefix;
3011 memcpy (&prefix.as_u8, mp->prefix.prefix, 16);
3014 nat64_add_del_prefix (&prefix, mp->prefix.len,
3015 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
3016 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
3020 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
3025 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
3026 format_ip6_address, mp->prefix.prefix, mp->prefix.len,
3027 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
3033 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
3035 vl_api_nat64_prefix_details_t *rmp;
3036 snat_main_t *sm = &snat_main;
3037 nat64_api_walk_ctx_t *ctx = arg;
3039 rmp = vl_msg_api_alloc (sizeof (*rmp));
3040 clib_memset (rmp, 0, sizeof (*rmp));
3041 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
3042 clib_memcpy (rmp->prefix.prefix, &(p->prefix), 16);
3043 rmp->prefix.len = p->plen;
3044 rmp->vrf_id = ntohl (p->vrf_id);
3045 rmp->context = ctx->context;
3047 vl_api_send_msg (ctx->reg, (u8 *) rmp);
3053 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
3055 vl_api_registration_t *reg;
3057 reg = vl_api_client_index_to_registration (mp->client_index);
3061 nat64_api_walk_ctx_t ctx = {
3063 .context = mp->context,
3066 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
3070 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
3075 s = format (0, "SCRIPT: nat64_prefix_dump\n");
3081 vl_api_nat64_add_del_interface_addr_t_handler
3082 (vl_api_nat64_add_del_interface_addr_t * mp)
3084 snat_main_t *sm = &snat_main;
3085 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
3086 u32 sw_if_index = ntohl (mp->sw_if_index);
3089 VALIDATE_SW_IF_INDEX (mp);
3091 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
3093 BAD_SW_IF_INDEX_LABEL;
3095 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
3098 static void *vl_api_nat64_add_del_interface_addr_t_print
3099 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
3103 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
3104 s = format (s, "sw_if_index %d %s",
3105 clib_host_to_net_u32 (mp->sw_if_index),
3106 mp->is_add ? "" : "del");
3116 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
3118 vl_api_dslite_set_aftr_addr_reply_t *rmp;
3119 snat_main_t *sm = &snat_main;
3120 dslite_main_t *dm = &dslite_main;
3122 ip6_address_t ip6_addr;
3123 ip4_address_t ip4_addr;
3125 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3126 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3128 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
3130 rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
3132 REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
3136 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
3141 s = format (0, "SCRIPT: dslite_set_aftr_addr ");
3142 s = format (s, "ip6_addr %U ip4_addr %U\n",
3143 format_ip6_address, mp->ip6_addr,
3144 format_ip4_address, mp->ip4_addr);
3150 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
3152 snat_main_t *sm = &snat_main;
3153 vl_api_dslite_get_aftr_addr_reply_t *rmp;
3154 dslite_main_t *dm = &dslite_main;
3158 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
3160 memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
3161 memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
3167 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
3172 s = format (0, "SCRIPT: dslite_get_aftr_addr");
3178 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
3180 vl_api_dslite_set_b4_addr_reply_t *rmp;
3181 snat_main_t *sm = &snat_main;
3182 dslite_main_t *dm = &dslite_main;
3184 ip6_address_t ip6_addr;
3185 ip4_address_t ip4_addr;
3187 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
3188 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
3190 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
3192 rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
3194 REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
3198 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
3203 s = format (0, "SCRIPT: dslite_set_b4_addr ");
3204 s = format (s, "ip6_addr %U ip4_addr %U\n",
3205 format_ip6_address, mp->ip6_addr,
3206 format_ip6_address, mp->ip4_addr);
3212 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
3214 snat_main_t *sm = &snat_main;
3215 vl_api_dslite_get_b4_addr_reply_t *rmp;
3216 dslite_main_t *dm = &dslite_main;
3220 REPLY_MACRO2 (VL_API_DSLITE_GET_B4_ADDR_REPLY,
3222 memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
3223 memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
3229 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
3234 s = format (0, "SCRIPT: dslite_get_b4_addr");
3240 vl_api_dslite_add_del_pool_addr_range_t_handler
3241 (vl_api_dslite_add_del_pool_addr_range_t * mp)
3243 vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
3244 snat_main_t *sm = &snat_main;
3245 dslite_main_t *dm = &dslite_main;
3247 ip4_address_t this_addr;
3248 u32 start_host_order, end_host_order;
3252 tmp = (u32 *) mp->start_addr;
3253 start_host_order = clib_host_to_net_u32 (tmp[0]);
3254 tmp = (u32 *) mp->end_addr;
3255 end_host_order = clib_host_to_net_u32 (tmp[0]);
3257 count = (end_host_order - start_host_order) + 1;
3258 memcpy (&this_addr.as_u8, mp->start_addr, 4);
3260 for (i = 0; i < count; i++)
3262 if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
3265 increment_v4_address (&this_addr);
3269 REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
3273 send_dslite_address_details (snat_address_t * ap,
3274 vl_api_registration_t * reg, u32 context)
3276 vl_api_dslite_address_details_t *rmp;
3277 snat_main_t *sm = &snat_main;
3279 rmp = vl_msg_api_alloc (sizeof (*rmp));
3281 clib_memset (rmp, 0, sizeof (*rmp));
3283 rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
3284 clib_memcpy (rmp->ip_address, &(ap->addr), 4);
3285 rmp->context = context;
3287 vl_api_send_msg (reg, (u8 *) rmp);
3291 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
3293 vl_api_registration_t *reg;
3294 dslite_main_t *dm = &dslite_main;
3297 reg = vl_api_client_index_to_registration (mp->client_index);
3302 vec_foreach (ap, dm->addr_pool)
3304 send_dslite_address_details (ap, reg, mp->context);
3310 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
3315 s = format (0, "SCRIPT: dslite_address_dump ");
3320 static void *vl_api_dslite_add_del_pool_addr_range_t_print
3321 (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
3325 s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
3326 s = format (s, "%U - %U\n",
3327 format_ip4_address, mp->start_addr,
3328 format_ip4_address, mp->end_addr);
3339 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
3342 snat_main_t *sm = &snat_main;
3343 vl_api_nat66_add_del_interface_reply_t *rmp;
3346 VALIDATE_SW_IF_INDEX (mp);
3349 nat66_interface_add_del (ntohl (mp->sw_if_index),
3350 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
3352 BAD_SW_IF_INDEX_LABEL;
3354 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
3358 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
3363 s = format (0, "SCRIPT: nat66_add_del_interface ");
3364 s = format (s, "sw_if_index %d %s %s",
3365 clib_host_to_net_u32 (mp->sw_if_index),
3366 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
3367 mp->is_add ? "" : "del");
3373 vl_api_nat66_add_del_static_mapping_t_handler
3374 (vl_api_nat66_add_del_static_mapping_t * mp)
3376 snat_main_t *sm = &snat_main;
3377 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
3378 ip6_address_t l_addr, e_addr;
3381 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
3382 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
3385 nat66_static_mapping_add_del (&l_addr, &e_addr,
3386 clib_net_to_host_u32 (mp->vrf_id),
3389 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
3392 static void *vl_api_nat66_add_del_static_mapping_t_print
3393 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
3397 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
3398 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
3399 format_ip6_address, mp->local_ip_address,
3400 format_ip6_address, mp->external_ip_address,
3401 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
3406 typedef struct nat66_api_walk_ctx_t_
3408 vl_api_registration_t *rp;
3410 } nat66_api_walk_ctx_t;
3413 nat66_api_interface_walk (snat_interface_t * i, void *arg)
3415 vl_api_nat66_interface_details_t *rmp;
3416 snat_main_t *sm = &snat_main;
3417 nat66_api_walk_ctx_t *ctx = arg;
3419 rmp = vl_msg_api_alloc (sizeof (*rmp));
3420 clib_memset (rmp, 0, sizeof (*rmp));
3421 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
3422 rmp->sw_if_index = ntohl (i->sw_if_index);
3423 if (nat_interface_is_inside (i))
3424 rmp->flags |= NAT_API_IS_INSIDE;
3425 rmp->context = ctx->context;
3427 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3433 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
3435 vl_api_registration_t *rp;
3437 rp = vl_api_client_index_to_registration (mp->client_index);
3441 nat66_api_walk_ctx_t ctx = {
3443 .context = mp->context,
3446 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
3450 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
3455 s = format (0, "SCRIPT: nat66_interface_dump ");
3461 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
3463 vl_api_nat66_static_mapping_details_t *rmp;
3464 nat66_main_t *nm = &nat66_main;
3465 snat_main_t *sm = &snat_main;
3466 nat66_api_walk_ctx_t *ctx = arg;
3470 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
3474 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
3476 rmp = vl_msg_api_alloc (sizeof (*rmp));
3477 clib_memset (rmp, 0, sizeof (*rmp));
3479 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
3480 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
3481 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
3482 rmp->vrf_id = ntohl (fib->ft_table_id);
3483 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
3484 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
3485 rmp->context = ctx->context;
3487 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3493 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
3496 vl_api_registration_t *rp;
3498 rp = vl_api_client_index_to_registration (mp->client_index);
3502 nat66_api_walk_ctx_t ctx = {
3504 .context = mp->context,
3507 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
3511 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
3516 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
3522 /* List of message types that this plugin understands */
3523 #define foreach_snat_plugin_api_msg \
3524 _(NAT_CONTROL_PING, nat_control_ping) \
3525 _(NAT_SHOW_CONFIG, nat_show_config) \
3526 _(NAT_SET_WORKERS, nat_set_workers) \
3527 _(NAT_WORKER_DUMP, nat_worker_dump) \
3528 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
3529 _(NAT_SET_REASS, nat_set_reass) \
3530 _(NAT_GET_REASS, nat_get_reass) \
3531 _(NAT_REASS_DUMP, nat_reass_dump) \
3532 _(NAT_SET_TIMEOUTS, nat_set_timeouts) \
3533 _(NAT_GET_TIMEOUTS, nat_get_timeouts) \
3534 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg) \
3535 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg) \
3536 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping) \
3537 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping) \
3538 _(NAT_HA_SET_LISTENER, nat_ha_set_listener) \
3539 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover) \
3540 _(NAT_HA_GET_LISTENER, nat_ha_get_listener) \
3541 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover) \
3542 _(NAT_HA_FLUSH, nat_ha_flush) \
3543 _(NAT_HA_RESYNC, nat_ha_resync) \
3544 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
3545 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
3546 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
3547 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3548 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3549 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3550 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3551 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3552 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3553 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3554 _(NAT44_USER_DUMP, nat44_user_dump) \
3555 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3556 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3557 nat44_interface_add_del_output_feature) \
3558 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3559 nat44_interface_output_feature_dump) \
3560 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3561 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL, \
3562 nat44_lb_static_mapping_add_del_local) \
3563 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3564 _(NAT44_DEL_SESSION, nat44_del_session) \
3565 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3566 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3567 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3568 _(NAT_DET_FORWARD, nat_det_forward) \
3569 _(NAT_DET_REVERSE, nat_det_reverse) \
3570 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3571 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3572 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3573 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3574 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3575 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3576 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3577 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3578 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3579 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3580 _(NAT64_ST_DUMP, nat64_st_dump) \
3581 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3582 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3583 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3584 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range) \
3585 _(DSLITE_ADDRESS_DUMP, dslite_address_dump) \
3586 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr) \
3587 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr) \
3588 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr) \
3589 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr) \
3590 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3591 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3592 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3593 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3595 /* Set up the API message handling tables */
3596 static clib_error_t *
3597 snat_plugin_api_hookup (vlib_main_t * vm)
3599 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3601 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3603 vl_api_##n##_t_handler, \
3605 vl_api_##n##_t_endian, \
3606 vl_api_##n##_t_print, \
3607 sizeof(vl_api_##n##_t), 1);
3608 foreach_snat_plugin_api_msg;
3614 #define vl_msg_name_crc_list
3615 #include <nat/nat_all_api_h.h>
3616 #undef vl_msg_name_crc_list
3619 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3621 #define _(id,n,crc) \
3622 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3623 foreach_vl_msg_name_crc_nat;
3628 plugin_custom_dump_configure (snat_main_t * sm)
3630 #define _(n,f) sm->api_main->msg_print_handlers \
3631 [VL_API_##n + sm->msg_id_base] \
3632 = (void *) vl_api_##f##_t_print;
3633 foreach_snat_plugin_api_msg;
3638 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3641 clib_error_t *error = 0;
3643 name = format (0, "nat_%08x%c", api_version, 0);
3645 /* Ask for a correctly-sized block of API message decode slots */
3647 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3649 error = snat_plugin_api_hookup (vm);
3651 /* Add our API messages to the global name_crc hash table */
3652 setup_message_id_table (sm, sm->api_main);
3654 plugin_custom_dump_configure (sm);
3662 * fd.io coding-style-patch-verification: ON
3665 * eval: (c-set-style "gnu")