d978da8c7fa003416e951efdb65b68c2390e684c
[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
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 IPv6 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                 }
144
145                 // Parse static entries
146                 struct lease *lease;
147                 list_for_each_entry(lease, &leases, head) {
148                         // Construct entry
149                         size_t hostlen = strlen(lease->hostname) + 1;
150                         struct dhcpv4_assignment *a = calloc(1, sizeof(*a) + hostlen);
151                         if (!a) {
152                                 syslog(LOG_ERR, "Calloc failed for static lease on interface %s",
153                                         iface->ifname);
154                                 return -1;
155                         }
156                         a->addr = ntohl(lease->ipaddr.s_addr);
157                         memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
158                         memcpy(a->hostname, lease->hostname, hostlen);
159                         a->valid_until = LONG_MAX;
160
161                         // Assign to all interfaces
162                         struct dhcpv4_assignment *c;
163                         list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
164                                 if (c->addr > a->addr) {
165                                         list_add_tail(&a->head, &c->head);
166                                         break;
167                                 } else if (c->addr == a->addr) {
168                                         // Already an assignment with that number
169                                         break;
170                                 }
171                         }
172                         if (&c->head == &iface->dhcpv4_assignments) {
173                                 list_add(&a->head, &iface->dhcpv4_assignments);
174                         }
175
176                         if (!a->head.next)
177                                 free(a);
178                 }
179
180                 // Clean invalid assignments
181                 struct dhcpv4_assignment *a, *n;
182                 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
183                         if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
184                                         (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) {
185                                 list_del(&a->head);
186                                 free(a);
187                         }
188                 }
189
190
191                 if (iface->dhcpv4_leasetime < 60)
192                         iface->dhcpv4_leasetime = 43200;
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                         struct dhcpv4_assignment *a = list_first_entry(&iface->dhcpv4_assignments,
200                                         struct dhcpv4_assignment, head);
201                         list_del(&a->head);
202                         free(a->hostname);
203                         free(a);
204                 }
205
206         }
207         return 0;
208 }
209
210
211 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
212                 uint8_t type, uint8_t len, const void *data)
213 {
214         uint8_t *c = *cookie;
215         if (*cookie + 2 + len > (uint8_t*)&msg[1])
216                 return;
217
218         *c++ = type;
219         *c++ = len;
220         memcpy(c, data, len);
221
222         *cookie = c + len;
223 }
224
225
226 // Simple DHCPv6-server for information requests
227 static void handle_dhcpv4(void *addr, void *data, size_t len,
228                 struct interface *iface, _unused void *dest_addr)
229 {
230         if (!iface->dhcpv4)
231                 return;
232
233         struct dhcpv4_message *req = data;
234         if (len < offsetof(struct dhcpv4_message, options) + 4 ||
235                         req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
236                 return;
237
238         int sock = iface->dhcpv4_event.uloop.fd;
239         struct sockaddr_in ifaddr;
240         struct sockaddr_in ifnetmask;
241
242         syslog(LOG_NOTICE, "Got DHCPv4 request");
243
244         struct ifreq ifreq;
245         memcpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
246         if (ioctl(sock, SIOCGIFADDR, &ifreq)) {
247                 syslog(LOG_WARNING, "DHCPv4 failed to detect address: %s", strerror(errno));
248                 return;
249         }
250
251         memcpy(&ifaddr, &ifreq.ifr_addr, sizeof(ifaddr));
252         if (ioctl(sock, SIOCGIFNETMASK, &ifreq))
253                 return;
254
255         memcpy(&ifnetmask, &ifreq.ifr_netmask, sizeof(ifnetmask));
256         uint32_t network = ifaddr.sin_addr.s_addr & ifnetmask.sin_addr.s_addr;
257
258         if ((iface->dhcpv4_start.s_addr & ifnetmask.sin_addr.s_addr) != network ||
259                         (iface->dhcpv4_end.s_addr & ifnetmask.sin_addr.s_addr) != network) {
260                 syslog(LOG_WARNING, "DHCPv4 range out of assigned network");
261                 return;
262         }
263
264         struct ifreq ifr = {.ifr_name = ""};
265         strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
266
267         struct dhcpv4_message reply = {
268                 .op = DHCPV4_BOOTREPLY,
269                 .htype = 1,
270                 .hlen = 6,
271                 .hops = 0,
272                 .xid = req->xid,
273                 .secs = 0,
274                 .flags = req->flags,
275                 .ciaddr = {INADDR_ANY},
276                 .giaddr = req->giaddr,
277                 .siaddr = ifaddr.sin_addr,
278         };
279         memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
280
281         reply.options[0] = 0x63;
282         reply.options[1] = 0x82;
283         reply.options[2] = 0x53;
284         reply.options[3] = 0x63;
285
286         uint8_t *cookie = &reply.options[4];
287         uint8_t reqmsg = DHCPV4_MSG_REQUEST;
288         uint8_t msg = DHCPV4_MSG_ACK;
289
290         struct in_addr reqaddr = {INADDR_ANY};
291         char hostname[256];
292         hostname[0] = 0;
293
294         uint8_t *start = &req->options[4];
295         uint8_t *end = ((uint8_t*)data) + len;
296         struct dhcpv4_option *opt;
297         dhcpv4_for_each_option(start, end, opt) {
298                 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1) {
299                         reqmsg = opt->data[0];
300                 } else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
301                         memcpy(hostname, opt->data, opt->len);
302                         hostname[opt->len] = 0;
303                 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) {
304                         memcpy(&reqaddr, opt->data, 4);
305                 } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
306                         if (memcmp(opt->data, &ifaddr.sin_addr, 4))
307                                 return;
308                 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
309                         uint8_t *c = opt->data, *cend = &opt->data[opt->len];
310                         for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
311                                 size_t elen = strlen(iface->filter_class);
312                                 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
313                                         return; // Ignore from homenet
314                         }
315                 }
316         }
317
318         if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
319                         reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
320                         reqmsg != DHCPV4_MSG_RELEASE)
321                 return;
322
323         struct dhcpv4_assignment *lease = NULL;
324         if (reqmsg != DHCPV4_MSG_INFORM)
325                 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr, hostname);
326
327         if (!lease) {
328                 if (reqmsg == DHCPV4_MSG_REQUEST)
329                         msg = DHCPV4_MSG_NAK;
330                 else if (reqmsg == DHCPV4_MSG_DISCOVER)
331                         return;
332         } else if (reqmsg == DHCPV4_MSG_DISCOVER) {
333                 msg = DHCPV4_MSG_OFFER;
334         } else if (reqmsg == DHCPV4_MSG_REQUEST && reqaddr.s_addr &&
335                         reqaddr.s_addr != htonl(lease->addr)) {
336                 msg = DHCPV4_MSG_NAK;
337                 /*
338                  * DHCP client requested an IP which we can't offer to him. Probably the
339                  * client changed the network. The reply type is set to DHCPV4_MSG_NAK,
340                  * because the client should not use that IP.
341                  *
342                  * For modern devices we build an answer that includes a valid IP, like
343                  * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
344                  * perform additional DHCP round trips.
345                  *
346                  */
347         }
348
349         if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
350                 return;
351
352         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
353         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &ifaddr.sin_addr);
354
355         if (lease) {
356                 reply.yiaddr.s_addr = htonl(lease->addr);
357
358                 uint32_t val = htonl(iface->dhcpv4_leasetime);
359                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
360
361                 val = htonl(500 * iface->dhcpv4_leasetime / 1000);
362                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
363
364                 val = htonl(875 * iface->dhcpv4_leasetime / 1000);
365                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
366
367                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr);
368
369                 if (lease->hostname[0])
370                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
371                                         strlen(lease->hostname), lease->hostname);
372
373                 if (!ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
374                         struct sockaddr_in *ina = (struct sockaddr_in*)&ifr.ifr_broadaddr;
375                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &ina->sin_addr);
376                 }
377         }
378
379         if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
380                 uint16_t mtu = htons(ifr.ifr_mtu);
381                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
382         }
383
384         if (iface->search && iface->search_len <= 255) {
385                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
386                                 iface->search_len, iface->search);
387         } else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
388                 uint8_t search_buf[256];
389                 int len = dn_comp(_res.dnsrch[0], search_buf,
390                                                 sizeof(search_buf), NULL, NULL);
391                 if (len > 0)
392                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
393                                         len, search_buf);
394         }
395
396         if (iface->dhcpv4_router_cnt == 0)
397                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &ifaddr.sin_addr);
398         else
399                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
400                                 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
401
402
403         if (iface->dhcpv4_dns_cnt == 0)
404                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &ifaddr.sin_addr);
405         else
406                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
407                                 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
408
409
410         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
411
412         struct sockaddr_in dest = *((struct sockaddr_in*)addr);
413         if (req->giaddr.s_addr) {
414                 dest.sin_addr = req->giaddr;
415                 dest.sin_port = htons(DHCPV4_SERVER_PORT);
416         } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
417                 dest.sin_addr = req->ciaddr;
418                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
419         } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
420                         req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
421                 dest.sin_addr.s_addr = INADDR_BROADCAST;
422                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
423         } else {
424                 dest.sin_addr = reply.yiaddr;
425                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
426
427                 struct arpreq arp = {.arp_flags = ATF_COM};
428                 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
429                 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
430                 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
431                 ioctl(sock, SIOCSARP, &arp);
432         }
433
434         sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
435                         (struct sockaddr*)&dest, sizeof(dest));
436 }
437
438
439 static bool dhcpv4_assign(struct interface *iface,
440                 struct dhcpv4_assignment *assign, uint32_t raddr)
441 {
442         const unsigned tries = 10;
443         uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
444         uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
445         uint32_t count = end - start + 1;
446
447         // Seed RNG with checksum of DUID
448         uint32_t seed = 0;
449         for (size_t i = 0; i < sizeof(assign->hwaddr); ++i)
450                 seed += assign->hwaddr[i];
451         srand(seed);
452
453         // Try to assign up to 100x
454         for (unsigned i = 0; i < tries; ++i) {
455                 uint32_t try = (((uint32_t)rand()) % count) + start;
456                 if (i == 0 && raddr >= start && raddr <= end)
457                         try = raddr;
458                 else if (i == tries - 1)
459                         try = start;
460
461                 if (list_empty(&iface->dhcpv4_assignments)) {
462                         assign->addr = try;
463                         list_add(&assign->head, &iface->dhcpv4_assignments);
464                         return true;
465                 }
466
467                 struct dhcpv4_assignment *c;
468                 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
469                         if (c->addr > try) {
470                                 assign->addr = try;
471                                 list_add_tail(&assign->head, &c->head);
472                                 return true;
473                         } else if (c->addr == try) {
474                                 if (i < tries - 1)
475                                         break;
476                                 else
477                                         ++try;
478                         }
479                 }
480         }
481
482         return false;
483 }
484
485
486 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
487                 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
488                 const char *hostname)
489 {
490         struct dhcpv4_assignment *lease = NULL;
491         uint32_t raddr = ntohl(reqaddr.s_addr);
492         time_t now = odhcpd_time();
493
494         struct dhcpv4_assignment *c, *n, *a = NULL;
495         list_for_each_entry_safe(c, n, &iface->dhcpv4_assignments, head) {
496                 if (!memcmp(c->hwaddr, mac, 6)) {
497                         a = c;
498                         if (c->addr == raddr)
499                                 break;
500                 } else if (c->valid_until < now) {
501                         list_del(&c->head);
502                         free(c);
503                 }
504         }
505
506         if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
507                 bool assigned = !!a;
508                 size_t hostlen = strlen(hostname) + 1;
509
510                 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
511                         a = calloc(1, sizeof(*a) + hostlen);
512                         if (!a) {
513                                 syslog(LOG_ERR, "Failed to calloc binding on interface %s", iface->ifname);
514                                 return NULL;
515                         }
516                         memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
517                         memcpy(a->hostname, hostname, hostlen);
518
519                         assigned = dhcpv4_assign(iface, a, raddr);
520                 }
521
522                 if (assigned && !a->hostname[0] && hostname) {
523                         a = realloc(a, sizeof(*a) + hostlen);
524                         if (!a) {
525                                 syslog(LOG_ERR, "Failed to realloc binding on interface %s", iface->ifname);
526                                 return NULL;
527                         }
528                         memcpy(a->hostname, hostname, hostlen);
529
530                         // Fixup list
531                         a->head.next->prev = &a->head;
532                         a->head.prev->next = &a->head;
533                 }
534
535                 // Was only a solicitation: mark binding for removal
536                 if (assigned && a->valid_until < now) {
537                         a->valid_until = (msg == DHCPV4_MSG_DISCOVER) ? 0 :
538                                         (now + iface->dhcpv4_leasetime);
539                 } else if (!assigned && a) { // Cleanup failed assignment
540                         free(a);
541                         a = NULL;
542                 }
543
544                 if (assigned && a)
545                         lease = a;
546         } else if (msg == DHCPV4_MSG_RELEASE) {
547                 if (a) {
548                         a->valid_until = 0;
549                 }
550         } else if (msg == DHCPV4_MSG_DECLINE) {
551                 memset(a->hwaddr, 0, sizeof(a->hwaddr));
552                 a->valid_until = now + 3600; // Block address for 1h
553         }
554
555         dhcpv6_write_statefile();
556
557         return lease;
558 }
559