treewide: align syslog tracing
[oweals/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 <libubox/md5.h>
33
34 #include "odhcpd.h"
35 #include "dhcpv4.h"
36 #include "dhcpv6.h"
37
38 static void dhcpv4_netevent_cb(unsigned long event, struct netevent_handler_info *info);
39 static int setup_dhcpv4_addresses(struct interface *iface);
40 static void update_static_assignments(struct interface *iface);
41 static bool addr_is_fr_ip(struct interface *iface, struct in_addr *addr);
42 static void valid_until_cb(struct uloop_timeout *event);
43 static void handle_addrlist_change(struct interface *iface);
44 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a);
45 static void dhcpv4_fr_start(struct dhcpv4_assignment *a);
46 static void dhcpv4_fr_rand_delay(struct dhcpv4_assignment *a);
47 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a);
48 static void handle_dhcpv4(void *addr, void *data, size_t len,
49                 struct interface *iface, void *dest_addr);
50 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
51                 enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
52                 uint32_t *leasetime, const char *hostname, const size_t hostname_len,
53                 const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid);
54
55 static struct netevent_handler dhcpv4_netevent_handler = { .cb = dhcpv4_netevent_cb, };
56 static struct uloop_timeout valid_until_timeout = {.cb = valid_until_cb};
57 static uint32_t serial = 0;
58
59 struct odhcpd_ref_ip {
60         struct list_head head;
61         int ref_cnt;
62         struct odhcpd_ipaddr addr;
63 };
64
65 /* Create socket and register events */
66 int dhcpv4_init(void)
67 {
68         uloop_timeout_set(&valid_until_timeout, 1000);
69         netlink_add_netevent_handler(&dhcpv4_netevent_handler);
70
71         return 0;
72 }
73
74 int dhcpv4_setup_interface(struct interface *iface, bool enable)
75 {
76         int ret = 0;
77
78         if (iface->dhcpv4_event.uloop.fd > 0) {
79                 uloop_fd_delete(&iface->dhcpv4_event.uloop);
80                 close(iface->dhcpv4_event.uloop.fd);
81                 iface->dhcpv4_event.uloop.fd = -1;
82         }
83
84         if (iface->dhcpv4 && enable) {
85                 struct sockaddr_in bind_addr = {AF_INET, htons(DHCPV4_SERVER_PORT),
86                                         {INADDR_ANY}, {0}};
87                 int val = 1;
88
89                 if (!iface->dhcpv4_assignments.next)
90                         INIT_LIST_HEAD(&iface->dhcpv4_assignments);
91
92                 if (!iface->dhcpv4_fr_ips.next)
93                         INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
94
95                 iface->dhcpv4_event.uloop.fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
96                 if (iface->dhcpv4_event.uloop.fd < 0) {
97                         syslog(LOG_ERR, "socket(AF_INET): %m");
98                         ret = -1;
99                         goto out;
100                 }
101
102                 /* Basic IPv4 configuration */
103                 if (setsockopt(iface->dhcpv4_event.uloop.fd, SOL_SOCKET, SO_REUSEADDR,
104                                         &val, sizeof(val)) < 0) {
105                         syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
106                         ret = -1;
107                         goto out;
108                 }
109
110                 if (setsockopt(iface->dhcpv4_event.uloop.fd, SOL_SOCKET, SO_BROADCAST,
111                                         &val, sizeof(val)) < 0) {
112                         syslog(LOG_ERR, "setsockopt(SO_BROADCAST): %m");
113                         ret = -1;
114                         goto out;
115                 }
116
117                 if (setsockopt(iface->dhcpv4_event.uloop.fd, IPPROTO_IP, IP_PKTINFO,
118                                         &val, sizeof(val)) < 0) {
119                         syslog(LOG_ERR, "setsockopt(IP_PKTINFO): %m");
120                         ret = -1;
121                         goto out;
122                 }
123
124                 val = IPTOS_PREC_INTERNETCONTROL;
125                 if (setsockopt(iface->dhcpv4_event.uloop.fd, IPPROTO_IP, IP_TOS,
126                                         &val, sizeof(val)) < 0) {
127                         syslog(LOG_ERR, "setsockopt(IP_TOS): %m");
128                         ret = -1;
129                         goto out;
130                 }
131
132                 val = IP_PMTUDISC_DONT;
133                 if (setsockopt(iface->dhcpv4_event.uloop.fd, IPPROTO_IP, IP_MTU_DISCOVER,
134                                         &val, sizeof(val)) < 0) {
135                         syslog(LOG_ERR, "setsockopt(IP_MTU_DISCOVER): %m");
136                         ret = -1;
137                         goto out;
138                 }
139
140                 if (setsockopt(iface->dhcpv4_event.uloop.fd, SOL_SOCKET, SO_BINDTODEVICE,
141                                         iface->ifname, strlen(iface->ifname)) < 0) {
142                         syslog(LOG_ERR, "setsockopt(SO_BINDTODEVICE): %m");
143                         ret = -1;
144                         goto out;
145                 }
146
147                 if (bind(iface->dhcpv4_event.uloop.fd, (struct sockaddr*)&bind_addr,
148                                         sizeof(bind_addr)) < 0) {
149                         syslog(LOG_ERR, "bind(): %m");
150                         ret = -1;
151                         goto out;
152                 }
153
154                 if (setup_dhcpv4_addresses(iface) < 0) {
155                         ret = -1;
156                         goto out;
157                 }
158
159                 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
160                 odhcpd_register(&iface->dhcpv4_event);
161
162                 update_static_assignments(iface);
163         } else if (iface->dhcpv4_assignments.next) {
164                 while (!list_empty(&iface->dhcpv4_assignments))
165                         free_dhcpv4_assignment(list_first_entry(&iface->dhcpv4_assignments,
166                                                         struct dhcpv4_assignment, head));
167         }
168
169 out:
170         if (ret < 0 && iface->dhcpv4_event.uloop.fd > 0) {
171                 close(iface->dhcpv4_event.uloop.fd);
172                 iface->dhcpv4_event.uloop.fd = -1;
173         }
174
175         return ret;
176 }
177
178
179 static void dhcpv4_netevent_cb(unsigned long event, struct netevent_handler_info *info)
180 {
181         struct interface *iface = info->iface;
182
183         if (!iface || iface->dhcpv4 == MODE_DISABLED)
184                 return;
185
186         switch (event) {
187         case NETEV_IFINDEX_CHANGE:
188                 dhcpv4_setup_interface(iface, true);
189                 break;
190         case NETEV_ADDRLIST_CHANGE:
191                 handle_addrlist_change(iface);
192                 break;
193         default:
194                 break;
195         }
196 }
197
198 static struct dhcpv4_assignment *find_assignment_by_hwaddr(struct interface *iface, const uint8_t *hwaddr)
199 {
200         struct dhcpv4_assignment *a;
201
202         list_for_each_entry(a, &iface->dhcpv4_assignments, head)
203                 if (!memcmp(a->hwaddr, hwaddr, 6))
204                         return a;
205
206         return NULL;
207 }
208
209 static struct dhcpv4_assignment *find_assignment_by_addr(struct interface *iface, const uint32_t addr)
210 {
211         struct dhcpv4_assignment *a;
212
213         list_for_each_entry(a, &iface->dhcpv4_assignments, head)
214                 if (a->addr == addr)
215                         return a;
216
217         return NULL;
218 }
219
220 static int setup_dhcpv4_addresses(struct interface *iface)
221 {
222         iface->dhcpv4_start_ip.s_addr = INADDR_ANY;
223         iface->dhcpv4_end_ip.s_addr = INADDR_ANY;
224         iface->dhcpv4_local.s_addr = INADDR_ANY;
225         iface->dhcpv4_bcast.s_addr = INADDR_ANY;
226         iface->dhcpv4_mask.s_addr = INADDR_ANY;
227
228         /* Sanity checks */
229         if (iface->dhcpv4_start.s_addr & htonl(0xffff0000) ||
230             iface->dhcpv4_end.s_addr & htonl(0xffff0000) ||
231             ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
232                 syslog(LOG_ERR, "Invalid DHCP range for %s", iface->name);
233                 return -1;
234         }
235
236         if (!iface->addr4_len) {
237                 syslog(LOG_WARNING, "No network(s) available on %s", iface->name);
238                 return -1;
239         }
240
241         uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
242         uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
243
244         for (size_t i = 0; i < iface->addr4_len && start && end; i++) {
245                 struct in_addr *addr = &iface->addr4[i].addr.in;
246                 struct in_addr mask;
247
248                 if (addr_is_fr_ip(iface, addr))
249                         continue;
250
251                 odhcpd_bitlen2netmask(false, iface->addr4[i].prefix, &mask);
252                 if ((start & ntohl(~mask.s_addr)) == start &&
253                             (end & ntohl(~mask.s_addr)) == end) {
254                         iface->dhcpv4_start_ip.s_addr = htonl(start) |
255                                                         (addr->s_addr & mask.s_addr);
256                         iface->dhcpv4_end_ip.s_addr = htonl(end) |
257                                                         (addr->s_addr & mask.s_addr);
258                         iface->dhcpv4_local = *addr;
259                         iface->dhcpv4_bcast = iface->addr4[i].broadcast;
260                         iface->dhcpv4_mask = mask;
261                         return 0;
262                 }
263         }
264
265         /* Don't allocate IP range for subnets bigger than 28 */
266         if (iface->addr4[0].prefix > 28) {
267                 syslog(LOG_WARNING, "Auto allocation of DHCP range fails on %s", iface->name);
268                 return -1;
269         }
270
271         iface->dhcpv4_local = iface->addr4[0].addr.in;
272         iface->dhcpv4_bcast = iface->addr4[0].broadcast;
273         odhcpd_bitlen2netmask(false, iface->addr4[0].prefix, &iface->dhcpv4_mask);
274         end = start = iface->dhcpv4_local.s_addr & iface->dhcpv4_mask.s_addr;
275
276         /* Auto allocate ranges */
277         if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffff00) {
278                 iface->dhcpv4_start_ip.s_addr = start | htonl(100);
279                 iface->dhcpv4_end_ip.s_addr = end | htonl(250);
280         } else if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffffc0) {
281                 iface->dhcpv4_start_ip.s_addr = start | htonl(10);
282                 iface->dhcpv4_end_ip.s_addr = end | htonl(60);
283         } else if (ntohl(iface->dhcpv4_mask.s_addr) <= 0xffffffe0) {
284                 iface->dhcpv4_start_ip.s_addr = start | htonl(10);
285                 iface->dhcpv4_end_ip.s_addr = end | htonl(30);
286         } else {
287                 iface->dhcpv4_start_ip.s_addr = start | htonl(3);
288                 iface->dhcpv4_end_ip.s_addr = end | htonl(12);
289         }
290
291         return 0;
292 }
293
294 static void update_static_assignments(struct interface *iface)
295 {
296         struct dhcpv4_assignment *a, *c;
297
298         /* Cleanup static entries not belonging to the network */
299         list_for_each_entry_safe(a, c, &iface->dhcpv4_assignments, head) {
300                 if ((a->flags & OAF_STATIC) &&
301                                 ((a->addr & iface->dhcpv4_mask.s_addr) !=
302                                  (iface->dhcpv4_start.s_addr & iface->dhcpv4_mask.s_addr)))
303                         free_dhcpv4_assignment(a);
304         }
305
306         /* Parse static entries */
307         struct lease *lease;
308         list_for_each_entry(lease, &leases, head) {
309                 if ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) !=
310                                 (lease->ipaddr.s_addr & iface->dhcpv4_mask.s_addr)) {
311                         continue;
312                 }
313
314                 a = find_assignment_by_hwaddr(iface, lease->mac.ether_addr_octet);
315
316                 if (!a) {
317                         /* Check if there's an assignment with the specified IP address */
318                         if (find_assignment_by_addr(iface, lease->ipaddr.s_addr))
319                                 continue;
320
321                         /* Construct entry */
322                         a = calloc(1, sizeof(*a));
323                         if (!a) {
324                                 syslog(LOG_ERR, "Calloc failed for static lease on %s", iface->name);
325                                 continue;
326                         }
327                         memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
328                 }
329
330                 a->leasetime = lease->dhcpv4_leasetime;
331
332                 a->addr = lease->ipaddr.s_addr;
333                 /* Static assignment */
334                 a->flags |= OAF_STATIC;
335                 /* Infinite valid */
336                 a->valid_until = 0;
337                 a->iface = iface;
338                 if (lease->hostname[0]) {
339                         free(a->hostname);
340                         a->hostname = strdup(lease->hostname);
341                 }
342
343                 /* Assign to all interfaces */
344                 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
345                         if (ntohl(c->addr) > ntohl(a->addr)) {
346                                 list_add_tail(&a->head, &c->head);
347                                 break;
348                         }
349                 }
350
351                 if (&c->head == &iface->dhcpv4_assignments)
352                         list_add(&a->head, &iface->dhcpv4_assignments);
353         }
354 }
355
356 static void inc_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct odhcpd_ref_ip *ip)
357 {
358         *ptr = ip;
359         ip->ref_cnt++;
360 }
361
362 static void decr_ref_cnt_ip(struct odhcpd_ref_ip **ptr, struct interface *iface)
363 {
364         struct odhcpd_ref_ip *ip = *ptr;
365
366         if (--ip->ref_cnt == 0) {
367                 netlink_setup_addr(&ip->addr, iface->ifindex, false, false);
368
369                 list_del(&ip->head);
370                 free(ip);
371         }
372
373         *ptr = NULL;
374 }
375
376 static bool addr_is_fr_ip(struct interface *iface, struct in_addr *addr)
377 {
378         struct odhcpd_ref_ip *p;
379
380         list_for_each_entry(p, &iface->dhcpv4_fr_ips, head) {
381                 if (addr->s_addr == p->addr.addr.in.s_addr)
382                         return true;
383         }
384
385         return false;
386 }
387
388 static bool leases_require_fr(struct interface *iface, struct odhcpd_ipaddr *addr,
389                                 uint32_t mask)
390 {
391         struct dhcpv4_assignment *a = NULL;
392         struct odhcpd_ref_ip *fr_ip = NULL;
393
394         list_for_each_entry(a, &iface->dhcpv4_assignments, head) {
395                 if ((a->accept_fr_nonce || iface->dhcpv4_forcereconf) &&
396                     !a->fr_ip &&
397                     ((a->addr & mask) == (addr->addr.in.s_addr & mask))) {
398                         if (!fr_ip) {
399                                 fr_ip = calloc(1, sizeof(*fr_ip));
400                                 if (!fr_ip)
401                                         break;
402
403                                 list_add(&fr_ip->head, &iface->dhcpv4_fr_ips);
404                                 fr_ip->addr = *addr;
405                         }
406                         inc_ref_cnt_ip(&a->fr_ip, fr_ip);
407                 }
408         }
409
410         return fr_ip ? true : false;
411 }
412
413 static void valid_until_cb(struct uloop_timeout *event)
414 {
415         time_t now = odhcpd_time();
416         struct interface *iface;
417         list_for_each_entry(iface, &interfaces, head) {
418                 if (iface->dhcpv4 != MODE_SERVER || iface->dhcpv4_assignments.next == NULL)
419                         continue;
420
421                 struct dhcpv4_assignment *a, *n;
422                 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
423                         if (!INFINITE_VALID(a->valid_until) && a->valid_until < now)
424                                 free_dhcpv4_assignment(a);
425                 }
426         }
427         uloop_timeout_set(event, 1000);
428 }
429
430 static void handle_addrlist_change(struct interface *iface)
431 {
432         struct odhcpd_ipaddr ip;
433         struct odhcpd_ref_ip *a;
434         struct dhcpv4_assignment *c;
435         uint32_t mask = iface->dhcpv4_mask.s_addr;
436
437         memset(&ip, 0, sizeof(ip));
438         ip.addr.in = iface->dhcpv4_local;
439         ip.prefix = odhcpd_netmask2bitlen(false, &iface->dhcpv4_mask);
440         ip.broadcast = iface->dhcpv4_bcast;
441
442         setup_dhcpv4_addresses(iface);
443
444         if ((ip.addr.in.s_addr & mask) ==
445             (iface->dhcpv4_local.s_addr & iface->dhcpv4_mask.s_addr))
446                 return;
447
448         if (ip.addr.in.s_addr && !leases_require_fr(iface, &ip, mask))
449                 return;
450
451         if (iface->dhcpv4_local.s_addr == INADDR_ANY || list_empty(&iface->dhcpv4_fr_ips))
452                 return;
453
454         a = list_first_entry(&iface->dhcpv4_fr_ips, struct odhcpd_ref_ip, head);
455
456         if (netlink_setup_addr(&a->addr, iface->ifindex, false, true)) {
457                 syslog(LOG_ERR, "Failed to add ip address on %s", iface->name);
458                 return;
459         }
460
461         list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
462                 if ((c->flags & OAF_BOUND) && c->fr_ip && !c->fr_cnt) {
463                         if (c->accept_fr_nonce || iface->dhcpv4_forcereconf)
464                                 dhcpv4_fr_rand_delay(c);
465                         else
466                                 dhcpv4_fr_stop(c);
467                 }
468         }
469 }
470
471 static char *dhcpv4_msg_to_string(uint8_t reqmsg)
472 {
473         switch (reqmsg) {
474         case (DHCPV4_MSG_DISCOVER):
475                 return "DHCPV4_MSG_DISCOVER";
476         case (DHCPV4_MSG_OFFER):
477                 return "DHCPV4_MSG_OFFER";
478         case (DHCPV4_MSG_REQUEST):
479                 return "DHCPV4_MSG_REQUEST";
480         case (DHCPV4_MSG_DECLINE):
481                 return "DHCPV4_MSG_DECLINE";
482         case (DHCPV4_MSG_ACK):
483                 return "DHCPV4_MSG_ACK";
484         case (DHCPV4_MSG_NAK):
485                 return "DHCPV4_MSG_NAK";
486         case (DHCPV4_MSG_RELEASE):
487                 return "DHCPV4_MSG_RELEASE";
488         case (DHCPV4_MSG_INFORM):
489                 return "DHCPV4_MSG_INFORM";
490         case (DHCPV4_MSG_FORCERENEW):
491                 return "DHCPV4_MSG_FORCERENEW";
492         default:
493                 return "UNKNOWN";
494         }
495 }
496
497 static void free_dhcpv4_assignment(struct dhcpv4_assignment *a)
498 {
499         if (a->head.next)
500                 list_del(&a->head);
501
502         if (a->fr_ip)
503                 dhcpv4_fr_stop(a);
504
505         free(a->hostname);
506         free(a);
507 }
508
509 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
510                 uint8_t type, uint8_t len, const void *data)
511 {
512         uint8_t *c = *cookie;
513         uint8_t *end = (uint8_t *)msg + sizeof(*msg);
514
515         if (*cookie + 2 + len > end)
516                 return;
517
518         *c++ = type;
519         *c++ = len;
520         memcpy(c, data, len);
521
522         *cookie = c + len;
523 }
524
525 static void dhcpv4_fr_send(struct dhcpv4_assignment *a)
526 {
527         struct dhcpv4_message fr_msg = {
528                 .op = DHCPV4_BOOTREPLY,
529                 .htype = 1,
530                 .hlen = 6,
531                 .hops = 0,
532                 .secs = 0,
533                 .flags = 0,
534                 .ciaddr = {INADDR_ANY},
535                 .yiaddr = {INADDR_ANY},
536                 .siaddr = {INADDR_ANY},
537                 .giaddr = {INADDR_ANY},
538                 .chaddr = {0},
539                 .sname = {0},
540                 .file = {0},
541         };
542         struct dhcpv4_auth_forcerenew *auth_o, auth = {
543                 .protocol = 3,
544                 .algorithm = 1,
545                 .rdm = 0,
546                 .replay = {htonl(time(NULL)), htonl(++serial)},
547                 .type = 2,
548                 .key = {0},
549         };
550         struct interface *iface = a->iface;
551
552         odhcpd_urandom(&fr_msg.xid, sizeof(fr_msg.xid));
553         memcpy(fr_msg.chaddr, a->hwaddr, fr_msg.hlen);
554
555         fr_msg.options[0] = 0x63;
556         fr_msg.options[1] = 0x82;
557         fr_msg.options[2] = 0x53;
558         fr_msg.options[3] = 0x63;
559
560         uint8_t *cookie = &fr_msg.options[4];
561         uint8_t msg = DHCPV4_MSG_FORCERENEW;
562
563         dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
564         if (a->accept_fr_nonce) {
565                 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
566                 auth_o = (struct dhcpv4_auth_forcerenew *)(cookie - sizeof(auth));
567                 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
568
569                 md5_ctx_t md5;
570                 uint8_t secretbytes[64];
571                 memset(secretbytes, 0, sizeof(secretbytes));
572                 memcpy(secretbytes, a->key, sizeof(a->key));
573
574                 for (size_t i = 0; i < sizeof(secretbytes); ++i)
575                         secretbytes[i] ^= 0x36;
576
577                 md5_begin(&md5);
578                 md5_hash(secretbytes, sizeof(secretbytes), &md5);
579                 md5_hash(&fr_msg, sizeof(fr_msg), &md5);
580                 md5_end(auth_o->key, &md5);
581
582                 for (size_t i = 0; i < sizeof(secretbytes); ++i) {
583                         secretbytes[i] ^= 0x36;
584                         secretbytes[i] ^= 0x5c;
585                 }
586
587                 md5_begin(&md5);
588                 md5_hash(secretbytes, sizeof(secretbytes), &md5);
589                 md5_hash(auth_o->key, sizeof(auth_o->key), &md5);
590                 md5_end(auth_o->key, &md5);
591         } else {
592                 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_SERVERID, 4,
593                                 &a->fr_ip->addr.addr.in.s_addr);
594                 dhcpv4_put(&fr_msg, &cookie, DHCPV4_OPT_END, 0, NULL);
595         }
596
597         struct sockaddr_in dest;
598         memset(&dest, 0, sizeof(dest));
599         dest.sin_family = AF_INET;
600         dest.sin_port = htons(DHCPV4_CLIENT_PORT);
601         dest.sin_addr.s_addr = a->addr;
602
603         if (sendto(iface->dhcpv4_event.uloop.fd, &fr_msg, sizeof(fr_msg),
604                         MSG_DONTWAIT, (struct sockaddr*)&dest, sizeof(dest)) < 0)
605                 syslog(LOG_ERR, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg),
606                         odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
607         else
608                 syslog(LOG_WARNING, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg),
609                         odhcpd_print_mac(a->hwaddr, sizeof(a->hwaddr)), inet_ntoa(dest.sin_addr));
610 }
611
612 static void dhcpv4_fr_timer(struct uloop_timeout *event)
613 {
614         struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
615
616         if (a->fr_cnt > 0 && a->fr_cnt < 8) {
617                 dhcpv4_fr_send(a);
618                 uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
619                 a->fr_cnt++;
620         } else
621                 dhcpv4_fr_stop(a);
622 }
623
624 static void dhcpv4_fr_start(struct dhcpv4_assignment *a)
625 {
626         uloop_timeout_set(&a->fr_timer, 1000 << a->fr_cnt);
627         a->fr_timer.cb = dhcpv4_fr_timer;
628         a->fr_cnt++;
629
630         dhcpv4_fr_send(a);
631 }
632
633 static void dhcpv4_fr_delay_timer(struct uloop_timeout *event)
634 {
635         struct dhcpv4_assignment *a = container_of(event, struct dhcpv4_assignment, fr_timer);
636         struct interface *iface = a->iface;
637
638         (iface->dhcpv4_event.uloop.fd == -1 ? dhcpv4_fr_rand_delay(a) : dhcpv4_fr_start(a));
639 }
640
641 static void dhcpv4_fr_rand_delay(struct dhcpv4_assignment *a)
642 {
643 #define MIN_DELAY   500
644 #define MAX_FUZZ    500
645         int msecs;
646
647         odhcpd_urandom(&msecs, sizeof(msecs));
648
649         msecs = labs(msecs)%MAX_FUZZ + MIN_DELAY;
650
651         uloop_timeout_set(&a->fr_timer, msecs);
652         a->fr_timer.cb = dhcpv4_fr_delay_timer;
653 }
654
655 static void dhcpv4_fr_stop(struct dhcpv4_assignment *a)
656 {
657         uloop_timeout_cancel(&a->fr_timer);
658         decr_ref_cnt_ip(&a->fr_ip, a->iface);
659         a->fr_cnt = 0;
660         a->fr_timer.cb = NULL;
661 }
662
663 /* Handler for DHCPv4 messages */
664 static void handle_dhcpv4(void *addr, void *data, size_t len,
665                 struct interface *iface, _unused void *dest_addr)
666 {
667         if (!iface->dhcpv4)
668                 return;
669
670         struct dhcpv4_message *req = data;
671         if (len < offsetof(struct dhcpv4_message, options) + 4 ||
672                         req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
673                 return;
674
675
676         syslog(LOG_NOTICE, "Got DHCPv4 request on %s", iface->name);
677
678         if (!iface->dhcpv4_start_ip.s_addr && !iface->dhcpv4_end_ip.s_addr) {
679                 syslog(LOG_WARNING, "No DHCP range available on %s", iface->name);
680                 return;
681         }
682
683         int sock = iface->dhcpv4_event.uloop.fd;
684
685         struct dhcpv4_message reply = {
686                 .op = DHCPV4_BOOTREPLY,
687                 .htype = req->htype,
688                 .hlen = req->hlen,
689                 .hops = 0,
690                 .xid = req->xid,
691                 .secs = 0,
692                 .flags = req->flags,
693                 .ciaddr = {INADDR_ANY},
694                 .giaddr = req->giaddr,
695                 .siaddr = iface->dhcpv4_local,
696         };
697         memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
698
699         reply.options[0] = 0x63;
700         reply.options[1] = 0x82;
701         reply.options[2] = 0x53;
702         reply.options[3] = 0x63;
703
704         uint8_t *cookie = &reply.options[4];
705         uint8_t reqmsg = DHCPV4_MSG_REQUEST;
706         uint8_t msg = DHCPV4_MSG_ACK;
707
708         uint32_t reqaddr = INADDR_ANY;
709         uint32_t leasetime = 0;
710         size_t hostname_len = 0;
711         char hostname[256];
712         bool accept_fr_nonce = false;
713         bool incl_fr_opt = false;
714
715         uint8_t *start = &req->options[4];
716         uint8_t *end = ((uint8_t*)data) + len;
717         struct dhcpv4_option *opt;
718         dhcpv4_for_each_option(start, end, opt) {
719                 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1)
720                         reqmsg = opt->data[0];
721                 else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
722                         hostname_len = opt->len;
723                         memcpy(hostname, opt->data, hostname_len);
724                         hostname[hostname_len] = 0;
725                 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4)
726                         memcpy(&reqaddr, opt->data, 4);
727                 else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
728                         if (memcmp(opt->data, &iface->dhcpv4_local, 4))
729                                 return;
730                 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
731                         uint8_t *c = opt->data, *cend = &opt->data[opt->len];
732                         for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
733                                 size_t elen = strlen(iface->filter_class);
734                                 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
735                                         return; // Ignore from homenet
736                         }
737                 } else if (opt->type == DHCPV4_OPT_LEASETIME && opt->len == 4)
738                         memcpy(&leasetime, opt->data, 4);
739                 else if (opt->type == DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE && opt->len > 0) {
740                         for (uint8_t i = 0; i < opt->len; i++) {
741                                 if (opt->data[i] == 1) {
742                                         accept_fr_nonce = true;
743                                         break;
744                                 }
745                         }
746
747                 }
748         }
749
750         if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
751                         reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
752                         reqmsg != DHCPV4_MSG_RELEASE)
753                 return;
754
755         struct dhcpv4_assignment *lease = NULL;
756         uint32_t serverid = iface->dhcpv4_local.s_addr;
757         uint32_t fr_serverid = INADDR_ANY;
758
759         if (reqmsg != DHCPV4_MSG_INFORM)
760                 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr,
761                                         &leasetime, hostname, hostname_len,
762                                         accept_fr_nonce, &incl_fr_opt, &fr_serverid);
763
764         if (!lease) {
765                 if (reqmsg == DHCPV4_MSG_REQUEST)
766                         msg = DHCPV4_MSG_NAK;
767                 else if (reqmsg == DHCPV4_MSG_DISCOVER)
768                         return;
769         } else if (reqmsg == DHCPV4_MSG_DISCOVER)
770                 msg = DHCPV4_MSG_OFFER;
771         else if (reqmsg == DHCPV4_MSG_REQUEST &&
772                         ((reqaddr && reqaddr != lease->addr) ||
773                          (req->ciaddr.s_addr && req->ciaddr.s_addr != lease->addr))) {
774                 msg = DHCPV4_MSG_NAK;
775                 /*
776                  * DHCP client requested an IP which we can't offer to him. Probably the
777                  * client changed the network or the network has been changed. The reply
778                  * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
779                  *
780                  * For modern devices we build an answer that includes a valid IP, like
781                  * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
782                  * perform additional DHCP round trips.
783                  *
784                  */
785
786                 /*
787                  *
788                  * Buggy clients do serverid checking in nack messages; therefore set the
789                  * serverid in nack messages triggered by a previous force renew equal to
790                  * the server id in use at that time by the server
791                  *
792                  */
793                 if (fr_serverid)
794                         serverid = fr_serverid;
795
796                 if (req->ciaddr.s_addr &&
797                                 ((iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr) !=
798                                  (req->ciaddr.s_addr & iface->dhcpv4_mask.s_addr)))
799                         req->ciaddr.s_addr = INADDR_ANY;
800         }
801
802         syslog(LOG_WARNING, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg),
803                         odhcpd_print_mac(req->chaddr, req->hlen), iface->name);
804
805 #ifdef WITH_UBUS
806         if (reqmsg == DHCPV4_MSG_RELEASE)
807                 ubus_bcast_dhcp_event("dhcp.release", req->chaddr, req->hlen,
808                                         &req->ciaddr, hostname, iface->ifname);
809 #endif
810         if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
811                 return;
812
813         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
814         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &serverid);
815
816         if (lease) {
817                 uint32_t val;
818
819                 reply.yiaddr.s_addr = lease->addr;
820
821                 val = htonl(leasetime);
822                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
823
824                 if (leasetime != UINT32_MAX) {
825                         val = htonl(500 * leasetime / 1000);
826                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
827
828                         val = htonl(875 * leasetime / 1000);
829                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
830                 }
831
832                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4,
833                                 &iface->dhcpv4_mask.s_addr);
834
835                 if (lease->hostname)
836                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
837                                         strlen(lease->hostname), lease->hostname);
838
839                 if (iface->dhcpv4_bcast.s_addr != INADDR_ANY)
840                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &iface->dhcpv4_bcast);
841
842                 if (incl_fr_opt) {
843                         if (reqmsg == DHCPV4_MSG_REQUEST) {
844                                 struct dhcpv4_auth_forcerenew auth = {
845                                         .protocol = 3,
846                                         .algorithm = 1,
847                                         .rdm = 0,
848                                         .replay = {htonl(time(NULL)), htonl(++serial)},
849                                         .type = 1,
850                                         .key = {0},
851                                 };
852
853                                 memcpy(auth.key, lease->key, sizeof(auth.key));
854                                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_AUTHENTICATION, sizeof(auth), &auth);
855                         } else {
856                                 uint8_t one = 1;
857                                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE,
858                                         sizeof(one), &one);
859                         }
860                 }
861         }
862
863         struct ifreq ifr;
864
865         memset(&ifr, 0, sizeof(ifr));
866         strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name) - 1);
867
868         if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
869                 uint16_t mtu = htons(ifr.ifr_mtu);
870                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
871         }
872
873         if (iface->search && iface->search_len <= 255)
874                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
875                                 iface->search_len, iface->search);
876         else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
877                 uint8_t search_buf[256];
878                 int len = dn_comp(_res.dnsrch[0], search_buf,
879                                                 sizeof(search_buf), NULL, NULL);
880                 if (len > 0)
881                         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
882                                         len, search_buf);
883         }
884
885         if (iface->dhcpv4_router_cnt == 0)
886                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &iface->dhcpv4_local);
887         else
888                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
889                                 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
890
891
892         if (iface->dhcpv4_dns_cnt == 0)
893                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &iface->dhcpv4_local);
894         else
895                 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
896                                 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
897
898
899         dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
900
901         struct sockaddr_in dest = *((struct sockaddr_in*)addr);
902         if (req->giaddr.s_addr) {
903                 /*
904                  * relay agent is configured, send reply to the agent
905                  */
906                 dest.sin_addr = req->giaddr;
907                 dest.sin_port = htons(DHCPV4_SERVER_PORT);
908         } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
909                 /*
910                  * client has existing configuration (ciaddr is set) AND this address is
911                  * not the address it used for the dhcp message
912                  */
913                 dest.sin_addr = req->ciaddr;
914                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
915         } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
916                         req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
917                 /*
918                  * client requests a broadcast reply OR we can't offer an IP
919                  */
920                 dest.sin_addr.s_addr = INADDR_BROADCAST;
921                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
922         } else if (!req->ciaddr.s_addr && msg == DHCPV4_MSG_NAK) {
923                 /*
924                  * client has no previous configuration -> no IP, so we need to reply
925                  * with a broadcast packet
926                  */
927                 dest.sin_addr.s_addr = INADDR_BROADCAST;
928                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
929         } else {
930                 struct arpreq arp = {.arp_flags = ATF_COM};
931
932                 /*
933                  * send reply to the newly (in this proccess) allocated IP
934                  */
935                 dest.sin_addr = reply.yiaddr;
936                 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
937
938                 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
939                 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
940                 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
941
942                 if (ioctl(sock, SIOCSARP, &arp) < 0)
943                         syslog(LOG_ERR, "ioctl(SIOCSARP): %m");
944         }
945
946         if (sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
947                         (struct sockaddr*)&dest, sizeof(dest)) < 0)
948                 syslog(LOG_ERR, "Failed to send %s to %s - %s: %m",
949                         dhcpv4_msg_to_string(msg),
950                         dest.sin_addr.s_addr == INADDR_BROADCAST ?
951                         "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
952                         inet_ntoa(dest.sin_addr));
953         else
954                 syslog(LOG_ERR, "Sent %s to %s - %s",
955                         dhcpv4_msg_to_string(msg),
956                         dest.sin_addr.s_addr == INADDR_BROADCAST ?
957                         "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req->chaddr, req->hlen),
958                         inet_ntoa(dest.sin_addr));
959
960
961 #ifdef WITH_UBUS
962         if (msg == DHCPV4_MSG_ACK)
963                 ubus_bcast_dhcp_event("dhcp.ack", req->chaddr, req->hlen, &reply.yiaddr,
964                                         hostname, iface->ifname);
965 #endif
966 }
967
968 static bool dhcpv4_assign(struct interface *iface,
969                 struct dhcpv4_assignment *assign, uint32_t raddr)
970 {
971         uint32_t start = ntohl(iface->dhcpv4_start_ip.s_addr);
972         uint32_t end = ntohl(iface->dhcpv4_end_ip.s_addr);
973         uint32_t count = end - start + 1;
974
975         /* try to assign the IP the client asked for */
976         if (start <= ntohl(raddr) && ntohl(raddr) <= end &&
977                         !find_assignment_by_addr(iface, raddr)) {
978                 assign->addr = raddr;
979                 syslog(LOG_INFO, "Assigning the IP the client asked for: %u.%u.%u.%u",
980                                 ((uint8_t *)&assign->addr)[0],
981                                 ((uint8_t *)&assign->addr)[1],
982                                 ((uint8_t *)&assign->addr)[2],
983                                 ((uint8_t *)&assign->addr)[3]);
984                 return true;
985         }
986
987         /* Seed RNG with checksum of hwaddress */
988         uint32_t seed = 0;
989         for (size_t i = 0; i < sizeof(assign->hwaddr); ++i) {
990                 /* Knuth's multiplicative method */
991                 uint8_t o = assign->hwaddr[i];
992                 seed += (o*2654435761) % UINT32_MAX;
993         }
994
995         srand(seed);
996
997         uint32_t try = (((uint32_t)rand()) % count) + start;
998
999         if (list_empty(&iface->dhcpv4_assignments)) {
1000                 assign->addr = htonl(try);
1001                 syslog(LOG_INFO, "Assigning mapped IP (empty list): %u.%u.%u.%u",
1002                                 ((uint8_t *)&assign->addr)[0],
1003                                 ((uint8_t *)&assign->addr)[1],
1004                                 ((uint8_t *)&assign->addr)[2],
1005                                 ((uint8_t *)&assign->addr)[3]);
1006                 return true;
1007         }
1008
1009         for (uint32_t i = 0; i < count; ++i) {
1010                 if (!find_assignment_by_addr(iface, htonl(try))) {
1011                         /* test was successful: IP address is not assigned, assign it */
1012                         assign->addr = htonl(try);
1013                         syslog(LOG_DEBUG, "Assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
1014                                         ((uint8_t *)&assign->addr)[0],
1015                                         ((uint8_t *)&assign->addr)[1],
1016                                         ((uint8_t *)&assign->addr)[2],
1017                                         ((uint8_t *)&assign->addr)[3],
1018                                         i, count);
1019                         return true;
1020                 }
1021                 try = (((try - start) + 1) % count) + start;
1022         }
1023
1024         syslog(LOG_WARNING, "Can't assign any IP address -> address space is full");
1025         return false;
1026 }
1027
1028
1029 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
1030                 enum dhcpv4_msg msg, const uint8_t *mac, const uint32_t reqaddr,
1031                 uint32_t *leasetime, const char *hostname, const size_t hostname_len,
1032                 const bool accept_fr_nonce, bool *incl_fr_opt, uint32_t *fr_serverid)
1033 {
1034         struct dhcpv4_assignment *a = find_assignment_by_hwaddr(iface, mac);
1035         struct dhcpv4_assignment *lease = NULL;
1036         time_t now = odhcpd_time();
1037
1038         if (a && (a->flags & OAF_BOUND) && a->fr_ip) {
1039                 *fr_serverid = a->fr_ip->addr.addr.in.s_addr;
1040                 dhcpv4_fr_stop(a);
1041         }
1042
1043         if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
1044                 bool assigned = !!a;
1045
1046                 if (!a) {
1047                         if (!iface->no_dynamic_dhcp) {
1048                                 /* Create new binding */
1049                                 a = calloc(1, sizeof(*a));
1050                                 if (!a) {
1051                                         syslog(LOG_ERR, "Failed to calloc binding on interface %s",
1052                                                         iface->ifname);
1053                                         return NULL;
1054                                 }
1055                                 memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
1056                                 /* Don't consider new assignment as infinite */
1057                                 a->valid_until = now;
1058
1059                                 assigned = dhcpv4_assign(iface, a, reqaddr);
1060                                 if (assigned) {
1061                                         a->iface = iface;
1062                                         list_add(&a->head, &iface->dhcpv4_assignments);
1063                                 }
1064                         }
1065                 } else if ((a->addr & iface->dhcpv4_mask.s_addr) !=
1066                                 (iface->dhcpv4_start_ip.s_addr & iface->dhcpv4_mask.s_addr)) {
1067                         list_del(&a->head);
1068
1069                         assigned = dhcpv4_assign(iface, a, reqaddr);
1070                         if (assigned)
1071                                 list_add(&a->head, &iface->dhcpv4_assignments);
1072                 }
1073
1074                 if (assigned) {
1075                         uint32_t my_leasetime;
1076
1077                         if (a->leasetime)
1078                                 my_leasetime = a->leasetime;
1079                         else
1080                                 my_leasetime = iface->dhcpv4_leasetime;
1081
1082                         if ((*leasetime == 0) || (my_leasetime < *leasetime))
1083                                 *leasetime = my_leasetime;
1084
1085                         if (msg == DHCPV4_MSG_DISCOVER) {
1086                                 a->flags &= ~OAF_BOUND;
1087
1088                                 *incl_fr_opt = accept_fr_nonce;
1089                                 if (!(a->flags & OAF_STATIC))
1090                                         a->valid_until = now;
1091                         } else {
1092                                 if (hostname_len > 0) {
1093                                         a->hostname = realloc(a->hostname, hostname_len + 1);
1094                                         if (a->hostname) {
1095                                                 memcpy(a->hostname, hostname, hostname_len);
1096                                                 a->hostname[hostname_len] = 0;
1097
1098                                                 if (odhcpd_valid_hostname(a->hostname))
1099                                                         a->flags &= ~OAF_BROKEN_HOSTNAME;
1100                                                 else
1101                                                         a->flags |= OAF_BROKEN_HOSTNAME;
1102                                         }
1103                                 }
1104
1105                                 if (!(a->flags & OAF_BOUND)) {
1106                                         a->accept_fr_nonce = accept_fr_nonce;
1107                                         *incl_fr_opt = accept_fr_nonce;
1108                                         odhcpd_urandom(a->key, sizeof(a->key));
1109                                         a->flags |= OAF_BOUND;
1110                                 } else
1111                                         *incl_fr_opt = false;
1112
1113                                 if (!(a->flags & OAF_STATIC))
1114                                         a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
1115                         }
1116                 } else if (!assigned && a) {
1117                         /* Cleanup failed assignment */
1118                         free_dhcpv4_assignment(a);
1119                         a = NULL;
1120                 }
1121
1122                 if (assigned && a)
1123                         lease = a;
1124         } else if (msg == DHCPV4_MSG_RELEASE && a) {
1125                 a->flags &= ~OAF_BOUND;
1126
1127                 if (!(a->flags & OAF_STATIC))
1128                         a->valid_until = now - 1;
1129
1130         } else if (msg == DHCPV4_MSG_DECLINE && a) {
1131                 a->flags &= ~OAF_BOUND;
1132
1133                 if (!(a->flags & OAF_STATIC)) {
1134                         memset(a->hwaddr, 0, sizeof(a->hwaddr));
1135                         a->valid_until = now + 3600; /* Block address for 1h */
1136                 }
1137         }
1138
1139         dhcpv6_write_statefile();
1140
1141         return lease;
1142 }