blob: 844bb60b17f77558300c9baa9747dd5412e92f8d [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/*
Mike Frysinger7031f622006-05-08 03:20:50 +00003 * Russ Dill <Russ.Dill@asu.edu> July 2001
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2, see file LICENSE in this source tree.
Mike Frysinger7031f622006-05-08 03:20:50 +00006 */
Mike Frysinger7031f622006-05-08 03:20:50 +00007#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +00008#include "dhcpd.h"
Mike Frysinger7031f622006-05-08 03:20:50 +00009
Denis Vlasenkofbd29182007-04-07 01:05:47 +000010/* Find the oldest expired lease, NULL if there are no expired leases */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020011static struct dyn_lease *oldest_expired_lease(void)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000012{
Denys Vlasenko31af3d52009-06-17 11:57:09 +020013 struct dyn_lease *oldest_lease = NULL;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000014 leasetime_t oldest_time = time(NULL);
Denis Vlasenkofbd29182007-04-07 01:05:47 +000015 unsigned i;
16
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020017 /* Unexpired leases have g_leases[i].expires >= current time
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000018 * and therefore can't ever match */
19 for (i = 0; i < server_config.max_leases; i++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020020 if (g_leases[i].expires < oldest_time) {
21 oldest_time = g_leases[i].expires;
22 oldest_lease = &g_leases[i];
Denis Vlasenkofbd29182007-04-07 01:05:47 +000023 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000024 }
25 return oldest_lease;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000026}
27
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010028/* Clear out all leases with matching nonzero chaddr OR yiaddr.
29 * If chaddr == NULL, this is a conflict lease.
30 */
31static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000032{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010033 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000034
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000035 for (i = 0; i < server_config.max_leases; i++) {
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010036 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020037 || (yiaddr && g_leases[i].lease_nip == yiaddr)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000038 ) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020039 memset(&g_leases[i], 0, sizeof(g_leases[i]));
Mike Frysinger7031f622006-05-08 03:20:50 +000040 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000041 }
Mike Frysinger7031f622006-05-08 03:20:50 +000042}
43
Denys Vlasenko385b4562010-03-26 10:09:34 +010044/* Add a lease into the table, clearing out any old ones.
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010045 * If chaddr == NULL, this is a conflict lease.
46 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020047struct dyn_lease* FAST_FUNC add_lease(
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000048 const uint8_t *chaddr, uint32_t yiaddr,
Denys Vlasenko95cc8142009-07-07 14:59:30 +020049 leasetime_t leasetime,
50 const char *hostname, int hostname_len)
Mike Frysinger7031f622006-05-08 03:20:50 +000051{
Denys Vlasenko31af3d52009-06-17 11:57:09 +020052 struct dyn_lease *oldest;
Mike Frysinger7031f622006-05-08 03:20:50 +000053
54 /* clean out any old ones */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010055 clear_leases(chaddr, yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +000056
57 oldest = oldest_expired_lease();
58
59 if (oldest) {
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010060 memset(oldest, 0, sizeof(*oldest));
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000061 if (hostname) {
Denys Vlasenko95cc8142009-07-07 14:59:30 +020062 char *p;
Denys Vlasenkofde3fb32010-05-02 14:17:07 +020063
64 hostname_len++; /* include NUL */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020065 if (hostname_len > sizeof(oldest->hostname))
66 hostname_len = sizeof(oldest->hostname);
Denys Vlasenko3581c622010-01-25 13:39:24 +010067 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
Denys Vlasenko9a512172015-10-24 04:45:22 +020068 /*
69 * Sanitization (s/bad_char/./g).
70 * The intent is not to allow only "DNS-valid" hostnames,
71 * but merely make dumpleases output safe for shells to use.
72 * We accept "0-9A-Za-z._-", all other chars turn to dots.
73 */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020074 while (*p) {
Denys Vlasenko9a512172015-10-24 04:45:22 +020075 if (!isalnum(*p) && *p != '-' && *p != '_')
76 *p = '.';
Denys Vlasenko95cc8142009-07-07 14:59:30 +020077 p++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000078 }
79 }
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010080 if (chaddr)
81 memcpy(oldest->lease_mac, chaddr, 6);
Denys Vlasenko1d924f52009-06-16 10:23:01 +020082 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000083 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000084 }
85
86 return oldest;
87}
88
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000089/* True if a lease has expired */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020090int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000091{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000092 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000093}
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
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200107/* Find the first lease that matches IP, NULL is no match */
108struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
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 Vlasenko6947d2c2009-06-17 13:24:03 +0200113 if (g_leases[i].lease_nip == nip)
114 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000115
116 return NULL;
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 */
Michel Stam9f412712014-10-30 11:59:04 +0100120static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms)
Mike Frysinger7031f622006-05-08 03:20:50 +0000121{
122 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000123 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000124
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200125 r = arpping(nip, safe_mac,
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200126 server_config.server_nip,
127 server_config.server_mac,
Michel Stam9f412712014-10-30 11:59:04 +0100128 server_config.interface,
129 arpping_ms);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000130 if (r)
131 return r;
132
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200133 temp.s_addr = nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000134 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
135 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100136 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000137 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000138}
139
Denys Vlasenkoed8982b2009-06-16 12:05:21 +0200140/* Find a new usable (we think) address */
Michel Stam9f412712014-10-30 11:59:04 +0100141uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms)
Mike Frysinger7031f622006-05-08 03:20:50 +0000142{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000143 uint32_t addr;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200144 struct dyn_lease *oldest_lease = NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +0000145
Vladislav Grishenko58271672011-02-16 13:31:30 +0100146#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
147 uint32_t stop;
148 unsigned i, hash;
149
150 /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
151 * dispersal even with similarly-valued "strings".
152 */
153 hash = 0;
154 for (i = 0; i < 6; i++)
155 hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
156
157 /* pick a seed based on hwaddr then iterate until we find a free address. */
158 addr = server_config.start_ip
159 + (hash % (1 + server_config.end_ip - server_config.start_ip));
160 stop = addr;
161#else
162 addr = server_config.start_ip;
163#define stop (server_config.end_ip + 1)
164#endif
165 do {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200166 uint32_t nip;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200167 struct dyn_lease *lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000168
Mike Frysinger7031f622006-05-08 03:20:50 +0000169 /* ie, 192.168.55.0 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000170 if ((addr & 0xff) == 0)
Vladislav Grishenko58271672011-02-16 13:31:30 +0100171 goto next_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000172 /* ie, 192.168.55.255 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000173 if ((addr & 0xff) == 0xff)
Vladislav Grishenko58271672011-02-16 13:31:30 +0100174 goto next_addr;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200175 nip = htonl(addr);
Vladislav Grishenko58271672011-02-16 13:31:30 +0100176 /* skip our own address */
177 if (nip == server_config.server_nip)
178 goto next_addr;
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200179 /* is this a static lease addr? */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200180 if (is_nip_reserved(server_config.static_leases, nip))
Vladislav Grishenko58271672011-02-16 13:31:30 +0100181 goto next_addr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000182
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200183 lease = find_lease_by_nip(nip);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000184 if (!lease) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100185//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
Michel Stam9f412712014-10-30 11:59:04 +0100186 if (nobody_responds_to_arp(nip, safe_mac, arpping_ms))
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200187 return nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000188 } else {
189 if (!oldest_lease || lease->expires < oldest_lease->expires)
190 oldest_lease = lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000191 }
Vladislav Grishenko58271672011-02-16 13:31:30 +0100192
193 next_addr:
194 addr++;
195#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
196 if (addr > server_config.end_ip)
197 addr = server_config.start_ip;
198#endif
199 } while (addr != stop);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000200
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100201 if (oldest_lease
202 && is_expired_lease(oldest_lease)
Michel Stam9f412712014-10-30 11:59:04 +0100203 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000204 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200205 return oldest_lease->lease_nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000206 }
207
Mike Frysinger7031f622006-05-08 03:20:50 +0000208 return 0;
209}