blob: 31b7e40a880dd1e78d1a6d64504660c4c5b35dd7 [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
Dave Barachb5e8a772016-12-06 12:04:42 -0500842 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
843
844 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
845 {
846 /*
847 * special route types that link directly to the adj
848 */
849 if (is_add)
850 {
851 dpo_id_t dpo = DPO_INVALID;
852 dpo_proto_t dproto;
853
854 dproto = fib_proto_to_dpo (prefix->fp_proto);
855
856 if (is_drop)
857 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
858 else if (is_local)
859 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
860 else if (is_unreach)
861 ip_null_dpo_add_and_lock (dproto,
862 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
863 else if (is_prohibit)
864 ip_null_dpo_add_and_lock (dproto,
865 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
866 &dpo);
867 else if (is_classify)
868 {
869 if (pool_is_free_index (cm->tables,
870 ntohl (classify_table_index)))
871 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500872 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500873 return VNET_API_ERROR_NO_SUCH_TABLE;
874 }
875
876 dpo_set (&dpo, DPO_CLASSIFY, dproto,
877 classify_dpo_create (dproto,
878 ntohl (classify_table_index)));
879 }
880 else
881 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500882 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500883 return VNET_API_ERROR_NO_SUCH_TABLE;
884 }
885
886 fib_table_entry_special_dpo_update (fib_index,
887 prefix,
888 FIB_SOURCE_API,
889 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
890 dpo_reset (&dpo);
891 }
892 else
893 {
894 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
895 }
896 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700897 else if (is_multipath)
898 {
899 vec_add1 (paths, path);
900
901 if (is_add)
902 fib_table_entry_path_add2 (fib_index,
903 prefix,
904 FIB_SOURCE_API, entry_flags, paths);
905 else
906 fib_table_entry_path_remove2 (fib_index,
907 prefix, FIB_SOURCE_API, paths);
908
909 vec_free (paths);
910 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500911 else
912 {
913 if (is_add)
914 {
915 vec_add1 (paths, path);
916 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800917 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500918 vec_free (paths);
919 }
920 else
921 {
922 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
923 }
924 }
925
Dave Barachd7cb1b52016-12-09 09:52:16 -0500926 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500927 return (0);
928}
929
930int
931add_del_route_check (fib_protocol_t table_proto,
932 u32 table_id,
933 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700934 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500935 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800936 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500937{
938 vnet_main_t *vnm = vnet_get_main ();
939
940 *fib_index = fib_table_find (table_proto, ntohl (table_id));
941 if (~0 == *fib_index)
942 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800943 /* No such VRF, and we weren't asked to create one */
944 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500945 }
946
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800947 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500948 {
949 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
950 ntohl (next_hop_sw_if_index)))
951 {
952 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
953 }
954 }
955 else
956 {
Neale Rannsda78f952017-05-24 09:15:43 -0700957 fib_protocol_t fib_nh_proto;
958
959 if (next_hop_table_proto > DPO_PROTO_MPLS)
960 return (0);
961
962 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
963
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800964 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700965 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800966 ntohl (next_hop_table_id));
967 else
Neale Rannsda78f952017-05-24 09:15:43 -0700968 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800969 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500970
971 if (~0 == *next_hop_fib_index)
972 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800973 /* No such VRF, and we weren't asked to create one */
974 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500975 }
976 }
977
978 return (0);
979}
980
981static int
982ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
983{
984 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800985 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500986 int rv, ii, n_labels;;
987
988 rv = add_del_route_check (FIB_PROTOCOL_IP4,
989 mp->table_id,
990 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700991 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500992 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700993 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500994
995 if (0 != rv)
996 return (rv);
997
998 fib_prefix_t pfx = {
999 .fp_len = mp->dst_address_length,
1000 .fp_proto = FIB_PROTOCOL_IP4,
1001 };
1002 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1003
1004 ip46_address_t nh;
1005 memset (&nh, 0, sizeof (nh));
1006 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1007
1008 n_labels = mp->next_hop_n_out_labels;
1009 if (n_labels == 0)
1010 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001011 else
1012 {
1013 vec_validate (label_stack, n_labels - 1);
1014 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001015 {
1016 label_stack[ii].fml_value =
1017 ntohl (mp->next_hop_out_label_stack[ii].label);
1018 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1019 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1020 label_stack[ii].fml_mode =
1021 (mp->next_hop_out_label_stack[ii].is_uniform ?
1022 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1023 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001024 }
1025
1026 return (add_del_route_t_handler (mp->is_multipath,
1027 mp->is_add,
1028 mp->is_drop,
1029 mp->is_unreach,
1030 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001031 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001032 mp->is_classify,
1033 mp->classify_table_index,
1034 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001035 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001036 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001037 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001038 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001039 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001040 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001041 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001042 ntohl (mp->next_hop_sw_if_index),
1043 next_hop_fib_index,
1044 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001045 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001046 ntohl (mp->next_hop_via_label),
1047 label_stack));
1048}
1049
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001050static int
Dave Barachb5e8a772016-12-06 12:04:42 -05001051ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1052{
Neale Ranns31ed7442018-02-23 05:29:09 -08001053 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001054 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001055 int rv, ii, n_labels;;
1056
1057 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1058 mp->table_id,
1059 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001060 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001061 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001062 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001063
1064 if (0 != rv)
1065 return (rv);
1066
1067 fib_prefix_t pfx = {
1068 .fp_len = mp->dst_address_length,
1069 .fp_proto = FIB_PROTOCOL_IP6,
1070 };
1071 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1072
1073 ip46_address_t nh;
1074 memset (&nh, 0, sizeof (nh));
1075 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1076
1077 n_labels = mp->next_hop_n_out_labels;
1078 if (n_labels == 0)
1079 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001080 else
1081 {
1082 vec_validate (label_stack, n_labels - 1);
1083 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001084 {
1085 label_stack[ii].fml_value =
1086 ntohl (mp->next_hop_out_label_stack[ii].label);
1087 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1088 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1089 label_stack[ii].fml_mode =
1090 (mp->next_hop_out_label_stack[ii].is_uniform ?
1091 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1092 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001093 }
1094
1095 return (add_del_route_t_handler (mp->is_multipath,
1096 mp->is_add,
1097 mp->is_drop,
1098 mp->is_unreach,
1099 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001100 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001101 mp->is_classify,
1102 mp->classify_table_index,
1103 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001104 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001105 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001106 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001107 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001108 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001109 &nh, ntohl (mp->next_hop_id),
1110 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001111 next_hop_fib_index,
1112 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001113 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001114 ntohl (mp->next_hop_via_label),
1115 label_stack));
1116}
1117
1118void
1119vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1120{
1121 vl_api_ip_add_del_route_reply_t *rmp;
1122 int rv;
1123 vnet_main_t *vnm = vnet_get_main ();
1124
1125 vnm->api_errno = 0;
1126
1127 if (mp->is_ipv6)
1128 rv = ip6_add_del_route_t_handler (mp);
1129 else
1130 rv = ip4_add_del_route_t_handler (mp);
1131
1132 rv = (rv == 0) ? vnm->api_errno : rv;
1133
1134 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1135}
1136
Neale Ranns15002542017-09-10 04:39:11 -07001137void
Neale Ranns2297af02017-09-12 09:45:04 -07001138ip_table_create (fib_protocol_t fproto,
1139 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001140{
1141 u32 fib_index, mfib_index;
1142
1143 /*
1144 * ignore action on the default table - this is always present
1145 * and cannot be added nor deleted from the API
1146 */
1147 if (0 != table_id)
1148 {
1149 /*
1150 * The API holds only one lock on the table.
1151 * i.e. it can be added many times via the API but needs to be
1152 * deleted only once.
1153 * The FIB index for unicast and multicast is not necessarily the
1154 * same, since internal VPP systesm (like LISP and SR) create
1155 * their own unicast tables.
1156 */
1157 fib_index = fib_table_find (fproto, table_id);
1158 mfib_index = mfib_table_find (fproto, table_id);
1159
1160 if (~0 == fib_index)
1161 {
Neale Ranns2297af02017-09-12 09:45:04 -07001162 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1163 (is_api ?
1164 FIB_SOURCE_API :
1165 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001166 }
1167 if (~0 == mfib_index)
1168 {
Neale Ranns2297af02017-09-12 09:45:04 -07001169 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1170 (is_api ?
1171 MFIB_SOURCE_API :
1172 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001173 }
1174 }
1175}
1176
Neale Ranns32e1c012016-11-22 17:07:28 +00001177static int
1178add_del_mroute_check (fib_protocol_t table_proto,
1179 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001180 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001181{
1182 vnet_main_t *vnm = vnet_get_main ();
1183
1184 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1185 if (~0 == *fib_index)
1186 {
Neale Ranns15002542017-09-10 04:39:11 -07001187 /* No such table */
1188 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001189 }
1190
1191 if (~0 != ntohl (next_hop_sw_if_index))
1192 {
1193 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1194 ntohl (next_hop_sw_if_index)))
1195 {
1196 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1197 }
1198 }
1199
1200 return (0);
1201}
1202
1203static int
1204mroute_add_del_handler (u8 is_add,
1205 u8 is_local,
1206 u32 fib_index,
1207 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001208 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001209 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001210 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001211 u32 next_hop_sw_if_index,
1212 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001213{
1214 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1215
1216 fib_route_path_t path = {
1217 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001218 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001219 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001220 };
1221
1222 if (is_local)
1223 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1224
Neale Rannsd792d9c2017-10-21 10:53:20 -07001225 if (DPO_PROTO_BIER == nh_proto)
1226 {
1227 path.frp_bier_imp = bier_imp;
1228 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1229 }
1230 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001231 {
1232 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001233 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001234 goto done;
1235 }
1236
1237 if (is_add)
1238 {
1239 mfib_table_entry_path_update (fib_index, prefix,
1240 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001241 }
1242 else
1243 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001244 mfib_table_entry_path_remove (fib_index, prefix,
1245 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001246 }
1247
Neale Rannsd792d9c2017-10-21 10:53:20 -07001248done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001249 stats_dsunlock ();
1250 return (0);
1251}
1252
1253static int
1254api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1255{
1256 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001257 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001258 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001259 u32 fib_index;
1260 int rv;
1261
Neale Rannsd792d9c2017-10-21 10:53:20 -07001262 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001263 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1264 rv = add_del_mroute_check (fproto,
1265 mp->table_id,
1266 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001267 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001268
1269 if (0 != rv)
1270 return (rv);
1271
1272 mfib_prefix_t pfx = {
1273 .fp_len = ntohs (mp->grp_address_length),
1274 .fp_proto = fproto,
1275 };
1276
1277 if (FIB_PROTOCOL_IP4 == fproto)
1278 {
1279 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1280 sizeof (pfx.fp_grp_addr.ip4));
1281 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1282 sizeof (pfx.fp_src_addr.ip4));
Neale Rannse821ab12017-06-01 07:45:05 -07001283 memset (&nh.ip6, 0, sizeof (nh.ip6));
1284 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns32e1c012016-11-22 17:07:28 +00001285 }
1286 else
1287 {
1288 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1289 sizeof (pfx.fp_grp_addr.ip6));
1290 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1291 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001292 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns32e1c012016-11-22 17:07:28 +00001293 }
1294
1295 return (mroute_add_del_handler (mp->is_add,
1296 mp->is_local,
1297 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001298 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001299 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001300 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001301 ntohl (mp->next_hop_sw_if_index),
Neale Rannse821ab12017-06-01 07:45:05 -07001302 &nh,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001303 ntohl (mp->itf_flags),
1304 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001305}
1306
1307void
1308vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1309{
1310 vl_api_ip_mroute_add_del_reply_t *rmp;
1311 int rv;
1312 vnet_main_t *vnm = vnet_get_main ();
1313
1314 vnm->api_errno = 0;
1315
1316 rv = api_mroute_add_del_t_handler (mp);
1317
1318 rv = (rv == 0) ? vnm->api_errno : rv;
1319
1320 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1321}
1322
Dave Barachb5e8a772016-12-06 12:04:42 -05001323static void
1324send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001325 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1326 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001327{
1328 vl_api_ip_details_t *mp;
1329
1330 mp = vl_msg_api_alloc (sizeof (*mp));
1331 memset (mp, 0, sizeof (*mp));
1332 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1333
1334 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001335 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001336 mp->context = context;
1337
Florin Coras6c4dae22018-01-09 06:39:23 -08001338 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001339}
1340
1341static void
1342send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001343 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001344 u8 * ip, u16 prefix_length,
1345 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001346{
1347 vl_api_ip_address_details_t *mp;
1348
1349 mp = vl_msg_api_alloc (sizeof (*mp));
1350 memset (mp, 0, sizeof (*mp));
1351 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1352
1353 if (is_ipv6)
1354 {
1355 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1356 }
1357 else
1358 {
1359 u32 *tp = (u32 *) mp->ip;
1360 *tp = *(u32 *) ip;
1361 }
1362 mp->prefix_length = prefix_length;
1363 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001364 mp->sw_if_index = htonl (sw_if_index);
1365 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001366
Florin Coras6c4dae22018-01-09 06:39:23 -08001367 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001368}
1369
1370static void
1371vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1372{
1373 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001374 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001375 ip6_address_t *r6;
1376 ip4_address_t *r4;
1377 ip6_main_t *im6 = &ip6_main;
1378 ip4_main_t *im4 = &ip4_main;
1379 ip_lookup_main_t *lm6 = &im6->lookup_main;
1380 ip_lookup_main_t *lm4 = &im4->lookup_main;
1381 ip_interface_address_t *ia = 0;
1382 u32 sw_if_index = ~0;
1383 int rv __attribute__ ((unused)) = 0;
1384
1385 VALIDATE_SW_IF_INDEX (mp);
1386
1387 sw_if_index = ntohl (mp->sw_if_index);
1388
Florin Coras6c4dae22018-01-09 06:39:23 -08001389 reg = vl_api_client_index_to_registration (mp->client_index);
1390 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001391 return;
1392
Dave Barachb5e8a772016-12-06 12:04:42 -05001393 if (mp->is_ipv6)
1394 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001395 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001396 /* Do not send subnet details of the IP-interface for
1397 * unnumbered interfaces. otherwise listening clients
1398 * will be confused that the subnet is applied on more
1399 * than one interface */
1400 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001401 ({
1402 r6 = ip_interface_address_get_address (lm6, ia);
1403 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001404 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001405 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001406 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001407 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001408 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001409 else
1410 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001411 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001412 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001413 ({
1414 r4 = ip_interface_address_get_address (lm4, ia);
1415 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001416 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001417 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001418 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001419 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001420 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001421 BAD_SW_IF_INDEX_LABEL;
1422}
1423
1424static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001425send_ip_unnumbered_details (vpe_api_main_t * am,
1426 vl_api_registration_t * reg,
1427 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1428{
1429 vl_api_ip_unnumbered_details_t *mp;
1430
1431 mp = vl_msg_api_alloc (sizeof (*mp));
1432 memset (mp, 0, sizeof (*mp));
1433 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1434
1435 mp->context = context;
1436 mp->sw_if_index = htonl (sw_if_index);
1437 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1438
1439 vl_api_send_msg (reg, (u8 *) mp);
1440}
1441
1442static void
1443vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1444{
1445 vnet_main_t *vnm = vnet_get_main ();
1446 vnet_interface_main_t *im = &vnm->interface_main;
1447 int rv __attribute__ ((unused)) = 0;
1448 vpe_api_main_t *am = &vpe_api_main;
1449 vl_api_registration_t *reg;
1450 vnet_sw_interface_t *si;
1451 u32 sw_if_index;
1452
1453 sw_if_index = ntohl (mp->sw_if_index);
1454
1455 reg = vl_api_client_index_to_registration (mp->client_index);
1456 if (!reg)
1457 return;
1458
1459 if (~0 != sw_if_index)
1460 {
1461 VALIDATE_SW_IF_INDEX (mp);
1462
1463 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1464
1465 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1466 {
1467 send_ip_unnumbered_details (am, reg,
1468 sw_if_index,
1469 si->unnumbered_sw_if_index,
1470 mp->context);
1471 }
1472 }
1473 else
1474 {
1475 /* *INDENT-OFF* */
1476 pool_foreach (si, im->sw_interfaces,
1477 ({
1478 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1479 {
1480 send_ip_unnumbered_details(am, reg,
1481 si->sw_if_index,
1482 si->unnumbered_sw_if_index,
1483 mp->context);
1484 }
1485 }));
1486 /* *INDENT-ON* */
1487 }
1488
1489 BAD_SW_IF_INDEX_LABEL;
1490}
1491
1492static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001493vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1494{
1495 vpe_api_main_t *am = &vpe_api_main;
1496 vnet_main_t *vnm = vnet_get_main ();
1497 vlib_main_t *vm = vlib_get_main ();
1498 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001499 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001500 vnet_sw_interface_t *si, *sorted_sis;
1501 u32 sw_if_index = ~0;
1502
Florin Coras6c4dae22018-01-09 06:39:23 -08001503 reg = vl_api_client_index_to_registration (mp->client_index);
1504 if (!reg)
1505 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001506
1507 /* Gather interfaces. */
1508 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1509 _vec_len (sorted_sis) = 0;
1510 /* *INDENT-OFF* */
1511 pool_foreach (si, im->sw_interfaces,
1512 ({
1513 vec_add1 (sorted_sis, si[0]);
1514 }));
1515 /* *INDENT-ON* */
1516
1517 vec_foreach (si, sorted_sis)
1518 {
1519 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1520 {
1521 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1522 {
1523 continue;
1524 }
1525 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001526 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001527 }
1528 }
1529}
1530
1531static void
1532set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1533{
1534 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001535 int rv;
1536 u32 table_id;
1537 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001538
Neale Ranns227038a2017-04-21 01:07:59 -07001539 table_id = ntohl (mp->vrf_id);
1540
1541#define _(a,b) if (mp->a) flow_hash_config |= b;
1542 foreach_flow_hash_bit;
1543#undef _
1544
1545 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001546
1547 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1548}
1549
1550static void
1551set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1552{
1553 vl_api_set_ip_flow_hash_reply_t *rmp;
1554 int rv;
1555 u32 table_id;
1556 flow_hash_config_t flow_hash_config = 0;
1557
1558 table_id = ntohl (mp->vrf_id);
1559
1560#define _(a,b) if (mp->a) flow_hash_config |= b;
1561 foreach_flow_hash_bit;
1562#undef _
1563
1564 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1565
1566 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1567}
1568
1569
1570static void
1571vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1572{
1573 if (mp->is_ipv6 == 0)
1574 set_ip4_flow_hash (mp);
1575 else
1576 set_ip6_flow_hash (mp);
1577}
1578
1579static void
1580 vl_api_sw_interface_ip6nd_ra_config_t_handler
1581 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1582{
1583 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1584 vlib_main_t *vm = vlib_get_main ();
1585 int rv = 0;
1586 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1587 default_router;
1588
1589 is_no = mp->is_no == 1;
1590 suppress = mp->suppress == 1;
1591 managed = mp->managed == 1;
1592 other = mp->other == 1;
1593 ll_option = mp->ll_option == 1;
1594 send_unicast = mp->send_unicast == 1;
1595 cease = mp->cease == 1;
1596 default_router = mp->default_router == 1;
1597
1598 VALIDATE_SW_IF_INDEX (mp);
1599
1600 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1601 suppress, managed, other,
1602 ll_option, send_unicast, cease,
1603 default_router, ntohl (mp->lifetime),
1604 ntohl (mp->initial_count),
1605 ntohl (mp->initial_interval),
1606 ntohl (mp->max_interval),
1607 ntohl (mp->min_interval), is_no);
1608
1609 BAD_SW_IF_INDEX_LABEL;
1610
1611 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1612}
1613
1614static void
1615 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1616 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1617{
1618 vlib_main_t *vm = vlib_get_main ();
1619 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1620 int rv = 0;
1621 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1622
1623 VALIDATE_SW_IF_INDEX (mp);
1624
1625 is_no = mp->is_no == 1;
1626 use_default = mp->use_default == 1;
1627 no_advertise = mp->no_advertise == 1;
1628 off_link = mp->off_link == 1;
1629 no_autoconfig = mp->no_autoconfig == 1;
1630 no_onlink = mp->no_onlink == 1;
1631
1632 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1633 (ip6_address_t *) mp->address,
1634 mp->address_length, use_default,
1635 ntohl (mp->val_lifetime),
1636 ntohl (mp->pref_lifetime), no_advertise,
1637 off_link, no_autoconfig, no_onlink, is_no);
1638
1639 BAD_SW_IF_INDEX_LABEL;
1640 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1641}
1642
1643static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001644send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001645 u32 context,
1646 const ip46_address_t * addr, u32 sw_if_index)
1647{
1648 vl_api_ip6nd_proxy_details_t *mp;
1649
1650 mp = vl_msg_api_alloc (sizeof (*mp));
1651 memset (mp, 0, sizeof (*mp));
1652 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1653 mp->context = context;
1654 mp->sw_if_index = htonl (sw_if_index);
1655 memcpy (mp->address, addr, 16);
1656
Florin Coras6c4dae22018-01-09 06:39:23 -08001657 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001658}
1659
Neale Ranns3f844d02017-02-18 00:03:54 -08001660typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1661{
1662 u32 *indices;
1663} api_ip6nd_proxy_fib_table_walk_ctx_t;
1664
Neale Ranns89541992017-04-06 04:41:02 -07001665static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001666api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1667{
1668 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1669
1670 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1671 {
1672 vec_add1 (ctx->indices, fei);
1673 }
1674
Neale Ranns89541992017-04-06 04:41:02 -07001675 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001676}
1677
1678static void
1679vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1680{
1681 ip6_main_t *im6 = &ip6_main;
1682 fib_table_t *fib_table;
1683 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1684 .indices = NULL,
1685 };
1686 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001687 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001688 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001689
Florin Coras6c4dae22018-01-09 06:39:23 -08001690 reg = vl_api_client_index_to_registration (mp->client_index);
1691 if (!reg)
1692 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001693
1694 /* *INDENT-OFF* */
1695 pool_foreach (fib_table, im6->fibs,
1696 ({
1697 fib_table_walk(fib_table->ft_index,
1698 FIB_PROTOCOL_IP6,
1699 api_ip6nd_proxy_fib_table_walk,
1700 &ctx);
1701 }));
1702 /* *INDENT-ON* */
1703
1704 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1705
1706 vec_foreach (feip, ctx.indices)
1707 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001708 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001709
Florin Coras6c4dae22018-01-09 06:39:23 -08001710 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001711 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001712 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001713 fib_entry_get_resolving_interface (*feip));
1714 }
1715
1716 vec_free (ctx.indices);
1717}
1718
1719static void
1720vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1721{
1722 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1723 int rv = 0;
1724
1725 VALIDATE_SW_IF_INDEX (mp);
1726
1727 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1728 (ip6_address_t *) mp->address, mp->is_del);
1729
1730 BAD_SW_IF_INDEX_LABEL;
1731 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1732}
1733
1734static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001735 vl_api_ip6nd_send_router_solicitation_t_handler
1736 (vl_api_ip6nd_send_router_solicitation_t * mp)
1737{
1738 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1739 icmp6_send_router_solicitation_params_t params;
1740 vlib_main_t *vm = vlib_get_main ();
1741 int rv = 0;
1742
1743 VALIDATE_SW_IF_INDEX (mp);
1744
1745 BAD_SW_IF_INDEX_LABEL;
1746 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1747
1748 if (rv != 0)
1749 return;
1750
1751 params.irt = ntohl (mp->irt);
1752 params.mrt = ntohl (mp->mrt);
1753 params.mrc = ntohl (mp->mrc);
1754 params.mrd = ntohl (mp->mrd);
1755
1756 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1757 &params);
1758}
1759
1760static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001761 vl_api_sw_interface_ip6_enable_disable_t_handler
1762 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1763{
1764 vlib_main_t *vm = vlib_get_main ();
1765 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1766 vnet_main_t *vnm = vnet_get_main ();
1767 int rv = 0;
1768 clib_error_t *error;
1769
1770 vnm->api_errno = 0;
1771
1772 VALIDATE_SW_IF_INDEX (mp);
1773
1774 error =
1775 (mp->enable == 1) ? enable_ip6_interface (vm,
1776 ntohl (mp->sw_if_index)) :
1777 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1778
1779 if (error)
1780 {
1781 clib_error_report (error);
1782 rv = VNET_API_ERROR_UNSPECIFIED;
1783 }
1784 else
1785 {
1786 rv = vnm->api_errno;
1787 }
1788
1789 BAD_SW_IF_INDEX_LABEL;
1790
1791 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1792}
1793
1794static void
1795 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1796 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1797{
1798 vlib_main_t *vm = vlib_get_main ();
1799 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1800 int rv = 0;
1801 clib_error_t *error;
1802 vnet_main_t *vnm = vnet_get_main ();
1803
1804 vnm->api_errno = 0;
1805
1806 VALIDATE_SW_IF_INDEX (mp);
1807
1808 error = set_ip6_link_local_address (vm,
1809 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001810 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001811 if (error)
1812 {
1813 clib_error_report (error);
1814 rv = VNET_API_ERROR_UNSPECIFIED;
1815 }
1816 else
1817 {
1818 rv = vnm->api_errno;
1819 }
1820
1821 BAD_SW_IF_INDEX_LABEL;
1822
1823 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1824}
1825
Neale Ranns32e1c012016-11-22 17:07:28 +00001826void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001827vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001828 u32 context, const mfib_signal_t * mfs)
1829{
1830 vl_api_mfib_signal_details_t *mp;
1831 mfib_prefix_t prefix;
1832 mfib_table_t *mfib;
1833 mfib_itf_t *mfi;
1834
1835 mp = vl_msg_api_alloc (sizeof (*mp));
1836
1837 memset (mp, 0, sizeof (*mp));
1838 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1839 mp->context = context;
1840
1841 mfi = mfib_itf_get (mfs->mfs_itf);
1842 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1843 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1844 prefix.fp_proto);
1845 mp->table_id = ntohl (mfib->mft_table_id);
1846 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1847
1848 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1849 {
1850 mp->grp_address_len = ntohs (prefix.fp_len);
1851
1852 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1853 if (prefix.fp_len > 32)
1854 {
1855 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1856 }
1857 }
1858 else
1859 {
1860 mp->grp_address_len = ntohs (prefix.fp_len);
1861
1862 ASSERT (0);
1863 }
1864
1865 if (0 != mfs->mfs_buffer_len)
1866 {
1867 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1868
1869 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1870 }
1871 else
1872 {
1873 mp->ip_packet_len = 0;
1874 }
1875
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001876 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001877}
1878
1879static void
1880vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1881{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001882 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001883
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001884 reg = vl_api_client_index_to_registration (mp->client_index);
1885 if (!reg)
1886 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001887
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001888 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001889 ;
1890}
Dave Barachb5e8a772016-12-06 12:04:42 -05001891
Florin Coras595992c2017-11-06 17:17:08 -08001892static void
1893 vl_api_ip_container_proxy_add_del_t_handler
1894 (vl_api_ip_container_proxy_add_del_t * mp)
1895{
1896 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1897 vnet_ip_container_proxy_args_t args;
1898 int rv = 0;
1899 clib_error_t *error;
1900
1901 memset (&args, 0, sizeof (args));
1902 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1903 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1904 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1905 args.is_add = mp->is_add;
1906 if ((error = vnet_ip_container_proxy_add_del (&args)))
1907 {
1908 rv = clib_error_get_code (error);
1909 clib_error_report (error);
1910 }
1911
1912 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1913}
1914
Neale Rannsb8d44812017-11-10 06:53:54 -08001915static void
1916vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1917{
1918 int rv = 0;
1919 vl_api_ioam_enable_reply_t *rmp;
1920 clib_error_t *error;
1921
1922 /* Ignoring the profile id as currently a single profile
1923 * is supported */
1924 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1925 mp->seqno, mp->analyse);
1926 if (error)
1927 {
1928 clib_error_report (error);
1929 rv = clib_error_get_code (error);
1930 }
1931
1932 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1933}
1934
1935static void
1936vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1937{
1938 int rv = 0;
1939 vl_api_ioam_disable_reply_t *rmp;
1940 clib_error_t *error;
1941
1942 error = clear_ioam_rewrite_fn ();
1943 if (error)
1944 {
1945 clib_error_report (error);
1946 rv = clib_error_get_code (error);
1947 }
1948
1949 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1950}
1951
1952static void
1953 vl_api_ip_source_and_port_range_check_add_del_t_handler
1954 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1955{
1956 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1957 int rv = 0;
1958
1959 u8 is_ipv6 = mp->is_ipv6;
1960 u8 is_add = mp->is_add;
1961 u8 mask_length = mp->mask_length;
1962 ip4_address_t ip4_addr;
1963 ip6_address_t ip6_addr;
1964 u16 *low_ports = 0;
1965 u16 *high_ports = 0;
1966 u32 vrf_id;
1967 u16 tmp_low, tmp_high;
1968 u8 num_ranges;
1969 int i;
1970
1971 // Validate port range
1972 num_ranges = mp->number_of_ranges;
1973 if (num_ranges > 32)
1974 { // This is size of array in VPE.API
1975 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1976 goto reply;
1977 }
1978
1979 vec_reset_length (low_ports);
1980 vec_reset_length (high_ports);
1981
1982 for (i = 0; i < num_ranges; i++)
1983 {
1984 tmp_low = mp->low_ports[i];
1985 tmp_high = mp->high_ports[i];
1986 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1987 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1988 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1989 {
1990 rv = VNET_API_ERROR_INVALID_VALUE;
1991 goto reply;
1992 }
1993 vec_add1 (low_ports, tmp_low);
1994 vec_add1 (high_ports, tmp_high + 1);
1995 }
1996
1997 // Validate mask_length
1998 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1999 {
2000 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2001 goto reply;
2002 }
2003
2004 vrf_id = ntohl (mp->vrf_id);
2005
2006 if (vrf_id < 1)
2007 {
2008 rv = VNET_API_ERROR_INVALID_VALUE;
2009 goto reply;
2010 }
2011
2012
2013 if (is_ipv6)
2014 {
2015 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2016 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2017 mask_length,
2018 vrf_id,
2019 low_ports,
2020 high_ports, is_add);
2021 }
2022 else
2023 {
2024 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2025 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2026 mask_length,
2027 vrf_id,
2028 low_ports,
2029 high_ports, is_add);
2030 }
2031
2032reply:
2033 vec_free (low_ports);
2034 vec_free (high_ports);
2035 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2036}
2037
2038static void
2039 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2040 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2041{
2042 vlib_main_t *vm = vlib_get_main ();
2043 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2044 ip4_main_t *im = &ip4_main;
2045 int rv;
2046 u32 sw_if_index;
2047 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2048 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2049 uword *p = 0;
2050 int i;
2051
2052 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2053 ntohl (mp->tcp_out_vrf_id);
2054 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2055 ntohl (mp->udp_out_vrf_id);
2056 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2057 ntohl (mp->tcp_in_vrf_id);
2058 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2059 ntohl (mp->udp_in_vrf_id);
2060
2061
2062 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2063 {
2064 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2065 {
2066 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2067
2068 if (p == 0)
2069 {
2070 rv = VNET_API_ERROR_INVALID_VALUE;
2071 goto reply;
2072 }
2073
2074 fib_index[i] = p[0];
2075 }
2076 else
2077 fib_index[i] = ~0;
2078 }
2079 sw_if_index = ntohl (mp->sw_if_index);
2080
2081 VALIDATE_SW_IF_INDEX (mp);
2082
2083 rv =
2084 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2085 mp->is_add);
2086
2087 BAD_SW_IF_INDEX_LABEL;
2088reply:
2089
2090 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2091}
2092
2093#define IP4_ARP_EVENT 3
2094#define IP6_ND_EVENT 4
2095
2096static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2097static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2098static vlib_node_registration_t ip_resolver_process_node;
2099
2100static void
2101handle_ip4_arp_event (u32 pool_index)
2102{
2103 vpe_api_main_t *vam = &vpe_api_main;
2104 vnet_main_t *vnm = vam->vnet_main;
2105 vlib_main_t *vm = vam->vlib_main;
2106 vl_api_ip4_arp_event_t *event;
2107 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002108 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002109
2110 /* Client can cancel, die, etc. */
2111 if (pool_is_free_index (vam->arp_events, pool_index))
2112 return;
2113
2114 event = pool_elt_at_index (vam->arp_events, pool_index);
2115
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002116 reg = vl_api_client_index_to_registration (event->client_index);
2117 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002118 {
2119 (void) vnet_add_del_ip4_arp_change_event
2120 (vnm, arp_change_delete_callback,
2121 event->pid, &event->address,
2122 ip_resolver_process_node.index, IP4_ARP_EVENT,
2123 ~0 /* pool index, notused */ , 0 /* is_add */ );
2124 return;
2125 }
2126
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002127 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002128 {
2129 mp = vl_msg_api_alloc (sizeof (*mp));
2130 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002131 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002132 }
2133 else
2134 {
2135 static f64 last_time;
2136 /*
2137 * Throttle syslog msgs.
2138 * It's pretty tempting to just revoke the registration...
2139 */
2140 if (vlib_time_now (vm) > last_time + 10.0)
2141 {
2142 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2143 format_ip4_address, &event->address, event->pid);
2144 last_time = vlib_time_now (vm);
2145 }
2146 }
2147}
2148
Neale Rannsf12dad62018-06-04 18:41:24 -07002149static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002150handle_ip6_nd_event (u32 pool_index)
2151{
2152 vpe_api_main_t *vam = &vpe_api_main;
2153 vnet_main_t *vnm = vam->vnet_main;
2154 vlib_main_t *vm = vam->vlib_main;
2155 vl_api_ip6_nd_event_t *event;
2156 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002157 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002158
2159 /* Client can cancel, die, etc. */
2160 if (pool_is_free_index (vam->nd_events, pool_index))
2161 return;
2162
2163 event = pool_elt_at_index (vam->nd_events, pool_index);
2164
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002165 reg = vl_api_client_index_to_registration (event->client_index);
2166 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002167 {
2168 (void) vnet_add_del_ip6_nd_change_event
2169 (vnm, nd_change_delete_callback,
2170 event->pid, &event->address,
2171 ip_resolver_process_node.index, IP6_ND_EVENT,
2172 ~0 /* pool index, notused */ , 0 /* is_add */ );
2173 return;
2174 }
2175
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002176 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002177 {
2178 mp = vl_msg_api_alloc (sizeof (*mp));
2179 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002180 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002181 }
2182 else
2183 {
2184 static f64 last_time;
2185 /*
2186 * Throttle syslog msgs.
2187 * It's pretty tempting to just revoke the registration...
2188 */
2189 if (vlib_time_now (vm) > last_time + 10.0)
2190 {
2191 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2192 format_ip6_address, &event->address, event->pid);
2193 last_time = vlib_time_now (vm);
2194 }
2195 }
2196}
2197
2198static uword
2199resolver_process (vlib_main_t * vm,
2200 vlib_node_runtime_t * rt, vlib_frame_t * f)
2201{
2202 volatile f64 timeout = 100.0;
2203 volatile uword *event_data = 0;
2204
2205 while (1)
2206 {
2207 vlib_process_wait_for_event_or_clock (vm, timeout);
2208
2209 uword event_type =
2210 vlib_process_get_events (vm, (uword **) & event_data);
2211
2212 int i;
2213 switch (event_type)
2214 {
2215 case IP4_ARP_EVENT:
2216 for (i = 0; i < vec_len (event_data); i++)
2217 handle_ip4_arp_event (event_data[i]);
2218 break;
2219
2220 case IP6_ND_EVENT:
2221 for (i = 0; i < vec_len (event_data); i++)
2222 handle_ip6_nd_event (event_data[i]);
2223 break;
2224
2225 case ~0: /* timeout */
2226 break;
2227 }
2228
2229 vec_reset_length (event_data);
2230 }
2231 return 0; /* or not */
2232}
2233
2234/* *INDENT-OFF* */
2235VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2236 .function = resolver_process,
2237 .type = VLIB_NODE_TYPE_PROCESS,
2238 .name = "ip-route-resolver-process",
2239};
2240/* *INDENT-ON* */
2241
2242static int
2243nd_change_data_callback (u32 pool_index, u8 * new_mac,
2244 u32 sw_if_index, ip6_address_t * address)
2245{
2246 vpe_api_main_t *am = &vpe_api_main;
2247 vl_api_ip6_nd_event_t *event;
2248
2249 if (pool_is_free_index (am->nd_events, pool_index))
2250 return 1;
2251
2252 event = pool_elt_at_index (am->nd_events, pool_index);
2253 if (eth_mac_equal (event->new_mac, new_mac) &&
2254 sw_if_index == ntohl (event->sw_if_index))
2255 {
2256 return 1;
2257 }
2258
2259 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2260 event->sw_if_index = htonl (sw_if_index);
2261 return 0;
2262}
2263
2264static int
2265arp_change_delete_callback (u32 pool_index, u8 * notused)
2266{
2267 vpe_api_main_t *am = &vpe_api_main;
2268
2269 if (pool_is_free_index (am->arp_events, pool_index))
2270 return 1;
2271
2272 pool_put_index (am->arp_events, pool_index);
2273 return 0;
2274}
2275
2276static int
2277nd_change_delete_callback (u32 pool_index, u8 * notused)
2278{
2279 vpe_api_main_t *am = &vpe_api_main;
2280
2281 if (pool_is_free_index (am->nd_events, pool_index))
2282 return 1;
2283
2284 pool_put_index (am->nd_events, pool_index);
2285 return 0;
2286}
2287
2288static vlib_node_registration_t wc_arp_process_node;
2289
2290enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002291{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002292
2293static uword
2294wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2295{
2296 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2297 * and need to be volatile - to prevent them from being optimized into
2298 * a register - which could change during suspension */
2299
2300 volatile wc_arp_report_t arp_prev = { 0 };
2301 volatile wc_nd_report_t nd_prev = { 0 };
2302 volatile f64 last_arp = vlib_time_now (vm);
2303 volatile f64 last_nd = vlib_time_now (vm);
2304
2305 while (1)
2306 {
2307 vlib_process_wait_for_event (vm);
2308 uword event_type = WC_ARP_REPORT;
2309 void *event_data = vlib_process_get_event_data (vm, &event_type);
2310
2311 f64 now = vlib_time_now (vm);
2312 int i;
2313 if (event_type == WC_ARP_REPORT)
2314 {
2315 wc_arp_report_t *arp_events = event_data;
2316 for (i = 0; i < vec_len (arp_events); i++)
2317 {
2318 /* discard dup event */
2319 if (arp_prev.ip4 == arp_events[i].ip4 &&
2320 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2321 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2322 (now - last_arp) < 10.0)
2323 {
2324 continue;
2325 }
2326 arp_prev = arp_events[i];
2327 last_arp = now;
2328 vpe_client_registration_t *reg;
2329 /* *INDENT-OFF* */
2330 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2331 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002332 vl_api_registration_t *vl_reg;
2333 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002334 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002335 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002336 {
2337 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2338 memset (event, 0, sizeof *event);
2339 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2340 event->client_index = reg->client_index;
2341 event->pid = reg->client_pid;
2342 event->mac_ip = 1;
2343 event->address = arp_events[i].ip4;
2344 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2345 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002346 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002347 }
2348 }));
2349 /* *INDENT-ON* */
2350 }
2351 }
2352 else if (event_type == WC_ND_REPORT)
2353 {
2354 wc_nd_report_t *nd_events = event_data;
2355 for (i = 0; i < vec_len (nd_events); i++)
2356 {
2357 /* discard dup event */
2358 if (ip6_address_is_equal
2359 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2360 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2361 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2362 && (now - last_nd) < 10.0)
2363 {
2364 continue;
2365 }
2366 nd_prev = nd_events[i];
2367 last_nd = now;
2368 vpe_client_registration_t *reg;
2369 /* *INDENT-OFF* */
2370 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2371 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002372 vl_api_registration_t *vl_reg;
2373 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2374 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002375 {
2376 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2377 memset (event, 0, sizeof *event);
2378 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2379 event->client_index = reg->client_index;
2380 event->pid = reg->client_pid;
2381 event->mac_ip = 1;
2382 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2383 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2384 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002385 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002386 }
2387 }));
2388 /* *INDENT-ON* */
2389 }
2390 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002391 else if (event_type == RA_REPORT)
2392 {
2393 ra_report_t *ra_events = event_data;
2394 for (i = 0; i < vec_len (ra_events); i++)
2395 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002396 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2397 call_ip6_neighbor_callbacks (&ra_events[i],
2398 npm->ra_report_functions);
2399
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002400 vpe_client_registration_t *reg;
2401 /* *INDENT-OFF* */
2402 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2403 ({
2404 vl_api_registration_t *vl_reg;
2405 vl_reg =
2406 vl_api_client_index_to_registration (reg->client_index);
2407 if (vl_reg && vl_api_can_send_msg (vl_reg))
2408 {
2409 u32 event_size =
2410 sizeof (vl_api_ip6_ra_event_t) +
2411 vec_len (ra_events[i].prefixes) *
2412 sizeof (vl_api_ip6_ra_prefix_info_t);
2413 vl_api_ip6_ra_event_t *event =
2414 vl_msg_api_alloc (event_size);
2415 memset (event, 0, event_size);
2416 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2417 event->client_index = reg->client_index;
2418 event->pid = reg->client_pid;
2419
2420 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2421
2422 memcpy (event->router_address, ra_events[i].router_address, 16);
2423
2424 event->current_hop_limit = ra_events[i].current_hop_limit;
2425 event->flags = ra_events[i].flags;
2426 event->router_lifetime_in_sec =
2427 clib_host_to_net_u16 (ra_events
2428 [i].router_lifetime_in_sec);
2429 event->neighbor_reachable_time_in_msec =
2430 clib_host_to_net_u32 (ra_events
2431 [i].neighbor_reachable_time_in_msec);
2432 event->time_in_msec_between_retransmitted_neighbor_solicitations
2433 =
2434 clib_host_to_net_u32 (ra_events
2435 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2436
2437 event->n_prefixes =
2438 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2439 vl_api_ip6_ra_prefix_info_t *prefix =
2440 (typeof (prefix)) event->prefixes;
2441 u32 j;
2442 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2443 {
2444 ra_report_prefix_info_t *info =
2445 &ra_events[i].prefixes[j];
2446 memcpy (prefix->dst_address, info->dst_address.as_u8,
2447 16);
2448 prefix->dst_address_length = info->dst_address_length;
2449 prefix->flags = info->flags;
2450 prefix->valid_time =
2451 clib_host_to_net_u32 (info->valid_time);
2452 prefix->preferred_time =
2453 clib_host_to_net_u32 (info->preferred_time);
2454 prefix++;
2455 }
2456
2457 vl_api_send_msg (vl_reg, (u8 *) event);
2458 }
2459 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002460 /* *INDENT-ON* */
2461 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002462 }
2463 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002464 vlib_process_put_event_data (vm, event_data);
2465 }
2466
2467 return 0;
2468}
2469
2470/* *INDENT-OFF* */
2471VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2472 .function = wc_arp_process,
2473 .type = VLIB_NODE_TYPE_PROCESS,
2474 .name = "wildcard-ip4-arp-publisher-process",
2475};
2476/* *INDENT-ON* */
2477
2478static int
2479arp_change_data_callback (u32 pool_index, u8 * new_mac,
2480 u32 sw_if_index, u32 address)
2481{
2482 vpe_api_main_t *am = &vpe_api_main;
2483 vl_api_ip4_arp_event_t *event;
2484
2485 if (pool_is_free_index (am->arp_events, pool_index))
2486 return 1;
2487
2488 event = pool_elt_at_index (am->arp_events, pool_index);
2489 if (eth_mac_equal (event->new_mac, new_mac) &&
2490 sw_if_index == ntohl (event->sw_if_index))
2491 {
2492 return 1;
2493 }
2494
2495 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2496 event->sw_if_index = htonl (sw_if_index);
2497 return 0;
2498}
2499
2500static void
2501vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2502{
2503 vpe_api_main_t *am = &vpe_api_main;
2504 vnet_main_t *vnm = vnet_get_main ();
2505 vl_api_want_ip4_arp_events_reply_t *rmp;
2506 int rv = 0;
2507
2508 if (mp->address == 0)
2509 {
2510 uword *p =
2511 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2512 vpe_client_registration_t *rp;
2513 if (p)
2514 {
2515 if (mp->enable_disable)
2516 {
2517 clib_warning ("pid %d: already enabled...", mp->pid);
2518 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2519 goto reply;
2520 }
2521 else
2522 {
2523 rp =
2524 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2525 pool_put (am->wc_ip4_arp_events_registrations, rp);
2526 hash_unset (am->wc_ip4_arp_events_registration_hash,
2527 mp->client_index);
2528 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002529 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002530 goto reply;
2531 }
2532 }
2533 if (mp->enable_disable == 0)
2534 {
2535 clib_warning ("pid %d: already disabled...", mp->pid);
2536 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2537 goto reply;
2538 }
2539 pool_get (am->wc_ip4_arp_events_registrations, rp);
2540 rp->client_index = mp->client_index;
2541 rp->client_pid = mp->pid;
2542 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2543 rp - am->wc_ip4_arp_events_registrations);
2544 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2545 goto reply;
2546 }
2547
2548 if (mp->enable_disable)
2549 {
2550 vl_api_ip4_arp_event_t *event;
2551 pool_get (am->arp_events, event);
2552 rv = vnet_add_del_ip4_arp_change_event
2553 (vnm, arp_change_data_callback,
2554 mp->pid, &mp->address /* addr, in net byte order */ ,
2555 ip_resolver_process_node.index,
2556 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2557
2558 if (rv)
2559 {
2560 pool_put (am->arp_events, event);
2561 goto reply;
2562 }
2563 memset (event, 0, sizeof (*event));
2564
2565 /* Python API expects events to have no context */
2566 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2567 event->client_index = mp->client_index;
2568 event->address = mp->address;
2569 event->pid = mp->pid;
2570 if (mp->address == 0)
2571 event->mac_ip = 1;
2572 }
2573 else
2574 {
2575 rv = vnet_add_del_ip4_arp_change_event
2576 (vnm, arp_change_delete_callback,
2577 mp->pid, &mp->address /* addr, in net byte order */ ,
2578 ip_resolver_process_node.index,
2579 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2580 }
2581reply:
2582 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2583}
2584
Neale Rannsf12dad62018-06-04 18:41:24 -07002585static clib_error_t *
2586want_ip4_arp_events_reaper (u32 client_index)
2587{
2588 vpe_client_registration_t *rp;
2589 vl_api_ip4_arp_event_t *event;
2590 u32 *to_delete, *event_id;
2591 vpe_api_main_t *am;
2592 vnet_main_t *vnm;
2593 uword *p;
2594
2595 am = &vpe_api_main;
2596 vnm = vnet_get_main ();
2597 to_delete = NULL;
2598
2599 /* clear out all of its pending resolutions */
2600 /* *INDENT-OFF* */
2601 pool_foreach(event, am->arp_events,
2602 ({
2603 if (event->client_index == client_index)
2604 {
2605 vec_add1(to_delete, event - am->arp_events);
2606 }
2607 }));
2608 /* *INDENT-ON* */
2609
2610 vec_foreach (event_id, to_delete)
2611 {
2612 event = pool_elt_at_index (am->arp_events, *event_id);
2613 vnet_add_del_ip4_arp_change_event
2614 (vnm, arp_change_delete_callback,
2615 event->pid, &event->address,
2616 ip_resolver_process_node.index, IP4_ARP_EVENT,
2617 ~0 /* pool index, notused */ , 0 /* is_add */ );
2618 }
2619 vec_free (to_delete);
2620
2621 /* remove from the registration hash */
2622 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2623
2624 if (p)
2625 {
2626 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2627 pool_put (am->wc_ip4_arp_events_registrations, rp);
2628 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2629 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2630 wc_arp_set_publisher_node (~0, REPORT_MAX);
2631 }
2632 return (NULL);
2633}
2634
2635VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2636
Neale Rannsb8d44812017-11-10 06:53:54 -08002637static void
2638vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2639{
2640 vpe_api_main_t *am = &vpe_api_main;
2641 vnet_main_t *vnm = vnet_get_main ();
2642 vl_api_want_ip6_nd_events_reply_t *rmp;
2643 int rv = 0;
2644
2645 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2646 {
2647 uword *p =
2648 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2649 vpe_client_registration_t *rp;
2650 if (p)
2651 {
2652 if (mp->enable_disable)
2653 {
2654 clib_warning ("pid %d: already enabled...", mp->pid);
2655 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2656 goto reply;
2657 }
2658 else
2659 {
2660 rp =
2661 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2662 pool_put (am->wc_ip6_nd_events_registrations, rp);
2663 hash_unset (am->wc_ip6_nd_events_registration_hash,
2664 mp->client_index);
2665 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002666 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002667 goto reply;
2668 }
2669 }
2670 if (mp->enable_disable == 0)
2671 {
2672 clib_warning ("pid %d: already disabled...", mp->pid);
2673 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2674 goto reply;
2675 }
2676 pool_get (am->wc_ip6_nd_events_registrations, rp);
2677 rp->client_index = mp->client_index;
2678 rp->client_pid = mp->pid;
2679 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2680 rp - am->wc_ip6_nd_events_registrations);
2681 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2682 goto reply;
2683 }
2684
2685 if (mp->enable_disable)
2686 {
2687 vl_api_ip6_nd_event_t *event;
2688 pool_get (am->nd_events, event);
2689
2690 rv = vnet_add_del_ip6_nd_change_event
2691 (vnm, nd_change_data_callback,
2692 mp->pid, mp->address /* addr, in net byte order */ ,
2693 ip_resolver_process_node.index,
2694 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2695
2696 if (rv)
2697 {
2698 pool_put (am->nd_events, event);
2699 goto reply;
2700 }
2701 memset (event, 0, sizeof (*event));
2702
2703 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2704 event->client_index = mp->client_index;
2705 clib_memcpy (event->address, mp->address, sizeof event->address);
2706 event->pid = mp->pid;
2707 }
2708 else
2709 {
2710 rv = vnet_add_del_ip6_nd_change_event
2711 (vnm, nd_change_delete_callback,
2712 mp->pid, mp->address /* addr, in net byte order */ ,
2713 ip_resolver_process_node.index,
2714 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2715 }
2716reply:
2717 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2718}
2719
Neale Rannsf12dad62018-06-04 18:41:24 -07002720static clib_error_t *
2721want_ip6_nd_events_reaper (u32 client_index)
2722{
2723
2724 vpe_client_registration_t *rp;
2725 vl_api_ip6_nd_event_t *event;
2726 u32 *to_delete, *event_id;
2727 vpe_api_main_t *am;
2728 vnet_main_t *vnm;
2729 uword *p;
2730
2731 am = &vpe_api_main;
2732 vnm = vnet_get_main ();
2733 to_delete = NULL;
2734
2735 /* clear out all of its pending resolutions */
2736 /* *INDENT-OFF* */
2737 pool_foreach(event, am->nd_events,
2738 ({
2739 if (event->client_index == client_index)
2740 {
2741 vec_add1(to_delete, event - am->nd_events);
2742 }
2743 }));
2744 /* *INDENT-ON* */
2745
2746 vec_foreach (event_id, to_delete)
2747 {
2748 event = pool_elt_at_index (am->nd_events, *event_id);
2749 vnet_add_del_ip6_nd_change_event
2750 (vnm, nd_change_delete_callback,
2751 event->pid, &event->address,
2752 ip_resolver_process_node.index, IP6_ND_EVENT,
2753 ~0 /* pool index, notused */ , 0 /* is_add */ );
2754 }
2755 vec_free (to_delete);
2756
2757 /* remove from the registration hash */
2758 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2759
2760 if (p)
2761 {
2762 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2763 pool_put (am->wc_ip6_nd_events_registrations, rp);
2764 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2765 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2766 wc_nd_set_publisher_node (~0, REPORT_MAX);
2767 }
2768 return (NULL);
2769}
2770
2771VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2772
Neale Rannsb8d44812017-11-10 06:53:54 -08002773static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002774vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2775{
2776 vpe_api_main_t *am = &vpe_api_main;
2777 vl_api_want_ip6_ra_events_reply_t *rmp;
2778 int rv = 0;
2779
2780 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2781 vpe_client_registration_t *rp;
2782 if (p)
2783 {
2784 if (mp->enable_disable)
2785 {
2786 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2787 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2788 goto reply;
2789 }
2790 else
2791 {
2792 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2793 pool_put (am->ip6_ra_events_registrations, rp);
2794 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002795 goto reply;
2796 }
2797 }
2798 if (mp->enable_disable == 0)
2799 {
2800 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2801 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2802 goto reply;
2803 }
2804 pool_get (am->ip6_ra_events_registrations, rp);
2805 rp->client_index = mp->client_index;
2806 rp->client_pid = ntohl (mp->pid);
2807 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2808 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002809
2810reply:
2811 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2812}
2813
Neale Rannsf12dad62018-06-04 18:41:24 -07002814static clib_error_t *
2815want_ip6_ra_events_reaper (u32 client_index)
2816{
2817 vpe_api_main_t *am = &vpe_api_main;
2818 vpe_client_registration_t *rp;
2819 uword *p;
2820
2821 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2822
2823 if (p)
2824 {
2825 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2826 pool_put (am->ip6_ra_events_registrations, rp);
2827 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2828 }
2829 return (NULL);
2830}
2831
2832VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2833
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002834static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002835vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2836{
2837 vl_api_proxy_arp_add_del_reply_t *rmp;
2838 u32 fib_index;
2839 int rv;
2840 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002841 uword *p;
2842
Ole Troan3288ed72017-12-06 17:00:05 +01002843 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002844
Neale Ranns0053de62018-05-22 08:40:52 -07002845 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002846
2847 if (!p)
2848 {
2849 rv = VNET_API_ERROR_NO_SUCH_FIB;
2850 goto out;
2851 }
2852
2853 fib_index = p[0];
2854
Neale Ranns0053de62018-05-22 08:40:52 -07002855 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2856 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002857 fib_index, mp->is_add == 0);
2858
2859out:
Ole Troan3288ed72017-12-06 17:00:05 +01002860 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002861 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2862}
2863
Neale Ranns0053de62018-05-22 08:40:52 -07002864typedef struct proxy_arp_walk_ctx_t_
2865{
2866 vl_api_registration_t *reg;
2867 u32 context;
2868} proxy_arp_walk_ctx_t;
2869
2870static walk_rc_t
2871send_proxy_arp_details (const ip4_address_t * lo_addr,
2872 const ip4_address_t * hi_addr,
2873 u32 fib_index, void *data)
2874{
2875 vl_api_proxy_arp_details_t *mp;
2876 proxy_arp_walk_ctx_t *ctx;
2877
2878 ctx = data;
2879
2880 mp = vl_msg_api_alloc (sizeof (*mp));
2881 memset (mp, 0, sizeof (*mp));
2882 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2883 mp->context = ctx->context;
2884 mp->proxy.vrf_id = htonl (fib_index);
2885 clib_memcpy (mp->proxy.low_address, lo_addr,
2886 sizeof (mp->proxy.low_address));
2887 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2888
2889 vl_api_send_msg (ctx->reg, (u8 *) mp);
2890
2891 return (WALK_CONTINUE);
2892}
2893
2894static void
2895vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2896{
2897 vl_api_registration_t *reg;
2898
2899 reg = vl_api_client_index_to_registration (mp->client_index);
2900 if (!reg)
2901 return;
2902
2903 proxy_arp_walk_ctx_t wctx = {
2904 .reg = reg,
2905 .context = mp->context,
2906 };
2907
2908 proxy_arp_walk (send_proxy_arp_details, &wctx);
2909}
2910
2911static walk_rc_t
2912send_proxy_arp_intfc_details (vnet_main_t * vnm,
2913 vnet_sw_interface_t * si, void *data)
2914{
2915 vl_api_proxy_arp_intfc_details_t *mp;
2916 proxy_arp_walk_ctx_t *ctx;
2917
2918 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
2919 return (WALK_CONTINUE);
2920
2921 ctx = data;
2922
2923 mp = vl_msg_api_alloc (sizeof (*mp));
2924 memset (mp, 0, sizeof (*mp));
2925 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
2926 mp->context = ctx->context;
2927 mp->sw_if_index = htonl (si->sw_if_index);
2928
2929 vl_api_send_msg (ctx->reg, (u8 *) mp);
2930
2931 return (WALK_CONTINUE);
2932}
2933
2934static void
2935vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
2936{
2937 vl_api_registration_t *reg;
2938
2939 reg = vl_api_client_index_to_registration (mp->client_index);
2940 if (!reg)
2941 return;
2942
2943 proxy_arp_walk_ctx_t wctx = {
2944 .reg = reg,
2945 .context = mp->context,
2946 };
2947
2948 vnet_sw_interface_walk (vnet_get_main (),
2949 send_proxy_arp_intfc_details, &wctx);
2950}
2951
Neale Rannsb8d44812017-11-10 06:53:54 -08002952static void
2953 vl_api_proxy_arp_intfc_enable_disable_t_handler
2954 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2955{
2956 int rv = 0;
2957 vnet_main_t *vnm = vnet_get_main ();
2958 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2959
2960 VALIDATE_SW_IF_INDEX (mp);
2961
2962 vnet_sw_interface_t *si =
2963 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2964
2965 ASSERT (si);
2966
2967 if (mp->enable_disable)
2968 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2969 else
2970 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2971
2972 BAD_SW_IF_INDEX_LABEL;
2973
2974 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2975}
2976
John Loc7b43042018-04-13 16:46:22 -04002977static void
2978vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
2979{
2980 int rv = 0;
2981 vlib_main_t *vm = vlib_get_main ();
2982 vl_api_ip_probe_neighbor_reply_t *rmp;
2983 clib_error_t *error;
2984
2985 VALIDATE_SW_IF_INDEX (mp);
2986
2987 u32 sw_if_index = ntohl (mp->sw_if_index);
2988
2989 if (mp->is_ipv6)
2990 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04002991 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04002992 else
2993 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04002994 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04002995
2996 if (error)
2997 {
2998 clib_error_report (error);
2999 rv = clib_error_get_code (error);
3000 }
3001
3002 BAD_SW_IF_INDEX_LABEL;
3003
3004 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3005}
3006
John Lo7f358b32018-04-28 01:19:24 -04003007static void
3008 vl_api_ip_scan_neighbor_enable_disable_t_handler
3009 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3010{
3011 int rv = 0;
3012 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3013 ip_neighbor_scan_arg_t arg;
3014
3015 arg.mode = mp->mode;
3016 arg.scan_interval = mp->scan_interval;
3017 arg.max_proc_time = mp->max_proc_time;
3018 arg.max_update = mp->max_update;
3019 arg.scan_int_delay = mp->scan_int_delay;
3020 arg.stale_threshold = mp->stale_threshold;
3021 ip_neighbor_scan_enable_disable (&arg);
3022
3023 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3024}
3025
Neale Rannsb8d44812017-11-10 06:53:54 -08003026static int
3027ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3028{
3029 vnet_main_t *vnm = vnet_get_main ();
3030 vnet_interface_main_t *im = &vnm->interface_main;
3031 ip4_main_t *im4 = &ip4_main;
3032 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003033 fib_table_t *fib_table;
3034 ip4_fib_t *fib;
3035 u32 sw_if_index;
3036 int i;
3037 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3038 u32 target_fib_id = ntohl (mp->vrf_id);
3039
Ole Troan3288ed72017-12-06 17:00:05 +01003040 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003041
3042 /* *INDENT-OFF* */
3043 pool_foreach (fib_table, im4->fibs,
3044 ({
3045 vnet_sw_interface_t * si;
3046
3047 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3048
3049 if (fib->table_id != target_fib_id)
3050 continue;
3051
3052 /* remove any mpls encap/decap labels */
3053 mpls_fib_reset_labels (fib->table_id);
3054
3055 /* remove any proxy arps in this fib */
3056 vnet_proxy_arp_fib_reset (fib->table_id);
3057
3058 /* Set the flow hash for this fib to the default */
3059 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3060
3061 vec_reset_length (sw_if_indices_to_shut);
3062
3063 /* Shut down interfaces in this FIB / clean out intfc routes */
3064 pool_foreach (si, im->sw_interfaces,
3065 ({
3066 u32 sw_if_index = si->sw_if_index;
3067
3068 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3069 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3070 fib->index))
3071 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3072 }));
3073
3074 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3075 sw_if_index = sw_if_indices_to_shut[i];
3076
3077 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3078 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3079 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3080 }
3081
3082 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3083
3084 rv = 0;
3085 break;
3086 })); /* pool_foreach (fib) */
3087 /* *INDENT-ON* */
3088
Ole Troan3288ed72017-12-06 17:00:05 +01003089 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003090 return rv;
3091}
3092
3093static int
3094ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3095{
3096 vnet_main_t *vnm = vnet_get_main ();
3097 vnet_interface_main_t *im = &vnm->interface_main;
3098 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003099 static u32 *sw_if_indices_to_shut;
3100 fib_table_t *fib_table;
3101 ip6_fib_t *fib;
3102 u32 sw_if_index;
3103 int i;
3104 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3105 u32 target_fib_id = ntohl (mp->vrf_id);
3106
Ole Troan3288ed72017-12-06 17:00:05 +01003107 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003108
3109 /* *INDENT-OFF* */
3110 pool_foreach (fib_table, im6->fibs,
3111 ({
3112 vnet_sw_interface_t * si;
3113
3114 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3115
3116 if (fib->table_id != target_fib_id)
3117 continue;
3118
3119 vec_reset_length (sw_if_indices_to_shut);
3120
3121 /* Set the flow hash for this fib to the default */
3122 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3123
3124 /* Shut down interfaces in this FIB / clean out intfc routes */
3125 pool_foreach (si, im->sw_interfaces,
3126 ({
3127 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3128 fib->index)
3129 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3130 }));
3131
3132 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3133 sw_if_index = sw_if_indices_to_shut[i];
3134
3135 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3136 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3137 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3138 }
3139
3140 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3141
3142 rv = 0;
3143 break;
3144 })); /* pool_foreach (fib) */
3145 /* *INDENT-ON* */
3146
Ole Troan3288ed72017-12-06 17:00:05 +01003147 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003148 return rv;
3149}
3150
3151static void
3152vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3153{
3154 int rv;
3155 vl_api_reset_fib_reply_t *rmp;
3156
3157 if (mp->is_ipv6)
3158 rv = ip6_reset_fib_t_handler (mp);
3159 else
3160 rv = ip4_reset_fib_t_handler (mp);
3161
3162 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3163}
3164
3165static void
3166vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3167{
3168 int rv;
3169 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3170 vnet_main_t *vnm = vnet_get_main ();
3171 clib_error_t *error;
3172
3173 vnm->api_errno = 0;
3174
3175 if (mp->is_ipv6)
3176 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3177 else
3178 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3179
3180 if (error)
3181 {
3182 clib_error_report (error);
3183 rv = VNET_API_ERROR_UNSPECIFIED;
3184 }
3185 else
3186 {
3187 rv = vnm->api_errno;
3188 }
3189
3190 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3191}
3192
Klement Sekera75e7d132017-09-20 08:26:30 +02003193void
3194vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3195{
3196 vl_api_ip_reassembly_set_reply_t *rmp;
3197 int rv = 0;
3198 if (mp->is_ip6)
3199 {
3200 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3201 clib_net_to_host_u32 (mp->max_reassemblies),
3202 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3203 }
3204 else
3205 {
3206 rv = ip4_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
3211 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3212}
3213
3214void
3215vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3216{
3217 unix_shared_memory_queue_t *q;
3218
3219 q = vl_api_client_index_to_input_queue (mp->client_index);
3220
3221 if (q == 0)
3222 return;
3223
3224 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
3225 memset (rmp, 0, sizeof (*rmp));
3226 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3227 rmp->context = mp->context;
3228 rmp->retval = 0;
3229 if (mp->is_ip6)
3230 {
3231 rmp->is_ip6 = 1;
3232 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3233 &rmp->expire_walk_interval_ms);
3234 }
3235 else
3236 {
3237 rmp->is_ip6 = 0;
3238 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3239 &rmp->expire_walk_interval_ms);
3240 }
3241 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3242 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3243 rmp->expire_walk_interval_ms =
3244 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3245 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3246}
3247
Klement Sekera4c533132018-02-22 11:41:12 +01003248void
3249 vl_api_ip_reassembly_enable_disable_t_handler
3250 (vl_api_ip_reassembly_enable_disable_t * mp)
3251{
3252 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3253 int rv = 0;
3254 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3255 mp->enable_ip4);
3256 if (0 == rv)
3257 {
3258 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3259 mp->enable_ip6);
3260 }
3261
3262 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3263}
3264
Dave Barachb5e8a772016-12-06 12:04:42 -05003265#define vl_msg_name_crc_list
3266#include <vnet/ip/ip.api.h>
3267#undef vl_msg_name_crc_list
3268
3269static void
3270setup_message_id_table (api_main_t * am)
3271{
3272#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3273 foreach_vl_msg_name_crc_ip;
3274#undef _
3275}
3276
3277static clib_error_t *
3278ip_api_hookup (vlib_main_t * vm)
3279{
3280 api_main_t *am = &api_main;
3281
3282#define _(N,n) \
3283 vl_msg_api_set_handlers(VL_API_##N, #n, \
3284 vl_api_##n##_t_handler, \
3285 vl_noop_handler, \
3286 vl_api_##n##_t_endian, \
3287 vl_api_##n##_t_print, \
3288 sizeof(vl_api_##n##_t), 1);
3289 foreach_ip_api_msg;
3290#undef _
3291
3292 /*
3293 * Set up the (msg_name, crc, message-id) table
3294 */
3295 setup_message_id_table (am);
3296
Juraj Sloboda52574522018-05-03 10:03:50 +02003297 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3298
Dave Barachb5e8a772016-12-06 12:04:42 -05003299 return 0;
3300}
3301
3302VLIB_API_INIT_FUNCTION (ip_api_hookup);
3303
3304/*
3305 * fd.io coding-style-patch-verification: ON
3306 *
3307 * Local Variables:
3308 * eval: (c-set-style "gnu")
3309 * End:
3310 */