OSDN Git Service

*: make GNU licensing statement forms more regular
[android-x86/external-busybox.git] / networking / udhcp / leases.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Russ Dill <Russ.Dill@asu.edu> July 2001
4  *
5  * Licensed under GPLv2, see file LICENSE in this source tree.
6  */
7 #include "common.h"
8 #include "dhcpd.h"
9
10 /* Find the oldest expired lease, NULL if there are no expired leases */
11 static struct dyn_lease *oldest_expired_lease(void)
12 {
13         struct dyn_lease *oldest_lease = NULL;
14         leasetime_t oldest_time = time(NULL);
15         unsigned i;
16
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];
23                 }
24         }
25         return oldest_lease;
26 }
27
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)
32 {
33         unsigned i;
34
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)
38                 ) {
39                         memset(&g_leases[i], 0, sizeof(g_leases[i]));
40                 }
41         }
42 }
43
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)
51 {
52         struct dyn_lease *oldest;
53
54         /* clean out any old ones */
55         clear_leases(chaddr, yiaddr);
56
57         oldest = oldest_expired_lease();
58
59         if (oldest) {
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;
79         }
80
81         return oldest;
82 }
83
84 /* True if a lease has expired */
85 int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
86 {
87         return (lease->expires < (leasetime_t) time(NULL));
88 }
89
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)
92 {
93         unsigned i;
94
95         for (i = 0; i < server_config.max_leases; i++)
96                 if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
97                         return &g_leases[i];
98
99         return NULL;
100 }
101
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)
104 {
105         unsigned i;
106
107         for (i = 0; i < server_config.max_leases; i++)
108                 if (g_leases[i].lease_nip == nip)
109                         return &g_leases[i];
110
111         return NULL;
112 }
113
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)
116 {
117         struct in_addr temp;
118         int r;
119
120         r = arpping(nip, safe_mac,
121                         server_config.server_nip,
122                         server_config.server_mac,
123                         server_config.interface);
124         if (r)
125                 return r;
126
127         temp.s_addr = nip;
128         bb_info_msg("%s belongs to someone, reserving it for %u seconds",
129                 inet_ntoa(temp), (unsigned)server_config.conflict_time);
130         add_lease(NULL, nip, server_config.conflict_time, NULL, 0);
131         return 0;
132 }
133
134 /* Find a new usable (we think) address */
135 uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
136 {
137         uint32_t addr;
138         struct dyn_lease *oldest_lease = NULL;
139
140         addr = server_config.start_ip; /* addr is in host order here */
141         for (; addr <= server_config.end_ip; addr++) {
142                 uint32_t nip;
143                 struct dyn_lease *lease;
144
145                 /* ie, 192.168.55.0 */
146                 if ((addr & 0xff) == 0)
147                         continue;
148                 /* ie, 192.168.55.255 */
149                 if ((addr & 0xff) == 0xff)
150                         continue;
151                 nip = htonl(addr);
152                 /* is this a static lease addr? */
153                 if (is_nip_reserved(server_config.static_leases, nip))
154                         continue;
155
156                 lease = find_lease_by_nip(nip);
157                 if (!lease) {
158 //TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
159                         if (nobody_responds_to_arp(nip, safe_mac))
160                                 return nip;
161                 } else {
162                         if (!oldest_lease || lease->expires < oldest_lease->expires)
163                                 oldest_lease = lease;
164                 }
165         }
166
167         if (oldest_lease
168          && is_expired_lease(oldest_lease)
169          && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
170         ) {
171                 return oldest_lease->lease_nip;
172         }
173
174         return 0;
175 }