blob: 3daea6bafbe79046b8ee687886b0e428eb4f12e1 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Billy McFall0683c9c2016-10-13 08:27:31 -040029/**
30 * @file
31 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
32 *
33 * The files contains the API and CLI code for managing IPv6 neighbor
34 * adjacency tables and neighbor discovery logic.
35 */
36
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010037/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#define ETHER_MAC_ADDR_LEN 6
39
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010040/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050041typedef struct
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 /* basic advertised information */
44 ip6_address_t prefix;
45 u8 prefix_len;
46 int adv_on_link_flag;
47 int adv_autonomous_flag;
48 u32 adv_valid_lifetime_in_secs;
49 u32 adv_pref_lifetime_in_secs;
50
51 /* advertised values are computed from these times if decrementing */
52 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050053 f64 pref_lifetime_expires;
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 /* local information */
56 int enabled;
57 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050058 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
60#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
61#define DEF_ADV_VALID_LIFETIME 2592000
62#define DEF_ADV_PREF_LIFETIME 604800
63
64 /* extensions are added here, mobile, DNS etc.. */
65} ip6_radv_prefix_t;
66
67
Dave Barachd7cb1b52016-12-09 09:52:16 -050068typedef struct
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 /* group information */
71 u8 type;
72 ip6_address_t mcast_address;
73 u16 num_sources;
74 ip6_address_t *mcast_source_address_pool;
75} ip6_mldp_group_t;
76
77/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050078typedef struct
79{
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050082 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 int adv_managed_flag;
84 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 u32 adv_neighbor_reachable_time_in_msec;
87 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
88
89 /* mtu option */
90 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050091
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050093 u8 link_layer_address[8];
94 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99 /* Hash table mapping address to index in interface advertised prefix pool. */
100 mhash_t address_to_prefix_index;
101
102 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500103 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 /* Hash table mapping address to index in mldp address pool. */
106 mhash_t address_to_mldp_index;
107
108 /* local information */
109 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500110 int send_radv; /* radv on/off on this interface - set by config */
111 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 int send_unicast;
113 int adv_link_layer_address;
114 int prefix_option;
115 int failed_device_check;
116 int all_routers_mcast;
117 u32 seed;
118 u64 randomizer;
119 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000120 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 /* timing information */
123#define DEF_MAX_RADV_INTERVAL 200
124#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125#define DEF_CURR_HOP_LIMIT 64
126#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
127#define MAX_DEF_RTR_LIFETIME 9000
128
Dave Barachd7cb1b52016-12-09 09:52:16 -0500129#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
130#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
131#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
132#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
133#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 f64 max_radv_interval;
136 f64 min_radv_interval;
137 f64 min_delay_between_radv;
138 f64 max_delay_between_radv;
139 f64 max_rtr_default_lifetime;
140
141 f64 last_radv_time;
142 f64 last_multicast_time;
143 f64 next_multicast_time;
144
145
146 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500147 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 u32 initial_adverts_sent;
149
150 /* stats */
151 u32 n_advertisements_sent;
152 u32 n_solicitations_rcvd;
153 u32 n_solicitations_dropped;
154
155 /* Link local address to use (defaults to underlying physical for logical interfaces */
156 ip6_address_t link_local_address;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100157
158 /* router solicitations sending state */
159 u8 keep_sending_rs; /* when true then next fields are valid */
160 icmp6_send_router_solicitation_params_t params;
161 f64 sleep_interval;
162 f64 due_time;
163 u32 n_left;
164 f64 start_time;
165 vlib_buffer_t *buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166} ip6_radv_t;
167
Dave Barachd7cb1b52016-12-09 09:52:16 -0500168typedef struct
169{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 u32 next_index;
171 uword node_index;
172 uword type_opaque;
173 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400174 /* Used for nd event notification only */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500175 void *data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400176 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177} pending_resolution_t;
178
179
Dave Barachd7cb1b52016-12-09 09:52:16 -0500180typedef struct
181{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500183 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
185 /* lite beer "glean" adjacency handling */
186 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500187 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
John Lo1edfba92016-08-27 01:11:57 -0400189 /* Mac address change notification */
190 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500191 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400192
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194
Dave Barachd7cb1b52016-12-09 09:52:16 -0500195 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
197 mhash_t neighbor_index_by_key;
198
Dave Barachd7cb1b52016-12-09 09:52:16 -0500199 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
Dave Barachd7cb1b52016-12-09 09:52:16 -0500201 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
203 /* Neighbor attack mitigation */
204 u32 limit_neighbor_cache_size;
205 u32 neighbor_delete_rotor;
206
Eyal Baric125ecc2017-09-20 11:29:17 +0300207 /* Wildcard nd report publisher */
208 uword wc_ip6_nd_publisher_node;
209 uword wc_ip6_nd_publisher_et;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100210
211 /* Router advertisement report publisher */
212 uword ip6_ra_publisher_node;
213 uword ip6_ra_publisher_et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214} ip6_neighbor_main_t;
215
Neale Ranns3f844d02017-02-18 00:03:54 -0800216/* ipv6 neighbor discovery - timer/event types */
217typedef enum
218{
219 ICMP6_ND_EVENT_INIT,
220} ip6_icmp_neighbor_discovery_event_type_t;
221
222typedef union
223{
224 u32 add_del_swindex;
225 struct
226 {
227 u32 up_down_swindex;
228 u32 fib_index;
229 } up_down_event;
230} ip6_icmp_neighbor_discovery_event_data_t;
231
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232static ip6_neighbor_main_t ip6_neighbor_main;
Juraj Sloboda52574522018-05-03 10:03:50 +0200233ip6_neighbor_public_main_t ip6_neighbor_public_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500234static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Eyal Baric125ecc2017-09-20 11:29:17 +0300236static void wc_nd_signal_report (wc_nd_report_t * r);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100237static void ra_signal_report (ra_report_t * r);
Eyal Baric125ecc2017-09-20 11:29:17 +0300238
239/**
240 * @brief publish wildcard arp event
241 * @param sw_if_index The interface on which the ARP entires are acted
242 */
243static int
244vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
245{
246 wc_nd_report_t r = {
247 .sw_if_index = sw_if_index,
248 .ip6 = *ip6,
249 };
250 memcpy (r.mac, mac, sizeof r.mac);
251
252 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
253 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
254 return 0;
255}
256
257static void
258wc_nd_signal_report (wc_nd_report_t * r)
259{
260 vlib_main_t *vm = vlib_get_main ();
261 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
262 uword ni = nm->wc_ip6_nd_publisher_node;
263 uword et = nm->wc_ip6_nd_publisher_et;
264
265 if (ni == (uword) ~ 0)
266 return;
267 wc_nd_report_t *q =
268 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
269
270 *q = *r;
271}
272
273void
274wc_nd_set_publisher_node (uword node_index, uword event_type)
275{
276 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
277 nm->wc_ip6_nd_publisher_node = node_index;
278 nm->wc_ip6_nd_publisher_et = event_type;
279}
280
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100281static int
282ra_publish (ra_report_t * r)
283{
284 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
285 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
286 return 0;
287}
288
289static void
290ra_signal_report (ra_report_t * r)
291{
292 vlib_main_t *vm = vlib_get_main ();
293 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
294 uword ni = nm->ip6_ra_publisher_node;
295 uword et = nm->ip6_ra_publisher_et;
296
297 if (ni == (uword) ~ 0)
298 return;
299 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
300
301 *q = *r;
302}
303
304void
305ra_set_publisher_node (uword node_index, uword event_type)
306{
307 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
308 nm->ip6_ra_publisher_node = node_index;
309 nm->ip6_ra_publisher_et = event_type;
310}
311
Dave Barachd7cb1b52016-12-09 09:52:16 -0500312static u8 *
313format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500315 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
316 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
317 vnet_main_t *vnm = vnet_get_main ();
318 vnet_sw_interface_t *si;
319 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Dave Barachd7cb1b52016-12-09 09:52:16 -0500321 if (!n)
Neale Ranns53da2212018-02-24 02:11:19 -0800322 return format (s, "%=12s%=25s%=6s%=20s%=40s", "Time", "Address", "Flags",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500323 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100324
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100325 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500326 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100327
328 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
Neale Rannsb3b2de72017-03-08 05:17:22 -0800331 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
332 flags = format (flags, "N");
333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
Neale Ranns53da2212018-02-24 02:11:19 -0800335 s = format (s, "%=12U%=25U%=6s%=20U%=40U",
John Lo7f358b32018-04-28 01:19:24 -0400336 format_vlib_time, vm, n->time_last_updated,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 format_ethernet_address, n->link_layer_address,
340 format_vnet_sw_interface_name, vnm, si);
341
Dave Barachd7cb1b52016-12-09 09:52:16 -0500342 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 return s;
344}
345
Neale Ranns15002542017-09-10 04:39:11 -0700346static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700347ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700348{
349 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
350 {
Neale Ranns53da2212018-02-24 02:11:19 -0800351 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
352 {
353 ip6_ll_prefix_t pfx = {
354 .ilp_addr = n->key.ip6_address,
355 .ilp_sw_if_index = n->key.sw_if_index,
356 };
357 ip6_ll_table_entry_delete (&pfx);
358 }
359 else
360 {
361 fib_prefix_t pfx = {
362 .fp_len = 128,
363 .fp_proto = FIB_PROTOCOL_IP6,
364 .fp_addr.ip6 = n->key.ip6_address,
365 };
366 fib_table_entry_path_remove (fib_index,
367 &pfx,
368 FIB_SOURCE_ADJ,
369 DPO_PROTO_IP6,
370 &pfx.fp_addr,
371 n->key.sw_if_index, ~0,
372 1, FIB_ROUTE_PATH_FLAG_NONE);
373 }
Neale Ranns15002542017-09-10 04:39:11 -0700374 }
375}
376
Dave Barachd7cb1b52016-12-09 09:52:16 -0500377typedef struct
378{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100380 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800381 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382 u8 link_layer_address[6];
383 u32 sw_if_index;
384 ip6_address_t addr;
385} ip6_neighbor_set_unset_rpc_args_t;
386
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387static void ip6_neighbor_set_unset_rpc_callback
388 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390static void set_unset_ip6_neighbor_rpc
391 (vlib_main_t * vm,
392 u32 sw_if_index,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800393 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
394 int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395{
396 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500397 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500398
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399 args.sw_if_index = sw_if_index;
400 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100401 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800402 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100403 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800404 if (NULL != link_layer_address)
405 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500406
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100411static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100413{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500414 icmp6_neighbor_solicitation_header_t *h;
415 vnet_main_t *vnm = vnet_get_main ();
416 ip6_main_t *im = &ip6_main;
417 ip_interface_address_t *ia;
418 ip6_address_t *dst, *src;
419 vnet_hw_interface_t *hi;
420 vnet_sw_interface_t *si;
421 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100422 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500423 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100424 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100425
Dave Barachd7cb1b52016-12-09 09:52:16 -0500426 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100427
Dave Barachd7cb1b52016-12-09 09:52:16 -0500428 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100429 dst = &adj->sub_type.nbr.next_hop.ip6;
430
431 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100432 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100433 return;
434 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500435 src = ip6_interface_address_matching_destination (im, dst,
436 adj->rewrite_header.
437 sw_if_index, &ia);
438 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100439 {
440 return;
441 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100442
Dave Barachd7cb1b52016-12-09 09:52:16 -0500443 h = vlib_packet_template_get_packet (vm,
444 &im->discover_neighbor_packet_template,
445 &bi);
Neale Rannsb80c5362016-10-08 13:03:40 +0100446
Dave Barachd7cb1b52016-12-09 09:52:16 -0500447 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100448
449 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
450 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
451 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
452 h->ip.src_address = src[0];
453 h->neighbor.target_address = dst[0];
454
455 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500456 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100457
458 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500459 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
460 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100461
462 b = vlib_get_buffer (vm, bi);
463 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500464 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100465
466 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500467 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
468 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100469
470 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500471 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
472 u32 *to_next = vlib_frame_vector_args (f);
473 to_next[0] = bi;
474 f->n_vectors = 1;
475 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100476 }
477}
478
479static void
480ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
481{
482 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
483 ethernet_build_rewrite (vnet_get_main (),
484 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500485 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100486 nbr->link_layer_address));
487}
488
489static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000490ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100491{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500492 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000493
Dave Barachd7cb1b52016-12-09 09:52:16 -0500494 adj_nbr_update_rewrite (ai,
495 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
496 ethernet_build_rewrite (vnet_get_main (),
497 adj->rewrite_header.
498 sw_if_index,
499 adj_get_link_type (ai),
500 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100501}
502
503#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
504{ \
505 k.sw_if_index = sw_if_index; \
506 k.ip6_address = *addr; \
507 k.pad = 0; \
508}
509
510static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500511ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100512{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500513 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
514 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100515 ip6_neighbor_key_t k;
516 uword *p;
517
Dave Barachd7cb1b52016-12-09 09:52:16 -0500518 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100519
520 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500521 if (p)
522 {
523 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
524 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100525
526 return (n);
527}
528
529static adj_walk_rc_t
530ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
531{
532 ip6_neighbor_t *nbr = ctx;
533
534 ip6_nd_mk_complete (ai, nbr);
535
536 return (ADJ_WALK_RC_CONTINUE);
537}
538
539static adj_walk_rc_t
540ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
541{
Neale Ranns19c68d22016-12-07 15:38:14 +0000542 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100543
544 return (ADJ_WALK_RC_CONTINUE);
545}
546
John Lo4fed5b12018-05-22 03:35:06 -0400547static clib_error_t *
548ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
549 u32 sw_if_index, u32 flags)
550{
551 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
552 ip6_neighbor_t *n;
553 u32 i, *to_delete = 0;
554
555 /* *INDENT-OFF* */
556 pool_foreach (n, nm->neighbor_pool,
557 ({
558 if (n->key.sw_if_index == sw_if_index)
559 vec_add1 (to_delete, n - nm->neighbor_pool);
560 }));
561 /* *INDENT-ON* */
562
563 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
564 {
565 for (i = 0; i < vec_len (to_delete); i++)
566 {
567 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
568 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
569 ip6_nd_mk_complete_walk, n);
570 }
571 }
572 else
573 {
574 for (i = 0; i < vec_len (to_delete); i++)
575 {
576 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
577 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
578 ip6_nd_mk_incomplete_walk, NULL);
579 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
580 continue;
581 ip6_neighbor_adj_fib_remove (n,
582 ip6_fib_table_get_index_for_sw_if_index
583 (n->key.sw_if_index));
584 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
585 pool_put (nm->neighbor_pool, n);
586 }
587 }
588
589 vec_free (to_delete);
590 return 0;
591}
592
593VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
594
Neale Rannsb80c5362016-10-08 13:03:40 +0100595void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500596ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100597{
598 ip6_neighbor_t *nbr;
599 ip_adjacency_t *adj;
600
601 adj = adj_get (ai);
602
603 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
604
Neale Ranns32e1c012016-11-22 17:07:28 +0000605 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100606 {
Ole Trøan438f6302018-02-15 21:56:06 +0000607 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800608 adj_glean_update_rewrite (ai);
609 break;
610 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000611 if (NULL != nbr)
612 {
613 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
614 ip6_nd_mk_complete_walk, nbr);
615 }
616 else
617 {
618 /*
619 * no matching ND entry.
620 * construct the rewrite required to for an ND packet, and stick
621 * that in the adj's pipe to smoke.
622 */
623 adj_nbr_update_rewrite (ai,
624 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
625 ethernet_build_rewrite (vnm,
626 sw_if_index,
627 VNET_LINK_IP6,
628 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
629
630 /*
631 * since the FIB has added this adj for a route, it makes sense it may
632 * want to forward traffic sometime soon. Let's send a speculative ND.
633 * just one. If we were to do periodically that wouldn't be bad either,
634 * but that's more code than i'm prepared to write at this time for
635 * relatively little reward.
636 */
637 ip6_nbr_probe (adj);
638 }
639 break;
640 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700641 {
642 /*
643 * Construct a partial rewrite from the known ethernet mcast dest MAC
644 */
645 u8 *rewrite;
646 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100647
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700648 rewrite = ethernet_build_rewrite (vnm,
649 sw_if_index,
650 adj->ia_link,
651 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000652
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700653 /*
654 * Complete the remaining fields of the adj's rewrite to direct the
655 * complete of the rewrite at switch time by copying in the IP
656 * dst address's bytes.
Neale Ranns889fe942017-06-01 05:43:19 -0400657 * Ofset is 2 bytes into the desintation address.
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700658 */
659 offset = vec_len (rewrite) - 2;
Neale Ranns889fe942017-06-01 05:43:19 -0400660 adj_mcast_update_rewrite (ai, rewrite, offset);
Neale Ranns32e1c012016-11-22 17:07:28 +0000661
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700662 break;
663 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000664 case IP_LOOKUP_NEXT_DROP:
665 case IP_LOOKUP_NEXT_PUNT:
666 case IP_LOOKUP_NEXT_LOCAL:
667 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800668 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000669 case IP_LOOKUP_NEXT_MIDCHAIN:
670 case IP_LOOKUP_NEXT_ICMP_ERROR:
671 case IP_LOOKUP_N_NEXT:
672 ASSERT (0);
673 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100674 }
675}
676
Neale Ranns15002542017-09-10 04:39:11 -0700677
678static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700679ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700680{
Neale Ranns53da2212018-02-24 02:11:19 -0800681 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
682 {
683 ip6_ll_prefix_t pfx = {
684 .ilp_addr = n->key.ip6_address,
685 .ilp_sw_if_index = n->key.sw_if_index,
686 };
687 n->fib_entry_index =
688 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
689 }
690 else
691 {
692 fib_prefix_t pfx = {
693 .fp_len = 128,
694 .fp_proto = FIB_PROTOCOL_IP6,
695 .fp_addr.ip6 = n->key.ip6_address,
696 };
Neale Ranns15002542017-09-10 04:39:11 -0700697
Neale Ranns53da2212018-02-24 02:11:19 -0800698 n->fib_entry_index =
699 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
700 FIB_ENTRY_FLAG_ATTACHED,
701 DPO_PROTO_IP6, &pfx.fp_addr,
702 n->key.sw_if_index, ~0, 1, NULL,
703 FIB_ROUTE_PATH_FLAG_NONE);
704 }
Neale Ranns15002542017-09-10 04:39:11 -0700705}
706
John Lo4fed5b12018-05-22 03:35:06 -0400707static ip6_neighbor_t *
708force_reuse_neighbor_entry (void)
709{
710 ip6_neighbor_t *n;
711 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
712 u32 count = 0;
713 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
714 if (index == ~0) /* Try again from elt 0 */
715 index = pool_next_index (nm->neighbor_pool, index);
716
717 /* Find a non-static random entry to free up for reuse */
718 do
719 {
720 if ((count++ == 100) || (index == ~0))
721 return NULL; /* give up after 100 entries */
722 n = pool_elt_at_index (nm->neighbor_pool, index);
723 nm->neighbor_delete_rotor = index;
724 index = pool_next_index (nm->neighbor_pool, index);
725 }
726 while (n->flags & IP6_NEIGHBOR_FLAG_STATIC);
727
728 /* Remove ARP entry from its interface and update fib */
729 adj_nbr_walk_nh6 (n->key.sw_if_index,
730 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
731 ip6_neighbor_adj_fib_remove
732 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
733 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
734
735 return n;
736}
737
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738int
739vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500740 u32 sw_if_index,
741 ip6_address_t * a,
742 u8 * link_layer_address,
743 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800744 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500746 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500748 ip6_neighbor_t *n = 0;
749 int make_new_nd_cache_entry = 1;
750 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500752 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753
Damjan Marion586afd72017-04-05 19:18:20 +0200754 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 {
756 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800757 1 /* set new neighbor */ , is_static,
758 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759 return 0;
760 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
762 k.sw_if_index = sw_if_index;
763 k.ip6_address = a[0];
764 k.pad = 0;
765
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500767 if (p)
768 {
769 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
770 /* Refuse to over-write static neighbor entry. */
771 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
772 return -2;
773 make_new_nd_cache_entry = 0;
774 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100775
Dave Barachd7cb1b52016-12-09 09:52:16 -0500776 if (make_new_nd_cache_entry)
777 {
John Lo4fed5b12018-05-22 03:35:06 -0400778 if (nm->limit_neighbor_cache_size &&
779 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
780 {
781 n = force_reuse_neighbor_entry ();
782 if (NULL == n)
783 return -2;
784 }
785 else
786 pool_get (nm->neighbor_pool, n);
787
Dave Barachd7cb1b52016-12-09 09:52:16 -0500788 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
789 /* old value */ 0);
790 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700791 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100792
Dave Barachd7cb1b52016-12-09 09:52:16 -0500793 clib_memcpy (n->link_layer_address,
794 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100795
Dave Barachd7cb1b52016-12-09 09:52:16 -0500796 /*
797 * create the adj-fib. the entry in the FIB table for and to the peer.
798 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800799 if (!is_no_fib_entry)
800 {
Neale Ranns53da2212018-02-24 02:11:19 -0800801 ip6_neighbor_adj_fib_add
802 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700803 }
804 else
805 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800806 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
807 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500808 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100809 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500810 {
811 /*
812 * prevent a DoS attack from the data-plane that
813 * spams us with no-op updates to the MAC address
814 */
815 if (0 == memcmp (n->link_layer_address,
816 link_layer_address, n_bytes_link_layer_address))
John Lo7f358b32018-04-28 01:19:24 -0400817 {
818 n->time_last_updated = vlib_time_now (vm);
819 goto check_customers;
820 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100821
Dave Barachd7cb1b52016-12-09 09:52:16 -0500822 clib_memcpy (n->link_layer_address,
823 link_layer_address, n_bytes_link_layer_address);
824 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825
Neale Rannsb80c5362016-10-08 13:03:40 +0100826 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400827 n->time_last_updated = vlib_time_now (vm);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100828 if (is_static)
John Lo4fed5b12018-05-22 03:35:06 -0400829 {
830 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
831 n->flags &= ~IP6_NEIGHBOR_FLAG_DYNAMIC;
832 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100833 else
John Lo4fed5b12018-05-22 03:35:06 -0400834 {
835 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
836 n->flags &= ~IP6_NEIGHBOR_FLAG_STATIC;
837 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100838
Neale Rannsb80c5362016-10-08 13:03:40 +0100839 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500840 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841
Dave Barach59b25652017-09-10 15:04:27 -0400842check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 /* Customer(s) waiting for this address to be resolved? */
844 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400845 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846 {
John Lo1edfba92016-08-27 01:11:57 -0400847 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500848
849 while (next_index != (u32) ~ 0)
850 {
John Lo1edfba92016-08-27 01:11:57 -0400851 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
852 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500853 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400854 next_index = pr->next_index;
855 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500856 }
John Lo1edfba92016-08-27 01:11:57 -0400857
858 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859 }
860
John Lo1edfba92016-08-27 01:11:57 -0400861 /* Customer(s) requesting ND event for this address? */
862 p = mhash_get (&nm->mac_changes_by_address, a);
863 if (p)
864 {
865 next_index = p[0];
866
Dave Barachd7cb1b52016-12-09 09:52:16 -0500867 while (next_index != (u32) ~ 0)
868 {
869 int (*fp) (u32, u8 *, u32, ip6_address_t *);
870 int rv = 1;
871 mc = pool_elt_at_index (nm->mac_changes, next_index);
872 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400873
Dave Barachd7cb1b52016-12-09 09:52:16 -0500874 /* Call the user's data callback, return 1 to suppress dup events */
875 if (fp)
876 rv =
877 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
878 /*
879 * Signal the resolver process, as long as the user
880 * says they want to be notified
881 */
882 if (rv == 0)
883 vlib_process_signal_event (vm, mc->node_index,
884 mc->type_opaque, mc->data);
885 next_index = mc->next_index;
886 }
John Lo1edfba92016-08-27 01:11:57 -0400887 }
888
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889 return 0;
890}
891
892int
893vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500894 u32 sw_if_index,
895 ip6_address_t * a,
896 u8 * link_layer_address,
897 uword n_bytes_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500899 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500901 ip6_neighbor_t *n;
902 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 int rv = 0;
904
Damjan Marion586afd72017-04-05 19:18:20 +0200905 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 {
907 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800908 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 return 0;
910 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911
912 k.sw_if_index = sw_if_index;
913 k.ip6_address = a[0];
914 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500915
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 p = mhash_get (&nm->neighbor_index_by_key, &k);
917 if (p == 0)
918 {
919 rv = -1;
920 goto out;
921 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500922
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100924
Neale Rannsb80c5362016-10-08 13:03:40 +0100925 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500926 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400927 ip6_neighbor_adj_fib_remove
928 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100929
John Lo4fed5b12018-05-22 03:35:06 -0400930 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500932
933out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934 return rv;
935}
936
Dave Barachd7cb1b52016-12-09 09:52:16 -0500937static void ip6_neighbor_set_unset_rpc_callback
938 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 vlib_main_t *vm = vlib_get_main ();
941 if (a->is_add)
942 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800943 a->link_layer_address, 6, a->is_static,
944 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500946 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
947 a->link_layer_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949
950static int
951ip6_neighbor_sort (void *a1, void *a2)
952{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953 vnet_main_t *vnm = vnet_get_main ();
954 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500956 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
957 n2->key.sw_if_index);
958 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
960 return cmp;
961}
962
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100963ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -0400964ip6_neighbors_pool (void)
965{
966 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
967 return nm->neighbor_pool;
968}
969
970ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100971ip6_neighbors_entries (u32 sw_if_index)
972{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500973 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100974 ip6_neighbor_t *n, *ns = 0;
975
976 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500977 pool_foreach (n, nm->neighbor_pool,
978 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100979 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
980 continue;
981 vec_add1 (ns, n[0]);
982 }));
983 /* *INDENT-ON* */
984
985 if (ns)
986 vec_sort_with_function (ns, ip6_neighbor_sort);
987 return ns;
988}
989
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990static clib_error_t *
991show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500992 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500994 vnet_main_t *vnm = vnet_get_main ();
995 ip6_neighbor_t *n, *ns;
996 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 u32 sw_if_index;
998
999 /* Filter entries by interface if given. */
1000 sw_if_index = ~0;
1001 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1002
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001003 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001004 if (ns)
1005 {
Billy McFall46529cd2016-10-14 10:45:49 -04001006 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001007 vec_foreach (n, ns)
1008 {
1009 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -04001010 }
1011 vec_free (ns);
1012 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013
1014 return error;
1015}
1016
Billy McFall0683c9c2016-10-13 08:27:31 -04001017/*?
1018 * This command is used to display the adjacent IPv6 hosts found via
1019 * neighbor discovery. Optionally, limit the output to the specified
1020 * interface.
1021 *
1022 * @cliexpar
1023 * Example of how to display the IPv6 neighbor adjacency table:
1024 * @cliexstart{show ip6 neighbors}
1025 * Time Address Flags Link layer Interface
1026 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1027 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1028 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1029 * @cliexend
1030 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1031 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1032 * Time Address Flags Link layer Interface
1033 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1034 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1035 * @cliexend
1036?*/
1037/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1039 .path = "show ip6 neighbors",
1040 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001041 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042};
Billy McFall0683c9c2016-10-13 08:27:31 -04001043/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044
1045static clib_error_t *
1046set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001047 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001049 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050 ip6_address_t addr;
1051 u8 mac_address[6];
1052 int addr_valid = 0;
1053 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001054 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001055 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056 u32 sw_if_index;
1057
Dave Barachd7cb1b52016-12-09 09:52:16 -05001058 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059 {
1060 /* intfc, ip6-address, mac-address */
1061 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001062 unformat_vnet_sw_interface, vnm, &sw_if_index,
1063 unformat_ip6_address, &addr,
1064 unformat_ethernet_address, mac_address))
1065 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066
1067 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001068 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001069 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001070 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001071 else if (unformat (input, "no-fib-entry"))
1072 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001074 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075 }
1076
1077 if (!addr_valid)
1078 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001079
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080 if (!is_del)
1081 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001082 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001083 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 else
1085 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001086 mac_address, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087 return 0;
1088}
1089
Billy McFall0683c9c2016-10-13 08:27:31 -04001090/*?
1091 * This command is used to manually add an entry to the IPv6 neighbor
1092 * adjacency table. Optionally, the entry can be added as static. It is
1093 * also used to remove an entry from the table. Use the '<em>show ip6
1094 * neighbors</em>' command to display all learned and manually entered entries.
1095 *
1096 * @cliexpar
1097 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1098 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1099 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1100 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1101?*/
1102/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001103VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1104{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105 .path = "set ip6 neighbor",
1106 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001107 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108};
Billy McFall0683c9c2016-10-13 08:27:31 -04001109/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110
Dave Barachd7cb1b52016-12-09 09:52:16 -05001111typedef enum
1112{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1114 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1115 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1116} icmp6_neighbor_solicitation_or_advertisement_next_t;
1117
1118static_always_inline uword
1119icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1120 vlib_node_runtime_t * node,
1121 vlib_frame_t * frame,
1122 uword is_solicitation)
1123{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001124 vnet_main_t *vnm = vnet_get_main ();
1125 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001127 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001128 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1129 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001130 vlib_node_runtime_t *error_node =
1131 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132 int bogus_length;
1133
1134 from = vlib_frame_vector_args (frame);
1135 n_left_from = n_packets;
1136 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001137
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138 if (node->flags & VLIB_NODE_FLAG_TRACE)
1139 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1140 /* stride */ 1,
1141 sizeof (icmp6_input_trace_t));
1142
Dave Barachd7cb1b52016-12-09 09:52:16 -05001143 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 (is_solicitation
1145 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1146 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1147 n_advertisements_sent = 0;
1148
1149 while (n_left_from > 0)
1150 {
1151 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1152
1153 while (n_left_from > 0 && n_left_to_next > 0)
1154 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001155 vlib_buffer_t *p0;
1156 ip6_header_t *ip0;
1157 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1158 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001160 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1161 int is_rewrite0;
1162 u32 ni0;
1163
Ed Warnickecb9cada2015-12-08 15:45:58 -07001164 bi0 = to_next[0] = from[0];
1165
1166 from += 1;
1167 to_next += 1;
1168 n_left_from -= 1;
1169 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001170
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171 p0 = vlib_get_buffer (vm, bi0);
1172 ip0 = vlib_buffer_get_current (p0);
1173 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001174 options_len0 =
1175 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176
1177 error0 = ICMP6_ERROR_NONE;
1178 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001179 ip6_sadd_link_local =
1180 ip6_address_is_link_local_unicast (&ip0->src_address);
1181 ip6_sadd_unspecified =
1182 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183
1184 /* Check that source address is unspecified, link-local or else on-link. */
1185 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1186 {
1187 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188
Dave Barachd7cb1b52016-12-09 09:52:16 -05001189 if (ADJ_INDEX_INVALID != src_adj_index0)
1190 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001191 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
Dave Barachd7cb1b52016-12-09 09:52:16 -05001193 /* Allow all realistic-looking rewrite adjacencies to pass */
1194 ni0 = adj0->lookup_next_index;
1195 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1196 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001197
Dave Barachd7cb1b52016-12-09 09:52:16 -05001198 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1199 || !is_rewrite0)
1200 ?
1201 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1202 : error0);
1203 }
1204 else
1205 {
1206 error0 =
1207 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1208 }
1209 }
1210
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 o0 = (void *) (h0 + 1);
1212 o0 = ((options_len0 == 8 && o0->header.type == option_type
1213 && o0->header.n_data_u64s == 1) ? o0 : 0);
1214
1215 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001216 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001217 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001218 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001219 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1220 is_solicitation ?
1221 &ip0->src_address :
1222 &h0->target_address,
1223 o0->ethernet_address,
1224 sizeof (o0->ethernet_address),
Neale Ranns53da2212018-02-24 02:11:19 -08001225 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001226 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227
1228 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1229 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001230 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001231 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001232 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233
Dave Barachd7cb1b52016-12-09 09:52:16 -05001234 fib_index =
1235 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001237 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001238 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001239 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1240 }
1241 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001242 {
Neale Ranns53da2212018-02-24 02:11:19 -08001243 if (ip6_address_is_link_local_unicast (&h0->target_address))
1244 {
1245 fei = ip6_fib_table_lookup_exact_match
1246 (ip6_ll_fib_get (sw_if_index0),
1247 &h0->target_address, 128);
1248 }
1249 else
1250 {
1251 fei = ip6_fib_table_lookup_exact_match (fib_index,
1252 &h0->target_address,
1253 128);
1254 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001255
Neale Ranns3f844d02017-02-18 00:03:54 -08001256 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001257 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001258 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001259 error0 =
1260 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001261 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001262 else
1263 {
1264 if (FIB_ENTRY_FLAG_LOCAL &
1265 fib_entry_get_flags_for_source (fei,
1266 FIB_SOURCE_INTERFACE))
1267 {
1268 /* It's an address that belongs to one of our interfaces
1269 * that's good. */
1270 }
1271 else
1272 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001273 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1274 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001275 {
1276 /* The address was added by IPv6 Proxy ND config.
1277 * We should only respond to these if the NS arrived on
1278 * the link that has a matching covering prefix */
1279 }
1280 else
1281 {
1282 error0 =
1283 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1284 }
1285 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001286 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287 }
1288
1289 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001290 next0 = (error0 != ICMP6_ERROR_NONE
1291 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1292 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001293 else
1294 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001295 next0 = 0;
1296 error0 = error0 == ICMP6_ERROR_NONE ?
1297 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298 }
1299
1300 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1301 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001302 vnet_sw_interface_t *sw_if0;
1303 ethernet_interface_t *eth_if0;
1304 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305
Dave Barachd7cb1b52016-12-09 09:52:16 -05001306 /* dst address is either source address or the all-nodes mcast addr */
1307 if (!ip6_sadd_unspecified)
1308 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001310 ip6_set_reserved_multicast_address (&ip0->dst_address,
1311 IP6_MULTICAST_SCOPE_link_local,
1312 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001313
1314 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001315 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 h0->icmp.type = ICMP6_neighbor_advertisement;
1317
1318 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1319 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001320 eth_if0 =
1321 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322 if (eth_if0 && o0)
1323 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001324 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1325 o0->header.type =
1326 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327 }
1328
1329 h0->advertisement_flags = clib_host_to_net_u32
1330 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1331 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1332
1333 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001334 h0->icmp.checksum =
1335 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1336 &bogus_length);
1337 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338
Dave Barachd7cb1b52016-12-09 09:52:16 -05001339 /* Reuse current MAC header, copy SMAC to DMAC and
1340 * interface MAC to SMAC */
1341 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1342 eth0 = vlib_buffer_get_current (p0);
1343 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1344 if (eth_if0)
1345 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346
Dave Barachd7cb1b52016-12-09 09:52:16 -05001347 /* Setup input and output sw_if_index for packet */
1348 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1349 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1350 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1351 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001352
1353 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001354 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001355
1356 p0->error = error_node->errors[error0];
1357
1358 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1359 to_next, n_left_to_next,
1360 bi0, next0);
1361 }
1362
1363 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1364 }
1365
1366 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001367 vlib_error_count (vm, error_node->node_index,
1368 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1369 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001370
1371 return frame->n_vectors;
1372}
1373
1374/* for "syslogging" - use elog for now */
1375#define foreach_log_level \
1376 _ (DEBUG, "DEBUG") \
1377 _ (INFO, "INFORMATION") \
1378 _ (NOTICE, "NOTICE") \
1379 _ (WARNING, "WARNING") \
1380 _ (ERR, "ERROR") \
1381 _ (CRIT, "CRITICAL") \
1382 _ (ALERT, "ALERT") \
1383 _ (EMERG, "EMERGENCY")
1384
Dave Barachd7cb1b52016-12-09 09:52:16 -05001385typedef enum
1386{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001387#define _(f,s) LOG_##f,
1388 foreach_log_level
1389#undef _
1390} log_level_t;
1391
Dave Barachd7cb1b52016-12-09 09:52:16 -05001392static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393#define _(f,s) s,
1394 foreach_log_level
1395#undef _
1396};
1397
Dave Barachd7cb1b52016-12-09 09:52:16 -05001398static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001399
1400static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001401ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001402{
1403 /* just use elog for now */
1404 u8 *what;
1405 va_list va;
1406
Dave Barachd7cb1b52016-12-09 09:52:16 -05001407 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1408 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409
1410 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001411 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001412 {
1413 what = va_format (0, fmt, &va);
1414
Dave Barachd7cb1b52016-12-09 09:52:16 -05001415 ELOG_TYPE_DECLARE (e) =
1416 {
1417 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1418 struct
1419 {
1420 u32 s[2];
1421 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001423 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1424 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425 }
1426 va_end (va);
1427 return;
1428}
1429
Juraj Sloboda52574522018-05-03 10:03:50 +02001430clib_error_t *
1431call_ip6_neighbor_callbacks (void *data,
1432 _vnet_ip6_neighbor_function_list_elt_t * elt)
1433{
1434 clib_error_t *error = 0;
1435
1436 while (elt)
1437 {
1438 error = elt->fp (data);
1439 if (error)
1440 return error;
1441 elt = elt->next_ip6_neighbor_function;
1442 }
1443
1444 return error;
1445}
1446
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001448typedef enum
1449{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001450 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1451 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1452 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1453 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1454} icmp6_router_solicitation_or_advertisement_next_t;
1455
1456static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001457icmp6_router_solicitation (vlib_main_t * vm,
1458 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001459{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001460 vnet_main_t *vnm = vnet_get_main ();
1461 ip6_main_t *im = &ip6_main;
1462 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001464 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001466 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001467 int bogus_length;
1468
1469 icmp6_neighbor_discovery_option_type_t option_type;
1470
Dave Barachd7cb1b52016-12-09 09:52:16 -05001471 vlib_node_runtime_t *error_node =
1472 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473
1474 from = vlib_frame_vector_args (frame);
1475 n_left_from = n_packets;
1476 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001477
Ed Warnickecb9cada2015-12-08 15:45:58 -07001478 if (node->flags & VLIB_NODE_FLAG_TRACE)
1479 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1480 /* stride */ 1,
1481 sizeof (icmp6_input_trace_t));
1482
1483 /* source may append his LL address */
1484 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1485
1486 while (n_left_from > 0)
1487 {
1488 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001489
Ed Warnickecb9cada2015-12-08 15:45:58 -07001490 while (n_left_from > 0 && n_left_to_next > 0)
1491 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001492 vlib_buffer_t *p0;
1493 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494 ip6_radv_t *radv_info = 0;
1495
Dave Barachd7cb1b52016-12-09 09:52:16 -05001496 icmp6_neighbor_discovery_header_t *h0;
1497 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1498
Ed Warnickecb9cada2015-12-08 15:45:58 -07001499 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001500 u32 is_solicitation = 1, is_dropped = 0;
1501 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502
1503 bi0 = to_next[0] = from[0];
1504
1505 from += 1;
1506 to_next += 1;
1507 n_left_from -= 1;
1508 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001509
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510 p0 = vlib_get_buffer (vm, bi0);
1511 ip0 = vlib_buffer_get_current (p0);
1512 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001513 options_len0 =
1514 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1515 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1516 is_link_local =
1517 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001518
1519 error0 = ICMP6_ERROR_NONE;
1520 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001521
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522 /* check if solicitation (not from nd_timer node) */
1523 if (ip6_address_is_unspecified (&ip0->dst_address))
1524 is_solicitation = 0;
1525
1526 /* Check that source address is unspecified, link-local or else on-link. */
1527 if (!is_unspecified && !is_link_local)
1528 {
1529 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530
Dave Barachd7cb1b52016-12-09 09:52:16 -05001531 if (ADJ_INDEX_INVALID != src_adj_index0)
1532 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001533 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001534
Dave Barachd7cb1b52016-12-09 09:52:16 -05001535 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1536 ?
1537 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1538 : error0);
1539 }
1540 else
1541 {
1542 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1543 }
1544 }
1545
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546 /* check for source LL option and process */
1547 o0 = (void *) (h0 + 1);
1548 o0 = ((options_len0 == 8
1549 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001550 && o0->header.n_data_u64s == 1) ? o0 : 0);
1551
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001553 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1554 !is_unspecified && !is_link_local))
1555 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001556 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1557 &ip0->src_address,
1558 o0->ethernet_address,
1559 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001560 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001561 }
1562
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563 /* default is to drop */
1564 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001565
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566 if (error0 == ICMP6_ERROR_NONE)
1567 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001568 vnet_sw_interface_t *sw_if0;
1569 ethernet_interface_t *eth_if0;
1570 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001571
1572 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1573 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001574 eth_if0 =
1575 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001576
1577 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001578 error0 =
1579 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1580 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001581
1582 if (error0 == ICMP6_ERROR_NONE)
1583 {
1584 u32 ri;
1585
1586 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001587 p0->current_length -=
1588 (options_len0 +
1589 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001590
1591 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001592 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1593 sw_if_index0)
1594 {
1595 ri =
1596 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001597
Neale Ranns8e254952018-04-25 05:40:48 -07001598 if (ri != ~0)
1599 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1600 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001601
1602 error0 =
1603 ((!radv_info) ?
1604 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1605 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606
1607 if (error0 == ICMP6_ERROR_NONE)
1608 {
1609 f64 now = vlib_time_now (vm);
1610
1611 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001612 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613 {
Neale Ranns75152282017-01-09 01:00:45 -08001614 if (0 != radv_info->last_radv_time &&
1615 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001616 MIN_DELAY_BETWEEN_RAS)
1617 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001618 else
1619 radv_info->last_radv_time = now;
1620 }
1621
1622 /* send now */
1623 icmp6_router_advertisement_header_t rh;
1624
1625 rh.icmp.type = ICMP6_router_advertisement;
1626 rh.icmp.code = 0;
1627 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001628
Ed Warnickecb9cada2015-12-08 15:45:58 -07001629 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001630 rh.router_lifetime_in_sec =
1631 clib_host_to_net_u16
1632 (radv_info->adv_router_lifetime_in_sec);
1633 rh.
1634 time_in_msec_between_retransmitted_neighbor_solicitations
1635 =
1636 clib_host_to_net_u32 (radv_info->
1637 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1638 rh.neighbor_reachable_time_in_msec =
1639 clib_host_to_net_u32 (radv_info->
1640 adv_neighbor_reachable_time_in_msec);
1641
1642 rh.flags =
1643 (radv_info->adv_managed_flag) ?
1644 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1645 0;
1646 rh.flags |=
1647 ((radv_info->adv_other_flag) ?
1648 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1649 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650
1651
Dave Barachd7cb1b52016-12-09 09:52:16 -05001652 u16 payload_length =
1653 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001654
1655 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001656 vlib_buffer_get_free_list_index
1657 (p0), bi0, (void *) &rh,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001658 sizeof
1659 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001660
Dave Barachd7cb1b52016-12-09 09:52:16 -05001661 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001662 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001663 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1664 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001665
Dave Barachd7cb1b52016-12-09 09:52:16 -05001666 h.header.type =
1667 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001668 h.header.n_data_u64s = 1;
1669
1670 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001671 clib_memcpy (&h.ethernet_address[0],
1672 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001673
1674 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001675 vlib_buffer_get_free_list_index
1676 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001677 sizeof
1678 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001679
Dave Barachd7cb1b52016-12-09 09:52:16 -05001680 payload_length +=
1681 sizeof
1682 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001684
Ed Warnickecb9cada2015-12-08 15:45:58 -07001685 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001686 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687 {
1688 icmp6_neighbor_discovery_mtu_option_t h;
1689
1690 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001691 h.mtu =
1692 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001693 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1694 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001695
1696 payload_length +=
1697 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
1699 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001700 vlib_buffer_get_free_list_index
1701 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001702 sizeof
1703 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001704 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001705
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001707 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001708
Dave Barachd7cb1b52016-12-09 09:52:16 -05001709 /* *INDENT-OFF* */
1710 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1711 ({
1712 if(pr_info->enabled &&
1713 (!pr_info->decrement_lifetime_flag
1714 || (pr_info->pref_lifetime_expires >0)))
1715 {
1716 /* advertise this prefix */
1717 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001718
Dave Barachd7cb1b52016-12-09 09:52:16 -05001719 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1720 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721
Dave Barachd7cb1b52016-12-09 09:52:16 -05001722 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723
Dave Barachd7cb1b52016-12-09 09:52:16 -05001724 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1725 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726
Dave Barachd7cb1b52016-12-09 09:52:16 -05001727 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1728 {
1729 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1730 h.preferred_time = 0;
1731 }
1732 else
1733 {
1734 if(pr_info->decrement_lifetime_flag)
1735 {
1736 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1737 (pr_info->valid_lifetime_expires - now) : 0;
1738
1739 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1740 (pr_info->pref_lifetime_expires - now) : 0;
1741 }
1742
1743 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1744 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1745 }
1746 h.unused = 0;
1747
1748 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1749
1750 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1751
1752 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001753 vlib_buffer_get_free_list_index (p0),
Dave Barachd7cb1b52016-12-09 09:52:16 -05001754 bi0,
1755 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1756
1757 }
1758 }));
1759 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001760
1761 /* add additional options before here */
1762
1763 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001764 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001765 {
1766 ip0->dst_address = ip0->src_address;
1767 }
1768 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001769 {
1770 /* target address is all-nodes mcast addr */
1771 ip6_set_reserved_multicast_address
1772 (&ip0->dst_address,
1773 IP6_MULTICAST_SCOPE_link_local,
1774 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001775 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001776
Ed Warnickecb9cada2015-12-08 15:45:58 -07001777 /* source address MUST be the link-local address */
1778 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001779
Dave Barachd7cb1b52016-12-09 09:52:16 -05001780 ip0->hop_limit = 255;
1781 ip0->payload_length =
1782 clib_host_to_net_u16 (payload_length);
1783
1784 icmp6_router_advertisement_header_t *rh0 =
1785 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1786 rh0->icmp.checksum =
1787 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1788 &bogus_length);
1789 ASSERT (bogus_length == 0);
1790
Ed Warnickecb9cada2015-12-08 15:45:58 -07001791 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001792 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001793 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001794
1795 if (is_solicitation)
1796 {
1797 ethernet_header_t *eth0;
1798 /* Reuse current MAC header, copy SMAC to DMAC and
1799 * interface MAC to SMAC */
1800 vlib_buffer_reset (p0);
1801 eth0 = vlib_buffer_get_current (p0);
1802 clib_memcpy (eth0->dst_address, eth0->src_address,
1803 6);
1804 clib_memcpy (eth0->src_address, eth_if0->address,
1805 6);
1806 next0 =
1807 is_dropped ? next0 :
1808 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1809 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1810 sw_if_index0;
1811 }
1812 else
1813 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001814 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001815 if (adj_index0 == 0)
1816 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1817 else
1818 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001819 next0 =
1820 is_dropped ? next0 :
1821 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1822 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1823 adj_index0;
1824 }
1825 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001826 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001827
1828 radv_info->n_solicitations_dropped += is_dropped;
1829 radv_info->n_solicitations_rcvd += is_solicitation;
1830
1831 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001832 {
1833 radv_info->n_advertisements_sent++;
1834 n_advertisements_sent++;
1835 }
1836 }
1837 }
1838 }
1839
1840 p0->error = error_node->errors[error0];
1841
Dave Barachd7cb1b52016-12-09 09:52:16 -05001842 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001843 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001844
Ed Warnickecb9cada2015-12-08 15:45:58 -07001845 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1846 to_next, n_left_to_next,
1847 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001848
Ed Warnickecb9cada2015-12-08 15:45:58 -07001849 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001850
Ed Warnickecb9cada2015-12-08 15:45:58 -07001851 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1852 }
1853
1854 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001855 vlib_error_count (vm, error_node->node_index,
1856 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1857 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001858
1859 return frame->n_vectors;
1860}
1861
1862 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1863static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001864icmp6_router_advertisement (vlib_main_t * vm,
1865 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001866{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001867 vnet_main_t *vnm = vnet_get_main ();
1868 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001869 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001870 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001871 u32 n_left_from, n_left_to_next, next_index;
1872 u32 n_advertisements_rcvd = 0;
1873
Dave Barachd7cb1b52016-12-09 09:52:16 -05001874 vlib_node_runtime_t *error_node =
1875 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001876
1877 from = vlib_frame_vector_args (frame);
1878 n_left_from = n_packets;
1879 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001880
Ed Warnickecb9cada2015-12-08 15:45:58 -07001881 if (node->flags & VLIB_NODE_FLAG_TRACE)
1882 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1883 /* stride */ 1,
1884 sizeof (icmp6_input_trace_t));
1885
1886 while (n_left_from > 0)
1887 {
1888 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001889
Ed Warnickecb9cada2015-12-08 15:45:58 -07001890 while (n_left_from > 0 && n_left_to_next > 0)
1891 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001892 vlib_buffer_t *p0;
1893 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001894 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001895 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001896 u32 bi0, options_len0, sw_if_index0, next0, error0;
1897
1898 bi0 = to_next[0] = from[0];
1899
1900 from += 1;
1901 to_next += 1;
1902 n_left_from -= 1;
1903 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001904
Ed Warnickecb9cada2015-12-08 15:45:58 -07001905 p0 = vlib_get_buffer (vm, bi0);
1906 ip0 = vlib_buffer_get_current (p0);
1907 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001908 options_len0 =
1909 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001910
1911 error0 = ICMP6_ERROR_NONE;
1912 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1913
Dave Barachd7cb1b52016-12-09 09:52:16 -05001914 /* Check that source address is link-local */
1915 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001916 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1917
1918 /* default is to drop */
1919 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001920
Ed Warnickecb9cada2015-12-08 15:45:58 -07001921 n_advertisements_rcvd++;
1922
1923 if (error0 == ICMP6_ERROR_NONE)
1924 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001925 vnet_sw_interface_t *sw_if0;
1926 ethernet_interface_t *eth_if0;
1927
Ed Warnickecb9cada2015-12-08 15:45:58 -07001928 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1929 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001930 eth_if0 =
1931 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001932
1933 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001934 error0 =
1935 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1936 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001937
1938 if (error0 == ICMP6_ERROR_NONE)
1939 {
1940 u32 ri;
1941
1942 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001943 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1944 sw_if_index0)
1945 {
1946 ri =
1947 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001948
Neale Ranns8e254952018-04-25 05:40:48 -07001949 if (ri != ~0)
1950 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1951 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001952
1953 error0 =
1954 ((!radv_info) ?
1955 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1956 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001957
1958 if (error0 == ICMP6_ERROR_NONE)
1959 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001960 radv_info->keep_sending_rs = 0;
1961
1962 ra_report_t r;
1963
1964 r.sw_if_index = sw_if_index0;
1965 memcpy (r.router_address, &ip0->src_address, 16);
1966 r.current_hop_limit = h0->current_hop_limit;
1967 r.flags = h0->flags;
1968 r.router_lifetime_in_sec =
1969 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
1970 r.neighbor_reachable_time_in_msec =
1971 clib_net_to_host_u32
1972 (h0->neighbor_reachable_time_in_msec);
1973 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
1974 r.prefixes = 0;
1975
Ed Warnickecb9cada2015-12-08 15:45:58 -07001976 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001977 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1978 && (h0->current_hop_limit !=
1979 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001980 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001981 ip6_neighbor_syslog (vm, LOG_WARNING,
1982 "our AdvCurHopLimit on %U doesn't agree with %U",
1983 format_vnet_sw_if_index_name,
1984 vnm, sw_if_index0,
1985 format_ip6_address,
1986 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001987 }
1988
Dave Barachd7cb1b52016-12-09 09:52:16 -05001989 if ((h0->flags &
1990 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1991 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001992 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001993 ip6_neighbor_syslog (vm, LOG_WARNING,
1994 "our AdvManagedFlag on %U doesn't agree with %U",
1995 format_vnet_sw_if_index_name,
1996 vnm, sw_if_index0,
1997 format_ip6_address,
1998 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001999 }
2000
Dave Barachd7cb1b52016-12-09 09:52:16 -05002001 if ((h0->flags &
2002 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2003 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002004 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002005 ip6_neighbor_syslog (vm, LOG_WARNING,
2006 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2007 format_vnet_sw_if_index_name,
2008 vnm, sw_if_index0,
2009 format_ip6_address,
2010 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011 }
2012
Dave Barachd7cb1b52016-12-09 09:52:16 -05002013 if ((h0->
2014 time_in_msec_between_retransmitted_neighbor_solicitations
2015 && radv_info->
2016 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2017 && (h0->
2018 time_in_msec_between_retransmitted_neighbor_solicitations
2019 !=
2020 clib_host_to_net_u32 (radv_info->
2021 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002022 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002023 ip6_neighbor_syslog (vm, LOG_WARNING,
2024 "our AdvRetransTimer on %U doesn't agree with %U",
2025 format_vnet_sw_if_index_name,
2026 vnm, sw_if_index0,
2027 format_ip6_address,
2028 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002029 }
2030
Dave Barachd7cb1b52016-12-09 09:52:16 -05002031 if ((h0->neighbor_reachable_time_in_msec &&
2032 radv_info->adv_neighbor_reachable_time_in_msec) &&
2033 (h0->neighbor_reachable_time_in_msec !=
2034 clib_host_to_net_u32
2035 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002036 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002037 ip6_neighbor_syslog (vm, LOG_WARNING,
2038 "our AdvReachableTime on %U doesn't agree with %U",
2039 format_vnet_sw_if_index_name,
2040 vnm, sw_if_index0,
2041 format_ip6_address,
2042 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002043 }
2044
2045 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002046 u8 *opt_hdr = (u8 *) (h0 + 1);
2047 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002048 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002049 icmp6_neighbor_discovery_option_header_t *o0 =
2050 (icmp6_neighbor_discovery_option_header_t *)
2051 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002052 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002053 icmp6_neighbor_discovery_option_type_t option_type =
2054 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002055
Dave Barachd7cb1b52016-12-09 09:52:16 -05002056 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002057 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002058 ip6_neighbor_syslog (vm, LOG_ERR,
2059 "malformed RA packet on %U from %U",
2060 format_vnet_sw_if_index_name,
2061 vnm, sw_if_index0,
2062 format_ip6_address,
2063 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002064 break;
2065 }
2066
Dave Barachd7cb1b52016-12-09 09:52:16 -05002067 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002068 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002069 ip6_neighbor_syslog (vm, LOG_ERR,
2070 " zero length option in RA on %U from %U",
2071 format_vnet_sw_if_index_name,
2072 vnm, sw_if_index0,
2073 format_ip6_address,
2074 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002075 break;
2076 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002077 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002078 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002079 ip6_neighbor_syslog (vm, LOG_ERR,
2080 "option length in RA packet greater than total length on %U from %U",
2081 format_vnet_sw_if_index_name,
2082 vnm, sw_if_index0,
2083 format_ip6_address,
2084 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002085 break;
2086 }
2087
2088 options_len0 -= opt_len;
2089 opt_hdr += opt_len;
2090
Dave Barachd7cb1b52016-12-09 09:52:16 -05002091 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002092 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002093 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2094 {
2095 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2096 * h =
2097 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2098 *) (o0);
2099
2100 if (opt_len < sizeof (*h))
2101 break;
2102
2103 memcpy (r.slla, h->ethernet_address, 6);
2104 }
2105 break;
2106
Ed Warnickecb9cada2015-12-08 15:45:58 -07002107 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002108 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002109 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002110 (icmp6_neighbor_discovery_mtu_option_t
2111 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002112
Dave Barachd7cb1b52016-12-09 09:52:16 -05002113 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002114 break;
2115
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002116 r.mtu = clib_net_to_host_u32 (h->mtu);
2117
Dave Barachd7cb1b52016-12-09 09:52:16 -05002118 if ((h->mtu && radv_info->adv_link_mtu) &&
2119 (h->mtu !=
2120 clib_host_to_net_u32
2121 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002122 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002123 ip6_neighbor_syslog (vm, LOG_WARNING,
2124 "our AdvLinkMTU on %U doesn't agree with %U",
2125 format_vnet_sw_if_index_name,
2126 vnm, sw_if_index0,
2127 format_ip6_address,
2128 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002129 }
2130 }
2131 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002132
Ed Warnickecb9cada2015-12-08 15:45:58 -07002133 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2134 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002135 icmp6_neighbor_discovery_prefix_information_option_t
2136 * h =
2137 (icmp6_neighbor_discovery_prefix_information_option_t
2138 *) (o0);
2139
Ed Warnickecb9cada2015-12-08 15:45:58 -07002140 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002141 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002142 u32 preferred, valid;
2143
Dave Barachd7cb1b52016-12-09 09:52:16 -05002144 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002145 break;
2146
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002147 vec_validate (r.prefixes,
2148 vec_len (r.prefixes));
2149 ra_report_prefix_info_t *prefix =
2150 vec_elt_at_index (r.prefixes,
2151 vec_len (r.prefixes) - 1);
2152
Dave Barachd7cb1b52016-12-09 09:52:16 -05002153 preferred =
2154 clib_net_to_host_u32 (h->preferred_time);
2155 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002156
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002157 prefix->preferred_time = preferred;
2158 prefix->valid_time = valid;
2159 prefix->flags = h->flags & 0xc0;
2160 prefix->dst_address_length =
2161 h->dst_address_length;
2162 prefix->dst_address = h->dst_address;
2163
Ed Warnickecb9cada2015-12-08 15:45:58 -07002164 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002165 /* *INDENT-OFF* */
2166 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2167 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002168
Dave Barachd7cb1b52016-12-09 09:52:16 -05002169 ip6_address_t mask;
2170 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2171
2172 if(pr_info->enabled &&
2173 (pr_info->prefix_len == h->dst_address_length) &&
2174 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2175 {
2176 /* found it */
2177 if(!pr_info->decrement_lifetime_flag &&
2178 valid != pr_info->adv_valid_lifetime_in_secs)
2179 {
2180 ip6_neighbor_syslog(vm, LOG_WARNING,
2181 "our ADV validlifetime on %U for %U does not agree with %U",
2182 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2183 format_ip6_address, &h->dst_address);
2184 }
2185 if(!pr_info->decrement_lifetime_flag &&
2186 preferred != pr_info->adv_pref_lifetime_in_secs)
2187 {
2188 ip6_neighbor_syslog(vm, LOG_WARNING,
2189 "our ADV preferredlifetime on %U for %U does not agree with %U",
2190 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2191 format_ip6_address, &h->dst_address);
2192 }
2193 }
2194 break;
2195 }));
2196 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002197 break;
2198 }
2199 default:
2200 /* skip this one */
2201 break;
2202 }
2203 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002204 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002205 }
2206 }
2207 }
2208
2209 p0->error = error_node->errors[error0];
2210
Dave Barachd7cb1b52016-12-09 09:52:16 -05002211 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002212 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002213
Ed Warnickecb9cada2015-12-08 15:45:58 -07002214 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2215 to_next, n_left_to_next,
2216 bi0, next0);
2217 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002218
Ed Warnickecb9cada2015-12-08 15:45:58 -07002219 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2220 }
2221
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002222 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002223 vlib_error_count (vm, error_node->node_index,
2224 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2225 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002226
2227 return frame->n_vectors;
2228}
2229
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002230static inline f64
2231random_f64_from_to (f64 from, f64 to)
2232{
2233 static u32 seed = 0;
2234 static u8 seed_set = 0;
2235 if (!seed_set)
2236 {
2237 seed = random_default_seed ();
2238 seed_set = 1;
2239 }
2240 return random_f64 (&seed) * (to - from) + from;
2241}
2242
2243static inline u8
2244get_mac_address (u32 sw_if_index, u8 * address)
2245{
2246 vnet_main_t *vnm = vnet_get_main ();
2247 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2248 if (!hw_if->hw_address)
2249 return 1;
2250 clib_memcpy (address, hw_if->hw_address, 6);
2251 return 0;
2252}
2253
2254static inline vlib_buffer_t *
2255create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2256{
2257 u32 bi0;
2258 vlib_buffer_t *p0;
2259 vlib_buffer_free_list_t *fl;
2260 icmp6_router_solicitation_header_t *rh;
2261 u16 payload_length;
2262 int bogus_length;
2263 u32 sw_if_index;
2264
2265 sw_if_index = radv_info->sw_if_index;
2266
2267 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2268 {
2269 clib_warning ("buffer allocation failure");
2270 return 0;
2271 }
2272
2273 p0 = vlib_get_buffer (vm, bi0);
2274 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2275 vlib_buffer_init_for_free_list (p0, fl);
2276 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2277 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2278
2279 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2280 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2281
2282 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2283
2284 rh = vlib_buffer_get_current (p0);
2285 p0->current_length = sizeof (*rh);
2286
2287 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2288 rh->neighbor.icmp.code = 0;
2289 rh->neighbor.icmp.checksum = 0;
2290 rh->neighbor.reserved_must_be_zero = 0;
2291
2292 rh->link_layer_option.header.type =
2293 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2294 if (0 != get_mac_address (sw_if_index,
2295 rh->link_layer_option.ethernet_address))
2296 {
2297 clib_warning ("interface with sw_if_index %u has no mac address",
2298 sw_if_index);
2299 vlib_buffer_free (vm, &bi0, 1);
2300 return 0;
2301 }
2302 rh->link_layer_option.header.n_data_u64s = 1;
2303
2304 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2305
2306 rh->ip.ip_version_traffic_class_and_flow_label =
2307 clib_host_to_net_u32 (0x6 << 28);
2308 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2309 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2310 rh->ip.hop_limit = 255;
2311 rh->ip.src_address = radv_info->link_local_address;
2312 /* set address ff02::2 */
2313 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02L << 48);
2314 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2315
2316 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2317 &rh->ip,
2318 &bogus_length);
2319
2320 return p0;
2321}
2322
2323static inline void
2324stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2325{
2326 u32 bi0;
2327
2328 ra->keep_sending_rs = 0;
2329 if (ra->buffer)
2330 {
2331 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2332 vlib_buffer_free (vm, &bi0, 1);
2333 ra->buffer = 0;
2334 }
2335}
2336
2337static inline bool
2338check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2339 f64 * due_time)
2340{
2341 vlib_buffer_t *p0;
2342 vlib_frame_t *f;
2343 u32 *to_next;
2344 u32 next_index;
2345 vlib_buffer_t *c0;
2346 u32 ci0;
2347
2348 icmp6_send_router_solicitation_params_t *params;
2349
2350 if (!radv_info->keep_sending_rs)
2351 return false;
2352
2353 params = &radv_info->params;
2354
2355 if (radv_info->due_time > current_time)
2356 {
2357 *due_time = radv_info->due_time;
2358 return true;
2359 }
2360
2361 p0 = radv_info->buffer;
2362
2363 next_index = ip6_rewrite_mcast_node.index;
2364
2365 c0 = vlib_buffer_copy (vm, p0);
2366 ci0 = vlib_get_buffer_index (vm, c0);
2367
2368 f = vlib_get_frame_to_node (vm, next_index);
2369 to_next = vlib_frame_vector_args (f);
2370 to_next[0] = ci0;
2371 f->n_vectors = 1;
2372 vlib_put_frame_to_node (vm, next_index, f);
2373
2374 if (params->mrc != 0 && --radv_info->n_left == 0)
2375 stop_sending_rs (vm, radv_info);
2376 else
2377 {
2378 radv_info->sleep_interval =
2379 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2380 if (radv_info->sleep_interval > params->mrt)
2381 radv_info->sleep_interval =
2382 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2383
2384 radv_info->due_time = current_time + radv_info->sleep_interval;
2385
2386 if (params->mrd != 0
2387 && current_time > radv_info->start_time + params->mrd)
2388 stop_sending_rs (vm, radv_info);
2389 else
2390 *due_time = radv_info->due_time;
2391 }
2392
2393 return radv_info->keep_sending_rs;
2394}
2395
2396static uword
2397send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2398 vlib_frame_t * f0)
2399{
2400 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2401 ip6_radv_t *radv_info;
2402 uword *event_data = 0;
2403 f64 sleep_time = 1e9;
2404 f64 current_time;
2405 f64 due_time;
2406 f64 dt = 0;
2407
2408 while (true)
2409 {
2410 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2411 vlib_process_get_events (vm, &event_data);
2412 vec_reset_length (event_data);
2413
2414 current_time = vlib_time_now (vm);
2415 do
2416 {
2417 due_time = current_time + 1e9;
2418 /* *INDENT-OFF* */
2419 pool_foreach (radv_info, nm->if_radv_pool,
2420 ({
2421 if (check_send_rs (vm, radv_info, current_time, &dt)
2422 && (dt < due_time))
2423 due_time = dt;
2424 }));
2425 /* *INDENT-ON* */
2426 current_time = vlib_time_now (vm);
2427 }
2428 while (due_time < current_time);
2429
2430 sleep_time = due_time - current_time;
2431 }
2432
2433 return 0;
2434}
2435
2436/* *INDENT-OFF* */
2437VLIB_REGISTER_NODE (send_rs_process_node) = {
2438 .function = send_rs_process,
2439 .type = VLIB_NODE_TYPE_PROCESS,
2440 .name = "send-rs-process",
2441};
2442/* *INDENT-ON* */
2443
2444void
2445icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2446 icmp6_send_router_solicitation_params_t *
2447 params)
2448{
2449 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2450 u32 rai;
2451 ip6_radv_t *ra = 0;
2452
2453 ASSERT (~0 != sw_if_index);
2454
2455 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2456 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2457
Juraj Sloboda52574522018-05-03 10:03:50 +02002458 stop_sending_rs (vm, ra);
2459
2460 if (!stop)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002461 {
2462 ra->keep_sending_rs = 1;
2463 ra->params = *params;
2464 ra->n_left = params->mrc;
2465 ra->start_time = vlib_time_now (vm);
2466 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2467 ra->due_time = 0; /* send first packet ASAP */
2468 ra->buffer = create_buffer_for_rs (vm, ra);
2469 if (!ra->buffer)
2470 ra->keep_sending_rs = 0;
2471 else
2472 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2473 }
2474}
2475
Neale Ranns87df12d2017-02-18 08:16:41 -08002476/**
2477 * @brief Add a multicast Address to the advertised MLD set
2478 */
2479static void
2480ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2481{
2482 ip6_mldp_group_t *mcast_group_info;
2483 uword *p;
2484
2485 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002486 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002487 mcast_group_info =
2488 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2489
2490 /* add address */
2491 if (!mcast_group_info)
2492 {
2493 /* add */
2494 u32 mi;
2495 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2496
2497 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002498 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002499 0);
2500
2501 mcast_group_info->type = 4;
2502 mcast_group_info->mcast_source_address_pool = 0;
2503 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002504 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002505 sizeof (ip6_address_t));
2506 }
2507}
2508
2509/**
2510 * @brief Delete a multicast Address from the advertised MLD set
2511 */
2512static void
2513ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2514{
2515 ip6_mldp_group_t *mcast_group_info;
2516 uword *p;
2517
2518 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2519 mcast_group_info =
2520 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2521
2522 if (mcast_group_info)
2523 {
2524 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2525 /* old_value */ 0);
2526 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2527 }
2528}
2529
2530/**
2531 * @brief Add a multicast Address to the advertised MLD set
2532 */
2533static void
2534ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2535 ip6_multicast_address_scope_t scope,
2536 ip6_multicast_link_local_group_id_t group)
2537{
2538 ip6_address_t addr;
2539
2540 ip6_set_reserved_multicast_address (&addr, scope, group);
2541
2542 ip6_neighbor_add_mld_prefix (a, &addr);
2543}
2544
2545/**
2546 * @brief create and initialize router advertisement parameters with default
2547 * values for this intfc
2548 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002549u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002550ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002551 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002552{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002553 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2554 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002555 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002556 vnet_sw_interface_t *sw_if0;
2557 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002558
2559 /* lookup radv container - ethernet interfaces only */
2560 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002561 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002562 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2563
Dave Barachd7cb1b52016-12-09 09:52:16 -05002564 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002565 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002566
2567 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2568 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002569 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2570
Dave Barachd7cb1b52016-12-09 09:52:16 -05002571 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002572 {
2573 a = pool_elt_at_index (nm->if_radv_pool, ri);
2574
Dave Barachd7cb1b52016-12-09 09:52:16 -05002575 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002576 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002577 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002578 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002579
Neale Ranns32e1c012016-11-22 17:07:28 +00002580 /* release the lock on the interface's mcast adj */
2581 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002582
Neale Ranns87df12d2017-02-18 08:16:41 -08002583 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002584 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002585 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002586 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002587 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002588 }));
2589 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002590 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002591 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002592 }));
2593 /* *INDENT-ON* */
2594
Neale Ranns87df12d2017-02-18 08:16:41 -08002595 pool_free (a->mldp_group_pool);
2596 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002597
Neale Ranns87df12d2017-02-18 08:16:41 -08002598 mhash_free (&a->address_to_prefix_index);
2599 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002600
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002601 if (a->keep_sending_rs)
2602 a->keep_sending_rs = 0;
2603
Dave Barachd7cb1b52016-12-09 09:52:16 -05002604 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002605 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2606 ri = ~0;
2607 }
2608 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002609 else
2610 {
2611 if (is_add)
2612 {
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002613 vnet_hw_interface_t *hw_if0;
2614
2615 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2616
Dave Barachd7cb1b52016-12-09 09:52:16 -05002617 pool_get (nm->if_radv_pool, a);
2618
2619 ri = a - nm->if_radv_pool;
2620 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2621
2622 /* initialize default values (most of which are zero) */
2623 memset (a, 0, sizeof (a[0]));
2624
2625 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002626 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2627 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2628 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2629 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2630
Neale Ranns87df12d2017-02-18 08:16:41 -08002631 /* send ll address source address option */
2632 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002633
2634 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2635 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2636 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2637 a->seed = (u32) clib_cpu_time_now ();
2638 (void) random_u32 (&a->seed);
2639 a->randomizer = clib_cpu_time_now ();
2640 (void) random_u64 (&a->randomizer);
2641
2642 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2643 a->initial_adverts_sent = a->initial_adverts_count - 1;
2644 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2645
2646 /* deafult is to send */
2647 a->send_radv = 1;
2648
2649 /* fill in radv_info for this interface that will be needed later */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02002650 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002651
2652 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2653
2654 /* fill in default link-local address (this may be overridden) */
2655 ip6_link_local_address_from_ethernet_address
2656 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002657
2658 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2659 sizeof (ip6_address_t));
2660 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2661 sizeof (ip6_address_t));
2662
Neale Ranns32e1c012016-11-22 17:07:28 +00002663 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2664 VNET_LINK_IP6,
2665 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002666
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002667 a->keep_sending_rs = 0;
2668
Dave Barachd7cb1b52016-12-09 09:52:16 -05002669 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002670 ip6_neighbor_add_mld_grp (a,
2671 IP6_MULTICAST_SCOPE_link_local,
2672 IP6_MULTICAST_GROUP_ID_all_hosts);
2673 ip6_neighbor_add_mld_grp (a,
2674 IP6_MULTICAST_SCOPE_link_local,
2675 IP6_MULTICAST_GROUP_ID_all_routers);
2676 ip6_neighbor_add_mld_grp (a,
2677 IP6_MULTICAST_SCOPE_link_local,
2678 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002679 }
2680 }
2681 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002682}
2683
2684/* send an mldpv2 report */
2685static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002686ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002687{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002688 vnet_main_t *vnm = vnet_get_main ();
2689 vlib_main_t *vm = vnm->vlib_main;
2690 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2691 vnet_sw_interface_t *sw_if0;
2692 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002693 u32 ri;
2694 int bogus_length;
2695
Dave Barachd7cb1b52016-12-09 09:52:16 -05002696 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002697 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002698 vlib_buffer_t *b0;
2699 ip6_header_t *ip0;
2700 u32 *to_next;
2701 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002702 u32 bo0;
2703 u32 n_to_alloc = 1;
2704 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002705
Ed Warnickecb9cada2015-12-08 15:45:58 -07002706 icmp6_multicast_listener_report_header_t *rh0;
2707 icmp6_multicast_listener_report_packet_t *rp0;
2708
2709 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2710 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2711 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2712
2713 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2714 return;
2715
2716 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002717 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2718 ~0);
2719
Ed Warnickecb9cada2015-12-08 15:45:58 -07002720 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002721
2722 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002723 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002724
Ed Warnickecb9cada2015-12-08 15:45:58 -07002725 /* send report now - build a mldpv2 report packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002726 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2727 &bo0,
2728 n_to_alloc,
2729 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2730 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002731 {
2732 clib_warning ("buffer allocation failure");
2733 return;
2734 }
2735
2736 b0 = vlib_get_buffer (vm, bo0);
2737
2738 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002739 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002740
Dave Barachd7cb1b52016-12-09 09:52:16 -05002741 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002742
2743 b0->error = ICMP6_ERROR_NONE;
2744
2745 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002746 ip0 = (ip6_header_t *) & rp0->ip;
2747 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002748
Dave Barachd7cb1b52016-12-09 09:52:16 -05002749 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2750
2751 ip0->ip_version_traffic_class_and_flow_label =
2752 clib_host_to_net_u32 (0x6 << 28);
2753
2754 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002755 /* for DEBUG - vnet driver won't seem to emit router alerts */
2756 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2757 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002758
Ed Warnickecb9cada2015-12-08 15:45:58 -07002759 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002760
Ed Warnickecb9cada2015-12-08 15:45:58 -07002761 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002762 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2763 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002764
2765 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002766 ip6_set_reserved_multicast_address (&ip0->dst_address,
2767 IP6_MULTICAST_SCOPE_link_local,
2768 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2769
Ed Warnickecb9cada2015-12-08 15:45:58 -07002770 /* add reports here */
2771 ip6_mldp_group_t *m;
2772 int num_addr_records = 0;
2773 icmp6_multicast_address_record_t rr;
2774
2775 /* fill in the hop-by-hop extension header (router alert) info */
2776 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2777 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002778
Ed Warnickecb9cada2015-12-08 15:45:58 -07002779 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2780 rh0->alert.len = 2;
2781 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002782
Ed Warnickecb9cada2015-12-08 15:45:58 -07002783 rh0->pad.type = 1;
2784 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002785
Ed Warnickecb9cada2015-12-08 15:45:58 -07002786 rh0->icmp.checksum = 0;
2787
Dave Barachd7cb1b52016-12-09 09:52:16 -05002788 /* *INDENT-OFF* */
2789 pool_foreach (m, radv_info->mldp_group_pool,
2790 ({
2791 rr.type = m->type;
2792 rr.aux_data_len_u32s = 0;
2793 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2794 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002795
Dave Barachd7cb1b52016-12-09 09:52:16 -05002796 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002797
Dave Barachd7cb1b52016-12-09 09:52:16 -05002798 vlib_buffer_add_data
Damjan Marion072401e2017-07-13 18:53:27 +02002799 (vm, vlib_buffer_get_free_list_index (b0), bo0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002800 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002801
Dave Barachd7cb1b52016-12-09 09:52:16 -05002802 payload_length += sizeof( icmp6_multicast_address_record_t);
2803 }));
2804 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002805
2806 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002807 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2808
Ed Warnickecb9cada2015-12-08 15:45:58 -07002809 /* update lengths */
2810 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2811
Dave Barachd7cb1b52016-12-09 09:52:16 -05002812 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2813 &bogus_length);
2814 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002815
Dave Barachd7cb1b52016-12-09 09:52:16 -05002816 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002817 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002818 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002819 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002820 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002821 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002822
Neale Ranns32e1c012016-11-22 17:07:28 +00002823 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002824 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002825
Neale Ranns32e1c012016-11-22 17:07:28 +00002826 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002827
Ed Warnickecb9cada2015-12-08 15:45:58 -07002828 f = vlib_get_frame_to_node (vm, node->index);
2829 to_next = vlib_frame_vector_args (f);
2830 to_next[0] = bo0;
2831 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002832
Ed Warnickecb9cada2015-12-08 15:45:58 -07002833 vlib_put_frame_to_node (vm, node->index, f);
2834 return;
2835}
2836
Dave Barachd7cb1b52016-12-09 09:52:16 -05002837/* *INDENT-OFF* */
2838VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2839{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002840 .function = icmp6_router_solicitation,
2841 .name = "icmp6-router-solicitation",
2842
2843 .vector_size = sizeof (u32),
2844
2845 .format_trace = format_icmp6_input_trace,
2846
2847 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2848 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002849 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002850 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2852 },
2853};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002854/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002855
2856/* send a RA or update the timer info etc.. */
2857static uword
2858ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002859 vlib_node_runtime_t * node,
2860 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002861{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002862 vnet_main_t *vnm = vnet_get_main ();
2863 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2864 ip6_radv_t *radv_info;
2865 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002866 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002867 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002868 u32 *to_next = 0;
2869 u32 bo0;
2870 icmp6_router_solicitation_header_t *h0;
2871 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 f64 now = vlib_time_now (vm);
2873
2874 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002875 /* *INDENT-OFF* */
2876 pool_foreach (radv_info, nm->if_radv_pool,
2877 ({
2878 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2879 {
2880 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2881 radv_info->next_multicast_time = now;
2882 radv_info->last_multicast_time = now;
2883 radv_info->last_radv_time = 0;
2884 radv_info->all_routers_mcast = 0;
2885 continue;
2886 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002887
Dave Barachd7cb1b52016-12-09 09:52:16 -05002888 /* Make sure that we've joined the all-routers multicast group */
2889 if(!radv_info->all_routers_mcast)
2890 {
2891 /* send MDLP_REPORT_EVENT message */
2892 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2893 radv_info->all_routers_mcast = 1;
2894 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002895
Dave Barachd7cb1b52016-12-09 09:52:16 -05002896 /* is it time to send a multicast RA on this interface? */
2897 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2898 {
2899 u32 n_to_alloc = 1;
2900 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002901
Dave Barachd7cb1b52016-12-09 09:52:16 -05002902 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2903 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002904
Dave Barachd7cb1b52016-12-09 09:52:16 -05002905 /* multicast send - compute next multicast send time */
2906 if( radv_info->initial_adverts_sent > 0)
2907 {
2908 radv_info->initial_adverts_sent--;
2909 if(rfn > radv_info-> initial_adverts_interval)
2910 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002911
Dave Barachd7cb1b52016-12-09 09:52:16 -05002912 /* check to see if we are ceasing to send */
2913 if( radv_info->initial_adverts_sent == 0)
2914 if(radv_info->cease_radv)
2915 radv_info->send_radv = 0;
2916 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917
Dave Barachd7cb1b52016-12-09 09:52:16 -05002918 radv_info->next_multicast_time = rfn + now;
2919 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002920
Dave Barachd7cb1b52016-12-09 09:52:16 -05002921 /* send advert now - build a "solicted" router advert with unspecified source address */
2922 n_allocated = vlib_buffer_alloc_from_free_list
2923 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002924
Dave Barachd7cb1b52016-12-09 09:52:16 -05002925 if (PREDICT_FALSE(n_allocated == 0))
2926 {
2927 clib_warning ("buffer allocation failure");
2928 continue;
2929 }
2930 b0 = vlib_get_buffer (vm, bo0);
2931 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2932 b0->error = ICMP6_ERROR_NONE;
2933 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2934
2935 h0 = vlib_buffer_get_current (b0);
2936
2937 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2938
2939 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2940 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2941 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2942 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2943 h0->ip.hop_limit = 255;
2944
2945 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2946 h0->ip.src_address.as_u64[0] = 0;
2947 h0->ip.src_address.as_u64[1] = 0;
2948
2949 h0->ip.dst_address.as_u64[0] = 0;
2950 h0->ip.dst_address.as_u64[1] = 0;
2951
2952 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2953
2954 if (PREDICT_FALSE(f == 0))
2955 {
2956 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2957 to_next = vlib_frame_vector_args (f);
2958 n_left_to_next = VLIB_FRAME_SIZE;
2959 n_this_frame = 0;
2960 }
2961
2962 n_this_frame++;
2963 n_left_to_next--;
2964 to_next[0] = bo0;
2965 to_next += 1;
2966
2967 if (PREDICT_FALSE(n_left_to_next == 0))
2968 {
2969 f->n_vectors = n_this_frame;
2970 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2971 f = 0;
2972 }
2973 }
2974 }));
2975 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002976
2977 if (f)
2978 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002979 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002980 f->n_vectors = n_this_frame;
2981 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2982 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002983 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002984}
2985
2986static uword
2987ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2988 vlib_node_runtime_t * node,
2989 vlib_frame_t * frame)
2990{
2991 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002992 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002993
2994 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002995
Ed Warnickecb9cada2015-12-08 15:45:58 -07002996 while (1)
2997 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002998 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07002999
Dave Barachd7cb1b52016-12-09 09:52:16 -05003000 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003001
Dave Barachd7cb1b52016-12-09 09:52:16 -05003002 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003003 {
3004 /* No events found: timer expired. */
3005 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003006 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003007 }
3008 else
3009 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003010 switch (event_type)
3011 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003012
Dave Barachd7cb1b52016-12-09 09:52:16 -05003013 case ICMP6_ND_EVENT_INIT:
3014 break;
3015
3016 case ~0:
3017 break;
3018
3019 default:
3020 ASSERT (0);
3021 }
3022
Ed Warnickecb9cada2015-12-08 15:45:58 -07003023 if (event_data)
3024 _vec_len (event_data) = 0;
3025 }
3026 }
3027 return frame->n_vectors;
3028}
3029
Dave Barachd7cb1b52016-12-09 09:52:16 -05003030/* *INDENT-OFF* */
3031VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3032{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003033 .function = icmp6_router_advertisement,
3034 .name = "icmp6-router-advertisement",
3035
3036 .vector_size = sizeof (u32),
3037
3038 .format_trace = format_icmp6_input_trace,
3039
3040 .n_next_nodes = 1,
3041 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003042 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003043 },
3044};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003045/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003046
3047vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3048
3049 .function = ip6_icmp_neighbor_discovery_event_process,
3050 .name = "ip6-icmp-neighbor-discovery-event-process",
3051 .type = VLIB_NODE_TYPE_PROCESS,
3052};
3053
3054static uword
3055icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003056 vlib_node_runtime_t * node, vlib_frame_t * frame)
3057{
3058 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3059 /* is_solicitation */
3060 1);
3061}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062
3063static uword
3064icmp6_neighbor_advertisement (vlib_main_t * vm,
3065 vlib_node_runtime_t * node,
3066 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003067{
3068 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3069 /* is_solicitation */
3070 0);
3071}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003072
Dave Barachd7cb1b52016-12-09 09:52:16 -05003073/* *INDENT-OFF* */
3074VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3075{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003076 .function = icmp6_neighbor_solicitation,
3077 .name = "icmp6-neighbor-solicitation",
3078
3079 .vector_size = sizeof (u32),
3080
3081 .format_trace = format_icmp6_input_trace,
3082
3083 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3084 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003085 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003086 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3087 },
3088};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003089/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003090
Dave Barachd7cb1b52016-12-09 09:52:16 -05003091/* *INDENT-OFF* */
3092VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3093{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003094 .function = icmp6_neighbor_advertisement,
3095 .name = "icmp6-neighbor-advertisement",
3096
3097 .vector_size = sizeof (u32),
3098
3099 .format_trace = format_icmp6_input_trace,
3100
3101 .n_next_nodes = 1,
3102 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003103 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003104 },
3105};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003106/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003107
Neale Rannsc7b8f202018-04-25 06:34:31 -07003108typedef enum
3109{
3110 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3111 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3112 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3113} ip6_discover_neighbor_next_t;
3114
3115typedef enum
3116{
3117 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3118 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3119 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3120} ip6_discover_neighbor_error_t;
3121
3122static uword
3123ip6_discover_neighbor_inline (vlib_main_t * vm,
3124 vlib_node_runtime_t * node,
3125 vlib_frame_t * frame, int is_glean)
3126{
3127 vnet_main_t *vnm = vnet_get_main ();
3128 ip6_main_t *im = &ip6_main;
3129 ip_lookup_main_t *lm = &im->lookup_main;
3130 u32 *from, *to_next_drop;
3131 uword n_left_from, n_left_to_next_drop;
3132 static f64 time_last_seed_change = -1e100;
3133 static u32 hash_seeds[3];
3134 static uword hash_bitmap[256 / BITS (uword)];
3135 f64 time_now;
3136 int bogus_length;
3137 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3138
3139 if (node->flags & VLIB_NODE_FLAG_TRACE)
3140 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3141
3142 time_now = vlib_time_now (vm);
3143 if (time_now - time_last_seed_change > 1e-3)
3144 {
3145 uword i;
3146 u32 *r = clib_random_buffer_get_data (&vm->random_buffer,
3147 sizeof (hash_seeds));
3148 for (i = 0; i < ARRAY_LEN (hash_seeds); i++)
3149 hash_seeds[i] = r[i];
3150
3151 /* Mark all hash keys as been not-seen before. */
3152 for (i = 0; i < ARRAY_LEN (hash_bitmap); i++)
3153 hash_bitmap[i] = 0;
3154
3155 time_last_seed_change = time_now;
3156 }
3157
3158 from = vlib_frame_vector_args (frame);
3159 n_left_from = frame->n_vectors;
3160
3161 while (n_left_from > 0)
3162 {
3163 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3164 to_next_drop, n_left_to_next_drop);
3165
3166 while (n_left_from > 0 && n_left_to_next_drop > 0)
3167 {
3168 vlib_buffer_t *p0;
3169 ip6_header_t *ip0;
3170 u32 pi0, adj_index0, a0, b0, c0, m0, sw_if_index0, drop0;
3171 uword bm0;
3172 ip_adjacency_t *adj0;
3173 vnet_hw_interface_t *hw_if0;
3174 ip6_radv_t *radv_info;
3175 u32 next0;
3176
3177 pi0 = from[0];
3178
3179 p0 = vlib_get_buffer (vm, pi0);
3180
3181 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3182
3183 ip0 = vlib_buffer_get_current (p0);
3184
3185 adj0 = adj_get (adj_index0);
3186
3187 if (!is_glean)
3188 {
3189 ip0->dst_address.as_u64[0] =
3190 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3191 ip0->dst_address.as_u64[1] =
3192 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3193 }
3194
3195 a0 = hash_seeds[0];
3196 b0 = hash_seeds[1];
3197 c0 = hash_seeds[2];
3198
3199 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3200 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3201
3202 a0 ^= sw_if_index0;
3203 b0 ^= ip0->dst_address.as_u32[0];
3204 c0 ^= ip0->dst_address.as_u32[1];
3205
3206 hash_v3_mix32 (a0, b0, c0);
3207
3208 b0 ^= ip0->dst_address.as_u32[2];
3209 c0 ^= ip0->dst_address.as_u32[3];
3210
3211 hash_v3_finalize32 (a0, b0, c0);
3212
3213 c0 &= BITS (hash_bitmap) - 1;
3214 c0 = c0 / BITS (uword);
3215 m0 = (uword) 1 << (c0 % BITS (uword));
3216
3217 bm0 = hash_bitmap[c0];
3218 drop0 = (bm0 & m0) != 0;
3219
3220 /* Mark it as seen. */
3221 hash_bitmap[c0] = bm0 | m0;
3222
3223 from += 1;
3224 n_left_from -= 1;
3225 to_next_drop[0] = pi0;
3226 to_next_drop += 1;
3227 n_left_to_next_drop -= 1;
3228
3229 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3230
3231 /* If the interface is link-down, drop the pkt */
3232 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3233 drop0 = 1;
3234
3235 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3236 {
3237 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3238
3239 if (ri != ~0)
3240 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3241 else
3242 drop0 = 1;
3243 }
3244 else
3245 drop0 = 1;
3246
3247 /*
3248 * the adj has been updated to a rewrite but the node the DPO that got
3249 * us here hasn't - yet. no big deal. we'll drop while we wait.
3250 */
3251 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3252 drop0 = 1;
3253
3254 p0->error =
3255 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3256 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3257
3258 if (drop0)
3259 continue;
3260
3261 {
3262 u32 bi0 = 0;
3263 icmp6_neighbor_solicitation_header_t *h0;
3264 vlib_buffer_t *b0;
3265
3266 h0 = vlib_packet_template_get_packet
3267 (vm, &im->discover_neighbor_packet_template, &bi0);
3268
3269 /*
3270 * Build ethernet header.
3271 * Choose source address based on destination lookup
3272 * adjacency.
3273 */
3274 if (!ip6_src_address_for_packet (lm,
3275 sw_if_index0,
3276 &ip0->dst_address,
3277 &h0->ip.src_address))
3278 {
3279 /* There is no address on the interface */
3280 p0->error =
3281 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3282 vlib_buffer_free (vm, &bi0, 1);
3283 continue;
3284 }
3285
3286 /*
3287 * Destination address is a solicited node multicast address.
3288 * We need to fill in
3289 * the low 24 bits with low 24 bits of target's address.
3290 */
3291 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3292 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3293 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3294
3295 h0->neighbor.target_address = ip0->dst_address;
3296
3297 clib_memcpy (h0->link_layer_option.ethernet_address,
3298 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3299
3300 /* $$$$ appears we need this; why is the checksum non-zero? */
3301 h0->neighbor.icmp.checksum = 0;
3302 h0->neighbor.icmp.checksum =
3303 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3304 &bogus_length);
3305
3306 ASSERT (bogus_length == 0);
3307
3308 vlib_buffer_copy_trace_flag (vm, p0, bi0);
3309 b0 = vlib_get_buffer (vm, bi0);
3310 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3311 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3312
3313 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3314 radv_info->mcast_adj_index;
3315
3316 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3317 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3318
3319 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3320 }
3321 }
3322
3323 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3324 n_left_to_next_drop);
3325 }
3326
3327 return frame->n_vectors;
3328}
3329
3330static uword
3331ip6_discover_neighbor (vlib_main_t * vm,
3332 vlib_node_runtime_t * node, vlib_frame_t * frame)
3333{
3334 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3335}
3336
3337static uword
3338ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3339{
3340 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3341}
3342
3343static char *ip6_discover_neighbor_error_strings[] = {
3344 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3345 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3346 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3347 = "no source address for ND solicitation",
3348};
3349
3350/* *INDENT-OFF* */
3351VLIB_REGISTER_NODE (ip6_glean_node) =
3352{
3353 .function = ip6_glean,
3354 .name = "ip6-glean",
3355 .vector_size = sizeof (u32),
3356 .format_trace = format_ip6_forward_next_trace,
3357 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3358 .error_strings = ip6_discover_neighbor_error_strings,
3359 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3360 .next_nodes =
3361 {
3362 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3363 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3364 },
3365};
3366VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3367{
3368 .function = ip6_discover_neighbor,
3369 .name = "ip6-discover-neighbor",
3370 .vector_size = sizeof (u32),
3371 .format_trace = format_ip6_forward_next_trace,
3372 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3373 .error_strings = ip6_discover_neighbor_error_strings,
3374 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3375 .next_nodes =
3376 {
3377 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3378 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3379 },
3380};
3381/* *INDENT-ON* */
3382
Dave Barachd7cb1b52016-12-09 09:52:16 -05003383/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003384int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003385ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3386 u8 suppress, u8 managed, u8 other,
3387 u8 ll_option, u8 send_unicast, u8 cease,
3388 u8 use_lifetime, u32 lifetime,
3389 u32 initial_count, u32 initial_interval,
3390 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003391{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003392 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3393 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003394 u32 ri;
3395
3396 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003397 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3398 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003399 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003400 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003401
Dave Barachd7cb1b52016-12-09 09:52:16 -05003402 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003403 {
3404
Dave Barachd7cb1b52016-12-09 09:52:16 -05003405 ip6_radv_t *radv_info;
3406 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003407
Dave Barachd7cb1b52016-12-09 09:52:16 -05003408 if ((max_interval != 0) && (min_interval == 0))
3409 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003410
Dave Barachd7cb1b52016-12-09 09:52:16 -05003411 max_interval =
3412 (max_interval !=
3413 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3414 radv_info->max_radv_interval;
3415 min_interval =
3416 (min_interval !=
3417 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3418 radv_info->min_radv_interval;
3419 lifetime =
3420 (use_lifetime !=
3421 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3422 radv_info->adv_router_lifetime_in_sec;
3423
3424 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003425 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003426 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003427 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003428
3429 if (lifetime <= max_interval)
3430 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003431 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003432
3433 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003434 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003435 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3436 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003437 }
3438
Dave Barachd7cb1b52016-12-09 09:52:16 -05003439 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3440 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3441 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003442
Dave Barachd7cb1b52016-12-09 09:52:16 -05003443 /*
3444 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3445 if "flag" is clear don't change corresponding value
3446 */
3447 radv_info->send_radv =
3448 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3449 radv_info->adv_managed_flag =
3450 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3451 radv_info->adv_other_flag =
3452 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3453 radv_info->adv_link_layer_address =
3454 (ll_option !=
3455 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3456 radv_info->send_unicast =
3457 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3458 radv_info->cease_radv =
3459 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3460
3461 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003462 radv_info->max_radv_interval = max_interval;
3463 radv_info->adv_router_lifetime_in_sec = lifetime;
3464
Dave Barachd7cb1b52016-12-09 09:52:16 -05003465 radv_info->initial_adverts_count =
3466 (initial_count !=
3467 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3468 radv_info->initial_adverts_count;
3469 radv_info->initial_adverts_interval =
3470 (initial_interval !=
3471 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3472 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003473
3474 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003475 if ((cease != 0) && (is_no))
3476 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003477
Dave Barachd7cb1b52016-12-09 09:52:16 -05003478 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3479 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003480 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003481 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003482 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003483 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003484}
3485
3486int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003487ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3488 ip6_address_t * prefix_addr, u8 prefix_len,
3489 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3490 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3491 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003492{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003493 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003494 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003495
Ed Warnickecb9cada2015-12-08 15:45:58 -07003496 u32 ri;
3497
3498 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003499 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3500 ~0);
3501
Ed Warnickecb9cada2015-12-08 15:45:58 -07003502 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3503
3504 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003505
3506 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003507 {
3508 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003509 ip6_radv_t *radv_info;
3510 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003511
3512 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003513 ip6_radv_prefix_t *prefix;
3514
Ed Warnickecb9cada2015-12-08 15:45:58 -07003515 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003516 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3517
Ed Warnickecb9cada2015-12-08 15:45:58 -07003518 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3519
Dave Barachd7cb1b52016-12-09 09:52:16 -05003520 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003521 {
3522 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003523 if (!prefix)
3524 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3525
3526 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003527 return VNET_API_ERROR_INVALID_VALUE_2;
3528
Dave Barachd7cb1b52016-12-09 09:52:16 -05003529 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003530 /* do specific delete processing here before returning */
3531 /* try to remove from routing table */
3532
Dave Barachd7cb1b52016-12-09 09:52:16 -05003533 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3534 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003535 pool_put (radv_info->adv_prefixes_pool, prefix);
3536
Dave Barachd7cb1b52016-12-09 09:52:16 -05003537 radv_info->initial_adverts_sent =
3538 radv_info->initial_adverts_count - 1;
3539 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003540 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003541 radv_info->last_radv_time = 0;
3542 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003543 }
3544
3545 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003546 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003547 {
3548 /* add */
3549 u32 pi;
3550 pool_get (radv_info->adv_prefixes_pool, prefix);
3551 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003552 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3553 /* old_value */ 0);
3554
3555 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
3556
Ed Warnickecb9cada2015-12-08 15:45:58 -07003557 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003558 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3559
Ed Warnickecb9cada2015-12-08 15:45:58 -07003560 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003561 prefix->adv_on_link_flag = 1; /* L bit set */
3562 prefix->adv_autonomous_flag = 1; /* A bit set */
3563 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003564 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3565 prefix->enabled = 1;
3566 prefix->decrement_lifetime_flag = 1;
3567 prefix->deprecated_prefix_flag = 1;
3568
Dave Barachd7cb1b52016-12-09 09:52:16 -05003569 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003570 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003571 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003572 /* insert prefix into routing table as a connected prefix */
3573 }
3574
Dave Barachd7cb1b52016-12-09 09:52:16 -05003575 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003576 goto restart;
3577 }
3578 else
3579 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003580
3581 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003582 return VNET_API_ERROR_INVALID_VALUE_2;
3583
Dave Barachd7cb1b52016-12-09 09:52:16 -05003584 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003585 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003586 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003587 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003588 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003589 }
3590
Dave Barachd7cb1b52016-12-09 09:52:16 -05003591 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003592 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003593 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003594 prefix->adv_pref_lifetime_in_secs = ~0;
3595 prefix->decrement_lifetime_flag = 0;
3596 }
3597 else
3598 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003599 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3600 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003601 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003602
Ed Warnickecb9cada2015-12-08 15:45:58 -07003603 /* copy remaining */
3604 prefix->enabled = !(no_advertise != 0);
3605 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3606 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3607
Dave Barachd7cb1b52016-12-09 09:52:16 -05003608 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003609 /* restart */
3610 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003611 prefix->valid_lifetime_expires =
3612 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003613 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003614
3615 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3616 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003617 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003618 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003619 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003620 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003621}
3622
3623clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003624ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3625 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003626{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003627 vnet_main_t *vnm = vnet_get_main ();
3628 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3629 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003630 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003631 u8 suppress = 0, managed = 0, other = 0;
3632 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003633 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003634 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3635 0, ra_initial_interval = 0;
3636 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003637
Dave Barachd7cb1b52016-12-09 09:52:16 -05003638 unformat_input_t _line_input, *line_input = &_line_input;
3639 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003640
3641 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003642 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003643 ip6_address_t ip6_addr;
3644 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003645
Ed Warnickecb9cada2015-12-08 15:45:58 -07003646
3647 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003648 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003649 return 0;
3650
3651 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003652 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003653 {
3654
Dave Barachd7cb1b52016-12-09 09:52:16 -05003655 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003656 unformat_vnet_sw_interface, vnm, &sw_if_index))
3657 {
3658 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003659 ethernet_interface_t *eth_if0 = 0;
3660
Ed Warnickecb9cada2015-12-08 15:45:58 -07003661 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003662 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3663 eth_if0 =
3664 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3665
3666 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003667 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003668 error =
3669 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003670 goto done;
3671 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003672
Ed Warnickecb9cada2015-12-08 15:45:58 -07003673 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003674 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3675 sw_if_index, ~0);
3676
Ed Warnickecb9cada2015-12-08 15:45:58 -07003677 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003678
3679 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003680 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003681 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003682 }
3683 else
3684 {
3685 error = clib_error_return (0, "unknown interface %U'",
3686 format_unformat_error, line_input);
3687 goto done;
3688 }
3689 }
3690 else
3691 {
3692 error = clib_error_return (0, "invalid interface name %U'",
3693 format_unformat_error, line_input);
3694 goto done;
3695 }
3696 }
3697
3698 /* get the rest of the command */
3699 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3700 {
3701 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003702 is_no = 1;
3703 else if (unformat (line_input, "prefix %U/%d",
3704 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003705 {
3706 add_radv_info = 0;
3707 break;
3708 }
3709 else if (unformat (line_input, "ra-managed-config-flag"))
3710 {
3711 managed = 1;
3712 break;
3713 }
3714 else if (unformat (line_input, "ra-other-config-flag"))
3715 {
3716 other = 1;
3717 break;
3718 }
Chris Luke33879c92016-06-28 19:54:21 -04003719 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003720 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003721 {
Chris Luke33879c92016-06-28 19:54:21 -04003722 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003723 break;
3724 }
Chris Luke33879c92016-06-28 19:54:21 -04003725 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003726 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003727 {
Chris Luke33879c92016-06-28 19:54:21 -04003728 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003729 break;
3730 }
3731 else if (unformat (line_input, "ra-send-unicast"))
3732 {
3733 send_unicast = 1;
3734 break;
3735 }
3736 else if (unformat (line_input, "ra-lifetime"))
3737 {
3738 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003739 {
3740 error = unformat_parse_error (line_input);
3741 goto done;
3742 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003743 use_lifetime = 1;
3744 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003745 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003746 else if (unformat (line_input, "ra-initial"))
3747 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003748 if (!unformat
3749 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003750 {
3751 error = unformat_parse_error (line_input);
3752 goto done;
3753 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003754 break;
3755 }
3756 else if (unformat (line_input, "ra-interval"))
3757 {
3758 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003759 {
3760 error = unformat_parse_error (line_input);
3761 goto done;
3762 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003763
3764 if (!unformat (line_input, "%d", &ra_min_interval))
3765 ra_min_interval = 0;
3766 break;
3767 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003768 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003769 {
3770 cease = 1;
3771 break;
3772 }
3773 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003774 {
3775 error = unformat_parse_error (line_input);
3776 goto done;
3777 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003778 }
3779
Dave Barachd7cb1b52016-12-09 09:52:16 -05003780 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003781 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003782 ip6_neighbor_ra_config (vm, sw_if_index,
3783 suppress, managed, other,
3784 suppress_ll_option, send_unicast, cease,
3785 use_lifetime, ra_lifetime,
3786 ra_initial_count, ra_initial_interval,
3787 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003788 }
3789 else
3790 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003791 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003792 u32 pref_lifetime_in_secs = 0;
3793 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003794 u8 no_advertise = 0;
3795 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003796 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003797 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003798
3799 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003800 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003801 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003802 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003803 {
3804 use_prefix_default_values = 1;
3805 break;
3806 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003807 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003808 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003809 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003810 pref_lifetime_in_secs = ~0;
3811 break;
3812 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003813 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3814 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003815 break;
3816 else
3817 break;
3818 }
3819
3820
3821 /* get the rest of the command */
3822 while (!use_prefix_default_values &&
3823 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3824 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003825 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003826 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003827 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003828 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003829 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003830 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003831 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003832 no_onlink = 1;
3833 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003834 {
3835 error = unformat_parse_error (line_input);
3836 goto done;
3837 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003838 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003839
3840 ip6_neighbor_ra_prefix (vm, sw_if_index,
3841 &ip6_addr, addr_len,
3842 use_prefix_default_values,
3843 valid_lifetime_in_secs,
3844 pref_lifetime_in_secs,
3845 no_advertise,
3846 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003847 }
3848
Billy McFalla9a20e72017-02-15 11:39:12 -05003849done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003850 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003851
Ed Warnickecb9cada2015-12-08 15:45:58 -07003852 return error;
3853}
3854
Chris Lukebfd32fd2016-06-24 20:38:06 -04003855static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003856ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003857{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003858 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003859 u32 i;
3860
3861 for (i = 0; i < vec_len (addrs); i++)
3862 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003863 ip_interface_address_t *a =
3864 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3865 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003866
3867 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003868 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003869 }
3870}
3871
Ed Warnickecb9cada2015-12-08 15:45:58 -07003872static clib_error_t *
3873show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003874 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003875{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003876 vnet_main_t *vnm = vnet_get_main ();
3877 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3878 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003879 u32 sw_if_index;
3880
3881 sw_if_index = ~0;
3882
Dave Barachd7cb1b52016-12-09 09:52:16 -05003883 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003884 {
3885 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003886
Dave Barachd7cb1b52016-12-09 09:52:16 -05003887 /* look up the radv_t information for this interface */
3888 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3889 sw_if_index, ~0);
3890
3891 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3892
3893 if (ri != ~0)
3894 {
3895 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3896 ip6_radv_t *radv_info;
3897 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3898
3899 vlib_cli_output (vm, "%U is admin %s\n",
3900 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003901 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003902 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3903 "up" : "down"));
3904
Chris Lukebfd32fd2016-06-24 20:38:06 -04003905 u32 ai;
3906 u32 *link_scope = 0, *global_scope = 0;
3907 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003908 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003909
Dave Barachd7cb1b52016-12-09 09:52:16 -05003910 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3911 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003912 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3913
Dave Barachd7cb1b52016-12-09 09:52:16 -05003914 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003915 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003916 a = pool_elt_at_index (lm->if_address_pool, ai);
3917 ip6_address_t *address =
3918 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003919
Chris Lukebfd32fd2016-06-24 20:38:06 -04003920 if (ip6_address_is_link_local_unicast (address))
3921 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003922 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003923 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003924 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003925 vec_add1 (local_scope, ai);
3926 else
3927 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003928
3929 ai = a->next_this_sw_interface;
3930 }
3931
Dave Barachd7cb1b52016-12-09 09:52:16 -05003932 if (vec_len (link_scope))
3933 {
3934 vlib_cli_output (vm, "\tLink-local address(es):\n");
3935 ip6_print_addrs (vm, link_scope);
3936 vec_free (link_scope);
3937 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003938
Dave Barachd7cb1b52016-12-09 09:52:16 -05003939 if (vec_len (local_scope))
3940 {
3941 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3942 ip6_print_addrs (vm, local_scope);
3943 vec_free (local_scope);
3944 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003945
Dave Barachd7cb1b52016-12-09 09:52:16 -05003946 if (vec_len (global_scope))
3947 {
3948 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3949 ip6_print_addrs (vm, global_scope);
3950 vec_free (global_scope);
3951 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003952
Dave Barachd7cb1b52016-12-09 09:52:16 -05003953 if (vec_len (unknown_scope))
3954 {
3955 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3956 ip6_print_addrs (vm, unknown_scope);
3957 vec_free (unknown_scope);
3958 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003959
Neale Ranns53da2212018-02-24 02:11:19 -08003960 vlib_cli_output (vm, "\tLink-local address(es):\n");
3961 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3962 &radv_info->link_local_address);
3963
Ed Warnickecb9cada2015-12-08 15:45:58 -07003964 vlib_cli_output (vm, "\tJoined group address(es):\n");
3965 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003966 /* *INDENT-OFF* */
3967 pool_foreach (m, radv_info->mldp_group_pool,
3968 ({
3969 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3970 &m->mcast_address);
3971 }));
3972 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003973
3974 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003975 ip6_radv_prefix_t *p;
3976 /* *INDENT-OFF* */
3977 pool_foreach (p, radv_info->adv_prefixes_pool,
3978 ({
3979 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3980 format_ip6_address, &p->prefix, p->prefix_len);
3981 }));
3982 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003983
Dave Barachd7cb1b52016-12-09 09:52:16 -05003984 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003985 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3986 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3987 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3988 vlib_cli_output (vm, "\tND DAD is disabled\n");
3989 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3990 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3991 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003992 vlib_cli_output (vm,
3993 "\tND advertised retransmit interval is %d (msec)\n",
3994 radv_info->
3995 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003996
3997 u32 ra_interval = radv_info->max_radv_interval;
3998 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003999 vlib_cli_output (vm,
4000 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004001 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004002 vlib_cli_output (vm,
4003 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004004 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004005 vlib_cli_output (vm,
4006 "\tHosts %s stateless autoconfig for addresses\n",
4007 (radv_info->adv_managed_flag) ? "use" :
4008 " don't use");
4009 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4010 radv_info->n_advertisements_sent);
4011 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4012 radv_info->n_solicitations_rcvd);
4013 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4014 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004015 }
4016 else
4017 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004018 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004019 format_unformat_error, input);
4020
4021 }
4022 }
4023 return error;
4024}
4025
Billy McFall0683c9c2016-10-13 08:27:31 -04004026/*?
4027 * This command is used to display various IPv6 attributes on a given
4028 * interface.
4029 *
4030 * @cliexpar
4031 * Example of how to display IPv6 settings:
4032 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4033 * GigabitEthernet2/0/0 is admin up
4034 * Link-local address(es):
4035 * fe80::ab8/64
4036 * Joined group address(es):
4037 * ff02::1
4038 * ff02::2
4039 * ff02::16
4040 * ff02::1:ff00:ab8
4041 * Advertised Prefixes:
4042 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4043 * MTU is 1500
4044 * ICMP error messages are unlimited
4045 * ICMP redirects are disabled
4046 * ICMP unreachables are not sent
4047 * ND DAD is disabled
4048 * ND advertised reachable time is 0
4049 * ND advertised retransmit interval is 0 (msec)
4050 * ND router advertisements are sent every 200 seconds (min interval is 150)
4051 * ND router advertisements live for 600 seconds
4052 * Hosts use stateless autoconfig for addresses
4053 * ND router advertisements sent 19336
4054 * ND router solicitations received 0
4055 * ND router solicitations dropped 0
4056 * @cliexend
4057 * Example of output if IPv6 is not enabled on the interface:
4058 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4059 * show ip6 interface: IPv6 not enabled on interface
4060 * @cliexend
4061?*/
4062/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004063VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4064{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004065 .path = "show ip6 interface",
4066 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004067 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004068};
Billy McFall0683c9c2016-10-13 08:27:31 -04004069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004070
4071clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004072disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004073{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004074 clib_error_t *error = 0;
4075 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004076 u32 ri;
4077
4078 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004079 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4080 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004081 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004082
Ed Warnickecb9cada2015-12-08 15:45:58 -07004083 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004084 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004085 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004086 ip6_radv_t *radv_info;
4087
4088 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004089
4090 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004091 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004092 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004093 {
4094 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004095 ip6_ll_prefix_t ilp = {
4096 .ilp_addr = radv_info->link_local_address,
4097 .ilp_sw_if_index = sw_if_index,
4098 };
4099 ip6_ll_table_entry_delete (&ilp);
4100 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004101 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004102 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4103 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004104 }
4105 }
4106 return error;
4107}
4108
4109int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004110ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004111{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004112 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4113 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004114
Dave Barachd7cb1b52016-12-09 09:52:16 -05004115 /* look up the radv_t information for this interface */
4116 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4117 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004118
Dave Barachd7cb1b52016-12-09 09:52:16 -05004119 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004120
Dave Barachd7cb1b52016-12-09 09:52:16 -05004121 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004122}
4123
Dave Barachd7cb1b52016-12-09 09:52:16 -05004124clib_error_t *
4125enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004126{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004127 clib_error_t *error = 0;
4128 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004129 u32 ri;
4130 int is_add = 1;
4131
4132 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004133 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4134 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004135
Dave Barachd7cb1b52016-12-09 09:52:16 -05004136 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4137
4138 /* if not created yet */
4139 if (ri == ~0)
4140 {
4141 vnet_main_t *vnm = vnet_get_main ();
4142 vnet_sw_interface_t *sw_if0;
4143
4144 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4145 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4146 {
4147 ethernet_interface_t *eth_if0;
4148
4149 eth_if0 =
4150 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4151 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004152 {
4153 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004154 ri =
4155 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004156
Dave Barachd7cb1b52016-12-09 09:52:16 -05004157 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004158 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004159 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004160 ip6_address_t link_local_address;
4161
Dave Barachd7cb1b52016-12-09 09:52:16 -05004162 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004163
Dave Barachd7cb1b52016-12-09 09:52:16 -05004164 ip6_link_local_address_from_ethernet_mac_address
4165 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004166
4167 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004168 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
4169 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004170 {
4171 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004172 link_local_address.as_u64[1] =
4173 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004174
4175 link_local_address.as_u64[0] =
4176 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004177 /* clear u bit */
4178 link_local_address.as_u8[8] &= 0xfd;
4179 }
Neale Ranns53da2212018-02-24 02:11:19 -08004180 {
4181 ip6_ll_prefix_t ilp = {
4182 .ilp_addr = link_local_address,
4183 .ilp_sw_if_index = sw_if_index,
4184 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004185
Neale Ranns53da2212018-02-24 02:11:19 -08004186 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4187 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004188
Ed Warnickecb9cada2015-12-08 15:45:58 -07004189 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004190 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4191 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004192
Neale Ranns53da2212018-02-24 02:11:19 -08004193 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004194 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004195 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004196 }
4197 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004198 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004199 }
4200 }
4201 return error;
4202}
4203
Neale Ranns53da2212018-02-24 02:11:19 -08004204int
4205ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4206{
4207 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4208 ip6_radv_t *radv_info;
4209 u32 ri;
4210
4211 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) < sw_if_index)
4212 return 0;
4213
4214 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4215
4216 if (ri == ~0)
4217 return 0;
4218
4219 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4220 *addr = radv_info->link_local_address;
4221
4222 return (!0);
4223}
4224
Ed Warnickecb9cada2015-12-08 15:45:58 -07004225static clib_error_t *
4226enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004227 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004228{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004229 vnet_main_t *vnm = vnet_get_main ();
4230 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004231 u32 sw_if_index;
4232
4233 sw_if_index = ~0;
4234
Dave Barachd7cb1b52016-12-09 09:52:16 -05004235 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004236 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004237 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004238 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004239 else
4240 {
4241 error = clib_error_return (0, "unknown interface\n'",
4242 format_unformat_error, input);
4243
4244 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004245 return error;
4246}
4247
Billy McFall0683c9c2016-10-13 08:27:31 -04004248/*?
4249 * This command is used to enable IPv6 on a given interface.
4250 *
4251 * @cliexpar
4252 * Example of how enable IPv6 on a given interface:
4253 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4254?*/
4255/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004256VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4257{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004258 .path = "enable ip6 interface",
4259 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004260 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004261};
Billy McFall0683c9c2016-10-13 08:27:31 -04004262/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004263
4264static clib_error_t *
4265disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004266 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004267{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004268 vnet_main_t *vnm = vnet_get_main ();
4269 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004270 u32 sw_if_index;
4271
4272 sw_if_index = ~0;
4273
Dave Barachd7cb1b52016-12-09 09:52:16 -05004274 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004275 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004276 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004277 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004278 else
4279 {
4280 error = clib_error_return (0, "unknown interface\n'",
4281 format_unformat_error, input);
4282
4283 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004284 return error;
4285}
4286
Billy McFall0683c9c2016-10-13 08:27:31 -04004287/*?
4288 * This command is used to disable IPv6 on a given interface.
4289 *
4290 * @cliexpar
4291 * Example of how disable IPv6 on a given interface:
4292 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4293?*/
4294/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004295VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4296{
Billy McFall0683c9c2016-10-13 08:27:31 -04004297 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004298 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004299 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004300};
Billy McFall0683c9c2016-10-13 08:27:31 -04004301/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004302
Billy McFall0683c9c2016-10-13 08:27:31 -04004303/*?
4304 * This command is used to configure the neighbor discovery
4305 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4306 * command to display some of the current neighbor discovery parameters
4307 * on a given interface. This command has three formats:
4308 *
4309 *
4310 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4311 *
4312 * '<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>'
4313 *
4314 * Where:
4315 *
4316 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4317 * router-advertisement messages to use stateful address
4318 * auto-configuration to obtain address information (sets the M-bit).
4319 * Default is the M-bit is not set and the '<em>no</em>' option
4320 * returns it to this default state.
4321 *
4322 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4323 * router-advertisement messages that hosts use stateful auto
4324 * configuration to obtain nonaddress related information (sets
4325 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4326 * option returns it to this default state.
4327 *
4328 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4329 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4330 * router-advertisement messages.
4331 *
4332 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4333 * optional source link-layer address in the ICMPv6 router-advertisement
4334 * messages. Default is to include the optional source link-layer address
4335 * and the '<em>no</em>' option returns it to this default state.
4336 *
4337 * <em>[no] ra-send-unicast</em> - Use the source address of the
4338 * router-solicitation message if availiable. The default is to use
4339 * multicast address of all nodes, and the '<em>no</em>' option returns
4340 * it to this default state.
4341 *
4342 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4343 * default router in ICMPv6 router-advertisement messages. The range is
4344 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4345 * '<em><max-interval></em>'. The default value is 600 seconds and the
4346 * '<em>no</em>' option returns it to this default value.
4347 *
4348 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4349 * router-advertisement messages sent and the interval between each
4350 * message. Range for count is 1 - 3 and default is 3. Range for interval
4351 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4352 * returns both to their default value.
4353 *
4354 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4355 * interval between sending ICMPv6 router-advertisement messages. The
4356 * range for max-interval is from 4 to 200 seconds. min-interval can not
4357 * be more than 75% of max-interval. If not set, min-interval will be
4358 * set to 75% of max-interval. The range for min-interval is from 3 to
4359 * 150 seconds. The '<em>no</em>' option returns both to their default
4360 * value.
4361 *
4362 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4363 * The '<em>no</em>' options implies to start (or restart) sending
4364 * ICMPv6 router-advertisement messages.
4365 *
4366 *
4367 * <b>Format 2 - Prefix Options:</b>
4368 *
4369 * '<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>'
4370 *
4371 * Where:
4372 *
4373 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4374 *
4375 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4376 * length of time in seconds during what the prefix is valid for the purpose of
4377 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4378 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4379 * length of time in seconds during what addresses generated from the prefix remain
4380 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4381 *
4382 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4383 * are inifinte, no timeout.
4384 *
4385 * <em>no-advertise</em> - Do not send full router address in prefix
4386 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4387 *
4388 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4389 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4390 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4391 *
4392 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4393 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4394 *
4395 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4396 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4397 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4398 * the L-bit.
4399 *
4400 *
4401 * <b>Format 3: - Default of Prefix:</b>
4402 *
4403 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4404 *
4405 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4406 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4407 * is ignored and the prefix is deleted.
4408 *
4409 *
4410 * @cliexpar
4411 * Example of how set a router advertisement option:
4412 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4413 * Example of how to add a prefix:
4414 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4415 * Example of how to delete a prefix:
4416 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4417?*/
4418/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004419VLIB_CLI_COMMAND (ip6_nd_command, static) =
4420{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004421 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004422 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004423 .function = ip6_neighbor_cmd,
4424};
Billy McFall0683c9c2016-10-13 08:27:31 -04004425/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004426
4427clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004428set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08004429 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004430{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004431 clib_error_t *error = 0;
4432 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004433 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004434 ip6_radv_t *radv_info;
4435 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07004436
Dave Barachd7cb1b52016-12-09 09:52:16 -05004437 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004438 {
4439 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004440 return (error = clib_error_return (0, "address not link-local",
4441 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004442 }
4443
4444 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004445 enable_ip6_interface (vm, sw_if_index);
4446
Ed Warnickecb9cada2015-12-08 15:45:58 -07004447 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004448
4449 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004450 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004451 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004452
4453 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004454
Ed Warnickecb9cada2015-12-08 15:45:58 -07004455 /* delete the old one */
4456 error = ip6_add_del_interface_address (vm, sw_if_index,
4457 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08004458 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05004459
4460 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004461 {
4462 /* add the new one */
4463 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08004464 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004465 0 /* is_del */ );
4466
4467 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004468 {
4469 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004470 }
4471 }
4472 }
4473 else
4474 {
4475 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4476 error = clib_error_return (0, "ip6 not enabled for interface",
4477 format_unformat_error);
4478 }
4479 return error;
4480}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004481
Ed Warnickecb9cada2015-12-08 15:45:58 -07004482clib_error_t *
4483set_ip6_link_local_address_cmd (vlib_main_t * vm,
4484 unformat_input_t * input,
4485 vlib_cli_command_t * cmd)
4486{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004487 vnet_main_t *vnm = vnet_get_main ();
4488 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004489 u32 sw_if_index;
4490 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004491
4492 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004493 {
4494 /* get the rest of the command */
4495 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4496 {
Neale Ranns75152282017-01-09 01:00:45 -08004497 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004498 break;
4499 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05004500 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004501 }
4502 }
Neale Ranns75152282017-01-09 01:00:45 -08004503 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004504 return error;
4505}
4506
Billy McFall0683c9c2016-10-13 08:27:31 -04004507/*?
4508 * This command is used to assign an IPv6 Link-local address to an
4509 * interface. This command will enable IPv6 on an interface if it
4510 * is not already enabled. Use the '<em>show ip6 interface</em>' command
4511 * to display the assigned Link-local address.
4512 *
4513 * @cliexpar
4514 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08004515 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04004516?*/
4517/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004518VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
4519{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004520 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08004521 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004522 .function = set_ip6_link_local_address_cmd,
4523};
Billy McFall0683c9c2016-10-13 08:27:31 -04004524/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004525
Neale Ranns87df12d2017-02-18 08:16:41 -08004526/**
4527 * @brief callback when an interface address is added or deleted
4528 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004529static void
4530ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4531 uword opaque,
4532 u32 sw_if_index,
4533 ip6_address_t * address,
4534 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004535 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004536{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004537 vnet_main_t *vnm = vnet_get_main ();
4538 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004539 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004540 vlib_main_t *vm = vnm->vlib_main;
4541 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004542 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004543
4544 /* create solicited node multicast address for this interface adddress */
4545 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004546
Ed Warnickecb9cada2015-12-08 15:45:58 -07004547 a.as_u8[0xd] = address->as_u8[0xd];
4548 a.as_u8[0xe] = address->as_u8[0xe];
4549 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004550
4551 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004552 {
4553 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004554 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004555
4556 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004557 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4558 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004559 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004560 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004561 {
4562 /* get radv_info */
4563 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4564
4565 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004566 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004567 radv_info->ref_count++;
4568
Neale Ranns87df12d2017-02-18 08:16:41 -08004569 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004570 }
4571 }
4572 else
4573 {
4574
4575 /* delete */
4576 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004577 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4578 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004579 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004580
Dave Barachd7cb1b52016-12-09 09:52:16 -05004581 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004582 {
4583 /* get radv_info */
4584 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4585
Neale Ranns87df12d2017-02-18 08:16:41 -08004586 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004587
4588 /* if interface up send MLDP "report" */
4589 radv_info->all_routers_mcast = 0;
4590
4591 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004592 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004593 radv_info->ref_count--;
4594 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004595 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4596 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004597 }
4598}
4599
Dave Barachd7cb1b52016-12-09 09:52:16 -05004600clib_error_t *
4601ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004602{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004603 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004604
4605 nm->limit_neighbor_cache_size = neighbor_limit;
4606 return 0;
4607}
4608
Neale Ranns15002542017-09-10 04:39:11 -07004609static void
4610ip6_neighbor_table_bind (ip6_main_t * im,
4611 uword opaque,
4612 u32 sw_if_index,
4613 u32 new_fib_index, u32 old_fib_index)
4614{
4615 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4616 ip6_neighbor_t *n = NULL;
4617 u32 i, *to_re_add = 0;
4618
4619 /* *INDENT-OFF* */
4620 pool_foreach (n, nm->neighbor_pool,
4621 ({
4622 if (n->key.sw_if_index == sw_if_index)
4623 vec_add1 (to_re_add, n - nm->neighbor_pool);
4624 }));
4625 /* *INDENT-ON* */
4626
4627 for (i = 0; i < vec_len (to_re_add); i++)
4628 {
4629 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4630 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4631 ip6_neighbor_adj_fib_add (n, new_fib_index);
4632 }
4633 vec_free (to_re_add);
4634}
4635
Dave Barachd7cb1b52016-12-09 09:52:16 -05004636static clib_error_t *
4637ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004638{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004639 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4640 ip6_main_t *im = &ip6_main;
4641
Ed Warnickecb9cada2015-12-08 15:45:58 -07004642 mhash_init (&nm->neighbor_index_by_key,
4643 /* value size */ sizeof (uword),
4644 /* key size */ sizeof (ip6_neighbor_key_t));
4645
Dave Barachd7cb1b52016-12-09 09:52:16 -05004646 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4647 ip6_icmp_neighbor_solicitation_node.index);
4648 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4649 ip6_icmp_neighbor_advertisement_node.index);
4650 icmp6_register_type (vm, ICMP6_router_solicitation,
4651 ip6_icmp_router_solicitation_node.index);
4652 icmp6_register_type (vm, ICMP6_router_advertisement,
4653 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004654
4655 /* handler node for ip6 neighbor discovery events and timers */
4656 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4657
4658 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004659 ip6_add_del_interface_address_callback_t cb;
4660 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
4661
Ed Warnickecb9cada2015-12-08 15:45:58 -07004662 /* when an interface address changes... */
4663 cb.function = ip6_neighbor_add_del_interface_address;
4664 cb.function_opaque = 0;
4665 vec_add1 (im->add_del_interface_address_callbacks, cb);
4666
Neale Ranns15002542017-09-10 04:39:11 -07004667 ip6_table_bind_callback_t cbt;
4668 cbt.function = ip6_neighbor_table_bind;
4669 cbt.function_opaque = 0;
4670 vec_add1 (im->table_bind_callbacks, cbt);
4671
Ed Warnickecb9cada2015-12-08 15:45:58 -07004672 mhash_init (&nm->pending_resolutions_by_address,
4673 /* value size */ sizeof (uword),
4674 /* key size */ sizeof (ip6_address_t));
4675
John Lo1edfba92016-08-27 01:11:57 -04004676 mhash_init (&nm->mac_changes_by_address,
4677 /* value size */ sizeof (uword),
4678 /* key size */ sizeof (ip6_address_t));
4679
Ed Warnickecb9cada2015-12-08 15:45:58 -07004680 /* default, configurable */
4681 nm->limit_neighbor_cache_size = 50000;
4682
Eyal Baric125ecc2017-09-20 11:29:17 +03004683 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4684
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004685 nm->ip6_ra_publisher_node = (uword) ~ 0;
4686
Ed Warnickecb9cada2015-12-08 15:45:58 -07004687#if 0
4688 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004689 vec_validate_init_empty
4690 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004691#endif
4692
Ed Warnickecb9cada2015-12-08 15:45:58 -07004693 return 0;
4694}
4695
4696VLIB_INIT_FUNCTION (ip6_neighbor_init);
4697
4698
Dave Barachd7cb1b52016-12-09 09:52:16 -05004699void
4700vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4701 void *address_arg,
4702 uword node_index,
4703 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004704{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004705 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4706 ip6_address_t *address = address_arg;
4707 uword *p;
4708 pending_resolution_t *pr;
4709
Ed Warnickecb9cada2015-12-08 15:45:58 -07004710 pool_get (nm->pending_resolutions, pr);
4711
4712 pr->next_index = ~0;
4713 pr->node_index = node_index;
4714 pr->type_opaque = type_opaque;
4715 pr->data = data;
4716
4717 p = mhash_get (&nm->pending_resolutions_by_address, address);
4718 if (p)
4719 {
4720 /* Insert new resolution at the head of the list */
4721 pr->next_index = p[0];
4722 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4723 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004724
4725 mhash_set (&nm->pending_resolutions_by_address, address,
4726 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004727}
4728
Dave Barachd7cb1b52016-12-09 09:52:16 -05004729int
4730vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4731 void *data_callback,
4732 u32 pid,
4733 void *address_arg,
4734 uword node_index,
4735 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004736{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004737 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4738 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004739
Eyal Barif9f40652017-04-01 03:55:08 +03004740 /* Try to find an existing entry */
4741 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4742 u32 *p = first;
4743 pending_resolution_t *mc;
4744 while (p && *p != ~0)
4745 {
4746 mc = pool_elt_at_index (nm->mac_changes, *p);
4747 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4748 && mc->pid == pid)
4749 break;
4750 p = &mc->next_index;
4751 }
4752
4753 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004754 if (is_add)
4755 {
Eyal Barif9f40652017-04-01 03:55:08 +03004756 if (found)
4757 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4758
John Lo1edfba92016-08-27 01:11:57 -04004759 pool_get (nm->mac_changes, mc);
Eyal Barif9f40652017-04-01 03:55:08 +03004760 *mc = (pending_resolution_t)
4761 {
4762 .next_index = ~0,.node_index = node_index,.type_opaque =
4763 type_opaque,.data = data,.data_callback = data_callback,.pid =
4764 pid,};
John Lo1edfba92016-08-27 01:11:57 -04004765
Eyal Barif9f40652017-04-01 03:55:08 +03004766 /* Insert new resolution at the end of the list */
4767 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004768 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004769 p[0] = new_idx;
4770 else
4771 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004772 }
4773 else
4774 {
Eyal Barif9f40652017-04-01 03:55:08 +03004775 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004776 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004777
Eyal Barif9f40652017-04-01 03:55:08 +03004778 /* Clients may need to clean up pool entries, too */
4779 void (*fp) (u32, u8 *) = data_callback;
4780 if (fp)
4781 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04004782
Eyal Barif9f40652017-04-01 03:55:08 +03004783 /* Remove the entry from the list and delete the entry */
4784 *p = mc->next_index;
4785 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004786
Eyal Barif9f40652017-04-01 03:55:08 +03004787 /* Remove from hash if we deleted the last entry */
4788 if (*p == ~0 && p == first)
4789 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004790 }
Eyal Barif9f40652017-04-01 03:55:08 +03004791 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004792}
4793
Dave Barachd7cb1b52016-12-09 09:52:16 -05004794int
4795vnet_ip6_nd_term (vlib_main_t * vm,
4796 vlib_node_runtime_t * node,
4797 vlib_buffer_t * p0,
4798 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004799 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004800{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004801 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4802 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
John Lo1edfba92016-08-27 01:11:57 -04004803
4804 ndh = ip6_next_header (ip);
4805 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4806 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004807 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004808
4809 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4810 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4811 {
4812 u8 *t0 = vlib_add_trace (vm, node, p0,
4813 sizeof (icmp6_input_trace_t));
4814 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4815 }
4816
4817 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004818 if (PREDICT_FALSE
4819 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4820 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004821 {
Eyal Baric125ecc2017-09-20 11:29:17 +03004822 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004823 }
4824
4825 /* Check if MAC entry exsist for solicited target IP */
4826 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4827 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004828 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004829 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004830 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004831
4832 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004833 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004834 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4835 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004836 return 0; /* source link layer address option not present */
4837
John Lo1edfba92016-08-27 01:11:57 -04004838 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004839 macp =
4840 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004841 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004842 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004843 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004844 vlib_node_runtime_t *error_node =
4845 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004846 ip->dst_address = ip->src_address;
4847 ip->src_address = ndh->target_address;
4848 ip->hop_limit = 255;
4849 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004850 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004851 clib_memcpy (opt->ethernet_address, macp, 6);
4852 ndh->icmp.type = ICMP6_neighbor_advertisement;
4853 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004854 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4855 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004856 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004857 ndh->icmp.checksum =
4858 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4859 clib_memcpy (eth->dst_address, eth->src_address, 6);
4860 clib_memcpy (eth->src_address, macp, 6);
4861 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004862 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004863 return 1;
4864 }
John Lo1edfba92016-08-27 01:11:57 -04004865 }
4866
4867 return 0;
4868
4869}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004870
Neale Ranns3f844d02017-02-18 00:03:54 -08004871int
4872ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4873{
4874 u32 fib_index;
4875
4876 fib_prefix_t pfx = {
4877 .fp_len = 128,
4878 .fp_proto = FIB_PROTOCOL_IP6,
4879 .fp_addr = {
4880 .ip6 = *addr,
4881 },
4882 };
4883 ip46_address_t nh = {
4884 .ip6 = *addr,
4885 };
4886
4887 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4888
4889 if (~0 == fib_index)
4890 return VNET_API_ERROR_NO_SUCH_FIB;
4891
4892 if (is_del)
4893 {
4894 fib_table_entry_path_remove (fib_index,
4895 &pfx,
4896 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004897 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004898 &nh,
4899 sw_if_index,
4900 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4901 /* flush the ND cache of this address if it's there */
4902 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4903 sw_if_index, addr, NULL, 0);
4904 }
4905 else
4906 {
4907 fib_table_entry_path_add (fib_index,
4908 &pfx,
4909 FIB_SOURCE_IP6_ND_PROXY,
4910 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004911 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004912 &nh,
4913 sw_if_index,
4914 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4915 }
4916 return (0);
4917}
4918
4919static clib_error_t *
4920set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4921 unformat_input_t * input, vlib_cli_command_t * cmd)
4922{
4923 vnet_main_t *vnm = vnet_get_main ();
4924 clib_error_t *error = 0;
4925 ip6_address_t addr;
4926 u32 sw_if_index;
4927 u8 is_del = 0;
4928
4929 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4930 {
4931 /* get the rest of the command */
4932 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4933 {
4934 if (unformat (input, "%U", unformat_ip6_address, &addr))
4935 break;
4936 else if (unformat (input, "delete") || unformat (input, "del"))
4937 is_del = 1;
4938 else
4939 return (unformat_parse_error (input));
4940 }
4941 }
4942
4943 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4944
4945 return error;
4946}
4947
4948/* *INDENT-OFF* */
4949VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4950{
4951 .path = "set ip6 nd proxy",
4952 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4953 .function = set_ip6_nd_proxy_cmd,
4954};
4955/* *INDENT-ON* */
4956
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004957void
Neale Ranns3be6b282016-12-20 14:24:01 +00004958ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004959{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004960 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4961 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004962 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004963
4964 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004965 pool_foreach (n, nm->neighbor_pool,
4966 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004967 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004968 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004969 adj_nbr_walk_nh6 (sw_if_index,
4970 &n->key.ip6_address,
4971 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004972 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004973 }));
4974 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08004975
4976 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4977
4978 if (ADJ_INDEX_INVALID != ai)
4979 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004980}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004981
John Lo8b81cb42017-06-26 01:40:20 -04004982void
Steven9f781d82018-06-05 11:09:32 -07004983send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04004984{
4985 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04004986 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004987
Steven9f781d82018-06-05 11:09:32 -07004988 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004989}
4990
4991void
4992send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07004993 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07004994{
4995 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07004996 vnet_main_t *vnm = vnet_get_main ();
4997 u8 *rewrite, rewrite_len;
4998 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4999 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07005000
John Lo8b81cb42017-06-26 01:40:20 -04005001 if (ip6_addr)
5002 {
5003 clib_warning
5004 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
5005 format_ip6_address, ip6_addr, sw_if_index);
5006
5007 /* Form unsolicited neighbor advertisement packet from NS pkt template */
5008 int bogus_length;
5009 u32 bi = 0;
5010 icmp6_neighbor_solicitation_header_t *h =
5011 vlib_packet_template_get_packet (vm,
5012 &i6m->discover_neighbor_packet_template,
5013 &bi);
5014 ip6_set_reserved_multicast_address (&h->ip.dst_address,
5015 IP6_MULTICAST_SCOPE_link_local,
5016 IP6_MULTICAST_GROUP_ID_all_hosts);
5017 h->ip.src_address = ip6_addr[0];
5018 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
5019 h->neighbor.target_address = ip6_addr[0];
5020 h->neighbor.advertisement_flags = clib_host_to_net_u32
5021 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
5022 clib_memcpy (h->link_layer_option.ethernet_address,
5023 hi->hw_address, vec_len (hi->hw_address));
5024 h->neighbor.icmp.checksum =
5025 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
5026 ASSERT (bogus_length == 0);
5027
5028 /* Setup MAC header with IP6 Etype and mcast DMAC */
5029 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07005030 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04005031 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07005032 rewrite =
5033 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
5034 rewrite_len = vec_len (rewrite);
5035 vlib_buffer_advance (b, -rewrite_len);
5036 ethernet_header_t *e = vlib_buffer_get_current (b);
5037 clib_memcpy (e->dst_address, rewrite, rewrite_len);
5038 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04005039
5040 /* Send unsolicited ND advertisement packet out the specified interface */
5041 vnet_buffer (b)->sw_if_index[VLIB_RX] =
5042 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
5043 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
5044 u32 *to_next = vlib_frame_vector_args (f);
5045 to_next[0] = bi;
5046 f->n_vectors = 1;
5047 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5048 }
5049}
5050
Dave Barachd7cb1b52016-12-09 09:52:16 -05005051/*
5052 * fd.io coding-style-patch-verification: ON
5053 *
5054 * Local Variables:
5055 * eval: (c-set-style "gnu")
5056 * End:
5057 */