blob: 345ebd34518162fc35b020e11906d524c43b6703 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * ip/ip6_neighbor.c: IP6 neighbor handling
3 *
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/ip/ip.h>
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010019#include <vnet/ip/ip6_neighbor.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ethernet/ethernet.h>
21#include <vppinfra/mhash.h>
22#include <vppinfra/md5.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/adj/adj.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000024#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010025#include <vnet/fib/fib_table.h>
26#include <vnet/fib/ip6_fib.h>
Neale Ranns4008ac92017-02-13 23:20:04 -080027#include <vnet/mfib/ip6_mfib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Billy McFall0683c9c2016-10-13 08:27:31 -040029/**
30 * @file
31 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
32 *
33 * The files contains the API and CLI code for managing IPv6 neighbor
34 * adjacency tables and neighbor discovery logic.
35 */
36
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010037/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#define ETHER_MAC_ADDR_LEN 6
39
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010040/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050041typedef struct
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 /* basic advertised information */
44 ip6_address_t prefix;
45 u8 prefix_len;
46 int adv_on_link_flag;
47 int adv_autonomous_flag;
48 u32 adv_valid_lifetime_in_secs;
49 u32 adv_pref_lifetime_in_secs;
50
51 /* advertised values are computed from these times if decrementing */
52 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050053 f64 pref_lifetime_expires;
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 /* local information */
56 int enabled;
57 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050058 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
60#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
61#define DEF_ADV_VALID_LIFETIME 2592000
62#define DEF_ADV_PREF_LIFETIME 604800
63
64 /* extensions are added here, mobile, DNS etc.. */
65} ip6_radv_prefix_t;
66
67
Dave Barachd7cb1b52016-12-09 09:52:16 -050068typedef struct
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 /* group information */
71 u8 type;
72 ip6_address_t mcast_address;
73 u16 num_sources;
74 ip6_address_t *mcast_source_address_pool;
75} ip6_mldp_group_t;
76
77/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050078typedef struct
79{
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050082 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 int adv_managed_flag;
84 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 u32 adv_neighbor_reachable_time_in_msec;
87 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
88
89 /* mtu option */
90 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050091
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050093 u8 link_layer_address[8];
94 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99 /* Hash table mapping address to index in interface advertised prefix pool. */
100 mhash_t address_to_prefix_index;
101
102 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500103 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 /* Hash table mapping address to index in mldp address pool. */
106 mhash_t address_to_mldp_index;
107
108 /* local information */
109 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500110 int send_radv; /* radv on/off on this interface - set by config */
111 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 int send_unicast;
113 int adv_link_layer_address;
114 int prefix_option;
115 int failed_device_check;
116 int all_routers_mcast;
117 u32 seed;
118 u64 randomizer;
119 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000120 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 /* timing information */
123#define DEF_MAX_RADV_INTERVAL 200
124#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125#define DEF_CURR_HOP_LIMIT 64
126#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
127#define MAX_DEF_RTR_LIFETIME 9000
128
Dave Barachd7cb1b52016-12-09 09:52:16 -0500129#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
130#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
131#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
132#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
133#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 f64 max_radv_interval;
136 f64 min_radv_interval;
137 f64 min_delay_between_radv;
138 f64 max_delay_between_radv;
139 f64 max_rtr_default_lifetime;
140
141 f64 last_radv_time;
142 f64 last_multicast_time;
143 f64 next_multicast_time;
144
145
146 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500147 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 u32 initial_adverts_sent;
149
150 /* stats */
151 u32 n_advertisements_sent;
152 u32 n_solicitations_rcvd;
153 u32 n_solicitations_dropped;
154
155 /* Link local address to use (defaults to underlying physical for logical interfaces */
156 ip6_address_t link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157} ip6_radv_t;
158
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159typedef struct
160{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u32 next_index;
162 uword node_index;
163 uword type_opaque;
164 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400165 /* Used for nd event notification only */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500166 void *data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400167 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168} pending_resolution_t;
169
170
Dave Barachd7cb1b52016-12-09 09:52:16 -0500171typedef struct
172{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500174 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 /* lite beer "glean" adjacency handling */
177 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500178 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
John Lo1edfba92016-08-27 01:11:57 -0400180 /* Mac address change notification */
181 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400183
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barachd7cb1b52016-12-09 09:52:16 -0500186 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 mhash_t neighbor_index_by_key;
189
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
194 /* Neighbor attack mitigation */
195 u32 limit_neighbor_cache_size;
196 u32 neighbor_delete_rotor;
197
198} ip6_neighbor_main_t;
199
Neale Ranns3f844d02017-02-18 00:03:54 -0800200/* ipv6 neighbor discovery - timer/event types */
201typedef enum
202{
203 ICMP6_ND_EVENT_INIT,
204} ip6_icmp_neighbor_discovery_event_type_t;
205
206typedef union
207{
208 u32 add_del_swindex;
209 struct
210 {
211 u32 up_down_swindex;
212 u32 fib_index;
213 } up_down_event;
214} ip6_icmp_neighbor_discovery_event_data_t;
215
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216static ip6_neighbor_main_t ip6_neighbor_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500217static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219static u8 *
220format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
223 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
224 vnet_main_t *vnm = vnet_get_main ();
225 vnet_sw_interface_t *si;
226 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 if (!n)
229 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
230 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100231
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100234
235 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Neale Rannsb3b2de72017-03-08 05:17:22 -0800238 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
239 flags = format (flags, "N");
240
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100242 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
244 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500245 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 format_ethernet_address, n->link_layer_address,
247 format_vnet_sw_interface_name, vnm, si);
248
Dave Barachd7cb1b52016-12-09 09:52:16 -0500249 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 return s;
251}
252
Neale Ranns15002542017-09-10 04:39:11 -0700253static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700254ip6_neighbor_adj_fib_remove (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700255{
256 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
257 {
258 fib_prefix_t pfx = {
259 .fp_len = 128,
260 .fp_proto = FIB_PROTOCOL_IP6,
261 .fp_addr.ip6 = n->key.ip6_address,
262 };
263 fib_table_entry_path_remove (fib_index,
264 &pfx,
265 FIB_SOURCE_ADJ,
266 DPO_PROTO_IP6,
267 &pfx.fp_addr,
268 n->key.sw_if_index, ~0,
269 1, FIB_ROUTE_PATH_FLAG_NONE);
270 }
271}
272
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273static clib_error_t *
274ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500275 u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500277 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
278 ip6_neighbor_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barachd7cb1b52016-12-09 09:52:16 -0500280 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
281 {
282 u32 i, *to_delete = 0;
283
284 /* *INDENT-OFF* */
285 pool_foreach (n, nm->neighbor_pool,
286 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 if (n->key.sw_if_index == sw_if_index)
288 vec_add1 (to_delete, n - nm->neighbor_pool);
289 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500290 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
292 for (i = 0; i < vec_len (to_delete); i++)
293 {
294 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
295 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Ranns15002542017-09-10 04:39:11 -0700296 ip6_neighbor_adj_fib_remove (n,
297 ip6_fib_table_get_index_for_sw_if_index
298 (n->key.sw_if_index));
299 pool_put (nm->neighbor_pool, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 vec_free (to_delete);
302 }
303
304 return 0;
305}
306
307VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
308
Dave Barachd7cb1b52016-12-09 09:52:16 -0500309static void
310unset_random_neighbor_entry (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500312 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
313 vnet_main_t *vnm = vnet_get_main ();
314 vlib_main_t *vm = vnm->vlib_main;
315 ip6_neighbor_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 u32 index;
317
318 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
319 nm->neighbor_delete_rotor = index;
320
321 /* Try again from elt 0, could happen if an intfc goes down */
322 if (index == ~0)
323 {
324 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
325 nm->neighbor_delete_rotor = index;
326 }
327
328 /* Nothing left in the pool */
329 if (index == ~0)
330 return;
331
332 e = pool_elt_at_index (nm->neighbor_pool, index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500335 &e->key.ip6_address,
336 e->link_layer_address,
337 ETHER_MAC_ADDR_LEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338}
339
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340typedef struct
341{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100343 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800344 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 u8 link_layer_address[6];
346 u32 sw_if_index;
347 ip6_address_t addr;
348} ip6_neighbor_set_unset_rpc_args_t;
349
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350static void ip6_neighbor_set_unset_rpc_callback
351 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Dave Barachd7cb1b52016-12-09 09:52:16 -0500353static void set_unset_ip6_neighbor_rpc
354 (vlib_main_t * vm,
355 u32 sw_if_index,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800356 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
357 int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358{
359 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500360 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500361
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 args.sw_if_index = sw_if_index;
363 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100364 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800365 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100366 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800367 if (NULL != link_layer_address)
368 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500369
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500375ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100376{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500377 icmp6_neighbor_solicitation_header_t *h;
378 vnet_main_t *vnm = vnet_get_main ();
379 ip6_main_t *im = &ip6_main;
380 ip_interface_address_t *ia;
381 ip6_address_t *dst, *src;
382 vnet_hw_interface_t *hi;
383 vnet_sw_interface_t *si;
384 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100385 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500386 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100387 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100388
Dave Barachd7cb1b52016-12-09 09:52:16 -0500389 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100390
Dave Barachd7cb1b52016-12-09 09:52:16 -0500391 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100392 dst = &adj->sub_type.nbr.next_hop.ip6;
393
394 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100395 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100396 return;
397 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500398 src = ip6_interface_address_matching_destination (im, dst,
399 adj->rewrite_header.
400 sw_if_index, &ia);
401 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100402 {
403 return;
404 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100405
Dave Barachd7cb1b52016-12-09 09:52:16 -0500406 h = vlib_packet_template_get_packet (vm,
407 &im->discover_neighbor_packet_template,
408 &bi);
Neale Rannsb80c5362016-10-08 13:03:40 +0100409
Dave Barachd7cb1b52016-12-09 09:52:16 -0500410 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100411
412 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
413 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
414 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
415 h->ip.src_address = src[0];
416 h->neighbor.target_address = dst[0];
417
418 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500419 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100420
421 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
423 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100424
425 b = vlib_get_buffer (vm, bi);
426 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500427 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100428
429 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500430 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
431 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100432
433 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500434 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
435 u32 *to_next = vlib_frame_vector_args (f);
436 to_next[0] = bi;
437 f->n_vectors = 1;
438 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100439 }
440}
441
442static void
443ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
444{
445 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
446 ethernet_build_rewrite (vnet_get_main (),
447 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500448 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100449 nbr->link_layer_address));
450}
451
452static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000453ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100454{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500455 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000456
Dave Barachd7cb1b52016-12-09 09:52:16 -0500457 adj_nbr_update_rewrite (ai,
458 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
459 ethernet_build_rewrite (vnet_get_main (),
460 adj->rewrite_header.
461 sw_if_index,
462 adj_get_link_type (ai),
463 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100464}
465
466#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
467{ \
468 k.sw_if_index = sw_if_index; \
469 k.ip6_address = *addr; \
470 k.pad = 0; \
471}
472
473static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500474ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100475{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500476 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
477 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100478 ip6_neighbor_key_t k;
479 uword *p;
480
Dave Barachd7cb1b52016-12-09 09:52:16 -0500481 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100482
483 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500484 if (p)
485 {
486 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
487 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100488
489 return (n);
490}
491
492static adj_walk_rc_t
493ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
494{
495 ip6_neighbor_t *nbr = ctx;
496
497 ip6_nd_mk_complete (ai, nbr);
498
499 return (ADJ_WALK_RC_CONTINUE);
500}
501
502static adj_walk_rc_t
503ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
504{
Neale Ranns19c68d22016-12-07 15:38:14 +0000505 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100506
507 return (ADJ_WALK_RC_CONTINUE);
508}
509
510void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500511ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100512{
513 ip6_neighbor_t *nbr;
514 ip_adjacency_t *adj;
515
516 adj = adj_get (ai);
517
518 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
519
Neale Ranns32e1c012016-11-22 17:07:28 +0000520 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100521 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000522 case IP_LOOKUP_NEXT_ARP:
523 case IP_LOOKUP_NEXT_GLEAN:
524 if (NULL != nbr)
525 {
526 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
527 ip6_nd_mk_complete_walk, nbr);
528 }
529 else
530 {
531 /*
532 * no matching ND entry.
533 * construct the rewrite required to for an ND packet, and stick
534 * that in the adj's pipe to smoke.
535 */
536 adj_nbr_update_rewrite (ai,
537 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
538 ethernet_build_rewrite (vnm,
539 sw_if_index,
540 VNET_LINK_IP6,
541 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
542
543 /*
544 * since the FIB has added this adj for a route, it makes sense it may
545 * want to forward traffic sometime soon. Let's send a speculative ND.
546 * just one. If we were to do periodically that wouldn't be bad either,
547 * but that's more code than i'm prepared to write at this time for
548 * relatively little reward.
549 */
550 ip6_nbr_probe (adj);
551 }
552 break;
553 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700554 {
555 /*
556 * Construct a partial rewrite from the known ethernet mcast dest MAC
557 */
558 u8 *rewrite;
559 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100560
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700561 rewrite = ethernet_build_rewrite (vnm,
562 sw_if_index,
563 adj->ia_link,
564 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000565
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700566 /*
567 * Complete the remaining fields of the adj's rewrite to direct the
568 * complete of the rewrite at switch time by copying in the IP
569 * dst address's bytes.
570 * Ofset is 2 bytes into the desintation address. And we write 4 bytes.
571 */
572 offset = vec_len (rewrite) - 2;
573 adj_mcast_update_rewrite (ai, rewrite, offset, 0xffffffff);
Neale Ranns32e1c012016-11-22 17:07:28 +0000574
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700575 break;
576 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000577 case IP_LOOKUP_NEXT_DROP:
578 case IP_LOOKUP_NEXT_PUNT:
579 case IP_LOOKUP_NEXT_LOCAL:
580 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800581 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000582 case IP_LOOKUP_NEXT_MIDCHAIN:
583 case IP_LOOKUP_NEXT_ICMP_ERROR:
584 case IP_LOOKUP_N_NEXT:
585 ASSERT (0);
586 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100587 }
588}
589
Neale Ranns15002542017-09-10 04:39:11 -0700590
591static void
Neale Ranns6b3a8ef2017-09-11 10:34:33 -0700592ip6_neighbor_adj_fib_add (ip6_neighbor_t * n, u32 fib_index)
Neale Ranns15002542017-09-10 04:39:11 -0700593{
594 fib_prefix_t pfx = {
595 .fp_len = 128,
596 .fp_proto = FIB_PROTOCOL_IP6,
597 .fp_addr.ip6 = n->key.ip6_address,
598 };
599
600 n->fib_entry_index =
601 fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ,
602 FIB_ENTRY_FLAG_ATTACHED,
603 DPO_PROTO_IP6, &pfx.fp_addr,
604 n->key.sw_if_index, ~0, 1, NULL,
605 FIB_ROUTE_PATH_FLAG_NONE);
606}
607
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608int
609vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500610 u32 sw_if_index,
611 ip6_address_t * a,
612 u8 * link_layer_address,
613 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800614 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500616 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500618 ip6_neighbor_t *n = 0;
619 int make_new_nd_cache_entry = 1;
620 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500622 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Damjan Marion586afd72017-04-05 19:18:20 +0200624 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 {
626 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800627 1 /* set new neighbor */ , is_static,
628 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 return 0;
630 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
632 k.sw_if_index = sw_if_index;
633 k.ip6_address = a[0];
634 k.pad = 0;
635
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500637 if (p)
638 {
639 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
640 /* Refuse to over-write static neighbor entry. */
641 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
642 return -2;
643 make_new_nd_cache_entry = 0;
644 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100645
Dave Barachd7cb1b52016-12-09 09:52:16 -0500646 if (make_new_nd_cache_entry)
647 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500648 pool_get (nm->neighbor_pool, n);
649 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
650 /* old value */ 0);
651 n->key = k;
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700652 n->fib_entry_index = FIB_NODE_INDEX_INVALID;
Neale Rannsb80c5362016-10-08 13:03:40 +0100653
Dave Barachd7cb1b52016-12-09 09:52:16 -0500654 clib_memcpy (n->link_layer_address,
655 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100656
Dave Barachd7cb1b52016-12-09 09:52:16 -0500657 /*
658 * create the adj-fib. the entry in the FIB table for and to the peer.
659 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800660 if (!is_no_fib_entry)
661 {
Neale Ranns15002542017-09-10 04:39:11 -0700662 ip6_neighbor_adj_fib_add (n,
663 ip6_fib_table_get_index_for_sw_if_index
664 (n->key.sw_if_index));
Neale Ranns2a3ea492017-04-19 05:24:40 -0700665 }
666 else
667 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800668 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
669 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500670 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100671 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500672 {
673 /*
674 * prevent a DoS attack from the data-plane that
675 * spams us with no-op updates to the MAC address
676 */
677 if (0 == memcmp (n->link_layer_address,
678 link_layer_address, n_bytes_link_layer_address))
679 return -1;
Neale Rannsb80c5362016-10-08 13:03:40 +0100680
Dave Barachd7cb1b52016-12-09 09:52:16 -0500681 clib_memcpy (n->link_layer_address,
682 link_layer_address, n_bytes_link_layer_address);
683 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684
Neale Rannsb80c5362016-10-08 13:03:40 +0100685 /* Update time stamp and flags. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 n->cpu_time_last_updated = clib_cpu_time_now ();
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100687 if (is_static)
688 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100689 else
690 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
691
Neale Rannsb80c5362016-10-08 13:03:40 +0100692 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500693 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
695 /* Customer(s) waiting for this address to be resolved? */
696 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400697 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698 {
John Lo1edfba92016-08-27 01:11:57 -0400699 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500700
701 while (next_index != (u32) ~ 0)
702 {
John Lo1edfba92016-08-27 01:11:57 -0400703 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
704 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500705 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400706 next_index = pr->next_index;
707 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500708 }
John Lo1edfba92016-08-27 01:11:57 -0400709
710 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 }
712
John Lo1edfba92016-08-27 01:11:57 -0400713 /* Customer(s) requesting ND event for this address? */
714 p = mhash_get (&nm->mac_changes_by_address, a);
715 if (p)
716 {
717 next_index = p[0];
718
Dave Barachd7cb1b52016-12-09 09:52:16 -0500719 while (next_index != (u32) ~ 0)
720 {
721 int (*fp) (u32, u8 *, u32, ip6_address_t *);
722 int rv = 1;
723 mc = pool_elt_at_index (nm->mac_changes, next_index);
724 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400725
Dave Barachd7cb1b52016-12-09 09:52:16 -0500726 /* Call the user's data callback, return 1 to suppress dup events */
727 if (fp)
728 rv =
729 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
730 /*
731 * Signal the resolver process, as long as the user
732 * says they want to be notified
733 */
734 if (rv == 0)
735 vlib_process_signal_event (vm, mc->node_index,
736 mc->type_opaque, mc->data);
737 next_index = mc->next_index;
738 }
John Lo1edfba92016-08-27 01:11:57 -0400739 }
740
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 return 0;
742}
743
744int
745vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500746 u32 sw_if_index,
747 ip6_address_t * a,
748 u8 * link_layer_address,
749 uword n_bytes_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500751 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500753 ip6_neighbor_t *n;
754 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 int rv = 0;
756
Damjan Marion586afd72017-04-05 19:18:20 +0200757 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758 {
759 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800760 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761 return 0;
762 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763
764 k.sw_if_index = sw_if_index;
765 k.ip6_address = a[0];
766 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500767
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 p = mhash_get (&nm->neighbor_index_by_key, &k);
769 if (p == 0)
770 {
771 rv = -1;
772 goto out;
773 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500774
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns19c68d22016-12-07 15:38:14 +0000776 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100777
Neale Rannsb80c5362016-10-08 13:03:40 +0100778 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500779 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100780
Neale Rannsd5b6aa12017-05-16 08:46:45 -0700781
782 if (FIB_NODE_INDEX_INVALID != n->fib_entry_index)
Neale Rannsdcd6d622017-05-26 02:59:16 -0700783 {
784 fib_prefix_t pfx = {
785 .fp_len = 128,
786 .fp_proto = FIB_PROTOCOL_IP6,
787 .fp_addr.ip6 = n->key.ip6_address,
788 };
789 fib_table_entry_path_remove
790 (ip6_fib_table_get_index_for_sw_if_index (n->key.sw_if_index),
791 &pfx,
792 FIB_SOURCE_ADJ,
Neale Rannsda78f952017-05-24 09:15:43 -0700793 DPO_PROTO_IP6,
Neale Rannsdcd6d622017-05-26 02:59:16 -0700794 &pfx.fp_addr, n->key.sw_if_index, ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
795 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500797
798out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 return rv;
800}
801
Dave Barachd7cb1b52016-12-09 09:52:16 -0500802static void ip6_neighbor_set_unset_rpc_callback
803 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500805 vlib_main_t *vm = vlib_get_main ();
806 if (a->is_add)
807 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800808 a->link_layer_address, 6, a->is_static,
809 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500811 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
812 a->link_layer_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814
815static int
816ip6_neighbor_sort (void *a1, void *a2)
817{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500818 vnet_main_t *vnm = vnet_get_main ();
819 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700820 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500821 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
822 n2->key.sw_if_index);
823 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
825 return cmp;
826}
827
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100828ip6_neighbor_t *
829ip6_neighbors_entries (u32 sw_if_index)
830{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500831 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100832 ip6_neighbor_t *n, *ns = 0;
833
834 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500835 pool_foreach (n, nm->neighbor_pool,
836 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100837 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
838 continue;
839 vec_add1 (ns, n[0]);
840 }));
841 /* *INDENT-ON* */
842
843 if (ns)
844 vec_sort_with_function (ns, ip6_neighbor_sort);
845 return ns;
846}
847
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848static clib_error_t *
849show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500850 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700851{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500852 vnet_main_t *vnm = vnet_get_main ();
853 ip6_neighbor_t *n, *ns;
854 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855 u32 sw_if_index;
856
857 /* Filter entries by interface if given. */
858 sw_if_index = ~0;
859 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
860
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100861 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -0400862 if (ns)
863 {
Billy McFall46529cd2016-10-14 10:45:49 -0400864 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500865 vec_foreach (n, ns)
866 {
867 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -0400868 }
869 vec_free (ns);
870 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700871
872 return error;
873}
874
Billy McFall0683c9c2016-10-13 08:27:31 -0400875/*?
876 * This command is used to display the adjacent IPv6 hosts found via
877 * neighbor discovery. Optionally, limit the output to the specified
878 * interface.
879 *
880 * @cliexpar
881 * Example of how to display the IPv6 neighbor adjacency table:
882 * @cliexstart{show ip6 neighbors}
883 * Time Address Flags Link layer Interface
884 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
885 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
886 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
887 * @cliexend
888 * Example of how to display the IPv6 neighbor adjacency table for given interface:
889 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
890 * Time Address Flags Link layer Interface
891 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
892 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
893 * @cliexend
894?*/
895/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
897 .path = "show ip6 neighbors",
898 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -0400899 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900};
Billy McFall0683c9c2016-10-13 08:27:31 -0400901/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902
903static clib_error_t *
904set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500905 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500907 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 ip6_address_t addr;
909 u8 mac_address[6];
910 int addr_valid = 0;
911 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100912 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800913 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 u32 sw_if_index;
915
Dave Barachd7cb1b52016-12-09 09:52:16 -0500916 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 {
918 /* intfc, ip6-address, mac-address */
919 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500920 unformat_vnet_sw_interface, vnm, &sw_if_index,
921 unformat_ip6_address, &addr,
922 unformat_ethernet_address, mac_address))
923 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924
925 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500926 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100927 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500928 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800929 else if (unformat (input, "no-fib-entry"))
930 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500932 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933 }
934
935 if (!addr_valid)
936 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -0500937
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 if (!is_del)
939 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800941 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 else
943 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500944 mac_address, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 return 0;
946}
947
Billy McFall0683c9c2016-10-13 08:27:31 -0400948/*?
949 * This command is used to manually add an entry to the IPv6 neighbor
950 * adjacency table. Optionally, the entry can be added as static. It is
951 * also used to remove an entry from the table. Use the '<em>show ip6
952 * neighbors</em>' command to display all learned and manually entered entries.
953 *
954 * @cliexpar
955 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
956 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
957 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
958 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
959?*/
960/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500961VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
962{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963 .path = "set ip6 neighbor",
964 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -0400965 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966};
Billy McFall0683c9c2016-10-13 08:27:31 -0400967/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968
Dave Barachd7cb1b52016-12-09 09:52:16 -0500969typedef enum
970{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
972 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
973 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
974} icmp6_neighbor_solicitation_or_advertisement_next_t;
975
976static_always_inline uword
977icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
978 vlib_node_runtime_t * node,
979 vlib_frame_t * frame,
980 uword is_solicitation)
981{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500982 vnet_main_t *vnm = vnet_get_main ();
983 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500985 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
987 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500988 vlib_node_runtime_t *error_node =
989 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 int bogus_length;
991
992 from = vlib_frame_vector_args (frame);
993 n_left_from = n_packets;
994 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500995
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996 if (node->flags & VLIB_NODE_FLAG_TRACE)
997 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
998 /* stride */ 1,
999 sizeof (icmp6_input_trace_t));
1000
Dave Barachd7cb1b52016-12-09 09:52:16 -05001001 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 (is_solicitation
1003 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
1004 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
1005 n_advertisements_sent = 0;
1006
1007 while (n_left_from > 0)
1008 {
1009 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1010
1011 while (n_left_from > 0 && n_left_to_next > 0)
1012 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001013 vlib_buffer_t *p0;
1014 ip6_header_t *ip0;
1015 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
1016 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001018 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
1019 int is_rewrite0;
1020 u32 ni0;
1021
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 bi0 = to_next[0] = from[0];
1023
1024 from += 1;
1025 to_next += 1;
1026 n_left_from -= 1;
1027 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001028
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 p0 = vlib_get_buffer (vm, bi0);
1030 ip0 = vlib_buffer_get_current (p0);
1031 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001032 options_len0 =
1033 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
1035 error0 = ICMP6_ERROR_NONE;
1036 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001037 ip6_sadd_link_local =
1038 ip6_address_is_link_local_unicast (&ip0->src_address);
1039 ip6_sadd_unspecified =
1040 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041
1042 /* Check that source address is unspecified, link-local or else on-link. */
1043 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1044 {
1045 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
Dave Barachd7cb1b52016-12-09 09:52:16 -05001047 if (ADJ_INDEX_INVALID != src_adj_index0)
1048 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001049 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Dave Barachd7cb1b52016-12-09 09:52:16 -05001051 /* Allow all realistic-looking rewrite adjacencies to pass */
1052 ni0 = adj0->lookup_next_index;
1053 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1054 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001055
Dave Barachd7cb1b52016-12-09 09:52:16 -05001056 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1057 || !is_rewrite0)
1058 ?
1059 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1060 : error0);
1061 }
1062 else
1063 {
1064 error0 =
1065 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1066 }
1067 }
1068
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069 o0 = (void *) (h0 + 1);
1070 o0 = ((options_len0 == 8 && o0->header.type == option_type
1071 && o0->header.n_data_u64s == 1) ? o0 : 0);
1072
1073 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001074 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001075 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001076 {
1077 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1078 if (nm->limit_neighbor_cache_size &&
1079 pool_elts (nm->neighbor_pool) >=
1080 nm->limit_neighbor_cache_size)
1081 unset_random_neighbor_entry ();
1082 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1083 is_solicitation ?
1084 &ip0->src_address :
1085 &h0->target_address,
1086 o0->ethernet_address,
1087 sizeof (o0->ethernet_address),
Neale Ranns2a3ea492017-04-19 05:24:40 -07001088 0, ip6_sadd_link_local);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001089 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090
1091 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1092 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001093 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001094 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001095 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096
Dave Barachd7cb1b52016-12-09 09:52:16 -05001097 fib_index =
1098 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001100 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001101 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001102 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1103 }
1104 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001105 {
1106 fei = ip6_fib_table_lookup_exact_match (fib_index,
1107 &h0->target_address,
1108 128);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001109
Neale Ranns3f844d02017-02-18 00:03:54 -08001110 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001111 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001112 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001113 error0 =
1114 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001115 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001116 else
1117 {
1118 if (FIB_ENTRY_FLAG_LOCAL &
1119 fib_entry_get_flags_for_source (fei,
1120 FIB_SOURCE_INTERFACE))
1121 {
1122 /* It's an address that belongs to one of our interfaces
1123 * that's good. */
1124 }
1125 else
1126 if (fib_entry_is_sourced
1127 (fei, FIB_SOURCE_IP6_ND_PROXY))
1128 {
1129 /* The address was added by IPv6 Proxy ND config.
1130 * We should only respond to these if the NS arrived on
1131 * the link that has a matching covering prefix */
1132 }
1133 else
1134 {
1135 error0 =
1136 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1137 }
1138 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001139 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140 }
1141
1142 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001143 next0 = (error0 != ICMP6_ERROR_NONE
1144 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1145 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146 else
1147 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001148 next0 = 0;
1149 error0 = error0 == ICMP6_ERROR_NONE ?
1150 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151 }
1152
1153 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1154 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001155 vnet_sw_interface_t *sw_if0;
1156 ethernet_interface_t *eth_if0;
1157 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158
Dave Barachd7cb1b52016-12-09 09:52:16 -05001159 /* dst address is either source address or the all-nodes mcast addr */
1160 if (!ip6_sadd_unspecified)
1161 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001163 ip6_set_reserved_multicast_address (&ip0->dst_address,
1164 IP6_MULTICAST_SCOPE_link_local,
1165 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166
1167 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001168 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001169 h0->icmp.type = ICMP6_neighbor_advertisement;
1170
1171 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1172 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001173 eth_if0 =
1174 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 if (eth_if0 && o0)
1176 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001177 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1178 o0->header.type =
1179 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180 }
1181
1182 h0->advertisement_flags = clib_host_to_net_u32
1183 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1184 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1185
1186 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001187 h0->icmp.checksum =
1188 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1189 &bogus_length);
1190 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001191
Dave Barachd7cb1b52016-12-09 09:52:16 -05001192 /* Reuse current MAC header, copy SMAC to DMAC and
1193 * interface MAC to SMAC */
1194 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1195 eth0 = vlib_buffer_get_current (p0);
1196 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1197 if (eth_if0)
1198 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199
Dave Barachd7cb1b52016-12-09 09:52:16 -05001200 /* Setup input and output sw_if_index for packet */
1201 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1202 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1203 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1204 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205
1206 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001207 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
1209 p0->error = error_node->errors[error0];
1210
1211 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1212 to_next, n_left_to_next,
1213 bi0, next0);
1214 }
1215
1216 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1217 }
1218
1219 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001220 vlib_error_count (vm, error_node->node_index,
1221 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1222 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223
1224 return frame->n_vectors;
1225}
1226
1227/* for "syslogging" - use elog for now */
1228#define foreach_log_level \
1229 _ (DEBUG, "DEBUG") \
1230 _ (INFO, "INFORMATION") \
1231 _ (NOTICE, "NOTICE") \
1232 _ (WARNING, "WARNING") \
1233 _ (ERR, "ERROR") \
1234 _ (CRIT, "CRITICAL") \
1235 _ (ALERT, "ALERT") \
1236 _ (EMERG, "EMERGENCY")
1237
Dave Barachd7cb1b52016-12-09 09:52:16 -05001238typedef enum
1239{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001240#define _(f,s) LOG_##f,
1241 foreach_log_level
1242#undef _
1243} log_level_t;
1244
Dave Barachd7cb1b52016-12-09 09:52:16 -05001245static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001246#define _(f,s) s,
1247 foreach_log_level
1248#undef _
1249};
1250
Dave Barachd7cb1b52016-12-09 09:52:16 -05001251static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252
1253static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001254ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255{
1256 /* just use elog for now */
1257 u8 *what;
1258 va_list va;
1259
Dave Barachd7cb1b52016-12-09 09:52:16 -05001260 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1261 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
1263 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001264 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265 {
1266 what = va_format (0, fmt, &va);
1267
Dave Barachd7cb1b52016-12-09 09:52:16 -05001268 ELOG_TYPE_DECLARE (e) =
1269 {
1270 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1271 struct
1272 {
1273 u32 s[2];
1274 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001275 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001276 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1277 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001278 }
1279 va_end (va);
1280 return;
1281}
1282
1283/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001284typedef enum
1285{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1287 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1288 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1289 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1290} icmp6_router_solicitation_or_advertisement_next_t;
1291
1292static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001293icmp6_router_solicitation (vlib_main_t * vm,
1294 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001295{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001296 vnet_main_t *vnm = vnet_get_main ();
1297 ip6_main_t *im = &ip6_main;
1298 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001300 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001301 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001302 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001303 int bogus_length;
1304
1305 icmp6_neighbor_discovery_option_type_t option_type;
1306
Dave Barachd7cb1b52016-12-09 09:52:16 -05001307 vlib_node_runtime_t *error_node =
1308 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309
1310 from = vlib_frame_vector_args (frame);
1311 n_left_from = n_packets;
1312 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001313
Ed Warnickecb9cada2015-12-08 15:45:58 -07001314 if (node->flags & VLIB_NODE_FLAG_TRACE)
1315 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1316 /* stride */ 1,
1317 sizeof (icmp6_input_trace_t));
1318
1319 /* source may append his LL address */
1320 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1321
1322 while (n_left_from > 0)
1323 {
1324 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001325
Ed Warnickecb9cada2015-12-08 15:45:58 -07001326 while (n_left_from > 0 && n_left_to_next > 0)
1327 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001328 vlib_buffer_t *p0;
1329 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001330 ip6_radv_t *radv_info = 0;
1331
Dave Barachd7cb1b52016-12-09 09:52:16 -05001332 icmp6_neighbor_discovery_header_t *h0;
1333 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1334
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001336 u32 is_solicitation = 1, is_dropped = 0;
1337 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001338
1339 bi0 = to_next[0] = from[0];
1340
1341 from += 1;
1342 to_next += 1;
1343 n_left_from -= 1;
1344 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001345
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 p0 = vlib_get_buffer (vm, bi0);
1347 ip0 = vlib_buffer_get_current (p0);
1348 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001349 options_len0 =
1350 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1351 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1352 is_link_local =
1353 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354
1355 error0 = ICMP6_ERROR_NONE;
1356 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001357
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358 /* check if solicitation (not from nd_timer node) */
1359 if (ip6_address_is_unspecified (&ip0->dst_address))
1360 is_solicitation = 0;
1361
1362 /* Check that source address is unspecified, link-local or else on-link. */
1363 if (!is_unspecified && !is_link_local)
1364 {
1365 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366
Dave Barachd7cb1b52016-12-09 09:52:16 -05001367 if (ADJ_INDEX_INVALID != src_adj_index0)
1368 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001369 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001370
Dave Barachd7cb1b52016-12-09 09:52:16 -05001371 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1372 ?
1373 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1374 : error0);
1375 }
1376 else
1377 {
1378 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1379 }
1380 }
1381
Ed Warnickecb9cada2015-12-08 15:45:58 -07001382 /* check for source LL option and process */
1383 o0 = (void *) (h0 + 1);
1384 o0 = ((options_len0 == 8
1385 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001386 && o0->header.n_data_u64s == 1) ? o0 : 0);
1387
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001389 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1390 !is_unspecified && !is_link_local))
1391 {
1392 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1393 if (nm->limit_neighbor_cache_size &&
1394 pool_elts (nm->neighbor_pool) >=
1395 nm->limit_neighbor_cache_size)
1396 unset_random_neighbor_entry ();
1397
1398 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1399 &ip0->src_address,
1400 o0->ethernet_address,
1401 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001402 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001403 }
1404
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405 /* default is to drop */
1406 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001407
Ed Warnickecb9cada2015-12-08 15:45:58 -07001408 if (error0 == ICMP6_ERROR_NONE)
1409 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001410 vnet_sw_interface_t *sw_if0;
1411 ethernet_interface_t *eth_if0;
1412 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001413
1414 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1415 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001416 eth_if0 =
1417 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001418
1419 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001420 error0 =
1421 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1422 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001423
1424 if (error0 == ICMP6_ERROR_NONE)
1425 {
1426 u32 ri;
1427
1428 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001429 p0->current_length -=
1430 (options_len0 +
1431 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
1433 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001434 vec_validate_init_empty
1435 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001436
1437 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1438
Dave Barachd7cb1b52016-12-09 09:52:16 -05001439 if (ri != ~0)
1440 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1441
1442 error0 =
1443 ((!radv_info) ?
1444 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1445 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446
1447 if (error0 == ICMP6_ERROR_NONE)
1448 {
1449 f64 now = vlib_time_now (vm);
1450
1451 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001452 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001453 {
Neale Ranns75152282017-01-09 01:00:45 -08001454 if (0 != radv_info->last_radv_time &&
1455 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001456 MIN_DELAY_BETWEEN_RAS)
1457 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001458 else
1459 radv_info->last_radv_time = now;
1460 }
1461
1462 /* send now */
1463 icmp6_router_advertisement_header_t rh;
1464
1465 rh.icmp.type = ICMP6_router_advertisement;
1466 rh.icmp.code = 0;
1467 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001468
Ed Warnickecb9cada2015-12-08 15:45:58 -07001469 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001470 rh.router_lifetime_in_sec =
1471 clib_host_to_net_u16
1472 (radv_info->adv_router_lifetime_in_sec);
1473 rh.
1474 time_in_msec_between_retransmitted_neighbor_solicitations
1475 =
1476 clib_host_to_net_u32 (radv_info->
1477 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1478 rh.neighbor_reachable_time_in_msec =
1479 clib_host_to_net_u32 (radv_info->
1480 adv_neighbor_reachable_time_in_msec);
1481
1482 rh.flags =
1483 (radv_info->adv_managed_flag) ?
1484 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1485 0;
1486 rh.flags |=
1487 ((radv_info->adv_other_flag) ?
1488 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1489 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001490
1491
Dave Barachd7cb1b52016-12-09 09:52:16 -05001492 u16 payload_length =
1493 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001494
1495 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001496 vlib_buffer_get_free_list_index
1497 (p0), bi0, (void *) &rh,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001498 sizeof
1499 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001500
Dave Barachd7cb1b52016-12-09 09:52:16 -05001501 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001503 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1504 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505
Dave Barachd7cb1b52016-12-09 09:52:16 -05001506 h.header.type =
1507 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 h.header.n_data_u64s = 1;
1509
1510 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001511 clib_memcpy (&h.ethernet_address[0],
1512 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513
1514 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001515 vlib_buffer_get_free_list_index
1516 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001517 sizeof
1518 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519
Dave Barachd7cb1b52016-12-09 09:52:16 -05001520 payload_length +=
1521 sizeof
1522 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001523 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001524
Ed Warnickecb9cada2015-12-08 15:45:58 -07001525 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001526 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001527 {
1528 icmp6_neighbor_discovery_mtu_option_t h;
1529
1530 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001531 h.mtu =
1532 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001533 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1534 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001535
1536 payload_length +=
1537 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001538
1539 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001540 vlib_buffer_get_free_list_index
1541 (p0), bi0, (void *) &h,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001542 sizeof
1543 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001544 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001545
Ed Warnickecb9cada2015-12-08 15:45:58 -07001546 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001547 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001548
Dave Barachd7cb1b52016-12-09 09:52:16 -05001549 /* *INDENT-OFF* */
1550 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1551 ({
1552 if(pr_info->enabled &&
1553 (!pr_info->decrement_lifetime_flag
1554 || (pr_info->pref_lifetime_expires >0)))
1555 {
1556 /* advertise this prefix */
1557 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001558
Dave Barachd7cb1b52016-12-09 09:52:16 -05001559 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1560 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561
Dave Barachd7cb1b52016-12-09 09:52:16 -05001562 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563
Dave Barachd7cb1b52016-12-09 09:52:16 -05001564 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1565 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566
Dave Barachd7cb1b52016-12-09 09:52:16 -05001567 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1568 {
1569 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1570 h.preferred_time = 0;
1571 }
1572 else
1573 {
1574 if(pr_info->decrement_lifetime_flag)
1575 {
1576 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1577 (pr_info->valid_lifetime_expires - now) : 0;
1578
1579 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1580 (pr_info->pref_lifetime_expires - now) : 0;
1581 }
1582
1583 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1584 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1585 }
1586 h.unused = 0;
1587
1588 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1589
1590 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1591
1592 vlib_buffer_add_data (vm,
Damjan Marion072401e2017-07-13 18:53:27 +02001593 vlib_buffer_get_free_list_index (p0),
Dave Barachd7cb1b52016-12-09 09:52:16 -05001594 bi0,
1595 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1596
1597 }
1598 }));
1599 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001600
1601 /* add additional options before here */
1602
1603 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001604 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001605 {
1606 ip0->dst_address = ip0->src_address;
1607 }
1608 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001609 {
1610 /* target address is all-nodes mcast addr */
1611 ip6_set_reserved_multicast_address
1612 (&ip0->dst_address,
1613 IP6_MULTICAST_SCOPE_link_local,
1614 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001615 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001616
Ed Warnickecb9cada2015-12-08 15:45:58 -07001617 /* source address MUST be the link-local address */
1618 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001619
Dave Barachd7cb1b52016-12-09 09:52:16 -05001620 ip0->hop_limit = 255;
1621 ip0->payload_length =
1622 clib_host_to_net_u16 (payload_length);
1623
1624 icmp6_router_advertisement_header_t *rh0 =
1625 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1626 rh0->icmp.checksum =
1627 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1628 &bogus_length);
1629 ASSERT (bogus_length == 0);
1630
Ed Warnickecb9cada2015-12-08 15:45:58 -07001631 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001632 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001633 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001634
1635 if (is_solicitation)
1636 {
1637 ethernet_header_t *eth0;
1638 /* Reuse current MAC header, copy SMAC to DMAC and
1639 * interface MAC to SMAC */
1640 vlib_buffer_reset (p0);
1641 eth0 = vlib_buffer_get_current (p0);
1642 clib_memcpy (eth0->dst_address, eth0->src_address,
1643 6);
1644 clib_memcpy (eth0->src_address, eth_if0->address,
1645 6);
1646 next0 =
1647 is_dropped ? next0 :
1648 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1649 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1650 sw_if_index0;
1651 }
1652 else
1653 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001654 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001655 if (adj_index0 == 0)
1656 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1657 else
1658 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001659 next0 =
1660 is_dropped ? next0 :
1661 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1662 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1663 adj_index0;
1664 }
1665 }
Damjan Marion213b5aa2017-07-13 21:19:27 +02001666 p0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001667
1668 radv_info->n_solicitations_dropped += is_dropped;
1669 radv_info->n_solicitations_rcvd += is_solicitation;
1670
1671 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672 {
1673 radv_info->n_advertisements_sent++;
1674 n_advertisements_sent++;
1675 }
1676 }
1677 }
1678 }
1679
1680 p0->error = error_node->errors[error0];
1681
Dave Barachd7cb1b52016-12-09 09:52:16 -05001682 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001683 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001684
Ed Warnickecb9cada2015-12-08 15:45:58 -07001685 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1686 to_next, n_left_to_next,
1687 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001688
Ed Warnickecb9cada2015-12-08 15:45:58 -07001689 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001690
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1692 }
1693
1694 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001695 vlib_error_count (vm, error_node->node_index,
1696 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1697 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001698
1699 return frame->n_vectors;
1700}
1701
1702 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1703static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001704icmp6_router_advertisement (vlib_main_t * vm,
1705 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001707 vnet_main_t *vnm = vnet_get_main ();
1708 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001709 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001710 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001711 u32 n_left_from, n_left_to_next, next_index;
1712 u32 n_advertisements_rcvd = 0;
1713
Dave Barachd7cb1b52016-12-09 09:52:16 -05001714 vlib_node_runtime_t *error_node =
1715 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001716
1717 from = vlib_frame_vector_args (frame);
1718 n_left_from = n_packets;
1719 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001720
Ed Warnickecb9cada2015-12-08 15:45:58 -07001721 if (node->flags & VLIB_NODE_FLAG_TRACE)
1722 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1723 /* stride */ 1,
1724 sizeof (icmp6_input_trace_t));
1725
1726 while (n_left_from > 0)
1727 {
1728 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001729
Ed Warnickecb9cada2015-12-08 15:45:58 -07001730 while (n_left_from > 0 && n_left_to_next > 0)
1731 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001732 vlib_buffer_t *p0;
1733 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001734 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001735 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001736 u32 bi0, options_len0, sw_if_index0, next0, error0;
1737
1738 bi0 = to_next[0] = from[0];
1739
1740 from += 1;
1741 to_next += 1;
1742 n_left_from -= 1;
1743 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001744
Ed Warnickecb9cada2015-12-08 15:45:58 -07001745 p0 = vlib_get_buffer (vm, bi0);
1746 ip0 = vlib_buffer_get_current (p0);
1747 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001748 options_len0 =
1749 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001750
1751 error0 = ICMP6_ERROR_NONE;
1752 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1753
Dave Barachd7cb1b52016-12-09 09:52:16 -05001754 /* Check that source address is link-local */
1755 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001756 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1757
1758 /* default is to drop */
1759 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001760
Ed Warnickecb9cada2015-12-08 15:45:58 -07001761 n_advertisements_rcvd++;
1762
1763 if (error0 == ICMP6_ERROR_NONE)
1764 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001765 vnet_sw_interface_t *sw_if0;
1766 ethernet_interface_t *eth_if0;
1767
Ed Warnickecb9cada2015-12-08 15:45:58 -07001768 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1769 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001770 eth_if0 =
1771 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001772
1773 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001774 error0 =
1775 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1776 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001777
1778 if (error0 == ICMP6_ERROR_NONE)
1779 {
1780 u32 ri;
1781
1782 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001783 vec_validate_init_empty
1784 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001785
1786 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1787
Dave Barachd7cb1b52016-12-09 09:52:16 -05001788 if (ri != ~0)
1789 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1790
1791 error0 =
1792 ((!radv_info) ?
1793 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1794 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001795
1796 if (error0 == ICMP6_ERROR_NONE)
1797 {
1798 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001799 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1800 && (h0->current_hop_limit !=
1801 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001802 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001803 ip6_neighbor_syslog (vm, LOG_WARNING,
1804 "our AdvCurHopLimit on %U doesn't agree with %U",
1805 format_vnet_sw_if_index_name,
1806 vnm, sw_if_index0,
1807 format_ip6_address,
1808 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001809 }
1810
Dave Barachd7cb1b52016-12-09 09:52:16 -05001811 if ((h0->flags &
1812 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1813 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001814 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001815 ip6_neighbor_syslog (vm, LOG_WARNING,
1816 "our AdvManagedFlag on %U doesn't agree with %U",
1817 format_vnet_sw_if_index_name,
1818 vnm, sw_if_index0,
1819 format_ip6_address,
1820 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001821 }
1822
Dave Barachd7cb1b52016-12-09 09:52:16 -05001823 if ((h0->flags &
1824 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1825 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001826 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001827 ip6_neighbor_syslog (vm, LOG_WARNING,
1828 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1829 format_vnet_sw_if_index_name,
1830 vnm, sw_if_index0,
1831 format_ip6_address,
1832 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001833 }
1834
Dave Barachd7cb1b52016-12-09 09:52:16 -05001835 if ((h0->
1836 time_in_msec_between_retransmitted_neighbor_solicitations
1837 && radv_info->
1838 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1839 && (h0->
1840 time_in_msec_between_retransmitted_neighbor_solicitations
1841 !=
1842 clib_host_to_net_u32 (radv_info->
1843 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001844 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001845 ip6_neighbor_syslog (vm, LOG_WARNING,
1846 "our AdvRetransTimer on %U doesn't agree with %U",
1847 format_vnet_sw_if_index_name,
1848 vnm, sw_if_index0,
1849 format_ip6_address,
1850 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001851 }
1852
Dave Barachd7cb1b52016-12-09 09:52:16 -05001853 if ((h0->neighbor_reachable_time_in_msec &&
1854 radv_info->adv_neighbor_reachable_time_in_msec) &&
1855 (h0->neighbor_reachable_time_in_msec !=
1856 clib_host_to_net_u32
1857 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001858 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001859 ip6_neighbor_syslog (vm, LOG_WARNING,
1860 "our AdvReachableTime on %U doesn't agree with %U",
1861 format_vnet_sw_if_index_name,
1862 vnm, sw_if_index0,
1863 format_ip6_address,
1864 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001865 }
1866
1867 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001868 u8 *opt_hdr = (u8 *) (h0 + 1);
1869 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001870 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001871 icmp6_neighbor_discovery_option_header_t *o0 =
1872 (icmp6_neighbor_discovery_option_header_t *)
1873 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001874 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001875 icmp6_neighbor_discovery_option_type_t option_type =
1876 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001877
Dave Barachd7cb1b52016-12-09 09:52:16 -05001878 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001879 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001880 ip6_neighbor_syslog (vm, LOG_ERR,
1881 "malformed RA packet on %U from %U",
1882 format_vnet_sw_if_index_name,
1883 vnm, sw_if_index0,
1884 format_ip6_address,
1885 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 break;
1887 }
1888
Dave Barachd7cb1b52016-12-09 09:52:16 -05001889 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001890 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001891 ip6_neighbor_syslog (vm, LOG_ERR,
1892 " zero length option in RA on %U from %U",
1893 format_vnet_sw_if_index_name,
1894 vnm, sw_if_index0,
1895 format_ip6_address,
1896 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001897 break;
1898 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001899 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001900 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001901 ip6_neighbor_syslog (vm, LOG_ERR,
1902 "option length in RA packet greater than total length on %U from %U",
1903 format_vnet_sw_if_index_name,
1904 vnm, sw_if_index0,
1905 format_ip6_address,
1906 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001907 break;
1908 }
1909
1910 options_len0 -= opt_len;
1911 opt_hdr += opt_len;
1912
Dave Barachd7cb1b52016-12-09 09:52:16 -05001913 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001914 {
1915 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05001916 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001918 (icmp6_neighbor_discovery_mtu_option_t
1919 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920
Dave Barachd7cb1b52016-12-09 09:52:16 -05001921 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001922 break;
1923
Dave Barachd7cb1b52016-12-09 09:52:16 -05001924 if ((h->mtu && radv_info->adv_link_mtu) &&
1925 (h->mtu !=
1926 clib_host_to_net_u32
1927 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001928 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001929 ip6_neighbor_syslog (vm, LOG_WARNING,
1930 "our AdvLinkMTU on %U doesn't agree with %U",
1931 format_vnet_sw_if_index_name,
1932 vnm, sw_if_index0,
1933 format_ip6_address,
1934 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001935 }
1936 }
1937 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001938
Ed Warnickecb9cada2015-12-08 15:45:58 -07001939 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1940 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001941 icmp6_neighbor_discovery_prefix_information_option_t
1942 * h =
1943 (icmp6_neighbor_discovery_prefix_information_option_t
1944 *) (o0);
1945
Ed Warnickecb9cada2015-12-08 15:45:58 -07001946 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001947 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001948 u32 preferred, valid;
1949
Dave Barachd7cb1b52016-12-09 09:52:16 -05001950 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001951 break;
1952
Dave Barachd7cb1b52016-12-09 09:52:16 -05001953 preferred =
1954 clib_net_to_host_u32 (h->preferred_time);
1955 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001956
1957 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001958 /* *INDENT-OFF* */
1959 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1960 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001961
Dave Barachd7cb1b52016-12-09 09:52:16 -05001962 ip6_address_t mask;
1963 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1964
1965 if(pr_info->enabled &&
1966 (pr_info->prefix_len == h->dst_address_length) &&
1967 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1968 {
1969 /* found it */
1970 if(!pr_info->decrement_lifetime_flag &&
1971 valid != pr_info->adv_valid_lifetime_in_secs)
1972 {
1973 ip6_neighbor_syslog(vm, LOG_WARNING,
1974 "our ADV validlifetime on %U for %U does not agree with %U",
1975 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1976 format_ip6_address, &h->dst_address);
1977 }
1978 if(!pr_info->decrement_lifetime_flag &&
1979 preferred != pr_info->adv_pref_lifetime_in_secs)
1980 {
1981 ip6_neighbor_syslog(vm, LOG_WARNING,
1982 "our ADV preferredlifetime on %U for %U does not agree with %U",
1983 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1984 format_ip6_address, &h->dst_address);
1985 }
1986 }
1987 break;
1988 }));
1989 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001990 break;
1991 }
1992 default:
1993 /* skip this one */
1994 break;
1995 }
1996 }
1997 }
1998 }
1999 }
2000
2001 p0->error = error_node->errors[error0];
2002
Dave Barachd7cb1b52016-12-09 09:52:16 -05002003 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002004 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002005
Ed Warnickecb9cada2015-12-08 15:45:58 -07002006 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2007 to_next, n_left_to_next,
2008 bi0, next0);
2009 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002010
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2012 }
2013
2014 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002015 vlib_error_count (vm, error_node->node_index,
2016 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
2017 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002018
2019 return frame->n_vectors;
2020}
2021
Neale Ranns87df12d2017-02-18 08:16:41 -08002022/**
2023 * @brief Add a multicast Address to the advertised MLD set
2024 */
2025static void
2026ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2027{
2028 ip6_mldp_group_t *mcast_group_info;
2029 uword *p;
2030
2031 /* lookup mldp info for this interface */
2032 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2033 mcast_group_info =
2034 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2035
2036 /* add address */
2037 if (!mcast_group_info)
2038 {
2039 /* add */
2040 u32 mi;
2041 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2042
2043 mi = mcast_group_info - radv_info->mldp_group_pool;
2044 mhash_set (&radv_info->address_to_mldp_index, &addr, mi, /* old_value */
2045 0);
2046
2047 mcast_group_info->type = 4;
2048 mcast_group_info->mcast_source_address_pool = 0;
2049 mcast_group_info->num_sources = 0;
2050 clib_memcpy (&mcast_group_info->mcast_address, &addr,
2051 sizeof (ip6_address_t));
2052 }
2053}
2054
2055/**
2056 * @brief Delete a multicast Address from the advertised MLD set
2057 */
2058static void
2059ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2060{
2061 ip6_mldp_group_t *mcast_group_info;
2062 uword *p;
2063
2064 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2065 mcast_group_info =
2066 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2067
2068 if (mcast_group_info)
2069 {
2070 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2071 /* old_value */ 0);
2072 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2073 }
2074}
2075
2076/**
2077 * @brief Add a multicast Address to the advertised MLD set
2078 */
2079static void
2080ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2081 ip6_multicast_address_scope_t scope,
2082 ip6_multicast_link_local_group_id_t group)
2083{
2084 ip6_address_t addr;
2085
2086 ip6_set_reserved_multicast_address (&addr, scope, group);
2087
2088 ip6_neighbor_add_mld_prefix (a, &addr);
2089}
2090
2091/**
2092 * @brief create and initialize router advertisement parameters with default
2093 * values for this intfc
2094 */
Pavel Kotucek9f5a2b62017-06-14 13:56:55 +02002095u32
Ed Warnickecb9cada2015-12-08 15:45:58 -07002096ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002097 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002098{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002099 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2100 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002101 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002102 vnet_sw_interface_t *sw_if0;
2103 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002104
2105 /* lookup radv container - ethernet interfaces only */
2106 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002108 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2109
Dave Barachd7cb1b52016-12-09 09:52:16 -05002110 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002111 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002112
2113 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2114 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002115 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2116
Dave Barachd7cb1b52016-12-09 09:52:16 -05002117 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002118 {
2119 a = pool_elt_at_index (nm->if_radv_pool, ri);
2120
Dave Barachd7cb1b52016-12-09 09:52:16 -05002121 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002122 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002123 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002124 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002125
Neale Ranns32e1c012016-11-22 17:07:28 +00002126 /* release the lock on the interface's mcast adj */
2127 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002128
Neale Ranns87df12d2017-02-18 08:16:41 -08002129 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002130 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002131 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002132 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002133 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002134 }));
2135 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002136 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002137 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002138 }));
2139 /* *INDENT-ON* */
2140
Neale Ranns87df12d2017-02-18 08:16:41 -08002141 pool_free (a->mldp_group_pool);
2142 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002143
Neale Ranns87df12d2017-02-18 08:16:41 -08002144 mhash_free (&a->address_to_prefix_index);
2145 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002146
2147 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002148 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2149 ri = ~0;
2150 }
2151 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002152 else
2153 {
2154 if (is_add)
2155 {
2156 vnet_hw_interface_t *hw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002157
Dave Barachd7cb1b52016-12-09 09:52:16 -05002158 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2159
2160 pool_get (nm->if_radv_pool, a);
2161
2162 ri = a - nm->if_radv_pool;
2163 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2164
2165 /* initialize default values (most of which are zero) */
2166 memset (a, 0, sizeof (a[0]));
2167
2168 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002169 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2170 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2171 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2172 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2173
Neale Ranns87df12d2017-02-18 08:16:41 -08002174 /* send ll address source address option */
2175 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002176
2177 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2178 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2179 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2180 a->seed = (u32) clib_cpu_time_now ();
2181 (void) random_u32 (&a->seed);
2182 a->randomizer = clib_cpu_time_now ();
2183 (void) random_u64 (&a->randomizer);
2184
2185 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2186 a->initial_adverts_sent = a->initial_adverts_count - 1;
2187 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2188
2189 /* deafult is to send */
2190 a->send_radv = 1;
2191
2192 /* fill in radv_info for this interface that will be needed later */
2193 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2194
2195 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2196
2197 /* fill in default link-local address (this may be overridden) */
2198 ip6_link_local_address_from_ethernet_address
2199 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002200
2201 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2202 sizeof (ip6_address_t));
2203 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2204 sizeof (ip6_address_t));
2205
Neale Ranns32e1c012016-11-22 17:07:28 +00002206 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2207 VNET_LINK_IP6,
2208 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002209
2210 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002211 ip6_neighbor_add_mld_grp (a,
2212 IP6_MULTICAST_SCOPE_link_local,
2213 IP6_MULTICAST_GROUP_ID_all_hosts);
2214 ip6_neighbor_add_mld_grp (a,
2215 IP6_MULTICAST_SCOPE_link_local,
2216 IP6_MULTICAST_GROUP_ID_all_routers);
2217 ip6_neighbor_add_mld_grp (a,
2218 IP6_MULTICAST_SCOPE_link_local,
2219 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002220 }
2221 }
2222 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002223}
2224
2225/* send an mldpv2 report */
2226static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002227ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002228{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002229 vnet_main_t *vnm = vnet_get_main ();
2230 vlib_main_t *vm = vnm->vlib_main;
2231 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2232 vnet_sw_interface_t *sw_if0;
2233 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002234 u32 ri;
2235 int bogus_length;
2236
Dave Barachd7cb1b52016-12-09 09:52:16 -05002237 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002238 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002239 vlib_buffer_t *b0;
2240 ip6_header_t *ip0;
2241 u32 *to_next;
2242 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002243 u32 bo0;
2244 u32 n_to_alloc = 1;
2245 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002246
Ed Warnickecb9cada2015-12-08 15:45:58 -07002247 icmp6_multicast_listener_report_header_t *rh0;
2248 icmp6_multicast_listener_report_packet_t *rp0;
2249
2250 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2251 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2252 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2253
2254 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2255 return;
2256
2257 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002258 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2259 ~0);
2260
Ed Warnickecb9cada2015-12-08 15:45:58 -07002261 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002262
2263 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002264 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002265
Ed Warnickecb9cada2015-12-08 15:45:58 -07002266 /* send report now - build a mldpv2 report packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002267 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2268 &bo0,
2269 n_to_alloc,
2270 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2271 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272 {
2273 clib_warning ("buffer allocation failure");
2274 return;
2275 }
2276
2277 b0 = vlib_get_buffer (vm, bo0);
2278
2279 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002280 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002281
Dave Barachd7cb1b52016-12-09 09:52:16 -05002282 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002283
2284 b0->error = ICMP6_ERROR_NONE;
2285
2286 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002287 ip0 = (ip6_header_t *) & rp0->ip;
2288 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002289
Dave Barachd7cb1b52016-12-09 09:52:16 -05002290 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2291
2292 ip0->ip_version_traffic_class_and_flow_label =
2293 clib_host_to_net_u32 (0x6 << 28);
2294
2295 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002296 /* for DEBUG - vnet driver won't seem to emit router alerts */
2297 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2298 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002299
Ed Warnickecb9cada2015-12-08 15:45:58 -07002300 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002301
Ed Warnickecb9cada2015-12-08 15:45:58 -07002302 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002303 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2304 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002305
2306 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002307 ip6_set_reserved_multicast_address (&ip0->dst_address,
2308 IP6_MULTICAST_SCOPE_link_local,
2309 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2310
Ed Warnickecb9cada2015-12-08 15:45:58 -07002311 /* add reports here */
2312 ip6_mldp_group_t *m;
2313 int num_addr_records = 0;
2314 icmp6_multicast_address_record_t rr;
2315
2316 /* fill in the hop-by-hop extension header (router alert) info */
2317 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2318 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002319
Ed Warnickecb9cada2015-12-08 15:45:58 -07002320 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2321 rh0->alert.len = 2;
2322 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002323
Ed Warnickecb9cada2015-12-08 15:45:58 -07002324 rh0->pad.type = 1;
2325 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002326
Ed Warnickecb9cada2015-12-08 15:45:58 -07002327 rh0->icmp.checksum = 0;
2328
Dave Barachd7cb1b52016-12-09 09:52:16 -05002329 /* *INDENT-OFF* */
2330 pool_foreach (m, radv_info->mldp_group_pool,
2331 ({
2332 rr.type = m->type;
2333 rr.aux_data_len_u32s = 0;
2334 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2335 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002336
Dave Barachd7cb1b52016-12-09 09:52:16 -05002337 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002338
Dave Barachd7cb1b52016-12-09 09:52:16 -05002339 vlib_buffer_add_data
Damjan Marion072401e2017-07-13 18:53:27 +02002340 (vm, vlib_buffer_get_free_list_index (b0), bo0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002341 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002342
Dave Barachd7cb1b52016-12-09 09:52:16 -05002343 payload_length += sizeof( icmp6_multicast_address_record_t);
2344 }));
2345 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002346
2347 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002348 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2349
Ed Warnickecb9cada2015-12-08 15:45:58 -07002350 /* update lengths */
2351 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2352
Dave Barachd7cb1b52016-12-09 09:52:16 -05002353 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2354 &bogus_length);
2355 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002356
Dave Barachd7cb1b52016-12-09 09:52:16 -05002357 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002358 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002359 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002360 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002361 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002362 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002363
Neale Ranns32e1c012016-11-22 17:07:28 +00002364 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Damjan Marion213b5aa2017-07-13 21:19:27 +02002365 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002366
Neale Ranns32e1c012016-11-22 17:07:28 +00002367 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002368
Ed Warnickecb9cada2015-12-08 15:45:58 -07002369 f = vlib_get_frame_to_node (vm, node->index);
2370 to_next = vlib_frame_vector_args (f);
2371 to_next[0] = bo0;
2372 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002373
Ed Warnickecb9cada2015-12-08 15:45:58 -07002374 vlib_put_frame_to_node (vm, node->index, f);
2375 return;
2376}
2377
Dave Barachd7cb1b52016-12-09 09:52:16 -05002378/* *INDENT-OFF* */
2379VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2380{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002381 .function = icmp6_router_solicitation,
2382 .name = "icmp6-router-solicitation",
2383
2384 .vector_size = sizeof (u32),
2385
2386 .format_trace = format_icmp6_input_trace,
2387
2388 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2389 .next_nodes = {
2390 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002391 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002392 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2393 },
2394};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002395/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002396
2397/* send a RA or update the timer info etc.. */
2398static uword
2399ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002400 vlib_node_runtime_t * node,
2401 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002402{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002403 vnet_main_t *vnm = vnet_get_main ();
2404 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2405 ip6_radv_t *radv_info;
2406 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002407 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002408 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002409 u32 *to_next = 0;
2410 u32 bo0;
2411 icmp6_router_solicitation_header_t *h0;
2412 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002413 f64 now = vlib_time_now (vm);
2414
2415 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002416 /* *INDENT-OFF* */
2417 pool_foreach (radv_info, nm->if_radv_pool,
2418 ({
2419 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2420 {
2421 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2422 radv_info->next_multicast_time = now;
2423 radv_info->last_multicast_time = now;
2424 radv_info->last_radv_time = 0;
2425 radv_info->all_routers_mcast = 0;
2426 continue;
2427 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002428
Dave Barachd7cb1b52016-12-09 09:52:16 -05002429 /* Make sure that we've joined the all-routers multicast group */
2430 if(!radv_info->all_routers_mcast)
2431 {
2432 /* send MDLP_REPORT_EVENT message */
2433 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2434 radv_info->all_routers_mcast = 1;
2435 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002436
Dave Barachd7cb1b52016-12-09 09:52:16 -05002437 /* is it time to send a multicast RA on this interface? */
2438 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2439 {
2440 u32 n_to_alloc = 1;
2441 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002442
Dave Barachd7cb1b52016-12-09 09:52:16 -05002443 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2444 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002445
Dave Barachd7cb1b52016-12-09 09:52:16 -05002446 /* multicast send - compute next multicast send time */
2447 if( radv_info->initial_adverts_sent > 0)
2448 {
2449 radv_info->initial_adverts_sent--;
2450 if(rfn > radv_info-> initial_adverts_interval)
2451 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002452
Dave Barachd7cb1b52016-12-09 09:52:16 -05002453 /* check to see if we are ceasing to send */
2454 if( radv_info->initial_adverts_sent == 0)
2455 if(radv_info->cease_radv)
2456 radv_info->send_radv = 0;
2457 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002458
Dave Barachd7cb1b52016-12-09 09:52:16 -05002459 radv_info->next_multicast_time = rfn + now;
2460 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002461
Dave Barachd7cb1b52016-12-09 09:52:16 -05002462 /* send advert now - build a "solicted" router advert with unspecified source address */
2463 n_allocated = vlib_buffer_alloc_from_free_list
2464 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002465
Dave Barachd7cb1b52016-12-09 09:52:16 -05002466 if (PREDICT_FALSE(n_allocated == 0))
2467 {
2468 clib_warning ("buffer allocation failure");
2469 continue;
2470 }
2471 b0 = vlib_get_buffer (vm, bo0);
2472 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2473 b0->error = ICMP6_ERROR_NONE;
2474 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2475
2476 h0 = vlib_buffer_get_current (b0);
2477
2478 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2479
2480 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2481 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2482 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2483 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2484 h0->ip.hop_limit = 255;
2485
2486 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2487 h0->ip.src_address.as_u64[0] = 0;
2488 h0->ip.src_address.as_u64[1] = 0;
2489
2490 h0->ip.dst_address.as_u64[0] = 0;
2491 h0->ip.dst_address.as_u64[1] = 0;
2492
2493 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2494
2495 if (PREDICT_FALSE(f == 0))
2496 {
2497 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2498 to_next = vlib_frame_vector_args (f);
2499 n_left_to_next = VLIB_FRAME_SIZE;
2500 n_this_frame = 0;
2501 }
2502
2503 n_this_frame++;
2504 n_left_to_next--;
2505 to_next[0] = bo0;
2506 to_next += 1;
2507
2508 if (PREDICT_FALSE(n_left_to_next == 0))
2509 {
2510 f->n_vectors = n_this_frame;
2511 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2512 f = 0;
2513 }
2514 }
2515 }));
2516 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002517
2518 if (f)
2519 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002520 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002521 f->n_vectors = n_this_frame;
2522 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2523 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002524 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002525}
2526
2527static uword
2528ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2529 vlib_node_runtime_t * node,
2530 vlib_frame_t * frame)
2531{
2532 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002533 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002534
2535 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002536
Ed Warnickecb9cada2015-12-08 15:45:58 -07002537 while (1)
2538 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002539 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07002540
Dave Barachd7cb1b52016-12-09 09:52:16 -05002541 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002542
Dave Barachd7cb1b52016-12-09 09:52:16 -05002543 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002544 {
2545 /* No events found: timer expired. */
2546 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002547 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002548 }
2549 else
2550 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002551 switch (event_type)
2552 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002553
Dave Barachd7cb1b52016-12-09 09:52:16 -05002554 case ICMP6_ND_EVENT_INIT:
2555 break;
2556
2557 case ~0:
2558 break;
2559
2560 default:
2561 ASSERT (0);
2562 }
2563
Ed Warnickecb9cada2015-12-08 15:45:58 -07002564 if (event_data)
2565 _vec_len (event_data) = 0;
2566 }
2567 }
2568 return frame->n_vectors;
2569}
2570
Dave Barachd7cb1b52016-12-09 09:52:16 -05002571/* *INDENT-OFF* */
2572VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2573{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002574 .function = icmp6_router_advertisement,
2575 .name = "icmp6-router-advertisement",
2576
2577 .vector_size = sizeof (u32),
2578
2579 .format_trace = format_icmp6_input_trace,
2580
2581 .n_next_nodes = 1,
2582 .next_nodes = {
2583 [0] = "error-drop",
2584 },
2585};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002586/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002587
2588vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2589
2590 .function = ip6_icmp_neighbor_discovery_event_process,
2591 .name = "ip6-icmp-neighbor-discovery-event-process",
2592 .type = VLIB_NODE_TYPE_PROCESS,
2593};
2594
2595static uword
2596icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002597 vlib_node_runtime_t * node, vlib_frame_t * frame)
2598{
2599 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2600 /* is_solicitation */
2601 1);
2602}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002603
2604static uword
2605icmp6_neighbor_advertisement (vlib_main_t * vm,
2606 vlib_node_runtime_t * node,
2607 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002608{
2609 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2610 /* is_solicitation */
2611 0);
2612}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002613
Dave Barachd7cb1b52016-12-09 09:52:16 -05002614/* *INDENT-OFF* */
2615VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2616{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002617 .function = icmp6_neighbor_solicitation,
2618 .name = "icmp6-neighbor-solicitation",
2619
2620 .vector_size = sizeof (u32),
2621
2622 .format_trace = format_icmp6_input_trace,
2623
2624 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2625 .next_nodes = {
2626 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2627 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2628 },
2629};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002630/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002631
Dave Barachd7cb1b52016-12-09 09:52:16 -05002632/* *INDENT-OFF* */
2633VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2634{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002635 .function = icmp6_neighbor_advertisement,
2636 .name = "icmp6-neighbor-advertisement",
2637
2638 .vector_size = sizeof (u32),
2639
2640 .format_trace = format_icmp6_input_trace,
2641
2642 .n_next_nodes = 1,
2643 .next_nodes = {
2644 [0] = "error-drop",
2645 },
2646};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002647/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002648
Dave Barachd7cb1b52016-12-09 09:52:16 -05002649/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002650int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002651ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2652 u8 suppress, u8 managed, u8 other,
2653 u8 ll_option, u8 send_unicast, u8 cease,
2654 u8 use_lifetime, u32 lifetime,
2655 u32 initial_count, u32 initial_interval,
2656 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002657{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002658 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2659 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002660 u32 ri;
2661
2662 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002663 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2664 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002665 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002666 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002667
Dave Barachd7cb1b52016-12-09 09:52:16 -05002668 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002669 {
2670
Dave Barachd7cb1b52016-12-09 09:52:16 -05002671 ip6_radv_t *radv_info;
2672 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002673
Dave Barachd7cb1b52016-12-09 09:52:16 -05002674 if ((max_interval != 0) && (min_interval == 0))
2675 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002676
Dave Barachd7cb1b52016-12-09 09:52:16 -05002677 max_interval =
2678 (max_interval !=
2679 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2680 radv_info->max_radv_interval;
2681 min_interval =
2682 (min_interval !=
2683 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2684 radv_info->min_radv_interval;
2685 lifetime =
2686 (use_lifetime !=
2687 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2688 radv_info->adv_router_lifetime_in_sec;
2689
2690 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002691 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002692 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002693 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002694
2695 if (lifetime <= max_interval)
2696 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002697 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002698
2699 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002700 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002701 if ((min_interval > .75 * max_interval) || (min_interval < 3))
2702 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002703 }
2704
Dave Barachd7cb1b52016-12-09 09:52:16 -05002705 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2706 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2707 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002708
Dave Barachd7cb1b52016-12-09 09:52:16 -05002709 /*
2710 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2711 if "flag" is clear don't change corresponding value
2712 */
2713 radv_info->send_radv =
2714 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2715 radv_info->adv_managed_flag =
2716 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2717 radv_info->adv_other_flag =
2718 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2719 radv_info->adv_link_layer_address =
2720 (ll_option !=
2721 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2722 radv_info->send_unicast =
2723 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2724 radv_info->cease_radv =
2725 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2726
2727 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002728 radv_info->max_radv_interval = max_interval;
2729 radv_info->adv_router_lifetime_in_sec = lifetime;
2730
Dave Barachd7cb1b52016-12-09 09:52:16 -05002731 radv_info->initial_adverts_count =
2732 (initial_count !=
2733 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2734 radv_info->initial_adverts_count;
2735 radv_info->initial_adverts_interval =
2736 (initial_interval !=
2737 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2738 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002739
2740 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002741 if ((cease != 0) && (is_no))
2742 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002743
Dave Barachd7cb1b52016-12-09 09:52:16 -05002744 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2745 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002746 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002747 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002748 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002749 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002750}
2751
2752int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002753ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2754 ip6_address_t * prefix_addr, u8 prefix_len,
2755 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2756 u8 no_advertise, u8 off_link, u8 no_autoconfig,
2757 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002758{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002759 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002760 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002761
Ed Warnickecb9cada2015-12-08 15:45:58 -07002762 u32 ri;
2763
2764 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002765 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2766 ~0);
2767
Ed Warnickecb9cada2015-12-08 15:45:58 -07002768 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2769
2770 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002771
2772 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002773 {
2774 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002775 ip6_radv_t *radv_info;
2776 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002777
2778 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002779 ip6_radv_prefix_t *prefix;
2780
Ed Warnickecb9cada2015-12-08 15:45:58 -07002781 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002782 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2783
Ed Warnickecb9cada2015-12-08 15:45:58 -07002784 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2785
Dave Barachd7cb1b52016-12-09 09:52:16 -05002786 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002787 {
2788 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002789 if (!prefix)
2790 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2791
2792 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002793 return VNET_API_ERROR_INVALID_VALUE_2;
2794
Dave Barachd7cb1b52016-12-09 09:52:16 -05002795 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002796 /* do specific delete processing here before returning */
2797 /* try to remove from routing table */
2798
Dave Barachd7cb1b52016-12-09 09:52:16 -05002799 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2800 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002801 pool_put (radv_info->adv_prefixes_pool, prefix);
2802
Dave Barachd7cb1b52016-12-09 09:52:16 -05002803 radv_info->initial_adverts_sent =
2804 radv_info->initial_adverts_count - 1;
2805 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002806 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002807 radv_info->last_radv_time = 0;
2808 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002809 }
2810
2811 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002812 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002813 {
2814 /* add */
2815 u32 pi;
2816 pool_get (radv_info->adv_prefixes_pool, prefix);
2817 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002818 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2819 /* old_value */ 0);
2820
2821 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2822
Ed Warnickecb9cada2015-12-08 15:45:58 -07002823 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002824 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2825
Ed Warnickecb9cada2015-12-08 15:45:58 -07002826 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002827 prefix->adv_on_link_flag = 1; /* L bit set */
2828 prefix->adv_autonomous_flag = 1; /* A bit set */
2829 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002830 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2831 prefix->enabled = 1;
2832 prefix->decrement_lifetime_flag = 1;
2833 prefix->deprecated_prefix_flag = 1;
2834
Dave Barachd7cb1b52016-12-09 09:52:16 -05002835 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002836 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002837 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838 /* insert prefix into routing table as a connected prefix */
2839 }
2840
Dave Barachd7cb1b52016-12-09 09:52:16 -05002841 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002842 goto restart;
2843 }
2844 else
2845 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002846
2847 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002848 return VNET_API_ERROR_INVALID_VALUE_2;
2849
Dave Barachd7cb1b52016-12-09 09:52:16 -05002850 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002851 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002852 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002854 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002855 }
2856
Dave Barachd7cb1b52016-12-09 09:52:16 -05002857 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002858 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002859 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002860 prefix->adv_pref_lifetime_in_secs = ~0;
2861 prefix->decrement_lifetime_flag = 0;
2862 }
2863 else
2864 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002865 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2866 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002867 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002868
Ed Warnickecb9cada2015-12-08 15:45:58 -07002869 /* copy remaining */
2870 prefix->enabled = !(no_advertise != 0);
2871 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2872 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2873
Dave Barachd7cb1b52016-12-09 09:52:16 -05002874 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002875 /* restart */
2876 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002877 prefix->valid_lifetime_expires =
2878 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002880
2881 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2882 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002883 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002884 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002885 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002886 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002887}
2888
2889clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05002890ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2891 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002892{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002893 vnet_main_t *vnm = vnet_get_main ();
2894 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2895 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002896 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002897 u8 suppress = 0, managed = 0, other = 0;
2898 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002899 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002900 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2901 0, ra_initial_interval = 0;
2902 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002903
Dave Barachd7cb1b52016-12-09 09:52:16 -05002904 unformat_input_t _line_input, *line_input = &_line_input;
2905 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002906
2907 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002908 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002909 ip6_address_t ip6_addr;
2910 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002911
Ed Warnickecb9cada2015-12-08 15:45:58 -07002912
2913 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002914 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002915 return 0;
2916
2917 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002918 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002919 {
2920
Dave Barachd7cb1b52016-12-09 09:52:16 -05002921 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002922 unformat_vnet_sw_interface, vnm, &sw_if_index))
2923 {
2924 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002925 ethernet_interface_t *eth_if0 = 0;
2926
Ed Warnickecb9cada2015-12-08 15:45:58 -07002927 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002928 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2929 eth_if0 =
2930 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2931
2932 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002933 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002934 error =
2935 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07002936 goto done;
2937 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002938
Ed Warnickecb9cada2015-12-08 15:45:58 -07002939 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002940 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2941 sw_if_index, ~0);
2942
Ed Warnickecb9cada2015-12-08 15:45:58 -07002943 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002944
2945 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002946 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002947 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002948 }
2949 else
2950 {
2951 error = clib_error_return (0, "unknown interface %U'",
2952 format_unformat_error, line_input);
2953 goto done;
2954 }
2955 }
2956 else
2957 {
2958 error = clib_error_return (0, "invalid interface name %U'",
2959 format_unformat_error, line_input);
2960 goto done;
2961 }
2962 }
2963
2964 /* get the rest of the command */
2965 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2966 {
2967 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05002968 is_no = 1;
2969 else if (unformat (line_input, "prefix %U/%d",
2970 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002971 {
2972 add_radv_info = 0;
2973 break;
2974 }
2975 else if (unformat (line_input, "ra-managed-config-flag"))
2976 {
2977 managed = 1;
2978 break;
2979 }
2980 else if (unformat (line_input, "ra-other-config-flag"))
2981 {
2982 other = 1;
2983 break;
2984 }
Chris Luke33879c92016-06-28 19:54:21 -04002985 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05002986 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002987 {
Chris Luke33879c92016-06-28 19:54:21 -04002988 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002989 break;
2990 }
Chris Luke33879c92016-06-28 19:54:21 -04002991 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05002992 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002993 {
Chris Luke33879c92016-06-28 19:54:21 -04002994 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002995 break;
2996 }
2997 else if (unformat (line_input, "ra-send-unicast"))
2998 {
2999 send_unicast = 1;
3000 break;
3001 }
3002 else if (unformat (line_input, "ra-lifetime"))
3003 {
3004 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05003005 {
3006 error = unformat_parse_error (line_input);
3007 goto done;
3008 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003009 use_lifetime = 1;
3010 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003011 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003012 else if (unformat (line_input, "ra-initial"))
3013 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003014 if (!unformat
3015 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003016 {
3017 error = unformat_parse_error (line_input);
3018 goto done;
3019 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003020 break;
3021 }
3022 else if (unformat (line_input, "ra-interval"))
3023 {
3024 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05003025 {
3026 error = unformat_parse_error (line_input);
3027 goto done;
3028 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003029
3030 if (!unformat (line_input, "%d", &ra_min_interval))
3031 ra_min_interval = 0;
3032 break;
3033 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003034 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003035 {
3036 cease = 1;
3037 break;
3038 }
3039 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003040 {
3041 error = unformat_parse_error (line_input);
3042 goto done;
3043 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003044 }
3045
Dave Barachd7cb1b52016-12-09 09:52:16 -05003046 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003047 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003048 ip6_neighbor_ra_config (vm, sw_if_index,
3049 suppress, managed, other,
3050 suppress_ll_option, send_unicast, cease,
3051 use_lifetime, ra_lifetime,
3052 ra_initial_count, ra_initial_interval,
3053 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003054 }
3055 else
3056 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003057 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003058 u32 pref_lifetime_in_secs = 0;
3059 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003060 u8 no_advertise = 0;
3061 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003063 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003064
3065 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003066 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003067 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003068 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003069 {
3070 use_prefix_default_values = 1;
3071 break;
3072 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003073 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003074 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003075 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003076 pref_lifetime_in_secs = ~0;
3077 break;
3078 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003079 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3080 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003081 break;
3082 else
3083 break;
3084 }
3085
3086
3087 /* get the rest of the command */
3088 while (!use_prefix_default_values &&
3089 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3090 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003091 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003092 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003093 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003094 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003095 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003096 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003097 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003098 no_onlink = 1;
3099 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003100 {
3101 error = unformat_parse_error (line_input);
3102 goto done;
3103 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003104 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003105
3106 ip6_neighbor_ra_prefix (vm, sw_if_index,
3107 &ip6_addr, addr_len,
3108 use_prefix_default_values,
3109 valid_lifetime_in_secs,
3110 pref_lifetime_in_secs,
3111 no_advertise,
3112 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003113 }
3114
Billy McFalla9a20e72017-02-15 11:39:12 -05003115done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003116 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003117
Ed Warnickecb9cada2015-12-08 15:45:58 -07003118 return error;
3119}
3120
Chris Lukebfd32fd2016-06-24 20:38:06 -04003121static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003122ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003123{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003124 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003125 u32 i;
3126
3127 for (i = 0; i < vec_len (addrs); i++)
3128 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003129 ip_interface_address_t *a =
3130 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3131 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003132
3133 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003134 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003135 }
3136}
3137
Ed Warnickecb9cada2015-12-08 15:45:58 -07003138static clib_error_t *
3139show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003140 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003141{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003142 vnet_main_t *vnm = vnet_get_main ();
3143 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3144 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003145 u32 sw_if_index;
3146
3147 sw_if_index = ~0;
3148
Dave Barachd7cb1b52016-12-09 09:52:16 -05003149 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003150 {
3151 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003152
Dave Barachd7cb1b52016-12-09 09:52:16 -05003153 /* look up the radv_t information for this interface */
3154 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3155 sw_if_index, ~0);
3156
3157 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3158
3159 if (ri != ~0)
3160 {
3161 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3162 ip6_radv_t *radv_info;
3163 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3164
3165 vlib_cli_output (vm, "%U is admin %s\n",
3166 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003167 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003168 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3169 "up" : "down"));
3170
Chris Lukebfd32fd2016-06-24 20:38:06 -04003171 u32 ai;
3172 u32 *link_scope = 0, *global_scope = 0;
3173 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003174 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003175
Dave Barachd7cb1b52016-12-09 09:52:16 -05003176 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3177 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003178 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3179
Dave Barachd7cb1b52016-12-09 09:52:16 -05003180 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003181 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003182 a = pool_elt_at_index (lm->if_address_pool, ai);
3183 ip6_address_t *address =
3184 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003185
Chris Lukebfd32fd2016-06-24 20:38:06 -04003186 if (ip6_address_is_link_local_unicast (address))
3187 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003188 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003189 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003190 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003191 vec_add1 (local_scope, ai);
3192 else
3193 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003194
3195 ai = a->next_this_sw_interface;
3196 }
3197
Dave Barachd7cb1b52016-12-09 09:52:16 -05003198 if (vec_len (link_scope))
3199 {
3200 vlib_cli_output (vm, "\tLink-local address(es):\n");
3201 ip6_print_addrs (vm, link_scope);
3202 vec_free (link_scope);
3203 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003204
Dave Barachd7cb1b52016-12-09 09:52:16 -05003205 if (vec_len (local_scope))
3206 {
3207 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3208 ip6_print_addrs (vm, local_scope);
3209 vec_free (local_scope);
3210 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003211
Dave Barachd7cb1b52016-12-09 09:52:16 -05003212 if (vec_len (global_scope))
3213 {
3214 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3215 ip6_print_addrs (vm, global_scope);
3216 vec_free (global_scope);
3217 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003218
Dave Barachd7cb1b52016-12-09 09:52:16 -05003219 if (vec_len (unknown_scope))
3220 {
3221 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3222 ip6_print_addrs (vm, unknown_scope);
3223 vec_free (unknown_scope);
3224 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003225
Ed Warnickecb9cada2015-12-08 15:45:58 -07003226 vlib_cli_output (vm, "\tJoined group address(es):\n");
3227 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003228 /* *INDENT-OFF* */
3229 pool_foreach (m, radv_info->mldp_group_pool,
3230 ({
3231 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3232 &m->mcast_address);
3233 }));
3234 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003235
3236 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003237 ip6_radv_prefix_t *p;
3238 /* *INDENT-OFF* */
3239 pool_foreach (p, radv_info->adv_prefixes_pool,
3240 ({
3241 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3242 format_ip6_address, &p->prefix, p->prefix_len);
3243 }));
3244 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003245
Dave Barachd7cb1b52016-12-09 09:52:16 -05003246 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003247 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3248 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3249 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3250 vlib_cli_output (vm, "\tND DAD is disabled\n");
3251 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3252 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3253 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003254 vlib_cli_output (vm,
3255 "\tND advertised retransmit interval is %d (msec)\n",
3256 radv_info->
3257 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003258
3259 u32 ra_interval = radv_info->max_radv_interval;
3260 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003261 vlib_cli_output (vm,
3262 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003263 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003264 vlib_cli_output (vm,
3265 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003266 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003267 vlib_cli_output (vm,
3268 "\tHosts %s stateless autoconfig for addresses\n",
3269 (radv_info->adv_managed_flag) ? "use" :
3270 " don't use");
3271 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3272 radv_info->n_advertisements_sent);
3273 vlib_cli_output (vm, "\tND router solicitations received %d\n",
3274 radv_info->n_solicitations_rcvd);
3275 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3276 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003277 }
3278 else
3279 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04003280 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003281 format_unformat_error, input);
3282
3283 }
3284 }
3285 return error;
3286}
3287
Billy McFall0683c9c2016-10-13 08:27:31 -04003288/*?
3289 * This command is used to display various IPv6 attributes on a given
3290 * interface.
3291 *
3292 * @cliexpar
3293 * Example of how to display IPv6 settings:
3294 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3295 * GigabitEthernet2/0/0 is admin up
3296 * Link-local address(es):
3297 * fe80::ab8/64
3298 * Joined group address(es):
3299 * ff02::1
3300 * ff02::2
3301 * ff02::16
3302 * ff02::1:ff00:ab8
3303 * Advertised Prefixes:
3304 * prefix fe80::fe:28ff:fe9c:75b3, length 64
3305 * MTU is 1500
3306 * ICMP error messages are unlimited
3307 * ICMP redirects are disabled
3308 * ICMP unreachables are not sent
3309 * ND DAD is disabled
3310 * ND advertised reachable time is 0
3311 * ND advertised retransmit interval is 0 (msec)
3312 * ND router advertisements are sent every 200 seconds (min interval is 150)
3313 * ND router advertisements live for 600 seconds
3314 * Hosts use stateless autoconfig for addresses
3315 * ND router advertisements sent 19336
3316 * ND router solicitations received 0
3317 * ND router solicitations dropped 0
3318 * @cliexend
3319 * Example of output if IPv6 is not enabled on the interface:
3320 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3321 * show ip6 interface: IPv6 not enabled on interface
3322 * @cliexend
3323?*/
3324/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003325VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3326{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003327 .path = "show ip6 interface",
3328 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003329 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003330};
Billy McFall0683c9c2016-10-13 08:27:31 -04003331/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003332
3333clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003334disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003335{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003336 clib_error_t *error = 0;
3337 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003338 u32 ri;
3339
3340 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003341 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3342 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003343 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003344
Ed Warnickecb9cada2015-12-08 15:45:58 -07003345 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003346 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003347 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003348 vnet_main_t *vnm = vnet_get_main ();
3349 ip6_radv_t *radv_info;
3350
3351 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003352
3353 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01003354 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003355 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003356 {
3357 /* essentially "disables" ipv6 on this interface */
3358 error = ip6_add_del_interface_address (vm, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003359 &radv_info->
Neale Ranns75152282017-01-09 01:00:45 -08003360 link_local_address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003361 1 /* is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003362
Dave Barachd7cb1b52016-12-09 09:52:16 -05003363 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3364 0 /* is_add */ );
Neale Ranns4008ac92017-02-13 23:20:04 -08003365 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003366 }
3367 }
3368 return error;
3369}
3370
3371int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003372ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003373{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003374 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3375 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003376
Dave Barachd7cb1b52016-12-09 09:52:16 -05003377 /* look up the radv_t information for this interface */
3378 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3379 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003380
Dave Barachd7cb1b52016-12-09 09:52:16 -05003381 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07003382
Dave Barachd7cb1b52016-12-09 09:52:16 -05003383 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003384}
3385
Dave Barachd7cb1b52016-12-09 09:52:16 -05003386clib_error_t *
3387enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003388{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003389 clib_error_t *error = 0;
3390 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003391 u32 ri;
3392 int is_add = 1;
3393
3394 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003395 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3396 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003397
Dave Barachd7cb1b52016-12-09 09:52:16 -05003398 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3399
3400 /* if not created yet */
3401 if (ri == ~0)
3402 {
3403 vnet_main_t *vnm = vnet_get_main ();
3404 vnet_sw_interface_t *sw_if0;
3405
3406 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3407 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3408 {
3409 ethernet_interface_t *eth_if0;
3410
3411 eth_if0 =
3412 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3413 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003414 {
3415 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003416 ri =
3417 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003418
Dave Barachd7cb1b52016-12-09 09:52:16 -05003419 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003420 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003421 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003422 ip6_address_t link_local_address;
3423
Dave Barachd7cb1b52016-12-09 09:52:16 -05003424 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003425
Dave Barachd7cb1b52016-12-09 09:52:16 -05003426 ip6_link_local_address_from_ethernet_mac_address
3427 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003428
3429 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek8dc80202017-08-15 13:52:22 +02003430 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB ||
3431 sw_if0->type == VNET_SW_INTERFACE_TYPE_P2P)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003432 {
3433 /* make up an interface id */
3434 md5_context_t m;
3435 u8 digest[16];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003436
Ed Warnickecb9cada2015-12-08 15:45:58 -07003437 link_local_address.as_u64[0] = radv_info->randomizer;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003438
Ed Warnickecb9cada2015-12-08 15:45:58 -07003439 md5_init (&m);
3440 md5_add (&m, &link_local_address, 16);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003441 md5_finish (&m, digest);
3442
3443 clib_memcpy (&link_local_address, digest, 16);
3444
Ed Warnickecb9cada2015-12-08 15:45:58 -07003445 radv_info->randomizer = link_local_address.as_u64[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003446
3447 link_local_address.as_u64[0] =
3448 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003449 /* clear u bit */
3450 link_local_address.as_u8[8] &= 0xfd;
3451 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003452
Neale Ranns4008ac92017-02-13 23:20:04 -08003453 ip6_mfib_interface_enable_disable (sw_if_index, 1);
3454
Ed Warnickecb9cada2015-12-08 15:45:58 -07003455 /* essentially "enables" ipv6 on this interface */
3456 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003457 &link_local_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003458 128
3459 /* address width */ ,
3460 0 /* is_del */ );
3461
3462 if (error)
3463 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3464 !is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003465 else
3466 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003467 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003468 }
3469 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003470 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003471 }
3472 }
3473 return error;
3474}
3475
3476static clib_error_t *
3477enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003478 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003479{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003480 vnet_main_t *vnm = vnet_get_main ();
3481 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003482 u32 sw_if_index;
3483
3484 sw_if_index = ~0;
3485
Dave Barachd7cb1b52016-12-09 09:52:16 -05003486 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003487 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003488 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003489 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003490 else
3491 {
3492 error = clib_error_return (0, "unknown interface\n'",
3493 format_unformat_error, input);
3494
3495 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003496 return error;
3497}
3498
Billy McFall0683c9c2016-10-13 08:27:31 -04003499/*?
3500 * This command is used to enable IPv6 on a given interface.
3501 *
3502 * @cliexpar
3503 * Example of how enable IPv6 on a given interface:
3504 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3505?*/
3506/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003507VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3508{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003509 .path = "enable ip6 interface",
3510 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003511 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003512};
Billy McFall0683c9c2016-10-13 08:27:31 -04003513/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003514
3515static clib_error_t *
3516disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003517 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003518{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003519 vnet_main_t *vnm = vnet_get_main ();
3520 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003521 u32 sw_if_index;
3522
3523 sw_if_index = ~0;
3524
Dave Barachd7cb1b52016-12-09 09:52:16 -05003525 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003526 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003527 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003528 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003529 else
3530 {
3531 error = clib_error_return (0, "unknown interface\n'",
3532 format_unformat_error, input);
3533
3534 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003535 return error;
3536}
3537
Billy McFall0683c9c2016-10-13 08:27:31 -04003538/*?
3539 * This command is used to disable IPv6 on a given interface.
3540 *
3541 * @cliexpar
3542 * Example of how disable IPv6 on a given interface:
3543 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3544?*/
3545/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003546VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3547{
Billy McFall0683c9c2016-10-13 08:27:31 -04003548 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003549 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003550 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003551};
Billy McFall0683c9c2016-10-13 08:27:31 -04003552/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003553
Billy McFall0683c9c2016-10-13 08:27:31 -04003554/*?
3555 * This command is used to configure the neighbor discovery
3556 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3557 * command to display some of the current neighbor discovery parameters
3558 * on a given interface. This command has three formats:
3559 *
3560 *
3561 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3562 *
3563 * '<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>'
3564 *
3565 * Where:
3566 *
3567 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3568 * router-advertisement messages to use stateful address
3569 * auto-configuration to obtain address information (sets the M-bit).
3570 * Default is the M-bit is not set and the '<em>no</em>' option
3571 * returns it to this default state.
3572 *
3573 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3574 * router-advertisement messages that hosts use stateful auto
3575 * configuration to obtain nonaddress related information (sets
3576 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3577 * option returns it to this default state.
3578 *
3579 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3580 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3581 * router-advertisement messages.
3582 *
3583 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3584 * optional source link-layer address in the ICMPv6 router-advertisement
3585 * messages. Default is to include the optional source link-layer address
3586 * and the '<em>no</em>' option returns it to this default state.
3587 *
3588 * <em>[no] ra-send-unicast</em> - Use the source address of the
3589 * router-solicitation message if availiable. The default is to use
3590 * multicast address of all nodes, and the '<em>no</em>' option returns
3591 * it to this default state.
3592 *
3593 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3594 * default router in ICMPv6 router-advertisement messages. The range is
3595 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3596 * '<em><max-interval></em>'. The default value is 600 seconds and the
3597 * '<em>no</em>' option returns it to this default value.
3598 *
3599 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3600 * router-advertisement messages sent and the interval between each
3601 * message. Range for count is 1 - 3 and default is 3. Range for interval
3602 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3603 * returns both to their default value.
3604 *
3605 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3606 * interval between sending ICMPv6 router-advertisement messages. The
3607 * range for max-interval is from 4 to 200 seconds. min-interval can not
3608 * be more than 75% of max-interval. If not set, min-interval will be
3609 * set to 75% of max-interval. The range for min-interval is from 3 to
3610 * 150 seconds. The '<em>no</em>' option returns both to their default
3611 * value.
3612 *
3613 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3614 * The '<em>no</em>' options implies to start (or restart) sending
3615 * ICMPv6 router-advertisement messages.
3616 *
3617 *
3618 * <b>Format 2 - Prefix Options:</b>
3619 *
3620 * '<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>'
3621 *
3622 * Where:
3623 *
3624 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3625 *
3626 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3627 * length of time in seconds during what the prefix is valid for the purpose of
3628 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3629 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3630 * length of time in seconds during what addresses generated from the prefix remain
3631 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3632 *
3633 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3634 * are inifinte, no timeout.
3635 *
3636 * <em>no-advertise</em> - Do not send full router address in prefix
3637 * advertisement. Default is to advertise (i.e. - This flag is off by default).
3638 *
3639 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3640 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3641 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3642 *
3643 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3644 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3645 *
3646 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3647 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3648 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3649 * the L-bit.
3650 *
3651 *
3652 * <b>Format 3: - Default of Prefix:</b>
3653 *
3654 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3655 *
3656 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3657 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3658 * is ignored and the prefix is deleted.
3659 *
3660 *
3661 * @cliexpar
3662 * Example of how set a router advertisement option:
3663 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3664 * Example of how to add a prefix:
3665 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3666 * Example of how to delete a prefix:
3667 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3668?*/
3669/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003670VLIB_CLI_COMMAND (ip6_nd_command, static) =
3671{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003672 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04003673 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003674 .function = ip6_neighbor_cmd,
3675};
Billy McFall0683c9c2016-10-13 08:27:31 -04003676/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003677
3678clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003679set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08003680 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003681{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003682 clib_error_t *error = 0;
3683 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003684 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003685 ip6_radv_t *radv_info;
3686 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07003687
Dave Barachd7cb1b52016-12-09 09:52:16 -05003688 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003689 {
3690 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003691 return (error = clib_error_return (0, "address not link-local",
3692 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003693 }
3694
3695 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003696 enable_ip6_interface (vm, sw_if_index);
3697
Ed Warnickecb9cada2015-12-08 15:45:58 -07003698 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003699
3700 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003701 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003702 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003703
3704 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003705
Ed Warnickecb9cada2015-12-08 15:45:58 -07003706 /* delete the old one */
3707 error = ip6_add_del_interface_address (vm, sw_if_index,
3708 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08003709 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05003710
3711 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003712 {
3713 /* add the new one */
3714 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08003715 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003716 0 /* is_del */ );
3717
3718 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003719 {
3720 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003721 }
3722 }
3723 }
3724 else
3725 {
3726 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3727 error = clib_error_return (0, "ip6 not enabled for interface",
3728 format_unformat_error);
3729 }
3730 return error;
3731}
Dave Barachd7cb1b52016-12-09 09:52:16 -05003732
Ed Warnickecb9cada2015-12-08 15:45:58 -07003733clib_error_t *
3734set_ip6_link_local_address_cmd (vlib_main_t * vm,
3735 unformat_input_t * input,
3736 vlib_cli_command_t * cmd)
3737{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003738 vnet_main_t *vnm = vnet_get_main ();
3739 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003740 u32 sw_if_index;
3741 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003742
3743 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003744 {
3745 /* get the rest of the command */
3746 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3747 {
Neale Ranns75152282017-01-09 01:00:45 -08003748 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003749 break;
3750 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003751 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003752 }
3753 }
Neale Ranns75152282017-01-09 01:00:45 -08003754 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003755 return error;
3756}
3757
Billy McFall0683c9c2016-10-13 08:27:31 -04003758/*?
3759 * This command is used to assign an IPv6 Link-local address to an
3760 * interface. This command will enable IPv6 on an interface if it
3761 * is not already enabled. Use the '<em>show ip6 interface</em>' command
3762 * to display the assigned Link-local address.
3763 *
3764 * @cliexpar
3765 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08003766 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04003767?*/
3768/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003769VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3770{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003771 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08003772 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003773 .function = set_ip6_link_local_address_cmd,
3774};
Billy McFall0683c9c2016-10-13 08:27:31 -04003775/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003776
Neale Ranns87df12d2017-02-18 08:16:41 -08003777/**
3778 * @brief callback when an interface address is added or deleted
3779 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003780static void
3781ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3782 uword opaque,
3783 u32 sw_if_index,
3784 ip6_address_t * address,
3785 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003786 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003787{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003788 vnet_main_t *vnm = vnet_get_main ();
3789 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003790 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003791 vlib_main_t *vm = vnm->vlib_main;
3792 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003793 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003794
3795 /* create solicited node multicast address for this interface adddress */
3796 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003797
Ed Warnickecb9cada2015-12-08 15:45:58 -07003798 a.as_u8[0xd] = address->as_u8[0xd];
3799 a.as_u8[0xe] = address->as_u8[0xe];
3800 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003801
3802 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003803 {
3804 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003805 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003806
3807 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003808 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3809 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003810 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003811 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003812 {
3813 /* get radv_info */
3814 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3815
3816 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003817 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003818 radv_info->ref_count++;
3819
Neale Ranns87df12d2017-02-18 08:16:41 -08003820 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003821 }
3822 }
3823 else
3824 {
3825
3826 /* delete */
3827 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003828 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3829 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003830 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01003831
Dave Barachd7cb1b52016-12-09 09:52:16 -05003832 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003833 {
3834 /* get radv_info */
3835 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3836
Neale Ranns87df12d2017-02-18 08:16:41 -08003837 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003838
3839 /* if interface up send MLDP "report" */
3840 radv_info->all_routers_mcast = 0;
3841
3842 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003843 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003844 radv_info->ref_count--;
3845 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01003846 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3847 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003848 }
3849}
3850
Dave Barachd7cb1b52016-12-09 09:52:16 -05003851clib_error_t *
3852ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003853{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003854 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003855
3856 nm->limit_neighbor_cache_size = neighbor_limit;
3857 return 0;
3858}
3859
Neale Ranns15002542017-09-10 04:39:11 -07003860static void
3861ip6_neighbor_table_bind (ip6_main_t * im,
3862 uword opaque,
3863 u32 sw_if_index,
3864 u32 new_fib_index, u32 old_fib_index)
3865{
3866 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3867 ip6_neighbor_t *n = NULL;
3868 u32 i, *to_re_add = 0;
3869
3870 /* *INDENT-OFF* */
3871 pool_foreach (n, nm->neighbor_pool,
3872 ({
3873 if (n->key.sw_if_index == sw_if_index)
3874 vec_add1 (to_re_add, n - nm->neighbor_pool);
3875 }));
3876 /* *INDENT-ON* */
3877
3878 for (i = 0; i < vec_len (to_re_add); i++)
3879 {
3880 n = pool_elt_at_index (nm->neighbor_pool, to_re_add[i]);
3881 ip6_neighbor_adj_fib_remove (n, old_fib_index);
3882 ip6_neighbor_adj_fib_add (n, new_fib_index);
3883 }
3884 vec_free (to_re_add);
3885}
3886
Dave Barachd7cb1b52016-12-09 09:52:16 -05003887static clib_error_t *
3888ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003889{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003890 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3891 ip6_main_t *im = &ip6_main;
3892
Ed Warnickecb9cada2015-12-08 15:45:58 -07003893 mhash_init (&nm->neighbor_index_by_key,
3894 /* value size */ sizeof (uword),
3895 /* key size */ sizeof (ip6_neighbor_key_t));
3896
Dave Barachd7cb1b52016-12-09 09:52:16 -05003897 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3898 ip6_icmp_neighbor_solicitation_node.index);
3899 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3900 ip6_icmp_neighbor_advertisement_node.index);
3901 icmp6_register_type (vm, ICMP6_router_solicitation,
3902 ip6_icmp_router_solicitation_node.index);
3903 icmp6_register_type (vm, ICMP6_router_advertisement,
3904 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003905
3906 /* handler node for ip6 neighbor discovery events and timers */
3907 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3908
3909 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003910 ip6_add_del_interface_address_callback_t cb;
3911 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3912
Ed Warnickecb9cada2015-12-08 15:45:58 -07003913 /* when an interface address changes... */
3914 cb.function = ip6_neighbor_add_del_interface_address;
3915 cb.function_opaque = 0;
3916 vec_add1 (im->add_del_interface_address_callbacks, cb);
3917
Neale Ranns15002542017-09-10 04:39:11 -07003918 ip6_table_bind_callback_t cbt;
3919 cbt.function = ip6_neighbor_table_bind;
3920 cbt.function_opaque = 0;
3921 vec_add1 (im->table_bind_callbacks, cbt);
3922
Ed Warnickecb9cada2015-12-08 15:45:58 -07003923 mhash_init (&nm->pending_resolutions_by_address,
3924 /* value size */ sizeof (uword),
3925 /* key size */ sizeof (ip6_address_t));
3926
John Lo1edfba92016-08-27 01:11:57 -04003927 mhash_init (&nm->mac_changes_by_address,
3928 /* value size */ sizeof (uword),
3929 /* key size */ sizeof (ip6_address_t));
3930
Ed Warnickecb9cada2015-12-08 15:45:58 -07003931 /* default, configurable */
3932 nm->limit_neighbor_cache_size = 50000;
3933
3934#if 0
3935 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003936 vec_validate_init_empty
3937 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003938#endif
3939
Ed Warnickecb9cada2015-12-08 15:45:58 -07003940 return 0;
3941}
3942
3943VLIB_INIT_FUNCTION (ip6_neighbor_init);
3944
3945
Dave Barachd7cb1b52016-12-09 09:52:16 -05003946void
3947vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3948 void *address_arg,
3949 uword node_index,
3950 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003951{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003952 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3953 ip6_address_t *address = address_arg;
3954 uword *p;
3955 pending_resolution_t *pr;
3956
Ed Warnickecb9cada2015-12-08 15:45:58 -07003957 pool_get (nm->pending_resolutions, pr);
3958
3959 pr->next_index = ~0;
3960 pr->node_index = node_index;
3961 pr->type_opaque = type_opaque;
3962 pr->data = data;
3963
3964 p = mhash_get (&nm->pending_resolutions_by_address, address);
3965 if (p)
3966 {
3967 /* Insert new resolution at the head of the list */
3968 pr->next_index = p[0];
3969 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3970 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003971
3972 mhash_set (&nm->pending_resolutions_by_address, address,
3973 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003974}
3975
Dave Barachd7cb1b52016-12-09 09:52:16 -05003976int
3977vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3978 void *data_callback,
3979 u32 pid,
3980 void *address_arg,
3981 uword node_index,
3982 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04003983{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003984 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3985 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003986
Eyal Barif9f40652017-04-01 03:55:08 +03003987 /* Try to find an existing entry */
3988 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
3989 u32 *p = first;
3990 pending_resolution_t *mc;
3991 while (p && *p != ~0)
3992 {
3993 mc = pool_elt_at_index (nm->mac_changes, *p);
3994 if (mc->node_index == node_index && mc->type_opaque == type_opaque
3995 && mc->pid == pid)
3996 break;
3997 p = &mc->next_index;
3998 }
3999
4000 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04004001 if (is_add)
4002 {
Eyal Barif9f40652017-04-01 03:55:08 +03004003 if (found)
4004 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
4005
John Lo1edfba92016-08-27 01:11:57 -04004006 pool_get (nm->mac_changes, mc);
Eyal Barif9f40652017-04-01 03:55:08 +03004007 *mc = (pending_resolution_t)
4008 {
4009 .next_index = ~0,.node_index = node_index,.type_opaque =
4010 type_opaque,.data = data,.data_callback = data_callback,.pid =
4011 pid,};
John Lo1edfba92016-08-27 01:11:57 -04004012
Eyal Barif9f40652017-04-01 03:55:08 +03004013 /* Insert new resolution at the end of the list */
4014 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04004015 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03004016 p[0] = new_idx;
4017 else
4018 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04004019 }
4020 else
4021 {
Eyal Barif9f40652017-04-01 03:55:08 +03004022 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004023 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04004024
Eyal Barif9f40652017-04-01 03:55:08 +03004025 /* Clients may need to clean up pool entries, too */
4026 void (*fp) (u32, u8 *) = data_callback;
4027 if (fp)
4028 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04004029
Eyal Barif9f40652017-04-01 03:55:08 +03004030 /* Remove the entry from the list and delete the entry */
4031 *p = mc->next_index;
4032 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004033
Eyal Barif9f40652017-04-01 03:55:08 +03004034 /* Remove from hash if we deleted the last entry */
4035 if (*p == ~0 && p == first)
4036 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04004037 }
Eyal Barif9f40652017-04-01 03:55:08 +03004038 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004039}
4040
Dave Barachd7cb1b52016-12-09 09:52:16 -05004041int
4042vnet_ip6_nd_term (vlib_main_t * vm,
4043 vlib_node_runtime_t * node,
4044 vlib_buffer_t * p0,
4045 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03004046 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04004047{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004048 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4049 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
4050 pending_resolution_t *mc;
John Lo1edfba92016-08-27 01:11:57 -04004051
4052 ndh = ip6_next_header (ip);
4053 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
4054 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004055 return 0;
John Lo1edfba92016-08-27 01:11:57 -04004056
4057 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
4058 (p0->flags & VLIB_BUFFER_IS_TRACED)))
4059 {
4060 u8 *t0 = vlib_add_trace (vm, node, p0,
4061 sizeof (icmp6_input_trace_t));
4062 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
4063 }
4064
4065 /* Check if anyone want ND events for L2 BDs */
Eyal Baria0623f82017-03-30 03:05:06 +03004066 uword *p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
4067 if (p && !ip6_address_is_link_local_unicast (&ip->src_address))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004068 {
John Lo1edfba92016-08-27 01:11:57 -04004069 u32 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05004070 while (next_index != (u32) ~ 0)
4071 {
4072 int (*fp) (u32, u8 *, u32, ip6_address_t *);
John Lo1edfba92016-08-27 01:11:57 -04004073 int rv = 1;
4074 mc = pool_elt_at_index (nm->mac_changes, next_index);
4075 fp = mc->data_callback;
4076 /* Call the callback, return 1 to suppress dup events */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004077 if (fp)
4078 rv = (*fp) (mc->data,
4079 eth->src_address, sw_if_index, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004080 /* Signal the resolver process */
4081 if (rv == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004082 vlib_process_signal_event (vm, mc->node_index,
4083 mc->type_opaque, mc->data);
John Lo1edfba92016-08-27 01:11:57 -04004084 next_index = mc->next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004085 }
John Lo1edfba92016-08-27 01:11:57 -04004086 }
4087
4088 /* Check if MAC entry exsist for solicited target IP */
4089 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4090 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004091 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004092 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004093 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004094
4095 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004096 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004097 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4098 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004099 return 0; /* source link layer address option not present */
4100
John Lo1edfba92016-08-27 01:11:57 -04004101 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004102 macp =
4103 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004104 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004105 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004106 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004107 vlib_node_runtime_t *error_node =
4108 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004109 ip->dst_address = ip->src_address;
4110 ip->src_address = ndh->target_address;
4111 ip->hop_limit = 255;
4112 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004113 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004114 clib_memcpy (opt->ethernet_address, macp, 6);
4115 ndh->icmp.type = ICMP6_neighbor_advertisement;
4116 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004117 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4118 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004119 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004120 ndh->icmp.checksum =
4121 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4122 clib_memcpy (eth->dst_address, eth->src_address, 6);
4123 clib_memcpy (eth->src_address, macp, 6);
4124 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004125 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004126 return 1;
4127 }
John Lo1edfba92016-08-27 01:11:57 -04004128 }
4129
4130 return 0;
4131
4132}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004133
Neale Ranns3f844d02017-02-18 00:03:54 -08004134int
4135ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4136{
4137 u32 fib_index;
4138
4139 fib_prefix_t pfx = {
4140 .fp_len = 128,
4141 .fp_proto = FIB_PROTOCOL_IP6,
4142 .fp_addr = {
4143 .ip6 = *addr,
4144 },
4145 };
4146 ip46_address_t nh = {
4147 .ip6 = *addr,
4148 };
4149
4150 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4151
4152 if (~0 == fib_index)
4153 return VNET_API_ERROR_NO_SUCH_FIB;
4154
4155 if (is_del)
4156 {
4157 fib_table_entry_path_remove (fib_index,
4158 &pfx,
4159 FIB_SOURCE_IP6_ND_PROXY,
Neale Rannsda78f952017-05-24 09:15:43 -07004160 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004161 &nh,
4162 sw_if_index,
4163 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4164 /* flush the ND cache of this address if it's there */
4165 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4166 sw_if_index, addr, NULL, 0);
4167 }
4168 else
4169 {
4170 fib_table_entry_path_add (fib_index,
4171 &pfx,
4172 FIB_SOURCE_IP6_ND_PROXY,
4173 FIB_ENTRY_FLAG_NONE,
Neale Rannsda78f952017-05-24 09:15:43 -07004174 DPO_PROTO_IP6,
Neale Ranns3f844d02017-02-18 00:03:54 -08004175 &nh,
4176 sw_if_index,
4177 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4178 }
4179 return (0);
4180}
4181
4182static clib_error_t *
4183set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4184 unformat_input_t * input, vlib_cli_command_t * cmd)
4185{
4186 vnet_main_t *vnm = vnet_get_main ();
4187 clib_error_t *error = 0;
4188 ip6_address_t addr;
4189 u32 sw_if_index;
4190 u8 is_del = 0;
4191
4192 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4193 {
4194 /* get the rest of the command */
4195 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4196 {
4197 if (unformat (input, "%U", unformat_ip6_address, &addr))
4198 break;
4199 else if (unformat (input, "delete") || unformat (input, "del"))
4200 is_del = 1;
4201 else
4202 return (unformat_parse_error (input));
4203 }
4204 }
4205
4206 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4207
4208 return error;
4209}
4210
4211/* *INDENT-OFF* */
4212VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4213{
4214 .path = "set ip6 nd proxy",
4215 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4216 .function = set_ip6_nd_proxy_cmd,
4217};
4218/* *INDENT-ON* */
4219
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004220void
Neale Ranns3be6b282016-12-20 14:24:01 +00004221ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004222{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004223 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4224 ip6_neighbor_t *n;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004225
4226 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004227 pool_foreach (n, nm->neighbor_pool,
4228 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004229 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004230 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004231 adj_nbr_walk_nh6 (sw_if_index,
4232 &n->key.ip6_address,
4233 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004234 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004235 }));
4236 /* *INDENT-ON* */
4237}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004238
John Lo8b81cb42017-06-26 01:40:20 -04004239void
4240send_ip6_na (vlib_main_t * vm, vnet_hw_interface_t * hi)
4241{
4242 ip6_main_t *i6m = &ip6_main;
4243 u32 sw_if_index = hi->sw_if_index;
4244 ip6_address_t *ip6_addr = ip6_interface_first_address (i6m, sw_if_index);
4245 if (ip6_addr)
4246 {
4247 clib_warning
4248 ("Sending unsolicitated NA IP6 address %U on sw_if_idex %d",
4249 format_ip6_address, ip6_addr, sw_if_index);
4250
4251 /* Form unsolicited neighbor advertisement packet from NS pkt template */
4252 int bogus_length;
4253 u32 bi = 0;
4254 icmp6_neighbor_solicitation_header_t *h =
4255 vlib_packet_template_get_packet (vm,
4256 &i6m->discover_neighbor_packet_template,
4257 &bi);
4258 ip6_set_reserved_multicast_address (&h->ip.dst_address,
4259 IP6_MULTICAST_SCOPE_link_local,
4260 IP6_MULTICAST_GROUP_ID_all_hosts);
4261 h->ip.src_address = ip6_addr[0];
4262 h->neighbor.icmp.type = ICMP6_neighbor_advertisement;
4263 h->neighbor.target_address = ip6_addr[0];
4264 h->neighbor.advertisement_flags = clib_host_to_net_u32
4265 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
4266 clib_memcpy (h->link_layer_option.ethernet_address,
4267 hi->hw_address, vec_len (hi->hw_address));
4268 h->neighbor.icmp.checksum =
4269 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
4270 ASSERT (bogus_length == 0);
4271
4272 /* Setup MAC header with IP6 Etype and mcast DMAC */
4273 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
4274 vlib_buffer_advance (b, -sizeof (ethernet_header_t));
4275 ethernet_header_t *e = vlib_buffer_get_current (b);
4276 e->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP6);
4277 clib_memcpy (e->src_address, hi->hw_address, sizeof (e->src_address));
4278 ip6_multicast_ethernet_address (e->dst_address,
4279 IP6_MULTICAST_GROUP_ID_all_hosts);
4280
4281 /* Send unsolicited ND advertisement packet out the specified interface */
4282 vnet_buffer (b)->sw_if_index[VLIB_RX] =
4283 vnet_buffer (b)->sw_if_index[VLIB_TX] = sw_if_index;
4284 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
4285 u32 *to_next = vlib_frame_vector_args (f);
4286 to_next[0] = bi;
4287 f->n_vectors = 1;
4288 vlib_put_frame_to_node (vm, hi->output_node_index, f);
4289 }
4290}
4291
Dave Barachd7cb1b52016-12-09 09:52:16 -05004292/*
4293 * fd.io coding-style-patch-verification: ON
4294 *
4295 * Local Variables:
4296 * eval: (c-set-style "gnu")
4297 * End:
4298 */