2 * ip/ip6_neighbor.c: IP6 neighbor handling
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/ip6-nd/ip6_nd.h>
20 #include <vnet/ip-neighbor/ip_neighbor.h>
21 #include <vnet/ip-neighbor/ip_neighbor_dp.h>
23 #include <vnet/fib/ip6_fib.h>
24 #include <vnet/ip/ip6_link.h>
25 #include <vnet/ip/ip6_ll_table.h>
29 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
31 * The files contains the API and CLI code for managing IPv6 neighbor
32 * adjacency tables and neighbor discovery logic.
35 #define DEF_MAX_RADV_INTERVAL 200
36 #define DEF_MIN_RADV_INTERVAL .75 * DEF_MAX_RADV_INTERVAL
38 typedef struct ip6_nd_t_
40 /* local information */
44 u32 n_solicitations_rcvd;
45 u32 n_solicitations_dropped;
48 static ip6_link_delegate_id_t ip6_nd_delegate_id;
49 static ip6_nd_t *ip6_nd_pool;
54 ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP,
55 ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY,
56 ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
57 } icmp6_neighbor_solicitation_or_advertisement_next_t;
59 static_always_inline uword
60 icmp6_neighbor_solicitation_or_advertisement (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
63 uword is_solicitation)
65 vnet_main_t *vnm = vnet_get_main ();
66 ip6_main_t *im = &ip6_main;
67 uword n_packets = frame->n_vectors;
69 u32 n_left_from, n_left_to_next, next_index, n_advertisements_sent;
70 icmp6_neighbor_discovery_option_type_t option_type;
71 vlib_node_runtime_t *error_node =
72 vlib_node_get_runtime (vm, ip6_icmp_input_node.index);
75 from = vlib_frame_vector_args (frame);
76 n_left_from = n_packets;
77 next_index = node->cached_next_index;
79 if (node->flags & VLIB_NODE_FLAG_TRACE)
80 vlib_trace_frame_buffers_only (vm, node, from, frame->n_vectors,
82 sizeof (icmp6_input_trace_t));
86 ? ICMP6_NEIGHBOR_DISCOVERY_OPTION_source_link_layer_address
87 : ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address);
88 n_advertisements_sent = 0;
90 while (n_left_from > 0)
92 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
94 while (n_left_from > 0 && n_left_to_next > 0)
98 icmp6_neighbor_solicitation_or_advertisement_header_t *h0;
99 icmp6_neighbor_discovery_ethernet_link_layer_address_option_t *o0;
100 u32 bi0, options_len0, sw_if_index0, next0, error0;
101 u32 ip6_sadd_link_local, ip6_sadd_unspecified;
105 bi0 = to_next[0] = from[0];
112 p0 = vlib_get_buffer (vm, bi0);
113 ip0 = vlib_buffer_get_current (p0);
114 h0 = ip6_next_header (ip0);
116 clib_net_to_host_u16 (ip0->payload_length) - sizeof (h0[0]);
118 error0 = ICMP6_ERROR_NONE;
119 sw_if_index0 = vnet_buffer (p0)->sw_if_index[VLIB_RX];
120 ip6_sadd_link_local =
121 ip6_address_is_link_local_unicast (&ip0->src_address);
122 ip6_sadd_unspecified =
123 ip6_address_is_unspecified (&ip0->src_address);
125 /* Check that source address is unspecified, link-local or else on-link. */
126 if (!ip6_sadd_unspecified && !ip6_sadd_link_local)
128 u32 src_adj_index0 = ip6_src_lookup_for_packet (im, p0, ip0);
130 if (ADJ_INDEX_INVALID != src_adj_index0)
132 ip_adjacency_t *adj0 = adj_get (src_adj_index0);
134 /* Allow all realistic-looking rewrite adjacencies to pass */
135 ni0 = adj0->lookup_next_index;
136 is_rewrite0 = (ni0 >= IP_LOOKUP_NEXT_ARP) &&
137 (ni0 < IP6_LOOKUP_N_NEXT);
139 error0 = ((adj0->rewrite_header.sw_if_index != sw_if_index0
142 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK
148 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_NOT_ON_LINK;
152 o0 = (void *) (h0 + 1);
153 o0 = ((options_len0 == 8 && o0->header.type == option_type
154 && o0->header.n_data_u64s == 1) ? o0 : 0);
156 /* If src address unspecified or link local, donot learn neighbor MAC */
157 if (PREDICT_TRUE (error0 == ICMP6_ERROR_NONE && o0 != 0 &&
158 !ip6_sadd_unspecified))
160 ip_neighbor_learn_t learn = {
161 .sw_if_index = sw_if_index0,
162 .type = IP46_TYPE_IP6,
163 .ip.ip6 = (is_solicitation ?
164 ip0->src_address : h0->target_address),
166 memcpy (&learn.mac, o0->ethernet_address, sizeof (learn.mac));
167 ip_neighbor_learn_dp (&learn);
170 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
172 /* Check that target address is local to this router. */
173 fib_node_index_t fei;
177 ip6_fib_table_get_index_for_sw_if_index (sw_if_index0);
181 error0 = ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
185 if (ip6_address_is_link_local_unicast (&h0->target_address))
187 fei = ip6_fib_table_lookup_exact_match
188 (ip6_ll_fib_get (sw_if_index0),
189 &h0->target_address, 128);
193 fei = ip6_fib_table_lookup_exact_match (fib_index,
198 if (FIB_NODE_INDEX_INVALID == fei)
200 /* The target address is not in the FIB */
202 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
206 if (FIB_ENTRY_FLAG_LOCAL &
207 fib_entry_get_flags_for_source (fei,
208 FIB_SOURCE_INTERFACE))
210 /* It's an address that belongs to one of our interfaces
214 if (fib_entry_is_sourced
215 (fei, FIB_SOURCE_IP6_ND_PROXY) ||
216 fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND))
218 /* The address was added by IPv6 Proxy ND config.
219 * We should only respond to these if the NS arrived on
220 * the link that has a matching covering prefix */
225 ICMP6_ERROR_NEIGHBOR_SOLICITATION_SOURCE_UNKNOWN;
232 next0 = (error0 != ICMP6_ERROR_NONE
233 ? ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP
234 : ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY);
238 error0 = error0 == ICMP6_ERROR_NONE ?
239 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_RX : error0;
242 if (is_solicitation && error0 == ICMP6_ERROR_NONE)
244 vnet_sw_interface_t *sw_if0;
245 ethernet_interface_t *eth_if0;
246 ethernet_header_t *eth0;
248 /* dst address is either source address or the all-nodes mcast addr */
249 if (!ip6_sadd_unspecified)
250 ip0->dst_address = ip0->src_address;
252 ip6_set_reserved_multicast_address (&ip0->dst_address,
253 IP6_MULTICAST_SCOPE_link_local,
254 IP6_MULTICAST_GROUP_ID_all_hosts);
256 ip0->src_address = h0->target_address;
257 ip0->hop_limit = 255;
258 h0->icmp.type = ICMP6_neighbor_advertisement;
260 sw_if0 = vnet_get_sup_sw_interface (vnm, sw_if_index0);
261 ASSERT (sw_if0->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
263 ethernet_get_interface (ðernet_main, sw_if0->hw_if_index);
266 clib_memcpy (o0->ethernet_address, eth_if0->address, 6);
268 ICMP6_NEIGHBOR_DISCOVERY_OPTION_target_link_layer_address;
271 h0->advertisement_flags = clib_host_to_net_u32
272 (ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_SOLICITED
273 | ICMP6_NEIGHBOR_ADVERTISEMENT_FLAG_OVERRIDE);
275 h0->icmp.checksum = 0;
277 ip6_tcp_udp_icmp_compute_checksum (vm, p0, ip0,
279 ASSERT (bogus_length == 0);
281 /* Reuse current MAC header, copy SMAC to DMAC and
282 * interface MAC to SMAC */
283 vlib_buffer_advance (p0, -ethernet_buffer_header_size (p0));
284 eth0 = vlib_buffer_get_current (p0);
285 clib_memcpy (eth0->dst_address, eth0->src_address, 6);
287 clib_memcpy (eth0->src_address, eth_if0->address, 6);
289 /* Setup input and output sw_if_index for packet */
290 ASSERT (vnet_buffer (p0)->sw_if_index[VLIB_RX] == sw_if_index0);
291 vnet_buffer (p0)->sw_if_index[VLIB_TX] = sw_if_index0;
292 vnet_buffer (p0)->sw_if_index[VLIB_RX] =
293 vnet_main.local_interface_sw_if_index;
295 n_advertisements_sent++;
298 p0->error = error_node->errors[error0];
300 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
301 to_next, n_left_to_next,
305 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
308 /* Account for advertisements sent. */
309 vlib_error_count (vm, error_node->node_index,
310 ICMP6_ERROR_NEIGHBOR_ADVERTISEMENTS_TX,
311 n_advertisements_sent);
313 return frame->n_vectors;
316 static const ethernet_interface_t *
317 ip6_nd_get_eth_itf (u32 sw_if_index)
319 const vnet_sw_interface_t *sw;
321 /* lookup radv container - ethernet interfaces only */
322 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
323 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
324 return (ethernet_get_interface (ðernet_main, sw->hw_if_index));
330 * @brief called when IP6 is enabled on a link.
331 * create and initialize router advertisement parameters with default
332 * values for this intfc
335 ip6_nd_link_enable (u32 sw_if_index)
337 const ethernet_interface_t *eth;
340 eth = ip6_nd_get_eth_itf (sw_if_index);
345 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
346 ip6_nd_delegate_id));
348 pool_get_zero (ip6_nd_pool, ind);
350 ind->sw_if_index = sw_if_index;
352 ip6_link_delegate_update (sw_if_index, ip6_nd_delegate_id,
357 ip6_nd_delegate_disable (index_t indi)
361 ind = pool_elt_at_index (ip6_nd_pool, indi);
363 pool_put (ip6_nd_pool, ind);
367 icmp6_neighbor_solicitation (vlib_main_t * vm,
368 vlib_node_runtime_t * node, vlib_frame_t * frame)
370 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
371 /* is_solicitation */
376 icmp6_neighbor_advertisement (vlib_main_t * vm,
377 vlib_node_runtime_t * node,
378 vlib_frame_t * frame)
380 return icmp6_neighbor_solicitation_or_advertisement (vm, node, frame,
381 /* is_solicitation */
386 VLIB_REGISTER_NODE (ip6_icmp_neighbor_solicitation_node,static) =
388 .function = icmp6_neighbor_solicitation,
389 .name = "icmp6-neighbor-solicitation",
391 .vector_size = sizeof (u32),
393 .format_trace = format_icmp6_input_trace,
395 .n_next_nodes = ICMP6_NEIGHBOR_SOLICITATION_N_NEXT,
397 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_DROP] = "ip6-drop",
398 [ICMP6_NEIGHBOR_SOLICITATION_NEXT_REPLY] = "interface-output",
402 VLIB_REGISTER_NODE (ip6_icmp_neighbor_advertisement_node,static) =
404 .function = icmp6_neighbor_advertisement,
405 .name = "icmp6-neighbor-advertisement",
407 .vector_size = sizeof (u32),
409 .format_trace = format_icmp6_input_trace,
419 format_ip6_nd (u8 * s, va_list * args)
421 CLIB_UNUSED (index_t indi) = va_arg (*args, index_t);
422 u32 indent = va_arg (*args, u32);
424 s = format (s, "%UNeighbor Discovery: enabled\n",
425 format_white_space, indent);
427 s = format (s, "%UICMP redirects are disabled\n",
428 format_white_space, indent + 2);
429 s = format (s, "%UICMP unreachables are not sent\n",
430 format_white_space, indent + 2);
431 s = format (s, "%UND DAD is disabled\n", format_white_space, indent + 2);
432 //s = format (s, "%UND reachable time is %d milliseconds\n",);
438 * VFT to act as an implementation of a neighbour protocol
440 const static ip_neighbor_vft_t ip6_nd_impl_vft = {
441 .inv_proxy6_add = ip6_nd_proxy_add,
442 .inv_proxy6_del = ip6_nd_proxy_del,
446 * VFT for registering as a delegate to an IP6 link
448 const static ip6_link_delegate_vft_t ip6_nd_delegate_vft = {
449 .ildv_disable = ip6_nd_delegate_disable,
450 .ildv_enable = ip6_nd_link_enable,
451 .ildv_format = format_ip6_nd,
454 static clib_error_t *
455 ip6_nd_init (vlib_main_t * vm)
457 icmp6_register_type (vm, ICMP6_neighbor_solicitation,
458 ip6_icmp_neighbor_solicitation_node.index);
459 icmp6_register_type (vm, ICMP6_neighbor_advertisement,
460 ip6_icmp_neighbor_advertisement_node.index);
462 ip_neighbor_register (IP46_TYPE_IP6, &ip6_nd_impl_vft);
464 ip6_nd_delegate_id = ip6_link_delegate_register (&ip6_nd_delegate_vft);
470 VLIB_INIT_FUNCTION (ip6_nd_init) =
472 .runs_after = VLIB_INITS("icmp6_init"),
477 * fd.io coding-style-patch-verification: ON
480 * eval: (c-set-style "gnu")