blob: 8466ba70313500762a08017a09be2434b9a7b641 [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 Ranns0bdd3192018-09-07 11:04:52 -0700417 const ip6_address_t * a,
418 const u8 * link_layer_address,
419 int is_add, int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420{
421 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500422 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500423
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 args.sw_if_index = sw_if_index;
425 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100426 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800427 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100428 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800429 if (NULL != link_layer_address)
430 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500431
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500433 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100436static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500437ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100438{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500439 icmp6_neighbor_solicitation_header_t *h;
440 vnet_main_t *vnm = vnet_get_main ();
441 ip6_main_t *im = &ip6_main;
442 ip_interface_address_t *ia;
443 ip6_address_t *dst, *src;
444 vnet_hw_interface_t *hi;
445 vnet_sw_interface_t *si;
446 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100447 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500448 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100449 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100450
Dave Barachd7cb1b52016-12-09 09:52:16 -0500451 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100452
Dave Barachd7cb1b52016-12-09 09:52:16 -0500453 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100454 dst = &adj->sub_type.nbr.next_hop.ip6;
455
456 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100457 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100458 return;
459 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500460 src = ip6_interface_address_matching_destination (im, dst,
461 adj->rewrite_header.
462 sw_if_index, &ia);
463 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100464 {
465 return;
466 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100467
Dave Barachd7cb1b52016-12-09 09:52:16 -0500468 h = vlib_packet_template_get_packet (vm,
469 &im->discover_neighbor_packet_template,
470 &bi);
John Lo084606b2018-06-19 15:27:48 -0400471 if (!h)
472 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100473
Dave Barachd7cb1b52016-12-09 09:52:16 -0500474 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100475
476 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
477 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
478 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
479 h->ip.src_address = src[0];
480 h->neighbor.target_address = dst[0];
481
482 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500483 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100484
485 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500486 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
487 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100488
489 b = vlib_get_buffer (vm, bi);
490 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500491 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100492
493 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500494 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
495 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100496
497 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500498 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
499 u32 *to_next = vlib_frame_vector_args (f);
500 to_next[0] = bi;
501 f->n_vectors = 1;
502 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100503 }
504}
505
506static void
507ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
508{
509 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
510 ethernet_build_rewrite (vnet_get_main (),
511 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500512 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100513 nbr->link_layer_address));
514}
515
516static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000517ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100518{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500519 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000520
Dave Barachd7cb1b52016-12-09 09:52:16 -0500521 adj_nbr_update_rewrite (ai,
522 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
523 ethernet_build_rewrite (vnet_get_main (),
524 adj->rewrite_header.
525 sw_if_index,
526 adj_get_link_type (ai),
527 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100528}
529
530#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
531{ \
532 k.sw_if_index = sw_if_index; \
533 k.ip6_address = *addr; \
534 k.pad = 0; \
535}
536
537static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500538ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100539{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500540 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
541 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100542 ip6_neighbor_key_t k;
543 uword *p;
544
Dave Barachd7cb1b52016-12-09 09:52:16 -0500545 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100546
547 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500548 if (p)
549 {
550 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
551 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100552
553 return (n);
554}
555
556static adj_walk_rc_t
557ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
558{
559 ip6_neighbor_t *nbr = ctx;
560
561 ip6_nd_mk_complete (ai, nbr);
562
563 return (ADJ_WALK_RC_CONTINUE);
564}
565
566static adj_walk_rc_t
567ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
568{
Neale Ranns19c68d22016-12-07 15:38:14 +0000569 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100570
571 return (ADJ_WALK_RC_CONTINUE);
572}
573
John Lo4fed5b12018-05-22 03:35:06 -0400574static clib_error_t *
575ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
576 u32 sw_if_index, u32 flags)
577{
578 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
579 ip6_neighbor_t *n;
580 u32 i, *to_delete = 0;
581
582 /* *INDENT-OFF* */
583 pool_foreach (n, nm->neighbor_pool,
584 ({
585 if (n->key.sw_if_index == sw_if_index)
586 vec_add1 (to_delete, n - nm->neighbor_pool);
587 }));
588 /* *INDENT-ON* */
589
590 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
591 {
592 for (i = 0; i < vec_len (to_delete); i++)
593 {
594 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
595 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
596 ip6_nd_mk_complete_walk, n);
597 }
598 }
599 else
600 {
601 for (i = 0; i < vec_len (to_delete); i++)
602 {
603 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
604 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
605 ip6_nd_mk_incomplete_walk, NULL);
606 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
607 continue;
608 ip6_neighbor_adj_fib_remove (n,
609 ip6_fib_table_get_index_for_sw_if_index
610 (n->key.sw_if_index));
611 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
612 pool_put (nm->neighbor_pool, n);
613 }
614 }
615
616 vec_free (to_delete);
617 return 0;
618}
619
620VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
621
Neale Rannsb80c5362016-10-08 13:03:40 +0100622void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500623ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100624{
625 ip6_neighbor_t *nbr;
626 ip_adjacency_t *adj;
627
628 adj = adj_get (ai);
629
630 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
631
Neale Ranns32e1c012016-11-22 17:07:28 +0000632 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100633 {
Ole Trøan438f6302018-02-15 21:56:06 +0000634 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800635 adj_glean_update_rewrite (ai);
636 break;
637 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000638 if (NULL != nbr)
639 {
640 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
641 ip6_nd_mk_complete_walk, nbr);
642 }
643 else
644 {
645 /*
646 * no matching ND entry.
647 * construct the rewrite required to for an ND packet, and stick
648 * that in the adj's pipe to smoke.
649 */
650 adj_nbr_update_rewrite (ai,
651 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
652 ethernet_build_rewrite (vnm,
653 sw_if_index,
654 VNET_LINK_IP6,
655 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
656
657 /*
658 * since the FIB has added this adj for a route, it makes sense it may
659 * want to forward traffic sometime soon. Let's send a speculative ND.
660 * just one. If we were to do periodically that wouldn't be bad either,
661 * but that's more code than i'm prepared to write at this time for
662 * relatively little reward.
663 */
664 ip6_nbr_probe (adj);
665 }
666 break;
Neale Ranns1855b8e2018-07-11 10:31:26 -0700667 case IP_LOOKUP_NEXT_BCAST:
668 adj_nbr_update_rewrite (ai,
669 ADJ_NBR_REWRITE_FLAG_COMPLETE,
670 ethernet_build_rewrite (vnm,
671 sw_if_index,
672 VNET_LINK_IP6,
673 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
674 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000675 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700676 {
677 /*
678 * Construct a partial rewrite from the known ethernet mcast dest MAC
679 */
680 u8 *rewrite;
681 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100682
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700683 rewrite = ethernet_build_rewrite (vnm,
684 sw_if_index,
685 adj->ia_link,
686 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000687
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700688 /*
689 * Complete the remaining fields of the adj's rewrite to direct the
690 * complete of the rewrite at switch time by copying in the IP
691 * dst address's bytes.
Neale Ranns889fe942017-06-01 05:43:19 -0400692 * Ofset is 2 bytes into the desintation address.
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700693 */
694 offset = vec_len (rewrite) - 2;
Neale Ranns889fe942017-06-01 05:43:19 -0400695 adj_mcast_update_rewrite (ai, rewrite, offset);
Neale Ranns32e1c012016-11-22 17:07:28 +0000696
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700697 break;
698 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000699 case IP_LOOKUP_NEXT_DROP:
700 case IP_LOOKUP_NEXT_PUNT:
701 case IP_LOOKUP_NEXT_LOCAL:
702 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800703 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000704 case IP_LOOKUP_NEXT_MIDCHAIN:
705 case IP_LOOKUP_NEXT_ICMP_ERROR:
706 case IP_LOOKUP_N_NEXT:
707 ASSERT (0);
708 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100709 }
710}
711
Neale Ranns15002542017-09-10 04:39:11 -0700712
713static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700714ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700715{
Neale Ranns53da2212018-02-24 02:11:19 -0800716 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
717 {
718 ip6_ll_prefix_t pfx = {
719 .ilp_addr = n->key.ip6_address,
720 .ilp_sw_if_index = n->key.sw_if_index,
721 };
722 n->fib_entry_index =
723 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
724 }
725 else
726 {
727 fib_prefix_t pfx = {
728 .fp_len = 128,
729 .fp_proto = FIB_PROTOCOL_IP6,
730 .fp_addr.ip6 = n->key.ip6_address,
731 };
Neale Ranns15002542017-09-10 04:39:11 -0700732
Neale Ranns53da2212018-02-24 02:11:19 -0800733 n->fib_entry_index =
734 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
735 FIB_ENTRY_FLAG_ATTACHED,
736 DPO_PROTO_IP6, &pfx.fp_addr,
737 n->key.sw_if_index, ~0, 1, NULL,
738 FIB_ROUTE_PATH_FLAG_NONE);
739 }
Neale Ranns15002542017-09-10 04:39:11 -0700740}
741
John Lo4fed5b12018-05-22 03:35:06 -0400742static ip6_neighbor_t *
743force_reuse_neighbor_entry (void)
744{
745 ip6_neighbor_t *n;
746 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
747 u32 count = 0;
748 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
749 if (index == ~0) /* Try again from elt 0 */
750 index = pool_next_index (nm->neighbor_pool, index);
751
752 /* Find a non-static random entry to free up for reuse */
753 do
754 {
755 if ((count++ == 100) || (index == ~0))
756 return NULL; /* give up after 100 entries */
757 n = pool_elt_at_index (nm->neighbor_pool, index);
758 nm->neighbor_delete_rotor = index;
759 index = pool_next_index (nm->neighbor_pool, index);
760 }
761 while (n->flags & IP6_NEIGHBOR_FLAG_STATIC);
762
763 /* Remove ARP entry from its interface and update fib */
764 adj_nbr_walk_nh6 (n->key.sw_if_index,
765 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
766 ip6_neighbor_adj_fib_remove
767 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
768 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
769
770 return n;
771}
772
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773int
774vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500775 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700776 const ip6_address_t * a,
777 const u8 * link_layer_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500778 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800779 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500781 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500783 ip6_neighbor_t *n = 0;
784 int make_new_nd_cache_entry = 1;
785 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500787 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
Damjan Marion586afd72017-04-05 19:18:20 +0200789 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 {
791 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800792 1 /* set new neighbor */ , is_static,
793 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794 return 0;
795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796
797 k.sw_if_index = sw_if_index;
798 k.ip6_address = a[0];
799 k.pad = 0;
800
Ed Warnickecb9cada2015-12-08 15:45:58 -0700801 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500802 if (p)
803 {
804 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
805 /* Refuse to over-write static neighbor entry. */
806 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
John Lo0e6f4d62018-07-13 17:29:58 -0400807 {
808 /* if MAC address match, still check to send event */
809 if (0 == memcmp (n->link_layer_address,
810 link_layer_address, n_bytes_link_layer_address))
811 goto check_customers;
812 return -2;
813 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500814 make_new_nd_cache_entry = 0;
815 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100816
Dave Barachd7cb1b52016-12-09 09:52:16 -0500817 if (make_new_nd_cache_entry)
818 {
John Lo4fed5b12018-05-22 03:35:06 -0400819 if (nm->limit_neighbor_cache_size &&
820 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
821 {
822 n = force_reuse_neighbor_entry ();
823 if (NULL == n)
824 return -2;
825 }
826 else
827 pool_get (nm->neighbor_pool, n);
828
Dave Barachd7cb1b52016-12-09 09:52:16 -0500829 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
830 /* old value */ 0);
831 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700832 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100833
Dave Barachd7cb1b52016-12-09 09:52:16 -0500834 clib_memcpy (n->link_layer_address,
835 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100836
Dave Barachd7cb1b52016-12-09 09:52:16 -0500837 /*
838 * create the adj-fib. the entry in the FIB table for and to the peer.
839 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800840 if (!is_no_fib_entry)
841 {
Neale Ranns53da2212018-02-24 02:11:19 -0800842 ip6_neighbor_adj_fib_add
843 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700844 }
845 else
846 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800847 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
848 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500849 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100850 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500851 {
852 /*
853 * prevent a DoS attack from the data-plane that
854 * spams us with no-op updates to the MAC address
855 */
856 if (0 == memcmp (n->link_layer_address,
857 link_layer_address, n_bytes_link_layer_address))
John Lo7f358b32018-04-28 01:19:24 -0400858 {
859 n->time_last_updated = vlib_time_now (vm);
860 goto check_customers;
861 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100862
Dave Barachd7cb1b52016-12-09 09:52:16 -0500863 clib_memcpy (n->link_layer_address,
864 link_layer_address, n_bytes_link_layer_address);
865 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866
Neale Rannsb80c5362016-10-08 13:03:40 +0100867 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400868 n->time_last_updated = vlib_time_now (vm);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100869 if (is_static)
John Lo4fed5b12018-05-22 03:35:06 -0400870 {
871 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
872 n->flags &= ~IP6_NEIGHBOR_FLAG_DYNAMIC;
873 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100874 else
John Lo4fed5b12018-05-22 03:35:06 -0400875 {
876 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
877 n->flags &= ~IP6_NEIGHBOR_FLAG_STATIC;
878 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100879
Neale Rannsb80c5362016-10-08 13:03:40 +0100880 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
Dave Barach59b25652017-09-10 15:04:27 -0400883check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 /* Customer(s) waiting for this address to be resolved? */
885 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400886 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 {
John Lo1edfba92016-08-27 01:11:57 -0400888 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500889
890 while (next_index != (u32) ~ 0)
891 {
John Lo1edfba92016-08-27 01:11:57 -0400892 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
893 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500894 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400895 next_index = pr->next_index;
896 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500897 }
John Lo1edfba92016-08-27 01:11:57 -0400898
Neale Ranns0bdd3192018-09-07 11:04:52 -0700899 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 }
901
John Lo1edfba92016-08-27 01:11:57 -0400902 /* Customer(s) requesting ND event for this address? */
903 p = mhash_get (&nm->mac_changes_by_address, a);
904 if (p)
905 {
906 next_index = p[0];
907
Dave Barachd7cb1b52016-12-09 09:52:16 -0500908 while (next_index != (u32) ~ 0)
909 {
910 int (*fp) (u32, u8 *, u32, ip6_address_t *);
911 int rv = 1;
912 mc = pool_elt_at_index (nm->mac_changes, next_index);
913 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400914
Dave Barachd7cb1b52016-12-09 09:52:16 -0500915 /* Call the user's data callback, return 1 to suppress dup events */
916 if (fp)
917 rv =
Neale Ranns0bdd3192018-09-07 11:04:52 -0700918 (*fp) (mc->data, (u8 *) link_layer_address, sw_if_index,
919 &ip6a_zero);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500920 /*
921 * Signal the resolver process, as long as the user
922 * says they want to be notified
923 */
924 if (rv == 0)
925 vlib_process_signal_event (vm, mc->node_index,
926 mc->type_opaque, mc->data);
927 next_index = mc->next_index;
928 }
John Lo1edfba92016-08-27 01:11:57 -0400929 }
930
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 return 0;
932}
933
934int
935vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700936 u32 sw_if_index, const ip6_address_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500938 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 ip6_neighbor_t *n;
941 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 int rv = 0;
943
Damjan Marion586afd72017-04-05 19:18:20 +0200944 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 {
Neale Ranns0bdd3192018-09-07 11:04:52 -0700946 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800947 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 return 0;
949 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
951 k.sw_if_index = sw_if_index;
952 k.ip6_address = a[0];
953 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500954
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955 p = mhash_get (&nm->neighbor_index_by_key, &k);
956 if (p == 0)
957 {
958 rv = -1;
959 goto out;
960 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500961
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100963
Neale Rannsb80c5362016-10-08 13:03:40 +0100964 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500965 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400966 ip6_neighbor_adj_fib_remove
967 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100968
John Lo4fed5b12018-05-22 03:35:06 -0400969 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500971
972out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 return rv;
974}
975
Dave Barachd7cb1b52016-12-09 09:52:16 -0500976static void ip6_neighbor_set_unset_rpc_callback
977 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500979 vlib_main_t *vm = vlib_get_main ();
980 if (a->is_add)
981 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800982 a->link_layer_address, 6, a->is_static,
983 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700985 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987
988static int
989ip6_neighbor_sort (void *a1, void *a2)
990{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500991 vnet_main_t *vnm = vnet_get_main ();
992 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500994 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
995 n2->key.sw_if_index);
996 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
998 return cmp;
999}
1000
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001001ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -04001002ip6_neighbors_pool (void)
1003{
1004 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1005 return nm->neighbor_pool;
1006}
1007
1008ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001009ip6_neighbors_entries (u32 sw_if_index)
1010{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001011 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001012 ip6_neighbor_t *n, *ns = 0;
1013
1014 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001015 pool_foreach (n, nm->neighbor_pool,
1016 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001017 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1018 continue;
1019 vec_add1 (ns, n[0]);
1020 }));
1021 /* *INDENT-ON* */
1022
1023 if (ns)
1024 vec_sort_with_function (ns, ip6_neighbor_sort);
1025 return ns;
1026}
1027
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028static clib_error_t *
1029show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001030 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001032 vnet_main_t *vnm = vnet_get_main ();
1033 ip6_neighbor_t *n, *ns;
1034 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035 u32 sw_if_index;
1036
1037 /* Filter entries by interface if given. */
1038 sw_if_index = ~0;
1039 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1040
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001041 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001042 if (ns)
1043 {
Billy McFall46529cd2016-10-14 10:45:49 -04001044 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001045 vec_foreach (n, ns)
1046 {
1047 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -04001048 }
1049 vec_free (ns);
1050 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
1052 return error;
1053}
1054
Billy McFall0683c9c2016-10-13 08:27:31 -04001055/*?
1056 * This command is used to display the adjacent IPv6 hosts found via
1057 * neighbor discovery. Optionally, limit the output to the specified
1058 * interface.
1059 *
1060 * @cliexpar
1061 * Example of how to display the IPv6 neighbor adjacency table:
1062 * @cliexstart{show ip6 neighbors}
1063 * Time Address Flags Link layer Interface
1064 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1065 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1066 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1067 * @cliexend
1068 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1069 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1070 * Time Address Flags Link layer Interface
1071 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1072 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1073 * @cliexend
1074?*/
1075/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1077 .path = "show ip6 neighbors",
1078 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001079 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080};
Billy McFall0683c9c2016-10-13 08:27:31 -04001081/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082
1083static clib_error_t *
1084set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001085 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001087 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088 ip6_address_t addr;
1089 u8 mac_address[6];
1090 int addr_valid = 0;
1091 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001092 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001093 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 u32 sw_if_index;
1095
Dave Barachd7cb1b52016-12-09 09:52:16 -05001096 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097 {
1098 /* intfc, ip6-address, mac-address */
1099 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001100 unformat_vnet_sw_interface, vnm, &sw_if_index,
1101 unformat_ip6_address, &addr,
1102 unformat_ethernet_address, mac_address))
1103 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
1105 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001106 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001107 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001108 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001109 else if (unformat (input, "no-fib-entry"))
1110 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001112 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 }
1114
1115 if (!addr_valid)
1116 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001117
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118 if (!is_del)
1119 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001120 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001121 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122 else
Neale Ranns0bdd3192018-09-07 11:04:52 -07001123 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124 return 0;
1125}
1126
Billy McFall0683c9c2016-10-13 08:27:31 -04001127/*?
1128 * This command is used to manually add an entry to the IPv6 neighbor
1129 * adjacency table. Optionally, the entry can be added as static. It is
1130 * also used to remove an entry from the table. Use the '<em>show ip6
1131 * neighbors</em>' command to display all learned and manually entered entries.
1132 *
1133 * @cliexpar
1134 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1135 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1136 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1137 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1138?*/
1139/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001140VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1141{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 .path = "set ip6 neighbor",
1143 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001144 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145};
Billy McFall0683c9c2016-10-13 08:27:31 -04001146/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147
Dave Barachd7cb1b52016-12-09 09:52:16 -05001148typedef enum
1149{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1151 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1152 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1153} icmp6_neighbor_solicitation_or_advertisement_next_t;
1154
1155static_always_inline uword
1156icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1157 vlib_node_runtime_t * node,
1158 vlib_frame_t * frame,
1159 uword is_solicitation)
1160{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001161 vnet_main_t *vnm = vnet_get_main ();
1162 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001164 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1166 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001167 vlib_node_runtime_t *error_node =
1168 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 int bogus_length;
1170
1171 from = vlib_frame_vector_args (frame);
1172 n_left_from = n_packets;
1173 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001174
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 if (node->flags & VLIB_NODE_FLAG_TRACE)
1176 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1177 /* stride */ 1,
1178 sizeof (icmp6_input_trace_t));
1179
Dave Barachd7cb1b52016-12-09 09:52:16 -05001180 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181 (is_solicitation
1182 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1183 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1184 n_advertisements_sent = 0;
1185
1186 while (n_left_from > 0)
1187 {
1188 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1189
1190 while (n_left_from > 0 && n_left_to_next > 0)
1191 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001192 vlib_buffer_t *p0;
1193 ip6_header_t *ip0;
1194 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1195 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001197 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1198 int is_rewrite0;
1199 u32 ni0;
1200
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201 bi0 = to_next[0] = from[0];
1202
1203 from += 1;
1204 to_next += 1;
1205 n_left_from -= 1;
1206 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001207
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208 p0 = vlib_get_buffer (vm, bi0);
1209 ip0 = vlib_buffer_get_current (p0);
1210 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001211 options_len0 =
1212 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213
1214 error0 = ICMP6_ERROR_NONE;
1215 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001216 ip6_sadd_link_local =
1217 ip6_address_is_link_local_unicast (&ip0->src_address);
1218 ip6_sadd_unspecified =
1219 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220
1221 /* Check that source address is unspecified, link-local or else on-link. */
1222 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1223 {
1224 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
Dave Barachd7cb1b52016-12-09 09:52:16 -05001226 if (ADJ_INDEX_INVALID != src_adj_index0)
1227 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001228 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229
Dave Barachd7cb1b52016-12-09 09:52:16 -05001230 /* Allow all realistic-looking rewrite adjacencies to pass */
1231 ni0 = adj0->lookup_next_index;
1232 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1233 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001234
Dave Barachd7cb1b52016-12-09 09:52:16 -05001235 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1236 || !is_rewrite0)
1237 ?
1238 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1239 : error0);
1240 }
1241 else
1242 {
1243 error0 =
1244 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1245 }
1246 }
1247
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248 o0 = (void *) (h0 + 1);
1249 o0 = ((options_len0 == 8 && o0->header.type == option_type
1250 && o0->header.n_data_u64s == 1) ? o0 : 0);
1251
1252 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001253 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001254 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001255 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1257 is_solicitation ?
1258 &ip0->src_address :
1259 &h0->target_address,
1260 o0->ethernet_address,
1261 sizeof (o0->ethernet_address),
Neale Ranns53da2212018-02-24 02:11:19 -08001262 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001263 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264
1265 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1266 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001267 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001268 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001269 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001270
Dave Barachd7cb1b52016-12-09 09:52:16 -05001271 fib_index =
1272 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001274 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001275 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001276 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1277 }
1278 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001279 {
Neale Ranns53da2212018-02-24 02:11:19 -08001280 if (ip6_address_is_link_local_unicast (&h0->target_address))
1281 {
1282 fei = ip6_fib_table_lookup_exact_match
1283 (ip6_ll_fib_get (sw_if_index0),
1284 &h0->target_address, 128);
1285 }
1286 else
1287 {
1288 fei = ip6_fib_table_lookup_exact_match (fib_index,
1289 &h0->target_address,
1290 128);
1291 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001292
Neale Ranns3f844d02017-02-18 00:03:54 -08001293 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001294 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001295 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001296 error0 =
1297 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001298 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001299 else
1300 {
1301 if (FIB_ENTRY_FLAG_LOCAL &
1302 fib_entry_get_flags_for_source (fei,
1303 FIB_SOURCE_INTERFACE))
1304 {
1305 /* It's an address that belongs to one of our interfaces
1306 * that's good. */
1307 }
1308 else
1309 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001310 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1311 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001312 {
1313 /* The address was added by IPv6 Proxy ND config.
1314 * We should only respond to these if the NS arrived on
1315 * the link that has a matching covering prefix */
1316 }
1317 else
1318 {
1319 error0 =
1320 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1321 }
1322 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001323 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324 }
1325
1326 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001327 next0 = (error0 != ICMP6_ERROR_NONE
1328 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1329 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001330 else
1331 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001332 next0 = 0;
1333 error0 = error0 == ICMP6_ERROR_NONE ?
1334 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335 }
1336
1337 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1338 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001339 vnet_sw_interface_t *sw_if0;
1340 ethernet_interface_t *eth_if0;
1341 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001342
Dave Barachd7cb1b52016-12-09 09:52:16 -05001343 /* dst address is either source address or the all-nodes mcast addr */
1344 if (!ip6_sadd_unspecified)
1345 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001347 ip6_set_reserved_multicast_address (&ip0->dst_address,
1348 IP6_MULTICAST_SCOPE_link_local,
1349 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001350
1351 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001352 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001353 h0->icmp.type = ICMP6_neighbor_advertisement;
1354
1355 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1356 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001357 eth_if0 =
1358 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359 if (eth_if0 && o0)
1360 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001361 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1362 o0->header.type =
1363 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364 }
1365
1366 h0->advertisement_flags = clib_host_to_net_u32
1367 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1368 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1369
1370 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001371 h0->icmp.checksum =
1372 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1373 &bogus_length);
1374 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001375
Dave Barachd7cb1b52016-12-09 09:52:16 -05001376 /* Reuse current MAC header, copy SMAC to DMAC and
1377 * interface MAC to SMAC */
1378 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1379 eth0 = vlib_buffer_get_current (p0);
1380 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1381 if (eth_if0)
1382 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001383
Dave Barachd7cb1b52016-12-09 09:52:16 -05001384 /* Setup input and output sw_if_index for packet */
1385 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1386 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1387 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1388 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001389
1390 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001391 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392
1393 p0->error = error_node->errors[error0];
1394
1395 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1396 to_next, n_left_to_next,
1397 bi0, next0);
1398 }
1399
1400 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1401 }
1402
1403 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001404 vlib_error_count (vm, error_node->node_index,
1405 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1406 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001407
1408 return frame->n_vectors;
1409}
1410
1411/* for "syslogging" - use elog for now */
1412#define foreach_log_level \
1413 _ (DEBUG, "DEBUG") \
1414 _ (INFO, "INFORMATION") \
1415 _ (NOTICE, "NOTICE") \
1416 _ (WARNING, "WARNING") \
1417 _ (ERR, "ERROR") \
1418 _ (CRIT, "CRITICAL") \
1419 _ (ALERT, "ALERT") \
1420 _ (EMERG, "EMERGENCY")
1421
Dave Barachd7cb1b52016-12-09 09:52:16 -05001422typedef enum
1423{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424#define _(f,s) LOG_##f,
1425 foreach_log_level
1426#undef _
1427} log_level_t;
1428
Dave Barachd7cb1b52016-12-09 09:52:16 -05001429static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430#define _(f,s) s,
1431 foreach_log_level
1432#undef _
1433};
1434
Dave Barachd7cb1b52016-12-09 09:52:16 -05001435static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436
1437static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001438ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439{
1440 /* just use elog for now */
1441 u8 *what;
1442 va_list va;
1443
Dave Barachd7cb1b52016-12-09 09:52:16 -05001444 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1445 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
1447 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001448 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001449 {
1450 what = va_format (0, fmt, &va);
1451
Dave Barachd7cb1b52016-12-09 09:52:16 -05001452 ELOG_TYPE_DECLARE (e) =
1453 {
1454 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1455 struct
1456 {
1457 u32 s[2];
1458 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001459 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001460 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1461 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462 }
1463 va_end (va);
1464 return;
1465}
1466
Juraj Sloboda52574522018-05-03 10:03:50 +02001467clib_error_t *
1468call_ip6_neighbor_callbacks (void *data,
1469 _vnet_ip6_neighbor_function_list_elt_t * elt)
1470{
1471 clib_error_t *error = 0;
1472
1473 while (elt)
1474 {
1475 error = elt->fp (data);
1476 if (error)
1477 return error;
1478 elt = elt->next_ip6_neighbor_function;
1479 }
1480
1481 return error;
1482}
1483
Ed Warnickecb9cada2015-12-08 15:45:58 -07001484/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001485typedef enum
1486{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1488 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1489 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1490 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1491} icmp6_router_solicitation_or_advertisement_next_t;
1492
1493static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001494icmp6_router_solicitation (vlib_main_t * vm,
1495 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001497 vnet_main_t *vnm = vnet_get_main ();
1498 ip6_main_t *im = &ip6_main;
1499 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001501 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001503 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001504 int bogus_length;
1505
1506 icmp6_neighbor_discovery_option_type_t option_type;
1507
Dave Barachd7cb1b52016-12-09 09:52:16 -05001508 vlib_node_runtime_t *error_node =
1509 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510
1511 from = vlib_frame_vector_args (frame);
1512 n_left_from = n_packets;
1513 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001514
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515 if (node->flags & VLIB_NODE_FLAG_TRACE)
1516 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1517 /* stride */ 1,
1518 sizeof (icmp6_input_trace_t));
1519
1520 /* source may append his LL address */
1521 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1522
1523 while (n_left_from > 0)
1524 {
1525 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001526
Ed Warnickecb9cada2015-12-08 15:45:58 -07001527 while (n_left_from > 0 && n_left_to_next > 0)
1528 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001529 vlib_buffer_t *p0;
1530 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001531 ip6_radv_t *radv_info = 0;
1532
Dave Barachd7cb1b52016-12-09 09:52:16 -05001533 icmp6_neighbor_discovery_header_t *h0;
1534 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1535
Ed Warnickecb9cada2015-12-08 15:45:58 -07001536 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001537 u32 is_solicitation = 1, is_dropped = 0;
1538 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001539
1540 bi0 = to_next[0] = from[0];
1541
1542 from += 1;
1543 to_next += 1;
1544 n_left_from -= 1;
1545 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001546
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547 p0 = vlib_get_buffer (vm, bi0);
1548 ip0 = vlib_buffer_get_current (p0);
1549 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001550 options_len0 =
1551 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1552 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1553 is_link_local =
1554 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555
1556 error0 = ICMP6_ERROR_NONE;
1557 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001558
Ed Warnickecb9cada2015-12-08 15:45:58 -07001559 /* check if solicitation (not from nd_timer node) */
1560 if (ip6_address_is_unspecified (&ip0->dst_address))
1561 is_solicitation = 0;
1562
1563 /* Check that source address is unspecified, link-local or else on-link. */
1564 if (!is_unspecified && !is_link_local)
1565 {
1566 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001567
Dave Barachd7cb1b52016-12-09 09:52:16 -05001568 if (ADJ_INDEX_INVALID != src_adj_index0)
1569 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001570 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001571
Dave Barachd7cb1b52016-12-09 09:52:16 -05001572 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1573 ?
1574 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1575 : error0);
1576 }
1577 else
1578 {
1579 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1580 }
1581 }
1582
Ed Warnickecb9cada2015-12-08 15:45:58 -07001583 /* check for source LL option and process */
1584 o0 = (void *) (h0 + 1);
1585 o0 = ((options_len0 == 8
1586 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001587 && o0->header.n_data_u64s == 1) ? o0 : 0);
1588
Ed Warnickecb9cada2015-12-08 15:45:58 -07001589 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001590 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1591 !is_unspecified && !is_link_local))
1592 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001593 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1594 &ip0->src_address,
1595 o0->ethernet_address,
1596 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001597 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001598 }
1599
Ed Warnickecb9cada2015-12-08 15:45:58 -07001600 /* default is to drop */
1601 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001602
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603 if (error0 == ICMP6_ERROR_NONE)
1604 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001605 vnet_sw_interface_t *sw_if0;
1606 ethernet_interface_t *eth_if0;
1607 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608
1609 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1610 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001611 eth_if0 =
1612 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613
1614 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001615 error0 =
1616 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1617 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001618
1619 if (error0 == ICMP6_ERROR_NONE)
1620 {
1621 u32 ri;
1622
1623 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001624 p0->current_length -=
1625 (options_len0 +
1626 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001627
1628 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001629 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1630 sw_if_index0)
1631 {
1632 ri =
1633 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001634
Neale Ranns8e254952018-04-25 05:40:48 -07001635 if (ri != ~0)
1636 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1637 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001638
1639 error0 =
1640 ((!radv_info) ?
1641 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1642 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001643
1644 if (error0 == ICMP6_ERROR_NONE)
1645 {
1646 f64 now = vlib_time_now (vm);
1647
1648 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001649 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650 {
Neale Ranns75152282017-01-09 01:00:45 -08001651 if (0 != radv_info->last_radv_time &&
1652 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001653 MIN_DELAY_BETWEEN_RAS)
1654 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001655 else
1656 radv_info->last_radv_time = now;
1657 }
1658
1659 /* send now */
1660 icmp6_router_advertisement_header_t rh;
1661
1662 rh.icmp.type = ICMP6_router_advertisement;
1663 rh.icmp.code = 0;
1664 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001665
Ed Warnickecb9cada2015-12-08 15:45:58 -07001666 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001667 rh.router_lifetime_in_sec =
1668 clib_host_to_net_u16
1669 (radv_info->adv_router_lifetime_in_sec);
1670 rh.
1671 time_in_msec_between_retransmitted_neighbor_solicitations
1672 =
1673 clib_host_to_net_u32 (radv_info->
1674 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1675 rh.neighbor_reachable_time_in_msec =
1676 clib_host_to_net_u32 (radv_info->
1677 adv_neighbor_reachable_time_in_msec);
1678
1679 rh.flags =
1680 (radv_info->adv_managed_flag) ?
1681 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1682 0;
1683 rh.flags |=
1684 ((radv_info->adv_other_flag) ?
1685 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1686 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687
1688
Dave Barachd7cb1b52016-12-09 09:52:16 -05001689 u16 payload_length =
1690 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691
1692 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001693 vlib_buffer_get_free_list_index
1694 (p0), bi0, (void *) &rh,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001695 sizeof
1696 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001697
Dave Barachd7cb1b52016-12-09 09:52:16 -05001698 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001699 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001700 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1701 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001702
Dave Barachd7cb1b52016-12-09 09:52:16 -05001703 h.header.type =
1704 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001705 h.header.n_data_u64s = 1;
1706
1707 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001708 clib_memcpy (&h.ethernet_address[0],
1709 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710
1711 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001712 vlib_buffer_get_free_list_index
1713 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001714 sizeof
1715 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001716
Dave Barachd7cb1b52016-12-09 09:52:16 -05001717 payload_length +=
1718 sizeof
1719 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001720 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001721
Ed Warnickecb9cada2015-12-08 15:45:58 -07001722 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001723 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001724 {
1725 icmp6_neighbor_discovery_mtu_option_t h;
1726
1727 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001728 h.mtu =
1729 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001730 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1731 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001732
1733 payload_length +=
1734 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001735
1736 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001737 vlib_buffer_get_free_list_index
1738 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001739 sizeof
1740 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001741 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001742
Ed Warnickecb9cada2015-12-08 15:45:58 -07001743 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001744 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001745
Dave Barachd7cb1b52016-12-09 09:52:16 -05001746 /* *INDENT-OFF* */
1747 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1748 ({
1749 if(pr_info->enabled &&
1750 (!pr_info->decrement_lifetime_flag
1751 || (pr_info->pref_lifetime_expires >0)))
1752 {
1753 /* advertise this prefix */
1754 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001755
Dave Barachd7cb1b52016-12-09 09:52:16 -05001756 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1757 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001758
Dave Barachd7cb1b52016-12-09 09:52:16 -05001759 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001760
Dave Barachd7cb1b52016-12-09 09:52:16 -05001761 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1762 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763
Dave Barachd7cb1b52016-12-09 09:52:16 -05001764 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1765 {
1766 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1767 h.preferred_time = 0;
1768 }
1769 else
1770 {
1771 if(pr_info->decrement_lifetime_flag)
1772 {
1773 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1774 (pr_info->valid_lifetime_expires - now) : 0;
1775
1776 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1777 (pr_info->pref_lifetime_expires - now) : 0;
1778 }
1779
1780 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1781 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1782 }
1783 h.unused = 0;
1784
1785 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1786
1787 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1788
1789 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001790 vlib_buffer_get_free_list_index (p0),
Dave Barachd7cb1b52016-12-09 09:52:16 -05001791 bi0,
1792 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1793
1794 }
1795 }));
1796 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001797
1798 /* add additional options before here */
1799
1800 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001801 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001802 {
1803 ip0->dst_address = ip0->src_address;
1804 }
1805 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001806 {
1807 /* target address is all-nodes mcast addr */
1808 ip6_set_reserved_multicast_address
1809 (&ip0->dst_address,
1810 IP6_MULTICAST_SCOPE_link_local,
1811 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001812 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001813
Ed Warnickecb9cada2015-12-08 15:45:58 -07001814 /* source address MUST be the link-local address */
1815 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001816
Dave Barachd7cb1b52016-12-09 09:52:16 -05001817 ip0->hop_limit = 255;
1818 ip0->payload_length =
1819 clib_host_to_net_u16 (payload_length);
1820
1821 icmp6_router_advertisement_header_t *rh0 =
1822 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1823 rh0->icmp.checksum =
1824 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1825 &bogus_length);
1826 ASSERT (bogus_length == 0);
1827
Ed Warnickecb9cada2015-12-08 15:45:58 -07001828 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001829 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001830 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001831
1832 if (is_solicitation)
1833 {
1834 ethernet_header_t *eth0;
1835 /* Reuse current MAC header, copy SMAC to DMAC and
1836 * interface MAC to SMAC */
1837 vlib_buffer_reset (p0);
1838 eth0 = vlib_buffer_get_current (p0);
1839 clib_memcpy (eth0->dst_address, eth0->src_address,
1840 6);
1841 clib_memcpy (eth0->src_address, eth_if0->address,
1842 6);
1843 next0 =
1844 is_dropped ? next0 :
1845 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1846 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1847 sw_if_index0;
1848 }
1849 else
1850 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001851 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001852 if (adj_index0 == 0)
1853 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1854 else
1855 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001856 next0 =
1857 is_dropped ? next0 :
1858 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1859 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1860 adj_index0;
1861 }
1862 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001863 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001864
1865 radv_info->n_solicitations_dropped += is_dropped;
1866 radv_info->n_solicitations_rcvd += is_solicitation;
1867
1868 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001869 {
1870 radv_info->n_advertisements_sent++;
1871 n_advertisements_sent++;
1872 }
1873 }
1874 }
1875 }
1876
1877 p0->error = error_node->errors[error0];
1878
Dave Barachd7cb1b52016-12-09 09:52:16 -05001879 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001880 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001881
Ed Warnickecb9cada2015-12-08 15:45:58 -07001882 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1883 to_next, n_left_to_next,
1884 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001885
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001887
Ed Warnickecb9cada2015-12-08 15:45:58 -07001888 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1889 }
1890
1891 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001892 vlib_error_count (vm, error_node->node_index,
1893 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1894 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001895
1896 return frame->n_vectors;
1897}
1898
1899 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1900static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001901icmp6_router_advertisement (vlib_main_t * vm,
1902 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001903{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001904 vnet_main_t *vnm = vnet_get_main ();
1905 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001907 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001908 u32 n_left_from, n_left_to_next, next_index;
1909 u32 n_advertisements_rcvd = 0;
1910
Dave Barachd7cb1b52016-12-09 09:52:16 -05001911 vlib_node_runtime_t *error_node =
1912 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001913
1914 from = vlib_frame_vector_args (frame);
1915 n_left_from = n_packets;
1916 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001917
Ed Warnickecb9cada2015-12-08 15:45:58 -07001918 if (node->flags & VLIB_NODE_FLAG_TRACE)
1919 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1920 /* stride */ 1,
1921 sizeof (icmp6_input_trace_t));
1922
1923 while (n_left_from > 0)
1924 {
1925 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001926
Ed Warnickecb9cada2015-12-08 15:45:58 -07001927 while (n_left_from > 0 && n_left_to_next > 0)
1928 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001929 vlib_buffer_t *p0;
1930 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001931 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001932 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001933 u32 bi0, options_len0, sw_if_index0, next0, error0;
1934
1935 bi0 = to_next[0] = from[0];
1936
1937 from += 1;
1938 to_next += 1;
1939 n_left_from -= 1;
1940 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001941
Ed Warnickecb9cada2015-12-08 15:45:58 -07001942 p0 = vlib_get_buffer (vm, bi0);
1943 ip0 = vlib_buffer_get_current (p0);
1944 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001945 options_len0 =
1946 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001947
1948 error0 = ICMP6_ERROR_NONE;
1949 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1950
Dave Barachd7cb1b52016-12-09 09:52:16 -05001951 /* Check that source address is link-local */
1952 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001953 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1954
1955 /* default is to drop */
1956 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001957
Ed Warnickecb9cada2015-12-08 15:45:58 -07001958 n_advertisements_rcvd++;
1959
1960 if (error0 == ICMP6_ERROR_NONE)
1961 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001962 vnet_sw_interface_t *sw_if0;
1963 ethernet_interface_t *eth_if0;
1964
Ed Warnickecb9cada2015-12-08 15:45:58 -07001965 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1966 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001967 eth_if0 =
1968 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001969
1970 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001971 error0 =
1972 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1973 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001974
1975 if (error0 == ICMP6_ERROR_NONE)
1976 {
1977 u32 ri;
1978
1979 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001980 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1981 sw_if_index0)
1982 {
1983 ri =
1984 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001985
Neale Ranns8e254952018-04-25 05:40:48 -07001986 if (ri != ~0)
1987 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1988 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001989
1990 error0 =
1991 ((!radv_info) ?
1992 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1993 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001994
1995 if (error0 == ICMP6_ERROR_NONE)
1996 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001997 radv_info->keep_sending_rs = 0;
1998
1999 ra_report_t r;
2000
2001 r.sw_if_index = sw_if_index0;
2002 memcpy (r.router_address, &ip0->src_address, 16);
2003 r.current_hop_limit = h0->current_hop_limit;
2004 r.flags = h0->flags;
2005 r.router_lifetime_in_sec =
2006 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2007 r.neighbor_reachable_time_in_msec =
2008 clib_net_to_host_u32
2009 (h0->neighbor_reachable_time_in_msec);
2010 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2011 r.prefixes = 0;
2012
Ed Warnickecb9cada2015-12-08 15:45:58 -07002013 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002014 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2015 && (h0->current_hop_limit !=
2016 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002017 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002018 ip6_neighbor_syslog (vm, LOG_WARNING,
2019 "our AdvCurHopLimit on %U doesn't agree with %U",
2020 format_vnet_sw_if_index_name,
2021 vnm, sw_if_index0,
2022 format_ip6_address,
2023 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002024 }
2025
Dave Barachd7cb1b52016-12-09 09:52:16 -05002026 if ((h0->flags &
2027 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2028 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002030 ip6_neighbor_syslog (vm, LOG_WARNING,
2031 "our AdvManagedFlag on %U doesn't agree with %U",
2032 format_vnet_sw_if_index_name,
2033 vnm, sw_if_index0,
2034 format_ip6_address,
2035 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002036 }
2037
Dave Barachd7cb1b52016-12-09 09:52:16 -05002038 if ((h0->flags &
2039 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2040 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002041 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002042 ip6_neighbor_syslog (vm, LOG_WARNING,
2043 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2044 format_vnet_sw_if_index_name,
2045 vnm, sw_if_index0,
2046 format_ip6_address,
2047 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002048 }
2049
Dave Barachd7cb1b52016-12-09 09:52:16 -05002050 if ((h0->
2051 time_in_msec_between_retransmitted_neighbor_solicitations
2052 && radv_info->
2053 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2054 && (h0->
2055 time_in_msec_between_retransmitted_neighbor_solicitations
2056 !=
2057 clib_host_to_net_u32 (radv_info->
2058 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002059 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002060 ip6_neighbor_syslog (vm, LOG_WARNING,
2061 "our AdvRetransTimer on %U doesn't agree with %U",
2062 format_vnet_sw_if_index_name,
2063 vnm, sw_if_index0,
2064 format_ip6_address,
2065 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002066 }
2067
Dave Barachd7cb1b52016-12-09 09:52:16 -05002068 if ((h0->neighbor_reachable_time_in_msec &&
2069 radv_info->adv_neighbor_reachable_time_in_msec) &&
2070 (h0->neighbor_reachable_time_in_msec !=
2071 clib_host_to_net_u32
2072 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002073 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002074 ip6_neighbor_syslog (vm, LOG_WARNING,
2075 "our AdvReachableTime on %U doesn't agree with %U",
2076 format_vnet_sw_if_index_name,
2077 vnm, sw_if_index0,
2078 format_ip6_address,
2079 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002080 }
2081
2082 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002083 u8 *opt_hdr = (u8 *) (h0 + 1);
2084 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002085 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002086 icmp6_neighbor_discovery_option_header_t *o0 =
2087 (icmp6_neighbor_discovery_option_header_t *)
2088 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002089 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002090 icmp6_neighbor_discovery_option_type_t option_type =
2091 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002092
Dave Barachd7cb1b52016-12-09 09:52:16 -05002093 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002094 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002095 ip6_neighbor_syslog (vm, LOG_ERR,
2096 "malformed RA packet on %U from %U",
2097 format_vnet_sw_if_index_name,
2098 vnm, sw_if_index0,
2099 format_ip6_address,
2100 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002101 break;
2102 }
2103
Dave Barachd7cb1b52016-12-09 09:52:16 -05002104 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002105 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002106 ip6_neighbor_syslog (vm, LOG_ERR,
2107 " zero length option in RA on %U from %U",
2108 format_vnet_sw_if_index_name,
2109 vnm, sw_if_index0,
2110 format_ip6_address,
2111 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002112 break;
2113 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002114 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002116 ip6_neighbor_syslog (vm, LOG_ERR,
2117 "option length in RA packet greater than total length on %U from %U",
2118 format_vnet_sw_if_index_name,
2119 vnm, sw_if_index0,
2120 format_ip6_address,
2121 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002122 break;
2123 }
2124
2125 options_len0 -= opt_len;
2126 opt_hdr += opt_len;
2127
Dave Barachd7cb1b52016-12-09 09:52:16 -05002128 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002129 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002130 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2131 {
2132 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2133 * h =
2134 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2135 *) (o0);
2136
2137 if (opt_len < sizeof (*h))
2138 break;
2139
2140 memcpy (r.slla, h->ethernet_address, 6);
2141 }
2142 break;
2143
Ed Warnickecb9cada2015-12-08 15:45:58 -07002144 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002145 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002146 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002147 (icmp6_neighbor_discovery_mtu_option_t
2148 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002149
Dave Barachd7cb1b52016-12-09 09:52:16 -05002150 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002151 break;
2152
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002153 r.mtu = clib_net_to_host_u32 (h->mtu);
2154
Dave Barachd7cb1b52016-12-09 09:52:16 -05002155 if ((h->mtu && radv_info->adv_link_mtu) &&
2156 (h->mtu !=
2157 clib_host_to_net_u32
2158 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002159 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002160 ip6_neighbor_syslog (vm, LOG_WARNING,
2161 "our AdvLinkMTU on %U doesn't agree with %U",
2162 format_vnet_sw_if_index_name,
2163 vnm, sw_if_index0,
2164 format_ip6_address,
2165 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002166 }
2167 }
2168 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002169
Ed Warnickecb9cada2015-12-08 15:45:58 -07002170 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2171 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002172 icmp6_neighbor_discovery_prefix_information_option_t
2173 * h =
2174 (icmp6_neighbor_discovery_prefix_information_option_t
2175 *) (o0);
2176
Ed Warnickecb9cada2015-12-08 15:45:58 -07002177 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002178 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002179 u32 preferred, valid;
2180
Dave Barachd7cb1b52016-12-09 09:52:16 -05002181 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002182 break;
2183
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002184 vec_validate (r.prefixes,
2185 vec_len (r.prefixes));
2186 ra_report_prefix_info_t *prefix =
2187 vec_elt_at_index (r.prefixes,
2188 vec_len (r.prefixes) - 1);
2189
Dave Barachd7cb1b52016-12-09 09:52:16 -05002190 preferred =
2191 clib_net_to_host_u32 (h->preferred_time);
2192 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002193
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002194 prefix->preferred_time = preferred;
2195 prefix->valid_time = valid;
2196 prefix->flags = h->flags & 0xc0;
2197 prefix->dst_address_length =
2198 h->dst_address_length;
2199 prefix->dst_address = h->dst_address;
2200
Ed Warnickecb9cada2015-12-08 15:45:58 -07002201 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002202 /* *INDENT-OFF* */
2203 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2204 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002205
Dave Barachd7cb1b52016-12-09 09:52:16 -05002206 ip6_address_t mask;
2207 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2208
2209 if(pr_info->enabled &&
2210 (pr_info->prefix_len == h->dst_address_length) &&
2211 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2212 {
2213 /* found it */
2214 if(!pr_info->decrement_lifetime_flag &&
2215 valid != pr_info->adv_valid_lifetime_in_secs)
2216 {
2217 ip6_neighbor_syslog(vm, LOG_WARNING,
2218 "our ADV validlifetime on %U for %U does not agree with %U",
2219 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2220 format_ip6_address, &h->dst_address);
2221 }
2222 if(!pr_info->decrement_lifetime_flag &&
2223 preferred != pr_info->adv_pref_lifetime_in_secs)
2224 {
2225 ip6_neighbor_syslog(vm, LOG_WARNING,
2226 "our ADV preferredlifetime on %U for %U does not agree with %U",
2227 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2228 format_ip6_address, &h->dst_address);
2229 }
2230 }
2231 break;
2232 }));
2233 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002234 break;
2235 }
2236 default:
2237 /* skip this one */
2238 break;
2239 }
2240 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002241 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002242 }
2243 }
2244 }
2245
2246 p0->error = error_node->errors[error0];
2247
Dave Barachd7cb1b52016-12-09 09:52:16 -05002248 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002249 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002250
Ed Warnickecb9cada2015-12-08 15:45:58 -07002251 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2252 to_next, n_left_to_next,
2253 bi0, next0);
2254 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002255
Ed Warnickecb9cada2015-12-08 15:45:58 -07002256 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2257 }
2258
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002259 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002260 vlib_error_count (vm, error_node->node_index,
2261 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2262 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002263
2264 return frame->n_vectors;
2265}
2266
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002267static inline f64
2268random_f64_from_to (f64 from, f64 to)
2269{
2270 static u32 seed = 0;
2271 static u8 seed_set = 0;
2272 if (!seed_set)
2273 {
2274 seed = random_default_seed ();
2275 seed_set = 1;
2276 }
2277 return random_f64 (&seed) * (to - from) + from;
2278}
2279
2280static inline u8
2281get_mac_address (u32 sw_if_index, u8 * address)
2282{
2283 vnet_main_t *vnm = vnet_get_main ();
2284 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2285 if (!hw_if->hw_address)
2286 return 1;
2287 clib_memcpy (address, hw_if->hw_address, 6);
2288 return 0;
2289}
2290
2291static inline vlib_buffer_t *
2292create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2293{
2294 u32 bi0;
2295 vlib_buffer_t *p0;
2296 vlib_buffer_free_list_t *fl;
2297 icmp6_router_solicitation_header_t *rh;
2298 u16 payload_length;
2299 int bogus_length;
2300 u32 sw_if_index;
2301
2302 sw_if_index = radv_info->sw_if_index;
2303
2304 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2305 {
2306 clib_warning ("buffer allocation failure");
2307 return 0;
2308 }
2309
2310 p0 = vlib_get_buffer (vm, bi0);
2311 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2312 vlib_buffer_init_for_free_list (p0, fl);
2313 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2314 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2315
2316 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2317 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2318
2319 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2320
2321 rh = vlib_buffer_get_current (p0);
2322 p0->current_length = sizeof (*rh);
2323
2324 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2325 rh->neighbor.icmp.code = 0;
2326 rh->neighbor.icmp.checksum = 0;
2327 rh->neighbor.reserved_must_be_zero = 0;
2328
2329 rh->link_layer_option.header.type =
2330 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2331 if (0 != get_mac_address (sw_if_index,
2332 rh->link_layer_option.ethernet_address))
2333 {
2334 clib_warning ("interface with sw_if_index %u has no mac address",
2335 sw_if_index);
2336 vlib_buffer_free (vm, &bi0, 1);
2337 return 0;
2338 }
2339 rh->link_layer_option.header.n_data_u64s = 1;
2340
2341 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2342
2343 rh->ip.ip_version_traffic_class_and_flow_label =
2344 clib_host_to_net_u32 (0x6 << 28);
2345 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2346 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2347 rh->ip.hop_limit = 255;
2348 rh->ip.src_address = radv_info->link_local_address;
2349 /* set address ff02::2 */
2350 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02L << 48);
2351 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2352
2353 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2354 &rh->ip,
2355 &bogus_length);
2356
2357 return p0;
2358}
2359
2360static inline void
2361stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2362{
2363 u32 bi0;
2364
2365 ra->keep_sending_rs = 0;
2366 if (ra->buffer)
2367 {
2368 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2369 vlib_buffer_free (vm, &bi0, 1);
2370 ra->buffer = 0;
2371 }
2372}
2373
2374static inline bool
2375check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2376 f64 * due_time)
2377{
2378 vlib_buffer_t *p0;
2379 vlib_frame_t *f;
2380 u32 *to_next;
2381 u32 next_index;
2382 vlib_buffer_t *c0;
2383 u32 ci0;
2384
2385 icmp6_send_router_solicitation_params_t *params;
2386
2387 if (!radv_info->keep_sending_rs)
2388 return false;
2389
2390 params = &radv_info->params;
2391
2392 if (radv_info->due_time > current_time)
2393 {
2394 *due_time = radv_info->due_time;
2395 return true;
2396 }
2397
2398 p0 = radv_info->buffer;
2399
2400 next_index = ip6_rewrite_mcast_node.index;
2401
2402 c0 = vlib_buffer_copy (vm, p0);
2403 ci0 = vlib_get_buffer_index (vm, c0);
2404
2405 f = vlib_get_frame_to_node (vm, next_index);
2406 to_next = vlib_frame_vector_args (f);
2407 to_next[0] = ci0;
2408 f->n_vectors = 1;
2409 vlib_put_frame_to_node (vm, next_index, f);
2410
2411 if (params->mrc != 0 && --radv_info->n_left == 0)
2412 stop_sending_rs (vm, radv_info);
2413 else
2414 {
2415 radv_info->sleep_interval =
2416 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2417 if (radv_info->sleep_interval > params->mrt)
2418 radv_info->sleep_interval =
2419 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2420
2421 radv_info->due_time = current_time + radv_info->sleep_interval;
2422
2423 if (params->mrd != 0
2424 && current_time > radv_info->start_time + params->mrd)
2425 stop_sending_rs (vm, radv_info);
2426 else
2427 *due_time = radv_info->due_time;
2428 }
2429
2430 return radv_info->keep_sending_rs;
2431}
2432
2433static uword
2434send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2435 vlib_frame_t * f0)
2436{
2437 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2438 ip6_radv_t *radv_info;
2439 uword *event_data = 0;
2440 f64 sleep_time = 1e9;
2441 f64 current_time;
2442 f64 due_time;
2443 f64 dt = 0;
2444
2445 while (true)
2446 {
2447 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2448 vlib_process_get_events (vm, &event_data);
2449 vec_reset_length (event_data);
2450
2451 current_time = vlib_time_now (vm);
2452 do
2453 {
2454 due_time = current_time + 1e9;
2455 /* *INDENT-OFF* */
2456 pool_foreach (radv_info, nm->if_radv_pool,
2457 ({
2458 if (check_send_rs (vm, radv_info, current_time, &dt)
2459 && (dt < due_time))
2460 due_time = dt;
2461 }));
2462 /* *INDENT-ON* */
2463 current_time = vlib_time_now (vm);
2464 }
2465 while (due_time < current_time);
2466
2467 sleep_time = due_time - current_time;
2468 }
2469
2470 return 0;
2471}
2472
2473/* *INDENT-OFF* */
2474VLIB_REGISTER_NODE (send_rs_process_node) = {
2475 .function = send_rs_process,
2476 .type = VLIB_NODE_TYPE_PROCESS,
2477 .name = "send-rs-process",
2478};
2479/* *INDENT-ON* */
2480
2481void
2482icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2483 icmp6_send_router_solicitation_params_t *
2484 params)
2485{
2486 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2487 u32 rai;
2488 ip6_radv_t *ra = 0;
2489
2490 ASSERT (~0 != sw_if_index);
2491
2492 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2493 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2494
Juraj Sloboda52574522018-05-03 10:03:50 +02002495 stop_sending_rs (vm, ra);
2496
2497 if (!stop)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002498 {
2499 ra->keep_sending_rs = 1;
2500 ra->params = *params;
2501 ra->n_left = params->mrc;
2502 ra->start_time = vlib_time_now (vm);
2503 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2504 ra->due_time = 0; /* send first packet ASAP */
2505 ra->buffer = create_buffer_for_rs (vm, ra);
2506 if (!ra->buffer)
2507 ra->keep_sending_rs = 0;
2508 else
2509 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2510 }
2511}
2512
Neale Ranns87df12d2017-02-18 08:16:41 -08002513/**
2514 * @brief Add a multicast Address to the advertised MLD set
2515 */
2516static void
2517ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2518{
2519 ip6_mldp_group_t *mcast_group_info;
2520 uword *p;
2521
2522 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002523 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002524 mcast_group_info =
2525 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2526
2527 /* add address */
2528 if (!mcast_group_info)
2529 {
2530 /* add */
2531 u32 mi;
2532 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2533
2534 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002535 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002536 0);
2537
2538 mcast_group_info->type = 4;
2539 mcast_group_info->mcast_source_address_pool = 0;
2540 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002541 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002542 sizeof (ip6_address_t));
2543 }
2544}
2545
2546/**
2547 * @brief Delete a multicast Address from the advertised MLD set
2548 */
2549static void
2550ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2551{
2552 ip6_mldp_group_t *mcast_group_info;
2553 uword *p;
2554
2555 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2556 mcast_group_info =
2557 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2558
2559 if (mcast_group_info)
2560 {
2561 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2562 /* old_value */ 0);
2563 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2564 }
2565}
2566
2567/**
2568 * @brief Add a multicast Address to the advertised MLD set
2569 */
2570static void
2571ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2572 ip6_multicast_address_scope_t scope,
2573 ip6_multicast_link_local_group_id_t group)
2574{
2575 ip6_address_t addr;
2576
2577 ip6_set_reserved_multicast_address (&addr, scope, group);
2578
2579 ip6_neighbor_add_mld_prefix (a, &addr);
2580}
2581
2582/**
2583 * @brief create and initialize router advertisement parameters with default
2584 * values for this intfc
2585 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002586u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002587ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002588 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002589{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002590 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2591 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002592 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002593 vnet_sw_interface_t *sw_if0;
2594 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002595
2596 /* lookup radv container - ethernet interfaces only */
2597 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002598 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002599 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2600
Dave Barachd7cb1b52016-12-09 09:52:16 -05002601 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002602 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002603
2604 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2605 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002606 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2607
Dave Barachd7cb1b52016-12-09 09:52:16 -05002608 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002609 {
2610 a = pool_elt_at_index (nm->if_radv_pool, ri);
2611
Dave Barachd7cb1b52016-12-09 09:52:16 -05002612 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002613 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002614 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002615 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002616
Neale Ranns32e1c012016-11-22 17:07:28 +00002617 /* release the lock on the interface's mcast adj */
2618 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002619
Neale Ranns87df12d2017-02-18 08:16:41 -08002620 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002621 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002622 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002623 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002624 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002625 }));
2626 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002627 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002628 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002629 }));
2630 /* *INDENT-ON* */
2631
Neale Ranns87df12d2017-02-18 08:16:41 -08002632 pool_free (a->mldp_group_pool);
2633 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002634
Neale Ranns87df12d2017-02-18 08:16:41 -08002635 mhash_free (&a->address_to_prefix_index);
2636 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002637
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002638 if (a->keep_sending_rs)
2639 a->keep_sending_rs = 0;
2640
Dave Barachd7cb1b52016-12-09 09:52:16 -05002641 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002642 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2643 ri = ~0;
2644 }
2645 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002646 else
2647 {
2648 if (is_add)
2649 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002650 pool_get (nm->if_radv_pool, a);
2651
2652 ri = a - nm->if_radv_pool;
2653 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2654
2655 /* initialize default values (most of which are zero) */
2656 memset (a, 0, sizeof (a[0]));
2657
2658 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002659 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2660 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2661 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2662 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2663
Neale Ranns87df12d2017-02-18 08:16:41 -08002664 /* send ll address source address option */
2665 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002666
2667 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2668 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2669 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2670 a->seed = (u32) clib_cpu_time_now ();
2671 (void) random_u32 (&a->seed);
2672 a->randomizer = clib_cpu_time_now ();
2673 (void) random_u64 (&a->randomizer);
2674
2675 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2676 a->initial_adverts_sent = a->initial_adverts_count - 1;
2677 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2678
2679 /* deafult is to send */
2680 a->send_radv = 1;
2681
2682 /* fill in radv_info for this interface that will be needed later */
Ole Troand7231612018-06-07 10:17:57 +02002683 a->adv_link_mtu =
2684 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002685
2686 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2687
2688 /* fill in default link-local address (this may be overridden) */
2689 ip6_link_local_address_from_ethernet_address
2690 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002691
2692 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2693 sizeof (ip6_address_t));
2694 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2695 sizeof (ip6_address_t));
2696
Neale Ranns32e1c012016-11-22 17:07:28 +00002697 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2698 VNET_LINK_IP6,
2699 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002700
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002701 a->keep_sending_rs = 0;
2702
Dave Barachd7cb1b52016-12-09 09:52:16 -05002703 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002704 ip6_neighbor_add_mld_grp (a,
2705 IP6_MULTICAST_SCOPE_link_local,
2706 IP6_MULTICAST_GROUP_ID_all_hosts);
2707 ip6_neighbor_add_mld_grp (a,
2708 IP6_MULTICAST_SCOPE_link_local,
2709 IP6_MULTICAST_GROUP_ID_all_routers);
2710 ip6_neighbor_add_mld_grp (a,
2711 IP6_MULTICAST_SCOPE_link_local,
2712 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002713 }
2714 }
2715 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002716}
2717
2718/* send an mldpv2 report */
2719static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002720ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002721{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002722 vnet_main_t *vnm = vnet_get_main ();
2723 vlib_main_t *vm = vnm->vlib_main;
2724 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2725 vnet_sw_interface_t *sw_if0;
2726 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002727 u32 ri;
2728 int bogus_length;
2729
Dave Barachd7cb1b52016-12-09 09:52:16 -05002730 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002731 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002732 vlib_buffer_t *b0;
2733 ip6_header_t *ip0;
2734 u32 *to_next;
2735 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002736 u32 bo0;
2737 u32 n_to_alloc = 1;
2738 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002739
Ed Warnickecb9cada2015-12-08 15:45:58 -07002740 icmp6_multicast_listener_report_header_t *rh0;
2741 icmp6_multicast_listener_report_packet_t *rp0;
2742
2743 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2744 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2745 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2746
2747 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2748 return;
2749
2750 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002751 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2752 ~0);
2753
Ed Warnickecb9cada2015-12-08 15:45:58 -07002754 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002755
2756 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002757 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002758
Ed Warnickecb9cada2015-12-08 15:45:58 -07002759 /* send report now - build a mldpv2 report packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002760 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2761 &bo0,
2762 n_to_alloc,
2763 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2764 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002765 {
2766 clib_warning ("buffer allocation failure");
2767 return;
2768 }
2769
2770 b0 = vlib_get_buffer (vm, bo0);
2771
2772 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002773 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002774
Dave Barachd7cb1b52016-12-09 09:52:16 -05002775 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002776
2777 b0->error = ICMP6_ERROR_NONE;
2778
2779 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002780 ip0 = (ip6_header_t *) & rp0->ip;
2781 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002782
Dave Barachd7cb1b52016-12-09 09:52:16 -05002783 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2784
2785 ip0->ip_version_traffic_class_and_flow_label =
2786 clib_host_to_net_u32 (0x6 << 28);
2787
2788 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002789 /* for DEBUG - vnet driver won't seem to emit router alerts */
2790 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2791 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002792
Ed Warnickecb9cada2015-12-08 15:45:58 -07002793 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002794
Ed Warnickecb9cada2015-12-08 15:45:58 -07002795 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002796 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2797 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798
2799 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002800 ip6_set_reserved_multicast_address (&ip0->dst_address,
2801 IP6_MULTICAST_SCOPE_link_local,
2802 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2803
Ed Warnickecb9cada2015-12-08 15:45:58 -07002804 /* add reports here */
2805 ip6_mldp_group_t *m;
2806 int num_addr_records = 0;
2807 icmp6_multicast_address_record_t rr;
2808
2809 /* fill in the hop-by-hop extension header (router alert) info */
2810 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2811 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002812
Ed Warnickecb9cada2015-12-08 15:45:58 -07002813 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2814 rh0->alert.len = 2;
2815 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002816
Ed Warnickecb9cada2015-12-08 15:45:58 -07002817 rh0->pad.type = 1;
2818 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002819
Ed Warnickecb9cada2015-12-08 15:45:58 -07002820 rh0->icmp.checksum = 0;
2821
Dave Barachd7cb1b52016-12-09 09:52:16 -05002822 /* *INDENT-OFF* */
2823 pool_foreach (m, radv_info->mldp_group_pool,
2824 ({
2825 rr.type = m->type;
2826 rr.aux_data_len_u32s = 0;
2827 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2828 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002829
Dave Barachd7cb1b52016-12-09 09:52:16 -05002830 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831
Dave Barachd7cb1b52016-12-09 09:52:16 -05002832 vlib_buffer_add_data
Damjan Marion072401e2017-07-13 18:53:27 +02002833 (vm, vlib_buffer_get_free_list_index (b0), bo0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002834 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002835
Dave Barachd7cb1b52016-12-09 09:52:16 -05002836 payload_length += sizeof( icmp6_multicast_address_record_t);
2837 }));
2838 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002839
2840 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002841 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2842
Ed Warnickecb9cada2015-12-08 15:45:58 -07002843 /* update lengths */
2844 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2845
Dave Barachd7cb1b52016-12-09 09:52:16 -05002846 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2847 &bogus_length);
2848 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002849
Dave Barachd7cb1b52016-12-09 09:52:16 -05002850 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002852 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002854 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002855 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002856
Neale Ranns32e1c012016-11-22 17:07:28 +00002857 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002858 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002859
Neale Ranns32e1c012016-11-22 17:07:28 +00002860 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002861
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862 f = vlib_get_frame_to_node (vm, node->index);
2863 to_next = vlib_frame_vector_args (f);
2864 to_next[0] = bo0;
2865 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002866
Ed Warnickecb9cada2015-12-08 15:45:58 -07002867 vlib_put_frame_to_node (vm, node->index, f);
2868 return;
2869}
2870
Dave Barachd7cb1b52016-12-09 09:52:16 -05002871/* *INDENT-OFF* */
2872VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2873{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874 .function = icmp6_router_solicitation,
2875 .name = "icmp6-router-solicitation",
2876
2877 .vector_size = sizeof (u32),
2878
2879 .format_trace = format_icmp6_input_trace,
2880
2881 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2882 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002883 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002884 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002885 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2886 },
2887};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002888/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002889
2890/* send a RA or update the timer info etc.. */
2891static uword
2892ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002893 vlib_node_runtime_t * node,
2894 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002895{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002896 vnet_main_t *vnm = vnet_get_main ();
2897 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2898 ip6_radv_t *radv_info;
2899 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002900 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002901 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002902 u32 *to_next = 0;
2903 u32 bo0;
2904 icmp6_router_solicitation_header_t *h0;
2905 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002906 f64 now = vlib_time_now (vm);
2907
2908 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002909 /* *INDENT-OFF* */
2910 pool_foreach (radv_info, nm->if_radv_pool,
2911 ({
2912 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2913 {
2914 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2915 radv_info->next_multicast_time = now;
2916 radv_info->last_multicast_time = now;
2917 radv_info->last_radv_time = 0;
2918 radv_info->all_routers_mcast = 0;
2919 continue;
2920 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002921
Dave Barachd7cb1b52016-12-09 09:52:16 -05002922 /* Make sure that we've joined the all-routers multicast group */
2923 if(!radv_info->all_routers_mcast)
2924 {
2925 /* send MDLP_REPORT_EVENT message */
2926 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2927 radv_info->all_routers_mcast = 1;
2928 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002929
Dave Barachd7cb1b52016-12-09 09:52:16 -05002930 /* is it time to send a multicast RA on this interface? */
2931 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2932 {
2933 u32 n_to_alloc = 1;
2934 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002935
Dave Barachd7cb1b52016-12-09 09:52:16 -05002936 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2937 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002938
Dave Barachd7cb1b52016-12-09 09:52:16 -05002939 /* multicast send - compute next multicast send time */
2940 if( radv_info->initial_adverts_sent > 0)
2941 {
2942 radv_info->initial_adverts_sent--;
2943 if(rfn > radv_info-> initial_adverts_interval)
2944 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002945
Dave Barachd7cb1b52016-12-09 09:52:16 -05002946 /* check to see if we are ceasing to send */
2947 if( radv_info->initial_adverts_sent == 0)
2948 if(radv_info->cease_radv)
2949 radv_info->send_radv = 0;
2950 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002951
Dave Barachd7cb1b52016-12-09 09:52:16 -05002952 radv_info->next_multicast_time = rfn + now;
2953 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002954
Dave Barachd7cb1b52016-12-09 09:52:16 -05002955 /* send advert now - build a "solicted" router advert with unspecified source address */
2956 n_allocated = vlib_buffer_alloc_from_free_list
2957 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002958
Dave Barachd7cb1b52016-12-09 09:52:16 -05002959 if (PREDICT_FALSE(n_allocated == 0))
2960 {
2961 clib_warning ("buffer allocation failure");
2962 continue;
2963 }
2964 b0 = vlib_get_buffer (vm, bo0);
2965 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2966 b0->error = ICMP6_ERROR_NONE;
2967 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2968
2969 h0 = vlib_buffer_get_current (b0);
2970
2971 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2972
2973 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2974 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2975 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2976 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2977 h0->ip.hop_limit = 255;
2978
2979 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2980 h0->ip.src_address.as_u64[0] = 0;
2981 h0->ip.src_address.as_u64[1] = 0;
2982
2983 h0->ip.dst_address.as_u64[0] = 0;
2984 h0->ip.dst_address.as_u64[1] = 0;
2985
2986 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2987
2988 if (PREDICT_FALSE(f == 0))
2989 {
2990 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2991 to_next = vlib_frame_vector_args (f);
2992 n_left_to_next = VLIB_FRAME_SIZE;
2993 n_this_frame = 0;
2994 }
2995
2996 n_this_frame++;
2997 n_left_to_next--;
2998 to_next[0] = bo0;
2999 to_next += 1;
3000
3001 if (PREDICT_FALSE(n_left_to_next == 0))
3002 {
3003 f->n_vectors = n_this_frame;
3004 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3005 f = 0;
3006 }
3007 }
3008 }));
3009 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003010
3011 if (f)
3012 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003013 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003014 f->n_vectors = n_this_frame;
3015 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3016 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003017 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003018}
3019
3020static uword
3021ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3022 vlib_node_runtime_t * node,
3023 vlib_frame_t * frame)
3024{
3025 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003026 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003027
3028 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003029
Ed Warnickecb9cada2015-12-08 15:45:58 -07003030 while (1)
3031 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003032 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003033
Dave Barachd7cb1b52016-12-09 09:52:16 -05003034 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003035
Dave Barachd7cb1b52016-12-09 09:52:16 -05003036 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003037 {
3038 /* No events found: timer expired. */
3039 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003040 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003041 }
3042 else
3043 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003044 switch (event_type)
3045 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003046
Dave Barachd7cb1b52016-12-09 09:52:16 -05003047 case ICMP6_ND_EVENT_INIT:
3048 break;
3049
3050 case ~0:
3051 break;
3052
3053 default:
3054 ASSERT (0);
3055 }
3056
Ed Warnickecb9cada2015-12-08 15:45:58 -07003057 if (event_data)
3058 _vec_len (event_data) = 0;
3059 }
3060 }
3061 return frame->n_vectors;
3062}
3063
Dave Barachd7cb1b52016-12-09 09:52:16 -05003064/* *INDENT-OFF* */
3065VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3066{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003067 .function = icmp6_router_advertisement,
3068 .name = "icmp6-router-advertisement",
3069
3070 .vector_size = sizeof (u32),
3071
3072 .format_trace = format_icmp6_input_trace,
3073
3074 .n_next_nodes = 1,
3075 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003076 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003077 },
3078};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003079/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003080
3081vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3082
3083 .function = ip6_icmp_neighbor_discovery_event_process,
3084 .name = "ip6-icmp-neighbor-discovery-event-process",
3085 .type = VLIB_NODE_TYPE_PROCESS,
3086};
3087
3088static uword
3089icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003090 vlib_node_runtime_t * node, vlib_frame_t * frame)
3091{
3092 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3093 /* is_solicitation */
3094 1);
3095}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003096
3097static uword
3098icmp6_neighbor_advertisement (vlib_main_t * vm,
3099 vlib_node_runtime_t * node,
3100 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003101{
3102 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3103 /* is_solicitation */
3104 0);
3105}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003106
Dave Barachd7cb1b52016-12-09 09:52:16 -05003107/* *INDENT-OFF* */
3108VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3109{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003110 .function = icmp6_neighbor_solicitation,
3111 .name = "icmp6-neighbor-solicitation",
3112
3113 .vector_size = sizeof (u32),
3114
3115 .format_trace = format_icmp6_input_trace,
3116
3117 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3118 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003119 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003120 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3121 },
3122};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003123/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003124
Dave Barachd7cb1b52016-12-09 09:52:16 -05003125/* *INDENT-OFF* */
3126VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3127{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003128 .function = icmp6_neighbor_advertisement,
3129 .name = "icmp6-neighbor-advertisement",
3130
3131 .vector_size = sizeof (u32),
3132
3133 .format_trace = format_icmp6_input_trace,
3134
3135 .n_next_nodes = 1,
3136 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003137 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003138 },
3139};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003140/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003141
Neale Rannsc7b8f202018-04-25 06:34:31 -07003142typedef enum
3143{
3144 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3145 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3146 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3147} ip6_discover_neighbor_next_t;
3148
3149typedef enum
3150{
3151 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3152 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3153 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3154} ip6_discover_neighbor_error_t;
3155
3156static uword
3157ip6_discover_neighbor_inline (vlib_main_t * vm,
3158 vlib_node_runtime_t * node,
3159 vlib_frame_t * frame, int is_glean)
3160{
3161 vnet_main_t *vnm = vnet_get_main ();
3162 ip6_main_t *im = &ip6_main;
3163 ip_lookup_main_t *lm = &im->lookup_main;
3164 u32 *from, *to_next_drop;
3165 uword n_left_from, n_left_to_next_drop;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003166 f64 time_now;
Dave Barach49433ad2018-08-08 17:59:03 -04003167 u64 seed;
3168 u32 thread_index = vm->thread_index;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003169 int bogus_length;
3170 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3171
3172 if (node->flags & VLIB_NODE_FLAG_TRACE)
3173 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3174
3175 time_now = vlib_time_now (vm);
Dave Barach49433ad2018-08-08 17:59:03 -04003176 if (time_now - im->nd_throttle_last_seed_change_time[thread_index] > 1e-3)
Neale Rannsc7b8f202018-04-25 06:34:31 -07003177 {
Dave Barach49433ad2018-08-08 17:59:03 -04003178 (void) random_u64 (&im->nd_throttle_seeds[thread_index]);
3179 memset (im->nd_throttle_bitmaps[thread_index], 0,
3180 ND_THROTTLE_BITS / BITS (u8));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003181
Dave Barach49433ad2018-08-08 17:59:03 -04003182 im->nd_throttle_last_seed_change_time[thread_index] = time_now;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003183 }
Dave Barach49433ad2018-08-08 17:59:03 -04003184 seed = im->nd_throttle_seeds[thread_index];
Neale Rannsc7b8f202018-04-25 06:34:31 -07003185
3186 from = vlib_frame_vector_args (frame);
3187 n_left_from = frame->n_vectors;
3188
3189 while (n_left_from > 0)
3190 {
3191 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3192 to_next_drop, n_left_to_next_drop);
3193
3194 while (n_left_from > 0 && n_left_to_next_drop > 0)
3195 {
3196 vlib_buffer_t *p0;
3197 ip6_header_t *ip0;
Dave Barach49433ad2018-08-08 17:59:03 -04003198 u32 pi0, adj_index0, w0, sw_if_index0, drop0;
3199 u64 r0;
3200 uword m0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003201 ip_adjacency_t *adj0;
3202 vnet_hw_interface_t *hw_if0;
3203 ip6_radv_t *radv_info;
3204 u32 next0;
3205
3206 pi0 = from[0];
3207
3208 p0 = vlib_get_buffer (vm, pi0);
3209
3210 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3211
3212 ip0 = vlib_buffer_get_current (p0);
3213
3214 adj0 = adj_get (adj_index0);
3215
3216 if (!is_glean)
3217 {
3218 ip0->dst_address.as_u64[0] =
3219 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3220 ip0->dst_address.as_u64[1] =
3221 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3222 }
3223
Neale Rannsc7b8f202018-04-25 06:34:31 -07003224 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3225 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3226
Dave Barach49433ad2018-08-08 17:59:03 -04003227 /* Compute the ND throttle bitmap hash */
3228 r0 = ip0->dst_address.as_u64[0] ^ ip0->dst_address.as_u64[1] ^ seed;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003229
Dave Barach49433ad2018-08-08 17:59:03 -04003230 /* Find the word and bit */
3231 r0 &= ND_THROTTLE_BITS - 1;
3232 w0 = r0 / BITS (uword);
3233 m0 = (uword) 1 << (r0 % BITS (uword));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003234
Dave Barach49433ad2018-08-08 17:59:03 -04003235 /* If the bit is set, drop the ND request */
3236 drop0 = (im->nd_throttle_bitmaps[thread_index][w0] & m0) != 0;
3237 /* (unconditionally) mark the bit "inuse" */
3238 im->nd_throttle_bitmaps[thread_index][w0] |= m0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003239
3240 from += 1;
3241 n_left_from -= 1;
3242 to_next_drop[0] = pi0;
3243 to_next_drop += 1;
3244 n_left_to_next_drop -= 1;
3245
3246 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3247
3248 /* If the interface is link-down, drop the pkt */
3249 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3250 drop0 = 1;
3251
3252 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3253 {
3254 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3255
3256 if (ri != ~0)
3257 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3258 else
3259 drop0 = 1;
3260 }
3261 else
3262 drop0 = 1;
3263
3264 /*
3265 * the adj has been updated to a rewrite but the node the DPO that got
3266 * us here hasn't - yet. no big deal. we'll drop while we wait.
3267 */
3268 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3269 drop0 = 1;
3270
3271 p0->error =
3272 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3273 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3274
3275 if (drop0)
3276 continue;
3277
3278 {
3279 u32 bi0 = 0;
3280 icmp6_neighbor_solicitation_header_t *h0;
3281 vlib_buffer_t *b0;
3282
3283 h0 = vlib_packet_template_get_packet
3284 (vm, &im->discover_neighbor_packet_template, &bi0);
John Lo084606b2018-06-19 15:27:48 -04003285 if (!h0)
3286 continue;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003287
3288 /*
3289 * Build ethernet header.
3290 * Choose source address based on destination lookup
3291 * adjacency.
3292 */
3293 if (!ip6_src_address_for_packet (lm,
3294 sw_if_index0,
3295 &ip0->dst_address,
3296 &h0->ip.src_address))
3297 {
3298 /* There is no address on the interface */
3299 p0->error =
3300 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3301 vlib_buffer_free (vm, &bi0, 1);
3302 continue;
3303 }
3304
3305 /*
3306 * Destination address is a solicited node multicast address.
3307 * We need to fill in
3308 * the low 24 bits with low 24 bits of target's address.
3309 */
3310 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3311 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3312 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3313
3314 h0->neighbor.target_address = ip0->dst_address;
3315
3316 clib_memcpy (h0->link_layer_option.ethernet_address,
3317 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3318
3319 /* $$$$ appears we need this; why is the checksum non-zero? */
3320 h0->neighbor.icmp.checksum = 0;
3321 h0->neighbor.icmp.checksum =
3322 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3323 &bogus_length);
3324
3325 ASSERT (bogus_length == 0);
3326
3327 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3328 b0 = vlib_get_buffer (vm, bi0);
3329 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3330 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3331
3332 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3333 radv_info->mcast_adj_index;
3334
3335 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3336 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3337
3338 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3339 }
3340 }
3341
3342 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3343 n_left_to_next_drop);
3344 }
3345
3346 return frame->n_vectors;
3347}
3348
3349static uword
3350ip6_discover_neighbor (vlib_main_t * vm,
3351 vlib_node_runtime_t * node, vlib_frame_t * frame)
3352{
3353 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3354}
3355
3356static uword
3357ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3358{
3359 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3360}
3361
3362static char *ip6_discover_neighbor_error_strings[] = {
3363 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3364 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3365 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3366 = "no source address for ND solicitation",
3367};
3368
3369/* *INDENT-OFF* */
3370VLIB_REGISTER_NODE (ip6_glean_node) =
3371{
3372 .function = ip6_glean,
3373 .name = "ip6-glean",
3374 .vector_size = sizeof (u32),
3375 .format_trace = format_ip6_forward_next_trace,
3376 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3377 .error_strings = ip6_discover_neighbor_error_strings,
3378 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3379 .next_nodes =
3380 {
3381 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3382 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3383 },
3384};
3385VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3386{
3387 .function = ip6_discover_neighbor,
3388 .name = "ip6-discover-neighbor",
3389 .vector_size = sizeof (u32),
3390 .format_trace = format_ip6_forward_next_trace,
3391 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3392 .error_strings = ip6_discover_neighbor_error_strings,
3393 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3394 .next_nodes =
3395 {
3396 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3397 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3398 },
3399};
3400/* *INDENT-ON* */
3401
Dave Barachd7cb1b52016-12-09 09:52:16 -05003402/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003403int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003404ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3405 u8 suppress, u8 managed, u8 other,
3406 u8 ll_option, u8 send_unicast, u8 cease,
3407 u8 use_lifetime, u32 lifetime,
3408 u32 initial_count, u32 initial_interval,
3409 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003410{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003411 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3412 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003413 u32 ri;
3414
3415 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003416 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3417 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003418 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003419 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003420
Dave Barachd7cb1b52016-12-09 09:52:16 -05003421 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003422 {
3423
Dave Barachd7cb1b52016-12-09 09:52:16 -05003424 ip6_radv_t *radv_info;
3425 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003426
Dave Barachd7cb1b52016-12-09 09:52:16 -05003427 if ((max_interval != 0) && (min_interval == 0))
3428 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003429
Dave Barachd7cb1b52016-12-09 09:52:16 -05003430 max_interval =
3431 (max_interval !=
3432 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3433 radv_info->max_radv_interval;
3434 min_interval =
3435 (min_interval !=
3436 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3437 radv_info->min_radv_interval;
3438 lifetime =
3439 (use_lifetime !=
3440 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3441 radv_info->adv_router_lifetime_in_sec;
3442
3443 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003444 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003445 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003446 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003447
3448 if (lifetime <= max_interval)
3449 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003450 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003451
3452 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003453 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003454 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3455 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003456 }
3457
Dave Barachd7cb1b52016-12-09 09:52:16 -05003458 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3459 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3460 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003461
Dave Barachd7cb1b52016-12-09 09:52:16 -05003462 /*
3463 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3464 if "flag" is clear don't change corresponding value
3465 */
3466 radv_info->send_radv =
3467 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3468 radv_info->adv_managed_flag =
3469 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3470 radv_info->adv_other_flag =
3471 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3472 radv_info->adv_link_layer_address =
3473 (ll_option !=
3474 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3475 radv_info->send_unicast =
3476 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3477 radv_info->cease_radv =
3478 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3479
3480 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003481 radv_info->max_radv_interval = max_interval;
3482 radv_info->adv_router_lifetime_in_sec = lifetime;
3483
Dave Barachd7cb1b52016-12-09 09:52:16 -05003484 radv_info->initial_adverts_count =
3485 (initial_count !=
3486 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3487 radv_info->initial_adverts_count;
3488 radv_info->initial_adverts_interval =
3489 (initial_interval !=
3490 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3491 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003492
3493 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003494 if ((cease != 0) && (is_no))
3495 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003496
Dave Barachd7cb1b52016-12-09 09:52:16 -05003497 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3498 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003499 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003500 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003501 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003502 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003503}
3504
3505int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003506ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3507 ip6_address_t * prefix_addr, u8 prefix_len,
3508 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3509 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3510 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003511{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003512 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003513 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003514
Ed Warnickecb9cada2015-12-08 15:45:58 -07003515 u32 ri;
3516
3517 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003518 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3519 ~0);
3520
Ed Warnickecb9cada2015-12-08 15:45:58 -07003521 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3522
3523 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003524
3525 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003526 {
3527 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003528 ip6_radv_t *radv_info;
3529 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003530
3531 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003532 ip6_radv_prefix_t *prefix;
3533
Ed Warnickecb9cada2015-12-08 15:45:58 -07003534 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003535 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3536
Ed Warnickecb9cada2015-12-08 15:45:58 -07003537 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3538
Dave Barachd7cb1b52016-12-09 09:52:16 -05003539 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003540 {
3541 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003542 if (!prefix)
3543 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3544
3545 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003546 return VNET_API_ERROR_INVALID_VALUE_2;
3547
Dave Barachd7cb1b52016-12-09 09:52:16 -05003548 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003549 /* do specific delete processing here before returning */
3550 /* try to remove from routing table */
3551
Dave Barachd7cb1b52016-12-09 09:52:16 -05003552 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3553 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003554 pool_put (radv_info->adv_prefixes_pool, prefix);
3555
Dave Barachd7cb1b52016-12-09 09:52:16 -05003556 radv_info->initial_adverts_sent =
3557 radv_info->initial_adverts_count - 1;
3558 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003559 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003560 radv_info->last_radv_time = 0;
3561 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003562 }
3563
3564 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003565 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003566 {
3567 /* add */
3568 u32 pi;
3569 pool_get (radv_info->adv_prefixes_pool, prefix);
3570 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003571 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3572 /* old_value */ 0);
3573
3574 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3575
Ed Warnickecb9cada2015-12-08 15:45:58 -07003576 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003577 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3578
Ed Warnickecb9cada2015-12-08 15:45:58 -07003579 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003580 prefix->adv_on_link_flag = 1; /* L bit set */
3581 prefix->adv_autonomous_flag = 1; /* A bit set */
3582 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003583 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3584 prefix->enabled = 1;
3585 prefix->decrement_lifetime_flag = 1;
3586 prefix->deprecated_prefix_flag = 1;
3587
Dave Barachd7cb1b52016-12-09 09:52:16 -05003588 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003589 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003590 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003591 /* insert prefix into routing table as a connected prefix */
3592 }
3593
Dave Barachd7cb1b52016-12-09 09:52:16 -05003594 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003595 goto restart;
3596 }
3597 else
3598 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003599
3600 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003601 return VNET_API_ERROR_INVALID_VALUE_2;
3602
Dave Barachd7cb1b52016-12-09 09:52:16 -05003603 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003604 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003605 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003606 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003607 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003608 }
3609
Dave Barachd7cb1b52016-12-09 09:52:16 -05003610 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003611 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003612 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003613 prefix->adv_pref_lifetime_in_secs = ~0;
3614 prefix->decrement_lifetime_flag = 0;
3615 }
3616 else
3617 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003618 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3619 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003620 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003621
Ed Warnickecb9cada2015-12-08 15:45:58 -07003622 /* copy remaining */
3623 prefix->enabled = !(no_advertise != 0);
3624 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3625 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3626
Dave Barachd7cb1b52016-12-09 09:52:16 -05003627 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003628 /* restart */
3629 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003630 prefix->valid_lifetime_expires =
3631 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003632 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003633
3634 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3635 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003636 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003637 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003638 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003639 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003640}
3641
3642clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003643ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3644 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003645{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003646 vnet_main_t *vnm = vnet_get_main ();
3647 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3648 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003649 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003650 u8 suppress = 0, managed = 0, other = 0;
3651 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003652 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003653 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3654 0, ra_initial_interval = 0;
3655 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003656
Dave Barachd7cb1b52016-12-09 09:52:16 -05003657 unformat_input_t _line_input, *line_input = &_line_input;
3658 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003659
3660 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003661 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003662 ip6_address_t ip6_addr;
3663 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003664
Ed Warnickecb9cada2015-12-08 15:45:58 -07003665
3666 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003667 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003668 return 0;
3669
3670 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003671 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003672 {
3673
Dave Barachd7cb1b52016-12-09 09:52:16 -05003674 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003675 unformat_vnet_sw_interface, vnm, &sw_if_index))
3676 {
3677 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003678 ethernet_interface_t *eth_if0 = 0;
3679
Ed Warnickecb9cada2015-12-08 15:45:58 -07003680 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003681 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3682 eth_if0 =
3683 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3684
3685 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003686 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003687 error =
3688 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003689 goto done;
3690 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003691
Ed Warnickecb9cada2015-12-08 15:45:58 -07003692 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003693 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3694 sw_if_index, ~0);
3695
Ed Warnickecb9cada2015-12-08 15:45:58 -07003696 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003697
3698 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003699 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003700 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003701 }
3702 else
3703 {
3704 error = clib_error_return (0, "unknown interface %U'",
3705 format_unformat_error, line_input);
3706 goto done;
3707 }
3708 }
3709 else
3710 {
3711 error = clib_error_return (0, "invalid interface name %U'",
3712 format_unformat_error, line_input);
3713 goto done;
3714 }
3715 }
3716
3717 /* get the rest of the command */
3718 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3719 {
3720 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003721 is_no = 1;
3722 else if (unformat (line_input, "prefix %U/%d",
3723 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003724 {
3725 add_radv_info = 0;
3726 break;
3727 }
3728 else if (unformat (line_input, "ra-managed-config-flag"))
3729 {
3730 managed = 1;
3731 break;
3732 }
3733 else if (unformat (line_input, "ra-other-config-flag"))
3734 {
3735 other = 1;
3736 break;
3737 }
Chris Luke33879c92016-06-28 19:54:21 -04003738 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003739 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003740 {
Chris Luke33879c92016-06-28 19:54:21 -04003741 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003742 break;
3743 }
Chris Luke33879c92016-06-28 19:54:21 -04003744 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003745 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003746 {
Chris Luke33879c92016-06-28 19:54:21 -04003747 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003748 break;
3749 }
3750 else if (unformat (line_input, "ra-send-unicast"))
3751 {
3752 send_unicast = 1;
3753 break;
3754 }
3755 else if (unformat (line_input, "ra-lifetime"))
3756 {
3757 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003758 {
3759 error = unformat_parse_error (line_input);
3760 goto done;
3761 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003762 use_lifetime = 1;
3763 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003764 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003765 else if (unformat (line_input, "ra-initial"))
3766 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003767 if (!unformat
3768 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003769 {
3770 error = unformat_parse_error (line_input);
3771 goto done;
3772 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003773 break;
3774 }
3775 else if (unformat (line_input, "ra-interval"))
3776 {
3777 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003778 {
3779 error = unformat_parse_error (line_input);
3780 goto done;
3781 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003782
3783 if (!unformat (line_input, "%d", &ra_min_interval))
3784 ra_min_interval = 0;
3785 break;
3786 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003787 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003788 {
3789 cease = 1;
3790 break;
3791 }
3792 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003793 {
3794 error = unformat_parse_error (line_input);
3795 goto done;
3796 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003797 }
3798
Dave Barachd7cb1b52016-12-09 09:52:16 -05003799 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003800 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003801 ip6_neighbor_ra_config (vm, sw_if_index,
3802 suppress, managed, other,
3803 suppress_ll_option, send_unicast, cease,
3804 use_lifetime, ra_lifetime,
3805 ra_initial_count, ra_initial_interval,
3806 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003807 }
3808 else
3809 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003810 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003811 u32 pref_lifetime_in_secs = 0;
3812 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003813 u8 no_advertise = 0;
3814 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003815 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003816 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003817
3818 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003819 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003820 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003821 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003822 {
3823 use_prefix_default_values = 1;
3824 break;
3825 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003826 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003827 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003828 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003829 pref_lifetime_in_secs = ~0;
3830 break;
3831 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003832 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3833 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003834 break;
3835 else
3836 break;
3837 }
3838
3839
3840 /* get the rest of the command */
3841 while (!use_prefix_default_values &&
3842 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3843 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003844 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003845 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003846 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003847 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003848 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003849 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003850 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003851 no_onlink = 1;
3852 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003853 {
3854 error = unformat_parse_error (line_input);
3855 goto done;
3856 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003857 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003858
3859 ip6_neighbor_ra_prefix (vm, sw_if_index,
3860 &ip6_addr, addr_len,
3861 use_prefix_default_values,
3862 valid_lifetime_in_secs,
3863 pref_lifetime_in_secs,
3864 no_advertise,
3865 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003866 }
3867
Billy McFalla9a20e72017-02-15 11:39:12 -05003868done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003869 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003870
Ed Warnickecb9cada2015-12-08 15:45:58 -07003871 return error;
3872}
3873
Chris Lukebfd32fd2016-06-24 20:38:06 -04003874static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003875ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003876{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003877 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003878 u32 i;
3879
3880 for (i = 0; i < vec_len (addrs); i++)
3881 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003882 ip_interface_address_t *a =
3883 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3884 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003885
3886 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003887 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003888 }
3889}
3890
Ed Warnickecb9cada2015-12-08 15:45:58 -07003891static clib_error_t *
3892show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003893 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003894{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003895 vnet_main_t *vnm = vnet_get_main ();
3896 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3897 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003898 u32 sw_if_index;
3899
3900 sw_if_index = ~0;
3901
Dave Barachd7cb1b52016-12-09 09:52:16 -05003902 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003903 {
3904 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003905
Dave Barachd7cb1b52016-12-09 09:52:16 -05003906 /* look up the radv_t information for this interface */
3907 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3908 sw_if_index, ~0);
3909
3910 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3911
3912 if (ri != ~0)
3913 {
3914 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3915 ip6_radv_t *radv_info;
3916 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3917
3918 vlib_cli_output (vm, "%U is admin %s\n",
3919 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003920 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003921 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3922 "up" : "down"));
3923
Chris Lukebfd32fd2016-06-24 20:38:06 -04003924 u32 ai;
3925 u32 *link_scope = 0, *global_scope = 0;
3926 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003927 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003928
Dave Barachd7cb1b52016-12-09 09:52:16 -05003929 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3930 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003931 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3932
Dave Barachd7cb1b52016-12-09 09:52:16 -05003933 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003934 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003935 a = pool_elt_at_index (lm->if_address_pool, ai);
3936 ip6_address_t *address =
3937 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003938
Chris Lukebfd32fd2016-06-24 20:38:06 -04003939 if (ip6_address_is_link_local_unicast (address))
3940 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003941 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003942 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003943 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003944 vec_add1 (local_scope, ai);
3945 else
3946 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003947
3948 ai = a->next_this_sw_interface;
3949 }
3950
Dave Barachd7cb1b52016-12-09 09:52:16 -05003951 if (vec_len (link_scope))
3952 {
3953 vlib_cli_output (vm, "\tLink-local address(es):\n");
3954 ip6_print_addrs (vm, link_scope);
3955 vec_free (link_scope);
3956 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003957
Dave Barachd7cb1b52016-12-09 09:52:16 -05003958 if (vec_len (local_scope))
3959 {
3960 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3961 ip6_print_addrs (vm, local_scope);
3962 vec_free (local_scope);
3963 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003964
Dave Barachd7cb1b52016-12-09 09:52:16 -05003965 if (vec_len (global_scope))
3966 {
3967 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3968 ip6_print_addrs (vm, global_scope);
3969 vec_free (global_scope);
3970 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003971
Dave Barachd7cb1b52016-12-09 09:52:16 -05003972 if (vec_len (unknown_scope))
3973 {
3974 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3975 ip6_print_addrs (vm, unknown_scope);
3976 vec_free (unknown_scope);
3977 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003978
Neale Ranns53da2212018-02-24 02:11:19 -08003979 vlib_cli_output (vm, "\tLink-local address(es):\n");
3980 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3981 &radv_info->link_local_address);
3982
Ed Warnickecb9cada2015-12-08 15:45:58 -07003983 vlib_cli_output (vm, "\tJoined group address(es):\n");
3984 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003985 /* *INDENT-OFF* */
3986 pool_foreach (m, radv_info->mldp_group_pool,
3987 ({
3988 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3989 &m->mcast_address);
3990 }));
3991 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003992
3993 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003994 ip6_radv_prefix_t *p;
3995 /* *INDENT-OFF* */
3996 pool_foreach (p, radv_info->adv_prefixes_pool,
3997 ({
3998 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3999 format_ip6_address, &p->prefix, p->prefix_len);
4000 }));
4001 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004002
Dave Barachd7cb1b52016-12-09 09:52:16 -05004003 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004004 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
4005 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
4006 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
4007 vlib_cli_output (vm, "\tND DAD is disabled\n");
4008 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
4009 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
4010 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004011 vlib_cli_output (vm,
4012 "\tND advertised retransmit interval is %d (msec)\n",
4013 radv_info->
4014 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004015
4016 u32 ra_interval = radv_info->max_radv_interval;
4017 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004018 vlib_cli_output (vm,
4019 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004020 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004021 vlib_cli_output (vm,
4022 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004023 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004024 vlib_cli_output (vm,
4025 "\tHosts %s stateless autoconfig for addresses\n",
4026 (radv_info->adv_managed_flag) ? "use" :
4027 " don't use");
4028 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4029 radv_info->n_advertisements_sent);
4030 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4031 radv_info->n_solicitations_rcvd);
4032 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4033 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004034 }
4035 else
4036 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004037 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004038 format_unformat_error, input);
4039
4040 }
4041 }
4042 return error;
4043}
4044
Billy McFall0683c9c2016-10-13 08:27:31 -04004045/*?
4046 * This command is used to display various IPv6 attributes on a given
4047 * interface.
4048 *
4049 * @cliexpar
4050 * Example of how to display IPv6 settings:
4051 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4052 * GigabitEthernet2/0/0 is admin up
4053 * Link-local address(es):
4054 * fe80::ab8/64
4055 * Joined group address(es):
4056 * ff02::1
4057 * ff02::2
4058 * ff02::16
4059 * ff02::1:ff00:ab8
4060 * Advertised Prefixes:
4061 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4062 * MTU is 1500
4063 * ICMP error messages are unlimited
4064 * ICMP redirects are disabled
4065 * ICMP unreachables are not sent
4066 * ND DAD is disabled
4067 * ND advertised reachable time is 0
4068 * ND advertised retransmit interval is 0 (msec)
4069 * ND router advertisements are sent every 200 seconds (min interval is 150)
4070 * ND router advertisements live for 600 seconds
4071 * Hosts use stateless autoconfig for addresses
4072 * ND router advertisements sent 19336
4073 * ND router solicitations received 0
4074 * ND router solicitations dropped 0
4075 * @cliexend
4076 * Example of output if IPv6 is not enabled on the interface:
4077 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4078 * show ip6 interface: IPv6 not enabled on interface
4079 * @cliexend
4080?*/
4081/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004082VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4083{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004084 .path = "show ip6 interface",
4085 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004086 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004087};
Billy McFall0683c9c2016-10-13 08:27:31 -04004088/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004089
4090clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004091disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004092{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004093 clib_error_t *error = 0;
4094 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004095 u32 ri;
4096
4097 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004098 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4099 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004100 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004101
Ed Warnickecb9cada2015-12-08 15:45:58 -07004102 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004103 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004104 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004105 ip6_radv_t *radv_info;
4106
4107 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004108
4109 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004110 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004111 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004112 {
4113 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004114 ip6_ll_prefix_t ilp = {
4115 .ilp_addr = radv_info->link_local_address,
4116 .ilp_sw_if_index = sw_if_index,
4117 };
4118 ip6_ll_table_entry_delete (&ilp);
4119 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004120 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004121 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4122 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004123 }
4124 }
4125 return error;
4126}
4127
4128int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004129ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004130{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004131 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4132 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004133
Dave Barachd7cb1b52016-12-09 09:52:16 -05004134 /* look up the radv_t information for this interface */
4135 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4136 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004137
Dave Barachd7cb1b52016-12-09 09:52:16 -05004138 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004139
Dave Barachd7cb1b52016-12-09 09:52:16 -05004140 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004141}
4142
Dave Barachd7cb1b52016-12-09 09:52:16 -05004143clib_error_t *
4144enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004145{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004146 clib_error_t *error = 0;
4147 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004148 u32 ri;
4149 int is_add = 1;
4150
4151 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004152 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4153 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004154
Dave Barachd7cb1b52016-12-09 09:52:16 -05004155 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4156
4157 /* if not created yet */
4158 if (ri == ~0)
4159 {
4160 vnet_main_t *vnm = vnet_get_main ();
4161 vnet_sw_interface_t *sw_if0;
4162
4163 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4164 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4165 {
4166 ethernet_interface_t *eth_if0;
4167
4168 eth_if0 =
4169 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4170 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004171 {
4172 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004173 ri =
4174 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004175
Dave Barachd7cb1b52016-12-09 09:52:16 -05004176 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004177 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004178 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004179 ip6_address_t link_local_address;
4180
Dave Barachd7cb1b52016-12-09 09:52:16 -05004181 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004182
Dave Barachd7cb1b52016-12-09 09:52:16 -05004183 ip6_link_local_address_from_ethernet_mac_address
4184 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004185
4186 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004187 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07004188 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004189 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004190 {
4191 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004192 link_local_address.as_u64[1] =
4193 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004194
4195 link_local_address.as_u64[0] =
4196 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004197 /* clear u bit */
4198 link_local_address.as_u8[8] &= 0xfd;
4199 }
Neale Ranns53da2212018-02-24 02:11:19 -08004200 {
4201 ip6_ll_prefix_t ilp = {
4202 .ilp_addr = link_local_address,
4203 .ilp_sw_if_index = sw_if_index,
4204 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004205
Neale Ranns53da2212018-02-24 02:11:19 -08004206 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4207 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004208
Ed Warnickecb9cada2015-12-08 15:45:58 -07004209 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004210 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4211 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004212
Neale Ranns53da2212018-02-24 02:11:19 -08004213 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004214 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004215 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004216 }
4217 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004218 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004219 }
4220 }
4221 return error;
4222}
4223
Neale Ranns53da2212018-02-24 02:11:19 -08004224int
4225ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4226{
4227 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4228 ip6_radv_t *radv_info;
4229 u32 ri;
4230
Eyal Baricd307742018-07-22 12:45:15 +03004231 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
Neale Ranns53da2212018-02-24 02:11:19 -08004232 return 0;
4233
4234 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4235
4236 if (ri == ~0)
4237 return 0;
4238
4239 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4240 *addr = radv_info->link_local_address;
4241
4242 return (!0);
4243}
4244
Ed Warnickecb9cada2015-12-08 15:45:58 -07004245static clib_error_t *
4246enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004247 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004248{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004249 vnet_main_t *vnm = vnet_get_main ();
4250 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004251 u32 sw_if_index;
4252
4253 sw_if_index = ~0;
4254
Dave Barachd7cb1b52016-12-09 09:52:16 -05004255 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004256 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004257 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004258 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004259 else
4260 {
4261 error = clib_error_return (0, "unknown interface\n'",
4262 format_unformat_error, input);
4263
4264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004265 return error;
4266}
4267
Billy McFall0683c9c2016-10-13 08:27:31 -04004268/*?
4269 * This command is used to enable IPv6 on a given interface.
4270 *
4271 * @cliexpar
4272 * Example of how enable IPv6 on a given interface:
4273 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4274?*/
4275/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004276VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4277{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004278 .path = "enable ip6 interface",
4279 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004280 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004281};
Billy McFall0683c9c2016-10-13 08:27:31 -04004282/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004283
4284static clib_error_t *
4285disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004286 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004287{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004288 vnet_main_t *vnm = vnet_get_main ();
4289 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004290 u32 sw_if_index;
4291
4292 sw_if_index = ~0;
4293
Dave Barachd7cb1b52016-12-09 09:52:16 -05004294 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004295 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004296 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004297 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004298 else
4299 {
4300 error = clib_error_return (0, "unknown interface\n'",
4301 format_unformat_error, input);
4302
4303 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004304 return error;
4305}
4306
Billy McFall0683c9c2016-10-13 08:27:31 -04004307/*?
4308 * This command is used to disable IPv6 on a given interface.
4309 *
4310 * @cliexpar
4311 * Example of how disable IPv6 on a given interface:
4312 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4313?*/
4314/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004315VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4316{
Billy McFall0683c9c2016-10-13 08:27:31 -04004317 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004318 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004319 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004320};
Billy McFall0683c9c2016-10-13 08:27:31 -04004321/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004322
Billy McFall0683c9c2016-10-13 08:27:31 -04004323/*?
4324 * This command is used to configure the neighbor discovery
4325 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4326 * command to display some of the current neighbor discovery parameters
4327 * on a given interface. This command has three formats:
4328 *
4329 *
4330 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4331 *
4332 * '<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>'
4333 *
4334 * Where:
4335 *
4336 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4337 * router-advertisement messages to use stateful address
4338 * auto-configuration to obtain address information (sets the M-bit).
4339 * Default is the M-bit is not set and the '<em>no</em>' option
4340 * returns it to this default state.
4341 *
4342 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4343 * router-advertisement messages that hosts use stateful auto
4344 * configuration to obtain nonaddress related information (sets
4345 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4346 * option returns it to this default state.
4347 *
4348 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4349 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4350 * router-advertisement messages.
4351 *
4352 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4353 * optional source link-layer address in the ICMPv6 router-advertisement
4354 * messages. Default is to include the optional source link-layer address
4355 * and the '<em>no</em>' option returns it to this default state.
4356 *
4357 * <em>[no] ra-send-unicast</em> - Use the source address of the
4358 * router-solicitation message if availiable. The default is to use
4359 * multicast address of all nodes, and the '<em>no</em>' option returns
4360 * it to this default state.
4361 *
4362 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4363 * default router in ICMPv6 router-advertisement messages. The range is
4364 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4365 * '<em><max-interval></em>'. The default value is 600 seconds and the
4366 * '<em>no</em>' option returns it to this default value.
4367 *
4368 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4369 * router-advertisement messages sent and the interval between each
4370 * message. Range for count is 1 - 3 and default is 3. Range for interval
4371 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4372 * returns both to their default value.
4373 *
4374 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4375 * interval between sending ICMPv6 router-advertisement messages. The
4376 * range for max-interval is from 4 to 200 seconds. min-interval can not
4377 * be more than 75% of max-interval. If not set, min-interval will be
4378 * set to 75% of max-interval. The range for min-interval is from 3 to
4379 * 150 seconds. The '<em>no</em>' option returns both to their default
4380 * value.
4381 *
4382 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4383 * The '<em>no</em>' options implies to start (or restart) sending
4384 * ICMPv6 router-advertisement messages.
4385 *
4386 *
4387 * <b>Format 2 - Prefix Options:</b>
4388 *
4389 * '<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>'
4390 *
4391 * Where:
4392 *
4393 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4394 *
4395 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4396 * length of time in seconds during what the prefix is valid for the purpose of
4397 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4398 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4399 * length of time in seconds during what addresses generated from the prefix remain
4400 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4401 *
4402 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4403 * are inifinte, no timeout.
4404 *
4405 * <em>no-advertise</em> - Do not send full router address in prefix
4406 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4407 *
4408 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4409 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4410 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4411 *
4412 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4413 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4414 *
4415 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4416 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4417 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4418 * the L-bit.
4419 *
4420 *
4421 * <b>Format 3: - Default of Prefix:</b>
4422 *
4423 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4424 *
4425 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4426 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4427 * is ignored and the prefix is deleted.
4428 *
4429 *
4430 * @cliexpar
4431 * Example of how set a router advertisement option:
4432 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4433 * Example of how to add a prefix:
4434 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4435 * Example of how to delete a prefix:
4436 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4437?*/
4438/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004439VLIB_CLI_COMMAND (ip6_nd_command, static) =
4440{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004441 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004442 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004443 .function = ip6_neighbor_cmd,
4444};
Billy McFall0683c9c2016-10-13 08:27:31 -04004445/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004446
4447clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004448set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08004449 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004450{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004451 clib_error_t *error = 0;
4452 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004453 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004454 ip6_radv_t *radv_info;
4455 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07004456
Dave Barachd7cb1b52016-12-09 09:52:16 -05004457 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004458 {
4459 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004460 return (error = clib_error_return (0, "address not link-local",
4461 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004462 }
4463
4464 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004465 enable_ip6_interface (vm, sw_if_index);
4466
Ed Warnickecb9cada2015-12-08 15:45:58 -07004467 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004468
4469 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004470 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004471 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004472
4473 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004474
Ed Warnickecb9cada2015-12-08 15:45:58 -07004475 /* delete the old one */
4476 error = ip6_add_del_interface_address (vm, sw_if_index,
4477 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08004478 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05004479
4480 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004481 {
4482 /* add the new one */
4483 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08004484 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004485 0 /* is_del */ );
4486
4487 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004488 {
4489 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004490 }
4491 }
4492 }
4493 else
4494 {
4495 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4496 error = clib_error_return (0, "ip6 not enabled for interface",
4497 format_unformat_error);
4498 }
4499 return error;
4500}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004501
Ed Warnickecb9cada2015-12-08 15:45:58 -07004502clib_error_t *
4503set_ip6_link_local_address_cmd (vlib_main_t * vm,
4504 unformat_input_t * input,
4505 vlib_cli_command_t * cmd)
4506{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004507 vnet_main_t *vnm = vnet_get_main ();
4508 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004509 u32 sw_if_index;
4510 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004511
4512 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004513 {
4514 /* get the rest of the command */
4515 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4516 {
Neale Ranns75152282017-01-09 01:00:45 -08004517 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004518 break;
4519 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05004520 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004521 }
4522 }
Neale Ranns75152282017-01-09 01:00:45 -08004523 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004524 return error;
4525}
4526
Billy McFall0683c9c2016-10-13 08:27:31 -04004527/*?
4528 * This command is used to assign an IPv6 Link-local address to an
4529 * interface. This command will enable IPv6 on an interface if it
4530 * is not already enabled. Use the '<em>show ip6 interface</em>' command
4531 * to display the assigned Link-local address.
4532 *
4533 * @cliexpar
4534 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08004535 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04004536?*/
4537/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004538VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
4539{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004540 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08004541 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004542 .function = set_ip6_link_local_address_cmd,
4543};
Billy McFall0683c9c2016-10-13 08:27:31 -04004544/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004545
Neale Ranns87df12d2017-02-18 08:16:41 -08004546/**
4547 * @brief callback when an interface address is added or deleted
4548 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004549static void
4550ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4551 uword opaque,
4552 u32 sw_if_index,
4553 ip6_address_t * address,
4554 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004555 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004556{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004557 vnet_main_t *vnm = vnet_get_main ();
4558 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004559 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004560 vlib_main_t *vm = vnm->vlib_main;
4561 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004562 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004563
4564 /* create solicited node multicast address for this interface adddress */
4565 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004566
Ed Warnickecb9cada2015-12-08 15:45:58 -07004567 a.as_u8[0xd] = address->as_u8[0xd];
4568 a.as_u8[0xe] = address->as_u8[0xe];
4569 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004570
4571 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004572 {
4573 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004574 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004575
4576 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004577 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4578 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004579 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004580 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004581 {
4582 /* get radv_info */
4583 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4584
4585 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004586 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004587 radv_info->ref_count++;
4588
Neale Ranns87df12d2017-02-18 08:16:41 -08004589 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004590 }
4591 }
4592 else
4593 {
4594
4595 /* delete */
4596 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004597 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4598 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004599 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004600
Dave Barachd7cb1b52016-12-09 09:52:16 -05004601 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004602 {
4603 /* get radv_info */
4604 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4605
Neale Ranns87df12d2017-02-18 08:16:41 -08004606 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004607
4608 /* if interface up send MLDP "report" */
4609 radv_info->all_routers_mcast = 0;
4610
4611 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004612 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004613 radv_info->ref_count--;
4614 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004615 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4616 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004617 }
4618}
4619
Dave Barachd7cb1b52016-12-09 09:52:16 -05004620clib_error_t *
4621ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004622{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004623 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004624
4625 nm->limit_neighbor_cache_size = neighbor_limit;
4626 return 0;
4627}
4628
Neale Ranns15002542017-09-10 04:39:11 -07004629static void
4630ip6_neighbor_table_bind (ip6_main_t * im,
4631 uword opaque,
4632 u32 sw_if_index,
4633 u32 new_fib_index, u32 old_fib_index)
4634{
4635 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4636 ip6_neighbor_t *n = NULL;
4637 u32 i, *to_re_add = 0;
4638
4639 /* *INDENT-OFF* */
4640 pool_foreach (n, nm->neighbor_pool,
4641 ({
4642 if (n->key.sw_if_index == sw_if_index)
4643 vec_add1 (to_re_add, n - nm->neighbor_pool);
4644 }));
4645 /* *INDENT-ON* */
4646
4647 for (i = 0; i < vec_len (to_re_add); i++)
4648 {
4649 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4650 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4651 ip6_neighbor_adj_fib_add (n, new_fib_index);
4652 }
4653 vec_free (to_re_add);
4654}
4655
Dave Barachd7cb1b52016-12-09 09:52:16 -05004656static clib_error_t *
4657ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004658{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004659 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4660 ip6_main_t *im = &ip6_main;
4661
Ed Warnickecb9cada2015-12-08 15:45:58 -07004662 mhash_init (&nm->neighbor_index_by_key,
4663 /* value size */ sizeof (uword),
4664 /* key size */ sizeof (ip6_neighbor_key_t));
4665
Dave Barachd7cb1b52016-12-09 09:52:16 -05004666 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4667 ip6_icmp_neighbor_solicitation_node.index);
4668 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4669 ip6_icmp_neighbor_advertisement_node.index);
4670 icmp6_register_type (vm, ICMP6_router_solicitation,
4671 ip6_icmp_router_solicitation_node.index);
4672 icmp6_register_type (vm, ICMP6_router_advertisement,
4673 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004674
4675 /* handler node for ip6 neighbor discovery events and timers */
4676 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4677
4678 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004679 ip6_add_del_interface_address_callback_t cb;
4680 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4681
Ed Warnickecb9cada2015-12-08 15:45:58 -07004682 /* when an interface address changes... */
4683 cb.function = ip6_neighbor_add_del_interface_address;
4684 cb.function_opaque = 0;
4685 vec_add1 (im->add_del_interface_address_callbacks, cb);
4686
Neale Ranns15002542017-09-10 04:39:11 -07004687 ip6_table_bind_callback_t cbt;
4688 cbt.function = ip6_neighbor_table_bind;
4689 cbt.function_opaque = 0;
4690 vec_add1 (im->table_bind_callbacks, cbt);
4691
Ed Warnickecb9cada2015-12-08 15:45:58 -07004692 mhash_init (&nm->pending_resolutions_by_address,
4693 /* value size */ sizeof (uword),
4694 /* key size */ sizeof (ip6_address_t));
4695
John Lo1edfba92016-08-27 01:11:57 -04004696 mhash_init (&nm->mac_changes_by_address,
4697 /* value size */ sizeof (uword),
4698 /* key size */ sizeof (ip6_address_t));
4699
Ed Warnickecb9cada2015-12-08 15:45:58 -07004700 /* default, configurable */
4701 nm->limit_neighbor_cache_size = 50000;
4702
Eyal Baric125ecc2017-09-20 11:29:17 +03004703 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4704
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004705 nm->ip6_ra_publisher_node = (uword) ~ 0;
4706
Ed Warnickecb9cada2015-12-08 15:45:58 -07004707#if 0
4708 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004709 vec_validate_init_empty
4710 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004711#endif
4712
Ed Warnickecb9cada2015-12-08 15:45:58 -07004713 return 0;
4714}
4715
4716VLIB_INIT_FUNCTION (ip6_neighbor_init);
4717
4718
Dave Barachd7cb1b52016-12-09 09:52:16 -05004719void
4720vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4721 void *address_arg,
4722 uword node_index,
4723 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004724{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004725 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4726 ip6_address_t *address = address_arg;
4727 uword *p;
4728 pending_resolution_t *pr;
4729
Ed Warnickecb9cada2015-12-08 15:45:58 -07004730 pool_get (nm->pending_resolutions, pr);
4731
4732 pr->next_index = ~0;
4733 pr->node_index = node_index;
4734 pr->type_opaque = type_opaque;
4735 pr->data = data;
4736
4737 p = mhash_get (&nm->pending_resolutions_by_address, address);
4738 if (p)
4739 {
4740 /* Insert new resolution at the head of the list */
4741 pr->next_index = p[0];
4742 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4743 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004744
4745 mhash_set (&nm->pending_resolutions_by_address, address,
4746 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004747}
4748
Dave Barachd7cb1b52016-12-09 09:52:16 -05004749int
4750vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4751 void *data_callback,
4752 u32 pid,
4753 void *address_arg,
4754 uword node_index,
4755 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004756{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004757 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4758 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004759
Eyal Barif9f40652017-04-01 03:55:08 +03004760 /* Try to find an existing entry */
4761 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4762 u32 *p = first;
4763 pending_resolution_t *mc;
4764 while (p && *p != ~0)
4765 {
4766 mc = pool_elt_at_index (nm->mac_changes, *p);
4767 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4768 && mc->pid == pid)
4769 break;
4770 p = &mc->next_index;
4771 }
4772
4773 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004774 if (is_add)
4775 {
Eyal Barif9f40652017-04-01 03:55:08 +03004776 if (found)
4777 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4778
John Lo1edfba92016-08-27 01:11:57 -04004779 pool_get (nm->mac_changes, mc);
Neale Rannsf12dad62018-06-04 18:41:24 -07004780 /* *INDENT-OFF* */
Eyal Barif9f40652017-04-01 03:55:08 +03004781 *mc = (pending_resolution_t)
4782 {
Neale Rannsf12dad62018-06-04 18:41:24 -07004783 .next_index = ~0,
4784 .node_index = node_index,
4785 .type_opaque = type_opaque,
4786 .data = data,
4787 .data_callback = data_callback,
4788 .pid = pid,
4789 };
4790 /* *INDENT-ON* */
John Lo1edfba92016-08-27 01:11:57 -04004791
Eyal Barif9f40652017-04-01 03:55:08 +03004792 /* Insert new resolution at the end of the list */
4793 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004794 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004795 p[0] = new_idx;
4796 else
4797 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004798 }
4799 else
4800 {
Eyal Barif9f40652017-04-01 03:55:08 +03004801 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004802 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004803
Eyal Barif9f40652017-04-01 03:55:08 +03004804 /* Clients may need to clean up pool entries, too */
4805 void (*fp) (u32, u8 *) = data_callback;
4806 if (fp)
4807 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04004808
Eyal Barif9f40652017-04-01 03:55:08 +03004809 /* Remove the entry from the list and delete the entry */
4810 *p = mc->next_index;
4811 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004812
Eyal Barif9f40652017-04-01 03:55:08 +03004813 /* Remove from hash if we deleted the last entry */
4814 if (*p == ~0 && p == first)
4815 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004816 }
Eyal Barif9f40652017-04-01 03:55:08 +03004817 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004818}
4819
Dave Barachd7cb1b52016-12-09 09:52:16 -05004820int
4821vnet_ip6_nd_term (vlib_main_t * vm,
4822 vlib_node_runtime_t * node,
4823 vlib_buffer_t * p0,
4824 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004825 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004826{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004827 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4828 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
John Lo1edfba92016-08-27 01:11:57 -04004829
4830 ndh = ip6_next_header (ip);
4831 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4832 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004833 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004834
4835 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4836 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4837 {
4838 u8 *t0 = vlib_add_trace (vm, node, p0,
4839 sizeof (icmp6_input_trace_t));
4840 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4841 }
4842
4843 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004844 if (PREDICT_FALSE
4845 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4846 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004847 {
Eyal Baric125ecc2017-09-20 11:29:17 +03004848 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004849 }
4850
4851 /* Check if MAC entry exsist for solicited target IP */
4852 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4853 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004854 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004855 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004856 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004857
4858 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004859 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004860 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4861 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004862 return 0; /* source link layer address option not present */
4863
John Lo1edfba92016-08-27 01:11:57 -04004864 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004865 macp =
4866 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004867 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004868 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004869 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004870 vlib_node_runtime_t *error_node =
4871 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004872 ip->dst_address = ip->src_address;
4873 ip->src_address = ndh->target_address;
4874 ip->hop_limit = 255;
4875 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004876 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004877 clib_memcpy (opt->ethernet_address, macp, 6);
4878 ndh->icmp.type = ICMP6_neighbor_advertisement;
4879 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004880 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4881 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004882 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004883 ndh->icmp.checksum =
4884 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4885 clib_memcpy (eth->dst_address, eth->src_address, 6);
4886 clib_memcpy (eth->src_address, macp, 6);
4887 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004888 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004889 return 1;
4890 }
John Lo1edfba92016-08-27 01:11:57 -04004891 }
4892
4893 return 0;
4894
4895}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004896
Neale Ranns3f844d02017-02-18 00:03:54 -08004897int
4898ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4899{
4900 u32 fib_index;
4901
4902 fib_prefix_t pfx = {
4903 .fp_len = 128,
4904 .fp_proto = FIB_PROTOCOL_IP6,
4905 .fp_addr = {
4906 .ip6 = *addr,
4907 },
4908 };
4909 ip46_address_t nh = {
4910 .ip6 = *addr,
4911 };
4912
4913 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4914
4915 if (~0 == fib_index)
4916 return VNET_API_ERROR_NO_SUCH_FIB;
4917
4918 if (is_del)
4919 {
4920 fib_table_entry_path_remove (fib_index,
4921 &pfx,
4922 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004923 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004924 &nh,
4925 sw_if_index,
4926 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4927 /* flush the ND cache of this address if it's there */
Neale Ranns0bdd3192018-09-07 11:04:52 -07004928 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
Neale Ranns3f844d02017-02-18 00:03:54 -08004929 }
4930 else
4931 {
4932 fib_table_entry_path_add (fib_index,
4933 &pfx,
4934 FIB_SOURCE_IP6_ND_PROXY,
4935 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004936 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004937 &nh,
4938 sw_if_index,
4939 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4940 }
4941 return (0);
4942}
4943
4944static clib_error_t *
4945set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4946 unformat_input_t * input, vlib_cli_command_t * cmd)
4947{
4948 vnet_main_t *vnm = vnet_get_main ();
4949 clib_error_t *error = 0;
4950 ip6_address_t addr;
4951 u32 sw_if_index;
4952 u8 is_del = 0;
4953
4954 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4955 {
4956 /* get the rest of the command */
4957 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4958 {
4959 if (unformat (input, "%U", unformat_ip6_address, &addr))
4960 break;
4961 else if (unformat (input, "delete") || unformat (input, "del"))
4962 is_del = 1;
4963 else
4964 return (unformat_parse_error (input));
4965 }
4966 }
4967
4968 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4969
4970 return error;
4971}
4972
4973/* *INDENT-OFF* */
4974VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4975{
4976 .path = "set ip6 nd proxy",
4977 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4978 .function = set_ip6_nd_proxy_cmd,
4979};
4980/* *INDENT-ON* */
4981
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004982void
Neale Ranns3be6b282016-12-20 14:24:01 +00004983ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004984{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004985 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4986 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004987 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004988
4989 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004990 pool_foreach (n, nm->neighbor_pool,
4991 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004992 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004993 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004994 adj_nbr_walk_nh6 (sw_if_index,
4995 &n->key.ip6_address,
4996 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004997 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004998 }));
4999 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08005000
5001 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
5002
5003 if (ADJ_INDEX_INVALID != ai)
5004 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02005005}
Dave Barachd7cb1b52016-12-09 09:52:16 -05005006
John Lo8b81cb42017-06-26 01:40:20 -04005007void
Steven9f781d82018-06-05 11:09:32 -07005008send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04005009{
5010 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04005011 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07005012
Steven9f781d82018-06-05 11:09:32 -07005013 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07005014}
5015
5016void
5017send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07005018 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07005019{
5020 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07005021 vnet_main_t *vnm = vnet_get_main ();
5022 u8 *rewrite, rewrite_len;
5023 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
5024 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07005025
John Lo8b81cb42017-06-26 01:40:20 -04005026 if (ip6_addr)
5027 {
5028 clib_warning
5029 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
5030 format_ip6_address, ip6_addr, sw_if_index);
5031
5032 /* Form unsolicited neighbor advertisement packet from NS pkt template */
5033 int bogus_length;
5034 u32 bi = 0;
5035 icmp6_neighbor_solicitation_header_t *h =
5036 vlib_packet_template_get_packet (vm,
5037 &i6m->discover_neighbor_packet_template,
5038 &bi);
John Lo084606b2018-06-19 15:27:48 -04005039 if (!h)
5040 return;
5041
John Lo8b81cb42017-06-26 01:40:20 -04005042 ip6_set_reserved_multicast_address (&h->ip.dst_address,
5043 IP6_MULTICAST_SCOPE_link_local,
5044 IP6_MULTICAST_GROUP_ID_all_hosts);
5045 h->ip.src_address = ip6_addr[0];
5046 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
5047 h->neighbor.target_address = ip6_addr[0];
5048 h->neighbor.advertisement_flags = clib_host_to_net_u32
5049 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
ahdj00722130e12018-08-14 10:35:43 +08005050 h->link_layer_option.header.type =
5051 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo8b81cb42017-06-26 01:40:20 -04005052 clib_memcpy (h->link_layer_option.ethernet_address,
5053 hi->hw_address, vec_len (hi->hw_address));
5054 h->neighbor.icmp.checksum =
5055 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
5056 ASSERT (bogus_length == 0);
5057
5058 /* Setup MAC header with IP6 Etype and mcast DMAC */
5059 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07005060 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04005061 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07005062 rewrite =
5063 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5064 rewrite_len = vec_len (rewrite);
5065 vlib_buffer_advance (b, -rewrite_len);
5066 ethernet_header_t *e = vlib_buffer_get_current (b);
5067 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5068 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04005069
5070 /* Send unsolicited ND advertisement packet out the specified interface */
5071 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5072 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5073 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5074 u32 *to_next = vlib_frame_vector_args (f);
5075 to_next[0] = bi;
5076 f->n_vectors = 1;
5077 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5078 }
5079}
5080
Dave Barachd7cb1b52016-12-09 09:52:16 -05005081/*
5082 * fd.io coding-style-patch-verification: ON
5083 *
5084 * Local Variables:
5085 * eval: (c-set-style "gnu")
5086 * End:
5087 */