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