Fix logging verbosity
[project/odhcpd.git] / src / odhcpd.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 *
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.
7 *
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.
12 *
13 */
14
15 #include <time.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <resolv.h>
20 #include <getopt.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdbool.h>
27
28 #include <arpa/inet.h>
29 #include <net/if.h>
30 #include <netinet/ip6.h>
31 #include <netpacket/packet.h>
32 #include <linux/rtnetlink.h>
33
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <sys/epoll.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <sys/syscall.h>
40
41 #include <libubox/uloop.h>
42 #include "odhcpd.h"
43
44
45
46 static int ioctl_sock;
47 static int rtnl_socket = -1;
48 static int rtnl_seq = 0;
49 static int urandom_fd = -1;
50
51
52 int main()
53 {
54 openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
55 setlogmask(LOG_UPTO(LOG_INFO));
56 uloop_init();
57
58 if (getuid() != 0) {
59 syslog(LOG_ERR, "Must be run as root!");
60 return 2;
61 }
62
63 ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
64
65 if ((rtnl_socket = odhcpd_open_rtnl()) < 0) {
66 syslog(LOG_ERR, "Unable to open socket: %s", strerror(errno));
67 return 2;
68 }
69
70 if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
71 return 4;
72
73 signal(SIGUSR1, SIG_IGN);
74
75 if (init_router())
76 return 4;
77
78 if (init_dhcpv6())
79 return 4;
80
81 if (init_ndp())
82 return 4;
83
84 if (init_dhcpv4())
85 return 4;
86
87 odhcpd_run();
88 return 0;
89 }
90
91 int odhcpd_open_rtnl(void)
92 {
93 int sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
94
95 // Connect to the kernel netlink interface
96 struct sockaddr_nl nl = {.nl_family = AF_NETLINK};
97 if (connect(sock, (struct sockaddr*)&nl, sizeof(nl))) {
98 syslog(LOG_ERR, "Failed to connect to kernel rtnetlink: %s",
99 strerror(errno));
100 return -1;
101 }
102
103 return sock;
104 }
105
106
107 // Read IPv6 MTU for interface
108 int odhcpd_get_interface_mtu(const char *ifname)
109 {
110 char buf[64];
111 const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/mtu";
112 snprintf(buf, sizeof(buf), sysctl_pattern, ifname);
113
114 int fd = open(buf, O_RDONLY);
115 ssize_t len = read(fd, buf, sizeof(buf) - 1);
116 close(fd);
117
118 if (len < 0)
119 return -1;
120
121
122 buf[len] = 0;
123 return atoi(buf);
124
125 }
126
127
128 // Read IPv6 MAC for interface
129 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
130 {
131 struct ifreq ifr;
132 memset(&ifr, 0, sizeof(ifr));
133 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
134 if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
135 return -1;
136 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
137 return 0;
138 }
139
140
141 // Forwards a packet on a specific interface
142 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
143 struct iovec *iov, size_t iov_len,
144 const struct interface *iface)
145 {
146 // Construct headers
147 uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
148 struct msghdr msg = {(void*)dest, sizeof(*dest), iov, iov_len,
149 cmsg_buf, sizeof(cmsg_buf), 0};
150
151 // Set control data (define destination interface)
152 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
153 chdr->cmsg_level = IPPROTO_IPV6;
154 chdr->cmsg_type = IPV6_PKTINFO;
155 chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
156 struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
157 pktinfo->ipi6_ifindex = iface->ifindex;
158
159 // Also set scope ID if link-local
160 if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
161 || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
162 dest->sin6_scope_id = iface->ifindex;
163
164 // IPV6_PKTINFO doesn't really work for IPv6-raw sockets (bug?)
165 if (dest->sin6_port == 0) {
166 msg.msg_control = NULL;
167 msg.msg_controllen = 0;
168 }
169
170 char ipbuf[INET6_ADDRSTRLEN];
171 inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
172
173 ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
174 if (sent < 0)
175 syslog(LOG_WARNING, "Failed to send to %s%%%s (%s)",
176 ipbuf, iface->ifname, strerror(errno));
177 else
178 syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
179 (long)sent, ipbuf, iface->ifname);
180 return sent;
181 }
182
183
184 // Detect an IPV6-address currently assigned to the given interface
185 ssize_t odhcpd_get_interface_addresses(int ifindex,
186 struct odhcpd_ipaddr *addrs, size_t cnt)
187 {
188 struct {
189 struct nlmsghdr nhm;
190 struct ifaddrmsg ifa;
191 } req = {{sizeof(req), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
192 ++rtnl_seq, 0}, {AF_INET6, 0, 0, 0, ifindex}};
193 if (send(rtnl_socket, &req, sizeof(req), 0) < (ssize_t)sizeof(req))
194 return 0;
195
196 uint8_t buf[8192];
197 ssize_t len = 0, ret = 0;
198
199 for (struct nlmsghdr *nhm = NULL; ; nhm = NLMSG_NEXT(nhm, len)) {
200 while (len < 0 || !NLMSG_OK(nhm, (size_t)len)) {
201 len = recv(rtnl_socket, buf, sizeof(buf), 0);
202 nhm = (struct nlmsghdr*)buf;
203 if (len < 0 || !NLMSG_OK(nhm, (size_t)len)) {
204 if (errno == EINTR)
205 continue;
206 else
207 return ret;
208 }
209 }
210
211 if (nhm->nlmsg_type != RTM_NEWADDR)
212 break;
213
214 // Skip address but keep clearing socket buffer
215 if (ret >= (ssize_t)cnt)
216 continue;
217
218 struct ifaddrmsg *ifa = NLMSG_DATA(nhm);
219 if (ifa->ifa_scope != RT_SCOPE_UNIVERSE ||
220 ifa->ifa_index != (unsigned)ifindex)
221 continue;
222
223 struct rtattr *rta = (struct rtattr*)&ifa[1];
224 size_t alen = NLMSG_PAYLOAD(nhm, sizeof(*ifa));
225 memset(&addrs[ret], 0, sizeof(addrs[ret]));
226 addrs[ret].prefix = ifa->ifa_prefixlen;
227
228 while (RTA_OK(rta, alen)) {
229 if (rta->rta_type == IFA_ADDRESS) {
230 memcpy(&addrs[ret].addr, RTA_DATA(rta),
231 sizeof(struct in6_addr));
232 } else if (rta->rta_type == IFA_CACHEINFO) {
233 struct ifa_cacheinfo *ifc = RTA_DATA(rta);
234 addrs[ret].preferred = ifc->ifa_prefered;
235 addrs[ret].valid = ifc->ifa_valid;
236 }
237
238 rta = RTA_NEXT(rta, alen);
239 }
240
241 if (ifa->ifa_flags & IFA_F_DEPRECATED)
242 addrs[ret].preferred = 0;
243
244 addrs[ret].has_class = false;
245 addrs[ret].class = 0;
246 #ifdef WITH_UBUS
247 struct interface *iface = odhcpd_get_interface_by_index(ifindex);
248 if (iface)
249 addrs[ret].has_class = ubus_get_class(iface->ifname,
250 &addrs[ret].addr, &addrs[ret].class);
251 #endif
252 ++ret;
253 }
254
255 return ret;
256 }
257
258
259 struct interface* odhcpd_get_interface_by_index(int ifindex)
260 {
261 struct interface *iface;
262 list_for_each_entry(iface, &interfaces, head)
263 if (iface->ifindex == ifindex)
264 return iface;
265
266 return NULL;
267 }
268
269
270 struct interface* odhcpd_get_interface_by_name(const char *name)
271 {
272 struct interface *iface;
273 list_for_each_entry(iface, &interfaces, head)
274 if (!strcmp(iface->ifname, name))
275 return iface;
276
277 return NULL;
278 }
279
280
281 struct interface* odhcpd_get_master_interface(void)
282 {
283 struct interface *iface;
284 list_for_each_entry(iface, &interfaces, head)
285 if (iface->master)
286 return iface;
287
288 return NULL;
289 }
290
291
292 // Convenience function to receive and do basic validation of packets
293 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
294 {
295 struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
296
297 uint8_t data_buf[RELAYD_BUFFER_SIZE], cmsg_buf[128];
298 union {
299 struct sockaddr_in6 in6;
300 struct sockaddr_in in;
301 struct sockaddr_ll ll;
302 struct sockaddr_nl nl;
303 } addr;
304
305 while (true) {
306 struct iovec iov = {data_buf, sizeof(data_buf)};
307 struct msghdr msg = {&addr, sizeof(addr), &iov, 1,
308 cmsg_buf, sizeof(cmsg_buf), 0};
309
310 ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
311 if (len < 0) {
312 if (errno == EAGAIN)
313 break;
314 else
315 continue;
316 }
317
318
319 // Extract destination interface
320 int destiface = 0;
321 int *hlim = NULL;
322 struct in6_pktinfo *pktinfo;
323 struct in_pktinfo *pkt4info;
324 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
325 if (ch->cmsg_level == IPPROTO_IPV6 &&
326 ch->cmsg_type == IPV6_PKTINFO) {
327 pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
328 destiface = pktinfo->ipi6_ifindex;
329 } else if (ch->cmsg_level == IPPROTO_IP &&
330 ch->cmsg_type == IP_PKTINFO) {
331 pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
332 destiface = pkt4info->ipi_ifindex;
333 } else if (ch->cmsg_level == IPPROTO_IPV6 &&
334 ch->cmsg_type == IPV6_HOPLIMIT) {
335 hlim = (int*)CMSG_DATA(ch);
336 }
337 }
338
339 // Check hoplimit if received
340 if (hlim && *hlim != 255)
341 continue;
342
343 // Detect interface for packet sockets
344 if (addr.ll.sll_family == AF_PACKET)
345 destiface = addr.ll.sll_ifindex;
346
347 struct interface *iface =
348 odhcpd_get_interface_by_index(destiface);
349
350 if (!iface && addr.nl.nl_family != AF_NETLINK)
351 continue;
352
353 char ipbuf[INET6_ADDRSTRLEN] = "kernel";
354 if (addr.ll.sll_family == AF_PACKET &&
355 len >= (ssize_t)sizeof(struct ip6_hdr))
356 inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
357 else if (addr.in6.sin6_family == AF_INET6)
358 inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
359 else if (addr.in.sin_family == AF_INET)
360 inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
361
362 syslog(LOG_DEBUG, "--");
363 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
364 ipbuf, (iface) ? iface->ifname : "netlink");
365
366 e->handle_dgram(&addr, data_buf, len, iface);
367 }
368 }
369
370 // Register events for the multiplexer
371 int odhcpd_register(struct odhcpd_event *event)
372 {
373 event->uloop.cb = odhcpd_receive_packets;
374 return uloop_fd_add(&event->uloop, ULOOP_READ);
375 }
376
377 void odhcpd_urandom(void *data, size_t len)
378 {
379 read(urandom_fd, data, len);
380 }
381
382
383 time_t odhcpd_time(void)
384 {
385 struct timespec ts;
386 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
387 return ts.tv_sec;
388 }
389
390
391 static const char hexdigits[] = "0123456789abcdef";
392 static const int8_t hexvals[] = {
393 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
394 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
395 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
396 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
397 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
398 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
399 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
400 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
401 };
402
403 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src)
404 {
405 size_t c;
406 for (c = 0; c < len && src[0] && src[1]; ++c) {
407 int8_t x = (int8_t)*src++;
408 int8_t y = (int8_t)*src++;
409 if (x < 0 || (x = hexvals[x]) < 0
410 || y < 0 || (y = hexvals[y]) < 0)
411 return -1;
412 dst[c] = x << 4 | y;
413 while (((int8_t)*src) < 0 ||
414 (*src && hexvals[(uint8_t)*src] < 0))
415 src++;
416 }
417
418 return c;
419 }
420
421
422 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
423 {
424 for (size_t i = 0; i < len; ++i) {
425 *dst++ = hexdigits[src[i] >> 4];
426 *dst++ = hexdigits[src[i] & 0x0f];
427 }
428 *dst = 0;
429 }