Don't print non bound assignments in the state file
authorHans Dedecker <dedeckeh@gmail.com>
Wed, 14 Dec 2016 21:22:22 +0000 (22:22 +0100)
committerHans Dedecker <dedeckeh@gmail.com>
Thu, 15 Dec 2016 14:17:15 +0000 (15:17 +0100)
Set bound flag for DHCPv4 and DHCPv6 assignments when the IPv6/IPv4 address
is leased to a client.
This will prevent the printing of leases and hostname/IPv4/IPv6 address
combinations in the state file for for which the IPv4/6 address has not
been assigned.
Also this will fix the printing of assignments which have been declined
by the clients

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

index 5220edf61dd3f9f2d190632658403084224e13e4..7695d6801a62b617d0eb63c2c59c995b15de510c 100644 (file)
@@ -643,10 +643,17 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                        *leasetime = my_leasetime;
 
                if (assigned) {
+                       bool is_discover = (msg == DHCPV4_MSG_DISCOVER);
+
                        if (!INFINITE_VALID(a->valid_until))
                                // Was only a discover; mark binding for removal
-                               a->valid_until = ((msg == DHCPV4_MSG_DISCOVER) ? now : ((*leasetime == UINT32_MAX) ?
+                               a->valid_until = (is_discover ? now : ((*leasetime == UINT32_MAX) ?
                                                        0 : (time_t)(now + *leasetime)));
+
+                       /* Mark assignment as bound */
+                       if (!is_discover)
+                               a->flags |= OAF_BOUND;
+
                } else if (!assigned && a) { // Cleanup failed assignment
                        free(a);
                        a = NULL;
@@ -654,12 +661,19 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
 
                if (assigned && a)
                        lease = a;
-       } else if (msg == DHCPV4_MSG_RELEASE) {
-               if (a && !INFINITE_VALID(a->valid_until))
+       } else if (msg == DHCPV4_MSG_RELEASE && a) {
+               a->flags &= ~OAF_BOUND;
+
+               if (!INFINITE_VALID(a->valid_until))
                        a->valid_until = now - 1;
-       } else if (msg == DHCPV4_MSG_DECLINE && a && !INFINITE_VALID(a->valid_until)) {
-               memset(a->hwaddr, 0, sizeof(a->hwaddr));
-               a->valid_until = now + 3600; // Block address for 1h
+
+       } else if (msg == DHCPV4_MSG_DECLINE && a) {
+               a->flags &= ~OAF_BOUND;
+
+               if (!INFINITE_VALID(a->valid_until)) {
+                       memset(a->hwaddr, 0, sizeof(a->hwaddr));
+                       a->valid_until = now + 3600; // Block address for 1h
+               }
        }
 
        dhcpv6_write_statefile();
index 6c4dd50e08b3879a26f90949e2d1958bd8314796..cd6c978431e54e4c4245a0ca1c751dc48d16bc7d 100644 (file)
@@ -79,6 +79,7 @@ struct dhcpv4_assignment {
        time_t valid_until;
        uint8_t hwaddr[6];
        uint32_t leasetime;
+       unsigned int flags;
        char hostname[];
 };
 
index 20ef1f8c3fa3b84e3440440f9e918d8adcb66cf7..e8b6e9fa07dae4dda39e1fb60c033e7506ad24e8 100644 (file)
@@ -83,7 +83,7 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
                                syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
                                return -1;
                        }
-                       
+
                        border->length = 64;
                        list_add(&border->head, &iface->ia_assignments);
                }
