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 bool addr_is_fr_ip(struct interface
*iface
, struct in_addr
*addr
);
41 static void valid_until_cb(struct uloop_timeout
*event
);
42 static void handle_addrlist_change(struct interface
*iface
);
43 static void dhcpv4_fr_start(struct dhcp_assignment
*a
);
44 static void dhcpv4_fr_rand_delay(struct dhcp_assignment
*a
);
45 static void dhcpv4_fr_stop(struct dhcp_assignment
*a
);
46 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
47 struct interface
*iface
, void *dest_addr
);
48 static struct dhcp_assignment
* dhcpv4_lease(struct interface
*iface
,
49 enum dhcpv4_msg msg
, const uint8_t *mac
, const uint32_t reqaddr
,
50 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
,
51 const bool accept_fr_nonce
, bool *incl_fr_opt
, uint32_t *fr_serverid
);
53 static struct netevent_handler dhcpv4_netevent_handler
= { .cb
= dhcpv4_netevent_cb
, };
54 static struct uloop_timeout valid_until_timeout
= {.cb
= valid_until_cb
};
55 static uint32_t serial
= 0;
57 struct odhcpd_ref_ip
{
58 struct list_head head
;
60 struct odhcpd_ipaddr addr
;
63 /* Create socket and register events */
66 uloop_timeout_set(&valid_until_timeout
, 1000);
67 netlink_add_netevent_handler(&dhcpv4_netevent_handler
);
72 int dhcpv4_setup_interface(struct interface
*iface
, bool enable
)
76 if (iface
->dhcpv4_event
.uloop
.fd
> 0) {
77 uloop_fd_delete(&iface
->dhcpv4_event
.uloop
);
78 close(iface
->dhcpv4_event
.uloop
.fd
);
79 iface
->dhcpv4_event
.uloop
.fd
= -1;
82 if (iface
->dhcpv4
&& enable
) {
83 struct sockaddr_in bind_addr
= {AF_INET
, htons(DHCPV4_SERVER_PORT
),
87 if (!iface
->dhcpv4_assignments
.next
)
88 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
90 if (!iface
->dhcpv4_fr_ips
.next
)
91 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
93 iface
->dhcpv4_event
.uloop
.fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
94 if (iface
->dhcpv4_event
.uloop
.fd
< 0) {
95 syslog(LOG_ERR
, "socket(AF_INET): %m");
100 /* Basic IPv4 configuration */
101 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, SOL_SOCKET
, SO_REUSEADDR
,
102 &val
, sizeof(val
)) < 0) {
103 syslog(LOG_ERR
, "setsockopt(SO_REUSEADDR): %m");
108 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, SOL_SOCKET
, SO_BROADCAST
,
109 &val
, sizeof(val
)) < 0) {
110 syslog(LOG_ERR
, "setsockopt(SO_BROADCAST): %m");
115 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, IPPROTO_IP
, IP_PKTINFO
,
116 &val
, sizeof(val
)) < 0) {
117 syslog(LOG_ERR
, "setsockopt(IP_PKTINFO): %m");
122 val
= IPTOS_PREC_INTERNETCONTROL
;
123 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, IPPROTO_IP
, IP_TOS
,
124 &val
, sizeof(val
)) < 0) {
125 syslog(LOG_ERR
, "setsockopt(IP_TOS): %m");
130 val
= IP_PMTUDISC_DONT
;
131 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, IPPROTO_IP
, IP_MTU_DISCOVER
,
132 &val
, sizeof(val
)) < 0) {
133 syslog(LOG_ERR
, "setsockopt(IP_MTU_DISCOVER): %m");
138 if (setsockopt(iface
->dhcpv4_event
.uloop
.fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
139 iface
->ifname
, strlen(iface
->ifname
)) < 0) {
140 syslog(LOG_ERR
, "setsockopt(SO_BINDTODEVICE): %m");
145 if (bind(iface
->dhcpv4_event
.uloop
.fd
, (struct sockaddr
*)&bind_addr
,
146 sizeof(bind_addr
)) < 0) {
147 syslog(LOG_ERR
, "bind(): %m");
152 if (setup_dhcpv4_addresses(iface
) < 0) {
157 iface
->dhcpv4_event
.handle_dgram
= handle_dhcpv4
;
158 odhcpd_register(&iface
->dhcpv4_event
);
159 } else if (iface
->dhcpv4_assignments
.next
) {
160 while (!list_empty(&iface
->dhcpv4_assignments
))
161 dhcpv4_free_assignment(list_first_entry(&iface
->dhcpv4_assignments
,
162 struct dhcp_assignment
, head
));
166 if (ret
< 0 && iface
->dhcpv4_event
.uloop
.fd
> 0) {
167 close(iface
->dhcpv4_event
.uloop
.fd
);
168 iface
->dhcpv4_event
.uloop
.fd
= -1;
175 static void dhcpv4_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
177 struct interface
*iface
= info
->iface
;
179 if (!iface
|| iface
->dhcpv4
== MODE_DISABLED
)
183 case NETEV_IFINDEX_CHANGE
:
184 dhcpv4_setup_interface(iface
, true);
186 case NETEV_ADDRLIST_CHANGE
:
187 handle_addrlist_change(iface
);
194 static struct dhcp_assignment
*find_assignment_by_hwaddr(struct interface
*iface
, const uint8_t *hwaddr
)
196 struct dhcp_assignment
*a
;
198 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
199 if (!memcmp(a
->hwaddr
, hwaddr
, 6))
205 static int setup_dhcpv4_addresses(struct interface
*iface
)
207 iface
->dhcpv4_start_ip
.s_addr
= INADDR_ANY
;
208 iface
->dhcpv4_end_ip
.s_addr
= INADDR_ANY
;
209 iface
->dhcpv4_local
.s_addr
= INADDR_ANY
;
210 iface
->dhcpv4_bcast
.s_addr
= INADDR_ANY
;
211 iface
->dhcpv4_mask
.s_addr
= INADDR_ANY
;
214 if (iface
->dhcpv4_start
.s_addr
& htonl(0xffff0000) ||
215 iface
->dhcpv4_end
.s_addr
& htonl(0xffff0000) ||
216 ntohl(iface
->dhcpv4_start
.s_addr
) > ntohl(iface
->dhcpv4_end
.s_addr
)) {
217 syslog(LOG_ERR
, "Invalid DHCP range for %s", iface
->name
);
221 if (!iface
->addr4_len
) {
222 syslog(LOG_WARNING
, "No network(s) available on %s", iface
->name
);
226 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
227 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
229 for (size_t i
= 0; i
< iface
->addr4_len
&& start
&& end
; i
++) {
230 struct in_addr
*addr
= &iface
->addr4
[i
].addr
.in
;
233 if (addr_is_fr_ip(iface
, addr
))
236 odhcpd_bitlen2netmask(false, iface
->addr4
[i
].prefix
, &mask
);
237 if ((start
& ntohl(~mask
.s_addr
)) == start
&&
238 (end
& ntohl(~mask
.s_addr
)) == end
) {
239 iface
->dhcpv4_start_ip
.s_addr
= htonl(start
) |
240 (addr
->s_addr
& mask
.s_addr
);
241 iface
->dhcpv4_end_ip
.s_addr
= htonl(end
) |
242 (addr
->s_addr
& mask
.s_addr
);
243 iface
->dhcpv4_local
= *addr
;
244 iface
->dhcpv4_bcast
= iface
->addr4
[i
].broadcast
;
245 iface
->dhcpv4_mask
= mask
;
250 /* Don't allocate IP range for subnets bigger than 28 */
251 if (iface
->addr4
[0].prefix
> 28) {
252 syslog(LOG_WARNING
, "Auto allocation of DHCP range fails on %s", iface
->name
);
256 iface
->dhcpv4_local
= iface
->addr4
[0].addr
.in
;
257 iface
->dhcpv4_bcast
= iface
->addr4
[0].broadcast
;
258 odhcpd_bitlen2netmask(false, iface
->addr4
[0].prefix
, &iface
->dhcpv4_mask
);
259 end
= start
= iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
;
261 /* Auto allocate ranges */
262 if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffff00) {
263 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(100);
264 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(250);
265 } else if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffffc0) {
266 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(10);
267 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(60);
268 } else if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffffe0) {
269 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(10);
270 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(30);
272 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(3);
273 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(12);
279 static void inc_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct odhcpd_ref_ip
*ip
)
285 static void decr_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct interface
*iface
)
287 struct odhcpd_ref_ip
*ip
= *ptr
;
289 if (--ip
->ref_cnt
== 0) {
290 netlink_setup_addr(&ip
->addr
, iface
->ifindex
, false, false);
299 static bool addr_is_fr_ip(struct interface
*iface
, struct in_addr
*addr
)
301 struct odhcpd_ref_ip
*p
;
303 list_for_each_entry(p
, &iface
->dhcpv4_fr_ips
, head
) {
304 if (addr
->s_addr
== p
->addr
.addr
.in
.s_addr
)
311 static bool leases_require_fr(struct interface
*iface
, struct odhcpd_ipaddr
*addr
,
314 struct dhcp_assignment
*a
= NULL
;
315 struct odhcpd_ref_ip
*fr_ip
= NULL
;
317 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
) {
318 if ((a
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
) &&
320 ((a
->addr
& mask
) == (addr
->addr
.in
.s_addr
& mask
))) {
322 fr_ip
= calloc(1, sizeof(*fr_ip
));
326 list_add(&fr_ip
->head
, &iface
->dhcpv4_fr_ips
);
329 inc_ref_cnt_ip(&a
->fr_ip
, fr_ip
);
333 return fr_ip
? true : false;
336 static void valid_until_cb(struct uloop_timeout
*event
)
338 struct interface
*iface
;
339 time_t now
= odhcpd_time();
341 avl_for_each_element(&interfaces
, iface
, avl
) {
342 if (iface
->dhcpv4
!= MODE_SERVER
|| iface
->dhcpv4_assignments
.next
== NULL
)
345 struct dhcp_assignment
*a
, *n
;
346 list_for_each_entry_safe(a
, n
, &iface
->dhcpv4_assignments
, head
) {
347 if (!INFINITE_VALID(a
->valid_until
) && a
->valid_until
< now
)
348 dhcpv4_free_assignment(a
);
351 uloop_timeout_set(event
, 1000);
354 static void handle_addrlist_change(struct interface
*iface
)
356 struct odhcpd_ipaddr ip
;
357 struct odhcpd_ref_ip
*a
;
358 struct dhcp_assignment
*c
;
359 uint32_t mask
= iface
->dhcpv4_mask
.s_addr
;
361 memset(&ip
, 0, sizeof(ip
));
362 ip
.addr
.in
= iface
->dhcpv4_local
;
363 ip
.prefix
= odhcpd_netmask2bitlen(false, &iface
->dhcpv4_mask
);
364 ip
.broadcast
= iface
->dhcpv4_bcast
;
366 setup_dhcpv4_addresses(iface
);
368 if ((ip
.addr
.in
.s_addr
& mask
) ==
369 (iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
))
372 if (ip
.addr
.in
.s_addr
&& !leases_require_fr(iface
, &ip
, mask
))
375 if (iface
->dhcpv4_local
.s_addr
== INADDR_ANY
|| list_empty(&iface
->dhcpv4_fr_ips
))
378 a
= list_first_entry(&iface
->dhcpv4_fr_ips
, struct odhcpd_ref_ip
, head
);
380 if (netlink_setup_addr(&a
->addr
, iface
->ifindex
, false, true)) {
381 syslog(LOG_ERR
, "Failed to add ip address on %s", iface
->name
);
385 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
386 if ((c
->flags
& OAF_BOUND
) && c
->fr_ip
&& !c
->fr_cnt
) {
387 if (c
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
)
388 dhcpv4_fr_rand_delay(c
);
395 static char *dhcpv4_msg_to_string(uint8_t reqmsg
)
398 case (DHCPV4_MSG_DISCOVER
):
399 return "DHCPV4_MSG_DISCOVER";
400 case (DHCPV4_MSG_OFFER
):
401 return "DHCPV4_MSG_OFFER";
402 case (DHCPV4_MSG_REQUEST
):
403 return "DHCPV4_MSG_REQUEST";
404 case (DHCPV4_MSG_DECLINE
):
405 return "DHCPV4_MSG_DECLINE";
406 case (DHCPV4_MSG_ACK
):
407 return "DHCPV4_MSG_ACK";
408 case (DHCPV4_MSG_NAK
):
409 return "DHCPV4_MSG_NAK";
410 case (DHCPV4_MSG_RELEASE
):
411 return "DHCPV4_MSG_RELEASE";
412 case (DHCPV4_MSG_INFORM
):
413 return "DHCPV4_MSG_INFORM";
414 case (DHCPV4_MSG_FORCERENEW
):
415 return "DHCPV4_MSG_FORCERENEW";
421 void dhcpv4_free_assignment(struct dhcp_assignment
*a
)
426 if (a
->lease_list
.next
)
427 list_del(&a
->lease_list
);
436 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
437 uint8_t type
, uint8_t len
, const void *data
)
439 uint8_t *c
= *cookie
;
440 uint8_t *end
= (uint8_t *)msg
+ sizeof(*msg
);
442 if (*cookie
+ 2 + len
> end
)
447 memcpy(c
, data
, len
);
452 static void dhcpv4_fr_send(struct dhcp_assignment
*a
)
454 struct dhcpv4_message fr_msg
= {
455 .op
= DHCPV4_BOOTREPLY
,
461 .ciaddr
= {INADDR_ANY
},
462 .yiaddr
= {INADDR_ANY
},
463 .siaddr
= {INADDR_ANY
},
464 .giaddr
= {INADDR_ANY
},
469 struct dhcpv4_auth_forcerenew
*auth_o
, auth
= {
473 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
477 struct interface
*iface
= a
->iface
;
479 odhcpd_urandom(&fr_msg
.xid
, sizeof(fr_msg
.xid
));
480 memcpy(fr_msg
.chaddr
, a
->hwaddr
, fr_msg
.hlen
);
482 fr_msg
.options
[0] = 0x63;
483 fr_msg
.options
[1] = 0x82;
484 fr_msg
.options
[2] = 0x53;
485 fr_msg
.options
[3] = 0x63;
487 uint8_t *cookie
= &fr_msg
.options
[4];
488 uint8_t msg
= DHCPV4_MSG_FORCERENEW
;
490 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
491 if (a
->accept_fr_nonce
) {
492 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
493 auth_o
= (struct dhcpv4_auth_forcerenew
*)(cookie
- sizeof(auth
));
494 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
497 uint8_t secretbytes
[64];
498 memset(secretbytes
, 0, sizeof(secretbytes
));
499 memcpy(secretbytes
, a
->key
, sizeof(a
->key
));
501 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
502 secretbytes
[i
] ^= 0x36;
505 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
506 md5_hash(&fr_msg
, sizeof(fr_msg
), &md5
);
507 md5_end(auth_o
->key
, &md5
);
509 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
510 secretbytes
[i
] ^= 0x36;
511 secretbytes
[i
] ^= 0x5c;
515 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
516 md5_hash(auth_o
->key
, sizeof(auth_o
->key
), &md5
);
517 md5_end(auth_o
->key
, &md5
);
519 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_SERVERID
, 4,
520 &a
->fr_ip
->addr
.addr
.in
.s_addr
);
521 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
524 struct sockaddr_in dest
;
525 memset(&dest
, 0, sizeof(dest
));
526 dest
.sin_family
= AF_INET
;
527 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
528 dest
.sin_addr
.s_addr
= a
->addr
;
530 if (sendto(iface
->dhcpv4_event
.uloop
.fd
, &fr_msg
, sizeof(fr_msg
),
531 MSG_DONTWAIT
, (struct sockaddr
*)&dest
, sizeof(dest
)) < 0)
532 syslog(LOG_ERR
, "Failed to send %s to %s - %s: %m", dhcpv4_msg_to_string(msg
),
533 odhcpd_print_mac(a
->hwaddr
, sizeof(a
->hwaddr
)), inet_ntoa(dest
.sin_addr
));
535 syslog(LOG_WARNING
, "Sent %s to %s - %s", dhcpv4_msg_to_string(msg
),
536 odhcpd_print_mac(a
->hwaddr
, sizeof(a
->hwaddr
)), inet_ntoa(dest
.sin_addr
));
539 static void dhcpv4_fr_timer(struct uloop_timeout
*event
)
541 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
543 if (a
->fr_cnt
> 0 && a
->fr_cnt
< 8) {
545 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
551 static void dhcpv4_fr_start(struct dhcp_assignment
*a
)
553 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
554 a
->fr_timer
.cb
= dhcpv4_fr_timer
;
560 static void dhcpv4_fr_delay_timer(struct uloop_timeout
*event
)
562 struct dhcp_assignment
*a
= container_of(event
, struct dhcp_assignment
, fr_timer
);
563 struct interface
*iface
= a
->iface
;
565 (iface
->dhcpv4_event
.uloop
.fd
== -1 ? dhcpv4_fr_rand_delay(a
) : dhcpv4_fr_start(a
));
568 static void dhcpv4_fr_rand_delay(struct dhcp_assignment
*a
)
570 #define MIN_DELAY 500
574 odhcpd_urandom(&msecs
, sizeof(msecs
));
576 msecs
= labs(msecs
)%MAX_FUZZ
+ MIN_DELAY
;
578 uloop_timeout_set(&a
->fr_timer
, msecs
);
579 a
->fr_timer
.cb
= dhcpv4_fr_delay_timer
;
582 static void dhcpv4_fr_stop(struct dhcp_assignment
*a
)
584 uloop_timeout_cancel(&a
->fr_timer
);
585 decr_ref_cnt_ip(&a
->fr_ip
, a
->iface
);
587 a
->fr_timer
.cb
= NULL
;
590 /* Handler for DHCPv4 messages */
591 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
592 struct interface
*iface
, _unused
void *dest_addr
)
594 struct dhcpv4_message
*req
= data
;
599 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
600 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
603 syslog(LOG_NOTICE
, "Got DHCPv4 request on %s", iface
->name
);
605 if (!iface
->dhcpv4_start_ip
.s_addr
&& !iface
->dhcpv4_end_ip
.s_addr
) {
606 syslog(LOG_WARNING
, "No DHCP range available on %s", iface
->name
);
610 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
612 struct dhcpv4_message reply
= {
613 .op
= DHCPV4_BOOTREPLY
,
620 .ciaddr
= {INADDR_ANY
},
621 .giaddr
= req
->giaddr
,
622 .siaddr
= iface
->dhcpv4_local
,
624 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
626 reply
.options
[0] = 0x63;
627 reply
.options
[1] = 0x82;
628 reply
.options
[2] = 0x53;
629 reply
.options
[3] = 0x63;
631 uint8_t *cookie
= &reply
.options
[4];
632 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
633 uint8_t msg
= DHCPV4_MSG_ACK
;
635 uint32_t reqaddr
= INADDR_ANY
;
636 uint32_t leasetime
= 0;
637 size_t hostname_len
= 0;
639 bool accept_fr_nonce
= false;
640 bool incl_fr_opt
= false;
642 uint8_t *start
= &req
->options
[4];
643 uint8_t *end
= ((uint8_t*)data
) + len
;
644 struct dhcpv4_option
*opt
;
645 dhcpv4_for_each_option(start
, end
, opt
) {
646 if (opt
->type
== DHCPV4_OPT_MESSAGE
&& opt
->len
== 1)
647 reqmsg
= opt
->data
[0];
648 else if (opt
->type
== DHCPV4_OPT_HOSTNAME
&& opt
->len
> 0) {
649 hostname_len
= opt
->len
;
650 memcpy(hostname
, opt
->data
, hostname_len
);
651 hostname
[hostname_len
] = 0;
652 } else if (opt
->type
== DHCPV4_OPT_IPADDRESS
&& opt
->len
== 4)
653 memcpy(&reqaddr
, opt
->data
, 4);
654 else if (opt
->type
== DHCPV4_OPT_SERVERID
&& opt
->len
== 4) {
655 if (memcmp(opt
->data
, &iface
->dhcpv4_local
, 4))
657 } else if (iface
->filter_class
&& opt
->type
== DHCPV4_OPT_USER_CLASS
) {
658 uint8_t *c
= opt
->data
, *cend
= &opt
->data
[opt
->len
];
659 for (; c
< cend
&& &c
[*c
] < cend
; c
= &c
[1 + *c
]) {
660 size_t elen
= strlen(iface
->filter_class
);
661 if (*c
== elen
&& !memcmp(&c
[1], iface
->filter_class
, elen
))
662 return; // Ignore from homenet
664 } else if (opt
->type
== DHCPV4_OPT_LEASETIME
&& opt
->len
== 4)
665 memcpy(&leasetime
, opt
->data
, 4);
666 else if (opt
->type
== DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
&& opt
->len
> 0) {
667 for (uint8_t i
= 0; i
< opt
->len
; i
++) {
668 if (opt
->data
[i
] == 1) {
669 accept_fr_nonce
= true;
677 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
678 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
679 reqmsg
!= DHCPV4_MSG_RELEASE
)
682 struct dhcp_assignment
*a
= NULL
;
683 uint32_t serverid
= iface
->dhcpv4_local
.s_addr
;
684 uint32_t fr_serverid
= INADDR_ANY
;
686 if (reqmsg
!= DHCPV4_MSG_INFORM
)
687 a
= dhcpv4_lease(iface
, reqmsg
, req
->chaddr
, reqaddr
,
688 &leasetime
, hostname
, hostname_len
,
689 accept_fr_nonce
, &incl_fr_opt
, &fr_serverid
);
692 if (reqmsg
== DHCPV4_MSG_REQUEST
)
693 msg
= DHCPV4_MSG_NAK
;
694 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
696 } else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
697 msg
= DHCPV4_MSG_OFFER
;
698 else if (reqmsg
== DHCPV4_MSG_REQUEST
&&
699 ((reqaddr
&& reqaddr
!= a
->addr
) ||
700 (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= a
->addr
))) {
701 msg
= DHCPV4_MSG_NAK
;
703 * DHCP client requested an IP which we can't offer to him. Probably the
704 * client changed the network or the network has been changed. The reply
705 * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
707 * For modern devices we build an answer that includes a valid IP, like
708 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
709 * perform additional DHCP round trips.
715 * Buggy clients do serverid checking in nack messages; therefore set the
716 * serverid in nack messages triggered by a previous force renew equal to
717 * the server id in use at that time by the server
721 serverid
= fr_serverid
;
723 if (req
->ciaddr
.s_addr
&&
724 ((iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
) !=
725 (req
->ciaddr
.s_addr
& iface
->dhcpv4_mask
.s_addr
)))
726 req
->ciaddr
.s_addr
= INADDR_ANY
;
729 syslog(LOG_WARNING
, "Received %s from %s on %s", dhcpv4_msg_to_string(reqmsg
),
730 odhcpd_print_mac(req
->chaddr
, req
->hlen
), iface
->name
);
733 if (reqmsg
== DHCPV4_MSG_RELEASE
)
734 ubus_bcast_dhcp_event("dhcp.release", req
->chaddr
, req
->hlen
,
735 &req
->ciaddr
, hostname
, iface
->ifname
);
737 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
740 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
741 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &serverid
);
746 reply
.yiaddr
.s_addr
= a
->addr
;
748 val
= htonl(leasetime
);
749 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
751 if (leasetime
!= UINT32_MAX
) {
752 val
= htonl(500 * leasetime
/ 1000);
753 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
755 val
= htonl(875 * leasetime
/ 1000);
756 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
759 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4,
760 &iface
->dhcpv4_mask
.s_addr
);
763 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
764 strlen(a
->hostname
), a
->hostname
);
766 if (iface
->dhcpv4_bcast
.s_addr
!= INADDR_ANY
)
767 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &iface
->dhcpv4_bcast
);
770 if (reqmsg
== DHCPV4_MSG_REQUEST
) {
771 struct dhcpv4_auth_forcerenew auth
= {
775 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
780 memcpy(auth
.key
, a
->key
, sizeof(auth
.key
));
781 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
784 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
,
792 memset(&ifr
, 0, sizeof(ifr
));
793 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
) - 1);
795 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
796 uint16_t mtu
= htons(ifr
.ifr_mtu
);
797 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
800 if (iface
->search
&& iface
->search_len
<= 255)
801 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
802 iface
->search_len
, iface
->search
);
803 else if (!res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
804 uint8_t search_buf
[256];
805 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
806 sizeof(search_buf
), NULL
, NULL
);
808 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
812 if (iface
->dhcpv4_router_cnt
== 0)
813 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &iface
->dhcpv4_local
);
815 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
,
816 4 * iface
->dhcpv4_router_cnt
, iface
->dhcpv4_router
);
819 if (iface
->dhcpv4_dns_cnt
== 0)
820 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &iface
->dhcpv4_local
);
822 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
823 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
826 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
828 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
829 if (req
->giaddr
.s_addr
) {
831 * relay agent is configured, send reply to the agent
833 dest
.sin_addr
= req
->giaddr
;
834 dest
.sin_port
= htons(DHCPV4_SERVER_PORT
);
835 } else if (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= dest
.sin_addr
.s_addr
) {
837 * client has existing configuration (ciaddr is set) AND this address is
838 * not the address it used for the dhcp message
840 dest
.sin_addr
= req
->ciaddr
;
841 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
842 } else if ((ntohs(req
->flags
) & DHCPV4_FLAG_BROADCAST
) ||
843 req
->hlen
!= reply
.hlen
|| !reply
.yiaddr
.s_addr
) {
845 * client requests a broadcast reply OR we can't offer an IP
847 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
848 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
849 } else if (!req
->ciaddr
.s_addr
&& msg
== DHCPV4_MSG_NAK
) {
851 * client has no previous configuration -> no IP, so we need to reply
852 * with a broadcast packet
854 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
855 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
857 struct arpreq arp
= {.arp_flags
= ATF_COM
};
860 * send reply to the newly (in this proccess) allocated IP
862 dest
.sin_addr
= reply
.yiaddr
;
863 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
865 memcpy(arp
.arp_ha
.sa_data
, req
->chaddr
, 6);
866 memcpy(&arp
.arp_pa
, &dest
, sizeof(arp
.arp_pa
));
867 memcpy(arp
.arp_dev
, iface
->ifname
, sizeof(arp
.arp_dev
));
869 if (ioctl(sock
, SIOCSARP
, &arp
) < 0)
870 syslog(LOG_ERR
, "ioctl(SIOCSARP): %m");
873 if (sendto(sock
, &reply
, sizeof(reply
), MSG_DONTWAIT
,
874 (struct sockaddr
*)&dest
, sizeof(dest
)) < 0)
875 syslog(LOG_ERR
, "Failed to send %s to %s - %s: %m",
876 dhcpv4_msg_to_string(msg
),
877 dest
.sin_addr
.s_addr
== INADDR_BROADCAST
?
878 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req
->chaddr
, req
->hlen
),
879 inet_ntoa(dest
.sin_addr
));
881 syslog(LOG_ERR
, "Sent %s to %s - %s",
882 dhcpv4_msg_to_string(msg
),
883 dest
.sin_addr
.s_addr
== INADDR_BROADCAST
?
884 "ff:ff:ff:ff:ff:ff": odhcpd_print_mac(req
->chaddr
, req
->hlen
),
885 inet_ntoa(dest
.sin_addr
));
889 if (msg
== DHCPV4_MSG_ACK
)
890 ubus_bcast_dhcp_event("dhcp.ack", req
->chaddr
, req
->hlen
, &reply
.yiaddr
,
891 hostname
, iface
->ifname
);
895 static bool dhcpv4_assign(struct interface
*iface
, struct dhcp_assignment
*a
,
898 struct dhcp_assignment
*c
;
899 uint32_t start
= ntohl(iface
->dhcpv4_start_ip
.s_addr
);
900 uint32_t end
= ntohl(iface
->dhcpv4_end_ip
.s_addr
);
901 uint32_t count
= end
- start
+ 1;
904 /* Preconfigured IP address by static lease */
906 if (list_empty(&iface
->dhcpv4_assignments
)) {
907 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
911 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
912 if (ntohl(c
->addr
) > ntohl(a
->addr
)) {
913 list_add_tail(&a
->head
, &c
->head
);
915 } else if (ntohl(a
->addr
) == ntohl(c
->addr
))
920 /* try to assign the IP the client asked for */
921 if (start
<= ntohl(raddr
) && ntohl(raddr
) <= end
&&
922 !config_find_lease_by_ipaddr(raddr
)) {
923 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
924 if (ntohl(c
->addr
) > ntohl(raddr
)) {
925 list_add_tail(&a
->head
, &c
->head
);
928 syslog(LOG_INFO
, "Assigning the IP the client asked for: %u.%u.%u.%u",
929 ((uint8_t *)&a
->addr
)[0], ((uint8_t *)&a
->addr
)[1],
930 ((uint8_t *)&a
->addr
)[2], ((uint8_t *)&a
->addr
)[3]);
933 } else if (ntohl(raddr
) == ntohl(c
->addr
))
938 /* Seed RNG with checksum of hwaddress */
939 for (size_t i
= 0; i
< sizeof(a
->hwaddr
); ++i
) {
940 /* Knuth's multiplicative method */
941 uint8_t o
= a
->hwaddr
[i
];
942 seed
+= (o
*2654435761) % UINT32_MAX
;
947 for (uint32_t i
= 0, try = (((uint32_t)rand()) % count
) + start
; i
< count
;
948 ++i
, try = (((try - start
) + 1) % count
) + start
) {
950 if (config_find_lease_by_ipaddr(try))
953 if (list_empty(&iface
->dhcpv4_assignments
)) {
954 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
955 a
->addr
= htonl(try);
957 syslog(LOG_INFO
, "Assigning mapped IP (empty list): %u.%u.%u.%u",
958 ((uint8_t *)&a
->addr
)[0], ((uint8_t *)&a
->addr
)[1],
959 ((uint8_t *)&a
->addr
)[2], ((uint8_t *)&a
->addr
)[3]);
964 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
965 if (ntohl(c
->addr
) > try) {
966 list_add_tail(&a
->head
, &c
->head
);
967 a
->addr
= htonl(try);
969 syslog(LOG_DEBUG
, "Assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
970 ((uint8_t *)&a
->addr
)[0], ((uint8_t *)&a
->addr
)[1],
971 ((uint8_t *)&a
->addr
)[2], ((uint8_t *)&a
->addr
)[3],
975 } else if (try == ntohl(c
->addr
))
980 syslog(LOG_WARNING
, "Can't assign any IP address -> address space is full");
985 static struct dhcp_assignment
*
986 dhcpv4_lease(struct interface
*iface
, enum dhcpv4_msg msg
, const uint8_t *mac
,
987 const uint32_t reqaddr
, uint32_t *leasetime
, const char *hostname
,
988 const size_t hostname_len
, const bool accept_fr_nonce
, bool *incl_fr_opt
,
989 uint32_t *fr_serverid
)
991 struct dhcp_assignment
*a
= find_assignment_by_hwaddr(iface
, mac
);
992 struct lease
*l
= config_find_lease_by_mac(mac
);
993 time_t now
= odhcpd_time();
995 if (l
&& a
&& a
->lease
!= l
) {
996 dhcpv4_free_assignment(a
);
1000 if (a
&& (a
->flags
& OAF_BOUND
) && a
->fr_ip
) {
1001 *fr_serverid
= a
->fr_ip
->addr
.addr
.in
.s_addr
;
1005 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
1006 bool assigned
= !!a
;
1009 if (!iface
->no_dynamic_dhcp
|| l
) {
1010 /* Create new binding */
1011 a
= calloc(1, sizeof(*a
));
1013 syslog(LOG_ERR
, "Failed to calloc binding on interface %s",
1017 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
1018 /* Set valid time to 0 for static lease indicating */
1019 /* infinite lifetime otherwise current time */
1020 a
->valid_until
= l
? 0 : now
;
1022 a
->flags
= OAF_DHCPV4
;
1023 a
->addr
= l
? l
->ipaddr
: INADDR_ANY
;
1025 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1028 a
->flags
|= OAF_STATIC
;
1031 a
->hostname
= strdup(l
->hostname
);
1034 a
->leasetime
= l
->leasetime
;
1036 list_add(&a
->lease_list
, &l
->assignments
);
1040 } else if (((a
->addr
& iface
->dhcpv4_mask
.s_addr
) !=
1041 (iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
)) &&
1042 !(a
->flags
& OAF_STATIC
)) {
1044 a
->addr
= INADDR_ANY
;
1046 assigned
= dhcpv4_assign(iface
, a
, reqaddr
);
1050 uint32_t my_leasetime
;
1053 my_leasetime
= a
->leasetime
;
1055 my_leasetime
= iface
->dhcpv4_leasetime
;
1057 if ((*leasetime
== 0) || (my_leasetime
< *leasetime
))
1058 *leasetime
= my_leasetime
;
1060 if (msg
== DHCPV4_MSG_DISCOVER
) {
1061 a
->flags
&= ~OAF_BOUND
;
1063 *incl_fr_opt
= accept_fr_nonce
;
1064 if (!(a
->flags
& OAF_STATIC
))
1065 a
->valid_until
= now
;
1067 if ((!(a
->flags
& OAF_STATIC
) || !a
->hostname
) && hostname_len
> 0) {
1068 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
1070 memcpy(a
->hostname
, hostname
, hostname_len
);
1071 a
->hostname
[hostname_len
] = 0;
1073 if (odhcpd_valid_hostname(a
->hostname
))
1074 a
->flags
&= ~OAF_BROKEN_HOSTNAME
;
1076 a
->flags
|= OAF_BROKEN_HOSTNAME
;
1080 if (!(a
->flags
& OAF_BOUND
)) {
1081 a
->accept_fr_nonce
= accept_fr_nonce
;
1082 *incl_fr_opt
= accept_fr_nonce
;
1083 odhcpd_urandom(a
->key
, sizeof(a
->key
));
1084 a
->flags
|= OAF_BOUND
;
1086 *incl_fr_opt
= false;
1088 if (!(a
->flags
& OAF_STATIC
))
1089 a
->valid_until
= ((*leasetime
== UINT32_MAX
) ? 0 : (time_t)(now
+ *leasetime
));
1091 } else if (!assigned
&& a
) {
1092 /* Cleanup failed assignment */
1093 dhcpv4_free_assignment(a
);
1097 } else if (msg
== DHCPV4_MSG_RELEASE
&& a
) {
1098 a
->flags
&= ~OAF_BOUND
;
1100 if (!(a
->flags
& OAF_STATIC
))
1101 a
->valid_until
= now
- 1;
1103 } else if (msg
== DHCPV4_MSG_DECLINE
&& a
) {
1104 a
->flags
&= ~OAF_BOUND
;
1106 if (!(a
->flags
& OAF_STATIC
)) {
1107 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
1108 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1112 dhcpv6_ia_write_statefile();