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/nat_inlines.h>
26 #include <nat/nat44/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 = clib_host_to_net_u64 (sm->translation_memory_size);
108 rmp->user_buckets = htonl (sm->user_buckets);
109 rmp->user_memory_size = clib_host_to_net_u64 (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 = clib_net_to_host_u32(n64m->bib_buckets);
121 rmp->nat64_bib_memory_size = clib_net_to_host_u64(n64m->bib_memory_size);
122 rmp->nat64_st_buckets = clib_net_to_host_u32(n64m->st_buckets);
123 rmp->nat64_st_memory_size = clib_net_to_host_u64(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_del_user_t_handler (vl_api_nat44_del_user_t * mp)
722 snat_main_t *sm = &snat_main;
723 vl_api_nat44_del_user_reply_t *rmp;
726 memcpy (&addr.as_u8, mp->ip_address, 4);
727 rv = nat44_user_del (&addr, ntohl (mp->fib_index));
728 REPLY_MACRO (VL_API_NAT44_DEL_USER_REPLY);
731 static void *vl_api_nat44_del_user_t_print
732 (vl_api_nat44_del_user_t * mp, void *handle)
735 s = format (0, "SCRIPT: nat44_del_user ");
736 s = format (s, "ip_address %U fib_index %U ",
737 format_ip4_address, mp->ip_address, ntohl (mp->fib_index));
742 vl_api_nat44_add_del_address_range_t_handler
743 (vl_api_nat44_add_del_address_range_t * mp)
745 snat_main_t *sm = &snat_main;
746 vl_api_nat44_add_del_address_range_reply_t *rmp;
747 ip4_address_t this_addr;
748 u8 is_add, twice_nat;
749 u32 start_host_order, end_host_order;
755 if (sm->deterministic)
757 rv = VNET_API_ERROR_UNSUPPORTED;
761 if (sm->static_mapping_only)
763 rv = VNET_API_ERROR_FEATURE_DISABLED;
768 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
770 tmp = (u32 *) mp->first_ip_address;
771 start_host_order = clib_host_to_net_u32 (tmp[0]);
772 tmp = (u32 *) mp->last_ip_address;
773 end_host_order = clib_host_to_net_u32 (tmp[0]);
775 count = (end_host_order - start_host_order) + 1;
777 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
780 nat_log_info ("%U - %U, %d addresses...",
781 format_ip4_address, mp->first_ip_address,
782 format_ip4_address, mp->last_ip_address, count);
784 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
786 for (i = 0; i < count; i++)
789 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
791 rv = snat_del_address (sm, this_addr, 0, twice_nat);
797 nat44_add_del_address_dpo (this_addr, is_add);
799 increment_v4_address (&this_addr);
803 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
806 static void *vl_api_nat44_add_del_address_range_t_print
807 (vl_api_nat44_add_del_address_range_t * mp, void *handle)
811 s = format (0, "SCRIPT: nat44_add_address_range ");
812 s = format (s, "%U ", format_ip4_address, mp->first_ip_address);
813 if (memcmp (mp->first_ip_address, mp->last_ip_address, 4))
815 s = format (s, " - %U ", format_ip4_address, mp->last_ip_address);
817 s = format (s, "twice_nat %d ", mp->flags & NAT_API_IS_TWICE_NAT);
822 send_nat44_address_details (snat_address_t * a,
823 vl_api_registration_t * reg, u32 context,
826 vl_api_nat44_address_details_t *rmp;
827 snat_main_t *sm = &snat_main;
829 rmp = vl_msg_api_alloc (sizeof (*rmp));
830 clib_memset (rmp, 0, sizeof (*rmp));
831 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
832 clib_memcpy (rmp->ip_address, &(a->addr), 4);
833 if (a->fib_index != ~0)
835 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
836 rmp->vrf_id = ntohl (fib->ft_table_id);
841 rmp->flags |= NAT_API_IS_TWICE_NAT;
842 rmp->context = context;
844 vl_api_send_msg (reg, (u8 *) rmp);
848 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
850 vl_api_registration_t *reg;
851 snat_main_t *sm = &snat_main;
854 if (sm->deterministic)
857 reg = vl_api_client_index_to_registration (mp->client_index);
862 vec_foreach (a, sm->addresses)
863 send_nat44_address_details (a, reg, mp->context, 0);
864 vec_foreach (a, sm->twice_nat_addresses)
865 send_nat44_address_details (a, reg, mp->context, 1);
870 vl_api_nat44_address_dump_t_print (vl_api_nat44_address_dump_t * mp,
875 s = format (0, "SCRIPT: nat44_address_dump ");
881 vl_api_nat44_interface_add_del_feature_t_handler
882 (vl_api_nat44_interface_add_del_feature_t * mp)
884 snat_main_t *sm = &snat_main;
885 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
886 u32 sw_if_index = ntohl (mp->sw_if_index);
890 is_del = !mp->is_add;
892 VALIDATE_SW_IF_INDEX (mp);
895 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
898 BAD_SW_IF_INDEX_LABEL;
900 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
903 static void *vl_api_nat44_interface_add_del_feature_t_print
904 (vl_api_nat44_interface_add_del_feature_t * mp, void *handle)
908 s = format (0, "SCRIPT: nat44_interface_add_del_feature ");
909 s = format (s, "sw_if_index %d %s %s",
910 clib_host_to_net_u32 (mp->sw_if_index),
911 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
912 mp->is_add ? "" : "del");
918 send_nat44_interface_details (snat_interface_t * i,
919 vl_api_registration_t * reg, u32 context)
921 vl_api_nat44_interface_details_t *rmp;
922 snat_main_t *sm = &snat_main;
924 rmp = vl_msg_api_alloc (sizeof (*rmp));
925 clib_memset (rmp, 0, sizeof (*rmp));
926 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
927 rmp->sw_if_index = ntohl (i->sw_if_index);
929 if (nat_interface_is_inside (i))
930 rmp->flags |= NAT_API_IS_INSIDE;
931 if (nat_interface_is_outside (i))
932 rmp->flags |= NAT_API_IS_OUTSIDE;
934 rmp->context = context;
936 vl_api_send_msg (reg, (u8 *) rmp);
940 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
942 vl_api_registration_t *reg;
943 snat_main_t *sm = &snat_main;
946 reg = vl_api_client_index_to_registration (mp->client_index);
951 pool_foreach (i, sm->interfaces,
953 send_nat44_interface_details(i, reg, mp->context);
959 vl_api_nat44_interface_dump_t_print (vl_api_nat44_interface_dump_t * mp,
964 s = format (0, "SCRIPT: nat44_interface_dump ");
970 vl_api_nat44_interface_add_del_output_feature_t_handler
971 (vl_api_nat44_interface_add_del_output_feature_t * mp)
973 snat_main_t *sm = &snat_main;
974 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
975 u32 sw_if_index = ntohl (mp->sw_if_index);
978 if (sm->deterministic)
980 rv = VNET_API_ERROR_UNSUPPORTED;
984 VALIDATE_SW_IF_INDEX (mp);
986 rv = snat_interface_add_del_output_feature (sw_if_index,
987 mp->flags & NAT_API_IS_INSIDE,
990 BAD_SW_IF_INDEX_LABEL;
992 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
995 static void *vl_api_nat44_interface_add_del_output_feature_t_print
996 (vl_api_nat44_interface_add_del_output_feature_t * mp, void *handle)
1000 s = format (0, "SCRIPT: nat44_interface_add_del_output_feature ");
1001 s = format (s, "sw_if_index %d %s %s",
1002 clib_host_to_net_u32 (mp->sw_if_index),
1003 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
1004 mp->is_add ? "" : "del");
1010 send_nat44_interface_output_feature_details (snat_interface_t * i,
1011 vl_api_registration_t * reg,
1014 vl_api_nat44_interface_output_feature_details_t *rmp;
1015 snat_main_t *sm = &snat_main;
1017 rmp = vl_msg_api_alloc (sizeof (*rmp));
1018 clib_memset (rmp, 0, sizeof (*rmp));
1020 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
1021 rmp->sw_if_index = ntohl (i->sw_if_index);
1022 rmp->context = context;
1024 if (nat_interface_is_inside (i))
1025 rmp->flags |= NAT_API_IS_INSIDE;
1027 vl_api_send_msg (reg, (u8 *) rmp);
1031 vl_api_nat44_interface_output_feature_dump_t_handler
1032 (vl_api_nat44_interface_output_feature_dump_t * mp)
1034 vl_api_registration_t *reg;
1035 snat_main_t *sm = &snat_main;
1036 snat_interface_t *i;
1038 if (sm->deterministic)
1041 reg = vl_api_client_index_to_registration (mp->client_index);
1046 pool_foreach (i, sm->output_feature_interfaces,
1048 send_nat44_interface_output_feature_details(i, reg, mp->context);
1053 static void *vl_api_nat44_interface_output_feature_dump_t_print
1054 (vl_api_nat44_interface_output_feature_dump_t * mp, void *handle)
1058 s = format (0, "SCRIPT: nat44_interface_output_feature_dump ");
1064 vl_api_nat44_add_del_static_mapping_t_handler
1065 (vl_api_nat44_add_del_static_mapping_t * mp)
1067 snat_main_t *sm = &snat_main;
1068 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
1069 ip4_address_t local_addr, external_addr;
1070 u16 local_port = 0, external_port = 0;
1071 u32 vrf_id, external_sw_if_index;
1072 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1074 snat_protocol_t proto;
1077 if (sm->deterministic)
1079 rv = VNET_API_ERROR_UNSUPPORTED;
1083 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
1084 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
1086 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1088 local_port = clib_net_to_host_u16 (mp->local_port);
1089 external_port = clib_net_to_host_u16 (mp->external_port);
1092 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1093 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
1094 proto = ip_proto_to_snat_proto (mp->protocol);
1096 if (mp->flags & NAT_API_IS_TWICE_NAT)
1097 twice_nat = TWICE_NAT;
1098 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1099 twice_nat = TWICE_NAT_SELF;
1100 mp->tag[sizeof (mp->tag) - 1] = 0;
1101 tag = format (0, "%s", mp->tag);
1102 vec_terminate_c_string (tag);
1104 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
1105 external_port, vrf_id,
1106 mp->flags & NAT_API_IS_ADDR_ONLY,
1107 external_sw_if_index, proto,
1108 mp->is_add, twice_nat,
1109 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0);
1113 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
1116 static void *vl_api_nat44_add_del_static_mapping_t_print
1117 (vl_api_nat44_add_del_static_mapping_t * mp, void *handle)
1121 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1122 s = format (s, "protocol %d local_addr %U external_addr %U ",
1124 format_ip4_address, mp->local_ip_address,
1125 format_ip4_address, mp->external_ip_address);
1127 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1128 s = format (s, "local_port %d external_port %d ",
1129 clib_net_to_host_u16 (mp->local_port),
1130 clib_net_to_host_u16 (mp->external_port));
1132 s = format (s, "twice_nat %d out2in_only %d ",
1133 mp->flags & NAT_API_IS_TWICE_NAT,
1134 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1136 if (mp->vrf_id != ~0)
1137 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1139 if (mp->external_sw_if_index != ~0)
1140 s = format (s, "external_sw_if_index %d",
1141 clib_net_to_host_u32 (mp->external_sw_if_index));
1146 send_nat44_static_mapping_details (snat_static_mapping_t * m,
1147 vl_api_registration_t * reg, u32 context)
1149 vl_api_nat44_static_mapping_details_t *rmp;
1150 snat_main_t *sm = &snat_main;
1151 u32 len = sizeof (*rmp);
1153 rmp = vl_msg_api_alloc (len);
1154 clib_memset (rmp, 0, len);
1156 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1158 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
1159 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
1160 rmp->external_sw_if_index = ~0;
1161 rmp->vrf_id = htonl (m->vrf_id);
1162 rmp->context = context;
1164 if (m->twice_nat == TWICE_NAT)
1165 rmp->flags |= NAT_API_IS_TWICE_NAT;
1166 else if (m->twice_nat == TWICE_NAT_SELF)
1167 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1169 if (is_out2in_only_static_mapping (m))
1170 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1172 if (is_addr_only_static_mapping (m))
1174 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1178 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1179 rmp->external_port = htons (m->external_port);
1180 rmp->local_port = htons (m->local_port);
1184 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1186 vl_api_send_msg (reg, (u8 *) rmp);
1190 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
1191 vl_api_registration_t * reg,
1194 vl_api_nat44_static_mapping_details_t *rmp;
1195 snat_main_t *sm = &snat_main;
1197 rmp = vl_msg_api_alloc (sizeof (*rmp));
1198 clib_memset (rmp, 0, sizeof (*rmp));
1200 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1201 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
1202 rmp->external_sw_if_index = htonl (m->sw_if_index);
1203 rmp->vrf_id = htonl (m->vrf_id);
1204 rmp->context = context;
1207 rmp->flags |= NAT_API_IS_TWICE_NAT;
1211 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1215 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1216 rmp->external_port = htons (m->e_port);
1217 rmp->local_port = htons (m->l_port);
1220 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1222 vl_api_send_msg (reg, (u8 *) rmp);
1226 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
1229 vl_api_registration_t *reg;
1230 snat_main_t *sm = &snat_main;
1231 snat_static_mapping_t *m;
1232 snat_static_map_resolve_t *rp;
1235 if (sm->deterministic)
1238 reg = vl_api_client_index_to_registration (mp->client_index);
1243 pool_foreach (m, sm->static_mappings,
1245 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1246 send_nat44_static_mapping_details (m, reg, mp->context);
1250 for (j = 0; j < vec_len (sm->to_resolve); j++)
1252 rp = sm->to_resolve + j;
1253 if (!rp->identity_nat)
1254 send_nat44_static_map_resolve_details (rp, reg, mp->context);
1259 vl_api_nat44_static_mapping_dump_t_print (vl_api_nat44_static_mapping_dump_t *
1264 s = format (0, "SCRIPT: nat44_static_mapping_dump ");
1270 vl_api_nat44_add_del_identity_mapping_t_handler
1271 (vl_api_nat44_add_del_identity_mapping_t * mp)
1273 snat_main_t *sm = &snat_main;
1274 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1277 u32 vrf_id, sw_if_index;
1279 snat_protocol_t proto = ~0;
1282 if (sm->deterministic)
1284 rv = VNET_API_ERROR_UNSUPPORTED;
1288 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1290 port = clib_net_to_host_u16 (mp->port);
1291 proto = ip_proto_to_snat_proto (mp->protocol);
1293 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1294 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1295 if (sw_if_index != ~0)
1298 memcpy (&addr.as_u8, mp->ip_address, 4);
1299 mp->tag[sizeof (mp->tag) - 1] = 0;
1300 tag = format (0, "%s", mp->tag);
1301 vec_terminate_c_string (tag);
1304 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1305 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1306 proto, mp->is_add, 0, 0, tag, 1);
1310 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1313 static void *vl_api_nat44_add_del_identity_mapping_t_print
1314 (vl_api_nat44_add_del_identity_mapping_t * mp, void *handle)
1318 s = format (0, "SCRIPT: nat44_add_del_identity_mapping ");
1319 if (mp->sw_if_index != ~0)
1320 s = format (s, "sw_if_index %d", clib_net_to_host_u32 (mp->sw_if_index));
1322 s = format (s, "addr %U", format_ip4_address, mp->ip_address);
1324 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1326 format (s, " protocol %d port %d", mp->protocol,
1327 clib_net_to_host_u16 (mp->port));
1329 if (mp->vrf_id != ~0)
1330 s = format (s, " vrf %d", clib_net_to_host_u32 (mp->vrf_id));
1336 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1337 vl_api_registration_t * reg, u32 context)
1339 vl_api_nat44_identity_mapping_details_t *rmp;
1340 snat_main_t *sm = &snat_main;
1341 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1343 rmp = vl_msg_api_alloc (sizeof (*rmp));
1344 clib_memset (rmp, 0, sizeof (*rmp));
1346 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1348 if (is_addr_only_static_mapping (m))
1349 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1351 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1352 rmp->port = htons (m->local_port);
1353 rmp->sw_if_index = ~0;
1354 rmp->vrf_id = htonl (local->vrf_id);
1355 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1356 rmp->context = context;
1358 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1360 vl_api_send_msg (reg, (u8 *) rmp);
1364 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1365 vl_api_registration_t * reg,
1368 vl_api_nat44_identity_mapping_details_t *rmp;
1369 snat_main_t *sm = &snat_main;
1371 rmp = vl_msg_api_alloc (sizeof (*rmp));
1372 clib_memset (rmp, 0, sizeof (*rmp));
1374 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1377 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1379 rmp->port = htons (m->l_port);
1380 rmp->sw_if_index = htonl (m->sw_if_index);
1381 rmp->vrf_id = htonl (m->vrf_id);
1382 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1383 rmp->context = context;
1385 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1387 vl_api_send_msg (reg, (u8 *) rmp);
1391 vl_api_nat44_identity_mapping_dump_t_handler
1392 (vl_api_nat44_identity_mapping_dump_t * mp)
1394 vl_api_registration_t *reg;
1395 snat_main_t *sm = &snat_main;
1396 snat_static_mapping_t *m;
1397 snat_static_map_resolve_t *rp;
1400 if (sm->deterministic)
1403 reg = vl_api_client_index_to_registration (mp->client_index);
1408 pool_foreach (m, sm->static_mappings,
1410 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1412 pool_foreach_index (j, m->locals,
1414 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1420 for (j = 0; j < vec_len (sm->to_resolve); j++)
1422 rp = sm->to_resolve + j;
1423 if (rp->identity_nat)
1424 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1428 static void *vl_api_nat44_identity_mapping_dump_t_print
1429 (vl_api_nat44_identity_mapping_dump_t * mp, void *handle)
1433 s = format (0, "SCRIPT: nat44_identity_mapping_dump ");
1439 vl_api_nat44_add_del_interface_addr_t_handler
1440 (vl_api_nat44_add_del_interface_addr_t * mp)
1442 snat_main_t *sm = &snat_main;
1443 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1444 u32 sw_if_index = ntohl (mp->sw_if_index);
1448 if (sm->deterministic)
1450 rv = VNET_API_ERROR_UNSUPPORTED;
1454 is_del = !mp->is_add;
1456 VALIDATE_SW_IF_INDEX (mp);
1458 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1459 mp->flags & NAT_API_IS_TWICE_NAT);
1461 BAD_SW_IF_INDEX_LABEL;
1463 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1466 static void *vl_api_nat44_add_del_interface_addr_t_print
1467 (vl_api_nat44_add_del_interface_addr_t * mp, void *handle)
1471 s = format (0, "SCRIPT: nat44_add_del_interface_addr ");
1472 s = format (s, "sw_if_index %d twice_nat %d %s",
1473 clib_host_to_net_u32 (mp->sw_if_index),
1474 mp->flags & NAT_API_IS_TWICE_NAT, mp->is_add ? "" : "del");
1480 send_nat44_interface_addr_details (u32 sw_if_index,
1481 vl_api_registration_t * reg, u32 context,
1484 vl_api_nat44_interface_addr_details_t *rmp;
1485 snat_main_t *sm = &snat_main;
1487 rmp = vl_msg_api_alloc (sizeof (*rmp));
1488 clib_memset (rmp, 0, sizeof (*rmp));
1490 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1491 rmp->sw_if_index = ntohl (sw_if_index);
1494 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1495 rmp->context = context;
1497 vl_api_send_msg (reg, (u8 *) rmp);
1501 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1504 vl_api_registration_t *reg;
1505 snat_main_t *sm = &snat_main;
1508 if (sm->deterministic)
1511 reg = vl_api_client_index_to_registration (mp->client_index);
1516 vec_foreach (i, sm->auto_add_sw_if_indices)
1517 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1518 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1519 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1524 vl_api_nat44_interface_addr_dump_t_print (vl_api_nat44_interface_addr_dump_t *
1529 s = format (0, "SCRIPT: nat44_interface_addr_dump ");
1535 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1538 vl_api_nat44_user_details_t *rmp;
1539 snat_main_t *sm = &snat_main;
1540 ip4_main_t *im = &ip4_main;
1542 rmp = vl_msg_api_alloc (sizeof (*rmp));
1543 clib_memset (rmp, 0, sizeof (*rmp));
1544 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1546 if (!pool_is_free_index (im->fibs, u->fib_index))
1548 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1549 rmp->vrf_id = ntohl (fib->ft_table_id);
1552 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1553 rmp->nsessions = ntohl (u->nsessions);
1554 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1555 rmp->context = context;
1557 vl_api_send_msg (reg, (u8 *) rmp);
1561 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1563 vl_api_registration_t *reg;
1564 snat_main_t *sm = &snat_main;
1565 snat_main_per_thread_data_t *tsm;
1568 if (sm->deterministic)
1571 reg = vl_api_client_index_to_registration (mp->client_index);
1576 vec_foreach (tsm, sm->per_thread_data)
1578 pool_foreach (u, tsm->users,
1580 send_nat44_user_details (u, reg, mp->context);
1587 vl_api_nat44_user_dump_t_print (vl_api_nat44_user_dump_t * mp, void *handle)
1591 s = format (0, "SCRIPT: nat44_user_dump ");
1597 send_nat44_user_session_details (snat_session_t * s,
1598 vl_api_registration_t * reg, u32 context)
1600 vl_api_nat44_user_session_details_t *rmp;
1601 snat_main_t *sm = &snat_main;
1603 rmp = vl_msg_api_alloc (sizeof (*rmp));
1604 clib_memset (rmp, 0, sizeof (*rmp));
1606 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1607 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1608 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1610 if (snat_is_session_static (s))
1611 rmp->flags |= NAT_API_IS_STATIC;
1613 if (is_twice_nat_session (s))
1614 rmp->flags |= NAT_API_IS_TWICE_NAT;
1616 if (is_ed_session (s) || is_fwd_bypass_session (s))
1617 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1619 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1620 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1621 rmp->total_pkts = ntohl (s->total_pkts);
1622 rmp->context = context;
1623 if (snat_is_unk_proto_session (s))
1625 rmp->outside_port = 0;
1626 rmp->inside_port = 0;
1627 rmp->protocol = ntohs (s->in2out.port);
1631 rmp->outside_port = s->out2in.port;
1632 rmp->inside_port = s->in2out.port;
1633 rmp->protocol = ntohs (snat_proto_to_ip_proto (s->in2out.protocol));
1635 if (is_ed_session (s) || is_fwd_bypass_session (s))
1637 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1638 rmp->ext_host_port = s->ext_host_port;
1639 if (is_twice_nat_session (s))
1641 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1642 rmp->ext_host_nat_port = s->ext_host_nat_port;
1646 vl_api_send_msg (reg, (u8 *) rmp);
1650 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1653 vl_api_registration_t *reg;
1654 snat_main_t *sm = &snat_main;
1655 snat_main_per_thread_data_t *tsm;
1657 clib_bihash_kv_8_8_t key, value;
1658 snat_user_key_t ukey;
1660 u32 session_index, head_index, elt_index;
1661 dlist_elt_t *head, *elt;
1664 if (sm->deterministic)
1667 reg = vl_api_client_index_to_registration (mp->client_index);
1671 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1672 ip.src_address.as_u32 = ukey.addr.as_u32;
1673 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1674 key.key = ukey.as_u64;
1675 if (sm->num_workers > 1)
1677 vec_elt_at_index (sm->per_thread_data,
1678 sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1680 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1681 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1683 u = pool_elt_at_index (tsm->users, value.value);
1684 if (!u->nsessions && !u->nstaticsessions)
1687 head_index = u->sessions_per_user_list_head_index;
1688 head = pool_elt_at_index (tsm->list_pool, head_index);
1689 elt_index = head->next;
1690 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1691 session_index = elt->value;
1692 while (session_index != ~0)
1694 s = pool_elt_at_index (tsm->sessions, session_index);
1696 send_nat44_user_session_details (s, reg, mp->context);
1698 elt_index = elt->next;
1699 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1700 session_index = elt->value;
1705 vl_api_nat44_user_session_dump_t_print (vl_api_nat44_user_session_dump_t * mp,
1710 s = format (0, "SCRIPT: nat44_user_session_dump ");
1711 s = format (s, "ip_address %U vrf_id %d\n",
1712 format_ip4_address, mp->ip_address,
1713 clib_net_to_host_u32 (mp->vrf_id));
1718 static nat44_lb_addr_port_t *
1719 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1720 u32 addr_port_pair_num)
1723 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1724 vl_api_nat44_lb_addr_port_t *ap;
1726 for (i = 0; i < addr_port_pair_num; i++)
1728 ap = &addr_port_pairs[i];
1729 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1730 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1731 lb_addr_port.port = clib_net_to_host_u16 (ap->port);
1732 lb_addr_port.probability = ap->probability;
1733 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1734 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1737 return lb_addr_port_pairs;
1741 vl_api_nat44_add_del_lb_static_mapping_t_handler
1742 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1744 snat_main_t *sm = &snat_main;
1745 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1746 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1748 nat44_lb_addr_port_t *locals = 0;
1749 ip4_address_t e_addr;
1750 snat_protocol_t proto;
1753 if (!sm->endpoint_dependent)
1755 rv = VNET_API_ERROR_UNSUPPORTED;
1760 unformat_nat44_lb_addr_port (mp->locals,
1761 clib_net_to_host_u32 (mp->local_num));
1762 clib_memcpy (&e_addr, mp->external_addr, 4);
1763 proto = ip_proto_to_snat_proto (mp->protocol);
1765 if (mp->flags & NAT_API_IS_TWICE_NAT)
1766 twice_nat = TWICE_NAT;
1767 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1768 twice_nat = TWICE_NAT_SELF;
1769 mp->tag[sizeof (mp->tag) - 1] = 0;
1770 tag = format (0, "%s", mp->tag);
1771 vec_terminate_c_string (tag);
1774 nat44_add_del_lb_static_mapping (e_addr,
1775 clib_net_to_host_u16 (mp->external_port),
1776 proto, locals, mp->is_add,
1778 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1779 clib_net_to_host_u32 (mp->affinity));
1785 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1788 static void *vl_api_nat44_add_del_lb_static_mapping_t_print
1789 (vl_api_nat44_add_del_lb_static_mapping_t * mp, void *handle)
1793 s = format (0, "SCRIPT: nat44_add_del_lb_static_mapping ");
1794 s = format (s, "is_add %d twice_nat %d out2in_only %d ",
1796 mp->flags & NAT_API_IS_TWICE_NAT,
1797 mp->flags & NAT_API_IS_OUT2IN_ONLY);
1803 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1804 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1806 snat_main_t *sm = &snat_main;
1807 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1809 ip4_address_t e_addr, l_addr;
1810 snat_protocol_t proto;
1812 if (!sm->endpoint_dependent)
1814 rv = VNET_API_ERROR_UNSUPPORTED;
1818 clib_memcpy (&e_addr, mp->external_addr, 4);
1819 clib_memcpy (&l_addr, mp->local.addr, 4);
1820 proto = ip_proto_to_snat_proto (mp->protocol);
1823 nat44_lb_static_mapping_add_del_local (e_addr,
1824 clib_net_to_host_u16
1825 (mp->external_port), l_addr,
1826 clib_net_to_host_u16 (mp->
1829 clib_net_to_host_u32 (mp->
1831 mp->local.probability, mp->is_add);
1834 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1837 static void *vl_api_nat44_lb_static_mapping_add_del_local_t_print
1838 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp, void *handle)
1842 s = format (0, "SCRIPT: nat44_lb_static_mapping_add_del_local ");
1843 s = format (s, "is_add %d", mp->is_add);
1849 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1850 vl_api_registration_t * reg,
1853 vl_api_nat44_lb_static_mapping_details_t *rmp;
1854 snat_main_t *sm = &snat_main;
1855 nat44_lb_addr_port_t *ap;
1856 vl_api_nat44_lb_addr_port_t *locals;
1860 vl_msg_api_alloc (sizeof (*rmp) +
1861 (pool_elts (m->locals) *
1862 sizeof (nat44_lb_addr_port_t)));
1863 clib_memset (rmp, 0, sizeof (*rmp));
1865 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1867 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1868 rmp->external_port = ntohs (m->external_port);
1869 rmp->protocol = snat_proto_to_ip_proto (m->proto);
1870 rmp->context = context;
1872 if (m->twice_nat == TWICE_NAT)
1873 rmp->flags |= NAT_API_IS_TWICE_NAT;
1874 else if (m->twice_nat == TWICE_NAT_SELF)
1875 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1876 if (is_out2in_only_static_mapping (m))
1877 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1879 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1881 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1883 pool_foreach (ap, m->locals,
1885 clib_memcpy (locals->addr, &(ap->addr), 4);
1886 locals->port = htons (ap->port);
1887 locals->probability = ap->probability;
1888 locals->vrf_id = ntohl (ap->vrf_id);
1893 rmp->local_num = ntohl (local_num);
1895 vl_api_send_msg (reg, (u8 *) rmp);
1899 vl_api_nat44_lb_static_mapping_dump_t_handler
1900 (vl_api_nat44_lb_static_mapping_dump_t * mp)
1902 vl_api_registration_t *reg;
1903 snat_main_t *sm = &snat_main;
1904 snat_static_mapping_t *m;
1906 if (!sm->endpoint_dependent)
1909 reg = vl_api_client_index_to_registration (mp->client_index);
1914 pool_foreach (m, sm->static_mappings,
1916 if (is_lb_static_mapping(m))
1917 send_nat44_lb_static_mapping_details (m, reg, mp->context);
1922 static void *vl_api_nat44_lb_static_mapping_dump_t_print
1923 (vl_api_nat44_lb_static_mapping_dump_t * mp, void *handle)
1927 s = format (0, "SCRIPT: nat44_lb_static_mapping_dump ");
1933 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1935 snat_main_t *sm = &snat_main;
1936 vl_api_nat44_del_session_reply_t *rmp;
1937 ip4_address_t addr, eh_addr;
1942 snat_protocol_t proto;
1944 if (sm->deterministic)
1946 rv = VNET_API_ERROR_UNSUPPORTED;
1950 memcpy (&addr.as_u8, mp->address, 4);
1951 port = clib_net_to_host_u16 (mp->port);
1952 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1953 proto = ip_proto_to_snat_proto (mp->protocol);
1954 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1955 eh_port = clib_net_to_host_u16 (mp->ext_host_port);
1957 is_in = mp->flags & NAT_API_IS_INSIDE;
1959 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1961 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
1964 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
1967 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1971 vl_api_nat44_del_session_t_print (vl_api_nat44_del_session_t * mp,
1976 s = format (0, "SCRIPT: nat44_add_del_static_mapping ");
1977 s = format (s, "addr %U port %d protocol %d vrf_id %d is_in %d",
1978 format_ip4_address, mp->address,
1979 clib_net_to_host_u16 (mp->port),
1980 mp->protocol, clib_net_to_host_u32 (mp->vrf_id),
1981 mp->flags & NAT_API_IS_INSIDE);
1982 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1983 s = format (s, "ext_host_address %U ext_host_port %d",
1984 format_ip4_address, mp->ext_host_address,
1985 clib_net_to_host_u16 (mp->ext_host_port));
1991 vl_api_nat44_forwarding_enable_disable_t_handler
1992 (vl_api_nat44_forwarding_enable_disable_t * mp)
1994 snat_main_t *sm = &snat_main;
1995 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1997 u32 *ses_to_be_removed = 0, *ses_index;
1998 snat_main_per_thread_data_t *tsm;
2001 sm->forwarding_enabled = mp->enable != 0;
2003 if (mp->enable == 0)
2006 vec_foreach (tsm, sm->per_thread_data)
2008 pool_foreach (s, tsm->sessions,
2010 if (is_fwd_bypass_session(s))
2012 vec_add1 (ses_to_be_removed, s - tsm->sessions);
2015 vec_foreach (ses_index, ses_to_be_removed)
2017 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
2018 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
2019 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
2021 vec_free (ses_to_be_removed);
2026 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
2029 static void *vl_api_nat44_forwarding_enable_disable_t_print
2030 (vl_api_nat44_forwarding_enable_disable_t * mp, void *handle)
2034 s = format (0, "SCRIPT: nat44_forwarding_enable_disable ");
2035 s = format (s, "enable %d", mp->enable != 0);
2041 vl_api_nat44_forwarding_is_enabled_t_handler
2042 (vl_api_nat44_forwarding_is_enabled_t * mp)
2044 vl_api_registration_t *reg;
2045 snat_main_t *sm = &snat_main;
2046 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
2048 reg = vl_api_client_index_to_registration (mp->client_index);
2052 rmp = vl_msg_api_alloc (sizeof (*rmp));
2053 clib_memset (rmp, 0, sizeof (*rmp));
2055 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
2056 rmp->context = mp->context;
2058 rmp->enabled = sm->forwarding_enabled;
2060 vl_api_send_msg (reg, (u8 *) rmp);
2063 static void *vl_api_nat44_forwarding_is_enabled_t_print
2064 (vl_api_nat44_forwarding_is_enabled_t * mp, void *handle)
2068 s = format (0, "SCRIPT: nat44_forwarding_is_enabled ");
2073 /*******************************/
2074 /*** Deterministic NAT (CGN) ***/
2075 /*******************************/
2078 vl_api_nat_det_add_del_map_t_handler (vl_api_nat_det_add_del_map_t * mp)
2080 snat_main_t *sm = &snat_main;
2081 vl_api_nat_det_add_del_map_reply_t *rmp;
2083 ip4_address_t in_addr, out_addr;
2085 if (!sm->deterministic)
2087 rv = VNET_API_ERROR_UNSUPPORTED;
2091 clib_memcpy (&in_addr, mp->in_addr, 4);
2092 clib_memcpy (&out_addr, mp->out_addr, 4);
2093 rv = snat_det_add_map (sm, &in_addr, mp->in_plen, &out_addr,
2094 mp->out_plen, mp->is_add);
2097 REPLY_MACRO (VL_API_NAT_DET_ADD_DEL_MAP_REPLY);
2101 vl_api_nat_det_add_del_map_t_print (vl_api_nat_det_add_del_map_t * mp,
2106 s = format (0, "SCRIPT: nat_det_add_del_map ");
2107 s = format (s, "inside address %U/%d outside address %U/%d\n",
2108 format_ip4_address, mp->in_addr, mp->in_plen,
2109 format_ip4_address, mp->out_addr, mp->out_plen);
2115 vl_api_nat_det_forward_t_handler (vl_api_nat_det_forward_t * mp)
2117 snat_main_t *sm = &snat_main;
2118 vl_api_nat_det_forward_reply_t *rmp;
2120 u16 lo_port = 0, hi_port = 0;
2122 ip4_address_t in_addr, out_addr;
2124 if (!sm->deterministic)
2126 rv = VNET_API_ERROR_UNSUPPORTED;
2127 REPLY_MACRO (VL_API_NAT_DET_FORWARD_REPLY);
2131 out_addr.as_u32 = 0;
2132 clib_memcpy (&in_addr, mp->in_addr, 4);
2133 dm = snat_det_map_by_user (sm, &in_addr);
2136 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2140 snat_det_forward (dm, &in_addr, &out_addr, &lo_port);
2141 hi_port = lo_port + dm->ports_per_host - 1;
2145 REPLY_MACRO2 (VL_API_NAT_DET_FORWARD_REPLY,
2147 rmp->out_port_lo = ntohs (lo_port);
2148 rmp->out_port_hi = ntohs (hi_port);
2149 clib_memcpy (rmp->out_addr, &out_addr, 4);
2155 vl_api_nat_det_forward_t_print (vl_api_nat_det_forward_t * mp, void *handle)
2159 s = format (0, "SCRIPT: nat_det_forward");
2160 s = format (s, "inside ip address %U\n", format_ip4_address, mp->in_addr);
2166 vl_api_nat_det_reverse_t_handler (vl_api_nat_det_reverse_t * mp)
2168 snat_main_t *sm = &snat_main;
2169 vl_api_nat_det_reverse_reply_t *rmp;
2171 ip4_address_t out_addr, in_addr;
2174 if (!sm->deterministic)
2176 rv = VNET_API_ERROR_UNSUPPORTED;
2177 REPLY_MACRO (VL_API_NAT_DET_REVERSE_REPLY);
2182 clib_memcpy (&out_addr, mp->out_addr, 4);
2183 dm = snat_det_map_by_out (sm, &out_addr);
2186 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2190 snat_det_reverse (dm, &out_addr, htons (mp->out_port), &in_addr);
2194 REPLY_MACRO2 (VL_API_NAT_DET_REVERSE_REPLY,
2196 clib_memcpy (rmp->in_addr, &in_addr, 4);
2202 vl_api_nat_det_reverse_t_print (vl_api_nat_det_reverse_t * mp, void *handle)
2206 s = format (0, "SCRIPT: nat_det_reverse");
2207 s = format (s, "outside ip address %U outside port %d",
2208 format_ip4_address, mp->out_addr, ntohs (mp->out_port));
2214 sent_nat_det_map_details (snat_det_map_t * m, vl_api_registration_t * reg,
2217 vl_api_nat_det_map_details_t *rmp;
2218 snat_main_t *sm = &snat_main;
2220 rmp = vl_msg_api_alloc (sizeof (*rmp));
2221 clib_memset (rmp, 0, sizeof (*rmp));
2222 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_MAP_DETAILS + sm->msg_id_base);
2223 clib_memcpy (rmp->in_addr, &m->in_addr, 4);
2224 rmp->in_plen = m->in_plen;
2225 clib_memcpy (rmp->out_addr, &m->out_addr, 4);
2226 rmp->out_plen = m->out_plen;
2227 rmp->sharing_ratio = htonl (m->sharing_ratio);
2228 rmp->ports_per_host = htons (m->ports_per_host);
2229 rmp->ses_num = htonl (m->ses_num);
2230 rmp->context = context;
2232 vl_api_send_msg (reg, (u8 *) rmp);
2236 vl_api_nat_det_map_dump_t_handler (vl_api_nat_det_map_dump_t * mp)
2238 vl_api_registration_t *reg;
2239 snat_main_t *sm = &snat_main;
2242 if (!sm->deterministic)
2245 reg = vl_api_client_index_to_registration (mp->client_index);
2250 vec_foreach(m, sm->det_maps)
2251 sent_nat_det_map_details(m, reg, mp->context);
2256 vl_api_nat_det_map_dump_t_print (vl_api_nat_det_map_dump_t * mp, void *handle)
2260 s = format (0, "SCRIPT: nat_det_map_dump ");
2266 vl_api_nat_det_close_session_out_t_handler (vl_api_nat_det_close_session_out_t
2269 snat_main_t *sm = &snat_main;
2270 vl_api_nat_det_close_session_out_reply_t *rmp;
2271 ip4_address_t out_addr, ext_addr, in_addr;
2272 snat_det_out_key_t key;
2274 snat_det_session_t *ses;
2277 if (!sm->deterministic)
2279 rv = VNET_API_ERROR_UNSUPPORTED;
2283 clib_memcpy (&out_addr, mp->out_addr, 4);
2284 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2286 dm = snat_det_map_by_out (sm, &out_addr);
2289 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2292 snat_det_reverse (dm, &ext_addr, ntohs (mp->out_port), &in_addr);
2293 key.ext_host_addr = ext_addr;
2294 key.ext_host_port = mp->ext_port;
2295 key.out_port = mp->out_port;
2296 ses = snat_det_get_ses_by_out (dm, &in_addr, key.as_u64);
2299 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2302 snat_det_ses_close (dm, ses);
2305 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2309 vl_api_nat_det_close_session_out_t_print (vl_api_nat_det_close_session_out_t *
2314 s = format (0, "SCRIPT: nat_det_close_session_out ");
2315 s = format (s, "out_addr %U out_port %d "
2316 "ext_addr %U ext_port %d\n",
2317 format_ip4_address, mp->out_addr, ntohs (mp->out_port),
2318 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2324 vl_api_nat_det_close_session_in_t_handler (vl_api_nat_det_close_session_in_t *
2327 snat_main_t *sm = &snat_main;
2328 vl_api_nat_det_close_session_in_reply_t *rmp;
2329 ip4_address_t in_addr, ext_addr;
2330 snat_det_out_key_t key;
2332 snat_det_session_t *ses;
2335 if (!sm->deterministic)
2337 rv = VNET_API_ERROR_UNSUPPORTED;
2341 clib_memcpy (&in_addr, mp->in_addr, 4);
2342 clib_memcpy (&ext_addr, mp->ext_addr, 4);
2344 dm = snat_det_map_by_user (sm, &in_addr);
2347 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2350 key.ext_host_addr = ext_addr;
2351 key.ext_host_port = mp->ext_port;
2352 ses = snat_det_find_ses_by_in (dm, &in_addr, mp->in_port, key);
2355 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
2358 snat_det_ses_close (dm, ses);
2361 REPLY_MACRO (VL_API_NAT_DET_CLOSE_SESSION_OUT_REPLY);
2365 vl_api_nat_det_close_session_in_t_print (vl_api_nat_det_close_session_in_t *
2369 s = format (0, "SCRIPT: nat_det_close_session_in ");
2370 s = format (s, "in_addr %U in_port %d ext_addr %U ext_port %d\n",
2371 format_ip4_address, mp->in_addr, ntohs (mp->in_port),
2372 format_ip4_address, mp->ext_addr, ntohs (mp->ext_port));
2378 send_nat_det_session_details (snat_det_session_t * s,
2379 vl_api_registration_t * reg, u32 context)
2381 vl_api_nat_det_session_details_t *rmp;
2382 snat_main_t *sm = &snat_main;
2384 rmp = vl_msg_api_alloc (sizeof (*rmp));
2385 clib_memset (rmp, 0, sizeof (*rmp));
2386 rmp->_vl_msg_id = ntohs (VL_API_NAT_DET_SESSION_DETAILS + sm->msg_id_base);
2387 rmp->in_port = s->in_port;
2388 clib_memcpy (rmp->ext_addr, &s->out.ext_host_addr, 4);
2389 rmp->ext_port = s->out.ext_host_port;
2390 rmp->out_port = s->out.out_port;
2391 rmp->state = s->state;
2392 rmp->expire = ntohl (s->expire);
2393 rmp->context = context;
2395 vl_api_send_msg (reg, (u8 *) rmp);
2399 vl_api_nat_det_session_dump_t_handler (vl_api_nat_det_session_dump_t * mp)
2401 vl_api_registration_t *reg;
2402 snat_main_t *sm = &snat_main;
2403 ip4_address_t user_addr;
2405 snat_det_session_t *s, empty_ses;
2408 if (!sm->deterministic)
2411 reg = vl_api_client_index_to_registration (mp->client_index);
2415 clib_memset (&empty_ses, 0, sizeof (empty_ses));
2416 clib_memcpy (&user_addr, mp->user_addr, 4);
2417 dm = snat_det_map_by_user (sm, &user_addr);
2421 s = dm->sessions + snat_det_user_ses_offset (&user_addr, dm->in_plen);
2422 for (i = 0; i < SNAT_DET_SES_PER_USER; i++)
2425 send_nat_det_session_details (s, reg, mp->context);
2431 vl_api_nat_det_session_dump_t_print (vl_api_nat_det_session_dump_t * mp,
2436 s = format (0, "SCRIPT: nat_det_session_dump ");
2437 s = format (s, "user_addr %U\n", format_ip4_address, mp->user_addr);
2447 vl_api_nat64_add_del_pool_addr_range_t_handler
2448 (vl_api_nat64_add_del_pool_addr_range_t * mp)
2450 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
2451 snat_main_t *sm = &snat_main;
2453 ip4_address_t this_addr;
2454 u32 start_host_order, end_host_order;
2459 tmp = (u32 *) mp->start_addr;
2460 start_host_order = clib_host_to_net_u32 (tmp[0]);
2461 tmp = (u32 *) mp->end_addr;
2462 end_host_order = clib_host_to_net_u32 (tmp[0]);
2464 count = (end_host_order - start_host_order) + 1;
2466 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
2468 memcpy (&this_addr.as_u8, mp->start_addr, 4);
2470 for (i = 0; i < count; i++)
2472 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
2475 increment_v4_address (&this_addr);
2479 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
2482 static void *vl_api_nat64_add_del_pool_addr_range_t_print
2483 (vl_api_nat64_add_del_pool_addr_range_t * mp, void *handle)
2487 s = format (0, "SCRIPT: nat64_add_del_pool_addr_range ");
2488 s = format (s, "%U - %U vrf_id %u %s\n",
2489 format_ip4_address, mp->start_addr,
2490 format_ip4_address, mp->end_addr,
2491 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2496 typedef struct nat64_api_walk_ctx_t_
2498 vl_api_registration_t *reg;
2501 } nat64_api_walk_ctx_t;
2504 nat64_api_pool_walk (snat_address_t * a, void *arg)
2506 vl_api_nat64_pool_addr_details_t *rmp;
2507 snat_main_t *sm = &snat_main;
2508 nat64_api_walk_ctx_t *ctx = arg;
2510 rmp = vl_msg_api_alloc (sizeof (*rmp));
2511 clib_memset (rmp, 0, sizeof (*rmp));
2512 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + sm->msg_id_base);
2513 clib_memcpy (rmp->address, &(a->addr), 4);
2514 if (a->fib_index != ~0)
2516 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
2519 rmp->vrf_id = ntohl (fib->ft_table_id);
2523 rmp->context = ctx->context;
2525 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2531 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
2533 vl_api_registration_t *reg;
2535 reg = vl_api_client_index_to_registration (mp->client_index);
2539 nat64_api_walk_ctx_t ctx = {
2541 .context = mp->context,
2544 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
2548 vl_api_nat64_pool_addr_dump_t_print (vl_api_nat64_pool_addr_dump_t * mp,
2553 s = format (0, "SCRIPT: nat64_pool_addr_dump\n");
2559 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
2562 snat_main_t *sm = &snat_main;
2563 vl_api_nat64_add_del_interface_reply_t *rmp;
2566 VALIDATE_SW_IF_INDEX (mp);
2569 nat64_add_del_interface (ntohl (mp->sw_if_index),
2570 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2572 BAD_SW_IF_INDEX_LABEL;
2574 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
2578 vl_api_nat64_add_del_interface_t_print (vl_api_nat64_add_del_interface_t * mp,
2583 s = format (0, "SCRIPT: nat64_add_del_interface ");
2584 s = format (s, "sw_if_index %d %s %s",
2585 clib_host_to_net_u32 (mp->sw_if_index),
2586 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2587 mp->is_add ? "" : "del");
2593 nat64_api_interface_walk (snat_interface_t * i, void *arg)
2595 vl_api_nat64_interface_details_t *rmp;
2596 snat_main_t *sm = &snat_main;
2597 nat64_api_walk_ctx_t *ctx = arg;
2599 rmp = vl_msg_api_alloc (sizeof (*rmp));
2600 clib_memset (rmp, 0, sizeof (*rmp));
2601 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + sm->msg_id_base);
2602 rmp->sw_if_index = ntohl (i->sw_if_index);
2604 if (nat_interface_is_inside (i))
2605 rmp->flags |= NAT_API_IS_INSIDE;
2606 if (nat_interface_is_outside (i))
2607 rmp->flags |= NAT_API_IS_OUTSIDE;
2609 rmp->context = ctx->context;
2611 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2617 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
2619 vl_api_registration_t *reg;
2621 reg = vl_api_client_index_to_registration (mp->client_index);
2625 nat64_api_walk_ctx_t ctx = {
2627 .context = mp->context,
2630 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
2634 vl_api_nat64_interface_dump_t_print (vl_api_nat64_interface_dump_t * mp,
2639 s = format (0, "SCRIPT: snat_interface_dump ");
2645 vl_api_nat64_add_del_static_bib_t_handler
2646 (vl_api_nat64_add_del_static_bib_t * mp)
2648 snat_main_t *sm = &snat_main;
2649 vl_api_nat64_add_del_static_bib_reply_t *rmp;
2650 ip6_address_t in_addr;
2651 ip4_address_t out_addr;
2654 memcpy (&in_addr.as_u8, mp->i_addr, 16);
2655 memcpy (&out_addr.as_u8, mp->o_addr, 4);
2658 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
2659 clib_net_to_host_u16 (mp->i_port),
2660 clib_net_to_host_u16 (mp->o_port),
2662 clib_net_to_host_u32 (mp->vrf_id),
2665 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
2668 static void *vl_api_nat64_add_del_static_bib_t_print
2669 (vl_api_nat64_add_del_static_bib_t * mp, void *handle)
2673 s = format (0, "SCRIPT: nat64_add_del_static_bib ");
2674 s = format (s, "protocol %d i_addr %U o_addr %U ",
2676 format_ip6_address, mp->i_addr, format_ip4_address, mp->o_addr);
2678 if (mp->vrf_id != ~0)
2679 s = format (s, "vrf %d", clib_net_to_host_u32 (mp->vrf_id));
2685 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
2687 vl_api_nat64_bib_details_t *rmp;
2688 snat_main_t *sm = &snat_main;
2689 nat64_api_walk_ctx_t *ctx = arg;
2692 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2696 rmp = vl_msg_api_alloc (sizeof (*rmp));
2697 clib_memset (rmp, 0, sizeof (*rmp));
2698 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + sm->msg_id_base);
2699 rmp->context = ctx->context;
2700 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
2701 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
2702 rmp->i_port = bibe->in_port;
2703 rmp->o_port = bibe->out_port;
2704 rmp->vrf_id = ntohl (fib->ft_table_id);
2705 rmp->proto = bibe->proto;
2706 if (bibe->is_static)
2707 rmp->flags |= NAT_API_IS_STATIC;
2708 rmp->ses_num = ntohl (bibe->ses_num);
2710 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2716 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
2718 vl_api_registration_t *reg;
2719 nat64_main_t *nm = &nat64_main;
2722 reg = vl_api_client_index_to_registration (mp->client_index);
2726 nat64_api_walk_ctx_t ctx = {
2728 .context = mp->context,
2732 vec_foreach (db, nm->db)
2733 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
2738 vl_api_nat64_bib_dump_t_print (vl_api_nat64_bib_dump_t * mp, void *handle)
2742 s = format (0, "SCRIPT: snat_bib_dump protocol %d", mp->proto);
2748 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
2750 vl_api_nat64_st_details_t *rmp;
2751 snat_main_t *sm = &snat_main;
2752 nat64_api_walk_ctx_t *ctx = arg;
2753 nat64_db_bib_entry_t *bibe;
2756 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
2760 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
2764 rmp = vl_msg_api_alloc (sizeof (*rmp));
2765 clib_memset (rmp, 0, sizeof (*rmp));
2766 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + sm->msg_id_base);
2767 rmp->context = ctx->context;
2768 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
2769 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
2770 rmp->il_port = bibe->in_port;
2771 rmp->ol_port = bibe->out_port;
2772 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
2773 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
2774 rmp->il_port = ste->r_port;
2775 rmp->vrf_id = ntohl (fib->ft_table_id);
2776 rmp->proto = ste->proto;
2778 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2784 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
2786 vl_api_registration_t *reg;
2787 nat64_main_t *nm = &nat64_main;
2790 reg = vl_api_client_index_to_registration (mp->client_index);
2794 nat64_api_walk_ctx_t ctx = {
2796 .context = mp->context,
2800 vec_foreach (db, nm->db)
2803 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
2809 vl_api_nat64_st_dump_t_print (vl_api_nat64_st_dump_t * mp, void *handle)
2813 s = format (0, "SCRIPT: snat_st_dump protocol %d", mp->proto);
2819 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
2821 vl_api_nat64_add_del_prefix_reply_t *rmp;
2822 snat_main_t *sm = &snat_main;
2823 ip6_address_t prefix;
2826 memcpy (&prefix.as_u8, mp->prefix.address, 16);
2829 nat64_add_del_prefix (&prefix, mp->prefix.len,
2830 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
2831 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
2835 vl_api_nat64_add_del_prefix_t_print (vl_api_nat64_add_del_prefix_t * mp,
2840 s = format (0, "SCRIPT: nat64_add_del_prefix %U/%u vrf_id %u %s\n",
2841 format_ip6_address, mp->prefix.address, mp->prefix.len,
2842 ntohl (mp->vrf_id), mp->is_add ? "" : "del");
2848 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
2850 vl_api_nat64_prefix_details_t *rmp;
2851 snat_main_t *sm = &snat_main;
2852 nat64_api_walk_ctx_t *ctx = arg;
2854 rmp = vl_msg_api_alloc (sizeof (*rmp));
2855 clib_memset (rmp, 0, sizeof (*rmp));
2856 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + sm->msg_id_base);
2857 clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
2858 rmp->prefix.len = p->plen;
2859 rmp->vrf_id = ntohl (p->vrf_id);
2860 rmp->context = ctx->context;
2862 vl_api_send_msg (ctx->reg, (u8 *) rmp);
2868 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
2870 vl_api_registration_t *reg;
2872 reg = vl_api_client_index_to_registration (mp->client_index);
2876 nat64_api_walk_ctx_t ctx = {
2878 .context = mp->context,
2881 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
2885 vl_api_nat64_prefix_dump_t_print (vl_api_nat64_prefix_dump_t * mp,
2890 s = format (0, "SCRIPT: nat64_prefix_dump\n");
2896 vl_api_nat64_add_del_interface_addr_t_handler
2897 (vl_api_nat64_add_del_interface_addr_t * mp)
2899 snat_main_t *sm = &snat_main;
2900 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
2901 u32 sw_if_index = ntohl (mp->sw_if_index);
2904 VALIDATE_SW_IF_INDEX (mp);
2906 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
2908 BAD_SW_IF_INDEX_LABEL;
2910 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
2913 static void *vl_api_nat64_add_del_interface_addr_t_print
2914 (vl_api_nat64_add_del_interface_addr_t * mp, void *handle)
2918 s = format (0, "SCRIPT: nat64_add_del_interface_addr ");
2919 s = format (s, "sw_if_index %d %s",
2920 clib_host_to_net_u32 (mp->sw_if_index),
2921 mp->is_add ? "" : "del");
2931 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
2934 snat_main_t *sm = &snat_main;
2935 vl_api_nat66_add_del_interface_reply_t *rmp;
2938 VALIDATE_SW_IF_INDEX (mp);
2941 nat66_interface_add_del (ntohl (mp->sw_if_index),
2942 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
2944 BAD_SW_IF_INDEX_LABEL;
2946 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
2950 vl_api_nat66_add_del_interface_t_print (vl_api_nat66_add_del_interface_t * mp,
2955 s = format (0, "SCRIPT: nat66_add_del_interface ");
2956 s = format (s, "sw_if_index %d %s %s",
2957 clib_host_to_net_u32 (mp->sw_if_index),
2958 mp->flags & NAT_API_IS_INSIDE ? "in" : "out",
2959 mp->is_add ? "" : "del");
2965 vl_api_nat66_add_del_static_mapping_t_handler
2966 (vl_api_nat66_add_del_static_mapping_t * mp)
2968 snat_main_t *sm = &snat_main;
2969 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
2970 ip6_address_t l_addr, e_addr;
2973 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
2974 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
2977 nat66_static_mapping_add_del (&l_addr, &e_addr,
2978 clib_net_to_host_u32 (mp->vrf_id),
2981 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
2984 static void *vl_api_nat66_add_del_static_mapping_t_print
2985 (vl_api_nat66_add_del_static_mapping_t * mp, void *handle)
2989 s = format (0, "SCRIPT: nat66_add_del_static_mapping ");
2990 s = format (s, "local_ip_address %U external_ip_address %U vrf_id %d %s",
2991 format_ip6_address, mp->local_ip_address,
2992 format_ip6_address, mp->external_ip_address,
2993 clib_net_to_host_u32 (mp->vrf_id), mp->is_add ? "" : "del");
2998 typedef struct nat66_api_walk_ctx_t_
3000 vl_api_registration_t *rp;
3002 } nat66_api_walk_ctx_t;
3005 nat66_api_interface_walk (snat_interface_t * i, void *arg)
3007 vl_api_nat66_interface_details_t *rmp;
3008 snat_main_t *sm = &snat_main;
3009 nat66_api_walk_ctx_t *ctx = arg;
3011 rmp = vl_msg_api_alloc (sizeof (*rmp));
3012 clib_memset (rmp, 0, sizeof (*rmp));
3013 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + sm->msg_id_base);
3014 rmp->sw_if_index = ntohl (i->sw_if_index);
3015 if (nat_interface_is_inside (i))
3016 rmp->flags |= NAT_API_IS_INSIDE;
3017 rmp->context = ctx->context;
3019 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3025 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
3027 vl_api_registration_t *rp;
3029 rp = vl_api_client_index_to_registration (mp->client_index);
3033 nat66_api_walk_ctx_t ctx = {
3035 .context = mp->context,
3038 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
3042 vl_api_nat66_interface_dump_t_print (vl_api_nat66_interface_dump_t * mp,
3047 s = format (0, "SCRIPT: nat66_interface_dump ");
3053 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
3055 vl_api_nat66_static_mapping_details_t *rmp;
3056 nat66_main_t *nm = &nat66_main;
3057 snat_main_t *sm = &snat_main;
3058 nat66_api_walk_ctx_t *ctx = arg;
3062 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
3066 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
3068 rmp = vl_msg_api_alloc (sizeof (*rmp));
3069 clib_memset (rmp, 0, sizeof (*rmp));
3071 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + sm->msg_id_base);
3072 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
3073 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
3074 rmp->vrf_id = ntohl (fib->ft_table_id);
3075 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
3076 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
3077 rmp->context = ctx->context;
3079 vl_api_send_msg (ctx->rp, (u8 *) rmp);
3085 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
3088 vl_api_registration_t *rp;
3090 rp = vl_api_client_index_to_registration (mp->client_index);
3094 nat66_api_walk_ctx_t ctx = {
3096 .context = mp->context,
3099 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
3103 vl_api_nat66_static_mapping_dump_t_print (vl_api_nat66_static_mapping_dump_t *
3108 s = format (0, "SCRIPT: nat66_static_mapping_dump ");
3114 /* List of message types that this plugin understands */
3115 #define foreach_snat_plugin_api_msg \
3116 _(NAT_CONTROL_PING, nat_control_ping) \
3117 _(NAT_SHOW_CONFIG, nat_show_config) \
3118 _(NAT_SET_WORKERS, nat_set_workers) \
3119 _(NAT_WORKER_DUMP, nat_worker_dump) \
3120 _(NAT44_DEL_USER, nat44_del_user) \
3121 _(NAT_SET_LOG_LEVEL, nat_set_log_level) \
3122 _(NAT_IPFIX_ENABLE_DISABLE, nat_ipfix_enable_disable) \
3123 _(NAT_SET_TIMEOUTS, nat_set_timeouts) \
3124 _(NAT_GET_TIMEOUTS, nat_get_timeouts) \
3125 _(NAT_SET_ADDR_AND_PORT_ALLOC_ALG, nat_set_addr_and_port_alloc_alg) \
3126 _(NAT_GET_ADDR_AND_PORT_ALLOC_ALG, nat_get_addr_and_port_alloc_alg) \
3127 _(NAT_SET_MSS_CLAMPING, nat_set_mss_clamping) \
3128 _(NAT_GET_MSS_CLAMPING, nat_get_mss_clamping) \
3129 _(NAT_HA_SET_LISTENER, nat_ha_set_listener) \
3130 _(NAT_HA_SET_FAILOVER, nat_ha_set_failover) \
3131 _(NAT_HA_GET_LISTENER, nat_ha_get_listener) \
3132 _(NAT_HA_GET_FAILOVER, nat_ha_get_failover) \
3133 _(NAT_HA_FLUSH, nat_ha_flush) \
3134 _(NAT_HA_RESYNC, nat_ha_resync) \
3135 _(NAT44_ADD_DEL_ADDRESS_RANGE, nat44_add_del_address_range) \
3136 _(NAT44_INTERFACE_ADD_DEL_FEATURE, nat44_interface_add_del_feature) \
3137 _(NAT44_ADD_DEL_STATIC_MAPPING, nat44_add_del_static_mapping) \
3138 _(NAT44_ADD_DEL_IDENTITY_MAPPING, nat44_add_del_identity_mapping) \
3139 _(NAT44_STATIC_MAPPING_DUMP, nat44_static_mapping_dump) \
3140 _(NAT44_IDENTITY_MAPPING_DUMP, nat44_identity_mapping_dump) \
3141 _(NAT44_ADDRESS_DUMP, nat44_address_dump) \
3142 _(NAT44_INTERFACE_DUMP, nat44_interface_dump) \
3143 _(NAT44_ADD_DEL_INTERFACE_ADDR, nat44_add_del_interface_addr) \
3144 _(NAT44_INTERFACE_ADDR_DUMP, nat44_interface_addr_dump) \
3145 _(NAT44_USER_DUMP, nat44_user_dump) \
3146 _(NAT44_USER_SESSION_DUMP, nat44_user_session_dump) \
3147 _(NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE, \
3148 nat44_interface_add_del_output_feature) \
3149 _(NAT44_INTERFACE_OUTPUT_FEATURE_DUMP, \
3150 nat44_interface_output_feature_dump) \
3151 _(NAT44_ADD_DEL_LB_STATIC_MAPPING, nat44_add_del_lb_static_mapping) \
3152 _(NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL, \
3153 nat44_lb_static_mapping_add_del_local) \
3154 _(NAT44_LB_STATIC_MAPPING_DUMP, nat44_lb_static_mapping_dump) \
3155 _(NAT44_DEL_SESSION, nat44_del_session) \
3156 _(NAT44_FORWARDING_ENABLE_DISABLE, nat44_forwarding_enable_disable) \
3157 _(NAT44_FORWARDING_IS_ENABLED, nat44_forwarding_is_enabled) \
3158 _(NAT_DET_ADD_DEL_MAP, nat_det_add_del_map) \
3159 _(NAT_DET_FORWARD, nat_det_forward) \
3160 _(NAT_DET_REVERSE, nat_det_reverse) \
3161 _(NAT_DET_MAP_DUMP, nat_det_map_dump) \
3162 _(NAT_DET_CLOSE_SESSION_OUT, nat_det_close_session_out) \
3163 _(NAT_DET_CLOSE_SESSION_IN, nat_det_close_session_in) \
3164 _(NAT_DET_SESSION_DUMP, nat_det_session_dump) \
3165 _(NAT64_ADD_DEL_POOL_ADDR_RANGE, nat64_add_del_pool_addr_range) \
3166 _(NAT64_POOL_ADDR_DUMP, nat64_pool_addr_dump) \
3167 _(NAT64_ADD_DEL_INTERFACE, nat64_add_del_interface) \
3168 _(NAT64_INTERFACE_DUMP, nat64_interface_dump) \
3169 _(NAT64_ADD_DEL_STATIC_BIB, nat64_add_del_static_bib) \
3170 _(NAT64_BIB_DUMP, nat64_bib_dump) \
3171 _(NAT64_ST_DUMP, nat64_st_dump) \
3172 _(NAT64_ADD_DEL_PREFIX, nat64_add_del_prefix) \
3173 _(NAT64_PREFIX_DUMP, nat64_prefix_dump) \
3174 _(NAT64_ADD_DEL_INTERFACE_ADDR, nat64_add_del_interface_addr) \
3175 _(NAT66_ADD_DEL_INTERFACE, nat66_add_del_interface) \
3176 _(NAT66_INTERFACE_DUMP, nat66_interface_dump) \
3177 _(NAT66_ADD_DEL_STATIC_MAPPING, nat66_add_del_static_mapping) \
3178 _(NAT66_STATIC_MAPPING_DUMP, nat66_static_mapping_dump)
3180 /* Set up the API message handling tables */
3181 static clib_error_t *
3182 snat_plugin_api_hookup (vlib_main_t * vm)
3184 snat_main_t *sm __attribute__ ((unused)) = &snat_main;
3186 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
3188 vl_api_##n##_t_handler, \
3190 vl_api_##n##_t_endian, \
3191 vl_api_##n##_t_print, \
3192 sizeof(vl_api_##n##_t), 1);
3193 foreach_snat_plugin_api_msg;
3199 #define vl_msg_name_crc_list
3200 #include <nat/nat_all_api_h.h>
3201 #undef vl_msg_name_crc_list
3204 setup_message_id_table (snat_main_t * sm, api_main_t * am)
3206 #define _(id,n,crc) \
3207 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
3208 foreach_vl_msg_name_crc_nat;
3213 plugin_custom_dump_configure (snat_main_t * sm)
3215 #define _(n,f) sm->api_main->msg_print_handlers \
3216 [VL_API_##n + sm->msg_id_base] \
3217 = (void *) vl_api_##f##_t_print;
3218 foreach_snat_plugin_api_msg;
3223 snat_api_init (vlib_main_t * vm, snat_main_t * sm)
3226 clib_error_t *error = 0;
3228 name = format (0, "nat_%08x%c", api_version, 0);
3230 /* Ask for a correctly-sized block of API message decode slots */
3232 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
3234 error = snat_plugin_api_hookup (vm);
3236 /* Add our API messages to the global name_crc hash table */
3237 setup_message_id_table (sm, sm->api_main);
3239 plugin_custom_dump_configure (sm);
3247 * fd.io coding-style-patch-verification: ON
3250 * eval: (c-set-style "gnu")