blob: 22f640c8543fc0470ad6f33100f6cc5d7884c57a [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
Eyal Baric125ecc2017-09-20 11:29:17 +0300198 /* Wildcard nd report publisher */
199 uword wc_ip6_nd_publisher_node;
200 uword wc_ip6_nd_publisher_et;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201} ip6_neighbor_main_t;
202
Neale Ranns3f844d02017-02-18 00:03:54 -0800203/* ipv6 neighbor discovery - timer/event types */
204typedef enum
205{
206 ICMP6_ND_EVENT_INIT,
207} ip6_icmp_neighbor_discovery_event_type_t;
208
209typedef union
210{
211 u32 add_del_swindex;
212 struct
213 {
214 u32 up_down_swindex;
215 u32 fib_index;
216 } up_down_event;
217} ip6_icmp_neighbor_discovery_event_data_t;
218
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219static ip6_neighbor_main_t ip6_neighbor_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500220static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Eyal Baric125ecc2017-09-20 11:29:17 +0300222static void wc_nd_signal_report (wc_nd_report_t * r);
223
224/**
225 * @brief publish wildcard arp event
226 * @param sw_if_index The interface on which the ARP entires are acted
227 */
228static int
229vnet_nd_wc_publish (u32 sw_if_index, u8 * mac, ip6_address_t * ip6)
230{
231 wc_nd_report_t r = {
232 .sw_if_index = sw_if_index,
233 .ip6 = *ip6,
234 };
235 memcpy (r.mac, mac, sizeof r.mac);
236
237 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
238 vl_api_rpc_call_main_thread (wc_nd_signal_report, (u8 *) & r, sizeof r);
239 return 0;
240}
241
242static void
243wc_nd_signal_report (wc_nd_report_t * r)
244{
245 vlib_main_t *vm = vlib_get_main ();
246 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
247 uword ni = nm->wc_ip6_nd_publisher_node;
248 uword et = nm->wc_ip6_nd_publisher_et;
249
250 if (ni == (uword) ~ 0)
251 return;
252 wc_nd_report_t *q =
253 vlib_process_signal_event_data (vm, ni, et, 1, sizeof *q);
254
255 *q = *r;
256}
257
258void
259wc_nd_set_publisher_node (uword node_index, uword event_type)
260{
261 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
262 nm->wc_ip6_nd_publisher_node = node_index;
263 nm->wc_ip6_nd_publisher_et = event_type;
264}
265
Dave Barachd7cb1b52016-12-09 09:52:16 -0500266static u8 *
267format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500269 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
270 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
271 vnet_main_t *vnm = vnet_get_main ();
272 vnet_sw_interface_t *si;
273 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 if (!n)
276 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
277 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100278
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100279 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100281
282 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500283 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Neale Rannsb3b2de72017-03-08 05:17:22 -0800285 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
286 flags = format (flags, "N");
287
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100289 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
291 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 format_ethernet_address, n->link_layer_address,
294 format_vnet_sw_interface_name, vnm, si);
295
Dave Barachd7cb1b52016-12-09 09:52:16 -0500296 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 return s;
298}
299
Neale Ranns15002542017-09-10 04:39:11 -0700300static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700301ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700302{
303 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
304 {
305 fib_prefix_t pfx = {
306 .fp_len = 128,
307 .fp_proto = FIB_PROTOCOL_IP6,
308 .fp_addr.ip6 = n->key.ip6_address,
309 };
310 fib_table_entry_path_remove (fib_index,
311 &pfx,
312 FIB_SOURCE_ADJ,
313 DPO_PROTO_IP6,
314 &pfx.fp_addr,
315 n->key.sw_if_index, ~0,
316 1, FIB_ROUTE_PATH_FLAG_NONE);
317 }
318}
319
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320static clib_error_t *
321ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500322 u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500324 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
325 ip6_neighbor_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Dave Barachd7cb1b52016-12-09 09:52:16 -0500327 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
328 {
329 u32 i, *to_delete = 0;
330
331 /* *INDENT-OFF* */
332 pool_foreach (n, nm->neighbor_pool,
333 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 if (n->key.sw_if_index == sw_if_index)
335 vec_add1 (to_delete, n - nm->neighbor_pool);
336 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
339 for (i = 0; i < vec_len (to_delete); i++)
340 {
341 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
342 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Ranns15002542017-09-10 04:39:11 -0700343 ip6_neighbor_adj_fib_remove (n,
344 ip6_fib_table_get_index_for_sw_if_index
345 (n->key.sw_if_index));
346 pool_put (nm->neighbor_pool, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700347 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 vec_free (to_delete);
349 }
350
351 return 0;
352}
353
354VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
355
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356static void
357unset_random_neighbor_entry (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500359 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
360 vnet_main_t *vnm = vnet_get_main ();
361 vlib_main_t *vm = vnm->vlib_main;
362 ip6_neighbor_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 u32 index;
364
365 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
366 nm->neighbor_delete_rotor = index;
367
368 /* Try again from elt 0, could happen if an intfc goes down */
369 if (index == ~0)
370 {
371 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
372 nm->neighbor_delete_rotor = index;
373 }
374
375 /* Nothing left in the pool */
376 if (index == ~0)
377 return;
378
379 e = pool_elt_at_index (nm->neighbor_pool, index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500380
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500382 &e->key.ip6_address,
383 e->link_layer_address,
384 ETHER_MAC_ADDR_LEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385}
386
Dave Barachd7cb1b52016-12-09 09:52:16 -0500387typedef struct
388{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100390 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800391 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 u8 link_layer_address[6];
393 u32 sw_if_index;
394 ip6_address_t addr;
395} ip6_neighbor_set_unset_rpc_args_t;
396
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397static void ip6_neighbor_set_unset_rpc_callback
398 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Dave Barachd7cb1b52016-12-09 09:52:16 -0500400static void set_unset_ip6_neighbor_rpc
401 (vlib_main_t * vm,
402 u32 sw_if_index,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800403 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
404 int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
406 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500407 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 args.sw_if_index = sw_if_index;
410 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100411 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800412 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100413 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800414 if (NULL != link_layer_address)
415 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500418 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100421static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100423{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500424 icmp6_neighbor_solicitation_header_t *h;
425 vnet_main_t *vnm = vnet_get_main ();
426 ip6_main_t *im = &ip6_main;
427 ip_interface_address_t *ia;
428 ip6_address_t *dst, *src;
429 vnet_hw_interface_t *hi;
430 vnet_sw_interface_t *si;
431 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100432 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500433 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100434 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100435
Dave Barachd7cb1b52016-12-09 09:52:16 -0500436 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100437
Dave Barachd7cb1b52016-12-09 09:52:16 -0500438 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100439 dst = &adj->sub_type.nbr.next_hop.ip6;
440
441 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100442 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100443 return;
444 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500445 src = ip6_interface_address_matching_destination (im, dst,
446 adj->rewrite_header.
447 sw_if_index, &ia);
448 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100449 {
450 return;
451 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100452
Dave Barachd7cb1b52016-12-09 09:52:16 -0500453 h = vlib_packet_template_get_packet (vm,
454 &im->discover_neighbor_packet_template,
455 &bi);
Neale Rannsb80c5362016-10-08 13:03:40 +0100456
Dave Barachd7cb1b52016-12-09 09:52:16 -0500457 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100458
459 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
460 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
461 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
462 h->ip.src_address = src[0];
463 h->neighbor.target_address = dst[0];
464
465 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500466 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100467
468 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500469 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
470 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100471
472 b = vlib_get_buffer (vm, bi);
473 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500474 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100475
476 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500477 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
478 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100479
480 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500481 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
482 u32 *to_next = vlib_frame_vector_args (f);
483 to_next[0] = bi;
484 f->n_vectors = 1;
485 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100486 }
487}
488
489static void
490ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
491{
492 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
493 ethernet_build_rewrite (vnet_get_main (),
494 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500495 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100496 nbr->link_layer_address));
497}
498
499static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000500ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100501{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500502 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000503
Dave Barachd7cb1b52016-12-09 09:52:16 -0500504 adj_nbr_update_rewrite (ai,
505 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
506 ethernet_build_rewrite (vnet_get_main (),
507 adj->rewrite_header.
508 sw_if_index,
509 adj_get_link_type (ai),
510 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100511}
512
513#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
514{ \
515 k.sw_if_index = sw_if_index; \
516 k.ip6_address = *addr; \
517 k.pad = 0; \
518}
519
520static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500521ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100522{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500523 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
524 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100525 ip6_neighbor_key_t k;
526 uword *p;
527
Dave Barachd7cb1b52016-12-09 09:52:16 -0500528 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100529
530 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500531 if (p)
532 {
533 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
534 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100535
536 return (n);
537}
538
539static adj_walk_rc_t
540ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
541{
542 ip6_neighbor_t *nbr = ctx;
543
544 ip6_nd_mk_complete (ai, nbr);
545
546 return (ADJ_WALK_RC_CONTINUE);
547}
548
549static adj_walk_rc_t
550ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
551{
Neale Ranns19c68d22016-12-07 15:38:14 +0000552 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100553
554 return (ADJ_WALK_RC_CONTINUE);
555}
556
557void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500558ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100559{
560 ip6_neighbor_t *nbr;
561 ip_adjacency_t *adj;
562
563 adj = adj_get (ai);
564
565 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
566
Neale Ranns32e1c012016-11-22 17:07:28 +0000567 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100568 {
Ole Trøan438f6302018-02-15 21:56:06 +0000569 case IP_LOOKUP_NEXT_GLEAN:
Neale Rannsc819fc62018-02-16 02:44:05 -0800570 adj_glean_update_rewrite (ai);
571 break;
572 case IP_LOOKUP_NEXT_ARP:
Neale Ranns32e1c012016-11-22 17:07:28 +0000573 if (NULL != nbr)
574 {
575 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
576 ip6_nd_mk_complete_walk, nbr);
577 }
578 else
579 {
580 /*
581 * no matching ND entry.
582 * construct the rewrite required to for an ND packet, and stick
583 * that in the adj's pipe to smoke.
584 */
585 adj_nbr_update_rewrite (ai,
586 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
587 ethernet_build_rewrite (vnm,
588 sw_if_index,
589 VNET_LINK_IP6,
590 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
591
592 /*
593 * since the FIB has added this adj for a route, it makes sense it may
594 * want to forward traffic sometime soon. Let's send a speculative ND.
595 * just one. If we were to do periodically that wouldn't be bad either,
596 * but that's more code than i'm prepared to write at this time for
597 * relatively little reward.
598 */
599 ip6_nbr_probe (adj);
600 }
601 break;
602 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700603 {
604 /*
605 * Construct a partial rewrite from the known ethernet mcast dest MAC
606 */
607 u8 *rewrite;
608 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100609
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700610 rewrite = ethernet_build_rewrite (vnm,
611 sw_if_index,
612 adj->ia_link,
613 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000614
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700615 /*
616 * Complete the remaining fields of the adj's rewrite to direct the
617 * complete of the rewrite at switch time by copying in the IP
618 * dst address's bytes.
619 * Ofset is 2 bytes into the desintation address. And we write 4 bytes.
620 */
621 offset = vec_len (rewrite) - 2;
622 adj_mcast_update_rewrite (ai, rewrite, offset, 0xffffffff);
Neale Ranns32e1c012016-11-22 17:07:28 +0000623
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700624 break;
625 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000626 case IP_LOOKUP_NEXT_DROP:
627 case IP_LOOKUP_NEXT_PUNT:
628 case IP_LOOKUP_NEXT_LOCAL:
629 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800630 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000631 case IP_LOOKUP_NEXT_MIDCHAIN:
632 case IP_LOOKUP_NEXT_ICMP_ERROR:
633 case IP_LOOKUP_N_NEXT:
634 ASSERT (0);
635 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100636 }
637}
638
Neale Ranns15002542017-09-10 04:39:11 -0700639
640static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700641ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700642{
643 fib_prefix_t pfx = {
644 .fp_len = 128,
645 .fp_proto = FIB_PROTOCOL_IP6,
646 .fp_addr.ip6 = n->key.ip6_address,
647 };
648
649 n->fib_entry_index =
650 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
651 FIB_ENTRY_FLAG_ATTACHED,
652 DPO_PROTO_IP6, &pfx.fp_addr,
653 n->key.sw_if_index, ~0, 1, NULL,
654 FIB_ROUTE_PATH_FLAG_NONE);
655}
656
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657int
658vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500659 u32 sw_if_index,
660 ip6_address_t * a,
661 u8 * link_layer_address,
662 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800663 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500665 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500667 ip6_neighbor_t *n = 0;
668 int make_new_nd_cache_entry = 1;
669 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500671 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
Damjan Marion586afd72017-04-05 19:18:20 +0200673 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700674 {
675 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800676 1 /* set new neighbor */ , is_static,
677 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678 return 0;
679 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
681 k.sw_if_index = sw_if_index;
682 k.ip6_address = a[0];
683 k.pad = 0;
684
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500686 if (p)
687 {
688 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
689 /* Refuse to over-write static neighbor entry. */
690 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
691 return -2;
692 make_new_nd_cache_entry = 0;
693 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100694
Dave Barachd7cb1b52016-12-09 09:52:16 -0500695 if (make_new_nd_cache_entry)
696 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500697 pool_get (nm->neighbor_pool, n);
698 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
699 /* old value */ 0);
700 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700701 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100702
Dave Barachd7cb1b52016-12-09 09:52:16 -0500703 clib_memcpy (n->link_layer_address,
704 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100705
Dave Barachd7cb1b52016-12-09 09:52:16 -0500706 /*
707 * create the adj-fib. the entry in the FIB table for and to the peer.
708 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800709 if (!is_no_fib_entry)
710 {
Neale Ranns15002542017-09-10 04:39:11 -0700711 ip6_neighbor_adj_fib_add (n,
712 ip6_fib_table_get_index_for_sw_if_index
713 (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700714 }
715 else
716 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800717 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
718 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500719 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100720 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500721 {
722 /*
723 * prevent a DoS attack from the data-plane that
724 * spams us with no-op updates to the MAC address
725 */
726 if (0 == memcmp (n->link_layer_address,
727 link_layer_address, n_bytes_link_layer_address))
Dave Barach59b25652017-09-10 15:04:27 -0400728 goto check_customers;
Neale Rannsb80c5362016-10-08 13:03:40 +0100729
Dave Barachd7cb1b52016-12-09 09:52:16 -0500730 clib_memcpy (n->link_layer_address,
731 link_layer_address, n_bytes_link_layer_address);
732 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733
Neale Rannsb80c5362016-10-08 13:03:40 +0100734 /* Update time stamp and flags. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735 n->cpu_time_last_updated = clib_cpu_time_now ();
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100736 if (is_static)
737 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100738 else
739 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
740
Neale Rannsb80c5362016-10-08 13:03:40 +0100741 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500742 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743
Dave Barach59b25652017-09-10 15:04:27 -0400744check_customers:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 /* Customer(s) waiting for this address to be resolved? */
746 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400747 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 {
John Lo1edfba92016-08-27 01:11:57 -0400749 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500750
751 while (next_index != (u32) ~ 0)
752 {
John Lo1edfba92016-08-27 01:11:57 -0400753 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
754 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500755 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400756 next_index = pr->next_index;
757 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500758 }
John Lo1edfba92016-08-27 01:11:57 -0400759
760 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 }
762
John Lo1edfba92016-08-27 01:11:57 -0400763 /* Customer(s) requesting ND event for this address? */
764 p = mhash_get (&nm->mac_changes_by_address, a);
765 if (p)
766 {
767 next_index = p[0];
768
Dave Barachd7cb1b52016-12-09 09:52:16 -0500769 while (next_index != (u32) ~ 0)
770 {
771 int (*fp) (u32, u8 *, u32, ip6_address_t *);
772 int rv = 1;
773 mc = pool_elt_at_index (nm->mac_changes, next_index);
774 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400775
Dave Barachd7cb1b52016-12-09 09:52:16 -0500776 /* Call the user's data callback, return 1 to suppress dup events */
777 if (fp)
778 rv =
779 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
780 /*
781 * Signal the resolver process, as long as the user
782 * says they want to be notified
783 */
784 if (rv == 0)
785 vlib_process_signal_event (vm, mc->node_index,
786 mc->type_opaque, mc->data);
787 next_index = mc->next_index;
788 }
John Lo1edfba92016-08-27 01:11:57 -0400789 }
790
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 return 0;
792}
793
794int
795vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500796 u32 sw_if_index,
797 ip6_address_t * a,
798 u8 * link_layer_address,
799 uword n_bytes_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500801 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500803 ip6_neighbor_t *n;
804 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805 int rv = 0;
806
Damjan Marion586afd72017-04-05 19:18:20 +0200807 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808 {
809 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800810 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811 return 0;
812 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813
814 k.sw_if_index = sw_if_index;
815 k.ip6_address = a[0];
816 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500817
Ed Warnickecb9cada2015-12-08 15:45:58 -0700818 p = mhash_get (&nm->neighbor_index_by_key, &k);
819 if (p == 0)
820 {
821 rv = -1;
822 goto out;
823 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500824
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns19c68d22016-12-07 15:38:14 +0000826 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100827
Neale Rannsb80c5362016-10-08 13:03:40 +0100828 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500829 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100830
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700831
832 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700833 {
834 fib_prefix_t pfx = {
835 .fp_len = 128,
836 .fp_proto = FIB_PROTOCOL_IP6,
837 .fp_addr.ip6 = n->key.ip6_address,
838 };
839 fib_table_entry_path_remove
840 (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
841 &pfx,
842 FIB_SOURCE_ADJ,
Neale Rannsda78f952017-05-24 09:15:43 -0700843 DPO_PROTO_IP6,
Neale Rannsdcd6d622017-05-26 02:59:16 -0700844 &pfx.fp_addr, n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
845 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500847
848out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 return rv;
850}
851
Dave Barachd7cb1b52016-12-09 09:52:16 -0500852static void ip6_neighbor_set_unset_rpc_callback
853 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500855 vlib_main_t *vm = vlib_get_main ();
856 if (a->is_add)
857 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800858 a->link_layer_address, 6, a->is_static,
859 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500861 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
862 a->link_layer_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
865static int
866ip6_neighbor_sort (void *a1, void *a2)
867{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500868 vnet_main_t *vnm = vnet_get_main ();
869 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500871 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
872 n2->key.sw_if_index);
873 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
875 return cmp;
876}
877
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100878ip6_neighbor_t *
879ip6_neighbors_entries (u32 sw_if_index)
880{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500881 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100882 ip6_neighbor_t *n, *ns = 0;
883
884 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500885 pool_foreach (n, nm->neighbor_pool,
886 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100887 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
888 continue;
889 vec_add1 (ns, n[0]);
890 }));
891 /* *INDENT-ON* */
892
893 if (ns)
894 vec_sort_with_function (ns, ip6_neighbor_sort);
895 return ns;
896}
897
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898static clib_error_t *
899show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500900 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500902 vnet_main_t *vnm = vnet_get_main ();
903 ip6_neighbor_t *n, *ns;
904 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 u32 sw_if_index;
906
907 /* Filter entries by interface if given. */
908 sw_if_index = ~0;
909 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
910
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100911 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -0400912 if (ns)
913 {
Billy McFall46529cd2016-10-14 10:45:49 -0400914 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500915 vec_foreach (n, ns)
916 {
917 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -0400918 }
919 vec_free (ns);
920 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
922 return error;
923}
924
Billy McFall0683c9c2016-10-13 08:27:31 -0400925/*?
926 * This command is used to display the adjacent IPv6 hosts found via
927 * neighbor discovery. Optionally, limit the output to the specified
928 * interface.
929 *
930 * @cliexpar
931 * Example of how to display the IPv6 neighbor adjacency table:
932 * @cliexstart{show ip6 neighbors}
933 * Time Address Flags Link layer Interface
934 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
935 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
936 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
937 * @cliexend
938 * Example of how to display the IPv6 neighbor adjacency table for given interface:
939 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
940 * Time Address Flags Link layer Interface
941 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
942 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
943 * @cliexend
944?*/
945/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700946VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
947 .path = "show ip6 neighbors",
948 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -0400949 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950};
Billy McFall0683c9c2016-10-13 08:27:31 -0400951/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952
953static clib_error_t *
954set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500955 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500957 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958 ip6_address_t addr;
959 u8 mac_address[6];
960 int addr_valid = 0;
961 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100962 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800963 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964 u32 sw_if_index;
965
Dave Barachd7cb1b52016-12-09 09:52:16 -0500966 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967 {
968 /* intfc, ip6-address, mac-address */
969 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500970 unformat_vnet_sw_interface, vnm, &sw_if_index,
971 unformat_ip6_address, &addr,
972 unformat_ethernet_address, mac_address))
973 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
975 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500976 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100977 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500978 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800979 else if (unformat (input, "no-fib-entry"))
980 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700981 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500982 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983 }
984
985 if (!addr_valid)
986 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -0500987
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988 if (!is_del)
989 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500990 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800991 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992 else
993 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500994 mac_address, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995 return 0;
996}
997
Billy McFall0683c9c2016-10-13 08:27:31 -0400998/*?
999 * This command is used to manually add an entry to the IPv6 neighbor
1000 * adjacency table. Optionally, the entry can be added as static. It is
1001 * also used to remove an entry from the table. Use the '<em>show ip6
1002 * neighbors</em>' command to display all learned and manually entered entries.
1003 *
1004 * @cliexpar
1005 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
1006 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
1007 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
1008 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
1009?*/
1010/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001011VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
1012{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013 .path = "set ip6 neighbor",
1014 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -04001015 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016};
Billy McFall0683c9c2016-10-13 08:27:31 -04001017/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018
Dave Barachd7cb1b52016-12-09 09:52:16 -05001019typedef enum
1020{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
1022 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
1023 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
1024} icmp6_neighbor_solicitation_or_advertisement_next_t;
1025
1026static_always_inline uword
1027icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
1028 vlib_node_runtime_t * node,
1029 vlib_frame_t * frame,
1030 uword is_solicitation)
1031{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001032 vnet_main_t *vnm = vnet_get_main ();
1033 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001035 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
1037 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001038 vlib_node_runtime_t *error_node =
1039 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 int bogus_length;
1041
1042 from = vlib_frame_vector_args (frame);
1043 n_left_from = n_packets;
1044 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001045
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 if (node->flags & VLIB_NODE_FLAG_TRACE)
1047 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1048 /* stride */ 1,
1049 sizeof (icmp6_input_trace_t));
1050
Dave Barachd7cb1b52016-12-09 09:52:16 -05001051 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 (is_solicitation
1053 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1054 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1055 n_advertisements_sent = 0;
1056
1057 while (n_left_from > 0)
1058 {
1059 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1060
1061 while (n_left_from > 0 && n_left_to_next > 0)
1062 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001063 vlib_buffer_t *p0;
1064 ip6_header_t *ip0;
1065 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1066 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001068 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1069 int is_rewrite0;
1070 u32 ni0;
1071
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 bi0 = to_next[0] = from[0];
1073
1074 from += 1;
1075 to_next += 1;
1076 n_left_from -= 1;
1077 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001078
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079 p0 = vlib_get_buffer (vm, bi0);
1080 ip0 = vlib_buffer_get_current (p0);
1081 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001082 options_len0 =
1083 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
1085 error0 = ICMP6_ERROR_NONE;
1086 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001087 ip6_sadd_link_local =
1088 ip6_address_is_link_local_unicast (&ip0->src_address);
1089 ip6_sadd_unspecified =
1090 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001091
1092 /* Check that source address is unspecified, link-local or else on-link. */
1093 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1094 {
1095 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096
Dave Barachd7cb1b52016-12-09 09:52:16 -05001097 if (ADJ_INDEX_INVALID != src_adj_index0)
1098 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001099 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100
Dave Barachd7cb1b52016-12-09 09:52:16 -05001101 /* Allow all realistic-looking rewrite adjacencies to pass */
1102 ni0 = adj0->lookup_next_index;
1103 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1104 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001105
Dave Barachd7cb1b52016-12-09 09:52:16 -05001106 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1107 || !is_rewrite0)
1108 ?
1109 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1110 : error0);
1111 }
1112 else
1113 {
1114 error0 =
1115 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1116 }
1117 }
1118
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 o0 = (void *) (h0 + 1);
1120 o0 = ((options_len0 == 8 && o0->header.type == option_type
1121 && o0->header.n_data_u64s == 1) ? o0 : 0);
1122
1123 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001124 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001125 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001126 {
1127 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1128 if (nm->limit_neighbor_cache_size &&
1129 pool_elts (nm->neighbor_pool) >=
1130 nm->limit_neighbor_cache_size)
1131 unset_random_neighbor_entry ();
1132 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1133 is_solicitation ?
1134 &ip0->src_address :
1135 &h0->target_address,
1136 o0->ethernet_address,
1137 sizeof (o0->ethernet_address),
Neale Ranns2a3ea492017-04-19 05:24:40 -07001138 0, ip6_sadd_link_local);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001139 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140
1141 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1142 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001143 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001144 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001145 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146
Dave Barachd7cb1b52016-12-09 09:52:16 -05001147 fib_index =
1148 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001150 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001151 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001152 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1153 }
1154 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001155 {
1156 fei = ip6_fib_table_lookup_exact_match (fib_index,
1157 &h0->target_address,
1158 128);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001159
Neale Ranns3f844d02017-02-18 00:03:54 -08001160 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001161 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001162 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001163 error0 =
1164 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001165 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001166 else
1167 {
1168 if (FIB_ENTRY_FLAG_LOCAL &
1169 fib_entry_get_flags_for_source (fei,
1170 FIB_SOURCE_INTERFACE))
1171 {
1172 /* It's an address that belongs to one of our interfaces
1173 * that's good. */
1174 }
1175 else
1176 if (fib_entry_is_sourced
1177 (fei, FIB_SOURCE_IP6_ND_PROXY))
1178 {
1179 /* The address was added by IPv6 Proxy ND config.
1180 * We should only respond to these if the NS arrived on
1181 * the link that has a matching covering prefix */
1182 }
1183 else
1184 {
1185 error0 =
1186 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1187 }
1188 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001189 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001190 }
1191
1192 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001193 next0 = (error0 != ICMP6_ERROR_NONE
1194 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1195 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196 else
1197 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001198 next0 = 0;
1199 error0 = error0 == ICMP6_ERROR_NONE ?
1200 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201 }
1202
1203 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1204 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001205 vnet_sw_interface_t *sw_if0;
1206 ethernet_interface_t *eth_if0;
1207 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
Dave Barachd7cb1b52016-12-09 09:52:16 -05001209 /* dst address is either source address or the all-nodes mcast addr */
1210 if (!ip6_sadd_unspecified)
1211 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001213 ip6_set_reserved_multicast_address (&ip0->dst_address,
1214 IP6_MULTICAST_SCOPE_link_local,
1215 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216
1217 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001218 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219 h0->icmp.type = ICMP6_neighbor_advertisement;
1220
1221 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1222 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001223 eth_if0 =
1224 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225 if (eth_if0 && o0)
1226 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001227 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1228 o0->header.type =
1229 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230 }
1231
1232 h0->advertisement_flags = clib_host_to_net_u32
1233 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1234 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1235
1236 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001237 h0->icmp.checksum =
1238 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1239 &bogus_length);
1240 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241
Dave Barachd7cb1b52016-12-09 09:52:16 -05001242 /* Reuse current MAC header, copy SMAC to DMAC and
1243 * interface MAC to SMAC */
1244 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1245 eth0 = vlib_buffer_get_current (p0);
1246 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1247 if (eth_if0)
1248 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249
Dave Barachd7cb1b52016-12-09 09:52:16 -05001250 /* Setup input and output sw_if_index for packet */
1251 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1252 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1253 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1254 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255
1256 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001257 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258
1259 p0->error = error_node->errors[error0];
1260
1261 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1262 to_next, n_left_to_next,
1263 bi0, next0);
1264 }
1265
1266 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1267 }
1268
1269 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001270 vlib_error_count (vm, error_node->node_index,
1271 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1272 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001273
1274 return frame->n_vectors;
1275}
1276
1277/* for "syslogging" - use elog for now */
1278#define foreach_log_level \
1279 _ (DEBUG, "DEBUG") \
1280 _ (INFO, "INFORMATION") \
1281 _ (NOTICE, "NOTICE") \
1282 _ (WARNING, "WARNING") \
1283 _ (ERR, "ERROR") \
1284 _ (CRIT, "CRITICAL") \
1285 _ (ALERT, "ALERT") \
1286 _ (EMERG, "EMERGENCY")
1287
Dave Barachd7cb1b52016-12-09 09:52:16 -05001288typedef enum
1289{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290#define _(f,s) LOG_##f,
1291 foreach_log_level
1292#undef _
1293} log_level_t;
1294
Dave Barachd7cb1b52016-12-09 09:52:16 -05001295static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296#define _(f,s) s,
1297 foreach_log_level
1298#undef _
1299};
1300
Dave Barachd7cb1b52016-12-09 09:52:16 -05001301static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302
1303static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001304ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001305{
1306 /* just use elog for now */
1307 u8 *what;
1308 va_list va;
1309
Dave Barachd7cb1b52016-12-09 09:52:16 -05001310 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1311 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312
1313 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001314 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001315 {
1316 what = va_format (0, fmt, &va);
1317
Dave Barachd7cb1b52016-12-09 09:52:16 -05001318 ELOG_TYPE_DECLARE (e) =
1319 {
1320 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1321 struct
1322 {
1323 u32 s[2];
1324 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001326 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1327 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001328 }
1329 va_end (va);
1330 return;
1331}
1332
1333/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001334typedef enum
1335{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001336 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1337 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1338 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1339 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1340} icmp6_router_solicitation_or_advertisement_next_t;
1341
1342static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001343icmp6_router_solicitation (vlib_main_t * vm,
1344 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001345{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001346 vnet_main_t *vnm = vnet_get_main ();
1347 ip6_main_t *im = &ip6_main;
1348 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001350 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001352 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001353 int bogus_length;
1354
1355 icmp6_neighbor_discovery_option_type_t option_type;
1356
Dave Barachd7cb1b52016-12-09 09:52:16 -05001357 vlib_node_runtime_t *error_node =
1358 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001359
1360 from = vlib_frame_vector_args (frame);
1361 n_left_from = n_packets;
1362 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001363
Ed Warnickecb9cada2015-12-08 15:45:58 -07001364 if (node->flags & VLIB_NODE_FLAG_TRACE)
1365 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1366 /* stride */ 1,
1367 sizeof (icmp6_input_trace_t));
1368
1369 /* source may append his LL address */
1370 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1371
1372 while (n_left_from > 0)
1373 {
1374 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001375
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376 while (n_left_from > 0 && n_left_to_next > 0)
1377 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001378 vlib_buffer_t *p0;
1379 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001380 ip6_radv_t *radv_info = 0;
1381
Dave Barachd7cb1b52016-12-09 09:52:16 -05001382 icmp6_neighbor_discovery_header_t *h0;
1383 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1384
Ed Warnickecb9cada2015-12-08 15:45:58 -07001385 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001386 u32 is_solicitation = 1, is_dropped = 0;
1387 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388
1389 bi0 = to_next[0] = from[0];
1390
1391 from += 1;
1392 to_next += 1;
1393 n_left_from -= 1;
1394 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001395
Ed Warnickecb9cada2015-12-08 15:45:58 -07001396 p0 = vlib_get_buffer (vm, bi0);
1397 ip0 = vlib_buffer_get_current (p0);
1398 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001399 options_len0 =
1400 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1401 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1402 is_link_local =
1403 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001404
1405 error0 = ICMP6_ERROR_NONE;
1406 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001407
Ed Warnickecb9cada2015-12-08 15:45:58 -07001408 /* check if solicitation (not from nd_timer node) */
1409 if (ip6_address_is_unspecified (&ip0->dst_address))
1410 is_solicitation = 0;
1411
1412 /* Check that source address is unspecified, link-local or else on-link. */
1413 if (!is_unspecified && !is_link_local)
1414 {
1415 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416
Dave Barachd7cb1b52016-12-09 09:52:16 -05001417 if (ADJ_INDEX_INVALID != src_adj_index0)
1418 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001419 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001420
Dave Barachd7cb1b52016-12-09 09:52:16 -05001421 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1422 ?
1423 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1424 : error0);
1425 }
1426 else
1427 {
1428 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1429 }
1430 }
1431
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432 /* check for source LL option and process */
1433 o0 = (void *) (h0 + 1);
1434 o0 = ((options_len0 == 8
1435 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001436 && o0->header.n_data_u64s == 1) ? o0 : 0);
1437
Ed Warnickecb9cada2015-12-08 15:45:58 -07001438 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001439 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1440 !is_unspecified && !is_link_local))
1441 {
1442 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1443 if (nm->limit_neighbor_cache_size &&
1444 pool_elts (nm->neighbor_pool) >=
1445 nm->limit_neighbor_cache_size)
1446 unset_random_neighbor_entry ();
1447
1448 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1449 &ip0->src_address,
1450 o0->ethernet_address,
1451 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001452 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001453 }
1454
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455 /* default is to drop */
1456 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001457
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458 if (error0 == ICMP6_ERROR_NONE)
1459 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001460 vnet_sw_interface_t *sw_if0;
1461 ethernet_interface_t *eth_if0;
1462 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463
1464 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1465 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001466 eth_if0 =
1467 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001468
1469 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001470 error0 =
1471 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1472 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473
1474 if (error0 == ICMP6_ERROR_NONE)
1475 {
1476 u32 ri;
1477
1478 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001479 p0->current_length -=
1480 (options_len0 +
1481 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482
1483 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001484 vec_validate_init_empty
1485 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486
1487 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1488
Dave Barachd7cb1b52016-12-09 09:52:16 -05001489 if (ri != ~0)
1490 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1491
1492 error0 =
1493 ((!radv_info) ?
1494 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1495 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496
1497 if (error0 == ICMP6_ERROR_NONE)
1498 {
1499 f64 now = vlib_time_now (vm);
1500
1501 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001502 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503 {
Neale Ranns75152282017-01-09 01:00:45 -08001504 if (0 != radv_info->last_radv_time &&
1505 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001506 MIN_DELAY_BETWEEN_RAS)
1507 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 else
1509 radv_info->last_radv_time = now;
1510 }
1511
1512 /* send now */
1513 icmp6_router_advertisement_header_t rh;
1514
1515 rh.icmp.type = ICMP6_router_advertisement;
1516 rh.icmp.code = 0;
1517 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001518
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001520 rh.router_lifetime_in_sec =
1521 clib_host_to_net_u16
1522 (radv_info->adv_router_lifetime_in_sec);
1523 rh.
1524 time_in_msec_between_retransmitted_neighbor_solicitations
1525 =
1526 clib_host_to_net_u32 (radv_info->
1527 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1528 rh.neighbor_reachable_time_in_msec =
1529 clib_host_to_net_u32 (radv_info->
1530 adv_neighbor_reachable_time_in_msec);
1531
1532 rh.flags =
1533 (radv_info->adv_managed_flag) ?
1534 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1535 0;
1536 rh.flags |=
1537 ((radv_info->adv_other_flag) ?
1538 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1539 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001540
1541
Dave Barachd7cb1b52016-12-09 09:52:16 -05001542 u16 payload_length =
1543 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544
1545 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001546 vlib_buffer_get_free_list_index
1547 (p0), bi0, (void *) &rh,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001548 sizeof
1549 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001550
Dave Barachd7cb1b52016-12-09 09:52:16 -05001551 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001552 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001553 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1554 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001555
Dave Barachd7cb1b52016-12-09 09:52:16 -05001556 h.header.type =
1557 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558 h.header.n_data_u64s = 1;
1559
1560 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001561 clib_memcpy (&h.ethernet_address[0],
1562 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563
1564 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001565 vlib_buffer_get_free_list_index
1566 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001567 sizeof
1568 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001569
Dave Barachd7cb1b52016-12-09 09:52:16 -05001570 payload_length +=
1571 sizeof
1572 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001573 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001574
Ed Warnickecb9cada2015-12-08 15:45:58 -07001575 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001576 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001577 {
1578 icmp6_neighbor_discovery_mtu_option_t h;
1579
1580 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001581 h.mtu =
1582 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001583 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1584 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001585
1586 payload_length +=
1587 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001588
1589 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001590 vlib_buffer_get_free_list_index
1591 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001592 sizeof
1593 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001595
Ed Warnickecb9cada2015-12-08 15:45:58 -07001596 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001597 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598
Dave Barachd7cb1b52016-12-09 09:52:16 -05001599 /* *INDENT-OFF* */
1600 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1601 ({
1602 if(pr_info->enabled &&
1603 (!pr_info->decrement_lifetime_flag
1604 || (pr_info->pref_lifetime_expires >0)))
1605 {
1606 /* advertise this prefix */
1607 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001608
Dave Barachd7cb1b52016-12-09 09:52:16 -05001609 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1610 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001611
Dave Barachd7cb1b52016-12-09 09:52:16 -05001612 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001613
Dave Barachd7cb1b52016-12-09 09:52:16 -05001614 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1615 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001616
Dave Barachd7cb1b52016-12-09 09:52:16 -05001617 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1618 {
1619 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1620 h.preferred_time = 0;
1621 }
1622 else
1623 {
1624 if(pr_info->decrement_lifetime_flag)
1625 {
1626 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1627 (pr_info->valid_lifetime_expires - now) : 0;
1628
1629 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1630 (pr_info->pref_lifetime_expires - now) : 0;
1631 }
1632
1633 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1634 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1635 }
1636 h.unused = 0;
1637
1638 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1639
1640 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1641
1642 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001643 vlib_buffer_get_free_list_index (p0),
Dave Barachd7cb1b52016-12-09 09:52:16 -05001644 bi0,
1645 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1646
1647 }
1648 }));
1649 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650
1651 /* add additional options before here */
1652
1653 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001654 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001655 {
1656 ip0->dst_address = ip0->src_address;
1657 }
1658 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001659 {
1660 /* target address is all-nodes mcast addr */
1661 ip6_set_reserved_multicast_address
1662 (&ip0->dst_address,
1663 IP6_MULTICAST_SCOPE_link_local,
1664 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001665 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001666
Ed Warnickecb9cada2015-12-08 15:45:58 -07001667 /* source address MUST be the link-local address */
1668 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001669
Dave Barachd7cb1b52016-12-09 09:52:16 -05001670 ip0->hop_limit = 255;
1671 ip0->payload_length =
1672 clib_host_to_net_u16 (payload_length);
1673
1674 icmp6_router_advertisement_header_t *rh0 =
1675 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1676 rh0->icmp.checksum =
1677 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1678 &bogus_length);
1679 ASSERT (bogus_length == 0);
1680
Ed Warnickecb9cada2015-12-08 15:45:58 -07001681 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001682 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001684
1685 if (is_solicitation)
1686 {
1687 ethernet_header_t *eth0;
1688 /* Reuse current MAC header, copy SMAC to DMAC and
1689 * interface MAC to SMAC */
1690 vlib_buffer_reset (p0);
1691 eth0 = vlib_buffer_get_current (p0);
1692 clib_memcpy (eth0->dst_address, eth0->src_address,
1693 6);
1694 clib_memcpy (eth0->src_address, eth_if0->address,
1695 6);
1696 next0 =
1697 is_dropped ? next0 :
1698 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1699 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1700 sw_if_index0;
1701 }
1702 else
1703 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001704 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001705 if (adj_index0 == 0)
1706 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1707 else
1708 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001709 next0 =
1710 is_dropped ? next0 :
1711 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1712 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1713 adj_index0;
1714 }
1715 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001716 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001717
1718 radv_info->n_solicitations_dropped += is_dropped;
1719 radv_info->n_solicitations_rcvd += is_solicitation;
1720
1721 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001722 {
1723 radv_info->n_advertisements_sent++;
1724 n_advertisements_sent++;
1725 }
1726 }
1727 }
1728 }
1729
1730 p0->error = error_node->errors[error0];
1731
Dave Barachd7cb1b52016-12-09 09:52:16 -05001732 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001733 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001734
Ed Warnickecb9cada2015-12-08 15:45:58 -07001735 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1736 to_next, n_left_to_next,
1737 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001738
Ed Warnickecb9cada2015-12-08 15:45:58 -07001739 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001740
Ed Warnickecb9cada2015-12-08 15:45:58 -07001741 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1742 }
1743
1744 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001745 vlib_error_count (vm, error_node->node_index,
1746 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1747 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001748
1749 return frame->n_vectors;
1750}
1751
1752 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1753static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001754icmp6_router_advertisement (vlib_main_t * vm,
1755 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001756{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001757 vnet_main_t *vnm = vnet_get_main ();
1758 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001760 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001761 u32 n_left_from, n_left_to_next, next_index;
1762 u32 n_advertisements_rcvd = 0;
1763
Dave Barachd7cb1b52016-12-09 09:52:16 -05001764 vlib_node_runtime_t *error_node =
1765 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766
1767 from = vlib_frame_vector_args (frame);
1768 n_left_from = n_packets;
1769 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001770
Ed Warnickecb9cada2015-12-08 15:45:58 -07001771 if (node->flags & VLIB_NODE_FLAG_TRACE)
1772 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1773 /* stride */ 1,
1774 sizeof (icmp6_input_trace_t));
1775
1776 while (n_left_from > 0)
1777 {
1778 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001779
Ed Warnickecb9cada2015-12-08 15:45:58 -07001780 while (n_left_from > 0 && n_left_to_next > 0)
1781 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001782 vlib_buffer_t *p0;
1783 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001784 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001785 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001786 u32 bi0, options_len0, sw_if_index0, next0, error0;
1787
1788 bi0 = to_next[0] = from[0];
1789
1790 from += 1;
1791 to_next += 1;
1792 n_left_from -= 1;
1793 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001794
Ed Warnickecb9cada2015-12-08 15:45:58 -07001795 p0 = vlib_get_buffer (vm, bi0);
1796 ip0 = vlib_buffer_get_current (p0);
1797 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001798 options_len0 =
1799 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001800
1801 error0 = ICMP6_ERROR_NONE;
1802 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1803
Dave Barachd7cb1b52016-12-09 09:52:16 -05001804 /* Check that source address is link-local */
1805 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001806 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1807
1808 /* default is to drop */
1809 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001810
Ed Warnickecb9cada2015-12-08 15:45:58 -07001811 n_advertisements_rcvd++;
1812
1813 if (error0 == ICMP6_ERROR_NONE)
1814 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001815 vnet_sw_interface_t *sw_if0;
1816 ethernet_interface_t *eth_if0;
1817
Ed Warnickecb9cada2015-12-08 15:45:58 -07001818 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1819 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001820 eth_if0 =
1821 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001822
1823 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001824 error0 =
1825 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1826 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001827
1828 if (error0 == ICMP6_ERROR_NONE)
1829 {
1830 u32 ri;
1831
1832 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001833 vec_validate_init_empty
1834 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835
1836 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1837
Dave Barachd7cb1b52016-12-09 09:52:16 -05001838 if (ri != ~0)
1839 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1840
1841 error0 =
1842 ((!radv_info) ?
1843 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1844 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001845
1846 if (error0 == ICMP6_ERROR_NONE)
1847 {
1848 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001849 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1850 && (h0->current_hop_limit !=
1851 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001852 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001853 ip6_neighbor_syslog (vm, LOG_WARNING,
1854 "our AdvCurHopLimit on %U doesn't agree with %U",
1855 format_vnet_sw_if_index_name,
1856 vnm, sw_if_index0,
1857 format_ip6_address,
1858 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001859 }
1860
Dave Barachd7cb1b52016-12-09 09:52:16 -05001861 if ((h0->flags &
1862 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1863 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001864 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001865 ip6_neighbor_syslog (vm, LOG_WARNING,
1866 "our AdvManagedFlag on %U doesn't agree with %U",
1867 format_vnet_sw_if_index_name,
1868 vnm, sw_if_index0,
1869 format_ip6_address,
1870 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001871 }
1872
Dave Barachd7cb1b52016-12-09 09:52:16 -05001873 if ((h0->flags &
1874 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1875 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001876 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001877 ip6_neighbor_syslog (vm, LOG_WARNING,
1878 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1879 format_vnet_sw_if_index_name,
1880 vnm, sw_if_index0,
1881 format_ip6_address,
1882 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001883 }
1884
Dave Barachd7cb1b52016-12-09 09:52:16 -05001885 if ((h0->
1886 time_in_msec_between_retransmitted_neighbor_solicitations
1887 && radv_info->
1888 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1889 && (h0->
1890 time_in_msec_between_retransmitted_neighbor_solicitations
1891 !=
1892 clib_host_to_net_u32 (radv_info->
1893 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001894 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001895 ip6_neighbor_syslog (vm, LOG_WARNING,
1896 "our AdvRetransTimer on %U doesn't agree with %U",
1897 format_vnet_sw_if_index_name,
1898 vnm, sw_if_index0,
1899 format_ip6_address,
1900 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001901 }
1902
Dave Barachd7cb1b52016-12-09 09:52:16 -05001903 if ((h0->neighbor_reachable_time_in_msec &&
1904 radv_info->adv_neighbor_reachable_time_in_msec) &&
1905 (h0->neighbor_reachable_time_in_msec !=
1906 clib_host_to_net_u32
1907 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001908 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001909 ip6_neighbor_syslog (vm, LOG_WARNING,
1910 "our AdvReachableTime on %U doesn't agree with %U",
1911 format_vnet_sw_if_index_name,
1912 vnm, sw_if_index0,
1913 format_ip6_address,
1914 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001915 }
1916
1917 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001918 u8 *opt_hdr = (u8 *) (h0 + 1);
1919 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001921 icmp6_neighbor_discovery_option_header_t *o0 =
1922 (icmp6_neighbor_discovery_option_header_t *)
1923 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001924 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001925 icmp6_neighbor_discovery_option_type_t option_type =
1926 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001927
Dave Barachd7cb1b52016-12-09 09:52:16 -05001928 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001929 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001930 ip6_neighbor_syslog (vm, LOG_ERR,
1931 "malformed RA packet on %U from %U",
1932 format_vnet_sw_if_index_name,
1933 vnm, sw_if_index0,
1934 format_ip6_address,
1935 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001936 break;
1937 }
1938
Dave Barachd7cb1b52016-12-09 09:52:16 -05001939 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001940 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001941 ip6_neighbor_syslog (vm, LOG_ERR,
1942 " zero length option in RA on %U from %U",
1943 format_vnet_sw_if_index_name,
1944 vnm, sw_if_index0,
1945 format_ip6_address,
1946 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001947 break;
1948 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001949 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001950 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001951 ip6_neighbor_syslog (vm, LOG_ERR,
1952 "option length in RA packet greater than total length on %U from %U",
1953 format_vnet_sw_if_index_name,
1954 vnm, sw_if_index0,
1955 format_ip6_address,
1956 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001957 break;
1958 }
1959
1960 options_len0 -= opt_len;
1961 opt_hdr += opt_len;
1962
Dave Barachd7cb1b52016-12-09 09:52:16 -05001963 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001964 {
1965 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05001966 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001968 (icmp6_neighbor_discovery_mtu_option_t
1969 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001970
Dave Barachd7cb1b52016-12-09 09:52:16 -05001971 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972 break;
1973
Dave Barachd7cb1b52016-12-09 09:52:16 -05001974 if ((h->mtu && radv_info->adv_link_mtu) &&
1975 (h->mtu !=
1976 clib_host_to_net_u32
1977 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001978 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001979 ip6_neighbor_syslog (vm, LOG_WARNING,
1980 "our AdvLinkMTU on %U doesn't agree with %U",
1981 format_vnet_sw_if_index_name,
1982 vnm, sw_if_index0,
1983 format_ip6_address,
1984 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001985 }
1986 }
1987 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001988
Ed Warnickecb9cada2015-12-08 15:45:58 -07001989 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1990 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001991 icmp6_neighbor_discovery_prefix_information_option_t
1992 * h =
1993 (icmp6_neighbor_discovery_prefix_information_option_t
1994 *) (o0);
1995
Ed Warnickecb9cada2015-12-08 15:45:58 -07001996 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001997 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001998 u32 preferred, valid;
1999
Dave Barachd7cb1b52016-12-09 09:52:16 -05002000 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002001 break;
2002
Dave Barachd7cb1b52016-12-09 09:52:16 -05002003 preferred =
2004 clib_net_to_host_u32 (h->preferred_time);
2005 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002006
2007 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002008 /* *INDENT-OFF* */
2009 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
2010 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011
Dave Barachd7cb1b52016-12-09 09:52:16 -05002012 ip6_address_t mask;
2013 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
2014
2015 if(pr_info->enabled &&
2016 (pr_info->prefix_len == h->dst_address_length) &&
2017 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
2018 {
2019 /* found it */
2020 if(!pr_info->decrement_lifetime_flag &&
2021 valid != pr_info->adv_valid_lifetime_in_secs)
2022 {
2023 ip6_neighbor_syslog(vm, LOG_WARNING,
2024 "our ADV validlifetime on %U for %U does not agree with %U",
2025 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2026 format_ip6_address, &h->dst_address);
2027 }
2028 if(!pr_info->decrement_lifetime_flag &&
2029 preferred != pr_info->adv_pref_lifetime_in_secs)
2030 {
2031 ip6_neighbor_syslog(vm, LOG_WARNING,
2032 "our ADV preferredlifetime on %U for %U does not agree with %U",
2033 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
2034 format_ip6_address, &h->dst_address);
2035 }
2036 }
2037 break;
2038 }));
2039 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002040 break;
2041 }
2042 default:
2043 /* skip this one */
2044 break;
2045 }
2046 }
2047 }
2048 }
2049 }
2050
2051 p0->error = error_node->errors[error0];
2052
Dave Barachd7cb1b52016-12-09 09:52:16 -05002053 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002054 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002055
Ed Warnickecb9cada2015-12-08 15:45:58 -07002056 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2057 to_next, n_left_to_next,
2058 bi0, next0);
2059 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002060
Ed Warnickecb9cada2015-12-08 15:45:58 -07002061 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2062 }
2063
2064 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002065 vlib_error_count (vm, error_node->node_index,
2066 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2067 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002068
2069 return frame->n_vectors;
2070}
2071
Neale Ranns87df12d2017-02-18 08:16:41 -08002072/**
2073 * @brief Add a multicast Address to the advertised MLD set
2074 */
2075static void
2076ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2077{
2078 ip6_mldp_group_t *mcast_group_info;
2079 uword *p;
2080
2081 /* lookup mldp info for this interface */
Neale Rannse0a35442018-02-25 10:39:02 -08002082 p = mhash_get (&radv_info->address_to_mldp_index, addr);
Neale Ranns87df12d2017-02-18 08:16:41 -08002083 mcast_group_info =
2084 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2085
2086 /* add address */
2087 if (!mcast_group_info)
2088 {
2089 /* add */
2090 u32 mi;
2091 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2092
2093 mi = mcast_group_info - radv_info->mldp_group_pool;
Neale Rannse0a35442018-02-25 10:39:02 -08002094 mhash_set (&radv_info->address_to_mldp_index, addr, mi, /* old_value */
Neale Ranns87df12d2017-02-18 08:16:41 -08002095 0);
2096
2097 mcast_group_info->type = 4;
2098 mcast_group_info->mcast_source_address_pool = 0;
2099 mcast_group_info->num_sources = 0;
Neale Rannse0a35442018-02-25 10:39:02 -08002100 clib_memcpy (&mcast_group_info->mcast_address, addr,
Neale Ranns87df12d2017-02-18 08:16:41 -08002101 sizeof (ip6_address_t));
2102 }
2103}
2104
2105/**
2106 * @brief Delete a multicast Address from the advertised MLD set
2107 */
2108static void
2109ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2110{
2111 ip6_mldp_group_t *mcast_group_info;
2112 uword *p;
2113
2114 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2115 mcast_group_info =
2116 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2117
2118 if (mcast_group_info)
2119 {
2120 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2121 /* old_value */ 0);
2122 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2123 }
2124}
2125
2126/**
2127 * @brief Add a multicast Address to the advertised MLD set
2128 */
2129static void
2130ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2131 ip6_multicast_address_scope_t scope,
2132 ip6_multicast_link_local_group_id_t group)
2133{
2134 ip6_address_t addr;
2135
2136 ip6_set_reserved_multicast_address (&addr, scope, group);
2137
2138 ip6_neighbor_add_mld_prefix (a, &addr);
2139}
2140
2141/**
2142 * @brief create and initialize router advertisement parameters with default
2143 * values for this intfc
2144 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002145u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002146ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002147 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002148{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002149 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2150 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002151 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002152 vnet_sw_interface_t *sw_if0;
2153 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002154
2155 /* lookup radv container - ethernet interfaces only */
2156 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002157 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002158 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2159
Dave Barachd7cb1b52016-12-09 09:52:16 -05002160 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002161 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002162
2163 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2164 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002165 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2166
Dave Barachd7cb1b52016-12-09 09:52:16 -05002167 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002168 {
2169 a = pool_elt_at_index (nm->if_radv_pool, ri);
2170
Dave Barachd7cb1b52016-12-09 09:52:16 -05002171 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002172 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002173 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002174 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002175
Neale Ranns32e1c012016-11-22 17:07:28 +00002176 /* release the lock on the interface's mcast adj */
2177 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002178
Neale Ranns87df12d2017-02-18 08:16:41 -08002179 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002180 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002181 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002182 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002183 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002184 }));
2185 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002186 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002187 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002188 }));
2189 /* *INDENT-ON* */
2190
Neale Ranns87df12d2017-02-18 08:16:41 -08002191 pool_free (a->mldp_group_pool);
2192 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002193
Neale Ranns87df12d2017-02-18 08:16:41 -08002194 mhash_free (&a->address_to_prefix_index);
2195 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002196
2197 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002198 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2199 ri = ~0;
2200 }
2201 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002202 else
2203 {
2204 if (is_add)
2205 {
2206 vnet_hw_interface_t *hw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002207
Dave Barachd7cb1b52016-12-09 09:52:16 -05002208 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2209
2210 pool_get (nm->if_radv_pool, a);
2211
2212 ri = a - nm->if_radv_pool;
2213 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2214
2215 /* initialize default values (most of which are zero) */
2216 memset (a, 0, sizeof (a[0]));
2217
2218 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002219 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2220 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2221 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2222 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2223
Neale Ranns87df12d2017-02-18 08:16:41 -08002224 /* send ll address source address option */
2225 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002226
2227 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2228 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2229 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2230 a->seed = (u32) clib_cpu_time_now ();
2231 (void) random_u32 (&a->seed);
2232 a->randomizer = clib_cpu_time_now ();
2233 (void) random_u64 (&a->randomizer);
2234
2235 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2236 a->initial_adverts_sent = a->initial_adverts_count - 1;
2237 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2238
2239 /* deafult is to send */
2240 a->send_radv = 1;
2241
2242 /* fill in radv_info for this interface that will be needed later */
2243 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2244
2245 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2246
2247 /* fill in default link-local address (this may be overridden) */
2248 ip6_link_local_address_from_ethernet_address
2249 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002250
2251 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2252 sizeof (ip6_address_t));
2253 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2254 sizeof (ip6_address_t));
2255
Neale Ranns32e1c012016-11-22 17:07:28 +00002256 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2257 VNET_LINK_IP6,
2258 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002259
2260 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002261 ip6_neighbor_add_mld_grp (a,
2262 IP6_MULTICAST_SCOPE_link_local,
2263 IP6_MULTICAST_GROUP_ID_all_hosts);
2264 ip6_neighbor_add_mld_grp (a,
2265 IP6_MULTICAST_SCOPE_link_local,
2266 IP6_MULTICAST_GROUP_ID_all_routers);
2267 ip6_neighbor_add_mld_grp (a,
2268 IP6_MULTICAST_SCOPE_link_local,
2269 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002270 }
2271 }
2272 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002273}
2274
2275/* send an mldpv2 report */
2276static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002277ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002278{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002279 vnet_main_t *vnm = vnet_get_main ();
2280 vlib_main_t *vm = vnm->vlib_main;
2281 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2282 vnet_sw_interface_t *sw_if0;
2283 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002284 u32 ri;
2285 int bogus_length;
2286
Dave Barachd7cb1b52016-12-09 09:52:16 -05002287 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002288 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002289 vlib_buffer_t *b0;
2290 ip6_header_t *ip0;
2291 u32 *to_next;
2292 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002293 u32 bo0;
2294 u32 n_to_alloc = 1;
2295 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002296
Ed Warnickecb9cada2015-12-08 15:45:58 -07002297 icmp6_multicast_listener_report_header_t *rh0;
2298 icmp6_multicast_listener_report_packet_t *rp0;
2299
2300 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2301 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2302 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2303
2304 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2305 return;
2306
2307 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002308 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2309 ~0);
2310
Ed Warnickecb9cada2015-12-08 15:45:58 -07002311 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002312
2313 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002314 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002315
Ed Warnickecb9cada2015-12-08 15:45:58 -07002316 /* send report now - build a mldpv2 report packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002317 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2318 &bo0,
2319 n_to_alloc,
2320 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2321 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002322 {
2323 clib_warning ("buffer allocation failure");
2324 return;
2325 }
2326
2327 b0 = vlib_get_buffer (vm, bo0);
2328
2329 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002330 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002331
Dave Barachd7cb1b52016-12-09 09:52:16 -05002332 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002333
2334 b0->error = ICMP6_ERROR_NONE;
2335
2336 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002337 ip0 = (ip6_header_t *) & rp0->ip;
2338 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002339
Dave Barachd7cb1b52016-12-09 09:52:16 -05002340 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2341
2342 ip0->ip_version_traffic_class_and_flow_label =
2343 clib_host_to_net_u32 (0x6 << 28);
2344
2345 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002346 /* for DEBUG - vnet driver won't seem to emit router alerts */
2347 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2348 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002349
Ed Warnickecb9cada2015-12-08 15:45:58 -07002350 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002351
Ed Warnickecb9cada2015-12-08 15:45:58 -07002352 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002353 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2354 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002355
2356 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002357 ip6_set_reserved_multicast_address (&ip0->dst_address,
2358 IP6_MULTICAST_SCOPE_link_local,
2359 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2360
Ed Warnickecb9cada2015-12-08 15:45:58 -07002361 /* add reports here */
2362 ip6_mldp_group_t *m;
2363 int num_addr_records = 0;
2364 icmp6_multicast_address_record_t rr;
2365
2366 /* fill in the hop-by-hop extension header (router alert) info */
2367 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2368 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002369
Ed Warnickecb9cada2015-12-08 15:45:58 -07002370 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2371 rh0->alert.len = 2;
2372 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002373
Ed Warnickecb9cada2015-12-08 15:45:58 -07002374 rh0->pad.type = 1;
2375 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002376
Ed Warnickecb9cada2015-12-08 15:45:58 -07002377 rh0->icmp.checksum = 0;
2378
Dave Barachd7cb1b52016-12-09 09:52:16 -05002379 /* *INDENT-OFF* */
2380 pool_foreach (m, radv_info->mldp_group_pool,
2381 ({
2382 rr.type = m->type;
2383 rr.aux_data_len_u32s = 0;
2384 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2385 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002386
Dave Barachd7cb1b52016-12-09 09:52:16 -05002387 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002388
Dave Barachd7cb1b52016-12-09 09:52:16 -05002389 vlib_buffer_add_data
Damjan Marion072401e2017-07-13 18:53:27 +02002390 (vm, vlib_buffer_get_free_list_index (b0), bo0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002391 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002392
Dave Barachd7cb1b52016-12-09 09:52:16 -05002393 payload_length += sizeof( icmp6_multicast_address_record_t);
2394 }));
2395 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002396
2397 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002398 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2399
Ed Warnickecb9cada2015-12-08 15:45:58 -07002400 /* update lengths */
2401 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2402
Dave Barachd7cb1b52016-12-09 09:52:16 -05002403 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2404 &bogus_length);
2405 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002406
Dave Barachd7cb1b52016-12-09 09:52:16 -05002407 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002408 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002409 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002410 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002411 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002412 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002413
Neale Ranns32e1c012016-11-22 17:07:28 +00002414 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002415 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002416
Neale Ranns32e1c012016-11-22 17:07:28 +00002417 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002418
Ed Warnickecb9cada2015-12-08 15:45:58 -07002419 f = vlib_get_frame_to_node (vm, node->index);
2420 to_next = vlib_frame_vector_args (f);
2421 to_next[0] = bo0;
2422 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002423
Ed Warnickecb9cada2015-12-08 15:45:58 -07002424 vlib_put_frame_to_node (vm, node->index, f);
2425 return;
2426}
2427
Dave Barachd7cb1b52016-12-09 09:52:16 -05002428/* *INDENT-OFF* */
2429VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2430{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002431 .function = icmp6_router_solicitation,
2432 .name = "icmp6-router-solicitation",
2433
2434 .vector_size = sizeof (u32),
2435
2436 .format_trace = format_icmp6_input_trace,
2437
2438 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2439 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002440 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "ip6-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002441 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002442 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2443 },
2444};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002445/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002446
2447/* send a RA or update the timer info etc.. */
2448static uword
2449ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002450 vlib_node_runtime_t * node,
2451 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002452{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002453 vnet_main_t *vnm = vnet_get_main ();
2454 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2455 ip6_radv_t *radv_info;
2456 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002457 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002458 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002459 u32 *to_next = 0;
2460 u32 bo0;
2461 icmp6_router_solicitation_header_t *h0;
2462 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002463 f64 now = vlib_time_now (vm);
2464
2465 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002466 /* *INDENT-OFF* */
2467 pool_foreach (radv_info, nm->if_radv_pool,
2468 ({
2469 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2470 {
2471 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2472 radv_info->next_multicast_time = now;
2473 radv_info->last_multicast_time = now;
2474 radv_info->last_radv_time = 0;
2475 radv_info->all_routers_mcast = 0;
2476 continue;
2477 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002478
Dave Barachd7cb1b52016-12-09 09:52:16 -05002479 /* Make sure that we've joined the all-routers multicast group */
2480 if(!radv_info->all_routers_mcast)
2481 {
2482 /* send MDLP_REPORT_EVENT message */
2483 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2484 radv_info->all_routers_mcast = 1;
2485 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002486
Dave Barachd7cb1b52016-12-09 09:52:16 -05002487 /* is it time to send a multicast RA on this interface? */
2488 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2489 {
2490 u32 n_to_alloc = 1;
2491 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002492
Dave Barachd7cb1b52016-12-09 09:52:16 -05002493 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2494 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002495
Dave Barachd7cb1b52016-12-09 09:52:16 -05002496 /* multicast send - compute next multicast send time */
2497 if( radv_info->initial_adverts_sent > 0)
2498 {
2499 radv_info->initial_adverts_sent--;
2500 if(rfn > radv_info-> initial_adverts_interval)
2501 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002502
Dave Barachd7cb1b52016-12-09 09:52:16 -05002503 /* check to see if we are ceasing to send */
2504 if( radv_info->initial_adverts_sent == 0)
2505 if(radv_info->cease_radv)
2506 radv_info->send_radv = 0;
2507 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002508
Dave Barachd7cb1b52016-12-09 09:52:16 -05002509 radv_info->next_multicast_time = rfn + now;
2510 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002511
Dave Barachd7cb1b52016-12-09 09:52:16 -05002512 /* send advert now - build a "solicted" router advert with unspecified source address */
2513 n_allocated = vlib_buffer_alloc_from_free_list
2514 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002515
Dave Barachd7cb1b52016-12-09 09:52:16 -05002516 if (PREDICT_FALSE(n_allocated == 0))
2517 {
2518 clib_warning ("buffer allocation failure");
2519 continue;
2520 }
2521 b0 = vlib_get_buffer (vm, bo0);
2522 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2523 b0->error = ICMP6_ERROR_NONE;
2524 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2525
2526 h0 = vlib_buffer_get_current (b0);
2527
2528 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2529
2530 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2531 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2532 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2533 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2534 h0->ip.hop_limit = 255;
2535
2536 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2537 h0->ip.src_address.as_u64[0] = 0;
2538 h0->ip.src_address.as_u64[1] = 0;
2539
2540 h0->ip.dst_address.as_u64[0] = 0;
2541 h0->ip.dst_address.as_u64[1] = 0;
2542
2543 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2544
2545 if (PREDICT_FALSE(f == 0))
2546 {
2547 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2548 to_next = vlib_frame_vector_args (f);
2549 n_left_to_next = VLIB_FRAME_SIZE;
2550 n_this_frame = 0;
2551 }
2552
2553 n_this_frame++;
2554 n_left_to_next--;
2555 to_next[0] = bo0;
2556 to_next += 1;
2557
2558 if (PREDICT_FALSE(n_left_to_next == 0))
2559 {
2560 f->n_vectors = n_this_frame;
2561 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2562 f = 0;
2563 }
2564 }
2565 }));
2566 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002567
2568 if (f)
2569 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002570 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002571 f->n_vectors = n_this_frame;
2572 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2573 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002574 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002575}
2576
2577static uword
2578ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2579 vlib_node_runtime_t * node,
2580 vlib_frame_t * frame)
2581{
2582 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002583 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002584
2585 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002586
Ed Warnickecb9cada2015-12-08 15:45:58 -07002587 while (1)
2588 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002589 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07002590
Dave Barachd7cb1b52016-12-09 09:52:16 -05002591 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002592
Dave Barachd7cb1b52016-12-09 09:52:16 -05002593 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002594 {
2595 /* No events found: timer expired. */
2596 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002597 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002598 }
2599 else
2600 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002601 switch (event_type)
2602 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002603
Dave Barachd7cb1b52016-12-09 09:52:16 -05002604 case ICMP6_ND_EVENT_INIT:
2605 break;
2606
2607 case ~0:
2608 break;
2609
2610 default:
2611 ASSERT (0);
2612 }
2613
Ed Warnickecb9cada2015-12-08 15:45:58 -07002614 if (event_data)
2615 _vec_len (event_data) = 0;
2616 }
2617 }
2618 return frame->n_vectors;
2619}
2620
Dave Barachd7cb1b52016-12-09 09:52:16 -05002621/* *INDENT-OFF* */
2622VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2623{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002624 .function = icmp6_router_advertisement,
2625 .name = "icmp6-router-advertisement",
2626
2627 .vector_size = sizeof (u32),
2628
2629 .format_trace = format_icmp6_input_trace,
2630
2631 .n_next_nodes = 1,
2632 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002633 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002634 },
2635};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002636/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002637
2638vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2639
2640 .function = ip6_icmp_neighbor_discovery_event_process,
2641 .name = "ip6-icmp-neighbor-discovery-event-process",
2642 .type = VLIB_NODE_TYPE_PROCESS,
2643};
2644
2645static uword
2646icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002647 vlib_node_runtime_t * node, vlib_frame_t * frame)
2648{
2649 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2650 /* is_solicitation */
2651 1);
2652}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002653
2654static uword
2655icmp6_neighbor_advertisement (vlib_main_t * vm,
2656 vlib_node_runtime_t * node,
2657 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002658{
2659 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2660 /* is_solicitation */
2661 0);
2662}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002663
Dave Barachd7cb1b52016-12-09 09:52:16 -05002664/* *INDENT-OFF* */
2665VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2666{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002667 .function = icmp6_neighbor_solicitation,
2668 .name = "icmp6-neighbor-solicitation",
2669
2670 .vector_size = sizeof (u32),
2671
2672 .format_trace = format_icmp6_input_trace,
2673
2674 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2675 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002676 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002677 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2678 },
2679};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002680/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002681
Dave Barachd7cb1b52016-12-09 09:52:16 -05002682/* *INDENT-OFF* */
2683VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2684{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002685 .function = icmp6_neighbor_advertisement,
2686 .name = "icmp6-neighbor-advertisement",
2687
2688 .vector_size = sizeof (u32),
2689
2690 .format_trace = format_icmp6_input_trace,
2691
2692 .n_next_nodes = 1,
2693 .next_nodes = {
Vijayabhaskar Katamreddyce074122017-11-15 13:50:26 -08002694 [0] = "ip6-drop",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002695 },
2696};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002697/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002698
Dave Barachd7cb1b52016-12-09 09:52:16 -05002699/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002700int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002701ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2702 u8 suppress, u8 managed, u8 other,
2703 u8 ll_option, u8 send_unicast, u8 cease,
2704 u8 use_lifetime, u32 lifetime,
2705 u32 initial_count, u32 initial_interval,
2706 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002707{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002708 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2709 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002710 u32 ri;
2711
2712 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002713 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2714 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002715 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002716 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002717
Dave Barachd7cb1b52016-12-09 09:52:16 -05002718 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002719 {
2720
Dave Barachd7cb1b52016-12-09 09:52:16 -05002721 ip6_radv_t *radv_info;
2722 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002723
Dave Barachd7cb1b52016-12-09 09:52:16 -05002724 if ((max_interval != 0) && (min_interval == 0))
2725 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002726
Dave Barachd7cb1b52016-12-09 09:52:16 -05002727 max_interval =
2728 (max_interval !=
2729 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2730 radv_info->max_radv_interval;
2731 min_interval =
2732 (min_interval !=
2733 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2734 radv_info->min_radv_interval;
2735 lifetime =
2736 (use_lifetime !=
2737 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2738 radv_info->adv_router_lifetime_in_sec;
2739
2740 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002741 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002742 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002743 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002744
2745 if (lifetime <= max_interval)
2746 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002747 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002748
2749 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002750 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002751 if ((min_interval > .75 * max_interval) || (min_interval < 3))
2752 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002753 }
2754
Dave Barachd7cb1b52016-12-09 09:52:16 -05002755 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2756 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2757 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002758
Dave Barachd7cb1b52016-12-09 09:52:16 -05002759 /*
2760 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2761 if "flag" is clear don't change corresponding value
2762 */
2763 radv_info->send_radv =
2764 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2765 radv_info->adv_managed_flag =
2766 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2767 radv_info->adv_other_flag =
2768 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2769 radv_info->adv_link_layer_address =
2770 (ll_option !=
2771 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2772 radv_info->send_unicast =
2773 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2774 radv_info->cease_radv =
2775 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2776
2777 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002778 radv_info->max_radv_interval = max_interval;
2779 radv_info->adv_router_lifetime_in_sec = lifetime;
2780
Dave Barachd7cb1b52016-12-09 09:52:16 -05002781 radv_info->initial_adverts_count =
2782 (initial_count !=
2783 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2784 radv_info->initial_adverts_count;
2785 radv_info->initial_adverts_interval =
2786 (initial_interval !=
2787 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2788 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002789
2790 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002791 if ((cease != 0) && (is_no))
2792 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002793
Dave Barachd7cb1b52016-12-09 09:52:16 -05002794 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2795 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002796 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002797 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002799 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002800}
2801
2802int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002803ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2804 ip6_address_t * prefix_addr, u8 prefix_len,
2805 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2806 u8 no_advertise, u8 off_link, u8 no_autoconfig,
2807 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002808{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002809 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002810 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002811
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812 u32 ri;
2813
2814 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002815 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2816 ~0);
2817
Ed Warnickecb9cada2015-12-08 15:45:58 -07002818 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2819
2820 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002821
2822 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002823 {
2824 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002825 ip6_radv_t *radv_info;
2826 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827
2828 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002829 ip6_radv_prefix_t *prefix;
2830
Ed Warnickecb9cada2015-12-08 15:45:58 -07002831 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002832 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2833
Ed Warnickecb9cada2015-12-08 15:45:58 -07002834 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2835
Dave Barachd7cb1b52016-12-09 09:52:16 -05002836 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002837 {
2838 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002839 if (!prefix)
2840 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2841
2842 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002843 return VNET_API_ERROR_INVALID_VALUE_2;
2844
Dave Barachd7cb1b52016-12-09 09:52:16 -05002845 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002846 /* do specific delete processing here before returning */
2847 /* try to remove from routing table */
2848
Dave Barachd7cb1b52016-12-09 09:52:16 -05002849 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2850 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851 pool_put (radv_info->adv_prefixes_pool, prefix);
2852
Dave Barachd7cb1b52016-12-09 09:52:16 -05002853 radv_info->initial_adverts_sent =
2854 radv_info->initial_adverts_count - 1;
2855 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002856 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002857 radv_info->last_radv_time = 0;
2858 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002859 }
2860
2861 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002862 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002863 {
2864 /* add */
2865 u32 pi;
2866 pool_get (radv_info->adv_prefixes_pool, prefix);
2867 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002868 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2869 /* old_value */ 0);
2870
2871 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2872
Ed Warnickecb9cada2015-12-08 15:45:58 -07002873 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002874 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2875
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002877 prefix->adv_on_link_flag = 1; /* L bit set */
2878 prefix->adv_autonomous_flag = 1; /* A bit set */
2879 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002880 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2881 prefix->enabled = 1;
2882 prefix->decrement_lifetime_flag = 1;
2883 prefix->deprecated_prefix_flag = 1;
2884
Dave Barachd7cb1b52016-12-09 09:52:16 -05002885 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002886 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002887 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002888 /* insert prefix into routing table as a connected prefix */
2889 }
2890
Dave Barachd7cb1b52016-12-09 09:52:16 -05002891 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002892 goto restart;
2893 }
2894 else
2895 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002896
2897 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002898 return VNET_API_ERROR_INVALID_VALUE_2;
2899
Dave Barachd7cb1b52016-12-09 09:52:16 -05002900 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002901 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002902 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002903 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002904 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002905 }
2906
Dave Barachd7cb1b52016-12-09 09:52:16 -05002907 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002908 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002909 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002910 prefix->adv_pref_lifetime_in_secs = ~0;
2911 prefix->decrement_lifetime_flag = 0;
2912 }
2913 else
2914 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002915 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2916 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002918
Ed Warnickecb9cada2015-12-08 15:45:58 -07002919 /* copy remaining */
2920 prefix->enabled = !(no_advertise != 0);
2921 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2922 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2923
Dave Barachd7cb1b52016-12-09 09:52:16 -05002924 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925 /* restart */
2926 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002927 prefix->valid_lifetime_expires =
2928 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002929 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002930
2931 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2932 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002933 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002934 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002935 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002936 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002937}
2938
2939clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05002940ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2941 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002942{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002943 vnet_main_t *vnm = vnet_get_main ();
2944 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2945 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002946 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002947 u8 suppress = 0, managed = 0, other = 0;
2948 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002949 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002950 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2951 0, ra_initial_interval = 0;
2952 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002953
Dave Barachd7cb1b52016-12-09 09:52:16 -05002954 unformat_input_t _line_input, *line_input = &_line_input;
2955 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002956
2957 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002958 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002959 ip6_address_t ip6_addr;
2960 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002961
Ed Warnickecb9cada2015-12-08 15:45:58 -07002962
2963 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002964 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002965 return 0;
2966
2967 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002968 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002969 {
2970
Dave Barachd7cb1b52016-12-09 09:52:16 -05002971 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002972 unformat_vnet_sw_interface, vnm, &sw_if_index))
2973 {
2974 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002975 ethernet_interface_t *eth_if0 = 0;
2976
Ed Warnickecb9cada2015-12-08 15:45:58 -07002977 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002978 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2979 eth_if0 =
2980 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2981
2982 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002983 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002984 error =
2985 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07002986 goto done;
2987 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002988
Ed Warnickecb9cada2015-12-08 15:45:58 -07002989 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002990 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2991 sw_if_index, ~0);
2992
Ed Warnickecb9cada2015-12-08 15:45:58 -07002993 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002994
2995 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002996 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002997 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002998 }
2999 else
3000 {
3001 error = clib_error_return (0, "unknown interface %U'",
3002 format_unformat_error, line_input);
3003 goto done;
3004 }
3005 }
3006 else
3007 {
3008 error = clib_error_return (0, "invalid interface name %U'",
3009 format_unformat_error, line_input);
3010 goto done;
3011 }
3012 }
3013
3014 /* get the rest of the command */
3015 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3016 {
3017 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003018 is_no = 1;
3019 else if (unformat (line_input, "prefix %U/%d",
3020 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003021 {
3022 add_radv_info = 0;
3023 break;
3024 }
3025 else if (unformat (line_input, "ra-managed-config-flag"))
3026 {
3027 managed = 1;
3028 break;
3029 }
3030 else if (unformat (line_input, "ra-other-config-flag"))
3031 {
3032 other = 1;
3033 break;
3034 }
Chris Luke33879c92016-06-28 19:54:21 -04003035 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003036 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003037 {
Chris Luke33879c92016-06-28 19:54:21 -04003038 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003039 break;
3040 }
Chris Luke33879c92016-06-28 19:54:21 -04003041 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05003042 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003043 {
Chris Luke33879c92016-06-28 19:54:21 -04003044 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003045 break;
3046 }
3047 else if (unformat (line_input, "ra-send-unicast"))
3048 {
3049 send_unicast = 1;
3050 break;
3051 }
3052 else if (unformat (line_input, "ra-lifetime"))
3053 {
3054 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003055 {
3056 error = unformat_parse_error (line_input);
3057 goto done;
3058 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003059 use_lifetime = 1;
3060 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003061 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062 else if (unformat (line_input, "ra-initial"))
3063 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003064 if (!unformat
3065 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003066 {
3067 error = unformat_parse_error (line_input);
3068 goto done;
3069 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003070 break;
3071 }
3072 else if (unformat (line_input, "ra-interval"))
3073 {
3074 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003075 {
3076 error = unformat_parse_error (line_input);
3077 goto done;
3078 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003079
3080 if (!unformat (line_input, "%d", &ra_min_interval))
3081 ra_min_interval = 0;
3082 break;
3083 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003084 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003085 {
3086 cease = 1;
3087 break;
3088 }
3089 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003090 {
3091 error = unformat_parse_error (line_input);
3092 goto done;
3093 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003094 }
3095
Dave Barachd7cb1b52016-12-09 09:52:16 -05003096 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003097 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003098 ip6_neighbor_ra_config (vm, sw_if_index,
3099 suppress, managed, other,
3100 suppress_ll_option, send_unicast, cease,
3101 use_lifetime, ra_lifetime,
3102 ra_initial_count, ra_initial_interval,
3103 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003104 }
3105 else
3106 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003107 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003108 u32 pref_lifetime_in_secs = 0;
3109 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003110 u8 no_advertise = 0;
3111 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003112 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003113 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003114
3115 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003116 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003117 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003118 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003119 {
3120 use_prefix_default_values = 1;
3121 break;
3122 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003123 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003124 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003125 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003126 pref_lifetime_in_secs = ~0;
3127 break;
3128 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003129 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3130 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003131 break;
3132 else
3133 break;
3134 }
3135
3136
3137 /* get the rest of the command */
3138 while (!use_prefix_default_values &&
3139 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3140 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003141 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003142 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003143 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003144 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003145 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003146 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003147 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003148 no_onlink = 1;
3149 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003150 {
3151 error = unformat_parse_error (line_input);
3152 goto done;
3153 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003154 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003155
3156 ip6_neighbor_ra_prefix (vm, sw_if_index,
3157 &ip6_addr, addr_len,
3158 use_prefix_default_values,
3159 valid_lifetime_in_secs,
3160 pref_lifetime_in_secs,
3161 no_advertise,
3162 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003163 }
3164
Billy McFalla9a20e72017-02-15 11:39:12 -05003165done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003166 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003167
Ed Warnickecb9cada2015-12-08 15:45:58 -07003168 return error;
3169}
3170
Chris Lukebfd32fd2016-06-24 20:38:06 -04003171static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003172ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003173{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003174 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003175 u32 i;
3176
3177 for (i = 0; i < vec_len (addrs); i++)
3178 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003179 ip_interface_address_t *a =
3180 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3181 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003182
3183 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003184 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003185 }
3186}
3187
Ed Warnickecb9cada2015-12-08 15:45:58 -07003188static clib_error_t *
3189show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003190 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003191{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003192 vnet_main_t *vnm = vnet_get_main ();
3193 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3194 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003195 u32 sw_if_index;
3196
3197 sw_if_index = ~0;
3198
Dave Barachd7cb1b52016-12-09 09:52:16 -05003199 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003200 {
3201 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003202
Dave Barachd7cb1b52016-12-09 09:52:16 -05003203 /* look up the radv_t information for this interface */
3204 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3205 sw_if_index, ~0);
3206
3207 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3208
3209 if (ri != ~0)
3210 {
3211 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3212 ip6_radv_t *radv_info;
3213 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3214
3215 vlib_cli_output (vm, "%U is admin %s\n",
3216 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003217 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003218 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3219 "up" : "down"));
3220
Chris Lukebfd32fd2016-06-24 20:38:06 -04003221 u32 ai;
3222 u32 *link_scope = 0, *global_scope = 0;
3223 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003224 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003225
Dave Barachd7cb1b52016-12-09 09:52:16 -05003226 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3227 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003228 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3229
Dave Barachd7cb1b52016-12-09 09:52:16 -05003230 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003231 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003232 a = pool_elt_at_index (lm->if_address_pool, ai);
3233 ip6_address_t *address =
3234 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003235
Chris Lukebfd32fd2016-06-24 20:38:06 -04003236 if (ip6_address_is_link_local_unicast (address))
3237 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003238 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003239 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003240 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003241 vec_add1 (local_scope, ai);
3242 else
3243 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003244
3245 ai = a->next_this_sw_interface;
3246 }
3247
Dave Barachd7cb1b52016-12-09 09:52:16 -05003248 if (vec_len (link_scope))
3249 {
3250 vlib_cli_output (vm, "\tLink-local address(es):\n");
3251 ip6_print_addrs (vm, link_scope);
3252 vec_free (link_scope);
3253 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003254
Dave Barachd7cb1b52016-12-09 09:52:16 -05003255 if (vec_len (local_scope))
3256 {
3257 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3258 ip6_print_addrs (vm, local_scope);
3259 vec_free (local_scope);
3260 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003261
Dave Barachd7cb1b52016-12-09 09:52:16 -05003262 if (vec_len (global_scope))
3263 {
3264 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3265 ip6_print_addrs (vm, global_scope);
3266 vec_free (global_scope);
3267 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003268
Dave Barachd7cb1b52016-12-09 09:52:16 -05003269 if (vec_len (unknown_scope))
3270 {
3271 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3272 ip6_print_addrs (vm, unknown_scope);
3273 vec_free (unknown_scope);
3274 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003275
Ed Warnickecb9cada2015-12-08 15:45:58 -07003276 vlib_cli_output (vm, "\tJoined group address(es):\n");
3277 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003278 /* *INDENT-OFF* */
3279 pool_foreach (m, radv_info->mldp_group_pool,
3280 ({
3281 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3282 &m->mcast_address);
3283 }));
3284 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003285
3286 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003287 ip6_radv_prefix_t *p;
3288 /* *INDENT-OFF* */
3289 pool_foreach (p, radv_info->adv_prefixes_pool,
3290 ({
3291 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3292 format_ip6_address, &p->prefix, p->prefix_len);
3293 }));
3294 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003295
Dave Barachd7cb1b52016-12-09 09:52:16 -05003296 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003297 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3298 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3299 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3300 vlib_cli_output (vm, "\tND DAD is disabled\n");
3301 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3302 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3303 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003304 vlib_cli_output (vm,
3305 "\tND advertised retransmit interval is %d (msec)\n",
3306 radv_info->
3307 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003308
3309 u32 ra_interval = radv_info->max_radv_interval;
3310 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003311 vlib_cli_output (vm,
3312 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003313 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003314 vlib_cli_output (vm,
3315 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003316 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003317 vlib_cli_output (vm,
3318 "\tHosts %s stateless autoconfig for addresses\n",
3319 (radv_info->adv_managed_flag) ? "use" :
3320 " don't use");
3321 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3322 radv_info->n_advertisements_sent);
3323 vlib_cli_output (vm, "\tND router solicitations received %d\n",
3324 radv_info->n_solicitations_rcvd);
3325 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3326 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003327 }
3328 else
3329 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04003330 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003331 format_unformat_error, input);
3332
3333 }
3334 }
3335 return error;
3336}
3337
Billy McFall0683c9c2016-10-13 08:27:31 -04003338/*?
3339 * This command is used to display various IPv6 attributes on a given
3340 * interface.
3341 *
3342 * @cliexpar
3343 * Example of how to display IPv6 settings:
3344 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3345 * GigabitEthernet2/0/0 is admin up
3346 * Link-local address(es):
3347 * fe80::ab8/64
3348 * Joined group address(es):
3349 * ff02::1
3350 * ff02::2
3351 * ff02::16
3352 * ff02::1:ff00:ab8
3353 * Advertised Prefixes:
3354 * prefix fe80::fe:28ff:fe9c:75b3, length 64
3355 * MTU is 1500
3356 * ICMP error messages are unlimited
3357 * ICMP redirects are disabled
3358 * ICMP unreachables are not sent
3359 * ND DAD is disabled
3360 * ND advertised reachable time is 0
3361 * ND advertised retransmit interval is 0 (msec)
3362 * ND router advertisements are sent every 200 seconds (min interval is 150)
3363 * ND router advertisements live for 600 seconds
3364 * Hosts use stateless autoconfig for addresses
3365 * ND router advertisements sent 19336
3366 * ND router solicitations received 0
3367 * ND router solicitations dropped 0
3368 * @cliexend
3369 * Example of output if IPv6 is not enabled on the interface:
3370 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3371 * show ip6 interface: IPv6 not enabled on interface
3372 * @cliexend
3373?*/
3374/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003375VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3376{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003377 .path = "show ip6 interface",
3378 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003379 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003380};
Billy McFall0683c9c2016-10-13 08:27:31 -04003381/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003382
3383clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003384disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003385{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003386 clib_error_t *error = 0;
3387 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003388 u32 ri;
3389
3390 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003391 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3392 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003393 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003394
Ed Warnickecb9cada2015-12-08 15:45:58 -07003395 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003396 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003397 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003398 vnet_main_t *vnm = vnet_get_main ();
3399 ip6_radv_t *radv_info;
3400
3401 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003402
3403 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01003404 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003405 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003406 {
3407 /* essentially "disables" ipv6 on this interface */
3408 error = ip6_add_del_interface_address (vm, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003409 &radv_info->
Neale Ranns75152282017-01-09 01:00:45 -08003410 link_local_address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003411 1 /* is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003412
Dave Barachd7cb1b52016-12-09 09:52:16 -05003413 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3414 0 /* is_add */ );
Neale Ranns4008ac92017-02-13 23:20:04 -08003415 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003416 }
3417 }
3418 return error;
3419}
3420
3421int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003422ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003423{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003424 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3425 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003426
Dave Barachd7cb1b52016-12-09 09:52:16 -05003427 /* look up the radv_t information for this interface */
3428 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3429 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003430
Dave Barachd7cb1b52016-12-09 09:52:16 -05003431 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07003432
Dave Barachd7cb1b52016-12-09 09:52:16 -05003433 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003434}
3435
Dave Barachd7cb1b52016-12-09 09:52:16 -05003436clib_error_t *
3437enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003438{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003439 clib_error_t *error = 0;
3440 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003441 u32 ri;
3442 int is_add = 1;
3443
3444 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003445 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3446 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003447
Dave Barachd7cb1b52016-12-09 09:52:16 -05003448 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3449
3450 /* if not created yet */
3451 if (ri == ~0)
3452 {
3453 vnet_main_t *vnm = vnet_get_main ();
3454 vnet_sw_interface_t *sw_if0;
3455
3456 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3457 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3458 {
3459 ethernet_interface_t *eth_if0;
3460
3461 eth_if0 =
3462 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3463 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003464 {
3465 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003466 ri =
3467 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003468
Dave Barachd7cb1b52016-12-09 09:52:16 -05003469 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003470 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003471 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003472 ip6_address_t link_local_address;
3473
Dave Barachd7cb1b52016-12-09 09:52:16 -05003474 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003475
Dave Barachd7cb1b52016-12-09 09:52:16 -05003476 ip6_link_local_address_from_ethernet_mac_address
3477 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003478
3479 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02003480 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
3481 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003482 {
3483 /* make up an interface id */
3484 md5_context_t m;
3485 u8 digest[16];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003486
Ed Warnickecb9cada2015-12-08 15:45:58 -07003487 link_local_address.as_u64[0] = radv_info->randomizer;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003488
Ed Warnickecb9cada2015-12-08 15:45:58 -07003489 md5_init (&m);
3490 md5_add (&m, &link_local_address, 16);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003491 md5_finish (&m, digest);
3492
3493 clib_memcpy (&link_local_address, digest, 16);
3494
Ed Warnickecb9cada2015-12-08 15:45:58 -07003495 radv_info->randomizer = link_local_address.as_u64[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003496
3497 link_local_address.as_u64[0] =
3498 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003499 /* clear u bit */
3500 link_local_address.as_u8[8] &= 0xfd;
3501 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003502
Neale Ranns4008ac92017-02-13 23:20:04 -08003503 ip6_mfib_interface_enable_disable (sw_if_index, 1);
3504
Ed Warnickecb9cada2015-12-08 15:45:58 -07003505 /* essentially "enables" ipv6 on this interface */
3506 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003507 &link_local_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003508 128
3509 /* address width */ ,
3510 0 /* is_del */ );
3511
3512 if (error)
3513 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3514 !is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003515 else
3516 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003517 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003518 }
3519 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003520 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003521 }
3522 }
3523 return error;
3524}
3525
3526static clib_error_t *
3527enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003528 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003529{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003530 vnet_main_t *vnm = vnet_get_main ();
3531 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003532 u32 sw_if_index;
3533
3534 sw_if_index = ~0;
3535
Dave Barachd7cb1b52016-12-09 09:52:16 -05003536 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003537 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003538 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003539 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003540 else
3541 {
3542 error = clib_error_return (0, "unknown interface\n'",
3543 format_unformat_error, input);
3544
3545 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003546 return error;
3547}
3548
Billy McFall0683c9c2016-10-13 08:27:31 -04003549/*?
3550 * This command is used to enable IPv6 on a given interface.
3551 *
3552 * @cliexpar
3553 * Example of how enable IPv6 on a given interface:
3554 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3555?*/
3556/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003557VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3558{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003559 .path = "enable ip6 interface",
3560 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003561 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003562};
Billy McFall0683c9c2016-10-13 08:27:31 -04003563/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003564
3565static clib_error_t *
3566disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003567 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003568{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003569 vnet_main_t *vnm = vnet_get_main ();
3570 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003571 u32 sw_if_index;
3572
3573 sw_if_index = ~0;
3574
Dave Barachd7cb1b52016-12-09 09:52:16 -05003575 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003576 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003577 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003578 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003579 else
3580 {
3581 error = clib_error_return (0, "unknown interface\n'",
3582 format_unformat_error, input);
3583
3584 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003585 return error;
3586}
3587
Billy McFall0683c9c2016-10-13 08:27:31 -04003588/*?
3589 * This command is used to disable IPv6 on a given interface.
3590 *
3591 * @cliexpar
3592 * Example of how disable IPv6 on a given interface:
3593 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3594?*/
3595/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003596VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3597{
Billy McFall0683c9c2016-10-13 08:27:31 -04003598 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003599 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003600 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003601};
Billy McFall0683c9c2016-10-13 08:27:31 -04003602/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003603
Billy McFall0683c9c2016-10-13 08:27:31 -04003604/*?
3605 * This command is used to configure the neighbor discovery
3606 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3607 * command to display some of the current neighbor discovery parameters
3608 * on a given interface. This command has three formats:
3609 *
3610 *
3611 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3612 *
3613 * '<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>'
3614 *
3615 * Where:
3616 *
3617 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3618 * router-advertisement messages to use stateful address
3619 * auto-configuration to obtain address information (sets the M-bit).
3620 * Default is the M-bit is not set and the '<em>no</em>' option
3621 * returns it to this default state.
3622 *
3623 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3624 * router-advertisement messages that hosts use stateful auto
3625 * configuration to obtain nonaddress related information (sets
3626 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3627 * option returns it to this default state.
3628 *
3629 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3630 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3631 * router-advertisement messages.
3632 *
3633 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3634 * optional source link-layer address in the ICMPv6 router-advertisement
3635 * messages. Default is to include the optional source link-layer address
3636 * and the '<em>no</em>' option returns it to this default state.
3637 *
3638 * <em>[no] ra-send-unicast</em> - Use the source address of the
3639 * router-solicitation message if availiable. The default is to use
3640 * multicast address of all nodes, and the '<em>no</em>' option returns
3641 * it to this default state.
3642 *
3643 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3644 * default router in ICMPv6 router-advertisement messages. The range is
3645 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3646 * '<em><max-interval></em>'. The default value is 600 seconds and the
3647 * '<em>no</em>' option returns it to this default value.
3648 *
3649 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3650 * router-advertisement messages sent and the interval between each
3651 * message. Range for count is 1 - 3 and default is 3. Range for interval
3652 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3653 * returns both to their default value.
3654 *
3655 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3656 * interval between sending ICMPv6 router-advertisement messages. The
3657 * range for max-interval is from 4 to 200 seconds. min-interval can not
3658 * be more than 75% of max-interval. If not set, min-interval will be
3659 * set to 75% of max-interval. The range for min-interval is from 3 to
3660 * 150 seconds. The '<em>no</em>' option returns both to their default
3661 * value.
3662 *
3663 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3664 * The '<em>no</em>' options implies to start (or restart) sending
3665 * ICMPv6 router-advertisement messages.
3666 *
3667 *
3668 * <b>Format 2 - Prefix Options:</b>
3669 *
3670 * '<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>'
3671 *
3672 * Where:
3673 *
3674 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3675 *
3676 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3677 * length of time in seconds during what the prefix is valid for the purpose of
3678 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3679 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3680 * length of time in seconds during what addresses generated from the prefix remain
3681 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3682 *
3683 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3684 * are inifinte, no timeout.
3685 *
3686 * <em>no-advertise</em> - Do not send full router address in prefix
3687 * advertisement. Default is to advertise (i.e. - This flag is off by default).
3688 *
3689 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3690 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3691 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3692 *
3693 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3694 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3695 *
3696 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3697 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3698 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3699 * the L-bit.
3700 *
3701 *
3702 * <b>Format 3: - Default of Prefix:</b>
3703 *
3704 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3705 *
3706 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3707 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3708 * is ignored and the prefix is deleted.
3709 *
3710 *
3711 * @cliexpar
3712 * Example of how set a router advertisement option:
3713 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3714 * Example of how to add a prefix:
3715 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3716 * Example of how to delete a prefix:
3717 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3718?*/
3719/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003720VLIB_CLI_COMMAND (ip6_nd_command, static) =
3721{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003722 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04003723 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003724 .function = ip6_neighbor_cmd,
3725};
Billy McFall0683c9c2016-10-13 08:27:31 -04003726/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003727
3728clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003729set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08003730 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003731{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003732 clib_error_t *error = 0;
3733 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003734 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003735 ip6_radv_t *radv_info;
3736 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07003737
Dave Barachd7cb1b52016-12-09 09:52:16 -05003738 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003739 {
3740 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003741 return (error = clib_error_return (0, "address not link-local",
3742 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003743 }
3744
3745 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003746 enable_ip6_interface (vm, sw_if_index);
3747
Ed Warnickecb9cada2015-12-08 15:45:58 -07003748 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003749
3750 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003751 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003752 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003753
3754 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003755
Ed Warnickecb9cada2015-12-08 15:45:58 -07003756 /* delete the old one */
3757 error = ip6_add_del_interface_address (vm, sw_if_index,
3758 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08003759 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05003760
3761 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003762 {
3763 /* add the new one */
3764 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08003765 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003766 0 /* is_del */ );
3767
3768 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003769 {
3770 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003771 }
3772 }
3773 }
3774 else
3775 {
3776 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3777 error = clib_error_return (0, "ip6 not enabled for interface",
3778 format_unformat_error);
3779 }
3780 return error;
3781}
Dave Barachd7cb1b52016-12-09 09:52:16 -05003782
Ed Warnickecb9cada2015-12-08 15:45:58 -07003783clib_error_t *
3784set_ip6_link_local_address_cmd (vlib_main_t * vm,
3785 unformat_input_t * input,
3786 vlib_cli_command_t * cmd)
3787{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003788 vnet_main_t *vnm = vnet_get_main ();
3789 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003790 u32 sw_if_index;
3791 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003792
3793 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003794 {
3795 /* get the rest of the command */
3796 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3797 {
Neale Ranns75152282017-01-09 01:00:45 -08003798 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003799 break;
3800 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003801 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003802 }
3803 }
Neale Ranns75152282017-01-09 01:00:45 -08003804 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003805 return error;
3806}
3807
Billy McFall0683c9c2016-10-13 08:27:31 -04003808/*?
3809 * This command is used to assign an IPv6 Link-local address to an
3810 * interface. This command will enable IPv6 on an interface if it
3811 * is not already enabled. Use the '<em>show ip6 interface</em>' command
3812 * to display the assigned Link-local address.
3813 *
3814 * @cliexpar
3815 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08003816 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04003817?*/
3818/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003819VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3820{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003821 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08003822 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003823 .function = set_ip6_link_local_address_cmd,
3824};
Billy McFall0683c9c2016-10-13 08:27:31 -04003825/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003826
Neale Ranns87df12d2017-02-18 08:16:41 -08003827/**
3828 * @brief callback when an interface address is added or deleted
3829 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003830static void
3831ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3832 uword opaque,
3833 u32 sw_if_index,
3834 ip6_address_t * address,
3835 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003836 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003837{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003838 vnet_main_t *vnm = vnet_get_main ();
3839 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003840 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003841 vlib_main_t *vm = vnm->vlib_main;
3842 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003843 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003844
3845 /* create solicited node multicast address for this interface adddress */
3846 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003847
Ed Warnickecb9cada2015-12-08 15:45:58 -07003848 a.as_u8[0xd] = address->as_u8[0xd];
3849 a.as_u8[0xe] = address->as_u8[0xe];
3850 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003851
3852 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003853 {
3854 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003855 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003856
3857 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003858 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3859 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003860 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003861 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003862 {
3863 /* get radv_info */
3864 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3865
3866 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003867 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003868 radv_info->ref_count++;
3869
Neale Ranns87df12d2017-02-18 08:16:41 -08003870 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003871 }
3872 }
3873 else
3874 {
3875
3876 /* delete */
3877 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003878 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3879 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003880 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01003881
Dave Barachd7cb1b52016-12-09 09:52:16 -05003882 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003883 {
3884 /* get radv_info */
3885 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3886
Neale Ranns87df12d2017-02-18 08:16:41 -08003887 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003888
3889 /* if interface up send MLDP "report" */
3890 radv_info->all_routers_mcast = 0;
3891
3892 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003893 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003894 radv_info->ref_count--;
3895 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01003896 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3897 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003898 }
3899}
3900
Dave Barachd7cb1b52016-12-09 09:52:16 -05003901clib_error_t *
3902ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003903{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003904 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003905
3906 nm->limit_neighbor_cache_size = neighbor_limit;
3907 return 0;
3908}
3909
Neale Ranns15002542017-09-10 04:39:11 -07003910static void
3911ip6_neighbor_table_bind (ip6_main_t * im,
3912 uword opaque,
3913 u32 sw_if_index,
3914 u32 new_fib_index, u32 old_fib_index)
3915{
3916 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3917 ip6_neighbor_t *n = NULL;
3918 u32 i, *to_re_add = 0;
3919
3920 /* *INDENT-OFF* */
3921 pool_foreach (n, nm->neighbor_pool,
3922 ({
3923 if (n->key.sw_if_index == sw_if_index)
3924 vec_add1 (to_re_add, n - nm->neighbor_pool);
3925 }));
3926 /* *INDENT-ON* */
3927
3928 for (i = 0; i < vec_len (to_re_add); i++)
3929 {
3930 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
3931 ip6_neighbor_adj_fib_remove (n, old_fib_index);
3932 ip6_neighbor_adj_fib_add (n, new_fib_index);
3933 }
3934 vec_free (to_re_add);
3935}
3936
Dave Barachd7cb1b52016-12-09 09:52:16 -05003937static clib_error_t *
3938ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003939{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003940 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3941 ip6_main_t *im = &ip6_main;
3942
Ed Warnickecb9cada2015-12-08 15:45:58 -07003943 mhash_init (&nm->neighbor_index_by_key,
3944 /* value size */ sizeof (uword),
3945 /* key size */ sizeof (ip6_neighbor_key_t));
3946
Dave Barachd7cb1b52016-12-09 09:52:16 -05003947 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3948 ip6_icmp_neighbor_solicitation_node.index);
3949 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3950 ip6_icmp_neighbor_advertisement_node.index);
3951 icmp6_register_type (vm, ICMP6_router_solicitation,
3952 ip6_icmp_router_solicitation_node.index);
3953 icmp6_register_type (vm, ICMP6_router_advertisement,
3954 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003955
3956 /* handler node for ip6 neighbor discovery events and timers */
3957 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3958
3959 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003960 ip6_add_del_interface_address_callback_t cb;
3961 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3962
Ed Warnickecb9cada2015-12-08 15:45:58 -07003963 /* when an interface address changes... */
3964 cb.function = ip6_neighbor_add_del_interface_address;
3965 cb.function_opaque = 0;
3966 vec_add1 (im->add_del_interface_address_callbacks, cb);
3967
Neale Ranns15002542017-09-10 04:39:11 -07003968 ip6_table_bind_callback_t cbt;
3969 cbt.function = ip6_neighbor_table_bind;
3970 cbt.function_opaque = 0;
3971 vec_add1 (im->table_bind_callbacks, cbt);
3972
Ed Warnickecb9cada2015-12-08 15:45:58 -07003973 mhash_init (&nm->pending_resolutions_by_address,
3974 /* value size */ sizeof (uword),
3975 /* key size */ sizeof (ip6_address_t));
3976
John Lo1edfba92016-08-27 01:11:57 -04003977 mhash_init (&nm->mac_changes_by_address,
3978 /* value size */ sizeof (uword),
3979 /* key size */ sizeof (ip6_address_t));
3980
Ed Warnickecb9cada2015-12-08 15:45:58 -07003981 /* default, configurable */
3982 nm->limit_neighbor_cache_size = 50000;
3983
Eyal Baric125ecc2017-09-20 11:29:17 +03003984 nm->wc_ip6_nd_publisher_node = (uword) ~ 0;
3985
Ed Warnickecb9cada2015-12-08 15:45:58 -07003986#if 0
3987 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003988 vec_validate_init_empty
3989 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003990#endif
3991
Ed Warnickecb9cada2015-12-08 15:45:58 -07003992 return 0;
3993}
3994
3995VLIB_INIT_FUNCTION (ip6_neighbor_init);
3996
3997
Dave Barachd7cb1b52016-12-09 09:52:16 -05003998void
3999vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
4000 void *address_arg,
4001 uword node_index,
4002 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004003{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004004 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4005 ip6_address_t *address = address_arg;
4006 uword *p;
4007 pending_resolution_t *pr;
4008
Ed Warnickecb9cada2015-12-08 15:45:58 -07004009 pool_get (nm->pending_resolutions, pr);
4010
4011 pr->next_index = ~0;
4012 pr->node_index = node_index;
4013 pr->type_opaque = type_opaque;
4014 pr->data = data;
4015
4016 p = mhash_get (&nm->pending_resolutions_by_address, address);
4017 if (p)
4018 {
4019 /* Insert new resolution at the head of the list */
4020 pr->next_index = p[0];
4021 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
4022 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05004023
4024 mhash_set (&nm->pending_resolutions_by_address, address,
4025 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07004026}
4027
Dave Barachd7cb1b52016-12-09 09:52:16 -05004028int
4029vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
4030 void *data_callback,
4031 u32 pid,
4032 void *address_arg,
4033 uword node_index,
4034 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04004035{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004036 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4037 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004038
Eyal Barif9f40652017-04-01 03:55:08 +03004039 /* Try to find an existing entry */
4040 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
4041 u32 *p = first;
4042 pending_resolution_t *mc;
4043 while (p && *p != ~0)
4044 {
4045 mc = pool_elt_at_index (nm->mac_changes, *p);
4046 if (mc->node_index == node_index && mc->type_opaque == type_opaque
4047 && mc->pid == pid)
4048 break;
4049 p = &mc->next_index;
4050 }
4051
4052 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004053 if (is_add)
4054 {
Eyal Barif9f40652017-04-01 03:55:08 +03004055 if (found)
4056 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4057
John Lo1edfba92016-08-27 01:11:57 -04004058 pool_get (nm->mac_changes, mc);
Eyal Barif9f40652017-04-01 03:55:08 +03004059 *mc = (pending_resolution_t)
4060 {
4061 .next_index = ~0,.node_index = node_index,.type_opaque =
4062 type_opaque,.data = data,.data_callback = data_callback,.pid =
4063 pid,};
John Lo1edfba92016-08-27 01:11:57 -04004064
Eyal Barif9f40652017-04-01 03:55:08 +03004065 /* Insert new resolution at the end of the list */
4066 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004067 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004068 p[0] = new_idx;
4069 else
4070 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004071 }
4072 else
4073 {
Eyal Barif9f40652017-04-01 03:55:08 +03004074 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004075 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004076
Eyal Barif9f40652017-04-01 03:55:08 +03004077 /* Clients may need to clean up pool entries, too */
4078 void (*fp) (u32, u8 *) = data_callback;
4079 if (fp)
4080 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04004081
Eyal Barif9f40652017-04-01 03:55:08 +03004082 /* Remove the entry from the list and delete the entry */
4083 *p = mc->next_index;
4084 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004085
Eyal Barif9f40652017-04-01 03:55:08 +03004086 /* Remove from hash if we deleted the last entry */
4087 if (*p == ~0 && p == first)
4088 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004089 }
Eyal Barif9f40652017-04-01 03:55:08 +03004090 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004091}
4092
Dave Barachd7cb1b52016-12-09 09:52:16 -05004093int
4094vnet_ip6_nd_term (vlib_main_t * vm,
4095 vlib_node_runtime_t * node,
4096 vlib_buffer_t * p0,
4097 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004098 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004099{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004100 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4101 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
John Lo1edfba92016-08-27 01:11:57 -04004102
4103 ndh = ip6_next_header (ip);
4104 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4105 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004106 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004107
4108 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4109 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4110 {
4111 u8 *t0 = vlib_add_trace (vm, node, p0,
4112 sizeof (icmp6_input_trace_t));
4113 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4114 }
4115
4116 /* Check if anyone want ND events for L2 BDs */
Eyal Baric125ecc2017-09-20 11:29:17 +03004117 if (PREDICT_FALSE
4118 (nm->wc_ip6_nd_publisher_node != (uword) ~ 0
4119 && !ip6_address_is_link_local_unicast (&ip->src_address)))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004120 {
Eyal Baric125ecc2017-09-20 11:29:17 +03004121 vnet_nd_wc_publish (sw_if_index, eth->src_address, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004122 }
4123
4124 /* Check if MAC entry exsist for solicited target IP */
4125 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4126 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004127 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004128 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004129 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004130
4131 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004132 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004133 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4134 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004135 return 0; /* source link layer address option not present */
4136
John Lo1edfba92016-08-27 01:11:57 -04004137 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004138 macp =
4139 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004140 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004141 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004142 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004143 vlib_node_runtime_t *error_node =
4144 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004145 ip->dst_address = ip->src_address;
4146 ip->src_address = ndh->target_address;
4147 ip->hop_limit = 255;
4148 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004149 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004150 clib_memcpy (opt->ethernet_address, macp, 6);
4151 ndh->icmp.type = ICMP6_neighbor_advertisement;
4152 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004153 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4154 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004155 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004156 ndh->icmp.checksum =
4157 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4158 clib_memcpy (eth->dst_address, eth->src_address, 6);
4159 clib_memcpy (eth->src_address, macp, 6);
4160 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004161 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004162 return 1;
4163 }
John Lo1edfba92016-08-27 01:11:57 -04004164 }
4165
4166 return 0;
4167
4168}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004169
Neale Ranns3f844d02017-02-18 00:03:54 -08004170int
4171ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4172{
4173 u32 fib_index;
4174
4175 fib_prefix_t pfx = {
4176 .fp_len = 128,
4177 .fp_proto = FIB_PROTOCOL_IP6,
4178 .fp_addr = {
4179 .ip6 = *addr,
4180 },
4181 };
4182 ip46_address_t nh = {
4183 .ip6 = *addr,
4184 };
4185
4186 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4187
4188 if (~0 == fib_index)
4189 return VNET_API_ERROR_NO_SUCH_FIB;
4190
4191 if (is_del)
4192 {
4193 fib_table_entry_path_remove (fib_index,
4194 &pfx,
4195 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004196 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004197 &nh,
4198 sw_if_index,
4199 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4200 /* flush the ND cache of this address if it's there */
4201 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4202 sw_if_index, addr, NULL, 0);
4203 }
4204 else
4205 {
4206 fib_table_entry_path_add (fib_index,
4207 &pfx,
4208 FIB_SOURCE_IP6_ND_PROXY,
4209 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004210 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004211 &nh,
4212 sw_if_index,
4213 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4214 }
4215 return (0);
4216}
4217
4218static clib_error_t *
4219set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4220 unformat_input_t * input, vlib_cli_command_t * cmd)
4221{
4222 vnet_main_t *vnm = vnet_get_main ();
4223 clib_error_t *error = 0;
4224 ip6_address_t addr;
4225 u32 sw_if_index;
4226 u8 is_del = 0;
4227
4228 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4229 {
4230 /* get the rest of the command */
4231 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4232 {
4233 if (unformat (input, "%U", unformat_ip6_address, &addr))
4234 break;
4235 else if (unformat (input, "delete") || unformat (input, "del"))
4236 is_del = 1;
4237 else
4238 return (unformat_parse_error (input));
4239 }
4240 }
4241
4242 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4243
4244 return error;
4245}
4246
4247/* *INDENT-OFF* */
4248VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4249{
4250 .path = "set ip6 nd proxy",
4251 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4252 .function = set_ip6_nd_proxy_cmd,
4253};
4254/* *INDENT-ON* */
4255
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004256void
Neale Ranns3be6b282016-12-20 14:24:01 +00004257ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004258{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004259 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4260 ip6_neighbor_t *n;
Neale Rannsc819fc62018-02-16 02:44:05 -08004261 adj_index_t ai;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004262
4263 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004264 pool_foreach (n, nm->neighbor_pool,
4265 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004266 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004267 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004268 adj_nbr_walk_nh6 (sw_if_index,
4269 &n->key.ip6_address,
4270 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004271 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004272 }));
4273 /* *INDENT-ON* */
Neale Rannsc819fc62018-02-16 02:44:05 -08004274
4275 ai = adj_glean_get (FIB_PROTOCOL_IP6, sw_if_index);
4276
4277 if (ADJ_INDEX_INVALID != ai)
4278 adj_glean_update_rewrite (ai);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004279}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004280
John Lo8b81cb42017-06-26 01:40:20 -04004281void
4282send_ip6_na (vlib_main_t * vm, vnet_hw_interface_t * hi)
4283{
4284 ip6_main_t *i6m = &ip6_main;
4285 u32 sw_if_index = hi->sw_if_index;
4286 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4287 if (ip6_addr)
4288 {
4289 clib_warning
4290 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4291 format_ip6_address, ip6_addr, sw_if_index);
4292
4293 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4294 int bogus_length;
4295 u32 bi = 0;
4296 icmp6_neighbor_solicitation_header_t *h =
4297 vlib_packet_template_get_packet (vm,
4298 &i6m->discover_neighbor_packet_template,
4299 &bi);
4300 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4301 IP6_MULTICAST_SCOPE_link_local,
4302 IP6_MULTICAST_GROUP_ID_all_hosts);
4303 h->ip.src_address = ip6_addr[0];
4304 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4305 h->neighbor.target_address = ip6_addr[0];
4306 h->neighbor.advertisement_flags = clib_host_to_net_u32
4307 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4308 clib_memcpy (h->link_layer_option.ethernet_address,
4309 hi->hw_address, vec_len (hi->hw_address));
4310 h->neighbor.icmp.checksum =
4311 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4312 ASSERT (bogus_length == 0);
4313
4314 /* Setup MAC header with IP6 Etype and mcast DMAC */
4315 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
4316 vlib_buffer_advance (b, -sizeof (ethernet_header_t));
4317 ethernet_header_t *e = vlib_buffer_get_current (b);
4318 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
4319 clib_memcpy (e->src_address, hi->hw_address, sizeof (e->src_address));
4320 ip6_multicast_ethernet_address (e->dst_address,
4321 IP6_MULTICAST_GROUP_ID_all_hosts);
4322
4323 /* Send unsolicited ND advertisement packet out the specified interface */
4324 vnet_buffer (b)->sw_if_index[VLIB_RX] =
4325 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
4326 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
4327 u32 *to_next = vlib_frame_vector_args (f);
4328 to_next[0] = bi;
4329 f->n_vectors = 1;
4330 vlib_put_frame_to_node (vm, hi->output_node_index, f);
4331 }
4332}
4333
Dave Barachd7cb1b52016-12-09 09:52:16 -05004334/*
4335 * fd.io coding-style-patch-verification: ON
4336 *
4337 * Local Variables:
4338 * eval: (c-set-style "gnu")
4339 * End:
4340 */