blob: 4999d8079eadf54088297915f97df8a0d0a8c714 [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 *
5 * Licensed under GPLv2, see file LICENSE in this tarball for details.
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;
63 if (hostname_len > sizeof(oldest->hostname))
64 hostname_len = sizeof(oldest->hostname);
Denys Vlasenko3581c622010-01-25 13:39:24 +010065 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
Denis Vlasenko551ffdc2009-04-01 19:48:05 +000066 /* sanitization (s/non-ASCII/^/g) */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020067 while (*p) {
68 if (*p < ' ' || *p > 126)
69 *p = '^';
70 p++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000071 }
72 }
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010073 if (chaddr)
74 memcpy(oldest->lease_mac, chaddr, 6);
Denys Vlasenko1d924f52009-06-16 10:23:01 +020075 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000076 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000077 }
78
79 return oldest;
80}
81
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000082/* True if a lease has expired */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020083int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000084{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000085 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000086}
87
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020088/* Find the first lease that matches MAC, NULL if no match */
89struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000090{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000091 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000092
93 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020094 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
95 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +000096
97 return NULL;
98}
99
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200100/* Find the first lease that matches IP, NULL is no match */
101struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +0000102{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000103 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000104
105 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200106 if (g_leases[i].lease_nip == nip)
107 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000108
109 return NULL;
110}
111
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200112/* Check if the IP is taken; if it is, add it to the lease table */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200113static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000114{
115 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000116 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000117
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200118 r = arpping(nip, safe_mac,
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200119 server_config.server_nip,
120 server_config.server_mac,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200121 server_config.interface);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000122 if (r)
123 return r;
124
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200125 temp.s_addr = nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000126 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
127 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100128 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000129 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000130}
131
Denys Vlasenkoed8982b2009-06-16 12:05:21 +0200132/* Find a new usable (we think) address */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200133uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000134{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000135 uint32_t addr;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200136 struct dyn_lease *oldest_lease = NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +0000137
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000138 addr = server_config.start_ip; /* addr is in host order here */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000139 for (; addr <= server_config.end_ip; addr++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200140 uint32_t nip;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200141 struct dyn_lease *lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000142
Mike Frysinger7031f622006-05-08 03:20:50 +0000143 /* ie, 192.168.55.0 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000144 if ((addr & 0xff) == 0)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000145 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000146 /* ie, 192.168.55.255 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000147 if ((addr & 0xff) == 0xff)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000148 continue;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200149 nip = htonl(addr);
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200150 /* is this a static lease addr? */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200151 if (is_nip_reserved(server_config.static_leases, nip))
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000152 continue;
153
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200154 lease = find_lease_by_nip(nip);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000155 if (!lease) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100156//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 +0200157 if (nobody_responds_to_arp(nip, safe_mac))
158 return nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000159 } else {
160 if (!oldest_lease || lease->expires < oldest_lease->expires)
161 oldest_lease = lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000162 }
163 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000164
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100165 if (oldest_lease
166 && is_expired_lease(oldest_lease)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200167 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000168 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200169 return oldest_lease->lease_nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000170 }
171
Mike Frysinger7031f622006-05-08 03:20:50 +0000172 return 0;
173}