blob: 81acb9910b8f5e58b25785c2d12b27bdc33c36cb [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 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020014static struct dyn_lease *oldest_expired_lease(void)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000015{
Denys Vlasenko31af3d52009-06-17 11:57:09 +020016 struct dyn_lease *oldest_lease = NULL;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000017 leasetime_t oldest_time = time(NULL);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000018 unsigned i;
19
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020020 /* Unexpired leases have g_leases[i].expires >= current time
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000021 * and therefore can't ever match */
22 for (i = 0; i < server_config.max_leases; i++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020023 if (g_leases[i].expires < oldest_time) {
24 oldest_time = g_leases[i].expires;
25 oldest_lease = &g_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
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010032/* Clear out all leases with matching nonzero chaddr OR yiaddr.
33 * If chaddr == NULL, this is a conflict lease.
34 */
35static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000036{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010037 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000038
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000039 for (i = 0; i < server_config.max_leases; i++) {
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010040 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020041 || (yiaddr && g_leases[i].lease_nip == yiaddr)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000042 ) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020043 memset(&g_leases[i], 0, sizeof(g_leases[i]));
Mike Frysinger7031f622006-05-08 03:20:50 +000044 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000045 }
Mike Frysinger7031f622006-05-08 03:20:50 +000046}
47
48
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010049/* Add a lease into the table, clearing out any old ones
50 * If chaddr == NULL, this is a conflict lease.
51 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020052struct dyn_lease* FAST_FUNC add_lease(
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000053 const uint8_t *chaddr, uint32_t yiaddr,
Denys Vlasenko95cc8142009-07-07 14:59:30 +020054 leasetime_t leasetime,
55 const char *hostname, int hostname_len)
Mike Frysinger7031f622006-05-08 03:20:50 +000056{
Denys Vlasenko31af3d52009-06-17 11:57:09 +020057 struct dyn_lease *oldest;
Mike Frysinger7031f622006-05-08 03:20:50 +000058
59 /* clean out any old ones */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010060 clear_leases(chaddr, yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +000061
62 oldest = oldest_expired_lease();
63
64 if (oldest) {
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010065 memset(oldest, 0, sizeof(*oldest));
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000066 if (hostname) {
Denys Vlasenko95cc8142009-07-07 14:59:30 +020067 char *p;
68 if (hostname_len > sizeof(oldest->hostname))
69 hostname_len = sizeof(oldest->hostname);
Denys Vlasenko3581c622010-01-25 13:39:24 +010070 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
Denis Vlasenko551ffdc2009-04-01 19:48:05 +000071 /* sanitization (s/non-ASCII/^/g) */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020072 while (*p) {
73 if (*p < ' ' || *p > 126)
74 *p = '^';
75 p++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000076 }
77 }
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010078 if (chaddr)
79 memcpy(oldest->lease_mac, chaddr, 6);
Denys Vlasenko1d924f52009-06-16 10:23:01 +020080 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000081 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000082 }
83
84 return oldest;
85}
86
87
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000088/* True if a lease has expired */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020089int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000090{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000091 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000092}
93
94
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020095/* Find the first lease that matches MAC, NULL if no match */
96struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000097{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000098 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000099
100 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200101 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
102 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000103
104 return NULL;
105}
106
107
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200108/* Find the first lease that matches IP, NULL is no match */
109struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +0000110{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000111 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000112
113 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200114 if (g_leases[i].lease_nip == nip)
115 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000116
117 return NULL;
118}
119
120
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200121/* Check if the IP is taken; if it is, add it to the lease table */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200122static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000123{
124 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000125 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000126
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200127 r = arpping(nip, safe_mac,
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200128 server_config.server_nip,
129 server_config.server_mac,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200130 server_config.interface);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000131 if (r)
132 return r;
133
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200134 temp.s_addr = nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000135 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
136 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100137 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000138 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000139}
140
141
Denys Vlasenkoed8982b2009-06-16 12:05:21 +0200142/* Find a new usable (we think) address */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200143uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000144{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000145 uint32_t addr;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200146 struct dyn_lease *oldest_lease = NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +0000147
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000148 addr = server_config.start_ip; /* addr is in host order here */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000149 for (; addr <= server_config.end_ip; addr++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200150 uint32_t nip;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200151 struct dyn_lease *lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000152
Mike Frysinger7031f622006-05-08 03:20:50 +0000153 /* ie, 192.168.55.0 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000154 if ((addr & 0xff) == 0)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000155 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000156 /* ie, 192.168.55.255 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000157 if ((addr & 0xff) == 0xff)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000158 continue;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200159 nip = htonl(addr);
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200160 /* is this a static lease addr? */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200161 if (is_nip_reserved(server_config.static_leases, nip))
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000162 continue;
163
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200164 lease = find_lease_by_nip(nip);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000165 if (!lease) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100166//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200167 if (nobody_responds_to_arp(nip, safe_mac))
168 return nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000169 } else {
170 if (!oldest_lease || lease->expires < oldest_lease->expires)
171 oldest_lease = lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000172 }
173 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000174
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100175 if (oldest_lease
176 && is_expired_lease(oldest_lease)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200177 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
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}