d6a2158257bac23b8c2dc4ebb694b71e66d2eaa9
[project/odhcp6c.git] / src / ra.c
1 /**
2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16 #include <fcntl.h>
17 #include <stdio.h>
18 #include <signal.h>
19 #include <string.h>
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include <syslog.h>
23 #include <unistd.h>
24 #include <resolv.h>
25 #include <alloca.h>
26
27 #include <net/if.h>
28 #include <arpa/inet.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <netinet/icmp6.h>
33
34 #include <linux/rtnetlink.h>
35
36 #ifndef SOL_NETLINK
37 #define SOL_NETLINK 270
38 #endif
39
40 #ifndef NETLINK_ADD_MEMBERSHIP
41 #define NETLINK_ADD_MEMBERSHIP 1
42 #endif
43
44 #ifndef IFF_LOWER_UP
45 #define IFF_LOWER_UP 0x10000
46 #endif
47
48 #include "odhcp6c.h"
49 #include "ra.h"
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 static unsigned int ra_options = 0;
59
60 struct {
61 struct icmp6_hdr hdr;
62 struct icmpv6_opt lladdr;
63 } rs = {
64 .hdr = {ND_ROUTER_SOLICIT, 0, 0, {{0}}},
65 .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
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, unsigned int options)
71 {
72 ra_options = options;
73
74 const pid_t ourpid = getpid();
75 sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
76 if (sock < 0)
77 return -1;
78
79 if_index = if_nametoindex(ifname);
80 if (!if_index)
81 return -1;
82
83 strncpy(if_name, ifname, sizeof(if_name) - 1);
84 lladdr = *ifid;
85
86 rtnl = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
87 if (rtnl < 0)
88 return -1;
89
90 struct sockaddr_nl rtnl_kernel = { .nl_family = AF_NETLINK };
91 if (connect(rtnl, (const struct sockaddr*)&rtnl_kernel, sizeof(rtnl_kernel)) < 0)
92 return -1;
93
94 int val = RTNLGRP_LINK;
95 setsockopt(rtnl, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &val, sizeof(val));
96 fcntl(rtnl, F_SETOWN, ourpid);
97 fcntl(rtnl, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
98
99 struct {
100 struct nlmsghdr hdr;
101 struct ifinfomsg ifi;
102 } req = {
103 .hdr = {sizeof(req), RTM_GETLINK, NLM_F_REQUEST, 1, 0},
104 .ifi = {.ifi_index = if_index}
105 };
106 send(rtnl, &req, sizeof(req), 0);
107 ra_link_up();
108
109 // Filter ICMPv6 package types
110 struct icmp6_filter filt;
111 ICMP6_FILTER_SETBLOCKALL(&filt);
112 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
113 setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
114
115 // Bind to all-nodes
116 struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
117 setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an));
118
119 // Let the kernel compute our checksums
120 val = 2;
121 setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
122
123 // This is required by RFC 4861
124 val = 255;
125 setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
126
127 // Receive multicast hops
128 val = 1;
129 setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val));
130
131 // Bind to one device
132 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
133
134 // Add async-mode
135 fcntl(sock, F_SETOWN, ourpid);
136 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
137
138 // Send RS
139 signal(SIGALRM, ra_send_rs);
140 ra_send_rs(SIGALRM);
141
142 return 0;
143 }
144
145 static void ra_send_rs(int signal __attribute__((unused)))
146 {
147 const struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
148 const struct icmpv6_opt llnull = {ND_OPT_SOURCE_LINKADDR, 1, {0}};
149 size_t len;
150
151 if ((rs_attempt % 2 == 0) && memcmp(&rs.lladdr, &llnull, sizeof(llnull)))
152 len = sizeof(rs);
153 else
154 len = sizeof(struct icmp6_hdr);
155
156 sendto(sock, &rs, len, MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest));
157
158 if (++rs_attempt <= 3)
159 alarm(4);
160 }
161
162 static int16_t pref_to_priority(uint8_t flags)
163 {
164 flags = (flags >> 3) & 0x03;
165
166 return (flags == 0x0) ? 512 : (flags == 0x1) ? 384 :
167 (flags == 0x3) ? 640 : -1;
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 bool ret = false;
179 ssize_t read;
180
181 do {
182 read = recv(rtnl, &resp, sizeof(resp), MSG_DONTWAIT);
183
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
245 if (newvalue > 0)
246 value = newvalue;
247
248 return value;
249 }
250
251 int ra_conf_mtu(int newvalue)
252 {
253 static int value = 0;
254
255 if (newvalue >= 1280 && newvalue <= 65535)
256 value = newvalue;
257
258 return value;
259 }
260
261 int ra_conf_reachable(int newvalue)
262 {
263 static int value = 0;
264
265 if (newvalue > 0 && newvalue <= 3600000)
266 value = newvalue;
267
268 return value;
269 }
270
271 int ra_conf_retransmit(int newvalue)
272 {
273 static int value = 0;
274
275 if (newvalue > 0 && newvalue <= 60000)
276 value = newvalue;
277
278 return value;
279 }
280
281 bool ra_process(void)
282 {
283 bool found = false;
284 bool changed = false;
285 uint8_t buf[1500] _aligned(4);
286 union {
287 struct cmsghdr hdr;
288 uint8_t buf[CMSG_SPACE(sizeof(int))];
289 } cmsg_buf;
290 struct nd_router_advert *adv = (struct nd_router_advert*)buf;
291 struct odhcp6c_entry *entry = alloca(sizeof(*entry) + 256);
292 const struct in6_addr any = IN6ADDR_ANY_INIT;
293
294 memset(entry, 0, sizeof(*entry));
295
296 if (IN6_IS_ADDR_UNSPECIFIED(&lladdr)) {
297 struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
298 socklen_t alen = sizeof(addr);
299 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
300
301 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
302 !getsockname(sock, (struct sockaddr*)&addr, &alen))
303 lladdr = addr.sin6_addr;
304
305 close(sock);
306 }
307
308 while (true) {
309 struct sockaddr_in6 from;
310 struct iovec iov = {buf, sizeof(buf)};
311 struct msghdr msg = {
312 .msg_name = (void *) &from,
313 .msg_namelen = sizeof(from),
314 .msg_iov = &iov,
315 .msg_iovlen = 1,
316 .msg_control = cmsg_buf.buf,
317 .msg_controllen = sizeof(cmsg_buf),
318 .msg_flags = 0
319 };
320
321 ssize_t len = recvmsg(sock, &msg, MSG_DONTWAIT);
322 if (len <= 0)
323 break;
324
325 if (IN6_IS_ADDR_UNSPECIFIED(&lladdr))
326 continue;
327
328 int hlim = 0;
329 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL;
330 ch = CMSG_NXTHDR(&msg, ch))
331 if (ch->cmsg_level == IPPROTO_IPV6 &&
332 ch->cmsg_type == IPV6_HOPLIMIT)
333 memcpy(&hlim, CMSG_DATA(ch), sizeof(hlim));
334
335 if (!ra_icmpv6_valid(&from, hlim, buf, len))
336 continue;
337
338 // Stop sending solicits
339 if (rs_attempt > 0) {
340 alarm(0);
341 rs_attempt = 0;
342 }
343
344 if (!found) {
345 odhcp6c_expire();
346 found = true;
347 }
348 uint32_t router_valid = ntohs(adv->nd_ra_router_lifetime);
349
350 // Parse default route
351 entry->target = any;
352 entry->length = 0;
353 entry->router = from.sin6_addr;
354 entry->priority = pref_to_priority(adv->nd_ra_flags_reserved);
355 if (entry->priority < 0)
356 entry->priority = pref_to_priority(0);
357
358 entry->valid = router_valid;
359 entry->preferred = entry->valid;
360 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 0, true);
361
362 // Parse hoplimit
363 ra_conf_hoplimit(adv->nd_ra_curhoplimit);
364
365 // Parse ND parameters
366 ra_conf_reachable(ntohl(adv->nd_ra_reachable));
367 ra_conf_retransmit(ntohl(adv->nd_ra_retransmit));
368
369 // Evaluate options
370 struct icmpv6_opt *opt;
371 icmpv6_for_each_option(opt, &adv[1], &buf[len]) {
372 if (opt->type == ND_OPT_MTU) {
373 uint32_t *mtu = (uint32_t*)&opt->data[2];
374 ra_conf_mtu(ntohl(*mtu));
375 } else if (opt->type == ND_OPT_ROUTE_INFORMATION && opt->len <= 3) {
376 entry->router = from.sin6_addr;
377 entry->target = any;
378 entry->priority = pref_to_priority(opt->data[1]);
379 entry->length = opt->data[0];
380 uint32_t *valid = (uint32_t*)&opt->data[2];
381 entry->valid = ntohl(*valid);
382 memcpy(&entry->target, &opt->data[6], (opt->len - 1) * 8);
383
384 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
385 || IN6_IS_ADDR_LOOPBACK(&entry->target)
386 || IN6_IS_ADDR_MULTICAST(&entry->target))
387 continue;
388
389 if (entry->priority > 0)
390 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 0, true);
391 } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
392 struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
393 entry->router = any;
394 entry->target = pinfo->nd_opt_pi_prefix;
395 entry->priority = 256;
396 entry->length = pinfo->nd_opt_pi_prefix_len;
397 entry->valid = ntohl(pinfo->nd_opt_pi_valid_time);
398 entry->preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
399
400 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
401 || IN6_IS_ADDR_LOOPBACK(&entry->target)
402 || IN6_IS_ADDR_MULTICAST(&entry->target)
403 || entry->valid < entry->preferred)
404 continue;
405
406 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
407 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 7200, true);
408
409 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
410 pinfo->nd_opt_pi_prefix_len != 64)
411 continue;
412
413 entry->target.s6_addr32[2] = lladdr.s6_addr32[2];
414 entry->target.s6_addr32[3] = lladdr.s6_addr32[3];
415
416 changed |= odhcp6c_update_entry(STATE_RA_PREFIX, entry, 7200, true);
417 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 2) {
418 entry->router = from.sin6_addr;
419 entry->priority = 0;
420 entry->length = 128;
421 uint32_t *valid = (uint32_t*)&opt->data[2];
422 entry->valid = ntohl(*valid);
423 entry->preferred = 0;
424
425 for (ssize_t i = 0; i < (opt->len - 1) / 2; ++i) {
426 memcpy(&entry->target, &opt->data[6 + i * sizeof(entry->target)],
427 sizeof(entry->target));
428 changed |= odhcp6c_update_entry(STATE_RA_DNS, entry, 0, true);
429 }
430 } else if (opt->type == ND_OPT_DNSSL && opt->len > 1) {
431 uint32_t *valid = (uint32_t*)&opt->data[2];
432 uint8_t *buf = &opt->data[6];
433 uint8_t *end = &buf[(opt->len - 1) * 8];
434
435 entry->router = from.sin6_addr;
436 entry->valid = ntohl(*valid);
437
438 while (buf < end) {
439 int len = dn_expand(buf, end, buf, (char*)entry->auxtarget, 256);
440 if (len < 1)
441 break;
442
443 buf = &buf[len];
444 entry->auxlen = strlen((char*)entry->auxtarget);
445
446 if (entry->auxlen == 0)
447 continue;
448
449 changed |= odhcp6c_update_entry(STATE_RA_SEARCH, entry, 0, true);
450 entry->auxlen = 0;
451 }
452 }
453 }
454
455 if (ra_options & RA_RDNSS_DEFAULT_LIFETIME) {
456 int states[2] = {STATE_RA_DNS, STATE_RA_SEARCH};
457
458 for (size_t i = 0; i < 2; ++i) {
459 size_t ra_dns_len;
460 uint8_t *start = odhcp6c_get_state(states[i], &ra_dns_len);
461
462 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
463 (uint8_t*)c < &start[ra_dns_len] &&
464 (uint8_t*)odhcp6c_next_entry(c) <= &start[ra_dns_len];
465 c = odhcp6c_next_entry(c)) {
466 if (IN6_ARE_ADDR_EQUAL(&c->router, &from.sin6_addr) &&
467 c->valid > router_valid)
468 c->valid = router_valid;
469 }
470 }
471 }
472 }
473
474 if (found)
475 odhcp6c_expire();
476
477 return found && changed;
478 }