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 <vlibmemory/api.h>
18 #include <nat/nat66/nat66.h>
19 #include <nat/nat66/nat66.api_enum.h>
20 #include <nat/nat66/nat66.api_types.h>
21 #include <vnet/fib/fib_table.h>
23 #define REPLY_MSG_ID_BASE nm->msg_id_base
24 #include <vlibapi/api_helper_macros.h>
27 vl_api_nat66_plugin_enable_disable_t_handler (
28 vl_api_nat66_plugin_enable_disable_t *mp)
30 nat66_main_t *nm = &nat66_main;
31 vl_api_nat66_plugin_enable_disable_reply_t *rmp;
36 rv = nat66_plugin_enable (ntohl (mp->outside_vrf));
39 rv = nat66_plugin_disable ();
41 REPLY_MACRO (VL_API_NAT66_PLUGIN_ENABLE_DISABLE_REPLY);
45 vl_api_nat66_add_del_interface_t_handler (vl_api_nat66_add_del_interface_t *
48 nat66_main_t *nm = &nat66_main;
49 vl_api_nat66_add_del_interface_reply_t *rmp;
52 VALIDATE_SW_IF_INDEX (mp);
55 nat66_interface_add_del (ntohl (mp->sw_if_index),
56 mp->flags & NAT_IS_INSIDE, mp->is_add);
58 BAD_SW_IF_INDEX_LABEL;
60 REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
64 vl_api_nat66_add_del_static_mapping_t_handler
65 (vl_api_nat66_add_del_static_mapping_t * mp)
67 nat66_main_t *nm = &nat66_main;
68 vl_api_nat66_add_del_static_mapping_reply_t *rmp;
69 ip6_address_t l_addr, e_addr;
72 memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
73 memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
76 nat66_static_mapping_add_del (&l_addr, &e_addr,
77 clib_net_to_host_u32 (mp->vrf_id),
80 REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
83 typedef struct nat66_api_walk_ctx_t_
85 vl_api_registration_t *rp;
87 } nat66_api_walk_ctx_t;
90 nat66_api_interface_walk (nat66_interface_t * i, void *arg)
92 vl_api_nat66_interface_details_t *rmp;
93 nat66_main_t *nm = &nat66_main;
94 nat66_api_walk_ctx_t *ctx = arg;
96 rmp = vl_msg_api_alloc (sizeof (*rmp));
97 clib_memset (rmp, 0, sizeof (*rmp));
98 rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + nm->msg_id_base);
99 rmp->sw_if_index = ntohl (i->sw_if_index);
100 if (nat66_interface_is_inside (i))
101 rmp->flags |= NAT_IS_INSIDE;
102 rmp->context = ctx->context;
104 vl_api_send_msg (ctx->rp, (u8 *) rmp);
110 vl_api_nat66_interface_dump_t_handler (vl_api_nat66_interface_dump_t * mp)
112 vl_api_registration_t *rp;
113 nat66_main_t *nm = &nat66_main;
115 if (PREDICT_FALSE (!nm->enabled))
118 rp = vl_api_client_index_to_registration (mp->client_index);
122 nat66_api_walk_ctx_t ctx = {
124 .context = mp->context,
127 nat66_interfaces_walk (nat66_api_interface_walk, &ctx);
131 nat66_api_static_mapping_walk (nat66_static_mapping_t * m, void *arg)
133 vl_api_nat66_static_mapping_details_t *rmp;
134 nat66_main_t *nm = &nat66_main;
135 nat66_api_walk_ctx_t *ctx = arg;
139 fib = fib_table_get (m->fib_index, FIB_PROTOCOL_IP6);
143 vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
145 rmp = vl_msg_api_alloc (sizeof (*rmp));
146 clib_memset (rmp, 0, sizeof (*rmp));
148 ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + nm->msg_id_base);
149 clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
150 clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
151 rmp->vrf_id = ntohl (fib->ft_table_id);
152 rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
153 rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
154 rmp->context = ctx->context;
156 vl_api_send_msg (ctx->rp, (u8 *) rmp);
162 vl_api_nat66_static_mapping_dump_t_handler (vl_api_nat66_static_mapping_dump_t
165 vl_api_registration_t *rp;
166 nat66_main_t *nm = &nat66_main;
168 if (PREDICT_FALSE (!nm->enabled))
171 rp = vl_api_client_index_to_registration (mp->client_index);
175 nat66_api_walk_ctx_t ctx = {
177 .context = mp->context,
180 nat66_static_mappings_walk (nat66_api_static_mapping_walk, &ctx);
183 /* API definitions */
184 #include <vnet/format_fns.h>
185 #include <nat/nat66/nat66.api.c>
187 /* Set up the API message handling tables */
189 nat66_plugin_api_hookup (vlib_main_t * vm)
191 nat66_main_t *nm = &nat66_main;
192 nm->msg_id_base = setup_message_id_table ();