blob: 5b8085d556d1da086c5c1f0f428edc50d85d3d3d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * ip/ip6_neighbor.c: IP6 neighbor handling
3 *
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/ip/ip.h>
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010019#include <vnet/ip/ip6_neighbor.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ethernet/ethernet.h>
21#include <vppinfra/mhash.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022#include <vnet/adj/adj.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000023#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010024#include <vnet/fib/fib_table.h>
25#include <vnet/fib/ip6_fib.h>
Neale Ranns4008ac92017-02-13 23:20:04 -080026#include <vnet/mfib/ip6_mfib.h>
Neale Ranns53da2212018-02-24 02:11:19 -080027#include <vnet/ip/ip6_ll_table.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070028#include <vnet/l2/l2_input.h>
Neale Ranns37029302018-08-10 05:30:06 -070029#include <vlibmemory/api.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Billy McFall0683c9c2016-10-13 08:27:31 -040031/**
32 * @file
33 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
34 *
35 * The files contains the API and CLI code for managing IPv6 neighbor
36 * adjacency tables and neighbor discovery logic.
37 */
38
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010039/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#define ETHER_MAC_ADDR_LEN 6
41
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010042/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050043typedef struct
44{
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 /* basic advertised information */
46 ip6_address_t prefix;
47 u8 prefix_len;
48 int adv_on_link_flag;
49 int adv_autonomous_flag;
50 u32 adv_valid_lifetime_in_secs;
51 u32 adv_pref_lifetime_in_secs;
52
53 /* advertised values are computed from these times if decrementing */
54 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050055 f64 pref_lifetime_expires;
56
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 /* local information */
58 int enabled;
59 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050060 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
62#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
63#define DEF_ADV_VALID_LIFETIME 2592000
64#define DEF_ADV_PREF_LIFETIME 604800
65
66 /* extensions are added here, mobile, DNS etc.. */
67} ip6_radv_prefix_t;
68
69
Dave Barachd7cb1b52016-12-09 09:52:16 -050070typedef struct
71{
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 /* group information */
73 u8 type;
74 ip6_address_t mcast_address;
75 u16 num_sources;
76 ip6_address_t *mcast_source_address_pool;
77} ip6_mldp_group_t;
78
79/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050080typedef struct
81{
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050084 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 int adv_managed_flag;
86 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050087 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 u32 adv_neighbor_reachable_time_in_msec;
89 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
90
91 /* mtu option */
92 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050093
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050095 u8 link_layer_address[8];
96 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050099 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 /* Hash table mapping address to index in interface advertised prefix pool. */
102 mhash_t address_to_prefix_index;
103
104 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500105 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 /* Hash table mapping address to index in mldp address pool. */
108 mhash_t address_to_mldp_index;
109
110 /* local information */
111 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112 int send_radv; /* radv on/off on this interface - set by config */
113 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 int send_unicast;
115 int adv_link_layer_address;
116 int prefix_option;
117 int failed_device_check;
118 int all_routers_mcast;
119 u32 seed;
120 u64 randomizer;
121 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000122 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500123
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 /* timing information */
125#define DEF_MAX_RADV_INTERVAL 200
126#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
127#define DEF_CURR_HOP_LIMIT 64
128#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
129#define MAX_DEF_RTR_LIFETIME 9000
130
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
132#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
133#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
134#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
135#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136
137 f64 max_radv_interval;
138 f64 min_radv_interval;
139 f64 min_delay_between_radv;
140 f64 max_delay_between_radv;
141 f64 max_rtr_default_lifetime;
142
143 f64 last_radv_time;
144 f64 last_multicast_time;
145 f64 next_multicast_time;
146
147
148 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500149 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 u32 initial_adverts_sent;
151
152 /* stats */
153 u32 n_advertisements_sent;
154 u32 n_solicitations_rcvd;
155 u32 n_solicitations_dropped;
156
157 /* Link local address to use (defaults to underlying physical for logical interfaces */
158 ip6_address_t link_local_address;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100159
160 /* router solicitations sending state */
161 u8 keep_sending_rs; /* when true then next fields are valid */
162 icmp6_send_router_solicitation_params_t params;
163 f64 sleep_interval;
164 f64 due_time;
165 u32 n_left;
166 f64 start_time;
167 vlib_buffer_t *buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168} ip6_radv_t;
169
Dave Barachd7cb1b52016-12-09 09:52:16 -0500170typedef struct
171{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 u32 next_index;
173 uword node_index;
174 uword type_opaque;
175 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400176 /* Used for nd event notification only */
Neale Ranns37029302018-08-10 05:30:06 -0700177 ip6_nd_change_event_cb_t data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400178 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179} pending_resolution_t;
180
181
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182typedef struct
183{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500185 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
187 /* lite beer "glean" adjacency handling */
188 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500189 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
John Lo1edfba92016-08-27 01:11:57 -0400191 /* Mac address change notification */
192 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400194
Dave Barachd7cb1b52016-12-09 09:52:16 -0500195 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barachd7cb1b52016-12-09 09:52:16 -0500197 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
199 mhash_t neighbor_index_by_key;
200
Dave Barachd7cb1b52016-12-09 09:52:16 -0500201 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Dave Barachd7cb1b52016-12-09 09:52:16 -0500203 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
205 /* Neighbor attack mitigation */
206 u32 limit_neighbor_cache_size;
207 u32 neighbor_delete_rotor;
208
Eyal Baric125ecc2017-09-20 11:29:17 +0300209 /* Wildcard nd report publisher */
210 uword wc_ip6_nd_publisher_node;
211 uword wc_ip6_nd_publisher_et;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100212
213 /* Router advertisement report publisher */
214 uword ip6_ra_publisher_node;
215 uword ip6_ra_publisher_et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216} ip6_neighbor_main_t;
217
Neale Ranns3f844d02017-02-18 00:03:54 -0800218/* ipv6 neighbor discovery - timer/event types */
219typedef enum
220{
221 ICMP6_ND_EVENT_INIT,
222} ip6_icmp_neighbor_discovery_event_type_t;
223
224typedef union
225{
226 u32 add_del_swindex;
227 struct
228 {
229 u32 up_down_swindex;
230 u32 fib_index;
231 } up_down_event;
232} ip6_icmp_neighbor_discovery_event_data_t;
233
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234static ip6_neighbor_main_t ip6_neighbor_main;
Juraj Sloboda52574522018-05-03 10:03:50 +0200235ip6_neighbor_public_main_t ip6_neighbor_public_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Eyal Baric125ecc2017-09-20 11:29:17 +0300238static void wc_nd_signal_report (wc_nd_report_t * r);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100239static void ra_signal_report (ra_report_t * r);
Eyal Baric125ecc2017-09-20 11:29:17 +0300240
Juraj Sloboda81119e82018-05-25 14:02:20 +0200241ip6_address_t
242ip6_neighbor_get_link_local_address (u32 sw_if_index)
243{
244 static ip6_address_t empty_address = { {0} };
245 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
246 ip6_radv_t *radv_info;
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200247 u32 ri = ~0;
Juraj Sloboda81119e82018-05-25 14:02:20 +0200248
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200249 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index)
250 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Juraj Sloboda81119e82018-05-25 14:02:20 +0200251 if (ri == ~0)
252 {
253 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
254 return empty_address;
255 }
256 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
257 if (radv_info == NULL)
258 {
259 clib_warning ("Internal error");
260 return empty_address;
261 }
262 return radv_info->link_local_address;
263}
264
Eyal Baric125ecc2017-09-20 11:29:17 +0300265/**
266 * @brief publish wildcard arp event
Jim Thompsonf324dec2019-04-08 03:22:21 -0500267 * @param sw_if_index The interface on which the ARP entries are acted
Eyal Baric125ecc2017-09-20 11:29:17 +0300268 */
269static int
Neale Ranns37029302018-08-10 05:30:06 -0700270vnet_nd_wc_publish (u32 sw_if_index,
271 const mac_address_t * mac, const ip6_address_t * ip6)
Eyal Baric125ecc2017-09-20 11:29:17 +0300272{
273 wc_nd_report_t r = {
274 .sw_if_index = sw_if_index,
275 .ip6 = *ip6,
Neale Ranns37029302018-08-10 05:30:06 -0700276 .mac = *mac,
Eyal Baric125ecc2017-09-20 11:29:17 +0300277 };
Eyal Baric125ecc2017-09-20 11:29:17 +0300278
Eyal Baric125ecc2017-09-20 11:29:17 +0300279 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
280 return 0;
281}
282
283static void
284wc_nd_signal_report (wc_nd_report_t * r)
285{
286 vlib_main_t *vm = vlib_get_main ();
287 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
288 uword ni = nm->wc_ip6_nd_publisher_node;
289 uword et = nm->wc_ip6_nd_publisher_et;
290
291 if (ni == (uword) ~ 0)
292 return;
293 wc_nd_report_t *q =
294 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
295
296 *q = *r;
297}
298
299void
300wc_nd_set_publisher_node (uword node_index, uword event_type)
301{
302 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
303 nm->wc_ip6_nd_publisher_node = node_index;
304 nm->wc_ip6_nd_publisher_et = event_type;
305}
306
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100307static int
308ra_publish (ra_report_t * r)
309{
310 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
311 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
312 return 0;
313}
314
315static void
316ra_signal_report (ra_report_t * r)
317{
318 vlib_main_t *vm = vlib_get_main ();
319 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
320 uword ni = nm->ip6_ra_publisher_node;
321 uword et = nm->ip6_ra_publisher_et;
322
323 if (ni == (uword) ~ 0)
324 return;
325 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
326
327 *q = *r;
328}
329
330void
331ra_set_publisher_node (uword node_index, uword event_type)
332{
333 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
334 nm->ip6_ra_publisher_node = node_index;
335 nm->ip6_ra_publisher_et = event_type;
336}
337
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338static u8 *
339format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500341 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
342 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
343 vnet_main_t *vnm = vnet_get_main ();
344 vnet_sw_interface_t *si;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Dave Barachd7cb1b52016-12-09 09:52:16 -0500346 if (!n)
shubing guo30746292018-08-10 15:51:44 +0800347 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500348 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100349
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351
Benoît Ganneadbaf7b2019-07-10 15:40:33 +0200352 return format (s, "%=12U%=45U%=6U%=20U%=40U",
353 format_vlib_time, vm, n->time_last_updated,
354 format_ip6_address, &n->key.ip6_address,
355 format_ip_neighbor_flags, n->flags,
356 format_mac_address_t, &n->mac,
357 format_vnet_sw_interface_name, vnm, si);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358}
359
Neale Ranns15002542017-09-10 04:39:11 -0700360static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700361ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700362{
363 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
364 {
Neale Ranns53da2212018-02-24 02:11:19 -0800365 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
366 {
367 ip6_ll_prefix_t pfx = {
368 .ilp_addr = n->key.ip6_address,
369 .ilp_sw_if_index = n->key.sw_if_index,
370 };
371 ip6_ll_table_entry_delete (&pfx);
372 }
373 else
374 {
375 fib_prefix_t pfx = {
376 .fp_len = 128,
377 .fp_proto = FIB_PROTOCOL_IP6,
378 .fp_addr.ip6 = n->key.ip6_address,
379 };
380 fib_table_entry_path_remove (fib_index,
381 &pfx,
382 FIB_SOURCE_ADJ,
383 DPO_PROTO_IP6,
384 &pfx.fp_addr,
385 n->key.sw_if_index, ~0,
386 1, FIB_ROUTE_PATH_FLAG_NONE);
387 }
Neale Ranns15002542017-09-10 04:39:11 -0700388 }
389}
390
Dave Barachd7cb1b52016-12-09 09:52:16 -0500391typedef struct
392{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 u8 is_add;
Neale Ranns37029302018-08-10 05:30:06 -0700394 ip_neighbor_flags_t flags;
395 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396 u32 sw_if_index;
397 ip6_address_t addr;
398} ip6_neighbor_set_unset_rpc_args_t;
399
Dave Barachd7cb1b52016-12-09 09:52:16 -0500400static void ip6_neighbor_set_unset_rpc_callback
401 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403static void set_unset_ip6_neighbor_rpc
404 (vlib_main_t * vm,
405 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700406 const ip6_address_t * a,
Neale Ranns37029302018-08-10 05:30:06 -0700407 const mac_address_t * mac, int is_add, ip_neighbor_flags_t flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408{
409 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500410 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500411
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 args.sw_if_index = sw_if_index;
413 args.is_add = is_add;
Neale Ranns37029302018-08-10 05:30:06 -0700414 args.flags = flags;
415 ip6_address_copy (&args.addr, a);
416 mac_address_copy (&args.mac, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500417
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500419 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100422static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500423ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100424{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500425 icmp6_neighbor_solicitation_header_t *h;
426 vnet_main_t *vnm = vnet_get_main ();
427 ip6_main_t *im = &ip6_main;
428 ip_interface_address_t *ia;
429 ip6_address_t *dst, *src;
430 vnet_hw_interface_t *hi;
431 vnet_sw_interface_t *si;
432 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100433 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500434 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100435 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100436
Dave Barachd7cb1b52016-12-09 09:52:16 -0500437 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100438
Dave Barachd7cb1b52016-12-09 09:52:16 -0500439 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100440 dst = &adj->sub_type.nbr.next_hop.ip6;
441
442 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100443 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100444 return;
445 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500446 src = ip6_interface_address_matching_destination (im, dst,
447 adj->rewrite_header.
448 sw_if_index, &ia);
449 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100450 {
451 return;
452 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100453
Dave Barachd7cb1b52016-12-09 09:52:16 -0500454 h = vlib_packet_template_get_packet (vm,
455 &im->discover_neighbor_packet_template,
456 &bi);
John Lo084606b2018-06-19 15:27:48 -0400457 if (!h)
458 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100459
Dave Barachd7cb1b52016-12-09 09:52:16 -0500460 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100461
462 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
463 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
464 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
465 h->ip.src_address = src[0];
466 h->neighbor.target_address = dst[0];
467
468 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500469 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100470
471 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500472 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
473 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100474
475 b = vlib_get_buffer (vm, bi);
476 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500477 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100478
479 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500480 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
481 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100482
483 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500484 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
485 u32 *to_next = vlib_frame_vector_args (f);
486 to_next[0] = bi;
487 f->n_vectors = 1;
488 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100489 }
490}
491
492static void
493ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
494{
495 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
496 ethernet_build_rewrite (vnet_get_main (),
497 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500498 adj_get_link_type (ai),
Neale Ranns37029302018-08-10 05:30:06 -0700499 nbr->mac.bytes));
Neale Rannsb80c5362016-10-08 13:03:40 +0100500}
501
502static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000503ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100504{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500505 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000506
Dave Barachd7cb1b52016-12-09 09:52:16 -0500507 adj_nbr_update_rewrite (ai,
508 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
509 ethernet_build_rewrite (vnet_get_main (),
510 adj->rewrite_header.
511 sw_if_index,
512 adj_get_link_type (ai),
513 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100514}
515
516#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
517{ \
518 k.sw_if_index = sw_if_index; \
519 k.ip6_address = *addr; \
520 k.pad = 0; \
521}
522
523static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500524ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100525{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500526 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
527 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100528 ip6_neighbor_key_t k;
529 uword *p;
530
Dave Barachd7cb1b52016-12-09 09:52:16 -0500531 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100532
533 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500534 if (p)
535 {
536 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
537 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100538
539 return (n);
540}
541
542static adj_walk_rc_t
543ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
544{
545 ip6_neighbor_t *nbr = ctx;
546
547 ip6_nd_mk_complete (ai, nbr);
548
549 return (ADJ_WALK_RC_CONTINUE);
550}
551
552static adj_walk_rc_t
553ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
554{
Neale Ranns19c68d22016-12-07 15:38:14 +0000555 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100556
557 return (ADJ_WALK_RC_CONTINUE);
558}
559
John Lo4fed5b12018-05-22 03:35:06 -0400560static clib_error_t *
561ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
562 u32 sw_if_index, u32 flags)
563{
564 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
565 ip6_neighbor_t *n;
566 u32 i, *to_delete = 0;
567
568 /* *INDENT-OFF* */
569 pool_foreach (n, nm->neighbor_pool,
570 ({
571 if (n->key.sw_if_index == sw_if_index)
572 vec_add1 (to_delete, n - nm->neighbor_pool);
573 }));
574 /* *INDENT-ON* */
575
576 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
577 {
578 for (i = 0; i < vec_len (to_delete); i++)
579 {
580 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
581 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
582 ip6_nd_mk_complete_walk, n);
583 }
584 }
585 else
586 {
587 for (i = 0; i < vec_len (to_delete); i++)
588 {
589 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
590 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
591 ip6_nd_mk_incomplete_walk, NULL);
Neale Ranns37029302018-08-10 05:30:06 -0700592 if (n->flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo4fed5b12018-05-22 03:35:06 -0400593 continue;
594 ip6_neighbor_adj_fib_remove (n,
595 ip6_fib_table_get_index_for_sw_if_index
596 (n->key.sw_if_index));
597 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
598 pool_put (nm->neighbor_pool, n);
599 }
600 }
601
602 vec_free (to_delete);
603 return 0;
604}
605
606VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
607
Neale Rannsb80c5362016-10-08 13:03:40 +0100608void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500609ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100610{
611 ip6_neighbor_t *nbr;
612 ip_adjacency_t *adj;
613
614 adj = adj_get (ai);
615
616 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
617
Neale Ranns32e1c012016-11-22 17:07:28 +0000618 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100619 {
Ole Trøan438f6302018-02-15 21:56:06 +0000620 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800621 adj_glean_update_rewrite (ai);
622 break;
623 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000624 if (NULL != nbr)
625 {
626 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
627 ip6_nd_mk_complete_walk, nbr);
628 }
629 else
630 {
631 /*
632 * no matching ND entry.
633 * construct the rewrite required to for an ND packet, and stick
634 * that in the adj's pipe to smoke.
635 */
636 adj_nbr_update_rewrite (ai,
637 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
638 ethernet_build_rewrite (vnm,
639 sw_if_index,
640 VNET_LINK_IP6,
641 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
642
643 /*
644 * since the FIB has added this adj for a route, it makes sense it may
645 * want to forward traffic sometime soon. Let's send a speculative ND.
646 * just one. If we were to do periodically that wouldn't be bad either,
647 * but that's more code than i'm prepared to write at this time for
648 * relatively little reward.
649 */
650 ip6_nbr_probe (adj);
651 }
652 break;
Neale Ranns1855b8e2018-07-11 10:31:26 -0700653 case IP_LOOKUP_NEXT_BCAST:
654 adj_nbr_update_rewrite (ai,
655 ADJ_NBR_REWRITE_FLAG_COMPLETE,
656 ethernet_build_rewrite (vnm,
657 sw_if_index,
658 VNET_LINK_IP6,
659 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
660 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000661 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700662 {
663 /*
664 * Construct a partial rewrite from the known ethernet mcast dest MAC
665 */
666 u8 *rewrite;
667 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100668
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700669 rewrite = ethernet_build_rewrite (vnm,
670 sw_if_index,
671 adj->ia_link,
672 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000673
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700674 /*
675 * Complete the remaining fields of the adj's rewrite to direct the
676 * complete of the rewrite at switch time by copying in the IP
677 * dst address's bytes.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500678 * Ofset is 2 bytes into the destintation address.
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700679 */
680 offset = vec_len (rewrite) - 2;
Neale Ranns889fe942017-06-01 05:43:19 -0400681 adj_mcast_update_rewrite (ai, rewrite, offset);
Neale Ranns32e1c012016-11-22 17:07:28 +0000682
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700683 break;
684 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000685 case IP_LOOKUP_NEXT_DROP:
686 case IP_LOOKUP_NEXT_PUNT:
687 case IP_LOOKUP_NEXT_LOCAL:
688 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800689 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000690 case IP_LOOKUP_NEXT_MIDCHAIN:
691 case IP_LOOKUP_NEXT_ICMP_ERROR:
692 case IP_LOOKUP_N_NEXT:
693 ASSERT (0);
694 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100695 }
696}
697
Neale Ranns15002542017-09-10 04:39:11 -0700698
699static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700700ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700701{
Neale Ranns53da2212018-02-24 02:11:19 -0800702 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
703 {
704 ip6_ll_prefix_t pfx = {
705 .ilp_addr = n->key.ip6_address,
706 .ilp_sw_if_index = n->key.sw_if_index,
707 };
708 n->fib_entry_index =
709 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
710 }
711 else
712 {
713 fib_prefix_t pfx = {
714 .fp_len = 128,
715 .fp_proto = FIB_PROTOCOL_IP6,
716 .fp_addr.ip6 = n->key.ip6_address,
717 };
Neale Ranns15002542017-09-10 04:39:11 -0700718
Neale Ranns53da2212018-02-24 02:11:19 -0800719 n->fib_entry_index =
720 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
721 FIB_ENTRY_FLAG_ATTACHED,
722 DPO_PROTO_IP6, &pfx.fp_addr,
723 n->key.sw_if_index, ~0, 1, NULL,
724 FIB_ROUTE_PATH_FLAG_NONE);
725 }
Neale Ranns15002542017-09-10 04:39:11 -0700726}
727
John Lo4fed5b12018-05-22 03:35:06 -0400728static ip6_neighbor_t *
729force_reuse_neighbor_entry (void)
730{
731 ip6_neighbor_t *n;
732 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
733 u32 count = 0;
734 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
735 if (index == ~0) /* Try again from elt 0 */
736 index = pool_next_index (nm->neighbor_pool, index);
737
738 /* Find a non-static random entry to free up for reuse */
739 do
740 {
741 if ((count++ == 100) || (index == ~0))
742 return NULL; /* give up after 100 entries */
743 n = pool_elt_at_index (nm->neighbor_pool, index);
744 nm->neighbor_delete_rotor = index;
745 index = pool_next_index (nm->neighbor_pool, index);
746 }
Neale Ranns37029302018-08-10 05:30:06 -0700747 while (n->flags & IP_NEIGHBOR_FLAG_STATIC);
John Lo4fed5b12018-05-22 03:35:06 -0400748
749 /* Remove ARP entry from its interface and update fib */
750 adj_nbr_walk_nh6 (n->key.sw_if_index,
751 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
752 ip6_neighbor_adj_fib_remove
753 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
754 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
755
756 return n;
757}
758
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759int
760vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500761 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700762 const ip6_address_t * a,
Neale Ranns37029302018-08-10 05:30:06 -0700763 const mac_address_t * mac,
764 ip_neighbor_flags_t flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500766 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500768 ip6_neighbor_t *n = 0;
769 int make_new_nd_cache_entry = 1;
770 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500772 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
Damjan Marion586afd72017-04-05 19:18:20 +0200774 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775 {
Neale Ranns37029302018-08-10 05:30:06 -0700776 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, mac, 1, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 return 0;
778 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779
780 k.sw_if_index = sw_if_index;
781 k.ip6_address = a[0];
782 k.pad = 0;
783
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500785 if (p)
786 {
787 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
788 /* Refuse to over-write static neighbor entry. */
Neale Ranns37029302018-08-10 05:30:06 -0700789 if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
790 (n->flags & IP_NEIGHBOR_FLAG_STATIC))
John Lo0e6f4d62018-07-13 17:29:58 -0400791 {
792 /* if MAC address match, still check to send event */
Neale Ranns37029302018-08-10 05:30:06 -0700793 if (0 == mac_address_cmp (&n->mac, mac))
John Lo0e6f4d62018-07-13 17:29:58 -0400794 goto check_customers;
795 return -2;
796 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500797 make_new_nd_cache_entry = 0;
798 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100799
Dave Barachd7cb1b52016-12-09 09:52:16 -0500800 if (make_new_nd_cache_entry)
801 {
John Lo4fed5b12018-05-22 03:35:06 -0400802 if (nm->limit_neighbor_cache_size &&
803 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
804 {
805 n = force_reuse_neighbor_entry ();
806 if (NULL == n)
807 return -2;
808 }
809 else
Dave Barachd318a992019-11-10 15:46:31 -0500810 {
811 pool_get (nm->neighbor_pool, n);
812 memset (n, 0, sizeof (*n));
813 }
John Lo4fed5b12018-05-22 03:35:06 -0400814
Dave Barachd7cb1b52016-12-09 09:52:16 -0500815 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
816 /* old value */ 0);
817 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700818 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100819
Neale Ranns37029302018-08-10 05:30:06 -0700820 mac_address_copy (&n->mac, mac);
Neale Rannsb80c5362016-10-08 13:03:40 +0100821
Dave Barachd7cb1b52016-12-09 09:52:16 -0500822 /*
823 * create the adj-fib. the entry in the FIB table for and to the peer.
824 */
Neale Ranns37029302018-08-10 05:30:06 -0700825 if (!(flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY))
Neale Rannsb3b2de72017-03-08 05:17:22 -0800826 {
Neale Ranns53da2212018-02-24 02:11:19 -0800827 ip6_neighbor_adj_fib_add
828 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700829 }
830 else
831 {
Neale Ranns37029302018-08-10 05:30:06 -0700832 n->flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800833 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500834 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100835 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500836 {
837 /*
838 * prevent a DoS attack from the data-plane that
839 * spams us with no-op updates to the MAC address
840 */
Neale Ranns37029302018-08-10 05:30:06 -0700841 if (0 == mac_address_cmp (&n->mac, mac))
John Lo7f358b32018-04-28 01:19:24 -0400842 {
843 n->time_last_updated = vlib_time_now (vm);
844 goto check_customers;
845 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100846
Neale Ranns37029302018-08-10 05:30:06 -0700847 mac_address_copy (&n->mac, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500848 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849
Neale Rannsb80c5362016-10-08 13:03:40 +0100850 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400851 n->time_last_updated = vlib_time_now (vm);
Neale Ranns37029302018-08-10 05:30:06 -0700852 if (flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo4fed5b12018-05-22 03:35:06 -0400853 {
Neale Ranns37029302018-08-10 05:30:06 -0700854 n->flags |= IP_NEIGHBOR_FLAG_STATIC;
855 n->flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
John Lo4fed5b12018-05-22 03:35:06 -0400856 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100857 else
John Lo4fed5b12018-05-22 03:35:06 -0400858 {
Neale Ranns37029302018-08-10 05:30:06 -0700859 n->flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
860 n->flags &= ~IP_NEIGHBOR_FLAG_STATIC;
John Lo4fed5b12018-05-22 03:35:06 -0400861 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100862
Neale Rannsb80c5362016-10-08 13:03:40 +0100863 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500864 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Dave Barach59b25652017-09-10 15:04:27 -0400866check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 /* Customer(s) waiting for this address to be resolved? */
868 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400869 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870 {
John Lo1edfba92016-08-27 01:11:57 -0400871 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500872
873 while (next_index != (u32) ~ 0)
874 {
John Lo1edfba92016-08-27 01:11:57 -0400875 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
876 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500877 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400878 next_index = pr->next_index;
879 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500880 }
John Lo1edfba92016-08-27 01:11:57 -0400881
Neale Ranns0bdd3192018-09-07 11:04:52 -0700882 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883 }
884
John Lo1edfba92016-08-27 01:11:57 -0400885 /* Customer(s) requesting ND event for this address? */
886 p = mhash_get (&nm->mac_changes_by_address, a);
887 if (p)
888 {
889 next_index = p[0];
890
Dave Barachd7cb1b52016-12-09 09:52:16 -0500891 while (next_index != (u32) ~ 0)
892 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500893 int rv = 1;
Neale Ranns37029302018-08-10 05:30:06 -0700894
Dave Barachd7cb1b52016-12-09 09:52:16 -0500895 mc = pool_elt_at_index (nm->mac_changes, next_index);
John Lo1edfba92016-08-27 01:11:57 -0400896
Dave Barachd7cb1b52016-12-09 09:52:16 -0500897 /* Call the user's data callback, return 1 to suppress dup events */
Neale Ranns37029302018-08-10 05:30:06 -0700898 if (mc->data_callback)
899 rv = (mc->data_callback) (mc->data, mac, sw_if_index, &ip6a_zero);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500900 /*
901 * Signal the resolver process, as long as the user
902 * says they want to be notified
903 */
904 if (rv == 0)
905 vlib_process_signal_event (vm, mc->node_index,
906 mc->type_opaque, mc->data);
907 next_index = mc->next_index;
908 }
John Lo1edfba92016-08-27 01:11:57 -0400909 }
910
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911 return 0;
912}
913
914int
915vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700916 u32 sw_if_index, const ip6_address_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500918 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500920 ip6_neighbor_t *n;
921 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 int rv = 0;
923
Damjan Marion586afd72017-04-05 19:18:20 +0200924 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 {
Neale Ranns37029302018-08-10 05:30:06 -0700926 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL, 0,
927 IP_NEIGHBOR_FLAG_NONE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 return 0;
929 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930
931 k.sw_if_index = sw_if_index;
932 k.ip6_address = a[0];
933 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500934
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935 p = mhash_get (&nm->neighbor_index_by_key, &k);
936 if (p == 0)
937 {
938 rv = -1;
939 goto out;
940 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500941
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100943
Neale Rannsb80c5362016-10-08 13:03:40 +0100944 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500945 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400946 ip6_neighbor_adj_fib_remove
947 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100948
John Lo4fed5b12018-05-22 03:35:06 -0400949 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500951
952out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953 return rv;
954}
955
Dave Barachd7cb1b52016-12-09 09:52:16 -0500956static void ip6_neighbor_set_unset_rpc_callback
957 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500959 vlib_main_t *vm = vlib_get_main ();
960 if (a->is_add)
961 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Ranns37029302018-08-10 05:30:06 -0700962 &a->mac, a->flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700964 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966
967static int
968ip6_neighbor_sort (void *a1, void *a2)
969{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500970 vnet_main_t *vnm = vnet_get_main ();
971 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500973 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
974 n2->key.sw_if_index);
975 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
977 return cmp;
978}
979
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100980ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -0400981ip6_neighbors_pool (void)
982{
983 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
984 return nm->neighbor_pool;
985}
986
987ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100988ip6_neighbors_entries (u32 sw_if_index)
989{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500990 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Neale Ranns37029302018-08-10 05:30:06 -0700991 ip6_neighbor_t *n, *ns = NULL;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100992
993 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500994 pool_foreach (n, nm->neighbor_pool,
995 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100996 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
997 continue;
998 vec_add1 (ns, n[0]);
999 }));
1000 /* *INDENT-ON* */
1001
1002 if (ns)
1003 vec_sort_with_function (ns, ip6_neighbor_sort);
1004 return ns;
1005}
1006
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007static clib_error_t *
1008show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001009 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001011 vnet_main_t *vnm = vnet_get_main ();
1012 ip6_neighbor_t *n, *ns;
1013 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014 u32 sw_if_index;
Dave Barach1bb981d2019-02-26 17:04:40 -05001015 int verbose = 0;
1016
1017 if (unformat (input, "verbose"))
1018 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019
1020 /* Filter entries by interface if given. */
1021 sw_if_index = ~0;
1022 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1023
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001024 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001025 if (ns)
1026 {
Dave Barach1bb981d2019-02-26 17:04:40 -05001027 /*
1028 * Show the entire table if it's not too big, otherwise just
1029 * show the size of the table.
1030 */
1031 if (vec_len (ns) < 50)
1032 verbose = 1;
1033 if (verbose)
1034 {
1035 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
1036 vec_foreach (n, ns)
1037 {
1038 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
1039 }
1040 }
1041 else
1042 vlib_cli_output
1043 (vm, "There are %u ip6 neighbors, "
1044 "'show ip6 neighbors verbose' to display the entire table...",
1045 vec_len (ns));
Billy McFall46529cd2016-10-14 10:45:49 -04001046 vec_free (ns);
1047 }
Dave Barach1bb981d2019-02-26 17:04:40 -05001048 else
1049 vlib_cli_output (vm, "No ip6 neighbors");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
1051 return error;
1052}
1053
Billy McFall0683c9c2016-10-13 08:27:31 -04001054/*?
1055 * This command is used to display the adjacent IPv6 hosts found via
1056 * neighbor discovery. Optionally, limit the output to the specified
1057 * interface.
1058 *
1059 * @cliexpar
1060 * Example of how to display the IPv6 neighbor adjacency table:
1061 * @cliexstart{show ip6 neighbors}
1062 * Time Address Flags Link layer Interface
1063 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1064 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1065 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1066 * @cliexend
1067 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1068 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1069 * Time Address Flags Link layer Interface
1070 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1071 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1072 * @cliexend
1073?*/
1074/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1076 .path = "show ip6 neighbors",
1077 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001078 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079};
Billy McFall0683c9c2016-10-13 08:27:31 -04001080/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081
1082static clib_error_t *
1083set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001084 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085{
Neale Ranns37029302018-08-10 05:30:06 -07001086 ip_neighbor_flags_t flags = IP_NEIGHBOR_FLAG_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001087 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088 ip6_address_t addr;
Neale Ranns37029302018-08-10 05:30:06 -07001089 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090 int addr_valid = 0;
1091 int is_del = 0;
1092 u32 sw_if_index;
1093
Dave Barachd7cb1b52016-12-09 09:52:16 -05001094 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 {
1096 /* intfc, ip6-address, mac-address */
1097 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001098 unformat_vnet_sw_interface, vnm, &sw_if_index,
1099 unformat_ip6_address, &addr,
Neale Ranns37029302018-08-10 05:30:06 -07001100 unformat_mac_address_t, &mac))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001101 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102
1103 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001104 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001105 else if (unformat (input, "static"))
Neale Ranns37029302018-08-10 05:30:06 -07001106 flags |= IP_NEIGHBOR_FLAG_STATIC;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001107 else if (unformat (input, "no-fib-entry"))
Neale Ranns37029302018-08-10 05:30:06 -07001108 flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001110 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111 }
1112
1113 if (!addr_valid)
1114 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001115
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 if (!is_del)
Neale Ranns37029302018-08-10 05:30:06 -07001117 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr, &mac, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 else
Neale Ranns0bdd3192018-09-07 11:04:52 -07001119 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 return 0;
1121}
1122
Billy McFall0683c9c2016-10-13 08:27:31 -04001123/*?
1124 * This command is used to manually add an entry to the IPv6 neighbor
1125 * adjacency table. Optionally, the entry can be added as static. It is
1126 * also used to remove an entry from the table. Use the '<em>show ip6
1127 * neighbors</em>' command to display all learned and manually entered entries.
1128 *
1129 * @cliexpar
1130 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1131 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1132 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1133 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1134?*/
1135/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001136VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1137{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138 .path = "set ip6 neighbor",
1139 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001140 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141};
Billy McFall0683c9c2016-10-13 08:27:31 -04001142/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143
Dave Barachd7cb1b52016-12-09 09:52:16 -05001144typedef enum
1145{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1147 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1148 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1149} icmp6_neighbor_solicitation_or_advertisement_next_t;
1150
1151static_always_inline uword
1152icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1153 vlib_node_runtime_t * node,
1154 vlib_frame_t * frame,
1155 uword is_solicitation)
1156{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001157 vnet_main_t *vnm = vnet_get_main ();
1158 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001160 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1162 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001163 vlib_node_runtime_t *error_node =
1164 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 int bogus_length;
1166
1167 from = vlib_frame_vector_args (frame);
1168 n_left_from = n_packets;
1169 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001170
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171 if (node->flags & VLIB_NODE_FLAG_TRACE)
1172 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1173 /* stride */ 1,
1174 sizeof (icmp6_input_trace_t));
1175
Dave Barachd7cb1b52016-12-09 09:52:16 -05001176 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177 (is_solicitation
1178 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1179 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1180 n_advertisements_sent = 0;
1181
1182 while (n_left_from > 0)
1183 {
1184 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1185
1186 while (n_left_from > 0 && n_left_to_next > 0)
1187 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001188 vlib_buffer_t *p0;
1189 ip6_header_t *ip0;
1190 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1191 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001193 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1194 int is_rewrite0;
1195 u32 ni0;
1196
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197 bi0 = to_next[0] = from[0];
1198
1199 from += 1;
1200 to_next += 1;
1201 n_left_from -= 1;
1202 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001203
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204 p0 = vlib_get_buffer (vm, bi0);
1205 ip0 = vlib_buffer_get_current (p0);
1206 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001207 options_len0 =
1208 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
1210 error0 = ICMP6_ERROR_NONE;
1211 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001212 ip6_sadd_link_local =
1213 ip6_address_is_link_local_unicast (&ip0->src_address);
1214 ip6_sadd_unspecified =
1215 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216
1217 /* Check that source address is unspecified, link-local or else on-link. */
1218 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1219 {
1220 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221
Dave Barachd7cb1b52016-12-09 09:52:16 -05001222 if (ADJ_INDEX_INVALID != src_adj_index0)
1223 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001224 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
Dave Barachd7cb1b52016-12-09 09:52:16 -05001226 /* Allow all realistic-looking rewrite adjacencies to pass */
1227 ni0 = adj0->lookup_next_index;
1228 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1229 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001230
Dave Barachd7cb1b52016-12-09 09:52:16 -05001231 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1232 || !is_rewrite0)
1233 ?
1234 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1235 : error0);
1236 }
1237 else
1238 {
1239 error0 =
1240 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1241 }
1242 }
1243
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244 o0 = (void *) (h0 + 1);
1245 o0 = ((options_len0 == 8 && o0->header.type == option_type
1246 && o0->header.n_data_u64s == 1) ? o0 : 0);
1247
1248 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001249 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001250 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001251 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001252 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1253 is_solicitation ?
1254 &ip0->src_address :
1255 &h0->target_address,
Neale Ranns37029302018-08-10 05:30:06 -07001256 (mac_address_t *)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001257 o0->ethernet_address,
Neale Ranns37029302018-08-10 05:30:06 -07001258 IP_NEIGHBOR_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001259 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260
1261 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1262 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001263 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001264 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001265 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
Dave Barachd7cb1b52016-12-09 09:52:16 -05001267 fib_index =
1268 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001269
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001270 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001271 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001272 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1273 }
1274 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001275 {
Neale Ranns53da2212018-02-24 02:11:19 -08001276 if (ip6_address_is_link_local_unicast (&h0->target_address))
1277 {
1278 fei = ip6_fib_table_lookup_exact_match
1279 (ip6_ll_fib_get (sw_if_index0),
1280 &h0->target_address, 128);
1281 }
1282 else
1283 {
1284 fei = ip6_fib_table_lookup_exact_match (fib_index,
1285 &h0->target_address,
1286 128);
1287 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001288
Neale Ranns3f844d02017-02-18 00:03:54 -08001289 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001290 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001291 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001292 error0 =
1293 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001294 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001295 else
1296 {
1297 if (FIB_ENTRY_FLAG_LOCAL &
1298 fib_entry_get_flags_for_source (fei,
1299 FIB_SOURCE_INTERFACE))
1300 {
1301 /* It's an address that belongs to one of our interfaces
1302 * that's good. */
1303 }
1304 else
1305 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001306 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1307 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001308 {
1309 /* The address was added by IPv6 Proxy ND config.
1310 * We should only respond to these if the NS arrived on
1311 * the link that has a matching covering prefix */
1312 }
1313 else
1314 {
1315 error0 =
1316 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1317 }
1318 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001319 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320 }
1321
1322 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001323 next0 = (error0 != ICMP6_ERROR_NONE
1324 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1325 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326 else
1327 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001328 next0 = 0;
1329 error0 = error0 == ICMP6_ERROR_NONE ?
1330 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331 }
1332
1333 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1334 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001335 vnet_sw_interface_t *sw_if0;
1336 ethernet_interface_t *eth_if0;
1337 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338
Dave Barachd7cb1b52016-12-09 09:52:16 -05001339 /* dst address is either source address or the all-nodes mcast addr */
1340 if (!ip6_sadd_unspecified)
1341 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001342 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001343 ip6_set_reserved_multicast_address (&ip0->dst_address,
1344 IP6_MULTICAST_SCOPE_link_local,
1345 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346
1347 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001348 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349 h0->icmp.type = ICMP6_neighbor_advertisement;
1350
1351 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1352 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001353 eth_if0 =
1354 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355 if (eth_if0 && o0)
1356 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001357 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1358 o0->header.type =
1359 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001360 }
1361
1362 h0->advertisement_flags = clib_host_to_net_u32
1363 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1364 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1365
1366 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001367 h0->icmp.checksum =
1368 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1369 &bogus_length);
1370 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371
Dave Barachd7cb1b52016-12-09 09:52:16 -05001372 /* Reuse current MAC header, copy SMAC to DMAC and
1373 * interface MAC to SMAC */
1374 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1375 eth0 = vlib_buffer_get_current (p0);
1376 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1377 if (eth_if0)
1378 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001379
Dave Barachd7cb1b52016-12-09 09:52:16 -05001380 /* Setup input and output sw_if_index for packet */
1381 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1382 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1383 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1384 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385
1386 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001387 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388
1389 p0->error = error_node->errors[error0];
1390
1391 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1392 to_next, n_left_to_next,
1393 bi0, next0);
1394 }
1395
1396 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1397 }
1398
1399 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001400 vlib_error_count (vm, error_node->node_index,
1401 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1402 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001403
1404 return frame->n_vectors;
1405}
1406
1407/* for "syslogging" - use elog for now */
1408#define foreach_log_level \
1409 _ (DEBUG, "DEBUG") \
1410 _ (INFO, "INFORMATION") \
1411 _ (NOTICE, "NOTICE") \
1412 _ (WARNING, "WARNING") \
1413 _ (ERR, "ERROR") \
1414 _ (CRIT, "CRITICAL") \
1415 _ (ALERT, "ALERT") \
1416 _ (EMERG, "EMERGENCY")
1417
Dave Barachd7cb1b52016-12-09 09:52:16 -05001418typedef enum
1419{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001420#define _(f,s) LOG_##f,
1421 foreach_log_level
1422#undef _
1423} log_level_t;
1424
Dave Barachd7cb1b52016-12-09 09:52:16 -05001425static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001426#define _(f,s) s,
1427 foreach_log_level
1428#undef _
1429};
1430
Dave Barachd7cb1b52016-12-09 09:52:16 -05001431static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
1433static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001434ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001435{
1436 /* just use elog for now */
1437 u8 *what;
1438 va_list va;
1439
Dave Barachd7cb1b52016-12-09 09:52:16 -05001440 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1441 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442
1443 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001444 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001445 {
1446 what = va_format (0, fmt, &va);
1447
Dave Barachd7cb1b52016-12-09 09:52:16 -05001448 ELOG_TYPE_DECLARE (e) =
1449 {
1450 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1451 struct
1452 {
1453 u32 s[2];
1454 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001456 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1457 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458 }
1459 va_end (va);
1460 return;
1461}
1462
Juraj Sloboda52574522018-05-03 10:03:50 +02001463clib_error_t *
1464call_ip6_neighbor_callbacks (void *data,
1465 _vnet_ip6_neighbor_function_list_elt_t * elt)
1466{
1467 clib_error_t *error = 0;
1468
1469 while (elt)
1470 {
1471 error = elt->fp (data);
1472 if (error)
1473 return error;
1474 elt = elt->next_ip6_neighbor_function;
1475 }
1476
1477 return error;
1478}
1479
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001481typedef enum
1482{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001483 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1484 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1485 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1486 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1487} icmp6_router_solicitation_or_advertisement_next_t;
1488
1489static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001490icmp6_router_solicitation (vlib_main_t * vm,
1491 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001493 vnet_main_t *vnm = vnet_get_main ();
1494 ip6_main_t *im = &ip6_main;
1495 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001497 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001499 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500 int bogus_length;
1501
1502 icmp6_neighbor_discovery_option_type_t option_type;
1503
Dave Barachd7cb1b52016-12-09 09:52:16 -05001504 vlib_node_runtime_t *error_node =
1505 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001506
1507 from = vlib_frame_vector_args (frame);
1508 n_left_from = n_packets;
1509 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001510
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511 if (node->flags & VLIB_NODE_FLAG_TRACE)
1512 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1513 /* stride */ 1,
1514 sizeof (icmp6_input_trace_t));
1515
1516 /* source may append his LL address */
1517 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1518
1519 while (n_left_from > 0)
1520 {
1521 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001522
Ed Warnickecb9cada2015-12-08 15:45:58 -07001523 while (n_left_from > 0 && n_left_to_next > 0)
1524 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001525 vlib_buffer_t *p0;
1526 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001527 ip6_radv_t *radv_info = 0;
1528
Dave Barachd7cb1b52016-12-09 09:52:16 -05001529 icmp6_neighbor_discovery_header_t *h0;
1530 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1531
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001533 u32 is_solicitation = 1, is_dropped = 0;
1534 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001535
1536 bi0 = to_next[0] = from[0];
1537
1538 from += 1;
1539 to_next += 1;
1540 n_left_from -= 1;
1541 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001542
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543 p0 = vlib_get_buffer (vm, bi0);
1544 ip0 = vlib_buffer_get_current (p0);
1545 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001546 options_len0 =
1547 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1548 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1549 is_link_local =
1550 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001551
1552 error0 = ICMP6_ERROR_NONE;
1553 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001554
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555 /* check if solicitation (not from nd_timer node) */
1556 if (ip6_address_is_unspecified (&ip0->dst_address))
1557 is_solicitation = 0;
1558
1559 /* Check that source address is unspecified, link-local or else on-link. */
1560 if (!is_unspecified && !is_link_local)
1561 {
1562 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563
Dave Barachd7cb1b52016-12-09 09:52:16 -05001564 if (ADJ_INDEX_INVALID != src_adj_index0)
1565 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001566 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001567
Dave Barachd7cb1b52016-12-09 09:52:16 -05001568 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1569 ?
1570 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1571 : error0);
1572 }
1573 else
1574 {
1575 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1576 }
1577 }
1578
Ed Warnickecb9cada2015-12-08 15:45:58 -07001579 /* check for source LL option and process */
1580 o0 = (void *) (h0 + 1);
1581 o0 = ((options_len0 == 8
1582 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001583 && o0->header.n_data_u64s == 1) ? o0 : 0);
1584
Ed Warnickecb9cada2015-12-08 15:45:58 -07001585 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001586 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1587 !is_unspecified && !is_link_local))
1588 {
Neale Ranns37029302018-08-10 05:30:06 -07001589 vnet_set_ip6_ethernet_neighbor
1590 (vm, sw_if_index0,
1591 &ip0->src_address,
1592 (mac_address_t *) o0->ethernet_address,
1593 IP_NEIGHBOR_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001594 }
1595
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596 /* default is to drop */
1597 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001598
Ed Warnickecb9cada2015-12-08 15:45:58 -07001599 if (error0 == ICMP6_ERROR_NONE)
1600 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001601 vnet_sw_interface_t *sw_if0;
1602 ethernet_interface_t *eth_if0;
1603 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001604
1605 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1606 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001607 eth_if0 =
1608 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001609
1610 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001611 error0 =
1612 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1613 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614
1615 if (error0 == ICMP6_ERROR_NONE)
1616 {
1617 u32 ri;
1618
1619 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001620 p0->current_length -=
1621 (options_len0 +
1622 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001623
1624 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001625 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1626 sw_if_index0)
1627 {
1628 ri =
1629 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001630
Neale Ranns8e254952018-04-25 05:40:48 -07001631 if (ri != ~0)
1632 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1633 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001634
1635 error0 =
1636 ((!radv_info) ?
1637 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1638 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001639
1640 if (error0 == ICMP6_ERROR_NONE)
1641 {
1642 f64 now = vlib_time_now (vm);
1643
1644 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001645 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646 {
Neale Ranns75152282017-01-09 01:00:45 -08001647 if (0 != radv_info->last_radv_time &&
1648 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001649 MIN_DELAY_BETWEEN_RAS)
1650 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001651 else
1652 radv_info->last_radv_time = now;
1653 }
1654
1655 /* send now */
1656 icmp6_router_advertisement_header_t rh;
1657
1658 rh.icmp.type = ICMP6_router_advertisement;
1659 rh.icmp.code = 0;
1660 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001661
Ed Warnickecb9cada2015-12-08 15:45:58 -07001662 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001663 rh.router_lifetime_in_sec =
1664 clib_host_to_net_u16
1665 (radv_info->adv_router_lifetime_in_sec);
1666 rh.
1667 time_in_msec_between_retransmitted_neighbor_solicitations
1668 =
1669 clib_host_to_net_u32 (radv_info->
1670 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1671 rh.neighbor_reachable_time_in_msec =
1672 clib_host_to_net_u32 (radv_info->
1673 adv_neighbor_reachable_time_in_msec);
1674
1675 rh.flags =
1676 (radv_info->adv_managed_flag) ?
1677 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1678 0;
1679 rh.flags |=
1680 ((radv_info->adv_other_flag) ?
1681 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1682 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683
1684
Dave Barachd7cb1b52016-12-09 09:52:16 -05001685 u16 payload_length =
1686 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687
Dave Barach3a63fc52019-01-07 09:15:47 -05001688 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001689 (vm, &bi0, (void *) &rh,
Dave Barach3a63fc52019-01-07 09:15:47 -05001690 sizeof (icmp6_router_advertisement_header_t)))
1691 {
1692 /* buffer allocation failed, drop the pkt */
1693 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1694 goto drop0;
1695 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001696
Dave Barachd7cb1b52016-12-09 09:52:16 -05001697 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001699 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1700 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001701
Dave Barachd7cb1b52016-12-09 09:52:16 -05001702 h.header.type =
1703 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001704 h.header.n_data_u64s = 1;
1705
1706 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001707 clib_memcpy (&h.ethernet_address[0],
1708 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001709
Dave Barach3a63fc52019-01-07 09:15:47 -05001710 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001711 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001712 sizeof
1713 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1714 {
1715 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1716 goto drop0;
1717 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001718
Dave Barachd7cb1b52016-12-09 09:52:16 -05001719 payload_length +=
1720 sizeof
1721 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001722 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001723
Ed Warnickecb9cada2015-12-08 15:45:58 -07001724 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001725 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726 {
1727 icmp6_neighbor_discovery_mtu_option_t h;
1728
1729 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001730 h.mtu =
1731 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001732 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1733 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001734
1735 payload_length +=
1736 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001737
Dave Barach3a63fc52019-01-07 09:15:47 -05001738 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001739 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001740 sizeof
1741 (icmp6_neighbor_discovery_mtu_option_t)))
1742 {
1743 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1744 goto drop0;
1745 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001746 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001747
Ed Warnickecb9cada2015-12-08 15:45:58 -07001748 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001749 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001750
Dave Barachd7cb1b52016-12-09 09:52:16 -05001751 /* *INDENT-OFF* */
1752 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1753 ({
1754 if(pr_info->enabled &&
1755 (!pr_info->decrement_lifetime_flag
1756 || (pr_info->pref_lifetime_expires >0)))
1757 {
1758 /* advertise this prefix */
1759 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001760
Dave Barachd7cb1b52016-12-09 09:52:16 -05001761 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1762 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763
Dave Barachd7cb1b52016-12-09 09:52:16 -05001764 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765
Dave Barachd7cb1b52016-12-09 09:52:16 -05001766 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1767 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768
Dave Barachd7cb1b52016-12-09 09:52:16 -05001769 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1770 {
1771 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1772 h.preferred_time = 0;
1773 }
1774 else
1775 {
1776 if(pr_info->decrement_lifetime_flag)
1777 {
1778 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1779 (pr_info->valid_lifetime_expires - now) : 0;
1780
1781 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1782 (pr_info->pref_lifetime_expires - now) : 0;
1783 }
1784
1785 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1786 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1787 }
1788 h.unused = 0;
1789
Dave Barachd318a992019-11-10 15:46:31 -05001790 clib_warning ("Prefix %U valid %u preferred %u",
1791 format_ip6_address, &pr_info->prefix,
1792 ntohl(h.valid_time),
1793 ntohl(h.preferred_time));
1794
1795 if (h.valid_time == 0)
1796 clib_warning ("WARNING: valid_time 0!!!");
1797
Dave Barachd7cb1b52016-12-09 09:52:16 -05001798 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1799
1800 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1801
Dave Barach3a63fc52019-01-07 09:15:47 -05001802 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001803 (vm, &bi0, (void *)&h,
1804 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05001805 {
1806 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1807 goto drop0;
1808 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001809
1810 }
1811 }));
1812 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001813
1814 /* add additional options before here */
1815
1816 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001817 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001818 {
1819 ip0->dst_address = ip0->src_address;
1820 }
1821 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001822 {
1823 /* target address is all-nodes mcast addr */
1824 ip6_set_reserved_multicast_address
1825 (&ip0->dst_address,
1826 IP6_MULTICAST_SCOPE_link_local,
1827 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001828 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001829
Ed Warnickecb9cada2015-12-08 15:45:58 -07001830 /* source address MUST be the link-local address */
1831 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001832
Dave Barachd7cb1b52016-12-09 09:52:16 -05001833 ip0->hop_limit = 255;
1834 ip0->payload_length =
1835 clib_host_to_net_u16 (payload_length);
1836
1837 icmp6_router_advertisement_header_t *rh0 =
1838 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1839 rh0->icmp.checksum =
1840 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1841 &bogus_length);
1842 ASSERT (bogus_length == 0);
1843
Ed Warnickecb9cada2015-12-08 15:45:58 -07001844 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001845 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001846 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001847
1848 if (is_solicitation)
1849 {
1850 ethernet_header_t *eth0;
1851 /* Reuse current MAC header, copy SMAC to DMAC and
1852 * interface MAC to SMAC */
1853 vlib_buffer_reset (p0);
1854 eth0 = vlib_buffer_get_current (p0);
1855 clib_memcpy (eth0->dst_address, eth0->src_address,
1856 6);
1857 clib_memcpy (eth0->src_address, eth_if0->address,
1858 6);
1859 next0 =
1860 is_dropped ? next0 :
1861 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1862 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1863 sw_if_index0;
1864 }
1865 else
1866 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001867 adj_index0 = radv_info->mcast_adj_index;
Jon Loeligera90b26c2019-12-03 10:51:23 -06001868 if (adj_index0 == ADJ_INDEX_INVALID)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001869 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1870 else
1871 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001872 next0 =
1873 is_dropped ? next0 :
1874 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1875 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1876 adj_index0;
1877 }
1878 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001879 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001880
1881 radv_info->n_solicitations_dropped += is_dropped;
1882 radv_info->n_solicitations_rcvd += is_solicitation;
1883
1884 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001885 {
1886 radv_info->n_advertisements_sent++;
1887 n_advertisements_sent++;
1888 }
1889 }
1890 }
1891 }
1892
Dave Barach3a63fc52019-01-07 09:15:47 -05001893 drop0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001894 p0->error = error_node->errors[error0];
1895
Dave Barachd7cb1b52016-12-09 09:52:16 -05001896 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001897 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001898
Ed Warnickecb9cada2015-12-08 15:45:58 -07001899 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1900 to_next, n_left_to_next,
1901 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001902
Ed Warnickecb9cada2015-12-08 15:45:58 -07001903 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001904
Ed Warnickecb9cada2015-12-08 15:45:58 -07001905 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1906 }
1907
1908 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001909 vlib_error_count (vm, error_node->node_index,
1910 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1911 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001912
1913 return frame->n_vectors;
1914}
1915
1916 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1917static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001918icmp6_router_advertisement (vlib_main_t * vm,
1919 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001921 vnet_main_t *vnm = vnet_get_main ();
1922 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001923 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001924 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001925 u32 n_left_from, n_left_to_next, next_index;
1926 u32 n_advertisements_rcvd = 0;
1927
Dave Barachd7cb1b52016-12-09 09:52:16 -05001928 vlib_node_runtime_t *error_node =
1929 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001930
1931 from = vlib_frame_vector_args (frame);
1932 n_left_from = n_packets;
1933 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001934
Ed Warnickecb9cada2015-12-08 15:45:58 -07001935 if (node->flags & VLIB_NODE_FLAG_TRACE)
1936 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1937 /* stride */ 1,
1938 sizeof (icmp6_input_trace_t));
1939
1940 while (n_left_from > 0)
1941 {
1942 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001943
Ed Warnickecb9cada2015-12-08 15:45:58 -07001944 while (n_left_from > 0 && n_left_to_next > 0)
1945 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001946 vlib_buffer_t *p0;
1947 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001948 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001949 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001950 u32 bi0, options_len0, sw_if_index0, next0, error0;
1951
1952 bi0 = to_next[0] = from[0];
1953
1954 from += 1;
1955 to_next += 1;
1956 n_left_from -= 1;
1957 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001958
Ed Warnickecb9cada2015-12-08 15:45:58 -07001959 p0 = vlib_get_buffer (vm, bi0);
1960 ip0 = vlib_buffer_get_current (p0);
1961 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001962 options_len0 =
1963 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001964
1965 error0 = ICMP6_ERROR_NONE;
1966 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1967
Dave Barachd7cb1b52016-12-09 09:52:16 -05001968 /* Check that source address is link-local */
1969 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001970 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1971
1972 /* default is to drop */
1973 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001974
Ed Warnickecb9cada2015-12-08 15:45:58 -07001975 n_advertisements_rcvd++;
1976
1977 if (error0 == ICMP6_ERROR_NONE)
1978 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001979 vnet_sw_interface_t *sw_if0;
1980 ethernet_interface_t *eth_if0;
1981
Ed Warnickecb9cada2015-12-08 15:45:58 -07001982 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1983 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001984 eth_if0 =
1985 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001986
1987 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001988 error0 =
1989 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1990 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001991
1992 if (error0 == ICMP6_ERROR_NONE)
1993 {
1994 u32 ri;
1995
1996 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001997 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1998 sw_if_index0)
1999 {
2000 ri =
2001 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002002
Neale Ranns8e254952018-04-25 05:40:48 -07002003 if (ri != ~0)
2004 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2005 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002006
2007 error0 =
2008 ((!radv_info) ?
2009 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
2010 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011
2012 if (error0 == ICMP6_ERROR_NONE)
2013 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002014 radv_info->keep_sending_rs = 0;
2015
2016 ra_report_t r;
2017
2018 r.sw_if_index = sw_if_index0;
Neale Ranns37029302018-08-10 05:30:06 -07002019 memcpy (&r.router_address, &ip0->src_address, 16);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002020 r.current_hop_limit = h0->current_hop_limit;
2021 r.flags = h0->flags;
2022 r.router_lifetime_in_sec =
2023 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2024 r.neighbor_reachable_time_in_msec =
2025 clib_net_to_host_u32
2026 (h0->neighbor_reachable_time_in_msec);
2027 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2028 r.prefixes = 0;
2029
Ed Warnickecb9cada2015-12-08 15:45:58 -07002030 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002031 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2032 && (h0->current_hop_limit !=
2033 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002034 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002035 ip6_neighbor_syslog (vm, LOG_WARNING,
2036 "our AdvCurHopLimit on %U doesn't agree with %U",
2037 format_vnet_sw_if_index_name,
2038 vnm, sw_if_index0,
2039 format_ip6_address,
2040 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002041 }
2042
Dave Barachd7cb1b52016-12-09 09:52:16 -05002043 if ((h0->flags &
2044 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2045 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002046 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002047 ip6_neighbor_syslog (vm, LOG_WARNING,
2048 "our AdvManagedFlag on %U doesn't agree with %U",
2049 format_vnet_sw_if_index_name,
2050 vnm, sw_if_index0,
2051 format_ip6_address,
2052 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002053 }
2054
Dave Barachd7cb1b52016-12-09 09:52:16 -05002055 if ((h0->flags &
2056 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2057 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002058 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002059 ip6_neighbor_syslog (vm, LOG_WARNING,
2060 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2061 format_vnet_sw_if_index_name,
2062 vnm, sw_if_index0,
2063 format_ip6_address,
2064 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002065 }
2066
Dave Barachd7cb1b52016-12-09 09:52:16 -05002067 if ((h0->
2068 time_in_msec_between_retransmitted_neighbor_solicitations
2069 && radv_info->
2070 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2071 && (h0->
2072 time_in_msec_between_retransmitted_neighbor_solicitations
2073 !=
2074 clib_host_to_net_u32 (radv_info->
2075 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002076 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002077 ip6_neighbor_syslog (vm, LOG_WARNING,
2078 "our AdvRetransTimer on %U doesn't agree with %U",
2079 format_vnet_sw_if_index_name,
2080 vnm, sw_if_index0,
2081 format_ip6_address,
2082 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002083 }
2084
Dave Barachd7cb1b52016-12-09 09:52:16 -05002085 if ((h0->neighbor_reachable_time_in_msec &&
2086 radv_info->adv_neighbor_reachable_time_in_msec) &&
2087 (h0->neighbor_reachable_time_in_msec !=
2088 clib_host_to_net_u32
2089 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002090 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002091 ip6_neighbor_syslog (vm, LOG_WARNING,
2092 "our AdvReachableTime on %U doesn't agree with %U",
2093 format_vnet_sw_if_index_name,
2094 vnm, sw_if_index0,
2095 format_ip6_address,
2096 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002097 }
2098
2099 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002100 u8 *opt_hdr = (u8 *) (h0 + 1);
2101 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002102 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002103 icmp6_neighbor_discovery_option_header_t *o0 =
2104 (icmp6_neighbor_discovery_option_header_t *)
2105 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002106 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107 icmp6_neighbor_discovery_option_type_t option_type =
2108 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002109
Dave Barachd7cb1b52016-12-09 09:52:16 -05002110 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002111 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002112 ip6_neighbor_syslog (vm, LOG_ERR,
2113 "malformed RA packet on %U from %U",
2114 format_vnet_sw_if_index_name,
2115 vnm, sw_if_index0,
2116 format_ip6_address,
2117 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002118 break;
2119 }
2120
Dave Barachd7cb1b52016-12-09 09:52:16 -05002121 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002122 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002123 ip6_neighbor_syslog (vm, LOG_ERR,
2124 " zero length option in RA on %U from %U",
2125 format_vnet_sw_if_index_name,
2126 vnm, sw_if_index0,
2127 format_ip6_address,
2128 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002129 break;
2130 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002131 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002132 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002133 ip6_neighbor_syslog (vm, LOG_ERR,
2134 "option length in RA packet greater than total length on %U from %U",
2135 format_vnet_sw_if_index_name,
2136 vnm, sw_if_index0,
2137 format_ip6_address,
2138 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002139 break;
2140 }
2141
2142 options_len0 -= opt_len;
2143 opt_hdr += opt_len;
2144
Dave Barachd7cb1b52016-12-09 09:52:16 -05002145 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002146 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002147 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2148 {
2149 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2150 * h =
2151 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2152 *) (o0);
2153
2154 if (opt_len < sizeof (*h))
2155 break;
2156
2157 memcpy (r.slla, h->ethernet_address, 6);
2158 }
2159 break;
2160
Ed Warnickecb9cada2015-12-08 15:45:58 -07002161 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002162 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002163 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002164 (icmp6_neighbor_discovery_mtu_option_t
2165 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002166
Dave Barachd7cb1b52016-12-09 09:52:16 -05002167 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002168 break;
2169
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002170 r.mtu = clib_net_to_host_u32 (h->mtu);
2171
Dave Barachd7cb1b52016-12-09 09:52:16 -05002172 if ((h->mtu && radv_info->adv_link_mtu) &&
2173 (h->mtu !=
2174 clib_host_to_net_u32
2175 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002176 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002177 ip6_neighbor_syslog (vm, LOG_WARNING,
2178 "our AdvLinkMTU on %U doesn't agree with %U",
2179 format_vnet_sw_if_index_name,
2180 vnm, sw_if_index0,
2181 format_ip6_address,
2182 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002183 }
2184 }
2185 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002186
Ed Warnickecb9cada2015-12-08 15:45:58 -07002187 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2188 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002189 icmp6_neighbor_discovery_prefix_information_option_t
2190 * h =
2191 (icmp6_neighbor_discovery_prefix_information_option_t
2192 *) (o0);
2193
Ed Warnickecb9cada2015-12-08 15:45:58 -07002194 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002195 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002196 u32 preferred, valid;
2197
Dave Barachd7cb1b52016-12-09 09:52:16 -05002198 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002199 break;
2200
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002201 vec_validate (r.prefixes,
2202 vec_len (r.prefixes));
2203 ra_report_prefix_info_t *prefix =
2204 vec_elt_at_index (r.prefixes,
2205 vec_len (r.prefixes) - 1);
2206
Dave Barachd7cb1b52016-12-09 09:52:16 -05002207 preferred =
2208 clib_net_to_host_u32 (h->preferred_time);
2209 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002210
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002211 prefix->preferred_time = preferred;
2212 prefix->valid_time = valid;
2213 prefix->flags = h->flags & 0xc0;
Neale Ranns37029302018-08-10 05:30:06 -07002214 prefix->prefix.fp_len = h->dst_address_length;
2215 prefix->prefix.fp_addr.ip6 = h->dst_address;
2216 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002217
Ed Warnickecb9cada2015-12-08 15:45:58 -07002218 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002219 /* *INDENT-OFF* */
2220 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2221 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002222
Dave Barachd7cb1b52016-12-09 09:52:16 -05002223 ip6_address_t mask;
2224 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2225
2226 if(pr_info->enabled &&
2227 (pr_info->prefix_len == h->dst_address_length) &&
2228 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2229 {
2230 /* found it */
2231 if(!pr_info->decrement_lifetime_flag &&
2232 valid != pr_info->adv_valid_lifetime_in_secs)
2233 {
2234 ip6_neighbor_syslog(vm, LOG_WARNING,
2235 "our ADV validlifetime on %U for %U does not agree with %U",
2236 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2237 format_ip6_address, &h->dst_address);
2238 }
2239 if(!pr_info->decrement_lifetime_flag &&
2240 preferred != pr_info->adv_pref_lifetime_in_secs)
2241 {
2242 ip6_neighbor_syslog(vm, LOG_WARNING,
2243 "our ADV preferredlifetime on %U for %U does not agree with %U",
2244 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2245 format_ip6_address, &h->dst_address);
2246 }
2247 }
2248 break;
2249 }));
2250 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002251 break;
2252 }
2253 default:
2254 /* skip this one */
2255 break;
2256 }
2257 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002258 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002259 }
2260 }
2261 }
2262
2263 p0->error = error_node->errors[error0];
2264
Dave Barachd7cb1b52016-12-09 09:52:16 -05002265 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002266 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002267
Ed Warnickecb9cada2015-12-08 15:45:58 -07002268 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2269 to_next, n_left_to_next,
2270 bi0, next0);
2271 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002272
Ed Warnickecb9cada2015-12-08 15:45:58 -07002273 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2274 }
2275
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002276 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002277 vlib_error_count (vm, error_node->node_index,
2278 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2279 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002280
2281 return frame->n_vectors;
2282}
2283
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002284static inline f64
2285random_f64_from_to (f64 from, f64 to)
2286{
2287 static u32 seed = 0;
2288 static u8 seed_set = 0;
2289 if (!seed_set)
2290 {
2291 seed = random_default_seed ();
2292 seed_set = 1;
2293 }
2294 return random_f64 (&seed) * (to - from) + from;
2295}
2296
2297static inline u8
2298get_mac_address (u32 sw_if_index, u8 * address)
2299{
2300 vnet_main_t *vnm = vnet_get_main ();
2301 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2302 if (!hw_if->hw_address)
2303 return 1;
2304 clib_memcpy (address, hw_if->hw_address, 6);
2305 return 0;
2306}
2307
2308static inline vlib_buffer_t *
2309create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2310{
2311 u32 bi0;
2312 vlib_buffer_t *p0;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002313 icmp6_router_solicitation_header_t *rh;
2314 u16 payload_length;
2315 int bogus_length;
2316 u32 sw_if_index;
2317
2318 sw_if_index = radv_info->sw_if_index;
2319
2320 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2321 {
2322 clib_warning ("buffer allocation failure");
2323 return 0;
2324 }
2325
2326 p0 = vlib_get_buffer (vm, bi0);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002327 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2328 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2329
2330 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2331 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2332
2333 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2334
2335 rh = vlib_buffer_get_current (p0);
2336 p0->current_length = sizeof (*rh);
2337
2338 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2339 rh->neighbor.icmp.code = 0;
2340 rh->neighbor.icmp.checksum = 0;
2341 rh->neighbor.reserved_must_be_zero = 0;
2342
2343 rh->link_layer_option.header.type =
2344 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2345 if (0 != get_mac_address (sw_if_index,
2346 rh->link_layer_option.ethernet_address))
2347 {
2348 clib_warning ("interface with sw_if_index %u has no mac address",
2349 sw_if_index);
2350 vlib_buffer_free (vm, &bi0, 1);
2351 return 0;
2352 }
2353 rh->link_layer_option.header.n_data_u64s = 1;
2354
2355 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2356
2357 rh->ip.ip_version_traffic_class_and_flow_label =
2358 clib_host_to_net_u32 (0x6 << 28);
2359 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2360 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2361 rh->ip.hop_limit = 255;
2362 rh->ip.src_address = radv_info->link_local_address;
2363 /* set address ff02::2 */
David Johnsond9818dd2018-12-14 14:53:41 -05002364 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002365 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2366
2367 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2368 &rh->ip,
2369 &bogus_length);
2370
2371 return p0;
2372}
2373
2374static inline void
2375stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2376{
2377 u32 bi0;
2378
2379 ra->keep_sending_rs = 0;
2380 if (ra->buffer)
2381 {
2382 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2383 vlib_buffer_free (vm, &bi0, 1);
2384 ra->buffer = 0;
2385 }
2386}
2387
2388static inline bool
2389check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2390 f64 * due_time)
2391{
2392 vlib_buffer_t *p0;
2393 vlib_frame_t *f;
2394 u32 *to_next;
2395 u32 next_index;
2396 vlib_buffer_t *c0;
2397 u32 ci0;
2398
2399 icmp6_send_router_solicitation_params_t *params;
2400
2401 if (!radv_info->keep_sending_rs)
2402 return false;
2403
2404 params = &radv_info->params;
2405
2406 if (radv_info->due_time > current_time)
2407 {
2408 *due_time = radv_info->due_time;
2409 return true;
2410 }
2411
2412 p0 = radv_info->buffer;
2413
2414 next_index = ip6_rewrite_mcast_node.index;
2415
2416 c0 = vlib_buffer_copy (vm, p0);
2417 ci0 = vlib_get_buffer_index (vm, c0);
2418
2419 f = vlib_get_frame_to_node (vm, next_index);
2420 to_next = vlib_frame_vector_args (f);
2421 to_next[0] = ci0;
2422 f->n_vectors = 1;
2423 vlib_put_frame_to_node (vm, next_index, f);
2424
2425 if (params->mrc != 0 && --radv_info->n_left == 0)
2426 stop_sending_rs (vm, radv_info);
2427 else
2428 {
2429 radv_info->sleep_interval =
2430 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2431 if (radv_info->sleep_interval > params->mrt)
2432 radv_info->sleep_interval =
2433 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2434
2435 radv_info->due_time = current_time + radv_info->sleep_interval;
2436
2437 if (params->mrd != 0
2438 && current_time > radv_info->start_time + params->mrd)
2439 stop_sending_rs (vm, radv_info);
2440 else
2441 *due_time = radv_info->due_time;
2442 }
2443
2444 return radv_info->keep_sending_rs;
2445}
2446
2447static uword
2448send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2449 vlib_frame_t * f0)
2450{
2451 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2452 ip6_radv_t *radv_info;
2453 uword *event_data = 0;
2454 f64 sleep_time = 1e9;
2455 f64 current_time;
2456 f64 due_time;
2457 f64 dt = 0;
2458
2459 while (true)
2460 {
2461 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2462 vlib_process_get_events (vm, &event_data);
2463 vec_reset_length (event_data);
2464
2465 current_time = vlib_time_now (vm);
2466 do
2467 {
2468 due_time = current_time + 1e9;
2469 /* *INDENT-OFF* */
2470 pool_foreach (radv_info, nm->if_radv_pool,
2471 ({
2472 if (check_send_rs (vm, radv_info, current_time, &dt)
2473 && (dt < due_time))
2474 due_time = dt;
2475 }));
2476 /* *INDENT-ON* */
2477 current_time = vlib_time_now (vm);
2478 }
2479 while (due_time < current_time);
2480
2481 sleep_time = due_time - current_time;
2482 }
2483
2484 return 0;
2485}
2486
2487/* *INDENT-OFF* */
2488VLIB_REGISTER_NODE (send_rs_process_node) = {
2489 .function = send_rs_process,
2490 .type = VLIB_NODE_TYPE_PROCESS,
2491 .name = "send-rs-process",
2492};
2493/* *INDENT-ON* */
2494
2495void
2496icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2497 icmp6_send_router_solicitation_params_t *
2498 params)
2499{
2500 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2501 u32 rai;
2502 ip6_radv_t *ra = 0;
2503
2504 ASSERT (~0 != sw_if_index);
2505
2506 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2507 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2508
Juraj Sloboda52574522018-05-03 10:03:50 +02002509 stop_sending_rs (vm, ra);
2510
2511 if (!stop)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002512 {
2513 ra->keep_sending_rs = 1;
2514 ra->params = *params;
2515 ra->n_left = params->mrc;
2516 ra->start_time = vlib_time_now (vm);
2517 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2518 ra->due_time = 0; /* send first packet ASAP */
2519 ra->buffer = create_buffer_for_rs (vm, ra);
2520 if (!ra->buffer)
2521 ra->keep_sending_rs = 0;
2522 else
2523 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2524 }
2525}
2526
Neale Ranns87df12d2017-02-18 08:16:41 -08002527/**
2528 * @brief Add a multicast Address to the advertised MLD set
2529 */
2530static void
2531ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2532{
2533 ip6_mldp_group_t *mcast_group_info;
2534 uword *p;
2535
2536 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002537 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002538 mcast_group_info =
2539 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2540
2541 /* add address */
2542 if (!mcast_group_info)
2543 {
2544 /* add */
2545 u32 mi;
2546 pool_get (radv_info->mldp_group_pool, mcast_group_info);
Dave Barachd318a992019-11-10 15:46:31 -05002547 memset (mcast_group_info, 0, sizeof (*mcast_group_info));
Neale Ranns87df12d2017-02-18 08:16:41 -08002548
2549 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002550 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002551 0);
2552
2553 mcast_group_info->type = 4;
2554 mcast_group_info->mcast_source_address_pool = 0;
2555 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002556 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002557 sizeof (ip6_address_t));
2558 }
2559}
2560
2561/**
2562 * @brief Delete a multicast Address from the advertised MLD set
2563 */
2564static void
2565ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2566{
2567 ip6_mldp_group_t *mcast_group_info;
2568 uword *p;
2569
2570 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2571 mcast_group_info =
2572 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2573
2574 if (mcast_group_info)
2575 {
2576 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2577 /* old_value */ 0);
2578 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2579 }
2580}
2581
2582/**
2583 * @brief Add a multicast Address to the advertised MLD set
2584 */
2585static void
2586ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2587 ip6_multicast_address_scope_t scope,
2588 ip6_multicast_link_local_group_id_t group)
2589{
2590 ip6_address_t addr;
2591
2592 ip6_set_reserved_multicast_address (&addr, scope, group);
2593
2594 ip6_neighbor_add_mld_prefix (a, &addr);
2595}
2596
2597/**
2598 * @brief create and initialize router advertisement parameters with default
2599 * values for this intfc
2600 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002601u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002602ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002603 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002604{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002605 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2606 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002607 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002608 vnet_sw_interface_t *sw_if0;
2609 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002610
2611 /* lookup radv container - ethernet interfaces only */
2612 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002613 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002614 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2615
Dave Barachd7cb1b52016-12-09 09:52:16 -05002616 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002617 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002618
2619 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2620 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002621 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2622
Dave Barachd7cb1b52016-12-09 09:52:16 -05002623 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002624 {
2625 a = pool_elt_at_index (nm->if_radv_pool, ri);
2626
Dave Barachd7cb1b52016-12-09 09:52:16 -05002627 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002628 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002629 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002630 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002631
Neale Ranns32e1c012016-11-22 17:07:28 +00002632 /* release the lock on the interface's mcast adj */
2633 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002634
Neale Ranns87df12d2017-02-18 08:16:41 -08002635 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002636 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002637 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002638 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002639 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002640 }));
2641 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002642 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002643 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002644 }));
2645 /* *INDENT-ON* */
2646
Neale Ranns87df12d2017-02-18 08:16:41 -08002647 pool_free (a->mldp_group_pool);
2648 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002649
Neale Ranns87df12d2017-02-18 08:16:41 -08002650 mhash_free (&a->address_to_prefix_index);
2651 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002652
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002653 if (a->keep_sending_rs)
2654 a->keep_sending_rs = 0;
2655
Dave Barachd7cb1b52016-12-09 09:52:16 -05002656 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002657 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2658 ri = ~0;
2659 }
2660 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002661 else
2662 {
2663 if (is_add)
2664 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002665 pool_get (nm->if_radv_pool, a);
Dave Barachd318a992019-11-10 15:46:31 -05002666 memset (a, 0, sizeof (*a));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002667
2668 ri = a - nm->if_radv_pool;
2669 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2670
2671 /* initialize default values (most of which are zero) */
Dave Barachb7b92992018-10-17 10:38:51 -04002672 clib_memset (a, 0, sizeof (a[0]));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002673
2674 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002675 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2676 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2677 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2678 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2679
Neale Ranns87df12d2017-02-18 08:16:41 -08002680 /* send ll address source address option */
2681 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002682
2683 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2684 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2685 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2686 a->seed = (u32) clib_cpu_time_now ();
2687 (void) random_u32 (&a->seed);
2688 a->randomizer = clib_cpu_time_now ();
2689 (void) random_u64 (&a->randomizer);
2690
2691 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2692 a->initial_adverts_sent = a->initial_adverts_count - 1;
2693 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2694
2695 /* deafult is to send */
2696 a->send_radv = 1;
2697
2698 /* fill in radv_info for this interface that will be needed later */
Ole Troand7231612018-06-07 10:17:57 +02002699 a->adv_link_mtu =
2700 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002701
2702 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2703
2704 /* fill in default link-local address (this may be overridden) */
Jon Loeligerdfa47dd2019-06-13 11:02:26 -05002705 ip6_link_local_address_from_ethernet_mac_address
Dave Barachd7cb1b52016-12-09 09:52:16 -05002706 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002707
2708 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2709 sizeof (ip6_address_t));
2710 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2711 sizeof (ip6_address_t));
2712
Neale Ranns32e1c012016-11-22 17:07:28 +00002713 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2714 VNET_LINK_IP6,
2715 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002716
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002717 a->keep_sending_rs = 0;
2718
Dave Barachd7cb1b52016-12-09 09:52:16 -05002719 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002720 ip6_neighbor_add_mld_grp (a,
2721 IP6_MULTICAST_SCOPE_link_local,
2722 IP6_MULTICAST_GROUP_ID_all_hosts);
2723 ip6_neighbor_add_mld_grp (a,
2724 IP6_MULTICAST_SCOPE_link_local,
2725 IP6_MULTICAST_GROUP_ID_all_routers);
2726 ip6_neighbor_add_mld_grp (a,
2727 IP6_MULTICAST_SCOPE_link_local,
2728 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002729 }
2730 }
2731 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002732}
2733
2734/* send an mldpv2 report */
2735static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002736ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002737{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002738 vnet_main_t *vnm = vnet_get_main ();
2739 vlib_main_t *vm = vnm->vlib_main;
2740 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2741 vnet_sw_interface_t *sw_if0;
2742 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002743 u32 ri;
2744 int bogus_length;
2745
Dave Barachd7cb1b52016-12-09 09:52:16 -05002746 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002747 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002748 vlib_buffer_t *b0;
2749 ip6_header_t *ip0;
2750 u32 *to_next;
2751 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002752 u32 bo0;
2753 u32 n_to_alloc = 1;
2754 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002755
Ed Warnickecb9cada2015-12-08 15:45:58 -07002756 icmp6_multicast_listener_report_header_t *rh0;
2757 icmp6_multicast_listener_report_packet_t *rp0;
2758
2759 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2760 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2761 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2762
2763 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2764 return;
2765
2766 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002767 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2768 ~0);
2769
Ed Warnickecb9cada2015-12-08 15:45:58 -07002770 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002771
2772 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002773 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002774
Ed Warnickecb9cada2015-12-08 15:45:58 -07002775 /* send report now - build a mldpv2 report packet */
Damjan Marion671e60e2018-12-30 18:09:59 +01002776 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002777 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002778 {
Dave Barach3a63fc52019-01-07 09:15:47 -05002779 alloc_fail:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002780 clib_warning ("buffer allocation failure");
2781 return;
2782 }
2783
2784 b0 = vlib_get_buffer (vm, bo0);
2785
2786 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002787 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002788
Dave Barachd7cb1b52016-12-09 09:52:16 -05002789 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002790
2791 b0->error = ICMP6_ERROR_NONE;
2792
2793 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002794 ip0 = (ip6_header_t *) & rp0->ip;
2795 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002796
Dave Barachb7b92992018-10-17 10:38:51 -04002797 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002798
2799 ip0->ip_version_traffic_class_and_flow_label =
2800 clib_host_to_net_u32 (0x6 << 28);
2801
2802 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002803 /* for DEBUG - vnet driver won't seem to emit router alerts */
2804 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2805 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002806
Ed Warnickecb9cada2015-12-08 15:45:58 -07002807 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002808
Ed Warnickecb9cada2015-12-08 15:45:58 -07002809 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002810 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2811 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812
2813 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002814 ip6_set_reserved_multicast_address (&ip0->dst_address,
2815 IP6_MULTICAST_SCOPE_link_local,
2816 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2817
Ed Warnickecb9cada2015-12-08 15:45:58 -07002818 /* add reports here */
2819 ip6_mldp_group_t *m;
2820 int num_addr_records = 0;
2821 icmp6_multicast_address_record_t rr;
2822
2823 /* fill in the hop-by-hop extension header (router alert) info */
2824 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2825 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002826
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2828 rh0->alert.len = 2;
2829 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002830
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831 rh0->pad.type = 1;
2832 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002833
Ed Warnickecb9cada2015-12-08 15:45:58 -07002834 rh0->icmp.checksum = 0;
2835
Dave Barachd7cb1b52016-12-09 09:52:16 -05002836 /* *INDENT-OFF* */
2837 pool_foreach (m, radv_info->mldp_group_pool,
2838 ({
2839 rr.type = m->type;
2840 rr.aux_data_len_u32s = 0;
2841 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2842 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002843
Dave Barachd7cb1b52016-12-09 09:52:16 -05002844 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002845
Damjan Marionab9b7ec2019-01-18 20:24:44 +01002846 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2847 sizeof(icmp6_multicast_address_record_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05002848 {
2849 vlib_buffer_free (vm, &bo0, 1);
2850 goto alloc_fail;
2851 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002852
Dave Barachd7cb1b52016-12-09 09:52:16 -05002853 payload_length += sizeof( icmp6_multicast_address_record_t);
2854 }));
2855 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002856
2857 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002858 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2859
Ed Warnickecb9cada2015-12-08 15:45:58 -07002860 /* update lengths */
2861 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2862
Dave Barachd7cb1b52016-12-09 09:52:16 -05002863 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2864 &bogus_length);
2865 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002866
Dave Barachd7cb1b52016-12-09 09:52:16 -05002867 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002868 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002869 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002870 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002871 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002873
Neale Ranns32e1c012016-11-22 17:07:28 +00002874 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002875 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876
Neale Ranns32e1c012016-11-22 17:07:28 +00002877 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002878
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879 f = vlib_get_frame_to_node (vm, node->index);
2880 to_next = vlib_frame_vector_args (f);
2881 to_next[0] = bo0;
2882 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002883
Ed Warnickecb9cada2015-12-08 15:45:58 -07002884 vlib_put_frame_to_node (vm, node->index, f);
2885 return;
2886}
2887
Dave Barachd7cb1b52016-12-09 09:52:16 -05002888/* *INDENT-OFF* */
2889VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2890{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002891 .function = icmp6_router_solicitation,
2892 .name = "icmp6-router-solicitation",
2893
2894 .vector_size = sizeof (u32),
2895
2896 .format_trace = format_icmp6_input_trace,
2897
2898 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2899 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002900 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002901 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002902 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2903 },
2904};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002905/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002906
2907/* send a RA or update the timer info etc.. */
2908static uword
2909ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002910 vlib_node_runtime_t * node,
2911 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002912{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002913 vnet_main_t *vnm = vnet_get_main ();
2914 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2915 ip6_radv_t *radv_info;
2916 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002918 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002919 u32 *to_next = 0;
2920 u32 bo0;
2921 icmp6_router_solicitation_header_t *h0;
2922 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002923 f64 now = vlib_time_now (vm);
2924
2925 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002926 /* *INDENT-OFF* */
2927 pool_foreach (radv_info, nm->if_radv_pool,
2928 ({
2929 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2930 {
2931 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2932 radv_info->next_multicast_time = now;
2933 radv_info->last_multicast_time = now;
2934 radv_info->last_radv_time = 0;
2935 radv_info->all_routers_mcast = 0;
2936 continue;
2937 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002938
Dave Barachd7cb1b52016-12-09 09:52:16 -05002939 /* Make sure that we've joined the all-routers multicast group */
2940 if(!radv_info->all_routers_mcast)
2941 {
2942 /* send MDLP_REPORT_EVENT message */
2943 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2944 radv_info->all_routers_mcast = 1;
2945 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002946
Dave Barachd7cb1b52016-12-09 09:52:16 -05002947 /* is it time to send a multicast RA on this interface? */
2948 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2949 {
2950 u32 n_to_alloc = 1;
2951 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002952
Dave Barachd7cb1b52016-12-09 09:52:16 -05002953 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2954 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002955
Dave Barachd7cb1b52016-12-09 09:52:16 -05002956 /* multicast send - compute next multicast send time */
2957 if( radv_info->initial_adverts_sent > 0)
2958 {
2959 radv_info->initial_adverts_sent--;
2960 if(rfn > radv_info-> initial_adverts_interval)
2961 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002962
Dave Barachd7cb1b52016-12-09 09:52:16 -05002963 /* check to see if we are ceasing to send */
2964 if( radv_info->initial_adverts_sent == 0)
2965 if(radv_info->cease_radv)
2966 radv_info->send_radv = 0;
2967 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002968
Dave Barachd7cb1b52016-12-09 09:52:16 -05002969 radv_info->next_multicast_time = rfn + now;
2970 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002971
Dave Barachd7cb1b52016-12-09 09:52:16 -05002972 /* send advert now - build a "solicted" router advert with unspecified source address */
Damjan Marion671e60e2018-12-30 18:09:59 +01002973 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002974
Dave Barachd7cb1b52016-12-09 09:52:16 -05002975 if (PREDICT_FALSE(n_allocated == 0))
2976 {
2977 clib_warning ("buffer allocation failure");
2978 continue;
2979 }
2980 b0 = vlib_get_buffer (vm, bo0);
2981 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2982 b0->error = ICMP6_ERROR_NONE;
2983 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2984
2985 h0 = vlib_buffer_get_current (b0);
2986
Dave Barachb7b92992018-10-17 10:38:51 -04002987 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002988
2989 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2990 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2991 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2992 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2993 h0->ip.hop_limit = 255;
2994
2995 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2996 h0->ip.src_address.as_u64[0] = 0;
2997 h0->ip.src_address.as_u64[1] = 0;
2998
2999 h0->ip.dst_address.as_u64[0] = 0;
3000 h0->ip.dst_address.as_u64[1] = 0;
3001
3002 h0->neighbor.icmp.type = ICMP6_router_solicitation;
3003
3004 if (PREDICT_FALSE(f == 0))
3005 {
3006 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
3007 to_next = vlib_frame_vector_args (f);
3008 n_left_to_next = VLIB_FRAME_SIZE;
3009 n_this_frame = 0;
3010 }
3011
3012 n_this_frame++;
3013 n_left_to_next--;
3014 to_next[0] = bo0;
3015 to_next += 1;
3016
3017 if (PREDICT_FALSE(n_left_to_next == 0))
3018 {
3019 f->n_vectors = n_this_frame;
3020 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3021 f = 0;
3022 }
3023 }
3024 }));
3025 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003026
3027 if (f)
3028 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003029 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003030 f->n_vectors = n_this_frame;
3031 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3032 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003033 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003034}
3035
3036static uword
3037ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3038 vlib_node_runtime_t * node,
3039 vlib_frame_t * frame)
3040{
3041 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003042 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003043
3044 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003045
Ed Warnickecb9cada2015-12-08 15:45:58 -07003046 while (1)
3047 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003048 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003049
Dave Barachd7cb1b52016-12-09 09:52:16 -05003050 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003051
Dave Barachd7cb1b52016-12-09 09:52:16 -05003052 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003053 {
3054 /* No events found: timer expired. */
3055 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003056 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003057 }
3058 else
3059 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003060 switch (event_type)
3061 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062
Dave Barachd7cb1b52016-12-09 09:52:16 -05003063 case ICMP6_ND_EVENT_INIT:
3064 break;
3065
3066 case ~0:
3067 break;
3068
3069 default:
3070 ASSERT (0);
3071 }
3072
Ed Warnickecb9cada2015-12-08 15:45:58 -07003073 if (event_data)
3074 _vec_len (event_data) = 0;
3075 }
3076 }
3077 return frame->n_vectors;
3078}
3079
Dave Barachd7cb1b52016-12-09 09:52:16 -05003080/* *INDENT-OFF* */
3081VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3082{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003083 .function = icmp6_router_advertisement,
3084 .name = "icmp6-router-advertisement",
3085
3086 .vector_size = sizeof (u32),
3087
3088 .format_trace = format_icmp6_input_trace,
3089
3090 .n_next_nodes = 1,
3091 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003092 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003093 },
3094};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003095/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003096
3097vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3098
3099 .function = ip6_icmp_neighbor_discovery_event_process,
3100 .name = "ip6-icmp-neighbor-discovery-event-process",
3101 .type = VLIB_NODE_TYPE_PROCESS,
3102};
3103
3104static uword
3105icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003106 vlib_node_runtime_t * node, vlib_frame_t * frame)
3107{
3108 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3109 /* is_solicitation */
3110 1);
3111}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003112
3113static uword
3114icmp6_neighbor_advertisement (vlib_main_t * vm,
3115 vlib_node_runtime_t * node,
3116 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003117{
3118 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3119 /* is_solicitation */
3120 0);
3121}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003122
Dave Barachd7cb1b52016-12-09 09:52:16 -05003123/* *INDENT-OFF* */
3124VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3125{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003126 .function = icmp6_neighbor_solicitation,
3127 .name = "icmp6-neighbor-solicitation",
3128
3129 .vector_size = sizeof (u32),
3130
3131 .format_trace = format_icmp6_input_trace,
3132
3133 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3134 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003135 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003136 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3137 },
3138};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003139/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003140
Dave Barachd7cb1b52016-12-09 09:52:16 -05003141/* *INDENT-OFF* */
3142VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3143{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003144 .function = icmp6_neighbor_advertisement,
3145 .name = "icmp6-neighbor-advertisement",
3146
3147 .vector_size = sizeof (u32),
3148
3149 .format_trace = format_icmp6_input_trace,
3150
3151 .n_next_nodes = 1,
3152 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003153 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003154 },
3155};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003156/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003157
Neale Rannsc7b8f202018-04-25 06:34:31 -07003158typedef enum
3159{
3160 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3161 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3162 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3163} ip6_discover_neighbor_next_t;
3164
3165typedef enum
3166{
3167 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3168 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3169 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3170} ip6_discover_neighbor_error_t;
3171
3172static uword
3173ip6_discover_neighbor_inline (vlib_main_t * vm,
3174 vlib_node_runtime_t * node,
3175 vlib_frame_t * frame, int is_glean)
3176{
3177 vnet_main_t *vnm = vnet_get_main ();
3178 ip6_main_t *im = &ip6_main;
3179 ip_lookup_main_t *lm = &im->lookup_main;
3180 u32 *from, *to_next_drop;
3181 uword n_left_from, n_left_to_next_drop;
Dave Barach49433ad2018-08-08 17:59:03 -04003182 u64 seed;
3183 u32 thread_index = vm->thread_index;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003184 int bogus_length;
3185 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3186
3187 if (node->flags & VLIB_NODE_FLAG_TRACE)
3188 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3189
Neale Rannscd35e532018-08-31 02:51:45 -07003190 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003191
3192 from = vlib_frame_vector_args (frame);
3193 n_left_from = frame->n_vectors;
3194
3195 while (n_left_from > 0)
3196 {
3197 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3198 to_next_drop, n_left_to_next_drop);
3199
3200 while (n_left_from > 0 && n_left_to_next_drop > 0)
3201 {
Neale Rannscd35e532018-08-31 02:51:45 -07003202 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003203 vnet_hw_interface_t *hw_if0;
3204 ip6_radv_t *radv_info;
Neale Rannscd35e532018-08-31 02:51:45 -07003205 ip_adjacency_t *adj0;
3206 vlib_buffer_t *p0;
3207 ip6_header_t *ip0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003208
3209 pi0 = from[0];
3210
3211 p0 = vlib_get_buffer (vm, pi0);
3212
3213 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3214
3215 ip0 = vlib_buffer_get_current (p0);
3216
3217 adj0 = adj_get (adj_index0);
3218
3219 if (!is_glean)
3220 {
3221 ip0->dst_address.as_u64[0] =
3222 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3223 ip0->dst_address.as_u64[1] =
3224 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3225 }
3226
Neale Rannsc7b8f202018-04-25 06:34:31 -07003227 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3228 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3229
Neale Rannscd35e532018-08-31 02:51:45 -07003230 /* combine the address and interface for a hash */
3231 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003232
Neale Rannscd35e532018-08-31 02:51:45 -07003233 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003234
3235 from += 1;
3236 n_left_from -= 1;
3237 to_next_drop[0] = pi0;
3238 to_next_drop += 1;
3239 n_left_to_next_drop -= 1;
3240
3241 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3242
3243 /* If the interface is link-down, drop the pkt */
3244 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3245 drop0 = 1;
3246
3247 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3248 {
3249 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3250
3251 if (ri != ~0)
3252 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3253 else
3254 drop0 = 1;
3255 }
3256 else
3257 drop0 = 1;
3258
3259 /*
3260 * the adj has been updated to a rewrite but the node the DPO that got
3261 * us here hasn't - yet. no big deal. we'll drop while we wait.
3262 */
3263 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3264 drop0 = 1;
3265
3266 p0->error =
3267 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3268 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3269
3270 if (drop0)
3271 continue;
3272
3273 {
3274 u32 bi0 = 0;
3275 icmp6_neighbor_solicitation_header_t *h0;
3276 vlib_buffer_t *b0;
3277
3278 h0 = vlib_packet_template_get_packet
3279 (vm, &im->discover_neighbor_packet_template, &bi0);
John Lo084606b2018-06-19 15:27:48 -04003280 if (!h0)
3281 continue;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003282
Neale Ranns45db8852019-01-09 00:04:04 -08003283 /* copy the persistent fields from the original */
3284 b0 = vlib_get_buffer (vm, bi0);
3285 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3286
Neale Rannsc7b8f202018-04-25 06:34:31 -07003287 /*
3288 * Build ethernet header.
3289 * Choose source address based on destination lookup
3290 * adjacency.
3291 */
3292 if (!ip6_src_address_for_packet (lm,
3293 sw_if_index0,
3294 &ip0->dst_address,
3295 &h0->ip.src_address))
3296 {
3297 /* There is no address on the interface */
3298 p0->error =
3299 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3300 vlib_buffer_free (vm, &bi0, 1);
3301 continue;
3302 }
3303
3304 /*
3305 * Destination address is a solicited node multicast address.
3306 * We need to fill in
3307 * the low 24 bits with low 24 bits of target's address.
3308 */
3309 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3310 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3311 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3312
3313 h0->neighbor.target_address = ip0->dst_address;
3314
3315 clib_memcpy (h0->link_layer_option.ethernet_address,
3316 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3317
3318 /* $$$$ appears we need this; why is the checksum non-zero? */
3319 h0->neighbor.icmp.checksum = 0;
3320 h0->neighbor.icmp.checksum =
3321 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3322 &bogus_length);
3323
3324 ASSERT (bogus_length == 0);
3325
3326 vlib_buffer_copy_trace_flag (vm, p0, bi0);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003327 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3328 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3329
3330 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3331 radv_info->mcast_adj_index;
3332
3333 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3334 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3335
3336 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3337 }
3338 }
3339
3340 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3341 n_left_to_next_drop);
3342 }
3343
3344 return frame->n_vectors;
3345}
3346
3347static uword
3348ip6_discover_neighbor (vlib_main_t * vm,
3349 vlib_node_runtime_t * node, vlib_frame_t * frame)
3350{
3351 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3352}
3353
3354static uword
3355ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3356{
3357 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3358}
3359
3360static char *ip6_discover_neighbor_error_strings[] = {
3361 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3362 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3363 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3364 = "no source address for ND solicitation",
3365};
3366
3367/* *INDENT-OFF* */
3368VLIB_REGISTER_NODE (ip6_glean_node) =
3369{
3370 .function = ip6_glean,
3371 .name = "ip6-glean",
3372 .vector_size = sizeof (u32),
3373 .format_trace = format_ip6_forward_next_trace,
3374 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3375 .error_strings = ip6_discover_neighbor_error_strings,
3376 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3377 .next_nodes =
3378 {
3379 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3380 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3381 },
3382};
3383VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3384{
3385 .function = ip6_discover_neighbor,
3386 .name = "ip6-discover-neighbor",
3387 .vector_size = sizeof (u32),
3388 .format_trace = format_ip6_forward_next_trace,
3389 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3390 .error_strings = ip6_discover_neighbor_error_strings,
3391 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3392 .next_nodes =
3393 {
3394 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3395 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3396 },
3397};
3398/* *INDENT-ON* */
3399
Dave Barachd7cb1b52016-12-09 09:52:16 -05003400/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003401int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003402ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3403 u8 suppress, u8 managed, u8 other,
3404 u8 ll_option, u8 send_unicast, u8 cease,
3405 u8 use_lifetime, u32 lifetime,
3406 u32 initial_count, u32 initial_interval,
3407 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003408{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003409 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3410 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003411 u32 ri;
3412
3413 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003414 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3415 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003416 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003417 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003418
Dave Barachd7cb1b52016-12-09 09:52:16 -05003419 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003420 {
3421
Dave Barachd7cb1b52016-12-09 09:52:16 -05003422 ip6_radv_t *radv_info;
3423 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003424
Dave Barachd7cb1b52016-12-09 09:52:16 -05003425 if ((max_interval != 0) && (min_interval == 0))
3426 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003427
Dave Barachd7cb1b52016-12-09 09:52:16 -05003428 max_interval =
3429 (max_interval !=
3430 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3431 radv_info->max_radv_interval;
3432 min_interval =
3433 (min_interval !=
3434 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3435 radv_info->min_radv_interval;
3436 lifetime =
3437 (use_lifetime !=
3438 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3439 radv_info->adv_router_lifetime_in_sec;
3440
3441 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003442 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003443 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003444 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003445
3446 if (lifetime <= max_interval)
3447 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003448 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003449
3450 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003451 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003452 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3453 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003454 }
3455
Dave Barachd7cb1b52016-12-09 09:52:16 -05003456 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3457 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3458 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003459
Dave Barachd7cb1b52016-12-09 09:52:16 -05003460 /*
3461 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3462 if "flag" is clear don't change corresponding value
3463 */
3464 radv_info->send_radv =
3465 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3466 radv_info->adv_managed_flag =
3467 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3468 radv_info->adv_other_flag =
3469 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3470 radv_info->adv_link_layer_address =
3471 (ll_option !=
3472 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3473 radv_info->send_unicast =
3474 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3475 radv_info->cease_radv =
3476 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3477
3478 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003479 radv_info->max_radv_interval = max_interval;
3480 radv_info->adv_router_lifetime_in_sec = lifetime;
3481
Dave Barachd7cb1b52016-12-09 09:52:16 -05003482 radv_info->initial_adverts_count =
3483 (initial_count !=
3484 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3485 radv_info->initial_adverts_count;
3486 radv_info->initial_adverts_interval =
3487 (initial_interval !=
3488 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3489 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003490
3491 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003492 if ((cease != 0) && (is_no))
3493 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003494
Dave Barachd7cb1b52016-12-09 09:52:16 -05003495 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3496 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003497 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003498 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003499 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003500 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003501}
3502
3503int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003504ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3505 ip6_address_t * prefix_addr, u8 prefix_len,
3506 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3507 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3508 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003509{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003510 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003511 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003512
Ed Warnickecb9cada2015-12-08 15:45:58 -07003513 u32 ri;
3514
3515 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003516 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3517 ~0);
3518
Ed Warnickecb9cada2015-12-08 15:45:58 -07003519 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3520
3521 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003522
3523 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003524 {
3525 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003526 ip6_radv_t *radv_info;
3527 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003528
3529 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003530 ip6_radv_prefix_t *prefix;
3531
Ed Warnickecb9cada2015-12-08 15:45:58 -07003532 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003533 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3534
Ed Warnickecb9cada2015-12-08 15:45:58 -07003535 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3536
Dave Barachd7cb1b52016-12-09 09:52:16 -05003537 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003538 {
3539 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003540 if (!prefix)
3541 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3542
3543 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003544 return VNET_API_ERROR_INVALID_VALUE_2;
3545
Dave Barachd7cb1b52016-12-09 09:52:16 -05003546 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003547 /* do specific delete processing here before returning */
3548 /* try to remove from routing table */
3549
Dave Barachd7cb1b52016-12-09 09:52:16 -05003550 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3551 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003552 pool_put (radv_info->adv_prefixes_pool, prefix);
3553
Dave Barachd7cb1b52016-12-09 09:52:16 -05003554 radv_info->initial_adverts_sent =
3555 radv_info->initial_adverts_count - 1;
3556 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003557 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003558 radv_info->last_radv_time = 0;
3559 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003560 }
3561
3562 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003563 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003564 {
3565 /* add */
3566 u32 pi;
3567 pool_get (radv_info->adv_prefixes_pool, prefix);
Dave Barachd318a992019-11-10 15:46:31 -05003568 memset (prefix, 0, sizeof (*prefix));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003569 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003570 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3571 /* old_value */ 0);
3572
Dave Barachb7b92992018-10-17 10:38:51 -04003573 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05003574
Ed Warnickecb9cada2015-12-08 15:45:58 -07003575 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003576 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3577
Ed Warnickecb9cada2015-12-08 15:45:58 -07003578 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003579 prefix->adv_on_link_flag = 1; /* L bit set */
3580 prefix->adv_autonomous_flag = 1; /* A bit set */
3581 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003582 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3583 prefix->enabled = 1;
3584 prefix->decrement_lifetime_flag = 1;
3585 prefix->deprecated_prefix_flag = 1;
3586
Dave Barachd7cb1b52016-12-09 09:52:16 -05003587 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003588 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003589 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003590 /* insert prefix into routing table as a connected prefix */
3591 }
3592
Dave Barachd7cb1b52016-12-09 09:52:16 -05003593 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003594 goto restart;
3595 }
3596 else
3597 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003598
3599 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003600 return VNET_API_ERROR_INVALID_VALUE_2;
3601
Dave Barachd7cb1b52016-12-09 09:52:16 -05003602 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003603 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003604 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003605 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003606 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003607 }
3608
Dave Barachd7cb1b52016-12-09 09:52:16 -05003609 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003610 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003611 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003612 prefix->adv_pref_lifetime_in_secs = ~0;
3613 prefix->decrement_lifetime_flag = 0;
3614 }
3615 else
3616 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003617 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3618 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003619 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003620
Ed Warnickecb9cada2015-12-08 15:45:58 -07003621 /* copy remaining */
3622 prefix->enabled = !(no_advertise != 0);
3623 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3624 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3625
Dave Barachd7cb1b52016-12-09 09:52:16 -05003626 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003627 /* restart */
3628 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003629 prefix->valid_lifetime_expires =
3630 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003631 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003632
3633 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3634 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003635 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003636 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003637 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003638 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003639}
3640
3641clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003642ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3643 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003644{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003645 vnet_main_t *vnm = vnet_get_main ();
3646 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3647 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003648 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003649 u8 suppress = 0, managed = 0, other = 0;
3650 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003651 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003652 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3653 0, ra_initial_interval = 0;
3654 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003655
Dave Barachd7cb1b52016-12-09 09:52:16 -05003656 unformat_input_t _line_input, *line_input = &_line_input;
3657 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003658
3659 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003660 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003661 ip6_address_t ip6_addr;
3662 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003663
Ed Warnickecb9cada2015-12-08 15:45:58 -07003664
3665 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003666 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003667 return 0;
3668
3669 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003670 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003671 {
3672
Dave Barachd7cb1b52016-12-09 09:52:16 -05003673 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003674 unformat_vnet_sw_interface, vnm, &sw_if_index))
3675 {
3676 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003677 ethernet_interface_t *eth_if0 = 0;
3678
Ed Warnickecb9cada2015-12-08 15:45:58 -07003679 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003680 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3681 eth_if0 =
3682 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3683
3684 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003685 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003686 error =
3687 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003688 goto done;
3689 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003690
Ed Warnickecb9cada2015-12-08 15:45:58 -07003691 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003692 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3693 sw_if_index, ~0);
3694
Ed Warnickecb9cada2015-12-08 15:45:58 -07003695 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003696
3697 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003698 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003699 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003700 }
3701 else
3702 {
3703 error = clib_error_return (0, "unknown interface %U'",
3704 format_unformat_error, line_input);
3705 goto done;
3706 }
3707 }
3708 else
3709 {
3710 error = clib_error_return (0, "invalid interface name %U'",
3711 format_unformat_error, line_input);
3712 goto done;
3713 }
3714 }
3715
3716 /* get the rest of the command */
3717 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3718 {
3719 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003720 is_no = 1;
3721 else if (unformat (line_input, "prefix %U/%d",
3722 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003723 {
3724 add_radv_info = 0;
3725 break;
3726 }
3727 else if (unformat (line_input, "ra-managed-config-flag"))
3728 {
3729 managed = 1;
3730 break;
3731 }
3732 else if (unformat (line_input, "ra-other-config-flag"))
3733 {
3734 other = 1;
3735 break;
3736 }
Chris Luke33879c92016-06-28 19:54:21 -04003737 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003738 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003739 {
Chris Luke33879c92016-06-28 19:54:21 -04003740 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003741 break;
3742 }
Chris Luke33879c92016-06-28 19:54:21 -04003743 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003744 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003745 {
Chris Luke33879c92016-06-28 19:54:21 -04003746 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003747 break;
3748 }
3749 else if (unformat (line_input, "ra-send-unicast"))
3750 {
3751 send_unicast = 1;
3752 break;
3753 }
3754 else if (unformat (line_input, "ra-lifetime"))
3755 {
3756 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003757 {
3758 error = unformat_parse_error (line_input);
3759 goto done;
3760 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003761 use_lifetime = 1;
3762 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003763 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003764 else if (unformat (line_input, "ra-initial"))
3765 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003766 if (!unformat
3767 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003768 {
3769 error = unformat_parse_error (line_input);
3770 goto done;
3771 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003772 break;
3773 }
3774 else if (unformat (line_input, "ra-interval"))
3775 {
3776 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003777 {
3778 error = unformat_parse_error (line_input);
3779 goto done;
3780 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003781
3782 if (!unformat (line_input, "%d", &ra_min_interval))
3783 ra_min_interval = 0;
3784 break;
3785 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003786 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003787 {
3788 cease = 1;
3789 break;
3790 }
3791 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003792 {
3793 error = unformat_parse_error (line_input);
3794 goto done;
3795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003796 }
3797
Dave Barachd7cb1b52016-12-09 09:52:16 -05003798 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003799 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003800 ip6_neighbor_ra_config (vm, sw_if_index,
3801 suppress, managed, other,
3802 suppress_ll_option, send_unicast, cease,
3803 use_lifetime, ra_lifetime,
3804 ra_initial_count, ra_initial_interval,
3805 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003806 }
3807 else
3808 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003809 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003810 u32 pref_lifetime_in_secs = 0;
3811 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003812 u8 no_advertise = 0;
3813 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003814 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003815 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003816
3817 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003818 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003819 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003820 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003821 {
3822 use_prefix_default_values = 1;
3823 break;
3824 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003825 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003826 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003827 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003828 pref_lifetime_in_secs = ~0;
3829 break;
3830 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003831 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3832 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003833 break;
3834 else
3835 break;
3836 }
3837
3838
3839 /* get the rest of the command */
3840 while (!use_prefix_default_values &&
3841 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3842 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003843 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003844 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003845 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003846 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003847 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003848 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003849 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003850 no_onlink = 1;
3851 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003852 {
3853 error = unformat_parse_error (line_input);
3854 goto done;
3855 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003856 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003857
3858 ip6_neighbor_ra_prefix (vm, sw_if_index,
3859 &ip6_addr, addr_len,
3860 use_prefix_default_values,
3861 valid_lifetime_in_secs,
3862 pref_lifetime_in_secs,
3863 no_advertise,
3864 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003865 }
3866
Billy McFalla9a20e72017-02-15 11:39:12 -05003867done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003868 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003869
Ed Warnickecb9cada2015-12-08 15:45:58 -07003870 return error;
3871}
3872
Chris Lukebfd32fd2016-06-24 20:38:06 -04003873static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003874ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003875{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003876 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003877 u32 i;
3878
3879 for (i = 0; i < vec_len (addrs); i++)
3880 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003881 ip_interface_address_t *a =
3882 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3883 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003884
3885 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003886 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003887 }
3888}
3889
Ed Warnickecb9cada2015-12-08 15:45:58 -07003890static clib_error_t *
3891show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003892 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003893{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003894 vnet_main_t *vnm = vnet_get_main ();
3895 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3896 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003897 u32 sw_if_index;
3898
3899 sw_if_index = ~0;
3900
Dave Barachd7cb1b52016-12-09 09:52:16 -05003901 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003902 {
3903 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003904
Dave Barachd7cb1b52016-12-09 09:52:16 -05003905 /* look up the radv_t information for this interface */
3906 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3907 sw_if_index, ~0);
3908
3909 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3910
3911 if (ri != ~0)
3912 {
3913 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3914 ip6_radv_t *radv_info;
3915 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3916
3917 vlib_cli_output (vm, "%U is admin %s\n",
3918 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003919 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003920 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3921 "up" : "down"));
3922
Chris Lukebfd32fd2016-06-24 20:38:06 -04003923 u32 ai;
3924 u32 *link_scope = 0, *global_scope = 0;
3925 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003926 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003927
Dave Barachd7cb1b52016-12-09 09:52:16 -05003928 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3929 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003930 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3931
Dave Barachd7cb1b52016-12-09 09:52:16 -05003932 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003933 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003934 a = pool_elt_at_index (lm->if_address_pool, ai);
3935 ip6_address_t *address =
3936 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003937
Chris Lukebfd32fd2016-06-24 20:38:06 -04003938 if (ip6_address_is_link_local_unicast (address))
3939 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003940 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003941 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003942 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003943 vec_add1 (local_scope, ai);
3944 else
3945 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003946
3947 ai = a->next_this_sw_interface;
3948 }
3949
Dave Barachd7cb1b52016-12-09 09:52:16 -05003950 if (vec_len (link_scope))
3951 {
3952 vlib_cli_output (vm, "\tLink-local address(es):\n");
3953 ip6_print_addrs (vm, link_scope);
3954 vec_free (link_scope);
3955 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003956
Dave Barachd7cb1b52016-12-09 09:52:16 -05003957 if (vec_len (local_scope))
3958 {
3959 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3960 ip6_print_addrs (vm, local_scope);
3961 vec_free (local_scope);
3962 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003963
Dave Barachd7cb1b52016-12-09 09:52:16 -05003964 if (vec_len (global_scope))
3965 {
3966 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3967 ip6_print_addrs (vm, global_scope);
3968 vec_free (global_scope);
3969 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003970
Dave Barachd7cb1b52016-12-09 09:52:16 -05003971 if (vec_len (unknown_scope))
3972 {
3973 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3974 ip6_print_addrs (vm, unknown_scope);
3975 vec_free (unknown_scope);
3976 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003977
Neale Ranns53da2212018-02-24 02:11:19 -08003978 vlib_cli_output (vm, "\tLink-local address(es):\n");
3979 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3980 &radv_info->link_local_address);
3981
Ed Warnickecb9cada2015-12-08 15:45:58 -07003982 vlib_cli_output (vm, "\tJoined group address(es):\n");
3983 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003984 /* *INDENT-OFF* */
3985 pool_foreach (m, radv_info->mldp_group_pool,
3986 ({
3987 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3988 &m->mcast_address);
3989 }));
3990 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003991
3992 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003993 ip6_radv_prefix_t *p;
3994 /* *INDENT-OFF* */
3995 pool_foreach (p, radv_info->adv_prefixes_pool,
3996 ({
3997 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3998 format_ip6_address, &p->prefix, p->prefix_len);
3999 }));
4000 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004001
Dave Barachd7cb1b52016-12-09 09:52:16 -05004002 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004003 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
4004 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
4005 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
4006 vlib_cli_output (vm, "\tND DAD is disabled\n");
4007 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
4008 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
4009 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004010 vlib_cli_output (vm,
4011 "\tND advertised retransmit interval is %d (msec)\n",
4012 radv_info->
4013 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004014
4015 u32 ra_interval = radv_info->max_radv_interval;
4016 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004017 vlib_cli_output (vm,
4018 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004019 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004020 vlib_cli_output (vm,
4021 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004022 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004023 vlib_cli_output (vm,
4024 "\tHosts %s stateless autoconfig for addresses\n",
4025 (radv_info->adv_managed_flag) ? "use" :
4026 " don't use");
4027 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4028 radv_info->n_advertisements_sent);
4029 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4030 radv_info->n_solicitations_rcvd);
4031 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4032 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004033 }
4034 else
4035 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004036 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004037 format_unformat_error, input);
4038
4039 }
4040 }
4041 return error;
4042}
4043
Billy McFall0683c9c2016-10-13 08:27:31 -04004044/*?
4045 * This command is used to display various IPv6 attributes on a given
4046 * interface.
4047 *
4048 * @cliexpar
4049 * Example of how to display IPv6 settings:
4050 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4051 * GigabitEthernet2/0/0 is admin up
4052 * Link-local address(es):
4053 * fe80::ab8/64
4054 * Joined group address(es):
4055 * ff02::1
4056 * ff02::2
4057 * ff02::16
4058 * ff02::1:ff00:ab8
4059 * Advertised Prefixes:
4060 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4061 * MTU is 1500
4062 * ICMP error messages are unlimited
4063 * ICMP redirects are disabled
4064 * ICMP unreachables are not sent
4065 * ND DAD is disabled
4066 * ND advertised reachable time is 0
4067 * ND advertised retransmit interval is 0 (msec)
4068 * ND router advertisements are sent every 200 seconds (min interval is 150)
4069 * ND router advertisements live for 600 seconds
4070 * Hosts use stateless autoconfig for addresses
4071 * ND router advertisements sent 19336
4072 * ND router solicitations received 0
4073 * ND router solicitations dropped 0
4074 * @cliexend
4075 * Example of output if IPv6 is not enabled on the interface:
4076 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4077 * show ip6 interface: IPv6 not enabled on interface
4078 * @cliexend
4079?*/
4080/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004081VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4082{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004083 .path = "show ip6 interface",
4084 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004085 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004086};
Billy McFall0683c9c2016-10-13 08:27:31 -04004087/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004088
4089clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004090disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004091{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004092 clib_error_t *error = 0;
4093 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004094 u32 ri;
4095
4096 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004097 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4098 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004099 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004100
Ed Warnickecb9cada2015-12-08 15:45:58 -07004101 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004102 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004103 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004104 ip6_radv_t *radv_info;
4105
4106 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004107
4108 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004109 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004110 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004111 {
4112 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004113 ip6_ll_prefix_t ilp = {
4114 .ilp_addr = radv_info->link_local_address,
4115 .ilp_sw_if_index = sw_if_index,
4116 };
4117 ip6_ll_table_entry_delete (&ilp);
4118 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004119 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004120 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4121 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004122 }
4123 }
4124 return error;
4125}
4126
4127int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004128ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004129{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004130 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4131 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004132
Dave Barachd7cb1b52016-12-09 09:52:16 -05004133 /* look up the radv_t information for this interface */
4134 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4135 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004136
Dave Barachd7cb1b52016-12-09 09:52:16 -05004137 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004138
Dave Barachd7cb1b52016-12-09 09:52:16 -05004139 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004140}
4141
Dave Barachd7cb1b52016-12-09 09:52:16 -05004142clib_error_t *
4143enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004144{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004145 clib_error_t *error = 0;
4146 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004147 u32 ri;
4148 int is_add = 1;
4149
4150 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004151 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4152 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004153
Dave Barachd7cb1b52016-12-09 09:52:16 -05004154 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4155
4156 /* if not created yet */
4157 if (ri == ~0)
4158 {
4159 vnet_main_t *vnm = vnet_get_main ();
4160 vnet_sw_interface_t *sw_if0;
4161
4162 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4163 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4164 {
4165 ethernet_interface_t *eth_if0;
4166
4167 eth_if0 =
4168 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4169 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004170 {
4171 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004172 ri =
4173 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004174
Dave Barachd7cb1b52016-12-09 09:52:16 -05004175 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004176 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004177 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004178 ip6_address_t link_local_address;
4179
Dave Barachd7cb1b52016-12-09 09:52:16 -05004180 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004181
Dave Barachd7cb1b52016-12-09 09:52:16 -05004182 ip6_link_local_address_from_ethernet_mac_address
4183 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004184
4185 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004186 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07004187 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004188 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004189 {
4190 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004191 link_local_address.as_u64[1] =
4192 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004193
4194 link_local_address.as_u64[0] =
4195 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004196 /* clear u bit */
4197 link_local_address.as_u8[8] &= 0xfd;
4198 }
Neale Ranns53da2212018-02-24 02:11:19 -08004199 {
4200 ip6_ll_prefix_t ilp = {
4201 .ilp_addr = link_local_address,
4202 .ilp_sw_if_index = sw_if_index,
4203 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004204
Neale Ranns53da2212018-02-24 02:11:19 -08004205 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4206 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004207
Ed Warnickecb9cada2015-12-08 15:45:58 -07004208 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004209 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4210 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004211
Neale Ranns53da2212018-02-24 02:11:19 -08004212 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004213 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004214 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004215 }
4216 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004217 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004218 }
4219 }
4220 return error;
4221}
4222
Neale Ranns53da2212018-02-24 02:11:19 -08004223int
4224ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4225{
4226 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4227 ip6_radv_t *radv_info;
4228 u32 ri;
4229
Eyal Baricd307742018-07-22 12:45:15 +03004230 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
Neale Ranns53da2212018-02-24 02:11:19 -08004231 return 0;
4232
4233 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4234
4235 if (ri == ~0)
4236 return 0;
4237
4238 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4239 *addr = radv_info->link_local_address;
4240
4241 return (!0);
4242}
4243
Ed Warnickecb9cada2015-12-08 15:45:58 -07004244static clib_error_t *
4245enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004246 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004247{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004248 vnet_main_t *vnm = vnet_get_main ();
4249 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004250 u32 sw_if_index;
4251
4252 sw_if_index = ~0;
4253
Dave Barachd7cb1b52016-12-09 09:52:16 -05004254 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004255 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004256 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004257 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004258 else
4259 {
4260 error = clib_error_return (0, "unknown interface\n'",
4261 format_unformat_error, input);
4262
4263 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004264 return error;
4265}
4266
Billy McFall0683c9c2016-10-13 08:27:31 -04004267/*?
4268 * This command is used to enable IPv6 on a given interface.
4269 *
4270 * @cliexpar
4271 * Example of how enable IPv6 on a given interface:
4272 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4273?*/
4274/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004275VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4276{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004277 .path = "enable ip6 interface",
4278 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004279 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004280};
Billy McFall0683c9c2016-10-13 08:27:31 -04004281/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004282
4283static clib_error_t *
4284disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004285 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004286{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004287 vnet_main_t *vnm = vnet_get_main ();
4288 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004289 u32 sw_if_index;
4290
4291 sw_if_index = ~0;
4292
Dave Barachd7cb1b52016-12-09 09:52:16 -05004293 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004294 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004295 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004296 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004297 else
4298 {
4299 error = clib_error_return (0, "unknown interface\n'",
4300 format_unformat_error, input);
4301
4302 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004303 return error;
4304}
4305
Billy McFall0683c9c2016-10-13 08:27:31 -04004306/*?
4307 * This command is used to disable IPv6 on a given interface.
4308 *
4309 * @cliexpar
4310 * Example of how disable IPv6 on a given interface:
4311 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4312?*/
4313/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004314VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4315{
Billy McFall0683c9c2016-10-13 08:27:31 -04004316 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004317 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004318 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004319};
Billy McFall0683c9c2016-10-13 08:27:31 -04004320/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004321
Billy McFall0683c9c2016-10-13 08:27:31 -04004322/*?
4323 * This command is used to configure the neighbor discovery
4324 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4325 * command to display some of the current neighbor discovery parameters
4326 * on a given interface. This command has three formats:
4327 *
4328 *
4329 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4330 *
4331 * '<em><b>ip6 nd <interface> [no] [ra-managed-config-flag] | [ra-other-config-flag] | [ra-suppress] | [ra-suppress-link-layer] | [ra-send-unicast] | [ra-lifetime <lifetime>] | [ra-initial <cnt> <interval>] | [ra-interval <max-interval> [<min-interval>]] | [ra-cease]</b></em>'
4332 *
4333 * Where:
4334 *
4335 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4336 * router-advertisement messages to use stateful address
4337 * auto-configuration to obtain address information (sets the M-bit).
4338 * Default is the M-bit is not set and the '<em>no</em>' option
4339 * returns it to this default state.
4340 *
4341 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4342 * router-advertisement messages that hosts use stateful auto
4343 * configuration to obtain nonaddress related information (sets
4344 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4345 * option returns it to this default state.
4346 *
4347 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4348 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4349 * router-advertisement messages.
4350 *
4351 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4352 * optional source link-layer address in the ICMPv6 router-advertisement
4353 * messages. Default is to include the optional source link-layer address
4354 * and the '<em>no</em>' option returns it to this default state.
4355 *
4356 * <em>[no] ra-send-unicast</em> - Use the source address of the
4357 * router-solicitation message if availiable. The default is to use
4358 * multicast address of all nodes, and the '<em>no</em>' option returns
4359 * it to this default state.
4360 *
4361 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4362 * default router in ICMPv6 router-advertisement messages. The range is
4363 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4364 * '<em><max-interval></em>'. The default value is 600 seconds and the
4365 * '<em>no</em>' option returns it to this default value.
4366 *
4367 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4368 * router-advertisement messages sent and the interval between each
4369 * message. Range for count is 1 - 3 and default is 3. Range for interval
4370 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4371 * returns both to their default value.
4372 *
4373 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4374 * interval between sending ICMPv6 router-advertisement messages. The
4375 * range for max-interval is from 4 to 200 seconds. min-interval can not
4376 * be more than 75% of max-interval. If not set, min-interval will be
4377 * set to 75% of max-interval. The range for min-interval is from 3 to
4378 * 150 seconds. The '<em>no</em>' option returns both to their default
4379 * value.
4380 *
4381 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4382 * The '<em>no</em>' options implies to start (or restart) sending
4383 * ICMPv6 router-advertisement messages.
4384 *
4385 *
4386 * <b>Format 2 - Prefix Options:</b>
4387 *
4388 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> [<valid-lifetime> <pref-lifetime> | infinite] [no-advertise] [off-link] [no-autoconfig] [no-onlink]</b></em>'
4389 *
4390 * Where:
4391 *
4392 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4393 *
4394 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4395 * length of time in seconds during what the prefix is valid for the purpose of
4396 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4397 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4398 * length of time in seconds during what addresses generated from the prefix remain
4399 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4400 *
4401 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4402 * are inifinte, no timeout.
4403 *
4404 * <em>no-advertise</em> - Do not send full router address in prefix
4405 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4406 *
4407 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4408 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4409 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4410 *
4411 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4412 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4413 *
4414 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4415 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4416 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4417 * the L-bit.
4418 *
4419 *
4420 * <b>Format 3: - Default of Prefix:</b>
4421 *
4422 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4423 *
4424 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4425 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4426 * is ignored and the prefix is deleted.
4427 *
4428 *
4429 * @cliexpar
4430 * Example of how set a router advertisement option:
4431 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4432 * Example of how to add a prefix:
4433 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4434 * Example of how to delete a prefix:
4435 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4436?*/
4437/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004438VLIB_CLI_COMMAND (ip6_nd_command, static) =
4439{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004440 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004441 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004442 .function = ip6_neighbor_cmd,
4443};
Billy McFall0683c9c2016-10-13 08:27:31 -04004444/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004445
4446clib_error_t *
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004447ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4448 ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004449{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004450 clib_error_t *error = 0;
4451 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004452 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004453 ip6_radv_t *radv_info;
4454 vnet_main_t *vnm = vnet_get_main ();
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004455 ip6_ll_prefix_t pfx = { 0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07004456
Dave Barachd7cb1b52016-12-09 09:52:16 -05004457 if (!ip6_address_is_link_local_unicast (address))
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004458 return (error = clib_error_return (0, "address not link-local",
4459 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004460
4461 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004462 enable_ip6_interface (vm, sw_if_index);
4463
Ed Warnickecb9cada2015-12-08 15:45:58 -07004464 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004465
4466 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004467 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004468 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004469
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004470 pfx.ilp_sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004471
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004472 pfx.ilp_addr = radv_info->link_local_address;
4473 ip6_ll_table_entry_delete (&pfx);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004474
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004475 pfx.ilp_addr = *address;
4476 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004477
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004478 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4479 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004480 }
4481 else
4482 {
4483 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4484 error = clib_error_return (0, "ip6 not enabled for interface",
4485 format_unformat_error);
4486 }
4487 return error;
4488}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004489
Neale Ranns87df12d2017-02-18 08:16:41 -08004490/**
4491 * @brief callback when an interface address is added or deleted
4492 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004493static void
4494ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4495 uword opaque,
4496 u32 sw_if_index,
4497 ip6_address_t * address,
4498 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004499 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004500{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004501 vnet_main_t *vnm = vnet_get_main ();
4502 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004503 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004504 vlib_main_t *vm = vnm->vlib_main;
4505 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004506 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004507
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07004508 /* create solicited node multicast address for this interface address */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004509 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004510
Ed Warnickecb9cada2015-12-08 15:45:58 -07004511 a.as_u8[0xd] = address->as_u8[0xd];
4512 a.as_u8[0xe] = address->as_u8[0xe];
4513 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004514
4515 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004516 {
4517 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004518 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004519
4520 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004521 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4522 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004523 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004524 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004525 {
4526 /* get radv_info */
4527 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4528
4529 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004530 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004531 radv_info->ref_count++;
4532
Neale Ranns87df12d2017-02-18 08:16:41 -08004533 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004534 }
4535 }
4536 else
4537 {
4538
4539 /* delete */
4540 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004541 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4542 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004543 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004544
Dave Barachd7cb1b52016-12-09 09:52:16 -05004545 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004546 {
4547 /* get radv_info */
4548 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4549
Neale Ranns87df12d2017-02-18 08:16:41 -08004550 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004551
4552 /* if interface up send MLDP "report" */
4553 radv_info->all_routers_mcast = 0;
4554
4555 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004556 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004557 radv_info->ref_count--;
4558 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004559 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4560 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004561 }
4562}
4563
Dave Barachd7cb1b52016-12-09 09:52:16 -05004564clib_error_t *
4565ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004566{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004567 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004568
4569 nm->limit_neighbor_cache_size = neighbor_limit;
4570 return 0;
4571}
4572
Neale Ranns15002542017-09-10 04:39:11 -07004573static void
4574ip6_neighbor_table_bind (ip6_main_t * im,
4575 uword opaque,
4576 u32 sw_if_index,
4577 u32 new_fib_index, u32 old_fib_index)
4578{
4579 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4580 ip6_neighbor_t *n = NULL;
4581 u32 i, *to_re_add = 0;
4582
4583 /* *INDENT-OFF* */
4584 pool_foreach (n, nm->neighbor_pool,
4585 ({
4586 if (n->key.sw_if_index == sw_if_index)
4587 vec_add1 (to_re_add, n - nm->neighbor_pool);
4588 }));
4589 /* *INDENT-ON* */
4590
4591 for (i = 0; i < vec_len (to_re_add); i++)
4592 {
4593 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4594 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4595 ip6_neighbor_adj_fib_add (n, new_fib_index);
4596 }
4597 vec_free (to_re_add);
4598}
4599
Dave Barachd7cb1b52016-12-09 09:52:16 -05004600static clib_error_t *
4601ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004602{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004603 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4604 ip6_main_t *im = &ip6_main;
4605
Ed Warnickecb9cada2015-12-08 15:45:58 -07004606 mhash_init (&nm->neighbor_index_by_key,
4607 /* value size */ sizeof (uword),
4608 /* key size */ sizeof (ip6_neighbor_key_t));
4609
Dave Barachd7cb1b52016-12-09 09:52:16 -05004610 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4611 ip6_icmp_neighbor_solicitation_node.index);
4612 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4613 ip6_icmp_neighbor_advertisement_node.index);
4614 icmp6_register_type (vm, ICMP6_router_solicitation,
4615 ip6_icmp_router_solicitation_node.index);
4616 icmp6_register_type (vm, ICMP6_router_advertisement,
4617 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004618
4619 /* handler node for ip6 neighbor discovery events and timers */
4620 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4621
4622 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004623 ip6_add_del_interface_address_callback_t cb;
Dave Barachb7b92992018-10-17 10:38:51 -04004624 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05004625
Ed Warnickecb9cada2015-12-08 15:45:58 -07004626 /* when an interface address changes... */
4627 cb.function = ip6_neighbor_add_del_interface_address;
4628 cb.function_opaque = 0;
4629 vec_add1 (im->add_del_interface_address_callbacks, cb);
4630
Neale Ranns15002542017-09-10 04:39:11 -07004631 ip6_table_bind_callback_t cbt;
4632 cbt.function = ip6_neighbor_table_bind;
4633 cbt.function_opaque = 0;
4634 vec_add1 (im->table_bind_callbacks, cbt);
4635
Ed Warnickecb9cada2015-12-08 15:45:58 -07004636 mhash_init (&nm->pending_resolutions_by_address,
4637 /* value size */ sizeof (uword),
4638 /* key size */ sizeof (ip6_address_t));
4639
John Lo1edfba92016-08-27 01:11:57 -04004640 mhash_init (&nm->mac_changes_by_address,
4641 /* value size */ sizeof (uword),
4642 /* key size */ sizeof (ip6_address_t));
4643
Ed Warnickecb9cada2015-12-08 15:45:58 -07004644 /* default, configurable */
4645 nm->limit_neighbor_cache_size = 50000;
4646
Eyal Baric125ecc2017-09-20 11:29:17 +03004647 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4648
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004649 nm->ip6_ra_publisher_node = (uword) ~ 0;
4650
Ed Warnickecb9cada2015-12-08 15:45:58 -07004651#if 0
4652 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004653 vec_validate_init_empty
4654 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004655#endif
4656
Ed Warnickecb9cada2015-12-08 15:45:58 -07004657 return 0;
4658}
4659
4660VLIB_INIT_FUNCTION (ip6_neighbor_init);
4661
4662
Dave Barachd7cb1b52016-12-09 09:52:16 -05004663void
4664vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4665 void *address_arg,
4666 uword node_index,
4667 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004668{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004669 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4670 ip6_address_t *address = address_arg;
4671 uword *p;
4672 pending_resolution_t *pr;
4673
Ed Warnickecb9cada2015-12-08 15:45:58 -07004674 pool_get (nm->pending_resolutions, pr);
Dave Barachd318a992019-11-10 15:46:31 -05004675 memset (pr, 0, sizeof (*pr));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004676
4677 pr->next_index = ~0;
4678 pr->node_index = node_index;
4679 pr->type_opaque = type_opaque;
4680 pr->data = data;
4681
4682 p = mhash_get (&nm->pending_resolutions_by_address, address);
4683 if (p)
4684 {
4685 /* Insert new resolution at the head of the list */
4686 pr->next_index = p[0];
4687 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4688 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004689
4690 mhash_set (&nm->pending_resolutions_by_address, address,
4691 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004692}
4693
Dave Barachd7cb1b52016-12-09 09:52:16 -05004694int
4695vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
Neale Ranns37029302018-08-10 05:30:06 -07004696 ip6_nd_change_event_cb_t data_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004697 u32 pid,
4698 void *address_arg,
4699 uword node_index,
4700 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004701{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004702 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4703 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004704
Eyal Barif9f40652017-04-01 03:55:08 +03004705 /* Try to find an existing entry */
4706 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4707 u32 *p = first;
4708 pending_resolution_t *mc;
4709 while (p && *p != ~0)
4710 {
4711 mc = pool_elt_at_index (nm->mac_changes, *p);
4712 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4713 && mc->pid == pid)
4714 break;
4715 p = &mc->next_index;
4716 }
4717
4718 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004719 if (is_add)
4720 {
Eyal Barif9f40652017-04-01 03:55:08 +03004721 if (found)
4722 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4723
John Lo1edfba92016-08-27 01:11:57 -04004724 pool_get (nm->mac_changes, mc);
Dave Barachd318a992019-11-10 15:46:31 -05004725 memset (mc, 0, sizeof (*mc));
Neale Rannsf12dad62018-06-04 18:41:24 -07004726 /* *INDENT-OFF* */
Eyal Barif9f40652017-04-01 03:55:08 +03004727 *mc = (pending_resolution_t)
4728 {
Neale Rannsf12dad62018-06-04 18:41:24 -07004729 .next_index = ~0,
4730 .node_index = node_index,
4731 .type_opaque = type_opaque,
4732 .data = data,
4733 .data_callback = data_callback,
4734 .pid = pid,
4735 };
4736 /* *INDENT-ON* */
John Lo1edfba92016-08-27 01:11:57 -04004737
Eyal Barif9f40652017-04-01 03:55:08 +03004738 /* Insert new resolution at the end of the list */
4739 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004740 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004741 p[0] = new_idx;
4742 else
4743 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004744 }
4745 else
4746 {
Eyal Barif9f40652017-04-01 03:55:08 +03004747 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004748 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004749
Eyal Barif9f40652017-04-01 03:55:08 +03004750 /* Clients may need to clean up pool entries, too */
Neale Ranns37029302018-08-10 05:30:06 -07004751 if (data_callback)
4752 (data_callback) (mc->data, NULL /* no new mac addrs */ , 0, NULL);
John Lo1edfba92016-08-27 01:11:57 -04004753
Eyal Barif9f40652017-04-01 03:55:08 +03004754 /* Remove the entry from the list and delete the entry */
4755 *p = mc->next_index;
4756 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004757
Eyal Barif9f40652017-04-01 03:55:08 +03004758 /* Remove from hash if we deleted the last entry */
4759 if (*p == ~0 && p == first)
4760 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004761 }
Eyal Barif9f40652017-04-01 03:55:08 +03004762 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004763}
4764
Dave Barachd7cb1b52016-12-09 09:52:16 -05004765int
4766vnet_ip6_nd_term (vlib_main_t * vm,
4767 vlib_node_runtime_t * node,
4768 vlib_buffer_t * p0,
4769 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004770 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004771{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004772 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4773 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
Neale Ranns37029302018-08-10 05:30:06 -07004774 mac_address_t mac;
John Lo1edfba92016-08-27 01:11:57 -04004775
Neale Ranns37029302018-08-10 05:30:06 -07004776 mac_address_from_bytes (&mac, eth->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004777 ndh = ip6_next_header (ip);
4778 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4779 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004780 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004781
4782 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4783 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4784 {
4785 u8 *t0 = vlib_add_trace (vm, node, p0,
4786 sizeof (icmp6_input_trace_t));
4787 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4788 }
4789
4790 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004791 if (PREDICT_FALSE
4792 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4793 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004794 {
Neale Ranns37029302018-08-10 05:30:06 -07004795 vnet_nd_wc_publish (sw_if_index, &mac, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004796 }
4797
4798 /* Check if MAC entry exsist for solicited target IP */
4799 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4800 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004801 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004802 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004803 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004804
4805 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004806 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004807 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4808 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004809 return 0; /* source link layer address option not present */
4810
John Lo1edfba92016-08-27 01:11:57 -04004811 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004812 macp =
4813 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004814 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004815 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004816 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004817 vlib_node_runtime_t *error_node =
4818 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004819 ip->dst_address = ip->src_address;
4820 ip->src_address = ndh->target_address;
4821 ip->hop_limit = 255;
4822 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004823 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004824 clib_memcpy (opt->ethernet_address, macp, 6);
4825 ndh->icmp.type = ICMP6_neighbor_advertisement;
4826 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004827 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4828 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004829 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004830 ndh->icmp.checksum =
4831 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4832 clib_memcpy (eth->dst_address, eth->src_address, 6);
4833 clib_memcpy (eth->src_address, macp, 6);
4834 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004835 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004836 return 1;
4837 }
John Lo1edfba92016-08-27 01:11:57 -04004838 }
4839
4840 return 0;
4841
4842}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004843
Neale Ranns3f844d02017-02-18 00:03:54 -08004844int
4845ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4846{
4847 u32 fib_index;
4848
4849 fib_prefix_t pfx = {
4850 .fp_len = 128,
4851 .fp_proto = FIB_PROTOCOL_IP6,
4852 .fp_addr = {
4853 .ip6 = *addr,
4854 },
4855 };
4856 ip46_address_t nh = {
4857 .ip6 = *addr,
4858 };
4859
4860 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4861
4862 if (~0 == fib_index)
4863 return VNET_API_ERROR_NO_SUCH_FIB;
4864
4865 if (is_del)
4866 {
4867 fib_table_entry_path_remove (fib_index,
4868 &pfx,
4869 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004870 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004871 &nh,
4872 sw_if_index,
4873 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4874 /* flush the ND cache of this address if it's there */
Neale Ranns0bdd3192018-09-07 11:04:52 -07004875 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
Neale Ranns3f844d02017-02-18 00:03:54 -08004876 }
4877 else
4878 {
4879 fib_table_entry_path_add (fib_index,
4880 &pfx,
4881 FIB_SOURCE_IP6_ND_PROXY,
4882 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004883 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004884 &nh,
4885 sw_if_index,
4886 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4887 }
4888 return (0);
4889}
4890
4891static clib_error_t *
4892set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4893 unformat_input_t * input, vlib_cli_command_t * cmd)
4894{
4895 vnet_main_t *vnm = vnet_get_main ();
4896 clib_error_t *error = 0;
4897 ip6_address_t addr;
4898 u32 sw_if_index;
4899 u8 is_del = 0;
4900
4901 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4902 {
4903 /* get the rest of the command */
4904 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4905 {
4906 if (unformat (input, "%U", unformat_ip6_address, &addr))
4907 break;
4908 else if (unformat (input, "delete") || unformat (input, "del"))
4909 is_del = 1;
4910 else
4911 return (unformat_parse_error (input));
4912 }
4913 }
4914
4915 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4916
4917 return error;
4918}
4919
4920/* *INDENT-OFF* */
4921VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4922{
4923 .path = "set ip6 nd proxy",
4924 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4925 .function = set_ip6_nd_proxy_cmd,
4926};
4927/* *INDENT-ON* */
4928
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004929void
Neale Ranns3be6b282016-12-20 14:24:01 +00004930ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004931{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004932 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4933 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004934 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004935
4936 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004937 pool_foreach (n, nm->neighbor_pool,
4938 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004939 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004940 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004941 adj_nbr_walk_nh6 (sw_if_index,
4942 &n->key.ip6_address,
4943 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004944 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004945 }));
4946 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08004947
4948 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4949
4950 if (ADJ_INDEX_INVALID != ai)
4951 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004952}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004953
John Lo8b81cb42017-06-26 01:40:20 -04004954void
Steven9f781d82018-06-05 11:09:32 -07004955send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04004956{
4957 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04004958 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004959
Steven9f781d82018-06-05 11:09:32 -07004960 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004961}
4962
4963void
4964send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07004965 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07004966{
4967 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07004968 vnet_main_t *vnm = vnet_get_main ();
4969 u8 *rewrite, rewrite_len;
4970 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4971 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07004972
John Lo8b81cb42017-06-26 01:40:20 -04004973 if (ip6_addr)
4974 {
4975 clib_warning
4976 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4977 format_ip6_address, ip6_addr, sw_if_index);
4978
4979 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4980 int bogus_length;
4981 u32 bi = 0;
4982 icmp6_neighbor_solicitation_header_t *h =
4983 vlib_packet_template_get_packet (vm,
4984 &i6m->discover_neighbor_packet_template,
4985 &bi);
John Lo084606b2018-06-19 15:27:48 -04004986 if (!h)
4987 return;
4988
John Lo8b81cb42017-06-26 01:40:20 -04004989 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4990 IP6_MULTICAST_SCOPE_link_local,
4991 IP6_MULTICAST_GROUP_ID_all_hosts);
4992 h->ip.src_address = ip6_addr[0];
4993 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4994 h->neighbor.target_address = ip6_addr[0];
4995 h->neighbor.advertisement_flags = clib_host_to_net_u32
4996 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
ahdj00722130e12018-08-14 10:35:43 +08004997 h->link_layer_option.header.type =
4998 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo8b81cb42017-06-26 01:40:20 -04004999 clib_memcpy (h->link_layer_option.ethernet_address,
5000 hi->hw_address, vec_len (hi->hw_address));
5001 h->neighbor.icmp.checksum =
5002 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
5003 ASSERT (bogus_length == 0);
5004
5005 /* Setup MAC header with IP6 Etype and mcast DMAC */
5006 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07005007 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04005008 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07005009 rewrite =
5010 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5011 rewrite_len = vec_len (rewrite);
5012 vlib_buffer_advance (b, -rewrite_len);
5013 ethernet_header_t *e = vlib_buffer_get_current (b);
5014 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5015 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04005016
5017 /* Send unsolicited ND advertisement packet out the specified interface */
5018 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5019 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5020 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5021 u32 *to_next = vlib_frame_vector_args (f);
5022 to_next[0] = bi;
5023 f->n_vectors = 1;
5024 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5025 }
5026}
5027
Dave Barachd7cb1b52016-12-09 09:52:16 -05005028/*
5029 * fd.io coding-style-patch-verification: ON
5030 *
5031 * Local Variables:
5032 * eval: (c-set-style "gnu")
5033 * End:
5034 */