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