blob: b4b6888a934028e839f30f80db7d6e5a1d700b98 [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;
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200246 u32 ri = ~0;
Juraj Sloboda81119e82018-05-25 14:02:20 +0200247
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200248 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index)
249 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Juraj Sloboda81119e82018-05-25 14:02:20 +0200250 if (ri == ~0)
251 {
252 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
253 return empty_address;
254 }
255 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
256 if (radv_info == NULL)
257 {
258 clib_warning ("Internal error");
259 return empty_address;
260 }
261 return radv_info->link_local_address;
262}
263
Eyal Baric125ecc2017-09-20 11:29:17 +0300264/**
265 * @brief publish wildcard arp event
266 * @param sw_if_index The interface on which the ARP entires are acted
267 */
268static int
269vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
270{
271 wc_nd_report_t r = {
272 .sw_if_index = sw_if_index,
273 .ip6 = *ip6,
274 };
275 memcpy (r.mac, mac, sizeof r.mac);
276
277 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
278 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
279 return 0;
280}
281
282static void
283wc_nd_signal_report (wc_nd_report_t * r)
284{
285 vlib_main_t *vm = vlib_get_main ();
286 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
287 uword ni = nm->wc_ip6_nd_publisher_node;
288 uword et = nm->wc_ip6_nd_publisher_et;
289
290 if (ni == (uword) ~ 0)
291 return;
292 wc_nd_report_t *q =
293 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
294
295 *q = *r;
296}
297
298void
299wc_nd_set_publisher_node (uword node_index, uword event_type)
300{
301 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
302 nm->wc_ip6_nd_publisher_node = node_index;
303 nm->wc_ip6_nd_publisher_et = event_type;
304}
305
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100306static int
307ra_publish (ra_report_t * r)
308{
309 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
310 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
311 return 0;
312}
313
314static void
315ra_signal_report (ra_report_t * r)
316{
317 vlib_main_t *vm = vlib_get_main ();
318 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
319 uword ni = nm->ip6_ra_publisher_node;
320 uword et = nm->ip6_ra_publisher_et;
321
322 if (ni == (uword) ~ 0)
323 return;
324 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
325
326 *q = *r;
327}
328
329void
330ra_set_publisher_node (uword node_index, uword event_type)
331{
332 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
333 nm->ip6_ra_publisher_node = node_index;
334 nm->ip6_ra_publisher_et = event_type;
335}
336
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337static u8 *
338format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
341 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
342 vnet_main_t *vnm = vnet_get_main ();
343 vnet_sw_interface_t *si;
344 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Dave Barachd7cb1b52016-12-09 09:52:16 -0500346 if (!n)
shubing guo30746292018-08-10 15:51:44 +0800347 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500348 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100349
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100350 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500351 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100352
353 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
Neale Rannsb3b2de72017-03-08 05:17:22 -0800356 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
357 flags = format (flags, "N");
358
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
shubing guo30746292018-08-10 15:51:44 +0800360 s = format (s, "%=12U%=45U%=6s%=20U%=40U",
John Lo7f358b32018-04-28 01:19:24 -0400361 format_vlib_time, vm, n->time_last_updated,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 format_ethernet_address, n->link_layer_address,
365 format_vnet_sw_interface_name, vnm, si);
366
Dave Barachd7cb1b52016-12-09 09:52:16 -0500367 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 return s;
369}
370
Neale Ranns15002542017-09-10 04:39:11 -0700371static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700372ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700373{
374 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
375 {
Neale Ranns53da2212018-02-24 02:11:19 -0800376 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
377 {
378 ip6_ll_prefix_t pfx = {
379 .ilp_addr = n->key.ip6_address,
380 .ilp_sw_if_index = n->key.sw_if_index,
381 };
382 ip6_ll_table_entry_delete (&pfx);
383 }
384 else
385 {
386 fib_prefix_t pfx = {
387 .fp_len = 128,
388 .fp_proto = FIB_PROTOCOL_IP6,
389 .fp_addr.ip6 = n->key.ip6_address,
390 };
391 fib_table_entry_path_remove (fib_index,
392 &pfx,
393 FIB_SOURCE_ADJ,
394 DPO_PROTO_IP6,
395 &pfx.fp_addr,
396 n->key.sw_if_index, ~0,
397 1, FIB_ROUTE_PATH_FLAG_NONE);
398 }
Neale Ranns15002542017-09-10 04:39:11 -0700399 }
400}
401
Dave Barachd7cb1b52016-12-09 09:52:16 -0500402typedef struct
403{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100405 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800406 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 u8 link_layer_address[6];
408 u32 sw_if_index;
409 ip6_address_t addr;
410} ip6_neighbor_set_unset_rpc_args_t;
411
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412static void ip6_neighbor_set_unset_rpc_callback
413 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Dave Barachd7cb1b52016-12-09 09:52:16 -0500415static void set_unset_ip6_neighbor_rpc
416 (vlib_main_t * vm,
417 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700418 const ip6_address_t * a,
419 const u8 * link_layer_address,
420 int is_add, int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421{
422 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500423 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500424
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 args.sw_if_index = sw_if_index;
426 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100427 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800428 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100429 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800430 if (NULL != link_layer_address)
431 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500432
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500434 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100437static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500438ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100439{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500440 icmp6_neighbor_solicitation_header_t *h;
441 vnet_main_t *vnm = vnet_get_main ();
442 ip6_main_t *im = &ip6_main;
443 ip_interface_address_t *ia;
444 ip6_address_t *dst, *src;
445 vnet_hw_interface_t *hi;
446 vnet_sw_interface_t *si;
447 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100448 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500449 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100450 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100451
Dave Barachd7cb1b52016-12-09 09:52:16 -0500452 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100453
Dave Barachd7cb1b52016-12-09 09:52:16 -0500454 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100455 dst = &adj->sub_type.nbr.next_hop.ip6;
456
457 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100458 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100459 return;
460 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500461 src = ip6_interface_address_matching_destination (im, dst,
462 adj->rewrite_header.
463 sw_if_index, &ia);
464 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100465 {
466 return;
467 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100468
Dave Barachd7cb1b52016-12-09 09:52:16 -0500469 h = vlib_packet_template_get_packet (vm,
470 &im->discover_neighbor_packet_template,
471 &bi);
John Lo084606b2018-06-19 15:27:48 -0400472 if (!h)
473 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100474
Dave Barachd7cb1b52016-12-09 09:52:16 -0500475 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100476
477 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
478 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
479 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
480 h->ip.src_address = src[0];
481 h->neighbor.target_address = dst[0];
482
483 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500484 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100485
486 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500487 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
488 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100489
490 b = vlib_get_buffer (vm, bi);
491 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500492 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100493
494 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500495 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
496 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100497
498 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500499 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
500 u32 *to_next = vlib_frame_vector_args (f);
501 to_next[0] = bi;
502 f->n_vectors = 1;
503 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100504 }
505}
506
507static void
508ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
509{
510 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
511 ethernet_build_rewrite (vnet_get_main (),
512 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500513 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100514 nbr->link_layer_address));
515}
516
517static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000518ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100519{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500520 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000521
Dave Barachd7cb1b52016-12-09 09:52:16 -0500522 adj_nbr_update_rewrite (ai,
523 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
524 ethernet_build_rewrite (vnet_get_main (),
525 adj->rewrite_header.
526 sw_if_index,
527 adj_get_link_type (ai),
528 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100529}
530
531#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
532{ \
533 k.sw_if_index = sw_if_index; \
534 k.ip6_address = *addr; \
535 k.pad = 0; \
536}
537
538static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500539ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100540{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500541 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
542 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100543 ip6_neighbor_key_t k;
544 uword *p;
545
Dave Barachd7cb1b52016-12-09 09:52:16 -0500546 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100547
548 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500549 if (p)
550 {
551 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
552 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100553
554 return (n);
555}
556
557static adj_walk_rc_t
558ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
559{
560 ip6_neighbor_t *nbr = ctx;
561
562 ip6_nd_mk_complete (ai, nbr);
563
564 return (ADJ_WALK_RC_CONTINUE);
565}
566
567static adj_walk_rc_t
568ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
569{
Neale Ranns19c68d22016-12-07 15:38:14 +0000570 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100571
572 return (ADJ_WALK_RC_CONTINUE);
573}
574
John Lo4fed5b12018-05-22 03:35:06 -0400575static clib_error_t *
576ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
577 u32 sw_if_index, u32 flags)
578{
579 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
580 ip6_neighbor_t *n;
581 u32 i, *to_delete = 0;
582
583 /* *INDENT-OFF* */
584 pool_foreach (n, nm->neighbor_pool,
585 ({
586 if (n->key.sw_if_index == sw_if_index)
587 vec_add1 (to_delete, n - nm->neighbor_pool);
588 }));
589 /* *INDENT-ON* */
590
591 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
592 {
593 for (i = 0; i < vec_len (to_delete); i++)
594 {
595 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
596 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
597 ip6_nd_mk_complete_walk, n);
598 }
599 }
600 else
601 {
602 for (i = 0; i < vec_len (to_delete); i++)
603 {
604 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
605 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
606 ip6_nd_mk_incomplete_walk, NULL);
607 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
608 continue;
609 ip6_neighbor_adj_fib_remove (n,
610 ip6_fib_table_get_index_for_sw_if_index
611 (n->key.sw_if_index));
612 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
613 pool_put (nm->neighbor_pool, n);
614 }
615 }
616
617 vec_free (to_delete);
618 return 0;
619}
620
621VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
622
Neale Rannsb80c5362016-10-08 13:03:40 +0100623void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500624ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100625{
626 ip6_neighbor_t *nbr;
627 ip_adjacency_t *adj;
628
629 adj = adj_get (ai);
630
631 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
632
Neale Ranns32e1c012016-11-22 17:07:28 +0000633 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100634 {
Ole Trøan438f6302018-02-15 21:56:06 +0000635 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800636 adj_glean_update_rewrite (ai);
637 break;
638 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000639 if (NULL != nbr)
640 {
641 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
642 ip6_nd_mk_complete_walk, nbr);
643 }
644 else
645 {
646 /*
647 * no matching ND entry.
648 * construct the rewrite required to for an ND packet, and stick
649 * that in the adj's pipe to smoke.
650 */
651 adj_nbr_update_rewrite (ai,
652 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
653 ethernet_build_rewrite (vnm,
654 sw_if_index,
655 VNET_LINK_IP6,
656 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
657
658 /*
659 * since the FIB has added this adj for a route, it makes sense it may
660 * want to forward traffic sometime soon. Let's send a speculative ND.
661 * just one. If we were to do periodically that wouldn't be bad either,
662 * but that's more code than i'm prepared to write at this time for
663 * relatively little reward.
664 */
665 ip6_nbr_probe (adj);
666 }
667 break;
Neale Ranns1855b8e2018-07-11 10:31:26 -0700668 case IP_LOOKUP_NEXT_BCAST:
669 adj_nbr_update_rewrite (ai,
670 ADJ_NBR_REWRITE_FLAG_COMPLETE,
671 ethernet_build_rewrite (vnm,
672 sw_if_index,
673 VNET_LINK_IP6,
674 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
675 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000676 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700677 {
678 /*
679 * Construct a partial rewrite from the known ethernet mcast dest MAC
680 */
681 u8 *rewrite;
682 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100683
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700684 rewrite = ethernet_build_rewrite (vnm,
685 sw_if_index,
686 adj->ia_link,
687 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000688
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700689 /*
690 * Complete the remaining fields of the adj's rewrite to direct the
691 * complete of the rewrite at switch time by copying in the IP
692 * dst address's bytes.
Neale Ranns889fe942017-06-01 05:43:19 -0400693 * Ofset is 2 bytes into the desintation address.
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700694 */
695 offset = vec_len (rewrite) - 2;
Neale Ranns889fe942017-06-01 05:43:19 -0400696 adj_mcast_update_rewrite (ai, rewrite, offset);
Neale Ranns32e1c012016-11-22 17:07:28 +0000697
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700698 break;
699 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000700 case IP_LOOKUP_NEXT_DROP:
701 case IP_LOOKUP_NEXT_PUNT:
702 case IP_LOOKUP_NEXT_LOCAL:
703 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800704 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000705 case IP_LOOKUP_NEXT_MIDCHAIN:
706 case IP_LOOKUP_NEXT_ICMP_ERROR:
707 case IP_LOOKUP_N_NEXT:
708 ASSERT (0);
709 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100710 }
711}
712
Neale Ranns15002542017-09-10 04:39:11 -0700713
714static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700715ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700716{
Neale Ranns53da2212018-02-24 02:11:19 -0800717 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
718 {
719 ip6_ll_prefix_t pfx = {
720 .ilp_addr = n->key.ip6_address,
721 .ilp_sw_if_index = n->key.sw_if_index,
722 };
723 n->fib_entry_index =
724 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
725 }
726 else
727 {
728 fib_prefix_t pfx = {
729 .fp_len = 128,
730 .fp_proto = FIB_PROTOCOL_IP6,
731 .fp_addr.ip6 = n->key.ip6_address,
732 };
Neale Ranns15002542017-09-10 04:39:11 -0700733
Neale Ranns53da2212018-02-24 02:11:19 -0800734 n->fib_entry_index =
735 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
736 FIB_ENTRY_FLAG_ATTACHED,
737 DPO_PROTO_IP6, &pfx.fp_addr,
738 n->key.sw_if_index, ~0, 1, NULL,
739 FIB_ROUTE_PATH_FLAG_NONE);
740 }
Neale Ranns15002542017-09-10 04:39:11 -0700741}
742
John Lo4fed5b12018-05-22 03:35:06 -0400743static ip6_neighbor_t *
744force_reuse_neighbor_entry (void)
745{
746 ip6_neighbor_t *n;
747 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
748 u32 count = 0;
749 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
750 if (index == ~0) /* Try again from elt 0 */
751 index = pool_next_index (nm->neighbor_pool, index);
752
753 /* Find a non-static random entry to free up for reuse */
754 do
755 {
756 if ((count++ == 100) || (index == ~0))
757 return NULL; /* give up after 100 entries */
758 n = pool_elt_at_index (nm->neighbor_pool, index);
759 nm->neighbor_delete_rotor = index;
760 index = pool_next_index (nm->neighbor_pool, index);
761 }
762 while (n->flags & IP6_NEIGHBOR_FLAG_STATIC);
763
764 /* Remove ARP entry from its interface and update fib */
765 adj_nbr_walk_nh6 (n->key.sw_if_index,
766 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
767 ip6_neighbor_adj_fib_remove
768 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
769 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
770
771 return n;
772}
773
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774int
775vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500776 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700777 const ip6_address_t * a,
778 const u8 * link_layer_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500779 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800780 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500782 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500784 ip6_neighbor_t *n = 0;
785 int make_new_nd_cache_entry = 1;
786 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500788 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789
Damjan Marion586afd72017-04-05 19:18:20 +0200790 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 {
792 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800793 1 /* set new neighbor */ , is_static,
794 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795 return 0;
796 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
798 k.sw_if_index = sw_if_index;
799 k.ip6_address = a[0];
800 k.pad = 0;
801
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500803 if (p)
804 {
805 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
806 /* Refuse to over-write static neighbor entry. */
807 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
John Lo0e6f4d62018-07-13 17:29:58 -0400808 {
809 /* if MAC address match, still check to send event */
810 if (0 == memcmp (n->link_layer_address,
811 link_layer_address, n_bytes_link_layer_address))
812 goto check_customers;
813 return -2;
814 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500815 make_new_nd_cache_entry = 0;
816 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100817
Dave Barachd7cb1b52016-12-09 09:52:16 -0500818 if (make_new_nd_cache_entry)
819 {
John Lo4fed5b12018-05-22 03:35:06 -0400820 if (nm->limit_neighbor_cache_size &&
821 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
822 {
823 n = force_reuse_neighbor_entry ();
824 if (NULL == n)
825 return -2;
826 }
827 else
828 pool_get (nm->neighbor_pool, n);
829
Dave Barachd7cb1b52016-12-09 09:52:16 -0500830 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
831 /* old value */ 0);
832 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700833 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100834
Dave Barachd7cb1b52016-12-09 09:52:16 -0500835 clib_memcpy (n->link_layer_address,
836 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100837
Dave Barachd7cb1b52016-12-09 09:52:16 -0500838 /*
839 * create the adj-fib. the entry in the FIB table for and to the peer.
840 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800841 if (!is_no_fib_entry)
842 {
Neale Ranns53da2212018-02-24 02:11:19 -0800843 ip6_neighbor_adj_fib_add
844 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700845 }
846 else
847 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800848 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
849 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500850 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100851 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500852 {
853 /*
854 * prevent a DoS attack from the data-plane that
855 * spams us with no-op updates to the MAC address
856 */
857 if (0 == memcmp (n->link_layer_address,
858 link_layer_address, n_bytes_link_layer_address))
John Lo7f358b32018-04-28 01:19:24 -0400859 {
860 n->time_last_updated = vlib_time_now (vm);
861 goto check_customers;
862 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100863
Dave Barachd7cb1b52016-12-09 09:52:16 -0500864 clib_memcpy (n->link_layer_address,
865 link_layer_address, n_bytes_link_layer_address);
866 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867
Neale Rannsb80c5362016-10-08 13:03:40 +0100868 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400869 n->time_last_updated = vlib_time_now (vm);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100870 if (is_static)
John Lo4fed5b12018-05-22 03:35:06 -0400871 {
872 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
873 n->flags &= ~IP6_NEIGHBOR_FLAG_DYNAMIC;
874 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100875 else
John Lo4fed5b12018-05-22 03:35:06 -0400876 {
877 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
878 n->flags &= ~IP6_NEIGHBOR_FLAG_STATIC;
879 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100880
Neale Rannsb80c5362016-10-08 13:03:40 +0100881 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500882 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883
Dave Barach59b25652017-09-10 15:04:27 -0400884check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885 /* Customer(s) waiting for this address to be resolved? */
886 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400887 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 {
John Lo1edfba92016-08-27 01:11:57 -0400889 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500890
891 while (next_index != (u32) ~ 0)
892 {
John Lo1edfba92016-08-27 01:11:57 -0400893 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
894 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500895 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400896 next_index = pr->next_index;
897 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500898 }
John Lo1edfba92016-08-27 01:11:57 -0400899
Neale Ranns0bdd3192018-09-07 11:04:52 -0700900 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901 }
902
John Lo1edfba92016-08-27 01:11:57 -0400903 /* Customer(s) requesting ND event for this address? */
904 p = mhash_get (&nm->mac_changes_by_address, a);
905 if (p)
906 {
907 next_index = p[0];
908
Dave Barachd7cb1b52016-12-09 09:52:16 -0500909 while (next_index != (u32) ~ 0)
910 {
911 int (*fp) (u32, u8 *, u32, ip6_address_t *);
912 int rv = 1;
913 mc = pool_elt_at_index (nm->mac_changes, next_index);
914 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400915
Dave Barachd7cb1b52016-12-09 09:52:16 -0500916 /* Call the user's data callback, return 1 to suppress dup events */
917 if (fp)
918 rv =
Neale Ranns0bdd3192018-09-07 11:04:52 -0700919 (*fp) (mc->data, (u8 *) link_layer_address, sw_if_index,
920 &ip6a_zero);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500921 /*
922 * Signal the resolver process, as long as the user
923 * says they want to be notified
924 */
925 if (rv == 0)
926 vlib_process_signal_event (vm, mc->node_index,
927 mc->type_opaque, mc->data);
928 next_index = mc->next_index;
929 }
John Lo1edfba92016-08-27 01:11:57 -0400930 }
931
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 return 0;
933}
934
935int
936vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700937 u32 sw_if_index, const ip6_address_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500939 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500941 ip6_neighbor_t *n;
942 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 int rv = 0;
944
Damjan Marion586afd72017-04-05 19:18:20 +0200945 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946 {
Neale Ranns0bdd3192018-09-07 11:04:52 -0700947 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800948 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949 return 0;
950 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
952 k.sw_if_index = sw_if_index;
953 k.ip6_address = a[0];
954 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500955
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956 p = mhash_get (&nm->neighbor_index_by_key, &k);
957 if (p == 0)
958 {
959 rv = -1;
960 goto out;
961 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500962
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100964
Neale Rannsb80c5362016-10-08 13:03:40 +0100965 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500966 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400967 ip6_neighbor_adj_fib_remove
968 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100969
John Lo4fed5b12018-05-22 03:35:06 -0400970 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500972
973out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974 return rv;
975}
976
Dave Barachd7cb1b52016-12-09 09:52:16 -0500977static void ip6_neighbor_set_unset_rpc_callback
978 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500980 vlib_main_t *vm = vlib_get_main ();
981 if (a->is_add)
982 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800983 a->link_layer_address, 6, a->is_static,
984 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700986 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988
989static int
990ip6_neighbor_sort (void *a1, void *a2)
991{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500992 vnet_main_t *vnm = vnet_get_main ();
993 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500995 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
996 n2->key.sw_if_index);
997 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
999 return cmp;
1000}
1001
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001002ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -04001003ip6_neighbors_pool (void)
1004{
1005 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1006 return nm->neighbor_pool;
1007}
1008
1009ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001010ip6_neighbors_entries (u32 sw_if_index)
1011{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001012 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001013 ip6_neighbor_t *n, *ns = 0;
1014
1015 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001016 pool_foreach (n, nm->neighbor_pool,
1017 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001018 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1019 continue;
1020 vec_add1 (ns, n[0]);
1021 }));
1022 /* *INDENT-ON* */
1023
1024 if (ns)
1025 vec_sort_with_function (ns, ip6_neighbor_sort);
1026 return ns;
1027}
1028
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029static clib_error_t *
1030show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001031 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001033 vnet_main_t *vnm = vnet_get_main ();
1034 ip6_neighbor_t *n, *ns;
1035 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036 u32 sw_if_index;
1037
1038 /* Filter entries by interface if given. */
1039 sw_if_index = ~0;
1040 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1041
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001042 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001043 if (ns)
1044 {
Billy McFall46529cd2016-10-14 10:45:49 -04001045 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001046 vec_foreach (n, ns)
1047 {
1048 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -04001049 }
1050 vec_free (ns);
1051 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052
1053 return error;
1054}
1055
Billy McFall0683c9c2016-10-13 08:27:31 -04001056/*?
1057 * This command is used to display the adjacent IPv6 hosts found via
1058 * neighbor discovery. Optionally, limit the output to the specified
1059 * interface.
1060 *
1061 * @cliexpar
1062 * Example of how to display the IPv6 neighbor adjacency table:
1063 * @cliexstart{show ip6 neighbors}
1064 * Time Address Flags Link layer Interface
1065 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1066 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1067 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1068 * @cliexend
1069 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1070 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1071 * Time Address Flags Link layer Interface
1072 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1073 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1074 * @cliexend
1075?*/
1076/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1078 .path = "show ip6 neighbors",
1079 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001080 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081};
Billy McFall0683c9c2016-10-13 08:27:31 -04001082/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001083
1084static clib_error_t *
1085set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001086 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001088 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 ip6_address_t addr;
1090 u8 mac_address[6];
1091 int addr_valid = 0;
1092 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001093 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001094 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 u32 sw_if_index;
1096
Dave Barachd7cb1b52016-12-09 09:52:16 -05001097 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 {
1099 /* intfc, ip6-address, mac-address */
1100 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001101 unformat_vnet_sw_interface, vnm, &sw_if_index,
1102 unformat_ip6_address, &addr,
1103 unformat_ethernet_address, mac_address))
1104 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105
1106 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001107 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001108 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001109 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001110 else if (unformat (input, "no-fib-entry"))
1111 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001112 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001113 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114 }
1115
1116 if (!addr_valid)
1117 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001118
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 if (!is_del)
1120 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001121 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001122 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123 else
Neale Ranns0bdd3192018-09-07 11:04:52 -07001124 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125 return 0;
1126}
1127
Billy McFall0683c9c2016-10-13 08:27:31 -04001128/*?
1129 * This command is used to manually add an entry to the IPv6 neighbor
1130 * adjacency table. Optionally, the entry can be added as static. It is
1131 * also used to remove an entry from the table. Use the '<em>show ip6
1132 * neighbors</em>' command to display all learned and manually entered entries.
1133 *
1134 * @cliexpar
1135 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1136 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1137 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1138 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1139?*/
1140/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001141VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1142{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143 .path = "set ip6 neighbor",
1144 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001145 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146};
Billy McFall0683c9c2016-10-13 08:27:31 -04001147/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148
Dave Barachd7cb1b52016-12-09 09:52:16 -05001149typedef enum
1150{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1152 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1153 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1154} icmp6_neighbor_solicitation_or_advertisement_next_t;
1155
1156static_always_inline uword
1157icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1158 vlib_node_runtime_t * node,
1159 vlib_frame_t * frame,
1160 uword is_solicitation)
1161{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001162 vnet_main_t *vnm = vnet_get_main ();
1163 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001164 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001165 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1167 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001168 vlib_node_runtime_t *error_node =
1169 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001170 int bogus_length;
1171
1172 from = vlib_frame_vector_args (frame);
1173 n_left_from = n_packets;
1174 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001175
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176 if (node->flags & VLIB_NODE_FLAG_TRACE)
1177 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1178 /* stride */ 1,
1179 sizeof (icmp6_input_trace_t));
1180
Dave Barachd7cb1b52016-12-09 09:52:16 -05001181 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001182 (is_solicitation
1183 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1184 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1185 n_advertisements_sent = 0;
1186
1187 while (n_left_from > 0)
1188 {
1189 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1190
1191 while (n_left_from > 0 && n_left_to_next > 0)
1192 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001193 vlib_buffer_t *p0;
1194 ip6_header_t *ip0;
1195 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1196 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001198 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1199 int is_rewrite0;
1200 u32 ni0;
1201
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202 bi0 = to_next[0] = from[0];
1203
1204 from += 1;
1205 to_next += 1;
1206 n_left_from -= 1;
1207 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001208
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 p0 = vlib_get_buffer (vm, bi0);
1210 ip0 = vlib_buffer_get_current (p0);
1211 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001212 options_len0 =
1213 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
1215 error0 = ICMP6_ERROR_NONE;
1216 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001217 ip6_sadd_link_local =
1218 ip6_address_is_link_local_unicast (&ip0->src_address);
1219 ip6_sadd_unspecified =
1220 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221
1222 /* Check that source address is unspecified, link-local or else on-link. */
1223 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1224 {
1225 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226
Dave Barachd7cb1b52016-12-09 09:52:16 -05001227 if (ADJ_INDEX_INVALID != src_adj_index0)
1228 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001229 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230
Dave Barachd7cb1b52016-12-09 09:52:16 -05001231 /* Allow all realistic-looking rewrite adjacencies to pass */
1232 ni0 = adj0->lookup_next_index;
1233 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1234 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001235
Dave Barachd7cb1b52016-12-09 09:52:16 -05001236 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1237 || !is_rewrite0)
1238 ?
1239 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1240 : error0);
1241 }
1242 else
1243 {
1244 error0 =
1245 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1246 }
1247 }
1248
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249 o0 = (void *) (h0 + 1);
1250 o0 = ((options_len0 == 8 && o0->header.type == option_type
1251 && o0->header.n_data_u64s == 1) ? o0 : 0);
1252
1253 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001254 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001255 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001257 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1258 is_solicitation ?
1259 &ip0->src_address :
1260 &h0->target_address,
1261 o0->ethernet_address,
1262 sizeof (o0->ethernet_address),
Neale Ranns53da2212018-02-24 02:11:19 -08001263 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265
1266 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1267 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001268 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001269 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001270 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001271
Dave Barachd7cb1b52016-12-09 09:52:16 -05001272 fib_index =
1273 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001274
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001275 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001276 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001277 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1278 }
1279 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001280 {
Neale Ranns53da2212018-02-24 02:11:19 -08001281 if (ip6_address_is_link_local_unicast (&h0->target_address))
1282 {
1283 fei = ip6_fib_table_lookup_exact_match
1284 (ip6_ll_fib_get (sw_if_index0),
1285 &h0->target_address, 128);
1286 }
1287 else
1288 {
1289 fei = ip6_fib_table_lookup_exact_match (fib_index,
1290 &h0->target_address,
1291 128);
1292 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001293
Neale Ranns3f844d02017-02-18 00:03:54 -08001294 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001295 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001296 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001297 error0 =
1298 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001299 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001300 else
1301 {
1302 if (FIB_ENTRY_FLAG_LOCAL &
1303 fib_entry_get_flags_for_source (fei,
1304 FIB_SOURCE_INTERFACE))
1305 {
1306 /* It's an address that belongs to one of our interfaces
1307 * that's good. */
1308 }
1309 else
1310 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001311 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1312 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001313 {
1314 /* The address was added by IPv6 Proxy ND config.
1315 * We should only respond to these if the NS arrived on
1316 * the link that has a matching covering prefix */
1317 }
1318 else
1319 {
1320 error0 =
1321 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1322 }
1323 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001324 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325 }
1326
1327 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001328 next0 = (error0 != ICMP6_ERROR_NONE
1329 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1330 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331 else
1332 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001333 next0 = 0;
1334 error0 = error0 == ICMP6_ERROR_NONE ?
1335 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336 }
1337
1338 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1339 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001340 vnet_sw_interface_t *sw_if0;
1341 ethernet_interface_t *eth_if0;
1342 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343
Dave Barachd7cb1b52016-12-09 09:52:16 -05001344 /* dst address is either source address or the all-nodes mcast addr */
1345 if (!ip6_sadd_unspecified)
1346 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001347 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001348 ip6_set_reserved_multicast_address (&ip0->dst_address,
1349 IP6_MULTICAST_SCOPE_link_local,
1350 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351
1352 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001353 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354 h0->icmp.type = ICMP6_neighbor_advertisement;
1355
1356 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1357 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001358 eth_if0 =
1359 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001360 if (eth_if0 && o0)
1361 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001362 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1363 o0->header.type =
1364 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365 }
1366
1367 h0->advertisement_flags = clib_host_to_net_u32
1368 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1369 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1370
1371 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001372 h0->icmp.checksum =
1373 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1374 &bogus_length);
1375 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376
Dave Barachd7cb1b52016-12-09 09:52:16 -05001377 /* Reuse current MAC header, copy SMAC to DMAC and
1378 * interface MAC to SMAC */
1379 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1380 eth0 = vlib_buffer_get_current (p0);
1381 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1382 if (eth_if0)
1383 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001384
Dave Barachd7cb1b52016-12-09 09:52:16 -05001385 /* Setup input and output sw_if_index for packet */
1386 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1387 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1388 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1389 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390
1391 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001392 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393
1394 p0->error = error_node->errors[error0];
1395
1396 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1397 to_next, n_left_to_next,
1398 bi0, next0);
1399 }
1400
1401 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1402 }
1403
1404 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001405 vlib_error_count (vm, error_node->node_index,
1406 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1407 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001408
1409 return frame->n_vectors;
1410}
1411
1412/* for "syslogging" - use elog for now */
1413#define foreach_log_level \
1414 _ (DEBUG, "DEBUG") \
1415 _ (INFO, "INFORMATION") \
1416 _ (NOTICE, "NOTICE") \
1417 _ (WARNING, "WARNING") \
1418 _ (ERR, "ERROR") \
1419 _ (CRIT, "CRITICAL") \
1420 _ (ALERT, "ALERT") \
1421 _ (EMERG, "EMERGENCY")
1422
Dave Barachd7cb1b52016-12-09 09:52:16 -05001423typedef enum
1424{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425#define _(f,s) LOG_##f,
1426 foreach_log_level
1427#undef _
1428} log_level_t;
1429
Dave Barachd7cb1b52016-12-09 09:52:16 -05001430static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001431#define _(f,s) s,
1432 foreach_log_level
1433#undef _
1434};
1435
Dave Barachd7cb1b52016-12-09 09:52:16 -05001436static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001437
1438static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001439ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001440{
1441 /* just use elog for now */
1442 u8 *what;
1443 va_list va;
1444
Dave Barachd7cb1b52016-12-09 09:52:16 -05001445 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1446 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447
1448 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001449 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450 {
1451 what = va_format (0, fmt, &va);
1452
Dave Barachd7cb1b52016-12-09 09:52:16 -05001453 ELOG_TYPE_DECLARE (e) =
1454 {
1455 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1456 struct
1457 {
1458 u32 s[2];
1459 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001460 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001461 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1462 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463 }
1464 va_end (va);
1465 return;
1466}
1467
Juraj Sloboda52574522018-05-03 10:03:50 +02001468clib_error_t *
1469call_ip6_neighbor_callbacks (void *data,
1470 _vnet_ip6_neighbor_function_list_elt_t * elt)
1471{
1472 clib_error_t *error = 0;
1473
1474 while (elt)
1475 {
1476 error = elt->fp (data);
1477 if (error)
1478 return error;
1479 elt = elt->next_ip6_neighbor_function;
1480 }
1481
1482 return error;
1483}
1484
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001486typedef enum
1487{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001488 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1489 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1490 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1491 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1492} icmp6_router_solicitation_or_advertisement_next_t;
1493
1494static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001495icmp6_router_solicitation (vlib_main_t * vm,
1496 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001497{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001498 vnet_main_t *vnm = vnet_get_main ();
1499 ip6_main_t *im = &ip6_main;
1500 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001501 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001502 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001504 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505 int bogus_length;
1506
1507 icmp6_neighbor_discovery_option_type_t option_type;
1508
Dave Barachd7cb1b52016-12-09 09:52:16 -05001509 vlib_node_runtime_t *error_node =
1510 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511
1512 from = vlib_frame_vector_args (frame);
1513 n_left_from = n_packets;
1514 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001515
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516 if (node->flags & VLIB_NODE_FLAG_TRACE)
1517 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1518 /* stride */ 1,
1519 sizeof (icmp6_input_trace_t));
1520
1521 /* source may append his LL address */
1522 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1523
1524 while (n_left_from > 0)
1525 {
1526 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001527
Ed Warnickecb9cada2015-12-08 15:45:58 -07001528 while (n_left_from > 0 && n_left_to_next > 0)
1529 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001530 vlib_buffer_t *p0;
1531 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532 ip6_radv_t *radv_info = 0;
1533
Dave Barachd7cb1b52016-12-09 09:52:16 -05001534 icmp6_neighbor_discovery_header_t *h0;
1535 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1536
Ed Warnickecb9cada2015-12-08 15:45:58 -07001537 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001538 u32 is_solicitation = 1, is_dropped = 0;
1539 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540
1541 bi0 = to_next[0] = from[0];
1542
1543 from += 1;
1544 to_next += 1;
1545 n_left_from -= 1;
1546 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001547
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548 p0 = vlib_get_buffer (vm, bi0);
1549 ip0 = vlib_buffer_get_current (p0);
1550 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001551 options_len0 =
1552 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1553 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1554 is_link_local =
1555 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001556
1557 error0 = ICMP6_ERROR_NONE;
1558 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001559
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560 /* check if solicitation (not from nd_timer node) */
1561 if (ip6_address_is_unspecified (&ip0->dst_address))
1562 is_solicitation = 0;
1563
1564 /* Check that source address is unspecified, link-local or else on-link. */
1565 if (!is_unspecified && !is_link_local)
1566 {
1567 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001568
Dave Barachd7cb1b52016-12-09 09:52:16 -05001569 if (ADJ_INDEX_INVALID != src_adj_index0)
1570 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001571 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001572
Dave Barachd7cb1b52016-12-09 09:52:16 -05001573 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1574 ?
1575 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1576 : error0);
1577 }
1578 else
1579 {
1580 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1581 }
1582 }
1583
Ed Warnickecb9cada2015-12-08 15:45:58 -07001584 /* check for source LL option and process */
1585 o0 = (void *) (h0 + 1);
1586 o0 = ((options_len0 == 8
1587 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001588 && o0->header.n_data_u64s == 1) ? o0 : 0);
1589
Ed Warnickecb9cada2015-12-08 15:45:58 -07001590 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001591 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1592 !is_unspecified && !is_link_local))
1593 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001594 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1595 &ip0->src_address,
1596 o0->ethernet_address,
1597 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001598 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001599 }
1600
Ed Warnickecb9cada2015-12-08 15:45:58 -07001601 /* default is to drop */
1602 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001603
Ed Warnickecb9cada2015-12-08 15:45:58 -07001604 if (error0 == ICMP6_ERROR_NONE)
1605 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001606 vnet_sw_interface_t *sw_if0;
1607 ethernet_interface_t *eth_if0;
1608 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001609
1610 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1611 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001612 eth_if0 =
1613 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001614
1615 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001616 error0 =
1617 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1618 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
1620 if (error0 == ICMP6_ERROR_NONE)
1621 {
1622 u32 ri;
1623
1624 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001625 p0->current_length -=
1626 (options_len0 +
1627 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001628
1629 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001630 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1631 sw_if_index0)
1632 {
1633 ri =
1634 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001635
Neale Ranns8e254952018-04-25 05:40:48 -07001636 if (ri != ~0)
1637 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1638 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001639
1640 error0 =
1641 ((!radv_info) ?
1642 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1643 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001644
1645 if (error0 == ICMP6_ERROR_NONE)
1646 {
1647 f64 now = vlib_time_now (vm);
1648
1649 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001650 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001651 {
Neale Ranns75152282017-01-09 01:00:45 -08001652 if (0 != radv_info->last_radv_time &&
1653 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001654 MIN_DELAY_BETWEEN_RAS)
1655 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001656 else
1657 radv_info->last_radv_time = now;
1658 }
1659
1660 /* send now */
1661 icmp6_router_advertisement_header_t rh;
1662
1663 rh.icmp.type = ICMP6_router_advertisement;
1664 rh.icmp.code = 0;
1665 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001666
Ed Warnickecb9cada2015-12-08 15:45:58 -07001667 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001668 rh.router_lifetime_in_sec =
1669 clib_host_to_net_u16
1670 (radv_info->adv_router_lifetime_in_sec);
1671 rh.
1672 time_in_msec_between_retransmitted_neighbor_solicitations
1673 =
1674 clib_host_to_net_u32 (radv_info->
1675 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1676 rh.neighbor_reachable_time_in_msec =
1677 clib_host_to_net_u32 (radv_info->
1678 adv_neighbor_reachable_time_in_msec);
1679
1680 rh.flags =
1681 (radv_info->adv_managed_flag) ?
1682 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1683 0;
1684 rh.flags |=
1685 ((radv_info->adv_other_flag) ?
1686 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1687 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001688
1689
Dave Barachd7cb1b52016-12-09 09:52:16 -05001690 u16 payload_length =
1691 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001692
Dave Barach3a63fc52019-01-07 09:15:47 -05001693 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001694 (vm, &bi0, (void *) &rh,
Dave Barach3a63fc52019-01-07 09:15:47 -05001695 sizeof (icmp6_router_advertisement_header_t)))
1696 {
1697 /* buffer allocation failed, drop the pkt */
1698 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1699 goto drop0;
1700 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001701
Dave Barachd7cb1b52016-12-09 09:52:16 -05001702 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001703 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001704 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1705 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706
Dave Barachd7cb1b52016-12-09 09:52:16 -05001707 h.header.type =
1708 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001709 h.header.n_data_u64s = 1;
1710
1711 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001712 clib_memcpy (&h.ethernet_address[0],
1713 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001714
Dave Barach3a63fc52019-01-07 09:15:47 -05001715 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001716 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001717 sizeof
1718 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1719 {
1720 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1721 goto drop0;
1722 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723
Dave Barachd7cb1b52016-12-09 09:52:16 -05001724 payload_length +=
1725 sizeof
1726 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001727 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001728
Ed Warnickecb9cada2015-12-08 15:45:58 -07001729 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001730 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001731 {
1732 icmp6_neighbor_discovery_mtu_option_t h;
1733
1734 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001735 h.mtu =
1736 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001737 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1738 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001739
1740 payload_length +=
1741 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001742
Dave Barach3a63fc52019-01-07 09:15:47 -05001743 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001744 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001745 sizeof
1746 (icmp6_neighbor_discovery_mtu_option_t)))
1747 {
1748 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1749 goto drop0;
1750 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001751 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001752
Ed Warnickecb9cada2015-12-08 15:45:58 -07001753 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001754 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001755
Dave Barachd7cb1b52016-12-09 09:52:16 -05001756 /* *INDENT-OFF* */
1757 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1758 ({
1759 if(pr_info->enabled &&
1760 (!pr_info->decrement_lifetime_flag
1761 || (pr_info->pref_lifetime_expires >0)))
1762 {
1763 /* advertise this prefix */
1764 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765
Dave Barachd7cb1b52016-12-09 09:52:16 -05001766 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1767 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768
Dave Barachd7cb1b52016-12-09 09:52:16 -05001769 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001770
Dave Barachd7cb1b52016-12-09 09:52:16 -05001771 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1772 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001773
Dave Barachd7cb1b52016-12-09 09:52:16 -05001774 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1775 {
1776 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1777 h.preferred_time = 0;
1778 }
1779 else
1780 {
1781 if(pr_info->decrement_lifetime_flag)
1782 {
1783 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1784 (pr_info->valid_lifetime_expires - now) : 0;
1785
1786 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1787 (pr_info->pref_lifetime_expires - now) : 0;
1788 }
1789
1790 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1791 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1792 }
1793 h.unused = 0;
1794
1795 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1796
1797 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1798
Dave Barach3a63fc52019-01-07 09:15:47 -05001799 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001800 (vm, &bi0, (void *)&h,
1801 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05001802 {
1803 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1804 goto drop0;
1805 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001806
1807 }
1808 }));
1809 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001810
1811 /* add additional options before here */
1812
1813 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001814 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001815 {
1816 ip0->dst_address = ip0->src_address;
1817 }
1818 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001819 {
1820 /* target address is all-nodes mcast addr */
1821 ip6_set_reserved_multicast_address
1822 (&ip0->dst_address,
1823 IP6_MULTICAST_SCOPE_link_local,
1824 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001825 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001826
Ed Warnickecb9cada2015-12-08 15:45:58 -07001827 /* source address MUST be the link-local address */
1828 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001829
Dave Barachd7cb1b52016-12-09 09:52:16 -05001830 ip0->hop_limit = 255;
1831 ip0->payload_length =
1832 clib_host_to_net_u16 (payload_length);
1833
1834 icmp6_router_advertisement_header_t *rh0 =
1835 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1836 rh0->icmp.checksum =
1837 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1838 &bogus_length);
1839 ASSERT (bogus_length == 0);
1840
Ed Warnickecb9cada2015-12-08 15:45:58 -07001841 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001842 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001843 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001844
1845 if (is_solicitation)
1846 {
1847 ethernet_header_t *eth0;
1848 /* Reuse current MAC header, copy SMAC to DMAC and
1849 * interface MAC to SMAC */
1850 vlib_buffer_reset (p0);
1851 eth0 = vlib_buffer_get_current (p0);
1852 clib_memcpy (eth0->dst_address, eth0->src_address,
1853 6);
1854 clib_memcpy (eth0->src_address, eth_if0->address,
1855 6);
1856 next0 =
1857 is_dropped ? next0 :
1858 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1859 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1860 sw_if_index0;
1861 }
1862 else
1863 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001864 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001865 if (adj_index0 == 0)
1866 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1867 else
1868 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001869 next0 =
1870 is_dropped ? next0 :
1871 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1872 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1873 adj_index0;
1874 }
1875 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001876 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001877
1878 radv_info->n_solicitations_dropped += is_dropped;
1879 radv_info->n_solicitations_rcvd += is_solicitation;
1880
1881 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001882 {
1883 radv_info->n_advertisements_sent++;
1884 n_advertisements_sent++;
1885 }
1886 }
1887 }
1888 }
1889
Dave Barach3a63fc52019-01-07 09:15:47 -05001890 drop0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001891 p0->error = error_node->errors[error0];
1892
Dave Barachd7cb1b52016-12-09 09:52:16 -05001893 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001894 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001895
Ed Warnickecb9cada2015-12-08 15:45:58 -07001896 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1897 to_next, n_left_to_next,
1898 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001899
Ed Warnickecb9cada2015-12-08 15:45:58 -07001900 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001901
Ed Warnickecb9cada2015-12-08 15:45:58 -07001902 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1903 }
1904
1905 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001906 vlib_error_count (vm, error_node->node_index,
1907 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1908 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001909
1910 return frame->n_vectors;
1911}
1912
1913 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1914static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001915icmp6_router_advertisement (vlib_main_t * vm,
1916 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001918 vnet_main_t *vnm = vnet_get_main ();
1919 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001921 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001922 u32 n_left_from, n_left_to_next, next_index;
1923 u32 n_advertisements_rcvd = 0;
1924
Dave Barachd7cb1b52016-12-09 09:52:16 -05001925 vlib_node_runtime_t *error_node =
1926 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001927
1928 from = vlib_frame_vector_args (frame);
1929 n_left_from = n_packets;
1930 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001931
Ed Warnickecb9cada2015-12-08 15:45:58 -07001932 if (node->flags & VLIB_NODE_FLAG_TRACE)
1933 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1934 /* stride */ 1,
1935 sizeof (icmp6_input_trace_t));
1936
1937 while (n_left_from > 0)
1938 {
1939 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001940
Ed Warnickecb9cada2015-12-08 15:45:58 -07001941 while (n_left_from > 0 && n_left_to_next > 0)
1942 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001943 vlib_buffer_t *p0;
1944 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001945 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001946 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001947 u32 bi0, options_len0, sw_if_index0, next0, error0;
1948
1949 bi0 = to_next[0] = from[0];
1950
1951 from += 1;
1952 to_next += 1;
1953 n_left_from -= 1;
1954 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001955
Ed Warnickecb9cada2015-12-08 15:45:58 -07001956 p0 = vlib_get_buffer (vm, bi0);
1957 ip0 = vlib_buffer_get_current (p0);
1958 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001959 options_len0 =
1960 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001961
1962 error0 = ICMP6_ERROR_NONE;
1963 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1964
Dave Barachd7cb1b52016-12-09 09:52:16 -05001965 /* Check that source address is link-local */
1966 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1968
1969 /* default is to drop */
1970 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001971
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972 n_advertisements_rcvd++;
1973
1974 if (error0 == ICMP6_ERROR_NONE)
1975 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001976 vnet_sw_interface_t *sw_if0;
1977 ethernet_interface_t *eth_if0;
1978
Ed Warnickecb9cada2015-12-08 15:45:58 -07001979 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1980 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001981 eth_if0 =
1982 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001983
1984 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001985 error0 =
1986 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1987 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001988
1989 if (error0 == ICMP6_ERROR_NONE)
1990 {
1991 u32 ri;
1992
1993 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001994 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1995 sw_if_index0)
1996 {
1997 ri =
1998 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001999
Neale Ranns8e254952018-04-25 05:40:48 -07002000 if (ri != ~0)
2001 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2002 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002003
2004 error0 =
2005 ((!radv_info) ?
2006 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
2007 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002008
2009 if (error0 == ICMP6_ERROR_NONE)
2010 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002011 radv_info->keep_sending_rs = 0;
2012
2013 ra_report_t r;
2014
2015 r.sw_if_index = sw_if_index0;
2016 memcpy (r.router_address, &ip0->src_address, 16);
2017 r.current_hop_limit = h0->current_hop_limit;
2018 r.flags = h0->flags;
2019 r.router_lifetime_in_sec =
2020 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2021 r.neighbor_reachable_time_in_msec =
2022 clib_net_to_host_u32
2023 (h0->neighbor_reachable_time_in_msec);
2024 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2025 r.prefixes = 0;
2026
Ed Warnickecb9cada2015-12-08 15:45:58 -07002027 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002028 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2029 && (h0->current_hop_limit !=
2030 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002031 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002032 ip6_neighbor_syslog (vm, LOG_WARNING,
2033 "our AdvCurHopLimit on %U doesn't agree with %U",
2034 format_vnet_sw_if_index_name,
2035 vnm, sw_if_index0,
2036 format_ip6_address,
2037 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002038 }
2039
Dave Barachd7cb1b52016-12-09 09:52:16 -05002040 if ((h0->flags &
2041 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2042 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002043 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002044 ip6_neighbor_syslog (vm, LOG_WARNING,
2045 "our AdvManagedFlag on %U doesn't agree with %U",
2046 format_vnet_sw_if_index_name,
2047 vnm, sw_if_index0,
2048 format_ip6_address,
2049 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002050 }
2051
Dave Barachd7cb1b52016-12-09 09:52:16 -05002052 if ((h0->flags &
2053 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2054 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002055 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002056 ip6_neighbor_syslog (vm, LOG_WARNING,
2057 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2058 format_vnet_sw_if_index_name,
2059 vnm, sw_if_index0,
2060 format_ip6_address,
2061 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002062 }
2063
Dave Barachd7cb1b52016-12-09 09:52:16 -05002064 if ((h0->
2065 time_in_msec_between_retransmitted_neighbor_solicitations
2066 && radv_info->
2067 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2068 && (h0->
2069 time_in_msec_between_retransmitted_neighbor_solicitations
2070 !=
2071 clib_host_to_net_u32 (radv_info->
2072 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002073 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002074 ip6_neighbor_syslog (vm, LOG_WARNING,
2075 "our AdvRetransTimer 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
Dave Barachd7cb1b52016-12-09 09:52:16 -05002082 if ((h0->neighbor_reachable_time_in_msec &&
2083 radv_info->adv_neighbor_reachable_time_in_msec) &&
2084 (h0->neighbor_reachable_time_in_msec !=
2085 clib_host_to_net_u32
2086 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002087 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002088 ip6_neighbor_syslog (vm, LOG_WARNING,
2089 "our AdvReachableTime on %U doesn't agree with %U",
2090 format_vnet_sw_if_index_name,
2091 vnm, sw_if_index0,
2092 format_ip6_address,
2093 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002094 }
2095
2096 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002097 u8 *opt_hdr = (u8 *) (h0 + 1);
2098 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002099 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002100 icmp6_neighbor_discovery_option_header_t *o0 =
2101 (icmp6_neighbor_discovery_option_header_t *)
2102 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002103 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002104 icmp6_neighbor_discovery_option_type_t option_type =
2105 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002106
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002108 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002109 ip6_neighbor_syslog (vm, LOG_ERR,
2110 "malformed RA packet on %U from %U",
2111 format_vnet_sw_if_index_name,
2112 vnm, sw_if_index0,
2113 format_ip6_address,
2114 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115 break;
2116 }
2117
Dave Barachd7cb1b52016-12-09 09:52:16 -05002118 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002119 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002120 ip6_neighbor_syslog (vm, LOG_ERR,
2121 " zero length option in RA on %U from %U",
2122 format_vnet_sw_if_index_name,
2123 vnm, sw_if_index0,
2124 format_ip6_address,
2125 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002126 break;
2127 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002128 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002129 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002130 ip6_neighbor_syslog (vm, LOG_ERR,
2131 "option length in RA packet greater than total length on %U from %U",
2132 format_vnet_sw_if_index_name,
2133 vnm, sw_if_index0,
2134 format_ip6_address,
2135 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002136 break;
2137 }
2138
2139 options_len0 -= opt_len;
2140 opt_hdr += opt_len;
2141
Dave Barachd7cb1b52016-12-09 09:52:16 -05002142 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002143 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002144 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2145 {
2146 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2147 * h =
2148 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2149 *) (o0);
2150
2151 if (opt_len < sizeof (*h))
2152 break;
2153
2154 memcpy (r.slla, h->ethernet_address, 6);
2155 }
2156 break;
2157
Ed Warnickecb9cada2015-12-08 15:45:58 -07002158 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002159 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002160 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002161 (icmp6_neighbor_discovery_mtu_option_t
2162 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002163
Dave Barachd7cb1b52016-12-09 09:52:16 -05002164 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002165 break;
2166
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002167 r.mtu = clib_net_to_host_u32 (h->mtu);
2168
Dave Barachd7cb1b52016-12-09 09:52:16 -05002169 if ((h->mtu && radv_info->adv_link_mtu) &&
2170 (h->mtu !=
2171 clib_host_to_net_u32
2172 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002173 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002174 ip6_neighbor_syslog (vm, LOG_WARNING,
2175 "our AdvLinkMTU on %U doesn't agree with %U",
2176 format_vnet_sw_if_index_name,
2177 vnm, sw_if_index0,
2178 format_ip6_address,
2179 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002180 }
2181 }
2182 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002183
Ed Warnickecb9cada2015-12-08 15:45:58 -07002184 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2185 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002186 icmp6_neighbor_discovery_prefix_information_option_t
2187 * h =
2188 (icmp6_neighbor_discovery_prefix_information_option_t
2189 *) (o0);
2190
Ed Warnickecb9cada2015-12-08 15:45:58 -07002191 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002192 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002193 u32 preferred, valid;
2194
Dave Barachd7cb1b52016-12-09 09:52:16 -05002195 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002196 break;
2197
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002198 vec_validate (r.prefixes,
2199 vec_len (r.prefixes));
2200 ra_report_prefix_info_t *prefix =
2201 vec_elt_at_index (r.prefixes,
2202 vec_len (r.prefixes) - 1);
2203
Dave Barachd7cb1b52016-12-09 09:52:16 -05002204 preferred =
2205 clib_net_to_host_u32 (h->preferred_time);
2206 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002207
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002208 prefix->preferred_time = preferred;
2209 prefix->valid_time = valid;
2210 prefix->flags = h->flags & 0xc0;
2211 prefix->dst_address_length =
2212 h->dst_address_length;
2213 prefix->dst_address = h->dst_address;
2214
Ed Warnickecb9cada2015-12-08 15:45:58 -07002215 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002216 /* *INDENT-OFF* */
2217 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2218 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002219
Dave Barachd7cb1b52016-12-09 09:52:16 -05002220 ip6_address_t mask;
2221 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2222
2223 if(pr_info->enabled &&
2224 (pr_info->prefix_len == h->dst_address_length) &&
2225 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2226 {
2227 /* found it */
2228 if(!pr_info->decrement_lifetime_flag &&
2229 valid != pr_info->adv_valid_lifetime_in_secs)
2230 {
2231 ip6_neighbor_syslog(vm, LOG_WARNING,
2232 "our ADV validlifetime on %U for %U does not agree with %U",
2233 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2234 format_ip6_address, &h->dst_address);
2235 }
2236 if(!pr_info->decrement_lifetime_flag &&
2237 preferred != pr_info->adv_pref_lifetime_in_secs)
2238 {
2239 ip6_neighbor_syslog(vm, LOG_WARNING,
2240 "our ADV preferredlifetime on %U for %U does not agree with %U",
2241 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2242 format_ip6_address, &h->dst_address);
2243 }
2244 }
2245 break;
2246 }));
2247 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002248 break;
2249 }
2250 default:
2251 /* skip this one */
2252 break;
2253 }
2254 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002255 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002256 }
2257 }
2258 }
2259
2260 p0->error = error_node->errors[error0];
2261
Dave Barachd7cb1b52016-12-09 09:52:16 -05002262 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002263 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002264
Ed Warnickecb9cada2015-12-08 15:45:58 -07002265 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2266 to_next, n_left_to_next,
2267 bi0, next0);
2268 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002269
Ed Warnickecb9cada2015-12-08 15:45:58 -07002270 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2271 }
2272
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002273 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002274 vlib_error_count (vm, error_node->node_index,
2275 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2276 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002277
2278 return frame->n_vectors;
2279}
2280
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002281static inline f64
2282random_f64_from_to (f64 from, f64 to)
2283{
2284 static u32 seed = 0;
2285 static u8 seed_set = 0;
2286 if (!seed_set)
2287 {
2288 seed = random_default_seed ();
2289 seed_set = 1;
2290 }
2291 return random_f64 (&seed) * (to - from) + from;
2292}
2293
2294static inline u8
2295get_mac_address (u32 sw_if_index, u8 * address)
2296{
2297 vnet_main_t *vnm = vnet_get_main ();
2298 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2299 if (!hw_if->hw_address)
2300 return 1;
2301 clib_memcpy (address, hw_if->hw_address, 6);
2302 return 0;
2303}
2304
2305static inline vlib_buffer_t *
2306create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2307{
2308 u32 bi0;
2309 vlib_buffer_t *p0;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002310 icmp6_router_solicitation_header_t *rh;
2311 u16 payload_length;
2312 int bogus_length;
2313 u32 sw_if_index;
2314
2315 sw_if_index = radv_info->sw_if_index;
2316
2317 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2318 {
2319 clib_warning ("buffer allocation failure");
2320 return 0;
2321 }
2322
2323 p0 = vlib_get_buffer (vm, bi0);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002324 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2325 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2326
2327 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2328 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2329
2330 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2331
2332 rh = vlib_buffer_get_current (p0);
2333 p0->current_length = sizeof (*rh);
2334
2335 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2336 rh->neighbor.icmp.code = 0;
2337 rh->neighbor.icmp.checksum = 0;
2338 rh->neighbor.reserved_must_be_zero = 0;
2339
2340 rh->link_layer_option.header.type =
2341 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2342 if (0 != get_mac_address (sw_if_index,
2343 rh->link_layer_option.ethernet_address))
2344 {
2345 clib_warning ("interface with sw_if_index %u has no mac address",
2346 sw_if_index);
2347 vlib_buffer_free (vm, &bi0, 1);
2348 return 0;
2349 }
2350 rh->link_layer_option.header.n_data_u64s = 1;
2351
2352 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2353
2354 rh->ip.ip_version_traffic_class_and_flow_label =
2355 clib_host_to_net_u32 (0x6 << 28);
2356 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2357 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2358 rh->ip.hop_limit = 255;
2359 rh->ip.src_address = radv_info->link_local_address;
2360 /* set address ff02::2 */
David Johnsond9818dd2018-12-14 14:53:41 -05002361 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002362 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2363
2364 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2365 &rh->ip,
2366 &bogus_length);
2367
2368 return p0;
2369}
2370
2371static inline void
2372stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2373{
2374 u32 bi0;
2375
2376 ra->keep_sending_rs = 0;
2377 if (ra->buffer)
2378 {
2379 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2380 vlib_buffer_free (vm, &bi0, 1);
2381 ra->buffer = 0;
2382 }
2383}
2384
2385static inline bool
2386check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2387 f64 * due_time)
2388{
2389 vlib_buffer_t *p0;
2390 vlib_frame_t *f;
2391 u32 *to_next;
2392 u32 next_index;
2393 vlib_buffer_t *c0;
2394 u32 ci0;
2395
2396 icmp6_send_router_solicitation_params_t *params;
2397
2398 if (!radv_info->keep_sending_rs)
2399 return false;
2400
2401 params = &radv_info->params;
2402
2403 if (radv_info->due_time > current_time)
2404 {
2405 *due_time = radv_info->due_time;
2406 return true;
2407 }
2408
2409 p0 = radv_info->buffer;
2410
2411 next_index = ip6_rewrite_mcast_node.index;
2412
2413 c0 = vlib_buffer_copy (vm, p0);
2414 ci0 = vlib_get_buffer_index (vm, c0);
2415
2416 f = vlib_get_frame_to_node (vm, next_index);
2417 to_next = vlib_frame_vector_args (f);
2418 to_next[0] = ci0;
2419 f->n_vectors = 1;
2420 vlib_put_frame_to_node (vm, next_index, f);
2421
2422 if (params->mrc != 0 && --radv_info->n_left == 0)
2423 stop_sending_rs (vm, radv_info);
2424 else
2425 {
2426 radv_info->sleep_interval =
2427 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2428 if (radv_info->sleep_interval > params->mrt)
2429 radv_info->sleep_interval =
2430 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2431
2432 radv_info->due_time = current_time + radv_info->sleep_interval;
2433
2434 if (params->mrd != 0
2435 && current_time > radv_info->start_time + params->mrd)
2436 stop_sending_rs (vm, radv_info);
2437 else
2438 *due_time = radv_info->due_time;
2439 }
2440
2441 return radv_info->keep_sending_rs;
2442}
2443
2444static uword
2445send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2446 vlib_frame_t * f0)
2447{
2448 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2449 ip6_radv_t *radv_info;
2450 uword *event_data = 0;
2451 f64 sleep_time = 1e9;
2452 f64 current_time;
2453 f64 due_time;
2454 f64 dt = 0;
2455
2456 while (true)
2457 {
2458 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2459 vlib_process_get_events (vm, &event_data);
2460 vec_reset_length (event_data);
2461
2462 current_time = vlib_time_now (vm);
2463 do
2464 {
2465 due_time = current_time + 1e9;
2466 /* *INDENT-OFF* */
2467 pool_foreach (radv_info, nm->if_radv_pool,
2468 ({
2469 if (check_send_rs (vm, radv_info, current_time, &dt)
2470 && (dt < due_time))
2471 due_time = dt;
2472 }));
2473 /* *INDENT-ON* */
2474 current_time = vlib_time_now (vm);
2475 }
2476 while (due_time < current_time);
2477
2478 sleep_time = due_time - current_time;
2479 }
2480
2481 return 0;
2482}
2483
2484/* *INDENT-OFF* */
2485VLIB_REGISTER_NODE (send_rs_process_node) = {
2486 .function = send_rs_process,
2487 .type = VLIB_NODE_TYPE_PROCESS,
2488 .name = "send-rs-process",
2489};
2490/* *INDENT-ON* */
2491
2492void
2493icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2494 icmp6_send_router_solicitation_params_t *
2495 params)
2496{
2497 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2498 u32 rai;
2499 ip6_radv_t *ra = 0;
2500
2501 ASSERT (~0 != sw_if_index);
2502
2503 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2504 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2505
Juraj Sloboda52574522018-05-03 10:03:50 +02002506 stop_sending_rs (vm, ra);
2507
2508 if (!stop)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002509 {
2510 ra->keep_sending_rs = 1;
2511 ra->params = *params;
2512 ra->n_left = params->mrc;
2513 ra->start_time = vlib_time_now (vm);
2514 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2515 ra->due_time = 0; /* send first packet ASAP */
2516 ra->buffer = create_buffer_for_rs (vm, ra);
2517 if (!ra->buffer)
2518 ra->keep_sending_rs = 0;
2519 else
2520 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2521 }
2522}
2523
Neale Ranns87df12d2017-02-18 08:16:41 -08002524/**
2525 * @brief Add a multicast Address to the advertised MLD set
2526 */
2527static void
2528ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2529{
2530 ip6_mldp_group_t *mcast_group_info;
2531 uword *p;
2532
2533 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002534 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002535 mcast_group_info =
2536 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2537
2538 /* add address */
2539 if (!mcast_group_info)
2540 {
2541 /* add */
2542 u32 mi;
2543 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2544
2545 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002546 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002547 0);
2548
2549 mcast_group_info->type = 4;
2550 mcast_group_info->mcast_source_address_pool = 0;
2551 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002552 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002553 sizeof (ip6_address_t));
2554 }
2555}
2556
2557/**
2558 * @brief Delete a multicast Address from the advertised MLD set
2559 */
2560static void
2561ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2562{
2563 ip6_mldp_group_t *mcast_group_info;
2564 uword *p;
2565
2566 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2567 mcast_group_info =
2568 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2569
2570 if (mcast_group_info)
2571 {
2572 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2573 /* old_value */ 0);
2574 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2575 }
2576}
2577
2578/**
2579 * @brief Add a multicast Address to the advertised MLD set
2580 */
2581static void
2582ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2583 ip6_multicast_address_scope_t scope,
2584 ip6_multicast_link_local_group_id_t group)
2585{
2586 ip6_address_t addr;
2587
2588 ip6_set_reserved_multicast_address (&addr, scope, group);
2589
2590 ip6_neighbor_add_mld_prefix (a, &addr);
2591}
2592
2593/**
2594 * @brief create and initialize router advertisement parameters with default
2595 * values for this intfc
2596 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002597u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002598ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002599 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002600{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002601 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2602 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002603 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002604 vnet_sw_interface_t *sw_if0;
2605 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002606
2607 /* lookup radv container - ethernet interfaces only */
2608 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002609 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002610 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2611
Dave Barachd7cb1b52016-12-09 09:52:16 -05002612 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002613 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002614
2615 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2616 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002617 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2618
Dave Barachd7cb1b52016-12-09 09:52:16 -05002619 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002620 {
2621 a = pool_elt_at_index (nm->if_radv_pool, ri);
2622
Dave Barachd7cb1b52016-12-09 09:52:16 -05002623 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002624 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002625 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002626 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002627
Neale Ranns32e1c012016-11-22 17:07:28 +00002628 /* release the lock on the interface's mcast adj */
2629 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002630
Neale Ranns87df12d2017-02-18 08:16:41 -08002631 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002632 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002633 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002634 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002635 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002636 }));
2637 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002638 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002639 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002640 }));
2641 /* *INDENT-ON* */
2642
Neale Ranns87df12d2017-02-18 08:16:41 -08002643 pool_free (a->mldp_group_pool);
2644 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002645
Neale Ranns87df12d2017-02-18 08:16:41 -08002646 mhash_free (&a->address_to_prefix_index);
2647 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002648
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002649 if (a->keep_sending_rs)
2650 a->keep_sending_rs = 0;
2651
Dave Barachd7cb1b52016-12-09 09:52:16 -05002652 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002653 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2654 ri = ~0;
2655 }
2656 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002657 else
2658 {
2659 if (is_add)
2660 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002661 pool_get (nm->if_radv_pool, a);
2662
2663 ri = a - nm->if_radv_pool;
2664 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2665
2666 /* initialize default values (most of which are zero) */
Dave Barachb7b92992018-10-17 10:38:51 -04002667 clib_memset (a, 0, sizeof (a[0]));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002668
2669 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002670 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2671 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2672 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2673 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2674
Neale Ranns87df12d2017-02-18 08:16:41 -08002675 /* send ll address source address option */
2676 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002677
2678 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2679 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2680 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2681 a->seed = (u32) clib_cpu_time_now ();
2682 (void) random_u32 (&a->seed);
2683 a->randomizer = clib_cpu_time_now ();
2684 (void) random_u64 (&a->randomizer);
2685
2686 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2687 a->initial_adverts_sent = a->initial_adverts_count - 1;
2688 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2689
2690 /* deafult is to send */
2691 a->send_radv = 1;
2692
2693 /* fill in radv_info for this interface that will be needed later */
Ole Troand7231612018-06-07 10:17:57 +02002694 a->adv_link_mtu =
2695 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002696
2697 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2698
2699 /* fill in default link-local address (this may be overridden) */
2700 ip6_link_local_address_from_ethernet_address
2701 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002702
2703 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2704 sizeof (ip6_address_t));
2705 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2706 sizeof (ip6_address_t));
2707
Neale Ranns32e1c012016-11-22 17:07:28 +00002708 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2709 VNET_LINK_IP6,
2710 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002711
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002712 a->keep_sending_rs = 0;
2713
Dave Barachd7cb1b52016-12-09 09:52:16 -05002714 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002715 ip6_neighbor_add_mld_grp (a,
2716 IP6_MULTICAST_SCOPE_link_local,
2717 IP6_MULTICAST_GROUP_ID_all_hosts);
2718 ip6_neighbor_add_mld_grp (a,
2719 IP6_MULTICAST_SCOPE_link_local,
2720 IP6_MULTICAST_GROUP_ID_all_routers);
2721 ip6_neighbor_add_mld_grp (a,
2722 IP6_MULTICAST_SCOPE_link_local,
2723 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002724 }
2725 }
2726 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002727}
2728
2729/* send an mldpv2 report */
2730static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002731ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002732{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002733 vnet_main_t *vnm = vnet_get_main ();
2734 vlib_main_t *vm = vnm->vlib_main;
2735 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2736 vnet_sw_interface_t *sw_if0;
2737 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002738 u32 ri;
2739 int bogus_length;
2740
Dave Barachd7cb1b52016-12-09 09:52:16 -05002741 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002742 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002743 vlib_buffer_t *b0;
2744 ip6_header_t *ip0;
2745 u32 *to_next;
2746 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002747 u32 bo0;
2748 u32 n_to_alloc = 1;
2749 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002750
Ed Warnickecb9cada2015-12-08 15:45:58 -07002751 icmp6_multicast_listener_report_header_t *rh0;
2752 icmp6_multicast_listener_report_packet_t *rp0;
2753
2754 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2755 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2756 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2757
2758 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2759 return;
2760
2761 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002762 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2763 ~0);
2764
Ed Warnickecb9cada2015-12-08 15:45:58 -07002765 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002766
2767 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002768 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002769
Ed Warnickecb9cada2015-12-08 15:45:58 -07002770 /* send report now - build a mldpv2 report packet */
Damjan Marion671e60e2018-12-30 18:09:59 +01002771 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002772 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002773 {
Dave Barach3a63fc52019-01-07 09:15:47 -05002774 alloc_fail:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002775 clib_warning ("buffer allocation failure");
2776 return;
2777 }
2778
2779 b0 = vlib_get_buffer (vm, bo0);
2780
2781 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002782 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002783
Dave Barachd7cb1b52016-12-09 09:52:16 -05002784 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002785
2786 b0->error = ICMP6_ERROR_NONE;
2787
2788 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002789 ip0 = (ip6_header_t *) & rp0->ip;
2790 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002791
Dave Barachb7b92992018-10-17 10:38:51 -04002792 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002793
2794 ip0->ip_version_traffic_class_and_flow_label =
2795 clib_host_to_net_u32 (0x6 << 28);
2796
2797 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798 /* for DEBUG - vnet driver won't seem to emit router alerts */
2799 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2800 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002801
Ed Warnickecb9cada2015-12-08 15:45:58 -07002802 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002803
Ed Warnickecb9cada2015-12-08 15:45:58 -07002804 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002805 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2806 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002807
2808 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002809 ip6_set_reserved_multicast_address (&ip0->dst_address,
2810 IP6_MULTICAST_SCOPE_link_local,
2811 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2812
Ed Warnickecb9cada2015-12-08 15:45:58 -07002813 /* add reports here */
2814 ip6_mldp_group_t *m;
2815 int num_addr_records = 0;
2816 icmp6_multicast_address_record_t rr;
2817
2818 /* fill in the hop-by-hop extension header (router alert) info */
2819 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2820 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002821
Ed Warnickecb9cada2015-12-08 15:45:58 -07002822 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2823 rh0->alert.len = 2;
2824 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002825
Ed Warnickecb9cada2015-12-08 15:45:58 -07002826 rh0->pad.type = 1;
2827 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002828
Ed Warnickecb9cada2015-12-08 15:45:58 -07002829 rh0->icmp.checksum = 0;
2830
Dave Barachd7cb1b52016-12-09 09:52:16 -05002831 /* *INDENT-OFF* */
2832 pool_foreach (m, radv_info->mldp_group_pool,
2833 ({
2834 rr.type = m->type;
2835 rr.aux_data_len_u32s = 0;
2836 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2837 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838
Dave Barachd7cb1b52016-12-09 09:52:16 -05002839 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002840
Damjan Marionab9b7ec2019-01-18 20:24:44 +01002841 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2842 sizeof(icmp6_multicast_address_record_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05002843 {
2844 vlib_buffer_free (vm, &bo0, 1);
2845 goto alloc_fail;
2846 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002847
Dave Barachd7cb1b52016-12-09 09:52:16 -05002848 payload_length += sizeof( icmp6_multicast_address_record_t);
2849 }));
2850 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851
2852 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002853 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2854
Ed Warnickecb9cada2015-12-08 15:45:58 -07002855 /* update lengths */
2856 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2857
Dave Barachd7cb1b52016-12-09 09:52:16 -05002858 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2859 &bogus_length);
2860 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002861
Dave Barachd7cb1b52016-12-09 09:52:16 -05002862 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002863 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002864 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002865 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002866 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002867 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002868
Neale Ranns32e1c012016-11-22 17:07:28 +00002869 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002870 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002871
Neale Ranns32e1c012016-11-22 17:07:28 +00002872 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002873
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874 f = vlib_get_frame_to_node (vm, node->index);
2875 to_next = vlib_frame_vector_args (f);
2876 to_next[0] = bo0;
2877 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002878
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879 vlib_put_frame_to_node (vm, node->index, f);
2880 return;
2881}
2882
Dave Barachd7cb1b52016-12-09 09:52:16 -05002883/* *INDENT-OFF* */
2884VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2885{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002886 .function = icmp6_router_solicitation,
2887 .name = "icmp6-router-solicitation",
2888
2889 .vector_size = sizeof (u32),
2890
2891 .format_trace = format_icmp6_input_trace,
2892
2893 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2894 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002895 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002896 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002897 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2898 },
2899};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002900/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002901
2902/* send a RA or update the timer info etc.. */
2903static uword
2904ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002905 vlib_node_runtime_t * node,
2906 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002907{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002908 vnet_main_t *vnm = vnet_get_main ();
2909 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2910 ip6_radv_t *radv_info;
2911 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002912 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002913 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002914 u32 *to_next = 0;
2915 u32 bo0;
2916 icmp6_router_solicitation_header_t *h0;
2917 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002918 f64 now = vlib_time_now (vm);
2919
2920 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002921 /* *INDENT-OFF* */
2922 pool_foreach (radv_info, nm->if_radv_pool,
2923 ({
2924 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2925 {
2926 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2927 radv_info->next_multicast_time = now;
2928 radv_info->last_multicast_time = now;
2929 radv_info->last_radv_time = 0;
2930 radv_info->all_routers_mcast = 0;
2931 continue;
2932 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002933
Dave Barachd7cb1b52016-12-09 09:52:16 -05002934 /* Make sure that we've joined the all-routers multicast group */
2935 if(!radv_info->all_routers_mcast)
2936 {
2937 /* send MDLP_REPORT_EVENT message */
2938 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2939 radv_info->all_routers_mcast = 1;
2940 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002941
Dave Barachd7cb1b52016-12-09 09:52:16 -05002942 /* is it time to send a multicast RA on this interface? */
2943 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2944 {
2945 u32 n_to_alloc = 1;
2946 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002947
Dave Barachd7cb1b52016-12-09 09:52:16 -05002948 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2949 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002950
Dave Barachd7cb1b52016-12-09 09:52:16 -05002951 /* multicast send - compute next multicast send time */
2952 if( radv_info->initial_adverts_sent > 0)
2953 {
2954 radv_info->initial_adverts_sent--;
2955 if(rfn > radv_info-> initial_adverts_interval)
2956 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002957
Dave Barachd7cb1b52016-12-09 09:52:16 -05002958 /* check to see if we are ceasing to send */
2959 if( radv_info->initial_adverts_sent == 0)
2960 if(radv_info->cease_radv)
2961 radv_info->send_radv = 0;
2962 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002963
Dave Barachd7cb1b52016-12-09 09:52:16 -05002964 radv_info->next_multicast_time = rfn + now;
2965 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002966
Dave Barachd7cb1b52016-12-09 09:52:16 -05002967 /* send advert now - build a "solicted" router advert with unspecified source address */
Damjan Marion671e60e2018-12-30 18:09:59 +01002968 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002969
Dave Barachd7cb1b52016-12-09 09:52:16 -05002970 if (PREDICT_FALSE(n_allocated == 0))
2971 {
2972 clib_warning ("buffer allocation failure");
2973 continue;
2974 }
2975 b0 = vlib_get_buffer (vm, bo0);
2976 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2977 b0->error = ICMP6_ERROR_NONE;
2978 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2979
2980 h0 = vlib_buffer_get_current (b0);
2981
Dave Barachb7b92992018-10-17 10:38:51 -04002982 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002983
2984 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2985 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2986 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2987 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2988 h0->ip.hop_limit = 255;
2989
2990 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2991 h0->ip.src_address.as_u64[0] = 0;
2992 h0->ip.src_address.as_u64[1] = 0;
2993
2994 h0->ip.dst_address.as_u64[0] = 0;
2995 h0->ip.dst_address.as_u64[1] = 0;
2996
2997 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2998
2999 if (PREDICT_FALSE(f == 0))
3000 {
3001 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
3002 to_next = vlib_frame_vector_args (f);
3003 n_left_to_next = VLIB_FRAME_SIZE;
3004 n_this_frame = 0;
3005 }
3006
3007 n_this_frame++;
3008 n_left_to_next--;
3009 to_next[0] = bo0;
3010 to_next += 1;
3011
3012 if (PREDICT_FALSE(n_left_to_next == 0))
3013 {
3014 f->n_vectors = n_this_frame;
3015 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3016 f = 0;
3017 }
3018 }
3019 }));
3020 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003021
3022 if (f)
3023 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003024 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003025 f->n_vectors = n_this_frame;
3026 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3027 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003028 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003029}
3030
3031static uword
3032ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3033 vlib_node_runtime_t * node,
3034 vlib_frame_t * frame)
3035{
3036 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003037 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003038
3039 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003040
Ed Warnickecb9cada2015-12-08 15:45:58 -07003041 while (1)
3042 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003043 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003044
Dave Barachd7cb1b52016-12-09 09:52:16 -05003045 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003046
Dave Barachd7cb1b52016-12-09 09:52:16 -05003047 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003048 {
3049 /* No events found: timer expired. */
3050 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003051 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003052 }
3053 else
3054 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003055 switch (event_type)
3056 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003057
Dave Barachd7cb1b52016-12-09 09:52:16 -05003058 case ICMP6_ND_EVENT_INIT:
3059 break;
3060
3061 case ~0:
3062 break;
3063
3064 default:
3065 ASSERT (0);
3066 }
3067
Ed Warnickecb9cada2015-12-08 15:45:58 -07003068 if (event_data)
3069 _vec_len (event_data) = 0;
3070 }
3071 }
3072 return frame->n_vectors;
3073}
3074
Dave Barachd7cb1b52016-12-09 09:52:16 -05003075/* *INDENT-OFF* */
3076VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3077{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003078 .function = icmp6_router_advertisement,
3079 .name = "icmp6-router-advertisement",
3080
3081 .vector_size = sizeof (u32),
3082
3083 .format_trace = format_icmp6_input_trace,
3084
3085 .n_next_nodes = 1,
3086 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003087 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003088 },
3089};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003090/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003091
3092vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3093
3094 .function = ip6_icmp_neighbor_discovery_event_process,
3095 .name = "ip6-icmp-neighbor-discovery-event-process",
3096 .type = VLIB_NODE_TYPE_PROCESS,
3097};
3098
3099static uword
3100icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003101 vlib_node_runtime_t * node, vlib_frame_t * frame)
3102{
3103 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3104 /* is_solicitation */
3105 1);
3106}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003107
3108static uword
3109icmp6_neighbor_advertisement (vlib_main_t * vm,
3110 vlib_node_runtime_t * node,
3111 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003112{
3113 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3114 /* is_solicitation */
3115 0);
3116}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003117
Dave Barachd7cb1b52016-12-09 09:52:16 -05003118/* *INDENT-OFF* */
3119VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3120{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003121 .function = icmp6_neighbor_solicitation,
3122 .name = "icmp6-neighbor-solicitation",
3123
3124 .vector_size = sizeof (u32),
3125
3126 .format_trace = format_icmp6_input_trace,
3127
3128 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3129 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003130 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003131 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3132 },
3133};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003134/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003135
Dave Barachd7cb1b52016-12-09 09:52:16 -05003136/* *INDENT-OFF* */
3137VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3138{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003139 .function = icmp6_neighbor_advertisement,
3140 .name = "icmp6-neighbor-advertisement",
3141
3142 .vector_size = sizeof (u32),
3143
3144 .format_trace = format_icmp6_input_trace,
3145
3146 .n_next_nodes = 1,
3147 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003148 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003149 },
3150};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003151/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003152
Neale Rannsc7b8f202018-04-25 06:34:31 -07003153typedef enum
3154{
3155 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3156 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3157 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3158} ip6_discover_neighbor_next_t;
3159
3160typedef enum
3161{
3162 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3163 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3164 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3165} ip6_discover_neighbor_error_t;
3166
3167static uword
3168ip6_discover_neighbor_inline (vlib_main_t * vm,
3169 vlib_node_runtime_t * node,
3170 vlib_frame_t * frame, int is_glean)
3171{
3172 vnet_main_t *vnm = vnet_get_main ();
3173 ip6_main_t *im = &ip6_main;
3174 ip_lookup_main_t *lm = &im->lookup_main;
3175 u32 *from, *to_next_drop;
3176 uword n_left_from, n_left_to_next_drop;
Dave Barach49433ad2018-08-08 17:59:03 -04003177 u64 seed;
3178 u32 thread_index = vm->thread_index;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003179 int bogus_length;
3180 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3181
3182 if (node->flags & VLIB_NODE_FLAG_TRACE)
3183 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3184
Neale Rannscd35e532018-08-31 02:51:45 -07003185 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003186
3187 from = vlib_frame_vector_args (frame);
3188 n_left_from = frame->n_vectors;
3189
3190 while (n_left_from > 0)
3191 {
3192 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3193 to_next_drop, n_left_to_next_drop);
3194
3195 while (n_left_from > 0 && n_left_to_next_drop > 0)
3196 {
Neale Rannscd35e532018-08-31 02:51:45 -07003197 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003198 vnet_hw_interface_t *hw_if0;
3199 ip6_radv_t *radv_info;
Neale Rannscd35e532018-08-31 02:51:45 -07003200 ip_adjacency_t *adj0;
3201 vlib_buffer_t *p0;
3202 ip6_header_t *ip0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003203
3204 pi0 = from[0];
3205
3206 p0 = vlib_get_buffer (vm, pi0);
3207
3208 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3209
3210 ip0 = vlib_buffer_get_current (p0);
3211
3212 adj0 = adj_get (adj_index0);
3213
3214 if (!is_glean)
3215 {
3216 ip0->dst_address.as_u64[0] =
3217 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3218 ip0->dst_address.as_u64[1] =
3219 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3220 }
3221
Neale Rannsc7b8f202018-04-25 06:34:31 -07003222 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3223 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3224
Neale Rannscd35e532018-08-31 02:51:45 -07003225 /* combine the address and interface for a hash */
3226 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003227
Neale Rannscd35e532018-08-31 02:51:45 -07003228 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003229
3230 from += 1;
3231 n_left_from -= 1;
3232 to_next_drop[0] = pi0;
3233 to_next_drop += 1;
3234 n_left_to_next_drop -= 1;
3235
3236 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3237
3238 /* If the interface is link-down, drop the pkt */
3239 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3240 drop0 = 1;
3241
3242 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3243 {
3244 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3245
3246 if (ri != ~0)
3247 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3248 else
3249 drop0 = 1;
3250 }
3251 else
3252 drop0 = 1;
3253
3254 /*
3255 * the adj has been updated to a rewrite but the node the DPO that got
3256 * us here hasn't - yet. no big deal. we'll drop while we wait.
3257 */
3258 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3259 drop0 = 1;
3260
3261 p0->error =
3262 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3263 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3264
3265 if (drop0)
3266 continue;
3267
3268 {
3269 u32 bi0 = 0;
3270 icmp6_neighbor_solicitation_header_t *h0;
3271 vlib_buffer_t *b0;
3272
3273 h0 = vlib_packet_template_get_packet
3274 (vm, &im->discover_neighbor_packet_template, &bi0);
John Lo084606b2018-06-19 15:27:48 -04003275 if (!h0)
3276 continue;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003277
Neale Ranns45db8852019-01-09 00:04:04 -08003278 /* copy the persistent fields from the original */
3279 b0 = vlib_get_buffer (vm, bi0);
3280 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3281
Neale Rannsc7b8f202018-04-25 06:34:31 -07003282 /*
3283 * Build ethernet header.
3284 * Choose source address based on destination lookup
3285 * adjacency.
3286 */
3287 if (!ip6_src_address_for_packet (lm,
3288 sw_if_index0,
3289 &ip0->dst_address,
3290 &h0->ip.src_address))
3291 {
3292 /* There is no address on the interface */
3293 p0->error =
3294 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3295 vlib_buffer_free (vm, &bi0, 1);
3296 continue;
3297 }
3298
3299 /*
3300 * Destination address is a solicited node multicast address.
3301 * We need to fill in
3302 * the low 24 bits with low 24 bits of target's address.
3303 */
3304 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3305 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3306 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3307
3308 h0->neighbor.target_address = ip0->dst_address;
3309
3310 clib_memcpy (h0->link_layer_option.ethernet_address,
3311 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3312
3313 /* $$$$ appears we need this; why is the checksum non-zero? */
3314 h0->neighbor.icmp.checksum = 0;
3315 h0->neighbor.icmp.checksum =
3316 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3317 &bogus_length);
3318
3319 ASSERT (bogus_length == 0);
3320
3321 vlib_buffer_copy_trace_flag (vm, p0, bi0);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003322 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3323 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3324
3325 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3326 radv_info->mcast_adj_index;
3327
3328 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3329 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3330
3331 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3332 }
3333 }
3334
3335 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3336 n_left_to_next_drop);
3337 }
3338
3339 return frame->n_vectors;
3340}
3341
3342static uword
3343ip6_discover_neighbor (vlib_main_t * vm,
3344 vlib_node_runtime_t * node, vlib_frame_t * frame)
3345{
3346 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3347}
3348
3349static uword
3350ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3351{
3352 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3353}
3354
3355static char *ip6_discover_neighbor_error_strings[] = {
3356 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3357 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3358 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3359 = "no source address for ND solicitation",
3360};
3361
3362/* *INDENT-OFF* */
3363VLIB_REGISTER_NODE (ip6_glean_node) =
3364{
3365 .function = ip6_glean,
3366 .name = "ip6-glean",
3367 .vector_size = sizeof (u32),
3368 .format_trace = format_ip6_forward_next_trace,
3369 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3370 .error_strings = ip6_discover_neighbor_error_strings,
3371 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3372 .next_nodes =
3373 {
3374 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3375 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3376 },
3377};
3378VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3379{
3380 .function = ip6_discover_neighbor,
3381 .name = "ip6-discover-neighbor",
3382 .vector_size = sizeof (u32),
3383 .format_trace = format_ip6_forward_next_trace,
3384 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3385 .error_strings = ip6_discover_neighbor_error_strings,
3386 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3387 .next_nodes =
3388 {
3389 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3390 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3391 },
3392};
3393/* *INDENT-ON* */
3394
Dave Barachd7cb1b52016-12-09 09:52:16 -05003395/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003396int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003397ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3398 u8 suppress, u8 managed, u8 other,
3399 u8 ll_option, u8 send_unicast, u8 cease,
3400 u8 use_lifetime, u32 lifetime,
3401 u32 initial_count, u32 initial_interval,
3402 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003403{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003404 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3405 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003406 u32 ri;
3407
3408 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003409 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3410 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003411 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003412 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003413
Dave Barachd7cb1b52016-12-09 09:52:16 -05003414 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003415 {
3416
Dave Barachd7cb1b52016-12-09 09:52:16 -05003417 ip6_radv_t *radv_info;
3418 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003419
Dave Barachd7cb1b52016-12-09 09:52:16 -05003420 if ((max_interval != 0) && (min_interval == 0))
3421 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003422
Dave Barachd7cb1b52016-12-09 09:52:16 -05003423 max_interval =
3424 (max_interval !=
3425 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3426 radv_info->max_radv_interval;
3427 min_interval =
3428 (min_interval !=
3429 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3430 radv_info->min_radv_interval;
3431 lifetime =
3432 (use_lifetime !=
3433 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3434 radv_info->adv_router_lifetime_in_sec;
3435
3436 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003437 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003438 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003439 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003440
3441 if (lifetime <= max_interval)
3442 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003443 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003444
3445 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003446 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003447 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3448 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003449 }
3450
Dave Barachd7cb1b52016-12-09 09:52:16 -05003451 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3452 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3453 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003454
Dave Barachd7cb1b52016-12-09 09:52:16 -05003455 /*
3456 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3457 if "flag" is clear don't change corresponding value
3458 */
3459 radv_info->send_radv =
3460 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3461 radv_info->adv_managed_flag =
3462 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3463 radv_info->adv_other_flag =
3464 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3465 radv_info->adv_link_layer_address =
3466 (ll_option !=
3467 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3468 radv_info->send_unicast =
3469 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3470 radv_info->cease_radv =
3471 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3472
3473 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003474 radv_info->max_radv_interval = max_interval;
3475 radv_info->adv_router_lifetime_in_sec = lifetime;
3476
Dave Barachd7cb1b52016-12-09 09:52:16 -05003477 radv_info->initial_adverts_count =
3478 (initial_count !=
3479 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3480 radv_info->initial_adverts_count;
3481 radv_info->initial_adverts_interval =
3482 (initial_interval !=
3483 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3484 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003485
3486 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003487 if ((cease != 0) && (is_no))
3488 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003489
Dave Barachd7cb1b52016-12-09 09:52:16 -05003490 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3491 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003492 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003493 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003494 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003495 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003496}
3497
3498int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003499ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3500 ip6_address_t * prefix_addr, u8 prefix_len,
3501 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3502 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3503 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003504{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003505 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003506 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003507
Ed Warnickecb9cada2015-12-08 15:45:58 -07003508 u32 ri;
3509
3510 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003511 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3512 ~0);
3513
Ed Warnickecb9cada2015-12-08 15:45:58 -07003514 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3515
3516 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003517
3518 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003519 {
3520 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003521 ip6_radv_t *radv_info;
3522 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003523
3524 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003525 ip6_radv_prefix_t *prefix;
3526
Ed Warnickecb9cada2015-12-08 15:45:58 -07003527 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003528 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3529
Ed Warnickecb9cada2015-12-08 15:45:58 -07003530 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3531
Dave Barachd7cb1b52016-12-09 09:52:16 -05003532 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003533 {
3534 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003535 if (!prefix)
3536 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3537
3538 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003539 return VNET_API_ERROR_INVALID_VALUE_2;
3540
Dave Barachd7cb1b52016-12-09 09:52:16 -05003541 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003542 /* do specific delete processing here before returning */
3543 /* try to remove from routing table */
3544
Dave Barachd7cb1b52016-12-09 09:52:16 -05003545 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3546 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003547 pool_put (radv_info->adv_prefixes_pool, prefix);
3548
Dave Barachd7cb1b52016-12-09 09:52:16 -05003549 radv_info->initial_adverts_sent =
3550 radv_info->initial_adverts_count - 1;
3551 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003552 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003553 radv_info->last_radv_time = 0;
3554 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003555 }
3556
3557 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003558 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003559 {
3560 /* add */
3561 u32 pi;
3562 pool_get (radv_info->adv_prefixes_pool, prefix);
3563 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003564 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3565 /* old_value */ 0);
3566
Dave Barachb7b92992018-10-17 10:38:51 -04003567 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05003568
Ed Warnickecb9cada2015-12-08 15:45:58 -07003569 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003570 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3571
Ed Warnickecb9cada2015-12-08 15:45:58 -07003572 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003573 prefix->adv_on_link_flag = 1; /* L bit set */
3574 prefix->adv_autonomous_flag = 1; /* A bit set */
3575 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003576 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3577 prefix->enabled = 1;
3578 prefix->decrement_lifetime_flag = 1;
3579 prefix->deprecated_prefix_flag = 1;
3580
Dave Barachd7cb1b52016-12-09 09:52:16 -05003581 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003582 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003583 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003584 /* insert prefix into routing table as a connected prefix */
3585 }
3586
Dave Barachd7cb1b52016-12-09 09:52:16 -05003587 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003588 goto restart;
3589 }
3590 else
3591 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003592
3593 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003594 return VNET_API_ERROR_INVALID_VALUE_2;
3595
Dave Barachd7cb1b52016-12-09 09:52:16 -05003596 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003597 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003598 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003599 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003600 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003601 }
3602
Dave Barachd7cb1b52016-12-09 09:52:16 -05003603 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003604 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003605 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003606 prefix->adv_pref_lifetime_in_secs = ~0;
3607 prefix->decrement_lifetime_flag = 0;
3608 }
3609 else
3610 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003611 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3612 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003613 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003614
Ed Warnickecb9cada2015-12-08 15:45:58 -07003615 /* copy remaining */
3616 prefix->enabled = !(no_advertise != 0);
3617 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3618 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3619
Dave Barachd7cb1b52016-12-09 09:52:16 -05003620 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003621 /* restart */
3622 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003623 prefix->valid_lifetime_expires =
3624 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003625 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003626
3627 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3628 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003629 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003630 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003631 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003632 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003633}
3634
3635clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003636ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3637 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003638{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003639 vnet_main_t *vnm = vnet_get_main ();
3640 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3641 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003642 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003643 u8 suppress = 0, managed = 0, other = 0;
3644 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003645 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003646 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3647 0, ra_initial_interval = 0;
3648 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003649
Dave Barachd7cb1b52016-12-09 09:52:16 -05003650 unformat_input_t _line_input, *line_input = &_line_input;
3651 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003652
3653 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003654 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003655 ip6_address_t ip6_addr;
3656 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003657
Ed Warnickecb9cada2015-12-08 15:45:58 -07003658
3659 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003660 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003661 return 0;
3662
3663 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003664 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003665 {
3666
Dave Barachd7cb1b52016-12-09 09:52:16 -05003667 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003668 unformat_vnet_sw_interface, vnm, &sw_if_index))
3669 {
3670 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003671 ethernet_interface_t *eth_if0 = 0;
3672
Ed Warnickecb9cada2015-12-08 15:45:58 -07003673 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003674 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3675 eth_if0 =
3676 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3677
3678 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003679 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003680 error =
3681 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003682 goto done;
3683 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003684
Ed Warnickecb9cada2015-12-08 15:45:58 -07003685 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003686 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3687 sw_if_index, ~0);
3688
Ed Warnickecb9cada2015-12-08 15:45:58 -07003689 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003690
3691 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003692 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003693 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003694 }
3695 else
3696 {
3697 error = clib_error_return (0, "unknown interface %U'",
3698 format_unformat_error, line_input);
3699 goto done;
3700 }
3701 }
3702 else
3703 {
3704 error = clib_error_return (0, "invalid interface name %U'",
3705 format_unformat_error, line_input);
3706 goto done;
3707 }
3708 }
3709
3710 /* get the rest of the command */
3711 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3712 {
3713 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003714 is_no = 1;
3715 else if (unformat (line_input, "prefix %U/%d",
3716 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003717 {
3718 add_radv_info = 0;
3719 break;
3720 }
3721 else if (unformat (line_input, "ra-managed-config-flag"))
3722 {
3723 managed = 1;
3724 break;
3725 }
3726 else if (unformat (line_input, "ra-other-config-flag"))
3727 {
3728 other = 1;
3729 break;
3730 }
Chris Luke33879c92016-06-28 19:54:21 -04003731 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003732 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003733 {
Chris Luke33879c92016-06-28 19:54:21 -04003734 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003735 break;
3736 }
Chris Luke33879c92016-06-28 19:54:21 -04003737 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003738 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003739 {
Chris Luke33879c92016-06-28 19:54:21 -04003740 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003741 break;
3742 }
3743 else if (unformat (line_input, "ra-send-unicast"))
3744 {
3745 send_unicast = 1;
3746 break;
3747 }
3748 else if (unformat (line_input, "ra-lifetime"))
3749 {
3750 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003751 {
3752 error = unformat_parse_error (line_input);
3753 goto done;
3754 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003755 use_lifetime = 1;
3756 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003757 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003758 else if (unformat (line_input, "ra-initial"))
3759 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003760 if (!unformat
3761 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003762 {
3763 error = unformat_parse_error (line_input);
3764 goto done;
3765 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003766 break;
3767 }
3768 else if (unformat (line_input, "ra-interval"))
3769 {
3770 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003771 {
3772 error = unformat_parse_error (line_input);
3773 goto done;
3774 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003775
3776 if (!unformat (line_input, "%d", &ra_min_interval))
3777 ra_min_interval = 0;
3778 break;
3779 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003780 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003781 {
3782 cease = 1;
3783 break;
3784 }
3785 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003786 {
3787 error = unformat_parse_error (line_input);
3788 goto done;
3789 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003790 }
3791
Dave Barachd7cb1b52016-12-09 09:52:16 -05003792 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003793 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003794 ip6_neighbor_ra_config (vm, sw_if_index,
3795 suppress, managed, other,
3796 suppress_ll_option, send_unicast, cease,
3797 use_lifetime, ra_lifetime,
3798 ra_initial_count, ra_initial_interval,
3799 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003800 }
3801 else
3802 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003803 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003804 u32 pref_lifetime_in_secs = 0;
3805 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003806 u8 no_advertise = 0;
3807 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003808 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003809 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003810
3811 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003812 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003813 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003814 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003815 {
3816 use_prefix_default_values = 1;
3817 break;
3818 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003819 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003820 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003821 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003822 pref_lifetime_in_secs = ~0;
3823 break;
3824 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003825 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3826 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003827 break;
3828 else
3829 break;
3830 }
3831
3832
3833 /* get the rest of the command */
3834 while (!use_prefix_default_values &&
3835 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3836 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003837 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003838 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003839 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003840 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003841 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003842 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003843 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003844 no_onlink = 1;
3845 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003846 {
3847 error = unformat_parse_error (line_input);
3848 goto done;
3849 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003850 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003851
3852 ip6_neighbor_ra_prefix (vm, sw_if_index,
3853 &ip6_addr, addr_len,
3854 use_prefix_default_values,
3855 valid_lifetime_in_secs,
3856 pref_lifetime_in_secs,
3857 no_advertise,
3858 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003859 }
3860
Billy McFalla9a20e72017-02-15 11:39:12 -05003861done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003862 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003863
Ed Warnickecb9cada2015-12-08 15:45:58 -07003864 return error;
3865}
3866
Chris Lukebfd32fd2016-06-24 20:38:06 -04003867static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003868ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003869{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003870 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003871 u32 i;
3872
3873 for (i = 0; i < vec_len (addrs); i++)
3874 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003875 ip_interface_address_t *a =
3876 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3877 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003878
3879 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003880 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003881 }
3882}
3883
Ed Warnickecb9cada2015-12-08 15:45:58 -07003884static clib_error_t *
3885show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003886 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003887{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003888 vnet_main_t *vnm = vnet_get_main ();
3889 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3890 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003891 u32 sw_if_index;
3892
3893 sw_if_index = ~0;
3894
Dave Barachd7cb1b52016-12-09 09:52:16 -05003895 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003896 {
3897 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003898
Dave Barachd7cb1b52016-12-09 09:52:16 -05003899 /* look up the radv_t information for this interface */
3900 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3901 sw_if_index, ~0);
3902
3903 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3904
3905 if (ri != ~0)
3906 {
3907 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3908 ip6_radv_t *radv_info;
3909 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3910
3911 vlib_cli_output (vm, "%U is admin %s\n",
3912 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003913 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003914 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3915 "up" : "down"));
3916
Chris Lukebfd32fd2016-06-24 20:38:06 -04003917 u32 ai;
3918 u32 *link_scope = 0, *global_scope = 0;
3919 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003920 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003921
Dave Barachd7cb1b52016-12-09 09:52:16 -05003922 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3923 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003924 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3925
Dave Barachd7cb1b52016-12-09 09:52:16 -05003926 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003927 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003928 a = pool_elt_at_index (lm->if_address_pool, ai);
3929 ip6_address_t *address =
3930 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003931
Chris Lukebfd32fd2016-06-24 20:38:06 -04003932 if (ip6_address_is_link_local_unicast (address))
3933 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003934 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003935 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003936 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003937 vec_add1 (local_scope, ai);
3938 else
3939 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003940
3941 ai = a->next_this_sw_interface;
3942 }
3943
Dave Barachd7cb1b52016-12-09 09:52:16 -05003944 if (vec_len (link_scope))
3945 {
3946 vlib_cli_output (vm, "\tLink-local address(es):\n");
3947 ip6_print_addrs (vm, link_scope);
3948 vec_free (link_scope);
3949 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003950
Dave Barachd7cb1b52016-12-09 09:52:16 -05003951 if (vec_len (local_scope))
3952 {
3953 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3954 ip6_print_addrs (vm, local_scope);
3955 vec_free (local_scope);
3956 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003957
Dave Barachd7cb1b52016-12-09 09:52:16 -05003958 if (vec_len (global_scope))
3959 {
3960 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3961 ip6_print_addrs (vm, global_scope);
3962 vec_free (global_scope);
3963 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003964
Dave Barachd7cb1b52016-12-09 09:52:16 -05003965 if (vec_len (unknown_scope))
3966 {
3967 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3968 ip6_print_addrs (vm, unknown_scope);
3969 vec_free (unknown_scope);
3970 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003971
Neale Ranns53da2212018-02-24 02:11:19 -08003972 vlib_cli_output (vm, "\tLink-local address(es):\n");
3973 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3974 &radv_info->link_local_address);
3975
Ed Warnickecb9cada2015-12-08 15:45:58 -07003976 vlib_cli_output (vm, "\tJoined group address(es):\n");
3977 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003978 /* *INDENT-OFF* */
3979 pool_foreach (m, radv_info->mldp_group_pool,
3980 ({
3981 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3982 &m->mcast_address);
3983 }));
3984 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003985
3986 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003987 ip6_radv_prefix_t *p;
3988 /* *INDENT-OFF* */
3989 pool_foreach (p, radv_info->adv_prefixes_pool,
3990 ({
3991 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3992 format_ip6_address, &p->prefix, p->prefix_len);
3993 }));
3994 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003995
Dave Barachd7cb1b52016-12-09 09:52:16 -05003996 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003997 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3998 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3999 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
4000 vlib_cli_output (vm, "\tND DAD is disabled\n");
4001 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
4002 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
4003 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004004 vlib_cli_output (vm,
4005 "\tND advertised retransmit interval is %d (msec)\n",
4006 radv_info->
4007 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004008
4009 u32 ra_interval = radv_info->max_radv_interval;
4010 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004011 vlib_cli_output (vm,
4012 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004013 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004014 vlib_cli_output (vm,
4015 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004016 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004017 vlib_cli_output (vm,
4018 "\tHosts %s stateless autoconfig for addresses\n",
4019 (radv_info->adv_managed_flag) ? "use" :
4020 " don't use");
4021 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4022 radv_info->n_advertisements_sent);
4023 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4024 radv_info->n_solicitations_rcvd);
4025 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4026 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004027 }
4028 else
4029 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004030 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004031 format_unformat_error, input);
4032
4033 }
4034 }
4035 return error;
4036}
4037
Billy McFall0683c9c2016-10-13 08:27:31 -04004038/*?
4039 * This command is used to display various IPv6 attributes on a given
4040 * interface.
4041 *
4042 * @cliexpar
4043 * Example of how to display IPv6 settings:
4044 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4045 * GigabitEthernet2/0/0 is admin up
4046 * Link-local address(es):
4047 * fe80::ab8/64
4048 * Joined group address(es):
4049 * ff02::1
4050 * ff02::2
4051 * ff02::16
4052 * ff02::1:ff00:ab8
4053 * Advertised Prefixes:
4054 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4055 * MTU is 1500
4056 * ICMP error messages are unlimited
4057 * ICMP redirects are disabled
4058 * ICMP unreachables are not sent
4059 * ND DAD is disabled
4060 * ND advertised reachable time is 0
4061 * ND advertised retransmit interval is 0 (msec)
4062 * ND router advertisements are sent every 200 seconds (min interval is 150)
4063 * ND router advertisements live for 600 seconds
4064 * Hosts use stateless autoconfig for addresses
4065 * ND router advertisements sent 19336
4066 * ND router solicitations received 0
4067 * ND router solicitations dropped 0
4068 * @cliexend
4069 * Example of output if IPv6 is not enabled on the interface:
4070 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4071 * show ip6 interface: IPv6 not enabled on interface
4072 * @cliexend
4073?*/
4074/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004075VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4076{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004077 .path = "show ip6 interface",
4078 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004079 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004080};
Billy McFall0683c9c2016-10-13 08:27:31 -04004081/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004082
4083clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004084disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004085{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004086 clib_error_t *error = 0;
4087 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004088 u32 ri;
4089
4090 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004091 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4092 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004093 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004094
Ed Warnickecb9cada2015-12-08 15:45:58 -07004095 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004096 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004097 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004098 ip6_radv_t *radv_info;
4099
4100 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004101
4102 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004103 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004104 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004105 {
4106 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004107 ip6_ll_prefix_t ilp = {
4108 .ilp_addr = radv_info->link_local_address,
4109 .ilp_sw_if_index = sw_if_index,
4110 };
4111 ip6_ll_table_entry_delete (&ilp);
4112 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004113 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004114 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4115 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004116 }
4117 }
4118 return error;
4119}
4120
4121int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004122ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004123{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004124 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4125 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004126
Dave Barachd7cb1b52016-12-09 09:52:16 -05004127 /* look up the radv_t information for this interface */
4128 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4129 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004130
Dave Barachd7cb1b52016-12-09 09:52:16 -05004131 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004132
Dave Barachd7cb1b52016-12-09 09:52:16 -05004133 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004134}
4135
Dave Barachd7cb1b52016-12-09 09:52:16 -05004136clib_error_t *
4137enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004138{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004139 clib_error_t *error = 0;
4140 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004141 u32 ri;
4142 int is_add = 1;
4143
4144 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004145 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4146 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004147
Dave Barachd7cb1b52016-12-09 09:52:16 -05004148 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4149
4150 /* if not created yet */
4151 if (ri == ~0)
4152 {
4153 vnet_main_t *vnm = vnet_get_main ();
4154 vnet_sw_interface_t *sw_if0;
4155
4156 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4157 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4158 {
4159 ethernet_interface_t *eth_if0;
4160
4161 eth_if0 =
4162 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4163 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004164 {
4165 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004166 ri =
4167 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004168
Dave Barachd7cb1b52016-12-09 09:52:16 -05004169 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004170 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004171 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004172 ip6_address_t link_local_address;
4173
Dave Barachd7cb1b52016-12-09 09:52:16 -05004174 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004175
Dave Barachd7cb1b52016-12-09 09:52:16 -05004176 ip6_link_local_address_from_ethernet_mac_address
4177 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004178
4179 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004180 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07004181 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004182 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004183 {
4184 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004185 link_local_address.as_u64[1] =
4186 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004187
4188 link_local_address.as_u64[0] =
4189 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004190 /* clear u bit */
4191 link_local_address.as_u8[8] &= 0xfd;
4192 }
Neale Ranns53da2212018-02-24 02:11:19 -08004193 {
4194 ip6_ll_prefix_t ilp = {
4195 .ilp_addr = link_local_address,
4196 .ilp_sw_if_index = sw_if_index,
4197 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004198
Neale Ranns53da2212018-02-24 02:11:19 -08004199 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4200 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004201
Ed Warnickecb9cada2015-12-08 15:45:58 -07004202 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004203 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4204 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004205
Neale Ranns53da2212018-02-24 02:11:19 -08004206 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004207 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004208 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004209 }
4210 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004211 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004212 }
4213 }
4214 return error;
4215}
4216
Neale Ranns53da2212018-02-24 02:11:19 -08004217int
4218ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4219{
4220 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4221 ip6_radv_t *radv_info;
4222 u32 ri;
4223
Eyal Baricd307742018-07-22 12:45:15 +03004224 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
Neale Ranns53da2212018-02-24 02:11:19 -08004225 return 0;
4226
4227 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4228
4229 if (ri == ~0)
4230 return 0;
4231
4232 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4233 *addr = radv_info->link_local_address;
4234
4235 return (!0);
4236}
4237
Ed Warnickecb9cada2015-12-08 15:45:58 -07004238static clib_error_t *
4239enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004240 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004241{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004242 vnet_main_t *vnm = vnet_get_main ();
4243 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004244 u32 sw_if_index;
4245
4246 sw_if_index = ~0;
4247
Dave Barachd7cb1b52016-12-09 09:52:16 -05004248 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004249 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004250 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004251 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004252 else
4253 {
4254 error = clib_error_return (0, "unknown interface\n'",
4255 format_unformat_error, input);
4256
4257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004258 return error;
4259}
4260
Billy McFall0683c9c2016-10-13 08:27:31 -04004261/*?
4262 * This command is used to enable IPv6 on a given interface.
4263 *
4264 * @cliexpar
4265 * Example of how enable IPv6 on a given interface:
4266 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4267?*/
4268/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004269VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4270{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004271 .path = "enable ip6 interface",
4272 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004273 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004274};
Billy McFall0683c9c2016-10-13 08:27:31 -04004275/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004276
4277static clib_error_t *
4278disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004279 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004280{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004281 vnet_main_t *vnm = vnet_get_main ();
4282 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004283 u32 sw_if_index;
4284
4285 sw_if_index = ~0;
4286
Dave Barachd7cb1b52016-12-09 09:52:16 -05004287 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004288 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004289 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004290 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004291 else
4292 {
4293 error = clib_error_return (0, "unknown interface\n'",
4294 format_unformat_error, input);
4295
4296 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004297 return error;
4298}
4299
Billy McFall0683c9c2016-10-13 08:27:31 -04004300/*?
4301 * This command is used to disable IPv6 on a given interface.
4302 *
4303 * @cliexpar
4304 * Example of how disable IPv6 on a given interface:
4305 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4306?*/
4307/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004308VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4309{
Billy McFall0683c9c2016-10-13 08:27:31 -04004310 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004311 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004312 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004313};
Billy McFall0683c9c2016-10-13 08:27:31 -04004314/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004315
Billy McFall0683c9c2016-10-13 08:27:31 -04004316/*?
4317 * This command is used to configure the neighbor discovery
4318 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4319 * command to display some of the current neighbor discovery parameters
4320 * on a given interface. This command has three formats:
4321 *
4322 *
4323 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4324 *
4325 * '<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>'
4326 *
4327 * Where:
4328 *
4329 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4330 * router-advertisement messages to use stateful address
4331 * auto-configuration to obtain address information (sets the M-bit).
4332 * Default is the M-bit is not set and the '<em>no</em>' option
4333 * returns it to this default state.
4334 *
4335 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4336 * router-advertisement messages that hosts use stateful auto
4337 * configuration to obtain nonaddress related information (sets
4338 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4339 * option returns it to this default state.
4340 *
4341 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4342 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4343 * router-advertisement messages.
4344 *
4345 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4346 * optional source link-layer address in the ICMPv6 router-advertisement
4347 * messages. Default is to include the optional source link-layer address
4348 * and the '<em>no</em>' option returns it to this default state.
4349 *
4350 * <em>[no] ra-send-unicast</em> - Use the source address of the
4351 * router-solicitation message if availiable. The default is to use
4352 * multicast address of all nodes, and the '<em>no</em>' option returns
4353 * it to this default state.
4354 *
4355 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4356 * default router in ICMPv6 router-advertisement messages. The range is
4357 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4358 * '<em><max-interval></em>'. The default value is 600 seconds and the
4359 * '<em>no</em>' option returns it to this default value.
4360 *
4361 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4362 * router-advertisement messages sent and the interval between each
4363 * message. Range for count is 1 - 3 and default is 3. Range for interval
4364 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4365 * returns both to their default value.
4366 *
4367 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4368 * interval between sending ICMPv6 router-advertisement messages. The
4369 * range for max-interval is from 4 to 200 seconds. min-interval can not
4370 * be more than 75% of max-interval. If not set, min-interval will be
4371 * set to 75% of max-interval. The range for min-interval is from 3 to
4372 * 150 seconds. The '<em>no</em>' option returns both to their default
4373 * value.
4374 *
4375 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4376 * The '<em>no</em>' options implies to start (or restart) sending
4377 * ICMPv6 router-advertisement messages.
4378 *
4379 *
4380 * <b>Format 2 - Prefix Options:</b>
4381 *
4382 * '<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>'
4383 *
4384 * Where:
4385 *
4386 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4387 *
4388 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4389 * length of time in seconds during what the prefix is valid for the purpose of
4390 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4391 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4392 * length of time in seconds during what addresses generated from the prefix remain
4393 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4394 *
4395 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4396 * are inifinte, no timeout.
4397 *
4398 * <em>no-advertise</em> - Do not send full router address in prefix
4399 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4400 *
4401 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4402 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4403 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4404 *
4405 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4406 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4407 *
4408 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4409 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4410 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4411 * the L-bit.
4412 *
4413 *
4414 * <b>Format 3: - Default of Prefix:</b>
4415 *
4416 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4417 *
4418 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4419 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4420 * is ignored and the prefix is deleted.
4421 *
4422 *
4423 * @cliexpar
4424 * Example of how set a router advertisement option:
4425 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4426 * Example of how to add a prefix:
4427 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4428 * Example of how to delete a prefix:
4429 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4430?*/
4431/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004432VLIB_CLI_COMMAND (ip6_nd_command, static) =
4433{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004434 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004435 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004436 .function = ip6_neighbor_cmd,
4437};
Billy McFall0683c9c2016-10-13 08:27:31 -04004438/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004439
4440clib_error_t *
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004441ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4442 ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004443{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004444 clib_error_t *error = 0;
4445 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004446 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004447 ip6_radv_t *radv_info;
4448 vnet_main_t *vnm = vnet_get_main ();
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004449 ip6_ll_prefix_t pfx = { 0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07004450
Dave Barachd7cb1b52016-12-09 09:52:16 -05004451 if (!ip6_address_is_link_local_unicast (address))
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004452 return (error = clib_error_return (0, "address not link-local",
4453 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004454
4455 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004456 enable_ip6_interface (vm, sw_if_index);
4457
Ed Warnickecb9cada2015-12-08 15:45:58 -07004458 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004459
4460 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004461 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004462 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004463
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004464 pfx.ilp_sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004465
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004466 pfx.ilp_addr = radv_info->link_local_address;
4467 ip6_ll_table_entry_delete (&pfx);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004468
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004469 pfx.ilp_addr = *address;
4470 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004471
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004472 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4473 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004474 }
4475 else
4476 {
4477 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4478 error = clib_error_return (0, "ip6 not enabled for interface",
4479 format_unformat_error);
4480 }
4481 return error;
4482}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004483
Neale Ranns87df12d2017-02-18 08:16:41 -08004484/**
4485 * @brief callback when an interface address is added or deleted
4486 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004487static void
4488ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4489 uword opaque,
4490 u32 sw_if_index,
4491 ip6_address_t * address,
4492 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004493 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004494{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004495 vnet_main_t *vnm = vnet_get_main ();
4496 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004497 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004498 vlib_main_t *vm = vnm->vlib_main;
4499 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004500 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004501
4502 /* create solicited node multicast address for this interface adddress */
4503 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004504
Ed Warnickecb9cada2015-12-08 15:45:58 -07004505 a.as_u8[0xd] = address->as_u8[0xd];
4506 a.as_u8[0xe] = address->as_u8[0xe];
4507 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004508
4509 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004510 {
4511 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004512 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004513
4514 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004515 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4516 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004517 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004518 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004519 {
4520 /* get radv_info */
4521 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4522
4523 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004524 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004525 radv_info->ref_count++;
4526
Neale Ranns87df12d2017-02-18 08:16:41 -08004527 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004528 }
4529 }
4530 else
4531 {
4532
4533 /* delete */
4534 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004535 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4536 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004537 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004538
Dave Barachd7cb1b52016-12-09 09:52:16 -05004539 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004540 {
4541 /* get radv_info */
4542 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4543
Neale Ranns87df12d2017-02-18 08:16:41 -08004544 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004545
4546 /* if interface up send MLDP "report" */
4547 radv_info->all_routers_mcast = 0;
4548
4549 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004550 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004551 radv_info->ref_count--;
4552 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004553 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4554 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004555 }
4556}
4557
Dave Barachd7cb1b52016-12-09 09:52:16 -05004558clib_error_t *
4559ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004560{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004561 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004562
4563 nm->limit_neighbor_cache_size = neighbor_limit;
4564 return 0;
4565}
4566
Neale Ranns15002542017-09-10 04:39:11 -07004567static void
4568ip6_neighbor_table_bind (ip6_main_t * im,
4569 uword opaque,
4570 u32 sw_if_index,
4571 u32 new_fib_index, u32 old_fib_index)
4572{
4573 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4574 ip6_neighbor_t *n = NULL;
4575 u32 i, *to_re_add = 0;
4576
4577 /* *INDENT-OFF* */
4578 pool_foreach (n, nm->neighbor_pool,
4579 ({
4580 if (n->key.sw_if_index == sw_if_index)
4581 vec_add1 (to_re_add, n - nm->neighbor_pool);
4582 }));
4583 /* *INDENT-ON* */
4584
4585 for (i = 0; i < vec_len (to_re_add); i++)
4586 {
4587 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4588 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4589 ip6_neighbor_adj_fib_add (n, new_fib_index);
4590 }
4591 vec_free (to_re_add);
4592}
4593
Dave Barachd7cb1b52016-12-09 09:52:16 -05004594static clib_error_t *
4595ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004596{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004597 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4598 ip6_main_t *im = &ip6_main;
4599
Ed Warnickecb9cada2015-12-08 15:45:58 -07004600 mhash_init (&nm->neighbor_index_by_key,
4601 /* value size */ sizeof (uword),
4602 /* key size */ sizeof (ip6_neighbor_key_t));
4603
Dave Barachd7cb1b52016-12-09 09:52:16 -05004604 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4605 ip6_icmp_neighbor_solicitation_node.index);
4606 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4607 ip6_icmp_neighbor_advertisement_node.index);
4608 icmp6_register_type (vm, ICMP6_router_solicitation,
4609 ip6_icmp_router_solicitation_node.index);
4610 icmp6_register_type (vm, ICMP6_router_advertisement,
4611 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004612
4613 /* handler node for ip6 neighbor discovery events and timers */
4614 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4615
4616 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004617 ip6_add_del_interface_address_callback_t cb;
Dave Barachb7b92992018-10-17 10:38:51 -04004618 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05004619
Ed Warnickecb9cada2015-12-08 15:45:58 -07004620 /* when an interface address changes... */
4621 cb.function = ip6_neighbor_add_del_interface_address;
4622 cb.function_opaque = 0;
4623 vec_add1 (im->add_del_interface_address_callbacks, cb);
4624
Neale Ranns15002542017-09-10 04:39:11 -07004625 ip6_table_bind_callback_t cbt;
4626 cbt.function = ip6_neighbor_table_bind;
4627 cbt.function_opaque = 0;
4628 vec_add1 (im->table_bind_callbacks, cbt);
4629
Ed Warnickecb9cada2015-12-08 15:45:58 -07004630 mhash_init (&nm->pending_resolutions_by_address,
4631 /* value size */ sizeof (uword),
4632 /* key size */ sizeof (ip6_address_t));
4633
John Lo1edfba92016-08-27 01:11:57 -04004634 mhash_init (&nm->mac_changes_by_address,
4635 /* value size */ sizeof (uword),
4636 /* key size */ sizeof (ip6_address_t));
4637
Ed Warnickecb9cada2015-12-08 15:45:58 -07004638 /* default, configurable */
4639 nm->limit_neighbor_cache_size = 50000;
4640
Eyal Baric125ecc2017-09-20 11:29:17 +03004641 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4642
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004643 nm->ip6_ra_publisher_node = (uword) ~ 0;
4644
Ed Warnickecb9cada2015-12-08 15:45:58 -07004645#if 0
4646 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004647 vec_validate_init_empty
4648 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004649#endif
4650
Ed Warnickecb9cada2015-12-08 15:45:58 -07004651 return 0;
4652}
4653
4654VLIB_INIT_FUNCTION (ip6_neighbor_init);
4655
4656
Dave Barachd7cb1b52016-12-09 09:52:16 -05004657void
4658vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4659 void *address_arg,
4660 uword node_index,
4661 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004662{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004663 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4664 ip6_address_t *address = address_arg;
4665 uword *p;
4666 pending_resolution_t *pr;
4667
Ed Warnickecb9cada2015-12-08 15:45:58 -07004668 pool_get (nm->pending_resolutions, pr);
4669
4670 pr->next_index = ~0;
4671 pr->node_index = node_index;
4672 pr->type_opaque = type_opaque;
4673 pr->data = data;
4674
4675 p = mhash_get (&nm->pending_resolutions_by_address, address);
4676 if (p)
4677 {
4678 /* Insert new resolution at the head of the list */
4679 pr->next_index = p[0];
4680 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4681 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004682
4683 mhash_set (&nm->pending_resolutions_by_address, address,
4684 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004685}
4686
Dave Barachd7cb1b52016-12-09 09:52:16 -05004687int
4688vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4689 void *data_callback,
4690 u32 pid,
4691 void *address_arg,
4692 uword node_index,
4693 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004694{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004695 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4696 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004697
Eyal Barif9f40652017-04-01 03:55:08 +03004698 /* Try to find an existing entry */
4699 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4700 u32 *p = first;
4701 pending_resolution_t *mc;
4702 while (p && *p != ~0)
4703 {
4704 mc = pool_elt_at_index (nm->mac_changes, *p);
4705 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4706 && mc->pid == pid)
4707 break;
4708 p = &mc->next_index;
4709 }
4710
4711 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004712 if (is_add)
4713 {
Eyal Barif9f40652017-04-01 03:55:08 +03004714 if (found)
4715 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4716
John Lo1edfba92016-08-27 01:11:57 -04004717 pool_get (nm->mac_changes, mc);
Neale Rannsf12dad62018-06-04 18:41:24 -07004718 /* *INDENT-OFF* */
Eyal Barif9f40652017-04-01 03:55:08 +03004719 *mc = (pending_resolution_t)
4720 {
Neale Rannsf12dad62018-06-04 18:41:24 -07004721 .next_index = ~0,
4722 .node_index = node_index,
4723 .type_opaque = type_opaque,
4724 .data = data,
4725 .data_callback = data_callback,
4726 .pid = pid,
4727 };
4728 /* *INDENT-ON* */
John Lo1edfba92016-08-27 01:11:57 -04004729
Eyal Barif9f40652017-04-01 03:55:08 +03004730 /* Insert new resolution at the end of the list */
4731 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004732 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004733 p[0] = new_idx;
4734 else
4735 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004736 }
4737 else
4738 {
Eyal Barif9f40652017-04-01 03:55:08 +03004739 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004740 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004741
Eyal Barif9f40652017-04-01 03:55:08 +03004742 /* Clients may need to clean up pool entries, too */
4743 void (*fp) (u32, u8 *) = data_callback;
4744 if (fp)
4745 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04004746
Eyal Barif9f40652017-04-01 03:55:08 +03004747 /* Remove the entry from the list and delete the entry */
4748 *p = mc->next_index;
4749 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004750
Eyal Barif9f40652017-04-01 03:55:08 +03004751 /* Remove from hash if we deleted the last entry */
4752 if (*p == ~0 && p == first)
4753 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004754 }
Eyal Barif9f40652017-04-01 03:55:08 +03004755 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004756}
4757
Dave Barachd7cb1b52016-12-09 09:52:16 -05004758int
4759vnet_ip6_nd_term (vlib_main_t * vm,
4760 vlib_node_runtime_t * node,
4761 vlib_buffer_t * p0,
4762 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004763 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004764{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004765 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4766 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
John Lo1edfba92016-08-27 01:11:57 -04004767
4768 ndh = ip6_next_header (ip);
4769 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4770 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004771 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004772
4773 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4774 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4775 {
4776 u8 *t0 = vlib_add_trace (vm, node, p0,
4777 sizeof (icmp6_input_trace_t));
4778 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4779 }
4780
4781 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004782 if (PREDICT_FALSE
4783 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4784 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004785 {
Eyal Baric125ecc2017-09-20 11:29:17 +03004786 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004787 }
4788
4789 /* Check if MAC entry exsist for solicited target IP */
4790 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4791 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004792 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004793 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004794 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004795
4796 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004797 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004798 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4799 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004800 return 0; /* source link layer address option not present */
4801
John Lo1edfba92016-08-27 01:11:57 -04004802 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004803 macp =
4804 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004805 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004806 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004807 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004808 vlib_node_runtime_t *error_node =
4809 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004810 ip->dst_address = ip->src_address;
4811 ip->src_address = ndh->target_address;
4812 ip->hop_limit = 255;
4813 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004814 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004815 clib_memcpy (opt->ethernet_address, macp, 6);
4816 ndh->icmp.type = ICMP6_neighbor_advertisement;
4817 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004818 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4819 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004820 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004821 ndh->icmp.checksum =
4822 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4823 clib_memcpy (eth->dst_address, eth->src_address, 6);
4824 clib_memcpy (eth->src_address, macp, 6);
4825 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004826 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004827 return 1;
4828 }
John Lo1edfba92016-08-27 01:11:57 -04004829 }
4830
4831 return 0;
4832
4833}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004834
Neale Ranns3f844d02017-02-18 00:03:54 -08004835int
4836ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4837{
4838 u32 fib_index;
4839
4840 fib_prefix_t pfx = {
4841 .fp_len = 128,
4842 .fp_proto = FIB_PROTOCOL_IP6,
4843 .fp_addr = {
4844 .ip6 = *addr,
4845 },
4846 };
4847 ip46_address_t nh = {
4848 .ip6 = *addr,
4849 };
4850
4851 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4852
4853 if (~0 == fib_index)
4854 return VNET_API_ERROR_NO_SUCH_FIB;
4855
4856 if (is_del)
4857 {
4858 fib_table_entry_path_remove (fib_index,
4859 &pfx,
4860 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004861 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004862 &nh,
4863 sw_if_index,
4864 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4865 /* flush the ND cache of this address if it's there */
Neale Ranns0bdd3192018-09-07 11:04:52 -07004866 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
Neale Ranns3f844d02017-02-18 00:03:54 -08004867 }
4868 else
4869 {
4870 fib_table_entry_path_add (fib_index,
4871 &pfx,
4872 FIB_SOURCE_IP6_ND_PROXY,
4873 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004874 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004875 &nh,
4876 sw_if_index,
4877 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4878 }
4879 return (0);
4880}
4881
4882static clib_error_t *
4883set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4884 unformat_input_t * input, vlib_cli_command_t * cmd)
4885{
4886 vnet_main_t *vnm = vnet_get_main ();
4887 clib_error_t *error = 0;
4888 ip6_address_t addr;
4889 u32 sw_if_index;
4890 u8 is_del = 0;
4891
4892 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4893 {
4894 /* get the rest of the command */
4895 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4896 {
4897 if (unformat (input, "%U", unformat_ip6_address, &addr))
4898 break;
4899 else if (unformat (input, "delete") || unformat (input, "del"))
4900 is_del = 1;
4901 else
4902 return (unformat_parse_error (input));
4903 }
4904 }
4905
4906 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4907
4908 return error;
4909}
4910
4911/* *INDENT-OFF* */
4912VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4913{
4914 .path = "set ip6 nd proxy",
4915 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4916 .function = set_ip6_nd_proxy_cmd,
4917};
4918/* *INDENT-ON* */
4919
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004920void
Neale Ranns3be6b282016-12-20 14:24:01 +00004921ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004922{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004923 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4924 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004925 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004926
4927 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004928 pool_foreach (n, nm->neighbor_pool,
4929 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004930 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004931 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004932 adj_nbr_walk_nh6 (sw_if_index,
4933 &n->key.ip6_address,
4934 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004935 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004936 }));
4937 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08004938
4939 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4940
4941 if (ADJ_INDEX_INVALID != ai)
4942 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004943}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004944
John Lo8b81cb42017-06-26 01:40:20 -04004945void
Steven9f781d82018-06-05 11:09:32 -07004946send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04004947{
4948 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04004949 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004950
Steven9f781d82018-06-05 11:09:32 -07004951 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004952}
4953
4954void
4955send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07004956 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07004957{
4958 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07004959 vnet_main_t *vnm = vnet_get_main ();
4960 u8 *rewrite, rewrite_len;
4961 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4962 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07004963
John Lo8b81cb42017-06-26 01:40:20 -04004964 if (ip6_addr)
4965 {
4966 clib_warning
4967 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4968 format_ip6_address, ip6_addr, sw_if_index);
4969
4970 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4971 int bogus_length;
4972 u32 bi = 0;
4973 icmp6_neighbor_solicitation_header_t *h =
4974 vlib_packet_template_get_packet (vm,
4975 &i6m->discover_neighbor_packet_template,
4976 &bi);
John Lo084606b2018-06-19 15:27:48 -04004977 if (!h)
4978 return;
4979
John Lo8b81cb42017-06-26 01:40:20 -04004980 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4981 IP6_MULTICAST_SCOPE_link_local,
4982 IP6_MULTICAST_GROUP_ID_all_hosts);
4983 h->ip.src_address = ip6_addr[0];
4984 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4985 h->neighbor.target_address = ip6_addr[0];
4986 h->neighbor.advertisement_flags = clib_host_to_net_u32
4987 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
ahdj00722130e12018-08-14 10:35:43 +08004988 h->link_layer_option.header.type =
4989 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo8b81cb42017-06-26 01:40:20 -04004990 clib_memcpy (h->link_layer_option.ethernet_address,
4991 hi->hw_address, vec_len (hi->hw_address));
4992 h->neighbor.icmp.checksum =
4993 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4994 ASSERT (bogus_length == 0);
4995
4996 /* Setup MAC header with IP6 Etype and mcast DMAC */
4997 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07004998 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04004999 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07005000 rewrite =
5001 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5002 rewrite_len = vec_len (rewrite);
5003 vlib_buffer_advance (b, -rewrite_len);
5004 ethernet_header_t *e = vlib_buffer_get_current (b);
5005 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5006 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04005007
5008 /* Send unsolicited ND advertisement packet out the specified interface */
5009 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5010 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5011 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5012 u32 *to_next = vlib_frame_vector_args (f);
5013 to_next[0] = bi;
5014 f->n_vectors = 1;
5015 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5016 }
5017}
5018
Dave Barachd7cb1b52016-12-09 09:52:16 -05005019/*
5020 * fd.io coding-style-patch-verification: ON
5021 *
5022 * Local Variables:
5023 * eval: (c-set-style "gnu")
5024 * End:
5025 */