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