[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
| 3 | * Russ Dill <Russ.Dill@asu.edu> July 2001
|
---|
[2725] | 4 | *
|
---|
| 5 | * Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
[821] | 6 | */
|
---|
[1765] | 7 | #include "common.h"
|
---|
[821] | 8 | #include "dhcpd.h"
|
---|
| 9 |
|
---|
[1765] | 10 | /* Find the oldest expired lease, NULL if there are no expired leases */
|
---|
[2725] | 11 | static struct dyn_lease *oldest_expired_lease(void)
|
---|
[1765] | 12 | {
|
---|
[2725] | 13 | struct dyn_lease *oldest_lease = NULL;
|
---|
| 14 | leasetime_t oldest_time = time(NULL);
|
---|
[1765] | 15 | unsigned i;
|
---|
[821] | 16 |
|
---|
[2725] | 17 | /* Unexpired leases have g_leases[i].expires >= current time
|
---|
| 18 | * and therefore can't ever match */
|
---|
| 19 | for (i = 0; i < server_config.max_leases; i++) {
|
---|
| 20 | if (g_leases[i].expires < oldest_time) {
|
---|
| 21 | oldest_time = g_leases[i].expires;
|
---|
| 22 | oldest_lease = &g_leases[i];
|
---|
[1765] | 23 | }
|
---|
[2725] | 24 | }
|
---|
| 25 | return oldest_lease;
|
---|
[1765] | 26 | }
|
---|
[821] | 27 |
|
---|
[2725] | 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)
|
---|
[821] | 32 | {
|
---|
[2725] | 33 | unsigned i;
|
---|
[821] | 34 |
|
---|
[2725] | 35 | for (i = 0; i < server_config.max_leases; i++) {
|
---|
| 36 | if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
|
---|
| 37 | || (yiaddr && g_leases[i].lease_nip == yiaddr)
|
---|
[1765] | 38 | ) {
|
---|
[2725] | 39 | memset(&g_leases[i], 0, sizeof(g_leases[i]));
|
---|
[821] | 40 | }
|
---|
[2725] | 41 | }
|
---|
[821] | 42 | }
|
---|
| 43 |
|
---|
[2725] | 44 | /* Add a lease into the table, clearing out any old ones.
|
---|
| 45 | * If chaddr == NULL, this is a conflict lease.
|
---|
| 46 | */
|
---|
| 47 | struct dyn_lease* FAST_FUNC add_lease(
|
---|
| 48 | const uint8_t *chaddr, uint32_t yiaddr,
|
---|
| 49 | leasetime_t leasetime,
|
---|
| 50 | const char *hostname, int hostname_len)
|
---|
[821] | 51 | {
|
---|
[2725] | 52 | struct dyn_lease *oldest;
|
---|
[821] | 53 |
|
---|
| 54 | /* clean out any old ones */
|
---|
[2725] | 55 | clear_leases(chaddr, yiaddr);
|
---|
[821] | 56 |
|
---|
| 57 | oldest = oldest_expired_lease();
|
---|
| 58 |
|
---|
| 59 | if (oldest) {
|
---|
[2725] | 60 | memset(oldest, 0, sizeof(*oldest));
|
---|
| 61 | if (hostname) {
|
---|
| 62 | char *p;
|
---|
| 63 |
|
---|
| 64 | hostname_len++; /* include NUL */
|
---|
| 65 | if (hostname_len > sizeof(oldest->hostname))
|
---|
| 66 | hostname_len = sizeof(oldest->hostname);
|
---|
| 67 | p = safe_strncpy(oldest->hostname, hostname, hostname_len);
|
---|
| 68 | /* sanitization (s/non-ASCII/^/g) */
|
---|
| 69 | while (*p) {
|
---|
| 70 | if (*p < ' ' || *p > 126)
|
---|
| 71 | *p = '^';
|
---|
| 72 | p++;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | if (chaddr)
|
---|
| 76 | memcpy(oldest->lease_mac, chaddr, 6);
|
---|
| 77 | oldest->lease_nip = yiaddr;
|
---|
| 78 | oldest->expires = time(NULL) + leasetime;
|
---|
[821] | 79 | }
|
---|
| 80 |
|
---|
| 81 | return oldest;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[2725] | 84 | /* True if a lease has expired */
|
---|
| 85 | int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
|
---|
[821] | 86 | {
|
---|
[2725] | 87 | return (lease->expires < (leasetime_t) time(NULL));
|
---|
[821] | 88 | }
|
---|
| 89 |
|
---|
[2725] | 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)
|
---|
[821] | 92 | {
|
---|
[1765] | 93 | unsigned i;
|
---|
[821] | 94 |
|
---|
| 95 | for (i = 0; i < server_config.max_leases; i++)
|
---|
[2725] | 96 | if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
|
---|
| 97 | return &g_leases[i];
|
---|
[821] | 98 |
|
---|
| 99 | return NULL;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[2725] | 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)
|
---|
[821] | 104 | {
|
---|
[1765] | 105 | unsigned i;
|
---|
[821] | 106 |
|
---|
| 107 | for (i = 0; i < server_config.max_leases; i++)
|
---|
[2725] | 108 | if (g_leases[i].lease_nip == nip)
|
---|
| 109 | return &g_leases[i];
|
---|
[821] | 110 |
|
---|
| 111 | return NULL;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[2725] | 114 | /* Check if the IP is taken; if it is, add it to the lease table */
|
---|
| 115 | static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
|
---|
[821] | 116 | {
|
---|
| 117 | struct in_addr temp;
|
---|
[1765] | 118 | int r;
|
---|
[821] | 119 |
|
---|
[2725] | 120 | r = arpping(nip, safe_mac,
|
---|
| 121 | server_config.server_nip,
|
---|
| 122 | server_config.server_mac,
|
---|
| 123 | server_config.interface);
|
---|
[1765] | 124 | if (r)
|
---|
| 125 | return r;
|
---|
| 126 |
|
---|
[2725] | 127 | temp.s_addr = nip;
|
---|
[1765] | 128 | bb_info_msg("%s belongs to someone, reserving it for %u seconds",
|
---|
| 129 | inet_ntoa(temp), (unsigned)server_config.conflict_time);
|
---|
[2725] | 130 | add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
|
---|
[1765] | 131 | return 0;
|
---|
[821] | 132 | }
|
---|
| 133 |
|
---|
[2725] | 134 | /* Find a new usable (we think) address */
|
---|
| 135 | uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
|
---|
[821] | 136 | {
|
---|
[2725] | 137 | uint32_t addr;
|
---|
| 138 | struct dyn_lease *oldest_lease = NULL;
|
---|
[821] | 139 |
|
---|
[3232] | 140 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
|
---|
| 141 | uint32_t stop;
|
---|
| 142 | unsigned i, hash;
|
---|
| 143 |
|
---|
| 144 | /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
|
---|
| 145 | * dispersal even with similarly-valued "strings".
|
---|
| 146 | */
|
---|
| 147 | hash = 0;
|
---|
| 148 | for (i = 0; i < 6; i++)
|
---|
| 149 | hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
|
---|
| 150 |
|
---|
| 151 | /* pick a seed based on hwaddr then iterate until we find a free address. */
|
---|
| 152 | addr = server_config.start_ip
|
---|
| 153 | + (hash % (1 + server_config.end_ip - server_config.start_ip));
|
---|
| 154 | stop = addr;
|
---|
| 155 | #else
|
---|
| 156 | addr = server_config.start_ip;
|
---|
| 157 | #define stop (server_config.end_ip + 1)
|
---|
| 158 | #endif
|
---|
| 159 | do {
|
---|
[2725] | 160 | uint32_t nip;
|
---|
| 161 | struct dyn_lease *lease;
|
---|
| 162 |
|
---|
[821] | 163 | /* ie, 192.168.55.0 */
|
---|
[2725] | 164 | if ((addr & 0xff) == 0)
|
---|
[3232] | 165 | goto next_addr;
|
---|
[821] | 166 | /* ie, 192.168.55.255 */
|
---|
[2725] | 167 | if ((addr & 0xff) == 0xff)
|
---|
[3232] | 168 | goto next_addr;
|
---|
[2725] | 169 | nip = htonl(addr);
|
---|
[3232] | 170 | /* skip our own address */
|
---|
| 171 | if (nip == server_config.server_nip)
|
---|
| 172 | goto next_addr;
|
---|
[2725] | 173 | /* is this a static lease addr? */
|
---|
| 174 | if (is_nip_reserved(server_config.static_leases, nip))
|
---|
[3232] | 175 | goto next_addr;
|
---|
[2725] | 176 |
|
---|
| 177 | lease = find_lease_by_nip(nip);
|
---|
| 178 | if (!lease) {
|
---|
| 179 | //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
|
---|
| 180 | if (nobody_responds_to_arp(nip, safe_mac))
|
---|
| 181 | return nip;
|
---|
| 182 | } else {
|
---|
| 183 | if (!oldest_lease || lease->expires < oldest_lease->expires)
|
---|
| 184 | oldest_lease = lease;
|
---|
[821] | 185 | }
|
---|
[2725] | 186 |
|
---|
[3232] | 187 | next_addr:
|
---|
| 188 | addr++;
|
---|
| 189 | #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
|
---|
| 190 | if (addr > server_config.end_ip)
|
---|
| 191 | addr = server_config.start_ip;
|
---|
| 192 | #endif
|
---|
| 193 | } while (addr != stop);
|
---|
| 194 |
|
---|
[2725] | 195 | if (oldest_lease
|
---|
| 196 | && is_expired_lease(oldest_lease)
|
---|
| 197 | && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
|
---|
| 198 | ) {
|
---|
| 199 | return oldest_lease->lease_nip;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[821] | 202 | return 0;
|
---|
| 203 | }
|
---|