blob: 25d0b8be09e19ac55fc75c5381e55b2c1312efb3 [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{
334 u32 fib_index;
335 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500336} api_ip6_fib_show_ctx_t;
337
338static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500339api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500340{
341 api_ip6_fib_show_ctx_t *ctx = arg;
342
343 if ((kvp->key[2] >> 32) == ctx->fib_index)
344 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500346 }
347}
348
349static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800350api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500351 vl_api_ip6_fib_dump_t * mp,
352 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500353{
354 vpe_api_main_t *am = &vpe_api_main;
355 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500356 fib_node_index_t *fib_entry_index;
357 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700358 .fib_index = fib_table->ft_index,
359 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500360 };
361 fib_route_path_encode_t *api_rpaths;
Neale Rannsc5d43172018-07-30 08:04:40 -0700362 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500363
Dave Barachd7cb1b52016-12-09 09:52:16 -0500364 BV (clib_bihash_foreach_key_value_pair)
365 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
366 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500367
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500369
Dave Barachd7cb1b52016-12-09 09:52:16 -0500370 vec_foreach (fib_entry_index, ctx.entries)
371 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700372 pfx = fib_entry_get_prefix (*fib_entry_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500373 api_rpaths = NULL;
374 fib_entry_encode (*fib_entry_index, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700375 send_ip6_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500376 vec_free (api_rpaths);
377 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500378
Dave Barachd7cb1b52016-12-09 09:52:16 -0500379 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500380}
381
382static void
383vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
384{
Florin Coras6c4dae22018-01-09 06:39:23 -0800385 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500386 ip6_main_t *im6 = &ip6_main;
387 fib_table_t *fib_table;
388
Florin Coras6c4dae22018-01-09 06:39:23 -0800389 reg = vl_api_client_index_to_registration (mp->client_index);
390 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500391 return;
392
393 /* *INDENT-OFF* */
394 pool_foreach (fib_table, im6->fibs,
395 ({
Neale Ranns81458422018-03-12 06:59:36 -0700396 /* don't send link locals */
397 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
398 continue;
399
Florin Coras6c4dae22018-01-09 06:39:23 -0800400 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500401 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500402 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500403}
404
405static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800406send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800407 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800408{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800409 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800410 vl_api_ip_mfib_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -0800411 const mfib_prefix_t *pfx;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800412 mfib_entry_t *mfib_entry;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800413 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800414 int path_count;
415
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800416 mfib_entry = mfib_entry_get (mfei);
Neale Ranns9e829a82018-12-17 05:50:32 -0800417 pfx = mfib_entry_get_prefix (mfei);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800418 mfib_entry_encode (mfei, &api_rpaths);
419
Neale Ranns5a8123b2017-01-26 01:18:23 -0800420 path_count = vec_len (api_rpaths);
421 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
422 if (!mp)
423 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400424 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700425 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800426 mp->context = context;
427
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800428 mp->rpf_id = mfib_entry->mfe_rpf_id;
429 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800430 mp->table_id = htonl (table_id);
Neale Ranns9e829a82018-12-17 05:50:32 -0800431 mp->address_length = pfx->fp_len;
432 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip4,
433 sizeof (pfx->fp_grp_addr.ip4));
434 memcpy (mp->src_address, &pfx->fp_src_addr.ip4,
435 sizeof (pfx->fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800436
437 mp->count = htonl (path_count);
438 fp = mp->path;
439 vec_foreach (api_rpath, api_rpaths)
440 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800441 fib_api_path_encode (api_rpath, &fp->path);
442 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800443 fp++;
444 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800445 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800446
Florin Coras6c4dae22018-01-09 06:39:23 -0800447 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800448}
449
450typedef struct vl_api_ip_mfib_dump_ctc_t_
451{
452 fib_node_index_t *entries;
453} vl_api_ip_mfib_dump_ctc_t;
454
455static int
456vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
457{
458 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
459
460 vec_add1 (ctx->entries, fei);
461
462 return (0);
463}
464
465static void
466vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
467{
Florin Coras6c4dae22018-01-09 06:39:23 -0800468 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800469 ip4_main_t *im = &ip4_main;
470 mfib_table_t *mfib_table;
471 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800472 vl_api_ip_mfib_dump_ctc_t ctx = {
473 .entries = NULL,
474 };
475
Florin Coras6c4dae22018-01-09 06:39:23 -0800476 reg = vl_api_client_index_to_registration (mp->client_index);
477 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800478 return;
479
Neale Ranns5a8123b2017-01-26 01:18:23 -0800480 /* *INDENT-OFF* */
481 pool_foreach (mfib_table, im->mfibs,
482 ({
483 ip4_mfib_table_walk(&mfib_table->v4,
484 vl_api_ip_mfib_table_dump_walk,
485 &ctx);
486
487 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
488
489 vec_foreach (mfeip, ctx.entries)
490 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800491 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800492 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800493 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800494 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800495 vec_reset_length (ctx.entries);
496
497 }));
498 /* *INDENT-ON* */
499
500 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800501}
502
503static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800504send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800505 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800506 u32 table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800507 const mfib_prefix_t * pfx,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800508 fib_route_path_encode_t * api_rpaths, u32 context)
509{
510 vl_api_ip6_mfib_details_t *mp;
511 fib_route_path_encode_t *api_rpath;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800512 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800513 int path_count;
514
515 path_count = vec_len (api_rpaths);
516 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
517 if (!mp)
518 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400519 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700520 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800521 mp->context = context;
522
523 mp->table_id = htonl (table_id);
524 mp->address_length = pfx->fp_len;
525 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
526 sizeof (pfx->fp_grp_addr.ip6));
527 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
528 sizeof (pfx->fp_src_addr.ip6));
529
530 mp->count = htonl (path_count);
531 fp = mp->path;
532 vec_foreach (api_rpath, api_rpaths)
533 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800534 fib_api_path_encode (api_rpath, &fp->path);
535 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800536 fp++;
537 }
538
Florin Coras6c4dae22018-01-09 06:39:23 -0800539 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800540}
541
542typedef struct vl_api_ip6_mfib_dump_ctc_t_
543{
544 fib_node_index_t *entries;
545} vl_api_ip6_mfib_dump_ctc_t;
546
547static int
548vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
549{
550 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
551
552 vec_add1 (ctx->entries, fei);
553
554 return (0);
555}
556
557static void
558vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
559{
560 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800561 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800562 ip6_main_t *im = &ip6_main;
563 mfib_table_t *mfib_table;
Neale Ranns9e829a82018-12-17 05:50:32 -0800564 const mfib_prefix_t *pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800565 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800566 fib_route_path_encode_t *api_rpaths = NULL;
567 vl_api_ip6_mfib_dump_ctc_t ctx = {
568 .entries = NULL,
569 };
570
Florin Coras6c4dae22018-01-09 06:39:23 -0800571 reg = vl_api_client_index_to_registration (mp->client_index);
572 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800573 return;
574
575
576 /* *INDENT-OFF* */
577 pool_foreach (mfib_table, im->mfibs,
578 ({
579 ip6_mfib_table_walk(&mfib_table->v6,
580 vl_api_ip6_mfib_table_dump_walk,
581 &ctx);
582
583 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
584
585 vec_foreach(mfeip, ctx.entries)
586 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800587 pfx = mfib_entry_get_prefix (*mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800588 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800589 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800590 mfib_table->mft_table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800591 pfx, api_rpaths,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800592 mp->context);
593 }
594 vec_reset_length (api_rpaths);
595 vec_reset_length (ctx.entries);
596
597 }));
598 /* *INDENT-ON* */
599
600 vec_free (ctx.entries);
601 vec_free (api_rpaths);
602}
603
604static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700605vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
606 vlib_main_t * vm)
607{
608 vl_api_ip_punt_police_reply_t *rmp;
609 int rv = 0;
610
611 if (mp->is_ip6)
612 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
613 else
614 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
615
616 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
617}
618
619static void
620vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
621 vlib_main_t * vm)
622{
623 vl_api_ip_punt_redirect_reply_t *rmp;
624 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100625 ip46_type_t ipv;
626 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700627
Pavel Kotucek609e1212018-11-27 09:59:44 +0100628 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
629 goto bad_sw_if_index;
630
631 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700632 if (mp->is_add)
633 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100634 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700635 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100636 ip6_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 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100639 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700640 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100641 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
642 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700643 }
644 }
645 else
646 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100647 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700648 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100649 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700650 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100651 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700652 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100653 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700654 }
655 }
656
Pavel Kotucek609e1212018-11-27 09:59:44 +0100657 BAD_SW_IF_INDEX_LABEL;
658
Neale Rannsd91c1db2017-07-31 02:30:50 -0700659 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
660}
661
662static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500663vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
664 vlib_main_t * vm)
665{
Neale Ranns0bdd3192018-09-07 11:04:52 -0700666 ip46_address_t ip = ip46_address_initializer;
Dave Barachb5e8a772016-12-06 12:04:42 -0500667 vl_api_ip_neighbor_add_del_reply_t *rmp;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700668 ip_neighbor_flags_t flags;
Neale Ranns14260392018-09-28 05:00:57 -0700669 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500670 int rv = 0;
671
672 VALIDATE_SW_IF_INDEX (mp);
673
674 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
675
Neale Ranns0bdd3192018-09-07 11:04:52 -0700676 flags = IP_NEIGHBOR_FLAG_NODE;
677 if (mp->is_static)
678 flags |= IP_NEIGHBOR_FLAG_STATIC;
679 if (mp->is_no_adj_fib)
680 flags |= IP_NEIGHBOR_FLAG_NO_ADJ_FIB;
681
Dave Barachb5e8a772016-12-06 12:04:42 -0500682 if (mp->is_ipv6)
Neale Ranns0bdd3192018-09-07 11:04:52 -0700683 clib_memcpy (&ip.ip6, mp->dst_address, 16);
Dave Barachb5e8a772016-12-06 12:04:42 -0500684 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700685 clib_memcpy (&ip.ip4, mp->dst_address, 4);
Dave Barachb5e8a772016-12-06 12:04:42 -0500686
Neale Ranns0bdd3192018-09-07 11:04:52 -0700687 if (mp->is_add)
688 rv = ip_neighbor_add (&ip, mp->is_ipv6, mp->mac_address,
Neale Ranns14260392018-09-28 05:00:57 -0700689 ntohl (mp->sw_if_index), flags, &stats_index);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700690 else
691 rv = ip_neighbor_del (&ip, mp->is_ipv6, ntohl (mp->sw_if_index));
Dave Barachb5e8a772016-12-06 12:04:42 -0500692
Dave Barachb5e8a772016-12-06 12:04:42 -0500693 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700694
695 BAD_SW_IF_INDEX_LABEL;
Neale Ranns14260392018-09-28 05:00:57 -0700696
697 /* *INDENT-OFF* */
698 REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
699 ({
700 rmp->stats_index = htonl (stats_index);
701 }));
702 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500703}
704
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700705void
Neale Ranns15002542017-09-10 04:39:11 -0700706ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
707{
708 u32 fib_index, mfib_index;
709
710 /*
711 * ignore action on the default table - this is always present
712 * and cannot be added nor deleted from the API
713 */
714 if (0 != table_id)
715 {
716 /*
717 * The API holds only one lock on the table.
718 * i.e. it can be added many times via the API but needs to be
719 * deleted only once.
720 * The FIB index for unicast and multicast is not necessarily the
721 * same, since internal VPP systesm (like LISP and SR) create
722 * their own unicast tables.
723 */
724 fib_index = fib_table_find (fproto, table_id);
725 mfib_index = mfib_table_find (fproto, table_id);
726
727 if (~0 != fib_index)
728 {
729 fib_table_unlock (fib_index, fproto,
730 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
731 }
732 if (~0 != mfib_index)
733 {
734 mfib_table_unlock (mfib_index, fproto,
735 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
736 }
737 }
738}
739
740void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700741vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
742{
743 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700744 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
745 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700746 int rv = 0;
747
Neale Ranns15002542017-09-10 04:39:11 -0700748 if (mp->is_add)
749 {
Neale Ranns2297af02017-09-12 09:45:04 -0700750 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700751 }
752 else
753 {
754 ip_table_delete (fproto, table_id, 1);
755 }
756
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700757 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
758}
759
Dave Barachb5e8a772016-12-06 12:04:42 -0500760int
761add_del_route_t_handler (u8 is_multipath,
762 u8 is_add,
763 u8 is_drop,
764 u8 is_unreach,
765 u8 is_prohibit,
766 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800767 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500768 u8 is_classify,
769 u32 classify_table_index,
770 u8 is_resolve_host,
771 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800772 u8 is_interface_rx,
773 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800774 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700775 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800776 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500777 u32 fib_index,
778 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700779 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500780 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800781 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500782 u32 next_hop_sw_if_index,
783 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700784 u16 next_hop_weight,
785 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500786 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800787 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500788{
789 vnet_classify_main_t *cm = &vnet_classify_main;
790 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
791 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700792 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500793 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
794 .frp_sw_if_index = next_hop_sw_if_index,
795 .frp_fib_index = next_hop_fib_index,
796 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700797 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500798 .frp_label_stack = next_hop_out_label_stack,
799 };
800 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800801 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500802
Neale Rannscaac3502017-09-18 18:04:10 -0700803 /*
804 * the special INVALID label meams we are not recursing via a
805 * label. Exp-null value is never a valid via-label so that
806 * also means it's not a via-label and means clients that set
807 * it to 0 by default get the expected behaviour
808 */
809 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500810 {
Neale Rannsda78f952017-05-24 09:15:43 -0700811 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500812 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800813 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500814 }
Neale Ranns775f73c2018-12-20 03:01:49 -0800815 if (is_local)
Neale Ranns7425f922019-01-23 00:36:16 -0800816 {
817 path_flags |= FIB_ROUTE_PATH_LOCAL;
818 if (~0 != next_hop_sw_if_index)
819 {
820 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
821 }
822 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800823 if (is_dvr)
824 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500825 if (is_resolve_host)
826 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
827 if (is_resolve_attached)
828 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800829 if (is_interface_rx)
830 path_flags |= FIB_ROUTE_PATH_INTF_RX;
831 if (is_rpf_id)
832 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700833 if (is_source_lookup)
834 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800835 if (is_multicast)
836 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800837 if (is_udp_encap)
838 {
839 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
840 path.frp_udp_encap_id = next_hop_id;
841 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800842 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
843 && path.frp_fib_index != ~0)
844 {
845 path_flags |= FIB_ROUTE_PATH_DEAG;
846 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500847
848 path.frp_flags = path_flags;
849
Dave Barachb5e8a772016-12-06 12:04:42 -0500850 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
851
Neale Ranns7425f922019-01-23 00:36:16 -0800852 if (is_drop || (is_local && (~0 == next_hop_sw_if_index)) ||
853 is_classify || is_unreach || is_prohibit)
Dave Barachb5e8a772016-12-06 12:04:42 -0500854 {
855 /*
856 * special route types that link directly to the adj
857 */
858 if (is_add)
859 {
860 dpo_id_t dpo = DPO_INVALID;
861 dpo_proto_t dproto;
862
863 dproto = fib_proto_to_dpo (prefix->fp_proto);
864
865 if (is_drop)
866 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
867 else if (is_local)
868 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
869 else if (is_unreach)
870 ip_null_dpo_add_and_lock (dproto,
871 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
872 else if (is_prohibit)
873 ip_null_dpo_add_and_lock (dproto,
874 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
875 &dpo);
876 else if (is_classify)
877 {
878 if (pool_is_free_index (cm->tables,
879 ntohl (classify_table_index)))
880 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500882 return VNET_API_ERROR_NO_SUCH_TABLE;
883 }
884
885 dpo_set (&dpo, DPO_CLASSIFY, dproto,
886 classify_dpo_create (dproto,
887 ntohl (classify_table_index)));
888 }
889 else
890 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500891 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500892 return VNET_API_ERROR_NO_SUCH_TABLE;
893 }
894
895 fib_table_entry_special_dpo_update (fib_index,
896 prefix,
897 FIB_SOURCE_API,
898 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
899 dpo_reset (&dpo);
900 }
901 else
902 {
903 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
904 }
905 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700906 else if (is_multipath)
907 {
908 vec_add1 (paths, path);
909
910 if (is_add)
911 fib_table_entry_path_add2 (fib_index,
912 prefix,
913 FIB_SOURCE_API, entry_flags, paths);
914 else
915 fib_table_entry_path_remove2 (fib_index,
916 prefix, FIB_SOURCE_API, paths);
917
918 vec_free (paths);
919 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500920 else
921 {
922 if (is_add)
923 {
924 vec_add1 (paths, path);
925 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800926 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500927 vec_free (paths);
928 }
929 else
930 {
931 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
932 }
933 }
934
Dave Barachd7cb1b52016-12-09 09:52:16 -0500935 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500936 return (0);
937}
938
939int
940add_del_route_check (fib_protocol_t table_proto,
941 u32 table_id,
942 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700943 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500944 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800945 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500946{
947 vnet_main_t *vnm = vnet_get_main ();
948
949 *fib_index = fib_table_find (table_proto, ntohl (table_id));
950 if (~0 == *fib_index)
951 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800952 /* No such VRF, and we weren't asked to create one */
953 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500954 }
955
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800956 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500957 {
958 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
959 ntohl (next_hop_sw_if_index)))
960 {
961 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
962 }
963 }
964 else
965 {
Neale Rannsda78f952017-05-24 09:15:43 -0700966 fib_protocol_t fib_nh_proto;
967
968 if (next_hop_table_proto > DPO_PROTO_MPLS)
969 return (0);
970
971 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
972
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800973 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700974 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800975 ntohl (next_hop_table_id));
976 else
Neale Rannsda78f952017-05-24 09:15:43 -0700977 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800978 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500979
980 if (~0 == *next_hop_fib_index)
981 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800982 /* No such VRF, and we weren't asked to create one */
983 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500984 }
985 }
986
987 return (0);
988}
989
990static int
Neale Ranns008dbe12018-09-07 09:32:36 -0700991ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
992 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500993{
994 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800995 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500996 int rv, ii, n_labels;;
997
998 rv = add_del_route_check (FIB_PROTOCOL_IP4,
999 mp->table_id,
1000 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001001 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001002 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001003 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001004
1005 if (0 != rv)
1006 return (rv);
1007
1008 fib_prefix_t pfx = {
1009 .fp_len = mp->dst_address_length,
1010 .fp_proto = FIB_PROTOCOL_IP4,
1011 };
1012 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1013
1014 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001015 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001016 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1017
1018 n_labels = mp->next_hop_n_out_labels;
1019 if (n_labels == 0)
1020 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001021 else
1022 {
1023 vec_validate (label_stack, n_labels - 1);
1024 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001025 {
1026 label_stack[ii].fml_value =
1027 ntohl (mp->next_hop_out_label_stack[ii].label);
1028 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1029 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1030 label_stack[ii].fml_mode =
1031 (mp->next_hop_out_label_stack[ii].is_uniform ?
1032 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1033 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001034 }
1035
Neale Ranns008dbe12018-09-07 09:32:36 -07001036 rv = add_del_route_t_handler (mp->is_multipath,
1037 mp->is_add,
1038 mp->is_drop,
1039 mp->is_unreach,
1040 mp->is_prohibit,
1041 mp->is_local, 0,
1042 mp->is_classify,
1043 mp->classify_table_index,
1044 mp->is_resolve_host,
1045 mp->is_resolve_attached, 0, 0,
1046 mp->is_dvr,
1047 mp->is_source_lookup,
1048 mp->is_udp_encap,
1049 fib_index, &pfx, DPO_PROTO_IP4,
1050 &nh,
1051 ntohl (mp->next_hop_id),
1052 ntohl (mp->next_hop_sw_if_index),
1053 next_hop_fib_index,
1054 mp->next_hop_weight,
1055 mp->next_hop_preference,
1056 ntohl (mp->next_hop_via_label), label_stack);
1057
1058 if (mp->is_add && 0 == rv)
1059 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1060
1061 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001062}
1063
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001064static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001065ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1066 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001067{
Neale Ranns31ed7442018-02-23 05:29:09 -08001068 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001069 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001070 int rv, ii, n_labels;;
1071
1072 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1073 mp->table_id,
1074 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001075 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001076 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001077 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001078
1079 if (0 != rv)
1080 return (rv);
1081
1082 fib_prefix_t pfx = {
1083 .fp_len = mp->dst_address_length,
1084 .fp_proto = FIB_PROTOCOL_IP6,
1085 };
1086 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1087
1088 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001089 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001090 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1091
1092 n_labels = mp->next_hop_n_out_labels;
1093 if (n_labels == 0)
1094 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001095 else
1096 {
1097 vec_validate (label_stack, n_labels - 1);
1098 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001099 {
1100 label_stack[ii].fml_value =
1101 ntohl (mp->next_hop_out_label_stack[ii].label);
1102 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1103 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1104 label_stack[ii].fml_mode =
1105 (mp->next_hop_out_label_stack[ii].is_uniform ?
1106 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1107 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001108 }
1109
Neale Ranns008dbe12018-09-07 09:32:36 -07001110 rv = add_del_route_t_handler (mp->is_multipath,
1111 mp->is_add,
1112 mp->is_drop,
1113 mp->is_unreach,
1114 mp->is_prohibit,
1115 mp->is_local, 0,
1116 mp->is_classify,
1117 mp->classify_table_index,
1118 mp->is_resolve_host,
1119 mp->is_resolve_attached, 0, 0,
1120 mp->is_dvr,
1121 mp->is_source_lookup,
1122 mp->is_udp_encap,
1123 fib_index, &pfx, DPO_PROTO_IP6,
1124 &nh, ntohl (mp->next_hop_id),
1125 ntohl (mp->next_hop_sw_if_index),
1126 next_hop_fib_index,
1127 mp->next_hop_weight,
1128 mp->next_hop_preference,
1129 ntohl (mp->next_hop_via_label), label_stack);
1130
1131 if (mp->is_add && 0 == rv)
1132 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1133
1134 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001135}
1136
1137void
1138vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1139{
1140 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001141 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001142 int rv;
1143 vnet_main_t *vnm = vnet_get_main ();
1144
1145 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001146 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001147
1148 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001149 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001150 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001151 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001152
1153 rv = (rv == 0) ? vnm->api_errno : rv;
1154
Neale Ranns008dbe12018-09-07 09:32:36 -07001155 /* *INDENT-OFF* */
1156 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1157 ({
1158 rmp->stats_index = htonl (stats_index);
1159 }))
1160 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001161}
1162
Neale Ranns15002542017-09-10 04:39:11 -07001163void
Neale Ranns2297af02017-09-12 09:45:04 -07001164ip_table_create (fib_protocol_t fproto,
1165 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001166{
1167 u32 fib_index, mfib_index;
1168
1169 /*
1170 * ignore action on the default table - this is always present
1171 * and cannot be added nor deleted from the API
1172 */
1173 if (0 != table_id)
1174 {
1175 /*
1176 * The API holds only one lock on the table.
1177 * i.e. it can be added many times via the API but needs to be
1178 * deleted only once.
1179 * The FIB index for unicast and multicast is not necessarily the
1180 * same, since internal VPP systesm (like LISP and SR) create
1181 * their own unicast tables.
1182 */
1183 fib_index = fib_table_find (fproto, table_id);
1184 mfib_index = mfib_table_find (fproto, table_id);
1185
1186 if (~0 == fib_index)
1187 {
Neale Ranns2297af02017-09-12 09:45:04 -07001188 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1189 (is_api ?
1190 FIB_SOURCE_API :
1191 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001192 }
1193 if (~0 == mfib_index)
1194 {
Neale Ranns2297af02017-09-12 09:45:04 -07001195 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1196 (is_api ?
1197 MFIB_SOURCE_API :
1198 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001199 }
1200 }
1201}
1202
Neale Ranns32e1c012016-11-22 17:07:28 +00001203static int
1204add_del_mroute_check (fib_protocol_t table_proto,
1205 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001206 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001207{
1208 vnet_main_t *vnm = vnet_get_main ();
1209
1210 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1211 if (~0 == *fib_index)
1212 {
Neale Ranns15002542017-09-10 04:39:11 -07001213 /* No such table */
1214 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001215 }
1216
1217 if (~0 != ntohl (next_hop_sw_if_index))
1218 {
1219 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1220 ntohl (next_hop_sw_if_index)))
1221 {
1222 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1223 }
1224 }
1225
1226 return (0);
1227}
1228
Neale Ranns28c142e2018-09-07 09:37:07 -07001229static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001230mroute_add_del_handler (u8 is_add,
1231 u8 is_local,
1232 u32 fib_index,
1233 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001234 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001235 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001236 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001237 u32 next_hop_sw_if_index,
1238 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001239{
Neale Ranns28c142e2018-09-07 09:37:07 -07001240 fib_node_index_t mfib_entry_index = ~0;
1241
Neale Ranns32e1c012016-11-22 17:07:28 +00001242 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1243
1244 fib_route_path_t path = {
1245 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001246 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001247 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001248 };
1249
1250 if (is_local)
1251 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1252
Neale Rannsd792d9c2017-10-21 10:53:20 -07001253 if (DPO_PROTO_BIER == nh_proto)
1254 {
1255 path.frp_bier_imp = bier_imp;
1256 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1257 }
1258 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001259 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001260 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1261 MFIB_SOURCE_API,
1262 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001263 goto done;
1264 }
1265
1266 if (is_add)
1267 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001268 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1269 MFIB_SOURCE_API,
1270 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001271 }
1272 else
1273 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001274 mfib_table_entry_path_remove (fib_index, prefix,
1275 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001276 }
1277
Neale Rannsd792d9c2017-10-21 10:53:20 -07001278done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001279 stats_dsunlock ();
Neale Ranns28c142e2018-09-07 09:37:07 -07001280 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001281}
1282
1283static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001284api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1285 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001286{
Neale Ranns28c142e2018-09-07 09:37:07 -07001287 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001288 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001289 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001290 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001291 u32 fib_index;
1292 int rv;
1293
Neale Rannsd792d9c2017-10-21 10:53:20 -07001294 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001295 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1296 rv = add_del_mroute_check (fproto,
1297 mp->table_id,
1298 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001299 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001300
1301 if (0 != rv)
1302 return (rv);
1303
1304 mfib_prefix_t pfx = {
1305 .fp_len = ntohs (mp->grp_address_length),
1306 .fp_proto = fproto,
1307 };
1308
1309 if (FIB_PROTOCOL_IP4 == fproto)
1310 {
1311 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1312 sizeof (pfx.fp_grp_addr.ip4));
1313 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1314 sizeof (pfx.fp_src_addr.ip4));
Dave Barachb7b92992018-10-17 10:38:51 -04001315 clib_memset (&nh.ip6, 0, sizeof (nh.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001316 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001317 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1318 pfx.fp_len = 64;
Neale Ranns32e1c012016-11-22 17:07:28 +00001319 }
1320 else
1321 {
1322 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1323 sizeof (pfx.fp_grp_addr.ip6));
1324 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1325 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001326 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001327 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1328 pfx.fp_len = 256;
Neale Ranns32e1c012016-11-22 17:07:28 +00001329 }
1330
Neale Ranns28c142e2018-09-07 09:37:07 -07001331 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1332 mp->is_local,
1333 fib_index, &pfx,
1334 nh_proto,
1335 ntohl (mp->entry_flags),
1336 ntohl (mp->rpf_id),
1337 ntohl (mp->next_hop_sw_if_index),
1338 &nh,
1339 ntohl (mp->itf_flags),
1340 ntohl (mp->bier_imp));
1341
1342 if (~0 != mfib_entry_index)
1343 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1344
1345 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001346}
1347
1348void
1349vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1350{
1351 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001352 vnet_main_t *vnm;
1353 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001354 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001355
Neale Ranns28c142e2018-09-07 09:37:07 -07001356 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001357 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001358 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001359
Neale Ranns28c142e2018-09-07 09:37:07 -07001360 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001361
Neale Ranns28c142e2018-09-07 09:37:07 -07001362 /* *INDENT-OFF* */
1363 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1364 ({
1365 rmp->stats_index = htonl (stats_index);
1366 }));
1367 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001368}
1369
Dave Barachb5e8a772016-12-06 12:04:42 -05001370static void
1371send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001372 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1373 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001374{
1375 vl_api_ip_details_t *mp;
1376
1377 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001378 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001379 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1380
1381 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001382 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001383 mp->context = context;
1384
Florin Coras6c4dae22018-01-09 06:39:23 -08001385 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001386}
1387
1388static void
1389send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001390 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001391 u8 * ip, u16 prefix_length,
1392 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001393{
1394 vl_api_ip_address_details_t *mp;
1395
1396 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001397 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001398 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1399
1400 if (is_ipv6)
1401 {
1402 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1403 }
1404 else
1405 {
1406 u32 *tp = (u32 *) mp->ip;
1407 *tp = *(u32 *) ip;
1408 }
1409 mp->prefix_length = prefix_length;
1410 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001411 mp->sw_if_index = htonl (sw_if_index);
1412 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001413
Florin Coras6c4dae22018-01-09 06:39:23 -08001414 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001415}
1416
1417static void
1418vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1419{
1420 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001421 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001422 ip6_address_t *r6;
1423 ip4_address_t *r4;
1424 ip6_main_t *im6 = &ip6_main;
1425 ip4_main_t *im4 = &ip4_main;
1426 ip_lookup_main_t *lm6 = &im6->lookup_main;
1427 ip_lookup_main_t *lm4 = &im4->lookup_main;
1428 ip_interface_address_t *ia = 0;
1429 u32 sw_if_index = ~0;
1430 int rv __attribute__ ((unused)) = 0;
1431
1432 VALIDATE_SW_IF_INDEX (mp);
1433
1434 sw_if_index = ntohl (mp->sw_if_index);
1435
Florin Coras6c4dae22018-01-09 06:39:23 -08001436 reg = vl_api_client_index_to_registration (mp->client_index);
1437 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001438 return;
1439
Dave Barachb5e8a772016-12-06 12:04:42 -05001440 if (mp->is_ipv6)
1441 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001442 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001443 /* Do not send subnet details of the IP-interface for
1444 * unnumbered interfaces. otherwise listening clients
1445 * will be confused that the subnet is applied on more
1446 * than one interface */
1447 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001448 ({
1449 r6 = ip_interface_address_get_address (lm6, ia);
1450 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001451 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001452 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001453 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001454 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001455 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001456 else
1457 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001458 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001459 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001460 ({
1461 r4 = ip_interface_address_get_address (lm4, ia);
1462 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001463 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001464 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001465 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001466 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001467 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001468
Dave Barachb5e8a772016-12-06 12:04:42 -05001469 BAD_SW_IF_INDEX_LABEL;
1470}
1471
1472static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001473send_ip_unnumbered_details (vpe_api_main_t * am,
1474 vl_api_registration_t * reg,
1475 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1476{
1477 vl_api_ip_unnumbered_details_t *mp;
1478
1479 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001480 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001481 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1482
1483 mp->context = context;
1484 mp->sw_if_index = htonl (sw_if_index);
1485 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1486
1487 vl_api_send_msg (reg, (u8 *) mp);
1488}
1489
1490static void
1491vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1492{
1493 vnet_main_t *vnm = vnet_get_main ();
1494 vnet_interface_main_t *im = &vnm->interface_main;
1495 int rv __attribute__ ((unused)) = 0;
1496 vpe_api_main_t *am = &vpe_api_main;
1497 vl_api_registration_t *reg;
1498 vnet_sw_interface_t *si;
1499 u32 sw_if_index;
1500
1501 sw_if_index = ntohl (mp->sw_if_index);
1502
1503 reg = vl_api_client_index_to_registration (mp->client_index);
1504 if (!reg)
1505 return;
1506
1507 if (~0 != sw_if_index)
1508 {
1509 VALIDATE_SW_IF_INDEX (mp);
1510
1511 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1512
1513 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1514 {
1515 send_ip_unnumbered_details (am, reg,
1516 sw_if_index,
1517 si->unnumbered_sw_if_index,
1518 mp->context);
1519 }
1520 }
1521 else
1522 {
1523 /* *INDENT-OFF* */
1524 pool_foreach (si, im->sw_interfaces,
1525 ({
1526 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1527 {
1528 send_ip_unnumbered_details(am, reg,
1529 si->sw_if_index,
1530 si->unnumbered_sw_if_index,
1531 mp->context);
1532 }
1533 }));
1534 /* *INDENT-ON* */
1535 }
1536
1537 BAD_SW_IF_INDEX_LABEL;
1538}
1539
1540static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001541vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1542{
1543 vpe_api_main_t *am = &vpe_api_main;
1544 vnet_main_t *vnm = vnet_get_main ();
1545 vlib_main_t *vm = vlib_get_main ();
1546 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001547 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001548 vnet_sw_interface_t *si, *sorted_sis;
1549 u32 sw_if_index = ~0;
1550
Florin Coras6c4dae22018-01-09 06:39:23 -08001551 reg = vl_api_client_index_to_registration (mp->client_index);
1552 if (!reg)
1553 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001554
1555 /* Gather interfaces. */
1556 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1557 _vec_len (sorted_sis) = 0;
1558 /* *INDENT-OFF* */
1559 pool_foreach (si, im->sw_interfaces,
1560 ({
1561 vec_add1 (sorted_sis, si[0]);
1562 }));
1563 /* *INDENT-ON* */
1564
1565 vec_foreach (si, sorted_sis)
1566 {
1567 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1568 {
1569 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1570 {
1571 continue;
1572 }
1573 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001574 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001575 }
1576 }
1577}
1578
1579static void
1580set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1581{
1582 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001583 int rv;
1584 u32 table_id;
1585 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001586
Neale Ranns227038a2017-04-21 01:07:59 -07001587 table_id = ntohl (mp->vrf_id);
1588
1589#define _(a,b) if (mp->a) flow_hash_config |= b;
1590 foreach_flow_hash_bit;
1591#undef _
1592
1593 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001594
1595 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1596}
1597
1598static void
1599set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1600{
1601 vl_api_set_ip_flow_hash_reply_t *rmp;
1602 int rv;
1603 u32 table_id;
1604 flow_hash_config_t flow_hash_config = 0;
1605
1606 table_id = ntohl (mp->vrf_id);
1607
1608#define _(a,b) if (mp->a) flow_hash_config |= b;
1609 foreach_flow_hash_bit;
1610#undef _
1611
1612 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1613
1614 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1615}
1616
1617
1618static void
1619vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1620{
1621 if (mp->is_ipv6 == 0)
1622 set_ip4_flow_hash (mp);
1623 else
1624 set_ip6_flow_hash (mp);
1625}
1626
1627static void
1628 vl_api_sw_interface_ip6nd_ra_config_t_handler
1629 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1630{
1631 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1632 vlib_main_t *vm = vlib_get_main ();
1633 int rv = 0;
1634 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1635 default_router;
1636
1637 is_no = mp->is_no == 1;
1638 suppress = mp->suppress == 1;
1639 managed = mp->managed == 1;
1640 other = mp->other == 1;
1641 ll_option = mp->ll_option == 1;
1642 send_unicast = mp->send_unicast == 1;
1643 cease = mp->cease == 1;
1644 default_router = mp->default_router == 1;
1645
1646 VALIDATE_SW_IF_INDEX (mp);
1647
1648 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1649 suppress, managed, other,
1650 ll_option, send_unicast, cease,
1651 default_router, ntohl (mp->lifetime),
1652 ntohl (mp->initial_count),
1653 ntohl (mp->initial_interval),
1654 ntohl (mp->max_interval),
1655 ntohl (mp->min_interval), is_no);
1656
1657 BAD_SW_IF_INDEX_LABEL;
1658
1659 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1660}
1661
1662static void
1663 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1664 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1665{
1666 vlib_main_t *vm = vlib_get_main ();
1667 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1668 int rv = 0;
1669 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1670
1671 VALIDATE_SW_IF_INDEX (mp);
1672
1673 is_no = mp->is_no == 1;
1674 use_default = mp->use_default == 1;
1675 no_advertise = mp->no_advertise == 1;
1676 off_link = mp->off_link == 1;
1677 no_autoconfig = mp->no_autoconfig == 1;
1678 no_onlink = mp->no_onlink == 1;
1679
1680 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1681 (ip6_address_t *) mp->address,
1682 mp->address_length, use_default,
1683 ntohl (mp->val_lifetime),
1684 ntohl (mp->pref_lifetime), no_advertise,
1685 off_link, no_autoconfig, no_onlink, is_no);
1686
1687 BAD_SW_IF_INDEX_LABEL;
1688 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1689}
1690
1691static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001692send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001693 u32 context,
1694 const ip46_address_t * addr, u32 sw_if_index)
1695{
1696 vl_api_ip6nd_proxy_details_t *mp;
1697
1698 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001699 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns3f844d02017-02-18 00:03:54 -08001700 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1701 mp->context = context;
1702 mp->sw_if_index = htonl (sw_if_index);
1703 memcpy (mp->address, addr, 16);
1704
Florin Coras6c4dae22018-01-09 06:39:23 -08001705 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001706}
1707
Neale Ranns3f844d02017-02-18 00:03:54 -08001708typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1709{
1710 u32 *indices;
1711} api_ip6nd_proxy_fib_table_walk_ctx_t;
1712
Neale Ranns89541992017-04-06 04:41:02 -07001713static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001714api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1715{
1716 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1717
1718 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1719 {
1720 vec_add1 (ctx->indices, fei);
1721 }
1722
Neale Ranns89541992017-04-06 04:41:02 -07001723 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001724}
1725
1726static void
1727vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1728{
1729 ip6_main_t *im6 = &ip6_main;
1730 fib_table_t *fib_table;
1731 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1732 .indices = NULL,
1733 };
1734 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001735 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001736 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001737
Florin Coras6c4dae22018-01-09 06:39:23 -08001738 reg = vl_api_client_index_to_registration (mp->client_index);
1739 if (!reg)
1740 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001741
1742 /* *INDENT-OFF* */
1743 pool_foreach (fib_table, im6->fibs,
1744 ({
1745 fib_table_walk(fib_table->ft_index,
1746 FIB_PROTOCOL_IP6,
1747 api_ip6nd_proxy_fib_table_walk,
1748 &ctx);
1749 }));
1750 /* *INDENT-ON* */
1751
1752 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1753
1754 vec_foreach (feip, ctx.indices)
1755 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001756 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001757
Florin Coras6c4dae22018-01-09 06:39:23 -08001758 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001759 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001760 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001761 fib_entry_get_resolving_interface (*feip));
1762 }
1763
1764 vec_free (ctx.indices);
1765}
1766
1767static void
1768vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1769{
1770 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1771 int rv = 0;
1772
1773 VALIDATE_SW_IF_INDEX (mp);
1774
1775 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1776 (ip6_address_t *) mp->address, mp->is_del);
1777
1778 BAD_SW_IF_INDEX_LABEL;
1779 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1780}
1781
1782static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001783 vl_api_ip6nd_send_router_solicitation_t_handler
1784 (vl_api_ip6nd_send_router_solicitation_t * mp)
1785{
1786 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1787 icmp6_send_router_solicitation_params_t params;
1788 vlib_main_t *vm = vlib_get_main ();
1789 int rv = 0;
1790
1791 VALIDATE_SW_IF_INDEX (mp);
1792
1793 BAD_SW_IF_INDEX_LABEL;
1794 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1795
1796 if (rv != 0)
1797 return;
1798
1799 params.irt = ntohl (mp->irt);
1800 params.mrt = ntohl (mp->mrt);
1801 params.mrc = ntohl (mp->mrc);
1802 params.mrd = ntohl (mp->mrd);
1803
1804 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1805 &params);
1806}
1807
1808static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001809 vl_api_sw_interface_ip6_enable_disable_t_handler
1810 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1811{
1812 vlib_main_t *vm = vlib_get_main ();
1813 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1814 vnet_main_t *vnm = vnet_get_main ();
1815 int rv = 0;
1816 clib_error_t *error;
1817
1818 vnm->api_errno = 0;
1819
1820 VALIDATE_SW_IF_INDEX (mp);
1821
1822 error =
1823 (mp->enable == 1) ? enable_ip6_interface (vm,
1824 ntohl (mp->sw_if_index)) :
1825 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1826
1827 if (error)
1828 {
1829 clib_error_report (error);
1830 rv = VNET_API_ERROR_UNSPECIFIED;
1831 }
1832 else
1833 {
1834 rv = vnm->api_errno;
1835 }
1836
1837 BAD_SW_IF_INDEX_LABEL;
1838
1839 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1840}
1841
Neale Ranns32e1c012016-11-22 17:07:28 +00001842void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001843vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001844 u32 context, const mfib_signal_t * mfs)
1845{
1846 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001847 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001848 mfib_table_t *mfib;
1849 mfib_itf_t *mfi;
1850
1851 mp = vl_msg_api_alloc (sizeof (*mp));
1852
Dave Barachb7b92992018-10-17 10:38:51 -04001853 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001854 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1855 mp->context = context;
1856
1857 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001858 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001859 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001860 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001861 mp->table_id = ntohl (mfib->mft_table_id);
1862 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1863
Neale Ranns9e829a82018-12-17 05:50:32 -08001864 if (FIB_PROTOCOL_IP4 == prefix->fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +00001865 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001866 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001867
Neale Ranns9e829a82018-12-17 05:50:32 -08001868 memcpy (mp->grp_address, &prefix->fp_grp_addr.ip4, 4);
1869 if (prefix->fp_len > 32)
Neale Ranns32e1c012016-11-22 17:07:28 +00001870 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001871 memcpy (mp->src_address, &prefix->fp_src_addr.ip4, 4);
Neale Ranns32e1c012016-11-22 17:07:28 +00001872 }
1873 }
1874 else
1875 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001876 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001877
1878 ASSERT (0);
1879 }
1880
1881 if (0 != mfs->mfs_buffer_len)
1882 {
1883 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1884
1885 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1886 }
1887 else
1888 {
1889 mp->ip_packet_len = 0;
1890 }
1891
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001892 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001893}
1894
1895static void
1896vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1897{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001898 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001899
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001900 reg = vl_api_client_index_to_registration (mp->client_index);
1901 if (!reg)
1902 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001903
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001904 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001905 ;
1906}
Dave Barachb5e8a772016-12-06 12:04:42 -05001907
Florin Coras595992c2017-11-06 17:17:08 -08001908static void
1909 vl_api_ip_container_proxy_add_del_t_handler
1910 (vl_api_ip_container_proxy_add_del_t * mp)
1911{
1912 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1913 vnet_ip_container_proxy_args_t args;
1914 int rv = 0;
1915 clib_error_t *error;
1916
Dave Barachb7b92992018-10-17 10:38:51 -04001917 clib_memset (&args, 0, sizeof (args));
Florin Coras595992c2017-11-06 17:17:08 -08001918 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1919 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1920 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1921 args.is_add = mp->is_add;
1922 if ((error = vnet_ip_container_proxy_add_del (&args)))
1923 {
1924 rv = clib_error_get_code (error);
1925 clib_error_report (error);
1926 }
1927
1928 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1929}
1930
Matus Fabian75b9f452018-10-02 23:27:21 -07001931typedef struct ip_container_proxy_walk_ctx_t_
1932{
1933 vl_api_registration_t *reg;
1934 u32 context;
1935} ip_container_proxy_walk_ctx_t;
1936
1937static int
1938ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1939 void *args)
1940{
1941 vl_api_ip_container_proxy_details_t *mp;
1942 ip_container_proxy_walk_ctx_t *ctx = args;
1943
1944 mp = vl_msg_api_alloc (sizeof (*mp));
1945 if (!mp)
1946 return 1;
1947
Dave Barachb7b92992018-10-17 10:38:51 -04001948 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001949 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1950 mp->context = ctx->context;
1951
1952 mp->sw_if_index = ntohl (sw_if_index);
1953 ip_prefix_encode (pfx, &mp->prefix);
1954
1955 vl_api_send_msg (ctx->reg, (u8 *) mp);
1956
1957 return 1;
1958}
1959
1960static void
1961vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1962 mp)
1963{
1964 vl_api_registration_t *reg;
1965
1966 reg = vl_api_client_index_to_registration (mp->client_index);
1967 if (!reg)
1968 return;
1969
1970 ip_container_proxy_walk_ctx_t ctx = {
1971 .context = mp->context,
1972 .reg = reg,
1973 };
1974
1975 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1976}
1977
Neale Rannsb8d44812017-11-10 06:53:54 -08001978static void
1979vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1980{
1981 int rv = 0;
1982 vl_api_ioam_enable_reply_t *rmp;
1983 clib_error_t *error;
1984
1985 /* Ignoring the profile id as currently a single profile
1986 * is supported */
1987 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1988 mp->seqno, mp->analyse);
1989 if (error)
1990 {
1991 clib_error_report (error);
1992 rv = clib_error_get_code (error);
1993 }
1994
1995 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1996}
1997
1998static void
1999vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
2000{
2001 int rv = 0;
2002 vl_api_ioam_disable_reply_t *rmp;
2003 clib_error_t *error;
2004
2005 error = clear_ioam_rewrite_fn ();
2006 if (error)
2007 {
2008 clib_error_report (error);
2009 rv = clib_error_get_code (error);
2010 }
2011
2012 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2013}
2014
2015static void
2016 vl_api_ip_source_and_port_range_check_add_del_t_handler
2017 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2018{
2019 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2020 int rv = 0;
2021
2022 u8 is_ipv6 = mp->is_ipv6;
2023 u8 is_add = mp->is_add;
2024 u8 mask_length = mp->mask_length;
2025 ip4_address_t ip4_addr;
2026 ip6_address_t ip6_addr;
2027 u16 *low_ports = 0;
2028 u16 *high_ports = 0;
2029 u32 vrf_id;
2030 u16 tmp_low, tmp_high;
2031 u8 num_ranges;
2032 int i;
2033
2034 // Validate port range
2035 num_ranges = mp->number_of_ranges;
2036 if (num_ranges > 32)
2037 { // This is size of array in VPE.API
2038 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2039 goto reply;
2040 }
2041
2042 vec_reset_length (low_ports);
2043 vec_reset_length (high_ports);
2044
2045 for (i = 0; i < num_ranges; i++)
2046 {
2047 tmp_low = mp->low_ports[i];
2048 tmp_high = mp->high_ports[i];
2049 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2050 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2051 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2052 {
2053 rv = VNET_API_ERROR_INVALID_VALUE;
2054 goto reply;
2055 }
2056 vec_add1 (low_ports, tmp_low);
2057 vec_add1 (high_ports, tmp_high + 1);
2058 }
2059
2060 // Validate mask_length
2061 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2062 {
2063 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2064 goto reply;
2065 }
2066
2067 vrf_id = ntohl (mp->vrf_id);
2068
2069 if (vrf_id < 1)
2070 {
2071 rv = VNET_API_ERROR_INVALID_VALUE;
2072 goto reply;
2073 }
2074
2075
2076 if (is_ipv6)
2077 {
2078 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2079 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2080 mask_length,
2081 vrf_id,
2082 low_ports,
2083 high_ports, is_add);
2084 }
2085 else
2086 {
2087 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2088 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2089 mask_length,
2090 vrf_id,
2091 low_ports,
2092 high_ports, is_add);
2093 }
2094
2095reply:
2096 vec_free (low_ports);
2097 vec_free (high_ports);
2098 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2099}
2100
2101static void
2102 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2103 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2104{
2105 vlib_main_t *vm = vlib_get_main ();
2106 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2107 ip4_main_t *im = &ip4_main;
2108 int rv;
2109 u32 sw_if_index;
2110 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2111 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2112 uword *p = 0;
2113 int i;
2114
2115 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2116 ntohl (mp->tcp_out_vrf_id);
2117 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2118 ntohl (mp->udp_out_vrf_id);
2119 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2120 ntohl (mp->tcp_in_vrf_id);
2121 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2122 ntohl (mp->udp_in_vrf_id);
2123
2124
2125 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2126 {
2127 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2128 {
2129 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2130
2131 if (p == 0)
2132 {
2133 rv = VNET_API_ERROR_INVALID_VALUE;
2134 goto reply;
2135 }
2136
2137 fib_index[i] = p[0];
2138 }
2139 else
2140 fib_index[i] = ~0;
2141 }
2142 sw_if_index = ntohl (mp->sw_if_index);
2143
2144 VALIDATE_SW_IF_INDEX (mp);
2145
2146 rv =
2147 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2148 mp->is_add);
2149
2150 BAD_SW_IF_INDEX_LABEL;
2151reply:
2152
2153 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2154}
2155
Chore3460b012018-11-28 10:53:11 +03302156typedef union
2157{
2158 u32 fib_index;
2159} ip4_source_check_config_t;
2160
2161static void
2162 vl_api_ip_source_check_interface_add_del_t_handler
2163 (vl_api_ip_source_check_interface_add_del_t * mp)
2164{
2165 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
2166 int rv;
2167 u32 sw_if_index = ntohl (mp->sw_if_index);
2168 u8 is_add = mp->is_add;
2169 char *feature_name =
2170 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
2171
2172 ip4_source_check_config_t config;
2173
2174 VALIDATE_SW_IF_INDEX (mp);
2175
2176 config.fib_index =
2177 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
2178 rv =
2179 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
2180 is_add, &config, sizeof (config));
2181 BAD_SW_IF_INDEX_LABEL;
2182
2183 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
2184}
2185
Neale Rannsb8d44812017-11-10 06:53:54 -08002186#define IP4_ARP_EVENT 3
2187#define IP6_ND_EVENT 4
2188
2189static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2190static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2191static vlib_node_registration_t ip_resolver_process_node;
2192
2193static void
2194handle_ip4_arp_event (u32 pool_index)
2195{
2196 vpe_api_main_t *vam = &vpe_api_main;
2197 vnet_main_t *vnm = vam->vnet_main;
2198 vlib_main_t *vm = vam->vlib_main;
2199 vl_api_ip4_arp_event_t *event;
2200 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002201 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002202
2203 /* Client can cancel, die, etc. */
2204 if (pool_is_free_index (vam->arp_events, pool_index))
2205 return;
2206
2207 event = pool_elt_at_index (vam->arp_events, pool_index);
2208
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002209 reg = vl_api_client_index_to_registration (event->client_index);
2210 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002211 {
2212 (void) vnet_add_del_ip4_arp_change_event
2213 (vnm, arp_change_delete_callback,
2214 event->pid, &event->address,
2215 ip_resolver_process_node.index, IP4_ARP_EVENT,
2216 ~0 /* pool index, notused */ , 0 /* is_add */ );
2217 return;
2218 }
2219
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002220 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002221 {
2222 mp = vl_msg_api_alloc (sizeof (*mp));
2223 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002224 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002225 }
2226 else
2227 {
2228 static f64 last_time;
2229 /*
2230 * Throttle syslog msgs.
2231 * It's pretty tempting to just revoke the registration...
2232 */
2233 if (vlib_time_now (vm) > last_time + 10.0)
2234 {
2235 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2236 format_ip4_address, &event->address, event->pid);
2237 last_time = vlib_time_now (vm);
2238 }
2239 }
2240}
2241
Neale Rannsf12dad62018-06-04 18:41:24 -07002242static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002243handle_ip6_nd_event (u32 pool_index)
2244{
2245 vpe_api_main_t *vam = &vpe_api_main;
2246 vnet_main_t *vnm = vam->vnet_main;
2247 vlib_main_t *vm = vam->vlib_main;
2248 vl_api_ip6_nd_event_t *event;
2249 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002250 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002251
2252 /* Client can cancel, die, etc. */
2253 if (pool_is_free_index (vam->nd_events, pool_index))
2254 return;
2255
2256 event = pool_elt_at_index (vam->nd_events, pool_index);
2257
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002258 reg = vl_api_client_index_to_registration (event->client_index);
2259 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002260 {
2261 (void) vnet_add_del_ip6_nd_change_event
2262 (vnm, nd_change_delete_callback,
2263 event->pid, &event->address,
2264 ip_resolver_process_node.index, IP6_ND_EVENT,
2265 ~0 /* pool index, notused */ , 0 /* is_add */ );
2266 return;
2267 }
2268
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002269 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002270 {
2271 mp = vl_msg_api_alloc (sizeof (*mp));
2272 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002273 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002274 }
2275 else
2276 {
2277 static f64 last_time;
2278 /*
2279 * Throttle syslog msgs.
2280 * It's pretty tempting to just revoke the registration...
2281 */
2282 if (vlib_time_now (vm) > last_time + 10.0)
2283 {
2284 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2285 format_ip6_address, &event->address, event->pid);
2286 last_time = vlib_time_now (vm);
2287 }
2288 }
2289}
2290
2291static uword
2292resolver_process (vlib_main_t * vm,
2293 vlib_node_runtime_t * rt, vlib_frame_t * f)
2294{
2295 volatile f64 timeout = 100.0;
2296 volatile uword *event_data = 0;
2297
2298 while (1)
2299 {
2300 vlib_process_wait_for_event_or_clock (vm, timeout);
2301
2302 uword event_type =
2303 vlib_process_get_events (vm, (uword **) & event_data);
2304
2305 int i;
2306 switch (event_type)
2307 {
2308 case IP4_ARP_EVENT:
2309 for (i = 0; i < vec_len (event_data); i++)
2310 handle_ip4_arp_event (event_data[i]);
2311 break;
2312
2313 case IP6_ND_EVENT:
2314 for (i = 0; i < vec_len (event_data); i++)
2315 handle_ip6_nd_event (event_data[i]);
2316 break;
2317
2318 case ~0: /* timeout */
2319 break;
2320 }
2321
2322 vec_reset_length (event_data);
2323 }
2324 return 0; /* or not */
2325}
2326
2327/* *INDENT-OFF* */
2328VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2329 .function = resolver_process,
2330 .type = VLIB_NODE_TYPE_PROCESS,
2331 .name = "ip-route-resolver-process",
2332};
2333/* *INDENT-ON* */
2334
2335static int
2336nd_change_data_callback (u32 pool_index, u8 * new_mac,
2337 u32 sw_if_index, ip6_address_t * address)
2338{
2339 vpe_api_main_t *am = &vpe_api_main;
2340 vl_api_ip6_nd_event_t *event;
2341
2342 if (pool_is_free_index (am->nd_events, pool_index))
2343 return 1;
2344
2345 event = pool_elt_at_index (am->nd_events, pool_index);
2346 if (eth_mac_equal (event->new_mac, new_mac) &&
2347 sw_if_index == ntohl (event->sw_if_index))
2348 {
2349 return 1;
2350 }
2351
2352 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2353 event->sw_if_index = htonl (sw_if_index);
2354 return 0;
2355}
2356
2357static int
2358arp_change_delete_callback (u32 pool_index, u8 * notused)
2359{
2360 vpe_api_main_t *am = &vpe_api_main;
2361
2362 if (pool_is_free_index (am->arp_events, pool_index))
2363 return 1;
2364
2365 pool_put_index (am->arp_events, pool_index);
2366 return 0;
2367}
2368
2369static int
2370nd_change_delete_callback (u32 pool_index, u8 * notused)
2371{
2372 vpe_api_main_t *am = &vpe_api_main;
2373
2374 if (pool_is_free_index (am->nd_events, pool_index))
2375 return 1;
2376
2377 pool_put_index (am->nd_events, pool_index);
2378 return 0;
2379}
2380
2381static vlib_node_registration_t wc_arp_process_node;
2382
2383enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002384{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002385
2386static uword
2387wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2388{
2389 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2390 * and need to be volatile - to prevent them from being optimized into
2391 * a register - which could change during suspension */
2392
2393 volatile wc_arp_report_t arp_prev = { 0 };
2394 volatile wc_nd_report_t nd_prev = { 0 };
2395 volatile f64 last_arp = vlib_time_now (vm);
2396 volatile f64 last_nd = vlib_time_now (vm);
2397
2398 while (1)
2399 {
2400 vlib_process_wait_for_event (vm);
2401 uword event_type = WC_ARP_REPORT;
2402 void *event_data = vlib_process_get_event_data (vm, &event_type);
2403
2404 f64 now = vlib_time_now (vm);
2405 int i;
2406 if (event_type == WC_ARP_REPORT)
2407 {
2408 wc_arp_report_t *arp_events = event_data;
2409 for (i = 0; i < vec_len (arp_events); i++)
2410 {
2411 /* discard dup event */
2412 if (arp_prev.ip4 == arp_events[i].ip4 &&
2413 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2414 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2415 (now - last_arp) < 10.0)
2416 {
2417 continue;
2418 }
2419 arp_prev = arp_events[i];
2420 last_arp = now;
2421 vpe_client_registration_t *reg;
2422 /* *INDENT-OFF* */
2423 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2424 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002425 vl_api_registration_t *vl_reg;
2426 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002427 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002428 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002429 {
2430 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002431 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002432 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2433 event->client_index = reg->client_index;
2434 event->pid = reg->client_pid;
2435 event->mac_ip = 1;
2436 event->address = arp_events[i].ip4;
2437 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2438 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002439 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002440 }
2441 }));
2442 /* *INDENT-ON* */
2443 }
2444 }
2445 else if (event_type == WC_ND_REPORT)
2446 {
2447 wc_nd_report_t *nd_events = event_data;
2448 for (i = 0; i < vec_len (nd_events); i++)
2449 {
2450 /* discard dup event */
2451 if (ip6_address_is_equal
2452 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2453 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2454 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2455 && (now - last_nd) < 10.0)
2456 {
2457 continue;
2458 }
2459 nd_prev = nd_events[i];
2460 last_nd = now;
2461 vpe_client_registration_t *reg;
2462 /* *INDENT-OFF* */
2463 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2464 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002465 vl_api_registration_t *vl_reg;
2466 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2467 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002468 {
2469 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002470 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002471 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2472 event->client_index = reg->client_index;
2473 event->pid = reg->client_pid;
2474 event->mac_ip = 1;
2475 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2476 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2477 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002478 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002479 }
2480 }));
2481 /* *INDENT-ON* */
2482 }
2483 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002484 else if (event_type == RA_REPORT)
2485 {
2486 ra_report_t *ra_events = event_data;
2487 for (i = 0; i < vec_len (ra_events); i++)
2488 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002489 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2490 call_ip6_neighbor_callbacks (&ra_events[i],
2491 npm->ra_report_functions);
2492
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002493 vpe_client_registration_t *reg;
2494 /* *INDENT-OFF* */
2495 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2496 ({
2497 vl_api_registration_t *vl_reg;
2498 vl_reg =
2499 vl_api_client_index_to_registration (reg->client_index);
2500 if (vl_reg && vl_api_can_send_msg (vl_reg))
2501 {
2502 u32 event_size =
2503 sizeof (vl_api_ip6_ra_event_t) +
2504 vec_len (ra_events[i].prefixes) *
2505 sizeof (vl_api_ip6_ra_prefix_info_t);
2506 vl_api_ip6_ra_event_t *event =
2507 vl_msg_api_alloc (event_size);
Dave Barachb7b92992018-10-17 10:38:51 -04002508 clib_memset (event, 0, event_size);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002509 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2510 event->client_index = reg->client_index;
2511 event->pid = reg->client_pid;
2512
2513 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2514
2515 memcpy (event->router_address, ra_events[i].router_address, 16);
2516
2517 event->current_hop_limit = ra_events[i].current_hop_limit;
2518 event->flags = ra_events[i].flags;
2519 event->router_lifetime_in_sec =
2520 clib_host_to_net_u16 (ra_events
2521 [i].router_lifetime_in_sec);
2522 event->neighbor_reachable_time_in_msec =
2523 clib_host_to_net_u32 (ra_events
2524 [i].neighbor_reachable_time_in_msec);
2525 event->time_in_msec_between_retransmitted_neighbor_solicitations
2526 =
2527 clib_host_to_net_u32 (ra_events
2528 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2529
2530 event->n_prefixes =
2531 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2532 vl_api_ip6_ra_prefix_info_t *prefix =
2533 (typeof (prefix)) event->prefixes;
2534 u32 j;
2535 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2536 {
2537 ra_report_prefix_info_t *info =
2538 &ra_events[i].prefixes[j];
2539 memcpy (prefix->dst_address, info->dst_address.as_u8,
2540 16);
2541 prefix->dst_address_length = info->dst_address_length;
2542 prefix->flags = info->flags;
2543 prefix->valid_time =
2544 clib_host_to_net_u32 (info->valid_time);
2545 prefix->preferred_time =
2546 clib_host_to_net_u32 (info->preferred_time);
2547 prefix++;
2548 }
2549
2550 vl_api_send_msg (vl_reg, (u8 *) event);
2551 }
2552 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002553 /* *INDENT-ON* */
2554 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002555 }
2556 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002557 vlib_process_put_event_data (vm, event_data);
2558 }
2559
2560 return 0;
2561}
2562
2563/* *INDENT-OFF* */
2564VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2565 .function = wc_arp_process,
2566 .type = VLIB_NODE_TYPE_PROCESS,
2567 .name = "wildcard-ip4-arp-publisher-process",
2568};
2569/* *INDENT-ON* */
2570
2571static int
2572arp_change_data_callback (u32 pool_index, u8 * new_mac,
2573 u32 sw_if_index, u32 address)
2574{
2575 vpe_api_main_t *am = &vpe_api_main;
2576 vl_api_ip4_arp_event_t *event;
2577
2578 if (pool_is_free_index (am->arp_events, pool_index))
2579 return 1;
2580
2581 event = pool_elt_at_index (am->arp_events, pool_index);
2582 if (eth_mac_equal (event->new_mac, new_mac) &&
2583 sw_if_index == ntohl (event->sw_if_index))
2584 {
2585 return 1;
2586 }
2587
2588 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2589 event->sw_if_index = htonl (sw_if_index);
2590 return 0;
2591}
2592
2593static void
2594vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2595{
2596 vpe_api_main_t *am = &vpe_api_main;
2597 vnet_main_t *vnm = vnet_get_main ();
2598 vl_api_want_ip4_arp_events_reply_t *rmp;
2599 int rv = 0;
2600
2601 if (mp->address == 0)
2602 {
2603 uword *p =
2604 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2605 vpe_client_registration_t *rp;
2606 if (p)
2607 {
2608 if (mp->enable_disable)
2609 {
2610 clib_warning ("pid %d: already enabled...", mp->pid);
2611 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2612 goto reply;
2613 }
2614 else
2615 {
2616 rp =
2617 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2618 pool_put (am->wc_ip4_arp_events_registrations, rp);
2619 hash_unset (am->wc_ip4_arp_events_registration_hash,
2620 mp->client_index);
2621 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002622 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002623 goto reply;
2624 }
2625 }
2626 if (mp->enable_disable == 0)
2627 {
2628 clib_warning ("pid %d: already disabled...", mp->pid);
2629 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2630 goto reply;
2631 }
2632 pool_get (am->wc_ip4_arp_events_registrations, rp);
2633 rp->client_index = mp->client_index;
2634 rp->client_pid = mp->pid;
2635 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2636 rp - am->wc_ip4_arp_events_registrations);
2637 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2638 goto reply;
2639 }
2640
2641 if (mp->enable_disable)
2642 {
2643 vl_api_ip4_arp_event_t *event;
2644 pool_get (am->arp_events, event);
2645 rv = vnet_add_del_ip4_arp_change_event
2646 (vnm, arp_change_data_callback,
2647 mp->pid, &mp->address /* addr, in net byte order */ ,
2648 ip_resolver_process_node.index,
2649 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2650
2651 if (rv)
2652 {
2653 pool_put (am->arp_events, event);
2654 goto reply;
2655 }
Dave Barachb7b92992018-10-17 10:38:51 -04002656 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002657
2658 /* Python API expects events to have no context */
2659 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2660 event->client_index = mp->client_index;
2661 event->address = mp->address;
2662 event->pid = mp->pid;
2663 if (mp->address == 0)
2664 event->mac_ip = 1;
2665 }
2666 else
2667 {
2668 rv = vnet_add_del_ip4_arp_change_event
2669 (vnm, arp_change_delete_callback,
2670 mp->pid, &mp->address /* addr, in net byte order */ ,
2671 ip_resolver_process_node.index,
2672 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2673 }
2674reply:
2675 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2676}
2677
Neale Rannsf12dad62018-06-04 18:41:24 -07002678static clib_error_t *
2679want_ip4_arp_events_reaper (u32 client_index)
2680{
2681 vpe_client_registration_t *rp;
2682 vl_api_ip4_arp_event_t *event;
2683 u32 *to_delete, *event_id;
2684 vpe_api_main_t *am;
2685 vnet_main_t *vnm;
2686 uword *p;
2687
2688 am = &vpe_api_main;
2689 vnm = vnet_get_main ();
2690 to_delete = NULL;
2691
2692 /* clear out all of its pending resolutions */
2693 /* *INDENT-OFF* */
2694 pool_foreach(event, am->arp_events,
2695 ({
2696 if (event->client_index == client_index)
2697 {
2698 vec_add1(to_delete, event - am->arp_events);
2699 }
2700 }));
2701 /* *INDENT-ON* */
2702
2703 vec_foreach (event_id, to_delete)
2704 {
2705 event = pool_elt_at_index (am->arp_events, *event_id);
2706 vnet_add_del_ip4_arp_change_event
2707 (vnm, arp_change_delete_callback,
2708 event->pid, &event->address,
2709 ip_resolver_process_node.index, IP4_ARP_EVENT,
2710 ~0 /* pool index, notused */ , 0 /* is_add */ );
2711 }
2712 vec_free (to_delete);
2713
2714 /* remove from the registration hash */
2715 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2716
2717 if (p)
2718 {
2719 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2720 pool_put (am->wc_ip4_arp_events_registrations, rp);
2721 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2722 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2723 wc_arp_set_publisher_node (~0, REPORT_MAX);
2724 }
2725 return (NULL);
2726}
2727
2728VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2729
Neale Rannsb8d44812017-11-10 06:53:54 -08002730static void
2731vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2732{
2733 vpe_api_main_t *am = &vpe_api_main;
2734 vnet_main_t *vnm = vnet_get_main ();
2735 vl_api_want_ip6_nd_events_reply_t *rmp;
2736 int rv = 0;
2737
2738 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2739 {
2740 uword *p =
2741 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2742 vpe_client_registration_t *rp;
2743 if (p)
2744 {
2745 if (mp->enable_disable)
2746 {
2747 clib_warning ("pid %d: already enabled...", mp->pid);
2748 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2749 goto reply;
2750 }
2751 else
2752 {
2753 rp =
2754 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2755 pool_put (am->wc_ip6_nd_events_registrations, rp);
2756 hash_unset (am->wc_ip6_nd_events_registration_hash,
2757 mp->client_index);
2758 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002759 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002760 goto reply;
2761 }
2762 }
2763 if (mp->enable_disable == 0)
2764 {
2765 clib_warning ("pid %d: already disabled...", mp->pid);
2766 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2767 goto reply;
2768 }
2769 pool_get (am->wc_ip6_nd_events_registrations, rp);
2770 rp->client_index = mp->client_index;
2771 rp->client_pid = mp->pid;
2772 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2773 rp - am->wc_ip6_nd_events_registrations);
2774 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2775 goto reply;
2776 }
2777
2778 if (mp->enable_disable)
2779 {
2780 vl_api_ip6_nd_event_t *event;
2781 pool_get (am->nd_events, event);
2782
2783 rv = vnet_add_del_ip6_nd_change_event
2784 (vnm, nd_change_data_callback,
2785 mp->pid, mp->address /* addr, in net byte order */ ,
2786 ip_resolver_process_node.index,
2787 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2788
2789 if (rv)
2790 {
2791 pool_put (am->nd_events, event);
2792 goto reply;
2793 }
Dave Barachb7b92992018-10-17 10:38:51 -04002794 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002795
2796 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2797 event->client_index = mp->client_index;
2798 clib_memcpy (event->address, mp->address, sizeof event->address);
2799 event->pid = mp->pid;
2800 }
2801 else
2802 {
2803 rv = vnet_add_del_ip6_nd_change_event
2804 (vnm, nd_change_delete_callback,
2805 mp->pid, mp->address /* addr, in net byte order */ ,
2806 ip_resolver_process_node.index,
2807 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2808 }
2809reply:
2810 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2811}
2812
Neale Rannsf12dad62018-06-04 18:41:24 -07002813static clib_error_t *
2814want_ip6_nd_events_reaper (u32 client_index)
2815{
2816
2817 vpe_client_registration_t *rp;
2818 vl_api_ip6_nd_event_t *event;
2819 u32 *to_delete, *event_id;
2820 vpe_api_main_t *am;
2821 vnet_main_t *vnm;
2822 uword *p;
2823
2824 am = &vpe_api_main;
2825 vnm = vnet_get_main ();
2826 to_delete = NULL;
2827
2828 /* clear out all of its pending resolutions */
2829 /* *INDENT-OFF* */
2830 pool_foreach(event, am->nd_events,
2831 ({
2832 if (event->client_index == client_index)
2833 {
2834 vec_add1(to_delete, event - am->nd_events);
2835 }
2836 }));
2837 /* *INDENT-ON* */
2838
2839 vec_foreach (event_id, to_delete)
2840 {
2841 event = pool_elt_at_index (am->nd_events, *event_id);
2842 vnet_add_del_ip6_nd_change_event
2843 (vnm, nd_change_delete_callback,
2844 event->pid, &event->address,
2845 ip_resolver_process_node.index, IP6_ND_EVENT,
2846 ~0 /* pool index, notused */ , 0 /* is_add */ );
2847 }
2848 vec_free (to_delete);
2849
2850 /* remove from the registration hash */
2851 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2852
2853 if (p)
2854 {
2855 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2856 pool_put (am->wc_ip6_nd_events_registrations, rp);
2857 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2858 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2859 wc_nd_set_publisher_node (~0, REPORT_MAX);
2860 }
2861 return (NULL);
2862}
2863
2864VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2865
Neale Rannsb8d44812017-11-10 06:53:54 -08002866static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002867vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2868{
2869 vpe_api_main_t *am = &vpe_api_main;
2870 vl_api_want_ip6_ra_events_reply_t *rmp;
2871 int rv = 0;
2872
2873 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2874 vpe_client_registration_t *rp;
2875 if (p)
2876 {
2877 if (mp->enable_disable)
2878 {
2879 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2880 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2881 goto reply;
2882 }
2883 else
2884 {
2885 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2886 pool_put (am->ip6_ra_events_registrations, rp);
2887 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002888 goto reply;
2889 }
2890 }
2891 if (mp->enable_disable == 0)
2892 {
2893 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2894 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2895 goto reply;
2896 }
2897 pool_get (am->ip6_ra_events_registrations, rp);
2898 rp->client_index = mp->client_index;
2899 rp->client_pid = ntohl (mp->pid);
2900 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2901 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002902
2903reply:
2904 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2905}
2906
Neale Rannsf12dad62018-06-04 18:41:24 -07002907static clib_error_t *
2908want_ip6_ra_events_reaper (u32 client_index)
2909{
2910 vpe_api_main_t *am = &vpe_api_main;
2911 vpe_client_registration_t *rp;
2912 uword *p;
2913
2914 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2915
2916 if (p)
2917 {
2918 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2919 pool_put (am->ip6_ra_events_registrations, rp);
2920 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2921 }
2922 return (NULL);
2923}
2924
2925VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2926
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002927static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002928vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2929{
2930 vl_api_proxy_arp_add_del_reply_t *rmp;
2931 u32 fib_index;
2932 int rv;
2933 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002934 uword *p;
2935
Ole Troan3288ed72017-12-06 17:00:05 +01002936 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002937
Neale Ranns0053de62018-05-22 08:40:52 -07002938 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002939
2940 if (!p)
2941 {
2942 rv = VNET_API_ERROR_NO_SUCH_FIB;
2943 goto out;
2944 }
2945
2946 fib_index = p[0];
2947
Neale Ranns0053de62018-05-22 08:40:52 -07002948 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2949 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002950 fib_index, mp->is_add == 0);
2951
2952out:
Ole Troan3288ed72017-12-06 17:00:05 +01002953 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002954 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2955}
2956
Neale Ranns0053de62018-05-22 08:40:52 -07002957typedef struct proxy_arp_walk_ctx_t_
2958{
2959 vl_api_registration_t *reg;
2960 u32 context;
2961} proxy_arp_walk_ctx_t;
2962
2963static walk_rc_t
2964send_proxy_arp_details (const ip4_address_t * lo_addr,
2965 const ip4_address_t * hi_addr,
2966 u32 fib_index, void *data)
2967{
2968 vl_api_proxy_arp_details_t *mp;
2969 proxy_arp_walk_ctx_t *ctx;
2970
2971 ctx = data;
2972
2973 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04002974 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07002975 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2976 mp->context = ctx->context;
2977 mp->proxy.vrf_id = htonl (fib_index);
2978 clib_memcpy (mp->proxy.low_address, lo_addr,
2979 sizeof (mp->proxy.low_address));
2980 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2981
2982 vl_api_send_msg (ctx->reg, (u8 *) mp);
2983
2984 return (WALK_CONTINUE);
2985}
2986
2987static void
2988vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2989{
2990 vl_api_registration_t *reg;
2991
2992 reg = vl_api_client_index_to_registration (mp->client_index);
2993 if (!reg)
2994 return;
2995
2996 proxy_arp_walk_ctx_t wctx = {
2997 .reg = reg,
2998 .context = mp->context,
2999 };
3000
3001 proxy_arp_walk (send_proxy_arp_details, &wctx);
3002}
3003
3004static walk_rc_t
3005send_proxy_arp_intfc_details (vnet_main_t * vnm,
3006 vnet_sw_interface_t * si, void *data)
3007{
3008 vl_api_proxy_arp_intfc_details_t *mp;
3009 proxy_arp_walk_ctx_t *ctx;
3010
3011 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
3012 return (WALK_CONTINUE);
3013
3014 ctx = data;
3015
3016 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003017 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003018 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
3019 mp->context = ctx->context;
3020 mp->sw_if_index = htonl (si->sw_if_index);
3021
3022 vl_api_send_msg (ctx->reg, (u8 *) mp);
3023
3024 return (WALK_CONTINUE);
3025}
3026
3027static void
3028vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
3029{
3030 vl_api_registration_t *reg;
3031
3032 reg = vl_api_client_index_to_registration (mp->client_index);
3033 if (!reg)
3034 return;
3035
3036 proxy_arp_walk_ctx_t wctx = {
3037 .reg = reg,
3038 .context = mp->context,
3039 };
3040
3041 vnet_sw_interface_walk (vnet_get_main (),
3042 send_proxy_arp_intfc_details, &wctx);
3043}
3044
Neale Rannsb8d44812017-11-10 06:53:54 -08003045static void
3046 vl_api_proxy_arp_intfc_enable_disable_t_handler
3047 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
3048{
3049 int rv = 0;
3050 vnet_main_t *vnm = vnet_get_main ();
3051 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
3052
3053 VALIDATE_SW_IF_INDEX (mp);
3054
3055 vnet_sw_interface_t *si =
3056 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
3057
3058 ASSERT (si);
3059
3060 if (mp->enable_disable)
3061 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3062 else
3063 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3064
3065 BAD_SW_IF_INDEX_LABEL;
3066
3067 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3068}
3069
John Loc7b43042018-04-13 16:46:22 -04003070static void
3071vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3072{
3073 int rv = 0;
3074 vlib_main_t *vm = vlib_get_main ();
3075 vl_api_ip_probe_neighbor_reply_t *rmp;
3076 clib_error_t *error;
3077
3078 VALIDATE_SW_IF_INDEX (mp);
3079
3080 u32 sw_if_index = ntohl (mp->sw_if_index);
3081
3082 if (mp->is_ipv6)
3083 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003084 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003085 else
3086 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003087 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003088
3089 if (error)
3090 {
3091 clib_error_report (error);
3092 rv = clib_error_get_code (error);
3093 }
3094
3095 BAD_SW_IF_INDEX_LABEL;
3096
3097 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3098}
3099
John Lo7f358b32018-04-28 01:19:24 -04003100static void
3101 vl_api_ip_scan_neighbor_enable_disable_t_handler
3102 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3103{
3104 int rv = 0;
3105 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3106 ip_neighbor_scan_arg_t arg;
3107
3108 arg.mode = mp->mode;
3109 arg.scan_interval = mp->scan_interval;
3110 arg.max_proc_time = mp->max_proc_time;
3111 arg.max_update = mp->max_update;
3112 arg.scan_int_delay = mp->scan_int_delay;
3113 arg.stale_threshold = mp->stale_threshold;
3114 ip_neighbor_scan_enable_disable (&arg);
3115
3116 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3117}
3118
Neale Rannsb8d44812017-11-10 06:53:54 -08003119static int
3120ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3121{
3122 vnet_main_t *vnm = vnet_get_main ();
3123 vnet_interface_main_t *im = &vnm->interface_main;
3124 ip4_main_t *im4 = &ip4_main;
3125 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003126 fib_table_t *fib_table;
3127 ip4_fib_t *fib;
3128 u32 sw_if_index;
3129 int i;
3130 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3131 u32 target_fib_id = ntohl (mp->vrf_id);
3132
Ole Troan3288ed72017-12-06 17:00:05 +01003133 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003134
3135 /* *INDENT-OFF* */
3136 pool_foreach (fib_table, im4->fibs,
3137 ({
3138 vnet_sw_interface_t * si;
3139
3140 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3141
3142 if (fib->table_id != target_fib_id)
3143 continue;
3144
3145 /* remove any mpls encap/decap labels */
3146 mpls_fib_reset_labels (fib->table_id);
3147
3148 /* remove any proxy arps in this fib */
3149 vnet_proxy_arp_fib_reset (fib->table_id);
3150
3151 /* Set the flow hash for this fib to the default */
3152 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3153
3154 vec_reset_length (sw_if_indices_to_shut);
3155
3156 /* Shut down interfaces in this FIB / clean out intfc routes */
3157 pool_foreach (si, im->sw_interfaces,
3158 ({
3159 u32 sw_if_index = si->sw_if_index;
3160
3161 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3162 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3163 fib->index))
3164 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3165 }));
3166
3167 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3168 sw_if_index = sw_if_indices_to_shut[i];
3169
3170 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3171 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3172 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3173 }
3174
3175 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3176
3177 rv = 0;
3178 break;
3179 })); /* pool_foreach (fib) */
3180 /* *INDENT-ON* */
3181
Ole Troan3288ed72017-12-06 17:00:05 +01003182 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003183 return rv;
3184}
3185
3186static int
3187ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3188{
3189 vnet_main_t *vnm = vnet_get_main ();
3190 vnet_interface_main_t *im = &vnm->interface_main;
3191 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003192 static u32 *sw_if_indices_to_shut;
3193 fib_table_t *fib_table;
3194 ip6_fib_t *fib;
3195 u32 sw_if_index;
3196 int i;
3197 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3198 u32 target_fib_id = ntohl (mp->vrf_id);
3199
Ole Troan3288ed72017-12-06 17:00:05 +01003200 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003201
3202 /* *INDENT-OFF* */
3203 pool_foreach (fib_table, im6->fibs,
3204 ({
3205 vnet_sw_interface_t * si;
3206
3207 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3208
3209 if (fib->table_id != target_fib_id)
3210 continue;
3211
3212 vec_reset_length (sw_if_indices_to_shut);
3213
3214 /* Set the flow hash for this fib to the default */
3215 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3216
3217 /* Shut down interfaces in this FIB / clean out intfc routes */
3218 pool_foreach (si, im->sw_interfaces,
3219 ({
3220 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3221 fib->index)
3222 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3223 }));
3224
3225 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3226 sw_if_index = sw_if_indices_to_shut[i];
3227
3228 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3229 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3230 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3231 }
3232
3233 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3234
3235 rv = 0;
3236 break;
3237 })); /* pool_foreach (fib) */
3238 /* *INDENT-ON* */
3239
Ole Troan3288ed72017-12-06 17:00:05 +01003240 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003241 return rv;
3242}
3243
3244static void
3245vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3246{
3247 int rv;
3248 vl_api_reset_fib_reply_t *rmp;
3249
3250 if (mp->is_ipv6)
3251 rv = ip6_reset_fib_t_handler (mp);
3252 else
3253 rv = ip4_reset_fib_t_handler (mp);
3254
3255 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3256}
3257
3258static void
3259vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3260{
3261 int rv;
3262 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3263 vnet_main_t *vnm = vnet_get_main ();
3264 clib_error_t *error;
3265
3266 vnm->api_errno = 0;
3267
3268 if (mp->is_ipv6)
3269 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3270 else
3271 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3272
3273 if (error)
3274 {
3275 clib_error_report (error);
3276 rv = VNET_API_ERROR_UNSPECIFIED;
3277 }
3278 else
3279 {
3280 rv = vnm->api_errno;
3281 }
3282
3283 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3284}
3285
Klement Sekera75e7d132017-09-20 08:26:30 +02003286void
3287vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3288{
3289 vl_api_ip_reassembly_set_reply_t *rmp;
3290 int rv = 0;
3291 if (mp->is_ip6)
3292 {
3293 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3294 clib_net_to_host_u32 (mp->max_reassemblies),
3295 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3296 }
3297 else
3298 {
3299 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3300 clib_net_to_host_u32 (mp->max_reassemblies),
3301 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3302 }
3303
3304 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3305}
3306
3307void
3308vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3309{
3310 unix_shared_memory_queue_t *q;
3311
3312 q = vl_api_client_index_to_input_queue (mp->client_index);
3313
3314 if (q == 0)
3315 return;
3316
3317 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04003318 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02003319 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3320 rmp->context = mp->context;
3321 rmp->retval = 0;
3322 if (mp->is_ip6)
3323 {
3324 rmp->is_ip6 = 1;
3325 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3326 &rmp->expire_walk_interval_ms);
3327 }
3328 else
3329 {
3330 rmp->is_ip6 = 0;
3331 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3332 &rmp->expire_walk_interval_ms);
3333 }
3334 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3335 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3336 rmp->expire_walk_interval_ms =
3337 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3338 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3339}
3340
Klement Sekera4c533132018-02-22 11:41:12 +01003341void
3342 vl_api_ip_reassembly_enable_disable_t_handler
3343 (vl_api_ip_reassembly_enable_disable_t * mp)
3344{
3345 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3346 int rv = 0;
3347 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3348 mp->enable_ip4);
3349 if (0 == rv)
3350 {
3351 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3352 mp->enable_ip6);
3353 }
3354
Choree7f61d12018-11-28 10:27:58 +03303355 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01003356}
3357
Pavel Kotucek609e1212018-11-27 09:59:44 +01003358void
3359send_ip_punt_redirect_details (vl_api_registration_t * reg,
3360 u32 context, u32 sw_if_index,
3361 ip_punt_redirect_rx_t * pr, u8 is_ipv6)
3362{
3363 vl_api_ip_punt_redirect_details_t *mp;
3364
3365 mp = vl_msg_api_alloc (sizeof (*mp));
3366 if (!mp)
3367 return;
3368
3369 clib_memset (mp, 0, sizeof (*mp));
3370 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
3371 mp->context = context;
3372 mp->punt.rx_sw_if_index = htonl (sw_if_index);
3373 mp->punt.tx_sw_if_index = htonl (pr->tx_sw_if_index);
3374 if (is_ipv6)
3375 {
3376 ip_address_encode (&pr->nh, IP46_TYPE_IP6, &mp->punt.nh);
3377 }
3378 else
3379 {
3380 ip_address_encode (&pr->nh, IP46_TYPE_IP4, &mp->punt.nh);
3381 }
3382
3383 vl_api_send_msg (reg, (u8 *) mp);
3384}
3385
3386static void
3387vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
3388{
3389 vl_api_registration_t *reg;
3390 u32 sw_if_index;
3391 int rv __attribute__ ((unused)) = 0;
3392
3393 sw_if_index = ntohl (mp->sw_if_index);
3394 reg = vl_api_client_index_to_registration (mp->client_index);
3395 if (!reg)
3396 return;
3397
3398 if (~0 != sw_if_index)
3399 VALIDATE_SW_IF_INDEX (mp);
3400
3401 ip_punt_redirect_detail_t *pr, *prs;
3402 if (mp->is_ipv6)
3403 {
3404 prs = ip6_punt_redirect_entries (sw_if_index);
3405 /* *INDENT-OFF* */
3406 vec_foreach (pr, prs)
3407 {
3408 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 1);
3409 }
3410 /* *INDENT-ON* */
3411 vec_free (prs);
3412 }
3413 else
3414 {
3415 prs = ip4_punt_redirect_entries (sw_if_index);
3416 /* *INDENT-OFF* */
3417 vec_foreach (pr, prs)
3418 {
3419 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 0);
3420 }
3421 /* *INDENT-ON* */
3422 vec_free (prs);
3423 }
3424
3425 BAD_SW_IF_INDEX_LABEL;
3426}
3427
Dave Barachb5e8a772016-12-06 12:04:42 -05003428#define vl_msg_name_crc_list
3429#include <vnet/ip/ip.api.h>
3430#undef vl_msg_name_crc_list
3431
3432static void
3433setup_message_id_table (api_main_t * am)
3434{
3435#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3436 foreach_vl_msg_name_crc_ip;
3437#undef _
3438}
3439
3440static clib_error_t *
3441ip_api_hookup (vlib_main_t * vm)
3442{
3443 api_main_t *am = &api_main;
3444
3445#define _(N,n) \
3446 vl_msg_api_set_handlers(VL_API_##N, #n, \
3447 vl_api_##n##_t_handler, \
3448 vl_noop_handler, \
3449 vl_api_##n##_t_endian, \
3450 vl_api_##n##_t_print, \
3451 sizeof(vl_api_##n##_t), 1);
3452 foreach_ip_api_msg;
3453#undef _
3454
3455 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00003456 * Mark the route add/del API as MP safe
3457 */
3458 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3459 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE_REPLY] = 1;
3460
3461 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05003462 * Set up the (msg_name, crc, message-id) table
3463 */
3464 setup_message_id_table (am);
3465
Juraj Sloboda52574522018-05-03 10:03:50 +02003466 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3467
Dave Barachb5e8a772016-12-06 12:04:42 -05003468 return 0;
3469}
3470
3471VLIB_API_INIT_FUNCTION (ip_api_hookup);
3472
3473/*
3474 * fd.io coding-style-patch-verification: ON
3475 *
3476 * Local Variables:
3477 * eval: (c-set-style "gnu")
3478 * End:
3479 */