blob: 073b67fed61f4839176853f399377729a36c5171 [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>
22#include <vppinfra/md5.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/adj/adj.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000024#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010025#include <vnet/fib/fib_table.h>
26#include <vnet/fib/ip6_fib.h>
Neale Ranns4008ac92017-02-13 23:20:04 -080027#include <vnet/mfib/ip6_mfib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Billy McFall0683c9c2016-10-13 08:27:31 -040029/**
30 * @file
31 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
32 *
33 * The files contains the API and CLI code for managing IPv6 neighbor
34 * adjacency tables and neighbor discovery logic.
35 */
36
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010037/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#define ETHER_MAC_ADDR_LEN 6
39
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010040/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050041typedef struct
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 /* basic advertised information */
44 ip6_address_t prefix;
45 u8 prefix_len;
46 int adv_on_link_flag;
47 int adv_autonomous_flag;
48 u32 adv_valid_lifetime_in_secs;
49 u32 adv_pref_lifetime_in_secs;
50
51 /* advertised values are computed from these times if decrementing */
52 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050053 f64 pref_lifetime_expires;
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 /* local information */
56 int enabled;
57 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050058 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
60#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
61#define DEF_ADV_VALID_LIFETIME 2592000
62#define DEF_ADV_PREF_LIFETIME 604800
63
64 /* extensions are added here, mobile, DNS etc.. */
65} ip6_radv_prefix_t;
66
67
Dave Barachd7cb1b52016-12-09 09:52:16 -050068typedef struct
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 /* group information */
71 u8 type;
72 ip6_address_t mcast_address;
73 u16 num_sources;
74 ip6_address_t *mcast_source_address_pool;
75} ip6_mldp_group_t;
76
77/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050078typedef struct
79{
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050082 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 int adv_managed_flag;
84 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 u32 adv_neighbor_reachable_time_in_msec;
87 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
88
89 /* mtu option */
90 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050091
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050093 u8 link_layer_address[8];
94 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99 /* Hash table mapping address to index in interface advertised prefix pool. */
100 mhash_t address_to_prefix_index;
101
102 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500103 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 /* Hash table mapping address to index in mldp address pool. */
106 mhash_t address_to_mldp_index;
107
108 /* local information */
109 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500110 int send_radv; /* radv on/off on this interface - set by config */
111 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 int send_unicast;
113 int adv_link_layer_address;
114 int prefix_option;
115 int failed_device_check;
116 int all_routers_mcast;
117 u32 seed;
118 u64 randomizer;
119 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000120 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 /* timing information */
123#define DEF_MAX_RADV_INTERVAL 200
124#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125#define DEF_CURR_HOP_LIMIT 64
126#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
127#define MAX_DEF_RTR_LIFETIME 9000
128
Dave Barachd7cb1b52016-12-09 09:52:16 -0500129#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
130#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
131#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
132#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
133#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 f64 max_radv_interval;
136 f64 min_radv_interval;
137 f64 min_delay_between_radv;
138 f64 max_delay_between_radv;
139 f64 max_rtr_default_lifetime;
140
141 f64 last_radv_time;
142 f64 last_multicast_time;
143 f64 next_multicast_time;
144
145
146 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500147 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 u32 initial_adverts_sent;
149
150 /* stats */
151 u32 n_advertisements_sent;
152 u32 n_solicitations_rcvd;
153 u32 n_solicitations_dropped;
154
155 /* Link local address to use (defaults to underlying physical for logical interfaces */
156 ip6_address_t link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157} ip6_radv_t;
158
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159typedef struct
160{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u32 next_index;
162 uword node_index;
163 uword type_opaque;
164 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400165 /* Used for nd event notification only */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500166 void *data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400167 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168} pending_resolution_t;
169
170
Dave Barachd7cb1b52016-12-09 09:52:16 -0500171typedef struct
172{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500174 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 /* lite beer "glean" adjacency handling */
177 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500178 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
John Lo1edfba92016-08-27 01:11:57 -0400180 /* Mac address change notification */
181 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400183
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barachd7cb1b52016-12-09 09:52:16 -0500186 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 mhash_t neighbor_index_by_key;
189
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
194 /* Neighbor attack mitigation */
195 u32 limit_neighbor_cache_size;
196 u32 neighbor_delete_rotor;
197
198} ip6_neighbor_main_t;
199
Neale Ranns3f844d02017-02-18 00:03:54 -0800200/* ipv6 neighbor discovery - timer/event types */
201typedef enum
202{
203 ICMP6_ND_EVENT_INIT,
204} ip6_icmp_neighbor_discovery_event_type_t;
205
206typedef union
207{
208 u32 add_del_swindex;
209 struct
210 {
211 u32 up_down_swindex;
212 u32 fib_index;
213 } up_down_event;
214} ip6_icmp_neighbor_discovery_event_data_t;
215
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216static ip6_neighbor_main_t ip6_neighbor_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500217static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219static u8 *
220format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
223 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
224 vnet_main_t *vnm = vnet_get_main ();
225 vnet_sw_interface_t *si;
226 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 if (!n)
229 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
230 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100231
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100234
235 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Neale Rannsb3b2de72017-03-08 05:17:22 -0800238 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
239 flags = format (flags, "N");
240
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100242 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
244 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500245 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 format_ethernet_address, n->link_layer_address,
247 format_vnet_sw_interface_name, vnm, si);
248
Dave Barachd7cb1b52016-12-09 09:52:16 -0500249 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 return s;
251}
252
253static clib_error_t *
254ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500255 u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500257 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
258 ip6_neighbor_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Dave Barachd7cb1b52016-12-09 09:52:16 -0500260 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
261 {
262 u32 i, *to_delete = 0;
263
264 /* *INDENT-OFF* */
265 pool_foreach (n, nm->neighbor_pool,
266 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 if (n->key.sw_if_index == sw_if_index)
268 vec_add1 (to_delete, n - nm->neighbor_pool);
269 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
272 for (i = 0; i < vec_len (to_delete); i++)
273 {
274 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
275 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700276 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700277 {
278 fib_prefix_t pfx = {
279 .fp_len = 128,
280 .fp_proto = FIB_PROTOCOL_IP6,
281 .fp_addr.ip6 = n->key.ip6_address,
282 };
283 fib_table_entry_path_remove
284 (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
285 &pfx,
286 FIB_SOURCE_ADJ,
287 FIB_PROTOCOL_IP6,
288 &pfx.fp_addr,
289 n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
290 pool_put (nm->neighbor_pool, n);
291 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 vec_free (to_delete);
294 }
295
296 return 0;
297}
298
299VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
300
Dave Barachd7cb1b52016-12-09 09:52:16 -0500301static void
302unset_random_neighbor_entry (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500304 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
305 vnet_main_t *vnm = vnet_get_main ();
306 vlib_main_t *vm = vnm->vlib_main;
307 ip6_neighbor_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 u32 index;
309
310 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
311 nm->neighbor_delete_rotor = index;
312
313 /* Try again from elt 0, could happen if an intfc goes down */
314 if (index == ~0)
315 {
316 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
317 nm->neighbor_delete_rotor = index;
318 }
319
320 /* Nothing left in the pool */
321 if (index == ~0)
322 return;
323
324 e = pool_elt_at_index (nm->neighbor_pool, index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500325
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327 &e->key.ip6_address,
328 e->link_layer_address,
329 ETHER_MAC_ADDR_LEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330}
331
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332typedef struct
333{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100335 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800336 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 u8 link_layer_address[6];
338 u32 sw_if_index;
339 ip6_address_t addr;
340} ip6_neighbor_set_unset_rpc_args_t;
341
Dave Barachd7cb1b52016-12-09 09:52:16 -0500342static void ip6_neighbor_set_unset_rpc_callback
343 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700344
Dave Barachd7cb1b52016-12-09 09:52:16 -0500345static void set_unset_ip6_neighbor_rpc
346 (vlib_main_t * vm,
347 u32 sw_if_index,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800348 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
349 int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350{
351 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500352 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354 args.sw_if_index = sw_if_index;
355 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100356 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800357 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100358 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800359 if (NULL != link_layer_address)
360 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500363 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100366static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500367ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100368{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500369 icmp6_neighbor_solicitation_header_t *h;
370 vnet_main_t *vnm = vnet_get_main ();
371 ip6_main_t *im = &ip6_main;
372 ip_interface_address_t *ia;
373 ip6_address_t *dst, *src;
374 vnet_hw_interface_t *hi;
375 vnet_sw_interface_t *si;
376 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100377 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500378 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100379 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100380
Dave Barachd7cb1b52016-12-09 09:52:16 -0500381 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100382
Dave Barachd7cb1b52016-12-09 09:52:16 -0500383 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100384 dst = &adj->sub_type.nbr.next_hop.ip6;
385
386 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100387 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100388 return;
389 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390 src = ip6_interface_address_matching_destination (im, dst,
391 adj->rewrite_header.
392 sw_if_index, &ia);
393 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100394 {
395 return;
396 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100397
Dave Barachd7cb1b52016-12-09 09:52:16 -0500398 h = vlib_packet_template_get_packet (vm,
399 &im->discover_neighbor_packet_template,
400 &bi);
Neale Rannsb80c5362016-10-08 13:03:40 +0100401
Dave Barachd7cb1b52016-12-09 09:52:16 -0500402 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100403
404 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
405 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
406 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
407 h->ip.src_address = src[0];
408 h->neighbor.target_address = dst[0];
409
410 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500411 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100412
413 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500414 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
415 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100416
417 b = vlib_get_buffer (vm, bi);
418 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500419 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100420
421 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
423 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100424
425 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500426 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
427 u32 *to_next = vlib_frame_vector_args (f);
428 to_next[0] = bi;
429 f->n_vectors = 1;
430 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100431 }
432}
433
434static void
435ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
436{
437 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
438 ethernet_build_rewrite (vnet_get_main (),
439 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500440 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100441 nbr->link_layer_address));
442}
443
444static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000445ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100446{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500447 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000448
Dave Barachd7cb1b52016-12-09 09:52:16 -0500449 adj_nbr_update_rewrite (ai,
450 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
451 ethernet_build_rewrite (vnet_get_main (),
452 adj->rewrite_header.
453 sw_if_index,
454 adj_get_link_type (ai),
455 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100456}
457
458#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
459{ \
460 k.sw_if_index = sw_if_index; \
461 k.ip6_address = *addr; \
462 k.pad = 0; \
463}
464
465static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500466ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100467{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500468 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
469 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100470 ip6_neighbor_key_t k;
471 uword *p;
472
Dave Barachd7cb1b52016-12-09 09:52:16 -0500473 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100474
475 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500476 if (p)
477 {
478 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
479 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100480
481 return (n);
482}
483
484static adj_walk_rc_t
485ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
486{
487 ip6_neighbor_t *nbr = ctx;
488
489 ip6_nd_mk_complete (ai, nbr);
490
491 return (ADJ_WALK_RC_CONTINUE);
492}
493
494static adj_walk_rc_t
495ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
496{
Neale Ranns19c68d22016-12-07 15:38:14 +0000497 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100498
499 return (ADJ_WALK_RC_CONTINUE);
500}
501
502void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500503ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100504{
505 ip6_neighbor_t *nbr;
506 ip_adjacency_t *adj;
507
508 adj = adj_get (ai);
509
510 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
511
Neale Ranns32e1c012016-11-22 17:07:28 +0000512 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100513 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000514 case IP_LOOKUP_NEXT_ARP:
515 case IP_LOOKUP_NEXT_GLEAN:
516 if (NULL != nbr)
517 {
518 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
519 ip6_nd_mk_complete_walk, nbr);
520 }
521 else
522 {
523 /*
524 * no matching ND entry.
525 * construct the rewrite required to for an ND packet, and stick
526 * that in the adj's pipe to smoke.
527 */
528 adj_nbr_update_rewrite (ai,
529 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
530 ethernet_build_rewrite (vnm,
531 sw_if_index,
532 VNET_LINK_IP6,
533 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
534
535 /*
536 * since the FIB has added this adj for a route, it makes sense it may
537 * want to forward traffic sometime soon. Let's send a speculative ND.
538 * just one. If we were to do periodically that wouldn't be bad either,
539 * but that's more code than i'm prepared to write at this time for
540 * relatively little reward.
541 */
542 ip6_nbr_probe (adj);
543 }
544 break;
545 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700546 {
547 /*
548 * Construct a partial rewrite from the known ethernet mcast dest MAC
549 */
550 u8 *rewrite;
551 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100552
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700553 rewrite = ethernet_build_rewrite (vnm,
554 sw_if_index,
555 adj->ia_link,
556 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000557
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700558 /*
559 * Complete the remaining fields of the adj's rewrite to direct the
560 * complete of the rewrite at switch time by copying in the IP
561 * dst address's bytes.
562 * Ofset is 2 bytes into the desintation address. And we write 4 bytes.
563 */
564 offset = vec_len (rewrite) - 2;
565 adj_mcast_update_rewrite (ai, rewrite, offset, 0xffffffff);
Neale Ranns32e1c012016-11-22 17:07:28 +0000566
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700567 break;
568 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000569 case IP_LOOKUP_NEXT_DROP:
570 case IP_LOOKUP_NEXT_PUNT:
571 case IP_LOOKUP_NEXT_LOCAL:
572 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800573 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000574 case IP_LOOKUP_NEXT_MIDCHAIN:
575 case IP_LOOKUP_NEXT_ICMP_ERROR:
576 case IP_LOOKUP_N_NEXT:
577 ASSERT (0);
578 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100579 }
580}
581
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582int
583vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500584 u32 sw_if_index,
585 ip6_address_t * a,
586 u8 * link_layer_address,
587 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800588 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500590 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500592 ip6_neighbor_t *n = 0;
593 int make_new_nd_cache_entry = 1;
594 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500596 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597
Damjan Marion586afd72017-04-05 19:18:20 +0200598 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 {
600 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800601 1 /* set new neighbor */ , is_static,
602 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603 return 0;
604 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
606 k.sw_if_index = sw_if_index;
607 k.ip6_address = a[0];
608 k.pad = 0;
609
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500611 if (p)
612 {
613 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
614 /* Refuse to over-write static neighbor entry. */
615 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
616 return -2;
617 make_new_nd_cache_entry = 0;
618 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100619
Dave Barachd7cb1b52016-12-09 09:52:16 -0500620 if (make_new_nd_cache_entry)
621 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500622 pool_get (nm->neighbor_pool, n);
623 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
624 /* old value */ 0);
625 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700626 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100627
Dave Barachd7cb1b52016-12-09 09:52:16 -0500628 clib_memcpy (n->link_layer_address,
629 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100630
Dave Barachd7cb1b52016-12-09 09:52:16 -0500631 /*
632 * create the adj-fib. the entry in the FIB table for and to the peer.
633 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800634 if (!is_no_fib_entry)
635 {
636 fib_prefix_t pfx = {
637 .fp_len = 128,
638 .fp_proto = FIB_PROTOCOL_IP6,
639 .fp_addr.ip6 = k.ip6_address,
640 };
641 u32 fib_index;
642
Neale Rannsdcd6d622017-05-26 02:59:16 -0700643 fib_index =
644 ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index);
Neale Rannsb3b2de72017-03-08 05:17:22 -0800645 n->fib_entry_index =
Neale Rannsdcd6d622017-05-26 02:59:16 -0700646 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
Neale Ranns81424992017-05-18 03:03:22 -0700647 FIB_ENTRY_FLAG_ATTACHED,
648 FIB_PROTOCOL_IP6, &pfx.fp_addr,
649 n->key.sw_if_index, ~0, 1, NULL,
650 FIB_ROUTE_PATH_FLAG_NONE);
Neale Ranns2a3ea492017-04-19 05:24:40 -0700651 }
652 else
653 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800654 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
655 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500656 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100657 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500658 {
659 /*
660 * prevent a DoS attack from the data-plane that
661 * spams us with no-op updates to the MAC address
662 */
663 if (0 == memcmp (n->link_layer_address,
664 link_layer_address, n_bytes_link_layer_address))
665 return -1;
Neale Rannsb80c5362016-10-08 13:03:40 +0100666
Dave Barachd7cb1b52016-12-09 09:52:16 -0500667 clib_memcpy (n->link_layer_address,
668 link_layer_address, n_bytes_link_layer_address);
669 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670
Neale Rannsb80c5362016-10-08 13:03:40 +0100671 /* Update time stamp and flags. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 n->cpu_time_last_updated = clib_cpu_time_now ();
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100673 if (is_static)
674 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100675 else
676 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
677
Neale Rannsb80c5362016-10-08 13:03:40 +0100678 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500679 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
681 /* Customer(s) waiting for this address to be resolved? */
682 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400683 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 {
John Lo1edfba92016-08-27 01:11:57 -0400685 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500686
687 while (next_index != (u32) ~ 0)
688 {
John Lo1edfba92016-08-27 01:11:57 -0400689 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
690 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500691 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400692 next_index = pr->next_index;
693 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500694 }
John Lo1edfba92016-08-27 01:11:57 -0400695
696 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 }
698
John Lo1edfba92016-08-27 01:11:57 -0400699 /* Customer(s) requesting ND event for this address? */
700 p = mhash_get (&nm->mac_changes_by_address, a);
701 if (p)
702 {
703 next_index = p[0];
704
Dave Barachd7cb1b52016-12-09 09:52:16 -0500705 while (next_index != (u32) ~ 0)
706 {
707 int (*fp) (u32, u8 *, u32, ip6_address_t *);
708 int rv = 1;
709 mc = pool_elt_at_index (nm->mac_changes, next_index);
710 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400711
Dave Barachd7cb1b52016-12-09 09:52:16 -0500712 /* Call the user's data callback, return 1 to suppress dup events */
713 if (fp)
714 rv =
715 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
716 /*
717 * Signal the resolver process, as long as the user
718 * says they want to be notified
719 */
720 if (rv == 0)
721 vlib_process_signal_event (vm, mc->node_index,
722 mc->type_opaque, mc->data);
723 next_index = mc->next_index;
724 }
John Lo1edfba92016-08-27 01:11:57 -0400725 }
726
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727 return 0;
728}
729
730int
731vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500732 u32 sw_if_index,
733 ip6_address_t * a,
734 u8 * link_layer_address,
735 uword n_bytes_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500737 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500739 ip6_neighbor_t *n;
740 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 int rv = 0;
742
Damjan Marion586afd72017-04-05 19:18:20 +0200743 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 {
745 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800746 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 return 0;
748 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
750 k.sw_if_index = sw_if_index;
751 k.ip6_address = a[0];
752 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500753
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 p = mhash_get (&nm->neighbor_index_by_key, &k);
755 if (p == 0)
756 {
757 rv = -1;
758 goto out;
759 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500760
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns19c68d22016-12-07 15:38:14 +0000762 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100763
Neale Rannsb80c5362016-10-08 13:03:40 +0100764 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500765 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100766
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700767
768 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700769 {
770 fib_prefix_t pfx = {
771 .fp_len = 128,
772 .fp_proto = FIB_PROTOCOL_IP6,
773 .fp_addr.ip6 = n->key.ip6_address,
774 };
775 fib_table_entry_path_remove
776 (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
777 &pfx,
778 FIB_SOURCE_ADJ,
779 FIB_PROTOCOL_IP6,
780 &pfx.fp_addr, n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
781 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500783
784out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 return rv;
786}
787
Dave Barachd7cb1b52016-12-09 09:52:16 -0500788static void ip6_neighbor_set_unset_rpc_callback
789 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500791 vlib_main_t *vm = vlib_get_main ();
792 if (a->is_add)
793 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800794 a->link_layer_address, 6, a->is_static,
795 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500797 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
798 a->link_layer_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800
801static int
802ip6_neighbor_sort (void *a1, void *a2)
803{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500804 vnet_main_t *vnm = vnet_get_main ();
805 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500807 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
808 n2->key.sw_if_index);
809 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
811 return cmp;
812}
813
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100814ip6_neighbor_t *
815ip6_neighbors_entries (u32 sw_if_index)
816{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500817 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100818 ip6_neighbor_t *n, *ns = 0;
819
820 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500821 pool_foreach (n, nm->neighbor_pool,
822 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100823 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
824 continue;
825 vec_add1 (ns, n[0]);
826 }));
827 /* *INDENT-ON* */
828
829 if (ns)
830 vec_sort_with_function (ns, ip6_neighbor_sort);
831 return ns;
832}
833
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834static clib_error_t *
835show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500836 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500838 vnet_main_t *vnm = vnet_get_main ();
839 ip6_neighbor_t *n, *ns;
840 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 u32 sw_if_index;
842
843 /* Filter entries by interface if given. */
844 sw_if_index = ~0;
845 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
846
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100847 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -0400848 if (ns)
849 {
Billy McFall46529cd2016-10-14 10:45:49 -0400850 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500851 vec_foreach (n, ns)
852 {
853 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -0400854 }
855 vec_free (ns);
856 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857
858 return error;
859}
860
Billy McFall0683c9c2016-10-13 08:27:31 -0400861/*?
862 * This command is used to display the adjacent IPv6 hosts found via
863 * neighbor discovery. Optionally, limit the output to the specified
864 * interface.
865 *
866 * @cliexpar
867 * Example of how to display the IPv6 neighbor adjacency table:
868 * @cliexstart{show ip6 neighbors}
869 * Time Address Flags Link layer Interface
870 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
871 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
872 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
873 * @cliexend
874 * Example of how to display the IPv6 neighbor adjacency table for given interface:
875 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
876 * Time Address Flags Link layer Interface
877 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
878 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
879 * @cliexend
880?*/
881/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
883 .path = "show ip6 neighbors",
884 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -0400885 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886};
Billy McFall0683c9c2016-10-13 08:27:31 -0400887/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
889static clib_error_t *
890set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500891 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500893 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 ip6_address_t addr;
895 u8 mac_address[6];
896 int addr_valid = 0;
897 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100898 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800899 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 u32 sw_if_index;
901
Dave Barachd7cb1b52016-12-09 09:52:16 -0500902 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 {
904 /* intfc, ip6-address, mac-address */
905 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500906 unformat_vnet_sw_interface, vnm, &sw_if_index,
907 unformat_ip6_address, &addr,
908 unformat_ethernet_address, mac_address))
909 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
911 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500912 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100913 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500914 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800915 else if (unformat (input, "no-fib-entry"))
916 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500918 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 }
920
921 if (!addr_valid)
922 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -0500923
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924 if (!is_del)
925 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500926 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800927 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 else
929 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500930 mac_address, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 return 0;
932}
933
Billy McFall0683c9c2016-10-13 08:27:31 -0400934/*?
935 * This command is used to manually add an entry to the IPv6 neighbor
936 * adjacency table. Optionally, the entry can be added as static. It is
937 * also used to remove an entry from the table. Use the '<em>show ip6
938 * neighbors</em>' command to display all learned and manually entered entries.
939 *
940 * @cliexpar
941 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
942 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
943 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
944 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
945?*/
946/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500947VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
948{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949 .path = "set ip6 neighbor",
950 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -0400951 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952};
Billy McFall0683c9c2016-10-13 08:27:31 -0400953/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
Dave Barachd7cb1b52016-12-09 09:52:16 -0500955typedef enum
956{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
958 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
959 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
960} icmp6_neighbor_solicitation_or_advertisement_next_t;
961
962static_always_inline uword
963icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
964 vlib_node_runtime_t * node,
965 vlib_frame_t * frame,
966 uword is_solicitation)
967{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500968 vnet_main_t *vnm = vnet_get_main ();
969 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500971 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
973 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500974 vlib_node_runtime_t *error_node =
975 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976 int bogus_length;
977
978 from = vlib_frame_vector_args (frame);
979 n_left_from = n_packets;
980 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500981
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982 if (node->flags & VLIB_NODE_FLAG_TRACE)
983 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
984 /* stride */ 1,
985 sizeof (icmp6_input_trace_t));
986
Dave Barachd7cb1b52016-12-09 09:52:16 -0500987 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988 (is_solicitation
989 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
990 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
991 n_advertisements_sent = 0;
992
993 while (n_left_from > 0)
994 {
995 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
996
997 while (n_left_from > 0 && n_left_to_next > 0)
998 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500999 vlib_buffer_t *p0;
1000 ip6_header_t *ip0;
1001 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1002 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001004 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1005 int is_rewrite0;
1006 u32 ni0;
1007
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 bi0 = to_next[0] = from[0];
1009
1010 from += 1;
1011 to_next += 1;
1012 n_left_from -= 1;
1013 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001014
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 p0 = vlib_get_buffer (vm, bi0);
1016 ip0 = vlib_buffer_get_current (p0);
1017 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001018 options_len0 =
1019 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020
1021 error0 = ICMP6_ERROR_NONE;
1022 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001023 ip6_sadd_link_local =
1024 ip6_address_is_link_local_unicast (&ip0->src_address);
1025 ip6_sadd_unspecified =
1026 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027
1028 /* Check that source address is unspecified, link-local or else on-link. */
1029 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1030 {
1031 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
Dave Barachd7cb1b52016-12-09 09:52:16 -05001033 if (ADJ_INDEX_INVALID != src_adj_index0)
1034 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001035 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
Dave Barachd7cb1b52016-12-09 09:52:16 -05001037 /* Allow all realistic-looking rewrite adjacencies to pass */
1038 ni0 = adj0->lookup_next_index;
1039 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1040 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001041
Dave Barachd7cb1b52016-12-09 09:52:16 -05001042 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1043 || !is_rewrite0)
1044 ?
1045 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1046 : error0);
1047 }
1048 else
1049 {
1050 error0 =
1051 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1052 }
1053 }
1054
Ed Warnickecb9cada2015-12-08 15:45:58 -07001055 o0 = (void *) (h0 + 1);
1056 o0 = ((options_len0 == 8 && o0->header.type == option_type
1057 && o0->header.n_data_u64s == 1) ? o0 : 0);
1058
1059 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001060 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001061 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001062 {
1063 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1064 if (nm->limit_neighbor_cache_size &&
1065 pool_elts (nm->neighbor_pool) >=
1066 nm->limit_neighbor_cache_size)
1067 unset_random_neighbor_entry ();
1068 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1069 is_solicitation ?
1070 &ip0->src_address :
1071 &h0->target_address,
1072 o0->ethernet_address,
1073 sizeof (o0->ethernet_address),
Neale Ranns2a3ea492017-04-19 05:24:40 -07001074 0, ip6_sadd_link_local);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001075 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001076
1077 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1078 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001079 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001080 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001081 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082
Dave Barachd7cb1b52016-12-09 09:52:16 -05001083 fib_index =
1084 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001086 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001087 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001088 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1089 }
1090 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001091 {
1092 fei = ip6_fib_table_lookup_exact_match (fib_index,
1093 &h0->target_address,
1094 128);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001095
Neale Ranns3f844d02017-02-18 00:03:54 -08001096 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001097 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001098 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001099 error0 =
1100 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001101 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001102 else
1103 {
1104 if (FIB_ENTRY_FLAG_LOCAL &
1105 fib_entry_get_flags_for_source (fei,
1106 FIB_SOURCE_INTERFACE))
1107 {
1108 /* It's an address that belongs to one of our interfaces
1109 * that's good. */
1110 }
1111 else
1112 if (fib_entry_is_sourced
1113 (fei, FIB_SOURCE_IP6_ND_PROXY))
1114 {
1115 /* The address was added by IPv6 Proxy ND config.
1116 * We should only respond to these if the NS arrived on
1117 * the link that has a matching covering prefix */
1118 }
1119 else
1120 {
1121 error0 =
1122 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1123 }
1124 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001125 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 }
1127
1128 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001129 next0 = (error0 != ICMP6_ERROR_NONE
1130 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1131 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132 else
1133 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001134 next0 = 0;
1135 error0 = error0 == ICMP6_ERROR_NONE ?
1136 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137 }
1138
1139 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1140 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001141 vnet_sw_interface_t *sw_if0;
1142 ethernet_interface_t *eth_if0;
1143 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
Dave Barachd7cb1b52016-12-09 09:52:16 -05001145 /* dst address is either source address or the all-nodes mcast addr */
1146 if (!ip6_sadd_unspecified)
1147 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001149 ip6_set_reserved_multicast_address (&ip0->dst_address,
1150 IP6_MULTICAST_SCOPE_link_local,
1151 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152
1153 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001154 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001155 h0->icmp.type = ICMP6_neighbor_advertisement;
1156
1157 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1158 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001159 eth_if0 =
1160 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161 if (eth_if0 && o0)
1162 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001163 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1164 o0->header.type =
1165 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166 }
1167
1168 h0->advertisement_flags = clib_host_to_net_u32
1169 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1170 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1171
1172 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001173 h0->icmp.checksum =
1174 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1175 &bogus_length);
1176 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177
Dave Barachd7cb1b52016-12-09 09:52:16 -05001178 /* Reuse current MAC header, copy SMAC to DMAC and
1179 * interface MAC to SMAC */
1180 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1181 eth0 = vlib_buffer_get_current (p0);
1182 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1183 if (eth_if0)
1184 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185
Dave Barachd7cb1b52016-12-09 09:52:16 -05001186 /* Setup input and output sw_if_index for packet */
1187 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1188 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1189 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1190 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
1192 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001193 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194
1195 p0->error = error_node->errors[error0];
1196
1197 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1198 to_next, n_left_to_next,
1199 bi0, next0);
1200 }
1201
1202 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1203 }
1204
1205 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001206 vlib_error_count (vm, error_node->node_index,
1207 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1208 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
1210 return frame->n_vectors;
1211}
1212
1213/* for "syslogging" - use elog for now */
1214#define foreach_log_level \
1215 _ (DEBUG, "DEBUG") \
1216 _ (INFO, "INFORMATION") \
1217 _ (NOTICE, "NOTICE") \
1218 _ (WARNING, "WARNING") \
1219 _ (ERR, "ERROR") \
1220 _ (CRIT, "CRITICAL") \
1221 _ (ALERT, "ALERT") \
1222 _ (EMERG, "EMERGENCY")
1223
Dave Barachd7cb1b52016-12-09 09:52:16 -05001224typedef enum
1225{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226#define _(f,s) LOG_##f,
1227 foreach_log_level
1228#undef _
1229} log_level_t;
1230
Dave Barachd7cb1b52016-12-09 09:52:16 -05001231static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232#define _(f,s) s,
1233 foreach_log_level
1234#undef _
1235};
1236
Dave Barachd7cb1b52016-12-09 09:52:16 -05001237static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238
1239static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001240ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241{
1242 /* just use elog for now */
1243 u8 *what;
1244 va_list va;
1245
Dave Barachd7cb1b52016-12-09 09:52:16 -05001246 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1247 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001248
1249 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001250 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251 {
1252 what = va_format (0, fmt, &va);
1253
Dave Barachd7cb1b52016-12-09 09:52:16 -05001254 ELOG_TYPE_DECLARE (e) =
1255 {
1256 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1257 struct
1258 {
1259 u32 s[2];
1260 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001262 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1263 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264 }
1265 va_end (va);
1266 return;
1267}
1268
1269/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001270typedef enum
1271{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1273 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1274 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1275 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1276} icmp6_router_solicitation_or_advertisement_next_t;
1277
1278static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001279icmp6_router_solicitation (vlib_main_t * vm,
1280 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001282 vnet_main_t *vnm = vnet_get_main ();
1283 ip6_main_t *im = &ip6_main;
1284 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001285 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001286 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001288 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289 int bogus_length;
1290
1291 icmp6_neighbor_discovery_option_type_t option_type;
1292
Dave Barachd7cb1b52016-12-09 09:52:16 -05001293 vlib_node_runtime_t *error_node =
1294 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295
1296 from = vlib_frame_vector_args (frame);
1297 n_left_from = n_packets;
1298 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001299
Ed Warnickecb9cada2015-12-08 15:45:58 -07001300 if (node->flags & VLIB_NODE_FLAG_TRACE)
1301 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1302 /* stride */ 1,
1303 sizeof (icmp6_input_trace_t));
1304
1305 /* source may append his LL address */
1306 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1307
1308 while (n_left_from > 0)
1309 {
1310 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001311
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312 while (n_left_from > 0 && n_left_to_next > 0)
1313 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001314 vlib_buffer_t *p0;
1315 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 ip6_radv_t *radv_info = 0;
1317
Dave Barachd7cb1b52016-12-09 09:52:16 -05001318 icmp6_neighbor_discovery_header_t *h0;
1319 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1320
Ed Warnickecb9cada2015-12-08 15:45:58 -07001321 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001322 u32 is_solicitation = 1, is_dropped = 0;
1323 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324
1325 bi0 = to_next[0] = from[0];
1326
1327 from += 1;
1328 to_next += 1;
1329 n_left_from -= 1;
1330 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001331
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332 p0 = vlib_get_buffer (vm, bi0);
1333 ip0 = vlib_buffer_get_current (p0);
1334 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001335 options_len0 =
1336 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1337 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1338 is_link_local =
1339 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340
1341 error0 = ICMP6_ERROR_NONE;
1342 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001343
Ed Warnickecb9cada2015-12-08 15:45:58 -07001344 /* check if solicitation (not from nd_timer node) */
1345 if (ip6_address_is_unspecified (&ip0->dst_address))
1346 is_solicitation = 0;
1347
1348 /* Check that source address is unspecified, link-local or else on-link. */
1349 if (!is_unspecified && !is_link_local)
1350 {
1351 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001352
Dave Barachd7cb1b52016-12-09 09:52:16 -05001353 if (ADJ_INDEX_INVALID != src_adj_index0)
1354 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001355 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001356
Dave Barachd7cb1b52016-12-09 09:52:16 -05001357 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1358 ?
1359 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1360 : error0);
1361 }
1362 else
1363 {
1364 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1365 }
1366 }
1367
Ed Warnickecb9cada2015-12-08 15:45:58 -07001368 /* check for source LL option and process */
1369 o0 = (void *) (h0 + 1);
1370 o0 = ((options_len0 == 8
1371 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001372 && o0->header.n_data_u64s == 1) ? o0 : 0);
1373
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001375 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1376 !is_unspecified && !is_link_local))
1377 {
1378 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1379 if (nm->limit_neighbor_cache_size &&
1380 pool_elts (nm->neighbor_pool) >=
1381 nm->limit_neighbor_cache_size)
1382 unset_random_neighbor_entry ();
1383
1384 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1385 &ip0->src_address,
1386 o0->ethernet_address,
1387 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001388 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001389 }
1390
Ed Warnickecb9cada2015-12-08 15:45:58 -07001391 /* default is to drop */
1392 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001393
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394 if (error0 == ICMP6_ERROR_NONE)
1395 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001396 vnet_sw_interface_t *sw_if0;
1397 ethernet_interface_t *eth_if0;
1398 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001399
1400 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1401 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001402 eth_if0 =
1403 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001404
1405 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001406 error0 =
1407 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1408 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409
1410 if (error0 == ICMP6_ERROR_NONE)
1411 {
1412 u32 ri;
1413
1414 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001415 p0->current_length -=
1416 (options_len0 +
1417 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001418
1419 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001420 vec_validate_init_empty
1421 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422
1423 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1424
Dave Barachd7cb1b52016-12-09 09:52:16 -05001425 if (ri != ~0)
1426 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1427
1428 error0 =
1429 ((!radv_info) ?
1430 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1431 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
1433 if (error0 == ICMP6_ERROR_NONE)
1434 {
1435 f64 now = vlib_time_now (vm);
1436
1437 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001438 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001439 {
Neale Ranns75152282017-01-09 01:00:45 -08001440 if (0 != radv_info->last_radv_time &&
1441 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001442 MIN_DELAY_BETWEEN_RAS)
1443 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001444 else
1445 radv_info->last_radv_time = now;
1446 }
1447
1448 /* send now */
1449 icmp6_router_advertisement_header_t rh;
1450
1451 rh.icmp.type = ICMP6_router_advertisement;
1452 rh.icmp.code = 0;
1453 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001454
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001456 rh.router_lifetime_in_sec =
1457 clib_host_to_net_u16
1458 (radv_info->adv_router_lifetime_in_sec);
1459 rh.
1460 time_in_msec_between_retransmitted_neighbor_solicitations
1461 =
1462 clib_host_to_net_u32 (radv_info->
1463 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1464 rh.neighbor_reachable_time_in_msec =
1465 clib_host_to_net_u32 (radv_info->
1466 adv_neighbor_reachable_time_in_msec);
1467
1468 rh.flags =
1469 (radv_info->adv_managed_flag) ?
1470 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1471 0;
1472 rh.flags |=
1473 ((radv_info->adv_other_flag) ?
1474 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1475 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476
1477
Dave Barachd7cb1b52016-12-09 09:52:16 -05001478 u16 payload_length =
1479 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001480
1481 vlib_buffer_add_data (vm,
1482 p0->free_list_index,
1483 bi0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001484 (void *) &rh,
1485 sizeof
1486 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487
Dave Barachd7cb1b52016-12-09 09:52:16 -05001488 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001490 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1491 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001492
Dave Barachd7cb1b52016-12-09 09:52:16 -05001493 h.header.type =
1494 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001495 h.header.n_data_u64s = 1;
1496
1497 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001498 clib_memcpy (&h.ethernet_address[0],
1499 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500
1501 vlib_buffer_add_data (vm,
1502 p0->free_list_index,
1503 bi0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001504 (void *) &h,
1505 sizeof
1506 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507
Dave Barachd7cb1b52016-12-09 09:52:16 -05001508 payload_length +=
1509 sizeof
1510 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001512
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001514 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515 {
1516 icmp6_neighbor_discovery_mtu_option_t h;
1517
1518 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001519 h.mtu =
1520 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001521 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1522 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001523
1524 payload_length +=
1525 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001526
1527 vlib_buffer_add_data (vm,
1528 p0->free_list_index,
1529 bi0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001530 (void *) &h,
1531 sizeof
1532 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001534
Ed Warnickecb9cada2015-12-08 15:45:58 -07001535 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001536 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001537
Dave Barachd7cb1b52016-12-09 09:52:16 -05001538 /* *INDENT-OFF* */
1539 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1540 ({
1541 if(pr_info->enabled &&
1542 (!pr_info->decrement_lifetime_flag
1543 || (pr_info->pref_lifetime_expires >0)))
1544 {
1545 /* advertise this prefix */
1546 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001547
Dave Barachd7cb1b52016-12-09 09:52:16 -05001548 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1549 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001550
Dave Barachd7cb1b52016-12-09 09:52:16 -05001551 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552
Dave Barachd7cb1b52016-12-09 09:52:16 -05001553 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1554 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555
Dave Barachd7cb1b52016-12-09 09:52:16 -05001556 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1557 {
1558 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1559 h.preferred_time = 0;
1560 }
1561 else
1562 {
1563 if(pr_info->decrement_lifetime_flag)
1564 {
1565 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1566 (pr_info->valid_lifetime_expires - now) : 0;
1567
1568 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1569 (pr_info->pref_lifetime_expires - now) : 0;
1570 }
1571
1572 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1573 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1574 }
1575 h.unused = 0;
1576
1577 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1578
1579 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1580
1581 vlib_buffer_add_data (vm,
1582 p0->free_list_index,
1583 bi0,
1584 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1585
1586 }
1587 }));
1588 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001589
1590 /* add additional options before here */
1591
1592 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001593 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594 {
1595 ip0->dst_address = ip0->src_address;
1596 }
1597 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001598 {
1599 /* target address is all-nodes mcast addr */
1600 ip6_set_reserved_multicast_address
1601 (&ip0->dst_address,
1602 IP6_MULTICAST_SCOPE_link_local,
1603 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001604 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001605
Ed Warnickecb9cada2015-12-08 15:45:58 -07001606 /* source address MUST be the link-local address */
1607 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608
Dave Barachd7cb1b52016-12-09 09:52:16 -05001609 ip0->hop_limit = 255;
1610 ip0->payload_length =
1611 clib_host_to_net_u16 (payload_length);
1612
1613 icmp6_router_advertisement_header_t *rh0 =
1614 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1615 rh0->icmp.checksum =
1616 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1617 &bogus_length);
1618 ASSERT (bogus_length == 0);
1619
Ed Warnickecb9cada2015-12-08 15:45:58 -07001620 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001621 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001622 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001623
1624 if (is_solicitation)
1625 {
1626 ethernet_header_t *eth0;
1627 /* Reuse current MAC header, copy SMAC to DMAC and
1628 * interface MAC to SMAC */
1629 vlib_buffer_reset (p0);
1630 eth0 = vlib_buffer_get_current (p0);
1631 clib_memcpy (eth0->dst_address, eth0->src_address,
1632 6);
1633 clib_memcpy (eth0->src_address, eth_if0->address,
1634 6);
1635 next0 =
1636 is_dropped ? next0 :
1637 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1638 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1639 sw_if_index0;
1640 }
1641 else
1642 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001643 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001644 if (adj_index0 == 0)
1645 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1646 else
1647 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001648 next0 =
1649 is_dropped ? next0 :
1650 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1651 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1652 adj_index0;
1653 }
1654 }
1655 p0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1656
1657 radv_info->n_solicitations_dropped += is_dropped;
1658 radv_info->n_solicitations_rcvd += is_solicitation;
1659
1660 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001661 {
1662 radv_info->n_advertisements_sent++;
1663 n_advertisements_sent++;
1664 }
1665 }
1666 }
1667 }
1668
1669 p0->error = error_node->errors[error0];
1670
Dave Barachd7cb1b52016-12-09 09:52:16 -05001671 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001673
Ed Warnickecb9cada2015-12-08 15:45:58 -07001674 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1675 to_next, n_left_to_next,
1676 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001677
Ed Warnickecb9cada2015-12-08 15:45:58 -07001678 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001679
Ed Warnickecb9cada2015-12-08 15:45:58 -07001680 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1681 }
1682
1683 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001684 vlib_error_count (vm, error_node->node_index,
1685 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1686 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001687
1688 return frame->n_vectors;
1689}
1690
1691 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1692static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001693icmp6_router_advertisement (vlib_main_t * vm,
1694 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001695{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001696 vnet_main_t *vnm = vnet_get_main ();
1697 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001699 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001700 u32 n_left_from, n_left_to_next, next_index;
1701 u32 n_advertisements_rcvd = 0;
1702
Dave Barachd7cb1b52016-12-09 09:52:16 -05001703 vlib_node_runtime_t *error_node =
1704 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001705
1706 from = vlib_frame_vector_args (frame);
1707 n_left_from = n_packets;
1708 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001709
Ed Warnickecb9cada2015-12-08 15:45:58 -07001710 if (node->flags & VLIB_NODE_FLAG_TRACE)
1711 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1712 /* stride */ 1,
1713 sizeof (icmp6_input_trace_t));
1714
1715 while (n_left_from > 0)
1716 {
1717 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001718
Ed Warnickecb9cada2015-12-08 15:45:58 -07001719 while (n_left_from > 0 && n_left_to_next > 0)
1720 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001721 vlib_buffer_t *p0;
1722 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001723 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001724 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001725 u32 bi0, options_len0, sw_if_index0, next0, error0;
1726
1727 bi0 = to_next[0] = from[0];
1728
1729 from += 1;
1730 to_next += 1;
1731 n_left_from -= 1;
1732 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001733
Ed Warnickecb9cada2015-12-08 15:45:58 -07001734 p0 = vlib_get_buffer (vm, bi0);
1735 ip0 = vlib_buffer_get_current (p0);
1736 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001737 options_len0 =
1738 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739
1740 error0 = ICMP6_ERROR_NONE;
1741 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1742
Dave Barachd7cb1b52016-12-09 09:52:16 -05001743 /* Check that source address is link-local */
1744 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001745 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1746
1747 /* default is to drop */
1748 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001749
Ed Warnickecb9cada2015-12-08 15:45:58 -07001750 n_advertisements_rcvd++;
1751
1752 if (error0 == ICMP6_ERROR_NONE)
1753 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001754 vnet_sw_interface_t *sw_if0;
1755 ethernet_interface_t *eth_if0;
1756
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1758 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001759 eth_if0 =
1760 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001761
1762 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001763 error0 =
1764 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1765 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766
1767 if (error0 == ICMP6_ERROR_NONE)
1768 {
1769 u32 ri;
1770
1771 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001772 vec_validate_init_empty
1773 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001774
1775 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1776
Dave Barachd7cb1b52016-12-09 09:52:16 -05001777 if (ri != ~0)
1778 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1779
1780 error0 =
1781 ((!radv_info) ?
1782 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1783 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001784
1785 if (error0 == ICMP6_ERROR_NONE)
1786 {
1787 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001788 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1789 && (h0->current_hop_limit !=
1790 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001791 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001792 ip6_neighbor_syslog (vm, LOG_WARNING,
1793 "our AdvCurHopLimit on %U doesn't agree with %U",
1794 format_vnet_sw_if_index_name,
1795 vnm, sw_if_index0,
1796 format_ip6_address,
1797 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001798 }
1799
Dave Barachd7cb1b52016-12-09 09:52:16 -05001800 if ((h0->flags &
1801 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1802 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001803 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001804 ip6_neighbor_syslog (vm, LOG_WARNING,
1805 "our AdvManagedFlag on %U doesn't agree with %U",
1806 format_vnet_sw_if_index_name,
1807 vnm, sw_if_index0,
1808 format_ip6_address,
1809 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001810 }
1811
Dave Barachd7cb1b52016-12-09 09:52:16 -05001812 if ((h0->flags &
1813 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1814 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001815 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001816 ip6_neighbor_syslog (vm, LOG_WARNING,
1817 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1818 format_vnet_sw_if_index_name,
1819 vnm, sw_if_index0,
1820 format_ip6_address,
1821 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001822 }
1823
Dave Barachd7cb1b52016-12-09 09:52:16 -05001824 if ((h0->
1825 time_in_msec_between_retransmitted_neighbor_solicitations
1826 && radv_info->
1827 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1828 && (h0->
1829 time_in_msec_between_retransmitted_neighbor_solicitations
1830 !=
1831 clib_host_to_net_u32 (radv_info->
1832 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001834 ip6_neighbor_syslog (vm, LOG_WARNING,
1835 "our AdvRetransTimer on %U doesn't agree with %U",
1836 format_vnet_sw_if_index_name,
1837 vnm, sw_if_index0,
1838 format_ip6_address,
1839 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840 }
1841
Dave Barachd7cb1b52016-12-09 09:52:16 -05001842 if ((h0->neighbor_reachable_time_in_msec &&
1843 radv_info->adv_neighbor_reachable_time_in_msec) &&
1844 (h0->neighbor_reachable_time_in_msec !=
1845 clib_host_to_net_u32
1846 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001847 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001848 ip6_neighbor_syslog (vm, LOG_WARNING,
1849 "our AdvReachableTime on %U doesn't agree with %U",
1850 format_vnet_sw_if_index_name,
1851 vnm, sw_if_index0,
1852 format_ip6_address,
1853 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001854 }
1855
1856 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001857 u8 *opt_hdr = (u8 *) (h0 + 1);
1858 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001859 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001860 icmp6_neighbor_discovery_option_header_t *o0 =
1861 (icmp6_neighbor_discovery_option_header_t *)
1862 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001863 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001864 icmp6_neighbor_discovery_option_type_t option_type =
1865 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001866
Dave Barachd7cb1b52016-12-09 09:52:16 -05001867 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001868 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001869 ip6_neighbor_syslog (vm, LOG_ERR,
1870 "malformed RA packet on %U from %U",
1871 format_vnet_sw_if_index_name,
1872 vnm, sw_if_index0,
1873 format_ip6_address,
1874 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001875 break;
1876 }
1877
Dave Barachd7cb1b52016-12-09 09:52:16 -05001878 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001879 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001880 ip6_neighbor_syslog (vm, LOG_ERR,
1881 " zero length option in RA on %U from %U",
1882 format_vnet_sw_if_index_name,
1883 vnm, sw_if_index0,
1884 format_ip6_address,
1885 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 break;
1887 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001888 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001889 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001890 ip6_neighbor_syslog (vm, LOG_ERR,
1891 "option length in RA packet greater than total length on %U from %U",
1892 format_vnet_sw_if_index_name,
1893 vnm, sw_if_index0,
1894 format_ip6_address,
1895 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001896 break;
1897 }
1898
1899 options_len0 -= opt_len;
1900 opt_hdr += opt_len;
1901
Dave Barachd7cb1b52016-12-09 09:52:16 -05001902 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001903 {
1904 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05001905 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001906 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001907 (icmp6_neighbor_discovery_mtu_option_t
1908 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001909
Dave Barachd7cb1b52016-12-09 09:52:16 -05001910 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001911 break;
1912
Dave Barachd7cb1b52016-12-09 09:52:16 -05001913 if ((h->mtu && radv_info->adv_link_mtu) &&
1914 (h->mtu !=
1915 clib_host_to_net_u32
1916 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001918 ip6_neighbor_syslog (vm, LOG_WARNING,
1919 "our AdvLinkMTU on %U doesn't agree with %U",
1920 format_vnet_sw_if_index_name,
1921 vnm, sw_if_index0,
1922 format_ip6_address,
1923 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001924 }
1925 }
1926 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001927
Ed Warnickecb9cada2015-12-08 15:45:58 -07001928 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1929 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001930 icmp6_neighbor_discovery_prefix_information_option_t
1931 * h =
1932 (icmp6_neighbor_discovery_prefix_information_option_t
1933 *) (o0);
1934
Ed Warnickecb9cada2015-12-08 15:45:58 -07001935 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001936 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001937 u32 preferred, valid;
1938
Dave Barachd7cb1b52016-12-09 09:52:16 -05001939 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001940 break;
1941
Dave Barachd7cb1b52016-12-09 09:52:16 -05001942 preferred =
1943 clib_net_to_host_u32 (h->preferred_time);
1944 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001945
1946 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001947 /* *INDENT-OFF* */
1948 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1949 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001950
Dave Barachd7cb1b52016-12-09 09:52:16 -05001951 ip6_address_t mask;
1952 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1953
1954 if(pr_info->enabled &&
1955 (pr_info->prefix_len == h->dst_address_length) &&
1956 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1957 {
1958 /* found it */
1959 if(!pr_info->decrement_lifetime_flag &&
1960 valid != pr_info->adv_valid_lifetime_in_secs)
1961 {
1962 ip6_neighbor_syslog(vm, LOG_WARNING,
1963 "our ADV validlifetime on %U for %U does not agree with %U",
1964 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1965 format_ip6_address, &h->dst_address);
1966 }
1967 if(!pr_info->decrement_lifetime_flag &&
1968 preferred != pr_info->adv_pref_lifetime_in_secs)
1969 {
1970 ip6_neighbor_syslog(vm, LOG_WARNING,
1971 "our ADV preferredlifetime on %U for %U does not agree with %U",
1972 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1973 format_ip6_address, &h->dst_address);
1974 }
1975 }
1976 break;
1977 }));
1978 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001979 break;
1980 }
1981 default:
1982 /* skip this one */
1983 break;
1984 }
1985 }
1986 }
1987 }
1988 }
1989
1990 p0->error = error_node->errors[error0];
1991
Dave Barachd7cb1b52016-12-09 09:52:16 -05001992 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001993 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001994
Ed Warnickecb9cada2015-12-08 15:45:58 -07001995 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1996 to_next, n_left_to_next,
1997 bi0, next0);
1998 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001999
Ed Warnickecb9cada2015-12-08 15:45:58 -07002000 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2001 }
2002
2003 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002004 vlib_error_count (vm, error_node->node_index,
2005 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2006 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002007
2008 return frame->n_vectors;
2009}
2010
Neale Ranns87df12d2017-02-18 08:16:41 -08002011/**
2012 * @brief Add a multicast Address to the advertised MLD set
2013 */
2014static void
2015ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2016{
2017 ip6_mldp_group_t *mcast_group_info;
2018 uword *p;
2019
2020 /* lookup mldp info for this interface */
2021 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2022 mcast_group_info =
2023 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2024
2025 /* add address */
2026 if (!mcast_group_info)
2027 {
2028 /* add */
2029 u32 mi;
2030 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2031
2032 mi = mcast_group_info - radv_info->mldp_group_pool;
2033 mhash_set (&radv_info->address_to_mldp_index, &addr, mi, /* old_value */
2034 0);
2035
2036 mcast_group_info->type = 4;
2037 mcast_group_info->mcast_source_address_pool = 0;
2038 mcast_group_info->num_sources = 0;
2039 clib_memcpy (&mcast_group_info->mcast_address, &addr,
2040 sizeof (ip6_address_t));
2041 }
2042}
2043
2044/**
2045 * @brief Delete a multicast Address from the advertised MLD set
2046 */
2047static void
2048ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2049{
2050 ip6_mldp_group_t *mcast_group_info;
2051 uword *p;
2052
2053 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2054 mcast_group_info =
2055 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2056
2057 if (mcast_group_info)
2058 {
2059 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2060 /* old_value */ 0);
2061 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2062 }
2063}
2064
2065/**
2066 * @brief Add a multicast Address to the advertised MLD set
2067 */
2068static void
2069ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2070 ip6_multicast_address_scope_t scope,
2071 ip6_multicast_link_local_group_id_t group)
2072{
2073 ip6_address_t addr;
2074
2075 ip6_set_reserved_multicast_address (&addr, scope, group);
2076
2077 ip6_neighbor_add_mld_prefix (a, &addr);
2078}
2079
2080/**
2081 * @brief create and initialize router advertisement parameters with default
2082 * values for this intfc
2083 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002084static u32
2085ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002086 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002087{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002088 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2089 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002090 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002091 vnet_sw_interface_t *sw_if0;
2092 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002093
2094 /* lookup radv container - ethernet interfaces only */
2095 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002096 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002097 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2098
Dave Barachd7cb1b52016-12-09 09:52:16 -05002099 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002100 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002101
2102 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2103 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002104 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2105
Dave Barachd7cb1b52016-12-09 09:52:16 -05002106 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002107 {
2108 a = pool_elt_at_index (nm->if_radv_pool, ri);
2109
Dave Barachd7cb1b52016-12-09 09:52:16 -05002110 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002111 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002112 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002113 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002114
Neale Ranns32e1c012016-11-22 17:07:28 +00002115 /* release the lock on the interface's mcast adj */
2116 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002117
Neale Ranns87df12d2017-02-18 08:16:41 -08002118 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002119 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002120 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002121 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002122 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002123 }));
2124 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002125 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002126 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002127 }));
2128 /* *INDENT-ON* */
2129
Neale Ranns87df12d2017-02-18 08:16:41 -08002130 pool_free (a->mldp_group_pool);
2131 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002132
Neale Ranns87df12d2017-02-18 08:16:41 -08002133 mhash_free (&a->address_to_prefix_index);
2134 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002135
2136 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002137 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2138 ri = ~0;
2139 }
2140 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002141 else
2142 {
2143 if (is_add)
2144 {
2145 vnet_hw_interface_t *hw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002146
Dave Barachd7cb1b52016-12-09 09:52:16 -05002147 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2148
2149 pool_get (nm->if_radv_pool, a);
2150
2151 ri = a - nm->if_radv_pool;
2152 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2153
2154 /* initialize default values (most of which are zero) */
2155 memset (a, 0, sizeof (a[0]));
2156
2157 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002158 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2159 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2160 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2161 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2162
Neale Ranns87df12d2017-02-18 08:16:41 -08002163 /* send ll address source address option */
2164 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002165
2166 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2167 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2168 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2169 a->seed = (u32) clib_cpu_time_now ();
2170 (void) random_u32 (&a->seed);
2171 a->randomizer = clib_cpu_time_now ();
2172 (void) random_u64 (&a->randomizer);
2173
2174 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2175 a->initial_adverts_sent = a->initial_adverts_count - 1;
2176 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2177
2178 /* deafult is to send */
2179 a->send_radv = 1;
2180
2181 /* fill in radv_info for this interface that will be needed later */
2182 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2183
2184 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2185
2186 /* fill in default link-local address (this may be overridden) */
2187 ip6_link_local_address_from_ethernet_address
2188 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002189
2190 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2191 sizeof (ip6_address_t));
2192 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2193 sizeof (ip6_address_t));
2194
Neale Ranns32e1c012016-11-22 17:07:28 +00002195 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2196 VNET_LINK_IP6,
2197 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002198
2199 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002200 ip6_neighbor_add_mld_grp (a,
2201 IP6_MULTICAST_SCOPE_link_local,
2202 IP6_MULTICAST_GROUP_ID_all_hosts);
2203 ip6_neighbor_add_mld_grp (a,
2204 IP6_MULTICAST_SCOPE_link_local,
2205 IP6_MULTICAST_GROUP_ID_all_routers);
2206 ip6_neighbor_add_mld_grp (a,
2207 IP6_MULTICAST_SCOPE_link_local,
2208 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002209 }
2210 }
2211 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002212}
2213
2214/* send an mldpv2 report */
2215static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002216ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002217{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002218 vnet_main_t *vnm = vnet_get_main ();
2219 vlib_main_t *vm = vnm->vlib_main;
2220 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2221 vnet_sw_interface_t *sw_if0;
2222 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002223 u32 ri;
2224 int bogus_length;
2225
Dave Barachd7cb1b52016-12-09 09:52:16 -05002226 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002227 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002228 vlib_buffer_t *b0;
2229 ip6_header_t *ip0;
2230 u32 *to_next;
2231 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002232 u32 bo0;
2233 u32 n_to_alloc = 1;
2234 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002235
Ed Warnickecb9cada2015-12-08 15:45:58 -07002236 icmp6_multicast_listener_report_header_t *rh0;
2237 icmp6_multicast_listener_report_packet_t *rp0;
2238
2239 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2240 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2241 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2242
2243 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2244 return;
2245
2246 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002247 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2248 ~0);
2249
Ed Warnickecb9cada2015-12-08 15:45:58 -07002250 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002251
2252 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002253 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002254
Ed Warnickecb9cada2015-12-08 15:45:58 -07002255 /* send report now - build a mldpv2 report packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002256 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2257 &bo0,
2258 n_to_alloc,
2259 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2260 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002261 {
2262 clib_warning ("buffer allocation failure");
2263 return;
2264 }
2265
2266 b0 = vlib_get_buffer (vm, bo0);
2267
2268 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002269 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002270
Dave Barachd7cb1b52016-12-09 09:52:16 -05002271 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272
2273 b0->error = ICMP6_ERROR_NONE;
2274
2275 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002276 ip0 = (ip6_header_t *) & rp0->ip;
2277 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002278
Dave Barachd7cb1b52016-12-09 09:52:16 -05002279 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2280
2281 ip0->ip_version_traffic_class_and_flow_label =
2282 clib_host_to_net_u32 (0x6 << 28);
2283
2284 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002285 /* for DEBUG - vnet driver won't seem to emit router alerts */
2286 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2287 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002288
Ed Warnickecb9cada2015-12-08 15:45:58 -07002289 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002290
Ed Warnickecb9cada2015-12-08 15:45:58 -07002291 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002292 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2293 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002294
2295 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002296 ip6_set_reserved_multicast_address (&ip0->dst_address,
2297 IP6_MULTICAST_SCOPE_link_local,
2298 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2299
Ed Warnickecb9cada2015-12-08 15:45:58 -07002300 /* add reports here */
2301 ip6_mldp_group_t *m;
2302 int num_addr_records = 0;
2303 icmp6_multicast_address_record_t rr;
2304
2305 /* fill in the hop-by-hop extension header (router alert) info */
2306 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2307 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002308
Ed Warnickecb9cada2015-12-08 15:45:58 -07002309 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2310 rh0->alert.len = 2;
2311 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002312
Ed Warnickecb9cada2015-12-08 15:45:58 -07002313 rh0->pad.type = 1;
2314 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002315
Ed Warnickecb9cada2015-12-08 15:45:58 -07002316 rh0->icmp.checksum = 0;
2317
Dave Barachd7cb1b52016-12-09 09:52:16 -05002318 /* *INDENT-OFF* */
2319 pool_foreach (m, radv_info->mldp_group_pool,
2320 ({
2321 rr.type = m->type;
2322 rr.aux_data_len_u32s = 0;
2323 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2324 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002325
Dave Barachd7cb1b52016-12-09 09:52:16 -05002326 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002327
Dave Barachd7cb1b52016-12-09 09:52:16 -05002328 vlib_buffer_add_data
2329 (vm, b0->free_list_index, bo0,
2330 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002331
Dave Barachd7cb1b52016-12-09 09:52:16 -05002332 payload_length += sizeof( icmp6_multicast_address_record_t);
2333 }));
2334 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002335
2336 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002337 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2338
Ed Warnickecb9cada2015-12-08 15:45:58 -07002339 /* update lengths */
2340 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2341
Dave Barachd7cb1b52016-12-09 09:52:16 -05002342 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2343 &bogus_length);
2344 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002345
Dave Barachd7cb1b52016-12-09 09:52:16 -05002346 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002347 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002348 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002349 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002350 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002351 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002352
Neale Ranns32e1c012016-11-22 17:07:28 +00002353 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Neale Rannsf06aea52016-11-29 06:51:37 -08002354 b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002355
Neale Ranns32e1c012016-11-22 17:07:28 +00002356 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002357
Ed Warnickecb9cada2015-12-08 15:45:58 -07002358 f = vlib_get_frame_to_node (vm, node->index);
2359 to_next = vlib_frame_vector_args (f);
2360 to_next[0] = bo0;
2361 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002362
Ed Warnickecb9cada2015-12-08 15:45:58 -07002363 vlib_put_frame_to_node (vm, node->index, f);
2364 return;
2365}
2366
Dave Barachd7cb1b52016-12-09 09:52:16 -05002367/* *INDENT-OFF* */
2368VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2369{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002370 .function = icmp6_router_solicitation,
2371 .name = "icmp6-router-solicitation",
2372
2373 .vector_size = sizeof (u32),
2374
2375 .format_trace = format_icmp6_input_trace,
2376
2377 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2378 .next_nodes = {
2379 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002380 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002381 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2382 },
2383};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002384/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002385
2386/* send a RA or update the timer info etc.. */
2387static uword
2388ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002389 vlib_node_runtime_t * node,
2390 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002391{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002392 vnet_main_t *vnm = vnet_get_main ();
2393 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2394 ip6_radv_t *radv_info;
2395 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002396 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002397 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002398 u32 *to_next = 0;
2399 u32 bo0;
2400 icmp6_router_solicitation_header_t *h0;
2401 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002402 f64 now = vlib_time_now (vm);
2403
2404 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002405 /* *INDENT-OFF* */
2406 pool_foreach (radv_info, nm->if_radv_pool,
2407 ({
2408 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2409 {
2410 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2411 radv_info->next_multicast_time = now;
2412 radv_info->last_multicast_time = now;
2413 radv_info->last_radv_time = 0;
2414 radv_info->all_routers_mcast = 0;
2415 continue;
2416 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002417
Dave Barachd7cb1b52016-12-09 09:52:16 -05002418 /* Make sure that we've joined the all-routers multicast group */
2419 if(!radv_info->all_routers_mcast)
2420 {
2421 /* send MDLP_REPORT_EVENT message */
2422 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2423 radv_info->all_routers_mcast = 1;
2424 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002425
Dave Barachd7cb1b52016-12-09 09:52:16 -05002426 /* is it time to send a multicast RA on this interface? */
2427 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2428 {
2429 u32 n_to_alloc = 1;
2430 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002431
Dave Barachd7cb1b52016-12-09 09:52:16 -05002432 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2433 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002434
Dave Barachd7cb1b52016-12-09 09:52:16 -05002435 /* multicast send - compute next multicast send time */
2436 if( radv_info->initial_adverts_sent > 0)
2437 {
2438 radv_info->initial_adverts_sent--;
2439 if(rfn > radv_info-> initial_adverts_interval)
2440 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002441
Dave Barachd7cb1b52016-12-09 09:52:16 -05002442 /* check to see if we are ceasing to send */
2443 if( radv_info->initial_adverts_sent == 0)
2444 if(radv_info->cease_radv)
2445 radv_info->send_radv = 0;
2446 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002447
Dave Barachd7cb1b52016-12-09 09:52:16 -05002448 radv_info->next_multicast_time = rfn + now;
2449 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002450
Dave Barachd7cb1b52016-12-09 09:52:16 -05002451 /* send advert now - build a "solicted" router advert with unspecified source address */
2452 n_allocated = vlib_buffer_alloc_from_free_list
2453 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002454
Dave Barachd7cb1b52016-12-09 09:52:16 -05002455 if (PREDICT_FALSE(n_allocated == 0))
2456 {
2457 clib_warning ("buffer allocation failure");
2458 continue;
2459 }
2460 b0 = vlib_get_buffer (vm, bo0);
2461 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2462 b0->error = ICMP6_ERROR_NONE;
2463 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2464
2465 h0 = vlib_buffer_get_current (b0);
2466
2467 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2468
2469 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2470 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2471 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2472 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2473 h0->ip.hop_limit = 255;
2474
2475 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2476 h0->ip.src_address.as_u64[0] = 0;
2477 h0->ip.src_address.as_u64[1] = 0;
2478
2479 h0->ip.dst_address.as_u64[0] = 0;
2480 h0->ip.dst_address.as_u64[1] = 0;
2481
2482 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2483
2484 if (PREDICT_FALSE(f == 0))
2485 {
2486 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2487 to_next = vlib_frame_vector_args (f);
2488 n_left_to_next = VLIB_FRAME_SIZE;
2489 n_this_frame = 0;
2490 }
2491
2492 n_this_frame++;
2493 n_left_to_next--;
2494 to_next[0] = bo0;
2495 to_next += 1;
2496
2497 if (PREDICT_FALSE(n_left_to_next == 0))
2498 {
2499 f->n_vectors = n_this_frame;
2500 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2501 f = 0;
2502 }
2503 }
2504 }));
2505 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002506
2507 if (f)
2508 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002509 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002510 f->n_vectors = n_this_frame;
2511 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2512 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002513 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002514}
2515
2516static uword
2517ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2518 vlib_node_runtime_t * node,
2519 vlib_frame_t * frame)
2520{
2521 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002522 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002523
2524 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002525
Ed Warnickecb9cada2015-12-08 15:45:58 -07002526 while (1)
2527 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002528 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07002529
Dave Barachd7cb1b52016-12-09 09:52:16 -05002530 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002531
Dave Barachd7cb1b52016-12-09 09:52:16 -05002532 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002533 {
2534 /* No events found: timer expired. */
2535 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002536 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002537 }
2538 else
2539 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002540 switch (event_type)
2541 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002542
Dave Barachd7cb1b52016-12-09 09:52:16 -05002543 case ICMP6_ND_EVENT_INIT:
2544 break;
2545
2546 case ~0:
2547 break;
2548
2549 default:
2550 ASSERT (0);
2551 }
2552
Ed Warnickecb9cada2015-12-08 15:45:58 -07002553 if (event_data)
2554 _vec_len (event_data) = 0;
2555 }
2556 }
2557 return frame->n_vectors;
2558}
2559
Dave Barachd7cb1b52016-12-09 09:52:16 -05002560/* *INDENT-OFF* */
2561VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2562{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002563 .function = icmp6_router_advertisement,
2564 .name = "icmp6-router-advertisement",
2565
2566 .vector_size = sizeof (u32),
2567
2568 .format_trace = format_icmp6_input_trace,
2569
2570 .n_next_nodes = 1,
2571 .next_nodes = {
2572 [0] = "error-drop",
2573 },
2574};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002575/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002576
2577vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2578
2579 .function = ip6_icmp_neighbor_discovery_event_process,
2580 .name = "ip6-icmp-neighbor-discovery-event-process",
2581 .type = VLIB_NODE_TYPE_PROCESS,
2582};
2583
2584static uword
2585icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002586 vlib_node_runtime_t * node, vlib_frame_t * frame)
2587{
2588 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2589 /* is_solicitation */
2590 1);
2591}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002592
2593static uword
2594icmp6_neighbor_advertisement (vlib_main_t * vm,
2595 vlib_node_runtime_t * node,
2596 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002597{
2598 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2599 /* is_solicitation */
2600 0);
2601}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002602
Dave Barachd7cb1b52016-12-09 09:52:16 -05002603/* *INDENT-OFF* */
2604VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2605{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002606 .function = icmp6_neighbor_solicitation,
2607 .name = "icmp6-neighbor-solicitation",
2608
2609 .vector_size = sizeof (u32),
2610
2611 .format_trace = format_icmp6_input_trace,
2612
2613 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2614 .next_nodes = {
2615 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2616 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2617 },
2618};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002619/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002620
Dave Barachd7cb1b52016-12-09 09:52:16 -05002621/* *INDENT-OFF* */
2622VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2623{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002624 .function = icmp6_neighbor_advertisement,
2625 .name = "icmp6-neighbor-advertisement",
2626
2627 .vector_size = sizeof (u32),
2628
2629 .format_trace = format_icmp6_input_trace,
2630
2631 .n_next_nodes = 1,
2632 .next_nodes = {
2633 [0] = "error-drop",
2634 },
2635};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002636/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002637
Dave Barachd7cb1b52016-12-09 09:52:16 -05002638/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002639int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002640ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2641 u8 suppress, u8 managed, u8 other,
2642 u8 ll_option, u8 send_unicast, u8 cease,
2643 u8 use_lifetime, u32 lifetime,
2644 u32 initial_count, u32 initial_interval,
2645 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002646{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002647 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2648 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002649 u32 ri;
2650
2651 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002652 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2653 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002654 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002655 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002656
Dave Barachd7cb1b52016-12-09 09:52:16 -05002657 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002658 {
2659
Dave Barachd7cb1b52016-12-09 09:52:16 -05002660 ip6_radv_t *radv_info;
2661 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002662
Dave Barachd7cb1b52016-12-09 09:52:16 -05002663 if ((max_interval != 0) && (min_interval == 0))
2664 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002665
Dave Barachd7cb1b52016-12-09 09:52:16 -05002666 max_interval =
2667 (max_interval !=
2668 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2669 radv_info->max_radv_interval;
2670 min_interval =
2671 (min_interval !=
2672 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2673 radv_info->min_radv_interval;
2674 lifetime =
2675 (use_lifetime !=
2676 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2677 radv_info->adv_router_lifetime_in_sec;
2678
2679 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002680 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002681 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002682 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002683
2684 if (lifetime <= max_interval)
2685 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002686 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002687
2688 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002689 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002690 if ((min_interval > .75 * max_interval) || (min_interval < 3))
2691 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002692 }
2693
Dave Barachd7cb1b52016-12-09 09:52:16 -05002694 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2695 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2696 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002697
Dave Barachd7cb1b52016-12-09 09:52:16 -05002698 /*
2699 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2700 if "flag" is clear don't change corresponding value
2701 */
2702 radv_info->send_radv =
2703 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2704 radv_info->adv_managed_flag =
2705 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2706 radv_info->adv_other_flag =
2707 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2708 radv_info->adv_link_layer_address =
2709 (ll_option !=
2710 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2711 radv_info->send_unicast =
2712 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2713 radv_info->cease_radv =
2714 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2715
2716 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002717 radv_info->max_radv_interval = max_interval;
2718 radv_info->adv_router_lifetime_in_sec = lifetime;
2719
Dave Barachd7cb1b52016-12-09 09:52:16 -05002720 radv_info->initial_adverts_count =
2721 (initial_count !=
2722 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2723 radv_info->initial_adverts_count;
2724 radv_info->initial_adverts_interval =
2725 (initial_interval !=
2726 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2727 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002728
2729 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002730 if ((cease != 0) && (is_no))
2731 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002732
Dave Barachd7cb1b52016-12-09 09:52:16 -05002733 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2734 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002735 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002736 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002737 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002738 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002739}
2740
2741int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002742ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2743 ip6_address_t * prefix_addr, u8 prefix_len,
2744 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2745 u8 no_advertise, u8 off_link, u8 no_autoconfig,
2746 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002747{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002748 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002749 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002750
Ed Warnickecb9cada2015-12-08 15:45:58 -07002751 u32 ri;
2752
2753 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002754 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2755 ~0);
2756
Ed Warnickecb9cada2015-12-08 15:45:58 -07002757 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2758
2759 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002760
2761 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002762 {
2763 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002764 ip6_radv_t *radv_info;
2765 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002766
2767 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002768 ip6_radv_prefix_t *prefix;
2769
Ed Warnickecb9cada2015-12-08 15:45:58 -07002770 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002771 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2772
Ed Warnickecb9cada2015-12-08 15:45:58 -07002773 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2774
Dave Barachd7cb1b52016-12-09 09:52:16 -05002775 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002776 {
2777 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002778 if (!prefix)
2779 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2780
2781 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002782 return VNET_API_ERROR_INVALID_VALUE_2;
2783
Dave Barachd7cb1b52016-12-09 09:52:16 -05002784 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002785 /* do specific delete processing here before returning */
2786 /* try to remove from routing table */
2787
Dave Barachd7cb1b52016-12-09 09:52:16 -05002788 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2789 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002790 pool_put (radv_info->adv_prefixes_pool, prefix);
2791
Dave Barachd7cb1b52016-12-09 09:52:16 -05002792 radv_info->initial_adverts_sent =
2793 radv_info->initial_adverts_count - 1;
2794 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002795 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002796 radv_info->last_radv_time = 0;
2797 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798 }
2799
2800 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002801 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002802 {
2803 /* add */
2804 u32 pi;
2805 pool_get (radv_info->adv_prefixes_pool, prefix);
2806 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002807 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2808 /* old_value */ 0);
2809
2810 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2811
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002813 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2814
Ed Warnickecb9cada2015-12-08 15:45:58 -07002815 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002816 prefix->adv_on_link_flag = 1; /* L bit set */
2817 prefix->adv_autonomous_flag = 1; /* A bit set */
2818 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002819 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2820 prefix->enabled = 1;
2821 prefix->decrement_lifetime_flag = 1;
2822 prefix->deprecated_prefix_flag = 1;
2823
Dave Barachd7cb1b52016-12-09 09:52:16 -05002824 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002825 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002826 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827 /* insert prefix into routing table as a connected prefix */
2828 }
2829
Dave Barachd7cb1b52016-12-09 09:52:16 -05002830 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831 goto restart;
2832 }
2833 else
2834 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002835
2836 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002837 return VNET_API_ERROR_INVALID_VALUE_2;
2838
Dave Barachd7cb1b52016-12-09 09:52:16 -05002839 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002840 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002841 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002842 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002843 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002844 }
2845
Dave Barachd7cb1b52016-12-09 09:52:16 -05002846 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002847 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002848 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002849 prefix->adv_pref_lifetime_in_secs = ~0;
2850 prefix->decrement_lifetime_flag = 0;
2851 }
2852 else
2853 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002854 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2855 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002856 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002857
Ed Warnickecb9cada2015-12-08 15:45:58 -07002858 /* copy remaining */
2859 prefix->enabled = !(no_advertise != 0);
2860 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2861 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2862
Dave Barachd7cb1b52016-12-09 09:52:16 -05002863 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002864 /* restart */
2865 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002866 prefix->valid_lifetime_expires =
2867 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002868 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002869
2870 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2871 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002873 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002875 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876}
2877
2878clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05002879ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2880 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002881{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002882 vnet_main_t *vnm = vnet_get_main ();
2883 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2884 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002885 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002886 u8 suppress = 0, managed = 0, other = 0;
2887 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002888 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002889 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2890 0, ra_initial_interval = 0;
2891 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002892
Dave Barachd7cb1b52016-12-09 09:52:16 -05002893 unformat_input_t _line_input, *line_input = &_line_input;
2894 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002895
2896 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002897 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002898 ip6_address_t ip6_addr;
2899 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002900
Ed Warnickecb9cada2015-12-08 15:45:58 -07002901
2902 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002903 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002904 return 0;
2905
2906 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002907 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002908 {
2909
Dave Barachd7cb1b52016-12-09 09:52:16 -05002910 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002911 unformat_vnet_sw_interface, vnm, &sw_if_index))
2912 {
2913 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002914 ethernet_interface_t *eth_if0 = 0;
2915
Ed Warnickecb9cada2015-12-08 15:45:58 -07002916 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002917 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2918 eth_if0 =
2919 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2920
2921 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002922 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002923 error =
2924 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925 goto done;
2926 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002927
Ed Warnickecb9cada2015-12-08 15:45:58 -07002928 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002929 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2930 sw_if_index, ~0);
2931
Ed Warnickecb9cada2015-12-08 15:45:58 -07002932 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002933
2934 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002935 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002936 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002937 }
2938 else
2939 {
2940 error = clib_error_return (0, "unknown interface %U'",
2941 format_unformat_error, line_input);
2942 goto done;
2943 }
2944 }
2945 else
2946 {
2947 error = clib_error_return (0, "invalid interface name %U'",
2948 format_unformat_error, line_input);
2949 goto done;
2950 }
2951 }
2952
2953 /* get the rest of the command */
2954 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2955 {
2956 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05002957 is_no = 1;
2958 else if (unformat (line_input, "prefix %U/%d",
2959 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002960 {
2961 add_radv_info = 0;
2962 break;
2963 }
2964 else if (unformat (line_input, "ra-managed-config-flag"))
2965 {
2966 managed = 1;
2967 break;
2968 }
2969 else if (unformat (line_input, "ra-other-config-flag"))
2970 {
2971 other = 1;
2972 break;
2973 }
Chris Luke33879c92016-06-28 19:54:21 -04002974 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05002975 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002976 {
Chris Luke33879c92016-06-28 19:54:21 -04002977 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002978 break;
2979 }
Chris Luke33879c92016-06-28 19:54:21 -04002980 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05002981 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002982 {
Chris Luke33879c92016-06-28 19:54:21 -04002983 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002984 break;
2985 }
2986 else if (unformat (line_input, "ra-send-unicast"))
2987 {
2988 send_unicast = 1;
2989 break;
2990 }
2991 else if (unformat (line_input, "ra-lifetime"))
2992 {
2993 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05002994 {
2995 error = unformat_parse_error (line_input);
2996 goto done;
2997 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002998 use_lifetime = 1;
2999 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003000 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003001 else if (unformat (line_input, "ra-initial"))
3002 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003003 if (!unformat
3004 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003005 {
3006 error = unformat_parse_error (line_input);
3007 goto done;
3008 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003009 break;
3010 }
3011 else if (unformat (line_input, "ra-interval"))
3012 {
3013 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003014 {
3015 error = unformat_parse_error (line_input);
3016 goto done;
3017 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003018
3019 if (!unformat (line_input, "%d", &ra_min_interval))
3020 ra_min_interval = 0;
3021 break;
3022 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003023 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003024 {
3025 cease = 1;
3026 break;
3027 }
3028 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003029 {
3030 error = unformat_parse_error (line_input);
3031 goto done;
3032 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003033 }
3034
Dave Barachd7cb1b52016-12-09 09:52:16 -05003035 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003036 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003037 ip6_neighbor_ra_config (vm, sw_if_index,
3038 suppress, managed, other,
3039 suppress_ll_option, send_unicast, cease,
3040 use_lifetime, ra_lifetime,
3041 ra_initial_count, ra_initial_interval,
3042 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003043 }
3044 else
3045 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003046 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003047 u32 pref_lifetime_in_secs = 0;
3048 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003049 u8 no_advertise = 0;
3050 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003051 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003052 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003053
3054 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003055 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003056 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003057 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003058 {
3059 use_prefix_default_values = 1;
3060 break;
3061 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003062 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003063 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003064 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003065 pref_lifetime_in_secs = ~0;
3066 break;
3067 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003068 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3069 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003070 break;
3071 else
3072 break;
3073 }
3074
3075
3076 /* get the rest of the command */
3077 while (!use_prefix_default_values &&
3078 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3079 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003080 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003081 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003082 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003083 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003084 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003085 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003086 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003087 no_onlink = 1;
3088 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003089 {
3090 error = unformat_parse_error (line_input);
3091 goto done;
3092 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003093 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003094
3095 ip6_neighbor_ra_prefix (vm, sw_if_index,
3096 &ip6_addr, addr_len,
3097 use_prefix_default_values,
3098 valid_lifetime_in_secs,
3099 pref_lifetime_in_secs,
3100 no_advertise,
3101 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003102 }
3103
Billy McFalla9a20e72017-02-15 11:39:12 -05003104done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003106
Ed Warnickecb9cada2015-12-08 15:45:58 -07003107 return error;
3108}
3109
Chris Lukebfd32fd2016-06-24 20:38:06 -04003110static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003111ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003112{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003113 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003114 u32 i;
3115
3116 for (i = 0; i < vec_len (addrs); i++)
3117 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003118 ip_interface_address_t *a =
3119 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3120 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003121
3122 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003123 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003124 }
3125}
3126
Ed Warnickecb9cada2015-12-08 15:45:58 -07003127static clib_error_t *
3128show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003129 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003130{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003131 vnet_main_t *vnm = vnet_get_main ();
3132 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3133 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003134 u32 sw_if_index;
3135
3136 sw_if_index = ~0;
3137
Dave Barachd7cb1b52016-12-09 09:52:16 -05003138 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003139 {
3140 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003141
Dave Barachd7cb1b52016-12-09 09:52:16 -05003142 /* look up the radv_t information for this interface */
3143 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3144 sw_if_index, ~0);
3145
3146 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3147
3148 if (ri != ~0)
3149 {
3150 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3151 ip6_radv_t *radv_info;
3152 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3153
3154 vlib_cli_output (vm, "%U is admin %s\n",
3155 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003156 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003157 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3158 "up" : "down"));
3159
Chris Lukebfd32fd2016-06-24 20:38:06 -04003160 u32 ai;
3161 u32 *link_scope = 0, *global_scope = 0;
3162 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003163 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003164
Dave Barachd7cb1b52016-12-09 09:52:16 -05003165 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3166 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003167 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3168
Dave Barachd7cb1b52016-12-09 09:52:16 -05003169 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003170 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003171 a = pool_elt_at_index (lm->if_address_pool, ai);
3172 ip6_address_t *address =
3173 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003174
Chris Lukebfd32fd2016-06-24 20:38:06 -04003175 if (ip6_address_is_link_local_unicast (address))
3176 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003177 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003178 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003179 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003180 vec_add1 (local_scope, ai);
3181 else
3182 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003183
3184 ai = a->next_this_sw_interface;
3185 }
3186
Dave Barachd7cb1b52016-12-09 09:52:16 -05003187 if (vec_len (link_scope))
3188 {
3189 vlib_cli_output (vm, "\tLink-local address(es):\n");
3190 ip6_print_addrs (vm, link_scope);
3191 vec_free (link_scope);
3192 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003193
Dave Barachd7cb1b52016-12-09 09:52:16 -05003194 if (vec_len (local_scope))
3195 {
3196 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3197 ip6_print_addrs (vm, local_scope);
3198 vec_free (local_scope);
3199 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003200
Dave Barachd7cb1b52016-12-09 09:52:16 -05003201 if (vec_len (global_scope))
3202 {
3203 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3204 ip6_print_addrs (vm, global_scope);
3205 vec_free (global_scope);
3206 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003207
Dave Barachd7cb1b52016-12-09 09:52:16 -05003208 if (vec_len (unknown_scope))
3209 {
3210 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3211 ip6_print_addrs (vm, unknown_scope);
3212 vec_free (unknown_scope);
3213 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003214
Ed Warnickecb9cada2015-12-08 15:45:58 -07003215 vlib_cli_output (vm, "\tJoined group address(es):\n");
3216 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003217 /* *INDENT-OFF* */
3218 pool_foreach (m, radv_info->mldp_group_pool,
3219 ({
3220 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3221 &m->mcast_address);
3222 }));
3223 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003224
3225 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003226 ip6_radv_prefix_t *p;
3227 /* *INDENT-OFF* */
3228 pool_foreach (p, radv_info->adv_prefixes_pool,
3229 ({
3230 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3231 format_ip6_address, &p->prefix, p->prefix_len);
3232 }));
3233 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003234
Dave Barachd7cb1b52016-12-09 09:52:16 -05003235 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003236 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3237 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3238 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3239 vlib_cli_output (vm, "\tND DAD is disabled\n");
3240 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3241 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3242 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003243 vlib_cli_output (vm,
3244 "\tND advertised retransmit interval is %d (msec)\n",
3245 radv_info->
3246 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003247
3248 u32 ra_interval = radv_info->max_radv_interval;
3249 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003250 vlib_cli_output (vm,
3251 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003252 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003253 vlib_cli_output (vm,
3254 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003255 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003256 vlib_cli_output (vm,
3257 "\tHosts %s stateless autoconfig for addresses\n",
3258 (radv_info->adv_managed_flag) ? "use" :
3259 " don't use");
3260 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3261 radv_info->n_advertisements_sent);
3262 vlib_cli_output (vm, "\tND router solicitations received %d\n",
3263 radv_info->n_solicitations_rcvd);
3264 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3265 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003266 }
3267 else
3268 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04003269 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003270 format_unformat_error, input);
3271
3272 }
3273 }
3274 return error;
3275}
3276
Billy McFall0683c9c2016-10-13 08:27:31 -04003277/*?
3278 * This command is used to display various IPv6 attributes on a given
3279 * interface.
3280 *
3281 * @cliexpar
3282 * Example of how to display IPv6 settings:
3283 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3284 * GigabitEthernet2/0/0 is admin up
3285 * Link-local address(es):
3286 * fe80::ab8/64
3287 * Joined group address(es):
3288 * ff02::1
3289 * ff02::2
3290 * ff02::16
3291 * ff02::1:ff00:ab8
3292 * Advertised Prefixes:
3293 * prefix fe80::fe:28ff:fe9c:75b3, length 64
3294 * MTU is 1500
3295 * ICMP error messages are unlimited
3296 * ICMP redirects are disabled
3297 * ICMP unreachables are not sent
3298 * ND DAD is disabled
3299 * ND advertised reachable time is 0
3300 * ND advertised retransmit interval is 0 (msec)
3301 * ND router advertisements are sent every 200 seconds (min interval is 150)
3302 * ND router advertisements live for 600 seconds
3303 * Hosts use stateless autoconfig for addresses
3304 * ND router advertisements sent 19336
3305 * ND router solicitations received 0
3306 * ND router solicitations dropped 0
3307 * @cliexend
3308 * Example of output if IPv6 is not enabled on the interface:
3309 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3310 * show ip6 interface: IPv6 not enabled on interface
3311 * @cliexend
3312?*/
3313/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003314VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3315{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003316 .path = "show ip6 interface",
3317 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003318 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003319};
Billy McFall0683c9c2016-10-13 08:27:31 -04003320/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003321
3322clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003323disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003324{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003325 clib_error_t *error = 0;
3326 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003327 u32 ri;
3328
3329 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003330 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3331 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003332 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003333
Ed Warnickecb9cada2015-12-08 15:45:58 -07003334 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003335 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003336 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003337 vnet_main_t *vnm = vnet_get_main ();
3338 ip6_radv_t *radv_info;
3339
3340 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003341
3342 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01003343 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003344 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003345 {
3346 /* essentially "disables" ipv6 on this interface */
3347 error = ip6_add_del_interface_address (vm, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003348 &radv_info->
Neale Ranns75152282017-01-09 01:00:45 -08003349 link_local_address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003350 1 /* is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003351
Dave Barachd7cb1b52016-12-09 09:52:16 -05003352 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3353 0 /* is_add */ );
Neale Ranns4008ac92017-02-13 23:20:04 -08003354 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003355 }
3356 }
3357 return error;
3358}
3359
3360int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003361ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003362{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003363 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3364 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003365
Dave Barachd7cb1b52016-12-09 09:52:16 -05003366 /* look up the radv_t information for this interface */
3367 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3368 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003369
Dave Barachd7cb1b52016-12-09 09:52:16 -05003370 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07003371
Dave Barachd7cb1b52016-12-09 09:52:16 -05003372 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003373}
3374
Dave Barachd7cb1b52016-12-09 09:52:16 -05003375clib_error_t *
3376enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003377{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003378 clib_error_t *error = 0;
3379 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003380 u32 ri;
3381 int is_add = 1;
3382
3383 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003384 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3385 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003386
Dave Barachd7cb1b52016-12-09 09:52:16 -05003387 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3388
3389 /* if not created yet */
3390 if (ri == ~0)
3391 {
3392 vnet_main_t *vnm = vnet_get_main ();
3393 vnet_sw_interface_t *sw_if0;
3394
3395 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3396 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3397 {
3398 ethernet_interface_t *eth_if0;
3399
3400 eth_if0 =
3401 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3402 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003403 {
3404 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003405 ri =
3406 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003407
Dave Barachd7cb1b52016-12-09 09:52:16 -05003408 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003409 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003410 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003411 ip6_address_t link_local_address;
3412
Dave Barachd7cb1b52016-12-09 09:52:16 -05003413 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003414
Dave Barachd7cb1b52016-12-09 09:52:16 -05003415 ip6_link_local_address_from_ethernet_mac_address
3416 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003417
3418 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003419 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003420 {
3421 /* make up an interface id */
3422 md5_context_t m;
3423 u8 digest[16];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003424
Ed Warnickecb9cada2015-12-08 15:45:58 -07003425 link_local_address.as_u64[0] = radv_info->randomizer;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003426
Ed Warnickecb9cada2015-12-08 15:45:58 -07003427 md5_init (&m);
3428 md5_add (&m, &link_local_address, 16);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003429 md5_finish (&m, digest);
3430
3431 clib_memcpy (&link_local_address, digest, 16);
3432
Ed Warnickecb9cada2015-12-08 15:45:58 -07003433 radv_info->randomizer = link_local_address.as_u64[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003434
3435 link_local_address.as_u64[0] =
3436 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003437 /* clear u bit */
3438 link_local_address.as_u8[8] &= 0xfd;
3439 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003440
Neale Ranns4008ac92017-02-13 23:20:04 -08003441 ip6_mfib_interface_enable_disable (sw_if_index, 1);
3442
Ed Warnickecb9cada2015-12-08 15:45:58 -07003443 /* essentially "enables" ipv6 on this interface */
3444 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003445 &link_local_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003446 128
3447 /* address width */ ,
3448 0 /* is_del */ );
3449
3450 if (error)
3451 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3452 !is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003453 else
3454 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003455 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003456 }
3457 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003458 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003459 }
3460 }
3461 return error;
3462}
3463
3464static clib_error_t *
3465enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003466 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003467{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003468 vnet_main_t *vnm = vnet_get_main ();
3469 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003470 u32 sw_if_index;
3471
3472 sw_if_index = ~0;
3473
Dave Barachd7cb1b52016-12-09 09:52:16 -05003474 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003475 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003476 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003477 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003478 else
3479 {
3480 error = clib_error_return (0, "unknown interface\n'",
3481 format_unformat_error, input);
3482
3483 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003484 return error;
3485}
3486
Billy McFall0683c9c2016-10-13 08:27:31 -04003487/*?
3488 * This command is used to enable IPv6 on a given interface.
3489 *
3490 * @cliexpar
3491 * Example of how enable IPv6 on a given interface:
3492 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3493?*/
3494/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003495VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3496{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003497 .path = "enable ip6 interface",
3498 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003499 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003500};
Billy McFall0683c9c2016-10-13 08:27:31 -04003501/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003502
3503static clib_error_t *
3504disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003505 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003506{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003507 vnet_main_t *vnm = vnet_get_main ();
3508 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003509 u32 sw_if_index;
3510
3511 sw_if_index = ~0;
3512
Dave Barachd7cb1b52016-12-09 09:52:16 -05003513 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003514 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003515 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003516 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003517 else
3518 {
3519 error = clib_error_return (0, "unknown interface\n'",
3520 format_unformat_error, input);
3521
3522 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003523 return error;
3524}
3525
Billy McFall0683c9c2016-10-13 08:27:31 -04003526/*?
3527 * This command is used to disable IPv6 on a given interface.
3528 *
3529 * @cliexpar
3530 * Example of how disable IPv6 on a given interface:
3531 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3532?*/
3533/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003534VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3535{
Billy McFall0683c9c2016-10-13 08:27:31 -04003536 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003537 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003538 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003539};
Billy McFall0683c9c2016-10-13 08:27:31 -04003540/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003541
Billy McFall0683c9c2016-10-13 08:27:31 -04003542/*?
3543 * This command is used to configure the neighbor discovery
3544 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3545 * command to display some of the current neighbor discovery parameters
3546 * on a given interface. This command has three formats:
3547 *
3548 *
3549 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3550 *
3551 * '<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>'
3552 *
3553 * Where:
3554 *
3555 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3556 * router-advertisement messages to use stateful address
3557 * auto-configuration to obtain address information (sets the M-bit).
3558 * Default is the M-bit is not set and the '<em>no</em>' option
3559 * returns it to this default state.
3560 *
3561 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3562 * router-advertisement messages that hosts use stateful auto
3563 * configuration to obtain nonaddress related information (sets
3564 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3565 * option returns it to this default state.
3566 *
3567 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3568 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3569 * router-advertisement messages.
3570 *
3571 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3572 * optional source link-layer address in the ICMPv6 router-advertisement
3573 * messages. Default is to include the optional source link-layer address
3574 * and the '<em>no</em>' option returns it to this default state.
3575 *
3576 * <em>[no] ra-send-unicast</em> - Use the source address of the
3577 * router-solicitation message if availiable. The default is to use
3578 * multicast address of all nodes, and the '<em>no</em>' option returns
3579 * it to this default state.
3580 *
3581 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3582 * default router in ICMPv6 router-advertisement messages. The range is
3583 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3584 * '<em><max-interval></em>'. The default value is 600 seconds and the
3585 * '<em>no</em>' option returns it to this default value.
3586 *
3587 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3588 * router-advertisement messages sent and the interval between each
3589 * message. Range for count is 1 - 3 and default is 3. Range for interval
3590 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3591 * returns both to their default value.
3592 *
3593 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3594 * interval between sending ICMPv6 router-advertisement messages. The
3595 * range for max-interval is from 4 to 200 seconds. min-interval can not
3596 * be more than 75% of max-interval. If not set, min-interval will be
3597 * set to 75% of max-interval. The range for min-interval is from 3 to
3598 * 150 seconds. The '<em>no</em>' option returns both to their default
3599 * value.
3600 *
3601 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3602 * The '<em>no</em>' options implies to start (or restart) sending
3603 * ICMPv6 router-advertisement messages.
3604 *
3605 *
3606 * <b>Format 2 - Prefix Options:</b>
3607 *
3608 * '<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>'
3609 *
3610 * Where:
3611 *
3612 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3613 *
3614 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3615 * length of time in seconds during what the prefix is valid for the purpose of
3616 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3617 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3618 * length of time in seconds during what addresses generated from the prefix remain
3619 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3620 *
3621 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3622 * are inifinte, no timeout.
3623 *
3624 * <em>no-advertise</em> - Do not send full router address in prefix
3625 * advertisement. Default is to advertise (i.e. - This flag is off by default).
3626 *
3627 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3628 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3629 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3630 *
3631 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3632 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3633 *
3634 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3635 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3636 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3637 * the L-bit.
3638 *
3639 *
3640 * <b>Format 3: - Default of Prefix:</b>
3641 *
3642 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3643 *
3644 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3645 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3646 * is ignored and the prefix is deleted.
3647 *
3648 *
3649 * @cliexpar
3650 * Example of how set a router advertisement option:
3651 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3652 * Example of how to add a prefix:
3653 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3654 * Example of how to delete a prefix:
3655 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3656?*/
3657/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003658VLIB_CLI_COMMAND (ip6_nd_command, static) =
3659{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003660 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04003661 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003662 .function = ip6_neighbor_cmd,
3663};
Billy McFall0683c9c2016-10-13 08:27:31 -04003664/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003665
3666clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003667set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08003668 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003669{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003670 clib_error_t *error = 0;
3671 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003672 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003673 ip6_radv_t *radv_info;
3674 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07003675
Dave Barachd7cb1b52016-12-09 09:52:16 -05003676 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003677 {
3678 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003679 return (error = clib_error_return (0, "address not link-local",
3680 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003681 }
3682
3683 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003684 enable_ip6_interface (vm, sw_if_index);
3685
Ed Warnickecb9cada2015-12-08 15:45:58 -07003686 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003687
3688 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003689 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003690 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003691
3692 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003693
Ed Warnickecb9cada2015-12-08 15:45:58 -07003694 /* delete the old one */
3695 error = ip6_add_del_interface_address (vm, sw_if_index,
3696 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08003697 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05003698
3699 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003700 {
3701 /* add the new one */
3702 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08003703 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003704 0 /* is_del */ );
3705
3706 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003707 {
3708 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003709 }
3710 }
3711 }
3712 else
3713 {
3714 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3715 error = clib_error_return (0, "ip6 not enabled for interface",
3716 format_unformat_error);
3717 }
3718 return error;
3719}
Dave Barachd7cb1b52016-12-09 09:52:16 -05003720
Ed Warnickecb9cada2015-12-08 15:45:58 -07003721clib_error_t *
3722set_ip6_link_local_address_cmd (vlib_main_t * vm,
3723 unformat_input_t * input,
3724 vlib_cli_command_t * cmd)
3725{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003726 vnet_main_t *vnm = vnet_get_main ();
3727 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003728 u32 sw_if_index;
3729 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003730
3731 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003732 {
3733 /* get the rest of the command */
3734 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3735 {
Neale Ranns75152282017-01-09 01:00:45 -08003736 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003737 break;
3738 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003739 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003740 }
3741 }
Neale Ranns75152282017-01-09 01:00:45 -08003742 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003743 return error;
3744}
3745
Billy McFall0683c9c2016-10-13 08:27:31 -04003746/*?
3747 * This command is used to assign an IPv6 Link-local address to an
3748 * interface. This command will enable IPv6 on an interface if it
3749 * is not already enabled. Use the '<em>show ip6 interface</em>' command
3750 * to display the assigned Link-local address.
3751 *
3752 * @cliexpar
3753 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08003754 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04003755?*/
3756/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003757VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3758{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003759 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08003760 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003761 .function = set_ip6_link_local_address_cmd,
3762};
Billy McFall0683c9c2016-10-13 08:27:31 -04003763/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003764
Neale Ranns87df12d2017-02-18 08:16:41 -08003765/**
3766 * @brief callback when an interface address is added or deleted
3767 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003768static void
3769ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3770 uword opaque,
3771 u32 sw_if_index,
3772 ip6_address_t * address,
3773 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003774 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003775{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003776 vnet_main_t *vnm = vnet_get_main ();
3777 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003778 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003779 vlib_main_t *vm = vnm->vlib_main;
3780 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003781 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003782
3783 /* create solicited node multicast address for this interface adddress */
3784 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003785
Ed Warnickecb9cada2015-12-08 15:45:58 -07003786 a.as_u8[0xd] = address->as_u8[0xd];
3787 a.as_u8[0xe] = address->as_u8[0xe];
3788 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003789
3790 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003791 {
3792 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003793 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003794
3795 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003796 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3797 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003798 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003799 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003800 {
3801 /* get radv_info */
3802 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3803
3804 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003805 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003806 radv_info->ref_count++;
3807
Neale Ranns87df12d2017-02-18 08:16:41 -08003808 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003809 }
3810 }
3811 else
3812 {
3813
3814 /* delete */
3815 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003816 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3817 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003818 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01003819
Dave Barachd7cb1b52016-12-09 09:52:16 -05003820 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003821 {
3822 /* get radv_info */
3823 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3824
Neale Ranns87df12d2017-02-18 08:16:41 -08003825 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003826
3827 /* if interface up send MLDP "report" */
3828 radv_info->all_routers_mcast = 0;
3829
3830 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003831 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003832 radv_info->ref_count--;
3833 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01003834 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3835 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003836 }
3837}
3838
Dave Barachd7cb1b52016-12-09 09:52:16 -05003839clib_error_t *
3840ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003841{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003842 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003843
3844 nm->limit_neighbor_cache_size = neighbor_limit;
3845 return 0;
3846}
3847
Dave Barachd7cb1b52016-12-09 09:52:16 -05003848static clib_error_t *
3849ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003850{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003851 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3852 ip6_main_t *im = &ip6_main;
3853
Ed Warnickecb9cada2015-12-08 15:45:58 -07003854 mhash_init (&nm->neighbor_index_by_key,
3855 /* value size */ sizeof (uword),
3856 /* key size */ sizeof (ip6_neighbor_key_t));
3857
Dave Barachd7cb1b52016-12-09 09:52:16 -05003858 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3859 ip6_icmp_neighbor_solicitation_node.index);
3860 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3861 ip6_icmp_neighbor_advertisement_node.index);
3862 icmp6_register_type (vm, ICMP6_router_solicitation,
3863 ip6_icmp_router_solicitation_node.index);
3864 icmp6_register_type (vm, ICMP6_router_advertisement,
3865 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003866
3867 /* handler node for ip6 neighbor discovery events and timers */
3868 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3869
3870 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003871 ip6_add_del_interface_address_callback_t cb;
3872 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3873
Ed Warnickecb9cada2015-12-08 15:45:58 -07003874 /* when an interface address changes... */
3875 cb.function = ip6_neighbor_add_del_interface_address;
3876 cb.function_opaque = 0;
3877 vec_add1 (im->add_del_interface_address_callbacks, cb);
3878
3879 mhash_init (&nm->pending_resolutions_by_address,
3880 /* value size */ sizeof (uword),
3881 /* key size */ sizeof (ip6_address_t));
3882
John Lo1edfba92016-08-27 01:11:57 -04003883 mhash_init (&nm->mac_changes_by_address,
3884 /* value size */ sizeof (uword),
3885 /* key size */ sizeof (ip6_address_t));
3886
Ed Warnickecb9cada2015-12-08 15:45:58 -07003887 /* default, configurable */
3888 nm->limit_neighbor_cache_size = 50000;
3889
3890#if 0
3891 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003892 vec_validate_init_empty
3893 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003894#endif
3895
Ed Warnickecb9cada2015-12-08 15:45:58 -07003896 return 0;
3897}
3898
3899VLIB_INIT_FUNCTION (ip6_neighbor_init);
3900
3901
Dave Barachd7cb1b52016-12-09 09:52:16 -05003902void
3903vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3904 void *address_arg,
3905 uword node_index,
3906 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003907{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003908 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3909 ip6_address_t *address = address_arg;
3910 uword *p;
3911 pending_resolution_t *pr;
3912
Ed Warnickecb9cada2015-12-08 15:45:58 -07003913 pool_get (nm->pending_resolutions, pr);
3914
3915 pr->next_index = ~0;
3916 pr->node_index = node_index;
3917 pr->type_opaque = type_opaque;
3918 pr->data = data;
3919
3920 p = mhash_get (&nm->pending_resolutions_by_address, address);
3921 if (p)
3922 {
3923 /* Insert new resolution at the head of the list */
3924 pr->next_index = p[0];
3925 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3926 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003927
3928 mhash_set (&nm->pending_resolutions_by_address, address,
3929 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003930}
3931
Dave Barachd7cb1b52016-12-09 09:52:16 -05003932int
3933vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3934 void *data_callback,
3935 u32 pid,
3936 void *address_arg,
3937 uword node_index,
3938 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04003939{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003940 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3941 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003942
Eyal Barif9f40652017-04-01 03:55:08 +03003943 /* Try to find an existing entry */
3944 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
3945 u32 *p = first;
3946 pending_resolution_t *mc;
3947 while (p && *p != ~0)
3948 {
3949 mc = pool_elt_at_index (nm->mac_changes, *p);
3950 if (mc->node_index == node_index && mc->type_opaque == type_opaque
3951 && mc->pid == pid)
3952 break;
3953 p = &mc->next_index;
3954 }
3955
3956 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04003957 if (is_add)
3958 {
Eyal Barif9f40652017-04-01 03:55:08 +03003959 if (found)
3960 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
3961
John Lo1edfba92016-08-27 01:11:57 -04003962 pool_get (nm->mac_changes, mc);
Eyal Barif9f40652017-04-01 03:55:08 +03003963 *mc = (pending_resolution_t)
3964 {
3965 .next_index = ~0,.node_index = node_index,.type_opaque =
3966 type_opaque,.data = data,.data_callback = data_callback,.pid =
3967 pid,};
John Lo1edfba92016-08-27 01:11:57 -04003968
Eyal Barif9f40652017-04-01 03:55:08 +03003969 /* Insert new resolution at the end of the list */
3970 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04003971 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03003972 p[0] = new_idx;
3973 else
3974 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04003975 }
3976 else
3977 {
Eyal Barif9f40652017-04-01 03:55:08 +03003978 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003979 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04003980
Eyal Barif9f40652017-04-01 03:55:08 +03003981 /* Clients may need to clean up pool entries, too */
3982 void (*fp) (u32, u8 *) = data_callback;
3983 if (fp)
3984 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04003985
Eyal Barif9f40652017-04-01 03:55:08 +03003986 /* Remove the entry from the list and delete the entry */
3987 *p = mc->next_index;
3988 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003989
Eyal Barif9f40652017-04-01 03:55:08 +03003990 /* Remove from hash if we deleted the last entry */
3991 if (*p == ~0 && p == first)
3992 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04003993 }
Eyal Barif9f40652017-04-01 03:55:08 +03003994 return 0;
John Lo1edfba92016-08-27 01:11:57 -04003995}
3996
Dave Barachd7cb1b52016-12-09 09:52:16 -05003997int
3998vnet_ip6_nd_term (vlib_main_t * vm,
3999 vlib_node_runtime_t * node,
4000 vlib_buffer_t * p0,
4001 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004002 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004003{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004004 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4005 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4006 pending_resolution_t *mc;
John Lo1edfba92016-08-27 01:11:57 -04004007
4008 ndh = ip6_next_header (ip);
4009 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4010 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004011 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004012
4013 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4014 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4015 {
4016 u8 *t0 = vlib_add_trace (vm, node, p0,
4017 sizeof (icmp6_input_trace_t));
4018 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4019 }
4020
4021 /* Check if anyone want ND events for L2 BDs */
Eyal Baria0623f82017-03-30 03:05:06 +03004022 uword *p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
4023 if (p && !ip6_address_is_link_local_unicast (&ip->src_address))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004024 {
John Lo1edfba92016-08-27 01:11:57 -04004025 u32 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004026 while (next_index != (u32) ~ 0)
4027 {
4028 int (*fp) (u32, u8 *, u32, ip6_address_t *);
John Lo1edfba92016-08-27 01:11:57 -04004029 int rv = 1;
4030 mc = pool_elt_at_index (nm->mac_changes, next_index);
4031 fp = mc->data_callback;
4032 /* Call the callback, return 1 to suppress dup events */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004033 if (fp)
4034 rv = (*fp) (mc->data,
4035 eth->src_address, sw_if_index, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004036 /* Signal the resolver process */
4037 if (rv == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004038 vlib_process_signal_event (vm, mc->node_index,
4039 mc->type_opaque, mc->data);
John Lo1edfba92016-08-27 01:11:57 -04004040 next_index = mc->next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004041 }
John Lo1edfba92016-08-27 01:11:57 -04004042 }
4043
4044 /* Check if MAC entry exsist for solicited target IP */
4045 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4046 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004047 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004048 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004049 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004050
4051 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004052 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004053 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4054 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004055 return 0; /* source link layer address option not present */
4056
John Lo1edfba92016-08-27 01:11:57 -04004057 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004058 macp =
4059 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004060 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004061 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004062 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004063 vlib_node_runtime_t *error_node =
4064 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004065 ip->dst_address = ip->src_address;
4066 ip->src_address = ndh->target_address;
4067 ip->hop_limit = 255;
4068 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004069 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004070 clib_memcpy (opt->ethernet_address, macp, 6);
4071 ndh->icmp.type = ICMP6_neighbor_advertisement;
4072 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004073 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4074 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004075 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004076 ndh->icmp.checksum =
4077 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4078 clib_memcpy (eth->dst_address, eth->src_address, 6);
4079 clib_memcpy (eth->src_address, macp, 6);
4080 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004081 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004082 return 1;
4083 }
John Lo1edfba92016-08-27 01:11:57 -04004084 }
4085
4086 return 0;
4087
4088}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004089
Neale Ranns3f844d02017-02-18 00:03:54 -08004090int
4091ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4092{
4093 u32 fib_index;
4094
4095 fib_prefix_t pfx = {
4096 .fp_len = 128,
4097 .fp_proto = FIB_PROTOCOL_IP6,
4098 .fp_addr = {
4099 .ip6 = *addr,
4100 },
4101 };
4102 ip46_address_t nh = {
4103 .ip6 = *addr,
4104 };
4105
4106 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4107
4108 if (~0 == fib_index)
4109 return VNET_API_ERROR_NO_SUCH_FIB;
4110
4111 if (is_del)
4112 {
4113 fib_table_entry_path_remove (fib_index,
4114 &pfx,
4115 FIB_SOURCE_IP6_ND_PROXY,
4116 FIB_PROTOCOL_IP6,
4117 &nh,
4118 sw_if_index,
4119 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4120 /* flush the ND cache of this address if it's there */
4121 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4122 sw_if_index, addr, NULL, 0);
4123 }
4124 else
4125 {
4126 fib_table_entry_path_add (fib_index,
4127 &pfx,
4128 FIB_SOURCE_IP6_ND_PROXY,
4129 FIB_ENTRY_FLAG_NONE,
4130 FIB_PROTOCOL_IP6,
4131 &nh,
4132 sw_if_index,
4133 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4134 }
4135 return (0);
4136}
4137
4138static clib_error_t *
4139set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4140 unformat_input_t * input, vlib_cli_command_t * cmd)
4141{
4142 vnet_main_t *vnm = vnet_get_main ();
4143 clib_error_t *error = 0;
4144 ip6_address_t addr;
4145 u32 sw_if_index;
4146 u8 is_del = 0;
4147
4148 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4149 {
4150 /* get the rest of the command */
4151 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4152 {
4153 if (unformat (input, "%U", unformat_ip6_address, &addr))
4154 break;
4155 else if (unformat (input, "delete") || unformat (input, "del"))
4156 is_del = 1;
4157 else
4158 return (unformat_parse_error (input));
4159 }
4160 }
4161
4162 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4163
4164 return error;
4165}
4166
4167/* *INDENT-OFF* */
4168VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4169{
4170 .path = "set ip6 nd proxy",
4171 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4172 .function = set_ip6_nd_proxy_cmd,
4173};
4174/* *INDENT-ON* */
4175
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004176void
Neale Ranns3be6b282016-12-20 14:24:01 +00004177ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004178{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004179 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4180 ip6_neighbor_t *n;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004181
4182 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004183 pool_foreach (n, nm->neighbor_pool,
4184 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004185 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004186 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004187 adj_nbr_walk_nh6 (sw_if_index,
4188 &n->key.ip6_address,
4189 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004190 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004191 }));
4192 /* *INDENT-ON* */
4193}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004194
4195/*
4196 * fd.io coding-style-patch-verification: ON
4197 *
4198 * Local Variables:
4199 * eval: (c-set-style "gnu")
4200 * End:
4201 */