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.
17 #include <vnet/ip/ip_types_api.h>
18 #include <vlibmemory/api.h>
20 #include <vnet/fib/fib_table.h>
22 #include <nat/lib/nat_inlines.h>
23 #include <nat/lib/ipfix_logging.h>
25 #include <nat/nat44-ei/nat44_ei.api_enum.h>
26 #include <nat/nat44-ei/nat44_ei.api_types.h>
28 #include <nat/nat44-ei/nat44_ei_ha.h>
29 #include <nat/nat44-ei/nat44_ei.h>
31 #define REPLY_MSG_ID_BASE nm->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
35 vl_api_nat44_ei_show_running_config_t_handler (
36 vl_api_nat44_ei_show_running_config_t *mp)
38 vl_api_nat44_ei_show_running_config_reply_t *rmp;
39 nat44_ei_main_t *nm = &nat44_ei_main;
40 nat44_ei_config_t *rc = &nm->rconfig;
44 VL_API_NAT44_EI_SHOW_RUNNING_CONFIG_REPLY, ({
45 rmp->inside_vrf = htonl (rc->inside_vrf);
46 rmp->outside_vrf = htonl (rc->outside_vrf);
47 rmp->users = htonl (rc->users);
48 rmp->sessions = htonl (rc->sessions);
49 rmp->user_sessions = htonl (rc->user_sessions);
51 rmp->user_buckets = htonl (nm->user_buckets);
52 rmp->translation_buckets = htonl (nm->translation_buckets);
54 rmp->timeouts.udp = htonl (nm->timeouts.udp);
55 rmp->timeouts.tcp_established = htonl (nm->timeouts.tcp.established);
56 rmp->timeouts.tcp_transitory = htonl (nm->timeouts.tcp.transitory);
57 rmp->timeouts.icmp = htonl (nm->timeouts.icmp);
59 rmp->forwarding_enabled = nm->forwarding_enabled == 1;
60 // consider how to split functionality between subplugins
61 rmp->ipfix_logging_enabled = nat_ipfix_logging_enabled ();
63 if (rc->static_mapping_only)
64 rmp->flags |= NAT44_EI_STATIC_MAPPING_ONLY;
65 if (rc->connection_tracking)
66 rmp->flags |= NAT44_EI_CONNECTION_TRACKING;
68 rmp->flags |= NAT44_EI_OUT2IN_DPO;
73 vl_api_nat44_ei_set_workers_t_handler (vl_api_nat44_ei_set_workers_t *mp)
75 nat44_ei_main_t *nm = &nat44_ei_main;
76 vl_api_nat44_ei_set_workers_reply_t *rmp;
81 mask = clib_net_to_host_u64 (mp->worker_mask);
83 if (nm->num_workers < 2)
85 rv = VNET_API_ERROR_FEATURE_DISABLED;
89 bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
90 rv = nat44_ei_set_workers (bitmap);
91 clib_bitmap_free (bitmap);
94 REPLY_MACRO (VL_API_NAT44_EI_SET_WORKERS_REPLY);
98 send_nat_worker_details (u32 worker_index, vl_api_registration_t *reg,
101 vl_api_nat44_ei_worker_details_t *rmp;
102 nat44_ei_main_t *nm = &nat44_ei_main;
103 vlib_worker_thread_t *w =
104 vlib_worker_threads + worker_index + nm->first_worker_index;
106 rmp = vl_msg_api_alloc (sizeof (*rmp));
107 clib_memset (rmp, 0, sizeof (*rmp));
108 rmp->_vl_msg_id = ntohs (VL_API_NAT44_EI_WORKER_DETAILS + nm->msg_id_base);
109 rmp->context = context;
110 rmp->worker_index = htonl (worker_index);
111 rmp->lcore_id = htonl (w->cpu_id);
112 strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
114 vl_api_send_msg (reg, (u8 *) rmp);
118 vl_api_nat44_ei_worker_dump_t_handler (vl_api_nat44_ei_worker_dump_t *mp)
120 vl_api_registration_t *reg;
121 nat44_ei_main_t *nm = &nat44_ei_main;
124 reg = vl_api_client_index_to_registration (mp->client_index);
128 vec_foreach (worker_index, nm->workers)
130 send_nat_worker_details (*worker_index, reg, mp->context);
135 vl_api_nat44_ei_set_log_level_t_handler (vl_api_nat44_ei_set_log_level_t *mp)
137 nat44_ei_main_t *nm = &nat44_ei_main;
138 vl_api_nat44_ei_set_log_level_reply_t *rmp;
141 if (nm->log_level > NAT_LOG_DEBUG)
142 rv = VNET_API_ERROR_UNSUPPORTED;
144 nm->log_level = mp->log_level;
146 REPLY_MACRO (VL_API_NAT44_EI_SET_LOG_LEVEL_REPLY);
150 vl_api_nat44_ei_plugin_enable_disable_t_handler (
151 vl_api_nat44_ei_plugin_enable_disable_t *mp)
153 nat44_ei_main_t *nm = &nat44_ei_main;
154 nat44_ei_config_t c = { 0 };
155 vl_api_nat44_ei_plugin_enable_disable_reply_t *rmp;
160 c.static_mapping_only = mp->flags & NAT44_EI_STATIC_MAPPING_ONLY;
161 c.connection_tracking = mp->flags & NAT44_EI_CONNECTION_TRACKING;
162 c.out2in_dpo = mp->flags & NAT44_EI_OUT2IN_DPO;
164 c.inside_vrf = ntohl (mp->inside_vrf);
165 c.outside_vrf = ntohl (mp->outside_vrf);
167 c.users = ntohl (mp->users);
169 c.sessions = ntohl (mp->sessions);
171 c.user_sessions = ntohl (mp->user_sessions);
173 rv = nat44_ei_plugin_enable (c);
176 rv = nat44_ei_plugin_disable ();
178 REPLY_MACRO (VL_API_NAT44_EI_PLUGIN_ENABLE_DISABLE_REPLY);
182 vl_api_nat44_ei_ipfix_enable_disable_t_handler (
183 vl_api_nat44_ei_ipfix_enable_disable_t *mp)
185 nat44_ei_main_t *nm = &nat44_ei_main;
186 vl_api_nat44_ei_ipfix_enable_disable_reply_t *rmp;
189 rv = nat_ipfix_logging_enable_disable (mp->enable,
190 clib_host_to_net_u32 (mp->domain_id),
191 clib_host_to_net_u16 (mp->src_port));
193 REPLY_MACRO (VL_API_NAT44_EI_IPFIX_ENABLE_DISABLE_REPLY);
197 vl_api_nat44_ei_set_timeouts_t_handler (vl_api_nat44_ei_set_timeouts_t *mp)
199 nat44_ei_main_t *nm = &nat44_ei_main;
200 vl_api_nat44_ei_set_timeouts_reply_t *rmp;
203 nm->timeouts.udp = ntohl (mp->udp);
204 nm->timeouts.tcp.established = ntohl (mp->tcp_established);
205 nm->timeouts.tcp.transitory = ntohl (mp->tcp_transitory);
206 nm->timeouts.icmp = ntohl (mp->icmp);
208 REPLY_MACRO (VL_API_NAT44_EI_SET_TIMEOUTS_REPLY);
212 vl_api_nat44_ei_set_addr_and_port_alloc_alg_t_handler (
213 vl_api_nat44_ei_set_addr_and_port_alloc_alg_t *mp)
215 nat44_ei_main_t *nm = &nat44_ei_main;
216 vl_api_nat44_ei_set_addr_and_port_alloc_alg_reply_t *rmp;
218 u16 port_start, port_end;
222 case NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
223 nat44_ei_set_alloc_default ();
225 case NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE:
226 nat44_ei_set_alloc_mape (ntohs (mp->psid), mp->psid_offset,
229 case NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE:
230 port_start = ntohs (mp->start_port);
231 port_end = ntohs (mp->end_port);
232 if (port_end <= port_start)
234 rv = VNET_API_ERROR_INVALID_VALUE;
237 nat44_ei_set_alloc_range (port_start, port_end);
240 rv = VNET_API_ERROR_INVALID_VALUE;
245 REPLY_MACRO (VL_API_NAT44_EI_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
249 vl_api_nat44_ei_get_addr_and_port_alloc_alg_t_handler (
250 vl_api_nat44_ei_get_addr_and_port_alloc_alg_t *mp)
252 nat44_ei_main_t *nm = &nat44_ei_main;
253 vl_api_nat44_ei_get_addr_and_port_alloc_alg_reply_t *rmp;
256 REPLY_MACRO2 (VL_API_NAT44_EI_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY, ({
257 rmp->alg = nm->addr_and_port_alloc_alg;
258 rmp->psid_offset = nm->psid_offset;
259 rmp->psid_length = nm->psid_length;
260 rmp->psid = htons (nm->psid);
261 rmp->start_port = htons (nm->start_port);
262 rmp->end_port = htons (nm->end_port);
267 vl_api_nat44_ei_set_mss_clamping_t_handler (
268 vl_api_nat44_ei_set_mss_clamping_t *mp)
270 nat44_ei_main_t *nm = &nat44_ei_main;
271 vl_api_nat44_ei_set_mss_clamping_reply_t *rmp;
275 nm->mss_clamping = ntohs (mp->mss_value);
277 nm->mss_clamping = 0;
279 REPLY_MACRO (VL_API_NAT44_EI_SET_MSS_CLAMPING_REPLY);
283 vl_api_nat44_ei_get_mss_clamping_t_handler (
284 vl_api_nat44_ei_get_mss_clamping_t *mp)
286 nat44_ei_main_t *nm = &nat44_ei_main;
287 vl_api_nat44_ei_get_mss_clamping_reply_t *rmp;
290 REPLY_MACRO2 (VL_API_NAT44_EI_GET_MSS_CLAMPING_REPLY, ({
291 rmp->enable = nm->mss_clamping ? 1 : 0;
292 rmp->mss_value = htons (nm->mss_clamping);
297 vl_api_nat44_ei_ha_set_listener_t_handler (
298 vl_api_nat44_ei_ha_set_listener_t *mp)
300 nat44_ei_main_t *nm = &nat44_ei_main;
301 vl_api_nat44_ei_ha_set_listener_reply_t *rmp;
305 memcpy (&addr, &mp->ip_address, sizeof (addr));
306 rv = nat_ha_set_listener (vlib_get_main (), &addr,
307 clib_net_to_host_u16 (mp->port),
308 clib_net_to_host_u32 (mp->path_mtu));
310 REPLY_MACRO (VL_API_NAT44_EI_HA_SET_LISTENER_REPLY);
314 vl_api_nat44_ei_ha_get_listener_t_handler (
315 vl_api_nat44_ei_ha_get_listener_t *mp)
317 nat44_ei_main_t *nm = &nat44_ei_main;
318 vl_api_nat44_ei_ha_get_listener_reply_t *rmp;
324 nat_ha_get_listener (&addr, &port, &path_mtu);
326 REPLY_MACRO2 (VL_API_NAT44_EI_HA_GET_LISTENER_REPLY, ({
327 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
328 rmp->port = clib_host_to_net_u16 (port);
329 rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
334 vl_api_nat44_ei_ha_set_failover_t_handler (
335 vl_api_nat44_ei_ha_set_failover_t *mp)
337 nat44_ei_main_t *nm = &nat44_ei_main;
338 vl_api_nat44_ei_ha_set_failover_reply_t *rmp;
342 memcpy (&addr, &mp->ip_address, sizeof (addr));
343 rv = nat_ha_set_failover (
344 vlib_get_main (), &addr, clib_net_to_host_u16 (mp->port),
345 clib_net_to_host_u32 (mp->session_refresh_interval));
347 REPLY_MACRO (VL_API_NAT44_EI_HA_SET_FAILOVER_REPLY);
351 vl_api_nat44_ei_ha_get_failover_t_handler (
352 vl_api_nat44_ei_ha_get_failover_t *mp)
354 nat44_ei_main_t *nm = &nat44_ei_main;
355 vl_api_nat44_ei_ha_get_failover_reply_t *rmp;
359 u32 session_refresh_interval;
361 nat_ha_get_failover (&addr, &port, &session_refresh_interval);
363 REPLY_MACRO2 (VL_API_NAT44_EI_HA_GET_FAILOVER_REPLY, ({
364 clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
365 rmp->port = clib_host_to_net_u16 (port);
366 rmp->session_refresh_interval =
367 clib_host_to_net_u32 (session_refresh_interval);
372 vl_api_nat44_ei_ha_flush_t_handler (vl_api_nat44_ei_ha_flush_t *mp)
374 nat44_ei_main_t *nm = &nat44_ei_main;
375 vl_api_nat44_ei_ha_flush_reply_t *rmp;
380 REPLY_MACRO (VL_API_NAT44_EI_HA_FLUSH_REPLY);
384 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
386 nat44_ei_main_t *nm = &nat44_ei_main;
387 vl_api_registration_t *reg;
388 vl_api_nat44_ei_ha_resync_completed_event_t *mp;
390 reg = vl_api_client_index_to_registration (client_index);
394 mp = vl_msg_api_alloc (sizeof (*mp));
395 clib_memset (mp, 0, sizeof (*mp));
396 mp->client_index = client_index;
398 mp->missed_count = clib_host_to_net_u32 (missed_count);
400 ntohs (VL_API_NAT44_EI_HA_RESYNC_COMPLETED_EVENT + nm->msg_id_base);
402 vl_api_send_msg (reg, (u8 *) mp);
406 vl_api_nat44_ei_ha_resync_t_handler (vl_api_nat44_ei_ha_resync_t *mp)
408 nat44_ei_main_t *nm = &nat44_ei_main;
409 vl_api_nat44_ei_ha_resync_reply_t *rmp;
413 mp->client_index, mp->pid,
414 mp->want_resync_event ? nat_ha_resync_completed_event_cb : NULL);
416 REPLY_MACRO (VL_API_NAT44_EI_HA_RESYNC_REPLY);
420 vl_api_nat44_ei_del_user_t_handler (vl_api_nat44_ei_del_user_t *mp)
422 nat44_ei_main_t *nm = &nat44_ei_main;
423 vl_api_nat44_ei_del_user_reply_t *rmp;
426 memcpy (&addr.as_u8, mp->ip_address, 4);
427 rv = nat44_ei_user_del (&addr, ntohl (mp->fib_index));
428 REPLY_MACRO (VL_API_NAT44_EI_DEL_USER_REPLY);
432 vl_api_nat44_ei_add_del_address_range_t_handler (
433 vl_api_nat44_ei_add_del_address_range_t *mp)
435 nat44_ei_main_t *nm = &nat44_ei_main;
436 vl_api_nat44_ei_add_del_address_range_reply_t *rmp;
437 ip4_address_t this_addr;
439 u32 start_host_order, end_host_order;
445 if (nm->static_mapping_only)
447 rv = VNET_API_ERROR_FEATURE_DISABLED;
453 tmp = (u32 *) mp->first_ip_address;
454 start_host_order = clib_host_to_net_u32 (tmp[0]);
455 tmp = (u32 *) mp->last_ip_address;
456 end_host_order = clib_host_to_net_u32 (tmp[0]);
458 count = (end_host_order - start_host_order) + 1;
460 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
463 nat44_ei_log_info ("%U - %U, %d addresses...", format_ip4_address,
464 mp->first_ip_address, format_ip4_address,
465 mp->last_ip_address, count);
467 memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
469 for (i = 0; i < count; i++)
472 rv = nat44_ei_add_address (&this_addr, vrf_id);
474 rv = nat44_ei_del_address (this_addr, 0);
480 nat44_ei_add_del_address_dpo (this_addr, is_add);
482 increment_v4_address (&this_addr);
486 REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_ADDRESS_RANGE_REPLY);
490 send_nat44_ei_address_details (nat44_ei_address_t *a,
491 vl_api_registration_t *reg, u32 context)
493 vl_api_nat44_ei_address_details_t *rmp;
494 nat44_ei_main_t *nm = &nat44_ei_main;
496 rmp = vl_msg_api_alloc (sizeof (*rmp));
497 clib_memset (rmp, 0, sizeof (*rmp));
498 rmp->_vl_msg_id = ntohs (VL_API_NAT44_EI_ADDRESS_DETAILS + nm->msg_id_base);
499 clib_memcpy (rmp->ip_address, &(a->addr), 4);
500 if (a->fib_index != ~0)
502 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
503 rmp->vrf_id = ntohl (fib->ft_table_id);
507 rmp->context = context;
509 vl_api_send_msg (reg, (u8 *) rmp);
513 vl_api_nat44_ei_address_dump_t_handler (vl_api_nat44_ei_address_dump_t *mp)
515 vl_api_registration_t *reg;
516 nat44_ei_main_t *nm = &nat44_ei_main;
517 nat44_ei_address_t *a;
519 reg = vl_api_client_index_to_registration (mp->client_index);
523 vec_foreach (a, nm->addresses)
525 send_nat44_ei_address_details (a, reg, mp->context);
530 vl_api_nat44_ei_interface_add_del_feature_t_handler (
531 vl_api_nat44_ei_interface_add_del_feature_t *mp)
533 nat44_ei_main_t *nm = &nat44_ei_main;
534 vl_api_nat44_ei_interface_add_del_feature_reply_t *rmp;
535 u32 sw_if_index = ntohl (mp->sw_if_index);
538 VALIDATE_SW_IF_INDEX (mp);
543 nat44_ei_add_interface (sw_if_index, mp->flags & NAT44_EI_IF_INSIDE);
548 nat44_ei_del_interface (sw_if_index, mp->flags & NAT44_EI_IF_INSIDE);
551 BAD_SW_IF_INDEX_LABEL;
552 REPLY_MACRO (VL_API_NAT44_EI_INTERFACE_ADD_DEL_FEATURE_REPLY);
556 send_nat44_ei_interface_details (nat44_ei_interface_t *i,
557 vl_api_registration_t *reg, u32 context)
559 vl_api_nat44_ei_interface_details_t *rmp;
560 nat44_ei_main_t *nm = &nat44_ei_main;
562 rmp = vl_msg_api_alloc (sizeof (*rmp));
563 clib_memset (rmp, 0, sizeof (*rmp));
565 ntohs (VL_API_NAT44_EI_INTERFACE_DETAILS + nm->msg_id_base);
566 rmp->sw_if_index = ntohl (i->sw_if_index);
568 if (nat44_ei_interface_is_inside (i))
569 rmp->flags |= NAT44_EI_IF_INSIDE;
570 if (nat44_ei_interface_is_outside (i))
571 rmp->flags |= NAT44_EI_IF_OUTSIDE;
573 rmp->context = context;
575 vl_api_send_msg (reg, (u8 *) rmp);
579 vl_api_nat44_ei_interface_dump_t_handler (vl_api_nat44_ei_interface_dump_t *mp)
581 vl_api_registration_t *reg;
582 nat44_ei_main_t *nm = &nat44_ei_main;
583 nat44_ei_interface_t *i;
585 reg = vl_api_client_index_to_registration (mp->client_index);
589 pool_foreach (i, nm->interfaces)
591 send_nat44_ei_interface_details (i, reg, mp->context);
595 static_always_inline int
596 add_del_dummy_output_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
598 nat44_ei_main_t *nm = &nat44_ei_main;
599 nat44_ei_interface_t *i;
602 pool_foreach (i, nm->output_feature_dummy_interfaces)
604 if (i->sw_if_index == sw_if_index)
608 pool_put (nm->output_feature_dummy_interfaces, i);
617 pool_get (nm->output_feature_dummy_interfaces, i);
618 i->sw_if_index = sw_if_index;
622 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
626 i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
637 vl_api_nat44_ei_interface_add_del_output_feature_t_handler (
638 vl_api_nat44_ei_interface_add_del_output_feature_t *mp)
640 vl_api_nat44_ei_interface_add_del_output_feature_reply_t *rmp;
641 nat44_ei_main_t *nm = &nat44_ei_main;
645 VALIDATE_SW_IF_INDEX (mp);
647 sw_if_index = ntohl (mp->sw_if_index);
649 // register all interfaces in the dummy structure
650 rv = add_del_dummy_output_interface (
651 sw_if_index, mp->flags & NAT44_EI_IF_INSIDE, mp->is_add);
653 if (!(mp->flags & NAT44_EI_IF_INSIDE))
657 rv = nat44_ei_add_output_interface (sw_if_index);
661 rv = nat44_ei_del_output_interface (sw_if_index);
665 BAD_SW_IF_INDEX_LABEL;
666 REPLY_MACRO (VL_API_NAT44_EI_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
670 send_nat44_ei_interface_output_feature_details (nat44_ei_interface_t *i,
671 vl_api_registration_t *reg,
674 vl_api_nat44_ei_interface_output_feature_details_t *rmp;
675 nat44_ei_main_t *nm = &nat44_ei_main;
677 rmp = vl_msg_api_alloc (sizeof (*rmp));
678 clib_memset (rmp, 0, sizeof (*rmp));
680 ntohs (VL_API_NAT44_EI_INTERFACE_OUTPUT_FEATURE_DETAILS + nm->msg_id_base);
681 rmp->sw_if_index = ntohl (i->sw_if_index);
682 rmp->context = context;
684 if (nat44_ei_interface_is_inside (i))
686 rmp->flags |= NAT44_EI_IF_INSIDE;
689 vl_api_send_msg (reg, (u8 *) rmp);
693 vl_api_nat44_ei_interface_output_feature_dump_t_handler (
694 vl_api_nat44_ei_interface_output_feature_dump_t *mp)
696 vl_api_registration_t *reg;
697 nat44_ei_main_t *nm = &nat44_ei_main;
698 nat44_ei_interface_t *i;
700 reg = vl_api_client_index_to_registration (mp->client_index);
704 pool_foreach (i, nm->output_feature_dummy_interfaces)
706 send_nat44_ei_interface_output_feature_details (i, reg, mp->context);
711 vl_api_nat44_ei_add_del_output_interface_t_handler (
712 vl_api_nat44_ei_add_del_output_interface_t *mp)
714 vl_api_nat44_ei_add_del_output_interface_reply_t *rmp;
715 nat44_ei_main_t *nm = &nat44_ei_main;
718 if (!vnet_sw_if_index_is_api_valid (mp->sw_if_index))
720 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
721 goto bad_sw_if_index;
726 rv = nat44_ei_add_output_interface (mp->sw_if_index);
730 rv = nat44_ei_del_output_interface (mp->sw_if_index);
734 REPLY_MACRO_END (VL_API_NAT44_EI_ADD_DEL_OUTPUT_INTERFACE_REPLY);
738 #include <nat/nat44-ei/nat44_ei.api.h>
741 send_nat44_ei_output_interface_details (u32 index, vl_api_registration_t *rp,
744 nat44_ei_main_t *nm = &nat44_ei_main;
745 vl_api_nat44_ei_output_interface_details_t *rmp;
746 nat44_ei_interface_t *i =
747 pool_elt_at_index (nm->output_feature_interfaces, index);
749 /* Make sure every field is initiated (or don't skip the clib_memset()) */
750 REPLY_MACRO_DETAILS4 (
751 VL_API_NAT44_EI_OUTPUT_INTERFACE_DETAILS, rp, context, ({
752 rmp->sw_if_index = i->sw_if_index;
754 /* Endian hack until apigen registers _details
755 * endian functions */
756 vl_api_nat44_ei_output_interface_details_t_endian (rmp);
757 rmp->_vl_msg_id = htons (rmp->_vl_msg_id);
758 rmp->context = htonl (rmp->context);
763 vl_api_nat44_ei_output_interface_get_t_handler (
764 vl_api_nat44_ei_output_interface_get_t *mp)
766 vl_api_nat44_ei_output_interface_get_reply_t *rmp;
767 nat44_ei_main_t *nm = &nat44_ei_main;
770 if (pool_elts (nm->output_feature_interfaces) == 0)
772 REPLY_MACRO (VL_API_NAT44_EI_OUTPUT_INTERFACE_GET_REPLY);
776 REPLY_AND_DETAILS_MACRO (
777 VL_API_NAT44_EI_OUTPUT_INTERFACE_GET_REPLY, nm->output_feature_interfaces,
778 ({ send_nat44_ei_output_interface_details (cursor, rp, mp->context); }));
782 vl_api_nat44_ei_add_del_static_mapping_t_handler (
783 vl_api_nat44_ei_add_del_static_mapping_t *mp)
785 vl_api_nat44_ei_add_del_static_mapping_reply_t *rmp;
787 nat44_ei_main_t *nm = &nat44_ei_main;
790 ip4_address_t l_addr, e_addr, pool_addr = { 0 };
791 u32 sw_if_index, flags = 0, vrf_id;
792 u16 l_port = 0, e_port = 0;
793 nat_protocol_t proto = 0;
796 memcpy (&l_addr.as_u8, mp->local_ip_address, 4);
798 if (mp->flags & NAT44_EI_ADDR_ONLY_MAPPING)
800 flags |= NAT44_EI_SM_FLAG_ADDR_ONLY;
804 l_port = mp->local_port;
805 e_port = mp->external_port;
806 proto = ip_proto_to_nat_proto (mp->protocol);
809 sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
810 if (sw_if_index != ~0)
812 flags |= NAT44_EI_SM_FLAG_SWITCH_ADDRESS;
816 memcpy (&e_addr.as_u8, mp->external_ip_address, 4);
819 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
823 mp->tag[sizeof (mp->tag) - 1] = 0;
824 tag = format (0, "%s", mp->tag);
825 vec_terminate_c_string (tag);
827 rv = nat44_ei_add_static_mapping (l_addr, e_addr, l_port, e_port, proto,
828 vrf_id, sw_if_index, flags, pool_addr,
834 rv = nat44_ei_del_static_mapping (l_addr, e_addr, l_port, e_port, proto,
835 vrf_id, sw_if_index, flags);
837 REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_STATIC_MAPPING_REPLY);
841 send_nat44_ei_static_mapping_details (nat44_ei_static_mapping_t *m,
842 vl_api_registration_t *reg, u32 context)
844 vl_api_nat44_ei_static_mapping_details_t *rmp;
845 nat44_ei_main_t *nm = &nat44_ei_main;
846 u32 len = sizeof (*rmp);
848 rmp = vl_msg_api_alloc (len);
849 clib_memset (rmp, 0, len);
851 ntohs (VL_API_NAT44_EI_STATIC_MAPPING_DETAILS + nm->msg_id_base);
853 clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
854 clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
855 rmp->external_sw_if_index = ~0;
856 rmp->vrf_id = htonl (m->vrf_id);
857 rmp->context = context;
859 if (is_sm_addr_only (m->flags))
861 rmp->flags |= NAT44_EI_ADDR_ONLY_MAPPING;
865 rmp->protocol = nat_proto_to_ip_proto (m->proto);
866 rmp->external_port = m->external_port;
867 rmp->local_port = m->local_port;
871 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
873 vl_api_send_msg (reg, (u8 *) rmp);
877 send_nat44_ei_static_map_resolve_details (nat44_ei_static_map_resolve_t *m,
878 vl_api_registration_t *reg,
881 vl_api_nat44_ei_static_mapping_details_t *rmp;
882 nat44_ei_main_t *nm = &nat44_ei_main;
884 rmp = vl_msg_api_alloc (sizeof (*rmp));
885 clib_memset (rmp, 0, sizeof (*rmp));
887 ntohs (VL_API_NAT44_EI_STATIC_MAPPING_DETAILS + nm->msg_id_base);
888 clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
889 rmp->external_sw_if_index = htonl (m->sw_if_index);
890 rmp->vrf_id = htonl (m->vrf_id);
891 rmp->context = context;
893 if (is_sm_addr_only (m->flags))
895 rmp->flags |= NAT44_EI_ADDR_ONLY_MAPPING;
899 rmp->protocol = nat_proto_to_ip_proto (m->proto);
900 rmp->external_port = m->e_port;
901 rmp->local_port = m->l_port;
904 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
906 vl_api_send_msg (reg, (u8 *) rmp);
910 vl_api_nat44_ei_static_mapping_dump_t_handler (
911 vl_api_nat44_ei_static_mapping_dump_t *mp)
913 vl_api_registration_t *reg;
914 nat44_ei_main_t *nm = &nat44_ei_main;
915 nat44_ei_static_mapping_t *m;
916 nat44_ei_static_map_resolve_t *rp;
919 reg = vl_api_client_index_to_registration (mp->client_index);
923 pool_foreach (m, nm->static_mappings)
925 if (!is_sm_identity_nat (m->flags))
926 send_nat44_ei_static_mapping_details (m, reg, mp->context);
929 for (j = 0; j < vec_len (nm->to_resolve); j++)
931 rp = nm->to_resolve + j;
932 if (!is_sm_identity_nat (rp->flags))
933 send_nat44_ei_static_map_resolve_details (rp, reg, mp->context);
938 vl_api_nat44_ei_add_del_identity_mapping_t_handler (
939 vl_api_nat44_ei_add_del_identity_mapping_t *mp)
941 vl_api_nat44_ei_add_del_identity_mapping_reply_t *rmp;
943 nat44_ei_main_t *nm = &nat44_ei_main;
946 ip4_address_t addr, pool_addr = { 0 };
947 u32 sw_if_index, flags, vrf_id;
948 nat_protocol_t proto = 0;
952 flags = NAT44_EI_SM_FLAG_IDENTITY_NAT;
954 if (mp->flags & NAT44_EI_ADDR_ONLY_MAPPING)
956 flags |= NAT44_EI_SM_FLAG_ADDR_ONLY;
961 proto = ip_proto_to_nat_proto (mp->protocol);
964 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
965 if (sw_if_index != ~0)
967 flags |= NAT44_EI_SM_FLAG_SWITCH_ADDRESS;
971 memcpy (&addr.as_u8, mp->ip_address, 4);
974 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
978 mp->tag[sizeof (mp->tag) - 1] = 0;
979 tag = format (0, "%s", mp->tag);
980 vec_terminate_c_string (tag);
982 rv = nat44_ei_add_static_mapping (addr, addr, port, port, proto, vrf_id,
983 sw_if_index, flags, pool_addr, tag);
988 rv = nat44_ei_del_static_mapping (addr, addr, port, port, proto, vrf_id,
992 REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_IDENTITY_MAPPING_REPLY);
996 send_nat44_ei_identity_mapping_details (nat44_ei_static_mapping_t *m,
997 int index, vl_api_registration_t *reg,
1000 vl_api_nat44_ei_identity_mapping_details_t *rmp;
1001 nat44_ei_main_t *nm = &nat44_ei_main;
1002 nat44_ei_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
1004 rmp = vl_msg_api_alloc (sizeof (*rmp));
1005 clib_memset (rmp, 0, sizeof (*rmp));
1007 ntohs (VL_API_NAT44_EI_IDENTITY_MAPPING_DETAILS + nm->msg_id_base);
1009 if (is_sm_addr_only (m->flags))
1010 rmp->flags |= NAT44_EI_ADDR_ONLY_MAPPING;
1012 clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1013 rmp->port = m->local_port;
1014 rmp->sw_if_index = ~0;
1015 rmp->vrf_id = htonl (local->vrf_id);
1016 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1017 rmp->context = context;
1019 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1021 vl_api_send_msg (reg, (u8 *) rmp);
1025 send_nat44_ei_identity_map_resolve_details (nat44_ei_static_map_resolve_t *m,
1026 vl_api_registration_t *reg,
1029 vl_api_nat44_ei_identity_mapping_details_t *rmp;
1030 nat44_ei_main_t *nm = &nat44_ei_main;
1031 rmp = vl_msg_api_alloc (sizeof (*rmp));
1032 clib_memset (rmp, 0, sizeof (*rmp));
1034 ntohs (VL_API_NAT44_EI_IDENTITY_MAPPING_DETAILS + nm->msg_id_base);
1036 if (is_sm_addr_only (m->flags))
1037 rmp->flags = (vl_api_nat44_ei_config_flags_t) NAT44_EI_ADDR_ONLY_MAPPING;
1039 rmp->port = m->l_port;
1040 rmp->sw_if_index = htonl (m->sw_if_index);
1041 rmp->vrf_id = htonl (m->vrf_id);
1042 rmp->protocol = nat_proto_to_ip_proto (m->proto);
1043 rmp->context = context;
1045 strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1047 vl_api_send_msg (reg, (u8 *) rmp);
1051 vl_api_nat44_ei_identity_mapping_dump_t_handler (
1052 vl_api_nat44_ei_identity_mapping_dump_t *mp)
1054 vl_api_registration_t *reg;
1055 nat44_ei_main_t *nm = &nat44_ei_main;
1056 nat44_ei_static_mapping_t *m;
1057 nat44_ei_static_map_resolve_t *rp;
1060 reg = vl_api_client_index_to_registration (mp->client_index);
1064 pool_foreach (m, nm->static_mappings)
1066 if (is_sm_identity_nat (m->flags))
1068 pool_foreach_index (j, m->locals)
1070 send_nat44_ei_identity_mapping_details (m, j, reg, mp->context);
1075 for (j = 0; j < vec_len (nm->to_resolve); j++)
1077 rp = nm->to_resolve + j;
1078 if (is_sm_identity_nat (rp->flags))
1079 send_nat44_ei_identity_map_resolve_details (rp, reg, mp->context);
1084 vl_api_nat44_ei_add_del_interface_addr_t_handler (
1085 vl_api_nat44_ei_add_del_interface_addr_t *mp)
1087 nat44_ei_main_t *nm = &nat44_ei_main;
1088 vl_api_nat44_ei_add_del_interface_addr_reply_t *rmp;
1089 u32 sw_if_index = ntohl (mp->sw_if_index);
1092 VALIDATE_SW_IF_INDEX (mp);
1096 rv = nat44_ei_add_interface_address (sw_if_index);
1100 rv = nat44_ei_del_interface_address (sw_if_index);
1103 BAD_SW_IF_INDEX_LABEL;
1104 REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_INTERFACE_ADDR_REPLY);
1108 send_nat44_ei_interface_addr_details (u32 sw_if_index,
1109 vl_api_registration_t *reg, u32 context)
1111 vl_api_nat44_ei_interface_addr_details_t *rmp;
1112 nat44_ei_main_t *nm = &nat44_ei_main;
1114 rmp = vl_msg_api_alloc (sizeof (*rmp));
1115 clib_memset (rmp, 0, sizeof (*rmp));
1117 ntohs (VL_API_NAT44_EI_INTERFACE_ADDR_DETAILS + nm->msg_id_base);
1118 rmp->sw_if_index = ntohl (sw_if_index);
1119 rmp->context = context;
1121 vl_api_send_msg (reg, (u8 *) rmp);
1125 vl_api_nat44_ei_interface_addr_dump_t_handler (
1126 vl_api_nat44_ei_interface_addr_dump_t *mp)
1128 vl_api_registration_t *reg;
1129 nat44_ei_main_t *nm = &nat44_ei_main;
1132 reg = vl_api_client_index_to_registration (mp->client_index);
1136 vec_foreach (i, nm->auto_add_sw_if_indices)
1137 send_nat44_ei_interface_addr_details (*i, reg, mp->context);
1141 send_nat44_ei_user_details (nat44_ei_user_t *u, vl_api_registration_t *reg,
1144 vl_api_nat44_ei_user_details_t *rmp;
1145 nat44_ei_main_t *nm = &nat44_ei_main;
1146 ip4_main_t *im = &ip4_main;
1148 rmp = vl_msg_api_alloc (sizeof (*rmp));
1149 clib_memset (rmp, 0, sizeof (*rmp));
1150 rmp->_vl_msg_id = ntohs (VL_API_NAT44_EI_USER_DETAILS + nm->msg_id_base);
1152 if (!pool_is_free_index (im->fibs, u->fib_index))
1154 fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1155 rmp->vrf_id = ntohl (fib->ft_table_id);
1158 clib_memcpy (rmp->ip_address, &(u->addr), 4);
1159 rmp->nsessions = ntohl (u->nsessions);
1160 rmp->nstaticsessions = ntohl (u->nstaticsessions);
1161 rmp->context = context;
1163 vl_api_send_msg (reg, (u8 *) rmp);
1167 vl_api_nat44_ei_user_dump_t_handler (vl_api_nat44_ei_user_dump_t *mp)
1169 vl_api_registration_t *reg;
1170 nat44_ei_main_t *nm = &nat44_ei_main;
1171 nat44_ei_main_per_thread_data_t *tnm;
1174 reg = vl_api_client_index_to_registration (mp->client_index);
1178 vec_foreach (tnm, nm->per_thread_data)
1180 pool_foreach (u, tnm->users)
1182 send_nat44_ei_user_details (u, reg, mp->context);
1188 send_nat44_ei_user_session_details (nat44_ei_session_t *s,
1189 vl_api_registration_t *reg, u32 context)
1191 vl_api_nat44_ei_user_session_details_t *rmp;
1192 nat44_ei_main_t *nm = &nat44_ei_main;
1194 rmp = vl_msg_api_alloc (sizeof (*rmp));
1195 clib_memset (rmp, 0, sizeof (*rmp));
1197 ntohs (VL_API_NAT44_EI_USER_SESSION_DETAILS + nm->msg_id_base);
1198 clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1199 clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1201 if (nat44_ei_is_session_static (s))
1202 rmp->flags |= NAT44_EI_STATIC_MAPPING;
1204 rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1205 rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1206 rmp->total_pkts = ntohl (s->total_pkts);
1207 rmp->context = context;
1208 if (nat44_ei_is_unk_proto_session (s))
1210 rmp->outside_port = 0;
1211 rmp->inside_port = 0;
1212 rmp->protocol = ntohs (s->in2out.port);
1216 rmp->outside_port = s->out2in.port;
1217 rmp->inside_port = s->in2out.port;
1218 rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1220 vl_api_send_msg (reg, (u8 *) rmp);
1224 vl_api_nat44_ei_user_session_dump_t_handler (
1225 vl_api_nat44_ei_user_session_dump_t *mp)
1227 vl_api_registration_t *reg;
1228 nat44_ei_main_t *nm = &nat44_ei_main;
1229 nat44_ei_main_per_thread_data_t *tnm;
1230 nat44_ei_session_t *s;
1231 clib_bihash_kv_8_8_t key, value;
1232 nat44_ei_user_key_t ukey;
1234 u32 session_index, head_index, elt_index;
1235 dlist_elt_t *head, *elt;
1238 reg = vl_api_client_index_to_registration (mp->client_index);
1242 clib_memcpy (&ukey.addr, mp->ip_address, 4);
1243 ip.src_address.as_u32 = ukey.addr.as_u32;
1244 ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1245 key.key = ukey.as_u64;
1246 if (nm->num_workers > 1)
1247 tnm = vec_elt_at_index (
1248 nm->per_thread_data,
1249 nat44_ei_get_in2out_worker_index (&ip, ukey.fib_index, 0));
1251 tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1253 if (clib_bihash_search_8_8 (&tnm->user_hash, &key, &value))
1255 u = pool_elt_at_index (tnm->users, value.value);
1256 if (!u->nsessions && !u->nstaticsessions)
1259 head_index = u->sessions_per_user_list_head_index;
1260 head = pool_elt_at_index (tnm->list_pool, head_index);
1261 elt_index = head->next;
1262 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1263 session_index = elt->value;
1264 while (session_index != ~0)
1266 s = pool_elt_at_index (tnm->sessions, session_index);
1268 send_nat44_ei_user_session_details (s, reg, mp->context);
1270 elt_index = elt->next;
1271 elt = pool_elt_at_index (tnm->list_pool, elt_index);
1272 session_index = elt->value;
1277 vl_api_nat44_ei_del_session_t_handler (vl_api_nat44_ei_del_session_t *mp)
1279 nat44_ei_main_t *nm = &nat44_ei_main;
1280 vl_api_nat44_ei_del_session_reply_t *rmp;
1281 ip4_address_t addr, eh_addr;
1286 nat_protocol_t proto;
1288 memcpy (&addr.as_u8, mp->address, 4);
1290 vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1291 proto = ip_proto_to_nat_proto (mp->protocol);
1292 memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1294 // is session inside ?
1295 is_in = mp->flags & NAT44_EI_IF_INSIDE;
1297 rv = nat44_ei_del_session (nm, &addr, port, proto, vrf_id, is_in);
1299 REPLY_MACRO (VL_API_NAT44_EI_DEL_SESSION_REPLY);
1303 vl_api_nat44_ei_forwarding_enable_disable_t_handler (
1304 vl_api_nat44_ei_forwarding_enable_disable_t *mp)
1306 nat44_ei_main_t *nm = &nat44_ei_main;
1307 vl_api_nat44_ei_forwarding_enable_disable_reply_t *rmp;
1309 u32 *ses_to_be_removed = 0, *ses_index;
1310 nat44_ei_main_per_thread_data_t *tnm;
1311 nat44_ei_session_t *s;
1313 nm->forwarding_enabled = mp->enable != 0;
1315 if (mp->enable == 0)
1317 vec_foreach (tnm, nm->per_thread_data)
1319 vec_foreach (ses_index, ses_to_be_removed)
1321 s = pool_elt_at_index (tnm->sessions, ses_index[0]);
1322 nat44_ei_free_session_data (nm, s, tnm - nm->per_thread_data, 0);
1323 nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1326 vec_free (ses_to_be_removed);
1330 REPLY_MACRO (VL_API_NAT44_EI_FORWARDING_ENABLE_DISABLE_REPLY);
1334 vl_api_nat44_ei_set_fq_options_t_handler (vl_api_nat44_ei_set_fq_options_t *mp)
1336 nat44_ei_main_t *nm = &nat44_ei_main;
1337 vl_api_nat44_ei_set_fq_options_reply_t *rmp;
1339 u32 frame_queue_nelts = ntohl (mp->frame_queue_nelts);
1340 rv = nat44_ei_set_frame_queue_nelts (frame_queue_nelts);
1341 REPLY_MACRO (VL_API_NAT44_EI_SET_FQ_OPTIONS_REPLY);
1345 vl_api_nat44_ei_show_fq_options_t_handler (
1346 vl_api_nat44_ei_show_fq_options_t *mp)
1348 nat44_ei_main_t *nm = &nat44_ei_main;
1349 vl_api_nat44_ei_show_fq_options_reply_t *rmp;
1351 /* clang-format off */
1352 REPLY_MACRO2_ZERO (VL_API_NAT44_EI_SHOW_FQ_OPTIONS_REPLY,
1354 rmp->frame_queue_nelts = htonl (nm->frame_queue_nelts);
1356 /* clang-format on */
1359 /* API definitions */
1360 #include <vnet/format_fns.h>
1361 #include <nat/nat44-ei/nat44_ei.api.c>
1363 /* Set up the API message handling tables */
1365 nat44_ei_api_hookup (vlib_main_t *vm)
1367 nat44_ei_main_t *nm = &nat44_ei_main;
1368 nm->msg_id_base = setup_message_id_table ();
1373 * fd.io coding-style-patch-verification: ON
1376 * eval: (c-set-style "gnu")