10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netinet/icmp6.h>
14 #include <linux/rtnetlink.h>
21 static int sock = -1, rtnl_sock = -1;
22 static unsigned if_index = 0;
23 static char if_name[IF_NAMESIZE] = {0};
24 static volatile int rs_attempt = 0;
25 static struct in6_addr lladdr = IN6ADDR_ANY_INIT;
27 static void ra_send_rs(int signal __attribute__((unused)));
29 int ra_init(const char *ifname)
31 sock = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6);
32 if_index = if_nametoindex(ifname);
33 strncpy(if_name, ifname, sizeof(if_name) - 1);
35 // Filter ICMPv6 package types
36 struct icmp6_filter filt;
37 ICMP6_FILTER_SETBLOCKALL(&filt);
38 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
39 setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt));
42 struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
43 setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an));
45 // Let the kernel compute our checksums
47 setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
49 // This is required by RFC 4861
51 setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
54 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
57 const pid_t ourpid = getpid();
58 fcntl(sock, F_SETOWN, ourpid);
59 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
62 FILE *fp = fopen("/proc/net/if_inet6", "r");
64 char addrbuf[33], ifbuf[16];
65 while (fscanf(fp, "%32s %*x %*x %*x %*x %15s", addrbuf, ifbuf) == 2) {
66 if (!strcmp(ifbuf, if_name)) {
67 script_unhexlify((uint8_t*)&lladdr, sizeof(lladdr), addrbuf);
74 // Open rtnetlink socket
75 rtnl_sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
76 struct sockaddr_nl rtnl_kernel = { .nl_family = AF_NETLINK };
77 if (connect(rtnl_sock, (struct sockaddr*)&rtnl_kernel, sizeof(rtnl_kernel)))
79 uint32_t group = RTNLGRP_IPV6_IFADDR;
80 setsockopt(rtnl_sock, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
83 fcntl(rtnl_sock, F_SETOWN, ourpid);
84 fcntl(rtnl_sock, F_SETFL, fcntl(rtnl_sock, F_GETFL) | O_ASYNC);
87 signal(SIGALRM, ra_send_rs);
94 static void ra_send_rs(int signal __attribute__((unused)))
96 const struct icmp6_hdr rs = {ND_ROUTER_SOLICIT, 0, 0, {{0}}};
97 const struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, if_index};
98 sendto(sock, &rs, sizeof(rs), MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest));
100 if (++rs_attempt <= 3)
105 static int16_t pref_to_priority(uint8_t flags)
107 flags = (flags >> 3) & 0x03;
108 return (flags == 0x0) ? 1024 : (flags == 0x1) ? 512 :
109 (flags == 0x3) ? 2048 : -1;
113 static void update_proc(const char *sect, const char *opt, uint32_t value)
116 snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/%s/%s/%s", sect, if_name, opt);
118 int fd = open(buf, O_WRONLY);
119 write(fd, buf, snprintf(buf, sizeof(buf), "%u", value));
124 static bool ra_deduplicate(const struct in6_addr *any, uint8_t length)
126 struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, length, 0, *any, 0, 0};
127 struct odhcp6c_entry *x = odhcp6c_find_entry(STATE_RA_PREFIX, &entry);
128 if (x && IN6_ARE_ADDR_EQUAL(&x->target, any)) {
129 odhcp6c_random(&x->target.s6_addr32[2], 2 * sizeof(uint32_t));
130 } else if (odhcp6c_find_entry(STATE_IA_NA, &entry)) {
131 dhcpv6_request(DHCPV6_MSG_DECLINE);
139 bool ra_rtnl_process(void)
144 ssize_t len = recv(rtnl_sock, buf, sizeof(buf), MSG_DONTWAIT);
148 for (struct nlmsghdr *nh = (struct nlmsghdr*)buf; NLMSG_OK(nh, (size_t)len);
149 nh = NLMSG_NEXT(nh, len)) {
150 struct ifaddrmsg *ifa = NLMSG_DATA(nh);
151 struct in6_addr *addr = NULL;
152 if (NLMSG_PAYLOAD(nh, 0) < sizeof(*ifa) || ifa->ifa_index != if_index ||
153 (nh->nlmsg_type == RTM_NEWADDR && !(ifa->ifa_flags & IFA_F_DADFAILED)) ||
154 (nh->nlmsg_type == RTM_DELADDR && !(ifa->ifa_flags & IFA_F_TENTATIVE)))
157 ssize_t alen = NLMSG_PAYLOAD(nh, sizeof(*ifa));
158 for (struct rtattr *rta = (struct rtattr*)&ifa[1]; RTA_OK(rta, alen);
159 rta = RTA_NEXT(rta, alen))
160 if (rta->rta_type == IFA_ADDRESS && RTA_PAYLOAD(rta) >= sizeof(*addr))
161 addr = RTA_DATA(rta);
164 found |= ra_deduplicate(addr, ifa->ifa_prefixlen);
171 bool ra_process(void)
175 struct nd_router_advert *adv = (struct nd_router_advert*)buf;
176 struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0, IN6ADDR_ANY_INIT, 0, 0};
177 const struct in6_addr any = IN6ADDR_ANY_INIT;
181 struct sockaddr_in6 from;
182 socklen_t from_len = sizeof(from);
183 ssize_t len = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT, &from, &from_len);
186 else if (len < (ssize_t)sizeof(*adv))
189 // Stop sending solicits
190 if (rs_attempt > 0) {
196 uint32_t router_valid = ntohs(adv->nd_ra_router_lifetime);
198 // Parse default route
199 entry.router = from.sin6_addr;
200 entry.priority = pref_to_priority(adv->nd_ra_flags_reserved);
201 if (entry.priority < 0)
202 entry.priority = pref_to_priority(0);
203 entry.valid = router_valid;
204 entry.preferred = entry.valid;
205 odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
207 // Parse ND parameters
208 if (adv->nd_ra_reachable)
209 update_proc("neigh", "base_reachable_time_ms", ntohl(adv->nd_ra_reachable));
211 if (adv->nd_ra_retransmit)
212 update_proc("neigh", "retrans_time_ms", ntohl(adv->nd_ra_retransmit));
216 struct icmpv6_opt *opt;
217 icmpv6_for_each_option(opt, &adv[1], &buf[len]) {
218 if (opt->type == ND_OPT_MTU) {
219 update_proc("conf", "mtu", ntohl(*((uint32_t*)&opt->data[2])));
220 } else if (opt->type == ND_OPT_ROUTE_INFORMATION && opt->len <= 3) {
221 entry.router = from.sin6_addr;
223 entry.priority = pref_to_priority(opt->data[1]);
224 entry.length = opt->data[0];
225 entry.valid = ntohl(*((uint32_t*)&opt->data[2]));
226 memcpy(&entry.target, &opt->data[6], (opt->len - 1) * 8);
228 if (entry.length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry.target)
229 || IN6_IS_ADDR_LOOPBACK(&entry.target)
230 || IN6_IS_ADDR_MULTICAST(&entry.target))
233 if (entry.priority > 0)
234 odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
235 } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
236 struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
238 entry.target = pinfo->nd_opt_pi_prefix;
240 entry.length = pinfo->nd_opt_pi_prefix_len;
241 entry.valid = ntohl(pinfo->nd_opt_pi_valid_time);
242 entry.preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
244 if (entry.length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry.target)
245 || IN6_IS_ADDR_LOOPBACK(&entry.target)
246 || IN6_IS_ADDR_MULTICAST(&entry.target)
247 || entry.valid < entry.preferred)
250 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
251 odhcp6c_update_entry_safe(STATE_RA_ROUTE, &entry, 7200);
253 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
254 pinfo->nd_opt_pi_prefix_len != 64)
257 entry.target.s6_addr32[2] = lladdr.s6_addr32[2];
258 entry.target.s6_addr32[3] = lladdr.s6_addr32[3];
260 odhcp6c_update_entry_safe(STATE_RA_PREFIX, &entry, 7200);
261 } else if (opt->type == ND_OPT_RECURSIVE_DNS && opt->len > 2) {
262 entry.router = from.sin6_addr;
265 entry.valid = ntohl(*((uint32_t*)&opt->data[2]));
268 for (ssize_t i = 0; i < (opt->len - 1) / 2; ++i) {
269 memcpy(&entry.target, &opt->data[6 + i * sizeof(entry.target)],
270 sizeof(entry.target));
271 odhcp6c_update_entry(STATE_RA_DNS, &entry);
277 struct odhcp6c_entry *entry = odhcp6c_get_state(STATE_RA_DNS, &ra_dns_len);
278 for (size_t i = 0; i < ra_dns_len / sizeof(*entry); ++i)
279 if (IN6_ARE_ADDR_EQUAL(&entry[i].router, &from.sin6_addr) &&
280 entry[i].valid > router_valid)
281 entry[i].valid = router_valid;