Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * leases.c -- tools to manage DHCP leases |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 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 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 12 | #include "dhcpd.h" |
| 13 | #include "files.h" |
| 14 | #include "options.h" |
Russ Dill | 76729b8 | 2003-12-16 20:44:15 +0000 | [diff] [blame] | 15 | #include "leases.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 16 | #include "arpping.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 17 | #include "common.h" |
| 18 | |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame^] | 19 | #include "static_leases.h" |
| 20 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 21 | |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 22 | uint8_t blank_chaddr[] = {[0 ... 15] = 0}; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 23 | |
| 24 | /* clear every lease out that chaddr OR yiaddr matches and is nonzero */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 25 | void clear_lease(uint8_t *chaddr, uint32_t yiaddr) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 26 | { |
| 27 | unsigned int i, j; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 28 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 29 | for (j = 0; j < 16 && !chaddr[j]; j++); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 30 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 31 | 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 Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 40 | struct dhcpOfferedAddr *add_lease(uint8_t *chaddr, uint32_t yiaddr, unsigned long lease) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 41 | { |
| 42 | struct dhcpOfferedAddr *oldest; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 43 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 44 | /* clean out any old ones */ |
| 45 | clear_lease(chaddr, yiaddr); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 46 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 47 | oldest = oldest_expired_lease(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 48 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 49 | if (oldest) { |
| 50 | memcpy(oldest->chaddr, chaddr, 16); |
| 51 | oldest->yiaddr = yiaddr; |
| 52 | oldest->expires = time(0) + lease; |
| 53 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 54 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 55 | return oldest; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* true if a lease has expired */ |
| 60 | int lease_expired(struct dhcpOfferedAddr *lease) |
| 61 | { |
| 62 | return (lease->expires < (unsigned long) time(0)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 63 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | /* Find the oldest expired lease, NULL if there are no expired leases */ |
| 67 | struct dhcpOfferedAddr *oldest_expired_lease(void) |
| 68 | { |
| 69 | struct dhcpOfferedAddr *oldest = NULL; |
| 70 | unsigned long oldest_lease = time(0); |
| 71 | unsigned int i; |
| 72 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 73 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 74 | 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 80 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | /* Find the first lease that matches chaddr, NULL if no match */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 85 | struct dhcpOfferedAddr *find_lease_by_chaddr(uint8_t *chaddr) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 86 | { |
| 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 91 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* Find the first lease that matches yiaddr, NULL is no match */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 97 | struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 98 | { |
| 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 103 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 108 | /* check is an IP is taken, if it is, add it to the lease table */ |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 109 | static int check_ip(uint32_t addr) |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 110 | { |
| 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 Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 122 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 123 | /* 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 Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 125 | uint32_t find_address(int check_expired) |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 126 | { |
Eric Andersen | ad95373 | 2004-01-30 23:45:53 +0000 | [diff] [blame] | 127 | uint32_t addr, ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 128 | struct dhcpOfferedAddr *lease = NULL; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 129 | |
| 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 Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame^] | 139 | /* Only do if it isn't an assigned as a static lease */ |
| 140 | if(!reservedIp(server_config.static_leases, htonl(addr))) |
| 141 | { |
| 142 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 143 | /* 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 */ |
| 151 | !check_ip(ret)) { |
| 152 | return ret; |
| 153 | break; |
| 154 | } |
| 155 | } |
Eric Andersen | abf58d6 | 2004-10-08 08:49:26 +0000 | [diff] [blame^] | 156 | } |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |