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);
97 if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
100 signal(SIGUSR1, SIG_IGN);
101 signal(SIGINT, sighandler);
102 signal(SIGTERM, sighandler);
116 #ifdef DHCPV4_SUPPORT
126 /* Read IPv6 MTU for interface */
127 int odhcpd_get_interface_config(const char *ifname, const char *what)
130 const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
131 snprintf(buf, sizeof(buf), sysctl_pattern, ifname, what);
133 int fd = open(buf, O_RDONLY);
137 ssize_t len = read(fd, buf, sizeof(buf) - 1);
148 /* Read IPv6 MAC for interface */
149 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
153 memset(&ifr, 0, sizeof(ifr));
154 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name) - 1);
155 if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
158 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
163 /* Forwards a packet on a specific interface */
164 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
165 struct iovec *iov, size_t iov_len,
166 const struct interface *iface)
168 /* Construct headers */
169 uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
170 struct msghdr msg = {
171 .msg_name = (void *) dest,
172 .msg_namelen = sizeof(*dest),
174 .msg_iovlen = iov_len,
175 .msg_control = cmsg_buf,
176 .msg_controllen = sizeof(cmsg_buf),
180 /* Set control data (define destination interface) */
181 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
182 chdr->cmsg_level = IPPROTO_IPV6;
183 chdr->cmsg_type = IPV6_PKTINFO;
184 chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
185 struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
186 pktinfo->ipi6_ifindex = iface->ifindex;
188 /* Also set scope ID if link-local */
189 if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
190 || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
191 dest->sin6_scope_id = iface->ifindex;
193 char ipbuf[INET6_ADDRSTRLEN];
194 inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
196 ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
198 syslog(LOG_NOTICE, "Failed to send to %s%%%s (%m)",
199 ipbuf, iface->ifname);
201 syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
202 (long)sent, ipbuf, iface->ifname);
207 static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
210 struct sockaddr_in6 addr;
211 socklen_t alen = sizeof(addr);
212 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
217 memset(&addr, 0, sizeof(addr));
218 addr.sin6_family = AF_INET6;
219 inet_pton(AF_INET6, ALL_IPV6_ROUTERS, &addr.sin6_addr);
220 addr.sin6_scope_id = ifindex;
222 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
223 !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
224 *lladdr = addr.sin6_addr;
233 * DNS address selection criteria order :
234 * - use IPv6 address with valid lifetime if none is yet selected
235 * - use IPv6 address with a preferred lifetime if the already selected IPv6 address is deprecated
236 * - use an IPv6 ULA address if the already selected IPv6 address is not an ULA address
237 * - use the IPv6 address with the longest preferred lifetime
239 int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr)
241 time_t now = odhcpd_time();
244 for (size_t i = 0; i < iface->addr6_len; ++i) {
245 if (iface->addr6[i].valid <= (uint32_t)now)
253 if (iface->addr6[m].preferred >= (uint32_t)now &&
254 iface->addr6[i].preferred < (uint32_t)now)
257 if (IN6_IS_ADDR_ULA(&iface->addr6[i].addr.in6)) {
258 if (!IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6)) {
262 } else if (IN6_IS_ADDR_ULA(&iface->addr6[m].addr.in6))
265 if (iface->addr6[i].preferred > iface->addr6[m].preferred)
270 *addr = iface->addr6[m].addr.in6;
274 return odhcpd_get_linklocal_interface_address(iface->ifindex, addr);
277 struct interface* odhcpd_get_interface_by_index(int ifindex)
279 struct interface *iface;
280 list_for_each_entry(iface, &interfaces, head)
281 if (iface->ifindex == ifindex)
288 struct interface* odhcpd_get_interface_by_name(const char *name)
290 struct interface *iface;
291 list_for_each_entry(iface, &interfaces, head)
292 if (!strcmp(iface->ifname, name))
299 struct interface* odhcpd_get_master_interface(void)
301 struct interface *iface;
302 list_for_each_entry(iface, &interfaces, head)
310 /* Convenience function to receive and do basic validation of packets */
311 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
313 struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
315 uint8_t data_buf[8192], cmsg_buf[128];
317 struct sockaddr_in6 in6;
318 struct sockaddr_in in;
319 struct sockaddr_ll ll;
320 struct sockaddr_nl nl;
325 socklen_t ret_len = sizeof(ret);
328 if (e->handle_error && getsockopt(u->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_len) == 0)
329 e->handle_error(e, ret);
338 struct iovec iov = {data_buf, sizeof(data_buf)};
339 struct msghdr msg = {
340 .msg_name = (void *) &addr,
341 .msg_namelen = sizeof(addr),
344 .msg_control = cmsg_buf,
345 .msg_controllen = sizeof(cmsg_buf),
349 ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
358 /* Extract destination interface */
362 struct in6_pktinfo *pktinfo;
363 struct in_pktinfo *pkt4info;
364 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
365 if (ch->cmsg_level == IPPROTO_IPV6 &&
366 ch->cmsg_type == IPV6_PKTINFO) {
367 pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
368 destiface = pktinfo->ipi6_ifindex;
369 dest = &pktinfo->ipi6_addr;
370 } else if (ch->cmsg_level == IPPROTO_IP &&
371 ch->cmsg_type == IP_PKTINFO) {
372 pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
373 destiface = pkt4info->ipi_ifindex;
374 dest = &pkt4info->ipi_addr;
375 } else if (ch->cmsg_level == IPPROTO_IPV6 &&
376 ch->cmsg_type == IPV6_HOPLIMIT) {
377 hlim = (int*)CMSG_DATA(ch);
381 /* Check hoplimit if received */
382 if (hlim && *hlim != 255)
385 /* Detect interface for packet sockets */
386 if (addr.ll.sll_family == AF_PACKET)
387 destiface = addr.ll.sll_ifindex;
389 char ipbuf[INET6_ADDRSTRLEN] = "kernel";
390 if (addr.ll.sll_family == AF_PACKET &&
391 len >= (ssize_t)sizeof(struct ip6_hdr))
392 inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
393 else if (addr.in6.sin6_family == AF_INET6)
394 inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
395 else if (addr.in.sin_family == AF_INET)
396 inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
399 if (addr.nl.nl_family == AF_NETLINK) {
400 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
402 e->handle_dgram(&addr, data_buf, len, NULL, dest);
404 } else if (destiface != 0) {
405 struct interface *iface;
406 list_for_each_entry(iface, &interfaces, head) {
407 if (iface->ifindex != destiface)
410 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
411 ipbuf, iface->ifname);
413 e->handle_dgram(&addr, data_buf, len, iface, dest);
421 /* Register events for the multiplexer */
422 int odhcpd_register(struct odhcpd_event *event)
424 event->uloop.cb = odhcpd_receive_packets;
425 return uloop_fd_add(&event->uloop, ULOOP_READ |
426 ((event->handle_error) ? ULOOP_ERROR_CB : 0));
429 int odhcpd_deregister(struct odhcpd_event *event)
431 event->uloop.cb = NULL;
432 return uloop_fd_delete(&event->uloop);
435 void odhcpd_process(struct odhcpd_event *event)
437 odhcpd_receive_packets(&event->uloop, 0);
440 int odhcpd_urandom(void *data, size_t len)
442 return read(urandom_fd, data, len);
446 time_t odhcpd_time(void)
449 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
454 static const char hexdigits[] = "0123456789abcdef";
455 static const int8_t hexvals[] = {
456 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
457 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
458 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
459 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
460 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
461 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
462 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
463 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
466 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src)
469 for (c = 0; c < len && src[0] && src[1]; ++c) {
470 int8_t x = (int8_t)*src++;
471 int8_t y = (int8_t)*src++;
472 if (x < 0 || (x = hexvals[x]) < 0
473 || y < 0 || (y = hexvals[y]) < 0)
476 while (((int8_t)*src) < 0 ||
477 (*src && hexvals[(uint8_t)*src] < 0))
485 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
487 for (size_t i = 0; i < len; ++i) {
488 *dst++ = hexdigits[src[i] >> 4];
489 *dst++ = hexdigits[src[i] & 0x0f];
494 const char *odhcpd_print_mac(const uint8_t *mac, const size_t len)
498 snprintf(buf, sizeof(buf), "%02x", mac[0]);
499 for (size_t i = 1, j = 2; i < len && j < sizeof(buf); i++, j += 3)
500 snprintf(buf + j, sizeof(buf) - j, ":%02x", mac[i]);
505 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits)
507 const uint8_t *a = av, *b = bv;
508 size_t bytes = bits / 8;
511 int res = memcmp(a, b, bytes);
512 if (res == 0 && bits > 0)
513 res = (a[bytes] >> (8 - bits)) - (b[bytes] >> (8 - bits));
519 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits)
522 const uint8_t *b = bv;
524 size_t bytes = bits / 8;
529 uint8_t mask = (1 << (8 - bits)) - 1;
530 a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]);
535 int odhcpd_netmask2bitlen(bool inet6, void *mask)
542 for (bits = 0, v6 = mask;
543 bits < 128 && (v6->s6_addr[bits / 8] << (bits % 8)) & 128;
546 for (bits = 0, v4 = mask;
547 bits < 32 && (ntohl(v4->s_addr) << bits) & 0x80000000;
553 bool odhcpd_bitlen2netmask(bool inet6, unsigned int bits, void *mask)
566 for (unsigned int i = 0; i < sizeof(v6->s6_addr); i++)
568 b = (bits > 8) ? 8 : bits;
569 v6->s6_addr[i] = (uint8_t)(0xFF << (8 - b));
579 v4->s_addr = bits ? htonl(~((1 << (32 - bits)) - 1)) : 0;
585 bool odhcpd_valid_hostname(const char *name)
588 const char *c, *label, *label_end;
591 for (c = name, label_sz = 0, label = name, label_end = name + strcspn(name, ".") - 1;
592 *c && label_sz <= MAX_LABEL; c++) {
593 if ((*c >= '0' && *c <= '9') ||
594 (*c >= 'A' && *c <= 'Z') ||
595 (*c >= 'a' && *c <= 'z')) {
600 if ((*c == '_' || *c == '-') && c != label && c != label_end) {
608 label_end = label + strcspn(label, ".") - 1;
617 return (label_sz && label_sz <= MAX_LABEL ? true : false);