blob: 69ff719f3b9f194bca9e5c076f53c0e4a0740c81 [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
Neale Rannsb8d44812017-11-10 06:53:54 -080023#include <vpp/stats/stats.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050024#include <vnet/interface.h>
25#include <vnet/api_errno.h>
26#include <vnet/ethernet/ethernet.h>
27#include <vnet/ip/ip.h>
28#include <vnet/ip/ip6_neighbor.h>
29#include <vnet/fib/fib_table.h>
30#include <vnet/fib/fib_api.h>
31#include <vnet/dpo/drop_dpo.h>
32#include <vnet/dpo/receive_dpo.h>
33#include <vnet/dpo/lookup_dpo.h>
34#include <vnet/dpo/classify_dpo.h>
35#include <vnet/dpo/ip_null_dpo.h>
36#include <vnet/ethernet/arp_packet.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080037#include <vnet/mfib/ip6_mfib.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000038#include <vnet/mfib/ip4_mfib.h>
39#include <vnet/mfib/mfib_signal.h>
Neale Ranns5a8123b2017-01-26 01:18:23 -080040#include <vnet/mfib/mfib_entry.h>
Neale Rannsb8d44812017-11-10 06:53:54 -080041#include <vnet/ip/ip_source_and_port_range_check.h>
42#include <vnet/fib/ip4_fib.h>
43#include <vnet/fib/ip6_fib.h>
44#include <vnet/ip/ip6_hop_by_hop.h>
Klement Sekera75e7d132017-09-20 08:26:30 +020045#include <vnet/ip/ip4_reassembly.h>
46#include <vnet/ip/ip6_reassembly.h>
Dave Barachb5e8a772016-12-06 12:04:42 -050047
48#include <vnet/vnet_msg_enum.h>
49
50#define vl_typedefs /* define message structures */
51#include <vnet/vnet_all_api_h.h>
52#undef vl_typedefs
53
54#define vl_endianfun /* define message structures */
55#include <vnet/vnet_all_api_h.h>
56#undef vl_endianfun
57
58/* instantiate all the print functions we know about */
59#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
60#define vl_printfun
61#include <vnet/vnet_all_api_h.h>
62#undef vl_printfun
63
64#include <vlibapi/api_helper_macros.h>
65
Neale Ranns5a8123b2017-01-26 01:18:23 -080066
Dave Barachb5e8a772016-12-06 12:04:42 -050067#define foreach_ip_api_msg \
68_(IP_FIB_DUMP, ip_fib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050069_(IP6_FIB_DUMP, ip6_fib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080070_(IP_MFIB_DUMP, ip_mfib_dump) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080071_(IP6_MFIB_DUMP, ip6_mfib_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050072_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
Neale Ranns32e1c012016-11-22 17:07:28 +000073_(IP_MROUTE_ADD_DEL, ip_mroute_add_del) \
Neale Ranns5a8123b2017-01-26 01:18:23 -080074_(MFIB_SIGNAL_DUMP, mfib_signal_dump) \
Dave Barachb5e8a772016-12-06 12:04:42 -050075_(IP_ADDRESS_DUMP, ip_address_dump) \
76_(IP_DUMP, ip_dump) \
77_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
Neale Rannsb8d44812017-11-10 06:53:54 -080078_(SET_ARP_NEIGHBOR_LIMIT, set_arp_neighbor_limit) \
John Loc7b43042018-04-13 16:46:22 -040079_(IP_PROBE_NEIGHBOR, ip_probe_neighbor) \
Neale Rannsb8d44812017-11-10 06:53:54 -080080_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
81_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010082_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080083_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
84_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
85_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050086_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070087_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070088_(IP_PUNT_POLICE, ip_punt_police) \
89_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050090_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
91_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
92_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080093_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
94_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010095_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -050096_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
97_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
Florin Coras595992c2017-11-06 17:17:08 -080098 sw_interface_ip6_set_link_local_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080099_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
100_(IOAM_ENABLE, ioam_enable) \
101_(IOAM_DISABLE, ioam_disable) \
102_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
103 ip_source_and_port_range_check_add_del) \
104_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200105 ip_source_and_port_range_check_interface_add_del) \
106_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100107_(IP_REASSEMBLY_GET, ip_reassembly_get) \
108_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)
Dave Barachb5e8a772016-12-06 12:04:42 -0500109
110extern void stats_dslock_with_hint (int hint, int tag);
111extern void stats_dsunlock (void);
112
113static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600114send_ip_neighbor_details (u32 sw_if_index,
115 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500116 u8 is_static,
117 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800118 u8 * ip_address, vl_api_registration_t * reg,
119 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500120{
121 vl_api_ip_neighbor_details_t *mp;
122
123 mp = vl_msg_api_alloc (sizeof (*mp));
124 memset (mp, 0, sizeof (*mp));
125 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
126 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600127 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500128 mp->is_ipv6 = is_ipv6;
129 mp->is_static = is_static;
130 memcpy (mp->mac_address, mac_address, 6);
131 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
132
Florin Coras6c4dae22018-01-09 06:39:23 -0800133 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500134}
135
136static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500137vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
138{
Florin Coras6c4dae22018-01-09 06:39:23 -0800139 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500140
Florin Coras6c4dae22018-01-09 06:39:23 -0800141 reg = vl_api_client_index_to_registration (mp->client_index);
142 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500143 return;
144
145 u32 sw_if_index = ntohl (mp->sw_if_index);
146
147 if (mp->is_ipv6)
148 {
149 ip6_neighbor_t *n, *ns;
150
151 ns = ip6_neighbors_entries (sw_if_index);
152 /* *INDENT-OFF* */
153 vec_foreach (n, ns)
154 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500155 send_ip_neighbor_details
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600156 (sw_if_index, mp->is_ipv6,
157 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500158 (u8 *) n->link_layer_address,
159 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800160 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500161 }
162 /* *INDENT-ON* */
163 vec_free (ns);
164 }
165 else
166 {
167 ethernet_arp_ip4_entry_t *n, *ns;
168
169 ns = ip4_neighbor_entries (sw_if_index);
170 /* *INDENT-OFF* */
171 vec_foreach (n, ns)
172 {
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600173 send_ip_neighbor_details (sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500174 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
175 (u8*) n->ethernet_address,
176 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800177 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500178 }
179 /* *INDENT-ON* */
180 vec_free (ns);
181 }
182}
183
Dave Barachb5e8a772016-12-06 12:04:42 -0500184static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500185send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800186 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700187 const fib_table_t * table,
188 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500189 fib_route_path_encode_t * api_rpaths, u32 context)
190{
191 vl_api_ip_fib_details_t *mp;
192 fib_route_path_encode_t *api_rpath;
193 vl_api_fib_path_t *fp;
194 int path_count;
195
196 path_count = vec_len (api_rpaths);
197 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
198 if (!mp)
199 return;
200 memset (mp, 0, sizeof (*mp));
201 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
202 mp->context = context;
203
Neale Ranns2297af02017-09-12 09:45:04 -0700204 mp->table_id = htonl (table->ft_table_id);
205 memcpy (mp->table_name, table->ft_desc,
206 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500207 mp->address_length = pfx->fp_len;
208 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
209
210 mp->count = htonl (path_count);
211 fp = mp->path;
212 vec_foreach (api_rpath, api_rpaths)
213 {
Neale Ranns81458422018-03-12 06:59:36 -0700214 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500215 fp++;
216 }
217
Florin Coras6c4dae22018-01-09 06:39:23 -0800218 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500219}
220
Neale Rannsa3af3372017-03-28 03:49:52 -0700221typedef struct vl_api_ip_fib_dump_walk_ctx_t_
222{
223 fib_node_index_t *feis;
224} vl_api_ip_fib_dump_walk_ctx_t;
225
Neale Ranns89541992017-04-06 04:41:02 -0700226static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700227vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
228{
229 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
230
231 vec_add1 (ctx->feis, fei);
232
Neale Ranns89541992017-04-06 04:41:02 -0700233 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700234}
235
Dave Barachb5e8a772016-12-06 12:04:42 -0500236static void
237vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
238{
239 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800240 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500241 ip4_main_t *im = &ip4_main;
242 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700243 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500244 fib_prefix_t pfx;
245 u32 fib_index;
246 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700247 vl_api_ip_fib_dump_walk_ctx_t ctx = {
248 .feis = NULL,
249 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500250
Florin Coras6c4dae22018-01-09 06:39:23 -0800251 reg = vl_api_client_index_to_registration (mp->client_index);
252 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500253 return;
254
255 /* *INDENT-OFF* */
256 pool_foreach (fib_table, im->fibs,
257 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700258 fib_table_walk(fib_table->ft_index,
259 FIB_PROTOCOL_IP4,
260 vl_api_ip_fib_dump_walk,
261 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500262 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500263 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500264
Neale Rannsa3af3372017-03-28 03:49:52 -0700265 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500266
Neale Rannsa3af3372017-03-28 03:49:52 -0700267 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500268 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500269 fib_entry_get_prefix (*lfeip, &pfx);
270 fib_index = fib_entry_get_fib_index (*lfeip);
271 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500272 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500273 fib_entry_encode (*lfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800274 send_ip_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500276 }
277
Neale Rannsa3af3372017-03-28 03:49:52 -0700278 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500279}
280
281static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500282send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800283 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800284 const fib_table_t * table,
285 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500286 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500287{
288 vl_api_ip6_fib_details_t *mp;
289 fib_route_path_encode_t *api_rpath;
290 vl_api_fib_path_t *fp;
291 int path_count;
292
Dave Barachd7cb1b52016-12-09 09:52:16 -0500293 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500294 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
295 if (!mp)
296 return;
297 memset (mp, 0, sizeof (*mp));
298 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
299 mp->context = context;
300
Neale Rannsa161a6d2017-11-14 08:10:41 -0800301 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500302 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500303 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800304 memcpy (mp->table_name, table->ft_desc,
305 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500306
307 mp->count = htonl (path_count);
308 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500309 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500310 {
Neale Ranns81458422018-03-12 06:59:36 -0700311 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500312 fp++;
313 }
314
Florin Coras6c4dae22018-01-09 06:39:23 -0800315 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500316}
317
Dave Barachd7cb1b52016-12-09 09:52:16 -0500318typedef struct apt_ip6_fib_show_ctx_t_
319{
320 u32 fib_index;
321 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500322} api_ip6_fib_show_ctx_t;
323
324static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500325api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500326{
327 api_ip6_fib_show_ctx_t *ctx = arg;
328
329 if ((kvp->key[2] >> 32) == ctx->fib_index)
330 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500331 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500332 }
333}
334
335static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800336api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337 vl_api_ip6_fib_dump_t * mp,
338 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500339{
340 vpe_api_main_t *am = &vpe_api_main;
341 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500342 fib_node_index_t *fib_entry_index;
343 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700344 .fib_index = fib_table->ft_index,
345 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500346 };
347 fib_route_path_encode_t *api_rpaths;
348 fib_prefix_t pfx;
349
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 BV (clib_bihash_foreach_key_value_pair)
351 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
352 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500353
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500355
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 vec_foreach (fib_entry_index, ctx.entries)
357 {
358 fib_entry_get_prefix (*fib_entry_index, &pfx);
359 api_rpaths = NULL;
360 fib_entry_encode (*fib_entry_index, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800361 send_ip6_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500362 vec_free (api_rpaths);
363 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500364
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500366}
367
368static void
369vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
370{
Florin Coras6c4dae22018-01-09 06:39:23 -0800371 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500372 ip6_main_t *im6 = &ip6_main;
373 fib_table_t *fib_table;
374
Florin Coras6c4dae22018-01-09 06:39:23 -0800375 reg = vl_api_client_index_to_registration (mp->client_index);
376 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500377 return;
378
379 /* *INDENT-OFF* */
380 pool_foreach (fib_table, im6->fibs,
381 ({
Neale Ranns81458422018-03-12 06:59:36 -0700382 /* don't send link locals */
383 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
384 continue;
385
Florin Coras6c4dae22018-01-09 06:39:23 -0800386 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500387 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500388 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500389}
390
391static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800392send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800393 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800394{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800395 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800396 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800397 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800398 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800399 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800400 int path_count;
401
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800402 mfib_entry = mfib_entry_get (mfei);
403 mfib_entry_get_prefix (mfei, &pfx);
404 mfib_entry_encode (mfei, &api_rpaths);
405
Neale Ranns5a8123b2017-01-26 01:18:23 -0800406 path_count = vec_len (api_rpaths);
407 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
408 if (!mp)
409 return;
410 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700411 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800412 mp->context = context;
413
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800414 mp->rpf_id = mfib_entry->mfe_rpf_id;
415 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800416 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800417 mp->address_length = pfx.fp_len;
418 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
419 sizeof (pfx.fp_grp_addr.ip4));
420 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
421 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800422
423 mp->count = htonl (path_count);
424 fp = mp->path;
425 vec_foreach (api_rpath, api_rpaths)
426 {
Neale Ranns81458422018-03-12 06:59:36 -0700427 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800428 fp++;
429 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800430 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800431
Florin Coras6c4dae22018-01-09 06:39:23 -0800432 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800433}
434
435typedef struct vl_api_ip_mfib_dump_ctc_t_
436{
437 fib_node_index_t *entries;
438} vl_api_ip_mfib_dump_ctc_t;
439
440static int
441vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
442{
443 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
444
445 vec_add1 (ctx->entries, fei);
446
447 return (0);
448}
449
450static void
451vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
452{
Florin Coras6c4dae22018-01-09 06:39:23 -0800453 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800454 ip4_main_t *im = &ip4_main;
455 mfib_table_t *mfib_table;
456 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800457 vl_api_ip_mfib_dump_ctc_t ctx = {
458 .entries = NULL,
459 };
460
Florin Coras6c4dae22018-01-09 06:39:23 -0800461 reg = vl_api_client_index_to_registration (mp->client_index);
462 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800463 return;
464
Neale Ranns5a8123b2017-01-26 01:18:23 -0800465 /* *INDENT-OFF* */
466 pool_foreach (mfib_table, im->mfibs,
467 ({
468 ip4_mfib_table_walk(&mfib_table->v4,
469 vl_api_ip_mfib_table_dump_walk,
470 &ctx);
471
472 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
473
474 vec_foreach (mfeip, ctx.entries)
475 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800476 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800477 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800478 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800479 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800480 vec_reset_length (ctx.entries);
481
482 }));
483 /* *INDENT-ON* */
484
485 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800486}
487
488static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800489send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800490 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800491 u32 table_id,
492 mfib_prefix_t * pfx,
493 fib_route_path_encode_t * api_rpaths, u32 context)
494{
495 vl_api_ip6_mfib_details_t *mp;
496 fib_route_path_encode_t *api_rpath;
497 vl_api_fib_path_t *fp;
498 int path_count;
499
500 path_count = vec_len (api_rpaths);
501 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
502 if (!mp)
503 return;
504 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700505 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800506 mp->context = context;
507
508 mp->table_id = htonl (table_id);
509 mp->address_length = pfx->fp_len;
510 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
511 sizeof (pfx->fp_grp_addr.ip6));
512 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
513 sizeof (pfx->fp_src_addr.ip6));
514
515 mp->count = htonl (path_count);
516 fp = mp->path;
517 vec_foreach (api_rpath, api_rpaths)
518 {
Neale Ranns81458422018-03-12 06:59:36 -0700519 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800520 fp++;
521 }
522
Florin Coras6c4dae22018-01-09 06:39:23 -0800523 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800524}
525
526typedef struct vl_api_ip6_mfib_dump_ctc_t_
527{
528 fib_node_index_t *entries;
529} vl_api_ip6_mfib_dump_ctc_t;
530
531static int
532vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
533{
534 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
535
536 vec_add1 (ctx->entries, fei);
537
538 return (0);
539}
540
541static void
542vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
543{
544 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800545 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800546 ip6_main_t *im = &ip6_main;
547 mfib_table_t *mfib_table;
548 fib_node_index_t *mfeip;
549 mfib_prefix_t pfx;
550 fib_route_path_encode_t *api_rpaths = NULL;
551 vl_api_ip6_mfib_dump_ctc_t ctx = {
552 .entries = NULL,
553 };
554
Florin Coras6c4dae22018-01-09 06:39:23 -0800555 reg = vl_api_client_index_to_registration (mp->client_index);
556 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800557 return;
558
559
560 /* *INDENT-OFF* */
561 pool_foreach (mfib_table, im->mfibs,
562 ({
563 ip6_mfib_table_walk(&mfib_table->v6,
564 vl_api_ip6_mfib_table_dump_walk,
565 &ctx);
566
567 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
568
569 vec_foreach(mfeip, ctx.entries)
570 {
571 mfib_entry_get_prefix (*mfeip, &pfx);
572 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800573 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800574 mfib_table->mft_table_id,
575 &pfx, api_rpaths,
576 mp->context);
577 }
578 vec_reset_length (api_rpaths);
579 vec_reset_length (ctx.entries);
580
581 }));
582 /* *INDENT-ON* */
583
584 vec_free (ctx.entries);
585 vec_free (api_rpaths);
586}
587
588static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700589vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
590 vlib_main_t * vm)
591{
592 vl_api_ip_punt_police_reply_t *rmp;
593 int rv = 0;
594
595 if (mp->is_ip6)
596 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
597 else
598 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
599
600 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
601}
602
603static void
604vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
605 vlib_main_t * vm)
606{
607 vl_api_ip_punt_redirect_reply_t *rmp;
608 int rv = 0;
609
610 if (mp->is_add)
611 {
612 ip46_address_t nh;
613
614 memset (&nh, 0, sizeof (nh));
615
616 if (mp->is_ip6)
617 {
618 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
619
620 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
621 ntohl (mp->tx_sw_if_index), &nh);
622 }
623 else
624 {
625 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
626
627 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
628 ntohl (mp->tx_sw_if_index), &nh);
629 }
630 }
631 else
632 {
633 if (mp->is_ip6)
634 {
635 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
636 }
637 else
638 {
639 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
640 }
641 }
642
643 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
644}
645
646static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500647vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
648 vlib_main_t * vm)
649{
650 vl_api_ip_neighbor_add_del_reply_t *rmp;
651 vnet_main_t *vnm = vnet_get_main ();
652 int rv = 0;
653
654 VALIDATE_SW_IF_INDEX (mp);
655
656 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
657
658 /*
659 * there's no validation here of the ND/ARP entry being added.
660 * The expectation is that the FIB will ensure that nothing bad
661 * will come of adding bogus entries.
662 */
663 if (mp->is_ipv6)
664 {
665 if (mp->is_add)
666 rv = vnet_set_ip6_ethernet_neighbor
667 (vm, ntohl (mp->sw_if_index),
668 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800669 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
670 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500671 else
672 rv = vnet_unset_ip6_ethernet_neighbor
673 (vm, ntohl (mp->sw_if_index),
674 (ip6_address_t *) (mp->dst_address),
675 mp->mac_address, sizeof (mp->mac_address));
676 }
677 else
678 {
679 ethernet_arp_ip4_over_ethernet_address_t a;
680
681 clib_memcpy (&a.ethernet, mp->mac_address, 6);
682 clib_memcpy (&a.ip4, mp->dst_address, 4);
683
684 if (mp->is_add)
685 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800686 &a, mp->is_static,
687 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500688 else
689 rv =
690 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
691 }
692
Dave Barachb5e8a772016-12-06 12:04:42 -0500693 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700694
695 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500696 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
697}
698
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700699void
Neale Ranns15002542017-09-10 04:39:11 -0700700ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
701{
702 u32 fib_index, mfib_index;
703
704 /*
705 * ignore action on the default table - this is always present
706 * and cannot be added nor deleted from the API
707 */
708 if (0 != table_id)
709 {
710 /*
711 * The API holds only one lock on the table.
712 * i.e. it can be added many times via the API but needs to be
713 * deleted only once.
714 * The FIB index for unicast and multicast is not necessarily the
715 * same, since internal VPP systesm (like LISP and SR) create
716 * their own unicast tables.
717 */
718 fib_index = fib_table_find (fproto, table_id);
719 mfib_index = mfib_table_find (fproto, table_id);
720
721 if (~0 != fib_index)
722 {
723 fib_table_unlock (fib_index, fproto,
724 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
725 }
726 if (~0 != mfib_index)
727 {
728 mfib_table_unlock (mfib_index, fproto,
729 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
730 }
731 }
732}
733
734void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700735vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
736{
737 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700738 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
739 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700740 int rv = 0;
741
Neale Ranns15002542017-09-10 04:39:11 -0700742 if (mp->is_add)
743 {
Neale Ranns2297af02017-09-12 09:45:04 -0700744 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700745 }
746 else
747 {
748 ip_table_delete (fproto, table_id, 1);
749 }
750
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700751 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
752}
753
Dave Barachb5e8a772016-12-06 12:04:42 -0500754int
755add_del_route_t_handler (u8 is_multipath,
756 u8 is_add,
757 u8 is_drop,
758 u8 is_unreach,
759 u8 is_prohibit,
760 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800761 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500762 u8 is_classify,
763 u32 classify_table_index,
764 u8 is_resolve_host,
765 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800766 u8 is_interface_rx,
767 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800768 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700769 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800770 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500771 u32 fib_index,
772 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700773 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500774 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800775 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500776 u32 next_hop_sw_if_index,
777 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700778 u16 next_hop_weight,
779 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500780 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800781 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500782{
783 vnet_classify_main_t *cm = &vnet_classify_main;
784 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
785 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700786 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500787 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
788 .frp_sw_if_index = next_hop_sw_if_index,
789 .frp_fib_index = next_hop_fib_index,
790 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700791 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500792 .frp_label_stack = next_hop_out_label_stack,
793 };
794 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800795 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500796
Neale Rannscaac3502017-09-18 18:04:10 -0700797 /*
798 * the special INVALID label meams we are not recursing via a
799 * label. Exp-null value is never a valid via-label so that
800 * also means it's not a via-label and means clients that set
801 * it to 0 by default get the expected behaviour
802 */
803 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500804 {
Neale Rannsda78f952017-05-24 09:15:43 -0700805 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500806 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800807 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500808 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800809 if (is_dvr)
810 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500811 if (is_resolve_host)
812 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
813 if (is_resolve_attached)
814 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800815 if (is_interface_rx)
816 path_flags |= FIB_ROUTE_PATH_INTF_RX;
817 if (is_rpf_id)
818 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700819 if (is_source_lookup)
820 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800821 if (is_multicast)
822 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800823 if (is_udp_encap)
824 {
825 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
826 path.frp_udp_encap_id = next_hop_id;
827 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800828 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
829 && path.frp_fib_index != ~0)
830 {
831 path_flags |= FIB_ROUTE_PATH_DEAG;
832 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500833
834 path.frp_flags = path_flags;
835
836 if (is_multipath)
837 {
838 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
839
840
841 vec_add1 (paths, path);
842
843 if (is_add)
844 fib_table_entry_path_add2 (fib_index,
845 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800846 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500847 else
848 fib_table_entry_path_remove2 (fib_index,
849 prefix, FIB_SOURCE_API, paths);
850
851 vec_free (paths);
852 stats_dsunlock ();
853 return 0;
854 }
855
856 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
857
858 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
859 {
860 /*
861 * special route types that link directly to the adj
862 */
863 if (is_add)
864 {
865 dpo_id_t dpo = DPO_INVALID;
866 dpo_proto_t dproto;
867
868 dproto = fib_proto_to_dpo (prefix->fp_proto);
869
870 if (is_drop)
871 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
872 else if (is_local)
873 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
874 else if (is_unreach)
875 ip_null_dpo_add_and_lock (dproto,
876 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
877 else if (is_prohibit)
878 ip_null_dpo_add_and_lock (dproto,
879 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
880 &dpo);
881 else if (is_classify)
882 {
883 if (pool_is_free_index (cm->tables,
884 ntohl (classify_table_index)))
885 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500886 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500887 return VNET_API_ERROR_NO_SUCH_TABLE;
888 }
889
890 dpo_set (&dpo, DPO_CLASSIFY, dproto,
891 classify_dpo_create (dproto,
892 ntohl (classify_table_index)));
893 }
894 else
895 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500896 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500897 return VNET_API_ERROR_NO_SUCH_TABLE;
898 }
899
900 fib_table_entry_special_dpo_update (fib_index,
901 prefix,
902 FIB_SOURCE_API,
903 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
904 dpo_reset (&dpo);
905 }
906 else
907 {
908 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
909 }
910 }
911 else
912 {
913 if (is_add)
914 {
915 vec_add1 (paths, path);
916 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800917 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500918 vec_free (paths);
919 }
920 else
921 {
922 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
923 }
924 }
925
Dave Barachd7cb1b52016-12-09 09:52:16 -0500926 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500927 return (0);
928}
929
930int
931add_del_route_check (fib_protocol_t table_proto,
932 u32 table_id,
933 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700934 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500935 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800936 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500937{
938 vnet_main_t *vnm = vnet_get_main ();
939
940 *fib_index = fib_table_find (table_proto, ntohl (table_id));
941 if (~0 == *fib_index)
942 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800943 /* No such VRF, and we weren't asked to create one */
944 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500945 }
946
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800947 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500948 {
949 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
950 ntohl (next_hop_sw_if_index)))
951 {
952 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
953 }
954 }
955 else
956 {
Neale Rannsda78f952017-05-24 09:15:43 -0700957 fib_protocol_t fib_nh_proto;
958
959 if (next_hop_table_proto > DPO_PROTO_MPLS)
960 return (0);
961
962 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
963
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800964 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700965 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800966 ntohl (next_hop_table_id));
967 else
Neale Rannsda78f952017-05-24 09:15:43 -0700968 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800969 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500970
971 if (~0 == *next_hop_fib_index)
972 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800973 /* No such VRF, and we weren't asked to create one */
974 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500975 }
976 }
977
978 return (0);
979}
980
981static int
982ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
983{
984 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800985 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500986 int rv, ii, n_labels;;
987
988 rv = add_del_route_check (FIB_PROTOCOL_IP4,
989 mp->table_id,
990 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700991 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500992 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700993 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500994
995 if (0 != rv)
996 return (rv);
997
998 fib_prefix_t pfx = {
999 .fp_len = mp->dst_address_length,
1000 .fp_proto = FIB_PROTOCOL_IP4,
1001 };
1002 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1003
1004 ip46_address_t nh;
1005 memset (&nh, 0, sizeof (nh));
1006 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1007
1008 n_labels = mp->next_hop_n_out_labels;
1009 if (n_labels == 0)
1010 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001011 else
1012 {
1013 vec_validate (label_stack, n_labels - 1);
1014 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001015 {
1016 label_stack[ii].fml_value =
1017 ntohl (mp->next_hop_out_label_stack[ii].label);
1018 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1019 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1020 label_stack[ii].fml_mode =
1021 (mp->next_hop_out_label_stack[ii].is_uniform ?
1022 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1023 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001024 }
1025
1026 return (add_del_route_t_handler (mp->is_multipath,
1027 mp->is_add,
1028 mp->is_drop,
1029 mp->is_unreach,
1030 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001031 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001032 mp->is_classify,
1033 mp->classify_table_index,
1034 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001035 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001036 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001037 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001038 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001039 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001040 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001041 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001042 ntohl (mp->next_hop_sw_if_index),
1043 next_hop_fib_index,
1044 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001045 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001046 ntohl (mp->next_hop_via_label),
1047 label_stack));
1048}
1049
1050static int
1051ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1052{
Neale Ranns31ed7442018-02-23 05:29:09 -08001053 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001054 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001055 int rv, ii, n_labels;;
1056
1057 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1058 mp->table_id,
1059 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001060 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001061 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001062 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001063
1064 if (0 != rv)
1065 return (rv);
1066
1067 fib_prefix_t pfx = {
1068 .fp_len = mp->dst_address_length,
1069 .fp_proto = FIB_PROTOCOL_IP6,
1070 };
1071 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1072
1073 ip46_address_t nh;
1074 memset (&nh, 0, sizeof (nh));
1075 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1076
1077 n_labels = mp->next_hop_n_out_labels;
1078 if (n_labels == 0)
1079 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001080 else
1081 {
1082 vec_validate (label_stack, n_labels - 1);
1083 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001084 {
1085 label_stack[ii].fml_value =
1086 ntohl (mp->next_hop_out_label_stack[ii].label);
1087 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1088 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1089 label_stack[ii].fml_mode =
1090 (mp->next_hop_out_label_stack[ii].is_uniform ?
1091 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1092 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001093 }
1094
1095 return (add_del_route_t_handler (mp->is_multipath,
1096 mp->is_add,
1097 mp->is_drop,
1098 mp->is_unreach,
1099 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001100 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001101 mp->is_classify,
1102 mp->classify_table_index,
1103 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001104 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001105 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001106 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001107 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001108 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001109 &nh, ntohl (mp->next_hop_id),
1110 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001111 next_hop_fib_index,
1112 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001113 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001114 ntohl (mp->next_hop_via_label),
1115 label_stack));
1116}
1117
1118void
1119vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1120{
1121 vl_api_ip_add_del_route_reply_t *rmp;
1122 int rv;
1123 vnet_main_t *vnm = vnet_get_main ();
1124
1125 vnm->api_errno = 0;
1126
1127 if (mp->is_ipv6)
1128 rv = ip6_add_del_route_t_handler (mp);
1129 else
1130 rv = ip4_add_del_route_t_handler (mp);
1131
1132 rv = (rv == 0) ? vnm->api_errno : rv;
1133
1134 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1135}
1136
Neale Ranns15002542017-09-10 04:39:11 -07001137void
Neale Ranns2297af02017-09-12 09:45:04 -07001138ip_table_create (fib_protocol_t fproto,
1139 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001140{
1141 u32 fib_index, mfib_index;
1142
1143 /*
1144 * ignore action on the default table - this is always present
1145 * and cannot be added nor deleted from the API
1146 */
1147 if (0 != table_id)
1148 {
1149 /*
1150 * The API holds only one lock on the table.
1151 * i.e. it can be added many times via the API but needs to be
1152 * deleted only once.
1153 * The FIB index for unicast and multicast is not necessarily the
1154 * same, since internal VPP systesm (like LISP and SR) create
1155 * their own unicast tables.
1156 */
1157 fib_index = fib_table_find (fproto, table_id);
1158 mfib_index = mfib_table_find (fproto, table_id);
1159
1160 if (~0 == fib_index)
1161 {
Neale Ranns2297af02017-09-12 09:45:04 -07001162 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1163 (is_api ?
1164 FIB_SOURCE_API :
1165 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001166 }
1167 if (~0 == mfib_index)
1168 {
Neale Ranns2297af02017-09-12 09:45:04 -07001169 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1170 (is_api ?
1171 MFIB_SOURCE_API :
1172 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001173 }
1174 }
1175}
1176
Neale Ranns32e1c012016-11-22 17:07:28 +00001177static int
1178add_del_mroute_check (fib_protocol_t table_proto,
1179 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001180 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001181{
1182 vnet_main_t *vnm = vnet_get_main ();
1183
1184 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1185 if (~0 == *fib_index)
1186 {
Neale Ranns15002542017-09-10 04:39:11 -07001187 /* No such table */
1188 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001189 }
1190
1191 if (~0 != ntohl (next_hop_sw_if_index))
1192 {
1193 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1194 ntohl (next_hop_sw_if_index)))
1195 {
1196 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1197 }
1198 }
1199
1200 return (0);
1201}
1202
1203static int
1204mroute_add_del_handler (u8 is_add,
1205 u8 is_local,
1206 u32 fib_index,
1207 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001208 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001209 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001210 fib_rpf_id_t rpf_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001211 u32 next_hop_sw_if_index, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001212{
1213 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1214
1215 fib_route_path_t path = {
1216 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001217 .frp_proto = nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001218 };
1219
1220 if (is_local)
1221 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1222
Neale Rannsd792d9c2017-10-21 10:53:20 -07001223 if (DPO_PROTO_BIER == nh_proto)
1224 {
1225 path.frp_bier_imp = bier_imp;
1226 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1227 }
1228 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001229 {
1230 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001231 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001232 goto done;
1233 }
1234
1235 if (is_add)
1236 {
1237 mfib_table_entry_path_update (fib_index, prefix,
1238 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001239 }
1240 else
1241 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001242 mfib_table_entry_path_remove (fib_index, prefix,
1243 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001244 }
1245
Neale Rannsd792d9c2017-10-21 10:53:20 -07001246done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001247 stats_dsunlock ();
1248 return (0);
1249}
1250
1251static int
1252api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1253{
1254 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001255 dpo_proto_t nh_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +00001256 u32 fib_index;
1257 int rv;
1258
Neale Rannsd792d9c2017-10-21 10:53:20 -07001259 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001260 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1261 rv = add_del_mroute_check (fproto,
1262 mp->table_id,
1263 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001264 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001265
1266 if (0 != rv)
1267 return (rv);
1268
1269 mfib_prefix_t pfx = {
1270 .fp_len = ntohs (mp->grp_address_length),
1271 .fp_proto = fproto,
1272 };
1273
1274 if (FIB_PROTOCOL_IP4 == fproto)
1275 {
1276 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1277 sizeof (pfx.fp_grp_addr.ip4));
1278 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1279 sizeof (pfx.fp_src_addr.ip4));
1280 }
1281 else
1282 {
1283 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1284 sizeof (pfx.fp_grp_addr.ip6));
1285 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1286 sizeof (pfx.fp_src_addr.ip6));
1287 }
1288
1289 return (mroute_add_del_handler (mp->is_add,
1290 mp->is_local,
1291 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001292 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001293 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001294 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001295 ntohl (mp->next_hop_sw_if_index),
Neale Rannsd792d9c2017-10-21 10:53:20 -07001296 ntohl (mp->itf_flags),
1297 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001298}
1299
1300void
1301vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1302{
1303 vl_api_ip_mroute_add_del_reply_t *rmp;
1304 int rv;
1305 vnet_main_t *vnm = vnet_get_main ();
1306
1307 vnm->api_errno = 0;
1308
1309 rv = api_mroute_add_del_t_handler (mp);
1310
1311 rv = (rv == 0) ? vnm->api_errno : rv;
1312
1313 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1314}
1315
Dave Barachb5e8a772016-12-06 12:04:42 -05001316static void
1317send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001318 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1319 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001320{
1321 vl_api_ip_details_t *mp;
1322
1323 mp = vl_msg_api_alloc (sizeof (*mp));
1324 memset (mp, 0, sizeof (*mp));
1325 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1326
1327 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001328 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001329 mp->context = context;
1330
Florin Coras6c4dae22018-01-09 06:39:23 -08001331 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001332}
1333
1334static void
1335send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001336 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001337 u8 * ip, u16 prefix_length,
1338 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001339{
1340 vl_api_ip_address_details_t *mp;
1341
1342 mp = vl_msg_api_alloc (sizeof (*mp));
1343 memset (mp, 0, sizeof (*mp));
1344 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1345
1346 if (is_ipv6)
1347 {
1348 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1349 }
1350 else
1351 {
1352 u32 *tp = (u32 *) mp->ip;
1353 *tp = *(u32 *) ip;
1354 }
1355 mp->prefix_length = prefix_length;
1356 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001357 mp->sw_if_index = htonl (sw_if_index);
1358 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001359
Florin Coras6c4dae22018-01-09 06:39:23 -08001360 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001361}
1362
1363static void
1364vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1365{
1366 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001367 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001368 ip6_address_t *r6;
1369 ip4_address_t *r4;
1370 ip6_main_t *im6 = &ip6_main;
1371 ip4_main_t *im4 = &ip4_main;
1372 ip_lookup_main_t *lm6 = &im6->lookup_main;
1373 ip_lookup_main_t *lm4 = &im4->lookup_main;
1374 ip_interface_address_t *ia = 0;
1375 u32 sw_if_index = ~0;
1376 int rv __attribute__ ((unused)) = 0;
1377
1378 VALIDATE_SW_IF_INDEX (mp);
1379
1380 sw_if_index = ntohl (mp->sw_if_index);
1381
Florin Coras6c4dae22018-01-09 06:39:23 -08001382 reg = vl_api_client_index_to_registration (mp->client_index);
1383 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001384 return;
1385
Dave Barachb5e8a772016-12-06 12:04:42 -05001386 if (mp->is_ipv6)
1387 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001388 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001389 foreach_ip_interface_address (lm6, ia, sw_if_index,
1390 1 /* honor unnumbered */,
1391 ({
1392 r6 = ip_interface_address_get_address (lm6, ia);
1393 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001394 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001395 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001396 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001397 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001398 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001399 else
1400 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001401 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001402 foreach_ip_interface_address (lm4, ia, sw_if_index,
1403 1 /* honor unnumbered */,
1404 ({
1405 r4 = ip_interface_address_get_address (lm4, ia);
1406 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001407 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001408 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001409 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001410 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001411 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001412 BAD_SW_IF_INDEX_LABEL;
1413}
1414
1415static void
1416vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1417{
1418 vpe_api_main_t *am = &vpe_api_main;
1419 vnet_main_t *vnm = vnet_get_main ();
1420 vlib_main_t *vm = vlib_get_main ();
1421 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001422 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001423 vnet_sw_interface_t *si, *sorted_sis;
1424 u32 sw_if_index = ~0;
1425
Florin Coras6c4dae22018-01-09 06:39:23 -08001426 reg = vl_api_client_index_to_registration (mp->client_index);
1427 if (!reg)
1428 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001429
1430 /* Gather interfaces. */
1431 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1432 _vec_len (sorted_sis) = 0;
1433 /* *INDENT-OFF* */
1434 pool_foreach (si, im->sw_interfaces,
1435 ({
1436 vec_add1 (sorted_sis, si[0]);
1437 }));
1438 /* *INDENT-ON* */
1439
1440 vec_foreach (si, sorted_sis)
1441 {
1442 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1443 {
1444 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1445 {
1446 continue;
1447 }
1448 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001449 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001450 }
1451 }
1452}
1453
1454static void
1455set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1456{
1457 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001458 int rv;
1459 u32 table_id;
1460 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001461
Neale Ranns227038a2017-04-21 01:07:59 -07001462 table_id = ntohl (mp->vrf_id);
1463
1464#define _(a,b) if (mp->a) flow_hash_config |= b;
1465 foreach_flow_hash_bit;
1466#undef _
1467
1468 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001469
1470 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1471}
1472
1473static void
1474set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1475{
1476 vl_api_set_ip_flow_hash_reply_t *rmp;
1477 int rv;
1478 u32 table_id;
1479 flow_hash_config_t flow_hash_config = 0;
1480
1481 table_id = ntohl (mp->vrf_id);
1482
1483#define _(a,b) if (mp->a) flow_hash_config |= b;
1484 foreach_flow_hash_bit;
1485#undef _
1486
1487 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1488
1489 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1490}
1491
1492
1493static void
1494vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1495{
1496 if (mp->is_ipv6 == 0)
1497 set_ip4_flow_hash (mp);
1498 else
1499 set_ip6_flow_hash (mp);
1500}
1501
1502static void
1503 vl_api_sw_interface_ip6nd_ra_config_t_handler
1504 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1505{
1506 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1507 vlib_main_t *vm = vlib_get_main ();
1508 int rv = 0;
1509 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1510 default_router;
1511
1512 is_no = mp->is_no == 1;
1513 suppress = mp->suppress == 1;
1514 managed = mp->managed == 1;
1515 other = mp->other == 1;
1516 ll_option = mp->ll_option == 1;
1517 send_unicast = mp->send_unicast == 1;
1518 cease = mp->cease == 1;
1519 default_router = mp->default_router == 1;
1520
1521 VALIDATE_SW_IF_INDEX (mp);
1522
1523 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1524 suppress, managed, other,
1525 ll_option, send_unicast, cease,
1526 default_router, ntohl (mp->lifetime),
1527 ntohl (mp->initial_count),
1528 ntohl (mp->initial_interval),
1529 ntohl (mp->max_interval),
1530 ntohl (mp->min_interval), is_no);
1531
1532 BAD_SW_IF_INDEX_LABEL;
1533
1534 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1535}
1536
1537static void
1538 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1539 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1540{
1541 vlib_main_t *vm = vlib_get_main ();
1542 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1543 int rv = 0;
1544 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1545
1546 VALIDATE_SW_IF_INDEX (mp);
1547
1548 is_no = mp->is_no == 1;
1549 use_default = mp->use_default == 1;
1550 no_advertise = mp->no_advertise == 1;
1551 off_link = mp->off_link == 1;
1552 no_autoconfig = mp->no_autoconfig == 1;
1553 no_onlink = mp->no_onlink == 1;
1554
1555 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1556 (ip6_address_t *) mp->address,
1557 mp->address_length, use_default,
1558 ntohl (mp->val_lifetime),
1559 ntohl (mp->pref_lifetime), no_advertise,
1560 off_link, no_autoconfig, no_onlink, is_no);
1561
1562 BAD_SW_IF_INDEX_LABEL;
1563 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1564}
1565
1566static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001567send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001568 u32 context,
1569 const ip46_address_t * addr, u32 sw_if_index)
1570{
1571 vl_api_ip6nd_proxy_details_t *mp;
1572
1573 mp = vl_msg_api_alloc (sizeof (*mp));
1574 memset (mp, 0, sizeof (*mp));
1575 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1576 mp->context = context;
1577 mp->sw_if_index = htonl (sw_if_index);
1578 memcpy (mp->address, addr, 16);
1579
Florin Coras6c4dae22018-01-09 06:39:23 -08001580 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001581}
1582
Neale Ranns3f844d02017-02-18 00:03:54 -08001583typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1584{
1585 u32 *indices;
1586} api_ip6nd_proxy_fib_table_walk_ctx_t;
1587
Neale Ranns89541992017-04-06 04:41:02 -07001588static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001589api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1590{
1591 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1592
1593 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1594 {
1595 vec_add1 (ctx->indices, fei);
1596 }
1597
Neale Ranns89541992017-04-06 04:41:02 -07001598 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001599}
1600
1601static void
1602vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1603{
1604 ip6_main_t *im6 = &ip6_main;
1605 fib_table_t *fib_table;
1606 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1607 .indices = NULL,
1608 };
1609 fib_node_index_t *feip;
1610 fib_prefix_t pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001611 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001612
Florin Coras6c4dae22018-01-09 06:39:23 -08001613 reg = vl_api_client_index_to_registration (mp->client_index);
1614 if (!reg)
1615 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001616
1617 /* *INDENT-OFF* */
1618 pool_foreach (fib_table, im6->fibs,
1619 ({
1620 fib_table_walk(fib_table->ft_index,
1621 FIB_PROTOCOL_IP6,
1622 api_ip6nd_proxy_fib_table_walk,
1623 &ctx);
1624 }));
1625 /* *INDENT-ON* */
1626
1627 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1628
1629 vec_foreach (feip, ctx.indices)
1630 {
1631 fib_entry_get_prefix (*feip, &pfx);
1632
Florin Coras6c4dae22018-01-09 06:39:23 -08001633 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001634 mp->context,
1635 &pfx.fp_addr,
1636 fib_entry_get_resolving_interface (*feip));
1637 }
1638
1639 vec_free (ctx.indices);
1640}
1641
1642static void
1643vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1644{
1645 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1646 int rv = 0;
1647
1648 VALIDATE_SW_IF_INDEX (mp);
1649
1650 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1651 (ip6_address_t *) mp->address, mp->is_del);
1652
1653 BAD_SW_IF_INDEX_LABEL;
1654 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1655}
1656
1657static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001658 vl_api_ip6nd_send_router_solicitation_t_handler
1659 (vl_api_ip6nd_send_router_solicitation_t * mp)
1660{
1661 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1662 icmp6_send_router_solicitation_params_t params;
1663 vlib_main_t *vm = vlib_get_main ();
1664 int rv = 0;
1665
1666 VALIDATE_SW_IF_INDEX (mp);
1667
1668 BAD_SW_IF_INDEX_LABEL;
1669 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1670
1671 if (rv != 0)
1672 return;
1673
1674 params.irt = ntohl (mp->irt);
1675 params.mrt = ntohl (mp->mrt);
1676 params.mrc = ntohl (mp->mrc);
1677 params.mrd = ntohl (mp->mrd);
1678
1679 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1680 &params);
1681}
1682
1683static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001684 vl_api_sw_interface_ip6_enable_disable_t_handler
1685 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1686{
1687 vlib_main_t *vm = vlib_get_main ();
1688 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1689 vnet_main_t *vnm = vnet_get_main ();
1690 int rv = 0;
1691 clib_error_t *error;
1692
1693 vnm->api_errno = 0;
1694
1695 VALIDATE_SW_IF_INDEX (mp);
1696
1697 error =
1698 (mp->enable == 1) ? enable_ip6_interface (vm,
1699 ntohl (mp->sw_if_index)) :
1700 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1701
1702 if (error)
1703 {
1704 clib_error_report (error);
1705 rv = VNET_API_ERROR_UNSPECIFIED;
1706 }
1707 else
1708 {
1709 rv = vnm->api_errno;
1710 }
1711
1712 BAD_SW_IF_INDEX_LABEL;
1713
1714 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1715}
1716
1717static void
1718 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1719 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1720{
1721 vlib_main_t *vm = vlib_get_main ();
1722 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1723 int rv = 0;
1724 clib_error_t *error;
1725 vnet_main_t *vnm = vnet_get_main ();
1726
1727 vnm->api_errno = 0;
1728
1729 VALIDATE_SW_IF_INDEX (mp);
1730
1731 error = set_ip6_link_local_address (vm,
1732 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001733 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001734 if (error)
1735 {
1736 clib_error_report (error);
1737 rv = VNET_API_ERROR_UNSPECIFIED;
1738 }
1739 else
1740 {
1741 rv = vnm->api_errno;
1742 }
1743
1744 BAD_SW_IF_INDEX_LABEL;
1745
1746 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1747}
1748
Neale Ranns32e1c012016-11-22 17:07:28 +00001749void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001750vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001751 u32 context, const mfib_signal_t * mfs)
1752{
1753 vl_api_mfib_signal_details_t *mp;
1754 mfib_prefix_t prefix;
1755 mfib_table_t *mfib;
1756 mfib_itf_t *mfi;
1757
1758 mp = vl_msg_api_alloc (sizeof (*mp));
1759
1760 memset (mp, 0, sizeof (*mp));
1761 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1762 mp->context = context;
1763
1764 mfi = mfib_itf_get (mfs->mfs_itf);
1765 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1766 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1767 prefix.fp_proto);
1768 mp->table_id = ntohl (mfib->mft_table_id);
1769 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1770
1771 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1772 {
1773 mp->grp_address_len = ntohs (prefix.fp_len);
1774
1775 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1776 if (prefix.fp_len > 32)
1777 {
1778 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1779 }
1780 }
1781 else
1782 {
1783 mp->grp_address_len = ntohs (prefix.fp_len);
1784
1785 ASSERT (0);
1786 }
1787
1788 if (0 != mfs->mfs_buffer_len)
1789 {
1790 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1791
1792 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1793 }
1794 else
1795 {
1796 mp->ip_packet_len = 0;
1797 }
1798
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001799 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001800}
1801
1802static void
1803vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1804{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001805 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001806
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001807 reg = vl_api_client_index_to_registration (mp->client_index);
1808 if (!reg)
1809 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001810
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001811 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001812 ;
1813}
Dave Barachb5e8a772016-12-06 12:04:42 -05001814
Florin Coras595992c2017-11-06 17:17:08 -08001815static void
1816 vl_api_ip_container_proxy_add_del_t_handler
1817 (vl_api_ip_container_proxy_add_del_t * mp)
1818{
1819 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1820 vnet_ip_container_proxy_args_t args;
1821 int rv = 0;
1822 clib_error_t *error;
1823
1824 memset (&args, 0, sizeof (args));
1825 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1826 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1827 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1828 args.is_add = mp->is_add;
1829 if ((error = vnet_ip_container_proxy_add_del (&args)))
1830 {
1831 rv = clib_error_get_code (error);
1832 clib_error_report (error);
1833 }
1834
1835 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1836}
1837
Neale Rannsb8d44812017-11-10 06:53:54 -08001838static void
1839vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1840{
1841 int rv = 0;
1842 vl_api_ioam_enable_reply_t *rmp;
1843 clib_error_t *error;
1844
1845 /* Ignoring the profile id as currently a single profile
1846 * is supported */
1847 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1848 mp->seqno, mp->analyse);
1849 if (error)
1850 {
1851 clib_error_report (error);
1852 rv = clib_error_get_code (error);
1853 }
1854
1855 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1856}
1857
1858static void
1859vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1860{
1861 int rv = 0;
1862 vl_api_ioam_disable_reply_t *rmp;
1863 clib_error_t *error;
1864
1865 error = clear_ioam_rewrite_fn ();
1866 if (error)
1867 {
1868 clib_error_report (error);
1869 rv = clib_error_get_code (error);
1870 }
1871
1872 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1873}
1874
1875static void
1876 vl_api_ip_source_and_port_range_check_add_del_t_handler
1877 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1878{
1879 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1880 int rv = 0;
1881
1882 u8 is_ipv6 = mp->is_ipv6;
1883 u8 is_add = mp->is_add;
1884 u8 mask_length = mp->mask_length;
1885 ip4_address_t ip4_addr;
1886 ip6_address_t ip6_addr;
1887 u16 *low_ports = 0;
1888 u16 *high_ports = 0;
1889 u32 vrf_id;
1890 u16 tmp_low, tmp_high;
1891 u8 num_ranges;
1892 int i;
1893
1894 // Validate port range
1895 num_ranges = mp->number_of_ranges;
1896 if (num_ranges > 32)
1897 { // This is size of array in VPE.API
1898 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1899 goto reply;
1900 }
1901
1902 vec_reset_length (low_ports);
1903 vec_reset_length (high_ports);
1904
1905 for (i = 0; i < num_ranges; i++)
1906 {
1907 tmp_low = mp->low_ports[i];
1908 tmp_high = mp->high_ports[i];
1909 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1910 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1911 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1912 {
1913 rv = VNET_API_ERROR_INVALID_VALUE;
1914 goto reply;
1915 }
1916 vec_add1 (low_ports, tmp_low);
1917 vec_add1 (high_ports, tmp_high + 1);
1918 }
1919
1920 // Validate mask_length
1921 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1922 {
1923 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1924 goto reply;
1925 }
1926
1927 vrf_id = ntohl (mp->vrf_id);
1928
1929 if (vrf_id < 1)
1930 {
1931 rv = VNET_API_ERROR_INVALID_VALUE;
1932 goto reply;
1933 }
1934
1935
1936 if (is_ipv6)
1937 {
1938 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1939 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1940 mask_length,
1941 vrf_id,
1942 low_ports,
1943 high_ports, is_add);
1944 }
1945 else
1946 {
1947 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1948 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
1949 mask_length,
1950 vrf_id,
1951 low_ports,
1952 high_ports, is_add);
1953 }
1954
1955reply:
1956 vec_free (low_ports);
1957 vec_free (high_ports);
1958 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1959}
1960
1961static void
1962 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1963 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1964{
1965 vlib_main_t *vm = vlib_get_main ();
1966 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1967 ip4_main_t *im = &ip4_main;
1968 int rv;
1969 u32 sw_if_index;
1970 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1971 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1972 uword *p = 0;
1973 int i;
1974
1975 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1976 ntohl (mp->tcp_out_vrf_id);
1977 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1978 ntohl (mp->udp_out_vrf_id);
1979 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1980 ntohl (mp->tcp_in_vrf_id);
1981 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1982 ntohl (mp->udp_in_vrf_id);
1983
1984
1985 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1986 {
1987 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1988 {
1989 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1990
1991 if (p == 0)
1992 {
1993 rv = VNET_API_ERROR_INVALID_VALUE;
1994 goto reply;
1995 }
1996
1997 fib_index[i] = p[0];
1998 }
1999 else
2000 fib_index[i] = ~0;
2001 }
2002 sw_if_index = ntohl (mp->sw_if_index);
2003
2004 VALIDATE_SW_IF_INDEX (mp);
2005
2006 rv =
2007 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2008 mp->is_add);
2009
2010 BAD_SW_IF_INDEX_LABEL;
2011reply:
2012
2013 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2014}
2015
2016#define IP4_ARP_EVENT 3
2017#define IP6_ND_EVENT 4
2018
2019static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2020static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2021static vlib_node_registration_t ip_resolver_process_node;
2022
2023static void
2024handle_ip4_arp_event (u32 pool_index)
2025{
2026 vpe_api_main_t *vam = &vpe_api_main;
2027 vnet_main_t *vnm = vam->vnet_main;
2028 vlib_main_t *vm = vam->vlib_main;
2029 vl_api_ip4_arp_event_t *event;
2030 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002031 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002032
2033 /* Client can cancel, die, etc. */
2034 if (pool_is_free_index (vam->arp_events, pool_index))
2035 return;
2036
2037 event = pool_elt_at_index (vam->arp_events, pool_index);
2038
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002039 reg = vl_api_client_index_to_registration (event->client_index);
2040 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002041 {
2042 (void) vnet_add_del_ip4_arp_change_event
2043 (vnm, arp_change_delete_callback,
2044 event->pid, &event->address,
2045 ip_resolver_process_node.index, IP4_ARP_EVENT,
2046 ~0 /* pool index, notused */ , 0 /* is_add */ );
2047 return;
2048 }
2049
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002050 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002051 {
2052 mp = vl_msg_api_alloc (sizeof (*mp));
2053 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002054 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002055 }
2056 else
2057 {
2058 static f64 last_time;
2059 /*
2060 * Throttle syslog msgs.
2061 * It's pretty tempting to just revoke the registration...
2062 */
2063 if (vlib_time_now (vm) > last_time + 10.0)
2064 {
2065 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2066 format_ip4_address, &event->address, event->pid);
2067 last_time = vlib_time_now (vm);
2068 }
2069 }
2070}
2071
2072void
2073handle_ip6_nd_event (u32 pool_index)
2074{
2075 vpe_api_main_t *vam = &vpe_api_main;
2076 vnet_main_t *vnm = vam->vnet_main;
2077 vlib_main_t *vm = vam->vlib_main;
2078 vl_api_ip6_nd_event_t *event;
2079 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002080 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002081
2082 /* Client can cancel, die, etc. */
2083 if (pool_is_free_index (vam->nd_events, pool_index))
2084 return;
2085
2086 event = pool_elt_at_index (vam->nd_events, pool_index);
2087
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002088 reg = vl_api_client_index_to_registration (event->client_index);
2089 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002090 {
2091 (void) vnet_add_del_ip6_nd_change_event
2092 (vnm, nd_change_delete_callback,
2093 event->pid, &event->address,
2094 ip_resolver_process_node.index, IP6_ND_EVENT,
2095 ~0 /* pool index, notused */ , 0 /* is_add */ );
2096 return;
2097 }
2098
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002099 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002100 {
2101 mp = vl_msg_api_alloc (sizeof (*mp));
2102 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002103 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002104 }
2105 else
2106 {
2107 static f64 last_time;
2108 /*
2109 * Throttle syslog msgs.
2110 * It's pretty tempting to just revoke the registration...
2111 */
2112 if (vlib_time_now (vm) > last_time + 10.0)
2113 {
2114 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2115 format_ip6_address, &event->address, event->pid);
2116 last_time = vlib_time_now (vm);
2117 }
2118 }
2119}
2120
2121static uword
2122resolver_process (vlib_main_t * vm,
2123 vlib_node_runtime_t * rt, vlib_frame_t * f)
2124{
2125 volatile f64 timeout = 100.0;
2126 volatile uword *event_data = 0;
2127
2128 while (1)
2129 {
2130 vlib_process_wait_for_event_or_clock (vm, timeout);
2131
2132 uword event_type =
2133 vlib_process_get_events (vm, (uword **) & event_data);
2134
2135 int i;
2136 switch (event_type)
2137 {
2138 case IP4_ARP_EVENT:
2139 for (i = 0; i < vec_len (event_data); i++)
2140 handle_ip4_arp_event (event_data[i]);
2141 break;
2142
2143 case IP6_ND_EVENT:
2144 for (i = 0; i < vec_len (event_data); i++)
2145 handle_ip6_nd_event (event_data[i]);
2146 break;
2147
2148 case ~0: /* timeout */
2149 break;
2150 }
2151
2152 vec_reset_length (event_data);
2153 }
2154 return 0; /* or not */
2155}
2156
2157/* *INDENT-OFF* */
2158VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2159 .function = resolver_process,
2160 .type = VLIB_NODE_TYPE_PROCESS,
2161 .name = "ip-route-resolver-process",
2162};
2163/* *INDENT-ON* */
2164
2165static int
2166nd_change_data_callback (u32 pool_index, u8 * new_mac,
2167 u32 sw_if_index, ip6_address_t * address)
2168{
2169 vpe_api_main_t *am = &vpe_api_main;
2170 vl_api_ip6_nd_event_t *event;
2171
2172 if (pool_is_free_index (am->nd_events, pool_index))
2173 return 1;
2174
2175 event = pool_elt_at_index (am->nd_events, pool_index);
2176 if (eth_mac_equal (event->new_mac, new_mac) &&
2177 sw_if_index == ntohl (event->sw_if_index))
2178 {
2179 return 1;
2180 }
2181
2182 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2183 event->sw_if_index = htonl (sw_if_index);
2184 return 0;
2185}
2186
2187static int
2188arp_change_delete_callback (u32 pool_index, u8 * notused)
2189{
2190 vpe_api_main_t *am = &vpe_api_main;
2191
2192 if (pool_is_free_index (am->arp_events, pool_index))
2193 return 1;
2194
2195 pool_put_index (am->arp_events, pool_index);
2196 return 0;
2197}
2198
2199static int
2200nd_change_delete_callback (u32 pool_index, u8 * notused)
2201{
2202 vpe_api_main_t *am = &vpe_api_main;
2203
2204 if (pool_is_free_index (am->nd_events, pool_index))
2205 return 1;
2206
2207 pool_put_index (am->nd_events, pool_index);
2208 return 0;
2209}
2210
2211static vlib_node_registration_t wc_arp_process_node;
2212
2213enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002214{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002215
2216static uword
2217wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2218{
2219 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2220 * and need to be volatile - to prevent them from being optimized into
2221 * a register - which could change during suspension */
2222
2223 volatile wc_arp_report_t arp_prev = { 0 };
2224 volatile wc_nd_report_t nd_prev = { 0 };
2225 volatile f64 last_arp = vlib_time_now (vm);
2226 volatile f64 last_nd = vlib_time_now (vm);
2227
2228 while (1)
2229 {
2230 vlib_process_wait_for_event (vm);
2231 uword event_type = WC_ARP_REPORT;
2232 void *event_data = vlib_process_get_event_data (vm, &event_type);
2233
2234 f64 now = vlib_time_now (vm);
2235 int i;
2236 if (event_type == WC_ARP_REPORT)
2237 {
2238 wc_arp_report_t *arp_events = event_data;
2239 for (i = 0; i < vec_len (arp_events); i++)
2240 {
2241 /* discard dup event */
2242 if (arp_prev.ip4 == arp_events[i].ip4 &&
2243 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2244 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2245 (now - last_arp) < 10.0)
2246 {
2247 continue;
2248 }
2249 arp_prev = arp_events[i];
2250 last_arp = now;
2251 vpe_client_registration_t *reg;
2252 /* *INDENT-OFF* */
2253 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2254 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002255 vl_api_registration_t *vl_reg;
2256 vl_reg = vl_api_client_index_to_registration (reg->client_index);
Chris Luke30684ac2018-03-29 12:56:58 -07002257 ASSERT (vl_reg != NULL);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002258 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002259 {
2260 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2261 memset (event, 0, sizeof *event);
2262 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2263 event->client_index = reg->client_index;
2264 event->pid = reg->client_pid;
2265 event->mac_ip = 1;
2266 event->address = arp_events[i].ip4;
2267 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2268 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002269 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002270 }
2271 }));
2272 /* *INDENT-ON* */
2273 }
2274 }
2275 else if (event_type == WC_ND_REPORT)
2276 {
2277 wc_nd_report_t *nd_events = event_data;
2278 for (i = 0; i < vec_len (nd_events); i++)
2279 {
2280 /* discard dup event */
2281 if (ip6_address_is_equal
2282 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2283 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2284 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2285 && (now - last_nd) < 10.0)
2286 {
2287 continue;
2288 }
2289 nd_prev = nd_events[i];
2290 last_nd = now;
2291 vpe_client_registration_t *reg;
2292 /* *INDENT-OFF* */
2293 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2294 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002295 vl_api_registration_t *vl_reg;
2296 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2297 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002298 {
2299 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2300 memset (event, 0, sizeof *event);
2301 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2302 event->client_index = reg->client_index;
2303 event->pid = reg->client_pid;
2304 event->mac_ip = 1;
2305 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2306 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2307 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002308 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002309 }
2310 }));
2311 /* *INDENT-ON* */
2312 }
2313 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002314 else if (event_type == RA_REPORT)
2315 {
2316 ra_report_t *ra_events = event_data;
2317 for (i = 0; i < vec_len (ra_events); i++)
2318 {
2319 vpe_client_registration_t *reg;
2320 /* *INDENT-OFF* */
2321 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2322 ({
2323 vl_api_registration_t *vl_reg;
2324 vl_reg =
2325 vl_api_client_index_to_registration (reg->client_index);
2326 if (vl_reg && vl_api_can_send_msg (vl_reg))
2327 {
2328 u32 event_size =
2329 sizeof (vl_api_ip6_ra_event_t) +
2330 vec_len (ra_events[i].prefixes) *
2331 sizeof (vl_api_ip6_ra_prefix_info_t);
2332 vl_api_ip6_ra_event_t *event =
2333 vl_msg_api_alloc (event_size);
2334 memset (event, 0, event_size);
2335 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2336 event->client_index = reg->client_index;
2337 event->pid = reg->client_pid;
2338
2339 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2340
2341 memcpy (event->router_address, ra_events[i].router_address, 16);
2342
2343 event->current_hop_limit = ra_events[i].current_hop_limit;
2344 event->flags = ra_events[i].flags;
2345 event->router_lifetime_in_sec =
2346 clib_host_to_net_u16 (ra_events
2347 [i].router_lifetime_in_sec);
2348 event->neighbor_reachable_time_in_msec =
2349 clib_host_to_net_u32 (ra_events
2350 [i].neighbor_reachable_time_in_msec);
2351 event->time_in_msec_between_retransmitted_neighbor_solicitations
2352 =
2353 clib_host_to_net_u32 (ra_events
2354 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2355
2356 event->n_prefixes =
2357 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2358 vl_api_ip6_ra_prefix_info_t *prefix =
2359 (typeof (prefix)) event->prefixes;
2360 u32 j;
2361 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2362 {
2363 ra_report_prefix_info_t *info =
2364 &ra_events[i].prefixes[j];
2365 memcpy (prefix->dst_address, info->dst_address.as_u8,
2366 16);
2367 prefix->dst_address_length = info->dst_address_length;
2368 prefix->flags = info->flags;
2369 prefix->valid_time =
2370 clib_host_to_net_u32 (info->valid_time);
2371 prefix->preferred_time =
2372 clib_host_to_net_u32 (info->preferred_time);
2373 prefix++;
2374 }
2375
2376 vl_api_send_msg (vl_reg, (u8 *) event);
2377 }
2378 }));
2379 /* *INDENT-ON* */
2380 }
2381 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002382 vlib_process_put_event_data (vm, event_data);
2383 }
2384
2385 return 0;
2386}
2387
2388/* *INDENT-OFF* */
2389VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2390 .function = wc_arp_process,
2391 .type = VLIB_NODE_TYPE_PROCESS,
2392 .name = "wildcard-ip4-arp-publisher-process",
2393};
2394/* *INDENT-ON* */
2395
2396static int
2397arp_change_data_callback (u32 pool_index, u8 * new_mac,
2398 u32 sw_if_index, u32 address)
2399{
2400 vpe_api_main_t *am = &vpe_api_main;
2401 vl_api_ip4_arp_event_t *event;
2402
2403 if (pool_is_free_index (am->arp_events, pool_index))
2404 return 1;
2405
2406 event = pool_elt_at_index (am->arp_events, pool_index);
2407 if (eth_mac_equal (event->new_mac, new_mac) &&
2408 sw_if_index == ntohl (event->sw_if_index))
2409 {
2410 return 1;
2411 }
2412
2413 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2414 event->sw_if_index = htonl (sw_if_index);
2415 return 0;
2416}
2417
2418static void
2419vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2420{
2421 vpe_api_main_t *am = &vpe_api_main;
2422 vnet_main_t *vnm = vnet_get_main ();
2423 vl_api_want_ip4_arp_events_reply_t *rmp;
2424 int rv = 0;
2425
2426 if (mp->address == 0)
2427 {
2428 uword *p =
2429 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2430 vpe_client_registration_t *rp;
2431 if (p)
2432 {
2433 if (mp->enable_disable)
2434 {
2435 clib_warning ("pid %d: already enabled...", mp->pid);
2436 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2437 goto reply;
2438 }
2439 else
2440 {
2441 rp =
2442 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2443 pool_put (am->wc_ip4_arp_events_registrations, rp);
2444 hash_unset (am->wc_ip4_arp_events_registration_hash,
2445 mp->client_index);
2446 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002447 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002448 goto reply;
2449 }
2450 }
2451 if (mp->enable_disable == 0)
2452 {
2453 clib_warning ("pid %d: already disabled...", mp->pid);
2454 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2455 goto reply;
2456 }
2457 pool_get (am->wc_ip4_arp_events_registrations, rp);
2458 rp->client_index = mp->client_index;
2459 rp->client_pid = mp->pid;
2460 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2461 rp - am->wc_ip4_arp_events_registrations);
2462 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2463 goto reply;
2464 }
2465
2466 if (mp->enable_disable)
2467 {
2468 vl_api_ip4_arp_event_t *event;
2469 pool_get (am->arp_events, event);
2470 rv = vnet_add_del_ip4_arp_change_event
2471 (vnm, arp_change_data_callback,
2472 mp->pid, &mp->address /* addr, in net byte order */ ,
2473 ip_resolver_process_node.index,
2474 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2475
2476 if (rv)
2477 {
2478 pool_put (am->arp_events, event);
2479 goto reply;
2480 }
2481 memset (event, 0, sizeof (*event));
2482
2483 /* Python API expects events to have no context */
2484 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2485 event->client_index = mp->client_index;
2486 event->address = mp->address;
2487 event->pid = mp->pid;
2488 if (mp->address == 0)
2489 event->mac_ip = 1;
2490 }
2491 else
2492 {
2493 rv = vnet_add_del_ip4_arp_change_event
2494 (vnm, arp_change_delete_callback,
2495 mp->pid, &mp->address /* addr, in net byte order */ ,
2496 ip_resolver_process_node.index,
2497 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2498 }
2499reply:
2500 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2501}
2502
2503static void
2504vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2505{
2506 vpe_api_main_t *am = &vpe_api_main;
2507 vnet_main_t *vnm = vnet_get_main ();
2508 vl_api_want_ip6_nd_events_reply_t *rmp;
2509 int rv = 0;
2510
2511 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2512 {
2513 uword *p =
2514 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2515 vpe_client_registration_t *rp;
2516 if (p)
2517 {
2518 if (mp->enable_disable)
2519 {
2520 clib_warning ("pid %d: already enabled...", mp->pid);
2521 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2522 goto reply;
2523 }
2524 else
2525 {
2526 rp =
2527 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2528 pool_put (am->wc_ip6_nd_events_registrations, rp);
2529 hash_unset (am->wc_ip6_nd_events_registration_hash,
2530 mp->client_index);
2531 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002532 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002533 goto reply;
2534 }
2535 }
2536 if (mp->enable_disable == 0)
2537 {
2538 clib_warning ("pid %d: already disabled...", mp->pid);
2539 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2540 goto reply;
2541 }
2542 pool_get (am->wc_ip6_nd_events_registrations, rp);
2543 rp->client_index = mp->client_index;
2544 rp->client_pid = mp->pid;
2545 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2546 rp - am->wc_ip6_nd_events_registrations);
2547 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2548 goto reply;
2549 }
2550
2551 if (mp->enable_disable)
2552 {
2553 vl_api_ip6_nd_event_t *event;
2554 pool_get (am->nd_events, event);
2555
2556 rv = vnet_add_del_ip6_nd_change_event
2557 (vnm, nd_change_data_callback,
2558 mp->pid, mp->address /* addr, in net byte order */ ,
2559 ip_resolver_process_node.index,
2560 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2561
2562 if (rv)
2563 {
2564 pool_put (am->nd_events, event);
2565 goto reply;
2566 }
2567 memset (event, 0, sizeof (*event));
2568
2569 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2570 event->client_index = mp->client_index;
2571 clib_memcpy (event->address, mp->address, sizeof event->address);
2572 event->pid = mp->pid;
2573 }
2574 else
2575 {
2576 rv = vnet_add_del_ip6_nd_change_event
2577 (vnm, nd_change_delete_callback,
2578 mp->pid, mp->address /* addr, in net byte order */ ,
2579 ip_resolver_process_node.index,
2580 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2581 }
2582reply:
2583 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2584}
2585
2586static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002587vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2588{
2589 vpe_api_main_t *am = &vpe_api_main;
2590 vl_api_want_ip6_ra_events_reply_t *rmp;
2591 int rv = 0;
2592
2593 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2594 vpe_client_registration_t *rp;
2595 if (p)
2596 {
2597 if (mp->enable_disable)
2598 {
2599 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2600 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2601 goto reply;
2602 }
2603 else
2604 {
2605 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2606 pool_put (am->ip6_ra_events_registrations, rp);
2607 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
2608 if (pool_elts (am->ip6_ra_events_registrations) == 0)
2609 ra_set_publisher_node (~0, REPORT_MAX);
2610 goto reply;
2611 }
2612 }
2613 if (mp->enable_disable == 0)
2614 {
2615 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2616 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2617 goto reply;
2618 }
2619 pool_get (am->ip6_ra_events_registrations, rp);
2620 rp->client_index = mp->client_index;
2621 rp->client_pid = ntohl (mp->pid);
2622 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2623 rp - am->ip6_ra_events_registrations);
2624 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
2625
2626reply:
2627 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2628}
2629
2630static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002631vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2632{
2633 vl_api_proxy_arp_add_del_reply_t *rmp;
2634 u32 fib_index;
2635 int rv;
2636 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002637 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2638 ip4_address_t * hi_addr,
2639 u32 fib_index, int is_del);
2640 uword *p;
2641
Ole Troan3288ed72017-12-06 17:00:05 +01002642 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002643
2644 p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2645
2646 if (!p)
2647 {
2648 rv = VNET_API_ERROR_NO_SUCH_FIB;
2649 goto out;
2650 }
2651
2652 fib_index = p[0];
2653
2654 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2655 (ip4_address_t *) mp->hi_address,
2656 fib_index, mp->is_add == 0);
2657
2658out:
Ole Troan3288ed72017-12-06 17:00:05 +01002659 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002660 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2661}
2662
2663static void
2664 vl_api_proxy_arp_intfc_enable_disable_t_handler
2665 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2666{
2667 int rv = 0;
2668 vnet_main_t *vnm = vnet_get_main ();
2669 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2670
2671 VALIDATE_SW_IF_INDEX (mp);
2672
2673 vnet_sw_interface_t *si =
2674 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2675
2676 ASSERT (si);
2677
2678 if (mp->enable_disable)
2679 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2680 else
2681 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2682
2683 BAD_SW_IF_INDEX_LABEL;
2684
2685 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2686}
2687
John Loc7b43042018-04-13 16:46:22 -04002688static void
2689vl_api_ip_probe_neighbor_t_handler (vl_api_ip_probe_neighbor_t * mp)
2690{
2691 int rv = 0;
2692 vlib_main_t *vm = vlib_get_main ();
2693 vl_api_ip_probe_neighbor_reply_t *rmp;
2694 clib_error_t *error;
2695
2696 VALIDATE_SW_IF_INDEX (mp);
2697
2698 u32 sw_if_index = ntohl (mp->sw_if_index);
2699
2700 if (mp->is_ipv6)
2701 error = ip6_probe_neighbor (vm, (ip6_address_t *) mp->dst_address,
2702 sw_if_index);
2703 else
2704 error = ip4_probe_neighbor (vm, (ip4_address_t *) mp->dst_address,
2705 sw_if_index);
2706
2707 if (error)
2708 {
2709 clib_error_report (error);
2710 rv = clib_error_get_code (error);
2711 }
2712
2713 BAD_SW_IF_INDEX_LABEL;
2714
2715 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2716}
2717
Neale Rannsb8d44812017-11-10 06:53:54 -08002718static int
2719ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2720{
2721 vnet_main_t *vnm = vnet_get_main ();
2722 vnet_interface_main_t *im = &vnm->interface_main;
2723 ip4_main_t *im4 = &ip4_main;
2724 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08002725 fib_table_t *fib_table;
2726 ip4_fib_t *fib;
2727 u32 sw_if_index;
2728 int i;
2729 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2730 u32 target_fib_id = ntohl (mp->vrf_id);
2731
Ole Troan3288ed72017-12-06 17:00:05 +01002732 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002733
2734 /* *INDENT-OFF* */
2735 pool_foreach (fib_table, im4->fibs,
2736 ({
2737 vnet_sw_interface_t * si;
2738
2739 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
2740
2741 if (fib->table_id != target_fib_id)
2742 continue;
2743
2744 /* remove any mpls encap/decap labels */
2745 mpls_fib_reset_labels (fib->table_id);
2746
2747 /* remove any proxy arps in this fib */
2748 vnet_proxy_arp_fib_reset (fib->table_id);
2749
2750 /* Set the flow hash for this fib to the default */
2751 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2752
2753 vec_reset_length (sw_if_indices_to_shut);
2754
2755 /* Shut down interfaces in this FIB / clean out intfc routes */
2756 pool_foreach (si, im->sw_interfaces,
2757 ({
2758 u32 sw_if_index = si->sw_if_index;
2759
2760 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2761 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2762 fib->index))
2763 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2764 }));
2765
2766 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2767 sw_if_index = sw_if_indices_to_shut[i];
2768
2769 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2770 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2771 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2772 }
2773
2774 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2775
2776 rv = 0;
2777 break;
2778 })); /* pool_foreach (fib) */
2779 /* *INDENT-ON* */
2780
Ole Troan3288ed72017-12-06 17:00:05 +01002781 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002782 return rv;
2783}
2784
2785static int
2786ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2787{
2788 vnet_main_t *vnm = vnet_get_main ();
2789 vnet_interface_main_t *im = &vnm->interface_main;
2790 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002791 static u32 *sw_if_indices_to_shut;
2792 fib_table_t *fib_table;
2793 ip6_fib_t *fib;
2794 u32 sw_if_index;
2795 int i;
2796 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2797 u32 target_fib_id = ntohl (mp->vrf_id);
2798
Ole Troan3288ed72017-12-06 17:00:05 +01002799 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002800
2801 /* *INDENT-OFF* */
2802 pool_foreach (fib_table, im6->fibs,
2803 ({
2804 vnet_sw_interface_t * si;
2805
2806 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
2807
2808 if (fib->table_id != target_fib_id)
2809 continue;
2810
2811 vec_reset_length (sw_if_indices_to_shut);
2812
2813 /* Set the flow hash for this fib to the default */
2814 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2815
2816 /* Shut down interfaces in this FIB / clean out intfc routes */
2817 pool_foreach (si, im->sw_interfaces,
2818 ({
2819 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2820 fib->index)
2821 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2822 }));
2823
2824 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2825 sw_if_index = sw_if_indices_to_shut[i];
2826
2827 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2828 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2829 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2830 }
2831
2832 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2833
2834 rv = 0;
2835 break;
2836 })); /* pool_foreach (fib) */
2837 /* *INDENT-ON* */
2838
Ole Troan3288ed72017-12-06 17:00:05 +01002839 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002840 return rv;
2841}
2842
2843static void
2844vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2845{
2846 int rv;
2847 vl_api_reset_fib_reply_t *rmp;
2848
2849 if (mp->is_ipv6)
2850 rv = ip6_reset_fib_t_handler (mp);
2851 else
2852 rv = ip4_reset_fib_t_handler (mp);
2853
2854 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2855}
2856
2857static void
2858vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
2859{
2860 int rv;
2861 vl_api_set_arp_neighbor_limit_reply_t *rmp;
2862 vnet_main_t *vnm = vnet_get_main ();
2863 clib_error_t *error;
2864
2865 vnm->api_errno = 0;
2866
2867 if (mp->is_ipv6)
2868 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
2869 else
2870 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
2871
2872 if (error)
2873 {
2874 clib_error_report (error);
2875 rv = VNET_API_ERROR_UNSPECIFIED;
2876 }
2877 else
2878 {
2879 rv = vnm->api_errno;
2880 }
2881
2882 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
2883}
2884
Klement Sekera75e7d132017-09-20 08:26:30 +02002885void
2886vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
2887{
2888 vl_api_ip_reassembly_set_reply_t *rmp;
2889 int rv = 0;
2890 if (mp->is_ip6)
2891 {
2892 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
2893 clib_net_to_host_u32 (mp->max_reassemblies),
2894 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
2895 }
2896 else
2897 {
2898 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
2899 clib_net_to_host_u32 (mp->max_reassemblies),
2900 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
2901 }
2902
2903 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
2904}
2905
2906void
2907vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
2908{
2909 unix_shared_memory_queue_t *q;
2910
2911 q = vl_api_client_index_to_input_queue (mp->client_index);
2912
2913 if (q == 0)
2914 return;
2915
2916 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
2917 memset (rmp, 0, sizeof (*rmp));
2918 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
2919 rmp->context = mp->context;
2920 rmp->retval = 0;
2921 if (mp->is_ip6)
2922 {
2923 rmp->is_ip6 = 1;
2924 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
2925 &rmp->expire_walk_interval_ms);
2926 }
2927 else
2928 {
2929 rmp->is_ip6 = 0;
2930 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
2931 &rmp->expire_walk_interval_ms);
2932 }
2933 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
2934 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
2935 rmp->expire_walk_interval_ms =
2936 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
2937 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2938}
2939
Klement Sekera4c533132018-02-22 11:41:12 +01002940void
2941 vl_api_ip_reassembly_enable_disable_t_handler
2942 (vl_api_ip_reassembly_enable_disable_t * mp)
2943{
2944 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
2945 int rv = 0;
2946 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
2947 mp->enable_ip4);
2948 if (0 == rv)
2949 {
2950 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
2951 mp->enable_ip6);
2952 }
2953
2954 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
2955}
2956
Dave Barachb5e8a772016-12-06 12:04:42 -05002957#define vl_msg_name_crc_list
2958#include <vnet/ip/ip.api.h>
2959#undef vl_msg_name_crc_list
2960
2961static void
2962setup_message_id_table (api_main_t * am)
2963{
2964#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2965 foreach_vl_msg_name_crc_ip;
2966#undef _
2967}
2968
2969static clib_error_t *
2970ip_api_hookup (vlib_main_t * vm)
2971{
2972 api_main_t *am = &api_main;
2973
2974#define _(N,n) \
2975 vl_msg_api_set_handlers(VL_API_##N, #n, \
2976 vl_api_##n##_t_handler, \
2977 vl_noop_handler, \
2978 vl_api_##n##_t_endian, \
2979 vl_api_##n##_t_print, \
2980 sizeof(vl_api_##n##_t), 1);
2981 foreach_ip_api_msg;
2982#undef _
2983
2984 /*
2985 * Set up the (msg_name, crc, message-id) table
2986 */
2987 setup_message_id_table (am);
2988
2989 return 0;
2990}
2991
2992VLIB_API_INIT_FUNCTION (ip_api_hookup);
2993
2994/*
2995 * fd.io coding-style-patch-verification: ON
2996 *
2997 * Local Variables:
2998 * eval: (c-set-style "gnu")
2999 * End:
3000 */