Debian Codegraph @

src/radv.c

raw
1 /* dnsmasq is Copyright (c) 2000-2026 Simon Kelley
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17
18 /* NB. This code may be called during a DHCPv4 or transaction which is in ping-wait
19 It therefore cannot use any DHCP buffer resources except outpacket, which is
20 not used by DHCPv4 code. This code may also be called when DHCP 4 or 6 isn't
21 active, so we ensure that outpacket is allocated here too */
22
23 #include "dnsmasq.h"
24
25 #ifdef HAVE_DHCP6
26
27 #include <netinet/icmp6.h>
28
29 struct ra_param {
30 time_t now;
31 int ind, managed, other, first, adv_router;
32 char *if_name;
33 struct dhcp_netid *tags;
34 struct in6_addr link_local, link_global, ula;
35 unsigned int glob_pref_time, link_pref_time, ula_pref_time, adv_interval, prio;
36 struct dhcp_context *found_context;
37 };
38
39 struct search_param {
40 time_t now; int iface;
41 char name[IF_NAMESIZE+1];
42 };
43
44 struct alias_param {
45 int iface;
46 struct dhcp_bridge *bridge;
47 int num_alias_ifs;
48 int max_alias_ifs;
49 int *alias_ifs;
50 };
51
52 static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest);
53 static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest,
54 int send_iface);
55 static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm);
56 static int add_prefixes(struct in6_addr *local, int prefix,
57 int scope, int if_index, int flags,
58 unsigned int preferred, unsigned int valid, void *vparam);
59 static int iface_search(struct in6_addr *local, int prefix,
60 int scope, int if_index, int flags,
61 unsigned int prefered, unsigned int valid, void *vparam);
62 static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
63 static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now);
64 static unsigned int calc_lifetime(struct ra_interface *ra);
65 static unsigned int calc_interval(struct ra_interface *ra);
66 static unsigned int calc_prio(struct ra_interface *ra);
67 static struct ra_interface *find_iface_param(char *iface);
68
69 static int hop_limit;
70
71 void ra_init(time_t now)
72 {
73 struct icmp6_filter filter;
74 int fd;
75 #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
76 int class = IPTOS_CLASS_CS6;
77 #endif
78 int val = 255; /* radvd uses this value */
79 socklen_t len = sizeof(int);
80 struct dhcp_context *context;
81
82 /* ensure this is around even if we're not doing DHCPv6 */
83 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
84
85 /* See if we're guessing SLAAC addresses, if so we need to receive ping replies */
86 for (context = daemon->dhcp6; context; context = context->next)
87 if ((context->flags & CONTEXT_RA_NAME))
88 break;
89
90 /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */
91
92 ICMP6_FILTER_SETBLOCKALL(&filter);
93 if (daemon->doing_ra)
94 {
95 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
96 if (context)
97 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
98 }
99
100 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
101 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
102 #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
103 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
104 #endif
105 !fix_fd(fd) ||
106 !set_ipv6pktinfo(fd) ||
107 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
108 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
109 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
110 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
111
112 daemon->icmp6fd = fd;
113
114 if (daemon->doing_ra)
115 ra_start_unsolicited(now, NULL);
116 }
117
118 void ra_start_unsolicited(time_t now, struct dhcp_context *context)
119 {
120 /* init timers so that we do ra's for some/all soon. some ra_times will end up zeroed
121 if it's not appropriate to advertise those contexts.
122 This gets re-called on a netlink route-change to re-do the advertisement
123 and pick up new interfaces */
124
125 if (context)
126 {
127 context->ra_short_period_start = now;
128 /* start after 1 second to get logging right at startup. */
129 context->ra_time = now + 1;
130 }
131 else
132 for (context = daemon->dhcp6; context; context = context->next)
133 if (!(context->flags & CONTEXT_TEMPLATE))
134 {
135 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
136 /* re-do frequently for a minute or so, in case the first gets lost. */
137 context->ra_short_period_start = now;
138 }
139 }
140
141 void icmp6_packet(time_t now)
142 {
143 char interface[IF_NAMESIZE+1];
144 ssize_t sz;
145 int if_index = 0;
146 struct cmsghdr *cmptr;
147 struct msghdr msg;
148 union {
149 struct cmsghdr align; /* this ensures alignment */
150 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
151 } control_u;
152 struct sockaddr_in6 from;
153 unsigned char *packet;
154 struct iname *tmp;
155
156 /* Note: use outpacket for input buffer */
157 msg.msg_control = control_u.control6;
158 msg.msg_controllen = sizeof(control_u);
159 msg.msg_flags = 0;
160 msg.msg_name = &from;
161 msg.msg_namelen = sizeof(from);
162 msg.msg_iov = &daemon->outpacket;
163 msg.msg_iovlen = 1;
164
165 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
166 return;
167
168 packet = (unsigned char *)daemon->outpacket.iov_base;
169
170 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
171 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
172 {
173 union {
174 unsigned char *c;
175 struct in6_pktinfo *p;
176 } p;
177 p.c = CMSG_DATA(cmptr);
178
179 if_index = p.p->ipi6_ifindex;
180 }
181
182 if (!indextoname(daemon->icmp6fd, if_index, interface))
183 return;
184
185 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
186 return;
187
188 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
189 if (tmp->name && (tmp->flags & INAME_6) &&
190 wildcard_match(tmp->name, interface))
191 return;
192
193 if (packet[1] != 0)
194 return;
195
196 if (packet[0] == ICMP6_ECHO_REPLY)
197 lease_ping_reply(&from.sin6_addr, packet, interface);
198 else if (packet[0] == ND_ROUTER_SOLICIT)
199 {
200 char *mac = "";
201 struct dhcp_bridge *bridge, *alias;
202 ssize_t rem;
203 unsigned char *p;
204 int opt_sz;
205
206 #ifdef HAVE_DUMPFILE
207 dump_packet_icmp(DUMP_RA, (void *)packet, sz, (union mysockaddr *)&from, NULL);
208 #endif
209
210 /* look for link-layer address option for logging */
211 for (rem = sz - 8, p = &packet[8]; rem >= 2; rem -= opt_sz, p += opt_sz)
212 {
213 opt_sz = p[1] * 8;
214
215 if (opt_sz == 0 || opt_sz > rem)
216 return; /* Bad packet */
217
218 if (p[0] == ICMP6_OPT_SOURCE_MAC)
219 mac = print_mac(&p[2], opt_sz - 2);
220 }
221
222 if (!option_bool(OPT_QUIET_RA))
223 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
224
225 /* If the incoming interface is an alias of some other one (as
226 specified by the --bridge-interface option), send an RA using
227 the context of the aliased interface. */
228 for (bridge = daemon->bridges; bridge; bridge = bridge->next)
229 {
230 int bridge_index = if_nametoindex(bridge->iface);
231 if (bridge_index)
232 {
233 for (alias = bridge->alias; alias; alias = alias->next)
234 if (wildcard_matchn(alias->iface, interface, IF_NAMESIZE))
235 {
236 /* Send an RA on if_index with information from
237 bridge_index. */
238 send_ra_alias(now, bridge_index, bridge->iface, NULL, if_index);
239 break;
240 }
241 if (alias)
242 break;
243 }
244 }
245
246 /* If the incoming interface wasn't an alias, send an RA using
247 the context of the incoming interface. */
248 if (!bridge)
249 /* source address may not be valid in solicit request. */
250 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
251 }
252 }
253
254 static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest, int send_iface)
255 {
256 struct ra_packet *ra;
257 struct ra_param parm;
258 struct sockaddr_in6 addr;
259 struct dhcp_context *context, *tmp, **up;
260 struct dhcp_netid iface_id;
261 struct dhcp_opt *opt_cfg;
262 struct ra_interface *ra_param = find_iface_param(iface_name);
263 int done_dns = 0, old_prefix = 0, mtu = 0;
264 unsigned int min_pref_time;
265 #ifdef HAVE_LINUX_NETWORK
266 FILE *f;
267 #endif
268
269 parm.ind = iface;
270 parm.managed = 0;
271 parm.other = 0;
272 parm.found_context = NULL;
273 parm.adv_router = 0;
274 parm.if_name = iface_name;
275 parm.first = 1;
276 parm.now = now;
277 parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0;
278 parm.adv_interval = calc_interval(ra_param);
279 parm.prio = calc_prio(ra_param);
280
281 reset_counter();
282
283 if (!(ra = expand(sizeof(struct ra_packet))))
284 return;
285
286 ra->type = ND_ROUTER_ADVERT;
287 ra->code = 0;
288 ra->hop_limit = hop_limit;
289 ra->flags = parm.prio;
290 ra->lifetime = htons(calc_lifetime(ra_param));
291 ra->reachable_time = 0;
292 ra->retrans_time = 0;
293
294 /* set tag with name == interface */
295 iface_id.net = iface_name;
296 iface_id.next = NULL;
297 parm.tags = &iface_id;
298
299 for (context = daemon->dhcp6; context; context = context->next)
300 {
301 context->flags &= ~CONTEXT_RA_DONE;
302 context->netid.next = &context->netid;
303 }
304
305 /* If no link-local address then we can't advertise since source address of
306 advertisement must be link local address: RFC 4861 para 6.1.2. */
307 if (!iface_enumerate(AF_INET6, &parm, (callback_t){.af_inet6=add_prefixes}) ||
308 parm.link_pref_time == 0)
309 return;
310
311 /* Find smallest preferred time within address classes,
312 to use as lifetime for options. This is a rather arbitrary choice. */
313 min_pref_time = 0xffffffff;
314 if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time)
315 min_pref_time = parm.glob_pref_time;
316
317 if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time)
318 min_pref_time = parm.ula_pref_time;
319
320 if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time)
321 min_pref_time = parm.link_pref_time;
322
323 /* Look for constructed contexts associated with addresses which have gone,
324 and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */
325 for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp)
326 {
327 tmp = context->next;
328
329 if (context->if_index == iface && (context->flags & CONTEXT_OLD))
330 {
331 unsigned int old = difftime(now, context->address_lost_time);
332
333 if (old > context->saved_valid)
334 {
335 /* We've advertised this enough, time to go */
336
337 /* If this context held the timeout, and there's another context in use
338 transfer the timeout there. */
339 if (context->ra_time != 0 && parm.found_context && parm.found_context->ra_time == 0)
340 new_timeout(parm.found_context, iface_name, now);
341
342 *up = context->next;
343 free(context);
344 }
345 else
346 {
347 struct prefix_opt *opt;
348 struct in6_addr local = context->start6;
349 int do_slaac = 0;
350
351 old_prefix = 1;
352
353 /* zero net part of address */
354 setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU));
355
356
357 if (context->flags & CONTEXT_RA)
358 {
359 do_slaac = 1;
360 if (context->flags & CONTEXT_DHCP)
361 {
362 parm.other = 1;
363 if (!(context->flags & CONTEXT_RA_STATELESS))
364 parm.managed = 1;
365 }
366 }
367 else
368 {
369 /* don't do RA for non-ra-only unless --enable-ra is set */
370 if (option_bool(OPT_RA))
371 {
372 parm.managed = 1;
373 parm.other = 1;
374 }
375 }
376
377 if ((opt = expand(sizeof(struct prefix_opt))))
378 {
379 opt->type = ICMP6_OPT_PREFIX;
380 opt->len = 4;
381 opt->prefix_len = context->prefix;
382 /* autonomous only if we're not doing dhcp, set
383 "on-link" unless "off-link" was specified */
384 opt->flags = (do_slaac ? 0x40 : 0) |
385 ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80);
386 opt->valid_lifetime = htonl(context->saved_valid - old);
387 opt->preferred_lifetime = htonl(0);
388 opt->reserved = 0;
389 opt->prefix = local;
390
391 inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN);
392 if (!option_bool(OPT_QUIET_RA))
393 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff);
394 }
395
396 up = &context->next;
397 }
398 }
399 else
400 up = &context->next;
401 }
402
403 /* If we're advertising only old prefixes, set router lifetime to zero. */
404 if (old_prefix && !parm.found_context)
405 ra->lifetime = htons(0);
406
407 /* No prefixes to advertise. */
408 if (!old_prefix && !parm.found_context)
409 return;
410
411 /* If we're sending router address instead of prefix in at least one prefix,
412 include the advertisement interval option. */
413 if (parm.adv_router)
414 {
415 put_opt6_char(ICMP6_OPT_ADV_INTERVAL);
416 put_opt6_char(1);
417 put_opt6_short(0);
418 /* interval value is in milliseconds */
419 put_opt6_long(1000 * calc_interval(find_iface_param(iface_name)));
420 }
421
422 /* Set the MTU from ra_param if any, an MTU of 0 mean automatic for linux, */
423 /* an MTU of -1 prevents the option from being sent. */
424 if (ra_param)
425 mtu = ra_param->mtu;
426 #ifdef HAVE_LINUX_NETWORK
427 /* Note that IPv6 MTU is not necessarily the same as the IPv4 MTU
428 available from SIOCGIFMTU */
429 if (mtu == 0)
430 {
431 char *mtu_name = ra_param ? ra_param->mtu_name : NULL;
432 sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", mtu_name ? mtu_name : iface_name);
433 if ((f = fopen(daemon->namebuff, "r")))
434 {
435 if (fgets(daemon->namebuff, MAXDNAMESTR, f))
436 mtu = atoi(daemon->namebuff);
437 fclose(f);
438 }
439 }
440 #endif
441 if (mtu > 0)
442 {
443 put_opt6_char(ICMP6_OPT_MTU);
444 put_opt6_char(1);
445 put_opt6_short(0);
446 put_opt6_long(mtu);
447 }
448
449 iface_enumerate(AF_LOCAL, &send_iface, (callback_t){.af_local=add_lla});
450
451 /* RDNSS, RFC 6106, use relevant DHCP6 options */
452 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6, 0);
453
454 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
455 {
456 int i;
457
458 /* netids match and not encapsulated? */
459 if (!(opt_cfg->flags & DHOPT_TAGOK))
460 continue;
461
462 if (opt_cfg->opt == OPTION6_DNS_SERVER)
463 {
464 struct in6_addr *a;
465 int len;
466
467 done_dns = 1;
468
469 if (opt_cfg->len == 0)
470 continue;
471
472 /* reduce len for any addresses we can't substitute */
473 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0;
474 i < opt_cfg->len; i += IN6ADDRSZ, a++)
475 if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) ||
476 (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) ||
477 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0))
478 len -= IN6ADDRSZ;
479
480 if (len != 0)
481 {
482 put_opt6_char(ICMP6_OPT_RDNSS);
483 put_opt6_char((len/8) + 1);
484 put_opt6_short(0);
485 put_opt6_long(min_pref_time);
486
487 for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
488 if (IN6_IS_ADDR_UNSPECIFIED(a))
489 {
490 if (parm.glob_pref_time != 0)
491 put_opt6(&parm.link_global, IN6ADDRSZ);
492 }
493 else if (IN6_IS_ADDR_ULA_ZERO(a))
494 {
495 if (parm.ula_pref_time != 0)
496 put_opt6(&parm.ula, IN6ADDRSZ);
497 }
498 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
499 {
500 if (parm.link_pref_time != 0)
501 put_opt6(&parm.link_local, IN6ADDRSZ);
502 }
503 else
504 put_opt6(a, IN6ADDRSZ);
505 }
506 }
507
508 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
509 {
510 int len = ((opt_cfg->len+7)/8);
511
512 put_opt6_char(ICMP6_OPT_DNSSL);
513 put_opt6_char(len + 1);
514 put_opt6_short(0);
515 put_opt6_long(min_pref_time);
516 put_opt6(opt_cfg->val, opt_cfg->len);
517
518 /* pad */
519 for (i = opt_cfg->len; i < len * 8; i++)
520 put_opt6_char(0);
521 }
522 }
523
524 if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0)
525 {
526 /* default == us, as long as we are supplying DNS service. */
527 put_opt6_char(ICMP6_OPT_RDNSS);
528 put_opt6_char(3);
529 put_opt6_short(0);
530 put_opt6_long(min_pref_time);
531 put_opt6(&parm.link_local, IN6ADDRSZ);
532 }
533
534 /* set managed bits unless we're providing only RA on this link */
535 if (parm.managed)
536 ra->flags |= 0x80; /* M flag, managed, */
537 if (parm.other)
538 ra->flags |= 0x40; /* O flag, other */
539
540 /* decide where we're sending */
541 memset(&addr, 0, sizeof(addr));
542 #ifdef HAVE_SOCKADDR_SA_LEN
543 addr.sin6_len = sizeof(struct sockaddr_in6);
544 #endif
545 addr.sin6_family = AF_INET6;
546 addr.sin6_port = htons(IPPROTO_ICMPV6);
547 if (dest)
548 {
549 addr.sin6_addr = *dest;
550 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
551 IN6_IS_ADDR_MC_LINKLOCAL(dest))
552 addr.sin6_scope_id = iface;
553 }
554 else
555 {
556 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
557 setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &send_iface, sizeof(send_iface));
558 }
559
560 #ifdef HAVE_DUMPFILE
561 {
562 struct sockaddr_in6 src;
563 src.sin6_family = AF_INET6;
564 src.sin6_addr = parm.link_local;
565
566 dump_packet_icmp(DUMP_RA, (void *)daemon->outpacket.iov_base, save_counter(-1), (union mysockaddr *)&src, (union mysockaddr *)&addr);
567 }
568 #endif
569
570 while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base,
571 save_counter(-1), 0, (struct sockaddr *)&addr,
572 sizeof(addr))));
573
574 }
575
576 static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
577 {
578 /* Send an RA on the same interface that the RA content is based
579 on. */
580 send_ra_alias(now, iface, iface_name, dest, iface);
581 }
582
583 static int add_prefixes(struct in6_addr *local, int prefix,
584 int scope, int if_index, int flags,
585 unsigned int preferred, unsigned int valid, void *vparam)
586 {
587 struct ra_param *param = vparam;
588
589 (void)scope; /* warning */
590
591 if (if_index == param->ind)
592 {
593 if (IN6_IS_ADDR_LINKLOCAL(local))
594 {
595 /* Can there be more than one LL address?
596 Select the one with the longest preferred time
597 if there is. */
598 if (preferred > param->link_pref_time)
599 {
600 param->link_pref_time = preferred;
601 param->link_local = *local;
602 }
603 }
604 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
605 !IN6_IS_ADDR_MULTICAST(local))
606 {
607 int real_prefix = 0;
608 int do_slaac = 0;
609 int deprecate = 0;
610 int constructed = 0;
611 int adv_router = 0;
612 int off_link = 0;
613 unsigned int time = 0xffffffff;
614 struct dhcp_context *context;
615
616 for (context = daemon->dhcp6; context; context = context->next)
617 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
618 prefix <= context->prefix &&
619 is_same_net6(local, &context->start6, context->prefix) &&
620 is_same_net6(local, &context->end6, context->prefix))
621 {
622 context->saved_valid = valid;
623
624 if (context->flags & CONTEXT_RA)
625 {
626 do_slaac = 1;
627 if (context->flags & CONTEXT_DHCP)
628 {
629 param->other = 1;
630 if (!(context->flags & CONTEXT_RA_STATELESS))
631 param->managed = 1;
632 }
633 }
634 else
635 {
636 /* don't do RA for non-ra-only unless --enable-ra is set */
637 if (!option_bool(OPT_RA))
638 continue;
639 param->managed = 1;
640 param->other = 1;
641 }
642
643 /* Configured to advertise router address, not prefix. See RFC 3775 7.2
644 In this case we do all addresses associated with a context,
645 hence the real_prefix setting here. */
646 if (context->flags & CONTEXT_RA_ROUTER)
647 {
648 adv_router = 1;
649 param->adv_router = 1;
650 real_prefix = context->prefix;
651 }
652
653 /* find floor time, don't reduce below 3 * RA interval.
654 If the lease time has been left as default, don't
655 use that as a floor. */
656 if ((context->flags & CONTEXT_SETLEASE) &&
657 time > context->lease_time)
658 {
659 time = context->lease_time;
660 if (time < ((unsigned int)(3 * param->adv_interval)))
661 time = 3 * param->adv_interval;
662 }
663
664 if (context->flags & CONTEXT_DEPRECATE)
665 deprecate = 1;
666
667 if (context->flags & CONTEXT_CONSTRUCTED)
668 constructed = 1;
669
670
671 /* collect dhcp-range tags */
672 if (context->netid.next == &context->netid && context->netid.net)
673 {
674 context->netid.next = param->tags;
675 param->tags = &context->netid;
676 }
677
678 /* subsequent prefixes on the same interface
679 and subsequent instances of this prefix don't need timers.
680 Be careful not to find the same prefix twice with different
681 addresses unless we're advertising the actual addresses. */
682 if (!(context->flags & CONTEXT_RA_DONE))
683 {
684 if (!param->first)
685 context->ra_time = 0;
686 context->flags |= CONTEXT_RA_DONE;
687 real_prefix = context->prefix;
688 off_link = (context->flags & CONTEXT_RA_OFF_LINK);
689 }
690
691 param->first = 0;
692 /* found_context is the _last_ one we found, so if there's
693 more than one, it's not the first. */
694 param->found_context = context;
695 }
696
697 /* configured time is ceiling */
698 if (!constructed || valid > time)
699 valid = time;
700
701 if (flags & IFACE_DEPRECATED)
702 preferred = 0;
703
704 if (deprecate)
705 time = 0;
706
707 /* configured time is ceiling */
708 if (!constructed || preferred > time)
709 preferred = time;
710
711 if (IN6_IS_ADDR_ULA(local))
712 {
713 if (preferred > param->ula_pref_time)
714 {
715 param->ula_pref_time = preferred;
716 param->ula = *local;
717 }
718 }
719 else
720 {
721 if (preferred > param->glob_pref_time)
722 {
723 param->glob_pref_time = preferred;
724 param->link_global = *local;
725 }
726 }
727
728 if (real_prefix != 0)
729 {
730 struct prefix_opt *opt;
731
732 if ((opt = expand(sizeof(struct prefix_opt))))
733 {
734 /* zero net part of address */
735 if (!adv_router)
736 setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU));
737
738 opt->type = ICMP6_OPT_PREFIX;
739 opt->len = 4;
740 opt->prefix_len = real_prefix;
741 /* autonomous only if we're not doing dhcp, set
742 "on-link" unless "off-link" was specified */
743 opt->flags = (off_link ? 0 : 0x80);
744 if (do_slaac)
745 opt->flags |= 0x40;
746 if (adv_router)
747 opt->flags |= 0x20;
748 opt->valid_lifetime = htonl(valid);
749 opt->preferred_lifetime = htonl(preferred);
750 opt->reserved = 0;
751 opt->prefix = *local;
752
753 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
754 if (!option_bool(OPT_QUIET_RA))
755 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
756 }
757 }
758 }
759 }
760 return 1;
761 }
762
763 static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
764 {
765 (void)type;
766
767 if (index == *((int *)parm))
768 {
769 /* size is in units of 8 octets and includes type and length (2 bytes)
770 add 7 to round up */
771 int len = (maclen + 9) >> 3;
772 unsigned char *p = expand(len << 3);
773 if (!p)
774 return 1;
775 memset(p, 0, len << 3);
776 *p++ = ICMP6_OPT_SOURCE_MAC;
777 *p++ = len;
778 memcpy(p, mac, maclen);
779
780 return 0;
781 }
782
783 return 1;
784 }
785
786 time_t periodic_ra(time_t now)
787 {
788 struct search_param param;
789 struct dhcp_context *context;
790 time_t next_event;
791 struct alias_param aparam;
792
793 param.now = now;
794 param.iface = 0;
795
796 while (1)
797 {
798 /* find overdue events, and time of first future event */
799 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
800 if (context->ra_time != 0)
801 {
802 if (difftime(context->ra_time, now) <= 0.0)
803 break; /* overdue */
804
805 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
806 next_event = context->ra_time;
807 }
808
809 /* none overdue */
810 if (!context)
811 break;
812
813 if ((context->flags & CONTEXT_OLD) &&
814 context->if_index != 0 &&
815 indextoname(daemon->icmp6fd, context->if_index, param.name))
816 {
817 /* A context for an old address. We'll not find the interface by
818 looking for addresses, but we know it anyway, since the context is
819 constructed */
820 param.iface = context->if_index;
821 new_timeout(context, param.name, now);
822 }
823 else if (iface_enumerate(AF_INET6, &param, (callback_t){.af_inet6=iface_search}))
824 /* There's a context overdue, but we can't find an interface
825 associated with it, because it's for a subnet we don't
826 have an interface on. Probably we're doing DHCP on
827 a remote subnet via a relay. Zero the timer, since we won't
828 ever be able to send RAs to satisfy it. */
829 context->ra_time = 0;
830
831 if (param.iface != 0 &&
832 iface_check(AF_LOCAL, NULL, param.name, NULL))
833 {
834 struct iname *tmp;
835 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
836 if (tmp->name && (tmp->flags & INAME_6) &&
837 wildcard_match(tmp->name, param.name))
838 break;
839 if (!tmp)
840 {
841 send_ra(now, param.iface, param.name, NULL);
842
843 /* Also send on all interfaces that are aliases of this
844 one. */
845 for (aparam.bridge = daemon->bridges;
846 aparam.bridge;
847 aparam.bridge = aparam.bridge->next)
848 if ((int)if_nametoindex(aparam.bridge->iface) == param.iface)
849 {
850 /* Count the number of alias interfaces for this
851 'bridge', by calling iface_enumerate with
852 send_ra_to_aliases and NULL alias_ifs. */
853 aparam.iface = param.iface;
854 aparam.alias_ifs = NULL;
855 aparam.num_alias_ifs = 0;
856 iface_enumerate(AF_LOCAL, &aparam, (callback_t){.af_local=send_ra_to_aliases});
857 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => %d alias(es)",
858 param.name, daemon->addrbuff, aparam.num_alias_ifs);
859
860 /* Allocate memory to store the alias interface
861 indices. */
862 aparam.alias_ifs = (int *)whine_malloc(aparam.num_alias_ifs *
863 sizeof(int));
864 if (aparam.alias_ifs)
865 {
866 /* Use iface_enumerate again to get the alias
867 interface indices, then send on each of
868 those. */
869 aparam.max_alias_ifs = aparam.num_alias_ifs;
870 aparam.num_alias_ifs = 0;
871 iface_enumerate(AF_LOCAL, &aparam, (callback_t){.af_local=send_ra_to_aliases});
872 for (; aparam.num_alias_ifs; aparam.num_alias_ifs--)
873 {
874 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => i/f %d",
875 param.name, daemon->addrbuff,
876 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
877 send_ra_alias(now,
878 param.iface,
879 param.name,
880 NULL,
881 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
882 }
883 free(aparam.alias_ifs);
884 }
885
886 /* The source interface can only appear in at most
887 one --bridge-interface. */
888 break;
889 }
890 }
891 }
892 }
893 return next_event;
894 }
895
896 static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm)
897 {
898 struct alias_param *aparam = (struct alias_param *)parm;
899 char ifrn_name[IFNAMSIZ];
900 struct dhcp_bridge *alias;
901
902 (void)type;
903 (void)mac;
904 (void)maclen;
905
906 if (if_indextoname(index, ifrn_name))
907 for (alias = aparam->bridge->alias; alias; alias = alias->next)
908 if (wildcard_matchn(alias->iface, ifrn_name, IFNAMSIZ))
909 {
910 if (aparam->alias_ifs && (aparam->num_alias_ifs < aparam->max_alias_ifs))
911 aparam->alias_ifs[aparam->num_alias_ifs] = index;
912 aparam->num_alias_ifs++;
913 }
914
915 return 1;
916 }
917
918 static int iface_search(struct in6_addr *local, int prefix,
919 int scope, int if_index, int flags,
920 unsigned int preferred, unsigned int valid, void *vparam)
921 {
922 struct search_param *param = vparam;
923 struct dhcp_context *context;
924 struct iname *tmp;
925
926 (void)scope;
927 (void)preferred;
928 (void)valid;
929
930 /* ignore interfaces we're not doing DHCP on. */
931 if (!indextoname(daemon->icmp6fd, if_index, param->name) ||
932 !iface_check(AF_LOCAL, NULL, param->name, NULL))
933 return 1;
934
935 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
936 if (tmp->name && (tmp->flags & INAME_6) &&
937 wildcard_match(tmp->name, param->name))
938 return 1;
939
940 for (context = daemon->dhcp6; context; context = context->next)
941 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
942 prefix <= context->prefix &&
943 is_same_net6(local, &context->start6, context->prefix) &&
944 is_same_net6(local, &context->end6, context->prefix) &&
945 context->ra_time != 0 &&
946 difftime(context->ra_time, param->now) <= 0.0)
947 {
948 /* found an interface that's overdue for RA determine new
949 timeout value and arrange for RA to be sent unless interface is
950 still doing DAD.*/
951 if (!(flags & IFACE_TENTATIVE))
952 param->iface = if_index;
953
954 new_timeout(context, param->name, param->now);
955
956 /* zero timers for other contexts on the same subnet, so they don't timeout
957 independently */
958 for (context = context->next; context; context = context->next)
959 if (prefix <= context->prefix &&
960 is_same_net6(local, &context->start6, context->prefix) &&
961 is_same_net6(local, &context->end6, context->prefix))
962 context->ra_time = 0;
963
964 return 0; /* found, abort */
965 }
966
967 return 1; /* keep searching */
968 }
969
970 static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now)
971 {
972 if (difftime(now, context->ra_short_period_start) < 60.0)
973 /* range 5 - 20 */
974 context->ra_time = now + 5 + (rand16()/4400);
975 else
976 {
977 /* range 3/4 - 1 times MaxRtrAdvInterval */
978 unsigned int adv_interval = calc_interval(find_iface_param(iface_name));
979 context->ra_time = now + (3 * adv_interval)/4 + ((adv_interval * (unsigned int)rand16()) >> 18);
980 }
981 }
982
983 static struct ra_interface *find_iface_param(char *iface)
984 {
985 struct ra_interface *ra;
986
987 for (ra = daemon->ra_interfaces; ra; ra = ra->next)
988 if (wildcard_match(ra->name, iface))
989 return ra;
990
991 return NULL;
992 }
993
994 static unsigned int calc_interval(struct ra_interface *ra)
995 {
996 int interval = 600;
997
998 if (ra && ra->interval != 0)
999 {
1000 interval = ra->interval;
1001 if (interval > 1800)
1002 interval = 1800;
1003 else if (interval < 4)
1004 interval = 4;
1005 }
1006
1007 return (unsigned int)interval;
1008 }
1009
1010 static unsigned int calc_lifetime(struct ra_interface *ra)
1011 {
1012 int lifetime, interval = (int)calc_interval(ra);
1013
1014 if (!ra || ra->lifetime == -1) /* not specified */
1015 lifetime = 3 * interval;
1016 else
1017 {
1018 lifetime = ra->lifetime;
1019 if (lifetime < interval && lifetime != 0)
1020 lifetime = interval;
1021 else if (lifetime > 9000)
1022 lifetime = 9000;
1023 }
1024
1025 return (unsigned int)lifetime;
1026 }
1027
1028 static unsigned int calc_prio(struct ra_interface *ra)
1029 {
1030 if (ra)
1031 return ra->prio;
1032
1033 return 0;
1034 }
1035
1036 #endif