@@ -236,7 +236,7 @@ void dhcpv6_write_statefile(void)
                        if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
                                struct dhcpv6_assignment *c;
                                list_for_each_entry(c, &iface->ia_assignments, head) {
-                                       if (c->clid_len == 0 || c->managed_size < 0)
+                                       if (!(c->flags & OAF_BOUND) || c->managed_size < 0)
                                                continue;
 
                                        char ipbuf[INET6_ADDRSTRLEN];
@@ -282,9 +282,9 @@ void dhcpv6_write_statefile(void)
                                                        fputs(ipbuf, fp);
 
                                                        char b[256];
-                                                       if (dn_expand(iface->search, iface->search + iface->search_len,
-                                                                       iface->search, b, sizeof(b)) > 0)
-                                                               fprintf(fp, "\t%s.%s", c->hostname, b);
+                                                       if (dn_expand(iface->search, iface->search + iface->search_len,
+                                                                       iface->search, b, sizeof(b)) > 0)
+                                                               fprintf(fp, "\t%s.%s", c->hostname, b);
 
                                                        fprintf(fp, "\t%s\n", c->hostname);
                                                        md5_hash(ipbuf, strlen(ipbuf), &md5);
@@ -302,6 +302,9 @@ void dhcpv6_write_statefile(void)
                        if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
                                struct dhcpv4_assignment *c;
                                list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
+                                       if (!(c->flags & OAF_BOUND))
+                                               continue;
+
                                        char ipbuf[INET6_ADDRSTRLEN];
                                        char leasebuf[512];
                                        char duidbuf[16];
@@ -323,9 +326,9 @@ void dhcpv6_write_statefile(void)
                                                fputs(ipbuf, fp);
 
                                                char b[256];
-                                               if (dn_expand(iface->search, iface->search + iface->search_len,
-                                                               iface->search, b, sizeof(b)) > 0)
-                                                       fprintf(fp, "\t%s.%s", c->hostname, b);
+                                               if (dn_expand(iface->search, iface->search + iface->search_len,
+                                                               iface->search, b, sizeof(b)) > 0)
+                                                       fprintf(fp, "\t%s.%s", c->hostname, b);
 
                                                fprintf(fp, "\t%s\n", c->hostname);
                                                md5_hash(ipbuf, strlen(ipbuf), &md5);
@@ -1125,6 +1128,8 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
 
                        // Was only a solicitation: mark binding for removal
                        if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
+                               a->flags &= ~OAF_BOUND;
+
                                if (!INFINITE_VALID(a->valid_until))
                                        a->valid_until = now;
                        } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
@@ -1136,6 +1141,7 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                                        }
                                }
                                a->accept_reconf = accept_reconf;
+                               a->flags |= OAF_BOUND;
                                apply_lease(iface, a, true);
                        } else if (!assigned && a && a->managed_size == 0) { // Cleanup failed assignment
                                free_dhcpv6_assignment(a);
@@ -1150,17 +1156,23 @@ ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
                        } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
                                        hdr->msg_type == DHCPV6_MSG_REBIND) {
                                ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
-                               if (a)
+                               if (a) {
+                                       a->flags |= OAF_BOUND;
                                        apply_lease(iface, a, true);
+                               }
                        } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
                                if (!INFINITE_VALID(a->valid_until))
                                        a->valid_until = now - 1;
 
+                               a->flags &= ~OAF_BOUND;
                                apply_lease(iface, a, false);
-                       } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128 &&
-                                       !INFINITE_VALID(a->valid_until)) {
-                               a->clid_len = 0;
-                               a->valid_until = now + 3600; // Block address for 1h
+                       } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
+                               a->flags &= ~OAF_BOUND;
+
+                               if (!INFINITE_VALID(a->valid_until)) {
+                                       a->clid_len = 0;
+                                       a->valid_until = now + 3600; // Block address for 1h
+                               }
                        }
                } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
                        // Send NOTONLINK for CONFIRM with addr present so that clients restart connection
index 09ab46f9e7a90073657000871d803ac09702092f..c0775f190c90559f61ffbbe65f68f7a2eb140356 100644 (file)
@@ -156,6 +156,7 @@ struct dhcpv6_assignment {
        struct ustream_fd managed_sock;
 
        uint32_t leasetime;
+       unsigned int flags;
 
        uint8_t clid_len;
        uint8_t clid_data[];
index e322ebd5b48b82da40a7fd609a798e687c484b0c..4b7731363f52955c29e98724e7abdd39d13f2d84 100644 (file)
@@ -84,6 +84,10 @@ enum odhcpd_mode {
 };
 
 
+enum odhcpd_assignment_flags {
+       OAF_BOUND       = (1 << 0),
+};
+
 struct config {
        bool legacy;
        char *dhcp_cb;