blob: bcbcf5ac8d4f2465108c10b0247d0134a61dba5e [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>
John Lo7f358b32018-04-28 01:19:24 -040028#include <vnet/ip/ip_neighbor.h>
Neale Ranns37029302018-08-10 05:30:06 -070029#include <vnet/ip/ip_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050030#include <vnet/ip/ip6_neighbor.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010031#include <vnet/ip/ip_punt_drop.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050032#include <vnet/fib/fib_table.h>
33#include <vnet/fib/fib_api.h>
34#include <vnet/dpo/drop_dpo.h>
35#include <vnet/dpo/receive_dpo.h>
36#include <vnet/dpo/lookup_dpo.h>
37#include <vnet/dpo/classify_dpo.h>
38#include <vnet/dpo/ip_null_dpo.h>
39#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080040#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000041#include <vnet/mfib/ip4_mfib.h>
42#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080043#include <vnet/mfib/mfib_entry.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080044#include <vnet/ip/ip_source_and_port_range_check.h>
45#include <vnet/fib/ip4_fib.h>
46#include <vnet/fib/ip6_fib.h>
Neale Ranns92207752019-06-03 13:21:40 +000047#include <vnet/fib/fib_path_list.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080048#include <vnet/ip/ip6_hop_by_hop.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020049#include <vnet/ip/ip4_reassembly.h>
50#include <vnet/ip/ip6_reassembly.h>
Neale Ranns0053de62018-05-22 08:40:52 -070051#include <vnet/ethernet/arp.h>
Matus Fabian75b9f452018-10-02 23:27:21 -070052#include <vnet/ip/ip_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050053
54#include <vnet/vnet_msg_enum.h>
55
56#define vl_typedefs /* define message structures */
57#include <vnet/vnet_all_api_h.h>
58#undef vl_typedefs
59
60#define vl_endianfun /* define message structures */
61#include <vnet/vnet_all_api_h.h>
62#undef vl_endianfun
63
64/* instantiate all the print functions we know about */
65#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
66#define vl_printfun
67#include <vnet/vnet_all_api_h.h>
68#undef vl_printfun
69
70#include <vlibapi/api_helper_macros.h>
71
Neale Ranns5a8123b2017-01-26 01:18:23 -080072
Dave Barachb5e8a772016-12-06 12:04:42 -050073#define foreach_ip_api_msg \
74_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050075_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080076_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080077_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050078_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000079_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080080_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050081_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070082_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050083_(IP_DUMP, ip_dump) \
84_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080085_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
John Loc7b43042018-04-13 16:46:22 -040086_(IP_PROBE_NEIGHBOR, ip_probe_neighbor) \
John Lo7f358b32018-04-28 01:19:24 -040087_(IP_SCAN_NEIGHBOR_ENABLE_DISABLE, ip_scan_neighbor_enable_disable) \
Neale Rannsb8d44812017-11-10 06:53:54 -080088_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
89_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010090_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080091_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
Neale Ranns0053de62018-05-22 08:40:52 -070092_(PROXY_ARP_DUMP, proxy_arp_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080093_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
Neale Ranns0053de62018-05-22 08:40:52 -070094 _(PROXY_ARP_INTFC_DUMP, proxy_arp_intfc_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080095_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050096_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070097_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070098_(IP_PUNT_POLICE, ip_punt_police) \
99_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -0500100_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
101_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
102_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -0800103_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
104_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100105_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -0500106_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800107_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
Matus Fabian75b9f452018-10-02 23:27:21 -0700108_(IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800109_(IOAM_ENABLE, ioam_enable) \
110_(IOAM_DISABLE, ioam_disable) \
111_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
112 ip_source_and_port_range_check_add_del) \
113_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200114 ip_source_and_port_range_check_interface_add_del) \
Chore3460b012018-11-28 10:53:11 +0330115_(IP_SOURCE_CHECK_INTERFACE_ADD_DEL, \
116 ip_source_check_interface_add_del) \
Klement Sekera75e7d132017-09-20 08:26:30 +0200117_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100118_(IP_REASSEMBLY_GET, ip_reassembly_get) \
Pavel Kotucek609e1212018-11-27 09:59:44 +0100119_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
120_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
121
Dave Barachb5e8a772016-12-06 12:04:42 -0500122
Neale Ranns37029302018-08-10 05:30:06 -0700123static vl_api_ip_neighbor_flags_t
124ip_neighbor_flags_encode (ip_neighbor_flags_t f)
125{
126 vl_api_ip_neighbor_flags_t v = IP_API_NEIGHBOR_FLAG_NONE;
127
128 if (f & IP_NEIGHBOR_FLAG_STATIC)
129 v |= IP_API_NEIGHBOR_FLAG_STATIC;
130 if (f & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)
131 v |= IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY;
132
133 return (clib_host_to_net_u32 (v));
134}
135
Dave Barachb5e8a772016-12-06 12:04:42 -0500136static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600137send_ip_neighbor_details (u32 sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700138 const ip46_address_t * ip_address,
139 const mac_address_t * mac,
140 ip_neighbor_flags_t flags,
141 vl_api_registration_t * reg, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500142{
143 vl_api_ip_neighbor_details_t *mp;
144
145 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400146 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500147 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
148 mp->context = context;
Neale Ranns37029302018-08-10 05:30:06 -0700149 mp->neighbor.sw_if_index = htonl (sw_if_index);
150 mp->neighbor.flags = ip_neighbor_flags_encode (flags);
151
152 ip_address_encode (ip_address, IP46_TYPE_ANY, &mp->neighbor.ip_address);
153 mac_address_encode (mac, mp->neighbor.mac_address);
Dave Barachb5e8a772016-12-06 12:04:42 -0500154
Florin Coras6c4dae22018-01-09 06:39:23 -0800155 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500156}
157
158static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500159vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
160{
Florin Coras6c4dae22018-01-09 06:39:23 -0800161 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500162
Florin Coras6c4dae22018-01-09 06:39:23 -0800163 reg = vl_api_client_index_to_registration (mp->client_index);
164 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500165 return;
166
167 u32 sw_if_index = ntohl (mp->sw_if_index);
168
169 if (mp->is_ipv6)
170 {
171 ip6_neighbor_t *n, *ns;
172
173 ns = ip6_neighbors_entries (sw_if_index);
174 /* *INDENT-OFF* */
175 vec_foreach (n, ns)
176 {
Neale Ranns37029302018-08-10 05:30:06 -0700177 ip46_address_t nh = {
178 .ip6 = {
179 .as_u64[0] = n->key.ip6_address.as_u64[0],
180 .as_u64[1] = n->key.ip6_address.as_u64[1],
181 },
182 };
183 send_ip_neighbor_details (n->key.sw_if_index, &nh,
184 &n->mac, n->flags,
185 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500186 }
187 /* *INDENT-ON* */
188 vec_free (ns);
189 }
190 else
191 {
192 ethernet_arp_ip4_entry_t *n, *ns;
193
194 ns = ip4_neighbor_entries (sw_if_index);
195 /* *INDENT-OFF* */
196 vec_foreach (n, ns)
197 {
Neale Ranns37029302018-08-10 05:30:06 -0700198 ip46_address_t nh = {
199 .ip4 = {
200 .as_u32 = n->ip4_address.as_u32,
201 },
202 };
203
204 send_ip_neighbor_details (n->sw_if_index, &nh,
205 &n->mac, n->flags,
206 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500207 }
208 /* *INDENT-ON* */
209 vec_free (ns);
210 }
211}
212
Dave Barachb5e8a772016-12-06 12:04:42 -0500213static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500214send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800215 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700216 const fib_table_t * table,
217 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500218 fib_route_path_encode_t * api_rpaths, u32 context)
219{
220 vl_api_ip_fib_details_t *mp;
221 fib_route_path_encode_t *api_rpath;
222 vl_api_fib_path_t *fp;
223 int path_count;
224
225 path_count = vec_len (api_rpaths);
226 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
227 if (!mp)
228 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400229 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500230 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
231 mp->context = context;
232
Neale Ranns2297af02017-09-12 09:45:04 -0700233 mp->table_id = htonl (table->ft_table_id);
234 memcpy (mp->table_name, table->ft_desc,
235 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500236 mp->address_length = pfx->fp_len;
237 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
Neale Ranns008dbe12018-09-07 09:32:36 -0700238 mp->stats_index =
239 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500240
241 mp->count = htonl (path_count);
242 fp = mp->path;
243 vec_foreach (api_rpath, api_rpaths)
244 {
Neale Ranns81458422018-03-12 06:59:36 -0700245 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500246 fp++;
247 }
248
Florin Coras6c4dae22018-01-09 06:39:23 -0800249 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500250}
251
Neale Rannsa3af3372017-03-28 03:49:52 -0700252typedef struct vl_api_ip_fib_dump_walk_ctx_t_
253{
254 fib_node_index_t *feis;
255} vl_api_ip_fib_dump_walk_ctx_t;
256
Neale Ranns89541992017-04-06 04:41:02 -0700257static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700258vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
259{
260 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
261
262 vec_add1 (ctx->feis, fei);
263
Neale Ranns89541992017-04-06 04:41:02 -0700264 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700265}
266
Dave Barachb5e8a772016-12-06 12:04:42 -0500267static void
268vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
269{
270 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800271 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500272 ip4_main_t *im = &ip4_main;
273 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700274 fib_node_index_t *lfeip;
Neale Rannsc5d43172018-07-30 08:04:40 -0700275 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500276 u32 fib_index;
277 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700278 vl_api_ip_fib_dump_walk_ctx_t ctx = {
279 .feis = NULL,
280 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500281
Florin Coras6c4dae22018-01-09 06:39:23 -0800282 reg = vl_api_client_index_to_registration (mp->client_index);
283 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500284 return;
285
286 /* *INDENT-OFF* */
287 pool_foreach (fib_table, im->fibs,
288 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700289 fib_table_walk(fib_table->ft_index,
290 FIB_PROTOCOL_IP4,
291 vl_api_ip_fib_dump_walk,
292 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500293 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500294 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500295
Neale Rannsa3af3372017-03-28 03:49:52 -0700296 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500297
Neale Rannsa3af3372017-03-28 03:49:52 -0700298 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500299 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700300 pfx = fib_entry_get_prefix (*lfeip);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500301 fib_index = fib_entry_get_fib_index (*lfeip);
Neale Rannsc5d43172018-07-30 08:04:40 -0700302 fib_table = fib_table_get (fib_index, pfx->fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500303 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304 fib_entry_encode (*lfeip, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700305 send_ip_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500306 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500307 }
308
Neale Rannsa3af3372017-03-28 03:49:52 -0700309 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500310}
311
312static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500313send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800314 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800315 const fib_table_t * table,
316 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500317 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500318{
319 vl_api_ip6_fib_details_t *mp;
320 fib_route_path_encode_t *api_rpath;
321 vl_api_fib_path_t *fp;
322 int path_count;
323
Dave Barachd7cb1b52016-12-09 09:52:16 -0500324 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500325 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
326 if (!mp)
327 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400328 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500329 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
330 mp->context = context;
331
Neale Rannsa161a6d2017-11-14 08:10:41 -0800332 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500333 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800335 memcpy (mp->table_name, table->ft_desc,
336 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Neale Ranns008dbe12018-09-07 09:32:36 -0700337 mp->stats_index =
338 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500339
340 mp->count = htonl (path_count);
341 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500342 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500343 {
Neale Ranns81458422018-03-12 06:59:36 -0700344 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500345 fp++;
346 }
347
Florin Coras6c4dae22018-01-09 06:39:23 -0800348 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500349}
350
Dave Barachd7cb1b52016-12-09 09:52:16 -0500351typedef struct apt_ip6_fib_show_ctx_t_
352{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500354} api_ip6_fib_show_ctx_t;
355
Neale Ranns5c1f5592019-01-25 00:51:09 -0800356static fib_table_walk_rc_t
357api_ip6_fib_table_put_entries (fib_node_index_t fei, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500358{
359 api_ip6_fib_show_ctx_t *ctx = arg;
360
Neale Ranns5c1f5592019-01-25 00:51:09 -0800361 vec_add1 (ctx->entries, fei);
362
363 return (FIB_TABLE_WALK_CONTINUE);
Dave Barachb5e8a772016-12-06 12:04:42 -0500364}
365
366static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800367api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 vl_api_ip6_fib_dump_t * mp,
369 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500370{
371 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500372 fib_node_index_t *fib_entry_index;
373 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700374 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500375 };
376 fib_route_path_encode_t *api_rpaths;
Neale Rannsc5d43172018-07-30 08:04:40 -0700377 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500378
Neale Ranns5c1f5592019-01-25 00:51:09 -0800379 ip6_fib_table_walk (fib_table->ft_index,
380 api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500381
Dave Barachd7cb1b52016-12-09 09:52:16 -0500382 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500383
Dave Barachd7cb1b52016-12-09 09:52:16 -0500384 vec_foreach (fib_entry_index, ctx.entries)
385 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700386 pfx = fib_entry_get_prefix (*fib_entry_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387 api_rpaths = NULL;
388 fib_entry_encode (*fib_entry_index, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700389 send_ip6_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390 vec_free (api_rpaths);
391 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500392
Dave Barachd7cb1b52016-12-09 09:52:16 -0500393 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500394}
395
396static void
397vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
398{
Florin Coras6c4dae22018-01-09 06:39:23 -0800399 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500400 ip6_main_t *im6 = &ip6_main;
401 fib_table_t *fib_table;
402
Florin Coras6c4dae22018-01-09 06:39:23 -0800403 reg = vl_api_client_index_to_registration (mp->client_index);
404 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500405 return;
406
407 /* *INDENT-OFF* */
408 pool_foreach (fib_table, im6->fibs,
409 ({
Neale Ranns81458422018-03-12 06:59:36 -0700410 /* don't send link locals */
411 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
412 continue;
413
Florin Coras6c4dae22018-01-09 06:39:23 -0800414 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500415 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500416 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500417}
418
419static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800420send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800421 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800422{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800423 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800424 vl_api_ip_mfib_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -0800425 const mfib_prefix_t *pfx;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800426 mfib_entry_t *mfib_entry;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800427 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800428 int path_count;
429
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800430 mfib_entry = mfib_entry_get (mfei);
Neale Ranns9e829a82018-12-17 05:50:32 -0800431 pfx = mfib_entry_get_prefix (mfei);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800432 mfib_entry_encode (mfei, &api_rpaths);
433
Neale Ranns5a8123b2017-01-26 01:18:23 -0800434 path_count = vec_len (api_rpaths);
435 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
436 if (!mp)
437 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400438 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700439 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800440 mp->context = context;
441
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800442 mp->rpf_id = mfib_entry->mfe_rpf_id;
443 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800444 mp->table_id = htonl (table_id);
Neale Ranns9e829a82018-12-17 05:50:32 -0800445 mp->address_length = pfx->fp_len;
446 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip4,
447 sizeof (pfx->fp_grp_addr.ip4));
448 memcpy (mp->src_address, &pfx->fp_src_addr.ip4,
449 sizeof (pfx->fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800450
451 mp->count = htonl (path_count);
452 fp = mp->path;
453 vec_foreach (api_rpath, api_rpaths)
454 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800455 fib_api_path_encode (api_rpath, &fp->path);
456 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800457 fp++;
458 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800459 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800460
Florin Coras6c4dae22018-01-09 06:39:23 -0800461 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800462}
463
464typedef struct vl_api_ip_mfib_dump_ctc_t_
465{
466 fib_node_index_t *entries;
467} vl_api_ip_mfib_dump_ctc_t;
468
469static int
470vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
471{
472 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
473
474 vec_add1 (ctx->entries, fei);
475
476 return (0);
477}
478
479static void
480vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
481{
Florin Coras6c4dae22018-01-09 06:39:23 -0800482 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800483 ip4_main_t *im = &ip4_main;
484 mfib_table_t *mfib_table;
485 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800486 vl_api_ip_mfib_dump_ctc_t ctx = {
487 .entries = NULL,
488 };
489
Florin Coras6c4dae22018-01-09 06:39:23 -0800490 reg = vl_api_client_index_to_registration (mp->client_index);
491 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800492 return;
493
Neale Ranns5a8123b2017-01-26 01:18:23 -0800494 /* *INDENT-OFF* */
495 pool_foreach (mfib_table, im->mfibs,
496 ({
497 ip4_mfib_table_walk(&mfib_table->v4,
498 vl_api_ip_mfib_table_dump_walk,
499 &ctx);
500
501 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
502
503 vec_foreach (mfeip, ctx.entries)
504 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800505 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800506 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800507 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800508 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800509 vec_reset_length (ctx.entries);
510
511 }));
512 /* *INDENT-ON* */
513
514 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800515}
516
517static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800518send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800519 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800520 u32 table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800521 const mfib_prefix_t * pfx,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800522 fib_route_path_encode_t * api_rpaths, u32 context)
523{
524 vl_api_ip6_mfib_details_t *mp;
525 fib_route_path_encode_t *api_rpath;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800526 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800527 int path_count;
528
529 path_count = vec_len (api_rpaths);
530 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
531 if (!mp)
532 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400533 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700534 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800535 mp->context = context;
536
537 mp->table_id = htonl (table_id);
538 mp->address_length = pfx->fp_len;
539 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
540 sizeof (pfx->fp_grp_addr.ip6));
541 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
542 sizeof (pfx->fp_src_addr.ip6));
543
544 mp->count = htonl (path_count);
545 fp = mp->path;
546 vec_foreach (api_rpath, api_rpaths)
547 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800548 fib_api_path_encode (api_rpath, &fp->path);
549 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800550 fp++;
551 }
552
Florin Coras6c4dae22018-01-09 06:39:23 -0800553 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800554}
555
556typedef struct vl_api_ip6_mfib_dump_ctc_t_
557{
558 fib_node_index_t *entries;
559} vl_api_ip6_mfib_dump_ctc_t;
560
561static int
562vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
563{
564 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
565
566 vec_add1 (ctx->entries, fei);
567
568 return (0);
569}
570
571static void
572vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
573{
574 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800575 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800576 ip6_main_t *im = &ip6_main;
577 mfib_table_t *mfib_table;
Neale Ranns9e829a82018-12-17 05:50:32 -0800578 const mfib_prefix_t *pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800579 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800580 fib_route_path_encode_t *api_rpaths = NULL;
581 vl_api_ip6_mfib_dump_ctc_t ctx = {
582 .entries = NULL,
583 };
584
Florin Coras6c4dae22018-01-09 06:39:23 -0800585 reg = vl_api_client_index_to_registration (mp->client_index);
586 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800587 return;
588
589
590 /* *INDENT-OFF* */
591 pool_foreach (mfib_table, im->mfibs,
592 ({
593 ip6_mfib_table_walk(&mfib_table->v6,
594 vl_api_ip6_mfib_table_dump_walk,
595 &ctx);
596
597 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
598
599 vec_foreach(mfeip, ctx.entries)
600 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800601 pfx = mfib_entry_get_prefix (*mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800602 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800603 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800604 mfib_table->mft_table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800605 pfx, api_rpaths,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800606 mp->context);
607 }
608 vec_reset_length (api_rpaths);
609 vec_reset_length (ctx.entries);
610
611 }));
612 /* *INDENT-ON* */
613
614 vec_free (ctx.entries);
615 vec_free (api_rpaths);
616}
617
618static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700619vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
620 vlib_main_t * vm)
621{
622 vl_api_ip_punt_police_reply_t *rmp;
623 int rv = 0;
624
625 if (mp->is_ip6)
626 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
627 else
628 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
629
630 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
631}
632
633static void
634vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
635 vlib_main_t * vm)
636{
637 vl_api_ip_punt_redirect_reply_t *rmp;
638 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100639 ip46_type_t ipv;
640 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700641
Pavel Kotucek609e1212018-11-27 09:59:44 +0100642 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
643 goto bad_sw_if_index;
644
645 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700646 if (mp->is_add)
647 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100648 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700649 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100650 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
651 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700652 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100653 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700654 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100655 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
656 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700657 }
658 }
659 else
660 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100661 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700662 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100663 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700664 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100665 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700666 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100667 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700668 }
669 }
670
Pavel Kotucek609e1212018-11-27 09:59:44 +0100671 BAD_SW_IF_INDEX_LABEL;
672
Neale Rannsd91c1db2017-07-31 02:30:50 -0700673 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
674}
675
Neale Ranns37029302018-08-10 05:30:06 -0700676static ip_neighbor_flags_t
677ip_neighbor_flags_decode (vl_api_ip_neighbor_flags_t v)
678{
679 ip_neighbor_flags_t f = IP_NEIGHBOR_FLAG_NONE;
680
681 v = clib_net_to_host_u32 (v);
682
683 if (v & IP_API_NEIGHBOR_FLAG_STATIC)
684 f |= IP_NEIGHBOR_FLAG_STATIC;
685 if (v & IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY)
686 f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
687
688 return (f);
689}
690
Neale Rannsd91c1db2017-07-31 02:30:50 -0700691static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500692vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
693 vlib_main_t * vm)
694{
695 vl_api_ip_neighbor_add_del_reply_t *rmp;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700696 ip_neighbor_flags_t flags;
Neale Ranns14260392018-09-28 05:00:57 -0700697 u32 stats_index = ~0;
Neale Ranns37029302018-08-10 05:30:06 -0700698 ip46_address_t ip;
699 mac_address_t mac;
700 ip46_type_t type;
Dave Barachb5e8a772016-12-06 12:04:42 -0500701 int rv = 0;
702
Neale Ranns37029302018-08-10 05:30:06 -0700703 VALIDATE_SW_IF_INDEX ((&mp->neighbor));
Dave Barachb5e8a772016-12-06 12:04:42 -0500704
Neale Ranns37029302018-08-10 05:30:06 -0700705 flags = ip_neighbor_flags_decode (mp->neighbor.flags);
706 type = ip_address_decode (&mp->neighbor.ip_address, &ip);
707 mac_address_decode (mp->neighbor.mac_address, &mac);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700708
Neale Ranns37029302018-08-10 05:30:06 -0700709 /*
710 * there's no validation here of the ND/ARP entry being added.
711 * The expectation is that the FIB will ensure that nothing bad
712 * will come of adding bogus entries.
713 */
Neale Ranns0bdd3192018-09-07 11:04:52 -0700714 if (mp->is_add)
Neale Ranns37029302018-08-10 05:30:06 -0700715 rv = ip_neighbor_add (&ip, type, &mac,
716 ntohl (mp->neighbor.sw_if_index),
717 flags, &stats_index);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700718 else
Neale Ranns37029302018-08-10 05:30:06 -0700719 rv = ip_neighbor_del (&ip, type, ntohl (mp->neighbor.sw_if_index));
Dave Barachb5e8a772016-12-06 12:04:42 -0500720
Neale Ranns8edad032017-10-09 05:26:13 -0700721 BAD_SW_IF_INDEX_LABEL;
Neale Ranns14260392018-09-28 05:00:57 -0700722
723 /* *INDENT-OFF* */
724 REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
725 ({
726 rmp->stats_index = htonl (stats_index);
727 }));
728 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500729}
730
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700731void
Neale Ranns15002542017-09-10 04:39:11 -0700732ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
733{
734 u32 fib_index, mfib_index;
735
736 /*
737 * ignore action on the default table - this is always present
738 * and cannot be added nor deleted from the API
739 */
740 if (0 != table_id)
741 {
742 /*
743 * The API holds only one lock on the table.
744 * i.e. it can be added many times via the API but needs to be
745 * deleted only once.
746 * The FIB index for unicast and multicast is not necessarily the
747 * same, since internal VPP systesm (like LISP and SR) create
748 * their own unicast tables.
749 */
750 fib_index = fib_table_find (fproto, table_id);
751 mfib_index = mfib_table_find (fproto, table_id);
752
753 if (~0 != fib_index)
754 {
755 fib_table_unlock (fib_index, fproto,
756 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
757 }
758 if (~0 != mfib_index)
759 {
760 mfib_table_unlock (mfib_index, fproto,
761 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
762 }
763 }
764}
765
766void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700767vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
768{
769 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700770 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
771 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700772 int rv = 0;
773
Neale Ranns15002542017-09-10 04:39:11 -0700774 if (mp->is_add)
775 {
Neale Ranns2297af02017-09-12 09:45:04 -0700776 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700777 }
778 else
779 {
780 ip_table_delete (fproto, table_id, 1);
781 }
782
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700783 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
784}
785
Dave Barachb5e8a772016-12-06 12:04:42 -0500786int
787add_del_route_t_handler (u8 is_multipath,
788 u8 is_add,
789 u8 is_drop,
790 u8 is_unreach,
791 u8 is_prohibit,
792 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800793 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500794 u8 is_classify,
795 u32 classify_table_index,
796 u8 is_resolve_host,
797 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800798 u8 is_interface_rx,
799 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800800 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700801 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800802 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500803 u32 fib_index,
804 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700805 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500806 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800807 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500808 u32 next_hop_sw_if_index,
809 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700810 u16 next_hop_weight,
811 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500812 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800813 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500814{
815 vnet_classify_main_t *cm = &vnet_classify_main;
816 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
817 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700818 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500819 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
820 .frp_sw_if_index = next_hop_sw_if_index,
821 .frp_fib_index = next_hop_fib_index,
822 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700823 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500824 .frp_label_stack = next_hop_out_label_stack,
825 };
826 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800827 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500828
Neale Rannscaac3502017-09-18 18:04:10 -0700829 /*
Jim Thompsonf324dec2019-04-08 03:22:21 -0500830 * the special INVALID label means we are not recursing via a
Neale Rannscaac3502017-09-18 18:04:10 -0700831 * label. Exp-null value is never a valid via-label so that
832 * also means it's not a via-label and means clients that set
833 * it to 0 by default get the expected behaviour
834 */
835 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500836 {
Neale Rannsda78f952017-05-24 09:15:43 -0700837 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500838 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800839 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500840 }
Neale Ranns775f73c2018-12-20 03:01:49 -0800841 if (is_local)
Neale Ranns7425f922019-01-23 00:36:16 -0800842 {
843 path_flags |= FIB_ROUTE_PATH_LOCAL;
844 if (~0 != next_hop_sw_if_index)
845 {
846 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
847 }
848 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800849 if (is_dvr)
850 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500851 if (is_resolve_host)
852 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
853 if (is_resolve_attached)
854 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800855 if (is_interface_rx)
856 path_flags |= FIB_ROUTE_PATH_INTF_RX;
857 if (is_rpf_id)
858 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700859 if (is_source_lookup)
860 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800861 if (is_multicast)
862 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800863 if (is_udp_encap)
864 {
865 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
866 path.frp_udp_encap_id = next_hop_id;
867 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800868 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
869 && path.frp_fib_index != ~0)
870 {
871 path_flags |= FIB_ROUTE_PATH_DEAG;
872 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500873
874 path.frp_flags = path_flags;
875
Neale Ranns7425f922019-01-23 00:36:16 -0800876 if (is_drop || (is_local && (~0 == next_hop_sw_if_index)) ||
877 is_classify || is_unreach || is_prohibit)
Dave Barachb5e8a772016-12-06 12:04:42 -0500878 {
879 /*
880 * special route types that link directly to the adj
881 */
882 if (is_add)
883 {
884 dpo_id_t dpo = DPO_INVALID;
885 dpo_proto_t dproto;
886
887 dproto = fib_proto_to_dpo (prefix->fp_proto);
888
889 if (is_drop)
890 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
891 else if (is_local)
892 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
893 else if (is_unreach)
894 ip_null_dpo_add_and_lock (dproto,
895 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
896 else if (is_prohibit)
897 ip_null_dpo_add_and_lock (dproto,
898 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
899 &dpo);
900 else if (is_classify)
901 {
902 if (pool_is_free_index (cm->tables,
903 ntohl (classify_table_index)))
904 {
Dave Barachb5e8a772016-12-06 12:04:42 -0500905 return VNET_API_ERROR_NO_SUCH_TABLE;
906 }
907
908 dpo_set (&dpo, DPO_CLASSIFY, dproto,
909 classify_dpo_create (dproto,
910 ntohl (classify_table_index)));
911 }
912 else
913 {
Dave Barachb5e8a772016-12-06 12:04:42 -0500914 return VNET_API_ERROR_NO_SUCH_TABLE;
915 }
916
917 fib_table_entry_special_dpo_update (fib_index,
918 prefix,
919 FIB_SOURCE_API,
920 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
921 dpo_reset (&dpo);
922 }
923 else
924 {
925 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
926 }
927 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700928 else if (is_multipath)
929 {
930 vec_add1 (paths, path);
931
932 if (is_add)
933 fib_table_entry_path_add2 (fib_index,
934 prefix,
935 FIB_SOURCE_API, entry_flags, paths);
936 else
937 fib_table_entry_path_remove2 (fib_index,
938 prefix, FIB_SOURCE_API, paths);
939
940 vec_free (paths);
941 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500942 else
943 {
944 if (is_add)
945 {
946 vec_add1 (paths, path);
947 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800948 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500949 vec_free (paths);
950 }
951 else
952 {
953 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
954 }
955 }
956
Dave Barachb5e8a772016-12-06 12:04:42 -0500957 return (0);
958}
959
960int
961add_del_route_check (fib_protocol_t table_proto,
962 u32 table_id,
963 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700964 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500965 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800966 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500967{
968 vnet_main_t *vnm = vnet_get_main ();
969
970 *fib_index = fib_table_find (table_proto, ntohl (table_id));
971 if (~0 == *fib_index)
972 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800973 /* No such VRF, and we weren't asked to create one */
974 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500975 }
976
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800977 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500978 {
979 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
980 ntohl (next_hop_sw_if_index)))
981 {
982 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
983 }
984 }
985 else
986 {
Neale Rannsda78f952017-05-24 09:15:43 -0700987 fib_protocol_t fib_nh_proto;
988
989 if (next_hop_table_proto > DPO_PROTO_MPLS)
990 return (0);
991
992 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
993
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800994 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700995 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800996 ntohl (next_hop_table_id));
997 else
Neale Rannsda78f952017-05-24 09:15:43 -0700998 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800999 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001000
1001 if (~0 == *next_hop_fib_index)
1002 {
Neale Rannsb93078d2018-01-25 07:48:12 -08001003 /* No such VRF, and we weren't asked to create one */
1004 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -05001005 }
1006 }
1007
1008 return (0);
1009}
1010
1011static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001012ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1013 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001014{
1015 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -08001016 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001017 int rv, ii, n_labels;;
1018
1019 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1020 mp->table_id,
1021 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001022 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001023 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001024 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001025
1026 if (0 != rv)
1027 return (rv);
1028
1029 fib_prefix_t pfx = {
1030 .fp_len = mp->dst_address_length,
1031 .fp_proto = FIB_PROTOCOL_IP4,
1032 };
1033 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1034
1035 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001036 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001037 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1038
1039 n_labels = mp->next_hop_n_out_labels;
1040 if (n_labels == 0)
1041 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001042 else
1043 {
1044 vec_validate (label_stack, n_labels - 1);
1045 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001046 {
1047 label_stack[ii].fml_value =
1048 ntohl (mp->next_hop_out_label_stack[ii].label);
1049 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1050 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1051 label_stack[ii].fml_mode =
1052 (mp->next_hop_out_label_stack[ii].is_uniform ?
1053 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1054 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001055 }
1056
Neale Ranns008dbe12018-09-07 09:32:36 -07001057 rv = add_del_route_t_handler (mp->is_multipath,
1058 mp->is_add,
1059 mp->is_drop,
1060 mp->is_unreach,
1061 mp->is_prohibit,
1062 mp->is_local, 0,
1063 mp->is_classify,
1064 mp->classify_table_index,
1065 mp->is_resolve_host,
1066 mp->is_resolve_attached, 0, 0,
1067 mp->is_dvr,
1068 mp->is_source_lookup,
1069 mp->is_udp_encap,
1070 fib_index, &pfx, DPO_PROTO_IP4,
1071 &nh,
1072 ntohl (mp->next_hop_id),
1073 ntohl (mp->next_hop_sw_if_index),
1074 next_hop_fib_index,
1075 mp->next_hop_weight,
1076 mp->next_hop_preference,
1077 ntohl (mp->next_hop_via_label), label_stack);
1078
1079 if (mp->is_add && 0 == rv)
1080 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1081
1082 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001083}
1084
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001085static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001086ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1087 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001088{
Neale Ranns31ed7442018-02-23 05:29:09 -08001089 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001090 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001091 int rv, ii, n_labels;;
1092
1093 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1094 mp->table_id,
1095 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001096 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001097 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001098 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001099
1100 if (0 != rv)
1101 return (rv);
1102
1103 fib_prefix_t pfx = {
1104 .fp_len = mp->dst_address_length,
1105 .fp_proto = FIB_PROTOCOL_IP6,
1106 };
1107 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1108
1109 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001110 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001111 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1112
1113 n_labels = mp->next_hop_n_out_labels;
1114 if (n_labels == 0)
1115 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001116 else
1117 {
1118 vec_validate (label_stack, n_labels - 1);
1119 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001120 {
1121 label_stack[ii].fml_value =
1122 ntohl (mp->next_hop_out_label_stack[ii].label);
1123 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1124 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1125 label_stack[ii].fml_mode =
1126 (mp->next_hop_out_label_stack[ii].is_uniform ?
1127 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1128 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001129 }
1130
Neale Ranns008dbe12018-09-07 09:32:36 -07001131 rv = add_del_route_t_handler (mp->is_multipath,
1132 mp->is_add,
1133 mp->is_drop,
1134 mp->is_unreach,
1135 mp->is_prohibit,
1136 mp->is_local, 0,
1137 mp->is_classify,
1138 mp->classify_table_index,
1139 mp->is_resolve_host,
1140 mp->is_resolve_attached, 0, 0,
1141 mp->is_dvr,
1142 mp->is_source_lookup,
1143 mp->is_udp_encap,
1144 fib_index, &pfx, DPO_PROTO_IP6,
1145 &nh, ntohl (mp->next_hop_id),
1146 ntohl (mp->next_hop_sw_if_index),
1147 next_hop_fib_index,
1148 mp->next_hop_weight,
1149 mp->next_hop_preference,
1150 ntohl (mp->next_hop_via_label), label_stack);
1151
1152 if (mp->is_add && 0 == rv)
1153 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1154
1155 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001156}
1157
1158void
1159vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1160{
1161 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001162 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001163 int rv;
1164 vnet_main_t *vnm = vnet_get_main ();
1165
1166 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001167 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001168
1169 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001170 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001171 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001172 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001173
1174 rv = (rv == 0) ? vnm->api_errno : rv;
1175
Neale Ranns008dbe12018-09-07 09:32:36 -07001176 /* *INDENT-OFF* */
1177 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1178 ({
1179 rmp->stats_index = htonl (stats_index);
1180 }))
1181 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001182}
1183
Neale Ranns15002542017-09-10 04:39:11 -07001184void
Neale Ranns2297af02017-09-12 09:45:04 -07001185ip_table_create (fib_protocol_t fproto,
1186 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001187{
1188 u32 fib_index, mfib_index;
1189
1190 /*
1191 * ignore action on the default table - this is always present
1192 * and cannot be added nor deleted from the API
1193 */
1194 if (0 != table_id)
1195 {
1196 /*
1197 * The API holds only one lock on the table.
1198 * i.e. it can be added many times via the API but needs to be
1199 * deleted only once.
1200 * The FIB index for unicast and multicast is not necessarily the
1201 * same, since internal VPP systesm (like LISP and SR) create
1202 * their own unicast tables.
1203 */
1204 fib_index = fib_table_find (fproto, table_id);
1205 mfib_index = mfib_table_find (fproto, table_id);
1206
1207 if (~0 == fib_index)
1208 {
Neale Ranns2297af02017-09-12 09:45:04 -07001209 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1210 (is_api ?
1211 FIB_SOURCE_API :
1212 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001213 }
1214 if (~0 == mfib_index)
1215 {
Neale Ranns2297af02017-09-12 09:45:04 -07001216 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1217 (is_api ?
1218 MFIB_SOURCE_API :
1219 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001220 }
1221 }
1222}
1223
Neale Ranns32e1c012016-11-22 17:07:28 +00001224static int
1225add_del_mroute_check (fib_protocol_t table_proto,
1226 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001227 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001228{
1229 vnet_main_t *vnm = vnet_get_main ();
1230
1231 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1232 if (~0 == *fib_index)
1233 {
Neale Ranns15002542017-09-10 04:39:11 -07001234 /* No such table */
1235 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001236 }
1237
1238 if (~0 != ntohl (next_hop_sw_if_index))
1239 {
1240 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1241 ntohl (next_hop_sw_if_index)))
1242 {
1243 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1244 }
1245 }
1246
1247 return (0);
1248}
1249
Neale Ranns28c142e2018-09-07 09:37:07 -07001250static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001251mroute_add_del_handler (u8 is_add,
1252 u8 is_local,
1253 u32 fib_index,
1254 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001255 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001256 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001257 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001258 u32 next_hop_sw_if_index,
1259 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001260{
Neale Ranns28c142e2018-09-07 09:37:07 -07001261 fib_node_index_t mfib_entry_index = ~0;
1262
Neale Ranns32e1c012016-11-22 17:07:28 +00001263 fib_route_path_t path = {
1264 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001265 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001266 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001267 };
1268
1269 if (is_local)
1270 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1271
Neale Rannsd792d9c2017-10-21 10:53:20 -07001272 if (DPO_PROTO_BIER == nh_proto)
1273 {
1274 path.frp_bier_imp = bier_imp;
1275 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1276 }
1277 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001278 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001279 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1280 MFIB_SOURCE_API,
1281 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001282 goto done;
1283 }
1284
1285 if (is_add)
1286 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001287 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1288 MFIB_SOURCE_API,
1289 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001290 }
1291 else
1292 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001293 mfib_table_entry_path_remove (fib_index, prefix,
1294 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001295 }
1296
Neale Rannsd792d9c2017-10-21 10:53:20 -07001297done:
Neale Ranns28c142e2018-09-07 09:37:07 -07001298 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001299}
1300
1301static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001302api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1303 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001304{
Neale Ranns28c142e2018-09-07 09:37:07 -07001305 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001306 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001307 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001308 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001309 u32 fib_index;
1310 int rv;
1311
Neale Rannsd792d9c2017-10-21 10:53:20 -07001312 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001313 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1314 rv = add_del_mroute_check (fproto,
1315 mp->table_id,
1316 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001317 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001318
1319 if (0 != rv)
1320 return (rv);
1321
1322 mfib_prefix_t pfx = {
1323 .fp_len = ntohs (mp->grp_address_length),
1324 .fp_proto = fproto,
1325 };
1326
1327 if (FIB_PROTOCOL_IP4 == fproto)
1328 {
1329 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1330 sizeof (pfx.fp_grp_addr.ip4));
1331 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1332 sizeof (pfx.fp_src_addr.ip4));
Dave Barachb7b92992018-10-17 10:38:51 -04001333 clib_memset (&nh.ip6, 0, sizeof (nh.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001334 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001335 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1336 pfx.fp_len = 64;
Neale Ranns32e1c012016-11-22 17:07:28 +00001337 }
1338 else
1339 {
1340 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1341 sizeof (pfx.fp_grp_addr.ip6));
1342 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1343 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001344 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001345 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1346 pfx.fp_len = 256;
Neale Ranns32e1c012016-11-22 17:07:28 +00001347 }
1348
Neale Ranns28c142e2018-09-07 09:37:07 -07001349 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1350 mp->is_local,
1351 fib_index, &pfx,
1352 nh_proto,
1353 ntohl (mp->entry_flags),
1354 ntohl (mp->rpf_id),
1355 ntohl (mp->next_hop_sw_if_index),
1356 &nh,
1357 ntohl (mp->itf_flags),
1358 ntohl (mp->bier_imp));
1359
1360 if (~0 != mfib_entry_index)
1361 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1362
1363 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001364}
1365
1366void
1367vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1368{
1369 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001370 vnet_main_t *vnm;
1371 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001372 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001373
Neale Ranns28c142e2018-09-07 09:37:07 -07001374 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001375 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001376 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001377
Neale Ranns28c142e2018-09-07 09:37:07 -07001378 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001379
Neale Ranns28c142e2018-09-07 09:37:07 -07001380 /* *INDENT-OFF* */
1381 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1382 ({
1383 rmp->stats_index = htonl (stats_index);
1384 }));
1385 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001386}
1387
Dave Barachb5e8a772016-12-06 12:04:42 -05001388static void
1389send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001390 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1391 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001392{
1393 vl_api_ip_details_t *mp;
1394
1395 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001396 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001397 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1398
1399 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001400 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001401 mp->context = context;
1402
Florin Coras6c4dae22018-01-09 06:39:23 -08001403 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001404}
1405
1406static void
1407send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001408 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001409 u8 * ip, u16 prefix_length,
1410 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001411{
1412 vl_api_ip_address_details_t *mp;
1413
1414 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001415 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001416 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1417
1418 if (is_ipv6)
1419 {
1420 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1421 }
1422 else
1423 {
1424 u32 *tp = (u32 *) mp->ip;
1425 *tp = *(u32 *) ip;
1426 }
1427 mp->prefix_length = prefix_length;
1428 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001429 mp->sw_if_index = htonl (sw_if_index);
1430 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001431
Florin Coras6c4dae22018-01-09 06:39:23 -08001432 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001433}
1434
1435static void
1436vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1437{
1438 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001439 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001440 ip6_address_t *r6;
1441 ip4_address_t *r4;
1442 ip6_main_t *im6 = &ip6_main;
1443 ip4_main_t *im4 = &ip4_main;
1444 ip_lookup_main_t *lm6 = &im6->lookup_main;
1445 ip_lookup_main_t *lm4 = &im4->lookup_main;
1446 ip_interface_address_t *ia = 0;
1447 u32 sw_if_index = ~0;
1448 int rv __attribute__ ((unused)) = 0;
1449
1450 VALIDATE_SW_IF_INDEX (mp);
1451
1452 sw_if_index = ntohl (mp->sw_if_index);
1453
Florin Coras6c4dae22018-01-09 06:39:23 -08001454 reg = vl_api_client_index_to_registration (mp->client_index);
1455 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001456 return;
1457
Dave Barachb5e8a772016-12-06 12:04:42 -05001458 if (mp->is_ipv6)
1459 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001460 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001461 /* Do not send subnet details of the IP-interface for
1462 * unnumbered interfaces. otherwise listening clients
1463 * will be confused that the subnet is applied on more
1464 * than one interface */
1465 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001466 ({
1467 r6 = ip_interface_address_get_address (lm6, ia);
1468 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001469 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001470 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001471 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001472 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001473 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001474 else
1475 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001476 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001477 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001478 ({
1479 r4 = ip_interface_address_get_address (lm4, ia);
1480 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001481 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001482 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001483 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001484 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001485 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001486
Dave Barachb5e8a772016-12-06 12:04:42 -05001487 BAD_SW_IF_INDEX_LABEL;
1488}
1489
1490static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001491send_ip_unnumbered_details (vpe_api_main_t * am,
1492 vl_api_registration_t * reg,
1493 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1494{
1495 vl_api_ip_unnumbered_details_t *mp;
1496
1497 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001498 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001499 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1500
1501 mp->context = context;
1502 mp->sw_if_index = htonl (sw_if_index);
1503 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1504
1505 vl_api_send_msg (reg, (u8 *) mp);
1506}
1507
1508static void
1509vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1510{
1511 vnet_main_t *vnm = vnet_get_main ();
1512 vnet_interface_main_t *im = &vnm->interface_main;
1513 int rv __attribute__ ((unused)) = 0;
1514 vpe_api_main_t *am = &vpe_api_main;
1515 vl_api_registration_t *reg;
1516 vnet_sw_interface_t *si;
1517 u32 sw_if_index;
1518
1519 sw_if_index = ntohl (mp->sw_if_index);
1520
1521 reg = vl_api_client_index_to_registration (mp->client_index);
1522 if (!reg)
1523 return;
1524
1525 if (~0 != sw_if_index)
1526 {
1527 VALIDATE_SW_IF_INDEX (mp);
1528
1529 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1530
1531 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1532 {
1533 send_ip_unnumbered_details (am, reg,
1534 sw_if_index,
1535 si->unnumbered_sw_if_index,
1536 mp->context);
1537 }
1538 }
1539 else
1540 {
1541 /* *INDENT-OFF* */
1542 pool_foreach (si, im->sw_interfaces,
1543 ({
1544 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1545 {
1546 send_ip_unnumbered_details(am, reg,
1547 si->sw_if_index,
1548 si->unnumbered_sw_if_index,
1549 mp->context);
1550 }
1551 }));
1552 /* *INDENT-ON* */
1553 }
1554
1555 BAD_SW_IF_INDEX_LABEL;
1556}
1557
1558static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001559vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1560{
1561 vpe_api_main_t *am = &vpe_api_main;
1562 vnet_main_t *vnm = vnet_get_main ();
1563 vlib_main_t *vm = vlib_get_main ();
1564 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001565 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001566 vnet_sw_interface_t *si, *sorted_sis;
1567 u32 sw_if_index = ~0;
1568
Florin Coras6c4dae22018-01-09 06:39:23 -08001569 reg = vl_api_client_index_to_registration (mp->client_index);
1570 if (!reg)
1571 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001572
1573 /* Gather interfaces. */
1574 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1575 _vec_len (sorted_sis) = 0;
1576 /* *INDENT-OFF* */
1577 pool_foreach (si, im->sw_interfaces,
1578 ({
1579 vec_add1 (sorted_sis, si[0]);
1580 }));
1581 /* *INDENT-ON* */
1582
1583 vec_foreach (si, sorted_sis)
1584 {
1585 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1586 {
1587 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1588 {
1589 continue;
1590 }
1591 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001592 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001593 }
1594 }
1595}
1596
1597static void
1598set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1599{
1600 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001601 int rv;
1602 u32 table_id;
1603 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001604
Neale Ranns227038a2017-04-21 01:07:59 -07001605 table_id = ntohl (mp->vrf_id);
1606
1607#define _(a,b) if (mp->a) flow_hash_config |= b;
1608 foreach_flow_hash_bit;
1609#undef _
1610
1611 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001612
1613 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1614}
1615
1616static void
1617set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1618{
1619 vl_api_set_ip_flow_hash_reply_t *rmp;
1620 int rv;
1621 u32 table_id;
1622 flow_hash_config_t flow_hash_config = 0;
1623
1624 table_id = ntohl (mp->vrf_id);
1625
1626#define _(a,b) if (mp->a) flow_hash_config |= b;
1627 foreach_flow_hash_bit;
1628#undef _
1629
1630 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1631
1632 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1633}
1634
1635
1636static void
1637vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1638{
1639 if (mp->is_ipv6 == 0)
1640 set_ip4_flow_hash (mp);
1641 else
1642 set_ip6_flow_hash (mp);
1643}
1644
1645static void
1646 vl_api_sw_interface_ip6nd_ra_config_t_handler
1647 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1648{
1649 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1650 vlib_main_t *vm = vlib_get_main ();
1651 int rv = 0;
1652 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1653 default_router;
1654
1655 is_no = mp->is_no == 1;
1656 suppress = mp->suppress == 1;
1657 managed = mp->managed == 1;
1658 other = mp->other == 1;
1659 ll_option = mp->ll_option == 1;
1660 send_unicast = mp->send_unicast == 1;
1661 cease = mp->cease == 1;
1662 default_router = mp->default_router == 1;
1663
1664 VALIDATE_SW_IF_INDEX (mp);
1665
1666 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1667 suppress, managed, other,
1668 ll_option, send_unicast, cease,
1669 default_router, ntohl (mp->lifetime),
1670 ntohl (mp->initial_count),
1671 ntohl (mp->initial_interval),
1672 ntohl (mp->max_interval),
1673 ntohl (mp->min_interval), is_no);
1674
1675 BAD_SW_IF_INDEX_LABEL;
1676
1677 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1678}
1679
1680static void
1681 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1682 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1683{
1684 vlib_main_t *vm = vlib_get_main ();
1685 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07001686 fib_prefix_t pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -05001687 int rv = 0;
1688 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1689
1690 VALIDATE_SW_IF_INDEX (mp);
1691
Neale Ranns37029302018-08-10 05:30:06 -07001692 ip_prefix_decode (&mp->prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -05001693 is_no = mp->is_no == 1;
1694 use_default = mp->use_default == 1;
1695 no_advertise = mp->no_advertise == 1;
1696 off_link = mp->off_link == 1;
1697 no_autoconfig = mp->no_autoconfig == 1;
1698 no_onlink = mp->no_onlink == 1;
1699
1700 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
Neale Ranns37029302018-08-10 05:30:06 -07001701 &pfx.fp_addr.ip6,
1702 pfx.fp_len, use_default,
Dave Barachb5e8a772016-12-06 12:04:42 -05001703 ntohl (mp->val_lifetime),
1704 ntohl (mp->pref_lifetime), no_advertise,
1705 off_link, no_autoconfig, no_onlink, is_no);
1706
1707 BAD_SW_IF_INDEX_LABEL;
1708 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1709}
1710
1711static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001712send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001713 u32 context,
1714 const ip46_address_t * addr, u32 sw_if_index)
1715{
1716 vl_api_ip6nd_proxy_details_t *mp;
1717
1718 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001719 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns3f844d02017-02-18 00:03:54 -08001720 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1721 mp->context = context;
1722 mp->sw_if_index = htonl (sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07001723
1724 ip6_address_encode (&addr->ip6, mp->ip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001725
Florin Coras6c4dae22018-01-09 06:39:23 -08001726 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001727}
1728
Neale Ranns3f844d02017-02-18 00:03:54 -08001729typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1730{
1731 u32 *indices;
1732} api_ip6nd_proxy_fib_table_walk_ctx_t;
1733
Neale Ranns89541992017-04-06 04:41:02 -07001734static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001735api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1736{
1737 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1738
1739 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1740 {
1741 vec_add1 (ctx->indices, fei);
1742 }
1743
Neale Ranns89541992017-04-06 04:41:02 -07001744 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001745}
1746
1747static void
1748vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1749{
1750 ip6_main_t *im6 = &ip6_main;
1751 fib_table_t *fib_table;
1752 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1753 .indices = NULL,
1754 };
1755 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001756 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001757 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001758
Florin Coras6c4dae22018-01-09 06:39:23 -08001759 reg = vl_api_client_index_to_registration (mp->client_index);
1760 if (!reg)
1761 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001762
1763 /* *INDENT-OFF* */
1764 pool_foreach (fib_table, im6->fibs,
1765 ({
1766 fib_table_walk(fib_table->ft_index,
1767 FIB_PROTOCOL_IP6,
1768 api_ip6nd_proxy_fib_table_walk,
1769 &ctx);
1770 }));
1771 /* *INDENT-ON* */
1772
1773 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1774
1775 vec_foreach (feip, ctx.indices)
1776 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001777 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001778
Florin Coras6c4dae22018-01-09 06:39:23 -08001779 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001780 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001781 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001782 fib_entry_get_resolving_interface (*feip));
1783 }
1784
1785 vec_free (ctx.indices);
1786}
1787
1788static void
1789vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1790{
1791 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07001792 ip6_address_t ip6;
Neale Ranns3f844d02017-02-18 00:03:54 -08001793 int rv = 0;
1794
1795 VALIDATE_SW_IF_INDEX (mp);
1796
Neale Ranns37029302018-08-10 05:30:06 -07001797 ip6_address_decode (mp->ip, &ip6);
1798 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index), &ip6, mp->is_del);
Neale Ranns3f844d02017-02-18 00:03:54 -08001799
1800 BAD_SW_IF_INDEX_LABEL;
1801 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1802}
1803
1804static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001805 vl_api_ip6nd_send_router_solicitation_t_handler
1806 (vl_api_ip6nd_send_router_solicitation_t * mp)
1807{
1808 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1809 icmp6_send_router_solicitation_params_t params;
1810 vlib_main_t *vm = vlib_get_main ();
1811 int rv = 0;
1812
1813 VALIDATE_SW_IF_INDEX (mp);
1814
1815 BAD_SW_IF_INDEX_LABEL;
1816 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1817
1818 if (rv != 0)
1819 return;
1820
1821 params.irt = ntohl (mp->irt);
1822 params.mrt = ntohl (mp->mrt);
1823 params.mrc = ntohl (mp->mrc);
1824 params.mrd = ntohl (mp->mrd);
1825
1826 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1827 &params);
1828}
1829
1830static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001831 vl_api_sw_interface_ip6_enable_disable_t_handler
1832 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1833{
1834 vlib_main_t *vm = vlib_get_main ();
1835 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1836 vnet_main_t *vnm = vnet_get_main ();
1837 int rv = 0;
1838 clib_error_t *error;
1839
1840 vnm->api_errno = 0;
1841
1842 VALIDATE_SW_IF_INDEX (mp);
1843
1844 error =
1845 (mp->enable == 1) ? enable_ip6_interface (vm,
1846 ntohl (mp->sw_if_index)) :
1847 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1848
1849 if (error)
1850 {
1851 clib_error_report (error);
1852 rv = VNET_API_ERROR_UNSPECIFIED;
1853 }
1854 else
1855 {
1856 rv = vnm->api_errno;
1857 }
1858
1859 BAD_SW_IF_INDEX_LABEL;
1860
1861 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1862}
1863
Neale Ranns32e1c012016-11-22 17:07:28 +00001864void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001865vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001866 u32 context, const mfib_signal_t * mfs)
1867{
1868 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001869 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001870 mfib_table_t *mfib;
1871 mfib_itf_t *mfi;
1872
1873 mp = vl_msg_api_alloc (sizeof (*mp));
1874
Dave Barachb7b92992018-10-17 10:38:51 -04001875 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001876 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1877 mp->context = context;
1878
1879 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001880 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001881 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001882 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001883 mp->table_id = ntohl (mfib->mft_table_id);
1884 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1885
Neale Ranns9e829a82018-12-17 05:50:32 -08001886 if (FIB_PROTOCOL_IP4 == prefix->fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +00001887 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001888 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001889
Neale Ranns9e829a82018-12-17 05:50:32 -08001890 memcpy (mp->grp_address, &prefix->fp_grp_addr.ip4, 4);
1891 if (prefix->fp_len > 32)
Neale Ranns32e1c012016-11-22 17:07:28 +00001892 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001893 memcpy (mp->src_address, &prefix->fp_src_addr.ip4, 4);
Neale Ranns32e1c012016-11-22 17:07:28 +00001894 }
1895 }
1896 else
1897 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001898 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001899
1900 ASSERT (0);
1901 }
1902
1903 if (0 != mfs->mfs_buffer_len)
1904 {
1905 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1906
1907 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1908 }
1909 else
1910 {
1911 mp->ip_packet_len = 0;
1912 }
1913
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001914 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001915}
1916
1917static void
1918vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1919{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001920 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001921
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001922 reg = vl_api_client_index_to_registration (mp->client_index);
1923 if (!reg)
1924 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001925
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001926 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001927 ;
1928}
Dave Barachb5e8a772016-12-06 12:04:42 -05001929
Florin Coras595992c2017-11-06 17:17:08 -08001930static void
1931 vl_api_ip_container_proxy_add_del_t_handler
1932 (vl_api_ip_container_proxy_add_del_t * mp)
1933{
1934 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1935 vnet_ip_container_proxy_args_t args;
1936 int rv = 0;
1937 clib_error_t *error;
1938
Dave Barachb7b92992018-10-17 10:38:51 -04001939 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001940
1941 ip_prefix_decode (&mp->pfx, &args.prefix);
1942
Florin Coras595992c2017-11-06 17:17:08 -08001943 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1944 args.is_add = mp->is_add;
1945 if ((error = vnet_ip_container_proxy_add_del (&args)))
1946 {
1947 rv = clib_error_get_code (error);
1948 clib_error_report (error);
1949 }
1950
1951 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1952}
1953
Matus Fabian75b9f452018-10-02 23:27:21 -07001954typedef struct ip_container_proxy_walk_ctx_t_
1955{
1956 vl_api_registration_t *reg;
1957 u32 context;
1958} ip_container_proxy_walk_ctx_t;
1959
1960static int
1961ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1962 void *args)
1963{
1964 vl_api_ip_container_proxy_details_t *mp;
1965 ip_container_proxy_walk_ctx_t *ctx = args;
1966
1967 mp = vl_msg_api_alloc (sizeof (*mp));
1968 if (!mp)
1969 return 1;
1970
Dave Barachb7b92992018-10-17 10:38:51 -04001971 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001972 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1973 mp->context = ctx->context;
1974
1975 mp->sw_if_index = ntohl (sw_if_index);
1976 ip_prefix_encode (pfx, &mp->prefix);
1977
1978 vl_api_send_msg (ctx->reg, (u8 *) mp);
1979
1980 return 1;
1981}
1982
1983static void
1984vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1985 mp)
1986{
1987 vl_api_registration_t *reg;
1988
1989 reg = vl_api_client_index_to_registration (mp->client_index);
1990 if (!reg)
1991 return;
1992
1993 ip_container_proxy_walk_ctx_t ctx = {
1994 .context = mp->context,
1995 .reg = reg,
1996 };
1997
1998 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1999}
2000
Neale Rannsb8d44812017-11-10 06:53:54 -08002001static void
2002vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
2003{
2004 int rv = 0;
2005 vl_api_ioam_enable_reply_t *rmp;
2006 clib_error_t *error;
2007
2008 /* Ignoring the profile id as currently a single profile
2009 * is supported */
2010 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
2011 mp->seqno, mp->analyse);
2012 if (error)
2013 {
2014 clib_error_report (error);
2015 rv = clib_error_get_code (error);
2016 }
2017
2018 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
2019}
2020
2021static void
2022vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
2023{
2024 int rv = 0;
2025 vl_api_ioam_disable_reply_t *rmp;
2026 clib_error_t *error;
2027
2028 error = clear_ioam_rewrite_fn ();
2029 if (error)
2030 {
2031 clib_error_report (error);
2032 rv = clib_error_get_code (error);
2033 }
2034
2035 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2036}
2037
2038static void
2039 vl_api_ip_source_and_port_range_check_add_del_t_handler
2040 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2041{
2042 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2043 int rv = 0;
2044
Neale Rannsb8d44812017-11-10 06:53:54 -08002045 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07002046 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08002047 u16 *low_ports = 0;
2048 u16 *high_ports = 0;
2049 u32 vrf_id;
2050 u16 tmp_low, tmp_high;
2051 u8 num_ranges;
2052 int i;
2053
Neale Ranns37029302018-08-10 05:30:06 -07002054 ip_prefix_decode (&mp->prefix, &pfx);
2055
Neale Rannsb8d44812017-11-10 06:53:54 -08002056 // Validate port range
2057 num_ranges = mp->number_of_ranges;
2058 if (num_ranges > 32)
2059 { // This is size of array in VPE.API
2060 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2061 goto reply;
2062 }
2063
2064 vec_reset_length (low_ports);
2065 vec_reset_length (high_ports);
2066
2067 for (i = 0; i < num_ranges; i++)
2068 {
2069 tmp_low = mp->low_ports[i];
2070 tmp_high = mp->high_ports[i];
2071 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2072 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2073 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2074 {
2075 rv = VNET_API_ERROR_INVALID_VALUE;
2076 goto reply;
2077 }
2078 vec_add1 (low_ports, tmp_low);
2079 vec_add1 (high_ports, tmp_high + 1);
2080 }
2081
Neale Rannsb8d44812017-11-10 06:53:54 -08002082 vrf_id = ntohl (mp->vrf_id);
2083
2084 if (vrf_id < 1)
2085 {
2086 rv = VNET_API_ERROR_INVALID_VALUE;
2087 goto reply;
2088 }
2089
2090
Neale Ranns37029302018-08-10 05:30:06 -07002091 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08002092 {
Neale Ranns37029302018-08-10 05:30:06 -07002093 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
2094 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08002095 vrf_id,
2096 low_ports,
2097 high_ports, is_add);
2098 }
2099 else
2100 {
Neale Ranns37029302018-08-10 05:30:06 -07002101 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
2102 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08002103 vrf_id,
2104 low_ports,
2105 high_ports, is_add);
2106 }
2107
2108reply:
2109 vec_free (low_ports);
2110 vec_free (high_ports);
2111 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2112}
2113
2114static void
2115 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2116 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2117{
2118 vlib_main_t *vm = vlib_get_main ();
2119 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2120 ip4_main_t *im = &ip4_main;
2121 int rv;
2122 u32 sw_if_index;
2123 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2124 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2125 uword *p = 0;
2126 int i;
2127
2128 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2129 ntohl (mp->tcp_out_vrf_id);
2130 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2131 ntohl (mp->udp_out_vrf_id);
2132 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2133 ntohl (mp->tcp_in_vrf_id);
2134 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2135 ntohl (mp->udp_in_vrf_id);
2136
2137
2138 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2139 {
2140 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2141 {
2142 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2143
2144 if (p == 0)
2145 {
2146 rv = VNET_API_ERROR_INVALID_VALUE;
2147 goto reply;
2148 }
2149
2150 fib_index[i] = p[0];
2151 }
2152 else
2153 fib_index[i] = ~0;
2154 }
2155 sw_if_index = ntohl (mp->sw_if_index);
2156
2157 VALIDATE_SW_IF_INDEX (mp);
2158
2159 rv =
2160 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2161 mp->is_add);
2162
2163 BAD_SW_IF_INDEX_LABEL;
2164reply:
2165
2166 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2167}
2168
Chore3460b012018-11-28 10:53:11 +03302169typedef union
2170{
2171 u32 fib_index;
2172} ip4_source_check_config_t;
2173
2174static void
2175 vl_api_ip_source_check_interface_add_del_t_handler
2176 (vl_api_ip_source_check_interface_add_del_t * mp)
2177{
2178 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
2179 int rv;
2180 u32 sw_if_index = ntohl (mp->sw_if_index);
2181 u8 is_add = mp->is_add;
2182 char *feature_name =
2183 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
2184
2185 ip4_source_check_config_t config;
2186
2187 VALIDATE_SW_IF_INDEX (mp);
2188
2189 config.fib_index =
2190 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
2191 rv =
2192 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
2193 is_add, &config, sizeof (config));
2194 BAD_SW_IF_INDEX_LABEL;
2195
2196 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
2197}
2198
Neale Rannsb8d44812017-11-10 06:53:54 -08002199#define IP4_ARP_EVENT 3
2200#define IP6_ND_EVENT 4
2201
Neale Rannsb8d44812017-11-10 06:53:54 -08002202static vlib_node_registration_t ip_resolver_process_node;
2203
Neale Ranns37029302018-08-10 05:30:06 -07002204static int
2205arp_change_delete_callback (u32 pool_index,
2206 const mac_address_t * mac,
2207 u32 sw_if_index, const ip4_address_t * address)
2208{
2209 vpe_api_main_t *am = &vpe_api_main;
2210
2211 if (pool_is_free_index (am->arp_events, pool_index))
2212 return 1;
2213
2214 pool_put_index (am->arp_events, pool_index);
2215 return 0;
2216}
2217
Neale Rannsb8d44812017-11-10 06:53:54 -08002218static void
2219handle_ip4_arp_event (u32 pool_index)
2220{
2221 vpe_api_main_t *vam = &vpe_api_main;
2222 vnet_main_t *vnm = vam->vnet_main;
2223 vlib_main_t *vm = vam->vlib_main;
2224 vl_api_ip4_arp_event_t *event;
2225 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002226 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002227
2228 /* Client can cancel, die, etc. */
2229 if (pool_is_free_index (vam->arp_events, pool_index))
2230 return;
2231
2232 event = pool_elt_at_index (vam->arp_events, pool_index);
2233
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002234 reg = vl_api_client_index_to_registration (event->client_index);
2235 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002236 {
2237 (void) vnet_add_del_ip4_arp_change_event
2238 (vnm, arp_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002239 event->pid, event->ip,
Neale Rannsb8d44812017-11-10 06:53:54 -08002240 ip_resolver_process_node.index, IP4_ARP_EVENT,
2241 ~0 /* pool index, notused */ , 0 /* is_add */ );
2242 return;
2243 }
2244
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002245 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002246 {
2247 mp = vl_msg_api_alloc (sizeof (*mp));
2248 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002249 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002250 }
2251 else
2252 {
2253 static f64 last_time;
2254 /*
2255 * Throttle syslog msgs.
2256 * It's pretty tempting to just revoke the registration...
2257 */
2258 if (vlib_time_now (vm) > last_time + 10.0)
2259 {
2260 clib_warning ("arp event for %U to pid %d: queue stuffed!",
Neale Ranns37029302018-08-10 05:30:06 -07002261 format_ip4_address, event->ip, event->pid);
Neale Rannsb8d44812017-11-10 06:53:54 -08002262 last_time = vlib_time_now (vm);
2263 }
2264 }
2265}
2266
Neale Ranns37029302018-08-10 05:30:06 -07002267static int
2268nd_change_delete_callback (u32 pool_index,
2269 const mac_address_t * mac,
2270 u32 sw_if_index, const ip6_address_t * addr)
2271{
2272 vpe_api_main_t *am = &vpe_api_main;
2273
2274 if (pool_is_free_index (am->nd_events, pool_index))
2275 return 1;
2276
2277 pool_put_index (am->nd_events, pool_index);
2278 return 0;
2279}
2280
Neale Rannsf12dad62018-06-04 18:41:24 -07002281static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002282handle_ip6_nd_event (u32 pool_index)
2283{
2284 vpe_api_main_t *vam = &vpe_api_main;
2285 vnet_main_t *vnm = vam->vnet_main;
2286 vlib_main_t *vm = vam->vlib_main;
2287 vl_api_ip6_nd_event_t *event;
2288 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002289 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002290
2291 /* Client can cancel, die, etc. */
2292 if (pool_is_free_index (vam->nd_events, pool_index))
2293 return;
2294
2295 event = pool_elt_at_index (vam->nd_events, pool_index);
2296
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002297 reg = vl_api_client_index_to_registration (event->client_index);
2298 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002299 {
2300 (void) vnet_add_del_ip6_nd_change_event
2301 (vnm, nd_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002302 event->pid, event->ip,
Neale Rannsb8d44812017-11-10 06:53:54 -08002303 ip_resolver_process_node.index, IP6_ND_EVENT,
2304 ~0 /* pool index, notused */ , 0 /* is_add */ );
2305 return;
2306 }
2307
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002308 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002309 {
2310 mp = vl_msg_api_alloc (sizeof (*mp));
2311 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002312 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002313 }
2314 else
2315 {
2316 static f64 last_time;
2317 /*
2318 * Throttle syslog msgs.
2319 * It's pretty tempting to just revoke the registration...
2320 */
2321 if (vlib_time_now (vm) > last_time + 10.0)
2322 {
2323 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
Neale Ranns37029302018-08-10 05:30:06 -07002324 format_ip6_address, event->ip, event->pid);
Neale Rannsb8d44812017-11-10 06:53:54 -08002325 last_time = vlib_time_now (vm);
2326 }
2327 }
2328}
2329
2330static uword
2331resolver_process (vlib_main_t * vm,
2332 vlib_node_runtime_t * rt, vlib_frame_t * f)
2333{
2334 volatile f64 timeout = 100.0;
2335 volatile uword *event_data = 0;
2336
2337 while (1)
2338 {
2339 vlib_process_wait_for_event_or_clock (vm, timeout);
2340
2341 uword event_type =
2342 vlib_process_get_events (vm, (uword **) & event_data);
2343
2344 int i;
2345 switch (event_type)
2346 {
2347 case IP4_ARP_EVENT:
2348 for (i = 0; i < vec_len (event_data); i++)
2349 handle_ip4_arp_event (event_data[i]);
2350 break;
2351
2352 case IP6_ND_EVENT:
2353 for (i = 0; i < vec_len (event_data); i++)
2354 handle_ip6_nd_event (event_data[i]);
2355 break;
2356
2357 case ~0: /* timeout */
2358 break;
2359 }
2360
2361 vec_reset_length (event_data);
2362 }
2363 return 0; /* or not */
2364}
2365
2366/* *INDENT-OFF* */
2367VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2368 .function = resolver_process,
2369 .type = VLIB_NODE_TYPE_PROCESS,
2370 .name = "ip-route-resolver-process",
2371};
2372/* *INDENT-ON* */
2373
2374static int
Neale Ranns37029302018-08-10 05:30:06 -07002375nd_change_data_callback (u32 pool_index, const mac_address_t * new_mac,
2376 u32 sw_if_index, const ip6_address_t * address)
Neale Rannsb8d44812017-11-10 06:53:54 -08002377{
2378 vpe_api_main_t *am = &vpe_api_main;
2379 vl_api_ip6_nd_event_t *event;
2380
2381 if (pool_is_free_index (am->nd_events, pool_index))
2382 return 1;
2383
2384 event = pool_elt_at_index (am->nd_events, pool_index);
Neale Ranns37029302018-08-10 05:30:06 -07002385 if (ethernet_mac_address_equal (event->mac, new_mac->bytes) &&
Neale Rannsb8d44812017-11-10 06:53:54 -08002386 sw_if_index == ntohl (event->sw_if_index))
2387 {
2388 return 1;
2389 }
2390
Neale Ranns37029302018-08-10 05:30:06 -07002391 mac_address_encode (new_mac, event->mac);
Neale Rannsb8d44812017-11-10 06:53:54 -08002392 event->sw_if_index = htonl (sw_if_index);
2393 return 0;
2394}
2395
Neale Rannsb8d44812017-11-10 06:53:54 -08002396static vlib_node_registration_t wc_arp_process_node;
2397
2398enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002399{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002400
2401static uword
2402wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2403{
Jim Thompsonf324dec2019-04-08 03:22:21 -05002404 /* These cross the longjmp boundary (vlib_process_wait_for_event)
Neale Rannsb8d44812017-11-10 06:53:54 -08002405 * and need to be volatile - to prevent them from being optimized into
2406 * a register - which could change during suspension */
2407
2408 volatile wc_arp_report_t arp_prev = { 0 };
2409 volatile wc_nd_report_t nd_prev = { 0 };
2410 volatile f64 last_arp = vlib_time_now (vm);
2411 volatile f64 last_nd = vlib_time_now (vm);
2412
2413 while (1)
2414 {
2415 vlib_process_wait_for_event (vm);
2416 uword event_type = WC_ARP_REPORT;
2417 void *event_data = vlib_process_get_event_data (vm, &event_type);
2418
2419 f64 now = vlib_time_now (vm);
2420 int i;
2421 if (event_type == WC_ARP_REPORT)
2422 {
2423 wc_arp_report_t *arp_events = event_data;
2424 for (i = 0; i < vec_len (arp_events); i++)
2425 {
Neale Ranns37029302018-08-10 05:30:06 -07002426 /* discard dup event - cast away volatile */
2427 if (arp_prev.ip.as_u32 == arp_events[i].ip.as_u32 &&
2428 mac_address_equal ((const mac_address_t *) &arp_prev.mac,
2429 &arp_events[i].mac) &&
Neale Rannsb8d44812017-11-10 06:53:54 -08002430 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2431 (now - last_arp) < 10.0)
2432 {
2433 continue;
2434 }
2435 arp_prev = arp_events[i];
2436 last_arp = now;
2437 vpe_client_registration_t *reg;
2438 /* *INDENT-OFF* */
2439 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2440 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002441 vl_api_registration_t *vl_reg;
2442 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002443 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002444 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002445 {
2446 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002447 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002448 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2449 event->client_index = reg->client_index;
2450 event->pid = reg->client_pid;
2451 event->mac_ip = 1;
Neale Ranns37029302018-08-10 05:30:06 -07002452 ip4_address_encode(&arp_events[i].ip, event->ip);
Neale Rannsb8d44812017-11-10 06:53:54 -08002453 event->sw_if_index = htonl(arp_events[i].sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07002454 mac_address_encode(&arp_events[i].mac, event->mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002455 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002456 }
2457 }));
2458 /* *INDENT-ON* */
2459 }
2460 }
2461 else if (event_type == WC_ND_REPORT)
2462 {
2463 wc_nd_report_t *nd_events = event_data;
2464 for (i = 0; i < vec_len (nd_events); i++)
2465 {
Neale Ranns37029302018-08-10 05:30:06 -07002466 /* discard dup event - cast away volatile */
2467 if (ip6_address_is_equal ((const ip6_address_t *) &nd_prev.ip6,
2468 &nd_events[i].ip6)
2469 && mac_address_equal ((const mac_address_t *) &nd_prev.mac,
2470 &nd_events[i].mac)
Neale Rannsb8d44812017-11-10 06:53:54 -08002471 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2472 && (now - last_nd) < 10.0)
2473 {
2474 continue;
2475 }
2476 nd_prev = nd_events[i];
2477 last_nd = now;
2478 vpe_client_registration_t *reg;
2479 /* *INDENT-OFF* */
2480 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2481 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002482 vl_api_registration_t *vl_reg;
2483 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2484 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002485 {
2486 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002487 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002488 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2489 event->client_index = reg->client_index;
2490 event->pid = reg->client_pid;
2491 event->mac_ip = 1;
Neale Ranns37029302018-08-10 05:30:06 -07002492 ip6_address_encode(&nd_events[i].ip6, event->ip);
Neale Rannsb8d44812017-11-10 06:53:54 -08002493 event->sw_if_index = htonl(nd_events[i].sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07002494 mac_address_encode(&nd_events[i].mac, event->mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002495 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002496 }
2497 }));
2498 /* *INDENT-ON* */
2499 }
2500 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002501 else if (event_type == RA_REPORT)
2502 {
2503 ra_report_t *ra_events = event_data;
2504 for (i = 0; i < vec_len (ra_events); i++)
2505 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002506 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2507 call_ip6_neighbor_callbacks (&ra_events[i],
2508 npm->ra_report_functions);
2509
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002510 vpe_client_registration_t *reg;
2511 /* *INDENT-OFF* */
2512 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2513 ({
2514 vl_api_registration_t *vl_reg;
2515 vl_reg =
2516 vl_api_client_index_to_registration (reg->client_index);
2517 if (vl_reg && vl_api_can_send_msg (vl_reg))
2518 {
2519 u32 event_size =
2520 sizeof (vl_api_ip6_ra_event_t) +
2521 vec_len (ra_events[i].prefixes) *
2522 sizeof (vl_api_ip6_ra_prefix_info_t);
2523 vl_api_ip6_ra_event_t *event =
2524 vl_msg_api_alloc (event_size);
Dave Barachb7b92992018-10-17 10:38:51 -04002525 clib_memset (event, 0, event_size);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002526 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2527 event->client_index = reg->client_index;
2528 event->pid = reg->client_pid;
2529
2530 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2531
Neale Ranns37029302018-08-10 05:30:06 -07002532 ip6_address_encode (&ra_events[i].router_address,
2533 event->router_addr);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002534
2535 event->current_hop_limit = ra_events[i].current_hop_limit;
2536 event->flags = ra_events[i].flags;
2537 event->router_lifetime_in_sec =
2538 clib_host_to_net_u16 (ra_events
2539 [i].router_lifetime_in_sec);
2540 event->neighbor_reachable_time_in_msec =
2541 clib_host_to_net_u32 (ra_events
2542 [i].neighbor_reachable_time_in_msec);
2543 event->time_in_msec_between_retransmitted_neighbor_solicitations
2544 =
2545 clib_host_to_net_u32 (ra_events
2546 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2547
2548 event->n_prefixes =
2549 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2550 vl_api_ip6_ra_prefix_info_t *prefix =
2551 (typeof (prefix)) event->prefixes;
2552 u32 j;
2553 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2554 {
2555 ra_report_prefix_info_t *info =
2556 &ra_events[i].prefixes[j];
Neale Ranns37029302018-08-10 05:30:06 -07002557 ip_prefix_encode(&info->prefix, &prefix->prefix);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002558 prefix->flags = info->flags;
2559 prefix->valid_time =
2560 clib_host_to_net_u32 (info->valid_time);
2561 prefix->preferred_time =
2562 clib_host_to_net_u32 (info->preferred_time);
2563 prefix++;
2564 }
2565
2566 vl_api_send_msg (vl_reg, (u8 *) event);
2567 }
2568 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002569 /* *INDENT-ON* */
2570 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002571 }
2572 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002573 vlib_process_put_event_data (vm, event_data);
2574 }
2575
2576 return 0;
2577}
2578
2579/* *INDENT-OFF* */
2580VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2581 .function = wc_arp_process,
2582 .type = VLIB_NODE_TYPE_PROCESS,
2583 .name = "wildcard-ip4-arp-publisher-process",
2584};
2585/* *INDENT-ON* */
2586
2587static int
Neale Ranns37029302018-08-10 05:30:06 -07002588arp_change_data_callback (u32 pool_index,
2589 const mac_address_t * mac,
2590 u32 sw_if_index, const ip4_address_t * address)
Neale Rannsb8d44812017-11-10 06:53:54 -08002591{
2592 vpe_api_main_t *am = &vpe_api_main;
2593 vl_api_ip4_arp_event_t *event;
2594
2595 if (pool_is_free_index (am->arp_events, pool_index))
2596 return 1;
2597
2598 event = pool_elt_at_index (am->arp_events, pool_index);
Neale Ranns37029302018-08-10 05:30:06 -07002599 if (ethernet_mac_address_equal (event->mac, mac->bytes) &&
Neale Rannsb8d44812017-11-10 06:53:54 -08002600 sw_if_index == ntohl (event->sw_if_index))
2601 {
2602 return 1;
2603 }
2604
Neale Ranns37029302018-08-10 05:30:06 -07002605 mac_address_encode (mac, event->mac);
Neale Rannsb8d44812017-11-10 06:53:54 -08002606 event->sw_if_index = htonl (sw_if_index);
2607 return 0;
2608}
2609
2610static void
2611vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2612{
2613 vpe_api_main_t *am = &vpe_api_main;
2614 vnet_main_t *vnm = vnet_get_main ();
2615 vl_api_want_ip4_arp_events_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07002616 ip4_address_t ip;
Neale Rannsb8d44812017-11-10 06:53:54 -08002617 int rv = 0;
2618
Neale Ranns37029302018-08-10 05:30:06 -07002619 ip4_address_decode (mp->ip, &ip);
2620
2621 if (ip.as_u32 == 0)
Neale Rannsb8d44812017-11-10 06:53:54 -08002622 {
2623 uword *p =
2624 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2625 vpe_client_registration_t *rp;
2626 if (p)
2627 {
2628 if (mp->enable_disable)
2629 {
2630 clib_warning ("pid %d: already enabled...", mp->pid);
2631 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2632 goto reply;
2633 }
2634 else
2635 {
2636 rp =
2637 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2638 pool_put (am->wc_ip4_arp_events_registrations, rp);
2639 hash_unset (am->wc_ip4_arp_events_registration_hash,
2640 mp->client_index);
2641 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002642 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002643 goto reply;
2644 }
2645 }
2646 if (mp->enable_disable == 0)
2647 {
2648 clib_warning ("pid %d: already disabled...", mp->pid);
2649 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2650 goto reply;
2651 }
2652 pool_get (am->wc_ip4_arp_events_registrations, rp);
2653 rp->client_index = mp->client_index;
2654 rp->client_pid = mp->pid;
2655 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2656 rp - am->wc_ip4_arp_events_registrations);
2657 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2658 goto reply;
2659 }
2660
2661 if (mp->enable_disable)
2662 {
2663 vl_api_ip4_arp_event_t *event;
2664 pool_get (am->arp_events, event);
2665 rv = vnet_add_del_ip4_arp_change_event
2666 (vnm, arp_change_data_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002667 mp->pid, mp->ip /* addr, in net byte order */ ,
Neale Rannsb8d44812017-11-10 06:53:54 -08002668 ip_resolver_process_node.index,
2669 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2670
2671 if (rv)
2672 {
2673 pool_put (am->arp_events, event);
2674 goto reply;
2675 }
Dave Barachb7b92992018-10-17 10:38:51 -04002676 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002677
2678 /* Python API expects events to have no context */
2679 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2680 event->client_index = mp->client_index;
Neale Ranns37029302018-08-10 05:30:06 -07002681 memcpy (event->ip, mp->ip, 4);
Neale Rannsb8d44812017-11-10 06:53:54 -08002682 event->pid = mp->pid;
Neale Ranns37029302018-08-10 05:30:06 -07002683 if (ip.as_u32 == 0)
Neale Rannsb8d44812017-11-10 06:53:54 -08002684 event->mac_ip = 1;
2685 }
2686 else
2687 {
2688 rv = vnet_add_del_ip4_arp_change_event
2689 (vnm, arp_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002690 mp->pid, mp->ip /* addr, in net byte order */ ,
Neale Rannsb8d44812017-11-10 06:53:54 -08002691 ip_resolver_process_node.index,
2692 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2693 }
2694reply:
2695 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2696}
2697
Neale Rannsf12dad62018-06-04 18:41:24 -07002698static clib_error_t *
2699want_ip4_arp_events_reaper (u32 client_index)
2700{
2701 vpe_client_registration_t *rp;
2702 vl_api_ip4_arp_event_t *event;
2703 u32 *to_delete, *event_id;
2704 vpe_api_main_t *am;
2705 vnet_main_t *vnm;
2706 uword *p;
2707
2708 am = &vpe_api_main;
2709 vnm = vnet_get_main ();
2710 to_delete = NULL;
2711
2712 /* clear out all of its pending resolutions */
2713 /* *INDENT-OFF* */
2714 pool_foreach(event, am->arp_events,
2715 ({
2716 if (event->client_index == client_index)
2717 {
2718 vec_add1(to_delete, event - am->arp_events);
2719 }
2720 }));
2721 /* *INDENT-ON* */
2722
2723 vec_foreach (event_id, to_delete)
2724 {
2725 event = pool_elt_at_index (am->arp_events, *event_id);
2726 vnet_add_del_ip4_arp_change_event
2727 (vnm, arp_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002728 event->pid, event->ip,
Neale Rannsf12dad62018-06-04 18:41:24 -07002729 ip_resolver_process_node.index, IP4_ARP_EVENT,
2730 ~0 /* pool index, notused */ , 0 /* is_add */ );
2731 }
2732 vec_free (to_delete);
2733
2734 /* remove from the registration hash */
2735 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2736
2737 if (p)
2738 {
2739 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2740 pool_put (am->wc_ip4_arp_events_registrations, rp);
2741 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2742 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2743 wc_arp_set_publisher_node (~0, REPORT_MAX);
2744 }
2745 return (NULL);
2746}
2747
2748VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2749
Neale Rannsb8d44812017-11-10 06:53:54 -08002750static void
2751vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2752{
2753 vpe_api_main_t *am = &vpe_api_main;
2754 vnet_main_t *vnm = vnet_get_main ();
2755 vl_api_want_ip6_nd_events_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07002756 ip6_address_t ip6;
Neale Rannsb8d44812017-11-10 06:53:54 -08002757 int rv = 0;
2758
Neale Ranns37029302018-08-10 05:30:06 -07002759 ip6_address_decode (mp->ip, &ip6);
2760
2761 if (ip6_address_is_zero (&ip6))
Neale Rannsb8d44812017-11-10 06:53:54 -08002762 {
2763 uword *p =
2764 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2765 vpe_client_registration_t *rp;
2766 if (p)
2767 {
2768 if (mp->enable_disable)
2769 {
2770 clib_warning ("pid %d: already enabled...", mp->pid);
2771 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2772 goto reply;
2773 }
2774 else
2775 {
2776 rp =
2777 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2778 pool_put (am->wc_ip6_nd_events_registrations, rp);
2779 hash_unset (am->wc_ip6_nd_events_registration_hash,
2780 mp->client_index);
2781 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002782 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002783 goto reply;
2784 }
2785 }
2786 if (mp->enable_disable == 0)
2787 {
2788 clib_warning ("pid %d: already disabled...", mp->pid);
2789 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2790 goto reply;
2791 }
2792 pool_get (am->wc_ip6_nd_events_registrations, rp);
2793 rp->client_index = mp->client_index;
2794 rp->client_pid = mp->pid;
2795 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2796 rp - am->wc_ip6_nd_events_registrations);
2797 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2798 goto reply;
2799 }
2800
2801 if (mp->enable_disable)
2802 {
2803 vl_api_ip6_nd_event_t *event;
2804 pool_get (am->nd_events, event);
2805
2806 rv = vnet_add_del_ip6_nd_change_event
2807 (vnm, nd_change_data_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002808 mp->pid, &ip6,
Neale Rannsb8d44812017-11-10 06:53:54 -08002809 ip_resolver_process_node.index,
2810 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2811
2812 if (rv)
2813 {
2814 pool_put (am->nd_events, event);
2815 goto reply;
2816 }
Dave Barachb7b92992018-10-17 10:38:51 -04002817 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002818
2819 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2820 event->client_index = mp->client_index;
Neale Ranns37029302018-08-10 05:30:06 -07002821 ip6_address_encode (&ip6, event->ip);
Neale Rannsb8d44812017-11-10 06:53:54 -08002822 event->pid = mp->pid;
2823 }
2824 else
2825 {
2826 rv = vnet_add_del_ip6_nd_change_event
2827 (vnm, nd_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002828 mp->pid, &ip6 /* addr, in net byte order */ ,
Neale Rannsb8d44812017-11-10 06:53:54 -08002829 ip_resolver_process_node.index,
2830 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2831 }
2832reply:
2833 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2834}
2835
Neale Rannsf12dad62018-06-04 18:41:24 -07002836static clib_error_t *
2837want_ip6_nd_events_reaper (u32 client_index)
2838{
2839
2840 vpe_client_registration_t *rp;
2841 vl_api_ip6_nd_event_t *event;
2842 u32 *to_delete, *event_id;
2843 vpe_api_main_t *am;
2844 vnet_main_t *vnm;
2845 uword *p;
2846
2847 am = &vpe_api_main;
2848 vnm = vnet_get_main ();
2849 to_delete = NULL;
2850
2851 /* clear out all of its pending resolutions */
2852 /* *INDENT-OFF* */
2853 pool_foreach(event, am->nd_events,
2854 ({
2855 if (event->client_index == client_index)
2856 {
2857 vec_add1(to_delete, event - am->nd_events);
2858 }
2859 }));
2860 /* *INDENT-ON* */
2861
2862 vec_foreach (event_id, to_delete)
2863 {
2864 event = pool_elt_at_index (am->nd_events, *event_id);
2865 vnet_add_del_ip6_nd_change_event
2866 (vnm, nd_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002867 event->pid, event->ip,
Neale Rannsf12dad62018-06-04 18:41:24 -07002868 ip_resolver_process_node.index, IP6_ND_EVENT,
2869 ~0 /* pool index, notused */ , 0 /* is_add */ );
2870 }
2871 vec_free (to_delete);
2872
2873 /* remove from the registration hash */
2874 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2875
2876 if (p)
2877 {
2878 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2879 pool_put (am->wc_ip6_nd_events_registrations, rp);
2880 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2881 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2882 wc_nd_set_publisher_node (~0, REPORT_MAX);
2883 }
2884 return (NULL);
2885}
2886
2887VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2888
Neale Rannsb8d44812017-11-10 06:53:54 -08002889static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002890vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2891{
2892 vpe_api_main_t *am = &vpe_api_main;
2893 vl_api_want_ip6_ra_events_reply_t *rmp;
2894 int rv = 0;
2895
2896 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2897 vpe_client_registration_t *rp;
2898 if (p)
2899 {
2900 if (mp->enable_disable)
2901 {
2902 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2903 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2904 goto reply;
2905 }
2906 else
2907 {
2908 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2909 pool_put (am->ip6_ra_events_registrations, rp);
2910 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002911 goto reply;
2912 }
2913 }
2914 if (mp->enable_disable == 0)
2915 {
2916 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2917 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2918 goto reply;
2919 }
2920 pool_get (am->ip6_ra_events_registrations, rp);
2921 rp->client_index = mp->client_index;
2922 rp->client_pid = ntohl (mp->pid);
2923 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2924 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002925
2926reply:
2927 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2928}
2929
Neale Rannsf12dad62018-06-04 18:41:24 -07002930static clib_error_t *
2931want_ip6_ra_events_reaper (u32 client_index)
2932{
2933 vpe_api_main_t *am = &vpe_api_main;
2934 vpe_client_registration_t *rp;
2935 uword *p;
2936
2937 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2938
2939 if (p)
2940 {
2941 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2942 pool_put (am->ip6_ra_events_registrations, rp);
2943 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2944 }
2945 return (NULL);
2946}
2947
2948VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2949
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002950static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002951vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2952{
2953 vl_api_proxy_arp_add_del_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07002954 ip4_address_t lo, hi;
Neale Rannsb8d44812017-11-10 06:53:54 -08002955 u32 fib_index;
2956 int rv;
Neale Rannsb8d44812017-11-10 06:53:54 -08002957
Neale Ranns37029302018-08-10 05:30:06 -07002958 fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->proxy.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002959
Neale Ranns37029302018-08-10 05:30:06 -07002960 if (~0 == fib_index)
Neale Rannsb8d44812017-11-10 06:53:54 -08002961 {
2962 rv = VNET_API_ERROR_NO_SUCH_FIB;
2963 goto out;
2964 }
2965
Neale Ranns37029302018-08-10 05:30:06 -07002966 ip4_address_decode (mp->proxy.low, &lo);
2967 ip4_address_decode (mp->proxy.hi, &hi);
Neale Rannsb8d44812017-11-10 06:53:54 -08002968
Neale Ranns37029302018-08-10 05:30:06 -07002969 rv = vnet_proxy_arp_add_del (&lo, &hi, fib_index, mp->is_add == 0);
Neale Rannsb8d44812017-11-10 06:53:54 -08002970
2971out:
Neale Rannsb8d44812017-11-10 06:53:54 -08002972 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2973}
2974
Neale Ranns0053de62018-05-22 08:40:52 -07002975typedef struct proxy_arp_walk_ctx_t_
2976{
2977 vl_api_registration_t *reg;
2978 u32 context;
2979} proxy_arp_walk_ctx_t;
2980
2981static walk_rc_t
2982send_proxy_arp_details (const ip4_address_t * lo_addr,
2983 const ip4_address_t * hi_addr,
2984 u32 fib_index, void *data)
2985{
2986 vl_api_proxy_arp_details_t *mp;
2987 proxy_arp_walk_ctx_t *ctx;
2988
2989 ctx = data;
2990
2991 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04002992 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07002993 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2994 mp->context = ctx->context;
Neale Ranns37029302018-08-10 05:30:06 -07002995 mp->proxy.table_id = htonl (fib_index);
2996
2997 ip4_address_encode (lo_addr, mp->proxy.low);
2998 ip4_address_encode (hi_addr, mp->proxy.hi);
Neale Ranns0053de62018-05-22 08:40:52 -07002999
3000 vl_api_send_msg (ctx->reg, (u8 *) mp);
3001
3002 return (WALK_CONTINUE);
3003}
3004
3005static void
3006vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
3007{
3008 vl_api_registration_t *reg;
3009
3010 reg = vl_api_client_index_to_registration (mp->client_index);
3011 if (!reg)
3012 return;
3013
3014 proxy_arp_walk_ctx_t wctx = {
3015 .reg = reg,
3016 .context = mp->context,
3017 };
3018
3019 proxy_arp_walk (send_proxy_arp_details, &wctx);
3020}
3021
3022static walk_rc_t
Neale Ranns57e53bb2019-05-29 13:58:43 +00003023send_proxy_arp_intfc_details (u32 sw_if_index, void *data)
Neale Ranns0053de62018-05-22 08:40:52 -07003024{
3025 vl_api_proxy_arp_intfc_details_t *mp;
3026 proxy_arp_walk_ctx_t *ctx;
3027
Neale Ranns0053de62018-05-22 08:40:52 -07003028 ctx = data;
3029
3030 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003031 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003032 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
3033 mp->context = ctx->context;
Neale Ranns57e53bb2019-05-29 13:58:43 +00003034 mp->sw_if_index = htonl (sw_if_index);
Neale Ranns0053de62018-05-22 08:40:52 -07003035
3036 vl_api_send_msg (ctx->reg, (u8 *) mp);
3037
3038 return (WALK_CONTINUE);
3039}
3040
3041static void
3042vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
3043{
3044 vl_api_registration_t *reg;
3045
3046 reg = vl_api_client_index_to_registration (mp->client_index);
3047 if (!reg)
3048 return;
3049
3050 proxy_arp_walk_ctx_t wctx = {
3051 .reg = reg,
3052 .context = mp->context,
3053 };
3054
Neale Ranns57e53bb2019-05-29 13:58:43 +00003055 proxy_arp_intfc_walk (send_proxy_arp_intfc_details, &wctx);
Neale Ranns0053de62018-05-22 08:40:52 -07003056}
3057
Neale Rannsb8d44812017-11-10 06:53:54 -08003058static void
3059 vl_api_proxy_arp_intfc_enable_disable_t_handler
3060 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
3061{
3062 int rv = 0;
3063 vnet_main_t *vnm = vnet_get_main ();
3064 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
3065
3066 VALIDATE_SW_IF_INDEX (mp);
3067
Neale Ranns57e53bb2019-05-29 13:58:43 +00003068 rv = vnet_proxy_arp_enable_disable (vnm,
3069 ntohl (mp->sw_if_index),
3070 mp->enable_disable);
Neale Rannsb8d44812017-11-10 06:53:54 -08003071
3072 BAD_SW_IF_INDEX_LABEL;
3073
3074 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3075}
3076
John Loc7b43042018-04-13 16:46:22 -04003077static void
3078vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3079{
3080 int rv = 0;
3081 vlib_main_t *vm = vlib_get_main ();
3082 vl_api_ip_probe_neighbor_reply_t *rmp;
3083 clib_error_t *error;
Neale Ranns37029302018-08-10 05:30:06 -07003084 ip46_address_t dst;
3085 ip46_type_t itype;
John Loc7b43042018-04-13 16:46:22 -04003086
3087 VALIDATE_SW_IF_INDEX (mp);
3088
3089 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07003090 itype = ip_address_decode (&mp->dst, &dst);
John Loc7b43042018-04-13 16:46:22 -04003091
Neale Ranns37029302018-08-10 05:30:06 -07003092 if (IP46_TYPE_IP6 == itype)
3093 error = ip6_probe_neighbor (vm, &dst.ip6, sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003094 else
Neale Ranns37029302018-08-10 05:30:06 -07003095 error = ip4_probe_neighbor (vm, &dst.ip4, sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003096
3097 if (error)
3098 {
3099 clib_error_report (error);
3100 rv = clib_error_get_code (error);
3101 }
3102
3103 BAD_SW_IF_INDEX_LABEL;
3104
3105 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3106}
3107
John Lo7f358b32018-04-28 01:19:24 -04003108static void
3109 vl_api_ip_scan_neighbor_enable_disable_t_handler
3110 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3111{
3112 int rv = 0;
3113 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3114 ip_neighbor_scan_arg_t arg;
3115
3116 arg.mode = mp->mode;
3117 arg.scan_interval = mp->scan_interval;
3118 arg.max_proc_time = mp->max_proc_time;
3119 arg.max_update = mp->max_update;
3120 arg.scan_int_delay = mp->scan_int_delay;
3121 arg.stale_threshold = mp->stale_threshold;
3122 ip_neighbor_scan_enable_disable (&arg);
3123
3124 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3125}
3126
Neale Rannsb8d44812017-11-10 06:53:54 -08003127static int
3128ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3129{
3130 vnet_main_t *vnm = vnet_get_main ();
3131 vnet_interface_main_t *im = &vnm->interface_main;
3132 ip4_main_t *im4 = &ip4_main;
3133 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003134 fib_table_t *fib_table;
3135 ip4_fib_t *fib;
3136 u32 sw_if_index;
3137 int i;
3138 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3139 u32 target_fib_id = ntohl (mp->vrf_id);
3140
Neale Rannsb8d44812017-11-10 06:53:54 -08003141 /* *INDENT-OFF* */
3142 pool_foreach (fib_table, im4->fibs,
3143 ({
3144 vnet_sw_interface_t * si;
3145
3146 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3147
3148 if (fib->table_id != target_fib_id)
3149 continue;
3150
3151 /* remove any mpls encap/decap labels */
3152 mpls_fib_reset_labels (fib->table_id);
3153
3154 /* remove any proxy arps in this fib */
3155 vnet_proxy_arp_fib_reset (fib->table_id);
3156
3157 /* Set the flow hash for this fib to the default */
3158 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3159
3160 vec_reset_length (sw_if_indices_to_shut);
3161
3162 /* Shut down interfaces in this FIB / clean out intfc routes */
3163 pool_foreach (si, im->sw_interfaces,
3164 ({
3165 u32 sw_if_index = si->sw_if_index;
3166
3167 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3168 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3169 fib->index))
3170 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3171 }));
3172
3173 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3174 sw_if_index = sw_if_indices_to_shut[i];
3175
3176 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3177 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3178 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3179 }
3180
3181 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3182
3183 rv = 0;
3184 break;
3185 })); /* pool_foreach (fib) */
3186 /* *INDENT-ON* */
3187
Neale Rannsb8d44812017-11-10 06:53:54 -08003188 return rv;
3189}
3190
3191static int
3192ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3193{
3194 vnet_main_t *vnm = vnet_get_main ();
3195 vnet_interface_main_t *im = &vnm->interface_main;
3196 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003197 static u32 *sw_if_indices_to_shut;
3198 fib_table_t *fib_table;
3199 ip6_fib_t *fib;
3200 u32 sw_if_index;
3201 int i;
3202 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3203 u32 target_fib_id = ntohl (mp->vrf_id);
3204
Neale Rannsb8d44812017-11-10 06:53:54 -08003205 /* *INDENT-OFF* */
3206 pool_foreach (fib_table, im6->fibs,
3207 ({
3208 vnet_sw_interface_t * si;
3209
3210 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3211
3212 if (fib->table_id != target_fib_id)
3213 continue;
3214
3215 vec_reset_length (sw_if_indices_to_shut);
3216
3217 /* Set the flow hash for this fib to the default */
3218 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3219
3220 /* Shut down interfaces in this FIB / clean out intfc routes */
3221 pool_foreach (si, im->sw_interfaces,
3222 ({
3223 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3224 fib->index)
3225 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3226 }));
3227
3228 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3229 sw_if_index = sw_if_indices_to_shut[i];
3230
3231 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3232 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3233 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3234 }
3235
3236 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3237
3238 rv = 0;
3239 break;
3240 })); /* pool_foreach (fib) */
3241 /* *INDENT-ON* */
3242
Neale Rannsb8d44812017-11-10 06:53:54 -08003243 return rv;
3244}
3245
3246static void
3247vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3248{
3249 int rv;
3250 vl_api_reset_fib_reply_t *rmp;
3251
3252 if (mp->is_ipv6)
3253 rv = ip6_reset_fib_t_handler (mp);
3254 else
3255 rv = ip4_reset_fib_t_handler (mp);
3256
3257 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3258}
3259
3260static void
3261vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3262{
3263 int rv;
3264 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3265 vnet_main_t *vnm = vnet_get_main ();
3266 clib_error_t *error;
3267
3268 vnm->api_errno = 0;
3269
3270 if (mp->is_ipv6)
3271 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3272 else
3273 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3274
3275 if (error)
3276 {
3277 clib_error_report (error);
3278 rv = VNET_API_ERROR_UNSPECIFIED;
3279 }
3280 else
3281 {
3282 rv = vnm->api_errno;
3283 }
3284
3285 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3286}
3287
Klement Sekera75e7d132017-09-20 08:26:30 +02003288void
3289vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3290{
3291 vl_api_ip_reassembly_set_reply_t *rmp;
3292 int rv = 0;
3293 if (mp->is_ip6)
3294 {
3295 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3296 clib_net_to_host_u32 (mp->max_reassemblies),
Klement Sekera3a343d42019-05-16 14:35:46 +02003297 clib_net_to_host_u32 (mp->max_reassembly_length),
Klement Sekera75e7d132017-09-20 08:26:30 +02003298 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3299 }
3300 else
3301 {
3302 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3303 clib_net_to_host_u32 (mp->max_reassemblies),
Klement Sekera3a343d42019-05-16 14:35:46 +02003304 clib_net_to_host_u32 (mp->max_reassembly_length),
Klement Sekera75e7d132017-09-20 08:26:30 +02003305 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3306 }
3307
3308 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3309}
3310
3311void
3312vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3313{
Ole Troan2e1c8962019-04-10 09:44:23 +02003314 vl_api_registration_t *rp;
Klement Sekera75e7d132017-09-20 08:26:30 +02003315
Ole Troan2e1c8962019-04-10 09:44:23 +02003316 rp = vl_api_client_index_to_registration (mp->client_index);
3317 if (rp == 0)
Klement Sekera75e7d132017-09-20 08:26:30 +02003318 return;
3319
3320 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04003321 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02003322 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3323 rmp->context = mp->context;
3324 rmp->retval = 0;
3325 if (mp->is_ip6)
3326 {
3327 rmp->is_ip6 = 1;
3328 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3329 &rmp->expire_walk_interval_ms);
3330 }
3331 else
3332 {
3333 rmp->is_ip6 = 0;
3334 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
Klement Sekera3a343d42019-05-16 14:35:46 +02003335 &rmp->max_reassembly_length,
Klement Sekera75e7d132017-09-20 08:26:30 +02003336 &rmp->expire_walk_interval_ms);
3337 }
3338 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3339 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3340 rmp->expire_walk_interval_ms =
3341 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
Ole Troan2e1c8962019-04-10 09:44:23 +02003342 vl_api_send_msg (rp, (u8 *) rmp);
Klement Sekera75e7d132017-09-20 08:26:30 +02003343}
3344
Klement Sekera4c533132018-02-22 11:41:12 +01003345void
3346 vl_api_ip_reassembly_enable_disable_t_handler
3347 (vl_api_ip_reassembly_enable_disable_t * mp)
3348{
3349 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3350 int rv = 0;
3351 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3352 mp->enable_ip4);
3353 if (0 == rv)
3354 {
3355 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3356 mp->enable_ip6);
3357 }
3358
Choree7f61d12018-11-28 10:27:58 +03303359 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01003360}
3361
Neale Ranns92207752019-06-03 13:21:40 +00003362typedef struct ip_punt_redirect_walk_ctx_t_
Pavel Kotucek609e1212018-11-27 09:59:44 +01003363{
Neale Ranns92207752019-06-03 13:21:40 +00003364 vl_api_registration_t *reg;
3365 u32 context;
3366} ip_punt_redirect_walk_ctx_t;
3367
3368static walk_rc_t
3369send_ip_punt_redirect_details (u32 rx_sw_if_index,
3370 const ip_punt_redirect_rx_t * ipr, void *arg)
3371{
3372 fib_route_path_encode_t *api_rpaths = NULL;
3373 ip_punt_redirect_walk_ctx_t *ctx = arg;
Pavel Kotucek609e1212018-11-27 09:59:44 +01003374 vl_api_ip_punt_redirect_details_t *mp;
3375
3376 mp = vl_msg_api_alloc (sizeof (*mp));
3377 if (!mp)
Neale Ranns92207752019-06-03 13:21:40 +00003378 return (WALK_STOP);;
Pavel Kotucek609e1212018-11-27 09:59:44 +01003379
3380 clib_memset (mp, 0, sizeof (*mp));
3381 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
Neale Ranns92207752019-06-03 13:21:40 +00003382 mp->context = ctx->context;
Pavel Kotucek609e1212018-11-27 09:59:44 +01003383
Neale Ranns92207752019-06-03 13:21:40 +00003384 fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &api_rpaths);
3385
3386 mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
3387 mp->punt.tx_sw_if_index = htonl (api_rpaths[0].rpath.frp_sw_if_index);
3388
3389 ip_address_encode (&api_rpaths[0].rpath.frp_addr,
3390 fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
3391
3392 vl_api_send_msg (ctx->reg, (u8 *) mp);
3393
3394 vec_free (api_rpaths);
3395
3396 return (WALK_CONTINUE);
Pavel Kotucek609e1212018-11-27 09:59:44 +01003397}
3398
3399static void
3400vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
3401{
3402 vl_api_registration_t *reg;
Neale Ranns92207752019-06-03 13:21:40 +00003403 fib_protocol_t fproto;
Pavel Kotucek609e1212018-11-27 09:59:44 +01003404
Pavel Kotucek609e1212018-11-27 09:59:44 +01003405 reg = vl_api_client_index_to_registration (mp->client_index);
3406 if (!reg)
3407 return;
3408
Neale Ranns92207752019-06-03 13:21:40 +00003409 fproto = mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4;
Pavel Kotucek609e1212018-11-27 09:59:44 +01003410
Neale Ranns92207752019-06-03 13:21:40 +00003411 ip_punt_redirect_walk_ctx_t ctx = {
3412 .reg = reg,
3413 .context = mp->context,
3414 };
3415
3416 if (~0 != mp->sw_if_index)
Pavel Kotucek609e1212018-11-27 09:59:44 +01003417 {
Neale Ranns92207752019-06-03 13:21:40 +00003418 u32 rx_sw_if_index;
3419 index_t pri;
3420
3421 rx_sw_if_index = ntohl (mp->sw_if_index);
3422 pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
3423
3424 if (INDEX_INVALID == pri)
3425 return;
3426
3427 send_ip_punt_redirect_details (rx_sw_if_index,
3428 ip_punt_redirect_get (pri), &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01003429 }
3430 else
Neale Ranns92207752019-06-03 13:21:40 +00003431 ip_punt_redirect_walk (fproto, send_ip_punt_redirect_details, &ctx);
Pavel Kotucek609e1212018-11-27 09:59:44 +01003432}
3433
Dave Barachb5e8a772016-12-06 12:04:42 -05003434#define vl_msg_name_crc_list
3435#include <vnet/ip/ip.api.h>
3436#undef vl_msg_name_crc_list
3437
3438static void
3439setup_message_id_table (api_main_t * am)
3440{
3441#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3442 foreach_vl_msg_name_crc_ip;
3443#undef _
3444}
3445
3446static clib_error_t *
3447ip_api_hookup (vlib_main_t * vm)
3448{
3449 api_main_t *am = &api_main;
3450
3451#define _(N,n) \
3452 vl_msg_api_set_handlers(VL_API_##N, #n, \
3453 vl_api_##n##_t_handler, \
3454 vl_noop_handler, \
3455 vl_api_##n##_t_endian, \
3456 vl_api_##n##_t_print, \
3457 sizeof(vl_api_##n##_t), 1);
3458 foreach_ip_api_msg;
3459#undef _
3460
3461 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00003462 * Mark the route add/del API as MP safe
3463 */
3464 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3465 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE_REPLY] = 1;
3466
3467 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05003468 * Set up the (msg_name, crc, message-id) table
3469 */
3470 setup_message_id_table (am);
3471
Juraj Sloboda52574522018-05-03 10:03:50 +02003472 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3473
Dave Barachb5e8a772016-12-06 12:04:42 -05003474 return 0;
3475}
3476
3477VLIB_API_INIT_FUNCTION (ip_api_hookup);
3478
3479/*
3480 * fd.io coding-style-patch-verification: ON
3481 *
3482 * Local Variables:
3483 * eval: (c-set-style "gnu")
3484 * End:
3485 */