blob: ee80ee3d4b4f58d8d2fcc0b926853ba52edb0f92 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * ip/ip6_neighbor.c: IP6 neighbor handling
3 *
4 * Copyright (c) 2010 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vnet/ip/ip.h>
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010019#include <vnet/ip/ip6_neighbor.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ethernet/ethernet.h>
21#include <vppinfra/mhash.h>
22#include <vppinfra/md5.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/adj/adj.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000024#include <vnet/adj/adj_mcast.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010025#include <vnet/fib/fib_table.h>
26#include <vnet/fib/ip6_fib.h>
Neale Ranns4008ac92017-02-13 23:20:04 -080027#include <vnet/mfib/ip6_mfib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Billy McFall0683c9c2016-10-13 08:27:31 -040029/**
30 * @file
31 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
32 *
33 * The files contains the API and CLI code for managing IPv6 neighbor
34 * adjacency tables and neighbor discovery logic.
35 */
36
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010037/* can't use sizeof link_layer_address, that's 8 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070038#define ETHER_MAC_ADDR_LEN 6
39
Pavel Kotucek3e046ea2016-12-05 08:27:37 +010040/* advertised prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050041typedef struct
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 /* basic advertised information */
44 ip6_address_t prefix;
45 u8 prefix_len;
46 int adv_on_link_flag;
47 int adv_autonomous_flag;
48 u32 adv_valid_lifetime_in_secs;
49 u32 adv_pref_lifetime_in_secs;
50
51 /* advertised values are computed from these times if decrementing */
52 f64 valid_lifetime_expires;
Dave Barachd7cb1b52016-12-09 09:52:16 -050053 f64 pref_lifetime_expires;
54
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 /* local information */
56 int enabled;
57 int deprecated_prefix_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050058 int decrement_lifetime_flag;
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
60#define MIN_ADV_VALID_LIFETIME 7203 /* seconds */
61#define DEF_ADV_VALID_LIFETIME 2592000
62#define DEF_ADV_PREF_LIFETIME 604800
63
64 /* extensions are added here, mobile, DNS etc.. */
65} ip6_radv_prefix_t;
66
67
Dave Barachd7cb1b52016-12-09 09:52:16 -050068typedef struct
69{
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 /* group information */
71 u8 type;
72 ip6_address_t mcast_address;
73 u16 num_sources;
74 ip6_address_t *mcast_source_address_pool;
75} ip6_mldp_group_t;
76
77/* configured router advertisement information per ipv6 interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -050078typedef struct
79{
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 /* advertised config information, zero means unspecified */
Dave Barachd7cb1b52016-12-09 09:52:16 -050082 u8 curr_hop_limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 int adv_managed_flag;
84 int adv_other_flag;
Dave Barachd7cb1b52016-12-09 09:52:16 -050085 u16 adv_router_lifetime_in_sec;
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 u32 adv_neighbor_reachable_time_in_msec;
87 u32 adv_time_in_msec_between_retransmitted_neighbor_solicitations;
88
89 /* mtu option */
90 u32 adv_link_mtu;
Dave Barachd7cb1b52016-12-09 09:52:16 -050091
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 /* source link layer option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050093 u8 link_layer_address[8];
94 u8 link_layer_addr_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 /* prefix option */
Dave Barachd7cb1b52016-12-09 09:52:16 -050097 ip6_radv_prefix_t *adv_prefixes_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
99 /* Hash table mapping address to index in interface advertised prefix pool. */
100 mhash_t address_to_prefix_index;
101
102 /* MLDP group information */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500103 ip6_mldp_group_t *mldp_group_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
105 /* Hash table mapping address to index in mldp address pool. */
106 mhash_t address_to_mldp_index;
107
108 /* local information */
109 u32 sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500110 int send_radv; /* radv on/off on this interface - set by config */
111 int cease_radv; /* we are ceasing to send - set byf config */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 int send_unicast;
113 int adv_link_layer_address;
114 int prefix_option;
115 int failed_device_check;
116 int all_routers_mcast;
117 u32 seed;
118 u64 randomizer;
119 int ref_count;
Neale Ranns32e1c012016-11-22 17:07:28 +0000120 adj_index_t mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 /* timing information */
123#define DEF_MAX_RADV_INTERVAL 200
124#define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
125#define DEF_CURR_HOP_LIMIT 64
126#define DEF_DEF_RTR_LIFETIME 3 * DEF_MAX_RADV_INTERVAL
127#define MAX_DEF_RTR_LIFETIME 9000
128
Dave Barachd7cb1b52016-12-09 09:52:16 -0500129#define MAX_INITIAL_RTR_ADVERT_INTERVAL 16 /* seconds */
130#define MAX_INITIAL_RTR_ADVERTISEMENTS 3 /*transmissions */
131#define MIN_DELAY_BETWEEN_RAS 3 /* seconds */
132#define MAX_DELAY_BETWEEN_RAS 1800 /* seconds */
133#define MAX_RA_DELAY_TIME .5 /* seconds */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 f64 max_radv_interval;
136 f64 min_radv_interval;
137 f64 min_delay_between_radv;
138 f64 max_delay_between_radv;
139 f64 max_rtr_default_lifetime;
140
141 f64 last_radv_time;
142 f64 last_multicast_time;
143 f64 next_multicast_time;
144
145
146 u32 initial_adverts_count;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500147 f64 initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 u32 initial_adverts_sent;
149
150 /* stats */
151 u32 n_advertisements_sent;
152 u32 n_solicitations_rcvd;
153 u32 n_solicitations_dropped;
154
155 /* Link local address to use (defaults to underlying physical for logical interfaces */
156 ip6_address_t link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157} ip6_radv_t;
158
Dave Barachd7cb1b52016-12-09 09:52:16 -0500159typedef struct
160{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u32 next_index;
162 uword node_index;
163 uword type_opaque;
164 uword data;
John Lo1edfba92016-08-27 01:11:57 -0400165 /* Used for nd event notification only */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500166 void *data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400167 u32 pid;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168} pending_resolution_t;
169
170
Dave Barachd7cb1b52016-12-09 09:52:16 -0500171typedef struct
172{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 /* Hash tables mapping name to opcode. */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500174 uword *opcode_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
176 /* lite beer "glean" adjacency handling */
177 mhash_t pending_resolutions_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500178 pending_resolution_t *pending_resolutions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
John Lo1edfba92016-08-27 01:11:57 -0400180 /* Mac address change notification */
181 mhash_t mac_changes_by_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500182 pending_resolution_t *mac_changes;
John Lo1edfba92016-08-27 01:11:57 -0400183
Dave Barachd7cb1b52016-12-09 09:52:16 -0500184 u32 *neighbor_input_next_index_by_hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barachd7cb1b52016-12-09 09:52:16 -0500186 ip6_neighbor_t *neighbor_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
188 mhash_t neighbor_index_by_key;
189
Dave Barachd7cb1b52016-12-09 09:52:16 -0500190 u32 *if_radv_pool_index_by_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barachd7cb1b52016-12-09 09:52:16 -0500192 ip6_radv_t *if_radv_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
194 /* Neighbor attack mitigation */
195 u32 limit_neighbor_cache_size;
196 u32 neighbor_delete_rotor;
197
198} ip6_neighbor_main_t;
199
Neale Ranns3f844d02017-02-18 00:03:54 -0800200/* ipv6 neighbor discovery - timer/event types */
201typedef enum
202{
203 ICMP6_ND_EVENT_INIT,
204} ip6_icmp_neighbor_discovery_event_type_t;
205
206typedef union
207{
208 u32 add_del_swindex;
209 struct
210 {
211 u32 up_down_swindex;
212 u32 fib_index;
213 } up_down_event;
214} ip6_icmp_neighbor_discovery_event_data_t;
215
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216static ip6_neighbor_main_t ip6_neighbor_main;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500217static ip6_address_t ip6a_zero; /* ip6 address 0 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218
Dave Barachd7cb1b52016-12-09 09:52:16 -0500219static u8 *
220format_ip6_neighbor_ip6_entry (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500222 vlib_main_t *vm = va_arg (*va, vlib_main_t *);
223 ip6_neighbor_t *n = va_arg (*va, ip6_neighbor_t *);
224 vnet_main_t *vnm = vnet_get_main ();
225 vnet_sw_interface_t *si;
226 u8 *flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Dave Barachd7cb1b52016-12-09 09:52:16 -0500228 if (!n)
229 return format (s, "%=12s%=20s%=6s%=20s%=40s", "Time", "Address", "Flags",
230 "Link layer", "Interface");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100231
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100232 if (n->flags & IP6_NEIGHBOR_FLAG_DYNAMIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500233 flags = format (flags, "D");
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100234
235 if (n->flags & IP6_NEIGHBOR_FLAG_STATIC)
Dave Barachd7cb1b52016-12-09 09:52:16 -0500236 flags = format (flags, "S");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Neale Rannsb3b2de72017-03-08 05:17:22 -0800238 if (n->flags & IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY)
239 flags = format (flags, "N");
240
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 si = vnet_get_sw_interface (vnm, n->key.sw_if_index);
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100242 s = format (s, "%=12U%=20U%=6s%=20U%=40U",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 format_vlib_cpu_time, vm, n->cpu_time_last_updated,
244 format_ip6_address, &n->key.ip6_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500245 flags ? (char *) flags : "",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 format_ethernet_address, n->link_layer_address,
247 format_vnet_sw_interface_name, vnm, si);
248
Dave Barachd7cb1b52016-12-09 09:52:16 -0500249 vec_free (flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250 return s;
251}
252
253static clib_error_t *
254ip6_neighbor_sw_interface_up_down (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500255 u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500257 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
258 ip6_neighbor_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Dave Barachd7cb1b52016-12-09 09:52:16 -0500260 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
261 {
262 u32 i, *to_delete = 0;
263
264 /* *INDENT-OFF* */
265 pool_foreach (n, nm->neighbor_pool,
266 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 if (n->key.sw_if_index == sw_if_index)
268 vec_add1 (to_delete, n - nm->neighbor_pool);
269 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500270 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
272 for (i = 0; i < vec_len (to_delete); i++)
273 {
274 n = pool_elt_at_index (nm->neighbor_pool, to_delete[i]);
275 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500276 fib_table_entry_delete_index (n->fib_entry_index, FIB_SOURCE_ADJ);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 pool_put (nm->neighbor_pool, n);
278 }
279
280 vec_free (to_delete);
281 }
282
283 return 0;
284}
285
286VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ip6_neighbor_sw_interface_up_down);
287
Dave Barachd7cb1b52016-12-09 09:52:16 -0500288static void
289unset_random_neighbor_entry (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500291 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
292 vnet_main_t *vnm = vnet_get_main ();
293 vlib_main_t *vm = vnm->vlib_main;
294 ip6_neighbor_t *e;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 u32 index;
296
297 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
298 nm->neighbor_delete_rotor = index;
299
300 /* Try again from elt 0, could happen if an intfc goes down */
301 if (index == ~0)
302 {
303 index = pool_next_index (nm->neighbor_pool, nm->neighbor_delete_rotor);
304 nm->neighbor_delete_rotor = index;
305 }
306
307 /* Nothing left in the pool */
308 if (index == ~0)
309 return;
310
311 e = pool_elt_at_index (nm->neighbor_pool, index);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500312
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 vnet_unset_ip6_ethernet_neighbor (vm, e->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500314 &e->key.ip6_address,
315 e->link_layer_address,
316 ETHER_MAC_ADDR_LEN);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317}
318
Dave Barachd7cb1b52016-12-09 09:52:16 -0500319typedef struct
320{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321 u8 is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100322 u8 is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800323 u8 is_no_fib_entry;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324 u8 link_layer_address[6];
325 u32 sw_if_index;
326 ip6_address_t addr;
327} ip6_neighbor_set_unset_rpc_args_t;
328
Dave Barachd7cb1b52016-12-09 09:52:16 -0500329static void ip6_neighbor_set_unset_rpc_callback
330 (ip6_neighbor_set_unset_rpc_args_t * a);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
Dave Barachd7cb1b52016-12-09 09:52:16 -0500332static void set_unset_ip6_neighbor_rpc
333 (vlib_main_t * vm,
334 u32 sw_if_index,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800335 ip6_address_t * a, u8 * link_layer_address, int is_add, int is_static,
336 int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337{
338 ip6_neighbor_set_unset_rpc_args_t args;
Dave Barachf9bd6202015-12-14 13:22:11 -0500339 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500340
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 args.sw_if_index = sw_if_index;
342 args.is_add = is_add;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100343 args.is_static = is_static;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800344 args.is_no_fib_entry = is_no_fib_entry;
Damjan Marionf1213b82016-03-13 02:22:06 +0100345 clib_memcpy (&args.addr, a, sizeof (*a));
Neale Ranns3f844d02017-02-18 00:03:54 -0800346 if (NULL != link_layer_address)
347 clib_memcpy (args.link_layer_address, link_layer_address, 6);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500348
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 vl_api_rpc_call_main_thread (ip6_neighbor_set_unset_rpc_callback,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500350 (u8 *) & args, sizeof (args));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100353static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500354ip6_nbr_probe (ip_adjacency_t * adj)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100355{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500356 icmp6_neighbor_solicitation_header_t *h;
357 vnet_main_t *vnm = vnet_get_main ();
358 ip6_main_t *im = &ip6_main;
359 ip_interface_address_t *ia;
360 ip6_address_t *dst, *src;
361 vnet_hw_interface_t *hi;
362 vnet_sw_interface_t *si;
363 vlib_buffer_t *b;
Neale Rannsb80c5362016-10-08 13:03:40 +0100364 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 vlib_main_t *vm;
Neale Rannsb80c5362016-10-08 13:03:40 +0100366 u32 bi = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100367
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 vm = vlib_get_main ();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100369
Dave Barachd7cb1b52016-12-09 09:52:16 -0500370 si = vnet_get_sw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100371 dst = &adj->sub_type.nbr.next_hop.ip6;
372
373 if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100375 return;
376 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500377 src = ip6_interface_address_matching_destination (im, dst,
378 adj->rewrite_header.
379 sw_if_index, &ia);
380 if (!src)
Neale Rannsb80c5362016-10-08 13:03:40 +0100381 {
382 return;
383 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100384
Dave Barachd7cb1b52016-12-09 09:52:16 -0500385 h = vlib_packet_template_get_packet (vm,
386 &im->discover_neighbor_packet_template,
387 &bi);
Neale Rannsb80c5362016-10-08 13:03:40 +0100388
Dave Barachd7cb1b52016-12-09 09:52:16 -0500389 hi = vnet_get_sup_hw_interface (vnm, adj->rewrite_header.sw_if_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100390
391 h->ip.dst_address.as_u8[13] = dst->as_u8[13];
392 h->ip.dst_address.as_u8[14] = dst->as_u8[14];
393 h->ip.dst_address.as_u8[15] = dst->as_u8[15];
394 h->ip.src_address = src[0];
395 h->neighbor.target_address = dst[0];
396
397 clib_memcpy (h->link_layer_option.ethernet_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500398 hi->hw_address, vec_len (hi->hw_address));
Neale Rannsb80c5362016-10-08 13:03:40 +0100399
400 h->neighbor.icmp.checksum =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500401 ip6_tcp_udp_icmp_compute_checksum (vm, 0, &h->ip, &bogus_length);
402 ASSERT (bogus_length == 0);
Neale Rannsb80c5362016-10-08 13:03:40 +0100403
404 b = vlib_get_buffer (vm, bi);
405 vnet_buffer (b)->sw_if_index[VLIB_RX] =
Dave Barachd7cb1b52016-12-09 09:52:16 -0500406 vnet_buffer (b)->sw_if_index[VLIB_TX] = adj->rewrite_header.sw_if_index;
Neale Rannsb80c5362016-10-08 13:03:40 +0100407
408 /* Add encapsulation string for software interface (e.g. ethernet header). */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500409 vnet_rewrite_one_header (adj[0], h, sizeof (ethernet_header_t));
410 vlib_buffer_advance (b, -adj->rewrite_header.data_bytes);
Neale Rannsb80c5362016-10-08 13:03:40 +0100411
412 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500413 vlib_frame_t *f = vlib_get_frame_to_node (vm, hi->output_node_index);
414 u32 *to_next = vlib_frame_vector_args (f);
415 to_next[0] = bi;
416 f->n_vectors = 1;
417 vlib_put_frame_to_node (vm, hi->output_node_index, f);
Neale Rannsb80c5362016-10-08 13:03:40 +0100418 }
419}
420
421static void
422ip6_nd_mk_complete (adj_index_t ai, ip6_neighbor_t * nbr)
423{
424 adj_nbr_update_rewrite (ai, ADJ_NBR_REWRITE_FLAG_COMPLETE,
425 ethernet_build_rewrite (vnet_get_main (),
426 nbr->key.sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500427 adj_get_link_type (ai),
Neale Rannsb80c5362016-10-08 13:03:40 +0100428 nbr->link_layer_address));
429}
430
431static void
Neale Ranns19c68d22016-12-07 15:38:14 +0000432ip6_nd_mk_incomplete (adj_index_t ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100433{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500434 ip_adjacency_t *adj = adj_get (ai);
Neale Ranns19c68d22016-12-07 15:38:14 +0000435
Dave Barachd7cb1b52016-12-09 09:52:16 -0500436 adj_nbr_update_rewrite (ai,
437 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
438 ethernet_build_rewrite (vnet_get_main (),
439 adj->rewrite_header.
440 sw_if_index,
441 adj_get_link_type (ai),
442 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
Neale Rannsb80c5362016-10-08 13:03:40 +0100443}
444
445#define IP6_NBR_MK_KEY(k, sw_if_index, addr) \
446{ \
447 k.sw_if_index = sw_if_index; \
448 k.ip6_address = *addr; \
449 k.pad = 0; \
450}
451
452static ip6_neighbor_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -0500453ip6_nd_find (u32 sw_if_index, const ip6_address_t * addr)
Neale Rannsb80c5362016-10-08 13:03:40 +0100454{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500455 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
456 ip6_neighbor_t *n = NULL;
Neale Rannsb80c5362016-10-08 13:03:40 +0100457 ip6_neighbor_key_t k;
458 uword *p;
459
Dave Barachd7cb1b52016-12-09 09:52:16 -0500460 IP6_NBR_MK_KEY (k, sw_if_index, addr);
Neale Rannsb80c5362016-10-08 13:03:40 +0100461
462 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500463 if (p)
464 {
465 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
466 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100467
468 return (n);
469}
470
471static adj_walk_rc_t
472ip6_nd_mk_complete_walk (adj_index_t ai, void *ctx)
473{
474 ip6_neighbor_t *nbr = ctx;
475
476 ip6_nd_mk_complete (ai, nbr);
477
478 return (ADJ_WALK_RC_CONTINUE);
479}
480
481static adj_walk_rc_t
482ip6_nd_mk_incomplete_walk (adj_index_t ai, void *ctx)
483{
Neale Ranns19c68d22016-12-07 15:38:14 +0000484 ip6_nd_mk_incomplete (ai);
Neale Rannsb80c5362016-10-08 13:03:40 +0100485
486 return (ADJ_WALK_RC_CONTINUE);
487}
488
489void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500490ip6_ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
Neale Rannsb80c5362016-10-08 13:03:40 +0100491{
492 ip6_neighbor_t *nbr;
493 ip_adjacency_t *adj;
494
495 adj = adj_get (ai);
496
497 nbr = ip6_nd_find (sw_if_index, &adj->sub_type.nbr.next_hop.ip6);
498
Neale Ranns32e1c012016-11-22 17:07:28 +0000499 switch (adj->lookup_next_index)
Neale Rannsb80c5362016-10-08 13:03:40 +0100500 {
Neale Ranns32e1c012016-11-22 17:07:28 +0000501 case IP_LOOKUP_NEXT_ARP:
502 case IP_LOOKUP_NEXT_GLEAN:
503 if (NULL != nbr)
504 {
505 adj_nbr_walk_nh6 (sw_if_index, &nbr->key.ip6_address,
506 ip6_nd_mk_complete_walk, nbr);
507 }
508 else
509 {
510 /*
511 * no matching ND entry.
512 * construct the rewrite required to for an ND packet, and stick
513 * that in the adj's pipe to smoke.
514 */
515 adj_nbr_update_rewrite (ai,
516 ADJ_NBR_REWRITE_FLAG_INCOMPLETE,
517 ethernet_build_rewrite (vnm,
518 sw_if_index,
519 VNET_LINK_IP6,
520 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
521
522 /*
523 * since the FIB has added this adj for a route, it makes sense it may
524 * want to forward traffic sometime soon. Let's send a speculative ND.
525 * just one. If we were to do periodically that wouldn't be bad either,
526 * but that's more code than i'm prepared to write at this time for
527 * relatively little reward.
528 */
529 ip6_nbr_probe (adj);
530 }
531 break;
532 case IP_LOOKUP_NEXT_MCAST:
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700533 {
534 /*
535 * Construct a partial rewrite from the known ethernet mcast dest MAC
536 */
537 u8 *rewrite;
538 u8 offset;
Neale Rannsb80c5362016-10-08 13:03:40 +0100539
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700540 rewrite = ethernet_build_rewrite (vnm,
541 sw_if_index,
542 adj->ia_link,
543 ethernet_ip6_mcast_dst_addr ());
Neale Ranns32e1c012016-11-22 17:07:28 +0000544
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700545 /*
546 * Complete the remaining fields of the adj's rewrite to direct the
547 * complete of the rewrite at switch time by copying in the IP
548 * dst address's bytes.
549 * Ofset is 2 bytes into the desintation address. And we write 4 bytes.
550 */
551 offset = vec_len (rewrite) - 2;
552 adj_mcast_update_rewrite (ai, rewrite, offset, 0xffffffff);
Neale Ranns32e1c012016-11-22 17:07:28 +0000553
Neale Ranns2e7fbcc2017-03-15 04:22:25 -0700554 break;
555 }
Neale Ranns32e1c012016-11-22 17:07:28 +0000556 case IP_LOOKUP_NEXT_DROP:
557 case IP_LOOKUP_NEXT_PUNT:
558 case IP_LOOKUP_NEXT_LOCAL:
559 case IP_LOOKUP_NEXT_REWRITE:
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800560 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
Neale Ranns32e1c012016-11-22 17:07:28 +0000561 case IP_LOOKUP_NEXT_MIDCHAIN:
562 case IP_LOOKUP_NEXT_ICMP_ERROR:
563 case IP_LOOKUP_N_NEXT:
564 ASSERT (0);
565 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100566 }
567}
568
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569int
570vnet_set_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500571 u32 sw_if_index,
572 ip6_address_t * a,
573 u8 * link_layer_address,
574 uword n_bytes_link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800575 int is_static, int is_no_fib_entry)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500577 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500579 ip6_neighbor_t *n = 0;
580 int make_new_nd_cache_entry = 1;
581 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 u32 next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500583 pending_resolution_t *pr, *mc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
Damjan Marion586afd72017-04-05 19:18:20 +0200585 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586 {
587 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800588 1 /* set new neighbor */ , is_static,
589 is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590 return 0;
591 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
593 k.sw_if_index = sw_if_index;
594 k.ip6_address = a[0];
595 k.pad = 0;
596
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 p = mhash_get (&nm->neighbor_index_by_key, &k);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500598 if (p)
599 {
600 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
601 /* Refuse to over-write static neighbor entry. */
602 if (!is_static && (n->flags & IP6_NEIGHBOR_FLAG_STATIC))
603 return -2;
604 make_new_nd_cache_entry = 0;
605 }
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100606
Dave Barachd7cb1b52016-12-09 09:52:16 -0500607 if (make_new_nd_cache_entry)
608 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500609 pool_get (nm->neighbor_pool, n);
610 mhash_set (&nm->neighbor_index_by_key, &k, n - nm->neighbor_pool,
611 /* old value */ 0);
612 n->key = k;
Neale Rannsb80c5362016-10-08 13:03:40 +0100613
Dave Barachd7cb1b52016-12-09 09:52:16 -0500614 clib_memcpy (n->link_layer_address,
615 link_layer_address, n_bytes_link_layer_address);
Neale Rannsb80c5362016-10-08 13:03:40 +0100616
Dave Barachd7cb1b52016-12-09 09:52:16 -0500617 /*
618 * create the adj-fib. the entry in the FIB table for and to the peer.
619 */
Neale Rannsb3b2de72017-03-08 05:17:22 -0800620 if (!is_no_fib_entry)
621 {
622 fib_prefix_t pfx = {
623 .fp_len = 128,
624 .fp_proto = FIB_PROTOCOL_IP6,
625 .fp_addr.ip6 = k.ip6_address,
626 };
627 u32 fib_index;
628
629 fib_index = ip6_main.fib_index_by_sw_if_index[n->key.sw_if_index];
630 n->fib_entry_index =
631 fib_table_entry_update_one_path (fib_index, &pfx,
632 FIB_SOURCE_ADJ,
Neale Rannsf12a83f2017-04-18 09:09:40 -0700633 FIB_ENTRY_FLAG_ATTACHED,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800634 FIB_PROTOCOL_IP6, &pfx.fp_addr,
635 n->key.sw_if_index, ~0, 1, NULL,
636 FIB_ROUTE_PATH_FLAG_NONE);
Neale Ranns2a3ea492017-04-19 05:24:40 -0700637 }
638 else
639 {
Neale Rannsb3b2de72017-03-08 05:17:22 -0800640 n->flags |= IP6_NEIGHBOR_FLAG_NO_FIB_ENTRY;
641 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500642 }
Neale Ranns33a7dd52016-10-07 15:14:33 +0100643 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500644 {
645 /*
646 * prevent a DoS attack from the data-plane that
647 * spams us with no-op updates to the MAC address
648 */
649 if (0 == memcmp (n->link_layer_address,
650 link_layer_address, n_bytes_link_layer_address))
651 return -1;
Neale Rannsb80c5362016-10-08 13:03:40 +0100652
Dave Barachd7cb1b52016-12-09 09:52:16 -0500653 clib_memcpy (n->link_layer_address,
654 link_layer_address, n_bytes_link_layer_address);
655 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Neale Rannsb80c5362016-10-08 13:03:40 +0100657 /* Update time stamp and flags. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658 n->cpu_time_last_updated = clib_cpu_time_now ();
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100659 if (is_static)
660 n->flags |= IP6_NEIGHBOR_FLAG_STATIC;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100661 else
662 n->flags |= IP6_NEIGHBOR_FLAG_DYNAMIC;
663
Neale Rannsb80c5362016-10-08 13:03:40 +0100664 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500665 &n->key.ip6_address, ip6_nd_mk_complete_walk, n);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666
667 /* Customer(s) waiting for this address to be resolved? */
668 p = mhash_get (&nm->pending_resolutions_by_address, a);
John Lo1edfba92016-08-27 01:11:57 -0400669 if (p)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 {
John Lo1edfba92016-08-27 01:11:57 -0400671 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500672
673 while (next_index != (u32) ~ 0)
674 {
John Lo1edfba92016-08-27 01:11:57 -0400675 pr = pool_elt_at_index (nm->pending_resolutions, next_index);
676 vlib_process_signal_event (vm, pr->node_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500677 pr->type_opaque, pr->data);
John Lo1edfba92016-08-27 01:11:57 -0400678 next_index = pr->next_index;
679 pool_put (nm->pending_resolutions, pr);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500680 }
John Lo1edfba92016-08-27 01:11:57 -0400681
682 mhash_unset (&nm->pending_resolutions_by_address, a, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 }
684
John Lo1edfba92016-08-27 01:11:57 -0400685 /* Customer(s) requesting ND event for this address? */
686 p = mhash_get (&nm->mac_changes_by_address, a);
687 if (p)
688 {
689 next_index = p[0];
690
Dave Barachd7cb1b52016-12-09 09:52:16 -0500691 while (next_index != (u32) ~ 0)
692 {
693 int (*fp) (u32, u8 *, u32, ip6_address_t *);
694 int rv = 1;
695 mc = pool_elt_at_index (nm->mac_changes, next_index);
696 fp = mc->data_callback;
John Lo1edfba92016-08-27 01:11:57 -0400697
Dave Barachd7cb1b52016-12-09 09:52:16 -0500698 /* Call the user's data callback, return 1 to suppress dup events */
699 if (fp)
700 rv =
701 (*fp) (mc->data, link_layer_address, sw_if_index, &ip6a_zero);
702 /*
703 * Signal the resolver process, as long as the user
704 * says they want to be notified
705 */
706 if (rv == 0)
707 vlib_process_signal_event (vm, mc->node_index,
708 mc->type_opaque, mc->data);
709 next_index = mc->next_index;
710 }
John Lo1edfba92016-08-27 01:11:57 -0400711 }
712
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713 return 0;
714}
715
716int
717vnet_unset_ip6_ethernet_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500718 u32 sw_if_index,
719 ip6_address_t * a,
720 u8 * link_layer_address,
721 uword n_bytes_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500723 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724 ip6_neighbor_key_t k;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500725 ip6_neighbor_t *n;
726 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727 int rv = 0;
728
Damjan Marion586afd72017-04-05 19:18:20 +0200729 if (vlib_get_thread_index ())
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730 {
731 set_unset_ip6_neighbor_rpc (vm, sw_if_index, a, link_layer_address,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800732 0 /* unset */ , 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 return 0;
734 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735
736 k.sw_if_index = sw_if_index;
737 k.ip6_address = a[0];
738 k.pad = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500739
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740 p = mhash_get (&nm->neighbor_index_by_key, &k);
741 if (p == 0)
742 {
743 rv = -1;
744 goto out;
745 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500746
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747 n = pool_elt_at_index (nm->neighbor_pool, p[0]);
Neale Ranns19c68d22016-12-07 15:38:14 +0000748 mhash_unset (&nm->neighbor_index_by_key, &n->key, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100749
Neale Rannsb80c5362016-10-08 13:03:40 +0100750 adj_nbr_walk_nh6 (sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500751 &n->key.ip6_address, ip6_nd_mk_incomplete_walk, NULL);
Neale Rannsb80c5362016-10-08 13:03:40 +0100752
Dave Barachd7cb1b52016-12-09 09:52:16 -0500753 fib_table_entry_delete_index (n->fib_entry_index, FIB_SOURCE_ADJ);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 pool_put (nm->neighbor_pool, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500755
756out:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 return rv;
758}
759
Dave Barachd7cb1b52016-12-09 09:52:16 -0500760static void ip6_neighbor_set_unset_rpc_callback
761 (ip6_neighbor_set_unset_rpc_args_t * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500763 vlib_main_t *vm = vlib_get_main ();
764 if (a->is_add)
765 vnet_set_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
Neale Rannsb3b2de72017-03-08 05:17:22 -0800766 a->link_layer_address, 6, a->is_static,
767 a->is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500769 vnet_unset_ip6_ethernet_neighbor (vm, a->sw_if_index, &a->addr,
770 a->link_layer_address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
773static int
774ip6_neighbor_sort (void *a1, void *a2)
775{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500776 vnet_main_t *vnm = vnet_get_main ();
777 ip6_neighbor_t *n1 = a1, *n2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 int cmp;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500779 cmp = vnet_sw_interface_compare (vnm, n1->key.sw_if_index,
780 n2->key.sw_if_index);
781 if (!cmp)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 cmp = ip6_address_compare (&n1->key.ip6_address, &n2->key.ip6_address);
783 return cmp;
784}
785
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100786ip6_neighbor_t *
787ip6_neighbors_entries (u32 sw_if_index)
788{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500789 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100790 ip6_neighbor_t *n, *ns = 0;
791
792 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500793 pool_foreach (n, nm->neighbor_pool,
794 ({
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100795 if (sw_if_index != ~0 && n->key.sw_if_index != sw_if_index)
796 continue;
797 vec_add1 (ns, n[0]);
798 }));
799 /* *INDENT-ON* */
800
801 if (ns)
802 vec_sort_with_function (ns, ip6_neighbor_sort);
803 return ns;
804}
805
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806static clib_error_t *
807show_ip6_neighbors (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500808 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500810 vnet_main_t *vnm = vnet_get_main ();
811 ip6_neighbor_t *n, *ns;
812 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700813 u32 sw_if_index;
814
815 /* Filter entries by interface if given. */
816 sw_if_index = ~0;
817 (void) unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index);
818
Pavel Kotucek3e046ea2016-12-05 08:27:37 +0100819 ns = ip6_neighbors_entries (sw_if_index);
Billy McFall46529cd2016-10-14 10:45:49 -0400820 if (ns)
821 {
Billy McFall46529cd2016-10-14 10:45:49 -0400822 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500823 vec_foreach (n, ns)
824 {
825 vlib_cli_output (vm, "%U", format_ip6_neighbor_ip6_entry, vm, n);
Billy McFall46529cd2016-10-14 10:45:49 -0400826 }
827 vec_free (ns);
828 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829
830 return error;
831}
832
Billy McFall0683c9c2016-10-13 08:27:31 -0400833/*?
834 * This command is used to display the adjacent IPv6 hosts found via
835 * neighbor discovery. Optionally, limit the output to the specified
836 * interface.
837 *
838 * @cliexpar
839 * Example of how to display the IPv6 neighbor adjacency table:
840 * @cliexstart{show ip6 neighbors}
841 * Time Address Flags Link layer Interface
842 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
843 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
844 * 886.6654 ::1:1:c:0:9 S 02:fe:e4:45:27:5b GigabitEthernet3/0/0
845 * @cliexend
846 * Example of how to display the IPv6 neighbor adjacency table for given interface:
847 * @cliexstart{show ip6 neighbors GigabitEthernet2/0/0}
848 * Time Address Flags Link layer Interface
849 * 34.0910 ::a:1:1:0:7 02:fe:6a:07:39:6f GigabitEthernet2/0/0
850 * 173.2916 ::b:5:1:c:2 02:fe:50:62:3a:94 GigabitEthernet2/0/0
851 * @cliexend
852?*/
853/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854VLIB_CLI_COMMAND (show_ip6_neighbors_command, static) = {
855 .path = "show ip6 neighbors",
856 .function = show_ip6_neighbors,
Billy McFall0683c9c2016-10-13 08:27:31 -0400857 .short_help = "show ip6 neighbors [<interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858};
Billy McFall0683c9c2016-10-13 08:27:31 -0400859/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
861static clib_error_t *
862set_ip6_neighbor (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500863 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500865 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700866 ip6_address_t addr;
867 u8 mac_address[6];
868 int addr_valid = 0;
869 int is_del = 0;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100870 int is_static = 0;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800871 int is_no_fib_entry = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872 u32 sw_if_index;
873
Dave Barachd7cb1b52016-12-09 09:52:16 -0500874 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 {
876 /* intfc, ip6-address, mac-address */
877 if (unformat (input, "%U %U %U",
Dave Barachd7cb1b52016-12-09 09:52:16 -0500878 unformat_vnet_sw_interface, vnm, &sw_if_index,
879 unformat_ip6_address, &addr,
880 unformat_ethernet_address, mac_address))
881 addr_valid = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882
883 else if (unformat (input, "delete") || unformat (input, "del"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500884 is_del = 1;
Pierre Pfister1dabaaf2016-04-25 14:15:15 +0100885 else if (unformat (input, "static"))
Dave Barachd7cb1b52016-12-09 09:52:16 -0500886 is_static = 1;
Neale Rannsb3b2de72017-03-08 05:17:22 -0800887 else if (unformat (input, "no-fib-entry"))
888 is_no_fib_entry = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889 else
Dave Barachd7cb1b52016-12-09 09:52:16 -0500890 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891 }
892
893 if (!addr_valid)
894 return clib_error_return (0, "Missing interface, ip6 or hw address");
Dave Barachd7cb1b52016-12-09 09:52:16 -0500895
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896 if (!is_del)
897 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500898 mac_address, sizeof (mac_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -0800899 is_static, is_no_fib_entry);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 else
901 vnet_unset_ip6_ethernet_neighbor (vm, sw_if_index, &addr,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500902 mac_address, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 return 0;
904}
905
Billy McFall0683c9c2016-10-13 08:27:31 -0400906/*?
907 * This command is used to manually add an entry to the IPv6 neighbor
908 * adjacency table. Optionally, the entry can be added as static. It is
909 * also used to remove an entry from the table. Use the '<em>show ip6
910 * neighbors</em>' command to display all learned and manually entered entries.
911 *
912 * @cliexpar
913 * Example of how to add a static entry to the IPv6 neighbor adjacency table:
914 * @cliexcmd{set ip6 neighbor GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b static}
915 * Example of how to delete an entry from the IPv6 neighbor adjacency table:
916 * @cliexcmd{set ip6 neighbor del GigabitEthernet2/0/0 ::1:1:c:0:9 02:fe:e4:45:27:5b}
917?*/
918/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -0500919VLIB_CLI_COMMAND (set_ip6_neighbor_command, static) =
920{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921 .path = "set ip6 neighbor",
922 .function = set_ip6_neighbor,
Billy McFall0683c9c2016-10-13 08:27:31 -0400923 .short_help = "set ip6 neighbor [del] <interface> <ip6-address> <mac-address> [static]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924};
Billy McFall0683c9c2016-10-13 08:27:31 -0400925/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926
Dave Barachd7cb1b52016-12-09 09:52:16 -0500927typedef enum
928{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
930 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
931 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
932} icmp6_neighbor_solicitation_or_advertisement_next_t;
933
934static_always_inline uword
935icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
936 vlib_node_runtime_t * node,
937 vlib_frame_t * frame,
938 uword is_solicitation)
939{
Dave Barachd7cb1b52016-12-09 09:52:16 -0500940 vnet_main_t *vnm = vnet_get_main ();
941 ip6_main_t *im = &ip6_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500943 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
945 icmp6_neighbor_discovery_option_type_t option_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500946 vlib_node_runtime_t *error_node =
947 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 int bogus_length;
949
950 from = vlib_frame_vector_args (frame);
951 n_left_from = n_packets;
952 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500953
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 if (node->flags & VLIB_NODE_FLAG_TRACE)
955 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
956 /* stride */ 1,
957 sizeof (icmp6_input_trace_t));
958
Dave Barachd7cb1b52016-12-09 09:52:16 -0500959 option_type =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960 (is_solicitation
961 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
962 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
963 n_advertisements_sent = 0;
964
965 while (n_left_from > 0)
966 {
967 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
968
969 while (n_left_from > 0 && n_left_to_next > 0)
970 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500971 vlib_buffer_t *p0;
972 ip6_header_t *ip0;
973 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
974 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500976 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
977 int is_rewrite0;
978 u32 ni0;
979
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980 bi0 = to_next[0] = from[0];
981
982 from += 1;
983 to_next += 1;
984 n_left_from -= 1;
985 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500986
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987 p0 = vlib_get_buffer (vm, bi0);
988 ip0 = vlib_buffer_get_current (p0);
989 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -0500990 options_len0 =
991 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992
993 error0 = ICMP6_ERROR_NONE;
994 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -0500995 ip6_sadd_link_local =
996 ip6_address_is_link_local_unicast (&ip0->src_address);
997 ip6_sadd_unspecified =
998 ip6_address_is_unspecified (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999
1000 /* Check that source address is unspecified, link-local or else on-link. */
1001 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
1002 {
1003 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004
Dave Barachd7cb1b52016-12-09 09:52:16 -05001005 if (ADJ_INDEX_INVALID != src_adj_index0)
1006 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001007 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008
Dave Barachd7cb1b52016-12-09 09:52:16 -05001009 /* Allow all realistic-looking rewrite adjacencies to pass */
1010 ni0 = adj0->lookup_next_index;
1011 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
1012 (ni0 < IP6_LOOKUP_N_NEXT);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001013
Dave Barachd7cb1b52016-12-09 09:52:16 -05001014 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
1015 || !is_rewrite0)
1016 ?
1017 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
1018 : error0);
1019 }
1020 else
1021 {
1022 error0 =
1023 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
1024 }
1025 }
1026
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027 o0 = (void *) (h0 + 1);
1028 o0 = ((options_len0 == 8 && o0->header.type == option_type
1029 && o0->header.n_data_u64s == 1) ? o0 : 0);
1030
1031 /* If src address unspecified or link local, donot learn neighbor MAC */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001032 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
Neale Ranns2a3ea492017-04-19 05:24:40 -07001033 !ip6_sadd_unspecified))
Dave Barachd7cb1b52016-12-09 09:52:16 -05001034 {
1035 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1036 if (nm->limit_neighbor_cache_size &&
1037 pool_elts (nm->neighbor_pool) >=
1038 nm->limit_neighbor_cache_size)
1039 unset_random_neighbor_entry ();
1040 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1041 is_solicitation ?
1042 &ip0->src_address :
1043 &h0->target_address,
1044 o0->ethernet_address,
1045 sizeof (o0->ethernet_address),
Neale Ranns2a3ea492017-04-19 05:24:40 -07001046 0, ip6_sadd_link_local);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001047 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
1049 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1050 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001051 /* Check that target address is local to this router. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001052 fib_node_index_t fei;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001053 u32 fib_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Dave Barachd7cb1b52016-12-09 09:52:16 -05001055 fib_index =
1056 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001058 if (~0 == fib_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001059 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001060 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1061 }
1062 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001063 {
1064 fei = ip6_fib_table_lookup_exact_match (fib_index,
1065 &h0->target_address,
1066 128);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001067
Neale Ranns3f844d02017-02-18 00:03:54 -08001068 if (FIB_NODE_INDEX_INVALID == fei)
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001069 {
Neale Ranns3f844d02017-02-18 00:03:54 -08001070 /* The target address is not in the FIB */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001071 error0 =
1072 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001073 }
Neale Ranns3f844d02017-02-18 00:03:54 -08001074 else
1075 {
1076 if (FIB_ENTRY_FLAG_LOCAL &
1077 fib_entry_get_flags_for_source (fei,
1078 FIB_SOURCE_INTERFACE))
1079 {
1080 /* It's an address that belongs to one of our interfaces
1081 * that's good. */
1082 }
1083 else
1084 if (fib_entry_is_sourced
1085 (fei, FIB_SOURCE_IP6_ND_PROXY))
1086 {
1087 /* The address was added by IPv6 Proxy ND config.
1088 * We should only respond to these if the NS arrived on
1089 * the link that has a matching covering prefix */
1090 }
1091 else
1092 {
1093 error0 =
1094 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
1095 }
1096 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001097 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 }
1099
1100 if (is_solicitation)
Dave Barachd7cb1b52016-12-09 09:52:16 -05001101 next0 = (error0 != ICMP6_ERROR_NONE
1102 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
1103 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104 else
1105 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001106 next0 = 0;
1107 error0 = error0 == ICMP6_ERROR_NONE ?
1108 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 }
1110
1111 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
1112 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001113 vnet_sw_interface_t *sw_if0;
1114 ethernet_interface_t *eth_if0;
1115 ethernet_header_t *eth0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116
Dave Barachd7cb1b52016-12-09 09:52:16 -05001117 /* dst address is either source address or the all-nodes mcast addr */
1118 if (!ip6_sadd_unspecified)
1119 ip0->dst_address = ip0->src_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001121 ip6_set_reserved_multicast_address (&ip0->dst_address,
1122 IP6_MULTICAST_SCOPE_link_local,
1123 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124
1125 ip0->src_address = h0->target_address;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001126 ip0->hop_limit = 255;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127 h0->icmp.type = ICMP6_neighbor_advertisement;
1128
1129 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1130 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001131 eth_if0 =
1132 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133 if (eth_if0 && o0)
1134 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001135 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
1136 o0->header.type =
1137 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138 }
1139
1140 h0->advertisement_flags = clib_host_to_net_u32
1141 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
1142 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
1143
1144 h0->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001145 h0->icmp.checksum =
1146 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1147 &bogus_length);
1148 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149
Dave Barachd7cb1b52016-12-09 09:52:16 -05001150 /* Reuse current MAC header, copy SMAC to DMAC and
1151 * interface MAC to SMAC */
1152 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
1153 eth0 = vlib_buffer_get_current (p0);
1154 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
1155 if (eth_if0)
1156 clib_memcpy (eth0->src_address, eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157
Dave Barachd7cb1b52016-12-09 09:52:16 -05001158 /* Setup input and output sw_if_index for packet */
1159 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
1160 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
1161 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
1162 vnet_main.local_interface_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163
1164 n_advertisements_sent++;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001165 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001166
1167 p0->error = error_node->errors[error0];
1168
1169 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1170 to_next, n_left_to_next,
1171 bi0, next0);
1172 }
1173
1174 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1175 }
1176
1177 /* Account for advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001178 vlib_error_count (vm, error_node->node_index,
1179 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
1180 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001181
1182 return frame->n_vectors;
1183}
1184
1185/* for "syslogging" - use elog for now */
1186#define foreach_log_level \
1187 _ (DEBUG, "DEBUG") \
1188 _ (INFO, "INFORMATION") \
1189 _ (NOTICE, "NOTICE") \
1190 _ (WARNING, "WARNING") \
1191 _ (ERR, "ERROR") \
1192 _ (CRIT, "CRITICAL") \
1193 _ (ALERT, "ALERT") \
1194 _ (EMERG, "EMERGENCY")
1195
Dave Barachd7cb1b52016-12-09 09:52:16 -05001196typedef enum
1197{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198#define _(f,s) LOG_##f,
1199 foreach_log_level
1200#undef _
1201} log_level_t;
1202
Dave Barachd7cb1b52016-12-09 09:52:16 -05001203static char *log_level_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204#define _(f,s) s,
1205 foreach_log_level
1206#undef _
1207};
1208
Dave Barachd7cb1b52016-12-09 09:52:16 -05001209static int logmask = 1 << LOG_DEBUG;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001210
1211static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05001212ip6_neighbor_syslog (vlib_main_t * vm, int priority, char *fmt, ...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001213{
1214 /* just use elog for now */
1215 u8 *what;
1216 va_list va;
1217
Dave Barachd7cb1b52016-12-09 09:52:16 -05001218 if ((priority > LOG_EMERG) || !(logmask & (1 << priority)))
1219 return;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220
1221 va_start (va, fmt);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001222 if (fmt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001223 {
1224 what = va_format (0, fmt, &va);
1225
Dave Barachd7cb1b52016-12-09 09:52:16 -05001226 ELOG_TYPE_DECLARE (e) =
1227 {
1228 .format = "ip6 nd: (%s): %s",.format_args = "T4T4",};
1229 struct
1230 {
1231 u32 s[2];
1232 } *ed;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001233 ed = ELOG_DATA (&vm->elog_main, e);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001234 ed->s[0] = elog_string (&vm->elog_main, log_level_strings[priority]);
1235 ed->s[1] = elog_string (&vm->elog_main, (char *) what);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236 }
1237 va_end (va);
1238 return;
1239}
1240
1241/* ipv6 neighbor discovery - router advertisements */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001242typedef enum
1243{
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244 ICMP6_ROUTER_SOLICITATION_NEXT_DROP,
1245 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW,
1246 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX,
1247 ICMP6_ROUTER_SOLICITATION_N_NEXT,
1248} icmp6_router_solicitation_or_advertisement_next_t;
1249
1250static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001251icmp6_router_solicitation (vlib_main_t * vm,
1252 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001253{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001254 vnet_main_t *vnm = vnet_get_main ();
1255 ip6_main_t *im = &ip6_main;
1256 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001258 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 u32 n_left_from, n_left_to_next, next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001260 u32 n_advertisements_sent = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261 int bogus_length;
1262
1263 icmp6_neighbor_discovery_option_type_t option_type;
1264
Dave Barachd7cb1b52016-12-09 09:52:16 -05001265 vlib_node_runtime_t *error_node =
1266 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001267
1268 from = vlib_frame_vector_args (frame);
1269 n_left_from = n_packets;
1270 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001271
Ed Warnickecb9cada2015-12-08 15:45:58 -07001272 if (node->flags & VLIB_NODE_FLAG_TRACE)
1273 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1274 /* stride */ 1,
1275 sizeof (icmp6_input_trace_t));
1276
1277 /* source may append his LL address */
1278 option_type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
1279
1280 while (n_left_from > 0)
1281 {
1282 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001283
Ed Warnickecb9cada2015-12-08 15:45:58 -07001284 while (n_left_from > 0 && n_left_to_next > 0)
1285 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001286 vlib_buffer_t *p0;
1287 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001288 ip6_radv_t *radv_info = 0;
1289
Dave Barachd7cb1b52016-12-09 09:52:16 -05001290 icmp6_neighbor_discovery_header_t *h0;
1291 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
1292
Ed Warnickecb9cada2015-12-08 15:45:58 -07001293 u32 bi0, options_len0, sw_if_index0, next0, error0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001294 u32 is_solicitation = 1, is_dropped = 0;
1295 u32 is_unspecified, is_link_local;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296
1297 bi0 = to_next[0] = from[0];
1298
1299 from += 1;
1300 to_next += 1;
1301 n_left_from -= 1;
1302 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001303
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 p0 = vlib_get_buffer (vm, bi0);
1305 ip0 = vlib_buffer_get_current (p0);
1306 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001307 options_len0 =
1308 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
1309 is_unspecified = ip6_address_is_unspecified (&ip0->src_address);
1310 is_link_local =
1311 ip6_address_is_link_local_unicast (&ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001312
1313 error0 = ICMP6_ERROR_NONE;
1314 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
Dave Barachd7cb1b52016-12-09 09:52:16 -05001315
Ed Warnickecb9cada2015-12-08 15:45:58 -07001316 /* check if solicitation (not from nd_timer node) */
1317 if (ip6_address_is_unspecified (&ip0->dst_address))
1318 is_solicitation = 0;
1319
1320 /* Check that source address is unspecified, link-local or else on-link. */
1321 if (!is_unspecified && !is_link_local)
1322 {
1323 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001324
Dave Barachd7cb1b52016-12-09 09:52:16 -05001325 if (ADJ_INDEX_INVALID != src_adj_index0)
1326 {
Neale Ranns107e7d42017-04-11 09:55:19 -07001327 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001328
Dave Barachd7cb1b52016-12-09 09:52:16 -05001329 error0 = (adj0->rewrite_header.sw_if_index != sw_if_index0
1330 ?
1331 ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK
1332 : error0);
1333 }
1334 else
1335 {
1336 error0 = ICMP6_ERROR_ROUTER_SOLICITATION_SOURCE_NOT_ON_LINK;
1337 }
1338 }
1339
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340 /* check for source LL option and process */
1341 o0 = (void *) (h0 + 1);
1342 o0 = ((options_len0 == 8
1343 && o0->header.type == option_type
Dave Barachd7cb1b52016-12-09 09:52:16 -05001344 && o0->header.n_data_u64s == 1) ? o0 : 0);
1345
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 /* if src address unspecified IGNORE any options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001347 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
1348 !is_unspecified && !is_link_local))
1349 {
1350 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
1351 if (nm->limit_neighbor_cache_size &&
1352 pool_elts (nm->neighbor_pool) >=
1353 nm->limit_neighbor_cache_size)
1354 unset_random_neighbor_entry ();
1355
1356 vnet_set_ip6_ethernet_neighbor (vm, sw_if_index0,
1357 &ip0->src_address,
1358 o0->ethernet_address,
1359 sizeof (o0->ethernet_address),
Neale Rannsb3b2de72017-03-08 05:17:22 -08001360 0, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001361 }
1362
Ed Warnickecb9cada2015-12-08 15:45:58 -07001363 /* default is to drop */
1364 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001365
Ed Warnickecb9cada2015-12-08 15:45:58 -07001366 if (error0 == ICMP6_ERROR_NONE)
1367 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001368 vnet_sw_interface_t *sw_if0;
1369 ethernet_interface_t *eth_if0;
1370 u32 adj_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371
1372 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1373 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001374 eth_if0 =
1375 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376
1377 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001378 error0 =
1379 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1380 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001381
1382 if (error0 == ICMP6_ERROR_NONE)
1383 {
1384 u32 ri;
1385
1386 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001387 p0->current_length -=
1388 (options_len0 +
1389 sizeof (icmp6_neighbor_discovery_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390
1391 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001392 vec_validate_init_empty
1393 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394
1395 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1396
Dave Barachd7cb1b52016-12-09 09:52:16 -05001397 if (ri != ~0)
1398 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1399
1400 error0 =
1401 ((!radv_info) ?
1402 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1403 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001404
1405 if (error0 == ICMP6_ERROR_NONE)
1406 {
1407 f64 now = vlib_time_now (vm);
1408
1409 /* for solicited adverts - need to rate limit */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001410 if (is_solicitation)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001411 {
Neale Ranns75152282017-01-09 01:00:45 -08001412 if (0 != radv_info->last_radv_time &&
1413 (now - radv_info->last_radv_time) <
Dave Barachd7cb1b52016-12-09 09:52:16 -05001414 MIN_DELAY_BETWEEN_RAS)
1415 is_dropped = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001416 else
1417 radv_info->last_radv_time = now;
1418 }
1419
1420 /* send now */
1421 icmp6_router_advertisement_header_t rh;
1422
1423 rh.icmp.type = ICMP6_router_advertisement;
1424 rh.icmp.code = 0;
1425 rh.icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001426
Ed Warnickecb9cada2015-12-08 15:45:58 -07001427 rh.current_hop_limit = radv_info->curr_hop_limit;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001428 rh.router_lifetime_in_sec =
1429 clib_host_to_net_u16
1430 (radv_info->adv_router_lifetime_in_sec);
1431 rh.
1432 time_in_msec_between_retransmitted_neighbor_solicitations
1433 =
1434 clib_host_to_net_u32 (radv_info->
1435 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
1436 rh.neighbor_reachable_time_in_msec =
1437 clib_host_to_net_u32 (radv_info->
1438 adv_neighbor_reachable_time_in_msec);
1439
1440 rh.flags =
1441 (radv_info->adv_managed_flag) ?
1442 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP :
1443 0;
1444 rh.flags |=
1445 ((radv_info->adv_other_flag) ?
1446 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP :
1447 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001448
1449
Dave Barachd7cb1b52016-12-09 09:52:16 -05001450 u16 payload_length =
1451 sizeof (icmp6_router_advertisement_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001452
1453 vlib_buffer_add_data (vm,
1454 p0->free_list_index,
1455 bi0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001456 (void *) &rh,
1457 sizeof
1458 (icmp6_router_advertisement_header_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001459
Dave Barachd7cb1b52016-12-09 09:52:16 -05001460 if (radv_info->adv_link_layer_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001461 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001462 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t
1463 h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001464
Dave Barachd7cb1b52016-12-09 09:52:16 -05001465 h.header.type =
1466 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001467 h.header.n_data_u64s = 1;
1468
1469 /* copy ll address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001470 clib_memcpy (&h.ethernet_address[0],
1471 eth_if0->address, 6);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001472
1473 vlib_buffer_add_data (vm,
1474 p0->free_list_index,
1475 bi0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001476 (void *) &h,
1477 sizeof
1478 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001479
Dave Barachd7cb1b52016-12-09 09:52:16 -05001480 payload_length +=
1481 sizeof
1482 (icmp6_neighbor_discovery_ethernet_link_layer_address_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001483 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001484
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485 /* add MTU option */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001486 if (radv_info->adv_link_mtu)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001487 {
1488 icmp6_neighbor_discovery_mtu_option_t h;
1489
1490 h.unused = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001491 h.mtu =
1492 clib_host_to_net_u32 (radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001493 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu;
1494 h.header.n_data_u64s = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001495
1496 payload_length +=
1497 sizeof (icmp6_neighbor_discovery_mtu_option_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001498
1499 vlib_buffer_add_data (vm,
1500 p0->free_list_index,
1501 bi0,
Dave Barachd7cb1b52016-12-09 09:52:16 -05001502 (void *) &h,
1503 sizeof
1504 (icmp6_neighbor_discovery_mtu_option_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001506
Ed Warnickecb9cada2015-12-08 15:45:58 -07001507 /* add advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001508 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001509
Dave Barachd7cb1b52016-12-09 09:52:16 -05001510 /* *INDENT-OFF* */
1511 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1512 ({
1513 if(pr_info->enabled &&
1514 (!pr_info->decrement_lifetime_flag
1515 || (pr_info->pref_lifetime_expires >0)))
1516 {
1517 /* advertise this prefix */
1518 icmp6_neighbor_discovery_prefix_information_option_t h;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001519
Dave Barachd7cb1b52016-12-09 09:52:16 -05001520 h.header.type = ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information;
1521 h.header.n_data_u64s = (sizeof(icmp6_neighbor_discovery_prefix_information_option_t) >> 3);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522
Dave Barachd7cb1b52016-12-09 09:52:16 -05001523 h.dst_address_length = pr_info->prefix_len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001524
Dave Barachd7cb1b52016-12-09 09:52:16 -05001525 h.flags = (pr_info->adv_on_link_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_FLAG_ON_LINK : 0;
1526 h.flags |= (pr_info->adv_autonomous_flag) ? ICMP6_NEIGHBOR_DISCOVERY_PREFIX_INFORMATION_AUTO : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001527
Dave Barachd7cb1b52016-12-09 09:52:16 -05001528 if(radv_info->cease_radv && pr_info->deprecated_prefix_flag)
1529 {
1530 h.valid_time = clib_host_to_net_u32(MIN_ADV_VALID_LIFETIME);
1531 h.preferred_time = 0;
1532 }
1533 else
1534 {
1535 if(pr_info->decrement_lifetime_flag)
1536 {
1537 pr_info->adv_valid_lifetime_in_secs = ((pr_info->valid_lifetime_expires > now)) ?
1538 (pr_info->valid_lifetime_expires - now) : 0;
1539
1540 pr_info->adv_pref_lifetime_in_secs = ((pr_info->pref_lifetime_expires > now)) ?
1541 (pr_info->pref_lifetime_expires - now) : 0;
1542 }
1543
1544 h.valid_time = clib_host_to_net_u32(pr_info->adv_valid_lifetime_in_secs);
1545 h.preferred_time = clib_host_to_net_u32(pr_info->adv_pref_lifetime_in_secs) ;
1546 }
1547 h.unused = 0;
1548
1549 clib_memcpy(&h.dst_address, &pr_info->prefix, sizeof(ip6_address_t));
1550
1551 payload_length += sizeof( icmp6_neighbor_discovery_prefix_information_option_t);
1552
1553 vlib_buffer_add_data (vm,
1554 p0->free_list_index,
1555 bi0,
1556 (void *)&h, sizeof(icmp6_neighbor_discovery_prefix_information_option_t));
1557
1558 }
1559 }));
1560 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001561
1562 /* add additional options before here */
1563
1564 /* finish building the router advertisement... */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001565 if (!is_unspecified && radv_info->send_unicast)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001566 {
1567 ip0->dst_address = ip0->src_address;
1568 }
1569 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05001570 {
1571 /* target address is all-nodes mcast addr */
1572 ip6_set_reserved_multicast_address
1573 (&ip0->dst_address,
1574 IP6_MULTICAST_SCOPE_link_local,
1575 IP6_MULTICAST_GROUP_ID_all_hosts);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001576 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001577
Ed Warnickecb9cada2015-12-08 15:45:58 -07001578 /* source address MUST be the link-local address */
1579 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001580
Dave Barachd7cb1b52016-12-09 09:52:16 -05001581 ip0->hop_limit = 255;
1582 ip0->payload_length =
1583 clib_host_to_net_u16 (payload_length);
1584
1585 icmp6_router_advertisement_header_t *rh0 =
1586 (icmp6_router_advertisement_header_t *) (ip0 + 1);
1587 rh0->icmp.checksum =
1588 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
1589 &bogus_length);
1590 ASSERT (bogus_length == 0);
1591
Ed Warnickecb9cada2015-12-08 15:45:58 -07001592 /* setup output if and adjacency */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001593 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001595
1596 if (is_solicitation)
1597 {
1598 ethernet_header_t *eth0;
1599 /* Reuse current MAC header, copy SMAC to DMAC and
1600 * interface MAC to SMAC */
1601 vlib_buffer_reset (p0);
1602 eth0 = vlib_buffer_get_current (p0);
1603 clib_memcpy (eth0->dst_address, eth0->src_address,
1604 6);
1605 clib_memcpy (eth0->src_address, eth_if0->address,
1606 6);
1607 next0 =
1608 is_dropped ? next0 :
1609 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX;
1610 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
1611 sw_if_index0;
1612 }
1613 else
1614 {
Neale Ranns32e1c012016-11-22 17:07:28 +00001615 adj_index0 = radv_info->mcast_adj_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001616 if (adj_index0 == 0)
1617 error0 = ICMP6_ERROR_DST_LOOKUP_MISS;
1618 else
1619 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001620 next0 =
1621 is_dropped ? next0 :
1622 ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW;
1623 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
1624 adj_index0;
1625 }
1626 }
1627 p0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
1628
1629 radv_info->n_solicitations_dropped += is_dropped;
1630 radv_info->n_solicitations_rcvd += is_solicitation;
1631
1632 if ((error0 == ICMP6_ERROR_NONE) && !is_dropped)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001633 {
1634 radv_info->n_advertisements_sent++;
1635 n_advertisements_sent++;
1636 }
1637 }
1638 }
1639 }
1640
1641 p0->error = error_node->errors[error0];
1642
Dave Barachd7cb1b52016-12-09 09:52:16 -05001643 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001644 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001645
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1647 to_next, n_left_to_next,
1648 bi0, next0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001649
Ed Warnickecb9cada2015-12-08 15:45:58 -07001650 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001651
Ed Warnickecb9cada2015-12-08 15:45:58 -07001652 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1653 }
1654
1655 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001656 vlib_error_count (vm, error_node->node_index,
1657 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_TX,
1658 n_advertisements_sent);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001659
1660 return frame->n_vectors;
1661}
1662
1663 /* validate advertised info for consistancy (see RFC-4861 section 6.2.7) - log any inconsistencies, packet will always be dropped */
1664static_always_inline uword
Dave Barachd7cb1b52016-12-09 09:52:16 -05001665icmp6_router_advertisement (vlib_main_t * vm,
1666 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001667{
Dave Barachd7cb1b52016-12-09 09:52:16 -05001668 vnet_main_t *vnm = vnet_get_main ();
1669 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001670 uword n_packets = frame->n_vectors;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001671 u32 *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001672 u32 n_left_from, n_left_to_next, next_index;
1673 u32 n_advertisements_rcvd = 0;
1674
Dave Barachd7cb1b52016-12-09 09:52:16 -05001675 vlib_node_runtime_t *error_node =
1676 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001677
1678 from = vlib_frame_vector_args (frame);
1679 n_left_from = n_packets;
1680 next_index = node->cached_next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001681
Ed Warnickecb9cada2015-12-08 15:45:58 -07001682 if (node->flags & VLIB_NODE_FLAG_TRACE)
1683 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
1684 /* stride */ 1,
1685 sizeof (icmp6_input_trace_t));
1686
1687 while (n_left_from > 0)
1688 {
1689 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001690
Ed Warnickecb9cada2015-12-08 15:45:58 -07001691 while (n_left_from > 0 && n_left_to_next > 0)
1692 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001693 vlib_buffer_t *p0;
1694 ip6_header_t *ip0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001695 ip6_radv_t *radv_info = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001696 icmp6_router_advertisement_header_t *h0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001697 u32 bi0, options_len0, sw_if_index0, next0, error0;
1698
1699 bi0 = to_next[0] = from[0];
1700
1701 from += 1;
1702 to_next += 1;
1703 n_left_from -= 1;
1704 n_left_to_next -= 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001705
Ed Warnickecb9cada2015-12-08 15:45:58 -07001706 p0 = vlib_get_buffer (vm, bi0);
1707 ip0 = vlib_buffer_get_current (p0);
1708 h0 = ip6_next_header (ip0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001709 options_len0 =
1710 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001711
1712 error0 = ICMP6_ERROR_NONE;
1713 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
1714
Dave Barachd7cb1b52016-12-09 09:52:16 -05001715 /* Check that source address is link-local */
1716 error0 = (!ip6_address_is_link_local_unicast (&ip0->src_address)) ?
Ed Warnickecb9cada2015-12-08 15:45:58 -07001717 ICMP6_ERROR_ROUTER_ADVERTISEMENT_SOURCE_NOT_LINK_LOCAL : error0;
1718
1719 /* default is to drop */
1720 next0 = ICMP6_ROUTER_SOLICITATION_NEXT_DROP;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001721
Ed Warnickecb9cada2015-12-08 15:45:58 -07001722 n_advertisements_rcvd++;
1723
1724 if (error0 == ICMP6_ERROR_NONE)
1725 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001726 vnet_sw_interface_t *sw_if0;
1727 ethernet_interface_t *eth_if0;
1728
Ed Warnickecb9cada2015-12-08 15:45:58 -07001729 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
1730 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001731 eth_if0 =
1732 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001733
1734 /* only support ethernet interface type for now */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001735 error0 =
1736 (!eth_if0) ? ICMP6_ERROR_ROUTER_SOLICITATION_UNSUPPORTED_INTF
1737 : error0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001738
1739 if (error0 == ICMP6_ERROR_NONE)
1740 {
1741 u32 ri;
1742
1743 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001744 vec_validate_init_empty
1745 (nm->if_radv_pool_index_by_sw_if_index, sw_if_index0, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001746
1747 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index0];
1748
Dave Barachd7cb1b52016-12-09 09:52:16 -05001749 if (ri != ~0)
1750 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
1751
1752 error0 =
1753 ((!radv_info) ?
1754 ICMP6_ERROR_ROUTER_SOLICITATION_RADV_NOT_CONFIG :
1755 error0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001756
1757 if (error0 == ICMP6_ERROR_NONE)
1758 {
1759 /* validate advertised information */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001760 if ((h0->current_hop_limit && radv_info->curr_hop_limit)
1761 && (h0->current_hop_limit !=
1762 radv_info->curr_hop_limit))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001764 ip6_neighbor_syslog (vm, LOG_WARNING,
1765 "our AdvCurHopLimit on %U doesn't agree with %U",
1766 format_vnet_sw_if_index_name,
1767 vnm, sw_if_index0,
1768 format_ip6_address,
1769 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001770 }
1771
Dave Barachd7cb1b52016-12-09 09:52:16 -05001772 if ((h0->flags &
1773 ICMP6_ROUTER_DISCOVERY_FLAG_ADDRESS_CONFIG_VIA_DHCP)
1774 != radv_info->adv_managed_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001775 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001776 ip6_neighbor_syslog (vm, LOG_WARNING,
1777 "our AdvManagedFlag on %U doesn't agree with %U",
1778 format_vnet_sw_if_index_name,
1779 vnm, sw_if_index0,
1780 format_ip6_address,
1781 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001782 }
1783
Dave Barachd7cb1b52016-12-09 09:52:16 -05001784 if ((h0->flags &
1785 ICMP6_ROUTER_DISCOVERY_FLAG_OTHER_CONFIG_VIA_DHCP)
1786 != radv_info->adv_other_flag)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001787 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001788 ip6_neighbor_syslog (vm, LOG_WARNING,
1789 "our AdvOtherConfigFlag on %U doesn't agree with %U",
1790 format_vnet_sw_if_index_name,
1791 vnm, sw_if_index0,
1792 format_ip6_address,
1793 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001794 }
1795
Dave Barachd7cb1b52016-12-09 09:52:16 -05001796 if ((h0->
1797 time_in_msec_between_retransmitted_neighbor_solicitations
1798 && radv_info->
1799 adv_time_in_msec_between_retransmitted_neighbor_solicitations)
1800 && (h0->
1801 time_in_msec_between_retransmitted_neighbor_solicitations
1802 !=
1803 clib_host_to_net_u32 (radv_info->
1804 adv_time_in_msec_between_retransmitted_neighbor_solicitations)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001805 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001806 ip6_neighbor_syslog (vm, LOG_WARNING,
1807 "our AdvRetransTimer on %U doesn't agree with %U",
1808 format_vnet_sw_if_index_name,
1809 vnm, sw_if_index0,
1810 format_ip6_address,
1811 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001812 }
1813
Dave Barachd7cb1b52016-12-09 09:52:16 -05001814 if ((h0->neighbor_reachable_time_in_msec &&
1815 radv_info->adv_neighbor_reachable_time_in_msec) &&
1816 (h0->neighbor_reachable_time_in_msec !=
1817 clib_host_to_net_u32
1818 (radv_info->adv_neighbor_reachable_time_in_msec)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001819 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001820 ip6_neighbor_syslog (vm, LOG_WARNING,
1821 "our AdvReachableTime on %U doesn't agree with %U",
1822 format_vnet_sw_if_index_name,
1823 vnm, sw_if_index0,
1824 format_ip6_address,
1825 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001826 }
1827
1828 /* check for MTU or prefix options or .. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001829 u8 *opt_hdr = (u8 *) (h0 + 1);
1830 while (options_len0 > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001831 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001832 icmp6_neighbor_discovery_option_header_t *o0 =
1833 (icmp6_neighbor_discovery_option_header_t *)
1834 opt_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001835 int opt_len = o0->n_data_u64s << 3;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001836 icmp6_neighbor_discovery_option_type_t option_type =
1837 o0->type;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001838
Dave Barachd7cb1b52016-12-09 09:52:16 -05001839 if (options_len0 < 2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001841 ip6_neighbor_syslog (vm, LOG_ERR,
1842 "malformed RA packet on %U from %U",
1843 format_vnet_sw_if_index_name,
1844 vnm, sw_if_index0,
1845 format_ip6_address,
1846 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001847 break;
1848 }
1849
Dave Barachd7cb1b52016-12-09 09:52:16 -05001850 if (opt_len == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001851 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001852 ip6_neighbor_syslog (vm, LOG_ERR,
1853 " zero length option in RA on %U from %U",
1854 format_vnet_sw_if_index_name,
1855 vnm, sw_if_index0,
1856 format_ip6_address,
1857 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001858 break;
1859 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001860 else if (opt_len > options_len0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001861 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001862 ip6_neighbor_syslog (vm, LOG_ERR,
1863 "option length in RA packet greater than total length on %U from %U",
1864 format_vnet_sw_if_index_name,
1865 vnm, sw_if_index0,
1866 format_ip6_address,
1867 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001868 break;
1869 }
1870
1871 options_len0 -= opt_len;
1872 opt_hdr += opt_len;
1873
Dave Barachd7cb1b52016-12-09 09:52:16 -05001874 switch (option_type)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001875 {
1876 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_mtu:
Dave Barachd7cb1b52016-12-09 09:52:16 -05001877 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878 icmp6_neighbor_discovery_mtu_option_t *h =
Dave Barachd7cb1b52016-12-09 09:52:16 -05001879 (icmp6_neighbor_discovery_mtu_option_t
1880 *) (o0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001881
Dave Barachd7cb1b52016-12-09 09:52:16 -05001882 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001883 break;
1884
Dave Barachd7cb1b52016-12-09 09:52:16 -05001885 if ((h->mtu && radv_info->adv_link_mtu) &&
1886 (h->mtu !=
1887 clib_host_to_net_u32
1888 (radv_info->adv_link_mtu)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001889 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001890 ip6_neighbor_syslog (vm, LOG_WARNING,
1891 "our AdvLinkMTU on %U doesn't agree with %U",
1892 format_vnet_sw_if_index_name,
1893 vnm, sw_if_index0,
1894 format_ip6_address,
1895 &ip0->src_address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001896 }
1897 }
1898 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05001899
Ed Warnickecb9cada2015-12-08 15:45:58 -07001900 case ICMP6_NEIGHBOR_DISCOVERY_OPTION_prefix_information:
1901 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05001902 icmp6_neighbor_discovery_prefix_information_option_t
1903 * h =
1904 (icmp6_neighbor_discovery_prefix_information_option_t
1905 *) (o0);
1906
Ed Warnickecb9cada2015-12-08 15:45:58 -07001907 /* validate advertised prefix options */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001908 ip6_radv_prefix_t *pr_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001909 u32 preferred, valid;
1910
Dave Barachd7cb1b52016-12-09 09:52:16 -05001911 if (opt_len < sizeof (*h))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001912 break;
1913
Dave Barachd7cb1b52016-12-09 09:52:16 -05001914 preferred =
1915 clib_net_to_host_u32 (h->preferred_time);
1916 valid = clib_net_to_host_u32 (h->valid_time);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917
1918 /* look for matching prefix - if we our advertising it, it better be consistant */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001919 /* *INDENT-OFF* */
1920 pool_foreach (pr_info, radv_info->adv_prefixes_pool,
1921 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07001922
Dave Barachd7cb1b52016-12-09 09:52:16 -05001923 ip6_address_t mask;
1924 ip6_address_mask_from_width(&mask, pr_info->prefix_len);
1925
1926 if(pr_info->enabled &&
1927 (pr_info->prefix_len == h->dst_address_length) &&
1928 ip6_address_is_equal_masked (&pr_info->prefix, &h->dst_address, &mask))
1929 {
1930 /* found it */
1931 if(!pr_info->decrement_lifetime_flag &&
1932 valid != pr_info->adv_valid_lifetime_in_secs)
1933 {
1934 ip6_neighbor_syslog(vm, LOG_WARNING,
1935 "our ADV validlifetime on %U for %U does not agree with %U",
1936 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1937 format_ip6_address, &h->dst_address);
1938 }
1939 if(!pr_info->decrement_lifetime_flag &&
1940 preferred != pr_info->adv_pref_lifetime_in_secs)
1941 {
1942 ip6_neighbor_syslog(vm, LOG_WARNING,
1943 "our ADV preferredlifetime on %U for %U does not agree with %U",
1944 format_vnet_sw_if_index_name, vnm, sw_if_index0,format_ip6_address, &pr_info->prefix,
1945 format_ip6_address, &h->dst_address);
1946 }
1947 }
1948 break;
1949 }));
1950 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001951 break;
1952 }
1953 default:
1954 /* skip this one */
1955 break;
1956 }
1957 }
1958 }
1959 }
1960 }
1961
1962 p0->error = error_node->errors[error0];
1963
Dave Barachd7cb1b52016-12-09 09:52:16 -05001964 if (error0 != ICMP6_ERROR_NONE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001965 vlib_error_count (vm, error_node->node_index, error0, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05001966
Ed Warnickecb9cada2015-12-08 15:45:58 -07001967 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1968 to_next, n_left_to_next,
1969 bi0, next0);
1970 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05001971
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1973 }
1974
1975 /* Account for router advertisements sent. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05001976 vlib_error_count (vm, error_node->node_index,
1977 ICMP6_ERROR_ROUTER_ADVERTISEMENTS_RX,
1978 n_advertisements_rcvd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001979
1980 return frame->n_vectors;
1981}
1982
Neale Ranns87df12d2017-02-18 08:16:41 -08001983/**
1984 * @brief Add a multicast Address to the advertised MLD set
1985 */
1986static void
1987ip6_neighbor_add_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
1988{
1989 ip6_mldp_group_t *mcast_group_info;
1990 uword *p;
1991
1992 /* lookup mldp info for this interface */
1993 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
1994 mcast_group_info =
1995 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
1996
1997 /* add address */
1998 if (!mcast_group_info)
1999 {
2000 /* add */
2001 u32 mi;
2002 pool_get (radv_info->mldp_group_pool, mcast_group_info);
2003
2004 mi = mcast_group_info - radv_info->mldp_group_pool;
2005 mhash_set (&radv_info->address_to_mldp_index, &addr, mi, /* old_value */
2006 0);
2007
2008 mcast_group_info->type = 4;
2009 mcast_group_info->mcast_source_address_pool = 0;
2010 mcast_group_info->num_sources = 0;
2011 clib_memcpy (&mcast_group_info->mcast_address, &addr,
2012 sizeof (ip6_address_t));
2013 }
2014}
2015
2016/**
2017 * @brief Delete a multicast Address from the advertised MLD set
2018 */
2019static void
2020ip6_neighbor_del_mld_prefix (ip6_radv_t * radv_info, ip6_address_t * addr)
2021{
2022 ip6_mldp_group_t *mcast_group_info;
2023 uword *p;
2024
2025 p = mhash_get (&radv_info->address_to_mldp_index, &addr);
2026 mcast_group_info =
2027 p ? pool_elt_at_index (radv_info->mldp_group_pool, p[0]) : 0;
2028
2029 if (mcast_group_info)
2030 {
2031 mhash_unset (&radv_info->address_to_mldp_index, &addr,
2032 /* old_value */ 0);
2033 pool_put (radv_info->mldp_group_pool, mcast_group_info);
2034 }
2035}
2036
2037/**
2038 * @brief Add a multicast Address to the advertised MLD set
2039 */
2040static void
2041ip6_neighbor_add_mld_grp (ip6_radv_t * a,
2042 ip6_multicast_address_scope_t scope,
2043 ip6_multicast_link_local_group_id_t group)
2044{
2045 ip6_address_t addr;
2046
2047 ip6_set_reserved_multicast_address (&addr, scope, group);
2048
2049 ip6_neighbor_add_mld_prefix (a, &addr);
2050}
2051
2052/**
2053 * @brief create and initialize router advertisement parameters with default
2054 * values for this intfc
2055 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002056static u32
2057ip6_neighbor_sw_interface_add_del (vnet_main_t * vnm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002058 u32 sw_if_index, u32 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002059{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002060 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2061 ip6_radv_t *a = 0;
Neale Ranns0bfe5d82016-08-25 15:29:12 +01002062 u32 ri = ~0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002063 vnet_sw_interface_t *sw_if0;
2064 ethernet_interface_t *eth_if0 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002065
2066 /* lookup radv container - ethernet interfaces only */
2067 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002068 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002069 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2070
Dave Barachd7cb1b52016-12-09 09:52:16 -05002071 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002072 return ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002073
2074 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2075 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002076 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2077
Dave Barachd7cb1b52016-12-09 09:52:16 -05002078 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002079 {
2080 a = pool_elt_at_index (nm->if_radv_pool, ri);
2081
Dave Barachd7cb1b52016-12-09 09:52:16 -05002082 if (!is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002083 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002084 ip6_radv_prefix_t *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002085 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002086
Neale Ranns32e1c012016-11-22 17:07:28 +00002087 /* release the lock on the interface's mcast adj */
2088 adj_unlock (a->mcast_adj_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002089
Neale Ranns87df12d2017-02-18 08:16:41 -08002090 /* clean up prefix and MDP pools */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002091 /* *INDENT-OFF* */
Neale Ranns87df12d2017-02-18 08:16:41 -08002092 pool_flush(p, a->adv_prefixes_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002093 ({
Ed Warnickecb9cada2015-12-08 15:45:58 -07002094 mhash_unset (&a->address_to_prefix_index, &p->prefix, 0);
Neale Ranns87df12d2017-02-18 08:16:41 -08002095 }));
2096 pool_flush (m, a->mldp_group_pool,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002097 ({
Neale Ranns87df12d2017-02-18 08:16:41 -08002098 mhash_unset (&a->address_to_mldp_index, &m->mcast_address, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002099 }));
2100 /* *INDENT-ON* */
2101
Neale Ranns87df12d2017-02-18 08:16:41 -08002102 pool_free (a->mldp_group_pool);
2103 pool_free (a->adv_prefixes_pool);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002104
Neale Ranns87df12d2017-02-18 08:16:41 -08002105 mhash_free (&a->address_to_prefix_index);
2106 mhash_free (&a->address_to_mldp_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002107
2108 pool_put (nm->if_radv_pool, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002109 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ~0;
2110 ri = ~0;
2111 }
2112 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002113 else
2114 {
2115 if (is_add)
2116 {
2117 vnet_hw_interface_t *hw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002118
Dave Barachd7cb1b52016-12-09 09:52:16 -05002119 hw_if0 = vnet_get_sup_hw_interface (vnm, sw_if_index);
2120
2121 pool_get (nm->if_radv_pool, a);
2122
2123 ri = a - nm->if_radv_pool;
2124 nm->if_radv_pool_index_by_sw_if_index[sw_if_index] = ri;
2125
2126 /* initialize default values (most of which are zero) */
2127 memset (a, 0, sizeof (a[0]));
2128
2129 a->sw_if_index = sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002130 a->max_radv_interval = DEF_MAX_RADV_INTERVAL;
2131 a->min_radv_interval = DEF_MIN_RADV_INTERVAL;
2132 a->curr_hop_limit = DEF_CURR_HOP_LIMIT;
2133 a->adv_router_lifetime_in_sec = DEF_DEF_RTR_LIFETIME;
2134
Neale Ranns87df12d2017-02-18 08:16:41 -08002135 /* send ll address source address option */
2136 a->adv_link_layer_address = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002137
2138 a->min_delay_between_radv = MIN_DELAY_BETWEEN_RAS;
2139 a->max_delay_between_radv = MAX_DELAY_BETWEEN_RAS;
2140 a->max_rtr_default_lifetime = MAX_DEF_RTR_LIFETIME;
2141 a->seed = (u32) clib_cpu_time_now ();
2142 (void) random_u32 (&a->seed);
2143 a->randomizer = clib_cpu_time_now ();
2144 (void) random_u64 (&a->randomizer);
2145
2146 a->initial_adverts_count = MAX_INITIAL_RTR_ADVERTISEMENTS;
2147 a->initial_adverts_sent = a->initial_adverts_count - 1;
2148 a->initial_adverts_interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
2149
2150 /* deafult is to send */
2151 a->send_radv = 1;
2152
2153 /* fill in radv_info for this interface that will be needed later */
2154 a->adv_link_mtu = hw_if0->max_l3_packet_bytes[VLIB_RX];
2155
2156 clib_memcpy (a->link_layer_address, eth_if0->address, 6);
2157
2158 /* fill in default link-local address (this may be overridden) */
2159 ip6_link_local_address_from_ethernet_address
2160 (&a->link_local_address, eth_if0->address);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002161
2162 mhash_init (&a->address_to_prefix_index, sizeof (uword),
2163 sizeof (ip6_address_t));
2164 mhash_init (&a->address_to_mldp_index, sizeof (uword),
2165 sizeof (ip6_address_t));
2166
Neale Ranns32e1c012016-11-22 17:07:28 +00002167 a->mcast_adj_index = adj_mcast_add_or_lock (FIB_PROTOCOL_IP6,
2168 VNET_LINK_IP6,
2169 sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002170
2171 /* add multicast groups we will always be reporting */
Neale Ranns87df12d2017-02-18 08:16:41 -08002172 ip6_neighbor_add_mld_grp (a,
2173 IP6_MULTICAST_SCOPE_link_local,
2174 IP6_MULTICAST_GROUP_ID_all_hosts);
2175 ip6_neighbor_add_mld_grp (a,
2176 IP6_MULTICAST_SCOPE_link_local,
2177 IP6_MULTICAST_GROUP_ID_all_routers);
2178 ip6_neighbor_add_mld_grp (a,
2179 IP6_MULTICAST_SCOPE_link_local,
2180 IP6_MULTICAST_GROUP_ID_mldv2_routers);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002181 }
2182 }
2183 return ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002184}
2185
2186/* send an mldpv2 report */
2187static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05002188ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002189{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002190 vnet_main_t *vnm = vnet_get_main ();
2191 vlib_main_t *vm = vnm->vlib_main;
2192 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2193 vnet_sw_interface_t *sw_if0;
2194 ethernet_interface_t *eth_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002195 u32 ri;
2196 int bogus_length;
2197
Dave Barachd7cb1b52016-12-09 09:52:16 -05002198 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002199 u16 payload_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002200 vlib_buffer_t *b0;
2201 ip6_header_t *ip0;
2202 u32 *to_next;
2203 vlib_frame_t *f;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002204 u32 bo0;
2205 u32 n_to_alloc = 1;
2206 u32 n_allocated;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002207
Ed Warnickecb9cada2015-12-08 15:45:58 -07002208 icmp6_multicast_listener_report_header_t *rh0;
2209 icmp6_multicast_listener_report_packet_t *rp0;
2210
2211 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
2212 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
2213 eth_if0 = ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2214
2215 if (!eth_if0 || !vnet_sw_interface_is_admin_up (vnm, sw_if_index))
2216 return;
2217
2218 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002219 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2220 ~0);
2221
Ed Warnickecb9cada2015-12-08 15:45:58 -07002222 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002223
2224 if (ri == ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002225 return;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002226
Ed Warnickecb9cada2015-12-08 15:45:58 -07002227 /* send report now - build a mldpv2 report packet */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002228 n_allocated = vlib_buffer_alloc_from_free_list (vm,
2229 &bo0,
2230 n_to_alloc,
2231 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
2232 if (PREDICT_FALSE (n_allocated == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002233 {
2234 clib_warning ("buffer allocation failure");
2235 return;
2236 }
2237
2238 b0 = vlib_get_buffer (vm, bo0);
2239
2240 /* adjust the sizeof the buffer to just include the ipv6 header */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002241 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002242
Dave Barachd7cb1b52016-12-09 09:52:16 -05002243 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002244
2245 b0->error = ICMP6_ERROR_NONE;
2246
2247 rp0 = vlib_buffer_get_current (b0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002248 ip0 = (ip6_header_t *) & rp0->ip;
2249 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002250
Dave Barachd7cb1b52016-12-09 09:52:16 -05002251 memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
2252
2253 ip0->ip_version_traffic_class_and_flow_label =
2254 clib_host_to_net_u32 (0x6 << 28);
2255
2256 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002257 /* for DEBUG - vnet driver won't seem to emit router alerts */
2258 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
2259 ip0->hop_limit = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002260
Ed Warnickecb9cada2015-12-08 15:45:58 -07002261 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002262
Ed Warnickecb9cada2015-12-08 15:45:58 -07002263 /* source address MUST be the link-local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002264 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
2265 ip0->src_address = radv_info->link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002266
2267 /* destination is all mldpv2 routers */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002268 ip6_set_reserved_multicast_address (&ip0->dst_address,
2269 IP6_MULTICAST_SCOPE_link_local,
2270 IP6_MULTICAST_GROUP_ID_mldv2_routers);
2271
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272 /* add reports here */
2273 ip6_mldp_group_t *m;
2274 int num_addr_records = 0;
2275 icmp6_multicast_address_record_t rr;
2276
2277 /* fill in the hop-by-hop extension header (router alert) info */
2278 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
2279 rh0->ext_hdr.n_data_u64s = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002280
Ed Warnickecb9cada2015-12-08 15:45:58 -07002281 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
2282 rh0->alert.len = 2;
2283 rh0->alert.value = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002284
Ed Warnickecb9cada2015-12-08 15:45:58 -07002285 rh0->pad.type = 1;
2286 rh0->pad.len = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002287
Ed Warnickecb9cada2015-12-08 15:45:58 -07002288 rh0->icmp.checksum = 0;
2289
Dave Barachd7cb1b52016-12-09 09:52:16 -05002290 /* *INDENT-OFF* */
2291 pool_foreach (m, radv_info->mldp_group_pool,
2292 ({
2293 rr.type = m->type;
2294 rr.aux_data_len_u32s = 0;
2295 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
2296 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002297
Dave Barachd7cb1b52016-12-09 09:52:16 -05002298 num_addr_records++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002299
Dave Barachd7cb1b52016-12-09 09:52:16 -05002300 vlib_buffer_add_data
2301 (vm, b0->free_list_index, bo0,
2302 (void *)&rr, sizeof(icmp6_multicast_address_record_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002303
Dave Barachd7cb1b52016-12-09 09:52:16 -05002304 payload_length += sizeof( icmp6_multicast_address_record_t);
2305 }));
2306 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002307
2308 rh0->rsvd = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002309 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
2310
Ed Warnickecb9cada2015-12-08 15:45:58 -07002311 /* update lengths */
2312 ip0->payload_length = clib_host_to_net_u16 (payload_length);
2313
Dave Barachd7cb1b52016-12-09 09:52:16 -05002314 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
2315 &bogus_length);
2316 ASSERT (bogus_length == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002317
Dave Barachd7cb1b52016-12-09 09:52:16 -05002318 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002319 * OK to override w/ no regard for actual FIB, because
Neale Rannsf06aea52016-11-29 06:51:37 -08002320 * ip6-rewrite only looks at the adjacency.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002321 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002322 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
Ed Warnickecb9cada2015-12-08 15:45:58 -07002323 vnet_main.local_interface_sw_if_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002324
Neale Ranns32e1c012016-11-22 17:07:28 +00002325 vnet_buffer (b0)->ip.adj_index[VLIB_TX] = radv_info->mcast_adj_index;
Neale Rannsf06aea52016-11-29 06:51:37 -08002326 b0->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002327
Neale Ranns32e1c012016-11-22 17:07:28 +00002328 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
Dave Barachd7cb1b52016-12-09 09:52:16 -05002329
Ed Warnickecb9cada2015-12-08 15:45:58 -07002330 f = vlib_get_frame_to_node (vm, node->index);
2331 to_next = vlib_frame_vector_args (f);
2332 to_next[0] = bo0;
2333 f->n_vectors = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002334
Ed Warnickecb9cada2015-12-08 15:45:58 -07002335 vlib_put_frame_to_node (vm, node->index, f);
2336 return;
2337}
2338
Dave Barachd7cb1b52016-12-09 09:52:16 -05002339/* *INDENT-OFF* */
2340VLIB_REGISTER_NODE (ip6_icmp_router_solicitation_node,static) =
2341{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002342 .function = icmp6_router_solicitation,
2343 .name = "icmp6-router-solicitation",
2344
2345 .vector_size = sizeof (u32),
2346
2347 .format_trace = format_icmp6_input_trace,
2348
2349 .n_next_nodes = ICMP6_ROUTER_SOLICITATION_N_NEXT,
2350 .next_nodes = {
2351 [ICMP6_ROUTER_SOLICITATION_NEXT_DROP] = "error-drop",
Neale Ranns32e1c012016-11-22 17:07:28 +00002352 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_RW] = "ip6-rewrite-mcast",
Ed Warnickecb9cada2015-12-08 15:45:58 -07002353 [ICMP6_ROUTER_SOLICITATION_NEXT_REPLY_TX] = "interface-output",
2354 },
2355};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002356/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002357
2358/* send a RA or update the timer info etc.. */
2359static uword
2360ip6_neighbor_process_timer_event (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002361 vlib_node_runtime_t * node,
2362 vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002363{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002364 vnet_main_t *vnm = vnet_get_main ();
2365 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2366 ip6_radv_t *radv_info;
2367 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002368 u32 n_this_frame = 0;
Benoît Ganned5304452016-04-08 22:25:05 -07002369 u32 n_left_to_next = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002370 u32 *to_next = 0;
2371 u32 bo0;
2372 icmp6_router_solicitation_header_t *h0;
2373 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002374 f64 now = vlib_time_now (vm);
2375
2376 /* Interface ip6 radv info list */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002377 /* *INDENT-OFF* */
2378 pool_foreach (radv_info, nm->if_radv_pool,
2379 ({
2380 if( !vnet_sw_interface_is_admin_up (vnm, radv_info->sw_if_index))
2381 {
2382 radv_info->initial_adverts_sent = radv_info->initial_adverts_count-1;
2383 radv_info->next_multicast_time = now;
2384 radv_info->last_multicast_time = now;
2385 radv_info->last_radv_time = 0;
2386 radv_info->all_routers_mcast = 0;
2387 continue;
2388 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002389
Dave Barachd7cb1b52016-12-09 09:52:16 -05002390 /* Make sure that we've joined the all-routers multicast group */
2391 if(!radv_info->all_routers_mcast)
2392 {
2393 /* send MDLP_REPORT_EVENT message */
2394 ip6_neighbor_send_mldpv2_report(radv_info->sw_if_index);
2395 radv_info->all_routers_mcast = 1;
2396 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002397
Dave Barachd7cb1b52016-12-09 09:52:16 -05002398 /* is it time to send a multicast RA on this interface? */
2399 if(radv_info->send_radv && (now >= radv_info->next_multicast_time))
2400 {
2401 u32 n_to_alloc = 1;
2402 u32 n_allocated;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002403
Dave Barachd7cb1b52016-12-09 09:52:16 -05002404 f64 rfn = (radv_info->max_radv_interval - radv_info->min_radv_interval) *
2405 random_f64 (&radv_info->seed) + radv_info->min_radv_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002406
Dave Barachd7cb1b52016-12-09 09:52:16 -05002407 /* multicast send - compute next multicast send time */
2408 if( radv_info->initial_adverts_sent > 0)
2409 {
2410 radv_info->initial_adverts_sent--;
2411 if(rfn > radv_info-> initial_adverts_interval)
2412 rfn = radv_info-> initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002413
Dave Barachd7cb1b52016-12-09 09:52:16 -05002414 /* check to see if we are ceasing to send */
2415 if( radv_info->initial_adverts_sent == 0)
2416 if(radv_info->cease_radv)
2417 radv_info->send_radv = 0;
2418 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002419
Dave Barachd7cb1b52016-12-09 09:52:16 -05002420 radv_info->next_multicast_time = rfn + now;
2421 radv_info->last_multicast_time = now;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002422
Dave Barachd7cb1b52016-12-09 09:52:16 -05002423 /* send advert now - build a "solicted" router advert with unspecified source address */
2424 n_allocated = vlib_buffer_alloc_from_free_list
2425 (vm, &bo0, n_to_alloc, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002426
Dave Barachd7cb1b52016-12-09 09:52:16 -05002427 if (PREDICT_FALSE(n_allocated == 0))
2428 {
2429 clib_warning ("buffer allocation failure");
2430 continue;
2431 }
2432 b0 = vlib_get_buffer (vm, bo0);
2433 b0->current_length = sizeof( icmp6_router_solicitation_header_t);
2434 b0->error = ICMP6_ERROR_NONE;
2435 vnet_buffer (b0)->sw_if_index[VLIB_RX] = radv_info->sw_if_index;
2436
2437 h0 = vlib_buffer_get_current (b0);
2438
2439 memset (h0, 0, sizeof (icmp6_router_solicitation_header_t));
2440
2441 h0->ip.ip_version_traffic_class_and_flow_label = clib_host_to_net_u32 (0x6 << 28);
2442 h0->ip.payload_length = clib_host_to_net_u16 (sizeof (icmp6_router_solicitation_header_t)
2443 - STRUCT_OFFSET_OF (icmp6_router_solicitation_header_t, neighbor));
2444 h0->ip.protocol = IP_PROTOCOL_ICMP6;
2445 h0->ip.hop_limit = 255;
2446
2447 /* set src/dst address as "unspecified" this marks this packet as internally generated rather than recieved */
2448 h0->ip.src_address.as_u64[0] = 0;
2449 h0->ip.src_address.as_u64[1] = 0;
2450
2451 h0->ip.dst_address.as_u64[0] = 0;
2452 h0->ip.dst_address.as_u64[1] = 0;
2453
2454 h0->neighbor.icmp.type = ICMP6_router_solicitation;
2455
2456 if (PREDICT_FALSE(f == 0))
2457 {
2458 f = vlib_get_frame_to_node (vm, ip6_icmp_router_solicitation_node.index);
2459 to_next = vlib_frame_vector_args (f);
2460 n_left_to_next = VLIB_FRAME_SIZE;
2461 n_this_frame = 0;
2462 }
2463
2464 n_this_frame++;
2465 n_left_to_next--;
2466 to_next[0] = bo0;
2467 to_next += 1;
2468
2469 if (PREDICT_FALSE(n_left_to_next == 0))
2470 {
2471 f->n_vectors = n_this_frame;
2472 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2473 f = 0;
2474 }
2475 }
2476 }));
2477 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002478
2479 if (f)
2480 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002481 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002482 f->n_vectors = n_this_frame;
2483 vlib_put_frame_to_node (vm, ip6_icmp_router_solicitation_node.index, f);
2484 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002485 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002486}
2487
2488static uword
2489ip6_icmp_neighbor_discovery_event_process (vlib_main_t * vm,
2490 vlib_node_runtime_t * node,
2491 vlib_frame_t * frame)
2492{
2493 uword event_type;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002494 ip6_icmp_neighbor_discovery_event_data_t *event_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002495
2496 /* init code here */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002497
Ed Warnickecb9cada2015-12-08 15:45:58 -07002498 while (1)
2499 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002500 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07002501
Dave Barachd7cb1b52016-12-09 09:52:16 -05002502 event_data = vlib_process_get_event_data (vm, &event_type);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002503
Dave Barachd7cb1b52016-12-09 09:52:16 -05002504 if (!event_data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002505 {
2506 /* No events found: timer expired. */
2507 /* process interface list and send RAs as appropriate, update timer info */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002508 ip6_neighbor_process_timer_event (vm, node, frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002509 }
2510 else
2511 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002512 switch (event_type)
2513 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002514
Dave Barachd7cb1b52016-12-09 09:52:16 -05002515 case ICMP6_ND_EVENT_INIT:
2516 break;
2517
2518 case ~0:
2519 break;
2520
2521 default:
2522 ASSERT (0);
2523 }
2524
Ed Warnickecb9cada2015-12-08 15:45:58 -07002525 if (event_data)
2526 _vec_len (event_data) = 0;
2527 }
2528 }
2529 return frame->n_vectors;
2530}
2531
Dave Barachd7cb1b52016-12-09 09:52:16 -05002532/* *INDENT-OFF* */
2533VLIB_REGISTER_NODE (ip6_icmp_router_advertisement_node,static) =
2534{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002535 .function = icmp6_router_advertisement,
2536 .name = "icmp6-router-advertisement",
2537
2538 .vector_size = sizeof (u32),
2539
2540 .format_trace = format_icmp6_input_trace,
2541
2542 .n_next_nodes = 1,
2543 .next_nodes = {
2544 [0] = "error-drop",
2545 },
2546};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002547/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002548
2549vlib_node_registration_t ip6_icmp_neighbor_discovery_event_node = {
2550
2551 .function = ip6_icmp_neighbor_discovery_event_process,
2552 .name = "ip6-icmp-neighbor-discovery-event-process",
2553 .type = VLIB_NODE_TYPE_PROCESS,
2554};
2555
2556static uword
2557icmp6_neighbor_solicitation (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05002558 vlib_node_runtime_t * node, vlib_frame_t * frame)
2559{
2560 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2561 /* is_solicitation */
2562 1);
2563}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002564
2565static uword
2566icmp6_neighbor_advertisement (vlib_main_t * vm,
2567 vlib_node_runtime_t * node,
2568 vlib_frame_t * frame)
Dave Barachd7cb1b52016-12-09 09:52:16 -05002569{
2570 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
2571 /* is_solicitation */
2572 0);
2573}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002574
Dave Barachd7cb1b52016-12-09 09:52:16 -05002575/* *INDENT-OFF* */
2576VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
2577{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002578 .function = icmp6_neighbor_solicitation,
2579 .name = "icmp6-neighbor-solicitation",
2580
2581 .vector_size = sizeof (u32),
2582
2583 .format_trace = format_icmp6_input_trace,
2584
2585 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
2586 .next_nodes = {
2587 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "error-drop",
2588 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
2589 },
2590};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002591/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002592
Dave Barachd7cb1b52016-12-09 09:52:16 -05002593/* *INDENT-OFF* */
2594VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
2595{
Ed Warnickecb9cada2015-12-08 15:45:58 -07002596 .function = icmp6_neighbor_advertisement,
2597 .name = "icmp6-neighbor-advertisement",
2598
2599 .vector_size = sizeof (u32),
2600
2601 .format_trace = format_icmp6_input_trace,
2602
2603 .n_next_nodes = 1,
2604 .next_nodes = {
2605 [0] = "error-drop",
2606 },
2607};
Dave Barachd7cb1b52016-12-09 09:52:16 -05002608/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002609
Dave Barachd7cb1b52016-12-09 09:52:16 -05002610/* API support functions */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002611int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002612ip6_neighbor_ra_config (vlib_main_t * vm, u32 sw_if_index,
2613 u8 suppress, u8 managed, u8 other,
2614 u8 ll_option, u8 send_unicast, u8 cease,
2615 u8 use_lifetime, u32 lifetime,
2616 u32 initial_count, u32 initial_interval,
2617 u32 max_interval, u32 min_interval, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002618{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002619 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2620 int error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002621 u32 ri;
2622
2623 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002624 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2625 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002626 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002627 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002628
Dave Barachd7cb1b52016-12-09 09:52:16 -05002629 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002630 {
2631
Dave Barachd7cb1b52016-12-09 09:52:16 -05002632 ip6_radv_t *radv_info;
2633 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002634
Dave Barachd7cb1b52016-12-09 09:52:16 -05002635 if ((max_interval != 0) && (min_interval == 0))
2636 min_interval = .75 * max_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002637
Dave Barachd7cb1b52016-12-09 09:52:16 -05002638 max_interval =
2639 (max_interval !=
2640 0) ? ((is_no) ? DEF_MAX_RADV_INTERVAL : max_interval) :
2641 radv_info->max_radv_interval;
2642 min_interval =
2643 (min_interval !=
2644 0) ? ((is_no) ? DEF_MIN_RADV_INTERVAL : min_interval) :
2645 radv_info->min_radv_interval;
2646 lifetime =
2647 (use_lifetime !=
2648 0) ? ((is_no) ? DEF_DEF_RTR_LIFETIME : lifetime) :
2649 radv_info->adv_router_lifetime_in_sec;
2650
2651 if (lifetime)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002652 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002653 if (lifetime > MAX_DEF_RTR_LIFETIME)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002654 lifetime = MAX_DEF_RTR_LIFETIME;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002655
2656 if (lifetime <= max_interval)
2657 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002658 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002659
2660 if (min_interval != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002661 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002662 if ((min_interval > .75 * max_interval) || (min_interval < 3))
2663 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002664 }
2665
Dave Barachd7cb1b52016-12-09 09:52:16 -05002666 if ((initial_count > MAX_INITIAL_RTR_ADVERTISEMENTS) ||
2667 (initial_interval > MAX_INITIAL_RTR_ADVERT_INTERVAL))
2668 return VNET_API_ERROR_INVALID_VALUE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002669
Dave Barachd7cb1b52016-12-09 09:52:16 -05002670 /*
2671 if "flag" is set and is_no is true then restore default value else set value corresponding to "flag"
2672 if "flag" is clear don't change corresponding value
2673 */
2674 radv_info->send_radv =
2675 (suppress != 0) ? ((is_no != 0) ? 1 : 0) : radv_info->send_radv;
2676 radv_info->adv_managed_flag =
2677 (managed != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_managed_flag;
2678 radv_info->adv_other_flag =
2679 (other != 0) ? ((is_no) ? 0 : 1) : radv_info->adv_other_flag;
2680 radv_info->adv_link_layer_address =
2681 (ll_option !=
2682 0) ? ((is_no) ? 1 : 0) : radv_info->adv_link_layer_address;
2683 radv_info->send_unicast =
2684 (send_unicast != 0) ? ((is_no) ? 0 : 1) : radv_info->send_unicast;
2685 radv_info->cease_radv =
2686 (cease != 0) ? ((is_no) ? 0 : 1) : radv_info->cease_radv;
2687
2688 radv_info->min_radv_interval = min_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002689 radv_info->max_radv_interval = max_interval;
2690 radv_info->adv_router_lifetime_in_sec = lifetime;
2691
Dave Barachd7cb1b52016-12-09 09:52:16 -05002692 radv_info->initial_adverts_count =
2693 (initial_count !=
2694 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERTISEMENTS : initial_count) :
2695 radv_info->initial_adverts_count;
2696 radv_info->initial_adverts_interval =
2697 (initial_interval !=
2698 0) ? ((is_no) ? MAX_INITIAL_RTR_ADVERT_INTERVAL : initial_interval) :
2699 radv_info->initial_adverts_interval;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002700
2701 /* restart */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002702 if ((cease != 0) && (is_no))
2703 radv_info->send_radv = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002704
Dave Barachd7cb1b52016-12-09 09:52:16 -05002705 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2706 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002707 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002708 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002709 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002710 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002711}
2712
2713int
Dave Barachd7cb1b52016-12-09 09:52:16 -05002714ip6_neighbor_ra_prefix (vlib_main_t * vm, u32 sw_if_index,
2715 ip6_address_t * prefix_addr, u8 prefix_len,
2716 u8 use_default, u32 val_lifetime, u32 pref_lifetime,
2717 u8 no_advertise, u8 off_link, u8 no_autoconfig,
2718 u8 no_onlink, u8 is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002719{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002720 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002721 int error;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002722
Ed Warnickecb9cada2015-12-08 15:45:58 -07002723 u32 ri;
2724
2725 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002726 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
2727 ~0);
2728
Ed Warnickecb9cada2015-12-08 15:45:58 -07002729 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
2730
2731 error = (ri != ~0) ? 0 : VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002732
2733 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002734 {
2735 f64 now = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002736 ip6_radv_t *radv_info;
2737 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002738
2739 /* prefix info add, delete or update */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002740 ip6_radv_prefix_t *prefix;
2741
Ed Warnickecb9cada2015-12-08 15:45:58 -07002742 /* lookup prefix info for this address on this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002743 uword *p = mhash_get (&radv_info->address_to_prefix_index, prefix_addr);
2744
Ed Warnickecb9cada2015-12-08 15:45:58 -07002745 prefix = p ? pool_elt_at_index (radv_info->adv_prefixes_pool, p[0]) : 0;
2746
Dave Barachd7cb1b52016-12-09 09:52:16 -05002747 if (is_no)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002748 {
2749 /* delete */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002750 if (!prefix)
2751 return VNET_API_ERROR_INVALID_VALUE; /* invalid prefix */
2752
2753 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002754 return VNET_API_ERROR_INVALID_VALUE_2;
2755
Dave Barachd7cb1b52016-12-09 09:52:16 -05002756 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002757 /* do specific delete processing here before returning */
2758 /* try to remove from routing table */
2759
Dave Barachd7cb1b52016-12-09 09:52:16 -05002760 mhash_unset (&radv_info->address_to_prefix_index, prefix_addr,
2761 /* old_value */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002762 pool_put (radv_info->adv_prefixes_pool, prefix);
2763
Dave Barachd7cb1b52016-12-09 09:52:16 -05002764 radv_info->initial_adverts_sent =
2765 radv_info->initial_adverts_count - 1;
2766 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002767 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002768 radv_info->last_radv_time = 0;
2769 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002770 }
2771
2772 /* adding or changing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002773 if (!prefix)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002774 {
2775 /* add */
2776 u32 pi;
2777 pool_get (radv_info->adv_prefixes_pool, prefix);
2778 pi = prefix - radv_info->adv_prefixes_pool;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002779 mhash_set (&radv_info->address_to_prefix_index, prefix_addr, pi,
2780 /* old_value */ 0);
2781
2782 memset (prefix, 0x0, sizeof (ip6_radv_prefix_t));
2783
Ed Warnickecb9cada2015-12-08 15:45:58 -07002784 prefix->prefix_len = prefix_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002785 clib_memcpy (&prefix->prefix, prefix_addr, sizeof (ip6_address_t));
2786
Ed Warnickecb9cada2015-12-08 15:45:58 -07002787 /* initialize default values */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002788 prefix->adv_on_link_flag = 1; /* L bit set */
2789 prefix->adv_autonomous_flag = 1; /* A bit set */
2790 prefix->adv_valid_lifetime_in_secs = DEF_ADV_VALID_LIFETIME;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002791 prefix->adv_pref_lifetime_in_secs = DEF_ADV_PREF_LIFETIME;
2792 prefix->enabled = 1;
2793 prefix->decrement_lifetime_flag = 1;
2794 prefix->deprecated_prefix_flag = 1;
2795
Dave Barachd7cb1b52016-12-09 09:52:16 -05002796 if (off_link == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002797 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002798 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002799 /* insert prefix into routing table as a connected prefix */
2800 }
2801
Dave Barachd7cb1b52016-12-09 09:52:16 -05002802 if (use_default)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002803 goto restart;
2804 }
2805 else
2806 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002807
2808 if (prefix->prefix_len != prefix_len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002809 return VNET_API_ERROR_INVALID_VALUE_2;
2810
Dave Barachd7cb1b52016-12-09 09:52:16 -05002811 if (off_link != 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002813 /* FIXME - Should the DP do this or the CP ? */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002814 /* remove from routing table if already there */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002815 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002816 }
2817
Dave Barachd7cb1b52016-12-09 09:52:16 -05002818 if ((val_lifetime == ~0) || (pref_lifetime == ~0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002819 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002820 prefix->adv_valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002821 prefix->adv_pref_lifetime_in_secs = ~0;
2822 prefix->decrement_lifetime_flag = 0;
2823 }
2824 else
2825 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002826 prefix->adv_valid_lifetime_in_secs = val_lifetime;;
2827 prefix->adv_pref_lifetime_in_secs = pref_lifetime;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002828 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002829
Ed Warnickecb9cada2015-12-08 15:45:58 -07002830 /* copy remaining */
2831 prefix->enabled = !(no_advertise != 0);
2832 prefix->adv_on_link_flag = !((off_link != 0) || (no_onlink != 0));
2833 prefix->adv_autonomous_flag = !(no_autoconfig != 0);
2834
Dave Barachd7cb1b52016-12-09 09:52:16 -05002835 restart:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002836 /* restart */
2837 /* fill in the expiration times */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002838 prefix->valid_lifetime_expires =
2839 now + prefix->adv_valid_lifetime_in_secs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002840 prefix->pref_lifetime_expires = now + prefix->adv_pref_lifetime_in_secs;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002841
2842 radv_info->initial_adverts_sent = radv_info->initial_adverts_count - 1;
2843 radv_info->next_multicast_time = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002844 radv_info->last_multicast_time = vlib_time_now (vm);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002845 radv_info->last_radv_time = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002846 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002847 return (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002848}
2849
2850clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05002851ip6_neighbor_cmd (vlib_main_t * vm, unformat_input_t * main_input,
2852 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002853{
Dave Barachd7cb1b52016-12-09 09:52:16 -05002854 vnet_main_t *vnm = vnet_get_main ();
2855 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
2856 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002857 u8 is_no = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002858 u8 suppress = 0, managed = 0, other = 0;
2859 u8 suppress_ll_option = 0, send_unicast = 0, cease = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002860 u8 use_lifetime = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002861 u32 sw_if_index, ra_lifetime = 0, ra_initial_count =
2862 0, ra_initial_interval = 0;
2863 u32 ra_max_interval = 0, ra_min_interval = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002864
Dave Barachd7cb1b52016-12-09 09:52:16 -05002865 unformat_input_t _line_input, *line_input = &_line_input;
2866 vnet_sw_interface_t *sw_if0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002867
2868 int add_radv_info = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002869 __attribute__ ((unused)) ip6_radv_t *radv_info = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002870 ip6_address_t ip6_addr;
2871 u32 addr_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002872
Ed Warnickecb9cada2015-12-08 15:45:58 -07002873
2874 /* Get a line of input. */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002875 if (!unformat_user (main_input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876 return 0;
2877
2878 /* get basic radv info for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002879 if (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002880 {
2881
Dave Barachd7cb1b52016-12-09 09:52:16 -05002882 if (unformat_user (line_input,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002883 unformat_vnet_sw_interface, vnm, &sw_if_index))
2884 {
2885 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002886 ethernet_interface_t *eth_if0 = 0;
2887
Ed Warnickecb9cada2015-12-08 15:45:58 -07002888 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05002889 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
2890 eth_if0 =
2891 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
2892
2893 if (!eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002894 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002895 error =
2896 clib_error_return (0, "Interface must be of ethernet type");
Ed Warnickecb9cada2015-12-08 15:45:58 -07002897 goto done;
2898 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002899
Ed Warnickecb9cada2015-12-08 15:45:58 -07002900 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05002901 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
2902 sw_if_index, ~0);
2903
Ed Warnickecb9cada2015-12-08 15:45:58 -07002904 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05002905
2906 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002907 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002908 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002909 }
2910 else
2911 {
2912 error = clib_error_return (0, "unknown interface %U'",
2913 format_unformat_error, line_input);
2914 goto done;
2915 }
2916 }
2917 else
2918 {
2919 error = clib_error_return (0, "invalid interface name %U'",
2920 format_unformat_error, line_input);
2921 goto done;
2922 }
2923 }
2924
2925 /* get the rest of the command */
2926 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2927 {
2928 if (unformat (line_input, "no"))
Dave Barachd7cb1b52016-12-09 09:52:16 -05002929 is_no = 1;
2930 else if (unformat (line_input, "prefix %U/%d",
2931 unformat_ip6_address, &ip6_addr, &addr_len))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002932 {
2933 add_radv_info = 0;
2934 break;
2935 }
2936 else if (unformat (line_input, "ra-managed-config-flag"))
2937 {
2938 managed = 1;
2939 break;
2940 }
2941 else if (unformat (line_input, "ra-other-config-flag"))
2942 {
2943 other = 1;
2944 break;
2945 }
Chris Luke33879c92016-06-28 19:54:21 -04002946 else if (unformat (line_input, "ra-suppress") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05002947 unformat (line_input, "ra-surpress"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002948 {
Chris Luke33879c92016-06-28 19:54:21 -04002949 suppress = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002950 break;
2951 }
Chris Luke33879c92016-06-28 19:54:21 -04002952 else if (unformat (line_input, "ra-suppress-link-layer") ||
Dave Barachd7cb1b52016-12-09 09:52:16 -05002953 unformat (line_input, "ra-surpress-link-layer"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002954 {
Chris Luke33879c92016-06-28 19:54:21 -04002955 suppress_ll_option = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002956 break;
2957 }
2958 else if (unformat (line_input, "ra-send-unicast"))
2959 {
2960 send_unicast = 1;
2961 break;
2962 }
2963 else if (unformat (line_input, "ra-lifetime"))
2964 {
2965 if (!unformat (line_input, "%d", &ra_lifetime))
Billy McFalla9a20e72017-02-15 11:39:12 -05002966 {
2967 error = unformat_parse_error (line_input);
2968 goto done;
2969 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002970 use_lifetime = 1;
2971 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -05002972 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002973 else if (unformat (line_input, "ra-initial"))
2974 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05002975 if (!unformat
2976 (line_input, "%d %d", &ra_initial_count, &ra_initial_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05002977 {
2978 error = unformat_parse_error (line_input);
2979 goto done;
2980 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002981 break;
2982 }
2983 else if (unformat (line_input, "ra-interval"))
2984 {
2985 if (!unformat (line_input, "%d", &ra_max_interval))
Billy McFalla9a20e72017-02-15 11:39:12 -05002986 {
2987 error = unformat_parse_error (line_input);
2988 goto done;
2989 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002990
2991 if (!unformat (line_input, "%d", &ra_min_interval))
2992 ra_min_interval = 0;
2993 break;
2994 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05002995 else if (unformat (line_input, "ra-cease"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002996 {
2997 cease = 1;
2998 break;
2999 }
3000 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003001 {
3002 error = unformat_parse_error (line_input);
3003 goto done;
3004 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003005 }
3006
Dave Barachd7cb1b52016-12-09 09:52:16 -05003007 if (add_radv_info)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003008 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003009 ip6_neighbor_ra_config (vm, sw_if_index,
3010 suppress, managed, other,
3011 suppress_ll_option, send_unicast, cease,
3012 use_lifetime, ra_lifetime,
3013 ra_initial_count, ra_initial_interval,
3014 ra_max_interval, ra_min_interval, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003015 }
3016 else
3017 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003018 u32 valid_lifetime_in_secs = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003019 u32 pref_lifetime_in_secs = 0;
3020 u8 use_prefix_default_values = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003021 u8 no_advertise = 0;
3022 u8 off_link = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003023 u8 no_autoconfig = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003024 u8 no_onlink = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003025
3026 /* get the rest of the command */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003027 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003028 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003029 if (unformat (line_input, "default"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003030 {
3031 use_prefix_default_values = 1;
3032 break;
3033 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003034 else if (unformat (line_input, "infinite"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003035 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003036 valid_lifetime_in_secs = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003037 pref_lifetime_in_secs = ~0;
3038 break;
3039 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003040 else if (unformat (line_input, "%d %d", &valid_lifetime_in_secs,
3041 &pref_lifetime_in_secs))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003042 break;
3043 else
3044 break;
3045 }
3046
3047
3048 /* get the rest of the command */
3049 while (!use_prefix_default_values &&
3050 unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3051 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003052 if (unformat (line_input, "no-advertise"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003053 no_advertise = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003054 else if (unformat (line_input, "off-link"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003055 off_link = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003056 else if (unformat (line_input, "no-autoconfig"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003057 no_autoconfig = 1;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003058 else if (unformat (line_input, "no-onlink"))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003059 no_onlink = 1;
3060 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003061 {
3062 error = unformat_parse_error (line_input);
3063 goto done;
3064 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003065 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003066
3067 ip6_neighbor_ra_prefix (vm, sw_if_index,
3068 &ip6_addr, addr_len,
3069 use_prefix_default_values,
3070 valid_lifetime_in_secs,
3071 pref_lifetime_in_secs,
3072 no_advertise,
3073 off_link, no_autoconfig, no_onlink, is_no);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003074 }
3075
Billy McFalla9a20e72017-02-15 11:39:12 -05003076done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003077 unformat_free (line_input);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003078
Ed Warnickecb9cada2015-12-08 15:45:58 -07003079 return error;
3080}
3081
Chris Lukebfd32fd2016-06-24 20:38:06 -04003082static void
Dave Barachd7cb1b52016-12-09 09:52:16 -05003083ip6_print_addrs (vlib_main_t * vm, u32 * addrs)
Chris Lukebfd32fd2016-06-24 20:38:06 -04003084{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003085 ip_lookup_main_t *lm = &ip6_main.lookup_main;
Chris Lukebfd32fd2016-06-24 20:38:06 -04003086 u32 i;
3087
3088 for (i = 0; i < vec_len (addrs); i++)
3089 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003090 ip_interface_address_t *a =
3091 pool_elt_at_index (lm->if_address_pool, addrs[i]);
3092 ip6_address_t *address = ip_interface_address_get_address (lm, a);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003093
3094 vlib_cli_output (vm, "\t\t%U/%d",
Dave Barachd7cb1b52016-12-09 09:52:16 -05003095 format_ip6_address, address, a->address_length);
Chris Lukebfd32fd2016-06-24 20:38:06 -04003096 }
3097}
3098
Ed Warnickecb9cada2015-12-08 15:45:58 -07003099static clib_error_t *
3100show_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003101 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003102{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003103 vnet_main_t *vnm = vnet_get_main ();
3104 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3105 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003106 u32 sw_if_index;
3107
3108 sw_if_index = ~0;
3109
Dave Barachd7cb1b52016-12-09 09:52:16 -05003110 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003111 {
3112 u32 ri;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003113
Dave Barachd7cb1b52016-12-09 09:52:16 -05003114 /* look up the radv_t information for this interface */
3115 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3116 sw_if_index, ~0);
3117
3118 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3119
3120 if (ri != ~0)
3121 {
3122 ip_lookup_main_t *lm = &ip6_main.lookup_main;
3123 ip6_radv_t *radv_info;
3124 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3125
3126 vlib_cli_output (vm, "%U is admin %s\n",
3127 format_vnet_sw_interface_name, vnm,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003128 vnet_get_sw_interface (vnm, sw_if_index),
Dave Barachd7cb1b52016-12-09 09:52:16 -05003129 (vnet_sw_interface_is_admin_up (vnm, sw_if_index) ?
3130 "up" : "down"));
3131
Chris Lukebfd32fd2016-06-24 20:38:06 -04003132 u32 ai;
3133 u32 *link_scope = 0, *global_scope = 0;
3134 u32 *local_scope = 0, *unknown_scope = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003135 ip_interface_address_t *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003136
Dave Barachd7cb1b52016-12-09 09:52:16 -05003137 vec_validate_init_empty (lm->if_address_pool_index_by_sw_if_index,
3138 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003139 ai = lm->if_address_pool_index_by_sw_if_index[sw_if_index];
3140
Dave Barachd7cb1b52016-12-09 09:52:16 -05003141 while (ai != (u32) ~ 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003142 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003143 a = pool_elt_at_index (lm->if_address_pool, ai);
3144 ip6_address_t *address =
3145 ip_interface_address_get_address (lm, a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003146
Chris Lukebfd32fd2016-06-24 20:38:06 -04003147 if (ip6_address_is_link_local_unicast (address))
3148 vec_add1 (link_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003149 else if (ip6_address_is_global_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003150 vec_add1 (global_scope, ai);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003151 else if (ip6_address_is_local_unicast (address))
Chris Lukebfd32fd2016-06-24 20:38:06 -04003152 vec_add1 (local_scope, ai);
3153 else
3154 vec_add1 (unknown_scope, ai);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003155
3156 ai = a->next_this_sw_interface;
3157 }
3158
Dave Barachd7cb1b52016-12-09 09:52:16 -05003159 if (vec_len (link_scope))
3160 {
3161 vlib_cli_output (vm, "\tLink-local address(es):\n");
3162 ip6_print_addrs (vm, link_scope);
3163 vec_free (link_scope);
3164 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003165
Dave Barachd7cb1b52016-12-09 09:52:16 -05003166 if (vec_len (local_scope))
3167 {
3168 vlib_cli_output (vm, "\tLocal unicast address(es):\n");
3169 ip6_print_addrs (vm, local_scope);
3170 vec_free (local_scope);
3171 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003172
Dave Barachd7cb1b52016-12-09 09:52:16 -05003173 if (vec_len (global_scope))
3174 {
3175 vlib_cli_output (vm, "\tGlobal unicast address(es):\n");
3176 ip6_print_addrs (vm, global_scope);
3177 vec_free (global_scope);
3178 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003179
Dave Barachd7cb1b52016-12-09 09:52:16 -05003180 if (vec_len (unknown_scope))
3181 {
3182 vlib_cli_output (vm, "\tOther-scope address(es):\n");
3183 ip6_print_addrs (vm, unknown_scope);
3184 vec_free (unknown_scope);
3185 }
Chris Lukebfd32fd2016-06-24 20:38:06 -04003186
Ed Warnickecb9cada2015-12-08 15:45:58 -07003187 vlib_cli_output (vm, "\tJoined group address(es):\n");
3188 ip6_mldp_group_t *m;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003189 /* *INDENT-OFF* */
3190 pool_foreach (m, radv_info->mldp_group_pool,
3191 ({
3192 vlib_cli_output (vm, "\t\t%U\n", format_ip6_address,
3193 &m->mcast_address);
3194 }));
3195 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003196
3197 vlib_cli_output (vm, "\tAdvertised Prefixes:\n");
Dave Barachd7cb1b52016-12-09 09:52:16 -05003198 ip6_radv_prefix_t *p;
3199 /* *INDENT-OFF* */
3200 pool_foreach (p, radv_info->adv_prefixes_pool,
3201 ({
3202 vlib_cli_output (vm, "\t\tprefix %U, length %d\n",
3203 format_ip6_address, &p->prefix, p->prefix_len);
3204 }));
3205 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003206
Dave Barachd7cb1b52016-12-09 09:52:16 -05003207 vlib_cli_output (vm, "\tMTU is %d\n", radv_info->adv_link_mtu);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003208 vlib_cli_output (vm, "\tICMP error messages are unlimited\n");
3209 vlib_cli_output (vm, "\tICMP redirects are disabled\n");
3210 vlib_cli_output (vm, "\tICMP unreachables are not sent\n");
3211 vlib_cli_output (vm, "\tND DAD is disabled\n");
3212 //vlib_cli_output (vm, "\tND reachable time is %d milliseconds\n",);
3213 vlib_cli_output (vm, "\tND advertised reachable time is %d\n",
3214 radv_info->adv_neighbor_reachable_time_in_msec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003215 vlib_cli_output (vm,
3216 "\tND advertised retransmit interval is %d (msec)\n",
3217 radv_info->
3218 adv_time_in_msec_between_retransmitted_neighbor_solicitations);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003219
3220 u32 ra_interval = radv_info->max_radv_interval;
3221 u32 ra_interval_min = radv_info->min_radv_interval;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003222 vlib_cli_output (vm,
3223 "\tND router advertisements are sent every %d seconds (min interval is %d)\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003224 ra_interval, ra_interval_min);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003225 vlib_cli_output (vm,
3226 "\tND router advertisements live for %d seconds\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003227 radv_info->adv_router_lifetime_in_sec);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003228 vlib_cli_output (vm,
3229 "\tHosts %s stateless autoconfig for addresses\n",
3230 (radv_info->adv_managed_flag) ? "use" :
3231 " don't use");
3232 vlib_cli_output (vm, "\tND router advertisements sent %d\n",
3233 radv_info->n_advertisements_sent);
3234 vlib_cli_output (vm, "\tND router solicitations received %d\n",
3235 radv_info->n_solicitations_rcvd);
3236 vlib_cli_output (vm, "\tND router solicitations dropped %d\n",
3237 radv_info->n_solicitations_dropped);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003238 }
3239 else
3240 {
Chris Lukebfd32fd2016-06-24 20:38:06 -04003241 error = clib_error_return (0, "IPv6 not enabled on interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003242 format_unformat_error, input);
3243
3244 }
3245 }
3246 return error;
3247}
3248
Billy McFall0683c9c2016-10-13 08:27:31 -04003249/*?
3250 * This command is used to display various IPv6 attributes on a given
3251 * interface.
3252 *
3253 * @cliexpar
3254 * Example of how to display IPv6 settings:
3255 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3256 * GigabitEthernet2/0/0 is admin up
3257 * Link-local address(es):
3258 * fe80::ab8/64
3259 * Joined group address(es):
3260 * ff02::1
3261 * ff02::2
3262 * ff02::16
3263 * ff02::1:ff00:ab8
3264 * Advertised Prefixes:
3265 * prefix fe80::fe:28ff:fe9c:75b3, length 64
3266 * MTU is 1500
3267 * ICMP error messages are unlimited
3268 * ICMP redirects are disabled
3269 * ICMP unreachables are not sent
3270 * ND DAD is disabled
3271 * ND advertised reachable time is 0
3272 * ND advertised retransmit interval is 0 (msec)
3273 * ND router advertisements are sent every 200 seconds (min interval is 150)
3274 * ND router advertisements live for 600 seconds
3275 * Hosts use stateless autoconfig for addresses
3276 * ND router advertisements sent 19336
3277 * ND router solicitations received 0
3278 * ND router solicitations dropped 0
3279 * @cliexend
3280 * Example of output if IPv6 is not enabled on the interface:
3281 * @cliexstart{show ip6 interface GigabitEthernet2/0/0}
3282 * show ip6 interface: IPv6 not enabled on interface
3283 * @cliexend
3284?*/
3285/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003286VLIB_CLI_COMMAND (show_ip6_interface_command, static) =
3287{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003288 .path = "show ip6 interface",
3289 .function = show_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003290 .short_help = "show ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003291};
Billy McFall0683c9c2016-10-13 08:27:31 -04003292/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003293
3294clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003295disable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003296{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003297 clib_error_t *error = 0;
3298 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003299 u32 ri;
3300
3301 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003302 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3303 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003304 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003305
Ed Warnickecb9cada2015-12-08 15:45:58 -07003306 /* if not created - do nothing */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003307 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003308 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003309 vnet_main_t *vnm = vnet_get_main ();
3310 ip6_radv_t *radv_info;
3311
3312 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003313
3314 /* check radv_info ref count for other ip6 addresses on this interface */
Wojciech Dec7bc73102017-02-14 16:24:28 +01003315 /* This implicitly excludes the link local address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003316 if (radv_info->ref_count == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003317 {
3318 /* essentially "disables" ipv6 on this interface */
3319 error = ip6_add_del_interface_address (vm, sw_if_index,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003320 &radv_info->
Neale Ranns75152282017-01-09 01:00:45 -08003321 link_local_address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003322 1 /* is_del */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003323
Dave Barachd7cb1b52016-12-09 09:52:16 -05003324 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3325 0 /* is_add */ );
Neale Ranns4008ac92017-02-13 23:20:04 -08003326 ip6_mfib_interface_enable_disable (sw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003327 }
3328 }
3329 return error;
3330}
3331
3332int
Dave Barachd7cb1b52016-12-09 09:52:16 -05003333ip6_interface_enabled (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003334{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003335 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3336 u32 ri = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003337
Dave Barachd7cb1b52016-12-09 09:52:16 -05003338 /* look up the radv_t information for this interface */
3339 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3340 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003341
Dave Barachd7cb1b52016-12-09 09:52:16 -05003342 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -07003343
Dave Barachd7cb1b52016-12-09 09:52:16 -05003344 return ri != ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003345}
3346
Dave Barachd7cb1b52016-12-09 09:52:16 -05003347clib_error_t *
3348enable_ip6_interface (vlib_main_t * vm, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003349{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003350 clib_error_t *error = 0;
3351 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003352 u32 ri;
3353 int is_add = 1;
3354
3355 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003356 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index, sw_if_index,
3357 ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003358
Dave Barachd7cb1b52016-12-09 09:52:16 -05003359 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
3360
3361 /* if not created yet */
3362 if (ri == ~0)
3363 {
3364 vnet_main_t *vnm = vnet_get_main ();
3365 vnet_sw_interface_t *sw_if0;
3366
3367 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index);
3368 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
3369 {
3370 ethernet_interface_t *eth_if0;
3371
3372 eth_if0 =
3373 ethernet_get_interface (&ethernet_main, sw_if0->hw_if_index);
3374 if (eth_if0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003375 {
3376 /* create radv_info. for this interface. This holds all the info needed for router adverts */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003377 ri =
3378 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index, is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003379
Dave Barachd7cb1b52016-12-09 09:52:16 -05003380 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003381 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003382 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003383 ip6_address_t link_local_address;
3384
Dave Barachd7cb1b52016-12-09 09:52:16 -05003385 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003386
Dave Barachd7cb1b52016-12-09 09:52:16 -05003387 ip6_link_local_address_from_ethernet_mac_address
3388 (&link_local_address, eth_if0->address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003389
3390 sw_if0 = vnet_get_sw_interface (vnm, sw_if_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003391 if (sw_if0->type == VNET_SW_INTERFACE_TYPE_SUB)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003392 {
3393 /* make up an interface id */
3394 md5_context_t m;
3395 u8 digest[16];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003396
Ed Warnickecb9cada2015-12-08 15:45:58 -07003397 link_local_address.as_u64[0] = radv_info->randomizer;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003398
Ed Warnickecb9cada2015-12-08 15:45:58 -07003399 md5_init (&m);
3400 md5_add (&m, &link_local_address, 16);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003401 md5_finish (&m, digest);
3402
3403 clib_memcpy (&link_local_address, digest, 16);
3404
Ed Warnickecb9cada2015-12-08 15:45:58 -07003405 radv_info->randomizer = link_local_address.as_u64[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003406
3407 link_local_address.as_u64[0] =
3408 clib_host_to_net_u64 (0xFE80000000000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003409 /* clear u bit */
3410 link_local_address.as_u8[8] &= 0xfd;
3411 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003412
Neale Ranns4008ac92017-02-13 23:20:04 -08003413 ip6_mfib_interface_enable_disable (sw_if_index, 1);
3414
Ed Warnickecb9cada2015-12-08 15:45:58 -07003415 /* essentially "enables" ipv6 on this interface */
3416 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns0bfe5d82016-08-25 15:29:12 +01003417 &link_local_address,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003418 128
3419 /* address width */ ,
3420 0 /* is_del */ );
3421
3422 if (error)
3423 ip6_neighbor_sw_interface_add_del (vnm, sw_if_index,
3424 !is_add);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003425 else
3426 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003427 radv_info->link_local_address = link_local_address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003428 }
3429 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003430 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003431 }
3432 }
3433 return error;
3434}
3435
3436static clib_error_t *
3437enable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003438 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003439{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003440 vnet_main_t *vnm = vnet_get_main ();
3441 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003442 u32 sw_if_index;
3443
3444 sw_if_index = ~0;
3445
Dave Barachd7cb1b52016-12-09 09:52:16 -05003446 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003447 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003448 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003449 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003450 else
3451 {
3452 error = clib_error_return (0, "unknown interface\n'",
3453 format_unformat_error, input);
3454
3455 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003456 return error;
3457}
3458
Billy McFall0683c9c2016-10-13 08:27:31 -04003459/*?
3460 * This command is used to enable IPv6 on a given interface.
3461 *
3462 * @cliexpar
3463 * Example of how enable IPv6 on a given interface:
3464 * @cliexcmd{enable ip6 interface GigabitEthernet2/0/0}
3465?*/
3466/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003467VLIB_CLI_COMMAND (enable_ip6_interface_command, static) =
3468{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003469 .path = "enable ip6 interface",
3470 .function = enable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003471 .short_help = "enable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003472};
Billy McFall0683c9c2016-10-13 08:27:31 -04003473/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003474
3475static clib_error_t *
3476disable_ip6_interface_cmd (vlib_main_t * vm,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003477 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003478{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003479 vnet_main_t *vnm = vnet_get_main ();
3480 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003481 u32 sw_if_index;
3482
3483 sw_if_index = ~0;
3484
Dave Barachd7cb1b52016-12-09 09:52:16 -05003485 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003486 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003487 error = disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003488 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003489 else
3490 {
3491 error = clib_error_return (0, "unknown interface\n'",
3492 format_unformat_error, input);
3493
3494 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003495 return error;
3496}
3497
Billy McFall0683c9c2016-10-13 08:27:31 -04003498/*?
3499 * This command is used to disable IPv6 on a given interface.
3500 *
3501 * @cliexpar
3502 * Example of how disable IPv6 on a given interface:
3503 * @cliexcmd{disable ip6 interface GigabitEthernet2/0/0}
3504?*/
3505/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003506VLIB_CLI_COMMAND (disable_ip6_interface_command, static) =
3507{
Billy McFall0683c9c2016-10-13 08:27:31 -04003508 .path = "disable ip6 interface",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003509 .function = disable_ip6_interface_cmd,
Billy McFall0683c9c2016-10-13 08:27:31 -04003510 .short_help = "disable ip6 interface <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003511};
Billy McFall0683c9c2016-10-13 08:27:31 -04003512/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003513
Billy McFall0683c9c2016-10-13 08:27:31 -04003514/*?
3515 * This command is used to configure the neighbor discovery
3516 * parameters on a given interface. Use the '<em>show ip6 interface</em>'
3517 * command to display some of the current neighbor discovery parameters
3518 * on a given interface. This command has three formats:
3519 *
3520 *
3521 * <b>Format 1 - Router Advertisement Options:</b> (Only one can be entered in a single command)
3522 *
3523 * '<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>'
3524 *
3525 * Where:
3526 *
3527 * <em>[no] ra-managed-config-flag</em> - Advertises in ICMPv6
3528 * router-advertisement messages to use stateful address
3529 * auto-configuration to obtain address information (sets the M-bit).
3530 * Default is the M-bit is not set and the '<em>no</em>' option
3531 * returns it to this default state.
3532 *
3533 * <em>[no] ra-other-config-flag</em> - Indicates in ICMPv6
3534 * router-advertisement messages that hosts use stateful auto
3535 * configuration to obtain nonaddress related information (sets
3536 * the O-bit). Default is the O-bit is not set and the '<em>no</em>'
3537 * option returns it to this default state.
3538 *
3539 * <em>[no] ra-suppress</em> - Disables sending ICMPv6 router-advertisement
3540 * messages. The '<em>no</em>' option implies to enable sending ICMPv6
3541 * router-advertisement messages.
3542 *
3543 * <em>[no] ra-suppress-link-layer</em> - Indicates not to include the
3544 * optional source link-layer address in the ICMPv6 router-advertisement
3545 * messages. Default is to include the optional source link-layer address
3546 * and the '<em>no</em>' option returns it to this default state.
3547 *
3548 * <em>[no] ra-send-unicast</em> - Use the source address of the
3549 * router-solicitation message if availiable. The default is to use
3550 * multicast address of all nodes, and the '<em>no</em>' option returns
3551 * it to this default state.
3552 *
3553 * <em>[no] ra-lifetime <lifetime></em> - Advertises the lifetime of a
3554 * default router in ICMPv6 router-advertisement messages. The range is
3555 * from 0 to 9000 seconds. '<em><lifetime></em>' must be greater than
3556 * '<em><max-interval></em>'. The default value is 600 seconds and the
3557 * '<em>no</em>' option returns it to this default value.
3558 *
3559 * <em>[no] ra-initial <cnt> <interval></em> - Number of initial ICMPv6
3560 * router-advertisement messages sent and the interval between each
3561 * message. Range for count is 1 - 3 and default is 3. Range for interval
3562 * is 1 to 16 seconds, and default is 16 seconds. The '<em>no</em>' option
3563 * returns both to their default value.
3564 *
3565 * <em>[no] ra-interval <max-interval> [<min-interval>]</em> - Configures the
3566 * interval between sending ICMPv6 router-advertisement messages. The
3567 * range for max-interval is from 4 to 200 seconds. min-interval can not
3568 * be more than 75% of max-interval. If not set, min-interval will be
3569 * set to 75% of max-interval. The range for min-interval is from 3 to
3570 * 150 seconds. The '<em>no</em>' option returns both to their default
3571 * value.
3572 *
3573 * <em>[no] ra-cease</em> - Cease sending ICMPv6 router-advertisement messages.
3574 * The '<em>no</em>' options implies to start (or restart) sending
3575 * ICMPv6 router-advertisement messages.
3576 *
3577 *
3578 * <b>Format 2 - Prefix Options:</b>
3579 *
3580 * '<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>'
3581 *
3582 * Where:
3583 *
3584 * <em>no</em> - All additional flags are ignored and the prefix is deleted.
3585 *
3586 * <em><valid-lifetime> <pref-lifetime></em> - '<em><valid-lifetime></em>' is the
3587 * length of time in seconds during what the prefix is valid for the purpose of
3588 * on-link determination. Range is 7203 to 2592000 seconds and default is 2592000
3589 * seconds (30 days). '<em><pref-lifetime></em>' is the prefered-lifetime and is the
3590 * length of time in seconds during what addresses generated from the prefix remain
3591 * preferred. Range is 0 to 604800 seconds and default is 604800 seconds (7 days).
3592 *
3593 * <em>infinite</em> - Both '<em><valid-lifetime></em>' and '<em><<pref-lifetime></em>'
3594 * are inifinte, no timeout.
3595 *
3596 * <em>no-advertise</em> - Do not send full router address in prefix
3597 * advertisement. Default is to advertise (i.e. - This flag is off by default).
3598 *
3599 * <em>off-link</em> - Prefix is off-link, clear L-bit in packet. Default is on-link
3600 * (i.e. - This flag is off and L-bit in packet is set by default and this prefix can
3601 * be used for on-link determination). '<em>no-onlink</em>' also controls the L-bit.
3602 *
3603 * <em>no-autoconfig</em> - Do not use prefix for autoconfiguration, clear A-bit in packet.
3604 * Default is autoconfig (i.e. - This flag is off and A-bit in packet is set by default.
3605 *
3606 * <em>no-onlink</em> - Do not use prefix for onlink determination, clear L-bit in packet.
3607 * Default is on-link (i.e. - This flag is off and L-bit in packet is set by default and
3608 * this prefix can be used for on-link determination). '<em>off-link</em>' also controls
3609 * the L-bit.
3610 *
3611 *
3612 * <b>Format 3: - Default of Prefix:</b>
3613 *
3614 * '<em><b>ip6 nd <interface> [no] prefix <ip6-address>/<width> default</b></em>'
3615 *
3616 * When a new prefix is added (or existing one is being overwritten) <em>default</em>
3617 * uses default values for the prefix. If <em>no</em> is used, the <em>default</em>
3618 * is ignored and the prefix is deleted.
3619 *
3620 *
3621 * @cliexpar
3622 * Example of how set a router advertisement option:
3623 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 ra-interval 100 20}
3624 * Example of how to add a prefix:
3625 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 prefix fe80::fe:28ff:fe9c:75b3/64 infinite no-advertise}
3626 * Example of how to delete a prefix:
3627 * @cliexcmd{ip6 nd GigabitEthernet2/0/0 no prefix fe80::fe:28ff:fe9c:75b3/64}
3628?*/
3629/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003630VLIB_CLI_COMMAND (ip6_nd_command, static) =
3631{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003632 .path = "ip6 nd",
Billy McFall0683c9c2016-10-13 08:27:31 -04003633 .short_help = "ip6 nd <interface> ...",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003634 .function = ip6_neighbor_cmd,
3635};
Billy McFall0683c9c2016-10-13 08:27:31 -04003636/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003637
3638clib_error_t *
Dave Barachd7cb1b52016-12-09 09:52:16 -05003639set_ip6_link_local_address (vlib_main_t * vm,
Neale Ranns75152282017-01-09 01:00:45 -08003640 u32 sw_if_index, ip6_address_t * address)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003641{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003642 clib_error_t *error = 0;
3643 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003644 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003645 ip6_radv_t *radv_info;
3646 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07003647
Dave Barachd7cb1b52016-12-09 09:52:16 -05003648 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003649 {
3650 vnm->api_errno = VNET_API_ERROR_ADDRESS_NOT_LINK_LOCAL;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003651 return (error = clib_error_return (0, "address not link-local",
3652 format_unformat_error));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003653 }
3654
3655 /* call enable ipv6 */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003656 enable_ip6_interface (vm, sw_if_index);
3657
Ed Warnickecb9cada2015-12-08 15:45:58 -07003658 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003659
3660 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003661 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05003662 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003663
3664 /* save if link local address (overwrite default) */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003665
Ed Warnickecb9cada2015-12-08 15:45:58 -07003666 /* delete the old one */
3667 error = ip6_add_del_interface_address (vm, sw_if_index,
3668 &radv_info->link_local_address,
Neale Ranns75152282017-01-09 01:00:45 -08003669 128, 1 /* is_del */ );
Dave Barachd7cb1b52016-12-09 09:52:16 -05003670
3671 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003672 {
3673 /* add the new one */
3674 error = ip6_add_del_interface_address (vm, sw_if_index,
Neale Ranns75152282017-01-09 01:00:45 -08003675 address, 128,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003676 0 /* is_del */ );
3677
3678 if (!error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003679 {
3680 radv_info->link_local_address = *address;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003681 }
3682 }
3683 }
3684 else
3685 {
3686 vnm->api_errno = VNET_API_ERROR_IP6_NOT_ENABLED;
3687 error = clib_error_return (0, "ip6 not enabled for interface",
3688 format_unformat_error);
3689 }
3690 return error;
3691}
Dave Barachd7cb1b52016-12-09 09:52:16 -05003692
Ed Warnickecb9cada2015-12-08 15:45:58 -07003693clib_error_t *
3694set_ip6_link_local_address_cmd (vlib_main_t * vm,
3695 unformat_input_t * input,
3696 vlib_cli_command_t * cmd)
3697{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003698 vnet_main_t *vnm = vnet_get_main ();
3699 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003700 u32 sw_if_index;
3701 ip6_address_t ip6_addr;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003702
3703 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003704 {
3705 /* get the rest of the command */
3706 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3707 {
Neale Ranns75152282017-01-09 01:00:45 -08003708 if (unformat (input, "%U", unformat_ip6_address, &ip6_addr))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003709 break;
3710 else
Dave Barachd7cb1b52016-12-09 09:52:16 -05003711 return (unformat_parse_error (input));
Ed Warnickecb9cada2015-12-08 15:45:58 -07003712 }
3713 }
Neale Ranns75152282017-01-09 01:00:45 -08003714 error = set_ip6_link_local_address (vm, sw_if_index, &ip6_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003715 return error;
3716}
3717
Billy McFall0683c9c2016-10-13 08:27:31 -04003718/*?
3719 * This command is used to assign an IPv6 Link-local address to an
3720 * interface. This command will enable IPv6 on an interface if it
3721 * is not already enabled. Use the '<em>show ip6 interface</em>' command
3722 * to display the assigned Link-local address.
3723 *
3724 * @cliexpar
3725 * Example of how to assign an IPv6 Link-local address to an interface:
Neale Ranns75152282017-01-09 01:00:45 -08003726 * @cliexcmd{set ip6 link-local address GigabitEthernet2/0/0 FE80::AB8}
Billy McFall0683c9c2016-10-13 08:27:31 -04003727?*/
3728/* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003729VLIB_CLI_COMMAND (set_ip6_link_local_address_command, static) =
3730{
Ed Warnickecb9cada2015-12-08 15:45:58 -07003731 .path = "set ip6 link-local address",
Neale Ranns75152282017-01-09 01:00:45 -08003732 .short_help = "set ip6 link-local address <interface> <ip6-address>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003733 .function = set_ip6_link_local_address_cmd,
3734};
Billy McFall0683c9c2016-10-13 08:27:31 -04003735/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003736
Neale Ranns87df12d2017-02-18 08:16:41 -08003737/**
3738 * @brief callback when an interface address is added or deleted
3739 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003740static void
3741ip6_neighbor_add_del_interface_address (ip6_main_t * im,
3742 uword opaque,
3743 u32 sw_if_index,
3744 ip6_address_t * address,
3745 u32 address_length,
Dave Barachd7cb1b52016-12-09 09:52:16 -05003746 u32 if_address_index, u32 is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003747{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003748 vnet_main_t *vnm = vnet_get_main ();
3749 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003750 u32 ri;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003751 vlib_main_t *vm = vnm->vlib_main;
3752 ip6_radv_t *radv_info;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003753 ip6_address_t a;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003754
3755 /* create solicited node multicast address for this interface adddress */
3756 ip6_set_solicited_node_multicast_address (&a, 0);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003757
Ed Warnickecb9cada2015-12-08 15:45:58 -07003758 a.as_u8[0xd] = address->as_u8[0xd];
3759 a.as_u8[0xe] = address->as_u8[0xe];
3760 a.as_u8[0xf] = address->as_u8[0xf];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003761
3762 if (!is_delete)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003763 {
3764 /* try to create radv_info - does nothing if ipv6 already enabled */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003765 enable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003766
3767 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003768 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3769 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003770 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003771 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003772 {
3773 /* get radv_info */
3774 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3775
3776 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003777 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003778 radv_info->ref_count++;
3779
Neale Ranns87df12d2017-02-18 08:16:41 -08003780 ip6_neighbor_add_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003781 }
3782 }
3783 else
3784 {
3785
3786 /* delete */
3787 /* look up the radv_t information for this interface */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003788 vec_validate_init_empty (nm->if_radv_pool_index_by_sw_if_index,
3789 sw_if_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003790 ri = nm->if_radv_pool_index_by_sw_if_index[sw_if_index];
Wojciech Dec7bc73102017-02-14 16:24:28 +01003791
Dave Barachd7cb1b52016-12-09 09:52:16 -05003792 if (ri != ~0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003793 {
3794 /* get radv_info */
3795 radv_info = pool_elt_at_index (nm->if_radv_pool, ri);
3796
Neale Ranns87df12d2017-02-18 08:16:41 -08003797 ip6_neighbor_del_mld_prefix (radv_info, &a);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003798
3799 /* if interface up send MLDP "report" */
3800 radv_info->all_routers_mcast = 0;
3801
3802 /* add address */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003803 if (!ip6_address_is_link_local_unicast (address))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003804 radv_info->ref_count--;
3805 }
Wojciech Dec7bc73102017-02-14 16:24:28 +01003806 /* Ensure that IPv6 is disabled, and LL removed after ref_count reaches 0 */
3807 disable_ip6_interface (vm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003808 }
3809}
3810
Dave Barachd7cb1b52016-12-09 09:52:16 -05003811clib_error_t *
3812ip6_set_neighbor_limit (u32 neighbor_limit)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003813{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003814 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003815
3816 nm->limit_neighbor_cache_size = neighbor_limit;
3817 return 0;
3818}
3819
Dave Barachd7cb1b52016-12-09 09:52:16 -05003820static clib_error_t *
3821ip6_neighbor_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003822{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003823 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3824 ip6_main_t *im = &ip6_main;
3825
Ed Warnickecb9cada2015-12-08 15:45:58 -07003826 mhash_init (&nm->neighbor_index_by_key,
3827 /* value size */ sizeof (uword),
3828 /* key size */ sizeof (ip6_neighbor_key_t));
3829
Dave Barachd7cb1b52016-12-09 09:52:16 -05003830 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
3831 ip6_icmp_neighbor_solicitation_node.index);
3832 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
3833 ip6_icmp_neighbor_advertisement_node.index);
3834 icmp6_register_type (vm, ICMP6_router_solicitation,
3835 ip6_icmp_router_solicitation_node.index);
3836 icmp6_register_type (vm, ICMP6_router_advertisement,
3837 ip6_icmp_router_advertisement_node.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003838
3839 /* handler node for ip6 neighbor discovery events and timers */
3840 vlib_register_node (vm, &ip6_icmp_neighbor_discovery_event_node);
3841
3842 /* add call backs */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003843 ip6_add_del_interface_address_callback_t cb;
3844 memset (&cb, 0x0, sizeof (ip6_add_del_interface_address_callback_t));
3845
Ed Warnickecb9cada2015-12-08 15:45:58 -07003846 /* when an interface address changes... */
3847 cb.function = ip6_neighbor_add_del_interface_address;
3848 cb.function_opaque = 0;
3849 vec_add1 (im->add_del_interface_address_callbacks, cb);
3850
3851 mhash_init (&nm->pending_resolutions_by_address,
3852 /* value size */ sizeof (uword),
3853 /* key size */ sizeof (ip6_address_t));
3854
John Lo1edfba92016-08-27 01:11:57 -04003855 mhash_init (&nm->mac_changes_by_address,
3856 /* value size */ sizeof (uword),
3857 /* key size */ sizeof (ip6_address_t));
3858
Ed Warnickecb9cada2015-12-08 15:45:58 -07003859 /* default, configurable */
3860 nm->limit_neighbor_cache_size = 50000;
3861
3862#if 0
3863 /* $$$$ Hack fix for today */
Dave Barachd7cb1b52016-12-09 09:52:16 -05003864 vec_validate_init_empty
3865 (im->discover_neighbor_next_index_by_hw_if_index, 32, 0 /* drop */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003866#endif
3867
Ed Warnickecb9cada2015-12-08 15:45:58 -07003868 return 0;
3869}
3870
3871VLIB_INIT_FUNCTION (ip6_neighbor_init);
3872
3873
Dave Barachd7cb1b52016-12-09 09:52:16 -05003874void
3875vnet_register_ip6_neighbor_resolution_event (vnet_main_t * vnm,
3876 void *address_arg,
3877 uword node_index,
3878 uword type_opaque, uword data)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003879{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003880 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3881 ip6_address_t *address = address_arg;
3882 uword *p;
3883 pending_resolution_t *pr;
3884
Ed Warnickecb9cada2015-12-08 15:45:58 -07003885 pool_get (nm->pending_resolutions, pr);
3886
3887 pr->next_index = ~0;
3888 pr->node_index = node_index;
3889 pr->type_opaque = type_opaque;
3890 pr->data = data;
3891
3892 p = mhash_get (&nm->pending_resolutions_by_address, address);
3893 if (p)
3894 {
3895 /* Insert new resolution at the head of the list */
3896 pr->next_index = p[0];
3897 mhash_unset (&nm->pending_resolutions_by_address, address, 0);
3898 }
Dave Barachd7cb1b52016-12-09 09:52:16 -05003899
3900 mhash_set (&nm->pending_resolutions_by_address, address,
3901 pr - nm->pending_resolutions, 0 /* old value */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003902}
3903
Dave Barachd7cb1b52016-12-09 09:52:16 -05003904int
3905vnet_add_del_ip6_nd_change_event (vnet_main_t * vnm,
3906 void *data_callback,
3907 u32 pid,
3908 void *address_arg,
3909 uword node_index,
3910 uword type_opaque, uword data, int is_add)
John Lo1edfba92016-08-27 01:11:57 -04003911{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003912 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3913 ip6_address_t *address = address_arg;
Dave Barachd7cb1b52016-12-09 09:52:16 -05003914
Eyal Barif9f40652017-04-01 03:55:08 +03003915 /* Try to find an existing entry */
3916 u32 *first = (u32 *) mhash_get (&nm->mac_changes_by_address, address);
3917 u32 *p = first;
3918 pending_resolution_t *mc;
3919 while (p && *p != ~0)
3920 {
3921 mc = pool_elt_at_index (nm->mac_changes, *p);
3922 if (mc->node_index == node_index && mc->type_opaque == type_opaque
3923 && mc->pid == pid)
3924 break;
3925 p = &mc->next_index;
3926 }
3927
3928 int found = p && *p != ~0;
John Lo1edfba92016-08-27 01:11:57 -04003929 if (is_add)
3930 {
Eyal Barif9f40652017-04-01 03:55:08 +03003931 if (found)
3932 return VNET_API_ERROR_ENTRY_ALREADY_EXISTS;
3933
John Lo1edfba92016-08-27 01:11:57 -04003934 pool_get (nm->mac_changes, mc);
Eyal Barif9f40652017-04-01 03:55:08 +03003935 *mc = (pending_resolution_t)
3936 {
3937 .next_index = ~0,.node_index = node_index,.type_opaque =
3938 type_opaque,.data = data,.data_callback = data_callback,.pid =
3939 pid,};
John Lo1edfba92016-08-27 01:11:57 -04003940
Eyal Barif9f40652017-04-01 03:55:08 +03003941 /* Insert new resolution at the end of the list */
3942 u32 new_idx = mc - nm->mac_changes;
John Lo1edfba92016-08-27 01:11:57 -04003943 if (p)
Eyal Barif9f40652017-04-01 03:55:08 +03003944 p[0] = new_idx;
3945 else
3946 mhash_set (&nm->mac_changes_by_address, address, new_idx, 0);
John Lo1edfba92016-08-27 01:11:57 -04003947 }
3948 else
3949 {
Eyal Barif9f40652017-04-01 03:55:08 +03003950 if (!found)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003951 return VNET_API_ERROR_NO_SUCH_ENTRY;
John Lo1edfba92016-08-27 01:11:57 -04003952
Eyal Barif9f40652017-04-01 03:55:08 +03003953 /* Clients may need to clean up pool entries, too */
3954 void (*fp) (u32, u8 *) = data_callback;
3955 if (fp)
3956 (*fp) (mc->data, 0 /* no new mac addrs */ );
John Lo1edfba92016-08-27 01:11:57 -04003957
Eyal Barif9f40652017-04-01 03:55:08 +03003958 /* Remove the entry from the list and delete the entry */
3959 *p = mc->next_index;
3960 pool_put (nm->mac_changes, mc);
Dave Barachd7cb1b52016-12-09 09:52:16 -05003961
Eyal Barif9f40652017-04-01 03:55:08 +03003962 /* Remove from hash if we deleted the last entry */
3963 if (*p == ~0 && p == first)
3964 mhash_unset (&nm->mac_changes_by_address, address, 0);
John Lo1edfba92016-08-27 01:11:57 -04003965 }
Eyal Barif9f40652017-04-01 03:55:08 +03003966 return 0;
John Lo1edfba92016-08-27 01:11:57 -04003967}
3968
Dave Barachd7cb1b52016-12-09 09:52:16 -05003969int
3970vnet_ip6_nd_term (vlib_main_t * vm,
3971 vlib_node_runtime_t * node,
3972 vlib_buffer_t * p0,
3973 ethernet_header_t * eth,
Eyal Baria0623f82017-03-30 03:05:06 +03003974 ip6_header_t * ip, u32 sw_if_index, u16 bd_index)
John Lo1edfba92016-08-27 01:11:57 -04003975{
Dave Barachd7cb1b52016-12-09 09:52:16 -05003976 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
3977 icmp6_neighbor_solicitation_or_advertisement_header_t *ndh;
3978 pending_resolution_t *mc;
John Lo1edfba92016-08-27 01:11:57 -04003979
3980 ndh = ip6_next_header (ip);
3981 if (ndh->icmp.type != ICMP6_neighbor_solicitation &&
3982 ndh->icmp.type != ICMP6_neighbor_advertisement)
Dave Barachd7cb1b52016-12-09 09:52:16 -05003983 return 0;
John Lo1edfba92016-08-27 01:11:57 -04003984
3985 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
3986 (p0->flags & VLIB_BUFFER_IS_TRACED)))
3987 {
3988 u8 *t0 = vlib_add_trace (vm, node, p0,
3989 sizeof (icmp6_input_trace_t));
3990 clib_memcpy (t0, ip, sizeof (icmp6_input_trace_t));
3991 }
3992
3993 /* Check if anyone want ND events for L2 BDs */
Eyal Baria0623f82017-03-30 03:05:06 +03003994 uword *p = mhash_get (&nm->mac_changes_by_address, &ip6a_zero);
3995 if (p && !ip6_address_is_link_local_unicast (&ip->src_address))
Dave Barachd7cb1b52016-12-09 09:52:16 -05003996 {
John Lo1edfba92016-08-27 01:11:57 -04003997 u32 next_index = p[0];
Dave Barachd7cb1b52016-12-09 09:52:16 -05003998 while (next_index != (u32) ~ 0)
3999 {
4000 int (*fp) (u32, u8 *, u32, ip6_address_t *);
John Lo1edfba92016-08-27 01:11:57 -04004001 int rv = 1;
4002 mc = pool_elt_at_index (nm->mac_changes, next_index);
4003 fp = mc->data_callback;
4004 /* Call the callback, return 1 to suppress dup events */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004005 if (fp)
4006 rv = (*fp) (mc->data,
4007 eth->src_address, sw_if_index, &ip->src_address);
John Lo1edfba92016-08-27 01:11:57 -04004008 /* Signal the resolver process */
4009 if (rv == 0)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004010 vlib_process_signal_event (vm, mc->node_index,
4011 mc->type_opaque, mc->data);
John Lo1edfba92016-08-27 01:11:57 -04004012 next_index = mc->next_index;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004013 }
John Lo1edfba92016-08-27 01:11:57 -04004014 }
4015
4016 /* Check if MAC entry exsist for solicited target IP */
4017 if (ndh->icmp.type == ICMP6_neighbor_solicitation)
4018 {
Dave Barachd7cb1b52016-12-09 09:52:16 -05004019 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *opt;
John Lo1edfba92016-08-27 01:11:57 -04004020 l2_bridge_domain_t *bd_config;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004021 u8 *macp;
John Lo1edfba92016-08-27 01:11:57 -04004022
4023 opt = (void *) (ndh + 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004024 if ((opt->header.type !=
John Lo1edfba92016-08-27 01:11:57 -04004025 ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address) ||
4026 (opt->header.n_data_u64s != 1))
Dave Barachd7cb1b52016-12-09 09:52:16 -05004027 return 0; /* source link layer address option not present */
4028
John Lo1edfba92016-08-27 01:11:57 -04004029 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004030 macp =
4031 (u8 *) hash_get_mem (bd_config->mac_by_ip6, &ndh->target_address);
John Lo1edfba92016-08-27 01:11:57 -04004032 if (macp)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004033 { /* found ip-mac entry, generate eighbor advertisement response */
John Lo1edfba92016-08-27 01:11:57 -04004034 int bogus_length;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004035 vlib_node_runtime_t *error_node =
4036 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
John Lo1edfba92016-08-27 01:11:57 -04004037 ip->dst_address = ip->src_address;
4038 ip->src_address = ndh->target_address;
4039 ip->hop_limit = 255;
4040 opt->header.type =
Dave Barachd7cb1b52016-12-09 09:52:16 -05004041 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
John Lo1edfba92016-08-27 01:11:57 -04004042 clib_memcpy (opt->ethernet_address, macp, 6);
4043 ndh->icmp.type = ICMP6_neighbor_advertisement;
4044 ndh->advertisement_flags = clib_host_to_net_u32
Dave Barachd7cb1b52016-12-09 09:52:16 -05004045 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED |
4046 ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
John Lo1edfba92016-08-27 01:11:57 -04004047 ndh->icmp.checksum = 0;
Dave Barachd7cb1b52016-12-09 09:52:16 -05004048 ndh->icmp.checksum =
4049 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip, &bogus_length);
4050 clib_memcpy (eth->dst_address, eth->src_address, 6);
4051 clib_memcpy (eth->src_address, macp, 6);
4052 vlib_error_count (vm, error_node->node_index,
John Lo1edfba92016-08-27 01:11:57 -04004053 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX, 1);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004054 return 1;
4055 }
John Lo1edfba92016-08-27 01:11:57 -04004056 }
4057
4058 return 0;
4059
4060}
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004061
Neale Ranns3f844d02017-02-18 00:03:54 -08004062int
4063ip6_neighbor_proxy_add_del (u32 sw_if_index, ip6_address_t * addr, u8 is_del)
4064{
4065 u32 fib_index;
4066
4067 fib_prefix_t pfx = {
4068 .fp_len = 128,
4069 .fp_proto = FIB_PROTOCOL_IP6,
4070 .fp_addr = {
4071 .ip6 = *addr,
4072 },
4073 };
4074 ip46_address_t nh = {
4075 .ip6 = *addr,
4076 };
4077
4078 fib_index = ip6_fib_table_get_index_for_sw_if_index (sw_if_index);
4079
4080 if (~0 == fib_index)
4081 return VNET_API_ERROR_NO_SUCH_FIB;
4082
4083 if (is_del)
4084 {
4085 fib_table_entry_path_remove (fib_index,
4086 &pfx,
4087 FIB_SOURCE_IP6_ND_PROXY,
4088 FIB_PROTOCOL_IP6,
4089 &nh,
4090 sw_if_index,
4091 ~0, 1, FIB_ROUTE_PATH_FLAG_NONE);
4092 /* flush the ND cache of this address if it's there */
4093 vnet_unset_ip6_ethernet_neighbor (vlib_get_main (),
4094 sw_if_index, addr, NULL, 0);
4095 }
4096 else
4097 {
4098 fib_table_entry_path_add (fib_index,
4099 &pfx,
4100 FIB_SOURCE_IP6_ND_PROXY,
4101 FIB_ENTRY_FLAG_NONE,
4102 FIB_PROTOCOL_IP6,
4103 &nh,
4104 sw_if_index,
4105 ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
4106 }
4107 return (0);
4108}
4109
4110static clib_error_t *
4111set_ip6_nd_proxy_cmd (vlib_main_t * vm,
4112 unformat_input_t * input, vlib_cli_command_t * cmd)
4113{
4114 vnet_main_t *vnm = vnet_get_main ();
4115 clib_error_t *error = 0;
4116 ip6_address_t addr;
4117 u32 sw_if_index;
4118 u8 is_del = 0;
4119
4120 if (unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
4121 {
4122 /* get the rest of the command */
4123 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
4124 {
4125 if (unformat (input, "%U", unformat_ip6_address, &addr))
4126 break;
4127 else if (unformat (input, "delete") || unformat (input, "del"))
4128 is_del = 1;
4129 else
4130 return (unformat_parse_error (input));
4131 }
4132 }
4133
4134 ip6_neighbor_proxy_add_del (sw_if_index, &addr, is_del);
4135
4136 return error;
4137}
4138
4139/* *INDENT-OFF* */
4140VLIB_CLI_COMMAND (set_ip6_nd_proxy_command, static) =
4141{
4142 .path = "set ip6 nd proxy",
4143 .short_help = "set ip6 nd proxy <HOST> <INTERFACE>",
4144 .function = set_ip6_nd_proxy_cmd,
4145};
4146/* *INDENT-ON* */
4147
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004148void
Neale Ranns3be6b282016-12-20 14:24:01 +00004149ethernet_ndp_change_mac (u32 sw_if_index)
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004150{
Dave Barachd7cb1b52016-12-09 09:52:16 -05004151 ip6_neighbor_main_t *nm = &ip6_neighbor_main;
4152 ip6_neighbor_t *n;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004153
4154 /* *INDENT-OFF* */
Dave Barachd7cb1b52016-12-09 09:52:16 -05004155 pool_foreach (n, nm->neighbor_pool,
4156 ({
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004157 if (n->key.sw_if_index == sw_if_index)
Dave Barachd7cb1b52016-12-09 09:52:16 -05004158 {
Neale Rannsb80c5362016-10-08 13:03:40 +01004159 adj_nbr_walk_nh6 (sw_if_index,
4160 &n->key.ip6_address,
4161 ip6_nd_mk_complete_walk, n);
Dave Barachd7cb1b52016-12-09 09:52:16 -05004162 }
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02004163 }));
4164 /* *INDENT-ON* */
4165}
Dave Barachd7cb1b52016-12-09 09:52:16 -05004166
4167/*
4168 * fd.io coding-style-patch-verification: ON
4169 *
4170 * Local Variables:
4171 * eval: (c-set-style "gnu")
4172 * End:
4173 */