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