"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * leases.c -- tools to manage DHCP leases |
| 4 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 10 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | |
| 12 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 13 | /* Find the oldest expired lease, NULL if there are no expired leases */ |
| 14 | static struct dhcpOfferedAddr *oldest_expired_lease(void) |
| 15 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 16 | struct dhcpOfferedAddr *oldest_lease = NULL; |
| 17 | leasetime_t oldest_time = time(NULL); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 18 | unsigned i; |
| 19 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 20 | /* Unexpired leases have leases[i].expires >= current time |
| 21 | * and therefore can't ever match */ |
| 22 | for (i = 0; i < server_config.max_leases; i++) { |
| 23 | if (leases[i].expires < oldest_time) { |
| 24 | oldest_time = leases[i].expires; |
| 25 | oldest_lease = &(leases[i]); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 26 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 27 | } |
| 28 | return oldest_lease; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 32 | /* Clear every lease out that chaddr OR yiaddr matches and is nonzero */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 33 | static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 35 | unsigned i, j; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 37 | for (j = 0; j < 16 && !chaddr[j]; j++) |
| 38 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 40 | for (i = 0; i < server_config.max_leases; i++) { |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 41 | if ((j != 16 && memcmp(leases[i].chaddr, chaddr, 16) == 0) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 42 | || (yiaddr && leases[i].yiaddr == yiaddr) |
| 43 | ) { |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 44 | memset(&(leases[i]), 0, sizeof(leases[i])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 45 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 46 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 50 | /* Add a lease into the table, clearing out any old ones */ |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 51 | struct dhcpOfferedAddr* FAST_FUNC add_lease( |
| 52 | const uint8_t *chaddr, uint32_t yiaddr, |
| 53 | leasetime_t leasetime, uint8_t *hostname) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 54 | { |
| 55 | struct dhcpOfferedAddr *oldest; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 56 | uint8_t hostname_length; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | |
| 58 | /* clean out any old ones */ |
| 59 | clear_lease(chaddr, yiaddr); |
| 60 | |
| 61 | oldest = oldest_expired_lease(); |
| 62 | |
| 63 | if (oldest) { |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 64 | oldest->hostname[0] = '\0'; |
| 65 | if (hostname) { |
Denis Vlasenko | 76478b8 | 2009-04-21 00:41:57 +0000 | [diff] [blame^] | 66 | /* option size byte, + 1 for NUL */ |
| 67 | hostname_length = hostname[-1] + 1; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 68 | if (hostname_length > sizeof(oldest->hostname)) |
| 69 | hostname_length = sizeof(oldest->hostname); |
| 70 | hostname = (uint8_t*) safe_strncpy((char*)oldest->hostname, (char*)hostname, hostname_length); |
Denis Vlasenko | 551ffdc | 2009-04-01 19:48:05 +0000 | [diff] [blame] | 71 | /* sanitization (s/non-ASCII/^/g) */ |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 72 | while (*hostname) { |
| 73 | if (*hostname < ' ' || *hostname > 126) |
| 74 | *hostname = '^'; |
| 75 | hostname++; |
| 76 | } |
| 77 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 78 | memcpy(oldest->chaddr, chaddr, 16); |
| 79 | oldest->yiaddr = yiaddr; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 80 | oldest->expires = time(NULL) + leasetime; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return oldest; |
| 84 | } |
| 85 | |
| 86 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 87 | /* True if a lease has expired */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 88 | int FAST_FUNC lease_expired(struct dhcpOfferedAddr *lease) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 89 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 90 | return (lease->expires < (leasetime_t) time(NULL)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 94 | /* Find the first lease that matches chaddr, NULL if no match */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 95 | struct dhcpOfferedAddr* FAST_FUNC find_lease_by_chaddr(const uint8_t *chaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 97 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 100 | if (!memcmp(leases[i].chaddr, chaddr, 16)) |
| 101 | return &(leases[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* Find the first lease that matches yiaddr, NULL is no match */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 108 | struct dhcpOfferedAddr* FAST_FUNC find_lease_by_yiaddr(uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 109 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 110 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 111 | |
| 112 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 113 | if (leases[i].yiaddr == yiaddr) |
| 114 | return &(leases[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 115 | |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /* check is an IP is taken, if it is, add it to the lease table */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 121 | static int nobody_responds_to_arp(uint32_t addr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 122 | { |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 123 | /* 16 zero bytes */ |
| 124 | static const uint8_t blank_chaddr[16] = { 0 }; |
| 125 | /* = { 0 } helps gcc to put it in rodata, not bss */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 126 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 127 | struct in_addr temp; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 128 | int r; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 130 | r = arpping(addr, server_config.server, server_config.arp, server_config.interface); |
| 131 | if (r) |
| 132 | return r; |
| 133 | |
| 134 | temp.s_addr = addr; |
| 135 | bb_info_msg("%s belongs to someone, reserving it for %u seconds", |
| 136 | inet_ntoa(temp), (unsigned)server_config.conflict_time); |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 137 | add_lease(blank_chaddr, addr, server_config.conflict_time, NULL); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 138 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 142 | /* Find a new usable (we think) address. */ |
| 143 | uint32_t FAST_FUNC find_free_or_expired_address(void) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 144 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 145 | uint32_t addr; |
| 146 | struct dhcpOfferedAddr *oldest_lease = NULL; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 148 | addr = server_config.start_ip; /* addr is in host order here */ |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 149 | for (; addr <= server_config.end_ip; addr++) { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 150 | uint32_t net_addr; |
| 151 | struct dhcpOfferedAddr *lease; |
| 152 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 153 | /* ie, 192.168.55.0 */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 154 | if ((addr & 0xff) == 0) |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 155 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 156 | /* ie, 192.168.55.255 */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 157 | if ((addr & 0xff) == 0xff) |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 158 | continue; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 159 | net_addr = htonl(addr); |
| 160 | /* addr has a static lease? */ |
| 161 | if (reservedIp(server_config.static_leases, net_addr)) |
| 162 | continue; |
| 163 | |
| 164 | lease = find_lease_by_yiaddr(net_addr); |
| 165 | if (!lease) { |
| 166 | if (nobody_responds_to_arp(net_addr)) |
| 167 | return net_addr; |
| 168 | } else { |
| 169 | if (!oldest_lease || lease->expires < oldest_lease->expires) |
| 170 | oldest_lease = lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 173 | |
| 174 | if (oldest_lease && lease_expired(oldest_lease) |
| 175 | && nobody_responds_to_arp(oldest_lease->yiaddr) |
| 176 | ) { |
| 177 | return oldest_lease->yiaddr; |
| 178 | } |
| 179 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |