config: Prioritize ifname resolving via ubus over ifname/networkid attributes
[project/odhcpd.git] / src / dhcpv4.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
16 #include <time.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <resolv.h>
23 #include <limits.h>
24 #include <net/if.h>
25 #include <net/if_arp.h>
26 #include <netinet/ip.h>
27 #include <sys/ioctl.h>
28 #include <sys/timerfd.h>
29 #include <arpa/inet.h>
30
31 #include "odhcpd.h"
32 #include "dhcpv4.h"
33 #include "dhcpv6.h"
34
35
36 static void handle_dhcpv4(void *addr, void *data, size_t len,
37                 struct interface *iface, void *dest_addr);
38 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
39                 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
40                 const char *hostname);
41
42 // Create socket and register events
43 int init_dhcpv4(void)
44 {
45         return 0;
46 }
47
48 char *dhcpv4_msg_to_string(uint8_t reqmsg)
49 {
50         switch (reqmsg) {
51         case (DHCPV4_MSG_DISCOVER):
52                 return "DHCPV4_MSG_DISCOVER";
53         case (DHCPV4_MSG_OFFER):
54                 return "DHCPV4_MSG_OFFER";
55         case (DHCPV4_MSG_REQUEST):
56                 return "DHCPV4_MSG_REQUEST";
57         case (DHCPV4_MSG_DECLINE):
58                 return "DHCPV4_MSG_DECLINE";
59         case (DHCPV4_MSG_ACK):
60                 return "DHCPV4_MSG_ACK";
61         case (DHCPV4_MSG_NAK):
62                 return "DHCPV4_MSG_NAK";
63         case (DHCPV4_MSG_RELEASE):
64                 return "DHCPV4_MSG_RELEASE";
65         case (DHCPV4_MSG_INFORM):
66                 return "DHCPV4_MSG_INFORM";
67         default:
68                 return "UNKNOWN";
69         }
70 }
71
72 int setup_dhcpv4_interface(struct interface *iface, bool enable)
73 {
74         if (iface->dhcpv4_event.uloop.fd > 0) {
75                 uloop_fd_delete(&iface->dhcpv4_event.uloop);
76                 close(iface->dhcpv4_event.uloop.fd);
77                 iface->dhcpv4_event.uloop.fd = -1;
78         }
79
80         if (iface->dhcpv4 && enable) {
81                 if (!iface->dhcpv4_assignments.next)
82                         INIT_LIST_HEAD(&iface->dhcpv4_assignments);
83
84                 int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
85                 if (sock < 0) {
86                         syslog(LOG_ERR, "Failed to create DHCPv4 server socket: %s",
87                                         strerror(errno));
88                         return -1;
89                 }
90
91                 // Basic IPv6 configuration
92                 int val = 1;
93                 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
94                 setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
95                 setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val));
96
97                 val = IPTOS_PREC_INTERNETCONTROL;
98                 setsockopt(sock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
99
100                 val = IP_PMTUDISC_DONT;
101                 setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
102
103                 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
104                                 iface->ifname, strlen(iface->ifname));
105
106                 struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
107                                         {INADDR_ANY}, {0}};
108
109                 if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
110                         syslog(LOG_ERR, "Failed to open DHCPv4 server socket: %s",
111                                         strerror(errno));
112                         return -1;
113                 }
114
115
116                 if (ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
117                         syslog(LOG_ERR, "Invalid DHCP range");
118                         return -1;
119                 }
120
121                 // Create a range if not specified
122                 struct ifreq ifreq;
123                 strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
124
125                 struct sockaddr_in *saddr = (struct sockaddr_in*)&ifreq.ifr_addr;
126                 struct sockaddr_in *smask = (struct sockaddr_in*)&ifreq.ifr_netmask;
127                 if (!(iface->dhcpv4_start.s_addr & htonl(0xffff0000)) &&
128                                 !(iface->dhcpv4_end.s_addr & htonl(0xffff0000)) &&
129                                 !ioctl(sock, SIOCGIFADDR, &ifreq)) {
130                         struct in_addr addr = saddr->sin_addr;
131
132                         ioctl(sock, SIOCGIFNETMASK, &ifreq);
133                         struct in_addr mask = smask->sin_addr;
134
135                         uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
136                         uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
137
138                         if (start && end && start < end &&
139                                         start > ntohl(addr.s_addr & ~mask.s_addr) &&
140                                         (start & ntohl(~mask.s_addr)) == start &&
141                                         (end & ntohl(~mask.s_addr)) == end) {
142                                 iface->dhcpv4_start.s_addr = htonl(start) |
143                                                 (addr.s_addr & mask.s_addr);
144                                 iface->dhcpv4_end.s_addr = htonl(end) |
145                                                 (addr.s_addr & mask.s_addr);
146                         } else if (ntohl(mask.s_addr) <= 0xfffffff0) {
147                                 start = addr.s_addr & mask.s_addr;
148                                 end = addr.s_addr & mask.s_addr;
149
150                                 if (ntohl(mask.s_addr) <= 0xffffff00) {
151                                         iface->dhcpv4_start.s_addr = start | htonl(100);
152                                         iface->dhcpv4_end.s_addr = end | htonl(250);
153                                 } else if (ntohl(mask.s_addr) <= 0xffffffc0) {
154                                         iface->dhcpv4_start.s_addr = start | htonl(10);
155                                         iface->dhcpv4_end.s_addr = end | htonl(60);
156                                 } else if (ntohl(mask.s_addr) <= 0xffffffe0) {
157                                         iface->dhcpv4_start.s_addr = start | htonl(10);
158                                         iface->dhcpv4_end.s_addr = end | htonl(30);
159                                 } else {
160                                         iface->dhcpv4_start.s_addr = start | htonl(3);
161                                         iface->dhcpv4_end.s_addr = end | htonl(12);
162                                 }
163                         }
164
165
166                 }
167
168                 // Parse static entries
169                 struct lease *lease;
170                 list_for_each_entry(lease, &leases, head) {
171                         // Construct entry
172                         size_t hostlen = strlen(lease->hostname) + 1;
173                         struct dhcpv4_assignment *a = calloc(1, sizeof(*a) + hostlen);
174                         if (!a) {
175                                 syslog(LOG_ERR, "Calloc failed for static lease on interface %s",
176                                         iface->ifname);
177                                 return -1;
178                         }
179                         a->addr = ntohl(lease->ipaddr.s_addr);
180                         memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
181                         memcpy(a->hostname, lease->hostname, hostlen);
182                         a->valid_until = LONG_MAX;
183
184                         // Assign to all interfaces
185                         struct dhcpv4_assignment *c;
186                         list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
187                                 if (c->addr > a->addr) {
188                                         list_add_tail(&a->head, &c->head);
189                                         break;
190                                 } else if (c->addr == a->addr) {
191                                         // Already an assignment with that number
192                                         break;
193                                 }
194                         }
195                         if (&c->head == &iface->dhcpv4_assignments) {
196                                 list_add(&a->head, &iface->dhcpv4_assignments);
197                         }
198
199                         if (!a->head.next)
200                                 free(a);
201                 }
202
203                 // Clean invalid assignments
204                 struct dhcpv4_assignment *a, *n;
205                 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
206                         if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
207                                         (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) {
208                                 list_del(&a->head);
209                                 free(a);
210                         }
211                 }
212
213
214                 if (iface->dhcpv4_leasetime < 60)
215                         iface->dhcpv4_leasetime = 43200;
216
217                 iface->dhcpv4_event.uloop.fd = sock;
218                 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
219                 odhcpd_register(&iface->dhcpv4_event);
220         } else if (iface->dhcpv4_assignments.next) {
221                 while (!list_empty(&iface->dhcpv4_assignments)) {
222                         struct dhcpv4_assignment *a = list_first_entry(&iface->dhcpv4_assignments,
223                                         struct dhcpv4_assignment, head);
224                         list_del(&a->head);
225                         free(a);
226                 }
227
228         }
229         return 0;
230 }
231
232
233 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
234                 uint8_t type, uint8_t len, const void *data)
235 {
236         uint8_t *c = *cookie;
237         if (*cookie + 2 + len > (uint8_t*)&msg[1])
238                 return;
239
240         *c++ = type;
241         *c++ = len;
242         memcpy(c, data, len);
243
244         *cookie = c + len;
245 }
246
247
248 // Simple DHCPv6-server for information requests
249 static void handle_dhcpv4(void *addr, void *data, size_t len,
250                 struct interface *iface, _unused void *dest_addr)
251 {
252         if (!iface->dhcpv4)
253                 return;
254
255         struct dhcpv4_message *req = data;
256         if (len < offsetof(struct dhcpv4_message, options) + 4 ||
257                         req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
258                 return;
259
260         int sock = iface->dhcpv4_event.uloop.fd;
261         struct sockaddr_in ifaddr;
262         struct sockaddr_in ifnetmask;
263
264         syslog(LOG_NOTICE, "Got DHCPv4 request");
265
266         struct ifreq ifreq;
267         memcpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
268         if (ioctl(sock, SIOCGIFADDR, &ifreq)) {
269                 syslog(LOG_WARNING, "DHCPv4 failed to detect address: %s", strerror(errno));
270                 return;
271         }
272
273         memcpy(&ifaddr, &ifreq.ifr_addr, sizeof(ifaddr));
274         if (ioctl(sock, SIOCGIFNETMASK, &ifreq))
275                 return;
276
277         memcpy(&ifnetmask, &ifreq.ifr_netmask, sizeof(ifnetmask));
278         uint32_t network = ifaddr.sin_addr.s_addr & ifnetmask.sin_addr.s_addr;
279
280         if ((iface->dhcpv4_start.s_addr & ifnetmask.sin_addr.s_addr) != network ||
281                         (iface->dhcpv4_end.s_addr & ifnetmask.sin_addr.s_addr) != network) {
282                 syslog(LOG_WARNING, "DHCPv4 range out of assigned network");
283                 return;
284         }
285
286         struct ifreq ifr = {.ifr_name = ""};
287         strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
288
289         struct dhcpv4_message reply = {
290                 .op = DHCPV4_BOOTREPLY,
291                 .htype = 1,
292                 .hlen = 6,
293                 .hops = 0,
294                 .xid = req->xid,
295                 .secs = 0,
296                 .flags = req->flags,
297                 .ciaddr = {INADDR_ANY},
298                 .giaddr = req->giaddr,
299                 .siaddr = ifaddr.sin_addr,
300         };
301         memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
302
303         reply.options[0] = 0x63;
304         reply.options[1] = 0x82;
305         reply.options[2] = 0x53;
306         reply.options[3] = 0x63;
307
308         uint8_t *cookie = &reply.options[4];
309         uint8_t reqmsg = DHCPV4_MSG_REQUEST;
310         uint8_t msg = DHCPV4_MSG_ACK;
311
312         struct in_addr reqaddr = {INADDR_ANY};
313         char hostname[256];
314         hostname[0] = 0;
315
316         uint8_t *start = &req->options[4];
317         uint8_t *end = ((uint8_t*)data) + len;
318         struct dhcpv4_option *opt;
319         dhcpv4_for_each_option(start, end, opt) {
320                 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1) {
321                         reqmsg = opt->data[0];
322                 } else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
323                         memcpy(hostname, opt->data, opt->len);
324                         hostname[opt->len] = 0;
325                 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) {
326                         memcpy(&reqaddr, opt->data, 4);
327                 } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
328                         if (memcmp(opt->data, &ifaddr.sin_addr, 4))
329                                 return;
330                 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
331                         uint8_t *c = opt->data, *cend = &opt->data[opt->len];
332                         for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
333                                 size_t elen = strlen(iface->filter_class);
334                                 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
335                                         return; // Ignore from homenet
336                         }
337                 }
338         }
339
340         if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
341                         reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
342                         reqmsg != DHCPV4_MSG_RELEASE)
343                 return;
344
345         struct dhcpv4_assignment *lease = NULL;
346         if (reqmsg != DHCPV4_MSG_INFORM)
347                 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr, hostname);
348
349         if (!lease) {
350                 if (reqmsg == DHCPV4_MSG_REQUEST)
351                         msg = DHCPV4_MSG_NAK;
352                 else if (reqmsg == DHCPV4_MSG_DISCOVER)
353                         return;
354         } else if (reqmsg == DHCPV4_MSG_DISCOVER) {
355                 msg = DHCPV4_MSG_OFFER;
356         } else if (reqmsg == DHCPV4_MSG_REQUEST && reqaddr.s_addr &&
357                         reqaddr.s_addr != htonl(lease->addr)) {
358                 msg = DHCPV4_MSG_NAK;
359                 /*
360                  * DHCP client requested an IP which we can't offer to him. Probably the
361                  * client changed the network. The reply type is set to DHCPV4_MSG_NAK,
362                  * because the client should not use that IP.
363                  *
364                  * For modern devices we build an answer that includes a valid IP, like
365                  * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
366                  * perform additional DHCP round trips.
367                  *
368                  */
369         }
370
371         syslog(LOG_WARNING, "received %s from %x:%x:%x:%x:%x:%x",
372                         dhcpv4_msg_to_string(reqmsg),
373                         req->chaddr[0],req->chaddr[1],req->chaddr[2],
374                         req->chaddr[3],req->chaddr[4],req->chaddr[5]);
375
376         if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
377                 return;
378
379         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
380         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &ifaddr.sin_addr);
381
382         if (lease) {
383                 reply.yiaddr.s_addr = htonl(lease->addr);
384
385                 uint32_t val = htonl(iface->dhcpv4_leasetime);
386                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
387
388                 val = htonl(500 * iface->dhcpv4_leasetime / 1000);
389                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
390
391                 val = htonl(875 * iface->dhcpv4_leasetime / 1000);
392                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
393
394                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr);
395
396                 if (lease->hostname[0])
397                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
398                                         strlen(lease->hostname), lease->hostname);
399
400                 if (!ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
401                         struct sockaddr_in *ina = (struct sockaddr_in*)&ifr.ifr_broadaddr;
402                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &ina->sin_addr);
403                 }
404         }
405
406         if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
407                 uint16_t mtu = htons(ifr.ifr_mtu);
408                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
409         }
410
411         if (iface->search && iface->search_len <= 255) {
412                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
413                                 iface->search_len, iface->search);
414         } else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
415                 uint8_t search_buf[256];
416                 int len = dn_comp(_res.dnsrch[0], search_buf,
417                                                 sizeof(search_buf), NULL, NULL);
418                 if (len > 0)
419                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
420                                         len, search_buf);
421         }
422
423         if (iface->dhcpv4_router_cnt == 0)
424                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &ifaddr.sin_addr);
425         else
426                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
427                                 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
428
429
430         if (iface->dhcpv4_dns_cnt == 0)
431                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &ifaddr.sin_addr);
432         else
433                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
434                                 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
435
436
437         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
438
439         struct sockaddr_in dest = *((struct sockaddr_in*)addr);
440         if (req->giaddr.s_addr) {
441                 /*
442                  * relay agent is configured, send reply to the agent
443                  */
444                 dest.sin_addr = req->giaddr;
445                 dest.sin_port = htons(DHCPV4_SERVER_PORT);
446         } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
447                 /*
448                  * client has existing configuration (ciaddr is set) AND this address is
449                  * not the address it used for the dhcp message
450                  */
451                 dest.sin_addr = req->ciaddr;
452                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
453         } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
454                         req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
455                 /*
456                  * client requests a broadcast reply OR we can't offer an IP
457                  */
458                 dest.sin_addr.s_addr = INADDR_BROADCAST;
459                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
460         } else if (!req->ciaddr.s_addr && msg == DHCPV4_MSG_NAK) {
461                 /*
462                  * client has no previous configuration -> no IP, so we need to reply
463                  * with a broadcast packet
464                  */
465                 dest.sin_addr.s_addr = INADDR_BROADCAST;
466                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
467         } else {
468                 /*
469                  * send reply to the newly (in this proccess) allocated IP
470                  */
471                 dest.sin_addr = reply.yiaddr;
472                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
473
474                 struct arpreq arp = {.arp_flags = ATF_COM};
475                 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
476                 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
477                 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
478                 ioctl(sock, SIOCSARP, &arp);
479         }
480
481         if (dest.sin_addr.s_addr == INADDR_BROADCAST) {
482                 /*
483                  * reply goes to IP broadcast -> MAC broadcast
484                  */
485                 syslog(LOG_WARNING, "sending %s to ff:ff:ff:ff:ff:ff - %s",
486                                 dhcpv4_msg_to_string(msg),
487                                 inet_ntoa(dest.sin_addr));
488         } else {
489                 /*
490                  * reply is send directly to IP,
491                  * MAC is assumed to be the same as the request
492                  */
493                 syslog(LOG_WARNING, "sending %s to %x:%x:%x:%x:%x:%x - %s",
494                                 dhcpv4_msg_to_string(msg),
495                                 req->chaddr[0],req->chaddr[1],req->chaddr[2],
496                                 req->chaddr[3],req->chaddr[4],req->chaddr[5],
497                                 inet_ntoa(dest.sin_addr));
498         }
499
500         sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
501                         (struct sockaddr*)&dest, sizeof(dest));
502 }
503
504 static bool dhcpv4_test(struct interface *iface, uint32_t try)
505 {
506         struct dhcpv4_assignment *c;
507         list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
508                 if (c->addr == try) {
509                         return false;
510                 }
511         }
512         return true;
513 }
514
515 static bool dhcpv4_assign(struct interface *iface,
516                 struct dhcpv4_assignment *assign, uint32_t raddr)
517 {
518         uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
519         uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
520         uint32_t count = end - start + 1;
521
522         // try to assign the IP the client asked for
523         if (start <= raddr && raddr <= end && dhcpv4_test(iface, raddr)) {
524                 assign->addr = raddr;
525                 list_add(&assign->head, &iface->dhcpv4_assignments);
526                 syslog(LOG_DEBUG, "assigning the IP the client asked for: %u.%u.%u.%u",
527                                 (assign->addr & 0xff000000) >> 24,
528                                 (assign->addr & 0x00ff0000) >> 16,
529                                 (assign->addr & 0x0000ff00) >> 8,
530                                 (assign->addr & 0x000000ff));
531                 return true;
532         }
533
534         // Seed RNG with checksum of hwaddress
535         uint32_t seed = 0;
536         for (size_t i = 0; i < sizeof(assign->hwaddr); ++i) {
537                 // Knuth's multiplicative method
538                 uint8_t o = assign->hwaddr[i];
539                 seed += (o*2654435761) % UINT32_MAX;
540         }
541         srand(seed);
542
543         uint32_t try = (((uint32_t)rand()) % count) + start;
544
545         if (list_empty(&iface->dhcpv4_assignments)) {
546                 assign->addr = try;
547                 list_add(&assign->head, &iface->dhcpv4_assignments);
548                 syslog(LOG_DEBUG, "assigning mapped IP (empty list): %u.%u.%u.%u",
549                                 (assign->addr & 0xff000000) >> 24,
550                                 (assign->addr & 0x00ff0000) >> 16,
551                                 (assign->addr & 0x0000ff00) >> 8,
552                                 (assign->addr & 0x000000ff));
553                 return true;
554         }
555
556         for (uint32_t i = 0; i < count; ++i) {
557                 if (dhcpv4_test(iface, try)) {
558                         /* test was successful: IP address is not assigned, assign it */
559                         assign->addr = try;
560                         list_add(&assign->head, &iface->dhcpv4_assignments);
561                         syslog(LOG_DEBUG, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
562                                         (assign->addr & 0xff000000) >> 24,
563                                         (assign->addr & 0x00ff0000) >> 16,
564                                         (assign->addr & 0x0000ff00) >> 8,
565                                         (assign->addr & 0x000000ff), i, count);
566                         return true;
567                 }
568                 try = (((try - start) + 1) % count) + start;
569         }
570
571         syslog(LOG_DEBUG, "can't assign any IP address -> address space is full");
572         return false;
573 }
574
575
576 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
577                 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
578                 const char *hostname)
579 {
580         struct dhcpv4_assignment *lease = NULL;
581         uint32_t raddr = ntohl(reqaddr.s_addr);
582         time_t now = odhcpd_time();
583
584         struct dhcpv4_assignment *c, *n, *a = NULL;
585         list_for_each_entry_safe(c, n, &iface->dhcpv4_assignments, head) {
586                 if (!memcmp(c->hwaddr, mac, 6)) {
587                         a = c;
588                         if (c->addr == raddr)
589                                 break;
590                 } else if (c->valid_until < now) {
591                         list_del(&c->head);
592                         free(c);
593                 }
594         }
595
596         if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
597                 bool assigned = !!a;
598                 size_t hostlen = strlen(hostname) + 1;
599
600                 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
601                         a = calloc(1, sizeof(*a) + hostlen);
602                         if (!a) {
603                                 syslog(LOG_ERR, "Failed to calloc binding on interface %s", iface->ifname);
604                                 return NULL;
605                         }
606                         memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
607                         memcpy(a->hostname, hostname, hostlen);
608
609                         assigned = dhcpv4_assign(iface, a, raddr);
610                 }
611
612                 if (assigned && !a->hostname[0] && hostname) {
613                         a = realloc(a, sizeof(*a) + hostlen);
614                         if (!a) {
615                                 syslog(LOG_ERR, "Failed to realloc binding on interface %s", iface->ifname);
616                                 return NULL;
617                         }
618                         memcpy(a->hostname, hostname, hostlen);
619
620                         // Fixup list
621                         a->head.next->prev = &a->head;
622                         a->head.prev->next = &a->head;
623                 }
624
625                 // Was only a solicitation: mark binding for removal
626                 if (assigned && a->valid_until < now) {
627                         a->valid_until = (msg == DHCPV4_MSG_DISCOVER) ? 0 :
628                                         (now + iface->dhcpv4_leasetime);
629                 } else if (!assigned && a) { // Cleanup failed assignment
630                         free(a);
631                         a = NULL;
632                 }
633
634                 if (assigned && a)
635                         lease = a;
636         } else if (msg == DHCPV4_MSG_RELEASE) {
637                 if (a && a->valid_until != LONG_MAX)
638                         a->valid_until = 0;
639         } else if (msg == DHCPV4_MSG_DECLINE && a && a->valid_until != LONG_MAX) {
640                 memset(a->hwaddr, 0, sizeof(a->hwaddr));
641                 a->valid_until = now + 3600; // Block address for 1h
642         }
643
644         dhcpv6_write_statefile();
645
646         return lease;
647 }
648