blob: ea8f90556ad465aea03e71ffea86855486c52922 [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 {
30 int ind, managed, found_context, first;
31 char *if_name;
32 struct in6_addr link_local;
33};
34
35struct search_param {
36 time_t now; int iface;
37};
38
39static void send_ra(int iface, char *iface_name, struct in6_addr *dest);
40static int add_prefixes(struct in6_addr *local, int prefix,
41 int scope, int if_index, int dad, void *vparam);
42static int iface_search(struct in6_addr *local, int prefix,
43 int scope, int if_index, int dad, void *vparam);
44static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm);
45
Simon Kelleyc5379c12012-02-24 20:05:52 +000046static int hop_limit;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000047static time_t ra_short_period_start;
48
49void ra_init(time_t now)
50{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000051 struct icmp6_filter filter;
52 int fd;
53#if defined(IP_TOS) && defined(IPTOS_CLASS_CS6)
54 int class = IPTOS_CLASS_CS6;
55#endif
56 int val = 255; /* radvd uses this value */
Simon Kelley7b6dd882012-03-01 10:26:16 +000057 socklen_t len = sizeof(int);
Simon Kelley353ae4d2012-03-19 20:07:51 +000058 struct dhcp_context *context;
59
Simon Kelley843c96b2012-02-27 17:42:38 +000060 /* ensure this is around even if we're not doing DHCPv6 */
61 expand_buf(&daemon->outpacket, sizeof(struct dhcp_packet));
Simon Kelley353ae4d2012-03-19 20:07:51 +000062
63 /* See if we're guessing SLAAC addresses, if so we need to recieve ping replies */
64 for (context = daemon->ra_contexts; context; context = context->next)
65 if ((context->flags & CONTEXT_RA_NAME))
66 break;
67
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000068 ICMP6_FILTER_SETBLOCKALL(&filter);
69 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
Simon Kelley353ae4d2012-03-19 20:07:51 +000070 if (context)
71 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000072
73 if ((fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) == -1 ||
Simon Kelleyc5379c12012-02-24 20:05:52 +000074 getsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hop_limit, &len) ||
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000075#if defined(IP_TOS) && defined(IPTOS_CLASS_CS6)
76 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
77#endif
78 !fix_fd(fd) ||
79 !set_ipv6pktinfo(fd) ||
80 setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val, sizeof(val)) ||
81 setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val)) ||
82 setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) == -1)
83 die (_("cannot create ICMPv6 socket: %s"), NULL, EC_BADNET);
84
85 daemon->icmp6fd = fd;
86
Simon Kelley353ae4d2012-03-19 20:07:51 +000087 ra_start_unsolicted(now, NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000088}
89
Simon Kelley353ae4d2012-03-19 20:07:51 +000090void ra_start_unsolicted(time_t now, struct dhcp_context *context)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000091{
Simon Kelley353ae4d2012-03-19 20:07:51 +000092 /* 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 +000093 if it's not appropriate to advertise those contexts.
94 This gets re-called on a netlink route-change to re-do the advertisement
95 and pick up new interfaces */
96
Simon Kelley353ae4d2012-03-19 20:07:51 +000097 if (context)
98 context->ra_time = now;
99 else
100 for (context = daemon->ra_contexts; context; context = context->next)
101 context->ra_time = now + (rand16()/13000); /* range 0 - 5 */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000102
Simon Kelley22d904d2012-02-26 20:13:45 +0000103 /* re-do frequently for a minute or so, in case the first gets lost. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000104 ra_short_period_start = now;
105}
106
107void icmp6_packet(void)
108{
109 char interface[IF_NAMESIZE+1];
110 ssize_t sz;
111 int if_index = 0;
112 struct cmsghdr *cmptr;
113 struct msghdr msg;
114 union {
115 struct cmsghdr align; /* this ensures alignment */
116 char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
117 } control_u;
118 struct sockaddr_in6 from;
119 unsigned char *p;
120 char *mac = "";
121 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;
135
136 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
137 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
138 {
139 union {
140 unsigned char *c;
141 struct in6_pktinfo *p;
142 } p;
143 p.c = CMSG_DATA(cmptr);
144
145 if_index = p.p->ipi6_ifindex;
146 }
147
148 if (!indextoname(daemon->icmp6fd, if_index, interface))
149 return;
150
151 if (!iface_check(AF_LOCAL, NULL, interface))
152 return;
153
154 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
155 if (tmp->name && (strcmp(tmp->name, interface) == 0))
156 return;
157
158 /* weird libvirt-inspired access control */
159 for (context = daemon->dhcp6; context; context = context->next)
160 if (!context->interface || strcmp(context->interface, interface) == 0)
161 break;
162
163 if (!context)
164 return;
165
166 p = (unsigned char *)daemon->outpacket.iov_base;
167
Simon Kelley353ae4d2012-03-19 20:07:51 +0000168 if (p[1] != 0)
169 return;
170
171 if (p[0] == ICMP6_ECHO_REPLY)
172 {
173 /* We may be doing RA but not DHCPv4, in which case the lease
174 database may not exist and we have nothing to do anyway */
175 if (daemon->dhcp)
176 lease_ping_reply(&from.sin6_addr, p, interface);
177 return;
178 }
179
180 if (p[0] != ND_ROUTER_SOLICIT)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000181 return;
182
183 /* look for link-layer address option for logging */
184 if (sz >= 16 && p[8] == ICMP6_OPT_SOURCE_MAC && (p[9] * 8) + 8 <= sz)
185 {
186 print_mac(daemon->namebuff, &p[10], (p[9] * 8) - 2);
187 mac = daemon->namebuff;
188 }
189
190 my_syslog(MS_DHCP | LOG_INFO, "RTR-SOLICIT(%s) %s", interface, mac);
191
192 send_ra(if_index, interface, &from.sin6_addr);
193}
194
195static void send_ra(int iface, char *iface_name, struct in6_addr *dest)
196{
197 struct ra_packet *ra;
198 struct ra_param parm;
199 struct ifreq ifr;
200 struct sockaddr_in6 addr;
201 struct dhcp_context *context;
Simon Kelley29689cf2012-03-22 14:01:00 +0000202
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000203 save_counter(0);
204 ra = expand(sizeof(struct ra_packet));
205
Simon Kelley353ae4d2012-03-19 20:07:51 +0000206 ra->type = ND_ROUTER_ADVERT;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000207 ra->code = 0;
Simon Kelleyc5379c12012-02-24 20:05:52 +0000208 ra->hop_limit = hop_limit;
Simon Kelley884a6df2012-03-20 16:20:22 +0000209 ra->flags = 0x00;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000210 ra->lifetime = htons(1800); /* AdvDefaultLifetime*/
211 ra->reachable_time = 0;
212 ra->retrans_time = 0;
213
214 parm.ind = iface;
215 parm.managed = 0;
216 parm.found_context = 0;
217 parm.if_name = iface_name;
218 parm.first = 1;
219
220 for (context = daemon->ra_contexts; context; context = context->next)
221 context->flags &= ~CONTEXT_RA_DONE;
222
223 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
224 !parm.found_context)
225 return;
226
227 strncpy(ifr.ifr_name, iface_name, IF_NAMESIZE);
228
229 if (ioctl(daemon->icmp6fd, SIOCGIFMTU, &ifr) != -1)
230 {
231 put_opt6_char(ICMP6_OPT_MTU);
232 put_opt6_char(1);
233 put_opt6_short(0);
234 put_opt6_long(ifr.ifr_mtu);
235 }
236
237 iface_enumerate(AF_LOCAL, &iface, add_lla);
238
239 /* RDNSS, RFC 6106 */
240 put_opt6_char(ICMP6_OPT_RDNSS);
241 put_opt6_char(3);
242 put_opt6_short(0);
243 put_opt6_long(1800); /* lifetime - twice RA retransmit */
244 put_opt6(&parm.link_local, IN6ADDRSZ);
245
246
247 /* set managed bits unless we're providing only RA on this link */
248 if (parm.managed)
Simon Kelley884a6df2012-03-20 16:20:22 +0000249 ra->flags = 0xc0; /* M flag, managed, O flag, other */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000250
251 /* decide where we're sending */
252 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000253#ifdef HAVE_SOCKADDR_SA_LEN
254 addr.sin6_len = sizeof(struct sockaddr_in6);
255#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000256 addr.sin6_family = AF_INET6;
257 addr.sin6_port = htons(IPPROTO_ICMPV6);
258 if (dest)
259 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000260 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000261 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
262 IN6_IS_ADDR_MC_LINKLOCAL(dest))
263 addr.sin6_scope_id = iface;
264 }
265 else
Simon Kelley22d904d2012-02-26 20:13:45 +0000266 inet_pton(AF_INET6, ALL_HOSTS, &addr.sin6_addr);
267
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000268 send_from(daemon->icmp6fd, 0, daemon->outpacket.iov_base, save_counter(0),
Simon Kelley29689cf2012-03-22 14:01:00 +0000269 (union mysockaddr *)&addr, (struct all_addr *)&parm.link_local, iface, NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000270
271}
272
273static int add_prefixes(struct in6_addr *local, int prefix,
274 int scope, int if_index, int dad, void *vparam)
275{
276 struct dhcp_context *context, *tmp;
277 struct ra_param *param = vparam;
278 struct prefix_opt *opt;
279
280 (void)scope; /* warning */
281 (void)dad;
282
283 if (if_index == param->ind)
284 {
285 if (IN6_IS_ADDR_LINKLOCAL(local))
286 param->link_local = *local;
287 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
288 !IN6_IS_ADDR_LINKLOCAL(local) &&
289 !IN6_IS_ADDR_MULTICAST(local))
290 {
291 for (context = daemon->ra_contexts; context; context = context->next)
292 if (prefix == context->prefix &&
293 is_same_net6(local, &context->start6, prefix) &&
294 is_same_net6(local, &context->end6, prefix))
295 {
Simon Kelley884a6df2012-03-20 16:20:22 +0000296 int do_slaac = 0;
297
298 if (context->flags & CONTEXT_RA_ONLY)
299 do_slaac = 1;
300 else
Simon Kelley0010b472012-02-29 12:18:30 +0000301 {
302 /* don't do RA for non-ra-only unless --enable-ra is set */
303 if (!option_bool(OPT_RA))
304 continue;
305 param->managed = 1;
306 }
307
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000308 if (context->flags & CONTEXT_RA_DONE)
309 continue;
310
311 /* subsequent prefixes on the same interface don't need timers */
312 if (!param->first)
313 context->ra_time = 0;
314 param->first = 0;
315 param->found_context = 1;
316 context->flags |= CONTEXT_RA_DONE;
317
318 /* mark this subnet and duplicates: as done. */
319 for (tmp = context->next; tmp; tmp = tmp->next)
320 if (tmp->prefix == prefix &&
321 is_same_net6(local, &tmp->start6, prefix) &&
322 is_same_net6(local, &tmp->end6, prefix))
323 {
Simon Kelley884a6df2012-03-20 16:20:22 +0000324 /* if any dhcp-range with ra-only on this subnet
325 set the "do_slaac" bit */
326 if (tmp->flags & CONTEXT_RA_ONLY)
327 do_slaac = 1;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000328 tmp->flags |= CONTEXT_RA_DONE;
329 context->ra_time = 0;
330 }
331
332 if ((opt = expand(sizeof(struct prefix_opt))))
333 {
334 u64 addrpart = addr6part(&context->start6);
335 u64 mask = (prefix == 64) ? (u64)-1LL : (1LLU << (128 - prefix)) - 1LLU;
336 unsigned int time = context->lease_time;
337
338 /* lifetimes must be min 2 hrs, by RFC 2462 */
339 if (time < 7200)
340 time = 7200;
341
342 opt->type = ICMP6_OPT_PREFIX;
343 opt->len = 4;
344 opt->prefix_len = prefix;
345 /* autonomous only is we're not doing dhcp */
Simon Kelley884a6df2012-03-20 16:20:22 +0000346 opt->flags = do_slaac ? 0x40 : 0x00;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000347 opt->valid_lifetime = opt->preferred_lifetime = htonl(time);
348 opt->reserved = 0;
349
350 opt->prefix = context->start6;
351 setaddr6part(&opt->prefix, addrpart & ~mask);
352
353 inet_ntop(AF_INET6, &opt->prefix, daemon->addrbuff, ADDRSTRLEN);
354 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
355 }
356 }
357 }
358 }
359 return 1;
360}
361
362static int add_lla(int index, unsigned int type, char *mac, size_t maclen, void *parm)
363{
364 (void)type;
365
366 if (index == *((int *)parm))
367 {
368 /* size is in units of 8 octets and includes type and length (2 bytes)
369 add 7 to round up */
370 int len = (maclen + 9) >> 3;
371 unsigned char *p = expand(len << 3);
372 memset(p, 0, len << 3);
373 *p++ = ICMP6_OPT_SOURCE_MAC;
374 *p++ = len;
375 memcpy(p, mac, maclen);
376
377 return 0;
378 }
379
380 return 1;
381}
382
383time_t periodic_ra(time_t now)
384{
385 struct search_param param;
386 struct dhcp_context *context;
387 time_t next_event;
388 char interface[IF_NAMESIZE+1];
389
390 param.now = now;
391
392 while (1)
393 {
394 /* find overdue events, and time of first future event */
395 for (next_event = 0, context = daemon->ra_contexts; context; context = context->next)
396 if (context->ra_time != 0)
397 {
398 if (difftime(context->ra_time, now) < 0.0)
399 break; /* overdue */
400
401 if (next_event == 0 || difftime(next_event, context->ra_time + 2) > 0.0)
402 next_event = context->ra_time + 2;
403 }
404
405 /* none overdue */
406 if (!context)
407 break;
408
409 /* There's a context overdue, but we can't find an interface
410 associated with it, because it's for a subnet we dont
411 have an interface on. Probably we're doing DHCP on
412 a remote subnet via a relay. Zero the timer, since we won't
413 ever be able to send ra's and satistfy it. */
414 if (iface_enumerate(AF_INET6, &param, iface_search))
415 context->ra_time = 0;
416 else if (indextoname(daemon->icmp6fd, param.iface, interface))
417 send_ra(param.iface, interface, NULL);
418 }
419
420 return next_event;
421}
422
423static int iface_search(struct in6_addr *local, int prefix,
424 int scope, int if_index, int dad, void *vparam)
425{
426 struct search_param *param = vparam;
Simon Kelley741c2952012-02-25 13:09:18 +0000427 struct dhcp_context *context;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000428
429 (void)scope;
430 (void)dad;
431
432 for (context = daemon->ra_contexts; context; context = context->next)
433 if (prefix == context->prefix &&
434 is_same_net6(local, &context->start6, prefix) &&
435 is_same_net6(local, &context->end6, prefix))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000436 if (context->ra_time != 0 && difftime(context->ra_time, param->now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000437 {
438 /* found an interface that's overdue for RA determine new
439 timeout value and zap other contexts on the same interface
440 so they don't timeout independently .*/
441 param->iface = if_index;
442
443 if (difftime(param->now, ra_short_period_start) < 60.0)
444 /* range 5 - 20 */
445 context->ra_time = param->now + 5 + (rand16()/4400);
446 else
447 /* range 450 - 600 */
448 context->ra_time = param->now + 450 + (rand16()/440);
449
450 return 0; /* found, abort */
451 }
452
453 return 1; /* keep searching */
454}
455
Simon Kelley801ca9a2012-03-06 19:30:17 +0000456
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000457#endif