blob: 78b0d0ade12ae537833aeb3f882c22a546125411 [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
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 Vlasenko6947d2c2009-06-17 13:24:03 +020041 if ((j != 16 && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
42 || (yiaddr && g_leases[i].lease_nip == yiaddr)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000043 ) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020044 memset(&g_leases[i], 0, sizeof(g_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 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020051struct dyn_lease* FAST_FUNC add_lease(
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000052 const uint8_t *chaddr, uint32_t yiaddr,
Denys Vlasenko95cc8142009-07-07 14:59:30 +020053 leasetime_t leasetime,
54 const char *hostname, int hostname_len)
Mike Frysinger7031f622006-05-08 03:20:50 +000055{
Denys Vlasenko31af3d52009-06-17 11:57:09 +020056 struct dyn_lease *oldest;
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) {
Denys Vlasenko95cc8142009-07-07 14:59:30 +020066 char *p;
67 if (hostname_len > sizeof(oldest->hostname))
68 hostname_len = sizeof(oldest->hostname);
Denys Vlasenko3581c622010-01-25 13:39:24 +010069 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
Denis Vlasenko551ffdc2009-04-01 19:48:05 +000070 /* sanitization (s/non-ASCII/^/g) */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020071 while (*p) {
72 if (*p < ' ' || *p > 126)
73 *p = '^';
74 p++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000075 }
76 }
Denys Vlasenko31af3d52009-06-17 11:57:09 +020077 memcpy(oldest->lease_mac, chaddr, 6);
Denys Vlasenko1d924f52009-06-16 10:23:01 +020078 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000079 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000080 }
81
82 return oldest;
83}
84
85
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000086/* True if a lease has expired */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020087int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000088{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000089 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000090}
91
92
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020093/* Find the first lease that matches MAC, NULL if no match */
94struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000095{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000096 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000097
98 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020099 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
100 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
102 return NULL;
103}
104
105
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200106/* Find the first lease that matches IP, NULL is no match */
107struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +0000108{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000109 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000110
111 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200112 if (g_leases[i].lease_nip == nip)
113 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000114
115 return NULL;
116}
117
118
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200119/* Check if the IP is taken; if it is, add it to the lease table */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200120static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000121{
Denis Vlasenkob539c842007-11-29 08:17:45 +0000122 /* 16 zero bytes */
123 static const uint8_t blank_chaddr[16] = { 0 };
124 /* = { 0 } helps gcc to put it in rodata, not bss */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000125
Mike Frysinger7031f622006-05-08 03:20:50 +0000126 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000127 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000128
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200129 r = arpping(nip, safe_mac,
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200130 server_config.server_nip,
131 server_config.server_mac,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200132 server_config.interface);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000133 if (r)
134 return r;
135
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200136 temp.s_addr = nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000137 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
138 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denys Vlasenko95cc8142009-07-07 14:59:30 +0200139 add_lease(blank_chaddr, nip, server_config.conflict_time, NULL, 0);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000140 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000141}
142
143
Denys Vlasenkoed8982b2009-06-16 12:05:21 +0200144/* Find a new usable (we think) address */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200145uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000146{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000147 uint32_t addr;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200148 struct dyn_lease *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++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200152 uint32_t nip;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200153 struct dyn_lease *lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000154
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;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200161 nip = htonl(addr);
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200162 /* is this a static lease addr? */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200163 if (is_nip_reserved(server_config.static_leases, nip))
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000164 continue;
165
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200166 lease = find_lease_by_nip(nip);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000167 if (!lease) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200168 if (nobody_responds_to_arp(nip, safe_mac))
169 return nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000170 } 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
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200176 if (oldest_lease && is_expired_lease(oldest_lease)
177 && 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}