"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; |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 15 | // TODO: use monotonic_sec() |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 16 | unsigned long oldest_lease = time(0); |
| 17 | unsigned i; |
| 18 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 33 | for (j = 0; j < 16 && !chaddr[j]; j++) |
| 34 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 35 | |
| 36 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 37 | if ((j != 16 && memcmp(leases[i].chaddr, chaddr, 16) == 0) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 38 | || (yiaddr && leases[i].yiaddr == yiaddr) |
| 39 | ) { |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 40 | memset(&(leases[i]), 0, sizeof(leases[i])); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | |
| 45 | /* add a lease into the table, clearing out any old ones */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 46 | 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] | 47 | { |
| 48 | struct dhcpOfferedAddr *oldest; |
| 49 | |
| 50 | /* clean out any old ones */ |
| 51 | clear_lease(chaddr, yiaddr); |
| 52 | |
| 53 | oldest = oldest_expired_lease(); |
| 54 | |
| 55 | if (oldest) { |
| 56 | memcpy(oldest->chaddr, chaddr, 16); |
| 57 | oldest->yiaddr = yiaddr; |
| 58 | oldest->expires = time(0) + lease; |
| 59 | } |
| 60 | |
| 61 | return oldest; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* true if a lease has expired */ |
| 66 | int lease_expired(struct dhcpOfferedAddr *lease) |
| 67 | { |
| 68 | return (lease->expires < (unsigned long) time(0)); |
| 69 | } |
| 70 | |
| 71 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | /* Find the first lease that matches chaddr, NULL if no match */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 73 | struct dhcpOfferedAddr *find_lease_by_chaddr(const uint8_t *chaddr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 74 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 75 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 76 | |
| 77 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 78 | if (!memcmp(leases[i].chaddr, chaddr, 16)) |
| 79 | return &(leases[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* Find the first lease that matches yiaddr, NULL is no match */ |
| 86 | struct dhcpOfferedAddr *find_lease_by_yiaddr(uint32_t yiaddr) |
| 87 | { |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 88 | unsigned i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 89 | |
| 90 | for (i = 0; i < server_config.max_leases; i++) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 91 | if (leases[i].yiaddr == yiaddr) |
| 92 | return &(leases[i]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 93 | |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* check is an IP is taken, if it is, add it to the lease table */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 99 | static int nobody_responds_to_arp(uint32_t addr) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 100 | { |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 101 | /* 16 zero bytes */ |
| 102 | static const uint8_t blank_chaddr[16] = { 0 }; |
| 103 | /* = { 0 } helps gcc to put it in rodata, not bss */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 104 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 105 | struct in_addr temp; |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 106 | int r; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 108 | r = arpping(addr, server_config.server, server_config.arp, server_config.interface); |
| 109 | if (r) |
| 110 | return r; |
| 111 | |
| 112 | temp.s_addr = addr; |
| 113 | bb_info_msg("%s belongs to someone, reserving it for %u seconds", |
| 114 | inet_ntoa(temp), (unsigned)server_config.conflict_time); |
| 115 | add_lease(blank_chaddr, addr, server_config.conflict_time); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 116 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 120 | /* find an assignable address, if check_expired is true, we check all the expired leases as well. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 121 | * Maybe this should try expired leases by age... */ |
| 122 | uint32_t find_address(int check_expired) |
| 123 | { |
| 124 | uint32_t addr, ret; |
| 125 | struct dhcpOfferedAddr *lease = NULL; |
| 126 | |
Denis Vlasenko | c82b510 | 2007-07-01 17:05:57 +0000 | [diff] [blame] | 127 | addr = server_config.start_ip; /* addr is in host order here */ |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 128 | for (; addr <= server_config.end_ip; addr++) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 129 | /* ie, 192.168.55.0 */ |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 130 | if (!(addr & 0xFF)) |
| 131 | continue; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 132 | /* ie, 192.168.55.255 */ |
Denis Vlasenko | 22a80d1 | 2007-08-09 08:04:05 +0000 | [diff] [blame] | 133 | if ((addr & 0xFF) == 0xFF) |
| 134 | continue; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 135 | /* Only do if it isn't assigned as a static lease */ |
Denis Vlasenko | 42b3dea | 2007-07-03 15:47:50 +0000 | [diff] [blame] | 136 | ret = htonl(addr); |
| 137 | if (!reservedIp(server_config.static_leases, ret)) { |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 138 | /* lease is not taken */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 139 | lease = find_lease_by_yiaddr(ret); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 140 | /* no lease or it expired and we are checking for expired leases */ |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 141 | if ((!lease || (check_expired && lease_expired(lease))) |
Denis Vlasenko | 49a128a | 2007-07-17 21:42:59 +0000 | [diff] [blame] | 142 | && nobody_responds_to_arp(ret) /* it isn't used on the network */ |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 143 | ) { |
| 144 | return ret; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 145 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |