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/ip.h>
21 #include <vnet/ip-neighbor/ip_neighbor_dp.h>
23 #include <vnet/ip/ip6_link.h>
24 #include <vnet/ip/ip6_ll_table.h>
26 #include <vnet/ethernet/ethernet.h>
30 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
32 * The files contains the API and CLI code for managing IPv6 neighbor
33 * adjacency tables and neighbor discovery logic.
36 /* multicast listener report packet format for ethernet. */
37 typedef CLIB_PACKED (struct
39 ip6_hop_by_hop_ext_t ext_hdr;
40 ip6_router_alert_option_t alert;
41 ip6_padN_option_t pad;
45 icmp6_multicast_address_record_t records[0];
46 }) icmp6_multicast_listener_report_header_t;
48 typedef CLIB_PACKED (struct
51 icmp6_multicast_listener_report_header_t report_hdr;
52 }) icmp6_multicast_listener_report_packet_t;
56 /* group information */
59 ip6_address_t mcast_address;
60 ip6_address_t *mcast_source_address_pool;
63 typedef struct ip6_nd_t_
65 /* local information */
67 int all_routers_mcast;
69 /* MLDP group information */
70 ip6_mldp_group_t *mldp_group_pool;
72 /* Hash table mapping address to index in mldp address pool. */
73 mhash_t address_to_mldp_index;
78 static ip6_link_delegate_id_t ip6_mld_delegate_id;
79 static ip6_mld_t *ip6_mld_pool;
83 static inline ip6_mld_t *
84 ip6_mld_get_itf (u32 sw_if_index)
88 imi = ip6_link_delegate_get (sw_if_index, ip6_mld_delegate_id);
90 if (INDEX_INVALID != imi)
91 return (pool_elt_at_index (ip6_mld_pool, imi));
97 * @brief Add a multicast Address to the advertised MLD set
100 ip6_neighbor_add_mld_prefix (ip6_mld_t * imd, ip6_address_t * addr)
102 ip6_mldp_group_t *mcast_group_info;
105 /* lookup mldp info for this interface */
106 p = mhash_get (&imd->address_to_mldp_index, addr);
107 mcast_group_info = p ? pool_elt_at_index (imd->mldp_group_pool, p[0]) : 0;
110 if (!mcast_group_info)
114 pool_get_zero (imd->mldp_group_pool, mcast_group_info);
116 mi = mcast_group_info - imd->mldp_group_pool;
117 mhash_set (&imd->address_to_mldp_index, addr, mi, /* old_value */
120 mcast_group_info->type = 4;
121 mcast_group_info->mcast_source_address_pool = 0;
122 mcast_group_info->num_sources = 0;
123 clib_memcpy (&mcast_group_info->mcast_address, addr,
124 sizeof (ip6_address_t));
129 * @brief Delete a multicast Address from the advertised MLD set
132 ip6_neighbor_del_mld_prefix (ip6_mld_t * imd, ip6_address_t * addr)
134 ip6_mldp_group_t *mcast_group_info;
137 p = mhash_get (&imd->address_to_mldp_index, addr);
138 mcast_group_info = p ? pool_elt_at_index (imd->mldp_group_pool, p[0]) : 0;
140 if (mcast_group_info)
142 mhash_unset (&imd->address_to_mldp_index, addr,
144 pool_put (imd->mldp_group_pool, mcast_group_info);
149 * @brief Add a multicast Address to the advertised MLD set
152 ip6_neighbor_add_mld_grp (ip6_mld_t * a,
153 ip6_multicast_address_scope_t scope,
154 ip6_multicast_link_local_group_id_t group)
158 ip6_set_reserved_multicast_address (&addr, scope, group);
160 ip6_neighbor_add_mld_prefix (a, &addr);
163 static const ethernet_interface_t *
164 ip6_mld_get_eth_itf (u32 sw_if_index)
166 const vnet_sw_interface_t *sw;
168 /* lookup radv container - ethernet interfaces only */
169 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
170 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
171 return (ethernet_get_interface (ðernet_main, sw->hw_if_index));
177 * @brief create and initialize router advertisement parameters with default
178 * values for this intfc
181 ip6_mld_link_enable (u32 sw_if_index)
183 const ethernet_interface_t *eth;
186 eth = ip6_mld_get_eth_itf (sw_if_index);
191 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
192 ip6_mld_delegate_id));
194 pool_get_zero (ip6_mld_pool, imd);
196 imd->sw_if_index = sw_if_index;
198 mhash_init (&imd->address_to_mldp_index, sizeof (uword),
199 sizeof (ip6_address_t));
201 /* add multicast groups we will always be reporting */
202 ip6_neighbor_add_mld_grp (imd,
203 IP6_MULTICAST_SCOPE_link_local,
204 IP6_MULTICAST_GROUP_ID_all_hosts);
205 ip6_neighbor_add_mld_grp (imd,
206 IP6_MULTICAST_SCOPE_link_local,
207 IP6_MULTICAST_GROUP_ID_all_routers);
208 ip6_neighbor_add_mld_grp (imd,
209 IP6_MULTICAST_SCOPE_link_local,
210 IP6_MULTICAST_GROUP_ID_mldv2_routers);
212 ip6_link_delegate_update (sw_if_index, ip6_mld_delegate_id,
217 ip6_mld_delegate_disable (index_t imdi)
222 imd = pool_elt_at_index (ip6_mld_pool, imdi);
224 /* clean MLD pools */
225 pool_flush (m, imd->mldp_group_pool,
227 mhash_unset (&imd->address_to_mldp_index, &m->mcast_address, 0);
230 pool_free (imd->mldp_group_pool);
232 mhash_free (&imd->address_to_mldp_index);
234 pool_put (ip6_mld_pool, imd);
237 /* send an mldpv2 report */
239 ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
241 vnet_main_t *vnm = vnet_get_main ();
242 vlib_main_t *vm = vnm->vlib_main;
254 icmp6_multicast_listener_report_header_t *rh0;
255 icmp6_multicast_listener_report_packet_t *rp0;
257 if (!vnet_sw_interface_is_admin_up (vnm, sw_if_index))
260 imd = ip6_mld_get_itf (sw_if_index);
265 /* send report now - build a mldpv2 report packet */
266 if (0 == vlib_buffer_alloc (vm, &bo0, n_to_alloc))
269 clib_warning ("buffer allocation failure");
273 b0 = vlib_get_buffer (vm, bo0);
275 /* adjust the sizeof the buffer to just include the ipv6 header */
276 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
278 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
280 b0->error = ICMP6_ERROR_NONE;
282 rp0 = vlib_buffer_get_current (b0);
283 ip0 = (ip6_header_t *) & rp0->ip;
284 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
286 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
288 ip0->ip_version_traffic_class_and_flow_label =
289 clib_host_to_net_u32 (0x6 << 28);
291 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
292 /* for DEBUG - vnet driver won't seem to emit router alerts */
293 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
296 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
298 /* source address MUST be the link-local address */
299 ip6_address_copy (&ip0->src_address,
300 ip6_get_link_local_address (sw_if_index));
302 /* destination is all mldpv2 routers */
303 ip6_set_reserved_multicast_address (&ip0->dst_address,
304 IP6_MULTICAST_SCOPE_link_local,
305 IP6_MULTICAST_GROUP_ID_mldv2_routers);
307 /* add reports here */
309 int num_addr_records = 0;
310 icmp6_multicast_address_record_t rr;
312 /* fill in the hop-by-hop extension header (router alert) info */
313 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
314 rh0->ext_hdr.n_data_u64s = 0;
316 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
318 rh0->alert.value = 0;
323 rh0->icmp.checksum = 0;
325 pool_foreach (m, imd->mldp_group_pool)
328 rr.aux_data_len_u32s = 0;
329 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
330 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
334 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
335 sizeof(icmp6_multicast_address_record_t)))
337 vlib_buffer_free (vm, &bo0, 1);
341 payload_length += sizeof( icmp6_multicast_address_record_t);
345 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
348 ip0->payload_length = clib_host_to_net_u16 (payload_length);
350 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
352 ASSERT (bogus_length == 0);
355 * OK to override w/ no regard for actual FIB, because
356 * ip6-rewrite only looks at the adjacency.
358 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
359 vnet_main.local_interface_sw_if_index;
361 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
362 ip6_link_get_mcast_adj (sw_if_index);
363 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
365 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
367 f = vlib_get_frame_to_node (vm, node->index);
368 to_next = vlib_frame_vector_args (f);
372 vlib_put_frame_to_node (vm, node->index, f);
376 /* send a RA or update the timer info etc.. */
378 ip6_mld_timer_event (vlib_main_t * vm,
379 vlib_node_runtime_t * node, vlib_frame_t * frame)
381 vnet_main_t *vnm = vnet_get_main ();
384 /* Interface ip6 radv info list */
385 pool_foreach (imd, ip6_mld_pool)
387 if (!vnet_sw_interface_is_admin_up (vnm, imd->sw_if_index))
389 imd->all_routers_mcast = 0;
393 /* Make sure that we've joined the all-routers multicast group */
394 if (!imd->all_routers_mcast)
396 /* send MDLP_REPORT_EVENT message */
397 ip6_neighbor_send_mldpv2_report(imd->sw_if_index);
398 imd->all_routers_mcast = 1;
406 ip6_mld_event_process (vlib_main_t * vm,
407 vlib_node_runtime_t * node, vlib_frame_t * frame)
415 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
417 if (!vlib_process_get_event_data (vm, &event_type))
419 /* No events found: timer expired. */
420 /* process interface list and send RAs as appropriate, update timer info */
421 ip6_mld_timer_event (vm, node, frame);
423 /* else; no events */
425 return frame->n_vectors;
428 VLIB_REGISTER_NODE (ip6_mld_event_process_node) = {
429 .function = ip6_mld_event_process,
430 .name = "ip6-mld-process",
431 .type = VLIB_NODE_TYPE_PROCESS,
435 format_ip6_mld (u8 * s, va_list * args)
437 index_t imi = va_arg (*args, index_t);
438 u32 indent = va_arg (*args, u32);
442 imd = pool_elt_at_index (ip6_mld_pool, imi);
444 s = format (s, "%UJoined group address(es):\n", format_white_space, indent);
446 pool_foreach (m, imd->mldp_group_pool)
448 s = format (s, "%U%U\n",
449 format_white_space, indent+2,
458 * @brief callback when an interface address is added or deleted
461 ip6_mld_address_add (u32 imi,
462 const ip6_address_t * address, u8 address_oength)
467 imd = pool_elt_at_index (ip6_mld_pool, imi);
469 /* create solicited node multicast address for this interface address */
470 ip6_set_solicited_node_multicast_address (&a, 0);
472 a.as_u8[0xd] = address->as_u8[0xd];
473 a.as_u8[0xe] = address->as_u8[0xe];
474 a.as_u8[0xf] = address->as_u8[0xf];
476 ip6_neighbor_add_mld_prefix (imd, &a);
480 ip6_mld_address_del (u32 imi,
481 const ip6_address_t * address, u8 address_oength)
486 imd = pool_elt_at_index (ip6_mld_pool, imi);
488 /* create solicited node multicast address for this interface address */
489 ip6_set_solicited_node_multicast_address (&a, 0);
491 a.as_u8[0xd] = address->as_u8[0xd];
492 a.as_u8[0xe] = address->as_u8[0xe];
493 a.as_u8[0xf] = address->as_u8[0xf];
495 ip6_neighbor_del_mld_prefix (imd, &a);
499 * VFT for registering as a delegate to an IP6 link
501 const static ip6_link_delegate_vft_t ip6_mld_delegate_vft = {
502 .ildv_disable = ip6_mld_delegate_disable,
503 .ildv_enable = ip6_mld_link_enable,
504 .ildv_format = format_ip6_mld,
505 .ildv_addr_add = ip6_mld_address_add,
506 .ildv_addr_del = ip6_mld_address_del,
509 static clib_error_t *
510 ip6_mld_init (vlib_main_t * vm)
512 ip6_mld_delegate_id = ip6_link_delegate_register (&ip6_mld_delegate_vft);
517 VLIB_INIT_FUNCTION (ip6_mld_init) =
519 .runs_after = VLIB_INITS("icmp6_init"),
523 * fd.io coding-style-patch-verification: ON
526 * eval: (c-set-style "gnu")