leases.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * leases.c -- tools to manage DHCP leases
  4. * Russ Dill <Russ.Dill@asu.edu> July 2001
  5. *
  6. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  7. */
  8. #include "common.h"
  9. #include "dhcpd.h"
  10. /* Find the oldest expired lease, NULL if there are no expired leases */
  11. static struct dyn_lease *oldest_expired_lease(void)
  12. {
  13. struct dyn_lease *oldest_lease = NULL;
  14. leasetime_t oldest_time = time(NULL);
  15. unsigned i;
  16. /* Unexpired leases have g_leases[i].expires >= current time
  17. * and therefore can't ever match */
  18. for (i = 0; i < server_config.max_leases; i++) {
  19. if (g_leases[i].expires < oldest_time) {
  20. oldest_time = g_leases[i].expires;
  21. oldest_lease = &g_leases[i];
  22. }
  23. }
  24. return oldest_lease;
  25. }
  26. /* Clear every lease out that chaddr OR yiaddr matches and is nonzero */
  27. static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
  28. {
  29. unsigned i, j;
  30. for (j = 0; j < 16 && !chaddr[j]; j++)
  31. continue;
  32. for (i = 0; i < server_config.max_leases; i++) {
  33. if ((j != 16 && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
  34. || (yiaddr && g_leases[i].lease_nip == yiaddr)
  35. ) {
  36. memset(&g_leases[i], 0, sizeof(g_leases[i]));
  37. }
  38. }
  39. }
  40. /* Add a lease into the table, clearing out any old ones */
  41. struct dyn_lease* FAST_FUNC add_lease(
  42. const uint8_t *chaddr, uint32_t yiaddr,
  43. leasetime_t leasetime,
  44. const char *hostname, int hostname_len)
  45. {
  46. struct dyn_lease *oldest;
  47. /* clean out any old ones */
  48. clear_lease(chaddr, yiaddr);
  49. oldest = oldest_expired_lease();
  50. if (oldest) {
  51. oldest->hostname[0] = '\0';
  52. if (hostname) {
  53. char *p;
  54. if (hostname_len > sizeof(oldest->hostname))
  55. hostname_len = sizeof(oldest->hostname);
  56. p = safe_strncpy(oldest->hostname, hostname, hostname_len);
  57. /* sanitization (s/non-ASCII/^/g) */
  58. while (*p) {
  59. if (*p < ' ' || *p > 126)
  60. *p = '^';
  61. p++;
  62. }
  63. }
  64. memcpy(oldest->lease_mac, chaddr, 6);
  65. oldest->lease_nip = yiaddr;
  66. oldest->expires = time(NULL) + leasetime;
  67. }
  68. return oldest;
  69. }
  70. /* True if a lease has expired */
  71. int FAST_FUNC is_expired_lease(struct dyn_lease *lease)
  72. {
  73. return (lease->expires < (leasetime_t) time(NULL));
  74. }
  75. /* Find the first lease that matches MAC, NULL if no match */
  76. struct dyn_lease* FAST_FUNC find_lease_by_mac(const uint8_t *mac)
  77. {
  78. unsigned i;
  79. for (i = 0; i < server_config.max_leases; i++)
  80. if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
  81. return &g_leases[i];
  82. return NULL;
  83. }
  84. /* Find the first lease that matches IP, NULL is no match */
  85. struct dyn_lease* FAST_FUNC find_lease_by_nip(uint32_t nip)
  86. {
  87. unsigned i;
  88. for (i = 0; i < server_config.max_leases; i++)
  89. if (g_leases[i].lease_nip == nip)
  90. return &g_leases[i];
  91. return NULL;
  92. }
  93. /* Check if the IP is taken; if it is, add it to the lease table */
  94. static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
  95. {
  96. /* 16 zero bytes */
  97. static const uint8_t blank_chaddr[16] = { 0 };
  98. /* = { 0 } helps gcc to put it in rodata, not bss */
  99. struct in_addr temp;
  100. int r;
  101. r = arpping(nip, safe_mac,
  102. server_config.server_nip,
  103. server_config.server_mac,
  104. server_config.interface);
  105. if (r)
  106. return r;
  107. temp.s_addr = nip;
  108. bb_info_msg("%s belongs to someone, reserving it for %u seconds",
  109. inet_ntoa(temp), (unsigned)server_config.conflict_time);
  110. add_lease(blank_chaddr, nip, server_config.conflict_time, NULL, 0);
  111. return 0;
  112. }
  113. /* Find a new usable (we think) address */
  114. uint32_t FAST_FUNC find_free_or_expired_nip(const uint8_t *safe_mac)
  115. {
  116. uint32_t addr;
  117. struct dyn_lease *oldest_lease = NULL;
  118. addr = server_config.start_ip; /* addr is in host order here */
  119. for (; addr <= server_config.end_ip; addr++) {
  120. uint32_t nip;
  121. struct dyn_lease *lease;
  122. /* ie, 192.168.55.0 */
  123. if ((addr & 0xff) == 0)
  124. continue;
  125. /* ie, 192.168.55.255 */
  126. if ((addr & 0xff) == 0xff)
  127. continue;
  128. nip = htonl(addr);
  129. /* is this a static lease addr? */
  130. if (is_nip_reserved(server_config.static_leases, nip))
  131. continue;
  132. lease = find_lease_by_nip(nip);
  133. if (!lease) {
  134. if (nobody_responds_to_arp(nip, safe_mac))
  135. return nip;
  136. } else {
  137. if (!oldest_lease || lease->expires < oldest_lease->expires)
  138. oldest_lease = lease;
  139. }
  140. }
  141. if (oldest_lease && is_expired_lease(oldest_lease)
  142. && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac)
  143. ) {
  144. return oldest_lease->lease_nip;
  145. }
  146. return 0;
  147. }