"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * leases.c -- tools to manage DHCP leases |
| 4 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 5 | */ |
| 6 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 7 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 8 | #include "dhcpd.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | |
| 10 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 11 | /* Find the oldest expired lease, NULL if there are no expired leases */ |
| 12 | static struct dhcpOfferedAddr *oldest_expired_lease(void) |
| 13 | { |
| 14 | struct dhcpOfferedAddr *oldest = NULL; |
| 15 | unsigned long oldest_lease = time(0); |
| 16 | unsigned i; |
| 17 | |
| 18 | |
| 19 | for (i = 0; i < server_config.max_leases; i++) |
| 20 | if (oldest_lease > leases[i].expires) { |
| 21 | oldest_lease = leases[i].expires; |
| 22 | oldest = &(leases[i]); |
| 23 | } |
| 24 | return oldest; |
| 25 | } |
| 26 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | |
| 28 | /* clear every lease out that chaddr OR yiaddr matches and is nonzero */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 29 | static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 30 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 31 | unsigned i, j; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 32 | |
| 33 | for (j = 0; j < 16 && !chaddr[j]; j++); |
| 34 | |
| 35 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 36 | if ((j != 16 && memcmp(leases[i].chaddr, chaddr, 16) != 0) |
| 37 | || (yiaddr && leases[i].yiaddr == yiaddr) |
| 38 | ) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 39 | memset(&(leases[i]), 0, sizeof(struct dhcpOfferedAddr)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* add a lease into the table, clearing out any old ones */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 45 | struct dhcpOfferedAddr *add_lease(const uint8_t *chaddr, uint32_t yiaddr, unsigned long lease) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 46 | { |
| 47 | struct dhcpOfferedAddr *oldest; |
| 48 | |
| 49 | /* clean out any old ones */ |
| 50 | clear_lease(chaddr, yiaddr); |
| 51 | |
| 52 | oldest = oldest_expired_lease(); |
| 53 | |
| 54 | if (oldest) { |
| 55 | memcpy(oldest->chaddr, chaddr, 16); |
| 56 | oldest->yiaddr = yiaddr; |
| 57 | oldest->expires = time(0) + lease; |
| 58 | } |
| 59 | |
| 60 | return oldest; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* true if a lease has expired */ |
| 65 | int lease_expired(struct dhcpOfferedAddr *lease) |
| 66 | { |
| 67 | return (lease->expires < (unsigned long) time(0)); |
| 68 | } |
| 69 | |
| 70 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 71 | /* Find the first lease that matches chaddr, NULL if no match */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 72 | struct dhcpOfferedAddr *find_lease_by_chaddr(const uint8_t *chaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 73 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 74 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | |
| 76 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 77 | if (!memcmp(leases[i].chaddr, chaddr, 16)) |
| 78 | return &(leases[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 79 | |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* Find the first lease that matches yiaddr, NULL is no match */ |
| 85 | struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr) |
| 86 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 87 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 88 | |
| 89 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 90 | if (leases[i].yiaddr == yiaddr) |
| 91 | return &(leases[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /* check is an IP is taken, if it is, add it to the lease table */ |
| 98 | static int check_ip(uint32_t addr) |
| 99 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 100 | static const uint8_t blank_chaddr[16]; /* 16 zero bytes */ |
| 101 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 102 | struct in_addr temp; |
| 103 | |
| 104 | if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) { |
| 105 | temp.s_addr = addr; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 106 | bb_info_msg("%s belongs to someone, reserving it for %ld seconds", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 107 | inet_ntoa(temp), server_config.conflict_time); |
| 108 | add_lease(blank_chaddr, addr, server_config.conflict_time); |
| 109 | return 1; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 110 | } |
| 111 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | |
| 115 | /* find an assignable address, it check_expired is true, we check all the expired leases as well. |
| 116 | * Maybe this should try expired leases by age... */ |
| 117 | uint32_t find_address(int check_expired) |
| 118 | { |
| 119 | uint32_t addr, ret; |
| 120 | struct dhcpOfferedAddr *lease = NULL; |
| 121 | |
| 122 | addr = ntohl(server_config.start); /* addr is in host order here */ |
| 123 | for (;addr <= ntohl(server_config.end); addr++) { |
| 124 | |
| 125 | /* ie, 192.168.55.0 */ |
| 126 | if (!(addr & 0xFF)) continue; |
| 127 | |
| 128 | /* ie, 192.168.55.255 */ |
| 129 | if ((addr & 0xFF) == 0xFF) continue; |
| 130 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 131 | /* Only do if it isn't assigned as a static lease */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 132 | if (!reservedIp(server_config.static_leases, htonl(addr))) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 133 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 134 | /* lease is not taken */ |
| 135 | ret = htonl(addr); |
| 136 | lease = find_lease_by_yiaddr(ret); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 137 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 138 | /* no lease or it expired and we are checking for expired leases */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 139 | if ((!lease || (check_expired && lease_expired(lease))) |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 140 | && /* and it isn't on the network */ !check_ip(ret) |
| 141 | ) { |
| 142 | return ret; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 143 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 146 | return 0; |
| 147 | } |