1/*
2 * leases.c -- tools to manage DHCP leases
3 * Russ Dill <Russ.Dill@asu.edu> July 2001
4 */
5
6#include <time.h>
7#include <string.h>
8#include <sys/socket.h>
9#include <netinet/in.h>
10#include <arpa/inet.h>
11
12#include "debug.h"
13#include "dhcpd.h"
14#include "files.h"
15#include "options.h"
16#include "leases.h"
17#include "arpping.h"
18
19unsigned char blank_chaddr[] = {[0 ... 15] = 0};
20
21/* clear every lease out that chaddr OR yiaddr matches and is nonzero */
22void clear_lease(u_int8_t *chaddr, u_int32_t yiaddr)
23{
24	unsigned int i, j;
25
26	for (j = 0; j < 16 && !chaddr[j]; j++);
27
28	for (i = 0; i < server_config.max_leases; i++)
29		if ((j != 16 && !memcmp(leases[i].chaddr, chaddr, 16)) ||
30		    (yiaddr && leases[i].yiaddr == yiaddr)) {
31			memset(&(leases[i]), 0, sizeof(struct dhcpOfferedAddr));
32		}
33}
34
35
36/* add a lease into the table, clearing out any old ones */
37struct dhcpOfferedAddr *add_lease(u_int8_t *chaddr, u_int32_t yiaddr, unsigned long lease)
38{
39	struct dhcpOfferedAddr *oldest;
40
41	/* clean out any old ones */
42	clear_lease(chaddr, yiaddr);
43
44	oldest = oldest_expired_lease();
45
46	if (oldest) {
47		memcpy(oldest->chaddr, chaddr, 16);
48		oldest->yiaddr = yiaddr;
49		oldest->expires = time(0) + lease;
50	}
51
52	return oldest;
53}
54
55
56/* true if a lease has expired */
57int lease_expired(struct dhcpOfferedAddr *lease)
58{
59	return (lease->expires < (unsigned long) time(0));
60}
61
62
63/* Find the oldest expired lease, NULL if there are no expired leases */
64struct dhcpOfferedAddr *oldest_expired_lease(void)
65{
66	struct dhcpOfferedAddr *oldest = NULL;
67	unsigned long oldest_lease = time(0);
68	unsigned int i;
69
70
71	for (i = 0; i < server_config.max_leases; i++)
72		if (oldest_lease > leases[i].expires) {
73			oldest_lease = leases[i].expires;
74			oldest = &(leases[i]);
75		}
76	return oldest;
77
78}
79
80
81/* Find the first lease that matches chaddr, NULL if no match */
82struct dhcpOfferedAddr *find_lease_by_chaddr(u_int8_t *chaddr)
83{
84	unsigned int i;
85
86	for (i = 0; i < server_config.max_leases; i++)
87		if (!memcmp(leases[i].chaddr, chaddr, 16)) return &(leases[i]);
88
89	return NULL;
90}
91
92
93/* Find the first lease that matches yiaddr, NULL is no match */
94struct dhcpOfferedAddr *find_lease_by_yiaddr(u_int32_t yiaddr)
95{
96	unsigned int i;
97
98	for (i = 0; i < server_config.max_leases; i++)
99		if (leases[i].yiaddr == yiaddr) return &(leases[i]);
100
101	return NULL;
102}
103
104/*find and assignable address and check if the client hw address is a reserved address.*/
105u_int32_t find_address2(int check_expired, unsigned char *chaddr)
106{
107	u_int32_t addr, ret;
108	struct dhcpOfferedAddr *lease = NULL;
109
110    /* First check for reserved IP for this mac */
111    addr = find_reserved_ip(chaddr);
112    if (addr) {
113        ret = htonl(addr);
114
115        /* Make sure this IP is not used in network */
116        if (!check_ip(addr)) {
117            LOG(LOG_INFO, "find_address2: found reserved ip 0x%x.\n", ret);
118            ret = htonl(addr);
119            return ret;
120        }
121    }
122
123	addr = ntohl(server_config.start); /* addr is in host order here */
124
125	for (;addr <= ntohl(server_config.end); addr++) {
126
127        LOG(LOG_INFO, "find_address2: assign ip 0x%x.\n", addr);
128            /* ie, 192.168.55.0 */
129        if (!(addr & 0xFF)) continue;
130
131        /* ie, 192.168.55.255 */
132        if ((addr & 0xFF) == 0xFF) continue;
133
134            /*to check if this is a reserved ip for this mac or this ip has been reserved for others*/
135            //if ( !check_reserved_ip(addr, chaddr) )  //foxconn modified, wenchia, 2007/09/10
136            if ( !check_reserved_ip(htonl(addr), chaddr) )
137            {
138                LOG(LOG_INFO, "find_address2: ip 0x%x has beed reserved.\n", addr);
139                continue;
140            }
141
142        /* lease is not taken */
143        ret = htonl(addr);
144        if ((!(lease = find_lease_by_yiaddr(ret)) ||
145
146                /* or it expired and we are checking for expired leases */
147                (check_expired  && lease_expired(lease))) &&
148
149                /* and it isn't on the network */
150                    !check_ip(ret)) {
151            return ret;
152            break;
153        }
154	}
155	return 0;
156}
157
158/* find an assignable address, it check_expired is true, we check all the expired leases as well.
159 * Maybe this should try expired leases by age... */
160u_int32_t find_address(int check_expired)
161{
162	u_int32_t addr, ret;
163	struct dhcpOfferedAddr *lease = NULL;
164
165	addr = ntohl(server_config.start); /* addr is in host order here */
166	for (;addr <= ntohl(server_config.end); addr++) {
167
168		/* ie, 192.168.55.0 */
169		if (!(addr & 0xFF)) continue;
170
171		/* ie, 192.168.55.255 */
172		if ((addr & 0xFF) == 0xFF) continue;
173
174		/* lease is not taken */
175		ret = htonl(addr);
176		if ((!(lease = find_lease_by_yiaddr(ret)) ||
177
178		     /* or it expired and we are checking for expired leases */
179		     (check_expired  && lease_expired(lease))) &&
180
181		     /* and it isn't on the network */
182	    	     !check_ip(ret)) {
183			return ret;
184			break;
185		}
186	}
187	return 0;
188}
189
190
191/* check is an IP is taken, if it is, add it to the lease table */
192int check_ip(u_int32_t addr)
193{
194	struct in_addr temp;
195
196	if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
197		temp.s_addr = addr;
198	 	LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
199	 		inet_ntoa(temp), server_config.conflict_time);
200        /* Any need to reserve this IP ??? */
201		/* add_lease(blank_chaddr, addr, server_config.conflict_time); */
202		return 1;
203	} else return 0;
204}
205
206