2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
24 #include <net/if_arp.h>
25 #include <netinet/ip.h>
26 #include <sys/ioctl.h>
27 #include <sys/timerfd.h>
28 #include <arpa/inet.h>
35 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
36 struct interface
*iface
);
37 static struct dhcpv4_assignment
* dhcpv4_lease(struct interface
*iface
,
38 enum dhcpv4_msg msg
, const uint8_t *mac
, struct in_addr reqaddr
,
39 const char *hostname
);
41 // Create socket and register events
48 int setup_dhcpv4_interface(struct interface
*iface
, bool enable
)
50 if (iface
->dhcpv4_event
.uloop
.fd
> 0) {
51 uloop_fd_delete(&iface
->dhcpv4_event
.uloop
);
52 close(iface
->dhcpv4_event
.uloop
.fd
);
53 iface
->dhcpv4_event
.uloop
.fd
= -1;
56 if (iface
->dhcpv4
&& enable
) {
57 if (!iface
->dhcpv4_assignments
.next
)
58 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
60 int sock
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, IPPROTO_UDP
);
62 syslog(LOG_ERR
, "Failed to create DHCPv4 server socket: %s",
67 // Basic IPv6 configuration
69 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
70 setsockopt(sock
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
));
71 setsockopt(sock
, IPPROTO_IP
, IP_PKTINFO
, &val
, sizeof(val
));
73 val
= IPTOS_PREC_INTERNETCONTROL
;
74 setsockopt(sock
, IPPROTO_IP
, IP_TOS
, &val
, sizeof(val
));
76 val
= IP_PMTUDISC_DONT
;
77 setsockopt(sock
, IPPROTO_IP
, IP_MTU_DISCOVER
, &val
, sizeof(val
));
79 setsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
,
80 iface
->ifname
, strlen(iface
->ifname
));
82 struct sockaddr_in bind_addr
= {AF_INET
, htons(DHCPV4_SERVER_PORT
),
85 if (bind(sock
, (struct sockaddr
*)&bind_addr
, sizeof(bind_addr
))) {
86 syslog(LOG_ERR
, "Failed to open DHCPv4 server socket: %s",
92 if (ntohl(iface
->dhcpv4_start
.s_addr
) > ntohl(iface
->dhcpv4_end
.s_addr
)) {
93 syslog(LOG_ERR
, "Invalid DHCP range");
97 // Create a range if not specified
99 strncpy(ifreq
.ifr_name
, iface
->ifname
, sizeof(ifreq
.ifr_name
));
101 struct sockaddr_in
*saddr
= (struct sockaddr_in
*)&ifreq
.ifr_addr
;
102 struct sockaddr_in
*smask
= (struct sockaddr_in
*)&ifreq
.ifr_netmask
;
103 if (!(iface
->dhcpv4_start
.s_addr
& htonl(0xffff0000)) &&
104 !(iface
->dhcpv4_end
.s_addr
& htonl(0xffff0000)) &&
105 !ioctl(sock
, SIOCGIFADDR
, &ifreq
)) {
106 struct in_addr addr
= saddr
->sin_addr
;
108 ioctl(sock
, SIOCGIFNETMASK
, &ifreq
);
109 struct in_addr mask
= smask
->sin_addr
;
111 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
112 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
114 if (start
&& end
&& start
< end
&&
115 start
> ntohl(addr
.s_addr
& ~mask
.s_addr
) &&
116 (start
& ntohl(mask
.s_addr
)) == start
&&
117 (end
& ntohl(mask
.s_addr
)) == end
) {
118 iface
->dhcpv4_start
.s_addr
= htonl(start
) |
119 (addr
.s_addr
& mask
.s_addr
);
120 iface
->dhcpv4_end
.s_addr
= htonl(end
) |
121 (addr
.s_addr
& mask
.s_addr
);
122 } else if (ntohl(mask
.s_addr
) <= 0xfffffff0) {
123 start
= addr
.s_addr
& mask
.s_addr
;
124 end
= addr
.s_addr
& mask
.s_addr
;
126 if (ntohl(mask
.s_addr
) <= 0xffffff00) {
127 iface
->dhcpv4_start
.s_addr
= start
| htonl(100);
128 iface
->dhcpv4_end
.s_addr
= end
| htonl(250);
129 } else if (ntohl(mask
.s_addr
) <= 0xffffffc0) {
130 iface
->dhcpv4_start
.s_addr
= start
| htonl(10);
131 iface
->dhcpv4_end
.s_addr
= end
| htonl(60);
132 } else if (ntohl(mask
.s_addr
) <= 0xffffffe0) {
133 iface
->dhcpv4_start
.s_addr
= start
| htonl(10);
134 iface
->dhcpv4_end
.s_addr
= end
| htonl(30);
136 iface
->dhcpv4_start
.s_addr
= start
| htonl(3);
137 iface
->dhcpv4_end
.s_addr
= end
| htonl(12);
144 // Parse static entries
146 list_for_each_entry(lease
, &leases
, head
) {
148 size_t hostlen
= strlen(lease
->hostname
) + 1;
149 struct dhcpv4_assignment
*a
= calloc(1, sizeof(*a
) + hostlen
);
151 syslog(LOG_ERR
, "Calloc failed for static lease on interface %s",
155 a
->addr
= ntohl(lease
->ipaddr
.s_addr
);
156 memcpy(a
->hwaddr
, lease
->mac
.ether_addr_octet
, sizeof(a
->hwaddr
));
157 memcpy(a
->hostname
, lease
->hostname
, hostlen
);
159 // Assign to all interfaces
160 struct dhcpv4_assignment
*c
;
161 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
162 if (c
->addr
> a
->addr
) {
163 list_add_tail(&a
->head
, &c
->head
);
164 } else if (c
->addr
== a
->addr
) {
165 // Already an assignment with that number
174 // Clean invalid assignments
175 struct dhcpv4_assignment
*a
, *n
;
176 list_for_each_entry_safe(a
, n
, &iface
->dhcpv4_assignments
, head
) {
177 if ((htonl(a
->addr
) & smask
->sin_addr
.s_addr
) !=
178 (saddr
->sin_addr
.s_addr
& smask
->sin_addr
.s_addr
)) {
185 if (iface
->dhcpv4_leasetime
< 60)
186 iface
->dhcpv4_leasetime
= 43200;
188 iface
->dhcpv4_event
.uloop
.fd
= sock
;
189 iface
->dhcpv4_event
.handle_dgram
= handle_dhcpv4
;
190 odhcpd_register(&iface
->dhcpv4_event
);
191 } else if (iface
->dhcpv4_assignments
.next
) {
192 while (!list_empty(&iface
->dhcpv4_assignments
)) {
193 struct dhcpv4_assignment
*a
= list_first_entry(&iface
->dhcpv4_assignments
,
194 struct dhcpv4_assignment
, head
);
205 static void dhcpv4_put(struct dhcpv4_message
*msg
, uint8_t **cookie
,
206 uint8_t type
, uint8_t len
, const void *data
)
208 uint8_t *c
= *cookie
;
209 if (*cookie
+ 2 + len
> (uint8_t*)&msg
[1])
214 memcpy(c
, data
, len
);
220 // Simple DHCPv6-server for information requests
221 static void handle_dhcpv4(void *addr
, void *data
, size_t len
,
222 struct interface
*iface
)
227 struct dhcpv4_message
*req
= data
;
228 if (len
< offsetof(struct dhcpv4_message
, options
) + 4 ||
229 req
->op
!= DHCPV4_BOOTREQUEST
|| req
->hlen
!= 6)
232 int sock
= iface
->dhcpv4_event
.uloop
.fd
;
233 struct sockaddr_in ifaddr
;
234 struct sockaddr_in ifnetmask
;
236 syslog(LOG_NOTICE
, "Got DHCPv4 request");
239 memcpy(ifreq
.ifr_name
, iface
->ifname
, sizeof(ifreq
.ifr_name
));
240 if (ioctl(sock
, SIOCGIFADDR
, &ifreq
)) {
241 syslog(LOG_WARNING
, "DHCPv4 failed to detect address: %s", strerror(errno
));
245 memcpy(&ifaddr
, &ifreq
.ifr_addr
, sizeof(ifaddr
));
246 if (ioctl(sock
, SIOCGIFNETMASK
, &ifreq
))
249 memcpy(&ifnetmask
, &ifreq
.ifr_netmask
, sizeof(ifnetmask
));
250 uint32_t network
= ifaddr
.sin_addr
.s_addr
& ifnetmask
.sin_addr
.s_addr
;
252 if ((iface
->dhcpv4_start
.s_addr
& ifnetmask
.sin_addr
.s_addr
) != network
||
253 (iface
->dhcpv4_end
.s_addr
& ifnetmask
.sin_addr
.s_addr
) != network
) {
254 syslog(LOG_WARNING
, "DHCPv4 range out of assigned network");
258 struct ifreq ifr
= {.ifr_name
= ""};
259 strncpy(ifr
.ifr_name
, iface
->ifname
, sizeof(ifr
.ifr_name
));
261 struct dhcpv4_message reply
= {
262 .op
= DHCPV4_BOOTREPLY
,
269 .ciaddr
= {INADDR_ANY
},
270 .giaddr
= req
->giaddr
,
271 .siaddr
= ifaddr
.sin_addr
,
273 memcpy(reply
.chaddr
, req
->chaddr
, sizeof(reply
.chaddr
));
275 reply
.options
[0] = 0x63;
276 reply
.options
[1] = 0x82;
277 reply
.options
[2] = 0x53;
278 reply
.options
[3] = 0x63;
280 uint8_t *cookie
= &reply
.options
[4];
281 uint8_t reqmsg
= DHCPV4_MSG_REQUEST
;
282 uint8_t msg
= DHCPV4_MSG_ACK
;
284 struct in_addr reqaddr
= {INADDR_ANY
};
288 uint8_t *start
= &req
->options
[4];
289 uint8_t *end
= ((uint8_t*)data
) + len
;
290 struct dhcpv4_option
*opt
;
291 dhcpv4_for_each_option(start
, end
, opt
) {
292 if (opt
->type
== DHCPV4_OPT_MESSAGE
&& opt
->len
== 1) {
293 reqmsg
= opt
->data
[0];
294 } else if (opt
->type
== DHCPV4_OPT_HOSTNAME
&& opt
->len
> 0) {
295 memcpy(hostname
, opt
->data
, opt
->len
);
296 hostname
[opt
->len
] = 0;
297 } else if (opt
->type
== DHCPV4_OPT_IPADDRESS
&& opt
->len
== 4) {
298 memcpy(&reqaddr
, opt
->data
, 4);
299 } else if (opt
->type
== DHCPV4_OPT_SERVERID
&& opt
->len
== 4) {
300 if (memcmp(opt
->data
, &ifaddr
.sin_addr
, 4))
302 } else if (iface
->filter_class
&& opt
->type
== DHCPV4_OPT_USER_CLASS
) {
303 uint8_t *c
= opt
->data
, *cend
= &opt
->data
[opt
->len
];
304 for (; c
< cend
&& &c
[*c
] < cend
; c
= &c
[1 + *c
]) {
305 size_t elen
= strlen(iface
->filter_class
);
306 if (*c
== elen
&& !memcmp(&c
[1], iface
->filter_class
, elen
))
307 return; // Ignore from homenet
312 if (reqmsg
!= DHCPV4_MSG_DISCOVER
&& reqmsg
!= DHCPV4_MSG_REQUEST
&&
313 reqmsg
!= DHCPV4_MSG_INFORM
&& reqmsg
!= DHCPV4_MSG_DECLINE
&&
314 reqmsg
!= DHCPV4_MSG_RELEASE
)
317 struct dhcpv4_assignment
*lease
= NULL
;
318 if (reqmsg
!= DHCPV4_MSG_INFORM
)
319 lease
= dhcpv4_lease(iface
, reqmsg
, req
->chaddr
, reqaddr
, hostname
);
322 if (reqmsg
== DHCPV4_MSG_REQUEST
)
323 msg
= DHCPV4_MSG_NAK
;
324 else if (reqmsg
== DHCPV4_MSG_DISCOVER
)
326 } else if (reqmsg
== DHCPV4_MSG_DISCOVER
) {
327 msg
= DHCPV4_MSG_OFFER
;
330 if (reqmsg
== DHCPV4_MSG_DECLINE
|| reqmsg
== DHCPV4_MSG_RELEASE
)
333 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MESSAGE
, 1, &msg
);
334 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_SERVERID
, 4, &ifaddr
.sin_addr
);
337 reply
.yiaddr
.s_addr
= htonl(lease
->addr
);
339 uint32_t val
= htonl(iface
->dhcpv4_leasetime
);
340 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_LEASETIME
, 4, &val
);
342 val
= htonl(500 * iface
->dhcpv4_leasetime
/ 1000);
343 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_RENEW
, 4, &val
);
345 val
= htonl(875 * iface
->dhcpv4_leasetime
/ 1000);
346 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_REBIND
, 4, &val
);
348 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_NETMASK
, 4, &ifnetmask
.sin_addr
);
350 if (lease
->hostname
[0])
351 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_HOSTNAME
,
352 strlen(lease
->hostname
), lease
->hostname
);
354 if (!ioctl(sock
, SIOCGIFBRDADDR
, &ifr
)) {
355 struct sockaddr_in
*ina
= (struct sockaddr_in
*)&ifr
.ifr_broadaddr
;
356 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_BROADCAST
, 4, &ina
->sin_addr
);
360 if (!ioctl(sock
, SIOCGIFMTU
, &ifr
)) {
361 uint16_t mtu
= htons(ifr
.ifr_mtu
);
362 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_MTU
, 2, &mtu
);
367 if (dn_expand(iface
->search
, iface
->search
+ iface
->search_len
,
368 iface
->search
, b
, sizeof(b
)) > 0)
369 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DOMAIN
, strlen(b
), b
);
370 } else if (!res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
371 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DOMAIN
,
372 strlen(_res
.dnsrch
[0]), _res
.dnsrch
[0]);
375 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_ROUTER
, 4, &ifaddr
.sin_addr
);
379 if (iface
->dhcpv4_dns_cnt
== 0)
380 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
, 4, &ifaddr
.sin_addr
);
382 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_DNSSERVER
,
383 4 * iface
->dhcpv4_dns_cnt
, iface
->dhcpv4_dns
);
386 dhcpv4_put(&reply
, &cookie
, DHCPV4_OPT_END
, 0, NULL
);
388 struct sockaddr_in dest
= *((struct sockaddr_in
*)addr
);
389 if (req
->giaddr
.s_addr
) {
390 dest
.sin_addr
= req
->giaddr
;
391 dest
.sin_port
= htons(DHCPV4_SERVER_PORT
);
392 } else if (req
->ciaddr
.s_addr
&& req
->ciaddr
.s_addr
!= dest
.sin_addr
.s_addr
) {
393 dest
.sin_addr
= req
->ciaddr
;
394 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
395 } else if ((ntohs(req
->flags
) & DHCPV4_FLAG_BROADCAST
) ||
396 req
->hlen
!= reply
.hlen
) {
397 dest
.sin_addr
.s_addr
= INADDR_BROADCAST
;
398 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
400 dest
.sin_addr
= reply
.yiaddr
;
401 dest
.sin_port
= htons(DHCPV4_CLIENT_PORT
);
403 struct arpreq arp
= {.arp_flags
= ATF_COM
};
404 memcpy(arp
.arp_ha
.sa_data
, req
->chaddr
, 6);
405 memcpy(&arp
.arp_pa
, &dest
, sizeof(arp
.arp_pa
));
406 memcpy(arp
.arp_dev
, iface
->ifname
, sizeof(arp
.arp_dev
));
407 ioctl(sock
, SIOCSARP
, &arp
);
410 sendto(sock
, &reply
, sizeof(reply
), MSG_DONTWAIT
,
411 (struct sockaddr
*)&dest
, sizeof(dest
));
415 static bool dhcpv4_assign(struct interface
*iface
,
416 struct dhcpv4_assignment
*assign
, uint32_t raddr
)
418 const unsigned tries
= 10;
419 uint32_t start
= ntohl(iface
->dhcpv4_start
.s_addr
);
420 uint32_t end
= ntohl(iface
->dhcpv4_end
.s_addr
);
421 uint32_t count
= end
- start
+ 1;
423 // Seed RNG with checksum of DUID
425 for (size_t i
= 0; i
< sizeof(assign
->hwaddr
); ++i
)
426 seed
+= assign
->hwaddr
[i
];
429 // Try to assign up to 100x
430 for (unsigned i
= 0; i
< tries
; ++i
) {
431 uint32_t try = (((uint32_t)rand()) % count
) + start
;
432 if (i
== 0 && raddr
>= start
&& raddr
<= end
)
434 else if (i
== tries
- 1)
437 if (list_empty(&iface
->dhcpv4_assignments
)) {
439 list_add(&assign
->head
, &iface
->dhcpv4_assignments
);
443 struct dhcpv4_assignment
*c
;
444 list_for_each_entry(c
, &iface
->dhcpv4_assignments
, head
) {
447 list_add_tail(&assign
->head
, &c
->head
);
449 } else if (c
->addr
== try) {
462 static struct dhcpv4_assignment
* dhcpv4_lease(struct interface
*iface
,
463 enum dhcpv4_msg msg
, const uint8_t *mac
, struct in_addr reqaddr
,
464 const char *hostname
)
466 struct dhcpv4_assignment
*lease
= NULL
;
467 uint32_t raddr
= ntohl(reqaddr
.s_addr
);
468 time_t now
= odhcpd_time();
470 struct dhcpv4_assignment
*c
, *n
, *a
= NULL
;
471 list_for_each_entry_safe(c
, n
, &iface
->dhcpv4_assignments
, head
) {
472 if (c
->addr
== raddr
&& !memcmp(c
->hwaddr
, mac
, 6)) {
475 } else if (c
->valid_until
< now
) {
481 bool update_state
= false;
482 if (msg
== DHCPV4_MSG_DISCOVER
|| msg
== DHCPV4_MSG_REQUEST
) {
484 size_t hostlen
= strlen(hostname
) + 1;
486 if (!a
&& !iface
->no_dynamic_dhcp
) { // Create new binding
487 a
= calloc(1, sizeof(*a
) + hostlen
);
489 syslog(LOG_ERR
, "Failed to calloc binding on interface %s", iface
->ifname
);
492 memcpy(a
->hwaddr
, mac
, sizeof(a
->hwaddr
));
493 memcpy(a
->hostname
, hostname
, hostlen
);
495 assigned
= dhcpv4_assign(iface
, a
, raddr
);
498 if (assigned
&& !a
->hostname
[0] && hostname
) {
499 a
= realloc(a
, sizeof(*a
) + hostlen
);
501 syslog(LOG_ERR
, "Failed to realloc binding on interface %s", iface
->ifname
);
504 memcpy(a
->hostname
, hostname
, hostlen
);
507 a
->head
.next
->prev
= &a
->head
;
508 a
->head
.prev
->next
= &a
->head
;
511 // Was only a solicitation: mark binding for removal
512 if (assigned
&& a
->valid_until
< now
) {
513 a
->valid_until
= (msg
== DHCPV4_MSG_DISCOVER
) ? 0 :
514 (now
+ iface
->dhcpv4_leasetime
);
515 } else if (!assigned
&& a
) { // Cleanup failed assignment
522 } else if (msg
== DHCPV4_MSG_RELEASE
) {
527 } else if (msg
== DHCPV4_MSG_DECLINE
) {
528 memset(a
->hwaddr
, 0, sizeof(a
->hwaddr
));
529 a
->valid_until
= now
+ 3600; // Block address for 1h
534 dhcpv6_write_statefile();