Merge branch 'bugfix' into hnet
[project/odhcp6c.git] / src / ra.c
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <signal.h>
4 #include <string.h>
5 #include <stddef.h>
6 #include <stdbool.h>
7 #include <unistd.h>
8
9 #include <net/if.h>
10 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <netinet/icmp6.h>
13
14 #include <linux/rtnetlink.h>
15
16
17 #include "odhcp6c.h"
18 #include "ra.h"
19
20
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;
26
27 static void ra_send_rs(int signal __attribute__((unused)));
28
29 int ra_init(const char *ifname)
30 {
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);
34
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));
40
41 // Bind to all-nodes
42 struct ipv6_mreq an = {ALL_IPV6_NODES, if_index};
43 setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &an, sizeof(an));
44
45 // Let the kernel compute our checksums
46 int val = 2;
47 setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val));
48
49 // This is required by RFC 4861
50 val = 255;
51 setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val, sizeof(val));
52
53 // Bind to one device
54 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
55
56 // Add async-mode
57 const pid_t ourpid = getpid();
58 fcntl(sock, F_SETOWN, ourpid);
59 fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_ASYNC);
60
61 // Get LL-addr
62 FILE *fp = fopen("/proc/net/if_inet6", "r");
63 if (fp) {
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);
68 break;
69 }
70 }
71 fclose(fp);
72 }
73
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)))
78 return -1;
79 uint32_t group = RTNLGRP_IPV6_IFADDR;
80 setsockopt(rtnl_sock, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &group, sizeof(group));
81
82 // Add async-mode
83 fcntl(rtnl_sock, F_SETOWN, ourpid);
84 fcntl(rtnl_sock, F_SETFL, fcntl(rtnl_sock, F_GETFL) | O_ASYNC);
85
86 // Send RS
87 signal(SIGALRM, ra_send_rs);
88 ra_send_rs(SIGALRM);
89
90 return 0;
91 }
92
93
94 static void ra_send_rs(int signal __attribute__((unused)))
95 {
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));
99
100 if (++rs_attempt <= 3)
101 alarm(4);
102 }
103
104
105 static int16_t pref_to_priority(uint8_t flags)
106 {
107 flags = (flags >> 3) & 0x03;
108 return (flags == 0x0) ? 1024 : (flags == 0x1) ? 512 :
109 (flags == 0x3) ? 2048 : -1;
110 }
111
112
113 static void update_proc(const char *sect, const char *opt, uint32_t value)
114 {
115 char buf[64];
116 snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/%s/%s/%s", sect, if_name, opt);
117
118 int fd = open(buf, O_WRONLY);
119 write(fd, buf, snprintf(buf, sizeof(buf), "%u", value));
120 close(fd);
121 }
122
123
124 static bool ra_deduplicate(const struct in6_addr *any, uint8_t length)
125 {
126 struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, length, 0, *any, 0, 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);
132 raise(SIGUSR2);
133 }
134
135 return !!x;
136 }
137
138
139 bool ra_rtnl_process(void)
140 {
141 bool found = false;
142 uint8_t buf[8192];
143 while (true) {
144 ssize_t len = recv(rtnl_sock, buf, sizeof(buf), MSG_DONTWAIT);
145 if (len < 0)
146 break;
147
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)))
155 continue;
156
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);
162
163 if (addr)
164 found |= ra_deduplicate(addr, ifa->ifa_prefixlen);
165 }
166 }
167 return found;
168 }
169
170
171 bool ra_process(void)
172 {
173 bool found = false;
174 uint8_t buf[1500];
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, 0};
177 const struct in6_addr any = IN6ADDR_ANY_INIT;
178 odhcp6c_expire();
179
180 while (true) {
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);
184 if (len < 0)
185 break;
186 else if (len < (ssize_t)sizeof(*adv))
187 continue;
188
189 // Stop sending solicits
190 if (rs_attempt > 0) {
191 alarm(0);
192 rs_attempt = 0;
193 }
194
195 found = true;
196 uint32_t router_valid = ntohs(adv->nd_ra_router_lifetime);
197
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);
206
207 // Parse ND parameters
208 if (adv->nd_ra_reachable)
209 update_proc("neigh", "base_reachable_time_ms", ntohl(adv->nd_ra_reachable));
210
211 if (adv->nd_ra_retransmit)
212 update_proc("neigh", "retrans_time_ms", ntohl(adv->nd_ra_retransmit));
213
214
215 // Evaluate options
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;
222 entry.target = any;
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);
227
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))
231 continue;
232
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;
237 entry.router = any;
238 entry.target = pinfo->nd_opt_pi_prefix;
239 entry.priority = 256;
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);
243
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)
248 continue;
249
250 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
251 odhcp6c_update_entry_safe(STATE_RA_ROUTE, &entry, 7200);
252
253 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
254 pinfo->nd_opt_pi_prefix_len != 64)
255 continue;
256
257 entry.target.s6_addr32[2] = lladdr.s6_addr32[2];
258 entry.target.s6_addr32[3] = lladdr.s6_addr32[3];
259
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;
263 entry.priority = 0;
264 entry.length = 128;
265 entry.valid = ntohl(*((uint32_t*)&opt->data[2]));
266 entry.preferred = 0;
267
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);
272 }
273 }
274 }
275
276 size_t ra_dns_len;
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;
282 }
283
284 odhcp6c_expire();
285 return found;
286 }