]> git.decadent.org.uk Git - odhcp6c.git/blob - src/ra.c
Add RA-handling support
[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 = 1;
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 == 0x00) ? 1024 : (flags == 0x01) ? 512 :
109                         (flags == 0x11) ? 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};
127         struct odhcp6c_entry *x = odhcp6c_find_entry(STATE_RA_PREFIX, &entry);
128         if (x) {
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, len);
149                                         nh = NLMSG_NEXT(nh, len)) {
150                         struct ifaddrmsg *ifa = NLMSG_DATA(nh);
151                         struct in6_addr *addr = NULL;
152                         if (nh->nlmsg_type != RTM_NEWADDR || NLMSG_PAYLOAD(nh, 0) < sizeof(*ifa) ||
153                                         !(ifa->ifa_flags & IFA_F_DADFAILED) ||
154                                         ifa->ifa_index != if_index)
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_LOCAL && 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};
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
197                 // Parse default route
198                 entry.router = from.sin6_addr;
199                 entry.priority = pref_to_priority(adv->nd_ra_flags_reserved);
200                 if (entry.priority < 0)
201                         entry.priority = pref_to_priority(0);
202                 entry.valid = ntohs(adv->nd_ra_router_lifetime);
203                 entry.preferred = entry.valid;
204                 odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
205
206                 // Parse ND parameters
207                 if (adv->nd_ra_reachable)
208                         update_proc("neigh", "base_reachable_time_ms", ntohl(adv->nd_ra_reachable));
209
210                 if (adv->nd_ra_retransmit)
211                         update_proc("neigh", "retrans_time_ms", ntohl(adv->nd_ra_retransmit));
212
213                 // Evaluate options
214                 struct icmpv6_opt *opt;
215                 icmpv6_for_each_option(opt, &adv[1], &buf[len]) {
216                         if (opt->type == ND_OPT_MTU) {
217                                 update_proc("conf", "mtu", ntohl(*((uint32_t*)&opt->data[2])));
218                         } else if (opt->type == ND_OPT_ROUTE_INFORMATION && opt->len <= 3) {
219                                 entry.router = from.sin6_addr;
220                                 entry.target = any;
221                                 entry.priority = pref_to_priority(opt->data[1]);
222                                 entry.length = opt->data[0];
223                                 entry.valid = ntohl(*((uint32_t*)&opt->data[2]));
224                                 memcpy(&entry.target, &opt->data[6], (opt->len - 1) * 8);
225
226                                 if (entry.length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry.target)
227                                                 || IN6_IS_ADDR_LOOPBACK(&entry.target)
228                                                 || IN6_IS_ADDR_MULTICAST(&entry.target))
229                                         continue;
230
231                                 if (entry.priority > 0)
232                                         odhcp6c_update_entry(STATE_RA_ROUTE, &entry);
233                         } else if (opt->type == ND_OPT_PREFIX_INFORMATION && opt->len == 4) {
234                                 struct nd_opt_prefix_info *pinfo = (struct nd_opt_prefix_info*)opt;
235                                 entry.router = any;
236                                 entry.target = pinfo->nd_opt_pi_prefix;
237                                 entry.priority = 0;
238                                 entry.length = pinfo->nd_opt_pi_prefix_len;
239                                 entry.valid = ntohl(pinfo->nd_opt_pi_valid_time);
240                                 entry.preferred = ntohl(pinfo->nd_opt_pi_preferred_time);
241
242                                 if (entry.length > 128 || IN6_IS_ADDR_LINKLOCAL(&entry.target)
243                                                 || IN6_IS_ADDR_LOOPBACK(&entry.target)
244                                                 || IN6_IS_ADDR_MULTICAST(&entry.target)
245                                                 || entry.valid < entry.preferred)
246                                         continue;
247
248                                 if (pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK)
249                                         odhcp6c_update_entry_safe(STATE_RA_ROUTE, &entry, 7201);
250
251                                 if (!(pinfo->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO) ||
252                                                 pinfo->nd_opt_pi_prefix_len != 64)
253                                         continue;
254
255                                 entry.target.s6_addr32[2] = lladdr.s6_addr32[2];
256                                 entry.target.s6_addr32[3] = lladdr.s6_addr32[3];
257
258                                 odhcp6c_update_entry_safe(STATE_RA_PREFIX, &entry, 7201);
259                         }
260
261                 }
262         }
263         return found;
264 }