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.
28 #include <arpa/inet.h>
30 #include <netinet/ip6.h>
31 #include <netpacket/packet.h>
32 #include <linux/rtnetlink.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <sys/epoll.h>
37 #include <sys/types.h>
39 #include <sys/syscall.h>
41 #include <libubox/uloop.h>
46 static int ioctl_sock;
47 static int rtnl_socket = -1;
48 static int rtnl_seq = 0;
49 static int urandom_fd = -1;
52 static void sighandler(_unused int signal)
60 openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
61 setlogmask(LOG_UPTO(LOG_WARNING));
65 syslog(LOG_ERR, "Must be run as root!");
69 ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
71 if ((rtnl_socket = odhcpd_open_rtnl()) < 0) {
72 syslog(LOG_ERR, "Unable to open socket: %s", strerror(errno));
76 if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
79 signal(SIGUSR1, SIG_IGN);
80 signal(SIGINT, sighandler);
81 signal(SIGTERM, sighandler);
99 int odhcpd_open_rtnl(void)
101 int sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
103 // Connect to the kernel netlink interface
104 struct sockaddr_nl nl = {.nl_family = AF_NETLINK};
105 if (connect(sock, (struct sockaddr*)&nl, sizeof(nl))) {
106 syslog(LOG_ERR, "Failed to connect to kernel rtnetlink: %s",
115 // Read IPv6 MTU for interface
116 int odhcpd_get_interface_config(const char *ifname, const char *what)
119 const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
120 snprintf(buf, sizeof(buf), sysctl_pattern, ifname, what);
122 int fd = open(buf, O_RDONLY);
123 ssize_t len = read(fd, buf, sizeof(buf) - 1);
134 // Read IPv6 MAC for interface
135 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
138 memset(&ifr, 0, sizeof(ifr));
139 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
140 if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
142 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
147 // Forwards a packet on a specific interface
148 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
149 struct iovec *iov, size_t iov_len,
150 const struct interface *iface)
153 uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
154 struct msghdr msg = {
155 .msg_name = (void *) dest,
156 .msg_namelen = sizeof(*dest),
158 .msg_iovlen = iov_len,
159 .msg_control = cmsg_buf,
160 .msg_controllen = sizeof(cmsg_buf),
164 // Set control data (define destination interface)
165 struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
166 chdr->cmsg_level = IPPROTO_IPV6;
167 chdr->cmsg_type = IPV6_PKTINFO;
168 chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
169 struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
170 pktinfo->ipi6_ifindex = iface->ifindex;
172 // Also set scope ID if link-local
173 if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
174 || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
175 dest->sin6_scope_id = iface->ifindex;
177 char ipbuf[INET6_ADDRSTRLEN];
178 inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
180 ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
182 syslog(LOG_NOTICE, "Failed to send to %s%%%s (%s)",
183 ipbuf, iface->ifname, strerror(errno));
185 syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
186 (long)sent, ipbuf, iface->ifname);
191 // Detect an IPV6-address currently assigned to the given interface
192 ssize_t odhcpd_get_interface_addresses(int ifindex,
193 struct odhcpd_ipaddr *addrs, size_t cnt)
197 struct ifaddrmsg ifa;
198 } req = {{sizeof(req), RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP,
199 ++rtnl_seq, 0}, {AF_INET6, 0, 0, 0, ifindex}};
200 if (send(rtnl_socket, &req, sizeof(req), 0) < (ssize_t)sizeof(req))
204 ssize_t len = 0, ret = 0;
206 for (struct nlmsghdr *nhm = NULL; ; nhm = NLMSG_NEXT(nhm, len)) {
207 while (len < 0 || !NLMSG_OK(nhm, (size_t)len)) {
208 len = recv(rtnl_socket, buf, sizeof(buf), 0);
209 nhm = (struct nlmsghdr*)buf;
210 if (len < 0 || !NLMSG_OK(nhm, (size_t)len)) {
218 if (nhm->nlmsg_type != RTM_NEWADDR)
221 // Skip address but keep clearing socket buffer
222 if (ret >= (ssize_t)cnt)
225 struct ifaddrmsg *ifa = NLMSG_DATA(nhm);
226 if (ifa->ifa_scope != RT_SCOPE_UNIVERSE ||
227 (ifindex && ifa->ifa_index != (unsigned)ifindex))
230 struct rtattr *rta = (struct rtattr*)&ifa[1];
231 size_t alen = NLMSG_PAYLOAD(nhm, sizeof(*ifa));
232 memset(&addrs[ret], 0, sizeof(addrs[ret]));
233 addrs[ret].prefix = ifa->ifa_prefixlen;
235 while (RTA_OK(rta, alen)) {
236 if (rta->rta_type == IFA_ADDRESS) {
237 memcpy(&addrs[ret].addr, RTA_DATA(rta),
238 sizeof(struct in6_addr));
239 } else if (rta->rta_type == IFA_CACHEINFO) {
240 struct ifa_cacheinfo *ifc = RTA_DATA(rta);
241 addrs[ret].preferred = ifc->ifa_prefered;
242 addrs[ret].valid = ifc->ifa_valid;
245 rta = RTA_NEXT(rta, alen);
248 if (ifa->ifa_flags & IFA_F_DEPRECATED)
249 addrs[ret].preferred = 0;
257 int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
260 struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex};
261 socklen_t alen = sizeof(addr);
262 int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
264 if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
265 !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
266 *lladdr = addr.sin6_addr;
274 void odhcpd_setup_route(const struct in6_addr *addr, int prefixlen,
275 const struct interface *iface, const struct in6_addr *gw,
276 int metric, bool add)
281 struct rtattr rta_dst;
282 struct in6_addr dst_addr;
283 struct rtattr rta_oif;
285 struct rtattr rta_table;
287 struct rtattr rta_prio;
289 struct rtattr rta_gw;
292 {sizeof(req), 0, NLM_F_REQUEST, ++rtnl_seq, 0},
293 {AF_INET6, prefixlen, 0, 0, 0, 0, 0, 0, 0},
294 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_DST},
296 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_OIF},
298 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_TABLE},
300 {sizeof(struct rtattr) + sizeof(uint32_t), RTA_PRIORITY},
302 {sizeof(struct rtattr) + sizeof(struct in6_addr), RTA_GATEWAY},
310 req.nh.nlmsg_type = RTM_NEWROUTE;
311 req.nh.nlmsg_flags |= (NLM_F_CREATE | NLM_F_REPLACE);
312 req.rtm.rtm_protocol = RTPROT_STATIC;
313 req.rtm.rtm_scope = (gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
314 req.rtm.rtm_type = RTN_UNICAST;
316 req.nh.nlmsg_type = RTM_DELROUTE;
317 req.rtm.rtm_scope = RT_SCOPE_NOWHERE;
320 req.nh.nlmsg_len = (gw) ? sizeof(req) : offsetof(struct req, rta_gw);
321 send(rtnl_socket, &req, req.nh.nlmsg_len, MSG_DONTWAIT);
324 struct interface* odhcpd_get_interface_by_index(int ifindex)
326 struct interface *iface;
327 list_for_each_entry(iface, &interfaces, head)
328 if (iface->ifindex == ifindex)
335 struct interface* odhcpd_get_interface_by_name(const char *name)
337 struct interface *iface;
338 list_for_each_entry(iface, &interfaces, head)
339 if (!strcmp(iface->ifname, name))
346 struct interface* odhcpd_get_master_interface(void)
348 struct interface *iface;
349 list_for_each_entry(iface, &interfaces, head)
357 // Convenience function to receive and do basic validation of packets
358 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
360 struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
362 uint8_t data_buf[RELAYD_BUFFER_SIZE], cmsg_buf[128];
364 struct sockaddr_in6 in6;
365 struct sockaddr_in in;
366 struct sockaddr_ll ll;
367 struct sockaddr_nl nl;
372 socklen_t ret_len = sizeof(ret);
373 getsockopt(u->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_len);
376 e->handle_error(ret);
380 struct iovec iov = {data_buf, sizeof(data_buf)};
381 struct msghdr msg = {
382 .msg_name = (void *) &addr,
383 .msg_namelen = sizeof(addr),
386 .msg_control = cmsg_buf,
387 .msg_controllen = sizeof(cmsg_buf),
391 ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
400 // Extract destination interface
404 struct in6_pktinfo *pktinfo;
405 struct in_pktinfo *pkt4info;
406 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
407 if (ch->cmsg_level == IPPROTO_IPV6 &&
408 ch->cmsg_type == IPV6_PKTINFO) {
409 pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
410 destiface = pktinfo->ipi6_ifindex;
411 dest = &pktinfo->ipi6_addr;
412 } else if (ch->cmsg_level == IPPROTO_IP &&
413 ch->cmsg_type == IP_PKTINFO) {
414 pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
415 destiface = pkt4info->ipi_ifindex;
416 dest = &pkt4info->ipi_addr;
417 } else if (ch->cmsg_level == IPPROTO_IPV6 &&
418 ch->cmsg_type == IPV6_HOPLIMIT) {
419 hlim = (int*)CMSG_DATA(ch);
423 // Check hoplimit if received
424 if (hlim && *hlim != 255)
427 // Detect interface for packet sockets
428 if (addr.ll.sll_family == AF_PACKET)
429 destiface = addr.ll.sll_ifindex;
431 struct interface *iface =
432 odhcpd_get_interface_by_index(destiface);
434 if (!iface && addr.nl.nl_family != AF_NETLINK)
437 char ipbuf[INET6_ADDRSTRLEN] = "kernel";
438 if (addr.ll.sll_family == AF_PACKET &&
439 len >= (ssize_t)sizeof(struct ip6_hdr))
440 inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
441 else if (addr.in6.sin6_family == AF_INET6)
442 inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
443 else if (addr.in.sin_family == AF_INET)
444 inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
446 syslog(LOG_DEBUG, "--");
447 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
448 ipbuf, (iface) ? iface->ifname : "netlink");
450 e->handle_dgram(&addr, data_buf, len, iface, dest);
454 // Register events for the multiplexer
455 int odhcpd_register(struct odhcpd_event *event)
457 event->uloop.cb = odhcpd_receive_packets;
458 return uloop_fd_add(&event->uloop, ULOOP_READ |
459 ((event->handle_error) ? ULOOP_ERROR_CB : 0));
462 void odhcpd_process(struct odhcpd_event *event)
464 odhcpd_receive_packets(&event->uloop, 0);
467 int odhcpd_urandom(void *data, size_t len)
469 return read(urandom_fd, data, len);
473 time_t odhcpd_time(void)
476 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
481 static const char hexdigits[] = "0123456789abcdef";
482 static const int8_t hexvals[] = {
483 -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
484 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
485 -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
486 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
487 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
488 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
489 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
490 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
493 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src)
496 for (c = 0; c < len && src[0] && src[1]; ++c) {
497 int8_t x = (int8_t)*src++;
498 int8_t y = (int8_t)*src++;
499 if (x < 0 || (x = hexvals[x]) < 0
500 || y < 0 || (y = hexvals[y]) < 0)
503 while (((int8_t)*src) < 0 ||
504 (*src && hexvals[(uint8_t)*src] < 0))
512 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
514 for (size_t i = 0; i < len; ++i) {
515 *dst++ = hexdigits[src[i] >> 4];
516 *dst++ = hexdigits[src[i] & 0x0f];
522 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits)
524 const uint8_t *a = av, *b = bv;
525 size_t bytes = bits / 8;
528 int res = memcmp(a, b, bytes);
529 if (res == 0 && bits > 0)
530 res = (a[bytes] >> (8 - bits)) - (b[bytes] >> (8 - bits));
536 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits)
539 const uint8_t *b = bv;
541 size_t bytes = bits / 8;
546 uint8_t mask = (1 << (8 - bits)) - 1;
547 a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]);