blob: ded5b868702ce3ef702d3d0bff97a4a649c0e6f5 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * ip/ip6_neighbor.c: IP6 neighbor handling
3 *
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/ip/ip.h>
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010019#include <vnet/ip/ip6_neighbor.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ethernet/ethernet.h>
21#include <vppinfra/mhash.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010022#include <vnet/adj/adj.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000023#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010024#include <vnet/fib/fib_table.h>
25#include <vnet/fib/ip6_fib.h>
Neale Ranns4008ac92017-02-13 23:20:04 -080026#include <vnet/mfib/ip6_mfib.h>
Neale Ranns53da2212018-02-24 02:11:19 -080027#include <vnet/ip/ip6_ll_table.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070028#include <vnet/l2/l2_input.h>
Neale Ranns37029302018-08-10 05:30:06 -070029#include <vlibmemory/api.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Billy McFall0683c9c2016-10-13 08:27:31 -040031/**
32 * @file
33 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
34 *
35 * The files contains the API and CLI code for managing IPv6 neighbor
36 * adjacency tables and neighbor discovery logic.
37 */
38
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010039/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070040#define ETHER_MAC_ADDR_LEN 6
41
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010042/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050043typedef struct
44{
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 /* basic advertised information */
46 ip6_address_t prefix;
47 u8 prefix_len;
48 int adv_on_link_flag;
49 int adv_autonomous_flag;
50 u32 adv_valid_lifetime_in_secs;
51 u32 adv_pref_lifetime_in_secs;
52
53 /* advertised values are computed from these times if decrementing */
54 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050055 f64 pref_lifetime_expires;
56
Ed Warnickecb9cada2015-12-08 15:45:58 -070057 /* local information */
58 int enabled;
59 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050060 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
62#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
63#define DEF_ADV_VALID_LIFETIME 2592000
64#define DEF_ADV_PREF_LIFETIME 604800
65
66 /* extensions are added here, mobile, DNS etc.. */
67} ip6_radv_prefix_t;
68
69
Dave Barachd7cb1b52016-12-09 09:52:16 -050070typedef struct
71{
Ed Warnickecb9cada2015-12-08 15:45:58 -070072 /* group information */
73 u8 type;
74 ip6_address_t mcast_address;
75 u16 num_sources;
76 ip6_address_t *mcast_source_address_pool;
77} ip6_mldp_group_t;
78
79/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050080typedef struct
81{
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050084 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 int adv_managed_flag;
86 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050087 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 u32 adv_neighbor_reachable_time_in_msec;
89 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
90
91 /* mtu option */
92 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050093
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050095 u8 link_layer_address[8];
96 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
98 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050099 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 /* Hash table mapping address to index in interface advertised prefix pool. */
102 mhash_t address_to_prefix_index;
103
104 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500105 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
107 /* Hash table mapping address to index in mldp address pool. */
108 mhash_t address_to_mldp_index;
109
110 /* local information */
111 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500112 int send_radv; /* radv on/off on this interface - set by config */
113 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 int send_unicast;
115 int adv_link_layer_address;
116 int prefix_option;
117 int failed_device_check;
118 int all_routers_mcast;
119 u32 seed;
120 u64 randomizer;
121 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000122 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500123
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 /* timing information */
125#define DEF_MAX_RADV_INTERVAL 200
126#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
127#define DEF_CURR_HOP_LIMIT 64
128#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
129#define MAX_DEF_RTR_LIFETIME 9000
130
Dave Barachd7cb1b52016-12-09 09:52:16 -0500131#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
132#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
133#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
134#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
135#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136
137 f64 max_radv_interval;
138 f64 min_radv_interval;
139 f64 min_delay_between_radv;
140 f64 max_delay_between_radv;
141 f64 max_rtr_default_lifetime;
142
143 f64 last_radv_time;
144 f64 last_multicast_time;
145 f64 next_multicast_time;
146
147
148 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500149 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 u32 initial_adverts_sent;
151
152 /* stats */
153 u32 n_advertisements_sent;
154 u32 n_solicitations_rcvd;
155 u32 n_solicitations_dropped;
156
157 /* Link local address to use (defaults to underlying physical for logical interfaces */
158 ip6_address_t link_local_address;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100159
160 /* router solicitations sending state */
161 u8 keep_sending_rs; /* when true then next fields are valid */
162 icmp6_send_router_solicitation_params_t params;
163 f64 sleep_interval;
164 f64 due_time;
165 u32 n_left;
166 f64 start_time;
167 vlib_buffer_t *buffer;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168} ip6_radv_t;
169
Dave Barachd7cb1b52016-12-09 09:52:16 -0500170typedef struct
171{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 u32 next_index;
173 uword node_index;
174 uword type_opaque;
175 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400176 /* Used for nd event notification only */
Neale Ranns37029302018-08-10 05:30:06 -0700177 ip6_nd_change_event_cb_t data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400178 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179} pending_resolution_t;
180
181
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182typedef struct
183{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500185 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
187 /* lite beer "glean" adjacency handling */
188 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500189 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
John Lo1edfba92016-08-27 01:11:57 -0400191 /* Mac address change notification */
192 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500193 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400194
Dave Barachd7cb1b52016-12-09 09:52:16 -0500195 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barachd7cb1b52016-12-09 09:52:16 -0500197 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
199 mhash_t neighbor_index_by_key;
200
Dave Barachd7cb1b52016-12-09 09:52:16 -0500201 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202
Dave Barachd7cb1b52016-12-09 09:52:16 -0500203 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
205 /* Neighbor attack mitigation */
206 u32 limit_neighbor_cache_size;
207 u32 neighbor_delete_rotor;
208
Eyal Baric125ecc2017-09-20 11:29:17 +0300209 /* Wildcard nd report publisher */
210 uword wc_ip6_nd_publisher_node;
211 uword wc_ip6_nd_publisher_et;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100212
213 /* Router advertisement report publisher */
214 uword ip6_ra_publisher_node;
215 uword ip6_ra_publisher_et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216} ip6_neighbor_main_t;
217
Neale Ranns3f844d02017-02-18 00:03:54 -0800218/* ipv6 neighbor discovery - timer/event types */
219typedef enum
220{
221 ICMP6_ND_EVENT_INIT,
222} ip6_icmp_neighbor_discovery_event_type_t;
223
224typedef union
225{
226 u32 add_del_swindex;
227 struct
228 {
229 u32 up_down_swindex;
230 u32 fib_index;
231 } up_down_event;
232} ip6_icmp_neighbor_discovery_event_data_t;
233
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234static ip6_neighbor_main_t ip6_neighbor_main;
Juraj Sloboda52574522018-05-03 10:03:50 +0200235ip6_neighbor_public_main_t ip6_neighbor_public_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Eyal Baric125ecc2017-09-20 11:29:17 +0300238static void wc_nd_signal_report (wc_nd_report_t * r);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100239static void ra_signal_report (ra_report_t * r);
Eyal Baric125ecc2017-09-20 11:29:17 +0300240
Juraj Sloboda81119e82018-05-25 14:02:20 +0200241ip6_address_t
242ip6_neighbor_get_link_local_address (u32 sw_if_index)
243{
244 static ip6_address_t empty_address = { {0} };
245 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
246 ip6_radv_t *radv_info;
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200247 u32 ri = ~0;
Juraj Sloboda81119e82018-05-25 14:02:20 +0200248
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +0200249 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index)
250 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Juraj Sloboda81119e82018-05-25 14:02:20 +0200251 if (ri == ~0)
252 {
253 clib_warning ("IPv6 is not enabled for sw_if_index %d", sw_if_index);
254 return empty_address;
255 }
256 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
257 if (radv_info == NULL)
258 {
259 clib_warning ("Internal error");
260 return empty_address;
261 }
262 return radv_info->link_local_address;
263}
264
Eyal Baric125ecc2017-09-20 11:29:17 +0300265/**
266 * @brief publish wildcard arp event
267 * @param sw_if_index The interface on which the ARP entires are acted
268 */
269static int
Neale Ranns37029302018-08-10 05:30:06 -0700270vnet_nd_wc_publish (u32 sw_if_index,
271 const mac_address_t * mac, const ip6_address_t * ip6)
Eyal Baric125ecc2017-09-20 11:29:17 +0300272{
273 wc_nd_report_t r = {
274 .sw_if_index = sw_if_index,
275 .ip6 = *ip6,
Neale Ranns37029302018-08-10 05:30:06 -0700276 .mac = *mac,
Eyal Baric125ecc2017-09-20 11:29:17 +0300277 };
Eyal Baric125ecc2017-09-20 11:29:17 +0300278
Eyal Baric125ecc2017-09-20 11:29:17 +0300279 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
280 return 0;
281}
282
283static void
284wc_nd_signal_report (wc_nd_report_t * r)
285{
286 vlib_main_t *vm = vlib_get_main ();
287 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
288 uword ni = nm->wc_ip6_nd_publisher_node;
289 uword et = nm->wc_ip6_nd_publisher_et;
290
291 if (ni == (uword) ~ 0)
292 return;
293 wc_nd_report_t *q =
294 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
295
296 *q = *r;
297}
298
299void
300wc_nd_set_publisher_node (uword node_index, uword event_type)
301{
302 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
303 nm->wc_ip6_nd_publisher_node = node_index;
304 nm->wc_ip6_nd_publisher_et = event_type;
305}
306
Juraj Sloboda4b9669d2018-01-15 10:39:21 +0100307static int
308ra_publish (ra_report_t * r)
309{
310 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
311 vl_api_rpc_call_main_thread (ra_signal_report, (u8 *) r, sizeof *r);
312 return 0;
313}
314
315static void
316ra_signal_report (ra_report_t * r)
317{
318 vlib_main_t *vm = vlib_get_main ();
319 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
320 uword ni = nm->ip6_ra_publisher_node;
321 uword et = nm->ip6_ra_publisher_et;
322
323 if (ni == (uword) ~ 0)
324 return;
325 ra_report_t *q = vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
326
327 *q = *r;
328}
329
330void
331ra_set_publisher_node (uword node_index, uword event_type)
332{
333 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
334 nm->ip6_ra_publisher_node = node_index;
335 nm->ip6_ra_publisher_et = event_type;
336}
337
Dave Barachd7cb1b52016-12-09 09:52:16 -0500338static u8 *
339format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500341 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
342 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
343 vnet_main_t *vnm = vnet_get_main ();
344 vnet_sw_interface_t *si;
345 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346
Dave Barachd7cb1b52016-12-09 09:52:16 -0500347 if (!n)
shubing guo30746292018-08-10 15:51:44 +0800348 return format (s, "%=12s%=45s%=6s%=20s%=40s", "Time", "Address", "Flags",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500349 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100350
Neale Ranns37029302018-08-10 05:30:06 -0700351 if (n->flags & IP_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500352 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100353
Neale Ranns37029302018-08-10 05:30:06 -0700354 if (n->flags & IP_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500355 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Neale Ranns37029302018-08-10 05:30:06 -0700357 if (n->flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY)
Neale Rannsb3b2de72017-03-08 05:17:22 -0800358 flags = format (flags, "N");
359
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
shubing guo30746292018-08-10 15:51:44 +0800361 s = format (s, "%=12U%=45U%=6s%=20U%=40U",
John Lo7f358b32018-04-28 01:19:24 -0400362 format_vlib_time, vm, n->time_last_updated,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500364 flags ? (char *) flags : "",
Neale Ranns37029302018-08-10 05:30:06 -0700365 format_mac_address_t, &n->mac,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 format_vnet_sw_interface_name, vnm, si);
367
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369 return s;
370}
371
Neale Ranns15002542017-09-10 04:39:11 -0700372static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700373ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700374{
375 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
376 {
Neale Ranns53da2212018-02-24 02:11:19 -0800377 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
378 {
379 ip6_ll_prefix_t pfx = {
380 .ilp_addr = n->key.ip6_address,
381 .ilp_sw_if_index = n->key.sw_if_index,
382 };
383 ip6_ll_table_entry_delete (&pfx);
384 }
385 else
386 {
387 fib_prefix_t pfx = {
388 .fp_len = 128,
389 .fp_proto = FIB_PROTOCOL_IP6,
390 .fp_addr.ip6 = n->key.ip6_address,
391 };
392 fib_table_entry_path_remove (fib_index,
393 &pfx,
394 FIB_SOURCE_ADJ,
395 DPO_PROTO_IP6,
396 &pfx.fp_addr,
397 n->key.sw_if_index, ~0,
398 1, FIB_ROUTE_PATH_FLAG_NONE);
399 }
Neale Ranns15002542017-09-10 04:39:11 -0700400 }
401}
402
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403typedef struct
404{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 u8 is_add;
Neale Ranns37029302018-08-10 05:30:06 -0700406 ip_neighbor_flags_t flags;
407 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 u32 sw_if_index;
409 ip6_address_t addr;
410} ip6_neighbor_set_unset_rpc_args_t;
411
Dave Barachd7cb1b52016-12-09 09:52:16 -0500412static void ip6_neighbor_set_unset_rpc_callback
413 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414
Dave Barachd7cb1b52016-12-09 09:52:16 -0500415static void set_unset_ip6_neighbor_rpc
416 (vlib_main_t * vm,
417 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700418 const ip6_address_t * a,
Neale Ranns37029302018-08-10 05:30:06 -0700419 const mac_address_t * mac, int is_add, ip_neighbor_flags_t flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420{
421 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500422 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500423
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 args.sw_if_index = sw_if_index;
425 args.is_add = is_add;
Neale Ranns37029302018-08-10 05:30:06 -0700426 args.flags = flags;
427 ip6_address_copy (&args.addr, a);
428 mac_address_copy (&args.mac, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500429
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500431 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100434static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500435ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100436{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500437 icmp6_neighbor_solicitation_header_t *h;
438 vnet_main_t *vnm = vnet_get_main ();
439 ip6_main_t *im = &ip6_main;
440 ip_interface_address_t *ia;
441 ip6_address_t *dst, *src;
442 vnet_hw_interface_t *hi;
443 vnet_sw_interface_t *si;
444 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100445 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500446 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100447 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100448
Dave Barachd7cb1b52016-12-09 09:52:16 -0500449 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100450
Dave Barachd7cb1b52016-12-09 09:52:16 -0500451 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100452 dst = &adj->sub_type.nbr.next_hop.ip6;
453
454 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100455 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100456 return;
457 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500458 src = ip6_interface_address_matching_destination (im, dst,
459 adj->rewrite_header.
460 sw_if_index, &ia);
461 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100462 {
463 return;
464 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100465
Dave Barachd7cb1b52016-12-09 09:52:16 -0500466 h = vlib_packet_template_get_packet (vm,
467 &im->discover_neighbor_packet_template,
468 &bi);
John Lo084606b2018-06-19 15:27:48 -0400469 if (!h)
470 return;
Neale Rannsb80c5362016-10-08 13:03:40 +0100471
Dave Barachd7cb1b52016-12-09 09:52:16 -0500472 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100473
474 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
475 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
476 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
477 h->ip.src_address = src[0];
478 h->neighbor.target_address = dst[0];
479
480 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500481 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100482
483 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500484 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
485 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100486
487 b = vlib_get_buffer (vm, bi);
488 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500489 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100490
491 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500492 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
493 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100494
495 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500496 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
497 u32 *to_next = vlib_frame_vector_args (f);
498 to_next[0] = bi;
499 f->n_vectors = 1;
500 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100501 }
502}
503
504static void
505ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
506{
507 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
508 ethernet_build_rewrite (vnet_get_main (),
509 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500510 adj_get_link_type (ai),
Neale Ranns37029302018-08-10 05:30:06 -0700511 nbr->mac.bytes));
Neale Rannsb80c5362016-10-08 13:03:40 +0100512}
513
514static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000515ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100516{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500517 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000518
Dave Barachd7cb1b52016-12-09 09:52:16 -0500519 adj_nbr_update_rewrite (ai,
520 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
521 ethernet_build_rewrite (vnet_get_main (),
522 adj->rewrite_header.
523 sw_if_index,
524 adj_get_link_type (ai),
525 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100526}
527
528#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
529{ \
530 k.sw_if_index = sw_if_index; \
531 k.ip6_address = *addr; \
532 k.pad = 0; \
533}
534
535static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500536ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100537{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500538 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
539 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100540 ip6_neighbor_key_t k;
541 uword *p;
542
Dave Barachd7cb1b52016-12-09 09:52:16 -0500543 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100544
545 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500546 if (p)
547 {
548 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
549 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100550
551 return (n);
552}
553
554static adj_walk_rc_t
555ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
556{
557 ip6_neighbor_t *nbr = ctx;
558
559 ip6_nd_mk_complete (ai, nbr);
560
561 return (ADJ_WALK_RC_CONTINUE);
562}
563
564static adj_walk_rc_t
565ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
566{
Neale Ranns19c68d22016-12-07 15:38:14 +0000567 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100568
569 return (ADJ_WALK_RC_CONTINUE);
570}
571
John Lo4fed5b12018-05-22 03:35:06 -0400572static clib_error_t *
573ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
574 u32 sw_if_index, u32 flags)
575{
576 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
577 ip6_neighbor_t *n;
578 u32 i, *to_delete = 0;
579
580 /* *INDENT-OFF* */
581 pool_foreach (n, nm->neighbor_pool,
582 ({
583 if (n->key.sw_if_index == sw_if_index)
584 vec_add1 (to_delete, n - nm->neighbor_pool);
585 }));
586 /* *INDENT-ON* */
587
588 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
589 {
590 for (i = 0; i < vec_len (to_delete); i++)
591 {
592 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
593 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
594 ip6_nd_mk_complete_walk, n);
595 }
596 }
597 else
598 {
599 for (i = 0; i < vec_len (to_delete); i++)
600 {
601 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
602 adj_nbr_walk_nh6 (n->key.sw_if_index, &n->key.ip6_address,
603 ip6_nd_mk_incomplete_walk, NULL);
Neale Ranns37029302018-08-10 05:30:06 -0700604 if (n->flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo4fed5b12018-05-22 03:35:06 -0400605 continue;
606 ip6_neighbor_adj_fib_remove (n,
607 ip6_fib_table_get_index_for_sw_if_index
608 (n->key.sw_if_index));
609 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
610 pool_put (nm->neighbor_pool, n);
611 }
612 }
613
614 vec_free (to_delete);
615 return 0;
616}
617
618VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
619
Neale Rannsb80c5362016-10-08 13:03:40 +0100620void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500621ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100622{
623 ip6_neighbor_t *nbr;
624 ip_adjacency_t *adj;
625
626 adj = adj_get (ai);
627
628 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
629
Neale Ranns32e1c012016-11-22 17:07:28 +0000630 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100631 {
Ole Trøan438f6302018-02-15 21:56:06 +0000632 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800633 adj_glean_update_rewrite (ai);
634 break;
635 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000636 if (NULL != nbr)
637 {
638 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
639 ip6_nd_mk_complete_walk, nbr);
640 }
641 else
642 {
643 /*
644 * no matching ND entry.
645 * construct the rewrite required to for an ND packet, and stick
646 * that in the adj's pipe to smoke.
647 */
648 adj_nbr_update_rewrite (ai,
649 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
650 ethernet_build_rewrite (vnm,
651 sw_if_index,
652 VNET_LINK_IP6,
653 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
654
655 /*
656 * since the FIB has added this adj for a route, it makes sense it may
657 * want to forward traffic sometime soon. Let's send a speculative ND.
658 * just one. If we were to do periodically that wouldn't be bad either,
659 * but that's more code than i'm prepared to write at this time for
660 * relatively little reward.
661 */
662 ip6_nbr_probe (adj);
663 }
664 break;
Neale Ranns1855b8e2018-07-11 10:31:26 -0700665 case IP_LOOKUP_NEXT_BCAST:
666 adj_nbr_update_rewrite (ai,
667 ADJ_NBR_REWRITE_FLAG_COMPLETE,
668 ethernet_build_rewrite (vnm,
669 sw_if_index,
670 VNET_LINK_IP6,
671 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
672 break;
Neale Ranns32e1c012016-11-22 17:07:28 +0000673 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700674 {
675 /*
676 * Construct a partial rewrite from the known ethernet mcast dest MAC
677 */
678 u8 *rewrite;
679 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100680
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700681 rewrite = ethernet_build_rewrite (vnm,
682 sw_if_index,
683 adj->ia_link,
684 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000685
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700686 /*
687 * Complete the remaining fields of the adj's rewrite to direct the
688 * complete of the rewrite at switch time by copying in the IP
689 * dst address's bytes.
Neale Ranns889fe942017-06-01 05:43:19 -0400690 * Ofset is 2 bytes into the desintation address.
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700691 */
692 offset = vec_len (rewrite) - 2;
Neale Ranns889fe942017-06-01 05:43:19 -0400693 adj_mcast_update_rewrite (ai, rewrite, offset);
Neale Ranns32e1c012016-11-22 17:07:28 +0000694
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700695 break;
696 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000697 case IP_LOOKUP_NEXT_DROP:
698 case IP_LOOKUP_NEXT_PUNT:
699 case IP_LOOKUP_NEXT_LOCAL:
700 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800701 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000702 case IP_LOOKUP_NEXT_MIDCHAIN:
703 case IP_LOOKUP_NEXT_ICMP_ERROR:
704 case IP_LOOKUP_N_NEXT:
705 ASSERT (0);
706 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100707 }
708}
709
Neale Ranns15002542017-09-10 04:39:11 -0700710
711static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700712ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700713{
Neale Ranns53da2212018-02-24 02:11:19 -0800714 if (ip6_address_is_link_local_unicast (&n->key.ip6_address))
715 {
716 ip6_ll_prefix_t pfx = {
717 .ilp_addr = n->key.ip6_address,
718 .ilp_sw_if_index = n->key.sw_if_index,
719 };
720 n->fib_entry_index =
721 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE);
722 }
723 else
724 {
725 fib_prefix_t pfx = {
726 .fp_len = 128,
727 .fp_proto = FIB_PROTOCOL_IP6,
728 .fp_addr.ip6 = n->key.ip6_address,
729 };
Neale Ranns15002542017-09-10 04:39:11 -0700730
Neale Ranns53da2212018-02-24 02:11:19 -0800731 n->fib_entry_index =
732 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
733 FIB_ENTRY_FLAG_ATTACHED,
734 DPO_PROTO_IP6, &pfx.fp_addr,
735 n->key.sw_if_index, ~0, 1, NULL,
736 FIB_ROUTE_PATH_FLAG_NONE);
737 }
Neale Ranns15002542017-09-10 04:39:11 -0700738}
739
John Lo4fed5b12018-05-22 03:35:06 -0400740static ip6_neighbor_t *
741force_reuse_neighbor_entry (void)
742{
743 ip6_neighbor_t *n;
744 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
745 u32 count = 0;
746 u32 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
747 if (index == ~0) /* Try again from elt 0 */
748 index = pool_next_index (nm->neighbor_pool, index);
749
750 /* Find a non-static random entry to free up for reuse */
751 do
752 {
753 if ((count++ == 100) || (index == ~0))
754 return NULL; /* give up after 100 entries */
755 n = pool_elt_at_index (nm->neighbor_pool, index);
756 nm->neighbor_delete_rotor = index;
757 index = pool_next_index (nm->neighbor_pool, index);
758 }
Neale Ranns37029302018-08-10 05:30:06 -0700759 while (n->flags & IP_NEIGHBOR_FLAG_STATIC);
John Lo4fed5b12018-05-22 03:35:06 -0400760
761 /* Remove ARP entry from its interface and update fib */
762 adj_nbr_walk_nh6 (n->key.sw_if_index,
763 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
764 ip6_neighbor_adj_fib_remove
765 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
766 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
767
768 return n;
769}
770
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771int
772vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500773 u32 sw_if_index,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700774 const ip6_address_t * a,
Neale Ranns37029302018-08-10 05:30:06 -0700775 const mac_address_t * mac,
776 ip_neighbor_flags_t flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500778 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500780 ip6_neighbor_t *n = 0;
781 int make_new_nd_cache_entry = 1;
782 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500784 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785
Damjan Marion586afd72017-04-05 19:18:20 +0200786 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 {
Neale Ranns37029302018-08-10 05:30:06 -0700788 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, mac, 1, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 return 0;
790 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791
792 k.sw_if_index = sw_if_index;
793 k.ip6_address = a[0];
794 k.pad = 0;
795
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500797 if (p)
798 {
799 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
800 /* Refuse to over-write static neighbor entry. */
Neale Ranns37029302018-08-10 05:30:06 -0700801 if (!(flags & IP_NEIGHBOR_FLAG_STATIC) &&
802 (n->flags & IP_NEIGHBOR_FLAG_STATIC))
John Lo0e6f4d62018-07-13 17:29:58 -0400803 {
804 /* if MAC address match, still check to send event */
Neale Ranns37029302018-08-10 05:30:06 -0700805 if (0 == mac_address_cmp (&n->mac, mac))
John Lo0e6f4d62018-07-13 17:29:58 -0400806 goto check_customers;
807 return -2;
808 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500809 make_new_nd_cache_entry = 0;
810 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100811
Dave Barachd7cb1b52016-12-09 09:52:16 -0500812 if (make_new_nd_cache_entry)
813 {
John Lo4fed5b12018-05-22 03:35:06 -0400814 if (nm->limit_neighbor_cache_size &&
815 pool_elts (nm->neighbor_pool) >= nm->limit_neighbor_cache_size)
816 {
817 n = force_reuse_neighbor_entry ();
818 if (NULL == n)
819 return -2;
820 }
821 else
822 pool_get (nm->neighbor_pool, n);
823
Dave Barachd7cb1b52016-12-09 09:52:16 -0500824 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
825 /* old value */ 0);
826 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700827 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100828
Neale Ranns37029302018-08-10 05:30:06 -0700829 mac_address_copy (&n->mac, mac);
Neale Rannsb80c5362016-10-08 13:03:40 +0100830
Dave Barachd7cb1b52016-12-09 09:52:16 -0500831 /*
832 * create the adj-fib. the entry in the FIB table for and to the peer.
833 */
Neale Ranns37029302018-08-10 05:30:06 -0700834 if (!(flags & IP_NEIGHBOR_FLAG_NO_FIB_ENTRY))
Neale Rannsb3b2de72017-03-08 05:17:22 -0800835 {
Neale Ranns53da2212018-02-24 02:11:19 -0800836 ip6_neighbor_adj_fib_add
837 (n, ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700838 }
839 else
840 {
Neale Ranns37029302018-08-10 05:30:06 -0700841 n->flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800842 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500843 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100844 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500845 {
846 /*
847 * prevent a DoS attack from the data-plane that
848 * spams us with no-op updates to the MAC address
849 */
Neale Ranns37029302018-08-10 05:30:06 -0700850 if (0 == mac_address_cmp (&n->mac, mac))
John Lo7f358b32018-04-28 01:19:24 -0400851 {
852 n->time_last_updated = vlib_time_now (vm);
853 goto check_customers;
854 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100855
Neale Ranns37029302018-08-10 05:30:06 -0700856 mac_address_copy (&n->mac, mac);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500857 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858
Neale Rannsb80c5362016-10-08 13:03:40 +0100859 /* Update time stamp and flags. */
John Lo7f358b32018-04-28 01:19:24 -0400860 n->time_last_updated = vlib_time_now (vm);
Neale Ranns37029302018-08-10 05:30:06 -0700861 if (flags & IP_NEIGHBOR_FLAG_STATIC)
John Lo4fed5b12018-05-22 03:35:06 -0400862 {
Neale Ranns37029302018-08-10 05:30:06 -0700863 n->flags |= IP_NEIGHBOR_FLAG_STATIC;
864 n->flags &= ~IP_NEIGHBOR_FLAG_DYNAMIC;
John Lo4fed5b12018-05-22 03:35:06 -0400865 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100866 else
John Lo4fed5b12018-05-22 03:35:06 -0400867 {
Neale Ranns37029302018-08-10 05:30:06 -0700868 n->flags |= IP_NEIGHBOR_FLAG_DYNAMIC;
869 n->flags &= ~IP_NEIGHBOR_FLAG_STATIC;
John Lo4fed5b12018-05-22 03:35:06 -0400870 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100871
Neale Rannsb80c5362016-10-08 13:03:40 +0100872 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500873 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874
Dave Barach59b25652017-09-10 15:04:27 -0400875check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876 /* Customer(s) waiting for this address to be resolved? */
877 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400878 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 {
John Lo1edfba92016-08-27 01:11:57 -0400880 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881
882 while (next_index != (u32) ~ 0)
883 {
John Lo1edfba92016-08-27 01:11:57 -0400884 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
885 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500886 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400887 next_index = pr->next_index;
888 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500889 }
John Lo1edfba92016-08-27 01:11:57 -0400890
Neale Ranns0bdd3192018-09-07 11:04:52 -0700891 mhash_unset (&nm->pending_resolutions_by_address, (void *) a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892 }
893
John Lo1edfba92016-08-27 01:11:57 -0400894 /* Customer(s) requesting ND event for this address? */
895 p = mhash_get (&nm->mac_changes_by_address, a);
896 if (p)
897 {
898 next_index = p[0];
899
Dave Barachd7cb1b52016-12-09 09:52:16 -0500900 while (next_index != (u32) ~ 0)
901 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500902 int rv = 1;
Neale Ranns37029302018-08-10 05:30:06 -0700903
Dave Barachd7cb1b52016-12-09 09:52:16 -0500904 mc = pool_elt_at_index (nm->mac_changes, next_index);
John Lo1edfba92016-08-27 01:11:57 -0400905
Dave Barachd7cb1b52016-12-09 09:52:16 -0500906 /* Call the user's data callback, return 1 to suppress dup events */
Neale Ranns37029302018-08-10 05:30:06 -0700907 if (mc->data_callback)
908 rv = (mc->data_callback) (mc->data, mac, sw_if_index, &ip6a_zero);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500909 /*
910 * Signal the resolver process, as long as the user
911 * says they want to be notified
912 */
913 if (rv == 0)
914 vlib_process_signal_event (vm, mc->node_index,
915 mc->type_opaque, mc->data);
916 next_index = mc->next_index;
917 }
John Lo1edfba92016-08-27 01:11:57 -0400918 }
919
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920 return 0;
921}
922
923int
924vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Neale Ranns0bdd3192018-09-07 11:04:52 -0700925 u32 sw_if_index, const ip6_address_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500927 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500929 ip6_neighbor_t *n;
930 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 int rv = 0;
932
Damjan Marion586afd72017-04-05 19:18:20 +0200933 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934 {
Neale Ranns37029302018-08-10 05:30:06 -0700935 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, NULL, 0,
936 IP_NEIGHBOR_FLAG_NONE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 return 0;
938 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939
940 k.sw_if_index = sw_if_index;
941 k.ip6_address = a[0];
942 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500943
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944 p = mhash_get (&nm->neighbor_index_by_key, &k);
945 if (p == 0)
946 {
947 rv = -1;
948 goto out;
949 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500950
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100952
Neale Rannsb80c5362016-10-08 13:03:40 +0100953 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500954 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
John Lo4fed5b12018-05-22 03:35:06 -0400955 ip6_neighbor_adj_fib_remove
956 (n, ip6_fib_table_get_index_for_sw_if_index (sw_if_index));
Neale Rannsb80c5362016-10-08 13:03:40 +0100957
John Lo4fed5b12018-05-22 03:35:06 -0400958 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500960
961out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962 return rv;
963}
964
Dave Barachd7cb1b52016-12-09 09:52:16 -0500965static void ip6_neighbor_set_unset_rpc_callback
966 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500968 vlib_main_t *vm = vlib_get_main ();
969 if (a->is_add)
970 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Ranns37029302018-08-10 05:30:06 -0700971 &a->mac, a->flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 else
Neale Ranns0bdd3192018-09-07 11:04:52 -0700973 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
976static int
977ip6_neighbor_sort (void *a1, void *a2)
978{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500979 vnet_main_t *vnm = vnet_get_main ();
980 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500982 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
983 n2->key.sw_if_index);
984 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
986 return cmp;
987}
988
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100989ip6_neighbor_t *
John Lo7f358b32018-04-28 01:19:24 -0400990ip6_neighbors_pool (void)
991{
992 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
993 return nm->neighbor_pool;
994}
995
996ip6_neighbor_t *
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100997ip6_neighbors_entries (u32 sw_if_index)
998{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500999 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Neale Ranns37029302018-08-10 05:30:06 -07001000 ip6_neighbor_t *n, *ns = NULL;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001001
1002 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001003 pool_foreach (n, nm->neighbor_pool,
1004 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001005 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
1006 continue;
1007 vec_add1 (ns, n[0]);
1008 }));
1009 /* *INDENT-ON* */
1010
1011 if (ns)
1012 vec_sort_with_function (ns, ip6_neighbor_sort);
1013 return ns;
1014}
1015
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016static clib_error_t *
1017show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001018 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001020 vnet_main_t *vnm = vnet_get_main ();
1021 ip6_neighbor_t *n, *ns;
1022 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023 u32 sw_if_index;
1024
1025 /* Filter entries by interface if given. */
1026 sw_if_index = ~0;
1027 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
1028
Pavel Kotucek3e046ea2016-12-05 08:27:37 +01001029 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -04001030 if (ns)
1031 {
Billy McFall46529cd2016-10-14 10:45:49 -04001032 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001033 vec_foreach (n, ns)
1034 {
1035 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -04001036 }
1037 vec_free (ns);
1038 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039
1040 return error;
1041}
1042
Billy McFall0683c9c2016-10-13 08:27:31 -04001043/*?
1044 * This command is used to display the adjacent IPv6 hosts found via
1045 * neighbor discovery. Optionally, limit the output to the specified
1046 * interface.
1047 *
1048 * @cliexpar
1049 * Example of how to display the IPv6 neighbor adjacency table:
1050 * @cliexstart{show ip6 neighbors}
1051 * Time Address Flags Link layer Interface
1052 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1053 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1054 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
1055 * @cliexend
1056 * Example of how to display the IPv6 neighbor adjacency table for given interface:
1057 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
1058 * Time Address Flags Link layer Interface
1059 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
1060 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
1061 * @cliexend
1062?*/
1063/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
1065 .path = "show ip6 neighbors",
1066 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -04001067 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068};
Billy McFall0683c9c2016-10-13 08:27:31 -04001069/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070
1071static clib_error_t *
1072set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001073 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074{
Neale Ranns37029302018-08-10 05:30:06 -07001075 ip_neighbor_flags_t flags = IP_NEIGHBOR_FLAG_NONE;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001076 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077 ip6_address_t addr;
Neale Ranns37029302018-08-10 05:30:06 -07001078 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079 int addr_valid = 0;
1080 int is_del = 0;
1081 u32 sw_if_index;
1082
Dave Barachd7cb1b52016-12-09 09:52:16 -05001083 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 {
1085 /* intfc, ip6-address, mac-address */
1086 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -05001087 unformat_vnet_sw_interface, vnm, &sw_if_index,
1088 unformat_ip6_address, &addr,
Neale Ranns37029302018-08-10 05:30:06 -07001089 unformat_mac_address_t, &mac))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001090 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091
1092 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001093 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +01001094 else if (unformat (input, "static"))
Neale Ranns37029302018-08-10 05:30:06 -07001095 flags |= IP_NEIGHBOR_FLAG_STATIC;
Neale Rannsb3b2de72017-03-08 05:17:22 -08001096 else if (unformat (input, "no-fib-entry"))
Neale Ranns37029302018-08-10 05:30:06 -07001097 flags |= IP_NEIGHBOR_FLAG_NO_FIB_ENTRY;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001099 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100 }
1101
1102 if (!addr_valid)
1103 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -05001104
Ed Warnickecb9cada2015-12-08 15:45:58 -07001105 if (!is_del)
Neale Ranns37029302018-08-10 05:30:06 -07001106 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr, &mac, flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 else
Neale Ranns0bdd3192018-09-07 11:04:52 -07001108 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 return 0;
1110}
1111
Billy McFall0683c9c2016-10-13 08:27:31 -04001112/*?
1113 * This command is used to manually add an entry to the IPv6 neighbor
1114 * adjacency table. Optionally, the entry can be added as static. It is
1115 * also used to remove an entry from the table. Use the '<em>show ip6
1116 * neighbors</em>' command to display all learned and manually entered entries.
1117 *
1118 * @cliexpar
1119 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1120 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1121 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1122 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1123?*/
1124/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001125VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1126{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 .path = "set ip6 neighbor",
1128 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001129 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130};
Billy McFall0683c9c2016-10-13 08:27:31 -04001131/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132
Dave Barachd7cb1b52016-12-09 09:52:16 -05001133typedef enum
1134{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1136 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1137 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1138} icmp6_neighbor_solicitation_or_advertisement_next_t;
1139
1140static_always_inline uword
1141icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1142 vlib_node_runtime_t * node,
1143 vlib_frame_t * frame,
1144 uword is_solicitation)
1145{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001146 vnet_main_t *vnm = vnet_get_main ();
1147 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001149 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1151 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001152 vlib_node_runtime_t *error_node =
1153 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154 int bogus_length;
1155
1156 from = vlib_frame_vector_args (frame);
1157 n_left_from = n_packets;
1158 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001159
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 if (node->flags & VLIB_NODE_FLAG_TRACE)
1161 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1162 /* stride */ 1,
1163 sizeof (icmp6_input_trace_t));
1164
Dave Barachd7cb1b52016-12-09 09:52:16 -05001165 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 (is_solicitation
1167 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1168 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1169 n_advertisements_sent = 0;
1170
1171 while (n_left_from > 0)
1172 {
1173 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1174
1175 while (n_left_from > 0 && n_left_to_next > 0)
1176 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001177 vlib_buffer_t *p0;
1178 ip6_header_t *ip0;
1179 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1180 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001182 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1183 int is_rewrite0;
1184 u32 ni0;
1185
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186 bi0 = to_next[0] = from[0];
1187
1188 from += 1;
1189 to_next += 1;
1190 n_left_from -= 1;
1191 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001192
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193 p0 = vlib_get_buffer (vm, bi0);
1194 ip0 = vlib_buffer_get_current (p0);
1195 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001196 options_len0 =
1197 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198
1199 error0 = ICMP6_ERROR_NONE;
1200 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001201 ip6_sadd_link_local =
1202 ip6_address_is_link_local_unicast (&ip0->src_address);
1203 ip6_sadd_unspecified =
1204 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205
1206 /* Check that source address is unspecified, link-local or else on-link. */
1207 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1208 {
1209 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210
Dave Barachd7cb1b52016-12-09 09:52:16 -05001211 if (ADJ_INDEX_INVALID != src_adj_index0)
1212 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001213 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
Dave Barachd7cb1b52016-12-09 09:52:16 -05001215 /* Allow all realistic-looking rewrite adjacencies to pass */
1216 ni0 = adj0->lookup_next_index;
1217 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1218 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001219
Dave Barachd7cb1b52016-12-09 09:52:16 -05001220 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1221 || !is_rewrite0)
1222 ?
1223 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1224 : error0);
1225 }
1226 else
1227 {
1228 error0 =
1229 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1230 }
1231 }
1232
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233 o0 = (void *) (h0 + 1);
1234 o0 = ((options_len0 == 8 && o0->header.type == option_type
1235 && o0->header.n_data_u64s == 1) ? o0 : 0);
1236
1237 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001238 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001239 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001240 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001241 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1242 is_solicitation ?
1243 &ip0->src_address :
1244 &h0->target_address,
Neale Ranns37029302018-08-10 05:30:06 -07001245 (mac_address_t *)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001246 o0->ethernet_address,
Neale Ranns37029302018-08-10 05:30:06 -07001247 IP_NEIGHBOR_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001248 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249
1250 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1251 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001252 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001253 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001254 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255
Dave Barachd7cb1b52016-12-09 09:52:16 -05001256 fib_index =
1257 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001259 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001260 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001261 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1262 }
1263 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001264 {
Neale Ranns53da2212018-02-24 02:11:19 -08001265 if (ip6_address_is_link_local_unicast (&h0->target_address))
1266 {
1267 fei = ip6_fib_table_lookup_exact_match
1268 (ip6_ll_fib_get (sw_if_index0),
1269 &h0->target_address, 128);
1270 }
1271 else
1272 {
1273 fei = ip6_fib_table_lookup_exact_match (fib_index,
1274 &h0->target_address,
1275 128);
1276 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001277
Neale Ranns3f844d02017-02-18 00:03:54 -08001278 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001279 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001280 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001281 error0 =
1282 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001283 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001284 else
1285 {
1286 if (FIB_ENTRY_FLAG_LOCAL &
1287 fib_entry_get_flags_for_source (fei,
1288 FIB_SOURCE_INTERFACE))
1289 {
1290 /* It's an address that belongs to one of our interfaces
1291 * that's good. */
1292 }
1293 else
1294 if (fib_entry_is_sourced
Neale Ranns53da2212018-02-24 02:11:19 -08001295 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
1296 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
Neale Ranns3f844d02017-02-18 00:03:54 -08001297 {
1298 /* The address was added by IPv6 Proxy ND config.
1299 * We should only respond to these if the NS arrived on
1300 * the link that has a matching covering prefix */
1301 }
1302 else
1303 {
1304 error0 =
1305 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1306 }
1307 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001308 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 }
1310
1311 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001312 next0 = (error0 != ICMP6_ERROR_NONE
1313 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1314 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315 else
1316 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001317 next0 = 0;
1318 error0 = error0 == ICMP6_ERROR_NONE ?
1319 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320 }
1321
1322 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1323 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001324 vnet_sw_interface_t *sw_if0;
1325 ethernet_interface_t *eth_if0;
1326 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327
Dave Barachd7cb1b52016-12-09 09:52:16 -05001328 /* dst address is either source address or the all-nodes mcast addr */
1329 if (!ip6_sadd_unspecified)
1330 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001332 ip6_set_reserved_multicast_address (&ip0->dst_address,
1333 IP6_MULTICAST_SCOPE_link_local,
1334 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335
1336 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001337 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338 h0->icmp.type = ICMP6_neighbor_advertisement;
1339
1340 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1341 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001342 eth_if0 =
1343 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001344 if (eth_if0 && o0)
1345 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001346 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1347 o0->header.type =
1348 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349 }
1350
1351 h0->advertisement_flags = clib_host_to_net_u32
1352 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1353 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1354
1355 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001356 h0->icmp.checksum =
1357 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1358 &bogus_length);
1359 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001360
Dave Barachd7cb1b52016-12-09 09:52:16 -05001361 /* Reuse current MAC header, copy SMAC to DMAC and
1362 * interface MAC to SMAC */
1363 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1364 eth0 = vlib_buffer_get_current (p0);
1365 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1366 if (eth_if0)
1367 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368
Dave Barachd7cb1b52016-12-09 09:52:16 -05001369 /* Setup input and output sw_if_index for packet */
1370 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1371 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1372 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1373 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374
1375 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001376 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001377
1378 p0->error = error_node->errors[error0];
1379
1380 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1381 to_next, n_left_to_next,
1382 bi0, next0);
1383 }
1384
1385 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1386 }
1387
1388 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001389 vlib_error_count (vm, error_node->node_index,
1390 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1391 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001392
1393 return frame->n_vectors;
1394}
1395
1396/* for "syslogging" - use elog for now */
1397#define foreach_log_level \
1398 _ (DEBUG, "DEBUG") \
1399 _ (INFO, "INFORMATION") \
1400 _ (NOTICE, "NOTICE") \
1401 _ (WARNING, "WARNING") \
1402 _ (ERR, "ERROR") \
1403 _ (CRIT, "CRITICAL") \
1404 _ (ALERT, "ALERT") \
1405 _ (EMERG, "EMERGENCY")
1406
Dave Barachd7cb1b52016-12-09 09:52:16 -05001407typedef enum
1408{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409#define _(f,s) LOG_##f,
1410 foreach_log_level
1411#undef _
1412} log_level_t;
1413
Dave Barachd7cb1b52016-12-09 09:52:16 -05001414static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001415#define _(f,s) s,
1416 foreach_log_level
1417#undef _
1418};
1419
Dave Barachd7cb1b52016-12-09 09:52:16 -05001420static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001421
1422static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001423ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001424{
1425 /* just use elog for now */
1426 u8 *what;
1427 va_list va;
1428
Dave Barachd7cb1b52016-12-09 09:52:16 -05001429 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1430 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001431
1432 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001433 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001434 {
1435 what = va_format (0, fmt, &va);
1436
Dave Barachd7cb1b52016-12-09 09:52:16 -05001437 ELOG_TYPE_DECLARE (e) =
1438 {
1439 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1440 struct
1441 {
1442 u32 s[2];
1443 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001445 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1446 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001447 }
1448 va_end (va);
1449 return;
1450}
1451
Juraj Sloboda52574522018-05-03 10:03:50 +02001452clib_error_t *
1453call_ip6_neighbor_callbacks (void *data,
1454 _vnet_ip6_neighbor_function_list_elt_t * elt)
1455{
1456 clib_error_t *error = 0;
1457
1458 while (elt)
1459 {
1460 error = elt->fp (data);
1461 if (error)
1462 return error;
1463 elt = elt->next_ip6_neighbor_function;
1464 }
1465
1466 return error;
1467}
1468
Ed Warnickecb9cada2015-12-08 15:45:58 -07001469/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001470typedef enum
1471{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001472 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1473 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1474 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1475 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1476} icmp6_router_solicitation_or_advertisement_next_t;
1477
1478static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001479icmp6_router_solicitation (vlib_main_t * vm,
1480 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001481{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001482 vnet_main_t *vnm = vnet_get_main ();
1483 ip6_main_t *im = &ip6_main;
1484 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001486 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001488 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489 int bogus_length;
1490
1491 icmp6_neighbor_discovery_option_type_t option_type;
1492
Dave Barachd7cb1b52016-12-09 09:52:16 -05001493 vlib_node_runtime_t *error_node =
1494 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495
1496 from = vlib_frame_vector_args (frame);
1497 n_left_from = n_packets;
1498 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001499
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500 if (node->flags & VLIB_NODE_FLAG_TRACE)
1501 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1502 /* stride */ 1,
1503 sizeof (icmp6_input_trace_t));
1504
1505 /* source may append his LL address */
1506 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1507
1508 while (n_left_from > 0)
1509 {
1510 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001511
Ed Warnickecb9cada2015-12-08 15:45:58 -07001512 while (n_left_from > 0 && n_left_to_next > 0)
1513 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001514 vlib_buffer_t *p0;
1515 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001516 ip6_radv_t *radv_info = 0;
1517
Dave Barachd7cb1b52016-12-09 09:52:16 -05001518 icmp6_neighbor_discovery_header_t *h0;
1519 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1520
Ed Warnickecb9cada2015-12-08 15:45:58 -07001521 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001522 u32 is_solicitation = 1, is_dropped = 0;
1523 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524
1525 bi0 = to_next[0] = from[0];
1526
1527 from += 1;
1528 to_next += 1;
1529 n_left_from -= 1;
1530 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001531
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532 p0 = vlib_get_buffer (vm, bi0);
1533 ip0 = vlib_buffer_get_current (p0);
1534 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001535 options_len0 =
1536 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1537 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1538 is_link_local =
1539 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540
1541 error0 = ICMP6_ERROR_NONE;
1542 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001543
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544 /* check if solicitation (not from nd_timer node) */
1545 if (ip6_address_is_unspecified (&ip0->dst_address))
1546 is_solicitation = 0;
1547
1548 /* Check that source address is unspecified, link-local or else on-link. */
1549 if (!is_unspecified && !is_link_local)
1550 {
1551 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552
Dave Barachd7cb1b52016-12-09 09:52:16 -05001553 if (ADJ_INDEX_INVALID != src_adj_index0)
1554 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001555 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001556
Dave Barachd7cb1b52016-12-09 09:52:16 -05001557 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1558 ?
1559 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1560 : error0);
1561 }
1562 else
1563 {
1564 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1565 }
1566 }
1567
Ed Warnickecb9cada2015-12-08 15:45:58 -07001568 /* check for source LL option and process */
1569 o0 = (void *) (h0 + 1);
1570 o0 = ((options_len0 == 8
1571 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001572 && o0->header.n_data_u64s == 1) ? o0 : 0);
1573
Ed Warnickecb9cada2015-12-08 15:45:58 -07001574 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001575 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1576 !is_unspecified && !is_link_local))
1577 {
Neale Ranns37029302018-08-10 05:30:06 -07001578 vnet_set_ip6_ethernet_neighbor
1579 (vm, sw_if_index0,
1580 &ip0->src_address,
1581 (mac_address_t *) o0->ethernet_address,
1582 IP_NEIGHBOR_FLAG_NONE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001583 }
1584
Ed Warnickecb9cada2015-12-08 15:45:58 -07001585 /* default is to drop */
1586 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001587
Ed Warnickecb9cada2015-12-08 15:45:58 -07001588 if (error0 == ICMP6_ERROR_NONE)
1589 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001590 vnet_sw_interface_t *sw_if0;
1591 ethernet_interface_t *eth_if0;
1592 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001593
1594 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1595 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001596 eth_if0 =
1597 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598
1599 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001600 error0 =
1601 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1602 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001603
1604 if (error0 == ICMP6_ERROR_NONE)
1605 {
1606 u32 ri;
1607
1608 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001609 p0->current_length -=
1610 (options_len0 +
1611 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001612
1613 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001614 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1615 sw_if_index0)
1616 {
1617 ri =
1618 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
Neale Ranns8e254952018-04-25 05:40:48 -07001620 if (ri != ~0)
1621 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1622 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001623
1624 error0 =
1625 ((!radv_info) ?
1626 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1627 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001628
1629 if (error0 == ICMP6_ERROR_NONE)
1630 {
1631 f64 now = vlib_time_now (vm);
1632
1633 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001634 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001635 {
Neale Ranns75152282017-01-09 01:00:45 -08001636 if (0 != radv_info->last_radv_time &&
1637 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001638 MIN_DELAY_BETWEEN_RAS)
1639 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001640 else
1641 radv_info->last_radv_time = now;
1642 }
1643
1644 /* send now */
1645 icmp6_router_advertisement_header_t rh;
1646
1647 rh.icmp.type = ICMP6_router_advertisement;
1648 rh.icmp.code = 0;
1649 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001650
Ed Warnickecb9cada2015-12-08 15:45:58 -07001651 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001652 rh.router_lifetime_in_sec =
1653 clib_host_to_net_u16
1654 (radv_info->adv_router_lifetime_in_sec);
1655 rh.
1656 time_in_msec_between_retransmitted_neighbor_solicitations
1657 =
1658 clib_host_to_net_u32 (radv_info->
1659 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1660 rh.neighbor_reachable_time_in_msec =
1661 clib_host_to_net_u32 (radv_info->
1662 adv_neighbor_reachable_time_in_msec);
1663
1664 rh.flags =
1665 (radv_info->adv_managed_flag) ?
1666 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1667 0;
1668 rh.flags |=
1669 ((radv_info->adv_other_flag) ?
1670 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1671 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672
1673
Dave Barachd7cb1b52016-12-09 09:52:16 -05001674 u16 payload_length =
1675 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001676
Dave Barach3a63fc52019-01-07 09:15:47 -05001677 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001678 (vm, &bi0, (void *) &rh,
Dave Barach3a63fc52019-01-07 09:15:47 -05001679 sizeof (icmp6_router_advertisement_header_t)))
1680 {
1681 /* buffer allocation failed, drop the pkt */
1682 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1683 goto drop0;
1684 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001685
Dave Barachd7cb1b52016-12-09 09:52:16 -05001686 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001688 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1689 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690
Dave Barachd7cb1b52016-12-09 09:52:16 -05001691 h.header.type =
1692 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001693 h.header.n_data_u64s = 1;
1694
1695 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001696 clib_memcpy (&h.ethernet_address[0],
1697 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
Dave Barach3a63fc52019-01-07 09:15:47 -05001699 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001700 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001701 sizeof
1702 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t)))
1703 {
1704 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1705 goto drop0;
1706 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001707
Dave Barachd7cb1b52016-12-09 09:52:16 -05001708 payload_length +=
1709 sizeof
1710 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001711 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001712
Ed Warnickecb9cada2015-12-08 15:45:58 -07001713 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001714 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001715 {
1716 icmp6_neighbor_discovery_mtu_option_t h;
1717
1718 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001719 h.mtu =
1720 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1722 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001723
1724 payload_length +=
1725 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001726
Dave Barach3a63fc52019-01-07 09:15:47 -05001727 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001728 (vm, &bi0, (void *) &h,
Dave Barach3a63fc52019-01-07 09:15:47 -05001729 sizeof
1730 (icmp6_neighbor_discovery_mtu_option_t)))
1731 {
1732 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1733 goto drop0;
1734 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001735 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001736
Ed Warnickecb9cada2015-12-08 15:45:58 -07001737 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001738 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739
Dave Barachd7cb1b52016-12-09 09:52:16 -05001740 /* *INDENT-OFF* */
1741 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1742 ({
1743 if(pr_info->enabled &&
1744 (!pr_info->decrement_lifetime_flag
1745 || (pr_info->pref_lifetime_expires >0)))
1746 {
1747 /* advertise this prefix */
1748 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001749
Dave Barachd7cb1b52016-12-09 09:52:16 -05001750 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1751 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001752
Dave Barachd7cb1b52016-12-09 09:52:16 -05001753 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001754
Dave Barachd7cb1b52016-12-09 09:52:16 -05001755 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1756 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
Dave Barachd7cb1b52016-12-09 09:52:16 -05001758 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1759 {
1760 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1761 h.preferred_time = 0;
1762 }
1763 else
1764 {
1765 if(pr_info->decrement_lifetime_flag)
1766 {
1767 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1768 (pr_info->valid_lifetime_expires - now) : 0;
1769
1770 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1771 (pr_info->pref_lifetime_expires - now) : 0;
1772 }
1773
1774 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1775 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1776 }
1777 h.unused = 0;
1778
1779 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1780
1781 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1782
Dave Barach3a63fc52019-01-07 09:15:47 -05001783 if (vlib_buffer_add_data
Damjan Marionab9b7ec2019-01-18 20:24:44 +01001784 (vm, &bi0, (void *)&h,
1785 sizeof(icmp6_neighbor_discovery_prefix_information_option_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05001786 {
1787 error0 = ICMP6_ERROR_ALLOC_FAILURE;
1788 goto drop0;
1789 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001790
1791 }
1792 }));
1793 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001794
1795 /* add additional options before here */
1796
1797 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001798 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001799 {
1800 ip0->dst_address = ip0->src_address;
1801 }
1802 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001803 {
1804 /* target address is all-nodes mcast addr */
1805 ip6_set_reserved_multicast_address
1806 (&ip0->dst_address,
1807 IP6_MULTICAST_SCOPE_link_local,
1808 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001809 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001810
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811 /* source address MUST be the link-local address */
1812 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001813
Dave Barachd7cb1b52016-12-09 09:52:16 -05001814 ip0->hop_limit = 255;
1815 ip0->payload_length =
1816 clib_host_to_net_u16 (payload_length);
1817
1818 icmp6_router_advertisement_header_t *rh0 =
1819 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1820 rh0->icmp.checksum =
1821 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1822 &bogus_length);
1823 ASSERT (bogus_length == 0);
1824
Ed Warnickecb9cada2015-12-08 15:45:58 -07001825 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001826 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001827 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001828
1829 if (is_solicitation)
1830 {
1831 ethernet_header_t *eth0;
1832 /* Reuse current MAC header, copy SMAC to DMAC and
1833 * interface MAC to SMAC */
1834 vlib_buffer_reset (p0);
1835 eth0 = vlib_buffer_get_current (p0);
1836 clib_memcpy (eth0->dst_address, eth0->src_address,
1837 6);
1838 clib_memcpy (eth0->src_address, eth_if0->address,
1839 6);
1840 next0 =
1841 is_dropped ? next0 :
1842 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1843 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1844 sw_if_index0;
1845 }
1846 else
1847 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001848 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001849 if (adj_index0 == 0)
1850 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1851 else
1852 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001853 next0 =
1854 is_dropped ? next0 :
1855 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1856 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1857 adj_index0;
1858 }
1859 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001860 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001861
1862 radv_info->n_solicitations_dropped += is_dropped;
1863 radv_info->n_solicitations_rcvd += is_solicitation;
1864
1865 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001866 {
1867 radv_info->n_advertisements_sent++;
1868 n_advertisements_sent++;
1869 }
1870 }
1871 }
1872 }
1873
Dave Barach3a63fc52019-01-07 09:15:47 -05001874 drop0:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001875 p0->error = error_node->errors[error0];
1876
Dave Barachd7cb1b52016-12-09 09:52:16 -05001877 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001879
Ed Warnickecb9cada2015-12-08 15:45:58 -07001880 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1881 to_next, n_left_to_next,
1882 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001883
Ed Warnickecb9cada2015-12-08 15:45:58 -07001884 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001885
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1887 }
1888
1889 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001890 vlib_error_count (vm, error_node->node_index,
1891 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1892 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001893
1894 return frame->n_vectors;
1895}
1896
1897 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1898static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001899icmp6_router_advertisement (vlib_main_t * vm,
1900 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001901{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001902 vnet_main_t *vnm = vnet_get_main ();
1903 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001904 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001905 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906 u32 n_left_from, n_left_to_next, next_index;
1907 u32 n_advertisements_rcvd = 0;
1908
Dave Barachd7cb1b52016-12-09 09:52:16 -05001909 vlib_node_runtime_t *error_node =
1910 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001911
1912 from = vlib_frame_vector_args (frame);
1913 n_left_from = n_packets;
1914 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001915
Ed Warnickecb9cada2015-12-08 15:45:58 -07001916 if (node->flags & VLIB_NODE_FLAG_TRACE)
1917 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1918 /* stride */ 1,
1919 sizeof (icmp6_input_trace_t));
1920
1921 while (n_left_from > 0)
1922 {
1923 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001924
Ed Warnickecb9cada2015-12-08 15:45:58 -07001925 while (n_left_from > 0 && n_left_to_next > 0)
1926 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001927 vlib_buffer_t *p0;
1928 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001929 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001930 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001931 u32 bi0, options_len0, sw_if_index0, next0, error0;
1932
1933 bi0 = to_next[0] = from[0];
1934
1935 from += 1;
1936 to_next += 1;
1937 n_left_from -= 1;
1938 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001939
Ed Warnickecb9cada2015-12-08 15:45:58 -07001940 p0 = vlib_get_buffer (vm, bi0);
1941 ip0 = vlib_buffer_get_current (p0);
1942 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001943 options_len0 =
1944 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001945
1946 error0 = ICMP6_ERROR_NONE;
1947 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1948
Dave Barachd7cb1b52016-12-09 09:52:16 -05001949 /* Check that source address is link-local */
1950 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001951 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1952
1953 /* default is to drop */
1954 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001955
Ed Warnickecb9cada2015-12-08 15:45:58 -07001956 n_advertisements_rcvd++;
1957
1958 if (error0 == ICMP6_ERROR_NONE)
1959 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001960 vnet_sw_interface_t *sw_if0;
1961 ethernet_interface_t *eth_if0;
1962
Ed Warnickecb9cada2015-12-08 15:45:58 -07001963 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1964 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001965 eth_if0 =
1966 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967
1968 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001969 error0 =
1970 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1971 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972
1973 if (error0 == ICMP6_ERROR_NONE)
1974 {
1975 u32 ri;
1976
1977 /* look up the radv_t information for this interface */
Neale Ranns8e254952018-04-25 05:40:48 -07001978 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) >
1979 sw_if_index0)
1980 {
1981 ri =
1982 nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001983
Neale Ranns8e254952018-04-25 05:40:48 -07001984 if (ri != ~0)
1985 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1986 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001987
1988 error0 =
1989 ((!radv_info) ?
1990 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1991 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001992
1993 if (error0 == ICMP6_ERROR_NONE)
1994 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01001995 radv_info->keep_sending_rs = 0;
1996
1997 ra_report_t r;
1998
1999 r.sw_if_index = sw_if_index0;
Neale Ranns37029302018-08-10 05:30:06 -07002000 memcpy (&r.router_address, &ip0->src_address, 16);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002001 r.current_hop_limit = h0->current_hop_limit;
2002 r.flags = h0->flags;
2003 r.router_lifetime_in_sec =
2004 clib_net_to_host_u16 (h0->router_lifetime_in_sec);
2005 r.neighbor_reachable_time_in_msec =
2006 clib_net_to_host_u32
2007 (h0->neighbor_reachable_time_in_msec);
2008 r.time_in_msec_between_retransmitted_neighbor_solicitations = clib_net_to_host_u32 (h0->time_in_msec_between_retransmitted_neighbor_solicitations);
2009 r.prefixes = 0;
2010
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002012 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
2013 && (h0->current_hop_limit !=
2014 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002015 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002016 ip6_neighbor_syslog (vm, LOG_WARNING,
2017 "our AdvCurHopLimit on %U doesn't agree with %U",
2018 format_vnet_sw_if_index_name,
2019 vnm, sw_if_index0,
2020 format_ip6_address,
2021 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002022 }
2023
Dave Barachd7cb1b52016-12-09 09:52:16 -05002024 if ((h0->flags &
2025 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
2026 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002027 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002028 ip6_neighbor_syslog (vm, LOG_WARNING,
2029 "our AdvManagedFlag on %U doesn't agree with %U",
2030 format_vnet_sw_if_index_name,
2031 vnm, sw_if_index0,
2032 format_ip6_address,
2033 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002034 }
2035
Dave Barachd7cb1b52016-12-09 09:52:16 -05002036 if ((h0->flags &
2037 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
2038 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002039 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002040 ip6_neighbor_syslog (vm, LOG_WARNING,
2041 "our AdvOtherConfigFlag on %U doesn't agree with %U",
2042 format_vnet_sw_if_index_name,
2043 vnm, sw_if_index0,
2044 format_ip6_address,
2045 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002046 }
2047
Dave Barachd7cb1b52016-12-09 09:52:16 -05002048 if ((h0->
2049 time_in_msec_between_retransmitted_neighbor_solicitations
2050 && radv_info->
2051 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
2052 && (h0->
2053 time_in_msec_between_retransmitted_neighbor_solicitations
2054 !=
2055 clib_host_to_net_u32 (radv_info->
2056 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002057 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002058 ip6_neighbor_syslog (vm, LOG_WARNING,
2059 "our AdvRetransTimer on %U doesn't agree with %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 }
2065
Dave Barachd7cb1b52016-12-09 09:52:16 -05002066 if ((h0->neighbor_reachable_time_in_msec &&
2067 radv_info->adv_neighbor_reachable_time_in_msec) &&
2068 (h0->neighbor_reachable_time_in_msec !=
2069 clib_host_to_net_u32
2070 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002071 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002072 ip6_neighbor_syslog (vm, LOG_WARNING,
2073 "our AdvReachableTime on %U doesn't agree with %U",
2074 format_vnet_sw_if_index_name,
2075 vnm, sw_if_index0,
2076 format_ip6_address,
2077 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002078 }
2079
2080 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002081 u8 *opt_hdr = (u8 *) (h0 + 1);
2082 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002083 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002084 icmp6_neighbor_discovery_option_header_t *o0 =
2085 (icmp6_neighbor_discovery_option_header_t *)
2086 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002087 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002088 icmp6_neighbor_discovery_option_type_t option_type =
2089 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002090
Dave Barachd7cb1b52016-12-09 09:52:16 -05002091 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002092 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002093 ip6_neighbor_syslog (vm, LOG_ERR,
2094 "malformed RA packet on %U from %U",
2095 format_vnet_sw_if_index_name,
2096 vnm, sw_if_index0,
2097 format_ip6_address,
2098 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002099 break;
2100 }
2101
Dave Barachd7cb1b52016-12-09 09:52:16 -05002102 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002103 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002104 ip6_neighbor_syslog (vm, LOG_ERR,
2105 " zero length option in RA on %U from %U",
2106 format_vnet_sw_if_index_name,
2107 vnm, sw_if_index0,
2108 format_ip6_address,
2109 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002110 break;
2111 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002112 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002113 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002114 ip6_neighbor_syslog (vm, LOG_ERR,
2115 "option length in RA packet greater than total length on %U from %U",
2116 format_vnet_sw_if_index_name,
2117 vnm, sw_if_index0,
2118 format_ip6_address,
2119 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002120 break;
2121 }
2122
2123 options_len0 -= opt_len;
2124 opt_hdr += opt_len;
2125
Dave Barachd7cb1b52016-12-09 09:52:16 -05002126 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002127 {
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002128 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address:
2129 {
2130 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2131 * h =
2132 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
2133 *) (o0);
2134
2135 if (opt_len < sizeof (*h))
2136 break;
2137
2138 memcpy (r.slla, h->ethernet_address, 6);
2139 }
2140 break;
2141
Ed Warnickecb9cada2015-12-08 15:45:58 -07002142 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05002143 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002144 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05002145 (icmp6_neighbor_discovery_mtu_option_t
2146 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002147
Dave Barachd7cb1b52016-12-09 09:52:16 -05002148 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002149 break;
2150
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002151 r.mtu = clib_net_to_host_u32 (h->mtu);
2152
Dave Barachd7cb1b52016-12-09 09:52:16 -05002153 if ((h->mtu && radv_info->adv_link_mtu) &&
2154 (h->mtu !=
2155 clib_host_to_net_u32
2156 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002157 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002158 ip6_neighbor_syslog (vm, LOG_WARNING,
2159 "our AdvLinkMTU on %U doesn't agree with %U",
2160 format_vnet_sw_if_index_name,
2161 vnm, sw_if_index0,
2162 format_ip6_address,
2163 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002164 }
2165 }
2166 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002167
Ed Warnickecb9cada2015-12-08 15:45:58 -07002168 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
2169 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002170 icmp6_neighbor_discovery_prefix_information_option_t
2171 * h =
2172 (icmp6_neighbor_discovery_prefix_information_option_t
2173 *) (o0);
2174
Ed Warnickecb9cada2015-12-08 15:45:58 -07002175 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002176 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002177 u32 preferred, valid;
2178
Dave Barachd7cb1b52016-12-09 09:52:16 -05002179 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002180 break;
2181
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002182 vec_validate (r.prefixes,
2183 vec_len (r.prefixes));
2184 ra_report_prefix_info_t *prefix =
2185 vec_elt_at_index (r.prefixes,
2186 vec_len (r.prefixes) - 1);
2187
Dave Barachd7cb1b52016-12-09 09:52:16 -05002188 preferred =
2189 clib_net_to_host_u32 (h->preferred_time);
2190 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002191
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002192 prefix->preferred_time = preferred;
2193 prefix->valid_time = valid;
2194 prefix->flags = h->flags & 0xc0;
Neale Ranns37029302018-08-10 05:30:06 -07002195 prefix->prefix.fp_len = h->dst_address_length;
2196 prefix->prefix.fp_addr.ip6 = h->dst_address;
2197 prefix->prefix.fp_proto = FIB_PROTOCOL_IP6;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002198
Ed Warnickecb9cada2015-12-08 15:45:58 -07002199 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002200 /* *INDENT-OFF* */
2201 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2202 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002203
Dave Barachd7cb1b52016-12-09 09:52:16 -05002204 ip6_address_t mask;
2205 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2206
2207 if(pr_info->enabled &&
2208 (pr_info->prefix_len == h->dst_address_length) &&
2209 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2210 {
2211 /* found it */
2212 if(!pr_info->decrement_lifetime_flag &&
2213 valid != pr_info->adv_valid_lifetime_in_secs)
2214 {
2215 ip6_neighbor_syslog(vm, LOG_WARNING,
2216 "our ADV validlifetime on %U for %U does not agree with %U",
2217 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2218 format_ip6_address, &h->dst_address);
2219 }
2220 if(!pr_info->decrement_lifetime_flag &&
2221 preferred != pr_info->adv_pref_lifetime_in_secs)
2222 {
2223 ip6_neighbor_syslog(vm, LOG_WARNING,
2224 "our ADV preferredlifetime on %U for %U does not agree with %U",
2225 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2226 format_ip6_address, &h->dst_address);
2227 }
2228 }
2229 break;
2230 }));
2231 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002232 break;
2233 }
2234 default:
2235 /* skip this one */
2236 break;
2237 }
2238 }
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002239 ra_publish (&r);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002240 }
2241 }
2242 }
2243
2244 p0->error = error_node->errors[error0];
2245
Dave Barachd7cb1b52016-12-09 09:52:16 -05002246 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002247 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002248
Ed Warnickecb9cada2015-12-08 15:45:58 -07002249 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2250 to_next, n_left_to_next,
2251 bi0, next0);
2252 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002253
Ed Warnickecb9cada2015-12-08 15:45:58 -07002254 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2255 }
2256
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002257 /* Account for router advertisements received. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002258 vlib_error_count (vm, error_node->node_index,
2259 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2260 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002261
2262 return frame->n_vectors;
2263}
2264
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002265static inline f64
2266random_f64_from_to (f64 from, f64 to)
2267{
2268 static u32 seed = 0;
2269 static u8 seed_set = 0;
2270 if (!seed_set)
2271 {
2272 seed = random_default_seed ();
2273 seed_set = 1;
2274 }
2275 return random_f64 (&seed) * (to - from) + from;
2276}
2277
2278static inline u8
2279get_mac_address (u32 sw_if_index, u8 * address)
2280{
2281 vnet_main_t *vnm = vnet_get_main ();
2282 vnet_hw_interface_t *hw_if = vnet_get_sup_hw_interface (vnm, sw_if_index);
2283 if (!hw_if->hw_address)
2284 return 1;
2285 clib_memcpy (address, hw_if->hw_address, 6);
2286 return 0;
2287}
2288
2289static inline vlib_buffer_t *
2290create_buffer_for_rs (vlib_main_t * vm, ip6_radv_t * radv_info)
2291{
2292 u32 bi0;
2293 vlib_buffer_t *p0;
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002294 icmp6_router_solicitation_header_t *rh;
2295 u16 payload_length;
2296 int bogus_length;
2297 u32 sw_if_index;
2298
2299 sw_if_index = radv_info->sw_if_index;
2300
2301 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
2302 {
2303 clib_warning ("buffer allocation failure");
2304 return 0;
2305 }
2306
2307 p0 = vlib_get_buffer (vm, bi0);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002308 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (p0);
2309 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
2310
2311 vnet_buffer (p0)->sw_if_index[VLIB_RX] = sw_if_index;
2312 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index;
2313
2314 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
2315
2316 rh = vlib_buffer_get_current (p0);
2317 p0->current_length = sizeof (*rh);
2318
2319 rh->neighbor.icmp.type = ICMP6_router_solicitation;
2320 rh->neighbor.icmp.code = 0;
2321 rh->neighbor.icmp.checksum = 0;
2322 rh->neighbor.reserved_must_be_zero = 0;
2323
2324 rh->link_layer_option.header.type =
2325 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
2326 if (0 != get_mac_address (sw_if_index,
2327 rh->link_layer_option.ethernet_address))
2328 {
2329 clib_warning ("interface with sw_if_index %u has no mac address",
2330 sw_if_index);
2331 vlib_buffer_free (vm, &bi0, 1);
2332 return 0;
2333 }
2334 rh->link_layer_option.header.n_data_u64s = 1;
2335
2336 payload_length = sizeof (rh->neighbor) + sizeof (u64);
2337
2338 rh->ip.ip_version_traffic_class_and_flow_label =
2339 clib_host_to_net_u32 (0x6 << 28);
2340 rh->ip.payload_length = clib_host_to_net_u16 (payload_length);
2341 rh->ip.protocol = IP_PROTOCOL_ICMP6;
2342 rh->ip.hop_limit = 255;
2343 rh->ip.src_address = radv_info->link_local_address;
2344 /* set address ff02::2 */
David Johnsond9818dd2018-12-14 14:53:41 -05002345 rh->ip.dst_address.as_u64[0] = clib_host_to_net_u64 (0xff02ULL << 48);
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002346 rh->ip.dst_address.as_u64[1] = clib_host_to_net_u64 (2);
2347
2348 rh->neighbor.icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, p0,
2349 &rh->ip,
2350 &bogus_length);
2351
2352 return p0;
2353}
2354
2355static inline void
2356stop_sending_rs (vlib_main_t * vm, ip6_radv_t * ra)
2357{
2358 u32 bi0;
2359
2360 ra->keep_sending_rs = 0;
2361 if (ra->buffer)
2362 {
2363 bi0 = vlib_get_buffer_index (vm, ra->buffer);
2364 vlib_buffer_free (vm, &bi0, 1);
2365 ra->buffer = 0;
2366 }
2367}
2368
2369static inline bool
2370check_send_rs (vlib_main_t * vm, ip6_radv_t * radv_info, f64 current_time,
2371 f64 * due_time)
2372{
2373 vlib_buffer_t *p0;
2374 vlib_frame_t *f;
2375 u32 *to_next;
2376 u32 next_index;
2377 vlib_buffer_t *c0;
2378 u32 ci0;
2379
2380 icmp6_send_router_solicitation_params_t *params;
2381
2382 if (!radv_info->keep_sending_rs)
2383 return false;
2384
2385 params = &radv_info->params;
2386
2387 if (radv_info->due_time > current_time)
2388 {
2389 *due_time = radv_info->due_time;
2390 return true;
2391 }
2392
2393 p0 = radv_info->buffer;
2394
2395 next_index = ip6_rewrite_mcast_node.index;
2396
2397 c0 = vlib_buffer_copy (vm, p0);
2398 ci0 = vlib_get_buffer_index (vm, c0);
2399
2400 f = vlib_get_frame_to_node (vm, next_index);
2401 to_next = vlib_frame_vector_args (f);
2402 to_next[0] = ci0;
2403 f->n_vectors = 1;
2404 vlib_put_frame_to_node (vm, next_index, f);
2405
2406 if (params->mrc != 0 && --radv_info->n_left == 0)
2407 stop_sending_rs (vm, radv_info);
2408 else
2409 {
2410 radv_info->sleep_interval =
2411 (2 + random_f64_from_to (-0.1, 0.1)) * radv_info->sleep_interval;
2412 if (radv_info->sleep_interval > params->mrt)
2413 radv_info->sleep_interval =
2414 (1 + random_f64_from_to (-0.1, 0.1)) * params->mrt;
2415
2416 radv_info->due_time = current_time + radv_info->sleep_interval;
2417
2418 if (params->mrd != 0
2419 && current_time > radv_info->start_time + params->mrd)
2420 stop_sending_rs (vm, radv_info);
2421 else
2422 *due_time = radv_info->due_time;
2423 }
2424
2425 return radv_info->keep_sending_rs;
2426}
2427
2428static uword
2429send_rs_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
2430 vlib_frame_t * f0)
2431{
2432 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2433 ip6_radv_t *radv_info;
2434 uword *event_data = 0;
2435 f64 sleep_time = 1e9;
2436 f64 current_time;
2437 f64 due_time;
2438 f64 dt = 0;
2439
2440 while (true)
2441 {
2442 vlib_process_wait_for_event_or_clock (vm, sleep_time);
2443 vlib_process_get_events (vm, &event_data);
2444 vec_reset_length (event_data);
2445
2446 current_time = vlib_time_now (vm);
2447 do
2448 {
2449 due_time = current_time + 1e9;
2450 /* *INDENT-OFF* */
2451 pool_foreach (radv_info, nm->if_radv_pool,
2452 ({
2453 if (check_send_rs (vm, radv_info, current_time, &dt)
2454 && (dt < due_time))
2455 due_time = dt;
2456 }));
2457 /* *INDENT-ON* */
2458 current_time = vlib_time_now (vm);
2459 }
2460 while (due_time < current_time);
2461
2462 sleep_time = due_time - current_time;
2463 }
2464
2465 return 0;
2466}
2467
2468/* *INDENT-OFF* */
2469VLIB_REGISTER_NODE (send_rs_process_node) = {
2470 .function = send_rs_process,
2471 .type = VLIB_NODE_TYPE_PROCESS,
2472 .name = "send-rs-process",
2473};
2474/* *INDENT-ON* */
2475
2476void
2477icmp6_send_router_solicitation (vlib_main_t * vm, u32 sw_if_index, u8 stop,
2478 icmp6_send_router_solicitation_params_t *
2479 params)
2480{
2481 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2482 u32 rai;
2483 ip6_radv_t *ra = 0;
2484
2485 ASSERT (~0 != sw_if_index);
2486
2487 rai = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2488 ra = pool_elt_at_index (nm->if_radv_pool, rai);
2489
Juraj Sloboda52574522018-05-03 10:03:50 +02002490 stop_sending_rs (vm, ra);
2491
2492 if (!stop)
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002493 {
2494 ra->keep_sending_rs = 1;
2495 ra->params = *params;
2496 ra->n_left = params->mrc;
2497 ra->start_time = vlib_time_now (vm);
2498 ra->sleep_interval = (1 + random_f64_from_to (-0.1, 0.1)) * params->irt;
2499 ra->due_time = 0; /* send first packet ASAP */
2500 ra->buffer = create_buffer_for_rs (vm, ra);
2501 if (!ra->buffer)
2502 ra->keep_sending_rs = 0;
2503 else
2504 vlib_process_signal_event (vm, send_rs_process_node.index, 1, 0);
2505 }
2506}
2507
Neale Ranns87df12d2017-02-18 08:16:41 -08002508/**
2509 * @brief Add a multicast Address to the advertised MLD set
2510 */
2511static void
2512ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2513{
2514 ip6_mldp_group_t *mcast_group_info;
2515 uword *p;
2516
2517 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002518 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002519 mcast_group_info =
2520 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2521
2522 /* add address */
2523 if (!mcast_group_info)
2524 {
2525 /* add */
2526 u32 mi;
2527 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2528
2529 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002530 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002531 0);
2532
2533 mcast_group_info->type = 4;
2534 mcast_group_info->mcast_source_address_pool = 0;
2535 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002536 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002537 sizeof (ip6_address_t));
2538 }
2539}
2540
2541/**
2542 * @brief Delete a multicast Address from the advertised MLD set
2543 */
2544static void
2545ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2546{
2547 ip6_mldp_group_t *mcast_group_info;
2548 uword *p;
2549
2550 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2551 mcast_group_info =
2552 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2553
2554 if (mcast_group_info)
2555 {
2556 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2557 /* old_value */ 0);
2558 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2559 }
2560}
2561
2562/**
2563 * @brief Add a multicast Address to the advertised MLD set
2564 */
2565static void
2566ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2567 ip6_multicast_address_scope_t scope,
2568 ip6_multicast_link_local_group_id_t group)
2569{
2570 ip6_address_t addr;
2571
2572 ip6_set_reserved_multicast_address (&addr, scope, group);
2573
2574 ip6_neighbor_add_mld_prefix (a, &addr);
2575}
2576
2577/**
2578 * @brief create and initialize router advertisement parameters with default
2579 * values for this intfc
2580 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002581u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002582ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002583 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002584{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002585 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2586 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002587 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002588 vnet_sw_interface_t *sw_if0;
2589 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002590
2591 /* lookup radv container - ethernet interfaces only */
2592 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002593 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002594 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2595
Dave Barachd7cb1b52016-12-09 09:52:16 -05002596 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002597 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002598
2599 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2600 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002601 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2602
Dave Barachd7cb1b52016-12-09 09:52:16 -05002603 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002604 {
2605 a = pool_elt_at_index (nm->if_radv_pool, ri);
2606
Dave Barachd7cb1b52016-12-09 09:52:16 -05002607 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002608 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002609 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002610 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002611
Neale Ranns32e1c012016-11-22 17:07:28 +00002612 /* release the lock on the interface's mcast adj */
2613 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002614
Neale Ranns87df12d2017-02-18 08:16:41 -08002615 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002616 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002617 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002618 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002619 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002620 }));
2621 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002622 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002623 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002624 }));
2625 /* *INDENT-ON* */
2626
Neale Ranns87df12d2017-02-18 08:16:41 -08002627 pool_free (a->mldp_group_pool);
2628 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002629
Neale Ranns87df12d2017-02-18 08:16:41 -08002630 mhash_free (&a->address_to_prefix_index);
2631 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002632
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002633 if (a->keep_sending_rs)
2634 a->keep_sending_rs = 0;
2635
Dave Barachd7cb1b52016-12-09 09:52:16 -05002636 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002637 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2638 ri = ~0;
2639 }
2640 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002641 else
2642 {
2643 if (is_add)
2644 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002645 pool_get (nm->if_radv_pool, a);
2646
2647 ri = a - nm->if_radv_pool;
2648 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2649
2650 /* initialize default values (most of which are zero) */
Dave Barachb7b92992018-10-17 10:38:51 -04002651 clib_memset (a, 0, sizeof (a[0]));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002652
2653 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002654 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2655 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2656 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2657 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2658
Neale Ranns87df12d2017-02-18 08:16:41 -08002659 /* send ll address source address option */
2660 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002661
2662 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2663 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2664 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2665 a->seed = (u32) clib_cpu_time_now ();
2666 (void) random_u32 (&a->seed);
2667 a->randomizer = clib_cpu_time_now ();
2668 (void) random_u64 (&a->randomizer);
2669
2670 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2671 a->initial_adverts_sent = a->initial_adverts_count - 1;
2672 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2673
2674 /* deafult is to send */
2675 a->send_radv = 1;
2676
2677 /* fill in radv_info for this interface that will be needed later */
Ole Troand7231612018-06-07 10:17:57 +02002678 a->adv_link_mtu =
2679 vnet_sw_interface_get_mtu (vnm, sw_if_index, VNET_MTU_IP6);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002680
2681 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2682
2683 /* fill in default link-local address (this may be overridden) */
2684 ip6_link_local_address_from_ethernet_address
2685 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002686
2687 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2688 sizeof (ip6_address_t));
2689 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2690 sizeof (ip6_address_t));
2691
Neale Ranns32e1c012016-11-22 17:07:28 +00002692 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2693 VNET_LINK_IP6,
2694 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002695
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01002696 a->keep_sending_rs = 0;
2697
Dave Barachd7cb1b52016-12-09 09:52:16 -05002698 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002699 ip6_neighbor_add_mld_grp (a,
2700 IP6_MULTICAST_SCOPE_link_local,
2701 IP6_MULTICAST_GROUP_ID_all_hosts);
2702 ip6_neighbor_add_mld_grp (a,
2703 IP6_MULTICAST_SCOPE_link_local,
2704 IP6_MULTICAST_GROUP_ID_all_routers);
2705 ip6_neighbor_add_mld_grp (a,
2706 IP6_MULTICAST_SCOPE_link_local,
2707 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002708 }
2709 }
2710 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002711}
2712
2713/* send an mldpv2 report */
2714static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002715ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002716{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002717 vnet_main_t *vnm = vnet_get_main ();
2718 vlib_main_t *vm = vnm->vlib_main;
2719 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2720 vnet_sw_interface_t *sw_if0;
2721 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002722 u32 ri;
2723 int bogus_length;
2724
Dave Barachd7cb1b52016-12-09 09:52:16 -05002725 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002726 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002727 vlib_buffer_t *b0;
2728 ip6_header_t *ip0;
2729 u32 *to_next;
2730 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002731 u32 bo0;
2732 u32 n_to_alloc = 1;
2733 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002734
Ed Warnickecb9cada2015-12-08 15:45:58 -07002735 icmp6_multicast_listener_report_header_t *rh0;
2736 icmp6_multicast_listener_report_packet_t *rp0;
2737
2738 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2739 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2740 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2741
2742 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2743 return;
2744
2745 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002746 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2747 ~0);
2748
Ed Warnickecb9cada2015-12-08 15:45:58 -07002749 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002750
2751 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002752 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002753
Ed Warnickecb9cada2015-12-08 15:45:58 -07002754 /* send report now - build a mldpv2 report packet */
Damjan Marion671e60e2018-12-30 18:09:59 +01002755 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002756 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002757 {
Dave Barach3a63fc52019-01-07 09:15:47 -05002758 alloc_fail:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002759 clib_warning ("buffer allocation failure");
2760 return;
2761 }
2762
2763 b0 = vlib_get_buffer (vm, bo0);
2764
2765 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002766 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002767
Dave Barachd7cb1b52016-12-09 09:52:16 -05002768 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002769
2770 b0->error = ICMP6_ERROR_NONE;
2771
2772 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002773 ip0 = (ip6_header_t *) & rp0->ip;
2774 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002775
Dave Barachb7b92992018-10-17 10:38:51 -04002776 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002777
2778 ip0->ip_version_traffic_class_and_flow_label =
2779 clib_host_to_net_u32 (0x6 << 28);
2780
2781 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002782 /* for DEBUG - vnet driver won't seem to emit router alerts */
2783 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2784 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002785
Ed Warnickecb9cada2015-12-08 15:45:58 -07002786 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002787
Ed Warnickecb9cada2015-12-08 15:45:58 -07002788 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002789 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2790 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002791
2792 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002793 ip6_set_reserved_multicast_address (&ip0->dst_address,
2794 IP6_MULTICAST_SCOPE_link_local,
2795 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2796
Ed Warnickecb9cada2015-12-08 15:45:58 -07002797 /* add reports here */
2798 ip6_mldp_group_t *m;
2799 int num_addr_records = 0;
2800 icmp6_multicast_address_record_t rr;
2801
2802 /* fill in the hop-by-hop extension header (router alert) info */
2803 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2804 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002805
Ed Warnickecb9cada2015-12-08 15:45:58 -07002806 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2807 rh0->alert.len = 2;
2808 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002809
Ed Warnickecb9cada2015-12-08 15:45:58 -07002810 rh0->pad.type = 1;
2811 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002812
Ed Warnickecb9cada2015-12-08 15:45:58 -07002813 rh0->icmp.checksum = 0;
2814
Dave Barachd7cb1b52016-12-09 09:52:16 -05002815 /* *INDENT-OFF* */
2816 pool_foreach (m, radv_info->mldp_group_pool,
2817 ({
2818 rr.type = m->type;
2819 rr.aux_data_len_u32s = 0;
2820 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2821 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002822
Dave Barachd7cb1b52016-12-09 09:52:16 -05002823 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002824
Damjan Marionab9b7ec2019-01-18 20:24:44 +01002825 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
2826 sizeof(icmp6_multicast_address_record_t)))
Dave Barach3a63fc52019-01-07 09:15:47 -05002827 {
2828 vlib_buffer_free (vm, &bo0, 1);
2829 goto alloc_fail;
2830 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831
Dave Barachd7cb1b52016-12-09 09:52:16 -05002832 payload_length += sizeof( icmp6_multicast_address_record_t);
2833 }));
2834 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002835
2836 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002837 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2838
Ed Warnickecb9cada2015-12-08 15:45:58 -07002839 /* update lengths */
2840 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2841
Dave Barachd7cb1b52016-12-09 09:52:16 -05002842 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2843 &bogus_length);
2844 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002845
Dave Barachd7cb1b52016-12-09 09:52:16 -05002846 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002847 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002848 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002849 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002850 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002852
Neale Ranns32e1c012016-11-22 17:07:28 +00002853 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002854 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002855
Neale Ranns32e1c012016-11-22 17:07:28 +00002856 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002857
Ed Warnickecb9cada2015-12-08 15:45:58 -07002858 f = vlib_get_frame_to_node (vm, node->index);
2859 to_next = vlib_frame_vector_args (f);
2860 to_next[0] = bo0;
2861 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002862
Ed Warnickecb9cada2015-12-08 15:45:58 -07002863 vlib_put_frame_to_node (vm, node->index, f);
2864 return;
2865}
2866
Dave Barachd7cb1b52016-12-09 09:52:16 -05002867/* *INDENT-OFF* */
2868VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2869{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002870 .function = icmp6_router_solicitation,
2871 .name = "icmp6-router-solicitation",
2872
2873 .vector_size = sizeof (u32),
2874
2875 .format_trace = format_icmp6_input_trace,
2876
2877 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2878 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002879 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002880 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002881 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2882 },
2883};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002884/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002885
2886/* send a RA or update the timer info etc.. */
2887static uword
2888ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002889 vlib_node_runtime_t * node,
2890 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002891{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002892 vnet_main_t *vnm = vnet_get_main ();
2893 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2894 ip6_radv_t *radv_info;
2895 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002896 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002897 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002898 u32 *to_next = 0;
2899 u32 bo0;
2900 icmp6_router_solicitation_header_t *h0;
2901 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002902 f64 now = vlib_time_now (vm);
2903
2904 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002905 /* *INDENT-OFF* */
2906 pool_foreach (radv_info, nm->if_radv_pool,
2907 ({
2908 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2909 {
2910 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2911 radv_info->next_multicast_time = now;
2912 radv_info->last_multicast_time = now;
2913 radv_info->last_radv_time = 0;
2914 radv_info->all_routers_mcast = 0;
2915 continue;
2916 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917
Dave Barachd7cb1b52016-12-09 09:52:16 -05002918 /* Make sure that we've joined the all-routers multicast group */
2919 if(!radv_info->all_routers_mcast)
2920 {
2921 /* send MDLP_REPORT_EVENT message */
2922 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2923 radv_info->all_routers_mcast = 1;
2924 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925
Dave Barachd7cb1b52016-12-09 09:52:16 -05002926 /* is it time to send a multicast RA on this interface? */
2927 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2928 {
2929 u32 n_to_alloc = 1;
2930 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002931
Dave Barachd7cb1b52016-12-09 09:52:16 -05002932 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2933 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002934
Dave Barachd7cb1b52016-12-09 09:52:16 -05002935 /* multicast send - compute next multicast send time */
2936 if( radv_info->initial_adverts_sent > 0)
2937 {
2938 radv_info->initial_adverts_sent--;
2939 if(rfn > radv_info-> initial_adverts_interval)
2940 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002941
Dave Barachd7cb1b52016-12-09 09:52:16 -05002942 /* check to see if we are ceasing to send */
2943 if( radv_info->initial_adverts_sent == 0)
2944 if(radv_info->cease_radv)
2945 radv_info->send_radv = 0;
2946 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002947
Dave Barachd7cb1b52016-12-09 09:52:16 -05002948 radv_info->next_multicast_time = rfn + now;
2949 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002950
Dave Barachd7cb1b52016-12-09 09:52:16 -05002951 /* send advert now - build a "solicted" router advert with unspecified source address */
Damjan Marion671e60e2018-12-30 18:09:59 +01002952 n_allocated = vlib_buffer_alloc (vm, &bo0, n_to_alloc);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002953
Dave Barachd7cb1b52016-12-09 09:52:16 -05002954 if (PREDICT_FALSE(n_allocated == 0))
2955 {
2956 clib_warning ("buffer allocation failure");
2957 continue;
2958 }
2959 b0 = vlib_get_buffer (vm, bo0);
2960 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2961 b0->error = ICMP6_ERROR_NONE;
2962 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2963
2964 h0 = vlib_buffer_get_current (b0);
2965
Dave Barachb7b92992018-10-17 10:38:51 -04002966 clib_memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05002967
2968 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2969 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2970 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2971 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2972 h0->ip.hop_limit = 255;
2973
2974 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2975 h0->ip.src_address.as_u64[0] = 0;
2976 h0->ip.src_address.as_u64[1] = 0;
2977
2978 h0->ip.dst_address.as_u64[0] = 0;
2979 h0->ip.dst_address.as_u64[1] = 0;
2980
2981 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2982
2983 if (PREDICT_FALSE(f == 0))
2984 {
2985 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2986 to_next = vlib_frame_vector_args (f);
2987 n_left_to_next = VLIB_FRAME_SIZE;
2988 n_this_frame = 0;
2989 }
2990
2991 n_this_frame++;
2992 n_left_to_next--;
2993 to_next[0] = bo0;
2994 to_next += 1;
2995
2996 if (PREDICT_FALSE(n_left_to_next == 0))
2997 {
2998 f->n_vectors = n_this_frame;
2999 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3000 f = 0;
3001 }
3002 }
3003 }));
3004 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003005
3006 if (f)
3007 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003008 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003009 f->n_vectors = n_this_frame;
3010 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
3011 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003012 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003013}
3014
3015static uword
3016ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
3017 vlib_node_runtime_t * node,
3018 vlib_frame_t * frame)
3019{
3020 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003021 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003022
3023 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003024
Ed Warnickecb9cada2015-12-08 15:45:58 -07003025 while (1)
3026 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003027 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003028
Dave Barachd7cb1b52016-12-09 09:52:16 -05003029 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003030
Dave Barachd7cb1b52016-12-09 09:52:16 -05003031 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003032 {
3033 /* No events found: timer expired. */
3034 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003035 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003036 }
3037 else
3038 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003039 switch (event_type)
3040 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003041
Dave Barachd7cb1b52016-12-09 09:52:16 -05003042 case ICMP6_ND_EVENT_INIT:
3043 break;
3044
3045 case ~0:
3046 break;
3047
3048 default:
3049 ASSERT (0);
3050 }
3051
Ed Warnickecb9cada2015-12-08 15:45:58 -07003052 if (event_data)
3053 _vec_len (event_data) = 0;
3054 }
3055 }
3056 return frame->n_vectors;
3057}
3058
Dave Barachd7cb1b52016-12-09 09:52:16 -05003059/* *INDENT-OFF* */
3060VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
3061{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062 .function = icmp6_router_advertisement,
3063 .name = "icmp6-router-advertisement",
3064
3065 .vector_size = sizeof (u32),
3066
3067 .format_trace = format_icmp6_input_trace,
3068
3069 .n_next_nodes = 1,
3070 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003071 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003072 },
3073};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003074/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003075
3076vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
3077
3078 .function = ip6_icmp_neighbor_discovery_event_process,
3079 .name = "ip6-icmp-neighbor-discovery-event-process",
3080 .type = VLIB_NODE_TYPE_PROCESS,
3081};
3082
3083static uword
3084icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003085 vlib_node_runtime_t * node, vlib_frame_t * frame)
3086{
3087 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3088 /* is_solicitation */
3089 1);
3090}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003091
3092static uword
3093icmp6_neighbor_advertisement (vlib_main_t * vm,
3094 vlib_node_runtime_t * node,
3095 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003096{
3097 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
3098 /* is_solicitation */
3099 0);
3100}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003101
Dave Barachd7cb1b52016-12-09 09:52:16 -05003102/* *INDENT-OFF* */
3103VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
3104{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105 .function = icmp6_neighbor_solicitation,
3106 .name = "icmp6-neighbor-solicitation",
3107
3108 .vector_size = sizeof (u32),
3109
3110 .format_trace = format_icmp6_input_trace,
3111
3112 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
3113 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003114 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003115 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
3116 },
3117};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003118/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003119
Dave Barachd7cb1b52016-12-09 09:52:16 -05003120/* *INDENT-OFF* */
3121VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
3122{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003123 .function = icmp6_neighbor_advertisement,
3124 .name = "icmp6-neighbor-advertisement",
3125
3126 .vector_size = sizeof (u32),
3127
3128 .format_trace = format_icmp6_input_trace,
3129
3130 .n_next_nodes = 1,
3131 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08003132 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003133 },
3134};
Dave Barachd7cb1b52016-12-09 09:52:16 -05003135/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003136
Neale Rannsc7b8f202018-04-25 06:34:31 -07003137typedef enum
3138{
3139 IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3140 IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX,
3141 IP6_DISCOVER_NEIGHBOR_N_NEXT,
3142} ip6_discover_neighbor_next_t;
3143
3144typedef enum
3145{
3146 IP6_DISCOVER_NEIGHBOR_ERROR_DROP,
3147 IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT,
3148 IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS,
3149} ip6_discover_neighbor_error_t;
3150
3151static uword
3152ip6_discover_neighbor_inline (vlib_main_t * vm,
3153 vlib_node_runtime_t * node,
3154 vlib_frame_t * frame, int is_glean)
3155{
3156 vnet_main_t *vnm = vnet_get_main ();
3157 ip6_main_t *im = &ip6_main;
3158 ip_lookup_main_t *lm = &im->lookup_main;
3159 u32 *from, *to_next_drop;
3160 uword n_left_from, n_left_to_next_drop;
Dave Barach49433ad2018-08-08 17:59:03 -04003161 u64 seed;
3162 u32 thread_index = vm->thread_index;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003163 int bogus_length;
3164 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3165
3166 if (node->flags & VLIB_NODE_FLAG_TRACE)
3167 ip6_forward_next_trace (vm, node, frame, VLIB_TX);
3168
Neale Rannscd35e532018-08-31 02:51:45 -07003169 seed = throttle_seed (&im->nd_throttle, thread_index, vlib_time_now (vm));
Neale Rannsc7b8f202018-04-25 06:34:31 -07003170
3171 from = vlib_frame_vector_args (frame);
3172 n_left_from = frame->n_vectors;
3173
3174 while (n_left_from > 0)
3175 {
3176 vlib_get_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3177 to_next_drop, n_left_to_next_drop);
3178
3179 while (n_left_from > 0 && n_left_to_next_drop > 0)
3180 {
Neale Rannscd35e532018-08-31 02:51:45 -07003181 u32 pi0, adj_index0, sw_if_index0, drop0, r0, next0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003182 vnet_hw_interface_t *hw_if0;
3183 ip6_radv_t *radv_info;
Neale Rannscd35e532018-08-31 02:51:45 -07003184 ip_adjacency_t *adj0;
3185 vlib_buffer_t *p0;
3186 ip6_header_t *ip0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003187
3188 pi0 = from[0];
3189
3190 p0 = vlib_get_buffer (vm, pi0);
3191
3192 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
3193
3194 ip0 = vlib_buffer_get_current (p0);
3195
3196 adj0 = adj_get (adj_index0);
3197
3198 if (!is_glean)
3199 {
3200 ip0->dst_address.as_u64[0] =
3201 adj0->sub_type.nbr.next_hop.ip6.as_u64[0];
3202 ip0->dst_address.as_u64[1] =
3203 adj0->sub_type.nbr.next_hop.ip6.as_u64[1];
3204 }
3205
Neale Rannsc7b8f202018-04-25 06:34:31 -07003206 sw_if_index0 = adj0->rewrite_header.sw_if_index;
3207 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
3208
Neale Rannscd35e532018-08-31 02:51:45 -07003209 /* combine the address and interface for a hash */
3210 r0 = ip6_address_hash_to_u64 (&ip0->dst_address) ^ sw_if_index0;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003211
Neale Rannscd35e532018-08-31 02:51:45 -07003212 drop0 = throttle_check (&im->nd_throttle, thread_index, r0, seed);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003213
3214 from += 1;
3215 n_left_from -= 1;
3216 to_next_drop[0] = pi0;
3217 to_next_drop += 1;
3218 n_left_to_next_drop -= 1;
3219
3220 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
3221
3222 /* If the interface is link-down, drop the pkt */
3223 if (!(hw_if0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP))
3224 drop0 = 1;
3225
3226 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) > sw_if_index0)
3227 {
3228 u32 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
3229
3230 if (ri != ~0)
3231 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3232 else
3233 drop0 = 1;
3234 }
3235 else
3236 drop0 = 1;
3237
3238 /*
3239 * the adj has been updated to a rewrite but the node the DPO that got
3240 * us here hasn't - yet. no big deal. we'll drop while we wait.
3241 */
3242 if (IP_LOOKUP_NEXT_REWRITE == adj0->lookup_next_index)
3243 drop0 = 1;
3244
3245 p0->error =
3246 node->errors[drop0 ? IP6_DISCOVER_NEIGHBOR_ERROR_DROP
3247 : IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT];
3248
3249 if (drop0)
3250 continue;
3251
3252 {
3253 u32 bi0 = 0;
3254 icmp6_neighbor_solicitation_header_t *h0;
3255 vlib_buffer_t *b0;
3256
3257 h0 = vlib_packet_template_get_packet
3258 (vm, &im->discover_neighbor_packet_template, &bi0);
John Lo084606b2018-06-19 15:27:48 -04003259 if (!h0)
3260 continue;
Neale Rannsc7b8f202018-04-25 06:34:31 -07003261
Neale Ranns45db8852019-01-09 00:04:04 -08003262 /* copy the persistent fields from the original */
3263 b0 = vlib_get_buffer (vm, bi0);
3264 clib_memcpy_fast (b0->opaque2, p0->opaque2, sizeof (p0->opaque2));
3265
Neale Rannsc7b8f202018-04-25 06:34:31 -07003266 /*
3267 * Build ethernet header.
3268 * Choose source address based on destination lookup
3269 * adjacency.
3270 */
3271 if (!ip6_src_address_for_packet (lm,
3272 sw_if_index0,
3273 &ip0->dst_address,
3274 &h0->ip.src_address))
3275 {
3276 /* There is no address on the interface */
3277 p0->error =
3278 node->errors[IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS];
3279 vlib_buffer_free (vm, &bi0, 1);
3280 continue;
3281 }
3282
3283 /*
3284 * Destination address is a solicited node multicast address.
3285 * We need to fill in
3286 * the low 24 bits with low 24 bits of target's address.
3287 */
3288 h0->ip.dst_address.as_u8[13] = ip0->dst_address.as_u8[13];
3289 h0->ip.dst_address.as_u8[14] = ip0->dst_address.as_u8[14];
3290 h0->ip.dst_address.as_u8[15] = ip0->dst_address.as_u8[15];
3291
3292 h0->neighbor.target_address = ip0->dst_address;
3293
3294 clib_memcpy (h0->link_layer_option.ethernet_address,
3295 hw_if0->hw_address, vec_len (hw_if0->hw_address));
3296
3297 /* $$$$ appears we need this; why is the checksum non-zero? */
3298 h0->neighbor.icmp.checksum = 0;
3299 h0->neighbor.icmp.checksum =
3300 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h0->ip,
3301 &bogus_length);
3302
3303 ASSERT (bogus_length == 0);
3304
3305 vlib_buffer_copy_trace_flag (vm, p0, bi0);
Neale Rannsc7b8f202018-04-25 06:34:31 -07003306 vnet_buffer (b0)->sw_if_index[VLIB_TX]
3307 = vnet_buffer (p0)->sw_if_index[VLIB_TX];
3308
3309 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
3310 radv_info->mcast_adj_index;
3311
3312 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
3313 next0 = IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX;
3314
3315 vlib_set_next_frame_buffer (vm, node, next0, bi0);
3316 }
3317 }
3318
3319 vlib_put_next_frame (vm, node, IP6_DISCOVER_NEIGHBOR_NEXT_DROP,
3320 n_left_to_next_drop);
3321 }
3322
3323 return frame->n_vectors;
3324}
3325
3326static uword
3327ip6_discover_neighbor (vlib_main_t * vm,
3328 vlib_node_runtime_t * node, vlib_frame_t * frame)
3329{
3330 return (ip6_discover_neighbor_inline (vm, node, frame, 0));
3331}
3332
3333static uword
3334ip6_glean (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
3335{
3336 return (ip6_discover_neighbor_inline (vm, node, frame, 1));
3337}
3338
3339static char *ip6_discover_neighbor_error_strings[] = {
3340 [IP6_DISCOVER_NEIGHBOR_ERROR_DROP] = "address overflow drops",
3341 [IP6_DISCOVER_NEIGHBOR_ERROR_REQUEST_SENT] = "neighbor solicitations sent",
3342 [IP6_DISCOVER_NEIGHBOR_ERROR_NO_SOURCE_ADDRESS]
3343 = "no source address for ND solicitation",
3344};
3345
3346/* *INDENT-OFF* */
3347VLIB_REGISTER_NODE (ip6_glean_node) =
3348{
3349 .function = ip6_glean,
3350 .name = "ip6-glean",
3351 .vector_size = sizeof (u32),
3352 .format_trace = format_ip6_forward_next_trace,
3353 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3354 .error_strings = ip6_discover_neighbor_error_strings,
3355 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3356 .next_nodes =
3357 {
3358 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3359 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3360 },
3361};
3362VLIB_REGISTER_NODE (ip6_discover_neighbor_node) =
3363{
3364 .function = ip6_discover_neighbor,
3365 .name = "ip6-discover-neighbor",
3366 .vector_size = sizeof (u32),
3367 .format_trace = format_ip6_forward_next_trace,
3368 .n_errors = ARRAY_LEN (ip6_discover_neighbor_error_strings),
3369 .error_strings = ip6_discover_neighbor_error_strings,
3370 .n_next_nodes = IP6_DISCOVER_NEIGHBOR_N_NEXT,
3371 .next_nodes =
3372 {
3373 [IP6_DISCOVER_NEIGHBOR_NEXT_DROP] = "ip6-drop",
3374 [IP6_DISCOVER_NEIGHBOR_NEXT_REPLY_TX] = "ip6-rewrite-mcast",
3375 },
3376};
3377/* *INDENT-ON* */
3378
Dave Barachd7cb1b52016-12-09 09:52:16 -05003379/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003380int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003381ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
3382 u8 suppress, u8 managed, u8 other,
3383 u8 ll_option, u8 send_unicast, u8 cease,
3384 u8 use_lifetime, u32 lifetime,
3385 u32 initial_count, u32 initial_interval,
3386 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003387{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003388 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3389 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003390 u32 ri;
3391
3392 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003393 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3394 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003395 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003396 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003397
Dave Barachd7cb1b52016-12-09 09:52:16 -05003398 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003399 {
3400
Dave Barachd7cb1b52016-12-09 09:52:16 -05003401 ip6_radv_t *radv_info;
3402 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003403
Dave Barachd7cb1b52016-12-09 09:52:16 -05003404 if ((max_interval != 0) && (min_interval == 0))
3405 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003406
Dave Barachd7cb1b52016-12-09 09:52:16 -05003407 max_interval =
3408 (max_interval !=
3409 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
3410 radv_info->max_radv_interval;
3411 min_interval =
3412 (min_interval !=
3413 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
3414 radv_info->min_radv_interval;
3415 lifetime =
3416 (use_lifetime !=
3417 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
3418 radv_info->adv_router_lifetime_in_sec;
3419
3420 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003421 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003422 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003423 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003424
3425 if (lifetime <= max_interval)
3426 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003427 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003428
3429 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003430 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003431 if ((min_interval > .75 * max_interval) || (min_interval < 3))
3432 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003433 }
3434
Dave Barachd7cb1b52016-12-09 09:52:16 -05003435 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
3436 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
3437 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003438
Dave Barachd7cb1b52016-12-09 09:52:16 -05003439 /*
3440 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
3441 if "flag" is clear don't change corresponding value
3442 */
3443 radv_info->send_radv =
3444 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
3445 radv_info->adv_managed_flag =
3446 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
3447 radv_info->adv_other_flag =
3448 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
3449 radv_info->adv_link_layer_address =
3450 (ll_option !=
3451 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
3452 radv_info->send_unicast =
3453 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
3454 radv_info->cease_radv =
3455 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
3456
3457 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003458 radv_info->max_radv_interval = max_interval;
3459 radv_info->adv_router_lifetime_in_sec = lifetime;
3460
Dave Barachd7cb1b52016-12-09 09:52:16 -05003461 radv_info->initial_adverts_count =
3462 (initial_count !=
3463 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
3464 radv_info->initial_adverts_count;
3465 radv_info->initial_adverts_interval =
3466 (initial_interval !=
3467 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
3468 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003469
3470 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003471 if ((cease != 0) && (is_no))
3472 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003473
Dave Barachd7cb1b52016-12-09 09:52:16 -05003474 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3475 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003476 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003477 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003478 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003479 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003480}
3481
3482int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003483ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
3484 ip6_address_t * prefix_addr, u8 prefix_len,
3485 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
3486 u8 no_advertise, u8 off_link, u8 no_autoconfig,
3487 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003488{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003489 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003490 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003491
Ed Warnickecb9cada2015-12-08 15:45:58 -07003492 u32 ri;
3493
3494 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003495 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3496 ~0);
3497
Ed Warnickecb9cada2015-12-08 15:45:58 -07003498 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3499
3500 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003501
3502 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003503 {
3504 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003505 ip6_radv_t *radv_info;
3506 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003507
3508 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003509 ip6_radv_prefix_t *prefix;
3510
Ed Warnickecb9cada2015-12-08 15:45:58 -07003511 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003512 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
3513
Ed Warnickecb9cada2015-12-08 15:45:58 -07003514 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
3515
Dave Barachd7cb1b52016-12-09 09:52:16 -05003516 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003517 {
3518 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003519 if (!prefix)
3520 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
3521
3522 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003523 return VNET_API_ERROR_INVALID_VALUE_2;
3524
Dave Barachd7cb1b52016-12-09 09:52:16 -05003525 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003526 /* do specific delete processing here before returning */
3527 /* try to remove from routing table */
3528
Dave Barachd7cb1b52016-12-09 09:52:16 -05003529 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
3530 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003531 pool_put (radv_info->adv_prefixes_pool, prefix);
3532
Dave Barachd7cb1b52016-12-09 09:52:16 -05003533 radv_info->initial_adverts_sent =
3534 radv_info->initial_adverts_count - 1;
3535 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003536 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003537 radv_info->last_radv_time = 0;
3538 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003539 }
3540
3541 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003542 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003543 {
3544 /* add */
3545 u32 pi;
3546 pool_get (radv_info->adv_prefixes_pool, prefix);
3547 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003548 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
3549 /* old_value */ 0);
3550
Dave Barachb7b92992018-10-17 10:38:51 -04003551 clib_memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05003552
Ed Warnickecb9cada2015-12-08 15:45:58 -07003553 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003554 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
3555
Ed Warnickecb9cada2015-12-08 15:45:58 -07003556 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003557 prefix->adv_on_link_flag = 1; /* L bit set */
3558 prefix->adv_autonomous_flag = 1; /* A bit set */
3559 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003560 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
3561 prefix->enabled = 1;
3562 prefix->decrement_lifetime_flag = 1;
3563 prefix->deprecated_prefix_flag = 1;
3564
Dave Barachd7cb1b52016-12-09 09:52:16 -05003565 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003566 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003567 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003568 /* insert prefix into routing table as a connected prefix */
3569 }
3570
Dave Barachd7cb1b52016-12-09 09:52:16 -05003571 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003572 goto restart;
3573 }
3574 else
3575 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003576
3577 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003578 return VNET_API_ERROR_INVALID_VALUE_2;
3579
Dave Barachd7cb1b52016-12-09 09:52:16 -05003580 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003581 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003582 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003583 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003584 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003585 }
3586
Dave Barachd7cb1b52016-12-09 09:52:16 -05003587 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003588 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003589 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003590 prefix->adv_pref_lifetime_in_secs = ~0;
3591 prefix->decrement_lifetime_flag = 0;
3592 }
3593 else
3594 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003595 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
3596 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003597 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003598
Ed Warnickecb9cada2015-12-08 15:45:58 -07003599 /* copy remaining */
3600 prefix->enabled = !(no_advertise != 0);
3601 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
3602 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
3603
Dave Barachd7cb1b52016-12-09 09:52:16 -05003604 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003605 /* restart */
3606 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003607 prefix->valid_lifetime_expires =
3608 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003609 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003610
3611 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
3612 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003613 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003614 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003615 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003616 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003617}
3618
3619clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003620ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
3621 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003622{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003623 vnet_main_t *vnm = vnet_get_main ();
3624 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3625 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003626 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003627 u8 suppress = 0, managed = 0, other = 0;
3628 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003629 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003630 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
3631 0, ra_initial_interval = 0;
3632 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003633
Dave Barachd7cb1b52016-12-09 09:52:16 -05003634 unformat_input_t _line_input, *line_input = &_line_input;
3635 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003636
3637 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003638 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003639 ip6_address_t ip6_addr;
3640 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003641
Ed Warnickecb9cada2015-12-08 15:45:58 -07003642
3643 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003644 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003645 return 0;
3646
3647 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003648 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003649 {
3650
Dave Barachd7cb1b52016-12-09 09:52:16 -05003651 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003652 unformat_vnet_sw_interface, vnm, &sw_if_index))
3653 {
3654 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003655 ethernet_interface_t *eth_if0 = 0;
3656
Ed Warnickecb9cada2015-12-08 15:45:58 -07003657 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003658 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3659 eth_if0 =
3660 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3661
3662 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003663 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003664 error =
3665 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07003666 goto done;
3667 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003668
Ed Warnickecb9cada2015-12-08 15:45:58 -07003669 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003670 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3671 sw_if_index, ~0);
3672
Ed Warnickecb9cada2015-12-08 15:45:58 -07003673 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003674
3675 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003676 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003677 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003678 }
3679 else
3680 {
3681 error = clib_error_return (0, "unknown interface %U'",
3682 format_unformat_error, line_input);
3683 goto done;
3684 }
3685 }
3686 else
3687 {
3688 error = clib_error_return (0, "invalid interface name %U'",
3689 format_unformat_error, line_input);
3690 goto done;
3691 }
3692 }
3693
3694 /* get the rest of the command */
3695 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3696 {
3697 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003698 is_no = 1;
3699 else if (unformat (line_input, "prefix %U/%d",
3700 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003701 {
3702 add_radv_info = 0;
3703 break;
3704 }
3705 else if (unformat (line_input, "ra-managed-config-flag"))
3706 {
3707 managed = 1;
3708 break;
3709 }
3710 else if (unformat (line_input, "ra-other-config-flag"))
3711 {
3712 other = 1;
3713 break;
3714 }
Chris Luke33879c92016-06-28 19:54:21 -04003715 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003716 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003717 {
Chris Luke33879c92016-06-28 19:54:21 -04003718 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003719 break;
3720 }
Chris Luke33879c92016-06-28 19:54:21 -04003721 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003722 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003723 {
Chris Luke33879c92016-06-28 19:54:21 -04003724 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003725 break;
3726 }
3727 else if (unformat (line_input, "ra-send-unicast"))
3728 {
3729 send_unicast = 1;
3730 break;
3731 }
3732 else if (unformat (line_input, "ra-lifetime"))
3733 {
3734 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003735 {
3736 error = unformat_parse_error (line_input);
3737 goto done;
3738 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003739 use_lifetime = 1;
3740 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003741 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003742 else if (unformat (line_input, "ra-initial"))
3743 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003744 if (!unformat
3745 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003746 {
3747 error = unformat_parse_error (line_input);
3748 goto done;
3749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003750 break;
3751 }
3752 else if (unformat (line_input, "ra-interval"))
3753 {
3754 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003755 {
3756 error = unformat_parse_error (line_input);
3757 goto done;
3758 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003759
3760 if (!unformat (line_input, "%d", &ra_min_interval))
3761 ra_min_interval = 0;
3762 break;
3763 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003764 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003765 {
3766 cease = 1;
3767 break;
3768 }
3769 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003770 {
3771 error = unformat_parse_error (line_input);
3772 goto done;
3773 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003774 }
3775
Dave Barachd7cb1b52016-12-09 09:52:16 -05003776 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003777 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003778 ip6_neighbor_ra_config (vm, sw_if_index,
3779 suppress, managed, other,
3780 suppress_ll_option, send_unicast, cease,
3781 use_lifetime, ra_lifetime,
3782 ra_initial_count, ra_initial_interval,
3783 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003784 }
3785 else
3786 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003787 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003788 u32 pref_lifetime_in_secs = 0;
3789 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003790 u8 no_advertise = 0;
3791 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003792 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003793 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003794
3795 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003796 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003797 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003798 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003799 {
3800 use_prefix_default_values = 1;
3801 break;
3802 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003803 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003804 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003805 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003806 pref_lifetime_in_secs = ~0;
3807 break;
3808 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003809 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3810 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003811 break;
3812 else
3813 break;
3814 }
3815
3816
3817 /* get the rest of the command */
3818 while (!use_prefix_default_values &&
3819 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3820 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003821 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003822 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003823 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003824 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003825 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003826 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003827 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003828 no_onlink = 1;
3829 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003830 {
3831 error = unformat_parse_error (line_input);
3832 goto done;
3833 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003834 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003835
3836 ip6_neighbor_ra_prefix (vm, sw_if_index,
3837 &ip6_addr, addr_len,
3838 use_prefix_default_values,
3839 valid_lifetime_in_secs,
3840 pref_lifetime_in_secs,
3841 no_advertise,
3842 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003843 }
3844
Billy McFalla9a20e72017-02-15 11:39:12 -05003845done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003846 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003847
Ed Warnickecb9cada2015-12-08 15:45:58 -07003848 return error;
3849}
3850
Chris Lukebfd32fd2016-06-24 20:38:06 -04003851static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003852ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003853{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003854 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003855 u32 i;
3856
3857 for (i = 0; i < vec_len (addrs); i++)
3858 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003859 ip_interface_address_t *a =
3860 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3861 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003862
3863 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003864 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003865 }
3866}
3867
Ed Warnickecb9cada2015-12-08 15:45:58 -07003868static clib_error_t *
3869show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003870 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003871{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003872 vnet_main_t *vnm = vnet_get_main ();
3873 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3874 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003875 u32 sw_if_index;
3876
3877 sw_if_index = ~0;
3878
Dave Barachd7cb1b52016-12-09 09:52:16 -05003879 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003880 {
3881 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003882
Dave Barachd7cb1b52016-12-09 09:52:16 -05003883 /* look up the radv_t information for this interface */
3884 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3885 sw_if_index, ~0);
3886
3887 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3888
3889 if (ri != ~0)
3890 {
3891 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3892 ip6_radv_t *radv_info;
3893 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3894
3895 vlib_cli_output (vm, "%U is admin %s\n",
3896 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003897 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003898 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3899 "up" : "down"));
3900
Chris Lukebfd32fd2016-06-24 20:38:06 -04003901 u32 ai;
3902 u32 *link_scope = 0, *global_scope = 0;
3903 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003904 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003905
Dave Barachd7cb1b52016-12-09 09:52:16 -05003906 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3907 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003908 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3909
Dave Barachd7cb1b52016-12-09 09:52:16 -05003910 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003911 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003912 a = pool_elt_at_index (lm->if_address_pool, ai);
3913 ip6_address_t *address =
3914 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003915
Chris Lukebfd32fd2016-06-24 20:38:06 -04003916 if (ip6_address_is_link_local_unicast (address))
3917 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003918 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003919 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003920 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003921 vec_add1 (local_scope, ai);
3922 else
3923 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003924
3925 ai = a->next_this_sw_interface;
3926 }
3927
Dave Barachd7cb1b52016-12-09 09:52:16 -05003928 if (vec_len (link_scope))
3929 {
3930 vlib_cli_output (vm, "\tLink-local address(es):\n");
3931 ip6_print_addrs (vm, link_scope);
3932 vec_free (link_scope);
3933 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003934
Dave Barachd7cb1b52016-12-09 09:52:16 -05003935 if (vec_len (local_scope))
3936 {
3937 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3938 ip6_print_addrs (vm, local_scope);
3939 vec_free (local_scope);
3940 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003941
Dave Barachd7cb1b52016-12-09 09:52:16 -05003942 if (vec_len (global_scope))
3943 {
3944 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3945 ip6_print_addrs (vm, global_scope);
3946 vec_free (global_scope);
3947 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003948
Dave Barachd7cb1b52016-12-09 09:52:16 -05003949 if (vec_len (unknown_scope))
3950 {
3951 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3952 ip6_print_addrs (vm, unknown_scope);
3953 vec_free (unknown_scope);
3954 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003955
Neale Ranns53da2212018-02-24 02:11:19 -08003956 vlib_cli_output (vm, "\tLink-local address(es):\n");
3957 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3958 &radv_info->link_local_address);
3959
Ed Warnickecb9cada2015-12-08 15:45:58 -07003960 vlib_cli_output (vm, "\tJoined group address(es):\n");
3961 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003962 /* *INDENT-OFF* */
3963 pool_foreach (m, radv_info->mldp_group_pool,
3964 ({
3965 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3966 &m->mcast_address);
3967 }));
3968 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003969
3970 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003971 ip6_radv_prefix_t *p;
3972 /* *INDENT-OFF* */
3973 pool_foreach (p, radv_info->adv_prefixes_pool,
3974 ({
3975 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3976 format_ip6_address, &p->prefix, p->prefix_len);
3977 }));
3978 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003979
Dave Barachd7cb1b52016-12-09 09:52:16 -05003980 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003981 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3982 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3983 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3984 vlib_cli_output (vm, "\tND DAD is disabled\n");
3985 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3986 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3987 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003988 vlib_cli_output (vm,
3989 "\tND advertised retransmit interval is %d (msec)\n",
3990 radv_info->
3991 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003992
3993 u32 ra_interval = radv_info->max_radv_interval;
3994 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003995 vlib_cli_output (vm,
3996 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003997 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003998 vlib_cli_output (vm,
3999 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004000 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004001 vlib_cli_output (vm,
4002 "\tHosts %s stateless autoconfig for addresses\n",
4003 (radv_info->adv_managed_flag) ? "use" :
4004 " don't use");
4005 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
4006 radv_info->n_advertisements_sent);
4007 vlib_cli_output (vm, "\tND router solicitations received %d\n",
4008 radv_info->n_solicitations_rcvd);
4009 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
4010 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004011 }
4012 else
4013 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04004014 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004015 format_unformat_error, input);
4016
4017 }
4018 }
4019 return error;
4020}
4021
Billy McFall0683c9c2016-10-13 08:27:31 -04004022/*?
4023 * This command is used to display various IPv6 attributes on a given
4024 * interface.
4025 *
4026 * @cliexpar
4027 * Example of how to display IPv6 settings:
4028 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4029 * GigabitEthernet2/0/0 is admin up
4030 * Link-local address(es):
4031 * fe80::ab8/64
4032 * Joined group address(es):
4033 * ff02::1
4034 * ff02::2
4035 * ff02::16
4036 * ff02::1:ff00:ab8
4037 * Advertised Prefixes:
4038 * prefix fe80::fe:28ff:fe9c:75b3, length 64
4039 * MTU is 1500
4040 * ICMP error messages are unlimited
4041 * ICMP redirects are disabled
4042 * ICMP unreachables are not sent
4043 * ND DAD is disabled
4044 * ND advertised reachable time is 0
4045 * ND advertised retransmit interval is 0 (msec)
4046 * ND router advertisements are sent every 200 seconds (min interval is 150)
4047 * ND router advertisements live for 600 seconds
4048 * Hosts use stateless autoconfig for addresses
4049 * ND router advertisements sent 19336
4050 * ND router solicitations received 0
4051 * ND router solicitations dropped 0
4052 * @cliexend
4053 * Example of output if IPv6 is not enabled on the interface:
4054 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
4055 * show ip6 interface: IPv6 not enabled on interface
4056 * @cliexend
4057?*/
4058/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004059VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
4060{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004061 .path = "show ip6 interface",
4062 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004063 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004064};
Billy McFall0683c9c2016-10-13 08:27:31 -04004065/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004066
4067clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05004068disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004069{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004070 clib_error_t *error = 0;
4071 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004072 u32 ri;
4073
4074 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004075 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4076 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004077 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004078
Ed Warnickecb9cada2015-12-08 15:45:58 -07004079 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004080 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004081 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004082 ip6_radv_t *radv_info;
4083
4084 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004085
4086 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01004087 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004088 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004089 {
4090 /* essentially "disables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004091 ip6_ll_prefix_t ilp = {
4092 .ilp_addr = radv_info->link_local_address,
4093 .ilp_sw_if_index = sw_if_index,
4094 };
4095 ip6_ll_table_entry_delete (&ilp);
4096 ip6_sw_interface_enable_disable (sw_if_index, 0);
Neale Ranns4008ac92017-02-13 23:20:04 -08004097 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Neale Ranns53da2212018-02-24 02:11:19 -08004098 ip6_neighbor_sw_interface_add_del (vnet_get_main (), sw_if_index,
4099 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004100 }
4101 }
4102 return error;
4103}
4104
4105int
Dave Barachd7cb1b52016-12-09 09:52:16 -05004106ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004107{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004108 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4109 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004110
Dave Barachd7cb1b52016-12-09 09:52:16 -05004111 /* look up the radv_t information for this interface */
4112 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4113 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004114
Dave Barachd7cb1b52016-12-09 09:52:16 -05004115 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07004116
Dave Barachd7cb1b52016-12-09 09:52:16 -05004117 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004118}
4119
Dave Barachd7cb1b52016-12-09 09:52:16 -05004120clib_error_t *
4121enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004122{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004123 clib_error_t *error = 0;
4124 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004125 u32 ri;
4126 int is_add = 1;
4127
4128 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004129 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
4130 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004131
Dave Barachd7cb1b52016-12-09 09:52:16 -05004132 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4133
4134 /* if not created yet */
4135 if (ri == ~0)
4136 {
4137 vnet_main_t *vnm = vnet_get_main ();
4138 vnet_sw_interface_t *sw_if0;
4139
4140 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
4141 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
4142 {
4143 ethernet_interface_t *eth_if0;
4144
4145 eth_if0 =
4146 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
4147 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004148 {
4149 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004150 ri =
4151 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004152
Dave Barachd7cb1b52016-12-09 09:52:16 -05004153 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004154 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004155 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004156 ip6_address_t link_local_address;
4157
Dave Barachd7cb1b52016-12-09 09:52:16 -05004158 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004159
Dave Barachd7cb1b52016-12-09 09:52:16 -05004160 ip6_link_local_address_from_ethernet_mac_address
4161 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004162
4163 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004164 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07004165 sw_if0->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek8dc80202017-08-15 13:52:22 +02004166 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004167 {
4168 /* make up an interface id */
Dave Barach611d9182018-03-12 15:56:41 -04004169 link_local_address.as_u64[1] =
4170 random_u64 (&radv_info->randomizer);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004171
4172 link_local_address.as_u64[0] =
4173 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004174 /* clear u bit */
4175 link_local_address.as_u8[8] &= 0xfd;
4176 }
Neale Ranns53da2212018-02-24 02:11:19 -08004177 {
4178 ip6_ll_prefix_t ilp = {
4179 .ilp_addr = link_local_address,
4180 .ilp_sw_if_index = sw_if_index,
4181 };
Dave Barachd7cb1b52016-12-09 09:52:16 -05004182
Neale Ranns53da2212018-02-24 02:11:19 -08004183 ip6_ll_table_entry_update (&ilp, FIB_ROUTE_PATH_LOCAL);
4184 }
Neale Ranns4008ac92017-02-13 23:20:04 -08004185
Ed Warnickecb9cada2015-12-08 15:45:58 -07004186 /* essentially "enables" ipv6 on this interface */
Neale Ranns53da2212018-02-24 02:11:19 -08004187 ip6_mfib_interface_enable_disable (sw_if_index, 1);
4188 ip6_sw_interface_enable_disable (sw_if_index, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004189
Neale Ranns53da2212018-02-24 02:11:19 -08004190 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004191 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004192 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004193 }
4194 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004195 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004196 }
4197 }
4198 return error;
4199}
4200
Neale Ranns53da2212018-02-24 02:11:19 -08004201int
4202ip6_get_ll_address (u32 sw_if_index, ip6_address_t * addr)
4203{
4204 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4205 ip6_radv_t *radv_info;
4206 u32 ri;
4207
Eyal Baricd307742018-07-22 12:45:15 +03004208 if (vec_len (nm->if_radv_pool_index_by_sw_if_index) <= sw_if_index)
Neale Ranns53da2212018-02-24 02:11:19 -08004209 return 0;
4210
4211 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
4212
4213 if (ri == ~0)
4214 return 0;
4215
4216 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4217 *addr = radv_info->link_local_address;
4218
4219 return (!0);
4220}
4221
Ed Warnickecb9cada2015-12-08 15:45:58 -07004222static clib_error_t *
4223enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004224 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004225{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004226 vnet_main_t *vnm = vnet_get_main ();
4227 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004228 u32 sw_if_index;
4229
4230 sw_if_index = ~0;
4231
Dave Barachd7cb1b52016-12-09 09:52:16 -05004232 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004233 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004234 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004235 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004236 else
4237 {
4238 error = clib_error_return (0, "unknown interface\n'",
4239 format_unformat_error, input);
4240
4241 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004242 return error;
4243}
4244
Billy McFall0683c9c2016-10-13 08:27:31 -04004245/*?
4246 * This command is used to enable IPv6 on a given interface.
4247 *
4248 * @cliexpar
4249 * Example of how enable IPv6 on a given interface:
4250 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
4251?*/
4252/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004253VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
4254{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004255 .path = "enable ip6 interface",
4256 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004257 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004258};
Billy McFall0683c9c2016-10-13 08:27:31 -04004259/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004260
4261static clib_error_t *
4262disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004263 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004264{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004265 vnet_main_t *vnm = vnet_get_main ();
4266 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004267 u32 sw_if_index;
4268
4269 sw_if_index = ~0;
4270
Dave Barachd7cb1b52016-12-09 09:52:16 -05004271 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004272 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004273 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004274 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004275 else
4276 {
4277 error = clib_error_return (0, "unknown interface\n'",
4278 format_unformat_error, input);
4279
4280 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07004281 return error;
4282}
4283
Billy McFall0683c9c2016-10-13 08:27:31 -04004284/*?
4285 * This command is used to disable IPv6 on a given interface.
4286 *
4287 * @cliexpar
4288 * Example of how disable IPv6 on a given interface:
4289 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
4290?*/
4291/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004292VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
4293{
Billy McFall0683c9c2016-10-13 08:27:31 -04004294 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004295 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04004296 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004297};
Billy McFall0683c9c2016-10-13 08:27:31 -04004298/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004299
Billy McFall0683c9c2016-10-13 08:27:31 -04004300/*?
4301 * This command is used to configure the neighbor discovery
4302 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
4303 * command to display some of the current neighbor discovery parameters
4304 * on a given interface. This command has three formats:
4305 *
4306 *
4307 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
4308 *
4309 * '<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>'
4310 *
4311 * Where:
4312 *
4313 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
4314 * router-advertisement messages to use stateful address
4315 * auto-configuration to obtain address information (sets the M-bit).
4316 * Default is the M-bit is not set and the '<em>no</em>' option
4317 * returns it to this default state.
4318 *
4319 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
4320 * router-advertisement messages that hosts use stateful auto
4321 * configuration to obtain nonaddress related information (sets
4322 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
4323 * option returns it to this default state.
4324 *
4325 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
4326 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
4327 * router-advertisement messages.
4328 *
4329 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
4330 * optional source link-layer address in the ICMPv6 router-advertisement
4331 * messages. Default is to include the optional source link-layer address
4332 * and the '<em>no</em>' option returns it to this default state.
4333 *
4334 * <em>[no] ra-send-unicast</em> - Use the source address of the
4335 * router-solicitation message if availiable. The default is to use
4336 * multicast address of all nodes, and the '<em>no</em>' option returns
4337 * it to this default state.
4338 *
4339 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
4340 * default router in ICMPv6 router-advertisement messages. The range is
4341 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
4342 * '<em><max-interval></em>'. The default value is 600 seconds and the
4343 * '<em>no</em>' option returns it to this default value.
4344 *
4345 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
4346 * router-advertisement messages sent and the interval between each
4347 * message. Range for count is 1 - 3 and default is 3. Range for interval
4348 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
4349 * returns both to their default value.
4350 *
4351 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
4352 * interval between sending ICMPv6 router-advertisement messages. The
4353 * range for max-interval is from 4 to 200 seconds. min-interval can not
4354 * be more than 75% of max-interval. If not set, min-interval will be
4355 * set to 75% of max-interval. The range for min-interval is from 3 to
4356 * 150 seconds. The '<em>no</em>' option returns both to their default
4357 * value.
4358 *
4359 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
4360 * The '<em>no</em>' options implies to start (or restart) sending
4361 * ICMPv6 router-advertisement messages.
4362 *
4363 *
4364 * <b>Format 2 - Prefix Options:</b>
4365 *
4366 * '<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>'
4367 *
4368 * Where:
4369 *
4370 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
4371 *
4372 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
4373 * length of time in seconds during what the prefix is valid for the purpose of
4374 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
4375 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
4376 * length of time in seconds during what addresses generated from the prefix remain
4377 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
4378 *
4379 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
4380 * are inifinte, no timeout.
4381 *
4382 * <em>no-advertise</em> - Do not send full router address in prefix
4383 * advertisement. Default is to advertise (i.e. - This flag is off by default).
4384 *
4385 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
4386 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
4387 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
4388 *
4389 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
4390 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
4391 *
4392 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
4393 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
4394 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
4395 * the L-bit.
4396 *
4397 *
4398 * <b>Format 3: - Default of Prefix:</b>
4399 *
4400 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
4401 *
4402 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
4403 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
4404 * is ignored and the prefix is deleted.
4405 *
4406 *
4407 * @cliexpar
4408 * Example of how set a router advertisement option:
4409 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
4410 * Example of how to add a prefix:
4411 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
4412 * Example of how to delete a prefix:
4413 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
4414?*/
4415/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004416VLIB_CLI_COMMAND (ip6_nd_command, static) =
4417{
Ed Warnickecb9cada2015-12-08 15:45:58 -07004418 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04004419 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07004420 .function = ip6_neighbor_cmd,
4421};
Billy McFall0683c9c2016-10-13 08:27:31 -04004422/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004423
4424clib_error_t *
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004425ip6_neighbor_set_link_local_address (vlib_main_t * vm, u32 sw_if_index,
4426 ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004427{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004428 clib_error_t *error = 0;
4429 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004430 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004431 ip6_radv_t *radv_info;
4432 vnet_main_t *vnm = vnet_get_main ();
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004433 ip6_ll_prefix_t pfx = { 0, };
Ed Warnickecb9cada2015-12-08 15:45:58 -07004434
Dave Barachd7cb1b52016-12-09 09:52:16 -05004435 if (!ip6_address_is_link_local_unicast (address))
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004436 return (error = clib_error_return (0, "address not link-local",
4437 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07004438
4439 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004440 enable_ip6_interface (vm, sw_if_index);
4441
Ed Warnickecb9cada2015-12-08 15:45:58 -07004442 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004443
4444 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004445 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004446 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004447
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004448 pfx.ilp_sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004449
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004450 pfx.ilp_addr = radv_info->link_local_address;
4451 ip6_ll_table_entry_delete (&pfx);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004452
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004453 pfx.ilp_addr = *address;
4454 ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_LOCAL);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004455
Juraj Sloboda5bb1eca2018-10-22 09:57:13 +02004456 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4457 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004458 }
4459 else
4460 {
4461 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
4462 error = clib_error_return (0, "ip6 not enabled for interface",
4463 format_unformat_error);
4464 }
4465 return error;
4466}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004467
Neale Ranns87df12d2017-02-18 08:16:41 -08004468/**
4469 * @brief callback when an interface address is added or deleted
4470 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07004471static void
4472ip6_neighbor_add_del_interface_address (ip6_main_t * im,
4473 uword opaque,
4474 u32 sw_if_index,
4475 ip6_address_t * address,
4476 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004477 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004478{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004479 vnet_main_t *vnm = vnet_get_main ();
4480 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004481 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004482 vlib_main_t *vm = vnm->vlib_main;
4483 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004484 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004485
4486 /* create solicited node multicast address for this interface adddress */
4487 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004488
Ed Warnickecb9cada2015-12-08 15:45:58 -07004489 a.as_u8[0xd] = address->as_u8[0xd];
4490 a.as_u8[0xe] = address->as_u8[0xe];
4491 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004492
4493 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004494 {
4495 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004496 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004497
4498 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004499 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4500 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004501 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004502 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004503 {
4504 /* get radv_info */
4505 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4506
4507 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004508 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004509 radv_info->ref_count++;
4510
Neale Ranns87df12d2017-02-18 08:16:41 -08004511 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004512 }
4513 }
4514 else
4515 {
4516
4517 /* delete */
4518 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004519 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
4520 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004521 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01004522
Dave Barachd7cb1b52016-12-09 09:52:16 -05004523 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004524 {
4525 /* get radv_info */
4526 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
4527
Neale Ranns87df12d2017-02-18 08:16:41 -08004528 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004529
4530 /* if interface up send MLDP "report" */
4531 radv_info->all_routers_mcast = 0;
4532
4533 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004534 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07004535 radv_info->ref_count--;
4536 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01004537 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
4538 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004539 }
4540}
4541
Dave Barachd7cb1b52016-12-09 09:52:16 -05004542clib_error_t *
4543ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004544{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004545 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07004546
4547 nm->limit_neighbor_cache_size = neighbor_limit;
4548 return 0;
4549}
4550
Neale Ranns15002542017-09-10 04:39:11 -07004551static void
4552ip6_neighbor_table_bind (ip6_main_t * im,
4553 uword opaque,
4554 u32 sw_if_index,
4555 u32 new_fib_index, u32 old_fib_index)
4556{
4557 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4558 ip6_neighbor_t *n = NULL;
4559 u32 i, *to_re_add = 0;
4560
4561 /* *INDENT-OFF* */
4562 pool_foreach (n, nm->neighbor_pool,
4563 ({
4564 if (n->key.sw_if_index == sw_if_index)
4565 vec_add1 (to_re_add, n - nm->neighbor_pool);
4566 }));
4567 /* *INDENT-ON* */
4568
4569 for (i = 0; i < vec_len (to_re_add); i++)
4570 {
4571 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
4572 ip6_neighbor_adj_fib_remove (n, old_fib_index);
4573 ip6_neighbor_adj_fib_add (n, new_fib_index);
4574 }
4575 vec_free (to_re_add);
4576}
4577
Dave Barachd7cb1b52016-12-09 09:52:16 -05004578static clib_error_t *
4579ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004580{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004581 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4582 ip6_main_t *im = &ip6_main;
4583
Ed Warnickecb9cada2015-12-08 15:45:58 -07004584 mhash_init (&nm->neighbor_index_by_key,
4585 /* value size */ sizeof (uword),
4586 /* key size */ sizeof (ip6_neighbor_key_t));
4587
Dave Barachd7cb1b52016-12-09 09:52:16 -05004588 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
4589 ip6_icmp_neighbor_solicitation_node.index);
4590 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
4591 ip6_icmp_neighbor_advertisement_node.index);
4592 icmp6_register_type (vm, ICMP6_router_solicitation,
4593 ip6_icmp_router_solicitation_node.index);
4594 icmp6_register_type (vm, ICMP6_router_advertisement,
4595 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07004596
4597 /* handler node for ip6 neighbor discovery events and timers */
4598 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
4599
4600 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004601 ip6_add_del_interface_address_callback_t cb;
Dave Barachb7b92992018-10-17 10:38:51 -04004602 clib_memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
Dave Barachd7cb1b52016-12-09 09:52:16 -05004603
Ed Warnickecb9cada2015-12-08 15:45:58 -07004604 /* when an interface address changes... */
4605 cb.function = ip6_neighbor_add_del_interface_address;
4606 cb.function_opaque = 0;
4607 vec_add1 (im->add_del_interface_address_callbacks, cb);
4608
Neale Ranns15002542017-09-10 04:39:11 -07004609 ip6_table_bind_callback_t cbt;
4610 cbt.function = ip6_neighbor_table_bind;
4611 cbt.function_opaque = 0;
4612 vec_add1 (im->table_bind_callbacks, cbt);
4613
Ed Warnickecb9cada2015-12-08 15:45:58 -07004614 mhash_init (&nm->pending_resolutions_by_address,
4615 /* value size */ sizeof (uword),
4616 /* key size */ sizeof (ip6_address_t));
4617
John Lo1edfba92016-08-27 01:11:57 -04004618 mhash_init (&nm->mac_changes_by_address,
4619 /* value size */ sizeof (uword),
4620 /* key size */ sizeof (ip6_address_t));
4621
Ed Warnickecb9cada2015-12-08 15:45:58 -07004622 /* default, configurable */
4623 nm->limit_neighbor_cache_size = 50000;
4624
Eyal Baric125ecc2017-09-20 11:29:17 +03004625 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
4626
Juraj Sloboda4b9669d2018-01-15 10:39:21 +01004627 nm->ip6_ra_publisher_node = (uword) ~ 0;
4628
Ed Warnickecb9cada2015-12-08 15:45:58 -07004629#if 0
4630 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004631 vec_validate_init_empty
4632 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004633#endif
4634
Ed Warnickecb9cada2015-12-08 15:45:58 -07004635 return 0;
4636}
4637
4638VLIB_INIT_FUNCTION (ip6_neighbor_init);
4639
4640
Dave Barachd7cb1b52016-12-09 09:52:16 -05004641void
4642vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4643 void *address_arg,
4644 uword node_index,
4645 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004646{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004647 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4648 ip6_address_t *address = address_arg;
4649 uword *p;
4650 pending_resolution_t *pr;
4651
Ed Warnickecb9cada2015-12-08 15:45:58 -07004652 pool_get (nm->pending_resolutions, pr);
4653
4654 pr->next_index = ~0;
4655 pr->node_index = node_index;
4656 pr->type_opaque = type_opaque;
4657 pr->data = data;
4658
4659 p = mhash_get (&nm->pending_resolutions_by_address, address);
4660 if (p)
4661 {
4662 /* Insert new resolution at the head of the list */
4663 pr->next_index = p[0];
4664 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4665 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004666
4667 mhash_set (&nm->pending_resolutions_by_address, address,
4668 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004669}
4670
Dave Barachd7cb1b52016-12-09 09:52:16 -05004671int
4672vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
Neale Ranns37029302018-08-10 05:30:06 -07004673 ip6_nd_change_event_cb_t data_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -05004674 u32 pid,
4675 void *address_arg,
4676 uword node_index,
4677 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004678{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004679 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4680 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004681
Eyal Barif9f40652017-04-01 03:55:08 +03004682 /* Try to find an existing entry */
4683 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4684 u32 *p = first;
4685 pending_resolution_t *mc;
4686 while (p && *p != ~0)
4687 {
4688 mc = pool_elt_at_index (nm->mac_changes, *p);
4689 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4690 && mc->pid == pid)
4691 break;
4692 p = &mc->next_index;
4693 }
4694
4695 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004696 if (is_add)
4697 {
Eyal Barif9f40652017-04-01 03:55:08 +03004698 if (found)
4699 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4700
John Lo1edfba92016-08-27 01:11:57 -04004701 pool_get (nm->mac_changes, mc);
Neale Rannsf12dad62018-06-04 18:41:24 -07004702 /* *INDENT-OFF* */
Eyal Barif9f40652017-04-01 03:55:08 +03004703 *mc = (pending_resolution_t)
4704 {
Neale Rannsf12dad62018-06-04 18:41:24 -07004705 .next_index = ~0,
4706 .node_index = node_index,
4707 .type_opaque = type_opaque,
4708 .data = data,
4709 .data_callback = data_callback,
4710 .pid = pid,
4711 };
4712 /* *INDENT-ON* */
John Lo1edfba92016-08-27 01:11:57 -04004713
Eyal Barif9f40652017-04-01 03:55:08 +03004714 /* Insert new resolution at the end of the list */
4715 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004716 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004717 p[0] = new_idx;
4718 else
4719 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004720 }
4721 else
4722 {
Eyal Barif9f40652017-04-01 03:55:08 +03004723 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004724 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004725
Eyal Barif9f40652017-04-01 03:55:08 +03004726 /* Clients may need to clean up pool entries, too */
Neale Ranns37029302018-08-10 05:30:06 -07004727 if (data_callback)
4728 (data_callback) (mc->data, NULL /* no new mac addrs */ , 0, NULL);
John Lo1edfba92016-08-27 01:11:57 -04004729
Eyal Barif9f40652017-04-01 03:55:08 +03004730 /* Remove the entry from the list and delete the entry */
4731 *p = mc->next_index;
4732 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004733
Eyal Barif9f40652017-04-01 03:55:08 +03004734 /* Remove from hash if we deleted the last entry */
4735 if (*p == ~0 && p == first)
4736 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004737 }
Eyal Barif9f40652017-04-01 03:55:08 +03004738 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004739}
4740
Dave Barachd7cb1b52016-12-09 09:52:16 -05004741int
4742vnet_ip6_nd_term (vlib_main_t * vm,
4743 vlib_node_runtime_t * node,
4744 vlib_buffer_t * p0,
4745 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004746 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004747{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004748 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4749 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
Neale Ranns37029302018-08-10 05:30:06 -07004750 mac_address_t mac;
John Lo1edfba92016-08-27 01:11:57 -04004751
Neale Ranns37029302018-08-10 05:30:06 -07004752 mac_address_from_bytes (&mac, eth->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004753 ndh = ip6_next_header (ip);
4754 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4755 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004756 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004757
4758 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4759 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4760 {
4761 u8 *t0 = vlib_add_trace (vm, node, p0,
4762 sizeof (icmp6_input_trace_t));
4763 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4764 }
4765
4766 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004767 if (PREDICT_FALSE
4768 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4769 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004770 {
Neale Ranns37029302018-08-10 05:30:06 -07004771 vnet_nd_wc_publish (sw_if_index, &mac, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004772 }
4773
4774 /* Check if MAC entry exsist for solicited target IP */
4775 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4776 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004777 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004778 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004779 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004780
4781 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004782 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004783 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4784 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004785 return 0; /* source link layer address option not present */
4786
John Lo1edfba92016-08-27 01:11:57 -04004787 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004788 macp =
4789 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004790 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004791 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004792 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004793 vlib_node_runtime_t *error_node =
4794 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004795 ip->dst_address = ip->src_address;
4796 ip->src_address = ndh->target_address;
4797 ip->hop_limit = 255;
4798 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004799 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004800 clib_memcpy (opt->ethernet_address, macp, 6);
4801 ndh->icmp.type = ICMP6_neighbor_advertisement;
4802 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004803 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4804 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004805 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004806 ndh->icmp.checksum =
4807 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4808 clib_memcpy (eth->dst_address, eth->src_address, 6);
4809 clib_memcpy (eth->src_address, macp, 6);
4810 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004811 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004812 return 1;
4813 }
John Lo1edfba92016-08-27 01:11:57 -04004814 }
4815
4816 return 0;
4817
4818}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004819
Neale Ranns3f844d02017-02-18 00:03:54 -08004820int
4821ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4822{
4823 u32 fib_index;
4824
4825 fib_prefix_t pfx = {
4826 .fp_len = 128,
4827 .fp_proto = FIB_PROTOCOL_IP6,
4828 .fp_addr = {
4829 .ip6 = *addr,
4830 },
4831 };
4832 ip46_address_t nh = {
4833 .ip6 = *addr,
4834 };
4835
4836 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4837
4838 if (~0 == fib_index)
4839 return VNET_API_ERROR_NO_SUCH_FIB;
4840
4841 if (is_del)
4842 {
4843 fib_table_entry_path_remove (fib_index,
4844 &pfx,
4845 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004846 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004847 &nh,
4848 sw_if_index,
4849 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4850 /* flush the ND cache of this address if it's there */
Neale Ranns0bdd3192018-09-07 11:04:52 -07004851 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (), sw_if_index, addr);
Neale Ranns3f844d02017-02-18 00:03:54 -08004852 }
4853 else
4854 {
4855 fib_table_entry_path_add (fib_index,
4856 &pfx,
4857 FIB_SOURCE_IP6_ND_PROXY,
4858 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004859 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004860 &nh,
4861 sw_if_index,
4862 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4863 }
4864 return (0);
4865}
4866
4867static clib_error_t *
4868set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4869 unformat_input_t * input, vlib_cli_command_t * cmd)
4870{
4871 vnet_main_t *vnm = vnet_get_main ();
4872 clib_error_t *error = 0;
4873 ip6_address_t addr;
4874 u32 sw_if_index;
4875 u8 is_del = 0;
4876
4877 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4878 {
4879 /* get the rest of the command */
4880 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4881 {
4882 if (unformat (input, "%U", unformat_ip6_address, &addr))
4883 break;
4884 else if (unformat (input, "delete") || unformat (input, "del"))
4885 is_del = 1;
4886 else
4887 return (unformat_parse_error (input));
4888 }
4889 }
4890
4891 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4892
4893 return error;
4894}
4895
4896/* *INDENT-OFF* */
4897VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4898{
4899 .path = "set ip6 nd proxy",
4900 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4901 .function = set_ip6_nd_proxy_cmd,
4902};
4903/* *INDENT-ON* */
4904
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004905void
Neale Ranns3be6b282016-12-20 14:24:01 +00004906ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004907{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004908 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4909 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004910 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004911
4912 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004913 pool_foreach (n, nm->neighbor_pool,
4914 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004915 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004916 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004917 adj_nbr_walk_nh6 (sw_if_index,
4918 &n->key.ip6_address,
4919 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004920 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004921 }));
4922 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08004923
4924 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4925
4926 if (ADJ_INDEX_INVALID != ai)
4927 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004928}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004929
John Lo8b81cb42017-06-26 01:40:20 -04004930void
Steven9f781d82018-06-05 11:09:32 -07004931send_ip6_na (vlib_main_t * vm, u32 sw_if_index)
John Lo8b81cb42017-06-26 01:40:20 -04004932{
4933 ip6_main_t *i6m = &ip6_main;
John Lo8b81cb42017-06-26 01:40:20 -04004934 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004935
Steven9f781d82018-06-05 11:09:32 -07004936 send_ip6_na_w_addr (vm, ip6_addr, sw_if_index);
Neale Ranns25b04942018-04-04 09:34:50 -07004937}
4938
4939void
4940send_ip6_na_w_addr (vlib_main_t * vm,
Steven9f781d82018-06-05 11:09:32 -07004941 const ip6_address_t * ip6_addr, u32 sw_if_index)
Neale Ranns25b04942018-04-04 09:34:50 -07004942{
4943 ip6_main_t *i6m = &ip6_main;
Steven9f781d82018-06-05 11:09:32 -07004944 vnet_main_t *vnm = vnet_get_main ();
4945 u8 *rewrite, rewrite_len;
4946 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
4947 u8 dst_address[6];
Neale Ranns25b04942018-04-04 09:34:50 -07004948
John Lo8b81cb42017-06-26 01:40:20 -04004949 if (ip6_addr)
4950 {
4951 clib_warning
4952 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4953 format_ip6_address, ip6_addr, sw_if_index);
4954
4955 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4956 int bogus_length;
4957 u32 bi = 0;
4958 icmp6_neighbor_solicitation_header_t *h =
4959 vlib_packet_template_get_packet (vm,
4960 &i6m->discover_neighbor_packet_template,
4961 &bi);
John Lo084606b2018-06-19 15:27:48 -04004962 if (!h)
4963 return;
4964
John Lo8b81cb42017-06-26 01:40:20 -04004965 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4966 IP6_MULTICAST_SCOPE_link_local,
4967 IP6_MULTICAST_GROUP_ID_all_hosts);
4968 h->ip.src_address = ip6_addr[0];
4969 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4970 h->neighbor.target_address = ip6_addr[0];
4971 h->neighbor.advertisement_flags = clib_host_to_net_u32
4972 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
ahdj00722130e12018-08-14 10:35:43 +08004973 h->link_layer_option.header.type =
4974 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo8b81cb42017-06-26 01:40:20 -04004975 clib_memcpy (h->link_layer_option.ethernet_address,
4976 hi->hw_address, vec_len (hi->hw_address));
4977 h->neighbor.icmp.checksum =
4978 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4979 ASSERT (bogus_length == 0);
4980
4981 /* Setup MAC header with IP6 Etype and mcast DMAC */
4982 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Steven9f781d82018-06-05 11:09:32 -07004983 ip6_multicast_ethernet_address (dst_address,
John Lo8b81cb42017-06-26 01:40:20 -04004984 IP6_MULTICAST_GROUP_ID_all_hosts);
Steven9f781d82018-06-05 11:09:32 -07004985 rewrite =
4986 ethernet_build_rewrite (vnm, sw_if_index, VNET_LINK_IP6, dst_address);
4987 rewrite_len = vec_len (rewrite);
4988 vlib_buffer_advance (b, -rewrite_len);
4989 ethernet_header_t *e = vlib_buffer_get_current (b);
4990 clib_memcpy (e->dst_address, rewrite, rewrite_len);
4991 vec_free (rewrite);
John Lo8b81cb42017-06-26 01:40:20 -04004992
4993 /* Send unsolicited ND advertisement packet out the specified interface */
4994 vnet_buffer (b)->sw_if_index[VLIB_RX] =
4995 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
4996 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
4997 u32 *to_next = vlib_frame_vector_args (f);
4998 to_next[0] = bi;
4999 f->n_vectors = 1;
5000 vlib_put_frame_to_node (vm, hi->output_node_index, f);
5001 }
5002}
5003
Dave Barachd7cb1b52016-12-09 09:52:16 -05005004/*
5005 * fd.io coding-style-patch-verification: ON
5006 *
5007 * Local Variables:
5008 * eval: (c-set-style "gnu")
5009 * End:
5010 */