blob: 668d56f3146b2816e344b1060ab97f0b5205c454 [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 Kelley55b42f62012-12-21 16:53:15 +000034 struct in6_addr link_local, link_global;
35 unsigned int pref_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000036};
37
38struct search_param {
39 time_t now; int iface;
40};
41
Simon Kelley1f776932012-12-16 19:46:08 +000042static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000043static int add_prefixes(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +000044 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +000045 unsigned int preferred, unsigned int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000046static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +000047 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +000048 int prefered, int valid, void *vparam);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000049static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
50
Simon Kelleyc5379c12012-02-24 20:05:52 +000051static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000052
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 */
Simon Kelley6e3dba32012-12-16 21:52:45 +0000100
Simon Kelley353ae4d2012-03-19 20:07:51 +0000101 if (context)
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000102 context->ra_short_period_start = context->ra_time = now;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000103 else
Simon Kelley1f776932012-12-16 19:46:08 +0000104 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000105 if (!(context->flags & CONTEXT_TEMPLATE))
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000106 {
107 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
108 /* re-do frequently for a minute or so, in case the first gets lost. */
109 context->ra_short_period_start = now;
110 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000111}
112
Simon Kelley1f776932012-12-16 19:46:08 +0000113void icmp6_packet(time_t now)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000114{
115 char interface[IF_NAMESIZE+1];
116 ssize_t sz;
117 int if_index = 0;
118 struct cmsghdr *cmptr;
119 struct msghdr msg;
120 union {
121 struct cmsghdr align; /* this ensures alignment */
122 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
123 } control_u;
124 struct sockaddr_in6 from;
Simon Kelley5ef33272012-03-30 15:10:28 +0100125 unsigned char *packet;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000126 struct iname *tmp;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000127
128 /* Note: use outpacket for input buffer */
129 msg.msg_control = control_u.control6;
130 msg.msg_controllen = sizeof(control_u);
131 msg.msg_flags = 0;
132 msg.msg_name = &from;
133 msg.msg_namelen = sizeof(from);
134 msg.msg_iov = &daemon->outpacket;
135 msg.msg_iovlen = 1;
136
137 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
138 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100139
140 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000141
142 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
143 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
144 {
145 union {
146 unsigned char *c;
147 struct in6_pktinfo *p;
148 } p;
149 p.c = CMSG_DATA(cmptr);
150
151 if_index = p.p->ipi6_ifindex;
152 }
153
154 if (!indextoname(daemon->icmp6fd, if_index, interface))
155 return;
156
Simon Kelley4f7b3042012-11-28 21:27:02 +0000157 if (!iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000158 return;
159
160 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
161 if (tmp->name && (strcmp(tmp->name, interface) == 0))
162 return;
163
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100164 if (packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000165 return;
Simon Kelley8bc4cec2012-07-03 21:04:11 +0100166
Simon Kelley5ef33272012-03-30 15:10:28 +0100167 if (packet[0] == ICMP6_ECHO_REPLY)
168 lease_ping_reply(&from.sin6_addr, packet, interface);
169 else if (packet[0] == ND_ROUTER_SOLICIT)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000170 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100171 char *mac = "";
172
173 /* look for link-layer address option for logging */
174 if (sz >= 16 && packet[8] == ICMP6_OPT_SOURCE_MAC && (packet[9] * 8) + 8 <= sz)
175 {
176 print_mac(daemon->namebuff, &packet[10], (packet[9] * 8) - 2);
177 mac = daemon->namebuff;
178 }
179
180 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
Simon Kelleyf632e562012-05-12 15:05:34 +0100181 /* source address may not be valid in solicit request. */
Simon Kelley1f776932012-12-16 19:46:08 +0000182 send_ra(now, if_index, interface, !IN6_IS_ADDR_UNSPECIFIED(&from.sin6_addr) ? &from.sin6_addr : NULL);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000183 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000184}
185
Simon Kelley1f776932012-12-16 19:46:08 +0000186static void send_ra(time_t now, int iface, char *iface_name, struct in6_addr *dest)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000187{
188 struct ra_packet *ra;
189 struct ra_param parm;
190 struct ifreq ifr;
191 struct sockaddr_in6 addr;
192 struct dhcp_context *context;
Simon Kelley18f0fb02012-03-31 21:18:55 +0100193 struct dhcp_netid iface_id;
194 struct dhcp_opt *opt_cfg;
195 int done_dns = 0;
196
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000197 save_counter(0);
198 ra = expand(sizeof(struct ra_packet));
199
Simon Kelley353ae4d2012-03-19 20:07:51 +0000200 ra->type = ND_ROUTER_ADVERT;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000201 ra->code = 0;
Simon Kelleyc5379c12012-02-24 20:05:52 +0000202 ra->hop_limit = hop_limit;
Simon Kelley884a6df2012-03-20 16:20:22 +0000203 ra->flags = 0x00;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000204 ra->lifetime = htons(1800); /* AdvDefaultLifetime*/
205 ra->reachable_time = 0;
206 ra->retrans_time = 0;
207
208 parm.ind = iface;
209 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100210 parm.other = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000211 parm.found_context = 0;
212 parm.if_name = iface_name;
213 parm.first = 1;
Simon Kelley1f776932012-12-16 19:46:08 +0000214 parm.now = now;
Simon Kelley55b42f62012-12-21 16:53:15 +0000215 parm.pref_time = 0;
Simon Kelley1f776932012-12-16 19:46:08 +0000216
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 Kelley1f776932012-12-16 19:46:08 +0000222 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley18f0fb02012-03-31 21:18:55 +0100223 {
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))
Simon Kelley55b42f62012-12-21 16:53:15 +0000270 put_opt6(&parm.link_global, IN6ADDRSZ);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100271 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 */
Simon Kelley55b42f62012-12-21 16:53:15 +0000298 put_opt6(&parm.link_global, IN6ADDRSZ);
Simon Kelley18f0fb02012-03-31 21:18:55 +0100299 }
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,
Simon Kelleybad7b872012-12-20 22:00:39 +0000330 int scope, int if_index, int flags,
Simon Kelley55b42f62012-12-21 16:53:15 +0000331 unsigned int preferred, unsigned int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000332{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000333 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000334
335 (void)scope; /* warning */
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000336
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000337 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 Kelley3bc0d932012-12-28 11:31:44 +0000347 int constructed = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100348 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100349 struct dhcp_context *context;
350
Simon Kelley1f776932012-12-16 19:46:08 +0000351 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000352 if (!(context->flags & CONTEXT_TEMPLATE) &&
353 prefix == context->prefix &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000354 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
Simon Kelley7f035f52012-12-22 21:27:08 +0000377 /* find floor time, don't reduce below RA interval. */
Simon Kelleyc8257542012-03-28 21:15:41 +0100378 if (time > context->lease_time)
Simon Kelley7f035f52012-12-22 21:27:08 +0000379 {
380 time = context->lease_time;
381 if (time < 600u)
382 time = 600;
383 }
384
Simon Kelleyc8257542012-03-28 21:15:41 +0100385 if (context->flags & CONTEXT_DEPRECATE)
386 deprecate = 1;
Simon Kelley3bc0d932012-12-28 11:31:44 +0000387
388 if (context->flags & CONTEXT_CONSTRUCTED)
389 constructed = 1;
390
Simon Kelleyc8257542012-03-28 21:15:41 +0100391
Simon Kelley18f0fb02012-03-31 21:18:55 +0100392 /* collect dhcp-range tags */
393 if (context->netid.next == &context->netid && context->netid.net)
394 {
395 context->netid.next = param->tags;
396 param->tags = &context->netid;
397 }
398
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100399 /* subsequent prefixes on the same interface
400 and subsequent instances of this prefix don't need timers.
401 Be careful not to find the same prefix twice with different
402 addresses. */
Simon Kelley1e02a852012-03-29 11:07:25 +0100403 if (!(context->flags & CONTEXT_RA_DONE))
404 {
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100405 if (!param->first)
406 context->ra_time = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100407 context->flags |= CONTEXT_RA_DONE;
408 do_prefix = 1;
409 }
Simon Kelley5ae34bf2012-06-04 21:14:03 +0100410
411 param->first = 0;
412 param->found_context = 1;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000413 }
Simon Kelley1f776932012-12-16 19:46:08 +0000414
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000415 /* configured time is ceiling */
Simon Kelley3bc0d932012-12-28 11:31:44 +0000416 if (!constructed || valid > time)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000417 valid = time;
418
Simon Kelley3bc0d932012-12-28 11:31:44 +0000419 if (flags & IFACE_DEPRECATED)
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000420 preferred = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100421
Simon Kelley3bc0d932012-12-28 11:31:44 +0000422 if (deprecate)
423 time = 0;
424
425 /* configured time is ceiling */
426 if (!constructed || preferred > time)
427 preferred = time;
428
Simon Kelley55b42f62012-12-21 16:53:15 +0000429 if (preferred > param->pref_time)
430 {
431 param->pref_time = preferred;
432 param->link_global = *local;
433 }
434
Simon Kelley1e02a852012-03-29 11:07:25 +0100435 if (do_prefix)
Simon Kelleyc8257542012-03-28 21:15:41 +0100436 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100437 struct prefix_opt *opt;
438
439 if ((opt = expand(sizeof(struct prefix_opt))))
440 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100441 /* zero net part of address */
442 setaddr6part(local, addr6part(local) & ~((prefix == 64) ? (u64)-1LL : (1LLU << (128 - prefix)) - 1LLU));
443
Simon Kelley1e02a852012-03-29 11:07:25 +0100444 opt->type = ICMP6_OPT_PREFIX;
445 opt->len = 4;
446 opt->prefix_len = prefix;
Simon Kelleyfd05f122012-08-12 17:48:50 +0100447 /* autonomous only if we're not doing dhcp, always set "on-link" */
448 opt->flags = do_slaac ? 0xC0 : 0x80;
Simon Kelleyed8b68a2012-12-21 16:23:26 +0000449 opt->valid_lifetime = htonl(valid);
450 opt->preferred_lifetime = htonl(preferred);
Simon Kelley5ef33272012-03-30 15:10:28 +0100451 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100452 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100453
Simon Kelley5ef33272012-03-30 15:10:28 +0100454 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley1e02a852012-03-29 11:07:25 +0100455 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
456 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100457 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000458 }
459 }
460 return 1;
461}
462
463static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
464{
465 (void)type;
466
467 if (index == *((int *)parm))
468 {
469 /* size is in units of 8 octets and includes type and length (2 bytes)
470 add 7 to round up */
471 int len = (maclen + 9) >> 3;
472 unsigned char *p = expand(len << 3);
473 memset(p, 0, len << 3);
474 *p++ = ICMP6_OPT_SOURCE_MAC;
475 *p++ = len;
476 memcpy(p, mac, maclen);
477
478 return 0;
479 }
480
481 return 1;
482}
483
484time_t periodic_ra(time_t now)
485{
486 struct search_param param;
487 struct dhcp_context *context;
488 time_t next_event;
489 char interface[IF_NAMESIZE+1];
490
491 param.now = now;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000492 param.iface = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000493
494 while (1)
495 {
496 /* find overdue events, and time of first future event */
Simon Kelley1f776932012-12-16 19:46:08 +0000497 for (next_event = 0, context = daemon->dhcp6; context; context = context->next)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000498 if (context->ra_time != 0)
499 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100500 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000501 break; /* overdue */
502
Simon Kelley7dbe9812012-03-25 14:49:54 +0100503 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
504 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000505 }
506
507 /* none overdue */
508 if (!context)
509 break;
510
511 /* There's a context overdue, but we can't find an interface
512 associated with it, because it's for a subnet we dont
513 have an interface on. Probably we're doing DHCP on
514 a remote subnet via a relay. Zero the timer, since we won't
515 ever be able to send ra's and satistfy it. */
516 if (iface_enumerate(AF_INET6, &param, iface_search))
517 context->ra_time = 0;
Simon Kelley29d28dd2012-12-03 14:05:59 +0000518 else if (param.iface != 0 &&
519 indextoname(daemon->icmp6fd, param.iface, interface) &&
Simon Kelleyf7fe3622012-12-04 20:55:54 +0000520 iface_check(AF_LOCAL, NULL, interface, NULL))
Simon Kelley421594f2012-12-02 12:17:35 +0000521 {
522 struct iname *tmp;
523 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
524 if (tmp->name && (strcmp(tmp->name, interface) == 0))
525 break;
526 if (!tmp)
Simon Kelley1f776932012-12-16 19:46:08 +0000527 send_ra(now, param.iface, interface, NULL);
Simon Kelley421594f2012-12-02 12:17:35 +0000528 }
529 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000530 return next_event;
531}
Simon Kelley421594f2012-12-02 12:17:35 +0000532
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000533static int iface_search(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000534 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000535 int preferred, int valid, void *vparam)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000536{
537 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000538 struct dhcp_context *context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000539
540 (void)scope;
Simon Kelley1f776932012-12-16 19:46:08 +0000541 (void)preferred;
542 (void)valid;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000543
Simon Kelley1f776932012-12-16 19:46:08 +0000544 for (context = daemon->dhcp6; context; context = context->next)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000545 if (!(context->flags & CONTEXT_TEMPLATE) &&
546 prefix == context->prefix &&
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000547 is_same_net6(local, &context->start6, prefix) &&
Simon Kelley6e3dba32012-12-16 21:52:45 +0000548 is_same_net6(local, &context->end6, prefix) &&
549 context->ra_time != 0 &&
550 difftime(context->ra_time, param->now) <= 0.0)
551 {
552 /* found an interface that's overdue for RA determine new
553 timeout value and arrange for RA to be sent unless interface is
554 still doing DAD.*/
555
Simon Kelleybad7b872012-12-20 22:00:39 +0000556 if (!(flags & IFACE_TENTATIVE))
Simon Kelley6e3dba32012-12-16 21:52:45 +0000557 param->iface = if_index;
558
Simon Kelley1b75c1e2012-12-18 19:55:25 +0000559 if (difftime(param->now, context->ra_short_period_start) < 60.0)
Simon Kelley6e3dba32012-12-16 21:52:45 +0000560 /* range 5 - 20 */
561 context->ra_time = param->now + 5 + (rand16()/4400);
562 else
563 /* range 450 - 600 */
564 context->ra_time = param->now + 450 + (rand16()/440);
565
566 /* zero timers for other contexts on the same subnet, so they don't timeout
567 independently */
568 for (context = context->next; context; context = context->next)
569 if (prefix == context->prefix &&
570 is_same_net6(local, &context->start6, prefix) &&
571 is_same_net6(local, &context->end6, prefix))
572 context->ra_time = 0;
573
574 return 0; /* found, abort */
575 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000576
577 return 1; /* keep searching */
578}
579
Simon Kelley801ca9a2012-03-06 19:30:17 +0000580
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000581#endif