Limit lifetime of non-static leases in case of release and decline
authorHans Dedecker <dedeckeh@gmail.com>
Thu, 15 Dec 2016 21:37:47 +0000 (22:37 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Tue, 27 Dec 2016 20:53:55 +0000 (21:53 +0100)
In case infinite leasetime is assigned to a non static DHCPv4/v6 lease
override the infinite lifetime of the lease when either a DHCPv4/v6
decline or release is received.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
src/dhcpv4.c
src/dhcpv6-ia.c
src/odhcpd.h

index 7695d6801a62b617d0eb63c2c59c995b15de510c..509b09230c5431d8582d1debc3415c082ccddd13 100644 (file)
@@ -181,6 +181,8 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                        a->addr = ntohl(lease->ipaddr.s_addr);
                        memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
                        memcpy(a->hostname, lease->hostname, hostlen);
+                       /* Static assignment */
+                       a->flags |= OAF_STATIC;
                        /* Infinite valid */
                        a->valid_until = 0;
 
@@ -643,17 +645,17 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                        *leasetime = my_leasetime;
 
                if (assigned) {
-                       bool is_discover = (msg == DHCPV4_MSG_DISCOVER);
+                       if (msg == DHCPV4_MSG_DISCOVER) {
+                               a->flags &= ~OAF_BOUND;
 
-                       if (!INFINITE_VALID(a->valid_until))
-                               // Was only a discover; mark binding for removal
-                               a->valid_until = (is_discover ? now : ((*leasetime == UINT32_MAX) ?
-                                                       0 : (time_t)(now + *leasetime)));
-
-                       /* Mark assignment as bound */
-                       if (!is_discover)
+                               if (!(a->flags & OAF_STATIC))
+                                       a->valid_until = now;
+                       } else {
                                a->flags |= OAF_BOUND;
 
+                               if (!(a->flags & OAF_STATIC))
+                                       a->valid_until = ((*leasetime == UINT32_MAX) ? 0 : (time_t)(now + *leasetime));
+                       }
                } else if (!assigned && a) { // Cleanup failed assignment
                        free(a);
                        a = NULL;
@@ -664,13 +666,13 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
        } else if (msg == DHCPV4_MSG_RELEASE && a) {
                a->flags &= ~OAF_BOUND;
 
-               if (!INFINITE_VALID(a->valid_until))
+               if (!(a->flags & OAF_STATIC))
                        a->valid_until = now - 1;
 
        } else if (msg == DHCPV4_MSG_DECLINE && a) {
                a->flags &= ~OAF_BOUND;
 
-               if (!INFINITE_VALID(a->valid_until)) {
+               if (!(a->flags & OAF_STATIC)) {
                        memset(a->hwaddr, 0, sizeof(a->hwaddr));
                        a->valid_until = now + 3600; // Block address for 1h
                }
index e8b6e9fa07dae4dda39e1fb60c033e7506ad24e8..a7146a767960cde012f1962a5e014f64e6a4c272 100644 (file)
@@ -114,6 +114,8 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
                        odhcpd_urandom(a->key, sizeof(a->key));
                        memcpy(a->clid_data, lease->duid, lease->duid_len);
                        memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
+                       /* Static assignment */
+                       a->flags |= OAF_STATIC;
                        /* Infinite valid */
                        a->valid_until = 0;
 
@@ -1130,7 +1132,7 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                        if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
                                a->flags &= ~OAF_BOUND;
 
-                               if (!INFINITE_VALID(a->valid_until))
+                               if (!(a->flags & OAF_STATIC))
                                        a->valid_until = now;
                        } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
                                if (hostname_len > 0) {
@@ -1161,7 +1163,7 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                        apply_lease(iface, a, true);
                                }
                        } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
-                               if (!INFINITE_VALID(a->valid_until))
+                               if (!(a->flags & OAF_STATIC))
                                        a->valid_until = now - 1;
 
                                a->flags &= ~OAF_BOUND;
@@ -1169,7 +1171,7 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                        } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
                                a->flags &= ~OAF_BOUND;
 
-                               if (!INFINITE_VALID(a->valid_until)) {
+                               if (!(a->flags & OAF_STATIC)) {
                                        a->clid_len = 0;
                                        a->valid_until = now + 3600; // Block address for 1h
                                }
index 4b7731363f52955c29e98724e7abdd39d13f2d84..0e5868cd06dc6b14cb14e732aa1bbce837f84f33 100644 (file)
@@ -86,6 +86,7 @@ enum odhcpd_mode {
 
 enum odhcpd_assignment_flags {
        OAF_BOUND       = (1 << 0),
+       OAF_STATIC      = (1 << 1),
 };
 
 struct config {