blob: 60e9edc68fde822c3f97744d85c57ea4233c20f4 [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
5 */
6
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
10
Denis Vlasenkofbd29182007-04-07 01:05:47 +000011/* Find the oldest expired lease, NULL if there are no expired leases */
12static struct dhcpOfferedAddr *oldest_expired_lease(void)
13{
14 struct dhcpOfferedAddr *oldest = NULL;
Denis Vlasenko22a80d12007-08-09 08:04:05 +000015// TODO: use monotonic_sec()
Denis Vlasenkofbd29182007-04-07 01:05:47 +000016 unsigned long oldest_lease = time(0);
17 unsigned i;
18
Denis Vlasenkofbd29182007-04-07 01:05:47 +000019 for (i = 0; i < server_config.max_leases; i++)
20 if (oldest_lease > leases[i].expires) {
21 oldest_lease = leases[i].expires;
22 oldest = &(leases[i]);
23 }
24 return oldest;
25}
26
Mike Frysinger7031f622006-05-08 03:20:50 +000027
28/* clear every lease out that chaddr OR yiaddr matches and is nonzero */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000029static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000030{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000031 unsigned i, j;
Mike Frysinger7031f622006-05-08 03:20:50 +000032
Denis Vlasenko22a80d12007-08-09 08:04:05 +000033 for (j = 0; j < 16 && !chaddr[j]; j++)
34 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +000035
36 for (i = 0; i < server_config.max_leases; i++)
Denis Vlasenko22a80d12007-08-09 08:04:05 +000037 if ((j != 16 && memcmp(leases[i].chaddr, chaddr, 16) == 0)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000038 || (yiaddr && leases[i].yiaddr == yiaddr)
39 ) {
Denis Vlasenko22a80d12007-08-09 08:04:05 +000040 memset(&(leases[i]), 0, sizeof(leases[i]));
Mike Frysinger7031f622006-05-08 03:20:50 +000041 }
42}
43
44
45/* add a lease into the table, clearing out any old ones */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000046struct dhcpOfferedAddr *add_lease(const uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
Mike Frysinger7031f622006-05-08 03:20:50 +000047{
48 struct dhcpOfferedAddr *oldest;
49
50 /* clean out any old ones */
51 clear_lease(chaddr, yiaddr);
52
53 oldest = oldest_expired_lease();
54
55 if (oldest) {
56 memcpy(oldest->chaddr, chaddr, 16);
57 oldest->yiaddr = yiaddr;
58 oldest->expires = time(0) + lease;
59 }
60
61 return oldest;
62}
63
64
65/* true if a lease has expired */
66int lease_expired(struct dhcpOfferedAddr *lease)
67{
68 return (lease->expires < (unsigned long) time(0));
69}
70
71
Mike Frysinger7031f622006-05-08 03:20:50 +000072/* Find the first lease that matches chaddr, NULL if no match */
Denis Vlasenkofbd29182007-04-07 01:05:47 +000073struct dhcpOfferedAddr *find_lease_by_chaddr(const uint8_t *chaddr)
Mike Frysinger7031f622006-05-08 03:20:50 +000074{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000075 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000076
77 for (i = 0; i < server_config.max_leases; i++)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000078 if (!memcmp(leases[i].chaddr, chaddr, 16))
79 return &(leases[i]);
Mike Frysinger7031f622006-05-08 03:20:50 +000080
81 return NULL;
82}
83
84
85/* Find the first lease that matches yiaddr, NULL is no match */
86struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
87{
Denis Vlasenkofbd29182007-04-07 01:05:47 +000088 unsigned i;
Mike Frysinger7031f622006-05-08 03:20:50 +000089
90 for (i = 0; i < server_config.max_leases; i++)
Denis Vlasenkofbd29182007-04-07 01:05:47 +000091 if (leases[i].yiaddr == yiaddr)
92 return &(leases[i]);
Mike Frysinger7031f622006-05-08 03:20:50 +000093
94 return NULL;
95}
96
97
98/* check is an IP is taken, if it is, add it to the lease table */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000099static int nobody_responds_to_arp(uint32_t addr)
Mike Frysinger7031f622006-05-08 03:20:50 +0000100{
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000101 static const uint8_t blank_chaddr[16]; /* 16 zero bytes */
102
Mike Frysinger7031f622006-05-08 03:20:50 +0000103 struct in_addr temp;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000104 int r;
Mike Frysinger7031f622006-05-08 03:20:50 +0000105
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000106 r = arpping(addr, server_config.server, server_config.arp, server_config.interface);
107 if (r)
108 return r;
109
110 temp.s_addr = addr;
111 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
112 inet_ntoa(temp), (unsigned)server_config.conflict_time);
113 add_lease(blank_chaddr, addr, server_config.conflict_time);
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000114 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000115}
116
117
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000118/* find an assignable address, if check_expired is true, we check all the expired leases as well.
Mike Frysinger7031f622006-05-08 03:20:50 +0000119 * Maybe this should try expired leases by age... */
120uint32_t find_address(int check_expired)
121{
122 uint32_t addr, ret;
123 struct dhcpOfferedAddr *lease = NULL;
124
Denis Vlasenkoc82b5102007-07-01 17:05:57 +0000125 addr = server_config.start_ip; /* addr is in host order here */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000126 for (; addr <= server_config.end_ip; addr++) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000127 /* ie, 192.168.55.0 */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000128 if (!(addr & 0xFF))
129 continue;
Mike Frysinger7031f622006-05-08 03:20:50 +0000130 /* ie, 192.168.55.255 */
Denis Vlasenko22a80d12007-08-09 08:04:05 +0000131 if ((addr & 0xFF) == 0xFF)
132 continue;
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000133 /* Only do if it isn't assigned as a static lease */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000134 ret = htonl(addr);
135 if (!reservedIp(server_config.static_leases, ret)) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000136 /* lease is not taken */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000137 lease = find_lease_by_yiaddr(ret);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000138 /* no lease or it expired and we are checking for expired leases */
Denis Vlasenkofbd29182007-04-07 01:05:47 +0000139 if ((!lease || (check_expired && lease_expired(lease)))
Denis Vlasenko49a128a2007-07-17 21:42:59 +0000140 && nobody_responds_to_arp(ret) /* it isn't used on the network */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000141 ) {
142 return ret;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +0000143 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000144 }
145 }
Mike Frysinger7031f622006-05-08 03:20:50 +0000146 return 0;
147}