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