blob: 654bdab96ab75511baf9bf0b3c77f3d574901657 [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;
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;
Simon Kelley0e88d532012-03-28 22:22:05 +010053#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000054 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 Kelley0e88d532012-03-28 22:22:05 +010075#if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +000076 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;
Simon Kelley5ef33272012-03-30 15:10:28 +0100119 unsigned char *packet;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000120 struct iname *tmp;
121 struct dhcp_context *context;
122
123 /* Note: use outpacket for input buffer */
124 msg.msg_control = control_u.control6;
125 msg.msg_controllen = sizeof(control_u);
126 msg.msg_flags = 0;
127 msg.msg_name = &from;
128 msg.msg_namelen = sizeof(from);
129 msg.msg_iov = &daemon->outpacket;
130 msg.msg_iovlen = 1;
131
132 if ((sz = recv_dhcp_packet(daemon->icmp6fd, &msg)) == -1 || sz < 8)
133 return;
Simon Kelley5ef33272012-03-30 15:10:28 +0100134
135 packet = (unsigned char *)daemon->outpacket.iov_base;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000136
137 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
138 if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
139 {
140 union {
141 unsigned char *c;
142 struct in6_pktinfo *p;
143 } p;
144 p.c = CMSG_DATA(cmptr);
145
146 if_index = p.p->ipi6_ifindex;
147 }
148
149 if (!indextoname(daemon->icmp6fd, if_index, interface))
150 return;
151
152 if (!iface_check(AF_LOCAL, NULL, interface))
153 return;
154
155 for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
156 if (tmp->name && (strcmp(tmp->name, interface) == 0))
157 return;
158
159 /* weird libvirt-inspired access control */
160 for (context = daemon->dhcp6; context; context = context->next)
161 if (!context->interface || strcmp(context->interface, interface) == 0)
162 break;
163
Simon Kelley5ef33272012-03-30 15:10:28 +0100164 if (!context || packet[1] != 0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000165 return;
166
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);
181 send_ra(if_index, interface, &from.sin6_addr);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000182 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000183}
184
185static void send_ra(int iface, char *iface_name, struct in6_addr *dest)
186{
187 struct ra_packet *ra;
188 struct ra_param parm;
189 struct ifreq ifr;
190 struct sockaddr_in6 addr;
191 struct dhcp_context *context;
Simon Kelley29689cf2012-03-22 14:01:00 +0000192
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000193 save_counter(0);
194 ra = expand(sizeof(struct ra_packet));
195
Simon Kelley353ae4d2012-03-19 20:07:51 +0000196 ra->type = ND_ROUTER_ADVERT;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000197 ra->code = 0;
Simon Kelleyc5379c12012-02-24 20:05:52 +0000198 ra->hop_limit = hop_limit;
Simon Kelley884a6df2012-03-20 16:20:22 +0000199 ra->flags = 0x00;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000200 ra->lifetime = htons(1800); /* AdvDefaultLifetime*/
201 ra->reachable_time = 0;
202 ra->retrans_time = 0;
203
204 parm.ind = iface;
205 parm.managed = 0;
Simon Kelley30cd9662012-03-25 20:44:38 +0100206 parm.other = 0;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000207 parm.found_context = 0;
208 parm.if_name = iface_name;
209 parm.first = 1;
210
211 for (context = daemon->ra_contexts; context; context = context->next)
212 context->flags &= ~CONTEXT_RA_DONE;
213
214 if (!iface_enumerate(AF_INET6, &parm, add_prefixes) ||
215 !parm.found_context)
216 return;
217
218 strncpy(ifr.ifr_name, iface_name, IF_NAMESIZE);
219
220 if (ioctl(daemon->icmp6fd, SIOCGIFMTU, &ifr) != -1)
221 {
222 put_opt6_char(ICMP6_OPT_MTU);
223 put_opt6_char(1);
224 put_opt6_short(0);
225 put_opt6_long(ifr.ifr_mtu);
226 }
227
228 iface_enumerate(AF_LOCAL, &iface, add_lla);
229
230 /* RDNSS, RFC 6106 */
231 put_opt6_char(ICMP6_OPT_RDNSS);
232 put_opt6_char(3);
233 put_opt6_short(0);
234 put_opt6_long(1800); /* lifetime - twice RA retransmit */
235 put_opt6(&parm.link_local, IN6ADDRSZ);
236
237
238 /* set managed bits unless we're providing only RA on this link */
239 if (parm.managed)
Simon Kelley30cd9662012-03-25 20:44:38 +0100240 ra->flags |= 0x80; /* M flag, managed, */
241 if (parm.other)
242 ra->flags |= 0x40; /* O flag, other */
243
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000244 /* decide where we're sending */
245 memset(&addr, 0, sizeof(addr));
Simon Kelley22d904d2012-02-26 20:13:45 +0000246#ifdef HAVE_SOCKADDR_SA_LEN
247 addr.sin6_len = sizeof(struct sockaddr_in6);
248#endif
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000249 addr.sin6_family = AF_INET6;
250 addr.sin6_port = htons(IPPROTO_ICMPV6);
251 if (dest)
252 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000253 addr.sin6_addr = *dest;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000254 if (IN6_IS_ADDR_LINKLOCAL(dest) ||
255 IN6_IS_ADDR_MC_LINKLOCAL(dest))
256 addr.sin6_scope_id = iface;
257 }
258 else
Simon Kelley22d904d2012-02-26 20:13:45 +0000259 inet_pton(AF_INET6, ALL_HOSTS, &addr.sin6_addr);
260
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000261 send_from(daemon->icmp6fd, 0, daemon->outpacket.iov_base, save_counter(0),
Simon Kelley29689cf2012-03-22 14:01:00 +0000262 (union mysockaddr *)&addr, (struct all_addr *)&parm.link_local, iface, NULL);
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000263
264}
265
266static int add_prefixes(struct in6_addr *local, int prefix,
267 int scope, int if_index, int dad, void *vparam)
268{
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000269 struct ra_param *param = vparam;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000270
271 (void)scope; /* warning */
272 (void)dad;
273
274 if (if_index == param->ind)
275 {
276 if (IN6_IS_ADDR_LINKLOCAL(local))
277 param->link_local = *local;
278 else if (!IN6_IS_ADDR_LOOPBACK(local) &&
279 !IN6_IS_ADDR_LINKLOCAL(local) &&
280 !IN6_IS_ADDR_MULTICAST(local))
281 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100282 int do_prefix = 0;
Simon Kelleyc8257542012-03-28 21:15:41 +0100283 int do_slaac = 0;
284 int deprecate = 0;
285 unsigned int time = 0xffffffff;
Simon Kelley1e02a852012-03-29 11:07:25 +0100286 struct dhcp_context *context;
287
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000288 for (context = daemon->ra_contexts; context; context = context->next)
289 if (prefix == context->prefix &&
290 is_same_net6(local, &context->start6, prefix) &&
291 is_same_net6(local, &context->end6, prefix))
292 {
Simon Kelley30cd9662012-03-25 20:44:38 +0100293 if ((context->flags &
294 (CONTEXT_RA_ONLY | CONTEXT_RA_NAME | CONTEXT_RA_STATELESS)))
295 {
296 do_slaac = 1;
297 if (context->flags & CONTEXT_RA_STATELESS)
298 param->other = 1;
299 }
Simon Kelley884a6df2012-03-20 16:20:22 +0000300 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;
Simon Kelley30cd9662012-03-25 20:44:38 +0100306 param->other = 1;
Simon Kelley0010b472012-02-29 12:18:30 +0000307 }
308
Simon Kelleyc8257542012-03-28 21:15:41 +0100309 /* find floor time */
310 if (time > context->lease_time)
311 time = context->lease_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000312
Simon Kelleyc8257542012-03-28 21:15:41 +0100313 if (context->flags & CONTEXT_DEPRECATE)
314 deprecate = 1;
315
316 /* subsequent prefixes on the same interface
317 and subsequent instances of this prefix don't need timers */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000318 if (!param->first)
319 context->ra_time = 0;
320 param->first = 0;
321 param->found_context = 1;
Simon Kelley30cd9662012-03-25 20:44:38 +0100322
Simon Kelley1e02a852012-03-29 11:07:25 +0100323 if (!(context->flags & CONTEXT_RA_DONE))
324 {
325 context->flags |= CONTEXT_RA_DONE;
326 do_prefix = 1;
327 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000328 }
Simon Kelley1e02a852012-03-29 11:07:25 +0100329
330 if (do_prefix)
Simon Kelleyc8257542012-03-28 21:15:41 +0100331 {
Simon Kelley1e02a852012-03-29 11:07:25 +0100332 struct prefix_opt *opt;
333
334 if ((opt = expand(sizeof(struct prefix_opt))))
335 {
Simon Kelley5ef33272012-03-30 15:10:28 +0100336 /* zero net part of address */
337 setaddr6part(local, addr6part(local) & ~((prefix == 64) ? (u64)-1LL : (1LLU << (128 - prefix)) - 1LLU));
338
Simon Kelley1e02a852012-03-29 11:07:25 +0100339 /* lifetimes must be min 2 hrs, by RFC 2462 */
340 if (time < 7200)
341 time = 7200;
342
343 opt->type = ICMP6_OPT_PREFIX;
344 opt->len = 4;
345 opt->prefix_len = prefix;
Simon Kelley5ef33272012-03-30 15:10:28 +0100346 /* autonomous only if we're not doing dhcp */
Simon Kelley1e02a852012-03-29 11:07:25 +0100347 opt->flags = do_slaac ? 0x40 : 0x00;
348 opt->valid_lifetime = htonl(time);
349 opt->preferred_lifetime = htonl(deprecate ? 0 : time);
Simon Kelley5ef33272012-03-30 15:10:28 +0100350 opt->reserved = 0;
Simon Kelley1e02a852012-03-29 11:07:25 +0100351 opt->prefix = *local;
Simon Kelley1e02a852012-03-29 11:07:25 +0100352
Simon Kelley5ef33272012-03-30 15:10:28 +0100353 inet_ntop(AF_INET6, local, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley1e02a852012-03-29 11:07:25 +0100354 my_syslog(MS_DHCP | LOG_INFO, "RTR-ADVERT(%s) %s", param->if_name, daemon->addrbuff);
355 }
Simon Kelleyc8257542012-03-28 21:15:41 +0100356 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000357 }
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 {
Simon Kelley7dbe9812012-03-25 14:49:54 +0100398 if (difftime(context->ra_time, now) <= 0.0)
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000399 break; /* overdue */
400
Simon Kelley7dbe9812012-03-25 14:49:54 +0100401 if (next_event == 0 || difftime(next_event, context->ra_time) > 0.0)
402 next_event = context->ra_time;
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000403 }
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