dhcpv4: allow lease takeover (bugfix)
authorDavid Härdeman <[email protected]>
Fri, 26 Sep 2025 17:22:34 +0000 (19:22 +0200)
committerTed Hess <[email protected]>
Sun, 28 Sep 2025 01:18:14 +0000 (21:18 -0400)
Multiple MAC addresses are supported per static lease (congruent to dnsmasq)
Request on different MAC frees any old assignments for a given lease.

Signed-off-by: David Härdeman <[email protected]>
src/dhcpv4.c

index 1ae7254dbed0eefd495641f716e2c1ab9dda4eca..5d7e08af38ba290b43efb0294f12e42ddb4eabc2 100644 (file)
@@ -1111,6 +1111,20 @@ dhcpv4_lease(struct interface *iface, enum dhcpv4_msg msg, const uint8_t *mac,
        struct lease *l = config_find_lease_by_mac(mac);
        time_t now = odhcpd_time();
 
+       /*
+        * If we found a static lease cfg, but no old assignment for this
+        * hwaddr, we need to clear out any old assignments given to other
+        * hwaddrs in order to take over the IP address.
+        */
+       if (l && !a && (msg == DHCPV4_MSG_DISCOVER || msg == DHCPV4_MSG_REQUEST)) {
+               struct dhcp_assignment *c, *tmp;
+
+               list_for_each_entry_safe(c, tmp, &l->assignments, lease_list) {
+                       if (c->flags & OAF_DHCPV4 && c->flags & OAF_STATIC)
+                               free_assignment(c);
+               }
+       }
+
        if (l && a && a->lease != l) {
                free_assignment(a);
                a = NULL;