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_inlines.h>
27 #include <nat/nat_ha.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
31 #include <nat/nat_msg_enum.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vnet/ip/ip_types_api.h>
35 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
36 #define vl_api_nat44_nat44_lb_static_mapping_details_t_endian vl_noop_handler
38 /* define message structures */
40 #include <nat/nat_all_api_h.h>
43 /* define generated endian-swappers */
45 #include <nat/nat_all_api_h.h>
48 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
50 #define REPLY_MSG_ID_BASE sm->msg_id_base
51 #include <vlibapi/api_helper_macros.h>
53 /* Get the API version number */
54 #define vl_api_version(n,v) static u32 api_version=(v);
55 #include <nat/nat_all_api_h.h>
58 /* Macro to finish up custom dump fns */
61 vl_print (handle, (char *)s); \
65 /******************************/
66 /*** Common NAT plugin APIs ***/
67 /******************************/
70 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
72 vl_api_nat_control_ping_reply_t *rmp;
73 snat_main_t *sm = &snat_main;
77 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
79 rmp->vpe_pid = ntohl (getpid ());
85 vl_api_nat_control_ping_t_print (vl_api_nat_control_ping_t * mp, void *handle)
89 s = format (0, "SCRIPT: nat_control_ping ");
95 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
97 vl_api_nat_show_config_reply_t *rmp;
98 snat_main_t *sm = &snat_main;
99 dslite_main_t *dm = &dslite_main;
100 nat64_main_t *n64m = &nat64_main;
104 REPLY_MACRO2 (VL_API_NAT_SHOW_CONFIG_REPLY,
106 rmp->translation_buckets = htonl (sm->translation_buckets);
107 rmp->translation_memory_size = htonl (sm->translation_memory_size);
108 rmp->user_buckets = htonl (sm->user_buckets);
109 rmp->user_memory_size = htonl (sm->user_memory_size);
110 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
111 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
112 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
113 rmp->static_mapping_only = sm->static_mapping_only;
114 rmp->static_mapping_connection_tracking =
115 sm->static_mapping_connection_tracking;
116 rmp->deterministic = sm->deterministic;
117 rmp->endpoint_dependent = sm->endpoint_dependent;
118 rmp->out2in_dpo = sm->out2in_dpo;
119 rmp->dslite_ce = dm->is_ce;
120 rmp->nat64_bib_buckets = n64m->bib_buckets;
121 rmp->nat64_bib_memory_size = n64m->bib_memory_size;
122 rmp->nat64_st_buckets = n64m->st_buckets;
123 rmp->nat64_st_memory_size = n64m->st_memory_size;
129 vl_api_nat_show_config_t_print (vl_api_nat_show_config_t * mp, void *handle)
133 s = format (0, "SCRIPT: nat_show_config ");
139 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
141 snat_main_t *sm = &snat_main;
142 vl_api_nat_set_workers_reply_t *rmp;
147 if (sm->deterministic)
149 rv = VNET_API_ERROR_UNSUPPORTED;
153 mask = clib_net_to_host_u64 (mp->worker_mask);
155 if (sm->num_workers < 2)
157 rv = VNET_API_ERROR_FEATURE_DISABLED;
161 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
162 rv = snat_set_workers (bitmap);
163 clib_bitmap_free (bitmap);
166 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
170 vl_api_nat_set_workers_t_print (vl_api_nat_set_workers_t * mp, void *handle)
176 u64 mask = clib_net_to_host_u64 (mp->worker_mask);
178 s = format (0, "SCRIPT: nat_set_workers ");
179 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
181 clib_bitmap_foreach (i, bitmap,
184 s = format (s, "%d", i);
186 s = format (s, ",%d", i);
190 clib_bitmap_free (bitmap);
195 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
198 vl_api_nat_worker_details_t *rmp;
199 snat_main_t *sm = &snat_main;
200 vlib_worker_thread_t *w =
201 vlib_worker_threads + worker_index + sm->first_worker_index;
203 rmp = vl_msg_api_alloc (sizeof (*rmp));
204 clib_memset (rmp, 0, sizeof (*rmp));
205 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
206 rmp->context = context;
207 rmp->worker_index = htonl (worker_index);
208 rmp->lcore_id = htonl (w->cpu_id);
209 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
211 vl_api_send_msg (reg, (u8 *) rmp);
215 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
217 vl_api_registration_t *reg;
218 snat_main_t *sm = &snat_main;
221 if (sm->deterministic)
224 reg = vl_api_client_index_to_registration (mp->client_index);
229 vec_foreach (worker_index, sm->workers)
230 send_nat_worker_details(*worker_index, reg, mp->context);
235 vl_api_nat_worker_dump_t_print (vl_api_nat_worker_dump_t * mp, void *handle)
239 s = format (0, "SCRIPT: nat_worker_dump ");
245 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
247 snat_main_t *sm = &snat_main;
248 vl_api_nat_set_log_level_reply_t *rmp;
251 if (sm->log_level > NAT_LOG_DEBUG)
252 rv = VNET_API_ERROR_UNSUPPORTED;
254 sm->log_level = mp->log_level;
256 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
260 vl_api_nat_set_log_level_t_print (vl_api_nat_set_log_level_t *
265 s = format (0, "SCRIPT: nat_set_log_level ");
266 s = format (s, "log_level %d", mp->log_level);
272 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
275 snat_main_t *sm = &snat_main;
276 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
279 rv = snat_ipfix_logging_enable_disable (mp->enable,
285 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
289 vl_api_nat_ipfix_enable_disable_t_print (vl_api_nat_ipfix_enable_disable_t *
294 s = format (0, "SCRIPT: nat_ipfix_enable_disable ");
296 s = format (s, "domain %d ", clib_net_to_host_u32 (mp->domain_id));
298 s = format (s, "src_port %d ", clib_net_to_host_u16 (mp->src_port));
300 s = format (s, "disable ");
306 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
308 snat_main_t *sm = &snat_main;
309 vl_api_nat_set_timeouts_reply_t *rmp;
312 sm->udp_timeout = ntohl (mp->udp);
313 sm->tcp_established_timeout = ntohl (mp->tcp_established);
314 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
315 sm->icmp_timeout = ntohl (mp->icmp);
317 rv = nat64_set_icmp_timeout (ntohl (mp->icmp));
320 rv = nat64_set_udp_timeout (ntohl (mp->udp));
324 nat64_set_tcp_timeouts (ntohl (mp->tcp_transitory),
325 ntohl (mp->tcp_established));
328 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
332 vl_api_nat_set_timeouts_t_print (vl_api_nat_set_timeouts_t * mp, void *handle)
336 s = format (0, "SCRIPT: nat_set_timeouts ");
337 s = format (s, "udp %d tcp_established %d tcp_transitory %d icmp %d\n",
339 ntohl (mp->tcp_established),
340 ntohl (mp->tcp_transitory), ntohl (mp->icmp));
346 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
348 snat_main_t *sm = &snat_main;
349 vl_api_nat_get_timeouts_reply_t *rmp;
353 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
355 rmp->udp = htonl (sm->udp_timeout);
356 rmp->tcp_established = htonl (sm->tcp_established_timeout);
357 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
358 rmp->icmp = htonl (sm->icmp_timeout);
364 vl_api_nat_get_timeouts_t_print (vl_api_nat_get_timeouts_t * mp, void *handle)
368 s = format (0, "SCRIPT: nat_get_timeouts");
374 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
375 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
377 snat_main_t *sm = &snat_main;
378 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
380 u16 port_start, port_end;
382 if (sm->deterministic)
384 rv = VNET_API_ERROR_UNSUPPORTED;
390 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
391 nat_set_alloc_addr_and_port_default ();
393 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
394 nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
397 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
398 port_start = ntohs (mp->start_port);
399 port_end = ntohs (mp->end_port);
400 if (port_end <= port_start)
402 rv = VNET_API_ERROR_INVALID_VALUE;
405 nat_set_alloc_addr_and_port_range (port_start, port_end);
408 rv = VNET_API_ERROR_INVALID_VALUE;
413 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
416 static void *vl_api_nat_set_addr_and_port_alloc_alg_t_print
417 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp, void *handle)
421 s = format (0, "SCRIPT: nat_set_addr_and_port_alloc_alg ");
422 s = format (s, "alg %d psid_offset %d psid_length %d psid %d start_port %d "
424 ntohl (mp->alg), ntohl (mp->psid_offset),
425 ntohl (mp->psid_length), ntohs (mp->psid),
426 ntohs (mp->start_port), ntohs (mp->end_port));
432 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
433 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
435 snat_main_t *sm = &snat_main;
436 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
440 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
442 rmp->alg = sm->addr_and_port_alloc_alg;
443 rmp->psid_offset = sm->psid_offset;
444 rmp->psid_length = sm->psid_length;
445 rmp->psid = htons (sm->psid);
446 rmp->start_port = htons (sm->start_port);
447 rmp->end_port = htons (sm->end_port);
452 static void *vl_api_nat_get_addr_and_port_alloc_alg_t_print
453 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp, void *handle)
457 s = format (0, "SCRIPT: nat_get_addr_and_port_alloc_alg");
463 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
465 snat_main_t *sm = &snat_main;
466 vl_api_nat_set_mss_clamping_reply_t *rmp;
471 sm->mss_clamping = ntohs (mp->mss_value);
472 sm->mss_value_net = mp->mss_value;
475 sm->mss_clamping = 0;
477 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
481 vl_api_nat_set_mss_clamping_t_print (vl_api_nat_set_mss_clamping_t * mp,
486 s = format (0, "SCRIPT: nat_set_mss_clamping enable %d mss_value %d\n",
487 mp->enable, ntohs (mp->mss_value));
493 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
495 snat_main_t *sm = &snat_main;
496 vl_api_nat_get_mss_clamping_reply_t *rmp;
500 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
502 rmp->enable = sm->mss_clamping ? 1 : 0;
503 rmp->mss_value = htons (sm->mss_clamping);
509 vl_api_nat_get_mss_clamping_t_print (vl_api_nat_get_mss_clamping_t * mp,
514 s = format (0, "SCRIPT: nat_get_mss_clamping");
520 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
522 snat_main_t *sm = &snat_main;
523 vl_api_nat_ha_set_listener_reply_t *rmp;
527 memcpy (&addr, &mp->ip_address, sizeof (addr));
529 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
530 clib_net_to_host_u32 (mp->path_mtu));
532 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
536 vl_api_nat_ha_set_listener_t_print (vl_api_nat_ha_set_listener_t * mp,
541 s = format (0, "SCRIPT: nat_ha_set_listener ");
542 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
543 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
544 s = format (s, "path_mtu %d", clib_net_to_host_u32 (mp->path_mtu));
550 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
552 snat_main_t *sm = &snat_main;
553 vl_api_nat_ha_get_listener_reply_t *rmp;
559 nat_ha_get_listener (&addr, &port, &path_mtu);
562 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
564 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
565 rmp->port = clib_host_to_net_u16 (port);
566 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
572 vl_api_nat_ha_get_listener_t_print (vl_api_nat_ha_get_listener_t * mp,
577 s = format (0, "SCRIPT: nat_ha_get_listener");
583 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
585 snat_main_t *sm = &snat_main;
586 vl_api_nat_ha_set_failover_reply_t *rmp;
590 memcpy (&addr, &mp->ip_address, sizeof (addr));
592 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
593 clib_net_to_host_u32 (mp->session_refresh_interval));
595 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
599 vl_api_nat_ha_set_failover_t_print (vl_api_nat_ha_set_failover_t * mp,
604 s = format (0, "SCRIPT: nat_ha_set_failover ");
605 s = format (s, "ip_address %U ", format_ip4_address, mp->ip_address);
606 s = format (s, "port %d ", clib_net_to_host_u16 (mp->port));
612 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
614 snat_main_t *sm = &snat_main;
615 vl_api_nat_ha_get_failover_reply_t *rmp;
619 u32 session_refresh_interval;
621 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
624 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
626 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
627 rmp->port = clib_host_to_net_u16 (port);
628 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
634 vl_api_nat_ha_get_failover_t_print (vl_api_nat_ha_get_failover_t * mp,
639 s = format (0, "SCRIPT: nat_ha_get_failover");
645 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
647 snat_main_t *sm = &snat_main;
648 vl_api_nat_ha_flush_reply_t *rmp;
653 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
657 vl_api_nat_ha_flush_t_print (vl_api_nat_ha_flush_t * mp, void *handle)
661 s = format (0, "SCRIPT: nat_ha_flush ");
667 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
669 snat_main_t *sm = &snat_main;
670 vl_api_registration_t *reg;
671 vl_api_nat_ha_resync_completed_event_t *mp;
673 reg = vl_api_client_index_to_registration (client_index);
677 mp = vl_msg_api_alloc (sizeof (*mp));
678 clib_memset (mp, 0, sizeof (*mp));
679 mp->client_index = client_index;
681 mp->missed_count = clib_host_to_net_u32 (missed_count);
683 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
685 vl_api_send_msg (reg, (u8 *) mp);
689 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
691 snat_main_t *sm = &snat_main;
692 vl_api_nat_ha_resync_reply_t *rmp;
696 nat_ha_resync (mp->client_index, mp->pid,
697 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
700 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
704 vl_api_nat_ha_resync_t_print (vl_api_nat_ha_resync_t * mp, void *handle)
708 s = format (0, "SCRIPT: nat_ha_resync ");
710 format (s, "want_resync_event %d pid %d", mp->want_resync_event,
711 clib_host_to_net_u32 (mp->pid));
720 vl_api_nat44_add_del_address_range_t_handler
721 (vl_api_nat44_add_del_address_range_t * mp)
723 snat_main_t *sm = &snat_main;
724 vl_api_nat44_add_del_address_range_reply_t *rmp;
725 ip4_address_t this_addr;
726 u8 is_add, twice_nat;
727 u32 start_host_order, end_host_order;
733 if (sm->deterministic)
735 rv = VNET_API_ERROR_UNSUPPORTED;
739 if (sm->static_mapping_only)
741 rv = VNET_API_ERROR_FEATURE_DISABLED;
746 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
748 tmp = (u32 *) mp->first_ip_address;
749 start_host_order = clib_host_to_net_u32 (tmp[0]);
750 tmp = (u32 *) mp->last_ip_address;
751 end_host_order = clib_host_to_net_u32 (tmp[0]);
753 count = (end_host_order - start_host_order) + 1;
755 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
758 nat_log_info ("%U - %U, %d addresses...",
759 format_ip4_address, mp->first_ip_address,
760 format_ip4_address, mp->last_ip_address, count);
762 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
764 for (i = 0; i < count; i++)
767 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
769 rv = snat_del_address (sm, this_addr, 0, twice_nat);
775 nat44_add_del_address_dpo (this_addr, is_add);
777 increment_v4_address (&this_addr);
781 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
784 static void *vl_api_nat44_add_del_address_range_t_print
785 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
789 s = format (0, "SCRIPT: nat44_add_address_range ");
790 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
791 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
793 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
795 s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
800 send_nat44_address_details (snat_address_t * a,
801 vl_api_registration_t * reg, u32 context,
804 vl_api_nat44_address_details_t *rmp;
805 snat_main_t *sm = &snat_main;
807 rmp = vl_msg_api_alloc (sizeof (*rmp));
808 clib_memset (rmp, 0, sizeof (*rmp));
809 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
810 clib_memcpy (rmp->ip_address, &(a->addr), 4);
811 if (a->fib_index != ~0)
813 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
814 rmp->vrf_id = ntohl (fib->ft_table_id);
819 rmp->flags |= NAT_API_IS_TWICE_NAT;
820 rmp->context = context;
822 vl_api_send_msg (reg, (u8 *) rmp);
826 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
828 vl_api_registration_t *reg;
829 snat_main_t *sm = &snat_main;
832 if (sm->deterministic)
835 reg = vl_api_client_index_to_registration (mp->client_index);
840 vec_foreach (a, sm->addresses)
841 send_nat44_address_details (a, reg, mp->context, 0);
842 vec_foreach (a, sm->twice_nat_addresses)
843 send_nat44_address_details (a, reg, mp->context, 1);
848 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
853 s = format (0, "SCRIPT: nat44_address_dump ");
859 vl_api_nat44_interface_add_del_feature_t_handler
860 (vl_api_nat44_interface_add_del_feature_t * mp)
862 snat_main_t *sm = &snat_main;
863 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
864 u32 sw_if_index = ntohl (mp->sw_if_index);
868 is_del = !mp->is_add;
870 VALIDATE_SW_IF_INDEX (mp);
873 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
876 BAD_SW_IF_INDEX_LABEL;
878 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
881 static void *vl_api_nat44_interface_add_del_feature_t_print
882 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
886 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
887 s = format (s, "sw_if_index %d %s %s",
888 clib_host_to_net_u32 (mp->sw_if_index),
889 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
890 mp->is_add ? "" : "del");
896 send_nat44_interface_details (snat_interface_t * i,
897 vl_api_registration_t * reg, u32 context)
899 vl_api_nat44_interface_details_t *rmp;
900 snat_main_t *sm = &snat_main;
902 rmp = vl_msg_api_alloc (sizeof (*rmp));
903 clib_memset (rmp, 0, sizeof (*rmp));
904 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
905 rmp->sw_if_index = ntohl (i->sw_if_index);
907 if (nat_interface_is_inside (i))
908 rmp->flags |= NAT_API_IS_INSIDE;
909 if (nat_interface_is_outside (i))
910 rmp->flags |= NAT_API_IS_OUTSIDE;
912 rmp->context = context;
914 vl_api_send_msg (reg, (u8 *) rmp);
918 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
920 vl_api_registration_t *reg;
921 snat_main_t *sm = &snat_main;
924 reg = vl_api_client_index_to_registration (mp->client_index);
929 pool_foreach (i, sm->interfaces,
931 send_nat44_interface_details(i, reg, mp->context);
937 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
942 s = format (0, "SCRIPT: nat44_interface_dump ");
948 vl_api_nat44_interface_add_del_output_feature_t_handler
949 (vl_api_nat44_interface_add_del_output_feature_t * mp)
951 snat_main_t *sm = &snat_main;
952 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
953 u32 sw_if_index = ntohl (mp->sw_if_index);
956 if (sm->deterministic)
958 rv = VNET_API_ERROR_UNSUPPORTED;
962 VALIDATE_SW_IF_INDEX (mp);
964 rv = snat_interface_add_del_output_feature (sw_if_index,
965 mp->flags & NAT_API_IS_INSIDE,
968 BAD_SW_IF_INDEX_LABEL;
970 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
973 static void *vl_api_nat44_interface_add_del_output_feature_t_print
974 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
978 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
979 s = format (s, "sw_if_index %d %s %s",
980 clib_host_to_net_u32 (mp->sw_if_index),
981 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
982 mp->is_add ? "" : "del");
988 send_nat44_interface_output_feature_details (snat_interface_t * i,
989 vl_api_registration_t * reg,
992 vl_api_nat44_interface_output_feature_details_t *rmp;
993 snat_main_t *sm = &snat_main;
995 rmp = vl_msg_api_alloc (sizeof (*rmp));
996 clib_memset (rmp, 0, sizeof (*rmp));
998 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
999 rmp->sw_if_index = ntohl (i->sw_if_index);
1000 rmp->context = context;
1002 if (nat_interface_is_inside (i))
1003 rmp->flags |= NAT_API_IS_INSIDE;
1005 vl_api_send_msg (reg, (u8 *) rmp);
1009 vl_api_nat44_interface_output_feature_dump_t_handler
1010 (vl_api_nat44_interface_output_feature_dump_t * mp)
1012 vl_api_registration_t *reg;
1013 snat_main_t *sm = &snat_main;
1014 snat_interface_t *i;
1016 if (sm->deterministic)
1019 reg = vl_api_client_index_to_registration (mp->client_index);
1024 pool_foreach (i, sm->output_feature_interfaces,
1026 send_nat44_interface_output_feature_details(i, reg, mp->context);
1031 static void *vl_api_nat44_interface_output_feature_dump_t_print
1032 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1036 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1042 vl_api_nat44_add_del_static_mapping_t_handler
1043 (vl_api_nat44_add_del_static_mapping_t * mp)
1045 snat_main_t *sm = &snat_main;
1046 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1047 ip4_address_t local_addr, external_addr;
1048 u16 local_port = 0, external_port = 0;
1049 u32 vrf_id, external_sw_if_index;
1050 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1052 snat_protocol_t proto;
1055 if (sm->deterministic)
1057 rv = VNET_API_ERROR_UNSUPPORTED;
1061 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1062 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1064 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1066 local_port = clib_net_to_host_u16 (mp->local_port);
1067 external_port = clib_net_to_host_u16 (mp->external_port);
1070 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1071 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1072 proto = ip_proto_to_snat_proto (mp->protocol);
1074 if (mp->flags & NAT_API_IS_TWICE_NAT)
1075 twice_nat = TWICE_NAT;
1076 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1077 twice_nat = TWICE_NAT_SELF;
1078 mp->tag[sizeof (mp->tag) - 1] = 0;
1079 tag = format (0, "%s", mp->tag);
1080 vec_terminate_c_string (tag);
1082 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1083 external_port, vrf_id,
1084 mp->flags & NAT_API_IS_ADDR_ONLY,
1085 external_sw_if_index, proto,
1086 mp->is_add, twice_nat,
1087 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0);
1091 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1094 static void *vl_api_nat44_add_del_static_mapping_t_print
1095 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1099 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1100 s = format (s, "protocol %d local_addr %U external_addr %U ",
1102 format_ip4_address, mp->local_ip_address,
1103 format_ip4_address, mp->external_ip_address);
1105 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1106 s = format (s, "local_port %d external_port %d ",
1107 clib_net_to_host_u16 (mp->local_port),
1108 clib_net_to_host_u16 (mp->external_port));
1110 s = format (s, "twice_nat %d out2in_only %d ",
1111 mp->flags & NAT_API_IS_TWICE_NAT,
1112 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1114 if (mp->vrf_id != ~0)
1115 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1117 if (mp->external_sw_if_index != ~0)
1118 s = format (s, "external_sw_if_index %d",
1119 clib_net_to_host_u32 (mp->external_sw_if_index));
1124 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1125 vl_api_registration_t * reg, u32 context)
1127 vl_api_nat44_static_mapping_details_t *rmp;
1128 snat_main_t *sm = &snat_main;
1129 u32 len = sizeof (*rmp);
1131 rmp = vl_msg_api_alloc (len);
1132 clib_memset (rmp, 0, len);
1134 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1136 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1137 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1138 rmp->external_sw_if_index = ~0;
1139 rmp->vrf_id = htonl (m->vrf_id);
1140 rmp->context = context;
1142 if (m->twice_nat == TWICE_NAT)
1143 rmp->flags |= NAT_API_IS_TWICE_NAT;
1144 else if (m->twice_nat == TWICE_NAT_SELF)
1145 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1147 if (is_out2in_only_static_mapping (m))
1148 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1150 if (is_addr_only_static_mapping (m))
1152 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1156 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1157 rmp->external_port = htons (m->external_port);
1158 rmp->local_port = htons (m->local_port);
1162 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1164 vl_api_send_msg (reg, (u8 *) rmp);
1168 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1169 vl_api_registration_t * reg,
1172 vl_api_nat44_static_mapping_details_t *rmp;
1173 snat_main_t *sm = &snat_main;
1175 rmp = vl_msg_api_alloc (sizeof (*rmp));
1176 clib_memset (rmp, 0, sizeof (*rmp));
1178 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1179 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1180 rmp->external_sw_if_index = htonl (m->sw_if_index);
1181 rmp->vrf_id = htonl (m->vrf_id);
1182 rmp->context = context;
1185 rmp->flags |= NAT_API_IS_TWICE_NAT;
1189 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1193 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1194 rmp->external_port = htons (m->e_port);
1195 rmp->local_port = htons (m->l_port);
1198 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1200 vl_api_send_msg (reg, (u8 *) rmp);
1204 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1207 vl_api_registration_t *reg;
1208 snat_main_t *sm = &snat_main;
1209 snat_static_mapping_t *m;
1210 snat_static_map_resolve_t *rp;
1213 if (sm->deterministic)
1216 reg = vl_api_client_index_to_registration (mp->client_index);
1221 pool_foreach (m, sm->static_mappings,
1223 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1224 send_nat44_static_mapping_details (m, reg, mp->context);
1228 for (j = 0; j < vec_len (sm->to_resolve); j++)
1230 rp = sm->to_resolve + j;
1231 if (!rp->identity_nat)
1232 send_nat44_static_map_resolve_details (rp, reg, mp->context);
1237 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1242 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1248 vl_api_nat44_add_del_identity_mapping_t_handler
1249 (vl_api_nat44_add_del_identity_mapping_t * mp)
1251 snat_main_t *sm = &snat_main;
1252 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1255 u32 vrf_id, sw_if_index;
1257 snat_protocol_t proto = ~0;
1260 if (sm->deterministic)
1262 rv = VNET_API_ERROR_UNSUPPORTED;
1266 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1268 port = clib_net_to_host_u16 (mp->port);
1269 proto = ip_proto_to_snat_proto (mp->protocol);
1271 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1272 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1273 if (sw_if_index != ~0)
1276 memcpy (&addr.as_u8, mp->ip_address, 4);
1277 mp->tag[sizeof (mp->tag) - 1] = 0;
1278 tag = format (0, "%s", mp->tag);
1279 vec_terminate_c_string (tag);
1282 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1283 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1284 proto, mp->is_add, 0, 0, tag, 1);
1288 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1291 static void *vl_api_nat44_add_del_identity_mapping_t_print
1292 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1296 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1297 if (mp->sw_if_index != ~0)
1298 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1300 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1302 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1304 format (s, " protocol %d port %d", mp->protocol,
1305 clib_net_to_host_u16 (mp->port));
1307 if (mp->vrf_id != ~0)
1308 s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1314 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1315 vl_api_registration_t * reg, u32 context)
1317 vl_api_nat44_identity_mapping_details_t *rmp;
1318 snat_main_t *sm = &snat_main;
1319 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1321 rmp = vl_msg_api_alloc (sizeof (*rmp));
1322 clib_memset (rmp, 0, sizeof (*rmp));
1324 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1326 if (is_addr_only_static_mapping (m))
1327 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1329 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1330 rmp->port = htons (m->local_port);
1331 rmp->sw_if_index = ~0;
1332 rmp->vrf_id = htonl (local->vrf_id);
1333 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1334 rmp->context = context;
1336 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1338 vl_api_send_msg (reg, (u8 *) rmp);
1342 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1343 vl_api_registration_t * reg,
1346 vl_api_nat44_identity_mapping_details_t *rmp;
1347 snat_main_t *sm = &snat_main;
1349 rmp = vl_msg_api_alloc (sizeof (*rmp));
1350 clib_memset (rmp, 0, sizeof (*rmp));
1352 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1355 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1357 rmp->port = htons (m->l_port);
1358 rmp->sw_if_index = htonl (m->sw_if_index);
1359 rmp->vrf_id = htonl (m->vrf_id);
1360 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1361 rmp->context = context;
1363 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1365 vl_api_send_msg (reg, (u8 *) rmp);
1369 vl_api_nat44_identity_mapping_dump_t_handler
1370 (vl_api_nat44_identity_mapping_dump_t * mp)
1372 vl_api_registration_t *reg;
1373 snat_main_t *sm = &snat_main;
1374 snat_static_mapping_t *m;
1375 snat_static_map_resolve_t *rp;
1378 if (sm->deterministic)
1381 reg = vl_api_client_index_to_registration (mp->client_index);
1386 pool_foreach (m, sm->static_mappings,
1388 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1390 pool_foreach_index (j, m->locals,
1392 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1398 for (j = 0; j < vec_len (sm->to_resolve); j++)
1400 rp = sm->to_resolve + j;
1401 if (rp->identity_nat)
1402 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1406 static void *vl_api_nat44_identity_mapping_dump_t_print
1407 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1411 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1417 vl_api_nat44_add_del_interface_addr_t_handler
1418 (vl_api_nat44_add_del_interface_addr_t * mp)
1420 snat_main_t *sm = &snat_main;
1421 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1422 u32 sw_if_index = ntohl (mp->sw_if_index);
1426 if (sm->deterministic)
1428 rv = VNET_API_ERROR_UNSUPPORTED;
1432 is_del = !mp->is_add;
1434 VALIDATE_SW_IF_INDEX (mp);
1436 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1437 mp->flags & NAT_API_IS_TWICE_NAT);
1439 BAD_SW_IF_INDEX_LABEL;
1441 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1444 static void *vl_api_nat44_add_del_interface_addr_t_print
1445 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1449 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1450 s = format (s, "sw_if_index %d twice_nat %d %s",
1451 clib_host_to_net_u32 (mp->sw_if_index),
1452 mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1458 send_nat44_interface_addr_details (u32 sw_if_index,
1459 vl_api_registration_t * reg, u32 context,
1462 vl_api_nat44_interface_addr_details_t *rmp;
1463 snat_main_t *sm = &snat_main;
1465 rmp = vl_msg_api_alloc (sizeof (*rmp));
1466 clib_memset (rmp, 0, sizeof (*rmp));
1468 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1469 rmp->sw_if_index = ntohl (sw_if_index);
1472 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1473 rmp->context = context;
1475 vl_api_send_msg (reg, (u8 *) rmp);
1479 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1482 vl_api_registration_t *reg;
1483 snat_main_t *sm = &snat_main;
1486 if (sm->deterministic)
1489 reg = vl_api_client_index_to_registration (mp->client_index);
1494 vec_foreach (i, sm->auto_add_sw_if_indices)
1495 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1496 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1497 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1502 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1507 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1513 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1516 vl_api_nat44_user_details_t *rmp;
1517 snat_main_t *sm = &snat_main;
1518 ip4_main_t *im = &ip4_main;
1520 rmp = vl_msg_api_alloc (sizeof (*rmp));
1521 clib_memset (rmp, 0, sizeof (*rmp));
1522 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1524 if (!pool_is_free_index (im->fibs, u->fib_index))
1526 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1527 rmp->vrf_id = ntohl (fib->ft_table_id);
1530 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1531 rmp->nsessions = ntohl (u->nsessions);
1532 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1533 rmp->context = context;
1535 vl_api_send_msg (reg, (u8 *) rmp);
1539 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1541 vl_api_registration_t *reg;
1542 snat_main_t *sm = &snat_main;
1543 snat_main_per_thread_data_t *tsm;
1546 if (sm->deterministic)
1549 reg = vl_api_client_index_to_registration (mp->client_index);
1554 vec_foreach (tsm, sm->per_thread_data)
1556 pool_foreach (u, tsm->users,
1558 send_nat44_user_details (u, reg, mp->context);
1565 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1569 s = format (0, "SCRIPT: nat44_user_dump ");
1575 send_nat44_user_session_details (snat_session_t * s,
1576 vl_api_registration_t * reg, u32 context)
1578 vl_api_nat44_user_session_details_t *rmp;
1579 snat_main_t *sm = &snat_main;
1581 rmp = vl_msg_api_alloc (sizeof (*rmp));
1582 clib_memset (rmp, 0, sizeof (*rmp));
1584 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1585 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1586 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1588 if (snat_is_session_static (s))
1589 rmp->flags |= NAT_API_IS_STATIC;
1591 if (is_twice_nat_session (s))
1592 rmp->flags |= NAT_API_IS_TWICE_NAT;
1594 if (is_ed_session (s) || is_fwd_bypass_session (s))
1595 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1597 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1598 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1599 rmp->total_pkts = ntohl (s->total_pkts);
1600 rmp->context = context;
1601 if (snat_is_unk_proto_session (s))
1603 rmp->outside_port = 0;
1604 rmp->inside_port = 0;
1605 rmp->protocol = ntohs (s->in2out.port);
1609 rmp->outside_port = s->out2in.port;
1610 rmp->inside_port = s->in2out.port;
1611 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1613 if (is_ed_session (s) || is_fwd_bypass_session (s))
1615 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1616 rmp->ext_host_port = s->ext_host_port;
1617 if (is_twice_nat_session (s))
1619 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1620 rmp->ext_host_nat_port = s->ext_host_nat_port;
1624 vl_api_send_msg (reg, (u8 *) rmp);
1628 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1631 vl_api_registration_t *reg;
1632 snat_main_t *sm = &snat_main;
1633 snat_main_per_thread_data_t *tsm;
1635 clib_bihash_kv_8_8_t key, value;
1636 snat_user_key_t ukey;
1638 u32 session_index, head_index, elt_index;
1639 dlist_elt_t *head, *elt;
1642 if (sm->deterministic)
1645 reg = vl_api_client_index_to_registration (mp->client_index);
1649 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1650 ip.src_address.as_u32 = ukey.addr.as_u32;
1651 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1652 key.key = ukey.as_u64;
1653 if (sm->num_workers > 1)
1655 vec_elt_at_index (sm->per_thread_data,
1656 sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1658 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1659 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1661 u = pool_elt_at_index (tsm->users, value.value);
1662 if (!u->nsessions && !u->nstaticsessions)
1665 head_index = u->sessions_per_user_list_head_index;
1666 head = pool_elt_at_index (tsm->list_pool, head_index);
1667 elt_index = head->next;
1668 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1669 session_index = elt->value;
1670 while (session_index != ~0)
1672 s = pool_elt_at_index (tsm->sessions, session_index);
1674 send_nat44_user_session_details (s, reg, mp->context);
1676 elt_index = elt->next;
1677 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1678 session_index = elt->value;
1683 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1688 s = format (0, "SCRIPT: nat44_user_session_dump ");
1689 s = format (s, "ip_address %U vrf_id %d\n",
1690 format_ip4_address, mp->ip_address,
1691 clib_net_to_host_u32 (mp->vrf_id));
1696 static nat44_lb_addr_port_t *
1697 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1698 u32 addr_port_pair_num)
1701 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1702 vl_api_nat44_lb_addr_port_t *ap;
1704 for (i = 0; i < addr_port_pair_num; i++)
1706 ap = &addr_port_pairs[i];
1707 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1708 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1709 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1710 lb_addr_port.probability = ap->probability;
1711 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1712 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1715 return lb_addr_port_pairs;
1719 vl_api_nat44_add_del_lb_static_mapping_t_handler
1720 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1722 snat_main_t *sm = &snat_main;
1723 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1724 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1726 nat44_lb_addr_port_t *locals = 0;
1727 ip4_address_t e_addr;
1728 snat_protocol_t proto;
1731 if (!sm->endpoint_dependent)
1733 rv = VNET_API_ERROR_UNSUPPORTED;
1738 unformat_nat44_lb_addr_port (mp->locals,
1739 clib_net_to_host_u32 (mp->local_num));
1740 clib_memcpy (&e_addr, mp->external_addr, 4);
1741 proto = ip_proto_to_snat_proto (mp->protocol);
1743 if (mp->flags & NAT_API_IS_TWICE_NAT)
1744 twice_nat = TWICE_NAT;
1745 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1746 twice_nat = TWICE_NAT_SELF;
1747 mp->tag[sizeof (mp->tag) - 1] = 0;
1748 tag = format (0, "%s", mp->tag);
1749 vec_terminate_c_string (tag);
1752 nat44_add_del_lb_static_mapping (e_addr,
1753 clib_net_to_host_u16 (mp->external_port),
1754 proto, locals, mp->is_add,
1756 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1757 clib_net_to_host_u32 (mp->affinity));
1763 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1766 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1767 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1771 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1772 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1774 mp->flags & NAT_API_IS_TWICE_NAT,
1775 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1781 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1782 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1784 snat_main_t *sm = &snat_main;
1785 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1787 ip4_address_t e_addr, l_addr;
1788 snat_protocol_t proto;
1790 if (!sm->endpoint_dependent)
1792 rv = VNET_API_ERROR_UNSUPPORTED;
1796 clib_memcpy (&e_addr, mp->external_addr, 4);
1797 clib_memcpy (&l_addr, mp->local.addr, 4);
1798 proto = ip_proto_to_snat_proto (mp->protocol);
1801 nat44_lb_static_mapping_add_del_local (e_addr,
1802 clib_net_to_host_u16
1803 (mp->external_port), l_addr,
1804 clib_net_to_host_u16 (mp->
1807 clib_net_to_host_u32 (mp->
1809 mp->local.probability, mp->is_add);
1812 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1815 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
1816 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
1820 s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
1821 s = format (s, "is_add %d", mp->is_add);
1827 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1828 vl_api_registration_t * reg,
1831 vl_api_nat44_lb_static_mapping_details_t *rmp;
1832 snat_main_t *sm = &snat_main;
1833 nat44_lb_addr_port_t *ap;
1834 vl_api_nat44_lb_addr_port_t *locals;
1838 vl_msg_api_alloc (sizeof (*rmp) +
1839 (pool_elts (m->locals) *
1840 sizeof (nat44_lb_addr_port_t)));
1841 clib_memset (rmp, 0, sizeof (*rmp));
1843 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1845 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1846 rmp->external_port = ntohs (m->external_port);
1847 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1848 rmp->context = context;
1850 if (m->twice_nat == TWICE_NAT)
1851 rmp->flags |= NAT_API_IS_TWICE_NAT;
1852 else if (m->twice_nat == TWICE_NAT_SELF)
1853 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1854 if (is_out2in_only_static_mapping (m))
1855 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1857 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1859 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1861 pool_foreach (ap, m->locals,
1863 clib_memcpy (locals->addr, &(ap->addr), 4);
1864 locals->port = htons (ap->port);
1865 locals->probability = ap->probability;
1866 locals->vrf_id = ntohl (ap->vrf_id);
1871 rmp->local_num = ntohl (local_num);
1873 vl_api_send_msg (reg, (u8 *) rmp);
1877 vl_api_nat44_lb_static_mapping_dump_t_handler
1878 (vl_api_nat44_lb_static_mapping_dump_t * mp)
1880 vl_api_registration_t *reg;
1881 snat_main_t *sm = &snat_main;
1882 snat_static_mapping_t *m;
1884 if (!sm->endpoint_dependent)
1887 reg = vl_api_client_index_to_registration (mp->client_index);
1892 pool_foreach (m, sm->static_mappings,
1894 if (is_lb_static_mapping(m))
1895 send_nat44_lb_static_mapping_details (m, reg, mp->context);
1900 static void *vl_api_nat44_lb_static_mapping_dump_t_print
1901 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
1905 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
1911 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1913 snat_main_t *sm = &snat_main;
1914 vl_api_nat44_del_session_reply_t *rmp;
1915 ip4_address_t addr, eh_addr;
1920 snat_protocol_t proto;
1922 if (sm->deterministic)
1924 rv = VNET_API_ERROR_UNSUPPORTED;
1928 memcpy (&addr.as_u8, mp->address, 4);
1929 port = clib_net_to_host_u16 (mp->port);
1930 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1931 proto = ip_proto_to_snat_proto (mp->protocol);
1932 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1933 eh_port = clib_net_to_host_u16 (mp->ext_host_port);
1935 is_in = mp->flags & NAT_API_IS_INSIDE;
1937 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1939 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
1942 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
1945 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1949 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
1954 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1955 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
1956 format_ip4_address, mp->address,
1957 clib_net_to_host_u16 (mp->port),
1958 mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
1959 mp->flags & NAT_API_IS_INSIDE);
1960 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1961 s = format (s, "ext_host_address %U ext_host_port %d",
1962 format_ip4_address, mp->ext_host_address,
1963 clib_net_to_host_u16 (mp->ext_host_port));
1969 vl_api_nat44_forwarding_enable_disable_t_handler
1970 (vl_api_nat44_forwarding_enable_disable_t * mp)
1972 snat_main_t *sm = &snat_main;
1973 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1975 u32 *ses_to_be_removed = 0, *ses_index;
1976 snat_main_per_thread_data_t *tsm;
1979 sm->forwarding_enabled = mp->enable != 0;
1981 if (mp->enable == 0)
1984 vec_foreach (tsm, sm->per_thread_data)
1986 pool_foreach (s, tsm->sessions,
1988 if (is_fwd_bypass_session(s))
1990 vec_add1 (ses_to_be_removed, s - tsm->sessions);
1993 vec_foreach (ses_index, ses_to_be_removed)
1995 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1996 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1997 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1999 vec_free (ses_to_be_removed);
2004 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2007 static void *vl_api_nat44_forwarding_enable_disable_t_print
2008 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2012 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2013 s = format (s, "enable %d", mp->enable != 0);
2019 vl_api_nat44_forwarding_is_enabled_t_handler
2020 (vl_api_nat44_forwarding_is_enabled_t * mp)
2022 vl_api_registration_t *reg;
2023 snat_main_t *sm = &snat_main;
2024 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2026 reg = vl_api_client_index_to_registration (mp->client_index);
2030 rmp = vl_msg_api_alloc (sizeof (*rmp));
2031 clib_memset (rmp, 0, sizeof (*rmp));
2033 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2034 rmp->context = mp->context;
2036 rmp->enabled = sm->forwarding_enabled;
2038 vl_api_send_msg (reg, (u8 *) rmp);
2041 static void *vl_api_nat44_forwarding_is_enabled_t_print
2042 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2046 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2051 /*******************************/
2052 /*** Deterministic NAT (CGN) ***/
2053 /*******************************/
2056 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
2058 snat_main_t *sm = &snat_main;
2059 vl_api_nat_det_add_del_map_reply_t *rmp;
2061 ip4_address_t in_addr, out_addr;
2063 if (!sm->deterministic)
2065 rv = VNET_API_ERROR_UNSUPPORTED;
2069 clib_memcpy (&in_addr, mp->in_addr, 4);
2070 clib_memcpy (&out_addr, mp->out_addr, 4);
2071 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
2072 mp->out_plen, mp->is_add);
2075 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
2079 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
2084 s = format (0, "SCRIPT: nat_det_add_del_map ");
2085 s = format (s, "inside address %U/%d outside address %U/%d\n",
2086 format_ip4_address, mp->in_addr, mp->in_plen,
2087 format_ip4_address, mp->out_addr, mp->out_plen);
2093 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
2095 snat_main_t *sm = &snat_main;
2096 vl_api_nat_det_forward_reply_t *rmp;
2098 u16 lo_port = 0, hi_port = 0;
2100 ip4_address_t in_addr, out_addr;
2102 if (!sm->deterministic)
2104 rv = VNET_API_ERROR_UNSUPPORTED;
2105 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
2109 out_addr.as_u32 = 0;
2110 clib_memcpy (&in_addr, mp->in_addr, 4);
2111 dm = snat_det_map_by_user (sm, &in_addr);
2114 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2118 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2119 hi_port = lo_port + dm->ports_per_host - 1;
2123 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
2125 rmp->out_port_lo = ntohs (lo_port);
2126 rmp->out_port_hi = ntohs (hi_port);
2127 clib_memcpy (rmp->out_addr, &out_addr, 4);
2133 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
2137 s = format (0, "SCRIPT: nat_det_forward");
2138 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
2144 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
2146 snat_main_t *sm = &snat_main;
2147 vl_api_nat_det_reverse_reply_t *rmp;
2149 ip4_address_t out_addr, in_addr;
2152 if (!sm->deterministic)
2154 rv = VNET_API_ERROR_UNSUPPORTED;
2155 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
2160 clib_memcpy (&out_addr, mp->out_addr, 4);
2161 dm = snat_det_map_by_out (sm, &out_addr);
2164 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2168 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
2172 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
2174 clib_memcpy (rmp->in_addr, &in_addr, 4);
2180 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
2184 s = format (0, "SCRIPT: nat_det_reverse");
2185 s = format (s, "outside ip address %U outside port %d",
2186 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
2192 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
2195 vl_api_nat_det_map_details_t *rmp;
2196 snat_main_t *sm = &snat_main;
2198 rmp = vl_msg_api_alloc (sizeof (*rmp));
2199 clib_memset (rmp, 0, sizeof (*rmp));
2200 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
2201 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
2202 rmp->in_plen = m->in_plen;
2203 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
2204 rmp->out_plen = m->out_plen;
2205 rmp->sharing_ratio = htonl (m->sharing_ratio);
2206 rmp->ports_per_host = htons (m->ports_per_host);
2207 rmp->ses_num = htonl (m->ses_num);
2208 rmp->context = context;
2210 vl_api_send_msg (reg, (u8 *) rmp);
2214 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
2216 vl_api_registration_t *reg;
2217 snat_main_t *sm = &snat_main;
2220 if (!sm->deterministic)
2223 reg = vl_api_client_index_to_registration (mp->client_index);
2228 vec_foreach(m, sm->det_maps)
2229 sent_nat_det_map_details(m, reg, mp->context);
2234 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
2238 s = format (0, "SCRIPT: nat_det_map_dump ");
2244 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
2247 snat_main_t *sm = &snat_main;
2248 vl_api_nat_det_close_session_out_reply_t *rmp;
2249 ip4_address_t out_addr, ext_addr, in_addr;
2250 snat_det_out_key_t key;
2252 snat_det_session_t *ses;
2255 if (!sm->deterministic)
2257 rv = VNET_API_ERROR_UNSUPPORTED;
2261 clib_memcpy (&out_addr, mp->out_addr, 4);
2262 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2264 dm = snat_det_map_by_out (sm, &out_addr);
2267 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2270 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
2271 key.ext_host_addr = ext_addr;
2272 key.ext_host_port = mp->ext_port;
2273 key.out_port = mp->out_port;
2274 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
2277 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2280 snat_det_ses_close (dm, ses);
2283 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2287 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
2292 s = format (0, "SCRIPT: nat_det_close_session_out ");
2293 s = format (s, "out_addr %U out_port %d "
2294 "ext_addr %U ext_port %d\n",
2295 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
2296 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2302 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
2305 snat_main_t *sm = &snat_main;
2306 vl_api_nat_det_close_session_in_reply_t *rmp;
2307 ip4_address_t in_addr, ext_addr;
2308 snat_det_out_key_t key;
2310 snat_det_session_t *ses;
2313 if (!sm->deterministic)
2315 rv = VNET_API_ERROR_UNSUPPORTED;
2319 clib_memcpy (&in_addr, mp->in_addr, 4);
2320 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2322 dm = snat_det_map_by_user (sm, &in_addr);
2325 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2328 key.ext_host_addr = ext_addr;
2329 key.ext_host_port = mp->ext_port;
2330 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
2333 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2336 snat_det_ses_close (dm, ses);
2339 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2343 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
2347 s = format (0, "SCRIPT: nat_det_close_session_in ");
2348 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
2349 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
2350 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2356 send_nat_det_session_details (snat_det_session_t * s,
2357 vl_api_registration_t * reg, u32 context)
2359 vl_api_nat_det_session_details_t *rmp;
2360 snat_main_t *sm = &snat_main;
2362 rmp = vl_msg_api_alloc (sizeof (*rmp));
2363 clib_memset (rmp, 0, sizeof (*rmp));
2364 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
2365 rmp->in_port = s->in_port;
2366 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
2367 rmp->ext_port = s->out.ext_host_port;
2368 rmp->out_port = s->out.out_port;
2369 rmp->state = s->state;
2370 rmp->expire = ntohl (s->expire);
2371 rmp->context = context;
2373 vl_api_send_msg (reg, (u8 *) rmp);
2377 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
2379 vl_api_registration_t *reg;
2380 snat_main_t *sm = &snat_main;
2381 ip4_address_t user_addr;
2383 snat_det_session_t *s, empty_ses;
2386 if (!sm->deterministic)
2389 reg = vl_api_client_index_to_registration (mp->client_index);
2393 clib_memset (&empty_ses, 0, sizeof (empty_ses));
2394 clib_memcpy (&user_addr, mp->user_addr, 4);
2395 dm = snat_det_map_by_user (sm, &user_addr);
2399 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2400 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2403 send_nat_det_session_details (s, reg, mp->context);
2409 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2414 s = format (0, "SCRIPT: nat_det_session_dump ");
2415 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2425 vl_api_nat64_add_del_pool_addr_range_t_handler
2426 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2428 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2429 snat_main_t *sm = &snat_main;
2431 ip4_address_t this_addr;
2432 u32 start_host_order, end_host_order;
2437 tmp = (u32 *) mp->start_addr;
2438 start_host_order = clib_host_to_net_u32 (tmp[0]);
2439 tmp = (u32 *) mp->end_addr;
2440 end_host_order = clib_host_to_net_u32 (tmp[0]);
2442 count = (end_host_order - start_host_order) + 1;
2444 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2446 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2448 for (i = 0; i < count; i++)
2450 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2453 increment_v4_address (&this_addr);
2457 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2460 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2461 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2465 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2466 s = format (s, "%U - %U vrf_id %u %s\n",
2467 format_ip4_address, mp->start_addr,
2468 format_ip4_address, mp->end_addr,
2469 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2474 typedef struct nat64_api_walk_ctx_t_
2476 vl_api_registration_t *reg;
2479 } nat64_api_walk_ctx_t;
2482 nat64_api_pool_walk (snat_address_t * a, void *arg)
2484 vl_api_nat64_pool_addr_details_t *rmp;
2485 snat_main_t *sm = &snat_main;
2486 nat64_api_walk_ctx_t *ctx = arg;
2488 rmp = vl_msg_api_alloc (sizeof (*rmp));
2489 clib_memset (rmp, 0, sizeof (*rmp));
2490 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2491 clib_memcpy (rmp->address, &(a->addr), 4);
2492 if (a->fib_index != ~0)
2494 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2497 rmp->vrf_id = ntohl (fib->ft_table_id);
2501 rmp->context = ctx->context;
2503 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2509 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2511 vl_api_registration_t *reg;
2513 reg = vl_api_client_index_to_registration (mp->client_index);
2517 nat64_api_walk_ctx_t ctx = {
2519 .context = mp->context,
2522 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2526 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2531 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2537 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2540 snat_main_t *sm = &snat_main;
2541 vl_api_nat64_add_del_interface_reply_t *rmp;
2544 VALIDATE_SW_IF_INDEX (mp);
2547 nat64_add_del_interface (ntohl (mp->sw_if_index),
2548 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2550 BAD_SW_IF_INDEX_LABEL;
2552 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2556 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2561 s = format (0, "SCRIPT: nat64_add_del_interface ");
2562 s = format (s, "sw_if_index %d %s %s",
2563 clib_host_to_net_u32 (mp->sw_if_index),
2564 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2565 mp->is_add ? "" : "del");
2571 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2573 vl_api_nat64_interface_details_t *rmp;
2574 snat_main_t *sm = &snat_main;
2575 nat64_api_walk_ctx_t *ctx = arg;
2577 rmp = vl_msg_api_alloc (sizeof (*rmp));
2578 clib_memset (rmp, 0, sizeof (*rmp));
2579 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2580 rmp->sw_if_index = ntohl (i->sw_if_index);
2582 if (nat_interface_is_inside (i))
2583 rmp->flags |= NAT_API_IS_INSIDE;
2584 if (nat_interface_is_outside (i))
2585 rmp->flags |= NAT_API_IS_OUTSIDE;
2587 rmp->context = ctx->context;
2589 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2595 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2597 vl_api_registration_t *reg;
2599 reg = vl_api_client_index_to_registration (mp->client_index);
2603 nat64_api_walk_ctx_t ctx = {
2605 .context = mp->context,
2608 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2612 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2617 s = format (0, "SCRIPT: snat_interface_dump ");
2623 vl_api_nat64_add_del_static_bib_t_handler
2624 (vl_api_nat64_add_del_static_bib_t * mp)
2626 snat_main_t *sm = &snat_main;
2627 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2628 ip6_address_t in_addr;
2629 ip4_address_t out_addr;
2632 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2633 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2636 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2637 clib_net_to_host_u16 (mp->i_port),
2638 clib_net_to_host_u16 (mp->o_port),
2640 clib_net_to_host_u32 (mp->vrf_id),
2643 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2646 static void *vl_api_nat64_add_del_static_bib_t_print
2647 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2651 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2652 s = format (s, "protocol %d i_addr %U o_addr %U ",
2654 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2656 if (mp->vrf_id != ~0)
2657 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2663 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2665 vl_api_nat64_bib_details_t *rmp;
2666 snat_main_t *sm = &snat_main;
2667 nat64_api_walk_ctx_t *ctx = arg;
2670 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2674 rmp = vl_msg_api_alloc (sizeof (*rmp));
2675 clib_memset (rmp, 0, sizeof (*rmp));
2676 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2677 rmp->context = ctx->context;
2678 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2679 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2680 rmp->i_port = bibe->in_port;
2681 rmp->o_port = bibe->out_port;
2682 rmp->vrf_id = ntohl (fib->ft_table_id);
2683 rmp->proto = bibe->proto;
2684 if (bibe->is_static)
2685 rmp->flags |= NAT_API_IS_STATIC;
2686 rmp->ses_num = ntohl (bibe->ses_num);
2688 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2694 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2696 vl_api_registration_t *reg;
2697 nat64_main_t *nm = &nat64_main;
2700 reg = vl_api_client_index_to_registration (mp->client_index);
2704 nat64_api_walk_ctx_t ctx = {
2706 .context = mp->context,
2710 vec_foreach (db, nm->db)
2711 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2716 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2720 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2726 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2728 vl_api_nat64_st_details_t *rmp;
2729 snat_main_t *sm = &snat_main;
2730 nat64_api_walk_ctx_t *ctx = arg;
2731 nat64_db_bib_entry_t *bibe;
2734 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2738 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2742 rmp = vl_msg_api_alloc (sizeof (*rmp));
2743 clib_memset (rmp, 0, sizeof (*rmp));
2744 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2745 rmp->context = ctx->context;
2746 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2747 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2748 rmp->il_port = bibe->in_port;
2749 rmp->ol_port = bibe->out_port;
2750 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2751 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2752 rmp->il_port = ste->r_port;
2753 rmp->vrf_id = ntohl (fib->ft_table_id);
2754 rmp->proto = ste->proto;
2756 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2762 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2764 vl_api_registration_t *reg;
2765 nat64_main_t *nm = &nat64_main;
2768 reg = vl_api_client_index_to_registration (mp->client_index);
2772 nat64_api_walk_ctx_t ctx = {
2774 .context = mp->context,
2778 vec_foreach (db, nm->db)
2781 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2787 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2791 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2797 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2799 vl_api_nat64_add_del_prefix_reply_t *rmp;
2800 snat_main_t *sm = &snat_main;
2801 ip6_address_t prefix;
2804 memcpy (&prefix.as_u8, mp->prefix.address, 16);
2807 nat64_add_del_prefix (&prefix, mp->prefix.len,
2808 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2809 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2813 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2818 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2819 format_ip6_address, mp->prefix.address, mp->prefix.len,
2820 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2826 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2828 vl_api_nat64_prefix_details_t *rmp;
2829 snat_main_t *sm = &snat_main;
2830 nat64_api_walk_ctx_t *ctx = arg;
2832 rmp = vl_msg_api_alloc (sizeof (*rmp));
2833 clib_memset (rmp, 0, sizeof (*rmp));
2834 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2835 clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
2836 rmp->prefix.len = p->plen;
2837 rmp->vrf_id = ntohl (p->vrf_id);
2838 rmp->context = ctx->context;
2840 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2846 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2848 vl_api_registration_t *reg;
2850 reg = vl_api_client_index_to_registration (mp->client_index);
2854 nat64_api_walk_ctx_t ctx = {
2856 .context = mp->context,
2859 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2863 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2868 s = format (0, "SCRIPT: nat64_prefix_dump\n");
2874 vl_api_nat64_add_del_interface_addr_t_handler
2875 (vl_api_nat64_add_del_interface_addr_t * mp)
2877 snat_main_t *sm = &snat_main;
2878 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2879 u32 sw_if_index = ntohl (mp->sw_if_index);
2882 VALIDATE_SW_IF_INDEX (mp);
2884 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2886 BAD_SW_IF_INDEX_LABEL;
2888 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2891 static void *vl_api_nat64_add_del_interface_addr_t_print
2892 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2896 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2897 s = format (s, "sw_if_index %d %s",
2898 clib_host_to_net_u32 (mp->sw_if_index),
2899 mp->is_add ? "" : "del");
2909 vl_api_dslite_set_aftr_addr_t_handler (vl_api_dslite_set_aftr_addr_t * mp)
2911 vl_api_dslite_set_aftr_addr_reply_t *rmp;
2912 snat_main_t *sm = &snat_main;
2913 dslite_main_t *dm = &dslite_main;
2915 ip6_address_t ip6_addr;
2916 ip4_address_t ip4_addr;
2918 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2919 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2921 rv = dslite_set_aftr_ip6_addr (dm, &ip6_addr);
2923 rv = dslite_set_aftr_ip4_addr (dm, &ip4_addr);
2925 REPLY_MACRO (VL_API_DSLITE_SET_AFTR_ADDR_REPLY);
2929 vl_api_dslite_set_aftr_addr_t_print (vl_api_dslite_set_aftr_addr_t * mp,
2934 s = format (0, "SCRIPT: dslite_set_aftr_addr ");
2935 s = format (s, "ip6_addr %U ip4_addr %U\n",
2936 format_ip6_address, mp->ip6_addr,
2937 format_ip4_address, mp->ip4_addr);
2943 vl_api_dslite_get_aftr_addr_t_handler (vl_api_dslite_get_aftr_addr_t * mp)
2945 snat_main_t *sm = &snat_main;
2946 vl_api_dslite_get_aftr_addr_reply_t *rmp;
2947 dslite_main_t *dm = &dslite_main;
2951 REPLY_MACRO2 (VL_API_DSLITE_GET_AFTR_ADDR_REPLY,
2953 memcpy (rmp->ip4_addr, &dm->aftr_ip4_addr.as_u8, 4);
2954 memcpy (rmp->ip6_addr, &dm->aftr_ip6_addr.as_u8, 16);
2960 vl_api_dslite_get_aftr_addr_t_print (vl_api_dslite_get_aftr_addr_t * mp,
2965 s = format (0, "SCRIPT: dslite_get_aftr_addr");
2971 vl_api_dslite_set_b4_addr_t_handler (vl_api_dslite_set_b4_addr_t * mp)
2973 vl_api_dslite_set_b4_addr_reply_t *rmp;
2974 snat_main_t *sm = &snat_main;
2975 dslite_main_t *dm = &dslite_main;
2977 ip6_address_t ip6_addr;
2978 ip4_address_t ip4_addr;
2980 memcpy (&ip6_addr.as_u8, mp->ip6_addr, 16);
2981 memcpy (&ip4_addr.as_u8, mp->ip4_addr, 4);
2983 rv = dslite_set_b4_ip6_addr (dm, &ip6_addr);
2985 rv = dslite_set_b4_ip4_addr (dm, &ip4_addr);
2987 REPLY_MACRO (VL_API_DSLITE_SET_B4_ADDR_REPLY);
2991 vl_api_dslite_set_b4_addr_t_print (vl_api_dslite_set_b4_addr_t * mp,
2996 s = format (0, "SCRIPT: dslite_set_b4_addr ");
2997 s = format (s, "ip6_addr %U ip4_addr %U\n",
2998 format_ip6_address, mp->ip6_addr,
2999 format_ip6_address, mp->ip4_addr);
3005 vl_api_dslite_get_b4_addr_t_handler (vl_api_dslite_get_b4_addr_t * mp)
3007 snat_main_t *sm = &snat_main;
3008 vl_api_dslite_get_b4_addr_reply_t *rmp;
3009 dslite_main_t *dm = &dslite_main;
3013 REPLY_MACRO2 (VL_API_DSLITE_GET_B4_ADDR_REPLY,
3015 memcpy (rmp->ip4_addr, &dm->b4_ip4_addr.as_u8, 4);
3016 memcpy (rmp->ip6_addr, &dm->b4_ip6_addr.as_u8, 16);
3022 vl_api_dslite_get_b4_addr_t_print (vl_api_dslite_get_b4_addr_t * mp,
3027 s = format (0, "SCRIPT: dslite_get_b4_addr");
3033 vl_api_dslite_add_del_pool_addr_range_t_handler
3034 (vl_api_dslite_add_del_pool_addr_range_t * mp)
3036 vl_api_dslite_add_del_pool_addr_range_reply_t *rmp;
3037 snat_main_t *sm = &snat_main;
3038 dslite_main_t *dm = &dslite_main;
3040 ip4_address_t this_addr;
3041 u32 start_host_order, end_host_order;
3045 tmp = (u32 *) mp->start_addr;
3046 start_host_order = clib_host_to_net_u32 (tmp[0]);
3047 tmp = (u32 *) mp->end_addr;
3048 end_host_order = clib_host_to_net_u32 (tmp[0]);
3050 count = (end_host_order - start_host_order) + 1;
3051 memcpy (&this_addr.as_u8, mp->start_addr, 4);
3053 for (i = 0; i < count; i++)
3055 if ((rv = dslite_add_del_pool_addr (dm, &this_addr, mp->is_add)))
3058 increment_v4_address (&this_addr);
3062 REPLY_MACRO (VL_API_DSLITE_ADD_DEL_POOL_ADDR_RANGE_REPLY);
3066 send_dslite_address_details (snat_address_t * ap,
3067 vl_api_registration_t * reg, u32 context)
3069 vl_api_dslite_address_details_t *rmp;
3070 snat_main_t *sm = &snat_main;
3072 rmp = vl_msg_api_alloc (sizeof (*rmp));
3074 clib_memset (rmp, 0, sizeof (*rmp));
3076 rmp->_vl_msg_id = ntohs (VL_API_DSLITE_ADDRESS_DETAILS + sm->msg_id_base);
3077 clib_memcpy (rmp->ip_address, &(ap->addr), 4);
3078 rmp->context = context;
3080 vl_api_send_msg (reg, (u8 *) rmp);
3084 vl_api_dslite_address_dump_t_handler (vl_api_dslite_address_dump_t * mp)
3086 vl_api_registration_t *reg;
3087 dslite_main_t *dm = &dslite_main;
3090 reg = vl_api_client_index_to_registration (mp->client_index);
3095 vec_foreach (ap, dm->addr_pool)
3097 send_dslite_address_details (ap, reg, mp->context);
3103 vl_api_dslite_address_dump_t_print (vl_api_dslite_address_dump_t * mp,
3108 s = format (0, "SCRIPT: dslite_address_dump ");
3113 static void *vl_api_dslite_add_del_pool_addr_range_t_print
3114 (vl_api_dslite_add_del_pool_addr_range_t * mp, void *handle)
3118 s = format (0, "SCRIPT: dslite_add_del_pool_addr_range ");
3119 s = format (s, "%U - %U\n",
3120 format_ip4_address, mp->start_addr,
3121 format_ip4_address, mp->end_addr);
3132 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
3135 snat_main_t *sm = &snat_main;
3136 vl_api_nat66_add_del_interface_reply_t *rmp;
3139 VALIDATE_SW_IF_INDEX (mp);
3142 nat66_interface_add_del (ntohl (mp->sw_if_index),
3143 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
3145 BAD_SW_IF_INDEX_LABEL;
3147 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
3151 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
3156 s = format (0, "SCRIPT: nat66_add_del_interface ");
3157 s = format (s, "sw_if_index %d %s %s",
3158 clib_host_to_net_u32 (mp->sw_if_index),
3159 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
3160 mp->is_add ? "" : "del");
3166 vl_api_nat66_add_del_static_mapping_t_handler
3167 (vl_api_nat66_add_del_static_mapping_t * mp)
3169 snat_main_t *sm = &snat_main;
3170 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
3171 ip6_address_t l_addr, e_addr;
3174 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
3175 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
3178 nat66_static_mapping_add_del (&l_addr, &e_addr,
3179 clib_net_to_host_u32 (mp->vrf_id),
3182 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
3185 static void *vl_api_nat66_add_del_static_mapping_t_print
3186 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
3190 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
3191 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
3192 format_ip6_address, mp->local_ip_address,
3193 format_ip6_address, mp->external_ip_address,
3194 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
3199 typedef struct nat66_api_walk_ctx_t_
3201 vl_api_registration_t *rp;
3203 } nat66_api_walk_ctx_t;
3206 nat66_api_interface_walk (snat_interface_t * i, void *arg)
3208 vl_api_nat66_interface_details_t *rmp;
3209 snat_main_t *sm = &snat_main;
3210 nat66_api_walk_ctx_t *ctx = arg;
3212 rmp = vl_msg_api_alloc (sizeof (*rmp));
3213 clib_memset (rmp, 0, sizeof (*rmp));
3214 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
3215 rmp->sw_if_index = ntohl (i->sw_if_index);
3216 if (nat_interface_is_inside (i))
3217 rmp->flags |= NAT_API_IS_INSIDE;
3218 rmp->context = ctx->context;
3220 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3226 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
3228 vl_api_registration_t *rp;
3230 rp = vl_api_client_index_to_registration (mp->client_index);
3234 nat66_api_walk_ctx_t ctx = {
3236 .context = mp->context,
3239 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
3243 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
3248 s = format (0, "SCRIPT: nat66_interface_dump ");
3254 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
3256 vl_api_nat66_static_mapping_details_t *rmp;
3257 nat66_main_t *nm = &nat66_main;
3258 snat_main_t *sm = &snat_main;
3259 nat66_api_walk_ctx_t *ctx = arg;
3263 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
3267 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
3269 rmp = vl_msg_api_alloc (sizeof (*rmp));
3270 clib_memset (rmp, 0, sizeof (*rmp));
3272 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
3273 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
3274 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
3275 rmp->vrf_id = ntohl (fib->ft_table_id);
3276 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
3277 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
3278 rmp->context = ctx->context;
3280 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3286 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
3289 vl_api_registration_t *rp;
3291 rp = vl_api_client_index_to_registration (mp->client_index);
3295 nat66_api_walk_ctx_t ctx = {
3297 .context = mp->context,
3300 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
3304 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
3309 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
3315 /* List of message types that this plugin understands */
3316 #define foreach_snat_plugin_api_msg \
3317 _(NAT_CONTROL_PING, nat_control_ping) \
3318 _(NAT_SHOW_CONFIG, nat_show_config) \
3319 _(NAT_SET_WORKERS, nat_set_workers) \
3320 _(NAT_WORKER_DUMP, nat_worker_dump) \
3321 _(NAT_SET_LOG_LEVEL, nat_set_log_level) \
3322 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
3323 _(NAT_SET_TIMEOUTS, nat_set_timeouts) \
3324 _(NAT_GET_TIMEOUTS, nat_get_timeouts) \
3325 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg) \
3326 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg) \
3327 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping) \
3328 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping) \
3329 _(NAT_HA_SET_LISTENER, nat_ha_set_listener) \
3330 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover) \
3331 _(NAT_HA_GET_LISTENER, nat_ha_get_listener) \
3332 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover) \
3333 _(NAT_HA_FLUSH, nat_ha_flush) \
3334 _(NAT_HA_RESYNC, nat_ha_resync) \
3335 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
3336 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
3337 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
3338 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3339 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3340 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3341 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3342 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3343 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3344 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3345 _(NAT44_USER_DUMP, nat44_user_dump) \
3346 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3347 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3348 nat44_interface_add_del_output_feature) \
3349 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3350 nat44_interface_output_feature_dump) \
3351 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3352 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL, \
3353 nat44_lb_static_mapping_add_del_local) \
3354 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3355 _(NAT44_DEL_SESSION, nat44_del_session) \
3356 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3357 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3358 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3359 _(NAT_DET_FORWARD, nat_det_forward) \
3360 _(NAT_DET_REVERSE, nat_det_reverse) \
3361 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3362 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3363 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3364 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3365 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3366 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3367 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3368 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3369 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3370 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3371 _(NAT64_ST_DUMP, nat64_st_dump) \
3372 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3373 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3374 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3375 _(DSLITE_ADD_DEL_POOL_ADDR_RANGE, dslite_add_del_pool_addr_range) \
3376 _(DSLITE_ADDRESS_DUMP, dslite_address_dump) \
3377 _(DSLITE_SET_AFTR_ADDR, dslite_set_aftr_addr) \
3378 _(DSLITE_GET_AFTR_ADDR, dslite_get_aftr_addr) \
3379 _(DSLITE_SET_B4_ADDR, dslite_set_b4_addr) \
3380 _(DSLITE_GET_B4_ADDR, dslite_get_b4_addr) \
3381 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3382 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3383 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3384 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3386 /* Set up the API message handling tables */
3387 static clib_error_t *
3388 snat_plugin_api_hookup (vlib_main_t * vm)
3390 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3392 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3394 vl_api_##n##_t_handler, \
3396 vl_api_##n##_t_endian, \
3397 vl_api_##n##_t_print, \
3398 sizeof(vl_api_##n##_t), 1);
3399 foreach_snat_plugin_api_msg;
3405 #define vl_msg_name_crc_list
3406 #include <nat/nat_all_api_h.h>
3407 #undef vl_msg_name_crc_list
3410 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3412 #define _(id,n,crc) \
3413 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3414 foreach_vl_msg_name_crc_nat;
3419 plugin_custom_dump_configure (snat_main_t * sm)
3421 #define _(n,f) sm->api_main->msg_print_handlers \
3422 [VL_API_##n + sm->msg_id_base] \
3423 = (void *) vl_api_##f##_t_print;
3424 foreach_snat_plugin_api_msg;
3429 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3432 clib_error_t *error = 0;
3434 name = format (0, "nat_%08x%c", api_version, 0);
3436 /* Ask for a correctly-sized block of API message decode slots */
3438 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3440 error = snat_plugin_api_hookup (vm);
3442 /* Add our API messages to the global name_crc hash table */
3443 setup_message_id_table (sm, sm->api_main);
3445 plugin_custom_dump_configure (sm);
3453 * fd.io coding-style-patch-verification: ON
3456 * eval: (c-set-style "gnu")