blob: bb08c3adbe7f7ea9d8cd5a197c8ad7e241c12b1f [file] [log] [blame]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001/*
2 * leases.c -- tools to manage DHCP leases
Russ Dill61fb4892002-10-14 21:41:28 +00003 * 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
Russ Dill61fb4892002-10-14 21:41:28 +000012#include "dhcpd.h"
13#include "files.h"
14#include "options.h"
Russ Dill76729b82003-12-16 20:44:15 +000015#include "leases.h"
Russ Dill61fb4892002-10-14 21:41:28 +000016#include "arpping.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000017#include "common.h"
18
Eric Andersenabf58d62004-10-08 08:49:26 +000019#include "static_leases.h"
20
Russ Dill61fb4892002-10-14 21:41:28 +000021
Eric Andersenad953732004-01-30 23:45:53 +000022uint8_t blank_chaddr[] = {[0 ... 15] = 0};
Russ Dill61fb4892002-10-14 21:41:28 +000023
24/* clear every lease out that chaddr OR yiaddr matches and is nonzero */
Eric Andersenad953732004-01-30 23:45:53 +000025void clear_lease(uint8_t *chaddr, uint32_t yiaddr)
Russ Dill61fb4892002-10-14 21:41:28 +000026{
27 unsigned int i, j;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000028
Russ Dill61fb4892002-10-14 21:41:28 +000029 for (j = 0; j < 16 && !chaddr[j]; j++);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000030
Russ Dill61fb4892002-10-14 21:41:28 +000031 for (i = 0; i < server_config.max_leases; i++)
32 if ((j != 16 && !memcmp(leases[i].chaddr, chaddr, 16)) ||
33 (yiaddr && leases[i].yiaddr == yiaddr)) {
34 memset(&(leases[i]), 0, sizeof(struct dhcpOfferedAddr));
35 }
36}
37
38
39/* add a lease into the table, clearing out any old ones */
Eric Andersenad953732004-01-30 23:45:53 +000040struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
Russ Dill61fb4892002-10-14 21:41:28 +000041{
42 struct dhcpOfferedAddr *oldest;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000043
Russ Dill61fb4892002-10-14 21:41:28 +000044 /* clean out any old ones */
45 clear_lease(chaddr, yiaddr);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000046
Russ Dill61fb4892002-10-14 21:41:28 +000047 oldest = oldest_expired_lease();
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048
Russ Dill61fb4892002-10-14 21:41:28 +000049 if (oldest) {
50 memcpy(oldest->chaddr, chaddr, 16);
51 oldest->yiaddr = yiaddr;
52 oldest->expires = time(0) + lease;
53 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Russ Dill61fb4892002-10-14 21:41:28 +000055 return oldest;
56}
57
58
59/* true if a lease has expired */
60int lease_expired(struct dhcpOfferedAddr *lease)
61{
62 return (lease->expires < (unsigned long) time(0));
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063}
Russ Dill61fb4892002-10-14 21:41:28 +000064
65
66/* Find the oldest expired lease, NULL if there are no expired leases */
67struct dhcpOfferedAddr *oldest_expired_lease(void)
68{
69 struct dhcpOfferedAddr *oldest = NULL;
70 unsigned long oldest_lease = time(0);
71 unsigned int i;
72
Eric Andersenc7bda1c2004-03-15 08:29:22 +000073
Russ Dill61fb4892002-10-14 21:41:28 +000074 for (i = 0; i < server_config.max_leases; i++)
75 if (oldest_lease > leases[i].expires) {
76 oldest_lease = leases[i].expires;
77 oldest = &(leases[i]);
78 }
79 return oldest;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000080
Russ Dill61fb4892002-10-14 21:41:28 +000081}
82
83
84/* Find the first lease that matches chaddr, NULL if no match */
Eric Andersenad953732004-01-30 23:45:53 +000085struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr)
Russ Dill61fb4892002-10-14 21:41:28 +000086{
87 unsigned int i;
88
89 for (i = 0; i < server_config.max_leases; i++)
90 if (!memcmp(leases[i].chaddr, chaddr, 16)) return &(leases[i]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091
Russ Dill61fb4892002-10-14 21:41:28 +000092 return NULL;
93}
94
95
96/* Find the first lease that matches yiaddr, NULL is no match */
Eric Andersenad953732004-01-30 23:45:53 +000097struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr)
Russ Dill61fb4892002-10-14 21:41:28 +000098{
99 unsigned int i;
100
101 for (i = 0; i < server_config.max_leases; i++)
102 if (leases[i].yiaddr == yiaddr) return &(leases[i]);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Russ Dill61fb4892002-10-14 21:41:28 +0000104 return NULL;
105}
106
107
Glenn L McGrath24833432003-06-10 17:22:49 +0000108/* check is an IP is taken, if it is, add it to the lease table */
Eric Andersenad953732004-01-30 23:45:53 +0000109static int check_ip(uint32_t addr)
Glenn L McGrath24833432003-06-10 17:22:49 +0000110{
111 struct in_addr temp;
112
113 if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
114 temp.s_addr = addr;
115 LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds",
116 inet_ntoa(temp), server_config.conflict_time);
117 add_lease(blank_chaddr, addr, server_config.conflict_time);
118 return 1;
119 } else return 0;
120}
121
Russ Dill4e864a32003-12-18 22:25:38 +0000122
Russ Dill61fb4892002-10-14 21:41:28 +0000123/* find an assignable address, it check_expired is true, we check all the expired leases as well.
124 * Maybe this should try expired leases by age... */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000125uint32_t find_address(int check_expired)
Russ Dill61fb4892002-10-14 21:41:28 +0000126{
Eric Andersenad953732004-01-30 23:45:53 +0000127 uint32_t addr, ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128 struct dhcpOfferedAddr *lease = NULL;
Russ Dill61fb4892002-10-14 21:41:28 +0000129
130 addr = ntohl(server_config.start); /* addr is in host order here */
131 for (;addr <= ntohl(server_config.end); addr++) {
132
133 /* ie, 192.168.55.0 */
134 if (!(addr & 0xFF)) continue;
135
136 /* ie, 192.168.55.255 */
137 if ((addr & 0xFF) == 0xFF) continue;
138
Eric Andersenabf58d62004-10-08 08:49:26 +0000139 /* Only do if it isn't an assigned as a static lease */
140 if(!reservedIp(server_config.static_leases, htonl(addr)))
141 {
142
Russ Dill61fb4892002-10-14 21:41:28 +0000143 /* lease is not taken */
144 ret = htonl(addr);
145 if ((!(lease = find_lease_by_yiaddr(ret)) ||
146
147 /* or it expired and we are checking for expired leases */
148 (check_expired && lease_expired(lease))) &&
149
150 /* and it isn't on the network */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000151 !check_ip(ret)) {
Russ Dill61fb4892002-10-14 21:41:28 +0000152 return ret;
153 break;
154 }
155 }
Eric Andersenabf58d62004-10-08 08:49:26 +0000156 }
Russ Dill61fb4892002-10-14 21:41:28 +0000157 return 0;
158}