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 dhcp_assignment
*a
);
45 static void dhcpv4_fr_start(struct dhcp_assignment
*a
);
46 static void dhcpv4_fr_rand_delay(struct dhcp_assignment
*a
);
47 static void dhcpv4_fr_stop(struct dhcp_assignment
*a
);
48 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
49 struct interface
*iface
, void *dest_addr
);
50 static struct dhcp_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 dhcp_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 dhcp_assignment
*find_assignment_by_hwaddr(struct interface
*iface
, const uint8_t *hwaddr
)
200 struct dhcp_assignment
*a
;
202 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
203 if (!memcmp(a
->hwaddr
, hwaddr
, 6))
209 static struct dhcp_assignment
*find_assignment_by_addr(struct interface
*iface
, const uint32_t addr
)
211 struct dhcp_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 dhcp_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 dhcp_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 struct interface
*iface
;
416 time_t now
= odhcpd_time();
418 avl_for_each_element(&interfaces
, iface
, avl
) {
419 if (iface
->dhcpv4
!= MODE_SERVER
|| iface
->dhcpv4_assignments
.next
== NULL
)
422 struct dhcp_assignment
*a
, *n
;
423 list_for_each_entry_safe(a
, n
, &iface
->dhcpv4_assignments
, head
) {
424 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
)
425 free_dhcpv4_assignment(a
);
428 uloop_timeout_set(event
, 1000);
431 static void handle_addrlist_change(struct interface
*iface
)
433 struct odhcpd_ipaddr ip
;
434 struct odhcpd_ref_ip
*a
;
435 struct dhcp_assignment
*c
;
436 uint32_t mask
= iface
->dhcpv4_mask
.s_addr
;
438 memset(&ip
, 0, sizeof(ip
));
439 ip
.addr
.in
= iface
->dhcpv4_local
;
440 ip
.prefix
= odhcpd_netmask2bitlen(false, &iface
->dhcpv4_mask
);
441 ip
.broadcast
= iface
->dhcpv4_bcast
;
443 setup_dhcpv4_addresses(iface
);
445 if ((ip
.addr
.in
.s_addr
& mask
) ==
446 (iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
))
449 if (ip
.addr
.in
.s_addr
&& !leases_require_fr(iface
, &ip
, mask
))
452 if (iface
->dhcpv4_local
.s_addr
== INADDR_ANY
|| list_empty(&iface
->dhcpv4_fr_ips
))
455 a
= list_first_entry(&iface
->dhcpv4_fr_ips
, struct odhcpd_ref_ip
, head
);
457 if (netlink_setup_addr(&a
->addr
, iface
->ifindex
, false, true)) {
458 syslog(LOG_ERR
, "Failed to add ip address on %s", iface
->name
);
462 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
463 if ((c
->flags
& OAF_BOUND
) && c
->fr_ip
&& !c
->fr_cnt
) {
464 if (c
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
)
465 dhcpv4_fr_rand_delay(c
);
472 static char *dhcpv4_msg_to_string(uint8_t reqmsg
)
475 case (DHCPV4_MSG_DISCOVER
):
476 return "DHCPV4_MSG_DISCOVER";
477 case (DHCPV4_MSG_OFFER
):
478 return "DHCPV4_MSG_OFFER";
479 case (DHCPV4_MSG_REQUEST
):
480 return "DHCPV4_MSG_REQUEST";
481 case (DHCPV4_MSG_DECLINE
):
482 return "DHCPV4_MSG_DECLINE";
483 case (DHCPV4_MSG_ACK
):
484 return "DHCPV4_MSG_ACK";
485 case (DHCPV4_MSG_NAK
):
486 return "DHCPV4_MSG_NAK";
487 case (DHCPV4_MSG_RELEASE
):
488 return "DHCPV4_MSG_RELEASE";
489 case (DHCPV4_MSG_INFORM
):
490 return "DHCPV4_MSG_INFORM";
491 case (DHCPV4_MSG_FORCERENEW
):
492 return "DHCPV4_MSG_FORCERENEW";
498 static void free_dhcpv4_assignment(struct dhcp_assignment
*a
)
510 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
511 uint8_t type
, uint8_t len
, const void *data
)
513 uint8_t *c
= *cookie
;
514 uint8_t *end
= (uint8_t *)msg
+ sizeof(*msg
);
516 if (*cookie
+ 2 + len
> end
)
521 memcpy(c
, data
, len
);
526 static void dhcpv4_fr_send(struct dhcp_assignment
*a
)
528 struct dhcpv4_message fr_msg
= {
529 .op
= DHCPV4_BOOTREPLY
,
535 .ciaddr
= {INADDR_ANY
},
536 .yiaddr
= {INADDR_ANY
},
537 .siaddr
= {INADDR_ANY
},
538 .giaddr
= {INADDR_ANY
},
543 struct dhcpv4_auth_forcerenew
*auth_o
, auth
= {
547 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
551 struct interface
*iface
= a
->iface
;
553 odhcpd_urandom(&fr_msg
.xid
, sizeof(fr_msg
.xid
));
554 memcpy(fr_msg
.chaddr
, a
->hwaddr
, fr_msg
.hlen
);
556 fr_msg
.options
[0] = 0x63;
557 fr_msg
.options
[1] = 0x82;
558 fr_msg
.options
[2] = 0x53;
559 fr_msg
.options
[3] = 0x63;
561 uint8_t *cookie
= &fr_msg
.options
[4];
562 uint8_t msg
= DHCPV4_MSG_FORCERENEW
;
564 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
565 if (a
->accept_fr_nonce
) {
566 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
567 auth_o
= (struct dhcpv4_auth_forcerenew
*)(cookie
- sizeof(auth
));
568 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
571 uint8_t secretbytes
[64];
572 memset(secretbytes
, 0, sizeof(secretbytes
));
573 memcpy(secretbytes
, a
->key
, sizeof(a
->key
));
575 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
576 secretbytes
[i
] ^= 0x36;
579 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
580 md5_hash(&fr_msg
, sizeof(fr_msg
), &md5
);
581 md5_end(auth_o
->key
, &md5
);
583 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
584 secretbytes
[i
] ^= 0x36;
585 secretbytes
[i
] ^= 0x5c;
589 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
590 md5_hash(auth_o
->key
, sizeof(auth_o
->key
), &md5
);
591 md5_end(auth_o
->key
, &md5
);
593 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_SERVERID
, 4,
594 &a
->fr_ip
->addr
.addr
.in
.s_addr
);
595 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
598 struct sockaddr_in dest
;
599 memset(&dest
, 0, sizeof(dest
));
600 dest
.sin_family
= AF_INET
;
601 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
602 dest
.sin_addr
.s_addr
= a
->addr
;
604 if (sendto(iface
->dhcpv4_event
.uloop
.fd
, &fr_msg
, sizeof(fr_msg
),
605 MSG_DONTWAIT
, (struct sockaddr
*)&dest
, sizeof(dest
)) < 0)
606 syslog(LOG_ERR
, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg
),
607 odhcpd_print_mac(a
->hwaddr
, sizeof(a
->hwaddr
)), inet_ntoa(dest
.sin_addr
));
609 syslog(LOG_WARNING
, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg
),
610 odhcpd_print_mac(a
->hwaddr
, sizeof(a
->hwaddr
)), inet_ntoa(dest
.sin_addr
));
613 static void dhcpv4_fr_timer(struct uloop_timeout
*event
)
615 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
617 if (a
->fr_cnt
> 0 && a
->fr_cnt
< 8) {
619 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
625 static void dhcpv4_fr_start(struct dhcp_assignment
*a
)
627 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
628 a
->fr_timer
.cb
= dhcpv4_fr_timer
;
634 static void dhcpv4_fr_delay_timer(struct uloop_timeout
*event
)
636 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
637 struct interface
*iface
= a
->iface
;
639 (iface
->dhcpv4_event
.uloop
.fd
== -1 ? dhcpv4_fr_rand_delay(a
) : dhcpv4_fr_start(a
));
642 static void dhcpv4_fr_rand_delay(struct dhcp_assignment
*a
)
644 #define MIN_DELAY 500
648 odhcpd_urandom(&msecs
, sizeof(msecs
));
650 msecs
= labs(msecs
)%MAX_FUZZ
+ MIN_DELAY
;
652 uloop_timeout_set(&a
->fr_timer
, msecs
);
653 a
->fr_timer
.cb
= dhcpv4_fr_delay_timer
;
656 static void dhcpv4_fr_stop(struct dhcp_assignment
*a
)
658 uloop_timeout_cancel(&a
->fr_timer
);
659 decr_ref_cnt_ip(&a
->fr_ip
, a
->iface
);
661 a
->fr_timer
.cb
= NULL
;
664 /* Handler for DHCPv4 messages */
665 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
666 struct interface
*iface
, _unused
void *dest_addr
)
671 struct dhcpv4_message
*req
= data
;
672 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
673 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
677 syslog(LOG_NOTICE
, "Got DHCPv4 request on %s", iface
->name
);
679 if (!iface
->dhcpv4_start_ip
.s_addr
&& !iface
->dhcpv4_end_ip
.s_addr
) {
680 syslog(LOG_WARNING
, "No DHCP range available on %s", iface
->name
);
684 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
686 struct dhcpv4_message reply
= {
687 .op
= DHCPV4_BOOTREPLY
,
694 .ciaddr
= {INADDR_ANY
},
695 .giaddr
= req
->giaddr
,
696 .siaddr
= iface
->dhcpv4_local
,
698 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
700 reply
.options
[0] = 0x63;
701 reply
.options
[1] = 0x82;
702 reply
.options
[2] = 0x53;
703 reply
.options
[3] = 0x63;
705 uint8_t *cookie
= &reply
.options
[4];
706 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
707 uint8_t msg
= DHCPV4_MSG_ACK
;
709 uint32_t reqaddr
= INADDR_ANY
;
710 uint32_t leasetime
= 0;
711 size_t hostname_len
= 0;
713 bool accept_fr_nonce
= false;
714 bool incl_fr_opt
= false;
716 uint8_t *start
= &req
->options
[4];
717 uint8_t *end
= ((uint8_t*)data
) + len
;
718 struct dhcpv4_option
*opt
;
719 dhcpv4_for_each_option(start
, end
, opt
) {
720 if (opt
->type
== DHCPV4_OPT_MESSAGE
&& opt
->len
== 1)
721 reqmsg
= opt
->data
[0];
722 else if (opt
->type
== DHCPV4_OPT_HOSTNAME
&& opt
->len
> 0) {
723 hostname_len
= opt
->len
;
724 memcpy(hostname
, opt
->data
, hostname_len
);
725 hostname
[hostname_len
] = 0;
726 } else if (opt
->type
== DHCPV4_OPT_IPADDRESS
&& opt
->len
== 4)
727 memcpy(&reqaddr
, opt
->data
, 4);
728 else if (opt
->type
== DHCPV4_OPT_SERVERID
&& opt
->len
== 4) {
729 if (memcmp(opt
->data
, &iface
->dhcpv4_local
, 4))
731 } else if (iface
->filter_class
&& opt
->type
== DHCPV4_OPT_USER_CLASS
) {
732 uint8_t *c
= opt
->data
, *cend
= &opt
->data
[opt
->len
];
733 for (; c
< cend
&& &c
[*c
] < cend
; c
= &c
[1 + *c
]) {
734 size_t elen
= strlen(iface
->filter_class
);
735 if (*c
== elen
&& !memcmp(&c
[1], iface
->filter_class
, elen
))
736 return; // Ignore from homenet
738 } else if (opt
->type
== DHCPV4_OPT_LEASETIME
&& opt
->len
== 4)
739 memcpy(&leasetime
, opt
->data
, 4);
740 else if (opt
->type
== DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
&& opt
->len
> 0) {
741 for (uint8_t i
= 0; i
< opt
->len
; i
++) {
742 if (opt
->data
[i
] == 1) {
743 accept_fr_nonce
= true;
751 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
752 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
753 reqmsg
!= DHCPV4_MSG_RELEASE
)
756 struct dhcp_assignment
*lease
= NULL
;
757 uint32_t serverid
= iface
->dhcpv4_local
.s_addr
;
758 uint32_t fr_serverid
= INADDR_ANY
;
760 if (reqmsg
!= DHCPV4_MSG_INFORM
)
761 lease
= dhcpv4_lease(iface
, reqmsg
, req
->chaddr
, reqaddr
,
762 &leasetime
, hostname
, hostname_len
,
763 accept_fr_nonce
, &incl_fr_opt
, &fr_serverid
);
766 if (reqmsg
== DHCPV4_MSG_REQUEST
)
767 msg
= DHCPV4_MSG_NAK
;
768 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
770 } else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
771 msg
= DHCPV4_MSG_OFFER
;
772 else if (reqmsg
== DHCPV4_MSG_REQUEST
&&
773 ((reqaddr
&& reqaddr
!= lease
->addr
) ||
774 (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= lease
->addr
))) {
775 msg
= DHCPV4_MSG_NAK
;
777 * DHCP client requested an IP which we can't offer to him. Probably the
778 * client changed the network or the network has been changed. The reply
779 * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
781 * For modern devices we build an answer that includes a valid IP, like
782 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
783 * perform additional DHCP round trips.
789 * Buggy clients do serverid checking in nack messages; therefore set the
790 * serverid in nack messages triggered by a previous force renew equal to
791 * the server id in use at that time by the server
795 serverid
= fr_serverid
;
797 if (req
->ciaddr
.s_addr
&&
798 ((iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
) !=
799 (req
->ciaddr
.s_addr
& iface
->dhcpv4_mask
.s_addr
)))
800 req
->ciaddr
.s_addr
= INADDR_ANY
;
803 syslog(LOG_WARNING
, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg
),
804 odhcpd_print_mac(req
->chaddr
, req
->hlen
), iface
->name
);
807 if (reqmsg
== DHCPV4_MSG_RELEASE
)
808 ubus_bcast_dhcp_event("dhcp.release", req
->chaddr
, req
->hlen
,
809 &req
->ciaddr
, hostname
, iface
->ifname
);
811 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
814 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
815 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &serverid
);
820 reply
.yiaddr
.s_addr
= lease
->addr
;
822 val
= htonl(leasetime
);
823 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
825 if (leasetime
!= UINT32_MAX
) {
826 val
= htonl(500 * leasetime
/ 1000);
827 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
829 val
= htonl(875 * leasetime
/ 1000);
830 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
833 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4,
834 &iface
->dhcpv4_mask
.s_addr
);
837 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
838 strlen(lease
->hostname
), lease
->hostname
);
840 if (iface
->dhcpv4_bcast
.s_addr
!= INADDR_ANY
)
841 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &iface
->dhcpv4_bcast
);
844 if (reqmsg
== DHCPV4_MSG_REQUEST
) {
845 struct dhcpv4_auth_forcerenew auth
= {
849 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
854 memcpy(auth
.key
, lease
->key
, sizeof(auth
.key
));
855 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
858 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
,
866 memset(&ifr
, 0, sizeof(ifr
));
867 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
) - 1);
869 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
870 uint16_t mtu
= htons(ifr
.ifr_mtu
);
871 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
874 if (iface
->search
&& iface
->search_len
<= 255)
875 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
876 iface
->search_len
, iface
->search
);
877 else if (!res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
878 uint8_t search_buf
[256];
879 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
880 sizeof(search_buf
), NULL
, NULL
);
882 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
886 if (iface
->dhcpv4_router_cnt
== 0)
887 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &iface
->dhcpv4_local
);
889 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
,
890 4 * iface
->dhcpv4_router_cnt
, iface
->dhcpv4_router
);
893 if (iface
->dhcpv4_dns_cnt
== 0)
894 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &iface
->dhcpv4_local
);
896 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
897 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
900 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
902 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
903 if (req
->giaddr
.s_addr
) {
905 * relay agent is configured, send reply to the agent
907 dest
.sin_addr
= req
->giaddr
;
908 dest
.sin_port
= htons(DHCPV4_SERVER_PORT
);
909 } else if (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= dest
.sin_addr
.s_addr
) {
911 * client has existing configuration (ciaddr is set) AND this address is
912 * not the address it used for the dhcp message
914 dest
.sin_addr
= req
->ciaddr
;
915 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
916 } else if ((ntohs(req
->flags
) & DHCPV4_FLAG_BROADCAST
) ||
917 req
->hlen
!= reply
.hlen
|| !reply
.yiaddr
.s_addr
) {
919 * client requests a broadcast reply OR we can't offer an IP
921 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
922 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
923 } else if (!req
->ciaddr
.s_addr
&& msg
== DHCPV4_MSG_NAK
) {
925 * client has no previous configuration -> no IP, so we need to reply
926 * with a broadcast packet
928 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
929 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
931 struct arpreq arp
= {.arp_flags
= ATF_COM
};
934 * send reply to the newly (in this proccess) allocated IP
936 dest
.sin_addr
= reply
.yiaddr
;
937 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
939 memcpy(arp
.arp_ha
.sa_data
, req
->chaddr
, 6);
940 memcpy(&arp
.arp_pa
, &dest
, sizeof(arp
.arp_pa
));
941 memcpy(arp
.arp_dev
, iface
->ifname
, sizeof(arp
.arp_dev
));
943 if (ioctl(sock
, SIOCSARP
, &arp
) < 0)
944 syslog(LOG_ERR
, "ioctl(SIOCSARP): %m");
947 if (sendto(sock
, &reply
, sizeof(reply
), MSG_DONTWAIT
,
948 (struct sockaddr
*)&dest
, sizeof(dest
)) < 0)
949 syslog(LOG_ERR
, "Failed to send %s to %s - %s: %m",
950 dhcpv4_msg_to_string(msg
),
951 dest
.sin_addr
.s_addr
== INADDR_BROADCAST
?
952 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req
->chaddr
, req
->hlen
),
953 inet_ntoa(dest
.sin_addr
));
955 syslog(LOG_ERR
, "Sent %s to %s - %s",
956 dhcpv4_msg_to_string(msg
),
957 dest
.sin_addr
.s_addr
== INADDR_BROADCAST
?
958 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req
->chaddr
, req
->hlen
),
959 inet_ntoa(dest
.sin_addr
));
963 if (msg
== DHCPV4_MSG_ACK
)
964 ubus_bcast_dhcp_event("dhcp.ack", req
->chaddr
, req
->hlen
, &reply
.yiaddr
,
965 hostname
, iface
->ifname
);
969 static bool dhcpv4_assign(struct interface
*iface
,
970 struct dhcp_assignment
*assign
, uint32_t raddr
)
972 uint32_t start
= ntohl(iface
->dhcpv4_start_ip
.s_addr
);
973 uint32_t end
= ntohl(iface
->dhcpv4_end_ip
.s_addr
);
974 uint32_t count
= end
- start
+ 1;
976 /* try to assign the IP the client asked for */
977 if (start
<= ntohl(raddr
) && ntohl(raddr
) <= end
&&
978 !find_assignment_by_addr(iface
, raddr
)) {
979 assign
->addr
= raddr
;
980 syslog(LOG_INFO
, "Assigning the IP the client asked for: %u.%u.%u.%u",
981 ((uint8_t *)&assign
->addr
)[0],
982 ((uint8_t *)&assign
->addr
)[1],
983 ((uint8_t *)&assign
->addr
)[2],
984 ((uint8_t *)&assign
->addr
)[3]);
988 /* Seed RNG with checksum of hwaddress */
990 for (size_t i
= 0; i
< sizeof(assign
->hwaddr
); ++i
) {
991 /* Knuth's multiplicative method */
992 uint8_t o
= assign
->hwaddr
[i
];
993 seed
+= (o
*2654435761) % UINT32_MAX
;
998 uint32_t try = (((uint32_t)rand()) % count
) + start
;
1000 if (list_empty(&iface
->dhcpv4_assignments
)) {
1001 assign
->addr
= htonl(try);
1002 syslog(LOG_INFO
, "Assigning mapped IP (empty list): %u.%u.%u.%u",
1003 ((uint8_t *)&assign
->addr
)[0],
1004 ((uint8_t *)&assign
->addr
)[1],
1005 ((uint8_t *)&assign
->addr
)[2],
1006 ((uint8_t *)&assign
->addr
)[3]);
1010 for (uint32_t i
= 0; i
< count
; ++i
) {
1011 if (!find_assignment_by_addr(iface
, htonl(try))) {
1012 /* test was successful: IP address is not assigned, assign it */
1013 assign
->addr
= htonl(try);
1014 syslog(LOG_DEBUG
, "Assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
1015 ((uint8_t *)&assign
->addr
)[0],
1016 ((uint8_t *)&assign
->addr
)[1],
1017 ((uint8_t *)&assign
->addr
)[2],
1018 ((uint8_t *)&assign
->addr
)[3],
1022 try = (((try - start
) + 1) % count
) + start
;
1025 syslog(LOG_WARNING
, "Can't assign any IP address -> address space is full");
1030 static struct dhcp_assignment
* dhcpv4_lease(struct interface
*iface
,
1031 enum dhcpv4_msg msg
, const uint8_t *mac
, const uint32_t reqaddr
,
1032 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
,
1033 const bool accept_fr_nonce
, bool *incl_fr_opt
, uint32_t *fr_serverid
)
1035 struct dhcp_assignment
*a
= find_assignment_by_hwaddr(iface
, mac
);
1036 struct dhcp_assignment
*lease
= NULL
;
1037 time_t now
= odhcpd_time();
1039 if (a
&& (a
->flags
& OAF_BOUND
) && a
->fr_ip
) {
1040 *fr_serverid
= a
->fr_ip
->addr
.addr
.in
.s_addr
;
1044 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
1045 bool assigned
= !!a
;
1048 if (!iface
->no_dynamic_dhcp
) {
1049 /* Create new binding */
1050 a
= calloc(1, sizeof(*a
));
1052 syslog(LOG_ERR
, "Failed to calloc binding on interface %s",
1056 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
1057 /* Don't consider new assignment as infinite */
1058 a
->valid_until
= now
;
1060 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1063 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
1066 } else if ((a
->addr
& iface
->dhcpv4_mask
.s_addr
) !=
1067 (iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
)) {
1070 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1072 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
1076 uint32_t my_leasetime
;
1079 my_leasetime
= a
->leasetime
;
1081 my_leasetime
= iface
->dhcpv4_leasetime
;
1083 if ((*leasetime
== 0) || (my_leasetime
< *leasetime
))
1084 *leasetime
= my_leasetime
;
1086 if (msg
== DHCPV4_MSG_DISCOVER
) {
1087 a
->flags
&= ~OAF_BOUND
;
1089 *incl_fr_opt
= accept_fr_nonce
;
1090 if (!(a
->flags
& OAF_STATIC
))
1091 a
->valid_until
= now
;
1093 if (hostname_len
> 0) {
1094 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1096 memcpy(a
->hostname
, hostname
, hostname_len
);
1097 a
->hostname
[hostname_len
] = 0;
1099 if (odhcpd_valid_hostname(a
->hostname
))
1100 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1102 a
->flags
|= OAF_BROKEN_HOSTNAME
;
1106 if (!(a
->flags
& OAF_BOUND
)) {
1107 a
->accept_fr_nonce
= accept_fr_nonce
;
1108 *incl_fr_opt
= accept_fr_nonce
;
1109 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1110 a
->flags
|= OAF_BOUND
;
1112 *incl_fr_opt
= false;
1114 if (!(a
->flags
& OAF_STATIC
))
1115 a
->valid_until
= ((*leasetime
== UINT32_MAX
) ? 0 : (time_t)(now
+ *leasetime
));
1117 } else if (!assigned
&& a
) {
1118 /* Cleanup failed assignment */
1119 free_dhcpv4_assignment(a
);
1125 } else if (msg
== DHCPV4_MSG_RELEASE
&& a
) {
1126 a
->flags
&= ~OAF_BOUND
;
1128 if (!(a
->flags
& OAF_STATIC
))
1129 a
->valid_until
= now
- 1;
1131 } else if (msg
== DHCPV4_MSG_DECLINE
&& a
) {
1132 a
->flags
&= ~OAF_BOUND
;
1134 if (!(a
->flags
& OAF_STATIC
)) {
1135 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
1136 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1140 dhcpv6_write_statefile();