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