blob: 4f5b6b9d0a64ac34453709089f5181b2206f4743 [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>
40#include <vnet/fib/ip4_fib.h>
41#include <vnet/fib/ip6_fib.h>
Neale Ranns92207752019-06-03 13:21:40 +000042#include <vnet/fib/fib_path_list.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080043#include <vnet/ip/ip6_hop_by_hop.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000044#include <vnet/ip/ip6_link.h>
Klement Sekerade34c352019-06-25 11:19:22 +000045#include <vnet/ip/reass/ip4_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000046#include <vnet/ip/reass/ip4_full_reass.h>
Klement Sekerade34c352019-06-25 11:19:22 +000047#include <vnet/ip/reass/ip6_sv_reass.h>
Klement Sekera896c8962019-06-24 11:52:49 +000048#include <vnet/ip/reass/ip6_full_reass.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) \
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) \
Chore3460b012018-11-28 10:53:11 +033097_(IP_SOURCE_CHECK_INTERFACE_ADD_DEL, \
98 ip_source_check_interface_add_del) \
Neale Rannscbe25aa2019-09-30 10:53:31 +000099 _(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
100 sw_interface_ip6_set_link_local_address) \
Klement Sekera75e7d132017-09-20 08:26:30 +0200101_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100102_(IP_REASSEMBLY_GET, ip_reassembly_get) \
Pavel Kotucek609e1212018-11-27 09:59:44 +0100103_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
104_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
105
Dave Barachb5e8a772016-12-06 12:04:42 -0500106static void
Neale Rannscbe25aa2019-09-30 10:53:31 +0000107 vl_api_sw_interface_ip6_enable_disable_t_handler
108 (vl_api_sw_interface_ip6_enable_disable_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500109{
Neale Rannscbe25aa2019-09-30 10:53:31 +0000110 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
111 int rv = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500112
Neale Rannscbe25aa2019-09-30 10:53:31 +0000113 VALIDATE_SW_IF_INDEX (mp);
Neale Ranns37029302018-08-10 05:30:06 -0700114
Neale Rannscbe25aa2019-09-30 10:53:31 +0000115 rv = ((mp->enable == 1) ?
116 ip6_link_enable (ntohl (mp->sw_if_index)) :
117 ip6_link_disable (ntohl (mp->sw_if_index)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500118
Neale Rannscbe25aa2019-09-30 10:53:31 +0000119 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500120
Neale Rannscbe25aa2019-09-30 10:53:31 +0000121 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
Dave Barachb5e8a772016-12-06 12:04:42 -0500122}
123
Dave Barachb5e8a772016-12-06 12:04:42 -0500124static void
Neale Ranns097fa662018-05-01 05:17:55 -0700125send_ip_table_details (vpe_api_main_t * am,
126 vl_api_registration_t * reg,
127 u32 context, const fib_table_t * table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500128{
Neale Ranns097fa662018-05-01 05:17:55 -0700129 vl_api_ip_table_details_t *mp;
Dave Barachb5e8a772016-12-06 12:04:42 -0500130
Neale Ranns097fa662018-05-01 05:17:55 -0700131 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500132 if (!mp)
133 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400134 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700135 mp->_vl_msg_id = ntohs (VL_API_IP_TABLE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500136 mp->context = context;
137
Neale Ranns097fa662018-05-01 05:17:55 -0700138 mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
139 mp->table.table_id = htonl (table->ft_table_id);
140 memcpy (mp->table.name, table->ft_desc,
141 clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500142
Florin Coras6c4dae22018-01-09 06:39:23 -0800143 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500144}
145
Neale Ranns097fa662018-05-01 05:17:55 -0700146static void
147vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
148{
149 vpe_api_main_t *am = &vpe_api_main;
150 vl_api_registration_t *reg;
151 fib_table_t *fib_table;
152
153 reg = vl_api_client_index_to_registration (mp->client_index);
154 if (!reg)
155 return;
156
157 /* *INDENT-OFF* */
158 pool_foreach (fib_table, ip4_main.fibs,
159 ({
160 send_ip_table_details(am, reg, mp->context, fib_table);
161 }));
162 pool_foreach (fib_table, ip6_main.fibs,
163 ({
164 /* don't send link locals */
165 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
166 continue;
167 send_ip_table_details(am, reg, mp->context, fib_table);
168 }));
169 /* *INDENT-ON* */
170}
171
Neale Rannsa3af3372017-03-28 03:49:52 -0700172typedef struct vl_api_ip_fib_dump_walk_ctx_t_
173{
174 fib_node_index_t *feis;
175} vl_api_ip_fib_dump_walk_ctx_t;
176
Neale Ranns89541992017-04-06 04:41:02 -0700177static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700178vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
179{
180 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
181
182 vec_add1 (ctx->feis, fei);
183
Neale Ranns89541992017-04-06 04:41:02 -0700184 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700185}
186
Dave Barachb5e8a772016-12-06 12:04:42 -0500187static void
Neale Ranns097fa662018-05-01 05:17:55 -0700188send_ip_route_details (vpe_api_main_t * am,
189 vl_api_registration_t * reg,
190 u32 context, fib_node_index_t fib_entry_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500191{
Neale Ranns097fa662018-05-01 05:17:55 -0700192 fib_route_path_t *rpaths, *rpath;
193 vl_api_ip_route_details_t *mp;
Neale Rannsc5d43172018-07-30 08:04:40 -0700194 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500195 vl_api_fib_path_t *fp;
196 int path_count;
197
Neale Ranns097fa662018-05-01 05:17:55 -0700198 rpaths = NULL;
199 pfx = fib_entry_get_prefix (fib_entry_index);
200 rpaths = fib_entry_encode (fib_entry_index);
201
202 path_count = vec_len (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500203 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
204 if (!mp)
205 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400206 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns097fa662018-05-01 05:17:55 -0700207 mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_DETAILS);
Dave Barachb5e8a772016-12-06 12:04:42 -0500208 mp->context = context;
209
Neale Ranns097fa662018-05-01 05:17:55 -0700210 ip_prefix_encode (pfx, &mp->route.prefix);
211 mp->route.table_id =
212 htonl (fib_table_get_table_id
213 (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
214 mp->route.n_paths = path_count;
215 mp->route.stats_index =
216 htonl (fib_table_entry_get_stats_index
217 (fib_entry_get_fib_index (fib_entry_index), pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500218
Neale Ranns097fa662018-05-01 05:17:55 -0700219 fp = mp->route.paths;
220 vec_foreach (rpath, rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500221 {
Neale Ranns097fa662018-05-01 05:17:55 -0700222 fib_api_path_encode (rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500223 fp++;
224 }
225
Florin Coras6c4dae22018-01-09 06:39:23 -0800226 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns9db6ada2019-11-08 12:42:31 +0000227 vec_free (rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500228}
229
Dave Barachd7cb1b52016-12-09 09:52:16 -0500230typedef struct apt_ip6_fib_show_ctx_t_
231{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500232 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500233} api_ip6_fib_show_ctx_t;
234
Dave Barachb5e8a772016-12-06 12:04:42 -0500235static void
Neale Ranns097fa662018-05-01 05:17:55 -0700236vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500237{
238 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500239 fib_node_index_t *fib_entry_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800240 vl_api_registration_t *reg;
Neale Ranns097fa662018-05-01 05:17:55 -0700241 fib_protocol_t fproto;
242 u32 fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -0500243
Florin Coras6c4dae22018-01-09 06:39:23 -0800244 reg = vl_api_client_index_to_registration (mp->client_index);
245 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500246 return;
247
Neale Ranns097fa662018-05-01 05:17:55 -0700248 vl_api_ip_fib_dump_walk_ctx_t ctx = {
249 .feis = NULL,
250 };
Neale Ranns81458422018-03-12 06:59:36 -0700251
Neale Ranns097fa662018-05-01 05:17:55 -0700252 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
253 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
254
255 if (INDEX_INVALID == fib_index)
256 return;
257
258 fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
259
260 vec_foreach (fib_entry_index, ctx.feis)
261 {
262 send_ip_route_details (am, reg, mp->context, *fib_entry_index);
263 }
264
265 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500266}
267
268static void
Neale Ranns097fa662018-05-01 05:17:55 -0700269send_ip_mtable_details (vl_api_registration_t * reg,
270 u32 context, const mfib_table_t * mfib_table)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800271{
Neale Ranns097fa662018-05-01 05:17:55 -0700272 vl_api_ip_mtable_details_t *mp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800273
Neale Ranns097fa662018-05-01 05:17:55 -0700274 mp = vl_msg_api_alloc (sizeof (*mp));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800275 if (!mp)
276 return;
Neale Ranns097fa662018-05-01 05:17:55 -0700277 memset (mp, 0, sizeof (*mp));
278 mp->_vl_msg_id = ntohs (VL_API_IP_MTABLE_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800279 mp->context = context;
280
Neale Ranns097fa662018-05-01 05:17:55 -0700281 mp->table.table_id = htonl (mfib_table->mft_table_id);
282 mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800283
Florin Coras6c4dae22018-01-09 06:39:23 -0800284 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800285}
286
Neale Ranns5a8123b2017-01-26 01:18:23 -0800287static void
Neale Ranns097fa662018-05-01 05:17:55 -0700288vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800289{
Florin Coras6c4dae22018-01-09 06:39:23 -0800290 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800291 mfib_table_t *mfib_table;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800292
Florin Coras6c4dae22018-01-09 06:39:23 -0800293 reg = vl_api_client_index_to_registration (mp->client_index);
294 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800295 return;
296
Neale Ranns5a8123b2017-01-26 01:18:23 -0800297 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700298 pool_foreach (mfib_table, ip4_main.mfibs,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800299 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700300 send_ip_mtable_details (reg, mp->context, mfib_table);
301 }));
302 pool_foreach (mfib_table, ip6_main.mfibs,
303 ({
304 send_ip_mtable_details (reg, mp->context, mfib_table);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800305 }));
306 /* *INDENT-ON* */
Neale Ranns5a8123b2017-01-26 01:18:23 -0800307}
308
Neale Ranns097fa662018-05-01 05:17:55 -0700309typedef struct vl_api_ip_mfib_dump_ctx_t_
Neale Ranns5a8123b2017-01-26 01:18:23 -0800310{
311 fib_node_index_t *entries;
Neale Ranns097fa662018-05-01 05:17:55 -0700312} vl_api_ip_mfib_dump_ctx_t;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800313
Neale Ranns9db6ada2019-11-08 12:42:31 +0000314static walk_rc_t
Neale Ranns097fa662018-05-01 05:17:55 -0700315mfib_route_dump_walk (fib_node_index_t fei, void *arg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800316{
Neale Ranns097fa662018-05-01 05:17:55 -0700317 vl_api_ip_mfib_dump_ctx_t *ctx = arg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800318
319 vec_add1 (ctx->entries, fei);
320
Neale Ranns9db6ada2019-11-08 12:42:31 +0000321 return (WALK_CONTINUE);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800322}
323
324static void
Neale Ranns097fa662018-05-01 05:17:55 -0700325send_ip_mroute_details (vpe_api_main_t * am,
326 vl_api_registration_t * reg,
327 u32 context, fib_node_index_t mfib_entry_index)
328{
329 fib_route_path_t *rpaths, *rpath;
330 vl_api_ip_mroute_details_t *mp;
331 const mfib_prefix_t *pfx;
332 vl_api_mfib_path_t *fp;
333 int path_count;
334
335 rpaths = NULL;
336 pfx = mfib_entry_get_prefix (mfib_entry_index);
337 rpaths = mfib_entry_encode (mfib_entry_index);
338
339 path_count = vec_len (rpaths);
340 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
341 if (!mp)
342 return;
343 clib_memset (mp, 0, sizeof (*mp));
344 mp->_vl_msg_id = ntohs (VL_API_IP_MROUTE_DETAILS);
345 mp->context = context;
346
347 ip_mprefix_encode (pfx, &mp->route.prefix);
348 mp->route.table_id =
349 htonl (mfib_table_get_table_id
350 (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
351 mp->route.n_paths = htonl (path_count);
352 fp = mp->route.paths;
353 vec_foreach (rpath, rpaths)
354 {
355 mfib_api_path_encode (rpath, fp);
356 fp++;
357 }
358
359 vl_api_send_msg (reg, (u8 *) mp);
360 vec_free (rpaths);
361}
362
363static void
364vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800365{
366 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800367 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800368 fib_node_index_t *mfeip;
Neale Ranns097fa662018-05-01 05:17:55 -0700369 fib_protocol_t fproto;
370 u32 fib_index;
371
372 vl_api_ip_mfib_dump_ctx_t ctx = {
Neale Ranns5a8123b2017-01-26 01:18:23 -0800373 .entries = NULL,
374 };
375
Florin Coras6c4dae22018-01-09 06:39:23 -0800376 reg = vl_api_client_index_to_registration (mp->client_index);
377 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800378 return;
379
Neale Ranns097fa662018-05-01 05:17:55 -0700380 fproto = fib_ip_proto (mp->table.is_ip6);
381 fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800382
Neale Ranns097fa662018-05-01 05:17:55 -0700383 if (INDEX_INVALID == fib_index)
384 return;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800385
Neale Ranns097fa662018-05-01 05:17:55 -0700386 mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800387
Neale Ranns097fa662018-05-01 05:17:55 -0700388 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800389
Neale Ranns097fa662018-05-01 05:17:55 -0700390 vec_foreach (mfeip, ctx.entries)
391 {
392 send_ip_mroute_details (am, reg, mp->context, *mfeip);
393 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800394
395 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800396}
397
398static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700399vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
400 vlib_main_t * vm)
401{
402 vl_api_ip_punt_police_reply_t *rmp;
403 int rv = 0;
404
405 if (mp->is_ip6)
406 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
407 else
408 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
409
410 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
411}
412
413static void
414vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
415 vlib_main_t * vm)
416{
417 vl_api_ip_punt_redirect_reply_t *rmp;
418 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100419 ip46_type_t ipv;
420 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700421
Pavel Kotucek609e1212018-11-27 09:59:44 +0100422 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
423 goto bad_sw_if_index;
424
425 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700426 if (mp->is_add)
427 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100428 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700429 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100430 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
431 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700432 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100433 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700434 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100435 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
436 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700437 }
438 }
439 else
440 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100441 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700442 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100443 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700444 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100445 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700446 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100447 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700448 }
449 }
450
Pavel Kotucek609e1212018-11-27 09:59:44 +0100451 BAD_SW_IF_INDEX_LABEL;
452
Neale Rannsd91c1db2017-07-31 02:30:50 -0700453 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
454}
455
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700456void
Neale Ranns15002542017-09-10 04:39:11 -0700457ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
458{
459 u32 fib_index, mfib_index;
460
461 /*
462 * ignore action on the default table - this is always present
463 * and cannot be added nor deleted from the API
464 */
465 if (0 != table_id)
466 {
467 /*
468 * The API holds only one lock on the table.
469 * i.e. it can be added many times via the API but needs to be
470 * deleted only once.
471 * The FIB index for unicast and multicast is not necessarily the
472 * same, since internal VPP systesm (like LISP and SR) create
473 * their own unicast tables.
474 */
475 fib_index = fib_table_find (fproto, table_id);
476 mfib_index = mfib_table_find (fproto, table_id);
477
478 if (~0 != fib_index)
479 {
480 fib_table_unlock (fib_index, fproto,
481 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
482 }
483 if (~0 != mfib_index)
484 {
485 mfib_table_unlock (mfib_index, fproto,
486 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
487 }
488 }
489}
490
491void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700492vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
493{
494 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700495 fib_protocol_t fproto = (mp->table.is_ip6 ?
496 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
497 u32 table_id = ntohl (mp->table.table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700498 int rv = 0;
499
Neale Ranns15002542017-09-10 04:39:11 -0700500 if (mp->is_add)
501 {
Neale Ranns097fa662018-05-01 05:17:55 -0700502 ip_table_create (fproto, table_id, 1, mp->table.name);
Neale Ranns15002542017-09-10 04:39:11 -0700503 }
504 else
505 {
506 ip_table_delete (fproto, table_id, 1);
507 }
508
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700509 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
510}
511
Dave Barachb5e8a772016-12-06 12:04:42 -0500512static int
Neale Ranns097fa662018-05-01 05:17:55 -0700513ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500514{
Neale Ranns097fa662018-05-01 05:17:55 -0700515 fib_route_path_t *rpaths = NULL, *rpath;
516 fib_entry_flag_t entry_flags;
517 vl_api_fib_path_t *apath;
518 fib_prefix_t pfx;
519 u32 fib_index;
520 int rv, ii;
Dave Barachb5e8a772016-12-06 12:04:42 -0500521
Neale Ranns097fa662018-05-01 05:17:55 -0700522 entry_flags = FIB_ENTRY_FLAG_NONE;
523 ip_prefix_decode (&mp->route.prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500524
Neale Ranns097fa662018-05-01 05:17:55 -0700525 rv = fib_api_table_id_decode (pfx.fp_proto,
526 ntohl (mp->route.table_id), &fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500527 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700528 goto out;
Dave Barachb5e8a772016-12-06 12:04:42 -0500529
Neale Rannsc2ac2352019-07-02 14:33:29 +0000530 if (0 != mp->route.n_paths)
531 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns097fa662018-05-01 05:17:55 -0700532
533 for (ii = 0; ii < mp->route.n_paths; ii++)
534 {
535 apath = &mp->route.paths[ii];
536 rpath = &rpaths[ii];
537
538 rv = fib_api_path_decode (apath, rpath);
539
540 if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
541 (~0 == rpath->frp_sw_if_index))
542 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
543
544 if (0 != rv)
545 goto out;
546 }
547
Neale Rannsc2ac2352019-07-02 14:33:29 +0000548 rv = fib_api_route_add_del (mp->is_add,
549 mp->is_multipath,
550 fib_index, &pfx, entry_flags, rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700551
552 if (mp->is_add && 0 == rv)
553 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
554
Neale Ranns097fa662018-05-01 05:17:55 -0700555out:
556 vec_free (rpaths);
Neale Ranns008dbe12018-09-07 09:32:36 -0700557
558 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -0500559}
560
561void
Neale Ranns097fa662018-05-01 05:17:55 -0700562vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
Dave Barachb5e8a772016-12-06 12:04:42 -0500563{
Neale Ranns097fa662018-05-01 05:17:55 -0700564 vl_api_ip_route_add_del_reply_t *rmp;
565 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500566 int rv;
Dave Barachb5e8a772016-12-06 12:04:42 -0500567
Neale Ranns097fa662018-05-01 05:17:55 -0700568 rv = ip_route_add_del_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500569
Neale Ranns008dbe12018-09-07 09:32:36 -0700570 /* *INDENT-OFF* */
Neale Ranns097fa662018-05-01 05:17:55 -0700571 REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
Neale Ranns008dbe12018-09-07 09:32:36 -0700572 ({
573 rmp->stats_index = htonl (stats_index);
574 }))
575 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500576}
577
Neale Ranns15002542017-09-10 04:39:11 -0700578void
Neale Ranns2297af02017-09-12 09:45:04 -0700579ip_table_create (fib_protocol_t fproto,
580 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -0700581{
582 u32 fib_index, mfib_index;
583
584 /*
585 * ignore action on the default table - this is always present
586 * and cannot be added nor deleted from the API
587 */
588 if (0 != table_id)
589 {
590 /*
591 * The API holds only one lock on the table.
592 * i.e. it can be added many times via the API but needs to be
593 * deleted only once.
594 * The FIB index for unicast and multicast is not necessarily the
595 * same, since internal VPP systesm (like LISP and SR) create
596 * their own unicast tables.
597 */
598 fib_index = fib_table_find (fproto, table_id);
599 mfib_index = mfib_table_find (fproto, table_id);
600
601 if (~0 == fib_index)
602 {
Neale Ranns2297af02017-09-12 09:45:04 -0700603 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
604 (is_api ?
605 FIB_SOURCE_API :
606 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700607 }
608 if (~0 == mfib_index)
609 {
Neale Ranns2297af02017-09-12 09:45:04 -0700610 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
611 (is_api ?
612 MFIB_SOURCE_API :
613 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -0700614 }
615 }
616}
617
Neale Ranns097fa662018-05-01 05:17:55 -0700618static u32
Neale Ranns32e1c012016-11-22 17:07:28 +0000619mroute_add_del_handler (u8 is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700620 u8 is_multipath,
Neale Ranns32e1c012016-11-22 17:07:28 +0000621 u32 fib_index,
622 const mfib_prefix_t * prefix,
623 u32 entry_flags,
Neale Ranns097fa662018-05-01 05:17:55 -0700624 u32 rpf_id, fib_route_path_t * rpaths)
Neale Ranns32e1c012016-11-22 17:07:28 +0000625{
Neale Ranns097fa662018-05-01 05:17:55 -0700626 u32 mfib_entry_index = ~0;
Neale Ranns28c142e2018-09-07 09:37:07 -0700627
Neale Ranns097fa662018-05-01 05:17:55 -0700628 if (0 == vec_len (rpaths))
Neale Ranns32e1c012016-11-22 17:07:28 +0000629 {
Neale Ranns28c142e2018-09-07 09:37:07 -0700630 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
631 MFIB_SOURCE_API,
632 rpf_id, entry_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +0000633 }
634 else
635 {
Neale Ranns097fa662018-05-01 05:17:55 -0700636 if (is_add)
637 {
638 mfib_entry_index =
639 mfib_table_entry_paths_update (fib_index, prefix,
640 MFIB_SOURCE_API, rpaths);
641 }
642 else
643 {
644 mfib_table_entry_paths_remove (fib_index, prefix,
645 MFIB_SOURCE_API, rpaths);
646 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000647 }
648
Neale Ranns28c142e2018-09-07 09:37:07 -0700649 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000650}
651
652static int
Neale Ranns28c142e2018-09-07 09:37:07 -0700653api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
654 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +0000655{
Neale Ranns097fa662018-05-01 05:17:55 -0700656 fib_route_path_t *rpath, *rpaths = NULL;
Neale Ranns28c142e2018-09-07 09:37:07 -0700657 fib_node_index_t mfib_entry_index;
Neale Ranns097fa662018-05-01 05:17:55 -0700658 mfib_prefix_t pfx;
Neale Ranns32e1c012016-11-22 17:07:28 +0000659 u32 fib_index;
660 int rv;
Neale Ranns097fa662018-05-01 05:17:55 -0700661 u16 ii;
Neale Ranns32e1c012016-11-22 17:07:28 +0000662
Neale Ranns097fa662018-05-01 05:17:55 -0700663 ip_mprefix_decode (&mp->route.prefix, &pfx);
Neale Ranns32e1c012016-11-22 17:07:28 +0000664
Neale Ranns097fa662018-05-01 05:17:55 -0700665 rv = mfib_api_table_id_decode (pfx.fp_proto,
666 ntohl (mp->route.table_id), &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000667 if (0 != rv)
Neale Ranns097fa662018-05-01 05:17:55 -0700668 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000669
Neale Ranns097fa662018-05-01 05:17:55 -0700670 vec_validate (rpaths, mp->route.n_paths - 1);
Neale Ranns32e1c012016-11-22 17:07:28 +0000671
Neale Ranns097fa662018-05-01 05:17:55 -0700672 for (ii = 0; ii < mp->route.n_paths; ii++)
Neale Ranns32e1c012016-11-22 17:07:28 +0000673 {
Neale Ranns097fa662018-05-01 05:17:55 -0700674 rpath = &rpaths[ii];
675
676 rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
677
678 if (0 != rv)
679 goto out;
Neale Ranns32e1c012016-11-22 17:07:28 +0000680 }
681
Neale Ranns28c142e2018-09-07 09:37:07 -0700682 mfib_entry_index = mroute_add_del_handler (mp->is_add,
Neale Ranns097fa662018-05-01 05:17:55 -0700683 mp->is_add,
Neale Ranns28c142e2018-09-07 09:37:07 -0700684 fib_index, &pfx,
Neale Ranns097fa662018-05-01 05:17:55 -0700685 ntohl (mp->route.entry_flags),
686 ntohl (mp->route.rpf_id),
687 rpaths);
Neale Ranns28c142e2018-09-07 09:37:07 -0700688
689 if (~0 != mfib_entry_index)
690 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
691
Neale Ranns097fa662018-05-01 05:17:55 -0700692out:
Neale Ranns28c142e2018-09-07 09:37:07 -0700693 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +0000694}
695
696void
697vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
698{
699 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns097fa662018-05-01 05:17:55 -0700700 u32 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +0000701 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +0000702
Neale Ranns28c142e2018-09-07 09:37:07 -0700703 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +0000704
Neale Ranns28c142e2018-09-07 09:37:07 -0700705 /* *INDENT-OFF* */
706 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
707 ({
708 rmp->stats_index = htonl (stats_index);
709 }));
710 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +0000711}
712
Dave Barachb5e8a772016-12-06 12:04:42 -0500713static void
714send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800715 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
716 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500717{
718 vl_api_ip_details_t *mp;
719
720 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400721 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500722 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
723
724 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -0600725 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -0500726 mp->context = context;
727
Florin Coras6c4dae22018-01-09 06:39:23 -0800728 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500729}
730
731static void
732send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800733 vl_api_registration_t * reg,
Neale Ranns097fa662018-05-01 05:17:55 -0700734 const fib_prefix_t * pfx,
735 u32 sw_if_index, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500736{
737 vl_api_ip_address_details_t *mp;
738
739 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400740 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500741 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
742
Neale Ranns097fa662018-05-01 05:17:55 -0700743 ip_prefix_encode (pfx, &mp->prefix);
Dave Barachb5e8a772016-12-06 12:04:42 -0500744 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -0600745 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500746
Florin Coras6c4dae22018-01-09 06:39:23 -0800747 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500748}
749
750static void
751vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
752{
753 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800754 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500755 ip6_main_t *im6 = &ip6_main;
756 ip4_main_t *im4 = &ip4_main;
757 ip_lookup_main_t *lm6 = &im6->lookup_main;
758 ip_lookup_main_t *lm4 = &im4->lookup_main;
759 ip_interface_address_t *ia = 0;
760 u32 sw_if_index = ~0;
761 int rv __attribute__ ((unused)) = 0;
762
763 VALIDATE_SW_IF_INDEX (mp);
764
765 sw_if_index = ntohl (mp->sw_if_index);
766
Florin Coras6c4dae22018-01-09 06:39:23 -0800767 reg = vl_api_client_index_to_registration (mp->client_index);
768 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500769 return;
770
Dave Barachb5e8a772016-12-06 12:04:42 -0500771 if (mp->is_ipv6)
772 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500773 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -0700774 /* Do not send subnet details of the IP-interface for
775 * unnumbered interfaces. otherwise listening clients
776 * will be confused that the subnet is applied on more
777 * than one interface */
778 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -0500779 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700780 fib_prefix_t pfx = {
781 .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
782 .fp_len = ia->address_length,
783 .fp_proto = FIB_PROTOCOL_IP6,
784 };
785 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500786 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500787 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500788 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500789 else
790 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500791 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -0700792 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -0500793 ({
Neale Ranns097fa662018-05-01 05:17:55 -0700794 fib_prefix_t pfx = {
795 .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
796 .fp_len = ia->address_length,
797 .fp_proto = FIB_PROTOCOL_IP4,
798 };
799
800 send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500801 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500802 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500803 }
Neale Ranns008dbe12018-09-07 09:32:36 -0700804
Dave Barachb5e8a772016-12-06 12:04:42 -0500805 BAD_SW_IF_INDEX_LABEL;
806}
807
808static void
Neale Ranns9e2f9152018-05-18 02:27:10 -0700809send_ip_unnumbered_details (vpe_api_main_t * am,
810 vl_api_registration_t * reg,
811 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
812{
813 vl_api_ip_unnumbered_details_t *mp;
814
815 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400816 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -0700817 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
818
819 mp->context = context;
820 mp->sw_if_index = htonl (sw_if_index);
821 mp->ip_sw_if_index = htonl (ip_sw_if_index);
822
823 vl_api_send_msg (reg, (u8 *) mp);
824}
825
826static void
827vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
828{
829 vnet_main_t *vnm = vnet_get_main ();
830 vnet_interface_main_t *im = &vnm->interface_main;
831 int rv __attribute__ ((unused)) = 0;
832 vpe_api_main_t *am = &vpe_api_main;
833 vl_api_registration_t *reg;
834 vnet_sw_interface_t *si;
835 u32 sw_if_index;
836
837 sw_if_index = ntohl (mp->sw_if_index);
838
839 reg = vl_api_client_index_to_registration (mp->client_index);
840 if (!reg)
841 return;
842
843 if (~0 != sw_if_index)
844 {
845 VALIDATE_SW_IF_INDEX (mp);
846
847 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
848
Neale Rannsac3e72c2019-10-06 01:04:26 -0700849 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
Neale Ranns9e2f9152018-05-18 02:27:10 -0700850 {
851 send_ip_unnumbered_details (am, reg,
852 sw_if_index,
853 si->unnumbered_sw_if_index,
854 mp->context);
855 }
856 }
857 else
858 {
859 /* *INDENT-OFF* */
860 pool_foreach (si, im->sw_interfaces,
861 ({
862 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
863 {
864 send_ip_unnumbered_details(am, reg,
865 si->sw_if_index,
866 si->unnumbered_sw_if_index,
867 mp->context);
868 }
869 }));
870 /* *INDENT-ON* */
871 }
872
873 BAD_SW_IF_INDEX_LABEL;
874}
875
876static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500877vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
878{
879 vpe_api_main_t *am = &vpe_api_main;
880 vnet_main_t *vnm = vnet_get_main ();
Neale Rannscbe25aa2019-09-30 10:53:31 +0000881 //vlib_main_t *vm = vlib_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500882 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800883 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500884 vnet_sw_interface_t *si, *sorted_sis;
885 u32 sw_if_index = ~0;
886
Florin Coras6c4dae22018-01-09 06:39:23 -0800887 reg = vl_api_client_index_to_registration (mp->client_index);
888 if (!reg)
889 return;
Dave Barachb5e8a772016-12-06 12:04:42 -0500890
891 /* Gather interfaces. */
892 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
893 _vec_len (sorted_sis) = 0;
894 /* *INDENT-OFF* */
895 pool_foreach (si, im->sw_interfaces,
896 ({
897 vec_add1 (sorted_sis, si[0]);
898 }));
899 /* *INDENT-ON* */
900
901 vec_foreach (si, sorted_sis)
902 {
903 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
904 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000905 /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
906 /* { */
907 /* continue; */
908 /* } */
Dave Barachb5e8a772016-12-06 12:04:42 -0500909 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -0800910 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500911 }
912 }
Matthew Smith6d5f6592019-09-18 13:51:46 -0500913
914 vec_free (sorted_sis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500915}
916
917static void
918set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
919{
920 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -0700921 int rv;
922 u32 table_id;
923 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500924
Neale Ranns227038a2017-04-21 01:07:59 -0700925 table_id = ntohl (mp->vrf_id);
926
927#define _(a,b) if (mp->a) flow_hash_config |= b;
928 foreach_flow_hash_bit;
929#undef _
930
931 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -0500932
933 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
934}
935
936static void
937set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
938{
939 vl_api_set_ip_flow_hash_reply_t *rmp;
940 int rv;
941 u32 table_id;
942 flow_hash_config_t flow_hash_config = 0;
943
944 table_id = ntohl (mp->vrf_id);
945
946#define _(a,b) if (mp->a) flow_hash_config |= b;
947 foreach_flow_hash_bit;
948#undef _
949
950 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
951
952 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
953}
954
955
956static void
957vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
958{
959 if (mp->is_ipv6 == 0)
960 set_ip4_flow_hash (mp);
961 else
962 set_ip6_flow_hash (mp);
963}
964
Neale Ranns32e1c012016-11-22 17:07:28 +0000965void
Florin Corasaf0ff5a2018-01-10 08:17:03 -0800966vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +0000967 u32 context, const mfib_signal_t * mfs)
968{
969 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -0800970 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +0000971 mfib_table_t *mfib;
972 mfib_itf_t *mfi;
973
974 mp = vl_msg_api_alloc (sizeof (*mp));
975
Dave Barachb7b92992018-10-17 10:38:51 -0400976 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +0000977 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
978 mp->context = context;
979
980 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -0800981 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +0000982 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -0800983 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +0000984 mp->table_id = ntohl (mfib->mft_table_id);
985 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
986
Neale Ranns097fa662018-05-01 05:17:55 -0700987 ip_mprefix_encode (prefix, &mp->prefix);
Neale Ranns32e1c012016-11-22 17:07:28 +0000988
989 if (0 != mfs->mfs_buffer_len)
990 {
991 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
992
993 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
994 }
995 else
996 {
997 mp->ip_packet_len = 0;
998 }
999
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001000 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001001}
1002
1003static void
1004vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1005{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001006 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001007
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001008 reg = vl_api_client_index_to_registration (mp->client_index);
1009 if (!reg)
1010 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001011
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001012 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001013 ;
1014}
Dave Barachb5e8a772016-12-06 12:04:42 -05001015
Florin Coras595992c2017-11-06 17:17:08 -08001016static void
1017 vl_api_ip_container_proxy_add_del_t_handler
1018 (vl_api_ip_container_proxy_add_del_t * mp)
1019{
1020 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1021 vnet_ip_container_proxy_args_t args;
1022 int rv = 0;
1023 clib_error_t *error;
1024
Dave Barachb7b92992018-10-17 10:38:51 -04001025 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001026
1027 ip_prefix_decode (&mp->pfx, &args.prefix);
1028
Florin Coras595992c2017-11-06 17:17:08 -08001029 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1030 args.is_add = mp->is_add;
1031 if ((error = vnet_ip_container_proxy_add_del (&args)))
1032 {
1033 rv = clib_error_get_code (error);
1034 clib_error_report (error);
1035 }
1036
1037 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1038}
1039
Matus Fabian75b9f452018-10-02 23:27:21 -07001040typedef struct ip_container_proxy_walk_ctx_t_
1041{
1042 vl_api_registration_t *reg;
1043 u32 context;
1044} ip_container_proxy_walk_ctx_t;
1045
1046static int
1047ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1048 void *args)
1049{
1050 vl_api_ip_container_proxy_details_t *mp;
1051 ip_container_proxy_walk_ctx_t *ctx = args;
1052
1053 mp = vl_msg_api_alloc (sizeof (*mp));
1054 if (!mp)
1055 return 1;
1056
Dave Barachb7b92992018-10-17 10:38:51 -04001057 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001058 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1059 mp->context = ctx->context;
1060
1061 mp->sw_if_index = ntohl (sw_if_index);
1062 ip_prefix_encode (pfx, &mp->prefix);
1063
1064 vl_api_send_msg (ctx->reg, (u8 *) mp);
1065
1066 return 1;
1067}
1068
1069static void
1070vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1071 mp)
1072{
1073 vl_api_registration_t *reg;
1074
1075 reg = vl_api_client_index_to_registration (mp->client_index);
1076 if (!reg)
1077 return;
1078
1079 ip_container_proxy_walk_ctx_t ctx = {
1080 .context = mp->context,
1081 .reg = reg,
1082 };
1083
1084 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1085}
1086
Neale Rannsb8d44812017-11-10 06:53:54 -08001087static void
1088vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1089{
1090 int rv = 0;
1091 vl_api_ioam_enable_reply_t *rmp;
1092 clib_error_t *error;
1093
1094 /* Ignoring the profile id as currently a single profile
1095 * is supported */
1096 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1097 mp->seqno, mp->analyse);
1098 if (error)
1099 {
1100 clib_error_report (error);
1101 rv = clib_error_get_code (error);
1102 }
1103
1104 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1105}
1106
1107static void
1108vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1109{
1110 int rv = 0;
1111 vl_api_ioam_disable_reply_t *rmp;
1112 clib_error_t *error;
1113
1114 error = clear_ioam_rewrite_fn ();
1115 if (error)
1116 {
1117 clib_error_report (error);
1118 rv = clib_error_get_code (error);
1119 }
1120
1121 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1122}
1123
1124static void
1125 vl_api_ip_source_and_port_range_check_add_del_t_handler
1126 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1127{
1128 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1129 int rv = 0;
1130
Neale Rannsb8d44812017-11-10 06:53:54 -08001131 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07001132 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08001133 u16 *low_ports = 0;
1134 u16 *high_ports = 0;
1135 u32 vrf_id;
1136 u16 tmp_low, tmp_high;
1137 u8 num_ranges;
1138 int i;
1139
Neale Ranns37029302018-08-10 05:30:06 -07001140 ip_prefix_decode (&mp->prefix, &pfx);
1141
Neale Rannsb8d44812017-11-10 06:53:54 -08001142 // Validate port range
1143 num_ranges = mp->number_of_ranges;
1144 if (num_ranges > 32)
1145 { // This is size of array in VPE.API
1146 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1147 goto reply;
1148 }
1149
1150 vec_reset_length (low_ports);
1151 vec_reset_length (high_ports);
1152
1153 for (i = 0; i < num_ranges; i++)
1154 {
1155 tmp_low = mp->low_ports[i];
1156 tmp_high = mp->high_ports[i];
1157 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1158 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1159 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1160 {
1161 rv = VNET_API_ERROR_INVALID_VALUE;
1162 goto reply;
1163 }
1164 vec_add1 (low_ports, tmp_low);
1165 vec_add1 (high_ports, tmp_high + 1);
1166 }
1167
Neale Rannsb8d44812017-11-10 06:53:54 -08001168 vrf_id = ntohl (mp->vrf_id);
1169
1170 if (vrf_id < 1)
1171 {
1172 rv = VNET_API_ERROR_INVALID_VALUE;
1173 goto reply;
1174 }
1175
1176
Neale Ranns37029302018-08-10 05:30:06 -07001177 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08001178 {
Neale Ranns37029302018-08-10 05:30:06 -07001179 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1180 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001181 vrf_id,
1182 low_ports,
1183 high_ports, is_add);
1184 }
1185 else
1186 {
Neale Ranns37029302018-08-10 05:30:06 -07001187 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1188 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08001189 vrf_id,
1190 low_ports,
1191 high_ports, is_add);
1192 }
1193
1194reply:
1195 vec_free (low_ports);
1196 vec_free (high_ports);
1197 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1198}
1199
1200static void
1201 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1202 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1203{
1204 vlib_main_t *vm = vlib_get_main ();
1205 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1206 ip4_main_t *im = &ip4_main;
1207 int rv;
1208 u32 sw_if_index;
1209 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1210 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1211 uword *p = 0;
1212 int i;
1213
1214 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1215 ntohl (mp->tcp_out_vrf_id);
1216 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1217 ntohl (mp->udp_out_vrf_id);
1218 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1219 ntohl (mp->tcp_in_vrf_id);
1220 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1221 ntohl (mp->udp_in_vrf_id);
1222
1223
1224 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1225 {
1226 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1227 {
1228 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1229
1230 if (p == 0)
1231 {
1232 rv = VNET_API_ERROR_INVALID_VALUE;
1233 goto reply;
1234 }
1235
1236 fib_index[i] = p[0];
1237 }
1238 else
1239 fib_index[i] = ~0;
1240 }
1241 sw_if_index = ntohl (mp->sw_if_index);
1242
1243 VALIDATE_SW_IF_INDEX (mp);
1244
1245 rv =
1246 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1247 mp->is_add);
1248
1249 BAD_SW_IF_INDEX_LABEL;
1250reply:
1251
1252 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1253}
1254
Neale Rannscbe25aa2019-09-30 10:53:31 +00001255static void
1256 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1257 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1258{
1259 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1260 ip6_address_t ip;
1261 int rv;
1262
1263 VALIDATE_SW_IF_INDEX (mp);
1264
1265 ip6_address_decode (mp->ip, &ip);
1266
1267 rv = ip6_set_link_local_address (ntohl (mp->sw_if_index), &ip);
1268
1269 BAD_SW_IF_INDEX_LABEL;
1270 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1271}
1272
Chore3460b012018-11-28 10:53:11 +03301273typedef union
1274{
1275 u32 fib_index;
1276} ip4_source_check_config_t;
1277
1278static void
1279 vl_api_ip_source_check_interface_add_del_t_handler
1280 (vl_api_ip_source_check_interface_add_del_t * mp)
1281{
1282 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
1283 int rv;
1284 u32 sw_if_index = ntohl (mp->sw_if_index);
1285 u8 is_add = mp->is_add;
1286 char *feature_name =
1287 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
1288
1289 ip4_source_check_config_t config;
1290
1291 VALIDATE_SW_IF_INDEX (mp);
1292
1293 config.fib_index =
1294 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
1295 rv =
1296 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
1297 is_add, &config, sizeof (config));
1298 BAD_SW_IF_INDEX_LABEL;
1299
1300 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
1301}
1302
John Lo7f358b32018-04-28 01:19:24 -04001303
Neale Ranns9db6ada2019-11-08 12:42:31 +00001304static void
1305vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001306{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001307 vl_api_ip_table_replace_begin_reply_t *rmp;
1308 fib_protocol_t fproto;
1309 u32 fib_index;
1310 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001311
Neale Ranns9db6ada2019-11-08 12:42:31 +00001312 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1313 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08001314
Neale Ranns9db6ada2019-11-08 12:42:31 +00001315 if (INDEX_INVALID == fib_index)
1316 rv = VNET_API_ERROR_NO_SUCH_FIB;
1317 else
1318 {
1319 fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1320 mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1321 fproto, MFIB_SOURCE_API);
Neale Rannsb8d44812017-11-10 06:53:54 -08001322 }
Neale Ranns9db6ada2019-11-08 12:42:31 +00001323 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001324}
1325
1326static void
Neale Ranns9db6ada2019-11-08 12:42:31 +00001327vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
Neale Rannsb8d44812017-11-10 06:53:54 -08001328{
Neale Ranns9db6ada2019-11-08 12:42:31 +00001329 vl_api_ip_table_replace_end_reply_t *rmp;
1330 fib_protocol_t fproto;
1331 u32 fib_index;
1332 int rv = 0;
Neale Rannsb8d44812017-11-10 06:53:54 -08001333
Neale Ranns9db6ada2019-11-08 12:42:31 +00001334 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1335 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1336
1337 if (INDEX_INVALID == fib_index)
1338 rv = VNET_API_ERROR_NO_SUCH_FIB;
Neale Rannsb8d44812017-11-10 06:53:54 -08001339 else
Neale Ranns9db6ada2019-11-08 12:42:31 +00001340 {
1341 fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1342 mfib_table_sweep (mfib_table_find
1343 (fproto, ntohl (mp->table.table_id)), fproto,
1344 MFIB_SOURCE_API);
1345 }
1346 REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1347}
Neale Rannsb8d44812017-11-10 06:53:54 -08001348
Neale Ranns9db6ada2019-11-08 12:42:31 +00001349static void
1350vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1351{
1352 vl_api_ip_table_flush_reply_t *rmp;
1353 fib_protocol_t fproto;
1354 u32 fib_index;
1355 int rv = 0;
1356
1357 fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1358 fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1359
1360 if (INDEX_INVALID == fib_index)
1361 rv = VNET_API_ERROR_NO_SUCH_FIB;
1362 else
1363 {
1364 vnet_main_t *vnm = vnet_get_main ();
1365 vnet_interface_main_t *im = &vnm->interface_main;
1366 vnet_sw_interface_t *si;
1367
1368 /* Shut down interfaces in this FIB / clean out intfc routes */
1369 /* *INDENT-OFF* */
1370 pool_foreach (si, im->sw_interfaces,
1371 ({
1372 if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1373 si->sw_if_index))
1374 {
1375 u32 flags = si->flags;
1376 flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1377 vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1378 }
1379 }));
1380 /* *INDENT-ON* */
1381
1382 fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1383 mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1384 fproto, MFIB_SOURCE_API);
1385 }
1386
1387 REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
Neale Rannsb8d44812017-11-10 06:53:54 -08001388}
1389
Klement Sekera75e7d132017-09-20 08:26:30 +02001390void
1391vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1392{
1393 vl_api_ip_reassembly_set_reply_t *rmp;
1394 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001395 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001396 {
Klement Sekerade34c352019-06-25 11:19:22 +00001397 case IP_REASS_TYPE_FULL:
1398 if (mp->is_ip6)
1399 {
1400 rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1401 clib_net_to_host_u32
1402 (mp->max_reassemblies),
1403 clib_net_to_host_u32
1404 (mp->max_reassembly_length),
1405 clib_net_to_host_u32
1406 (mp->expire_walk_interval_ms));
1407 }
1408 else
1409 {
1410 rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1411 clib_net_to_host_u32
1412 (mp->max_reassemblies),
1413 clib_net_to_host_u32
1414 (mp->max_reassembly_length),
1415 clib_net_to_host_u32
1416 (mp->expire_walk_interval_ms));
1417 }
1418 break;
1419 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1420 if (mp->is_ip6)
1421 {
1422 rv =
1423 ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1424 clib_net_to_host_u32 (mp->max_reassemblies),
1425 clib_net_to_host_u32
1426 (mp->max_reassembly_length),
1427 clib_net_to_host_u32
1428 (mp->expire_walk_interval_ms));
1429 }
1430 else
1431 {
1432 rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1433 clib_net_to_host_u32 (mp->max_reassemblies),
1434 clib_net_to_host_u32
1435 (mp->max_reassembly_length),
1436 clib_net_to_host_u32
1437 (mp->expire_walk_interval_ms));
1438 }
1439 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001440 }
1441
1442 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1443}
1444
1445void
1446vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1447{
Ole Troan2e1c8962019-04-10 09:44:23 +02001448 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02001449
Ole Troan2e1c8962019-04-10 09:44:23 +02001450 rp = vl_api_client_index_to_registration (mp->client_index);
1451 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02001452 return;
1453
1454 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001455 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02001456 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
1457 rmp->context = mp->context;
1458 rmp->retval = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001459 u32 timeout_ms;
1460 u32 max_reassemblies;
1461 u32 max_reassembly_length;
1462 u32 expire_walk_interval_ms;
1463 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera75e7d132017-09-20 08:26:30 +02001464 {
Klement Sekerade34c352019-06-25 11:19:22 +00001465 case IP_REASS_TYPE_FULL:
1466 if (mp->is_ip6)
1467 {
1468 rmp->is_ip6 = 1;
1469 ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1470 &max_reassembly_length,
1471 &expire_walk_interval_ms);
1472 }
1473 else
1474 {
1475 rmp->is_ip6 = 0;
1476 ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1477 &max_reassembly_length,
1478 &expire_walk_interval_ms);
1479 }
1480 break;
1481 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1482 if (mp->is_ip6)
1483 {
1484 rmp->is_ip6 = 1;
1485 ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1486 &max_reassembly_length, &expire_walk_interval_ms);
1487 }
1488 else
1489 {
1490 rmp->is_ip6 = 0;
1491 ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1492 &max_reassembly_length, &expire_walk_interval_ms);
1493 }
1494 break;
Klement Sekera75e7d132017-09-20 08:26:30 +02001495 }
Matthew Smithb3174df2019-12-03 12:41:53 -06001496 rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1497 rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1498 rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
Klement Sekera75e7d132017-09-20 08:26:30 +02001499 rmp->expire_walk_interval_ms =
Matthew Smithb3174df2019-12-03 12:41:53 -06001500 clib_host_to_net_u32 (expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02001501 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02001502}
1503
Klement Sekera4c533132018-02-22 11:41:12 +01001504void
1505 vl_api_ip_reassembly_enable_disable_t_handler
1506 (vl_api_ip_reassembly_enable_disable_t * mp)
1507{
1508 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1509 int rv = 0;
Klement Sekerade34c352019-06-25 11:19:22 +00001510 switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
Klement Sekera4c533132018-02-22 11:41:12 +01001511 {
Klement Sekerade34c352019-06-25 11:19:22 +00001512 case IP_REASS_TYPE_FULL:
Klement Sekera896c8962019-06-24 11:52:49 +00001513 rv =
Klement Sekerade34c352019-06-25 11:19:22 +00001514 ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1515 mp->enable_ip4);
1516 if (0 == rv)
1517 rv =
1518 ip6_full_reass_enable_disable (clib_net_to_host_u32
1519 (mp->sw_if_index), mp->enable_ip6);
1520 break;
1521 case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1522 rv =
1523 ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1524 mp->enable_ip4);
1525 if (0 == rv)
1526 {
1527 rv =
1528 ip6_sv_reass_enable_disable (clib_net_to_host_u32
1529 (mp->sw_if_index), mp->enable_ip6);
1530 }
1531 break;
Klement Sekera4c533132018-02-22 11:41:12 +01001532 }
1533
Choree7f61d12018-11-28 10:27:58 +03301534 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01001535}
1536
Neale Ranns92207752019-06-03 13:21:40 +00001537typedef struct ip_punt_redirect_walk_ctx_t_
Pavel Kotucek609e1212018-11-27 09:59:44 +01001538{
Neale Ranns92207752019-06-03 13:21:40 +00001539 vl_api_registration_t *reg;
1540 u32 context;
1541} ip_punt_redirect_walk_ctx_t;
1542
1543static walk_rc_t
1544send_ip_punt_redirect_details (u32 rx_sw_if_index,
1545 const ip_punt_redirect_rx_t * ipr, void *arg)
1546{
Neale Ranns92207752019-06-03 13:21:40 +00001547 ip_punt_redirect_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001548 vl_api_ip_punt_redirect_details_t *mp;
Neale Ranns097fa662018-05-01 05:17:55 -07001549 fib_path_encode_ctx_t path_ctx = {
1550 .rpaths = NULL,
1551 };
Pavel Kotucek609e1212018-11-27 09:59:44 +01001552
1553 mp = vl_msg_api_alloc (sizeof (*mp));
1554 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00001555 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001556
1557 clib_memset (mp, 0, sizeof (*mp));
1558 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00001559 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001560
Neale Ranns097fa662018-05-01 05:17:55 -07001561 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
Neale Ranns92207752019-06-03 13:21:40 +00001562
1563 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
Neale Ranns097fa662018-05-01 05:17:55 -07001564 mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
Neale Ranns92207752019-06-03 13:21:40 +00001565
Neale Ranns097fa662018-05-01 05:17:55 -07001566 ip_address_encode (&path_ctx.rpaths[0].frp_addr,
Neale Ranns92207752019-06-03 13:21:40 +00001567 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1568
1569 vl_api_send_msg (ctx->reg, (u8 *) mp);
1570
Neale Ranns097fa662018-05-01 05:17:55 -07001571 vec_free (path_ctx.rpaths);
Neale Ranns92207752019-06-03 13:21:40 +00001572
1573 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001574}
1575
1576static void
1577vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1578{
1579 vl_api_registration_t *reg;
Neale Ranns92207752019-06-03 13:21:40 +00001580 fib_protocol_t fproto;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001581
Pavel Kotucek609e1212018-11-27 09:59:44 +01001582 reg = vl_api_client_index_to_registration (mp->client_index);
1583 if (!reg)
1584 return;
1585
Neale Ranns92207752019-06-03 13:21:40 +00001586 fproto = mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01001587
Neale Ranns92207752019-06-03 13:21:40 +00001588 ip_punt_redirect_walk_ctx_t ctx = {
1589 .reg = reg,
1590 .context = mp->context,
1591 };
1592
1593 if (~0 != mp->sw_if_index)
Pavel Kotucek609e1212018-11-27 09:59:44 +01001594 {
Neale Ranns92207752019-06-03 13:21:40 +00001595 u32 rx_sw_if_index;
1596 index_t pri;
1597
1598 rx_sw_if_index = ntohl (mp->sw_if_index);
1599 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1600
1601 if (INDEX_INVALID == pri)
1602 return;
1603
1604 send_ip_punt_redirect_details (rx_sw_if_index,
1605 ip_punt_redirect_get (pri), &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001606 }
1607 else
Neale Ranns92207752019-06-03 13:21:40 +00001608 ip_punt_redirect_walk (fproto, send_ip_punt_redirect_details, &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01001609}
1610
Dave Barachb5e8a772016-12-06 12:04:42 -05001611#define vl_msg_name_crc_list
1612#include <vnet/ip/ip.api.h>
1613#undef vl_msg_name_crc_list
1614
1615static void
1616setup_message_id_table (api_main_t * am)
1617{
1618#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1619 foreach_vl_msg_name_crc_ip;
1620#undef _
1621}
1622
1623static clib_error_t *
1624ip_api_hookup (vlib_main_t * vm)
1625{
Dave Barach39d69112019-11-27 11:42:13 -05001626 api_main_t *am = vlibapi_get_main ();
Dave Barachb5e8a772016-12-06 12:04:42 -05001627
1628#define _(N,n) \
1629 vl_msg_api_set_handlers(VL_API_##N, #n, \
1630 vl_api_##n##_t_handler, \
1631 vl_noop_handler, \
1632 vl_api_##n##_t_endian, \
1633 vl_api_##n##_t_print, \
1634 sizeof(vl_api_##n##_t), 1);
1635 foreach_ip_api_msg;
1636#undef _
1637
1638 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00001639 * Mark the route add/del API as MP safe
1640 */
Neale Ranns097fa662018-05-01 05:17:55 -07001641 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
1642 am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
Neale Ranns6a231a12018-10-17 06:38:00 +00001643
1644 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05001645 * Set up the (msg_name, crc, message-id) table
1646 */
1647 setup_message_id_table (am);
1648
Dave Barachb5e8a772016-12-06 12:04:42 -05001649 return 0;
1650}
1651
1652VLIB_API_INIT_FUNCTION (ip_api_hookup);
1653
1654/*
1655 * fd.io coding-style-patch-verification: ON
1656 *
1657 * Local Variables:
1658 * eval: (c-set-style "gnu")
1659 * End:
1660 */