leases.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "common.h"
  7. #include "dhcpd.h"
  8. /* Find the oldest expired lease, NULL if there are no expired leases */
  9. static struct dhcpOfferedAddr *oldest_expired_lease(void)
  10. {
  11. struct dhcpOfferedAddr *oldest = NULL;
  12. // TODO: use monotonic_sec()
  13. unsigned long oldest_lease = time(0);
  14. unsigned i;
  15. for (i = 0; i < server_config.max_leases; i++)
  16. if (oldest_lease > leases[i].expires) {
  17. oldest_lease = leases[i].expires;
  18. oldest = &(leases[i]);
  19. }
  20. return oldest;
  21. }
  22. /* clear every lease out that chaddr OR yiaddr matches and is nonzero */
  23. static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
  24. {
  25. unsigned i, j;
  26. for (j = 0; j < 16 && !chaddr[j]; j++)
  27. continue;
  28. for (i = 0; i < server_config.max_leases; i++)
  29. if ((j != 16 && memcmp(leases[i].chaddr, chaddr, 16) == 0)
  30. || (yiaddr && leases[i].yiaddr == yiaddr)
  31. ) {
  32. memset(&(leases[i]), 0, sizeof(leases[i]));
  33. }
  34. }
  35. /* add a lease into the table, clearing out any old ones */
  36. struct dhcpOfferedAddr* FAST_FUNC add_lease(const uint8_t *chaddr, uint32_t yiaddr, unsigned long lease)
  37. {
  38. struct dhcpOfferedAddr *oldest;
  39. /* clean out any old ones */
  40. clear_lease(chaddr, yiaddr);
  41. oldest = oldest_expired_lease();
  42. if (oldest) {
  43. memcpy(oldest->chaddr, chaddr, 16);
  44. oldest->yiaddr = yiaddr;
  45. oldest->expires = time(0) + lease;
  46. }
  47. return oldest;
  48. }
  49. /* true if a lease has expired */
  50. int FAST_FUNC lease_expired(struct dhcpOfferedAddr *lease)
  51. {
  52. return (lease->expires < (unsigned long) time(0));
  53. }
  54. /* Find the first lease that matches chaddr, NULL if no match */
  55. struct dhcpOfferedAddr* FAST_FUNC find_lease_by_chaddr(const uint8_t *chaddr)
  56. {
  57. unsigned i;
  58. for (i = 0; i < server_config.max_leases; i++)
  59. if (!memcmp(leases[i].chaddr, chaddr, 16))
  60. return &(leases[i]);
  61. return NULL;
  62. }
  63. /* Find the first lease that matches yiaddr, NULL is no match */
  64. struct dhcpOfferedAddr* FAST_FUNC find_lease_by_yiaddr(uint32_t yiaddr)
  65. {
  66. unsigned i;
  67. for (i = 0; i < server_config.max_leases; i++)
  68. if (leases[i].yiaddr == yiaddr)
  69. return &(leases[i]);
  70. return NULL;
  71. }
  72. /* check is an IP is taken, if it is, add it to the lease table */
  73. static int nobody_responds_to_arp(uint32_t addr)
  74. {
  75. /* 16 zero bytes */
  76. static const uint8_t blank_chaddr[16] = { 0 };
  77. /* = { 0 } helps gcc to put it in rodata, not bss */
  78. struct in_addr temp;
  79. int r;
  80. r = arpping(addr, server_config.server, server_config.arp, server_config.interface);
  81. if (r)
  82. return r;
  83. temp.s_addr = addr;
  84. bb_info_msg("%s belongs to someone, reserving it for %u seconds",
  85. inet_ntoa(temp), (unsigned)server_config.conflict_time);
  86. add_lease(blank_chaddr, addr, server_config.conflict_time);
  87. return 0;
  88. }
  89. /* find an assignable address, if check_expired is true, we check all the expired leases as well.
  90. * Maybe this should try expired leases by age... */
  91. uint32_t FAST_FUNC find_address(int check_expired)
  92. {
  93. uint32_t addr, ret;
  94. struct dhcpOfferedAddr *lease = NULL;
  95. addr = server_config.start_ip; /* addr is in host order here */
  96. for (; addr <= server_config.end_ip; addr++) {
  97. /* ie, 192.168.55.0 */
  98. if (!(addr & 0xFF))
  99. continue;
  100. /* ie, 192.168.55.255 */
  101. if ((addr & 0xFF) == 0xFF)
  102. continue;
  103. /* Only do if it isn't assigned as a static lease */
  104. ret = htonl(addr);
  105. if (!reservedIp(server_config.static_leases, ret)) {
  106. /* lease is not taken */
  107. lease = find_lease_by_yiaddr(ret);
  108. /* no lease or it expired and we are checking for expired leases */
  109. if ((!lease || (check_expired && lease_expired(lease)))
  110. && nobody_responds_to_arp(ret) /* it isn't used on the network */
  111. ) {
  112. return ret;
  113. }
  114. }
  115. }
  116. return 0;
  117. }