"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 */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 14 | static struct dyn_lease *oldest_expired_lease(void) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 15 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 16 | struct dyn_lease *oldest_lease = NULL; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 17 | leasetime_t oldest_time = time(NULL); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 18 | unsigned i; |
| 19 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 20 | /* Unexpired leases have g_leases[i].expires >= current time |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 21 | * and therefore can't ever match */ |
| 22 | for (i = 0; i < server_config.max_leases; i++) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 23 | if (g_leases[i].expires < oldest_time) { |
| 24 | oldest_time = g_leases[i].expires; |
| 25 | oldest_lease = &g_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 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 32 | /* Clear out all leases with matching nonzero chaddr OR yiaddr. |
| 33 | * If chaddr == NULL, this is a conflict lease. |
| 34 | */ |
| 35 | static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 37 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 39 | for (i = 0; i < server_config.max_leases; i++) { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 40 | if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 41 | || (yiaddr && g_leases[i].lease_nip == yiaddr) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 42 | ) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 43 | memset(&g_leases[i], 0, sizeof(g_leases[i])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 44 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 45 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 49 | /* Add a lease into the table, clearing out any old ones |
| 50 | * If chaddr == NULL, this is a conflict lease. |
| 51 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 52 | struct dyn_lease* FAST_FUNC add_lease( |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 53 | const uint8_t *chaddr, uint32_t yiaddr, |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 54 | leasetime_t leasetime, |
| 55 | const char *hostname, int hostname_len) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 56 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 57 | struct dyn_lease *oldest; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | |
| 59 | /* clean out any old ones */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 60 | clear_leases(chaddr, yiaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 61 | |
| 62 | oldest = oldest_expired_lease(); |
| 63 | |
| 64 | if (oldest) { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 65 | memset(oldest, 0, sizeof(*oldest)); |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 66 | if (hostname) { |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 67 | char *p; |
| 68 | if (hostname_len > sizeof(oldest->hostname)) |
| 69 | hostname_len = sizeof(oldest->hostname); |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 70 | p = safe_strncpy(oldest->hostname, hostname, hostname_len); |
Denis Vlasenko | 551ffdc | 2009-04-01 19:48:05 +0000 | [diff] [blame] | 71 | /* sanitization (s/non-ASCII/^/g) */ |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 72 | while (*p) { |
| 73 | if (*p < ' ' || *p > 126) |
| 74 | *p = '^'; |
| 75 | p++; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 78 | if (chaddr) |
| 79 | memcpy(oldest->lease_mac, chaddr, 6); |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 80 | oldest->lease_nip = yiaddr; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 81 | oldest->expires = time(NULL) + leasetime; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return oldest; |
| 85 | } |
| 86 | |
| 87 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 88 | /* True if a lease has expired */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 89 | int FAST_FUNC is_expired_lease(struct dyn_lease *lease) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 91 | return (lease->expires < (leasetime_t) time(NULL)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 95 | /* Find the first lease that matches MAC, NULL if no match */ |
| 96 | struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 98 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 99 | |
| 100 | for (i = 0; i < server_config.max_leases; i++) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 101 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) |
| 102 | return &g_leases[i]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 108 | /* Find the first lease that matches IP, NULL is no match */ |
| 109 | struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 110 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 111 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | |
| 113 | for (i = 0; i < server_config.max_leases; i++) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 114 | if (g_leases[i].lease_nip == nip) |
| 115 | return &g_leases[i]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
| 120 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 121 | /* Check if the IP is taken; if it is, add it to the lease table */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 122 | static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 123 | { |
| 124 | struct in_addr temp; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 125 | int r; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 126 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 127 | r = arpping(nip, safe_mac, |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 128 | server_config.server_nip, |
| 129 | server_config.server_mac, |
Denys Vlasenko | 47f2d7e | 2009-06-16 10:20:27 +0200 | [diff] [blame] | 130 | server_config.interface); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 131 | if (r) |
| 132 | return r; |
| 133 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 134 | temp.s_addr = nip; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 135 | bb_info_msg("%s belongs to someone, reserving it for %u seconds", |
| 136 | inet_ntoa(temp), (unsigned)server_config.conflict_time); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 137 | add_lease(NULL, nip, server_config.conflict_time, NULL, 0); |
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 | |
Denys Vlasenko | ed8982b | 2009-06-16 12:05:21 +0200 | [diff] [blame] | 142 | /* Find a new usable (we think) address */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 143 | uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac) |
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; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 146 | struct dyn_lease *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++) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 150 | uint32_t nip; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 151 | struct dyn_lease *lease; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 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; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 159 | nip = htonl(addr); |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 160 | /* is this a static lease addr? */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 161 | if (is_nip_reserved(server_config.static_leases, nip)) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 162 | continue; |
| 163 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 164 | lease = find_lease_by_nip(nip); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 165 | if (!lease) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 166 | if (nobody_responds_to_arp(nip, safe_mac)) |
| 167 | return nip; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame^] | 174 | if (oldest_lease |
| 175 | && is_expired_lease(oldest_lease) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 176 | && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 177 | ) { |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 178 | return oldest_lease->lease_nip; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | } |