2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
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>
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 uint32_t *leasetime, const char *hostname);
42 // Create socket and register events
48 char *dhcpv4_msg_to_string(uint8_t 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";
72 int setup_dhcpv4_interface(struct interface *iface, bool enable)
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;
80 if (iface->dhcpv4 && enable) {
81 if (!iface->dhcpv4_assignments.next)
82 INIT_LIST_HEAD(&iface->dhcpv4_assignments);
84 int sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
86 syslog(LOG_ERR, "Failed to create DHCPv4 server socket: %s",
91 // Basic IPv6 configuration
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));
97 val = IPTOS_PREC_INTERNETCONTROL;
98 setsockopt(sock, IPPROTO_IP, IP_TOS, &val, sizeof(val));
100 val = IP_PMTUDISC_DONT;
101 setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val));
103 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE,
104 iface->ifname, strlen(iface->ifname));
106 struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
109 if (bind(sock, (struct sockaddr*)&bind_addr, sizeof(bind_addr))) {
110 syslog(LOG_ERR, "Failed to open DHCPv4 server socket: %s",
116 if (ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
117 syslog(LOG_ERR, "Invalid DHCP range");
121 // Create a range if not specified
123 strncpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
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;
132 ioctl(sock, SIOCGIFNETMASK, &ifreq);
133 struct in_addr mask = smask->sin_addr;
135 uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
136 uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
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;
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);
160 iface->dhcpv4_start.s_addr = start | htonl(3);
161 iface->dhcpv4_end.s_addr = end | htonl(12);
168 // Parse static entries
170 list_for_each_entry(lease, &leases, head) {
172 size_t hostlen = strlen(lease->hostname) + 1;
173 struct dhcpv4_assignment *a = calloc(1, sizeof(*a) + hostlen);
175 syslog(LOG_ERR, "Calloc failed for static lease on interface %s",
179 if (lease->dhcpv4_leasetime >= 60)
180 a->leasetime = lease->dhcpv4_leasetime;
181 a->addr = ntohl(lease->ipaddr.s_addr);
182 memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
183 memcpy(a->hostname, lease->hostname, hostlen);
184 /* Static assignment */
185 a->flags |= OAF_STATIC;
189 // Assign to all interfaces
190 struct dhcpv4_assignment *c;
191 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
192 if (c->addr > a->addr) {
193 list_add_tail(&a->head, &c->head);
195 } else if (c->addr == a->addr) {
196 // Already an assignment with that number
200 if (&c->head == &iface->dhcpv4_assignments) {
201 list_add(&a->head, &iface->dhcpv4_assignments);
208 // Clean invalid assignments
209 struct dhcpv4_assignment *a, *n;
210 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
211 if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
212 (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) {
219 if (iface->dhcpv4_leasetime < 60)
220 iface->dhcpv4_leasetime = 43200;
222 iface->dhcpv4_event.uloop.fd = sock;
223 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
224 odhcpd_register(&iface->dhcpv4_event);
225 } else if (iface->dhcpv4_assignments.next) {
226 while (!list_empty(&iface->dhcpv4_assignments)) {
227 struct dhcpv4_assignment *a = list_first_entry(&iface->dhcpv4_assignments,
228 struct dhcpv4_assignment, head);
238 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
239 uint8_t type, uint8_t len, const void *data)
241 uint8_t *c = *cookie;
242 if (*cookie + 2 + len > (uint8_t*)&msg[1])
247 memcpy(c, data, len);
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)
259 struct dhcpv4_message *req = data;
260 if (len < offsetof(struct dhcpv4_message, options) + 4 ||
261 req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
264 int sock = iface->dhcpv4_event.uloop.fd;
265 struct sockaddr_in ifaddr;
266 struct sockaddr_in ifnetmask;
268 syslog(LOG_NOTICE, "Got DHCPv4 request");
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));
277 memcpy(&ifaddr, &ifreq.ifr_addr, sizeof(ifaddr));
278 if (ioctl(sock, SIOCGIFNETMASK, &ifreq))
281 memcpy(&ifnetmask, &ifreq.ifr_netmask, sizeof(ifnetmask));
282 uint32_t network = ifaddr.sin_addr.s_addr & ifnetmask.sin_addr.s_addr;
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");
290 struct ifreq ifr = {.ifr_name = ""};
291 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
293 struct dhcpv4_message reply = {
294 .op = DHCPV4_BOOTREPLY,
301 .ciaddr = {INADDR_ANY},
302 .giaddr = req->giaddr,
303 .siaddr = ifaddr.sin_addr,
305 memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
307 reply.options[0] = 0x63;
308 reply.options[1] = 0x82;
309 reply.options[2] = 0x53;
310 reply.options[3] = 0x63;
312 uint8_t *cookie = &reply.options[4];
313 uint8_t reqmsg = DHCPV4_MSG_REQUEST;
314 uint8_t msg = DHCPV4_MSG_ACK;
316 struct in_addr reqaddr = {INADDR_ANY};
317 uint32_t leasetime = 0;
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 memcpy(hostname, opt->data, opt->len);
329 hostname[opt->len] = 0;
330 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) {
331 memcpy(&reqaddr, opt->data, 4);
332 } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
333 if (memcmp(opt->data, &ifaddr.sin_addr, 4))
335 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
336 uint8_t *c = opt->data, *cend = &opt->data[opt->len];
337 for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
338 size_t elen = strlen(iface->filter_class);
339 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
340 return; // Ignore from homenet
342 } else if (opt->type == DHCPV4_OPT_LEASETIME && opt->len == 4)
343 memcpy(&leasetime, opt->data, 4);
346 if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
347 reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
348 reqmsg != DHCPV4_MSG_RELEASE)
351 struct dhcpv4_assignment *lease = NULL;
352 if (reqmsg != DHCPV4_MSG_INFORM)
353 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr, &leasetime, hostname);
356 if (reqmsg == DHCPV4_MSG_REQUEST)
357 msg = DHCPV4_MSG_NAK;
358 else if (reqmsg == DHCPV4_MSG_DISCOVER)
360 } else if (reqmsg == DHCPV4_MSG_DISCOVER) {
361 msg = DHCPV4_MSG_OFFER;
362 } else if (reqmsg == DHCPV4_MSG_REQUEST && reqaddr.s_addr &&
363 reqaddr.s_addr != htonl(lease->addr)) {
364 msg = DHCPV4_MSG_NAK;
366 * DHCP client requested an IP which we can't offer to him. Probably the
367 * client changed the network. The reply type is set to DHCPV4_MSG_NAK,
368 * because the client should not use that IP.
370 * For modern devices we build an answer that includes a valid IP, like
371 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
372 * perform additional DHCP round trips.
377 syslog(LOG_WARNING, "received %s from %x:%x:%x:%x:%x:%x",
378 dhcpv4_msg_to_string(reqmsg),
379 req->chaddr[0],req->chaddr[1],req->chaddr[2],
380 req->chaddr[3],req->chaddr[4],req->chaddr[5]);
382 if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
385 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
386 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &ifaddr.sin_addr);
391 reply.yiaddr.s_addr = htonl(lease->addr);
393 val = htonl(leasetime);
394 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
396 if (leasetime != UINT32_MAX) {
397 val = htonl(500 * leasetime / 1000);
398 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
400 val = htonl(875 * leasetime / 1000);
401 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
404 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr);
406 if (lease->hostname[0])
407 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
408 strlen(lease->hostname), lease->hostname);
410 if (!ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
411 struct sockaddr_in *ina = (struct sockaddr_in*)&ifr.ifr_broadaddr;
412 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &ina->sin_addr);
416 if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
417 uint16_t mtu = htons(ifr.ifr_mtu);
418 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
421 if (iface->search && iface->search_len <= 255) {
422 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
423 iface->search_len, iface->search);
424 } else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
425 uint8_t search_buf[256];
426 int len = dn_comp(_res.dnsrch[0], search_buf,
427 sizeof(search_buf), NULL, NULL);
429 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
433 if (iface->dhcpv4_router_cnt == 0)
434 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &ifaddr.sin_addr);
436 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
437 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
440 if (iface->dhcpv4_dns_cnt == 0)
441 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &ifaddr.sin_addr);
443 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
444 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
447 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
449 struct sockaddr_in dest = *((struct sockaddr_in*)addr);
450 if (req->giaddr.s_addr) {
452 * relay agent is configured, send reply to the agent
454 dest.sin_addr = req->giaddr;
455 dest.sin_port = htons(DHCPV4_SERVER_PORT);
456 } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
458 * client has existing configuration (ciaddr is set) AND this address is
459 * not the address it used for the dhcp message
461 dest.sin_addr = req->ciaddr;
462 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
463 } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
464 req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
466 * client requests a broadcast reply OR we can't offer an IP
468 dest.sin_addr.s_addr = INADDR_BROADCAST;
469 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
470 } else if (!req->ciaddr.s_addr && msg == DHCPV4_MSG_NAK) {
472 * client has no previous configuration -> no IP, so we need to reply
473 * with a broadcast packet
475 dest.sin_addr.s_addr = INADDR_BROADCAST;
476 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
479 * send reply to the newly (in this proccess) allocated IP
481 dest.sin_addr = reply.yiaddr;
482 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
484 struct arpreq arp = {.arp_flags = ATF_COM};
485 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
486 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
487 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
488 ioctl(sock, SIOCSARP, &arp);
491 if (dest.sin_addr.s_addr == INADDR_BROADCAST) {
493 * 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));
500 * reply is send directly to IP,
501 * MAC is assumed to be the same as the request
503 syslog(LOG_WARNING, "sending %s to %x:%x:%x:%x:%x:%x - %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));
510 sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
511 (struct sockaddr*)&dest, sizeof(dest));
514 static bool dhcpv4_test(struct interface *iface, uint32_t try)
516 struct dhcpv4_assignment *c;
517 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
518 if (c->addr == try) {
525 static bool dhcpv4_assign(struct interface *iface,
526 struct dhcpv4_assignment *assign, uint32_t raddr)
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;
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_DEBUG, "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));
544 // Seed RNG with checksum of hwaddress
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;
553 uint32_t try = (((uint32_t)rand()) % count) + start;
555 if (list_empty(&iface->dhcpv4_assignments)) {
557 list_add(&assign->head, &iface->dhcpv4_assignments);
558 syslog(LOG_DEBUG, "assigning mapped IP (empty list): %u.%u.%u.%u",
559 (assign->addr & 0xff000000) >> 24,
560 (assign->addr & 0x00ff0000) >> 16,
561 (assign->addr & 0x0000ff00) >> 8,
562 (assign->addr & 0x000000ff));
566 for (uint32_t i = 0; i < count; ++i) {
567 if (dhcpv4_test(iface, try)) {
568 /* test was successful: IP address is not assigned, assign it */
570 list_add(&assign->head, &iface->dhcpv4_assignments);
571 syslog(LOG_DEBUG, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
572 (assign->addr & 0xff000000) >> 24,
573 (assign->addr & 0x00ff0000) >> 16,
574 (assign->addr & 0x0000ff00) >> 8,
575 (assign->addr & 0x000000ff), i, count);
578 try = (((try - start) + 1) % count) + start;
581 syslog(LOG_DEBUG, "can't assign any IP address -> address space is full");
586 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
587 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
588 uint32_t *leasetime, const char *hostname)
590 struct dhcpv4_assignment *lease = NULL;
591 uint32_t raddr = ntohl(reqaddr.s_addr);
592 time_t now = odhcpd_time();
594 struct dhcpv4_assignment *c, *n, *a = NULL;
595 list_for_each_entry_safe(c, n, &iface->dhcpv4_assignments, head) {
596 if (!memcmp(c->hwaddr, mac, 6)) {
598 if (c->addr == raddr)
600 } else if (!INFINITE_VALID(c->valid_until) && c->valid_until < now) {
606 if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
608 size_t hostlen = strlen(hostname) + 1;
609 uint32_t my_leasetime;
611 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
612 a = calloc(1, sizeof(*a) + hostlen);
614 syslog(LOG_ERR, "Failed to calloc binding on interface %s", iface->ifname);
617 memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
618 memcpy(a->hostname, hostname, hostlen);
619 // Don't consider new assignment as infinite
620 a->valid_until = now;
622 assigned = dhcpv4_assign(iface, a, raddr);
625 if (assigned && !a->hostname[0] && hostname) {
626 a = realloc(a, sizeof(*a) + hostlen);
628 syslog(LOG_ERR, "Failed to realloc binding on interface %s", iface->ifname);
631 memcpy(a->hostname, hostname, hostlen);
634 a->head.next->prev = &a->head;
635 a->head.prev->next = &a->head;
638 if (a->leasetime >= 60) {
639 my_leasetime = a->leasetime;
641 my_leasetime = iface->dhcpv4_leasetime;
644 if ((*leasetime == 0) || (my_leasetime < *leasetime))
645 *leasetime = my_leasetime;
648 if (msg == DHCPV4_MSG_DISCOVER) {
649 a->flags &= ~OAF_BOUND;
651 if (!(a->flags & OAF_STATIC))
652 a->valid_until = now;
654 a->flags |= OAF_BOUND;
656 if (!(a->flags & OAF_STATIC))
657 a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
659 } else if (!assigned && a) { // Cleanup failed assignment
666 } else if (msg == DHCPV4_MSG_RELEASE && a) {
667 a->flags &= ~OAF_BOUND;
669 if (!(a->flags & OAF_STATIC))
670 a->valid_until = now - 1;
672 } else if (msg == DHCPV4_MSG_DECLINE && a) {
673 a->flags &= ~OAF_BOUND;
675 if (!(a->flags & OAF_STATIC)) {
676 memset(a->hwaddr, 0, sizeof(a->hwaddr));
677 a->valid_until = now + 3600; // Block address for 1h
681 dhcpv6_write_statefile();