509b09230c5431d8582d1debc3415c082ccddd13
[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 uint32_t *leasetime, 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 if (lease->dhcpv4_leasetime >= 60)
180 a->leasetime = lease->dhcpv4_leasetime;
181 a->addr = ntohl(lease->ipaddr.s_addr);
182 memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
183 memcpy(a->hostname, lease->hostname, hostlen);
184 /* Static assignment */
185 a->flags |= OAF_STATIC;
186 /* Infinite valid */
187 a->valid_until = 0;
188
189 // Assign to all interfaces
190 struct dhcpv4_assignment *c;
191 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
192 if (c->addr > a->addr) {
193 list_add_tail(&a->head, &c->head);
194 break;
195 } else if (c->addr == a->addr) {
196 // Already an assignment with that number
197 break;
198 }
199 }
200 if (&c->head == &iface->dhcpv4_assignments) {
201 list_add(&a->head, &iface->dhcpv4_assignments);
202 }
203
204 if (!a->head.next)
205 free(a);
206 }
207
208 // Clean invalid assignments
209 struct dhcpv4_assignment *a, *n;
210 list_for_each_entry_safe(a, n, &iface->dhcpv4_assignments, head) {
211 if ((htonl(a->addr) & smask->sin_addr.s_addr) !=
212 (iface->dhcpv4_start.s_addr & smask->sin_addr.s_addr)) {
213 list_del(&a->head);
214 free(a);
215 }
216 }
217
218
219 if (iface->dhcpv4_leasetime < 60)
220 iface->dhcpv4_leasetime = 43200;
221
222 iface->dhcpv4_event.uloop.fd = sock;
223 iface->dhcpv4_event.handle_dgram = handle_dhcpv4;
224 odhcpd_register(&iface->dhcpv4_event);
225 } else if (iface->dhcpv4_assignments.next) {
226 while (!list_empty(&iface->dhcpv4_assignments)) {
227 struct dhcpv4_assignment *a = list_first_entry(&iface->dhcpv4_assignments,
228 struct dhcpv4_assignment, head);
229 list_del(&a->head);
230 free(a);
231 }
232
233 }
234 return 0;
235 }
236
237
238 static void dhcpv4_put(struct dhcpv4_message *msg, uint8_t **cookie,
239 uint8_t type, uint8_t len, const void *data)
240 {
241 uint8_t *c = *cookie;
242 if (*cookie + 2 + len > (uint8_t*)&msg[1])
243 return;
244
245 *c++ = type;
246 *c++ = len;
247 memcpy(c, data, len);
248
249 *cookie = c + len;
250 }
251
252 // Handler for DHCPv4 messages
253 static void handle_dhcpv4(void *addr, void *data, size_t len,
254 struct interface *iface, _unused void *dest_addr)
255 {
256 if (!iface->dhcpv4)
257 return;
258
259 struct dhcpv4_message *req = data;
260 if (len < offsetof(struct dhcpv4_message, options) + 4 ||
261 req->op != DHCPV4_BOOTREQUEST || req->hlen != 6)
262 return;
263
264 int sock = iface->dhcpv4_event.uloop.fd;
265 struct sockaddr_in ifaddr;
266 struct sockaddr_in ifnetmask;
267
268 syslog(LOG_NOTICE, "Got DHCPv4 request");
269
270 struct ifreq ifreq;
271 memcpy(ifreq.ifr_name, iface->ifname, sizeof(ifreq.ifr_name));
272 if (ioctl(sock, SIOCGIFADDR, &ifreq)) {
273 syslog(LOG_WARNING, "DHCPv4 failed to detect address: %s", strerror(errno));
274 return;
275 }
276
277 memcpy(&ifaddr, &ifreq.ifr_addr, sizeof(ifaddr));
278 if (ioctl(sock, SIOCGIFNETMASK, &ifreq))
279 return;
280
281 memcpy(&ifnetmask, &ifreq.ifr_netmask, sizeof(ifnetmask));
282 uint32_t network = ifaddr.sin_addr.s_addr & ifnetmask.sin_addr.s_addr;
283
284 if ((iface->dhcpv4_start.s_addr & ifnetmask.sin_addr.s_addr) != network ||
285 (iface->dhcpv4_end.s_addr & ifnetmask.sin_addr.s_addr) != network) {
286 syslog(LOG_WARNING, "DHCPv4 range out of assigned network");
287 return;
288 }
289
290 struct ifreq ifr = {.ifr_name = ""};
291 strncpy(ifr.ifr_name, iface->ifname, sizeof(ifr.ifr_name));
292
293 struct dhcpv4_message reply = {
294 .op = DHCPV4_BOOTREPLY,
295 .htype = 1,
296 .hlen = 6,
297 .hops = 0,
298 .xid = req->xid,
299 .secs = 0,
300 .flags = req->flags,
301 .ciaddr = {INADDR_ANY},
302 .giaddr = req->giaddr,
303 .siaddr = ifaddr.sin_addr,
304 };
305 memcpy(reply.chaddr, req->chaddr, sizeof(reply.chaddr));
306
307 reply.options[0] = 0x63;
308 reply.options[1] = 0x82;
309 reply.options[2] = 0x53;
310 reply.options[3] = 0x63;
311
312 uint8_t *cookie = &reply.options[4];
313 uint8_t reqmsg = DHCPV4_MSG_REQUEST;
314 uint8_t msg = DHCPV4_MSG_ACK;
315
316 struct in_addr reqaddr = {INADDR_ANY};
317 uint32_t leasetime = 0;
318 char hostname[256];
319 hostname[0] = 0;
320
321 uint8_t *start = &req->options[4];
322 uint8_t *end = ((uint8_t*)data) + len;
323 struct dhcpv4_option *opt;
324 dhcpv4_for_each_option(start, end, opt) {
325 if (opt->type == DHCPV4_OPT_MESSAGE && opt->len == 1) {
326 reqmsg = opt->data[0];
327 } else if (opt->type == DHCPV4_OPT_HOSTNAME && opt->len > 0) {
328 memcpy(hostname, opt->data, opt->len);
329 hostname[opt->len] = 0;
330 } else if (opt->type == DHCPV4_OPT_IPADDRESS && opt->len == 4) {
331 memcpy(&reqaddr, opt->data, 4);
332 } else if (opt->type == DHCPV4_OPT_SERVERID && opt->len == 4) {
333 if (memcmp(opt->data, &ifaddr.sin_addr, 4))
334 return;
335 } else if (iface->filter_class && opt->type == DHCPV4_OPT_USER_CLASS) {
336 uint8_t *c = opt->data, *cend = &opt->data[opt->len];
337 for (; c < cend && &c[*c] < cend; c = &c[1 + *c]) {
338 size_t elen = strlen(iface->filter_class);
339 if (*c == elen && !memcmp(&c[1], iface->filter_class, elen))
340 return; // Ignore from homenet
341 }
342 } else if (opt->type == DHCPV4_OPT_LEASETIME && opt->len == 4)
343 memcpy(&leasetime, opt->data, 4);
344 }
345
346 if (reqmsg != DHCPV4_MSG_DISCOVER && reqmsg != DHCPV4_MSG_REQUEST &&
347 reqmsg != DHCPV4_MSG_INFORM && reqmsg != DHCPV4_MSG_DECLINE &&
348 reqmsg != DHCPV4_MSG_RELEASE)
349 return;
350
351 struct dhcpv4_assignment *lease = NULL;
352 if (reqmsg != DHCPV4_MSG_INFORM)
353 lease = dhcpv4_lease(iface, reqmsg, req->chaddr, reqaddr, &leasetime, hostname);
354
355 if (!lease) {
356 if (reqmsg == DHCPV4_MSG_REQUEST)
357 msg = DHCPV4_MSG_NAK;
358 else if (reqmsg == DHCPV4_MSG_DISCOVER)
359 return;
360 } else if (reqmsg == DHCPV4_MSG_DISCOVER) {
361 msg = DHCPV4_MSG_OFFER;
362 } else if (reqmsg == DHCPV4_MSG_REQUEST && reqaddr.s_addr &&
363 reqaddr.s_addr != htonl(lease->addr)) {
364 msg = DHCPV4_MSG_NAK;
365 /*
366 * DHCP client requested an IP which we can't offer to him. Probably the
367 * client changed the network. The reply type is set to DHCPV4_MSG_NAK,
368 * because the client should not use that IP.
369 *
370 * For modern devices we build an answer that includes a valid IP, like
371 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
372 * perform additional DHCP round trips.
373 *
374 */
375 }
376
377 syslog(LOG_WARNING, "received %s from %x:%x:%x:%x:%x:%x",
378 dhcpv4_msg_to_string(reqmsg),
379 req->chaddr[0],req->chaddr[1],req->chaddr[2],
380 req->chaddr[3],req->chaddr[4],req->chaddr[5]);
381
382 if (reqmsg == DHCPV4_MSG_DECLINE || reqmsg == DHCPV4_MSG_RELEASE)
383 return;
384
385 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MESSAGE, 1, &msg);
386 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SERVERID, 4, &ifaddr.sin_addr);
387
388 if (lease) {
389 uint32_t val;
390
391 reply.yiaddr.s_addr = htonl(lease->addr);
392
393 val = htonl(leasetime);
394 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
395
396 if (leasetime != UINT32_MAX) {
397 val = htonl(500 * leasetime / 1000);
398 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
399
400 val = htonl(875 * leasetime / 1000);
401 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
402 }
403
404 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr);
405
406 if (lease->hostname[0])
407 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_HOSTNAME,
408 strlen(lease->hostname), lease->hostname);
409
410 if (!ioctl(sock, SIOCGIFBRDADDR, &ifr)) {
411 struct sockaddr_in *ina = (struct sockaddr_in*)&ifr.ifr_broadaddr;
412 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_BROADCAST, 4, &ina->sin_addr);
413 }
414 }
415
416 if (!ioctl(sock, SIOCGIFMTU, &ifr)) {
417 uint16_t mtu = htons(ifr.ifr_mtu);
418 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_MTU, 2, &mtu);
419 }
420
421 if (iface->search && iface->search_len <= 255) {
422 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
423 iface->search_len, iface->search);
424 } else if (!res_init() && _res.dnsrch[0] && _res.dnsrch[0][0]) {
425 uint8_t search_buf[256];
426 int len = dn_comp(_res.dnsrch[0], search_buf,
427 sizeof(search_buf), NULL, NULL);
428 if (len > 0)
429 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_SEARCH_DOMAIN,
430 len, search_buf);
431 }
432
433 if (iface->dhcpv4_router_cnt == 0)
434 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER, 4, &ifaddr.sin_addr);
435 else
436 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_ROUTER,
437 4 * iface->dhcpv4_router_cnt, iface->dhcpv4_router);
438
439
440 if (iface->dhcpv4_dns_cnt == 0)
441 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER, 4, &ifaddr.sin_addr);
442 else
443 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_DNSSERVER,
444 4 * iface->dhcpv4_dns_cnt, iface->dhcpv4_dns);
445
446
447 dhcpv4_put(&reply, &cookie, DHCPV4_OPT_END, 0, NULL);
448
449 struct sockaddr_in dest = *((struct sockaddr_in*)addr);
450 if (req->giaddr.s_addr) {
451 /*
452 * relay agent is configured, send reply to the agent
453 */
454 dest.sin_addr = req->giaddr;
455 dest.sin_port = htons(DHCPV4_SERVER_PORT);
456 } else if (req->ciaddr.s_addr && req->ciaddr.s_addr != dest.sin_addr.s_addr) {
457 /*
458 * client has existing configuration (ciaddr is set) AND this address is
459 * not the address it used for the dhcp message
460 */
461 dest.sin_addr = req->ciaddr;
462 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
463 } else if ((ntohs(req->flags) & DHCPV4_FLAG_BROADCAST) ||
464 req->hlen != reply.hlen || !reply.yiaddr.s_addr) {
465 /*
466 * client requests a broadcast reply OR we can't offer an IP
467 */
468 dest.sin_addr.s_addr = INADDR_BROADCAST;
469 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
470 } else if (!req->ciaddr.s_addr && msg == DHCPV4_MSG_NAK) {
471 /*
472 * client has no previous configuration -> no IP, so we need to reply
473 * with a broadcast packet
474 */
475 dest.sin_addr.s_addr = INADDR_BROADCAST;
476 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
477 } else {
478 /*
479 * send reply to the newly (in this proccess) allocated IP
480 */
481 dest.sin_addr = reply.yiaddr;
482 dest.sin_port = htons(DHCPV4_CLIENT_PORT);
483
484 struct arpreq arp = {.arp_flags = ATF_COM};
485 memcpy(arp.arp_ha.sa_data, req->chaddr, 6);
486 memcpy(&arp.arp_pa, &dest, sizeof(arp.arp_pa));
487 memcpy(arp.arp_dev, iface->ifname, sizeof(arp.arp_dev));
488 ioctl(sock, SIOCSARP, &arp);
489 }
490
491 if (dest.sin_addr.s_addr == INADDR_BROADCAST) {
492 /*
493 * reply goes to IP broadcast -> MAC broadcast
494 */
495 syslog(LOG_WARNING, "sending %s to ff:ff:ff:ff:ff:ff - %s",
496 dhcpv4_msg_to_string(msg),
497 inet_ntoa(dest.sin_addr));
498 } else {
499 /*
500 * reply is send directly to IP,
501 * MAC is assumed to be the same as the request
502 */
503 syslog(LOG_WARNING, "sending %s to %x:%x:%x:%x:%x:%x - %s",
504 dhcpv4_msg_to_string(msg),
505 req->chaddr[0],req->chaddr[1],req->chaddr[2],
506 req->chaddr[3],req->chaddr[4],req->chaddr[5],
507 inet_ntoa(dest.sin_addr));
508 }
509
510 sendto(sock, &reply, sizeof(reply), MSG_DONTWAIT,
511 (struct sockaddr*)&dest, sizeof(dest));
512 }
513
514 static bool dhcpv4_test(struct interface *iface, uint32_t try)
515 {
516 struct dhcpv4_assignment *c;
517 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
518 if (c->addr == try) {
519 return false;
520 }
521 }
522 return true;
523 }
524
525 static bool dhcpv4_assign(struct interface *iface,
526 struct dhcpv4_assignment *assign, uint32_t raddr)
527 {
528 uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
529 uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
530 uint32_t count = end - start + 1;
531
532 // try to assign the IP the client asked for
533 if (start <= raddr && raddr <= end && dhcpv4_test(iface, raddr)) {
534 assign->addr = raddr;
535 list_add(&assign->head, &iface->dhcpv4_assignments);
536 syslog(LOG_DEBUG, "assigning the IP the client asked for: %u.%u.%u.%u",
537 (assign->addr & 0xff000000) >> 24,
538 (assign->addr & 0x00ff0000) >> 16,
539 (assign->addr & 0x0000ff00) >> 8,
540 (assign->addr & 0x000000ff));
541 return true;
542 }
543
544 // Seed RNG with checksum of hwaddress
545 uint32_t seed = 0;
546 for (size_t i = 0; i < sizeof(assign->hwaddr); ++i) {
547 // Knuth's multiplicative method
548 uint8_t o = assign->hwaddr[i];
549 seed += (o*2654435761) % UINT32_MAX;
550 }
551 srand(seed);
552
553 uint32_t try = (((uint32_t)rand()) % count) + start;
554
555 if (list_empty(&iface->dhcpv4_assignments)) {
556 assign->addr = try;
557 list_add(&assign->head, &iface->dhcpv4_assignments);
558 syslog(LOG_DEBUG, "assigning mapped IP (empty list): %u.%u.%u.%u",
559 (assign->addr & 0xff000000) >> 24,
560 (assign->addr & 0x00ff0000) >> 16,
561 (assign->addr & 0x0000ff00) >> 8,
562 (assign->addr & 0x000000ff));
563 return true;
564 }
565
566 for (uint32_t i = 0; i < count; ++i) {
567 if (dhcpv4_test(iface, try)) {
568 /* test was successful: IP address is not assigned, assign it */
569 assign->addr = try;
570 list_add(&assign->head, &iface->dhcpv4_assignments);
571 syslog(LOG_DEBUG, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
572 (assign->addr & 0xff000000) >> 24,
573 (assign->addr & 0x00ff0000) >> 16,
574 (assign->addr & 0x0000ff00) >> 8,
575 (assign->addr & 0x000000ff), i, count);
576 return true;
577 }
578 try = (((try - start) + 1) % count) + start;
579 }
580
581 syslog(LOG_DEBUG, "can't assign any IP address -> address space is full");
582 return false;
583 }
584
585
586 static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
587 enum dhcpv4_msg msg, const uint8_t *mac, struct in_addr reqaddr,
588 uint32_t *leasetime, const char *hostname)
589 {
590 struct dhcpv4_assignment *lease = NULL;
591 uint32_t raddr = ntohl(reqaddr.s_addr);
592 time_t now = odhcpd_time();
593
594 struct dhcpv4_assignment *c, *n, *a = NULL;
595 list_for_each_entry_safe(c, n, &iface->dhcpv4_assignments, head) {
596 if (!memcmp(c->hwaddr, mac, 6)) {
597 a = c;
598 if (c->addr == raddr)
599 break;
600 } else if (!INFINITE_VALID(c->valid_until) && c->valid_until < now) {
601 list_del(&c->head);
602 free(c);
603 }
604 }
605
606 if (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST) {
607 bool assigned = !!a;
608 size_t hostlen = strlen(hostname) + 1;
609 uint32_t my_leasetime;
610
611 if (!a && !iface->no_dynamic_dhcp) { // Create new binding
612 a = calloc(1, sizeof(*a) + hostlen);
613 if (!a) {
614 syslog(LOG_ERR, "Failed to calloc binding on interface %s", iface->ifname);
615 return NULL;
616 }
617 memcpy(a->hwaddr, mac, sizeof(a->hwaddr));
618 memcpy(a->hostname, hostname, hostlen);
619 // Don't consider new assignment as infinite
620 a->valid_until = now;
621
622 assigned = dhcpv4_assign(iface, a, raddr);
623 }
624
625 if (assigned && !a->hostname[0] && hostname) {
626 a = realloc(a, sizeof(*a) + hostlen);
627 if (!a) {
628 syslog(LOG_ERR, "Failed to realloc binding on interface %s", iface->ifname);
629 return NULL;
630 }
631 memcpy(a->hostname, hostname, hostlen);
632
633 // Fixup list
634 a->head.next->prev = &a->head;
635 a->head.prev->next = &a->head;
636 }
637
638 if (a->leasetime >= 60) {
639 my_leasetime = a->leasetime;
640 } else {
641 my_leasetime = iface->dhcpv4_leasetime;
642 }
643
644 if ((*leasetime == 0) || (my_leasetime < *leasetime))
645 *leasetime = my_leasetime;
646
647 if (assigned) {
648 if (msg == DHCPV4_MSG_DISCOVER) {
649 a->flags &= ~OAF_BOUND;
650
651 if (!(a->flags & OAF_STATIC))
652 a->valid_until = now;
653 } else {
654 a->flags |= OAF_BOUND;
655
656 if (!(a->flags & OAF_STATIC))
657 a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
658 }
659 } else if (!assigned && a) { // Cleanup failed assignment
660 free(a);
661 a = NULL;
662 }
663
664 if (assigned && a)
665 lease = a;
666 } else if (msg == DHCPV4_MSG_RELEASE && a) {
667 a->flags &= ~OAF_BOUND;
668
669 if (!(a->flags & OAF_STATIC))
670 a->valid_until = now - 1;
671
672 } else if (msg == DHCPV4_MSG_DECLINE && a) {
673 a->flags &= ~OAF_BOUND;
674
675 if (!(a->flags & OAF_STATIC)) {
676 memset(a->hwaddr, 0, sizeof(a->hwaddr));
677 a->valid_until = now + 3600; // Block address for 1h
678 }
679 }
680
681 dhcpv6_write_statefile();
682
683 return lease;
684 }
685