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>
32 #include <libubox/md5.h>
38 #define PACKET_SIZE(start, end) (((uint8_t *)end - (uint8_t *)start) < DHCPV4_MIN_PACKET_SIZE ? \
39 DHCPV4_MIN_PACKET_SIZE : (uint8_t *)end - (uint8_t *)start)
41 static void dhcpv4_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
);
42 static int setup_dhcpv4_addresses(struct interface
*iface
);
43 static bool addr_is_fr_ip(struct interface
*iface
, struct in_addr
*addr
);
44 static void valid_until_cb(struct uloop_timeout
*event
);
45 static void handle_addrlist_change(struct interface
*iface
);
46 static void dhcpv4_fr_start(struct dhcp_assignment
*a
);
47 static void dhcpv4_fr_rand_delay(struct dhcp_assignment
*a
);
48 static void dhcpv4_fr_stop(struct dhcp_assignment
*a
);
49 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
50 struct interface
*iface
, void *dest_addr
);
51 static struct dhcp_assignment
* dhcpv4_lease(struct interface
*iface
,
52 enum dhcpv4_msg msg
, const uint8_t *mac
, const uint32_t reqaddr
,
53 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
,
54 const bool accept_fr_nonce
, bool *incl_fr_opt
, uint32_t *fr_serverid
);
56 static struct netevent_handler dhcpv4_netevent_handler
= { .cb
= dhcpv4_netevent_cb
, };
57 static struct uloop_timeout valid_until_timeout
= {.cb
= valid_until_cb
};
58 static uint32_t serial
= 0;
60 struct odhcpd_ref_ip
{
61 struct list_head head
;
63 struct odhcpd_ipaddr addr
;
66 /* Create socket and register events */
69 uloop_timeout_set(&valid_until_timeout
, 1000);
70 netlink_add_netevent_handler(&dhcpv4_netevent_handler
);
75 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
)
79 if (iface
->dhcpv4_event
.uloop
.fd
>= 0) {
80 uloop_fd_delete(&iface
->dhcpv4_event
.uloop
);
81 close(iface
->dhcpv4_event
.uloop
.fd
);
82 iface
->dhcpv4_event
.uloop
.fd
= -1;
85 if (iface
->dhcpv4
&& enable
) {
86 struct sockaddr_in bind_addr
= {AF_INET
, htons(DHCPV4_SERVER_PORT
),
90 if (!iface
->dhcpv4_assignments
.next
)
91 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
93 if (!iface
->dhcpv4_fr_ips
.next
)
94 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
96 iface
->dhcpv4_event
.uloop
.fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
97 if (iface
->dhcpv4_event
.uloop
.fd
< 0) {
98 syslog(LOG_ERR
, "socket(AF_INET): %m");
103 /* Basic IPv4 configuration */
104 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, SOL_SOCKET
, SO_REUSEADDR
,
105 &val
, sizeof(val
)) < 0) {
106 syslog(LOG_ERR
, "setsockopt(SO_REUSEADDR): %m");
111 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, SOL_SOCKET
, SO_BROADCAST
,
112 &val
, sizeof(val
)) < 0) {
113 syslog(LOG_ERR
, "setsockopt(SO_BROADCAST): %m");
118 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, IPPROTO_IP
, IP_PKTINFO
,
119 &val
, sizeof(val
)) < 0) {
120 syslog(LOG_ERR
, "setsockopt(IP_PKTINFO): %m");
125 val
= IPTOS_PREC_INTERNETCONTROL
;
126 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, IPPROTO_IP
, IP_TOS
,
127 &val
, sizeof(val
)) < 0) {
128 syslog(LOG_ERR
, "setsockopt(IP_TOS): %m");
133 val
= IP_PMTUDISC_DONT
;
134 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, IPPROTO_IP
, IP_MTU_DISCOVER
,
135 &val
, sizeof(val
)) < 0) {
136 syslog(LOG_ERR
, "setsockopt(IP_MTU_DISCOVER): %m");
141 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
142 iface
->ifname
, strlen(iface
->ifname
)) < 0) {
143 syslog(LOG_ERR
, "setsockopt(SO_BINDTODEVICE): %m");
148 if (bind(iface
->dhcpv4_event
.uloop
.fd
, (struct sockaddr
*)&bind_addr
,
149 sizeof(bind_addr
)) < 0) {
150 syslog(LOG_ERR
, "bind(): %m");
155 if (setup_dhcpv4_addresses(iface
) < 0) {
160 iface
->dhcpv4_event
.handle_dgram
= handle_dhcpv4
;
161 odhcpd_register(&iface
->dhcpv4_event
);
162 } else if (iface
->dhcpv4_assignments
.next
) {
163 while (!list_empty(&iface
->dhcpv4_assignments
))
164 free_assignment(list_first_entry(&iface
->dhcpv4_assignments
,
165 struct dhcp_assignment
, head
));
169 if (ret
< 0 && iface
->dhcpv4_event
.uloop
.fd
>= 0) {
170 close(iface
->dhcpv4_event
.uloop
.fd
);
171 iface
->dhcpv4_event
.uloop
.fd
= -1;
178 static void dhcpv4_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
180 struct interface
*iface
= info
->iface
;
182 if (!iface
|| iface
->dhcpv4
== MODE_DISABLED
)
186 case NETEV_IFINDEX_CHANGE
:
187 dhcpv4_setup_interface(iface
, true);
189 case NETEV_ADDRLIST_CHANGE
:
190 handle_addrlist_change(iface
);
197 static struct dhcp_assignment
*find_assignment_by_hwaddr(struct interface
*iface
, const uint8_t *hwaddr
)
199 struct dhcp_assignment
*a
;
201 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
202 if (!memcmp(a
->hwaddr
, hwaddr
, 6))
208 static int setup_dhcpv4_addresses(struct interface
*iface
)
210 iface
->dhcpv4_start_ip
.s_addr
= INADDR_ANY
;
211 iface
->dhcpv4_end_ip
.s_addr
= INADDR_ANY
;
212 iface
->dhcpv4_local
.s_addr
= INADDR_ANY
;
213 iface
->dhcpv4_bcast
.s_addr
= INADDR_ANY
;
214 iface
->dhcpv4_mask
.s_addr
= INADDR_ANY
;
217 if (iface
->dhcpv4_start
.s_addr
& htonl(0xffff0000) ||
218 iface
->dhcpv4_end
.s_addr
& htonl(0xffff0000) ||
219 ntohl(iface
->dhcpv4_start
.s_addr
) > ntohl(iface
->dhcpv4_end
.s_addr
)) {
220 syslog(LOG_ERR
, "Invalid DHCP range for %s", iface
->name
);
224 if (!iface
->addr4_len
) {
225 syslog(LOG_ERR
, "No network(s) available on %s", iface
->name
);
229 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
230 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
232 for (size_t i
= 0; i
< iface
->addr4_len
&& start
&& end
; i
++) {
233 struct in_addr
*addr
= &iface
->addr4
[i
].addr
.in
;
236 if (addr_is_fr_ip(iface
, addr
))
239 odhcpd_bitlen2netmask(false, iface
->addr4
[i
].prefix
, &mask
);
240 if ((start
& ntohl(~mask
.s_addr
)) == start
&&
241 (end
& ntohl(~mask
.s_addr
)) == end
) {
242 iface
->dhcpv4_start_ip
.s_addr
= htonl(start
) |
243 (addr
->s_addr
& mask
.s_addr
);
244 iface
->dhcpv4_end_ip
.s_addr
= htonl(end
) |
245 (addr
->s_addr
& mask
.s_addr
);
246 iface
->dhcpv4_local
= *addr
;
247 iface
->dhcpv4_bcast
= iface
->addr4
[i
].broadcast
;
248 iface
->dhcpv4_mask
= mask
;
253 /* Don't allocate IP range for subnets bigger than 28 */
254 if (iface
->addr4
[0].prefix
> 28) {
255 syslog(LOG_ERR
, "Auto allocation of DHCP range fails on %s", iface
->name
);
259 iface
->dhcpv4_local
= iface
->addr4
[0].addr
.in
;
260 iface
->dhcpv4_bcast
= iface
->addr4
[0].broadcast
;
261 odhcpd_bitlen2netmask(false, iface
->addr4
[0].prefix
, &iface
->dhcpv4_mask
);
262 end
= start
= iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
;
264 /* Auto allocate ranges */
265 if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffff00) {
266 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(100);
267 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(250);
268 } else if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffffc0) {
269 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(10);
270 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(60);
271 } else if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffffe0) {
272 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(10);
273 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(30);
275 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(3);
276 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(12);
282 static void inc_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct odhcpd_ref_ip
*ip
)
288 static void decr_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct interface
*iface
)
290 struct odhcpd_ref_ip
*ip
= *ptr
;
292 if (--ip
->ref_cnt
== 0) {
293 netlink_setup_addr(&ip
->addr
, iface
->ifindex
, false, false);
302 static bool addr_is_fr_ip(struct interface
*iface
, struct in_addr
*addr
)
304 struct odhcpd_ref_ip
*p
;
306 list_for_each_entry(p
, &iface
->dhcpv4_fr_ips
, head
) {
307 if (addr
->s_addr
== p
->addr
.addr
.in
.s_addr
)
314 static bool leases_require_fr(struct interface
*iface
, struct odhcpd_ipaddr
*addr
,
317 struct dhcp_assignment
*a
= NULL
;
318 struct odhcpd_ref_ip
*fr_ip
= NULL
;
320 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
) {
321 if ((a
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
) &&
323 ((a
->addr
& mask
) == (addr
->addr
.in
.s_addr
& mask
))) {
325 fr_ip
= calloc(1, sizeof(*fr_ip
));
329 list_add(&fr_ip
->head
, &iface
->dhcpv4_fr_ips
);
332 inc_ref_cnt_ip(&a
->fr_ip
, fr_ip
);
336 return fr_ip
? true : false;
339 static void valid_until_cb(struct uloop_timeout
*event
)
341 struct interface
*iface
;
342 time_t now
= odhcpd_time();
344 avl_for_each_element(&interfaces
, iface
, avl
) {
345 if (iface
->dhcpv4
!= MODE_SERVER
|| iface
->dhcpv4_assignments
.next
== NULL
)
348 struct dhcp_assignment
*a
, *n
;
349 list_for_each_entry_safe(a
, n
, &iface
->dhcpv4_assignments
, head
) {
350 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
)
354 uloop_timeout_set(event
, 1000);
357 static void handle_addrlist_change(struct interface
*iface
)
359 struct odhcpd_ipaddr ip
;
360 struct odhcpd_ref_ip
*a
;
361 struct dhcp_assignment
*c
;
362 uint32_t mask
= iface
->dhcpv4_mask
.s_addr
;
364 memset(&ip
, 0, sizeof(ip
));
365 ip
.addr
.in
= iface
->dhcpv4_local
;
366 ip
.prefix
= odhcpd_netmask2bitlen(false, &iface
->dhcpv4_mask
);
367 ip
.broadcast
= iface
->dhcpv4_bcast
;
369 setup_dhcpv4_addresses(iface
);
371 if ((ip
.addr
.in
.s_addr
& mask
) ==
372 (iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
))
375 if (ip
.addr
.in
.s_addr
&& !leases_require_fr(iface
, &ip
, mask
))
378 if (iface
->dhcpv4_local
.s_addr
== INADDR_ANY
|| list_empty(&iface
->dhcpv4_fr_ips
))
381 a
= list_first_entry(&iface
->dhcpv4_fr_ips
, struct odhcpd_ref_ip
, head
);
383 if (netlink_setup_addr(&a
->addr
, iface
->ifindex
, false, true)) {
384 syslog(LOG_ERR
, "Failed to add ip address on %s", iface
->name
);
388 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
389 if ((c
->flags
& OAF_BOUND
) && c
->fr_ip
&& !c
->fr_cnt
) {
390 if (c
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
)
391 dhcpv4_fr_rand_delay(c
);
398 static char *dhcpv4_msg_to_string(uint8_t reqmsg
)
401 case (DHCPV4_MSG_DISCOVER
):
402 return "DHCPV4_MSG_DISCOVER";
403 case (DHCPV4_MSG_OFFER
):
404 return "DHCPV4_MSG_OFFER";
405 case (DHCPV4_MSG_REQUEST
):
406 return "DHCPV4_MSG_REQUEST";
407 case (DHCPV4_MSG_DECLINE
):
408 return "DHCPV4_MSG_DECLINE";
409 case (DHCPV4_MSG_ACK
):
410 return "DHCPV4_MSG_ACK";
411 case (DHCPV4_MSG_NAK
):
412 return "DHCPV4_MSG_NAK";
413 case (DHCPV4_MSG_RELEASE
):
414 return "DHCPV4_MSG_RELEASE";
415 case (DHCPV4_MSG_INFORM
):
416 return "DHCPV4_MSG_INFORM";
417 case (DHCPV4_MSG_FORCERENEW
):
418 return "DHCPV4_MSG_FORCERENEW";
424 static void dhcpv4_free_assignment(struct dhcp_assignment
*a
)
430 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
431 uint8_t type
, uint8_t len
, const void *data
)
433 uint8_t *c
= *cookie
;
434 uint8_t *end
= (uint8_t *)msg
+ sizeof(*msg
);
435 bool tag_only
= type
== DHCPV4_OPT_PAD
|| type
== DHCPV4_OPT_END
;
436 int total_len
= tag_only
? 1 : 2 + len
;
438 if (*cookie
+ total_len
> end
)
441 *cookie
+= total_len
;
448 memcpy(c
, data
, len
);
451 static void dhcpv4_fr_send(struct dhcp_assignment
*a
)
453 struct dhcpv4_message fr_msg
= {
454 .op
= DHCPV4_BOOTREPLY
,
460 .ciaddr
= {INADDR_ANY
},
461 .yiaddr
= {INADDR_ANY
},
462 .siaddr
= {INADDR_ANY
},
463 .giaddr
= {INADDR_ANY
},
468 struct dhcpv4_auth_forcerenew
*auth_o
, auth
= {
472 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
476 struct interface
*iface
= a
->iface
;
478 odhcpd_urandom(&fr_msg
.xid
, sizeof(fr_msg
.xid
));
479 memcpy(fr_msg
.chaddr
, a
->hwaddr
, fr_msg
.hlen
);
481 fr_msg
.options
[0] = 0x63;
482 fr_msg
.options
[1] = 0x82;
483 fr_msg
.options
[2] = 0x53;
484 fr_msg
.options
[3] = 0x63;
486 uint8_t *cookie
= &fr_msg
.options
[4];
487 uint8_t msg
= DHCPV4_MSG_FORCERENEW
;
489 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
490 if (a
->accept_fr_nonce
) {
491 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
492 auth_o
= (struct dhcpv4_auth_forcerenew
*)(cookie
- sizeof(auth
));
493 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
496 uint8_t secretbytes
[64];
497 memset(secretbytes
, 0, sizeof(secretbytes
));
498 memcpy(secretbytes
, a
->key
, sizeof(a
->key
));
500 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
501 secretbytes
[i
] ^= 0x36;
504 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
505 md5_hash(&fr_msg
, sizeof(fr_msg
), &md5
);
506 md5_end(auth_o
->key
, &md5
);
508 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
509 secretbytes
[i
] ^= 0x36;
510 secretbytes
[i
] ^= 0x5c;
514 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
515 md5_hash(auth_o
->key
, sizeof(auth_o
->key
), &md5
);
516 md5_end(auth_o
->key
, &md5
);
518 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_SERVERID
, 4,
519 &a
->fr_ip
->addr
.addr
.in
.s_addr
);
520 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
523 struct sockaddr_in dest
;
524 memset(&dest
, 0, sizeof(dest
));
525 dest
.sin_family
= AF_INET
;
526 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
527 dest
.sin_addr
.s_addr
= a
->addr
;
529 if (sendto(iface
->dhcpv4_event
.uloop
.fd
, &fr_msg
, PACKET_SIZE(&fr_msg
, cookie
),
530 MSG_DONTWAIT
, (struct sockaddr
*)&dest
, sizeof(dest
)) < 0)
531 syslog(LOG_ERR
, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg
),
532 odhcpd_print_mac(a
->hwaddr
, sizeof(a
->hwaddr
)), inet_ntoa(dest
.sin_addr
));
534 syslog(LOG_NOTICE
, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg
),
535 odhcpd_print_mac(a
->hwaddr
, sizeof(a
->hwaddr
)), inet_ntoa(dest
.sin_addr
));
538 static void dhcpv4_fr_timer(struct uloop_timeout
*event
)
540 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
542 if (a
->fr_cnt
> 0 && a
->fr_cnt
< 8) {
544 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
550 static void dhcpv4_fr_start(struct dhcp_assignment
*a
)
552 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
553 a
->fr_timer
.cb
= dhcpv4_fr_timer
;
559 static void dhcpv4_fr_delay_timer(struct uloop_timeout
*event
)
561 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
562 struct interface
*iface
= a
->iface
;
564 (iface
->dhcpv4_event
.uloop
.fd
== -1 ? dhcpv4_fr_rand_delay(a
) : dhcpv4_fr_start(a
));
567 static void dhcpv4_fr_rand_delay(struct dhcp_assignment
*a
)
569 #define MIN_DELAY 500
573 odhcpd_urandom(&msecs
, sizeof(msecs
));
575 msecs
= labs(msecs
)%MAX_FUZZ
+ MIN_DELAY
;
577 uloop_timeout_set(&a
->fr_timer
, msecs
);
578 a
->fr_timer
.cb
= dhcpv4_fr_delay_timer
;
581 static void dhcpv4_fr_stop(struct dhcp_assignment
*a
)
583 uloop_timeout_cancel(&a
->fr_timer
);
584 decr_ref_cnt_ip(&a
->fr_ip
, a
->iface
);
586 a
->fr_timer
.cb
= NULL
;
589 /* Handler for DHCPv4 messages */
590 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
591 struct interface
*iface
, _unused
void *dest_addr
)
593 struct dhcpv4_message
*req
= data
;
598 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
599 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
602 syslog(LOG_NOTICE
, "Got DHCPv4 request on %s", iface
->name
);
604 if (!iface
->dhcpv4_start_ip
.s_addr
&& !iface
->dhcpv4_end_ip
.s_addr
) {
605 syslog(LOG_ERR
, "No DHCP range available on %s", iface
->name
);
609 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
611 struct dhcpv4_message reply
= {
612 .op
= DHCPV4_BOOTREPLY
,
619 .ciaddr
= {INADDR_ANY
},
620 .giaddr
= req
->giaddr
,
621 .siaddr
= iface
->dhcpv4_local
,
623 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
625 reply
.options
[0] = 0x63;
626 reply
.options
[1] = 0x82;
627 reply
.options
[2] = 0x53;
628 reply
.options
[3] = 0x63;
630 uint8_t *cookie
= &reply
.options
[4];
631 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
632 uint8_t msg
= DHCPV4_MSG_ACK
;
634 uint32_t reqaddr
= INADDR_ANY
;
635 uint32_t leasetime
= 0;
636 size_t hostname_len
= 0;
638 bool accept_fr_nonce
= false;
639 bool incl_fr_opt
= false;
641 uint8_t *start
= &req
->options
[4];
642 uint8_t *end
= ((uint8_t*)data
) + len
;
643 struct dhcpv4_option
*opt
;
644 dhcpv4_for_each_option(start
, end
, opt
) {
645 if (opt
->type
== DHCPV4_OPT_MESSAGE
&& opt
->len
== 1)
646 reqmsg
= opt
->data
[0];
647 else if (opt
->type
== DHCPV4_OPT_HOSTNAME
&& opt
->len
> 0) {
648 hostname_len
= opt
->len
;
649 memcpy(hostname
, opt
->data
, hostname_len
);
650 hostname
[hostname_len
] = 0;
651 } else if (opt
->type
== DHCPV4_OPT_IPADDRESS
&& opt
->len
== 4)
652 memcpy(&reqaddr
, opt
->data
, 4);
653 else if (opt
->type
== DHCPV4_OPT_SERVERID
&& opt
->len
== 4) {
654 if (memcmp(opt
->data
, &iface
->dhcpv4_local
, 4))
656 } else if (iface
->filter_class
&& opt
->type
== DHCPV4_OPT_USER_CLASS
) {
657 uint8_t *c
= opt
->data
, *cend
= &opt
->data
[opt
->len
];
658 for (; c
< cend
&& &c
[*c
] < cend
; c
= &c
[1 + *c
]) {
659 size_t elen
= strlen(iface
->filter_class
);
660 if (*c
== elen
&& !memcmp(&c
[1], iface
->filter_class
, elen
))
661 return; // Ignore from homenet
663 } else if (opt
->type
== DHCPV4_OPT_LEASETIME
&& opt
->len
== 4)
664 memcpy(&leasetime
, opt
->data
, 4);
665 else if (opt
->type
== DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
&& opt
->len
> 0) {
666 for (uint8_t i
= 0; i
< opt
->len
; i
++) {
667 if (opt
->data
[i
] == 1) {
668 accept_fr_nonce
= true;
676 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
677 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
678 reqmsg
!= DHCPV4_MSG_RELEASE
)
681 struct dhcp_assignment
*a
= NULL
;
682 uint32_t serverid
= iface
->dhcpv4_local
.s_addr
;
683 uint32_t fr_serverid
= INADDR_ANY
;
685 if (reqmsg
!= DHCPV4_MSG_INFORM
)
686 a
= dhcpv4_lease(iface
, reqmsg
, req
->chaddr
, reqaddr
,
687 &leasetime
, hostname
, hostname_len
,
688 accept_fr_nonce
, &incl_fr_opt
, &fr_serverid
);
691 if (reqmsg
== DHCPV4_MSG_REQUEST
)
692 msg
= DHCPV4_MSG_NAK
;
693 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
695 } else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
696 msg
= DHCPV4_MSG_OFFER
;
697 else if (reqmsg
== DHCPV4_MSG_REQUEST
&&
698 ((reqaddr
&& reqaddr
!= a
->addr
) ||
699 (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= a
->addr
))) {
700 msg
= DHCPV4_MSG_NAK
;
702 * DHCP client requested an IP which we can't offer to him. Probably the
703 * client changed the network or the network has been changed. The reply
704 * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
706 * For modern devices we build an answer that includes a valid IP, like
707 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
708 * perform additional DHCP round trips.
714 * Buggy clients do serverid checking in nack messages; therefore set the
715 * serverid in nack messages triggered by a previous force renew equal to
716 * the server id in use at that time by the server
720 serverid
= fr_serverid
;
722 if (req
->ciaddr
.s_addr
&&
723 ((iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
) !=
724 (req
->ciaddr
.s_addr
& iface
->dhcpv4_mask
.s_addr
)))
725 req
->ciaddr
.s_addr
= INADDR_ANY
;
728 syslog(LOG_NOTICE
, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg
),
729 odhcpd_print_mac(req
->chaddr
, req
->hlen
), iface
->name
);
732 if (reqmsg
== DHCPV4_MSG_RELEASE
)
733 ubus_bcast_dhcp_event("dhcp.release", req
->chaddr
, req
->hlen
,
734 &req
->ciaddr
, hostname
, iface
->ifname
);
736 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
739 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
740 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &serverid
);
745 reply
.yiaddr
.s_addr
= a
->addr
;
747 val
= htonl(leasetime
);
748 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
750 if (leasetime
!= UINT32_MAX
) {
751 val
= htonl(500 * leasetime
/ 1000);
752 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
754 val
= htonl(875 * leasetime
/ 1000);
755 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
758 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4,
759 &iface
->dhcpv4_mask
.s_addr
);
762 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
763 strlen(a
->hostname
), a
->hostname
);
765 if (iface
->dhcpv4_bcast
.s_addr
!= INADDR_ANY
)
766 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &iface
->dhcpv4_bcast
);
769 if (reqmsg
== DHCPV4_MSG_REQUEST
) {
770 struct dhcpv4_auth_forcerenew auth
= {
774 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
779 memcpy(auth
.key
, a
->key
, sizeof(auth
.key
));
780 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
783 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
,
791 memset(&ifr
, 0, sizeof(ifr
));
792 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
) - 1);
794 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
795 uint16_t mtu
= htons(ifr
.ifr_mtu
);
796 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
799 if (iface
->search
&& iface
->search_len
<= 255)
800 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
801 iface
->search_len
, iface
->search
);
802 else if (!res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
803 uint8_t search_buf
[256];
804 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
805 sizeof(search_buf
), NULL
, NULL
);
807 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
811 if (iface
->dhcpv4_router_cnt
== 0)
812 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &iface
->dhcpv4_local
);
814 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
,
815 4 * iface
->dhcpv4_router_cnt
, iface
->dhcpv4_router
);
818 if (iface
->dhcpv4_dns_cnt
== 0)
819 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &iface
->dhcpv4_local
);
821 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
822 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
825 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
827 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
828 if (req
->giaddr
.s_addr
) {
830 * relay agent is configured, send reply to the agent
832 dest
.sin_addr
= req
->giaddr
;
833 dest
.sin_port
= htons(DHCPV4_SERVER_PORT
);
834 } else if (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= dest
.sin_addr
.s_addr
) {
836 * client has existing configuration (ciaddr is set) AND this address is
837 * not the address it used for the dhcp message
839 dest
.sin_addr
= req
->ciaddr
;
840 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
841 } else if ((ntohs(req
->flags
) & DHCPV4_FLAG_BROADCAST
) ||
842 req
->hlen
!= reply
.hlen
|| !reply
.yiaddr
.s_addr
) {
844 * client requests a broadcast reply OR we can't offer an IP
846 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
847 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
848 } else if (!req
->ciaddr
.s_addr
&& msg
== DHCPV4_MSG_NAK
) {
850 * client has no previous configuration -> no IP, so we need to reply
851 * with a broadcast packet
853 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
854 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
856 struct arpreq arp
= {.arp_flags
= ATF_COM
};
859 * send reply to the newly (in this proccess) allocated IP
861 dest
.sin_addr
= reply
.yiaddr
;
862 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
864 memcpy(arp
.arp_ha
.sa_data
, req
->chaddr
, 6);
865 memcpy(&arp
.arp_pa
, &dest
, sizeof(arp
.arp_pa
));
866 memcpy(arp
.arp_dev
, iface
->ifname
, sizeof(arp
.arp_dev
));
868 if (ioctl(sock
, SIOCSARP
, &arp
) < 0)
869 syslog(LOG_ERR
, "ioctl(SIOCSARP): %m");
872 if (sendto(sock
, &reply
, PACKET_SIZE(&reply
, cookie
), MSG_DONTWAIT
,
873 (struct sockaddr
*)&dest
, sizeof(dest
)) < 0)
874 syslog(LOG_ERR
, "Failed to send %s to %s - %s: %m",
875 dhcpv4_msg_to_string(msg
),
876 dest
.sin_addr
.s_addr
== INADDR_BROADCAST
?
877 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req
->chaddr
, req
->hlen
),
878 inet_ntoa(dest
.sin_addr
));
880 syslog(LOG_NOTICE
, "Sent %s to %s - %s",
881 dhcpv4_msg_to_string(msg
),
882 dest
.sin_addr
.s_addr
== INADDR_BROADCAST
?
883 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req
->chaddr
, req
->hlen
),
884 inet_ntoa(dest
.sin_addr
));
888 if (msg
== DHCPV4_MSG_ACK
)
889 ubus_bcast_dhcp_event("dhcp.ack", req
->chaddr
, req
->hlen
, &reply
.yiaddr
,
890 hostname
, iface
->ifname
);
894 static bool dhcpv4_assign(struct interface
*iface
, struct dhcp_assignment
*a
,
897 struct dhcp_assignment
*c
;
898 uint32_t start
= ntohl(iface
->dhcpv4_start_ip
.s_addr
);
899 uint32_t end
= ntohl(iface
->dhcpv4_end_ip
.s_addr
);
900 uint32_t count
= end
- start
+ 1;
903 /* Preconfigured IP address by static lease */
905 if (list_empty(&iface
->dhcpv4_assignments
)) {
906 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
910 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
911 if (ntohl(c
->addr
) > ntohl(a
->addr
)) {
912 list_add_tail(&a
->head
, &c
->head
);
914 } else if (ntohl(a
->addr
) == ntohl(c
->addr
))
919 /* try to assign the IP the client asked for */
920 if (start
<= ntohl(raddr
) && ntohl(raddr
) <= end
&&
921 !config_find_lease_by_ipaddr(raddr
)) {
922 if (list_empty(&iface
->dhcpv4_assignments
)) {
923 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
927 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
928 if (ntohl(raddr
) == ntohl(c
->addr
))
931 if (ntohl(c
->addr
) > ntohl(raddr
) || list_is_last(&c
->head
, &iface
->dhcpv4_assignments
)) {
932 list_add_tail(&a
->head
,
933 ntohl(c
->addr
) > ntohl(raddr
) ? &c
->head
: &iface
->dhcpv4_assignments
);
937 syslog(LOG_INFO
, "Assigning the IP the client asked for: %u.%u.%u.%u",
938 ((uint8_t *)&a
->addr
)[0], ((uint8_t *)&a
->addr
)[1],
939 ((uint8_t *)&a
->addr
)[2], ((uint8_t *)&a
->addr
)[3]);
946 /* Seed RNG with checksum of hwaddress */
947 for (size_t i
= 0; i
< sizeof(a
->hwaddr
); ++i
) {
948 /* Knuth's multiplicative method */
949 uint8_t o
= a
->hwaddr
[i
];
950 seed
+= (o
*2654435761) % UINT32_MAX
;
955 for (uint32_t i
= 0, try = (((uint32_t)rand()) % count
) + start
; i
< count
;
956 ++i
, try = (((try - start
) + 1) % count
) + start
) {
958 if (config_find_lease_by_ipaddr(htonl(try)))
961 if (list_empty(&iface
->dhcpv4_assignments
)) {
962 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
963 a
->addr
= htonl(try);
965 syslog(LOG_INFO
, "Assigning mapped IP (empty list): %u.%u.%u.%u",
966 ((uint8_t *)&a
->addr
)[0], ((uint8_t *)&a
->addr
)[1],
967 ((uint8_t *)&a
->addr
)[2], ((uint8_t *)&a
->addr
)[3]);
972 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
973 if (try == ntohl(c
->addr
))
976 if (ntohl(c
->addr
) > try || list_is_last(&c
->head
, &iface
->dhcpv4_assignments
)) {
977 list_add_tail(&a
->head
,
978 ntohl(c
->addr
) > try ? &c
->head
: &iface
->dhcpv4_assignments
);
979 a
->addr
= htonl(try);
981 syslog(LOG_INFO
, "Assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
982 ((uint8_t *)&a
->addr
)[0], ((uint8_t *)&a
->addr
)[1],
983 ((uint8_t *)&a
->addr
)[2], ((uint8_t *)&a
->addr
)[3],
991 syslog(LOG_WARNING
, "Can't assign any IP address -> address space is full");
996 static struct dhcp_assignment
*
997 dhcpv4_lease(struct interface
*iface
, enum dhcpv4_msg msg
, const uint8_t *mac
,
998 const uint32_t reqaddr
, uint32_t *leasetime
, const char *hostname
,
999 const size_t hostname_len
, const bool accept_fr_nonce
, bool *incl_fr_opt
,
1000 uint32_t *fr_serverid
)
1002 struct dhcp_assignment
*a
= find_assignment_by_hwaddr(iface
, mac
);
1003 struct lease
*l
= config_find_lease_by_mac(mac
);
1004 time_t now
= odhcpd_time();
1006 if (l
&& a
&& a
->lease
!= l
) {
1011 if (a
&& (a
->flags
& OAF_BOUND
) && a
->fr_ip
) {
1012 *fr_serverid
= a
->fr_ip
->addr
.addr
.in
.s_addr
;
1016 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
1017 bool assigned
= !!a
;
1020 if (!iface
->no_dynamic_dhcp
|| l
) {
1021 /* Create new binding */
1022 a
= calloc(1, sizeof(*a
));
1024 syslog(LOG_ERR
, "Failed to calloc binding on interface %s",
1028 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
1029 /* Set valid time to 0 for static lease indicating */
1030 /* infinite lifetime otherwise current time */
1031 a
->valid_until
= l
? 0 : now
;
1032 a
->dhcp_free_cb
= dhcpv4_free_assignment
;
1034 a
->flags
= OAF_DHCPV4
;
1035 a
->addr
= l
? l
->ipaddr
: INADDR_ANY
;
1037 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1040 a
->flags
|= OAF_STATIC
;
1043 a
->hostname
= strdup(l
->hostname
);
1046 a
->leasetime
= l
->leasetime
;
1048 list_add(&a
->lease_list
, &l
->assignments
);
1052 } else if (((a
->addr
& iface
->dhcpv4_mask
.s_addr
) !=
1053 (iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
)) &&
1054 !(a
->flags
& OAF_STATIC
)) {
1056 a
->addr
= INADDR_ANY
;
1058 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1062 uint32_t my_leasetime
;
1065 my_leasetime
= a
->leasetime
;
1067 my_leasetime
= iface
->dhcpv4_leasetime
;
1069 if ((*leasetime
== 0) || (my_leasetime
< *leasetime
))
1070 *leasetime
= my_leasetime
;
1072 if (msg
== DHCPV4_MSG_DISCOVER
) {
1073 a
->flags
&= ~OAF_BOUND
;
1075 *incl_fr_opt
= accept_fr_nonce
;
1076 if (!(a
->flags
& OAF_STATIC
))
1077 a
->valid_until
= now
;
1079 if ((!(a
->flags
& OAF_STATIC
) || !a
->hostname
) && hostname_len
> 0) {
1080 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1082 memcpy(a
->hostname
, hostname
, hostname_len
);
1083 a
->hostname
[hostname_len
] = 0;
1085 if (odhcpd_valid_hostname(a
->hostname
))
1086 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1088 a
->flags
|= OAF_BROKEN_HOSTNAME
;
1092 if (!(a
->flags
& OAF_BOUND
)) {
1093 a
->accept_fr_nonce
= accept_fr_nonce
;
1094 *incl_fr_opt
= accept_fr_nonce
;
1095 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1096 a
->flags
|= OAF_BOUND
;
1098 *incl_fr_opt
= false;
1100 if (!(a
->flags
& OAF_STATIC
))
1101 a
->valid_until
= ((*leasetime
== UINT32_MAX
) ? 0 : (time_t)(now
+ *leasetime
));
1103 } else if (!assigned
&& a
) {
1104 /* Cleanup failed assignment */
1109 } else if (msg
== DHCPV4_MSG_RELEASE
&& a
) {
1110 a
->flags
&= ~OAF_BOUND
;
1112 if (!(a
->flags
& OAF_STATIC
))
1113 a
->valid_until
= now
- 1;
1115 } else if (msg
== DHCPV4_MSG_DECLINE
&& a
) {
1116 a
->flags
&= ~OAF_BOUND
;
1118 if (!(a
->flags
& OAF_STATIC
)) {
1119 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
1120 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1124 dhcpv6_ia_write_statefile();