blob: 6e6a5cab45e27338515ba2df1c6d74aa7ea90b1a [file] [log] [blame]
Neale Rannscbe25aa2019-09-30 10:53:31 +00001/*
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/ip6-nd/ip6_nd.h>
19
20#include <vnet/ip/ip.h>
21#include <vnet/ip-neighbor/ip_neighbor_dp.h>
22
23#include <vnet/fib/ip6_fib.h>
24#include <vnet/ip/ip6_link.h>
25#include <vnet/ip/ip6_ll_table.h>
26
27/**
28 * @file
29 * @brief IPv6 Neighbor Adjacency and Neighbor Discovery.
30 *
31 * The files contains the API and CLI code for managing IPv6 neighbor
32 * adjacency tables and neighbor discovery logic.
33 */
34
35/* *INDENT-OFF*/
36/* multicast listener report packet format for ethernet. */
37typedef CLIB_PACKED (struct
38{
39 ip6_hop_by_hop_ext_t ext_hdr;
40 ip6_router_alert_option_t alert;
41 ip6_padN_option_t pad;
42 icmp46_header_t icmp;
43 u16 rsvd;
44 u16 num_addr_records;
45 icmp6_multicast_address_record_t records[0];
46}) icmp6_multicast_listener_report_header_t;
47
48typedef CLIB_PACKED (struct
49{
50 ip6_header_t ip;
51 icmp6_multicast_listener_report_header_t report_hdr;
52}) icmp6_multicast_listener_report_packet_t;
53/* *INDENT-ON*/
54
55typedef struct
56{
57 /* group information */
58 u16 num_sources;
59 u8 type;
60 ip6_address_t mcast_address;
61 ip6_address_t *mcast_source_address_pool;
62} ip6_mldp_group_t;
63
64typedef struct ip6_nd_t_
65{
66 /* local information */
67 u32 sw_if_index;
68 int all_routers_mcast;
69
70 /* MLDP group information */
71 ip6_mldp_group_t *mldp_group_pool;
72
73 /* Hash table mapping address to index in mldp address pool. */
74 mhash_t address_to_mldp_index;
75
76} ip6_mld_t;
77
78
79static ip6_link_delegate_id_t ip6_mld_delegate_id;
80static ip6_mld_t *ip6_mld_pool;
81
82/////
83
84static inline ip6_mld_t *
85ip6_mld_get_itf (u32 sw_if_index)
86{
87 index_t imi;
88
89 imi = ip6_link_delegate_get (sw_if_index, ip6_mld_delegate_id);
90
91 if (INDEX_INVALID != imi)
92 return (pool_elt_at_index (ip6_mld_pool, imi));
93
94 return (NULL);
95}
96
97/**
98 * @brief Add a multicast Address to the advertised MLD set
99 */
100static void
101ip6_neighbor_add_mld_prefix (ip6_mld_t * imd, ip6_address_t * addr)
102{
103 ip6_mldp_group_t *mcast_group_info;
104 uword *p;
105
106 /* lookup mldp info for this interface */
107 p = mhash_get (&imd->address_to_mldp_index, addr);
108 mcast_group_info = p ? pool_elt_at_index (imd->mldp_group_pool, p[0]) : 0;
109
110 /* add address */
111 if (!mcast_group_info)
112 {
113 /* add */
114 u32 mi;
115 pool_get_zero (imd->mldp_group_pool, mcast_group_info);
116
117 mi = mcast_group_info - imd->mldp_group_pool;
118 mhash_set (&imd->address_to_mldp_index, addr, mi, /* old_value */
119 0);
120
121 mcast_group_info->type = 4;
122 mcast_group_info->mcast_source_address_pool = 0;
123 mcast_group_info->num_sources = 0;
124 clib_memcpy (&mcast_group_info->mcast_address, addr,
125 sizeof (ip6_address_t));
126 }
127}
128
129/**
130 * @brief Delete a multicast Address from the advertised MLD set
131 */
132static void
133ip6_neighbor_del_mld_prefix (ip6_mld_t * imd, ip6_address_t * addr)
134{
135 ip6_mldp_group_t *mcast_group_info;
136 uword *p;
137
Benoît Ganneae45d472020-06-16 12:07:09 +0200138 p = mhash_get (&imd->address_to_mldp_index, addr);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000139 mcast_group_info = p ? pool_elt_at_index (imd->mldp_group_pool, p[0]) : 0;
140
141 if (mcast_group_info)
142 {
Benoît Ganneae45d472020-06-16 12:07:09 +0200143 mhash_unset (&imd->address_to_mldp_index, addr,
Neale Rannscbe25aa2019-09-30 10:53:31 +0000144 /* old_value */ 0);
145 pool_put (imd->mldp_group_pool, mcast_group_info);
146 }
147}
148
149/**
150 * @brief Add a multicast Address to the advertised MLD set
151 */
152static void
153ip6_neighbor_add_mld_grp (ip6_mld_t * a,
154 ip6_multicast_address_scope_t scope,
155 ip6_multicast_link_local_group_id_t group)
156{
157 ip6_address_t addr;
158
159 ip6_set_reserved_multicast_address (&addr, scope, group);
160
161 ip6_neighbor_add_mld_prefix (a, &addr);
162}
163
164static const ethernet_interface_t *
165ip6_mld_get_eth_itf (u32 sw_if_index)
166{
167 const vnet_sw_interface_t *sw;
168
169 /* lookup radv container - ethernet interfaces only */
170 sw = vnet_get_sup_sw_interface (vnet_get_main (), sw_if_index);
171 if (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE)
172 return (ethernet_get_interface (&ethernet_main, sw->hw_if_index));
173
174 return (NULL);
175}
176
177/**
178 * @brief create and initialize router advertisement parameters with default
179 * values for this intfc
180 */
181static void
182ip6_mld_link_enable (u32 sw_if_index)
183{
184 const ethernet_interface_t *eth;
185 ip6_mld_t *imd;
186
187 eth = ip6_mld_get_eth_itf (sw_if_index);
188
189 if (NULL == eth)
190 return;
191
192 ASSERT (INDEX_INVALID == ip6_link_delegate_get (sw_if_index,
193 ip6_mld_delegate_id));
194
195 pool_get_zero (ip6_mld_pool, imd);
196
197 imd->sw_if_index = sw_if_index;
198
199 mhash_init (&imd->address_to_mldp_index, sizeof (uword),
200 sizeof (ip6_address_t));
201
202 /* add multicast groups we will always be reporting */
203 ip6_neighbor_add_mld_grp (imd,
204 IP6_MULTICAST_SCOPE_link_local,
205 IP6_MULTICAST_GROUP_ID_all_hosts);
206 ip6_neighbor_add_mld_grp (imd,
207 IP6_MULTICAST_SCOPE_link_local,
208 IP6_MULTICAST_GROUP_ID_all_routers);
209 ip6_neighbor_add_mld_grp (imd,
210 IP6_MULTICAST_SCOPE_link_local,
211 IP6_MULTICAST_GROUP_ID_mldv2_routers);
212
213 ip6_link_delegate_update (sw_if_index, ip6_mld_delegate_id,
214 imd - ip6_mld_pool);
215}
216
217static void
218ip6_mld_delegate_disable (index_t imdi)
219{
220 ip6_mldp_group_t *m;
221 ip6_mld_t *imd;
222
223 imd = pool_elt_at_index (ip6_mld_pool, imdi);
224
225 /* clean MLD pools */
226 /* *INDENT-OFF* */
227 pool_flush (m, imd->mldp_group_pool,
228 ({
229 mhash_unset (&imd->address_to_mldp_index, &m->mcast_address, 0);
230 }));
231 /* *INDENT-ON* */
232
233 pool_free (imd->mldp_group_pool);
234
235 mhash_free (&imd->address_to_mldp_index);
236
237 pool_put (ip6_mld_pool, imd);
238}
239
240/* send an mldpv2 report */
241static void
242ip6_neighbor_send_mldpv2_report (u32 sw_if_index)
243{
244 vnet_main_t *vnm = vnet_get_main ();
245 vlib_main_t *vm = vnm->vlib_main;
246 int bogus_length;
247
248 ip6_mld_t *imd;
249 u16 payload_length;
250 vlib_buffer_t *b0;
251 ip6_header_t *ip0;
252 u32 *to_next;
253 vlib_frame_t *f;
254 u32 bo0;
255 u32 n_to_alloc = 1;
256
257 icmp6_multicast_listener_report_header_t *rh0;
258 icmp6_multicast_listener_report_packet_t *rp0;
259
Neale Rannsf267d112020-02-07 09:45:07 +0000260 if (!vnet_sw_interface_is_admin_up (vnm, sw_if_index))
Neale Rannscbe25aa2019-09-30 10:53:31 +0000261 return;
262
263 imd = ip6_mld_get_itf (sw_if_index);
264
265 if (NULL == imd)
266 return;
267
268 /* send report now - build a mldpv2 report packet */
269 if (0 == vlib_buffer_alloc (vm, &bo0, n_to_alloc))
270 {
271 alloc_fail:
272 clib_warning ("buffer allocation failure");
273 return;
274 }
275
276 b0 = vlib_get_buffer (vm, bo0);
277
278 /* adjust the sizeof the buffer to just include the ipv6 header */
279 b0->current_length = sizeof (icmp6_multicast_listener_report_packet_t);
280
281 payload_length = sizeof (icmp6_multicast_listener_report_header_t);
282
283 b0->error = ICMP6_ERROR_NONE;
284
285 rp0 = vlib_buffer_get_current (b0);
286 ip0 = (ip6_header_t *) & rp0->ip;
287 rh0 = (icmp6_multicast_listener_report_header_t *) & rp0->report_hdr;
288
289 clib_memset (rp0, 0x0, sizeof (icmp6_multicast_listener_report_packet_t));
290
291 ip0->ip_version_traffic_class_and_flow_label =
292 clib_host_to_net_u32 (0x6 << 28);
293
294 ip0->protocol = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS;
295 /* for DEBUG - vnet driver won't seem to emit router alerts */
296 /* ip0->protocol = IP_PROTOCOL_ICMP6; */
297 ip0->hop_limit = 1;
298
299 rh0->icmp.type = ICMP6_multicast_listener_report_v2;
300
301 /* source address MUST be the link-local address */
302 ip6_address_copy (&ip0->src_address,
303 ip6_get_link_local_address (sw_if_index));
304
305 /* destination is all mldpv2 routers */
306 ip6_set_reserved_multicast_address (&ip0->dst_address,
307 IP6_MULTICAST_SCOPE_link_local,
308 IP6_MULTICAST_GROUP_ID_mldv2_routers);
309
310 /* add reports here */
311 ip6_mldp_group_t *m;
312 int num_addr_records = 0;
313 icmp6_multicast_address_record_t rr;
314
315 /* fill in the hop-by-hop extension header (router alert) info */
316 rh0->ext_hdr.next_hdr = IP_PROTOCOL_ICMP6;
317 rh0->ext_hdr.n_data_u64s = 0;
318
319 rh0->alert.type = IP6_MLDP_ALERT_TYPE;
320 rh0->alert.len = 2;
321 rh0->alert.value = 0;
322
323 rh0->pad.type = 1;
324 rh0->pad.len = 0;
325
326 rh0->icmp.checksum = 0;
327
328 /* *INDENT-OFF* */
329 pool_foreach (m, imd->mldp_group_pool,
330 ({
331 rr.type = m->type;
332 rr.aux_data_len_u32s = 0;
333 rr.num_sources = clib_host_to_net_u16 (m->num_sources);
334 clib_memcpy(&rr.mcast_addr, &m->mcast_address, sizeof(ip6_address_t));
335
336 num_addr_records++;
337
338 if(vlib_buffer_add_data (vm, &bo0, (void *)&rr,
339 sizeof(icmp6_multicast_address_record_t)))
340 {
341 vlib_buffer_free (vm, &bo0, 1);
342 goto alloc_fail;
343 }
344
345 payload_length += sizeof( icmp6_multicast_address_record_t);
346 }));
347 /* *INDENT-ON* */
348
349 rh0->rsvd = 0;
350 rh0->num_addr_records = clib_host_to_net_u16 (num_addr_records);
351
352 /* update lengths */
353 ip0->payload_length = clib_host_to_net_u16 (payload_length);
354
355 rh0->icmp.checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip0,
356 &bogus_length);
357 ASSERT (bogus_length == 0);
358
359 /*
360 * OK to override w/ no regard for actual FIB, because
361 * ip6-rewrite only looks at the adjacency.
362 */
363 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
364 vnet_main.local_interface_sw_if_index;
365
366 vnet_buffer (b0)->ip.adj_index[VLIB_TX] =
367 ip6_link_get_mcast_adj (sw_if_index);
368 b0->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
369
370 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite-mcast");
371
372 f = vlib_get_frame_to_node (vm, node->index);
373 to_next = vlib_frame_vector_args (f);
374 to_next[0] = bo0;
375 f->n_vectors = 1;
376
377 vlib_put_frame_to_node (vm, node->index, f);
378 return;
379}
380
381/* send a RA or update the timer info etc.. */
382static uword
383ip6_mld_timer_event (vlib_main_t * vm,
384 vlib_node_runtime_t * node, vlib_frame_t * frame)
385{
386 vnet_main_t *vnm = vnet_get_main ();
387 ip6_mld_t *imd;
388
389 /* Interface ip6 radv info list */
390 /* *INDENT-OFF* */
391 pool_foreach (imd, ip6_mld_pool,
392 ({
Neale Rannsf267d112020-02-07 09:45:07 +0000393 if (!vnet_sw_interface_is_admin_up (vnm, imd->sw_if_index))
Neale Rannscbe25aa2019-09-30 10:53:31 +0000394 {
395 imd->all_routers_mcast = 0;
396 continue;
397 }
398
399 /* Make sure that we've joined the all-routers multicast group */
Neale Rannsf267d112020-02-07 09:45:07 +0000400 if (!imd->all_routers_mcast)
Neale Rannscbe25aa2019-09-30 10:53:31 +0000401 {
402 /* send MDLP_REPORT_EVENT message */
403 ip6_neighbor_send_mldpv2_report(imd->sw_if_index);
404 imd->all_routers_mcast = 1;
405 }
406 }));
407 /* *INDENT-ON* */
408
409 return 0;
410}
411
412static uword
413ip6_mld_event_process (vlib_main_t * vm,
414 vlib_node_runtime_t * node, vlib_frame_t * frame)
415{
416 uword event_type;
417
418 /* init code here */
419
420 while (1)
421 {
422 vlib_process_wait_for_event_or_clock (vm, 1. /* seconds */ );
423
424 if (!vlib_process_get_event_data (vm, &event_type))
425 {
426 /* No events found: timer expired. */
427 /* process interface list and send RAs as appropriate, update timer info */
428 ip6_mld_timer_event (vm, node, frame);
429 }
430 /* else; no events */
431 }
432 return frame->n_vectors;
433}
434
435/* *INDENT-OFF* */
436VLIB_REGISTER_NODE (ip6_mld_event_process_node) = {
437 .function = ip6_mld_event_process,
438 .name = "ip6-mld-process",
439 .type = VLIB_NODE_TYPE_PROCESS,
440};
441/* *INDENT-ON* */
442
443static u8 *
444format_ip6_mld (u8 * s, va_list * args)
445{
446 index_t imi = va_arg (*args, index_t);
447 u32 indent = va_arg (*args, u32);
448 ip6_mldp_group_t *m;
449 ip6_mld_t *imd;
450
451 imd = pool_elt_at_index (ip6_mld_pool, imi);
452
453 s = format (s, "%UJoined group address(es):\n", format_white_space, indent);
454
455 /* *INDENT-OFF* */
456 pool_foreach (m, imd->mldp_group_pool,
457 ({
458 s = format (s, "%U%U\n",
459 format_white_space, indent+2,
460 format_ip6_address,
461 &m->mcast_address);
462 }));
463 /* *INDENT-ON* */
464
465 return (s);
466}
467
468/**
469 * @brief callback when an interface address is added or deleted
470 */
471static void
472ip6_mld_address_add (u32 imi,
473 const ip6_address_t * address, u8 address_oength)
474{
475 ip6_mld_t *imd;
476 ip6_address_t a;
477
478 imd = pool_elt_at_index (ip6_mld_pool, imi);
479
480 /* create solicited node multicast address for this interface address */
481 ip6_set_solicited_node_multicast_address (&a, 0);
482
483 a.as_u8[0xd] = address->as_u8[0xd];
484 a.as_u8[0xe] = address->as_u8[0xe];
485 a.as_u8[0xf] = address->as_u8[0xf];
486
487 ip6_neighbor_add_mld_prefix (imd, &a);
488}
489
490static void
491ip6_mld_address_del (u32 imi,
492 const ip6_address_t * address, u8 address_oength)
493{
494 ip6_mld_t *imd;
495 ip6_address_t a;
496
497 imd = pool_elt_at_index (ip6_mld_pool, imi);
498
499 /* create solicited node multicast address for this interface address */
500 ip6_set_solicited_node_multicast_address (&a, 0);
501
502 a.as_u8[0xd] = address->as_u8[0xd];
503 a.as_u8[0xe] = address->as_u8[0xe];
504 a.as_u8[0xf] = address->as_u8[0xf];
505
506 ip6_neighbor_del_mld_prefix (imd, &a);
507}
508
509/**
510 * VFT for registering as a delegate to an IP6 link
511 */
512const static ip6_link_delegate_vft_t ip6_mld_delegate_vft = {
513 .ildv_disable = ip6_mld_delegate_disable,
514 .ildv_enable = ip6_mld_link_enable,
515 .ildv_format = format_ip6_mld,
516 .ildv_addr_add = ip6_mld_address_add,
517 .ildv_addr_del = ip6_mld_address_del,
518};
519
520static clib_error_t *
521ip6_mld_init (vlib_main_t * vm)
522{
523 ip6_mld_delegate_id = ip6_link_delegate_register (&ip6_mld_delegate_vft);
524
525 return (NULL);
526}
527
528/* *INDENT-OFF* */
529VLIB_INIT_FUNCTION (ip6_mld_init) =
530{
531 .runs_after = VLIB_INITS("icmp6_init"),
532};
533/* *INDENT-ON* */
534
535/*
536 * fd.io coding-style-patch-verification: ON
537 *
538 * Local Variables:
539 * eval: (c-set-style "gnu")
540 * End:
541 */