router: fix interface mtu read error
[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 #include <syslog.h>
28
29 #include <arpa/inet.h>
30 #include <net/if.h>
31 #include <netinet/ip6.h>
32 #include <netpacket/packet.h>
33 #include <linux/netlink.h>
34 #include <linux/if_addr.h>
35 #include <linux/neighbour.h>
36 #include <linux/rtnetlink.h>
37
38 #include <sys/socket.h>
39 #include <sys/ioctl.h>
40 #include <sys/epoll.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <sys/syscall.h>
44
45 #include <netlink/msg.h>
46 #include <netlink/socket.h>
47 #include <netlink/attr.h>
48 #include <libubox/uloop.h>
49 #include "odhcpd.h"
50
51
52
53 static int ioctl_sock;
54 static struct nl_sock *rtnl_socket = NULL;
55 static int urandom_fd = -1;
56
57 static void sighandler(_unused int signal)
58 {
59         uloop_end();
60 }
61
62 static void print_usage(const char *app)
63 {
64         printf(
65         "== %s Usage ==\n\n"
66         "  -h, --help   Print this help\n"
67         "  -l level     Specify log level 0..7 (default %d)\n",
68                 app, config.log_level
69         );
70 }
71
72 int main(int argc, char **argv)
73 {
74         openlog("odhcpd", LOG_PERROR | LOG_PID, LOG_DAEMON);
75         int opt;
76
77         while ((opt = getopt(argc, argv, "hl:")) != -1) {
78                 switch (opt) {
79                 case 'h':
80                         print_usage(argv[0]);
81                         return 0;
82                 case 'l':
83                         config.log_level = (atoi(optarg) & LOG_PRIMASK);
84                         fprintf(stderr, "Log level set to %d\n", config.log_level);
85                         break;
86                 }
87         }
88         setlogmask(LOG_UPTO(config.log_level));
89         uloop_init();
90
91         if (getuid() != 0) {
92                 syslog(LOG_ERR, "Must be run as root!");
93                 return 2;
94         }
95
96         ioctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
97
98         if (!(rtnl_socket = odhcpd_create_nl_socket(NETLINK_ROUTE))) {
99                 syslog(LOG_ERR, "Unable to open nl socket: %s", strerror(errno));
100                 return 2;
101         }
102
103         if ((urandom_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC)) < 0)
104                 return 4;
105
106         signal(SIGUSR1, SIG_IGN);
107         signal(SIGINT, sighandler);
108         signal(SIGTERM, sighandler);
109
110         if (init_router())
111                 return 4;
112
113         if (init_dhcpv6())
114                 return 4;
115
116         if (init_ndp())
117                 return 4;
118
119         if (init_dhcpv4())
120                 return 4;
121
122         odhcpd_run();
123         return 0;
124 }
125
126 struct nl_sock *odhcpd_create_nl_socket(int protocol)
127 {
128         struct nl_sock *nl_sock;
129
130         nl_sock = nl_socket_alloc();
131         if (!nl_sock)
132                 goto err;
133
134         if (nl_connect(nl_sock, protocol) < 0)
135                 goto err;
136
137         return nl_sock;
138
139 err:
140         if (nl_sock)
141                 nl_socket_free(nl_sock);
142
143         return NULL;
144 }
145
146
147 // Read IPv6 MTU for interface
148 int odhcpd_get_interface_config(const char *ifname, const char *what)
149 {
150         char buf[64];
151         const char *sysctl_pattern = "/proc/sys/net/ipv6/conf/%s/%s";
152         snprintf(buf, sizeof(buf), sysctl_pattern, ifname, what);
153
154         int fd = open(buf, O_RDONLY);
155         ssize_t len = read(fd, buf, sizeof(buf) - 1);
156         close(fd);
157
158         if (len < 0)
159                 return -1;
160
161         buf[len] = 0;
162         return atoi(buf);
163 }
164
165
166 // Read IPv6 MAC for interface
167 int odhcpd_get_mac(const struct interface *iface, uint8_t mac[6])
168 {
169         struct ifreq ifr;
170         memset(&ifr, 0, sizeof(ifr));
171         strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
172         if (ioctl(ioctl_sock, SIOCGIFHWADDR, &ifr) < 0)
173                 return -1;
174         memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
175         return 0;
176 }
177
178
179 // Forwards a packet on a specific interface
180 ssize_t odhcpd_send(int socket, struct sockaddr_in6 *dest,
181                 struct iovec *iov, size_t iov_len,
182                 const struct interface *iface)
183 {
184         // Construct headers
185         uint8_t cmsg_buf[CMSG_SPACE(sizeof(struct in6_pktinfo))] = {0};
186         struct msghdr msg = {
187                 .msg_name = (void *) dest,
188                 .msg_namelen = sizeof(*dest),
189                 .msg_iov = iov,
190                 .msg_iovlen = iov_len,
191                 .msg_control = cmsg_buf,
192                 .msg_controllen = sizeof(cmsg_buf),
193                 .msg_flags = 0
194         };
195
196         // Set control data (define destination interface)
197         struct cmsghdr *chdr = CMSG_FIRSTHDR(&msg);
198         chdr->cmsg_level = IPPROTO_IPV6;
199         chdr->cmsg_type = IPV6_PKTINFO;
200         chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
201         struct in6_pktinfo *pktinfo = (struct in6_pktinfo*)CMSG_DATA(chdr);
202         pktinfo->ipi6_ifindex = iface->ifindex;
203
204         // Also set scope ID if link-local
205         if (IN6_IS_ADDR_LINKLOCAL(&dest->sin6_addr)
206                         || IN6_IS_ADDR_MC_LINKLOCAL(&dest->sin6_addr))
207                 dest->sin6_scope_id = iface->ifindex;
208
209         char ipbuf[INET6_ADDRSTRLEN];
210         inet_ntop(AF_INET6, &dest->sin6_addr, ipbuf, sizeof(ipbuf));
211
212         ssize_t sent = sendmsg(socket, &msg, MSG_DONTWAIT);
213         if (sent < 0)
214                 syslog(LOG_NOTICE, "Failed to send to %s%%%s (%s)",
215                                 ipbuf, iface->ifname, strerror(errno));
216         else
217                 syslog(LOG_DEBUG, "Sent %li bytes to %s%%%s",
218                                 (long)sent, ipbuf, iface->ifname);
219         return sent;
220 }
221
222 struct addr_info {
223         int ifindex;
224         struct odhcpd_ipaddr *addrs;
225         size_t addrs_sz;
226         int pending;
227         ssize_t ret;
228 };
229
230 static int cb_valid_handler(struct nl_msg *msg, void *arg)
231 {
232         struct addr_info *ctxt = (struct addr_info *)arg;
233         struct nlmsghdr *hdr = nlmsg_hdr(msg);
234         struct ifaddrmsg *ifa;
235         struct nlattr *nla[__IFA_MAX];
236
237         if (hdr->nlmsg_type != RTM_NEWADDR || ctxt->ret >= (ssize_t)ctxt->addrs_sz)
238                 return NL_SKIP;
239
240         ifa = NLMSG_DATA(hdr);
241         if (ifa->ifa_scope != RT_SCOPE_UNIVERSE ||
242                         (ctxt->ifindex && ifa->ifa_index != (unsigned)ctxt->ifindex))
243                 return NL_SKIP;
244
245         nlmsg_parse(hdr, sizeof(*ifa), nla, __IFA_MAX - 1, NULL);
246         if (!nla[IFA_ADDRESS])
247                 return NL_SKIP;
248
249         memset(&ctxt->addrs[ctxt->ret], 0, sizeof(ctxt->addrs[ctxt->ret]));
250         ctxt->addrs[ctxt->ret].prefix = ifa->ifa_prefixlen;
251
252         nla_memcpy(&ctxt->addrs[ctxt->ret].addr, nla[IFA_ADDRESS],
253                         sizeof(ctxt->addrs[ctxt->ret].addr));
254
255         if (nla[IFA_CACHEINFO]) {
256                 struct ifa_cacheinfo *ifc = nla_data(nla[IFA_CACHEINFO]);
257
258                 ctxt->addrs[ctxt->ret].preferred = ifc->ifa_prefered;
259                 ctxt->addrs[ctxt->ret].valid = ifc->ifa_valid;
260         }
261
262         if (ifa->ifa_flags & IFA_F_DEPRECATED)
263                 ctxt->addrs[ctxt->ret].preferred = 0;
264
265         ctxt->ret++;
266
267         return NL_OK;
268 }
269
270 static int cb_finish_handler(_unused struct nl_msg *msg, void *arg)
271 {
272         struct addr_info *ctxt = (struct addr_info *)arg;
273
274         ctxt->pending = 0;
275
276         return NL_STOP;
277 }
278
279 static int cb_error_handler(_unused struct sockaddr_nl *nla, struct nlmsgerr *err,
280                 void *arg)
281 {
282         struct addr_info *ctxt = (struct addr_info *)arg;
283
284         ctxt->pending = 0;
285         ctxt->ret = err->error;
286
287         return NL_STOP;
288 }
289
290 // Detect an IPV6-address currently assigned to the given interface
291 ssize_t odhcpd_get_interface_addresses(int ifindex,
292                 struct odhcpd_ipaddr *addrs, size_t cnt)
293 {
294         struct nl_msg *msg;
295         struct ifaddrmsg ifa = {
296                 .ifa_family = AF_INET6,
297                 .ifa_prefixlen = 0,
298                 .ifa_flags = 0,
299                 .ifa_scope = 0,
300                 .ifa_index = ifindex, };
301         struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
302         struct addr_info ctxt = {
303                 .ifindex = ifindex,
304                 .addrs = addrs,
305                 .addrs_sz = cnt,
306                 .ret = 0,
307                 .pending = 1,
308         };
309
310         if (!cb) {
311                 ctxt.ret = -1;
312                 goto out;
313         }
314
315         msg = nlmsg_alloc_simple(RTM_GETADDR, NLM_F_REQUEST | NLM_F_DUMP);
316
317         if (!msg) {
318                 ctxt.ret = - 1;
319                 goto out;
320         }
321
322         nlmsg_append(msg, &ifa, sizeof(ifa), 0);
323
324         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_valid_handler, &ctxt);
325         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_handler, &ctxt);
326         nl_cb_err(cb, NL_CB_CUSTOM, cb_error_handler, &ctxt);
327
328         nl_send_auto_complete(rtnl_socket, msg);
329         while (ctxt.pending > 0)
330                 nl_recvmsgs(rtnl_socket, cb);
331
332         nlmsg_free(msg);
333 out:
334         nl_cb_put(cb);
335
336         return ctxt.ret;
337 }
338
339 static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr)
340 {
341         int status = -1;
342         struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex};
343         socklen_t alen = sizeof(addr);
344         int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
345
346         if (!connect(sock, (struct sockaddr*)&addr, sizeof(addr)) &&
347                         !getsockname(sock, (struct sockaddr*)&addr, &alen)) {
348                 *lladdr = addr.sin6_addr;
349                 status = 0;
350         }
351
352         close(sock);
353
354         return status;
355 }
356
357 /*
358  * DNS address selection criteria order :
359  * - use IPv6 address with valid lifetime if none is yet selected
360  * - use IPv6 address with a preferred lifetime if the already selected IPv6 address is deprecated
361  * - use an IPv6 ULA address if the already selected IPv6 address is not an ULA address
362  * - use the IPv6 address with the longest preferred lifetime
363  */
364 int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr)
365 {
366         time_t now = odhcpd_time();
367         ssize_t m = -1;
368
369         for (size_t i = 0; i < iface->ia_addr_len; ++i) {
370                 if (iface->ia_addr[i].valid <= (uint32_t)now)
371                         continue;
372
373                 if (m < 0) {
374                         m = i;
375                         continue;
376                 }
377
378                 if (iface->ia_addr[m].preferred >= (uint32_t)now &&
379                                 iface->ia_addr[i].preferred < (uint32_t)now)
380                         continue;
381
382                 if (IN6_IS_ADDR_ULA(&iface->ia_addr[i].addr)) {
383                         if (!IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr)) {
384                                 m = i;
385                                 continue;
386                         }
387                 } else if (IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr))
388                         continue;
389
390                 if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred)
391                         m = i;
392         }
393
394         if (m >= 0) {
395                 *addr = iface->ia_addr[m].addr;
396                 return 0;
397         }
398
399         return odhcpd_get_linklocal_interface_address(iface->ifindex, addr);
400 }
401
402 int odhcpd_setup_route(const struct in6_addr *addr, const int prefixlen,
403                 const struct interface *iface, const struct in6_addr *gw,
404                 const uint32_t metric, const bool add)
405 {
406         struct nl_msg *msg;
407         struct rtmsg rtm = {
408                 .rtm_family = AF_INET6,
409                 .rtm_dst_len = prefixlen,
410                 .rtm_src_len = 0,
411                 .rtm_table = RT_TABLE_MAIN,
412                 .rtm_protocol = (add ? RTPROT_STATIC : RTPROT_UNSPEC),
413                 .rtm_scope = (add ? (gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK) : RT_SCOPE_NOWHERE),
414                 .rtm_type = (add ? RTN_UNICAST : RTN_UNSPEC),
415         };
416         int ret = 0;
417
418         msg = nlmsg_alloc_simple(add ? RTM_NEWROUTE : RTM_DELROUTE,
419                                         add ? NLM_F_CREATE | NLM_F_REPLACE : 0);
420         if (!msg)
421                 return -1;
422
423         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
424
425         nla_put(msg, RTA_DST, sizeof(*addr), addr);
426         nla_put_u32(msg, RTA_OIF, iface->ifindex);
427         nla_put_u32(msg, RTA_PRIORITY, metric);
428
429         if (gw)
430                 nla_put(msg, RTA_GATEWAY, sizeof(*gw), gw);
431
432         ret = nl_send_auto_complete(rtnl_socket, msg);
433         nlmsg_free(msg);
434
435         if (ret < 0)
436                 return ret;
437
438         return nl_wait_for_ack(rtnl_socket);
439 }
440
441 int odhcpd_setup_proxy_neigh(const struct in6_addr *addr,
442                 const struct interface *iface, const bool add)
443 {
444         struct nl_msg *msg;
445         struct ndmsg ndm = {
446                 .ndm_family = AF_INET6,
447                 .ndm_flags = NTF_PROXY,
448                 .ndm_ifindex = iface->ifindex,
449         };
450         int ret = 0, flags = NLM_F_REQUEST;
451
452         if (add)
453                 flags |= NLM_F_REPLACE | NLM_F_CREATE;
454
455         msg = nlmsg_alloc_simple(add ? RTM_NEWNEIGH : RTM_DELNEIGH, flags);
456         if (!msg)
457                 return -1;
458
459         nlmsg_append(msg, &ndm, sizeof(ndm), 0);
460
461         nla_put(msg, NDA_DST, sizeof(*addr), addr);
462
463         ret = nl_send_auto_complete(rtnl_socket, msg);
464         nlmsg_free(msg);
465
466         if (ret < 0)
467                 return ret;
468
469         return nl_wait_for_ack(rtnl_socket);
470 }
471
472 struct interface* odhcpd_get_interface_by_index(int ifindex)
473 {
474         struct interface *iface;
475         list_for_each_entry(iface, &interfaces, head)
476                 if (iface->ifindex == ifindex)
477                         return iface;
478
479         return NULL;
480 }
481
482
483 struct interface* odhcpd_get_interface_by_name(const char *name)
484 {
485         struct interface *iface;
486         list_for_each_entry(iface, &interfaces, head)
487                 if (!strcmp(iface->ifname, name))
488                         return iface;
489
490         return NULL;
491 }
492
493
494 struct interface* odhcpd_get_master_interface(void)
495 {
496         struct interface *iface;
497         list_for_each_entry(iface, &interfaces, head)
498                 if (iface->master)
499                         return iface;
500
501         return NULL;
502 }
503
504
505 // Convenience function to receive and do basic validation of packets
506 static void odhcpd_receive_packets(struct uloop_fd *u, _unused unsigned int events)
507 {
508         struct odhcpd_event *e = container_of(u, struct odhcpd_event, uloop);
509
510         uint8_t data_buf[RELAYD_BUFFER_SIZE], cmsg_buf[128];
511         union {
512                 struct sockaddr_in6 in6;
513                 struct sockaddr_in in;
514                 struct sockaddr_ll ll;
515                 struct sockaddr_nl nl;
516         } addr;
517
518         if (u->error) {
519                 int ret = -1;
520                 socklen_t ret_len = sizeof(ret);
521                 getsockopt(u->fd, SOL_SOCKET, SO_ERROR, &ret, &ret_len);
522                 u->error = false;
523                 if (e->handle_error)
524                         e->handle_error(e, ret);
525         }
526
527         if (e->recv_msgs) {
528                 e->recv_msgs(e);
529                 return;
530         }
531
532         while (true) {
533                 struct iovec iov = {data_buf, sizeof(data_buf)};
534                 struct msghdr msg = {
535                         .msg_name = (void *) &addr,
536                         .msg_namelen = sizeof(addr),
537                         .msg_iov = &iov,
538                         .msg_iovlen = 1,
539                         .msg_control = cmsg_buf,
540                         .msg_controllen = sizeof(cmsg_buf),
541                         .msg_flags = 0
542                 };
543
544                 ssize_t len = recvmsg(u->fd, &msg, MSG_DONTWAIT);
545                 if (len < 0) {
546                         if (errno == EAGAIN)
547                                 break;
548                         else
549                                 continue;
550                 }
551
552
553                 // Extract destination interface
554                 int destiface = 0;
555                 int *hlim = NULL;
556                 void *dest = NULL;
557                 struct in6_pktinfo *pktinfo;
558                 struct in_pktinfo *pkt4info;
559                 for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) {
560                         if (ch->cmsg_level == IPPROTO_IPV6 &&
561                                         ch->cmsg_type == IPV6_PKTINFO) {
562                                 pktinfo = (struct in6_pktinfo*)CMSG_DATA(ch);
563                                 destiface = pktinfo->ipi6_ifindex;
564                                 dest = &pktinfo->ipi6_addr;
565                         } else if (ch->cmsg_level == IPPROTO_IP &&
566                                         ch->cmsg_type == IP_PKTINFO) {
567                                 pkt4info = (struct in_pktinfo*)CMSG_DATA(ch);
568                                 destiface = pkt4info->ipi_ifindex;
569                                 dest = &pkt4info->ipi_addr;
570                         } else if (ch->cmsg_level == IPPROTO_IPV6 &&
571                                         ch->cmsg_type == IPV6_HOPLIMIT) {
572                                 hlim = (int*)CMSG_DATA(ch);
573                         }
574                 }
575
576                 // Check hoplimit if received
577                 if (hlim && *hlim != 255)
578                         continue;
579
580                 // Detect interface for packet sockets
581                 if (addr.ll.sll_family == AF_PACKET)
582                         destiface = addr.ll.sll_ifindex;
583
584                 struct interface *iface =
585                                 odhcpd_get_interface_by_index(destiface);
586
587                 if (!iface && addr.nl.nl_family != AF_NETLINK)
588                         continue;
589
590                 char ipbuf[INET6_ADDRSTRLEN] = "kernel";
591                 if (addr.ll.sll_family == AF_PACKET &&
592                                 len >= (ssize_t)sizeof(struct ip6_hdr))
593                         inet_ntop(AF_INET6, &data_buf[8], ipbuf, sizeof(ipbuf));
594                 else if (addr.in6.sin6_family == AF_INET6)
595                         inet_ntop(AF_INET6, &addr.in6.sin6_addr, ipbuf, sizeof(ipbuf));
596                 else if (addr.in.sin_family == AF_INET)
597                         inet_ntop(AF_INET, &addr.in.sin_addr, ipbuf, sizeof(ipbuf));
598
599                 syslog(LOG_DEBUG, "Received %li Bytes from %s%%%s", (long)len,
600                                 ipbuf, (iface) ? iface->ifname : "netlink");
601
602                 e->handle_dgram(&addr, data_buf, len, iface, dest);
603         }
604 }
605
606 // Register events for the multiplexer
607 int odhcpd_register(struct odhcpd_event *event)
608 {
609         event->uloop.cb = odhcpd_receive_packets;
610         return uloop_fd_add(&event->uloop, ULOOP_READ |
611                         ((event->handle_error) ? ULOOP_ERROR_CB : 0));
612 }
613
614 int odhcpd_deregister(struct odhcpd_event *event)
615 {
616         event->uloop.cb = NULL;
617         return uloop_fd_delete(&event->uloop);
618 }
619
620 void odhcpd_process(struct odhcpd_event *event)
621 {
622         odhcpd_receive_packets(&event->uloop, 0);
623 }
624
625 int odhcpd_urandom(void *data, size_t len)
626 {
627         return read(urandom_fd, data, len);
628 }
629
630
631 time_t odhcpd_time(void)
632 {
633         struct timespec ts;
634         syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
635         return ts.tv_sec;
636 }
637
638
639 static const char hexdigits[] = "0123456789abcdef";
640 static const int8_t hexvals[] = {
641     -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
642     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
643     -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
644      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
645     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
646     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
647     -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
648     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
649 };
650
651 ssize_t odhcpd_unhexlify(uint8_t *dst, size_t len, const char *src)
652 {
653         size_t c;
654         for (c = 0; c < len && src[0] && src[1]; ++c) {
655                 int8_t x = (int8_t)*src++;
656                 int8_t y = (int8_t)*src++;
657                 if (x < 0 || (x = hexvals[x]) < 0
658                                 || y < 0 || (y = hexvals[y]) < 0)
659                         return -1;
660                 dst[c] = x << 4 | y;
661                 while (((int8_t)*src) < 0 ||
662                                 (*src && hexvals[(uint8_t)*src] < 0))
663                         src++;
664         }
665
666         return c;
667 }
668
669
670 void odhcpd_hexlify(char *dst, const uint8_t *src, size_t len)
671 {
672         for (size_t i = 0; i < len; ++i) {
673                 *dst++ = hexdigits[src[i] >> 4];
674                 *dst++ = hexdigits[src[i] & 0x0f];
675         }
676         *dst = 0;
677 }
678
679
680 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits)
681 {
682         const uint8_t *a = av, *b = bv;
683         size_t bytes = bits / 8;
684         bits %= 8;
685
686         int res = memcmp(a, b, bytes);
687         if (res == 0 && bits > 0)
688                 res = (a[bytes] >> (8 - bits)) - (b[bytes] >> (8 - bits));
689
690         return res;
691 }
692
693
694 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits)
695 {
696         uint8_t *a = av;
697         const uint8_t *b = bv;
698
699         size_t bytes = bits / 8;
700         bits %= 8;
701         memcpy(a, b, bytes);
702
703         if (bits > 0) {
704                 uint8_t mask = (1 << (8 - bits)) - 1;
705                 a[bytes] = (a[bytes] & mask) | ((~mask) & b[bytes]);
706         }
707 }