blob: 632e008d2ff9db5f1ab3cf0fa341c4a695428253 [file] [log] [blame]
Simon Kelleyc5ad4e72012-02-24 16:06:20 +00001/* dnsmasq is Copyright (c) 2000-2012 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
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 Kelley30cd9662012-03-25 20:44:38 +010030 int ind, managed, other, found_context, first;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000031 char *if_name;
Simon Kelley18f0fb02012-03-31 21:18:55 +010032 struct dhcp_netid *tags;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000033 struct in6_addr link_local;
34};
35
36struct search_param {
37 time_t now; int iface;
38};
39
40static void send_ra(int iface, char *iface_name, struct in6_addr *dest);
41static int add_prefixes(struct in6_addr *local, int prefix,
42 int scope, int if_index, int dad, void *vparam);
43static int iface_search(struct in6_addr *local, int prefix,
44 int scope, int if_index, int dad, void *vparam);
45static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
46
Simon Kelleyc5379c12012-02-24 20:05:52 +000047static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000048static time_t ra_short_period_start;
49
50void ra_init(time_t now)
51{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000052 struct icmp6_filter filter;
53 int fd;
Simon Kelley0e88d532012-03-28 22:22:05 +010054#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000055 int class = IPTOS_CLASS_CS6;
56#endif
57 int val = 255; /* radvd uses this value */
Simon Kelley7b6dd882012-03-01 10:26:16 +000058 socklen_t len = sizeof(int);
Simon Kelley353ae4d2012-03-19 20:07:51 +000059 struct dhcp_context *context;
60
Simon Kelley843c96b2012-02-27 17:42:38 +000061 /* ensure this is around even if we're not doing DHCPv6 */
62 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000063
64 /* See if we're guessing SLAAC addresses, if so we need to recieve ping replies */
65 for (context = daemon->ra_contexts; context; context = context->next)
66 if ((context->flags & CONTEXT_RA_NAME))
67 break;
68
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000069 ICMP6_FILTER_SETBLOCKALL(&filter);
70 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
Simon Kelley353ae4d2012-03-19 20:07:51 +000071 if (context)
72 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000073
74 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +000075 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelley0e88d532012-03-28 22:22:05 +010076#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000077 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
78#endif
79 !fix_fd(fd) ||
80 !set_ipv6pktinfo(fd) ||
81 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
82 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
83 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
84 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
85
86 daemon->icmp6fd = fd;
87
Simon Kelley353ae4d2012-03-19 20:07:51 +000088 ra_start_unsolicted(now, NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000089}
90
Simon Kelley353ae4d2012-03-19 20:07:51 +000091void ra_start_unsolicted(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000092{
Simon Kelley353ae4d2012-03-19 20:07:51 +000093 /* 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 +000094 if it's not appropriate to advertise those contexts.
95 This gets re-called on a netlink route-change to re-do the advertisement
96 and pick up new interfaces */
97
Simon Kelley353ae4d2012-03-19 20:07:51 +000098 if (context)
99 context->ra_time = now;
100 else
101 for (context = daemon->ra_contexts; context; context = context->next)
102 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000103
Simon Kelley22d904d2012-02-26 20:13:45 +0000104 /* re-do frequently for a minute or so, in case the first gets lost. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000105 ra_short_period_start = now;
106}
107
108void icmp6_packet(void)
109{
110 char interface[IF_NAMESIZE+1];
111 ssize_t sz;
112 int if_index = 0;
113 struct cmsghdr *cmptr;
114 struct msghdr msg;
115 union {
116 struct cmsghdr align; /* this ensures alignment */
117 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
118 } control_u;
119 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100120 unsigned char *packet;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000121 struct iname *tmp;
122 struct dhcp_context *context;
123
124 /* Note: use outpacket for input buffer */
125 msg.msg_control = control_u.control6;
126 msg.msg_controllen = sizeof(control_u);
127 msg.msg_flags = 0;
128 msg.msg_name = &from;
129 msg.msg_namelen = sizeof(from);
130 msg.msg_iov = &daemon->outpacket;
131 msg.msg_iovlen = 1;
132
133 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
134 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100135
136 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000137
138 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
139 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
140 {
141 union {
142 unsigned char *c;
143 struct in6_pktinfo *p;
144 } p;
145 p.c = CMSG_DATA(cmptr);
146
147 if_index = p.p->ipi6_ifindex;
148 }
149
150 if (!indextoname(daemon->icmp6fd, if_index, interface))
151 return;
152
153 if (!iface_check(AF_LOCAL, NULL, interface))
154 return;
155
156 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
157 if (tmp->name && (strcmp(tmp->name, interface) == 0))
158 return;
159
160 /* weird libvirt-inspired access control */
Simon Kelley51931b82012-05-29 17:06:02 +0100161 for (context = daemon->ra_contexts ? daemon->ra_contexts : daemon->dhcp6;
162 context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000163 if (!context->interface || strcmp(context->interface, interface) == 0)
164 break;
165
Simon Kelley5ef33272012-03-30 15:10:28 +0100166 if (!context || packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000167 return;
168
Simon Kelley5ef33272012-03-30 15:10:28 +0100169 if (packet[0] == ICMP6_ECHO_REPLY)
170 lease_ping_reply(&from.sin6_addr, packet, interface);
171 else if (packet[0] == ND_ROUTER_SOLICIT)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000172 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100173 char *mac = "";
174
175 /* look for link-layer address option for logging */
176 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
177 {
178 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
179 mac = daemon->namebuff;
180 }
181
182 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Simon Kelleyf632e562012-05-12 15:05:34 +0100183 /* source address may not be valid in solicit request. */
184 send_ra(if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000185 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000186}
187
188static void send_ra(int iface, char *iface_name, struct in6_addr *dest)
189{
190 struct ra_packet *ra;
191 struct ra_param parm;
192 struct ifreq ifr;
193 struct sockaddr_in6 addr;
194 struct dhcp_context *context;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100195 struct dhcp_netid iface_id;
196 struct dhcp_opt *opt_cfg;
197 int done_dns = 0;
198
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000199 save_counter(0);
200 ra = expand(sizeof(struct ra_packet));
201
Simon Kelley353ae4d2012-03-19 20:07:51 +0000202 ra->type = ND_ROUTER_ADVERT;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000203 ra->code = 0;
Simon Kelleyc5379c12012-02-24 20:05:52 +0000204 ra->hop_limit = hop_limit;
Simon Kelley884a6df2012-03-20 16:20:22 +0000205 ra->flags = 0x00;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000206 ra->lifetime = htons(1800); /* AdvDefaultLifetime*/
207 ra->reachable_time = 0;
208 ra->retrans_time = 0;
209
210 parm.ind = iface;
211 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100212 parm.other = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000213 parm.found_context = 0;
214 parm.if_name = iface_name;
215 parm.first = 1;
216
Simon Kelley18f0fb02012-03-31 21:18:55 +0100217 /* set tag with name == interface */
218 iface_id.net = iface_name;
219 iface_id.next = NULL;
220 parm.tags = &iface_id;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000221
Simon Kelley18f0fb02012-03-31 21:18:55 +0100222 for (context = daemon->ra_contexts; context; context = context->next)
223 {
224 context->flags &= ~CONTEXT_RA_DONE;
225 context->netid.next = &context->netid;
226 }
227
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000228 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
229 !parm.found_context)
230 return;
231
232 strncpy(ifr.ifr_name, iface_name, IF_NAMESIZE);
233
234 if (ioctl(daemon->icmp6fd, SIOCGIFMTU, &ifr) != -1)
235 {
236 put_opt6_char(ICMP6_OPT_MTU);
237 put_opt6_char(1);
238 put_opt6_short(0);
239 put_opt6_long(ifr.ifr_mtu);
240 }
241
242 iface_enumerate(AF_LOCAL, &iface, add_lla);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100243
244 /* RDNSS, RFC 6106, use relevant DHCP6 options */
245 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000246
Simon Kelley18f0fb02012-03-31 21:18:55 +0100247 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
248 {
249 int i;
250
251 /* netids match and not encapsulated? */
252 if (!(opt_cfg->flags & DHOPT_TAGOK))
253 continue;
254
255 if (opt_cfg->opt == OPTION6_DNS_SERVER)
256 {
257 struct in6_addr *a = (struct in6_addr *)opt_cfg->val;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000258
Simon Kelley18f0fb02012-03-31 21:18:55 +0100259 done_dns = 1;
260 if (opt_cfg->len == 0)
261 continue;
262
263 put_opt6_char(ICMP6_OPT_RDNSS);
264 put_opt6_char((opt_cfg->len/8) + 1);
265 put_opt6_short(0);
266 put_opt6_long(1800); /* lifetime - twice RA retransmit */
267 /* zero means "self" */
268 for (i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
269 if (IN6_IS_ADDR_UNSPECIFIED(a))
270 put_opt6(&parm.link_local, IN6ADDRSZ);
271 else
272 put_opt6(a, IN6ADDRSZ);
273 }
274
275 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
276 {
277 int len = ((opt_cfg->len+7)/8);
278
279 put_opt6_char(ICMP6_OPT_DNSSL);
280 put_opt6_char(len + 1);
281 put_opt6_short(0);
282 put_opt6_long(1800); /* lifetime - twice RA retransmit */
283 put_opt6(opt_cfg->val, opt_cfg->len);
284
285 /* pad */
286 for (i = opt_cfg->len; i < len * 8; i++)
287 put_opt6_char(0);
288 }
289 }
290
291 if (!done_dns)
292 {
293 /* default == us. */
294 put_opt6_char(ICMP6_OPT_RDNSS);
295 put_opt6_char(3);
296 put_opt6_short(0);
297 put_opt6_long(1800); /* lifetime - twice RA retransmit */
298 put_opt6(&parm.link_local, IN6ADDRSZ);
299 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000300
301 /* set managed bits unless we're providing only RA on this link */
302 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100303 ra->flags |= 0x80; /* M flag, managed, */
304 if (parm.other)
305 ra->flags |= 0x40; /* O flag, other */
306
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000307 /* decide where we're sending */
308 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000309#ifdef HAVE_SOCKADDR_SA_LEN
310 addr.sin6_len = sizeof(struct sockaddr_in6);
311#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000312 addr.sin6_family = AF_INET6;
313 addr.sin6_port = htons(IPPROTO_ICMPV6);
314 if (dest)
315 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000316 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000317 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
318 IN6_IS_ADDR_MC_LINKLOCAL(dest))
319 addr.sin6_scope_id = iface;
320 }
321 else
Simon Kelleyf632e562012-05-12 15:05:34 +0100322 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
Simon Kelley22d904d2012-02-26 20:13:45 +0000323
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000324 send_from(daemon->icmp6fd, 0, daemon->outpacket.iov_base, save_counter(0),
Simon Kelley50303b12012-04-04 22:13:17 +0100325 (union mysockaddr *)&addr, (struct all_addr *)&parm.link_local, iface);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000326
327}
328
329static int add_prefixes(struct in6_addr *local, int prefix,
330 int scope, int if_index, int dad, void *vparam)
331{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000332 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000333
334 (void)scope; /* warning */
335 (void)dad;
336
337 if (if_index == param->ind)
338 {
339 if (IN6_IS_ADDR_LINKLOCAL(local))
340 param->link_local = *local;
341 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
342 !IN6_IS_ADDR_LINKLOCAL(local) &&
343 !IN6_IS_ADDR_MULTICAST(local))
344 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100345 int do_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100346 int do_slaac = 0;
347 int deprecate = 0;
348 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100349 struct dhcp_context *context;
350
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000351 for (context = daemon->ra_contexts; context; context = context->next)
352 if (prefix == context->prefix &&
353 is_same_net6(local, &context->start6, prefix) &&
354 is_same_net6(local, &context->end6, prefix))
355 {
Simon Kelley30cd9662012-03-25 20:44:38 +0100356 if ((context->flags &
357 (CONTEXT_RA_ONLY | CONTEXT_RA_NAME | CONTEXT_RA_STATELESS)))
358 {
359 do_slaac = 1;
Simon Kelley4723d492012-03-30 21:04:17 +0100360 if (context->flags & CONTEXT_DHCP)
Simon Kelley05e92e52012-03-30 22:24:15 +0100361 {
362 param->other = 1;
363 if (!(context->flags & CONTEXT_RA_STATELESS))
364 param->managed = 1;
365 }
Simon Kelley30cd9662012-03-25 20:44:38 +0100366 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000367 else
Simon Kelley0010b472012-02-29 12:18:30 +0000368 {
369 /* don't do RA for non-ra-only unless --enable-ra is set */
370 if (!option_bool(OPT_RA))
371 continue;
372 param->managed = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100373 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000374 }
375
Simon Kelleyc8257542012-03-28 21:15:41 +0100376 /* find floor time */
377 if (time > context->lease_time)
378 time = context->lease_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000379
Simon Kelleyc8257542012-03-28 21:15:41 +0100380 if (context->flags & CONTEXT_DEPRECATE)
381 deprecate = 1;
382
Simon Kelley18f0fb02012-03-31 21:18:55 +0100383 /* collect dhcp-range tags */
384 if (context->netid.next == &context->netid && context->netid.net)
385 {
386 context->netid.next = param->tags;
387 param->tags = &context->netid;
388 }
389
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100390 /* subsequent prefixes on the same interface
391 and subsequent instances of this prefix don't need timers.
392 Be careful not to find the same prefix twice with different
393 addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100394 if (!(context->flags & CONTEXT_RA_DONE))
395 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100396 if (!param->first)
397 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100398 context->flags |= CONTEXT_RA_DONE;
399 do_prefix = 1;
400 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100401
402 param->first = 0;
403 param->found_context = 1;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000404 }
Simon Kelley1e02a852012-03-29 11:07:25 +0100405
406 if (do_prefix)
Simon Kelleyc8257542012-03-28 21:15:41 +0100407 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100408 struct prefix_opt *opt;
409
410 if ((opt = expand(sizeof(struct prefix_opt))))
411 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100412 /* zero net part of address */
413 setaddr6part(local, addr6part(local) & ~((prefix == 64) ? (u64)-1LL : (1LLU << (128 - prefix)) - 1LLU));
414
Simon Kelley1e02a852012-03-29 11:07:25 +0100415 /* lifetimes must be min 2 hrs, by RFC 2462 */
416 if (time < 7200)
417 time = 7200;
418
419 opt->type = ICMP6_OPT_PREFIX;
420 opt->len = 4;
421 opt->prefix_len = prefix;
Simon Kelley5ef33272012-03-30 15:10:28 +0100422 /* autonomous only if we're not doing dhcp */
Simon Kelley1e02a852012-03-29 11:07:25 +0100423 opt->flags = do_slaac ? 0x40 : 0x00;
424 opt->valid_lifetime = htonl(time);
425 opt->preferred_lifetime = htonl(deprecate ? 0 : time);
Simon Kelley5ef33272012-03-30 15:10:28 +0100426 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100427 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100428
Simon Kelley5ef33272012-03-30 15:10:28 +0100429 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley1e02a852012-03-29 11:07:25 +0100430 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
431 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100432 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000433 }
434 }
435 return 1;
436}
437
438static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
439{
440 (void)type;
441
442 if (index == *((int *)parm))
443 {
444 /* size is in units of 8 octets and includes type and length (2 bytes)
445 add 7 to round up */
446 int len = (maclen + 9) >> 3;
447 unsigned char *p = expand(len << 3);
448 memset(p, 0, len << 3);
449 *p++ = ICMP6_OPT_SOURCE_MAC;
450 *p++ = len;
451 memcpy(p, mac, maclen);
452
453 return 0;
454 }
455
456 return 1;
457}
458
459time_t periodic_ra(time_t now)
460{
461 struct search_param param;
462 struct dhcp_context *context;
463 time_t next_event;
464 char interface[IF_NAMESIZE+1];
465
466 param.now = now;
467
468 while (1)
469 {
470 /* find overdue events, and time of first future event */
471 for (next_event = 0, context = daemon->ra_contexts; context; context = context->next)
472 if (context->ra_time != 0)
473 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100474 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000475 break; /* overdue */
476
Simon Kelley7dbe9812012-03-25 14:49:54 +0100477 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
478 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000479 }
480
481 /* none overdue */
482 if (!context)
483 break;
484
485 /* There's a context overdue, but we can't find an interface
486 associated with it, because it's for a subnet we dont
487 have an interface on. Probably we're doing DHCP on
488 a remote subnet via a relay. Zero the timer, since we won't
489 ever be able to send ra's and satistfy it. */
490 if (iface_enumerate(AF_INET6, &param, iface_search))
491 context->ra_time = 0;
492 else if (indextoname(daemon->icmp6fd, param.iface, interface))
493 send_ra(param.iface, interface, NULL);
494 }
495
496 return next_event;
497}
498
499static int iface_search(struct in6_addr *local, int prefix,
500 int scope, int if_index, int dad, void *vparam)
501{
502 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000503 struct dhcp_context *context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000504
505 (void)scope;
506 (void)dad;
507
508 for (context = daemon->ra_contexts; context; context = context->next)
509 if (prefix == context->prefix &&
510 is_same_net6(local, &context->start6, prefix) &&
511 is_same_net6(local, &context->end6, prefix))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000512 if (context->ra_time != 0 && difftime(context->ra_time, param->now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000513 {
514 /* found an interface that's overdue for RA determine new
515 timeout value and zap other contexts on the same interface
516 so they don't timeout independently .*/
517 param->iface = if_index;
518
519 if (difftime(param->now, ra_short_period_start) < 60.0)
520 /* range 5 - 20 */
521 context->ra_time = param->now + 5 + (rand16()/4400);
522 else
523 /* range 450 - 600 */
524 context->ra_time = param->now + 450 + (rand16()/440);
525
526 return 0; /* found, abort */
527 }
528
529 return 1; /* keep searching */
530}
531
Simon Kelley801ca9a2012-03-06 19:30:17 +0000532
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000533#endif