blob: 6642e396d61d8b7d19a996cecbf45ab5e80585bc [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++) {
Christian Lindebergea2b71b2016-03-01 19:23:22 +010020 if (g_leases[i].expires == 0 /* empty entry */
21 || g_leases[i].expires < oldest_time
22 ) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020023 oldest_time = g_leases[i].expires;
24 oldest_lease = &g_leases[i];
Denis Vlasenkofbd29182007-04-07 01:05:47 +000025 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000026 }
27 return oldest_lease;
Denis Vlasenkofbd29182007-04-07 01:05:47 +000028}
29
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010030/* Clear out all leases with matching nonzero chaddr OR yiaddr.
31 * If chaddr == NULL, this is a conflict lease.
32 */
33static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000034{
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010035 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000037 for (i = 0; i < server_config.max_leases; i++) {
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010038 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020039 || (yiaddr && g_leases[i].lease_nip == yiaddr)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000040 ) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020041 memset(&g_leases[i], 0, sizeof(g_leases[i]));
Mike Frysinger7031f622006-05-08 03:20:50 +000042 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000043 }
Mike Frysinger7031f622006-05-08 03:20:50 +000044}
45
Denys Vlasenko385b4562010-03-26 10:09:34 +010046/* Add a lease into the table, clearing out any old ones.
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010047 * If chaddr == NULL, this is a conflict lease.
48 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020049struct dyn_lease* FAST_FUNC add_lease(
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000050 const uint8_t *chaddr, uint32_t yiaddr,
Denys Vlasenko95cc8142009-07-07 14:59:30 +020051 leasetime_t leasetime,
52 const char *hostname, int hostname_len)
Mike Frysinger7031f622006-05-08 03:20:50 +000053{
Denys Vlasenko31af3d52009-06-17 11:57:09 +020054 struct dyn_lease *oldest;
Mike Frysinger7031f622006-05-08 03:20:50 +000055
56 /* clean out any old ones */
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010057 clear_leases(chaddr, yiaddr);
Mike Frysinger7031f622006-05-08 03:20:50 +000058
59 oldest = oldest_expired_lease();
60
61 if (oldest) {
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010062 memset(oldest, 0, sizeof(*oldest));
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000063 if (hostname) {
Denys Vlasenko95cc8142009-07-07 14:59:30 +020064 char *p;
Denys Vlasenkofde3fb32010-05-02 14:17:07 +020065
66 hostname_len++; /* include NUL */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020067 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);
Denys Vlasenko9a512172015-10-24 04:45:22 +020070 /*
71 * Sanitization (s/bad_char/./g).
72 * The intent is not to allow only "DNS-valid" hostnames,
73 * but merely make dumpleases output safe for shells to use.
74 * We accept "0-9A-Za-z._-", all other chars turn to dots.
75 */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020076 while (*p) {
Denys Vlasenko9a512172015-10-24 04:45:22 +020077 if (!isalnum(*p) && *p != '-' && *p != '_')
78 *p = '.';
Denys Vlasenko95cc8142009-07-07 14:59:30 +020079 p++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000080 }
81 }
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010082 if (chaddr)
83 memcpy(oldest->lease_mac, chaddr, 6);
Denys Vlasenko1d924f52009-06-16 10:23:01 +020084 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000085 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000086 }
87
88 return oldest;
89}
90
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000091/* True if a lease has expired */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020092int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000093{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000094 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000095}
96
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020097/* Find the first lease that matches MAC, NULL if no match */
98struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000099{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000100 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000101
102 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200103 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
104 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000105
106 return NULL;
107}
108
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200109/* Find the first lease that matches IP, NULL is no match */
110struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +0000111{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000112 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000113
114 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200115 if (g_leases[i].lease_nip == nip)
116 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000117
118 return NULL;
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 */
Michel Stam9f412712014-10-30 11:59:04 +0100122static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms)
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,
Michel Stam9f412712014-10-30 11:59:04 +0100130 server_config.interface,
131 arpping_ms);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000132 if (r)
133 return r;
134
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200135 temp.s_addr = nip;
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +0200136 bb_error_msg("%s belongs to someone, reserving it for %u seconds",
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000137 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100138 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000139 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000140}
141
Denys Vlasenkoed8982b2009-06-16 12:05:21 +0200142/* Find a new usable (we think) address */
Michel Stam9f412712014-10-30 11:59:04 +0100143uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms)
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
Vladislav Grishenko58271672011-02-16 13:31:30 +0100148#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
149 uint32_t stop;
150 unsigned i, hash;
151
152 /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
153 * dispersal even with similarly-valued "strings".
154 */
155 hash = 0;
156 for (i = 0; i < 6; i++)
157 hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
158
159 /* pick a seed based on hwaddr then iterate until we find a free address. */
160 addr = server_config.start_ip
161 + (hash % (1 + server_config.end_ip - server_config.start_ip));
162 stop = addr;
163#else
164 addr = server_config.start_ip;
165#define stop (server_config.end_ip + 1)
166#endif
167 do {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200168 uint32_t nip;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200169 struct dyn_lease *lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000170
Mike Frysinger7031f622006-05-08 03:20:50 +0000171 /* ie, 192.168.55.0 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000172 if ((addr & 0xff) == 0)
Vladislav Grishenko58271672011-02-16 13:31:30 +0100173 goto next_addr;
Mike Frysinger7031f622006-05-08 03:20:50 +0000174 /* ie, 192.168.55.255 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000175 if ((addr & 0xff) == 0xff)
Vladislav Grishenko58271672011-02-16 13:31:30 +0100176 goto next_addr;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200177 nip = htonl(addr);
Vladislav Grishenko58271672011-02-16 13:31:30 +0100178 /* skip our own address */
179 if (nip == server_config.server_nip)
180 goto next_addr;
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200181 /* is this a static lease addr? */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200182 if (is_nip_reserved(server_config.static_leases, nip))
Vladislav Grishenko58271672011-02-16 13:31:30 +0100183 goto next_addr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000184
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200185 lease = find_lease_by_nip(nip);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000186 if (!lease) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100187//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 +0100188 if (nobody_responds_to_arp(nip, safe_mac, arpping_ms))
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200189 return nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000190 } else {
191 if (!oldest_lease || lease->expires < oldest_lease->expires)
192 oldest_lease = lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000193 }
Vladislav Grishenko58271672011-02-16 13:31:30 +0100194
195 next_addr:
196 addr++;
197#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
198 if (addr > server_config.end_ip)
199 addr = server_config.start_ip;
200#endif
201 } while (addr != stop);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000202
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100203 if (oldest_lease
204 && is_expired_lease(oldest_lease)
Michel Stam9f412712014-10-30 11:59:04 +0100205 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000206 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200207 return oldest_lease->lease_nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000208 }
209
Mike Frysinger7031f622006-05-08 03:20:50 +0000210 return 0;
211}