blob: 5dbc02d5c5941b75aca71b0489831c90932befe5 [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>
26#include <vnet/ip/ip.h>
John Lo7f358b32018-04-28 01:19:24 -040027#include <vnet/ip/ip_neighbor.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050028#include <vnet/ip/ip6_neighbor.h>
29#include <vnet/fib/fib_table.h>
30#include <vnet/fib/fib_api.h>
31#include <vnet/dpo/drop_dpo.h>
32#include <vnet/dpo/receive_dpo.h>
33#include <vnet/dpo/lookup_dpo.h>
34#include <vnet/dpo/classify_dpo.h>
35#include <vnet/dpo/ip_null_dpo.h>
36#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080037#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000038#include <vnet/mfib/ip4_mfib.h>
39#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080040#include <vnet/mfib/mfib_entry.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080041#include <vnet/ip/ip_source_and_port_range_check.h>
42#include <vnet/fib/ip4_fib.h>
43#include <vnet/fib/ip6_fib.h>
44#include <vnet/ip/ip6_hop_by_hop.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020045#include <vnet/ip/ip4_reassembly.h>
46#include <vnet/ip/ip6_reassembly.h>
Neale Ranns0053de62018-05-22 08:40:52 -070047#include <vnet/ethernet/arp.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050048
49#include <vnet/vnet_msg_enum.h>
50
51#define vl_typedefs /* define message structures */
52#include <vnet/vnet_all_api_h.h>
53#undef vl_typedefs
54
55#define vl_endianfun /* define message structures */
56#include <vnet/vnet_all_api_h.h>
57#undef vl_endianfun
58
59/* instantiate all the print functions we know about */
60#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
61#define vl_printfun
62#include <vnet/vnet_all_api_h.h>
63#undef vl_printfun
64
65#include <vlibapi/api_helper_macros.h>
66
Neale Ranns5a8123b2017-01-26 01:18:23 -080067
Dave Barachb5e8a772016-12-06 12:04:42 -050068#define foreach_ip_api_msg \
69_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050070_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080071_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080072_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050073_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000074_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080075_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050076_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070077_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050078_(IP_DUMP, ip_dump) \
79_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080080_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
John Loc7b43042018-04-13 16:46:22 -040081_(IP_PROBE_NEIGHBOR, ip_probe_neighbor) \
John Lo7f358b32018-04-28 01:19:24 -040082_(IP_SCAN_NEIGHBOR_ENABLE_DISABLE, ip_scan_neighbor_enable_disable) \
Neale Rannsb8d44812017-11-10 06:53:54 -080083_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
84_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010085_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080086_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
Neale Ranns0053de62018-05-22 08:40:52 -070087_(PROXY_ARP_DUMP, proxy_arp_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080088_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
Neale Ranns0053de62018-05-22 08:40:52 -070089 _(PROXY_ARP_INTFC_DUMP, proxy_arp_intfc_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080090_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050091_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070092_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070093_(IP_PUNT_POLICE, ip_punt_police) \
94_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050095_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
96_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
97_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080098_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
99_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100100_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -0500101_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
102_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
Florin Coras595992c2017-11-06 17:17:08 -0800103 sw_interface_ip6_set_link_local_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800104_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
105_(IOAM_ENABLE, ioam_enable) \
106_(IOAM_DISABLE, ioam_disable) \
107_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
108 ip_source_and_port_range_check_add_del) \
109_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200110 ip_source_and_port_range_check_interface_add_del) \
111_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100112_(IP_REASSEMBLY_GET, ip_reassembly_get) \
113_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)
Dave Barachb5e8a772016-12-06 12:04:42 -0500114
115extern void stats_dslock_with_hint (int hint, int tag);
116extern void stats_dsunlock (void);
117
118static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600119send_ip_neighbor_details (u32 sw_if_index,
120 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500121 u8 is_static,
122 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800123 u8 * ip_address, vl_api_registration_t * reg,
124 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500125{
126 vl_api_ip_neighbor_details_t *mp;
127
128 mp = vl_msg_api_alloc (sizeof (*mp));
129 memset (mp, 0, sizeof (*mp));
130 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
131 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600132 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500133 mp->is_ipv6 = is_ipv6;
134 mp->is_static = is_static;
135 memcpy (mp->mac_address, mac_address, 6);
136 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
137
Florin Coras6c4dae22018-01-09 06:39:23 -0800138 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500139}
140
141static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500142vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
143{
Florin Coras6c4dae22018-01-09 06:39:23 -0800144 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500145
Florin Coras6c4dae22018-01-09 06:39:23 -0800146 reg = vl_api_client_index_to_registration (mp->client_index);
147 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500148 return;
149
150 u32 sw_if_index = ntohl (mp->sw_if_index);
151
152 if (mp->is_ipv6)
153 {
154 ip6_neighbor_t *n, *ns;
155
156 ns = ip6_neighbors_entries (sw_if_index);
157 /* *INDENT-OFF* */
158 vec_foreach (n, ns)
159 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500160 send_ip_neighbor_details
Brant Lin285434a2018-06-13 06:01:58 -0400161 (n->key.sw_if_index, mp->is_ipv6,
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600162 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500163 (u8 *) n->link_layer_address,
164 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800165 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500166 }
167 /* *INDENT-ON* */
168 vec_free (ns);
169 }
170 else
171 {
172 ethernet_arp_ip4_entry_t *n, *ns;
173
174 ns = ip4_neighbor_entries (sw_if_index);
175 /* *INDENT-OFF* */
176 vec_foreach (n, ns)
177 {
Brant Lin285434a2018-06-13 06:01:58 -0400178 send_ip_neighbor_details (n->sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500179 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
180 (u8*) n->ethernet_address,
181 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800182 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500183 }
184 /* *INDENT-ON* */
185 vec_free (ns);
186 }
187}
188
Dave Barachb5e8a772016-12-06 12:04:42 -0500189static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500190send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800191 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700192 const fib_table_t * table,
193 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500194 fib_route_path_encode_t * api_rpaths, u32 context)
195{
196 vl_api_ip_fib_details_t *mp;
197 fib_route_path_encode_t *api_rpath;
198 vl_api_fib_path_t *fp;
199 int path_count;
200
201 path_count = vec_len (api_rpaths);
202 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
203 if (!mp)
204 return;
205 memset (mp, 0, sizeof (*mp));
206 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
207 mp->context = context;
208
Neale Ranns2297af02017-09-12 09:45:04 -0700209 mp->table_id = htonl (table->ft_table_id);
210 memcpy (mp->table_name, table->ft_desc,
211 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500212 mp->address_length = pfx->fp_len;
213 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
Neale Ranns008dbe12018-09-07 09:32:36 -0700214 mp->stats_index =
215 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500216
217 mp->count = htonl (path_count);
218 fp = mp->path;
219 vec_foreach (api_rpath, api_rpaths)
220 {
Neale Ranns81458422018-03-12 06:59:36 -0700221 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500222 fp++;
223 }
224
Florin Coras6c4dae22018-01-09 06:39:23 -0800225 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500226}
227
Neale Rannsa3af3372017-03-28 03:49:52 -0700228typedef struct vl_api_ip_fib_dump_walk_ctx_t_
229{
230 fib_node_index_t *feis;
231} vl_api_ip_fib_dump_walk_ctx_t;
232
Neale Ranns89541992017-04-06 04:41:02 -0700233static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700234vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
235{
236 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
237
238 vec_add1 (ctx->feis, fei);
239
Neale Ranns89541992017-04-06 04:41:02 -0700240 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700241}
242
Dave Barachb5e8a772016-12-06 12:04:42 -0500243static void
244vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
245{
246 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800247 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500248 ip4_main_t *im = &ip4_main;
249 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700250 fib_node_index_t *lfeip;
Neale Rannsc5d43172018-07-30 08:04:40 -0700251 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500252 u32 fib_index;
253 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700254 vl_api_ip_fib_dump_walk_ctx_t ctx = {
255 .feis = NULL,
256 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500257
Florin Coras6c4dae22018-01-09 06:39:23 -0800258 reg = vl_api_client_index_to_registration (mp->client_index);
259 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500260 return;
261
262 /* *INDENT-OFF* */
263 pool_foreach (fib_table, im->fibs,
264 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700265 fib_table_walk(fib_table->ft_index,
266 FIB_PROTOCOL_IP4,
267 vl_api_ip_fib_dump_walk,
268 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500269 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500271
Neale Rannsa3af3372017-03-28 03:49:52 -0700272 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500273
Neale Rannsa3af3372017-03-28 03:49:52 -0700274 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500275 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700276 pfx = fib_entry_get_prefix (*lfeip);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500277 fib_index = fib_entry_get_fib_index (*lfeip);
Neale Rannsc5d43172018-07-30 08:04:40 -0700278 fib_table = fib_table_get (fib_index, pfx->fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500279 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 fib_entry_encode (*lfeip, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700281 send_ip_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500283 }
284
Neale Rannsa3af3372017-03-28 03:49:52 -0700285 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500286}
287
288static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500289send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800290 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800291 const fib_table_t * table,
292 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500294{
295 vl_api_ip6_fib_details_t *mp;
296 fib_route_path_encode_t *api_rpath;
297 vl_api_fib_path_t *fp;
298 int path_count;
299
Dave Barachd7cb1b52016-12-09 09:52:16 -0500300 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500301 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
302 if (!mp)
303 return;
304 memset (mp, 0, sizeof (*mp));
305 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
306 mp->context = context;
307
Neale Rannsa161a6d2017-11-14 08:10:41 -0800308 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500309 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500310 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800311 memcpy (mp->table_name, table->ft_desc,
312 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Neale Ranns008dbe12018-09-07 09:32:36 -0700313 mp->stats_index =
314 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500315
316 mp->count = htonl (path_count);
317 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500318 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500319 {
Neale Ranns81458422018-03-12 06:59:36 -0700320 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500321 fp++;
322 }
323
Florin Coras6c4dae22018-01-09 06:39:23 -0800324 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500325}
326
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327typedef struct apt_ip6_fib_show_ctx_t_
328{
329 u32 fib_index;
330 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500331} api_ip6_fib_show_ctx_t;
332
333static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500335{
336 api_ip6_fib_show_ctx_t *ctx = arg;
337
338 if ((kvp->key[2] >> 32) == ctx->fib_index)
339 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500341 }
342}
343
344static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800345api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500346 vl_api_ip6_fib_dump_t * mp,
347 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500348{
349 vpe_api_main_t *am = &vpe_api_main;
350 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500351 fib_node_index_t *fib_entry_index;
352 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700353 .fib_index = fib_table->ft_index,
354 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500355 };
356 fib_route_path_encode_t *api_rpaths;
Neale Rannsc5d43172018-07-30 08:04:40 -0700357 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500358
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 BV (clib_bihash_foreach_key_value_pair)
360 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
361 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500362
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500364
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 vec_foreach (fib_entry_index, ctx.entries)
366 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700367 pfx = fib_entry_get_prefix (*fib_entry_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 api_rpaths = NULL;
369 fib_entry_encode (*fib_entry_index, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700370 send_ip6_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371 vec_free (api_rpaths);
372 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500373
Dave Barachd7cb1b52016-12-09 09:52:16 -0500374 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500375}
376
377static void
378vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
379{
Florin Coras6c4dae22018-01-09 06:39:23 -0800380 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500381 ip6_main_t *im6 = &ip6_main;
382 fib_table_t *fib_table;
383
Florin Coras6c4dae22018-01-09 06:39:23 -0800384 reg = vl_api_client_index_to_registration (mp->client_index);
385 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500386 return;
387
388 /* *INDENT-OFF* */
389 pool_foreach (fib_table, im6->fibs,
390 ({
Neale Ranns81458422018-03-12 06:59:36 -0700391 /* don't send link locals */
392 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
393 continue;
394
Florin Coras6c4dae22018-01-09 06:39:23 -0800395 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500396 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500398}
399
400static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800401send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800402 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800403{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800404 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800405 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800406 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800407 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800408 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800409 int path_count;
410
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800411 mfib_entry = mfib_entry_get (mfei);
412 mfib_entry_get_prefix (mfei, &pfx);
413 mfib_entry_encode (mfei, &api_rpaths);
414
Neale Ranns5a8123b2017-01-26 01:18:23 -0800415 path_count = vec_len (api_rpaths);
416 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
417 if (!mp)
418 return;
419 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700420 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800421 mp->context = context;
422
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800423 mp->rpf_id = mfib_entry->mfe_rpf_id;
424 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800425 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800426 mp->address_length = pfx.fp_len;
427 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
428 sizeof (pfx.fp_grp_addr.ip4));
429 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
430 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800431
432 mp->count = htonl (path_count);
433 fp = mp->path;
434 vec_foreach (api_rpath, api_rpaths)
435 {
Neale Ranns81458422018-03-12 06:59:36 -0700436 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800437 fp++;
438 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800439 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800440
Florin Coras6c4dae22018-01-09 06:39:23 -0800441 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800442}
443
444typedef struct vl_api_ip_mfib_dump_ctc_t_
445{
446 fib_node_index_t *entries;
447} vl_api_ip_mfib_dump_ctc_t;
448
449static int
450vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
451{
452 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
453
454 vec_add1 (ctx->entries, fei);
455
456 return (0);
457}
458
459static void
460vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
461{
Florin Coras6c4dae22018-01-09 06:39:23 -0800462 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800463 ip4_main_t *im = &ip4_main;
464 mfib_table_t *mfib_table;
465 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800466 vl_api_ip_mfib_dump_ctc_t ctx = {
467 .entries = NULL,
468 };
469
Florin Coras6c4dae22018-01-09 06:39:23 -0800470 reg = vl_api_client_index_to_registration (mp->client_index);
471 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800472 return;
473
Neale Ranns5a8123b2017-01-26 01:18:23 -0800474 /* *INDENT-OFF* */
475 pool_foreach (mfib_table, im->mfibs,
476 ({
477 ip4_mfib_table_walk(&mfib_table->v4,
478 vl_api_ip_mfib_table_dump_walk,
479 &ctx);
480
481 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
482
483 vec_foreach (mfeip, ctx.entries)
484 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800485 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800486 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800487 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800488 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800489 vec_reset_length (ctx.entries);
490
491 }));
492 /* *INDENT-ON* */
493
494 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800495}
496
497static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800498send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800499 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800500 u32 table_id,
501 mfib_prefix_t * pfx,
502 fib_route_path_encode_t * api_rpaths, u32 context)
503{
504 vl_api_ip6_mfib_details_t *mp;
505 fib_route_path_encode_t *api_rpath;
506 vl_api_fib_path_t *fp;
507 int path_count;
508
509 path_count = vec_len (api_rpaths);
510 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
511 if (!mp)
512 return;
513 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700514 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800515 mp->context = context;
516
517 mp->table_id = htonl (table_id);
518 mp->address_length = pfx->fp_len;
519 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
520 sizeof (pfx->fp_grp_addr.ip6));
521 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
522 sizeof (pfx->fp_src_addr.ip6));
523
524 mp->count = htonl (path_count);
525 fp = mp->path;
526 vec_foreach (api_rpath, api_rpaths)
527 {
Neale Ranns81458422018-03-12 06:59:36 -0700528 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800529 fp++;
530 }
531
Florin Coras6c4dae22018-01-09 06:39:23 -0800532 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800533}
534
535typedef struct vl_api_ip6_mfib_dump_ctc_t_
536{
537 fib_node_index_t *entries;
538} vl_api_ip6_mfib_dump_ctc_t;
539
540static int
541vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
542{
543 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
544
545 vec_add1 (ctx->entries, fei);
546
547 return (0);
548}
549
550static void
551vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
552{
553 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800554 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800555 ip6_main_t *im = &ip6_main;
556 mfib_table_t *mfib_table;
557 fib_node_index_t *mfeip;
558 mfib_prefix_t pfx;
559 fib_route_path_encode_t *api_rpaths = NULL;
560 vl_api_ip6_mfib_dump_ctc_t ctx = {
561 .entries = NULL,
562 };
563
Florin Coras6c4dae22018-01-09 06:39:23 -0800564 reg = vl_api_client_index_to_registration (mp->client_index);
565 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800566 return;
567
568
569 /* *INDENT-OFF* */
570 pool_foreach (mfib_table, im->mfibs,
571 ({
572 ip6_mfib_table_walk(&mfib_table->v6,
573 vl_api_ip6_mfib_table_dump_walk,
574 &ctx);
575
576 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
577
578 vec_foreach(mfeip, ctx.entries)
579 {
580 mfib_entry_get_prefix (*mfeip, &pfx);
581 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800582 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800583 mfib_table->mft_table_id,
584 &pfx, api_rpaths,
585 mp->context);
586 }
587 vec_reset_length (api_rpaths);
588 vec_reset_length (ctx.entries);
589
590 }));
591 /* *INDENT-ON* */
592
593 vec_free (ctx.entries);
594 vec_free (api_rpaths);
595}
596
597static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700598vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
599 vlib_main_t * vm)
600{
601 vl_api_ip_punt_police_reply_t *rmp;
602 int rv = 0;
603
604 if (mp->is_ip6)
605 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
606 else
607 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
608
609 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
610}
611
612static void
613vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
614 vlib_main_t * vm)
615{
616 vl_api_ip_punt_redirect_reply_t *rmp;
617 int rv = 0;
618
619 if (mp->is_add)
620 {
621 ip46_address_t nh;
622
623 memset (&nh, 0, sizeof (nh));
624
625 if (mp->is_ip6)
626 {
627 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
628
629 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
630 ntohl (mp->tx_sw_if_index), &nh);
631 }
632 else
633 {
634 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
635
636 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
637 ntohl (mp->tx_sw_if_index), &nh);
638 }
639 }
640 else
641 {
642 if (mp->is_ip6)
643 {
644 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
645 }
646 else
647 {
648 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
649 }
650 }
651
652 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
653}
654
655static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500656vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
657 vlib_main_t * vm)
658{
Neale Ranns0bdd3192018-09-07 11:04:52 -0700659 ip46_address_t ip = ip46_address_initializer;
Dave Barachb5e8a772016-12-06 12:04:42 -0500660 vl_api_ip_neighbor_add_del_reply_t *rmp;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700661 ip_neighbor_flags_t flags;
Dave Barachb5e8a772016-12-06 12:04:42 -0500662 int rv = 0;
663
664 VALIDATE_SW_IF_INDEX (mp);
665
666 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
667
Neale Ranns0bdd3192018-09-07 11:04:52 -0700668 flags = IP_NEIGHBOR_FLAG_NODE;
669 if (mp->is_static)
670 flags |= IP_NEIGHBOR_FLAG_STATIC;
671 if (mp->is_no_adj_fib)
672 flags |= IP_NEIGHBOR_FLAG_NO_ADJ_FIB;
673
Dave Barachb5e8a772016-12-06 12:04:42 -0500674 if (mp->is_ipv6)
Neale Ranns0bdd3192018-09-07 11:04:52 -0700675 clib_memcpy (&ip.ip6, mp->dst_address, 16);
Dave Barachb5e8a772016-12-06 12:04:42 -0500676 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700677 clib_memcpy (&ip.ip4, mp->dst_address, 4);
Dave Barachb5e8a772016-12-06 12:04:42 -0500678
Neale Ranns0bdd3192018-09-07 11:04:52 -0700679 if (mp->is_add)
680 rv = ip_neighbor_add (&ip, mp->is_ipv6, mp->mac_address,
681 ntohl (mp->sw_if_index), flags);
682 else
683 rv = ip_neighbor_del (&ip, mp->is_ipv6, ntohl (mp->sw_if_index));
Dave Barachb5e8a772016-12-06 12:04:42 -0500684
Dave Barachb5e8a772016-12-06 12:04:42 -0500685 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700686
687 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500688 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
689}
690
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700691void
Neale Ranns15002542017-09-10 04:39:11 -0700692ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
693{
694 u32 fib_index, mfib_index;
695
696 /*
697 * ignore action on the default table - this is always present
698 * and cannot be added nor deleted from the API
699 */
700 if (0 != table_id)
701 {
702 /*
703 * The API holds only one lock on the table.
704 * i.e. it can be added many times via the API but needs to be
705 * deleted only once.
706 * The FIB index for unicast and multicast is not necessarily the
707 * same, since internal VPP systesm (like LISP and SR) create
708 * their own unicast tables.
709 */
710 fib_index = fib_table_find (fproto, table_id);
711 mfib_index = mfib_table_find (fproto, table_id);
712
713 if (~0 != fib_index)
714 {
715 fib_table_unlock (fib_index, fproto,
716 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
717 }
718 if (~0 != mfib_index)
719 {
720 mfib_table_unlock (mfib_index, fproto,
721 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
722 }
723 }
724}
725
726void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700727vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
728{
729 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700730 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
731 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700732 int rv = 0;
733
Neale Ranns15002542017-09-10 04:39:11 -0700734 if (mp->is_add)
735 {
Neale Ranns2297af02017-09-12 09:45:04 -0700736 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700737 }
738 else
739 {
740 ip_table_delete (fproto, table_id, 1);
741 }
742
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700743 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
744}
745
Dave Barachb5e8a772016-12-06 12:04:42 -0500746int
747add_del_route_t_handler (u8 is_multipath,
748 u8 is_add,
749 u8 is_drop,
750 u8 is_unreach,
751 u8 is_prohibit,
752 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800753 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500754 u8 is_classify,
755 u32 classify_table_index,
756 u8 is_resolve_host,
757 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800758 u8 is_interface_rx,
759 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800760 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700761 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800762 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500763 u32 fib_index,
764 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700765 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500766 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800767 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500768 u32 next_hop_sw_if_index,
769 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700770 u16 next_hop_weight,
771 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500772 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800773 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500774{
775 vnet_classify_main_t *cm = &vnet_classify_main;
776 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
777 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700778 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500779 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
780 .frp_sw_if_index = next_hop_sw_if_index,
781 .frp_fib_index = next_hop_fib_index,
782 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700783 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500784 .frp_label_stack = next_hop_out_label_stack,
785 };
786 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800787 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500788
Neale Rannscaac3502017-09-18 18:04:10 -0700789 /*
790 * the special INVALID label meams we are not recursing via a
791 * label. Exp-null value is never a valid via-label so that
792 * also means it's not a via-label and means clients that set
793 * it to 0 by default get the expected behaviour
794 */
795 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500796 {
Neale Rannsda78f952017-05-24 09:15:43 -0700797 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500798 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800799 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500800 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800801 if (is_dvr)
802 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500803 if (is_resolve_host)
804 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
805 if (is_resolve_attached)
806 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800807 if (is_interface_rx)
808 path_flags |= FIB_ROUTE_PATH_INTF_RX;
809 if (is_rpf_id)
810 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700811 if (is_source_lookup)
812 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800813 if (is_multicast)
814 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800815 if (is_udp_encap)
816 {
817 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
818 path.frp_udp_encap_id = next_hop_id;
819 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800820 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
821 && path.frp_fib_index != ~0)
822 {
823 path_flags |= FIB_ROUTE_PATH_DEAG;
824 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500825
826 path.frp_flags = path_flags;
827
Dave Barachb5e8a772016-12-06 12:04:42 -0500828 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
829
830 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
831 {
832 /*
833 * special route types that link directly to the adj
834 */
835 if (is_add)
836 {
837 dpo_id_t dpo = DPO_INVALID;
838 dpo_proto_t dproto;
839
840 dproto = fib_proto_to_dpo (prefix->fp_proto);
841
842 if (is_drop)
843 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
844 else if (is_local)
845 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
846 else if (is_unreach)
847 ip_null_dpo_add_and_lock (dproto,
848 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
849 else if (is_prohibit)
850 ip_null_dpo_add_and_lock (dproto,
851 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
852 &dpo);
853 else if (is_classify)
854 {
855 if (pool_is_free_index (cm->tables,
856 ntohl (classify_table_index)))
857 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500858 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500859 return VNET_API_ERROR_NO_SUCH_TABLE;
860 }
861
862 dpo_set (&dpo, DPO_CLASSIFY, dproto,
863 classify_dpo_create (dproto,
864 ntohl (classify_table_index)));
865 }
866 else
867 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500868 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500869 return VNET_API_ERROR_NO_SUCH_TABLE;
870 }
871
872 fib_table_entry_special_dpo_update (fib_index,
873 prefix,
874 FIB_SOURCE_API,
875 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
876 dpo_reset (&dpo);
877 }
878 else
879 {
880 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
881 }
882 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700883 else if (is_multipath)
884 {
885 vec_add1 (paths, path);
886
887 if (is_add)
888 fib_table_entry_path_add2 (fib_index,
889 prefix,
890 FIB_SOURCE_API, entry_flags, paths);
891 else
892 fib_table_entry_path_remove2 (fib_index,
893 prefix, FIB_SOURCE_API, paths);
894
895 vec_free (paths);
896 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500897 else
898 {
899 if (is_add)
900 {
901 vec_add1 (paths, path);
902 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800903 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500904 vec_free (paths);
905 }
906 else
907 {
908 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
909 }
910 }
911
Dave Barachd7cb1b52016-12-09 09:52:16 -0500912 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500913 return (0);
914}
915
916int
917add_del_route_check (fib_protocol_t table_proto,
918 u32 table_id,
919 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700920 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500921 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800922 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500923{
924 vnet_main_t *vnm = vnet_get_main ();
925
926 *fib_index = fib_table_find (table_proto, ntohl (table_id));
927 if (~0 == *fib_index)
928 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800929 /* No such VRF, and we weren't asked to create one */
930 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500931 }
932
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800933 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500934 {
935 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
936 ntohl (next_hop_sw_if_index)))
937 {
938 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
939 }
940 }
941 else
942 {
Neale Rannsda78f952017-05-24 09:15:43 -0700943 fib_protocol_t fib_nh_proto;
944
945 if (next_hop_table_proto > DPO_PROTO_MPLS)
946 return (0);
947
948 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
949
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800950 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700951 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800952 ntohl (next_hop_table_id));
953 else
Neale Rannsda78f952017-05-24 09:15:43 -0700954 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800955 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500956
957 if (~0 == *next_hop_fib_index)
958 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800959 /* No such VRF, and we weren't asked to create one */
960 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500961 }
962 }
963
964 return (0);
965}
966
967static int
Neale Ranns008dbe12018-09-07 09:32:36 -0700968ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
969 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500970{
971 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800972 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500973 int rv, ii, n_labels;;
974
975 rv = add_del_route_check (FIB_PROTOCOL_IP4,
976 mp->table_id,
977 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700978 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500979 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700980 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500981
982 if (0 != rv)
983 return (rv);
984
985 fib_prefix_t pfx = {
986 .fp_len = mp->dst_address_length,
987 .fp_proto = FIB_PROTOCOL_IP4,
988 };
989 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
990
991 ip46_address_t nh;
992 memset (&nh, 0, sizeof (nh));
993 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
994
995 n_labels = mp->next_hop_n_out_labels;
996 if (n_labels == 0)
997 ;
Dave Barachb5e8a772016-12-06 12:04:42 -0500998 else
999 {
1000 vec_validate (label_stack, n_labels - 1);
1001 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001002 {
1003 label_stack[ii].fml_value =
1004 ntohl (mp->next_hop_out_label_stack[ii].label);
1005 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1006 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1007 label_stack[ii].fml_mode =
1008 (mp->next_hop_out_label_stack[ii].is_uniform ?
1009 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1010 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001011 }
1012
Neale Ranns008dbe12018-09-07 09:32:36 -07001013 rv = add_del_route_t_handler (mp->is_multipath,
1014 mp->is_add,
1015 mp->is_drop,
1016 mp->is_unreach,
1017 mp->is_prohibit,
1018 mp->is_local, 0,
1019 mp->is_classify,
1020 mp->classify_table_index,
1021 mp->is_resolve_host,
1022 mp->is_resolve_attached, 0, 0,
1023 mp->is_dvr,
1024 mp->is_source_lookup,
1025 mp->is_udp_encap,
1026 fib_index, &pfx, DPO_PROTO_IP4,
1027 &nh,
1028 ntohl (mp->next_hop_id),
1029 ntohl (mp->next_hop_sw_if_index),
1030 next_hop_fib_index,
1031 mp->next_hop_weight,
1032 mp->next_hop_preference,
1033 ntohl (mp->next_hop_via_label), label_stack);
1034
1035 if (mp->is_add && 0 == rv)
1036 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1037
1038 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001039}
1040
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001041static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001042ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1043 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001044{
Neale Ranns31ed7442018-02-23 05:29:09 -08001045 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001046 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001047 int rv, ii, n_labels;;
1048
1049 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1050 mp->table_id,
1051 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001052 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001053 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001054 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001055
1056 if (0 != rv)
1057 return (rv);
1058
1059 fib_prefix_t pfx = {
1060 .fp_len = mp->dst_address_length,
1061 .fp_proto = FIB_PROTOCOL_IP6,
1062 };
1063 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1064
1065 ip46_address_t nh;
1066 memset (&nh, 0, sizeof (nh));
1067 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1068
1069 n_labels = mp->next_hop_n_out_labels;
1070 if (n_labels == 0)
1071 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001072 else
1073 {
1074 vec_validate (label_stack, n_labels - 1);
1075 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001076 {
1077 label_stack[ii].fml_value =
1078 ntohl (mp->next_hop_out_label_stack[ii].label);
1079 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1080 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1081 label_stack[ii].fml_mode =
1082 (mp->next_hop_out_label_stack[ii].is_uniform ?
1083 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1084 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001085 }
1086
Neale Ranns008dbe12018-09-07 09:32:36 -07001087 rv = add_del_route_t_handler (mp->is_multipath,
1088 mp->is_add,
1089 mp->is_drop,
1090 mp->is_unreach,
1091 mp->is_prohibit,
1092 mp->is_local, 0,
1093 mp->is_classify,
1094 mp->classify_table_index,
1095 mp->is_resolve_host,
1096 mp->is_resolve_attached, 0, 0,
1097 mp->is_dvr,
1098 mp->is_source_lookup,
1099 mp->is_udp_encap,
1100 fib_index, &pfx, DPO_PROTO_IP6,
1101 &nh, ntohl (mp->next_hop_id),
1102 ntohl (mp->next_hop_sw_if_index),
1103 next_hop_fib_index,
1104 mp->next_hop_weight,
1105 mp->next_hop_preference,
1106 ntohl (mp->next_hop_via_label), label_stack);
1107
1108 if (mp->is_add && 0 == rv)
1109 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1110
1111 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001112}
1113
1114void
1115vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1116{
1117 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001118 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001119 int rv;
1120 vnet_main_t *vnm = vnet_get_main ();
1121
1122 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001123 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001124
1125 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001126 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001127 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001128 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001129
1130 rv = (rv == 0) ? vnm->api_errno : rv;
1131
Neale Ranns008dbe12018-09-07 09:32:36 -07001132 /* *INDENT-OFF* */
1133 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1134 ({
1135 rmp->stats_index = htonl (stats_index);
1136 }))
1137 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001138}
1139
Neale Ranns15002542017-09-10 04:39:11 -07001140void
Neale Ranns2297af02017-09-12 09:45:04 -07001141ip_table_create (fib_protocol_t fproto,
1142 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001143{
1144 u32 fib_index, mfib_index;
1145
1146 /*
1147 * ignore action on the default table - this is always present
1148 * and cannot be added nor deleted from the API
1149 */
1150 if (0 != table_id)
1151 {
1152 /*
1153 * The API holds only one lock on the table.
1154 * i.e. it can be added many times via the API but needs to be
1155 * deleted only once.
1156 * The FIB index for unicast and multicast is not necessarily the
1157 * same, since internal VPP systesm (like LISP and SR) create
1158 * their own unicast tables.
1159 */
1160 fib_index = fib_table_find (fproto, table_id);
1161 mfib_index = mfib_table_find (fproto, table_id);
1162
1163 if (~0 == fib_index)
1164 {
Neale Ranns2297af02017-09-12 09:45:04 -07001165 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1166 (is_api ?
1167 FIB_SOURCE_API :
1168 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001169 }
1170 if (~0 == mfib_index)
1171 {
Neale Ranns2297af02017-09-12 09:45:04 -07001172 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1173 (is_api ?
1174 MFIB_SOURCE_API :
1175 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001176 }
1177 }
1178}
1179
Neale Ranns32e1c012016-11-22 17:07:28 +00001180static int
1181add_del_mroute_check (fib_protocol_t table_proto,
1182 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001183 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001184{
1185 vnet_main_t *vnm = vnet_get_main ();
1186
1187 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1188 if (~0 == *fib_index)
1189 {
Neale Ranns15002542017-09-10 04:39:11 -07001190 /* No such table */
1191 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001192 }
1193
1194 if (~0 != ntohl (next_hop_sw_if_index))
1195 {
1196 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1197 ntohl (next_hop_sw_if_index)))
1198 {
1199 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1200 }
1201 }
1202
1203 return (0);
1204}
1205
Neale Ranns28c142e2018-09-07 09:37:07 -07001206static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001207mroute_add_del_handler (u8 is_add,
1208 u8 is_local,
1209 u32 fib_index,
1210 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001211 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001212 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001213 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001214 u32 next_hop_sw_if_index,
1215 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001216{
Neale Ranns28c142e2018-09-07 09:37:07 -07001217 fib_node_index_t mfib_entry_index = ~0;
1218
Neale Ranns32e1c012016-11-22 17:07:28 +00001219 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1220
1221 fib_route_path_t path = {
1222 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001223 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001224 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001225 };
1226
1227 if (is_local)
1228 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1229
Neale Rannsd792d9c2017-10-21 10:53:20 -07001230 if (DPO_PROTO_BIER == nh_proto)
1231 {
1232 path.frp_bier_imp = bier_imp;
1233 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1234 }
1235 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001236 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001237 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1238 MFIB_SOURCE_API,
1239 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001240 goto done;
1241 }
1242
1243 if (is_add)
1244 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001245 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1246 MFIB_SOURCE_API,
1247 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001248 }
1249 else
1250 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001251 mfib_table_entry_path_remove (fib_index, prefix,
1252 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001253 }
1254
Neale Rannsd792d9c2017-10-21 10:53:20 -07001255done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001256 stats_dsunlock ();
Neale Ranns28c142e2018-09-07 09:37:07 -07001257 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001258}
1259
1260static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001261api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1262 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001263{
Neale Ranns28c142e2018-09-07 09:37:07 -07001264 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001265 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001266 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001267 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001268 u32 fib_index;
1269 int rv;
1270
Neale Rannsd792d9c2017-10-21 10:53:20 -07001271 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001272 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1273 rv = add_del_mroute_check (fproto,
1274 mp->table_id,
1275 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001276 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001277
1278 if (0 != rv)
1279 return (rv);
1280
1281 mfib_prefix_t pfx = {
1282 .fp_len = ntohs (mp->grp_address_length),
1283 .fp_proto = fproto,
1284 };
1285
1286 if (FIB_PROTOCOL_IP4 == fproto)
1287 {
1288 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1289 sizeof (pfx.fp_grp_addr.ip4));
1290 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1291 sizeof (pfx.fp_src_addr.ip4));
Neale Rannse821ab12017-06-01 07:45:05 -07001292 memset (&nh.ip6, 0, sizeof (nh.ip6));
1293 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns32e1c012016-11-22 17:07:28 +00001294 }
1295 else
1296 {
1297 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1298 sizeof (pfx.fp_grp_addr.ip6));
1299 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1300 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001301 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns32e1c012016-11-22 17:07:28 +00001302 }
1303
Neale Ranns28c142e2018-09-07 09:37:07 -07001304 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1305 mp->is_local,
1306 fib_index, &pfx,
1307 nh_proto,
1308 ntohl (mp->entry_flags),
1309 ntohl (mp->rpf_id),
1310 ntohl (mp->next_hop_sw_if_index),
1311 &nh,
1312 ntohl (mp->itf_flags),
1313 ntohl (mp->bier_imp));
1314
1315 if (~0 != mfib_entry_index)
1316 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1317
1318 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001319}
1320
1321void
1322vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1323{
1324 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001325 vnet_main_t *vnm;
1326 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001327 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001328
Neale Ranns28c142e2018-09-07 09:37:07 -07001329 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001330 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001331 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001332
Neale Ranns28c142e2018-09-07 09:37:07 -07001333 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001334
Neale Ranns28c142e2018-09-07 09:37:07 -07001335 /* *INDENT-OFF* */
1336 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1337 ({
1338 rmp->stats_index = htonl (stats_index);
1339 }));
1340 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001341}
1342
Dave Barachb5e8a772016-12-06 12:04:42 -05001343static void
1344send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001345 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1346 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001347{
1348 vl_api_ip_details_t *mp;
1349
1350 mp = vl_msg_api_alloc (sizeof (*mp));
1351 memset (mp, 0, sizeof (*mp));
1352 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1353
1354 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001355 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001356 mp->context = context;
1357
Florin Coras6c4dae22018-01-09 06:39:23 -08001358 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001359}
1360
1361static void
1362send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001363 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001364 u8 * ip, u16 prefix_length,
1365 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001366{
1367 vl_api_ip_address_details_t *mp;
1368
1369 mp = vl_msg_api_alloc (sizeof (*mp));
1370 memset (mp, 0, sizeof (*mp));
1371 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1372
1373 if (is_ipv6)
1374 {
1375 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1376 }
1377 else
1378 {
1379 u32 *tp = (u32 *) mp->ip;
1380 *tp = *(u32 *) ip;
1381 }
1382 mp->prefix_length = prefix_length;
1383 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001384 mp->sw_if_index = htonl (sw_if_index);
1385 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001386
Florin Coras6c4dae22018-01-09 06:39:23 -08001387 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001388}
1389
1390static void
1391vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1392{
1393 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001394 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001395 ip6_address_t *r6;
1396 ip4_address_t *r4;
1397 ip6_main_t *im6 = &ip6_main;
1398 ip4_main_t *im4 = &ip4_main;
1399 ip_lookup_main_t *lm6 = &im6->lookup_main;
1400 ip_lookup_main_t *lm4 = &im4->lookup_main;
1401 ip_interface_address_t *ia = 0;
1402 u32 sw_if_index = ~0;
1403 int rv __attribute__ ((unused)) = 0;
1404
1405 VALIDATE_SW_IF_INDEX (mp);
1406
1407 sw_if_index = ntohl (mp->sw_if_index);
1408
Florin Coras6c4dae22018-01-09 06:39:23 -08001409 reg = vl_api_client_index_to_registration (mp->client_index);
1410 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001411 return;
1412
Dave Barachb5e8a772016-12-06 12:04:42 -05001413 if (mp->is_ipv6)
1414 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001415 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001416 /* Do not send subnet details of the IP-interface for
1417 * unnumbered interfaces. otherwise listening clients
1418 * will be confused that the subnet is applied on more
1419 * than one interface */
1420 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001421 ({
1422 r6 = ip_interface_address_get_address (lm6, ia);
1423 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001424 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001425 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001426 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001427 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001428 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001429 else
1430 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001431 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001432 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001433 ({
1434 r4 = ip_interface_address_get_address (lm4, ia);
1435 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001436 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001437 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001438 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001439 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001440 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001441
Dave Barachb5e8a772016-12-06 12:04:42 -05001442 BAD_SW_IF_INDEX_LABEL;
1443}
1444
1445static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001446send_ip_unnumbered_details (vpe_api_main_t * am,
1447 vl_api_registration_t * reg,
1448 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1449{
1450 vl_api_ip_unnumbered_details_t *mp;
1451
1452 mp = vl_msg_api_alloc (sizeof (*mp));
1453 memset (mp, 0, sizeof (*mp));
1454 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1455
1456 mp->context = context;
1457 mp->sw_if_index = htonl (sw_if_index);
1458 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1459
1460 vl_api_send_msg (reg, (u8 *) mp);
1461}
1462
1463static void
1464vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1465{
1466 vnet_main_t *vnm = vnet_get_main ();
1467 vnet_interface_main_t *im = &vnm->interface_main;
1468 int rv __attribute__ ((unused)) = 0;
1469 vpe_api_main_t *am = &vpe_api_main;
1470 vl_api_registration_t *reg;
1471 vnet_sw_interface_t *si;
1472 u32 sw_if_index;
1473
1474 sw_if_index = ntohl (mp->sw_if_index);
1475
1476 reg = vl_api_client_index_to_registration (mp->client_index);
1477 if (!reg)
1478 return;
1479
1480 if (~0 != sw_if_index)
1481 {
1482 VALIDATE_SW_IF_INDEX (mp);
1483
1484 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1485
1486 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1487 {
1488 send_ip_unnumbered_details (am, reg,
1489 sw_if_index,
1490 si->unnumbered_sw_if_index,
1491 mp->context);
1492 }
1493 }
1494 else
1495 {
1496 /* *INDENT-OFF* */
1497 pool_foreach (si, im->sw_interfaces,
1498 ({
1499 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1500 {
1501 send_ip_unnumbered_details(am, reg,
1502 si->sw_if_index,
1503 si->unnumbered_sw_if_index,
1504 mp->context);
1505 }
1506 }));
1507 /* *INDENT-ON* */
1508 }
1509
1510 BAD_SW_IF_INDEX_LABEL;
1511}
1512
1513static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001514vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1515{
1516 vpe_api_main_t *am = &vpe_api_main;
1517 vnet_main_t *vnm = vnet_get_main ();
1518 vlib_main_t *vm = vlib_get_main ();
1519 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001520 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001521 vnet_sw_interface_t *si, *sorted_sis;
1522 u32 sw_if_index = ~0;
1523
Florin Coras6c4dae22018-01-09 06:39:23 -08001524 reg = vl_api_client_index_to_registration (mp->client_index);
1525 if (!reg)
1526 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001527
1528 /* Gather interfaces. */
1529 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1530 _vec_len (sorted_sis) = 0;
1531 /* *INDENT-OFF* */
1532 pool_foreach (si, im->sw_interfaces,
1533 ({
1534 vec_add1 (sorted_sis, si[0]);
1535 }));
1536 /* *INDENT-ON* */
1537
1538 vec_foreach (si, sorted_sis)
1539 {
1540 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1541 {
1542 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1543 {
1544 continue;
1545 }
1546 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001547 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001548 }
1549 }
1550}
1551
1552static void
1553set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1554{
1555 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001556 int rv;
1557 u32 table_id;
1558 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001559
Neale Ranns227038a2017-04-21 01:07:59 -07001560 table_id = ntohl (mp->vrf_id);
1561
1562#define _(a,b) if (mp->a) flow_hash_config |= b;
1563 foreach_flow_hash_bit;
1564#undef _
1565
1566 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001567
1568 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1569}
1570
1571static void
1572set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1573{
1574 vl_api_set_ip_flow_hash_reply_t *rmp;
1575 int rv;
1576 u32 table_id;
1577 flow_hash_config_t flow_hash_config = 0;
1578
1579 table_id = ntohl (mp->vrf_id);
1580
1581#define _(a,b) if (mp->a) flow_hash_config |= b;
1582 foreach_flow_hash_bit;
1583#undef _
1584
1585 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1586
1587 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1588}
1589
1590
1591static void
1592vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1593{
1594 if (mp->is_ipv6 == 0)
1595 set_ip4_flow_hash (mp);
1596 else
1597 set_ip6_flow_hash (mp);
1598}
1599
1600static void
1601 vl_api_sw_interface_ip6nd_ra_config_t_handler
1602 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1603{
1604 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1605 vlib_main_t *vm = vlib_get_main ();
1606 int rv = 0;
1607 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1608 default_router;
1609
1610 is_no = mp->is_no == 1;
1611 suppress = mp->suppress == 1;
1612 managed = mp->managed == 1;
1613 other = mp->other == 1;
1614 ll_option = mp->ll_option == 1;
1615 send_unicast = mp->send_unicast == 1;
1616 cease = mp->cease == 1;
1617 default_router = mp->default_router == 1;
1618
1619 VALIDATE_SW_IF_INDEX (mp);
1620
1621 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1622 suppress, managed, other,
1623 ll_option, send_unicast, cease,
1624 default_router, ntohl (mp->lifetime),
1625 ntohl (mp->initial_count),
1626 ntohl (mp->initial_interval),
1627 ntohl (mp->max_interval),
1628 ntohl (mp->min_interval), is_no);
1629
1630 BAD_SW_IF_INDEX_LABEL;
1631
1632 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1633}
1634
1635static void
1636 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1637 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1638{
1639 vlib_main_t *vm = vlib_get_main ();
1640 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1641 int rv = 0;
1642 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1643
1644 VALIDATE_SW_IF_INDEX (mp);
1645
1646 is_no = mp->is_no == 1;
1647 use_default = mp->use_default == 1;
1648 no_advertise = mp->no_advertise == 1;
1649 off_link = mp->off_link == 1;
1650 no_autoconfig = mp->no_autoconfig == 1;
1651 no_onlink = mp->no_onlink == 1;
1652
1653 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1654 (ip6_address_t *) mp->address,
1655 mp->address_length, use_default,
1656 ntohl (mp->val_lifetime),
1657 ntohl (mp->pref_lifetime), no_advertise,
1658 off_link, no_autoconfig, no_onlink, is_no);
1659
1660 BAD_SW_IF_INDEX_LABEL;
1661 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1662}
1663
1664static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001665send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001666 u32 context,
1667 const ip46_address_t * addr, u32 sw_if_index)
1668{
1669 vl_api_ip6nd_proxy_details_t *mp;
1670
1671 mp = vl_msg_api_alloc (sizeof (*mp));
1672 memset (mp, 0, sizeof (*mp));
1673 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1674 mp->context = context;
1675 mp->sw_if_index = htonl (sw_if_index);
1676 memcpy (mp->address, addr, 16);
1677
Florin Coras6c4dae22018-01-09 06:39:23 -08001678 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001679}
1680
Neale Ranns3f844d02017-02-18 00:03:54 -08001681typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1682{
1683 u32 *indices;
1684} api_ip6nd_proxy_fib_table_walk_ctx_t;
1685
Neale Ranns89541992017-04-06 04:41:02 -07001686static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001687api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1688{
1689 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1690
1691 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1692 {
1693 vec_add1 (ctx->indices, fei);
1694 }
1695
Neale Ranns89541992017-04-06 04:41:02 -07001696 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001697}
1698
1699static void
1700vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1701{
1702 ip6_main_t *im6 = &ip6_main;
1703 fib_table_t *fib_table;
1704 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1705 .indices = NULL,
1706 };
1707 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001708 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001709 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001710
Florin Coras6c4dae22018-01-09 06:39:23 -08001711 reg = vl_api_client_index_to_registration (mp->client_index);
1712 if (!reg)
1713 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001714
1715 /* *INDENT-OFF* */
1716 pool_foreach (fib_table, im6->fibs,
1717 ({
1718 fib_table_walk(fib_table->ft_index,
1719 FIB_PROTOCOL_IP6,
1720 api_ip6nd_proxy_fib_table_walk,
1721 &ctx);
1722 }));
1723 /* *INDENT-ON* */
1724
1725 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1726
1727 vec_foreach (feip, ctx.indices)
1728 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001729 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001730
Florin Coras6c4dae22018-01-09 06:39:23 -08001731 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001732 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001733 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001734 fib_entry_get_resolving_interface (*feip));
1735 }
1736
1737 vec_free (ctx.indices);
1738}
1739
1740static void
1741vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1742{
1743 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1744 int rv = 0;
1745
1746 VALIDATE_SW_IF_INDEX (mp);
1747
1748 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1749 (ip6_address_t *) mp->address, mp->is_del);
1750
1751 BAD_SW_IF_INDEX_LABEL;
1752 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1753}
1754
1755static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001756 vl_api_ip6nd_send_router_solicitation_t_handler
1757 (vl_api_ip6nd_send_router_solicitation_t * mp)
1758{
1759 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1760 icmp6_send_router_solicitation_params_t params;
1761 vlib_main_t *vm = vlib_get_main ();
1762 int rv = 0;
1763
1764 VALIDATE_SW_IF_INDEX (mp);
1765
1766 BAD_SW_IF_INDEX_LABEL;
1767 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1768
1769 if (rv != 0)
1770 return;
1771
1772 params.irt = ntohl (mp->irt);
1773 params.mrt = ntohl (mp->mrt);
1774 params.mrc = ntohl (mp->mrc);
1775 params.mrd = ntohl (mp->mrd);
1776
1777 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1778 &params);
1779}
1780
1781static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001782 vl_api_sw_interface_ip6_enable_disable_t_handler
1783 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1784{
1785 vlib_main_t *vm = vlib_get_main ();
1786 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1787 vnet_main_t *vnm = vnet_get_main ();
1788 int rv = 0;
1789 clib_error_t *error;
1790
1791 vnm->api_errno = 0;
1792
1793 VALIDATE_SW_IF_INDEX (mp);
1794
1795 error =
1796 (mp->enable == 1) ? enable_ip6_interface (vm,
1797 ntohl (mp->sw_if_index)) :
1798 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1799
1800 if (error)
1801 {
1802 clib_error_report (error);
1803 rv = VNET_API_ERROR_UNSPECIFIED;
1804 }
1805 else
1806 {
1807 rv = vnm->api_errno;
1808 }
1809
1810 BAD_SW_IF_INDEX_LABEL;
1811
1812 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1813}
1814
1815static void
1816 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1817 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1818{
1819 vlib_main_t *vm = vlib_get_main ();
1820 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1821 int rv = 0;
1822 clib_error_t *error;
1823 vnet_main_t *vnm = vnet_get_main ();
1824
1825 vnm->api_errno = 0;
1826
1827 VALIDATE_SW_IF_INDEX (mp);
1828
1829 error = set_ip6_link_local_address (vm,
1830 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001831 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001832 if (error)
1833 {
1834 clib_error_report (error);
1835 rv = VNET_API_ERROR_UNSPECIFIED;
1836 }
1837 else
1838 {
1839 rv = vnm->api_errno;
1840 }
1841
1842 BAD_SW_IF_INDEX_LABEL;
1843
1844 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1845}
1846
Neale Ranns32e1c012016-11-22 17:07:28 +00001847void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001848vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001849 u32 context, const mfib_signal_t * mfs)
1850{
1851 vl_api_mfib_signal_details_t *mp;
1852 mfib_prefix_t prefix;
1853 mfib_table_t *mfib;
1854 mfib_itf_t *mfi;
1855
1856 mp = vl_msg_api_alloc (sizeof (*mp));
1857
1858 memset (mp, 0, sizeof (*mp));
1859 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1860 mp->context = context;
1861
1862 mfi = mfib_itf_get (mfs->mfs_itf);
1863 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1864 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1865 prefix.fp_proto);
1866 mp->table_id = ntohl (mfib->mft_table_id);
1867 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1868
1869 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1870 {
1871 mp->grp_address_len = ntohs (prefix.fp_len);
1872
1873 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1874 if (prefix.fp_len > 32)
1875 {
1876 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1877 }
1878 }
1879 else
1880 {
1881 mp->grp_address_len = ntohs (prefix.fp_len);
1882
1883 ASSERT (0);
1884 }
1885
1886 if (0 != mfs->mfs_buffer_len)
1887 {
1888 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1889
1890 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1891 }
1892 else
1893 {
1894 mp->ip_packet_len = 0;
1895 }
1896
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001897 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001898}
1899
1900static void
1901vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1902{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001903 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001904
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001905 reg = vl_api_client_index_to_registration (mp->client_index);
1906 if (!reg)
1907 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001908
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001909 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001910 ;
1911}
Dave Barachb5e8a772016-12-06 12:04:42 -05001912
Florin Coras595992c2017-11-06 17:17:08 -08001913static void
1914 vl_api_ip_container_proxy_add_del_t_handler
1915 (vl_api_ip_container_proxy_add_del_t * mp)
1916{
1917 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1918 vnet_ip_container_proxy_args_t args;
1919 int rv = 0;
1920 clib_error_t *error;
1921
1922 memset (&args, 0, sizeof (args));
1923 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1924 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1925 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1926 args.is_add = mp->is_add;
1927 if ((error = vnet_ip_container_proxy_add_del (&args)))
1928 {
1929 rv = clib_error_get_code (error);
1930 clib_error_report (error);
1931 }
1932
1933 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1934}
1935
Neale Rannsb8d44812017-11-10 06:53:54 -08001936static void
1937vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1938{
1939 int rv = 0;
1940 vl_api_ioam_enable_reply_t *rmp;
1941 clib_error_t *error;
1942
1943 /* Ignoring the profile id as currently a single profile
1944 * is supported */
1945 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1946 mp->seqno, mp->analyse);
1947 if (error)
1948 {
1949 clib_error_report (error);
1950 rv = clib_error_get_code (error);
1951 }
1952
1953 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1954}
1955
1956static void
1957vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1958{
1959 int rv = 0;
1960 vl_api_ioam_disable_reply_t *rmp;
1961 clib_error_t *error;
1962
1963 error = clear_ioam_rewrite_fn ();
1964 if (error)
1965 {
1966 clib_error_report (error);
1967 rv = clib_error_get_code (error);
1968 }
1969
1970 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1971}
1972
1973static void
1974 vl_api_ip_source_and_port_range_check_add_del_t_handler
1975 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1976{
1977 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1978 int rv = 0;
1979
1980 u8 is_ipv6 = mp->is_ipv6;
1981 u8 is_add = mp->is_add;
1982 u8 mask_length = mp->mask_length;
1983 ip4_address_t ip4_addr;
1984 ip6_address_t ip6_addr;
1985 u16 *low_ports = 0;
1986 u16 *high_ports = 0;
1987 u32 vrf_id;
1988 u16 tmp_low, tmp_high;
1989 u8 num_ranges;
1990 int i;
1991
1992 // Validate port range
1993 num_ranges = mp->number_of_ranges;
1994 if (num_ranges > 32)
1995 { // This is size of array in VPE.API
1996 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1997 goto reply;
1998 }
1999
2000 vec_reset_length (low_ports);
2001 vec_reset_length (high_ports);
2002
2003 for (i = 0; i < num_ranges; i++)
2004 {
2005 tmp_low = mp->low_ports[i];
2006 tmp_high = mp->high_ports[i];
2007 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2008 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2009 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2010 {
2011 rv = VNET_API_ERROR_INVALID_VALUE;
2012 goto reply;
2013 }
2014 vec_add1 (low_ports, tmp_low);
2015 vec_add1 (high_ports, tmp_high + 1);
2016 }
2017
2018 // Validate mask_length
2019 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2020 {
2021 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2022 goto reply;
2023 }
2024
2025 vrf_id = ntohl (mp->vrf_id);
2026
2027 if (vrf_id < 1)
2028 {
2029 rv = VNET_API_ERROR_INVALID_VALUE;
2030 goto reply;
2031 }
2032
2033
2034 if (is_ipv6)
2035 {
2036 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2037 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2038 mask_length,
2039 vrf_id,
2040 low_ports,
2041 high_ports, is_add);
2042 }
2043 else
2044 {
2045 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2046 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2047 mask_length,
2048 vrf_id,
2049 low_ports,
2050 high_ports, is_add);
2051 }
2052
2053reply:
2054 vec_free (low_ports);
2055 vec_free (high_ports);
2056 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2057}
2058
2059static void
2060 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2061 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2062{
2063 vlib_main_t *vm = vlib_get_main ();
2064 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2065 ip4_main_t *im = &ip4_main;
2066 int rv;
2067 u32 sw_if_index;
2068 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2069 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2070 uword *p = 0;
2071 int i;
2072
2073 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2074 ntohl (mp->tcp_out_vrf_id);
2075 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2076 ntohl (mp->udp_out_vrf_id);
2077 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2078 ntohl (mp->tcp_in_vrf_id);
2079 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2080 ntohl (mp->udp_in_vrf_id);
2081
2082
2083 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2084 {
2085 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2086 {
2087 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2088
2089 if (p == 0)
2090 {
2091 rv = VNET_API_ERROR_INVALID_VALUE;
2092 goto reply;
2093 }
2094
2095 fib_index[i] = p[0];
2096 }
2097 else
2098 fib_index[i] = ~0;
2099 }
2100 sw_if_index = ntohl (mp->sw_if_index);
2101
2102 VALIDATE_SW_IF_INDEX (mp);
2103
2104 rv =
2105 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2106 mp->is_add);
2107
2108 BAD_SW_IF_INDEX_LABEL;
2109reply:
2110
2111 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2112}
2113
2114#define IP4_ARP_EVENT 3
2115#define IP6_ND_EVENT 4
2116
2117static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2118static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2119static vlib_node_registration_t ip_resolver_process_node;
2120
2121static void
2122handle_ip4_arp_event (u32 pool_index)
2123{
2124 vpe_api_main_t *vam = &vpe_api_main;
2125 vnet_main_t *vnm = vam->vnet_main;
2126 vlib_main_t *vm = vam->vlib_main;
2127 vl_api_ip4_arp_event_t *event;
2128 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002129 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002130
2131 /* Client can cancel, die, etc. */
2132 if (pool_is_free_index (vam->arp_events, pool_index))
2133 return;
2134
2135 event = pool_elt_at_index (vam->arp_events, pool_index);
2136
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002137 reg = vl_api_client_index_to_registration (event->client_index);
2138 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002139 {
2140 (void) vnet_add_del_ip4_arp_change_event
2141 (vnm, arp_change_delete_callback,
2142 event->pid, &event->address,
2143 ip_resolver_process_node.index, IP4_ARP_EVENT,
2144 ~0 /* pool index, notused */ , 0 /* is_add */ );
2145 return;
2146 }
2147
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002148 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002149 {
2150 mp = vl_msg_api_alloc (sizeof (*mp));
2151 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002152 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002153 }
2154 else
2155 {
2156 static f64 last_time;
2157 /*
2158 * Throttle syslog msgs.
2159 * It's pretty tempting to just revoke the registration...
2160 */
2161 if (vlib_time_now (vm) > last_time + 10.0)
2162 {
2163 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2164 format_ip4_address, &event->address, event->pid);
2165 last_time = vlib_time_now (vm);
2166 }
2167 }
2168}
2169
Neale Rannsf12dad62018-06-04 18:41:24 -07002170static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002171handle_ip6_nd_event (u32 pool_index)
2172{
2173 vpe_api_main_t *vam = &vpe_api_main;
2174 vnet_main_t *vnm = vam->vnet_main;
2175 vlib_main_t *vm = vam->vlib_main;
2176 vl_api_ip6_nd_event_t *event;
2177 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002178 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002179
2180 /* Client can cancel, die, etc. */
2181 if (pool_is_free_index (vam->nd_events, pool_index))
2182 return;
2183
2184 event = pool_elt_at_index (vam->nd_events, pool_index);
2185
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002186 reg = vl_api_client_index_to_registration (event->client_index);
2187 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002188 {
2189 (void) vnet_add_del_ip6_nd_change_event
2190 (vnm, nd_change_delete_callback,
2191 event->pid, &event->address,
2192 ip_resolver_process_node.index, IP6_ND_EVENT,
2193 ~0 /* pool index, notused */ , 0 /* is_add */ );
2194 return;
2195 }
2196
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002197 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002198 {
2199 mp = vl_msg_api_alloc (sizeof (*mp));
2200 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002201 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002202 }
2203 else
2204 {
2205 static f64 last_time;
2206 /*
2207 * Throttle syslog msgs.
2208 * It's pretty tempting to just revoke the registration...
2209 */
2210 if (vlib_time_now (vm) > last_time + 10.0)
2211 {
2212 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2213 format_ip6_address, &event->address, event->pid);
2214 last_time = vlib_time_now (vm);
2215 }
2216 }
2217}
2218
2219static uword
2220resolver_process (vlib_main_t * vm,
2221 vlib_node_runtime_t * rt, vlib_frame_t * f)
2222{
2223 volatile f64 timeout = 100.0;
2224 volatile uword *event_data = 0;
2225
2226 while (1)
2227 {
2228 vlib_process_wait_for_event_or_clock (vm, timeout);
2229
2230 uword event_type =
2231 vlib_process_get_events (vm, (uword **) & event_data);
2232
2233 int i;
2234 switch (event_type)
2235 {
2236 case IP4_ARP_EVENT:
2237 for (i = 0; i < vec_len (event_data); i++)
2238 handle_ip4_arp_event (event_data[i]);
2239 break;
2240
2241 case IP6_ND_EVENT:
2242 for (i = 0; i < vec_len (event_data); i++)
2243 handle_ip6_nd_event (event_data[i]);
2244 break;
2245
2246 case ~0: /* timeout */
2247 break;
2248 }
2249
2250 vec_reset_length (event_data);
2251 }
2252 return 0; /* or not */
2253}
2254
2255/* *INDENT-OFF* */
2256VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2257 .function = resolver_process,
2258 .type = VLIB_NODE_TYPE_PROCESS,
2259 .name = "ip-route-resolver-process",
2260};
2261/* *INDENT-ON* */
2262
2263static int
2264nd_change_data_callback (u32 pool_index, u8 * new_mac,
2265 u32 sw_if_index, ip6_address_t * address)
2266{
2267 vpe_api_main_t *am = &vpe_api_main;
2268 vl_api_ip6_nd_event_t *event;
2269
2270 if (pool_is_free_index (am->nd_events, pool_index))
2271 return 1;
2272
2273 event = pool_elt_at_index (am->nd_events, pool_index);
2274 if (eth_mac_equal (event->new_mac, new_mac) &&
2275 sw_if_index == ntohl (event->sw_if_index))
2276 {
2277 return 1;
2278 }
2279
2280 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2281 event->sw_if_index = htonl (sw_if_index);
2282 return 0;
2283}
2284
2285static int
2286arp_change_delete_callback (u32 pool_index, u8 * notused)
2287{
2288 vpe_api_main_t *am = &vpe_api_main;
2289
2290 if (pool_is_free_index (am->arp_events, pool_index))
2291 return 1;
2292
2293 pool_put_index (am->arp_events, pool_index);
2294 return 0;
2295}
2296
2297static int
2298nd_change_delete_callback (u32 pool_index, u8 * notused)
2299{
2300 vpe_api_main_t *am = &vpe_api_main;
2301
2302 if (pool_is_free_index (am->nd_events, pool_index))
2303 return 1;
2304
2305 pool_put_index (am->nd_events, pool_index);
2306 return 0;
2307}
2308
2309static vlib_node_registration_t wc_arp_process_node;
2310
2311enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002312{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002313
2314static uword
2315wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2316{
2317 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2318 * and need to be volatile - to prevent them from being optimized into
2319 * a register - which could change during suspension */
2320
2321 volatile wc_arp_report_t arp_prev = { 0 };
2322 volatile wc_nd_report_t nd_prev = { 0 };
2323 volatile f64 last_arp = vlib_time_now (vm);
2324 volatile f64 last_nd = vlib_time_now (vm);
2325
2326 while (1)
2327 {
2328 vlib_process_wait_for_event (vm);
2329 uword event_type = WC_ARP_REPORT;
2330 void *event_data = vlib_process_get_event_data (vm, &event_type);
2331
2332 f64 now = vlib_time_now (vm);
2333 int i;
2334 if (event_type == WC_ARP_REPORT)
2335 {
2336 wc_arp_report_t *arp_events = event_data;
2337 for (i = 0; i < vec_len (arp_events); i++)
2338 {
2339 /* discard dup event */
2340 if (arp_prev.ip4 == arp_events[i].ip4 &&
2341 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2342 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2343 (now - last_arp) < 10.0)
2344 {
2345 continue;
2346 }
2347 arp_prev = arp_events[i];
2348 last_arp = now;
2349 vpe_client_registration_t *reg;
2350 /* *INDENT-OFF* */
2351 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2352 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002353 vl_api_registration_t *vl_reg;
2354 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002355 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002356 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002357 {
2358 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2359 memset (event, 0, sizeof *event);
2360 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2361 event->client_index = reg->client_index;
2362 event->pid = reg->client_pid;
2363 event->mac_ip = 1;
2364 event->address = arp_events[i].ip4;
2365 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2366 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002367 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002368 }
2369 }));
2370 /* *INDENT-ON* */
2371 }
2372 }
2373 else if (event_type == WC_ND_REPORT)
2374 {
2375 wc_nd_report_t *nd_events = event_data;
2376 for (i = 0; i < vec_len (nd_events); i++)
2377 {
2378 /* discard dup event */
2379 if (ip6_address_is_equal
2380 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2381 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2382 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2383 && (now - last_nd) < 10.0)
2384 {
2385 continue;
2386 }
2387 nd_prev = nd_events[i];
2388 last_nd = now;
2389 vpe_client_registration_t *reg;
2390 /* *INDENT-OFF* */
2391 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2392 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002393 vl_api_registration_t *vl_reg;
2394 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2395 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002396 {
2397 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2398 memset (event, 0, sizeof *event);
2399 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2400 event->client_index = reg->client_index;
2401 event->pid = reg->client_pid;
2402 event->mac_ip = 1;
2403 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2404 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2405 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002406 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002407 }
2408 }));
2409 /* *INDENT-ON* */
2410 }
2411 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002412 else if (event_type == RA_REPORT)
2413 {
2414 ra_report_t *ra_events = event_data;
2415 for (i = 0; i < vec_len (ra_events); i++)
2416 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002417 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2418 call_ip6_neighbor_callbacks (&ra_events[i],
2419 npm->ra_report_functions);
2420
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002421 vpe_client_registration_t *reg;
2422 /* *INDENT-OFF* */
2423 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2424 ({
2425 vl_api_registration_t *vl_reg;
2426 vl_reg =
2427 vl_api_client_index_to_registration (reg->client_index);
2428 if (vl_reg && vl_api_can_send_msg (vl_reg))
2429 {
2430 u32 event_size =
2431 sizeof (vl_api_ip6_ra_event_t) +
2432 vec_len (ra_events[i].prefixes) *
2433 sizeof (vl_api_ip6_ra_prefix_info_t);
2434 vl_api_ip6_ra_event_t *event =
2435 vl_msg_api_alloc (event_size);
2436 memset (event, 0, event_size);
2437 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2438 event->client_index = reg->client_index;
2439 event->pid = reg->client_pid;
2440
2441 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2442
2443 memcpy (event->router_address, ra_events[i].router_address, 16);
2444
2445 event->current_hop_limit = ra_events[i].current_hop_limit;
2446 event->flags = ra_events[i].flags;
2447 event->router_lifetime_in_sec =
2448 clib_host_to_net_u16 (ra_events
2449 [i].router_lifetime_in_sec);
2450 event->neighbor_reachable_time_in_msec =
2451 clib_host_to_net_u32 (ra_events
2452 [i].neighbor_reachable_time_in_msec);
2453 event->time_in_msec_between_retransmitted_neighbor_solicitations
2454 =
2455 clib_host_to_net_u32 (ra_events
2456 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2457
2458 event->n_prefixes =
2459 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2460 vl_api_ip6_ra_prefix_info_t *prefix =
2461 (typeof (prefix)) event->prefixes;
2462 u32 j;
2463 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2464 {
2465 ra_report_prefix_info_t *info =
2466 &ra_events[i].prefixes[j];
2467 memcpy (prefix->dst_address, info->dst_address.as_u8,
2468 16);
2469 prefix->dst_address_length = info->dst_address_length;
2470 prefix->flags = info->flags;
2471 prefix->valid_time =
2472 clib_host_to_net_u32 (info->valid_time);
2473 prefix->preferred_time =
2474 clib_host_to_net_u32 (info->preferred_time);
2475 prefix++;
2476 }
2477
2478 vl_api_send_msg (vl_reg, (u8 *) event);
2479 }
2480 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002481 /* *INDENT-ON* */
2482 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002483 }
2484 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002485 vlib_process_put_event_data (vm, event_data);
2486 }
2487
2488 return 0;
2489}
2490
2491/* *INDENT-OFF* */
2492VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2493 .function = wc_arp_process,
2494 .type = VLIB_NODE_TYPE_PROCESS,
2495 .name = "wildcard-ip4-arp-publisher-process",
2496};
2497/* *INDENT-ON* */
2498
2499static int
2500arp_change_data_callback (u32 pool_index, u8 * new_mac,
2501 u32 sw_if_index, u32 address)
2502{
2503 vpe_api_main_t *am = &vpe_api_main;
2504 vl_api_ip4_arp_event_t *event;
2505
2506 if (pool_is_free_index (am->arp_events, pool_index))
2507 return 1;
2508
2509 event = pool_elt_at_index (am->arp_events, pool_index);
2510 if (eth_mac_equal (event->new_mac, new_mac) &&
2511 sw_if_index == ntohl (event->sw_if_index))
2512 {
2513 return 1;
2514 }
2515
2516 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2517 event->sw_if_index = htonl (sw_if_index);
2518 return 0;
2519}
2520
2521static void
2522vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2523{
2524 vpe_api_main_t *am = &vpe_api_main;
2525 vnet_main_t *vnm = vnet_get_main ();
2526 vl_api_want_ip4_arp_events_reply_t *rmp;
2527 int rv = 0;
2528
2529 if (mp->address == 0)
2530 {
2531 uword *p =
2532 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2533 vpe_client_registration_t *rp;
2534 if (p)
2535 {
2536 if (mp->enable_disable)
2537 {
2538 clib_warning ("pid %d: already enabled...", mp->pid);
2539 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2540 goto reply;
2541 }
2542 else
2543 {
2544 rp =
2545 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2546 pool_put (am->wc_ip4_arp_events_registrations, rp);
2547 hash_unset (am->wc_ip4_arp_events_registration_hash,
2548 mp->client_index);
2549 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002550 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002551 goto reply;
2552 }
2553 }
2554 if (mp->enable_disable == 0)
2555 {
2556 clib_warning ("pid %d: already disabled...", mp->pid);
2557 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2558 goto reply;
2559 }
2560 pool_get (am->wc_ip4_arp_events_registrations, rp);
2561 rp->client_index = mp->client_index;
2562 rp->client_pid = mp->pid;
2563 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2564 rp - am->wc_ip4_arp_events_registrations);
2565 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2566 goto reply;
2567 }
2568
2569 if (mp->enable_disable)
2570 {
2571 vl_api_ip4_arp_event_t *event;
2572 pool_get (am->arp_events, event);
2573 rv = vnet_add_del_ip4_arp_change_event
2574 (vnm, arp_change_data_callback,
2575 mp->pid, &mp->address /* addr, in net byte order */ ,
2576 ip_resolver_process_node.index,
2577 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2578
2579 if (rv)
2580 {
2581 pool_put (am->arp_events, event);
2582 goto reply;
2583 }
2584 memset (event, 0, sizeof (*event));
2585
2586 /* Python API expects events to have no context */
2587 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2588 event->client_index = mp->client_index;
2589 event->address = mp->address;
2590 event->pid = mp->pid;
2591 if (mp->address == 0)
2592 event->mac_ip = 1;
2593 }
2594 else
2595 {
2596 rv = vnet_add_del_ip4_arp_change_event
2597 (vnm, arp_change_delete_callback,
2598 mp->pid, &mp->address /* addr, in net byte order */ ,
2599 ip_resolver_process_node.index,
2600 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2601 }
2602reply:
2603 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2604}
2605
Neale Rannsf12dad62018-06-04 18:41:24 -07002606static clib_error_t *
2607want_ip4_arp_events_reaper (u32 client_index)
2608{
2609 vpe_client_registration_t *rp;
2610 vl_api_ip4_arp_event_t *event;
2611 u32 *to_delete, *event_id;
2612 vpe_api_main_t *am;
2613 vnet_main_t *vnm;
2614 uword *p;
2615
2616 am = &vpe_api_main;
2617 vnm = vnet_get_main ();
2618 to_delete = NULL;
2619
2620 /* clear out all of its pending resolutions */
2621 /* *INDENT-OFF* */
2622 pool_foreach(event, am->arp_events,
2623 ({
2624 if (event->client_index == client_index)
2625 {
2626 vec_add1(to_delete, event - am->arp_events);
2627 }
2628 }));
2629 /* *INDENT-ON* */
2630
2631 vec_foreach (event_id, to_delete)
2632 {
2633 event = pool_elt_at_index (am->arp_events, *event_id);
2634 vnet_add_del_ip4_arp_change_event
2635 (vnm, arp_change_delete_callback,
2636 event->pid, &event->address,
2637 ip_resolver_process_node.index, IP4_ARP_EVENT,
2638 ~0 /* pool index, notused */ , 0 /* is_add */ );
2639 }
2640 vec_free (to_delete);
2641
2642 /* remove from the registration hash */
2643 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2644
2645 if (p)
2646 {
2647 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2648 pool_put (am->wc_ip4_arp_events_registrations, rp);
2649 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2650 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2651 wc_arp_set_publisher_node (~0, REPORT_MAX);
2652 }
2653 return (NULL);
2654}
2655
2656VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2657
Neale Rannsb8d44812017-11-10 06:53:54 -08002658static void
2659vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2660{
2661 vpe_api_main_t *am = &vpe_api_main;
2662 vnet_main_t *vnm = vnet_get_main ();
2663 vl_api_want_ip6_nd_events_reply_t *rmp;
2664 int rv = 0;
2665
2666 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2667 {
2668 uword *p =
2669 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2670 vpe_client_registration_t *rp;
2671 if (p)
2672 {
2673 if (mp->enable_disable)
2674 {
2675 clib_warning ("pid %d: already enabled...", mp->pid);
2676 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2677 goto reply;
2678 }
2679 else
2680 {
2681 rp =
2682 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2683 pool_put (am->wc_ip6_nd_events_registrations, rp);
2684 hash_unset (am->wc_ip6_nd_events_registration_hash,
2685 mp->client_index);
2686 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002687 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002688 goto reply;
2689 }
2690 }
2691 if (mp->enable_disable == 0)
2692 {
2693 clib_warning ("pid %d: already disabled...", mp->pid);
2694 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2695 goto reply;
2696 }
2697 pool_get (am->wc_ip6_nd_events_registrations, rp);
2698 rp->client_index = mp->client_index;
2699 rp->client_pid = mp->pid;
2700 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2701 rp - am->wc_ip6_nd_events_registrations);
2702 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2703 goto reply;
2704 }
2705
2706 if (mp->enable_disable)
2707 {
2708 vl_api_ip6_nd_event_t *event;
2709 pool_get (am->nd_events, event);
2710
2711 rv = vnet_add_del_ip6_nd_change_event
2712 (vnm, nd_change_data_callback,
2713 mp->pid, mp->address /* addr, in net byte order */ ,
2714 ip_resolver_process_node.index,
2715 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2716
2717 if (rv)
2718 {
2719 pool_put (am->nd_events, event);
2720 goto reply;
2721 }
2722 memset (event, 0, sizeof (*event));
2723
2724 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2725 event->client_index = mp->client_index;
2726 clib_memcpy (event->address, mp->address, sizeof event->address);
2727 event->pid = mp->pid;
2728 }
2729 else
2730 {
2731 rv = vnet_add_del_ip6_nd_change_event
2732 (vnm, nd_change_delete_callback,
2733 mp->pid, mp->address /* addr, in net byte order */ ,
2734 ip_resolver_process_node.index,
2735 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2736 }
2737reply:
2738 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2739}
2740
Neale Rannsf12dad62018-06-04 18:41:24 -07002741static clib_error_t *
2742want_ip6_nd_events_reaper (u32 client_index)
2743{
2744
2745 vpe_client_registration_t *rp;
2746 vl_api_ip6_nd_event_t *event;
2747 u32 *to_delete, *event_id;
2748 vpe_api_main_t *am;
2749 vnet_main_t *vnm;
2750 uword *p;
2751
2752 am = &vpe_api_main;
2753 vnm = vnet_get_main ();
2754 to_delete = NULL;
2755
2756 /* clear out all of its pending resolutions */
2757 /* *INDENT-OFF* */
2758 pool_foreach(event, am->nd_events,
2759 ({
2760 if (event->client_index == client_index)
2761 {
2762 vec_add1(to_delete, event - am->nd_events);
2763 }
2764 }));
2765 /* *INDENT-ON* */
2766
2767 vec_foreach (event_id, to_delete)
2768 {
2769 event = pool_elt_at_index (am->nd_events, *event_id);
2770 vnet_add_del_ip6_nd_change_event
2771 (vnm, nd_change_delete_callback,
2772 event->pid, &event->address,
2773 ip_resolver_process_node.index, IP6_ND_EVENT,
2774 ~0 /* pool index, notused */ , 0 /* is_add */ );
2775 }
2776 vec_free (to_delete);
2777
2778 /* remove from the registration hash */
2779 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2780
2781 if (p)
2782 {
2783 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2784 pool_put (am->wc_ip6_nd_events_registrations, rp);
2785 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2786 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2787 wc_nd_set_publisher_node (~0, REPORT_MAX);
2788 }
2789 return (NULL);
2790}
2791
2792VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2793
Neale Rannsb8d44812017-11-10 06:53:54 -08002794static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002795vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2796{
2797 vpe_api_main_t *am = &vpe_api_main;
2798 vl_api_want_ip6_ra_events_reply_t *rmp;
2799 int rv = 0;
2800
2801 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2802 vpe_client_registration_t *rp;
2803 if (p)
2804 {
2805 if (mp->enable_disable)
2806 {
2807 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2808 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2809 goto reply;
2810 }
2811 else
2812 {
2813 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2814 pool_put (am->ip6_ra_events_registrations, rp);
2815 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002816 goto reply;
2817 }
2818 }
2819 if (mp->enable_disable == 0)
2820 {
2821 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2822 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2823 goto reply;
2824 }
2825 pool_get (am->ip6_ra_events_registrations, rp);
2826 rp->client_index = mp->client_index;
2827 rp->client_pid = ntohl (mp->pid);
2828 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2829 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002830
2831reply:
2832 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2833}
2834
Neale Rannsf12dad62018-06-04 18:41:24 -07002835static clib_error_t *
2836want_ip6_ra_events_reaper (u32 client_index)
2837{
2838 vpe_api_main_t *am = &vpe_api_main;
2839 vpe_client_registration_t *rp;
2840 uword *p;
2841
2842 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2843
2844 if (p)
2845 {
2846 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2847 pool_put (am->ip6_ra_events_registrations, rp);
2848 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2849 }
2850 return (NULL);
2851}
2852
2853VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2854
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002855static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002856vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2857{
2858 vl_api_proxy_arp_add_del_reply_t *rmp;
2859 u32 fib_index;
2860 int rv;
2861 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002862 uword *p;
2863
Ole Troan3288ed72017-12-06 17:00:05 +01002864 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002865
Neale Ranns0053de62018-05-22 08:40:52 -07002866 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002867
2868 if (!p)
2869 {
2870 rv = VNET_API_ERROR_NO_SUCH_FIB;
2871 goto out;
2872 }
2873
2874 fib_index = p[0];
2875
Neale Ranns0053de62018-05-22 08:40:52 -07002876 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2877 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002878 fib_index, mp->is_add == 0);
2879
2880out:
Ole Troan3288ed72017-12-06 17:00:05 +01002881 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002882 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2883}
2884
Neale Ranns0053de62018-05-22 08:40:52 -07002885typedef struct proxy_arp_walk_ctx_t_
2886{
2887 vl_api_registration_t *reg;
2888 u32 context;
2889} proxy_arp_walk_ctx_t;
2890
2891static walk_rc_t
2892send_proxy_arp_details (const ip4_address_t * lo_addr,
2893 const ip4_address_t * hi_addr,
2894 u32 fib_index, void *data)
2895{
2896 vl_api_proxy_arp_details_t *mp;
2897 proxy_arp_walk_ctx_t *ctx;
2898
2899 ctx = data;
2900
2901 mp = vl_msg_api_alloc (sizeof (*mp));
2902 memset (mp, 0, sizeof (*mp));
2903 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2904 mp->context = ctx->context;
2905 mp->proxy.vrf_id = htonl (fib_index);
2906 clib_memcpy (mp->proxy.low_address, lo_addr,
2907 sizeof (mp->proxy.low_address));
2908 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2909
2910 vl_api_send_msg (ctx->reg, (u8 *) mp);
2911
2912 return (WALK_CONTINUE);
2913}
2914
2915static void
2916vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2917{
2918 vl_api_registration_t *reg;
2919
2920 reg = vl_api_client_index_to_registration (mp->client_index);
2921 if (!reg)
2922 return;
2923
2924 proxy_arp_walk_ctx_t wctx = {
2925 .reg = reg,
2926 .context = mp->context,
2927 };
2928
2929 proxy_arp_walk (send_proxy_arp_details, &wctx);
2930}
2931
2932static walk_rc_t
2933send_proxy_arp_intfc_details (vnet_main_t * vnm,
2934 vnet_sw_interface_t * si, void *data)
2935{
2936 vl_api_proxy_arp_intfc_details_t *mp;
2937 proxy_arp_walk_ctx_t *ctx;
2938
2939 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
2940 return (WALK_CONTINUE);
2941
2942 ctx = data;
2943
2944 mp = vl_msg_api_alloc (sizeof (*mp));
2945 memset (mp, 0, sizeof (*mp));
2946 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
2947 mp->context = ctx->context;
2948 mp->sw_if_index = htonl (si->sw_if_index);
2949
2950 vl_api_send_msg (ctx->reg, (u8 *) mp);
2951
2952 return (WALK_CONTINUE);
2953}
2954
2955static void
2956vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
2957{
2958 vl_api_registration_t *reg;
2959
2960 reg = vl_api_client_index_to_registration (mp->client_index);
2961 if (!reg)
2962 return;
2963
2964 proxy_arp_walk_ctx_t wctx = {
2965 .reg = reg,
2966 .context = mp->context,
2967 };
2968
2969 vnet_sw_interface_walk (vnet_get_main (),
2970 send_proxy_arp_intfc_details, &wctx);
2971}
2972
Neale Rannsb8d44812017-11-10 06:53:54 -08002973static void
2974 vl_api_proxy_arp_intfc_enable_disable_t_handler
2975 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2976{
2977 int rv = 0;
2978 vnet_main_t *vnm = vnet_get_main ();
2979 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2980
2981 VALIDATE_SW_IF_INDEX (mp);
2982
2983 vnet_sw_interface_t *si =
2984 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2985
2986 ASSERT (si);
2987
2988 if (mp->enable_disable)
2989 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2990 else
2991 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2992
2993 BAD_SW_IF_INDEX_LABEL;
2994
2995 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2996}
2997
John Loc7b43042018-04-13 16:46:22 -04002998static void
2999vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3000{
3001 int rv = 0;
3002 vlib_main_t *vm = vlib_get_main ();
3003 vl_api_ip_probe_neighbor_reply_t *rmp;
3004 clib_error_t *error;
3005
3006 VALIDATE_SW_IF_INDEX (mp);
3007
3008 u32 sw_if_index = ntohl (mp->sw_if_index);
3009
3010 if (mp->is_ipv6)
3011 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003012 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003013 else
3014 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003015 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003016
3017 if (error)
3018 {
3019 clib_error_report (error);
3020 rv = clib_error_get_code (error);
3021 }
3022
3023 BAD_SW_IF_INDEX_LABEL;
3024
3025 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3026}
3027
John Lo7f358b32018-04-28 01:19:24 -04003028static void
3029 vl_api_ip_scan_neighbor_enable_disable_t_handler
3030 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3031{
3032 int rv = 0;
3033 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3034 ip_neighbor_scan_arg_t arg;
3035
3036 arg.mode = mp->mode;
3037 arg.scan_interval = mp->scan_interval;
3038 arg.max_proc_time = mp->max_proc_time;
3039 arg.max_update = mp->max_update;
3040 arg.scan_int_delay = mp->scan_int_delay;
3041 arg.stale_threshold = mp->stale_threshold;
3042 ip_neighbor_scan_enable_disable (&arg);
3043
3044 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3045}
3046
Neale Rannsb8d44812017-11-10 06:53:54 -08003047static int
3048ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3049{
3050 vnet_main_t *vnm = vnet_get_main ();
3051 vnet_interface_main_t *im = &vnm->interface_main;
3052 ip4_main_t *im4 = &ip4_main;
3053 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003054 fib_table_t *fib_table;
3055 ip4_fib_t *fib;
3056 u32 sw_if_index;
3057 int i;
3058 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3059 u32 target_fib_id = ntohl (mp->vrf_id);
3060
Ole Troan3288ed72017-12-06 17:00:05 +01003061 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003062
3063 /* *INDENT-OFF* */
3064 pool_foreach (fib_table, im4->fibs,
3065 ({
3066 vnet_sw_interface_t * si;
3067
3068 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3069
3070 if (fib->table_id != target_fib_id)
3071 continue;
3072
3073 /* remove any mpls encap/decap labels */
3074 mpls_fib_reset_labels (fib->table_id);
3075
3076 /* remove any proxy arps in this fib */
3077 vnet_proxy_arp_fib_reset (fib->table_id);
3078
3079 /* Set the flow hash for this fib to the default */
3080 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3081
3082 vec_reset_length (sw_if_indices_to_shut);
3083
3084 /* Shut down interfaces in this FIB / clean out intfc routes */
3085 pool_foreach (si, im->sw_interfaces,
3086 ({
3087 u32 sw_if_index = si->sw_if_index;
3088
3089 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3090 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3091 fib->index))
3092 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3093 }));
3094
3095 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3096 sw_if_index = sw_if_indices_to_shut[i];
3097
3098 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3099 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3100 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3101 }
3102
3103 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3104
3105 rv = 0;
3106 break;
3107 })); /* pool_foreach (fib) */
3108 /* *INDENT-ON* */
3109
Ole Troan3288ed72017-12-06 17:00:05 +01003110 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003111 return rv;
3112}
3113
3114static int
3115ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3116{
3117 vnet_main_t *vnm = vnet_get_main ();
3118 vnet_interface_main_t *im = &vnm->interface_main;
3119 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003120 static u32 *sw_if_indices_to_shut;
3121 fib_table_t *fib_table;
3122 ip6_fib_t *fib;
3123 u32 sw_if_index;
3124 int i;
3125 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3126 u32 target_fib_id = ntohl (mp->vrf_id);
3127
Ole Troan3288ed72017-12-06 17:00:05 +01003128 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003129
3130 /* *INDENT-OFF* */
3131 pool_foreach (fib_table, im6->fibs,
3132 ({
3133 vnet_sw_interface_t * si;
3134
3135 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3136
3137 if (fib->table_id != target_fib_id)
3138 continue;
3139
3140 vec_reset_length (sw_if_indices_to_shut);
3141
3142 /* Set the flow hash for this fib to the default */
3143 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3144
3145 /* Shut down interfaces in this FIB / clean out intfc routes */
3146 pool_foreach (si, im->sw_interfaces,
3147 ({
3148 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3149 fib->index)
3150 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3151 }));
3152
3153 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3154 sw_if_index = sw_if_indices_to_shut[i];
3155
3156 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3157 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3158 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3159 }
3160
3161 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3162
3163 rv = 0;
3164 break;
3165 })); /* pool_foreach (fib) */
3166 /* *INDENT-ON* */
3167
Ole Troan3288ed72017-12-06 17:00:05 +01003168 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003169 return rv;
3170}
3171
3172static void
3173vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3174{
3175 int rv;
3176 vl_api_reset_fib_reply_t *rmp;
3177
3178 if (mp->is_ipv6)
3179 rv = ip6_reset_fib_t_handler (mp);
3180 else
3181 rv = ip4_reset_fib_t_handler (mp);
3182
3183 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3184}
3185
3186static void
3187vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3188{
3189 int rv;
3190 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3191 vnet_main_t *vnm = vnet_get_main ();
3192 clib_error_t *error;
3193
3194 vnm->api_errno = 0;
3195
3196 if (mp->is_ipv6)
3197 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3198 else
3199 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3200
3201 if (error)
3202 {
3203 clib_error_report (error);
3204 rv = VNET_API_ERROR_UNSPECIFIED;
3205 }
3206 else
3207 {
3208 rv = vnm->api_errno;
3209 }
3210
3211 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3212}
3213
Klement Sekera75e7d132017-09-20 08:26:30 +02003214void
3215vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3216{
3217 vl_api_ip_reassembly_set_reply_t *rmp;
3218 int rv = 0;
3219 if (mp->is_ip6)
3220 {
3221 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3222 clib_net_to_host_u32 (mp->max_reassemblies),
3223 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3224 }
3225 else
3226 {
3227 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3228 clib_net_to_host_u32 (mp->max_reassemblies),
3229 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3230 }
3231
3232 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3233}
3234
3235void
3236vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3237{
3238 unix_shared_memory_queue_t *q;
3239
3240 q = vl_api_client_index_to_input_queue (mp->client_index);
3241
3242 if (q == 0)
3243 return;
3244
3245 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
3246 memset (rmp, 0, sizeof (*rmp));
3247 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3248 rmp->context = mp->context;
3249 rmp->retval = 0;
3250 if (mp->is_ip6)
3251 {
3252 rmp->is_ip6 = 1;
3253 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3254 &rmp->expire_walk_interval_ms);
3255 }
3256 else
3257 {
3258 rmp->is_ip6 = 0;
3259 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3260 &rmp->expire_walk_interval_ms);
3261 }
3262 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3263 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3264 rmp->expire_walk_interval_ms =
3265 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3266 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3267}
3268
Klement Sekera4c533132018-02-22 11:41:12 +01003269void
3270 vl_api_ip_reassembly_enable_disable_t_handler
3271 (vl_api_ip_reassembly_enable_disable_t * mp)
3272{
3273 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3274 int rv = 0;
3275 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3276 mp->enable_ip4);
3277 if (0 == rv)
3278 {
3279 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3280 mp->enable_ip6);
3281 }
3282
3283 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3284}
3285
Dave Barachb5e8a772016-12-06 12:04:42 -05003286#define vl_msg_name_crc_list
3287#include <vnet/ip/ip.api.h>
3288#undef vl_msg_name_crc_list
3289
3290static void
3291setup_message_id_table (api_main_t * am)
3292{
3293#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3294 foreach_vl_msg_name_crc_ip;
3295#undef _
3296}
3297
3298static clib_error_t *
3299ip_api_hookup (vlib_main_t * vm)
3300{
3301 api_main_t *am = &api_main;
3302
3303#define _(N,n) \
3304 vl_msg_api_set_handlers(VL_API_##N, #n, \
3305 vl_api_##n##_t_handler, \
3306 vl_noop_handler, \
3307 vl_api_##n##_t_endian, \
3308 vl_api_##n##_t_print, \
3309 sizeof(vl_api_##n##_t), 1);
3310 foreach_ip_api_msg;
3311#undef _
3312
3313 /*
3314 * Set up the (msg_name, crc, message-id) table
3315 */
3316 setup_message_id_table (am);
3317
Juraj Sloboda52574522018-05-03 10:03:50 +02003318 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3319
Dave Barachb5e8a772016-12-06 12:04:42 -05003320 return 0;
3321}
3322
3323VLIB_API_INIT_FUNCTION (ip_api_hookup);
3324
3325/*
3326 * fd.io coding-style-patch-verification: ON
3327 *
3328 * Local Variables:
3329 * eval: (c-set-style "gnu")
3330 * End:
3331 */