blob: a935343cb70e9f952a96eed63b60c65d7af8fc04 [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 Kelley1f776932012-12-16 19:46:08 +000030 time_t now;
Simon Kelley30cd9662012-03-25 20:44:38 +010031 int ind, managed, other, found_context, first;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000032 char *if_name;
Simon Kelley18f0fb02012-03-31 21:18:55 +010033 struct dhcp_netid *tags;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000034 struct in6_addr link_local;
35};
36
37struct search_param {
38 time_t now; int iface;
39};
40
Simon Kelley1f776932012-12-16 19:46:08 +000041static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000042static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelley1f776932012-12-16 19:46:08 +000043 int scope, int if_index, int dad,
44 int preferred, int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000045static int iface_search(struct in6_addr *local, int prefix,
Simon Kelley1f776932012-12-16 19:46:08 +000046 int scope, int if_index, int dad,
47 int prefered, int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000048static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
49
Simon Kelleyc5379c12012-02-24 20:05:52 +000050static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000051static time_t ra_short_period_start;
52
53void ra_init(time_t now)
54{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000055 struct icmp6_filter filter;
56 int fd;
Simon Kelley0e88d532012-03-28 22:22:05 +010057#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000058 int class = IPTOS_CLASS_CS6;
59#endif
60 int val = 255; /* radvd uses this value */
Simon Kelley7b6dd882012-03-01 10:26:16 +000061 socklen_t len = sizeof(int);
Simon Kelley353ae4d2012-03-19 20:07:51 +000062 struct dhcp_context *context;
63
Simon Kelley843c96b2012-02-27 17:42:38 +000064 /* ensure this is around even if we're not doing DHCPv6 */
65 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000066
67 /* See if we're guessing SLAAC addresses, if so we need to recieve ping replies */
Simon Kelley1f776932012-12-16 19:46:08 +000068 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley353ae4d2012-03-19 20:07:51 +000069 if ((context->flags & CONTEXT_RA_NAME))
70 break;
71
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000072 ICMP6_FILTER_SETBLOCKALL(&filter);
73 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
Simon Kelley353ae4d2012-03-19 20:07:51 +000074 if (context)
75 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000076
77 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +000078 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelley0e88d532012-03-28 22:22:05 +010079#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000080 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
81#endif
82 !fix_fd(fd) ||
83 !set_ipv6pktinfo(fd) ||
84 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
85 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
86 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
87 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
88
89 daemon->icmp6fd = fd;
90
Simon Kelley353ae4d2012-03-19 20:07:51 +000091 ra_start_unsolicted(now, NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000092}
93
Simon Kelley353ae4d2012-03-19 20:07:51 +000094void ra_start_unsolicted(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000095{
Simon Kelley353ae4d2012-03-19 20:07:51 +000096 /* 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 +000097 if it's not appropriate to advertise those contexts.
98 This gets re-called on a netlink route-change to re-do the advertisement
99 and pick up new interfaces */
100
Simon Kelley353ae4d2012-03-19 20:07:51 +0000101 if (context)
102 context->ra_time = now;
103 else
Simon Kelley1f776932012-12-16 19:46:08 +0000104 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley7558ecd2012-12-16 21:45:16 +0000105 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
106
Simon Kelley22d904d2012-02-26 20:13:45 +0000107 /* re-do frequently for a minute or so, in case the first gets lost. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000108 ra_short_period_start = now;
109}
110
Simon Kelley1f776932012-12-16 19:46:08 +0000111void icmp6_packet(time_t now)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000112{
113 char interface[IF_NAMESIZE+1];
114 ssize_t sz;
115 int if_index = 0;
116 struct cmsghdr *cmptr;
117 struct msghdr msg;
118 union {
119 struct cmsghdr align; /* this ensures alignment */
120 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
121 } control_u;
122 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100123 unsigned char *packet;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000124 struct iname *tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000125
126 /* Note: use outpacket for input buffer */
127 msg.msg_control = control_u.control6;
128 msg.msg_controllen = sizeof(control_u);
129 msg.msg_flags = 0;
130 msg.msg_name = &from;
131 msg.msg_namelen = sizeof(from);
132 msg.msg_iov = &daemon->outpacket;
133 msg.msg_iovlen = 1;
134
135 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
136 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100137
138 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000139
140 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
141 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
142 {
143 union {
144 unsigned char *c;
145 struct in6_pktinfo *p;
146 } p;
147 p.c = CMSG_DATA(cmptr);
148
149 if_index = p.p->ipi6_ifindex;
150 }
151
152 if (!indextoname(daemon->icmp6fd, if_index, interface))
153 return;
154
Simon Kelley4f7b3042012-11-28 21:27:02 +0000155 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000156 return;
157
158 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
159 if (tmp->name && (strcmp(tmp->name, interface) == 0))
160 return;
161
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100162 if (packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000163 return;
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100164
Simon Kelley5ef33272012-03-30 15:10:28 +0100165 if (packet[0] == ICMP6_ECHO_REPLY)
166 lease_ping_reply(&from.sin6_addr, packet, interface);
167 else if (packet[0] == ND_ROUTER_SOLICIT)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000168 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100169 char *mac = "";
170
171 /* look for link-layer address option for logging */
172 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
173 {
174 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
175 mac = daemon->namebuff;
176 }
177
178 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Simon Kelleyf632e562012-05-12 15:05:34 +0100179 /* source address may not be valid in solicit request. */
Simon Kelley1f776932012-12-16 19:46:08 +0000180 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000181 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000182}
183
Simon Kelley1f776932012-12-16 19:46:08 +0000184static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000185{
186 struct ra_packet *ra;
187 struct ra_param parm;
188 struct ifreq ifr;
189 struct sockaddr_in6 addr;
190 struct dhcp_context *context;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100191 struct dhcp_netid iface_id;
192 struct dhcp_opt *opt_cfg;
193 int done_dns = 0;
194
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000195 save_counter(0);
196 ra = expand(sizeof(struct ra_packet));
197
Simon Kelley353ae4d2012-03-19 20:07:51 +0000198 ra->type = ND_ROUTER_ADVERT;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000199 ra->code = 0;
Simon Kelleyc5379c12012-02-24 20:05:52 +0000200 ra->hop_limit = hop_limit;
Simon Kelley884a6df2012-03-20 16:20:22 +0000201 ra->flags = 0x00;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000202 ra->lifetime = htons(1800); /* AdvDefaultLifetime*/
203 ra->reachable_time = 0;
204 ra->retrans_time = 0;
205
206 parm.ind = iface;
207 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100208 parm.other = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000209 parm.found_context = 0;
210 parm.if_name = iface_name;
211 parm.first = 1;
Simon Kelley1f776932012-12-16 19:46:08 +0000212 parm.now = now;
213
Simon Kelley18f0fb02012-03-31 21:18:55 +0100214 /* set tag with name == interface */
215 iface_id.net = iface_name;
216 iface_id.next = NULL;
217 parm.tags = &iface_id;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000218
Simon Kelley1f776932012-12-16 19:46:08 +0000219 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100220 {
221 context->flags &= ~CONTEXT_RA_DONE;
222 context->netid.next = &context->netid;
223 }
224
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000225 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
226 !parm.found_context)
227 return;
228
229 strncpy(ifr.ifr_name, iface_name, IF_NAMESIZE);
230
231 if (ioctl(daemon->icmp6fd, SIOCGIFMTU, &ifr) != -1)
232 {
233 put_opt6_char(ICMP6_OPT_MTU);
234 put_opt6_char(1);
235 put_opt6_short(0);
236 put_opt6_long(ifr.ifr_mtu);
237 }
238
239 iface_enumerate(AF_LOCAL, &iface, add_lla);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100240
241 /* RDNSS, RFC 6106, use relevant DHCP6 options */
242 (void)option_filter(parm.tags, NULL, daemon->dhcp_opts6);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000243
Simon Kelley18f0fb02012-03-31 21:18:55 +0100244 for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
245 {
246 int i;
247
248 /* netids match and not encapsulated? */
249 if (!(opt_cfg->flags & DHOPT_TAGOK))
250 continue;
251
252 if (opt_cfg->opt == OPTION6_DNS_SERVER)
253 {
254 struct in6_addr *a = (struct in6_addr *)opt_cfg->val;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000255
Simon Kelley18f0fb02012-03-31 21:18:55 +0100256 done_dns = 1;
257 if (opt_cfg->len == 0)
258 continue;
259
260 put_opt6_char(ICMP6_OPT_RDNSS);
261 put_opt6_char((opt_cfg->len/8) + 1);
262 put_opt6_short(0);
263 put_opt6_long(1800); /* lifetime - twice RA retransmit */
264 /* zero means "self" */
265 for (i = 0; i < opt_cfg->len; i += IN6ADDRSZ, a++)
266 if (IN6_IS_ADDR_UNSPECIFIED(a))
267 put_opt6(&parm.link_local, IN6ADDRSZ);
268 else
269 put_opt6(a, IN6ADDRSZ);
270 }
271
272 if (opt_cfg->opt == OPTION6_DOMAIN_SEARCH && opt_cfg->len != 0)
273 {
274 int len = ((opt_cfg->len+7)/8);
275
276 put_opt6_char(ICMP6_OPT_DNSSL);
277 put_opt6_char(len + 1);
278 put_opt6_short(0);
279 put_opt6_long(1800); /* lifetime - twice RA retransmit */
280 put_opt6(opt_cfg->val, opt_cfg->len);
281
282 /* pad */
283 for (i = opt_cfg->len; i < len * 8; i++)
284 put_opt6_char(0);
285 }
286 }
287
288 if (!done_dns)
289 {
290 /* default == us. */
291 put_opt6_char(ICMP6_OPT_RDNSS);
292 put_opt6_char(3);
293 put_opt6_short(0);
294 put_opt6_long(1800); /* lifetime - twice RA retransmit */
295 put_opt6(&parm.link_local, IN6ADDRSZ);
296 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000297
298 /* set managed bits unless we're providing only RA on this link */
299 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100300 ra->flags |= 0x80; /* M flag, managed, */
301 if (parm.other)
302 ra->flags |= 0x40; /* O flag, other */
303
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000304 /* decide where we're sending */
305 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000306#ifdef HAVE_SOCKADDR_SA_LEN
307 addr.sin6_len = sizeof(struct sockaddr_in6);
308#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000309 addr.sin6_family = AF_INET6;
310 addr.sin6_port = htons(IPPROTO_ICMPV6);
311 if (dest)
312 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000313 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000314 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
315 IN6_IS_ADDR_MC_LINKLOCAL(dest))
316 addr.sin6_scope_id = iface;
317 }
318 else
Simon Kelleyf632e562012-05-12 15:05:34 +0100319 inet_pton(AF_INET6, ALL_NODES, &addr.sin6_addr);
Simon Kelley22d904d2012-02-26 20:13:45 +0000320
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000321 send_from(daemon->icmp6fd, 0, daemon->outpacket.iov_base, save_counter(0),
Simon Kelley50303b12012-04-04 22:13:17 +0100322 (union mysockaddr *)&addr, (struct all_addr *)&parm.link_local, iface);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000323
324}
325
326static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelley1f776932012-12-16 19:46:08 +0000327 int scope, int if_index, int dad,
328 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000329{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000330 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000331
332 (void)scope; /* warning */
333 (void)dad;
Simon Kelley1f776932012-12-16 19:46:08 +0000334 (void)preferred;
335 (void)valid;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000336
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) &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000342 !IN6_IS_ADDR_MULTICAST(local))
343 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100344 int do_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100345 int do_slaac = 0;
346 int deprecate = 0;
Simon Kelley1f776932012-12-16 19:46:08 +0000347 int found_constructed = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100348 unsigned int time = 0xffffffff;
Simon Kelley1f776932012-12-16 19:46:08 +0000349 int calc_valid = 0, calc_preferred = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100350 struct dhcp_context *context;
351
Simon Kelley1f776932012-12-16 19:46:08 +0000352 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000353 if (prefix == context->prefix &&
354 is_same_net6(local, &context->start6, prefix) &&
355 is_same_net6(local, &context->end6, prefix))
356 {
Simon Kelley30cd9662012-03-25 20:44:38 +0100357 if ((context->flags &
358 (CONTEXT_RA_ONLY | CONTEXT_RA_NAME | CONTEXT_RA_STATELESS)))
359 {
360 do_slaac = 1;
Simon Kelley4723d492012-03-30 21:04:17 +0100361 if (context->flags & CONTEXT_DHCP)
Simon Kelley05e92e52012-03-30 22:24:15 +0100362 {
363 param->other = 1;
364 if (!(context->flags & CONTEXT_RA_STATELESS))
365 param->managed = 1;
366 }
Simon Kelley30cd9662012-03-25 20:44:38 +0100367 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000368 else
Simon Kelley0010b472012-02-29 12:18:30 +0000369 {
370 /* don't do RA for non-ra-only unless --enable-ra is set */
371 if (!option_bool(OPT_RA))
372 continue;
373 param->managed = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100374 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000375 }
Simon Kelley1f776932012-12-16 19:46:08 +0000376
377 if (context->flags & CONTEXT_CONSTRUCTED)
378 {
379 found_constructed = 1;
380 calc_valid = context->valid;
381 calc_preferred = context->preferred;
382 if (context->valid != -1)
383 calc_valid -= (int)param->now;
384 if (context->preferred != -1)
385 calc_preferred -= (int)param->now;
386 }
387
Simon Kelleyc8257542012-03-28 21:15:41 +0100388 /* find floor time */
389 if (time > context->lease_time)
390 time = context->lease_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000391
Simon Kelleyc8257542012-03-28 21:15:41 +0100392 if (context->flags & CONTEXT_DEPRECATE)
393 deprecate = 1;
394
Simon Kelley18f0fb02012-03-31 21:18:55 +0100395 /* collect dhcp-range tags */
396 if (context->netid.next == &context->netid && context->netid.net)
397 {
398 context->netid.next = param->tags;
399 param->tags = &context->netid;
400 }
401
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100402 /* subsequent prefixes on the same interface
403 and subsequent instances of this prefix don't need timers.
404 Be careful not to find the same prefix twice with different
405 addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100406 if (!(context->flags & CONTEXT_RA_DONE))
407 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100408 if (!param->first)
409 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100410 context->flags |= CONTEXT_RA_DONE;
411 do_prefix = 1;
412 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100413
414 param->first = 0;
415 param->found_context = 1;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000416 }
Simon Kelley1f776932012-12-16 19:46:08 +0000417
418 if (!found_constructed)
419 {
420 calc_valid = time;
421 calc_preferred = deprecate ? 0 : time;
422 }
Simon Kelley1e02a852012-03-29 11:07:25 +0100423
424 if (do_prefix)
Simon Kelleyc8257542012-03-28 21:15:41 +0100425 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100426 struct prefix_opt *opt;
427
428 if ((opt = expand(sizeof(struct prefix_opt))))
429 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100430 /* zero net part of address */
431 setaddr6part(local, addr6part(local) & ~((prefix == 64) ? (u64)-1LL : (1LLU << (128 - prefix)) - 1LLU));
432
Simon Kelley1e02a852012-03-29 11:07:25 +0100433 /* lifetimes must be min 2 hrs, by RFC 2462 */
434 if (time < 7200)
435 time = 7200;
436
437 opt->type = ICMP6_OPT_PREFIX;
438 opt->len = 4;
439 opt->prefix_len = prefix;
Simon Kelleyfd05f122012-08-12 17:48:50 +0100440 /* autonomous only if we're not doing dhcp, always set "on-link" */
441 opt->flags = do_slaac ? 0xC0 : 0x80;
Simon Kelley1f776932012-12-16 19:46:08 +0000442 opt->valid_lifetime = htonl(calc_valid);
443 opt->preferred_lifetime = htonl(calc_preferred);
Simon Kelley5ef33272012-03-30 15:10:28 +0100444 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100445 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100446
Simon Kelley5ef33272012-03-30 15:10:28 +0100447 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley1e02a852012-03-29 11:07:25 +0100448 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
449 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100450 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000451 }
452 }
453 return 1;
454}
455
456static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
457{
458 (void)type;
459
460 if (index == *((int *)parm))
461 {
462 /* size is in units of 8 octets and includes type and length (2 bytes)
463 add 7 to round up */
464 int len = (maclen + 9) >> 3;
465 unsigned char *p = expand(len << 3);
466 memset(p, 0, len << 3);
467 *p++ = ICMP6_OPT_SOURCE_MAC;
468 *p++ = len;
469 memcpy(p, mac, maclen);
470
471 return 0;
472 }
473
474 return 1;
475}
476
477time_t periodic_ra(time_t now)
478{
479 struct search_param param;
480 struct dhcp_context *context;
481 time_t next_event;
482 char interface[IF_NAMESIZE+1];
483
484 param.now = now;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000485 param.iface = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000486
487 while (1)
488 {
489 /* find overdue events, and time of first future event */
Simon Kelley1f776932012-12-16 19:46:08 +0000490 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000491 if (context->ra_time != 0)
492 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100493 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000494 break; /* overdue */
495
Simon Kelley7dbe9812012-03-25 14:49:54 +0100496 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
497 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000498 }
499
500 /* none overdue */
501 if (!context)
502 break;
503
504 /* There's a context overdue, but we can't find an interface
505 associated with it, because it's for a subnet we dont
506 have an interface on. Probably we're doing DHCP on
507 a remote subnet via a relay. Zero the timer, since we won't
508 ever be able to send ra's and satistfy it. */
509 if (iface_enumerate(AF_INET6, &param, iface_search))
510 context->ra_time = 0;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000511 else if (param.iface != 0 &&
512 indextoname(daemon->icmp6fd, param.iface, interface) &&
Simon Kelleyf7fe3622012-12-04 20:55:54 +0000513 iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelley421594f2012-12-02 12:17:35 +0000514 {
515 struct iname *tmp;
516 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
517 if (tmp->name && (strcmp(tmp->name, interface) == 0))
518 break;
519 if (!tmp)
Simon Kelley1f776932012-12-16 19:46:08 +0000520 send_ra(now, param.iface, interface, NULL);
Simon Kelley421594f2012-12-02 12:17:35 +0000521 }
522 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000523 return next_event;
524}
Simon Kelley421594f2012-12-02 12:17:35 +0000525
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000526static int iface_search(struct in6_addr *local, int prefix,
Simon Kelley1f776932012-12-16 19:46:08 +0000527 int scope, int if_index, int dad,
528 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000529{
530 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000531 struct dhcp_context *context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000532
533 (void)scope;
534 (void)dad;
Simon Kelley1f776932012-12-16 19:46:08 +0000535 (void)preferred;
536 (void)valid;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000537
Simon Kelley1f776932012-12-16 19:46:08 +0000538 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000539 if (prefix == context->prefix &&
540 is_same_net6(local, &context->start6, prefix) &&
541 is_same_net6(local, &context->end6, prefix))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000542 if (context->ra_time != 0 && difftime(context->ra_time, param->now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000543 {
544 /* found an interface that's overdue for RA determine new
Simon Kelley29d28dd2012-12-03 14:05:59 +0000545 timeout value and arrange for RA to be sent unless interface is
546 still doing DAD.*/
547
548 if (!dad)
549 param->iface = if_index;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000550
551 if (difftime(param->now, ra_short_period_start) < 60.0)
552 /* range 5 - 20 */
553 context->ra_time = param->now + 5 + (rand16()/4400);
554 else
555 /* range 450 - 600 */
556 context->ra_time = param->now + 450 + (rand16()/440);
557
Simon Kelley29d28dd2012-12-03 14:05:59 +0000558 /* zero timers for other contexts on the same subnet, so they don't timeout
559 independently */
560 for (context = context->next; context; context = context->next)
561 if (prefix == context->prefix &&
562 is_same_net6(local, &context->start6, prefix) &&
563 is_same_net6(local, &context->end6, prefix))
564 context->ra_time = 0;
565
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000566 return 0; /* found, abort */
567 }
568
569 return 1; /* keep searching */
570}
571
Simon Kelley801ca9a2012-03-06 19:30:17 +0000572
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000573#endif