rework IPv6 address dump logic
[project/odhcpd.git] / src / dhcpv6-ia.c
index 88686c9..fb50448 100644 (file)
@@ -88,8 +88,7 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
                                return -1;
                        }
 
-                       if (lease->dhcpv4_leasetime > 0)
-                               a->leasetime = lease->dhcpv4_leasetime;
+                       a->leasetime = lease->dhcpv4_leasetime;
 
                        a->clid_len = duid_len;
                        a->length = 128;
@@ -243,8 +242,12 @@ void dhcpv6_write_statefile(void)
                int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
                if (fd < 0)
                        return;
-
-               lockf(fd, F_LOCK, 0);
+               int ret;
+               ret = lockf(fd, F_LOCK, 0);
+               if (ret < 0) {
+                       close(fd);
+                       return;
+               }
                if (ftruncate(fd, 0) < 0) {}
 
                FILE *fp = fdopen(fd, "w");
@@ -531,7 +534,10 @@ static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
 
                        if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
                                list_add_tail(&assign->head, &c->head);
-                               apply_lease(iface, assign, true);
+
+                               if (assign->flags & OAF_BOUND)
+                                       apply_lease(iface, assign, true);
+
                                return true;
                        }
 
@@ -550,7 +556,10 @@ static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
                if (current + asize < c->assigned) {
                        assign->assigned = current;
                        list_add_tail(&assign->head, &c->head);
-                       apply_lease(iface, assign, true);
+
+                       if (assign->flags & OAF_BOUND)
+                               apply_lease(iface, assign, true);
+
                        return true;
                }
 
@@ -600,7 +609,7 @@ void dhcpv6_ia_preupdate(struct interface *iface)
                        &iface->ia_assignments, struct dhcpv6_assignment, head);
 
        list_for_each_entry(c, &iface->ia_assignments, head)
-               if (c != border && !iface->managed)
+               if (c != border && !iface->managed && (c->flags & OAF_BOUND))
                        apply_lease(iface, c, false);
 }
 
@@ -634,7 +643,7 @@ void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
 
                if (c->length < 128 && c->assigned >= border->assigned && c != border)
                        list_move(&c->head, &reassign);
-               else if (c != border)
+               else if (c != border && (c->flags & OAF_BOUND))
                        apply_lease(iface, c, true);
 
                if (c->accept_reconf && c->reconf_cnt == 0) {
@@ -713,16 +722,11 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
        } else {
                if (a) {
                        uint32_t leasetime;
-                       if (a->leasetime > 0)
+                       if (a->leasetime)
                                leasetime = a->leasetime;
                        else
                                leasetime = iface->dhcpv4_leasetime;
 
-                       if (leasetime == 0)
-                               leasetime = 3600;
-                       else if (leasetime < 60)
-                               leasetime = 60;
-
                        uint32_t pref = leasetime;
                        uint32_t valid = leasetime;
 
@@ -1068,7 +1072,9 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                a = c;
 
                                /* Reset state */
-                               apply_lease(iface, a, false);
+                               if (a->flags & OAF_BOUND)
+                                       apply_lease(iface, a, false);
+
                                memcpy(a->clid_data, clid_data, clid_len);
                                a->clid_len = clid_len;
                                a->iaid = ia->iaid;
@@ -1190,8 +1196,10 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                if (!(a->flags & OAF_STATIC))
                                        a->valid_until = now - 1;
 
-                               a->flags &= ~OAF_BOUND;
-                               apply_lease(iface, a, false);
+                               if (a->flags & OAF_BOUND) {
+                                       apply_lease(iface, a, false);
+                                       a->flags &= ~OAF_BOUND;
+                               }
                        } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
                                a->flags &= ~OAF_BOUND;