blob: f58ca079b041bdbaae07fc25aac0861183216a37 [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)
816 path_flags |= FIB_ROUTE_PATH_LOCAL;
Neale Rannsf068c3e2018-01-03 04:18:48 -0800817 if (is_dvr)
818 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500819 if (is_resolve_host)
820 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
821 if (is_resolve_attached)
822 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800823 if (is_interface_rx)
824 path_flags |= FIB_ROUTE_PATH_INTF_RX;
825 if (is_rpf_id)
826 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700827 if (is_source_lookup)
828 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800829 if (is_multicast)
830 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800831 if (is_udp_encap)
832 {
833 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
834 path.frp_udp_encap_id = next_hop_id;
835 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800836 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
837 && path.frp_fib_index != ~0)
838 {
839 path_flags |= FIB_ROUTE_PATH_DEAG;
840 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500841
842 path.frp_flags = path_flags;
843
Dave Barachb5e8a772016-12-06 12:04:42 -0500844 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
845
846 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
847 {
848 /*
849 * special route types that link directly to the adj
850 */
851 if (is_add)
852 {
853 dpo_id_t dpo = DPO_INVALID;
854 dpo_proto_t dproto;
855
856 dproto = fib_proto_to_dpo (prefix->fp_proto);
857
858 if (is_drop)
859 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
860 else if (is_local)
861 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
862 else if (is_unreach)
863 ip_null_dpo_add_and_lock (dproto,
864 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
865 else if (is_prohibit)
866 ip_null_dpo_add_and_lock (dproto,
867 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
868 &dpo);
869 else if (is_classify)
870 {
871 if (pool_is_free_index (cm->tables,
872 ntohl (classify_table_index)))
873 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500874 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500875 return VNET_API_ERROR_NO_SUCH_TABLE;
876 }
877
878 dpo_set (&dpo, DPO_CLASSIFY, dproto,
879 classify_dpo_create (dproto,
880 ntohl (classify_table_index)));
881 }
882 else
883 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500884 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500885 return VNET_API_ERROR_NO_SUCH_TABLE;
886 }
887
888 fib_table_entry_special_dpo_update (fib_index,
889 prefix,
890 FIB_SOURCE_API,
891 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
892 dpo_reset (&dpo);
893 }
894 else
895 {
896 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
897 }
898 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700899 else if (is_multipath)
900 {
901 vec_add1 (paths, path);
902
903 if (is_add)
904 fib_table_entry_path_add2 (fib_index,
905 prefix,
906 FIB_SOURCE_API, entry_flags, paths);
907 else
908 fib_table_entry_path_remove2 (fib_index,
909 prefix, FIB_SOURCE_API, paths);
910
911 vec_free (paths);
912 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500913 else
914 {
915 if (is_add)
916 {
917 vec_add1 (paths, path);
918 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800919 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500920 vec_free (paths);
921 }
922 else
923 {
924 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
925 }
926 }
927
Dave Barachd7cb1b52016-12-09 09:52:16 -0500928 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500929 return (0);
930}
931
932int
933add_del_route_check (fib_protocol_t table_proto,
934 u32 table_id,
935 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700936 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500937 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800938 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500939{
940 vnet_main_t *vnm = vnet_get_main ();
941
942 *fib_index = fib_table_find (table_proto, ntohl (table_id));
943 if (~0 == *fib_index)
944 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800945 /* No such VRF, and we weren't asked to create one */
946 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500947 }
948
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800949 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500950 {
951 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
952 ntohl (next_hop_sw_if_index)))
953 {
954 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
955 }
956 }
957 else
958 {
Neale Rannsda78f952017-05-24 09:15:43 -0700959 fib_protocol_t fib_nh_proto;
960
961 if (next_hop_table_proto > DPO_PROTO_MPLS)
962 return (0);
963
964 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
965
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800966 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700967 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800968 ntohl (next_hop_table_id));
969 else
Neale Rannsda78f952017-05-24 09:15:43 -0700970 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800971 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500972
973 if (~0 == *next_hop_fib_index)
974 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800975 /* No such VRF, and we weren't asked to create one */
976 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500977 }
978 }
979
980 return (0);
981}
982
983static int
Neale Ranns008dbe12018-09-07 09:32:36 -0700984ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
985 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500986{
987 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800988 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500989 int rv, ii, n_labels;;
990
991 rv = add_del_route_check (FIB_PROTOCOL_IP4,
992 mp->table_id,
993 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700994 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500995 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700996 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500997
998 if (0 != rv)
999 return (rv);
1000
1001 fib_prefix_t pfx = {
1002 .fp_len = mp->dst_address_length,
1003 .fp_proto = FIB_PROTOCOL_IP4,
1004 };
1005 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1006
1007 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001008 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001009 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1010
1011 n_labels = mp->next_hop_n_out_labels;
1012 if (n_labels == 0)
1013 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001014 else
1015 {
1016 vec_validate (label_stack, n_labels - 1);
1017 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001018 {
1019 label_stack[ii].fml_value =
1020 ntohl (mp->next_hop_out_label_stack[ii].label);
1021 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1022 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1023 label_stack[ii].fml_mode =
1024 (mp->next_hop_out_label_stack[ii].is_uniform ?
1025 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1026 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001027 }
1028
Neale Ranns008dbe12018-09-07 09:32:36 -07001029 rv = add_del_route_t_handler (mp->is_multipath,
1030 mp->is_add,
1031 mp->is_drop,
1032 mp->is_unreach,
1033 mp->is_prohibit,
1034 mp->is_local, 0,
1035 mp->is_classify,
1036 mp->classify_table_index,
1037 mp->is_resolve_host,
1038 mp->is_resolve_attached, 0, 0,
1039 mp->is_dvr,
1040 mp->is_source_lookup,
1041 mp->is_udp_encap,
1042 fib_index, &pfx, DPO_PROTO_IP4,
1043 &nh,
1044 ntohl (mp->next_hop_id),
1045 ntohl (mp->next_hop_sw_if_index),
1046 next_hop_fib_index,
1047 mp->next_hop_weight,
1048 mp->next_hop_preference,
1049 ntohl (mp->next_hop_via_label), label_stack);
1050
1051 if (mp->is_add && 0 == rv)
1052 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1053
1054 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001055}
1056
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001057static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001058ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1059 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001060{
Neale Ranns31ed7442018-02-23 05:29:09 -08001061 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001062 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001063 int rv, ii, n_labels;;
1064
1065 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1066 mp->table_id,
1067 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001068 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001069 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001070 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001071
1072 if (0 != rv)
1073 return (rv);
1074
1075 fib_prefix_t pfx = {
1076 .fp_len = mp->dst_address_length,
1077 .fp_proto = FIB_PROTOCOL_IP6,
1078 };
1079 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1080
1081 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001082 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001083 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1084
1085 n_labels = mp->next_hop_n_out_labels;
1086 if (n_labels == 0)
1087 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001088 else
1089 {
1090 vec_validate (label_stack, n_labels - 1);
1091 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001092 {
1093 label_stack[ii].fml_value =
1094 ntohl (mp->next_hop_out_label_stack[ii].label);
1095 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1096 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1097 label_stack[ii].fml_mode =
1098 (mp->next_hop_out_label_stack[ii].is_uniform ?
1099 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1100 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001101 }
1102
Neale Ranns008dbe12018-09-07 09:32:36 -07001103 rv = add_del_route_t_handler (mp->is_multipath,
1104 mp->is_add,
1105 mp->is_drop,
1106 mp->is_unreach,
1107 mp->is_prohibit,
1108 mp->is_local, 0,
1109 mp->is_classify,
1110 mp->classify_table_index,
1111 mp->is_resolve_host,
1112 mp->is_resolve_attached, 0, 0,
1113 mp->is_dvr,
1114 mp->is_source_lookup,
1115 mp->is_udp_encap,
1116 fib_index, &pfx, DPO_PROTO_IP6,
1117 &nh, ntohl (mp->next_hop_id),
1118 ntohl (mp->next_hop_sw_if_index),
1119 next_hop_fib_index,
1120 mp->next_hop_weight,
1121 mp->next_hop_preference,
1122 ntohl (mp->next_hop_via_label), label_stack);
1123
1124 if (mp->is_add && 0 == rv)
1125 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1126
1127 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001128}
1129
1130void
1131vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1132{
1133 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001134 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001135 int rv;
1136 vnet_main_t *vnm = vnet_get_main ();
1137
1138 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001139 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001140
1141 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001142 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001143 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001144 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001145
1146 rv = (rv == 0) ? vnm->api_errno : rv;
1147
Neale Ranns008dbe12018-09-07 09:32:36 -07001148 /* *INDENT-OFF* */
1149 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1150 ({
1151 rmp->stats_index = htonl (stats_index);
1152 }))
1153 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001154}
1155
Neale Ranns15002542017-09-10 04:39:11 -07001156void
Neale Ranns2297af02017-09-12 09:45:04 -07001157ip_table_create (fib_protocol_t fproto,
1158 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001159{
1160 u32 fib_index, mfib_index;
1161
1162 /*
1163 * ignore action on the default table - this is always present
1164 * and cannot be added nor deleted from the API
1165 */
1166 if (0 != table_id)
1167 {
1168 /*
1169 * The API holds only one lock on the table.
1170 * i.e. it can be added many times via the API but needs to be
1171 * deleted only once.
1172 * The FIB index for unicast and multicast is not necessarily the
1173 * same, since internal VPP systesm (like LISP and SR) create
1174 * their own unicast tables.
1175 */
1176 fib_index = fib_table_find (fproto, table_id);
1177 mfib_index = mfib_table_find (fproto, table_id);
1178
1179 if (~0 == fib_index)
1180 {
Neale Ranns2297af02017-09-12 09:45:04 -07001181 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1182 (is_api ?
1183 FIB_SOURCE_API :
1184 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001185 }
1186 if (~0 == mfib_index)
1187 {
Neale Ranns2297af02017-09-12 09:45:04 -07001188 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1189 (is_api ?
1190 MFIB_SOURCE_API :
1191 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001192 }
1193 }
1194}
1195
Neale Ranns32e1c012016-11-22 17:07:28 +00001196static int
1197add_del_mroute_check (fib_protocol_t table_proto,
1198 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001199 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001200{
1201 vnet_main_t *vnm = vnet_get_main ();
1202
1203 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1204 if (~0 == *fib_index)
1205 {
Neale Ranns15002542017-09-10 04:39:11 -07001206 /* No such table */
1207 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001208 }
1209
1210 if (~0 != ntohl (next_hop_sw_if_index))
1211 {
1212 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1213 ntohl (next_hop_sw_if_index)))
1214 {
1215 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1216 }
1217 }
1218
1219 return (0);
1220}
1221
Neale Ranns28c142e2018-09-07 09:37:07 -07001222static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001223mroute_add_del_handler (u8 is_add,
1224 u8 is_local,
1225 u32 fib_index,
1226 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001227 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001228 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001229 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001230 u32 next_hop_sw_if_index,
1231 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001232{
Neale Ranns28c142e2018-09-07 09:37:07 -07001233 fib_node_index_t mfib_entry_index = ~0;
1234
Neale Ranns32e1c012016-11-22 17:07:28 +00001235 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1236
1237 fib_route_path_t path = {
1238 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001239 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001240 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001241 };
1242
1243 if (is_local)
1244 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1245
Neale Rannsd792d9c2017-10-21 10:53:20 -07001246 if (DPO_PROTO_BIER == nh_proto)
1247 {
1248 path.frp_bier_imp = bier_imp;
1249 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1250 }
1251 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001252 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001253 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1254 MFIB_SOURCE_API,
1255 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001256 goto done;
1257 }
1258
1259 if (is_add)
1260 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001261 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1262 MFIB_SOURCE_API,
1263 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001264 }
1265 else
1266 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001267 mfib_table_entry_path_remove (fib_index, prefix,
1268 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001269 }
1270
Neale Rannsd792d9c2017-10-21 10:53:20 -07001271done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001272 stats_dsunlock ();
Neale Ranns28c142e2018-09-07 09:37:07 -07001273 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001274}
1275
1276static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001277api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1278 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001279{
Neale Ranns28c142e2018-09-07 09:37:07 -07001280 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001281 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001282 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001283 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001284 u32 fib_index;
1285 int rv;
1286
Neale Rannsd792d9c2017-10-21 10:53:20 -07001287 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001288 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1289 rv = add_del_mroute_check (fproto,
1290 mp->table_id,
1291 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001292 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001293
1294 if (0 != rv)
1295 return (rv);
1296
1297 mfib_prefix_t pfx = {
1298 .fp_len = ntohs (mp->grp_address_length),
1299 .fp_proto = fproto,
1300 };
1301
1302 if (FIB_PROTOCOL_IP4 == fproto)
1303 {
1304 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1305 sizeof (pfx.fp_grp_addr.ip4));
1306 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1307 sizeof (pfx.fp_src_addr.ip4));
Dave Barachb7b92992018-10-17 10:38:51 -04001308 clib_memset (&nh.ip6, 0, sizeof (nh.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001309 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001310 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1311 pfx.fp_len = 64;
Neale Ranns32e1c012016-11-22 17:07:28 +00001312 }
1313 else
1314 {
1315 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1316 sizeof (pfx.fp_grp_addr.ip6));
1317 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1318 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001319 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001320 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1321 pfx.fp_len = 256;
Neale Ranns32e1c012016-11-22 17:07:28 +00001322 }
1323
Neale Ranns28c142e2018-09-07 09:37:07 -07001324 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1325 mp->is_local,
1326 fib_index, &pfx,
1327 nh_proto,
1328 ntohl (mp->entry_flags),
1329 ntohl (mp->rpf_id),
1330 ntohl (mp->next_hop_sw_if_index),
1331 &nh,
1332 ntohl (mp->itf_flags),
1333 ntohl (mp->bier_imp));
1334
1335 if (~0 != mfib_entry_index)
1336 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1337
1338 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001339}
1340
1341void
1342vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1343{
1344 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001345 vnet_main_t *vnm;
1346 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001347 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001348
Neale Ranns28c142e2018-09-07 09:37:07 -07001349 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001350 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001351 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001352
Neale Ranns28c142e2018-09-07 09:37:07 -07001353 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001354
Neale Ranns28c142e2018-09-07 09:37:07 -07001355 /* *INDENT-OFF* */
1356 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1357 ({
1358 rmp->stats_index = htonl (stats_index);
1359 }));
1360 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001361}
1362
Dave Barachb5e8a772016-12-06 12:04:42 -05001363static void
1364send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001365 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1366 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001367{
1368 vl_api_ip_details_t *mp;
1369
1370 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001371 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001372 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1373
1374 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001375 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001376 mp->context = context;
1377
Florin Coras6c4dae22018-01-09 06:39:23 -08001378 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001379}
1380
1381static void
1382send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001383 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001384 u8 * ip, u16 prefix_length,
1385 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001386{
1387 vl_api_ip_address_details_t *mp;
1388
1389 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001390 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001391 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1392
1393 if (is_ipv6)
1394 {
1395 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1396 }
1397 else
1398 {
1399 u32 *tp = (u32 *) mp->ip;
1400 *tp = *(u32 *) ip;
1401 }
1402 mp->prefix_length = prefix_length;
1403 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001404 mp->sw_if_index = htonl (sw_if_index);
1405 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001406
Florin Coras6c4dae22018-01-09 06:39:23 -08001407 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001408}
1409
1410static void
1411vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1412{
1413 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001414 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001415 ip6_address_t *r6;
1416 ip4_address_t *r4;
1417 ip6_main_t *im6 = &ip6_main;
1418 ip4_main_t *im4 = &ip4_main;
1419 ip_lookup_main_t *lm6 = &im6->lookup_main;
1420 ip_lookup_main_t *lm4 = &im4->lookup_main;
1421 ip_interface_address_t *ia = 0;
1422 u32 sw_if_index = ~0;
1423 int rv __attribute__ ((unused)) = 0;
1424
1425 VALIDATE_SW_IF_INDEX (mp);
1426
1427 sw_if_index = ntohl (mp->sw_if_index);
1428
Florin Coras6c4dae22018-01-09 06:39:23 -08001429 reg = vl_api_client_index_to_registration (mp->client_index);
1430 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001431 return;
1432
Dave Barachb5e8a772016-12-06 12:04:42 -05001433 if (mp->is_ipv6)
1434 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001435 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001436 /* Do not send subnet details of the IP-interface for
1437 * unnumbered interfaces. otherwise listening clients
1438 * will be confused that the subnet is applied on more
1439 * than one interface */
1440 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001441 ({
1442 r6 = ip_interface_address_get_address (lm6, ia);
1443 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001444 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001445 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001446 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001447 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001448 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001449 else
1450 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001451 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001452 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001453 ({
1454 r4 = ip_interface_address_get_address (lm4, ia);
1455 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001456 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001457 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001458 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001459 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001460 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001461
Dave Barachb5e8a772016-12-06 12:04:42 -05001462 BAD_SW_IF_INDEX_LABEL;
1463}
1464
1465static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001466send_ip_unnumbered_details (vpe_api_main_t * am,
1467 vl_api_registration_t * reg,
1468 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1469{
1470 vl_api_ip_unnumbered_details_t *mp;
1471
1472 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001473 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001474 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1475
1476 mp->context = context;
1477 mp->sw_if_index = htonl (sw_if_index);
1478 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1479
1480 vl_api_send_msg (reg, (u8 *) mp);
1481}
1482
1483static void
1484vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1485{
1486 vnet_main_t *vnm = vnet_get_main ();
1487 vnet_interface_main_t *im = &vnm->interface_main;
1488 int rv __attribute__ ((unused)) = 0;
1489 vpe_api_main_t *am = &vpe_api_main;
1490 vl_api_registration_t *reg;
1491 vnet_sw_interface_t *si;
1492 u32 sw_if_index;
1493
1494 sw_if_index = ntohl (mp->sw_if_index);
1495
1496 reg = vl_api_client_index_to_registration (mp->client_index);
1497 if (!reg)
1498 return;
1499
1500 if (~0 != sw_if_index)
1501 {
1502 VALIDATE_SW_IF_INDEX (mp);
1503
1504 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1505
1506 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1507 {
1508 send_ip_unnumbered_details (am, reg,
1509 sw_if_index,
1510 si->unnumbered_sw_if_index,
1511 mp->context);
1512 }
1513 }
1514 else
1515 {
1516 /* *INDENT-OFF* */
1517 pool_foreach (si, im->sw_interfaces,
1518 ({
1519 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1520 {
1521 send_ip_unnumbered_details(am, reg,
1522 si->sw_if_index,
1523 si->unnumbered_sw_if_index,
1524 mp->context);
1525 }
1526 }));
1527 /* *INDENT-ON* */
1528 }
1529
1530 BAD_SW_IF_INDEX_LABEL;
1531}
1532
1533static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001534vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1535{
1536 vpe_api_main_t *am = &vpe_api_main;
1537 vnet_main_t *vnm = vnet_get_main ();
1538 vlib_main_t *vm = vlib_get_main ();
1539 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001540 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001541 vnet_sw_interface_t *si, *sorted_sis;
1542 u32 sw_if_index = ~0;
1543
Florin Coras6c4dae22018-01-09 06:39:23 -08001544 reg = vl_api_client_index_to_registration (mp->client_index);
1545 if (!reg)
1546 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001547
1548 /* Gather interfaces. */
1549 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1550 _vec_len (sorted_sis) = 0;
1551 /* *INDENT-OFF* */
1552 pool_foreach (si, im->sw_interfaces,
1553 ({
1554 vec_add1 (sorted_sis, si[0]);
1555 }));
1556 /* *INDENT-ON* */
1557
1558 vec_foreach (si, sorted_sis)
1559 {
1560 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1561 {
1562 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1563 {
1564 continue;
1565 }
1566 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001567 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001568 }
1569 }
1570}
1571
1572static void
1573set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1574{
1575 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001576 int rv;
1577 u32 table_id;
1578 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001579
Neale Ranns227038a2017-04-21 01:07:59 -07001580 table_id = ntohl (mp->vrf_id);
1581
1582#define _(a,b) if (mp->a) flow_hash_config |= b;
1583 foreach_flow_hash_bit;
1584#undef _
1585
1586 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001587
1588 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1589}
1590
1591static void
1592set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1593{
1594 vl_api_set_ip_flow_hash_reply_t *rmp;
1595 int rv;
1596 u32 table_id;
1597 flow_hash_config_t flow_hash_config = 0;
1598
1599 table_id = ntohl (mp->vrf_id);
1600
1601#define _(a,b) if (mp->a) flow_hash_config |= b;
1602 foreach_flow_hash_bit;
1603#undef _
1604
1605 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1606
1607 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1608}
1609
1610
1611static void
1612vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1613{
1614 if (mp->is_ipv6 == 0)
1615 set_ip4_flow_hash (mp);
1616 else
1617 set_ip6_flow_hash (mp);
1618}
1619
1620static void
1621 vl_api_sw_interface_ip6nd_ra_config_t_handler
1622 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1623{
1624 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1625 vlib_main_t *vm = vlib_get_main ();
1626 int rv = 0;
1627 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1628 default_router;
1629
1630 is_no = mp->is_no == 1;
1631 suppress = mp->suppress == 1;
1632 managed = mp->managed == 1;
1633 other = mp->other == 1;
1634 ll_option = mp->ll_option == 1;
1635 send_unicast = mp->send_unicast == 1;
1636 cease = mp->cease == 1;
1637 default_router = mp->default_router == 1;
1638
1639 VALIDATE_SW_IF_INDEX (mp);
1640
1641 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1642 suppress, managed, other,
1643 ll_option, send_unicast, cease,
1644 default_router, ntohl (mp->lifetime),
1645 ntohl (mp->initial_count),
1646 ntohl (mp->initial_interval),
1647 ntohl (mp->max_interval),
1648 ntohl (mp->min_interval), is_no);
1649
1650 BAD_SW_IF_INDEX_LABEL;
1651
1652 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1653}
1654
1655static void
1656 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1657 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1658{
1659 vlib_main_t *vm = vlib_get_main ();
1660 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1661 int rv = 0;
1662 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1663
1664 VALIDATE_SW_IF_INDEX (mp);
1665
1666 is_no = mp->is_no == 1;
1667 use_default = mp->use_default == 1;
1668 no_advertise = mp->no_advertise == 1;
1669 off_link = mp->off_link == 1;
1670 no_autoconfig = mp->no_autoconfig == 1;
1671 no_onlink = mp->no_onlink == 1;
1672
1673 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1674 (ip6_address_t *) mp->address,
1675 mp->address_length, use_default,
1676 ntohl (mp->val_lifetime),
1677 ntohl (mp->pref_lifetime), no_advertise,
1678 off_link, no_autoconfig, no_onlink, is_no);
1679
1680 BAD_SW_IF_INDEX_LABEL;
1681 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1682}
1683
1684static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001685send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001686 u32 context,
1687 const ip46_address_t * addr, u32 sw_if_index)
1688{
1689 vl_api_ip6nd_proxy_details_t *mp;
1690
1691 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001692 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns3f844d02017-02-18 00:03:54 -08001693 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1694 mp->context = context;
1695 mp->sw_if_index = htonl (sw_if_index);
1696 memcpy (mp->address, addr, 16);
1697
Florin Coras6c4dae22018-01-09 06:39:23 -08001698 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001699}
1700
Neale Ranns3f844d02017-02-18 00:03:54 -08001701typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1702{
1703 u32 *indices;
1704} api_ip6nd_proxy_fib_table_walk_ctx_t;
1705
Neale Ranns89541992017-04-06 04:41:02 -07001706static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001707api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1708{
1709 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1710
1711 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1712 {
1713 vec_add1 (ctx->indices, fei);
1714 }
1715
Neale Ranns89541992017-04-06 04:41:02 -07001716 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001717}
1718
1719static void
1720vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1721{
1722 ip6_main_t *im6 = &ip6_main;
1723 fib_table_t *fib_table;
1724 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1725 .indices = NULL,
1726 };
1727 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001728 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001729 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001730
Florin Coras6c4dae22018-01-09 06:39:23 -08001731 reg = vl_api_client_index_to_registration (mp->client_index);
1732 if (!reg)
1733 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001734
1735 /* *INDENT-OFF* */
1736 pool_foreach (fib_table, im6->fibs,
1737 ({
1738 fib_table_walk(fib_table->ft_index,
1739 FIB_PROTOCOL_IP6,
1740 api_ip6nd_proxy_fib_table_walk,
1741 &ctx);
1742 }));
1743 /* *INDENT-ON* */
1744
1745 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1746
1747 vec_foreach (feip, ctx.indices)
1748 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001749 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001750
Florin Coras6c4dae22018-01-09 06:39:23 -08001751 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001752 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001753 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001754 fib_entry_get_resolving_interface (*feip));
1755 }
1756
1757 vec_free (ctx.indices);
1758}
1759
1760static void
1761vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1762{
1763 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1764 int rv = 0;
1765
1766 VALIDATE_SW_IF_INDEX (mp);
1767
1768 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1769 (ip6_address_t *) mp->address, mp->is_del);
1770
1771 BAD_SW_IF_INDEX_LABEL;
1772 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1773}
1774
1775static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001776 vl_api_ip6nd_send_router_solicitation_t_handler
1777 (vl_api_ip6nd_send_router_solicitation_t * mp)
1778{
1779 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1780 icmp6_send_router_solicitation_params_t params;
1781 vlib_main_t *vm = vlib_get_main ();
1782 int rv = 0;
1783
1784 VALIDATE_SW_IF_INDEX (mp);
1785
1786 BAD_SW_IF_INDEX_LABEL;
1787 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1788
1789 if (rv != 0)
1790 return;
1791
1792 params.irt = ntohl (mp->irt);
1793 params.mrt = ntohl (mp->mrt);
1794 params.mrc = ntohl (mp->mrc);
1795 params.mrd = ntohl (mp->mrd);
1796
1797 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1798 &params);
1799}
1800
1801static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001802 vl_api_sw_interface_ip6_enable_disable_t_handler
1803 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1804{
1805 vlib_main_t *vm = vlib_get_main ();
1806 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1807 vnet_main_t *vnm = vnet_get_main ();
1808 int rv = 0;
1809 clib_error_t *error;
1810
1811 vnm->api_errno = 0;
1812
1813 VALIDATE_SW_IF_INDEX (mp);
1814
1815 error =
1816 (mp->enable == 1) ? enable_ip6_interface (vm,
1817 ntohl (mp->sw_if_index)) :
1818 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1819
1820 if (error)
1821 {
1822 clib_error_report (error);
1823 rv = VNET_API_ERROR_UNSPECIFIED;
1824 }
1825 else
1826 {
1827 rv = vnm->api_errno;
1828 }
1829
1830 BAD_SW_IF_INDEX_LABEL;
1831
1832 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1833}
1834
Neale Ranns32e1c012016-11-22 17:07:28 +00001835void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001836vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001837 u32 context, const mfib_signal_t * mfs)
1838{
1839 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001840 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001841 mfib_table_t *mfib;
1842 mfib_itf_t *mfi;
1843
1844 mp = vl_msg_api_alloc (sizeof (*mp));
1845
Dave Barachb7b92992018-10-17 10:38:51 -04001846 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001847 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1848 mp->context = context;
1849
1850 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001851 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001852 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001853 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001854 mp->table_id = ntohl (mfib->mft_table_id);
1855 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1856
Neale Ranns9e829a82018-12-17 05:50:32 -08001857 if (FIB_PROTOCOL_IP4 == prefix->fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +00001858 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001859 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001860
Neale Ranns9e829a82018-12-17 05:50:32 -08001861 memcpy (mp->grp_address, &prefix->fp_grp_addr.ip4, 4);
1862 if (prefix->fp_len > 32)
Neale Ranns32e1c012016-11-22 17:07:28 +00001863 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001864 memcpy (mp->src_address, &prefix->fp_src_addr.ip4, 4);
Neale Ranns32e1c012016-11-22 17:07:28 +00001865 }
1866 }
1867 else
1868 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001869 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001870
1871 ASSERT (0);
1872 }
1873
1874 if (0 != mfs->mfs_buffer_len)
1875 {
1876 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1877
1878 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1879 }
1880 else
1881 {
1882 mp->ip_packet_len = 0;
1883 }
1884
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001885 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001886}
1887
1888static void
1889vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1890{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001891 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001892
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001893 reg = vl_api_client_index_to_registration (mp->client_index);
1894 if (!reg)
1895 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001896
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001897 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001898 ;
1899}
Dave Barachb5e8a772016-12-06 12:04:42 -05001900
Florin Coras595992c2017-11-06 17:17:08 -08001901static void
1902 vl_api_ip_container_proxy_add_del_t_handler
1903 (vl_api_ip_container_proxy_add_del_t * mp)
1904{
1905 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1906 vnet_ip_container_proxy_args_t args;
1907 int rv = 0;
1908 clib_error_t *error;
1909
Dave Barachb7b92992018-10-17 10:38:51 -04001910 clib_memset (&args, 0, sizeof (args));
Florin Coras595992c2017-11-06 17:17:08 -08001911 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1912 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1913 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1914 args.is_add = mp->is_add;
1915 if ((error = vnet_ip_container_proxy_add_del (&args)))
1916 {
1917 rv = clib_error_get_code (error);
1918 clib_error_report (error);
1919 }
1920
1921 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1922}
1923
Matus Fabian75b9f452018-10-02 23:27:21 -07001924typedef struct ip_container_proxy_walk_ctx_t_
1925{
1926 vl_api_registration_t *reg;
1927 u32 context;
1928} ip_container_proxy_walk_ctx_t;
1929
1930static int
1931ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1932 void *args)
1933{
1934 vl_api_ip_container_proxy_details_t *mp;
1935 ip_container_proxy_walk_ctx_t *ctx = args;
1936
1937 mp = vl_msg_api_alloc (sizeof (*mp));
1938 if (!mp)
1939 return 1;
1940
Dave Barachb7b92992018-10-17 10:38:51 -04001941 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001942 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1943 mp->context = ctx->context;
1944
1945 mp->sw_if_index = ntohl (sw_if_index);
1946 ip_prefix_encode (pfx, &mp->prefix);
1947
1948 vl_api_send_msg (ctx->reg, (u8 *) mp);
1949
1950 return 1;
1951}
1952
1953static void
1954vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1955 mp)
1956{
1957 vl_api_registration_t *reg;
1958
1959 reg = vl_api_client_index_to_registration (mp->client_index);
1960 if (!reg)
1961 return;
1962
1963 ip_container_proxy_walk_ctx_t ctx = {
1964 .context = mp->context,
1965 .reg = reg,
1966 };
1967
1968 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1969}
1970
Neale Rannsb8d44812017-11-10 06:53:54 -08001971static void
1972vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1973{
1974 int rv = 0;
1975 vl_api_ioam_enable_reply_t *rmp;
1976 clib_error_t *error;
1977
1978 /* Ignoring the profile id as currently a single profile
1979 * is supported */
1980 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1981 mp->seqno, mp->analyse);
1982 if (error)
1983 {
1984 clib_error_report (error);
1985 rv = clib_error_get_code (error);
1986 }
1987
1988 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1989}
1990
1991static void
1992vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1993{
1994 int rv = 0;
1995 vl_api_ioam_disable_reply_t *rmp;
1996 clib_error_t *error;
1997
1998 error = clear_ioam_rewrite_fn ();
1999 if (error)
2000 {
2001 clib_error_report (error);
2002 rv = clib_error_get_code (error);
2003 }
2004
2005 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2006}
2007
2008static void
2009 vl_api_ip_source_and_port_range_check_add_del_t_handler
2010 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2011{
2012 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2013 int rv = 0;
2014
2015 u8 is_ipv6 = mp->is_ipv6;
2016 u8 is_add = mp->is_add;
2017 u8 mask_length = mp->mask_length;
2018 ip4_address_t ip4_addr;
2019 ip6_address_t ip6_addr;
2020 u16 *low_ports = 0;
2021 u16 *high_ports = 0;
2022 u32 vrf_id;
2023 u16 tmp_low, tmp_high;
2024 u8 num_ranges;
2025 int i;
2026
2027 // Validate port range
2028 num_ranges = mp->number_of_ranges;
2029 if (num_ranges > 32)
2030 { // This is size of array in VPE.API
2031 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2032 goto reply;
2033 }
2034
2035 vec_reset_length (low_ports);
2036 vec_reset_length (high_ports);
2037
2038 for (i = 0; i < num_ranges; i++)
2039 {
2040 tmp_low = mp->low_ports[i];
2041 tmp_high = mp->high_ports[i];
2042 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2043 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2044 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2045 {
2046 rv = VNET_API_ERROR_INVALID_VALUE;
2047 goto reply;
2048 }
2049 vec_add1 (low_ports, tmp_low);
2050 vec_add1 (high_ports, tmp_high + 1);
2051 }
2052
2053 // Validate mask_length
2054 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2055 {
2056 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2057 goto reply;
2058 }
2059
2060 vrf_id = ntohl (mp->vrf_id);
2061
2062 if (vrf_id < 1)
2063 {
2064 rv = VNET_API_ERROR_INVALID_VALUE;
2065 goto reply;
2066 }
2067
2068
2069 if (is_ipv6)
2070 {
2071 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2072 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2073 mask_length,
2074 vrf_id,
2075 low_ports,
2076 high_ports, is_add);
2077 }
2078 else
2079 {
2080 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2081 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2082 mask_length,
2083 vrf_id,
2084 low_ports,
2085 high_ports, is_add);
2086 }
2087
2088reply:
2089 vec_free (low_ports);
2090 vec_free (high_ports);
2091 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2092}
2093
2094static void
2095 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2096 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2097{
2098 vlib_main_t *vm = vlib_get_main ();
2099 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2100 ip4_main_t *im = &ip4_main;
2101 int rv;
2102 u32 sw_if_index;
2103 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2104 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2105 uword *p = 0;
2106 int i;
2107
2108 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2109 ntohl (mp->tcp_out_vrf_id);
2110 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2111 ntohl (mp->udp_out_vrf_id);
2112 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2113 ntohl (mp->tcp_in_vrf_id);
2114 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2115 ntohl (mp->udp_in_vrf_id);
2116
2117
2118 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2119 {
2120 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2121 {
2122 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2123
2124 if (p == 0)
2125 {
2126 rv = VNET_API_ERROR_INVALID_VALUE;
2127 goto reply;
2128 }
2129
2130 fib_index[i] = p[0];
2131 }
2132 else
2133 fib_index[i] = ~0;
2134 }
2135 sw_if_index = ntohl (mp->sw_if_index);
2136
2137 VALIDATE_SW_IF_INDEX (mp);
2138
2139 rv =
2140 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2141 mp->is_add);
2142
2143 BAD_SW_IF_INDEX_LABEL;
2144reply:
2145
2146 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2147}
2148
Chore3460b012018-11-28 10:53:11 +03302149typedef union
2150{
2151 u32 fib_index;
2152} ip4_source_check_config_t;
2153
2154static void
2155 vl_api_ip_source_check_interface_add_del_t_handler
2156 (vl_api_ip_source_check_interface_add_del_t * mp)
2157{
2158 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
2159 int rv;
2160 u32 sw_if_index = ntohl (mp->sw_if_index);
2161 u8 is_add = mp->is_add;
2162 char *feature_name =
2163 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
2164
2165 ip4_source_check_config_t config;
2166
2167 VALIDATE_SW_IF_INDEX (mp);
2168
2169 config.fib_index =
2170 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
2171 rv =
2172 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
2173 is_add, &config, sizeof (config));
2174 BAD_SW_IF_INDEX_LABEL;
2175
2176 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
2177}
2178
Neale Rannsb8d44812017-11-10 06:53:54 -08002179#define IP4_ARP_EVENT 3
2180#define IP6_ND_EVENT 4
2181
2182static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2183static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2184static vlib_node_registration_t ip_resolver_process_node;
2185
2186static void
2187handle_ip4_arp_event (u32 pool_index)
2188{
2189 vpe_api_main_t *vam = &vpe_api_main;
2190 vnet_main_t *vnm = vam->vnet_main;
2191 vlib_main_t *vm = vam->vlib_main;
2192 vl_api_ip4_arp_event_t *event;
2193 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002194 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002195
2196 /* Client can cancel, die, etc. */
2197 if (pool_is_free_index (vam->arp_events, pool_index))
2198 return;
2199
2200 event = pool_elt_at_index (vam->arp_events, pool_index);
2201
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002202 reg = vl_api_client_index_to_registration (event->client_index);
2203 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002204 {
2205 (void) vnet_add_del_ip4_arp_change_event
2206 (vnm, arp_change_delete_callback,
2207 event->pid, &event->address,
2208 ip_resolver_process_node.index, IP4_ARP_EVENT,
2209 ~0 /* pool index, notused */ , 0 /* is_add */ );
2210 return;
2211 }
2212
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002213 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002214 {
2215 mp = vl_msg_api_alloc (sizeof (*mp));
2216 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002217 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002218 }
2219 else
2220 {
2221 static f64 last_time;
2222 /*
2223 * Throttle syslog msgs.
2224 * It's pretty tempting to just revoke the registration...
2225 */
2226 if (vlib_time_now (vm) > last_time + 10.0)
2227 {
2228 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2229 format_ip4_address, &event->address, event->pid);
2230 last_time = vlib_time_now (vm);
2231 }
2232 }
2233}
2234
Neale Rannsf12dad62018-06-04 18:41:24 -07002235static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002236handle_ip6_nd_event (u32 pool_index)
2237{
2238 vpe_api_main_t *vam = &vpe_api_main;
2239 vnet_main_t *vnm = vam->vnet_main;
2240 vlib_main_t *vm = vam->vlib_main;
2241 vl_api_ip6_nd_event_t *event;
2242 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002243 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002244
2245 /* Client can cancel, die, etc. */
2246 if (pool_is_free_index (vam->nd_events, pool_index))
2247 return;
2248
2249 event = pool_elt_at_index (vam->nd_events, pool_index);
2250
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002251 reg = vl_api_client_index_to_registration (event->client_index);
2252 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002253 {
2254 (void) vnet_add_del_ip6_nd_change_event
2255 (vnm, nd_change_delete_callback,
2256 event->pid, &event->address,
2257 ip_resolver_process_node.index, IP6_ND_EVENT,
2258 ~0 /* pool index, notused */ , 0 /* is_add */ );
2259 return;
2260 }
2261
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002262 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002263 {
2264 mp = vl_msg_api_alloc (sizeof (*mp));
2265 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002266 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002267 }
2268 else
2269 {
2270 static f64 last_time;
2271 /*
2272 * Throttle syslog msgs.
2273 * It's pretty tempting to just revoke the registration...
2274 */
2275 if (vlib_time_now (vm) > last_time + 10.0)
2276 {
2277 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2278 format_ip6_address, &event->address, event->pid);
2279 last_time = vlib_time_now (vm);
2280 }
2281 }
2282}
2283
2284static uword
2285resolver_process (vlib_main_t * vm,
2286 vlib_node_runtime_t * rt, vlib_frame_t * f)
2287{
2288 volatile f64 timeout = 100.0;
2289 volatile uword *event_data = 0;
2290
2291 while (1)
2292 {
2293 vlib_process_wait_for_event_or_clock (vm, timeout);
2294
2295 uword event_type =
2296 vlib_process_get_events (vm, (uword **) & event_data);
2297
2298 int i;
2299 switch (event_type)
2300 {
2301 case IP4_ARP_EVENT:
2302 for (i = 0; i < vec_len (event_data); i++)
2303 handle_ip4_arp_event (event_data[i]);
2304 break;
2305
2306 case IP6_ND_EVENT:
2307 for (i = 0; i < vec_len (event_data); i++)
2308 handle_ip6_nd_event (event_data[i]);
2309 break;
2310
2311 case ~0: /* timeout */
2312 break;
2313 }
2314
2315 vec_reset_length (event_data);
2316 }
2317 return 0; /* or not */
2318}
2319
2320/* *INDENT-OFF* */
2321VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2322 .function = resolver_process,
2323 .type = VLIB_NODE_TYPE_PROCESS,
2324 .name = "ip-route-resolver-process",
2325};
2326/* *INDENT-ON* */
2327
2328static int
2329nd_change_data_callback (u32 pool_index, u8 * new_mac,
2330 u32 sw_if_index, ip6_address_t * address)
2331{
2332 vpe_api_main_t *am = &vpe_api_main;
2333 vl_api_ip6_nd_event_t *event;
2334
2335 if (pool_is_free_index (am->nd_events, pool_index))
2336 return 1;
2337
2338 event = pool_elt_at_index (am->nd_events, pool_index);
2339 if (eth_mac_equal (event->new_mac, new_mac) &&
2340 sw_if_index == ntohl (event->sw_if_index))
2341 {
2342 return 1;
2343 }
2344
2345 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2346 event->sw_if_index = htonl (sw_if_index);
2347 return 0;
2348}
2349
2350static int
2351arp_change_delete_callback (u32 pool_index, u8 * notused)
2352{
2353 vpe_api_main_t *am = &vpe_api_main;
2354
2355 if (pool_is_free_index (am->arp_events, pool_index))
2356 return 1;
2357
2358 pool_put_index (am->arp_events, pool_index);
2359 return 0;
2360}
2361
2362static int
2363nd_change_delete_callback (u32 pool_index, u8 * notused)
2364{
2365 vpe_api_main_t *am = &vpe_api_main;
2366
2367 if (pool_is_free_index (am->nd_events, pool_index))
2368 return 1;
2369
2370 pool_put_index (am->nd_events, pool_index);
2371 return 0;
2372}
2373
2374static vlib_node_registration_t wc_arp_process_node;
2375
2376enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002377{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002378
2379static uword
2380wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2381{
2382 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2383 * and need to be volatile - to prevent them from being optimized into
2384 * a register - which could change during suspension */
2385
2386 volatile wc_arp_report_t arp_prev = { 0 };
2387 volatile wc_nd_report_t nd_prev = { 0 };
2388 volatile f64 last_arp = vlib_time_now (vm);
2389 volatile f64 last_nd = vlib_time_now (vm);
2390
2391 while (1)
2392 {
2393 vlib_process_wait_for_event (vm);
2394 uword event_type = WC_ARP_REPORT;
2395 void *event_data = vlib_process_get_event_data (vm, &event_type);
2396
2397 f64 now = vlib_time_now (vm);
2398 int i;
2399 if (event_type == WC_ARP_REPORT)
2400 {
2401 wc_arp_report_t *arp_events = event_data;
2402 for (i = 0; i < vec_len (arp_events); i++)
2403 {
2404 /* discard dup event */
2405 if (arp_prev.ip4 == arp_events[i].ip4 &&
2406 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2407 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2408 (now - last_arp) < 10.0)
2409 {
2410 continue;
2411 }
2412 arp_prev = arp_events[i];
2413 last_arp = now;
2414 vpe_client_registration_t *reg;
2415 /* *INDENT-OFF* */
2416 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2417 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002418 vl_api_registration_t *vl_reg;
2419 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002420 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002421 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002422 {
2423 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002424 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002425 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2426 event->client_index = reg->client_index;
2427 event->pid = reg->client_pid;
2428 event->mac_ip = 1;
2429 event->address = arp_events[i].ip4;
2430 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2431 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002432 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002433 }
2434 }));
2435 /* *INDENT-ON* */
2436 }
2437 }
2438 else if (event_type == WC_ND_REPORT)
2439 {
2440 wc_nd_report_t *nd_events = event_data;
2441 for (i = 0; i < vec_len (nd_events); i++)
2442 {
2443 /* discard dup event */
2444 if (ip6_address_is_equal
2445 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2446 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2447 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2448 && (now - last_nd) < 10.0)
2449 {
2450 continue;
2451 }
2452 nd_prev = nd_events[i];
2453 last_nd = now;
2454 vpe_client_registration_t *reg;
2455 /* *INDENT-OFF* */
2456 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2457 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002458 vl_api_registration_t *vl_reg;
2459 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2460 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002461 {
2462 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002463 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002464 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2465 event->client_index = reg->client_index;
2466 event->pid = reg->client_pid;
2467 event->mac_ip = 1;
2468 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2469 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2470 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002471 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002472 }
2473 }));
2474 /* *INDENT-ON* */
2475 }
2476 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002477 else if (event_type == RA_REPORT)
2478 {
2479 ra_report_t *ra_events = event_data;
2480 for (i = 0; i < vec_len (ra_events); i++)
2481 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002482 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2483 call_ip6_neighbor_callbacks (&ra_events[i],
2484 npm->ra_report_functions);
2485
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002486 vpe_client_registration_t *reg;
2487 /* *INDENT-OFF* */
2488 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2489 ({
2490 vl_api_registration_t *vl_reg;
2491 vl_reg =
2492 vl_api_client_index_to_registration (reg->client_index);
2493 if (vl_reg && vl_api_can_send_msg (vl_reg))
2494 {
2495 u32 event_size =
2496 sizeof (vl_api_ip6_ra_event_t) +
2497 vec_len (ra_events[i].prefixes) *
2498 sizeof (vl_api_ip6_ra_prefix_info_t);
2499 vl_api_ip6_ra_event_t *event =
2500 vl_msg_api_alloc (event_size);
Dave Barachb7b92992018-10-17 10:38:51 -04002501 clib_memset (event, 0, event_size);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002502 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2503 event->client_index = reg->client_index;
2504 event->pid = reg->client_pid;
2505
2506 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2507
2508 memcpy (event->router_address, ra_events[i].router_address, 16);
2509
2510 event->current_hop_limit = ra_events[i].current_hop_limit;
2511 event->flags = ra_events[i].flags;
2512 event->router_lifetime_in_sec =
2513 clib_host_to_net_u16 (ra_events
2514 [i].router_lifetime_in_sec);
2515 event->neighbor_reachable_time_in_msec =
2516 clib_host_to_net_u32 (ra_events
2517 [i].neighbor_reachable_time_in_msec);
2518 event->time_in_msec_between_retransmitted_neighbor_solicitations
2519 =
2520 clib_host_to_net_u32 (ra_events
2521 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2522
2523 event->n_prefixes =
2524 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2525 vl_api_ip6_ra_prefix_info_t *prefix =
2526 (typeof (prefix)) event->prefixes;
2527 u32 j;
2528 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2529 {
2530 ra_report_prefix_info_t *info =
2531 &ra_events[i].prefixes[j];
2532 memcpy (prefix->dst_address, info->dst_address.as_u8,
2533 16);
2534 prefix->dst_address_length = info->dst_address_length;
2535 prefix->flags = info->flags;
2536 prefix->valid_time =
2537 clib_host_to_net_u32 (info->valid_time);
2538 prefix->preferred_time =
2539 clib_host_to_net_u32 (info->preferred_time);
2540 prefix++;
2541 }
2542
2543 vl_api_send_msg (vl_reg, (u8 *) event);
2544 }
2545 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002546 /* *INDENT-ON* */
2547 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002548 }
2549 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002550 vlib_process_put_event_data (vm, event_data);
2551 }
2552
2553 return 0;
2554}
2555
2556/* *INDENT-OFF* */
2557VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2558 .function = wc_arp_process,
2559 .type = VLIB_NODE_TYPE_PROCESS,
2560 .name = "wildcard-ip4-arp-publisher-process",
2561};
2562/* *INDENT-ON* */
2563
2564static int
2565arp_change_data_callback (u32 pool_index, u8 * new_mac,
2566 u32 sw_if_index, u32 address)
2567{
2568 vpe_api_main_t *am = &vpe_api_main;
2569 vl_api_ip4_arp_event_t *event;
2570
2571 if (pool_is_free_index (am->arp_events, pool_index))
2572 return 1;
2573
2574 event = pool_elt_at_index (am->arp_events, pool_index);
2575 if (eth_mac_equal (event->new_mac, new_mac) &&
2576 sw_if_index == ntohl (event->sw_if_index))
2577 {
2578 return 1;
2579 }
2580
2581 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2582 event->sw_if_index = htonl (sw_if_index);
2583 return 0;
2584}
2585
2586static void
2587vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2588{
2589 vpe_api_main_t *am = &vpe_api_main;
2590 vnet_main_t *vnm = vnet_get_main ();
2591 vl_api_want_ip4_arp_events_reply_t *rmp;
2592 int rv = 0;
2593
2594 if (mp->address == 0)
2595 {
2596 uword *p =
2597 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2598 vpe_client_registration_t *rp;
2599 if (p)
2600 {
2601 if (mp->enable_disable)
2602 {
2603 clib_warning ("pid %d: already enabled...", mp->pid);
2604 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2605 goto reply;
2606 }
2607 else
2608 {
2609 rp =
2610 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2611 pool_put (am->wc_ip4_arp_events_registrations, rp);
2612 hash_unset (am->wc_ip4_arp_events_registration_hash,
2613 mp->client_index);
2614 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002615 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002616 goto reply;
2617 }
2618 }
2619 if (mp->enable_disable == 0)
2620 {
2621 clib_warning ("pid %d: already disabled...", mp->pid);
2622 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2623 goto reply;
2624 }
2625 pool_get (am->wc_ip4_arp_events_registrations, rp);
2626 rp->client_index = mp->client_index;
2627 rp->client_pid = mp->pid;
2628 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2629 rp - am->wc_ip4_arp_events_registrations);
2630 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2631 goto reply;
2632 }
2633
2634 if (mp->enable_disable)
2635 {
2636 vl_api_ip4_arp_event_t *event;
2637 pool_get (am->arp_events, event);
2638 rv = vnet_add_del_ip4_arp_change_event
2639 (vnm, arp_change_data_callback,
2640 mp->pid, &mp->address /* addr, in net byte order */ ,
2641 ip_resolver_process_node.index,
2642 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2643
2644 if (rv)
2645 {
2646 pool_put (am->arp_events, event);
2647 goto reply;
2648 }
Dave Barachb7b92992018-10-17 10:38:51 -04002649 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002650
2651 /* Python API expects events to have no context */
2652 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2653 event->client_index = mp->client_index;
2654 event->address = mp->address;
2655 event->pid = mp->pid;
2656 if (mp->address == 0)
2657 event->mac_ip = 1;
2658 }
2659 else
2660 {
2661 rv = vnet_add_del_ip4_arp_change_event
2662 (vnm, arp_change_delete_callback,
2663 mp->pid, &mp->address /* addr, in net byte order */ ,
2664 ip_resolver_process_node.index,
2665 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2666 }
2667reply:
2668 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2669}
2670
Neale Rannsf12dad62018-06-04 18:41:24 -07002671static clib_error_t *
2672want_ip4_arp_events_reaper (u32 client_index)
2673{
2674 vpe_client_registration_t *rp;
2675 vl_api_ip4_arp_event_t *event;
2676 u32 *to_delete, *event_id;
2677 vpe_api_main_t *am;
2678 vnet_main_t *vnm;
2679 uword *p;
2680
2681 am = &vpe_api_main;
2682 vnm = vnet_get_main ();
2683 to_delete = NULL;
2684
2685 /* clear out all of its pending resolutions */
2686 /* *INDENT-OFF* */
2687 pool_foreach(event, am->arp_events,
2688 ({
2689 if (event->client_index == client_index)
2690 {
2691 vec_add1(to_delete, event - am->arp_events);
2692 }
2693 }));
2694 /* *INDENT-ON* */
2695
2696 vec_foreach (event_id, to_delete)
2697 {
2698 event = pool_elt_at_index (am->arp_events, *event_id);
2699 vnet_add_del_ip4_arp_change_event
2700 (vnm, arp_change_delete_callback,
2701 event->pid, &event->address,
2702 ip_resolver_process_node.index, IP4_ARP_EVENT,
2703 ~0 /* pool index, notused */ , 0 /* is_add */ );
2704 }
2705 vec_free (to_delete);
2706
2707 /* remove from the registration hash */
2708 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2709
2710 if (p)
2711 {
2712 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2713 pool_put (am->wc_ip4_arp_events_registrations, rp);
2714 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2715 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2716 wc_arp_set_publisher_node (~0, REPORT_MAX);
2717 }
2718 return (NULL);
2719}
2720
2721VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2722
Neale Rannsb8d44812017-11-10 06:53:54 -08002723static void
2724vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2725{
2726 vpe_api_main_t *am = &vpe_api_main;
2727 vnet_main_t *vnm = vnet_get_main ();
2728 vl_api_want_ip6_nd_events_reply_t *rmp;
2729 int rv = 0;
2730
2731 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2732 {
2733 uword *p =
2734 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2735 vpe_client_registration_t *rp;
2736 if (p)
2737 {
2738 if (mp->enable_disable)
2739 {
2740 clib_warning ("pid %d: already enabled...", mp->pid);
2741 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2742 goto reply;
2743 }
2744 else
2745 {
2746 rp =
2747 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2748 pool_put (am->wc_ip6_nd_events_registrations, rp);
2749 hash_unset (am->wc_ip6_nd_events_registration_hash,
2750 mp->client_index);
2751 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002752 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002753 goto reply;
2754 }
2755 }
2756 if (mp->enable_disable == 0)
2757 {
2758 clib_warning ("pid %d: already disabled...", mp->pid);
2759 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2760 goto reply;
2761 }
2762 pool_get (am->wc_ip6_nd_events_registrations, rp);
2763 rp->client_index = mp->client_index;
2764 rp->client_pid = mp->pid;
2765 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2766 rp - am->wc_ip6_nd_events_registrations);
2767 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2768 goto reply;
2769 }
2770
2771 if (mp->enable_disable)
2772 {
2773 vl_api_ip6_nd_event_t *event;
2774 pool_get (am->nd_events, event);
2775
2776 rv = vnet_add_del_ip6_nd_change_event
2777 (vnm, nd_change_data_callback,
2778 mp->pid, mp->address /* addr, in net byte order */ ,
2779 ip_resolver_process_node.index,
2780 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2781
2782 if (rv)
2783 {
2784 pool_put (am->nd_events, event);
2785 goto reply;
2786 }
Dave Barachb7b92992018-10-17 10:38:51 -04002787 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002788
2789 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2790 event->client_index = mp->client_index;
2791 clib_memcpy (event->address, mp->address, sizeof event->address);
2792 event->pid = mp->pid;
2793 }
2794 else
2795 {
2796 rv = vnet_add_del_ip6_nd_change_event
2797 (vnm, nd_change_delete_callback,
2798 mp->pid, mp->address /* addr, in net byte order */ ,
2799 ip_resolver_process_node.index,
2800 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2801 }
2802reply:
2803 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2804}
2805
Neale Rannsf12dad62018-06-04 18:41:24 -07002806static clib_error_t *
2807want_ip6_nd_events_reaper (u32 client_index)
2808{
2809
2810 vpe_client_registration_t *rp;
2811 vl_api_ip6_nd_event_t *event;
2812 u32 *to_delete, *event_id;
2813 vpe_api_main_t *am;
2814 vnet_main_t *vnm;
2815 uword *p;
2816
2817 am = &vpe_api_main;
2818 vnm = vnet_get_main ();
2819 to_delete = NULL;
2820
2821 /* clear out all of its pending resolutions */
2822 /* *INDENT-OFF* */
2823 pool_foreach(event, am->nd_events,
2824 ({
2825 if (event->client_index == client_index)
2826 {
2827 vec_add1(to_delete, event - am->nd_events);
2828 }
2829 }));
2830 /* *INDENT-ON* */
2831
2832 vec_foreach (event_id, to_delete)
2833 {
2834 event = pool_elt_at_index (am->nd_events, *event_id);
2835 vnet_add_del_ip6_nd_change_event
2836 (vnm, nd_change_delete_callback,
2837 event->pid, &event->address,
2838 ip_resolver_process_node.index, IP6_ND_EVENT,
2839 ~0 /* pool index, notused */ , 0 /* is_add */ );
2840 }
2841 vec_free (to_delete);
2842
2843 /* remove from the registration hash */
2844 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2845
2846 if (p)
2847 {
2848 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2849 pool_put (am->wc_ip6_nd_events_registrations, rp);
2850 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2851 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2852 wc_nd_set_publisher_node (~0, REPORT_MAX);
2853 }
2854 return (NULL);
2855}
2856
2857VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2858
Neale Rannsb8d44812017-11-10 06:53:54 -08002859static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002860vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2861{
2862 vpe_api_main_t *am = &vpe_api_main;
2863 vl_api_want_ip6_ra_events_reply_t *rmp;
2864 int rv = 0;
2865
2866 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2867 vpe_client_registration_t *rp;
2868 if (p)
2869 {
2870 if (mp->enable_disable)
2871 {
2872 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2873 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2874 goto reply;
2875 }
2876 else
2877 {
2878 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2879 pool_put (am->ip6_ra_events_registrations, rp);
2880 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002881 goto reply;
2882 }
2883 }
2884 if (mp->enable_disable == 0)
2885 {
2886 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2887 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2888 goto reply;
2889 }
2890 pool_get (am->ip6_ra_events_registrations, rp);
2891 rp->client_index = mp->client_index;
2892 rp->client_pid = ntohl (mp->pid);
2893 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2894 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002895
2896reply:
2897 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2898}
2899
Neale Rannsf12dad62018-06-04 18:41:24 -07002900static clib_error_t *
2901want_ip6_ra_events_reaper (u32 client_index)
2902{
2903 vpe_api_main_t *am = &vpe_api_main;
2904 vpe_client_registration_t *rp;
2905 uword *p;
2906
2907 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2908
2909 if (p)
2910 {
2911 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2912 pool_put (am->ip6_ra_events_registrations, rp);
2913 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2914 }
2915 return (NULL);
2916}
2917
2918VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2919
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002920static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002921vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2922{
2923 vl_api_proxy_arp_add_del_reply_t *rmp;
2924 u32 fib_index;
2925 int rv;
2926 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002927 uword *p;
2928
Ole Troan3288ed72017-12-06 17:00:05 +01002929 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002930
Neale Ranns0053de62018-05-22 08:40:52 -07002931 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002932
2933 if (!p)
2934 {
2935 rv = VNET_API_ERROR_NO_SUCH_FIB;
2936 goto out;
2937 }
2938
2939 fib_index = p[0];
2940
Neale Ranns0053de62018-05-22 08:40:52 -07002941 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2942 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002943 fib_index, mp->is_add == 0);
2944
2945out:
Ole Troan3288ed72017-12-06 17:00:05 +01002946 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002947 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2948}
2949
Neale Ranns0053de62018-05-22 08:40:52 -07002950typedef struct proxy_arp_walk_ctx_t_
2951{
2952 vl_api_registration_t *reg;
2953 u32 context;
2954} proxy_arp_walk_ctx_t;
2955
2956static walk_rc_t
2957send_proxy_arp_details (const ip4_address_t * lo_addr,
2958 const ip4_address_t * hi_addr,
2959 u32 fib_index, void *data)
2960{
2961 vl_api_proxy_arp_details_t *mp;
2962 proxy_arp_walk_ctx_t *ctx;
2963
2964 ctx = data;
2965
2966 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04002967 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07002968 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2969 mp->context = ctx->context;
2970 mp->proxy.vrf_id = htonl (fib_index);
2971 clib_memcpy (mp->proxy.low_address, lo_addr,
2972 sizeof (mp->proxy.low_address));
2973 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2974
2975 vl_api_send_msg (ctx->reg, (u8 *) mp);
2976
2977 return (WALK_CONTINUE);
2978}
2979
2980static void
2981vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2982{
2983 vl_api_registration_t *reg;
2984
2985 reg = vl_api_client_index_to_registration (mp->client_index);
2986 if (!reg)
2987 return;
2988
2989 proxy_arp_walk_ctx_t wctx = {
2990 .reg = reg,
2991 .context = mp->context,
2992 };
2993
2994 proxy_arp_walk (send_proxy_arp_details, &wctx);
2995}
2996
2997static walk_rc_t
2998send_proxy_arp_intfc_details (vnet_main_t * vnm,
2999 vnet_sw_interface_t * si, void *data)
3000{
3001 vl_api_proxy_arp_intfc_details_t *mp;
3002 proxy_arp_walk_ctx_t *ctx;
3003
3004 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
3005 return (WALK_CONTINUE);
3006
3007 ctx = data;
3008
3009 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003010 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003011 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
3012 mp->context = ctx->context;
3013 mp->sw_if_index = htonl (si->sw_if_index);
3014
3015 vl_api_send_msg (ctx->reg, (u8 *) mp);
3016
3017 return (WALK_CONTINUE);
3018}
3019
3020static void
3021vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
3022{
3023 vl_api_registration_t *reg;
3024
3025 reg = vl_api_client_index_to_registration (mp->client_index);
3026 if (!reg)
3027 return;
3028
3029 proxy_arp_walk_ctx_t wctx = {
3030 .reg = reg,
3031 .context = mp->context,
3032 };
3033
3034 vnet_sw_interface_walk (vnet_get_main (),
3035 send_proxy_arp_intfc_details, &wctx);
3036}
3037
Neale Rannsb8d44812017-11-10 06:53:54 -08003038static void
3039 vl_api_proxy_arp_intfc_enable_disable_t_handler
3040 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
3041{
3042 int rv = 0;
3043 vnet_main_t *vnm = vnet_get_main ();
3044 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
3045
3046 VALIDATE_SW_IF_INDEX (mp);
3047
3048 vnet_sw_interface_t *si =
3049 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
3050
3051 ASSERT (si);
3052
3053 if (mp->enable_disable)
3054 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3055 else
3056 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3057
3058 BAD_SW_IF_INDEX_LABEL;
3059
3060 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3061}
3062
John Loc7b43042018-04-13 16:46:22 -04003063static void
3064vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3065{
3066 int rv = 0;
3067 vlib_main_t *vm = vlib_get_main ();
3068 vl_api_ip_probe_neighbor_reply_t *rmp;
3069 clib_error_t *error;
3070
3071 VALIDATE_SW_IF_INDEX (mp);
3072
3073 u32 sw_if_index = ntohl (mp->sw_if_index);
3074
3075 if (mp->is_ipv6)
3076 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003077 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003078 else
3079 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003080 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003081
3082 if (error)
3083 {
3084 clib_error_report (error);
3085 rv = clib_error_get_code (error);
3086 }
3087
3088 BAD_SW_IF_INDEX_LABEL;
3089
3090 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3091}
3092
John Lo7f358b32018-04-28 01:19:24 -04003093static void
3094 vl_api_ip_scan_neighbor_enable_disable_t_handler
3095 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3096{
3097 int rv = 0;
3098 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3099 ip_neighbor_scan_arg_t arg;
3100
3101 arg.mode = mp->mode;
3102 arg.scan_interval = mp->scan_interval;
3103 arg.max_proc_time = mp->max_proc_time;
3104 arg.max_update = mp->max_update;
3105 arg.scan_int_delay = mp->scan_int_delay;
3106 arg.stale_threshold = mp->stale_threshold;
3107 ip_neighbor_scan_enable_disable (&arg);
3108
3109 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3110}
3111
Neale Rannsb8d44812017-11-10 06:53:54 -08003112static int
3113ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3114{
3115 vnet_main_t *vnm = vnet_get_main ();
3116 vnet_interface_main_t *im = &vnm->interface_main;
3117 ip4_main_t *im4 = &ip4_main;
3118 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003119 fib_table_t *fib_table;
3120 ip4_fib_t *fib;
3121 u32 sw_if_index;
3122 int i;
3123 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3124 u32 target_fib_id = ntohl (mp->vrf_id);
3125
Ole Troan3288ed72017-12-06 17:00:05 +01003126 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003127
3128 /* *INDENT-OFF* */
3129 pool_foreach (fib_table, im4->fibs,
3130 ({
3131 vnet_sw_interface_t * si;
3132
3133 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3134
3135 if (fib->table_id != target_fib_id)
3136 continue;
3137
3138 /* remove any mpls encap/decap labels */
3139 mpls_fib_reset_labels (fib->table_id);
3140
3141 /* remove any proxy arps in this fib */
3142 vnet_proxy_arp_fib_reset (fib->table_id);
3143
3144 /* Set the flow hash for this fib to the default */
3145 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3146
3147 vec_reset_length (sw_if_indices_to_shut);
3148
3149 /* Shut down interfaces in this FIB / clean out intfc routes */
3150 pool_foreach (si, im->sw_interfaces,
3151 ({
3152 u32 sw_if_index = si->sw_if_index;
3153
3154 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3155 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3156 fib->index))
3157 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3158 }));
3159
3160 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3161 sw_if_index = sw_if_indices_to_shut[i];
3162
3163 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3164 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3165 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3166 }
3167
3168 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3169
3170 rv = 0;
3171 break;
3172 })); /* pool_foreach (fib) */
3173 /* *INDENT-ON* */
3174
Ole Troan3288ed72017-12-06 17:00:05 +01003175 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003176 return rv;
3177}
3178
3179static int
3180ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3181{
3182 vnet_main_t *vnm = vnet_get_main ();
3183 vnet_interface_main_t *im = &vnm->interface_main;
3184 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003185 static u32 *sw_if_indices_to_shut;
3186 fib_table_t *fib_table;
3187 ip6_fib_t *fib;
3188 u32 sw_if_index;
3189 int i;
3190 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3191 u32 target_fib_id = ntohl (mp->vrf_id);
3192
Ole Troan3288ed72017-12-06 17:00:05 +01003193 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003194
3195 /* *INDENT-OFF* */
3196 pool_foreach (fib_table, im6->fibs,
3197 ({
3198 vnet_sw_interface_t * si;
3199
3200 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3201
3202 if (fib->table_id != target_fib_id)
3203 continue;
3204
3205 vec_reset_length (sw_if_indices_to_shut);
3206
3207 /* Set the flow hash for this fib to the default */
3208 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3209
3210 /* Shut down interfaces in this FIB / clean out intfc routes */
3211 pool_foreach (si, im->sw_interfaces,
3212 ({
3213 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3214 fib->index)
3215 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3216 }));
3217
3218 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3219 sw_if_index = sw_if_indices_to_shut[i];
3220
3221 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3222 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3223 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3224 }
3225
3226 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3227
3228 rv = 0;
3229 break;
3230 })); /* pool_foreach (fib) */
3231 /* *INDENT-ON* */
3232
Ole Troan3288ed72017-12-06 17:00:05 +01003233 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003234 return rv;
3235}
3236
3237static void
3238vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3239{
3240 int rv;
3241 vl_api_reset_fib_reply_t *rmp;
3242
3243 if (mp->is_ipv6)
3244 rv = ip6_reset_fib_t_handler (mp);
3245 else
3246 rv = ip4_reset_fib_t_handler (mp);
3247
3248 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3249}
3250
3251static void
3252vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3253{
3254 int rv;
3255 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3256 vnet_main_t *vnm = vnet_get_main ();
3257 clib_error_t *error;
3258
3259 vnm->api_errno = 0;
3260
3261 if (mp->is_ipv6)
3262 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3263 else
3264 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3265
3266 if (error)
3267 {
3268 clib_error_report (error);
3269 rv = VNET_API_ERROR_UNSPECIFIED;
3270 }
3271 else
3272 {
3273 rv = vnm->api_errno;
3274 }
3275
3276 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3277}
3278
Klement Sekera75e7d132017-09-20 08:26:30 +02003279void
3280vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3281{
3282 vl_api_ip_reassembly_set_reply_t *rmp;
3283 int rv = 0;
3284 if (mp->is_ip6)
3285 {
3286 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3287 clib_net_to_host_u32 (mp->max_reassemblies),
3288 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3289 }
3290 else
3291 {
3292 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3293 clib_net_to_host_u32 (mp->max_reassemblies),
3294 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3295 }
3296
3297 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3298}
3299
3300void
3301vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3302{
3303 unix_shared_memory_queue_t *q;
3304
3305 q = vl_api_client_index_to_input_queue (mp->client_index);
3306
3307 if (q == 0)
3308 return;
3309
3310 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04003311 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02003312 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3313 rmp->context = mp->context;
3314 rmp->retval = 0;
3315 if (mp->is_ip6)
3316 {
3317 rmp->is_ip6 = 1;
3318 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3319 &rmp->expire_walk_interval_ms);
3320 }
3321 else
3322 {
3323 rmp->is_ip6 = 0;
3324 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3325 &rmp->expire_walk_interval_ms);
3326 }
3327 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3328 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3329 rmp->expire_walk_interval_ms =
3330 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3331 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3332}
3333
Klement Sekera4c533132018-02-22 11:41:12 +01003334void
3335 vl_api_ip_reassembly_enable_disable_t_handler
3336 (vl_api_ip_reassembly_enable_disable_t * mp)
3337{
3338 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3339 int rv = 0;
3340 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3341 mp->enable_ip4);
3342 if (0 == rv)
3343 {
3344 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3345 mp->enable_ip6);
3346 }
3347
Choree7f61d12018-11-28 10:27:58 +03303348 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01003349}
3350
Pavel Kotucek609e1212018-11-27 09:59:44 +01003351void
3352send_ip_punt_redirect_details (vl_api_registration_t * reg,
3353 u32 context, u32 sw_if_index,
3354 ip_punt_redirect_rx_t * pr, u8 is_ipv6)
3355{
3356 vl_api_ip_punt_redirect_details_t *mp;
3357
3358 mp = vl_msg_api_alloc (sizeof (*mp));
3359 if (!mp)
3360 return;
3361
3362 clib_memset (mp, 0, sizeof (*mp));
3363 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
3364 mp->context = context;
3365 mp->punt.rx_sw_if_index = htonl (sw_if_index);
3366 mp->punt.tx_sw_if_index = htonl (pr->tx_sw_if_index);
3367 if (is_ipv6)
3368 {
3369 ip_address_encode (&pr->nh, IP46_TYPE_IP6, &mp->punt.nh);
3370 }
3371 else
3372 {
3373 ip_address_encode (&pr->nh, IP46_TYPE_IP4, &mp->punt.nh);
3374 }
3375
3376 vl_api_send_msg (reg, (u8 *) mp);
3377}
3378
3379static void
3380vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
3381{
3382 vl_api_registration_t *reg;
3383 u32 sw_if_index;
3384 int rv __attribute__ ((unused)) = 0;
3385
3386 sw_if_index = ntohl (mp->sw_if_index);
3387 reg = vl_api_client_index_to_registration (mp->client_index);
3388 if (!reg)
3389 return;
3390
3391 if (~0 != sw_if_index)
3392 VALIDATE_SW_IF_INDEX (mp);
3393
3394 ip_punt_redirect_detail_t *pr, *prs;
3395 if (mp->is_ipv6)
3396 {
3397 prs = ip6_punt_redirect_entries (sw_if_index);
3398 /* *INDENT-OFF* */
3399 vec_foreach (pr, prs)
3400 {
3401 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 1);
3402 }
3403 /* *INDENT-ON* */
3404 vec_free (prs);
3405 }
3406 else
3407 {
3408 prs = ip4_punt_redirect_entries (sw_if_index);
3409 /* *INDENT-OFF* */
3410 vec_foreach (pr, prs)
3411 {
3412 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 0);
3413 }
3414 /* *INDENT-ON* */
3415 vec_free (prs);
3416 }
3417
3418 BAD_SW_IF_INDEX_LABEL;
3419}
3420
Dave Barachb5e8a772016-12-06 12:04:42 -05003421#define vl_msg_name_crc_list
3422#include <vnet/ip/ip.api.h>
3423#undef vl_msg_name_crc_list
3424
3425static void
3426setup_message_id_table (api_main_t * am)
3427{
3428#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3429 foreach_vl_msg_name_crc_ip;
3430#undef _
3431}
3432
3433static clib_error_t *
3434ip_api_hookup (vlib_main_t * vm)
3435{
3436 api_main_t *am = &api_main;
3437
3438#define _(N,n) \
3439 vl_msg_api_set_handlers(VL_API_##N, #n, \
3440 vl_api_##n##_t_handler, \
3441 vl_noop_handler, \
3442 vl_api_##n##_t_endian, \
3443 vl_api_##n##_t_print, \
3444 sizeof(vl_api_##n##_t), 1);
3445 foreach_ip_api_msg;
3446#undef _
3447
3448 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00003449 * Mark the route add/del API as MP safe
3450 */
3451 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3452 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE_REPLY] = 1;
3453
3454 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05003455 * Set up the (msg_name, crc, message-id) table
3456 */
3457 setup_message_id_table (am);
3458
Juraj Sloboda52574522018-05-03 10:03:50 +02003459 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3460
Dave Barachb5e8a772016-12-06 12:04:42 -05003461 return 0;
3462}
3463
3464VLIB_API_INIT_FUNCTION (ip_api_hookup);
3465
3466/*
3467 * fd.io coding-style-patch-verification: ON
3468 *
3469 * Local Variables:
3470 * eval: (c-set-style "gnu")
3471 * End:
3472 */