blob: 5a2aa60b057c88b576821ad56b9e42215f7463c6 [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 Ranns5a8123b2017-01-26 01:18:23 -0800413 vl_api_fib_path_t *fp;
414 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 Ranns81458422018-03-12 06:59:36 -0700441 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800442 fp++;
443 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800444 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800445
Florin Coras6c4dae22018-01-09 06:39:23 -0800446 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800447}
448
449typedef struct vl_api_ip_mfib_dump_ctc_t_
450{
451 fib_node_index_t *entries;
452} vl_api_ip_mfib_dump_ctc_t;
453
454static int
455vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
456{
457 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
458
459 vec_add1 (ctx->entries, fei);
460
461 return (0);
462}
463
464static void
465vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
466{
Florin Coras6c4dae22018-01-09 06:39:23 -0800467 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800468 ip4_main_t *im = &ip4_main;
469 mfib_table_t *mfib_table;
470 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800471 vl_api_ip_mfib_dump_ctc_t ctx = {
472 .entries = NULL,
473 };
474
Florin Coras6c4dae22018-01-09 06:39:23 -0800475 reg = vl_api_client_index_to_registration (mp->client_index);
476 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800477 return;
478
Neale Ranns5a8123b2017-01-26 01:18:23 -0800479 /* *INDENT-OFF* */
480 pool_foreach (mfib_table, im->mfibs,
481 ({
482 ip4_mfib_table_walk(&mfib_table->v4,
483 vl_api_ip_mfib_table_dump_walk,
484 &ctx);
485
486 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
487
488 vec_foreach (mfeip, ctx.entries)
489 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800490 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800491 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800492 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800493 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800494 vec_reset_length (ctx.entries);
495
496 }));
497 /* *INDENT-ON* */
498
499 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800500}
501
502static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800503send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800504 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800505 u32 table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800506 const mfib_prefix_t * pfx,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800507 fib_route_path_encode_t * api_rpaths, u32 context)
508{
509 vl_api_ip6_mfib_details_t *mp;
510 fib_route_path_encode_t *api_rpath;
511 vl_api_fib_path_t *fp;
512 int path_count;
513
514 path_count = vec_len (api_rpaths);
515 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
516 if (!mp)
517 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400518 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700519 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800520 mp->context = context;
521
522 mp->table_id = htonl (table_id);
523 mp->address_length = pfx->fp_len;
524 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
525 sizeof (pfx->fp_grp_addr.ip6));
526 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
527 sizeof (pfx->fp_src_addr.ip6));
528
529 mp->count = htonl (path_count);
530 fp = mp->path;
531 vec_foreach (api_rpath, api_rpaths)
532 {
Neale Ranns81458422018-03-12 06:59:36 -0700533 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800534 fp++;
535 }
536
Florin Coras6c4dae22018-01-09 06:39:23 -0800537 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800538}
539
540typedef struct vl_api_ip6_mfib_dump_ctc_t_
541{
542 fib_node_index_t *entries;
543} vl_api_ip6_mfib_dump_ctc_t;
544
545static int
546vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
547{
548 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
549
550 vec_add1 (ctx->entries, fei);
551
552 return (0);
553}
554
555static void
556vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
557{
558 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800559 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800560 ip6_main_t *im = &ip6_main;
561 mfib_table_t *mfib_table;
Neale Ranns9e829a82018-12-17 05:50:32 -0800562 const mfib_prefix_t *pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800563 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800564 fib_route_path_encode_t *api_rpaths = NULL;
565 vl_api_ip6_mfib_dump_ctc_t ctx = {
566 .entries = NULL,
567 };
568
Florin Coras6c4dae22018-01-09 06:39:23 -0800569 reg = vl_api_client_index_to_registration (mp->client_index);
570 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800571 return;
572
573
574 /* *INDENT-OFF* */
575 pool_foreach (mfib_table, im->mfibs,
576 ({
577 ip6_mfib_table_walk(&mfib_table->v6,
578 vl_api_ip6_mfib_table_dump_walk,
579 &ctx);
580
581 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
582
583 vec_foreach(mfeip, ctx.entries)
584 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800585 pfx = mfib_entry_get_prefix (*mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800586 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800587 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800588 mfib_table->mft_table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800589 pfx, api_rpaths,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800590 mp->context);
591 }
592 vec_reset_length (api_rpaths);
593 vec_reset_length (ctx.entries);
594
595 }));
596 /* *INDENT-ON* */
597
598 vec_free (ctx.entries);
599 vec_free (api_rpaths);
600}
601
602static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700603vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
604 vlib_main_t * vm)
605{
606 vl_api_ip_punt_police_reply_t *rmp;
607 int rv = 0;
608
609 if (mp->is_ip6)
610 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
611 else
612 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
613
614 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
615}
616
617static void
618vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
619 vlib_main_t * vm)
620{
621 vl_api_ip_punt_redirect_reply_t *rmp;
622 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100623 ip46_type_t ipv;
624 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700625
Pavel Kotucek609e1212018-11-27 09:59:44 +0100626 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
627 goto bad_sw_if_index;
628
629 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700630 if (mp->is_add)
631 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100632 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700633 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100634 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
635 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700636 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100637 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700638 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100639 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
640 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700641 }
642 }
643 else
644 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100645 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700646 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100647 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700648 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100649 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700650 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100651 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700652 }
653 }
654
Pavel Kotucek609e1212018-11-27 09:59:44 +0100655 BAD_SW_IF_INDEX_LABEL;
656
Neale Rannsd91c1db2017-07-31 02:30:50 -0700657 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
658}
659
660static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500661vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
662 vlib_main_t * vm)
663{
Neale Ranns0bdd3192018-09-07 11:04:52 -0700664 ip46_address_t ip = ip46_address_initializer;
Dave Barachb5e8a772016-12-06 12:04:42 -0500665 vl_api_ip_neighbor_add_del_reply_t *rmp;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700666 ip_neighbor_flags_t flags;
Neale Ranns14260392018-09-28 05:00:57 -0700667 u32 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -0500668 int rv = 0;
669
670 VALIDATE_SW_IF_INDEX (mp);
671
672 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
673
Neale Ranns0bdd3192018-09-07 11:04:52 -0700674 flags = IP_NEIGHBOR_FLAG_NODE;
675 if (mp->is_static)
676 flags |= IP_NEIGHBOR_FLAG_STATIC;
677 if (mp->is_no_adj_fib)
678 flags |= IP_NEIGHBOR_FLAG_NO_ADJ_FIB;
679
Dave Barachb5e8a772016-12-06 12:04:42 -0500680 if (mp->is_ipv6)
Neale Ranns0bdd3192018-09-07 11:04:52 -0700681 clib_memcpy (&ip.ip6, mp->dst_address, 16);
Dave Barachb5e8a772016-12-06 12:04:42 -0500682 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700683 clib_memcpy (&ip.ip4, mp->dst_address, 4);
Dave Barachb5e8a772016-12-06 12:04:42 -0500684
Neale Ranns0bdd3192018-09-07 11:04:52 -0700685 if (mp->is_add)
686 rv = ip_neighbor_add (&ip, mp->is_ipv6, mp->mac_address,
Neale Ranns14260392018-09-28 05:00:57 -0700687 ntohl (mp->sw_if_index), flags, &stats_index);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700688 else
689 rv = ip_neighbor_del (&ip, mp->is_ipv6, ntohl (mp->sw_if_index));
Dave Barachb5e8a772016-12-06 12:04:42 -0500690
Dave Barachb5e8a772016-12-06 12:04:42 -0500691 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700692
693 BAD_SW_IF_INDEX_LABEL;
Neale Ranns14260392018-09-28 05:00:57 -0700694
695 /* *INDENT-OFF* */
696 REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
697 ({
698 rmp->stats_index = htonl (stats_index);
699 }));
700 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500701}
702
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700703void
Neale Ranns15002542017-09-10 04:39:11 -0700704ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
705{
706 u32 fib_index, mfib_index;
707
708 /*
709 * ignore action on the default table - this is always present
710 * and cannot be added nor deleted from the API
711 */
712 if (0 != table_id)
713 {
714 /*
715 * The API holds only one lock on the table.
716 * i.e. it can be added many times via the API but needs to be
717 * deleted only once.
718 * The FIB index for unicast and multicast is not necessarily the
719 * same, since internal VPP systesm (like LISP and SR) create
720 * their own unicast tables.
721 */
722 fib_index = fib_table_find (fproto, table_id);
723 mfib_index = mfib_table_find (fproto, table_id);
724
725 if (~0 != fib_index)
726 {
727 fib_table_unlock (fib_index, fproto,
728 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
729 }
730 if (~0 != mfib_index)
731 {
732 mfib_table_unlock (mfib_index, fproto,
733 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
734 }
735 }
736}
737
738void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700739vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
740{
741 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700742 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
743 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700744 int rv = 0;
745
Neale Ranns15002542017-09-10 04:39:11 -0700746 if (mp->is_add)
747 {
Neale Ranns2297af02017-09-12 09:45:04 -0700748 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700749 }
750 else
751 {
752 ip_table_delete (fproto, table_id, 1);
753 }
754
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700755 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
756}
757
Dave Barachb5e8a772016-12-06 12:04:42 -0500758int
759add_del_route_t_handler (u8 is_multipath,
760 u8 is_add,
761 u8 is_drop,
762 u8 is_unreach,
763 u8 is_prohibit,
764 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800765 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500766 u8 is_classify,
767 u32 classify_table_index,
768 u8 is_resolve_host,
769 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800770 u8 is_interface_rx,
771 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800772 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700773 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800774 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500775 u32 fib_index,
776 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700777 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500778 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800779 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500780 u32 next_hop_sw_if_index,
781 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700782 u16 next_hop_weight,
783 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500784 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800785 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500786{
787 vnet_classify_main_t *cm = &vnet_classify_main;
788 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
789 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700790 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500791 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
792 .frp_sw_if_index = next_hop_sw_if_index,
793 .frp_fib_index = next_hop_fib_index,
794 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700795 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500796 .frp_label_stack = next_hop_out_label_stack,
797 };
798 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800799 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500800
Neale Rannscaac3502017-09-18 18:04:10 -0700801 /*
802 * the special INVALID label meams we are not recursing via a
803 * label. Exp-null value is never a valid via-label so that
804 * also means it's not a via-label and means clients that set
805 * it to 0 by default get the expected behaviour
806 */
807 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500808 {
Neale Rannsda78f952017-05-24 09:15:43 -0700809 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500810 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800811 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500812 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800813 if (is_dvr)
814 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500815 if (is_resolve_host)
816 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
817 if (is_resolve_attached)
818 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800819 if (is_interface_rx)
820 path_flags |= FIB_ROUTE_PATH_INTF_RX;
821 if (is_rpf_id)
822 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700823 if (is_source_lookup)
824 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800825 if (is_multicast)
826 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800827 if (is_udp_encap)
828 {
829 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
830 path.frp_udp_encap_id = next_hop_id;
831 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800832 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
833 && path.frp_fib_index != ~0)
834 {
835 path_flags |= FIB_ROUTE_PATH_DEAG;
836 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500837
838 path.frp_flags = path_flags;
839
Dave Barachb5e8a772016-12-06 12:04:42 -0500840 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
841
842 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
843 {
844 /*
845 * special route types that link directly to the adj
846 */
847 if (is_add)
848 {
849 dpo_id_t dpo = DPO_INVALID;
850 dpo_proto_t dproto;
851
852 dproto = fib_proto_to_dpo (prefix->fp_proto);
853
854 if (is_drop)
855 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
856 else if (is_local)
857 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
858 else if (is_unreach)
859 ip_null_dpo_add_and_lock (dproto,
860 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
861 else if (is_prohibit)
862 ip_null_dpo_add_and_lock (dproto,
863 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
864 &dpo);
865 else if (is_classify)
866 {
867 if (pool_is_free_index (cm->tables,
868 ntohl (classify_table_index)))
869 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500870 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500871 return VNET_API_ERROR_NO_SUCH_TABLE;
872 }
873
874 dpo_set (&dpo, DPO_CLASSIFY, dproto,
875 classify_dpo_create (dproto,
876 ntohl (classify_table_index)));
877 }
878 else
879 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500880 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500881 return VNET_API_ERROR_NO_SUCH_TABLE;
882 }
883
884 fib_table_entry_special_dpo_update (fib_index,
885 prefix,
886 FIB_SOURCE_API,
887 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
888 dpo_reset (&dpo);
889 }
890 else
891 {
892 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
893 }
894 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700895 else if (is_multipath)
896 {
897 vec_add1 (paths, path);
898
899 if (is_add)
900 fib_table_entry_path_add2 (fib_index,
901 prefix,
902 FIB_SOURCE_API, entry_flags, paths);
903 else
904 fib_table_entry_path_remove2 (fib_index,
905 prefix, FIB_SOURCE_API, paths);
906
907 vec_free (paths);
908 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500909 else
910 {
911 if (is_add)
912 {
913 vec_add1 (paths, path);
914 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800915 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500916 vec_free (paths);
917 }
918 else
919 {
920 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
921 }
922 }
923
Dave Barachd7cb1b52016-12-09 09:52:16 -0500924 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500925 return (0);
926}
927
928int
929add_del_route_check (fib_protocol_t table_proto,
930 u32 table_id,
931 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700932 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500933 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800934 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500935{
936 vnet_main_t *vnm = vnet_get_main ();
937
938 *fib_index = fib_table_find (table_proto, ntohl (table_id));
939 if (~0 == *fib_index)
940 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800941 /* No such VRF, and we weren't asked to create one */
942 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500943 }
944
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800945 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500946 {
947 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
948 ntohl (next_hop_sw_if_index)))
949 {
950 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
951 }
952 }
953 else
954 {
Neale Rannsda78f952017-05-24 09:15:43 -0700955 fib_protocol_t fib_nh_proto;
956
957 if (next_hop_table_proto > DPO_PROTO_MPLS)
958 return (0);
959
960 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
961
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800962 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700963 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800964 ntohl (next_hop_table_id));
965 else
Neale Rannsda78f952017-05-24 09:15:43 -0700966 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800967 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500968
969 if (~0 == *next_hop_fib_index)
970 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800971 /* No such VRF, and we weren't asked to create one */
972 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500973 }
974 }
975
976 return (0);
977}
978
979static int
Neale Ranns008dbe12018-09-07 09:32:36 -0700980ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
981 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500982{
983 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800984 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500985 int rv, ii, n_labels;;
986
987 rv = add_del_route_check (FIB_PROTOCOL_IP4,
988 mp->table_id,
989 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700990 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500991 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700992 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500993
994 if (0 != rv)
995 return (rv);
996
997 fib_prefix_t pfx = {
998 .fp_len = mp->dst_address_length,
999 .fp_proto = FIB_PROTOCOL_IP4,
1000 };
1001 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1002
1003 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001004 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001005 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1006
1007 n_labels = mp->next_hop_n_out_labels;
1008 if (n_labels == 0)
1009 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001010 else
1011 {
1012 vec_validate (label_stack, n_labels - 1);
1013 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001014 {
1015 label_stack[ii].fml_value =
1016 ntohl (mp->next_hop_out_label_stack[ii].label);
1017 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1018 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1019 label_stack[ii].fml_mode =
1020 (mp->next_hop_out_label_stack[ii].is_uniform ?
1021 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1022 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001023 }
1024
Neale Ranns008dbe12018-09-07 09:32:36 -07001025 rv = add_del_route_t_handler (mp->is_multipath,
1026 mp->is_add,
1027 mp->is_drop,
1028 mp->is_unreach,
1029 mp->is_prohibit,
1030 mp->is_local, 0,
1031 mp->is_classify,
1032 mp->classify_table_index,
1033 mp->is_resolve_host,
1034 mp->is_resolve_attached, 0, 0,
1035 mp->is_dvr,
1036 mp->is_source_lookup,
1037 mp->is_udp_encap,
1038 fib_index, &pfx, DPO_PROTO_IP4,
1039 &nh,
1040 ntohl (mp->next_hop_id),
1041 ntohl (mp->next_hop_sw_if_index),
1042 next_hop_fib_index,
1043 mp->next_hop_weight,
1044 mp->next_hop_preference,
1045 ntohl (mp->next_hop_via_label), label_stack);
1046
1047 if (mp->is_add && 0 == rv)
1048 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1049
1050 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001051}
1052
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001053static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001054ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1055 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001056{
Neale Ranns31ed7442018-02-23 05:29:09 -08001057 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001058 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001059 int rv, ii, n_labels;;
1060
1061 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1062 mp->table_id,
1063 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001064 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001065 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001066 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001067
1068 if (0 != rv)
1069 return (rv);
1070
1071 fib_prefix_t pfx = {
1072 .fp_len = mp->dst_address_length,
1073 .fp_proto = FIB_PROTOCOL_IP6,
1074 };
1075 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1076
1077 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001078 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001079 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1080
1081 n_labels = mp->next_hop_n_out_labels;
1082 if (n_labels == 0)
1083 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001084 else
1085 {
1086 vec_validate (label_stack, n_labels - 1);
1087 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001088 {
1089 label_stack[ii].fml_value =
1090 ntohl (mp->next_hop_out_label_stack[ii].label);
1091 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1092 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1093 label_stack[ii].fml_mode =
1094 (mp->next_hop_out_label_stack[ii].is_uniform ?
1095 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1096 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001097 }
1098
Neale Ranns008dbe12018-09-07 09:32:36 -07001099 rv = add_del_route_t_handler (mp->is_multipath,
1100 mp->is_add,
1101 mp->is_drop,
1102 mp->is_unreach,
1103 mp->is_prohibit,
1104 mp->is_local, 0,
1105 mp->is_classify,
1106 mp->classify_table_index,
1107 mp->is_resolve_host,
1108 mp->is_resolve_attached, 0, 0,
1109 mp->is_dvr,
1110 mp->is_source_lookup,
1111 mp->is_udp_encap,
1112 fib_index, &pfx, DPO_PROTO_IP6,
1113 &nh, ntohl (mp->next_hop_id),
1114 ntohl (mp->next_hop_sw_if_index),
1115 next_hop_fib_index,
1116 mp->next_hop_weight,
1117 mp->next_hop_preference,
1118 ntohl (mp->next_hop_via_label), label_stack);
1119
1120 if (mp->is_add && 0 == rv)
1121 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1122
1123 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001124}
1125
1126void
1127vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1128{
1129 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001130 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001131 int rv;
1132 vnet_main_t *vnm = vnet_get_main ();
1133
1134 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001135 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001136
1137 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001138 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001139 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001140 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001141
1142 rv = (rv == 0) ? vnm->api_errno : rv;
1143
Neale Ranns008dbe12018-09-07 09:32:36 -07001144 /* *INDENT-OFF* */
1145 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1146 ({
1147 rmp->stats_index = htonl (stats_index);
1148 }))
1149 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001150}
1151
Neale Ranns15002542017-09-10 04:39:11 -07001152void
Neale Ranns2297af02017-09-12 09:45:04 -07001153ip_table_create (fib_protocol_t fproto,
1154 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001155{
1156 u32 fib_index, mfib_index;
1157
1158 /*
1159 * ignore action on the default table - this is always present
1160 * and cannot be added nor deleted from the API
1161 */
1162 if (0 != table_id)
1163 {
1164 /*
1165 * The API holds only one lock on the table.
1166 * i.e. it can be added many times via the API but needs to be
1167 * deleted only once.
1168 * The FIB index for unicast and multicast is not necessarily the
1169 * same, since internal VPP systesm (like LISP and SR) create
1170 * their own unicast tables.
1171 */
1172 fib_index = fib_table_find (fproto, table_id);
1173 mfib_index = mfib_table_find (fproto, table_id);
1174
1175 if (~0 == fib_index)
1176 {
Neale Ranns2297af02017-09-12 09:45:04 -07001177 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1178 (is_api ?
1179 FIB_SOURCE_API :
1180 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001181 }
1182 if (~0 == mfib_index)
1183 {
Neale Ranns2297af02017-09-12 09:45:04 -07001184 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1185 (is_api ?
1186 MFIB_SOURCE_API :
1187 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001188 }
1189 }
1190}
1191
Neale Ranns32e1c012016-11-22 17:07:28 +00001192static int
1193add_del_mroute_check (fib_protocol_t table_proto,
1194 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001195 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001196{
1197 vnet_main_t *vnm = vnet_get_main ();
1198
1199 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1200 if (~0 == *fib_index)
1201 {
Neale Ranns15002542017-09-10 04:39:11 -07001202 /* No such table */
1203 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001204 }
1205
1206 if (~0 != ntohl (next_hop_sw_if_index))
1207 {
1208 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1209 ntohl (next_hop_sw_if_index)))
1210 {
1211 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1212 }
1213 }
1214
1215 return (0);
1216}
1217
Neale Ranns28c142e2018-09-07 09:37:07 -07001218static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001219mroute_add_del_handler (u8 is_add,
1220 u8 is_local,
1221 u32 fib_index,
1222 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001223 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001224 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001225 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001226 u32 next_hop_sw_if_index,
1227 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001228{
Neale Ranns28c142e2018-09-07 09:37:07 -07001229 fib_node_index_t mfib_entry_index = ~0;
1230
Neale Ranns32e1c012016-11-22 17:07:28 +00001231 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1232
1233 fib_route_path_t path = {
1234 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001235 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001236 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001237 };
1238
1239 if (is_local)
1240 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1241
Neale Rannsd792d9c2017-10-21 10:53:20 -07001242 if (DPO_PROTO_BIER == nh_proto)
1243 {
1244 path.frp_bier_imp = bier_imp;
1245 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1246 }
1247 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001248 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001249 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1250 MFIB_SOURCE_API,
1251 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001252 goto done;
1253 }
1254
1255 if (is_add)
1256 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001257 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1258 MFIB_SOURCE_API,
1259 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001260 }
1261 else
1262 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001263 mfib_table_entry_path_remove (fib_index, prefix,
1264 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001265 }
1266
Neale Rannsd792d9c2017-10-21 10:53:20 -07001267done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001268 stats_dsunlock ();
Neale Ranns28c142e2018-09-07 09:37:07 -07001269 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001270}
1271
1272static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001273api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1274 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001275{
Neale Ranns28c142e2018-09-07 09:37:07 -07001276 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001277 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001278 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001279 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001280 u32 fib_index;
1281 int rv;
1282
Neale Rannsd792d9c2017-10-21 10:53:20 -07001283 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001284 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1285 rv = add_del_mroute_check (fproto,
1286 mp->table_id,
1287 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001288 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001289
1290 if (0 != rv)
1291 return (rv);
1292
1293 mfib_prefix_t pfx = {
1294 .fp_len = ntohs (mp->grp_address_length),
1295 .fp_proto = fproto,
1296 };
1297
1298 if (FIB_PROTOCOL_IP4 == fproto)
1299 {
1300 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1301 sizeof (pfx.fp_grp_addr.ip4));
1302 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1303 sizeof (pfx.fp_src_addr.ip4));
Dave Barachb7b92992018-10-17 10:38:51 -04001304 clib_memset (&nh.ip6, 0, sizeof (nh.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001305 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001306 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1307 pfx.fp_len = 64;
Neale Ranns32e1c012016-11-22 17:07:28 +00001308 }
1309 else
1310 {
1311 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1312 sizeof (pfx.fp_grp_addr.ip6));
1313 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1314 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001315 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001316 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1317 pfx.fp_len = 256;
Neale Ranns32e1c012016-11-22 17:07:28 +00001318 }
1319
Neale Ranns28c142e2018-09-07 09:37:07 -07001320 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1321 mp->is_local,
1322 fib_index, &pfx,
1323 nh_proto,
1324 ntohl (mp->entry_flags),
1325 ntohl (mp->rpf_id),
1326 ntohl (mp->next_hop_sw_if_index),
1327 &nh,
1328 ntohl (mp->itf_flags),
1329 ntohl (mp->bier_imp));
1330
1331 if (~0 != mfib_entry_index)
1332 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1333
1334 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001335}
1336
1337void
1338vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1339{
1340 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001341 vnet_main_t *vnm;
1342 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001343 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001344
Neale Ranns28c142e2018-09-07 09:37:07 -07001345 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001346 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001347 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001348
Neale Ranns28c142e2018-09-07 09:37:07 -07001349 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001350
Neale Ranns28c142e2018-09-07 09:37:07 -07001351 /* *INDENT-OFF* */
1352 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1353 ({
1354 rmp->stats_index = htonl (stats_index);
1355 }));
1356 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001357}
1358
Dave Barachb5e8a772016-12-06 12:04:42 -05001359static void
1360send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001361 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1362 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001363{
1364 vl_api_ip_details_t *mp;
1365
1366 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001367 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001368 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1369
1370 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001371 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001372 mp->context = context;
1373
Florin Coras6c4dae22018-01-09 06:39:23 -08001374 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001375}
1376
1377static void
1378send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001379 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001380 u8 * ip, u16 prefix_length,
1381 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001382{
1383 vl_api_ip_address_details_t *mp;
1384
1385 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001386 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001387 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1388
1389 if (is_ipv6)
1390 {
1391 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1392 }
1393 else
1394 {
1395 u32 *tp = (u32 *) mp->ip;
1396 *tp = *(u32 *) ip;
1397 }
1398 mp->prefix_length = prefix_length;
1399 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001400 mp->sw_if_index = htonl (sw_if_index);
1401 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001402
Florin Coras6c4dae22018-01-09 06:39:23 -08001403 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001404}
1405
1406static void
1407vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1408{
1409 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001410 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001411 ip6_address_t *r6;
1412 ip4_address_t *r4;
1413 ip6_main_t *im6 = &ip6_main;
1414 ip4_main_t *im4 = &ip4_main;
1415 ip_lookup_main_t *lm6 = &im6->lookup_main;
1416 ip_lookup_main_t *lm4 = &im4->lookup_main;
1417 ip_interface_address_t *ia = 0;
1418 u32 sw_if_index = ~0;
1419 int rv __attribute__ ((unused)) = 0;
1420
1421 VALIDATE_SW_IF_INDEX (mp);
1422
1423 sw_if_index = ntohl (mp->sw_if_index);
1424
Florin Coras6c4dae22018-01-09 06:39:23 -08001425 reg = vl_api_client_index_to_registration (mp->client_index);
1426 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001427 return;
1428
Dave Barachb5e8a772016-12-06 12:04:42 -05001429 if (mp->is_ipv6)
1430 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001431 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001432 /* Do not send subnet details of the IP-interface for
1433 * unnumbered interfaces. otherwise listening clients
1434 * will be confused that the subnet is applied on more
1435 * than one interface */
1436 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001437 ({
1438 r6 = ip_interface_address_get_address (lm6, ia);
1439 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001440 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001441 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001442 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001443 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001444 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001445 else
1446 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001447 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001448 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001449 ({
1450 r4 = ip_interface_address_get_address (lm4, ia);
1451 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001452 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001453 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001454 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001455 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001456 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001457
Dave Barachb5e8a772016-12-06 12:04:42 -05001458 BAD_SW_IF_INDEX_LABEL;
1459}
1460
1461static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001462send_ip_unnumbered_details (vpe_api_main_t * am,
1463 vl_api_registration_t * reg,
1464 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1465{
1466 vl_api_ip_unnumbered_details_t *mp;
1467
1468 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001469 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001470 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1471
1472 mp->context = context;
1473 mp->sw_if_index = htonl (sw_if_index);
1474 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1475
1476 vl_api_send_msg (reg, (u8 *) mp);
1477}
1478
1479static void
1480vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1481{
1482 vnet_main_t *vnm = vnet_get_main ();
1483 vnet_interface_main_t *im = &vnm->interface_main;
1484 int rv __attribute__ ((unused)) = 0;
1485 vpe_api_main_t *am = &vpe_api_main;
1486 vl_api_registration_t *reg;
1487 vnet_sw_interface_t *si;
1488 u32 sw_if_index;
1489
1490 sw_if_index = ntohl (mp->sw_if_index);
1491
1492 reg = vl_api_client_index_to_registration (mp->client_index);
1493 if (!reg)
1494 return;
1495
1496 if (~0 != sw_if_index)
1497 {
1498 VALIDATE_SW_IF_INDEX (mp);
1499
1500 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1501
1502 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1503 {
1504 send_ip_unnumbered_details (am, reg,
1505 sw_if_index,
1506 si->unnumbered_sw_if_index,
1507 mp->context);
1508 }
1509 }
1510 else
1511 {
1512 /* *INDENT-OFF* */
1513 pool_foreach (si, im->sw_interfaces,
1514 ({
1515 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1516 {
1517 send_ip_unnumbered_details(am, reg,
1518 si->sw_if_index,
1519 si->unnumbered_sw_if_index,
1520 mp->context);
1521 }
1522 }));
1523 /* *INDENT-ON* */
1524 }
1525
1526 BAD_SW_IF_INDEX_LABEL;
1527}
1528
1529static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001530vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1531{
1532 vpe_api_main_t *am = &vpe_api_main;
1533 vnet_main_t *vnm = vnet_get_main ();
1534 vlib_main_t *vm = vlib_get_main ();
1535 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001536 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001537 vnet_sw_interface_t *si, *sorted_sis;
1538 u32 sw_if_index = ~0;
1539
Florin Coras6c4dae22018-01-09 06:39:23 -08001540 reg = vl_api_client_index_to_registration (mp->client_index);
1541 if (!reg)
1542 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001543
1544 /* Gather interfaces. */
1545 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1546 _vec_len (sorted_sis) = 0;
1547 /* *INDENT-OFF* */
1548 pool_foreach (si, im->sw_interfaces,
1549 ({
1550 vec_add1 (sorted_sis, si[0]);
1551 }));
1552 /* *INDENT-ON* */
1553
1554 vec_foreach (si, sorted_sis)
1555 {
1556 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1557 {
1558 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1559 {
1560 continue;
1561 }
1562 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001563 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001564 }
1565 }
1566}
1567
1568static void
1569set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1570{
1571 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001572 int rv;
1573 u32 table_id;
1574 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001575
Neale Ranns227038a2017-04-21 01:07:59 -07001576 table_id = ntohl (mp->vrf_id);
1577
1578#define _(a,b) if (mp->a) flow_hash_config |= b;
1579 foreach_flow_hash_bit;
1580#undef _
1581
1582 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001583
1584 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1585}
1586
1587static void
1588set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1589{
1590 vl_api_set_ip_flow_hash_reply_t *rmp;
1591 int rv;
1592 u32 table_id;
1593 flow_hash_config_t flow_hash_config = 0;
1594
1595 table_id = ntohl (mp->vrf_id);
1596
1597#define _(a,b) if (mp->a) flow_hash_config |= b;
1598 foreach_flow_hash_bit;
1599#undef _
1600
1601 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1602
1603 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1604}
1605
1606
1607static void
1608vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1609{
1610 if (mp->is_ipv6 == 0)
1611 set_ip4_flow_hash (mp);
1612 else
1613 set_ip6_flow_hash (mp);
1614}
1615
1616static void
1617 vl_api_sw_interface_ip6nd_ra_config_t_handler
1618 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1619{
1620 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1621 vlib_main_t *vm = vlib_get_main ();
1622 int rv = 0;
1623 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1624 default_router;
1625
1626 is_no = mp->is_no == 1;
1627 suppress = mp->suppress == 1;
1628 managed = mp->managed == 1;
1629 other = mp->other == 1;
1630 ll_option = mp->ll_option == 1;
1631 send_unicast = mp->send_unicast == 1;
1632 cease = mp->cease == 1;
1633 default_router = mp->default_router == 1;
1634
1635 VALIDATE_SW_IF_INDEX (mp);
1636
1637 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1638 suppress, managed, other,
1639 ll_option, send_unicast, cease,
1640 default_router, ntohl (mp->lifetime),
1641 ntohl (mp->initial_count),
1642 ntohl (mp->initial_interval),
1643 ntohl (mp->max_interval),
1644 ntohl (mp->min_interval), is_no);
1645
1646 BAD_SW_IF_INDEX_LABEL;
1647
1648 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1649}
1650
1651static void
1652 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1653 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1654{
1655 vlib_main_t *vm = vlib_get_main ();
1656 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1657 int rv = 0;
1658 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1659
1660 VALIDATE_SW_IF_INDEX (mp);
1661
1662 is_no = mp->is_no == 1;
1663 use_default = mp->use_default == 1;
1664 no_advertise = mp->no_advertise == 1;
1665 off_link = mp->off_link == 1;
1666 no_autoconfig = mp->no_autoconfig == 1;
1667 no_onlink = mp->no_onlink == 1;
1668
1669 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1670 (ip6_address_t *) mp->address,
1671 mp->address_length, use_default,
1672 ntohl (mp->val_lifetime),
1673 ntohl (mp->pref_lifetime), no_advertise,
1674 off_link, no_autoconfig, no_onlink, is_no);
1675
1676 BAD_SW_IF_INDEX_LABEL;
1677 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1678}
1679
1680static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001681send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001682 u32 context,
1683 const ip46_address_t * addr, u32 sw_if_index)
1684{
1685 vl_api_ip6nd_proxy_details_t *mp;
1686
1687 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001688 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns3f844d02017-02-18 00:03:54 -08001689 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1690 mp->context = context;
1691 mp->sw_if_index = htonl (sw_if_index);
1692 memcpy (mp->address, addr, 16);
1693
Florin Coras6c4dae22018-01-09 06:39:23 -08001694 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001695}
1696
Neale Ranns3f844d02017-02-18 00:03:54 -08001697typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1698{
1699 u32 *indices;
1700} api_ip6nd_proxy_fib_table_walk_ctx_t;
1701
Neale Ranns89541992017-04-06 04:41:02 -07001702static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001703api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1704{
1705 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1706
1707 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1708 {
1709 vec_add1 (ctx->indices, fei);
1710 }
1711
Neale Ranns89541992017-04-06 04:41:02 -07001712 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001713}
1714
1715static void
1716vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1717{
1718 ip6_main_t *im6 = &ip6_main;
1719 fib_table_t *fib_table;
1720 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1721 .indices = NULL,
1722 };
1723 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001724 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001725 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001726
Florin Coras6c4dae22018-01-09 06:39:23 -08001727 reg = vl_api_client_index_to_registration (mp->client_index);
1728 if (!reg)
1729 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001730
1731 /* *INDENT-OFF* */
1732 pool_foreach (fib_table, im6->fibs,
1733 ({
1734 fib_table_walk(fib_table->ft_index,
1735 FIB_PROTOCOL_IP6,
1736 api_ip6nd_proxy_fib_table_walk,
1737 &ctx);
1738 }));
1739 /* *INDENT-ON* */
1740
1741 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1742
1743 vec_foreach (feip, ctx.indices)
1744 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001745 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001746
Florin Coras6c4dae22018-01-09 06:39:23 -08001747 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001748 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001749 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001750 fib_entry_get_resolving_interface (*feip));
1751 }
1752
1753 vec_free (ctx.indices);
1754}
1755
1756static void
1757vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1758{
1759 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1760 int rv = 0;
1761
1762 VALIDATE_SW_IF_INDEX (mp);
1763
1764 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1765 (ip6_address_t *) mp->address, mp->is_del);
1766
1767 BAD_SW_IF_INDEX_LABEL;
1768 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1769}
1770
1771static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001772 vl_api_ip6nd_send_router_solicitation_t_handler
1773 (vl_api_ip6nd_send_router_solicitation_t * mp)
1774{
1775 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1776 icmp6_send_router_solicitation_params_t params;
1777 vlib_main_t *vm = vlib_get_main ();
1778 int rv = 0;
1779
1780 VALIDATE_SW_IF_INDEX (mp);
1781
1782 BAD_SW_IF_INDEX_LABEL;
1783 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1784
1785 if (rv != 0)
1786 return;
1787
1788 params.irt = ntohl (mp->irt);
1789 params.mrt = ntohl (mp->mrt);
1790 params.mrc = ntohl (mp->mrc);
1791 params.mrd = ntohl (mp->mrd);
1792
1793 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1794 &params);
1795}
1796
1797static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001798 vl_api_sw_interface_ip6_enable_disable_t_handler
1799 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1800{
1801 vlib_main_t *vm = vlib_get_main ();
1802 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1803 vnet_main_t *vnm = vnet_get_main ();
1804 int rv = 0;
1805 clib_error_t *error;
1806
1807 vnm->api_errno = 0;
1808
1809 VALIDATE_SW_IF_INDEX (mp);
1810
1811 error =
1812 (mp->enable == 1) ? enable_ip6_interface (vm,
1813 ntohl (mp->sw_if_index)) :
1814 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1815
1816 if (error)
1817 {
1818 clib_error_report (error);
1819 rv = VNET_API_ERROR_UNSPECIFIED;
1820 }
1821 else
1822 {
1823 rv = vnm->api_errno;
1824 }
1825
1826 BAD_SW_IF_INDEX_LABEL;
1827
1828 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1829}
1830
Neale Ranns32e1c012016-11-22 17:07:28 +00001831void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001832vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001833 u32 context, const mfib_signal_t * mfs)
1834{
1835 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001836 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001837 mfib_table_t *mfib;
1838 mfib_itf_t *mfi;
1839
1840 mp = vl_msg_api_alloc (sizeof (*mp));
1841
Dave Barachb7b92992018-10-17 10:38:51 -04001842 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001843 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1844 mp->context = context;
1845
1846 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001847 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001848 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001849 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001850 mp->table_id = ntohl (mfib->mft_table_id);
1851 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1852
Neale Ranns9e829a82018-12-17 05:50:32 -08001853 if (FIB_PROTOCOL_IP4 == prefix->fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +00001854 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001855 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001856
Neale Ranns9e829a82018-12-17 05:50:32 -08001857 memcpy (mp->grp_address, &prefix->fp_grp_addr.ip4, 4);
1858 if (prefix->fp_len > 32)
Neale Ranns32e1c012016-11-22 17:07:28 +00001859 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001860 memcpy (mp->src_address, &prefix->fp_src_addr.ip4, 4);
Neale Ranns32e1c012016-11-22 17:07:28 +00001861 }
1862 }
1863 else
1864 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001865 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001866
1867 ASSERT (0);
1868 }
1869
1870 if (0 != mfs->mfs_buffer_len)
1871 {
1872 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1873
1874 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1875 }
1876 else
1877 {
1878 mp->ip_packet_len = 0;
1879 }
1880
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001881 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001882}
1883
1884static void
1885vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1886{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001887 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001888
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001889 reg = vl_api_client_index_to_registration (mp->client_index);
1890 if (!reg)
1891 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001892
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001893 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001894 ;
1895}
Dave Barachb5e8a772016-12-06 12:04:42 -05001896
Florin Coras595992c2017-11-06 17:17:08 -08001897static void
1898 vl_api_ip_container_proxy_add_del_t_handler
1899 (vl_api_ip_container_proxy_add_del_t * mp)
1900{
1901 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1902 vnet_ip_container_proxy_args_t args;
1903 int rv = 0;
1904 clib_error_t *error;
1905
Dave Barachb7b92992018-10-17 10:38:51 -04001906 clib_memset (&args, 0, sizeof (args));
Florin Coras595992c2017-11-06 17:17:08 -08001907 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1908 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1909 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1910 args.is_add = mp->is_add;
1911 if ((error = vnet_ip_container_proxy_add_del (&args)))
1912 {
1913 rv = clib_error_get_code (error);
1914 clib_error_report (error);
1915 }
1916
1917 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1918}
1919
Matus Fabian75b9f452018-10-02 23:27:21 -07001920typedef struct ip_container_proxy_walk_ctx_t_
1921{
1922 vl_api_registration_t *reg;
1923 u32 context;
1924} ip_container_proxy_walk_ctx_t;
1925
1926static int
1927ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1928 void *args)
1929{
1930 vl_api_ip_container_proxy_details_t *mp;
1931 ip_container_proxy_walk_ctx_t *ctx = args;
1932
1933 mp = vl_msg_api_alloc (sizeof (*mp));
1934 if (!mp)
1935 return 1;
1936
Dave Barachb7b92992018-10-17 10:38:51 -04001937 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001938 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1939 mp->context = ctx->context;
1940
1941 mp->sw_if_index = ntohl (sw_if_index);
1942 ip_prefix_encode (pfx, &mp->prefix);
1943
1944 vl_api_send_msg (ctx->reg, (u8 *) mp);
1945
1946 return 1;
1947}
1948
1949static void
1950vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1951 mp)
1952{
1953 vl_api_registration_t *reg;
1954
1955 reg = vl_api_client_index_to_registration (mp->client_index);
1956 if (!reg)
1957 return;
1958
1959 ip_container_proxy_walk_ctx_t ctx = {
1960 .context = mp->context,
1961 .reg = reg,
1962 };
1963
1964 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1965}
1966
Neale Rannsb8d44812017-11-10 06:53:54 -08001967static void
1968vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1969{
1970 int rv = 0;
1971 vl_api_ioam_enable_reply_t *rmp;
1972 clib_error_t *error;
1973
1974 /* Ignoring the profile id as currently a single profile
1975 * is supported */
1976 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1977 mp->seqno, mp->analyse);
1978 if (error)
1979 {
1980 clib_error_report (error);
1981 rv = clib_error_get_code (error);
1982 }
1983
1984 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1985}
1986
1987static void
1988vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1989{
1990 int rv = 0;
1991 vl_api_ioam_disable_reply_t *rmp;
1992 clib_error_t *error;
1993
1994 error = clear_ioam_rewrite_fn ();
1995 if (error)
1996 {
1997 clib_error_report (error);
1998 rv = clib_error_get_code (error);
1999 }
2000
2001 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2002}
2003
2004static void
2005 vl_api_ip_source_and_port_range_check_add_del_t_handler
2006 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2007{
2008 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2009 int rv = 0;
2010
2011 u8 is_ipv6 = mp->is_ipv6;
2012 u8 is_add = mp->is_add;
2013 u8 mask_length = mp->mask_length;
2014 ip4_address_t ip4_addr;
2015 ip6_address_t ip6_addr;
2016 u16 *low_ports = 0;
2017 u16 *high_ports = 0;
2018 u32 vrf_id;
2019 u16 tmp_low, tmp_high;
2020 u8 num_ranges;
2021 int i;
2022
2023 // Validate port range
2024 num_ranges = mp->number_of_ranges;
2025 if (num_ranges > 32)
2026 { // This is size of array in VPE.API
2027 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2028 goto reply;
2029 }
2030
2031 vec_reset_length (low_ports);
2032 vec_reset_length (high_ports);
2033
2034 for (i = 0; i < num_ranges; i++)
2035 {
2036 tmp_low = mp->low_ports[i];
2037 tmp_high = mp->high_ports[i];
2038 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2039 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2040 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2041 {
2042 rv = VNET_API_ERROR_INVALID_VALUE;
2043 goto reply;
2044 }
2045 vec_add1 (low_ports, tmp_low);
2046 vec_add1 (high_ports, tmp_high + 1);
2047 }
2048
2049 // Validate mask_length
2050 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
2051 {
2052 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
2053 goto reply;
2054 }
2055
2056 vrf_id = ntohl (mp->vrf_id);
2057
2058 if (vrf_id < 1)
2059 {
2060 rv = VNET_API_ERROR_INVALID_VALUE;
2061 goto reply;
2062 }
2063
2064
2065 if (is_ipv6)
2066 {
2067 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
2068 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
2069 mask_length,
2070 vrf_id,
2071 low_ports,
2072 high_ports, is_add);
2073 }
2074 else
2075 {
2076 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
2077 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
2078 mask_length,
2079 vrf_id,
2080 low_ports,
2081 high_ports, is_add);
2082 }
2083
2084reply:
2085 vec_free (low_ports);
2086 vec_free (high_ports);
2087 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2088}
2089
2090static void
2091 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2092 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2093{
2094 vlib_main_t *vm = vlib_get_main ();
2095 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2096 ip4_main_t *im = &ip4_main;
2097 int rv;
2098 u32 sw_if_index;
2099 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2100 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2101 uword *p = 0;
2102 int i;
2103
2104 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2105 ntohl (mp->tcp_out_vrf_id);
2106 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2107 ntohl (mp->udp_out_vrf_id);
2108 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2109 ntohl (mp->tcp_in_vrf_id);
2110 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2111 ntohl (mp->udp_in_vrf_id);
2112
2113
2114 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2115 {
2116 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2117 {
2118 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2119
2120 if (p == 0)
2121 {
2122 rv = VNET_API_ERROR_INVALID_VALUE;
2123 goto reply;
2124 }
2125
2126 fib_index[i] = p[0];
2127 }
2128 else
2129 fib_index[i] = ~0;
2130 }
2131 sw_if_index = ntohl (mp->sw_if_index);
2132
2133 VALIDATE_SW_IF_INDEX (mp);
2134
2135 rv =
2136 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2137 mp->is_add);
2138
2139 BAD_SW_IF_INDEX_LABEL;
2140reply:
2141
2142 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2143}
2144
Chore3460b012018-11-28 10:53:11 +03302145typedef union
2146{
2147 u32 fib_index;
2148} ip4_source_check_config_t;
2149
2150static void
2151 vl_api_ip_source_check_interface_add_del_t_handler
2152 (vl_api_ip_source_check_interface_add_del_t * mp)
2153{
2154 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
2155 int rv;
2156 u32 sw_if_index = ntohl (mp->sw_if_index);
2157 u8 is_add = mp->is_add;
2158 char *feature_name =
2159 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
2160
2161 ip4_source_check_config_t config;
2162
2163 VALIDATE_SW_IF_INDEX (mp);
2164
2165 config.fib_index =
2166 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
2167 rv =
2168 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
2169 is_add, &config, sizeof (config));
2170 BAD_SW_IF_INDEX_LABEL;
2171
2172 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
2173}
2174
Neale Rannsb8d44812017-11-10 06:53:54 -08002175#define IP4_ARP_EVENT 3
2176#define IP6_ND_EVENT 4
2177
2178static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2179static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2180static vlib_node_registration_t ip_resolver_process_node;
2181
2182static void
2183handle_ip4_arp_event (u32 pool_index)
2184{
2185 vpe_api_main_t *vam = &vpe_api_main;
2186 vnet_main_t *vnm = vam->vnet_main;
2187 vlib_main_t *vm = vam->vlib_main;
2188 vl_api_ip4_arp_event_t *event;
2189 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002190 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002191
2192 /* Client can cancel, die, etc. */
2193 if (pool_is_free_index (vam->arp_events, pool_index))
2194 return;
2195
2196 event = pool_elt_at_index (vam->arp_events, pool_index);
2197
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002198 reg = vl_api_client_index_to_registration (event->client_index);
2199 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002200 {
2201 (void) vnet_add_del_ip4_arp_change_event
2202 (vnm, arp_change_delete_callback,
2203 event->pid, &event->address,
2204 ip_resolver_process_node.index, IP4_ARP_EVENT,
2205 ~0 /* pool index, notused */ , 0 /* is_add */ );
2206 return;
2207 }
2208
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002209 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002210 {
2211 mp = vl_msg_api_alloc (sizeof (*mp));
2212 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002213 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002214 }
2215 else
2216 {
2217 static f64 last_time;
2218 /*
2219 * Throttle syslog msgs.
2220 * It's pretty tempting to just revoke the registration...
2221 */
2222 if (vlib_time_now (vm) > last_time + 10.0)
2223 {
2224 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2225 format_ip4_address, &event->address, event->pid);
2226 last_time = vlib_time_now (vm);
2227 }
2228 }
2229}
2230
Neale Rannsf12dad62018-06-04 18:41:24 -07002231static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002232handle_ip6_nd_event (u32 pool_index)
2233{
2234 vpe_api_main_t *vam = &vpe_api_main;
2235 vnet_main_t *vnm = vam->vnet_main;
2236 vlib_main_t *vm = vam->vlib_main;
2237 vl_api_ip6_nd_event_t *event;
2238 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002239 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002240
2241 /* Client can cancel, die, etc. */
2242 if (pool_is_free_index (vam->nd_events, pool_index))
2243 return;
2244
2245 event = pool_elt_at_index (vam->nd_events, pool_index);
2246
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002247 reg = vl_api_client_index_to_registration (event->client_index);
2248 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002249 {
2250 (void) vnet_add_del_ip6_nd_change_event
2251 (vnm, nd_change_delete_callback,
2252 event->pid, &event->address,
2253 ip_resolver_process_node.index, IP6_ND_EVENT,
2254 ~0 /* pool index, notused */ , 0 /* is_add */ );
2255 return;
2256 }
2257
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002258 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002259 {
2260 mp = vl_msg_api_alloc (sizeof (*mp));
2261 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002262 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002263 }
2264 else
2265 {
2266 static f64 last_time;
2267 /*
2268 * Throttle syslog msgs.
2269 * It's pretty tempting to just revoke the registration...
2270 */
2271 if (vlib_time_now (vm) > last_time + 10.0)
2272 {
2273 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2274 format_ip6_address, &event->address, event->pid);
2275 last_time = vlib_time_now (vm);
2276 }
2277 }
2278}
2279
2280static uword
2281resolver_process (vlib_main_t * vm,
2282 vlib_node_runtime_t * rt, vlib_frame_t * f)
2283{
2284 volatile f64 timeout = 100.0;
2285 volatile uword *event_data = 0;
2286
2287 while (1)
2288 {
2289 vlib_process_wait_for_event_or_clock (vm, timeout);
2290
2291 uword event_type =
2292 vlib_process_get_events (vm, (uword **) & event_data);
2293
2294 int i;
2295 switch (event_type)
2296 {
2297 case IP4_ARP_EVENT:
2298 for (i = 0; i < vec_len (event_data); i++)
2299 handle_ip4_arp_event (event_data[i]);
2300 break;
2301
2302 case IP6_ND_EVENT:
2303 for (i = 0; i < vec_len (event_data); i++)
2304 handle_ip6_nd_event (event_data[i]);
2305 break;
2306
2307 case ~0: /* timeout */
2308 break;
2309 }
2310
2311 vec_reset_length (event_data);
2312 }
2313 return 0; /* or not */
2314}
2315
2316/* *INDENT-OFF* */
2317VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2318 .function = resolver_process,
2319 .type = VLIB_NODE_TYPE_PROCESS,
2320 .name = "ip-route-resolver-process",
2321};
2322/* *INDENT-ON* */
2323
2324static int
2325nd_change_data_callback (u32 pool_index, u8 * new_mac,
2326 u32 sw_if_index, ip6_address_t * address)
2327{
2328 vpe_api_main_t *am = &vpe_api_main;
2329 vl_api_ip6_nd_event_t *event;
2330
2331 if (pool_is_free_index (am->nd_events, pool_index))
2332 return 1;
2333
2334 event = pool_elt_at_index (am->nd_events, pool_index);
2335 if (eth_mac_equal (event->new_mac, new_mac) &&
2336 sw_if_index == ntohl (event->sw_if_index))
2337 {
2338 return 1;
2339 }
2340
2341 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2342 event->sw_if_index = htonl (sw_if_index);
2343 return 0;
2344}
2345
2346static int
2347arp_change_delete_callback (u32 pool_index, u8 * notused)
2348{
2349 vpe_api_main_t *am = &vpe_api_main;
2350
2351 if (pool_is_free_index (am->arp_events, pool_index))
2352 return 1;
2353
2354 pool_put_index (am->arp_events, pool_index);
2355 return 0;
2356}
2357
2358static int
2359nd_change_delete_callback (u32 pool_index, u8 * notused)
2360{
2361 vpe_api_main_t *am = &vpe_api_main;
2362
2363 if (pool_is_free_index (am->nd_events, pool_index))
2364 return 1;
2365
2366 pool_put_index (am->nd_events, pool_index);
2367 return 0;
2368}
2369
2370static vlib_node_registration_t wc_arp_process_node;
2371
2372enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002373{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002374
2375static uword
2376wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2377{
2378 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2379 * and need to be volatile - to prevent them from being optimized into
2380 * a register - which could change during suspension */
2381
2382 volatile wc_arp_report_t arp_prev = { 0 };
2383 volatile wc_nd_report_t nd_prev = { 0 };
2384 volatile f64 last_arp = vlib_time_now (vm);
2385 volatile f64 last_nd = vlib_time_now (vm);
2386
2387 while (1)
2388 {
2389 vlib_process_wait_for_event (vm);
2390 uword event_type = WC_ARP_REPORT;
2391 void *event_data = vlib_process_get_event_data (vm, &event_type);
2392
2393 f64 now = vlib_time_now (vm);
2394 int i;
2395 if (event_type == WC_ARP_REPORT)
2396 {
2397 wc_arp_report_t *arp_events = event_data;
2398 for (i = 0; i < vec_len (arp_events); i++)
2399 {
2400 /* discard dup event */
2401 if (arp_prev.ip4 == arp_events[i].ip4 &&
2402 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2403 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2404 (now - last_arp) < 10.0)
2405 {
2406 continue;
2407 }
2408 arp_prev = arp_events[i];
2409 last_arp = now;
2410 vpe_client_registration_t *reg;
2411 /* *INDENT-OFF* */
2412 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2413 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002414 vl_api_registration_t *vl_reg;
2415 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002416 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002417 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002418 {
2419 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002420 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002421 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2422 event->client_index = reg->client_index;
2423 event->pid = reg->client_pid;
2424 event->mac_ip = 1;
2425 event->address = arp_events[i].ip4;
2426 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2427 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002428 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002429 }
2430 }));
2431 /* *INDENT-ON* */
2432 }
2433 }
2434 else if (event_type == WC_ND_REPORT)
2435 {
2436 wc_nd_report_t *nd_events = event_data;
2437 for (i = 0; i < vec_len (nd_events); i++)
2438 {
2439 /* discard dup event */
2440 if (ip6_address_is_equal
2441 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2442 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2443 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2444 && (now - last_nd) < 10.0)
2445 {
2446 continue;
2447 }
2448 nd_prev = nd_events[i];
2449 last_nd = now;
2450 vpe_client_registration_t *reg;
2451 /* *INDENT-OFF* */
2452 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2453 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002454 vl_api_registration_t *vl_reg;
2455 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2456 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002457 {
2458 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002459 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002460 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2461 event->client_index = reg->client_index;
2462 event->pid = reg->client_pid;
2463 event->mac_ip = 1;
2464 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2465 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2466 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002467 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002468 }
2469 }));
2470 /* *INDENT-ON* */
2471 }
2472 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002473 else if (event_type == RA_REPORT)
2474 {
2475 ra_report_t *ra_events = event_data;
2476 for (i = 0; i < vec_len (ra_events); i++)
2477 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002478 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2479 call_ip6_neighbor_callbacks (&ra_events[i],
2480 npm->ra_report_functions);
2481
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002482 vpe_client_registration_t *reg;
2483 /* *INDENT-OFF* */
2484 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2485 ({
2486 vl_api_registration_t *vl_reg;
2487 vl_reg =
2488 vl_api_client_index_to_registration (reg->client_index);
2489 if (vl_reg && vl_api_can_send_msg (vl_reg))
2490 {
2491 u32 event_size =
2492 sizeof (vl_api_ip6_ra_event_t) +
2493 vec_len (ra_events[i].prefixes) *
2494 sizeof (vl_api_ip6_ra_prefix_info_t);
2495 vl_api_ip6_ra_event_t *event =
2496 vl_msg_api_alloc (event_size);
Dave Barachb7b92992018-10-17 10:38:51 -04002497 clib_memset (event, 0, event_size);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002498 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2499 event->client_index = reg->client_index;
2500 event->pid = reg->client_pid;
2501
2502 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2503
2504 memcpy (event->router_address, ra_events[i].router_address, 16);
2505
2506 event->current_hop_limit = ra_events[i].current_hop_limit;
2507 event->flags = ra_events[i].flags;
2508 event->router_lifetime_in_sec =
2509 clib_host_to_net_u16 (ra_events
2510 [i].router_lifetime_in_sec);
2511 event->neighbor_reachable_time_in_msec =
2512 clib_host_to_net_u32 (ra_events
2513 [i].neighbor_reachable_time_in_msec);
2514 event->time_in_msec_between_retransmitted_neighbor_solicitations
2515 =
2516 clib_host_to_net_u32 (ra_events
2517 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2518
2519 event->n_prefixes =
2520 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2521 vl_api_ip6_ra_prefix_info_t *prefix =
2522 (typeof (prefix)) event->prefixes;
2523 u32 j;
2524 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2525 {
2526 ra_report_prefix_info_t *info =
2527 &ra_events[i].prefixes[j];
2528 memcpy (prefix->dst_address, info->dst_address.as_u8,
2529 16);
2530 prefix->dst_address_length = info->dst_address_length;
2531 prefix->flags = info->flags;
2532 prefix->valid_time =
2533 clib_host_to_net_u32 (info->valid_time);
2534 prefix->preferred_time =
2535 clib_host_to_net_u32 (info->preferred_time);
2536 prefix++;
2537 }
2538
2539 vl_api_send_msg (vl_reg, (u8 *) event);
2540 }
2541 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002542 /* *INDENT-ON* */
2543 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002544 }
2545 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002546 vlib_process_put_event_data (vm, event_data);
2547 }
2548
2549 return 0;
2550}
2551
2552/* *INDENT-OFF* */
2553VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2554 .function = wc_arp_process,
2555 .type = VLIB_NODE_TYPE_PROCESS,
2556 .name = "wildcard-ip4-arp-publisher-process",
2557};
2558/* *INDENT-ON* */
2559
2560static int
2561arp_change_data_callback (u32 pool_index, u8 * new_mac,
2562 u32 sw_if_index, u32 address)
2563{
2564 vpe_api_main_t *am = &vpe_api_main;
2565 vl_api_ip4_arp_event_t *event;
2566
2567 if (pool_is_free_index (am->arp_events, pool_index))
2568 return 1;
2569
2570 event = pool_elt_at_index (am->arp_events, pool_index);
2571 if (eth_mac_equal (event->new_mac, new_mac) &&
2572 sw_if_index == ntohl (event->sw_if_index))
2573 {
2574 return 1;
2575 }
2576
2577 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2578 event->sw_if_index = htonl (sw_if_index);
2579 return 0;
2580}
2581
2582static void
2583vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2584{
2585 vpe_api_main_t *am = &vpe_api_main;
2586 vnet_main_t *vnm = vnet_get_main ();
2587 vl_api_want_ip4_arp_events_reply_t *rmp;
2588 int rv = 0;
2589
2590 if (mp->address == 0)
2591 {
2592 uword *p =
2593 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2594 vpe_client_registration_t *rp;
2595 if (p)
2596 {
2597 if (mp->enable_disable)
2598 {
2599 clib_warning ("pid %d: already enabled...", mp->pid);
2600 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2601 goto reply;
2602 }
2603 else
2604 {
2605 rp =
2606 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2607 pool_put (am->wc_ip4_arp_events_registrations, rp);
2608 hash_unset (am->wc_ip4_arp_events_registration_hash,
2609 mp->client_index);
2610 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002611 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002612 goto reply;
2613 }
2614 }
2615 if (mp->enable_disable == 0)
2616 {
2617 clib_warning ("pid %d: already disabled...", mp->pid);
2618 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2619 goto reply;
2620 }
2621 pool_get (am->wc_ip4_arp_events_registrations, rp);
2622 rp->client_index = mp->client_index;
2623 rp->client_pid = mp->pid;
2624 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2625 rp - am->wc_ip4_arp_events_registrations);
2626 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2627 goto reply;
2628 }
2629
2630 if (mp->enable_disable)
2631 {
2632 vl_api_ip4_arp_event_t *event;
2633 pool_get (am->arp_events, event);
2634 rv = vnet_add_del_ip4_arp_change_event
2635 (vnm, arp_change_data_callback,
2636 mp->pid, &mp->address /* addr, in net byte order */ ,
2637 ip_resolver_process_node.index,
2638 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2639
2640 if (rv)
2641 {
2642 pool_put (am->arp_events, event);
2643 goto reply;
2644 }
Dave Barachb7b92992018-10-17 10:38:51 -04002645 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002646
2647 /* Python API expects events to have no context */
2648 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2649 event->client_index = mp->client_index;
2650 event->address = mp->address;
2651 event->pid = mp->pid;
2652 if (mp->address == 0)
2653 event->mac_ip = 1;
2654 }
2655 else
2656 {
2657 rv = vnet_add_del_ip4_arp_change_event
2658 (vnm, arp_change_delete_callback,
2659 mp->pid, &mp->address /* addr, in net byte order */ ,
2660 ip_resolver_process_node.index,
2661 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2662 }
2663reply:
2664 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2665}
2666
Neale Rannsf12dad62018-06-04 18:41:24 -07002667static clib_error_t *
2668want_ip4_arp_events_reaper (u32 client_index)
2669{
2670 vpe_client_registration_t *rp;
2671 vl_api_ip4_arp_event_t *event;
2672 u32 *to_delete, *event_id;
2673 vpe_api_main_t *am;
2674 vnet_main_t *vnm;
2675 uword *p;
2676
2677 am = &vpe_api_main;
2678 vnm = vnet_get_main ();
2679 to_delete = NULL;
2680
2681 /* clear out all of its pending resolutions */
2682 /* *INDENT-OFF* */
2683 pool_foreach(event, am->arp_events,
2684 ({
2685 if (event->client_index == client_index)
2686 {
2687 vec_add1(to_delete, event - am->arp_events);
2688 }
2689 }));
2690 /* *INDENT-ON* */
2691
2692 vec_foreach (event_id, to_delete)
2693 {
2694 event = pool_elt_at_index (am->arp_events, *event_id);
2695 vnet_add_del_ip4_arp_change_event
2696 (vnm, arp_change_delete_callback,
2697 event->pid, &event->address,
2698 ip_resolver_process_node.index, IP4_ARP_EVENT,
2699 ~0 /* pool index, notused */ , 0 /* is_add */ );
2700 }
2701 vec_free (to_delete);
2702
2703 /* remove from the registration hash */
2704 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2705
2706 if (p)
2707 {
2708 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2709 pool_put (am->wc_ip4_arp_events_registrations, rp);
2710 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2711 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2712 wc_arp_set_publisher_node (~0, REPORT_MAX);
2713 }
2714 return (NULL);
2715}
2716
2717VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2718
Neale Rannsb8d44812017-11-10 06:53:54 -08002719static void
2720vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2721{
2722 vpe_api_main_t *am = &vpe_api_main;
2723 vnet_main_t *vnm = vnet_get_main ();
2724 vl_api_want_ip6_nd_events_reply_t *rmp;
2725 int rv = 0;
2726
2727 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2728 {
2729 uword *p =
2730 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2731 vpe_client_registration_t *rp;
2732 if (p)
2733 {
2734 if (mp->enable_disable)
2735 {
2736 clib_warning ("pid %d: already enabled...", mp->pid);
2737 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2738 goto reply;
2739 }
2740 else
2741 {
2742 rp =
2743 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2744 pool_put (am->wc_ip6_nd_events_registrations, rp);
2745 hash_unset (am->wc_ip6_nd_events_registration_hash,
2746 mp->client_index);
2747 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002748 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002749 goto reply;
2750 }
2751 }
2752 if (mp->enable_disable == 0)
2753 {
2754 clib_warning ("pid %d: already disabled...", mp->pid);
2755 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2756 goto reply;
2757 }
2758 pool_get (am->wc_ip6_nd_events_registrations, rp);
2759 rp->client_index = mp->client_index;
2760 rp->client_pid = mp->pid;
2761 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2762 rp - am->wc_ip6_nd_events_registrations);
2763 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2764 goto reply;
2765 }
2766
2767 if (mp->enable_disable)
2768 {
2769 vl_api_ip6_nd_event_t *event;
2770 pool_get (am->nd_events, event);
2771
2772 rv = vnet_add_del_ip6_nd_change_event
2773 (vnm, nd_change_data_callback,
2774 mp->pid, mp->address /* addr, in net byte order */ ,
2775 ip_resolver_process_node.index,
2776 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2777
2778 if (rv)
2779 {
2780 pool_put (am->nd_events, event);
2781 goto reply;
2782 }
Dave Barachb7b92992018-10-17 10:38:51 -04002783 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002784
2785 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2786 event->client_index = mp->client_index;
2787 clib_memcpy (event->address, mp->address, sizeof event->address);
2788 event->pid = mp->pid;
2789 }
2790 else
2791 {
2792 rv = vnet_add_del_ip6_nd_change_event
2793 (vnm, nd_change_delete_callback,
2794 mp->pid, mp->address /* addr, in net byte order */ ,
2795 ip_resolver_process_node.index,
2796 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2797 }
2798reply:
2799 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2800}
2801
Neale Rannsf12dad62018-06-04 18:41:24 -07002802static clib_error_t *
2803want_ip6_nd_events_reaper (u32 client_index)
2804{
2805
2806 vpe_client_registration_t *rp;
2807 vl_api_ip6_nd_event_t *event;
2808 u32 *to_delete, *event_id;
2809 vpe_api_main_t *am;
2810 vnet_main_t *vnm;
2811 uword *p;
2812
2813 am = &vpe_api_main;
2814 vnm = vnet_get_main ();
2815 to_delete = NULL;
2816
2817 /* clear out all of its pending resolutions */
2818 /* *INDENT-OFF* */
2819 pool_foreach(event, am->nd_events,
2820 ({
2821 if (event->client_index == client_index)
2822 {
2823 vec_add1(to_delete, event - am->nd_events);
2824 }
2825 }));
2826 /* *INDENT-ON* */
2827
2828 vec_foreach (event_id, to_delete)
2829 {
2830 event = pool_elt_at_index (am->nd_events, *event_id);
2831 vnet_add_del_ip6_nd_change_event
2832 (vnm, nd_change_delete_callback,
2833 event->pid, &event->address,
2834 ip_resolver_process_node.index, IP6_ND_EVENT,
2835 ~0 /* pool index, notused */ , 0 /* is_add */ );
2836 }
2837 vec_free (to_delete);
2838
2839 /* remove from the registration hash */
2840 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2841
2842 if (p)
2843 {
2844 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2845 pool_put (am->wc_ip6_nd_events_registrations, rp);
2846 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2847 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2848 wc_nd_set_publisher_node (~0, REPORT_MAX);
2849 }
2850 return (NULL);
2851}
2852
2853VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2854
Neale Rannsb8d44812017-11-10 06:53:54 -08002855static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002856vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2857{
2858 vpe_api_main_t *am = &vpe_api_main;
2859 vl_api_want_ip6_ra_events_reply_t *rmp;
2860 int rv = 0;
2861
2862 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2863 vpe_client_registration_t *rp;
2864 if (p)
2865 {
2866 if (mp->enable_disable)
2867 {
2868 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2869 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2870 goto reply;
2871 }
2872 else
2873 {
2874 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2875 pool_put (am->ip6_ra_events_registrations, rp);
2876 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002877 goto reply;
2878 }
2879 }
2880 if (mp->enable_disable == 0)
2881 {
2882 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2883 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2884 goto reply;
2885 }
2886 pool_get (am->ip6_ra_events_registrations, rp);
2887 rp->client_index = mp->client_index;
2888 rp->client_pid = ntohl (mp->pid);
2889 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2890 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002891
2892reply:
2893 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2894}
2895
Neale Rannsf12dad62018-06-04 18:41:24 -07002896static clib_error_t *
2897want_ip6_ra_events_reaper (u32 client_index)
2898{
2899 vpe_api_main_t *am = &vpe_api_main;
2900 vpe_client_registration_t *rp;
2901 uword *p;
2902
2903 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2904
2905 if (p)
2906 {
2907 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2908 pool_put (am->ip6_ra_events_registrations, rp);
2909 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2910 }
2911 return (NULL);
2912}
2913
2914VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2915
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002916static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002917vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2918{
2919 vl_api_proxy_arp_add_del_reply_t *rmp;
2920 u32 fib_index;
2921 int rv;
2922 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002923 uword *p;
2924
Ole Troan3288ed72017-12-06 17:00:05 +01002925 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002926
Neale Ranns0053de62018-05-22 08:40:52 -07002927 p = hash_get (im->fib_index_by_table_id, ntohl (mp->proxy.vrf_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002928
2929 if (!p)
2930 {
2931 rv = VNET_API_ERROR_NO_SUCH_FIB;
2932 goto out;
2933 }
2934
2935 fib_index = p[0];
2936
Neale Ranns0053de62018-05-22 08:40:52 -07002937 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->proxy.low_address,
2938 (ip4_address_t *) mp->proxy.hi_address,
Neale Rannsb8d44812017-11-10 06:53:54 -08002939 fib_index, mp->is_add == 0);
2940
2941out:
Ole Troan3288ed72017-12-06 17:00:05 +01002942 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002943 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2944}
2945
Neale Ranns0053de62018-05-22 08:40:52 -07002946typedef struct proxy_arp_walk_ctx_t_
2947{
2948 vl_api_registration_t *reg;
2949 u32 context;
2950} proxy_arp_walk_ctx_t;
2951
2952static walk_rc_t
2953send_proxy_arp_details (const ip4_address_t * lo_addr,
2954 const ip4_address_t * hi_addr,
2955 u32 fib_index, void *data)
2956{
2957 vl_api_proxy_arp_details_t *mp;
2958 proxy_arp_walk_ctx_t *ctx;
2959
2960 ctx = data;
2961
2962 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04002963 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07002964 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
2965 mp->context = ctx->context;
2966 mp->proxy.vrf_id = htonl (fib_index);
2967 clib_memcpy (mp->proxy.low_address, lo_addr,
2968 sizeof (mp->proxy.low_address));
2969 clib_memcpy (mp->proxy.hi_address, hi_addr, sizeof (mp->proxy.hi_address));
2970
2971 vl_api_send_msg (ctx->reg, (u8 *) mp);
2972
2973 return (WALK_CONTINUE);
2974}
2975
2976static void
2977vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
2978{
2979 vl_api_registration_t *reg;
2980
2981 reg = vl_api_client_index_to_registration (mp->client_index);
2982 if (!reg)
2983 return;
2984
2985 proxy_arp_walk_ctx_t wctx = {
2986 .reg = reg,
2987 .context = mp->context,
2988 };
2989
2990 proxy_arp_walk (send_proxy_arp_details, &wctx);
2991}
2992
2993static walk_rc_t
2994send_proxy_arp_intfc_details (vnet_main_t * vnm,
2995 vnet_sw_interface_t * si, void *data)
2996{
2997 vl_api_proxy_arp_intfc_details_t *mp;
2998 proxy_arp_walk_ctx_t *ctx;
2999
3000 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
3001 return (WALK_CONTINUE);
3002
3003 ctx = data;
3004
3005 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003006 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003007 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
3008 mp->context = ctx->context;
3009 mp->sw_if_index = htonl (si->sw_if_index);
3010
3011 vl_api_send_msg (ctx->reg, (u8 *) mp);
3012
3013 return (WALK_CONTINUE);
3014}
3015
3016static void
3017vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
3018{
3019 vl_api_registration_t *reg;
3020
3021 reg = vl_api_client_index_to_registration (mp->client_index);
3022 if (!reg)
3023 return;
3024
3025 proxy_arp_walk_ctx_t wctx = {
3026 .reg = reg,
3027 .context = mp->context,
3028 };
3029
3030 vnet_sw_interface_walk (vnet_get_main (),
3031 send_proxy_arp_intfc_details, &wctx);
3032}
3033
Neale Rannsb8d44812017-11-10 06:53:54 -08003034static void
3035 vl_api_proxy_arp_intfc_enable_disable_t_handler
3036 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
3037{
3038 int rv = 0;
3039 vnet_main_t *vnm = vnet_get_main ();
3040 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
3041
3042 VALIDATE_SW_IF_INDEX (mp);
3043
3044 vnet_sw_interface_t *si =
3045 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
3046
3047 ASSERT (si);
3048
3049 if (mp->enable_disable)
3050 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3051 else
3052 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3053
3054 BAD_SW_IF_INDEX_LABEL;
3055
3056 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3057}
3058
John Loc7b43042018-04-13 16:46:22 -04003059static void
3060vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3061{
3062 int rv = 0;
3063 vlib_main_t *vm = vlib_get_main ();
3064 vl_api_ip_probe_neighbor_reply_t *rmp;
3065 clib_error_t *error;
3066
3067 VALIDATE_SW_IF_INDEX (mp);
3068
3069 u32 sw_if_index = ntohl (mp->sw_if_index);
3070
3071 if (mp->is_ipv6)
3072 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003073 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003074 else
3075 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
John Lo86376342018-06-11 20:14:49 -04003076 sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003077
3078 if (error)
3079 {
3080 clib_error_report (error);
3081 rv = clib_error_get_code (error);
3082 }
3083
3084 BAD_SW_IF_INDEX_LABEL;
3085
3086 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3087}
3088
John Lo7f358b32018-04-28 01:19:24 -04003089static void
3090 vl_api_ip_scan_neighbor_enable_disable_t_handler
3091 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3092{
3093 int rv = 0;
3094 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3095 ip_neighbor_scan_arg_t arg;
3096
3097 arg.mode = mp->mode;
3098 arg.scan_interval = mp->scan_interval;
3099 arg.max_proc_time = mp->max_proc_time;
3100 arg.max_update = mp->max_update;
3101 arg.scan_int_delay = mp->scan_int_delay;
3102 arg.stale_threshold = mp->stale_threshold;
3103 ip_neighbor_scan_enable_disable (&arg);
3104
3105 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3106}
3107
Neale Rannsb8d44812017-11-10 06:53:54 -08003108static int
3109ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3110{
3111 vnet_main_t *vnm = vnet_get_main ();
3112 vnet_interface_main_t *im = &vnm->interface_main;
3113 ip4_main_t *im4 = &ip4_main;
3114 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003115 fib_table_t *fib_table;
3116 ip4_fib_t *fib;
3117 u32 sw_if_index;
3118 int i;
3119 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3120 u32 target_fib_id = ntohl (mp->vrf_id);
3121
Ole Troan3288ed72017-12-06 17:00:05 +01003122 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003123
3124 /* *INDENT-OFF* */
3125 pool_foreach (fib_table, im4->fibs,
3126 ({
3127 vnet_sw_interface_t * si;
3128
3129 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3130
3131 if (fib->table_id != target_fib_id)
3132 continue;
3133
3134 /* remove any mpls encap/decap labels */
3135 mpls_fib_reset_labels (fib->table_id);
3136
3137 /* remove any proxy arps in this fib */
3138 vnet_proxy_arp_fib_reset (fib->table_id);
3139
3140 /* Set the flow hash for this fib to the default */
3141 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3142
3143 vec_reset_length (sw_if_indices_to_shut);
3144
3145 /* Shut down interfaces in this FIB / clean out intfc routes */
3146 pool_foreach (si, im->sw_interfaces,
3147 ({
3148 u32 sw_if_index = si->sw_if_index;
3149
3150 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3151 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3152 fib->index))
3153 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3154 }));
3155
3156 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3157 sw_if_index = sw_if_indices_to_shut[i];
3158
3159 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3160 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3161 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3162 }
3163
3164 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3165
3166 rv = 0;
3167 break;
3168 })); /* pool_foreach (fib) */
3169 /* *INDENT-ON* */
3170
Ole Troan3288ed72017-12-06 17:00:05 +01003171 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003172 return rv;
3173}
3174
3175static int
3176ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3177{
3178 vnet_main_t *vnm = vnet_get_main ();
3179 vnet_interface_main_t *im = &vnm->interface_main;
3180 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003181 static u32 *sw_if_indices_to_shut;
3182 fib_table_t *fib_table;
3183 ip6_fib_t *fib;
3184 u32 sw_if_index;
3185 int i;
3186 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3187 u32 target_fib_id = ntohl (mp->vrf_id);
3188
Ole Troan3288ed72017-12-06 17:00:05 +01003189 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003190
3191 /* *INDENT-OFF* */
3192 pool_foreach (fib_table, im6->fibs,
3193 ({
3194 vnet_sw_interface_t * si;
3195
3196 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3197
3198 if (fib->table_id != target_fib_id)
3199 continue;
3200
3201 vec_reset_length (sw_if_indices_to_shut);
3202
3203 /* Set the flow hash for this fib to the default */
3204 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3205
3206 /* Shut down interfaces in this FIB / clean out intfc routes */
3207 pool_foreach (si, im->sw_interfaces,
3208 ({
3209 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3210 fib->index)
3211 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3212 }));
3213
3214 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3215 sw_if_index = sw_if_indices_to_shut[i];
3216
3217 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3218 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3219 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3220 }
3221
3222 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3223
3224 rv = 0;
3225 break;
3226 })); /* pool_foreach (fib) */
3227 /* *INDENT-ON* */
3228
Ole Troan3288ed72017-12-06 17:00:05 +01003229 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003230 return rv;
3231}
3232
3233static void
3234vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3235{
3236 int rv;
3237 vl_api_reset_fib_reply_t *rmp;
3238
3239 if (mp->is_ipv6)
3240 rv = ip6_reset_fib_t_handler (mp);
3241 else
3242 rv = ip4_reset_fib_t_handler (mp);
3243
3244 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3245}
3246
3247static void
3248vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3249{
3250 int rv;
3251 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3252 vnet_main_t *vnm = vnet_get_main ();
3253 clib_error_t *error;
3254
3255 vnm->api_errno = 0;
3256
3257 if (mp->is_ipv6)
3258 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3259 else
3260 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3261
3262 if (error)
3263 {
3264 clib_error_report (error);
3265 rv = VNET_API_ERROR_UNSPECIFIED;
3266 }
3267 else
3268 {
3269 rv = vnm->api_errno;
3270 }
3271
3272 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3273}
3274
Klement Sekera75e7d132017-09-20 08:26:30 +02003275void
3276vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3277{
3278 vl_api_ip_reassembly_set_reply_t *rmp;
3279 int rv = 0;
3280 if (mp->is_ip6)
3281 {
3282 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3283 clib_net_to_host_u32 (mp->max_reassemblies),
3284 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3285 }
3286 else
3287 {
3288 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3289 clib_net_to_host_u32 (mp->max_reassemblies),
3290 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3291 }
3292
3293 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3294}
3295
3296void
3297vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3298{
3299 unix_shared_memory_queue_t *q;
3300
3301 q = vl_api_client_index_to_input_queue (mp->client_index);
3302
3303 if (q == 0)
3304 return;
3305
3306 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04003307 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02003308 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3309 rmp->context = mp->context;
3310 rmp->retval = 0;
3311 if (mp->is_ip6)
3312 {
3313 rmp->is_ip6 = 1;
3314 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3315 &rmp->expire_walk_interval_ms);
3316 }
3317 else
3318 {
3319 rmp->is_ip6 = 0;
3320 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3321 &rmp->expire_walk_interval_ms);
3322 }
3323 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3324 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3325 rmp->expire_walk_interval_ms =
3326 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3327 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3328}
3329
Klement Sekera4c533132018-02-22 11:41:12 +01003330void
3331 vl_api_ip_reassembly_enable_disable_t_handler
3332 (vl_api_ip_reassembly_enable_disable_t * mp)
3333{
3334 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3335 int rv = 0;
3336 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3337 mp->enable_ip4);
3338 if (0 == rv)
3339 {
3340 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3341 mp->enable_ip6);
3342 }
3343
Choree7f61d12018-11-28 10:27:58 +03303344 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01003345}
3346
Pavel Kotucek609e1212018-11-27 09:59:44 +01003347void
3348send_ip_punt_redirect_details (vl_api_registration_t * reg,
3349 u32 context, u32 sw_if_index,
3350 ip_punt_redirect_rx_t * pr, u8 is_ipv6)
3351{
3352 vl_api_ip_punt_redirect_details_t *mp;
3353
3354 mp = vl_msg_api_alloc (sizeof (*mp));
3355 if (!mp)
3356 return;
3357
3358 clib_memset (mp, 0, sizeof (*mp));
3359 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
3360 mp->context = context;
3361 mp->punt.rx_sw_if_index = htonl (sw_if_index);
3362 mp->punt.tx_sw_if_index = htonl (pr->tx_sw_if_index);
3363 if (is_ipv6)
3364 {
3365 ip_address_encode (&pr->nh, IP46_TYPE_IP6, &mp->punt.nh);
3366 }
3367 else
3368 {
3369 ip_address_encode (&pr->nh, IP46_TYPE_IP4, &mp->punt.nh);
3370 }
3371
3372 vl_api_send_msg (reg, (u8 *) mp);
3373}
3374
3375static void
3376vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
3377{
3378 vl_api_registration_t *reg;
3379 u32 sw_if_index;
3380 int rv __attribute__ ((unused)) = 0;
3381
3382 sw_if_index = ntohl (mp->sw_if_index);
3383 reg = vl_api_client_index_to_registration (mp->client_index);
3384 if (!reg)
3385 return;
3386
3387 if (~0 != sw_if_index)
3388 VALIDATE_SW_IF_INDEX (mp);
3389
3390 ip_punt_redirect_detail_t *pr, *prs;
3391 if (mp->is_ipv6)
3392 {
3393 prs = ip6_punt_redirect_entries (sw_if_index);
3394 /* *INDENT-OFF* */
3395 vec_foreach (pr, prs)
3396 {
3397 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 1);
3398 }
3399 /* *INDENT-ON* */
3400 vec_free (prs);
3401 }
3402 else
3403 {
3404 prs = ip4_punt_redirect_entries (sw_if_index);
3405 /* *INDENT-OFF* */
3406 vec_foreach (pr, prs)
3407 {
3408 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 0);
3409 }
3410 /* *INDENT-ON* */
3411 vec_free (prs);
3412 }
3413
3414 BAD_SW_IF_INDEX_LABEL;
3415}
3416
Dave Barachb5e8a772016-12-06 12:04:42 -05003417#define vl_msg_name_crc_list
3418#include <vnet/ip/ip.api.h>
3419#undef vl_msg_name_crc_list
3420
3421static void
3422setup_message_id_table (api_main_t * am)
3423{
3424#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3425 foreach_vl_msg_name_crc_ip;
3426#undef _
3427}
3428
3429static clib_error_t *
3430ip_api_hookup (vlib_main_t * vm)
3431{
3432 api_main_t *am = &api_main;
3433
3434#define _(N,n) \
3435 vl_msg_api_set_handlers(VL_API_##N, #n, \
3436 vl_api_##n##_t_handler, \
3437 vl_noop_handler, \
3438 vl_api_##n##_t_endian, \
3439 vl_api_##n##_t_print, \
3440 sizeof(vl_api_##n##_t), 1);
3441 foreach_ip_api_msg;
3442#undef _
3443
3444 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00003445 * Mark the route add/del API as MP safe
3446 */
3447 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3448 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE_REPLY] = 1;
3449
3450 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05003451 * Set up the (msg_name, crc, message-id) table
3452 */
3453 setup_message_id_table (am);
3454
Juraj Sloboda52574522018-05-03 10:03:50 +02003455 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3456
Dave Barachb5e8a772016-12-06 12:04:42 -05003457 return 0;
3458}
3459
3460VLIB_API_INIT_FUNCTION (ip_api_hookup);
3461
3462/*
3463 * fd.io coding-style-patch-verification: ON
3464 *
3465 * Local Variables:
3466 * eval: (c-set-style "gnu")
3467 * End:
3468 */