2 * Copyright (C) 2012-2013 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.
30 #include <arpa/inet.h>
32 #include <netinet/ip6.h>
33 #include <netpacket/packet.h>
34 #include <linux/netlink.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/epoll.h>
39 #include <sys/types.h>
41 #include <sys/syscall.h>
43 #include <libubox/uloop.h>
48 static int ioctl_sock;
49 static int urandom_fd = -1;
51 static void sighandler(_unused int signal)
57 static void print_usage(const char *app)
61 " -h, --help Print this help\n"
62 " -l level Specify log level 0..7 (default %d)\n",
68 int main(int argc, char **argv)
70 openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
73 while ((opt = getopt(argc, argv, "hl:")) != -1) {
79 config.log_level = (atoi(optarg) & LOG_PRIMASK);
80 fprintf(stderr, "Log level set to %d\n", config.log_level);
84 setlogmask(LOG_UPTO(config.log_level));
88 syslog(LOG_ERR, "Must be run as root!");
92 ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
94 if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
97 signal(SIGUSR1, SIG_IGN);
98 signal(SIGINT, sighandler);
99 signal(SIGTERM, sighandler);
113 #ifdef DHCPV4_SUPPORT
123 /* Read IPv6 MTU for interface */
124 int odhcpd_get_interface_config(const char *ifname, const char *what)
127 const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
128 snprintf(buf, sizeof(buf), sysctl_pattern, ifname, what);
130 int fd = open(buf, O_RDONLY);
134 ssize_t len = read(fd, buf, sizeof(buf) - 1);
145 /* Read IPv6 MAC for interface */
146 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
150 memset(&ifr, 0, sizeof(ifr));
151 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name) - 1);
152 if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
155 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
160 /* Forwards a packet on a specific interface */
161 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
162 struct iovec *iov, size_t iov_len,
163 const struct interface *iface)
165 /* Construct headers */
166 uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
167 struct msghdr msg = {
168 .msg_name = (void *) dest,
169 .msg_namelen = sizeof(*dest),
171 .msg_iovlen = iov_len,
172 .msg_control = cmsg_buf,
173 .msg_controllen = sizeof(cmsg_buf),
177 /* Set control data (define destination interface) */
178 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
179 chdr->cmsg_level = IPPROTO_IPV6;
180 chdr->cmsg_type = IPV6_PKTINFO;
181 chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
182 struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
183 pktinfo->ipi6_ifindex = iface->ifindex;
185 /* Also set scope ID if link-local */
186 if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
187 || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
188 dest->sin6_scope_id = iface->ifindex;
190 char ipbuf[INET6_ADDRSTRLEN];
191 inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
193 ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
195 syslog(LOG_NOTICE, "Failed to send to %s%%%s (%m)",
196 ipbuf, iface->ifname);
198 syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
199 (long)sent, ipbuf, iface->ifname);
204 static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
207 struct sockaddr_in6 addr;
208 socklen_t alen = sizeof(addr);
209 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
214 memset(&addr, 0, sizeof(addr));
215 addr.sin6_family = AF_INET6;
216 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &addr.sin6_addr);
217 addr.sin6_scope_id = ifindex;
219 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
220 !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
221 *lladdr = addr.sin6_addr;
230 * DNS address selection criteria order :
231 * - use IPv6 address with valid lifetime if none is yet selected
232 * - use IPv6 address with a preferred lifetime if the already selected IPv6 address is deprecated
233 * - use an IPv6 ULA address if the already selected IPv6 address is not an ULA address
234 * - use the IPv6 address with the longest preferred lifetime
236 int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr)
238 time_t now = odhcpd_time();
241 for (size_t i = 0; i < iface->addr6_len; ++i) {
242 if (iface->addr6[i].valid <= (uint32_t)now)
250 if (iface->addr6[m].preferred >= (uint32_t)now &&
251 iface->addr6[i].preferred < (uint32_t)now)
254 if (IN6_IS_ADDR_ULA(&iface->addr6[i].addr.in6)) {
255 if (!IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6)) {
259 } else if (IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6))
262 if (iface->addr6[i].preferred > iface->addr6[m].preferred)
267 *addr = iface->addr6[m].addr.in6;
271 return odhcpd_get_linklocal_interface_address(iface->ifindex, addr);
274 struct interface* odhcpd_get_interface_by_index(int ifindex)
276 struct interface *iface;
277 list_for_each_entry(iface, &interfaces, head)
278 if (iface->ifindex == ifindex)
285 struct interface* odhcpd_get_interface_by_name(const char *name)
287 struct interface *iface;
288 list_for_each_entry(iface, &interfaces, head)
289 if (!strcmp(iface->ifname, name))
296 struct interface* odhcpd_get_master_interface(void)
298 struct interface *iface;
299 list_for_each_entry(iface, &interfaces, head)
307 /* Convenience function to receive and do basic validation of packets */
308 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
310 struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
312 uint8_t data_buf[8192], cmsg_buf[128];
314 struct sockaddr_in6 in6;
315 struct sockaddr_in in;
316 struct sockaddr_ll ll;
317 struct sockaddr_nl nl;
322 socklen_t ret_len = sizeof(ret);
323 getsockopt(u->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_len);
326 e->handle_error(e, ret);
335 struct iovec iov = {data_buf, sizeof(data_buf)};
336 struct msghdr msg = {
337 .msg_name = (void *) &addr,
338 .msg_namelen = sizeof(addr),
341 .msg_control = cmsg_buf,
342 .msg_controllen = sizeof(cmsg_buf),
346 ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
355 /* Extract destination interface */
359 struct in6_pktinfo *pktinfo;
360 struct in_pktinfo *pkt4info;
361 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
362 if (ch->cmsg_level == IPPROTO_IPV6 &&
363 ch->cmsg_type == IPV6_PKTINFO) {
364 pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
365 destiface = pktinfo->ipi6_ifindex;
366 dest = &pktinfo->ipi6_addr;
367 } else if (ch->cmsg_level == IPPROTO_IP &&
368 ch->cmsg_type == IP_PKTINFO) {
369 pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
370 destiface = pkt4info->ipi_ifindex;
371 dest = &pkt4info->ipi_addr;
372 } else if (ch->cmsg_level == IPPROTO_IPV6 &&
373 ch->cmsg_type == IPV6_HOPLIMIT) {
374 hlim = (int*)CMSG_DATA(ch);
378 /* Check hoplimit if received */
379 if (hlim && *hlim != 255)
382 /* Detect interface for packet sockets */
383 if (addr.ll.sll_family == AF_PACKET)
384 destiface = addr.ll.sll_ifindex;
386 char ipbuf[INET6_ADDRSTRLEN] = "kernel";
387 if (addr.ll.sll_family == AF_PACKET &&
388 len >= (ssize_t)sizeof(struct ip6_hdr))
389 inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
390 else if (addr.in6.sin6_family == AF_INET6)
391 inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
392 else if (addr.in.sin_family == AF_INET)
393 inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
396 if (addr.nl.nl_family == AF_NETLINK) {
397 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
399 e->handle_dgram(&addr, data_buf, len, NULL, dest);
401 } else if (destiface != 0) {
402 struct interface *iface;
403 list_for_each_entry(iface, &interfaces, head) {
404 if (iface->ifindex != destiface)
407 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
408 ipbuf, iface->ifname);
410 e->handle_dgram(&addr, data_buf, len, iface, dest);
418 /* Register events for the multiplexer */
419 int odhcpd_register(struct odhcpd_event *event)
421 event->uloop.cb = odhcpd_receive_packets;
422 return uloop_fd_add(&event->uloop, ULOOP_READ |
423 ((event->handle_error) ? ULOOP_ERROR_CB : 0));
426 int odhcpd_deregister(struct odhcpd_event *event)
428 event->uloop.cb = NULL;
429 return uloop_fd_delete(&event->uloop);
432 void odhcpd_process(struct odhcpd_event *event)
434 odhcpd_receive_packets(&event->uloop, 0);
437 int odhcpd_urandom(void *data, size_t len)
439 return read(urandom_fd, data, len);
443 time_t odhcpd_time(void)
446 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
451 static const char hexdigits[] = "0123456789abcdef";
452 static const int8_t hexvals[] = {
453 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
454 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
455 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
456 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
457 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
458 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
459 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
460 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
463 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src)
466 for (c = 0; c < len && src[0] && src[1]; ++c) {
467 int8_t x = (int8_t)*src++;
468 int8_t y = (int8_t)*src++;
469 if (x < 0 || (x = hexvals[x]) < 0
470 || y < 0 || (y = hexvals[y]) < 0)
473 while (((int8_t)*src) < 0 ||
474 (*src && hexvals[(uint8_t)*src] < 0))
482 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
484 for (size_t i = 0; i < len; ++i) {
485 *dst++ = hexdigits[src[i] >> 4];
486 *dst++ = hexdigits[src[i] & 0x0f];
491 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len)
495 snprintf(buf, sizeof(buf), "%02x", mac[0]);
496 for (size_t i = 1, j = 2; i < len && j < sizeof(buf); i++, j += 3)
497 snprintf(buf + j, sizeof(buf) - j, ":%02x", mac[i]);
502 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits)
504 const uint8_t *a = av, *b = bv;
505 size_t bytes = bits / 8;
508 int res = memcmp(a, b, bytes);
509 if (res == 0 && bits > 0)
510 res = (a[bytes] >> (8 - bits)) - (b[bytes] >> (8 - bits));
516 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits)
519 const uint8_t *b = bv;
521 size_t bytes = bits / 8;
526 uint8_t mask = (1 << (8 - bits)) - 1;
527 a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]);
532 int odhcpd_netmask2bitlen(bool inet6, void *mask)
539 for (bits = 0, v6 = mask;
540 bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
543 for (bits = 0, v4 = mask;
544 bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
550 bool odhcpd_bitlen2netmask(bool inet6, unsigned int bits, void *mask)
563 for (unsigned int i = 0; i < sizeof(v6->s6_addr); i++)
565 b = (bits > 8) ? 8 : bits;
566 v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
576 v4->s_addr = bits ? htonl(~((1 << (32 - bits)) - 1)) : 0;