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 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
);
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;
59 struct odhcpd_ref_ip
{
60 struct list_head head
;
62 struct odhcpd_ipaddr addr
;
65 /* Create socket and register events */
68 uloop_timeout_set(&valid_until_timeout
, 1000);
69 netlink_add_netevent_handler(&dhcpv4_netevent_handler
);
74 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
)
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;
84 if (iface
->dhcpv4
&& enable
) {
85 struct sockaddr_in bind_addr
= {AF_INET
, htons(DHCPV4_SERVER_PORT
),
89 if (!iface
->dhcpv4_assignments
.next
)
90 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
92 if (!iface
->dhcpv4_fr_ips
.next
)
93 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
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");
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");
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");
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");
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");
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");
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");
147 if (bind(iface
->dhcpv4_event
.uloop
.fd
, (struct sockaddr
*)&bind_addr
,
148 sizeof(bind_addr
)) < 0) {
149 syslog(LOG_ERR
, "bind(): %m");
154 if (setup_dhcpv4_addresses(iface
) < 0) {
159 iface
->dhcpv4_event
.handle_dgram
= handle_dhcpv4
;
160 odhcpd_register(&iface
->dhcpv4_event
);
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
));
170 if (ret
< 0 && iface
->dhcpv4_event
.uloop
.fd
> 0) {
171 close(iface
->dhcpv4_event
.uloop
.fd
);
172 iface
->dhcpv4_event
.uloop
.fd
= -1;
179 static void dhcpv4_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
181 struct interface
*iface
= info
->iface
;
183 if (!iface
|| iface
->dhcpv4
== MODE_DISABLED
)
187 case NETEV_IFINDEX_CHANGE
:
188 dhcpv4_setup_interface(iface
, true);
190 case NETEV_ADDRLIST_CHANGE
:
191 handle_addrlist_change(iface
);
198 static struct dhcpv4_assignment
*find_assignment_by_hwaddr(struct interface
*iface
, const uint8_t *hwaddr
)
200 struct dhcpv4_assignment
*a
;
202 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
203 if (!memcmp(a
->hwaddr
, hwaddr
, 6))
209 static struct dhcpv4_assignment
*find_assignment_by_addr(struct interface
*iface
, const uint32_t addr
)
211 struct dhcpv4_assignment
*a
;
213 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
220 static int setup_dhcpv4_addresses(struct interface
*iface
)
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
;
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
);
236 if (!iface
->addr4_len
) {
237 syslog(LOG_WARNING
, "No network(s) available on %s", iface
->name
);
241 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
242 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
244 for (size_t i
= 0; i
< iface
->addr4_len
&& start
&& end
; i
++) {
245 struct in_addr
*addr
= &iface
->addr4
[i
].addr
.in
;
248 if (addr_is_fr_ip(iface
, addr
))
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
;
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
);
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
;
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);
287 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(3);
288 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(12);
294 static void update_static_assignments(struct interface
*iface
)
296 struct dhcpv4_assignment
*a
, *c
;
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
);
306 /* Parse static entries */
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
)) {
314 a
= find_assignment_by_hwaddr(iface
, lease
->mac
.ether_addr_octet
);
317 /* Check if there's an assignment with the specified IP address */
318 if (find_assignment_by_addr(iface
, lease
->ipaddr
.s_addr
))
321 /* Construct entry */
322 a
= calloc(1, sizeof(*a
));
324 syslog(LOG_ERR
, "Calloc failed for static lease on %s", iface
->name
);
327 memcpy(a
->hwaddr
, lease
->mac
.ether_addr_octet
, sizeof(a
->hwaddr
));
330 a
->leasetime
= lease
->dhcpv4_leasetime
;
332 a
->addr
= lease
->ipaddr
.s_addr
;
333 /* Static assignment */
334 a
->flags
|= OAF_STATIC
;
338 if (lease
->hostname
[0]) {
340 a
->hostname
= strdup(lease
->hostname
);
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
);
351 if (&c
->head
== &iface
->dhcpv4_assignments
)
352 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
356 static void inc_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct odhcpd_ref_ip
*ip
)
362 static void decr_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct interface
*iface
)
364 struct odhcpd_ref_ip
*ip
= *ptr
;
366 if (--ip
->ref_cnt
== 0) {
367 netlink_setup_addr(&ip
->addr
, iface
->ifindex
, false, false);
376 static bool addr_is_fr_ip(struct interface
*iface
, struct in_addr
*addr
)
378 struct odhcpd_ref_ip
*p
;
380 list_for_each_entry(p
, &iface
->dhcpv4_fr_ips
, head
) {
381 if (addr
->s_addr
== p
->addr
.addr
.in
.s_addr
)
388 static bool leases_require_fr(struct interface
*iface
, struct odhcpd_ipaddr
*addr
,
391 struct dhcpv4_assignment
*a
= NULL
;
392 struct odhcpd_ref_ip
*fr_ip
= NULL
;
394 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
) {
395 if ((a
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
) &&
397 ((a
->addr
& mask
) == (addr
->addr
.in
.s_addr
& mask
))) {
399 fr_ip
= calloc(1, sizeof(*fr_ip
));
403 list_add(&fr_ip
->head
, &iface
->dhcpv4_fr_ips
);
406 inc_ref_cnt_ip(&a
->fr_ip
, fr_ip
);
410 return fr_ip
? true : false;
413 static void valid_until_cb(struct uloop_timeout
*event
)
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
)
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
);
427 uloop_timeout_set(event
, 1000);
430 static void handle_addrlist_change(struct interface
*iface
)
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
;
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
;
442 setup_dhcpv4_addresses(iface
);
444 if ((ip
.addr
.in
.s_addr
& mask
) ==
445 (iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
))
448 if (ip
.addr
.in
.s_addr
&& !leases_require_fr(iface
, &ip
, mask
))
451 if (iface
->dhcpv4_local
.s_addr
== INADDR_ANY
|| list_empty(&iface
->dhcpv4_fr_ips
))
454 a
= list_first_entry(&iface
->dhcpv4_fr_ips
, struct odhcpd_ref_ip
, head
);
456 if (netlink_setup_addr(&a
->addr
, iface
->ifindex
, false, true)) {
457 syslog(LOG_ERR
, "Failed to add ip address on %s", iface
->name
);
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
);
471 static char *dhcpv4_msg_to_string(uint8_t 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";
497 static void free_dhcpv4_assignment(struct dhcpv4_assignment
*a
)
509 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
510 uint8_t type
, uint8_t len
, const void *data
)
512 uint8_t *c
= *cookie
;
513 uint8_t *end
= (uint8_t *)msg
+ sizeof(*msg
);
515 if (*cookie
+ 2 + len
> end
)
520 memcpy(c
, data
, len
);
525 static void dhcpv4_fr_send(struct dhcpv4_assignment
*a
)
527 struct dhcpv4_message fr_msg
= {
528 .op
= DHCPV4_BOOTREPLY
,
534 .ciaddr
= {INADDR_ANY
},
535 .yiaddr
= {INADDR_ANY
},
536 .siaddr
= {INADDR_ANY
},
537 .giaddr
= {INADDR_ANY
},
542 struct dhcpv4_auth_forcerenew
*auth_o
, auth
= {
546 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
550 struct interface
*iface
= a
->iface
;
552 odhcpd_urandom(&fr_msg
.xid
, sizeof(fr_msg
.xid
));
553 memcpy(fr_msg
.chaddr
, a
->hwaddr
, fr_msg
.hlen
);
555 fr_msg
.options
[0] = 0x63;
556 fr_msg
.options
[1] = 0x82;
557 fr_msg
.options
[2] = 0x53;
558 fr_msg
.options
[3] = 0x63;
560 uint8_t *cookie
= &fr_msg
.options
[4];
561 uint8_t msg
= DHCPV4_MSG_FORCERENEW
;
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
);
570 uint8_t secretbytes
[64];
571 memset(secretbytes
, 0, sizeof(secretbytes
));
572 memcpy(secretbytes
, a
->key
, sizeof(a
->key
));
574 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
575 secretbytes
[i
] ^= 0x36;
578 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
579 md5_hash(&fr_msg
, sizeof(fr_msg
), &md5
);
580 md5_end(auth_o
->key
, &md5
);
582 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
583 secretbytes
[i
] ^= 0x36;
584 secretbytes
[i
] ^= 0x5c;
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
);
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
);
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
;
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
));
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
));
612 static void dhcpv4_fr_timer(struct uloop_timeout
*event
)
614 struct dhcpv4_assignment
*a
= container_of(event
, struct dhcpv4_assignment
, fr_timer
);
616 if (a
->fr_cnt
> 0 && a
->fr_cnt
< 8) {
618 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
624 static void dhcpv4_fr_start(struct dhcpv4_assignment
*a
)
626 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
627 a
->fr_timer
.cb
= dhcpv4_fr_timer
;
633 static void dhcpv4_fr_delay_timer(struct uloop_timeout
*event
)
635 struct dhcpv4_assignment
*a
= container_of(event
, struct dhcpv4_assignment
, fr_timer
);
636 struct interface
*iface
= a
->iface
;
638 (iface
->dhcpv4_event
.uloop
.fd
== -1 ? dhcpv4_fr_rand_delay(a
) : dhcpv4_fr_start(a
));
641 static void dhcpv4_fr_rand_delay(struct dhcpv4_assignment
*a
)
643 #define MIN_DELAY 500
647 odhcpd_urandom(&msecs
, sizeof(msecs
));
649 msecs
= labs(msecs
)%MAX_FUZZ
+ MIN_DELAY
;
651 uloop_timeout_set(&a
->fr_timer
, msecs
);
652 a
->fr_timer
.cb
= dhcpv4_fr_delay_timer
;
655 static void dhcpv4_fr_stop(struct dhcpv4_assignment
*a
)
657 uloop_timeout_cancel(&a
->fr_timer
);
658 decr_ref_cnt_ip(&a
->fr_ip
, a
->iface
);
660 a
->fr_timer
.cb
= NULL
;
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
)
670 struct dhcpv4_message
*req
= data
;
671 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
672 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
676 syslog(LOG_NOTICE
, "Got DHCPv4 request on %s", iface
->name
);
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
);
683 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
685 struct dhcpv4_message reply
= {
686 .op
= DHCPV4_BOOTREPLY
,
693 .ciaddr
= {INADDR_ANY
},
694 .giaddr
= req
->giaddr
,
695 .siaddr
= iface
->dhcpv4_local
,
697 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
699 reply
.options
[0] = 0x63;
700 reply
.options
[1] = 0x82;
701 reply
.options
[2] = 0x53;
702 reply
.options
[3] = 0x63;
704 uint8_t *cookie
= &reply
.options
[4];
705 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
706 uint8_t msg
= DHCPV4_MSG_ACK
;
708 uint32_t reqaddr
= INADDR_ANY
;
709 uint32_t leasetime
= 0;
710 size_t hostname_len
= 0;
712 bool accept_fr_nonce
= false;
713 bool incl_fr_opt
= false;
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))
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
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;
750 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
751 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
752 reqmsg
!= DHCPV4_MSG_RELEASE
)
755 struct dhcpv4_assignment
*lease
= NULL
;
756 uint32_t serverid
= iface
->dhcpv4_local
.s_addr
;
757 uint32_t fr_serverid
= INADDR_ANY
;
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
);
765 if (reqmsg
== DHCPV4_MSG_REQUEST
)
766 msg
= DHCPV4_MSG_NAK
;
767 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
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
;
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.
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.
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
794 serverid
= fr_serverid
;
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
;
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
);
806 if (reqmsg
== DHCPV4_MSG_RELEASE
)
807 ubus_bcast_dhcp_event("dhcp.release", req
->chaddr
, req
->hlen
,
808 &req
->ciaddr
, hostname
, iface
->ifname
);
810 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
813 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
814 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &serverid
);
819 reply
.yiaddr
.s_addr
= lease
->addr
;
821 val
= htonl(leasetime
);
822 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
824 if (leasetime
!= UINT32_MAX
) {
825 val
= htonl(500 * leasetime
/ 1000);
826 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
828 val
= htonl(875 * leasetime
/ 1000);
829 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
832 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4,
833 &iface
->dhcpv4_mask
.s_addr
);
836 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
837 strlen(lease
->hostname
), lease
->hostname
);
839 if (iface
->dhcpv4_bcast
.s_addr
!= INADDR_ANY
)
840 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &iface
->dhcpv4_bcast
);
843 if (reqmsg
== DHCPV4_MSG_REQUEST
) {
844 struct dhcpv4_auth_forcerenew auth
= {
848 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
853 memcpy(auth
.key
, lease
->key
, sizeof(auth
.key
));
854 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
857 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
,
865 memset(&ifr
, 0, sizeof(ifr
));
866 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
) - 1);
868 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
869 uint16_t mtu
= htons(ifr
.ifr_mtu
);
870 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
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
);
881 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
885 if (iface
->dhcpv4_router_cnt
== 0)
886 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &iface
->dhcpv4_local
);
888 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
,
889 4 * iface
->dhcpv4_router_cnt
, iface
->dhcpv4_router
);
892 if (iface
->dhcpv4_dns_cnt
== 0)
893 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &iface
->dhcpv4_local
);
895 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
896 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
899 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
901 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
902 if (req
->giaddr
.s_addr
) {
904 * relay agent is configured, send reply to the agent
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
) {
910 * client has existing configuration (ciaddr is set) AND this address is
911 * not the address it used for the dhcp message
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
) {
918 * client requests a broadcast reply OR we can't offer an IP
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
) {
924 * client has no previous configuration -> no IP, so we need to reply
925 * with a broadcast packet
927 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
928 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
930 struct arpreq arp
= {.arp_flags
= ATF_COM
};
933 * send reply to the newly (in this proccess) allocated IP
935 dest
.sin_addr
= reply
.yiaddr
;
936 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
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
));
942 if (ioctl(sock
, SIOCSARP
, &arp
) < 0)
943 syslog(LOG_ERR
, "ioctl(SIOCSARP): %m");
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
));
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
));
962 if (msg
== DHCPV4_MSG_ACK
)
963 ubus_bcast_dhcp_event("dhcp.ack", req
->chaddr
, req
->hlen
, &reply
.yiaddr
,
964 hostname
, iface
->ifname
);
968 static bool dhcpv4_assign(struct interface
*iface
,
969 struct dhcpv4_assignment
*assign
, uint32_t raddr
)
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;
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]);
987 /* Seed RNG with checksum of hwaddress */
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
;
997 uint32_t try = (((uint32_t)rand()) % count
) + start
;
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]);
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],
1021 try = (((try - start
) + 1) % count
) + start
;
1024 syslog(LOG_WARNING
, "Can't assign any IP address -> address space is full");
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
)
1034 struct dhcpv4_assignment
*a
= find_assignment_by_hwaddr(iface
, mac
);
1035 struct dhcpv4_assignment
*lease
= NULL
;
1036 time_t now
= odhcpd_time();
1038 if (a
&& (a
->flags
& OAF_BOUND
) && a
->fr_ip
) {
1039 *fr_serverid
= a
->fr_ip
->addr
.addr
.in
.s_addr
;
1043 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
1044 bool assigned
= !!a
;
1047 if (!iface
->no_dynamic_dhcp
) {
1048 /* Create new binding */
1049 a
= calloc(1, sizeof(*a
));
1051 syslog(LOG_ERR
, "Failed to calloc binding on interface %s",
1055 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
1056 /* Don't consider new assignment as infinite */
1057 a
->valid_until
= now
;
1059 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1062 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
1065 } else if ((a
->addr
& iface
->dhcpv4_mask
.s_addr
) !=
1066 (iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
)) {
1069 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1071 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
1075 uint32_t my_leasetime
;
1078 my_leasetime
= a
->leasetime
;
1080 my_leasetime
= iface
->dhcpv4_leasetime
;
1082 if ((*leasetime
== 0) || (my_leasetime
< *leasetime
))
1083 *leasetime
= my_leasetime
;
1085 if (msg
== DHCPV4_MSG_DISCOVER
) {
1086 a
->flags
&= ~OAF_BOUND
;
1088 *incl_fr_opt
= accept_fr_nonce
;
1089 if (!(a
->flags
& OAF_STATIC
))
1090 a
->valid_until
= now
;
1092 if (hostname_len
> 0) {
1093 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1095 memcpy(a
->hostname
, hostname
, hostname_len
);
1096 a
->hostname
[hostname_len
] = 0;
1098 if (odhcpd_valid_hostname(a
->hostname
))
1099 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1101 a
->flags
|= OAF_BROKEN_HOSTNAME
;
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
;
1111 *incl_fr_opt
= false;
1113 if (!(a
->flags
& OAF_STATIC
))
1114 a
->valid_until
= ((*leasetime
== UINT32_MAX
) ? 0 : (time_t)(now
+ *leasetime
));
1116 } else if (!assigned
&& a
) {
1117 /* Cleanup failed assignment */
1118 free_dhcpv4_assignment(a
);
1124 } else if (msg
== DHCPV4_MSG_RELEASE
&& a
) {
1125 a
->flags
&= ~OAF_BOUND
;
1127 if (!(a
->flags
& OAF_STATIC
))
1128 a
->valid_until
= now
- 1;
1130 } else if (msg
== DHCPV4_MSG_DECLINE
&& a
) {
1131 a
->flags
&= ~OAF_BOUND
;
1133 if (!(a
->flags
& OAF_STATIC
)) {
1134 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
1135 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1139 dhcpv6_write_statefile();