blob: 7217f9d101a7f990f4b6e346b0afb31febbd97d9 [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
Neale Rannsb8d44812017-11-10 06:53:54 -080023#include <vpp/stats/stats.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050024#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26#include <vnet/ethernet/ethernet.h>
27#include <vnet/ip/ip.h>
John Lo7f358b32018-04-28 01:19:24 -040028#include <vnet/ip/ip_neighbor.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050029#include <vnet/ip/ip6_neighbor.h>
30#include <vnet/fib/fib_table.h>
31#include <vnet/fib/fib_api.h>
32#include <vnet/dpo/drop_dpo.h>
33#include <vnet/dpo/receive_dpo.h>
34#include <vnet/dpo/lookup_dpo.h>
35#include <vnet/dpo/classify_dpo.h>
36#include <vnet/dpo/ip_null_dpo.h>
37#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080038#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000039#include <vnet/mfib/ip4_mfib.h>
40#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080041#include <vnet/mfib/mfib_entry.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080042#include <vnet/ip/ip_source_and_port_range_check.h>
43#include <vnet/fib/ip4_fib.h>
44#include <vnet/fib/ip6_fib.h>
45#include <vnet/ip/ip6_hop_by_hop.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020046#include <vnet/ip/ip4_reassembly.h>
47#include <vnet/ip/ip6_reassembly.h>
Neale Ranns0053de62018-05-22 08:40:52 -070048#include <vnet/ethernet/arp.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050049
50#include <vnet/vnet_msg_enum.h>
51
52#define vl_typedefs /* define message structures */
53#include <vnet/vnet_all_api_h.h>
54#undef vl_typedefs
55
56#define vl_endianfun /* define message structures */
57#include <vnet/vnet_all_api_h.h>
58#undef vl_endianfun
59
60/* instantiate all the print functions we know about */
61#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
62#define vl_printfun
63#include <vnet/vnet_all_api_h.h>
64#undef vl_printfun
65
66#include <vlibapi/api_helper_macros.h>
67
Neale Ranns5a8123b2017-01-26 01:18:23 -080068
Dave Barachb5e8a772016-12-06 12:04:42 -050069#define foreach_ip_api_msg \
70_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050071_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080072_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080073_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050074_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000075_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080076_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050077_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070078_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050079_(IP_DUMP, ip_dump) \
80_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080081_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
John Loc7b43042018-04-13 16:46:22 -040082_(IP_PROBE_NEIGHBOR, ip_probe_neighbor) \
John Lo7f358b32018-04-28 01:19:24 -040083_(IP_SCAN_NEIGHBOR_ENABLE_DISABLE, ip_scan_neighbor_enable_disable) \
Neale Rannsb8d44812017-11-10 06:53:54 -080084_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
85_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010086_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080087_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
Neale Ranns0053de62018-05-22 08:40:52 -070088_(PROXY_ARP_DUMP, proxy_arp_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080089_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
Neale Ranns0053de62018-05-22 08:40:52 -070090 _(PROXY_ARP_INTFC_DUMP, proxy_arp_intfc_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080091_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050092_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070093_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070094_(IP_PUNT_POLICE, ip_punt_police) \
95_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050096_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
97_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
98_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080099_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
100_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100101_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -0500102_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
103_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
Florin Coras595992c2017-11-06 17:17:08 -0800104 sw_interface_ip6_set_link_local_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800105_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
106_(IOAM_ENABLE, ioam_enable) \
107_(IOAM_DISABLE, ioam_disable) \
108_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
109 ip_source_and_port_range_check_add_del) \
110_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200111 ip_source_and_port_range_check_interface_add_del) \
112_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100113_(IP_REASSEMBLY_GET, ip_reassembly_get) \
114_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)
Dave Barachb5e8a772016-12-06 12:04:42 -0500115
116extern void stats_dslock_with_hint (int hint, int tag);
117extern void stats_dsunlock (void);
118
119static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600120send_ip_neighbor_details (u32 sw_if_index,
121 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500122 u8 is_static,
123 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800124 u8 * ip_address, vl_api_registration_t * reg,
125 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500126{
127 vl_api_ip_neighbor_details_t *mp;
128
129 mp = vl_msg_api_alloc (sizeof (*mp));
130 memset (mp, 0, sizeof (*mp));
131 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
132 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600133 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500134 mp->is_ipv6 = is_ipv6;
135 mp->is_static = is_static;
136 memcpy (mp->mac_address, mac_address, 6);
137 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
138
Florin Coras6c4dae22018-01-09 06:39:23 -0800139 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500140}
141
142static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500143vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
144{
Florin Coras6c4dae22018-01-09 06:39:23 -0800145 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500146
Florin Coras6c4dae22018-01-09 06:39:23 -0800147 reg = vl_api_client_index_to_registration (mp->client_index);
148 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500149 return;
150
151 u32 sw_if_index = ntohl (mp->sw_if_index);
152
153 if (mp->is_ipv6)
154 {
155 ip6_neighbor_t *n, *ns;
156
157 ns = ip6_neighbors_entries (sw_if_index);
158 /* *INDENT-OFF* */
159 vec_foreach (n, ns)
160 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500161 send_ip_neighbor_details
Brant Lin285434a2018-06-13 06:01:58 -0400162 (n->key.sw_if_index, mp->is_ipv6,
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600163 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500164 (u8 *) n->link_layer_address,
165 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800166 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500167 }
168 /* *INDENT-ON* */
169 vec_free (ns);
170 }
171 else
172 {
173 ethernet_arp_ip4_entry_t *n, *ns;
174
175 ns = ip4_neighbor_entries (sw_if_index);
176 /* *INDENT-OFF* */
177 vec_foreach (n, ns)
178 {
Brant Lin285434a2018-06-13 06:01:58 -0400179 send_ip_neighbor_details (n->sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500180 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
181 (u8*) n->ethernet_address,
182 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800183 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500184 }
185 /* *INDENT-ON* */
186 vec_free (ns);
187 }
188}
189
Dave Barachb5e8a772016-12-06 12:04:42 -0500190static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500191send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800192 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700193 const fib_table_t * table,
194 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500195 fib_route_path_encode_t * api_rpaths, u32 context)
196{
197 vl_api_ip_fib_details_t *mp;
198 fib_route_path_encode_t *api_rpath;
199 vl_api_fib_path_t *fp;
200 int path_count;
201
202 path_count = vec_len (api_rpaths);
203 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
204 if (!mp)
205 return;
206 memset (mp, 0, sizeof (*mp));
207 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
208 mp->context = context;
209
Neale Ranns2297af02017-09-12 09:45:04 -0700210 mp->table_id = htonl (table->ft_table_id);
211 memcpy (mp->table_name, table->ft_desc,
212 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500213 mp->address_length = pfx->fp_len;
214 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
215
216 mp->count = htonl (path_count);
217 fp = mp->path;
218 vec_foreach (api_rpath, api_rpaths)
219 {
Neale Ranns81458422018-03-12 06:59:36 -0700220 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500221 fp++;
222 }
223
Florin Coras6c4dae22018-01-09 06:39:23 -0800224 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500225}
226
Neale Rannsa3af3372017-03-28 03:49:52 -0700227typedef struct vl_api_ip_fib_dump_walk_ctx_t_
228{
229 fib_node_index_t *feis;
230} vl_api_ip_fib_dump_walk_ctx_t;
231
Neale Ranns89541992017-04-06 04:41:02 -0700232static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700233vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
234{
235 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
236
237 vec_add1 (ctx->feis, fei);
238
Neale Ranns89541992017-04-06 04:41:02 -0700239 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700240}
241
Dave Barachb5e8a772016-12-06 12:04:42 -0500242static void
243vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
244{
245 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800246 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500247 ip4_main_t *im = &ip4_main;
248 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700249 fib_node_index_t *lfeip;
Neale Rannsc5d43172018-07-30 08:04:40 -0700250 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500251 u32 fib_index;
252 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700253 vl_api_ip_fib_dump_walk_ctx_t ctx = {
254 .feis = NULL,
255 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500256
Florin Coras6c4dae22018-01-09 06:39:23 -0800257 reg = vl_api_client_index_to_registration (mp->client_index);
258 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500259 return;
260
261 /* *INDENT-OFF* */
262 pool_foreach (fib_table, im->fibs,
263 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700264 fib_table_walk(fib_table->ft_index,
265 FIB_PROTOCOL_IP4,
266 vl_api_ip_fib_dump_walk,
267 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500268 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500269 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500270
Neale Rannsa3af3372017-03-28 03:49:52 -0700271 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500272
Neale Rannsa3af3372017-03-28 03:49:52 -0700273 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500274 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700275 pfx = fib_entry_get_prefix (*lfeip);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 fib_index = fib_entry_get_fib_index (*lfeip);
Neale Rannsc5d43172018-07-30 08:04:40 -0700277 fib_table = fib_table_get (fib_index, pfx->fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500278 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500279 fib_entry_encode (*lfeip, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700280 send_ip_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500281 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500282 }
283
Neale Rannsa3af3372017-03-28 03:49:52 -0700284 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500285}
286
287static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500288send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800289 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800290 const fib_table_t * table,
291 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500293{
294 vl_api_ip6_fib_details_t *mp;
295 fib_route_path_encode_t *api_rpath;
296 vl_api_fib_path_t *fp;
297 int path_count;
298
Dave Barachd7cb1b52016-12-09 09:52:16 -0500299 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500300 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
301 if (!mp)
302 return;
303 memset (mp, 0, sizeof (*mp));
304 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
305 mp->context = context;
306
Neale Rannsa161a6d2017-11-14 08:10:41 -0800307 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500308 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500309 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800310 memcpy (mp->table_name, table->ft_desc,
311 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500312
313 mp->count = htonl (path_count);
314 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500315 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500316 {
Neale Ranns81458422018-03-12 06:59:36 -0700317 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500318 fp++;
319 }
320
Florin Coras6c4dae22018-01-09 06:39:23 -0800321 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500322}
323
Dave Barachd7cb1b52016-12-09 09:52:16 -0500324typedef struct apt_ip6_fib_show_ctx_t_
325{
326 u32 fib_index;
327 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500328} api_ip6_fib_show_ctx_t;
329
330static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500331api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500332{
333 api_ip6_fib_show_ctx_t *ctx = arg;
334
335 if ((kvp->key[2] >> 32) == ctx->fib_index)
336 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500338 }
339}
340
341static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800342api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500343 vl_api_ip6_fib_dump_t * mp,
344 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500345{
346 vpe_api_main_t *am = &vpe_api_main;
347 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500348 fib_node_index_t *fib_entry_index;
349 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700350 .fib_index = fib_table->ft_index,
351 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500352 };
353 fib_route_path_encode_t *api_rpaths;
Neale Rannsc5d43172018-07-30 08:04:40 -0700354 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500355
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 BV (clib_bihash_foreach_key_value_pair)
357 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
358 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500359
Dave Barachd7cb1b52016-12-09 09:52:16 -0500360 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500361
Dave Barachd7cb1b52016-12-09 09:52:16 -0500362 vec_foreach (fib_entry_index, ctx.entries)
363 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700364 pfx = fib_entry_get_prefix (*fib_entry_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 api_rpaths = NULL;
366 fib_entry_encode (*fib_entry_index, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700367 send_ip6_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 vec_free (api_rpaths);
369 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500370
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500372}
373
374static void
375vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
376{
Florin Coras6c4dae22018-01-09 06:39:23 -0800377 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500378 ip6_main_t *im6 = &ip6_main;
379 fib_table_t *fib_table;
380
Florin Coras6c4dae22018-01-09 06:39:23 -0800381 reg = vl_api_client_index_to_registration (mp->client_index);
382 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500383 return;
384
385 /* *INDENT-OFF* */
386 pool_foreach (fib_table, im6->fibs,
387 ({
Neale Ranns81458422018-03-12 06:59:36 -0700388 /* don't send link locals */
389 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
390 continue;
391
Florin Coras6c4dae22018-01-09 06:39:23 -0800392 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500393 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500394 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500395}
396
397static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800398send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800399 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800400{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800401 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800402 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800403 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800404 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800405 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800406 int path_count;
407
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800408 mfib_entry = mfib_entry_get (mfei);
409 mfib_entry_get_prefix (mfei, &pfx);
410 mfib_entry_encode (mfei, &api_rpaths);
411
Neale Ranns5a8123b2017-01-26 01:18:23 -0800412 path_count = vec_len (api_rpaths);
413 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
414 if (!mp)
415 return;
416 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700417 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800418 mp->context = context;
419
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800420 mp->rpf_id = mfib_entry->mfe_rpf_id;
421 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800422 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800423 mp->address_length = pfx.fp_len;
424 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
425 sizeof (pfx.fp_grp_addr.ip4));
426 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
427 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800428
429 mp->count = htonl (path_count);
430 fp = mp->path;
431 vec_foreach (api_rpath, api_rpaths)
432 {
Neale Ranns81458422018-03-12 06:59:36 -0700433 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800434 fp++;
435 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800436 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800437
Florin Coras6c4dae22018-01-09 06:39:23 -0800438 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800439}
440
441typedef struct vl_api_ip_mfib_dump_ctc_t_
442{
443 fib_node_index_t *entries;
444} vl_api_ip_mfib_dump_ctc_t;
445
446static int
447vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
448{
449 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
450
451 vec_add1 (ctx->entries, fei);
452
453 return (0);
454}
455
456static void
457vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
458{
Florin Coras6c4dae22018-01-09 06:39:23 -0800459 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800460 ip4_main_t *im = &ip4_main;
461 mfib_table_t *mfib_table;
462 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800463 vl_api_ip_mfib_dump_ctc_t ctx = {
464 .entries = NULL,
465 };
466
Florin Coras6c4dae22018-01-09 06:39:23 -0800467 reg = vl_api_client_index_to_registration (mp->client_index);
468 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800469 return;
470
Neale Ranns5a8123b2017-01-26 01:18:23 -0800471 /* *INDENT-OFF* */
472 pool_foreach (mfib_table, im->mfibs,
473 ({
474 ip4_mfib_table_walk(&mfib_table->v4,
475 vl_api_ip_mfib_table_dump_walk,
476 &ctx);
477
478 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
479
480 vec_foreach (mfeip, ctx.entries)
481 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800482 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800483 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800484 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800485 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800486 vec_reset_length (ctx.entries);
487
488 }));
489 /* *INDENT-ON* */
490
491 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800492}
493
494static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800495send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800496 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800497 u32 table_id,
498 mfib_prefix_t * pfx,
499 fib_route_path_encode_t * api_rpaths, u32 context)
500{
501 vl_api_ip6_mfib_details_t *mp;
502 fib_route_path_encode_t *api_rpath;
503 vl_api_fib_path_t *fp;
504 int path_count;
505
506 path_count = vec_len (api_rpaths);
507 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
508 if (!mp)
509 return;
510 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700511 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800512 mp->context = context;
513
514 mp->table_id = htonl (table_id);
515 mp->address_length = pfx->fp_len;
516 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
517 sizeof (pfx->fp_grp_addr.ip6));
518 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
519 sizeof (pfx->fp_src_addr.ip6));
520
521 mp->count = htonl (path_count);
522 fp = mp->path;
523 vec_foreach (api_rpath, api_rpaths)
524 {
Neale Ranns81458422018-03-12 06:59:36 -0700525 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800526 fp++;
527 }
528
Florin Coras6c4dae22018-01-09 06:39:23 -0800529 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800530}
531
532typedef struct vl_api_ip6_mfib_dump_ctc_t_
533{
534 fib_node_index_t *entries;
535} vl_api_ip6_mfib_dump_ctc_t;
536
537static int
538vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
539{
540 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
541
542 vec_add1 (ctx->entries, fei);
543
544 return (0);
545}
546
547static void
548vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
549{
550 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800551 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800552 ip6_main_t *im = &ip6_main;
553 mfib_table_t *mfib_table;
554 fib_node_index_t *mfeip;
555 mfib_prefix_t pfx;
556 fib_route_path_encode_t *api_rpaths = NULL;
557 vl_api_ip6_mfib_dump_ctc_t ctx = {
558 .entries = NULL,
559 };
560
Florin Coras6c4dae22018-01-09 06:39:23 -0800561 reg = vl_api_client_index_to_registration (mp->client_index);
562 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800563 return;
564
565
566 /* *INDENT-OFF* */
567 pool_foreach (mfib_table, im->mfibs,
568 ({
569 ip6_mfib_table_walk(&mfib_table->v6,
570 vl_api_ip6_mfib_table_dump_walk,
571 &ctx);
572
573 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
574
575 vec_foreach(mfeip, ctx.entries)
576 {
577 mfib_entry_get_prefix (*mfeip, &pfx);
578 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800579 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800580 mfib_table->mft_table_id,
581 &pfx, api_rpaths,
582 mp->context);
583 }
584 vec_reset_length (api_rpaths);
585 vec_reset_length (ctx.entries);
586
587 }));
588 /* *INDENT-ON* */
589
590 vec_free (ctx.entries);
591 vec_free (api_rpaths);
592}
593
594static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700595vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
596 vlib_main_t * vm)
597{
598 vl_api_ip_punt_police_reply_t *rmp;
599 int rv = 0;
600
601 if (mp->is_ip6)
602 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
603 else
604 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
605
606 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
607}
608
609static void
610vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
611 vlib_main_t * vm)
612{
613 vl_api_ip_punt_redirect_reply_t *rmp;
614 int rv = 0;
615
616 if (mp->is_add)
617 {
618 ip46_address_t nh;
619
620 memset (&nh, 0, sizeof (nh));
621
622 if (mp->is_ip6)
623 {
624 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
625
626 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
627 ntohl (mp->tx_sw_if_index), &nh);
628 }
629 else
630 {
631 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
632
633 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
634 ntohl (mp->tx_sw_if_index), &nh);
635 }
636 }
637 else
638 {
639 if (mp->is_ip6)
640 {
641 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
642 }
643 else
644 {
645 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
646 }
647 }
648
649 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
650}
651
652static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500653vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
654 vlib_main_t * vm)
655{
656 vl_api_ip_neighbor_add_del_reply_t *rmp;
657 vnet_main_t *vnm = vnet_get_main ();
658 int rv = 0;
659
660 VALIDATE_SW_IF_INDEX (mp);
661
662 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
663
664 /*
665 * there's no validation here of the ND/ARP entry being added.
666 * The expectation is that the FIB will ensure that nothing bad
667 * will come of adding bogus entries.
668 */
669 if (mp->is_ipv6)
670 {
671 if (mp->is_add)
672 rv = vnet_set_ip6_ethernet_neighbor
673 (vm, ntohl (mp->sw_if_index),
674 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800675 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
676 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500677 else
678 rv = vnet_unset_ip6_ethernet_neighbor
679 (vm, ntohl (mp->sw_if_index),
680 (ip6_address_t *) (mp->dst_address),
681 mp->mac_address, sizeof (mp->mac_address));
682 }
683 else
684 {
685 ethernet_arp_ip4_over_ethernet_address_t a;
686
687 clib_memcpy (&a.ethernet, mp->mac_address, 6);
688 clib_memcpy (&a.ip4, mp->dst_address, 4);
689
690 if (mp->is_add)
691 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800692 &a, mp->is_static,
693 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500694 else
695 rv =
696 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
697 }
698
Dave Barachb5e8a772016-12-06 12:04:42 -0500699 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700700
701 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500702 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
703}
704
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700705void
Neale Ranns15002542017-09-10 04:39:11 -0700706ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
707{
708 u32 fib_index, mfib_index;
709
710 /*
711 * ignore action on the default table - this is always present
712 * and cannot be added nor deleted from the API
713 */
714 if (0 != table_id)
715 {
716 /*
717 * The API holds only one lock on the table.
718 * i.e. it can be added many times via the API but needs to be
719 * deleted only once.
720 * The FIB index for unicast and multicast is not necessarily the
721 * same, since internal VPP systesm (like LISP and SR) create
722 * their own unicast tables.
723 */
724 fib_index = fib_table_find (fproto, table_id);
725 mfib_index = mfib_table_find (fproto, table_id);
726
727 if (~0 != fib_index)
728 {
729 fib_table_unlock (fib_index, fproto,
730 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
731 }
732 if (~0 != mfib_index)
733 {
734 mfib_table_unlock (mfib_index, fproto,
735 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
736 }
737 }
738}
739
740void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700741vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
742{
743 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700744 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
745 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700746 int rv = 0;
747
Neale Ranns15002542017-09-10 04:39:11 -0700748 if (mp->is_add)
749 {
Neale Ranns2297af02017-09-12 09:45:04 -0700750 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700751 }
752 else
753 {
754 ip_table_delete (fproto, table_id, 1);
755 }
756
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700757 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
758}
759
Dave Barachb5e8a772016-12-06 12:04:42 -0500760int
761add_del_route_t_handler (u8 is_multipath,
762 u8 is_add,
763 u8 is_drop,
764 u8 is_unreach,
765 u8 is_prohibit,
766 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800767 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500768 u8 is_classify,
769 u32 classify_table_index,
770 u8 is_resolve_host,
771 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800772 u8 is_interface_rx,
773 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800774 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700775 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800776 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500777 u32 fib_index,
778 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700779 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500780 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800781 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500782 u32 next_hop_sw_if_index,
783 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700784 u16 next_hop_weight,
785 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500786 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800787 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500788{
789 vnet_classify_main_t *cm = &vnet_classify_main;
790 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
791 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700792 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500793 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
794 .frp_sw_if_index = next_hop_sw_if_index,
795 .frp_fib_index = next_hop_fib_index,
796 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700797 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500798 .frp_label_stack = next_hop_out_label_stack,
799 };
800 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800801 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500802
Neale Rannscaac3502017-09-18 18:04:10 -0700803 /*
804 * the special INVALID label meams we are not recursing via a
805 * label. Exp-null value is never a valid via-label so that
806 * also means it's not a via-label and means clients that set
807 * it to 0 by default get the expected behaviour
808 */
809 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500810 {
Neale Rannsda78f952017-05-24 09:15:43 -0700811 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500812 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800813 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500814 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800815 if (is_dvr)
816 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500817 if (is_resolve_host)
818 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
819 if (is_resolve_attached)
820 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800821 if (is_interface_rx)
822 path_flags |= FIB_ROUTE_PATH_INTF_RX;
823 if (is_rpf_id)
824 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700825 if (is_source_lookup)
826 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800827 if (is_multicast)
828 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800829 if (is_udp_encap)
830 {
831 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
832 path.frp_udp_encap_id = next_hop_id;
833 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800834 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
835 && path.frp_fib_index != ~0)
836 {
837 path_flags |= FIB_ROUTE_PATH_DEAG;
838 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500839
840 path.frp_flags = path_flags;
841
842 if (is_multipath)
843 {
844 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
845
846
847 vec_add1 (paths, path);
848
849 if (is_add)
850 fib_table_entry_path_add2 (fib_index,
851 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800852 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500853 else
854 fib_table_entry_path_remove2 (fib_index,
855 prefix, FIB_SOURCE_API, paths);
856
857 vec_free (paths);
858 stats_dsunlock ();
859 return 0;
860 }
861
862 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
863
864 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
865 {
866 /*
867 * special route types that link directly to the adj
868 */
869 if (is_add)
870 {
871 dpo_id_t dpo = DPO_INVALID;
872 dpo_proto_t dproto;
873
874 dproto = fib_proto_to_dpo (prefix->fp_proto);
875
876 if (is_drop)
877 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
878 else if (is_local)
879 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
880 else if (is_unreach)
881 ip_null_dpo_add_and_lock (dproto,
882 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
883 else if (is_prohibit)
884 ip_null_dpo_add_and_lock (dproto,
885 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
886 &dpo);
887 else if (is_classify)
888 {
889 if (pool_is_free_index (cm->tables,
890 ntohl (classify_table_index)))
891 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500892 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500893 return VNET_API_ERROR_NO_SUCH_TABLE;
894 }
895
896 dpo_set (&dpo, DPO_CLASSIFY, dproto,
897 classify_dpo_create (dproto,
898 ntohl (classify_table_index)));
899 }
900 else
901 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500902 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500903 return VNET_API_ERROR_NO_SUCH_TABLE;
904 }
905
906 fib_table_entry_special_dpo_update (fib_index,
907 prefix,
908 FIB_SOURCE_API,
909 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
910 dpo_reset (&dpo);
911 }
912 else
913 {
914 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
915 }
916 }
917 else
918 {
919 if (is_add)
920 {
921 vec_add1 (paths, path);
922 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800923 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500924 vec_free (paths);
925 }
926 else
927 {
928 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
929 }
930 }
931
Dave Barachd7cb1b52016-12-09 09:52:16 -0500932 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500933 return (0);
934}
935
936int
937add_del_route_check (fib_protocol_t table_proto,
938 u32 table_id,
939 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700940 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500941 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800942 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500943{
944 vnet_main_t *vnm = vnet_get_main ();
945
946 *fib_index = fib_table_find (table_proto, ntohl (table_id));
947 if (~0 == *fib_index)
948 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800949 /* No such VRF, and we weren't asked to create one */
950 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500951 }
952
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800953 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500954 {
955 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
956 ntohl (next_hop_sw_if_index)))
957 {
958 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
959 }
960 }
961 else
962 {
Neale Rannsda78f952017-05-24 09:15:43 -0700963 fib_protocol_t fib_nh_proto;
964
965 if (next_hop_table_proto > DPO_PROTO_MPLS)
966 return (0);
967
968 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
969
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800970 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700971 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800972 ntohl (next_hop_table_id));
973 else
Neale Rannsda78f952017-05-24 09:15:43 -0700974 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800975 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500976
977 if (~0 == *next_hop_fib_index)
978 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800979 /* No such VRF, and we weren't asked to create one */
980 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500981 }
982 }
983
984 return (0);
985}
986
987static int
988ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
989{
990 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800991 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500992 int rv, ii, n_labels;;
993
994 rv = add_del_route_check (FIB_PROTOCOL_IP4,
995 mp->table_id,
996 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700997 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500998 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700999 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001000
1001 if (0 != rv)
1002 return (rv);
1003
1004 fib_prefix_t pfx = {
1005 .fp_len = mp->dst_address_length,
1006 .fp_proto = FIB_PROTOCOL_IP4,
1007 };
1008 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1009
1010 ip46_address_t nh;
1011 memset (&nh, 0, sizeof (nh));
1012 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1013
1014 n_labels = mp->next_hop_n_out_labels;
1015 if (n_labels == 0)
1016 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001017 else
1018 {
1019 vec_validate (label_stack, n_labels - 1);
1020 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001021 {
1022 label_stack[ii].fml_value =
1023 ntohl (mp->next_hop_out_label_stack[ii].label);
1024 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1025 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1026 label_stack[ii].fml_mode =
1027 (mp->next_hop_out_label_stack[ii].is_uniform ?
1028 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1029 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001030 }
1031
1032 return (add_del_route_t_handler (mp->is_multipath,
1033 mp->is_add,
1034 mp->is_drop,
1035 mp->is_unreach,
1036 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001037 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001038 mp->is_classify,
1039 mp->classify_table_index,
1040 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001041 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001042 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001043 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001044 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001045 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001046 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001047 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001048 ntohl (mp->next_hop_sw_if_index),
1049 next_hop_fib_index,
1050 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001051 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001052 ntohl (mp->next_hop_via_label),
1053 label_stack));
1054}
1055
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001056static int
Dave Barachb5e8a772016-12-06 12:04:42 -05001057ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1058{
Neale Ranns31ed7442018-02-23 05:29:09 -08001059 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001060 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001061 int rv, ii, n_labels;;
1062
1063 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1064 mp->table_id,
1065 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001066 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001067 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001068 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001069
1070 if (0 != rv)
1071 return (rv);
1072
1073 fib_prefix_t pfx = {
1074 .fp_len = mp->dst_address_length,
1075 .fp_proto = FIB_PROTOCOL_IP6,
1076 };
1077 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1078
1079 ip46_address_t nh;
1080 memset (&nh, 0, sizeof (nh));
1081 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1082
1083 n_labels = mp->next_hop_n_out_labels;
1084 if (n_labels == 0)
1085 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001086 else
1087 {
1088 vec_validate (label_stack, n_labels - 1);
1089 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001090 {
1091 label_stack[ii].fml_value =
1092 ntohl (mp->next_hop_out_label_stack[ii].label);
1093 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1094 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1095 label_stack[ii].fml_mode =
1096 (mp->next_hop_out_label_stack[ii].is_uniform ?
1097 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1098 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001099 }
1100
1101 return (add_del_route_t_handler (mp->is_multipath,
1102 mp->is_add,
1103 mp->is_drop,
1104 mp->is_unreach,
1105 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001106 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001107 mp->is_classify,
1108 mp->classify_table_index,
1109 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001110 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001111 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001112 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001113 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001114 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001115 &nh, ntohl (mp->next_hop_id),
1116 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001117 next_hop_fib_index,
1118 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001119 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001120 ntohl (mp->next_hop_via_label),
1121 label_stack));
1122}
1123
1124void
1125vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1126{
1127 vl_api_ip_add_del_route_reply_t *rmp;
1128 int rv;
1129 vnet_main_t *vnm = vnet_get_main ();
1130
1131 vnm->api_errno = 0;
1132
1133 if (mp->is_ipv6)
1134 rv = ip6_add_del_route_t_handler (mp);
1135 else
1136 rv = ip4_add_del_route_t_handler (mp);
1137
1138 rv = (rv == 0) ? vnm->api_errno : rv;
1139
1140 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1141}
1142
Neale Ranns15002542017-09-10 04:39:11 -07001143void
Neale Ranns2297af02017-09-12 09:45:04 -07001144ip_table_create (fib_protocol_t fproto,
1145 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001146{
1147 u32 fib_index, mfib_index;
1148
1149 /*
1150 * ignore action on the default table - this is always present
1151 * and cannot be added nor deleted from the API
1152 */
1153 if (0 != table_id)
1154 {
1155 /*
1156 * The API holds only one lock on the table.
1157 * i.e. it can be added many times via the API but needs to be
1158 * deleted only once.
1159 * The FIB index for unicast and multicast is not necessarily the
1160 * same, since internal VPP systesm (like LISP and SR) create
1161 * their own unicast tables.
1162 */
1163 fib_index = fib_table_find (fproto, table_id);
1164 mfib_index = mfib_table_find (fproto, table_id);
1165
1166 if (~0 == fib_index)
1167 {
Neale Ranns2297af02017-09-12 09:45:04 -07001168 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1169 (is_api ?
1170 FIB_SOURCE_API :
1171 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001172 }
1173 if (~0 == mfib_index)
1174 {
Neale Ranns2297af02017-09-12 09:45:04 -07001175 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1176 (is_api ?
1177 MFIB_SOURCE_API :
1178 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001179 }
1180 }
1181}
1182
Neale Ranns32e1c012016-11-22 17:07:28 +00001183static int
1184add_del_mroute_check (fib_protocol_t table_proto,
1185 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001186 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001187{
1188 vnet_main_t *vnm = vnet_get_main ();
1189
1190 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1191 if (~0 == *fib_index)
1192 {
Neale Ranns15002542017-09-10 04:39:11 -07001193 /* No such table */
1194 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001195 }
1196
1197 if (~0 != ntohl (next_hop_sw_if_index))
1198 {
1199 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1200 ntohl (next_hop_sw_if_index)))
1201 {
1202 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1203 }
1204 }
1205
1206 return (0);
1207}
1208
1209static int
1210mroute_add_del_handler (u8 is_add,
1211 u8 is_local,
1212 u32 fib_index,
1213 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001214 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001215 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001216 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001217 u32 next_hop_sw_if_index,
1218 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001219{
1220 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1221
1222 fib_route_path_t path = {
1223 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001224 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001225 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001226 };
1227
1228 if (is_local)
1229 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1230
Neale Rannsd792d9c2017-10-21 10:53:20 -07001231 if (DPO_PROTO_BIER == nh_proto)
1232 {
1233 path.frp_bier_imp = bier_imp;
1234 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1235 }
1236 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001237 {
1238 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001239 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001240 goto done;
1241 }
1242
1243 if (is_add)
1244 {
1245 mfib_table_entry_path_update (fib_index, prefix,
1246 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001247 }
1248 else
1249 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001250 mfib_table_entry_path_remove (fib_index, prefix,
1251 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001252 }
1253
Neale Rannsd792d9c2017-10-21 10:53:20 -07001254done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001255 stats_dsunlock ();
1256 return (0);
1257}
1258
1259static int
1260api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1261{
1262 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001263 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001264 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001265 u32 fib_index;
1266 int rv;
1267
Neale Rannsd792d9c2017-10-21 10:53:20 -07001268 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001269 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1270 rv = add_del_mroute_check (fproto,
1271 mp->table_id,
1272 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001273 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001274
1275 if (0 != rv)
1276 return (rv);
1277
1278 mfib_prefix_t pfx = {
1279 .fp_len = ntohs (mp->grp_address_length),
1280 .fp_proto = fproto,
1281 };
1282
1283 if (FIB_PROTOCOL_IP4 == fproto)
1284 {
1285 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1286 sizeof (pfx.fp_grp_addr.ip4));
1287 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1288 sizeof (pfx.fp_src_addr.ip4));
Neale Rannse821ab12017-06-01 07:45:05 -07001289 memset (&nh.ip6, 0, sizeof (nh.ip6));
1290 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns32e1c012016-11-22 17:07:28 +00001291 }
1292 else
1293 {
1294 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1295 sizeof (pfx.fp_grp_addr.ip6));
1296 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1297 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001298 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns32e1c012016-11-22 17:07:28 +00001299 }
1300
1301 return (mroute_add_del_handler (mp->is_add,
1302 mp->is_local,
1303 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001304 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001305 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001306 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001307 ntohl (mp->next_hop_sw_if_index),
Neale Rannse821ab12017-06-01 07:45:05 -07001308 &nh,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001309 ntohl (mp->itf_flags),
1310 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001311}
1312
1313void
1314vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1315{
1316 vl_api_ip_mroute_add_del_reply_t *rmp;
1317 int rv;
1318 vnet_main_t *vnm = vnet_get_main ();
1319
1320 vnm->api_errno = 0;
1321
1322 rv = api_mroute_add_del_t_handler (mp);
1323
1324 rv = (rv == 0) ? vnm->api_errno : rv;
1325
1326 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1327}
1328
Dave Barachb5e8a772016-12-06 12:04:42 -05001329static void
1330send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001331 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1332 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001333{
1334 vl_api_ip_details_t *mp;
1335
1336 mp = vl_msg_api_alloc (sizeof (*mp));
1337 memset (mp, 0, sizeof (*mp));
1338 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1339
1340 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001341 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001342 mp->context = context;
1343
Florin Coras6c4dae22018-01-09 06:39:23 -08001344 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001345}
1346
1347static void
1348send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001349 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001350 u8 * ip, u16 prefix_length,
1351 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001352{
1353 vl_api_ip_address_details_t *mp;
1354
1355 mp = vl_msg_api_alloc (sizeof (*mp));
1356 memset (mp, 0, sizeof (*mp));
1357 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1358
1359 if (is_ipv6)
1360 {
1361 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1362 }
1363 else
1364 {
1365 u32 *tp = (u32 *) mp->ip;
1366 *tp = *(u32 *) ip;
1367 }
1368 mp->prefix_length = prefix_length;
1369 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001370 mp->sw_if_index = htonl (sw_if_index);
1371 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001372
Florin Coras6c4dae22018-01-09 06:39:23 -08001373 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001374}
1375
1376static void
1377vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1378{
1379 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001380 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001381 ip6_address_t *r6;
1382 ip4_address_t *r4;
1383 ip6_main_t *im6 = &ip6_main;
1384 ip4_main_t *im4 = &ip4_main;
1385 ip_lookup_main_t *lm6 = &im6->lookup_main;
1386 ip_lookup_main_t *lm4 = &im4->lookup_main;
1387 ip_interface_address_t *ia = 0;
1388 u32 sw_if_index = ~0;
1389 int rv __attribute__ ((unused)) = 0;
1390
1391 VALIDATE_SW_IF_INDEX (mp);
1392
1393 sw_if_index = ntohl (mp->sw_if_index);
1394
Florin Coras6c4dae22018-01-09 06:39:23 -08001395 reg = vl_api_client_index_to_registration (mp->client_index);
1396 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001397 return;
1398
Dave Barachb5e8a772016-12-06 12:04:42 -05001399 if (mp->is_ipv6)
1400 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001401 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001402 /* Do not send subnet details of the IP-interface for
1403 * unnumbered interfaces. otherwise listening clients
1404 * will be confused that the subnet is applied on more
1405 * than one interface */
1406 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001407 ({
1408 r6 = ip_interface_address_get_address (lm6, ia);
1409 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001410 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001411 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001412 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001413 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001414 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001415 else
1416 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001417 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001418 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001419 ({
1420 r4 = ip_interface_address_get_address (lm4, ia);
1421 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001422 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001423 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001424 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001425 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001426 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001427 BAD_SW_IF_INDEX_LABEL;
1428}
1429
1430static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001431send_ip_unnumbered_details (vpe_api_main_t * am,
1432 vl_api_registration_t * reg,
1433 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1434{
1435 vl_api_ip_unnumbered_details_t *mp;
1436
1437 mp = vl_msg_api_alloc (sizeof (*mp));
1438 memset (mp, 0, sizeof (*mp));
1439 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1440
1441 mp->context = context;
1442 mp->sw_if_index = htonl (sw_if_index);
1443 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1444
1445 vl_api_send_msg (reg, (u8 *) mp);
1446}
1447
1448static void
1449vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1450{
1451 vnet_main_t *vnm = vnet_get_main ();
1452 vnet_interface_main_t *im = &vnm->interface_main;
1453 int rv __attribute__ ((unused)) = 0;
1454 vpe_api_main_t *am = &vpe_api_main;
1455 vl_api_registration_t *reg;
1456 vnet_sw_interface_t *si;
1457 u32 sw_if_index;
1458
1459 sw_if_index = ntohl (mp->sw_if_index);
1460
1461 reg = vl_api_client_index_to_registration (mp->client_index);
1462 if (!reg)
1463 return;
1464
1465 if (~0 != sw_if_index)
1466 {
1467 VALIDATE_SW_IF_INDEX (mp);
1468
1469 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1470
1471 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1472 {
1473 send_ip_unnumbered_details (am, reg,
1474 sw_if_index,
1475 si->unnumbered_sw_if_index,
1476 mp->context);
1477 }
1478 }
1479 else
1480 {
1481 /* *INDENT-OFF* */
1482 pool_foreach (si, im->sw_interfaces,
1483 ({
1484 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1485 {
1486 send_ip_unnumbered_details(am, reg,
1487 si->sw_if_index,
1488 si->unnumbered_sw_if_index,
1489 mp->context);
1490 }
1491 }));
1492 /* *INDENT-ON* */
1493 }
1494
1495 BAD_SW_IF_INDEX_LABEL;
1496}
1497
1498static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001499vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1500{
1501 vpe_api_main_t *am = &vpe_api_main;
1502 vnet_main_t *vnm = vnet_get_main ();
1503 vlib_main_t *vm = vlib_get_main ();
1504 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001505 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001506 vnet_sw_interface_t *si, *sorted_sis;
1507 u32 sw_if_index = ~0;
1508
Florin Coras6c4dae22018-01-09 06:39:23 -08001509 reg = vl_api_client_index_to_registration (mp->client_index);
1510 if (!reg)
1511 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001512
1513 /* Gather interfaces. */
1514 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1515 _vec_len (sorted_sis) = 0;
1516 /* *INDENT-OFF* */
1517 pool_foreach (si, im->sw_interfaces,
1518 ({
1519 vec_add1 (sorted_sis, si[0]);
1520 }));
1521 /* *INDENT-ON* */
1522
1523 vec_foreach (si, sorted_sis)
1524 {
1525 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1526 {
1527 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1528 {
1529 continue;
1530 }
1531 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001532 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001533 }
1534 }
1535}
1536
1537static void
1538set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1539{
1540 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001541 int rv;
1542 u32 table_id;
1543 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001544
Neale Ranns227038a2017-04-21 01:07:59 -07001545 table_id = ntohl (mp->vrf_id);
1546
1547#define _(a,b) if (mp->a) flow_hash_config |= b;
1548 foreach_flow_hash_bit;
1549#undef _
1550
1551 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001552
1553 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1554}
1555
1556static void
1557set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1558{
1559 vl_api_set_ip_flow_hash_reply_t *rmp;
1560 int rv;
1561 u32 table_id;
1562 flow_hash_config_t flow_hash_config = 0;
1563
1564 table_id = ntohl (mp->vrf_id);
1565
1566#define _(a,b) if (mp->a) flow_hash_config |= b;
1567 foreach_flow_hash_bit;
1568#undef _
1569
1570 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1571
1572 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1573}
1574
1575
1576static void
1577vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1578{
1579 if (mp->is_ipv6 == 0)
1580 set_ip4_flow_hash (mp);
1581 else
1582 set_ip6_flow_hash (mp);
1583}
1584
1585static void
1586 vl_api_sw_interface_ip6nd_ra_config_t_handler
1587 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1588{
1589 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1590 vlib_main_t *vm = vlib_get_main ();
1591 int rv = 0;
1592 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1593 default_router;
1594
1595 is_no = mp->is_no == 1;
1596 suppress = mp->suppress == 1;
1597 managed = mp->managed == 1;
1598 other = mp->other == 1;
1599 ll_option = mp->ll_option == 1;
1600 send_unicast = mp->send_unicast == 1;
1601 cease = mp->cease == 1;
1602 default_router = mp->default_router == 1;
1603
1604 VALIDATE_SW_IF_INDEX (mp);
1605
1606 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1607 suppress, managed, other,
1608 ll_option, send_unicast, cease,
1609 default_router, ntohl (mp->lifetime),
1610 ntohl (mp->initial_count),
1611 ntohl (mp->initial_interval),
1612 ntohl (mp->max_interval),
1613 ntohl (mp->min_interval), is_no);
1614
1615 BAD_SW_IF_INDEX_LABEL;
1616
1617 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1618}
1619
1620static void
1621 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1622 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1623{
1624 vlib_main_t *vm = vlib_get_main ();
1625 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1626 int rv = 0;
1627 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1628
1629 VALIDATE_SW_IF_INDEX (mp);
1630
1631 is_no = mp->is_no == 1;
1632 use_default = mp->use_default == 1;
1633 no_advertise = mp->no_advertise == 1;
1634 off_link = mp->off_link == 1;
1635 no_autoconfig = mp->no_autoconfig == 1;
1636 no_onlink = mp->no_onlink == 1;
1637
1638 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1639 (ip6_address_t *) mp->address,
1640 mp->address_length, use_default,
1641 ntohl (mp->val_lifetime),
1642 ntohl (mp->pref_lifetime), no_advertise,
1643 off_link, no_autoconfig, no_onlink, is_no);
1644
1645 BAD_SW_IF_INDEX_LABEL;
1646 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1647}
1648
1649static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001650send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001651 u32 context,
1652 const ip46_address_t * addr, u32 sw_if_index)
1653{
1654 vl_api_ip6nd_proxy_details_t *mp;
1655
1656 mp = vl_msg_api_alloc (sizeof (*mp));
1657 memset (mp, 0, sizeof (*mp));
1658 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1659 mp->context = context;
1660 mp->sw_if_index = htonl (sw_if_index);
1661 memcpy (mp->address, addr, 16);
1662
Florin Coras6c4dae22018-01-09 06:39:23 -08001663 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001664}
1665
Neale Ranns3f844d02017-02-18 00:03:54 -08001666typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1667{
1668 u32 *indices;
1669} api_ip6nd_proxy_fib_table_walk_ctx_t;
1670
Neale Ranns89541992017-04-06 04:41:02 -07001671static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001672api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1673{
1674 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1675
1676 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1677 {
1678 vec_add1 (ctx->indices, fei);
1679 }
1680
Neale Ranns89541992017-04-06 04:41:02 -07001681 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001682}
1683
1684static void
1685vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1686{
1687 ip6_main_t *im6 = &ip6_main;
1688 fib_table_t *fib_table;
1689 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1690 .indices = NULL,
1691 };
1692 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001693 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001694 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001695
Florin Coras6c4dae22018-01-09 06:39:23 -08001696 reg = vl_api_client_index_to_registration (mp->client_index);
1697 if (!reg)
1698 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001699
1700 /* *INDENT-OFF* */
1701 pool_foreach (fib_table, im6->fibs,
1702 ({
1703 fib_table_walk(fib_table->ft_index,
1704 FIB_PROTOCOL_IP6,
1705 api_ip6nd_proxy_fib_table_walk,
1706 &ctx);
1707 }));
1708 /* *INDENT-ON* */
1709
1710 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1711
1712 vec_foreach (feip, ctx.indices)
1713 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001714 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001715
Florin Coras6c4dae22018-01-09 06:39:23 -08001716 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001717 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001718 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001719 fib_entry_get_resolving_interface (*feip));
1720 }
1721
1722 vec_free (ctx.indices);
1723}
1724
1725static void
1726vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1727{
1728 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1729 int rv = 0;
1730
1731 VALIDATE_SW_IF_INDEX (mp);
1732
1733 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1734 (ip6_address_t *) mp->address, mp->is_del);
1735
1736 BAD_SW_IF_INDEX_LABEL;
1737 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1738}
1739
1740static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001741 vl_api_ip6nd_send_router_solicitation_t_handler
1742 (vl_api_ip6nd_send_router_solicitation_t * mp)
1743{
1744 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1745 icmp6_send_router_solicitation_params_t params;
1746 vlib_main_t *vm = vlib_get_main ();
1747 int rv = 0;
1748
1749 VALIDATE_SW_IF_INDEX (mp);
1750
1751 BAD_SW_IF_INDEX_LABEL;
1752 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1753
1754 if (rv != 0)
1755 return;
1756
1757 params.irt = ntohl (mp->irt);
1758 params.mrt = ntohl (mp->mrt);
1759 params.mrc = ntohl (mp->mrc);
1760 params.mrd = ntohl (mp->mrd);
1761
1762 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1763 &params);
1764}
1765
1766static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001767 vl_api_sw_interface_ip6_enable_disable_t_handler
1768 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1769{
1770 vlib_main_t *vm = vlib_get_main ();
1771 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1772 vnet_main_t *vnm = vnet_get_main ();
1773 int rv = 0;
1774 clib_error_t *error;
1775
1776 vnm->api_errno = 0;
1777
1778 VALIDATE_SW_IF_INDEX (mp);
1779
1780 error =
1781 (mp->enable == 1) ? enable_ip6_interface (vm,
1782 ntohl (mp->sw_if_index)) :
1783 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1784
1785 if (error)
1786 {
1787 clib_error_report (error);
1788 rv = VNET_API_ERROR_UNSPECIFIED;
1789 }
1790 else
1791 {
1792 rv = vnm->api_errno;
1793 }
1794
1795 BAD_SW_IF_INDEX_LABEL;
1796
1797 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1798}
1799
1800static void
1801 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1802 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1803{
1804 vlib_main_t *vm = vlib_get_main ();
1805 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1806 int rv = 0;
1807 clib_error_t *error;
1808 vnet_main_t *vnm = vnet_get_main ();
1809
1810 vnm->api_errno = 0;
1811
1812 VALIDATE_SW_IF_INDEX (mp);
1813
1814 error = set_ip6_link_local_address (vm,
1815 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001816 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001817 if (error)
1818 {
1819 clib_error_report (error);
1820 rv = VNET_API_ERROR_UNSPECIFIED;
1821 }
1822 else
1823 {
1824 rv = vnm->api_errno;
1825 }
1826
1827 BAD_SW_IF_INDEX_LABEL;
1828
1829 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1830}
1831
Neale Ranns32e1c012016-11-22 17:07:28 +00001832void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001833vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001834 u32 context, const mfib_signal_t * mfs)
1835{
1836 vl_api_mfib_signal_details_t *mp;
1837 mfib_prefix_t prefix;
1838 mfib_table_t *mfib;
1839 mfib_itf_t *mfi;
1840
1841 mp = vl_msg_api_alloc (sizeof (*mp));
1842
1843 memset (mp, 0, sizeof (*mp));
1844 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1845 mp->context = context;
1846
1847 mfi = mfib_itf_get (mfs->mfs_itf);
1848 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1849 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1850 prefix.fp_proto);
1851 mp->table_id = ntohl (mfib->mft_table_id);
1852 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1853
1854 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1855 {
1856 mp->grp_address_len = ntohs (prefix.fp_len);
1857
1858 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1859 if (prefix.fp_len > 32)
1860 {
1861 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1862 }
1863 }
1864 else
1865 {
1866 mp->grp_address_len = ntohs (prefix.fp_len);
1867
1868 ASSERT (0);
1869 }
1870
1871 if (0 != mfs->mfs_buffer_len)
1872 {
1873 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1874
1875 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1876 }
1877 else
1878 {
1879 mp->ip_packet_len = 0;
1880 }
1881
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001882 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001883}
1884
1885static void
1886vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1887{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001888 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001889
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001890 reg = vl_api_client_index_to_registration (mp->client_index);
1891 if (!reg)
1892 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001893
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001894 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001895 ;
1896}
Dave Barachb5e8a772016-12-06 12:04:42 -05001897
Florin Coras595992c2017-11-06 17:17:08 -08001898static void
1899 vl_api_ip_container_proxy_add_del_t_handler
1900 (vl_api_ip_container_proxy_add_del_t * mp)
1901{
1902 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1903 vnet_ip_container_proxy_args_t args;
1904 int rv = 0;
1905 clib_error_t *error;
1906
1907 memset (&args, 0, sizeof (args));
1908 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1909 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1910 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1911 args.is_add = mp->is_add;
1912 if ((error = vnet_ip_container_proxy_add_del (&args)))
1913 {
1914 rv = clib_error_get_code (error);
1915 clib_error_report (error);
1916 }
1917
1918 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1919}
1920
Neale Rannsb8d44812017-11-10 06:53:54 -08001921static void
1922vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1923{
1924 int rv = 0;
1925 vl_api_ioam_enable_reply_t *rmp;
1926 clib_error_t *error;
1927
1928 /* Ignoring the profile id as currently a single profile
1929 * is supported */
1930 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1931 mp->seqno, mp->analyse);
1932 if (error)
1933 {
1934 clib_error_report (error);
1935 rv = clib_error_get_code (error);
1936 }
1937
1938 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1939}
1940
1941static void
1942vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1943{
1944 int rv = 0;
1945 vl_api_ioam_disable_reply_t *rmp;
1946 clib_error_t *error;
1947
1948 error = clear_ioam_rewrite_fn ();
1949 if (error)
1950 {
1951 clib_error_report (error);
1952 rv = clib_error_get_code (error);
1953 }
1954
1955 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1956}
1957
1958static void
1959 vl_api_ip_source_and_port_range_check_add_del_t_handler
1960 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1961{
1962 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1963 int rv = 0;
1964
1965 u8 is_ipv6 = mp->is_ipv6;
1966 u8 is_add = mp->is_add;
1967 u8 mask_length = mp->mask_length;
1968 ip4_address_t ip4_addr;
1969 ip6_address_t ip6_addr;
1970 u16 *low_ports = 0;
1971 u16 *high_ports = 0;
1972 u32 vrf_id;
1973 u16 tmp_low, tmp_high;
1974 u8 num_ranges;
1975 int i;
1976
1977 // Validate port range
1978 num_ranges = mp->number_of_ranges;
1979 if (num_ranges > 32)
1980 { // This is size of array in VPE.API
1981 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1982 goto reply;
1983 }
1984
1985 vec_reset_length (low_ports);
1986 vec_reset_length (high_ports);
1987
1988 for (i = 0; i < num_ranges; i++)
1989 {
1990 tmp_low = mp->low_ports[i];
1991 tmp_high = mp->high_ports[i];
1992 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1993 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1994 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1995 {
1996 rv = VNET_API_ERROR_INVALID_VALUE;
1997 goto reply;
1998 }
1999 vec_add1 (low_ports, tmp_low);
2000 vec_add1 (high_ports, tmp_high + 1);
2001 }
2002
2003 // Validate mask_length
2004 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2005 {
2006 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2007 goto reply;
2008 }
2009
2010 vrf_id = ntohl (mp->vrf_id);
2011
2012 if (vrf_id < 1)
2013 {
2014 rv = VNET_API_ERROR_INVALID_VALUE;
2015 goto reply;
2016 }
2017
2018
2019 if (is_ipv6)
2020 {
2021 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2022 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2023 mask_length,
2024 vrf_id,
2025 low_ports,
2026 high_ports, is_add);
2027 }
2028 else
2029 {
2030 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2031 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2032 mask_length,
2033 vrf_id,
2034 low_ports,
2035 high_ports, is_add);
2036 }
2037
2038reply:
2039 vec_free (low_ports);
2040 vec_free (high_ports);
2041 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2042}
2043
2044static void
2045 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2046 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2047{
2048 vlib_main_t *vm = vlib_get_main ();
2049 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2050 ip4_main_t *im = &ip4_main;
2051 int rv;
2052 u32 sw_if_index;
2053 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2054 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2055 uword *p = 0;
2056 int i;
2057
2058 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2059 ntohl (mp->tcp_out_vrf_id);
2060 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2061 ntohl (mp->udp_out_vrf_id);
2062 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2063 ntohl (mp->tcp_in_vrf_id);
2064 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2065 ntohl (mp->udp_in_vrf_id);
2066
2067
2068 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2069 {
2070 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2071 {
2072 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2073
2074 if (p == 0)
2075 {
2076 rv = VNET_API_ERROR_INVALID_VALUE;
2077 goto reply;
2078 }
2079
2080 fib_index[i] = p[0];
2081 }
2082 else
2083 fib_index[i] = ~0;
2084 }
2085 sw_if_index = ntohl (mp->sw_if_index);
2086
2087 VALIDATE_SW_IF_INDEX (mp);
2088
2089 rv =
2090 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2091 mp->is_add);
2092
2093 BAD_SW_IF_INDEX_LABEL;
2094reply:
2095
2096 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2097}
2098
2099#define IP4_ARP_EVENT 3
2100#define IP6_ND_EVENT 4
2101
2102static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2103static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2104static vlib_node_registration_t ip_resolver_process_node;
2105
2106static void
2107handle_ip4_arp_event (u32 pool_index)
2108{
2109 vpe_api_main_t *vam = &vpe_api_main;
2110 vnet_main_t *vnm = vam->vnet_main;
2111 vlib_main_t *vm = vam->vlib_main;
2112 vl_api_ip4_arp_event_t *event;
2113 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002114 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002115
2116 /* Client can cancel, die, etc. */
2117 if (pool_is_free_index (vam->arp_events, pool_index))
2118 return;
2119
2120 event = pool_elt_at_index (vam->arp_events, pool_index);
2121
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002122 reg = vl_api_client_index_to_registration (event->client_index);
2123 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002124 {
2125 (void) vnet_add_del_ip4_arp_change_event
2126 (vnm, arp_change_delete_callback,
2127 event->pid, &event->address,
2128 ip_resolver_process_node.index, IP4_ARP_EVENT,
2129 ~0 /* pool index, notused */ , 0 /* is_add */ );
2130 return;
2131 }
2132
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002133 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002134 {
2135 mp = vl_msg_api_alloc (sizeof (*mp));
2136 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002137 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002138 }
2139 else
2140 {
2141 static f64 last_time;
2142 /*
2143 * Throttle syslog msgs.
2144 * It's pretty tempting to just revoke the registration...
2145 */
2146 if (vlib_time_now (vm) > last_time + 10.0)
2147 {
2148 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2149 format_ip4_address, &event->address, event->pid);
2150 last_time = vlib_time_now (vm);
2151 }
2152 }
2153}
2154
Neale Rannsf12dad62018-06-04 18:41:24 -07002155static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002156handle_ip6_nd_event (u32 pool_index)
2157{
2158 vpe_api_main_t *vam = &vpe_api_main;
2159 vnet_main_t *vnm = vam->vnet_main;
2160 vlib_main_t *vm = vam->vlib_main;
2161 vl_api_ip6_nd_event_t *event;
2162 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002163 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002164
2165 /* Client can cancel, die, etc. */
2166 if (pool_is_free_index (vam->nd_events, pool_index))
2167 return;
2168
2169 event = pool_elt_at_index (vam->nd_events, pool_index);
2170
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002171 reg = vl_api_client_index_to_registration (event->client_index);
2172 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002173 {
2174 (void) vnet_add_del_ip6_nd_change_event
2175 (vnm, nd_change_delete_callback,
2176 event->pid, &event->address,
2177 ip_resolver_process_node.index, IP6_ND_EVENT,
2178 ~0 /* pool index, notused */ , 0 /* is_add */ );
2179 return;
2180 }
2181
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002182 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002183 {
2184 mp = vl_msg_api_alloc (sizeof (*mp));
2185 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002186 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002187 }
2188 else
2189 {
2190 static f64 last_time;
2191 /*
2192 * Throttle syslog msgs.
2193 * It's pretty tempting to just revoke the registration...
2194 */
2195 if (vlib_time_now (vm) > last_time + 10.0)
2196 {
2197 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2198 format_ip6_address, &event->address, event->pid);
2199 last_time = vlib_time_now (vm);
2200 }
2201 }
2202}
2203
2204static uword
2205resolver_process (vlib_main_t * vm,
2206 vlib_node_runtime_t * rt, vlib_frame_t * f)
2207{
2208 volatile f64 timeout = 100.0;
2209 volatile uword *event_data = 0;
2210
2211 while (1)
2212 {
2213 vlib_process_wait_for_event_or_clock (vm, timeout);
2214
2215 uword event_type =
2216 vlib_process_get_events (vm, (uword **) & event_data);
2217
2218 int i;
2219 switch (event_type)
2220 {
2221 case IP4_ARP_EVENT:
2222 for (i = 0; i < vec_len (event_data); i++)
2223 handle_ip4_arp_event (event_data[i]);
2224 break;
2225
2226 case IP6_ND_EVENT:
2227 for (i = 0; i < vec_len (event_data); i++)
2228 handle_ip6_nd_event (event_data[i]);
2229 break;
2230
2231 case ~0: /* timeout */
2232 break;
2233 }
2234
2235 vec_reset_length (event_data);
2236 }
2237 return 0; /* or not */
2238}
2239
2240/* *INDENT-OFF* */
2241VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2242 .function = resolver_process,
2243 .type = VLIB_NODE_TYPE_PROCESS,
2244 .name = "ip-route-resolver-process",
2245};
2246/* *INDENT-ON* */
2247
2248static int
2249nd_change_data_callback (u32 pool_index, u8 * new_mac,
2250 u32 sw_if_index, ip6_address_t * address)
2251{
2252 vpe_api_main_t *am = &vpe_api_main;
2253 vl_api_ip6_nd_event_t *event;
2254
2255 if (pool_is_free_index (am->nd_events, pool_index))
2256 return 1;
2257
2258 event = pool_elt_at_index (am->nd_events, pool_index);
2259 if (eth_mac_equal (event->new_mac, new_mac) &&
2260 sw_if_index == ntohl (event->sw_if_index))
2261 {
2262 return 1;
2263 }
2264
2265 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2266 event->sw_if_index = htonl (sw_if_index);
2267 return 0;
2268}
2269
2270static int
2271arp_change_delete_callback (u32 pool_index, u8 * notused)
2272{
2273 vpe_api_main_t *am = &vpe_api_main;
2274
2275 if (pool_is_free_index (am->arp_events, pool_index))
2276 return 1;
2277
2278 pool_put_index (am->arp_events, pool_index);
2279 return 0;
2280}
2281
2282static int
2283nd_change_delete_callback (u32 pool_index, u8 * notused)
2284{
2285 vpe_api_main_t *am = &vpe_api_main;
2286
2287 if (pool_is_free_index (am->nd_events, pool_index))
2288 return 1;
2289
2290 pool_put_index (am->nd_events, pool_index);
2291 return 0;
2292}
2293
2294static vlib_node_registration_t wc_arp_process_node;
2295
2296enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002297{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002298
2299static uword
2300wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2301{
2302 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2303 * and need to be volatile - to prevent them from being optimized into
2304 * a register - which could change during suspension */
2305
2306 volatile wc_arp_report_t arp_prev = { 0 };
2307 volatile wc_nd_report_t nd_prev = { 0 };
2308 volatile f64 last_arp = vlib_time_now (vm);
2309 volatile f64 last_nd = vlib_time_now (vm);
2310
2311 while (1)
2312 {
2313 vlib_process_wait_for_event (vm);
2314 uword event_type = WC_ARP_REPORT;
2315 void *event_data = vlib_process_get_event_data (vm, &event_type);
2316
2317 f64 now = vlib_time_now (vm);
2318 int i;
2319 if (event_type == WC_ARP_REPORT)
2320 {
2321 wc_arp_report_t *arp_events = event_data;
2322 for (i = 0; i < vec_len (arp_events); i++)
2323 {
2324 /* discard dup event */
2325 if (arp_prev.ip4 == arp_events[i].ip4 &&
2326 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2327 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2328 (now - last_arp) < 10.0)
2329 {
2330 continue;
2331 }
2332 arp_prev = arp_events[i];
2333 last_arp = now;
2334 vpe_client_registration_t *reg;
2335 /* *INDENT-OFF* */
2336 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2337 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002338 vl_api_registration_t *vl_reg;
2339 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002340 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002341 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002342 {
2343 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2344 memset (event, 0, sizeof *event);
2345 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2346 event->client_index = reg->client_index;
2347 event->pid = reg->client_pid;
2348 event->mac_ip = 1;
2349 event->address = arp_events[i].ip4;
2350 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2351 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002352 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002353 }
2354 }));
2355 /* *INDENT-ON* */
2356 }
2357 }
2358 else if (event_type == WC_ND_REPORT)
2359 {
2360 wc_nd_report_t *nd_events = event_data;
2361 for (i = 0; i < vec_len (nd_events); i++)
2362 {
2363 /* discard dup event */
2364 if (ip6_address_is_equal
2365 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2366 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2367 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2368 && (now - last_nd) < 10.0)
2369 {
2370 continue;
2371 }
2372 nd_prev = nd_events[i];
2373 last_nd = now;
2374 vpe_client_registration_t *reg;
2375 /* *INDENT-OFF* */
2376 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2377 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002378 vl_api_registration_t *vl_reg;
2379 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2380 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002381 {
2382 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2383 memset (event, 0, sizeof *event);
2384 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2385 event->client_index = reg->client_index;
2386 event->pid = reg->client_pid;
2387 event->mac_ip = 1;
2388 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2389 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2390 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002391 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002392 }
2393 }));
2394 /* *INDENT-ON* */
2395 }
2396 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002397 else if (event_type == RA_REPORT)
2398 {
2399 ra_report_t *ra_events = event_data;
2400 for (i = 0; i < vec_len (ra_events); i++)
2401 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002402 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2403 call_ip6_neighbor_callbacks (&ra_events[i],
2404 npm->ra_report_functions);
2405
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002406 vpe_client_registration_t *reg;
2407 /* *INDENT-OFF* */
2408 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2409 ({
2410 vl_api_registration_t *vl_reg;
2411 vl_reg =
2412 vl_api_client_index_to_registration (reg->client_index);
2413 if (vl_reg && vl_api_can_send_msg (vl_reg))
2414 {
2415 u32 event_size =
2416 sizeof (vl_api_ip6_ra_event_t) +
2417 vec_len (ra_events[i].prefixes) *
2418 sizeof (vl_api_ip6_ra_prefix_info_t);
2419 vl_api_ip6_ra_event_t *event =
2420 vl_msg_api_alloc (event_size);
2421 memset (event, 0, event_size);
2422 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2423 event->client_index = reg->client_index;
2424 event->pid = reg->client_pid;
2425
2426 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2427
2428 memcpy (event->router_address, ra_events[i].router_address, 16);
2429
2430 event->current_hop_limit = ra_events[i].current_hop_limit;
2431 event->flags = ra_events[i].flags;
2432 event->router_lifetime_in_sec =
2433 clib_host_to_net_u16 (ra_events
2434 [i].router_lifetime_in_sec);
2435 event->neighbor_reachable_time_in_msec =
2436 clib_host_to_net_u32 (ra_events
2437 [i].neighbor_reachable_time_in_msec);
2438 event->time_in_msec_between_retransmitted_neighbor_solicitations
2439 =
2440 clib_host_to_net_u32 (ra_events
2441 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2442
2443 event->n_prefixes =
2444 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2445 vl_api_ip6_ra_prefix_info_t *prefix =
2446 (typeof (prefix)) event->prefixes;
2447 u32 j;
2448 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2449 {
2450 ra_report_prefix_info_t *info =
2451 &ra_events[i].prefixes[j];
2452 memcpy (prefix->dst_address, info->dst_address.as_u8,
2453 16);
2454 prefix->dst_address_length = info->dst_address_length;
2455 prefix->flags = info->flags;
2456 prefix->valid_time =
2457 clib_host_to_net_u32 (info->valid_time);
2458 prefix->preferred_time =
2459 clib_host_to_net_u32 (info->preferred_time);
2460 prefix++;
2461 }
2462
2463 vl_api_send_msg (vl_reg, (u8 *) event);
2464 }
2465 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002466 /* *INDENT-ON* */
2467 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002468 }
2469 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002470 vlib_process_put_event_data (vm, event_data);
2471 }
2472
2473 return 0;
2474}
2475
2476/* *INDENT-OFF* */
2477VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2478 .function = wc_arp_process,
2479 .type = VLIB_NODE_TYPE_PROCESS,
2480 .name = "wildcard-ip4-arp-publisher-process",
2481};
2482/* *INDENT-ON* */
2483
2484static int
2485arp_change_data_callback (u32 pool_index, u8 * new_mac,
2486 u32 sw_if_index, u32 address)
2487{
2488 vpe_api_main_t *am = &vpe_api_main;
2489 vl_api_ip4_arp_event_t *event;
2490
2491 if (pool_is_free_index (am->arp_events, pool_index))
2492 return 1;
2493
2494 event = pool_elt_at_index (am->arp_events, pool_index);
2495 if (eth_mac_equal (event->new_mac, new_mac) &&
2496 sw_if_index == ntohl (event->sw_if_index))
2497 {
2498 return 1;
2499 }
2500
2501 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2502 event->sw_if_index = htonl (sw_if_index);
2503 return 0;
2504}
2505
2506static void
2507vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2508{
2509 vpe_api_main_t *am = &vpe_api_main;
2510 vnet_main_t *vnm = vnet_get_main ();
2511 vl_api_want_ip4_arp_events_reply_t *rmp;
2512 int rv = 0;
2513
2514 if (mp->address == 0)
2515 {
2516 uword *p =
2517 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2518 vpe_client_registration_t *rp;
2519 if (p)
2520 {
2521 if (mp->enable_disable)
2522 {
2523 clib_warning ("pid %d: already enabled...", mp->pid);
2524 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2525 goto reply;
2526 }
2527 else
2528 {
2529 rp =
2530 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2531 pool_put (am->wc_ip4_arp_events_registrations, rp);
2532 hash_unset (am->wc_ip4_arp_events_registration_hash,
2533 mp->client_index);
2534 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002535 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002536 goto reply;
2537 }
2538 }
2539 if (mp->enable_disable == 0)
2540 {
2541 clib_warning ("pid %d: already disabled...", mp->pid);
2542 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2543 goto reply;
2544 }
2545 pool_get (am->wc_ip4_arp_events_registrations, rp);
2546 rp->client_index = mp->client_index;
2547 rp->client_pid = mp->pid;
2548 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2549 rp - am->wc_ip4_arp_events_registrations);
2550 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2551 goto reply;
2552 }
2553
2554 if (mp->enable_disable)
2555 {
2556 vl_api_ip4_arp_event_t *event;
2557 pool_get (am->arp_events, event);
2558 rv = vnet_add_del_ip4_arp_change_event
2559 (vnm, arp_change_data_callback,
2560 mp->pid, &mp->address /* addr, in net byte order */ ,
2561 ip_resolver_process_node.index,
2562 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2563
2564 if (rv)
2565 {
2566 pool_put (am->arp_events, event);
2567 goto reply;
2568 }
2569 memset (event, 0, sizeof (*event));
2570
2571 /* Python API expects events to have no context */
2572 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2573 event->client_index = mp->client_index;
2574 event->address = mp->address;
2575 event->pid = mp->pid;
2576 if (mp->address == 0)
2577 event->mac_ip = 1;
2578 }
2579 else
2580 {
2581 rv = vnet_add_del_ip4_arp_change_event
2582 (vnm, arp_change_delete_callback,
2583 mp->pid, &mp->address /* addr, in net byte order */ ,
2584 ip_resolver_process_node.index,
2585 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2586 }
2587reply:
2588 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2589}
2590
Neale Rannsf12dad62018-06-04 18:41:24 -07002591static clib_error_t *
2592want_ip4_arp_events_reaper (u32 client_index)
2593{
2594 vpe_client_registration_t *rp;
2595 vl_api_ip4_arp_event_t *event;
2596 u32 *to_delete, *event_id;
2597 vpe_api_main_t *am;
2598 vnet_main_t *vnm;
2599 uword *p;
2600
2601 am = &vpe_api_main;
2602 vnm = vnet_get_main ();
2603 to_delete = NULL;
2604
2605 /* clear out all of its pending resolutions */
2606 /* *INDENT-OFF* */
2607 pool_foreach(event, am->arp_events,
2608 ({
2609 if (event->client_index == client_index)
2610 {
2611 vec_add1(to_delete, event - am->arp_events);
2612 }
2613 }));
2614 /* *INDENT-ON* */
2615
2616 vec_foreach (event_id, to_delete)
2617 {
2618 event = pool_elt_at_index (am->arp_events, *event_id);
2619 vnet_add_del_ip4_arp_change_event
2620 (vnm, arp_change_delete_callback,
2621 event->pid, &event->address,
2622 ip_resolver_process_node.index, IP4_ARP_EVENT,
2623 ~0 /* pool index, notused */ , 0 /* is_add */ );
2624 }
2625 vec_free (to_delete);
2626
2627 /* remove from the registration hash */
2628 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2629
2630 if (p)
2631 {
2632 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2633 pool_put (am->wc_ip4_arp_events_registrations, rp);
2634 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2635 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2636 wc_arp_set_publisher_node (~0, REPORT_MAX);
2637 }
2638 return (NULL);
2639}
2640
2641VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2642
Neale Rannsb8d44812017-11-10 06:53:54 -08002643static void
2644vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2645{
2646 vpe_api_main_t *am = &vpe_api_main;
2647 vnet_main_t *vnm = vnet_get_main ();
2648 vl_api_want_ip6_nd_events_reply_t *rmp;
2649 int rv = 0;
2650
2651 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2652 {
2653 uword *p =
2654 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2655 vpe_client_registration_t *rp;
2656 if (p)
2657 {
2658 if (mp->enable_disable)
2659 {
2660 clib_warning ("pid %d: already enabled...", mp->pid);
2661 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2662 goto reply;
2663 }
2664 else
2665 {
2666 rp =
2667 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2668 pool_put (am->wc_ip6_nd_events_registrations, rp);
2669 hash_unset (am->wc_ip6_nd_events_registration_hash,
2670 mp->client_index);
2671 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002672 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002673 goto reply;
2674 }
2675 }
2676 if (mp->enable_disable == 0)
2677 {
2678 clib_warning ("pid %d: already disabled...", mp->pid);
2679 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2680 goto reply;
2681 }
2682 pool_get (am->wc_ip6_nd_events_registrations, rp);
2683 rp->client_index = mp->client_index;
2684 rp->client_pid = mp->pid;
2685 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2686 rp - am->wc_ip6_nd_events_registrations);
2687 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2688 goto reply;
2689 }
2690
2691 if (mp->enable_disable)
2692 {
2693 vl_api_ip6_nd_event_t *event;
2694 pool_get (am->nd_events, event);
2695
2696 rv = vnet_add_del_ip6_nd_change_event
2697 (vnm, nd_change_data_callback,
2698 mp->pid, mp->address /* addr, in net byte order */ ,
2699 ip_resolver_process_node.index,
2700 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2701
2702 if (rv)
2703 {
2704 pool_put (am->nd_events, event);
2705 goto reply;
2706 }
2707 memset (event, 0, sizeof (*event));
2708
2709 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2710 event->client_index = mp->client_index;
2711 clib_memcpy (event->address, mp->address, sizeof event->address);
2712 event->pid = mp->pid;
2713 }
2714 else
2715 {
2716 rv = vnet_add_del_ip6_nd_change_event
2717 (vnm, nd_change_delete_callback,
2718 mp->pid, mp->address /* addr, in net byte order */ ,
2719 ip_resolver_process_node.index,
2720 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2721 }
2722reply:
2723 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2724}
2725
Neale Rannsf12dad62018-06-04 18:41:24 -07002726static clib_error_t *
2727want_ip6_nd_events_reaper (u32 client_index)
2728{
2729
2730 vpe_client_registration_t *rp;
2731 vl_api_ip6_nd_event_t *event;
2732 u32 *to_delete, *event_id;
2733 vpe_api_main_t *am;
2734 vnet_main_t *vnm;
2735 uword *p;
2736
2737 am = &vpe_api_main;
2738 vnm = vnet_get_main ();
2739 to_delete = NULL;
2740
2741 /* clear out all of its pending resolutions */
2742 /* *INDENT-OFF* */
2743 pool_foreach(event, am->nd_events,
2744 ({
2745 if (event->client_index == client_index)
2746 {
2747 vec_add1(to_delete, event - am->nd_events);
2748 }
2749 }));
2750 /* *INDENT-ON* */
2751
2752 vec_foreach (event_id, to_delete)
2753 {
2754 event = pool_elt_at_index (am->nd_events, *event_id);
2755 vnet_add_del_ip6_nd_change_event
2756 (vnm, nd_change_delete_callback,
2757 event->pid, &event->address,
2758 ip_resolver_process_node.index, IP6_ND_EVENT,
2759 ~0 /* pool index, notused */ , 0 /* is_add */ );
2760 }
2761 vec_free (to_delete);
2762
2763 /* remove from the registration hash */
2764 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2765
2766 if (p)
2767 {
2768 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2769 pool_put (am->wc_ip6_nd_events_registrations, rp);
2770 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2771 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2772 wc_nd_set_publisher_node (~0, REPORT_MAX);
2773 }
2774 return (NULL);
2775}
2776
2777VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2778
Neale Rannsb8d44812017-11-10 06:53:54 -08002779static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002780vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2781{
2782 vpe_api_main_t *am = &vpe_api_main;
2783 vl_api_want_ip6_ra_events_reply_t *rmp;
2784 int rv = 0;
2785
2786 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2787 vpe_client_registration_t *rp;
2788 if (p)
2789 {
2790 if (mp->enable_disable)
2791 {
2792 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2793 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2794 goto reply;
2795 }
2796 else
2797 {
2798 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2799 pool_put (am->ip6_ra_events_registrations, rp);
2800 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002801 goto reply;
2802 }
2803 }
2804 if (mp->enable_disable == 0)
2805 {
2806 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2807 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2808 goto reply;
2809 }
2810 pool_get (am->ip6_ra_events_registrations, rp);
2811 rp->client_index = mp->client_index;
2812 rp->client_pid = ntohl (mp->pid);
2813 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2814 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002815
2816reply:
2817 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2818}
2819
Neale Rannsf12dad62018-06-04 18:41:24 -07002820static clib_error_t *
2821want_ip6_ra_events_reaper (u32 client_index)
2822{
2823 vpe_api_main_t *am = &vpe_api_main;
2824 vpe_client_registration_t *rp;
2825 uword *p;
2826
2827 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2828
2829 if (p)
2830 {
2831 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2832 pool_put (am->ip6_ra_events_registrations, rp);
2833 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2834 }
2835 return (NULL);
2836}
2837
2838VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2839
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002840static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002841vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2842{
2843 vl_api_proxy_arp_add_del_reply_t *rmp;
2844 u32 fib_index;
2845 int rv;
2846 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002847 uword *p;
2848
Ole Troan3288ed72017-12-06 17:00:05 +01002849 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002850
Neale Ranns0053de62018-05-22 08:40:52 -07002851 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002852
2853 if (!p)
2854 {
2855 rv = VNET_API_ERROR_NO_SUCH_FIB;
2856 goto out;
2857 }
2858
2859 fib_index = p[0];
2860
Neale Ranns0053de62018-05-22 08:40:52 -07002861 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2862 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002863 fib_index, mp->is_add == 0);
2864
2865out:
Ole Troan3288ed72017-12-06 17:00:05 +01002866 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002867 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2868}
2869
Neale Ranns0053de62018-05-22 08:40:52 -07002870typedef struct proxy_arp_walk_ctx_t_
2871{
2872 vl_api_registration_t *reg;
2873 u32 context;
2874} proxy_arp_walk_ctx_t;
2875
2876static walk_rc_t
2877send_proxy_arp_details (const ip4_address_t * lo_addr,
2878 const ip4_address_t * hi_addr,
2879 u32 fib_index, void *data)
2880{
2881 vl_api_proxy_arp_details_t *mp;
2882 proxy_arp_walk_ctx_t *ctx;
2883
2884 ctx = data;
2885
2886 mp = vl_msg_api_alloc (sizeof (*mp));
2887 memset (mp, 0, sizeof (*mp));
2888 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2889 mp->context = ctx->context;
2890 mp->proxy.vrf_id = htonl (fib_index);
2891 clib_memcpy (mp->proxy.low_address, lo_addr,
2892 sizeof (mp->proxy.low_address));
2893 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2894
2895 vl_api_send_msg (ctx->reg, (u8 *) mp);
2896
2897 return (WALK_CONTINUE);
2898}
2899
2900static void
2901vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2902{
2903 vl_api_registration_t *reg;
2904
2905 reg = vl_api_client_index_to_registration (mp->client_index);
2906 if (!reg)
2907 return;
2908
2909 proxy_arp_walk_ctx_t wctx = {
2910 .reg = reg,
2911 .context = mp->context,
2912 };
2913
2914 proxy_arp_walk (send_proxy_arp_details, &wctx);
2915}
2916
2917static walk_rc_t
2918send_proxy_arp_intfc_details (vnet_main_t * vnm,
2919 vnet_sw_interface_t * si, void *data)
2920{
2921 vl_api_proxy_arp_intfc_details_t *mp;
2922 proxy_arp_walk_ctx_t *ctx;
2923
2924 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
2925 return (WALK_CONTINUE);
2926
2927 ctx = data;
2928
2929 mp = vl_msg_api_alloc (sizeof (*mp));
2930 memset (mp, 0, sizeof (*mp));
2931 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
2932 mp->context = ctx->context;
2933 mp->sw_if_index = htonl (si->sw_if_index);
2934
2935 vl_api_send_msg (ctx->reg, (u8 *) mp);
2936
2937 return (WALK_CONTINUE);
2938}
2939
2940static void
2941vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
2942{
2943 vl_api_registration_t *reg;
2944
2945 reg = vl_api_client_index_to_registration (mp->client_index);
2946 if (!reg)
2947 return;
2948
2949 proxy_arp_walk_ctx_t wctx = {
2950 .reg = reg,
2951 .context = mp->context,
2952 };
2953
2954 vnet_sw_interface_walk (vnet_get_main (),
2955 send_proxy_arp_intfc_details, &wctx);
2956}
2957
Neale Rannsb8d44812017-11-10 06:53:54 -08002958static void
2959 vl_api_proxy_arp_intfc_enable_disable_t_handler
2960 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2961{
2962 int rv = 0;
2963 vnet_main_t *vnm = vnet_get_main ();
2964 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2965
2966 VALIDATE_SW_IF_INDEX (mp);
2967
2968 vnet_sw_interface_t *si =
2969 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2970
2971 ASSERT (si);
2972
2973 if (mp->enable_disable)
2974 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2975 else
2976 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2977
2978 BAD_SW_IF_INDEX_LABEL;
2979
2980 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2981}
2982
John Loc7b43042018-04-13 16:46:22 -04002983static void
2984vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
2985{
2986 int rv = 0;
2987 vlib_main_t *vm = vlib_get_main ();
2988 vl_api_ip_probe_neighbor_reply_t *rmp;
2989 clib_error_t *error;
2990
2991 VALIDATE_SW_IF_INDEX (mp);
2992
2993 u32 sw_if_index = ntohl (mp->sw_if_index);
2994
2995 if (mp->is_ipv6)
2996 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04002997 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04002998 else
2999 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003000 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003001
3002 if (error)
3003 {
3004 clib_error_report (error);
3005 rv = clib_error_get_code (error);
3006 }
3007
3008 BAD_SW_IF_INDEX_LABEL;
3009
3010 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3011}
3012
John Lo7f358b32018-04-28 01:19:24 -04003013static void
3014 vl_api_ip_scan_neighbor_enable_disable_t_handler
3015 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3016{
3017 int rv = 0;
3018 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3019 ip_neighbor_scan_arg_t arg;
3020
3021 arg.mode = mp->mode;
3022 arg.scan_interval = mp->scan_interval;
3023 arg.max_proc_time = mp->max_proc_time;
3024 arg.max_update = mp->max_update;
3025 arg.scan_int_delay = mp->scan_int_delay;
3026 arg.stale_threshold = mp->stale_threshold;
3027 ip_neighbor_scan_enable_disable (&arg);
3028
3029 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3030}
3031
Neale Rannsb8d44812017-11-10 06:53:54 -08003032static int
3033ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3034{
3035 vnet_main_t *vnm = vnet_get_main ();
3036 vnet_interface_main_t *im = &vnm->interface_main;
3037 ip4_main_t *im4 = &ip4_main;
3038 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003039 fib_table_t *fib_table;
3040 ip4_fib_t *fib;
3041 u32 sw_if_index;
3042 int i;
3043 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3044 u32 target_fib_id = ntohl (mp->vrf_id);
3045
Ole Troan3288ed72017-12-06 17:00:05 +01003046 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003047
3048 /* *INDENT-OFF* */
3049 pool_foreach (fib_table, im4->fibs,
3050 ({
3051 vnet_sw_interface_t * si;
3052
3053 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3054
3055 if (fib->table_id != target_fib_id)
3056 continue;
3057
3058 /* remove any mpls encap/decap labels */
3059 mpls_fib_reset_labels (fib->table_id);
3060
3061 /* remove any proxy arps in this fib */
3062 vnet_proxy_arp_fib_reset (fib->table_id);
3063
3064 /* Set the flow hash for this fib to the default */
3065 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3066
3067 vec_reset_length (sw_if_indices_to_shut);
3068
3069 /* Shut down interfaces in this FIB / clean out intfc routes */
3070 pool_foreach (si, im->sw_interfaces,
3071 ({
3072 u32 sw_if_index = si->sw_if_index;
3073
3074 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3075 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3076 fib->index))
3077 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3078 }));
3079
3080 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3081 sw_if_index = sw_if_indices_to_shut[i];
3082
3083 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3084 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3085 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3086 }
3087
3088 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3089
3090 rv = 0;
3091 break;
3092 })); /* pool_foreach (fib) */
3093 /* *INDENT-ON* */
3094
Ole Troan3288ed72017-12-06 17:00:05 +01003095 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003096 return rv;
3097}
3098
3099static int
3100ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3101{
3102 vnet_main_t *vnm = vnet_get_main ();
3103 vnet_interface_main_t *im = &vnm->interface_main;
3104 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003105 static u32 *sw_if_indices_to_shut;
3106 fib_table_t *fib_table;
3107 ip6_fib_t *fib;
3108 u32 sw_if_index;
3109 int i;
3110 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3111 u32 target_fib_id = ntohl (mp->vrf_id);
3112
Ole Troan3288ed72017-12-06 17:00:05 +01003113 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003114
3115 /* *INDENT-OFF* */
3116 pool_foreach (fib_table, im6->fibs,
3117 ({
3118 vnet_sw_interface_t * si;
3119
3120 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3121
3122 if (fib->table_id != target_fib_id)
3123 continue;
3124
3125 vec_reset_length (sw_if_indices_to_shut);
3126
3127 /* Set the flow hash for this fib to the default */
3128 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3129
3130 /* Shut down interfaces in this FIB / clean out intfc routes */
3131 pool_foreach (si, im->sw_interfaces,
3132 ({
3133 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3134 fib->index)
3135 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3136 }));
3137
3138 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3139 sw_if_index = sw_if_indices_to_shut[i];
3140
3141 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3142 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3143 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3144 }
3145
3146 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3147
3148 rv = 0;
3149 break;
3150 })); /* pool_foreach (fib) */
3151 /* *INDENT-ON* */
3152
Ole Troan3288ed72017-12-06 17:00:05 +01003153 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003154 return rv;
3155}
3156
3157static void
3158vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3159{
3160 int rv;
3161 vl_api_reset_fib_reply_t *rmp;
3162
3163 if (mp->is_ipv6)
3164 rv = ip6_reset_fib_t_handler (mp);
3165 else
3166 rv = ip4_reset_fib_t_handler (mp);
3167
3168 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3169}
3170
3171static void
3172vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3173{
3174 int rv;
3175 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3176 vnet_main_t *vnm = vnet_get_main ();
3177 clib_error_t *error;
3178
3179 vnm->api_errno = 0;
3180
3181 if (mp->is_ipv6)
3182 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3183 else
3184 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3185
3186 if (error)
3187 {
3188 clib_error_report (error);
3189 rv = VNET_API_ERROR_UNSPECIFIED;
3190 }
3191 else
3192 {
3193 rv = vnm->api_errno;
3194 }
3195
3196 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3197}
3198
Klement Sekera75e7d132017-09-20 08:26:30 +02003199void
3200vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3201{
3202 vl_api_ip_reassembly_set_reply_t *rmp;
3203 int rv = 0;
3204 if (mp->is_ip6)
3205 {
3206 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3207 clib_net_to_host_u32 (mp->max_reassemblies),
3208 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3209 }
3210 else
3211 {
3212 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3213 clib_net_to_host_u32 (mp->max_reassemblies),
3214 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3215 }
3216
3217 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3218}
3219
3220void
3221vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3222{
3223 unix_shared_memory_queue_t *q;
3224
3225 q = vl_api_client_index_to_input_queue (mp->client_index);
3226
3227 if (q == 0)
3228 return;
3229
3230 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
3231 memset (rmp, 0, sizeof (*rmp));
3232 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3233 rmp->context = mp->context;
3234 rmp->retval = 0;
3235 if (mp->is_ip6)
3236 {
3237 rmp->is_ip6 = 1;
3238 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3239 &rmp->expire_walk_interval_ms);
3240 }
3241 else
3242 {
3243 rmp->is_ip6 = 0;
3244 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3245 &rmp->expire_walk_interval_ms);
3246 }
3247 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3248 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3249 rmp->expire_walk_interval_ms =
3250 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3251 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3252}
3253
Klement Sekera4c533132018-02-22 11:41:12 +01003254void
3255 vl_api_ip_reassembly_enable_disable_t_handler
3256 (vl_api_ip_reassembly_enable_disable_t * mp)
3257{
3258 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3259 int rv = 0;
3260 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3261 mp->enable_ip4);
3262 if (0 == rv)
3263 {
3264 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3265 mp->enable_ip6);
3266 }
3267
3268 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3269}
3270
Dave Barachb5e8a772016-12-06 12:04:42 -05003271#define vl_msg_name_crc_list
3272#include <vnet/ip/ip.api.h>
3273#undef vl_msg_name_crc_list
3274
3275static void
3276setup_message_id_table (api_main_t * am)
3277{
3278#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3279 foreach_vl_msg_name_crc_ip;
3280#undef _
3281}
3282
3283static clib_error_t *
3284ip_api_hookup (vlib_main_t * vm)
3285{
3286 api_main_t *am = &api_main;
3287
3288#define _(N,n) \
3289 vl_msg_api_set_handlers(VL_API_##N, #n, \
3290 vl_api_##n##_t_handler, \
3291 vl_noop_handler, \
3292 vl_api_##n##_t_endian, \
3293 vl_api_##n##_t_print, \
3294 sizeof(vl_api_##n##_t), 1);
3295 foreach_ip_api_msg;
3296#undef _
3297
3298 /*
3299 * Set up the (msg_name, crc, message-id) table
3300 */
3301 setup_message_id_table (am);
3302
Juraj Sloboda52574522018-05-03 10:03:50 +02003303 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3304
Dave Barachb5e8a772016-12-06 12:04:42 -05003305 return 0;
3306}
3307
3308VLIB_API_INIT_FUNCTION (ip_api_hookup);
3309
3310/*
3311 * fd.io coding-style-patch-verification: ON
3312 *
3313 * Local Variables:
3314 * eval: (c-set-style "gnu")
3315 * End:
3316 */