blob: 1d0dea864430d85ea5659d50457cf04f67ac4b7c [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>
Neale Rannse4031132020-10-26 13:00:06 +000048#include <vnet/ip/ip_container_proxy.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050049
50#include <vnet/vnet_msg_enum.h>
51
52#define vl_typedefs /* define message structures */
53#include <vnet/vnet_all_api_h.h>
54#undef vl_typedefs
55
56#define vl_endianfun /* define message structures */
57#include <vnet/vnet_all_api_h.h>
58#undef vl_endianfun
59
60/* instantiate all the print functions we know about */
61#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
62#define vl_printfun
63#include <vnet/vnet_all_api_h.h>
64#undef vl_printfun
65
66#include <vlibapi/api_helper_macros.h>
67
Ole Troan33a58172019-09-04 09:12:29 +020068#include <vnet/format_fns.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080069
Dave Barachb5e8a772016-12-06 12:04:42 -050070#define foreach_ip_api_msg \
Neale Rannscbe25aa2019-09-30 10:53:31 +000071_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable) \
Neale Ranns097fa662018-05-01 05:17:55 -070072_(IP_TABLE_DUMP, ip_table_dump) \
73_(IP_ROUTE_DUMP, ip_route_dump) \
74_(IP_MTABLE_DUMP, ip_mtable_dump) \
75_(IP_MROUTE_DUMP, ip_mroute_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000076_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080077_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050078_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070079_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050080_(IP_DUMP, ip_dump) \
Neale Ranns9db6ada2019-11-08 12:42:31 +000081_(IP_TABLE_REPLACE_BEGIN, ip_table_replace_begin) \
82_(IP_TABLE_REPLACE_END, ip_table_replace_end) \
83_(IP_TABLE_FLUSH, ip_table_flush) \
Neale Ranns097fa662018-05-01 05:17:55 -070084_(IP_ROUTE_ADD_DEL, ip_route_add_del) \
Christian Hoppsf5d38e02020-05-04 10:28:03 -040085_(IP_ROUTE_LOOKUP, ip_route_lookup) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070086_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070087_(IP_PUNT_POLICE, ip_punt_police) \
88_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050089_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
Neale Rannsb8d44812017-11-10 06:53:54 -080090_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
Matus Fabian75b9f452018-10-02 23:27:21 -070091_(IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080092_(IOAM_ENABLE, ioam_enable) \
93_(IOAM_DISABLE, ioam_disable) \
94_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
95 ip_source_and_port_range_check_add_del) \
96_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +020097 ip_source_and_port_range_check_interface_add_del) \
Neale Rannscbe25aa2019-09-30 10:53:31 +000098 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
99 sw_interface_ip6_set_link_local_address) \
Klement Sekera75e7d132017-09-20 08:26:30 +0200100_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100101_(IP_REASSEMBLY_GET, ip_reassembly_get) \
Pavel Kotucek609e1212018-11-27 09:59:44 +0100102_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
103_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
104
Dave Barachb5e8a772016-12-06 12:04:42 -0500105static void
Neale Rannscbe25aa2019-09-30 10:53:31 +0000106 vl_api_sw_interface_ip6_enable_disable_t_handler
107 (vl_api_sw_interface_ip6_enable_disable_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500108{
Neale Rannscbe25aa2019-09-30 10:53:31 +0000109 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
110 int rv = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500111
Neale Rannscbe25aa2019-09-30 10:53:31 +0000112 VALIDATE_SW_IF_INDEX (mp);
Neale Ranns37029302018-08-10 05:30:06 -0700113
Neale Rannscbe25aa2019-09-30 10:53:31 +0000114 rv = ((mp->enable == 1) ?
Neale Rannsec40a7d2020-04-23 07:36:12 +0000115 ip6_link_enable (ntohl (mp->sw_if_index), NULL) :
Neale Rannscbe25aa2019-09-30 10:53:31 +0000116 ip6_link_disable (ntohl (mp->sw_if_index)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500117
Neale Rannscbe25aa2019-09-30 10:53:31 +0000118 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500119
Neale Rannscbe25aa2019-09-30 10:53:31 +0000120 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -0500121}
122
Dave Barachb5e8a772016-12-06 12:04:42 -0500123static void
Neale Ranns097fa662018-05-01 05:17:55 -0700124send_ip_table_details (vpe_api_main_t * am,
125 vl_api_registration_t * reg,
126 u32 context, const fib_table_t * table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500127{
Neale Ranns097fa662018-05-01 05:17:55 -0700128 vl_api_ip_table_details_t *mp;
Dave Barachb5e8a772016-12-06 12:04:42 -0500129
Neale Ranns097fa662018-05-01 05:17:55 -0700130 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500131 if (!mp)
132 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400133 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700134 mp->_vl_msg_id = ntohs (VL_API_IP_TABLE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500135 mp->context = context;
136
Neale Ranns097fa662018-05-01 05:17:55 -0700137 mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
138 mp->table.table_id = htonl (table->ft_table_id);
139 memcpy (mp->table.name, table->ft_desc,
140 clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500141
Florin Coras6c4dae22018-01-09 06:39:23 -0800142 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500143}
144
Neale Ranns097fa662018-05-01 05:17:55 -0700145static void
146vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
147{
148 vpe_api_main_t *am = &vpe_api_main;
149 vl_api_registration_t *reg;
150 fib_table_t *fib_table;
151
152 reg = vl_api_client_index_to_registration (mp->client_index);
153 if (!reg)
154 return;
155
156 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100157 pool_foreach (fib_table, ip4_main.fibs)
158 {
Neale Ranns097fa662018-05-01 05:17:55 -0700159 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100160 }
161 pool_foreach (fib_table, ip6_main.fibs)
162 {
Neale Ranns097fa662018-05-01 05:17:55 -0700163 /* don't send link locals */
164 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
165 continue;
166 send_ip_table_details(am, reg, mp->context, fib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100167 }
Neale Ranns097fa662018-05-01 05:17:55 -0700168 /* *INDENT-ON* */
169}
170
Neale Rannsa3af3372017-03-28 03:49:52 -0700171typedef struct vl_api_ip_fib_dump_walk_ctx_t_
172{
173 fib_node_index_t *feis;
174} vl_api_ip_fib_dump_walk_ctx_t;
175
Neale Ranns89541992017-04-06 04:41:02 -0700176static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700177vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
178{
179 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
180
181 vec_add1 (ctx->feis, fei);
182
Neale Ranns89541992017-04-06 04:41:02 -0700183 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700184}
185
Dave Barachb5e8a772016-12-06 12:04:42 -0500186static void
Neale Ranns097fa662018-05-01 05:17:55 -0700187send_ip_route_details (vpe_api_main_t * am,
188 vl_api_registration_t * reg,
189 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500190{
Neale Ranns097fa662018-05-01 05:17:55 -0700191 fib_route_path_t *rpaths, *rpath;
192 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700193 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500194 vl_api_fib_path_t *fp;
195 int path_count;
196
Neale Ranns097fa662018-05-01 05:17:55 -0700197 rpaths = NULL;
198 pfx = fib_entry_get_prefix (fib_entry_index);
199 rpaths = fib_entry_encode (fib_entry_index);
200
201 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500202 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
203 if (!mp)
204 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400205 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700206 mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500207 mp->context = context;
208
Neale Ranns097fa662018-05-01 05:17:55 -0700209 ip_prefix_encode (pfx, &mp->route.prefix);
210 mp->route.table_id =
211 htonl (fib_table_get_table_id
212 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
213 mp->route.n_paths = path_count;
214 mp->route.stats_index =
215 htonl (fib_table_entry_get_stats_index
216 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500217
Neale Ranns097fa662018-05-01 05:17:55 -0700218 fp = mp->route.paths;
219 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500220 {
Neale Ranns097fa662018-05-01 05:17:55 -0700221 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500222 fp++;
223 }
224
Florin Coras6c4dae22018-01-09 06:39:23 -0800225 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000226 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500227}
228
Dave Barachd7cb1b52016-12-09 09:52:16 -0500229typedef struct apt_ip6_fib_show_ctx_t_
230{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500231 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500232} api_ip6_fib_show_ctx_t;
233
Dave Barachb5e8a772016-12-06 12:04:42 -0500234static void
Neale Ranns097fa662018-05-01 05:17:55 -0700235vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500236{
237 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500238 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800239 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700240 fib_protocol_t fproto;
241 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500242
Florin Coras6c4dae22018-01-09 06:39:23 -0800243 reg = vl_api_client_index_to_registration (mp->client_index);
244 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500245 return;
246
Neale Ranns097fa662018-05-01 05:17:55 -0700247 vl_api_ip_fib_dump_walk_ctx_t ctx = {
248 .feis = NULL,
249 };
Neale Ranns81458422018-03-12 06:59:36 -0700250
Neale Ranns097fa662018-05-01 05:17:55 -0700251 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
252 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
253
254 if (INDEX_INVALID == fib_index)
255 return;
256
257 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
258
259 vec_foreach (fib_entry_index, ctx.feis)
260 {
261 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
262 }
263
264 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500265}
266
267static void
Neale Ranns097fa662018-05-01 05:17:55 -0700268send_ip_mtable_details (vl_api_registration_t * reg,
269 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800270{
Neale Ranns097fa662018-05-01 05:17:55 -0700271 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800272
Neale Ranns097fa662018-05-01 05:17:55 -0700273 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800274 if (!mp)
275 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700276 memset (mp, 0, sizeof (*mp));
277 mp->_vl_msg_id = ntohs (VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800278 mp->context = context;
279
Neale Ranns097fa662018-05-01 05:17:55 -0700280 mp->table.table_id = htonl (mfib_table->mft_table_id);
281 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800282
Florin Coras6c4dae22018-01-09 06:39:23 -0800283 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800284}
285
Neale Ranns5a8123b2017-01-26 01:18:23 -0800286static void
Neale Ranns097fa662018-05-01 05:17:55 -0700287vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800288{
Florin Coras6c4dae22018-01-09 06:39:23 -0800289 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800290 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800291
Florin Coras6c4dae22018-01-09 06:39:23 -0800292 reg = vl_api_client_index_to_registration (mp->client_index);
293 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800294 return;
295
Neale Ranns5a8123b2017-01-26 01:18:23 -0800296 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100297 pool_foreach (mfib_table, ip4_main.mfibs)
298 {
Neale Ranns097fa662018-05-01 05:17:55 -0700299 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100300 }
301 pool_foreach (mfib_table, ip6_main.mfibs)
302 {
Neale Ranns097fa662018-05-01 05:17:55 -0700303 send_ip_mtable_details (reg, mp->context, mfib_table);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100304 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800305 /* *INDENT-ON* */
Neale Ranns5a8123b2017-01-26 01:18:23 -0800306}
307
Neale Ranns097fa662018-05-01 05:17:55 -0700308typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800309{
310 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700311} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800312
Neale Ranns9db6ada2019-11-08 12:42:31 +0000313static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700314mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800315{
Neale Ranns097fa662018-05-01 05:17:55 -0700316 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800317
318 vec_add1 (ctx->entries, fei);
319
Neale Ranns9db6ada2019-11-08 12:42:31 +0000320 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800321}
322
323static void
Neale Ranns097fa662018-05-01 05:17:55 -0700324send_ip_mroute_details (vpe_api_main_t * am,
325 vl_api_registration_t * reg,
326 u32 context, fib_node_index_t mfib_entry_index)
327{
328 fib_route_path_t *rpaths, *rpath;
329 vl_api_ip_mroute_details_t *mp;
330 const mfib_prefix_t *pfx;
331 vl_api_mfib_path_t *fp;
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500332 u8 path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700333
334 rpaths = NULL;
335 pfx = mfib_entry_get_prefix (mfib_entry_index);
336 rpaths = mfib_entry_encode (mfib_entry_index);
337
338 path_count = vec_len (rpaths);
339 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
340 if (!mp)
341 return;
342 clib_memset (mp, 0, sizeof (*mp));
343 mp->_vl_msg_id = ntohs (VL_API_IP_MROUTE_DETAILS);
344 mp->context = context;
345
346 ip_mprefix_encode (pfx, &mp->route.prefix);
347 mp->route.table_id =
348 htonl (mfib_table_get_table_id
349 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
Matthew Smitha2bbeb82020-09-17 16:33:10 -0500350 mp->route.n_paths = path_count;
Neale Ranns097fa662018-05-01 05:17:55 -0700351 fp = mp->route.paths;
352 vec_foreach (rpath, rpaths)
353 {
354 mfib_api_path_encode (rpath, fp);
355 fp++;
356 }
357
358 vl_api_send_msg (reg, (u8 *) mp);
359 vec_free (rpaths);
360}
361
362static void
363vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800364{
365 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800366 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800367 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700368 fib_protocol_t fproto;
369 u32 fib_index;
370
371 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800372 .entries = NULL,
373 };
374
Florin Coras6c4dae22018-01-09 06:39:23 -0800375 reg = vl_api_client_index_to_registration (mp->client_index);
376 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800377 return;
378
Neale Ranns097fa662018-05-01 05:17:55 -0700379 fproto = fib_ip_proto (mp->table.is_ip6);
380 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800381
Neale Ranns097fa662018-05-01 05:17:55 -0700382 if (INDEX_INVALID == fib_index)
383 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800384
Neale Ranns097fa662018-05-01 05:17:55 -0700385 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800386
Neale Ranns097fa662018-05-01 05:17:55 -0700387 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800388
Neale Ranns097fa662018-05-01 05:17:55 -0700389 vec_foreach (mfeip, ctx.entries)
390 {
391 send_ip_mroute_details (am, reg, mp->context, *mfeip);
392 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800393
394 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800395}
396
397static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700398vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
399 vlib_main_t * vm)
400{
401 vl_api_ip_punt_police_reply_t *rmp;
402 int rv = 0;
403
404 if (mp->is_ip6)
405 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
406 else
407 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
408
409 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
410}
411
412static void
413vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
414 vlib_main_t * vm)
415{
416 vl_api_ip_punt_redirect_reply_t *rmp;
417 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100418 ip46_type_t ipv;
419 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700420
Pavel Kotucek609e1212018-11-27 09:59:44 +0100421 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
422 goto bad_sw_if_index;
423
424 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700425 if (mp->is_add)
426 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100427 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700428 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100429 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
430 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700431 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100432 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700433 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100434 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
435 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700436 }
437 }
438 else
439 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100440 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700441 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100442 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700443 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100444 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700445 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100446 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700447 }
448 }
449
Pavel Kotucek609e1212018-11-27 09:59:44 +0100450 BAD_SW_IF_INDEX_LABEL;
451
Neale Rannsd91c1db2017-07-31 02:30:50 -0700452 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
453}
454
Steven Luongc3ed1c92020-07-27 10:06:58 -0700455static clib_error_t *
456call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
457 u32 flags,
458 _vnet_ip_table_function_list_elt_t **
459 elts)
460{
461 _vnet_ip_table_function_list_elt_t *elt;
462 vnet_ip_table_function_priority_t prio;
463 clib_error_t *error = 0;
464
465 for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
466 prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
467 {
468 elt = elts[prio];
469
470 while (elt)
471 {
472 error = elt->fp (vnm, table_id, flags);
473 if (error)
474 return error;
475 elt = elt->next_ip_table_function;
476 }
477 }
478 return error;
479}
480
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700481void
Neale Ranns15002542017-09-10 04:39:11 -0700482ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
483{
484 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700485 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700486
487 /*
488 * ignore action on the default table - this is always present
489 * and cannot be added nor deleted from the API
490 */
491 if (0 != table_id)
492 {
493 /*
494 * The API holds only one lock on the table.
495 * i.e. it can be added many times via the API but needs to be
496 * deleted only once.
497 * The FIB index for unicast and multicast is not necessarily the
498 * same, since internal VPP systesm (like LISP and SR) create
499 * their own unicast tables.
500 */
501 fib_index = fib_table_find (fproto, table_id);
502 mfib_index = mfib_table_find (fproto, table_id);
503
Steven Luongc3ed1c92020-07-27 10:06:58 -0700504 if ((~0 != fib_index) || (~0 != mfib_index))
505 call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
506 vnm->ip_table_add_del_functions);
507
Neale Ranns15002542017-09-10 04:39:11 -0700508 if (~0 != fib_index)
509 {
510 fib_table_unlock (fib_index, fproto,
511 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
512 }
513 if (~0 != mfib_index)
514 {
515 mfib_table_unlock (mfib_index, fproto,
516 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
517 }
518 }
519}
520
521void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700522vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
523{
524 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700525 fib_protocol_t fproto = (mp->table.is_ip6 ?
526 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
527 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700528 int rv = 0;
529
Neale Ranns15002542017-09-10 04:39:11 -0700530 if (mp->is_add)
531 {
Neale Ranns097fa662018-05-01 05:17:55 -0700532 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700533 }
534 else
535 {
536 ip_table_delete (fproto, table_id, 1);
537 }
538
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700539 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
540}
541
Dave Barachb5e8a772016-12-06 12:04:42 -0500542static int
Neale Ranns097fa662018-05-01 05:17:55 -0700543ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500544{
Neale Ranns097fa662018-05-01 05:17:55 -0700545 fib_route_path_t *rpaths = NULL, *rpath;
546 fib_entry_flag_t entry_flags;
547 vl_api_fib_path_t *apath;
548 fib_prefix_t pfx;
549 u32 fib_index;
550 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500551
Neale Ranns097fa662018-05-01 05:17:55 -0700552 entry_flags = FIB_ENTRY_FLAG_NONE;
553 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500554
Neale Ranns097fa662018-05-01 05:17:55 -0700555 rv = fib_api_table_id_decode (pfx.fp_proto,
556 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500557 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700558 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500559
Neale Rannsc2ac2352019-07-02 14:33:29 +0000560 if (0 != mp->route.n_paths)
561 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700562
563 for (ii = 0; ii < mp->route.n_paths; ii++)
564 {
565 apath = &mp->route.paths[ii];
566 rpath = &rpaths[ii];
567
568 rv = fib_api_path_decode (apath, rpath);
569
570 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
571 (~0 == rpath->frp_sw_if_index))
572 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
573
574 if (0 != rv)
575 goto out;
576 }
577
Neale Rannsc2ac2352019-07-02 14:33:29 +0000578 rv = fib_api_route_add_del (mp->is_add,
579 mp->is_multipath,
580 fib_index, &pfx, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700581
582 if (mp->is_add && 0 == rv)
583 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
584
Neale Ranns097fa662018-05-01 05:17:55 -0700585out:
586 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700587
588 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500589}
590
591void
Neale Ranns097fa662018-05-01 05:17:55 -0700592vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500593{
Neale Ranns097fa662018-05-01 05:17:55 -0700594 vl_api_ip_route_add_del_reply_t *rmp;
595 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500596 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500597
Neale Ranns097fa662018-05-01 05:17:55 -0700598 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500599
Neale Ranns008dbe12018-09-07 09:32:36 -0700600 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700601 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700602 ({
603 rmp->stats_index = htonl (stats_index);
604 }))
605 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500606}
607
Neale Ranns15002542017-09-10 04:39:11 -0700608void
Christian Hoppsf5d38e02020-05-04 10:28:03 -0400609vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
610{
611 vl_api_ip_route_lookup_reply_t *rmp = NULL;
612 fib_route_path_t *rpaths = NULL, *rpath;
613 const fib_prefix_t *pfx = NULL;
614 fib_prefix_t lookup;
615 vl_api_fib_path_t *fp;
616 fib_node_index_t fib_entry_index;
617 u32 fib_index;
618 int npaths = 0;
619 int rv;
620
621 ip_prefix_decode (&mp->prefix, &lookup);
622 rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
623 &fib_index);
624 if (PREDICT_TRUE (!rv))
625 {
626 if (mp->exact)
627 fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
628 else
629 fib_entry_index = fib_table_lookup (fib_index, &lookup);
630 if (fib_entry_index == FIB_NODE_INDEX_INVALID)
631 rv = VNET_API_ERROR_NO_SUCH_ENTRY;
632 else
633 {
634 pfx = fib_entry_get_prefix (fib_entry_index);
635 rpaths = fib_entry_encode (fib_entry_index);
636 npaths = vec_len (rpaths);
637 }
638 }
639
640 /* *INDENT-OFF* */
641 REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
642 npaths * sizeof (*fp),
643 ({
644 if (!rv)
645 {
646 ip_prefix_encode (pfx, &rmp->route.prefix);
647 rmp->route.table_id = mp->table_id;
648 rmp->route.n_paths = npaths;
649 rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
650 rmp->route.stats_index = htonl (rmp->route.stats_index);
651
652 fp = rmp->route.paths;
653 vec_foreach (rpath, rpaths)
654 {
655 fib_api_path_encode (rpath, fp);
656 fp++;
657 }
658 }
659 }));
660 /* *INDENT-ON* */
661 vec_free (rpaths);
662}
663
664void
Neale Ranns2297af02017-09-12 09:45:04 -0700665ip_table_create (fib_protocol_t fproto,
666 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700667{
668 u32 fib_index, mfib_index;
Steven Luongc3ed1c92020-07-27 10:06:58 -0700669 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns15002542017-09-10 04:39:11 -0700670
671 /*
672 * ignore action on the default table - this is always present
673 * and cannot be added nor deleted from the API
674 */
675 if (0 != table_id)
676 {
677 /*
678 * The API holds only one lock on the table.
679 * i.e. it can be added many times via the API but needs to be
680 * deleted only once.
681 * The FIB index for unicast and multicast is not necessarily the
682 * same, since internal VPP systesm (like LISP and SR) create
683 * their own unicast tables.
684 */
685 fib_index = fib_table_find (fproto, table_id);
686 mfib_index = mfib_table_find (fproto, table_id);
687
688 if (~0 == fib_index)
689 {
Neale Ranns2297af02017-09-12 09:45:04 -0700690 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
691 (is_api ?
692 FIB_SOURCE_API :
693 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700694 }
695 if (~0 == mfib_index)
696 {
Neale Ranns2297af02017-09-12 09:45:04 -0700697 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
698 (is_api ?
699 MFIB_SOURCE_API :
700 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700701 }
Steven Luongc3ed1c92020-07-27 10:06:58 -0700702
703 if ((~0 == fib_index) || (~0 == mfib_index))
704 call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
705 vnm->ip_table_add_del_functions);
Neale Ranns15002542017-09-10 04:39:11 -0700706 }
707}
708
Neale Ranns097fa662018-05-01 05:17:55 -0700709static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000710mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700711 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000712 u32 fib_index,
713 const mfib_prefix_t * prefix,
714 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700715 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000716{
Neale Ranns097fa662018-05-01 05:17:55 -0700717 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700718
Neale Ranns097fa662018-05-01 05:17:55 -0700719 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000720 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700721 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
722 MFIB_SOURCE_API,
723 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000724 }
725 else
726 {
Neale Ranns097fa662018-05-01 05:17:55 -0700727 if (is_add)
728 {
729 mfib_entry_index =
730 mfib_table_entry_paths_update (fib_index, prefix,
731 MFIB_SOURCE_API, rpaths);
732 }
733 else
734 {
735 mfib_table_entry_paths_remove (fib_index, prefix,
736 MFIB_SOURCE_API, rpaths);
737 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000738 }
739
Neale Ranns28c142e2018-09-07 09:37:07 -0700740 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000741}
742
743static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700744api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
745 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000746{
Neale Ranns097fa662018-05-01 05:17:55 -0700747 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -0700748 fib_node_index_t mfib_entry_index;
Neale Ranns990f6942020-10-20 07:20:17 +0000749 mfib_entry_flags_t eflags;
Neale Ranns097fa662018-05-01 05:17:55 -0700750 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +0000751 u32 fib_index;
752 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -0700753 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +0000754
Neale Ranns097fa662018-05-01 05:17:55 -0700755 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000756
Neale Ranns097fa662018-05-01 05:17:55 -0700757 rv = mfib_api_table_id_decode (pfx.fp_proto,
758 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000759 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700760 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000761
Neale Ranns097fa662018-05-01 05:17:55 -0700762 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +0000763
Neale Ranns097fa662018-05-01 05:17:55 -0700764 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000765 {
Neale Ranns097fa662018-05-01 05:17:55 -0700766 rpath = &rpaths[ii];
767
768 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
769
770 if (0 != rv)
771 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000772 }
773
Neale Ranns990f6942020-10-20 07:20:17 +0000774 eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
Neale Ranns28c142e2018-09-07 09:37:07 -0700775 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700776 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -0700777 fib_index, &pfx,
Neale Ranns990f6942020-10-20 07:20:17 +0000778 eflags,
Neale Ranns097fa662018-05-01 05:17:55 -0700779 ntohl (mp->route.rpf_id),
780 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -0700781
782 if (~0 != mfib_entry_index)
783 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
784
Neale Ranns097fa662018-05-01 05:17:55 -0700785out:
Neale Ranns28c142e2018-09-07 09:37:07 -0700786 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +0000787}
788
789void
790vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
791{
792 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700793 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000794 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +0000795
Neale Ranns28c142e2018-09-07 09:37:07 -0700796 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000797
Neale Ranns28c142e2018-09-07 09:37:07 -0700798 /* *INDENT-OFF* */
799 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
800 ({
801 rmp->stats_index = htonl (stats_index);
802 }));
803 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +0000804}
805
Dave Barachb5e8a772016-12-06 12:04:42 -0500806static void
807send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800808 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
809 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500810{
811 vl_api_ip_details_t *mp;
812
813 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400814 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500815 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
816
817 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -0600818 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -0500819 mp->context = context;
820
Florin Coras6c4dae22018-01-09 06:39:23 -0800821 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500822}
823
824static void
825send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800826 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -0700827 const fib_prefix_t * pfx,
828 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500829{
830 vl_api_ip_address_details_t *mp;
831
832 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400833 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500834 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
835
Neale Ranns097fa662018-05-01 05:17:55 -0700836 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -0500837 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -0600838 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500839
Florin Coras6c4dae22018-01-09 06:39:23 -0800840 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500841}
842
843static void
844vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
845{
846 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800847 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500848 ip6_main_t *im6 = &ip6_main;
849 ip4_main_t *im4 = &ip4_main;
850 ip_lookup_main_t *lm6 = &im6->lookup_main;
851 ip_lookup_main_t *lm4 = &im4->lookup_main;
852 ip_interface_address_t *ia = 0;
853 u32 sw_if_index = ~0;
854 int rv __attribute__ ((unused)) = 0;
855
856 VALIDATE_SW_IF_INDEX (mp);
857
858 sw_if_index = ntohl (mp->sw_if_index);
859
Florin Coras6c4dae22018-01-09 06:39:23 -0800860 reg = vl_api_client_index_to_registration (mp->client_index);
861 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500862 return;
863
Dave Barachb5e8a772016-12-06 12:04:42 -0500864 if (mp->is_ipv6)
865 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500866 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -0700867 /* Do not send subnet details of the IP-interface for
868 * unnumbered interfaces. otherwise listening clients
869 * will be confused that the subnet is applied on more
870 * than one interface */
871 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -0500872 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700873 fib_prefix_t pfx = {
874 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
875 .fp_len = ia->address_length,
876 .fp_proto = FIB_PROTOCOL_IP6,
877 };
878 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500879 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500880 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500881 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500882 else
883 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500884 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -0700885 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -0500886 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700887 fib_prefix_t pfx = {
888 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
889 .fp_len = ia->address_length,
890 .fp_proto = FIB_PROTOCOL_IP4,
891 };
892
893 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500894 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500895 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500896 }
Neale Ranns008dbe12018-09-07 09:32:36 -0700897
Dave Barachb5e8a772016-12-06 12:04:42 -0500898 BAD_SW_IF_INDEX_LABEL;
899}
900
901static void
Neale Ranns9e2f9152018-05-18 02:27:10 -0700902send_ip_unnumbered_details (vpe_api_main_t * am,
903 vl_api_registration_t * reg,
904 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
905{
906 vl_api_ip_unnumbered_details_t *mp;
907
908 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400909 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -0700910 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
911
912 mp->context = context;
913 mp->sw_if_index = htonl (sw_if_index);
914 mp->ip_sw_if_index = htonl (ip_sw_if_index);
915
916 vl_api_send_msg (reg, (u8 *) mp);
917}
918
919static void
920vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
921{
922 vnet_main_t *vnm = vnet_get_main ();
923 vnet_interface_main_t *im = &vnm->interface_main;
924 int rv __attribute__ ((unused)) = 0;
925 vpe_api_main_t *am = &vpe_api_main;
926 vl_api_registration_t *reg;
927 vnet_sw_interface_t *si;
928 u32 sw_if_index;
929
930 sw_if_index = ntohl (mp->sw_if_index);
931
932 reg = vl_api_client_index_to_registration (mp->client_index);
933 if (!reg)
934 return;
935
936 if (~0 != sw_if_index)
937 {
938 VALIDATE_SW_IF_INDEX (mp);
939
940 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
941
Neale Rannsac3e72c2019-10-06 01:04:26 -0700942 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -0700943 {
944 send_ip_unnumbered_details (am, reg,
945 sw_if_index,
946 si->unnumbered_sw_if_index,
947 mp->context);
948 }
949 }
950 else
951 {
952 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100953 pool_foreach (si, im->sw_interfaces)
954 {
Neale Ranns9e2f9152018-05-18 02:27:10 -0700955 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
956 {
957 send_ip_unnumbered_details(am, reg,
958 si->sw_if_index,
959 si->unnumbered_sw_if_index,
960 mp->context);
961 }
Damjan Marionb2c31b62020-12-13 21:47:40 +0100962 }
Neale Ranns9e2f9152018-05-18 02:27:10 -0700963 /* *INDENT-ON* */
964 }
965
966 BAD_SW_IF_INDEX_LABEL;
967}
968
969static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500970vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
971{
972 vpe_api_main_t *am = &vpe_api_main;
973 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +0000974 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500975 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800976 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500977 vnet_sw_interface_t *si, *sorted_sis;
978 u32 sw_if_index = ~0;
979
Florin Coras6c4dae22018-01-09 06:39:23 -0800980 reg = vl_api_client_index_to_registration (mp->client_index);
981 if (!reg)
982 return;
Dave Barachb5e8a772016-12-06 12:04:42 -0500983
984 /* Gather interfaces. */
985 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
986 _vec_len (sorted_sis) = 0;
987 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100988 pool_foreach (si, im->sw_interfaces)
989 {
Dave Barachb5e8a772016-12-06 12:04:42 -0500990 vec_add1 (sorted_sis, si[0]);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100991 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500992 /* *INDENT-ON* */
993
994 vec_foreach (si, sorted_sis)
995 {
996 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
997 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000998 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
999 /* { */
1000 /* continue; */
1001 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -05001002 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001003 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001004 }
1005 }
Matthew Smith6d5f6592019-09-18 13:51:46 -05001006
1007 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -05001008}
1009
1010static void
1011set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1012{
1013 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001014 int rv;
1015 u32 table_id;
1016 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001017
Neale Ranns227038a2017-04-21 01:07:59 -07001018 table_id = ntohl (mp->vrf_id);
1019
1020#define _(a,b) if (mp->a) flow_hash_config |= b;
1021 foreach_flow_hash_bit;
1022#undef _
1023
1024 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001025
1026 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1027}
1028
1029static void
1030set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1031{
1032 vl_api_set_ip_flow_hash_reply_t *rmp;
1033 int rv;
1034 u32 table_id;
1035 flow_hash_config_t flow_hash_config = 0;
1036
1037 table_id = ntohl (mp->vrf_id);
1038
1039#define _(a,b) if (mp->a) flow_hash_config |= b;
1040 foreach_flow_hash_bit;
1041#undef _
1042
1043 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1044
1045 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1046}
1047
1048
1049static void
1050vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1051{
1052 if (mp->is_ipv6 == 0)
1053 set_ip4_flow_hash (mp);
1054 else
1055 set_ip6_flow_hash (mp);
1056}
1057
Neale Ranns32e1c012016-11-22 17:07:28 +00001058void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001059vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001060 u32 context, const mfib_signal_t * mfs)
1061{
1062 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001063 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001064 mfib_table_t *mfib;
1065 mfib_itf_t *mfi;
1066
1067 mp = vl_msg_api_alloc (sizeof (*mp));
1068
Dave Barachb7b92992018-10-17 10:38:51 -04001069 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001070 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1071 mp->context = context;
1072
1073 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001074 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001075 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001076 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001077 mp->table_id = ntohl (mfib->mft_table_id);
1078 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1079
Neale Ranns097fa662018-05-01 05:17:55 -07001080 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +00001081
1082 if (0 != mfs->mfs_buffer_len)
1083 {
1084 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1085
1086 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1087 }
1088 else
1089 {
1090 mp->ip_packet_len = 0;
1091 }
1092
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001093 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001094}
1095
1096static void
1097vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1098{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001099 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001100
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001101 reg = vl_api_client_index_to_registration (mp->client_index);
1102 if (!reg)
1103 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001104
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001105 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001106 ;
1107}
Dave Barachb5e8a772016-12-06 12:04:42 -05001108
Florin Coras595992c2017-11-06 17:17:08 -08001109static void
1110 vl_api_ip_container_proxy_add_del_t_handler
1111 (vl_api_ip_container_proxy_add_del_t * mp)
1112{
1113 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1114 vnet_ip_container_proxy_args_t args;
1115 int rv = 0;
1116 clib_error_t *error;
1117
Dave Barachb7b92992018-10-17 10:38:51 -04001118 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001119
1120 ip_prefix_decode (&mp->pfx, &args.prefix);
1121
Florin Coras595992c2017-11-06 17:17:08 -08001122 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1123 args.is_add = mp->is_add;
1124 if ((error = vnet_ip_container_proxy_add_del (&args)))
1125 {
1126 rv = clib_error_get_code (error);
1127 clib_error_report (error);
1128 }
1129
1130 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1131}
1132
Matus Fabian75b9f452018-10-02 23:27:21 -07001133typedef struct ip_container_proxy_walk_ctx_t_
1134{
1135 vl_api_registration_t *reg;
1136 u32 context;
1137} ip_container_proxy_walk_ctx_t;
1138
1139static int
1140ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1141 void *args)
1142{
1143 vl_api_ip_container_proxy_details_t *mp;
1144 ip_container_proxy_walk_ctx_t *ctx = args;
1145
1146 mp = vl_msg_api_alloc (sizeof (*mp));
1147 if (!mp)
1148 return 1;
1149
Dave Barachb7b92992018-10-17 10:38:51 -04001150 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001151 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1152 mp->context = ctx->context;
1153
1154 mp->sw_if_index = ntohl (sw_if_index);
1155 ip_prefix_encode (pfx, &mp->prefix);
1156
1157 vl_api_send_msg (ctx->reg, (u8 *) mp);
1158
1159 return 1;
1160}
1161
1162static void
1163vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1164 mp)
1165{
1166 vl_api_registration_t *reg;
1167
1168 reg = vl_api_client_index_to_registration (mp->client_index);
1169 if (!reg)
1170 return;
1171
1172 ip_container_proxy_walk_ctx_t ctx = {
1173 .context = mp->context,
1174 .reg = reg,
1175 };
1176
1177 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1178}
1179
Neale Rannsb8d44812017-11-10 06:53:54 -08001180static void
1181vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1182{
1183 int rv = 0;
1184 vl_api_ioam_enable_reply_t *rmp;
1185 clib_error_t *error;
1186
1187 /* Ignoring the profile id as currently a single profile
1188 * is supported */
1189 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1190 mp->seqno, mp->analyse);
1191 if (error)
1192 {
1193 clib_error_report (error);
1194 rv = clib_error_get_code (error);
1195 }
1196
1197 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1198}
1199
1200static void
1201vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1202{
1203 int rv = 0;
1204 vl_api_ioam_disable_reply_t *rmp;
1205 clib_error_t *error;
1206
1207 error = clear_ioam_rewrite_fn ();
1208 if (error)
1209 {
1210 clib_error_report (error);
1211 rv = clib_error_get_code (error);
1212 }
1213
1214 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1215}
1216
1217static void
1218 vl_api_ip_source_and_port_range_check_add_del_t_handler
1219 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1220{
1221 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1222 int rv = 0;
1223
Neale Rannsb8d44812017-11-10 06:53:54 -08001224 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001225 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001226 u16 *low_ports = 0;
1227 u16 *high_ports = 0;
1228 u32 vrf_id;
1229 u16 tmp_low, tmp_high;
1230 u8 num_ranges;
1231 int i;
1232
Neale Ranns37029302018-08-10 05:30:06 -07001233 ip_prefix_decode (&mp->prefix, &pfx);
1234
Neale Rannsb8d44812017-11-10 06:53:54 -08001235 // Validate port range
1236 num_ranges = mp->number_of_ranges;
1237 if (num_ranges > 32)
1238 { // This is size of array in VPE.API
1239 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1240 goto reply;
1241 }
1242
1243 vec_reset_length (low_ports);
1244 vec_reset_length (high_ports);
1245
1246 for (i = 0; i < num_ranges; i++)
1247 {
1248 tmp_low = mp->low_ports[i];
1249 tmp_high = mp->high_ports[i];
1250 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1251 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1252 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1253 {
1254 rv = VNET_API_ERROR_INVALID_VALUE;
1255 goto reply;
1256 }
1257 vec_add1 (low_ports, tmp_low);
1258 vec_add1 (high_ports, tmp_high + 1);
1259 }
1260
Neale Rannsb8d44812017-11-10 06:53:54 -08001261 vrf_id = ntohl (mp->vrf_id);
1262
1263 if (vrf_id < 1)
1264 {
1265 rv = VNET_API_ERROR_INVALID_VALUE;
1266 goto reply;
1267 }
1268
1269
Neale Ranns37029302018-08-10 05:30:06 -07001270 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001271 {
Neale Ranns37029302018-08-10 05:30:06 -07001272 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1273 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001274 vrf_id,
1275 low_ports,
1276 high_ports, is_add);
1277 }
1278 else
1279 {
Neale Ranns37029302018-08-10 05:30:06 -07001280 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1281 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001282 vrf_id,
1283 low_ports,
1284 high_ports, is_add);
1285 }
1286
1287reply:
1288 vec_free (low_ports);
1289 vec_free (high_ports);
1290 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1291}
1292
1293static void
1294 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1295 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1296{
1297 vlib_main_t *vm = vlib_get_main ();
1298 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1299 ip4_main_t *im = &ip4_main;
1300 int rv;
1301 u32 sw_if_index;
1302 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1303 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1304 uword *p = 0;
1305 int i;
1306
1307 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1308 ntohl (mp->tcp_out_vrf_id);
1309 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1310 ntohl (mp->udp_out_vrf_id);
1311 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1312 ntohl (mp->tcp_in_vrf_id);
1313 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1314 ntohl (mp->udp_in_vrf_id);
1315
1316
1317 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1318 {
1319 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1320 {
1321 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1322
1323 if (p == 0)
1324 {
1325 rv = VNET_API_ERROR_INVALID_VALUE;
1326 goto reply;
1327 }
1328
1329 fib_index[i] = p[0];
1330 }
1331 else
1332 fib_index[i] = ~0;
1333 }
1334 sw_if_index = ntohl (mp->sw_if_index);
1335
1336 VALIDATE_SW_IF_INDEX (mp);
1337
1338 rv =
1339 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1340 mp->is_add);
1341
1342 BAD_SW_IF_INDEX_LABEL;
1343reply:
1344
1345 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1346}
1347
Neale Rannscbe25aa2019-09-30 10:53:31 +00001348static void
1349 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1350 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1351{
1352 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1353 ip6_address_t ip;
1354 int rv;
1355
1356 VALIDATE_SW_IF_INDEX (mp);
1357
1358 ip6_address_decode (mp->ip, &ip);
1359
Neale Rannsec40a7d2020-04-23 07:36:12 +00001360 rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
Neale Rannscbe25aa2019-09-30 10:53:31 +00001361
1362 BAD_SW_IF_INDEX_LABEL;
1363 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1364}
1365
Neale Ranns9db6ada2019-11-08 12:42:31 +00001366static void
1367vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001368{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001369 vl_api_ip_table_replace_begin_reply_t *rmp;
1370 fib_protocol_t fproto;
1371 u32 fib_index;
1372 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001373
Neale Ranns9db6ada2019-11-08 12:42:31 +00001374 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1375 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001376
Neale Ranns9db6ada2019-11-08 12:42:31 +00001377 if (INDEX_INVALID == fib_index)
1378 rv = VNET_API_ERROR_NO_SUCH_FIB;
1379 else
1380 {
1381 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1382 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1383 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001384 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001385 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001386}
1387
1388static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001389vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001390{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001391 vl_api_ip_table_replace_end_reply_t *rmp;
1392 fib_protocol_t fproto;
1393 u32 fib_index;
1394 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001395
Neale Ranns9db6ada2019-11-08 12:42:31 +00001396 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1397 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1398
1399 if (INDEX_INVALID == fib_index)
1400 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001401 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001402 {
1403 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1404 mfib_table_sweep (mfib_table_find
1405 (fproto, ntohl (mp->table.table_id)), fproto,
1406 MFIB_SOURCE_API);
1407 }
1408 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1409}
Neale Rannsb8d44812017-11-10 06:53:54 -08001410
Neale Ranns9db6ada2019-11-08 12:42:31 +00001411static void
1412vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1413{
1414 vl_api_ip_table_flush_reply_t *rmp;
1415 fib_protocol_t fproto;
1416 u32 fib_index;
1417 int rv = 0;
1418
1419 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1420 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1421
1422 if (INDEX_INVALID == fib_index)
1423 rv = VNET_API_ERROR_NO_SUCH_FIB;
1424 else
1425 {
1426 vnet_main_t *vnm = vnet_get_main ();
1427 vnet_interface_main_t *im = &vnm->interface_main;
1428 vnet_sw_interface_t *si;
1429
1430 /* Shut down interfaces in this FIB / clean out intfc routes */
1431 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001432 pool_foreach (si, im->sw_interfaces)
1433 {
Neale Ranns9db6ada2019-11-08 12:42:31 +00001434 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1435 si->sw_if_index))
1436 {
1437 u32 flags = si->flags;
1438 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1439 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1440 }
Damjan Marionb2c31b62020-12-13 21:47:40 +01001441 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001442 /* *INDENT-ON* */
1443
1444 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1445 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1446 fproto, MFIB_SOURCE_API);
1447 }
1448
1449 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001450}
1451
Klement Sekera75e7d132017-09-20 08:26:30 +02001452void
1453vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1454{
1455 vl_api_ip_reassembly_set_reply_t *rmp;
1456 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001457 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001458 {
Klement Sekerade34c352019-06-25 11:19:22 +00001459 case IP_REASS_TYPE_FULL:
1460 if (mp->is_ip6)
1461 {
1462 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1463 clib_net_to_host_u32
1464 (mp->max_reassemblies),
1465 clib_net_to_host_u32
1466 (mp->max_reassembly_length),
1467 clib_net_to_host_u32
1468 (mp->expire_walk_interval_ms));
1469 }
1470 else
1471 {
1472 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1473 clib_net_to_host_u32
1474 (mp->max_reassemblies),
1475 clib_net_to_host_u32
1476 (mp->max_reassembly_length),
1477 clib_net_to_host_u32
1478 (mp->expire_walk_interval_ms));
1479 }
1480 break;
1481 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1482 if (mp->is_ip6)
1483 {
1484 rv =
1485 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1486 clib_net_to_host_u32 (mp->max_reassemblies),
1487 clib_net_to_host_u32
1488 (mp->max_reassembly_length),
1489 clib_net_to_host_u32
1490 (mp->expire_walk_interval_ms));
1491 }
1492 else
1493 {
1494 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1495 clib_net_to_host_u32 (mp->max_reassemblies),
1496 clib_net_to_host_u32
1497 (mp->max_reassembly_length),
1498 clib_net_to_host_u32
1499 (mp->expire_walk_interval_ms));
1500 }
1501 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001502 }
1503
1504 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1505}
1506
1507void
1508vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1509{
Ole Troan2e1c8962019-04-10 09:44:23 +02001510 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001511
Ole Troan2e1c8962019-04-10 09:44:23 +02001512 rp = vl_api_client_index_to_registration (mp->client_index);
1513 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001514 return;
1515
1516 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001517 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02001518 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
1519 rmp->context = mp->context;
1520 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001521 u32 timeout_ms;
1522 u32 max_reassemblies;
1523 u32 max_reassembly_length;
1524 u32 expire_walk_interval_ms;
1525 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001526 {
Klement Sekerade34c352019-06-25 11:19:22 +00001527 case IP_REASS_TYPE_FULL:
1528 if (mp->is_ip6)
1529 {
1530 rmp->is_ip6 = 1;
1531 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1532 &max_reassembly_length,
1533 &expire_walk_interval_ms);
1534 }
1535 else
1536 {
1537 rmp->is_ip6 = 0;
1538 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1539 &max_reassembly_length,
1540 &expire_walk_interval_ms);
1541 }
1542 break;
1543 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1544 if (mp->is_ip6)
1545 {
1546 rmp->is_ip6 = 1;
1547 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1548 &max_reassembly_length, &expire_walk_interval_ms);
1549 }
1550 else
1551 {
1552 rmp->is_ip6 = 0;
1553 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1554 &max_reassembly_length, &expire_walk_interval_ms);
1555 }
1556 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001557 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001558 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1559 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1560 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001561 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001562 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001563 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001564}
1565
Klement Sekera4c533132018-02-22 11:41:12 +01001566void
1567 vl_api_ip_reassembly_enable_disable_t_handler
1568 (vl_api_ip_reassembly_enable_disable_t * mp)
1569{
1570 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1571 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001572 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001573 {
Klement Sekerade34c352019-06-25 11:19:22 +00001574 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001575 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001576 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1577 mp->enable_ip4);
1578 if (0 == rv)
1579 rv =
1580 ip6_full_reass_enable_disable (clib_net_to_host_u32
1581 (mp->sw_if_index), mp->enable_ip6);
1582 break;
1583 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1584 rv =
1585 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1586 mp->enable_ip4);
1587 if (0 == rv)
1588 {
1589 rv =
1590 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1591 (mp->sw_if_index), mp->enable_ip6);
1592 }
1593 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001594 }
1595
Choree7f61d12018-11-28 10:27:58 +03301596 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001597}
1598
Neale Ranns92207752019-06-03 13:21:40 +00001599typedef struct ip_punt_redirect_walk_ctx_t_
Pavel Kotucek609e1212018-11-27 09:59:44 +01001600{
Neale Ranns92207752019-06-03 13:21:40 +00001601 vl_api_registration_t *reg;
1602 u32 context;
1603} ip_punt_redirect_walk_ctx_t;
1604
1605static walk_rc_t
1606send_ip_punt_redirect_details (u32 rx_sw_if_index,
1607 const ip_punt_redirect_rx_t * ipr, void *arg)
1608{
Neale Ranns92207752019-06-03 13:21:40 +00001609 ip_punt_redirect_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001610 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001611 fib_path_encode_ctx_t path_ctx = {
1612 .rpaths = NULL,
1613 };
Pavel Kotucek609e1212018-11-27 09:59:44 +01001614
1615 mp = vl_msg_api_alloc (sizeof (*mp));
1616 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001617 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001618
1619 clib_memset (mp, 0, sizeof (*mp));
1620 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001621 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001622
Neale Ranns097fa662018-05-01 05:17:55 -07001623 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001624
1625 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001626 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001627
Neale Ranns097fa662018-05-01 05:17:55 -07001628 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001629 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1630
1631 vl_api_send_msg (ctx->reg, (u8 *) mp);
1632
Neale Ranns097fa662018-05-01 05:17:55 -07001633 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001634
1635 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001636}
1637
1638static void
1639vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1640{
1641 vl_api_registration_t *reg;
Dave Barach77841402020-04-29 17:04:10 -04001642 fib_protocol_t fproto = FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001643
Pavel Kotucek609e1212018-11-27 09:59:44 +01001644 reg = vl_api_client_index_to_registration (mp->client_index);
1645 if (!reg)
1646 return;
1647
Dave Barach77841402020-04-29 17:04:10 -04001648 if (mp->is_ipv6 == 1)
1649 fproto = FIB_PROTOCOL_IP6;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001650
Neale Ranns92207752019-06-03 13:21:40 +00001651 ip_punt_redirect_walk_ctx_t ctx = {
1652 .reg = reg,
1653 .context = mp->context,
1654 };
1655
1656 if (~0 != mp->sw_if_index)
Pavel Kotucek609e1212018-11-27 09:59:44 +01001657 {
Neale Ranns92207752019-06-03 13:21:40 +00001658 u32 rx_sw_if_index;
1659 index_t pri;
1660
1661 rx_sw_if_index = ntohl (mp->sw_if_index);
1662 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1663
1664 if (INDEX_INVALID == pri)
1665 return;
1666
1667 send_ip_punt_redirect_details (rx_sw_if_index,
1668 ip_punt_redirect_get (pri), &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001669 }
1670 else
Neale Ranns92207752019-06-03 13:21:40 +00001671 ip_punt_redirect_walk (fproto, send_ip_punt_redirect_details, &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001672}
1673
Dave Barachb5e8a772016-12-06 12:04:42 -05001674#define vl_msg_name_crc_list
1675#include <vnet/ip/ip.api.h>
1676#undef vl_msg_name_crc_list
1677
1678static void
1679setup_message_id_table (api_main_t * am)
1680{
1681#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1682 foreach_vl_msg_name_crc_ip;
1683#undef _
1684}
1685
1686static clib_error_t *
1687ip_api_hookup (vlib_main_t * vm)
1688{
Dave Barach39d69112019-11-27 11:42:13 -05001689 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001690
1691#define _(N,n) \
1692 vl_msg_api_set_handlers(VL_API_##N, #n, \
1693 vl_api_##n##_t_handler, \
1694 vl_noop_handler, \
1695 vl_api_##n##_t_endian, \
1696 vl_api_##n##_t_print, \
1697 sizeof(vl_api_##n##_t), 1);
1698 foreach_ip_api_msg;
1699#undef _
1700
1701 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00001702 * Mark the route add/del API as MP safe
1703 */
Neale Ranns097fa662018-05-01 05:17:55 -07001704 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
1705 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
Neale Ranns6a231a12018-10-17 06:38:00 +00001706
1707 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05001708 * Set up the (msg_name, crc, message-id) table
1709 */
1710 setup_message_id_table (am);
1711
Dave Barachb5e8a772016-12-06 12:04:42 -05001712 return 0;
1713}
1714
1715VLIB_API_INIT_FUNCTION (ip_api_hookup);
1716
1717/*
1718 * fd.io coding-style-patch-verification: ON
1719 *
1720 * Local Variables:
1721 * eval: (c-set-style "gnu")
1722 * End:
1723 */