2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 03:20:50 +00:00
|
|
|
/*
|
|
|
|
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
2008-12-07 00:52:58 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2006-05-08 03:20:50 +00:00
|
|
|
*/
|
|
|
|
#include "common.h"
|
2006-11-18 19:51:32 +00:00
|
|
|
#include "dhcpd.h"
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-04-07 01:05:47 +00:00
|
|
|
/* Find the oldest expired lease, NULL if there are no expired leases */
|
2009-06-17 09:57:09 +00:00
|
|
|
static struct dyn_lease *oldest_expired_lease(void)
|
2007-04-07 01:05:47 +00:00
|
|
|
{
|
2009-06-17 09:57:09 +00:00
|
|
|
struct dyn_lease *oldest_lease = NULL;
|
2009-01-01 17:52:09 +00:00
|
|
|
leasetime_t oldest_time = time(NULL);
|
2007-04-07 01:05:47 +00:00
|
|
|
unsigned i;
|
|
|
|
|
2009-06-17 11:24:03 +00:00
|
|
|
/* Unexpired leases have g_leases[i].expires >= current time
|
2009-01-01 17:52:09 +00:00
|
|
|
* and therefore can't ever match */
|
|
|
|
for (i = 0; i < server_config.max_leases; i++) {
|
2009-06-17 11:24:03 +00:00
|
|
|
if (g_leases[i].expires < oldest_time) {
|
|
|
|
oldest_time = g_leases[i].expires;
|
|
|
|
oldest_lease = &g_leases[i];
|
2007-04-07 01:05:47 +00:00
|
|
|
}
|
2009-01-01 17:52:09 +00:00
|
|
|
}
|
|
|
|
return oldest_lease;
|
2007-04-07 01:05:47 +00:00
|
|
|
}
|
|
|
|
|
2010-03-20 23:43:11 +00:00
|
|
|
/* Clear out all leases with matching nonzero chaddr OR yiaddr.
|
|
|
|
* If chaddr == NULL, this is a conflict lease.
|
|
|
|
*/
|
|
|
|
static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
2010-03-20 23:43:11 +00:00
|
|
|
unsigned i;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2009-01-01 17:52:09 +00:00
|
|
|
for (i = 0; i < server_config.max_leases; i++) {
|
2010-03-20 23:43:11 +00:00
|
|
|
if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
|
2009-06-17 11:24:03 +00:00
|
|
|
|| (yiaddr && g_leases[i].lease_nip == yiaddr)
|
2007-04-07 01:05:47 +00:00
|
|
|
) {
|
2009-06-17 11:24:03 +00:00
|
|
|
memset(&g_leases[i], 0, sizeof(g_leases[i]));
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
2009-01-01 17:52:09 +00:00
|
|
|
}
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
2010-03-26 09:09:34 +00:00
|
|
|
/* Add a lease into the table, clearing out any old ones.
|
2010-03-20 23:43:11 +00:00
|
|
|
* If chaddr == NULL, this is a conflict lease.
|
|
|
|
*/
|
2009-06-17 09:57:09 +00:00
|
|
|
struct dyn_lease* FAST_FUNC add_lease(
|
2009-04-01 12:36:09 +00:00
|
|
|
const uint8_t *chaddr, uint32_t yiaddr,
|
2009-07-07 12:59:30 +00:00
|
|
|
leasetime_t leasetime,
|
|
|
|
const char *hostname, int hostname_len)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
2009-06-17 09:57:09 +00:00
|
|
|
struct dyn_lease *oldest;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
/* clean out any old ones */
|
2010-03-20 23:43:11 +00:00
|
|
|
clear_leases(chaddr, yiaddr);
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
oldest = oldest_expired_lease();
|
|
|
|
|
|
|
|
if (oldest) {
|
2010-03-20 23:43:11 +00:00
|
|
|
memset(oldest, 0, sizeof(*oldest));
|
2009-04-01 12:36:09 +00:00
|
|
|
if (hostname) {
|
2009-07-07 12:59:30 +00:00
|
|
|
char *p;
|
2010-05-02 12:17:07 +00:00
|
|
|
|
|
|
|
hostname_len++; /* include NUL */
|
2009-07-07 12:59:30 +00:00
|
|
|
if (hostname_len > sizeof(oldest->hostname))
|
|
|
|
hostname_len = sizeof(oldest->hostname);
|
2010-01-25 12:39:24 +00:00
|
|
|
p = safe_strncpy(oldest->hostname, hostname, hostname_len);
|
2009-04-01 19:48:05 +00:00
|
|
|
/* sanitization (s/non-ASCII/^/g) */
|
2009-07-07 12:59:30 +00:00
|
|
|
while (*p) {
|
|
|
|
if (*p < ' ' || *p > 126)
|
|
|
|
*p = '^';
|
|
|
|
p++;
|
2009-04-01 12:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-20 23:43:11 +00:00
|
|
|
if (chaddr)
|
|
|
|
memcpy(oldest->lease_mac, chaddr, 6);
|
2009-06-16 08:23:01 +00:00
|
|
|
oldest->lease_nip = yiaddr;
|
2009-01-01 17:52:09 +00:00
|
|
|
oldest->expires = time(NULL) + leasetime;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return oldest;
|
|
|
|
}
|
|
|
|
|
2009-01-01 17:52:09 +00:00
|
|
|
/* True if a lease has expired */
|
2009-06-17 11:24:03 +00:00
|
|
|
int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
2009-01-01 17:52:09 +00:00
|
|
|
return (lease->expires < (leasetime_t) time(NULL));
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:03 +00:00
|
|
|
/* Find the first lease that matches MAC, NULL if no match */
|
|
|
|
struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
2007-04-07 01:05:47 +00:00
|
|
|
unsigned i;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
for (i = 0; i < server_config.max_leases; i++)
|
2009-06-17 11:24:03 +00:00
|
|
|
if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
|
|
|
|
return &g_leases[i];
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-17 11:24:03 +00:00
|
|
|
/* Find the first lease that matches IP, NULL is no match */
|
|
|
|
struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
2007-04-07 01:05:47 +00:00
|
|
|
unsigned i;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
for (i = 0; i < server_config.max_leases; i++)
|
2009-06-17 11:24:03 +00:00
|
|
|
if (g_leases[i].lease_nip == nip)
|
|
|
|
return &g_leases[i];
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-16 10:04:23 +00:00
|
|
|
/* Check if the IP is taken; if it is, add it to the lease table */
|
2009-06-17 11:24:03 +00:00
|
|
|
static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
|
|
|
struct in_addr temp;
|
2007-07-03 15:47:50 +00:00
|
|
|
int r;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2009-06-17 11:24:03 +00:00
|
|
|
r = arpping(nip, safe_mac,
|
2009-06-16 10:04:23 +00:00
|
|
|
server_config.server_nip,
|
|
|
|
server_config.server_mac,
|
2009-06-16 08:20:27 +00:00
|
|
|
server_config.interface);
|
2007-07-03 15:47:50 +00:00
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2009-06-17 11:24:03 +00:00
|
|
|
temp.s_addr = nip;
|
2007-07-03 15:47:50 +00:00
|
|
|
bb_info_msg("%s belongs to someone, reserving it for %u seconds",
|
|
|
|
inet_ntoa(temp), (unsigned)server_config.conflict_time);
|
2010-03-20 23:43:11 +00:00
|
|
|
add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
|
2007-04-07 01:05:47 +00:00
|
|
|
return 0;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
2009-06-16 10:05:21 +00:00
|
|
|
/* Find a new usable (we think) address */
|
2009-06-17 11:24:03 +00:00
|
|
|
uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
|
2006-05-08 03:20:50 +00:00
|
|
|
{
|
2009-01-01 17:52:09 +00:00
|
|
|
uint32_t addr;
|
2009-06-17 09:57:09 +00:00
|
|
|
struct dyn_lease *oldest_lease = NULL;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-07-01 17:05:57 +00:00
|
|
|
addr = server_config.start_ip; /* addr is in host order here */
|
2007-08-09 08:04:05 +00:00
|
|
|
for (; addr <= server_config.end_ip; addr++) {
|
2009-06-17 11:24:03 +00:00
|
|
|
uint32_t nip;
|
2009-06-17 09:57:09 +00:00
|
|
|
struct dyn_lease *lease;
|
2009-01-01 17:52:09 +00:00
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
/* ie, 192.168.55.0 */
|
2009-01-01 17:52:09 +00:00
|
|
|
if ((addr & 0xff) == 0)
|
2007-08-09 08:04:05 +00:00
|
|
|
continue;
|
2006-05-08 03:20:50 +00:00
|
|
|
/* ie, 192.168.55.255 */
|
2009-01-01 17:52:09 +00:00
|
|
|
if ((addr & 0xff) == 0xff)
|
2007-08-09 08:04:05 +00:00
|
|
|
continue;
|
2009-06-17 11:24:03 +00:00
|
|
|
nip = htonl(addr);
|
2009-06-16 10:03:12 +00:00
|
|
|
/* is this a static lease addr? */
|
2009-06-17 11:24:03 +00:00
|
|
|
if (is_nip_reserved(server_config.static_leases, nip))
|
2009-01-01 17:52:09 +00:00
|
|
|
continue;
|
|
|
|
|
2009-06-17 11:24:03 +00:00
|
|
|
lease = find_lease_by_nip(nip);
|
2009-01-01 17:52:09 +00:00
|
|
|
if (!lease) {
|
2010-03-26 08:32:09 +00:00
|
|
|
//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
|
2009-06-17 11:24:03 +00:00
|
|
|
if (nobody_responds_to_arp(nip, safe_mac))
|
|
|
|
return nip;
|
2009-01-01 17:52:09 +00:00
|
|
|
} else {
|
|
|
|
if (!oldest_lease || lease->expires < oldest_lease->expires)
|
|
|
|
oldest_lease = lease;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-01 17:52:09 +00:00
|
|
|
|
2010-03-20 23:43:11 +00:00
|
|
|
if (oldest_lease
|
|
|
|
&& is_expired_lease(oldest_lease)
|
2009-06-17 11:24:03 +00:00
|
|
|
&& nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
|
2009-01-01 17:52:09 +00:00
|
|
|
) {
|
2009-06-16 08:23:01 +00:00
|
|
|
return oldest_lease->lease_nip;
|
2009-01-01 17:52:09 +00:00
|
|
|
}
|
|
|
|
|
2006-05-08 03:20:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|