blob: 749cb889be9696a08fa5ed6ed7ec7f6b1c26a8c0 [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>
Neale Ranns37029302018-08-10 05:30:06 -070026#include <vnet/ethernet/ethernet_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050027#include <vnet/ip/ip.h>
John Lo7f358b32018-04-28 01:19:24 -040028#include <vnet/ip/ip_neighbor.h>
Neale Ranns37029302018-08-10 05:30:06 -070029#include <vnet/ip/ip_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050030#include <vnet/ip/ip6_neighbor.h>
Pavel Kotucek609e1212018-11-27 09:59:44 +010031#include <vnet/ip/ip_punt_drop.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050032#include <vnet/fib/fib_table.h>
33#include <vnet/fib/fib_api.h>
34#include <vnet/dpo/drop_dpo.h>
35#include <vnet/dpo/receive_dpo.h>
36#include <vnet/dpo/lookup_dpo.h>
37#include <vnet/dpo/classify_dpo.h>
38#include <vnet/dpo/ip_null_dpo.h>
39#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080040#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000041#include <vnet/mfib/ip4_mfib.h>
42#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080043#include <vnet/mfib/mfib_entry.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080044#include <vnet/ip/ip_source_and_port_range_check.h>
45#include <vnet/fib/ip4_fib.h>
46#include <vnet/fib/ip6_fib.h>
47#include <vnet/ip/ip6_hop_by_hop.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020048#include <vnet/ip/ip4_reassembly.h>
49#include <vnet/ip/ip6_reassembly.h>
Neale Ranns0053de62018-05-22 08:40:52 -070050#include <vnet/ethernet/arp.h>
Matus Fabian75b9f452018-10-02 23:27:21 -070051#include <vnet/ip/ip_types_api.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050052
53#include <vnet/vnet_msg_enum.h>
54
55#define vl_typedefs /* define message structures */
56#include <vnet/vnet_all_api_h.h>
57#undef vl_typedefs
58
59#define vl_endianfun /* define message structures */
60#include <vnet/vnet_all_api_h.h>
61#undef vl_endianfun
62
63/* instantiate all the print functions we know about */
64#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
65#define vl_printfun
66#include <vnet/vnet_all_api_h.h>
67#undef vl_printfun
68
69#include <vlibapi/api_helper_macros.h>
70
Neale Ranns5a8123b2017-01-26 01:18:23 -080071
Dave Barachb5e8a772016-12-06 12:04:42 -050072#define foreach_ip_api_msg \
73_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050074_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080075_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080076_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050077_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000078_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080079_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050080_(IP_ADDRESS_DUMP, ip_address_dump) \
Neale Ranns9e2f9152018-05-18 02:27:10 -070081_(IP_UNNUMBERED_DUMP, ip_unnumbered_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050082_(IP_DUMP, ip_dump) \
83_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080084_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
John Loc7b43042018-04-13 16:46:22 -040085_(IP_PROBE_NEIGHBOR, ip_probe_neighbor) \
John Lo7f358b32018-04-28 01:19:24 -040086_(IP_SCAN_NEIGHBOR_ENABLE_DISABLE, ip_scan_neighbor_enable_disable) \
Neale Rannsb8d44812017-11-10 06:53:54 -080087_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
88_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010089_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080090_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
Neale Ranns0053de62018-05-22 08:40:52 -070091_(PROXY_ARP_DUMP, proxy_arp_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080092_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
Neale Ranns0053de62018-05-22 08:40:52 -070093 _(PROXY_ARP_INTFC_DUMP, proxy_arp_intfc_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -080094_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050095_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070096_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070097_(IP_PUNT_POLICE, ip_punt_police) \
98_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050099_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
100_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
101_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -0800102_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
103_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100104_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -0500105_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800106_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
Matus Fabian75b9f452018-10-02 23:27:21 -0700107_(IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump) \
Neale Rannsb8d44812017-11-10 06:53:54 -0800108_(IOAM_ENABLE, ioam_enable) \
109_(IOAM_DISABLE, ioam_disable) \
110_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
111 ip_source_and_port_range_check_add_del) \
112_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200113 ip_source_and_port_range_check_interface_add_del) \
Chore3460b012018-11-28 10:53:11 +0330114_(IP_SOURCE_CHECK_INTERFACE_ADD_DEL, \
115 ip_source_check_interface_add_del) \
Klement Sekera75e7d132017-09-20 08:26:30 +0200116_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100117_(IP_REASSEMBLY_GET, ip_reassembly_get) \
Pavel Kotucek609e1212018-11-27 09:59:44 +0100118_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable) \
119_(IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
120
Dave Barachb5e8a772016-12-06 12:04:42 -0500121
122extern void stats_dslock_with_hint (int hint, int tag);
123extern void stats_dsunlock (void);
124
Neale Ranns37029302018-08-10 05:30:06 -0700125static vl_api_ip_neighbor_flags_t
126ip_neighbor_flags_encode (ip_neighbor_flags_t f)
127{
128 vl_api_ip_neighbor_flags_t v = IP_API_NEIGHBOR_FLAG_NONE;
129
130 if (f & IP_NEIGHBOR_FLAG_STATIC)
131 v |= IP_API_NEIGHBOR_FLAG_STATIC;
132 if (f & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)
133 v |= IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY;
134
135 return (clib_host_to_net_u32 (v));
136}
137
Dave Barachb5e8a772016-12-06 12:04:42 -0500138static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600139send_ip_neighbor_details (u32 sw_if_index,
Neale Ranns37029302018-08-10 05:30:06 -0700140 const ip46_address_t * ip_address,
141 const mac_address_t * mac,
142 ip_neighbor_flags_t flags,
143 vl_api_registration_t * reg, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500144{
145 vl_api_ip_neighbor_details_t *mp;
146
147 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400148 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500149 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
150 mp->context = context;
Neale Ranns37029302018-08-10 05:30:06 -0700151 mp->neighbor.sw_if_index = htonl (sw_if_index);
152 mp->neighbor.flags = ip_neighbor_flags_encode (flags);
153
154 ip_address_encode (ip_address, IP46_TYPE_ANY, &mp->neighbor.ip_address);
155 mac_address_encode (mac, mp->neighbor.mac_address);
Dave Barachb5e8a772016-12-06 12:04:42 -0500156
Florin Coras6c4dae22018-01-09 06:39:23 -0800157 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500158}
159
160static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500161vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
162{
Florin Coras6c4dae22018-01-09 06:39:23 -0800163 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500164
Florin Coras6c4dae22018-01-09 06:39:23 -0800165 reg = vl_api_client_index_to_registration (mp->client_index);
166 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500167 return;
168
169 u32 sw_if_index = ntohl (mp->sw_if_index);
170
171 if (mp->is_ipv6)
172 {
173 ip6_neighbor_t *n, *ns;
174
175 ns = ip6_neighbors_entries (sw_if_index);
176 /* *INDENT-OFF* */
177 vec_foreach (n, ns)
178 {
Neale Ranns37029302018-08-10 05:30:06 -0700179 ip46_address_t nh = {
180 .ip6 = {
181 .as_u64[0] = n->key.ip6_address.as_u64[0],
182 .as_u64[1] = n->key.ip6_address.as_u64[1],
183 },
184 };
185 send_ip_neighbor_details (n->key.sw_if_index, &nh,
186 &n->mac, n->flags,
187 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500188 }
189 /* *INDENT-ON* */
190 vec_free (ns);
191 }
192 else
193 {
194 ethernet_arp_ip4_entry_t *n, *ns;
195
196 ns = ip4_neighbor_entries (sw_if_index);
197 /* *INDENT-OFF* */
198 vec_foreach (n, ns)
199 {
Neale Ranns37029302018-08-10 05:30:06 -0700200 ip46_address_t nh = {
201 .ip4 = {
202 .as_u32 = n->ip4_address.as_u32,
203 },
204 };
205
206 send_ip_neighbor_details (n->sw_if_index, &nh,
207 &n->mac, n->flags,
208 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500209 }
210 /* *INDENT-ON* */
211 vec_free (ns);
212 }
213}
214
Dave Barachb5e8a772016-12-06 12:04:42 -0500215static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500216send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800217 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700218 const fib_table_t * table,
219 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500220 fib_route_path_encode_t * api_rpaths, u32 context)
221{
222 vl_api_ip_fib_details_t *mp;
223 fib_route_path_encode_t *api_rpath;
224 vl_api_fib_path_t *fp;
225 int path_count;
226
227 path_count = vec_len (api_rpaths);
228 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
229 if (!mp)
230 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400231 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500232 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
233 mp->context = context;
234
Neale Ranns2297af02017-09-12 09:45:04 -0700235 mp->table_id = htonl (table->ft_table_id);
236 memcpy (mp->table_name, table->ft_desc,
237 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500238 mp->address_length = pfx->fp_len;
239 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
Neale Ranns008dbe12018-09-07 09:32:36 -0700240 mp->stats_index =
241 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500242
243 mp->count = htonl (path_count);
244 fp = mp->path;
245 vec_foreach (api_rpath, api_rpaths)
246 {
Neale Ranns81458422018-03-12 06:59:36 -0700247 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500248 fp++;
249 }
250
Florin Coras6c4dae22018-01-09 06:39:23 -0800251 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500252}
253
Neale Rannsa3af3372017-03-28 03:49:52 -0700254typedef struct vl_api_ip_fib_dump_walk_ctx_t_
255{
256 fib_node_index_t *feis;
257} vl_api_ip_fib_dump_walk_ctx_t;
258
Neale Ranns89541992017-04-06 04:41:02 -0700259static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700260vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
261{
262 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
263
264 vec_add1 (ctx->feis, fei);
265
Neale Ranns89541992017-04-06 04:41:02 -0700266 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700267}
268
Dave Barachb5e8a772016-12-06 12:04:42 -0500269static void
270vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
271{
272 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800273 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500274 ip4_main_t *im = &ip4_main;
275 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700276 fib_node_index_t *lfeip;
Neale Rannsc5d43172018-07-30 08:04:40 -0700277 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500278 u32 fib_index;
279 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700280 vl_api_ip_fib_dump_walk_ctx_t ctx = {
281 .feis = NULL,
282 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500283
Florin Coras6c4dae22018-01-09 06:39:23 -0800284 reg = vl_api_client_index_to_registration (mp->client_index);
285 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500286 return;
287
288 /* *INDENT-OFF* */
289 pool_foreach (fib_table, im->fibs,
290 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700291 fib_table_walk(fib_table->ft_index,
292 FIB_PROTOCOL_IP4,
293 vl_api_ip_fib_dump_walk,
294 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500295 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500296 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500297
Neale Rannsa3af3372017-03-28 03:49:52 -0700298 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500299
Neale Rannsa3af3372017-03-28 03:49:52 -0700300 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500301 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700302 pfx = fib_entry_get_prefix (*lfeip);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500303 fib_index = fib_entry_get_fib_index (*lfeip);
Neale Rannsc5d43172018-07-30 08:04:40 -0700304 fib_table = fib_table_get (fib_index, pfx->fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500305 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500306 fib_entry_encode (*lfeip, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700307 send_ip_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500308 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500309 }
310
Neale Rannsa3af3372017-03-28 03:49:52 -0700311 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500312}
313
314static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500315send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800316 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800317 const fib_table_t * table,
318 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500319 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500320{
321 vl_api_ip6_fib_details_t *mp;
322 fib_route_path_encode_t *api_rpath;
323 vl_api_fib_path_t *fp;
324 int path_count;
325
Dave Barachd7cb1b52016-12-09 09:52:16 -0500326 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500327 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
328 if (!mp)
329 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400330 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -0500331 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
332 mp->context = context;
333
Neale Rannsa161a6d2017-11-14 08:10:41 -0800334 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500335 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800337 memcpy (mp->table_name, table->ft_desc,
338 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Neale Ranns008dbe12018-09-07 09:32:36 -0700339 mp->stats_index =
340 htonl (fib_table_entry_get_stats_index (table->ft_index, pfx));
Dave Barachb5e8a772016-12-06 12:04:42 -0500341
342 mp->count = htonl (path_count);
343 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500344 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500345 {
Neale Ranns81458422018-03-12 06:59:36 -0700346 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500347 fp++;
348 }
349
Florin Coras6c4dae22018-01-09 06:39:23 -0800350 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500351}
352
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353typedef struct apt_ip6_fib_show_ctx_t_
354{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500355 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500356} api_ip6_fib_show_ctx_t;
357
Neale Ranns5c1f5592019-01-25 00:51:09 -0800358static fib_table_walk_rc_t
359api_ip6_fib_table_put_entries (fib_node_index_t fei, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500360{
361 api_ip6_fib_show_ctx_t *ctx = arg;
362
Neale Ranns5c1f5592019-01-25 00:51:09 -0800363 vec_add1 (ctx->entries, fei);
364
365 return (FIB_TABLE_WALK_CONTINUE);
Dave Barachb5e8a772016-12-06 12:04:42 -0500366}
367
368static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800369api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500370 vl_api_ip6_fib_dump_t * mp,
371 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500372{
373 vpe_api_main_t *am = &vpe_api_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500374 fib_node_index_t *fib_entry_index;
375 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700376 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500377 };
378 fib_route_path_encode_t *api_rpaths;
Neale Rannsc5d43172018-07-30 08:04:40 -0700379 const fib_prefix_t *pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -0500380
Neale Ranns5c1f5592019-01-25 00:51:09 -0800381 ip6_fib_table_walk (fib_table->ft_index,
382 api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500383
Dave Barachd7cb1b52016-12-09 09:52:16 -0500384 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500385
Dave Barachd7cb1b52016-12-09 09:52:16 -0500386 vec_foreach (fib_entry_index, ctx.entries)
387 {
Neale Rannsc5d43172018-07-30 08:04:40 -0700388 pfx = fib_entry_get_prefix (*fib_entry_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500389 api_rpaths = NULL;
390 fib_entry_encode (*fib_entry_index, &api_rpaths);
Neale Rannsc5d43172018-07-30 08:04:40 -0700391 send_ip6_fib_details (am, reg, fib_table, pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500392 vec_free (api_rpaths);
393 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500394
Dave Barachd7cb1b52016-12-09 09:52:16 -0500395 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500396}
397
398static void
399vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
400{
Florin Coras6c4dae22018-01-09 06:39:23 -0800401 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500402 ip6_main_t *im6 = &ip6_main;
403 fib_table_t *fib_table;
404
Florin Coras6c4dae22018-01-09 06:39:23 -0800405 reg = vl_api_client_index_to_registration (mp->client_index);
406 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500407 return;
408
409 /* *INDENT-OFF* */
410 pool_foreach (fib_table, im6->fibs,
411 ({
Neale Ranns81458422018-03-12 06:59:36 -0700412 /* don't send link locals */
413 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
414 continue;
415
Florin Coras6c4dae22018-01-09 06:39:23 -0800416 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500417 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500418 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500419}
420
421static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800422send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800423 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800424{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800425 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800426 vl_api_ip_mfib_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -0800427 const mfib_prefix_t *pfx;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800428 mfib_entry_t *mfib_entry;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800429 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800430 int path_count;
431
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800432 mfib_entry = mfib_entry_get (mfei);
Neale Ranns9e829a82018-12-17 05:50:32 -0800433 pfx = mfib_entry_get_prefix (mfei);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800434 mfib_entry_encode (mfei, &api_rpaths);
435
Neale Ranns5a8123b2017-01-26 01:18:23 -0800436 path_count = vec_len (api_rpaths);
437 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
438 if (!mp)
439 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400440 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700441 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800442 mp->context = context;
443
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800444 mp->rpf_id = mfib_entry->mfe_rpf_id;
445 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800446 mp->table_id = htonl (table_id);
Neale Ranns9e829a82018-12-17 05:50:32 -0800447 mp->address_length = pfx->fp_len;
448 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip4,
449 sizeof (pfx->fp_grp_addr.ip4));
450 memcpy (mp->src_address, &pfx->fp_src_addr.ip4,
451 sizeof (pfx->fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800452
453 mp->count = htonl (path_count);
454 fp = mp->path;
455 vec_foreach (api_rpath, api_rpaths)
456 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800457 fib_api_path_encode (api_rpath, &fp->path);
458 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800459 fp++;
460 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800461 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800462
Florin Coras6c4dae22018-01-09 06:39:23 -0800463 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800464}
465
466typedef struct vl_api_ip_mfib_dump_ctc_t_
467{
468 fib_node_index_t *entries;
469} vl_api_ip_mfib_dump_ctc_t;
470
471static int
472vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
473{
474 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
475
476 vec_add1 (ctx->entries, fei);
477
478 return (0);
479}
480
481static void
482vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
483{
Florin Coras6c4dae22018-01-09 06:39:23 -0800484 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800485 ip4_main_t *im = &ip4_main;
486 mfib_table_t *mfib_table;
487 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800488 vl_api_ip_mfib_dump_ctc_t ctx = {
489 .entries = NULL,
490 };
491
Florin Coras6c4dae22018-01-09 06:39:23 -0800492 reg = vl_api_client_index_to_registration (mp->client_index);
493 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800494 return;
495
Neale Ranns5a8123b2017-01-26 01:18:23 -0800496 /* *INDENT-OFF* */
497 pool_foreach (mfib_table, im->mfibs,
498 ({
499 ip4_mfib_table_walk(&mfib_table->v4,
500 vl_api_ip_mfib_table_dump_walk,
501 &ctx);
502
503 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
504
505 vec_foreach (mfeip, ctx.entries)
506 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800507 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800508 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800509 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800510 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800511 vec_reset_length (ctx.entries);
512
513 }));
514 /* *INDENT-ON* */
515
516 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800517}
518
519static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800520send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800521 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800522 u32 table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800523 const mfib_prefix_t * pfx,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800524 fib_route_path_encode_t * api_rpaths, u32 context)
525{
526 vl_api_ip6_mfib_details_t *mp;
527 fib_route_path_encode_t *api_rpath;
Neale Ranns7c03ed42018-12-27 03:21:28 -0800528 vl_api_mfib_path_t *fp;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800529 int path_count;
530
531 path_count = vec_len (api_rpaths);
532 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
533 if (!mp)
534 return;
Dave Barachb7b92992018-10-17 10:38:51 -0400535 clib_memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700536 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800537 mp->context = context;
538
539 mp->table_id = htonl (table_id);
540 mp->address_length = pfx->fp_len;
541 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
542 sizeof (pfx->fp_grp_addr.ip6));
543 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
544 sizeof (pfx->fp_src_addr.ip6));
545
546 mp->count = htonl (path_count);
547 fp = mp->path;
548 vec_foreach (api_rpath, api_rpaths)
549 {
Neale Ranns7c03ed42018-12-27 03:21:28 -0800550 fib_api_path_encode (api_rpath, &fp->path);
551 fp->itf_flags = ntohl (api_rpath->rpath.frp_mitf_flags);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800552 fp++;
553 }
554
Florin Coras6c4dae22018-01-09 06:39:23 -0800555 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800556}
557
558typedef struct vl_api_ip6_mfib_dump_ctc_t_
559{
560 fib_node_index_t *entries;
561} vl_api_ip6_mfib_dump_ctc_t;
562
563static int
564vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
565{
566 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
567
568 vec_add1 (ctx->entries, fei);
569
570 return (0);
571}
572
573static void
574vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
575{
576 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800577 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800578 ip6_main_t *im = &ip6_main;
579 mfib_table_t *mfib_table;
Neale Ranns9e829a82018-12-17 05:50:32 -0800580 const mfib_prefix_t *pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800581 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800582 fib_route_path_encode_t *api_rpaths = NULL;
583 vl_api_ip6_mfib_dump_ctc_t ctx = {
584 .entries = NULL,
585 };
586
Florin Coras6c4dae22018-01-09 06:39:23 -0800587 reg = vl_api_client_index_to_registration (mp->client_index);
588 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800589 return;
590
591
592 /* *INDENT-OFF* */
593 pool_foreach (mfib_table, im->mfibs,
594 ({
595 ip6_mfib_table_walk(&mfib_table->v6,
596 vl_api_ip6_mfib_table_dump_walk,
597 &ctx);
598
599 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
600
601 vec_foreach(mfeip, ctx.entries)
602 {
Neale Ranns9e829a82018-12-17 05:50:32 -0800603 pfx = mfib_entry_get_prefix (*mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800604 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800605 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800606 mfib_table->mft_table_id,
Neale Ranns9e829a82018-12-17 05:50:32 -0800607 pfx, api_rpaths,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800608 mp->context);
609 }
610 vec_reset_length (api_rpaths);
611 vec_reset_length (ctx.entries);
612
613 }));
614 /* *INDENT-ON* */
615
616 vec_free (ctx.entries);
617 vec_free (api_rpaths);
618}
619
620static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700621vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
622 vlib_main_t * vm)
623{
624 vl_api_ip_punt_police_reply_t *rmp;
625 int rv = 0;
626
627 if (mp->is_ip6)
628 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
629 else
630 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
631
632 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
633}
634
635static void
636vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
637 vlib_main_t * vm)
638{
639 vl_api_ip_punt_redirect_reply_t *rmp;
640 int rv = 0;
Pavel Kotucek609e1212018-11-27 09:59:44 +0100641 ip46_type_t ipv;
642 ip46_address_t nh;
Neale Rannsd91c1db2017-07-31 02:30:50 -0700643
Pavel Kotucek609e1212018-11-27 09:59:44 +0100644 if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
645 goto bad_sw_if_index;
646
647 ipv = ip_address_decode (&mp->punt.nh, &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700648 if (mp->is_add)
649 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100650 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700651 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100652 ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
653 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700654 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100655 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700656 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100657 ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
658 ntohl (mp->punt.tx_sw_if_index), &nh);
Neale Rannsd91c1db2017-07-31 02:30:50 -0700659 }
660 }
661 else
662 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100663 if (ipv == IP46_TYPE_IP6)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700664 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100665 ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700666 }
Pavel Kotucek609e1212018-11-27 09:59:44 +0100667 else if (ipv == IP46_TYPE_IP4)
Neale Rannsd91c1db2017-07-31 02:30:50 -0700668 {
Pavel Kotucek609e1212018-11-27 09:59:44 +0100669 ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
Neale Rannsd91c1db2017-07-31 02:30:50 -0700670 }
671 }
672
Pavel Kotucek609e1212018-11-27 09:59:44 +0100673 BAD_SW_IF_INDEX_LABEL;
674
Neale Rannsd91c1db2017-07-31 02:30:50 -0700675 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
676}
677
Neale Ranns37029302018-08-10 05:30:06 -0700678static ip_neighbor_flags_t
679ip_neighbor_flags_decode (vl_api_ip_neighbor_flags_t v)
680{
681 ip_neighbor_flags_t f = IP_NEIGHBOR_FLAG_NONE;
682
683 v = clib_net_to_host_u32 (v);
684
685 if (v & IP_API_NEIGHBOR_FLAG_STATIC)
686 f |= IP_NEIGHBOR_FLAG_STATIC;
687 if (v & IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY)
688 f |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
689
690 return (f);
691}
692
Neale Rannsd91c1db2017-07-31 02:30:50 -0700693static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500694vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
695 vlib_main_t * vm)
696{
697 vl_api_ip_neighbor_add_del_reply_t *rmp;
Neale Ranns0bdd3192018-09-07 11:04:52 -0700698 ip_neighbor_flags_t flags;
Neale Ranns14260392018-09-28 05:00:57 -0700699 u32 stats_index = ~0;
Neale Ranns37029302018-08-10 05:30:06 -0700700 ip46_address_t ip;
701 mac_address_t mac;
702 ip46_type_t type;
Dave Barachb5e8a772016-12-06 12:04:42 -0500703 int rv = 0;
704
Neale Ranns37029302018-08-10 05:30:06 -0700705 VALIDATE_SW_IF_INDEX ((&mp->neighbor));
Dave Barachb5e8a772016-12-06 12:04:42 -0500706
707 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
708
Neale Ranns37029302018-08-10 05:30:06 -0700709 flags = ip_neighbor_flags_decode (mp->neighbor.flags);
710 type = ip_address_decode (&mp->neighbor.ip_address, &ip);
711 mac_address_decode (mp->neighbor.mac_address, &mac);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700712
Neale Ranns37029302018-08-10 05:30:06 -0700713 /*
714 * there's no validation here of the ND/ARP entry being added.
715 * The expectation is that the FIB will ensure that nothing bad
716 * will come of adding bogus entries.
717 */
Neale Ranns0bdd3192018-09-07 11:04:52 -0700718 if (mp->is_add)
Neale Ranns37029302018-08-10 05:30:06 -0700719 rv = ip_neighbor_add (&ip, type, &mac,
720 ntohl (mp->neighbor.sw_if_index),
721 flags, &stats_index);
Neale Ranns0bdd3192018-09-07 11:04:52 -0700722 else
Neale Ranns37029302018-08-10 05:30:06 -0700723 rv = ip_neighbor_del (&ip, type, ntohl (mp->neighbor.sw_if_index));
Dave Barachb5e8a772016-12-06 12:04:42 -0500724
Dave Barachb5e8a772016-12-06 12:04:42 -0500725 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700726
727 BAD_SW_IF_INDEX_LABEL;
Neale Ranns14260392018-09-28 05:00:57 -0700728
729 /* *INDENT-OFF* */
730 REPLY_MACRO2 (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY,
731 ({
732 rmp->stats_index = htonl (stats_index);
733 }));
734 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500735}
736
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700737void
Neale Ranns15002542017-09-10 04:39:11 -0700738ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
739{
740 u32 fib_index, mfib_index;
741
742 /*
743 * ignore action on the default table - this is always present
744 * and cannot be added nor deleted from the API
745 */
746 if (0 != table_id)
747 {
748 /*
749 * The API holds only one lock on the table.
750 * i.e. it can be added many times via the API but needs to be
751 * deleted only once.
752 * The FIB index for unicast and multicast is not necessarily the
753 * same, since internal VPP systesm (like LISP and SR) create
754 * their own unicast tables.
755 */
756 fib_index = fib_table_find (fproto, table_id);
757 mfib_index = mfib_table_find (fproto, table_id);
758
759 if (~0 != fib_index)
760 {
761 fib_table_unlock (fib_index, fproto,
762 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
763 }
764 if (~0 != mfib_index)
765 {
766 mfib_table_unlock (mfib_index, fproto,
767 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
768 }
769 }
770}
771
772void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700773vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
774{
775 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700776 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
777 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700778 int rv = 0;
779
Neale Ranns15002542017-09-10 04:39:11 -0700780 if (mp->is_add)
781 {
Neale Ranns2297af02017-09-12 09:45:04 -0700782 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700783 }
784 else
785 {
786 ip_table_delete (fproto, table_id, 1);
787 }
788
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700789 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
790}
791
Dave Barachb5e8a772016-12-06 12:04:42 -0500792int
793add_del_route_t_handler (u8 is_multipath,
794 u8 is_add,
795 u8 is_drop,
796 u8 is_unreach,
797 u8 is_prohibit,
798 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800799 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500800 u8 is_classify,
801 u32 classify_table_index,
802 u8 is_resolve_host,
803 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800804 u8 is_interface_rx,
805 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800806 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700807 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800808 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500809 u32 fib_index,
810 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700811 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500812 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800813 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500814 u32 next_hop_sw_if_index,
815 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700816 u16 next_hop_weight,
817 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500818 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800819 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500820{
821 vnet_classify_main_t *cm = &vnet_classify_main;
822 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
823 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700824 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500825 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
826 .frp_sw_if_index = next_hop_sw_if_index,
827 .frp_fib_index = next_hop_fib_index,
828 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700829 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500830 .frp_label_stack = next_hop_out_label_stack,
831 };
832 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800833 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500834
Neale Rannscaac3502017-09-18 18:04:10 -0700835 /*
836 * the special INVALID label meams we are not recursing via a
837 * label. Exp-null value is never a valid via-label so that
838 * also means it's not a via-label and means clients that set
839 * it to 0 by default get the expected behaviour
840 */
841 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500842 {
Neale Rannsda78f952017-05-24 09:15:43 -0700843 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500844 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800845 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500846 }
Neale Ranns775f73c2018-12-20 03:01:49 -0800847 if (is_local)
Neale Ranns7425f922019-01-23 00:36:16 -0800848 {
849 path_flags |= FIB_ROUTE_PATH_LOCAL;
850 if (~0 != next_hop_sw_if_index)
851 {
852 entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
853 }
854 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800855 if (is_dvr)
856 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500857 if (is_resolve_host)
858 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
859 if (is_resolve_attached)
860 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800861 if (is_interface_rx)
862 path_flags |= FIB_ROUTE_PATH_INTF_RX;
863 if (is_rpf_id)
864 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700865 if (is_source_lookup)
866 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800867 if (is_multicast)
868 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800869 if (is_udp_encap)
870 {
871 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
872 path.frp_udp_encap_id = next_hop_id;
873 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800874 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
875 && path.frp_fib_index != ~0)
876 {
877 path_flags |= FIB_ROUTE_PATH_DEAG;
878 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500879
880 path.frp_flags = path_flags;
881
Dave Barachb5e8a772016-12-06 12:04:42 -0500882 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
883
Neale Ranns7425f922019-01-23 00:36:16 -0800884 if (is_drop || (is_local && (~0 == next_hop_sw_if_index)) ||
885 is_classify || is_unreach || is_prohibit)
Dave Barachb5e8a772016-12-06 12:04:42 -0500886 {
887 /*
888 * special route types that link directly to the adj
889 */
890 if (is_add)
891 {
892 dpo_id_t dpo = DPO_INVALID;
893 dpo_proto_t dproto;
894
895 dproto = fib_proto_to_dpo (prefix->fp_proto);
896
897 if (is_drop)
898 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
899 else if (is_local)
900 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
901 else if (is_unreach)
902 ip_null_dpo_add_and_lock (dproto,
903 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
904 else if (is_prohibit)
905 ip_null_dpo_add_and_lock (dproto,
906 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
907 &dpo);
908 else if (is_classify)
909 {
910 if (pool_is_free_index (cm->tables,
911 ntohl (classify_table_index)))
912 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500913 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500914 return VNET_API_ERROR_NO_SUCH_TABLE;
915 }
916
917 dpo_set (&dpo, DPO_CLASSIFY, dproto,
918 classify_dpo_create (dproto,
919 ntohl (classify_table_index)));
920 }
921 else
922 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500923 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500924 return VNET_API_ERROR_NO_SUCH_TABLE;
925 }
926
927 fib_table_entry_special_dpo_update (fib_index,
928 prefix,
929 FIB_SOURCE_API,
930 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
931 dpo_reset (&dpo);
932 }
933 else
934 {
935 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
936 }
937 }
Neale Ranns3b93be52018-09-07 01:48:54 -0700938 else if (is_multipath)
939 {
940 vec_add1 (paths, path);
941
942 if (is_add)
943 fib_table_entry_path_add2 (fib_index,
944 prefix,
945 FIB_SOURCE_API, entry_flags, paths);
946 else
947 fib_table_entry_path_remove2 (fib_index,
948 prefix, FIB_SOURCE_API, paths);
949
950 vec_free (paths);
951 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500952 else
953 {
954 if (is_add)
955 {
956 vec_add1 (paths, path);
957 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800958 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500959 vec_free (paths);
960 }
961 else
962 {
963 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
964 }
965 }
966
Dave Barachd7cb1b52016-12-09 09:52:16 -0500967 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500968 return (0);
969}
970
971int
972add_del_route_check (fib_protocol_t table_proto,
973 u32 table_id,
974 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700975 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500976 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800977 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500978{
979 vnet_main_t *vnm = vnet_get_main ();
980
981 *fib_index = fib_table_find (table_proto, ntohl (table_id));
982 if (~0 == *fib_index)
983 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800984 /* No such VRF, and we weren't asked to create one */
985 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500986 }
987
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800988 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500989 {
990 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
991 ntohl (next_hop_sw_if_index)))
992 {
993 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
994 }
995 }
996 else
997 {
Neale Rannsda78f952017-05-24 09:15:43 -0700998 fib_protocol_t fib_nh_proto;
999
1000 if (next_hop_table_proto > DPO_PROTO_MPLS)
1001 return (0);
1002
1003 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
1004
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001005 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -07001006 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001007 ntohl (next_hop_table_id));
1008 else
Neale Rannsda78f952017-05-24 09:15:43 -07001009 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001010 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -05001011
1012 if (~0 == *next_hop_fib_index)
1013 {
Neale Rannsb93078d2018-01-25 07:48:12 -08001014 /* No such VRF, and we weren't asked to create one */
1015 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -05001016 }
1017 }
1018
1019 return (0);
1020}
1021
1022static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001023ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1024 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001025{
1026 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -08001027 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001028 int rv, ii, n_labels;;
1029
1030 rv = add_del_route_check (FIB_PROTOCOL_IP4,
1031 mp->table_id,
1032 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001033 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001034 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001035 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001036
1037 if (0 != rv)
1038 return (rv);
1039
1040 fib_prefix_t pfx = {
1041 .fp_len = mp->dst_address_length,
1042 .fp_proto = FIB_PROTOCOL_IP4,
1043 };
1044 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1045
1046 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001047 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001048 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1049
1050 n_labels = mp->next_hop_n_out_labels;
1051 if (n_labels == 0)
1052 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001053 else
1054 {
1055 vec_validate (label_stack, n_labels - 1);
1056 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001057 {
1058 label_stack[ii].fml_value =
1059 ntohl (mp->next_hop_out_label_stack[ii].label);
1060 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1061 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1062 label_stack[ii].fml_mode =
1063 (mp->next_hop_out_label_stack[ii].is_uniform ?
1064 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1065 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001066 }
1067
Neale Ranns008dbe12018-09-07 09:32:36 -07001068 rv = add_del_route_t_handler (mp->is_multipath,
1069 mp->is_add,
1070 mp->is_drop,
1071 mp->is_unreach,
1072 mp->is_prohibit,
1073 mp->is_local, 0,
1074 mp->is_classify,
1075 mp->classify_table_index,
1076 mp->is_resolve_host,
1077 mp->is_resolve_attached, 0, 0,
1078 mp->is_dvr,
1079 mp->is_source_lookup,
1080 mp->is_udp_encap,
1081 fib_index, &pfx, DPO_PROTO_IP4,
1082 &nh,
1083 ntohl (mp->next_hop_id),
1084 ntohl (mp->next_hop_sw_if_index),
1085 next_hop_fib_index,
1086 mp->next_hop_weight,
1087 mp->next_hop_preference,
1088 ntohl (mp->next_hop_via_label), label_stack);
1089
1090 if (mp->is_add && 0 == rv)
1091 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1092
1093 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001094}
1095
Juraj Sloboda0012fcc2018-05-17 12:05:27 +02001096static int
Neale Ranns008dbe12018-09-07 09:32:36 -07001097ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp,
1098 u32 * stats_index)
Dave Barachb5e8a772016-12-06 12:04:42 -05001099{
Neale Ranns31ed7442018-02-23 05:29:09 -08001100 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001101 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001102 int rv, ii, n_labels;;
1103
1104 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1105 mp->table_id,
1106 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001107 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001108 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001109 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001110
1111 if (0 != rv)
1112 return (rv);
1113
1114 fib_prefix_t pfx = {
1115 .fp_len = mp->dst_address_length,
1116 .fp_proto = FIB_PROTOCOL_IP6,
1117 };
1118 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1119
1120 ip46_address_t nh;
Dave Barachb7b92992018-10-17 10:38:51 -04001121 clib_memset (&nh, 0, sizeof (nh));
Dave Barachb5e8a772016-12-06 12:04:42 -05001122 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1123
1124 n_labels = mp->next_hop_n_out_labels;
1125 if (n_labels == 0)
1126 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001127 else
1128 {
1129 vec_validate (label_stack, n_labels - 1);
1130 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001131 {
1132 label_stack[ii].fml_value =
1133 ntohl (mp->next_hop_out_label_stack[ii].label);
1134 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1135 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1136 label_stack[ii].fml_mode =
1137 (mp->next_hop_out_label_stack[ii].is_uniform ?
1138 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1139 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001140 }
1141
Neale Ranns008dbe12018-09-07 09:32:36 -07001142 rv = add_del_route_t_handler (mp->is_multipath,
1143 mp->is_add,
1144 mp->is_drop,
1145 mp->is_unreach,
1146 mp->is_prohibit,
1147 mp->is_local, 0,
1148 mp->is_classify,
1149 mp->classify_table_index,
1150 mp->is_resolve_host,
1151 mp->is_resolve_attached, 0, 0,
1152 mp->is_dvr,
1153 mp->is_source_lookup,
1154 mp->is_udp_encap,
1155 fib_index, &pfx, DPO_PROTO_IP6,
1156 &nh, ntohl (mp->next_hop_id),
1157 ntohl (mp->next_hop_sw_if_index),
1158 next_hop_fib_index,
1159 mp->next_hop_weight,
1160 mp->next_hop_preference,
1161 ntohl (mp->next_hop_via_label), label_stack);
1162
1163 if (mp->is_add && 0 == rv)
1164 *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
1165
1166 return (rv);
Dave Barachb5e8a772016-12-06 12:04:42 -05001167}
1168
1169void
1170vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1171{
1172 vl_api_ip_add_del_route_reply_t *rmp;
Neale Ranns008dbe12018-09-07 09:32:36 -07001173 u32 stats_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001174 int rv;
1175 vnet_main_t *vnm = vnet_get_main ();
1176
1177 vnm->api_errno = 0;
Neale Ranns008dbe12018-09-07 09:32:36 -07001178 stats_index = ~0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001179
1180 if (mp->is_ipv6)
Neale Ranns008dbe12018-09-07 09:32:36 -07001181 rv = ip6_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001182 else
Neale Ranns008dbe12018-09-07 09:32:36 -07001183 rv = ip4_add_del_route_t_handler (mp, &stats_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001184
1185 rv = (rv == 0) ? vnm->api_errno : rv;
1186
Neale Ranns008dbe12018-09-07 09:32:36 -07001187 /* *INDENT-OFF* */
1188 REPLY_MACRO2 (VL_API_IP_ADD_DEL_ROUTE_REPLY,
1189 ({
1190 rmp->stats_index = htonl (stats_index);
1191 }))
1192 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001193}
1194
Neale Ranns15002542017-09-10 04:39:11 -07001195void
Neale Ranns2297af02017-09-12 09:45:04 -07001196ip_table_create (fib_protocol_t fproto,
1197 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001198{
1199 u32 fib_index, mfib_index;
1200
1201 /*
1202 * ignore action on the default table - this is always present
1203 * and cannot be added nor deleted from the API
1204 */
1205 if (0 != table_id)
1206 {
1207 /*
1208 * The API holds only one lock on the table.
1209 * i.e. it can be added many times via the API but needs to be
1210 * deleted only once.
1211 * The FIB index for unicast and multicast is not necessarily the
1212 * same, since internal VPP systesm (like LISP and SR) create
1213 * their own unicast tables.
1214 */
1215 fib_index = fib_table_find (fproto, table_id);
1216 mfib_index = mfib_table_find (fproto, table_id);
1217
1218 if (~0 == fib_index)
1219 {
Neale Ranns2297af02017-09-12 09:45:04 -07001220 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1221 (is_api ?
1222 FIB_SOURCE_API :
1223 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001224 }
1225 if (~0 == mfib_index)
1226 {
Neale Ranns2297af02017-09-12 09:45:04 -07001227 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1228 (is_api ?
1229 MFIB_SOURCE_API :
1230 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001231 }
1232 }
1233}
1234
Neale Ranns32e1c012016-11-22 17:07:28 +00001235static int
1236add_del_mroute_check (fib_protocol_t table_proto,
1237 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001238 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001239{
1240 vnet_main_t *vnm = vnet_get_main ();
1241
1242 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1243 if (~0 == *fib_index)
1244 {
Neale Ranns15002542017-09-10 04:39:11 -07001245 /* No such table */
1246 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001247 }
1248
1249 if (~0 != ntohl (next_hop_sw_if_index))
1250 {
1251 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1252 ntohl (next_hop_sw_if_index)))
1253 {
1254 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1255 }
1256 }
1257
1258 return (0);
1259}
1260
Neale Ranns28c142e2018-09-07 09:37:07 -07001261static fib_node_index_t
Neale Ranns32e1c012016-11-22 17:07:28 +00001262mroute_add_del_handler (u8 is_add,
1263 u8 is_local,
1264 u32 fib_index,
1265 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001266 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001267 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001268 fib_rpf_id_t rpf_id,
Neale Rannse821ab12017-06-01 07:45:05 -07001269 u32 next_hop_sw_if_index,
1270 ip46_address_t * nh, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001271{
Neale Ranns28c142e2018-09-07 09:37:07 -07001272 fib_node_index_t mfib_entry_index = ~0;
1273
Neale Ranns32e1c012016-11-22 17:07:28 +00001274 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1275
1276 fib_route_path_t path = {
1277 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001278 .frp_proto = nh_proto,
Neale Rannse821ab12017-06-01 07:45:05 -07001279 .frp_addr = *nh,
Neale Ranns32e1c012016-11-22 17:07:28 +00001280 };
1281
1282 if (is_local)
1283 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1284
Neale Rannsd792d9c2017-10-21 10:53:20 -07001285 if (DPO_PROTO_BIER == nh_proto)
1286 {
1287 path.frp_bier_imp = bier_imp;
1288 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1289 }
1290 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001291 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001292 mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
1293 MFIB_SOURCE_API,
1294 rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001295 goto done;
1296 }
1297
1298 if (is_add)
1299 {
Neale Ranns28c142e2018-09-07 09:37:07 -07001300 mfib_entry_index = mfib_table_entry_path_update (fib_index, prefix,
1301 MFIB_SOURCE_API,
1302 &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001303 }
1304 else
1305 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001306 mfib_table_entry_path_remove (fib_index, prefix,
1307 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001308 }
1309
Neale Rannsd792d9c2017-10-21 10:53:20 -07001310done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001311 stats_dsunlock ();
Neale Ranns28c142e2018-09-07 09:37:07 -07001312 return (mfib_entry_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001313}
1314
1315static int
Neale Ranns28c142e2018-09-07 09:37:07 -07001316api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
1317 u32 * stats_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001318{
Neale Ranns28c142e2018-09-07 09:37:07 -07001319 fib_node_index_t mfib_entry_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001320 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001321 dpo_proto_t nh_proto;
Neale Rannse821ab12017-06-01 07:45:05 -07001322 ip46_address_t nh;
Neale Ranns32e1c012016-11-22 17:07:28 +00001323 u32 fib_index;
1324 int rv;
1325
Neale Rannsd792d9c2017-10-21 10:53:20 -07001326 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001327 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1328 rv = add_del_mroute_check (fproto,
1329 mp->table_id,
1330 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001331 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001332
1333 if (0 != rv)
1334 return (rv);
1335
1336 mfib_prefix_t pfx = {
1337 .fp_len = ntohs (mp->grp_address_length),
1338 .fp_proto = fproto,
1339 };
1340
1341 if (FIB_PROTOCOL_IP4 == fproto)
1342 {
1343 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1344 sizeof (pfx.fp_grp_addr.ip4));
1345 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1346 sizeof (pfx.fp_src_addr.ip4));
Dave Barachb7b92992018-10-17 10:38:51 -04001347 clib_memset (&nh.ip6, 0, sizeof (nh.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001348 clib_memcpy (&nh.ip4, mp->nh_address, sizeof (nh.ip4));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001349 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1350 pfx.fp_len = 64;
Neale Ranns32e1c012016-11-22 17:07:28 +00001351 }
1352 else
1353 {
1354 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1355 sizeof (pfx.fp_grp_addr.ip6));
1356 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1357 sizeof (pfx.fp_src_addr.ip6));
Neale Rannse821ab12017-06-01 07:45:05 -07001358 clib_memcpy (&nh.ip6, mp->nh_address, sizeof (nh.ip6));
Neale Ranns3e42ebe2018-10-04 08:36:56 -07001359 if (!ip46_address_is_zero (&pfx.fp_src_addr))
1360 pfx.fp_len = 256;
Neale Ranns32e1c012016-11-22 17:07:28 +00001361 }
1362
Neale Ranns28c142e2018-09-07 09:37:07 -07001363 mfib_entry_index = mroute_add_del_handler (mp->is_add,
1364 mp->is_local,
1365 fib_index, &pfx,
1366 nh_proto,
1367 ntohl (mp->entry_flags),
1368 ntohl (mp->rpf_id),
1369 ntohl (mp->next_hop_sw_if_index),
1370 &nh,
1371 ntohl (mp->itf_flags),
1372 ntohl (mp->bier_imp));
1373
1374 if (~0 != mfib_entry_index)
1375 *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
1376
1377 return (rv);
Neale Ranns32e1c012016-11-22 17:07:28 +00001378}
1379
1380void
1381vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1382{
1383 vl_api_ip_mroute_add_del_reply_t *rmp;
Neale Ranns28c142e2018-09-07 09:37:07 -07001384 vnet_main_t *vnm;
1385 u32 stats_index;
Neale Ranns32e1c012016-11-22 17:07:28 +00001386 int rv;
Neale Ranns32e1c012016-11-22 17:07:28 +00001387
Neale Ranns28c142e2018-09-07 09:37:07 -07001388 vnm = vnet_get_main ();
Neale Ranns32e1c012016-11-22 17:07:28 +00001389 vnm->api_errno = 0;
Neale Ranns28c142e2018-09-07 09:37:07 -07001390 stats_index = ~0;
Neale Ranns32e1c012016-11-22 17:07:28 +00001391
Neale Ranns28c142e2018-09-07 09:37:07 -07001392 rv = api_mroute_add_del_t_handler (mp, &stats_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001393
Neale Ranns28c142e2018-09-07 09:37:07 -07001394 /* *INDENT-OFF* */
1395 REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
1396 ({
1397 rmp->stats_index = htonl (stats_index);
1398 }));
1399 /* *INDENT-ON* */
Neale Ranns32e1c012016-11-22 17:07:28 +00001400}
1401
Dave Barachb5e8a772016-12-06 12:04:42 -05001402static void
1403send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001404 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1405 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001406{
1407 vl_api_ip_details_t *mp;
1408
1409 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001410 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001411 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1412
1413 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001414 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001415 mp->context = context;
1416
Florin Coras6c4dae22018-01-09 06:39:23 -08001417 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001418}
1419
1420static void
1421send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001422 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001423 u8 * ip, u16 prefix_length,
1424 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001425{
1426 vl_api_ip_address_details_t *mp;
1427
1428 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001429 clib_memset (mp, 0, sizeof (*mp));
Dave Barachb5e8a772016-12-06 12:04:42 -05001430 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1431
1432 if (is_ipv6)
1433 {
1434 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1435 }
1436 else
1437 {
1438 u32 *tp = (u32 *) mp->ip;
1439 *tp = *(u32 *) ip;
1440 }
1441 mp->prefix_length = prefix_length;
1442 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001443 mp->sw_if_index = htonl (sw_if_index);
1444 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001445
Florin Coras6c4dae22018-01-09 06:39:23 -08001446 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001447}
1448
1449static void
1450vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1451{
1452 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001453 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001454 ip6_address_t *r6;
1455 ip4_address_t *r4;
1456 ip6_main_t *im6 = &ip6_main;
1457 ip4_main_t *im4 = &ip4_main;
1458 ip_lookup_main_t *lm6 = &im6->lookup_main;
1459 ip_lookup_main_t *lm4 = &im4->lookup_main;
1460 ip_interface_address_t *ia = 0;
1461 u32 sw_if_index = ~0;
1462 int rv __attribute__ ((unused)) = 0;
1463
1464 VALIDATE_SW_IF_INDEX (mp);
1465
1466 sw_if_index = ntohl (mp->sw_if_index);
1467
Florin Coras6c4dae22018-01-09 06:39:23 -08001468 reg = vl_api_client_index_to_registration (mp->client_index);
1469 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001470 return;
1471
Dave Barachb5e8a772016-12-06 12:04:42 -05001472 if (mp->is_ipv6)
1473 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001474 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001475 /* Do not send subnet details of the IP-interface for
1476 * unnumbered interfaces. otherwise listening clients
1477 * will be confused that the subnet is applied on more
1478 * than one interface */
1479 foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001480 ({
1481 r6 = ip_interface_address_get_address (lm6, ia);
1482 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001483 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001484 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001485 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001486 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001487 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001488 else
1489 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001490 /* *INDENT-OFF* */
Neale Ranns4f2db7d2018-05-17 09:38:13 -07001491 foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001492 ({
1493 r4 = ip_interface_address_get_address (lm4, ia);
1494 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001495 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001496 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001497 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001498 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001499 }
Neale Ranns008dbe12018-09-07 09:32:36 -07001500
Dave Barachb5e8a772016-12-06 12:04:42 -05001501 BAD_SW_IF_INDEX_LABEL;
1502}
1503
1504static void
Neale Ranns9e2f9152018-05-18 02:27:10 -07001505send_ip_unnumbered_details (vpe_api_main_t * am,
1506 vl_api_registration_t * reg,
1507 u32 sw_if_index, u32 ip_sw_if_index, u32 context)
1508{
1509 vl_api_ip_unnumbered_details_t *mp;
1510
1511 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001512 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns9e2f9152018-05-18 02:27:10 -07001513 mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
1514
1515 mp->context = context;
1516 mp->sw_if_index = htonl (sw_if_index);
1517 mp->ip_sw_if_index = htonl (ip_sw_if_index);
1518
1519 vl_api_send_msg (reg, (u8 *) mp);
1520}
1521
1522static void
1523vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
1524{
1525 vnet_main_t *vnm = vnet_get_main ();
1526 vnet_interface_main_t *im = &vnm->interface_main;
1527 int rv __attribute__ ((unused)) = 0;
1528 vpe_api_main_t *am = &vpe_api_main;
1529 vl_api_registration_t *reg;
1530 vnet_sw_interface_t *si;
1531 u32 sw_if_index;
1532
1533 sw_if_index = ntohl (mp->sw_if_index);
1534
1535 reg = vl_api_client_index_to_registration (mp->client_index);
1536 if (!reg)
1537 return;
1538
1539 if (~0 != sw_if_index)
1540 {
1541 VALIDATE_SW_IF_INDEX (mp);
1542
1543 si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
1544
1545 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1546 {
1547 send_ip_unnumbered_details (am, reg,
1548 sw_if_index,
1549 si->unnumbered_sw_if_index,
1550 mp->context);
1551 }
1552 }
1553 else
1554 {
1555 /* *INDENT-OFF* */
1556 pool_foreach (si, im->sw_interfaces,
1557 ({
1558 if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1559 {
1560 send_ip_unnumbered_details(am, reg,
1561 si->sw_if_index,
1562 si->unnumbered_sw_if_index,
1563 mp->context);
1564 }
1565 }));
1566 /* *INDENT-ON* */
1567 }
1568
1569 BAD_SW_IF_INDEX_LABEL;
1570}
1571
1572static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001573vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1574{
1575 vpe_api_main_t *am = &vpe_api_main;
1576 vnet_main_t *vnm = vnet_get_main ();
1577 vlib_main_t *vm = vlib_get_main ();
1578 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001579 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001580 vnet_sw_interface_t *si, *sorted_sis;
1581 u32 sw_if_index = ~0;
1582
Florin Coras6c4dae22018-01-09 06:39:23 -08001583 reg = vl_api_client_index_to_registration (mp->client_index);
1584 if (!reg)
1585 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001586
1587 /* Gather interfaces. */
1588 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1589 _vec_len (sorted_sis) = 0;
1590 /* *INDENT-OFF* */
1591 pool_foreach (si, im->sw_interfaces,
1592 ({
1593 vec_add1 (sorted_sis, si[0]);
1594 }));
1595 /* *INDENT-ON* */
1596
1597 vec_foreach (si, sorted_sis)
1598 {
1599 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1600 {
1601 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1602 {
1603 continue;
1604 }
1605 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001606 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001607 }
1608 }
1609}
1610
1611static void
1612set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1613{
1614 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001615 int rv;
1616 u32 table_id;
1617 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001618
Neale Ranns227038a2017-04-21 01:07:59 -07001619 table_id = ntohl (mp->vrf_id);
1620
1621#define _(a,b) if (mp->a) flow_hash_config |= b;
1622 foreach_flow_hash_bit;
1623#undef _
1624
1625 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001626
1627 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1628}
1629
1630static void
1631set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1632{
1633 vl_api_set_ip_flow_hash_reply_t *rmp;
1634 int rv;
1635 u32 table_id;
1636 flow_hash_config_t flow_hash_config = 0;
1637
1638 table_id = ntohl (mp->vrf_id);
1639
1640#define _(a,b) if (mp->a) flow_hash_config |= b;
1641 foreach_flow_hash_bit;
1642#undef _
1643
1644 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1645
1646 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1647}
1648
1649
1650static void
1651vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1652{
1653 if (mp->is_ipv6 == 0)
1654 set_ip4_flow_hash (mp);
1655 else
1656 set_ip6_flow_hash (mp);
1657}
1658
1659static void
1660 vl_api_sw_interface_ip6nd_ra_config_t_handler
1661 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1662{
1663 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1664 vlib_main_t *vm = vlib_get_main ();
1665 int rv = 0;
1666 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1667 default_router;
1668
1669 is_no = mp->is_no == 1;
1670 suppress = mp->suppress == 1;
1671 managed = mp->managed == 1;
1672 other = mp->other == 1;
1673 ll_option = mp->ll_option == 1;
1674 send_unicast = mp->send_unicast == 1;
1675 cease = mp->cease == 1;
1676 default_router = mp->default_router == 1;
1677
1678 VALIDATE_SW_IF_INDEX (mp);
1679
1680 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1681 suppress, managed, other,
1682 ll_option, send_unicast, cease,
1683 default_router, ntohl (mp->lifetime),
1684 ntohl (mp->initial_count),
1685 ntohl (mp->initial_interval),
1686 ntohl (mp->max_interval),
1687 ntohl (mp->min_interval), is_no);
1688
1689 BAD_SW_IF_INDEX_LABEL;
1690
1691 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1692}
1693
1694static void
1695 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1696 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1697{
1698 vlib_main_t *vm = vlib_get_main ();
1699 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07001700 fib_prefix_t pfx;
Dave Barachb5e8a772016-12-06 12:04:42 -05001701 int rv = 0;
1702 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1703
1704 VALIDATE_SW_IF_INDEX (mp);
1705
Neale Ranns37029302018-08-10 05:30:06 -07001706 ip_prefix_decode (&mp->prefix, &pfx);
Dave Barachb5e8a772016-12-06 12:04:42 -05001707 is_no = mp->is_no == 1;
1708 use_default = mp->use_default == 1;
1709 no_advertise = mp->no_advertise == 1;
1710 off_link = mp->off_link == 1;
1711 no_autoconfig = mp->no_autoconfig == 1;
1712 no_onlink = mp->no_onlink == 1;
1713
1714 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
Neale Ranns37029302018-08-10 05:30:06 -07001715 &pfx.fp_addr.ip6,
1716 pfx.fp_len, use_default,
Dave Barachb5e8a772016-12-06 12:04:42 -05001717 ntohl (mp->val_lifetime),
1718 ntohl (mp->pref_lifetime), no_advertise,
1719 off_link, no_autoconfig, no_onlink, is_no);
1720
1721 BAD_SW_IF_INDEX_LABEL;
1722 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1723}
1724
1725static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001726send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001727 u32 context,
1728 const ip46_address_t * addr, u32 sw_if_index)
1729{
1730 vl_api_ip6nd_proxy_details_t *mp;
1731
1732 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04001733 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns3f844d02017-02-18 00:03:54 -08001734 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1735 mp->context = context;
1736 mp->sw_if_index = htonl (sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07001737
1738 ip6_address_encode (&addr->ip6, mp->ip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001739
Florin Coras6c4dae22018-01-09 06:39:23 -08001740 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001741}
1742
Neale Ranns3f844d02017-02-18 00:03:54 -08001743typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1744{
1745 u32 *indices;
1746} api_ip6nd_proxy_fib_table_walk_ctx_t;
1747
Neale Ranns89541992017-04-06 04:41:02 -07001748static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001749api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1750{
1751 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1752
1753 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1754 {
1755 vec_add1 (ctx->indices, fei);
1756 }
1757
Neale Ranns89541992017-04-06 04:41:02 -07001758 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001759}
1760
1761static void
1762vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1763{
1764 ip6_main_t *im6 = &ip6_main;
1765 fib_table_t *fib_table;
1766 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1767 .indices = NULL,
1768 };
1769 fib_node_index_t *feip;
Neale Rannsc5d43172018-07-30 08:04:40 -07001770 const fib_prefix_t *pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001771 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001772
Florin Coras6c4dae22018-01-09 06:39:23 -08001773 reg = vl_api_client_index_to_registration (mp->client_index);
1774 if (!reg)
1775 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001776
1777 /* *INDENT-OFF* */
1778 pool_foreach (fib_table, im6->fibs,
1779 ({
1780 fib_table_walk(fib_table->ft_index,
1781 FIB_PROTOCOL_IP6,
1782 api_ip6nd_proxy_fib_table_walk,
1783 &ctx);
1784 }));
1785 /* *INDENT-ON* */
1786
1787 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1788
1789 vec_foreach (feip, ctx.indices)
1790 {
Neale Rannsc5d43172018-07-30 08:04:40 -07001791 pfx = fib_entry_get_prefix (*feip);
Neale Ranns3f844d02017-02-18 00:03:54 -08001792
Florin Coras6c4dae22018-01-09 06:39:23 -08001793 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001794 mp->context,
Neale Rannsc5d43172018-07-30 08:04:40 -07001795 &pfx->fp_addr,
Neale Ranns3f844d02017-02-18 00:03:54 -08001796 fib_entry_get_resolving_interface (*feip));
1797 }
1798
1799 vec_free (ctx.indices);
1800}
1801
1802static void
1803vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1804{
1805 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07001806 ip6_address_t ip6;
Neale Ranns3f844d02017-02-18 00:03:54 -08001807 int rv = 0;
1808
1809 VALIDATE_SW_IF_INDEX (mp);
1810
Neale Ranns37029302018-08-10 05:30:06 -07001811 ip6_address_decode (mp->ip, &ip6);
1812 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index), &ip6, mp->is_del);
Neale Ranns3f844d02017-02-18 00:03:54 -08001813
1814 BAD_SW_IF_INDEX_LABEL;
1815 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1816}
1817
1818static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001819 vl_api_ip6nd_send_router_solicitation_t_handler
1820 (vl_api_ip6nd_send_router_solicitation_t * mp)
1821{
1822 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1823 icmp6_send_router_solicitation_params_t params;
1824 vlib_main_t *vm = vlib_get_main ();
1825 int rv = 0;
1826
1827 VALIDATE_SW_IF_INDEX (mp);
1828
1829 BAD_SW_IF_INDEX_LABEL;
1830 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1831
1832 if (rv != 0)
1833 return;
1834
1835 params.irt = ntohl (mp->irt);
1836 params.mrt = ntohl (mp->mrt);
1837 params.mrc = ntohl (mp->mrc);
1838 params.mrd = ntohl (mp->mrd);
1839
1840 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1841 &params);
1842}
1843
1844static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001845 vl_api_sw_interface_ip6_enable_disable_t_handler
1846 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1847{
1848 vlib_main_t *vm = vlib_get_main ();
1849 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1850 vnet_main_t *vnm = vnet_get_main ();
1851 int rv = 0;
1852 clib_error_t *error;
1853
1854 vnm->api_errno = 0;
1855
1856 VALIDATE_SW_IF_INDEX (mp);
1857
1858 error =
1859 (mp->enable == 1) ? enable_ip6_interface (vm,
1860 ntohl (mp->sw_if_index)) :
1861 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1862
1863 if (error)
1864 {
1865 clib_error_report (error);
1866 rv = VNET_API_ERROR_UNSPECIFIED;
1867 }
1868 else
1869 {
1870 rv = vnm->api_errno;
1871 }
1872
1873 BAD_SW_IF_INDEX_LABEL;
1874
1875 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1876}
1877
Neale Ranns32e1c012016-11-22 17:07:28 +00001878void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001879vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001880 u32 context, const mfib_signal_t * mfs)
1881{
1882 vl_api_mfib_signal_details_t *mp;
Neale Ranns9e829a82018-12-17 05:50:32 -08001883 const mfib_prefix_t *prefix;
Neale Ranns32e1c012016-11-22 17:07:28 +00001884 mfib_table_t *mfib;
1885 mfib_itf_t *mfi;
1886
1887 mp = vl_msg_api_alloc (sizeof (*mp));
1888
Dave Barachb7b92992018-10-17 10:38:51 -04001889 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns32e1c012016-11-22 17:07:28 +00001890 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1891 mp->context = context;
1892
1893 mfi = mfib_itf_get (mfs->mfs_itf);
Neale Ranns9e829a82018-12-17 05:50:32 -08001894 prefix = mfib_entry_get_prefix (mfs->mfs_entry);
Neale Ranns32e1c012016-11-22 17:07:28 +00001895 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
Neale Ranns9e829a82018-12-17 05:50:32 -08001896 prefix->fp_proto);
Neale Ranns32e1c012016-11-22 17:07:28 +00001897 mp->table_id = ntohl (mfib->mft_table_id);
1898 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1899
Neale Ranns9e829a82018-12-17 05:50:32 -08001900 if (FIB_PROTOCOL_IP4 == prefix->fp_proto)
Neale Ranns32e1c012016-11-22 17:07:28 +00001901 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001902 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001903
Neale Ranns9e829a82018-12-17 05:50:32 -08001904 memcpy (mp->grp_address, &prefix->fp_grp_addr.ip4, 4);
1905 if (prefix->fp_len > 32)
Neale Ranns32e1c012016-11-22 17:07:28 +00001906 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001907 memcpy (mp->src_address, &prefix->fp_src_addr.ip4, 4);
Neale Ranns32e1c012016-11-22 17:07:28 +00001908 }
1909 }
1910 else
1911 {
Neale Ranns9e829a82018-12-17 05:50:32 -08001912 mp->grp_address_len = ntohs (prefix->fp_len);
Neale Ranns32e1c012016-11-22 17:07:28 +00001913
1914 ASSERT (0);
1915 }
1916
1917 if (0 != mfs->mfs_buffer_len)
1918 {
1919 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1920
1921 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1922 }
1923 else
1924 {
1925 mp->ip_packet_len = 0;
1926 }
1927
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001928 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001929}
1930
1931static void
1932vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1933{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001934 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001935
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001936 reg = vl_api_client_index_to_registration (mp->client_index);
1937 if (!reg)
1938 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001939
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001940 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001941 ;
1942}
Dave Barachb5e8a772016-12-06 12:04:42 -05001943
Florin Coras595992c2017-11-06 17:17:08 -08001944static void
1945 vl_api_ip_container_proxy_add_del_t_handler
1946 (vl_api_ip_container_proxy_add_del_t * mp)
1947{
1948 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1949 vnet_ip_container_proxy_args_t args;
1950 int rv = 0;
1951 clib_error_t *error;
1952
Dave Barachb7b92992018-10-17 10:38:51 -04001953 clib_memset (&args, 0, sizeof (args));
Neale Ranns37029302018-08-10 05:30:06 -07001954
1955 ip_prefix_decode (&mp->pfx, &args.prefix);
1956
Florin Coras595992c2017-11-06 17:17:08 -08001957 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1958 args.is_add = mp->is_add;
1959 if ((error = vnet_ip_container_proxy_add_del (&args)))
1960 {
1961 rv = clib_error_get_code (error);
1962 clib_error_report (error);
1963 }
1964
1965 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1966}
1967
Matus Fabian75b9f452018-10-02 23:27:21 -07001968typedef struct ip_container_proxy_walk_ctx_t_
1969{
1970 vl_api_registration_t *reg;
1971 u32 context;
1972} ip_container_proxy_walk_ctx_t;
1973
1974static int
1975ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1976 void *args)
1977{
1978 vl_api_ip_container_proxy_details_t *mp;
1979 ip_container_proxy_walk_ctx_t *ctx = args;
1980
1981 mp = vl_msg_api_alloc (sizeof (*mp));
1982 if (!mp)
1983 return 1;
1984
Dave Barachb7b92992018-10-17 10:38:51 -04001985 clib_memset (mp, 0, sizeof (*mp));
Matus Fabian75b9f452018-10-02 23:27:21 -07001986 mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1987 mp->context = ctx->context;
1988
1989 mp->sw_if_index = ntohl (sw_if_index);
1990 ip_prefix_encode (pfx, &mp->prefix);
1991
1992 vl_api_send_msg (ctx->reg, (u8 *) mp);
1993
1994 return 1;
1995}
1996
1997static void
1998vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1999 mp)
2000{
2001 vl_api_registration_t *reg;
2002
2003 reg = vl_api_client_index_to_registration (mp->client_index);
2004 if (!reg)
2005 return;
2006
2007 ip_container_proxy_walk_ctx_t ctx = {
2008 .context = mp->context,
2009 .reg = reg,
2010 };
2011
2012 ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
2013}
2014
Neale Rannsb8d44812017-11-10 06:53:54 -08002015static void
2016vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
2017{
2018 int rv = 0;
2019 vl_api_ioam_enable_reply_t *rmp;
2020 clib_error_t *error;
2021
2022 /* Ignoring the profile id as currently a single profile
2023 * is supported */
2024 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
2025 mp->seqno, mp->analyse);
2026 if (error)
2027 {
2028 clib_error_report (error);
2029 rv = clib_error_get_code (error);
2030 }
2031
2032 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
2033}
2034
2035static void
2036vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
2037{
2038 int rv = 0;
2039 vl_api_ioam_disable_reply_t *rmp;
2040 clib_error_t *error;
2041
2042 error = clear_ioam_rewrite_fn ();
2043 if (error)
2044 {
2045 clib_error_report (error);
2046 rv = clib_error_get_code (error);
2047 }
2048
2049 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
2050}
2051
2052static void
2053 vl_api_ip_source_and_port_range_check_add_del_t_handler
2054 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
2055{
2056 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
2057 int rv = 0;
2058
Neale Rannsb8d44812017-11-10 06:53:54 -08002059 u8 is_add = mp->is_add;
Neale Ranns37029302018-08-10 05:30:06 -07002060 fib_prefix_t pfx;
Neale Rannsb8d44812017-11-10 06:53:54 -08002061 u16 *low_ports = 0;
2062 u16 *high_ports = 0;
2063 u32 vrf_id;
2064 u16 tmp_low, tmp_high;
2065 u8 num_ranges;
2066 int i;
2067
Neale Ranns37029302018-08-10 05:30:06 -07002068 ip_prefix_decode (&mp->prefix, &pfx);
2069
Neale Rannsb8d44812017-11-10 06:53:54 -08002070 // Validate port range
2071 num_ranges = mp->number_of_ranges;
2072 if (num_ranges > 32)
2073 { // This is size of array in VPE.API
2074 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
2075 goto reply;
2076 }
2077
2078 vec_reset_length (low_ports);
2079 vec_reset_length (high_ports);
2080
2081 for (i = 0; i < num_ranges; i++)
2082 {
2083 tmp_low = mp->low_ports[i];
2084 tmp_high = mp->high_ports[i];
2085 // If tmp_low <= tmp_high then only need to check tmp_low = 0
2086 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
2087 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
2088 {
2089 rv = VNET_API_ERROR_INVALID_VALUE;
2090 goto reply;
2091 }
2092 vec_add1 (low_ports, tmp_low);
2093 vec_add1 (high_ports, tmp_high + 1);
2094 }
2095
Neale Rannsb8d44812017-11-10 06:53:54 -08002096 vrf_id = ntohl (mp->vrf_id);
2097
2098 if (vrf_id < 1)
2099 {
2100 rv = VNET_API_ERROR_INVALID_VALUE;
2101 goto reply;
2102 }
2103
2104
Neale Ranns37029302018-08-10 05:30:06 -07002105 if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
Neale Rannsb8d44812017-11-10 06:53:54 -08002106 {
Neale Ranns37029302018-08-10 05:30:06 -07002107 rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
2108 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08002109 vrf_id,
2110 low_ports,
2111 high_ports, is_add);
2112 }
2113 else
2114 {
Neale Ranns37029302018-08-10 05:30:06 -07002115 rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
2116 pfx.fp_len,
Neale Rannsb8d44812017-11-10 06:53:54 -08002117 vrf_id,
2118 low_ports,
2119 high_ports, is_add);
2120 }
2121
2122reply:
2123 vec_free (low_ports);
2124 vec_free (high_ports);
2125 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
2126}
2127
2128static void
2129 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
2130 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
2131{
2132 vlib_main_t *vm = vlib_get_main ();
2133 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
2134 ip4_main_t *im = &ip4_main;
2135 int rv;
2136 u32 sw_if_index;
2137 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2138 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
2139 uword *p = 0;
2140 int i;
2141
2142 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
2143 ntohl (mp->tcp_out_vrf_id);
2144 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
2145 ntohl (mp->udp_out_vrf_id);
2146 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
2147 ntohl (mp->tcp_in_vrf_id);
2148 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
2149 ntohl (mp->udp_in_vrf_id);
2150
2151
2152 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
2153 {
2154 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
2155 {
2156 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
2157
2158 if (p == 0)
2159 {
2160 rv = VNET_API_ERROR_INVALID_VALUE;
2161 goto reply;
2162 }
2163
2164 fib_index[i] = p[0];
2165 }
2166 else
2167 fib_index[i] = ~0;
2168 }
2169 sw_if_index = ntohl (mp->sw_if_index);
2170
2171 VALIDATE_SW_IF_INDEX (mp);
2172
2173 rv =
2174 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2175 mp->is_add);
2176
2177 BAD_SW_IF_INDEX_LABEL;
2178reply:
2179
2180 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2181}
2182
Chore3460b012018-11-28 10:53:11 +03302183typedef union
2184{
2185 u32 fib_index;
2186} ip4_source_check_config_t;
2187
2188static void
2189 vl_api_ip_source_check_interface_add_del_t_handler
2190 (vl_api_ip_source_check_interface_add_del_t * mp)
2191{
2192 vl_api_ip_source_check_interface_add_del_reply_t *rmp;
2193 int rv;
2194 u32 sw_if_index = ntohl (mp->sw_if_index);
2195 u8 is_add = mp->is_add;
2196 char *feature_name =
2197 mp->loose ? "ip4-source-check-via-any" : "ip4-source-check-via-rx";
2198
2199 ip4_source_check_config_t config;
2200
2201 VALIDATE_SW_IF_INDEX (mp);
2202
2203 config.fib_index =
2204 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index);
2205 rv =
2206 vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index,
2207 is_add, &config, sizeof (config));
2208 BAD_SW_IF_INDEX_LABEL;
2209
2210 REPLY_MACRO (VL_API_IP_SOURCE_CHECK_INTERFACE_ADD_DEL_REPLY);
2211}
2212
Neale Rannsb8d44812017-11-10 06:53:54 -08002213#define IP4_ARP_EVENT 3
2214#define IP6_ND_EVENT 4
2215
Neale Rannsb8d44812017-11-10 06:53:54 -08002216static vlib_node_registration_t ip_resolver_process_node;
2217
Neale Ranns37029302018-08-10 05:30:06 -07002218static int
2219arp_change_delete_callback (u32 pool_index,
2220 const mac_address_t * mac,
2221 u32 sw_if_index, const ip4_address_t * address)
2222{
2223 vpe_api_main_t *am = &vpe_api_main;
2224
2225 if (pool_is_free_index (am->arp_events, pool_index))
2226 return 1;
2227
2228 pool_put_index (am->arp_events, pool_index);
2229 return 0;
2230}
2231
Neale Rannsb8d44812017-11-10 06:53:54 -08002232static void
2233handle_ip4_arp_event (u32 pool_index)
2234{
2235 vpe_api_main_t *vam = &vpe_api_main;
2236 vnet_main_t *vnm = vam->vnet_main;
2237 vlib_main_t *vm = vam->vlib_main;
2238 vl_api_ip4_arp_event_t *event;
2239 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002240 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002241
2242 /* Client can cancel, die, etc. */
2243 if (pool_is_free_index (vam->arp_events, pool_index))
2244 return;
2245
2246 event = pool_elt_at_index (vam->arp_events, pool_index);
2247
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002248 reg = vl_api_client_index_to_registration (event->client_index);
2249 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002250 {
2251 (void) vnet_add_del_ip4_arp_change_event
2252 (vnm, arp_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002253 event->pid, event->ip,
Neale Rannsb8d44812017-11-10 06:53:54 -08002254 ip_resolver_process_node.index, IP4_ARP_EVENT,
2255 ~0 /* pool index, notused */ , 0 /* is_add */ );
2256 return;
2257 }
2258
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002259 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002260 {
2261 mp = vl_msg_api_alloc (sizeof (*mp));
2262 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002263 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002264 }
2265 else
2266 {
2267 static f64 last_time;
2268 /*
2269 * Throttle syslog msgs.
2270 * It's pretty tempting to just revoke the registration...
2271 */
2272 if (vlib_time_now (vm) > last_time + 10.0)
2273 {
2274 clib_warning ("arp event for %U to pid %d: queue stuffed!",
Neale Ranns37029302018-08-10 05:30:06 -07002275 format_ip4_address, event->ip, event->pid);
Neale Rannsb8d44812017-11-10 06:53:54 -08002276 last_time = vlib_time_now (vm);
2277 }
2278 }
2279}
2280
Neale Ranns37029302018-08-10 05:30:06 -07002281static int
2282nd_change_delete_callback (u32 pool_index,
2283 const mac_address_t * mac,
2284 u32 sw_if_index, const ip6_address_t * addr)
2285{
2286 vpe_api_main_t *am = &vpe_api_main;
2287
2288 if (pool_is_free_index (am->nd_events, pool_index))
2289 return 1;
2290
2291 pool_put_index (am->nd_events, pool_index);
2292 return 0;
2293}
2294
Neale Rannsf12dad62018-06-04 18:41:24 -07002295static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002296handle_ip6_nd_event (u32 pool_index)
2297{
2298 vpe_api_main_t *vam = &vpe_api_main;
2299 vnet_main_t *vnm = vam->vnet_main;
2300 vlib_main_t *vm = vam->vlib_main;
2301 vl_api_ip6_nd_event_t *event;
2302 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002303 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002304
2305 /* Client can cancel, die, etc. */
2306 if (pool_is_free_index (vam->nd_events, pool_index))
2307 return;
2308
2309 event = pool_elt_at_index (vam->nd_events, pool_index);
2310
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002311 reg = vl_api_client_index_to_registration (event->client_index);
2312 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002313 {
2314 (void) vnet_add_del_ip6_nd_change_event
2315 (vnm, nd_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002316 event->pid, event->ip,
Neale Rannsb8d44812017-11-10 06:53:54 -08002317 ip_resolver_process_node.index, IP6_ND_EVENT,
2318 ~0 /* pool index, notused */ , 0 /* is_add */ );
2319 return;
2320 }
2321
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002322 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002323 {
2324 mp = vl_msg_api_alloc (sizeof (*mp));
2325 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002326 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002327 }
2328 else
2329 {
2330 static f64 last_time;
2331 /*
2332 * Throttle syslog msgs.
2333 * It's pretty tempting to just revoke the registration...
2334 */
2335 if (vlib_time_now (vm) > last_time + 10.0)
2336 {
2337 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
Neale Ranns37029302018-08-10 05:30:06 -07002338 format_ip6_address, event->ip, event->pid);
Neale Rannsb8d44812017-11-10 06:53:54 -08002339 last_time = vlib_time_now (vm);
2340 }
2341 }
2342}
2343
2344static uword
2345resolver_process (vlib_main_t * vm,
2346 vlib_node_runtime_t * rt, vlib_frame_t * f)
2347{
2348 volatile f64 timeout = 100.0;
2349 volatile uword *event_data = 0;
2350
2351 while (1)
2352 {
2353 vlib_process_wait_for_event_or_clock (vm, timeout);
2354
2355 uword event_type =
2356 vlib_process_get_events (vm, (uword **) & event_data);
2357
2358 int i;
2359 switch (event_type)
2360 {
2361 case IP4_ARP_EVENT:
2362 for (i = 0; i < vec_len (event_data); i++)
2363 handle_ip4_arp_event (event_data[i]);
2364 break;
2365
2366 case IP6_ND_EVENT:
2367 for (i = 0; i < vec_len (event_data); i++)
2368 handle_ip6_nd_event (event_data[i]);
2369 break;
2370
2371 case ~0: /* timeout */
2372 break;
2373 }
2374
2375 vec_reset_length (event_data);
2376 }
2377 return 0; /* or not */
2378}
2379
2380/* *INDENT-OFF* */
2381VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2382 .function = resolver_process,
2383 .type = VLIB_NODE_TYPE_PROCESS,
2384 .name = "ip-route-resolver-process",
2385};
2386/* *INDENT-ON* */
2387
2388static int
Neale Ranns37029302018-08-10 05:30:06 -07002389nd_change_data_callback (u32 pool_index, const mac_address_t * new_mac,
2390 u32 sw_if_index, const ip6_address_t * address)
Neale Rannsb8d44812017-11-10 06:53:54 -08002391{
2392 vpe_api_main_t *am = &vpe_api_main;
2393 vl_api_ip6_nd_event_t *event;
2394
2395 if (pool_is_free_index (am->nd_events, pool_index))
2396 return 1;
2397
2398 event = pool_elt_at_index (am->nd_events, pool_index);
Neale Ranns37029302018-08-10 05:30:06 -07002399 if (ethernet_mac_address_equal (event->mac, new_mac->bytes) &&
Neale Rannsb8d44812017-11-10 06:53:54 -08002400 sw_if_index == ntohl (event->sw_if_index))
2401 {
2402 return 1;
2403 }
2404
Neale Ranns37029302018-08-10 05:30:06 -07002405 mac_address_encode (new_mac, event->mac);
Neale Rannsb8d44812017-11-10 06:53:54 -08002406 event->sw_if_index = htonl (sw_if_index);
2407 return 0;
2408}
2409
Neale Rannsb8d44812017-11-10 06:53:54 -08002410static vlib_node_registration_t wc_arp_process_node;
2411
2412enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002413{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002414
2415static uword
2416wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2417{
2418 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2419 * and need to be volatile - to prevent them from being optimized into
2420 * a register - which could change during suspension */
2421
2422 volatile wc_arp_report_t arp_prev = { 0 };
2423 volatile wc_nd_report_t nd_prev = { 0 };
2424 volatile f64 last_arp = vlib_time_now (vm);
2425 volatile f64 last_nd = vlib_time_now (vm);
2426
2427 while (1)
2428 {
2429 vlib_process_wait_for_event (vm);
2430 uword event_type = WC_ARP_REPORT;
2431 void *event_data = vlib_process_get_event_data (vm, &event_type);
2432
2433 f64 now = vlib_time_now (vm);
2434 int i;
2435 if (event_type == WC_ARP_REPORT)
2436 {
2437 wc_arp_report_t *arp_events = event_data;
2438 for (i = 0; i < vec_len (arp_events); i++)
2439 {
Neale Ranns37029302018-08-10 05:30:06 -07002440 /* discard dup event - cast away volatile */
2441 if (arp_prev.ip.as_u32 == arp_events[i].ip.as_u32 &&
2442 mac_address_equal ((const mac_address_t *) &arp_prev.mac,
2443 &arp_events[i].mac) &&
Neale Rannsb8d44812017-11-10 06:53:54 -08002444 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2445 (now - last_arp) < 10.0)
2446 {
2447 continue;
2448 }
2449 arp_prev = arp_events[i];
2450 last_arp = now;
2451 vpe_client_registration_t *reg;
2452 /* *INDENT-OFF* */
2453 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2454 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002455 vl_api_registration_t *vl_reg;
2456 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002457 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002458 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002459 {
2460 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002461 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002462 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2463 event->client_index = reg->client_index;
2464 event->pid = reg->client_pid;
2465 event->mac_ip = 1;
Neale Ranns37029302018-08-10 05:30:06 -07002466 ip4_address_encode(&arp_events[i].ip, event->ip);
Neale Rannsb8d44812017-11-10 06:53:54 -08002467 event->sw_if_index = htonl(arp_events[i].sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07002468 mac_address_encode(&arp_events[i].mac, event->mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002469 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002470 }
2471 }));
2472 /* *INDENT-ON* */
2473 }
2474 }
2475 else if (event_type == WC_ND_REPORT)
2476 {
2477 wc_nd_report_t *nd_events = event_data;
2478 for (i = 0; i < vec_len (nd_events); i++)
2479 {
Neale Ranns37029302018-08-10 05:30:06 -07002480 /* discard dup event - cast away volatile */
2481 if (ip6_address_is_equal ((const ip6_address_t *) &nd_prev.ip6,
2482 &nd_events[i].ip6)
2483 && mac_address_equal ((const mac_address_t *) &nd_prev.mac,
2484 &nd_events[i].mac)
Neale Rannsb8d44812017-11-10 06:53:54 -08002485 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2486 && (now - last_nd) < 10.0)
2487 {
2488 continue;
2489 }
2490 nd_prev = nd_events[i];
2491 last_nd = now;
2492 vpe_client_registration_t *reg;
2493 /* *INDENT-OFF* */
2494 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2495 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002496 vl_api_registration_t *vl_reg;
2497 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2498 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002499 {
2500 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
Dave Barachb7b92992018-10-17 10:38:51 -04002501 clib_memset (event, 0, sizeof *event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002502 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2503 event->client_index = reg->client_index;
2504 event->pid = reg->client_pid;
2505 event->mac_ip = 1;
Neale Ranns37029302018-08-10 05:30:06 -07002506 ip6_address_encode(&nd_events[i].ip6, event->ip);
Neale Rannsb8d44812017-11-10 06:53:54 -08002507 event->sw_if_index = htonl(nd_events[i].sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07002508 mac_address_encode(&nd_events[i].mac, event->mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002509 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002510 }
2511 }));
2512 /* *INDENT-ON* */
2513 }
2514 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002515 else if (event_type == RA_REPORT)
2516 {
2517 ra_report_t *ra_events = event_data;
2518 for (i = 0; i < vec_len (ra_events); i++)
2519 {
Juraj Sloboda52574522018-05-03 10:03:50 +02002520 ip6_neighbor_public_main_t *npm = &ip6_neighbor_public_main;
2521 call_ip6_neighbor_callbacks (&ra_events[i],
2522 npm->ra_report_functions);
2523
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002524 vpe_client_registration_t *reg;
2525 /* *INDENT-OFF* */
2526 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2527 ({
2528 vl_api_registration_t *vl_reg;
2529 vl_reg =
2530 vl_api_client_index_to_registration (reg->client_index);
2531 if (vl_reg && vl_api_can_send_msg (vl_reg))
2532 {
2533 u32 event_size =
2534 sizeof (vl_api_ip6_ra_event_t) +
2535 vec_len (ra_events[i].prefixes) *
2536 sizeof (vl_api_ip6_ra_prefix_info_t);
2537 vl_api_ip6_ra_event_t *event =
2538 vl_msg_api_alloc (event_size);
Dave Barachb7b92992018-10-17 10:38:51 -04002539 clib_memset (event, 0, event_size);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002540 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2541 event->client_index = reg->client_index;
2542 event->pid = reg->client_pid;
2543
2544 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2545
Neale Ranns37029302018-08-10 05:30:06 -07002546 ip6_address_encode (&ra_events[i].router_address,
2547 event->router_addr);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002548
2549 event->current_hop_limit = ra_events[i].current_hop_limit;
2550 event->flags = ra_events[i].flags;
2551 event->router_lifetime_in_sec =
2552 clib_host_to_net_u16 (ra_events
2553 [i].router_lifetime_in_sec);
2554 event->neighbor_reachable_time_in_msec =
2555 clib_host_to_net_u32 (ra_events
2556 [i].neighbor_reachable_time_in_msec);
2557 event->time_in_msec_between_retransmitted_neighbor_solicitations
2558 =
2559 clib_host_to_net_u32 (ra_events
2560 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2561
2562 event->n_prefixes =
2563 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2564 vl_api_ip6_ra_prefix_info_t *prefix =
2565 (typeof (prefix)) event->prefixes;
2566 u32 j;
2567 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2568 {
2569 ra_report_prefix_info_t *info =
2570 &ra_events[i].prefixes[j];
Neale Ranns37029302018-08-10 05:30:06 -07002571 ip_prefix_encode(&info->prefix, &prefix->prefix);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002572 prefix->flags = info->flags;
2573 prefix->valid_time =
2574 clib_host_to_net_u32 (info->valid_time);
2575 prefix->preferred_time =
2576 clib_host_to_net_u32 (info->preferred_time);
2577 prefix++;
2578 }
2579
2580 vl_api_send_msg (vl_reg, (u8 *) event);
2581 }
2582 }));
Juraj Slobodad7f58cc2018-07-27 11:23:14 +02002583 /* *INDENT-ON* */
2584 vec_free (ra_events[i].prefixes);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002585 }
2586 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002587 vlib_process_put_event_data (vm, event_data);
2588 }
2589
2590 return 0;
2591}
2592
2593/* *INDENT-OFF* */
2594VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2595 .function = wc_arp_process,
2596 .type = VLIB_NODE_TYPE_PROCESS,
2597 .name = "wildcard-ip4-arp-publisher-process",
2598};
2599/* *INDENT-ON* */
2600
2601static int
Neale Ranns37029302018-08-10 05:30:06 -07002602arp_change_data_callback (u32 pool_index,
2603 const mac_address_t * mac,
2604 u32 sw_if_index, const ip4_address_t * address)
Neale Rannsb8d44812017-11-10 06:53:54 -08002605{
2606 vpe_api_main_t *am = &vpe_api_main;
2607 vl_api_ip4_arp_event_t *event;
2608
2609 if (pool_is_free_index (am->arp_events, pool_index))
2610 return 1;
2611
2612 event = pool_elt_at_index (am->arp_events, pool_index);
Neale Ranns37029302018-08-10 05:30:06 -07002613 if (ethernet_mac_address_equal (event->mac, mac->bytes) &&
Neale Rannsb8d44812017-11-10 06:53:54 -08002614 sw_if_index == ntohl (event->sw_if_index))
2615 {
2616 return 1;
2617 }
2618
Neale Ranns37029302018-08-10 05:30:06 -07002619 mac_address_encode (mac, event->mac);
Neale Rannsb8d44812017-11-10 06:53:54 -08002620 event->sw_if_index = htonl (sw_if_index);
2621 return 0;
2622}
2623
2624static void
2625vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2626{
2627 vpe_api_main_t *am = &vpe_api_main;
2628 vnet_main_t *vnm = vnet_get_main ();
2629 vl_api_want_ip4_arp_events_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07002630 ip4_address_t ip;
Neale Rannsb8d44812017-11-10 06:53:54 -08002631 int rv = 0;
2632
Neale Ranns37029302018-08-10 05:30:06 -07002633 ip4_address_decode (mp->ip, &ip);
2634
2635 if (ip.as_u32 == 0)
Neale Rannsb8d44812017-11-10 06:53:54 -08002636 {
2637 uword *p =
2638 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2639 vpe_client_registration_t *rp;
2640 if (p)
2641 {
2642 if (mp->enable_disable)
2643 {
2644 clib_warning ("pid %d: already enabled...", mp->pid);
2645 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2646 goto reply;
2647 }
2648 else
2649 {
2650 rp =
2651 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2652 pool_put (am->wc_ip4_arp_events_registrations, rp);
2653 hash_unset (am->wc_ip4_arp_events_registration_hash,
2654 mp->client_index);
2655 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002656 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002657 goto reply;
2658 }
2659 }
2660 if (mp->enable_disable == 0)
2661 {
2662 clib_warning ("pid %d: already disabled...", mp->pid);
2663 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2664 goto reply;
2665 }
2666 pool_get (am->wc_ip4_arp_events_registrations, rp);
2667 rp->client_index = mp->client_index;
2668 rp->client_pid = mp->pid;
2669 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2670 rp - am->wc_ip4_arp_events_registrations);
2671 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2672 goto reply;
2673 }
2674
2675 if (mp->enable_disable)
2676 {
2677 vl_api_ip4_arp_event_t *event;
2678 pool_get (am->arp_events, event);
2679 rv = vnet_add_del_ip4_arp_change_event
2680 (vnm, arp_change_data_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002681 mp->pid, mp->ip /* addr, in net byte order */ ,
Neale Rannsb8d44812017-11-10 06:53:54 -08002682 ip_resolver_process_node.index,
2683 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2684
2685 if (rv)
2686 {
2687 pool_put (am->arp_events, event);
2688 goto reply;
2689 }
Dave Barachb7b92992018-10-17 10:38:51 -04002690 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002691
2692 /* Python API expects events to have no context */
2693 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2694 event->client_index = mp->client_index;
Neale Ranns37029302018-08-10 05:30:06 -07002695 memcpy (event->ip, mp->ip, 4);
Neale Rannsb8d44812017-11-10 06:53:54 -08002696 event->pid = mp->pid;
Neale Ranns37029302018-08-10 05:30:06 -07002697 if (ip.as_u32 == 0)
Neale Rannsb8d44812017-11-10 06:53:54 -08002698 event->mac_ip = 1;
2699 }
2700 else
2701 {
2702 rv = vnet_add_del_ip4_arp_change_event
2703 (vnm, arp_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002704 mp->pid, mp->ip /* addr, in net byte order */ ,
Neale Rannsb8d44812017-11-10 06:53:54 -08002705 ip_resolver_process_node.index,
2706 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2707 }
2708reply:
2709 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2710}
2711
Neale Rannsf12dad62018-06-04 18:41:24 -07002712static clib_error_t *
2713want_ip4_arp_events_reaper (u32 client_index)
2714{
2715 vpe_client_registration_t *rp;
2716 vl_api_ip4_arp_event_t *event;
2717 u32 *to_delete, *event_id;
2718 vpe_api_main_t *am;
2719 vnet_main_t *vnm;
2720 uword *p;
2721
2722 am = &vpe_api_main;
2723 vnm = vnet_get_main ();
2724 to_delete = NULL;
2725
2726 /* clear out all of its pending resolutions */
2727 /* *INDENT-OFF* */
2728 pool_foreach(event, am->arp_events,
2729 ({
2730 if (event->client_index == client_index)
2731 {
2732 vec_add1(to_delete, event - am->arp_events);
2733 }
2734 }));
2735 /* *INDENT-ON* */
2736
2737 vec_foreach (event_id, to_delete)
2738 {
2739 event = pool_elt_at_index (am->arp_events, *event_id);
2740 vnet_add_del_ip4_arp_change_event
2741 (vnm, arp_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002742 event->pid, event->ip,
Neale Rannsf12dad62018-06-04 18:41:24 -07002743 ip_resolver_process_node.index, IP4_ARP_EVENT,
2744 ~0 /* pool index, notused */ , 0 /* is_add */ );
2745 }
2746 vec_free (to_delete);
2747
2748 /* remove from the registration hash */
2749 p = hash_get (am->wc_ip4_arp_events_registration_hash, client_index);
2750
2751 if (p)
2752 {
2753 rp = pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2754 pool_put (am->wc_ip4_arp_events_registrations, rp);
2755 hash_unset (am->wc_ip4_arp_events_registration_hash, client_index);
2756 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
2757 wc_arp_set_publisher_node (~0, REPORT_MAX);
2758 }
2759 return (NULL);
2760}
2761
2762VL_MSG_API_REAPER_FUNCTION (want_ip4_arp_events_reaper);
2763
Neale Rannsb8d44812017-11-10 06:53:54 -08002764static void
2765vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2766{
2767 vpe_api_main_t *am = &vpe_api_main;
2768 vnet_main_t *vnm = vnet_get_main ();
2769 vl_api_want_ip6_nd_events_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07002770 ip6_address_t ip6;
Neale Rannsb8d44812017-11-10 06:53:54 -08002771 int rv = 0;
2772
Neale Ranns37029302018-08-10 05:30:06 -07002773 ip6_address_decode (mp->ip, &ip6);
2774
2775 if (ip6_address_is_zero (&ip6))
Neale Rannsb8d44812017-11-10 06:53:54 -08002776 {
2777 uword *p =
2778 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2779 vpe_client_registration_t *rp;
2780 if (p)
2781 {
2782 if (mp->enable_disable)
2783 {
2784 clib_warning ("pid %d: already enabled...", mp->pid);
2785 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2786 goto reply;
2787 }
2788 else
2789 {
2790 rp =
2791 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2792 pool_put (am->wc_ip6_nd_events_registrations, rp);
2793 hash_unset (am->wc_ip6_nd_events_registration_hash,
2794 mp->client_index);
2795 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002796 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002797 goto reply;
2798 }
2799 }
2800 if (mp->enable_disable == 0)
2801 {
2802 clib_warning ("pid %d: already disabled...", mp->pid);
2803 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2804 goto reply;
2805 }
2806 pool_get (am->wc_ip6_nd_events_registrations, rp);
2807 rp->client_index = mp->client_index;
2808 rp->client_pid = mp->pid;
2809 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2810 rp - am->wc_ip6_nd_events_registrations);
2811 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2812 goto reply;
2813 }
2814
2815 if (mp->enable_disable)
2816 {
2817 vl_api_ip6_nd_event_t *event;
2818 pool_get (am->nd_events, event);
2819
2820 rv = vnet_add_del_ip6_nd_change_event
2821 (vnm, nd_change_data_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002822 mp->pid, &ip6,
Neale Rannsb8d44812017-11-10 06:53:54 -08002823 ip_resolver_process_node.index,
2824 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2825
2826 if (rv)
2827 {
2828 pool_put (am->nd_events, event);
2829 goto reply;
2830 }
Dave Barachb7b92992018-10-17 10:38:51 -04002831 clib_memset (event, 0, sizeof (*event));
Neale Rannsb8d44812017-11-10 06:53:54 -08002832
2833 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2834 event->client_index = mp->client_index;
Neale Ranns37029302018-08-10 05:30:06 -07002835 ip6_address_encode (&ip6, event->ip);
Neale Rannsb8d44812017-11-10 06:53:54 -08002836 event->pid = mp->pid;
2837 }
2838 else
2839 {
2840 rv = vnet_add_del_ip6_nd_change_event
2841 (vnm, nd_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002842 mp->pid, &ip6 /* addr, in net byte order */ ,
Neale Rannsb8d44812017-11-10 06:53:54 -08002843 ip_resolver_process_node.index,
2844 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2845 }
2846reply:
2847 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2848}
2849
Neale Rannsf12dad62018-06-04 18:41:24 -07002850static clib_error_t *
2851want_ip6_nd_events_reaper (u32 client_index)
2852{
2853
2854 vpe_client_registration_t *rp;
2855 vl_api_ip6_nd_event_t *event;
2856 u32 *to_delete, *event_id;
2857 vpe_api_main_t *am;
2858 vnet_main_t *vnm;
2859 uword *p;
2860
2861 am = &vpe_api_main;
2862 vnm = vnet_get_main ();
2863 to_delete = NULL;
2864
2865 /* clear out all of its pending resolutions */
2866 /* *INDENT-OFF* */
2867 pool_foreach(event, am->nd_events,
2868 ({
2869 if (event->client_index == client_index)
2870 {
2871 vec_add1(to_delete, event - am->nd_events);
2872 }
2873 }));
2874 /* *INDENT-ON* */
2875
2876 vec_foreach (event_id, to_delete)
2877 {
2878 event = pool_elt_at_index (am->nd_events, *event_id);
2879 vnet_add_del_ip6_nd_change_event
2880 (vnm, nd_change_delete_callback,
Neale Ranns37029302018-08-10 05:30:06 -07002881 event->pid, event->ip,
Neale Rannsf12dad62018-06-04 18:41:24 -07002882 ip_resolver_process_node.index, IP6_ND_EVENT,
2883 ~0 /* pool index, notused */ , 0 /* is_add */ );
2884 }
2885 vec_free (to_delete);
2886
2887 /* remove from the registration hash */
2888 p = hash_get (am->wc_ip6_nd_events_registration_hash, client_index);
2889
2890 if (p)
2891 {
2892 rp = pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2893 pool_put (am->wc_ip6_nd_events_registrations, rp);
2894 hash_unset (am->wc_ip6_nd_events_registration_hash, client_index);
2895 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
2896 wc_nd_set_publisher_node (~0, REPORT_MAX);
2897 }
2898 return (NULL);
2899}
2900
2901VL_MSG_API_REAPER_FUNCTION (want_ip6_nd_events_reaper);
2902
Neale Rannsb8d44812017-11-10 06:53:54 -08002903static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002904vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2905{
2906 vpe_api_main_t *am = &vpe_api_main;
2907 vl_api_want_ip6_ra_events_reply_t *rmp;
2908 int rv = 0;
2909
2910 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2911 vpe_client_registration_t *rp;
2912 if (p)
2913 {
2914 if (mp->enable_disable)
2915 {
2916 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2917 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2918 goto reply;
2919 }
2920 else
2921 {
2922 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2923 pool_put (am->ip6_ra_events_registrations, rp);
2924 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002925 goto reply;
2926 }
2927 }
2928 if (mp->enable_disable == 0)
2929 {
2930 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2931 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2932 goto reply;
2933 }
2934 pool_get (am->ip6_ra_events_registrations, rp);
2935 rp->client_index = mp->client_index;
2936 rp->client_pid = ntohl (mp->pid);
2937 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2938 rp - am->ip6_ra_events_registrations);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002939
2940reply:
2941 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2942}
2943
Neale Rannsf12dad62018-06-04 18:41:24 -07002944static clib_error_t *
2945want_ip6_ra_events_reaper (u32 client_index)
2946{
2947 vpe_api_main_t *am = &vpe_api_main;
2948 vpe_client_registration_t *rp;
2949 uword *p;
2950
2951 p = hash_get (am->ip6_ra_events_registration_hash, client_index);
2952
2953 if (p)
2954 {
2955 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2956 pool_put (am->ip6_ra_events_registrations, rp);
2957 hash_unset (am->ip6_ra_events_registration_hash, client_index);
2958 }
2959 return (NULL);
2960}
2961
2962VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
2963
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002964static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002965vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2966{
2967 vl_api_proxy_arp_add_del_reply_t *rmp;
Neale Ranns37029302018-08-10 05:30:06 -07002968 ip4_address_t lo, hi;
Neale Rannsb8d44812017-11-10 06:53:54 -08002969 u32 fib_index;
2970 int rv;
Neale Rannsb8d44812017-11-10 06:53:54 -08002971
Ole Troan3288ed72017-12-06 17:00:05 +01002972 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002973
Neale Ranns37029302018-08-10 05:30:06 -07002974 fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->proxy.table_id));
Neale Rannsb8d44812017-11-10 06:53:54 -08002975
Neale Ranns37029302018-08-10 05:30:06 -07002976 if (~0 == fib_index)
Neale Rannsb8d44812017-11-10 06:53:54 -08002977 {
2978 rv = VNET_API_ERROR_NO_SUCH_FIB;
2979 goto out;
2980 }
2981
Neale Ranns37029302018-08-10 05:30:06 -07002982 ip4_address_decode (mp->proxy.low, &lo);
2983 ip4_address_decode (mp->proxy.hi, &hi);
Neale Rannsb8d44812017-11-10 06:53:54 -08002984
Neale Ranns37029302018-08-10 05:30:06 -07002985 rv = vnet_proxy_arp_add_del (&lo, &hi, fib_index, mp->is_add == 0);
Neale Rannsb8d44812017-11-10 06:53:54 -08002986
2987out:
Ole Troan3288ed72017-12-06 17:00:05 +01002988 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002989 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2990}
2991
Neale Ranns0053de62018-05-22 08:40:52 -07002992typedef struct proxy_arp_walk_ctx_t_
2993{
2994 vl_api_registration_t *reg;
2995 u32 context;
2996} proxy_arp_walk_ctx_t;
2997
2998static walk_rc_t
2999send_proxy_arp_details (const ip4_address_t * lo_addr,
3000 const ip4_address_t * hi_addr,
3001 u32 fib_index, void *data)
3002{
3003 vl_api_proxy_arp_details_t *mp;
3004 proxy_arp_walk_ctx_t *ctx;
3005
3006 ctx = data;
3007
3008 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003009 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003010 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_DETAILS);
3011 mp->context = ctx->context;
Neale Ranns37029302018-08-10 05:30:06 -07003012 mp->proxy.table_id = htonl (fib_index);
3013
3014 ip4_address_encode (lo_addr, mp->proxy.low);
3015 ip4_address_encode (hi_addr, mp->proxy.hi);
Neale Ranns0053de62018-05-22 08:40:52 -07003016
3017 vl_api_send_msg (ctx->reg, (u8 *) mp);
3018
3019 return (WALK_CONTINUE);
3020}
3021
3022static void
3023vl_api_proxy_arp_dump_t_handler (vl_api_proxy_arp_dump_t * mp)
3024{
3025 vl_api_registration_t *reg;
3026
3027 reg = vl_api_client_index_to_registration (mp->client_index);
3028 if (!reg)
3029 return;
3030
3031 proxy_arp_walk_ctx_t wctx = {
3032 .reg = reg,
3033 .context = mp->context,
3034 };
3035
3036 proxy_arp_walk (send_proxy_arp_details, &wctx);
3037}
3038
3039static walk_rc_t
3040send_proxy_arp_intfc_details (vnet_main_t * vnm,
3041 vnet_sw_interface_t * si, void *data)
3042{
3043 vl_api_proxy_arp_intfc_details_t *mp;
3044 proxy_arp_walk_ctx_t *ctx;
3045
3046 if (!(si->flags & VNET_SW_INTERFACE_FLAG_PROXY_ARP))
3047 return (WALK_CONTINUE);
3048
3049 ctx = data;
3050
3051 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -04003052 clib_memset (mp, 0, sizeof (*mp));
Neale Ranns0053de62018-05-22 08:40:52 -07003053 mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_INTFC_DETAILS);
3054 mp->context = ctx->context;
3055 mp->sw_if_index = htonl (si->sw_if_index);
3056
3057 vl_api_send_msg (ctx->reg, (u8 *) mp);
3058
3059 return (WALK_CONTINUE);
3060}
3061
3062static void
3063vl_api_proxy_arp_intfc_dump_t_handler (vl_api_proxy_arp_intfc_dump_t * mp)
3064{
3065 vl_api_registration_t *reg;
3066
3067 reg = vl_api_client_index_to_registration (mp->client_index);
3068 if (!reg)
3069 return;
3070
3071 proxy_arp_walk_ctx_t wctx = {
3072 .reg = reg,
3073 .context = mp->context,
3074 };
3075
3076 vnet_sw_interface_walk (vnet_get_main (),
3077 send_proxy_arp_intfc_details, &wctx);
3078}
3079
Neale Rannsb8d44812017-11-10 06:53:54 -08003080static void
3081 vl_api_proxy_arp_intfc_enable_disable_t_handler
3082 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
3083{
3084 int rv = 0;
3085 vnet_main_t *vnm = vnet_get_main ();
3086 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
3087
3088 VALIDATE_SW_IF_INDEX (mp);
3089
3090 vnet_sw_interface_t *si =
3091 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
3092
3093 ASSERT (si);
3094
3095 if (mp->enable_disable)
3096 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3097 else
3098 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
3099
3100 BAD_SW_IF_INDEX_LABEL;
3101
3102 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3103}
3104
John Loc7b43042018-04-13 16:46:22 -04003105static void
3106vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
3107{
3108 int rv = 0;
3109 vlib_main_t *vm = vlib_get_main ();
3110 vl_api_ip_probe_neighbor_reply_t *rmp;
3111 clib_error_t *error;
Neale Ranns37029302018-08-10 05:30:06 -07003112 ip46_address_t dst;
3113 ip46_type_t itype;
John Loc7b43042018-04-13 16:46:22 -04003114
3115 VALIDATE_SW_IF_INDEX (mp);
3116
3117 u32 sw_if_index = ntohl (mp->sw_if_index);
Neale Ranns37029302018-08-10 05:30:06 -07003118 itype = ip_address_decode (&mp->dst, &dst);
John Loc7b43042018-04-13 16:46:22 -04003119
Neale Ranns37029302018-08-10 05:30:06 -07003120 if (IP46_TYPE_IP6 == itype)
3121 error = ip6_probe_neighbor (vm, &dst.ip6, sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003122 else
Neale Ranns37029302018-08-10 05:30:06 -07003123 error = ip4_probe_neighbor (vm, &dst.ip4, sw_if_index, 0);
John Loc7b43042018-04-13 16:46:22 -04003124
3125 if (error)
3126 {
3127 clib_error_report (error);
3128 rv = clib_error_get_code (error);
3129 }
3130
3131 BAD_SW_IF_INDEX_LABEL;
3132
3133 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
3134}
3135
John Lo7f358b32018-04-28 01:19:24 -04003136static void
3137 vl_api_ip_scan_neighbor_enable_disable_t_handler
3138 (vl_api_ip_scan_neighbor_enable_disable_t * mp)
3139{
3140 int rv = 0;
3141 vl_api_ip_scan_neighbor_enable_disable_reply_t *rmp;
3142 ip_neighbor_scan_arg_t arg;
3143
3144 arg.mode = mp->mode;
3145 arg.scan_interval = mp->scan_interval;
3146 arg.max_proc_time = mp->max_proc_time;
3147 arg.max_update = mp->max_update;
3148 arg.scan_int_delay = mp->scan_int_delay;
3149 arg.stale_threshold = mp->stale_threshold;
3150 ip_neighbor_scan_enable_disable (&arg);
3151
3152 REPLY_MACRO (VL_API_IP_SCAN_NEIGHBOR_ENABLE_DISABLE_REPLY);
3153}
3154
Neale Rannsb8d44812017-11-10 06:53:54 -08003155static int
3156ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3157{
3158 vnet_main_t *vnm = vnet_get_main ();
3159 vnet_interface_main_t *im = &vnm->interface_main;
3160 ip4_main_t *im4 = &ip4_main;
3161 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08003162 fib_table_t *fib_table;
3163 ip4_fib_t *fib;
3164 u32 sw_if_index;
3165 int i;
3166 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3167 u32 target_fib_id = ntohl (mp->vrf_id);
3168
Ole Troan3288ed72017-12-06 17:00:05 +01003169 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003170
3171 /* *INDENT-OFF* */
3172 pool_foreach (fib_table, im4->fibs,
3173 ({
3174 vnet_sw_interface_t * si;
3175
3176 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
3177
3178 if (fib->table_id != target_fib_id)
3179 continue;
3180
3181 /* remove any mpls encap/decap labels */
3182 mpls_fib_reset_labels (fib->table_id);
3183
3184 /* remove any proxy arps in this fib */
3185 vnet_proxy_arp_fib_reset (fib->table_id);
3186
3187 /* Set the flow hash for this fib to the default */
3188 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3189
3190 vec_reset_length (sw_if_indices_to_shut);
3191
3192 /* Shut down interfaces in this FIB / clean out intfc routes */
3193 pool_foreach (si, im->sw_interfaces,
3194 ({
3195 u32 sw_if_index = si->sw_if_index;
3196
3197 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
3198 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
3199 fib->index))
3200 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3201 }));
3202
3203 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3204 sw_if_index = sw_if_indices_to_shut[i];
3205
3206 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3207 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3208 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3209 }
3210
3211 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
3212
3213 rv = 0;
3214 break;
3215 })); /* pool_foreach (fib) */
3216 /* *INDENT-ON* */
3217
Ole Troan3288ed72017-12-06 17:00:05 +01003218 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003219 return rv;
3220}
3221
3222static int
3223ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3224{
3225 vnet_main_t *vnm = vnet_get_main ();
3226 vnet_interface_main_t *im = &vnm->interface_main;
3227 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08003228 static u32 *sw_if_indices_to_shut;
3229 fib_table_t *fib_table;
3230 ip6_fib_t *fib;
3231 u32 sw_if_index;
3232 int i;
3233 int rv = VNET_API_ERROR_NO_SUCH_FIB;
3234 u32 target_fib_id = ntohl (mp->vrf_id);
3235
Ole Troan3288ed72017-12-06 17:00:05 +01003236 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08003237
3238 /* *INDENT-OFF* */
3239 pool_foreach (fib_table, im6->fibs,
3240 ({
3241 vnet_sw_interface_t * si;
3242
3243 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
3244
3245 if (fib->table_id != target_fib_id)
3246 continue;
3247
3248 vec_reset_length (sw_if_indices_to_shut);
3249
3250 /* Set the flow hash for this fib to the default */
3251 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
3252
3253 /* Shut down interfaces in this FIB / clean out intfc routes */
3254 pool_foreach (si, im->sw_interfaces,
3255 ({
3256 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
3257 fib->index)
3258 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
3259 }));
3260
3261 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
3262 sw_if_index = sw_if_indices_to_shut[i];
3263
3264 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
3265 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
3266 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
3267 }
3268
3269 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
3270
3271 rv = 0;
3272 break;
3273 })); /* pool_foreach (fib) */
3274 /* *INDENT-ON* */
3275
Ole Troan3288ed72017-12-06 17:00:05 +01003276 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08003277 return rv;
3278}
3279
3280static void
3281vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
3282{
3283 int rv;
3284 vl_api_reset_fib_reply_t *rmp;
3285
3286 if (mp->is_ipv6)
3287 rv = ip6_reset_fib_t_handler (mp);
3288 else
3289 rv = ip4_reset_fib_t_handler (mp);
3290
3291 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
3292}
3293
3294static void
3295vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
3296{
3297 int rv;
3298 vl_api_set_arp_neighbor_limit_reply_t *rmp;
3299 vnet_main_t *vnm = vnet_get_main ();
3300 clib_error_t *error;
3301
3302 vnm->api_errno = 0;
3303
3304 if (mp->is_ipv6)
3305 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
3306 else
3307 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
3308
3309 if (error)
3310 {
3311 clib_error_report (error);
3312 rv = VNET_API_ERROR_UNSPECIFIED;
3313 }
3314 else
3315 {
3316 rv = vnm->api_errno;
3317 }
3318
3319 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
3320}
3321
Klement Sekera75e7d132017-09-20 08:26:30 +02003322void
3323vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
3324{
3325 vl_api_ip_reassembly_set_reply_t *rmp;
3326 int rv = 0;
3327 if (mp->is_ip6)
3328 {
3329 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3330 clib_net_to_host_u32 (mp->max_reassemblies),
3331 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3332 }
3333 else
3334 {
3335 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
3336 clib_net_to_host_u32 (mp->max_reassemblies),
3337 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
3338 }
3339
3340 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
3341}
3342
3343void
3344vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
3345{
3346 unix_shared_memory_queue_t *q;
3347
3348 q = vl_api_client_index_to_input_queue (mp->client_index);
3349
3350 if (q == 0)
3351 return;
3352
3353 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04003354 clib_memset (rmp, 0, sizeof (*rmp));
Klement Sekera75e7d132017-09-20 08:26:30 +02003355 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
3356 rmp->context = mp->context;
3357 rmp->retval = 0;
3358 if (mp->is_ip6)
3359 {
3360 rmp->is_ip6 = 1;
3361 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3362 &rmp->expire_walk_interval_ms);
3363 }
3364 else
3365 {
3366 rmp->is_ip6 = 0;
3367 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
3368 &rmp->expire_walk_interval_ms);
3369 }
3370 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
3371 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
3372 rmp->expire_walk_interval_ms =
3373 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
3374 vl_msg_api_send_shmem (q, (u8 *) & rmp);
3375}
3376
Klement Sekera4c533132018-02-22 11:41:12 +01003377void
3378 vl_api_ip_reassembly_enable_disable_t_handler
3379 (vl_api_ip_reassembly_enable_disable_t * mp)
3380{
3381 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
3382 int rv = 0;
3383 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3384 mp->enable_ip4);
3385 if (0 == rv)
3386 {
3387 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
3388 mp->enable_ip6);
3389 }
3390
Choree7f61d12018-11-28 10:27:58 +03303391 REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
Klement Sekera4c533132018-02-22 11:41:12 +01003392}
3393
Pavel Kotucek609e1212018-11-27 09:59:44 +01003394void
3395send_ip_punt_redirect_details (vl_api_registration_t * reg,
3396 u32 context, u32 sw_if_index,
3397 ip_punt_redirect_rx_t * pr, u8 is_ipv6)
3398{
3399 vl_api_ip_punt_redirect_details_t *mp;
3400
3401 mp = vl_msg_api_alloc (sizeof (*mp));
3402 if (!mp)
3403 return;
3404
3405 clib_memset (mp, 0, sizeof (*mp));
3406 mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
3407 mp->context = context;
3408 mp->punt.rx_sw_if_index = htonl (sw_if_index);
3409 mp->punt.tx_sw_if_index = htonl (pr->tx_sw_if_index);
3410 if (is_ipv6)
3411 {
3412 ip_address_encode (&pr->nh, IP46_TYPE_IP6, &mp->punt.nh);
3413 }
3414 else
3415 {
3416 ip_address_encode (&pr->nh, IP46_TYPE_IP4, &mp->punt.nh);
3417 }
3418
3419 vl_api_send_msg (reg, (u8 *) mp);
3420}
3421
3422static void
3423vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
3424{
3425 vl_api_registration_t *reg;
3426 u32 sw_if_index;
3427 int rv __attribute__ ((unused)) = 0;
3428
3429 sw_if_index = ntohl (mp->sw_if_index);
3430 reg = vl_api_client_index_to_registration (mp->client_index);
3431 if (!reg)
3432 return;
3433
3434 if (~0 != sw_if_index)
3435 VALIDATE_SW_IF_INDEX (mp);
3436
3437 ip_punt_redirect_detail_t *pr, *prs;
3438 if (mp->is_ipv6)
3439 {
3440 prs = ip6_punt_redirect_entries (sw_if_index);
3441 /* *INDENT-OFF* */
3442 vec_foreach (pr, prs)
3443 {
3444 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 1);
3445 }
3446 /* *INDENT-ON* */
3447 vec_free (prs);
3448 }
3449 else
3450 {
3451 prs = ip4_punt_redirect_entries (sw_if_index);
3452 /* *INDENT-OFF* */
3453 vec_foreach (pr, prs)
3454 {
3455 send_ip_punt_redirect_details (reg, mp->context, pr->rx_sw_if_index, &pr->punt_redirect, 0);
3456 }
3457 /* *INDENT-ON* */
3458 vec_free (prs);
3459 }
3460
3461 BAD_SW_IF_INDEX_LABEL;
3462}
3463
Dave Barachb5e8a772016-12-06 12:04:42 -05003464#define vl_msg_name_crc_list
3465#include <vnet/ip/ip.api.h>
3466#undef vl_msg_name_crc_list
3467
3468static void
3469setup_message_id_table (api_main_t * am)
3470{
3471#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
3472 foreach_vl_msg_name_crc_ip;
3473#undef _
3474}
3475
3476static clib_error_t *
3477ip_api_hookup (vlib_main_t * vm)
3478{
3479 api_main_t *am = &api_main;
3480
3481#define _(N,n) \
3482 vl_msg_api_set_handlers(VL_API_##N, #n, \
3483 vl_api_##n##_t_handler, \
3484 vl_noop_handler, \
3485 vl_api_##n##_t_endian, \
3486 vl_api_##n##_t_print, \
3487 sizeof(vl_api_##n##_t), 1);
3488 foreach_ip_api_msg;
3489#undef _
3490
3491 /*
Neale Ranns6a231a12018-10-17 06:38:00 +00003492 * Mark the route add/del API as MP safe
3493 */
3494 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE] = 1;
3495 am->is_mp_safe[VL_API_IP_ADD_DEL_ROUTE_REPLY] = 1;
3496
3497 /*
Dave Barachb5e8a772016-12-06 12:04:42 -05003498 * Set up the (msg_name, crc, message-id) table
3499 */
3500 setup_message_id_table (am);
3501
Juraj Sloboda52574522018-05-03 10:03:50 +02003502 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
3503
Dave Barachb5e8a772016-12-06 12:04:42 -05003504 return 0;
3505}
3506
3507VLIB_API_INIT_FUNCTION (ip_api_hookup);
3508
3509/*
3510 * fd.io coding-style-patch-verification: ON
3511 *
3512 * Local Variables:
3513 * eval: (c-set-style "gnu")
3514 * End:
3515 */