blob: 071d3eb667329ccddfa1c1f295d0a4b1bffc265c [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
810 pool_get (nm->neighbor_pool, n);
811
Dave Barachd7cb1b52016-12-09 09:52:16 -0500812 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
813 /* old value */ 0);
814 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700815 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100816
Neale Ranns37029302018-08-10 05:30:06 -0700817 mac_address_copy (&n->mac, mac);
Neale Rannsb80c5362016-10-08 13:03:40 +0100818
Dave Barachd7cb1b52016-12-09 09:52:16 -0500819 /*
820 * create the adj-fib. the entry in the FIB table for and to the peer.
821 */
Neale Ranns37029302018-08-10 05:30:06 -0700822 if (!(flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY))
Neale Rannsb3b2de72017-03-08 05:17:22 -0800823 {
Neale Ranns53da2212018-02-24 02:11:19 -0800824 ip6_neighbor_adj_fib_add
825 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700826 }
827 else
828 {
Neale Ranns37029302018-08-10 05:30:06 -0700829 n->flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800830 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500831 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100832 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500833 {
834 /*
835 * prevent a DoS attack from the data-plane that
836 * spams us with no-op updates to the MAC address
837 */
Neale Ranns37029302018-08-10 05:30:06 -0700838 if (0 == mac_address_cmp (&n->mac, mac))
John Lo7f358b32018-04-28 01:19:24 -0400839 {
840 n->time_last_updated = vlib_time_now (vm);
841 goto check_customers;
842 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100843
Neale Ranns37029302018-08-10 05:30:06 -0700844 mac_address_copy (&n->mac, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500845 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846
Neale Rannsb80c5362016-10-08 13:03:40 +0100847 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400848 n->time_last_updated = vlib_time_now (vm);
Neale Ranns37029302018-08-10 05:30:06 -0700849 if (flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo4fed5b12018-05-22 03:35:06 -0400850 {
Neale Ranns37029302018-08-10 05:30:06 -0700851 n->flags |= IP_NEIGHBOR_FLAG_STATIC;
852 n->flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
John Lo4fed5b12018-05-22 03:35:06 -0400853 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100854 else
John Lo4fed5b12018-05-22 03:35:06 -0400855 {
Neale Ranns37029302018-08-10 05:30:06 -0700856 n->flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
857 n->flags &= ~IP_NEIGHBOR_FLAG_STATIC;
John Lo4fed5b12018-05-22 03:35:06 -0400858 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100859
Neale Rannsb80c5362016-10-08 13:03:40 +0100860 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500861 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
Dave Barach59b25652017-09-10 15:04:27 -0400863check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864 /* Customer(s) waiting for this address to be resolved? */
865 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400866 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 {
John Lo1edfba92016-08-27 01:11:57 -0400868 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500869
870 while (next_index != (u32) ~ 0)
871 {
John Lo1edfba92016-08-27 01:11:57 -0400872 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
873 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500874 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400875 next_index = pr->next_index;
876 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500877 }
John Lo1edfba92016-08-27 01:11:57 -0400878
Neale Ranns0bdd3192018-09-07 11:04:52 -0700879 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 }
881
John Lo1edfba92016-08-27 01:11:57 -0400882 /* Customer(s) requesting ND event for this address? */
883 p = mhash_get (&nm->mac_changes_by_address, a);
884 if (p)
885 {
886 next_index = p[0];
887
Dave Barachd7cb1b52016-12-09 09:52:16 -0500888 while (next_index != (u32) ~ 0)
889 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500890 int rv = 1;
Neale Ranns37029302018-08-10 05:30:06 -0700891
Dave Barachd7cb1b52016-12-09 09:52:16 -0500892 mc = pool_elt_at_index (nm->mac_changes, next_index);
John Lo1edfba92016-08-27 01:11:57 -0400893
Dave Barachd7cb1b52016-12-09 09:52:16 -0500894 /* Call the user's data callback, return 1 to suppress dup events */
Neale Ranns37029302018-08-10 05:30:06 -0700895 if (mc->data_callback)
896 rv = (mc->data_callback) (mc->data, mac, sw_if_index, &ip6a_zero);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500897 /*
898 * Signal the resolver process, as long as the user
899 * says they want to be notified
900 */
901 if (rv == 0)
902 vlib_process_signal_event (vm, mc->node_index,
903 mc->type_opaque, mc->data);
904 next_index = mc->next_index;
905 }
John Lo1edfba92016-08-27 01:11:57 -0400906 }
907
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 return 0;
909}
910
911int
912vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700913 u32 sw_if_index, const ip6_address_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500915 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500917 ip6_neighbor_t *n;
918 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 int rv = 0;
920
Damjan Marion586afd72017-04-05 19:18:20 +0200921 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 {
Neale Ranns37029302018-08-10 05:30:06 -0700923 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL, 0,
924 IP_NEIGHBOR_FLAG_NONE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 return 0;
926 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
928 k.sw_if_index = sw_if_index;
929 k.ip6_address = a[0];
930 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500931
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 p = mhash_get (&nm->neighbor_index_by_key, &k);
933 if (p == 0)
934 {
935 rv = -1;
936 goto out;
937 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500938
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100940
Neale Rannsb80c5362016-10-08 13:03:40 +0100941 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500942 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400943 ip6_neighbor_adj_fib_remove
944 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100945
John Lo4fed5b12018-05-22 03:35:06 -0400946 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500948
949out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 return rv;
951}
952
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953static void ip6_neighbor_set_unset_rpc_callback
954 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500956 vlib_main_t *vm = vlib_get_main ();
957 if (a->is_add)
958 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Ranns37029302018-08-10 05:30:06 -0700959 &a->mac, a->flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700961 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963
964static int
965ip6_neighbor_sort (void *a1, void *a2)
966{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500967 vnet_main_t *vnm = vnet_get_main ();
968 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500970 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
971 n2->key.sw_if_index);
972 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
974 return cmp;
975}
976
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100977ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -0400978ip6_neighbors_pool (void)
979{
980 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
981 return nm->neighbor_pool;
982}
983
984ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100985ip6_neighbors_entries (u32 sw_if_index)
986{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500987 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Neale Ranns37029302018-08-10 05:30:06 -0700988 ip6_neighbor_t *n, *ns = NULL;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100989
990 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500991 pool_foreach (n, nm->neighbor_pool,
992 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100993 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
994 continue;
995 vec_add1 (ns, n[0]);
996 }));
997 /* *INDENT-ON* */
998
999 if (ns)
1000 vec_sort_with_function (ns, ip6_neighbor_sort);
1001 return ns;
1002}
1003
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004static clib_error_t *
1005show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001006 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001008 vnet_main_t *vnm = vnet_get_main ();
1009 ip6_neighbor_t *n, *ns;
1010 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 u32 sw_if_index;
Dave Barach1bb981d2019-02-26 17:04:40 -05001012 int verbose = 0;
1013
1014 if (unformat (input, "verbose"))
1015 verbose = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016
1017 /* Filter entries by interface if given. */
1018 sw_if_index = ~0;
1019 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1020
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001021 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001022 if (ns)
1023 {
Dave Barach1bb981d2019-02-26 17:04:40 -05001024 /*
1025 * Show the entire table if it's not too big, otherwise just
1026 * show the size of the table.
1027 */
1028 if (vec_len (ns) < 50)
1029 verbose = 1;
1030 if (verbose)
1031 {
1032 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
1033 vec_foreach (n, ns)
1034 {
1035 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
1036 }
1037 }
1038 else
1039 vlib_cli_output
1040 (vm, "There are %u ip6 neighbors, "
1041 "'show ip6 neighbors verbose' to display the entire table...",
1042 vec_len (ns));
Billy McFall46529cd2016-10-14 10:45:49 -04001043 vec_free (ns);
1044 }
Dave Barach1bb981d2019-02-26 17:04:40 -05001045 else
1046 vlib_cli_output (vm, "No ip6 neighbors");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047
1048 return error;
1049}
1050
Billy McFall0683c9c2016-10-13 08:27:31 -04001051/*?
1052 * This command is used to display the adjacent IPv6 hosts found via
1053 * neighbor discovery. Optionally, limit the output to the specified
1054 * interface.
1055 *
1056 * @cliexpar
1057 * Example of how to display the IPv6 neighbor adjacency table:
1058 * @cliexstart{show ip6 neighbors}
1059 * Time Address Flags Link layer Interface
1060 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1061 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1062 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1063 * @cliexend
1064 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1065 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1066 * Time Address Flags Link layer Interface
1067 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1068 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1069 * @cliexend
1070?*/
1071/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1073 .path = "show ip6 neighbors",
1074 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001075 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076};
Billy McFall0683c9c2016-10-13 08:27:31 -04001077/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078
1079static clib_error_t *
1080set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001081 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082{
Neale Ranns37029302018-08-10 05:30:06 -07001083 ip_neighbor_flags_t flags = IP_NEIGHBOR_FLAG_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001084 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085 ip6_address_t addr;
Neale Ranns37029302018-08-10 05:30:06 -07001086 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087 int addr_valid = 0;
1088 int is_del = 0;
1089 u32 sw_if_index;
1090
Dave Barachd7cb1b52016-12-09 09:52:16 -05001091 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092 {
1093 /* intfc, ip6-address, mac-address */
1094 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001095 unformat_vnet_sw_interface, vnm, &sw_if_index,
1096 unformat_ip6_address, &addr,
Neale Ranns37029302018-08-10 05:30:06 -07001097 unformat_mac_address_t, &mac))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001098 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099
1100 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001101 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001102 else if (unformat (input, "static"))
Neale Ranns37029302018-08-10 05:30:06 -07001103 flags |= IP_NEIGHBOR_FLAG_STATIC;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001104 else if (unformat (input, "no-fib-entry"))
Neale Ranns37029302018-08-10 05:30:06 -07001105 flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001107 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108 }
1109
1110 if (!addr_valid)
1111 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001112
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 if (!is_del)
Neale Ranns37029302018-08-10 05:30:06 -07001114 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr, &mac, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 else
Neale Ranns0bdd3192018-09-07 11:04:52 -07001116 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117 return 0;
1118}
1119
Billy McFall0683c9c2016-10-13 08:27:31 -04001120/*?
1121 * This command is used to manually add an entry to the IPv6 neighbor
1122 * adjacency table. Optionally, the entry can be added as static. It is
1123 * also used to remove an entry from the table. Use the '<em>show ip6
1124 * neighbors</em>' command to display all learned and manually entered entries.
1125 *
1126 * @cliexpar
1127 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1128 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1129 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1130 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1131?*/
1132/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001133VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1134{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135 .path = "set ip6 neighbor",
1136 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001137 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138};
Billy McFall0683c9c2016-10-13 08:27:31 -04001139/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140
Dave Barachd7cb1b52016-12-09 09:52:16 -05001141typedef enum
1142{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1144 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1145 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1146} icmp6_neighbor_solicitation_or_advertisement_next_t;
1147
1148static_always_inline uword
1149icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1150 vlib_node_runtime_t * node,
1151 vlib_frame_t * frame,
1152 uword is_solicitation)
1153{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001154 vnet_main_t *vnm = vnet_get_main ();
1155 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001156 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001157 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1159 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001160 vlib_node_runtime_t *error_node =
1161 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162 int bogus_length;
1163
1164 from = vlib_frame_vector_args (frame);
1165 n_left_from = n_packets;
1166 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001167
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 if (node->flags & VLIB_NODE_FLAG_TRACE)
1169 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1170 /* stride */ 1,
1171 sizeof (icmp6_input_trace_t));
1172
Dave Barachd7cb1b52016-12-09 09:52:16 -05001173 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001174 (is_solicitation
1175 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1176 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1177 n_advertisements_sent = 0;
1178
1179 while (n_left_from > 0)
1180 {
1181 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1182
1183 while (n_left_from > 0 && n_left_to_next > 0)
1184 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001185 vlib_buffer_t *p0;
1186 ip6_header_t *ip0;
1187 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1188 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001190 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1191 int is_rewrite0;
1192 u32 ni0;
1193
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194 bi0 = to_next[0] = from[0];
1195
1196 from += 1;
1197 to_next += 1;
1198 n_left_from -= 1;
1199 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001200
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201 p0 = vlib_get_buffer (vm, bi0);
1202 ip0 = vlib_buffer_get_current (p0);
1203 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001204 options_len0 =
1205 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
1207 error0 = ICMP6_ERROR_NONE;
1208 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001209 ip6_sadd_link_local =
1210 ip6_address_is_link_local_unicast (&ip0->src_address);
1211 ip6_sadd_unspecified =
1212 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213
1214 /* Check that source address is unspecified, link-local or else on-link. */
1215 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1216 {
1217 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218
Dave Barachd7cb1b52016-12-09 09:52:16 -05001219 if (ADJ_INDEX_INVALID != src_adj_index0)
1220 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001221 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001222
Dave Barachd7cb1b52016-12-09 09:52:16 -05001223 /* Allow all realistic-looking rewrite adjacencies to pass */
1224 ni0 = adj0->lookup_next_index;
1225 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1226 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001227
Dave Barachd7cb1b52016-12-09 09:52:16 -05001228 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1229 || !is_rewrite0)
1230 ?
1231 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1232 : error0);
1233 }
1234 else
1235 {
1236 error0 =
1237 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1238 }
1239 }
1240
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241 o0 = (void *) (h0 + 1);
1242 o0 = ((options_len0 == 8 && o0->header.type == option_type
1243 && o0->header.n_data_u64s == 1) ? o0 : 0);
1244
1245 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001246 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001247 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001248 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001249 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1250 is_solicitation ?
1251 &ip0->src_address :
1252 &h0->target_address,
Neale Ranns37029302018-08-10 05:30:06 -07001253 (mac_address_t *)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001254 o0->ethernet_address,
Neale Ranns37029302018-08-10 05:30:06 -07001255 IP_NEIGHBOR_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257
1258 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1259 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001260 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001261 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001262 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
Dave Barachd7cb1b52016-12-09 09:52:16 -05001264 fib_index =
1265 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001266
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001267 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001268 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001269 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1270 }
1271 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001272 {
Neale Ranns53da2212018-02-24 02:11:19 -08001273 if (ip6_address_is_link_local_unicast (&h0->target_address))
1274 {
1275 fei = ip6_fib_table_lookup_exact_match
1276 (ip6_ll_fib_get (sw_if_index0),
1277 &h0->target_address, 128);
1278 }
1279 else
1280 {
1281 fei = ip6_fib_table_lookup_exact_match (fib_index,
1282 &h0->target_address,
1283 128);
1284 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001285
Neale Ranns3f844d02017-02-18 00:03:54 -08001286 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001287 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001288 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001289 error0 =
1290 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001291 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001292 else
1293 {
1294 if (FIB_ENTRY_FLAG_LOCAL &
1295 fib_entry_get_flags_for_source (fei,
1296 FIB_SOURCE_INTERFACE))
1297 {
1298 /* It's an address that belongs to one of our interfaces
1299 * that's good. */
1300 }
1301 else
1302 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001303 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1304 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001305 {
1306 /* The address was added by IPv6 Proxy ND config.
1307 * We should only respond to these if the NS arrived on
1308 * the link that has a matching covering prefix */
1309 }
1310 else
1311 {
1312 error0 =
1313 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1314 }
1315 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001317 }
1318
1319 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001320 next0 = (error0 != ICMP6_ERROR_NONE
1321 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1322 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323 else
1324 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001325 next0 = 0;
1326 error0 = error0 == ICMP6_ERROR_NONE ?
1327 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328 }
1329
1330 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1331 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001332 vnet_sw_interface_t *sw_if0;
1333 ethernet_interface_t *eth_if0;
1334 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335
Dave Barachd7cb1b52016-12-09 09:52:16 -05001336 /* dst address is either source address or the all-nodes mcast addr */
1337 if (!ip6_sadd_unspecified)
1338 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001339 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001340 ip6_set_reserved_multicast_address (&ip0->dst_address,
1341 IP6_MULTICAST_SCOPE_link_local,
1342 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343
1344 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001345 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 h0->icmp.type = ICMP6_neighbor_advertisement;
1347
1348 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1349 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001350 eth_if0 =
1351 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001352 if (eth_if0 && o0)
1353 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001354 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1355 o0->header.type =
1356 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001357 }
1358
1359 h0->advertisement_flags = clib_host_to_net_u32
1360 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1361 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1362
1363 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001364 h0->icmp.checksum =
1365 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1366 &bogus_length);
1367 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368
Dave Barachd7cb1b52016-12-09 09:52:16 -05001369 /* Reuse current MAC header, copy SMAC to DMAC and
1370 * interface MAC to SMAC */
1371 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1372 eth0 = vlib_buffer_get_current (p0);
1373 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1374 if (eth_if0)
1375 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376
Dave Barachd7cb1b52016-12-09 09:52:16 -05001377 /* Setup input and output sw_if_index for packet */
1378 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1379 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1380 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1381 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382
1383 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001384 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385
1386 p0->error = error_node->errors[error0];
1387
1388 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1389 to_next, n_left_to_next,
1390 bi0, next0);
1391 }
1392
1393 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1394 }
1395
1396 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001397 vlib_error_count (vm, error_node->node_index,
1398 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1399 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001400
1401 return frame->n_vectors;
1402}
1403
1404/* for "syslogging" - use elog for now */
1405#define foreach_log_level \
1406 _ (DEBUG, "DEBUG") \
1407 _ (INFO, "INFORMATION") \
1408 _ (NOTICE, "NOTICE") \
1409 _ (WARNING, "WARNING") \
1410 _ (ERR, "ERROR") \
1411 _ (CRIT, "CRITICAL") \
1412 _ (ALERT, "ALERT") \
1413 _ (EMERG, "EMERGENCY")
1414
Dave Barachd7cb1b52016-12-09 09:52:16 -05001415typedef enum
1416{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001417#define _(f,s) LOG_##f,
1418 foreach_log_level
1419#undef _
1420} log_level_t;
1421
Dave Barachd7cb1b52016-12-09 09:52:16 -05001422static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001423#define _(f,s) s,
1424 foreach_log_level
1425#undef _
1426};
1427
Dave Barachd7cb1b52016-12-09 09:52:16 -05001428static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001429
1430static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001431ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432{
1433 /* just use elog for now */
1434 u8 *what;
1435 va_list va;
1436
Dave Barachd7cb1b52016-12-09 09:52:16 -05001437 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1438 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439
1440 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001441 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442 {
1443 what = va_format (0, fmt, &va);
1444
Dave Barachd7cb1b52016-12-09 09:52:16 -05001445 ELOG_TYPE_DECLARE (e) =
1446 {
1447 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1448 struct
1449 {
1450 u32 s[2];
1451 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001453 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1454 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455 }
1456 va_end (va);
1457 return;
1458}
1459
Juraj Sloboda52574522018-05-03 10:03:50 +02001460clib_error_t *
1461call_ip6_neighbor_callbacks (void *data,
1462 _vnet_ip6_neighbor_function_list_elt_t * elt)
1463{
1464 clib_error_t *error = 0;
1465
1466 while (elt)
1467 {
1468 error = elt->fp (data);
1469 if (error)
1470 return error;
1471 elt = elt->next_ip6_neighbor_function;
1472 }
1473
1474 return error;
1475}
1476
Ed Warnickecb9cada2015-12-08 15:45:58 -07001477/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001478typedef enum
1479{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1481 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1482 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1483 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1484} icmp6_router_solicitation_or_advertisement_next_t;
1485
1486static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001487icmp6_router_solicitation (vlib_main_t * vm,
1488 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001490 vnet_main_t *vnm = vnet_get_main ();
1491 ip6_main_t *im = &ip6_main;
1492 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001493 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001494 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001496 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497 int bogus_length;
1498
1499 icmp6_neighbor_discovery_option_type_t option_type;
1500
Dave Barachd7cb1b52016-12-09 09:52:16 -05001501 vlib_node_runtime_t *error_node =
1502 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503
1504 from = vlib_frame_vector_args (frame);
1505 n_left_from = n_packets;
1506 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001507
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 if (node->flags & VLIB_NODE_FLAG_TRACE)
1509 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1510 /* stride */ 1,
1511 sizeof (icmp6_input_trace_t));
1512
1513 /* source may append his LL address */
1514 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1515
1516 while (n_left_from > 0)
1517 {
1518 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001519
Ed Warnickecb9cada2015-12-08 15:45:58 -07001520 while (n_left_from > 0 && n_left_to_next > 0)
1521 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001522 vlib_buffer_t *p0;
1523 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524 ip6_radv_t *radv_info = 0;
1525
Dave Barachd7cb1b52016-12-09 09:52:16 -05001526 icmp6_neighbor_discovery_header_t *h0;
1527 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1528
Ed Warnickecb9cada2015-12-08 15:45:58 -07001529 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001530 u32 is_solicitation = 1, is_dropped = 0;
1531 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532
1533 bi0 = to_next[0] = from[0];
1534
1535 from += 1;
1536 to_next += 1;
1537 n_left_from -= 1;
1538 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001539
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540 p0 = vlib_get_buffer (vm, bi0);
1541 ip0 = vlib_buffer_get_current (p0);
1542 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001543 options_len0 =
1544 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1545 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1546 is_link_local =
1547 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548
1549 error0 = ICMP6_ERROR_NONE;
1550 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001551
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552 /* check if solicitation (not from nd_timer node) */
1553 if (ip6_address_is_unspecified (&ip0->dst_address))
1554 is_solicitation = 0;
1555
1556 /* Check that source address is unspecified, link-local or else on-link. */
1557 if (!is_unspecified && !is_link_local)
1558 {
1559 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560
Dave Barachd7cb1b52016-12-09 09:52:16 -05001561 if (ADJ_INDEX_INVALID != src_adj_index0)
1562 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001563 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001564
Dave Barachd7cb1b52016-12-09 09:52:16 -05001565 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1566 ?
1567 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1568 : error0);
1569 }
1570 else
1571 {
1572 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1573 }
1574 }
1575
Ed Warnickecb9cada2015-12-08 15:45:58 -07001576 /* check for source LL option and process */
1577 o0 = (void *) (h0 + 1);
1578 o0 = ((options_len0 == 8
1579 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001580 && o0->header.n_data_u64s == 1) ? o0 : 0);
1581
Ed Warnickecb9cada2015-12-08 15:45:58 -07001582 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001583 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1584 !is_unspecified && !is_link_local))
1585 {
Neale Ranns37029302018-08-10 05:30:06 -07001586 vnet_set_ip6_ethernet_neighbor
1587 (vm, sw_if_index0,
1588 &ip0->src_address,
1589 (mac_address_t *) o0->ethernet_address,
1590 IP_NEIGHBOR_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001591 }
1592
Ed Warnickecb9cada2015-12-08 15:45:58 -07001593 /* default is to drop */
1594 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001595
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596 if (error0 == ICMP6_ERROR_NONE)
1597 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001598 vnet_sw_interface_t *sw_if0;
1599 ethernet_interface_t *eth_if0;
1600 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001601
1602 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1603 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001604 eth_if0 =
1605 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606
1607 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001608 error0 =
1609 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1610 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001611
1612 if (error0 == ICMP6_ERROR_NONE)
1613 {
1614 u32 ri;
1615
1616 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001617 p0->current_length -=
1618 (options_len0 +
1619 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001620
1621 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001622 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1623 sw_if_index0)
1624 {
1625 ri =
1626 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627
Neale Ranns8e254952018-04-25 05:40:48 -07001628 if (ri != ~0)
1629 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1630 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001631
1632 error0 =
1633 ((!radv_info) ?
1634 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1635 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001636
1637 if (error0 == ICMP6_ERROR_NONE)
1638 {
1639 f64 now = vlib_time_now (vm);
1640
1641 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001642 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001643 {
Neale Ranns75152282017-01-09 01:00:45 -08001644 if (0 != radv_info->last_radv_time &&
1645 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001646 MIN_DELAY_BETWEEN_RAS)
1647 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001648 else
1649 radv_info->last_radv_time = now;
1650 }
1651
1652 /* send now */
1653 icmp6_router_advertisement_header_t rh;
1654
1655 rh.icmp.type = ICMP6_router_advertisement;
1656 rh.icmp.code = 0;
1657 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001658
Ed Warnickecb9cada2015-12-08 15:45:58 -07001659 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001660 rh.router_lifetime_in_sec =
1661 clib_host_to_net_u16
1662 (radv_info->adv_router_lifetime_in_sec);
1663 rh.
1664 time_in_msec_between_retransmitted_neighbor_solicitations
1665 =
1666 clib_host_to_net_u32 (radv_info->
1667 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1668 rh.neighbor_reachable_time_in_msec =
1669 clib_host_to_net_u32 (radv_info->
1670 adv_neighbor_reachable_time_in_msec);
1671
1672 rh.flags =
1673 (radv_info->adv_managed_flag) ?
1674 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1675 0;
1676 rh.flags |=
1677 ((radv_info->adv_other_flag) ?
1678 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1679 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001680
1681
Dave Barachd7cb1b52016-12-09 09:52:16 -05001682 u16 payload_length =
1683 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001684
Dave Barach3a63fc52019-01-07 09:15:47 -05001685 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001686 (vm, &bi0, (void *) &rh,
Dave Barach3a63fc52019-01-07 09:15:47 -05001687 sizeof (icmp6_router_advertisement_header_t)))
1688 {
1689 /* buffer allocation failed, drop the pkt */
1690 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1691 goto drop0;
1692 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001693
Dave Barachd7cb1b52016-12-09 09:52:16 -05001694 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001695 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001696 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1697 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
Dave Barachd7cb1b52016-12-09 09:52:16 -05001699 h.header.type =
1700 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001701 h.header.n_data_u64s = 1;
1702
1703 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001704 clib_memcpy (&h.ethernet_address[0],
1705 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706
Dave Barach3a63fc52019-01-07 09:15:47 -05001707 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001708 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001709 sizeof
1710 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1711 {
1712 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1713 goto drop0;
1714 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001715
Dave Barachd7cb1b52016-12-09 09:52:16 -05001716 payload_length +=
1717 sizeof
1718 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001719 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001720
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001722 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723 {
1724 icmp6_neighbor_discovery_mtu_option_t h;
1725
1726 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001727 h.mtu =
1728 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001729 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1730 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001731
1732 payload_length +=
1733 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001734
Dave Barach3a63fc52019-01-07 09:15:47 -05001735 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001736 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001737 sizeof
1738 (icmp6_neighbor_discovery_mtu_option_t)))
1739 {
1740 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1741 goto drop0;
1742 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001744
Ed Warnickecb9cada2015-12-08 15:45:58 -07001745 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001746 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001747
Dave Barachd7cb1b52016-12-09 09:52:16 -05001748 /* *INDENT-OFF* */
1749 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1750 ({
1751 if(pr_info->enabled &&
1752 (!pr_info->decrement_lifetime_flag
1753 || (pr_info->pref_lifetime_expires >0)))
1754 {
1755 /* advertise this prefix */
1756 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
Dave Barachd7cb1b52016-12-09 09:52:16 -05001758 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1759 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001760
Dave Barachd7cb1b52016-12-09 09:52:16 -05001761 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001762
Dave Barachd7cb1b52016-12-09 09:52:16 -05001763 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1764 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765
Dave Barachd7cb1b52016-12-09 09:52:16 -05001766 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1767 {
1768 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1769 h.preferred_time = 0;
1770 }
1771 else
1772 {
1773 if(pr_info->decrement_lifetime_flag)
1774 {
1775 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1776 (pr_info->valid_lifetime_expires - now) : 0;
1777
1778 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1779 (pr_info->pref_lifetime_expires - now) : 0;
1780 }
1781
1782 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1783 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1784 }
1785 h.unused = 0;
1786
1787 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1788
1789 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1790
Dave Barach3a63fc52019-01-07 09:15:47 -05001791 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001792 (vm, &bi0, (void *)&h,
1793 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05001794 {
1795 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1796 goto drop0;
1797 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001798
1799 }
1800 }));
1801 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001802
1803 /* add additional options before here */
1804
1805 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001806 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001807 {
1808 ip0->dst_address = ip0->src_address;
1809 }
1810 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001811 {
1812 /* target address is all-nodes mcast addr */
1813 ip6_set_reserved_multicast_address
1814 (&ip0->dst_address,
1815 IP6_MULTICAST_SCOPE_link_local,
1816 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001817 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001818
Ed Warnickecb9cada2015-12-08 15:45:58 -07001819 /* source address MUST be the link-local address */
1820 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001821
Dave Barachd7cb1b52016-12-09 09:52:16 -05001822 ip0->hop_limit = 255;
1823 ip0->payload_length =
1824 clib_host_to_net_u16 (payload_length);
1825
1826 icmp6_router_advertisement_header_t *rh0 =
1827 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1828 rh0->icmp.checksum =
1829 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1830 &bogus_length);
1831 ASSERT (bogus_length == 0);
1832
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001834 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001836
1837 if (is_solicitation)
1838 {
1839 ethernet_header_t *eth0;
1840 /* Reuse current MAC header, copy SMAC to DMAC and
1841 * interface MAC to SMAC */
1842 vlib_buffer_reset (p0);
1843 eth0 = vlib_buffer_get_current (p0);
1844 clib_memcpy (eth0->dst_address, eth0->src_address,
1845 6);
1846 clib_memcpy (eth0->src_address, eth_if0->address,
1847 6);
1848 next0 =
1849 is_dropped ? next0 :
1850 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1851 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1852 sw_if_index0;
1853 }
1854 else
1855 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001856 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001857 if (adj_index0 == 0)
1858 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1859 else
1860 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001861 next0 =
1862 is_dropped ? next0 :
1863 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1864 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1865 adj_index0;
1866 }
1867 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001868 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001869
1870 radv_info->n_solicitations_dropped += is_dropped;
1871 radv_info->n_solicitations_rcvd += is_solicitation;
1872
1873 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001874 {
1875 radv_info->n_advertisements_sent++;
1876 n_advertisements_sent++;
1877 }
1878 }
1879 }
1880 }
1881
Dave Barach3a63fc52019-01-07 09:15:47 -05001882 drop0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001883 p0->error = error_node->errors[error0];
1884
Dave Barachd7cb1b52016-12-09 09:52:16 -05001885 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001887
Ed Warnickecb9cada2015-12-08 15:45:58 -07001888 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1889 to_next, n_left_to_next,
1890 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001891
Ed Warnickecb9cada2015-12-08 15:45:58 -07001892 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001893
Ed Warnickecb9cada2015-12-08 15:45:58 -07001894 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1895 }
1896
1897 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001898 vlib_error_count (vm, error_node->node_index,
1899 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1900 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001901
1902 return frame->n_vectors;
1903}
1904
1905 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1906static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001907icmp6_router_advertisement (vlib_main_t * vm,
1908 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001909{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001910 vnet_main_t *vnm = vnet_get_main ();
1911 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001912 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001913 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001914 u32 n_left_from, n_left_to_next, next_index;
1915 u32 n_advertisements_rcvd = 0;
1916
Dave Barachd7cb1b52016-12-09 09:52:16 -05001917 vlib_node_runtime_t *error_node =
1918 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001919
1920 from = vlib_frame_vector_args (frame);
1921 n_left_from = n_packets;
1922 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001923
Ed Warnickecb9cada2015-12-08 15:45:58 -07001924 if (node->flags & VLIB_NODE_FLAG_TRACE)
1925 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1926 /* stride */ 1,
1927 sizeof (icmp6_input_trace_t));
1928
1929 while (n_left_from > 0)
1930 {
1931 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001932
Ed Warnickecb9cada2015-12-08 15:45:58 -07001933 while (n_left_from > 0 && n_left_to_next > 0)
1934 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001935 vlib_buffer_t *p0;
1936 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001937 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001938 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001939 u32 bi0, options_len0, sw_if_index0, next0, error0;
1940
1941 bi0 = to_next[0] = from[0];
1942
1943 from += 1;
1944 to_next += 1;
1945 n_left_from -= 1;
1946 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001947
Ed Warnickecb9cada2015-12-08 15:45:58 -07001948 p0 = vlib_get_buffer (vm, bi0);
1949 ip0 = vlib_buffer_get_current (p0);
1950 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001951 options_len0 =
1952 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001953
1954 error0 = ICMP6_ERROR_NONE;
1955 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1956
Dave Barachd7cb1b52016-12-09 09:52:16 -05001957 /* Check that source address is link-local */
1958 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001959 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1960
1961 /* default is to drop */
1962 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001963
Ed Warnickecb9cada2015-12-08 15:45:58 -07001964 n_advertisements_rcvd++;
1965
1966 if (error0 == ICMP6_ERROR_NONE)
1967 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001968 vnet_sw_interface_t *sw_if0;
1969 ethernet_interface_t *eth_if0;
1970
Ed Warnickecb9cada2015-12-08 15:45:58 -07001971 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1972 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001973 eth_if0 =
1974 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001975
1976 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001977 error0 =
1978 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1979 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001980
1981 if (error0 == ICMP6_ERROR_NONE)
1982 {
1983 u32 ri;
1984
1985 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001986 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1987 sw_if_index0)
1988 {
1989 ri =
1990 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001991
Neale Ranns8e254952018-04-25 05:40:48 -07001992 if (ri != ~0)
1993 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1994 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001995
1996 error0 =
1997 ((!radv_info) ?
1998 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1999 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002000
2001 if (error0 == ICMP6_ERROR_NONE)
2002 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002003 radv_info->keep_sending_rs = 0;
2004
2005 ra_report_t r;
2006
2007 r.sw_if_index = sw_if_index0;
Neale Ranns37029302018-08-10 05:30:06 -07002008 memcpy (&r.router_address, &ip0->src_address, 16);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002009 r.current_hop_limit = h0->current_hop_limit;
2010 r.flags = h0->flags;
2011 r.router_lifetime_in_sec =
2012 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2013 r.neighbor_reachable_time_in_msec =
2014 clib_net_to_host_u32
2015 (h0->neighbor_reachable_time_in_msec);
2016 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2017 r.prefixes = 0;
2018
Ed Warnickecb9cada2015-12-08 15:45:58 -07002019 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002020 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2021 && (h0->current_hop_limit !=
2022 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002023 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002024 ip6_neighbor_syslog (vm, LOG_WARNING,
2025 "our AdvCurHopLimit on %U doesn't agree with %U",
2026 format_vnet_sw_if_index_name,
2027 vnm, sw_if_index0,
2028 format_ip6_address,
2029 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002030 }
2031
Dave Barachd7cb1b52016-12-09 09:52:16 -05002032 if ((h0->flags &
2033 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2034 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002035 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002036 ip6_neighbor_syslog (vm, LOG_WARNING,
2037 "our AdvManagedFlag on %U doesn't agree with %U",
2038 format_vnet_sw_if_index_name,
2039 vnm, sw_if_index0,
2040 format_ip6_address,
2041 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002042 }
2043
Dave Barachd7cb1b52016-12-09 09:52:16 -05002044 if ((h0->flags &
2045 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2046 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002047 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002048 ip6_neighbor_syslog (vm, LOG_WARNING,
2049 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2050 format_vnet_sw_if_index_name,
2051 vnm, sw_if_index0,
2052 format_ip6_address,
2053 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002054 }
2055
Dave Barachd7cb1b52016-12-09 09:52:16 -05002056 if ((h0->
2057 time_in_msec_between_retransmitted_neighbor_solicitations
2058 && radv_info->
2059 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2060 && (h0->
2061 time_in_msec_between_retransmitted_neighbor_solicitations
2062 !=
2063 clib_host_to_net_u32 (radv_info->
2064 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002065 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002066 ip6_neighbor_syslog (vm, LOG_WARNING,
2067 "our AdvRetransTimer on %U doesn't agree with %U",
2068 format_vnet_sw_if_index_name,
2069 vnm, sw_if_index0,
2070 format_ip6_address,
2071 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002072 }
2073
Dave Barachd7cb1b52016-12-09 09:52:16 -05002074 if ((h0->neighbor_reachable_time_in_msec &&
2075 radv_info->adv_neighbor_reachable_time_in_msec) &&
2076 (h0->neighbor_reachable_time_in_msec !=
2077 clib_host_to_net_u32
2078 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002079 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002080 ip6_neighbor_syslog (vm, LOG_WARNING,
2081 "our AdvReachableTime on %U doesn't agree with %U",
2082 format_vnet_sw_if_index_name,
2083 vnm, sw_if_index0,
2084 format_ip6_address,
2085 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002086 }
2087
2088 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002089 u8 *opt_hdr = (u8 *) (h0 + 1);
2090 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002091 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002092 icmp6_neighbor_discovery_option_header_t *o0 =
2093 (icmp6_neighbor_discovery_option_header_t *)
2094 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002095 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002096 icmp6_neighbor_discovery_option_type_t option_type =
2097 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002098
Dave Barachd7cb1b52016-12-09 09:52:16 -05002099 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002100 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002101 ip6_neighbor_syslog (vm, LOG_ERR,
2102 "malformed RA packet on %U from %U",
2103 format_vnet_sw_if_index_name,
2104 vnm, sw_if_index0,
2105 format_ip6_address,
2106 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002107 break;
2108 }
2109
Dave Barachd7cb1b52016-12-09 09:52:16 -05002110 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002111 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002112 ip6_neighbor_syslog (vm, LOG_ERR,
2113 " zero length option in RA 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 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002120 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002121 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002122 ip6_neighbor_syslog (vm, LOG_ERR,
2123 "option length in RA packet greater than total length on %U from %U",
2124 format_vnet_sw_if_index_name,
2125 vnm, sw_if_index0,
2126 format_ip6_address,
2127 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002128 break;
2129 }
2130
2131 options_len0 -= opt_len;
2132 opt_hdr += opt_len;
2133
Dave Barachd7cb1b52016-12-09 09:52:16 -05002134 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002135 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002136 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2137 {
2138 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2139 * h =
2140 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2141 *) (o0);
2142
2143 if (opt_len < sizeof (*h))
2144 break;
2145
2146 memcpy (r.slla, h->ethernet_address, 6);
2147 }
2148 break;
2149
Ed Warnickecb9cada2015-12-08 15:45:58 -07002150 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002151 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002152 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002153 (icmp6_neighbor_discovery_mtu_option_t
2154 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002155
Dave Barachd7cb1b52016-12-09 09:52:16 -05002156 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002157 break;
2158
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002159 r.mtu = clib_net_to_host_u32 (h->mtu);
2160
Dave Barachd7cb1b52016-12-09 09:52:16 -05002161 if ((h->mtu && radv_info->adv_link_mtu) &&
2162 (h->mtu !=
2163 clib_host_to_net_u32
2164 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002165 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002166 ip6_neighbor_syslog (vm, LOG_WARNING,
2167 "our AdvLinkMTU on %U doesn't agree with %U",
2168 format_vnet_sw_if_index_name,
2169 vnm, sw_if_index0,
2170 format_ip6_address,
2171 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002172 }
2173 }
2174 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002175
Ed Warnickecb9cada2015-12-08 15:45:58 -07002176 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2177 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002178 icmp6_neighbor_discovery_prefix_information_option_t
2179 * h =
2180 (icmp6_neighbor_discovery_prefix_information_option_t
2181 *) (o0);
2182
Ed Warnickecb9cada2015-12-08 15:45:58 -07002183 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002184 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002185 u32 preferred, valid;
2186
Dave Barachd7cb1b52016-12-09 09:52:16 -05002187 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002188 break;
2189
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002190 vec_validate (r.prefixes,
2191 vec_len (r.prefixes));
2192 ra_report_prefix_info_t *prefix =
2193 vec_elt_at_index (r.prefixes,
2194 vec_len (r.prefixes) - 1);
2195
Dave Barachd7cb1b52016-12-09 09:52:16 -05002196 preferred =
2197 clib_net_to_host_u32 (h->preferred_time);
2198 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002199
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002200 prefix->preferred_time = preferred;
2201 prefix->valid_time = valid;
2202 prefix->flags = h->flags & 0xc0;
Neale Ranns37029302018-08-10 05:30:06 -07002203 prefix->prefix.fp_len = h->dst_address_length;
2204 prefix->prefix.fp_addr.ip6 = h->dst_address;
2205 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002206
Ed Warnickecb9cada2015-12-08 15:45:58 -07002207 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002208 /* *INDENT-OFF* */
2209 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2210 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002211
Dave Barachd7cb1b52016-12-09 09:52:16 -05002212 ip6_address_t mask;
2213 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2214
2215 if(pr_info->enabled &&
2216 (pr_info->prefix_len == h->dst_address_length) &&
2217 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2218 {
2219 /* found it */
2220 if(!pr_info->decrement_lifetime_flag &&
2221 valid != pr_info->adv_valid_lifetime_in_secs)
2222 {
2223 ip6_neighbor_syslog(vm, LOG_WARNING,
2224 "our ADV validlifetime on %U for %U does not agree with %U",
2225 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2226 format_ip6_address, &h->dst_address);
2227 }
2228 if(!pr_info->decrement_lifetime_flag &&
2229 preferred != pr_info->adv_pref_lifetime_in_secs)
2230 {
2231 ip6_neighbor_syslog(vm, LOG_WARNING,
2232 "our ADV preferredlifetime on %U for %U does not agree with %U",
2233 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2234 format_ip6_address, &h->dst_address);
2235 }
2236 }
2237 break;
2238 }));
2239 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002240 break;
2241 }
2242 default:
2243 /* skip this one */
2244 break;
2245 }
2246 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002247 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002248 }
2249 }
2250 }
2251
2252 p0->error = error_node->errors[error0];
2253
Dave Barachd7cb1b52016-12-09 09:52:16 -05002254 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002255 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002256
Ed Warnickecb9cada2015-12-08 15:45:58 -07002257 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2258 to_next, n_left_to_next,
2259 bi0, next0);
2260 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002261
Ed Warnickecb9cada2015-12-08 15:45:58 -07002262 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2263 }
2264
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002265 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002266 vlib_error_count (vm, error_node->node_index,
2267 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2268 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002269
2270 return frame->n_vectors;
2271}
2272
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002273static inline f64
2274random_f64_from_to (f64 from, f64 to)
2275{
2276 static u32 seed = 0;
2277 static u8 seed_set = 0;
2278 if (!seed_set)
2279 {
2280 seed = random_default_seed ();
2281 seed_set = 1;
2282 }
2283 return random_f64 (&seed) * (to - from) + from;
2284}
2285
2286static inline u8
2287get_mac_address (u32 sw_if_index, u8 * address)
2288{
2289 vnet_main_t *vnm = vnet_get_main ();
2290 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2291 if (!hw_if->hw_address)
2292 return 1;
2293 clib_memcpy (address, hw_if->hw_address, 6);
2294 return 0;
2295}
2296
2297static inline vlib_buffer_t *
2298create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2299{
2300 u32 bi0;
2301 vlib_buffer_t *p0;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002302 icmp6_router_solicitation_header_t *rh;
2303 u16 payload_length;
2304 int bogus_length;
2305 u32 sw_if_index;
2306
2307 sw_if_index = radv_info->sw_if_index;
2308
2309 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2310 {
2311 clib_warning ("buffer allocation failure");
2312 return 0;
2313 }
2314
2315 p0 = vlib_get_buffer (vm, bi0);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002316 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2317 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2318
2319 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2320 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2321
2322 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2323
2324 rh = vlib_buffer_get_current (p0);
2325 p0->current_length = sizeof (*rh);
2326
2327 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2328 rh->neighbor.icmp.code = 0;
2329 rh->neighbor.icmp.checksum = 0;
2330 rh->neighbor.reserved_must_be_zero = 0;
2331
2332 rh->link_layer_option.header.type =
2333 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2334 if (0 != get_mac_address (sw_if_index,
2335 rh->link_layer_option.ethernet_address))
2336 {
2337 clib_warning ("interface with sw_if_index %u has no mac address",
2338 sw_if_index);
2339 vlib_buffer_free (vm, &bi0, 1);
2340 return 0;
2341 }
2342 rh->link_layer_option.header.n_data_u64s = 1;
2343
2344 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2345
2346 rh->ip.ip_version_traffic_class_and_flow_label =
2347 clib_host_to_net_u32 (0x6 << 28);
2348 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2349 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2350 rh->ip.hop_limit = 255;
2351 rh->ip.src_address = radv_info->link_local_address;
2352 /* set address ff02::2 */
David Johnsond9818dd2018-12-14 14:53:41 -05002353 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002354 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2355
2356 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2357 &rh->ip,
2358 &bogus_length);
2359
2360 return p0;
2361}
2362
2363static inline void
2364stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2365{
2366 u32 bi0;
2367
2368 ra->keep_sending_rs = 0;
2369 if (ra->buffer)
2370 {
2371 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2372 vlib_buffer_free (vm, &bi0, 1);
2373 ra->buffer = 0;
2374 }
2375}
2376
2377static inline bool
2378check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2379 f64 * due_time)
2380{
2381 vlib_buffer_t *p0;
2382 vlib_frame_t *f;
2383 u32 *to_next;
2384 u32 next_index;
2385 vlib_buffer_t *c0;
2386 u32 ci0;
2387
2388 icmp6_send_router_solicitation_params_t *params;
2389
2390 if (!radv_info->keep_sending_rs)
2391 return false;
2392
2393 params = &radv_info->params;
2394
2395 if (radv_info->due_time > current_time)
2396 {
2397 *due_time = radv_info->due_time;
2398 return true;
2399 }
2400
2401 p0 = radv_info->buffer;
2402
2403 next_index = ip6_rewrite_mcast_node.index;
2404
2405 c0 = vlib_buffer_copy (vm, p0);
2406 ci0 = vlib_get_buffer_index (vm, c0);
2407
2408 f = vlib_get_frame_to_node (vm, next_index);
2409 to_next = vlib_frame_vector_args (f);
2410 to_next[0] = ci0;
2411 f->n_vectors = 1;
2412 vlib_put_frame_to_node (vm, next_index, f);
2413
2414 if (params->mrc != 0 && --radv_info->n_left == 0)
2415 stop_sending_rs (vm, radv_info);
2416 else
2417 {
2418 radv_info->sleep_interval =
2419 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2420 if (radv_info->sleep_interval > params->mrt)
2421 radv_info->sleep_interval =
2422 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2423
2424 radv_info->due_time = current_time + radv_info->sleep_interval;
2425
2426 if (params->mrd != 0
2427 && current_time > radv_info->start_time + params->mrd)
2428 stop_sending_rs (vm, radv_info);
2429 else
2430 *due_time = radv_info->due_time;
2431 }
2432
2433 return radv_info->keep_sending_rs;
2434}
2435
2436static uword
2437send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2438 vlib_frame_t * f0)
2439{
2440 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2441 ip6_radv_t *radv_info;
2442 uword *event_data = 0;
2443 f64 sleep_time = 1e9;
2444 f64 current_time;
2445 f64 due_time;
2446 f64 dt = 0;
2447
2448 while (true)
2449 {
2450 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2451 vlib_process_get_events (vm, &event_data);
2452 vec_reset_length (event_data);
2453
2454 current_time = vlib_time_now (vm);
2455 do
2456 {
2457 due_time = current_time + 1e9;
2458 /* *INDENT-OFF* */
2459 pool_foreach (radv_info, nm->if_radv_pool,
2460 ({
2461 if (check_send_rs (vm, radv_info, current_time, &dt)
2462 && (dt < due_time))
2463 due_time = dt;
2464 }));
2465 /* *INDENT-ON* */
2466 current_time = vlib_time_now (vm);
2467 }
2468 while (due_time < current_time);
2469
2470 sleep_time = due_time - current_time;
2471 }
2472
2473 return 0;
2474}
2475
2476/* *INDENT-OFF* */
2477VLIB_REGISTER_NODE (send_rs_process_node) = {
2478 .function = send_rs_process,
2479 .type = VLIB_NODE_TYPE_PROCESS,
2480 .name = "send-rs-process",
2481};
2482/* *INDENT-ON* */
2483
2484void
2485icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2486 icmp6_send_router_solicitation_params_t *
2487 params)
2488{
2489 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2490 u32 rai;
2491 ip6_radv_t *ra = 0;
2492
2493 ASSERT (~0 != sw_if_index);
2494
2495 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2496 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2497
Juraj Sloboda52574522018-05-03 10:03:50 +02002498 stop_sending_rs (vm, ra);
2499
2500 if (!stop)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002501 {
2502 ra->keep_sending_rs = 1;
2503 ra->params = *params;
2504 ra->n_left = params->mrc;
2505 ra->start_time = vlib_time_now (vm);
2506 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2507 ra->due_time = 0; /* send first packet ASAP */
2508 ra->buffer = create_buffer_for_rs (vm, ra);
2509 if (!ra->buffer)
2510 ra->keep_sending_rs = 0;
2511 else
2512 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2513 }
2514}
2515
Neale Ranns87df12d2017-02-18 08:16:41 -08002516/**
2517 * @brief Add a multicast Address to the advertised MLD set
2518 */
2519static void
2520ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2521{
2522 ip6_mldp_group_t *mcast_group_info;
2523 uword *p;
2524
2525 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002526 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002527 mcast_group_info =
2528 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2529
2530 /* add address */
2531 if (!mcast_group_info)
2532 {
2533 /* add */
2534 u32 mi;
2535 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2536
2537 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002538 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002539 0);
2540
2541 mcast_group_info->type = 4;
2542 mcast_group_info->mcast_source_address_pool = 0;
2543 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002544 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002545 sizeof (ip6_address_t));
2546 }
2547}
2548
2549/**
2550 * @brief Delete a multicast Address from the advertised MLD set
2551 */
2552static void
2553ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2554{
2555 ip6_mldp_group_t *mcast_group_info;
2556 uword *p;
2557
2558 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2559 mcast_group_info =
2560 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2561
2562 if (mcast_group_info)
2563 {
2564 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2565 /* old_value */ 0);
2566 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2567 }
2568}
2569
2570/**
2571 * @brief Add a multicast Address to the advertised MLD set
2572 */
2573static void
2574ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2575 ip6_multicast_address_scope_t scope,
2576 ip6_multicast_link_local_group_id_t group)
2577{
2578 ip6_address_t addr;
2579
2580 ip6_set_reserved_multicast_address (&addr, scope, group);
2581
2582 ip6_neighbor_add_mld_prefix (a, &addr);
2583}
2584
2585/**
2586 * @brief create and initialize router advertisement parameters with default
2587 * values for this intfc
2588 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002589u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002590ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002591 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002592{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002593 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2594 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002595 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002596 vnet_sw_interface_t *sw_if0;
2597 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002598
2599 /* lookup radv container - ethernet interfaces only */
2600 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002601 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002602 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2603
Dave Barachd7cb1b52016-12-09 09:52:16 -05002604 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002605 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002606
2607 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2608 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002609 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2610
Dave Barachd7cb1b52016-12-09 09:52:16 -05002611 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002612 {
2613 a = pool_elt_at_index (nm->if_radv_pool, ri);
2614
Dave Barachd7cb1b52016-12-09 09:52:16 -05002615 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002616 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002617 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002618 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002619
Neale Ranns32e1c012016-11-22 17:07:28 +00002620 /* release the lock on the interface's mcast adj */
2621 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002622
Neale Ranns87df12d2017-02-18 08:16:41 -08002623 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002624 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002625 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002626 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002627 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002628 }));
2629 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002630 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002631 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002632 }));
2633 /* *INDENT-ON* */
2634
Neale Ranns87df12d2017-02-18 08:16:41 -08002635 pool_free (a->mldp_group_pool);
2636 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002637
Neale Ranns87df12d2017-02-18 08:16:41 -08002638 mhash_free (&a->address_to_prefix_index);
2639 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002640
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002641 if (a->keep_sending_rs)
2642 a->keep_sending_rs = 0;
2643
Dave Barachd7cb1b52016-12-09 09:52:16 -05002644 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002645 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2646 ri = ~0;
2647 }
2648 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002649 else
2650 {
2651 if (is_add)
2652 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002653 pool_get (nm->if_radv_pool, a);
2654
2655 ri = a - nm->if_radv_pool;
2656 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2657
2658 /* initialize default values (most of which are zero) */
Dave Barachb7b92992018-10-17 10:38:51 -04002659 clib_memset (a, 0, sizeof (a[0]));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002660
2661 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002662 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2663 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2664 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2665 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2666
Neale Ranns87df12d2017-02-18 08:16:41 -08002667 /* send ll address source address option */
2668 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002669
2670 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2671 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2672 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2673 a->seed = (u32) clib_cpu_time_now ();
2674 (void) random_u32 (&a->seed);
2675 a->randomizer = clib_cpu_time_now ();
2676 (void) random_u64 (&a->randomizer);
2677
2678 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2679 a->initial_adverts_sent = a->initial_adverts_count - 1;
2680 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2681
2682 /* deafult is to send */
2683 a->send_radv = 1;
2684
2685 /* fill in radv_info for this interface that will be needed later */
Ole Troand7231612018-06-07 10:17:57 +02002686 a->adv_link_mtu =
2687 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002688
2689 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2690
2691 /* fill in default link-local address (this may be overridden) */
Jon Loeligerdfa47dd2019-06-13 11:02:26 -05002692 ip6_link_local_address_from_ethernet_mac_address
Dave Barachd7cb1b52016-12-09 09:52:16 -05002693 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002694
2695 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2696 sizeof (ip6_address_t));
2697 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2698 sizeof (ip6_address_t));
2699
Neale Ranns32e1c012016-11-22 17:07:28 +00002700 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2701 VNET_LINK_IP6,
2702 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002703
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002704 a->keep_sending_rs = 0;
2705
Dave Barachd7cb1b52016-12-09 09:52:16 -05002706 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002707 ip6_neighbor_add_mld_grp (a,
2708 IP6_MULTICAST_SCOPE_link_local,
2709 IP6_MULTICAST_GROUP_ID_all_hosts);
2710 ip6_neighbor_add_mld_grp (a,
2711 IP6_MULTICAST_SCOPE_link_local,
2712 IP6_MULTICAST_GROUP_ID_all_routers);
2713 ip6_neighbor_add_mld_grp (a,
2714 IP6_MULTICAST_SCOPE_link_local,
2715 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002716 }
2717 }
2718 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002719}
2720
2721/* send an mldpv2 report */
2722static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002723ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002724{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002725 vnet_main_t *vnm = vnet_get_main ();
2726 vlib_main_t *vm = vnm->vlib_main;
2727 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2728 vnet_sw_interface_t *sw_if0;
2729 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002730 u32 ri;
2731 int bogus_length;
2732
Dave Barachd7cb1b52016-12-09 09:52:16 -05002733 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002734 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002735 vlib_buffer_t *b0;
2736 ip6_header_t *ip0;
2737 u32 *to_next;
2738 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002739 u32 bo0;
2740 u32 n_to_alloc = 1;
2741 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002742
Ed Warnickecb9cada2015-12-08 15:45:58 -07002743 icmp6_multicast_listener_report_header_t *rh0;
2744 icmp6_multicast_listener_report_packet_t *rp0;
2745
2746 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2747 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2748 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2749
2750 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2751 return;
2752
2753 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002754 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2755 ~0);
2756
Ed Warnickecb9cada2015-12-08 15:45:58 -07002757 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002758
2759 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002760 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002761
Ed Warnickecb9cada2015-12-08 15:45:58 -07002762 /* send report now - build a mldpv2 report packet */
Damjan Marion671e60e2018-12-30 18:09:59 +01002763 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002764 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002765 {
Dave Barach3a63fc52019-01-07 09:15:47 -05002766 alloc_fail:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002767 clib_warning ("buffer allocation failure");
2768 return;
2769 }
2770
2771 b0 = vlib_get_buffer (vm, bo0);
2772
2773 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002774 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002775
Dave Barachd7cb1b52016-12-09 09:52:16 -05002776 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002777
2778 b0->error = ICMP6_ERROR_NONE;
2779
2780 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002781 ip0 = (ip6_header_t *) & rp0->ip;
2782 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002783
Dave Barachb7b92992018-10-17 10:38:51 -04002784 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002785
2786 ip0->ip_version_traffic_class_and_flow_label =
2787 clib_host_to_net_u32 (0x6 << 28);
2788
2789 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002790 /* for DEBUG - vnet driver won't seem to emit router alerts */
2791 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2792 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002793
Ed Warnickecb9cada2015-12-08 15:45:58 -07002794 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002795
Ed Warnickecb9cada2015-12-08 15:45:58 -07002796 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002797 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2798 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002799
2800 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002801 ip6_set_reserved_multicast_address (&ip0->dst_address,
2802 IP6_MULTICAST_SCOPE_link_local,
2803 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2804
Ed Warnickecb9cada2015-12-08 15:45:58 -07002805 /* add reports here */
2806 ip6_mldp_group_t *m;
2807 int num_addr_records = 0;
2808 icmp6_multicast_address_record_t rr;
2809
2810 /* fill in the hop-by-hop extension header (router alert) info */
2811 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2812 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002813
Ed Warnickecb9cada2015-12-08 15:45:58 -07002814 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2815 rh0->alert.len = 2;
2816 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002817
Ed Warnickecb9cada2015-12-08 15:45:58 -07002818 rh0->pad.type = 1;
2819 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002820
Ed Warnickecb9cada2015-12-08 15:45:58 -07002821 rh0->icmp.checksum = 0;
2822
Dave Barachd7cb1b52016-12-09 09:52:16 -05002823 /* *INDENT-OFF* */
2824 pool_foreach (m, radv_info->mldp_group_pool,
2825 ({
2826 rr.type = m->type;
2827 rr.aux_data_len_u32s = 0;
2828 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2829 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002830
Dave Barachd7cb1b52016-12-09 09:52:16 -05002831 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002832
Damjan Marionab9b7ec2019-01-18 20:24:44 +01002833 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2834 sizeof(icmp6_multicast_address_record_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05002835 {
2836 vlib_buffer_free (vm, &bo0, 1);
2837 goto alloc_fail;
2838 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002839
Dave Barachd7cb1b52016-12-09 09:52:16 -05002840 payload_length += sizeof( icmp6_multicast_address_record_t);
2841 }));
2842 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002843
2844 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002845 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2846
Ed Warnickecb9cada2015-12-08 15:45:58 -07002847 /* update lengths */
2848 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2849
Dave Barachd7cb1b52016-12-09 09:52:16 -05002850 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2851 &bogus_length);
2852 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853
Dave Barachd7cb1b52016-12-09 09:52:16 -05002854 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002855 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002856 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002857 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002858 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002859 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002860
Neale Ranns32e1c012016-11-22 17:07:28 +00002861 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002862 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002863
Neale Ranns32e1c012016-11-22 17:07:28 +00002864 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002865
Ed Warnickecb9cada2015-12-08 15:45:58 -07002866 f = vlib_get_frame_to_node (vm, node->index);
2867 to_next = vlib_frame_vector_args (f);
2868 to_next[0] = bo0;
2869 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002870
Ed Warnickecb9cada2015-12-08 15:45:58 -07002871 vlib_put_frame_to_node (vm, node->index, f);
2872 return;
2873}
2874
Dave Barachd7cb1b52016-12-09 09:52:16 -05002875/* *INDENT-OFF* */
2876VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2877{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002878 .function = icmp6_router_solicitation,
2879 .name = "icmp6-router-solicitation",
2880
2881 .vector_size = sizeof (u32),
2882
2883 .format_trace = format_icmp6_input_trace,
2884
2885 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2886 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002887 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002888 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002889 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2890 },
2891};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002892/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002893
2894/* send a RA or update the timer info etc.. */
2895static uword
2896ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002897 vlib_node_runtime_t * node,
2898 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002899{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002900 vnet_main_t *vnm = vnet_get_main ();
2901 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2902 ip6_radv_t *radv_info;
2903 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002904 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002905 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002906 u32 *to_next = 0;
2907 u32 bo0;
2908 icmp6_router_solicitation_header_t *h0;
2909 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002910 f64 now = vlib_time_now (vm);
2911
2912 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002913 /* *INDENT-OFF* */
2914 pool_foreach (radv_info, nm->if_radv_pool,
2915 ({
2916 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2917 {
2918 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2919 radv_info->next_multicast_time = now;
2920 radv_info->last_multicast_time = now;
2921 radv_info->last_radv_time = 0;
2922 radv_info->all_routers_mcast = 0;
2923 continue;
2924 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925
Dave Barachd7cb1b52016-12-09 09:52:16 -05002926 /* Make sure that we've joined the all-routers multicast group */
2927 if(!radv_info->all_routers_mcast)
2928 {
2929 /* send MDLP_REPORT_EVENT message */
2930 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2931 radv_info->all_routers_mcast = 1;
2932 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002933
Dave Barachd7cb1b52016-12-09 09:52:16 -05002934 /* is it time to send a multicast RA on this interface? */
2935 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2936 {
2937 u32 n_to_alloc = 1;
2938 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002939
Dave Barachd7cb1b52016-12-09 09:52:16 -05002940 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2941 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002942
Dave Barachd7cb1b52016-12-09 09:52:16 -05002943 /* multicast send - compute next multicast send time */
2944 if( radv_info->initial_adverts_sent > 0)
2945 {
2946 radv_info->initial_adverts_sent--;
2947 if(rfn > radv_info-> initial_adverts_interval)
2948 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002949
Dave Barachd7cb1b52016-12-09 09:52:16 -05002950 /* check to see if we are ceasing to send */
2951 if( radv_info->initial_adverts_sent == 0)
2952 if(radv_info->cease_radv)
2953 radv_info->send_radv = 0;
2954 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002955
Dave Barachd7cb1b52016-12-09 09:52:16 -05002956 radv_info->next_multicast_time = rfn + now;
2957 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002958
Dave Barachd7cb1b52016-12-09 09:52:16 -05002959 /* send advert now - build a "solicted" router advert with unspecified source address */
Damjan Marion671e60e2018-12-30 18:09:59 +01002960 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002961
Dave Barachd7cb1b52016-12-09 09:52:16 -05002962 if (PREDICT_FALSE(n_allocated == 0))
2963 {
2964 clib_warning ("buffer allocation failure");
2965 continue;
2966 }
2967 b0 = vlib_get_buffer (vm, bo0);
2968 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2969 b0->error = ICMP6_ERROR_NONE;
2970 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2971
2972 h0 = vlib_buffer_get_current (b0);
2973
Dave Barachb7b92992018-10-17 10:38:51 -04002974 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002975
2976 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2977 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2978 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2979 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2980 h0->ip.hop_limit = 255;
2981
2982 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2983 h0->ip.src_address.as_u64[0] = 0;
2984 h0->ip.src_address.as_u64[1] = 0;
2985
2986 h0->ip.dst_address.as_u64[0] = 0;
2987 h0->ip.dst_address.as_u64[1] = 0;
2988
2989 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2990
2991 if (PREDICT_FALSE(f == 0))
2992 {
2993 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2994 to_next = vlib_frame_vector_args (f);
2995 n_left_to_next = VLIB_FRAME_SIZE;
2996 n_this_frame = 0;
2997 }
2998
2999 n_this_frame++;
3000 n_left_to_next--;
3001 to_next[0] = bo0;
3002 to_next += 1;
3003
3004 if (PREDICT_FALSE(n_left_to_next == 0))
3005 {
3006 f->n_vectors = n_this_frame;
3007 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3008 f = 0;
3009 }
3010 }
3011 }));
3012 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003013
3014 if (f)
3015 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003016 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003017 f->n_vectors = n_this_frame;
3018 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3019 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003020 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003021}
3022
3023static uword
3024ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3025 vlib_node_runtime_t * node,
3026 vlib_frame_t * frame)
3027{
3028 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003029 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003030
3031 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003032
Ed Warnickecb9cada2015-12-08 15:45:58 -07003033 while (1)
3034 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003035 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003036
Dave Barachd7cb1b52016-12-09 09:52:16 -05003037 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003038
Dave Barachd7cb1b52016-12-09 09:52:16 -05003039 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003040 {
3041 /* No events found: timer expired. */
3042 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003043 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003044 }
3045 else
3046 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003047 switch (event_type)
3048 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003049
Dave Barachd7cb1b52016-12-09 09:52:16 -05003050 case ICMP6_ND_EVENT_INIT:
3051 break;
3052
3053 case ~0:
3054 break;
3055
3056 default:
3057 ASSERT (0);
3058 }
3059
Ed Warnickecb9cada2015-12-08 15:45:58 -07003060 if (event_data)
3061 _vec_len (event_data) = 0;
3062 }
3063 }
3064 return frame->n_vectors;
3065}
3066
Dave Barachd7cb1b52016-12-09 09:52:16 -05003067/* *INDENT-OFF* */
3068VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3069{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003070 .function = icmp6_router_advertisement,
3071 .name = "icmp6-router-advertisement",
3072
3073 .vector_size = sizeof (u32),
3074
3075 .format_trace = format_icmp6_input_trace,
3076
3077 .n_next_nodes = 1,
3078 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003079 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003080 },
3081};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003082/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003083
3084vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3085
3086 .function = ip6_icmp_neighbor_discovery_event_process,
3087 .name = "ip6-icmp-neighbor-discovery-event-process",
3088 .type = VLIB_NODE_TYPE_PROCESS,
3089};
3090
3091static uword
3092icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003093 vlib_node_runtime_t * node, vlib_frame_t * frame)
3094{
3095 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3096 /* is_solicitation */
3097 1);
3098}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003099
3100static uword
3101icmp6_neighbor_advertisement (vlib_main_t * vm,
3102 vlib_node_runtime_t * node,
3103 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003104{
3105 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3106 /* is_solicitation */
3107 0);
3108}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003109
Dave Barachd7cb1b52016-12-09 09:52:16 -05003110/* *INDENT-OFF* */
3111VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3112{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003113 .function = icmp6_neighbor_solicitation,
3114 .name = "icmp6-neighbor-solicitation",
3115
3116 .vector_size = sizeof (u32),
3117
3118 .format_trace = format_icmp6_input_trace,
3119
3120 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3121 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003122 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003123 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3124 },
3125};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003126/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003127
Dave Barachd7cb1b52016-12-09 09:52:16 -05003128/* *INDENT-OFF* */
3129VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3130{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003131 .function = icmp6_neighbor_advertisement,
3132 .name = "icmp6-neighbor-advertisement",
3133
3134 .vector_size = sizeof (u32),
3135
3136 .format_trace = format_icmp6_input_trace,
3137
3138 .n_next_nodes = 1,
3139 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003140 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003141 },
3142};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003143/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003144
Neale Rannsc7b8f202018-04-25 06:34:31 -07003145typedef enum
3146{
3147 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3148 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3149 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3150} ip6_discover_neighbor_next_t;
3151
3152typedef enum
3153{
3154 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3155 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3156 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3157} ip6_discover_neighbor_error_t;
3158
3159static uword
3160ip6_discover_neighbor_inline (vlib_main_t * vm,
3161 vlib_node_runtime_t * node,
3162 vlib_frame_t * frame, int is_glean)
3163{
3164 vnet_main_t *vnm = vnet_get_main ();
3165 ip6_main_t *im = &ip6_main;
3166 ip_lookup_main_t *lm = &im->lookup_main;
3167 u32 *from, *to_next_drop;
3168 uword n_left_from, n_left_to_next_drop;
Dave Barach49433ad2018-08-08 17:59:03 -04003169 u64 seed;
3170 u32 thread_index = vm->thread_index;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003171 int bogus_length;
3172 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3173
3174 if (node->flags & VLIB_NODE_FLAG_TRACE)
3175 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3176
Neale Rannscd35e532018-08-31 02:51:45 -07003177 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003178
3179 from = vlib_frame_vector_args (frame);
3180 n_left_from = frame->n_vectors;
3181
3182 while (n_left_from > 0)
3183 {
3184 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3185 to_next_drop, n_left_to_next_drop);
3186
3187 while (n_left_from > 0 && n_left_to_next_drop > 0)
3188 {
Neale Rannscd35e532018-08-31 02:51:45 -07003189 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003190 vnet_hw_interface_t *hw_if0;
3191 ip6_radv_t *radv_info;
Neale Rannscd35e532018-08-31 02:51:45 -07003192 ip_adjacency_t *adj0;
3193 vlib_buffer_t *p0;
3194 ip6_header_t *ip0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003195
3196 pi0 = from[0];
3197
3198 p0 = vlib_get_buffer (vm, pi0);
3199
3200 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3201
3202 ip0 = vlib_buffer_get_current (p0);
3203
3204 adj0 = adj_get (adj_index0);
3205
3206 if (!is_glean)
3207 {
3208 ip0->dst_address.as_u64[0] =
3209 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3210 ip0->dst_address.as_u64[1] =
3211 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3212 }
3213
Neale Rannsc7b8f202018-04-25 06:34:31 -07003214 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3215 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3216
Neale Rannscd35e532018-08-31 02:51:45 -07003217 /* combine the address and interface for a hash */
3218 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003219
Neale Rannscd35e532018-08-31 02:51:45 -07003220 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003221
3222 from += 1;
3223 n_left_from -= 1;
3224 to_next_drop[0] = pi0;
3225 to_next_drop += 1;
3226 n_left_to_next_drop -= 1;
3227
3228 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3229
3230 /* If the interface is link-down, drop the pkt */
3231 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3232 drop0 = 1;
3233
3234 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3235 {
3236 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3237
3238 if (ri != ~0)
3239 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3240 else
3241 drop0 = 1;
3242 }
3243 else
3244 drop0 = 1;
3245
3246 /*
3247 * the adj has been updated to a rewrite but the node the DPO that got
3248 * us here hasn't - yet. no big deal. we'll drop while we wait.
3249 */
3250 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3251 drop0 = 1;
3252
3253 p0->error =
3254 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3255 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3256
3257 if (drop0)
3258 continue;
3259
3260 {
3261 u32 bi0 = 0;
3262 icmp6_neighbor_solicitation_header_t *h0;
3263 vlib_buffer_t *b0;
3264
3265 h0 = vlib_packet_template_get_packet
3266 (vm, &im->discover_neighbor_packet_template, &bi0);
John Lo084606b2018-06-19 15:27:48 -04003267 if (!h0)
3268 continue;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003269
Neale Ranns45db8852019-01-09 00:04:04 -08003270 /* copy the persistent fields from the original */
3271 b0 = vlib_get_buffer (vm, bi0);
3272 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3273
Neale Rannsc7b8f202018-04-25 06:34:31 -07003274 /*
3275 * Build ethernet header.
3276 * Choose source address based on destination lookup
3277 * adjacency.
3278 */
3279 if (!ip6_src_address_for_packet (lm,
3280 sw_if_index0,
3281 &ip0->dst_address,
3282 &h0->ip.src_address))
3283 {
3284 /* There is no address on the interface */
3285 p0->error =
3286 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3287 vlib_buffer_free (vm, &bi0, 1);
3288 continue;
3289 }
3290
3291 /*
3292 * Destination address is a solicited node multicast address.
3293 * We need to fill in
3294 * the low 24 bits with low 24 bits of target's address.
3295 */
3296 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3297 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3298 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3299
3300 h0->neighbor.target_address = ip0->dst_address;
3301
3302 clib_memcpy (h0->link_layer_option.ethernet_address,
3303 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3304
3305 /* $$$$ appears we need this; why is the checksum non-zero? */
3306 h0->neighbor.icmp.checksum = 0;
3307 h0->neighbor.icmp.checksum =
3308 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3309 &bogus_length);
3310
3311 ASSERT (bogus_length == 0);
3312
3313 vlib_buffer_copy_trace_flag (vm, p0, bi0);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003314 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3315 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3316
3317 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3318 radv_info->mcast_adj_index;
3319
3320 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3321 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3322
3323 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3324 }
3325 }
3326
3327 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3328 n_left_to_next_drop);
3329 }
3330
3331 return frame->n_vectors;
3332}
3333
3334static uword
3335ip6_discover_neighbor (vlib_main_t * vm,
3336 vlib_node_runtime_t * node, vlib_frame_t * frame)
3337{
3338 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3339}
3340
3341static uword
3342ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3343{
3344 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3345}
3346
3347static char *ip6_discover_neighbor_error_strings[] = {
3348 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3349 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3350 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3351 = "no source address for ND solicitation",
3352};
3353
3354/* *INDENT-OFF* */
3355VLIB_REGISTER_NODE (ip6_glean_node) =
3356{
3357 .function = ip6_glean,
3358 .name = "ip6-glean",
3359 .vector_size = sizeof (u32),
3360 .format_trace = format_ip6_forward_next_trace,
3361 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3362 .error_strings = ip6_discover_neighbor_error_strings,
3363 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3364 .next_nodes =
3365 {
3366 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3367 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3368 },
3369};
3370VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3371{
3372 .function = ip6_discover_neighbor,
3373 .name = "ip6-discover-neighbor",
3374 .vector_size = sizeof (u32),
3375 .format_trace = format_ip6_forward_next_trace,
3376 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3377 .error_strings = ip6_discover_neighbor_error_strings,
3378 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3379 .next_nodes =
3380 {
3381 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3382 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3383 },
3384};
3385/* *INDENT-ON* */
3386
Dave Barachd7cb1b52016-12-09 09:52:16 -05003387/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003388int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003389ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3390 u8 suppress, u8 managed, u8 other,
3391 u8 ll_option, u8 send_unicast, u8 cease,
3392 u8 use_lifetime, u32 lifetime,
3393 u32 initial_count, u32 initial_interval,
3394 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003395{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003396 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3397 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003398 u32 ri;
3399
3400 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003401 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3402 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003403 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003404 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003405
Dave Barachd7cb1b52016-12-09 09:52:16 -05003406 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003407 {
3408
Dave Barachd7cb1b52016-12-09 09:52:16 -05003409 ip6_radv_t *radv_info;
3410 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003411
Dave Barachd7cb1b52016-12-09 09:52:16 -05003412 if ((max_interval != 0) && (min_interval == 0))
3413 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003414
Dave Barachd7cb1b52016-12-09 09:52:16 -05003415 max_interval =
3416 (max_interval !=
3417 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3418 radv_info->max_radv_interval;
3419 min_interval =
3420 (min_interval !=
3421 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3422 radv_info->min_radv_interval;
3423 lifetime =
3424 (use_lifetime !=
3425 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3426 radv_info->adv_router_lifetime_in_sec;
3427
3428 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003429 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003430 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003431 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003432
3433 if (lifetime <= max_interval)
3434 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003435 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003436
3437 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003438 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003439 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3440 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003441 }
3442
Dave Barachd7cb1b52016-12-09 09:52:16 -05003443 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3444 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3445 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003446
Dave Barachd7cb1b52016-12-09 09:52:16 -05003447 /*
3448 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3449 if "flag" is clear don't change corresponding value
3450 */
3451 radv_info->send_radv =
3452 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3453 radv_info->adv_managed_flag =
3454 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3455 radv_info->adv_other_flag =
3456 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3457 radv_info->adv_link_layer_address =
3458 (ll_option !=
3459 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3460 radv_info->send_unicast =
3461 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3462 radv_info->cease_radv =
3463 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3464
3465 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003466 radv_info->max_radv_interval = max_interval;
3467 radv_info->adv_router_lifetime_in_sec = lifetime;
3468
Dave Barachd7cb1b52016-12-09 09:52:16 -05003469 radv_info->initial_adverts_count =
3470 (initial_count !=
3471 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3472 radv_info->initial_adverts_count;
3473 radv_info->initial_adverts_interval =
3474 (initial_interval !=
3475 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3476 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003477
3478 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003479 if ((cease != 0) && (is_no))
3480 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003481
Dave Barachd7cb1b52016-12-09 09:52:16 -05003482 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3483 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003484 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003485 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003486 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003487 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003488}
3489
3490int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003491ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3492 ip6_address_t * prefix_addr, u8 prefix_len,
3493 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3494 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3495 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003496{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003497 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003498 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003499
Ed Warnickecb9cada2015-12-08 15:45:58 -07003500 u32 ri;
3501
3502 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003503 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3504 ~0);
3505
Ed Warnickecb9cada2015-12-08 15:45:58 -07003506 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3507
3508 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003509
3510 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003511 {
3512 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003513 ip6_radv_t *radv_info;
3514 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003515
3516 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003517 ip6_radv_prefix_t *prefix;
3518
Ed Warnickecb9cada2015-12-08 15:45:58 -07003519 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003520 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3521
Ed Warnickecb9cada2015-12-08 15:45:58 -07003522 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3523
Dave Barachd7cb1b52016-12-09 09:52:16 -05003524 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003525 {
3526 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003527 if (!prefix)
3528 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3529
3530 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003531 return VNET_API_ERROR_INVALID_VALUE_2;
3532
Dave Barachd7cb1b52016-12-09 09:52:16 -05003533 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003534 /* do specific delete processing here before returning */
3535 /* try to remove from routing table */
3536
Dave Barachd7cb1b52016-12-09 09:52:16 -05003537 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3538 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003539 pool_put (radv_info->adv_prefixes_pool, prefix);
3540
Dave Barachd7cb1b52016-12-09 09:52:16 -05003541 radv_info->initial_adverts_sent =
3542 radv_info->initial_adverts_count - 1;
3543 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003544 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003545 radv_info->last_radv_time = 0;
3546 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003547 }
3548
3549 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003550 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003551 {
3552 /* add */
3553 u32 pi;
3554 pool_get (radv_info->adv_prefixes_pool, prefix);
3555 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003556 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3557 /* old_value */ 0);
3558
Dave Barachb7b92992018-10-17 10:38:51 -04003559 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05003560
Ed Warnickecb9cada2015-12-08 15:45:58 -07003561 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003562 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3563
Ed Warnickecb9cada2015-12-08 15:45:58 -07003564 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003565 prefix->adv_on_link_flag = 1; /* L bit set */
3566 prefix->adv_autonomous_flag = 1; /* A bit set */
3567 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003568 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3569 prefix->enabled = 1;
3570 prefix->decrement_lifetime_flag = 1;
3571 prefix->deprecated_prefix_flag = 1;
3572
Dave Barachd7cb1b52016-12-09 09:52:16 -05003573 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003574 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003575 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003576 /* insert prefix into routing table as a connected prefix */
3577 }
3578
Dave Barachd7cb1b52016-12-09 09:52:16 -05003579 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003580 goto restart;
3581 }
3582 else
3583 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003584
3585 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003586 return VNET_API_ERROR_INVALID_VALUE_2;
3587
Dave Barachd7cb1b52016-12-09 09:52:16 -05003588 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003589 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003590 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003591 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003592 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003593 }
3594
Dave Barachd7cb1b52016-12-09 09:52:16 -05003595 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003596 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003597 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003598 prefix->adv_pref_lifetime_in_secs = ~0;
3599 prefix->decrement_lifetime_flag = 0;
3600 }
3601 else
3602 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003603 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3604 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003605 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003606
Ed Warnickecb9cada2015-12-08 15:45:58 -07003607 /* copy remaining */
3608 prefix->enabled = !(no_advertise != 0);
3609 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3610 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3611
Dave Barachd7cb1b52016-12-09 09:52:16 -05003612 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003613 /* restart */
3614 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003615 prefix->valid_lifetime_expires =
3616 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003617 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003618
3619 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3620 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003621 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003622 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003623 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003624 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003625}
3626
3627clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003628ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3629 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003630{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003631 vnet_main_t *vnm = vnet_get_main ();
3632 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3633 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003634 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003635 u8 suppress = 0, managed = 0, other = 0;
3636 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003637 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003638 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3639 0, ra_initial_interval = 0;
3640 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003641
Dave Barachd7cb1b52016-12-09 09:52:16 -05003642 unformat_input_t _line_input, *line_input = &_line_input;
3643 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003644
3645 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003646 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003647 ip6_address_t ip6_addr;
3648 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003649
Ed Warnickecb9cada2015-12-08 15:45:58 -07003650
3651 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003652 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003653 return 0;
3654
3655 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003656 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003657 {
3658
Dave Barachd7cb1b52016-12-09 09:52:16 -05003659 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003660 unformat_vnet_sw_interface, vnm, &sw_if_index))
3661 {
3662 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003663 ethernet_interface_t *eth_if0 = 0;
3664
Ed Warnickecb9cada2015-12-08 15:45:58 -07003665 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003666 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3667 eth_if0 =
3668 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3669
3670 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003671 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003672 error =
3673 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003674 goto done;
3675 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003676
Ed Warnickecb9cada2015-12-08 15:45:58 -07003677 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003678 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3679 sw_if_index, ~0);
3680
Ed Warnickecb9cada2015-12-08 15:45:58 -07003681 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003682
3683 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003684 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003685 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003686 }
3687 else
3688 {
3689 error = clib_error_return (0, "unknown interface %U'",
3690 format_unformat_error, line_input);
3691 goto done;
3692 }
3693 }
3694 else
3695 {
3696 error = clib_error_return (0, "invalid interface name %U'",
3697 format_unformat_error, line_input);
3698 goto done;
3699 }
3700 }
3701
3702 /* get the rest of the command */
3703 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3704 {
3705 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003706 is_no = 1;
3707 else if (unformat (line_input, "prefix %U/%d",
3708 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003709 {
3710 add_radv_info = 0;
3711 break;
3712 }
3713 else if (unformat (line_input, "ra-managed-config-flag"))
3714 {
3715 managed = 1;
3716 break;
3717 }
3718 else if (unformat (line_input, "ra-other-config-flag"))
3719 {
3720 other = 1;
3721 break;
3722 }
Chris Luke33879c92016-06-28 19:54:21 -04003723 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003724 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003725 {
Chris Luke33879c92016-06-28 19:54:21 -04003726 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003727 break;
3728 }
Chris Luke33879c92016-06-28 19:54:21 -04003729 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003730 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003731 {
Chris Luke33879c92016-06-28 19:54:21 -04003732 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003733 break;
3734 }
3735 else if (unformat (line_input, "ra-send-unicast"))
3736 {
3737 send_unicast = 1;
3738 break;
3739 }
3740 else if (unformat (line_input, "ra-lifetime"))
3741 {
3742 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003743 {
3744 error = unformat_parse_error (line_input);
3745 goto done;
3746 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003747 use_lifetime = 1;
3748 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003750 else if (unformat (line_input, "ra-initial"))
3751 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003752 if (!unformat
3753 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003754 {
3755 error = unformat_parse_error (line_input);
3756 goto done;
3757 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003758 break;
3759 }
3760 else if (unformat (line_input, "ra-interval"))
3761 {
3762 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003763 {
3764 error = unformat_parse_error (line_input);
3765 goto done;
3766 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003767
3768 if (!unformat (line_input, "%d", &ra_min_interval))
3769 ra_min_interval = 0;
3770 break;
3771 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003772 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003773 {
3774 cease = 1;
3775 break;
3776 }
3777 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003778 {
3779 error = unformat_parse_error (line_input);
3780 goto done;
3781 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003782 }
3783
Dave Barachd7cb1b52016-12-09 09:52:16 -05003784 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003785 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003786 ip6_neighbor_ra_config (vm, sw_if_index,
3787 suppress, managed, other,
3788 suppress_ll_option, send_unicast, cease,
3789 use_lifetime, ra_lifetime,
3790 ra_initial_count, ra_initial_interval,
3791 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003792 }
3793 else
3794 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003795 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003796 u32 pref_lifetime_in_secs = 0;
3797 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003798 u8 no_advertise = 0;
3799 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003800 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003801 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003802
3803 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003804 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003805 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003806 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003807 {
3808 use_prefix_default_values = 1;
3809 break;
3810 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003811 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003812 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003813 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003814 pref_lifetime_in_secs = ~0;
3815 break;
3816 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003817 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3818 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003819 break;
3820 else
3821 break;
3822 }
3823
3824
3825 /* get the rest of the command */
3826 while (!use_prefix_default_values &&
3827 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3828 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003829 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003830 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003831 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003832 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003833 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003834 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003835 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003836 no_onlink = 1;
3837 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003838 {
3839 error = unformat_parse_error (line_input);
3840 goto done;
3841 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003842 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003843
3844 ip6_neighbor_ra_prefix (vm, sw_if_index,
3845 &ip6_addr, addr_len,
3846 use_prefix_default_values,
3847 valid_lifetime_in_secs,
3848 pref_lifetime_in_secs,
3849 no_advertise,
3850 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003851 }
3852
Billy McFalla9a20e72017-02-15 11:39:12 -05003853done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003854 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003855
Ed Warnickecb9cada2015-12-08 15:45:58 -07003856 return error;
3857}
3858
Chris Lukebfd32fd2016-06-24 20:38:06 -04003859static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003860ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003861{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003862 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003863 u32 i;
3864
3865 for (i = 0; i < vec_len (addrs); i++)
3866 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003867 ip_interface_address_t *a =
3868 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3869 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003870
3871 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003872 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003873 }
3874}
3875
Ed Warnickecb9cada2015-12-08 15:45:58 -07003876static clib_error_t *
3877show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003878 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003879{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003880 vnet_main_t *vnm = vnet_get_main ();
3881 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3882 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003883 u32 sw_if_index;
3884
3885 sw_if_index = ~0;
3886
Dave Barachd7cb1b52016-12-09 09:52:16 -05003887 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003888 {
3889 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003890
Dave Barachd7cb1b52016-12-09 09:52:16 -05003891 /* look up the radv_t information for this interface */
3892 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3893 sw_if_index, ~0);
3894
3895 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3896
3897 if (ri != ~0)
3898 {
3899 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3900 ip6_radv_t *radv_info;
3901 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3902
3903 vlib_cli_output (vm, "%U is admin %s\n",
3904 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003905 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003906 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3907 "up" : "down"));
3908
Chris Lukebfd32fd2016-06-24 20:38:06 -04003909 u32 ai;
3910 u32 *link_scope = 0, *global_scope = 0;
3911 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003912 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003913
Dave Barachd7cb1b52016-12-09 09:52:16 -05003914 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3915 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003916 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3917
Dave Barachd7cb1b52016-12-09 09:52:16 -05003918 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003919 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003920 a = pool_elt_at_index (lm->if_address_pool, ai);
3921 ip6_address_t *address =
3922 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003923
Chris Lukebfd32fd2016-06-24 20:38:06 -04003924 if (ip6_address_is_link_local_unicast (address))
3925 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003926 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003927 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003928 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003929 vec_add1 (local_scope, ai);
3930 else
3931 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003932
3933 ai = a->next_this_sw_interface;
3934 }
3935
Dave Barachd7cb1b52016-12-09 09:52:16 -05003936 if (vec_len (link_scope))
3937 {
3938 vlib_cli_output (vm, "\tLink-local address(es):\n");
3939 ip6_print_addrs (vm, link_scope);
3940 vec_free (link_scope);
3941 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003942
Dave Barachd7cb1b52016-12-09 09:52:16 -05003943 if (vec_len (local_scope))
3944 {
3945 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3946 ip6_print_addrs (vm, local_scope);
3947 vec_free (local_scope);
3948 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003949
Dave Barachd7cb1b52016-12-09 09:52:16 -05003950 if (vec_len (global_scope))
3951 {
3952 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3953 ip6_print_addrs (vm, global_scope);
3954 vec_free (global_scope);
3955 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003956
Dave Barachd7cb1b52016-12-09 09:52:16 -05003957 if (vec_len (unknown_scope))
3958 {
3959 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3960 ip6_print_addrs (vm, unknown_scope);
3961 vec_free (unknown_scope);
3962 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003963
Neale Ranns53da2212018-02-24 02:11:19 -08003964 vlib_cli_output (vm, "\tLink-local address(es):\n");
3965 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3966 &radv_info->link_local_address);
3967
Ed Warnickecb9cada2015-12-08 15:45:58 -07003968 vlib_cli_output (vm, "\tJoined group address(es):\n");
3969 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003970 /* *INDENT-OFF* */
3971 pool_foreach (m, radv_info->mldp_group_pool,
3972 ({
3973 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3974 &m->mcast_address);
3975 }));
3976 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003977
3978 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003979 ip6_radv_prefix_t *p;
3980 /* *INDENT-OFF* */
3981 pool_foreach (p, radv_info->adv_prefixes_pool,
3982 ({
3983 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3984 format_ip6_address, &p->prefix, p->prefix_len);
3985 }));
3986 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003987
Dave Barachd7cb1b52016-12-09 09:52:16 -05003988 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003989 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3990 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3991 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3992 vlib_cli_output (vm, "\tND DAD is disabled\n");
3993 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3994 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3995 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003996 vlib_cli_output (vm,
3997 "\tND advertised retransmit interval is %d (msec)\n",
3998 radv_info->
3999 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004000
4001 u32 ra_interval = radv_info->max_radv_interval;
4002 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004003 vlib_cli_output (vm,
4004 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004005 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004006 vlib_cli_output (vm,
4007 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004008 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004009 vlib_cli_output (vm,
4010 "\tHosts %s stateless autoconfig for addresses\n",
4011 (radv_info->adv_managed_flag) ? "use" :
4012 " don't use");
4013 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4014 radv_info->n_advertisements_sent);
4015 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4016 radv_info->n_solicitations_rcvd);
4017 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4018 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004019 }
4020 else
4021 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004022 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004023 format_unformat_error, input);
4024
4025 }
4026 }
4027 return error;
4028}
4029
Billy McFall0683c9c2016-10-13 08:27:31 -04004030/*?
4031 * This command is used to display various IPv6 attributes on a given
4032 * interface.
4033 *
4034 * @cliexpar
4035 * Example of how to display IPv6 settings:
4036 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4037 * GigabitEthernet2/0/0 is admin up
4038 * Link-local address(es):
4039 * fe80::ab8/64
4040 * Joined group address(es):
4041 * ff02::1
4042 * ff02::2
4043 * ff02::16
4044 * ff02::1:ff00:ab8
4045 * Advertised Prefixes:
4046 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4047 * MTU is 1500
4048 * ICMP error messages are unlimited
4049 * ICMP redirects are disabled
4050 * ICMP unreachables are not sent
4051 * ND DAD is disabled
4052 * ND advertised reachable time is 0
4053 * ND advertised retransmit interval is 0 (msec)
4054 * ND router advertisements are sent every 200 seconds (min interval is 150)
4055 * ND router advertisements live for 600 seconds
4056 * Hosts use stateless autoconfig for addresses
4057 * ND router advertisements sent 19336
4058 * ND router solicitations received 0
4059 * ND router solicitations dropped 0
4060 * @cliexend
4061 * Example of output if IPv6 is not enabled on the interface:
4062 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4063 * show ip6 interface: IPv6 not enabled on interface
4064 * @cliexend
4065?*/
4066/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004067VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4068{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004069 .path = "show ip6 interface",
4070 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004071 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004072};
Billy McFall0683c9c2016-10-13 08:27:31 -04004073/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004074
4075clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004076disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004077{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004078 clib_error_t *error = 0;
4079 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004080 u32 ri;
4081
4082 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004083 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4084 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004085 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004086
Ed Warnickecb9cada2015-12-08 15:45:58 -07004087 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004088 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004089 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004090 ip6_radv_t *radv_info;
4091
4092 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004093
4094 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004095 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004096 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004097 {
4098 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004099 ip6_ll_prefix_t ilp = {
4100 .ilp_addr = radv_info->link_local_address,
4101 .ilp_sw_if_index = sw_if_index,
4102 };
4103 ip6_ll_table_entry_delete (&ilp);
4104 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004105 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004106 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4107 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004108 }
4109 }
4110 return error;
4111}
4112
4113int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004114ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004115{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004116 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4117 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004118
Dave Barachd7cb1b52016-12-09 09:52:16 -05004119 /* look up the radv_t information for this interface */
4120 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4121 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004122
Dave Barachd7cb1b52016-12-09 09:52:16 -05004123 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004124
Dave Barachd7cb1b52016-12-09 09:52:16 -05004125 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004126}
4127
Dave Barachd7cb1b52016-12-09 09:52:16 -05004128clib_error_t *
4129enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004130{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004131 clib_error_t *error = 0;
4132 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004133 u32 ri;
4134 int is_add = 1;
4135
4136 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004137 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4138 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004139
Dave Barachd7cb1b52016-12-09 09:52:16 -05004140 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4141
4142 /* if not created yet */
4143 if (ri == ~0)
4144 {
4145 vnet_main_t *vnm = vnet_get_main ();
4146 vnet_sw_interface_t *sw_if0;
4147
4148 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4149 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4150 {
4151 ethernet_interface_t *eth_if0;
4152
4153 eth_if0 =
4154 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4155 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004156 {
4157 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004158 ri =
4159 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004160
Dave Barachd7cb1b52016-12-09 09:52:16 -05004161 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004162 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004163 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004164 ip6_address_t link_local_address;
4165
Dave Barachd7cb1b52016-12-09 09:52:16 -05004166 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004167
Dave Barachd7cb1b52016-12-09 09:52:16 -05004168 ip6_link_local_address_from_ethernet_mac_address
4169 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004170
4171 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004172 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07004173 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004174 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004175 {
4176 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004177 link_local_address.as_u64[1] =
4178 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004179
4180 link_local_address.as_u64[0] =
4181 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004182 /* clear u bit */
4183 link_local_address.as_u8[8] &= 0xfd;
4184 }
Neale Ranns53da2212018-02-24 02:11:19 -08004185 {
4186 ip6_ll_prefix_t ilp = {
4187 .ilp_addr = link_local_address,
4188 .ilp_sw_if_index = sw_if_index,
4189 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004190
Neale Ranns53da2212018-02-24 02:11:19 -08004191 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4192 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004193
Ed Warnickecb9cada2015-12-08 15:45:58 -07004194 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004195 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4196 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004197
Neale Ranns53da2212018-02-24 02:11:19 -08004198 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004199 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004200 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004201 }
4202 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004204 }
4205 }
4206 return error;
4207}
4208
Neale Ranns53da2212018-02-24 02:11:19 -08004209int
4210ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4211{
4212 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4213 ip6_radv_t *radv_info;
4214 u32 ri;
4215
Eyal Baricd307742018-07-22 12:45:15 +03004216 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
Neale Ranns53da2212018-02-24 02:11:19 -08004217 return 0;
4218
4219 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4220
4221 if (ri == ~0)
4222 return 0;
4223
4224 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4225 *addr = radv_info->link_local_address;
4226
4227 return (!0);
4228}
4229
Ed Warnickecb9cada2015-12-08 15:45:58 -07004230static clib_error_t *
4231enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004232 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004233{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004234 vnet_main_t *vnm = vnet_get_main ();
4235 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004236 u32 sw_if_index;
4237
4238 sw_if_index = ~0;
4239
Dave Barachd7cb1b52016-12-09 09:52:16 -05004240 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004241 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004242 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004243 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004244 else
4245 {
4246 error = clib_error_return (0, "unknown interface\n'",
4247 format_unformat_error, input);
4248
4249 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004250 return error;
4251}
4252
Billy McFall0683c9c2016-10-13 08:27:31 -04004253/*?
4254 * This command is used to enable IPv6 on a given interface.
4255 *
4256 * @cliexpar
4257 * Example of how enable IPv6 on a given interface:
4258 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4259?*/
4260/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004261VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4262{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004263 .path = "enable ip6 interface",
4264 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004265 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004266};
Billy McFall0683c9c2016-10-13 08:27:31 -04004267/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004268
4269static clib_error_t *
4270disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004271 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004272{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004273 vnet_main_t *vnm = vnet_get_main ();
4274 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004275 u32 sw_if_index;
4276
4277 sw_if_index = ~0;
4278
Dave Barachd7cb1b52016-12-09 09:52:16 -05004279 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004280 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004281 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004282 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004283 else
4284 {
4285 error = clib_error_return (0, "unknown interface\n'",
4286 format_unformat_error, input);
4287
4288 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004289 return error;
4290}
4291
Billy McFall0683c9c2016-10-13 08:27:31 -04004292/*?
4293 * This command is used to disable IPv6 on a given interface.
4294 *
4295 * @cliexpar
4296 * Example of how disable IPv6 on a given interface:
4297 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4298?*/
4299/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004300VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4301{
Billy McFall0683c9c2016-10-13 08:27:31 -04004302 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004303 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004304 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004305};
Billy McFall0683c9c2016-10-13 08:27:31 -04004306/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004307
Billy McFall0683c9c2016-10-13 08:27:31 -04004308/*?
4309 * This command is used to configure the neighbor discovery
4310 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4311 * command to display some of the current neighbor discovery parameters
4312 * on a given interface. This command has three formats:
4313 *
4314 *
4315 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4316 *
4317 * '<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>'
4318 *
4319 * Where:
4320 *
4321 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4322 * router-advertisement messages to use stateful address
4323 * auto-configuration to obtain address information (sets the M-bit).
4324 * Default is the M-bit is not set and the '<em>no</em>' option
4325 * returns it to this default state.
4326 *
4327 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4328 * router-advertisement messages that hosts use stateful auto
4329 * configuration to obtain nonaddress related information (sets
4330 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4331 * option returns it to this default state.
4332 *
4333 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4334 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4335 * router-advertisement messages.
4336 *
4337 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4338 * optional source link-layer address in the ICMPv6 router-advertisement
4339 * messages. Default is to include the optional source link-layer address
4340 * and the '<em>no</em>' option returns it to this default state.
4341 *
4342 * <em>[no] ra-send-unicast</em> - Use the source address of the
4343 * router-solicitation message if availiable. The default is to use
4344 * multicast address of all nodes, and the '<em>no</em>' option returns
4345 * it to this default state.
4346 *
4347 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4348 * default router in ICMPv6 router-advertisement messages. The range is
4349 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4350 * '<em><max-interval></em>'. The default value is 600 seconds and the
4351 * '<em>no</em>' option returns it to this default value.
4352 *
4353 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4354 * router-advertisement messages sent and the interval between each
4355 * message. Range for count is 1 - 3 and default is 3. Range for interval
4356 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4357 * returns both to their default value.
4358 *
4359 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4360 * interval between sending ICMPv6 router-advertisement messages. The
4361 * range for max-interval is from 4 to 200 seconds. min-interval can not
4362 * be more than 75% of max-interval. If not set, min-interval will be
4363 * set to 75% of max-interval. The range for min-interval is from 3 to
4364 * 150 seconds. The '<em>no</em>' option returns both to their default
4365 * value.
4366 *
4367 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4368 * The '<em>no</em>' options implies to start (or restart) sending
4369 * ICMPv6 router-advertisement messages.
4370 *
4371 *
4372 * <b>Format 2 - Prefix Options:</b>
4373 *
4374 * '<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>'
4375 *
4376 * Where:
4377 *
4378 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4379 *
4380 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4381 * length of time in seconds during what the prefix is valid for the purpose of
4382 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4383 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4384 * length of time in seconds during what addresses generated from the prefix remain
4385 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4386 *
4387 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4388 * are inifinte, no timeout.
4389 *
4390 * <em>no-advertise</em> - Do not send full router address in prefix
4391 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4392 *
4393 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4394 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4395 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4396 *
4397 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4398 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4399 *
4400 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4401 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4402 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4403 * the L-bit.
4404 *
4405 *
4406 * <b>Format 3: - Default of Prefix:</b>
4407 *
4408 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4409 *
4410 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4411 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4412 * is ignored and the prefix is deleted.
4413 *
4414 *
4415 * @cliexpar
4416 * Example of how set a router advertisement option:
4417 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4418 * Example of how to add a prefix:
4419 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4420 * Example of how to delete a prefix:
4421 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4422?*/
4423/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004424VLIB_CLI_COMMAND (ip6_nd_command, static) =
4425{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004426 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004427 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004428 .function = ip6_neighbor_cmd,
4429};
Billy McFall0683c9c2016-10-13 08:27:31 -04004430/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004431
4432clib_error_t *
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004433ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4434 ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004435{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004436 clib_error_t *error = 0;
4437 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004438 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004439 ip6_radv_t *radv_info;
4440 vnet_main_t *vnm = vnet_get_main ();
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004441 ip6_ll_prefix_t pfx = { 0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07004442
Dave Barachd7cb1b52016-12-09 09:52:16 -05004443 if (!ip6_address_is_link_local_unicast (address))
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004444 return (error = clib_error_return (0, "address not link-local",
4445 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004446
4447 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004448 enable_ip6_interface (vm, sw_if_index);
4449
Ed Warnickecb9cada2015-12-08 15:45:58 -07004450 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004451
4452 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004453 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004454 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004455
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004456 pfx.ilp_sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004457
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004458 pfx.ilp_addr = radv_info->link_local_address;
4459 ip6_ll_table_entry_delete (&pfx);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004460
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004461 pfx.ilp_addr = *address;
4462 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004463
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004464 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4465 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004466 }
4467 else
4468 {
4469 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4470 error = clib_error_return (0, "ip6 not enabled for interface",
4471 format_unformat_error);
4472 }
4473 return error;
4474}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004475
Neale Ranns87df12d2017-02-18 08:16:41 -08004476/**
4477 * @brief callback when an interface address is added or deleted
4478 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004479static void
4480ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4481 uword opaque,
4482 u32 sw_if_index,
4483 ip6_address_t * address,
4484 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004485 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004486{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004487 vnet_main_t *vnm = vnet_get_main ();
4488 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004489 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004490 vlib_main_t *vm = vnm->vlib_main;
4491 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004492 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004493
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -07004494 /* create solicited node multicast address for this interface address */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004495 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004496
Ed Warnickecb9cada2015-12-08 15:45:58 -07004497 a.as_u8[0xd] = address->as_u8[0xd];
4498 a.as_u8[0xe] = address->as_u8[0xe];
4499 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004500
4501 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004502 {
4503 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004504 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004505
4506 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004507 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4508 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004509 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004510 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004511 {
4512 /* get radv_info */
4513 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4514
4515 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004516 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004517 radv_info->ref_count++;
4518
Neale Ranns87df12d2017-02-18 08:16:41 -08004519 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004520 }
4521 }
4522 else
4523 {
4524
4525 /* delete */
4526 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004527 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4528 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004529 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004530
Dave Barachd7cb1b52016-12-09 09:52:16 -05004531 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004532 {
4533 /* get radv_info */
4534 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4535
Neale Ranns87df12d2017-02-18 08:16:41 -08004536 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004537
4538 /* if interface up send MLDP "report" */
4539 radv_info->all_routers_mcast = 0;
4540
4541 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004542 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004543 radv_info->ref_count--;
4544 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004545 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4546 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004547 }
4548}
4549
Dave Barachd7cb1b52016-12-09 09:52:16 -05004550clib_error_t *
4551ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004552{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004553 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004554
4555 nm->limit_neighbor_cache_size = neighbor_limit;
4556 return 0;
4557}
4558
Neale Ranns15002542017-09-10 04:39:11 -07004559static void
4560ip6_neighbor_table_bind (ip6_main_t * im,
4561 uword opaque,
4562 u32 sw_if_index,
4563 u32 new_fib_index, u32 old_fib_index)
4564{
4565 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4566 ip6_neighbor_t *n = NULL;
4567 u32 i, *to_re_add = 0;
4568
4569 /* *INDENT-OFF* */
4570 pool_foreach (n, nm->neighbor_pool,
4571 ({
4572 if (n->key.sw_if_index == sw_if_index)
4573 vec_add1 (to_re_add, n - nm->neighbor_pool);
4574 }));
4575 /* *INDENT-ON* */
4576
4577 for (i = 0; i < vec_len (to_re_add); i++)
4578 {
4579 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4580 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4581 ip6_neighbor_adj_fib_add (n, new_fib_index);
4582 }
4583 vec_free (to_re_add);
4584}
4585
Dave Barachd7cb1b52016-12-09 09:52:16 -05004586static clib_error_t *
4587ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004588{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004589 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4590 ip6_main_t *im = &ip6_main;
4591
Ed Warnickecb9cada2015-12-08 15:45:58 -07004592 mhash_init (&nm->neighbor_index_by_key,
4593 /* value size */ sizeof (uword),
4594 /* key size */ sizeof (ip6_neighbor_key_t));
4595
Dave Barachd7cb1b52016-12-09 09:52:16 -05004596 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4597 ip6_icmp_neighbor_solicitation_node.index);
4598 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4599 ip6_icmp_neighbor_advertisement_node.index);
4600 icmp6_register_type (vm, ICMP6_router_solicitation,
4601 ip6_icmp_router_solicitation_node.index);
4602 icmp6_register_type (vm, ICMP6_router_advertisement,
4603 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004604
4605 /* handler node for ip6 neighbor discovery events and timers */
4606 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4607
4608 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004609 ip6_add_del_interface_address_callback_t cb;
Dave Barachb7b92992018-10-17 10:38:51 -04004610 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05004611
Ed Warnickecb9cada2015-12-08 15:45:58 -07004612 /* when an interface address changes... */
4613 cb.function = ip6_neighbor_add_del_interface_address;
4614 cb.function_opaque = 0;
4615 vec_add1 (im->add_del_interface_address_callbacks, cb);
4616
Neale Ranns15002542017-09-10 04:39:11 -07004617 ip6_table_bind_callback_t cbt;
4618 cbt.function = ip6_neighbor_table_bind;
4619 cbt.function_opaque = 0;
4620 vec_add1 (im->table_bind_callbacks, cbt);
4621
Ed Warnickecb9cada2015-12-08 15:45:58 -07004622 mhash_init (&nm->pending_resolutions_by_address,
4623 /* value size */ sizeof (uword),
4624 /* key size */ sizeof (ip6_address_t));
4625
John Lo1edfba92016-08-27 01:11:57 -04004626 mhash_init (&nm->mac_changes_by_address,
4627 /* value size */ sizeof (uword),
4628 /* key size */ sizeof (ip6_address_t));
4629
Ed Warnickecb9cada2015-12-08 15:45:58 -07004630 /* default, configurable */
4631 nm->limit_neighbor_cache_size = 50000;
4632
Eyal Baric125ecc2017-09-20 11:29:17 +03004633 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4634
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004635 nm->ip6_ra_publisher_node = (uword) ~ 0;
4636
Ed Warnickecb9cada2015-12-08 15:45:58 -07004637#if 0
4638 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004639 vec_validate_init_empty
4640 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004641#endif
4642
Ed Warnickecb9cada2015-12-08 15:45:58 -07004643 return 0;
4644}
4645
4646VLIB_INIT_FUNCTION (ip6_neighbor_init);
4647
4648
Dave Barachd7cb1b52016-12-09 09:52:16 -05004649void
4650vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4651 void *address_arg,
4652 uword node_index,
4653 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004654{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004655 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4656 ip6_address_t *address = address_arg;
4657 uword *p;
4658 pending_resolution_t *pr;
4659
Ed Warnickecb9cada2015-12-08 15:45:58 -07004660 pool_get (nm->pending_resolutions, pr);
4661
4662 pr->next_index = ~0;
4663 pr->node_index = node_index;
4664 pr->type_opaque = type_opaque;
4665 pr->data = data;
4666
4667 p = mhash_get (&nm->pending_resolutions_by_address, address);
4668 if (p)
4669 {
4670 /* Insert new resolution at the head of the list */
4671 pr->next_index = p[0];
4672 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4673 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004674
4675 mhash_set (&nm->pending_resolutions_by_address, address,
4676 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004677}
4678
Dave Barachd7cb1b52016-12-09 09:52:16 -05004679int
4680vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
Neale Ranns37029302018-08-10 05:30:06 -07004681 ip6_nd_change_event_cb_t data_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004682 u32 pid,
4683 void *address_arg,
4684 uword node_index,
4685 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004686{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004687 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4688 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004689
Eyal Barif9f40652017-04-01 03:55:08 +03004690 /* Try to find an existing entry */
4691 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4692 u32 *p = first;
4693 pending_resolution_t *mc;
4694 while (p && *p != ~0)
4695 {
4696 mc = pool_elt_at_index (nm->mac_changes, *p);
4697 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4698 && mc->pid == pid)
4699 break;
4700 p = &mc->next_index;
4701 }
4702
4703 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004704 if (is_add)
4705 {
Eyal Barif9f40652017-04-01 03:55:08 +03004706 if (found)
4707 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4708
John Lo1edfba92016-08-27 01:11:57 -04004709 pool_get (nm->mac_changes, mc);
Neale Rannsf12dad62018-06-04 18:41:24 -07004710 /* *INDENT-OFF* */
Eyal Barif9f40652017-04-01 03:55:08 +03004711 *mc = (pending_resolution_t)
4712 {
Neale Rannsf12dad62018-06-04 18:41:24 -07004713 .next_index = ~0,
4714 .node_index = node_index,
4715 .type_opaque = type_opaque,
4716 .data = data,
4717 .data_callback = data_callback,
4718 .pid = pid,
4719 };
4720 /* *INDENT-ON* */
John Lo1edfba92016-08-27 01:11:57 -04004721
Eyal Barif9f40652017-04-01 03:55:08 +03004722 /* Insert new resolution at the end of the list */
4723 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004724 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004725 p[0] = new_idx;
4726 else
4727 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004728 }
4729 else
4730 {
Eyal Barif9f40652017-04-01 03:55:08 +03004731 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004732 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004733
Eyal Barif9f40652017-04-01 03:55:08 +03004734 /* Clients may need to clean up pool entries, too */
Neale Ranns37029302018-08-10 05:30:06 -07004735 if (data_callback)
4736 (data_callback) (mc->data, NULL /* no new mac addrs */ , 0, NULL);
John Lo1edfba92016-08-27 01:11:57 -04004737
Eyal Barif9f40652017-04-01 03:55:08 +03004738 /* Remove the entry from the list and delete the entry */
4739 *p = mc->next_index;
4740 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004741
Eyal Barif9f40652017-04-01 03:55:08 +03004742 /* Remove from hash if we deleted the last entry */
4743 if (*p == ~0 && p == first)
4744 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004745 }
Eyal Barif9f40652017-04-01 03:55:08 +03004746 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004747}
4748
Dave Barachd7cb1b52016-12-09 09:52:16 -05004749int
4750vnet_ip6_nd_term (vlib_main_t * vm,
4751 vlib_node_runtime_t * node,
4752 vlib_buffer_t * p0,
4753 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004754 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004755{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004756 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4757 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
Neale Ranns37029302018-08-10 05:30:06 -07004758 mac_address_t mac;
John Lo1edfba92016-08-27 01:11:57 -04004759
Neale Ranns37029302018-08-10 05:30:06 -07004760 mac_address_from_bytes (&mac, eth->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004761 ndh = ip6_next_header (ip);
4762 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4763 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004764 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004765
4766 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4767 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4768 {
4769 u8 *t0 = vlib_add_trace (vm, node, p0,
4770 sizeof (icmp6_input_trace_t));
4771 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4772 }
4773
4774 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004775 if (PREDICT_FALSE
4776 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4777 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004778 {
Neale Ranns37029302018-08-10 05:30:06 -07004779 vnet_nd_wc_publish (sw_if_index, &mac, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004780 }
4781
4782 /* Check if MAC entry exsist for solicited target IP */
4783 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4784 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004785 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004786 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004787 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004788
4789 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004790 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004791 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4792 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004793 return 0; /* source link layer address option not present */
4794
John Lo1edfba92016-08-27 01:11:57 -04004795 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004796 macp =
4797 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004798 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004799 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004800 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004801 vlib_node_runtime_t *error_node =
4802 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004803 ip->dst_address = ip->src_address;
4804 ip->src_address = ndh->target_address;
4805 ip->hop_limit = 255;
4806 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004807 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004808 clib_memcpy (opt->ethernet_address, macp, 6);
4809 ndh->icmp.type = ICMP6_neighbor_advertisement;
4810 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004811 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4812 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004813 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004814 ndh->icmp.checksum =
4815 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4816 clib_memcpy (eth->dst_address, eth->src_address, 6);
4817 clib_memcpy (eth->src_address, macp, 6);
4818 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004819 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004820 return 1;
4821 }
John Lo1edfba92016-08-27 01:11:57 -04004822 }
4823
4824 return 0;
4825
4826}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004827
Neale Ranns3f844d02017-02-18 00:03:54 -08004828int
4829ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4830{
4831 u32 fib_index;
4832
4833 fib_prefix_t pfx = {
4834 .fp_len = 128,
4835 .fp_proto = FIB_PROTOCOL_IP6,
4836 .fp_addr = {
4837 .ip6 = *addr,
4838 },
4839 };
4840 ip46_address_t nh = {
4841 .ip6 = *addr,
4842 };
4843
4844 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4845
4846 if (~0 == fib_index)
4847 return VNET_API_ERROR_NO_SUCH_FIB;
4848
4849 if (is_del)
4850 {
4851 fib_table_entry_path_remove (fib_index,
4852 &pfx,
4853 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004854 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004855 &nh,
4856 sw_if_index,
4857 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4858 /* flush the ND cache of this address if it's there */
Neale Ranns0bdd3192018-09-07 11:04:52 -07004859 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
Neale Ranns3f844d02017-02-18 00:03:54 -08004860 }
4861 else
4862 {
4863 fib_table_entry_path_add (fib_index,
4864 &pfx,
4865 FIB_SOURCE_IP6_ND_PROXY,
4866 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004867 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004868 &nh,
4869 sw_if_index,
4870 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4871 }
4872 return (0);
4873}
4874
4875static clib_error_t *
4876set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4877 unformat_input_t * input, vlib_cli_command_t * cmd)
4878{
4879 vnet_main_t *vnm = vnet_get_main ();
4880 clib_error_t *error = 0;
4881 ip6_address_t addr;
4882 u32 sw_if_index;
4883 u8 is_del = 0;
4884
4885 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4886 {
4887 /* get the rest of the command */
4888 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4889 {
4890 if (unformat (input, "%U", unformat_ip6_address, &addr))
4891 break;
4892 else if (unformat (input, "delete") || unformat (input, "del"))
4893 is_del = 1;
4894 else
4895 return (unformat_parse_error (input));
4896 }
4897 }
4898
4899 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4900
4901 return error;
4902}
4903
4904/* *INDENT-OFF* */
4905VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4906{
4907 .path = "set ip6 nd proxy",
4908 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4909 .function = set_ip6_nd_proxy_cmd,
4910};
4911/* *INDENT-ON* */
4912
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004913void
Neale Ranns3be6b282016-12-20 14:24:01 +00004914ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004915{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004916 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4917 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004918 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004919
4920 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004921 pool_foreach (n, nm->neighbor_pool,
4922 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004923 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004924 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004925 adj_nbr_walk_nh6 (sw_if_index,
4926 &n->key.ip6_address,
4927 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004928 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004929 }));
4930 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08004931
4932 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4933
4934 if (ADJ_INDEX_INVALID != ai)
4935 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004936}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004937
John Lo8b81cb42017-06-26 01:40:20 -04004938void
Steven9f781d82018-06-05 11:09:32 -07004939send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04004940{
4941 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04004942 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004943
Steven9f781d82018-06-05 11:09:32 -07004944 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004945}
4946
4947void
4948send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07004949 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07004950{
4951 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07004952 vnet_main_t *vnm = vnet_get_main ();
4953 u8 *rewrite, rewrite_len;
4954 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4955 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07004956
John Lo8b81cb42017-06-26 01:40:20 -04004957 if (ip6_addr)
4958 {
4959 clib_warning
4960 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4961 format_ip6_address, ip6_addr, sw_if_index);
4962
4963 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4964 int bogus_length;
4965 u32 bi = 0;
4966 icmp6_neighbor_solicitation_header_t *h =
4967 vlib_packet_template_get_packet (vm,
4968 &i6m->discover_neighbor_packet_template,
4969 &bi);
John Lo084606b2018-06-19 15:27:48 -04004970 if (!h)
4971 return;
4972
John Lo8b81cb42017-06-26 01:40:20 -04004973 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4974 IP6_MULTICAST_SCOPE_link_local,
4975 IP6_MULTICAST_GROUP_ID_all_hosts);
4976 h->ip.src_address = ip6_addr[0];
4977 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4978 h->neighbor.target_address = ip6_addr[0];
4979 h->neighbor.advertisement_flags = clib_host_to_net_u32
4980 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
ahdj00722130e12018-08-14 10:35:43 +08004981 h->link_layer_option.header.type =
4982 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo8b81cb42017-06-26 01:40:20 -04004983 clib_memcpy (h->link_layer_option.ethernet_address,
4984 hi->hw_address, vec_len (hi->hw_address));
4985 h->neighbor.icmp.checksum =
4986 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4987 ASSERT (bogus_length == 0);
4988
4989 /* Setup MAC header with IP6 Etype and mcast DMAC */
4990 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07004991 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04004992 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07004993 rewrite =
4994 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
4995 rewrite_len = vec_len (rewrite);
4996 vlib_buffer_advance (b, -rewrite_len);
4997 ethernet_header_t *e = vlib_buffer_get_current (b);
4998 clib_memcpy (e->dst_address, rewrite, rewrite_len);
4999 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04005000
5001 /* Send unsolicited ND advertisement packet out the specified interface */
5002 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5003 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5004 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5005 u32 *to_next = vlib_frame_vector_args (f);
5006 to_next[0] = bi;
5007 f->n_vectors = 1;
5008 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5009 }
5010}
5011
Dave Barachd7cb1b52016-12-09 09:52:16 -05005012/*
5013 * fd.io coding-style-patch-verification: ON
5014 *
5015 * Local Variables:
5016 * eval: (c-set-style "gnu")
5017 * End:
5018 */