]> git.decadent.org.uk Git - odhcp6c.git/blob - src/ra.c
Improve handling of DNS search domains
[odhcp6c.git] / src / ra.c
1 /**
2  * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License v2 as published by
6  * the Free Software Foundation.
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  */
14
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <signal.h>
18 #include <string.h>
19 #include <stddef.h>
20 #include <stdbool.h>
21 #include <syslog.h>
22 #include <unistd.h>
23 #include <resolv.h>
24 #include <alloca.h>
25
26 #include <net/if.h>
27 #include <arpa/inet.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <netinet/icmp6.h>
32
33 #include <linux/rtnetlink.h>
34
35 #ifndef SOL_NETLINK
36 #define SOL_NETLINK 270
37 #endif
38
39 #ifndef NETLINK_ADD_MEMBERSHIP
40 #define NETLINK_ADD_MEMBERSHIP 1
41 #endif
42
43 #ifndef IFF_LOWER_UP
44 #define IFF_LOWER_UP 0x10000
45 #endif
46
47 #include "odhcp6c.h"
48 #include "ra.h"
49
50
51 static bool nocarrier = false;
52
53 static int sock = -1, rtnl = -1;
54 static int if_index = 0;
55 static char if_name[IF_NAMESIZE] = {0};
56 static volatile int rs_attempt = 0;
57 static struct in6_addr lladdr = IN6ADDR_ANY_INIT;
58
59 struct {
60         struct icmp6_hdr hdr;
61         struct icmpv6_opt lladdr;
62 } rs = {
63         .hdr = {ND_ROUTER_SOLICIT, 0, 0, {{0}}},
64         .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
65 };
66
67
68 static void ra_send_rs(int signal __attribute__((unused)));
69
70 int ra_init(const char *ifname, const struct in6_addr *ifid)
71 {
72         const pid_t ourpid = getpid();
73         sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
74         if (sock < 0)
75                 return -1;
76
77         if_index = if_nametoindex(ifname);
78         if (!if_index)
79                 return -1;
80
81         strncpy(if_name, ifname, sizeof(if_name) - 1);
82         lladdr = *ifid;
83
84         rtnl = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
85         if (rtnl < 0)
86                 return -1;
87
88         struct sockaddr_nl rtnl_kernel = { .nl_family = AF_NETLINK };
89         if (connect(rtnl, (const struct sockaddr*)&rtnl_kernel, sizeof(rtnl_kernel)) < 0)
90                 return -1;
91
92         int val = RTNLGRP_LINK;
93         setsockopt(rtnl, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &val, sizeof(val));
94         fcntl(rtnl, F_SETOWN, ourpid);
95         fcntl(rtnl, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
96
97         struct {
98                 struct nlmsghdr hdr;
99                 struct ifinfomsg ifi;
100         } req = {
101                 .hdr = {sizeof(req), RTM_GETLINK, NLM_F_REQUEST, 1, 0},
102                 .ifi = {.ifi_index = if_index}
103         };
104         send(rtnl, &req, sizeof(req), 0);
105         ra_link_up();
106
107         // Filter ICMPv6 package types
108         struct icmp6_filter filt;
109         ICMP6_FILTER_SETBLOCKALL(&filt);
110         ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
111         setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
112
113         // Bind to all-nodes
114         struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
115         setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an));
116
117         // Let the kernel compute our checksums
118         val = 2;
119         setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
120
121         // This is required by RFC 4861
122         val = 255;
123         setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
124
125         // Receive multicast hops
126         val = 1;
127         setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val));
128
129         // Bind to one device
130         setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
131
132         // Add async-mode
133         fcntl(sock, F_SETOWN, ourpid);
134         fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
135
136         // Send RS
137         signal(SIGALRM, ra_send_rs);
138         ra_send_rs(SIGALRM);
139
140         return 0;
141 }
142
143
144 static void ra_send_rs(int signal __attribute__((unused)))
145 {
146         const struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
147         const struct icmpv6_opt llnull = {ND_OPT_SOURCE_LINKADDR, 1, {0}};
148         size_t len;
149
150         if ((rs_attempt % 2 == 0) && memcmp(&rs.lladdr, &llnull, sizeof(llnull)))
151                 len = sizeof(rs);
152         else
153                 len = sizeof(struct icmp6_hdr);
154
155         sendto(sock, &rs, len, MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest));
156
157         if (++rs_attempt <= 3)
158                 alarm(4);
159 }
160
161
162 static int16_t pref_to_priority(uint8_t flags)
163 {
164         flags = (flags >> 3) & 0x03;
165         return (flags == 0x0) ? 512 : (flags == 0x1) ? 384 :
166                         (flags == 0x3) ? 640 : -1;
167 }
168
169
170 bool ra_link_up(void)
171 {
172         static bool firstcall = true;
173         struct {
174                 struct nlmsghdr hdr;
175                 struct ifinfomsg msg;
176                 uint8_t pad[4000];
177         } resp;
178
179         bool ret = false;
180         ssize_t read;
181
182         do {
183                 read = recv(rtnl, &resp, sizeof(resp), MSG_DONTWAIT);
184                 if (read < 0 || !NLMSG_OK(&resp.hdr, (size_t)read) ||
185                                 resp.hdr.nlmsg_type != RTM_NEWLINK ||
186                                 resp.msg.ifi_index != if_index)
187                         continue;
188
189                 ssize_t alen = NLMSG_PAYLOAD(&resp.hdr, sizeof(resp.msg));
190                 for (struct rtattr *rta = (struct rtattr*)(resp.pad);
191                                 RTA_OK(rta, alen); rta = RTA_NEXT(rta, alen)) {
192                         if (rta->rta_type == IFLA_ADDRESS &&
193                                         RTA_PAYLOAD(rta) >= sizeof(rs.lladdr.data))
194                                 memcpy(rs.lladdr.data, RTA_DATA(rta), sizeof(rs.lladdr.data));
195                 }
196
197                 bool hascarrier = resp.msg.ifi_flags & IFF_LOWER_UP;
198                 if (!firstcall && nocarrier != !hascarrier)
199                         ret = true;
200
201                 nocarrier = !hascarrier;
202                 firstcall = false;
203         } while (read > 0);
204
205         if (ret) {
206                 syslog(LOG_NOTICE, "carrier => %i event on %s", (int)!nocarrier, if_name);
207
208                 rs_attempt = 0;
209                 ra_send_rs(SIGALRM);
210         }
211
212         return ret;
213 }
214
215 static bool ra_icmpv6_valid(struct sockaddr_in6 *source, int hlim, uint8_t *data, size_t len)
216 {
217         struct icmp6_hdr *hdr = (struct icmp6_hdr*)data;
218         struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
219
220         if (hlim != 255 || len < sizeof(*hdr) || hdr->icmp6_code)
221                 return false;
222
223         switch (hdr->icmp6_type) {
224         case ND_ROUTER_ADVERT:
225                 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
226                         return false;
227
228                 opt = (struct icmpv6_opt*)((struct nd_router_advert*)data + 1);
229                 break;
230
231         default:
232                 return false;
233         }
234
235         icmpv6_for_each_option(opt, opt, end)
236                 ;
237
238         return opt == end;
239 }
240
241 int ra_conf_hoplimit(int newvalue)
242 {
243         static int value = 0;
244         if (newvalue > 0)
245                 value = newvalue;
246         return value;
247 }
248
249 int ra_conf_mtu(int newvalue)
250 {
251         static int value = 0;
252         if (newvalue >= 1280 && newvalue <= 65535)
253                 value = newvalue;
254         return value;
255 }
256
257 int ra_conf_reachable(int newvalue)
258 {
259         static int value = 0;
260         if (newvalue > 0 && newvalue <= 3600000)
261                 value = newvalue;
262         return value;
263 }
264
265 int ra_conf_retransmit(int newvalue)
266 {
267         static int value = 0;
268         if (newvalue > 0 && newvalue <= 60000)
269                 value = newvalue;
270         return value;
271 }
272
273 bool ra_process(void)
274 {
275         bool found = false;
276         bool changed = false;
277         uint8_t buf[1500], cmsg_buf[128];
278         struct nd_router_advert *adv = (struct nd_router_advert*)buf;
279         struct odhcp6c_entry *entry = alloca(sizeof(*entry) + 256);
280         const struct in6_addr any = IN6ADDR_ANY_INIT;
281
282         memset(entry, 0, sizeof(*entry));
283
284         if (IN6_IS_ADDR_UNSPECIFIED(&lladdr)) {
285                 struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
286                 socklen_t alen = sizeof(addr);
287                 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
288
289                 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
290                                 !getsockname(sock, (struct sockaddr*)&addr, &alen))
291                         lladdr = addr.sin6_addr;
292
293                 close(sock);
294         }
295
296         while (true) {
297                 struct sockaddr_in6 from;
298                 struct iovec iov = {buf, sizeof(buf)};
299                 struct msghdr msg = {
300                         .msg_name = (void *) &from,
301                         .msg_namelen = sizeof(from),
302                         .msg_iov = &iov,
303                         .msg_iovlen = 1,
304                         .msg_control = cmsg_buf,
305                         .msg_controllen = sizeof(cmsg_buf),
306                         .msg_flags = 0
307                 };
308
309                 ssize_t len = recvmsg(sock, &msg, MSG_DONTWAIT);
310                 if (len <= 0)
311                         break;
312
313                 if (IN6_IS_ADDR_UNSPECIFIED(&lladdr))
314                         continue;
315
316                 int hlim = 0;
317                 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL;
318                                 ch = CMSG_NXTHDR(&msg, ch))
319                         if (ch->cmsg_level == IPPROTO_IPV6 &&
320                                         ch->cmsg_type == IPV6_HOPLIMIT)
321                                 memcpy(&hlim, CMSG_DATA(ch), sizeof(hlim));
322
323                 if (!ra_icmpv6_valid(&from, hlim, buf, len))
324                         continue;
325
326                 // Stop sending solicits
327                 if (rs_attempt > 0) {
328                         alarm(0);
329                         rs_attempt = 0;
330                 }
331
332                 if (!found) {
333                         odhcp6c_expire();
334                         found = true;
335                 }
336                 uint32_t router_valid = ntohs(adv->nd_ra_router_lifetime);
337
338                 // Parse default route
339                 entry->target = any;
340                 entry->length = 0;
341                 entry->router = from.sin6_addr;
342                 entry->priority = pref_to_priority(adv->nd_ra_flags_reserved);
343                 if (entry->priority < 0)
344                         entry->priority = pref_to_priority(0);
345                 entry->valid = router_valid;
346                 entry->preferred = entry->valid;
347                 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 0, true);
348
349                 // Parse hoplimit
350                 ra_conf_hoplimit(adv->nd_ra_curhoplimit);
351
352                 // Parse ND parameters
353                 ra_conf_reachable(ntohl(adv->nd_ra_reachable));
354                 ra_conf_retransmit(ntohl(adv->nd_ra_retransmit));
355
356                 // Evaluate options
357                 struct icmpv6_opt *opt;
358                 icmpv6_for_each_option(opt, &adv[1], &buf[len]) {
359                         if (opt->type == ND_OPT_MTU) {
360                                 uint32_t *mtu = (uint32_t*)&opt->data[2];
361                                 ra_conf_mtu(ntohl(*mtu));
362                         } else if (opt->type == ND_OPT_ROUTE_INFORMATION && opt->len <= 3) {
363                                 entry->router = from.sin6_addr;
364                                 entry->target = any;
365                                 entry->priority = pref_to_priority(opt->data[1]);
366                                 entry->length = opt->data[0];
367                                 uint32_t *valid = (uint32_t*)&opt->data[2];
368                                 entry->valid = ntohl(*valid);
369                                 memcpy(&entry->target, &opt->data[6], (opt->len - 1) * 8);
370
371                                 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
372                                                 || IN6_IS_ADDR_LOOPBACK(&entry->target)
373                                                 || IN6_IS_ADDR_MULTICAST(&entry->target))
374                                         continue;
375
376                                 if (entry->priority > 0)
377                                         changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 0, true);
378                         } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
379                                 struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
380                                 entry->router = any;
381                                 entry->target = pinfo->nd_opt_pi_prefix;
382                                 entry->priority = 256;
383                                 entry->length = pinfo->nd_opt_pi_prefix_len;
384                                 entry->valid = ntohl(pinfo->nd_opt_pi_valid_time);
385                                 entry->preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
386
387                                 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
388                                                 || IN6_IS_ADDR_LOOPBACK(&entry->target)
389                                                 || IN6_IS_ADDR_MULTICAST(&entry->target)
390                                                 || entry->valid < entry->preferred)
391                                         continue;
392
393                                 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
394                                         changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 7200, true);
395
396                                 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
397                                                 pinfo->nd_opt_pi_prefix_len != 64)
398                                         continue;
399
400                                 entry->target.s6_addr32[2] = lladdr.s6_addr32[2];
401                                 entry->target.s6_addr32[3] = lladdr.s6_addr32[3];
402
403                                 changed |= odhcp6c_update_entry(STATE_RA_PREFIX, entry, 7200, true);
404                         } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 2) {
405                                 entry->router = from.sin6_addr;
406                                 entry->priority = 0;
407                                 entry->length = 128;
408                                 uint32_t *valid = (uint32_t*)&opt->data[2];
409                                 entry->valid = ntohl(*valid);
410                                 entry->preferred = 0;
411
412                                 for (ssize_t i = 0; i < (opt->len - 1) / 2; ++i) {
413                                         memcpy(&entry->target, &opt->data[6 + i * sizeof(entry->target)],
414                                                         sizeof(entry->target));
415                                         changed |= odhcp6c_update_entry(STATE_RA_DNS, entry, 0, true);
416                                 }
417                         } else if (opt->type == ND_OPT_DNSSL && opt->len > 1) {
418                                 uint32_t *valid = (uint32_t*)&opt->data[2];
419                                 uint8_t *buf = &opt->data[6];
420                                 uint8_t *end = &buf[(opt->len - 1) * 8];
421
422                                 entry->router = from.sin6_addr;
423                                 entry->valid = ntohl(*valid);
424
425                                 while (buf < end) {
426                                         int len = dn_expand(buf, end, buf, (char*)entry->auxtarget, 256);
427                                         if (len > 0) {
428                                                 buf = &buf[len];
429                                                 entry->auxlen = strlen((char*)entry->auxtarget);
430                                                 changed |= odhcp6c_update_entry(STATE_RA_SEARCH, entry, 0, true);
431                                                 entry->auxlen = 0;
432                                         } else {
433                                                 break;
434                                         }
435                                 }
436                         }
437                 }
438
439                 int states[2] = {STATE_RA_DNS, STATE_RA_SEARCH};
440                 for (size_t i = 0; i < 2; ++i) {
441                         size_t ra_dns_len;
442                         uint8_t *start = odhcp6c_get_state(states[i], &ra_dns_len);
443                         for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
444                                                 (uint8_t*)c < &start[ra_dns_len] && &c->auxtarget[c->auxlen] <= &start[ra_dns_len];
445                                                 c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen]))
446                                 if (IN6_ARE_ADDR_EQUAL(&c->router, &from.sin6_addr) &&
447                                                 c->valid > router_valid)
448                                         c->valid = router_valid;
449                 }
450         }
451
452         if (found)
453                 odhcp6c_expire();
454
455         return found && changed;
456 }