blob: b9fa001553f8f4dacaac7ebc773fe2c26baf8859 [file] [log] [blame]
Dave Barachb5e8a772016-12-06 12:04:42 -05001/*
2 *------------------------------------------------------------------
3 * ip_api.c - vnet ip api
4 *
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/vnet.h>
21#include <vlibmemory/api.h>
22
23#include <vnet/interface.h>
24#include <vnet/api_errno.h>
25#include <vnet/ethernet/ethernet.h>
26#include <vnet/ip/ip.h>
John Lo7f358b32018-04-28 01:19:24 -040027#include <vnet/ip/ip_neighbor.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050028#include <vnet/ip/ip6_neighbor.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010029#include <vnet/ip/ip_punt_drop.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050030#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>
Matus Fabian75b9f452018-10-02 23:27:21 -070049#include <vnet/ip/ip_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050050
51#include <vnet/vnet_msg_enum.h>
52
53#define vl_typedefs /* define message structures */
54#include <vnet/vnet_all_api_h.h>
55#undef vl_typedefs
56
57#define vl_endianfun /* define message structures */
58#include <vnet/vnet_all_api_h.h>
59#undef vl_endianfun
60
61/* instantiate all the print functions we know about */
62#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
63#define vl_printfun
64#include <vnet/vnet_all_api_h.h>
65#undef vl_printfun
66
67#include <vlibapi/api_helper_macros.h>
68
Neale Ranns5a8123b2017-01-26 01:18:23 -080069
Dave Barachb5e8a772016-12-06 12:04:42 -050070#define foreach_ip_api_msg \
71_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050072_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080073_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080074_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050075_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000076_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080077_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050078_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070079_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050080_(IP_DUMP, ip_dump) \
81_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080082_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
John Loc7b43042018-04-13 16:46:22 -040083_(IP_PROBE_NEIGHBOR, ip_probe_neighbor) \
John Lo7f358b32018-04-28 01:19:24 -040084_(IP_SCAN_NEIGHBOR_ENABLE_DISABLE, ip_scan_neighbor_enable_disable) \
Neale Rannsb8d44812017-11-10 06:53:54 -080085_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
86_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010087_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080088_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
Neale Ranns0053de62018-05-22 08:40:52 -070089_(PROXY_ARP_DUMP, proxy_arp_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080090_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
Neale Ranns0053de62018-05-22 08:40:52 -070091 _(PROXY_ARP_INTFC_DUMP, proxy_arp_intfc_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080092_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050093_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070094_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070095_(IP_PUNT_POLICE, ip_punt_police) \
96_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050097_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
98_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
99_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -0800100_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
101_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100102_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -0500103_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800104_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
Matus Fabian75b9f452018-10-02 23:27:21 -0700105_(IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800106_(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) \
Chore3460b012018-11-28 10:53:11 +0330112_(IP_SOURCE_CHECK_INTERFACE_ADD_DEL, \
113 ip_source_check_interface_add_del) \
Klement Sekera75e7d132017-09-20 08:26:30 +0200114_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100115_(IP_REASSEMBLY_GET, ip_reassembly_get) \
Pavel Kotucek609e1212018-11-27 09:59:44 +0100116_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
117_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
118
Dave Barachb5e8a772016-12-06 12:04:42 -0500119
120extern void stats_dslock_with_hint (int hint, int tag);
121extern void stats_dsunlock (void);
122
123static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600124send_ip_neighbor_details (u32 sw_if_index,
125 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500126 u8 is_static,
127 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800128 u8 * ip_address, vl_api_registration_t * reg,
129 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500130{
131 vl_api_ip_neighbor_details_t *mp;
132
133 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400134 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500135 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
136 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600137 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500138 mp->is_ipv6 = is_ipv6;
139 mp->is_static = is_static;
140 memcpy (mp->mac_address, mac_address, 6);
141 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
142
Florin Coras6c4dae22018-01-09 06:39:23 -0800143 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500144}
145
146static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500147vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
148{
Florin Coras6c4dae22018-01-09 06:39:23 -0800149 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500150
Florin Coras6c4dae22018-01-09 06:39:23 -0800151 reg = vl_api_client_index_to_registration (mp->client_index);
152 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500153 return;
154
155 u32 sw_if_index = ntohl (mp->sw_if_index);
156
157 if (mp->is_ipv6)
158 {
159 ip6_neighbor_t *n, *ns;
160
161 ns = ip6_neighbors_entries (sw_if_index);
162 /* *INDENT-OFF* */
163 vec_foreach (n, ns)
164 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500165 send_ip_neighbor_details
Brant Lin285434a2018-06-13 06:01:58 -0400166 (n->key.sw_if_index, mp->is_ipv6,
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600167 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168 (u8 *) n->link_layer_address,
169 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800170 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500171 }
172 /* *INDENT-ON* */
173 vec_free (ns);
174 }
175 else
176 {
177 ethernet_arp_ip4_entry_t *n, *ns;
178
179 ns = ip4_neighbor_entries (sw_if_index);
180 /* *INDENT-OFF* */
181 vec_foreach (n, ns)
182 {
Brant Lin285434a2018-06-13 06:01:58 -0400183 send_ip_neighbor_details (n->sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500184 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
185 (u8*) n->ethernet_address,
186 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800187 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500188 }
189 /* *INDENT-ON* */
190 vec_free (ns);
191 }
192}
193
Dave Barachb5e8a772016-12-06 12:04:42 -0500194static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500195send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800196 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700197 const fib_table_t * table,
198 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500199 fib_route_path_encode_t * api_rpaths, u32 context)
200{
201 vl_api_ip_fib_details_t *mp;
202 fib_route_path_encode_t *api_rpath;
203 vl_api_fib_path_t *fp;
204 int path_count;
205
206 path_count = vec_len (api_rpaths);
207 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
208 if (!mp)
209 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400210 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500211 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
212 mp->context = context;
213
Neale Ranns2297af02017-09-12 09:45:04 -0700214 mp->table_id = htonl (table->ft_table_id);
215 memcpy (mp->table_name, table->ft_desc,
216 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500217 mp->address_length = pfx->fp_len;
218 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
Neale Ranns008dbe12018-09-07 09:32:36 -0700219 mp->stats_index =
220 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500221
222 mp->count = htonl (path_count);
223 fp = mp->path;
224 vec_foreach (api_rpath, api_rpaths)
225 {
Neale Ranns81458422018-03-12 06:59:36 -0700226 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500227 fp++;
228 }
229
Florin Coras6c4dae22018-01-09 06:39:23 -0800230 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500231}
232
Neale Rannsa3af3372017-03-28 03:49:52 -0700233typedef struct vl_api_ip_fib_dump_walk_ctx_t_
234{
235 fib_node_index_t *feis;
236} vl_api_ip_fib_dump_walk_ctx_t;
237
Neale Ranns89541992017-04-06 04:41:02 -0700238static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700239vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
240{
241 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
242
243 vec_add1 (ctx->feis, fei);
244
Neale Ranns89541992017-04-06 04:41:02 -0700245 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700246}
247
Dave Barachb5e8a772016-12-06 12:04:42 -0500248static void
249vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
250{
251 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800252 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500253 ip4_main_t *im = &ip4_main;
254 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700255 fib_node_index_t *lfeip;
Neale Rannsc5d43172018-07-30 08:04:40 -0700256 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500257 u32 fib_index;
258 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700259 vl_api_ip_fib_dump_walk_ctx_t ctx = {
260 .feis = NULL,
261 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500262
Florin Coras6c4dae22018-01-09 06:39:23 -0800263 reg = vl_api_client_index_to_registration (mp->client_index);
264 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500265 return;
266
267 /* *INDENT-OFF* */
268 pool_foreach (fib_table, im->fibs,
269 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700270 fib_table_walk(fib_table->ft_index,
271 FIB_PROTOCOL_IP4,
272 vl_api_ip_fib_dump_walk,
273 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500274 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500276
Neale Rannsa3af3372017-03-28 03:49:52 -0700277 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500278
Neale Rannsa3af3372017-03-28 03:49:52 -0700279 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500280 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700281 pfx = fib_entry_get_prefix (*lfeip);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500282 fib_index = fib_entry_get_fib_index (*lfeip);
Neale Rannsc5d43172018-07-30 08:04:40 -0700283 fib_table = fib_table_get (fib_index, pfx->fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500284 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 fib_entry_encode (*lfeip, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700286 send_ip_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500287 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500288 }
289
Neale Rannsa3af3372017-03-28 03:49:52 -0700290 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500291}
292
293static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500294send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800295 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800296 const fib_table_t * table,
297 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500298 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500299{
300 vl_api_ip6_fib_details_t *mp;
301 fib_route_path_encode_t *api_rpath;
302 vl_api_fib_path_t *fp;
303 int path_count;
304
Dave Barachd7cb1b52016-12-09 09:52:16 -0500305 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500306 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
307 if (!mp)
308 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400309 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500310 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
311 mp->context = context;
312
Neale Rannsa161a6d2017-11-14 08:10:41 -0800313 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500314 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500315 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800316 memcpy (mp->table_name, table->ft_desc,
317 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Neale Ranns008dbe12018-09-07 09:32:36 -0700318 mp->stats_index =
319 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500320
321 mp->count = htonl (path_count);
322 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500324 {
Neale Ranns81458422018-03-12 06:59:36 -0700325 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500326 fp++;
327 }
328
Florin Coras6c4dae22018-01-09 06:39:23 -0800329 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500330}
331
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332typedef struct apt_ip6_fib_show_ctx_t_
333{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500334 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500335} api_ip6_fib_show_ctx_t;
336
Neale Ranns5c1f5592019-01-25 00:51:09 -0800337static fib_table_walk_rc_t
338api_ip6_fib_table_put_entries (fib_node_index_t fei, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500339{
340 api_ip6_fib_show_ctx_t *ctx = arg;
341
Neale Ranns5c1f5592019-01-25 00:51:09 -0800342 vec_add1 (ctx->entries, fei);
343
344 return (FIB_TABLE_WALK_CONTINUE);
Dave Barachb5e8a772016-12-06 12:04:42 -0500345}
346
347static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800348api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500349 vl_api_ip6_fib_dump_t * mp,
350 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500351{
352 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500353 fib_node_index_t *fib_entry_index;
354 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700355 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500356 };
357 fib_route_path_encode_t *api_rpaths;
Neale Rannsc5d43172018-07-30 08:04:40 -0700358 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500359
Neale Ranns5c1f5592019-01-25 00:51:09 -0800360 ip6_fib_table_walk (fib_table->ft_index,
361 api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500362
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500364
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 vec_foreach (fib_entry_index, ctx.entries)
366 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700367 pfx = fib_entry_get_prefix (*fib_entry_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 api_rpaths = NULL;
369 fib_entry_encode (*fib_entry_index, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700370 send_ip6_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371 vec_free (api_rpaths);
372 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500373
Dave Barachd7cb1b52016-12-09 09:52:16 -0500374 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500375}
376
377static void
378vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
379{
Florin Coras6c4dae22018-01-09 06:39:23 -0800380 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500381 ip6_main_t *im6 = &ip6_main;
382 fib_table_t *fib_table;
383
Florin Coras6c4dae22018-01-09 06:39:23 -0800384 reg = vl_api_client_index_to_registration (mp->client_index);
385 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500386 return;
387
388 /* *INDENT-OFF* */
389 pool_foreach (fib_table, im6->fibs,
390 ({
Neale Ranns81458422018-03-12 06:59:36 -0700391 /* don't send link locals */
392 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
393 continue;
394
Florin Coras6c4dae22018-01-09 06:39:23 -0800395 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500396 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500398}
399
400static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800401send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800402 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800403{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800404 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800405 vl_api_ip_mfib_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -0800406 const mfib_prefix_t *pfx;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800407 mfib_entry_t *mfib_entry;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800408 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800409 int path_count;
410
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800411 mfib_entry = mfib_entry_get (mfei);
Neale Ranns9e829a82018-12-17 05:50:32 -0800412 pfx = mfib_entry_get_prefix (mfei);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800413 mfib_entry_encode (mfei, &api_rpaths);
414
Neale Ranns5a8123b2017-01-26 01:18:23 -0800415 path_count = vec_len (api_rpaths);
416 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
417 if (!mp)
418 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400419 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700420 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800421 mp->context = context;
422
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800423 mp->rpf_id = mfib_entry->mfe_rpf_id;
424 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800425 mp->table_id = htonl (table_id);
Neale Ranns9e829a82018-12-17 05:50:32 -0800426 mp->address_length = pfx->fp_len;
427 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip4,
428 sizeof (pfx->fp_grp_addr.ip4));
429 memcpy (mp->src_address, &pfx->fp_src_addr.ip4,
430 sizeof (pfx->fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800431
432 mp->count = htonl (path_count);
433 fp = mp->path;
434 vec_foreach (api_rpath, api_rpaths)
435 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800436 fib_api_path_encode (api_rpath, &fp->path);
437 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800438 fp++;
439 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800440 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800441
Florin Coras6c4dae22018-01-09 06:39:23 -0800442 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800443}
444
445typedef struct vl_api_ip_mfib_dump_ctc_t_
446{
447 fib_node_index_t *entries;
448} vl_api_ip_mfib_dump_ctc_t;
449
450static int
451vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
452{
453 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
454
455 vec_add1 (ctx->entries, fei);
456
457 return (0);
458}
459
460static void
461vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
462{
Florin Coras6c4dae22018-01-09 06:39:23 -0800463 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800464 ip4_main_t *im = &ip4_main;
465 mfib_table_t *mfib_table;
466 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800467 vl_api_ip_mfib_dump_ctc_t ctx = {
468 .entries = NULL,
469 };
470
Florin Coras6c4dae22018-01-09 06:39:23 -0800471 reg = vl_api_client_index_to_registration (mp->client_index);
472 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800473 return;
474
Neale Ranns5a8123b2017-01-26 01:18:23 -0800475 /* *INDENT-OFF* */
476 pool_foreach (mfib_table, im->mfibs,
477 ({
478 ip4_mfib_table_walk(&mfib_table->v4,
479 vl_api_ip_mfib_table_dump_walk,
480 &ctx);
481
482 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
483
484 vec_foreach (mfeip, ctx.entries)
485 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800486 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800487 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800488 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800489 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800490 vec_reset_length (ctx.entries);
491
492 }));
493 /* *INDENT-ON* */
494
495 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800496}
497
498static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800499send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800500 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800501 u32 table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800502 const mfib_prefix_t * pfx,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800503 fib_route_path_encode_t * api_rpaths, u32 context)
504{
505 vl_api_ip6_mfib_details_t *mp;
506 fib_route_path_encode_t *api_rpath;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800507 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800508 int path_count;
509
510 path_count = vec_len (api_rpaths);
511 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
512 if (!mp)
513 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400514 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700515 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800516 mp->context = context;
517
518 mp->table_id = htonl (table_id);
519 mp->address_length = pfx->fp_len;
520 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
521 sizeof (pfx->fp_grp_addr.ip6));
522 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
523 sizeof (pfx->fp_src_addr.ip6));
524
525 mp->count = htonl (path_count);
526 fp = mp->path;
527 vec_foreach (api_rpath, api_rpaths)
528 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800529 fib_api_path_encode (api_rpath, &fp->path);
530 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800531 fp++;
532 }
533
Florin Coras6c4dae22018-01-09 06:39:23 -0800534 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800535}
536
537typedef struct vl_api_ip6_mfib_dump_ctc_t_
538{
539 fib_node_index_t *entries;
540} vl_api_ip6_mfib_dump_ctc_t;
541
542static int
543vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
544{
545 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
546
547 vec_add1 (ctx->entries, fei);
548
549 return (0);
550}
551
552static void
553vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
554{
555 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800556 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800557 ip6_main_t *im = &ip6_main;
558 mfib_table_t *mfib_table;
Neale Ranns9e829a82018-12-17 05:50:32 -0800559 const mfib_prefix_t *pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800560 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800561 fib_route_path_encode_t *api_rpaths = NULL;
562 vl_api_ip6_mfib_dump_ctc_t ctx = {
563 .entries = NULL,
564 };
565
Florin Coras6c4dae22018-01-09 06:39:23 -0800566 reg = vl_api_client_index_to_registration (mp->client_index);
567 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800568 return;
569
570
571 /* *INDENT-OFF* */
572 pool_foreach (mfib_table, im->mfibs,
573 ({
574 ip6_mfib_table_walk(&mfib_table->v6,
575 vl_api_ip6_mfib_table_dump_walk,
576 &ctx);
577
578 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
579
580 vec_foreach(mfeip, ctx.entries)
581 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800582 pfx = mfib_entry_get_prefix (*mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800583 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800584 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800585 mfib_table->mft_table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800586 pfx, api_rpaths,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800587 mp->context);
588 }
589 vec_reset_length (api_rpaths);
590 vec_reset_length (ctx.entries);
591
592 }));
593 /* *INDENT-ON* */
594
595 vec_free (ctx.entries);
596 vec_free (api_rpaths);
597}
598
599static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700600vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
601 vlib_main_t * vm)
602{
603 vl_api_ip_punt_police_reply_t *rmp;
604 int rv = 0;
605
606 if (mp->is_ip6)
607 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
608 else
609 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
610
611 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
612}
613
614static void
615vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
616 vlib_main_t * vm)
617{
618 vl_api_ip_punt_redirect_reply_t *rmp;
619 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100620 ip46_type_t ipv;
621 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700622
Pavel Kotucek609e1212018-11-27 09:59:44 +0100623 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
624 goto bad_sw_if_index;
625
626 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700627 if (mp->is_add)
628 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100629 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700630 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100631 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
632 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700633 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100634 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700635 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100636 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
637 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700638 }
639 }
640 else
641 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100642 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700643 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100644 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700645 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100646 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700647 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100648 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700649 }
650 }
651
Pavel Kotucek609e1212018-11-27 09:59:44 +0100652 BAD_SW_IF_INDEX_LABEL;
653
Neale Rannsd91c1db2017-07-31 02:30:50 -0700654 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
655}
656
657static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500658vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
659 vlib_main_t * vm)
660{
Neale Ranns0bdd3192018-09-07 11:04:52 -0700661 ip46_address_t ip = ip46_address_initializer;
Dave Barachb5e8a772016-12-06 12:04:42 -0500662 vl_api_ip_neighbor_add_del_reply_t *rmp;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700663 ip_neighbor_flags_t flags;
Neale Ranns14260392018-09-28 05:00:57 -0700664 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500665 int rv = 0;
666
667 VALIDATE_SW_IF_INDEX (mp);
668
669 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
670
Neale Ranns0bdd3192018-09-07 11:04:52 -0700671 flags = IP_NEIGHBOR_FLAG_NODE;
672 if (mp->is_static)
673 flags |= IP_NEIGHBOR_FLAG_STATIC;
674 if (mp->is_no_adj_fib)
675 flags |= IP_NEIGHBOR_FLAG_NO_ADJ_FIB;
676
Dave Barachb5e8a772016-12-06 12:04:42 -0500677 if (mp->is_ipv6)
Neale Ranns0bdd3192018-09-07 11:04:52 -0700678 clib_memcpy (&ip.ip6, mp->dst_address, 16);
Dave Barachb5e8a772016-12-06 12:04:42 -0500679 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700680 clib_memcpy (&ip.ip4, mp->dst_address, 4);
Dave Barachb5e8a772016-12-06 12:04:42 -0500681
Neale Ranns0bdd3192018-09-07 11:04:52 -0700682 if (mp->is_add)
683 rv = ip_neighbor_add (&ip, mp->is_ipv6, mp->mac_address,
Neale Ranns14260392018-09-28 05:00:57 -0700684 ntohl (mp->sw_if_index), flags, &stats_index);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700685 else
686 rv = ip_neighbor_del (&ip, mp->is_ipv6, ntohl (mp->sw_if_index));
Dave Barachb5e8a772016-12-06 12:04:42 -0500687
Dave Barachb5e8a772016-12-06 12:04:42 -0500688 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700689
690 BAD_SW_IF_INDEX_LABEL;
Neale Ranns14260392018-09-28 05:00:57 -0700691
692 /* *INDENT-OFF* */
693 REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
694 ({
695 rmp->stats_index = htonl (stats_index);
696 }));
697 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500698}
699
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700700void
Neale Ranns15002542017-09-10 04:39:11 -0700701ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
702{
703 u32 fib_index, mfib_index;
704
705 /*
706 * ignore action on the default table - this is always present
707 * and cannot be added nor deleted from the API
708 */
709 if (0 != table_id)
710 {
711 /*
712 * The API holds only one lock on the table.
713 * i.e. it can be added many times via the API but needs to be
714 * deleted only once.
715 * The FIB index for unicast and multicast is not necessarily the
716 * same, since internal VPP systesm (like LISP and SR) create
717 * their own unicast tables.
718 */
719 fib_index = fib_table_find (fproto, table_id);
720 mfib_index = mfib_table_find (fproto, table_id);
721
722 if (~0 != fib_index)
723 {
724 fib_table_unlock (fib_index, fproto,
725 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
726 }
727 if (~0 != mfib_index)
728 {
729 mfib_table_unlock (mfib_index, fproto,
730 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
731 }
732 }
733}
734
735void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700736vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
737{
738 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700739 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
740 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700741 int rv = 0;
742
Neale Ranns15002542017-09-10 04:39:11 -0700743 if (mp->is_add)
744 {
Neale Ranns2297af02017-09-12 09:45:04 -0700745 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700746 }
747 else
748 {
749 ip_table_delete (fproto, table_id, 1);
750 }
751
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700752 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
753}
754
Dave Barachb5e8a772016-12-06 12:04:42 -0500755int
756add_del_route_t_handler (u8 is_multipath,
757 u8 is_add,
758 u8 is_drop,
759 u8 is_unreach,
760 u8 is_prohibit,
761 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800762 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500763 u8 is_classify,
764 u32 classify_table_index,
765 u8 is_resolve_host,
766 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800767 u8 is_interface_rx,
768 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800769 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700770 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800771 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500772 u32 fib_index,
773 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700774 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500775 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800776 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500777 u32 next_hop_sw_if_index,
778 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700779 u16 next_hop_weight,
780 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500781 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800782 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500783{
784 vnet_classify_main_t *cm = &vnet_classify_main;
785 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
786 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700787 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500788 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
789 .frp_sw_if_index = next_hop_sw_if_index,
790 .frp_fib_index = next_hop_fib_index,
791 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700792 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500793 .frp_label_stack = next_hop_out_label_stack,
794 };
795 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800796 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500797
Neale Rannscaac3502017-09-18 18:04:10 -0700798 /*
799 * the special INVALID label meams we are not recursing via a
800 * label. Exp-null value is never a valid via-label so that
801 * also means it's not a via-label and means clients that set
802 * it to 0 by default get the expected behaviour
803 */
804 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500805 {
Neale Rannsda78f952017-05-24 09:15:43 -0700806 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500807 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800808 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500809 }
Neale Ranns775f73c2018-12-20 03:01:49 -0800810 if (is_local)
Neale Ranns7425f922019-01-23 00:36:16 -0800811 {
812 path_flags |= FIB_ROUTE_PATH_LOCAL;
813 if (~0 != next_hop_sw_if_index)
814 {
815 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
816 }
817 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800818 if (is_dvr)
819 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500820 if (is_resolve_host)
821 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
822 if (is_resolve_attached)
823 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800824 if (is_interface_rx)
825 path_flags |= FIB_ROUTE_PATH_INTF_RX;
826 if (is_rpf_id)
827 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700828 if (is_source_lookup)
829 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800830 if (is_multicast)
831 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800832 if (is_udp_encap)
833 {
834 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
835 path.frp_udp_encap_id = next_hop_id;
836 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800837 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
838 && path.frp_fib_index != ~0)
839 {
840 path_flags |= FIB_ROUTE_PATH_DEAG;
841 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500842
843 path.frp_flags = path_flags;
844
Dave Barachb5e8a772016-12-06 12:04:42 -0500845 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
846
Neale Ranns7425f922019-01-23 00:36:16 -0800847 if (is_drop || (is_local && (~0 == next_hop_sw_if_index)) ||
848 is_classify || is_unreach || is_prohibit)
Dave Barachb5e8a772016-12-06 12:04:42 -0500849 {
850 /*
851 * special route types that link directly to the adj
852 */
853 if (is_add)
854 {
855 dpo_id_t dpo = DPO_INVALID;
856 dpo_proto_t dproto;
857
858 dproto = fib_proto_to_dpo (prefix->fp_proto);
859
860 if (is_drop)
861 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
862 else if (is_local)
863 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
864 else if (is_unreach)
865 ip_null_dpo_add_and_lock (dproto,
866 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
867 else if (is_prohibit)
868 ip_null_dpo_add_and_lock (dproto,
869 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
870 &dpo);
871 else if (is_classify)
872 {
873 if (pool_is_free_index (cm->tables,
874 ntohl (classify_table_index)))
875 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500876 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500877 return VNET_API_ERROR_NO_SUCH_TABLE;
878 }
879
880 dpo_set (&dpo, DPO_CLASSIFY, dproto,
881 classify_dpo_create (dproto,
882 ntohl (classify_table_index)));
883 }
884 else
885 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500886 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500887 return VNET_API_ERROR_NO_SUCH_TABLE;
888 }
889
890 fib_table_entry_special_dpo_update (fib_index,
891 prefix,
892 FIB_SOURCE_API,
893 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
894 dpo_reset (&dpo);
895 }
896 else
897 {
898 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
899 }
900 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700901 else if (is_multipath)
902 {
903 vec_add1 (paths, path);
904
905 if (is_add)
906 fib_table_entry_path_add2 (fib_index,
907 prefix,
908 FIB_SOURCE_API, entry_flags, paths);
909 else
910 fib_table_entry_path_remove2 (fib_index,
911 prefix, FIB_SOURCE_API, paths);
912
913 vec_free (paths);
914 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500915 else
916 {
917 if (is_add)
918 {
919 vec_add1 (paths, path);
920 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800921 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500922 vec_free (paths);
923 }
924 else
925 {
926 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
927 }
928 }
929
Dave Barachd7cb1b52016-12-09 09:52:16 -0500930 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500931 return (0);
932}
933
934int
935add_del_route_check (fib_protocol_t table_proto,
936 u32 table_id,
937 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700938 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500939 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800940 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500941{
942 vnet_main_t *vnm = vnet_get_main ();
943
944 *fib_index = fib_table_find (table_proto, ntohl (table_id));
945 if (~0 == *fib_index)
946 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800947 /* No such VRF, and we weren't asked to create one */
948 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500949 }
950
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800951 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500952 {
953 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
954 ntohl (next_hop_sw_if_index)))
955 {
956 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
957 }
958 }
959 else
960 {
Neale Rannsda78f952017-05-24 09:15:43 -0700961 fib_protocol_t fib_nh_proto;
962
963 if (next_hop_table_proto > DPO_PROTO_MPLS)
964 return (0);
965
966 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
967
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800968 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700969 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800970 ntohl (next_hop_table_id));
971 else
Neale Rannsda78f952017-05-24 09:15:43 -0700972 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800973 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500974
975 if (~0 == *next_hop_fib_index)
976 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800977 /* No such VRF, and we weren't asked to create one */
978 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500979 }
980 }
981
982 return (0);
983}
984
985static int
Neale Ranns008dbe12018-09-07 09:32:36 -0700986ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
987 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500988{
989 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800990 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500991 int rv, ii, n_labels;;
992
993 rv = add_del_route_check (FIB_PROTOCOL_IP4,
994 mp->table_id,
995 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700996 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500997 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700998 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500999
1000 if (0 != rv)
1001 return (rv);
1002
1003 fib_prefix_t pfx = {
1004 .fp_len = mp->dst_address_length,
1005 .fp_proto = FIB_PROTOCOL_IP4,
1006 };
1007 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1008
1009 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001010 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001011 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1012
1013 n_labels = mp->next_hop_n_out_labels;
1014 if (n_labels == 0)
1015 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001016 else
1017 {
1018 vec_validate (label_stack, n_labels - 1);
1019 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001020 {
1021 label_stack[ii].fml_value =
1022 ntohl (mp->next_hop_out_label_stack[ii].label);
1023 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1024 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1025 label_stack[ii].fml_mode =
1026 (mp->next_hop_out_label_stack[ii].is_uniform ?
1027 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1028 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001029 }
1030
Neale Ranns008dbe12018-09-07 09:32:36 -07001031 rv = add_del_route_t_handler (mp->is_multipath,
1032 mp->is_add,
1033 mp->is_drop,
1034 mp->is_unreach,
1035 mp->is_prohibit,
1036 mp->is_local, 0,
1037 mp->is_classify,
1038 mp->classify_table_index,
1039 mp->is_resolve_host,
1040 mp->is_resolve_attached, 0, 0,
1041 mp->is_dvr,
1042 mp->is_source_lookup,
1043 mp->is_udp_encap,
1044 fib_index, &pfx, DPO_PROTO_IP4,
1045 &nh,
1046 ntohl (mp->next_hop_id),
1047 ntohl (mp->next_hop_sw_if_index),
1048 next_hop_fib_index,
1049 mp->next_hop_weight,
1050 mp->next_hop_preference,
1051 ntohl (mp->next_hop_via_label), label_stack);
1052
1053 if (mp->is_add && 0 == rv)
1054 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1055
1056 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001057}
1058
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001059static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001060ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1061 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001062{
Neale Ranns31ed7442018-02-23 05:29:09 -08001063 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001064 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001065 int rv, ii, n_labels;;
1066
1067 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1068 mp->table_id,
1069 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001070 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001071 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001072 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001073
1074 if (0 != rv)
1075 return (rv);
1076
1077 fib_prefix_t pfx = {
1078 .fp_len = mp->dst_address_length,
1079 .fp_proto = FIB_PROTOCOL_IP6,
1080 };
1081 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1082
1083 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001084 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001085 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1086
1087 n_labels = mp->next_hop_n_out_labels;
1088 if (n_labels == 0)
1089 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001090 else
1091 {
1092 vec_validate (label_stack, n_labels - 1);
1093 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001094 {
1095 label_stack[ii].fml_value =
1096 ntohl (mp->next_hop_out_label_stack[ii].label);
1097 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1098 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1099 label_stack[ii].fml_mode =
1100 (mp->next_hop_out_label_stack[ii].is_uniform ?
1101 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1102 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001103 }
1104
Neale Ranns008dbe12018-09-07 09:32:36 -07001105 rv = add_del_route_t_handler (mp->is_multipath,
1106 mp->is_add,
1107 mp->is_drop,
1108 mp->is_unreach,
1109 mp->is_prohibit,
1110 mp->is_local, 0,
1111 mp->is_classify,
1112 mp->classify_table_index,
1113 mp->is_resolve_host,
1114 mp->is_resolve_attached, 0, 0,
1115 mp->is_dvr,
1116 mp->is_source_lookup,
1117 mp->is_udp_encap,
1118 fib_index, &pfx, DPO_PROTO_IP6,
1119 &nh, ntohl (mp->next_hop_id),
1120 ntohl (mp->next_hop_sw_if_index),
1121 next_hop_fib_index,
1122 mp->next_hop_weight,
1123 mp->next_hop_preference,
1124 ntohl (mp->next_hop_via_label), label_stack);
1125
1126 if (mp->is_add && 0 == rv)
1127 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1128
1129 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001130}
1131
1132void
1133vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1134{
1135 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001136 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001137 int rv;
1138 vnet_main_t *vnm = vnet_get_main ();
1139
1140 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001141 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001142
1143 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001144 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001145 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001146 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001147
1148 rv = (rv == 0) ? vnm->api_errno : rv;
1149
Neale Ranns008dbe12018-09-07 09:32:36 -07001150 /* *INDENT-OFF* */
1151 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1152 ({
1153 rmp->stats_index = htonl (stats_index);
1154 }))
1155 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001156}
1157
Neale Ranns15002542017-09-10 04:39:11 -07001158void
Neale Ranns2297af02017-09-12 09:45:04 -07001159ip_table_create (fib_protocol_t fproto,
1160 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001161{
1162 u32 fib_index, mfib_index;
1163
1164 /*
1165 * ignore action on the default table - this is always present
1166 * and cannot be added nor deleted from the API
1167 */
1168 if (0 != table_id)
1169 {
1170 /*
1171 * The API holds only one lock on the table.
1172 * i.e. it can be added many times via the API but needs to be
1173 * deleted only once.
1174 * The FIB index for unicast and multicast is not necessarily the
1175 * same, since internal VPP systesm (like LISP and SR) create
1176 * their own unicast tables.
1177 */
1178 fib_index = fib_table_find (fproto, table_id);
1179 mfib_index = mfib_table_find (fproto, table_id);
1180
1181 if (~0 == fib_index)
1182 {
Neale Ranns2297af02017-09-12 09:45:04 -07001183 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1184 (is_api ?
1185 FIB_SOURCE_API :
1186 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001187 }
1188 if (~0 == mfib_index)
1189 {
Neale Ranns2297af02017-09-12 09:45:04 -07001190 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1191 (is_api ?
1192 MFIB_SOURCE_API :
1193 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001194 }
1195 }
1196}
1197
Neale Ranns32e1c012016-11-22 17:07:28 +00001198static int
1199add_del_mroute_check (fib_protocol_t table_proto,
1200 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001201 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001202{
1203 vnet_main_t *vnm = vnet_get_main ();
1204
1205 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1206 if (~0 == *fib_index)
1207 {
Neale Ranns15002542017-09-10 04:39:11 -07001208 /* No such table */
1209 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001210 }
1211
1212 if (~0 != ntohl (next_hop_sw_if_index))
1213 {
1214 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1215 ntohl (next_hop_sw_if_index)))
1216 {
1217 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1218 }
1219 }
1220
1221 return (0);
1222}
1223
Neale Ranns28c142e2018-09-07 09:37:07 -07001224static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001225mroute_add_del_handler (u8 is_add,
1226 u8 is_local,
1227 u32 fib_index,
1228 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001229 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001230 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001231 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001232 u32 next_hop_sw_if_index,
1233 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001234{
Neale Ranns28c142e2018-09-07 09:37:07 -07001235 fib_node_index_t mfib_entry_index = ~0;
1236
Neale Ranns32e1c012016-11-22 17:07:28 +00001237 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1238
1239 fib_route_path_t path = {
1240 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001241 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001242 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001243 };
1244
1245 if (is_local)
1246 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1247
Neale Rannsd792d9c2017-10-21 10:53:20 -07001248 if (DPO_PROTO_BIER == nh_proto)
1249 {
1250 path.frp_bier_imp = bier_imp;
1251 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1252 }
1253 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001254 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001255 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1256 MFIB_SOURCE_API,
1257 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001258 goto done;
1259 }
1260
1261 if (is_add)
1262 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001263 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1264 MFIB_SOURCE_API,
1265 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001266 }
1267 else
1268 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001269 mfib_table_entry_path_remove (fib_index, prefix,
1270 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001271 }
1272
Neale Rannsd792d9c2017-10-21 10:53:20 -07001273done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001274 stats_dsunlock ();
Neale Ranns28c142e2018-09-07 09:37:07 -07001275 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001276}
1277
1278static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001279api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1280 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001281{
Neale Ranns28c142e2018-09-07 09:37:07 -07001282 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001283 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001284 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001285 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001286 u32 fib_index;
1287 int rv;
1288
Neale Rannsd792d9c2017-10-21 10:53:20 -07001289 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001290 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1291 rv = add_del_mroute_check (fproto,
1292 mp->table_id,
1293 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001294 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001295
1296 if (0 != rv)
1297 return (rv);
1298
1299 mfib_prefix_t pfx = {
1300 .fp_len = ntohs (mp->grp_address_length),
1301 .fp_proto = fproto,
1302 };
1303
1304 if (FIB_PROTOCOL_IP4 == fproto)
1305 {
1306 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1307 sizeof (pfx.fp_grp_addr.ip4));
1308 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1309 sizeof (pfx.fp_src_addr.ip4));
Dave Barachb7b92992018-10-17 10:38:51 -04001310 clib_memset (&nh.ip6, 0, sizeof (nh.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001311 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001312 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1313 pfx.fp_len = 64;
Neale Ranns32e1c012016-11-22 17:07:28 +00001314 }
1315 else
1316 {
1317 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1318 sizeof (pfx.fp_grp_addr.ip6));
1319 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1320 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001321 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001322 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1323 pfx.fp_len = 256;
Neale Ranns32e1c012016-11-22 17:07:28 +00001324 }
1325
Neale Ranns28c142e2018-09-07 09:37:07 -07001326 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1327 mp->is_local,
1328 fib_index, &pfx,
1329 nh_proto,
1330 ntohl (mp->entry_flags),
1331 ntohl (mp->rpf_id),
1332 ntohl (mp->next_hop_sw_if_index),
1333 &nh,
1334 ntohl (mp->itf_flags),
1335 ntohl (mp->bier_imp));
1336
1337 if (~0 != mfib_entry_index)
1338 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1339
1340 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001341}
1342
1343void
1344vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1345{
1346 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001347 vnet_main_t *vnm;
1348 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001349 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001350
Neale Ranns28c142e2018-09-07 09:37:07 -07001351 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001352 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001353 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001354
Neale Ranns28c142e2018-09-07 09:37:07 -07001355 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001356
Neale Ranns28c142e2018-09-07 09:37:07 -07001357 /* *INDENT-OFF* */
1358 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1359 ({
1360 rmp->stats_index = htonl (stats_index);
1361 }));
1362 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001363}
1364
Dave Barachb5e8a772016-12-06 12:04:42 -05001365static void
1366send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001367 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1368 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001369{
1370 vl_api_ip_details_t *mp;
1371
1372 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001373 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001374 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1375
1376 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001377 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001378 mp->context = context;
1379
Florin Coras6c4dae22018-01-09 06:39:23 -08001380 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001381}
1382
1383static void
1384send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001385 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001386 u8 * ip, u16 prefix_length,
1387 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001388{
1389 vl_api_ip_address_details_t *mp;
1390
1391 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001392 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001393 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1394
1395 if (is_ipv6)
1396 {
1397 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1398 }
1399 else
1400 {
1401 u32 *tp = (u32 *) mp->ip;
1402 *tp = *(u32 *) ip;
1403 }
1404 mp->prefix_length = prefix_length;
1405 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001406 mp->sw_if_index = htonl (sw_if_index);
1407 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001408
Florin Coras6c4dae22018-01-09 06:39:23 -08001409 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001410}
1411
1412static void
1413vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1414{
1415 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001416 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001417 ip6_address_t *r6;
1418 ip4_address_t *r4;
1419 ip6_main_t *im6 = &ip6_main;
1420 ip4_main_t *im4 = &ip4_main;
1421 ip_lookup_main_t *lm6 = &im6->lookup_main;
1422 ip_lookup_main_t *lm4 = &im4->lookup_main;
1423 ip_interface_address_t *ia = 0;
1424 u32 sw_if_index = ~0;
1425 int rv __attribute__ ((unused)) = 0;
1426
1427 VALIDATE_SW_IF_INDEX (mp);
1428
1429 sw_if_index = ntohl (mp->sw_if_index);
1430
Florin Coras6c4dae22018-01-09 06:39:23 -08001431 reg = vl_api_client_index_to_registration (mp->client_index);
1432 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001433 return;
1434
Dave Barachb5e8a772016-12-06 12:04:42 -05001435 if (mp->is_ipv6)
1436 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001437 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001438 /* Do not send subnet details of the IP-interface for
1439 * unnumbered interfaces. otherwise listening clients
1440 * will be confused that the subnet is applied on more
1441 * than one interface */
1442 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001443 ({
1444 r6 = ip_interface_address_get_address (lm6, ia);
1445 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001446 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001447 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001448 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001449 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001450 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001451 else
1452 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001453 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001454 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001455 ({
1456 r4 = ip_interface_address_get_address (lm4, ia);
1457 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001458 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001459 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001460 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001461 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001462 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001463
Dave Barachb5e8a772016-12-06 12:04:42 -05001464 BAD_SW_IF_INDEX_LABEL;
1465}
1466
1467static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001468send_ip_unnumbered_details (vpe_api_main_t * am,
1469 vl_api_registration_t * reg,
1470 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1471{
1472 vl_api_ip_unnumbered_details_t *mp;
1473
1474 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001475 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001476 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1477
1478 mp->context = context;
1479 mp->sw_if_index = htonl (sw_if_index);
1480 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1481
1482 vl_api_send_msg (reg, (u8 *) mp);
1483}
1484
1485static void
1486vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1487{
1488 vnet_main_t *vnm = vnet_get_main ();
1489 vnet_interface_main_t *im = &vnm->interface_main;
1490 int rv __attribute__ ((unused)) = 0;
1491 vpe_api_main_t *am = &vpe_api_main;
1492 vl_api_registration_t *reg;
1493 vnet_sw_interface_t *si;
1494 u32 sw_if_index;
1495
1496 sw_if_index = ntohl (mp->sw_if_index);
1497
1498 reg = vl_api_client_index_to_registration (mp->client_index);
1499 if (!reg)
1500 return;
1501
1502 if (~0 != sw_if_index)
1503 {
1504 VALIDATE_SW_IF_INDEX (mp);
1505
1506 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1507
1508 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1509 {
1510 send_ip_unnumbered_details (am, reg,
1511 sw_if_index,
1512 si->unnumbered_sw_if_index,
1513 mp->context);
1514 }
1515 }
1516 else
1517 {
1518 /* *INDENT-OFF* */
1519 pool_foreach (si, im->sw_interfaces,
1520 ({
1521 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1522 {
1523 send_ip_unnumbered_details(am, reg,
1524 si->sw_if_index,
1525 si->unnumbered_sw_if_index,
1526 mp->context);
1527 }
1528 }));
1529 /* *INDENT-ON* */
1530 }
1531
1532 BAD_SW_IF_INDEX_LABEL;
1533}
1534
1535static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001536vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1537{
1538 vpe_api_main_t *am = &vpe_api_main;
1539 vnet_main_t *vnm = vnet_get_main ();
1540 vlib_main_t *vm = vlib_get_main ();
1541 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001542 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001543 vnet_sw_interface_t *si, *sorted_sis;
1544 u32 sw_if_index = ~0;
1545
Florin Coras6c4dae22018-01-09 06:39:23 -08001546 reg = vl_api_client_index_to_registration (mp->client_index);
1547 if (!reg)
1548 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001549
1550 /* Gather interfaces. */
1551 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1552 _vec_len (sorted_sis) = 0;
1553 /* *INDENT-OFF* */
1554 pool_foreach (si, im->sw_interfaces,
1555 ({
1556 vec_add1 (sorted_sis, si[0]);
1557 }));
1558 /* *INDENT-ON* */
1559
1560 vec_foreach (si, sorted_sis)
1561 {
1562 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1563 {
1564 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1565 {
1566 continue;
1567 }
1568 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001569 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001570 }
1571 }
1572}
1573
1574static void
1575set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1576{
1577 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001578 int rv;
1579 u32 table_id;
1580 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001581
Neale Ranns227038a2017-04-21 01:07:59 -07001582 table_id = ntohl (mp->vrf_id);
1583
1584#define _(a,b) if (mp->a) flow_hash_config |= b;
1585 foreach_flow_hash_bit;
1586#undef _
1587
1588 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001589
1590 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1591}
1592
1593static void
1594set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1595{
1596 vl_api_set_ip_flow_hash_reply_t *rmp;
1597 int rv;
1598 u32 table_id;
1599 flow_hash_config_t flow_hash_config = 0;
1600
1601 table_id = ntohl (mp->vrf_id);
1602
1603#define _(a,b) if (mp->a) flow_hash_config |= b;
1604 foreach_flow_hash_bit;
1605#undef _
1606
1607 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1608
1609 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1610}
1611
1612
1613static void
1614vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1615{
1616 if (mp->is_ipv6 == 0)
1617 set_ip4_flow_hash (mp);
1618 else
1619 set_ip6_flow_hash (mp);
1620}
1621
1622static void
1623 vl_api_sw_interface_ip6nd_ra_config_t_handler
1624 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1625{
1626 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1627 vlib_main_t *vm = vlib_get_main ();
1628 int rv = 0;
1629 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1630 default_router;
1631
1632 is_no = mp->is_no == 1;
1633 suppress = mp->suppress == 1;
1634 managed = mp->managed == 1;
1635 other = mp->other == 1;
1636 ll_option = mp->ll_option == 1;
1637 send_unicast = mp->send_unicast == 1;
1638 cease = mp->cease == 1;
1639 default_router = mp->default_router == 1;
1640
1641 VALIDATE_SW_IF_INDEX (mp);
1642
1643 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1644 suppress, managed, other,
1645 ll_option, send_unicast, cease,
1646 default_router, ntohl (mp->lifetime),
1647 ntohl (mp->initial_count),
1648 ntohl (mp->initial_interval),
1649 ntohl (mp->max_interval),
1650 ntohl (mp->min_interval), is_no);
1651
1652 BAD_SW_IF_INDEX_LABEL;
1653
1654 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1655}
1656
1657static void
1658 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1659 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1660{
1661 vlib_main_t *vm = vlib_get_main ();
1662 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1663 int rv = 0;
1664 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1665
1666 VALIDATE_SW_IF_INDEX (mp);
1667
1668 is_no = mp->is_no == 1;
1669 use_default = mp->use_default == 1;
1670 no_advertise = mp->no_advertise == 1;
1671 off_link = mp->off_link == 1;
1672 no_autoconfig = mp->no_autoconfig == 1;
1673 no_onlink = mp->no_onlink == 1;
1674
1675 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1676 (ip6_address_t *) mp->address,
1677 mp->address_length, use_default,
1678 ntohl (mp->val_lifetime),
1679 ntohl (mp->pref_lifetime), no_advertise,
1680 off_link, no_autoconfig, no_onlink, is_no);
1681
1682 BAD_SW_IF_INDEX_LABEL;
1683 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1684}
1685
1686static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001687send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001688 u32 context,
1689 const ip46_address_t * addr, u32 sw_if_index)
1690{
1691 vl_api_ip6nd_proxy_details_t *mp;
1692
1693 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001694 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns3f844d02017-02-18 00:03:54 -08001695 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1696 mp->context = context;
1697 mp->sw_if_index = htonl (sw_if_index);
1698 memcpy (mp->address, addr, 16);
1699
Florin Coras6c4dae22018-01-09 06:39:23 -08001700 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001701}
1702
Neale Ranns3f844d02017-02-18 00:03:54 -08001703typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1704{
1705 u32 *indices;
1706} api_ip6nd_proxy_fib_table_walk_ctx_t;
1707
Neale Ranns89541992017-04-06 04:41:02 -07001708static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001709api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1710{
1711 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1712
1713 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1714 {
1715 vec_add1 (ctx->indices, fei);
1716 }
1717
Neale Ranns89541992017-04-06 04:41:02 -07001718 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001719}
1720
1721static void
1722vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1723{
1724 ip6_main_t *im6 = &ip6_main;
1725 fib_table_t *fib_table;
1726 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1727 .indices = NULL,
1728 };
1729 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001730 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001731 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001732
Florin Coras6c4dae22018-01-09 06:39:23 -08001733 reg = vl_api_client_index_to_registration (mp->client_index);
1734 if (!reg)
1735 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001736
1737 /* *INDENT-OFF* */
1738 pool_foreach (fib_table, im6->fibs,
1739 ({
1740 fib_table_walk(fib_table->ft_index,
1741 FIB_PROTOCOL_IP6,
1742 api_ip6nd_proxy_fib_table_walk,
1743 &ctx);
1744 }));
1745 /* *INDENT-ON* */
1746
1747 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1748
1749 vec_foreach (feip, ctx.indices)
1750 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001751 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001752
Florin Coras6c4dae22018-01-09 06:39:23 -08001753 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001754 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001755 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001756 fib_entry_get_resolving_interface (*feip));
1757 }
1758
1759 vec_free (ctx.indices);
1760}
1761
1762static void
1763vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1764{
1765 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1766 int rv = 0;
1767
1768 VALIDATE_SW_IF_INDEX (mp);
1769
1770 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1771 (ip6_address_t *) mp->address, mp->is_del);
1772
1773 BAD_SW_IF_INDEX_LABEL;
1774 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1775}
1776
1777static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001778 vl_api_ip6nd_send_router_solicitation_t_handler
1779 (vl_api_ip6nd_send_router_solicitation_t * mp)
1780{
1781 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1782 icmp6_send_router_solicitation_params_t params;
1783 vlib_main_t *vm = vlib_get_main ();
1784 int rv = 0;
1785
1786 VALIDATE_SW_IF_INDEX (mp);
1787
1788 BAD_SW_IF_INDEX_LABEL;
1789 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1790
1791 if (rv != 0)
1792 return;
1793
1794 params.irt = ntohl (mp->irt);
1795 params.mrt = ntohl (mp->mrt);
1796 params.mrc = ntohl (mp->mrc);
1797 params.mrd = ntohl (mp->mrd);
1798
1799 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1800 &params);
1801}
1802
1803static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001804 vl_api_sw_interface_ip6_enable_disable_t_handler
1805 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1806{
1807 vlib_main_t *vm = vlib_get_main ();
1808 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1809 vnet_main_t *vnm = vnet_get_main ();
1810 int rv = 0;
1811 clib_error_t *error;
1812
1813 vnm->api_errno = 0;
1814
1815 VALIDATE_SW_IF_INDEX (mp);
1816
1817 error =
1818 (mp->enable == 1) ? enable_ip6_interface (vm,
1819 ntohl (mp->sw_if_index)) :
1820 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1821
1822 if (error)
1823 {
1824 clib_error_report (error);
1825 rv = VNET_API_ERROR_UNSPECIFIED;
1826 }
1827 else
1828 {
1829 rv = vnm->api_errno;
1830 }
1831
1832 BAD_SW_IF_INDEX_LABEL;
1833
1834 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1835}
1836
Neale Ranns32e1c012016-11-22 17:07:28 +00001837void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001838vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001839 u32 context, const mfib_signal_t * mfs)
1840{
1841 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001842 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001843 mfib_table_t *mfib;
1844 mfib_itf_t *mfi;
1845
1846 mp = vl_msg_api_alloc (sizeof (*mp));
1847
Dave Barachb7b92992018-10-17 10:38:51 -04001848 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001849 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1850 mp->context = context;
1851
1852 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001853 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001854 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001855 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001856 mp->table_id = ntohl (mfib->mft_table_id);
1857 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1858
Neale Ranns9e829a82018-12-17 05:50:32 -08001859 if (FIB_PROTOCOL_IP4 == prefix->fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +00001860 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001861 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001862
Neale Ranns9e829a82018-12-17 05:50:32 -08001863 memcpy (mp->grp_address, &prefix->fp_grp_addr.ip4, 4);
1864 if (prefix->fp_len > 32)
Neale Ranns32e1c012016-11-22 17:07:28 +00001865 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001866 memcpy (mp->src_address, &prefix->fp_src_addr.ip4, 4);
Neale Ranns32e1c012016-11-22 17:07:28 +00001867 }
1868 }
1869 else
1870 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001871 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001872
1873 ASSERT (0);
1874 }
1875
1876 if (0 != mfs->mfs_buffer_len)
1877 {
1878 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1879
1880 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1881 }
1882 else
1883 {
1884 mp->ip_packet_len = 0;
1885 }
1886
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001887 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001888}
1889
1890static void
1891vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1892{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001893 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001894
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001895 reg = vl_api_client_index_to_registration (mp->client_index);
1896 if (!reg)
1897 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001898
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001899 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001900 ;
1901}
Dave Barachb5e8a772016-12-06 12:04:42 -05001902
Florin Coras595992c2017-11-06 17:17:08 -08001903static void
1904 vl_api_ip_container_proxy_add_del_t_handler
1905 (vl_api_ip_container_proxy_add_del_t * mp)
1906{
1907 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1908 vnet_ip_container_proxy_args_t args;
1909 int rv = 0;
1910 clib_error_t *error;
1911
Dave Barachb7b92992018-10-17 10:38:51 -04001912 clib_memset (&args, 0, sizeof (args));
Florin Coras595992c2017-11-06 17:17:08 -08001913 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1914 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1915 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1916 args.is_add = mp->is_add;
1917 if ((error = vnet_ip_container_proxy_add_del (&args)))
1918 {
1919 rv = clib_error_get_code (error);
1920 clib_error_report (error);
1921 }
1922
1923 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1924}
1925
Matus Fabian75b9f452018-10-02 23:27:21 -07001926typedef struct ip_container_proxy_walk_ctx_t_
1927{
1928 vl_api_registration_t *reg;
1929 u32 context;
1930} ip_container_proxy_walk_ctx_t;
1931
1932static int
1933ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1934 void *args)
1935{
1936 vl_api_ip_container_proxy_details_t *mp;
1937 ip_container_proxy_walk_ctx_t *ctx = args;
1938
1939 mp = vl_msg_api_alloc (sizeof (*mp));
1940 if (!mp)
1941 return 1;
1942
Dave Barachb7b92992018-10-17 10:38:51 -04001943 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001944 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1945 mp->context = ctx->context;
1946
1947 mp->sw_if_index = ntohl (sw_if_index);
1948 ip_prefix_encode (pfx, &mp->prefix);
1949
1950 vl_api_send_msg (ctx->reg, (u8 *) mp);
1951
1952 return 1;
1953}
1954
1955static void
1956vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1957 mp)
1958{
1959 vl_api_registration_t *reg;
1960
1961 reg = vl_api_client_index_to_registration (mp->client_index);
1962 if (!reg)
1963 return;
1964
1965 ip_container_proxy_walk_ctx_t ctx = {
1966 .context = mp->context,
1967 .reg = reg,
1968 };
1969
1970 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1971}
1972
Neale Rannsb8d44812017-11-10 06:53:54 -08001973static void
1974vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1975{
1976 int rv = 0;
1977 vl_api_ioam_enable_reply_t *rmp;
1978 clib_error_t *error;
1979
1980 /* Ignoring the profile id as currently a single profile
1981 * is supported */
1982 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1983 mp->seqno, mp->analyse);
1984 if (error)
1985 {
1986 clib_error_report (error);
1987 rv = clib_error_get_code (error);
1988 }
1989
1990 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1991}
1992
1993static void
1994vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1995{
1996 int rv = 0;
1997 vl_api_ioam_disable_reply_t *rmp;
1998 clib_error_t *error;
1999
2000 error = clear_ioam_rewrite_fn ();
2001 if (error)
2002 {
2003 clib_error_report (error);
2004 rv = clib_error_get_code (error);
2005 }
2006
2007 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2008}
2009
2010static void
2011 vl_api_ip_source_and_port_range_check_add_del_t_handler
2012 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2013{
2014 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2015 int rv = 0;
2016
2017 u8 is_ipv6 = mp->is_ipv6;
2018 u8 is_add = mp->is_add;
2019 u8 mask_length = mp->mask_length;
2020 ip4_address_t ip4_addr;
2021 ip6_address_t ip6_addr;
2022 u16 *low_ports = 0;
2023 u16 *high_ports = 0;
2024 u32 vrf_id;
2025 u16 tmp_low, tmp_high;
2026 u8 num_ranges;
2027 int i;
2028
2029 // Validate port range
2030 num_ranges = mp->number_of_ranges;
2031 if (num_ranges > 32)
2032 { // This is size of array in VPE.API
2033 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2034 goto reply;
2035 }
2036
2037 vec_reset_length (low_ports);
2038 vec_reset_length (high_ports);
2039
2040 for (i = 0; i < num_ranges; i++)
2041 {
2042 tmp_low = mp->low_ports[i];
2043 tmp_high = mp->high_ports[i];
2044 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2045 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2046 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2047 {
2048 rv = VNET_API_ERROR_INVALID_VALUE;
2049 goto reply;
2050 }
2051 vec_add1 (low_ports, tmp_low);
2052 vec_add1 (high_ports, tmp_high + 1);
2053 }
2054
2055 // Validate mask_length
2056 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2057 {
2058 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2059 goto reply;
2060 }
2061
2062 vrf_id = ntohl (mp->vrf_id);
2063
2064 if (vrf_id < 1)
2065 {
2066 rv = VNET_API_ERROR_INVALID_VALUE;
2067 goto reply;
2068 }
2069
2070
2071 if (is_ipv6)
2072 {
2073 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2074 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2075 mask_length,
2076 vrf_id,
2077 low_ports,
2078 high_ports, is_add);
2079 }
2080 else
2081 {
2082 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2083 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2084 mask_length,
2085 vrf_id,
2086 low_ports,
2087 high_ports, is_add);
2088 }
2089
2090reply:
2091 vec_free (low_ports);
2092 vec_free (high_ports);
2093 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2094}
2095
2096static void
2097 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2098 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2099{
2100 vlib_main_t *vm = vlib_get_main ();
2101 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2102 ip4_main_t *im = &ip4_main;
2103 int rv;
2104 u32 sw_if_index;
2105 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2106 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2107 uword *p = 0;
2108 int i;
2109
2110 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2111 ntohl (mp->tcp_out_vrf_id);
2112 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2113 ntohl (mp->udp_out_vrf_id);
2114 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2115 ntohl (mp->tcp_in_vrf_id);
2116 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2117 ntohl (mp->udp_in_vrf_id);
2118
2119
2120 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2121 {
2122 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2123 {
2124 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2125
2126 if (p == 0)
2127 {
2128 rv = VNET_API_ERROR_INVALID_VALUE;
2129 goto reply;
2130 }
2131
2132 fib_index[i] = p[0];
2133 }
2134 else
2135 fib_index[i] = ~0;
2136 }
2137 sw_if_index = ntohl (mp->sw_if_index);
2138
2139 VALIDATE_SW_IF_INDEX (mp);
2140
2141 rv =
2142 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2143 mp->is_add);
2144
2145 BAD_SW_IF_INDEX_LABEL;
2146reply:
2147
2148 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2149}
2150
Chore3460b012018-11-28 10:53:11 +03302151typedef union
2152{
2153 u32 fib_index;
2154} ip4_source_check_config_t;
2155
2156static void
2157 vl_api_ip_source_check_interface_add_del_t_handler
2158 (vl_api_ip_source_check_interface_add_del_t * mp)
2159{
2160 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
2161 int rv;
2162 u32 sw_if_index = ntohl (mp->sw_if_index);
2163 u8 is_add = mp->is_add;
2164 char *feature_name =
2165 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
2166
2167 ip4_source_check_config_t config;
2168
2169 VALIDATE_SW_IF_INDEX (mp);
2170
2171 config.fib_index =
2172 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
2173 rv =
2174 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
2175 is_add, &config, sizeof (config));
2176 BAD_SW_IF_INDEX_LABEL;
2177
2178 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
2179}
2180
Neale Rannsb8d44812017-11-10 06:53:54 -08002181#define IP4_ARP_EVENT 3
2182#define IP6_ND_EVENT 4
2183
2184static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2185static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2186static vlib_node_registration_t ip_resolver_process_node;
2187
2188static void
2189handle_ip4_arp_event (u32 pool_index)
2190{
2191 vpe_api_main_t *vam = &vpe_api_main;
2192 vnet_main_t *vnm = vam->vnet_main;
2193 vlib_main_t *vm = vam->vlib_main;
2194 vl_api_ip4_arp_event_t *event;
2195 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002196 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002197
2198 /* Client can cancel, die, etc. */
2199 if (pool_is_free_index (vam->arp_events, pool_index))
2200 return;
2201
2202 event = pool_elt_at_index (vam->arp_events, pool_index);
2203
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002204 reg = vl_api_client_index_to_registration (event->client_index);
2205 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002206 {
2207 (void) vnet_add_del_ip4_arp_change_event
2208 (vnm, arp_change_delete_callback,
2209 event->pid, &event->address,
2210 ip_resolver_process_node.index, IP4_ARP_EVENT,
2211 ~0 /* pool index, notused */ , 0 /* is_add */ );
2212 return;
2213 }
2214
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002215 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002216 {
2217 mp = vl_msg_api_alloc (sizeof (*mp));
2218 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002219 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002220 }
2221 else
2222 {
2223 static f64 last_time;
2224 /*
2225 * Throttle syslog msgs.
2226 * It's pretty tempting to just revoke the registration...
2227 */
2228 if (vlib_time_now (vm) > last_time + 10.0)
2229 {
2230 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2231 format_ip4_address, &event->address, event->pid);
2232 last_time = vlib_time_now (vm);
2233 }
2234 }
2235}
2236
Neale Rannsf12dad62018-06-04 18:41:24 -07002237static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002238handle_ip6_nd_event (u32 pool_index)
2239{
2240 vpe_api_main_t *vam = &vpe_api_main;
2241 vnet_main_t *vnm = vam->vnet_main;
2242 vlib_main_t *vm = vam->vlib_main;
2243 vl_api_ip6_nd_event_t *event;
2244 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002245 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002246
2247 /* Client can cancel, die, etc. */
2248 if (pool_is_free_index (vam->nd_events, pool_index))
2249 return;
2250
2251 event = pool_elt_at_index (vam->nd_events, pool_index);
2252
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002253 reg = vl_api_client_index_to_registration (event->client_index);
2254 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002255 {
2256 (void) vnet_add_del_ip6_nd_change_event
2257 (vnm, nd_change_delete_callback,
2258 event->pid, &event->address,
2259 ip_resolver_process_node.index, IP6_ND_EVENT,
2260 ~0 /* pool index, notused */ , 0 /* is_add */ );
2261 return;
2262 }
2263
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002264 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002265 {
2266 mp = vl_msg_api_alloc (sizeof (*mp));
2267 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002268 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002269 }
2270 else
2271 {
2272 static f64 last_time;
2273 /*
2274 * Throttle syslog msgs.
2275 * It's pretty tempting to just revoke the registration...
2276 */
2277 if (vlib_time_now (vm) > last_time + 10.0)
2278 {
2279 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2280 format_ip6_address, &event->address, event->pid);
2281 last_time = vlib_time_now (vm);
2282 }
2283 }
2284}
2285
2286static uword
2287resolver_process (vlib_main_t * vm,
2288 vlib_node_runtime_t * rt, vlib_frame_t * f)
2289{
2290 volatile f64 timeout = 100.0;
2291 volatile uword *event_data = 0;
2292
2293 while (1)
2294 {
2295 vlib_process_wait_for_event_or_clock (vm, timeout);
2296
2297 uword event_type =
2298 vlib_process_get_events (vm, (uword **) & event_data);
2299
2300 int i;
2301 switch (event_type)
2302 {
2303 case IP4_ARP_EVENT:
2304 for (i = 0; i < vec_len (event_data); i++)
2305 handle_ip4_arp_event (event_data[i]);
2306 break;
2307
2308 case IP6_ND_EVENT:
2309 for (i = 0; i < vec_len (event_data); i++)
2310 handle_ip6_nd_event (event_data[i]);
2311 break;
2312
2313 case ~0: /* timeout */
2314 break;
2315 }
2316
2317 vec_reset_length (event_data);
2318 }
2319 return 0; /* or not */
2320}
2321
2322/* *INDENT-OFF* */
2323VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2324 .function = resolver_process,
2325 .type = VLIB_NODE_TYPE_PROCESS,
2326 .name = "ip-route-resolver-process",
2327};
2328/* *INDENT-ON* */
2329
2330static int
2331nd_change_data_callback (u32 pool_index, u8 * new_mac,
2332 u32 sw_if_index, ip6_address_t * address)
2333{
2334 vpe_api_main_t *am = &vpe_api_main;
2335 vl_api_ip6_nd_event_t *event;
2336
2337 if (pool_is_free_index (am->nd_events, pool_index))
2338 return 1;
2339
2340 event = pool_elt_at_index (am->nd_events, pool_index);
2341 if (eth_mac_equal (event->new_mac, new_mac) &&
2342 sw_if_index == ntohl (event->sw_if_index))
2343 {
2344 return 1;
2345 }
2346
2347 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2348 event->sw_if_index = htonl (sw_if_index);
2349 return 0;
2350}
2351
2352static int
2353arp_change_delete_callback (u32 pool_index, u8 * notused)
2354{
2355 vpe_api_main_t *am = &vpe_api_main;
2356
2357 if (pool_is_free_index (am->arp_events, pool_index))
2358 return 1;
2359
2360 pool_put_index (am->arp_events, pool_index);
2361 return 0;
2362}
2363
2364static int
2365nd_change_delete_callback (u32 pool_index, u8 * notused)
2366{
2367 vpe_api_main_t *am = &vpe_api_main;
2368
2369 if (pool_is_free_index (am->nd_events, pool_index))
2370 return 1;
2371
2372 pool_put_index (am->nd_events, pool_index);
2373 return 0;
2374}
2375
2376static vlib_node_registration_t wc_arp_process_node;
2377
2378enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002379{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002380
2381static uword
2382wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2383{
2384 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2385 * and need to be volatile - to prevent them from being optimized into
2386 * a register - which could change during suspension */
2387
2388 volatile wc_arp_report_t arp_prev = { 0 };
2389 volatile wc_nd_report_t nd_prev = { 0 };
2390 volatile f64 last_arp = vlib_time_now (vm);
2391 volatile f64 last_nd = vlib_time_now (vm);
2392
2393 while (1)
2394 {
2395 vlib_process_wait_for_event (vm);
2396 uword event_type = WC_ARP_REPORT;
2397 void *event_data = vlib_process_get_event_data (vm, &event_type);
2398
2399 f64 now = vlib_time_now (vm);
2400 int i;
2401 if (event_type == WC_ARP_REPORT)
2402 {
2403 wc_arp_report_t *arp_events = event_data;
2404 for (i = 0; i < vec_len (arp_events); i++)
2405 {
2406 /* discard dup event */
2407 if (arp_prev.ip4 == arp_events[i].ip4 &&
2408 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2409 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2410 (now - last_arp) < 10.0)
2411 {
2412 continue;
2413 }
2414 arp_prev = arp_events[i];
2415 last_arp = now;
2416 vpe_client_registration_t *reg;
2417 /* *INDENT-OFF* */
2418 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2419 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002420 vl_api_registration_t *vl_reg;
2421 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002422 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002423 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002424 {
2425 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002426 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002427 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2428 event->client_index = reg->client_index;
2429 event->pid = reg->client_pid;
2430 event->mac_ip = 1;
2431 event->address = arp_events[i].ip4;
2432 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2433 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002434 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002435 }
2436 }));
2437 /* *INDENT-ON* */
2438 }
2439 }
2440 else if (event_type == WC_ND_REPORT)
2441 {
2442 wc_nd_report_t *nd_events = event_data;
2443 for (i = 0; i < vec_len (nd_events); i++)
2444 {
2445 /* discard dup event */
2446 if (ip6_address_is_equal
2447 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2448 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2449 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2450 && (now - last_nd) < 10.0)
2451 {
2452 continue;
2453 }
2454 nd_prev = nd_events[i];
2455 last_nd = now;
2456 vpe_client_registration_t *reg;
2457 /* *INDENT-OFF* */
2458 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2459 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002460 vl_api_registration_t *vl_reg;
2461 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2462 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002463 {
2464 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002465 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002466 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2467 event->client_index = reg->client_index;
2468 event->pid = reg->client_pid;
2469 event->mac_ip = 1;
2470 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2471 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2472 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002473 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002474 }
2475 }));
2476 /* *INDENT-ON* */
2477 }
2478 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002479 else if (event_type == RA_REPORT)
2480 {
2481 ra_report_t *ra_events = event_data;
2482 for (i = 0; i < vec_len (ra_events); i++)
2483 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002484 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2485 call_ip6_neighbor_callbacks (&ra_events[i],
2486 npm->ra_report_functions);
2487
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002488 vpe_client_registration_t *reg;
2489 /* *INDENT-OFF* */
2490 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2491 ({
2492 vl_api_registration_t *vl_reg;
2493 vl_reg =
2494 vl_api_client_index_to_registration (reg->client_index);
2495 if (vl_reg && vl_api_can_send_msg (vl_reg))
2496 {
2497 u32 event_size =
2498 sizeof (vl_api_ip6_ra_event_t) +
2499 vec_len (ra_events[i].prefixes) *
2500 sizeof (vl_api_ip6_ra_prefix_info_t);
2501 vl_api_ip6_ra_event_t *event =
2502 vl_msg_api_alloc (event_size);
Dave Barachb7b92992018-10-17 10:38:51 -04002503 clib_memset (event, 0, event_size);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002504 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2505 event->client_index = reg->client_index;
2506 event->pid = reg->client_pid;
2507
2508 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2509
2510 memcpy (event->router_address, ra_events[i].router_address, 16);
2511
2512 event->current_hop_limit = ra_events[i].current_hop_limit;
2513 event->flags = ra_events[i].flags;
2514 event->router_lifetime_in_sec =
2515 clib_host_to_net_u16 (ra_events
2516 [i].router_lifetime_in_sec);
2517 event->neighbor_reachable_time_in_msec =
2518 clib_host_to_net_u32 (ra_events
2519 [i].neighbor_reachable_time_in_msec);
2520 event->time_in_msec_between_retransmitted_neighbor_solicitations
2521 =
2522 clib_host_to_net_u32 (ra_events
2523 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2524
2525 event->n_prefixes =
2526 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2527 vl_api_ip6_ra_prefix_info_t *prefix =
2528 (typeof (prefix)) event->prefixes;
2529 u32 j;
2530 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2531 {
2532 ra_report_prefix_info_t *info =
2533 &ra_events[i].prefixes[j];
2534 memcpy (prefix->dst_address, info->dst_address.as_u8,
2535 16);
2536 prefix->dst_address_length = info->dst_address_length;
2537 prefix->flags = info->flags;
2538 prefix->valid_time =
2539 clib_host_to_net_u32 (info->valid_time);
2540 prefix->preferred_time =
2541 clib_host_to_net_u32 (info->preferred_time);
2542 prefix++;
2543 }
2544
2545 vl_api_send_msg (vl_reg, (u8 *) event);
2546 }
2547 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002548 /* *INDENT-ON* */
2549 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002550 }
2551 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002552 vlib_process_put_event_data (vm, event_data);
2553 }
2554
2555 return 0;
2556}
2557
2558/* *INDENT-OFF* */
2559VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2560 .function = wc_arp_process,
2561 .type = VLIB_NODE_TYPE_PROCESS,
2562 .name = "wildcard-ip4-arp-publisher-process",
2563};
2564/* *INDENT-ON* */
2565
2566static int
2567arp_change_data_callback (u32 pool_index, u8 * new_mac,
2568 u32 sw_if_index, u32 address)
2569{
2570 vpe_api_main_t *am = &vpe_api_main;
2571 vl_api_ip4_arp_event_t *event;
2572
2573 if (pool_is_free_index (am->arp_events, pool_index))
2574 return 1;
2575
2576 event = pool_elt_at_index (am->arp_events, pool_index);
2577 if (eth_mac_equal (event->new_mac, new_mac) &&
2578 sw_if_index == ntohl (event->sw_if_index))
2579 {
2580 return 1;
2581 }
2582
2583 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2584 event->sw_if_index = htonl (sw_if_index);
2585 return 0;
2586}
2587
2588static void
2589vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2590{
2591 vpe_api_main_t *am = &vpe_api_main;
2592 vnet_main_t *vnm = vnet_get_main ();
2593 vl_api_want_ip4_arp_events_reply_t *rmp;
2594 int rv = 0;
2595
2596 if (mp->address == 0)
2597 {
2598 uword *p =
2599 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2600 vpe_client_registration_t *rp;
2601 if (p)
2602 {
2603 if (mp->enable_disable)
2604 {
2605 clib_warning ("pid %d: already enabled...", mp->pid);
2606 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2607 goto reply;
2608 }
2609 else
2610 {
2611 rp =
2612 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2613 pool_put (am->wc_ip4_arp_events_registrations, rp);
2614 hash_unset (am->wc_ip4_arp_events_registration_hash,
2615 mp->client_index);
2616 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002617 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002618 goto reply;
2619 }
2620 }
2621 if (mp->enable_disable == 0)
2622 {
2623 clib_warning ("pid %d: already disabled...", mp->pid);
2624 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2625 goto reply;
2626 }
2627 pool_get (am->wc_ip4_arp_events_registrations, rp);
2628 rp->client_index = mp->client_index;
2629 rp->client_pid = mp->pid;
2630 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2631 rp - am->wc_ip4_arp_events_registrations);
2632 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2633 goto reply;
2634 }
2635
2636 if (mp->enable_disable)
2637 {
2638 vl_api_ip4_arp_event_t *event;
2639 pool_get (am->arp_events, event);
2640 rv = vnet_add_del_ip4_arp_change_event
2641 (vnm, arp_change_data_callback,
2642 mp->pid, &mp->address /* addr, in net byte order */ ,
2643 ip_resolver_process_node.index,
2644 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2645
2646 if (rv)
2647 {
2648 pool_put (am->arp_events, event);
2649 goto reply;
2650 }
Dave Barachb7b92992018-10-17 10:38:51 -04002651 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002652
2653 /* Python API expects events to have no context */
2654 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2655 event->client_index = mp->client_index;
2656 event->address = mp->address;
2657 event->pid = mp->pid;
2658 if (mp->address == 0)
2659 event->mac_ip = 1;
2660 }
2661 else
2662 {
2663 rv = vnet_add_del_ip4_arp_change_event
2664 (vnm, arp_change_delete_callback,
2665 mp->pid, &mp->address /* addr, in net byte order */ ,
2666 ip_resolver_process_node.index,
2667 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2668 }
2669reply:
2670 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2671}
2672
Neale Rannsf12dad62018-06-04 18:41:24 -07002673static clib_error_t *
2674want_ip4_arp_events_reaper (u32 client_index)
2675{
2676 vpe_client_registration_t *rp;
2677 vl_api_ip4_arp_event_t *event;
2678 u32 *to_delete, *event_id;
2679 vpe_api_main_t *am;
2680 vnet_main_t *vnm;
2681 uword *p;
2682
2683 am = &vpe_api_main;
2684 vnm = vnet_get_main ();
2685 to_delete = NULL;
2686
2687 /* clear out all of its pending resolutions */
2688 /* *INDENT-OFF* */
2689 pool_foreach(event, am->arp_events,
2690 ({
2691 if (event->client_index == client_index)
2692 {
2693 vec_add1(to_delete, event - am->arp_events);
2694 }
2695 }));
2696 /* *INDENT-ON* */
2697
2698 vec_foreach (event_id, to_delete)
2699 {
2700 event = pool_elt_at_index (am->arp_events, *event_id);
2701 vnet_add_del_ip4_arp_change_event
2702 (vnm, arp_change_delete_callback,
2703 event->pid, &event->address,
2704 ip_resolver_process_node.index, IP4_ARP_EVENT,
2705 ~0 /* pool index, notused */ , 0 /* is_add */ );
2706 }
2707 vec_free (to_delete);
2708
2709 /* remove from the registration hash */
2710 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2711
2712 if (p)
2713 {
2714 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2715 pool_put (am->wc_ip4_arp_events_registrations, rp);
2716 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2717 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2718 wc_arp_set_publisher_node (~0, REPORT_MAX);
2719 }
2720 return (NULL);
2721}
2722
2723VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2724
Neale Rannsb8d44812017-11-10 06:53:54 -08002725static void
2726vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2727{
2728 vpe_api_main_t *am = &vpe_api_main;
2729 vnet_main_t *vnm = vnet_get_main ();
2730 vl_api_want_ip6_nd_events_reply_t *rmp;
2731 int rv = 0;
2732
2733 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2734 {
2735 uword *p =
2736 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2737 vpe_client_registration_t *rp;
2738 if (p)
2739 {
2740 if (mp->enable_disable)
2741 {
2742 clib_warning ("pid %d: already enabled...", mp->pid);
2743 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2744 goto reply;
2745 }
2746 else
2747 {
2748 rp =
2749 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2750 pool_put (am->wc_ip6_nd_events_registrations, rp);
2751 hash_unset (am->wc_ip6_nd_events_registration_hash,
2752 mp->client_index);
2753 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002754 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002755 goto reply;
2756 }
2757 }
2758 if (mp->enable_disable == 0)
2759 {
2760 clib_warning ("pid %d: already disabled...", mp->pid);
2761 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2762 goto reply;
2763 }
2764 pool_get (am->wc_ip6_nd_events_registrations, rp);
2765 rp->client_index = mp->client_index;
2766 rp->client_pid = mp->pid;
2767 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2768 rp - am->wc_ip6_nd_events_registrations);
2769 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2770 goto reply;
2771 }
2772
2773 if (mp->enable_disable)
2774 {
2775 vl_api_ip6_nd_event_t *event;
2776 pool_get (am->nd_events, event);
2777
2778 rv = vnet_add_del_ip6_nd_change_event
2779 (vnm, nd_change_data_callback,
2780 mp->pid, mp->address /* addr, in net byte order */ ,
2781 ip_resolver_process_node.index,
2782 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2783
2784 if (rv)
2785 {
2786 pool_put (am->nd_events, event);
2787 goto reply;
2788 }
Dave Barachb7b92992018-10-17 10:38:51 -04002789 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002790
2791 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2792 event->client_index = mp->client_index;
2793 clib_memcpy (event->address, mp->address, sizeof event->address);
2794 event->pid = mp->pid;
2795 }
2796 else
2797 {
2798 rv = vnet_add_del_ip6_nd_change_event
2799 (vnm, nd_change_delete_callback,
2800 mp->pid, mp->address /* addr, in net byte order */ ,
2801 ip_resolver_process_node.index,
2802 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2803 }
2804reply:
2805 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2806}
2807
Neale Rannsf12dad62018-06-04 18:41:24 -07002808static clib_error_t *
2809want_ip6_nd_events_reaper (u32 client_index)
2810{
2811
2812 vpe_client_registration_t *rp;
2813 vl_api_ip6_nd_event_t *event;
2814 u32 *to_delete, *event_id;
2815 vpe_api_main_t *am;
2816 vnet_main_t *vnm;
2817 uword *p;
2818
2819 am = &vpe_api_main;
2820 vnm = vnet_get_main ();
2821 to_delete = NULL;
2822
2823 /* clear out all of its pending resolutions */
2824 /* *INDENT-OFF* */
2825 pool_foreach(event, am->nd_events,
2826 ({
2827 if (event->client_index == client_index)
2828 {
2829 vec_add1(to_delete, event - am->nd_events);
2830 }
2831 }));
2832 /* *INDENT-ON* */
2833
2834 vec_foreach (event_id, to_delete)
2835 {
2836 event = pool_elt_at_index (am->nd_events, *event_id);
2837 vnet_add_del_ip6_nd_change_event
2838 (vnm, nd_change_delete_callback,
2839 event->pid, &event->address,
2840 ip_resolver_process_node.index, IP6_ND_EVENT,
2841 ~0 /* pool index, notused */ , 0 /* is_add */ );
2842 }
2843 vec_free (to_delete);
2844
2845 /* remove from the registration hash */
2846 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2847
2848 if (p)
2849 {
2850 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2851 pool_put (am->wc_ip6_nd_events_registrations, rp);
2852 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2853 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2854 wc_nd_set_publisher_node (~0, REPORT_MAX);
2855 }
2856 return (NULL);
2857}
2858
2859VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2860
Neale Rannsb8d44812017-11-10 06:53:54 -08002861static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002862vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2863{
2864 vpe_api_main_t *am = &vpe_api_main;
2865 vl_api_want_ip6_ra_events_reply_t *rmp;
2866 int rv = 0;
2867
2868 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2869 vpe_client_registration_t *rp;
2870 if (p)
2871 {
2872 if (mp->enable_disable)
2873 {
2874 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2875 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2876 goto reply;
2877 }
2878 else
2879 {
2880 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2881 pool_put (am->ip6_ra_events_registrations, rp);
2882 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002883 goto reply;
2884 }
2885 }
2886 if (mp->enable_disable == 0)
2887 {
2888 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2889 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2890 goto reply;
2891 }
2892 pool_get (am->ip6_ra_events_registrations, rp);
2893 rp->client_index = mp->client_index;
2894 rp->client_pid = ntohl (mp->pid);
2895 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2896 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002897
2898reply:
2899 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2900}
2901
Neale Rannsf12dad62018-06-04 18:41:24 -07002902static clib_error_t *
2903want_ip6_ra_events_reaper (u32 client_index)
2904{
2905 vpe_api_main_t *am = &vpe_api_main;
2906 vpe_client_registration_t *rp;
2907 uword *p;
2908
2909 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2910
2911 if (p)
2912 {
2913 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2914 pool_put (am->ip6_ra_events_registrations, rp);
2915 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2916 }
2917 return (NULL);
2918}
2919
2920VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2921
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002922static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002923vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2924{
2925 vl_api_proxy_arp_add_del_reply_t *rmp;
2926 u32 fib_index;
2927 int rv;
2928 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002929 uword *p;
2930
Ole Troan3288ed72017-12-06 17:00:05 +01002931 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002932
Neale Ranns0053de62018-05-22 08:40:52 -07002933 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002934
2935 if (!p)
2936 {
2937 rv = VNET_API_ERROR_NO_SUCH_FIB;
2938 goto out;
2939 }
2940
2941 fib_index = p[0];
2942
Neale Ranns0053de62018-05-22 08:40:52 -07002943 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2944 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002945 fib_index, mp->is_add == 0);
2946
2947out:
Ole Troan3288ed72017-12-06 17:00:05 +01002948 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002949 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2950}
2951
Neale Ranns0053de62018-05-22 08:40:52 -07002952typedef struct proxy_arp_walk_ctx_t_
2953{
2954 vl_api_registration_t *reg;
2955 u32 context;
2956} proxy_arp_walk_ctx_t;
2957
2958static walk_rc_t
2959send_proxy_arp_details (const ip4_address_t * lo_addr,
2960 const ip4_address_t * hi_addr,
2961 u32 fib_index, void *data)
2962{
2963 vl_api_proxy_arp_details_t *mp;
2964 proxy_arp_walk_ctx_t *ctx;
2965
2966 ctx = data;
2967
2968 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04002969 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07002970 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2971 mp->context = ctx->context;
2972 mp->proxy.vrf_id = htonl (fib_index);
2973 clib_memcpy (mp->proxy.low_address, lo_addr,
2974 sizeof (mp->proxy.low_address));
2975 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2976
2977 vl_api_send_msg (ctx->reg, (u8 *) mp);
2978
2979 return (WALK_CONTINUE);
2980}
2981
2982static void
2983vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2984{
2985 vl_api_registration_t *reg;
2986
2987 reg = vl_api_client_index_to_registration (mp->client_index);
2988 if (!reg)
2989 return;
2990
2991 proxy_arp_walk_ctx_t wctx = {
2992 .reg = reg,
2993 .context = mp->context,
2994 };
2995
2996 proxy_arp_walk (send_proxy_arp_details, &wctx);
2997}
2998
2999static walk_rc_t
3000send_proxy_arp_intfc_details (vnet_main_t * vnm,
3001 vnet_sw_interface_t * si, void *data)
3002{
3003 vl_api_proxy_arp_intfc_details_t *mp;
3004 proxy_arp_walk_ctx_t *ctx;
3005
3006 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
3007 return (WALK_CONTINUE);
3008
3009 ctx = data;
3010
3011 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003012 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003013 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
3014 mp->context = ctx->context;
3015 mp->sw_if_index = htonl (si->sw_if_index);
3016
3017 vl_api_send_msg (ctx->reg, (u8 *) mp);
3018
3019 return (WALK_CONTINUE);
3020}
3021
3022static void
3023vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
3024{
3025 vl_api_registration_t *reg;
3026
3027 reg = vl_api_client_index_to_registration (mp->client_index);
3028 if (!reg)
3029 return;
3030
3031 proxy_arp_walk_ctx_t wctx = {
3032 .reg = reg,
3033 .context = mp->context,
3034 };
3035
3036 vnet_sw_interface_walk (vnet_get_main (),
3037 send_proxy_arp_intfc_details, &wctx);
3038}
3039
Neale Rannsb8d44812017-11-10 06:53:54 -08003040static void
3041 vl_api_proxy_arp_intfc_enable_disable_t_handler
3042 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
3043{
3044 int rv = 0;
3045 vnet_main_t *vnm = vnet_get_main ();
3046 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
3047
3048 VALIDATE_SW_IF_INDEX (mp);
3049
3050 vnet_sw_interface_t *si =
3051 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
3052
3053 ASSERT (si);
3054
3055 if (mp->enable_disable)
3056 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3057 else
3058 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3059
3060 BAD_SW_IF_INDEX_LABEL;
3061
3062 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3063}
3064
John Loc7b43042018-04-13 16:46:22 -04003065static void
3066vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3067{
3068 int rv = 0;
3069 vlib_main_t *vm = vlib_get_main ();
3070 vl_api_ip_probe_neighbor_reply_t *rmp;
3071 clib_error_t *error;
3072
3073 VALIDATE_SW_IF_INDEX (mp);
3074
3075 u32 sw_if_index = ntohl (mp->sw_if_index);
3076
3077 if (mp->is_ipv6)
3078 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003079 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003080 else
3081 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003082 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003083
3084 if (error)
3085 {
3086 clib_error_report (error);
3087 rv = clib_error_get_code (error);
3088 }
3089
3090 BAD_SW_IF_INDEX_LABEL;
3091
3092 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3093}
3094
John Lo7f358b32018-04-28 01:19:24 -04003095static void
3096 vl_api_ip_scan_neighbor_enable_disable_t_handler
3097 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3098{
3099 int rv = 0;
3100 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3101 ip_neighbor_scan_arg_t arg;
3102
3103 arg.mode = mp->mode;
3104 arg.scan_interval = mp->scan_interval;
3105 arg.max_proc_time = mp->max_proc_time;
3106 arg.max_update = mp->max_update;
3107 arg.scan_int_delay = mp->scan_int_delay;
3108 arg.stale_threshold = mp->stale_threshold;
3109 ip_neighbor_scan_enable_disable (&arg);
3110
3111 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3112}
3113
Neale Rannsb8d44812017-11-10 06:53:54 -08003114static int
3115ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3116{
3117 vnet_main_t *vnm = vnet_get_main ();
3118 vnet_interface_main_t *im = &vnm->interface_main;
3119 ip4_main_t *im4 = &ip4_main;
3120 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003121 fib_table_t *fib_table;
3122 ip4_fib_t *fib;
3123 u32 sw_if_index;
3124 int i;
3125 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3126 u32 target_fib_id = ntohl (mp->vrf_id);
3127
Ole Troan3288ed72017-12-06 17:00:05 +01003128 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003129
3130 /* *INDENT-OFF* */
3131 pool_foreach (fib_table, im4->fibs,
3132 ({
3133 vnet_sw_interface_t * si;
3134
3135 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3136
3137 if (fib->table_id != target_fib_id)
3138 continue;
3139
3140 /* remove any mpls encap/decap labels */
3141 mpls_fib_reset_labels (fib->table_id);
3142
3143 /* remove any proxy arps in this fib */
3144 vnet_proxy_arp_fib_reset (fib->table_id);
3145
3146 /* Set the flow hash for this fib to the default */
3147 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3148
3149 vec_reset_length (sw_if_indices_to_shut);
3150
3151 /* Shut down interfaces in this FIB / clean out intfc routes */
3152 pool_foreach (si, im->sw_interfaces,
3153 ({
3154 u32 sw_if_index = si->sw_if_index;
3155
3156 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3157 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3158 fib->index))
3159 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3160 }));
3161
3162 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3163 sw_if_index = sw_if_indices_to_shut[i];
3164
3165 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3166 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3167 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3168 }
3169
3170 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3171
3172 rv = 0;
3173 break;
3174 })); /* pool_foreach (fib) */
3175 /* *INDENT-ON* */
3176
Ole Troan3288ed72017-12-06 17:00:05 +01003177 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003178 return rv;
3179}
3180
3181static int
3182ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3183{
3184 vnet_main_t *vnm = vnet_get_main ();
3185 vnet_interface_main_t *im = &vnm->interface_main;
3186 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003187 static u32 *sw_if_indices_to_shut;
3188 fib_table_t *fib_table;
3189 ip6_fib_t *fib;
3190 u32 sw_if_index;
3191 int i;
3192 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3193 u32 target_fib_id = ntohl (mp->vrf_id);
3194
Ole Troan3288ed72017-12-06 17:00:05 +01003195 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003196
3197 /* *INDENT-OFF* */
3198 pool_foreach (fib_table, im6->fibs,
3199 ({
3200 vnet_sw_interface_t * si;
3201
3202 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3203
3204 if (fib->table_id != target_fib_id)
3205 continue;
3206
3207 vec_reset_length (sw_if_indices_to_shut);
3208
3209 /* Set the flow hash for this fib to the default */
3210 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3211
3212 /* Shut down interfaces in this FIB / clean out intfc routes */
3213 pool_foreach (si, im->sw_interfaces,
3214 ({
3215 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3216 fib->index)
3217 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3218 }));
3219
3220 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3221 sw_if_index = sw_if_indices_to_shut[i];
3222
3223 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3224 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3225 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3226 }
3227
3228 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3229
3230 rv = 0;
3231 break;
3232 })); /* pool_foreach (fib) */
3233 /* *INDENT-ON* */
3234
Ole Troan3288ed72017-12-06 17:00:05 +01003235 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003236 return rv;
3237}
3238
3239static void
3240vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3241{
3242 int rv;
3243 vl_api_reset_fib_reply_t *rmp;
3244
3245 if (mp->is_ipv6)
3246 rv = ip6_reset_fib_t_handler (mp);
3247 else
3248 rv = ip4_reset_fib_t_handler (mp);
3249
3250 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3251}
3252
3253static void
3254vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3255{
3256 int rv;
3257 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3258 vnet_main_t *vnm = vnet_get_main ();
3259 clib_error_t *error;
3260
3261 vnm->api_errno = 0;
3262
3263 if (mp->is_ipv6)
3264 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3265 else
3266 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3267
3268 if (error)
3269 {
3270 clib_error_report (error);
3271 rv = VNET_API_ERROR_UNSPECIFIED;
3272 }
3273 else
3274 {
3275 rv = vnm->api_errno;
3276 }
3277
3278 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3279}
3280
Klement Sekera75e7d132017-09-20 08:26:30 +02003281void
3282vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3283{
3284 vl_api_ip_reassembly_set_reply_t *rmp;
3285 int rv = 0;
3286 if (mp->is_ip6)
3287 {
3288 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3289 clib_net_to_host_u32 (mp->max_reassemblies),
3290 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3291 }
3292 else
3293 {
3294 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3295 clib_net_to_host_u32 (mp->max_reassemblies),
3296 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3297 }
3298
3299 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3300}
3301
3302void
3303vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3304{
3305 unix_shared_memory_queue_t *q;
3306
3307 q = vl_api_client_index_to_input_queue (mp->client_index);
3308
3309 if (q == 0)
3310 return;
3311
3312 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04003313 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02003314 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3315 rmp->context = mp->context;
3316 rmp->retval = 0;
3317 if (mp->is_ip6)
3318 {
3319 rmp->is_ip6 = 1;
3320 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3321 &rmp->expire_walk_interval_ms);
3322 }
3323 else
3324 {
3325 rmp->is_ip6 = 0;
3326 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3327 &rmp->expire_walk_interval_ms);
3328 }
3329 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3330 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3331 rmp->expire_walk_interval_ms =
3332 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3333 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3334}
3335
Klement Sekera4c533132018-02-22 11:41:12 +01003336void
3337 vl_api_ip_reassembly_enable_disable_t_handler
3338 (vl_api_ip_reassembly_enable_disable_t * mp)
3339{
3340 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3341 int rv = 0;
3342 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3343 mp->enable_ip4);
3344 if (0 == rv)
3345 {
3346 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3347 mp->enable_ip6);
3348 }
3349
Choree7f61d12018-11-28 10:27:58 +03303350 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01003351}
3352
Pavel Kotucek609e1212018-11-27 09:59:44 +01003353void
3354send_ip_punt_redirect_details (vl_api_registration_t * reg,
3355 u32 context, u32 sw_if_index,
3356 ip_punt_redirect_rx_t * pr, u8 is_ipv6)
3357{
3358 vl_api_ip_punt_redirect_details_t *mp;
3359
3360 mp = vl_msg_api_alloc (sizeof (*mp));
3361 if (!mp)
3362 return;
3363
3364 clib_memset (mp, 0, sizeof (*mp));
3365 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
3366 mp->context = context;
3367 mp->punt.rx_sw_if_index = htonl (sw_if_index);
3368 mp->punt.tx_sw_if_index = htonl (pr->tx_sw_if_index);
3369 if (is_ipv6)
3370 {
3371 ip_address_encode (&pr->nh, IP46_TYPE_IP6, &mp->punt.nh);
3372 }
3373 else
3374 {
3375 ip_address_encode (&pr->nh, IP46_TYPE_IP4, &mp->punt.nh);
3376 }
3377
3378 vl_api_send_msg (reg, (u8 *) mp);
3379}
3380
3381static void
3382vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
3383{
3384 vl_api_registration_t *reg;
3385 u32 sw_if_index;
3386 int rv __attribute__ ((unused)) = 0;
3387
3388 sw_if_index = ntohl (mp->sw_if_index);
3389 reg = vl_api_client_index_to_registration (mp->client_index);
3390 if (!reg)
3391 return;
3392
3393 if (~0 != sw_if_index)
3394 VALIDATE_SW_IF_INDEX (mp);
3395
3396 ip_punt_redirect_detail_t *pr, *prs;
3397 if (mp->is_ipv6)
3398 {
3399 prs = ip6_punt_redirect_entries (sw_if_index);
3400 /* *INDENT-OFF* */
3401 vec_foreach (pr, prs)
3402 {
3403 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 1);
3404 }
3405 /* *INDENT-ON* */
3406 vec_free (prs);
3407 }
3408 else
3409 {
3410 prs = ip4_punt_redirect_entries (sw_if_index);
3411 /* *INDENT-OFF* */
3412 vec_foreach (pr, prs)
3413 {
3414 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 0);
3415 }
3416 /* *INDENT-ON* */
3417 vec_free (prs);
3418 }
3419
3420 BAD_SW_IF_INDEX_LABEL;
3421}
3422
Dave Barachb5e8a772016-12-06 12:04:42 -05003423#define vl_msg_name_crc_list
3424#include <vnet/ip/ip.api.h>
3425#undef vl_msg_name_crc_list
3426
3427static void
3428setup_message_id_table (api_main_t * am)
3429{
3430#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3431 foreach_vl_msg_name_crc_ip;
3432#undef _
3433}
3434
3435static clib_error_t *
3436ip_api_hookup (vlib_main_t * vm)
3437{
3438 api_main_t *am = &api_main;
3439
3440#define _(N,n) \
3441 vl_msg_api_set_handlers(VL_API_##N, #n, \
3442 vl_api_##n##_t_handler, \
3443 vl_noop_handler, \
3444 vl_api_##n##_t_endian, \
3445 vl_api_##n##_t_print, \
3446 sizeof(vl_api_##n##_t), 1);
3447 foreach_ip_api_msg;
3448#undef _
3449
3450 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00003451 * Mark the route add/del API as MP safe
3452 */
3453 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3454 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE_REPLY] = 1;
3455
3456 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05003457 * Set up the (msg_name, crc, message-id) table
3458 */
3459 setup_message_id_table (am);
3460
Juraj Sloboda52574522018-05-03 10:03:50 +02003461 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3462
Dave Barachb5e8a772016-12-06 12:04:42 -05003463 return 0;
3464}
3465
3466VLIB_API_INIT_FUNCTION (ip_api_hookup);
3467
3468/*
3469 * fd.io coding-style-patch-verification: ON
3470 *
3471 * Local Variables:
3472 * eval: (c-set-style "gnu")
3473 * End:
3474 */