2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2016 Hans Dedecker <dedeckeh@gmail.com>
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.
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.
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>
36 static void free_dhcpv4_assignment(struct dhcpv4_assignment
*a
);
37 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
38 struct interface
*iface
, void *dest_addr
);
39 static struct dhcpv4_assignment
* dhcpv4_lease(struct interface
*iface
,
40 enum dhcpv4_msg msg
, const uint8_t *mac
, struct in_addr reqaddr
,
41 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
);
43 /* Create socket and register events */
49 int setup_dhcpv4_interface(struct interface
*iface
, bool enable
)
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;
57 if (iface
->dhcpv4
&& enable
) {
58 if (!iface
->dhcpv4_assignments
.next
)
59 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
61 int sock
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
63 syslog(LOG_ERR
, "Failed to create DHCPv4 server socket: %s",
68 /* Basic IPv4 configuration */
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
));
74 val
= IPTOS_PREC_INTERNETCONTROL
;
75 setsockopt(sock
, IPPROTO_IP
, IP_TOS
, &val
, sizeof(val
));
77 val
= IP_PMTUDISC_DONT
;
78 setsockopt(sock
, IPPROTO_IP
, IP_MTU_DISCOVER
, &val
, sizeof(val
));
80 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
,
81 iface
->ifname
, strlen(iface
->ifname
));
83 struct sockaddr_in bind_addr
= {AF_INET
, htons(DHCPV4_SERVER_PORT
),
86 if (bind(sock
, (struct sockaddr
*)&bind_addr
, sizeof(bind_addr
))) {
87 syslog(LOG_ERR
, "Failed to open DHCPv4 server socket: %s",
93 if (ntohl(iface
->dhcpv4_start
.s_addr
) > ntohl(iface
->dhcpv4_end
.s_addr
)) {
94 syslog(LOG_ERR
, "Invalid DHCP range");
98 /* Create a range if not specified */
100 strncpy(ifreq
.ifr_name
, iface
->ifname
, sizeof(ifreq
.ifr_name
));
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
;
109 ioctl(sock
, SIOCGIFNETMASK
, &ifreq
);
110 struct in_addr mask
= smask
->sin_addr
;
112 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
113 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
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
;
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);
137 iface
->dhcpv4_start
.s_addr
= start
| htonl(3);
138 iface
->dhcpv4_end
.s_addr
= end
| htonl(12);
143 /* Parse static entries */
145 list_for_each_entry(lease
, &leases
, head
) {
146 /* Construct entry */
147 struct dhcpv4_assignment
*a
= calloc(1, sizeof(*a
));
149 syslog(LOG_ERR
, "Calloc failed for static lease on interface %s",
154 a
->leasetime
= lease
->dhcpv4_leasetime
;
156 a
->addr
= ntohl(lease
->ipaddr
.s_addr
);
157 memcpy(a
->hwaddr
, lease
->mac
.ether_addr_octet
, sizeof(a
->hwaddr
));
158 /* Static assignment */
159 a
->flags
|= OAF_STATIC
;
163 /* Assign to all interfaces */
164 struct dhcpv4_assignment
*c
;
165 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
166 if (c
->addr
> a
->addr
) {
167 list_add_tail(&a
->head
, &c
->head
);
169 } else if (c
->addr
== a
->addr
)
170 /* Already an assignment with that number */
174 if (&c
->head
== &iface
->dhcpv4_assignments
)
175 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
178 if (lease
->hostname
[0]) {
180 a
->hostname
= strdup(lease
->hostname
);
183 free_dhcpv4_assignment(a
);
186 // Clean invalid assignments
187 struct dhcpv4_assignment
*a
, *n
;
188 list_for_each_entry_safe(a
, n
, &iface
->dhcpv4_assignments
, head
) {
189 if ((htonl(a
->addr
) & smask
->sin_addr
.s_addr
) !=
190 (iface
->dhcpv4_start
.s_addr
& smask
->sin_addr
.s_addr
))
191 free_dhcpv4_assignment(a
);
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 free_dhcpv4_assignment(list_first_entry(&iface
->dhcpv4_assignments
,
200 struct dhcpv4_assignment
, head
));
205 static char *dhcpv4_msg_to_string(uint8_t reqmsg
)
208 case (DHCPV4_MSG_DISCOVER
):
209 return "DHCPV4_MSG_DISCOVER";
210 case (DHCPV4_MSG_OFFER
):
211 return "DHCPV4_MSG_OFFER";
212 case (DHCPV4_MSG_REQUEST
):
213 return "DHCPV4_MSG_REQUEST";
214 case (DHCPV4_MSG_DECLINE
):
215 return "DHCPV4_MSG_DECLINE";
216 case (DHCPV4_MSG_ACK
):
217 return "DHCPV4_MSG_ACK";
218 case (DHCPV4_MSG_NAK
):
219 return "DHCPV4_MSG_NAK";
220 case (DHCPV4_MSG_RELEASE
):
221 return "DHCPV4_MSG_RELEASE";
222 case (DHCPV4_MSG_INFORM
):
223 return "DHCPV4_MSG_INFORM";
229 static void free_dhcpv4_assignment(struct dhcpv4_assignment
*a
)
238 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
239 uint8_t type
, uint8_t len
, const void *data
)
241 uint8_t *c
= *cookie
;
242 if (*cookie
+ 2 + len
> (uint8_t*)&msg
[1])
247 memcpy(c
, data
, len
);
252 // Handler for DHCPv4 messages
253 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
254 struct interface
*iface
, _unused
void *dest_addr
)
259 struct dhcpv4_message
*req
= data
;
260 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
261 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
264 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
265 struct sockaddr_in ifaddr
;
266 struct sockaddr_in ifnetmask
;
268 syslog(LOG_NOTICE
, "Got DHCPv4 request");
271 memcpy(ifreq
.ifr_name
, iface
->ifname
, sizeof(ifreq
.ifr_name
));
272 if (ioctl(sock
, SIOCGIFADDR
, &ifreq
)) {
273 syslog(LOG_WARNING
, "DHCPv4 failed to detect address: %s", strerror(errno
));
277 memcpy(&ifaddr
, &ifreq
.ifr_addr
, sizeof(ifaddr
));
278 if (ioctl(sock
, SIOCGIFNETMASK
, &ifreq
))
281 memcpy(&ifnetmask
, &ifreq
.ifr_netmask
, sizeof(ifnetmask
));
282 uint32_t network
= ifaddr
.sin_addr
.s_addr
& ifnetmask
.sin_addr
.s_addr
;
284 if ((iface
->dhcpv4_start
.s_addr
& ifnetmask
.sin_addr
.s_addr
) != network
||
285 (iface
->dhcpv4_end
.s_addr
& ifnetmask
.sin_addr
.s_addr
) != network
) {
286 syslog(LOG_WARNING
, "DHCPv4 range out of assigned network");
290 struct ifreq ifr
= {.ifr_name
= ""};
291 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
));
293 struct dhcpv4_message reply
= {
294 .op
= DHCPV4_BOOTREPLY
,
301 .ciaddr
= {INADDR_ANY
},
302 .giaddr
= req
->giaddr
,
303 .siaddr
= ifaddr
.sin_addr
,
305 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
307 reply
.options
[0] = 0x63;
308 reply
.options
[1] = 0x82;
309 reply
.options
[2] = 0x53;
310 reply
.options
[3] = 0x63;
312 uint8_t *cookie
= &reply
.options
[4];
313 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
314 uint8_t msg
= DHCPV4_MSG_ACK
;
316 struct in_addr reqaddr
= {INADDR_ANY
};
317 uint32_t leasetime
= 0;
318 size_t hostname_len
= 0;
321 uint8_t *start
= &req
->options
[4];
322 uint8_t *end
= ((uint8_t*)data
) + len
;
323 struct dhcpv4_option
*opt
;
324 dhcpv4_for_each_option(start
, end
, opt
) {
325 if (opt
->type
== DHCPV4_OPT_MESSAGE
&& opt
->len
== 1)
326 reqmsg
= opt
->data
[0];
327 else if (opt
->type
== DHCPV4_OPT_HOSTNAME
&& opt
->len
> 0) {
328 hostname_len
= opt
->len
;
329 memcpy(hostname
, opt
->data
, hostname_len
);
330 hostname
[hostname_len
] = 0;
331 } else if (opt
->type
== DHCPV4_OPT_IPADDRESS
&& opt
->len
== 4)
332 memcpy(&reqaddr
, opt
->data
, 4);
333 else if (opt
->type
== DHCPV4_OPT_SERVERID
&& opt
->len
== 4) {
334 if (memcmp(opt
->data
, &ifaddr
.sin_addr
, 4))
336 } else if (iface
->filter_class
&& opt
->type
== DHCPV4_OPT_USER_CLASS
) {
337 uint8_t *c
= opt
->data
, *cend
= &opt
->data
[opt
->len
];
338 for (; c
< cend
&& &c
[*c
] < cend
; c
= &c
[1 + *c
]) {
339 size_t elen
= strlen(iface
->filter_class
);
340 if (*c
== elen
&& !memcmp(&c
[1], iface
->filter_class
, elen
))
341 return; // Ignore from homenet
343 } else if (opt
->type
== DHCPV4_OPT_LEASETIME
&& opt
->len
== 4)
344 memcpy(&leasetime
, opt
->data
, 4);
347 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
348 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
349 reqmsg
!= DHCPV4_MSG_RELEASE
)
352 struct dhcpv4_assignment
*lease
= NULL
;
353 if (reqmsg
!= DHCPV4_MSG_INFORM
)
354 lease
= dhcpv4_lease(iface
, reqmsg
, req
->chaddr
, reqaddr
,
355 &leasetime
, hostname
, hostname_len
);
358 if (reqmsg
== DHCPV4_MSG_REQUEST
)
359 msg
= DHCPV4_MSG_NAK
;
360 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
362 } else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
363 msg
= DHCPV4_MSG_OFFER
;
364 else if (reqmsg
== DHCPV4_MSG_REQUEST
&& reqaddr
.s_addr
&&
365 reqaddr
.s_addr
!= htonl(lease
->addr
)) {
366 msg
= DHCPV4_MSG_NAK
;
368 * DHCP client requested an IP which we can't offer to him. Probably the
369 * client changed the network. The reply type is set to DHCPV4_MSG_NAK,
370 * because the client should not use that IP.
372 * For modern devices we build an answer that includes a valid IP, like
373 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
374 * perform additional DHCP round trips.
379 syslog(LOG_WARNING
, "received %s from %02x:%02x:%02x:%02x:%02x:%02x",
380 dhcpv4_msg_to_string(reqmsg
),
381 req
->chaddr
[0],req
->chaddr
[1],req
->chaddr
[2],
382 req
->chaddr
[3],req
->chaddr
[4],req
->chaddr
[5]);
384 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
387 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
388 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &ifaddr
.sin_addr
);
393 reply
.yiaddr
.s_addr
= htonl(lease
->addr
);
395 val
= htonl(leasetime
);
396 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
398 if (leasetime
!= UINT32_MAX
) {
399 val
= htonl(500 * leasetime
/ 1000);
400 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
402 val
= htonl(875 * leasetime
/ 1000);
403 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
406 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4, &ifnetmask
.sin_addr
);
409 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
410 strlen(lease
->hostname
), lease
->hostname
);
412 if (!ioctl(sock
, SIOCGIFBRDADDR
, &ifr
)) {
413 struct sockaddr_in
*ina
= (struct sockaddr_in
*)&ifr
.ifr_broadaddr
;
414 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &ina
->sin_addr
);
418 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
419 uint16_t mtu
= htons(ifr
.ifr_mtu
);
420 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
423 if (iface
->search
&& iface
->search_len
<= 255)
424 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
425 iface
->search_len
, iface
->search
);
426 else if (!res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
427 uint8_t search_buf
[256];
428 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
429 sizeof(search_buf
), NULL
, NULL
);
431 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
435 if (iface
->dhcpv4_router_cnt
== 0)
436 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &ifaddr
.sin_addr
);
438 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
,
439 4 * iface
->dhcpv4_router_cnt
, iface
->dhcpv4_router
);
442 if (iface
->dhcpv4_dns_cnt
== 0)
443 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &ifaddr
.sin_addr
);
445 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
446 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
449 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
451 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
452 if (req
->giaddr
.s_addr
) {
454 * relay agent is configured, send reply to the agent
456 dest
.sin_addr
= req
->giaddr
;
457 dest
.sin_port
= htons(DHCPV4_SERVER_PORT
);
458 } else if (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= dest
.sin_addr
.s_addr
) {
460 * client has existing configuration (ciaddr is set) AND this address is
461 * not the address it used for the dhcp message
463 dest
.sin_addr
= req
->ciaddr
;
464 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
465 } else if ((ntohs(req
->flags
) & DHCPV4_FLAG_BROADCAST
) ||
466 req
->hlen
!= reply
.hlen
|| !reply
.yiaddr
.s_addr
) {
468 * client requests a broadcast reply OR we can't offer an IP
470 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
471 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
472 } else if (!req
->ciaddr
.s_addr
&& msg
== DHCPV4_MSG_NAK
) {
474 * client has no previous configuration -> no IP, so we need to reply
475 * with a broadcast packet
477 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
478 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
481 * send reply to the newly (in this proccess) allocated IP
483 dest
.sin_addr
= reply
.yiaddr
;
484 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
486 struct arpreq arp
= {.arp_flags
= ATF_COM
};
487 memcpy(arp
.arp_ha
.sa_data
, req
->chaddr
, 6);
488 memcpy(&arp
.arp_pa
, &dest
, sizeof(arp
.arp_pa
));
489 memcpy(arp
.arp_dev
, iface
->ifname
, sizeof(arp
.arp_dev
));
490 ioctl(sock
, SIOCSARP
, &arp
);
493 if (dest
.sin_addr
.s_addr
== INADDR_BROADCAST
)
494 /* reply goes to IP broadcast -> MAC broadcast */
495 syslog(LOG_WARNING
, "sending %s to ff:ff:ff:ff:ff:ff - %s",
496 dhcpv4_msg_to_string(msg
),
497 inet_ntoa(dest
.sin_addr
));
500 * reply is send directly to IP,
501 * MAC is assumed to be the same as the request
503 syslog(LOG_WARNING
, "sending %s to %02x:%02x:%02x:%02x:%02x:%02x - %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
));
509 sendto(sock
, &reply
, sizeof(reply
), MSG_DONTWAIT
,
510 (struct sockaddr
*)&dest
, sizeof(dest
));
513 static bool dhcpv4_test(struct interface
*iface
, uint32_t try)
515 struct dhcpv4_assignment
*c
;
516 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
525 static bool dhcpv4_assign(struct interface
*iface
,
526 struct dhcpv4_assignment
*assign
, uint32_t raddr
)
528 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
529 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
530 uint32_t count
= end
- start
+ 1;
532 // try to assign the IP the client asked for
533 if (start
<= raddr
&& raddr
<= end
&& dhcpv4_test(iface
, raddr
)) {
534 assign
->addr
= raddr
;
535 list_add(&assign
->head
, &iface
->dhcpv4_assignments
);
536 syslog(LOG_INFO
, "assigning the IP the client asked for: %u.%u.%u.%u",
537 (assign
->addr
& 0xff000000) >> 24,
538 (assign
->addr
& 0x00ff0000) >> 16,
539 (assign
->addr
& 0x0000ff00) >> 8,
540 (assign
->addr
& 0x000000ff));
544 // Seed RNG with checksum of hwaddress
546 for (size_t i
= 0; i
< sizeof(assign
->hwaddr
); ++i
) {
547 // Knuth's multiplicative method
548 uint8_t o
= assign
->hwaddr
[i
];
549 seed
+= (o
*2654435761) % UINT32_MAX
;
554 uint32_t try = (((uint32_t)rand()) % count
) + start
;
556 if (list_empty(&iface
->dhcpv4_assignments
)) {
558 list_add(&assign
->head
, &iface
->dhcpv4_assignments
);
559 syslog(LOG_INFO
, "assigning mapped IP (empty list): %u.%u.%u.%u",
560 (assign
->addr
& 0xff000000) >> 24,
561 (assign
->addr
& 0x00ff0000) >> 16,
562 (assign
->addr
& 0x0000ff00) >> 8,
563 (assign
->addr
& 0x000000ff));
567 for (uint32_t i
= 0; i
< count
; ++i
) {
568 if (dhcpv4_test(iface
, try)) {
569 /* test was successful: IP address is not assigned, assign it */
571 list_add(&assign
->head
, &iface
->dhcpv4_assignments
);
572 syslog(LOG_DEBUG
, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
573 (assign
->addr
& 0xff000000) >> 24,
574 (assign
->addr
& 0x00ff0000) >> 16,
575 (assign
->addr
& 0x0000ff00) >> 8,
576 (assign
->addr
& 0x000000ff), i
, count
);
579 try = (((try - start
) + 1) % count
) + start
;
582 syslog(LOG_WARNING
, "can't assign any IP address -> address space is full");
587 static struct dhcpv4_assignment
* dhcpv4_lease(struct interface
*iface
,
588 enum dhcpv4_msg msg
, const uint8_t *mac
, struct in_addr reqaddr
,
589 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
)
591 struct dhcpv4_assignment
*lease
= NULL
;
592 uint32_t raddr
= ntohl(reqaddr
.s_addr
);
593 time_t now
= odhcpd_time();
595 struct dhcpv4_assignment
*c
, *n
, *a
= NULL
;
596 list_for_each_entry_safe(c
, n
, &iface
->dhcpv4_assignments
, head
) {
597 if (!memcmp(c
->hwaddr
, mac
, 6)) {
599 if (c
->addr
== raddr
)
601 } else if (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
)
602 free_dhcpv4_assignment(c
);
605 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
607 uint32_t my_leasetime
;
609 if (!a
&& !iface
->no_dynamic_dhcp
) {
610 /* Create new binding */
611 a
= calloc(1, sizeof(*a
));
613 syslog(LOG_ERR
, "Failed to calloc binding on interface %s", iface
->ifname
);
616 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
617 /* Don't consider new assignment as infinite */
618 a
->valid_until
= now
;
620 assigned
= dhcpv4_assign(iface
, a
, raddr
);
624 my_leasetime
= a
->leasetime
;
626 my_leasetime
= iface
->dhcpv4_leasetime
;
628 if ((*leasetime
== 0) || (my_leasetime
< *leasetime
))
629 *leasetime
= my_leasetime
;
632 if (msg
== DHCPV4_MSG_DISCOVER
) {
633 a
->flags
&= ~OAF_BOUND
;
635 if (!(a
->flags
& OAF_STATIC
))
636 a
->valid_until
= now
;
638 if (hostname_len
> 0) {
639 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
641 memcpy(a
->hostname
, hostname
, hostname_len
);
642 a
->hostname
[hostname_len
] = 0;
646 a
->flags
|= OAF_BOUND
;
648 if (!(a
->flags
& OAF_STATIC
))
649 a
->valid_until
= ((*leasetime
== UINT32_MAX
) ? 0 : (time_t)(now
+ *leasetime
));
651 } else if (!assigned
&& a
) {
652 /* Cleanup failed assignment */
653 free_dhcpv4_assignment(a
);
659 } else if (msg
== DHCPV4_MSG_RELEASE
&& a
) {
660 a
->flags
&= ~OAF_BOUND
;
662 if (!(a
->flags
& OAF_STATIC
))
663 a
->valid_until
= now
- 1;
665 } else if (msg
== DHCPV4_MSG_DECLINE
&& a
) {
666 a
->flags
&= ~OAF_BOUND
;
668 if (!(a
->flags
& OAF_STATIC
)) {
669 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
670 a
->valid_until
= now
+ 3600; /* Block address for 1h */
674 dhcpv6_write_statefile();