blob: a8dbb3ae36c15b7899a5a8d153ede209de3fee38 [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) \
79_(WANT_IP4_ARP_EVENTS, want_ip4_arp_events) \
80_(WANT_IP6_ND_EVENTS, want_ip6_nd_events) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010081_(WANT_IP6_RA_EVENTS, want_ip6_ra_events) \
Neale Rannsb8d44812017-11-10 06:53:54 -080082_(PROXY_ARP_ADD_DEL, proxy_arp_add_del) \
83_(PROXY_ARP_INTFC_ENABLE_DISABLE, proxy_arp_intfc_enable_disable) \
84_(RESET_FIB, reset_fib) \
Dave Barachb5e8a772016-12-06 12:04:42 -050085_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
Neale Ranns28ab9cc2017-08-14 07:18:42 -070086_(IP_TABLE_ADD_DEL, ip_table_add_del) \
Neale Rannsd91c1db2017-07-31 02:30:50 -070087_(IP_PUNT_POLICE, ip_punt_police) \
88_(IP_PUNT_REDIRECT, ip_punt_redirect) \
Dave Barachb5e8a772016-12-06 12:04:42 -050089_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
90_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
91_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
Neale Ranns3f844d02017-02-18 00:03:54 -080092_(IP6ND_PROXY_ADD_DEL, ip6nd_proxy_add_del) \
93_(IP6ND_PROXY_DUMP, ip6nd_proxy_dump) \
Juraj Sloboda4b9669d2018-01-15 10:39:21 +010094_(IP6ND_SEND_ROUTER_SOLICITATION, ip6nd_send_router_solicitation) \
Dave Barachb5e8a772016-12-06 12:04:42 -050095_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
96_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
Florin Coras595992c2017-11-06 17:17:08 -080097 sw_interface_ip6_set_link_local_address) \
Neale Rannsb8d44812017-11-10 06:53:54 -080098_(IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del) \
99_(IOAM_ENABLE, ioam_enable) \
100_(IOAM_DISABLE, ioam_disable) \
101_(IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL, \
102 ip_source_and_port_range_check_add_del) \
103_(IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL, \
Klement Sekera75e7d132017-09-20 08:26:30 +0200104 ip_source_and_port_range_check_interface_add_del) \
105_(IP_REASSEMBLY_SET, ip_reassembly_set) \
Klement Sekera4c533132018-02-22 11:41:12 +0100106_(IP_REASSEMBLY_GET, ip_reassembly_get) \
107_(IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)
Dave Barachb5e8a772016-12-06 12:04:42 -0500108
109extern void stats_dslock_with_hint (int hint, int tag);
110extern void stats_dsunlock (void);
111
112static void
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600113send_ip_neighbor_details (u32 sw_if_index,
114 u8 is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500115 u8 is_static,
116 u8 * mac_address,
Florin Coras6c4dae22018-01-09 06:39:23 -0800117 u8 * ip_address, vl_api_registration_t * reg,
118 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500119{
120 vl_api_ip_neighbor_details_t *mp;
121
122 mp = vl_msg_api_alloc (sizeof (*mp));
123 memset (mp, 0, sizeof (*mp));
124 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
125 mp->context = context;
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600126 mp->sw_if_index = htonl (sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500127 mp->is_ipv6 = is_ipv6;
128 mp->is_static = is_static;
129 memcpy (mp->mac_address, mac_address, 6);
130 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
131
Florin Coras6c4dae22018-01-09 06:39:23 -0800132 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500133}
134
135static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500136vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
137{
Florin Coras6c4dae22018-01-09 06:39:23 -0800138 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500139
Florin Coras6c4dae22018-01-09 06:39:23 -0800140 reg = vl_api_client_index_to_registration (mp->client_index);
141 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500142 return;
143
144 u32 sw_if_index = ntohl (mp->sw_if_index);
145
146 if (mp->is_ipv6)
147 {
148 ip6_neighbor_t *n, *ns;
149
150 ns = ip6_neighbors_entries (sw_if_index);
151 /* *INDENT-OFF* */
152 vec_foreach (n, ns)
153 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500154 send_ip_neighbor_details
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600155 (sw_if_index, mp->is_ipv6,
156 ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
Dave Barachd7cb1b52016-12-09 09:52:16 -0500157 (u8 *) n->link_layer_address,
158 (u8 *) & (n->key.ip6_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800159 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500160 }
161 /* *INDENT-ON* */
162 vec_free (ns);
163 }
164 else
165 {
166 ethernet_arp_ip4_entry_t *n, *ns;
167
168 ns = ip4_neighbor_entries (sw_if_index);
169 /* *INDENT-OFF* */
170 vec_foreach (n, ns)
171 {
Jon Loeligeraf8dfbf2017-11-08 13:07:39 -0600172 send_ip_neighbor_details (sw_if_index, mp->is_ipv6,
Dave Barachb5e8a772016-12-06 12:04:42 -0500173 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
174 (u8*) n->ethernet_address,
175 (u8*) & (n->ip4_address.as_u8),
Florin Coras6c4dae22018-01-09 06:39:23 -0800176 reg, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500177 }
178 /* *INDENT-ON* */
179 vec_free (ns);
180 }
181}
182
Dave Barachb5e8a772016-12-06 12:04:42 -0500183static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500184send_ip_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800185 vl_api_registration_t * reg,
Neale Ranns2297af02017-09-12 09:45:04 -0700186 const fib_table_t * table,
187 const fib_prefix_t * pfx,
Dave Barachb5e8a772016-12-06 12:04:42 -0500188 fib_route_path_encode_t * api_rpaths, u32 context)
189{
190 vl_api_ip_fib_details_t *mp;
191 fib_route_path_encode_t *api_rpath;
192 vl_api_fib_path_t *fp;
193 int path_count;
194
195 path_count = vec_len (api_rpaths);
196 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
197 if (!mp)
198 return;
199 memset (mp, 0, sizeof (*mp));
200 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
201 mp->context = context;
202
Neale Ranns2297af02017-09-12 09:45:04 -0700203 mp->table_id = htonl (table->ft_table_id);
204 memcpy (mp->table_name, table->ft_desc,
205 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500206 mp->address_length = pfx->fp_len;
207 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
208
209 mp->count = htonl (path_count);
210 fp = mp->path;
211 vec_foreach (api_rpath, api_rpaths)
212 {
Neale Ranns81458422018-03-12 06:59:36 -0700213 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500214 fp++;
215 }
216
Florin Coras6c4dae22018-01-09 06:39:23 -0800217 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500218}
219
Neale Rannsa3af3372017-03-28 03:49:52 -0700220typedef struct vl_api_ip_fib_dump_walk_ctx_t_
221{
222 fib_node_index_t *feis;
223} vl_api_ip_fib_dump_walk_ctx_t;
224
Neale Ranns89541992017-04-06 04:41:02 -0700225static fib_table_walk_rc_t
Neale Rannsa3af3372017-03-28 03:49:52 -0700226vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
227{
228 vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
229
230 vec_add1 (ctx->feis, fei);
231
Neale Ranns89541992017-04-06 04:41:02 -0700232 return (FIB_TABLE_WALK_CONTINUE);
Neale Rannsa3af3372017-03-28 03:49:52 -0700233}
234
Dave Barachb5e8a772016-12-06 12:04:42 -0500235static void
236vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
237{
238 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800239 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500240 ip4_main_t *im = &ip4_main;
241 fib_table_t *fib_table;
Neale Rannsa3af3372017-03-28 03:49:52 -0700242 fib_node_index_t *lfeip;
Dave Barachb5e8a772016-12-06 12:04:42 -0500243 fib_prefix_t pfx;
244 u32 fib_index;
245 fib_route_path_encode_t *api_rpaths;
Neale Rannsa3af3372017-03-28 03:49:52 -0700246 vl_api_ip_fib_dump_walk_ctx_t ctx = {
247 .feis = NULL,
248 };
Dave Barachb5e8a772016-12-06 12:04:42 -0500249
Florin Coras6c4dae22018-01-09 06:39:23 -0800250 reg = vl_api_client_index_to_registration (mp->client_index);
251 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500252 return;
253
254 /* *INDENT-OFF* */
255 pool_foreach (fib_table, im->fibs,
256 ({
Neale Rannsa3af3372017-03-28 03:49:52 -0700257 fib_table_walk(fib_table->ft_index,
258 FIB_PROTOCOL_IP4,
259 vl_api_ip_fib_dump_walk,
260 &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500261 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500262 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500263
Neale Rannsa3af3372017-03-28 03:49:52 -0700264 vec_sort_with_function (ctx.feis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500265
Neale Rannsa3af3372017-03-28 03:49:52 -0700266 vec_foreach (lfeip, ctx.feis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500267 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500268 fib_entry_get_prefix (*lfeip, &pfx);
269 fib_index = fib_entry_get_fib_index (*lfeip);
270 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500271 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500272 fib_entry_encode (*lfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800273 send_ip_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500274 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500275 }
276
Neale Rannsa3af3372017-03-28 03:49:52 -0700277 vec_free (ctx.feis);
Dave Barachb5e8a772016-12-06 12:04:42 -0500278}
279
280static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500281send_ip6_fib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800282 vl_api_registration_t * reg,
Neale Rannsa161a6d2017-11-14 08:10:41 -0800283 const fib_table_t * table,
284 const fib_prefix_t * pfx,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500285 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500286{
287 vl_api_ip6_fib_details_t *mp;
288 fib_route_path_encode_t *api_rpath;
289 vl_api_fib_path_t *fp;
290 int path_count;
291
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500293 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
294 if (!mp)
295 return;
296 memset (mp, 0, sizeof (*mp));
297 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
298 mp->context = context;
299
Neale Rannsa161a6d2017-11-14 08:10:41 -0800300 mp->table_id = htonl (table->ft_table_id);
Dave Barachb5e8a772016-12-06 12:04:42 -0500301 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500302 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Neale Rannsa161a6d2017-11-14 08:10:41 -0800303 memcpy (mp->table_name, table->ft_desc,
304 clib_min (vec_len (table->ft_desc), sizeof (mp->table_name)));
Dave Barachb5e8a772016-12-06 12:04:42 -0500305
306 mp->count = htonl (path_count);
307 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500308 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500309 {
Neale Ranns81458422018-03-12 06:59:36 -0700310 fib_api_path_encode (api_rpath, fp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500311 fp++;
312 }
313
Florin Coras6c4dae22018-01-09 06:39:23 -0800314 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -0500315}
316
Dave Barachd7cb1b52016-12-09 09:52:16 -0500317typedef struct apt_ip6_fib_show_ctx_t_
318{
319 u32 fib_index;
320 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500321} api_ip6_fib_show_ctx_t;
322
323static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500324api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500325{
326 api_ip6_fib_show_ctx_t *ctx = arg;
327
328 if ((kvp->key[2] >> 32) == ctx->fib_index)
329 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500330 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500331 }
332}
333
334static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800335api_ip6_fib_table_get_all (vl_api_registration_t * reg,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336 vl_api_ip6_fib_dump_t * mp,
337 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500338{
339 vpe_api_main_t *am = &vpe_api_main;
340 ip6_main_t *im6 = &ip6_main;
Dave Barachb5e8a772016-12-06 12:04:42 -0500341 fib_node_index_t *fib_entry_index;
342 api_ip6_fib_show_ctx_t ctx = {
Neale Rannsa3af3372017-03-28 03:49:52 -0700343 .fib_index = fib_table->ft_index,
344 .entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500345 };
346 fib_route_path_encode_t *api_rpaths;
347 fib_prefix_t pfx;
348
Dave Barachd7cb1b52016-12-09 09:52:16 -0500349 BV (clib_bihash_foreach_key_value_pair)
350 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
351 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500352
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500354
Dave Barachd7cb1b52016-12-09 09:52:16 -0500355 vec_foreach (fib_entry_index, ctx.entries)
356 {
357 fib_entry_get_prefix (*fib_entry_index, &pfx);
358 api_rpaths = NULL;
359 fib_entry_encode (*fib_entry_index, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800360 send_ip6_fib_details (am, reg, fib_table, &pfx, api_rpaths, mp->context);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361 vec_free (api_rpaths);
362 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500363
Dave Barachd7cb1b52016-12-09 09:52:16 -0500364 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500365}
366
367static void
368vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
369{
Florin Coras6c4dae22018-01-09 06:39:23 -0800370 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -0500371 ip6_main_t *im6 = &ip6_main;
372 fib_table_t *fib_table;
373
Florin Coras6c4dae22018-01-09 06:39:23 -0800374 reg = vl_api_client_index_to_registration (mp->client_index);
375 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500376 return;
377
378 /* *INDENT-OFF* */
379 pool_foreach (fib_table, im6->fibs,
380 ({
Neale Ranns81458422018-03-12 06:59:36 -0700381 /* don't send link locals */
382 if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
383 continue;
384
Florin Coras6c4dae22018-01-09 06:39:23 -0800385 api_ip6_fib_table_get_all(reg, mp, fib_table);
Dave Barachb5e8a772016-12-06 12:04:42 -0500386 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500388}
389
390static void
Florin Coras6c4dae22018-01-09 06:39:23 -0800391send_ip_mfib_details (vl_api_registration_t * reg,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800392 u32 context, u32 table_id, fib_node_index_t mfei)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800393{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800394 fib_route_path_encode_t *api_rpath, *api_rpaths = NULL;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800395 vl_api_ip_mfib_details_t *mp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800396 mfib_entry_t *mfib_entry;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800397 vl_api_fib_path_t *fp;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800398 mfib_prefix_t pfx;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800399 int path_count;
400
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800401 mfib_entry = mfib_entry_get (mfei);
402 mfib_entry_get_prefix (mfei, &pfx);
403 mfib_entry_encode (mfei, &api_rpaths);
404
Neale Ranns5a8123b2017-01-26 01:18:23 -0800405 path_count = vec_len (api_rpaths);
406 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
407 if (!mp)
408 return;
409 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700410 mp->_vl_msg_id = ntohs (VL_API_IP_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800411 mp->context = context;
412
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800413 mp->rpf_id = mfib_entry->mfe_rpf_id;
414 mp->entry_flags = mfib_entry->mfe_flags;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800415 mp->table_id = htonl (table_id);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800416 mp->address_length = pfx.fp_len;
417 memcpy (mp->grp_address, &pfx.fp_grp_addr.ip4,
418 sizeof (pfx.fp_grp_addr.ip4));
419 memcpy (mp->src_address, &pfx.fp_src_addr.ip4,
420 sizeof (pfx.fp_src_addr.ip4));
Neale Ranns5a8123b2017-01-26 01:18:23 -0800421
422 mp->count = htonl (path_count);
423 fp = mp->path;
424 vec_foreach (api_rpath, api_rpaths)
425 {
Neale Ranns81458422018-03-12 06:59:36 -0700426 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800427 fp++;
428 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800429 vec_free (api_rpaths);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800430
Florin Coras6c4dae22018-01-09 06:39:23 -0800431 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800432}
433
434typedef struct vl_api_ip_mfib_dump_ctc_t_
435{
436 fib_node_index_t *entries;
437} vl_api_ip_mfib_dump_ctc_t;
438
439static int
440vl_api_ip_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
441{
442 vl_api_ip_mfib_dump_ctc_t *ctx = arg;
443
444 vec_add1 (ctx->entries, fei);
445
446 return (0);
447}
448
449static void
450vl_api_ip_mfib_dump_t_handler (vl_api_ip_mfib_dump_t * mp)
451{
Florin Coras6c4dae22018-01-09 06:39:23 -0800452 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800453 ip4_main_t *im = &ip4_main;
454 mfib_table_t *mfib_table;
455 fib_node_index_t *mfeip;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800456 vl_api_ip_mfib_dump_ctc_t ctx = {
457 .entries = NULL,
458 };
459
Florin Coras6c4dae22018-01-09 06:39:23 -0800460 reg = vl_api_client_index_to_registration (mp->client_index);
461 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800462 return;
463
Neale Ranns5a8123b2017-01-26 01:18:23 -0800464 /* *INDENT-OFF* */
465 pool_foreach (mfib_table, im->mfibs,
466 ({
467 ip4_mfib_table_walk(&mfib_table->v4,
468 vl_api_ip_mfib_table_dump_walk,
469 &ctx);
470
471 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
472
473 vec_foreach (mfeip, ctx.entries)
474 {
Florin Coras6c4dae22018-01-09 06:39:23 -0800475 send_ip_mfib_details (reg, mp->context,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800476 mfib_table->mft_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800477 *mfeip);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800478 }
Neale Ranns5a8123b2017-01-26 01:18:23 -0800479 vec_reset_length (ctx.entries);
480
481 }));
482 /* *INDENT-ON* */
483
484 vec_free (ctx.entries);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800485}
486
487static void
Neale Ranns5a8123b2017-01-26 01:18:23 -0800488send_ip6_mfib_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -0800489 vl_api_registration_t * reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800490 u32 table_id,
491 mfib_prefix_t * pfx,
492 fib_route_path_encode_t * api_rpaths, u32 context)
493{
494 vl_api_ip6_mfib_details_t *mp;
495 fib_route_path_encode_t *api_rpath;
496 vl_api_fib_path_t *fp;
497 int path_count;
498
499 path_count = vec_len (api_rpaths);
500 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
501 if (!mp)
502 return;
503 memset (mp, 0, sizeof (*mp));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700504 mp->_vl_msg_id = ntohs (VL_API_IP6_MFIB_DETAILS);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800505 mp->context = context;
506
507 mp->table_id = htonl (table_id);
508 mp->address_length = pfx->fp_len;
509 memcpy (mp->grp_address, &pfx->fp_grp_addr.ip6,
510 sizeof (pfx->fp_grp_addr.ip6));
511 memcpy (mp->src_address, &pfx->fp_src_addr.ip6,
512 sizeof (pfx->fp_src_addr.ip6));
513
514 mp->count = htonl (path_count);
515 fp = mp->path;
516 vec_foreach (api_rpath, api_rpaths)
517 {
Neale Ranns81458422018-03-12 06:59:36 -0700518 fib_api_path_encode (api_rpath, fp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800519 fp++;
520 }
521
Florin Coras6c4dae22018-01-09 06:39:23 -0800522 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns5a8123b2017-01-26 01:18:23 -0800523}
524
525typedef struct vl_api_ip6_mfib_dump_ctc_t_
526{
527 fib_node_index_t *entries;
528} vl_api_ip6_mfib_dump_ctc_t;
529
530static int
531vl_api_ip6_mfib_table_dump_walk (fib_node_index_t fei, void *arg)
532{
533 vl_api_ip6_mfib_dump_ctc_t *ctx = arg;
534
535 vec_add1 (ctx->entries, fei);
536
537 return (0);
538}
539
540static void
541vl_api_ip6_mfib_dump_t_handler (vl_api_ip6_mfib_dump_t * mp)
542{
543 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -0800544 vl_api_registration_t *reg;
Neale Ranns5a8123b2017-01-26 01:18:23 -0800545 ip6_main_t *im = &ip6_main;
546 mfib_table_t *mfib_table;
547 fib_node_index_t *mfeip;
548 mfib_prefix_t pfx;
549 fib_route_path_encode_t *api_rpaths = NULL;
550 vl_api_ip6_mfib_dump_ctc_t ctx = {
551 .entries = NULL,
552 };
553
Florin Coras6c4dae22018-01-09 06:39:23 -0800554 reg = vl_api_client_index_to_registration (mp->client_index);
555 if (!reg)
Neale Ranns5a8123b2017-01-26 01:18:23 -0800556 return;
557
558
559 /* *INDENT-OFF* */
560 pool_foreach (mfib_table, im->mfibs,
561 ({
562 ip6_mfib_table_walk(&mfib_table->v6,
563 vl_api_ip6_mfib_table_dump_walk,
564 &ctx);
565
566 vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
567
568 vec_foreach(mfeip, ctx.entries)
569 {
570 mfib_entry_get_prefix (*mfeip, &pfx);
571 mfib_entry_encode (*mfeip, &api_rpaths);
Florin Coras6c4dae22018-01-09 06:39:23 -0800572 send_ip6_mfib_details (am, reg,
Neale Ranns5a8123b2017-01-26 01:18:23 -0800573 mfib_table->mft_table_id,
574 &pfx, api_rpaths,
575 mp->context);
576 }
577 vec_reset_length (api_rpaths);
578 vec_reset_length (ctx.entries);
579
580 }));
581 /* *INDENT-ON* */
582
583 vec_free (ctx.entries);
584 vec_free (api_rpaths);
585}
586
587static void
Neale Rannsd91c1db2017-07-31 02:30:50 -0700588vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
589 vlib_main_t * vm)
590{
591 vl_api_ip_punt_police_reply_t *rmp;
592 int rv = 0;
593
594 if (mp->is_ip6)
595 ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
596 else
597 ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
598
599 REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
600}
601
602static void
603vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
604 vlib_main_t * vm)
605{
606 vl_api_ip_punt_redirect_reply_t *rmp;
607 int rv = 0;
608
609 if (mp->is_add)
610 {
611 ip46_address_t nh;
612
613 memset (&nh, 0, sizeof (nh));
614
615 if (mp->is_ip6)
616 {
617 memcpy (&nh.ip6, mp->nh, sizeof (nh.ip6));
618
619 ip6_punt_redirect_add (ntohl (mp->rx_sw_if_index),
620 ntohl (mp->tx_sw_if_index), &nh);
621 }
622 else
623 {
624 memcpy (&nh.ip4, mp->nh, sizeof (nh.ip4));
625
626 ip4_punt_redirect_add (ntohl (mp->rx_sw_if_index),
627 ntohl (mp->tx_sw_if_index), &nh);
628 }
629 }
630 else
631 {
632 if (mp->is_ip6)
633 {
634 ip6_punt_redirect_del (ntohl (mp->rx_sw_if_index));
635 }
636 else
637 {
638 ip4_punt_redirect_del (ntohl (mp->rx_sw_if_index));
639 }
640 }
641
642 REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
643}
644
645static void
Dave Barachb5e8a772016-12-06 12:04:42 -0500646vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
647 vlib_main_t * vm)
648{
649 vl_api_ip_neighbor_add_del_reply_t *rmp;
650 vnet_main_t *vnm = vnet_get_main ();
651 int rv = 0;
652
653 VALIDATE_SW_IF_INDEX (mp);
654
655 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
656
657 /*
658 * there's no validation here of the ND/ARP entry being added.
659 * The expectation is that the FIB will ensure that nothing bad
660 * will come of adding bogus entries.
661 */
662 if (mp->is_ipv6)
663 {
664 if (mp->is_add)
665 rv = vnet_set_ip6_ethernet_neighbor
666 (vm, ntohl (mp->sw_if_index),
667 (ip6_address_t *) (mp->dst_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800668 mp->mac_address, sizeof (mp->mac_address), mp->is_static,
669 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500670 else
671 rv = vnet_unset_ip6_ethernet_neighbor
672 (vm, ntohl (mp->sw_if_index),
673 (ip6_address_t *) (mp->dst_address),
674 mp->mac_address, sizeof (mp->mac_address));
675 }
676 else
677 {
678 ethernet_arp_ip4_over_ethernet_address_t a;
679
680 clib_memcpy (&a.ethernet, mp->mac_address, 6);
681 clib_memcpy (&a.ip4, mp->dst_address, 4);
682
683 if (mp->is_add)
684 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800685 &a, mp->is_static,
686 mp->is_no_adj_fib);
Dave Barachb5e8a772016-12-06 12:04:42 -0500687 else
688 rv =
689 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
690 }
691
Dave Barachb5e8a772016-12-06 12:04:42 -0500692 stats_dsunlock ();
Neale Ranns8edad032017-10-09 05:26:13 -0700693
694 BAD_SW_IF_INDEX_LABEL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500695 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
696}
697
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700698void
Neale Ranns15002542017-09-10 04:39:11 -0700699ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
700{
701 u32 fib_index, mfib_index;
702
703 /*
704 * ignore action on the default table - this is always present
705 * and cannot be added nor deleted from the API
706 */
707 if (0 != table_id)
708 {
709 /*
710 * The API holds only one lock on the table.
711 * i.e. it can be added many times via the API but needs to be
712 * deleted only once.
713 * The FIB index for unicast and multicast is not necessarily the
714 * same, since internal VPP systesm (like LISP and SR) create
715 * their own unicast tables.
716 */
717 fib_index = fib_table_find (fproto, table_id);
718 mfib_index = mfib_table_find (fproto, table_id);
719
720 if (~0 != fib_index)
721 {
722 fib_table_unlock (fib_index, fproto,
723 (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
724 }
725 if (~0 != mfib_index)
726 {
727 mfib_table_unlock (mfib_index, fproto,
728 (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
729 }
730 }
731}
732
733void
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700734vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
735{
736 vl_api_ip_table_add_del_reply_t *rmp;
Neale Ranns15002542017-09-10 04:39:11 -0700737 fib_protocol_t fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
738 u32 table_id = ntohl (mp->table_id);
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700739 int rv = 0;
740
Neale Ranns15002542017-09-10 04:39:11 -0700741 if (mp->is_add)
742 {
Neale Ranns2297af02017-09-12 09:45:04 -0700743 ip_table_create (fproto, table_id, 1, mp->name);
Neale Ranns15002542017-09-10 04:39:11 -0700744 }
745 else
746 {
747 ip_table_delete (fproto, table_id, 1);
748 }
749
Neale Ranns28ab9cc2017-08-14 07:18:42 -0700750 REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
751}
752
Dave Barachb5e8a772016-12-06 12:04:42 -0500753int
754add_del_route_t_handler (u8 is_multipath,
755 u8 is_add,
756 u8 is_drop,
757 u8 is_unreach,
758 u8 is_prohibit,
759 u8 is_local,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800760 u8 is_multicast,
Dave Barachb5e8a772016-12-06 12:04:42 -0500761 u8 is_classify,
762 u32 classify_table_index,
763 u8 is_resolve_host,
764 u8 is_resolve_attached,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800765 u8 is_interface_rx,
766 u8 is_rpf_id,
Neale Rannsf068c3e2018-01-03 04:18:48 -0800767 u8 is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -0700768 u8 is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -0800769 u8 is_udp_encap,
Dave Barachb5e8a772016-12-06 12:04:42 -0500770 u32 fib_index,
771 const fib_prefix_t * prefix,
Neale Rannsda78f952017-05-24 09:15:43 -0700772 dpo_proto_t next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500773 const ip46_address_t * next_hop,
Neale Ranns810086d2017-11-05 16:26:46 -0800774 u32 next_hop_id,
Dave Barachb5e8a772016-12-06 12:04:42 -0500775 u32 next_hop_sw_if_index,
776 u8 next_hop_fib_index,
Neale Ranns57b58602017-07-15 07:37:25 -0700777 u16 next_hop_weight,
778 u16 next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500779 mpls_label_t next_hop_via_label,
Neale Ranns31ed7442018-02-23 05:29:09 -0800780 fib_mpls_label_t * next_hop_out_label_stack)
Dave Barachb5e8a772016-12-06 12:04:42 -0500781{
782 vnet_classify_main_t *cm = &vnet_classify_main;
783 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
784 fib_route_path_t path = {
Neale Rannsda78f952017-05-24 09:15:43 -0700785 .frp_proto = next_hop_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500786 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
787 .frp_sw_if_index = next_hop_sw_if_index,
788 .frp_fib_index = next_hop_fib_index,
789 .frp_weight = next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -0700790 .frp_preference = next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -0500791 .frp_label_stack = next_hop_out_label_stack,
792 };
793 fib_route_path_t *paths = NULL;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800794 fib_entry_flag_t entry_flags = FIB_ENTRY_FLAG_NONE;
Dave Barachb5e8a772016-12-06 12:04:42 -0500795
Neale Rannscaac3502017-09-18 18:04:10 -0700796 /*
797 * the special INVALID label meams we are not recursing via a
798 * label. Exp-null value is never a valid via-label so that
799 * also means it's not a via-label and means clients that set
800 * it to 0 by default get the expected behaviour
801 */
802 if ((MPLS_LABEL_INVALID != next_hop_via_label) && (0 != next_hop_via_label))
Dave Barachb5e8a772016-12-06 12:04:42 -0500803 {
Neale Rannsda78f952017-05-24 09:15:43 -0700804 path.frp_proto = DPO_PROTO_MPLS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500805 path.frp_local_label = next_hop_via_label;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800806 path.frp_eos = MPLS_NON_EOS;
Dave Barachb5e8a772016-12-06 12:04:42 -0500807 }
Neale Rannsf068c3e2018-01-03 04:18:48 -0800808 if (is_dvr)
809 path_flags |= FIB_ROUTE_PATH_DVR;
Dave Barachb5e8a772016-12-06 12:04:42 -0500810 if (is_resolve_host)
811 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
812 if (is_resolve_attached)
813 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800814 if (is_interface_rx)
815 path_flags |= FIB_ROUTE_PATH_INTF_RX;
816 if (is_rpf_id)
817 path_flags |= FIB_ROUTE_PATH_RPF_ID;
Neale Ranns054c03a2017-10-13 05:15:07 -0700818 if (is_source_lookup)
819 path_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800820 if (is_multicast)
821 entry_flags |= FIB_ENTRY_FLAG_MULTICAST;
Neale Ranns810086d2017-11-05 16:26:46 -0800822 if (is_udp_encap)
823 {
824 path_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
825 path.frp_udp_encap_id = next_hop_id;
826 }
Florin Coras79ae2d32017-12-16 08:31:06 -0800827 if (path.frp_sw_if_index == ~0 && ip46_address_is_zero (&path.frp_addr)
828 && path.frp_fib_index != ~0)
829 {
830 path_flags |= FIB_ROUTE_PATH_DEAG;
831 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500832
833 path.frp_flags = path_flags;
834
835 if (is_multipath)
836 {
837 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
838
839
840 vec_add1 (paths, path);
841
842 if (is_add)
843 fib_table_entry_path_add2 (fib_index,
844 prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800845 FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500846 else
847 fib_table_entry_path_remove2 (fib_index,
848 prefix, FIB_SOURCE_API, paths);
849
850 vec_free (paths);
851 stats_dsunlock ();
852 return 0;
853 }
854
855 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
856
857 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
858 {
859 /*
860 * special route types that link directly to the adj
861 */
862 if (is_add)
863 {
864 dpo_id_t dpo = DPO_INVALID;
865 dpo_proto_t dproto;
866
867 dproto = fib_proto_to_dpo (prefix->fp_proto);
868
869 if (is_drop)
870 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
871 else if (is_local)
872 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
873 else if (is_unreach)
874 ip_null_dpo_add_and_lock (dproto,
875 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
876 else if (is_prohibit)
877 ip_null_dpo_add_and_lock (dproto,
878 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
879 &dpo);
880 else if (is_classify)
881 {
882 if (pool_is_free_index (cm->tables,
883 ntohl (classify_table_index)))
884 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500885 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500886 return VNET_API_ERROR_NO_SUCH_TABLE;
887 }
888
889 dpo_set (&dpo, DPO_CLASSIFY, dproto,
890 classify_dpo_create (dproto,
891 ntohl (classify_table_index)));
892 }
893 else
894 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500895 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500896 return VNET_API_ERROR_NO_SUCH_TABLE;
897 }
898
899 fib_table_entry_special_dpo_update (fib_index,
900 prefix,
901 FIB_SOURCE_API,
902 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
903 dpo_reset (&dpo);
904 }
905 else
906 {
907 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
908 }
909 }
910 else
911 {
912 if (is_add)
913 {
914 vec_add1 (paths, path);
915 fib_table_entry_update (fib_index,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800916 prefix, FIB_SOURCE_API, entry_flags, paths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500917 vec_free (paths);
918 }
919 else
920 {
921 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
922 }
923 }
924
Dave Barachd7cb1b52016-12-09 09:52:16 -0500925 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500926 return (0);
927}
928
929int
930add_del_route_check (fib_protocol_t table_proto,
931 u32 table_id,
932 u32 next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700933 dpo_proto_t next_hop_table_proto,
Dave Barachb5e8a772016-12-06 12:04:42 -0500934 u32 next_hop_table_id,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800935 u8 is_rpf_id, u32 * fib_index, u32 * next_hop_fib_index)
Dave Barachb5e8a772016-12-06 12:04:42 -0500936{
937 vnet_main_t *vnm = vnet_get_main ();
938
939 *fib_index = fib_table_find (table_proto, ntohl (table_id));
940 if (~0 == *fib_index)
941 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800942 /* No such VRF, and we weren't asked to create one */
943 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500944 }
945
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800946 if (!is_rpf_id && ~0 != ntohl (next_hop_sw_if_index))
Dave Barachb5e8a772016-12-06 12:04:42 -0500947 {
948 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
949 ntohl (next_hop_sw_if_index)))
950 {
951 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
952 }
953 }
954 else
955 {
Neale Rannsda78f952017-05-24 09:15:43 -0700956 fib_protocol_t fib_nh_proto;
957
958 if (next_hop_table_proto > DPO_PROTO_MPLS)
959 return (0);
960
961 fib_nh_proto = dpo_proto_to_fib (next_hop_table_proto);
962
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800963 if (is_rpf_id)
Neale Rannsda78f952017-05-24 09:15:43 -0700964 *next_hop_fib_index = mfib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800965 ntohl (next_hop_table_id));
966 else
Neale Rannsda78f952017-05-24 09:15:43 -0700967 *next_hop_fib_index = fib_table_find (fib_nh_proto,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800968 ntohl (next_hop_table_id));
Dave Barachb5e8a772016-12-06 12:04:42 -0500969
970 if (~0 == *next_hop_fib_index)
971 {
Neale Rannsb93078d2018-01-25 07:48:12 -0800972 /* No such VRF, and we weren't asked to create one */
973 return VNET_API_ERROR_NO_SUCH_FIB;
Dave Barachb5e8a772016-12-06 12:04:42 -0500974 }
975 }
976
977 return (0);
978}
979
980static int
981ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
982{
983 u32 fib_index, next_hop_fib_index;
Neale Ranns31ed7442018-02-23 05:29:09 -0800984 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -0500985 int rv, ii, n_labels;;
986
987 rv = add_del_route_check (FIB_PROTOCOL_IP4,
988 mp->table_id,
989 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -0700990 DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -0500991 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -0700992 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500993
994 if (0 != rv)
995 return (rv);
996
997 fib_prefix_t pfx = {
998 .fp_len = mp->dst_address_length,
999 .fp_proto = FIB_PROTOCOL_IP4,
1000 };
1001 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
1002
1003 ip46_address_t nh;
1004 memset (&nh, 0, sizeof (nh));
1005 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
1006
1007 n_labels = mp->next_hop_n_out_labels;
1008 if (n_labels == 0)
1009 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001010 else
1011 {
1012 vec_validate (label_stack, n_labels - 1);
1013 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001014 {
1015 label_stack[ii].fml_value =
1016 ntohl (mp->next_hop_out_label_stack[ii].label);
1017 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1018 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1019 label_stack[ii].fml_mode =
1020 (mp->next_hop_out_label_stack[ii].is_uniform ?
1021 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1022 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001023 }
1024
1025 return (add_del_route_t_handler (mp->is_multipath,
1026 mp->is_add,
1027 mp->is_drop,
1028 mp->is_unreach,
1029 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001030 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001031 mp->is_classify,
1032 mp->classify_table_index,
1033 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001034 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001035 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001036 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001037 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001038 fib_index, &pfx, DPO_PROTO_IP4,
Dave Barachb5e8a772016-12-06 12:04:42 -05001039 &nh,
Neale Ranns810086d2017-11-05 16:26:46 -08001040 ntohl (mp->next_hop_id),
Dave Barachb5e8a772016-12-06 12:04:42 -05001041 ntohl (mp->next_hop_sw_if_index),
1042 next_hop_fib_index,
1043 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001044 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001045 ntohl (mp->next_hop_via_label),
1046 label_stack));
1047}
1048
1049static int
1050ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1051{
Neale Ranns31ed7442018-02-23 05:29:09 -08001052 fib_mpls_label_t *label_stack = NULL;
Dave Barachb5e8a772016-12-06 12:04:42 -05001053 u32 fib_index, next_hop_fib_index;
Dave Barachb5e8a772016-12-06 12:04:42 -05001054 int rv, ii, n_labels;;
1055
1056 rv = add_del_route_check (FIB_PROTOCOL_IP6,
1057 mp->table_id,
1058 mp->next_hop_sw_if_index,
Neale Rannsda78f952017-05-24 09:15:43 -07001059 DPO_PROTO_IP6,
Dave Barachb5e8a772016-12-06 12:04:42 -05001060 mp->next_hop_table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001061 0, &fib_index, &next_hop_fib_index);
Dave Barachb5e8a772016-12-06 12:04:42 -05001062
1063 if (0 != rv)
1064 return (rv);
1065
1066 fib_prefix_t pfx = {
1067 .fp_len = mp->dst_address_length,
1068 .fp_proto = FIB_PROTOCOL_IP6,
1069 };
1070 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
1071
1072 ip46_address_t nh;
1073 memset (&nh, 0, sizeof (nh));
1074 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
1075
1076 n_labels = mp->next_hop_n_out_labels;
1077 if (n_labels == 0)
1078 ;
Dave Barachb5e8a772016-12-06 12:04:42 -05001079 else
1080 {
1081 vec_validate (label_stack, n_labels - 1);
1082 for (ii = 0; ii < n_labels; ii++)
Neale Ranns31ed7442018-02-23 05:29:09 -08001083 {
1084 label_stack[ii].fml_value =
1085 ntohl (mp->next_hop_out_label_stack[ii].label);
1086 label_stack[ii].fml_ttl = mp->next_hop_out_label_stack[ii].ttl;
1087 label_stack[ii].fml_exp = mp->next_hop_out_label_stack[ii].exp;
1088 label_stack[ii].fml_mode =
1089 (mp->next_hop_out_label_stack[ii].is_uniform ?
1090 FIB_MPLS_LSP_MODE_UNIFORM : FIB_MPLS_LSP_MODE_PIPE);
1091 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001092 }
1093
1094 return (add_del_route_t_handler (mp->is_multipath,
1095 mp->is_add,
1096 mp->is_drop,
1097 mp->is_unreach,
1098 mp->is_prohibit,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001099 mp->is_local, 0,
Dave Barachb5e8a772016-12-06 12:04:42 -05001100 mp->is_classify,
1101 mp->classify_table_index,
1102 mp->is_resolve_host,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001103 mp->is_resolve_attached, 0, 0,
Neale Rannsf068c3e2018-01-03 04:18:48 -08001104 mp->is_dvr,
Neale Ranns054c03a2017-10-13 05:15:07 -07001105 mp->is_source_lookup,
Neale Ranns810086d2017-11-05 16:26:46 -08001106 mp->is_udp_encap,
Neale Rannsda78f952017-05-24 09:15:43 -07001107 fib_index, &pfx, DPO_PROTO_IP6,
Neale Ranns810086d2017-11-05 16:26:46 -08001108 &nh, ntohl (mp->next_hop_id),
1109 ntohl (mp->next_hop_sw_if_index),
Dave Barachb5e8a772016-12-06 12:04:42 -05001110 next_hop_fib_index,
1111 mp->next_hop_weight,
Neale Ranns57b58602017-07-15 07:37:25 -07001112 mp->next_hop_preference,
Dave Barachb5e8a772016-12-06 12:04:42 -05001113 ntohl (mp->next_hop_via_label),
1114 label_stack));
1115}
1116
1117void
1118vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
1119{
1120 vl_api_ip_add_del_route_reply_t *rmp;
1121 int rv;
1122 vnet_main_t *vnm = vnet_get_main ();
1123
1124 vnm->api_errno = 0;
1125
1126 if (mp->is_ipv6)
1127 rv = ip6_add_del_route_t_handler (mp);
1128 else
1129 rv = ip4_add_del_route_t_handler (mp);
1130
1131 rv = (rv == 0) ? vnm->api_errno : rv;
1132
1133 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
1134}
1135
Neale Ranns15002542017-09-10 04:39:11 -07001136void
Neale Ranns2297af02017-09-12 09:45:04 -07001137ip_table_create (fib_protocol_t fproto,
1138 u32 table_id, u8 is_api, const u8 * name)
Neale Ranns15002542017-09-10 04:39:11 -07001139{
1140 u32 fib_index, mfib_index;
1141
1142 /*
1143 * ignore action on the default table - this is always present
1144 * and cannot be added nor deleted from the API
1145 */
1146 if (0 != table_id)
1147 {
1148 /*
1149 * The API holds only one lock on the table.
1150 * i.e. it can be added many times via the API but needs to be
1151 * deleted only once.
1152 * The FIB index for unicast and multicast is not necessarily the
1153 * same, since internal VPP systesm (like LISP and SR) create
1154 * their own unicast tables.
1155 */
1156 fib_index = fib_table_find (fproto, table_id);
1157 mfib_index = mfib_table_find (fproto, table_id);
1158
1159 if (~0 == fib_index)
1160 {
Neale Ranns2297af02017-09-12 09:45:04 -07001161 fib_table_find_or_create_and_lock_w_name (fproto, table_id,
1162 (is_api ?
1163 FIB_SOURCE_API :
1164 FIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001165 }
1166 if (~0 == mfib_index)
1167 {
Neale Ranns2297af02017-09-12 09:45:04 -07001168 mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
1169 (is_api ?
1170 MFIB_SOURCE_API :
1171 MFIB_SOURCE_CLI), name);
Neale Ranns15002542017-09-10 04:39:11 -07001172 }
1173 }
1174}
1175
Neale Ranns32e1c012016-11-22 17:07:28 +00001176static int
1177add_del_mroute_check (fib_protocol_t table_proto,
1178 u32 table_id,
Neale Ranns15002542017-09-10 04:39:11 -07001179 u32 next_hop_sw_if_index, u8 is_local, u32 * fib_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001180{
1181 vnet_main_t *vnm = vnet_get_main ();
1182
1183 *fib_index = mfib_table_find (table_proto, ntohl (table_id));
1184 if (~0 == *fib_index)
1185 {
Neale Ranns15002542017-09-10 04:39:11 -07001186 /* No such table */
1187 return VNET_API_ERROR_NO_SUCH_FIB;
Neale Ranns32e1c012016-11-22 17:07:28 +00001188 }
1189
1190 if (~0 != ntohl (next_hop_sw_if_index))
1191 {
1192 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
1193 ntohl (next_hop_sw_if_index)))
1194 {
1195 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
1196 }
1197 }
1198
1199 return (0);
1200}
1201
1202static int
1203mroute_add_del_handler (u8 is_add,
1204 u8 is_local,
1205 u32 fib_index,
1206 const mfib_prefix_t * prefix,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001207 dpo_proto_t nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001208 u32 entry_flags,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001209 fib_rpf_id_t rpf_id,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001210 u32 next_hop_sw_if_index, u32 itf_flags, u32 bier_imp)
Neale Ranns32e1c012016-11-22 17:07:28 +00001211{
1212 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
1213
1214 fib_route_path_t path = {
1215 .frp_sw_if_index = next_hop_sw_if_index,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001216 .frp_proto = nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001217 };
1218
1219 if (is_local)
1220 path.frp_flags |= FIB_ROUTE_PATH_LOCAL;
1221
Neale Rannsd792d9c2017-10-21 10:53:20 -07001222 if (DPO_PROTO_BIER == nh_proto)
1223 {
1224 path.frp_bier_imp = bier_imp;
1225 path.frp_flags = FIB_ROUTE_PATH_BIER_IMP;
1226 }
1227 else if (!is_local && ~0 == next_hop_sw_if_index)
Neale Ranns32e1c012016-11-22 17:07:28 +00001228 {
1229 mfib_table_entry_update (fib_index, prefix,
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001230 MFIB_SOURCE_API, rpf_id, entry_flags);
Neale Rannsd792d9c2017-10-21 10:53:20 -07001231 goto done;
1232 }
1233
1234 if (is_add)
1235 {
1236 mfib_table_entry_path_update (fib_index, prefix,
1237 MFIB_SOURCE_API, &path, itf_flags);
Neale Ranns32e1c012016-11-22 17:07:28 +00001238 }
1239 else
1240 {
Neale Rannsd792d9c2017-10-21 10:53:20 -07001241 mfib_table_entry_path_remove (fib_index, prefix,
1242 MFIB_SOURCE_API, &path);
Neale Ranns32e1c012016-11-22 17:07:28 +00001243 }
1244
Neale Rannsd792d9c2017-10-21 10:53:20 -07001245done:
Neale Ranns32e1c012016-11-22 17:07:28 +00001246 stats_dsunlock ();
1247 return (0);
1248}
1249
1250static int
1251api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1252{
1253 fib_protocol_t fproto;
Neale Rannsd792d9c2017-10-21 10:53:20 -07001254 dpo_proto_t nh_proto;
Neale Ranns32e1c012016-11-22 17:07:28 +00001255 u32 fib_index;
1256 int rv;
1257
Neale Rannsd792d9c2017-10-21 10:53:20 -07001258 nh_proto = mp->next_hop_afi;
Neale Ranns32e1c012016-11-22 17:07:28 +00001259 fproto = (mp->is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1260 rv = add_del_mroute_check (fproto,
1261 mp->table_id,
1262 mp->next_hop_sw_if_index,
Neale Ranns15002542017-09-10 04:39:11 -07001263 mp->is_local, &fib_index);
Neale Ranns32e1c012016-11-22 17:07:28 +00001264
1265 if (0 != rv)
1266 return (rv);
1267
1268 mfib_prefix_t pfx = {
1269 .fp_len = ntohs (mp->grp_address_length),
1270 .fp_proto = fproto,
1271 };
1272
1273 if (FIB_PROTOCOL_IP4 == fproto)
1274 {
1275 clib_memcpy (&pfx.fp_grp_addr.ip4, mp->grp_address,
1276 sizeof (pfx.fp_grp_addr.ip4));
1277 clib_memcpy (&pfx.fp_src_addr.ip4, mp->src_address,
1278 sizeof (pfx.fp_src_addr.ip4));
1279 }
1280 else
1281 {
1282 clib_memcpy (&pfx.fp_grp_addr.ip6, mp->grp_address,
1283 sizeof (pfx.fp_grp_addr.ip6));
1284 clib_memcpy (&pfx.fp_src_addr.ip6, mp->src_address,
1285 sizeof (pfx.fp_src_addr.ip6));
1286 }
1287
1288 return (mroute_add_del_handler (mp->is_add,
1289 mp->is_local,
1290 fib_index, &pfx,
Neale Rannsd792d9c2017-10-21 10:53:20 -07001291 nh_proto,
Neale Ranns32e1c012016-11-22 17:07:28 +00001292 ntohl (mp->entry_flags),
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001293 ntohl (mp->rpf_id),
Neale Ranns32e1c012016-11-22 17:07:28 +00001294 ntohl (mp->next_hop_sw_if_index),
Neale Rannsd792d9c2017-10-21 10:53:20 -07001295 ntohl (mp->itf_flags),
1296 ntohl (mp->bier_imp)));
Neale Ranns32e1c012016-11-22 17:07:28 +00001297}
1298
1299void
1300vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
1301{
1302 vl_api_ip_mroute_add_del_reply_t *rmp;
1303 int rv;
1304 vnet_main_t *vnm = vnet_get_main ();
1305
1306 vnm->api_errno = 0;
1307
1308 rv = api_mroute_add_del_t_handler (mp);
1309
1310 rv = (rv == 0) ? vnm->api_errno : rv;
1311
1312 REPLY_MACRO (VL_API_IP_MROUTE_ADD_DEL_REPLY);
1313}
1314
Dave Barachb5e8a772016-12-06 12:04:42 -05001315static void
1316send_ip_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001317 vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
1318 u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001319{
1320 vl_api_ip_details_t *mp;
1321
1322 mp = vl_msg_api_alloc (sizeof (*mp));
1323 memset (mp, 0, sizeof (*mp));
1324 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
1325
1326 mp->sw_if_index = ntohl (sw_if_index);
Jon Loeliger466f0d42017-02-09 12:17:50 -06001327 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001328 mp->context = context;
1329
Florin Coras6c4dae22018-01-09 06:39:23 -08001330 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001331}
1332
1333static void
1334send_ip_address_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -08001335 vl_api_registration_t * reg,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001336 u8 * ip, u16 prefix_length,
1337 u32 sw_if_index, u8 is_ipv6, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -05001338{
1339 vl_api_ip_address_details_t *mp;
1340
1341 mp = vl_msg_api_alloc (sizeof (*mp));
1342 memset (mp, 0, sizeof (*mp));
1343 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
1344
1345 if (is_ipv6)
1346 {
1347 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
1348 }
1349 else
1350 {
1351 u32 *tp = (u32 *) mp->ip;
1352 *tp = *(u32 *) ip;
1353 }
1354 mp->prefix_length = prefix_length;
1355 mp->context = context;
Jon Loeliger466f0d42017-02-09 12:17:50 -06001356 mp->sw_if_index = htonl (sw_if_index);
1357 mp->is_ipv6 = is_ipv6;
Dave Barachb5e8a772016-12-06 12:04:42 -05001358
Florin Coras6c4dae22018-01-09 06:39:23 -08001359 vl_api_send_msg (reg, (u8 *) mp);
Dave Barachb5e8a772016-12-06 12:04:42 -05001360}
1361
1362static void
1363vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
1364{
1365 vpe_api_main_t *am = &vpe_api_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001366 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001367 ip6_address_t *r6;
1368 ip4_address_t *r4;
1369 ip6_main_t *im6 = &ip6_main;
1370 ip4_main_t *im4 = &ip4_main;
1371 ip_lookup_main_t *lm6 = &im6->lookup_main;
1372 ip_lookup_main_t *lm4 = &im4->lookup_main;
1373 ip_interface_address_t *ia = 0;
1374 u32 sw_if_index = ~0;
1375 int rv __attribute__ ((unused)) = 0;
1376
1377 VALIDATE_SW_IF_INDEX (mp);
1378
1379 sw_if_index = ntohl (mp->sw_if_index);
1380
Florin Coras6c4dae22018-01-09 06:39:23 -08001381 reg = vl_api_client_index_to_registration (mp->client_index);
1382 if (!reg)
Dave Barachb5e8a772016-12-06 12:04:42 -05001383 return;
1384
Dave Barachb5e8a772016-12-06 12:04:42 -05001385 if (mp->is_ipv6)
1386 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001387 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001388 foreach_ip_interface_address (lm6, ia, sw_if_index,
1389 1 /* honor unnumbered */,
1390 ({
1391 r6 = ip_interface_address_get_address (lm6, ia);
1392 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001393 send_ip_address_details(am, reg, (u8*)r6, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001394 sw_if_index, 1, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001395 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001396 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001397 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001398 else
1399 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001400 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001401 foreach_ip_interface_address (lm4, ia, sw_if_index,
1402 1 /* honor unnumbered */,
1403 ({
1404 r4 = ip_interface_address_get_address (lm4, ia);
1405 u16 prefix_length = ia->address_length;
Florin Coras6c4dae22018-01-09 06:39:23 -08001406 send_ip_address_details(am, reg, (u8*)r4, prefix_length,
Jon Loeliger466f0d42017-02-09 12:17:50 -06001407 sw_if_index, 0, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001408 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -05001409 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -05001410 }
Dave Barachb5e8a772016-12-06 12:04:42 -05001411 BAD_SW_IF_INDEX_LABEL;
1412}
1413
1414static void
1415vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
1416{
1417 vpe_api_main_t *am = &vpe_api_main;
1418 vnet_main_t *vnm = vnet_get_main ();
1419 vlib_main_t *vm = vlib_get_main ();
1420 vnet_interface_main_t *im = &vnm->interface_main;
Florin Coras6c4dae22018-01-09 06:39:23 -08001421 vl_api_registration_t *reg;
Dave Barachb5e8a772016-12-06 12:04:42 -05001422 vnet_sw_interface_t *si, *sorted_sis;
1423 u32 sw_if_index = ~0;
1424
Florin Coras6c4dae22018-01-09 06:39:23 -08001425 reg = vl_api_client_index_to_registration (mp->client_index);
1426 if (!reg)
1427 return;
Dave Barachb5e8a772016-12-06 12:04:42 -05001428
1429 /* Gather interfaces. */
1430 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1431 _vec_len (sorted_sis) = 0;
1432 /* *INDENT-OFF* */
1433 pool_foreach (si, im->sw_interfaces,
1434 ({
1435 vec_add1 (sorted_sis, si[0]);
1436 }));
1437 /* *INDENT-ON* */
1438
1439 vec_foreach (si, sorted_sis)
1440 {
1441 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1442 {
1443 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
1444 {
1445 continue;
1446 }
1447 sw_if_index = si->sw_if_index;
Florin Coras6c4dae22018-01-09 06:39:23 -08001448 send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -05001449 }
1450 }
1451}
1452
1453static void
1454set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1455{
1456 vl_api_set_ip_flow_hash_reply_t *rmp;
Neale Ranns227038a2017-04-21 01:07:59 -07001457 int rv;
1458 u32 table_id;
1459 flow_hash_config_t flow_hash_config = 0;
Dave Barachb5e8a772016-12-06 12:04:42 -05001460
Neale Ranns227038a2017-04-21 01:07:59 -07001461 table_id = ntohl (mp->vrf_id);
1462
1463#define _(a,b) if (mp->a) flow_hash_config |= b;
1464 foreach_flow_hash_bit;
1465#undef _
1466
1467 rv = vnet_set_ip6_flow_hash (table_id, flow_hash_config);
Dave Barachb5e8a772016-12-06 12:04:42 -05001468
1469 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1470}
1471
1472static void
1473set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
1474{
1475 vl_api_set_ip_flow_hash_reply_t *rmp;
1476 int rv;
1477 u32 table_id;
1478 flow_hash_config_t flow_hash_config = 0;
1479
1480 table_id = ntohl (mp->vrf_id);
1481
1482#define _(a,b) if (mp->a) flow_hash_config |= b;
1483 foreach_flow_hash_bit;
1484#undef _
1485
1486 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1487
1488 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1489}
1490
1491
1492static void
1493vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1494{
1495 if (mp->is_ipv6 == 0)
1496 set_ip4_flow_hash (mp);
1497 else
1498 set_ip6_flow_hash (mp);
1499}
1500
1501static void
1502 vl_api_sw_interface_ip6nd_ra_config_t_handler
1503 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1504{
1505 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1506 vlib_main_t *vm = vlib_get_main ();
1507 int rv = 0;
1508 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1509 default_router;
1510
1511 is_no = mp->is_no == 1;
1512 suppress = mp->suppress == 1;
1513 managed = mp->managed == 1;
1514 other = mp->other == 1;
1515 ll_option = mp->ll_option == 1;
1516 send_unicast = mp->send_unicast == 1;
1517 cease = mp->cease == 1;
1518 default_router = mp->default_router == 1;
1519
1520 VALIDATE_SW_IF_INDEX (mp);
1521
1522 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1523 suppress, managed, other,
1524 ll_option, send_unicast, cease,
1525 default_router, ntohl (mp->lifetime),
1526 ntohl (mp->initial_count),
1527 ntohl (mp->initial_interval),
1528 ntohl (mp->max_interval),
1529 ntohl (mp->min_interval), is_no);
1530
1531 BAD_SW_IF_INDEX_LABEL;
1532
1533 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1534}
1535
1536static void
1537 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1538 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1539{
1540 vlib_main_t *vm = vlib_get_main ();
1541 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1542 int rv = 0;
1543 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1544
1545 VALIDATE_SW_IF_INDEX (mp);
1546
1547 is_no = mp->is_no == 1;
1548 use_default = mp->use_default == 1;
1549 no_advertise = mp->no_advertise == 1;
1550 off_link = mp->off_link == 1;
1551 no_autoconfig = mp->no_autoconfig == 1;
1552 no_onlink = mp->no_onlink == 1;
1553
1554 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1555 (ip6_address_t *) mp->address,
1556 mp->address_length, use_default,
1557 ntohl (mp->val_lifetime),
1558 ntohl (mp->pref_lifetime), no_advertise,
1559 off_link, no_autoconfig, no_onlink, is_no);
1560
1561 BAD_SW_IF_INDEX_LABEL;
1562 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1563}
1564
1565static void
Florin Coras6c4dae22018-01-09 06:39:23 -08001566send_ip6nd_proxy_details (vl_api_registration_t * reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001567 u32 context,
1568 const ip46_address_t * addr, u32 sw_if_index)
1569{
1570 vl_api_ip6nd_proxy_details_t *mp;
1571
1572 mp = vl_msg_api_alloc (sizeof (*mp));
1573 memset (mp, 0, sizeof (*mp));
1574 mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
1575 mp->context = context;
1576 mp->sw_if_index = htonl (sw_if_index);
1577 memcpy (mp->address, addr, 16);
1578
Florin Coras6c4dae22018-01-09 06:39:23 -08001579 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns3f844d02017-02-18 00:03:54 -08001580}
1581
Neale Ranns3f844d02017-02-18 00:03:54 -08001582typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
1583{
1584 u32 *indices;
1585} api_ip6nd_proxy_fib_table_walk_ctx_t;
1586
Neale Ranns89541992017-04-06 04:41:02 -07001587static fib_table_walk_rc_t
Neale Ranns3f844d02017-02-18 00:03:54 -08001588api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
1589{
1590 api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
1591
1592 if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
1593 {
1594 vec_add1 (ctx->indices, fei);
1595 }
1596
Neale Ranns89541992017-04-06 04:41:02 -07001597 return (FIB_TABLE_WALK_CONTINUE);
Neale Ranns3f844d02017-02-18 00:03:54 -08001598}
1599
1600static void
1601vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
1602{
1603 ip6_main_t *im6 = &ip6_main;
1604 fib_table_t *fib_table;
1605 api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
1606 .indices = NULL,
1607 };
1608 fib_node_index_t *feip;
1609 fib_prefix_t pfx;
Florin Coras6c4dae22018-01-09 06:39:23 -08001610 vl_api_registration_t *reg;
Neale Ranns3f844d02017-02-18 00:03:54 -08001611
Florin Coras6c4dae22018-01-09 06:39:23 -08001612 reg = vl_api_client_index_to_registration (mp->client_index);
1613 if (!reg)
1614 return;
Neale Ranns3f844d02017-02-18 00:03:54 -08001615
1616 /* *INDENT-OFF* */
1617 pool_foreach (fib_table, im6->fibs,
1618 ({
1619 fib_table_walk(fib_table->ft_index,
1620 FIB_PROTOCOL_IP6,
1621 api_ip6nd_proxy_fib_table_walk,
1622 &ctx);
1623 }));
1624 /* *INDENT-ON* */
1625
1626 vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
1627
1628 vec_foreach (feip, ctx.indices)
1629 {
1630 fib_entry_get_prefix (*feip, &pfx);
1631
Florin Coras6c4dae22018-01-09 06:39:23 -08001632 send_ip6nd_proxy_details (reg,
Neale Ranns3f844d02017-02-18 00:03:54 -08001633 mp->context,
1634 &pfx.fp_addr,
1635 fib_entry_get_resolving_interface (*feip));
1636 }
1637
1638 vec_free (ctx.indices);
1639}
1640
1641static void
1642vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
1643{
1644 vl_api_ip6nd_proxy_add_del_reply_t *rmp;
1645 int rv = 0;
1646
1647 VALIDATE_SW_IF_INDEX (mp);
1648
1649 rv = ip6_neighbor_proxy_add_del (ntohl (mp->sw_if_index),
1650 (ip6_address_t *) mp->address, mp->is_del);
1651
1652 BAD_SW_IF_INDEX_LABEL;
1653 REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
1654}
1655
1656static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001657 vl_api_ip6nd_send_router_solicitation_t_handler
1658 (vl_api_ip6nd_send_router_solicitation_t * mp)
1659{
1660 vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
1661 icmp6_send_router_solicitation_params_t params;
1662 vlib_main_t *vm = vlib_get_main ();
1663 int rv = 0;
1664
1665 VALIDATE_SW_IF_INDEX (mp);
1666
1667 BAD_SW_IF_INDEX_LABEL;
1668 REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
1669
1670 if (rv != 0)
1671 return;
1672
1673 params.irt = ntohl (mp->irt);
1674 params.mrt = ntohl (mp->mrt);
1675 params.mrc = ntohl (mp->mrc);
1676 params.mrd = ntohl (mp->mrd);
1677
1678 icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
1679 &params);
1680}
1681
1682static void
Dave Barachb5e8a772016-12-06 12:04:42 -05001683 vl_api_sw_interface_ip6_enable_disable_t_handler
1684 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1685{
1686 vlib_main_t *vm = vlib_get_main ();
1687 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1688 vnet_main_t *vnm = vnet_get_main ();
1689 int rv = 0;
1690 clib_error_t *error;
1691
1692 vnm->api_errno = 0;
1693
1694 VALIDATE_SW_IF_INDEX (mp);
1695
1696 error =
1697 (mp->enable == 1) ? enable_ip6_interface (vm,
1698 ntohl (mp->sw_if_index)) :
1699 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1700
1701 if (error)
1702 {
1703 clib_error_report (error);
1704 rv = VNET_API_ERROR_UNSPECIFIED;
1705 }
1706 else
1707 {
1708 rv = vnm->api_errno;
1709 }
1710
1711 BAD_SW_IF_INDEX_LABEL;
1712
1713 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1714}
1715
1716static void
1717 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1718 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1719{
1720 vlib_main_t *vm = vlib_get_main ();
1721 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1722 int rv = 0;
1723 clib_error_t *error;
1724 vnet_main_t *vnm = vnet_get_main ();
1725
1726 vnm->api_errno = 0;
1727
1728 VALIDATE_SW_IF_INDEX (mp);
1729
1730 error = set_ip6_link_local_address (vm,
1731 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001732 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001733 if (error)
1734 {
1735 clib_error_report (error);
1736 rv = VNET_API_ERROR_UNSPECIFIED;
1737 }
1738 else
1739 {
1740 rv = vnm->api_errno;
1741 }
1742
1743 BAD_SW_IF_INDEX_LABEL;
1744
1745 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1746}
1747
Neale Ranns32e1c012016-11-22 17:07:28 +00001748void
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001749vl_mfib_signal_send_one (vl_api_registration_t * reg,
Neale Ranns32e1c012016-11-22 17:07:28 +00001750 u32 context, const mfib_signal_t * mfs)
1751{
1752 vl_api_mfib_signal_details_t *mp;
1753 mfib_prefix_t prefix;
1754 mfib_table_t *mfib;
1755 mfib_itf_t *mfi;
1756
1757 mp = vl_msg_api_alloc (sizeof (*mp));
1758
1759 memset (mp, 0, sizeof (*mp));
1760 mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1761 mp->context = context;
1762
1763 mfi = mfib_itf_get (mfs->mfs_itf);
1764 mfib_entry_get_prefix (mfs->mfs_entry, &prefix);
1765 mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1766 prefix.fp_proto);
1767 mp->table_id = ntohl (mfib->mft_table_id);
1768 mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1769
1770 if (FIB_PROTOCOL_IP4 == prefix.fp_proto)
1771 {
1772 mp->grp_address_len = ntohs (prefix.fp_len);
1773
1774 memcpy (mp->grp_address, &prefix.fp_grp_addr.ip4, 4);
1775 if (prefix.fp_len > 32)
1776 {
1777 memcpy (mp->src_address, &prefix.fp_src_addr.ip4, 4);
1778 }
1779 }
1780 else
1781 {
1782 mp->grp_address_len = ntohs (prefix.fp_len);
1783
1784 ASSERT (0);
1785 }
1786
1787 if (0 != mfs->mfs_buffer_len)
1788 {
1789 mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1790
1791 memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1792 }
1793 else
1794 {
1795 mp->ip_packet_len = 0;
1796 }
1797
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001798 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns32e1c012016-11-22 17:07:28 +00001799}
1800
1801static void
1802vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1803{
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001804 vl_api_registration_t *reg;
Neale Ranns32e1c012016-11-22 17:07:28 +00001805
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001806 reg = vl_api_client_index_to_registration (mp->client_index);
1807 if (!reg)
1808 return;
Neale Ranns32e1c012016-11-22 17:07:28 +00001809
Florin Corasaf0ff5a2018-01-10 08:17:03 -08001810 while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
Neale Ranns32e1c012016-11-22 17:07:28 +00001811 ;
1812}
Dave Barachb5e8a772016-12-06 12:04:42 -05001813
Florin Coras595992c2017-11-06 17:17:08 -08001814static void
1815 vl_api_ip_container_proxy_add_del_t_handler
1816 (vl_api_ip_container_proxy_add_del_t * mp)
1817{
1818 vl_api_ip_container_proxy_add_del_reply_t *rmp;
1819 vnet_ip_container_proxy_args_t args;
1820 int rv = 0;
1821 clib_error_t *error;
1822
1823 memset (&args, 0, sizeof (args));
1824 ip_set (&args.prefix.fp_addr, mp->ip, mp->is_ip4);
1825 args.prefix.fp_len = mp->plen ? mp->plen : (mp->is_ip4 ? 32 : 128);
1826 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1827 args.is_add = mp->is_add;
1828 if ((error = vnet_ip_container_proxy_add_del (&args)))
1829 {
1830 rv = clib_error_get_code (error);
1831 clib_error_report (error);
1832 }
1833
1834 REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1835}
1836
Neale Rannsb8d44812017-11-10 06:53:54 -08001837static void
1838vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1839{
1840 int rv = 0;
1841 vl_api_ioam_enable_reply_t *rmp;
1842 clib_error_t *error;
1843
1844 /* Ignoring the profile id as currently a single profile
1845 * is supported */
1846 error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1847 mp->seqno, mp->analyse);
1848 if (error)
1849 {
1850 clib_error_report (error);
1851 rv = clib_error_get_code (error);
1852 }
1853
1854 REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1855}
1856
1857static void
1858vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1859{
1860 int rv = 0;
1861 vl_api_ioam_disable_reply_t *rmp;
1862 clib_error_t *error;
1863
1864 error = clear_ioam_rewrite_fn ();
1865 if (error)
1866 {
1867 clib_error_report (error);
1868 rv = clib_error_get_code (error);
1869 }
1870
1871 REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1872}
1873
1874static void
1875 vl_api_ip_source_and_port_range_check_add_del_t_handler
1876 (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1877{
1878 vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1879 int rv = 0;
1880
1881 u8 is_ipv6 = mp->is_ipv6;
1882 u8 is_add = mp->is_add;
1883 u8 mask_length = mp->mask_length;
1884 ip4_address_t ip4_addr;
1885 ip6_address_t ip6_addr;
1886 u16 *low_ports = 0;
1887 u16 *high_ports = 0;
1888 u32 vrf_id;
1889 u16 tmp_low, tmp_high;
1890 u8 num_ranges;
1891 int i;
1892
1893 // Validate port range
1894 num_ranges = mp->number_of_ranges;
1895 if (num_ranges > 32)
1896 { // This is size of array in VPE.API
1897 rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1898 goto reply;
1899 }
1900
1901 vec_reset_length (low_ports);
1902 vec_reset_length (high_ports);
1903
1904 for (i = 0; i < num_ranges; i++)
1905 {
1906 tmp_low = mp->low_ports[i];
1907 tmp_high = mp->high_ports[i];
1908 // If tmp_low <= tmp_high then only need to check tmp_low = 0
1909 // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1910 if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1911 {
1912 rv = VNET_API_ERROR_INVALID_VALUE;
1913 goto reply;
1914 }
1915 vec_add1 (low_ports, tmp_low);
1916 vec_add1 (high_ports, tmp_high + 1);
1917 }
1918
1919 // Validate mask_length
1920 if ((is_ipv6 && mask_length > 128) || (!is_ipv6 && mask_length > 32))
1921 {
1922 rv = VNET_API_ERROR_ADDRESS_LENGTH_MISMATCH;
1923 goto reply;
1924 }
1925
1926 vrf_id = ntohl (mp->vrf_id);
1927
1928 if (vrf_id < 1)
1929 {
1930 rv = VNET_API_ERROR_INVALID_VALUE;
1931 goto reply;
1932 }
1933
1934
1935 if (is_ipv6)
1936 {
1937 clib_memcpy (ip6_addr.as_u8, mp->address, sizeof (ip6_addr.as_u8));
1938 rv = ip6_source_and_port_range_check_add_del (&ip6_addr,
1939 mask_length,
1940 vrf_id,
1941 low_ports,
1942 high_ports, is_add);
1943 }
1944 else
1945 {
1946 clib_memcpy (ip4_addr.data, mp->address, sizeof (ip4_addr));
1947 rv = ip4_source_and_port_range_check_add_del (&ip4_addr,
1948 mask_length,
1949 vrf_id,
1950 low_ports,
1951 high_ports, is_add);
1952 }
1953
1954reply:
1955 vec_free (low_ports);
1956 vec_free (high_ports);
1957 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1958}
1959
1960static void
1961 vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1962 (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1963{
1964 vlib_main_t *vm = vlib_get_main ();
1965 vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1966 ip4_main_t *im = &ip4_main;
1967 int rv;
1968 u32 sw_if_index;
1969 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1970 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1971 uword *p = 0;
1972 int i;
1973
1974 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1975 ntohl (mp->tcp_out_vrf_id);
1976 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1977 ntohl (mp->udp_out_vrf_id);
1978 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1979 ntohl (mp->tcp_in_vrf_id);
1980 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1981 ntohl (mp->udp_in_vrf_id);
1982
1983
1984 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1985 {
1986 if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1987 {
1988 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1989
1990 if (p == 0)
1991 {
1992 rv = VNET_API_ERROR_INVALID_VALUE;
1993 goto reply;
1994 }
1995
1996 fib_index[i] = p[0];
1997 }
1998 else
1999 fib_index[i] = ~0;
2000 }
2001 sw_if_index = ntohl (mp->sw_if_index);
2002
2003 VALIDATE_SW_IF_INDEX (mp);
2004
2005 rv =
2006 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
2007 mp->is_add);
2008
2009 BAD_SW_IF_INDEX_LABEL;
2010reply:
2011
2012 REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
2013}
2014
2015#define IP4_ARP_EVENT 3
2016#define IP6_ND_EVENT 4
2017
2018static int arp_change_delete_callback (u32 pool_index, u8 * notused);
2019static int nd_change_delete_callback (u32 pool_index, u8 * notused);
2020static vlib_node_registration_t ip_resolver_process_node;
2021
2022static void
2023handle_ip4_arp_event (u32 pool_index)
2024{
2025 vpe_api_main_t *vam = &vpe_api_main;
2026 vnet_main_t *vnm = vam->vnet_main;
2027 vlib_main_t *vm = vam->vlib_main;
2028 vl_api_ip4_arp_event_t *event;
2029 vl_api_ip4_arp_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002030 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002031
2032 /* Client can cancel, die, etc. */
2033 if (pool_is_free_index (vam->arp_events, pool_index))
2034 return;
2035
2036 event = pool_elt_at_index (vam->arp_events, pool_index);
2037
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002038 reg = vl_api_client_index_to_registration (event->client_index);
2039 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002040 {
2041 (void) vnet_add_del_ip4_arp_change_event
2042 (vnm, arp_change_delete_callback,
2043 event->pid, &event->address,
2044 ip_resolver_process_node.index, IP4_ARP_EVENT,
2045 ~0 /* pool index, notused */ , 0 /* is_add */ );
2046 return;
2047 }
2048
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002049 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002050 {
2051 mp = vl_msg_api_alloc (sizeof (*mp));
2052 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002053 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002054 }
2055 else
2056 {
2057 static f64 last_time;
2058 /*
2059 * Throttle syslog msgs.
2060 * It's pretty tempting to just revoke the registration...
2061 */
2062 if (vlib_time_now (vm) > last_time + 10.0)
2063 {
2064 clib_warning ("arp event for %U to pid %d: queue stuffed!",
2065 format_ip4_address, &event->address, event->pid);
2066 last_time = vlib_time_now (vm);
2067 }
2068 }
2069}
2070
2071void
2072handle_ip6_nd_event (u32 pool_index)
2073{
2074 vpe_api_main_t *vam = &vpe_api_main;
2075 vnet_main_t *vnm = vam->vnet_main;
2076 vlib_main_t *vm = vam->vlib_main;
2077 vl_api_ip6_nd_event_t *event;
2078 vl_api_ip6_nd_event_t *mp;
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002079 vl_api_registration_t *reg;
Neale Rannsb8d44812017-11-10 06:53:54 -08002080
2081 /* Client can cancel, die, etc. */
2082 if (pool_is_free_index (vam->nd_events, pool_index))
2083 return;
2084
2085 event = pool_elt_at_index (vam->nd_events, pool_index);
2086
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002087 reg = vl_api_client_index_to_registration (event->client_index);
2088 if (!reg)
Neale Rannsb8d44812017-11-10 06:53:54 -08002089 {
2090 (void) vnet_add_del_ip6_nd_change_event
2091 (vnm, nd_change_delete_callback,
2092 event->pid, &event->address,
2093 ip_resolver_process_node.index, IP6_ND_EVENT,
2094 ~0 /* pool index, notused */ , 0 /* is_add */ );
2095 return;
2096 }
2097
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002098 if (vl_api_can_send_msg (reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002099 {
2100 mp = vl_msg_api_alloc (sizeof (*mp));
2101 clib_memcpy (mp, event, sizeof (*mp));
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002102 vl_api_send_msg (reg, (u8 *) mp);
Neale Rannsb8d44812017-11-10 06:53:54 -08002103 }
2104 else
2105 {
2106 static f64 last_time;
2107 /*
2108 * Throttle syslog msgs.
2109 * It's pretty tempting to just revoke the registration...
2110 */
2111 if (vlib_time_now (vm) > last_time + 10.0)
2112 {
2113 clib_warning ("ip6 nd event for %U to pid %d: queue stuffed!",
2114 format_ip6_address, &event->address, event->pid);
2115 last_time = vlib_time_now (vm);
2116 }
2117 }
2118}
2119
2120static uword
2121resolver_process (vlib_main_t * vm,
2122 vlib_node_runtime_t * rt, vlib_frame_t * f)
2123{
2124 volatile f64 timeout = 100.0;
2125 volatile uword *event_data = 0;
2126
2127 while (1)
2128 {
2129 vlib_process_wait_for_event_or_clock (vm, timeout);
2130
2131 uword event_type =
2132 vlib_process_get_events (vm, (uword **) & event_data);
2133
2134 int i;
2135 switch (event_type)
2136 {
2137 case IP4_ARP_EVENT:
2138 for (i = 0; i < vec_len (event_data); i++)
2139 handle_ip4_arp_event (event_data[i]);
2140 break;
2141
2142 case IP6_ND_EVENT:
2143 for (i = 0; i < vec_len (event_data); i++)
2144 handle_ip6_nd_event (event_data[i]);
2145 break;
2146
2147 case ~0: /* timeout */
2148 break;
2149 }
2150
2151 vec_reset_length (event_data);
2152 }
2153 return 0; /* or not */
2154}
2155
2156/* *INDENT-OFF* */
2157VLIB_REGISTER_NODE (ip_resolver_process_node,static) = {
2158 .function = resolver_process,
2159 .type = VLIB_NODE_TYPE_PROCESS,
2160 .name = "ip-route-resolver-process",
2161};
2162/* *INDENT-ON* */
2163
2164static int
2165nd_change_data_callback (u32 pool_index, u8 * new_mac,
2166 u32 sw_if_index, ip6_address_t * address)
2167{
2168 vpe_api_main_t *am = &vpe_api_main;
2169 vl_api_ip6_nd_event_t *event;
2170
2171 if (pool_is_free_index (am->nd_events, pool_index))
2172 return 1;
2173
2174 event = pool_elt_at_index (am->nd_events, pool_index);
2175 if (eth_mac_equal (event->new_mac, new_mac) &&
2176 sw_if_index == ntohl (event->sw_if_index))
2177 {
2178 return 1;
2179 }
2180
2181 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2182 event->sw_if_index = htonl (sw_if_index);
2183 return 0;
2184}
2185
2186static int
2187arp_change_delete_callback (u32 pool_index, u8 * notused)
2188{
2189 vpe_api_main_t *am = &vpe_api_main;
2190
2191 if (pool_is_free_index (am->arp_events, pool_index))
2192 return 1;
2193
2194 pool_put_index (am->arp_events, pool_index);
2195 return 0;
2196}
2197
2198static int
2199nd_change_delete_callback (u32 pool_index, u8 * notused)
2200{
2201 vpe_api_main_t *am = &vpe_api_main;
2202
2203 if (pool_is_free_index (am->nd_events, pool_index))
2204 return 1;
2205
2206 pool_put_index (am->nd_events, pool_index);
2207 return 0;
2208}
2209
2210static vlib_node_registration_t wc_arp_process_node;
2211
2212enum
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002213{ WC_ARP_REPORT, WC_ND_REPORT, RA_REPORT, REPORT_MAX };
Neale Rannsb8d44812017-11-10 06:53:54 -08002214
2215static uword
2216wc_arp_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
2217{
2218 /* These cross the longjmp boundry (vlib_process_wait_for_event)
2219 * and need to be volatile - to prevent them from being optimized into
2220 * a register - which could change during suspension */
2221
2222 volatile wc_arp_report_t arp_prev = { 0 };
2223 volatile wc_nd_report_t nd_prev = { 0 };
2224 volatile f64 last_arp = vlib_time_now (vm);
2225 volatile f64 last_nd = vlib_time_now (vm);
2226
2227 while (1)
2228 {
2229 vlib_process_wait_for_event (vm);
2230 uword event_type = WC_ARP_REPORT;
2231 void *event_data = vlib_process_get_event_data (vm, &event_type);
2232
2233 f64 now = vlib_time_now (vm);
2234 int i;
2235 if (event_type == WC_ARP_REPORT)
2236 {
2237 wc_arp_report_t *arp_events = event_data;
2238 for (i = 0; i < vec_len (arp_events); i++)
2239 {
2240 /* discard dup event */
2241 if (arp_prev.ip4 == arp_events[i].ip4 &&
2242 eth_mac_equal ((u8 *) arp_prev.mac, arp_events[i].mac) &&
2243 arp_prev.sw_if_index == arp_events[i].sw_if_index &&
2244 (now - last_arp) < 10.0)
2245 {
2246 continue;
2247 }
2248 arp_prev = arp_events[i];
2249 last_arp = now;
2250 vpe_client_registration_t *reg;
2251 /* *INDENT-OFF* */
2252 pool_foreach(reg, vpe_api_main.wc_ip4_arp_events_registrations,
2253 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002254 vl_api_registration_t *vl_reg;
2255 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2256 if (reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002257 {
2258 vl_api_ip4_arp_event_t * event = vl_msg_api_alloc (sizeof *event);
2259 memset (event, 0, sizeof *event);
2260 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2261 event->client_index = reg->client_index;
2262 event->pid = reg->client_pid;
2263 event->mac_ip = 1;
2264 event->address = arp_events[i].ip4;
2265 event->sw_if_index = htonl(arp_events[i].sw_if_index);
2266 memcpy(event->new_mac, arp_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002267 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002268 }
2269 }));
2270 /* *INDENT-ON* */
2271 }
2272 }
2273 else if (event_type == WC_ND_REPORT)
2274 {
2275 wc_nd_report_t *nd_events = event_data;
2276 for (i = 0; i < vec_len (nd_events); i++)
2277 {
2278 /* discard dup event */
2279 if (ip6_address_is_equal
2280 ((ip6_address_t *) & nd_prev.ip6, &nd_events[i].ip6)
2281 && eth_mac_equal ((u8 *) nd_prev.mac, nd_events[i].mac)
2282 && nd_prev.sw_if_index == nd_events[i].sw_if_index
2283 && (now - last_nd) < 10.0)
2284 {
2285 continue;
2286 }
2287 nd_prev = nd_events[i];
2288 last_nd = now;
2289 vpe_client_registration_t *reg;
2290 /* *INDENT-OFF* */
2291 pool_foreach(reg, vpe_api_main.wc_ip6_nd_events_registrations,
2292 ({
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002293 vl_api_registration_t *vl_reg;
2294 vl_reg = vl_api_client_index_to_registration (reg->client_index);
2295 if (vl_reg && vl_api_can_send_msg (vl_reg))
Neale Rannsb8d44812017-11-10 06:53:54 -08002296 {
2297 vl_api_ip6_nd_event_t * event = vl_msg_api_alloc (sizeof *event);
2298 memset (event, 0, sizeof *event);
2299 event->_vl_msg_id = htons (VL_API_IP6_ND_EVENT);
2300 event->client_index = reg->client_index;
2301 event->pid = reg->client_pid;
2302 event->mac_ip = 1;
2303 memcpy(event->address, nd_events[i].ip6.as_u8, sizeof event->address);
2304 event->sw_if_index = htonl(nd_events[i].sw_if_index);
2305 memcpy(event->new_mac, nd_events[i].mac, sizeof event->new_mac);
Florin Corasaf0ff5a2018-01-10 08:17:03 -08002306 vl_api_send_msg (vl_reg, (u8 *) event);
Neale Rannsb8d44812017-11-10 06:53:54 -08002307 }
2308 }));
2309 /* *INDENT-ON* */
2310 }
2311 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002312 else if (event_type == RA_REPORT)
2313 {
2314 ra_report_t *ra_events = event_data;
2315 for (i = 0; i < vec_len (ra_events); i++)
2316 {
2317 vpe_client_registration_t *reg;
2318 /* *INDENT-OFF* */
2319 pool_foreach(reg, vpe_api_main.ip6_ra_events_registrations,
2320 ({
2321 vl_api_registration_t *vl_reg;
2322 vl_reg =
2323 vl_api_client_index_to_registration (reg->client_index);
2324 if (vl_reg && vl_api_can_send_msg (vl_reg))
2325 {
2326 u32 event_size =
2327 sizeof (vl_api_ip6_ra_event_t) +
2328 vec_len (ra_events[i].prefixes) *
2329 sizeof (vl_api_ip6_ra_prefix_info_t);
2330 vl_api_ip6_ra_event_t *event =
2331 vl_msg_api_alloc (event_size);
2332 memset (event, 0, event_size);
2333 event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT);
2334 event->client_index = reg->client_index;
2335 event->pid = reg->client_pid;
2336
2337 event->sw_if_index = clib_host_to_net_u32 (ra_events[i].sw_if_index);
2338
2339 memcpy (event->router_address, ra_events[i].router_address, 16);
2340
2341 event->current_hop_limit = ra_events[i].current_hop_limit;
2342 event->flags = ra_events[i].flags;
2343 event->router_lifetime_in_sec =
2344 clib_host_to_net_u16 (ra_events
2345 [i].router_lifetime_in_sec);
2346 event->neighbor_reachable_time_in_msec =
2347 clib_host_to_net_u32 (ra_events
2348 [i].neighbor_reachable_time_in_msec);
2349 event->time_in_msec_between_retransmitted_neighbor_solicitations
2350 =
2351 clib_host_to_net_u32 (ra_events
2352 [i].time_in_msec_between_retransmitted_neighbor_solicitations);
2353
2354 event->n_prefixes =
2355 clib_host_to_net_u32 (vec_len (ra_events[i].prefixes));
2356 vl_api_ip6_ra_prefix_info_t *prefix =
2357 (typeof (prefix)) event->prefixes;
2358 u32 j;
2359 for (j = 0; j < vec_len (ra_events[i].prefixes); j++)
2360 {
2361 ra_report_prefix_info_t *info =
2362 &ra_events[i].prefixes[j];
2363 memcpy (prefix->dst_address, info->dst_address.as_u8,
2364 16);
2365 prefix->dst_address_length = info->dst_address_length;
2366 prefix->flags = info->flags;
2367 prefix->valid_time =
2368 clib_host_to_net_u32 (info->valid_time);
2369 prefix->preferred_time =
2370 clib_host_to_net_u32 (info->preferred_time);
2371 prefix++;
2372 }
2373
2374 vl_api_send_msg (vl_reg, (u8 *) event);
2375 }
2376 }));
2377 /* *INDENT-ON* */
2378 }
2379 }
Neale Rannsb8d44812017-11-10 06:53:54 -08002380 vlib_process_put_event_data (vm, event_data);
2381 }
2382
2383 return 0;
2384}
2385
2386/* *INDENT-OFF* */
2387VLIB_REGISTER_NODE (wc_arp_process_node,static) = {
2388 .function = wc_arp_process,
2389 .type = VLIB_NODE_TYPE_PROCESS,
2390 .name = "wildcard-ip4-arp-publisher-process",
2391};
2392/* *INDENT-ON* */
2393
2394static int
2395arp_change_data_callback (u32 pool_index, u8 * new_mac,
2396 u32 sw_if_index, u32 address)
2397{
2398 vpe_api_main_t *am = &vpe_api_main;
2399 vl_api_ip4_arp_event_t *event;
2400
2401 if (pool_is_free_index (am->arp_events, pool_index))
2402 return 1;
2403
2404 event = pool_elt_at_index (am->arp_events, pool_index);
2405 if (eth_mac_equal (event->new_mac, new_mac) &&
2406 sw_if_index == ntohl (event->sw_if_index))
2407 {
2408 return 1;
2409 }
2410
2411 clib_memcpy (event->new_mac, new_mac, sizeof (event->new_mac));
2412 event->sw_if_index = htonl (sw_if_index);
2413 return 0;
2414}
2415
2416static void
2417vl_api_want_ip4_arp_events_t_handler (vl_api_want_ip4_arp_events_t * mp)
2418{
2419 vpe_api_main_t *am = &vpe_api_main;
2420 vnet_main_t *vnm = vnet_get_main ();
2421 vl_api_want_ip4_arp_events_reply_t *rmp;
2422 int rv = 0;
2423
2424 if (mp->address == 0)
2425 {
2426 uword *p =
2427 hash_get (am->wc_ip4_arp_events_registration_hash, mp->client_index);
2428 vpe_client_registration_t *rp;
2429 if (p)
2430 {
2431 if (mp->enable_disable)
2432 {
2433 clib_warning ("pid %d: already enabled...", mp->pid);
2434 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2435 goto reply;
2436 }
2437 else
2438 {
2439 rp =
2440 pool_elt_at_index (am->wc_ip4_arp_events_registrations, p[0]);
2441 pool_put (am->wc_ip4_arp_events_registrations, rp);
2442 hash_unset (am->wc_ip4_arp_events_registration_hash,
2443 mp->client_index);
2444 if (pool_elts (am->wc_ip4_arp_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002445 wc_arp_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002446 goto reply;
2447 }
2448 }
2449 if (mp->enable_disable == 0)
2450 {
2451 clib_warning ("pid %d: already disabled...", mp->pid);
2452 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2453 goto reply;
2454 }
2455 pool_get (am->wc_ip4_arp_events_registrations, rp);
2456 rp->client_index = mp->client_index;
2457 rp->client_pid = mp->pid;
2458 hash_set (am->wc_ip4_arp_events_registration_hash, rp->client_index,
2459 rp - am->wc_ip4_arp_events_registrations);
2460 wc_arp_set_publisher_node (wc_arp_process_node.index, WC_ARP_REPORT);
2461 goto reply;
2462 }
2463
2464 if (mp->enable_disable)
2465 {
2466 vl_api_ip4_arp_event_t *event;
2467 pool_get (am->arp_events, event);
2468 rv = vnet_add_del_ip4_arp_change_event
2469 (vnm, arp_change_data_callback,
2470 mp->pid, &mp->address /* addr, in net byte order */ ,
2471 ip_resolver_process_node.index,
2472 IP4_ARP_EVENT, event - am->arp_events, 1 /* is_add */ );
2473
2474 if (rv)
2475 {
2476 pool_put (am->arp_events, event);
2477 goto reply;
2478 }
2479 memset (event, 0, sizeof (*event));
2480
2481 /* Python API expects events to have no context */
2482 event->_vl_msg_id = htons (VL_API_IP4_ARP_EVENT);
2483 event->client_index = mp->client_index;
2484 event->address = mp->address;
2485 event->pid = mp->pid;
2486 if (mp->address == 0)
2487 event->mac_ip = 1;
2488 }
2489 else
2490 {
2491 rv = vnet_add_del_ip4_arp_change_event
2492 (vnm, arp_change_delete_callback,
2493 mp->pid, &mp->address /* addr, in net byte order */ ,
2494 ip_resolver_process_node.index,
2495 IP4_ARP_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2496 }
2497reply:
2498 REPLY_MACRO (VL_API_WANT_IP4_ARP_EVENTS_REPLY);
2499}
2500
2501static void
2502vl_api_want_ip6_nd_events_t_handler (vl_api_want_ip6_nd_events_t * mp)
2503{
2504 vpe_api_main_t *am = &vpe_api_main;
2505 vnet_main_t *vnm = vnet_get_main ();
2506 vl_api_want_ip6_nd_events_reply_t *rmp;
2507 int rv = 0;
2508
2509 if (ip6_address_is_zero ((ip6_address_t *) mp->address))
2510 {
2511 uword *p =
2512 hash_get (am->wc_ip6_nd_events_registration_hash, mp->client_index);
2513 vpe_client_registration_t *rp;
2514 if (p)
2515 {
2516 if (mp->enable_disable)
2517 {
2518 clib_warning ("pid %d: already enabled...", mp->pid);
2519 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2520 goto reply;
2521 }
2522 else
2523 {
2524 rp =
2525 pool_elt_at_index (am->wc_ip6_nd_events_registrations, p[0]);
2526 pool_put (am->wc_ip6_nd_events_registrations, rp);
2527 hash_unset (am->wc_ip6_nd_events_registration_hash,
2528 mp->client_index);
2529 if (pool_elts (am->wc_ip6_nd_events_registrations) == 0)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002530 wc_nd_set_publisher_node (~0, REPORT_MAX);
Neale Rannsb8d44812017-11-10 06:53:54 -08002531 goto reply;
2532 }
2533 }
2534 if (mp->enable_disable == 0)
2535 {
2536 clib_warning ("pid %d: already disabled...", mp->pid);
2537 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2538 goto reply;
2539 }
2540 pool_get (am->wc_ip6_nd_events_registrations, rp);
2541 rp->client_index = mp->client_index;
2542 rp->client_pid = mp->pid;
2543 hash_set (am->wc_ip6_nd_events_registration_hash, rp->client_index,
2544 rp - am->wc_ip6_nd_events_registrations);
2545 wc_nd_set_publisher_node (wc_arp_process_node.index, WC_ND_REPORT);
2546 goto reply;
2547 }
2548
2549 if (mp->enable_disable)
2550 {
2551 vl_api_ip6_nd_event_t *event;
2552 pool_get (am->nd_events, event);
2553
2554 rv = vnet_add_del_ip6_nd_change_event
2555 (vnm, nd_change_data_callback,
2556 mp->pid, mp->address /* addr, in net byte order */ ,
2557 ip_resolver_process_node.index,
2558 IP6_ND_EVENT, event - am->nd_events, 1 /* is_add */ );
2559
2560 if (rv)
2561 {
2562 pool_put (am->nd_events, event);
2563 goto reply;
2564 }
2565 memset (event, 0, sizeof (*event));
2566
2567 event->_vl_msg_id = ntohs (VL_API_IP6_ND_EVENT);
2568 event->client_index = mp->client_index;
2569 clib_memcpy (event->address, mp->address, sizeof event->address);
2570 event->pid = mp->pid;
2571 }
2572 else
2573 {
2574 rv = vnet_add_del_ip6_nd_change_event
2575 (vnm, nd_change_delete_callback,
2576 mp->pid, mp->address /* addr, in net byte order */ ,
2577 ip_resolver_process_node.index,
2578 IP6_ND_EVENT, ~0 /* pool index */ , 0 /* is_add */ );
2579 }
2580reply:
2581 REPLY_MACRO (VL_API_WANT_IP6_ND_EVENTS_REPLY);
2582}
2583
2584static void
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002585vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
2586{
2587 vpe_api_main_t *am = &vpe_api_main;
2588 vl_api_want_ip6_ra_events_reply_t *rmp;
2589 int rv = 0;
2590
2591 uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
2592 vpe_client_registration_t *rp;
2593 if (p)
2594 {
2595 if (mp->enable_disable)
2596 {
2597 clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
2598 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2599 goto reply;
2600 }
2601 else
2602 {
2603 rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
2604 pool_put (am->ip6_ra_events_registrations, rp);
2605 hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
2606 if (pool_elts (am->ip6_ra_events_registrations) == 0)
2607 ra_set_publisher_node (~0, REPORT_MAX);
2608 goto reply;
2609 }
2610 }
2611 if (mp->enable_disable == 0)
2612 {
2613 clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
2614 rv = VNET_API_ERROR_INVALID_REGISTRATION;
2615 goto reply;
2616 }
2617 pool_get (am->ip6_ra_events_registrations, rp);
2618 rp->client_index = mp->client_index;
2619 rp->client_pid = ntohl (mp->pid);
2620 hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
2621 rp - am->ip6_ra_events_registrations);
2622 ra_set_publisher_node (wc_arp_process_node.index, RA_REPORT);
2623
2624reply:
2625 REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
2626}
2627
2628static void
Neale Rannsb8d44812017-11-10 06:53:54 -08002629vl_api_proxy_arp_add_del_t_handler (vl_api_proxy_arp_add_del_t * mp)
2630{
2631 vl_api_proxy_arp_add_del_reply_t *rmp;
2632 u32 fib_index;
2633 int rv;
2634 ip4_main_t *im = &ip4_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002635 int vnet_proxy_arp_add_del (ip4_address_t * lo_addr,
2636 ip4_address_t * hi_addr,
2637 u32 fib_index, int is_del);
2638 uword *p;
2639
Ole Troan3288ed72017-12-06 17:00:05 +01002640 stats_dslock_with_hint (1 /* release hint */ , 6 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002641
2642 p = hash_get (im->fib_index_by_table_id, ntohl (mp->vrf_id));
2643
2644 if (!p)
2645 {
2646 rv = VNET_API_ERROR_NO_SUCH_FIB;
2647 goto out;
2648 }
2649
2650 fib_index = p[0];
2651
2652 rv = vnet_proxy_arp_add_del ((ip4_address_t *) mp->low_address,
2653 (ip4_address_t *) mp->hi_address,
2654 fib_index, mp->is_add == 0);
2655
2656out:
Ole Troan3288ed72017-12-06 17:00:05 +01002657 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002658 REPLY_MACRO (VL_API_PROXY_ARP_ADD_DEL_REPLY);
2659}
2660
2661static void
2662 vl_api_proxy_arp_intfc_enable_disable_t_handler
2663 (vl_api_proxy_arp_intfc_enable_disable_t * mp)
2664{
2665 int rv = 0;
2666 vnet_main_t *vnm = vnet_get_main ();
2667 vl_api_proxy_arp_intfc_enable_disable_reply_t *rmp;
2668
2669 VALIDATE_SW_IF_INDEX (mp);
2670
2671 vnet_sw_interface_t *si =
2672 vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
2673
2674 ASSERT (si);
2675
2676 if (mp->enable_disable)
2677 si->flags |= VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2678 else
2679 si->flags &= ~VNET_SW_INTERFACE_FLAG_PROXY_ARP;
2680
2681 BAD_SW_IF_INDEX_LABEL;
2682
2683 REPLY_MACRO (VL_API_PROXY_ARP_INTFC_ENABLE_DISABLE_REPLY);
2684}
2685
2686static int
2687ip4_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2688{
2689 vnet_main_t *vnm = vnet_get_main ();
2690 vnet_interface_main_t *im = &vnm->interface_main;
2691 ip4_main_t *im4 = &ip4_main;
2692 static u32 *sw_if_indices_to_shut;
Neale Rannsb8d44812017-11-10 06:53:54 -08002693 fib_table_t *fib_table;
2694 ip4_fib_t *fib;
2695 u32 sw_if_index;
2696 int i;
2697 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2698 u32 target_fib_id = ntohl (mp->vrf_id);
2699
Ole Troan3288ed72017-12-06 17:00:05 +01002700 stats_dslock_with_hint (1 /* release hint */ , 8 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002701
2702 /* *INDENT-OFF* */
2703 pool_foreach (fib_table, im4->fibs,
2704 ({
2705 vnet_sw_interface_t * si;
2706
2707 fib = pool_elt_at_index (im4->v4_fibs, fib_table->ft_index);
2708
2709 if (fib->table_id != target_fib_id)
2710 continue;
2711
2712 /* remove any mpls encap/decap labels */
2713 mpls_fib_reset_labels (fib->table_id);
2714
2715 /* remove any proxy arps in this fib */
2716 vnet_proxy_arp_fib_reset (fib->table_id);
2717
2718 /* Set the flow hash for this fib to the default */
2719 vnet_set_ip4_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2720
2721 vec_reset_length (sw_if_indices_to_shut);
2722
2723 /* Shut down interfaces in this FIB / clean out intfc routes */
2724 pool_foreach (si, im->sw_interfaces,
2725 ({
2726 u32 sw_if_index = si->sw_if_index;
2727
2728 if (sw_if_index < vec_len (im4->fib_index_by_sw_if_index)
2729 && (im4->fib_index_by_sw_if_index[si->sw_if_index] ==
2730 fib->index))
2731 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2732 }));
2733
2734 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2735 sw_if_index = sw_if_indices_to_shut[i];
2736
2737 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2738 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2739 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2740 }
2741
2742 fib_table_flush(fib->index, FIB_PROTOCOL_IP4, FIB_SOURCE_API);
2743
2744 rv = 0;
2745 break;
2746 })); /* pool_foreach (fib) */
2747 /* *INDENT-ON* */
2748
Ole Troan3288ed72017-12-06 17:00:05 +01002749 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002750 return rv;
2751}
2752
2753static int
2754ip6_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2755{
2756 vnet_main_t *vnm = vnet_get_main ();
2757 vnet_interface_main_t *im = &vnm->interface_main;
2758 ip6_main_t *im6 = &ip6_main;
Neale Rannsb8d44812017-11-10 06:53:54 -08002759 static u32 *sw_if_indices_to_shut;
2760 fib_table_t *fib_table;
2761 ip6_fib_t *fib;
2762 u32 sw_if_index;
2763 int i;
2764 int rv = VNET_API_ERROR_NO_SUCH_FIB;
2765 u32 target_fib_id = ntohl (mp->vrf_id);
2766
Ole Troan3288ed72017-12-06 17:00:05 +01002767 stats_dslock_with_hint (1 /* release hint */ , 9 /* tag */ );
Neale Rannsb8d44812017-11-10 06:53:54 -08002768
2769 /* *INDENT-OFF* */
2770 pool_foreach (fib_table, im6->fibs,
2771 ({
2772 vnet_sw_interface_t * si;
2773
2774 fib = pool_elt_at_index (im6->v6_fibs, fib_table->ft_index);
2775
2776 if (fib->table_id != target_fib_id)
2777 continue;
2778
2779 vec_reset_length (sw_if_indices_to_shut);
2780
2781 /* Set the flow hash for this fib to the default */
2782 vnet_set_ip6_flow_hash (fib->table_id, IP_FLOW_HASH_DEFAULT);
2783
2784 /* Shut down interfaces in this FIB / clean out intfc routes */
2785 pool_foreach (si, im->sw_interfaces,
2786 ({
2787 if (im6->fib_index_by_sw_if_index[si->sw_if_index] ==
2788 fib->index)
2789 vec_add1 (sw_if_indices_to_shut, si->sw_if_index);
2790 }));
2791
2792 for (i = 0; i < vec_len (sw_if_indices_to_shut); i++) {
2793 sw_if_index = sw_if_indices_to_shut[i];
2794
2795 u32 flags = vnet_sw_interface_get_flags (vnm, sw_if_index);
2796 flags &= ~(VNET_SW_INTERFACE_FLAG_ADMIN_UP);
2797 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
2798 }
2799
2800 fib_table_flush(fib->index, FIB_PROTOCOL_IP6, FIB_SOURCE_API);
2801
2802 rv = 0;
2803 break;
2804 })); /* pool_foreach (fib) */
2805 /* *INDENT-ON* */
2806
Ole Troan3288ed72017-12-06 17:00:05 +01002807 stats_dsunlock ();
Neale Rannsb8d44812017-11-10 06:53:54 -08002808 return rv;
2809}
2810
2811static void
2812vl_api_reset_fib_t_handler (vl_api_reset_fib_t * mp)
2813{
2814 int rv;
2815 vl_api_reset_fib_reply_t *rmp;
2816
2817 if (mp->is_ipv6)
2818 rv = ip6_reset_fib_t_handler (mp);
2819 else
2820 rv = ip4_reset_fib_t_handler (mp);
2821
2822 REPLY_MACRO (VL_API_RESET_FIB_REPLY);
2823}
2824
2825static void
2826vl_api_set_arp_neighbor_limit_t_handler (vl_api_set_arp_neighbor_limit_t * mp)
2827{
2828 int rv;
2829 vl_api_set_arp_neighbor_limit_reply_t *rmp;
2830 vnet_main_t *vnm = vnet_get_main ();
2831 clib_error_t *error;
2832
2833 vnm->api_errno = 0;
2834
2835 if (mp->is_ipv6)
2836 error = ip6_set_neighbor_limit (ntohl (mp->arp_neighbor_limit));
2837 else
2838 error = ip4_set_arp_limit (ntohl (mp->arp_neighbor_limit));
2839
2840 if (error)
2841 {
2842 clib_error_report (error);
2843 rv = VNET_API_ERROR_UNSPECIFIED;
2844 }
2845 else
2846 {
2847 rv = vnm->api_errno;
2848 }
2849
2850 REPLY_MACRO (VL_API_SET_ARP_NEIGHBOR_LIMIT_REPLY);
2851}
2852
Klement Sekera75e7d132017-09-20 08:26:30 +02002853void
2854vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
2855{
2856 vl_api_ip_reassembly_set_reply_t *rmp;
2857 int rv = 0;
2858 if (mp->is_ip6)
2859 {
2860 rv = ip6_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
2861 clib_net_to_host_u32 (mp->max_reassemblies),
2862 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
2863 }
2864 else
2865 {
2866 rv = ip4_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
2867 clib_net_to_host_u32 (mp->max_reassemblies),
2868 clib_net_to_host_u32 (mp->expire_walk_interval_ms));
2869 }
2870
2871 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
2872}
2873
2874void
2875vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
2876{
2877 unix_shared_memory_queue_t *q;
2878
2879 q = vl_api_client_index_to_input_queue (mp->client_index);
2880
2881 if (q == 0)
2882 return;
2883
2884 vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
2885 memset (rmp, 0, sizeof (*rmp));
2886 rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
2887 rmp->context = mp->context;
2888 rmp->retval = 0;
2889 if (mp->is_ip6)
2890 {
2891 rmp->is_ip6 = 1;
2892 ip6_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
2893 &rmp->expire_walk_interval_ms);
2894 }
2895 else
2896 {
2897 rmp->is_ip6 = 0;
2898 ip4_reass_get (&rmp->timeout_ms, &rmp->max_reassemblies,
2899 &rmp->expire_walk_interval_ms);
2900 }
2901 rmp->timeout_ms = clib_host_to_net_u32 (rmp->timeout_ms);
2902 rmp->max_reassemblies = clib_host_to_net_u32 (rmp->max_reassemblies);
2903 rmp->expire_walk_interval_ms =
2904 clib_host_to_net_u32 (rmp->expire_walk_interval_ms);
2905 vl_msg_api_send_shmem (q, (u8 *) & rmp);
2906}
2907
Klement Sekera4c533132018-02-22 11:41:12 +01002908void
2909 vl_api_ip_reassembly_enable_disable_t_handler
2910 (vl_api_ip_reassembly_enable_disable_t * mp)
2911{
2912 vl_api_ip_reassembly_enable_disable_reply_t *rmp;
2913 int rv = 0;
2914 rv = ip4_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
2915 mp->enable_ip4);
2916 if (0 == rv)
2917 {
2918 rv = ip6_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
2919 mp->enable_ip6);
2920 }
2921
2922 REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
2923}
2924
Dave Barachb5e8a772016-12-06 12:04:42 -05002925#define vl_msg_name_crc_list
2926#include <vnet/ip/ip.api.h>
2927#undef vl_msg_name_crc_list
2928
2929static void
2930setup_message_id_table (api_main_t * am)
2931{
2932#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
2933 foreach_vl_msg_name_crc_ip;
2934#undef _
2935}
2936
2937static clib_error_t *
2938ip_api_hookup (vlib_main_t * vm)
2939{
2940 api_main_t *am = &api_main;
2941
2942#define _(N,n) \
2943 vl_msg_api_set_handlers(VL_API_##N, #n, \
2944 vl_api_##n##_t_handler, \
2945 vl_noop_handler, \
2946 vl_api_##n##_t_endian, \
2947 vl_api_##n##_t_print, \
2948 sizeof(vl_api_##n##_t), 1);
2949 foreach_ip_api_msg;
2950#undef _
2951
2952 /*
2953 * Set up the (msg_name, crc, message-id) table
2954 */
2955 setup_message_id_table (am);
2956
2957 return 0;
2958}
2959
2960VLIB_API_INIT_FUNCTION (ip_api_hookup);
2961
2962/*
2963 * fd.io coding-style-patch-verification: ON
2964 *
2965 * Local Variables:
2966 * eval: (c-set-style "gnu")
2967 * End:
2968 */