busybox: update to 1.25.0
[tomato.git] / release / src / router / busybox / networking / udhcp / leases.c
blob6642e396d61d8b7d19a996cecbf45ab5e80585bc
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Russ Dill <Russ.Dill@asu.edu> July 2001
5 * Licensed under GPLv2, see file LICENSE in this source tree.
6 */
7 #include "common.h"
8 #include "dhcpd.h"
10 /* Find the oldest expired lease, NULL if there are no expired leases */
11 static struct dyn_lease *oldest_expired_lease(void)
13 struct dyn_lease *oldest_lease = NULL;
14 leasetime_t oldest_time = time(NULL);
15 unsigned i;
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 == 0 /* empty entry */
21 || g_leases[i].expires < oldest_time
22 ) {
23 oldest_time = g_leases[i].expires;
24 oldest_lease = &g_leases[i];
27 return oldest_lease;
30 /* Clear out all leases with matching nonzero chaddr OR yiaddr.
31 * If chaddr == NULL, this is a conflict lease.
33 static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
35 unsigned i;
37 for (i = 0; i < server_config.max_leases; i++) {
38 if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
39 || (yiaddr && g_leases[i].lease_nip == yiaddr)
40 ) {
41 memset(&g_leases[i], 0, sizeof(g_leases[i]));
46 /* Add a lease into the table, clearing out any old ones.
47 * If chaddr == NULL, this is a conflict lease.
49 struct dyn_lease* FAST_FUNC add_lease(
50 const uint8_t *chaddr, uint32_t yiaddr,
51 leasetime_t leasetime,
52 const char *hostname, int hostname_len)
54 struct dyn_lease *oldest;
56 /* clean out any old ones */
57 clear_leases(chaddr, yiaddr);
59 oldest = oldest_expired_lease();
61 if (oldest) {
62 memset(oldest, 0, sizeof(*oldest));
63 if (hostname) {
64 char *p;
66 hostname_len++; /* include NUL */
67 if (hostname_len > sizeof(oldest->hostname))
68 hostname_len = sizeof(oldest->hostname);
69 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
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.
76 while (*p) {
77 if (!isalnum(*p) && *p != '-' && *p != '_')
78 *p = '.';
79 p++;
82 if (chaddr)
83 memcpy(oldest->lease_mac, chaddr, 6);
84 oldest->lease_nip = yiaddr;
85 oldest->expires = time(NULL) + leasetime;
88 return oldest;
91 /* True if a lease has expired */
92 int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
94 return (lease->expires < (leasetime_t) time(NULL));
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)
100 unsigned i;
102 for (i = 0; i < server_config.max_leases; i++)
103 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
104 return &g_leases[i];
106 return NULL;
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)
112 unsigned i;
114 for (i = 0; i < server_config.max_leases; i++)
115 if (g_leases[i].lease_nip == nip)
116 return &g_leases[i];
118 return NULL;
121 /* Check if the IP is taken; if it is, add it to the lease table */
122 static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms)
124 struct in_addr temp;
125 int r;
127 r = arpping(nip, safe_mac,
128 server_config.server_nip,
129 server_config.server_mac,
130 server_config.interface,
131 arpping_ms);
132 if (r)
133 return r;
135 temp.s_addr = nip;
136 bb_error_msg("%s belongs to someone, reserving it for %u seconds",
137 inet_ntoa(temp), (unsigned)server_config.conflict_time);
138 add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
139 return 0;
142 /* Find a new usable (we think) address */
143 uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms)
145 uint32_t addr;
146 struct dyn_lease *oldest_lease = NULL;
148 #if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
149 uint32_t stop;
150 unsigned i, hash;
152 /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
153 * dispersal even with similarly-valued "strings".
155 hash = 0;
156 for (i = 0; i < 6; i++)
157 hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
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 {
168 uint32_t nip;
169 struct dyn_lease *lease;
171 /* ie, 192.168.55.0 */
172 if ((addr & 0xff) == 0)
173 goto next_addr;
174 /* ie, 192.168.55.255 */
175 if ((addr & 0xff) == 0xff)
176 goto next_addr;
177 nip = htonl(addr);
178 /* skip our own address */
179 if (nip == server_config.server_nip)
180 goto next_addr;
181 /* is this a static lease addr? */
182 if (is_nip_reserved(server_config.static_leases, nip))
183 goto next_addr;
185 lease = find_lease_by_nip(nip);
186 if (!lease) {
187 //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
188 if (nobody_responds_to_arp(nip, safe_mac, arpping_ms))
189 return nip;
190 } else {
191 if (!oldest_lease || lease->expires < oldest_lease->expires)
192 oldest_lease = lease;
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);
203 if (oldest_lease
204 && is_expired_lease(oldest_lease)
205 && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms)
207 return oldest_lease->lease_nip;
210 return 0;