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 #define vl_api_nat44_lb_static_mapping_details_t_endian vl_noop_handler
22 #define vl_api_nat44_add_del_lb_static_mapping_t_endian vl_noop_handler
24 #include <vnet/ip/ip_types_api.h>
25 #include <vlibmemory/api.h>
27 #include <vnet/fib/fib_table.h>
29 #include <nat/lib/nat_inlines.h>
30 #include <nat/lib/ipfix_logging.h>
34 #include <nat/nat44.api_enum.h>
35 #include <nat/nat44.api_types.h>
37 #include <nat/nat_ha.h>
38 #include <nat/nat_inlines.h>
40 #include <nat/nat44/inlines.h>
41 #include <nat/nat44/ed_inlines.h>
43 #define REPLY_MSG_ID_BASE sm->msg_id_base
44 #include <vlibapi/api_helper_macros.h>
47 vl_api_nat_control_ping_t_handler (vl_api_nat_control_ping_t * mp)
49 vl_api_nat_control_ping_reply_t *rmp;
50 snat_main_t *sm = &snat_main;
54 REPLY_MACRO2 (VL_API_NAT_CONTROL_PING_REPLY,
56 rmp->vpe_pid = ntohl (getpid ());
62 vl_api_nat_show_config_t_handler (vl_api_nat_show_config_t * mp)
64 vl_api_nat_show_config_reply_t *rmp;
65 snat_main_t *sm = &snat_main;
69 REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_CONFIG_REPLY,
71 rmp->translation_buckets = htonl (sm->translation_buckets);
72 rmp->user_buckets = htonl (sm->user_buckets);
73 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
74 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
75 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
76 rmp->static_mapping_only = sm->static_mapping_only;
77 rmp->static_mapping_connection_tracking =
78 sm->static_mapping_connection_tracking;
79 rmp->endpoint_dependent = sm->endpoint_dependent;
80 rmp->out2in_dpo = sm->out2in_dpo;
86 vl_api_nat_show_config_2_t_handler (vl_api_nat_show_config_2_t * mp)
88 vl_api_nat_show_config_2_reply_t *rmp;
89 snat_main_t *sm = &snat_main;
93 REPLY_MACRO2_ZERO (VL_API_NAT_SHOW_CONFIG_2_REPLY,
95 rmp->translation_buckets = htonl (sm->translation_buckets);
96 rmp->user_buckets = htonl (sm->user_buckets);
97 rmp->max_translations_per_user = htonl (sm->max_translations_per_user);
98 rmp->outside_vrf_id = htonl (sm->outside_vrf_id);
99 rmp->inside_vrf_id = htonl (sm->inside_vrf_id);
100 rmp->static_mapping_only = sm->static_mapping_only;
101 rmp->static_mapping_connection_tracking =
102 sm->static_mapping_connection_tracking;
103 rmp->endpoint_dependent = sm->endpoint_dependent;
104 rmp->out2in_dpo = sm->out2in_dpo;
105 rmp->max_translations_per_thread = clib_net_to_host_u32(sm->max_translations_per_thread);
106 rmp->max_users_per_thread = clib_net_to_host_u32(sm->max_users_per_thread);
112 vl_api_nat44_show_running_config_t_handler (vl_api_nat44_show_running_config_t
115 vl_api_nat44_show_running_config_reply_t *rmp;
116 snat_main_t *sm = &snat_main;
117 nat44_config_t *rc = &sm->rconfig;
121 REPLY_MACRO2_ZERO (VL_API_NAT44_SHOW_RUNNING_CONFIG_REPLY,
123 rmp->inside_vrf = htonl (rc->inside_vrf);
124 rmp->outside_vrf = htonl (rc->outside_vrf);
125 rmp->users = htonl (rc->users);
126 rmp->sessions = htonl (rc->sessions);
127 rmp->user_sessions = htonl (rc->user_sessions);
129 rmp->user_buckets = htonl (sm->user_buckets);
130 rmp->translation_buckets = htonl (sm->translation_buckets);
132 if (rc->endpoint_dependent)
133 rmp->flags |= NAT44_IS_ENDPOINT_DEPENDENT;
135 rmp->flags |= NAT44_IS_ENDPOINT_INDEPENDENT;
137 if (rc->static_mapping_only)
138 rmp->flags |= NAT44_IS_STATIC_MAPPING_ONLY;
139 if (rc->connection_tracking)
140 rmp->flags |= NAT44_IS_CONNECTION_TRACKING;
142 rmp->flags |= NAT44_IS_OUT2IN_DPO;
148 vl_api_nat_set_workers_t_handler (vl_api_nat_set_workers_t * mp)
150 snat_main_t *sm = &snat_main;
151 vl_api_nat_set_workers_reply_t *rmp;
156 mask = clib_net_to_host_u64 (mp->worker_mask);
158 if (sm->num_workers < 2)
160 rv = VNET_API_ERROR_FEATURE_DISABLED;
164 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
165 rv = snat_set_workers (bitmap);
166 clib_bitmap_free (bitmap);
169 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
173 send_nat_worker_details (u32 worker_index, vl_api_registration_t * reg,
176 vl_api_nat_worker_details_t *rmp;
177 snat_main_t *sm = &snat_main;
178 vlib_worker_thread_t *w =
179 vlib_worker_threads + worker_index + sm->first_worker_index;
181 rmp = vl_msg_api_alloc (sizeof (*rmp));
182 clib_memset (rmp, 0, sizeof (*rmp));
183 rmp->_vl_msg_id = ntohs (VL_API_NAT_WORKER_DETAILS + sm->msg_id_base);
184 rmp->context = context;
185 rmp->worker_index = htonl (worker_index);
186 rmp->lcore_id = htonl (w->cpu_id);
187 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
189 vl_api_send_msg (reg, (u8 *) rmp);
193 vl_api_nat_worker_dump_t_handler (vl_api_nat_worker_dump_t * mp)
195 vl_api_registration_t *reg;
196 snat_main_t *sm = &snat_main;
199 reg = vl_api_client_index_to_registration (mp->client_index);
204 vec_foreach (worker_index, sm->workers)
205 send_nat_worker_details(*worker_index, reg, mp->context);
210 vl_api_nat44_session_cleanup_t_handler (vl_api_nat44_session_cleanup_t * mp)
212 snat_main_t *sm = &snat_main;
213 vl_api_nat44_session_cleanup_reply_t *rmp;
215 REPLY_MACRO (VL_API_NAT44_SESSION_CLEANUP_REPLY);
219 vl_api_nat44_set_session_limit_t_handler (vl_api_nat44_set_session_limit_t *
222 snat_main_t *sm = &snat_main;
223 vl_api_nat44_set_session_limit_reply_t *rmp;
226 rv = nat44_set_session_limit
227 (ntohl (mp->session_limit), ntohl (mp->vrf_id));
229 REPLY_MACRO (VL_API_NAT44_SET_SESSION_LIMIT_REPLY);
233 vl_api_nat_set_log_level_t_handler (vl_api_nat_set_log_level_t * mp)
235 snat_main_t *sm = &snat_main;
236 vl_api_nat_set_log_level_reply_t *rmp;
239 if (sm->log_level > NAT_LOG_DEBUG)
240 rv = VNET_API_ERROR_UNSUPPORTED;
242 sm->log_level = mp->log_level;
244 REPLY_MACRO (VL_API_NAT_SET_WORKERS_REPLY);
248 vl_api_nat44_plugin_enable_disable_t_handler
249 (vl_api_nat44_plugin_enable_disable_t * mp)
251 snat_main_t *sm = &snat_main;
252 nat44_config_t c = { 0 };
253 vl_api_nat44_plugin_enable_disable_reply_t *rmp;
258 c.endpoint_dependent = mp->flags & NAT44_API_IS_ENDPOINT_DEPENDENT;
259 c.static_mapping_only = mp->flags & NAT44_API_IS_STATIC_MAPPING_ONLY;
260 c.connection_tracking = mp->flags & NAT44_API_IS_CONNECTION_TRACKING;
261 c.out2in_dpo = mp->flags & NAT44_API_IS_OUT2IN_DPO;
263 c.inside_vrf = ntohl (mp->inside_vrf);
264 c.outside_vrf = ntohl (mp->outside_vrf);
266 c.users = ntohl (mp->users);
268 c.sessions = ntohl (mp->sessions);
270 c.user_sessions = ntohl (mp->user_sessions);
272 rv = nat44_plugin_enable (c);
275 rv = nat44_plugin_disable ();
277 REPLY_MACRO (VL_API_NAT44_PLUGIN_ENABLE_DISABLE_REPLY);
281 vl_api_nat_ipfix_enable_disable_t_handler (vl_api_nat_ipfix_enable_disable_t *
284 snat_main_t *sm = &snat_main;
285 vl_api_nat_ipfix_enable_disable_reply_t *rmp;
288 rv = nat_ipfix_logging_enable_disable (mp->enable,
291 clib_host_to_net_u16 (mp->src_port));
293 REPLY_MACRO (VL_API_NAT_IPFIX_ENABLE_DISABLE_REPLY);
297 vl_api_nat_set_timeouts_t_handler (vl_api_nat_set_timeouts_t * mp)
299 snat_main_t *sm = &snat_main;
300 vl_api_nat_set_timeouts_reply_t *rmp;
303 sm->udp_timeout = ntohl (mp->udp);
304 sm->tcp_established_timeout = ntohl (mp->tcp_established);
305 sm->tcp_transitory_timeout = ntohl (mp->tcp_transitory);
306 sm->icmp_timeout = ntohl (mp->icmp);
308 REPLY_MACRO (VL_API_NAT_SET_TIMEOUTS_REPLY);
312 vl_api_nat_get_timeouts_t_handler (vl_api_nat_get_timeouts_t * mp)
314 snat_main_t *sm = &snat_main;
315 vl_api_nat_get_timeouts_reply_t *rmp;
319 REPLY_MACRO2 (VL_API_NAT_GET_TIMEOUTS_REPLY,
321 rmp->udp = htonl (sm->udp_timeout);
322 rmp->tcp_established = htonl (sm->tcp_established_timeout);
323 rmp->tcp_transitory = htonl (sm->tcp_transitory_timeout);
324 rmp->icmp = htonl (sm->icmp_timeout);
330 vl_api_nat_set_addr_and_port_alloc_alg_t_handler
331 (vl_api_nat_set_addr_and_port_alloc_alg_t * mp)
333 snat_main_t *sm = &snat_main;
334 vl_api_nat_set_addr_and_port_alloc_alg_reply_t *rmp;
336 u16 port_start, port_end;
340 case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
341 nat_set_alloc_addr_and_port_default ();
343 case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
344 nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
347 case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
348 port_start = ntohs (mp->start_port);
349 port_end = ntohs (mp->end_port);
350 if (port_end <= port_start)
352 rv = VNET_API_ERROR_INVALID_VALUE;
355 nat_set_alloc_addr_and_port_range (port_start, port_end);
358 rv = VNET_API_ERROR_INVALID_VALUE;
363 REPLY_MACRO (VL_API_NAT_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
367 vl_api_nat_get_addr_and_port_alloc_alg_t_handler
368 (vl_api_nat_get_addr_and_port_alloc_alg_t * mp)
370 snat_main_t *sm = &snat_main;
371 vl_api_nat_get_addr_and_port_alloc_alg_reply_t *rmp;
375 REPLY_MACRO2 (VL_API_NAT_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY,
377 rmp->alg = sm->addr_and_port_alloc_alg;
378 rmp->psid_offset = sm->psid_offset;
379 rmp->psid_length = sm->psid_length;
380 rmp->psid = htons (sm->psid);
381 rmp->start_port = htons (sm->start_port);
382 rmp->end_port = htons (sm->end_port);
388 vl_api_nat_set_mss_clamping_t_handler (vl_api_nat_set_mss_clamping_t * mp)
390 snat_main_t *sm = &snat_main;
391 vl_api_nat_set_mss_clamping_reply_t *rmp;
395 sm->mss_clamping = ntohs (mp->mss_value);
397 sm->mss_clamping = 0;
399 REPLY_MACRO (VL_API_NAT_SET_MSS_CLAMPING_REPLY);
403 vl_api_nat_get_mss_clamping_t_handler (vl_api_nat_get_mss_clamping_t * mp)
405 snat_main_t *sm = &snat_main;
406 vl_api_nat_get_mss_clamping_reply_t *rmp;
410 REPLY_MACRO2 (VL_API_NAT_GET_MSS_CLAMPING_REPLY,
412 rmp->enable = sm->mss_clamping ? 1 : 0;
413 rmp->mss_value = htons (sm->mss_clamping);
419 vl_api_nat_ha_set_listener_t_handler (vl_api_nat_ha_set_listener_t * mp)
421 snat_main_t *sm = &snat_main;
422 vl_api_nat_ha_set_listener_reply_t *rmp;
426 memcpy (&addr, &mp->ip_address, sizeof (addr));
428 nat_ha_set_listener (&addr, clib_net_to_host_u16 (mp->port),
429 clib_net_to_host_u32 (mp->path_mtu));
431 REPLY_MACRO (VL_API_NAT_HA_SET_LISTENER_REPLY);
435 vl_api_nat_ha_get_listener_t_handler (vl_api_nat_ha_get_listener_t * mp)
437 snat_main_t *sm = &snat_main;
438 vl_api_nat_ha_get_listener_reply_t *rmp;
444 nat_ha_get_listener (&addr, &port, &path_mtu);
447 REPLY_MACRO2 (VL_API_NAT_HA_GET_LISTENER_REPLY,
449 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
450 rmp->port = clib_host_to_net_u16 (port);
451 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
457 vl_api_nat_ha_set_failover_t_handler (vl_api_nat_ha_set_failover_t * mp)
459 snat_main_t *sm = &snat_main;
460 vl_api_nat_ha_set_failover_reply_t *rmp;
464 memcpy (&addr, &mp->ip_address, sizeof (addr));
466 nat_ha_set_failover (&addr, clib_net_to_host_u16 (mp->port),
467 clib_net_to_host_u32 (mp->session_refresh_interval));
469 REPLY_MACRO (VL_API_NAT_HA_SET_FAILOVER_REPLY);
473 vl_api_nat_ha_get_failover_t_handler (vl_api_nat_ha_get_failover_t * mp)
475 snat_main_t *sm = &snat_main;
476 vl_api_nat_ha_get_failover_reply_t *rmp;
480 u32 session_refresh_interval;
482 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
485 REPLY_MACRO2 (VL_API_NAT_HA_GET_FAILOVER_REPLY,
487 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
488 rmp->port = clib_host_to_net_u16 (port);
489 rmp->session_refresh_interval = clib_host_to_net_u32 (session_refresh_interval);
495 vl_api_nat_ha_flush_t_handler (vl_api_nat_ha_flush_t * mp)
497 snat_main_t *sm = &snat_main;
498 vl_api_nat_ha_flush_reply_t *rmp;
503 REPLY_MACRO (VL_API_NAT_HA_FLUSH_REPLY);
507 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
509 snat_main_t *sm = &snat_main;
510 vl_api_registration_t *reg;
511 vl_api_nat_ha_resync_completed_event_t *mp;
513 reg = vl_api_client_index_to_registration (client_index);
517 mp = vl_msg_api_alloc (sizeof (*mp));
518 clib_memset (mp, 0, sizeof (*mp));
519 mp->client_index = client_index;
521 mp->missed_count = clib_host_to_net_u32 (missed_count);
523 ntohs (VL_API_NAT_HA_RESYNC_COMPLETED_EVENT + sm->msg_id_base);
525 vl_api_send_msg (reg, (u8 *) mp);
529 vl_api_nat_ha_resync_t_handler (vl_api_nat_ha_resync_t * mp)
531 snat_main_t *sm = &snat_main;
532 vl_api_nat_ha_resync_reply_t *rmp;
536 nat_ha_resync (mp->client_index, mp->pid,
537 mp->want_resync_event ? nat_ha_resync_completed_event_cb :
540 REPLY_MACRO (VL_API_NAT_HA_RESYNC_REPLY);
544 vl_api_nat44_del_user_t_handler (vl_api_nat44_del_user_t * mp)
546 snat_main_t *sm = &snat_main;
547 vl_api_nat44_del_user_reply_t *rmp;
550 memcpy (&addr.as_u8, mp->ip_address, 4);
551 rv = nat44_ei_user_del (&addr, ntohl (mp->fib_index));
552 REPLY_MACRO (VL_API_NAT44_DEL_USER_REPLY);
556 vl_api_nat44_add_del_address_range_t_handler
557 (vl_api_nat44_add_del_address_range_t * mp)
559 snat_main_t *sm = &snat_main;
560 vl_api_nat44_add_del_address_range_reply_t *rmp;
561 ip4_address_t this_addr;
562 u8 is_add, twice_nat;
563 u32 start_host_order, end_host_order;
569 if (sm->static_mapping_only)
571 rv = VNET_API_ERROR_FEATURE_DISABLED;
576 twice_nat = mp->flags & NAT_API_IS_TWICE_NAT;
578 tmp = (u32 *) mp->first_ip_address;
579 start_host_order = clib_host_to_net_u32 (tmp[0]);
580 tmp = (u32 *) mp->last_ip_address;
581 end_host_order = clib_host_to_net_u32 (tmp[0]);
583 count = (end_host_order - start_host_order) + 1;
585 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
588 nat_log_info ("%U - %U, %d addresses...",
589 format_ip4_address, mp->first_ip_address,
590 format_ip4_address, mp->last_ip_address, count);
592 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
594 for (i = 0; i < count; i++)
597 rv = snat_add_address (sm, &this_addr, vrf_id, twice_nat);
599 rv = snat_del_address (sm, this_addr, 0, twice_nat);
605 nat44_add_del_address_dpo (this_addr, is_add);
607 increment_v4_address (&this_addr);
611 REPLY_MACRO (VL_API_NAT44_ADD_DEL_ADDRESS_RANGE_REPLY);
615 send_nat44_address_details (snat_address_t * a,
616 vl_api_registration_t * reg, u32 context,
619 vl_api_nat44_address_details_t *rmp;
620 snat_main_t *sm = &snat_main;
622 rmp = vl_msg_api_alloc (sizeof (*rmp));
623 clib_memset (rmp, 0, sizeof (*rmp));
624 rmp->_vl_msg_id = ntohs (VL_API_NAT44_ADDRESS_DETAILS + sm->msg_id_base);
625 clib_memcpy (rmp->ip_address, &(a->addr), 4);
626 if (a->fib_index != ~0)
628 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
629 rmp->vrf_id = ntohl (fib->ft_table_id);
634 rmp->flags |= NAT_API_IS_TWICE_NAT;
635 rmp->context = context;
637 vl_api_send_msg (reg, (u8 *) rmp);
641 vl_api_nat44_address_dump_t_handler (vl_api_nat44_address_dump_t * mp)
643 vl_api_registration_t *reg;
644 snat_main_t *sm = &snat_main;
647 reg = vl_api_client_index_to_registration (mp->client_index);
652 vec_foreach (a, sm->addresses)
653 send_nat44_address_details (a, reg, mp->context, 0);
654 vec_foreach (a, sm->twice_nat_addresses)
655 send_nat44_address_details (a, reg, mp->context, 1);
660 vl_api_nat44_interface_add_del_feature_t_handler
661 (vl_api_nat44_interface_add_del_feature_t * mp)
663 snat_main_t *sm = &snat_main;
664 vl_api_nat44_interface_add_del_feature_reply_t *rmp;
665 u32 sw_if_index = ntohl (mp->sw_if_index);
669 is_del = !mp->is_add;
671 VALIDATE_SW_IF_INDEX (mp);
674 snat_interface_add_del (sw_if_index, mp->flags & NAT_API_IS_INSIDE,
677 BAD_SW_IF_INDEX_LABEL;
679 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_FEATURE_REPLY);
683 send_nat44_interface_details (snat_interface_t * i,
684 vl_api_registration_t * reg, u32 context)
686 vl_api_nat44_interface_details_t *rmp;
687 snat_main_t *sm = &snat_main;
689 rmp = vl_msg_api_alloc (sizeof (*rmp));
690 clib_memset (rmp, 0, sizeof (*rmp));
691 rmp->_vl_msg_id = ntohs (VL_API_NAT44_INTERFACE_DETAILS + sm->msg_id_base);
692 rmp->sw_if_index = ntohl (i->sw_if_index);
694 if (nat_interface_is_inside (i))
695 rmp->flags |= NAT_API_IS_INSIDE;
696 if (nat_interface_is_outside (i))
697 rmp->flags |= NAT_API_IS_OUTSIDE;
699 rmp->context = context;
701 vl_api_send_msg (reg, (u8 *) rmp);
705 vl_api_nat44_interface_dump_t_handler (vl_api_nat44_interface_dump_t * mp)
707 vl_api_registration_t *reg;
708 snat_main_t *sm = &snat_main;
711 reg = vl_api_client_index_to_registration (mp->client_index);
716 pool_foreach (i, sm->interfaces,
718 send_nat44_interface_details(i, reg, mp->context);
724 vl_api_nat44_interface_add_del_output_feature_t_handler
725 (vl_api_nat44_interface_add_del_output_feature_t * mp)
727 snat_main_t *sm = &snat_main;
728 vl_api_nat44_interface_add_del_output_feature_reply_t *rmp;
729 u32 sw_if_index = ntohl (mp->sw_if_index);
732 VALIDATE_SW_IF_INDEX (mp);
734 rv = snat_interface_add_del_output_feature (sw_if_index,
735 mp->flags & NAT_API_IS_INSIDE,
738 BAD_SW_IF_INDEX_LABEL;
739 REPLY_MACRO (VL_API_NAT44_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
743 send_nat44_interface_output_feature_details (snat_interface_t * i,
744 vl_api_registration_t * reg,
747 vl_api_nat44_interface_output_feature_details_t *rmp;
748 snat_main_t *sm = &snat_main;
750 rmp = vl_msg_api_alloc (sizeof (*rmp));
751 clib_memset (rmp, 0, sizeof (*rmp));
753 ntohs (VL_API_NAT44_INTERFACE_OUTPUT_FEATURE_DETAILS + sm->msg_id_base);
754 rmp->sw_if_index = ntohl (i->sw_if_index);
755 rmp->context = context;
757 if (nat_interface_is_inside (i))
758 rmp->flags |= NAT_API_IS_INSIDE;
760 vl_api_send_msg (reg, (u8 *) rmp);
764 vl_api_nat44_interface_output_feature_dump_t_handler
765 (vl_api_nat44_interface_output_feature_dump_t * mp)
767 vl_api_registration_t *reg;
768 snat_main_t *sm = &snat_main;
771 reg = vl_api_client_index_to_registration (mp->client_index);
776 pool_foreach (i, sm->output_feature_interfaces,
778 send_nat44_interface_output_feature_details(i, reg, mp->context);
784 vl_api_nat44_add_del_static_mapping_t_handler
785 (vl_api_nat44_add_del_static_mapping_t * mp)
787 snat_main_t *sm = &snat_main;
788 vl_api_nat44_add_del_static_mapping_reply_t *rmp;
789 ip4_address_t local_addr, external_addr, pool_addr = { 0 };
790 u16 local_port = 0, external_port = 0;
791 u32 vrf_id, external_sw_if_index;
792 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
794 nat_protocol_t proto;
797 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
798 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
800 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
802 local_port = mp->local_port;
803 external_port = mp->external_port;
806 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
807 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
808 proto = ip_proto_to_nat_proto (mp->protocol);
810 if (mp->flags & NAT_API_IS_TWICE_NAT)
811 twice_nat = TWICE_NAT;
812 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
813 twice_nat = TWICE_NAT_SELF;
814 mp->tag[sizeof (mp->tag) - 1] = 0;
815 tag = format (0, "%s", mp->tag);
816 vec_terminate_c_string (tag);
818 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
819 external_port, vrf_id,
820 mp->flags & NAT_API_IS_ADDR_ONLY,
821 external_sw_if_index, proto,
822 mp->is_add, twice_nat,
823 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
827 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_REPLY);
831 vl_api_nat44_add_del_static_mapping_v2_t_handler
832 (vl_api_nat44_add_del_static_mapping_v2_t * mp)
834 snat_main_t *sm = &snat_main;
835 vl_api_nat44_add_del_static_mapping_v2_reply_t *rmp;
836 ip4_address_t local_addr, external_addr, pool_addr;
837 u16 local_port = 0, external_port = 0;
838 u32 vrf_id, external_sw_if_index;
839 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
841 nat_protocol_t proto;
844 memcpy (&pool_addr.as_u8, mp->pool_ip_address, 4);
845 memcpy (&local_addr.as_u8, mp->local_ip_address, 4);
846 memcpy (&external_addr.as_u8, mp->external_ip_address, 4);
848 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
850 local_port = mp->local_port;
851 external_port = mp->external_port;
854 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
855 external_sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
856 proto = ip_proto_to_nat_proto (mp->protocol);
858 if (mp->flags & NAT_API_IS_TWICE_NAT)
859 twice_nat = TWICE_NAT;
860 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
861 twice_nat = TWICE_NAT_SELF;
862 mp->tag[sizeof (mp->tag) - 1] = 0;
863 tag = format (0, "%s", mp->tag);
864 vec_terminate_c_string (tag);
866 rv = snat_add_static_mapping (local_addr, external_addr, local_port,
867 external_port, vrf_id,
868 mp->flags & NAT_API_IS_ADDR_ONLY,
869 external_sw_if_index, proto,
870 mp->is_add, twice_nat,
871 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag, 0,
872 pool_addr, mp->match_pool);
875 REPLY_MACRO (VL_API_NAT44_ADD_DEL_STATIC_MAPPING_V2_REPLY);
879 send_nat44_static_mapping_details (snat_static_mapping_t * m,
880 vl_api_registration_t * reg, u32 context)
882 vl_api_nat44_static_mapping_details_t *rmp;
883 snat_main_t *sm = &snat_main;
884 u32 len = sizeof (*rmp);
886 rmp = vl_msg_api_alloc (len);
887 clib_memset (rmp, 0, len);
889 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
891 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
892 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
893 rmp->external_sw_if_index = ~0;
894 rmp->vrf_id = htonl (m->vrf_id);
895 rmp->context = context;
897 if (m->twice_nat == TWICE_NAT)
898 rmp->flags |= NAT_API_IS_TWICE_NAT;
899 else if (m->twice_nat == TWICE_NAT_SELF)
900 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
902 if (is_out2in_only_static_mapping (m))
903 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
905 if (is_addr_only_static_mapping (m))
907 rmp->flags |= NAT_API_IS_ADDR_ONLY;
911 rmp->protocol = nat_proto_to_ip_proto (m->proto);
912 rmp->external_port = m->external_port;
913 rmp->local_port = m->local_port;
917 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
919 vl_api_send_msg (reg, (u8 *) rmp);
923 send_nat44_static_map_resolve_details (snat_static_map_resolve_t * m,
924 vl_api_registration_t * reg,
927 vl_api_nat44_static_mapping_details_t *rmp;
928 snat_main_t *sm = &snat_main;
930 rmp = vl_msg_api_alloc (sizeof (*rmp));
931 clib_memset (rmp, 0, sizeof (*rmp));
933 ntohs (VL_API_NAT44_STATIC_MAPPING_DETAILS + sm->msg_id_base);
934 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
935 rmp->external_sw_if_index = htonl (m->sw_if_index);
936 rmp->vrf_id = htonl (m->vrf_id);
937 rmp->context = context;
940 rmp->flags |= NAT_API_IS_TWICE_NAT;
944 rmp->flags |= NAT_API_IS_ADDR_ONLY;
948 rmp->protocol = nat_proto_to_ip_proto (m->proto);
949 rmp->external_port = m->e_port;
950 rmp->local_port = m->l_port;
953 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
955 vl_api_send_msg (reg, (u8 *) rmp);
959 vl_api_nat44_static_mapping_dump_t_handler (vl_api_nat44_static_mapping_dump_t
962 vl_api_registration_t *reg;
963 snat_main_t *sm = &snat_main;
964 snat_static_mapping_t *m;
965 snat_static_map_resolve_t *rp;
968 reg = vl_api_client_index_to_registration (mp->client_index);
973 pool_foreach (m, sm->static_mappings,
975 if (!is_identity_static_mapping(m) && !is_lb_static_mapping (m))
976 send_nat44_static_mapping_details (m, reg, mp->context);
980 for (j = 0; j < vec_len (sm->to_resolve); j++)
982 rp = sm->to_resolve + j;
983 if (!rp->identity_nat)
984 send_nat44_static_map_resolve_details (rp, reg, mp->context);
989 vl_api_nat44_add_del_identity_mapping_t_handler
990 (vl_api_nat44_add_del_identity_mapping_t * mp)
992 snat_main_t *sm = &snat_main;
993 vl_api_nat44_add_del_identity_mapping_reply_t *rmp;
994 ip4_address_t addr, pool_addr = { 0 };
996 u32 vrf_id, sw_if_index;
998 nat_protocol_t proto = NAT_PROTOCOL_OTHER;
1001 if (!(mp->flags & NAT_API_IS_ADDR_ONLY))
1004 proto = ip_proto_to_nat_proto (mp->protocol);
1006 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1007 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1008 if (sw_if_index != ~0)
1011 memcpy (&addr.as_u8, mp->ip_address, 4);
1012 mp->tag[sizeof (mp->tag) - 1] = 0;
1013 tag = format (0, "%s", mp->tag);
1014 vec_terminate_c_string (tag);
1017 snat_add_static_mapping (addr, addr, port, port, vrf_id,
1018 mp->flags & NAT_API_IS_ADDR_ONLY, sw_if_index,
1019 proto, mp->is_add, 0, 0, tag, 1, pool_addr, 0);
1022 REPLY_MACRO (VL_API_NAT44_ADD_DEL_IDENTITY_MAPPING_REPLY);
1026 send_nat44_identity_mapping_details (snat_static_mapping_t * m, int index,
1027 vl_api_registration_t * reg, u32 context)
1029 vl_api_nat44_identity_mapping_details_t *rmp;
1030 snat_main_t *sm = &snat_main;
1031 nat44_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1033 rmp = vl_msg_api_alloc (sizeof (*rmp));
1034 clib_memset (rmp, 0, sizeof (*rmp));
1036 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1038 if (is_addr_only_static_mapping (m))
1039 rmp->flags |= NAT_API_IS_ADDR_ONLY;
1041 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1042 rmp->port = m->local_port;
1043 rmp->sw_if_index = ~0;
1044 rmp->vrf_id = htonl (local->vrf_id);
1045 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1046 rmp->context = context;
1048 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1050 vl_api_send_msg (reg, (u8 *) rmp);
1054 send_nat44_identity_map_resolve_details (snat_static_map_resolve_t * m,
1055 vl_api_registration_t * reg,
1058 vl_api_nat44_identity_mapping_details_t *rmp;
1059 snat_main_t *sm = &snat_main;
1061 rmp = vl_msg_api_alloc (sizeof (*rmp));
1062 clib_memset (rmp, 0, sizeof (*rmp));
1064 ntohs (VL_API_NAT44_IDENTITY_MAPPING_DETAILS + sm->msg_id_base);
1067 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_ADDR_ONLY;
1069 rmp->port = m->l_port;
1070 rmp->sw_if_index = htonl (m->sw_if_index);
1071 rmp->vrf_id = htonl (m->vrf_id);
1072 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1073 rmp->context = context;
1075 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1077 vl_api_send_msg (reg, (u8 *) rmp);
1081 vl_api_nat44_identity_mapping_dump_t_handler
1082 (vl_api_nat44_identity_mapping_dump_t * mp)
1084 vl_api_registration_t *reg;
1085 snat_main_t *sm = &snat_main;
1086 snat_static_mapping_t *m;
1087 snat_static_map_resolve_t *rp;
1090 reg = vl_api_client_index_to_registration (mp->client_index);
1095 pool_foreach (m, sm->static_mappings,
1097 if (is_identity_static_mapping(m) && !is_lb_static_mapping (m))
1099 pool_foreach_index (j, m->locals,
1101 send_nat44_identity_mapping_details (m, j, reg, mp->context);
1107 for (j = 0; j < vec_len (sm->to_resolve); j++)
1109 rp = sm->to_resolve + j;
1110 if (rp->identity_nat)
1111 send_nat44_identity_map_resolve_details (rp, reg, mp->context);
1116 vl_api_nat44_add_del_interface_addr_t_handler
1117 (vl_api_nat44_add_del_interface_addr_t * mp)
1119 snat_main_t *sm = &snat_main;
1120 vl_api_nat44_add_del_interface_addr_reply_t *rmp;
1121 u32 sw_if_index = ntohl (mp->sw_if_index);
1125 is_del = !mp->is_add;
1127 VALIDATE_SW_IF_INDEX (mp);
1129 rv = snat_add_interface_address (sm, sw_if_index, is_del,
1130 mp->flags & NAT_API_IS_TWICE_NAT);
1132 BAD_SW_IF_INDEX_LABEL;
1133 REPLY_MACRO (VL_API_NAT44_ADD_DEL_INTERFACE_ADDR_REPLY);
1137 send_nat44_interface_addr_details (u32 sw_if_index,
1138 vl_api_registration_t * reg, u32 context,
1141 vl_api_nat44_interface_addr_details_t *rmp;
1142 snat_main_t *sm = &snat_main;
1144 rmp = vl_msg_api_alloc (sizeof (*rmp));
1145 clib_memset (rmp, 0, sizeof (*rmp));
1147 ntohs (VL_API_NAT44_INTERFACE_ADDR_DETAILS + sm->msg_id_base);
1148 rmp->sw_if_index = ntohl (sw_if_index);
1151 rmp->flags = (vl_api_nat_config_flags_t) NAT_API_IS_TWICE_NAT;
1152 rmp->context = context;
1154 vl_api_send_msg (reg, (u8 *) rmp);
1158 vl_api_nat44_interface_addr_dump_t_handler (vl_api_nat44_interface_addr_dump_t
1161 vl_api_registration_t *reg;
1162 snat_main_t *sm = &snat_main;
1165 reg = vl_api_client_index_to_registration (mp->client_index);
1170 vec_foreach (i, sm->auto_add_sw_if_indices)
1171 send_nat44_interface_addr_details(*i, reg, mp->context, 0);
1172 vec_foreach (i, sm->auto_add_sw_if_indices_twice_nat)
1173 send_nat44_interface_addr_details(*i, reg, mp->context, 1);
1178 send_nat44_user_details (snat_user_t * u, vl_api_registration_t * reg,
1181 vl_api_nat44_user_details_t *rmp;
1182 snat_main_t *sm = &snat_main;
1183 ip4_main_t *im = &ip4_main;
1185 rmp = vl_msg_api_alloc (sizeof (*rmp));
1186 clib_memset (rmp, 0, sizeof (*rmp));
1187 rmp->_vl_msg_id = ntohs (VL_API_NAT44_USER_DETAILS + sm->msg_id_base);
1189 if (!pool_is_free_index (im->fibs, u->fib_index))
1191 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1192 rmp->vrf_id = ntohl (fib->ft_table_id);
1195 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1196 rmp->nsessions = ntohl (u->nsessions);
1197 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1198 rmp->context = context;
1200 vl_api_send_msg (reg, (u8 *) rmp);
1204 nat_ed_user_create_helper (snat_main_per_thread_data_t * tsm,
1208 k.addr = s->in2out.addr;
1209 k.fib_index = s->in2out.fib_index;
1210 clib_bihash_kv_8_8_t key, value;
1213 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1215 pool_get (tsm->users, u);
1217 u->fib_index = k.fib_index;
1219 u->nstaticsessions = 0;
1220 key.value = u - tsm->users;
1221 clib_bihash_add_del_8_8 (&tsm->user_hash, &key, 1);
1225 u = pool_elt_at_index (tsm->users, value.value);
1227 if (snat_is_session_static (s))
1229 ++u->nstaticsessions;
1238 nat_ed_users_create (snat_main_per_thread_data_t * tsm)
1242 pool_foreach (s, tsm->sessions, { nat_ed_user_create_helper (tsm, s); });
1247 nat_ed_users_destroy (snat_main_per_thread_data_t * tsm)
1251 pool_flush (u, tsm->users, { });
1253 clib_bihash_free_8_8 (&tsm->user_hash);
1254 clib_bihash_init_8_8 (&tsm->user_hash, "users", snat_main.user_buckets, 0);
1255 clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp);
1259 vl_api_nat44_user_dump_t_handler (vl_api_nat44_user_dump_t * mp)
1261 vl_api_registration_t *reg;
1262 snat_main_t *sm = &snat_main;
1263 snat_main_per_thread_data_t *tsm;
1266 reg = vl_api_client_index_to_registration (mp->client_index);
1271 vec_foreach (tsm, sm->per_thread_data)
1273 if (sm->endpoint_dependent)
1275 nat_ed_users_create (tsm);
1277 pool_foreach (u, tsm->users,
1279 send_nat44_user_details (u, reg, mp->context);
1281 if (sm->endpoint_dependent)
1283 nat_ed_users_destroy (tsm);
1290 send_nat44_user_session_details (snat_session_t * s,
1291 vl_api_registration_t * reg, u32 context)
1293 vl_api_nat44_user_session_details_t *rmp;
1294 snat_main_t *sm = &snat_main;
1296 rmp = vl_msg_api_alloc (sizeof (*rmp));
1297 clib_memset (rmp, 0, sizeof (*rmp));
1299 ntohs (VL_API_NAT44_USER_SESSION_DETAILS + sm->msg_id_base);
1300 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1301 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1303 if (snat_is_session_static (s))
1304 rmp->flags |= NAT_API_IS_STATIC;
1306 if (is_twice_nat_session (s))
1307 rmp->flags |= NAT_API_IS_TWICE_NAT;
1309 if (is_ed_session (s) || is_fwd_bypass_session (s))
1310 rmp->flags |= NAT_API_IS_EXT_HOST_VALID;
1312 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1313 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1314 rmp->total_pkts = ntohl (s->total_pkts);
1315 rmp->context = context;
1316 if (snat_is_unk_proto_session (s))
1318 rmp->outside_port = 0;
1319 rmp->inside_port = 0;
1320 rmp->protocol = ntohs (s->in2out.port);
1324 rmp->outside_port = s->out2in.port;
1325 rmp->inside_port = s->in2out.port;
1326 rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1328 if (is_ed_session (s) || is_fwd_bypass_session (s))
1330 clib_memcpy (rmp->ext_host_address, &s->ext_host_addr, 4);
1331 rmp->ext_host_port = s->ext_host_port;
1332 if (is_twice_nat_session (s))
1334 clib_memcpy (rmp->ext_host_nat_address, &s->ext_host_nat_addr, 4);
1335 rmp->ext_host_nat_port = s->ext_host_nat_port;
1339 vl_api_send_msg (reg, (u8 *) rmp);
1343 vl_api_nat44_user_session_dump_t_handler (vl_api_nat44_user_session_dump_t *
1346 vl_api_registration_t *reg;
1347 snat_main_t *sm = &snat_main;
1348 snat_main_per_thread_data_t *tsm;
1350 clib_bihash_kv_8_8_t key, value;
1351 snat_user_key_t ukey;
1353 u32 session_index, head_index, elt_index;
1354 dlist_elt_t *head, *elt;
1357 reg = vl_api_client_index_to_registration (mp->client_index);
1361 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1362 ip.src_address.as_u32 = ukey.addr.as_u32;
1363 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1364 key.key = ukey.as_u64;
1365 if (sm->num_workers > 1)
1367 vec_elt_at_index (sm->per_thread_data,
1368 sm->worker_in2out_cb (&ip, ukey.fib_index, 0));
1370 tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
1371 if (!sm->endpoint_dependent)
1373 if (clib_bihash_search_8_8 (&tsm->user_hash, &key, &value))
1375 u = pool_elt_at_index (tsm->users, value.value);
1376 if (!u->nsessions && !u->nstaticsessions)
1379 head_index = u->sessions_per_user_list_head_index;
1380 head = pool_elt_at_index (tsm->list_pool, head_index);
1381 elt_index = head->next;
1382 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1383 session_index = elt->value;
1384 while (session_index != ~0)
1386 s = pool_elt_at_index (tsm->sessions, session_index);
1388 send_nat44_user_session_details (s, reg, mp->context);
1390 elt_index = elt->next;
1391 elt = pool_elt_at_index (tsm->list_pool, elt_index);
1392 session_index = elt->value;
1398 pool_foreach (s, tsm->sessions, {
1399 if (s->in2out.addr.as_u32 == ukey.addr.as_u32)
1401 send_nat44_user_session_details (s, reg, mp->context);
1408 static nat44_lb_addr_port_t *
1409 unformat_nat44_lb_addr_port (vl_api_nat44_lb_addr_port_t * addr_port_pairs,
1410 u32 addr_port_pair_num)
1413 nat44_lb_addr_port_t *lb_addr_port_pairs = 0, lb_addr_port;
1414 vl_api_nat44_lb_addr_port_t *ap;
1416 for (i = 0; i < addr_port_pair_num; i++)
1418 ap = &addr_port_pairs[i];
1419 clib_memset (&lb_addr_port, 0, sizeof (lb_addr_port));
1420 clib_memcpy (&lb_addr_port.addr, ap->addr, 4);
1421 lb_addr_port.port = ap->port;
1422 lb_addr_port.probability = ap->probability;
1423 lb_addr_port.vrf_id = clib_net_to_host_u32 (ap->vrf_id);
1424 vec_add1 (lb_addr_port_pairs, lb_addr_port);
1427 return lb_addr_port_pairs;
1431 vl_api_nat44_add_del_lb_static_mapping_t_handler
1432 (vl_api_nat44_add_del_lb_static_mapping_t * mp)
1434 snat_main_t *sm = &snat_main;
1435 vl_api_nat44_add_del_lb_static_mapping_reply_t *rmp;
1436 twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
1438 nat44_lb_addr_port_t *locals = 0;
1439 ip4_address_t e_addr;
1440 nat_protocol_t proto;
1443 if (!sm->endpoint_dependent)
1445 rv = VNET_API_ERROR_UNSUPPORTED;
1450 unformat_nat44_lb_addr_port (mp->locals,
1451 clib_net_to_host_u32 (mp->local_num));
1452 clib_memcpy (&e_addr, mp->external_addr, 4);
1453 proto = ip_proto_to_nat_proto (mp->protocol);
1455 if (mp->flags & NAT_API_IS_TWICE_NAT)
1456 twice_nat = TWICE_NAT;
1457 else if (mp->flags & NAT_API_IS_SELF_TWICE_NAT)
1458 twice_nat = TWICE_NAT_SELF;
1459 mp->tag[sizeof (mp->tag) - 1] = 0;
1460 tag = format (0, "%s", mp->tag);
1461 vec_terminate_c_string (tag);
1464 nat44_add_del_lb_static_mapping (e_addr,
1466 proto, locals, mp->is_add,
1468 mp->flags & NAT_API_IS_OUT2IN_ONLY, tag,
1469 clib_net_to_host_u32 (mp->affinity));
1475 REPLY_MACRO (VL_API_NAT44_ADD_DEL_LB_STATIC_MAPPING_REPLY);
1479 vl_api_nat44_lb_static_mapping_add_del_local_t_handler
1480 (vl_api_nat44_lb_static_mapping_add_del_local_t * mp)
1482 snat_main_t *sm = &snat_main;
1483 vl_api_nat44_lb_static_mapping_add_del_local_reply_t *rmp;
1485 ip4_address_t e_addr, l_addr;
1486 nat_protocol_t proto;
1488 if (!sm->endpoint_dependent)
1490 rv = VNET_API_ERROR_UNSUPPORTED;
1494 clib_memcpy (&e_addr, mp->external_addr, 4);
1495 clib_memcpy (&l_addr, mp->local.addr, 4);
1496 proto = ip_proto_to_nat_proto (mp->protocol);
1499 nat44_lb_static_mapping_add_del_local (e_addr,
1500 clib_net_to_host_u16
1501 (mp->external_port), l_addr,
1502 clib_net_to_host_u16 (mp->
1505 clib_net_to_host_u32 (mp->
1507 mp->local.probability, mp->is_add);
1510 REPLY_MACRO (VL_API_NAT44_LB_STATIC_MAPPING_ADD_DEL_LOCAL_REPLY);
1514 send_nat44_lb_static_mapping_details (snat_static_mapping_t * m,
1515 vl_api_registration_t * reg,
1518 vl_api_nat44_lb_static_mapping_details_t *rmp;
1519 snat_main_t *sm = &snat_main;
1520 nat44_lb_addr_port_t *ap;
1521 vl_api_nat44_lb_addr_port_t *locals;
1525 vl_msg_api_alloc (sizeof (*rmp) +
1526 (pool_elts (m->locals) *
1527 sizeof (nat44_lb_addr_port_t)));
1528 clib_memset (rmp, 0, sizeof (*rmp));
1530 ntohs (VL_API_NAT44_LB_STATIC_MAPPING_DETAILS + sm->msg_id_base);
1532 clib_memcpy (rmp->external_addr, &(m->external_addr), 4);
1533 rmp->external_port = m->external_port;
1534 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1535 rmp->context = context;
1537 if (m->twice_nat == TWICE_NAT)
1538 rmp->flags |= NAT_API_IS_TWICE_NAT;
1539 else if (m->twice_nat == TWICE_NAT_SELF)
1540 rmp->flags |= NAT_API_IS_SELF_TWICE_NAT;
1541 if (is_out2in_only_static_mapping (m))
1542 rmp->flags |= NAT_API_IS_OUT2IN_ONLY;
1544 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1546 locals = (vl_api_nat44_lb_addr_port_t *) rmp->locals;
1548 pool_foreach (ap, m->locals,
1550 clib_memcpy (locals->addr, &(ap->addr), 4);
1551 locals->port = ap->port;
1552 locals->probability = ap->probability;
1553 locals->vrf_id = ntohl (ap->vrf_id);
1558 rmp->local_num = ntohl (local_num);
1560 vl_api_send_msg (reg, (u8 *) rmp);
1564 vl_api_nat44_lb_static_mapping_dump_t_handler
1565 (vl_api_nat44_lb_static_mapping_dump_t * mp)
1567 vl_api_registration_t *reg;
1568 snat_main_t *sm = &snat_main;
1569 snat_static_mapping_t *m;
1571 if (!sm->endpoint_dependent)
1574 reg = vl_api_client_index_to_registration (mp->client_index);
1579 pool_foreach (m, sm->static_mappings,
1581 if (is_lb_static_mapping(m))
1582 send_nat44_lb_static_mapping_details (m, reg, mp->context);
1588 vl_api_nat44_del_session_t_handler (vl_api_nat44_del_session_t * mp)
1590 snat_main_t *sm = &snat_main;
1591 vl_api_nat44_del_session_reply_t *rmp;
1592 ip4_address_t addr, eh_addr;
1597 nat_protocol_t proto;
1599 memcpy (&addr.as_u8, mp->address, 4);
1601 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1602 proto = ip_proto_to_nat_proto (mp->protocol);
1603 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1604 eh_port = mp->ext_host_port;
1606 is_in = mp->flags & NAT_API_IS_INSIDE;
1608 if (mp->flags & NAT_API_IS_EXT_HOST_VALID)
1610 nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port, mp->protocol,
1613 rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
1615 REPLY_MACRO (VL_API_NAT44_DEL_SESSION_REPLY);
1619 vl_api_nat44_forwarding_enable_disable_t_handler
1620 (vl_api_nat44_forwarding_enable_disable_t * mp)
1622 snat_main_t *sm = &snat_main;
1623 vl_api_nat44_forwarding_enable_disable_reply_t *rmp;
1625 u32 *ses_to_be_removed = 0, *ses_index;
1626 snat_main_per_thread_data_t *tsm;
1629 sm->forwarding_enabled = mp->enable != 0;
1631 if (mp->enable == 0)
1634 vec_foreach (tsm, sm->per_thread_data)
1636 pool_foreach (s, tsm->sessions,
1638 if (is_fwd_bypass_session(s))
1640 vec_add1 (ses_to_be_removed, s - tsm->sessions);
1643 if(sm->endpoint_dependent){
1644 vec_foreach (ses_index, ses_to_be_removed)
1646 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1647 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1648 nat_ed_session_delete (sm, s, tsm - sm->per_thread_data, 1);
1651 vec_foreach (ses_index, ses_to_be_removed)
1653 s = pool_elt_at_index(tsm->sessions, ses_index[0]);
1654 nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0);
1655 nat44_delete_session (sm, s, tsm - sm->per_thread_data);
1658 vec_free (ses_to_be_removed);
1663 REPLY_MACRO (VL_API_NAT44_FORWARDING_ENABLE_DISABLE_REPLY);
1667 vl_api_nat44_forwarding_is_enabled_t_handler
1668 (vl_api_nat44_forwarding_is_enabled_t * mp)
1670 vl_api_registration_t *reg;
1671 snat_main_t *sm = &snat_main;
1672 vl_api_nat44_forwarding_is_enabled_reply_t *rmp;
1674 reg = vl_api_client_index_to_registration (mp->client_index);
1678 rmp = vl_msg_api_alloc (sizeof (*rmp));
1679 clib_memset (rmp, 0, sizeof (*rmp));
1681 ntohs (VL_API_NAT44_FORWARDING_IS_ENABLED_REPLY + sm->msg_id_base);
1682 rmp->context = mp->context;
1684 rmp->enabled = sm->forwarding_enabled;
1686 vl_api_send_msg (reg, (u8 *) rmp);
1689 /* API definitions */
1690 #include <vnet/format_fns.h>
1691 #include <nat/nat44.api.c>
1693 /* Set up the API message handling tables */
1695 nat44_api_hookup (vlib_main_t * vm)
1697 snat_main_t *sm = &snat_main;
1698 sm->msg_id_base = setup_message_id_table ();
1703 * fd.io coding-style-patch-verification: ON
1706 * eval: (c-set-style "gnu")