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 int setup_dhcpv4_addresses(struct interface
*iface
);
39 static void update_static_assignments(struct interface
*iface
);
40 static void free_dhcpv4_assignment(struct dhcpv4_assignment
*a
);
41 static void dhcpv4_fr_start(struct dhcpv4_assignment
*a
);
42 static void dhcpv4_fr_stop(struct dhcpv4_assignment
*a
);
43 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
44 struct interface
*iface
, void *dest_addr
);
45 static struct dhcpv4_assignment
* dhcpv4_lease(struct interface
*iface
,
46 enum dhcpv4_msg msg
, const uint8_t *mac
, struct in_addr reqaddr
,
47 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
,
48 const bool accept_fr_nonce
, bool *incl_fr_opt
, uint32_t *fr_serverid
);
49 static uint32_t serial
= 0;
51 struct odhcpd_ref_ip
{
52 struct list_head head
;
54 struct odhcpd_ipaddr addr
;
57 /* Create socket and register events */
63 int setup_dhcpv4_interface(struct interface
*iface
, bool enable
)
65 if (iface
->dhcpv4_event
.uloop
.fd
> 0) {
66 uloop_fd_delete(&iface
->dhcpv4_event
.uloop
);
67 close(iface
->dhcpv4_event
.uloop
.fd
);
68 iface
->dhcpv4_event
.uloop
.fd
= -1;
71 if (iface
->dhcpv4
&& enable
) {
72 if (!iface
->dhcpv4_assignments
.next
)
73 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
75 if (!iface
->dhcpv4_fr_ips
.next
)
76 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
78 int sock
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
80 syslog(LOG_ERR
, "Failed to create DHCPv4 server socket: %s",
85 /* Basic IPv4 configuration */
87 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
88 setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
));
89 setsockopt(sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof(val
));
91 val
= IPTOS_PREC_INTERNETCONTROL
;
92 setsockopt(sock
, IPPROTO_IP
, IP_TOS
, &val
, sizeof(val
));
94 val
= IP_PMTUDISC_DONT
;
95 setsockopt(sock
, IPPROTO_IP
, IP_MTU_DISCOVER
, &val
, sizeof(val
));
97 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
,
98 iface
->ifname
, strlen(iface
->ifname
));
100 struct sockaddr_in bind_addr
= {AF_INET
, htons(DHCPV4_SERVER_PORT
),
103 if (bind(sock
, (struct sockaddr
*)&bind_addr
, sizeof(bind_addr
))) {
104 syslog(LOG_ERR
, "Failed to open DHCPv4 server socket: %s",
109 iface
->dhcpv4_event
.uloop
.fd
= sock
;
110 iface
->dhcpv4_event
.handle_dgram
= handle_dhcpv4
;
111 odhcpd_register(&iface
->dhcpv4_event
);
113 if (setup_dhcpv4_addresses(iface
) < 0)
116 update_static_assignments(iface
);
117 } else if (iface
->dhcpv4_assignments
.next
) {
118 while (!list_empty(&iface
->dhcpv4_assignments
))
119 free_dhcpv4_assignment(list_first_entry(&iface
->dhcpv4_assignments
,
120 struct dhcpv4_assignment
, head
));
125 static struct dhcpv4_assignment
*find_assignment_by_hwaddr(struct interface
*iface
, const uint8_t *hwaddr
)
127 struct dhcpv4_assignment
*a
;
129 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
130 if (!memcmp(a
->hwaddr
, hwaddr
, 6))
136 static struct dhcpv4_assignment
*find_assignment_by_addr(struct interface
*iface
, const uint32_t addr
)
138 struct dhcpv4_assignment
*a
;
140 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
)
147 static int setup_dhcpv4_addresses(struct interface
*iface
)
149 iface
->dhcpv4_start_ip
.s_addr
= INADDR_ANY
;
150 iface
->dhcpv4_end_ip
.s_addr
= INADDR_ANY
;
151 iface
->dhcpv4_local
.s_addr
= INADDR_ANY
;
152 iface
->dhcpv4_bcast
.s_addr
= INADDR_ANY
;
153 iface
->dhcpv4_mask
.s_addr
= INADDR_ANY
;
156 if (iface
->dhcpv4_start
.s_addr
& htonl(0xffff0000) ||
157 iface
->dhcpv4_end
.s_addr
& htonl(0xffff0000) ||
158 ntohl(iface
->dhcpv4_start
.s_addr
) > ntohl(iface
->dhcpv4_end
.s_addr
)) {
159 syslog(LOG_ERR
, "invalid DHCP range for %s", iface
->name
);
163 if (!iface
->addr4_len
) {
164 syslog(LOG_WARNING
, "no network(s) available on %s", iface
->name
);
168 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
169 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
171 for (size_t i
= 0; i
< iface
->addr4_len
&& start
&& end
; i
++) {
172 struct in_addr
*addr
= &iface
->addr4
[i
].addr
.in
;
175 odhcpd_bitlen2netmask(false, iface
->addr4
[i
].prefix
, &mask
);
176 if ((start
& ntohl(~mask
.s_addr
)) == start
&&
177 (end
& ntohl(~mask
.s_addr
)) == end
) {
178 iface
->dhcpv4_start_ip
.s_addr
= htonl(start
) |
179 (addr
->s_addr
& mask
.s_addr
);
180 iface
->dhcpv4_end_ip
.s_addr
= htonl(end
) |
181 (addr
->s_addr
& mask
.s_addr
);
182 iface
->dhcpv4_local
= *addr
;
183 iface
->dhcpv4_bcast
= iface
->addr4
[i
].broadcast
;
184 iface
->dhcpv4_mask
= mask
;
189 /* Don't allocate IP range for subnets bigger than 28 */
190 if (iface
->addr4
[0].prefix
> 28) {
191 syslog(LOG_WARNING
, "auto allocation of DHCP range fails on %s", iface
->name
);
195 iface
->dhcpv4_local
= iface
->addr4
[0].addr
.in
;
196 iface
->dhcpv4_bcast
= iface
->addr4
[0].broadcast
;
197 odhcpd_bitlen2netmask(false, iface
->addr4
[0].prefix
, &iface
->dhcpv4_mask
);
198 end
= start
= iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
;
200 /* Auto allocate ranges */
201 if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffff00) {
202 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(100);
203 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(250);
204 } else if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffffc0) {
205 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(10);
206 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(60);
207 } else if (ntohl(iface
->dhcpv4_mask
.s_addr
) <= 0xffffffe0) {
208 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(10);
209 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(30);
211 iface
->dhcpv4_start_ip
.s_addr
= start
| htonl(3);
212 iface
->dhcpv4_end_ip
.s_addr
= end
| htonl(12);
218 static void update_static_assignments(struct interface
*iface
)
220 struct dhcpv4_assignment
*a
, *c
;
222 /* Cleanup static entries not belonging to the network */
223 list_for_each_entry_safe(a
, c
, &iface
->dhcpv4_assignments
, head
) {
224 if ((a
->flags
& OAF_STATIC
) &&
225 ((a
->addr
& iface
->dhcpv4_mask
.s_addr
) !=
226 (iface
->dhcpv4_start
.s_addr
& iface
->dhcpv4_mask
.s_addr
)))
227 free_dhcpv4_assignment(a
);
230 /* Parse static entries */
232 list_for_each_entry(lease
, &leases
, head
) {
233 if ((iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
) !=
234 (lease
->ipaddr
.s_addr
& iface
->dhcpv4_mask
.s_addr
)) {
238 a
= find_assignment_by_hwaddr(iface
, lease
->mac
.ether_addr_octet
);
241 /* Check if there's an assignment with the specified IP address */
242 if (find_assignment_by_addr(iface
, lease
->ipaddr
.s_addr
))
245 /* Construct entry */
246 a
= calloc(1, sizeof(*a
));
248 syslog(LOG_ERR
, "Calloc failed for static lease on interface %s",
252 memcpy(a
->hwaddr
, lease
->mac
.ether_addr_octet
, sizeof(a
->hwaddr
));
255 a
->leasetime
= lease
->dhcpv4_leasetime
;
257 a
->addr
= lease
->ipaddr
.s_addr
;
258 /* Static assignment */
259 a
->flags
|= OAF_STATIC
;
263 if (lease
->hostname
[0]) {
265 a
->hostname
= strdup(lease
->hostname
);
268 /* Assign to all interfaces */
269 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
270 if (ntohl(c
->addr
) > ntohl(a
->addr
)) {
271 list_add_tail(&a
->head
, &c
->head
);
276 if (&c
->head
== &iface
->dhcpv4_assignments
)
277 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
281 static void inc_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct odhcpd_ref_ip
*ip
)
287 static void decr_ref_cnt_ip(struct odhcpd_ref_ip
**ptr
, struct interface
*iface
)
289 struct odhcpd_ref_ip
*ip
= *ptr
;
291 if (--ip
->ref_cnt
== 0) {
292 odhcpd_setup_addr(&ip
->addr
, iface
, false, false);
301 static bool leases_require_fr(struct interface
*iface
, struct odhcpd_ipaddr
*addr
,
304 struct dhcpv4_assignment
*a
= NULL
;
305 struct odhcpd_ref_ip
*fr_ip
= NULL
;
307 list_for_each_entry(a
, &iface
->dhcpv4_assignments
, head
) {
308 if ((a
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
) &&
310 ((a
->addr
& mask
) == (addr
->addr
.in
.s_addr
& mask
))) {
312 fr_ip
= calloc(1, sizeof(*fr_ip
));
316 list_add(&fr_ip
->head
, &iface
->dhcpv4_fr_ips
);
319 inc_ref_cnt_ip(&a
->fr_ip
, fr_ip
);
323 return fr_ip
? true : false;
326 void dhcpv4_addr_update(struct interface
*iface
)
328 if (iface
->dhcpv4
== MODE_DISABLED
)
331 struct odhcpd_ipaddr ip
;
332 struct odhcpd_ref_ip
*a
;
333 struct dhcpv4_assignment
*c
;
334 uint32_t mask
= iface
->dhcpv4_mask
.s_addr
;
336 memset(&ip
, sizeof(ip
), 0);
337 ip
.addr
.in
= iface
->dhcpv4_local
;
338 ip
.prefix
= odhcpd_netmask2bitlen(false, &iface
->dhcpv4_mask
);
339 ip
.broadcast
= iface
->dhcpv4_bcast
;
341 setup_dhcpv4_addresses(iface
);
343 if ((ip
.addr
.in
.s_addr
& mask
) ==
344 (iface
->dhcpv4_local
.s_addr
& iface
->dhcpv4_mask
.s_addr
))
347 if (ip
.addr
.in
.s_addr
&& !leases_require_fr(iface
, &ip
, mask
))
350 if (iface
->dhcpv4_local
.s_addr
== INADDR_ANY
|| list_empty(&iface
->dhcpv4_fr_ips
))
353 a
= list_first_entry(&iface
->dhcpv4_fr_ips
, struct odhcpd_ref_ip
, head
);
355 if (odhcpd_setup_addr(&a
->addr
, iface
, false, true)) {
356 syslog(LOG_ERR
, "Failed to add ip address");
360 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
361 if ((c
->flags
& OAF_BOUND
) && c
->fr_ip
&& !c
->fr_cnt
) {
362 if (c
->accept_fr_nonce
|| iface
->dhcpv4_forcereconf
)
370 static char *dhcpv4_msg_to_string(uint8_t reqmsg
)
373 case (DHCPV4_MSG_DISCOVER
):
374 return "DHCPV4_MSG_DISCOVER";
375 case (DHCPV4_MSG_OFFER
):
376 return "DHCPV4_MSG_OFFER";
377 case (DHCPV4_MSG_REQUEST
):
378 return "DHCPV4_MSG_REQUEST";
379 case (DHCPV4_MSG_DECLINE
):
380 return "DHCPV4_MSG_DECLINE";
381 case (DHCPV4_MSG_ACK
):
382 return "DHCPV4_MSG_ACK";
383 case (DHCPV4_MSG_NAK
):
384 return "DHCPV4_MSG_NAK";
385 case (DHCPV4_MSG_RELEASE
):
386 return "DHCPV4_MSG_RELEASE";
387 case (DHCPV4_MSG_INFORM
):
388 return "DHCPV4_MSG_INFORM";
389 case (DHCPV4_MSG_FORCERENEW
):
390 return "DHCPV4_MSG_FORCERENEW";
396 static void free_dhcpv4_assignment(struct dhcpv4_assignment
*a
)
408 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
409 uint8_t type
, uint8_t len
, const void *data
)
411 uint8_t *c
= *cookie
;
412 if (*cookie
+ 2 + len
> (uint8_t*)&msg
[1])
417 memcpy(c
, data
, len
);
422 static void dhcpv4_fr_send(struct dhcpv4_assignment
*a
)
424 struct dhcpv4_message fr_msg
= {
425 .op
= DHCPV4_BOOTREPLY
,
431 .ciaddr
= {INADDR_ANY
},
432 .yiaddr
= {INADDR_ANY
},
433 .siaddr
= {INADDR_ANY
},
434 .giaddr
= {INADDR_ANY
},
439 struct dhcpv4_auth_forcerenew
*auth_o
, auth
= {
443 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
447 struct interface
*iface
= a
->iface
;
449 odhcpd_urandom(&fr_msg
.xid
, sizeof(fr_msg
.xid
));
450 memcpy(fr_msg
.chaddr
, a
->hwaddr
, fr_msg
.hlen
);
452 fr_msg
.options
[0] = 0x63;
453 fr_msg
.options
[1] = 0x82;
454 fr_msg
.options
[2] = 0x53;
455 fr_msg
.options
[3] = 0x63;
457 uint8_t *cookie
= &fr_msg
.options
[4];
458 uint8_t msg
= DHCPV4_MSG_FORCERENEW
;
460 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
461 if (a
->accept_fr_nonce
) {
462 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
463 auth_o
= (struct dhcpv4_auth_forcerenew
*)(cookie
- sizeof(auth
));
464 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
467 uint8_t secretbytes
[64];
468 memset(secretbytes
, 0, sizeof(secretbytes
));
469 memcpy(secretbytes
, a
->key
, sizeof(a
->key
));
471 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
)
472 secretbytes
[i
] ^= 0x36;
475 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
476 md5_hash(&fr_msg
, sizeof(fr_msg
), &md5
);
477 md5_end(auth_o
->key
, &md5
);
479 for (size_t i
= 0; i
< sizeof(secretbytes
); ++i
) {
480 secretbytes
[i
] ^= 0x36;
481 secretbytes
[i
] ^= 0x5c;
485 md5_hash(secretbytes
, sizeof(secretbytes
), &md5
);
486 md5_hash(auth_o
->key
, sizeof(auth_o
->key
), &md5
);
487 md5_end(auth_o
->key
, &md5
);
489 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_SERVERID
, 4,
490 &a
->fr_ip
->addr
.addr
.in
.s_addr
);
491 dhcpv4_put(&fr_msg
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
494 struct sockaddr_in dest
;
495 memset(&dest
, 0, sizeof(dest
));
496 dest
.sin_family
= AF_INET
;
497 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
498 dest
.sin_addr
.s_addr
= a
->addr
;
500 syslog(LOG_WARNING
, "sending %s to %02x:%02x:%02x:%02x:%02x:%02x - %s",
501 dhcpv4_msg_to_string(msg
),
502 a
->hwaddr
[0], a
->hwaddr
[1], a
->hwaddr
[2],
503 a
->hwaddr
[3], a
->hwaddr
[4], a
->hwaddr
[5],
504 inet_ntoa(dest
.sin_addr
));
506 sendto(iface
->dhcpv4_event
.uloop
.fd
, &fr_msg
, sizeof(fr_msg
),
507 MSG_DONTWAIT
, (struct sockaddr
*)&dest
, sizeof(dest
));
510 static void dhcpv4_fr_timer(struct uloop_timeout
*event
)
512 struct dhcpv4_assignment
*a
= container_of(event
, struct dhcpv4_assignment
, fr_timer
);
514 if (a
->fr_cnt
> 0 && a
->fr_cnt
< 8) {
516 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
522 static void dhcpv4_fr_start(struct dhcpv4_assignment
*a
)
524 uloop_timeout_set(&a
->fr_timer
, 1000 << a
->fr_cnt
);
525 a
->fr_timer
.cb
= dhcpv4_fr_timer
;
531 static void dhcpv4_fr_stop(struct dhcpv4_assignment
*a
)
533 uloop_timeout_cancel(&a
->fr_timer
);
534 decr_ref_cnt_ip(&a
->fr_ip
, a
->iface
);
536 a
->fr_timer
.cb
= NULL
;
539 // Handler for DHCPv4 messages
540 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
541 struct interface
*iface
, _unused
void *dest_addr
)
546 struct dhcpv4_message
*req
= data
;
547 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
548 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
552 syslog(LOG_NOTICE
, "Got DHCPv4 request");
554 if (!iface
->dhcpv4_start_ip
.s_addr
&& !iface
->dhcpv4_end_ip
.s_addr
) {
555 syslog(LOG_WARNING
, "No DHCP range available on %s", iface
->name
);
559 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
561 struct dhcpv4_message reply
= {
562 .op
= DHCPV4_BOOTREPLY
,
569 .ciaddr
= {INADDR_ANY
},
570 .giaddr
= req
->giaddr
,
571 .siaddr
= iface
->dhcpv4_local
,
573 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
575 reply
.options
[0] = 0x63;
576 reply
.options
[1] = 0x82;
577 reply
.options
[2] = 0x53;
578 reply
.options
[3] = 0x63;
580 uint8_t *cookie
= &reply
.options
[4];
581 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
582 uint8_t msg
= DHCPV4_MSG_ACK
;
584 struct in_addr reqaddr
= {INADDR_ANY
};
585 uint32_t leasetime
= 0;
586 size_t hostname_len
= 0;
588 bool accept_fr_nonce
= false;
589 bool incl_fr_opt
= false;
591 uint8_t *start
= &req
->options
[4];
592 uint8_t *end
= ((uint8_t*)data
) + len
;
593 struct dhcpv4_option
*opt
;
594 dhcpv4_for_each_option(start
, end
, opt
) {
595 if (opt
->type
== DHCPV4_OPT_MESSAGE
&& opt
->len
== 1)
596 reqmsg
= opt
->data
[0];
597 else if (opt
->type
== DHCPV4_OPT_HOSTNAME
&& opt
->len
> 0) {
598 hostname_len
= opt
->len
;
599 memcpy(hostname
, opt
->data
, hostname_len
);
600 hostname
[hostname_len
] = 0;
601 } else if (opt
->type
== DHCPV4_OPT_IPADDRESS
&& opt
->len
== 4)
602 memcpy(&reqaddr
, opt
->data
, 4);
603 else if (opt
->type
== DHCPV4_OPT_SERVERID
&& opt
->len
== 4) {
604 if (memcmp(opt
->data
, &iface
->dhcpv4_local
, 4))
606 } else if (iface
->filter_class
&& opt
->type
== DHCPV4_OPT_USER_CLASS
) {
607 uint8_t *c
= opt
->data
, *cend
= &opt
->data
[opt
->len
];
608 for (; c
< cend
&& &c
[*c
] < cend
; c
= &c
[1 + *c
]) {
609 size_t elen
= strlen(iface
->filter_class
);
610 if (*c
== elen
&& !memcmp(&c
[1], iface
->filter_class
, elen
))
611 return; // Ignore from homenet
613 } else if (opt
->type
== DHCPV4_OPT_LEASETIME
&& opt
->len
== 4)
614 memcpy(&leasetime
, opt
->data
, 4);
615 else if (opt
->type
== DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
&& opt
->len
> 0) {
616 for (uint8_t i
= 0; i
< opt
->len
; i
++) {
617 if (opt
->data
[i
] == 1) {
618 accept_fr_nonce
= true;
626 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
627 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
628 reqmsg
!= DHCPV4_MSG_RELEASE
)
631 struct dhcpv4_assignment
*lease
= NULL
;
632 uint32_t serverid
= iface
->dhcpv4_local
.s_addr
;
633 uint32_t fr_serverid
= INADDR_ANY
;
635 if (reqmsg
!= DHCPV4_MSG_INFORM
)
636 lease
= dhcpv4_lease(iface
, reqmsg
, req
->chaddr
, reqaddr
,
637 &leasetime
, hostname
, hostname_len
,
638 accept_fr_nonce
, &incl_fr_opt
, &fr_serverid
);
641 if (reqmsg
== DHCPV4_MSG_REQUEST
)
642 msg
= DHCPV4_MSG_NAK
;
643 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
645 } else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
646 msg
= DHCPV4_MSG_OFFER
;
647 else if (reqmsg
== DHCPV4_MSG_REQUEST
&&
648 ((reqaddr
.s_addr
&& reqaddr
.s_addr
!= lease
->addr
) ||
649 (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= lease
->addr
))) {
650 msg
= DHCPV4_MSG_NAK
;
652 * DHCP client requested an IP which we can't offer to him. Probably the
653 * client changed the network or the network has been changed. The reply
654 * type is set to DHCPV4_MSG_NAK, because the client should not use that IP.
656 * For modern devices we build an answer that includes a valid IP, like
657 * a DHCPV4_MSG_ACK. The client will use that IP and doesn't need to
658 * perform additional DHCP round trips.
664 * Buggy clients do serverid checking in nack messages; therefore set the
665 * serverid in nack messages triggered by a previous force renew equal to
666 * the server id in use at that time by the server
670 serverid
= fr_serverid
;
672 if (req
->ciaddr
.s_addr
&&
673 ((iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
) !=
674 (req
->ciaddr
.s_addr
& iface
->dhcpv4_mask
.s_addr
)))
675 req
->ciaddr
.s_addr
= INADDR_ANY
;
678 syslog(LOG_WARNING
, "received %s from %02x:%02x:%02x:%02x:%02x:%02x",
679 dhcpv4_msg_to_string(reqmsg
),
680 req
->chaddr
[0],req
->chaddr
[1],req
->chaddr
[2],
681 req
->chaddr
[3],req
->chaddr
[4],req
->chaddr
[5]);
683 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
686 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
687 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &serverid
);
692 reply
.yiaddr
.s_addr
= lease
->addr
;
694 val
= htonl(leasetime
);
695 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
697 if (leasetime
!= UINT32_MAX
) {
698 val
= htonl(500 * leasetime
/ 1000);
699 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
701 val
= htonl(875 * leasetime
/ 1000);
702 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
705 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4,
706 &iface
->dhcpv4_mask
.s_addr
);
709 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
710 strlen(lease
->hostname
), lease
->hostname
);
712 if (iface
->dhcpv4_bcast
.s_addr
!= INADDR_ANY
)
713 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &iface
->dhcpv4_bcast
);
716 if (reqmsg
== DHCPV4_MSG_REQUEST
) {
717 struct dhcpv4_auth_forcerenew auth
= {
721 .replay
= {htonl(time(NULL
)), htonl(++serial
)},
726 memcpy(auth
.key
, lease
->key
, sizeof(auth
.key
));
727 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_AUTHENTICATION
, sizeof(auth
), &auth
);
730 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_FORCERENEW_NONCE_CAPABLE
,
736 struct ifreq ifr
= {.ifr_name
= ""};
737 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
));
739 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
740 uint16_t mtu
= htons(ifr
.ifr_mtu
);
741 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
744 if (iface
->search
&& iface
->search_len
<= 255)
745 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
746 iface
->search_len
, iface
->search
);
747 else if (!res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
748 uint8_t search_buf
[256];
749 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
750 sizeof(search_buf
), NULL
, NULL
);
752 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SEARCH_DOMAIN
,
756 if (iface
->dhcpv4_router_cnt
== 0)
757 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &iface
->dhcpv4_local
);
759 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
,
760 4 * iface
->dhcpv4_router_cnt
, iface
->dhcpv4_router
);
763 if (iface
->dhcpv4_dns_cnt
== 0)
764 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &iface
->dhcpv4_local
);
766 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
767 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
770 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
772 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
773 if (req
->giaddr
.s_addr
) {
775 * relay agent is configured, send reply to the agent
777 dest
.sin_addr
= req
->giaddr
;
778 dest
.sin_port
= htons(DHCPV4_SERVER_PORT
);
779 } else if (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= dest
.sin_addr
.s_addr
) {
781 * client has existing configuration (ciaddr is set) AND this address is
782 * not the address it used for the dhcp message
784 dest
.sin_addr
= req
->ciaddr
;
785 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
786 } else if ((ntohs(req
->flags
) & DHCPV4_FLAG_BROADCAST
) ||
787 req
->hlen
!= reply
.hlen
|| !reply
.yiaddr
.s_addr
) {
789 * client requests a broadcast reply OR we can't offer an IP
791 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
792 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
793 } else if (!req
->ciaddr
.s_addr
&& msg
== DHCPV4_MSG_NAK
) {
795 * client has no previous configuration -> no IP, so we need to reply
796 * with a broadcast packet
798 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
799 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
802 * send reply to the newly (in this proccess) allocated IP
804 dest
.sin_addr
= reply
.yiaddr
;
805 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
807 struct arpreq arp
= {.arp_flags
= ATF_COM
};
808 memcpy(arp
.arp_ha
.sa_data
, req
->chaddr
, 6);
809 memcpy(&arp
.arp_pa
, &dest
, sizeof(arp
.arp_pa
));
810 memcpy(arp
.arp_dev
, iface
->ifname
, sizeof(arp
.arp_dev
));
811 ioctl(sock
, SIOCSARP
, &arp
);
814 if (dest
.sin_addr
.s_addr
== INADDR_BROADCAST
)
815 /* reply goes to IP broadcast -> MAC broadcast */
816 syslog(LOG_WARNING
, "sending %s to ff:ff:ff:ff:ff:ff - %s",
817 dhcpv4_msg_to_string(msg
),
818 inet_ntoa(dest
.sin_addr
));
821 * reply is send directly to IP,
822 * MAC is assumed to be the same as the request
824 syslog(LOG_WARNING
, "sending %s to %02x:%02x:%02x:%02x:%02x:%02x - %s",
825 dhcpv4_msg_to_string(msg
),
826 req
->chaddr
[0],req
->chaddr
[1],req
->chaddr
[2],
827 req
->chaddr
[3],req
->chaddr
[4],req
->chaddr
[5],
828 inet_ntoa(dest
.sin_addr
));
830 sendto(sock
, &reply
, sizeof(reply
), MSG_DONTWAIT
,
831 (struct sockaddr
*)&dest
, sizeof(dest
));
834 static bool dhcpv4_test(struct interface
*iface
, uint32_t try)
836 struct dhcpv4_assignment
*c
;
837 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
845 static bool dhcpv4_assign(struct interface
*iface
,
846 struct dhcpv4_assignment
*assign
, uint32_t raddr
)
848 uint32_t start
= ntohl(iface
->dhcpv4_start_ip
.s_addr
);
849 uint32_t end
= ntohl(iface
->dhcpv4_end_ip
.s_addr
);
850 uint32_t count
= end
- start
+ 1;
852 // try to assign the IP the client asked for
853 if (start
<= ntohl(raddr
) && ntohl(raddr
) <= end
&& dhcpv4_test(iface
, raddr
)) {
854 assign
->addr
= raddr
;
855 syslog(LOG_INFO
, "assigning the IP the client asked for: %u.%u.%u.%u",
856 ((uint8_t *)&assign
->addr
)[0],
857 ((uint8_t *)&assign
->addr
)[1],
858 ((uint8_t *)&assign
->addr
)[2],
859 ((uint8_t *)&assign
->addr
)[3]);
863 // Seed RNG with checksum of hwaddress
865 for (size_t i
= 0; i
< sizeof(assign
->hwaddr
); ++i
) {
866 // Knuth's multiplicative method
867 uint8_t o
= assign
->hwaddr
[i
];
868 seed
+= (o
*2654435761) % UINT32_MAX
;
873 uint32_t try = (((uint32_t)rand()) % count
) + start
;
875 if (list_empty(&iface
->dhcpv4_assignments
)) {
876 assign
->addr
= htonl(try);
877 syslog(LOG_INFO
, "assigning mapped IP (empty list): %u.%u.%u.%u",
878 ((uint8_t *)&assign
->addr
)[0],
879 ((uint8_t *)&assign
->addr
)[1],
880 ((uint8_t *)&assign
->addr
)[2],
881 ((uint8_t *)&assign
->addr
)[3]);
885 for (uint32_t i
= 0; i
< count
; ++i
) {
886 if (dhcpv4_test(iface
, htonl(try))) {
887 /* test was successful: IP address is not assigned, assign it */
888 assign
->addr
= htonl(try);
889 syslog(LOG_DEBUG
, "assigning mapped IP: %u.%u.%u.%u (try %u of %u)",
890 ((uint8_t *)&assign
->addr
)[0],
891 ((uint8_t *)&assign
->addr
)[1],
892 ((uint8_t *)&assign
->addr
)[2],
893 ((uint8_t *)&assign
->addr
)[3],
897 try = (((try - start
) + 1) % count
) + start
;
900 syslog(LOG_WARNING
, "can't assign any IP address -> address space is full");
905 static struct dhcpv4_assignment
* dhcpv4_lease(struct interface
*iface
,
906 enum dhcpv4_msg msg
, const uint8_t *mac
, struct in_addr reqaddr
,
907 uint32_t *leasetime
, const char *hostname
, const size_t hostname_len
,
908 const bool accept_fr_nonce
, bool *incl_fr_opt
, uint32_t *fr_serverid
)
910 struct dhcpv4_assignment
*lease
= NULL
;
911 time_t now
= odhcpd_time();
913 struct dhcpv4_assignment
*c
, *n
, *a
= NULL
;
914 list_for_each_entry_safe(c
, n
, &iface
->dhcpv4_assignments
, head
) {
915 if (!memcmp(c
->hwaddr
, mac
, 6)) {
917 if (c
->addr
== reqaddr
.s_addr
)
919 } else if (!INFINITE_VALID(c
->valid_until
) && c
->valid_until
< now
)
920 free_dhcpv4_assignment(c
);
923 if (a
&& (a
->flags
& OAF_BOUND
) && a
->fr_ip
) {
924 *fr_serverid
= a
->fr_ip
->addr
.addr
.in
.s_addr
;
928 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
932 if (!iface
->no_dynamic_dhcp
) {
933 /* Create new binding */
934 a
= calloc(1, sizeof(*a
));
936 syslog(LOG_ERR
, "Failed to calloc binding on interface %s",
940 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
941 /* Don't consider new assignment as infinite */
942 a
->valid_until
= now
;
944 assigned
= dhcpv4_assign(iface
, a
, reqaddr
.s_addr
);
947 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
950 } else if ((a
->addr
& iface
->dhcpv4_mask
.s_addr
) !=
951 (iface
->dhcpv4_start_ip
.s_addr
& iface
->dhcpv4_mask
.s_addr
)) {
954 assigned
= dhcpv4_assign(iface
, a
, reqaddr
.s_addr
);
956 list_add(&a
->head
, &iface
->dhcpv4_assignments
);
960 uint32_t my_leasetime
;
963 my_leasetime
= a
->leasetime
;
965 my_leasetime
= iface
->dhcpv4_leasetime
;
967 if ((*leasetime
== 0) || (my_leasetime
< *leasetime
))
968 *leasetime
= my_leasetime
;
970 if (msg
== DHCPV4_MSG_DISCOVER
) {
971 a
->flags
&= ~OAF_BOUND
;
973 *incl_fr_opt
= accept_fr_nonce
;
974 if (!(a
->flags
& OAF_STATIC
))
975 a
->valid_until
= now
;
977 if (hostname_len
> 0) {
978 a
->hostname
= realloc(a
->hostname
, hostname_len
+ 1);
980 memcpy(a
->hostname
, hostname
, hostname_len
);
981 a
->hostname
[hostname_len
] = 0;
985 if (!(a
->flags
& OAF_BOUND
)) {
986 a
->accept_fr_nonce
= accept_fr_nonce
;
987 *incl_fr_opt
= accept_fr_nonce
;
988 odhcpd_urandom(a
->key
, sizeof(a
->key
));
989 a
->flags
|= OAF_BOUND
;
991 *incl_fr_opt
= false;
993 if (!(a
->flags
& OAF_STATIC
))
994 a
->valid_until
= ((*leasetime
== UINT32_MAX
) ? 0 : (time_t)(now
+ *leasetime
));
996 } else if (!assigned
&& a
) {
997 /* Cleanup failed assignment */
998 free_dhcpv4_assignment(a
);
1004 } else if (msg
== DHCPV4_MSG_RELEASE
&& a
) {
1005 a
->flags
&= ~OAF_BOUND
;
1007 if (!(a
->flags
& OAF_STATIC
))
1008 a
->valid_until
= now
- 1;
1010 } else if (msg
== DHCPV4_MSG_DECLINE
&& a
) {
1011 a
->flags
&= ~OAF_BOUND
;
1013 if (!(a
->flags
& OAF_STATIC
)) {
1014 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
1015 a
->valid_until
= now
+ 3600; /* Block address for 1h */
1019 dhcpv6_write_statefile();