2 * Copyright (c) 2020 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 NAT44 plugin API implementation
21 #include <vnet/ip/ip_types_api.h>
22 #include <vlibmemory/api.h>
24 #include <vnet/fib/fib_table.h>
26 #include <nat/lib/nat_inlines.h>
27 #include <nat/lib/ipfix_logging.h>
31 #include <nat/nat44.api_enum.h>
32 #include <nat/nat44.api_types.h>
34 #include <nat/nat44-ei/nat44_ei_ha.h>
35 #include <nat/nat44-ei/nat44_ei.h>
36 #include <nat/nat_inlines.h>
38 #include <nat/nat44/inlines.h>
39 #include <nat/nat44/ed_inlines.h>
41 #define REPLY_MSG_ID_BASE sm->msg_id_base
42 #include <vlibapi/api_helper_macros.h>
45 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
47 vl_api_nat_control_ping_reply_t *rmp;
48 snat_main_t *sm = &snat_main;
52 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
54 rmp->vpe_pid = ntohl (getpid ());
60 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
62 vl_api_nat_show_config_reply_t *rmp;
63 snat_main_t *sm = &snat_main;
67 REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_CONFIG_REPLY,
69 rmp->translation_buckets = htonl (sm->translation_buckets);
70 rmp->user_buckets = htonl (sm->user_buckets);
71 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
72 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
73 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
74 rmp->static_mapping_only = sm->static_mapping_only;
75 rmp->static_mapping_connection_tracking =
76 sm->static_mapping_connection_tracking;
77 rmp->endpoint_dependent = sm->endpoint_dependent;
78 rmp->out2in_dpo = sm->out2in_dpo;
84 vl_api_nat_show_config_2_t_handler (vl_api_nat_show_config_2_t * mp)
86 vl_api_nat_show_config_2_reply_t *rmp;
87 snat_main_t *sm = &snat_main;
91 REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_CONFIG_2_REPLY,
93 rmp->translation_buckets = htonl (sm->translation_buckets);
94 rmp->user_buckets = htonl (sm->user_buckets);
95 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
96 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
97 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
98 rmp->static_mapping_only = sm->static_mapping_only;
99 rmp->static_mapping_connection_tracking =
100 sm->static_mapping_connection_tracking;
101 rmp->endpoint_dependent = sm->endpoint_dependent;
102 rmp->out2in_dpo = sm->out2in_dpo;
103 rmp->max_translations_per_thread = clib_net_to_host_u32(sm->max_translations_per_thread);
104 rmp->max_users_per_thread = clib_net_to_host_u32(sm->max_users_per_thread);
110 vl_api_nat44_show_running_config_t_handler (vl_api_nat44_show_running_config_t
113 vl_api_nat44_show_running_config_reply_t *rmp;
114 snat_main_t *sm = &snat_main;
115 nat44_config_t *rc = &sm->rconfig;
119 REPLY_MACRO2_ZERO (VL_API_NAT44_SHOW_RUNNING_CONFIG_REPLY,
121 rmp->inside_vrf = htonl (rc->inside_vrf);
122 rmp->outside_vrf = htonl (rc->outside_vrf);
123 rmp->users = htonl (rc->users);
124 rmp->sessions = htonl (rc->sessions);
125 rmp->user_sessions = htonl (rc->user_sessions);
127 rmp->user_buckets = htonl (sm->user_buckets);
128 rmp->translation_buckets = htonl (sm->translation_buckets);
130 rmp->timeouts.udp = htonl (sm->timeouts.udp);
131 rmp->timeouts.tcp_established = htonl (sm->timeouts.tcp.established);
132 rmp->timeouts.tcp_transitory = htonl (sm->timeouts.tcp.transitory);
133 rmp->timeouts.icmp = htonl (sm->timeouts.icmp);
135 rmp->forwarding_enabled = sm->forwarding_enabled == 1;
136 // consider how to split functionality between subplugins
137 rmp->ipfix_logging_enabled = nat_ipfix_logging_enabled ();
139 if (rc->endpoint_dependent)
140 rmp->flags |= NAT44_IS_ENDPOINT_DEPENDENT;
142 rmp->flags |= NAT44_IS_ENDPOINT_INDEPENDENT;
144 if (rc->static_mapping_only)
145 rmp->flags |= NAT44_IS_STATIC_MAPPING_ONLY;
146 if (rc->connection_tracking)
147 rmp->flags |= NAT44_IS_CONNECTION_TRACKING;
149 rmp->flags |= NAT44_IS_OUT2IN_DPO;
155 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
157 snat_main_t *sm = &snat_main;
158 vl_api_nat_set_workers_reply_t *rmp;
163 mask = clib_net_to_host_u64 (mp->worker_mask);
165 if (sm->num_workers < 2)
167 rv = VNET_API_ERROR_FEATURE_DISABLED;
171 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
172 rv = snat_set_workers (bitmap);
173 clib_bitmap_free (bitmap);
176 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
180 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
183 vl_api_nat_worker_details_t *rmp;
184 snat_main_t *sm = &snat_main;
185 vlib_worker_thread_t *w =
186 vlib_worker_threads + worker_index + sm->first_worker_index;
188 rmp = vl_msg_api_alloc (sizeof (*rmp));
189 clib_memset (rmp, 0, sizeof (*rmp));
190 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
191 rmp->context = context;
192 rmp->worker_index = htonl (worker_index);
193 rmp->lcore_id = htonl (w->cpu_id);
194 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
196 vl_api_send_msg (reg, (u8 *) rmp);
200 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
202 vl_api_registration_t *reg;
203 snat_main_t *sm = &snat_main;
206 reg = vl_api_client_index_to_registration (mp->client_index);
211 vec_foreach (worker_index, sm->workers)
212 send_nat_worker_details(*worker_index, reg, mp->context);
217 vl_api_nat44_session_cleanup_t_handler (vl_api_nat44_session_cleanup_t * mp)
219 snat_main_t *sm = &snat_main;
220 vl_api_nat44_session_cleanup_reply_t *rmp;
222 REPLY_MACRO (VL_API_NAT44_SESSION_CLEANUP_REPLY);
226 vl_api_nat44_set_session_limit_t_handler (vl_api_nat44_set_session_limit_t *
229 snat_main_t *sm = &snat_main;
230 vl_api_nat44_set_session_limit_reply_t *rmp;
233 rv = nat44_set_session_limit
234 (ntohl (mp->session_limit), ntohl (mp->vrf_id));
236 REPLY_MACRO (VL_API_NAT44_SET_SESSION_LIMIT_REPLY);
240 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
242 snat_main_t *sm = &snat_main;
243 vl_api_nat_set_log_level_reply_t *rmp;
246 if (sm->log_level > NAT_LOG_DEBUG)
247 rv = VNET_API_ERROR_UNSUPPORTED;
249 sm->log_level = mp->log_level;
251 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
255 vl_api_nat44_plugin_enable_disable_t_handler
256 (vl_api_nat44_plugin_enable_disable_t * mp)
258 snat_main_t *sm = &snat_main;
259 nat44_config_t c = { 0 };
260 vl_api_nat44_plugin_enable_disable_reply_t *rmp;
265 c.endpoint_dependent = mp->flags & NAT44_API_IS_ENDPOINT_DEPENDENT;
266 c.static_mapping_only = mp->flags & NAT44_API_IS_STATIC_MAPPING_ONLY;
267 c.connection_tracking = mp->flags & NAT44_API_IS_CONNECTION_TRACKING;
268 c.out2in_dpo = mp->flags & NAT44_API_IS_OUT2IN_DPO;
270 c.inside_vrf = ntohl (mp->inside_vrf);
271 c.outside_vrf = ntohl (mp->outside_vrf);
273 c.users = ntohl (mp->users);
275 c.sessions = ntohl (mp->sessions);
277 c.user_sessions = ntohl (mp->user_sessions);
279 rv = nat44_plugin_enable (c);
282 rv = nat44_plugin_disable ();
284 REPLY_MACRO (VL_API_NAT44_PLUGIN_ENABLE_DISABLE_REPLY);
288 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
291 snat_main_t *sm = &snat_main;
292 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
295 rv = nat_ipfix_logging_enable_disable (mp->enable,
298 clib_host_to_net_u16 (mp->src_port));
300 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
304 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
306 snat_main_t *sm = &snat_main;
307 vl_api_nat_set_timeouts_reply_t *rmp;
310 sm->timeouts.udp = ntohl (mp->udp);
311 sm->timeouts.tcp.established = ntohl (mp->tcp_established);
312 sm->timeouts.tcp.transitory = ntohl (mp->tcp_transitory);
313 sm->timeouts.icmp = ntohl (mp->icmp);
315 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
319 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
321 snat_main_t *sm = &snat_main;
322 vl_api_nat_get_timeouts_reply_t *rmp;
326 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
328 rmp->udp = htonl (sm->timeouts.udp);
329 rmp->tcp_established = htonl (sm->timeouts.tcp.established);
330 rmp->tcp_transitory = htonl (sm->timeouts.tcp.transitory);
331 rmp->icmp = htonl (sm->timeouts.icmp);
337 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
338 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
340 snat_main_t *sm = &snat_main;
341 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
343 u16 port_start, port_end;
345 if (sm->endpoint_dependent)
347 rv = VNET_API_ERROR_UNSUPPORTED;
353 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
354 nat44_ei_set_alloc_default ();
356 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
357 nat44_ei_set_alloc_mape (ntohs (mp->psid), mp->psid_offset,
360 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
361 port_start = ntohs (mp->start_port);
362 port_end = ntohs (mp->end_port);
363 if (port_end <= port_start)
365 rv = VNET_API_ERROR_INVALID_VALUE;
368 nat44_ei_set_alloc_range (port_start, port_end);
371 rv = VNET_API_ERROR_INVALID_VALUE;
376 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
380 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
381 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
383 snat_main_t *sm = &snat_main;
384 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
388 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
390 rmp->alg = sm->addr_and_port_alloc_alg;
391 rmp->psid_offset = sm->psid_offset;
392 rmp->psid_length = sm->psid_length;
393 rmp->psid = htons (sm->psid);
394 rmp->start_port = htons (sm->start_port);
395 rmp->end_port = htons (sm->end_port);
401 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
403 snat_main_t *sm = &snat_main;
404 vl_api_nat_set_mss_clamping_reply_t *rmp;
408 sm->mss_clamping = ntohs (mp->mss_value);
410 sm->mss_clamping = 0;
412 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
416 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
418 snat_main_t *sm = &snat_main;
419 vl_api_nat_get_mss_clamping_reply_t *rmp;
423 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
425 rmp->enable = sm->mss_clamping ? 1 : 0;
426 rmp->mss_value = htons (sm->mss_clamping);
432 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
434 snat_main_t *sm = &snat_main;
435 vl_api_nat_ha_set_listener_reply_t *rmp;
439 memcpy (&addr, &mp->ip_address, sizeof (addr));
441 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
442 clib_net_to_host_u32 (mp->path_mtu));
444 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
448 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
450 snat_main_t *sm = &snat_main;
451 vl_api_nat_ha_get_listener_reply_t *rmp;
457 nat_ha_get_listener (&addr, &port, &path_mtu);
460 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
462 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
463 rmp->port = clib_host_to_net_u16 (port);
464 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
470 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
472 snat_main_t *sm = &snat_main;
473 vl_api_nat_ha_set_failover_reply_t *rmp;
477 memcpy (&addr, &mp->ip_address, sizeof (addr));
479 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
480 clib_net_to_host_u32 (mp->session_refresh_interval));
482 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
486 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
488 snat_main_t *sm = &snat_main;
489 vl_api_nat_ha_get_failover_reply_t *rmp;
493 u32 session_refresh_interval;
495 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
498 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
500 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
501 rmp->port = clib_host_to_net_u16 (port);
502 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
508 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
510 snat_main_t *sm = &snat_main;
511 vl_api_nat_ha_flush_reply_t *rmp;
516 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
520 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
522 snat_main_t *sm = &snat_main;
523 vl_api_registration_t *reg;
524 vl_api_nat_ha_resync_completed_event_t *mp;
526 reg = vl_api_client_index_to_registration (client_index);
530 mp = vl_msg_api_alloc (sizeof (*mp));
531 clib_memset (mp, 0, sizeof (*mp));
532 mp->client_index = client_index;
534 mp->missed_count = clib_host_to_net_u32 (missed_count);
536 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
538 vl_api_send_msg (reg, (u8 *) mp);
542 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
544 snat_main_t *sm = &snat_main;
545 vl_api_nat_ha_resync_reply_t *rmp;
549 nat_ha_resync (mp->client_index, mp->pid,
550 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
553 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
557 vl_api_nat44_del_user_t_handler (vl_api_nat44_del_user_t * mp)
559 snat_main_t *sm = &snat_main;
560 vl_api_nat44_del_user_reply_t *rmp;
563 memcpy (&addr.as_u8, mp->ip_address, 4);
564 rv = nat44_ei_user_del (&addr, ntohl (mp->fib_index));
565 REPLY_MACRO (VL_API_NAT44_DEL_USER_REPLY);
569 vl_api_nat44_add_del_address_range_t_handler
570 (vl_api_nat44_add_del_address_range_t * mp)
572 snat_main_t *sm = &snat_main;
573 vl_api_nat44_add_del_address_range_reply_t *rmp;
574 ip4_address_t this_addr;
575 u8 is_add, twice_nat;
576 u32 start_host_order, end_host_order;
582 if (sm->static_mapping_only)
584 rv = VNET_API_ERROR_FEATURE_DISABLED;
589 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
591 tmp = (u32 *) mp->first_ip_address;
592 start_host_order = clib_host_to_net_u32 (tmp[0]);
593 tmp = (u32 *) mp->last_ip_address;
594 end_host_order = clib_host_to_net_u32 (tmp[0]);
596 count = (end_host_order - start_host_order) + 1;
598 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
601 nat_log_info ("%U - %U, %d addresses...",
602 format_ip4_address, mp->first_ip_address,
603 format_ip4_address, mp->last_ip_address, count);
605 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
607 for (i = 0; i < count; i++)
610 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
612 rv = snat_del_address (sm, this_addr, 0, twice_nat);
618 nat44_add_del_address_dpo (this_addr, is_add);
620 increment_v4_address (&this_addr);
624 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
628 send_nat44_address_details (snat_address_t * a,
629 vl_api_registration_t * reg, u32 context,
632 vl_api_nat44_address_details_t *rmp;
633 snat_main_t *sm = &snat_main;
635 rmp = vl_msg_api_alloc (sizeof (*rmp));
636 clib_memset (rmp, 0, sizeof (*rmp));
637 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
638 clib_memcpy (rmp->ip_address, &(a->addr), 4);
639 if (a->fib_index != ~0)
641 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
642 rmp->vrf_id = ntohl (fib->ft_table_id);
647 rmp->flags |= NAT_API_IS_TWICE_NAT;
648 rmp->context = context;
650 vl_api_send_msg (reg, (u8 *) rmp);
654 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
656 vl_api_registration_t *reg;
657 snat_main_t *sm = &snat_main;
660 reg = vl_api_client_index_to_registration (mp->client_index);
665 vec_foreach (a, sm->addresses)
666 send_nat44_address_details (a, reg, mp->context, 0);
667 vec_foreach (a, sm->twice_nat_addresses)
668 send_nat44_address_details (a, reg, mp->context, 1);
673 vl_api_nat44_interface_add_del_feature_t_handler
674 (vl_api_nat44_interface_add_del_feature_t * mp)
676 snat_main_t *sm = &snat_main;
677 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
678 u32 sw_if_index = ntohl (mp->sw_if_index);
682 is_del = !mp->is_add;
684 VALIDATE_SW_IF_INDEX (mp);
687 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
690 BAD_SW_IF_INDEX_LABEL;
692 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
696 send_nat44_interface_details (snat_interface_t * i,
697 vl_api_registration_t * reg, u32 context)
699 vl_api_nat44_interface_details_t *rmp;
700 snat_main_t *sm = &snat_main;
702 rmp = vl_msg_api_alloc (sizeof (*rmp));
703 clib_memset (rmp, 0, sizeof (*rmp));
704 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
705 rmp->sw_if_index = ntohl (i->sw_if_index);
707 if (nat_interface_is_inside (i))
708 rmp->flags |= NAT_API_IS_INSIDE;
709 if (nat_interface_is_outside (i))
710 rmp->flags |= NAT_API_IS_OUTSIDE;
712 rmp->context = context;
714 vl_api_send_msg (reg, (u8 *) rmp);
718 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
720 vl_api_registration_t *reg;
721 snat_main_t *sm = &snat_main;
724 reg = vl_api_client_index_to_registration (mp->client_index);
729 pool_foreach (i, sm->interfaces)
731 send_nat44_interface_details(i, reg, mp->context);
737 vl_api_nat44_interface_add_del_output_feature_t_handler
738 (vl_api_nat44_interface_add_del_output_feature_t * mp)
740 snat_main_t *sm = &snat_main;
741 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
742 u32 sw_if_index = ntohl (mp->sw_if_index);
745 VALIDATE_SW_IF_INDEX (mp);
747 rv = snat_interface_add_del_output_feature (sw_if_index,
748 mp->flags & NAT_API_IS_INSIDE,
751 BAD_SW_IF_INDEX_LABEL;
752 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
756 send_nat44_interface_output_feature_details (snat_interface_t * i,
757 vl_api_registration_t * reg,
760 vl_api_nat44_interface_output_feature_details_t *rmp;
761 snat_main_t *sm = &snat_main;
763 rmp = vl_msg_api_alloc (sizeof (*rmp));
764 clib_memset (rmp, 0, sizeof (*rmp));
766 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
767 rmp->sw_if_index = ntohl (i->sw_if_index);
768 rmp->context = context;
770 if (nat_interface_is_inside (i))
771 rmp->flags |= NAT_API_IS_INSIDE;
773 vl_api_send_msg (reg, (u8 *) rmp);
777 vl_api_nat44_interface_output_feature_dump_t_handler
778 (vl_api_nat44_interface_output_feature_dump_t * mp)
780 vl_api_registration_t *reg;
781 snat_main_t *sm = &snat_main;
784 reg = vl_api_client_index_to_registration (mp->client_index);
789 pool_foreach (i, sm->output_feature_interfaces)
791 send_nat44_interface_output_feature_details(i, reg, mp->context);
797 vl_api_nat44_add_del_static_mapping_t_handler
798 (vl_api_nat44_add_del_static_mapping_t * mp)
800 snat_main_t *sm = &snat_main;
801 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
802 ip4_address_t local_addr, external_addr, pool_addr = { 0 };
803 u16 local_port = 0, external_port = 0;
804 u32 vrf_id, external_sw_if_index;
805 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
807 nat_protocol_t proto;
810 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
811 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
813 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
815 local_port = mp->local_port;
816 external_port = mp->external_port;
819 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
820 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
821 proto = ip_proto_to_nat_proto (mp->protocol);
823 if (mp->flags & NAT_API_IS_TWICE_NAT)
824 twice_nat = TWICE_NAT;
825 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
826 twice_nat = TWICE_NAT_SELF;
827 mp->tag[sizeof (mp->tag) - 1] = 0;
828 tag = format (0, "%s", mp->tag);
829 vec_terminate_c_string (tag);
831 rv = snat_add_static_mapping (
832 local_addr, external_addr, local_port, external_port, vrf_id,
833 mp->flags & NAT_API_IS_ADDR_ONLY, external_sw_if_index, proto, mp->is_add,
834 twice_nat, mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0, pool_addr, 0);
837 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
841 vl_api_nat44_add_del_static_mapping_v2_t_handler
842 (vl_api_nat44_add_del_static_mapping_v2_t * mp)
844 snat_main_t *sm = &snat_main;
845 vl_api_nat44_add_del_static_mapping_v2_reply_t *rmp;
846 ip4_address_t local_addr, external_addr, pool_addr;
847 u16 local_port = 0, external_port = 0;
848 u32 vrf_id, external_sw_if_index;
849 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
851 nat_protocol_t proto;
854 memcpy (&pool_addr.as_u8, mp->pool_ip_address, 4);
855 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
856 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
858 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
860 local_port = mp->local_port;
861 external_port = mp->external_port;
864 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
865 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
866 proto = ip_proto_to_nat_proto (mp->protocol);
868 if (mp->flags & NAT_API_IS_TWICE_NAT)
869 twice_nat = TWICE_NAT;
870 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
871 twice_nat = TWICE_NAT_SELF;
872 mp->tag[sizeof (mp->tag) - 1] = 0;
873 tag = format (0, "%s", mp->tag);
874 vec_terminate_c_string (tag);
876 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
877 external_port, vrf_id,
878 mp->flags & NAT_API_IS_ADDR_ONLY,
879 external_sw_if_index, proto,
880 mp->is_add, twice_nat,
881 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
882 pool_addr, mp->match_pool);
885 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_V2_REPLY);
889 send_nat44_static_mapping_details (snat_static_mapping_t * m,
890 vl_api_registration_t * reg, u32 context)
892 vl_api_nat44_static_mapping_details_t *rmp;
893 snat_main_t *sm = &snat_main;
894 u32 len = sizeof (*rmp);
896 rmp = vl_msg_api_alloc (len);
897 clib_memset (rmp, 0, len);
899 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
901 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
902 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
903 rmp->external_sw_if_index = ~0;
904 rmp->vrf_id = htonl (m->vrf_id);
905 rmp->context = context;
907 if (m->twice_nat == TWICE_NAT)
908 rmp->flags |= NAT_API_IS_TWICE_NAT;
909 else if (m->twice_nat == TWICE_NAT_SELF)
910 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
912 if (is_out2in_only_static_mapping (m))
913 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
915 if (is_addr_only_static_mapping (m))
917 rmp->flags |= NAT_API_IS_ADDR_ONLY;
921 rmp->protocol = nat_proto_to_ip_proto (m->proto);
922 rmp->external_port = m->external_port;
923 rmp->local_port = m->local_port;
927 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
929 vl_api_send_msg (reg, (u8 *) rmp);
933 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
934 vl_api_registration_t * reg,
937 vl_api_nat44_static_mapping_details_t *rmp;
938 snat_main_t *sm = &snat_main;
940 rmp = vl_msg_api_alloc (sizeof (*rmp));
941 clib_memset (rmp, 0, sizeof (*rmp));
943 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
944 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
945 rmp->external_sw_if_index = htonl (m->sw_if_index);
946 rmp->vrf_id = htonl (m->vrf_id);
947 rmp->context = context;
950 rmp->flags |= NAT_API_IS_TWICE_NAT;
954 rmp->flags |= NAT_API_IS_ADDR_ONLY;
958 rmp->protocol = nat_proto_to_ip_proto (m->proto);
959 rmp->external_port = m->e_port;
960 rmp->local_port = m->l_port;
963 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
965 vl_api_send_msg (reg, (u8 *) rmp);
969 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
972 vl_api_registration_t *reg;
973 snat_main_t *sm = &snat_main;
974 snat_static_mapping_t *m;
975 snat_static_map_resolve_t *rp;
978 reg = vl_api_client_index_to_registration (mp->client_index);
983 pool_foreach (m, sm->static_mappings)
985 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
986 send_nat44_static_mapping_details (m, reg, mp->context);
990 for (j = 0; j < vec_len (sm->to_resolve); j++)
992 rp = sm->to_resolve + j;
993 if (!rp->identity_nat)
994 send_nat44_static_map_resolve_details (rp, reg, mp->context);
999 vl_api_nat44_add_del_identity_mapping_t_handler
1000 (vl_api_nat44_add_del_identity_mapping_t * mp)
1002 snat_main_t *sm = &snat_main;
1003 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
1004 ip4_address_t addr, pool_addr = { 0 };
1006 u32 vrf_id, sw_if_index;
1008 nat_protocol_t proto = NAT_PROTOCOL_OTHER;
1011 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1014 proto = ip_proto_to_nat_proto (mp->protocol);
1016 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1017 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1018 if (sw_if_index != ~0)
1021 memcpy (&addr.as_u8, mp->ip_address, 4);
1022 mp->tag[sizeof (mp->tag) - 1] = 0;
1023 tag = format (0, "%s", mp->tag);
1024 vec_terminate_c_string (tag);
1027 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1028 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1029 proto, mp->is_add, 0, 0, tag, 1, pool_addr, 0);
1032 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1036 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1037 vl_api_registration_t * reg, u32 context)
1039 vl_api_nat44_identity_mapping_details_t *rmp;
1040 snat_main_t *sm = &snat_main;
1041 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1043 rmp = vl_msg_api_alloc (sizeof (*rmp));
1044 clib_memset (rmp, 0, sizeof (*rmp));
1046 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1048 if (is_addr_only_static_mapping (m))
1049 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1051 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1052 rmp->port = m->local_port;
1053 rmp->sw_if_index = ~0;
1054 rmp->vrf_id = htonl (local->vrf_id);
1055 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1056 rmp->context = context;
1058 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1060 vl_api_send_msg (reg, (u8 *) rmp);
1064 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1065 vl_api_registration_t * reg,
1068 vl_api_nat44_identity_mapping_details_t *rmp;
1069 snat_main_t *sm = &snat_main;
1071 rmp = vl_msg_api_alloc (sizeof (*rmp));
1072 clib_memset (rmp, 0, sizeof (*rmp));
1074 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1077 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1079 rmp->port = m->l_port;
1080 rmp->sw_if_index = htonl (m->sw_if_index);
1081 rmp->vrf_id = htonl (m->vrf_id);
1082 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1083 rmp->context = context;
1085 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1087 vl_api_send_msg (reg, (u8 *) rmp);
1091 vl_api_nat44_identity_mapping_dump_t_handler
1092 (vl_api_nat44_identity_mapping_dump_t * mp)
1094 vl_api_registration_t *reg;
1095 snat_main_t *sm = &snat_main;
1096 snat_static_mapping_t *m;
1097 snat_static_map_resolve_t *rp;
1100 reg = vl_api_client_index_to_registration (mp->client_index);
1105 pool_foreach (m, sm->static_mappings)
1107 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1109 pool_foreach_index (j, m->locals)
1111 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1117 for (j = 0; j < vec_len (sm->to_resolve); j++)
1119 rp = sm->to_resolve + j;
1120 if (rp->identity_nat)
1121 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1126 vl_api_nat44_add_del_interface_addr_t_handler
1127 (vl_api_nat44_add_del_interface_addr_t * mp)
1129 snat_main_t *sm = &snat_main;
1130 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1131 u32 sw_if_index = ntohl (mp->sw_if_index);
1135 is_del = !mp->is_add;
1137 VALIDATE_SW_IF_INDEX (mp);
1139 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1140 mp->flags & NAT_API_IS_TWICE_NAT);
1142 BAD_SW_IF_INDEX_LABEL;
1143 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1147 send_nat44_interface_addr_details (u32 sw_if_index,
1148 vl_api_registration_t * reg, u32 context,
1151 vl_api_nat44_interface_addr_details_t *rmp;
1152 snat_main_t *sm = &snat_main;
1154 rmp = vl_msg_api_alloc (sizeof (*rmp));
1155 clib_memset (rmp, 0, sizeof (*rmp));
1157 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1158 rmp->sw_if_index = ntohl (sw_if_index);
1161 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1162 rmp->context = context;
1164 vl_api_send_msg (reg, (u8 *) rmp);
1168 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1171 vl_api_registration_t *reg;
1172 snat_main_t *sm = &snat_main;
1175 reg = vl_api_client_index_to_registration (mp->client_index);
1180 vec_foreach (i, sm->auto_add_sw_if_indices)
1181 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1182 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1183 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1188 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1191 vl_api_nat44_user_details_t *rmp;
1192 snat_main_t *sm = &snat_main;
1193 ip4_main_t *im = &ip4_main;
1195 rmp = vl_msg_api_alloc (sizeof (*rmp));
1196 clib_memset (rmp, 0, sizeof (*rmp));
1197 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1199 if (!pool_is_free_index (im->fibs, u->fib_index))
1201 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1202 rmp->vrf_id = ntohl (fib->ft_table_id);
1205 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1206 rmp->nsessions = ntohl (u->nsessions);
1207 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1208 rmp->context = context;
1210 vl_api_send_msg (reg, (u8 *) rmp);
1214 nat_ed_user_create_helper (snat_main_per_thread_data_t * tsm,
1218 k.addr = s->in2out.addr;
1219 k.fib_index = s->in2out.fib_index;
1220 clib_bihash_kv_8_8_t key, value;
1223 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1225 pool_get (tsm->users, u);
1227 u->fib_index = k.fib_index;
1229 u->nstaticsessions = 0;
1230 key.value = u - tsm->users;
1231 clib_bihash_add_del_8_8 (&tsm->user_hash, &key, 1);
1235 u = pool_elt_at_index (tsm->users, value.value);
1237 if (snat_is_session_static (s))
1239 ++u->nstaticsessions;
1248 nat_ed_users_create (snat_main_per_thread_data_t * tsm)
1252 pool_foreach (s, tsm->sessions) { nat_ed_user_create_helper (tsm, s); }
1257 nat_ed_users_destroy (snat_main_per_thread_data_t * tsm)
1261 pool_flush (u, tsm->users, { });
1263 clib_bihash_free_8_8 (&tsm->user_hash);
1264 clib_bihash_init_8_8 (&tsm->user_hash, "users", snat_main.user_buckets, 0);
1265 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
1269 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1271 vl_api_registration_t *reg;
1272 snat_main_t *sm = &snat_main;
1273 snat_main_per_thread_data_t *tsm;
1276 reg = vl_api_client_index_to_registration (mp->client_index);
1281 vec_foreach (tsm, sm->per_thread_data)
1283 if (sm->endpoint_dependent)
1285 nat_ed_users_create (tsm);
1287 pool_foreach (u, tsm->users)
1289 send_nat44_user_details (u, reg, mp->context);
1291 if (sm->endpoint_dependent)
1293 nat_ed_users_destroy (tsm);
1300 send_nat44_user_session_details (snat_session_t * s,
1301 vl_api_registration_t * reg, u32 context)
1303 vl_api_nat44_user_session_details_t *rmp;
1304 snat_main_t *sm = &snat_main;
1306 rmp = vl_msg_api_alloc (sizeof (*rmp));
1307 clib_memset (rmp, 0, sizeof (*rmp));
1309 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1310 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1311 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1313 if (snat_is_session_static (s))
1314 rmp->flags |= NAT_API_IS_STATIC;
1316 if (is_twice_nat_session (s))
1317 rmp->flags |= NAT_API_IS_TWICE_NAT;
1319 if (is_ed_session (s) || is_fwd_bypass_session (s))
1320 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1322 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1323 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1324 rmp->total_pkts = ntohl (s->total_pkts);
1325 rmp->context = context;
1326 if (snat_is_unk_proto_session (s))
1328 rmp->outside_port = 0;
1329 rmp->inside_port = 0;
1330 rmp->protocol = ntohs (s->in2out.port);
1334 rmp->outside_port = s->out2in.port;
1335 rmp->inside_port = s->in2out.port;
1336 rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1338 if (is_ed_session (s) || is_fwd_bypass_session (s))
1340 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1341 rmp->ext_host_port = s->ext_host_port;
1342 if (is_twice_nat_session (s))
1344 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1345 rmp->ext_host_nat_port = s->ext_host_nat_port;
1349 vl_api_send_msg (reg, (u8 *) rmp);
1353 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1356 vl_api_registration_t *reg;
1357 snat_main_t *sm = &snat_main;
1358 snat_main_per_thread_data_t *tsm;
1360 clib_bihash_kv_8_8_t key, value;
1361 snat_user_key_t ukey;
1363 u32 session_index, head_index, elt_index;
1364 dlist_elt_t *head, *elt;
1367 reg = vl_api_client_index_to_registration (mp->client_index);
1371 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1372 ip.src_address.as_u32 = ukey.addr.as_u32;
1373 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1374 key.key = ukey.as_u64;
1375 if (sm->num_workers > 1)
1377 vec_elt_at_index (sm->per_thread_data,
1378 sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1380 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1381 if (!sm->endpoint_dependent)
1383 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1385 u = pool_elt_at_index (tsm->users, value.value);
1386 if (!u->nsessions && !u->nstaticsessions)
1389 head_index = u->sessions_per_user_list_head_index;
1390 head = pool_elt_at_index (tsm->list_pool, head_index);
1391 elt_index = head->next;
1392 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1393 session_index = elt->value;
1394 while (session_index != ~0)
1396 s = pool_elt_at_index (tsm->sessions, session_index);
1398 send_nat44_user_session_details (s, reg, mp->context);
1400 elt_index = elt->next;
1401 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1402 session_index = elt->value;
1408 pool_foreach (s, tsm->sessions) {
1409 if (s->in2out.addr.as_u32 == ukey.addr.as_u32)
1411 send_nat44_user_session_details (s, reg, mp->context);
1418 static nat44_lb_addr_port_t *
1419 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1420 u32 addr_port_pair_num)
1423 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1424 vl_api_nat44_lb_addr_port_t *ap;
1426 for (i = 0; i < addr_port_pair_num; i++)
1428 ap = &addr_port_pairs[i];
1429 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1430 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1431 lb_addr_port.port = ap->port;
1432 lb_addr_port.probability = ap->probability;
1433 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1434 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1437 return lb_addr_port_pairs;
1441 vl_api_nat44_add_del_lb_static_mapping_t_handler
1442 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1444 snat_main_t *sm = &snat_main;
1445 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1446 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1448 nat44_lb_addr_port_t *locals = 0;
1449 ip4_address_t e_addr;
1450 nat_protocol_t proto;
1453 if (!sm->endpoint_dependent)
1455 rv = VNET_API_ERROR_UNSUPPORTED;
1460 unformat_nat44_lb_addr_port (mp->locals,
1461 clib_net_to_host_u32 (mp->local_num));
1462 clib_memcpy (&e_addr, mp->external_addr, 4);
1463 proto = ip_proto_to_nat_proto (mp->protocol);
1465 if (mp->flags & NAT_API_IS_TWICE_NAT)
1466 twice_nat = TWICE_NAT;
1467 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1468 twice_nat = TWICE_NAT_SELF;
1469 mp->tag[sizeof (mp->tag) - 1] = 0;
1470 tag = format (0, "%s", mp->tag);
1471 vec_terminate_c_string (tag);
1474 nat44_add_del_lb_static_mapping (e_addr,
1476 proto, locals, mp->is_add,
1478 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1479 clib_net_to_host_u32 (mp->affinity));
1485 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1489 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1490 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1492 snat_main_t *sm = &snat_main;
1493 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1495 ip4_address_t e_addr, l_addr;
1496 nat_protocol_t proto;
1498 if (!sm->endpoint_dependent)
1500 rv = VNET_API_ERROR_UNSUPPORTED;
1504 clib_memcpy (&e_addr, mp->external_addr, 4);
1505 clib_memcpy (&l_addr, mp->local.addr, 4);
1506 proto = ip_proto_to_nat_proto (mp->protocol);
1509 nat44_lb_static_mapping_add_del_local (e_addr,
1514 clib_net_to_host_u32 (mp->
1516 mp->local.probability, mp->is_add);
1519 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1523 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1524 vl_api_registration_t * reg,
1527 vl_api_nat44_lb_static_mapping_details_t *rmp;
1528 snat_main_t *sm = &snat_main;
1529 nat44_lb_addr_port_t *ap;
1530 vl_api_nat44_lb_addr_port_t *locals;
1534 vl_msg_api_alloc (sizeof (*rmp) +
1535 (pool_elts (m->locals) *
1536 sizeof (nat44_lb_addr_port_t)));
1537 clib_memset (rmp, 0, sizeof (*rmp));
1539 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1541 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1542 rmp->external_port = m->external_port;
1543 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1544 rmp->context = context;
1546 if (m->twice_nat == TWICE_NAT)
1547 rmp->flags |= NAT_API_IS_TWICE_NAT;
1548 else if (m->twice_nat == TWICE_NAT_SELF)
1549 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1550 if (is_out2in_only_static_mapping (m))
1551 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1553 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1555 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1557 pool_foreach (ap, m->locals)
1559 clib_memcpy (locals->addr, &(ap->addr), 4);
1560 locals->port = ap->port;
1561 locals->probability = ap->probability;
1562 locals->vrf_id = ntohl (ap->vrf_id);
1567 rmp->local_num = ntohl (local_num);
1569 vl_api_send_msg (reg, (u8 *) rmp);
1573 vl_api_nat44_lb_static_mapping_dump_t_handler
1574 (vl_api_nat44_lb_static_mapping_dump_t * mp)
1576 vl_api_registration_t *reg;
1577 snat_main_t *sm = &snat_main;
1578 snat_static_mapping_t *m;
1580 if (!sm->endpoint_dependent)
1583 reg = vl_api_client_index_to_registration (mp->client_index);
1588 pool_foreach (m, sm->static_mappings)
1590 if (is_lb_static_mapping(m))
1591 send_nat44_lb_static_mapping_details (m, reg, mp->context);
1597 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1599 snat_main_t *sm = &snat_main;
1600 vl_api_nat44_del_session_reply_t *rmp;
1601 ip4_address_t addr, eh_addr;
1606 nat_protocol_t proto;
1608 memcpy (&addr.as_u8, mp->address, 4);
1610 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1611 proto = ip_proto_to_nat_proto (mp->protocol);
1612 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1613 eh_port = mp->ext_host_port;
1615 is_in = mp->flags & NAT_API_IS_INSIDE;
1617 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1619 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
1622 rv = nat44_ei_del_session (sm, &addr, port, proto, vrf_id, is_in);
1624 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1628 vl_api_nat44_forwarding_enable_disable_t_handler
1629 (vl_api_nat44_forwarding_enable_disable_t * mp)
1631 snat_main_t *sm = &snat_main;
1632 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1634 u32 *ses_to_be_removed = 0, *ses_index;
1635 snat_main_per_thread_data_t *tsm;
1638 sm->forwarding_enabled = mp->enable != 0;
1640 if (mp->enable == 0)
1643 vec_foreach (tsm, sm->per_thread_data)
1645 pool_foreach (s, tsm->sessions)
1647 if (is_fwd_bypass_session(s))
1649 vec_add1 (ses_to_be_removed, s - tsm->sessions);
1652 if(sm->endpoint_dependent){
1653 vec_foreach (ses_index, ses_to_be_removed)
1655 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1656 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1657 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1660 vec_foreach (ses_index, ses_to_be_removed)
1662 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1663 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1664 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1667 vec_free (ses_to_be_removed);
1672 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1676 vl_api_nat44_forwarding_is_enabled_t_handler
1677 (vl_api_nat44_forwarding_is_enabled_t * mp)
1679 vl_api_registration_t *reg;
1680 snat_main_t *sm = &snat_main;
1681 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1683 reg = vl_api_client_index_to_registration (mp->client_index);
1687 rmp = vl_msg_api_alloc (sizeof (*rmp));
1688 clib_memset (rmp, 0, sizeof (*rmp));
1690 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1691 rmp->context = mp->context;
1693 rmp->enabled = sm->forwarding_enabled;
1695 vl_api_send_msg (reg, (u8 *) rmp);
1698 /* API definitions */
1699 #include <vnet/format_fns.h>
1700 #include <nat/nat44.api.c>
1702 /* Set up the API message handling tables */
1704 nat44_api_hookup (vlib_main_t * vm)
1706 snat_main_t *sm = &snat_main;
1707 sm->msg_id_base = setup_message_id_table ();
1712 * fd.io coding-style-patch-verification: ON
1715 * eval: (c-set-style "gnu")