blob: 3f593c6d3ce8ad96cdb0dc0e14f0ccb2a6c65a4c [file] [log] [blame]
Dave Barachb5e8a772016-12-06 12:04:42 -05001/*
2 *------------------------------------------------------------------
3 * ip_api.c - vnet ip api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ethernet/ethernet.h>
Neale Ranns37029302018-08-10 05:30:06 -070026#include <vnet/ethernet/ethernet_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050027#include <vnet/ip/ip.h>
Neale Ranns37029302018-08-10 05:30:06 -070028#include <vnet/ip/ip_types_api.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010029#include <vnet/ip/ip_punt_drop.h>
Neale Ranns097fa662018-05-01 05:17:55 -070030#include <vnet/ip/ip_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050031#include <vnet/fib/fib_table.h>
32#include <vnet/fib/fib_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050033#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080034#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000035#include <vnet/mfib/ip4_mfib.h>
36#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080037#include <vnet/mfib/mfib_entry.h>
Neale Ranns097fa662018-05-01 05:17:55 -070038#include <vnet/mfib/mfib_api.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080039#include <vnet/ip/ip_source_and_port_range_check.h>
Neale Ranns92207752019-06-03 13:21:40 +000040#include <vnet/fib/fib_path_list.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080041#include <vnet/ip/ip6_hop_by_hop.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000042#include <vnet/ip/ip6_link.h>
Klement Sekerade34c352019-06-25 11:19:22 +000043#include <vnet/ip/reass/ip4_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000044#include <vnet/ip/reass/ip4_full_reass.h>
Klement Sekerade34c352019-06-25 11:19:22 +000045#include <vnet/ip/reass/ip6_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000046#include <vnet/ip/reass/ip6_full_reass.h>
Steven Luongc3ed1c92020-07-27 10:06:58 -070047#include <vnet/ip/ip_table.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050048
49#include <vnet/vnet_msg_enum.h>
50
51#define vl_typedefs /* define message structures */
52#include <vnet/vnet_all_api_h.h>
53#undef vl_typedefs
54
55#define vl_endianfun /* define message structures */
56#include <vnet/vnet_all_api_h.h>
57#undef vl_endianfun
58
59/* instantiate all the print functions we know about */
60#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
61#define vl_printfun
62#include <vnet/vnet_all_api_h.h>
63#undef vl_printfun
64
65#include <vlibapi/api_helper_macros.h>
66
Ole Troan33a58172019-09-04 09:12:29 +020067#include <vnet/format_fns.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080068
Dave Barachb5e8a772016-12-06 12:04:42 -050069#define foreach_ip_api_msg \
Neale Rannscbe25aa2019-09-30 10:53:31 +000070_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable) \
Neale Ranns097fa662018-05-01 05:17:55 -070071_(IP_TABLE_DUMP, ip_table_dump) \
72_(IP_ROUTE_DUMP, ip_route_dump) \
73_(IP_MTABLE_DUMP, ip_mtable_dump) \
74_(IP_MROUTE_DUMP, ip_mroute_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000075_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080076_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050077_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070078_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050079_(IP_DUMP, ip_dump) \
Neale Ranns9db6ada2019-11-08 12:42:31 +000080_(IP_TABLE_REPLACE_BEGIN, ip_table_replace_begin) \
81_(IP_TABLE_REPLACE_END, ip_table_replace_end) \
82_(IP_TABLE_FLUSH, ip_table_flush) \
Neale Ranns097fa662018-05-01 05:17:55 -070083_(IP_ROUTE_ADD_DEL, ip_route_add_del) \
Christian Hoppsf5d38e02020-05-04 10:28:03 -040084_(IP_ROUTE_LOOKUP, ip_route_lookup) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070085_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070086_(IP_PUNT_POLICE, ip_punt_police) \
87_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050088_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
Neale Rannsb8d44812017-11-10 06:53:54 -080089_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
Matus Fabian75b9f452018-10-02 23:27:21 -070090_(IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080091_(IOAM_ENABLE, ioam_enable) \
92_(IOAM_DISABLE, ioam_disable) \
93_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
94 ip_source_and_port_range_check_add_del) \
95_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +020096 ip_source_and_port_range_check_interface_add_del) \
Neale Rannscbe25aa2019-09-30 10:53:31 +000097 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
98 sw_interface_ip6_set_link_local_address) \
Klement Sekera75e7d132017-09-20 08:26:30 +020099_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100100_(IP_REASSEMBLY_GET, ip_reassembly_get) \
Pavel Kotucek609e1212018-11-27 09:59:44 +0100101_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
102_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
103
Dave Barachb5e8a772016-12-06 12:04:42 -0500104static void
Neale Rannscbe25aa2019-09-30 10:53:31 +0000105 vl_api_sw_interface_ip6_enable_disable_t_handler
106 (vl_api_sw_interface_ip6_enable_disable_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500107{
Neale Rannscbe25aa2019-09-30 10:53:31 +0000108 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
109 int rv = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500110
Neale Rannscbe25aa2019-09-30 10:53:31 +0000111 VALIDATE_SW_IF_INDEX (mp);
Neale Ranns37029302018-08-10 05:30:06 -0700112
Neale Rannscbe25aa2019-09-30 10:53:31 +0000113 rv = ((mp->enable == 1) ?
Neale Rannsec40a7d2020-04-23 07:36:12 +0000114 ip6_link_enable (ntohl (mp->sw_if_index), NULL) :
Neale Rannscbe25aa2019-09-30 10:53:31 +0000115 ip6_link_disable (ntohl (mp->sw_if_index)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500116
Neale Rannscbe25aa2019-09-30 10:53:31 +0000117 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500118
Neale Rannscbe25aa2019-09-30 10:53:31 +0000119 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -0500120}
121
Dave Barachb5e8a772016-12-06 12:04:42 -0500122static void
Neale Ranns097fa662018-05-01 05:17:55 -0700123send_ip_table_details (vpe_api_main_t * am,
124 vl_api_registration_t * reg,
125 u32 context, const fib_table_t * table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500126{
Neale Ranns097fa662018-05-01 05:17:55 -0700127 vl_api_ip_table_details_t *mp;
Dave Barachb5e8a772016-12-06 12:04:42 -0500128
Neale Ranns097fa662018-05-01 05:17:55 -0700129 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500130 if (!mp)
131 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400132 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700133 mp->_vl_msg_id = ntohs (VL_API_IP_TABLE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500134 mp->context = context;
135
Neale Ranns097fa662018-05-01 05:17:55 -0700136 mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
137 mp->table.table_id = htonl (table->ft_table_id);
138 memcpy (mp->table.name, table->ft_desc,
139 clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500140
Florin Coras6c4dae22018-01-09 06:39:23 -0800141 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500142}
143
Neale Ranns097fa662018-05-01 05:17:55 -0700144static void
145vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
146{
147 vpe_api_main_t *am = &vpe_api_main;
148 vl_api_registration_t *reg;
149 fib_table_t *fib_table;
150
151 reg = vl_api_client_index_to_registration (mp->client_index);
152 if (!reg)
153 return;
154
155 /* *INDENT-OFF* */
156 pool_foreach (fib_table, ip4_main.fibs,
157 ({
158 send_ip_table_details(am, reg, mp->context, fib_table);
159 }));
160 pool_foreach (fib_table, ip6_main.fibs,
161 ({
162 /* don't send link locals */
163 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
164 continue;
165 send_ip_table_details(am, reg, mp->context, fib_table);
166 }));
167 /* *INDENT-ON* */
168}
169
Neale Rannsa3af3372017-03-28 03:49:52 -0700170typedef struct vl_api_ip_fib_dump_walk_ctx_t_
171{
172 fib_node_index_t *feis;
173} vl_api_ip_fib_dump_walk_ctx_t;
174
Neale Ranns89541992017-04-06 04:41:02 -0700175static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700176vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
177{
178 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
179
180 vec_add1 (ctx->feis, fei);
181
Neale Ranns89541992017-04-06 04:41:02 -0700182 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700183}
184
Dave Barachb5e8a772016-12-06 12:04:42 -0500185static void
Neale Ranns097fa662018-05-01 05:17:55 -0700186send_ip_route_details (vpe_api_main_t * am,
187 vl_api_registration_t * reg,
188 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500189{
Neale Ranns097fa662018-05-01 05:17:55 -0700190 fib_route_path_t *rpaths, *rpath;
191 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700192 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500193 vl_api_fib_path_t *fp;
194 int path_count;
195
Neale Ranns097fa662018-05-01 05:17:55 -0700196 rpaths = NULL;
197 pfx = fib_entry_get_prefix (fib_entry_index);
198 rpaths = fib_entry_encode (fib_entry_index);
199
200 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500201 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
202 if (!mp)
203 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400204 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700205 mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500206 mp->context = context;
207
Neale Ranns097fa662018-05-01 05:17:55 -0700208 ip_prefix_encode (pfx, &mp->route.prefix);
209 mp->route.table_id =
210 htonl (fib_table_get_table_id
211 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
212 mp->route.n_paths = path_count;
213 mp->route.stats_index =
214 htonl (fib_table_entry_get_stats_index
215 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500216
Neale Ranns097fa662018-05-01 05:17:55 -0700217 fp = mp->route.paths;
218 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500219 {
Neale Ranns097fa662018-05-01 05:17:55 -0700220 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500221 fp++;
222 }
223
Florin Coras6c4dae22018-01-09 06:39:23 -0800224 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000225 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500226}
227
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228typedef struct apt_ip6_fib_show_ctx_t_
229{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500230 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500231} api_ip6_fib_show_ctx_t;
232
Dave Barachb5e8a772016-12-06 12:04:42 -0500233static void
Neale Ranns097fa662018-05-01 05:17:55 -0700234vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500235{
236 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500237 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800238 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700239 fib_protocol_t fproto;
240 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500241
Florin Coras6c4dae22018-01-09 06:39:23 -0800242 reg = vl_api_client_index_to_registration (mp->client_index);
243 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500244 return;
245
Neale Ranns097fa662018-05-01 05:17:55 -0700246 vl_api_ip_fib_dump_walk_ctx_t ctx = {
247 .feis = NULL,
248 };
Neale Ranns81458422018-03-12 06:59:36 -0700249
Neale Ranns097fa662018-05-01 05:17:55 -0700250 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
251 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
252
253 if (INDEX_INVALID == fib_index)
254 return;
255
256 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
257
258 vec_foreach (fib_entry_index, ctx.feis)
259 {
260 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
261 }
262
263 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500264}
265
266static void
Neale Ranns097fa662018-05-01 05:17:55 -0700267send_ip_mtable_details (vl_api_registration_t * reg,
268 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800269{
Neale Ranns097fa662018-05-01 05:17:55 -0700270 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800271
Neale Ranns097fa662018-05-01 05:17:55 -0700272 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800273 if (!mp)
274 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700275 memset (mp, 0, sizeof (*mp));
276 mp->_vl_msg_id = ntohs (VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800277 mp->context = context;
278
Neale Ranns097fa662018-05-01 05:17:55 -0700279 mp->table.table_id = htonl (mfib_table->mft_table_id);
280 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800281
Florin Coras6c4dae22018-01-09 06:39:23 -0800282 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800283}
284
Neale Ranns5a8123b2017-01-26 01:18:23 -0800285static void
Neale Ranns097fa662018-05-01 05:17:55 -0700286vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800287{
Florin Coras6c4dae22018-01-09 06:39:23 -0800288 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800289 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800290
Florin Coras6c4dae22018-01-09 06:39:23 -0800291 reg = vl_api_client_index_to_registration (mp->client_index);
292 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800293 return;
294
Neale Ranns5a8123b2017-01-26 01:18:23 -0800295 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700296 pool_foreach (mfib_table, ip4_main.mfibs,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800297 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700298 send_ip_mtable_details (reg, mp->context, mfib_table);
299 }));
300 pool_foreach (mfib_table, ip6_main.mfibs,
301 ({
302 send_ip_mtable_details (reg, mp->context, mfib_table);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800303 }));
304 /* *INDENT-ON* */
Neale Ranns5a8123b2017-01-26 01:18:23 -0800305}
306
Neale Ranns097fa662018-05-01 05:17:55 -0700307typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800308{
309 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700310} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800311
Neale Ranns9db6ada2019-11-08 12:42:31 +0000312static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700313mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800314{
Neale Ranns097fa662018-05-01 05:17:55 -0700315 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800316
317 vec_add1 (ctx->entries, fei);
318
Neale Ranns9db6ada2019-11-08 12:42:31 +0000319 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800320}
321
322static void
Neale Ranns097fa662018-05-01 05:17:55 -0700323send_ip_mroute_details (vpe_api_main_t * am,
324 vl_api_registration_t * reg,
325 u32 context, fib_node_index_t mfib_entry_index)
326{
327 fib_route_path_t *rpaths, *rpath;
328 vl_api_ip_mroute_details_t *mp;
329 const mfib_prefix_t *pfx;
330 vl_api_mfib_path_t *fp;
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500331 u8 path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700332
333 rpaths = NULL;
334 pfx = mfib_entry_get_prefix (mfib_entry_index);
335 rpaths = mfib_entry_encode (mfib_entry_index);
336
337 path_count = vec_len (rpaths);
338 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
339 if (!mp)
340 return;
341 clib_memset (mp, 0, sizeof (*mp));
342 mp->_vl_msg_id = ntohs (VL_API_IP_MROUTE_DETAILS);
343 mp->context = context;
344
345 ip_mprefix_encode (pfx, &mp->route.prefix);
346 mp->route.table_id =
347 htonl (mfib_table_get_table_id
348 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500349 mp->route.n_paths = path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700350 fp = mp->route.paths;
351 vec_foreach (rpath, rpaths)
352 {
353 mfib_api_path_encode (rpath, fp);
354 fp++;
355 }
356
357 vl_api_send_msg (reg, (u8 *) mp);
358 vec_free (rpaths);
359}
360
361static void
362vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800363{
364 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800365 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800366 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700367 fib_protocol_t fproto;
368 u32 fib_index;
369
370 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800371 .entries = NULL,
372 };
373
Florin Coras6c4dae22018-01-09 06:39:23 -0800374 reg = vl_api_client_index_to_registration (mp->client_index);
375 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800376 return;
377
Neale Ranns097fa662018-05-01 05:17:55 -0700378 fproto = fib_ip_proto (mp->table.is_ip6);
379 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800380
Neale Ranns097fa662018-05-01 05:17:55 -0700381 if (INDEX_INVALID == fib_index)
382 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800383
Neale Ranns097fa662018-05-01 05:17:55 -0700384 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800385
Neale Ranns097fa662018-05-01 05:17:55 -0700386 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800387
Neale Ranns097fa662018-05-01 05:17:55 -0700388 vec_foreach (mfeip, ctx.entries)
389 {
390 send_ip_mroute_details (am, reg, mp->context, *mfeip);
391 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800392
393 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800394}
395
396static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700397vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
398 vlib_main_t * vm)
399{
400 vl_api_ip_punt_police_reply_t *rmp;
401 int rv = 0;
402
403 if (mp->is_ip6)
404 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
405 else
406 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
407
408 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
409}
410
411static void
412vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
413 vlib_main_t * vm)
414{
415 vl_api_ip_punt_redirect_reply_t *rmp;
416 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100417 ip46_type_t ipv;
418 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700419
Pavel Kotucek609e1212018-11-27 09:59:44 +0100420 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
421 goto bad_sw_if_index;
422
423 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700424 if (mp->is_add)
425 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100426 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700427 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100428 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
429 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700430 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100431 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700432 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100433 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
434 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700435 }
436 }
437 else
438 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100439 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700440 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100441 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700442 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100443 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700444 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100445 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700446 }
447 }
448
Pavel Kotucek609e1212018-11-27 09:59:44 +0100449 BAD_SW_IF_INDEX_LABEL;
450
Neale Rannsd91c1db2017-07-31 02:30:50 -0700451 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
452}
453
Steven Luongc3ed1c92020-07-27 10:06:58 -0700454static clib_error_t *
455call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
456 u32 flags,
457 _vnet_ip_table_function_list_elt_t **
458 elts)
459{
460 _vnet_ip_table_function_list_elt_t *elt;
461 vnet_ip_table_function_priority_t prio;
462 clib_error_t *error = 0;
463
464 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
465 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
466 {
467 elt = elts[prio];
468
469 while (elt)
470 {
471 error = elt->fp (vnm, table_id, flags);
472 if (error)
473 return error;
474 elt = elt->next_ip_table_function;
475 }
476 }
477 return error;
478}
479
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700480void
Neale Ranns15002542017-09-10 04:39:11 -0700481ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
482{
483 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700484 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700485
486 /*
487 * ignore action on the default table - this is always present
488 * and cannot be added nor deleted from the API
489 */
490 if (0 != table_id)
491 {
492 /*
493 * The API holds only one lock on the table.
494 * i.e. it can be added many times via the API but needs to be
495 * deleted only once.
496 * The FIB index for unicast and multicast is not necessarily the
497 * same, since internal VPP systesm (like LISP and SR) create
498 * their own unicast tables.
499 */
500 fib_index = fib_table_find (fproto, table_id);
501 mfib_index = mfib_table_find (fproto, table_id);
502
Steven Luongc3ed1c92020-07-27 10:06:58 -0700503 if ((~0 != fib_index) || (~0 != mfib_index))
504 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
505 vnm->ip_table_add_del_functions);
506
Neale Ranns15002542017-09-10 04:39:11 -0700507 if (~0 != fib_index)
508 {
509 fib_table_unlock (fib_index, fproto,
510 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
511 }
512 if (~0 != mfib_index)
513 {
514 mfib_table_unlock (mfib_index, fproto,
515 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
516 }
517 }
518}
519
520void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700521vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
522{
523 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700524 fib_protocol_t fproto = (mp->table.is_ip6 ?
525 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
526 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700527 int rv = 0;
528
Neale Ranns15002542017-09-10 04:39:11 -0700529 if (mp->is_add)
530 {
Neale Ranns097fa662018-05-01 05:17:55 -0700531 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700532 }
533 else
534 {
535 ip_table_delete (fproto, table_id, 1);
536 }
537
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700538 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
539}
540
Dave Barachb5e8a772016-12-06 12:04:42 -0500541static int
Neale Ranns097fa662018-05-01 05:17:55 -0700542ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500543{
Neale Ranns097fa662018-05-01 05:17:55 -0700544 fib_route_path_t *rpaths = NULL, *rpath;
545 fib_entry_flag_t entry_flags;
546 vl_api_fib_path_t *apath;
547 fib_prefix_t pfx;
548 u32 fib_index;
549 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500550
Neale Ranns097fa662018-05-01 05:17:55 -0700551 entry_flags = FIB_ENTRY_FLAG_NONE;
552 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500553
Neale Ranns097fa662018-05-01 05:17:55 -0700554 rv = fib_api_table_id_decode (pfx.fp_proto,
555 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500556 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700557 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500558
Neale Rannsc2ac2352019-07-02 14:33:29 +0000559 if (0 != mp->route.n_paths)
560 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700561
562 for (ii = 0; ii < mp->route.n_paths; ii++)
563 {
564 apath = &mp->route.paths[ii];
565 rpath = &rpaths[ii];
566
567 rv = fib_api_path_decode (apath, rpath);
568
569 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
570 (~0 == rpath->frp_sw_if_index))
571 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
572
573 if (0 != rv)
574 goto out;
575 }
576
Neale Rannsc2ac2352019-07-02 14:33:29 +0000577 rv = fib_api_route_add_del (mp->is_add,
578 mp->is_multipath,
579 fib_index, &pfx, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700580
581 if (mp->is_add && 0 == rv)
582 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
583
Neale Ranns097fa662018-05-01 05:17:55 -0700584out:
585 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700586
587 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500588}
589
590void
Neale Ranns097fa662018-05-01 05:17:55 -0700591vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500592{
Neale Ranns097fa662018-05-01 05:17:55 -0700593 vl_api_ip_route_add_del_reply_t *rmp;
594 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500595 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500596
Neale Ranns097fa662018-05-01 05:17:55 -0700597 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500598
Neale Ranns008dbe12018-09-07 09:32:36 -0700599 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700600 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700601 ({
602 rmp->stats_index = htonl (stats_index);
603 }))
604 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500605}
606
Neale Ranns15002542017-09-10 04:39:11 -0700607void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400608vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
609{
610 vl_api_ip_route_lookup_reply_t *rmp = NULL;
611 fib_route_path_t *rpaths = NULL, *rpath;
612 const fib_prefix_t *pfx = NULL;
613 fib_prefix_t lookup;
614 vl_api_fib_path_t *fp;
615 fib_node_index_t fib_entry_index;
616 u32 fib_index;
617 int npaths = 0;
618 int rv;
619
620 ip_prefix_decode (&mp->prefix, &lookup);
621 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
622 &fib_index);
623 if (PREDICT_TRUE (!rv))
624 {
625 if (mp->exact)
626 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
627 else
628 fib_entry_index = fib_table_lookup (fib_index, &lookup);
629 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
630 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
631 else
632 {
633 pfx = fib_entry_get_prefix (fib_entry_index);
634 rpaths = fib_entry_encode (fib_entry_index);
635 npaths = vec_len (rpaths);
636 }
637 }
638
639 /* *INDENT-OFF* */
640 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
641 npaths * sizeof (*fp),
642 ({
643 if (!rv)
644 {
645 ip_prefix_encode (pfx, &rmp->route.prefix);
646 rmp->route.table_id = mp->table_id;
647 rmp->route.n_paths = npaths;
648 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
649 rmp->route.stats_index = htonl (rmp->route.stats_index);
650
651 fp = rmp->route.paths;
652 vec_foreach (rpath, rpaths)
653 {
654 fib_api_path_encode (rpath, fp);
655 fp++;
656 }
657 }
658 }));
659 /* *INDENT-ON* */
660 vec_free (rpaths);
661}
662
663void
Neale Ranns2297af02017-09-12 09:45:04 -0700664ip_table_create (fib_protocol_t fproto,
665 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700666{
667 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700668 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700669
670 /*
671 * ignore action on the default table - this is always present
672 * and cannot be added nor deleted from the API
673 */
674 if (0 != table_id)
675 {
676 /*
677 * The API holds only one lock on the table.
678 * i.e. it can be added many times via the API but needs to be
679 * deleted only once.
680 * The FIB index for unicast and multicast is not necessarily the
681 * same, since internal VPP systesm (like LISP and SR) create
682 * their own unicast tables.
683 */
684 fib_index = fib_table_find (fproto, table_id);
685 mfib_index = mfib_table_find (fproto, table_id);
686
687 if (~0 == fib_index)
688 {
Neale Ranns2297af02017-09-12 09:45:04 -0700689 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
690 (is_api ?
691 FIB_SOURCE_API :
692 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700693 }
694 if (~0 == mfib_index)
695 {
Neale Ranns2297af02017-09-12 09:45:04 -0700696 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
697 (is_api ?
698 MFIB_SOURCE_API :
699 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700700 }
Steven Luongc3ed1c92020-07-27 10:06:58 -0700701
702 if ((~0 == fib_index) || (~0 == mfib_index))
703 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
704 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700705 }
706}
707
Neale Ranns097fa662018-05-01 05:17:55 -0700708static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000709mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700710 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000711 u32 fib_index,
712 const mfib_prefix_t * prefix,
713 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700714 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000715{
Neale Ranns097fa662018-05-01 05:17:55 -0700716 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700717
Neale Ranns097fa662018-05-01 05:17:55 -0700718 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000719 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700720 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
721 MFIB_SOURCE_API,
722 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000723 }
724 else
725 {
Neale Ranns097fa662018-05-01 05:17:55 -0700726 if (is_add)
727 {
728 mfib_entry_index =
729 mfib_table_entry_paths_update (fib_index, prefix,
730 MFIB_SOURCE_API, rpaths);
731 }
732 else
733 {
734 mfib_table_entry_paths_remove (fib_index, prefix,
735 MFIB_SOURCE_API, rpaths);
736 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000737 }
738
Neale Ranns28c142e2018-09-07 09:37:07 -0700739 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000740}
741
742static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700743api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
744 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000745{
Neale Ranns097fa662018-05-01 05:17:55 -0700746 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -0700747 fib_node_index_t mfib_entry_index;
Neale Ranns097fa662018-05-01 05:17:55 -0700748 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +0000749 u32 fib_index;
750 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -0700751 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +0000752
Neale Ranns097fa662018-05-01 05:17:55 -0700753 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000754
Neale Ranns097fa662018-05-01 05:17:55 -0700755 rv = mfib_api_table_id_decode (pfx.fp_proto,
756 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000757 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700758 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000759
Neale Ranns097fa662018-05-01 05:17:55 -0700760 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +0000761
Neale Ranns097fa662018-05-01 05:17:55 -0700762 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000763 {
Neale Ranns097fa662018-05-01 05:17:55 -0700764 rpath = &rpaths[ii];
765
766 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
767
768 if (0 != rv)
769 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000770 }
771
Neale Ranns28c142e2018-09-07 09:37:07 -0700772 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700773 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -0700774 fib_index, &pfx,
Neale Ranns097fa662018-05-01 05:17:55 -0700775 ntohl (mp->route.entry_flags),
776 ntohl (mp->route.rpf_id),
777 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -0700778
779 if (~0 != mfib_entry_index)
780 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
781
Neale Ranns097fa662018-05-01 05:17:55 -0700782out:
Neale Ranns28c142e2018-09-07 09:37:07 -0700783 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +0000784}
785
786void
787vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
788{
789 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700790 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000791 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +0000792
Neale Ranns28c142e2018-09-07 09:37:07 -0700793 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000794
Neale Ranns28c142e2018-09-07 09:37:07 -0700795 /* *INDENT-OFF* */
796 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
797 ({
798 rmp->stats_index = htonl (stats_index);
799 }));
800 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +0000801}
802
Dave Barachb5e8a772016-12-06 12:04:42 -0500803static void
804send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800805 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
806 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500807{
808 vl_api_ip_details_t *mp;
809
810 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400811 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500812 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
813
814 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -0600815 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -0500816 mp->context = context;
817
Florin Coras6c4dae22018-01-09 06:39:23 -0800818 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500819}
820
821static void
822send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800823 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -0700824 const fib_prefix_t * pfx,
825 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500826{
827 vl_api_ip_address_details_t *mp;
828
829 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400830 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500831 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
832
Neale Ranns097fa662018-05-01 05:17:55 -0700833 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -0500834 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -0600835 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500836
Florin Coras6c4dae22018-01-09 06:39:23 -0800837 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500838}
839
840static void
841vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
842{
843 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800844 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500845 ip6_main_t *im6 = &ip6_main;
846 ip4_main_t *im4 = &ip4_main;
847 ip_lookup_main_t *lm6 = &im6->lookup_main;
848 ip_lookup_main_t *lm4 = &im4->lookup_main;
849 ip_interface_address_t *ia = 0;
850 u32 sw_if_index = ~0;
851 int rv __attribute__ ((unused)) = 0;
852
853 VALIDATE_SW_IF_INDEX (mp);
854
855 sw_if_index = ntohl (mp->sw_if_index);
856
Florin Coras6c4dae22018-01-09 06:39:23 -0800857 reg = vl_api_client_index_to_registration (mp->client_index);
858 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500859 return;
860
Dave Barachb5e8a772016-12-06 12:04:42 -0500861 if (mp->is_ipv6)
862 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500863 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -0700864 /* Do not send subnet details of the IP-interface for
865 * unnumbered interfaces. otherwise listening clients
866 * will be confused that the subnet is applied on more
867 * than one interface */
868 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -0500869 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700870 fib_prefix_t pfx = {
871 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
872 .fp_len = ia->address_length,
873 .fp_proto = FIB_PROTOCOL_IP6,
874 };
875 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500876 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500877 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500878 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500879 else
880 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -0700882 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -0500883 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700884 fib_prefix_t pfx = {
885 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
886 .fp_len = ia->address_length,
887 .fp_proto = FIB_PROTOCOL_IP4,
888 };
889
890 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500891 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500892 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500893 }
Neale Ranns008dbe12018-09-07 09:32:36 -0700894
Dave Barachb5e8a772016-12-06 12:04:42 -0500895 BAD_SW_IF_INDEX_LABEL;
896}
897
898static void
Neale Ranns9e2f9152018-05-18 02:27:10 -0700899send_ip_unnumbered_details (vpe_api_main_t * am,
900 vl_api_registration_t * reg,
901 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
902{
903 vl_api_ip_unnumbered_details_t *mp;
904
905 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400906 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -0700907 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
908
909 mp->context = context;
910 mp->sw_if_index = htonl (sw_if_index);
911 mp->ip_sw_if_index = htonl (ip_sw_if_index);
912
913 vl_api_send_msg (reg, (u8 *) mp);
914}
915
916static void
917vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
918{
919 vnet_main_t *vnm = vnet_get_main ();
920 vnet_interface_main_t *im = &vnm->interface_main;
921 int rv __attribute__ ((unused)) = 0;
922 vpe_api_main_t *am = &vpe_api_main;
923 vl_api_registration_t *reg;
924 vnet_sw_interface_t *si;
925 u32 sw_if_index;
926
927 sw_if_index = ntohl (mp->sw_if_index);
928
929 reg = vl_api_client_index_to_registration (mp->client_index);
930 if (!reg)
931 return;
932
933 if (~0 != sw_if_index)
934 {
935 VALIDATE_SW_IF_INDEX (mp);
936
937 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
938
Neale Rannsac3e72c2019-10-06 01:04:26 -0700939 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -0700940 {
941 send_ip_unnumbered_details (am, reg,
942 sw_if_index,
943 si->unnumbered_sw_if_index,
944 mp->context);
945 }
946 }
947 else
948 {
949 /* *INDENT-OFF* */
950 pool_foreach (si, im->sw_interfaces,
951 ({
952 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
953 {
954 send_ip_unnumbered_details(am, reg,
955 si->sw_if_index,
956 si->unnumbered_sw_if_index,
957 mp->context);
958 }
959 }));
960 /* *INDENT-ON* */
961 }
962
963 BAD_SW_IF_INDEX_LABEL;
964}
965
966static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500967vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
968{
969 vpe_api_main_t *am = &vpe_api_main;
970 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +0000971 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500972 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800973 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500974 vnet_sw_interface_t *si, *sorted_sis;
975 u32 sw_if_index = ~0;
976
Florin Coras6c4dae22018-01-09 06:39:23 -0800977 reg = vl_api_client_index_to_registration (mp->client_index);
978 if (!reg)
979 return;
Dave Barachb5e8a772016-12-06 12:04:42 -0500980
981 /* Gather interfaces. */
982 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
983 _vec_len (sorted_sis) = 0;
984 /* *INDENT-OFF* */
985 pool_foreach (si, im->sw_interfaces,
986 ({
987 vec_add1 (sorted_sis, si[0]);
988 }));
989 /* *INDENT-ON* */
990
991 vec_foreach (si, sorted_sis)
992 {
993 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
994 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000995 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
996 /* { */
997 /* continue; */
998 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -0500999 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001000 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001001 }
1002 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001003
1004 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001005}
1006
1007static void
1008set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1009{
1010 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001011 int rv;
1012 u32 table_id;
1013 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001014
Neale Ranns227038a2017-04-21 01:07:59 -07001015 table_id = ntohl (mp->vrf_id);
1016
1017#define _(a,b) if (mp->a) flow_hash_config |= b;
1018 foreach_flow_hash_bit;
1019#undef _
1020
1021 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001022
1023 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1024}
1025
1026static void
1027set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1028{
1029 vl_api_set_ip_flow_hash_reply_t *rmp;
1030 int rv;
1031 u32 table_id;
1032 flow_hash_config_t flow_hash_config = 0;
1033
1034 table_id = ntohl (mp->vrf_id);
1035
1036#define _(a,b) if (mp->a) flow_hash_config |= b;
1037 foreach_flow_hash_bit;
1038#undef _
1039
1040 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1041
1042 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1043}
1044
1045
1046static void
1047vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1048{
1049 if (mp->is_ipv6 == 0)
1050 set_ip4_flow_hash (mp);
1051 else
1052 set_ip6_flow_hash (mp);
1053}
1054
Neale Ranns32e1c012016-11-22 17:07:28 +00001055void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001056vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001057 u32 context, const mfib_signal_t * mfs)
1058{
1059 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001060 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001061 mfib_table_t *mfib;
1062 mfib_itf_t *mfi;
1063
1064 mp = vl_msg_api_alloc (sizeof (*mp));
1065
Dave Barachb7b92992018-10-17 10:38:51 -04001066 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001067 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1068 mp->context = context;
1069
1070 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001071 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001072 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001073 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001074 mp->table_id = ntohl (mfib->mft_table_id);
1075 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1076
Neale Ranns097fa662018-05-01 05:17:55 -07001077 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001078
1079 if (0 != mfs->mfs_buffer_len)
1080 {
1081 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1082
1083 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1084 }
1085 else
1086 {
1087 mp->ip_packet_len = 0;
1088 }
1089
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001090 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001091}
1092
1093static void
1094vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1095{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001096 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001097
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001098 reg = vl_api_client_index_to_registration (mp->client_index);
1099 if (!reg)
1100 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001101
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001102 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001103 ;
1104}
Dave Barachb5e8a772016-12-06 12:04:42 -05001105
Florin Coras595992c2017-11-06 17:17:08 -08001106static void
1107 vl_api_ip_container_proxy_add_del_t_handler
1108 (vl_api_ip_container_proxy_add_del_t * mp)
1109{
1110 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1111 vnet_ip_container_proxy_args_t args;
1112 int rv = 0;
1113 clib_error_t *error;
1114
Dave Barachb7b92992018-10-17 10:38:51 -04001115 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001116
1117 ip_prefix_decode (&mp->pfx, &args.prefix);
1118
Florin Coras595992c2017-11-06 17:17:08 -08001119 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1120 args.is_add = mp->is_add;
1121 if ((error = vnet_ip_container_proxy_add_del (&args)))
1122 {
1123 rv = clib_error_get_code (error);
1124 clib_error_report (error);
1125 }
1126
1127 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1128}
1129
Matus Fabian75b9f452018-10-02 23:27:21 -07001130typedef struct ip_container_proxy_walk_ctx_t_
1131{
1132 vl_api_registration_t *reg;
1133 u32 context;
1134} ip_container_proxy_walk_ctx_t;
1135
1136static int
1137ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1138 void *args)
1139{
1140 vl_api_ip_container_proxy_details_t *mp;
1141 ip_container_proxy_walk_ctx_t *ctx = args;
1142
1143 mp = vl_msg_api_alloc (sizeof (*mp));
1144 if (!mp)
1145 return 1;
1146
Dave Barachb7b92992018-10-17 10:38:51 -04001147 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001148 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1149 mp->context = ctx->context;
1150
1151 mp->sw_if_index = ntohl (sw_if_index);
1152 ip_prefix_encode (pfx, &mp->prefix);
1153
1154 vl_api_send_msg (ctx->reg, (u8 *) mp);
1155
1156 return 1;
1157}
1158
1159static void
1160vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1161 mp)
1162{
1163 vl_api_registration_t *reg;
1164
1165 reg = vl_api_client_index_to_registration (mp->client_index);
1166 if (!reg)
1167 return;
1168
1169 ip_container_proxy_walk_ctx_t ctx = {
1170 .context = mp->context,
1171 .reg = reg,
1172 };
1173
1174 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1175}
1176
Neale Rannsb8d44812017-11-10 06:53:54 -08001177static void
1178vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1179{
1180 int rv = 0;
1181 vl_api_ioam_enable_reply_t *rmp;
1182 clib_error_t *error;
1183
1184 /* Ignoring the profile id as currently a single profile
1185 * is supported */
1186 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1187 mp->seqno, mp->analyse);
1188 if (error)
1189 {
1190 clib_error_report (error);
1191 rv = clib_error_get_code (error);
1192 }
1193
1194 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1195}
1196
1197static void
1198vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1199{
1200 int rv = 0;
1201 vl_api_ioam_disable_reply_t *rmp;
1202 clib_error_t *error;
1203
1204 error = clear_ioam_rewrite_fn ();
1205 if (error)
1206 {
1207 clib_error_report (error);
1208 rv = clib_error_get_code (error);
1209 }
1210
1211 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1212}
1213
1214static void
1215 vl_api_ip_source_and_port_range_check_add_del_t_handler
1216 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1217{
1218 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1219 int rv = 0;
1220
Neale Rannsb8d44812017-11-10 06:53:54 -08001221 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001222 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001223 u16 *low_ports = 0;
1224 u16 *high_ports = 0;
1225 u32 vrf_id;
1226 u16 tmp_low, tmp_high;
1227 u8 num_ranges;
1228 int i;
1229
Neale Ranns37029302018-08-10 05:30:06 -07001230 ip_prefix_decode (&mp->prefix, &pfx);
1231
Neale Rannsb8d44812017-11-10 06:53:54 -08001232 // Validate port range
1233 num_ranges = mp->number_of_ranges;
1234 if (num_ranges > 32)
1235 { // This is size of array in VPE.API
1236 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1237 goto reply;
1238 }
1239
1240 vec_reset_length (low_ports);
1241 vec_reset_length (high_ports);
1242
1243 for (i = 0; i < num_ranges; i++)
1244 {
1245 tmp_low = mp->low_ports[i];
1246 tmp_high = mp->high_ports[i];
1247 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1248 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1249 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1250 {
1251 rv = VNET_API_ERROR_INVALID_VALUE;
1252 goto reply;
1253 }
1254 vec_add1 (low_ports, tmp_low);
1255 vec_add1 (high_ports, tmp_high + 1);
1256 }
1257
Neale Rannsb8d44812017-11-10 06:53:54 -08001258 vrf_id = ntohl (mp->vrf_id);
1259
1260 if (vrf_id < 1)
1261 {
1262 rv = VNET_API_ERROR_INVALID_VALUE;
1263 goto reply;
1264 }
1265
1266
Neale Ranns37029302018-08-10 05:30:06 -07001267 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001268 {
Neale Ranns37029302018-08-10 05:30:06 -07001269 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1270 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001271 vrf_id,
1272 low_ports,
1273 high_ports, is_add);
1274 }
1275 else
1276 {
Neale Ranns37029302018-08-10 05:30:06 -07001277 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1278 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001279 vrf_id,
1280 low_ports,
1281 high_ports, is_add);
1282 }
1283
1284reply:
1285 vec_free (low_ports);
1286 vec_free (high_ports);
1287 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1288}
1289
1290static void
1291 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1292 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1293{
1294 vlib_main_t *vm = vlib_get_main ();
1295 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1296 ip4_main_t *im = &ip4_main;
1297 int rv;
1298 u32 sw_if_index;
1299 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1300 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1301 uword *p = 0;
1302 int i;
1303
1304 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1305 ntohl (mp->tcp_out_vrf_id);
1306 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1307 ntohl (mp->udp_out_vrf_id);
1308 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1309 ntohl (mp->tcp_in_vrf_id);
1310 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1311 ntohl (mp->udp_in_vrf_id);
1312
1313
1314 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1315 {
1316 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1317 {
1318 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1319
1320 if (p == 0)
1321 {
1322 rv = VNET_API_ERROR_INVALID_VALUE;
1323 goto reply;
1324 }
1325
1326 fib_index[i] = p[0];
1327 }
1328 else
1329 fib_index[i] = ~0;
1330 }
1331 sw_if_index = ntohl (mp->sw_if_index);
1332
1333 VALIDATE_SW_IF_INDEX (mp);
1334
1335 rv =
1336 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1337 mp->is_add);
1338
1339 BAD_SW_IF_INDEX_LABEL;
1340reply:
1341
1342 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1343}
1344
Neale Rannscbe25aa2019-09-30 10:53:31 +00001345static void
1346 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1347 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1348{
1349 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1350 ip6_address_t ip;
1351 int rv;
1352
1353 VALIDATE_SW_IF_INDEX (mp);
1354
1355 ip6_address_decode (mp->ip, &ip);
1356
Neale Rannsec40a7d2020-04-23 07:36:12 +00001357 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001358
1359 BAD_SW_IF_INDEX_LABEL;
1360 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1361}
1362
Neale Ranns9db6ada2019-11-08 12:42:31 +00001363static void
1364vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001365{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001366 vl_api_ip_table_replace_begin_reply_t *rmp;
1367 fib_protocol_t fproto;
1368 u32 fib_index;
1369 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001370
Neale Ranns9db6ada2019-11-08 12:42:31 +00001371 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1372 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001373
Neale Ranns9db6ada2019-11-08 12:42:31 +00001374 if (INDEX_INVALID == fib_index)
1375 rv = VNET_API_ERROR_NO_SUCH_FIB;
1376 else
1377 {
1378 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1379 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1380 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001381 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001382 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001383}
1384
1385static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001386vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001387{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001388 vl_api_ip_table_replace_end_reply_t *rmp;
1389 fib_protocol_t fproto;
1390 u32 fib_index;
1391 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001392
Neale Ranns9db6ada2019-11-08 12:42:31 +00001393 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1394 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1395
1396 if (INDEX_INVALID == fib_index)
1397 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001398 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001399 {
1400 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1401 mfib_table_sweep (mfib_table_find
1402 (fproto, ntohl (mp->table.table_id)), fproto,
1403 MFIB_SOURCE_API);
1404 }
1405 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1406}
Neale Rannsb8d44812017-11-10 06:53:54 -08001407
Neale Ranns9db6ada2019-11-08 12:42:31 +00001408static void
1409vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1410{
1411 vl_api_ip_table_flush_reply_t *rmp;
1412 fib_protocol_t fproto;
1413 u32 fib_index;
1414 int rv = 0;
1415
1416 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1417 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1418
1419 if (INDEX_INVALID == fib_index)
1420 rv = VNET_API_ERROR_NO_SUCH_FIB;
1421 else
1422 {
1423 vnet_main_t *vnm = vnet_get_main ();
1424 vnet_interface_main_t *im = &vnm->interface_main;
1425 vnet_sw_interface_t *si;
1426
1427 /* Shut down interfaces in this FIB / clean out intfc routes */
1428 /* *INDENT-OFF* */
1429 pool_foreach (si, im->sw_interfaces,
1430 ({
1431 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1432 si->sw_if_index))
1433 {
1434 u32 flags = si->flags;
1435 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1436 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1437 }
1438 }));
1439 /* *INDENT-ON* */
1440
1441 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1442 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1443 fproto, MFIB_SOURCE_API);
1444 }
1445
1446 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001447}
1448
Klement Sekera75e7d132017-09-20 08:26:30 +02001449void
1450vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1451{
1452 vl_api_ip_reassembly_set_reply_t *rmp;
1453 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001454 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001455 {
Klement Sekerade34c352019-06-25 11:19:22 +00001456 case IP_REASS_TYPE_FULL:
1457 if (mp->is_ip6)
1458 {
1459 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1460 clib_net_to_host_u32
1461 (mp->max_reassemblies),
1462 clib_net_to_host_u32
1463 (mp->max_reassembly_length),
1464 clib_net_to_host_u32
1465 (mp->expire_walk_interval_ms));
1466 }
1467 else
1468 {
1469 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1470 clib_net_to_host_u32
1471 (mp->max_reassemblies),
1472 clib_net_to_host_u32
1473 (mp->max_reassembly_length),
1474 clib_net_to_host_u32
1475 (mp->expire_walk_interval_ms));
1476 }
1477 break;
1478 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1479 if (mp->is_ip6)
1480 {
1481 rv =
1482 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1483 clib_net_to_host_u32 (mp->max_reassemblies),
1484 clib_net_to_host_u32
1485 (mp->max_reassembly_length),
1486 clib_net_to_host_u32
1487 (mp->expire_walk_interval_ms));
1488 }
1489 else
1490 {
1491 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1492 clib_net_to_host_u32 (mp->max_reassemblies),
1493 clib_net_to_host_u32
1494 (mp->max_reassembly_length),
1495 clib_net_to_host_u32
1496 (mp->expire_walk_interval_ms));
1497 }
1498 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001499 }
1500
1501 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1502}
1503
1504void
1505vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1506{
Ole Troan2e1c8962019-04-10 09:44:23 +02001507 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001508
Ole Troan2e1c8962019-04-10 09:44:23 +02001509 rp = vl_api_client_index_to_registration (mp->client_index);
1510 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001511 return;
1512
1513 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001514 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02001515 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
1516 rmp->context = mp->context;
1517 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001518 u32 timeout_ms;
1519 u32 max_reassemblies;
1520 u32 max_reassembly_length;
1521 u32 expire_walk_interval_ms;
1522 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001523 {
Klement Sekerade34c352019-06-25 11:19:22 +00001524 case IP_REASS_TYPE_FULL:
1525 if (mp->is_ip6)
1526 {
1527 rmp->is_ip6 = 1;
1528 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1529 &max_reassembly_length,
1530 &expire_walk_interval_ms);
1531 }
1532 else
1533 {
1534 rmp->is_ip6 = 0;
1535 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1536 &max_reassembly_length,
1537 &expire_walk_interval_ms);
1538 }
1539 break;
1540 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1541 if (mp->is_ip6)
1542 {
1543 rmp->is_ip6 = 1;
1544 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1545 &max_reassembly_length, &expire_walk_interval_ms);
1546 }
1547 else
1548 {
1549 rmp->is_ip6 = 0;
1550 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1551 &max_reassembly_length, &expire_walk_interval_ms);
1552 }
1553 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001554 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001555 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1556 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1557 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001558 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001559 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001560 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001561}
1562
Klement Sekera4c533132018-02-22 11:41:12 +01001563void
1564 vl_api_ip_reassembly_enable_disable_t_handler
1565 (vl_api_ip_reassembly_enable_disable_t * mp)
1566{
1567 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1568 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001569 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001570 {
Klement Sekerade34c352019-06-25 11:19:22 +00001571 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001572 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001573 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1574 mp->enable_ip4);
1575 if (0 == rv)
1576 rv =
1577 ip6_full_reass_enable_disable (clib_net_to_host_u32
1578 (mp->sw_if_index), mp->enable_ip6);
1579 break;
1580 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1581 rv =
1582 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1583 mp->enable_ip4);
1584 if (0 == rv)
1585 {
1586 rv =
1587 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1588 (mp->sw_if_index), mp->enable_ip6);
1589 }
1590 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001591 }
1592
Choree7f61d12018-11-28 10:27:58 +03301593 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001594}
1595
Neale Ranns92207752019-06-03 13:21:40 +00001596typedef struct ip_punt_redirect_walk_ctx_t_
Pavel Kotucek609e1212018-11-27 09:59:44 +01001597{
Neale Ranns92207752019-06-03 13:21:40 +00001598 vl_api_registration_t *reg;
1599 u32 context;
1600} ip_punt_redirect_walk_ctx_t;
1601
1602static walk_rc_t
1603send_ip_punt_redirect_details (u32 rx_sw_if_index,
1604 const ip_punt_redirect_rx_t * ipr, void *arg)
1605{
Neale Ranns92207752019-06-03 13:21:40 +00001606 ip_punt_redirect_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001607 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001608 fib_path_encode_ctx_t path_ctx = {
1609 .rpaths = NULL,
1610 };
Pavel Kotucek609e1212018-11-27 09:59:44 +01001611
1612 mp = vl_msg_api_alloc (sizeof (*mp));
1613 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001614 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001615
1616 clib_memset (mp, 0, sizeof (*mp));
1617 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001618 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001619
Neale Ranns097fa662018-05-01 05:17:55 -07001620 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001621
1622 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001623 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001624
Neale Ranns097fa662018-05-01 05:17:55 -07001625 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001626 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1627
1628 vl_api_send_msg (ctx->reg, (u8 *) mp);
1629
Neale Ranns097fa662018-05-01 05:17:55 -07001630 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001631
1632 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001633}
1634
1635static void
1636vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1637{
1638 vl_api_registration_t *reg;
Dave Barach77841402020-04-29 17:04:10 -04001639 fib_protocol_t fproto = FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001640
Pavel Kotucek609e1212018-11-27 09:59:44 +01001641 reg = vl_api_client_index_to_registration (mp->client_index);
1642 if (!reg)
1643 return;
1644
Dave Barach77841402020-04-29 17:04:10 -04001645 if (mp->is_ipv6 == 1)
1646 fproto = FIB_PROTOCOL_IP6;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001647
Neale Ranns92207752019-06-03 13:21:40 +00001648 ip_punt_redirect_walk_ctx_t ctx = {
1649 .reg = reg,
1650 .context = mp->context,
1651 };
1652
1653 if (~0 != mp->sw_if_index)
Pavel Kotucek609e1212018-11-27 09:59:44 +01001654 {
Neale Ranns92207752019-06-03 13:21:40 +00001655 u32 rx_sw_if_index;
1656 index_t pri;
1657
1658 rx_sw_if_index = ntohl (mp->sw_if_index);
1659 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1660
1661 if (INDEX_INVALID == pri)
1662 return;
1663
1664 send_ip_punt_redirect_details (rx_sw_if_index,
1665 ip_punt_redirect_get (pri), &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001666 }
1667 else
Neale Ranns92207752019-06-03 13:21:40 +00001668 ip_punt_redirect_walk (fproto, send_ip_punt_redirect_details, &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001669}
1670
Dave Barachb5e8a772016-12-06 12:04:42 -05001671#define vl_msg_name_crc_list
1672#include <vnet/ip/ip.api.h>
1673#undef vl_msg_name_crc_list
1674
1675static void
1676setup_message_id_table (api_main_t * am)
1677{
1678#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1679 foreach_vl_msg_name_crc_ip;
1680#undef _
1681}
1682
1683static clib_error_t *
1684ip_api_hookup (vlib_main_t * vm)
1685{
Dave Barach39d69112019-11-27 11:42:13 -05001686 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001687
1688#define _(N,n) \
1689 vl_msg_api_set_handlers(VL_API_##N, #n, \
1690 vl_api_##n##_t_handler, \
1691 vl_noop_handler, \
1692 vl_api_##n##_t_endian, \
1693 vl_api_##n##_t_print, \
1694 sizeof(vl_api_##n##_t), 1);
1695 foreach_ip_api_msg;
1696#undef _
1697
1698 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00001699 * Mark the route add/del API as MP safe
1700 */
Neale Ranns097fa662018-05-01 05:17:55 -07001701 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
1702 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
Neale Ranns6a231a12018-10-17 06:38:00 +00001703
1704 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05001705 * Set up the (msg_name, crc, message-id) table
1706 */
1707 setup_message_id_table (am);
1708
Dave Barachb5e8a772016-12-06 12:04:42 -05001709 return 0;
1710}
1711
1712VLIB_API_INIT_FUNCTION (ip_api_hookup);
1713
1714/*
1715 * fd.io coding-style-patch-verification: ON
1716 *
1717 * Local Variables:
1718 * eval: (c-set-style "gnu")
1719 * End:
1720 */