2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/ip/ip_types_api.h>
17 #include <vlibmemory/api.h>
18 #include <nat/nat64/nat64.h>
19 #include <nat/nat64/nat64.api_enum.h>
20 #include <nat/nat64/nat64.api_types.h>
21 #include <vnet/fib/fib_table.h>
22 #include <vnet/ip/ip.h>
24 #define REPLY_MSG_ID_BASE nm->msg_id_base
25 #include <vlibapi/api_helper_macros.h>
28 vl_api_nat64_plugin_enable_disable_t_handler
29 (vl_api_nat64_plugin_enable_disable_t * mp)
31 nat64_main_t *nm = &nat64_main;
32 vl_api_nat64_plugin_enable_disable_reply_t *rmp;
33 nat64_config_t c = { 0 };
37 c.bib_buckets = ntohl (mp->bib_buckets);
38 c.bib_memory_size = ntohl (mp->bib_memory_size);
39 c.st_buckets = ntohl (mp->st_buckets);
40 c.st_memory_size = ntohl (mp->st_memory_size);
41 rv = nat64_plugin_enable (c);
45 rv = nat64_plugin_disable ();
47 REPLY_MACRO (VL_API_NAT64_PLUGIN_ENABLE_DISABLE_REPLY);
51 vl_api_nat64_set_timeouts_t_handler (vl_api_nat64_set_timeouts_t * mp)
53 nat64_main_t *nm = &nat64_main;
54 vl_api_nat64_set_timeouts_reply_t *rmp;
57 nm->udp_timeout = ntohl (mp->udp);
58 nm->tcp_est_timeout = ntohl (mp->tcp_established);
59 nm->tcp_trans_timeout = ntohl (mp->tcp_transitory);
60 nm->icmp_timeout = ntohl (mp->icmp);
62 REPLY_MACRO (VL_API_NAT64_SET_TIMEOUTS_REPLY);
66 vl_api_nat64_get_timeouts_t_handler (vl_api_nat64_get_timeouts_t * mp)
68 nat64_main_t *nm = &nat64_main;
69 vl_api_nat64_get_timeouts_reply_t *rmp;
73 REPLY_MACRO2 (VL_API_NAT64_GET_TIMEOUTS_REPLY,
75 rmp->udp = htonl (nm->udp_timeout);
76 rmp->tcp_established = htonl (nm->tcp_est_timeout);
77 rmp->tcp_transitory = htonl (nm->tcp_trans_timeout);
78 rmp->icmp = htonl (nm->icmp_timeout);
84 vl_api_nat64_add_del_pool_addr_range_t_handler
85 (vl_api_nat64_add_del_pool_addr_range_t * mp)
87 nat64_main_t *nm = &nat64_main;
88 vl_api_nat64_add_del_pool_addr_range_reply_t *rmp;
90 ip4_address_t this_addr;
91 u32 start_host_order, end_host_order;
96 tmp = (u32 *) mp->start_addr;
97 start_host_order = clib_host_to_net_u32 (tmp[0]);
98 tmp = (u32 *) mp->end_addr;
99 end_host_order = clib_host_to_net_u32 (tmp[0]);
101 count = (end_host_order - start_host_order) + 1;
103 vrf_id = clib_host_to_net_u32 (mp->vrf_id);
105 memcpy (&this_addr.as_u8, mp->start_addr, 4);
107 for (i = 0; i < count; i++)
109 if ((rv = nat64_add_del_pool_addr (0, &this_addr, vrf_id, mp->is_add)))
112 increment_v4_address (&this_addr);
116 REPLY_MACRO (VL_API_NAT64_ADD_DEL_POOL_ADDR_RANGE_REPLY);
119 typedef struct nat64_api_walk_ctx_t_
121 vl_api_registration_t *reg;
124 } nat64_api_walk_ctx_t;
127 nat64_api_pool_walk (nat64_address_t * a, void *arg)
129 nat64_main_t *nm = &nat64_main;
130 vl_api_nat64_pool_addr_details_t *rmp;
131 nat64_api_walk_ctx_t *ctx = arg;
133 rmp = vl_msg_api_alloc (sizeof (*rmp));
134 clib_memset (rmp, 0, sizeof (*rmp));
135 rmp->_vl_msg_id = ntohs (VL_API_NAT64_POOL_ADDR_DETAILS + nm->msg_id_base);
136 clib_memcpy (rmp->address, &(a->addr), 4);
137 if (a->fib_index != ~0)
139 fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP6);
142 rmp->vrf_id = ntohl (fib->ft_table_id);
146 rmp->context = ctx->context;
148 vl_api_send_msg (ctx->reg, (u8 *) rmp);
154 vl_api_nat64_pool_addr_dump_t_handler (vl_api_nat64_pool_addr_dump_t * mp)
156 vl_api_registration_t *reg;
158 reg = vl_api_client_index_to_registration (mp->client_index);
162 nat64_api_walk_ctx_t ctx = {
164 .context = mp->context,
167 nat64_pool_addr_walk (nat64_api_pool_walk, &ctx);
171 vl_api_nat64_add_del_interface_t_handler (vl_api_nat64_add_del_interface_t *
174 nat64_main_t *nm = &nat64_main;
175 vl_api_nat64_add_del_interface_reply_t *rmp;
178 VALIDATE_SW_IF_INDEX (mp);
181 nat64_interface_add_del (ntohl (mp->sw_if_index),
182 mp->flags & NAT_API_IS_INSIDE, mp->is_add);
184 BAD_SW_IF_INDEX_LABEL;
186 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_REPLY);
190 nat64_api_interface_walk (nat64_interface_t * i, void *arg)
192 nat64_main_t *nm = &nat64_main;
193 vl_api_nat64_interface_details_t *rmp;
194 nat64_api_walk_ctx_t *ctx = arg;
196 rmp = vl_msg_api_alloc (sizeof (*rmp));
197 clib_memset (rmp, 0, sizeof (*rmp));
198 rmp->_vl_msg_id = ntohs (VL_API_NAT64_INTERFACE_DETAILS + nm->msg_id_base);
199 rmp->sw_if_index = ntohl (i->sw_if_index);
201 if (nat64_interface_is_inside (i))
202 rmp->flags |= NAT_API_IS_INSIDE;
203 if (nat64_interface_is_outside (i))
204 rmp->flags |= NAT_API_IS_OUTSIDE;
206 rmp->context = ctx->context;
208 vl_api_send_msg (ctx->reg, (u8 *) rmp);
214 vl_api_nat64_interface_dump_t_handler (vl_api_nat64_interface_dump_t * mp)
216 vl_api_registration_t *reg;
218 reg = vl_api_client_index_to_registration (mp->client_index);
222 nat64_api_walk_ctx_t ctx = {
224 .context = mp->context,
227 nat64_interfaces_walk (nat64_api_interface_walk, &ctx);
231 vl_api_nat64_add_del_static_bib_t_handler
232 (vl_api_nat64_add_del_static_bib_t * mp)
234 nat64_main_t *nm = &nat64_main;
235 vl_api_nat64_add_del_static_bib_reply_t *rmp;
236 ip6_address_t in_addr;
237 ip4_address_t out_addr;
240 memcpy (&in_addr.as_u8, mp->i_addr, 16);
241 memcpy (&out_addr.as_u8, mp->o_addr, 4);
244 nat64_add_del_static_bib_entry (&in_addr, &out_addr,
245 clib_net_to_host_u16 (mp->i_port),
246 clib_net_to_host_u16 (mp->o_port),
248 clib_net_to_host_u32 (mp->vrf_id),
251 REPLY_MACRO (VL_API_NAT64_ADD_DEL_STATIC_BIB_REPLY);
255 nat64_api_bib_walk (nat64_db_bib_entry_t * bibe, void *arg)
257 nat64_main_t *nm = &nat64_main;
258 vl_api_nat64_bib_details_t *rmp;
259 nat64_api_walk_ctx_t *ctx = arg;
262 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
266 rmp = vl_msg_api_alloc (sizeof (*rmp));
267 clib_memset (rmp, 0, sizeof (*rmp));
268 rmp->_vl_msg_id = ntohs (VL_API_NAT64_BIB_DETAILS + nm->msg_id_base);
269 rmp->context = ctx->context;
270 clib_memcpy (rmp->i_addr, &(bibe->in_addr), 16);
271 clib_memcpy (rmp->o_addr, &(bibe->out_addr), 4);
272 rmp->i_port = bibe->in_port;
273 rmp->o_port = bibe->out_port;
274 rmp->vrf_id = ntohl (fib->ft_table_id);
275 rmp->proto = bibe->proto;
277 rmp->flags |= NAT_API_IS_STATIC;
278 rmp->ses_num = ntohl (bibe->ses_num);
280 vl_api_send_msg (ctx->reg, (u8 *) rmp);
286 vl_api_nat64_bib_dump_t_handler (vl_api_nat64_bib_dump_t * mp)
288 nat64_main_t *nm = &nat64_main;
289 vl_api_registration_t *reg;
292 reg = vl_api_client_index_to_registration (mp->client_index);
296 nat64_api_walk_ctx_t ctx = {
298 .context = mp->context,
302 vec_foreach (db, nm->db)
303 nat64_db_bib_walk (db, mp->proto, nat64_api_bib_walk, &ctx);
308 nat64_api_st_walk (nat64_db_st_entry_t * ste, void *arg)
310 nat64_main_t *nm = &nat64_main;
311 vl_api_nat64_st_details_t *rmp;
312 nat64_api_walk_ctx_t *ctx = arg;
313 nat64_db_bib_entry_t *bibe;
316 bibe = nat64_db_bib_entry_by_index (ctx->db, ste->proto, ste->bibe_index);
320 fib = fib_table_get (bibe->fib_index, FIB_PROTOCOL_IP6);
324 rmp = vl_msg_api_alloc (sizeof (*rmp));
325 clib_memset (rmp, 0, sizeof (*rmp));
326 rmp->_vl_msg_id = ntohs (VL_API_NAT64_ST_DETAILS + nm->msg_id_base);
327 rmp->context = ctx->context;
328 clib_memcpy (rmp->il_addr, &(bibe->in_addr), 16);
329 clib_memcpy (rmp->ol_addr, &(bibe->out_addr), 4);
330 rmp->il_port = bibe->in_port;
331 rmp->ol_port = bibe->out_port;
332 clib_memcpy (rmp->ir_addr, &(ste->in_r_addr), 16);
333 clib_memcpy (rmp->or_addr, &(ste->out_r_addr), 4);
334 rmp->il_port = ste->r_port;
335 rmp->vrf_id = ntohl (fib->ft_table_id);
336 rmp->proto = ste->proto;
338 vl_api_send_msg (ctx->reg, (u8 *) rmp);
344 vl_api_nat64_st_dump_t_handler (vl_api_nat64_st_dump_t * mp)
346 nat64_main_t *nm = &nat64_main;
347 vl_api_registration_t *reg;
350 reg = vl_api_client_index_to_registration (mp->client_index);
354 nat64_api_walk_ctx_t ctx = {
356 .context = mp->context,
360 vec_foreach (db, nm->db)
363 nat64_db_st_walk (db, mp->proto, nat64_api_st_walk, &ctx);
369 vl_api_nat64_add_del_prefix_t_handler (vl_api_nat64_add_del_prefix_t * mp)
371 nat64_main_t *nm = &nat64_main;
372 vl_api_nat64_add_del_prefix_reply_t *rmp;
373 ip6_address_t prefix;
376 memcpy (&prefix.as_u8, mp->prefix.address, 16);
379 nat64_add_del_prefix (&prefix, mp->prefix.len,
380 clib_net_to_host_u32 (mp->vrf_id), mp->is_add);
381 REPLY_MACRO (VL_API_NAT64_ADD_DEL_PREFIX_REPLY);
385 nat64_api_prefix_walk (nat64_prefix_t * p, void *arg)
387 nat64_main_t *nm = &nat64_main;
388 vl_api_nat64_prefix_details_t *rmp;
389 nat64_api_walk_ctx_t *ctx = arg;
391 rmp = vl_msg_api_alloc (sizeof (*rmp));
392 clib_memset (rmp, 0, sizeof (*rmp));
393 rmp->_vl_msg_id = ntohs (VL_API_NAT64_PREFIX_DETAILS + nm->msg_id_base);
394 clib_memcpy (rmp->prefix.address, &(p->prefix), 16);
395 rmp->prefix.len = p->plen;
396 rmp->vrf_id = ntohl (p->vrf_id);
397 rmp->context = ctx->context;
399 vl_api_send_msg (ctx->reg, (u8 *) rmp);
405 vl_api_nat64_prefix_dump_t_handler (vl_api_nat64_prefix_dump_t * mp)
407 vl_api_registration_t *reg;
409 reg = vl_api_client_index_to_registration (mp->client_index);
413 nat64_api_walk_ctx_t ctx = {
415 .context = mp->context,
418 nat64_prefix_walk (nat64_api_prefix_walk, &ctx);
422 vl_api_nat64_add_del_interface_addr_t_handler
423 (vl_api_nat64_add_del_interface_addr_t * mp)
425 nat64_main_t *nm = &nat64_main;
426 vl_api_nat64_add_del_interface_addr_reply_t *rmp;
427 u32 sw_if_index = ntohl (mp->sw_if_index);
430 VALIDATE_SW_IF_INDEX (mp);
432 rv = nat64_add_interface_address (sw_if_index, mp->is_add);
434 BAD_SW_IF_INDEX_LABEL;
436 REPLY_MACRO (VL_API_NAT64_ADD_DEL_INTERFACE_ADDR_REPLY);
439 /* API definitions */
440 #include <vnet/format_fns.h>
441 #include <nat/nat64/nat64.api.c>
443 /* Set up the API message handling tables */
445 nat64_api_hookup (vlib_main_t * vm)
447 nat64_main_t *nm = &nat64_main;
448 nm->msg_id_base = setup_message_id_table ();
453 * fd.io coding-style-patch-verification: ON
456 * eval: (c-set-style "gnu")