"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 | /* |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 3 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 6 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 8 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 10 | /* Find the oldest expired lease, NULL if there are no expired leases */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 11 | static struct dyn_lease *oldest_expired_lease(void) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 12 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 13 | struct dyn_lease *oldest_lease = NULL; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 14 | leasetime_t oldest_time = time(NULL); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 15 | unsigned i; |
| 16 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 17 | /* Unexpired leases have g_leases[i].expires >= current time |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 18 | * and therefore can't ever match */ |
| 19 | for (i = 0; i < server_config.max_leases; i++) { |
Christian Lindeberg | ea2b71b | 2016-03-01 19:23:22 +0100 | [diff] [blame] | 20 | if (g_leases[i].expires == 0 /* empty entry */ |
| 21 | || g_leases[i].expires < oldest_time |
| 22 | ) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 23 | oldest_time = g_leases[i].expires; |
| 24 | oldest_lease = &g_leases[i]; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 25 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 26 | } |
| 27 | return oldest_lease; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 30 | /* Clear out all leases with matching nonzero chaddr OR yiaddr. |
| 31 | * If chaddr == NULL, this is a conflict lease. |
| 32 | */ |
| 33 | static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 35 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 37 | for (i = 0; i < server_config.max_leases; i++) { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 38 | if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 39 | || (yiaddr && g_leases[i].lease_nip == yiaddr) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 40 | ) { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 41 | memset(&g_leases[i], 0, sizeof(g_leases[i])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 42 | } |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 43 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 46 | /* Add a lease into the table, clearing out any old ones. |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 47 | * If chaddr == NULL, this is a conflict lease. |
| 48 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 49 | struct dyn_lease* FAST_FUNC add_lease( |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 50 | const uint8_t *chaddr, uint32_t yiaddr, |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 51 | leasetime_t leasetime, |
| 52 | const char *hostname, int hostname_len) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 53 | { |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 54 | struct dyn_lease *oldest; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 55 | |
| 56 | /* clean out any old ones */ |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 57 | clear_leases(chaddr, yiaddr); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 58 | |
| 59 | oldest = oldest_expired_lease(); |
| 60 | |
| 61 | if (oldest) { |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 62 | memset(oldest, 0, sizeof(*oldest)); |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 63 | if (hostname) { |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 64 | char *p; |
Denys Vlasenko | fde3fb3 | 2010-05-02 14:17:07 +0200 | [diff] [blame] | 65 | |
| 66 | hostname_len++; /* include NUL */ |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 67 | if (hostname_len > sizeof(oldest->hostname)) |
| 68 | hostname_len = sizeof(oldest->hostname); |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 69 | p = safe_strncpy(oldest->hostname, hostname, hostname_len); |
Denys Vlasenko | 9a51217 | 2015-10-24 04:45:22 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Sanitization (s/bad_char/./g). |
| 72 | * The intent is not to allow only "DNS-valid" hostnames, |
| 73 | * but merely make dumpleases output safe for shells to use. |
| 74 | * We accept "0-9A-Za-z._-", all other chars turn to dots. |
| 75 | */ |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 76 | while (*p) { |
Denys Vlasenko | 9a51217 | 2015-10-24 04:45:22 +0200 | [diff] [blame] | 77 | if (!isalnum(*p) && *p != '-' && *p != '_') |
| 78 | *p = '.'; |
Denys Vlasenko | 95cc814 | 2009-07-07 14:59:30 +0200 | [diff] [blame] | 79 | p++; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 82 | if (chaddr) |
| 83 | memcpy(oldest->lease_mac, chaddr, 6); |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 84 | oldest->lease_nip = yiaddr; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 85 | oldest->expires = time(NULL) + leasetime; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return oldest; |
| 89 | } |
| 90 | |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 91 | /* True if a lease has expired */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 92 | int FAST_FUNC is_expired_lease(struct dyn_lease *lease) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 93 | { |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 94 | return (lease->expires < (leasetime_t) time(NULL)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 97 | /* Find the first lease that matches MAC, NULL if no match */ |
| 98 | 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] | 99 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 100 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 101 | |
| 102 | for (i = 0; i < server_config.max_leases; i++) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 103 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) |
| 104 | return &g_leases[i]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 105 | |
| 106 | return NULL; |
| 107 | } |
| 108 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 109 | /* Find the first lease that matches IP, NULL is no match */ |
| 110 | struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 111 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 112 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 113 | |
| 114 | for (i = 0; i < server_config.max_leases; i++) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 115 | if (g_leases[i].lease_nip == nip) |
| 116 | return &g_leases[i]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 117 | |
| 118 | return NULL; |
| 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 */ |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 122 | static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms) |
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, |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 130 | server_config.interface, |
| 131 | arpping_ms); |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 132 | if (r) |
| 133 | return r; |
| 134 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 135 | temp.s_addr = nip; |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 136 | bb_error_msg("%s belongs to someone, reserving it for %u seconds", |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 137 | inet_ntoa(temp), (unsigned)server_config.conflict_time); |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 138 | add_lease(NULL, nip, server_config.conflict_time, NULL, 0); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 139 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Denys Vlasenko | ed8982b | 2009-06-16 12:05:21 +0200 | [diff] [blame] | 142 | /* Find a new usable (we think) address */ |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 143 | uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms) |
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 | |
Vladislav Grishenko | 5827167 | 2011-02-16 13:31:30 +0100 | [diff] [blame] | 148 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC |
| 149 | uint32_t stop; |
| 150 | unsigned i, hash; |
| 151 | |
| 152 | /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good |
| 153 | * dispersal even with similarly-valued "strings". |
| 154 | */ |
| 155 | hash = 0; |
| 156 | for (i = 0; i < 6; i++) |
| 157 | hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash; |
| 158 | |
| 159 | /* pick a seed based on hwaddr then iterate until we find a free address. */ |
| 160 | addr = server_config.start_ip |
| 161 | + (hash % (1 + server_config.end_ip - server_config.start_ip)); |
| 162 | stop = addr; |
| 163 | #else |
| 164 | addr = server_config.start_ip; |
| 165 | #define stop (server_config.end_ip + 1) |
| 166 | #endif |
| 167 | do { |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 168 | uint32_t nip; |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 169 | struct dyn_lease *lease; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 170 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 171 | /* ie, 192.168.55.0 */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 172 | if ((addr & 0xff) == 0) |
Vladislav Grishenko | 5827167 | 2011-02-16 13:31:30 +0100 | [diff] [blame] | 173 | goto next_addr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 174 | /* ie, 192.168.55.255 */ |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 175 | if ((addr & 0xff) == 0xff) |
Vladislav Grishenko | 5827167 | 2011-02-16 13:31:30 +0100 | [diff] [blame] | 176 | goto next_addr; |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 177 | nip = htonl(addr); |
Vladislav Grishenko | 5827167 | 2011-02-16 13:31:30 +0100 | [diff] [blame] | 178 | /* skip our own address */ |
| 179 | if (nip == server_config.server_nip) |
| 180 | goto next_addr; |
Denys Vlasenko | cab3a01 | 2009-06-16 12:03:12 +0200 | [diff] [blame] | 181 | /* is this a static lease addr? */ |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 182 | if (is_nip_reserved(server_config.static_leases, nip)) |
Vladislav Grishenko | 5827167 | 2011-02-16 13:31:30 +0100 | [diff] [blame] | 183 | goto next_addr; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 184 | |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 185 | lease = find_lease_by_nip(nip); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 186 | if (!lease) { |
Denys Vlasenko | 7724c76 | 2010-03-26 09:32:09 +0100 | [diff] [blame] | 187 | //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping! |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 188 | if (nobody_responds_to_arp(nip, safe_mac, arpping_ms)) |
Denys Vlasenko | 6947d2c | 2009-06-17 13:24:03 +0200 | [diff] [blame] | 189 | return nip; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 190 | } else { |
| 191 | if (!oldest_lease || lease->expires < oldest_lease->expires) |
| 192 | oldest_lease = lease; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 193 | } |
Vladislav Grishenko | 5827167 | 2011-02-16 13:31:30 +0100 | [diff] [blame] | 194 | |
| 195 | next_addr: |
| 196 | addr++; |
| 197 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC |
| 198 | if (addr > server_config.end_ip) |
| 199 | addr = server_config.start_ip; |
| 200 | #endif |
| 201 | } while (addr != stop); |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 202 | |
Denys Vlasenko | e5ce91b | 2010-03-21 00:43:11 +0100 | [diff] [blame] | 203 | if (oldest_lease |
| 204 | && is_expired_lease(oldest_lease) |
Michel Stam | 9f41271 | 2014-10-30 11:59:04 +0100 | [diff] [blame] | 205 | && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms) |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 206 | ) { |
Denys Vlasenko | 1d924f5 | 2009-06-16 10:23:01 +0200 | [diff] [blame] | 207 | return oldest_lease->lease_nip; |
Denis Vlasenko | 0416e3d | 2009-01-01 17:52:09 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |