blob: 7aeb37baecc9caef67358524b996592965353c0f [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);
Denis Vlasenko551ffdc2009-04-01 19:48:05 +000068 /* sanitization (s/non-ASCII/^/g) */
Denys Vlasenko95cc8142009-07-07 14:59:30 +020069 while (*p) {
70 if (*p < ' ' || *p > 126)
71 *p = '^';
72 p++;
Denis Vlasenkobd79c3d2009-04-01 12:36:09 +000073 }
74 }
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +010075 if (chaddr)
76 memcpy(oldest->lease_mac, chaddr, 6);
Denys Vlasenko1d924f52009-06-16 10:23:01 +020077 oldest->lease_nip = yiaddr;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000078 oldest->expires = time(NULL) + leasetime;
Mike Frysinger7031f622006-05-08 03:20:50 +000079 }
80
81 return oldest;
82}
83
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000084/* True if a lease has expired */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020085int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000086{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +000087 return (lease->expires < (leasetime_t) time(NULL));
Mike Frysinger7031f622006-05-08 03:20:50 +000088}
89
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020090/* Find the first lease that matches MAC, NULL if no match */
91struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000092{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000093 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000094
95 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020096 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
97 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +000098
99 return NULL;
100}
101
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200102/* Find the first lease that matches IP, NULL is no match */
103struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
Mike Frysinger7031f622006-05-08 03:20:50 +0000104{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000105 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +0000106
107 for (i = 0; i < server_config.max_leases; i++)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200108 if (g_leases[i].lease_nip == nip)
109 return &g_leases[i];
Mike Frysinger7031f622006-05-08 03:20:50 +0000110
111 return NULL;
112}
113
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200114/* Check if the IP is taken; if it is, add it to the lease table */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200115static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000116{
117 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000118 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000119
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200120 r = arpping(nip, safe_mac,
Denys Vlasenko26918dd2009-06-16 12:04:23 +0200121 server_config.server_nip,
122 server_config.server_mac,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200123 server_config.interface);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000124 if (r)
125 return r;
126
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200127 temp.s_addr = nip;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000128 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
129 inet_ntoa(temp), (unsigned)server_config.conflict_time);
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100130 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000131 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000132}
133
Denys Vlasenkoed8982b2009-06-16 12:05:21 +0200134/* Find a new usable (we think) address */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200135uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
Mike Frysinger7031f622006-05-08 03:20:50 +0000136{
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000137 uint32_t addr;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200138 struct dyn_lease *oldest_lease = NULL;
Mike Frysinger7031f622006-05-08 03:20:50 +0000139
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000140 addr = server_config.start_ip; /* addr is in host order here */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000141 for (; addr <= server_config.end_ip; addr++) {
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200142 uint32_t nip;
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200143 struct dyn_lease *lease;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000144
Mike Frysinger7031f622006-05-08 03:20:50 +0000145 /* ie, 192.168.55.0 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000146 if ((addr & 0xff) == 0)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000147 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000148 /* ie, 192.168.55.255 */
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000149 if ((addr & 0xff) == 0xff)
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000150 continue;
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200151 nip = htonl(addr);
Denys Vlasenkocab3a012009-06-16 12:03:12 +0200152 /* is this a static lease addr? */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200153 if (is_nip_reserved(server_config.static_leases, nip))
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000154 continue;
155
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200156 lease = find_lease_by_nip(nip);
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000157 if (!lease) {
Denys Vlasenko7724c762010-03-26 09:32:09 +0100158//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 +0200159 if (nobody_responds_to_arp(nip, safe_mac))
160 return nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000161 } else {
162 if (!oldest_lease || lease->expires < oldest_lease->expires)
163 oldest_lease = lease;
Mike Frysinger7031f622006-05-08 03:20:50 +0000164 }
165 }
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000166
Denys Vlasenkoe5ce91b2010-03-21 00:43:11 +0100167 if (oldest_lease
168 && is_expired_lease(oldest_lease)
Denys Vlasenko6947d2c2009-06-17 13:24:03 +0200169 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000170 ) {
Denys Vlasenko1d924f52009-06-16 10:23:01 +0200171 return oldest_lease->lease_nip;
Denis Vlasenko0416e3d2009-01-01 17:52:09 +0000172 }
173
Mike Frysinger7031f622006-05-08 03:20:50 +0000174 return 0;
175}