blob: 040126fe897d8d8fbedaffaddaa7210455c272cd [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
3 * leases.c -- tools to manage DHCP leases
4 * Russ Dill <Russ.Dill@asu.edu> July 2001
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 *
6 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00007 */
8
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000010#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000011
12
Denis Vlasenkofbd29182007-04-07 01:05:47 +000013/* Find the oldest expired lease, NULL if there are no expired leases */
14static struct dhcpOfferedAddr *oldest_expired_lease(void)
15{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000016 struct dhcpOfferedAddr *oldest_lease = NULL;
17 leasetime_t oldest_time = time(NULL);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000018 unsigned i;
19
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000020 /* Unexpired leases have leases[i].expires >= current time
21 * and therefore can't ever match */
22 for (i = 0; i < server_config.max_leases; i++) {
23 if (leases[i].expires < oldest_time) {
24 oldest_time = leases[i].expires;
25 oldest_lease = &(leases[i]);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000026 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000027 }
28 return oldest_lease;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000029}
30
Mike Frysinger7031f622006-05-08 03:20:50 +000031
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000032/* Clear every lease out that chaddr OR yiaddr matches and is nonzero */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000033static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000034{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000035 unsigned i, j;
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Denis Vlasenko22a80d12007-08-09 08:04:05 +000037 for (j = 0; j < 16 && !chaddr[j]; j++)
38 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +000039
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000040 for (i = 0; i < server_config.max_leases; i++) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +020041 if ((j != 16 && memcmp(leases[i].lease_mac16, chaddr, 16) == 0)
42 || (yiaddr && leases[i].lease_nip == yiaddr)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000043 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +020044 memset(&leases[i], 0, sizeof(leases[i]));
Mike Frysinger7031f622006-05-08 03:20:50 +000045 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000046 }
Mike Frysinger7031f622006-05-08 03:20:50 +000047}
48
49
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000050/* Add a lease into the table, clearing out any old ones */
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000051struct dhcpOfferedAddr* FAST_FUNC add_lease(
52 const uint8_t *chaddr, uint32_t yiaddr,
53 leasetime_t leasetime, uint8_t *hostname)
Mike Frysinger7031f622006-05-08 03:20:50 +000054{
55 struct dhcpOfferedAddr *oldest;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000056 uint8_t hostname_length;
Mike Frysinger7031f622006-05-08 03:20:50 +000057
58 /* clean out any old ones */
59 clear_lease(chaddr, yiaddr);
60
61 oldest = oldest_expired_lease();
62
63 if (oldest) {
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000064 oldest->hostname[0] = '\0';
65 if (hostname) {
Denis Vlasenko76478b82009-04-21 00:41:57 +000066 /* option size byte, + 1 for NUL */
67 hostname_length = hostname[-1] + 1;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000068 if (hostname_length > sizeof(oldest->hostname))
69 hostname_length = sizeof(oldest->hostname);
70 hostname = (uint8_t*) safe_strncpy((char*)oldest->hostname, (char*)hostname, hostname_length);
Denis Vlasenko551ffdc2009-04-01 19:48:05 +000071 /* sanitization (s/non-ASCII/^/g) */
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000072 while (*hostname) {
73 if (*hostname < ' ' || *hostname > 126)
74 *hostname = '^';
75 hostname++;
76 }
77 }
Denys Vlasenko1d924f52009-06-16 10:23:01 +020078 memcpy(oldest->lease_mac16, chaddr, 16);
79 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000080 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000081 }
82
83 return oldest;
84}
85
86
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000087/* True if a lease has expired */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000088int FAST_FUNC lease_expired(struct dhcpOfferedAddr *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000089{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000090 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000091}
92
93
Mike Frysinger7031f622006-05-08 03:20:50 +000094/* Find the first lease that matches chaddr, NULL if no match */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000095struct dhcpOfferedAddr* FAST_FUNC find_lease_by_chaddr(const uint8_t *chaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000096{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000097 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000098
99 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200100 if (!memcmp(leases[i].lease_mac16, chaddr, 16))
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000101 return &(leases[i]);
Mike Frysinger7031f622006-05-08 03:20:50 +0000102
103 return NULL;
104}
105
106
107/* Find the first lease that matches yiaddr, NULL is no match */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000108struct dhcpOfferedAddr* FAST_FUNC find_lease_by_yiaddr(uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000109{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000110 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000111
112 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200113 if (leases[i].lease_nip == yiaddr)
114 return &leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000115
116 return NULL;
117}
118
119
120/* check is an IP is taken, if it is, add it to the lease table */
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200121static int nobody_responds_to_arp(uint32_t addr, const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000122{
Denis Vlasenkob539c842007-11-29 08:17:45 +0000123 /* 16 zero bytes */
124 static const uint8_t blank_chaddr[16] = { 0 };
125 /* = { 0 } helps gcc to put it in rodata, not bss */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000126
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000128 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000129
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200130 r = arpping(addr, safe_mac,
131 server_config.server, server_config.arp,
132 server_config.interface);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000133 if (r)
134 return r;
135
136 temp.s_addr = addr;
137 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
138 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +0000139 add_lease(blank_chaddr, addr, server_config.conflict_time, NULL);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000140 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000141}
142
143
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000144/* Find a new usable (we think) address. */
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200145uint32_t FAST_FUNC find_free_or_expired_address(const uint8_t *chaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000146{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000147 uint32_t addr;
148 struct dhcpOfferedAddr *oldest_lease = NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +0000149
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000150 addr = server_config.start_ip; /* addr is in host order here */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000151 for (; addr <= server_config.end_ip; addr++) {
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000152 uint32_t net_addr;
153 struct dhcpOfferedAddr *lease;
154
Mike Frysinger7031f622006-05-08 03:20:50 +0000155 /* ie, 192.168.55.0 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000156 if ((addr & 0xff) == 0)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000157 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000158 /* ie, 192.168.55.255 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000159 if ((addr & 0xff) == 0xff)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000160 continue;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000161 net_addr = htonl(addr);
162 /* addr has a static lease? */
163 if (reservedIp(server_config.static_leases, net_addr))
164 continue;
165
166 lease = find_lease_by_yiaddr(net_addr);
167 if (!lease) {
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200168 if (nobody_responds_to_arp(net_addr, chaddr))
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000169 return net_addr;
170 } else {
171 if (!oldest_lease || lease->expires < oldest_lease->expires)
172 oldest_lease = lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000173 }
174 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000175
176 if (oldest_lease && lease_expired(oldest_lease)
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200177 && nobody_responds_to_arp(oldest_lease->lease_nip, chaddr)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000178 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200179 return oldest_lease->lease_nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000180 }
181
Mike Frysinger7031f622006-05-08 03:20:50 +0000182 return 0;
183}