2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
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.
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.
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>
33 #include <linux/rtnetlink.h>
36 #define SOL_NETLINK 270
39 #ifndef NETLINK_ADD_MEMBERSHIP
40 #define NETLINK_ADD_MEMBERSHIP 1
44 #define IFF_LOWER_UP 0x10000
51 static bool nocarrier = false;
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;
62 struct icmpv6_opt lladdr;
64 .hdr = {ND_ROUTER_SOLICIT, 0, 0, {{0}}},
65 .lladdr = {ND_OPT_SOURCE_LINKADDR, 1, {0}},
69 static void ra_send_rs(int signal __attribute__((unused)));
71 int ra_init(const char *ifname, const struct in6_addr *ifid, unsigned int options)
75 const pid_t ourpid = getpid();
76 sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
80 if_index = if_nametoindex(ifname);
84 strncpy(if_name, ifname, sizeof(if_name) - 1);
87 rtnl = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
91 struct sockaddr_nl rtnl_kernel = { .nl_family = AF_NETLINK };
92 if (connect(rtnl, (const struct sockaddr*)&rtnl_kernel, sizeof(rtnl_kernel)) < 0)
95 int val = RTNLGRP_LINK;
96 setsockopt(rtnl, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &val, sizeof(val));
97 fcntl(rtnl, F_SETOWN, ourpid);
98 fcntl(rtnl, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
102 struct ifinfomsg ifi;
104 .hdr = {sizeof(req), RTM_GETLINK, NLM_F_REQUEST, 1, 0},
105 .ifi = {.ifi_index = if_index}
107 send(rtnl, &req, sizeof(req), 0);
110 // Filter ICMPv6 package types
111 struct icmp6_filter filt;
112 ICMP6_FILTER_SETBLOCKALL(&filt);
113 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
114 setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
117 struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
118 setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an));
120 // Let the kernel compute our checksums
122 setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
124 // This is required by RFC 4861
126 setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
128 // Receive multicast hops
130 setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val, sizeof(val));
132 // Bind to one device
133 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
136 fcntl(sock, F_SETOWN, ourpid);
137 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
140 signal(SIGALRM, ra_send_rs);
147 static void ra_send_rs(int signal __attribute__((unused)))
149 const struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
150 const struct icmpv6_opt llnull = {ND_OPT_SOURCE_LINKADDR, 1, {0}};
153 if ((rs_attempt % 2 == 0) && memcmp(&rs.lladdr, &llnull, sizeof(llnull)))
156 len = sizeof(struct icmp6_hdr);
158 sendto(sock, &rs, len, MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest));
160 if (++rs_attempt <= 3)
165 static int16_t pref_to_priority(uint8_t flags)
167 flags = (flags >> 3) & 0x03;
168 return (flags == 0x0) ? 512 : (flags == 0x1) ? 384 :
169 (flags == 0x3) ? 640 : -1;
173 bool ra_link_up(void)
175 static bool firstcall = true;
178 struct ifinfomsg msg;
186 read = recv(rtnl, &resp, sizeof(resp), MSG_DONTWAIT);
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)
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));
200 bool hascarrier = resp.msg.ifi_flags & IFF_LOWER_UP;
201 if (!firstcall && nocarrier != !hascarrier)
204 nocarrier = !hascarrier;
209 syslog(LOG_NOTICE, "carrier => %i event on %s", (int)!nocarrier, if_name);
218 static bool ra_icmpv6_valid(struct sockaddr_in6 *source, int hlim, uint8_t *data, size_t len)
220 struct icmp6_hdr *hdr = (struct icmp6_hdr*)data;
221 struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
223 if (hlim != 255 || len < sizeof(*hdr) || hdr->icmp6_code)
226 switch (hdr->icmp6_type) {
227 case ND_ROUTER_ADVERT:
228 if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
231 opt = (struct icmpv6_opt*)((struct nd_router_advert*)data + 1);
238 icmpv6_for_each_option(opt, opt, end)
244 int ra_conf_hoplimit(int newvalue)
246 static int value = 0;
252 int ra_conf_mtu(int newvalue)
254 static int value = 0;
255 if (newvalue >= 1280 && newvalue <= 65535)
260 int ra_conf_reachable(int newvalue)
262 static int value = 0;
263 if (newvalue > 0 && newvalue <= 3600000)
268 int ra_conf_retransmit(int newvalue)
270 static int value = 0;
271 if (newvalue > 0 && newvalue <= 60000)
276 bool ra_process(void)
279 bool changed = false;
280 uint8_t buf[1500] _aligned(4);
283 uint8_t buf[CMSG_SPACE(sizeof(int))];
285 struct nd_router_advert *adv = (struct nd_router_advert*)buf;
286 struct odhcp6c_entry *entry = alloca(sizeof(*entry) + 256);
287 const struct in6_addr any = IN6ADDR_ANY_INIT;
289 memset(entry, 0, sizeof(*entry));
291 if (IN6_IS_ADDR_UNSPECIFIED(&lladdr)) {
292 struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
293 socklen_t alen = sizeof(addr);
294 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
296 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
297 !getsockname(sock, (struct sockaddr*)&addr, &alen))
298 lladdr = addr.sin6_addr;
304 struct sockaddr_in6 from;
305 struct iovec iov = {buf, sizeof(buf)};
306 struct msghdr msg = {
307 .msg_name = (void *) &from,
308 .msg_namelen = sizeof(from),
311 .msg_control = cmsg_buf.buf,
312 .msg_controllen = sizeof(cmsg_buf),
316 ssize_t len = recvmsg(sock, &msg, MSG_DONTWAIT);
320 if (IN6_IS_ADDR_UNSPECIFIED(&lladdr))
324 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL;
325 ch = CMSG_NXTHDR(&msg, ch))
326 if (ch->cmsg_level == IPPROTO_IPV6 &&
327 ch->cmsg_type == IPV6_HOPLIMIT)
328 memcpy(&hlim, CMSG_DATA(ch), sizeof(hlim));
330 if (!ra_icmpv6_valid(&from, hlim, buf, len))
333 // Stop sending solicits
334 if (rs_attempt > 0) {
343 uint32_t router_valid = ntohs(adv->nd_ra_router_lifetime);
345 // Parse default route
348 entry->router = from.sin6_addr;
349 entry->priority = pref_to_priority(adv->nd_ra_flags_reserved);
350 if (entry->priority < 0)
351 entry->priority = pref_to_priority(0);
352 entry->valid = router_valid;
353 entry->preferred = entry->valid;
354 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 0, true);
357 ra_conf_hoplimit(adv->nd_ra_curhoplimit);
359 // Parse ND parameters
360 ra_conf_reachable(ntohl(adv->nd_ra_reachable));
361 ra_conf_retransmit(ntohl(adv->nd_ra_retransmit));
364 struct icmpv6_opt *opt;
365 icmpv6_for_each_option(opt, &adv[1], &buf[len]) {
366 if (opt->type == ND_OPT_MTU) {
367 uint32_t *mtu = (uint32_t*)&opt->data[2];
368 ra_conf_mtu(ntohl(*mtu));
369 } else if (opt->type == ND_OPT_ROUTE_INFORMATION && opt->len <= 3) {
370 entry->router = from.sin6_addr;
372 entry->priority = pref_to_priority(opt->data[1]);
373 entry->length = opt->data[0];
374 uint32_t *valid = (uint32_t*)&opt->data[2];
375 entry->valid = ntohl(*valid);
376 memcpy(&entry->target, &opt->data[6], (opt->len - 1) * 8);
378 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
379 || IN6_IS_ADDR_LOOPBACK(&entry->target)
380 || IN6_IS_ADDR_MULTICAST(&entry->target))
383 if (entry->priority > 0)
384 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 0, true);
385 } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
386 struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
388 entry->target = pinfo->nd_opt_pi_prefix;
389 entry->priority = 256;
390 entry->length = pinfo->nd_opt_pi_prefix_len;
391 entry->valid = ntohl(pinfo->nd_opt_pi_valid_time);
392 entry->preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
394 if (entry->length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry->target)
395 || IN6_IS_ADDR_LOOPBACK(&entry->target)
396 || IN6_IS_ADDR_MULTICAST(&entry->target)
397 || entry->valid < entry->preferred)
400 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
401 changed |= odhcp6c_update_entry(STATE_RA_ROUTE, entry, 7200, true);
403 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
404 pinfo->nd_opt_pi_prefix_len != 64)
407 entry->target.s6_addr32[2] = lladdr.s6_addr32[2];
408 entry->target.s6_addr32[3] = lladdr.s6_addr32[3];
410 changed |= odhcp6c_update_entry(STATE_RA_PREFIX, entry, 7200, true);
411 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 2) {
412 entry->router = from.sin6_addr;
415 uint32_t *valid = (uint32_t*)&opt->data[2];
416 entry->valid = ntohl(*valid);
417 entry->preferred = 0;
419 for (ssize_t i = 0; i < (opt->len - 1) / 2; ++i) {
420 memcpy(&entry->target, &opt->data[6 + i * sizeof(entry->target)],
421 sizeof(entry->target));
422 changed |= odhcp6c_update_entry(STATE_RA_DNS, entry, 0, true);
424 } else if (opt->type == ND_OPT_DNSSL && opt->len > 1) {
425 uint32_t *valid = (uint32_t*)&opt->data[2];
426 uint8_t *buf = &opt->data[6];
427 uint8_t *end = &buf[(opt->len - 1) * 8];
429 entry->router = from.sin6_addr;
430 entry->valid = ntohl(*valid);
433 int len = dn_expand(buf, end, buf, (char*)entry->auxtarget, 256);
438 entry->auxlen = strlen((char*)entry->auxtarget);
440 if (entry->auxlen == 0)
443 changed |= odhcp6c_update_entry(STATE_RA_SEARCH, entry, 0, true);
449 if (ra_options & RA_RDNSS_DEFAULT_LIFETIME) {
450 int states[2] = {STATE_RA_DNS, STATE_RA_SEARCH};
451 for (size_t i = 0; i < 2; ++i) {
453 uint8_t *start = odhcp6c_get_state(states[i], &ra_dns_len);
454 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
455 (uint8_t*)c < &start[ra_dns_len] &&
456 (uint8_t*)odhcp6c_next_entry(c) <= &start[ra_dns_len];
457 c = odhcp6c_next_entry(c))
458 if (IN6_ARE_ADDR_EQUAL(&c->router, &from.sin6_addr) &&
459 c->valid > router_valid)
460 c->valid = router_valid;
468 return found && changed;