blob: 70211c5436d1534cff2975fbe67e96d9af783d5b [file] [log] [blame]
Simon Kelleyc49778d2016-01-06 18:52:33 +00001/* dnsmasq is Copyright (c) 2000-2016 Simon Kelley
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00002
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
Simon Kelley843c96b2012-02-27 17:42:38 +000018/* NB. This code may be called during a DHCPv4 or transaction which is in ping-wait
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000019 It therefore cannot use any DHCP buffer resources except outpacket, which is
Simon Kelley843c96b2012-02-27 17:42:38 +000020 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 */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000022
23#include "dnsmasq.h"
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000024
25#ifdef HAVE_DHCP6
26
Simon Kelley22d904d2012-02-26 20:13:45 +000027#include <netinet/icmp6.h>
28
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000029struct ra_param {
Simon Kelley1f776932012-12-16 19:46:08 +000030 time_t now;
S La18bf312016-02-12 17:36:20 +000031 int ind, managed, other, first, adv_router;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000032 char *if_name;
Simon Kelley18f0fb02012-03-31 21:18:55 +010033 struct dhcp_netid *tags;
Simon Kelleyc3a04082014-01-11 22:18:19 +000034 struct in6_addr link_local, link_global, ula;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +010035 unsigned int glob_pref_time, link_pref_time, ula_pref_time, adv_interval, prio;
S La18bf312016-02-12 17:36:20 +000036 struct dhcp_context *found_context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000037};
38
39struct search_param {
40 time_t now; int iface;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +010041 char name[IF_NAMESIZE+1];
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000042};
43
Neil Jerram9bad3392015-06-10 22:16:35 +010044struct 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
Simon Kelley1f776932012-12-16 19:46:08 +000052static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest);
Neil Jerramba4fc0f2015-06-10 22:14:49 +010053static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest,
54 int send_iface);
Neil Jerram9bad3392015-06-10 22:16:35 +010055static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000056static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +000057 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +000058 unsigned int preferred, unsigned int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000059static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +000060 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +000061 int prefered, int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000062static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
Simon Kelleyc4cd95d2013-10-10 20:58:11 +010063static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now);
64static unsigned int calc_lifetime(struct ra_interface *ra);
65static unsigned int calc_interval(struct ra_interface *ra);
66static unsigned int calc_prio(struct ra_interface *ra);
67static struct ra_interface *find_iface_param(char *iface);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000068
Simon Kelleyc5379c12012-02-24 20:05:52 +000069static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000070
71void ra_init(time_t now)
72{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000073 struct icmp6_filter filter;
74 int fd;
Simon Kelley0e88d532012-03-28 22:22:05 +010075#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000076 int class = IPTOS_CLASS_CS6;
77#endif
78 int val = 255; /* radvd uses this value */
Simon Kelley7b6dd882012-03-01 10:26:16 +000079 socklen_t len = sizeof(int);
Simon Kelley353ae4d2012-03-19 20:07:51 +000080 struct dhcp_context *context;
81
Simon Kelley843c96b2012-02-27 17:42:38 +000082 /* ensure this is around even if we're not doing DHCPv6 */
83 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000084
Josh Soref730c6742017-02-06 16:14:04 +000085 /* See if we're guessing SLAAC addresses, if so we need to receive ping replies */
Simon Kelley1f776932012-12-16 19:46:08 +000086 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley353ae4d2012-03-19 20:07:51 +000087 if ((context->flags & CONTEXT_RA_NAME))
88 break;
89
Simon Kelley89500e32013-09-20 16:29:20 +010090 /* Need ICMP6 socket for transmission for DHCPv6 even when not doing RA. */
91
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000092 ICMP6_FILTER_SETBLOCKALL(&filter);
Simon Kelley89500e32013-09-20 16:29:20 +010093 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 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000099
100 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +0000101 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelley0e88d532012-03-28 22:22:05 +0100102#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000103 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
Simon Kelley89500e32013-09-20 16:29:20 +0100114 if (daemon->doing_ra)
Josh Soref730c6742017-02-06 16:14:04 +0000115 ra_start_unsolicited(now, NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000116}
117
Josh Soref730c6742017-02-06 16:14:04 +0000118void ra_start_unsolicited(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000119{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000120 /* init timers so that we do ra's for some/all soon. some ra_times will end up zeroed
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000121 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 */
Simon Kelley6e3dba32012-12-16 21:52:45 +0000124
Simon Kelley353ae4d2012-03-19 20:07:51 +0000125 if (context)
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000126 context->ra_short_period_start = context->ra_time = now;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000127 else
Simon Kelley1f776932012-12-16 19:46:08 +0000128 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000129 if (!(context->flags & CONTEXT_TEMPLATE))
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000130 {
131 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
132 /* re-do frequently for a minute or so, in case the first gets lost. */
133 context->ra_short_period_start = now;
134 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000135}
136
Simon Kelley1f776932012-12-16 19:46:08 +0000137void icmp6_packet(time_t now)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000138{
139 char interface[IF_NAMESIZE+1];
140 ssize_t sz;
141 int if_index = 0;
142 struct cmsghdr *cmptr;
143 struct msghdr msg;
144 union {
145 struct cmsghdr align; /* this ensures alignment */
146 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
147 } control_u;
148 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100149 unsigned char *packet;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000150 struct iname *tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000151
152 /* Note: use outpacket for input buffer */
153 msg.msg_control = control_u.control6;
154 msg.msg_controllen = sizeof(control_u);
155 msg.msg_flags = 0;
156 msg.msg_name = &from;
157 msg.msg_namelen = sizeof(from);
158 msg.msg_iov = &daemon->outpacket;
159 msg.msg_iovlen = 1;
160
161 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
162 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100163
164 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000165
166 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
167 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
168 {
169 union {
170 unsigned char *c;
171 struct in6_pktinfo *p;
172 } p;
173 p.c = CMSG_DATA(cmptr);
174
175 if_index = p.p->ipi6_ifindex;
176 }
177
178 if (!indextoname(daemon->icmp6fd, if_index, interface))
179 return;
180
Simon Kelley4f7b3042012-11-28 21:27:02 +0000181 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000182 return;
183
184 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelley49333cb2013-03-15 20:30:51 +0000185 if (tmp->name && wildcard_match(tmp->name, interface))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000186 return;
187
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100188 if (packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000189 return;
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100190
Simon Kelley5ef33272012-03-30 15:10:28 +0100191 if (packet[0] == ICMP6_ECHO_REPLY)
192 lease_ping_reply(&from.sin6_addr, packet, interface);
193 else if (packet[0] == ND_ROUTER_SOLICIT)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000194 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100195 char *mac = "";
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100196 struct dhcp_bridge *bridge, *alias;
Simon Kelley5ef33272012-03-30 15:10:28 +0100197
198 /* look for link-layer address option for logging */
199 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
200 {
201 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
202 mac = daemon->namebuff;
203 }
204
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100205 if (!option_bool(OPT_QUIET_RA))
206 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100207
208 /* If the incoming interface is an alias of some other one (as
Neil Jerram4918bd52015-06-10 22:23:20 +0100209 specified by the --bridge-interface option), send an RA using
210 the context of the aliased interface. */
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100211 for (bridge = daemon->bridges; bridge; bridge = bridge->next)
212 {
213 int bridge_index = if_nametoindex(bridge->iface);
214 if (bridge_index)
215 {
216 for (alias = bridge->alias; alias; alias = alias->next)
217 if (wildcard_matchn(alias->iface, interface, IF_NAMESIZE))
218 {
219 /* Send an RA on if_index with information from
220 bridge_index. */
221 send_ra_alias(now, bridge_index, bridge->iface, NULL, if_index);
222 break;
223 }
224 if (alias)
225 break;
226 }
227 }
228
229 /* If the incoming interface wasn't an alias, send an RA using
230 the context of the incoming interface. */
231 if (!bridge)
232 /* source address may not be valid in solicit request. */
233 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000234 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000235}
236
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100237static void send_ra_alias(time_t now, int iface, char *iface_name, struct in6_addr *dest, int send_iface)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000238{
239 struct ra_packet *ra;
240 struct ra_param parm;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000241 struct sockaddr_in6 addr;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100242 struct dhcp_context *context, *tmp, **up;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100243 struct dhcp_netid iface_id;
244 struct dhcp_opt *opt_cfg;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100245 struct ra_interface *ra_param = find_iface_param(iface_name);
David Flamand005c46d2017-04-11 11:49:54 +0100246 int done_dns = 0, old_prefix = 0, mtu = 0;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000247 unsigned int min_pref_time;
Simon Kelley3b436462012-12-28 11:55:45 +0000248#ifdef HAVE_LINUX_NETWORK
249 FILE *f;
250#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000251
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000252 parm.ind = iface;
253 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100254 parm.other = 0;
S La18bf312016-02-12 17:36:20 +0000255 parm.found_context = NULL;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100256 parm.adv_router = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000257 parm.if_name = iface_name;
258 parm.first = 1;
Simon Kelley1f776932012-12-16 19:46:08 +0000259 parm.now = now;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000260 parm.glob_pref_time = parm.link_pref_time = parm.ula_pref_time = 0;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100261 parm.adv_interval = calc_interval(ra_param);
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100262 parm.prio = calc_prio(ra_param);
Simon Kelley1f776932012-12-16 19:46:08 +0000263
Simon Kelleyfa785732016-07-22 20:56:01 +0100264 reset_counter();
Simon Kelleyce7845b2016-07-06 21:42:27 +0100265
266 if (!(ra = expand(sizeof(struct ra_packet))))
267 return;
Ilya Ponetaev5bf50af2014-09-09 12:46:21 +0100268
269 ra->type = ND_ROUTER_ADVERT;
270 ra->code = 0;
271 ra->hop_limit = hop_limit;
272 ra->flags = parm.prio;
273 ra->lifetime = htons(calc_lifetime(ra_param));
274 ra->reachable_time = 0;
275 ra->retrans_time = 0;
276
Simon Kelley18f0fb02012-03-31 21:18:55 +0100277 /* set tag with name == interface */
278 iface_id.net = iface_name;
279 iface_id.next = NULL;
280 parm.tags = &iface_id;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000281
Simon Kelley1f776932012-12-16 19:46:08 +0000282 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100283 {
284 context->flags &= ~CONTEXT_RA_DONE;
285 context->netid.next = &context->netid;
286 }
287
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100288 if (!iface_enumerate(AF_INET6, &parm, add_prefixes))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000289 return;
290
Simon Kelleyc3a04082014-01-11 22:18:19 +0000291 /* Find smallest preferred time within address classes,
292 to use as lifetime for options. This is a rather arbitrary choice. */
293 min_pref_time = 0xffffffff;
294 if (parm.glob_pref_time != 0 && parm.glob_pref_time < min_pref_time)
295 min_pref_time = parm.glob_pref_time;
296
297 if (parm.ula_pref_time != 0 && parm.ula_pref_time < min_pref_time)
298 min_pref_time = parm.ula_pref_time;
299
300 if (parm.link_pref_time != 0 && parm.link_pref_time < min_pref_time)
301 min_pref_time = parm.link_pref_time;
302
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100303 /* Look for constructed contexts associated with addresses which have gone,
304 and advertise them with preferred_time == 0 RFC 6204 4.3 L-13 */
305 for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp)
306 {
307 tmp = context->next;
Simon Kelley3b436462012-12-28 11:55:45 +0000308
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100309 if (context->if_index == iface && (context->flags & CONTEXT_OLD))
310 {
311 unsigned int old = difftime(now, context->address_lost_time);
312
313 if (old > context->saved_valid)
S La18bf312016-02-12 17:36:20 +0000314 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100315 /* We've advertised this enough, time to go */
S La18bf312016-02-12 17:36:20 +0000316
317 /* If this context held the timeout, and there's another context in use
318 transfer the timeout there. */
319 if (context->ra_time != 0 && parm.found_context && parm.found_context->ra_time == 0)
320 new_timeout(parm.found_context, iface_name, now);
321
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100322 *up = context->next;
323 free(context);
324 }
325 else
326 {
327 struct prefix_opt *opt;
328 struct in6_addr local = context->start6;
329 int do_slaac = 0;
330
Simon Kelley44522922013-11-15 14:45:04 +0000331 old_prefix = 1;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100332
333 /* zero net part of address */
334 setaddr6part(&local, addr6part(&local) & ~((context->prefix == 64) ? (u64)-1LL : (1LLU << (128 - context->prefix)) - 1LLU));
335
Simon Kelley6ea1f232013-12-19 15:45:12 +0000336
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100337 if (context->flags & CONTEXT_RA)
Simon Kelley6ea1f232013-12-19 15:45:12 +0000338 {
339 do_slaac = 1;
340 if (context->flags & CONTEXT_DHCP)
341 {
342 parm.other = 1;
343 if (!(context->flags & CONTEXT_RA_STATELESS))
344 parm.managed = 1;
345 }
346 }
347 else
348 {
349 /* don't do RA for non-ra-only unless --enable-ra is set */
350 if (option_bool(OPT_RA))
351 {
352 parm.managed = 1;
353 parm.other = 1;
354 }
355 }
356
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100357 if ((opt = expand(sizeof(struct prefix_opt))))
358 {
359 opt->type = ICMP6_OPT_PREFIX;
360 opt->len = 4;
361 opt->prefix_len = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100362 /* autonomous only if we're not doing dhcp, set
363 "on-link" unless "off-link" was specified */
364 opt->flags = (do_slaac ? 0x40 : 0) |
365 ((context->flags & CONTEXT_RA_OFF_LINK) ? 0 : 0x80);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100366 opt->valid_lifetime = htonl(context->saved_valid - old);
367 opt->preferred_lifetime = htonl(0);
368 opt->reserved = 0;
369 opt->prefix = local;
370
371 inet_ntop(AF_INET6, &local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100372 if (!option_bool(OPT_QUIET_RA))
373 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s old prefix", iface_name, daemon->addrbuff);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100374 }
375
376 up = &context->next;
377 }
378 }
379 else
380 up = &context->next;
381 }
382
Simon Kelley44522922013-11-15 14:45:04 +0000383 /* If we're advertising only old prefixes, set router lifetime to zero. */
384 if (old_prefix && !parm.found_context)
385 ra->lifetime = htons(0);
386
387 /* No prefixes to advertise. */
388 if (!old_prefix && !parm.found_context)
389 return;
390
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100391 /* If we're sending router address instead of prefix in at least on prefix,
392 include the advertisement interval option. */
393 if (parm.adv_router)
394 {
395 put_opt6_char(ICMP6_OPT_ADV_INTERVAL);
396 put_opt6_char(1);
397 put_opt6_short(0);
398 /* interval value is in milliseconds */
399 put_opt6_long(1000 * calc_interval(find_iface_param(iface_name)));
400 }
401
David Flamand005c46d2017-04-11 11:49:54 +0100402 /* Set the MTU from ra_param if any, an MTU of 0 mean automatic for linux, */
403 /* an MTU of -1 prevents the option from being sent. */
404 if (ra_param)
405 mtu = ra_param->mtu;
Simon Kelley3b436462012-12-28 11:55:45 +0000406#ifdef HAVE_LINUX_NETWORK
Josh Soref730c6742017-02-06 16:14:04 +0000407 /* Note that IPv6 MTU is not neccessarily the same as the IPv4 MTU
Simon Kelley3b436462012-12-28 11:55:45 +0000408 available from SIOCGIFMTU */
David Flamand005c46d2017-04-11 11:49:54 +0100409 if (mtu == 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000410 {
David Flamand005c46d2017-04-11 11:49:54 +0100411 sprintf(daemon->namebuff, "/proc/sys/net/ipv6/conf/%s/mtu", iface_name);
412 if ((f = fopen(daemon->namebuff, "r")))
413 {
414 if (fgets(daemon->namebuff, MAXDNAME, f))
415 mtu = atoi(daemon->namebuff);
416 fclose(f);
417 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000418 }
Simon Kelley3b436462012-12-28 11:55:45 +0000419#endif
David Flamand005c46d2017-04-11 11:49:54 +0100420 if (mtu > 0)
421 {
422 put_opt6_char(ICMP6_OPT_MTU);
423 put_opt6_char(1);
424 put_opt6_short(0);
425 put_opt6_long(mtu);
426 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000427
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100428 iface_enumerate(AF_LOCAL, &send_iface, add_lla);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100429
430 /* RDNSS, RFC 6106, use relevant DHCP6 options */
431 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000432
Simon Kelley18f0fb02012-03-31 21:18:55 +0100433 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
434 {
435 int i;
436
437 /* netids match and not encapsulated? */
438 if (!(opt_cfg->flags & DHOPT_TAGOK))
439 continue;
440
441 if (opt_cfg->opt == OPTION6_DNS_SERVER)
442 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000443 struct in6_addr *a;
444 int len;
445
Simon Kelley18f0fb02012-03-31 21:18:55 +0100446 done_dns = 1;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000447
448 if (opt_cfg->len == 0)
Simon Kelley806cf782013-10-14 14:08:44 +0100449 continue;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100450
Simon Kelleyc3a04082014-01-11 22:18:19 +0000451 /* reduce len for any addresses we can't substitute */
452 for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, i = 0;
453 i < opt_cfg->len; i += IN6ADDRSZ, a++)
454 if ((IN6_IS_ADDR_UNSPECIFIED(a) && parm.glob_pref_time == 0) ||
455 (IN6_IS_ADDR_ULA_ZERO(a) && parm.ula_pref_time == 0) ||
456 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && parm.link_pref_time == 0))
457 len -= IN6ADDRSZ;
458
459 if (len != 0)
460 {
461 put_opt6_char(ICMP6_OPT_RDNSS);
462 put_opt6_char((len/8) + 1);
463 put_opt6_short(0);
464 put_opt6_long(min_pref_time);
465
466 for (a = (struct in6_addr *)opt_cfg->val, i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
467 if (IN6_IS_ADDR_UNSPECIFIED(a))
468 {
469 if (parm.glob_pref_time != 0)
470 put_opt6(&parm.link_global, IN6ADDRSZ);
471 }
472 else if (IN6_IS_ADDR_ULA_ZERO(a))
473 {
474 if (parm.ula_pref_time != 0)
475 put_opt6(&parm.ula, IN6ADDRSZ);
476 }
477 else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
478 {
479 if (parm.link_pref_time != 0)
480 put_opt6(&parm.link_local, IN6ADDRSZ);
481 }
482 else
483 put_opt6(a, IN6ADDRSZ);
484 }
Simon Kelley18f0fb02012-03-31 21:18:55 +0100485 }
486
487 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
488 {
489 int len = ((opt_cfg->len+7)/8);
490
491 put_opt6_char(ICMP6_OPT_DNSSL);
492 put_opt6_char(len + 1);
493 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000494 put_opt6_long(min_pref_time);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100495 put_opt6(opt_cfg->val, opt_cfg->len);
496
497 /* pad */
498 for (i = opt_cfg->len; i < len * 8; i++)
499 put_opt6_char(0);
500 }
501 }
502
Simon Kelleyc3a04082014-01-11 22:18:19 +0000503 if (daemon->port == NAMESERVER_PORT && !done_dns && parm.link_pref_time != 0)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100504 {
Simon Kelleyab915f82013-04-30 10:41:28 +0100505 /* default == us, as long as we are supplying DNS service. */
Simon Kelley18f0fb02012-03-31 21:18:55 +0100506 put_opt6_char(ICMP6_OPT_RDNSS);
507 put_opt6_char(3);
508 put_opt6_short(0);
Simon Kelleyc3a04082014-01-11 22:18:19 +0000509 put_opt6_long(min_pref_time);
Simon Kelley806cf782013-10-14 14:08:44 +0100510 put_opt6(&parm.link_local, IN6ADDRSZ);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100511 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000512
513 /* set managed bits unless we're providing only RA on this link */
514 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100515 ra->flags |= 0x80; /* M flag, managed, */
516 if (parm.other)
517 ra->flags |= 0x40; /* O flag, other */
518
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000519 /* decide where we're sending */
520 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000521#ifdef HAVE_SOCKADDR_SA_LEN
522 addr.sin6_len = sizeof(struct sockaddr_in6);
523#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000524 addr.sin6_family = AF_INET6;
525 addr.sin6_port = htons(IPPROTO_ICMPV6);
526 if (dest)
527 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000528 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000529 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
530 IN6_IS_ADDR_MC_LINKLOCAL(dest))
531 addr.sin6_scope_id = iface;
532 }
533 else
Simon Kelley8f3194f2013-09-30 15:04:58 +0100534 {
535 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100536 setsockopt(daemon->icmp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &send_iface, sizeof(send_iface));
Simon Kelley8f3194f2013-09-30 15:04:58 +0100537 }
Simon Kelley22d904d2012-02-26 20:13:45 +0000538
Simon Kelleyff841eb2015-03-11 21:36:30 +0000539 while (retry_send(sendto(daemon->icmp6fd, daemon->outpacket.iov_base,
Simon Kelley6b1c4642016-07-22 20:59:16 +0100540 save_counter(-1), 0, (struct sockaddr *)&addr,
Simon Kelleyff841eb2015-03-11 21:36:30 +0000541 sizeof(addr))));
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000542
543}
544
Neil Jerramba4fc0f2015-06-10 22:14:49 +0100545static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
546{
547 /* Send an RA on the same interface that the RA content is based
548 on. */
549 send_ra_alias(now, iface, iface_name, dest, iface);
550}
551
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000552static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000553 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +0000554 unsigned int preferred, unsigned int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000555{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000556 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000557
558 (void)scope; /* warning */
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000559
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000560 if (if_index == param->ind)
561 {
562 if (IN6_IS_ADDR_LINKLOCAL(local))
Simon Kelleyc3a04082014-01-11 22:18:19 +0000563 {
564 /* Can there be more than one LL address?
565 Select the one with the longest preferred time
566 if there is. */
567 if (preferred > param->link_pref_time)
568 {
569 param->link_pref_time = preferred;
570 param->link_local = *local;
571 }
572 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000573 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000574 !IN6_IS_ADDR_MULTICAST(local))
575 {
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000576 int real_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100577 int do_slaac = 0;
578 int deprecate = 0;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000579 int constructed = 0;
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100580 int adv_router = 0;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100581 int off_link = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100582 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100583 struct dhcp_context *context;
584
Simon Kelley1f776932012-12-16 19:46:08 +0000585 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100586 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000587 prefix <= context->prefix &&
588 is_same_net6(local, &context->start6, context->prefix) &&
589 is_same_net6(local, &context->end6, context->prefix))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000590 {
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100591 context->saved_valid = valid;
592
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100593 if (context->flags & CONTEXT_RA)
Simon Kelley30cd9662012-03-25 20:44:38 +0100594 {
595 do_slaac = 1;
Simon Kelley4723d492012-03-30 21:04:17 +0100596 if (context->flags & CONTEXT_DHCP)
Simon Kelley05e92e52012-03-30 22:24:15 +0100597 {
598 param->other = 1;
599 if (!(context->flags & CONTEXT_RA_STATELESS))
600 param->managed = 1;
601 }
Simon Kelley30cd9662012-03-25 20:44:38 +0100602 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000603 else
Simon Kelley0010b472012-02-29 12:18:30 +0000604 {
605 /* don't do RA for non-ra-only unless --enable-ra is set */
606 if (!option_bool(OPT_RA))
607 continue;
608 param->managed = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100609 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000610 }
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100611
612 /* Configured to advertise router address, not prefix. See RFC 3775 7.2
613 In this case we do all addresses associated with a context,
614 hence the real_prefix setting here. */
615 if (context->flags & CONTEXT_RA_ROUTER)
616 {
617 adv_router = 1;
618 param->adv_router = 1;
619 real_prefix = context->prefix;
620 }
621
Simon Kelley1ecbaaa2013-07-25 14:19:27 +0100622 /* find floor time, don't reduce below 3 * RA interval. */
Simon Kelleyc8257542012-03-28 21:15:41 +0100623 if (time > context->lease_time)
Simon Kelley7f035f52012-12-22 21:27:08 +0000624 {
625 time = context->lease_time;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100626 if (time < ((unsigned int)(3 * param->adv_interval)))
627 time = 3 * param->adv_interval;
Simon Kelley7f035f52012-12-22 21:27:08 +0000628 }
629
Simon Kelleyc8257542012-03-28 21:15:41 +0100630 if (context->flags & CONTEXT_DEPRECATE)
631 deprecate = 1;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000632
633 if (context->flags & CONTEXT_CONSTRUCTED)
634 constructed = 1;
635
Simon Kelleyc8257542012-03-28 21:15:41 +0100636
Simon Kelley18f0fb02012-03-31 21:18:55 +0100637 /* collect dhcp-range tags */
638 if (context->netid.next == &context->netid && context->netid.net)
639 {
640 context->netid.next = param->tags;
641 param->tags = &context->netid;
642 }
643
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100644 /* subsequent prefixes on the same interface
645 and subsequent instances of this prefix don't need timers.
646 Be careful not to find the same prefix twice with different
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100647 addresses unless we're advertising the actual addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100648 if (!(context->flags & CONTEXT_RA_DONE))
649 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100650 if (!param->first)
651 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100652 context->flags |= CONTEXT_RA_DONE;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000653 real_prefix = context->prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100654 off_link = (context->flags & CONTEXT_RA_OFF_LINK);
Simon Kelley1e02a852012-03-29 11:07:25 +0100655 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100656
S La18bf312016-02-12 17:36:20 +0000657 param->first = 0;
658 /* found_context is the _last_ one we found, so if there's
659 more than one, it's not the first. */
660 param->found_context = context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000661 }
Simon Kelley1f776932012-12-16 19:46:08 +0000662
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000663 /* configured time is ceiling */
Simon Kelley3bc0d932012-12-28 11:31:44 +0000664 if (!constructed || valid > time)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000665 valid = time;
666
Simon Kelley3bc0d932012-12-28 11:31:44 +0000667 if (flags & IFACE_DEPRECATED)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000668 preferred = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100669
Simon Kelley3bc0d932012-12-28 11:31:44 +0000670 if (deprecate)
671 time = 0;
672
673 /* configured time is ceiling */
674 if (!constructed || preferred > time)
675 preferred = time;
Simon Kelleyc3a04082014-01-11 22:18:19 +0000676
677 if (IN6_IS_ADDR_ULA(local))
Simon Kelley55b42f62012-12-21 16:53:15 +0000678 {
Simon Kelleyc3a04082014-01-11 22:18:19 +0000679 if (preferred > param->ula_pref_time)
680 {
681 param->ula_pref_time = preferred;
682 param->ula = *local;
683 }
684 }
685 else
686 {
687 if (preferred > param->glob_pref_time)
688 {
689 param->glob_pref_time = preferred;
690 param->link_global = *local;
691 }
Simon Kelley55b42f62012-12-21 16:53:15 +0000692 }
693
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000694 if (real_prefix != 0)
Simon Kelleyc8257542012-03-28 21:15:41 +0100695 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100696 struct prefix_opt *opt;
697
698 if ((opt = expand(sizeof(struct prefix_opt))))
699 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100700 /* zero net part of address */
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100701 if (!adv_router)
702 setaddr6part(local, addr6part(local) & ~((real_prefix == 64) ? (u64)-1LL : (1LLU << (128 - real_prefix)) - 1LLU));
Simon Kelley5ef33272012-03-30 15:10:28 +0100703
Simon Kelley1e02a852012-03-29 11:07:25 +0100704 opt->type = ICMP6_OPT_PREFIX;
705 opt->len = 4;
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000706 opt->prefix_len = real_prefix;
Neil Jerram2fd5bc92015-06-10 22:13:06 +0100707 /* autonomous only if we're not doing dhcp, set
708 "on-link" unless "off-link" was specified */
709 opt->flags = (off_link ? 0 : 0x80);
Simon Kelley7ea3d3f2014-04-25 22:04:05 +0100710 if (do_slaac)
711 opt->flags |= 0x40;
712 if (adv_router)
713 opt->flags |= 0x20;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000714 opt->valid_lifetime = htonl(valid);
715 opt->preferred_lifetime = htonl(preferred);
Simon Kelley5ef33272012-03-30 15:10:28 +0100716 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100717 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100718
Simon Kelley5ef33272012-03-30 15:10:28 +0100719 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Kevin Darbyshire-Bryant8c0b73d2013-10-11 11:56:33 +0100720 if (!option_bool(OPT_QUIET_RA))
721 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
Simon Kelley1e02a852012-03-29 11:07:25 +0100722 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100723 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000724 }
725 }
726 return 1;
727}
728
729static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
730{
731 (void)type;
732
733 if (index == *((int *)parm))
734 {
735 /* size is in units of 8 octets and includes type and length (2 bytes)
736 add 7 to round up */
737 int len = (maclen + 9) >> 3;
738 unsigned char *p = expand(len << 3);
739 memset(p, 0, len << 3);
740 *p++ = ICMP6_OPT_SOURCE_MAC;
741 *p++ = len;
742 memcpy(p, mac, maclen);
743
744 return 0;
745 }
746
747 return 1;
748}
749
750time_t periodic_ra(time_t now)
751{
752 struct search_param param;
753 struct dhcp_context *context;
754 time_t next_event;
Neil Jerram9bad3392015-06-10 22:16:35 +0100755 struct alias_param aparam;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100756
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000757 param.now = now;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000758 param.iface = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000759
760 while (1)
761 {
762 /* find overdue events, and time of first future event */
Simon Kelley1f776932012-12-16 19:46:08 +0000763 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000764 if (context->ra_time != 0)
765 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100766 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000767 break; /* overdue */
768
Simon Kelley7dbe9812012-03-25 14:49:54 +0100769 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
770 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000771 }
772
773 /* none overdue */
774 if (!context)
775 break;
776
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100777 if ((context->flags & CONTEXT_OLD) &&
778 context->if_index != 0 &&
Simon Kelleydd9d9ce2013-11-15 11:24:00 +0000779 indextoname(daemon->icmp6fd, context->if_index, param.name))
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100780 {
781 /* A context for an old address. We'll not find the interface by
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100782 looking for addresses, but we know it anyway, since the context is
783 constructed */
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100784 param.iface = context->if_index;
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100785 new_timeout(context, param.name, now);
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100786 }
787 else if (iface_enumerate(AF_INET6, &param, iface_search))
788 /* There's a context overdue, but we can't find an interface
789 associated with it, because it's for a subnet we dont
790 have an interface on. Probably we're doing DHCP on
791 a remote subnet via a relay. Zero the timer, since we won't
Josh Soref730c6742017-02-06 16:14:04 +0000792 ever be able to send ra's and satisfy it. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000793 context->ra_time = 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100794
795 if (param.iface != 0 &&
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100796 iface_check(AF_LOCAL, NULL, param.name, NULL))
Simon Kelley421594f2012-12-02 12:17:35 +0000797 {
798 struct iname *tmp;
799 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100800 if (tmp->name && wildcard_match(tmp->name, param.name))
Simon Kelley421594f2012-12-02 12:17:35 +0000801 break;
802 if (!tmp)
Neil Jerram9bad3392015-06-10 22:16:35 +0100803 {
804 send_ra(now, param.iface, param.name, NULL);
805
806 /* Also send on all interfaces that are aliases of this
807 one. */
808 for (aparam.bridge = daemon->bridges;
809 aparam.bridge;
810 aparam.bridge = aparam.bridge->next)
811 if ((int)if_nametoindex(aparam.bridge->iface) == param.iface)
812 {
813 /* Count the number of alias interfaces for this
814 'bridge', by calling iface_enumerate with
815 send_ra_to_aliases and NULL alias_ifs. */
816 aparam.iface = param.iface;
817 aparam.alias_ifs = NULL;
818 aparam.num_alias_ifs = 0;
819 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
820 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => %d alias(es)",
821 param.name, daemon->addrbuff, aparam.num_alias_ifs);
822
823 /* Allocate memory to store the alias interface
824 indices. */
825 aparam.alias_ifs = (int *)whine_malloc(aparam.num_alias_ifs *
826 sizeof(int));
827 if (aparam.alias_ifs)
828 {
829 /* Use iface_enumerate again to get the alias
830 interface indices, then send on each of
831 those. */
832 aparam.max_alias_ifs = aparam.num_alias_ifs;
833 aparam.num_alias_ifs = 0;
834 iface_enumerate(AF_LOCAL, &aparam, send_ra_to_aliases);
835 for (; aparam.num_alias_ifs; aparam.num_alias_ifs--)
836 {
837 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s => i/f %d",
838 param.name, daemon->addrbuff,
839 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
840 send_ra_alias(now,
841 param.iface,
842 param.name,
843 NULL,
844 aparam.alias_ifs[aparam.num_alias_ifs - 1]);
845 }
846 free(aparam.alias_ifs);
847 }
848
849 /* The source interface can only appear in at most
Neil Jerram4918bd52015-06-10 22:23:20 +0100850 one --bridge-interface. */
Neil Jerram9bad3392015-06-10 22:16:35 +0100851 break;
852 }
853 }
Simon Kelley421594f2012-12-02 12:17:35 +0000854 }
855 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000856 return next_event;
857}
Neil Jerram9bad3392015-06-10 22:16:35 +0100858
859static int send_ra_to_aliases(int index, unsigned int type, char *mac, size_t maclen, void *parm)
860{
861 struct alias_param *aparam = (struct alias_param *)parm;
862 char ifrn_name[IFNAMSIZ];
863 struct dhcp_bridge *alias;
864
865 (void)type;
866 (void)mac;
867 (void)maclen;
868
869 if (if_indextoname(index, ifrn_name))
870 for (alias = aparam->bridge->alias; alias; alias = alias->next)
871 if (wildcard_matchn(alias->iface, ifrn_name, IFNAMSIZ))
872 {
873 if (aparam->alias_ifs && (aparam->num_alias_ifs < aparam->max_alias_ifs))
874 aparam->alias_ifs[aparam->num_alias_ifs] = index;
875 aparam->num_alias_ifs++;
876 }
877
878 return 1;
879}
880
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000881static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000882 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000883 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000884{
885 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000886 struct dhcp_context *context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000887
888 (void)scope;
Simon Kelley1f776932012-12-16 19:46:08 +0000889 (void)preferred;
890 (void)valid;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000891
Simon Kelley1f776932012-12-16 19:46:08 +0000892 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100893 if (!(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000894 prefix <= context->prefix &&
895 is_same_net6(local, &context->start6, context->prefix) &&
896 is_same_net6(local, &context->end6, context->prefix) &&
Simon Kelley6e3dba32012-12-16 21:52:45 +0000897 context->ra_time != 0 &&
898 difftime(context->ra_time, param->now) <= 0.0)
899 {
900 /* found an interface that's overdue for RA determine new
901 timeout value and arrange for RA to be sent unless interface is
902 still doing DAD.*/
903
Simon Kelleybad7b872012-12-20 22:00:39 +0000904 if (!(flags & IFACE_TENTATIVE))
Simon Kelley6e3dba32012-12-16 21:52:45 +0000905 param->iface = if_index;
906
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100907 /* should never fail */
908 if (!indextoname(daemon->icmp6fd, if_index, param->name))
909 {
910 param->iface = 0;
911 return 0;
912 }
913
914 new_timeout(context, param->name, param->now);
Simon Kelley6e3dba32012-12-16 21:52:45 +0000915
916 /* zero timers for other contexts on the same subnet, so they don't timeout
917 independently */
918 for (context = context->next; context; context = context->next)
Vladislav Grishenko4c82efc2013-12-03 16:05:30 +0000919 if (prefix <= context->prefix &&
920 is_same_net6(local, &context->start6, context->prefix) &&
921 is_same_net6(local, &context->end6, context->prefix))
Simon Kelley6e3dba32012-12-16 21:52:45 +0000922 context->ra_time = 0;
923
924 return 0; /* found, abort */
925 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000926
927 return 1; /* keep searching */
928}
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100929
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100930static void new_timeout(struct dhcp_context *context, char *iface_name, time_t now)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100931{
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100932 if (difftime(now, context->ra_short_period_start) < 60.0)
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100933 /* range 5 - 20 */
934 context->ra_time = now + 5 + (rand16()/4400);
935 else
Simon Kelleyc4cd95d2013-10-10 20:58:11 +0100936 {
937 /* range 3/4 - 1 times MaxRtrAdvInterval */
938 unsigned int adv_interval = calc_interval(find_iface_param(iface_name));
939 context->ra_time = now + (3 * adv_interval)/4 + ((adv_interval * (unsigned int)rand16()) >> 18);
940 }
941}
942
943static struct ra_interface *find_iface_param(char *iface)
944{
945 struct ra_interface *ra;
946
947 for (ra = daemon->ra_interfaces; ra; ra = ra->next)
948 if (wildcard_match(ra->name, iface))
949 return ra;
950
951 return NULL;
952}
953
954static unsigned int calc_interval(struct ra_interface *ra)
955{
956 int interval = 600;
957
958 if (ra && ra->interval != 0)
959 {
960 interval = ra->interval;
961 if (interval > 1800)
962 interval = 1800;
963 else if (interval < 4)
964 interval = 4;
965 }
966
967 return (unsigned int)interval;
968}
969
970static unsigned int calc_lifetime(struct ra_interface *ra)
971{
972 int lifetime, interval = (int)calc_interval(ra);
973
974 if (!ra || ra->lifetime == -1) /* not specified */
975 lifetime = 3 * interval;
976 else
977 {
978 lifetime = ra->lifetime;
979 if (lifetime < interval && lifetime != 0)
980 lifetime = interval;
981 else if (lifetime > 9000)
982 lifetime = 9000;
983 }
984
985 return (unsigned int)lifetime;
986}
987
988static unsigned int calc_prio(struct ra_interface *ra)
989{
990 if (ra)
991 return ra->prio;
992
993 return 0;
Simon Kelleyef1a94a2013-07-26 13:59:03 +0100994}
Simon Kelley801ca9a2012-03-06 19:30:17 +0000995
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000996#endif