blob: a54f9e91810fbb984b31cda874e5a13835d46a5f [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
Billy McFall0683c9c2016-10-13 08:27:31 -040030/**
31 * @file
32 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
33 *
34 * The files contains the API and CLI code for managing IPv6 neighbor
35 * adjacency tables and neighbor discovery logic.
36 */
37
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010038/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#define ETHER_MAC_ADDR_LEN 6
40
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010041/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050042typedef struct
43{
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 /* basic advertised information */
45 ip6_address_t prefix;
46 u8 prefix_len;
47 int adv_on_link_flag;
48 int adv_autonomous_flag;
49 u32 adv_valid_lifetime_in_secs;
50 u32 adv_pref_lifetime_in_secs;
51
52 /* advertised values are computed from these times if decrementing */
53 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050054 f64 pref_lifetime_expires;
55
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 /* local information */
57 int enabled;
58 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050059 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
61#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
62#define DEF_ADV_VALID_LIFETIME 2592000
63#define DEF_ADV_PREF_LIFETIME 604800
64
65 /* extensions are added here, mobile, DNS etc.. */
66} ip6_radv_prefix_t;
67
68
Dave Barachd7cb1b52016-12-09 09:52:16 -050069typedef struct
70{
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 /* group information */
72 u8 type;
73 ip6_address_t mcast_address;
74 u16 num_sources;
75 ip6_address_t *mcast_source_address_pool;
76} ip6_mldp_group_t;
77
78/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050079typedef struct
80{
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050083 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 int adv_managed_flag;
85 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050086 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 u32 adv_neighbor_reachable_time_in_msec;
88 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
89
90 /* mtu option */
91 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050092
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050094 u8 link_layer_address[8];
95 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050098 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100 /* Hash table mapping address to index in interface advertised prefix pool. */
101 mhash_t address_to_prefix_index;
102
103 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500104 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
106 /* Hash table mapping address to index in mldp address pool. */
107 mhash_t address_to_mldp_index;
108
109 /* local information */
110 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500111 int send_radv; /* radv on/off on this interface - set by config */
112 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 int send_unicast;
114 int adv_link_layer_address;
115 int prefix_option;
116 int failed_device_check;
117 int all_routers_mcast;
118 u32 seed;
119 u64 randomizer;
120 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000121 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 /* timing information */
124#define DEF_MAX_RADV_INTERVAL 200
125#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
126#define DEF_CURR_HOP_LIMIT 64
127#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
128#define MAX_DEF_RTR_LIFETIME 9000
129
Dave Barachd7cb1b52016-12-09 09:52:16 -0500130#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
131#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
132#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
133#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
134#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135
136 f64 max_radv_interval;
137 f64 min_radv_interval;
138 f64 min_delay_between_radv;
139 f64 max_delay_between_radv;
140 f64 max_rtr_default_lifetime;
141
142 f64 last_radv_time;
143 f64 last_multicast_time;
144 f64 next_multicast_time;
145
146
147 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500148 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 u32 initial_adverts_sent;
150
151 /* stats */
152 u32 n_advertisements_sent;
153 u32 n_solicitations_rcvd;
154 u32 n_solicitations_dropped;
155
156 /* Link local address to use (defaults to underlying physical for logical interfaces */
157 ip6_address_t link_local_address;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100158
159 /* router solicitations sending state */
160 u8 keep_sending_rs; /* when true then next fields are valid */
161 icmp6_send_router_solicitation_params_t params;
162 f64 sleep_interval;
163 f64 due_time;
164 u32 n_left;
165 f64 start_time;
166 vlib_buffer_t *buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167} ip6_radv_t;
168
Dave Barachd7cb1b52016-12-09 09:52:16 -0500169typedef struct
170{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 u32 next_index;
172 uword node_index;
173 uword type_opaque;
174 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400175 /* Used for nd event notification only */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500176 void *data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400177 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178} pending_resolution_t;
179
180
Dave Barachd7cb1b52016-12-09 09:52:16 -0500181typedef struct
182{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
186 /* lite beer "glean" adjacency handling */
187 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500188 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
John Lo1edfba92016-08-27 01:11:57 -0400190 /* Mac address change notification */
191 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400193
Dave Barachd7cb1b52016-12-09 09:52:16 -0500194 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
Dave Barachd7cb1b52016-12-09 09:52:16 -0500196 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
198 mhash_t neighbor_index_by_key;
199
Dave Barachd7cb1b52016-12-09 09:52:16 -0500200 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201
Dave Barachd7cb1b52016-12-09 09:52:16 -0500202 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
204 /* Neighbor attack mitigation */
205 u32 limit_neighbor_cache_size;
206 u32 neighbor_delete_rotor;
207
Eyal Baric125ecc2017-09-20 11:29:17 +0300208 /* Wildcard nd report publisher */
209 uword wc_ip6_nd_publisher_node;
210 uword wc_ip6_nd_publisher_et;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100211
212 /* Router advertisement report publisher */
213 uword ip6_ra_publisher_node;
214 uword ip6_ra_publisher_et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215} ip6_neighbor_main_t;
216
Neale Ranns3f844d02017-02-18 00:03:54 -0800217/* ipv6 neighbor discovery - timer/event types */
218typedef enum
219{
220 ICMP6_ND_EVENT_INIT,
221} ip6_icmp_neighbor_discovery_event_type_t;
222
223typedef union
224{
225 u32 add_del_swindex;
226 struct
227 {
228 u32 up_down_swindex;
229 u32 fib_index;
230 } up_down_event;
231} ip6_icmp_neighbor_discovery_event_data_t;
232
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233static ip6_neighbor_main_t ip6_neighbor_main;
Juraj Sloboda52574522018-05-03 10:03:50 +0200234ip6_neighbor_public_main_t ip6_neighbor_public_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500235static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Eyal Baric125ecc2017-09-20 11:29:17 +0300237static void wc_nd_signal_report (wc_nd_report_t * r);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100238static void ra_signal_report (ra_report_t * r);
Eyal Baric125ecc2017-09-20 11:29:17 +0300239
Juraj Sloboda81119e82018-05-25 14:02:20 +0200240ip6_address_t
241ip6_neighbor_get_link_local_address (u32 sw_if_index)
242{
243 static ip6_address_t empty_address = { {0} };
244 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
245 ip6_radv_t *radv_info;
246 u32 ri;
247
248 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
249 if (ri == ~0)
250 {
251 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
252 return empty_address;
253 }
254 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
255 if (radv_info == NULL)
256 {
257 clib_warning ("Internal error");
258 return empty_address;
259 }
260 return radv_info->link_local_address;
261}
262
Eyal Baric125ecc2017-09-20 11:29:17 +0300263/**
264 * @brief publish wildcard arp event
265 * @param sw_if_index The interface on which the ARP entires are acted
266 */
267static int
268vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
269{
270 wc_nd_report_t r = {
271 .sw_if_index = sw_if_index,
272 .ip6 = *ip6,
273 };
274 memcpy (r.mac, mac, sizeof r.mac);
275
276 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
277 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
278 return 0;
279}
280
281static void
282wc_nd_signal_report (wc_nd_report_t * r)
283{
284 vlib_main_t *vm = vlib_get_main ();
285 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
286 uword ni = nm->wc_ip6_nd_publisher_node;
287 uword et = nm->wc_ip6_nd_publisher_et;
288
289 if (ni == (uword) ~ 0)
290 return;
291 wc_nd_report_t *q =
292 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
293
294 *q = *r;
295}
296
297void
298wc_nd_set_publisher_node (uword node_index, uword event_type)
299{
300 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
301 nm->wc_ip6_nd_publisher_node = node_index;
302 nm->wc_ip6_nd_publisher_et = event_type;
303}
304
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100305static int
306ra_publish (ra_report_t * r)
307{
308 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
309 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
310 return 0;
311}
312
313static void
314ra_signal_report (ra_report_t * r)
315{
316 vlib_main_t *vm = vlib_get_main ();
317 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
318 uword ni = nm->ip6_ra_publisher_node;
319 uword et = nm->ip6_ra_publisher_et;
320
321 if (ni == (uword) ~ 0)
322 return;
323 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
324
325 *q = *r;
326}
327
328void
329ra_set_publisher_node (uword node_index, uword event_type)
330{
331 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
332 nm->ip6_ra_publisher_node = node_index;
333 nm->ip6_ra_publisher_et = event_type;
334}
335
Dave Barachd7cb1b52016-12-09 09:52:16 -0500336static u8 *
337format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500339 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
340 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
341 vnet_main_t *vnm = vnet_get_main ();
342 vnet_sw_interface_t *si;
343 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345 if (!n)
shubing guo30746292018-08-10 15:51:44 +0800346 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500347 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100348
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100349 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100351
352 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Neale Rannsb3b2de72017-03-08 05:17:22 -0800355 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
356 flags = format (flags, "N");
357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
shubing guo30746292018-08-10 15:51:44 +0800359 s = format (s, "%=12U%=45U%=6s%=20U%=40U",
John Lo7f358b32018-04-28 01:19:24 -0400360 format_vlib_time, vm, n->time_last_updated,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500362 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 format_ethernet_address, n->link_layer_address,
364 format_vnet_sw_interface_name, vnm, si);
365
Dave Barachd7cb1b52016-12-09 09:52:16 -0500366 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 return s;
368}
369
Neale Ranns15002542017-09-10 04:39:11 -0700370static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700371ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700372{
373 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
374 {
Neale Ranns53da2212018-02-24 02:11:19 -0800375 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
376 {
377 ip6_ll_prefix_t pfx = {
378 .ilp_addr = n->key.ip6_address,
379 .ilp_sw_if_index = n->key.sw_if_index,
380 };
381 ip6_ll_table_entry_delete (&pfx);
382 }
383 else
384 {
385 fib_prefix_t pfx = {
386 .fp_len = 128,
387 .fp_proto = FIB_PROTOCOL_IP6,
388 .fp_addr.ip6 = n->key.ip6_address,
389 };
390 fib_table_entry_path_remove (fib_index,
391 &pfx,
392 FIB_SOURCE_ADJ,
393 DPO_PROTO_IP6,
394 &pfx.fp_addr,
395 n->key.sw_if_index, ~0,
396 1, FIB_ROUTE_PATH_FLAG_NONE);
397 }
Neale Ranns15002542017-09-10 04:39:11 -0700398 }
399}
400
Dave Barachd7cb1b52016-12-09 09:52:16 -0500401typedef struct
402{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100404 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800405 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 u8 link_layer_address[6];
407 u32 sw_if_index;
408 ip6_address_t addr;
409} ip6_neighbor_set_unset_rpc_args_t;
410
Dave Barachd7cb1b52016-12-09 09:52:16 -0500411static void ip6_neighbor_set_unset_rpc_callback
412 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413
Dave Barachd7cb1b52016-12-09 09:52:16 -0500414static void set_unset_ip6_neighbor_rpc
415 (vlib_main_t * vm,
416 u32 sw_if_index,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800417 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
418 int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419{
420 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500421 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 args.sw_if_index = sw_if_index;
424 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100425 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800426 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100427 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800428 if (NULL != link_layer_address)
429 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500432 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100435static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500436ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100437{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500438 icmp6_neighbor_solicitation_header_t *h;
439 vnet_main_t *vnm = vnet_get_main ();
440 ip6_main_t *im = &ip6_main;
441 ip_interface_address_t *ia;
442 ip6_address_t *dst, *src;
443 vnet_hw_interface_t *hi;
444 vnet_sw_interface_t *si;
445 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100446 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500447 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100448 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100449
Dave Barachd7cb1b52016-12-09 09:52:16 -0500450 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100451
Dave Barachd7cb1b52016-12-09 09:52:16 -0500452 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100453 dst = &adj->sub_type.nbr.next_hop.ip6;
454
455 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100456 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100457 return;
458 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500459 src = ip6_interface_address_matching_destination (im, dst,
460 adj->rewrite_header.
461 sw_if_index, &ia);
462 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100463 {
464 return;
465 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100466
Dave Barachd7cb1b52016-12-09 09:52:16 -0500467 h = vlib_packet_template_get_packet (vm,
468 &im->discover_neighbor_packet_template,
469 &bi);
John Lo084606b2018-06-19 15:27:48 -0400470 if (!h)
471 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100472
Dave Barachd7cb1b52016-12-09 09:52:16 -0500473 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100474
475 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
476 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
477 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
478 h->ip.src_address = src[0];
479 h->neighbor.target_address = dst[0];
480
481 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500482 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100483
484 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500485 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
486 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100487
488 b = vlib_get_buffer (vm, bi);
489 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500490 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100491
492 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500493 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
494 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100495
496 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500497 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
498 u32 *to_next = vlib_frame_vector_args (f);
499 to_next[0] = bi;
500 f->n_vectors = 1;
501 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100502 }
503}
504
505static void
506ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
507{
508 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
509 ethernet_build_rewrite (vnet_get_main (),
510 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500511 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100512 nbr->link_layer_address));
513}
514
515static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000516ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100517{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500518 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000519
Dave Barachd7cb1b52016-12-09 09:52:16 -0500520 adj_nbr_update_rewrite (ai,
521 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
522 ethernet_build_rewrite (vnet_get_main (),
523 adj->rewrite_header.
524 sw_if_index,
525 adj_get_link_type (ai),
526 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100527}
528
529#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
530{ \
531 k.sw_if_index = sw_if_index; \
532 k.ip6_address = *addr; \
533 k.pad = 0; \
534}
535
536static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500537ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100538{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500539 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
540 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100541 ip6_neighbor_key_t k;
542 uword *p;
543
Dave Barachd7cb1b52016-12-09 09:52:16 -0500544 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100545
546 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500547 if (p)
548 {
549 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
550 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100551
552 return (n);
553}
554
555static adj_walk_rc_t
556ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
557{
558 ip6_neighbor_t *nbr = ctx;
559
560 ip6_nd_mk_complete (ai, nbr);
561
562 return (ADJ_WALK_RC_CONTINUE);
563}
564
565static adj_walk_rc_t
566ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
567{
Neale Ranns19c68d22016-12-07 15:38:14 +0000568 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100569
570 return (ADJ_WALK_RC_CONTINUE);
571}
572
John Lo4fed5b12018-05-22 03:35:06 -0400573static clib_error_t *
574ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
575 u32 sw_if_index, u32 flags)
576{
577 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
578 ip6_neighbor_t *n;
579 u32 i, *to_delete = 0;
580
581 /* *INDENT-OFF* */
582 pool_foreach (n, nm->neighbor_pool,
583 ({
584 if (n->key.sw_if_index == sw_if_index)
585 vec_add1 (to_delete, n - nm->neighbor_pool);
586 }));
587 /* *INDENT-ON* */
588
589 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
590 {
591 for (i = 0; i < vec_len (to_delete); i++)
592 {
593 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
594 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
595 ip6_nd_mk_complete_walk, n);
596 }
597 }
598 else
599 {
600 for (i = 0; i < vec_len (to_delete); i++)
601 {
602 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
603 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
604 ip6_nd_mk_incomplete_walk, NULL);
605 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
606 continue;
607 ip6_neighbor_adj_fib_remove (n,
608 ip6_fib_table_get_index_for_sw_if_index
609 (n->key.sw_if_index));
610 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
611 pool_put (nm->neighbor_pool, n);
612 }
613 }
614
615 vec_free (to_delete);
616 return 0;
617}
618
619VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
620
Neale Rannsb80c5362016-10-08 13:03:40 +0100621void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500622ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100623{
624 ip6_neighbor_t *nbr;
625 ip_adjacency_t *adj;
626
627 adj = adj_get (ai);
628
629 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
630
Neale Ranns32e1c012016-11-22 17:07:28 +0000631 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100632 {
Ole Trøan438f6302018-02-15 21:56:06 +0000633 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800634 adj_glean_update_rewrite (ai);
635 break;
636 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000637 if (NULL != nbr)
638 {
639 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
640 ip6_nd_mk_complete_walk, nbr);
641 }
642 else
643 {
644 /*
645 * no matching ND entry.
646 * construct the rewrite required to for an ND packet, and stick
647 * that in the adj's pipe to smoke.
648 */
649 adj_nbr_update_rewrite (ai,
650 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
651 ethernet_build_rewrite (vnm,
652 sw_if_index,
653 VNET_LINK_IP6,
654 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
655
656 /*
657 * since the FIB has added this adj for a route, it makes sense it may
658 * want to forward traffic sometime soon. Let's send a speculative ND.
659 * just one. If we were to do periodically that wouldn't be bad either,
660 * but that's more code than i'm prepared to write at this time for
661 * relatively little reward.
662 */
663 ip6_nbr_probe (adj);
664 }
665 break;
Neale Ranns1855b8e2018-07-11 10:31:26 -0700666 case IP_LOOKUP_NEXT_BCAST:
667 adj_nbr_update_rewrite (ai,
668 ADJ_NBR_REWRITE_FLAG_COMPLETE,
669 ethernet_build_rewrite (vnm,
670 sw_if_index,
671 VNET_LINK_IP6,
672 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
673 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000674 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700675 {
676 /*
677 * Construct a partial rewrite from the known ethernet mcast dest MAC
678 */
679 u8 *rewrite;
680 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100681
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700682 rewrite = ethernet_build_rewrite (vnm,
683 sw_if_index,
684 adj->ia_link,
685 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000686
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700687 /*
688 * Complete the remaining fields of the adj's rewrite to direct the
689 * complete of the rewrite at switch time by copying in the IP
690 * dst address's bytes.
Neale Ranns889fe942017-06-01 05:43:19 -0400691 * Ofset is 2 bytes into the desintation address.
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700692 */
693 offset = vec_len (rewrite) - 2;
Neale Ranns889fe942017-06-01 05:43:19 -0400694 adj_mcast_update_rewrite (ai, rewrite, offset);
Neale Ranns32e1c012016-11-22 17:07:28 +0000695
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700696 break;
697 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000698 case IP_LOOKUP_NEXT_DROP:
699 case IP_LOOKUP_NEXT_PUNT:
700 case IP_LOOKUP_NEXT_LOCAL:
701 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800702 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000703 case IP_LOOKUP_NEXT_MIDCHAIN:
704 case IP_LOOKUP_NEXT_ICMP_ERROR:
705 case IP_LOOKUP_N_NEXT:
706 ASSERT (0);
707 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100708 }
709}
710
Neale Ranns15002542017-09-10 04:39:11 -0700711
712static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700713ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700714{
Neale Ranns53da2212018-02-24 02:11:19 -0800715 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
716 {
717 ip6_ll_prefix_t pfx = {
718 .ilp_addr = n->key.ip6_address,
719 .ilp_sw_if_index = n->key.sw_if_index,
720 };
721 n->fib_entry_index =
722 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
723 }
724 else
725 {
726 fib_prefix_t pfx = {
727 .fp_len = 128,
728 .fp_proto = FIB_PROTOCOL_IP6,
729 .fp_addr.ip6 = n->key.ip6_address,
730 };
Neale Ranns15002542017-09-10 04:39:11 -0700731
Neale Ranns53da2212018-02-24 02:11:19 -0800732 n->fib_entry_index =
733 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
734 FIB_ENTRY_FLAG_ATTACHED,
735 DPO_PROTO_IP6, &pfx.fp_addr,
736 n->key.sw_if_index, ~0, 1, NULL,
737 FIB_ROUTE_PATH_FLAG_NONE);
738 }
Neale Ranns15002542017-09-10 04:39:11 -0700739}
740
John Lo4fed5b12018-05-22 03:35:06 -0400741static ip6_neighbor_t *
742force_reuse_neighbor_entry (void)
743{
744 ip6_neighbor_t *n;
745 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
746 u32 count = 0;
747 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
748 if (index == ~0) /* Try again from elt 0 */
749 index = pool_next_index (nm->neighbor_pool, index);
750
751 /* Find a non-static random entry to free up for reuse */
752 do
753 {
754 if ((count++ == 100) || (index == ~0))
755 return NULL; /* give up after 100 entries */
756 n = pool_elt_at_index (nm->neighbor_pool, index);
757 nm->neighbor_delete_rotor = index;
758 index = pool_next_index (nm->neighbor_pool, index);
759 }
760 while (n->flags & IP6_NEIGHBOR_FLAG_STATIC);
761
762 /* Remove ARP entry from its interface and update fib */
763 adj_nbr_walk_nh6 (n->key.sw_if_index,
764 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
765 ip6_neighbor_adj_fib_remove
766 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
767 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
768
769 return n;
770}
771
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772int
773vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500774 u32 sw_if_index,
775 ip6_address_t * a,
776 u8 * link_layer_address,
777 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800778 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500780 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500782 ip6_neighbor_t *n = 0;
783 int make_new_nd_cache_entry = 1;
784 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500786 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
Damjan Marion586afd72017-04-05 19:18:20 +0200788 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 {
790 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800791 1 /* set new neighbor */ , is_static,
792 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 return 0;
794 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
796 k.sw_if_index = sw_if_index;
797 k.ip6_address = a[0];
798 k.pad = 0;
799
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500801 if (p)
802 {
803 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
804 /* Refuse to over-write static neighbor entry. */
805 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
John Lo0e6f4d62018-07-13 17:29:58 -0400806 {
807 /* if MAC address match, still check to send event */
808 if (0 == memcmp (n->link_layer_address,
809 link_layer_address, n_bytes_link_layer_address))
810 goto check_customers;
811 return -2;
812 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500813 make_new_nd_cache_entry = 0;
814 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100815
Dave Barachd7cb1b52016-12-09 09:52:16 -0500816 if (make_new_nd_cache_entry)
817 {
John Lo4fed5b12018-05-22 03:35:06 -0400818 if (nm->limit_neighbor_cache_size &&
819 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
820 {
821 n = force_reuse_neighbor_entry ();
822 if (NULL == n)
823 return -2;
824 }
825 else
826 pool_get (nm->neighbor_pool, n);
827
Dave Barachd7cb1b52016-12-09 09:52:16 -0500828 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
829 /* old value */ 0);
830 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700831 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100832
Dave Barachd7cb1b52016-12-09 09:52:16 -0500833 clib_memcpy (n->link_layer_address,
834 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100835
Dave Barachd7cb1b52016-12-09 09:52:16 -0500836 /*
837 * create the adj-fib. the entry in the FIB table for and to the peer.
838 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800839 if (!is_no_fib_entry)
840 {
Neale Ranns53da2212018-02-24 02:11:19 -0800841 ip6_neighbor_adj_fib_add
842 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700843 }
844 else
845 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800846 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
847 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500848 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100849 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500850 {
851 /*
852 * prevent a DoS attack from the data-plane that
853 * spams us with no-op updates to the MAC address
854 */
855 if (0 == memcmp (n->link_layer_address,
856 link_layer_address, n_bytes_link_layer_address))
John Lo7f358b32018-04-28 01:19:24 -0400857 {
858 n->time_last_updated = vlib_time_now (vm);
859 goto check_customers;
860 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100861
Dave Barachd7cb1b52016-12-09 09:52:16 -0500862 clib_memcpy (n->link_layer_address,
863 link_layer_address, n_bytes_link_layer_address);
864 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Neale Rannsb80c5362016-10-08 13:03:40 +0100866 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400867 n->time_last_updated = vlib_time_now (vm);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100868 if (is_static)
John Lo4fed5b12018-05-22 03:35:06 -0400869 {
870 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
871 n->flags &= ~IP6_NEIGHBOR_FLAG_DYNAMIC;
872 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100873 else
John Lo4fed5b12018-05-22 03:35:06 -0400874 {
875 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
876 n->flags &= ~IP6_NEIGHBOR_FLAG_STATIC;
877 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100878
Neale Rannsb80c5362016-10-08 13:03:40 +0100879 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500880 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881
Dave Barach59b25652017-09-10 15:04:27 -0400882check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883 /* Customer(s) waiting for this address to be resolved? */
884 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400885 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 {
John Lo1edfba92016-08-27 01:11:57 -0400887 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500888
889 while (next_index != (u32) ~ 0)
890 {
John Lo1edfba92016-08-27 01:11:57 -0400891 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
892 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500893 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400894 next_index = pr->next_index;
895 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500896 }
John Lo1edfba92016-08-27 01:11:57 -0400897
898 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 }
900
John Lo1edfba92016-08-27 01:11:57 -0400901 /* Customer(s) requesting ND event for this address? */
902 p = mhash_get (&nm->mac_changes_by_address, a);
903 if (p)
904 {
905 next_index = p[0];
906
Dave Barachd7cb1b52016-12-09 09:52:16 -0500907 while (next_index != (u32) ~ 0)
908 {
909 int (*fp) (u32, u8 *, u32, ip6_address_t *);
910 int rv = 1;
911 mc = pool_elt_at_index (nm->mac_changes, next_index);
912 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400913
Dave Barachd7cb1b52016-12-09 09:52:16 -0500914 /* Call the user's data callback, return 1 to suppress dup events */
915 if (fp)
916 rv =
917 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
918 /*
919 * Signal the resolver process, as long as the user
920 * says they want to be notified
921 */
922 if (rv == 0)
923 vlib_process_signal_event (vm, mc->node_index,
924 mc->type_opaque, mc->data);
925 next_index = mc->next_index;
926 }
John Lo1edfba92016-08-27 01:11:57 -0400927 }
928
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929 return 0;
930}
931
932int
933vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500934 u32 sw_if_index,
935 ip6_address_t * a,
936 u8 * link_layer_address,
937 uword n_bytes_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500939 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500941 ip6_neighbor_t *n;
942 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 int rv = 0;
944
Damjan Marion586afd72017-04-05 19:18:20 +0200945 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 {
947 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800948 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949 return 0;
950 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
952 k.sw_if_index = sw_if_index;
953 k.ip6_address = a[0];
954 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500955
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956 p = mhash_get (&nm->neighbor_index_by_key, &k);
957 if (p == 0)
958 {
959 rv = -1;
960 goto out;
961 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500962
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100964
Neale Rannsb80c5362016-10-08 13:03:40 +0100965 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500966 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400967 ip6_neighbor_adj_fib_remove
968 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100969
John Lo4fed5b12018-05-22 03:35:06 -0400970 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500972
973out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974 return rv;
975}
976
Dave Barachd7cb1b52016-12-09 09:52:16 -0500977static void ip6_neighbor_set_unset_rpc_callback
978 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500980 vlib_main_t *vm = vlib_get_main ();
981 if (a->is_add)
982 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800983 a->link_layer_address, 6, a->is_static,
984 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500986 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
987 a->link_layer_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989
990static int
991ip6_neighbor_sort (void *a1, void *a2)
992{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500993 vnet_main_t *vnm = vnet_get_main ();
994 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500996 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
997 n2->key.sw_if_index);
998 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
1000 return cmp;
1001}
1002
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001003ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -04001004ip6_neighbors_pool (void)
1005{
1006 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1007 return nm->neighbor_pool;
1008}
1009
1010ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001011ip6_neighbors_entries (u32 sw_if_index)
1012{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001013 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001014 ip6_neighbor_t *n, *ns = 0;
1015
1016 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001017 pool_foreach (n, nm->neighbor_pool,
1018 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001019 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1020 continue;
1021 vec_add1 (ns, n[0]);
1022 }));
1023 /* *INDENT-ON* */
1024
1025 if (ns)
1026 vec_sort_with_function (ns, ip6_neighbor_sort);
1027 return ns;
1028}
1029
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030static clib_error_t *
1031show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001032 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001034 vnet_main_t *vnm = vnet_get_main ();
1035 ip6_neighbor_t *n, *ns;
1036 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 u32 sw_if_index;
1038
1039 /* Filter entries by interface if given. */
1040 sw_if_index = ~0;
1041 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1042
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001043 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001044 if (ns)
1045 {
Billy McFall46529cd2016-10-14 10:45:49 -04001046 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001047 vec_foreach (n, ns)
1048 {
1049 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -04001050 }
1051 vec_free (ns);
1052 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053
1054 return error;
1055}
1056
Billy McFall0683c9c2016-10-13 08:27:31 -04001057/*?
1058 * This command is used to display the adjacent IPv6 hosts found via
1059 * neighbor discovery. Optionally, limit the output to the specified
1060 * interface.
1061 *
1062 * @cliexpar
1063 * Example of how to display the IPv6 neighbor adjacency table:
1064 * @cliexstart{show ip6 neighbors}
1065 * Time Address Flags Link layer Interface
1066 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1067 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1068 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1069 * @cliexend
1070 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1071 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1072 * Time Address Flags Link layer Interface
1073 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1074 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1075 * @cliexend
1076?*/
1077/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1079 .path = "show ip6 neighbors",
1080 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001081 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082};
Billy McFall0683c9c2016-10-13 08:27:31 -04001083/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
1085static clib_error_t *
1086set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001087 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001089 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090 ip6_address_t addr;
1091 u8 mac_address[6];
1092 int addr_valid = 0;
1093 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001094 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001095 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096 u32 sw_if_index;
1097
Dave Barachd7cb1b52016-12-09 09:52:16 -05001098 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 {
1100 /* intfc, ip6-address, mac-address */
1101 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001102 unformat_vnet_sw_interface, vnm, &sw_if_index,
1103 unformat_ip6_address, &addr,
1104 unformat_ethernet_address, mac_address))
1105 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106
1107 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001108 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001109 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001110 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001111 else if (unformat (input, "no-fib-entry"))
1112 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001114 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115 }
1116
1117 if (!addr_valid)
1118 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001119
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 if (!is_del)
1121 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001122 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001123 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124 else
1125 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001126 mac_address, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 return 0;
1128}
1129
Billy McFall0683c9c2016-10-13 08:27:31 -04001130/*?
1131 * This command is used to manually add an entry to the IPv6 neighbor
1132 * adjacency table. Optionally, the entry can be added as static. It is
1133 * also used to remove an entry from the table. Use the '<em>show ip6
1134 * neighbors</em>' command to display all learned and manually entered entries.
1135 *
1136 * @cliexpar
1137 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1138 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1139 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1140 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1141?*/
1142/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001143VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1144{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145 .path = "set ip6 neighbor",
1146 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001147 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148};
Billy McFall0683c9c2016-10-13 08:27:31 -04001149/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150
Dave Barachd7cb1b52016-12-09 09:52:16 -05001151typedef enum
1152{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1154 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1155 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1156} icmp6_neighbor_solicitation_or_advertisement_next_t;
1157
1158static_always_inline uword
1159icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1160 vlib_node_runtime_t * node,
1161 vlib_frame_t * frame,
1162 uword is_solicitation)
1163{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001164 vnet_main_t *vnm = vnet_get_main ();
1165 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001167 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1169 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001170 vlib_node_runtime_t *error_node =
1171 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001172 int bogus_length;
1173
1174 from = vlib_frame_vector_args (frame);
1175 n_left_from = n_packets;
1176 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001177
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 if (node->flags & VLIB_NODE_FLAG_TRACE)
1179 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1180 /* stride */ 1,
1181 sizeof (icmp6_input_trace_t));
1182
Dave Barachd7cb1b52016-12-09 09:52:16 -05001183 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 (is_solicitation
1185 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1186 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1187 n_advertisements_sent = 0;
1188
1189 while (n_left_from > 0)
1190 {
1191 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1192
1193 while (n_left_from > 0 && n_left_to_next > 0)
1194 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001195 vlib_buffer_t *p0;
1196 ip6_header_t *ip0;
1197 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1198 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001200 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1201 int is_rewrite0;
1202 u32 ni0;
1203
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204 bi0 = to_next[0] = from[0];
1205
1206 from += 1;
1207 to_next += 1;
1208 n_left_from -= 1;
1209 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001210
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 p0 = vlib_get_buffer (vm, bi0);
1212 ip0 = vlib_buffer_get_current (p0);
1213 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001214 options_len0 =
1215 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216
1217 error0 = ICMP6_ERROR_NONE;
1218 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001219 ip6_sadd_link_local =
1220 ip6_address_is_link_local_unicast (&ip0->src_address);
1221 ip6_sadd_unspecified =
1222 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223
1224 /* Check that source address is unspecified, link-local or else on-link. */
1225 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1226 {
1227 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001228
Dave Barachd7cb1b52016-12-09 09:52:16 -05001229 if (ADJ_INDEX_INVALID != src_adj_index0)
1230 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001231 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232
Dave Barachd7cb1b52016-12-09 09:52:16 -05001233 /* Allow all realistic-looking rewrite adjacencies to pass */
1234 ni0 = adj0->lookup_next_index;
1235 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1236 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001237
Dave Barachd7cb1b52016-12-09 09:52:16 -05001238 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1239 || !is_rewrite0)
1240 ?
1241 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1242 : error0);
1243 }
1244 else
1245 {
1246 error0 =
1247 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1248 }
1249 }
1250
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251 o0 = (void *) (h0 + 1);
1252 o0 = ((options_len0 == 8 && o0->header.type == option_type
1253 && o0->header.n_data_u64s == 1) ? o0 : 0);
1254
1255 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001257 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001258 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001259 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1260 is_solicitation ?
1261 &ip0->src_address :
1262 &h0->target_address,
1263 o0->ethernet_address,
1264 sizeof (o0->ethernet_address),
Neale Ranns53da2212018-02-24 02:11:19 -08001265 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001266 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267
1268 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1269 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001270 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001271 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001272 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273
Dave Barachd7cb1b52016-12-09 09:52:16 -05001274 fib_index =
1275 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001277 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001278 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001279 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1280 }
1281 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001282 {
Neale Ranns53da2212018-02-24 02:11:19 -08001283 if (ip6_address_is_link_local_unicast (&h0->target_address))
1284 {
1285 fei = ip6_fib_table_lookup_exact_match
1286 (ip6_ll_fib_get (sw_if_index0),
1287 &h0->target_address, 128);
1288 }
1289 else
1290 {
1291 fei = ip6_fib_table_lookup_exact_match (fib_index,
1292 &h0->target_address,
1293 128);
1294 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001295
Neale Ranns3f844d02017-02-18 00:03:54 -08001296 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001297 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001298 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001299 error0 =
1300 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001301 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001302 else
1303 {
1304 if (FIB_ENTRY_FLAG_LOCAL &
1305 fib_entry_get_flags_for_source (fei,
1306 FIB_SOURCE_INTERFACE))
1307 {
1308 /* It's an address that belongs to one of our interfaces
1309 * that's good. */
1310 }
1311 else
1312 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001313 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1314 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001315 {
1316 /* The address was added by IPv6 Proxy ND config.
1317 * We should only respond to these if the NS arrived on
1318 * the link that has a matching covering prefix */
1319 }
1320 else
1321 {
1322 error0 =
1323 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1324 }
1325 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001326 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327 }
1328
1329 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001330 next0 = (error0 != ICMP6_ERROR_NONE
1331 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1332 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333 else
1334 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001335 next0 = 0;
1336 error0 = error0 == ICMP6_ERROR_NONE ?
1337 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338 }
1339
1340 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1341 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001342 vnet_sw_interface_t *sw_if0;
1343 ethernet_interface_t *eth_if0;
1344 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345
Dave Barachd7cb1b52016-12-09 09:52:16 -05001346 /* dst address is either source address or the all-nodes mcast addr */
1347 if (!ip6_sadd_unspecified)
1348 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001350 ip6_set_reserved_multicast_address (&ip0->dst_address,
1351 IP6_MULTICAST_SCOPE_link_local,
1352 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001353
1354 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001355 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001356 h0->icmp.type = ICMP6_neighbor_advertisement;
1357
1358 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1359 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001360 eth_if0 =
1361 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001362 if (eth_if0 && o0)
1363 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001364 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1365 o0->header.type =
1366 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001367 }
1368
1369 h0->advertisement_flags = clib_host_to_net_u32
1370 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1371 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1372
1373 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001374 h0->icmp.checksum =
1375 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1376 &bogus_length);
1377 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001378
Dave Barachd7cb1b52016-12-09 09:52:16 -05001379 /* Reuse current MAC header, copy SMAC to DMAC and
1380 * interface MAC to SMAC */
1381 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1382 eth0 = vlib_buffer_get_current (p0);
1383 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1384 if (eth_if0)
1385 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001386
Dave Barachd7cb1b52016-12-09 09:52:16 -05001387 /* Setup input and output sw_if_index for packet */
1388 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1389 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1390 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1391 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392
1393 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001394 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001395
1396 p0->error = error_node->errors[error0];
1397
1398 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1399 to_next, n_left_to_next,
1400 bi0, next0);
1401 }
1402
1403 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1404 }
1405
1406 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001407 vlib_error_count (vm, error_node->node_index,
1408 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1409 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410
1411 return frame->n_vectors;
1412}
1413
1414/* for "syslogging" - use elog for now */
1415#define foreach_log_level \
1416 _ (DEBUG, "DEBUG") \
1417 _ (INFO, "INFORMATION") \
1418 _ (NOTICE, "NOTICE") \
1419 _ (WARNING, "WARNING") \
1420 _ (ERR, "ERROR") \
1421 _ (CRIT, "CRITICAL") \
1422 _ (ALERT, "ALERT") \
1423 _ (EMERG, "EMERGENCY")
1424
Dave Barachd7cb1b52016-12-09 09:52:16 -05001425typedef enum
1426{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001427#define _(f,s) LOG_##f,
1428 foreach_log_level
1429#undef _
1430} log_level_t;
1431
Dave Barachd7cb1b52016-12-09 09:52:16 -05001432static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001433#define _(f,s) s,
1434 foreach_log_level
1435#undef _
1436};
1437
Dave Barachd7cb1b52016-12-09 09:52:16 -05001438static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439
1440static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001441ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442{
1443 /* just use elog for now */
1444 u8 *what;
1445 va_list va;
1446
Dave Barachd7cb1b52016-12-09 09:52:16 -05001447 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1448 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001449
1450 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001451 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452 {
1453 what = va_format (0, fmt, &va);
1454
Dave Barachd7cb1b52016-12-09 09:52:16 -05001455 ELOG_TYPE_DECLARE (e) =
1456 {
1457 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1458 struct
1459 {
1460 u32 s[2];
1461 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001463 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1464 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465 }
1466 va_end (va);
1467 return;
1468}
1469
Juraj Sloboda52574522018-05-03 10:03:50 +02001470clib_error_t *
1471call_ip6_neighbor_callbacks (void *data,
1472 _vnet_ip6_neighbor_function_list_elt_t * elt)
1473{
1474 clib_error_t *error = 0;
1475
1476 while (elt)
1477 {
1478 error = elt->fp (data);
1479 if (error)
1480 return error;
1481 elt = elt->next_ip6_neighbor_function;
1482 }
1483
1484 return error;
1485}
1486
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001488typedef enum
1489{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001490 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1491 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1492 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1493 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1494} icmp6_router_solicitation_or_advertisement_next_t;
1495
1496static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001497icmp6_router_solicitation (vlib_main_t * vm,
1498 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001499{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001500 vnet_main_t *vnm = vnet_get_main ();
1501 ip6_main_t *im = &ip6_main;
1502 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001504 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001506 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507 int bogus_length;
1508
1509 icmp6_neighbor_discovery_option_type_t option_type;
1510
Dave Barachd7cb1b52016-12-09 09:52:16 -05001511 vlib_node_runtime_t *error_node =
1512 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513
1514 from = vlib_frame_vector_args (frame);
1515 n_left_from = n_packets;
1516 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001517
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518 if (node->flags & VLIB_NODE_FLAG_TRACE)
1519 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1520 /* stride */ 1,
1521 sizeof (icmp6_input_trace_t));
1522
1523 /* source may append his LL address */
1524 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1525
1526 while (n_left_from > 0)
1527 {
1528 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001529
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530 while (n_left_from > 0 && n_left_to_next > 0)
1531 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001532 vlib_buffer_t *p0;
1533 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001534 ip6_radv_t *radv_info = 0;
1535
Dave Barachd7cb1b52016-12-09 09:52:16 -05001536 icmp6_neighbor_discovery_header_t *h0;
1537 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1538
Ed Warnickecb9cada2015-12-08 15:45:58 -07001539 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001540 u32 is_solicitation = 1, is_dropped = 0;
1541 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001542
1543 bi0 = to_next[0] = from[0];
1544
1545 from += 1;
1546 to_next += 1;
1547 n_left_from -= 1;
1548 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001549
Ed Warnickecb9cada2015-12-08 15:45:58 -07001550 p0 = vlib_get_buffer (vm, bi0);
1551 ip0 = vlib_buffer_get_current (p0);
1552 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001553 options_len0 =
1554 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1555 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1556 is_link_local =
1557 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558
1559 error0 = ICMP6_ERROR_NONE;
1560 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001561
Ed Warnickecb9cada2015-12-08 15:45:58 -07001562 /* check if solicitation (not from nd_timer node) */
1563 if (ip6_address_is_unspecified (&ip0->dst_address))
1564 is_solicitation = 0;
1565
1566 /* Check that source address is unspecified, link-local or else on-link. */
1567 if (!is_unspecified && !is_link_local)
1568 {
1569 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001570
Dave Barachd7cb1b52016-12-09 09:52:16 -05001571 if (ADJ_INDEX_INVALID != src_adj_index0)
1572 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001573 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001574
Dave Barachd7cb1b52016-12-09 09:52:16 -05001575 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1576 ?
1577 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1578 : error0);
1579 }
1580 else
1581 {
1582 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1583 }
1584 }
1585
Ed Warnickecb9cada2015-12-08 15:45:58 -07001586 /* check for source LL option and process */
1587 o0 = (void *) (h0 + 1);
1588 o0 = ((options_len0 == 8
1589 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001590 && o0->header.n_data_u64s == 1) ? o0 : 0);
1591
Ed Warnickecb9cada2015-12-08 15:45:58 -07001592 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001593 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1594 !is_unspecified && !is_link_local))
1595 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001596 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1597 &ip0->src_address,
1598 o0->ethernet_address,
1599 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001600 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001601 }
1602
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603 /* default is to drop */
1604 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001605
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606 if (error0 == ICMP6_ERROR_NONE)
1607 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001608 vnet_sw_interface_t *sw_if0;
1609 ethernet_interface_t *eth_if0;
1610 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001611
1612 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1613 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001614 eth_if0 =
1615 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001616
1617 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001618 error0 =
1619 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1620 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001621
1622 if (error0 == ICMP6_ERROR_NONE)
1623 {
1624 u32 ri;
1625
1626 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001627 p0->current_length -=
1628 (options_len0 +
1629 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001630
1631 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001632 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1633 sw_if_index0)
1634 {
1635 ri =
1636 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001637
Neale Ranns8e254952018-04-25 05:40:48 -07001638 if (ri != ~0)
1639 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1640 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001641
1642 error0 =
1643 ((!radv_info) ?
1644 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1645 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646
1647 if (error0 == ICMP6_ERROR_NONE)
1648 {
1649 f64 now = vlib_time_now (vm);
1650
1651 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001652 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001653 {
Neale Ranns75152282017-01-09 01:00:45 -08001654 if (0 != radv_info->last_radv_time &&
1655 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001656 MIN_DELAY_BETWEEN_RAS)
1657 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001658 else
1659 radv_info->last_radv_time = now;
1660 }
1661
1662 /* send now */
1663 icmp6_router_advertisement_header_t rh;
1664
1665 rh.icmp.type = ICMP6_router_advertisement;
1666 rh.icmp.code = 0;
1667 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001668
Ed Warnickecb9cada2015-12-08 15:45:58 -07001669 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001670 rh.router_lifetime_in_sec =
1671 clib_host_to_net_u16
1672 (radv_info->adv_router_lifetime_in_sec);
1673 rh.
1674 time_in_msec_between_retransmitted_neighbor_solicitations
1675 =
1676 clib_host_to_net_u32 (radv_info->
1677 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1678 rh.neighbor_reachable_time_in_msec =
1679 clib_host_to_net_u32 (radv_info->
1680 adv_neighbor_reachable_time_in_msec);
1681
1682 rh.flags =
1683 (radv_info->adv_managed_flag) ?
1684 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1685 0;
1686 rh.flags |=
1687 ((radv_info->adv_other_flag) ?
1688 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1689 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690
1691
Dave Barachd7cb1b52016-12-09 09:52:16 -05001692 u16 payload_length =
1693 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001694
1695 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001696 vlib_buffer_get_free_list_index
1697 (p0), bi0, (void *) &rh,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001698 sizeof
1699 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001700
Dave Barachd7cb1b52016-12-09 09:52:16 -05001701 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001702 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001703 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1704 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001705
Dave Barachd7cb1b52016-12-09 09:52:16 -05001706 h.header.type =
1707 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001708 h.header.n_data_u64s = 1;
1709
1710 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001711 clib_memcpy (&h.ethernet_address[0],
1712 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001713
1714 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001715 vlib_buffer_get_free_list_index
1716 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001717 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 payload_length +=
1721 sizeof
1722 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001724
Ed Warnickecb9cada2015-12-08 15:45:58 -07001725 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001726 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001727 {
1728 icmp6_neighbor_discovery_mtu_option_t h;
1729
1730 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001731 h.mtu =
1732 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001733 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1734 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001735
1736 payload_length +=
1737 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001738
1739 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001740 vlib_buffer_get_free_list_index
1741 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001742 sizeof
1743 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001744 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001745
Ed Warnickecb9cada2015-12-08 15:45:58 -07001746 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001747 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001748
Dave Barachd7cb1b52016-12-09 09:52:16 -05001749 /* *INDENT-OFF* */
1750 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1751 ({
1752 if(pr_info->enabled &&
1753 (!pr_info->decrement_lifetime_flag
1754 || (pr_info->pref_lifetime_expires >0)))
1755 {
1756 /* advertise this prefix */
1757 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001758
Dave Barachd7cb1b52016-12-09 09:52:16 -05001759 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1760 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001761
Dave Barachd7cb1b52016-12-09 09:52:16 -05001762 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763
Dave Barachd7cb1b52016-12-09 09:52:16 -05001764 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1765 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766
Dave Barachd7cb1b52016-12-09 09:52:16 -05001767 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1768 {
1769 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1770 h.preferred_time = 0;
1771 }
1772 else
1773 {
1774 if(pr_info->decrement_lifetime_flag)
1775 {
1776 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1777 (pr_info->valid_lifetime_expires - now) : 0;
1778
1779 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1780 (pr_info->pref_lifetime_expires - now) : 0;
1781 }
1782
1783 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1784 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1785 }
1786 h.unused = 0;
1787
1788 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1789
1790 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1791
1792 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001793 vlib_buffer_get_free_list_index (p0),
Dave Barachd7cb1b52016-12-09 09:52:16 -05001794 bi0,
1795 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1796
1797 }
1798 }));
1799 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001800
1801 /* add additional options before here */
1802
1803 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001804 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001805 {
1806 ip0->dst_address = ip0->src_address;
1807 }
1808 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001809 {
1810 /* target address is all-nodes mcast addr */
1811 ip6_set_reserved_multicast_address
1812 (&ip0->dst_address,
1813 IP6_MULTICAST_SCOPE_link_local,
1814 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001815 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001816
Ed Warnickecb9cada2015-12-08 15:45:58 -07001817 /* source address MUST be the link-local address */
1818 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001819
Dave Barachd7cb1b52016-12-09 09:52:16 -05001820 ip0->hop_limit = 255;
1821 ip0->payload_length =
1822 clib_host_to_net_u16 (payload_length);
1823
1824 icmp6_router_advertisement_header_t *rh0 =
1825 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1826 rh0->icmp.checksum =
1827 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1828 &bogus_length);
1829 ASSERT (bogus_length == 0);
1830
Ed Warnickecb9cada2015-12-08 15:45:58 -07001831 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001832 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001834
1835 if (is_solicitation)
1836 {
1837 ethernet_header_t *eth0;
1838 /* Reuse current MAC header, copy SMAC to DMAC and
1839 * interface MAC to SMAC */
1840 vlib_buffer_reset (p0);
1841 eth0 = vlib_buffer_get_current (p0);
1842 clib_memcpy (eth0->dst_address, eth0->src_address,
1843 6);
1844 clib_memcpy (eth0->src_address, eth_if0->address,
1845 6);
1846 next0 =
1847 is_dropped ? next0 :
1848 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1849 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1850 sw_if_index0;
1851 }
1852 else
1853 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001854 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001855 if (adj_index0 == 0)
1856 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1857 else
1858 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001859 next0 =
1860 is_dropped ? next0 :
1861 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1862 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1863 adj_index0;
1864 }
1865 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001866 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001867
1868 radv_info->n_solicitations_dropped += is_dropped;
1869 radv_info->n_solicitations_rcvd += is_solicitation;
1870
1871 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872 {
1873 radv_info->n_advertisements_sent++;
1874 n_advertisements_sent++;
1875 }
1876 }
1877 }
1878 }
1879
1880 p0->error = error_node->errors[error0];
1881
Dave Barachd7cb1b52016-12-09 09:52:16 -05001882 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001883 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001884
Ed Warnickecb9cada2015-12-08 15:45:58 -07001885 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1886 to_next, n_left_to_next,
1887 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001888
Ed Warnickecb9cada2015-12-08 15:45:58 -07001889 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001890
Ed Warnickecb9cada2015-12-08 15:45:58 -07001891 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1892 }
1893
1894 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001895 vlib_error_count (vm, error_node->node_index,
1896 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1897 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001898
1899 return frame->n_vectors;
1900}
1901
1902 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1903static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001904icmp6_router_advertisement (vlib_main_t * vm,
1905 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001907 vnet_main_t *vnm = vnet_get_main ();
1908 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001909 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001910 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001911 u32 n_left_from, n_left_to_next, next_index;
1912 u32 n_advertisements_rcvd = 0;
1913
Dave Barachd7cb1b52016-12-09 09:52:16 -05001914 vlib_node_runtime_t *error_node =
1915 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001916
1917 from = vlib_frame_vector_args (frame);
1918 n_left_from = n_packets;
1919 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001920
Ed Warnickecb9cada2015-12-08 15:45:58 -07001921 if (node->flags & VLIB_NODE_FLAG_TRACE)
1922 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1923 /* stride */ 1,
1924 sizeof (icmp6_input_trace_t));
1925
1926 while (n_left_from > 0)
1927 {
1928 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001929
Ed Warnickecb9cada2015-12-08 15:45:58 -07001930 while (n_left_from > 0 && n_left_to_next > 0)
1931 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001932 vlib_buffer_t *p0;
1933 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001934 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001935 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001936 u32 bi0, options_len0, sw_if_index0, next0, error0;
1937
1938 bi0 = to_next[0] = from[0];
1939
1940 from += 1;
1941 to_next += 1;
1942 n_left_from -= 1;
1943 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001944
Ed Warnickecb9cada2015-12-08 15:45:58 -07001945 p0 = vlib_get_buffer (vm, bi0);
1946 ip0 = vlib_buffer_get_current (p0);
1947 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001948 options_len0 =
1949 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001950
1951 error0 = ICMP6_ERROR_NONE;
1952 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1953
Dave Barachd7cb1b52016-12-09 09:52:16 -05001954 /* Check that source address is link-local */
1955 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001956 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1957
1958 /* default is to drop */
1959 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001960
Ed Warnickecb9cada2015-12-08 15:45:58 -07001961 n_advertisements_rcvd++;
1962
1963 if (error0 == ICMP6_ERROR_NONE)
1964 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001965 vnet_sw_interface_t *sw_if0;
1966 ethernet_interface_t *eth_if0;
1967
Ed Warnickecb9cada2015-12-08 15:45:58 -07001968 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1969 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001970 eth_if0 =
1971 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972
1973 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001974 error0 =
1975 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1976 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001977
1978 if (error0 == ICMP6_ERROR_NONE)
1979 {
1980 u32 ri;
1981
1982 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001983 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1984 sw_if_index0)
1985 {
1986 ri =
1987 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001988
Neale Ranns8e254952018-04-25 05:40:48 -07001989 if (ri != ~0)
1990 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1991 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001992
1993 error0 =
1994 ((!radv_info) ?
1995 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1996 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001997
1998 if (error0 == ICMP6_ERROR_NONE)
1999 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002000 radv_info->keep_sending_rs = 0;
2001
2002 ra_report_t r;
2003
2004 r.sw_if_index = sw_if_index0;
2005 memcpy (r.router_address, &ip0->src_address, 16);
2006 r.current_hop_limit = h0->current_hop_limit;
2007 r.flags = h0->flags;
2008 r.router_lifetime_in_sec =
2009 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2010 r.neighbor_reachable_time_in_msec =
2011 clib_net_to_host_u32
2012 (h0->neighbor_reachable_time_in_msec);
2013 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2014 r.prefixes = 0;
2015
Ed Warnickecb9cada2015-12-08 15:45:58 -07002016 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002017 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2018 && (h0->current_hop_limit !=
2019 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002020 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002021 ip6_neighbor_syslog (vm, LOG_WARNING,
2022 "our AdvCurHopLimit on %U doesn't agree with %U",
2023 format_vnet_sw_if_index_name,
2024 vnm, sw_if_index0,
2025 format_ip6_address,
2026 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002027 }
2028
Dave Barachd7cb1b52016-12-09 09:52:16 -05002029 if ((h0->flags &
2030 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2031 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002032 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002033 ip6_neighbor_syslog (vm, LOG_WARNING,
2034 "our AdvManagedFlag on %U doesn't agree with %U",
2035 format_vnet_sw_if_index_name,
2036 vnm, sw_if_index0,
2037 format_ip6_address,
2038 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002039 }
2040
Dave Barachd7cb1b52016-12-09 09:52:16 -05002041 if ((h0->flags &
2042 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2043 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002044 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002045 ip6_neighbor_syslog (vm, LOG_WARNING,
2046 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2047 format_vnet_sw_if_index_name,
2048 vnm, sw_if_index0,
2049 format_ip6_address,
2050 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002051 }
2052
Dave Barachd7cb1b52016-12-09 09:52:16 -05002053 if ((h0->
2054 time_in_msec_between_retransmitted_neighbor_solicitations
2055 && radv_info->
2056 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2057 && (h0->
2058 time_in_msec_between_retransmitted_neighbor_solicitations
2059 !=
2060 clib_host_to_net_u32 (radv_info->
2061 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002062 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002063 ip6_neighbor_syslog (vm, LOG_WARNING,
2064 "our AdvRetransTimer on %U doesn't agree with %U",
2065 format_vnet_sw_if_index_name,
2066 vnm, sw_if_index0,
2067 format_ip6_address,
2068 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002069 }
2070
Dave Barachd7cb1b52016-12-09 09:52:16 -05002071 if ((h0->neighbor_reachable_time_in_msec &&
2072 radv_info->adv_neighbor_reachable_time_in_msec) &&
2073 (h0->neighbor_reachable_time_in_msec !=
2074 clib_host_to_net_u32
2075 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002076 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002077 ip6_neighbor_syslog (vm, LOG_WARNING,
2078 "our AdvReachableTime on %U doesn't agree with %U",
2079 format_vnet_sw_if_index_name,
2080 vnm, sw_if_index0,
2081 format_ip6_address,
2082 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002083 }
2084
2085 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002086 u8 *opt_hdr = (u8 *) (h0 + 1);
2087 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002088 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002089 icmp6_neighbor_discovery_option_header_t *o0 =
2090 (icmp6_neighbor_discovery_option_header_t *)
2091 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002092 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002093 icmp6_neighbor_discovery_option_type_t option_type =
2094 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002095
Dave Barachd7cb1b52016-12-09 09:52:16 -05002096 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002097 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002098 ip6_neighbor_syslog (vm, LOG_ERR,
2099 "malformed RA packet on %U from %U",
2100 format_vnet_sw_if_index_name,
2101 vnm, sw_if_index0,
2102 format_ip6_address,
2103 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002104 break;
2105 }
2106
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002108 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002109 ip6_neighbor_syslog (vm, LOG_ERR,
2110 " zero length option in RA on %U from %U",
2111 format_vnet_sw_if_index_name,
2112 vnm, sw_if_index0,
2113 format_ip6_address,
2114 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115 break;
2116 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002117 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002118 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002119 ip6_neighbor_syslog (vm, LOG_ERR,
2120 "option length in RA packet greater than total length on %U from %U",
2121 format_vnet_sw_if_index_name,
2122 vnm, sw_if_index0,
2123 format_ip6_address,
2124 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002125 break;
2126 }
2127
2128 options_len0 -= opt_len;
2129 opt_hdr += opt_len;
2130
Dave Barachd7cb1b52016-12-09 09:52:16 -05002131 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002132 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002133 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2134 {
2135 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2136 * h =
2137 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2138 *) (o0);
2139
2140 if (opt_len < sizeof (*h))
2141 break;
2142
2143 memcpy (r.slla, h->ethernet_address, 6);
2144 }
2145 break;
2146
Ed Warnickecb9cada2015-12-08 15:45:58 -07002147 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002148 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002149 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002150 (icmp6_neighbor_discovery_mtu_option_t
2151 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002152
Dave Barachd7cb1b52016-12-09 09:52:16 -05002153 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002154 break;
2155
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002156 r.mtu = clib_net_to_host_u32 (h->mtu);
2157
Dave Barachd7cb1b52016-12-09 09:52:16 -05002158 if ((h->mtu && radv_info->adv_link_mtu) &&
2159 (h->mtu !=
2160 clib_host_to_net_u32
2161 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002162 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002163 ip6_neighbor_syslog (vm, LOG_WARNING,
2164 "our AdvLinkMTU on %U doesn't agree with %U",
2165 format_vnet_sw_if_index_name,
2166 vnm, sw_if_index0,
2167 format_ip6_address,
2168 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002169 }
2170 }
2171 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002172
Ed Warnickecb9cada2015-12-08 15:45:58 -07002173 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2174 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002175 icmp6_neighbor_discovery_prefix_information_option_t
2176 * h =
2177 (icmp6_neighbor_discovery_prefix_information_option_t
2178 *) (o0);
2179
Ed Warnickecb9cada2015-12-08 15:45:58 -07002180 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002181 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002182 u32 preferred, valid;
2183
Dave Barachd7cb1b52016-12-09 09:52:16 -05002184 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002185 break;
2186
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002187 vec_validate (r.prefixes,
2188 vec_len (r.prefixes));
2189 ra_report_prefix_info_t *prefix =
2190 vec_elt_at_index (r.prefixes,
2191 vec_len (r.prefixes) - 1);
2192
Dave Barachd7cb1b52016-12-09 09:52:16 -05002193 preferred =
2194 clib_net_to_host_u32 (h->preferred_time);
2195 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002196
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002197 prefix->preferred_time = preferred;
2198 prefix->valid_time = valid;
2199 prefix->flags = h->flags & 0xc0;
2200 prefix->dst_address_length =
2201 h->dst_address_length;
2202 prefix->dst_address = h->dst_address;
2203
Ed Warnickecb9cada2015-12-08 15:45:58 -07002204 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002205 /* *INDENT-OFF* */
2206 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2207 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002208
Dave Barachd7cb1b52016-12-09 09:52:16 -05002209 ip6_address_t mask;
2210 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2211
2212 if(pr_info->enabled &&
2213 (pr_info->prefix_len == h->dst_address_length) &&
2214 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2215 {
2216 /* found it */
2217 if(!pr_info->decrement_lifetime_flag &&
2218 valid != pr_info->adv_valid_lifetime_in_secs)
2219 {
2220 ip6_neighbor_syslog(vm, LOG_WARNING,
2221 "our ADV validlifetime on %U for %U does not agree with %U",
2222 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2223 format_ip6_address, &h->dst_address);
2224 }
2225 if(!pr_info->decrement_lifetime_flag &&
2226 preferred != pr_info->adv_pref_lifetime_in_secs)
2227 {
2228 ip6_neighbor_syslog(vm, LOG_WARNING,
2229 "our ADV preferredlifetime on %U for %U does not agree with %U",
2230 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2231 format_ip6_address, &h->dst_address);
2232 }
2233 }
2234 break;
2235 }));
2236 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002237 break;
2238 }
2239 default:
2240 /* skip this one */
2241 break;
2242 }
2243 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002244 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002245 }
2246 }
2247 }
2248
2249 p0->error = error_node->errors[error0];
2250
Dave Barachd7cb1b52016-12-09 09:52:16 -05002251 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002252 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002253
Ed Warnickecb9cada2015-12-08 15:45:58 -07002254 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2255 to_next, n_left_to_next,
2256 bi0, next0);
2257 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002258
Ed Warnickecb9cada2015-12-08 15:45:58 -07002259 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2260 }
2261
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002262 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002263 vlib_error_count (vm, error_node->node_index,
2264 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2265 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002266
2267 return frame->n_vectors;
2268}
2269
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002270static inline f64
2271random_f64_from_to (f64 from, f64 to)
2272{
2273 static u32 seed = 0;
2274 static u8 seed_set = 0;
2275 if (!seed_set)
2276 {
2277 seed = random_default_seed ();
2278 seed_set = 1;
2279 }
2280 return random_f64 (&seed) * (to - from) + from;
2281}
2282
2283static inline u8
2284get_mac_address (u32 sw_if_index, u8 * address)
2285{
2286 vnet_main_t *vnm = vnet_get_main ();
2287 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2288 if (!hw_if->hw_address)
2289 return 1;
2290 clib_memcpy (address, hw_if->hw_address, 6);
2291 return 0;
2292}
2293
2294static inline vlib_buffer_t *
2295create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2296{
2297 u32 bi0;
2298 vlib_buffer_t *p0;
2299 vlib_buffer_free_list_t *fl;
2300 icmp6_router_solicitation_header_t *rh;
2301 u16 payload_length;
2302 int bogus_length;
2303 u32 sw_if_index;
2304
2305 sw_if_index = radv_info->sw_if_index;
2306
2307 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2308 {
2309 clib_warning ("buffer allocation failure");
2310 return 0;
2311 }
2312
2313 p0 = vlib_get_buffer (vm, bi0);
2314 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2315 vlib_buffer_init_for_free_list (p0, fl);
2316 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 */
2353 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02L << 48);
2354 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) */
2659 memset (a, 0, sizeof (a[0]));
2660
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) */
2692 ip6_link_local_address_from_ethernet_address
2693 (&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 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002763 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2764 &bo0,
2765 n_to_alloc,
2766 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2767 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002768 {
2769 clib_warning ("buffer allocation failure");
2770 return;
2771 }
2772
2773 b0 = vlib_get_buffer (vm, bo0);
2774
2775 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002776 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002777
Dave Barachd7cb1b52016-12-09 09:52:16 -05002778 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002779
2780 b0->error = ICMP6_ERROR_NONE;
2781
2782 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002783 ip0 = (ip6_header_t *) & rp0->ip;
2784 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002785
Dave Barachd7cb1b52016-12-09 09:52:16 -05002786 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2787
2788 ip0->ip_version_traffic_class_and_flow_label =
2789 clib_host_to_net_u32 (0x6 << 28);
2790
2791 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002792 /* for DEBUG - vnet driver won't seem to emit router alerts */
2793 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2794 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002795
Ed Warnickecb9cada2015-12-08 15:45:58 -07002796 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002797
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002799 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2800 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002801
2802 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002803 ip6_set_reserved_multicast_address (&ip0->dst_address,
2804 IP6_MULTICAST_SCOPE_link_local,
2805 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2806
Ed Warnickecb9cada2015-12-08 15:45:58 -07002807 /* add reports here */
2808 ip6_mldp_group_t *m;
2809 int num_addr_records = 0;
2810 icmp6_multicast_address_record_t rr;
2811
2812 /* fill in the hop-by-hop extension header (router alert) info */
2813 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2814 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002815
Ed Warnickecb9cada2015-12-08 15:45:58 -07002816 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2817 rh0->alert.len = 2;
2818 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002819
Ed Warnickecb9cada2015-12-08 15:45:58 -07002820 rh0->pad.type = 1;
2821 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002822
Ed Warnickecb9cada2015-12-08 15:45:58 -07002823 rh0->icmp.checksum = 0;
2824
Dave Barachd7cb1b52016-12-09 09:52:16 -05002825 /* *INDENT-OFF* */
2826 pool_foreach (m, radv_info->mldp_group_pool,
2827 ({
2828 rr.type = m->type;
2829 rr.aux_data_len_u32s = 0;
2830 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2831 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002832
Dave Barachd7cb1b52016-12-09 09:52:16 -05002833 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002834
Dave Barachd7cb1b52016-12-09 09:52:16 -05002835 vlib_buffer_add_data
Damjan Marion072401e2017-07-13 18:53:27 +02002836 (vm, vlib_buffer_get_free_list_index (b0), bo0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002837 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838
Dave Barachd7cb1b52016-12-09 09:52:16 -05002839 payload_length += sizeof( icmp6_multicast_address_record_t);
2840 }));
2841 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002842
2843 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002844 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2845
Ed Warnickecb9cada2015-12-08 15:45:58 -07002846 /* update lengths */
2847 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2848
Dave Barachd7cb1b52016-12-09 09:52:16 -05002849 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2850 &bogus_length);
2851 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002852
Dave Barachd7cb1b52016-12-09 09:52:16 -05002853 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002854 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002855 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002856 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002857 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002858 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002859
Neale Ranns32e1c012016-11-22 17:07:28 +00002860 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002861 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862
Neale Ranns32e1c012016-11-22 17:07:28 +00002863 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002864
Ed Warnickecb9cada2015-12-08 15:45:58 -07002865 f = vlib_get_frame_to_node (vm, node->index);
2866 to_next = vlib_frame_vector_args (f);
2867 to_next[0] = bo0;
2868 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002869
Ed Warnickecb9cada2015-12-08 15:45:58 -07002870 vlib_put_frame_to_node (vm, node->index, f);
2871 return;
2872}
2873
Dave Barachd7cb1b52016-12-09 09:52:16 -05002874/* *INDENT-OFF* */
2875VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2876{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877 .function = icmp6_router_solicitation,
2878 .name = "icmp6-router-solicitation",
2879
2880 .vector_size = sizeof (u32),
2881
2882 .format_trace = format_icmp6_input_trace,
2883
2884 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2885 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002886 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002887 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002888 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2889 },
2890};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002891/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002892
2893/* send a RA or update the timer info etc.. */
2894static uword
2895ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002896 vlib_node_runtime_t * node,
2897 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002898{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002899 vnet_main_t *vnm = vnet_get_main ();
2900 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2901 ip6_radv_t *radv_info;
2902 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002903 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002904 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002905 u32 *to_next = 0;
2906 u32 bo0;
2907 icmp6_router_solicitation_header_t *h0;
2908 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002909 f64 now = vlib_time_now (vm);
2910
2911 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002912 /* *INDENT-OFF* */
2913 pool_foreach (radv_info, nm->if_radv_pool,
2914 ({
2915 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2916 {
2917 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2918 radv_info->next_multicast_time = now;
2919 radv_info->last_multicast_time = now;
2920 radv_info->last_radv_time = 0;
2921 radv_info->all_routers_mcast = 0;
2922 continue;
2923 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002924
Dave Barachd7cb1b52016-12-09 09:52:16 -05002925 /* Make sure that we've joined the all-routers multicast group */
2926 if(!radv_info->all_routers_mcast)
2927 {
2928 /* send MDLP_REPORT_EVENT message */
2929 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2930 radv_info->all_routers_mcast = 1;
2931 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002932
Dave Barachd7cb1b52016-12-09 09:52:16 -05002933 /* is it time to send a multicast RA on this interface? */
2934 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2935 {
2936 u32 n_to_alloc = 1;
2937 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002938
Dave Barachd7cb1b52016-12-09 09:52:16 -05002939 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2940 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002941
Dave Barachd7cb1b52016-12-09 09:52:16 -05002942 /* multicast send - compute next multicast send time */
2943 if( radv_info->initial_adverts_sent > 0)
2944 {
2945 radv_info->initial_adverts_sent--;
2946 if(rfn > radv_info-> initial_adverts_interval)
2947 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002948
Dave Barachd7cb1b52016-12-09 09:52:16 -05002949 /* check to see if we are ceasing to send */
2950 if( radv_info->initial_adverts_sent == 0)
2951 if(radv_info->cease_radv)
2952 radv_info->send_radv = 0;
2953 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002954
Dave Barachd7cb1b52016-12-09 09:52:16 -05002955 radv_info->next_multicast_time = rfn + now;
2956 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002957
Dave Barachd7cb1b52016-12-09 09:52:16 -05002958 /* send advert now - build a "solicted" router advert with unspecified source address */
2959 n_allocated = vlib_buffer_alloc_from_free_list
2960 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
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
2974 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2975
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;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003169 f64 time_now;
Dave Barach49433ad2018-08-08 17:59:03 -04003170 u64 seed;
3171 u32 thread_index = vm->thread_index;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003172 int bogus_length;
3173 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3174
3175 if (node->flags & VLIB_NODE_FLAG_TRACE)
3176 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3177
3178 time_now = vlib_time_now (vm);
Dave Barach49433ad2018-08-08 17:59:03 -04003179 if (time_now - im->nd_throttle_last_seed_change_time[thread_index] > 1e-3)
Neale Rannsc7b8f202018-04-25 06:34:31 -07003180 {
Dave Barach49433ad2018-08-08 17:59:03 -04003181 (void) random_u64 (&im->nd_throttle_seeds[thread_index]);
3182 memset (im->nd_throttle_bitmaps[thread_index], 0,
3183 ND_THROTTLE_BITS / BITS (u8));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003184
Dave Barach49433ad2018-08-08 17:59:03 -04003185 im->nd_throttle_last_seed_change_time[thread_index] = time_now;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003186 }
Dave Barach49433ad2018-08-08 17:59:03 -04003187 seed = im->nd_throttle_seeds[thread_index];
Neale Rannsc7b8f202018-04-25 06:34:31 -07003188
3189 from = vlib_frame_vector_args (frame);
3190 n_left_from = frame->n_vectors;
3191
3192 while (n_left_from > 0)
3193 {
3194 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3195 to_next_drop, n_left_to_next_drop);
3196
3197 while (n_left_from > 0 && n_left_to_next_drop > 0)
3198 {
3199 vlib_buffer_t *p0;
3200 ip6_header_t *ip0;
Dave Barach49433ad2018-08-08 17:59:03 -04003201 u32 pi0, adj_index0, w0, sw_if_index0, drop0;
3202 u64 r0;
3203 uword m0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003204 ip_adjacency_t *adj0;
3205 vnet_hw_interface_t *hw_if0;
3206 ip6_radv_t *radv_info;
3207 u32 next0;
3208
3209 pi0 = from[0];
3210
3211 p0 = vlib_get_buffer (vm, pi0);
3212
3213 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3214
3215 ip0 = vlib_buffer_get_current (p0);
3216
3217 adj0 = adj_get (adj_index0);
3218
3219 if (!is_glean)
3220 {
3221 ip0->dst_address.as_u64[0] =
3222 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3223 ip0->dst_address.as_u64[1] =
3224 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3225 }
3226
Neale Rannsc7b8f202018-04-25 06:34:31 -07003227 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3228 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3229
Dave Barach49433ad2018-08-08 17:59:03 -04003230 /* Compute the ND throttle bitmap hash */
3231 r0 = ip0->dst_address.as_u64[0] ^ ip0->dst_address.as_u64[1] ^ seed;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003232
Dave Barach49433ad2018-08-08 17:59:03 -04003233 /* Find the word and bit */
3234 r0 &= ND_THROTTLE_BITS - 1;
3235 w0 = r0 / BITS (uword);
3236 m0 = (uword) 1 << (r0 % BITS (uword));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003237
Dave Barach49433ad2018-08-08 17:59:03 -04003238 /* If the bit is set, drop the ND request */
3239 drop0 = (im->nd_throttle_bitmaps[thread_index][w0] & m0) != 0;
3240 /* (unconditionally) mark the bit "inuse" */
3241 im->nd_throttle_bitmaps[thread_index][w0] |= m0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003242
3243 from += 1;
3244 n_left_from -= 1;
3245 to_next_drop[0] = pi0;
3246 to_next_drop += 1;
3247 n_left_to_next_drop -= 1;
3248
3249 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3250
3251 /* If the interface is link-down, drop the pkt */
3252 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3253 drop0 = 1;
3254
3255 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3256 {
3257 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3258
3259 if (ri != ~0)
3260 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3261 else
3262 drop0 = 1;
3263 }
3264 else
3265 drop0 = 1;
3266
3267 /*
3268 * the adj has been updated to a rewrite but the node the DPO that got
3269 * us here hasn't - yet. no big deal. we'll drop while we wait.
3270 */
3271 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3272 drop0 = 1;
3273
3274 p0->error =
3275 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3276 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3277
3278 if (drop0)
3279 continue;
3280
3281 {
3282 u32 bi0 = 0;
3283 icmp6_neighbor_solicitation_header_t *h0;
3284 vlib_buffer_t *b0;
3285
3286 h0 = vlib_packet_template_get_packet
3287 (vm, &im->discover_neighbor_packet_template, &bi0);
John Lo084606b2018-06-19 15:27:48 -04003288 if (!h0)
3289 continue;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003290
3291 /*
3292 * Build ethernet header.
3293 * Choose source address based on destination lookup
3294 * adjacency.
3295 */
3296 if (!ip6_src_address_for_packet (lm,
3297 sw_if_index0,
3298 &ip0->dst_address,
3299 &h0->ip.src_address))
3300 {
3301 /* There is no address on the interface */
3302 p0->error =
3303 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3304 vlib_buffer_free (vm, &bi0, 1);
3305 continue;
3306 }
3307
3308 /*
3309 * Destination address is a solicited node multicast address.
3310 * We need to fill in
3311 * the low 24 bits with low 24 bits of target's address.
3312 */
3313 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3314 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3315 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3316
3317 h0->neighbor.target_address = ip0->dst_address;
3318
3319 clib_memcpy (h0->link_layer_option.ethernet_address,
3320 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3321
3322 /* $$$$ appears we need this; why is the checksum non-zero? */
3323 h0->neighbor.icmp.checksum = 0;
3324 h0->neighbor.icmp.checksum =
3325 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3326 &bogus_length);
3327
3328 ASSERT (bogus_length == 0);
3329
3330 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3331 b0 = vlib_get_buffer (vm, bi0);
3332 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3333 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3334
3335 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3336 radv_info->mcast_adj_index;
3337
3338 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3339 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3340
3341 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3342 }
3343 }
3344
3345 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3346 n_left_to_next_drop);
3347 }
3348
3349 return frame->n_vectors;
3350}
3351
3352static uword
3353ip6_discover_neighbor (vlib_main_t * vm,
3354 vlib_node_runtime_t * node, vlib_frame_t * frame)
3355{
3356 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3357}
3358
3359static uword
3360ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3361{
3362 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3363}
3364
3365static char *ip6_discover_neighbor_error_strings[] = {
3366 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3367 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3368 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3369 = "no source address for ND solicitation",
3370};
3371
3372/* *INDENT-OFF* */
3373VLIB_REGISTER_NODE (ip6_glean_node) =
3374{
3375 .function = ip6_glean,
3376 .name = "ip6-glean",
3377 .vector_size = sizeof (u32),
3378 .format_trace = format_ip6_forward_next_trace,
3379 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3380 .error_strings = ip6_discover_neighbor_error_strings,
3381 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3382 .next_nodes =
3383 {
3384 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3385 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3386 },
3387};
3388VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3389{
3390 .function = ip6_discover_neighbor,
3391 .name = "ip6-discover-neighbor",
3392 .vector_size = sizeof (u32),
3393 .format_trace = format_ip6_forward_next_trace,
3394 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3395 .error_strings = ip6_discover_neighbor_error_strings,
3396 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3397 .next_nodes =
3398 {
3399 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3400 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3401 },
3402};
3403/* *INDENT-ON* */
3404
Dave Barachd7cb1b52016-12-09 09:52:16 -05003405/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003406int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003407ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3408 u8 suppress, u8 managed, u8 other,
3409 u8 ll_option, u8 send_unicast, u8 cease,
3410 u8 use_lifetime, u32 lifetime,
3411 u32 initial_count, u32 initial_interval,
3412 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003413{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003414 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3415 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003416 u32 ri;
3417
3418 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003419 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3420 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003421 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003422 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003423
Dave Barachd7cb1b52016-12-09 09:52:16 -05003424 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003425 {
3426
Dave Barachd7cb1b52016-12-09 09:52:16 -05003427 ip6_radv_t *radv_info;
3428 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003429
Dave Barachd7cb1b52016-12-09 09:52:16 -05003430 if ((max_interval != 0) && (min_interval == 0))
3431 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003432
Dave Barachd7cb1b52016-12-09 09:52:16 -05003433 max_interval =
3434 (max_interval !=
3435 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3436 radv_info->max_radv_interval;
3437 min_interval =
3438 (min_interval !=
3439 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3440 radv_info->min_radv_interval;
3441 lifetime =
3442 (use_lifetime !=
3443 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3444 radv_info->adv_router_lifetime_in_sec;
3445
3446 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003447 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003448 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003449 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003450
3451 if (lifetime <= max_interval)
3452 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003453 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003454
3455 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003456 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003457 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3458 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003459 }
3460
Dave Barachd7cb1b52016-12-09 09:52:16 -05003461 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3462 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3463 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003464
Dave Barachd7cb1b52016-12-09 09:52:16 -05003465 /*
3466 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3467 if "flag" is clear don't change corresponding value
3468 */
3469 radv_info->send_radv =
3470 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3471 radv_info->adv_managed_flag =
3472 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3473 radv_info->adv_other_flag =
3474 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3475 radv_info->adv_link_layer_address =
3476 (ll_option !=
3477 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3478 radv_info->send_unicast =
3479 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3480 radv_info->cease_radv =
3481 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3482
3483 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003484 radv_info->max_radv_interval = max_interval;
3485 radv_info->adv_router_lifetime_in_sec = lifetime;
3486
Dave Barachd7cb1b52016-12-09 09:52:16 -05003487 radv_info->initial_adverts_count =
3488 (initial_count !=
3489 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3490 radv_info->initial_adverts_count;
3491 radv_info->initial_adverts_interval =
3492 (initial_interval !=
3493 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3494 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003495
3496 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003497 if ((cease != 0) && (is_no))
3498 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003499
Dave Barachd7cb1b52016-12-09 09:52:16 -05003500 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3501 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003502 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003503 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003504 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003505 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003506}
3507
3508int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003509ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3510 ip6_address_t * prefix_addr, u8 prefix_len,
3511 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3512 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3513 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003514{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003515 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003516 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003517
Ed Warnickecb9cada2015-12-08 15:45:58 -07003518 u32 ri;
3519
3520 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003521 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3522 ~0);
3523
Ed Warnickecb9cada2015-12-08 15:45:58 -07003524 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3525
3526 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003527
3528 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003529 {
3530 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003531 ip6_radv_t *radv_info;
3532 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003533
3534 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003535 ip6_radv_prefix_t *prefix;
3536
Ed Warnickecb9cada2015-12-08 15:45:58 -07003537 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003538 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3539
Ed Warnickecb9cada2015-12-08 15:45:58 -07003540 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3541
Dave Barachd7cb1b52016-12-09 09:52:16 -05003542 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003543 {
3544 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003545 if (!prefix)
3546 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3547
3548 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003549 return VNET_API_ERROR_INVALID_VALUE_2;
3550
Dave Barachd7cb1b52016-12-09 09:52:16 -05003551 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003552 /* do specific delete processing here before returning */
3553 /* try to remove from routing table */
3554
Dave Barachd7cb1b52016-12-09 09:52:16 -05003555 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3556 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003557 pool_put (radv_info->adv_prefixes_pool, prefix);
3558
Dave Barachd7cb1b52016-12-09 09:52:16 -05003559 radv_info->initial_adverts_sent =
3560 radv_info->initial_adverts_count - 1;
3561 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003562 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003563 radv_info->last_radv_time = 0;
3564 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003565 }
3566
3567 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003568 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003569 {
3570 /* add */
3571 u32 pi;
3572 pool_get (radv_info->adv_prefixes_pool, prefix);
3573 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003574 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3575 /* old_value */ 0);
3576
3577 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3578
Ed Warnickecb9cada2015-12-08 15:45:58 -07003579 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003580 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3581
Ed Warnickecb9cada2015-12-08 15:45:58 -07003582 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003583 prefix->adv_on_link_flag = 1; /* L bit set */
3584 prefix->adv_autonomous_flag = 1; /* A bit set */
3585 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003586 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3587 prefix->enabled = 1;
3588 prefix->decrement_lifetime_flag = 1;
3589 prefix->deprecated_prefix_flag = 1;
3590
Dave Barachd7cb1b52016-12-09 09:52:16 -05003591 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003592 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003593 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003594 /* insert prefix into routing table as a connected prefix */
3595 }
3596
Dave Barachd7cb1b52016-12-09 09:52:16 -05003597 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003598 goto restart;
3599 }
3600 else
3601 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003602
3603 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003604 return VNET_API_ERROR_INVALID_VALUE_2;
3605
Dave Barachd7cb1b52016-12-09 09:52:16 -05003606 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003607 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003608 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003609 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003610 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003611 }
3612
Dave Barachd7cb1b52016-12-09 09:52:16 -05003613 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003614 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003615 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003616 prefix->adv_pref_lifetime_in_secs = ~0;
3617 prefix->decrement_lifetime_flag = 0;
3618 }
3619 else
3620 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003621 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3622 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003623 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003624
Ed Warnickecb9cada2015-12-08 15:45:58 -07003625 /* copy remaining */
3626 prefix->enabled = !(no_advertise != 0);
3627 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3628 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3629
Dave Barachd7cb1b52016-12-09 09:52:16 -05003630 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003631 /* restart */
3632 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003633 prefix->valid_lifetime_expires =
3634 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003635 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003636
3637 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3638 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003639 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003640 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003641 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003642 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003643}
3644
3645clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003646ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3647 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003648{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003649 vnet_main_t *vnm = vnet_get_main ();
3650 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3651 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003652 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003653 u8 suppress = 0, managed = 0, other = 0;
3654 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003655 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003656 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3657 0, ra_initial_interval = 0;
3658 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003659
Dave Barachd7cb1b52016-12-09 09:52:16 -05003660 unformat_input_t _line_input, *line_input = &_line_input;
3661 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003662
3663 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003664 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003665 ip6_address_t ip6_addr;
3666 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003667
Ed Warnickecb9cada2015-12-08 15:45:58 -07003668
3669 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003670 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003671 return 0;
3672
3673 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003674 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003675 {
3676
Dave Barachd7cb1b52016-12-09 09:52:16 -05003677 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003678 unformat_vnet_sw_interface, vnm, &sw_if_index))
3679 {
3680 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003681 ethernet_interface_t *eth_if0 = 0;
3682
Ed Warnickecb9cada2015-12-08 15:45:58 -07003683 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003684 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3685 eth_if0 =
3686 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3687
3688 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003689 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003690 error =
3691 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003692 goto done;
3693 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003694
Ed Warnickecb9cada2015-12-08 15:45:58 -07003695 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003696 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3697 sw_if_index, ~0);
3698
Ed Warnickecb9cada2015-12-08 15:45:58 -07003699 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003700
3701 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003702 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003703 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003704 }
3705 else
3706 {
3707 error = clib_error_return (0, "unknown interface %U'",
3708 format_unformat_error, line_input);
3709 goto done;
3710 }
3711 }
3712 else
3713 {
3714 error = clib_error_return (0, "invalid interface name %U'",
3715 format_unformat_error, line_input);
3716 goto done;
3717 }
3718 }
3719
3720 /* get the rest of the command */
3721 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3722 {
3723 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003724 is_no = 1;
3725 else if (unformat (line_input, "prefix %U/%d",
3726 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003727 {
3728 add_radv_info = 0;
3729 break;
3730 }
3731 else if (unformat (line_input, "ra-managed-config-flag"))
3732 {
3733 managed = 1;
3734 break;
3735 }
3736 else if (unformat (line_input, "ra-other-config-flag"))
3737 {
3738 other = 1;
3739 break;
3740 }
Chris Luke33879c92016-06-28 19:54:21 -04003741 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003742 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003743 {
Chris Luke33879c92016-06-28 19:54:21 -04003744 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003745 break;
3746 }
Chris Luke33879c92016-06-28 19:54:21 -04003747 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003748 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003749 {
Chris Luke33879c92016-06-28 19:54:21 -04003750 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003751 break;
3752 }
3753 else if (unformat (line_input, "ra-send-unicast"))
3754 {
3755 send_unicast = 1;
3756 break;
3757 }
3758 else if (unformat (line_input, "ra-lifetime"))
3759 {
3760 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003761 {
3762 error = unformat_parse_error (line_input);
3763 goto done;
3764 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003765 use_lifetime = 1;
3766 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003767 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003768 else if (unformat (line_input, "ra-initial"))
3769 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003770 if (!unformat
3771 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003772 {
3773 error = unformat_parse_error (line_input);
3774 goto done;
3775 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003776 break;
3777 }
3778 else if (unformat (line_input, "ra-interval"))
3779 {
3780 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003781 {
3782 error = unformat_parse_error (line_input);
3783 goto done;
3784 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003785
3786 if (!unformat (line_input, "%d", &ra_min_interval))
3787 ra_min_interval = 0;
3788 break;
3789 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003790 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003791 {
3792 cease = 1;
3793 break;
3794 }
3795 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003796 {
3797 error = unformat_parse_error (line_input);
3798 goto done;
3799 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003800 }
3801
Dave Barachd7cb1b52016-12-09 09:52:16 -05003802 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003803 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003804 ip6_neighbor_ra_config (vm, sw_if_index,
3805 suppress, managed, other,
3806 suppress_ll_option, send_unicast, cease,
3807 use_lifetime, ra_lifetime,
3808 ra_initial_count, ra_initial_interval,
3809 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003810 }
3811 else
3812 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003813 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003814 u32 pref_lifetime_in_secs = 0;
3815 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003816 u8 no_advertise = 0;
3817 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003818 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003819 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003820
3821 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003822 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003823 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003824 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003825 {
3826 use_prefix_default_values = 1;
3827 break;
3828 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003829 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003830 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003831 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003832 pref_lifetime_in_secs = ~0;
3833 break;
3834 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003835 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3836 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003837 break;
3838 else
3839 break;
3840 }
3841
3842
3843 /* get the rest of the command */
3844 while (!use_prefix_default_values &&
3845 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3846 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003847 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003848 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003849 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003850 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003851 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003852 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003853 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003854 no_onlink = 1;
3855 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003856 {
3857 error = unformat_parse_error (line_input);
3858 goto done;
3859 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003860 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003861
3862 ip6_neighbor_ra_prefix (vm, sw_if_index,
3863 &ip6_addr, addr_len,
3864 use_prefix_default_values,
3865 valid_lifetime_in_secs,
3866 pref_lifetime_in_secs,
3867 no_advertise,
3868 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003869 }
3870
Billy McFalla9a20e72017-02-15 11:39:12 -05003871done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003872 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003873
Ed Warnickecb9cada2015-12-08 15:45:58 -07003874 return error;
3875}
3876
Chris Lukebfd32fd2016-06-24 20:38:06 -04003877static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003878ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003879{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003880 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003881 u32 i;
3882
3883 for (i = 0; i < vec_len (addrs); i++)
3884 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003885 ip_interface_address_t *a =
3886 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3887 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003888
3889 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003890 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003891 }
3892}
3893
Ed Warnickecb9cada2015-12-08 15:45:58 -07003894static clib_error_t *
3895show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003896 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003897{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003898 vnet_main_t *vnm = vnet_get_main ();
3899 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3900 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003901 u32 sw_if_index;
3902
3903 sw_if_index = ~0;
3904
Dave Barachd7cb1b52016-12-09 09:52:16 -05003905 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003906 {
3907 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003908
Dave Barachd7cb1b52016-12-09 09:52:16 -05003909 /* look up the radv_t information for this interface */
3910 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3911 sw_if_index, ~0);
3912
3913 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3914
3915 if (ri != ~0)
3916 {
3917 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3918 ip6_radv_t *radv_info;
3919 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3920
3921 vlib_cli_output (vm, "%U is admin %s\n",
3922 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003923 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003924 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3925 "up" : "down"));
3926
Chris Lukebfd32fd2016-06-24 20:38:06 -04003927 u32 ai;
3928 u32 *link_scope = 0, *global_scope = 0;
3929 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003930 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003931
Dave Barachd7cb1b52016-12-09 09:52:16 -05003932 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3933 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003934 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3935
Dave Barachd7cb1b52016-12-09 09:52:16 -05003936 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003937 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003938 a = pool_elt_at_index (lm->if_address_pool, ai);
3939 ip6_address_t *address =
3940 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003941
Chris Lukebfd32fd2016-06-24 20:38:06 -04003942 if (ip6_address_is_link_local_unicast (address))
3943 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003944 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003945 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003946 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003947 vec_add1 (local_scope, ai);
3948 else
3949 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003950
3951 ai = a->next_this_sw_interface;
3952 }
3953
Dave Barachd7cb1b52016-12-09 09:52:16 -05003954 if (vec_len (link_scope))
3955 {
3956 vlib_cli_output (vm, "\tLink-local address(es):\n");
3957 ip6_print_addrs (vm, link_scope);
3958 vec_free (link_scope);
3959 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003960
Dave Barachd7cb1b52016-12-09 09:52:16 -05003961 if (vec_len (local_scope))
3962 {
3963 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3964 ip6_print_addrs (vm, local_scope);
3965 vec_free (local_scope);
3966 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003967
Dave Barachd7cb1b52016-12-09 09:52:16 -05003968 if (vec_len (global_scope))
3969 {
3970 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3971 ip6_print_addrs (vm, global_scope);
3972 vec_free (global_scope);
3973 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003974
Dave Barachd7cb1b52016-12-09 09:52:16 -05003975 if (vec_len (unknown_scope))
3976 {
3977 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3978 ip6_print_addrs (vm, unknown_scope);
3979 vec_free (unknown_scope);
3980 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003981
Neale Ranns53da2212018-02-24 02:11:19 -08003982 vlib_cli_output (vm, "\tLink-local address(es):\n");
3983 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3984 &radv_info->link_local_address);
3985
Ed Warnickecb9cada2015-12-08 15:45:58 -07003986 vlib_cli_output (vm, "\tJoined group address(es):\n");
3987 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003988 /* *INDENT-OFF* */
3989 pool_foreach (m, radv_info->mldp_group_pool,
3990 ({
3991 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3992 &m->mcast_address);
3993 }));
3994 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003995
3996 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003997 ip6_radv_prefix_t *p;
3998 /* *INDENT-OFF* */
3999 pool_foreach (p, radv_info->adv_prefixes_pool,
4000 ({
4001 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
4002 format_ip6_address, &p->prefix, p->prefix_len);
4003 }));
4004 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004005
Dave Barachd7cb1b52016-12-09 09:52:16 -05004006 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004007 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
4008 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
4009 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
4010 vlib_cli_output (vm, "\tND DAD is disabled\n");
4011 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
4012 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
4013 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004014 vlib_cli_output (vm,
4015 "\tND advertised retransmit interval is %d (msec)\n",
4016 radv_info->
4017 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004018
4019 u32 ra_interval = radv_info->max_radv_interval;
4020 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004021 vlib_cli_output (vm,
4022 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004023 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004024 vlib_cli_output (vm,
4025 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004026 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004027 vlib_cli_output (vm,
4028 "\tHosts %s stateless autoconfig for addresses\n",
4029 (radv_info->adv_managed_flag) ? "use" :
4030 " don't use");
4031 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4032 radv_info->n_advertisements_sent);
4033 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4034 radv_info->n_solicitations_rcvd);
4035 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4036 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004037 }
4038 else
4039 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004040 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004041 format_unformat_error, input);
4042
4043 }
4044 }
4045 return error;
4046}
4047
Billy McFall0683c9c2016-10-13 08:27:31 -04004048/*?
4049 * This command is used to display various IPv6 attributes on a given
4050 * interface.
4051 *
4052 * @cliexpar
4053 * Example of how to display IPv6 settings:
4054 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4055 * GigabitEthernet2/0/0 is admin up
4056 * Link-local address(es):
4057 * fe80::ab8/64
4058 * Joined group address(es):
4059 * ff02::1
4060 * ff02::2
4061 * ff02::16
4062 * ff02::1:ff00:ab8
4063 * Advertised Prefixes:
4064 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4065 * MTU is 1500
4066 * ICMP error messages are unlimited
4067 * ICMP redirects are disabled
4068 * ICMP unreachables are not sent
4069 * ND DAD is disabled
4070 * ND advertised reachable time is 0
4071 * ND advertised retransmit interval is 0 (msec)
4072 * ND router advertisements are sent every 200 seconds (min interval is 150)
4073 * ND router advertisements live for 600 seconds
4074 * Hosts use stateless autoconfig for addresses
4075 * ND router advertisements sent 19336
4076 * ND router solicitations received 0
4077 * ND router solicitations dropped 0
4078 * @cliexend
4079 * Example of output if IPv6 is not enabled on the interface:
4080 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4081 * show ip6 interface: IPv6 not enabled on interface
4082 * @cliexend
4083?*/
4084/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004085VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4086{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004087 .path = "show ip6 interface",
4088 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004089 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004090};
Billy McFall0683c9c2016-10-13 08:27:31 -04004091/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004092
4093clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004094disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004095{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004096 clib_error_t *error = 0;
4097 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004098 u32 ri;
4099
4100 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004101 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4102 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004103 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004104
Ed Warnickecb9cada2015-12-08 15:45:58 -07004105 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004106 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004107 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004108 ip6_radv_t *radv_info;
4109
4110 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004111
4112 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004113 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004114 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004115 {
4116 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004117 ip6_ll_prefix_t ilp = {
4118 .ilp_addr = radv_info->link_local_address,
4119 .ilp_sw_if_index = sw_if_index,
4120 };
4121 ip6_ll_table_entry_delete (&ilp);
4122 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004123 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004124 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4125 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004126 }
4127 }
4128 return error;
4129}
4130
4131int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004132ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004133{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004134 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4135 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004136
Dave Barachd7cb1b52016-12-09 09:52:16 -05004137 /* look up the radv_t information for this interface */
4138 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4139 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004140
Dave Barachd7cb1b52016-12-09 09:52:16 -05004141 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004142
Dave Barachd7cb1b52016-12-09 09:52:16 -05004143 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004144}
4145
Dave Barachd7cb1b52016-12-09 09:52:16 -05004146clib_error_t *
4147enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004148{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004149 clib_error_t *error = 0;
4150 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004151 u32 ri;
4152 int is_add = 1;
4153
4154 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004155 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4156 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004157
Dave Barachd7cb1b52016-12-09 09:52:16 -05004158 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4159
4160 /* if not created yet */
4161 if (ri == ~0)
4162 {
4163 vnet_main_t *vnm = vnet_get_main ();
4164 vnet_sw_interface_t *sw_if0;
4165
4166 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4167 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4168 {
4169 ethernet_interface_t *eth_if0;
4170
4171 eth_if0 =
4172 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4173 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004174 {
4175 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004176 ri =
4177 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004178
Dave Barachd7cb1b52016-12-09 09:52:16 -05004179 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004180 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004181 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004182 ip6_address_t link_local_address;
4183
Dave Barachd7cb1b52016-12-09 09:52:16 -05004184 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004185
Dave Barachd7cb1b52016-12-09 09:52:16 -05004186 ip6_link_local_address_from_ethernet_mac_address
4187 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004188
4189 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004190 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07004191 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004192 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004193 {
4194 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004195 link_local_address.as_u64[1] =
4196 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004197
4198 link_local_address.as_u64[0] =
4199 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004200 /* clear u bit */
4201 link_local_address.as_u8[8] &= 0xfd;
4202 }
Neale Ranns53da2212018-02-24 02:11:19 -08004203 {
4204 ip6_ll_prefix_t ilp = {
4205 .ilp_addr = link_local_address,
4206 .ilp_sw_if_index = sw_if_index,
4207 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004208
Neale Ranns53da2212018-02-24 02:11:19 -08004209 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4210 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004211
Ed Warnickecb9cada2015-12-08 15:45:58 -07004212 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004213 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4214 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004215
Neale Ranns53da2212018-02-24 02:11:19 -08004216 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004217 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004218 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004219 }
4220 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004221 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004222 }
4223 }
4224 return error;
4225}
4226
Neale Ranns53da2212018-02-24 02:11:19 -08004227int
4228ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4229{
4230 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4231 ip6_radv_t *radv_info;
4232 u32 ri;
4233
Eyal Baricd307742018-07-22 12:45:15 +03004234 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
Neale Ranns53da2212018-02-24 02:11:19 -08004235 return 0;
4236
4237 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4238
4239 if (ri == ~0)
4240 return 0;
4241
4242 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4243 *addr = radv_info->link_local_address;
4244
4245 return (!0);
4246}
4247
Ed Warnickecb9cada2015-12-08 15:45:58 -07004248static clib_error_t *
4249enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004250 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004251{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004252 vnet_main_t *vnm = vnet_get_main ();
4253 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004254 u32 sw_if_index;
4255
4256 sw_if_index = ~0;
4257
Dave Barachd7cb1b52016-12-09 09:52:16 -05004258 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004259 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004260 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004261 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004262 else
4263 {
4264 error = clib_error_return (0, "unknown interface\n'",
4265 format_unformat_error, input);
4266
4267 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004268 return error;
4269}
4270
Billy McFall0683c9c2016-10-13 08:27:31 -04004271/*?
4272 * This command is used to enable IPv6 on a given interface.
4273 *
4274 * @cliexpar
4275 * Example of how enable IPv6 on a given interface:
4276 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4277?*/
4278/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004279VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4280{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004281 .path = "enable ip6 interface",
4282 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004283 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004284};
Billy McFall0683c9c2016-10-13 08:27:31 -04004285/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004286
4287static clib_error_t *
4288disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004289 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004290{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004291 vnet_main_t *vnm = vnet_get_main ();
4292 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004293 u32 sw_if_index;
4294
4295 sw_if_index = ~0;
4296
Dave Barachd7cb1b52016-12-09 09:52:16 -05004297 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004298 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004299 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004300 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004301 else
4302 {
4303 error = clib_error_return (0, "unknown interface\n'",
4304 format_unformat_error, input);
4305
4306 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004307 return error;
4308}
4309
Billy McFall0683c9c2016-10-13 08:27:31 -04004310/*?
4311 * This command is used to disable IPv6 on a given interface.
4312 *
4313 * @cliexpar
4314 * Example of how disable IPv6 on a given interface:
4315 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4316?*/
4317/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004318VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4319{
Billy McFall0683c9c2016-10-13 08:27:31 -04004320 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004321 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004322 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004323};
Billy McFall0683c9c2016-10-13 08:27:31 -04004324/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004325
Billy McFall0683c9c2016-10-13 08:27:31 -04004326/*?
4327 * This command is used to configure the neighbor discovery
4328 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4329 * command to display some of the current neighbor discovery parameters
4330 * on a given interface. This command has three formats:
4331 *
4332 *
4333 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4334 *
4335 * '<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>'
4336 *
4337 * Where:
4338 *
4339 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4340 * router-advertisement messages to use stateful address
4341 * auto-configuration to obtain address information (sets the M-bit).
4342 * Default is the M-bit is not set and the '<em>no</em>' option
4343 * returns it to this default state.
4344 *
4345 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4346 * router-advertisement messages that hosts use stateful auto
4347 * configuration to obtain nonaddress related information (sets
4348 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4349 * option returns it to this default state.
4350 *
4351 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4352 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4353 * router-advertisement messages.
4354 *
4355 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4356 * optional source link-layer address in the ICMPv6 router-advertisement
4357 * messages. Default is to include the optional source link-layer address
4358 * and the '<em>no</em>' option returns it to this default state.
4359 *
4360 * <em>[no] ra-send-unicast</em> - Use the source address of the
4361 * router-solicitation message if availiable. The default is to use
4362 * multicast address of all nodes, and the '<em>no</em>' option returns
4363 * it to this default state.
4364 *
4365 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4366 * default router in ICMPv6 router-advertisement messages. The range is
4367 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4368 * '<em><max-interval></em>'. The default value is 600 seconds and the
4369 * '<em>no</em>' option returns it to this default value.
4370 *
4371 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4372 * router-advertisement messages sent and the interval between each
4373 * message. Range for count is 1 - 3 and default is 3. Range for interval
4374 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4375 * returns both to their default value.
4376 *
4377 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4378 * interval between sending ICMPv6 router-advertisement messages. The
4379 * range for max-interval is from 4 to 200 seconds. min-interval can not
4380 * be more than 75% of max-interval. If not set, min-interval will be
4381 * set to 75% of max-interval. The range for min-interval is from 3 to
4382 * 150 seconds. The '<em>no</em>' option returns both to their default
4383 * value.
4384 *
4385 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4386 * The '<em>no</em>' options implies to start (or restart) sending
4387 * ICMPv6 router-advertisement messages.
4388 *
4389 *
4390 * <b>Format 2 - Prefix Options:</b>
4391 *
4392 * '<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>'
4393 *
4394 * Where:
4395 *
4396 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4397 *
4398 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4399 * length of time in seconds during what the prefix is valid for the purpose of
4400 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4401 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4402 * length of time in seconds during what addresses generated from the prefix remain
4403 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4404 *
4405 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4406 * are inifinte, no timeout.
4407 *
4408 * <em>no-advertise</em> - Do not send full router address in prefix
4409 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4410 *
4411 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4412 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4413 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4414 *
4415 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4416 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4417 *
4418 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4419 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4420 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4421 * the L-bit.
4422 *
4423 *
4424 * <b>Format 3: - Default of Prefix:</b>
4425 *
4426 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4427 *
4428 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4429 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4430 * is ignored and the prefix is deleted.
4431 *
4432 *
4433 * @cliexpar
4434 * Example of how set a router advertisement option:
4435 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4436 * Example of how to add a prefix:
4437 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4438 * Example of how to delete a prefix:
4439 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4440?*/
4441/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004442VLIB_CLI_COMMAND (ip6_nd_command, static) =
4443{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004444 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004445 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004446 .function = ip6_neighbor_cmd,
4447};
Billy McFall0683c9c2016-10-13 08:27:31 -04004448/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004449
4450clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004451set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08004452 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004453{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004454 clib_error_t *error = 0;
4455 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004456 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004457 ip6_radv_t *radv_info;
4458 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07004459
Dave Barachd7cb1b52016-12-09 09:52:16 -05004460 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004461 {
4462 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004463 return (error = clib_error_return (0, "address not link-local",
4464 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004465 }
4466
4467 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004468 enable_ip6_interface (vm, sw_if_index);
4469
Ed Warnickecb9cada2015-12-08 15:45:58 -07004470 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004471
4472 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004473 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004474 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004475
4476 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004477
Ed Warnickecb9cada2015-12-08 15:45:58 -07004478 /* delete the old one */
4479 error = ip6_add_del_interface_address (vm, sw_if_index,
4480 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08004481 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05004482
4483 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004484 {
4485 /* add the new one */
4486 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08004487 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004488 0 /* is_del */ );
4489
4490 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004491 {
4492 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004493 }
4494 }
4495 }
4496 else
4497 {
4498 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4499 error = clib_error_return (0, "ip6 not enabled for interface",
4500 format_unformat_error);
4501 }
4502 return error;
4503}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004504
Ed Warnickecb9cada2015-12-08 15:45:58 -07004505clib_error_t *
4506set_ip6_link_local_address_cmd (vlib_main_t * vm,
4507 unformat_input_t * input,
4508 vlib_cli_command_t * cmd)
4509{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004510 vnet_main_t *vnm = vnet_get_main ();
4511 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004512 u32 sw_if_index;
4513 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004514
4515 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004516 {
4517 /* get the rest of the command */
4518 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4519 {
Neale Ranns75152282017-01-09 01:00:45 -08004520 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004521 break;
4522 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05004523 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004524 }
4525 }
Neale Ranns75152282017-01-09 01:00:45 -08004526 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004527 return error;
4528}
4529
Billy McFall0683c9c2016-10-13 08:27:31 -04004530/*?
4531 * This command is used to assign an IPv6 Link-local address to an
4532 * interface. This command will enable IPv6 on an interface if it
4533 * is not already enabled. Use the '<em>show ip6 interface</em>' command
4534 * to display the assigned Link-local address.
4535 *
4536 * @cliexpar
4537 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08004538 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04004539?*/
4540/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004541VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
4542{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004543 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08004544 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004545 .function = set_ip6_link_local_address_cmd,
4546};
Billy McFall0683c9c2016-10-13 08:27:31 -04004547/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004548
Neale Ranns87df12d2017-02-18 08:16:41 -08004549/**
4550 * @brief callback when an interface address is added or deleted
4551 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004552static void
4553ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4554 uword opaque,
4555 u32 sw_if_index,
4556 ip6_address_t * address,
4557 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004558 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004559{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004560 vnet_main_t *vnm = vnet_get_main ();
4561 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004562 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004563 vlib_main_t *vm = vnm->vlib_main;
4564 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004565 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004566
4567 /* create solicited node multicast address for this interface adddress */
4568 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004569
Ed Warnickecb9cada2015-12-08 15:45:58 -07004570 a.as_u8[0xd] = address->as_u8[0xd];
4571 a.as_u8[0xe] = address->as_u8[0xe];
4572 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004573
4574 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004575 {
4576 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004577 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004578
4579 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004580 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4581 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004582 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004583 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004584 {
4585 /* get radv_info */
4586 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4587
4588 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004589 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004590 radv_info->ref_count++;
4591
Neale Ranns87df12d2017-02-18 08:16:41 -08004592 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004593 }
4594 }
4595 else
4596 {
4597
4598 /* delete */
4599 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004600 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4601 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004602 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004603
Dave Barachd7cb1b52016-12-09 09:52:16 -05004604 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004605 {
4606 /* get radv_info */
4607 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4608
Neale Ranns87df12d2017-02-18 08:16:41 -08004609 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004610
4611 /* if interface up send MLDP "report" */
4612 radv_info->all_routers_mcast = 0;
4613
4614 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004615 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004616 radv_info->ref_count--;
4617 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004618 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4619 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004620 }
4621}
4622
Dave Barachd7cb1b52016-12-09 09:52:16 -05004623clib_error_t *
4624ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004625{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004626 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004627
4628 nm->limit_neighbor_cache_size = neighbor_limit;
4629 return 0;
4630}
4631
Neale Ranns15002542017-09-10 04:39:11 -07004632static void
4633ip6_neighbor_table_bind (ip6_main_t * im,
4634 uword opaque,
4635 u32 sw_if_index,
4636 u32 new_fib_index, u32 old_fib_index)
4637{
4638 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4639 ip6_neighbor_t *n = NULL;
4640 u32 i, *to_re_add = 0;
4641
4642 /* *INDENT-OFF* */
4643 pool_foreach (n, nm->neighbor_pool,
4644 ({
4645 if (n->key.sw_if_index == sw_if_index)
4646 vec_add1 (to_re_add, n - nm->neighbor_pool);
4647 }));
4648 /* *INDENT-ON* */
4649
4650 for (i = 0; i < vec_len (to_re_add); i++)
4651 {
4652 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4653 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4654 ip6_neighbor_adj_fib_add (n, new_fib_index);
4655 }
4656 vec_free (to_re_add);
4657}
4658
Dave Barachd7cb1b52016-12-09 09:52:16 -05004659static clib_error_t *
4660ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004661{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004662 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4663 ip6_main_t *im = &ip6_main;
4664
Ed Warnickecb9cada2015-12-08 15:45:58 -07004665 mhash_init (&nm->neighbor_index_by_key,
4666 /* value size */ sizeof (uword),
4667 /* key size */ sizeof (ip6_neighbor_key_t));
4668
Dave Barachd7cb1b52016-12-09 09:52:16 -05004669 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4670 ip6_icmp_neighbor_solicitation_node.index);
4671 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4672 ip6_icmp_neighbor_advertisement_node.index);
4673 icmp6_register_type (vm, ICMP6_router_solicitation,
4674 ip6_icmp_router_solicitation_node.index);
4675 icmp6_register_type (vm, ICMP6_router_advertisement,
4676 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004677
4678 /* handler node for ip6 neighbor discovery events and timers */
4679 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4680
4681 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004682 ip6_add_del_interface_address_callback_t cb;
4683 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4684
Ed Warnickecb9cada2015-12-08 15:45:58 -07004685 /* when an interface address changes... */
4686 cb.function = ip6_neighbor_add_del_interface_address;
4687 cb.function_opaque = 0;
4688 vec_add1 (im->add_del_interface_address_callbacks, cb);
4689
Neale Ranns15002542017-09-10 04:39:11 -07004690 ip6_table_bind_callback_t cbt;
4691 cbt.function = ip6_neighbor_table_bind;
4692 cbt.function_opaque = 0;
4693 vec_add1 (im->table_bind_callbacks, cbt);
4694
Ed Warnickecb9cada2015-12-08 15:45:58 -07004695 mhash_init (&nm->pending_resolutions_by_address,
4696 /* value size */ sizeof (uword),
4697 /* key size */ sizeof (ip6_address_t));
4698
John Lo1edfba92016-08-27 01:11:57 -04004699 mhash_init (&nm->mac_changes_by_address,
4700 /* value size */ sizeof (uword),
4701 /* key size */ sizeof (ip6_address_t));
4702
Ed Warnickecb9cada2015-12-08 15:45:58 -07004703 /* default, configurable */
4704 nm->limit_neighbor_cache_size = 50000;
4705
Eyal Baric125ecc2017-09-20 11:29:17 +03004706 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4707
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004708 nm->ip6_ra_publisher_node = (uword) ~ 0;
4709
Ed Warnickecb9cada2015-12-08 15:45:58 -07004710#if 0
4711 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004712 vec_validate_init_empty
4713 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004714#endif
4715
Ed Warnickecb9cada2015-12-08 15:45:58 -07004716 return 0;
4717}
4718
4719VLIB_INIT_FUNCTION (ip6_neighbor_init);
4720
4721
Dave Barachd7cb1b52016-12-09 09:52:16 -05004722void
4723vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4724 void *address_arg,
4725 uword node_index,
4726 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004727{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004728 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4729 ip6_address_t *address = address_arg;
4730 uword *p;
4731 pending_resolution_t *pr;
4732
Ed Warnickecb9cada2015-12-08 15:45:58 -07004733 pool_get (nm->pending_resolutions, pr);
4734
4735 pr->next_index = ~0;
4736 pr->node_index = node_index;
4737 pr->type_opaque = type_opaque;
4738 pr->data = data;
4739
4740 p = mhash_get (&nm->pending_resolutions_by_address, address);
4741 if (p)
4742 {
4743 /* Insert new resolution at the head of the list */
4744 pr->next_index = p[0];
4745 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4746 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004747
4748 mhash_set (&nm->pending_resolutions_by_address, address,
4749 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004750}
4751
Dave Barachd7cb1b52016-12-09 09:52:16 -05004752int
4753vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4754 void *data_callback,
4755 u32 pid,
4756 void *address_arg,
4757 uword node_index,
4758 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004759{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004760 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4761 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004762
Eyal Barif9f40652017-04-01 03:55:08 +03004763 /* Try to find an existing entry */
4764 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4765 u32 *p = first;
4766 pending_resolution_t *mc;
4767 while (p && *p != ~0)
4768 {
4769 mc = pool_elt_at_index (nm->mac_changes, *p);
4770 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4771 && mc->pid == pid)
4772 break;
4773 p = &mc->next_index;
4774 }
4775
4776 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004777 if (is_add)
4778 {
Eyal Barif9f40652017-04-01 03:55:08 +03004779 if (found)
4780 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4781
John Lo1edfba92016-08-27 01:11:57 -04004782 pool_get (nm->mac_changes, mc);
Neale Rannsf12dad62018-06-04 18:41:24 -07004783 /* *INDENT-OFF* */
Eyal Barif9f40652017-04-01 03:55:08 +03004784 *mc = (pending_resolution_t)
4785 {
Neale Rannsf12dad62018-06-04 18:41:24 -07004786 .next_index = ~0,
4787 .node_index = node_index,
4788 .type_opaque = type_opaque,
4789 .data = data,
4790 .data_callback = data_callback,
4791 .pid = pid,
4792 };
4793 /* *INDENT-ON* */
John Lo1edfba92016-08-27 01:11:57 -04004794
Eyal Barif9f40652017-04-01 03:55:08 +03004795 /* Insert new resolution at the end of the list */
4796 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004797 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004798 p[0] = new_idx;
4799 else
4800 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004801 }
4802 else
4803 {
Eyal Barif9f40652017-04-01 03:55:08 +03004804 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004805 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004806
Eyal Barif9f40652017-04-01 03:55:08 +03004807 /* Clients may need to clean up pool entries, too */
4808 void (*fp) (u32, u8 *) = data_callback;
4809 if (fp)
4810 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04004811
Eyal Barif9f40652017-04-01 03:55:08 +03004812 /* Remove the entry from the list and delete the entry */
4813 *p = mc->next_index;
4814 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004815
Eyal Barif9f40652017-04-01 03:55:08 +03004816 /* Remove from hash if we deleted the last entry */
4817 if (*p == ~0 && p == first)
4818 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004819 }
Eyal Barif9f40652017-04-01 03:55:08 +03004820 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004821}
4822
Dave Barachd7cb1b52016-12-09 09:52:16 -05004823int
4824vnet_ip6_nd_term (vlib_main_t * vm,
4825 vlib_node_runtime_t * node,
4826 vlib_buffer_t * p0,
4827 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004828 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004829{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004830 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4831 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
John Lo1edfba92016-08-27 01:11:57 -04004832
4833 ndh = ip6_next_header (ip);
4834 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4835 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004836 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004837
4838 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4839 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4840 {
4841 u8 *t0 = vlib_add_trace (vm, node, p0,
4842 sizeof (icmp6_input_trace_t));
4843 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4844 }
4845
4846 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004847 if (PREDICT_FALSE
4848 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4849 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004850 {
Eyal Baric125ecc2017-09-20 11:29:17 +03004851 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004852 }
4853
4854 /* Check if MAC entry exsist for solicited target IP */
4855 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4856 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004857 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004858 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004859 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004860
4861 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004862 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004863 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4864 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004865 return 0; /* source link layer address option not present */
4866
John Lo1edfba92016-08-27 01:11:57 -04004867 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004868 macp =
4869 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004870 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004871 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004872 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004873 vlib_node_runtime_t *error_node =
4874 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004875 ip->dst_address = ip->src_address;
4876 ip->src_address = ndh->target_address;
4877 ip->hop_limit = 255;
4878 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004879 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004880 clib_memcpy (opt->ethernet_address, macp, 6);
4881 ndh->icmp.type = ICMP6_neighbor_advertisement;
4882 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004883 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4884 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004885 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004886 ndh->icmp.checksum =
4887 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4888 clib_memcpy (eth->dst_address, eth->src_address, 6);
4889 clib_memcpy (eth->src_address, macp, 6);
4890 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004891 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004892 return 1;
4893 }
John Lo1edfba92016-08-27 01:11:57 -04004894 }
4895
4896 return 0;
4897
4898}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004899
Neale Ranns3f844d02017-02-18 00:03:54 -08004900int
4901ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4902{
4903 u32 fib_index;
4904
4905 fib_prefix_t pfx = {
4906 .fp_len = 128,
4907 .fp_proto = FIB_PROTOCOL_IP6,
4908 .fp_addr = {
4909 .ip6 = *addr,
4910 },
4911 };
4912 ip46_address_t nh = {
4913 .ip6 = *addr,
4914 };
4915
4916 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4917
4918 if (~0 == fib_index)
4919 return VNET_API_ERROR_NO_SUCH_FIB;
4920
4921 if (is_del)
4922 {
4923 fib_table_entry_path_remove (fib_index,
4924 &pfx,
4925 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004926 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004927 &nh,
4928 sw_if_index,
4929 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4930 /* flush the ND cache of this address if it's there */
4931 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4932 sw_if_index, addr, NULL, 0);
4933 }
4934 else
4935 {
4936 fib_table_entry_path_add (fib_index,
4937 &pfx,
4938 FIB_SOURCE_IP6_ND_PROXY,
4939 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004940 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004941 &nh,
4942 sw_if_index,
4943 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4944 }
4945 return (0);
4946}
4947
4948static clib_error_t *
4949set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4950 unformat_input_t * input, vlib_cli_command_t * cmd)
4951{
4952 vnet_main_t *vnm = vnet_get_main ();
4953 clib_error_t *error = 0;
4954 ip6_address_t addr;
4955 u32 sw_if_index;
4956 u8 is_del = 0;
4957
4958 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4959 {
4960 /* get the rest of the command */
4961 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4962 {
4963 if (unformat (input, "%U", unformat_ip6_address, &addr))
4964 break;
4965 else if (unformat (input, "delete") || unformat (input, "del"))
4966 is_del = 1;
4967 else
4968 return (unformat_parse_error (input));
4969 }
4970 }
4971
4972 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4973
4974 return error;
4975}
4976
4977/* *INDENT-OFF* */
4978VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4979{
4980 .path = "set ip6 nd proxy",
4981 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4982 .function = set_ip6_nd_proxy_cmd,
4983};
4984/* *INDENT-ON* */
4985
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004986void
Neale Ranns3be6b282016-12-20 14:24:01 +00004987ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004988{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004989 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4990 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004991 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004992
4993 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004994 pool_foreach (n, nm->neighbor_pool,
4995 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004996 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004997 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004998 adj_nbr_walk_nh6 (sw_if_index,
4999 &n->key.ip6_address,
5000 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05005001 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02005002 }));
5003 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08005004
5005 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
5006
5007 if (ADJ_INDEX_INVALID != ai)
5008 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02005009}
Dave Barachd7cb1b52016-12-09 09:52:16 -05005010
John Lo8b81cb42017-06-26 01:40:20 -04005011void
Steven9f781d82018-06-05 11:09:32 -07005012send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04005013{
5014 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04005015 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07005016
Steven9f781d82018-06-05 11:09:32 -07005017 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07005018}
5019
5020void
5021send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07005022 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07005023{
5024 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07005025 vnet_main_t *vnm = vnet_get_main ();
5026 u8 *rewrite, rewrite_len;
5027 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
5028 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07005029
John Lo8b81cb42017-06-26 01:40:20 -04005030 if (ip6_addr)
5031 {
5032 clib_warning
5033 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
5034 format_ip6_address, ip6_addr, sw_if_index);
5035
5036 /* Form unsolicited neighbor advertisement packet from NS pkt template */
5037 int bogus_length;
5038 u32 bi = 0;
5039 icmp6_neighbor_solicitation_header_t *h =
5040 vlib_packet_template_get_packet (vm,
5041 &i6m->discover_neighbor_packet_template,
5042 &bi);
John Lo084606b2018-06-19 15:27:48 -04005043 if (!h)
5044 return;
5045
John Lo8b81cb42017-06-26 01:40:20 -04005046 ip6_set_reserved_multicast_address (&h->ip.dst_address,
5047 IP6_MULTICAST_SCOPE_link_local,
5048 IP6_MULTICAST_GROUP_ID_all_hosts);
5049 h->ip.src_address = ip6_addr[0];
5050 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
5051 h->neighbor.target_address = ip6_addr[0];
5052 h->neighbor.advertisement_flags = clib_host_to_net_u32
5053 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
ahdj00722130e12018-08-14 10:35:43 +08005054 h->link_layer_option.header.type =
5055 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo8b81cb42017-06-26 01:40:20 -04005056 clib_memcpy (h->link_layer_option.ethernet_address,
5057 hi->hw_address, vec_len (hi->hw_address));
5058 h->neighbor.icmp.checksum =
5059 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
5060 ASSERT (bogus_length == 0);
5061
5062 /* Setup MAC header with IP6 Etype and mcast DMAC */
5063 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07005064 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04005065 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07005066 rewrite =
5067 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5068 rewrite_len = vec_len (rewrite);
5069 vlib_buffer_advance (b, -rewrite_len);
5070 ethernet_header_t *e = vlib_buffer_get_current (b);
5071 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5072 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04005073
5074 /* Send unsolicited ND advertisement packet out the specified interface */
5075 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5076 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5077 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5078 u32 *to_next = vlib_frame_vector_args (f);
5079 to_next[0] = bi;
5080 f->n_vectors = 1;
5081 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5082 }
5083}
5084
Dave Barachd7cb1b52016-12-09 09:52:16 -05005085/*
5086 * fd.io coding-style-patch-verification: ON
5087 *
5088 * Local Variables:
5089 * eval: (c-set-style "gnu")
5090 * End:
5091 */