Add per-host leasetime support
authorSteven Barth <stbarth@cisco.com>
Thu, 19 May 2016 06:54:24 +0000 (08:54 +0200)
committerSteven Barth <stbarth@cisco.com>
Thu, 19 May 2016 06:54:24 +0000 (08:54 +0200)
Patch by Daniel Dickinson

Signed-off-by: Steven Barth <steven@midlink.org>
src/config.c
src/dhcpv4.c
src/dhcpv4.h
src/dhcpv6-ia.c
src/dhcpv6.h
src/odhcpd.h

index 8f2c6bf71f7c9d321c0b30a071adad3e5281c83e..ef511129f64c3a9e290d6084830c5ec70332edd5 100644 (file)
@@ -98,6 +98,7 @@ enum {
        LEASE_ATTR_MAC,
        LEASE_ATTR_DUID,
        LEASE_ATTR_HOSTID,
+       LEASE_ATTR_LEASETIME,
        LEASE_ATTR_NAME,
        LEASE_ATTR_MAX
 };
@@ -108,6 +109,7 @@ static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
        [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
        [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
        [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
+       [LEASE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
        [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
 };
 
@@ -215,6 +217,32 @@ static void set_config(struct uci_section *s)
        }
 }
 
+static double parse_leasetime(struct blob_attr *c) {
+       char *val = blobmsg_get_string(c), *endptr;
+       double time = strtod(val, &endptr);
+       if (time && endptr[0]) {
+               if (endptr[0] == 's')
+                       time *= 1;
+               else if (endptr[0] == 'm')
+                       time *= 60;
+               else if (endptr[0] == 'h')
+                       time *= 3600;
+               else if (endptr[0] == 'd')
+                       time *= 24 * 3600;
+               else if (endptr[0] == 'w')
+                       time *= 7 * 24 * 3600;
+               else
+                       goto err;
+       }
+
+       if (time >= 60)
+               return time;
+
+       return 0;
+
+err:
+       return -1;
+}
 
 static int set_lease(struct uci_section *s)
 {
@@ -265,6 +293,15 @@ static int set_lease(struct uci_section *s)
                        goto err;
        }
 
+       if ((c = tb[LEASE_ATTR_LEASETIME])) {
+               double time = parse_leasetime(c);
+               if (time < 0)
+                       goto err;
+
+               if (time >= 60)
+                       lease->dhcpv4_leasetime = time;
+       }
+
        list_add(&lease->head, &leases);
        return 0;
 
@@ -330,22 +367,9 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                iface->ignore = blobmsg_get_bool(c);
 
        if ((c = tb[IFACE_ATTR_LEASETIME])) {
-               char *val = blobmsg_get_string(c), *endptr;
-               double time = strtod(val, &endptr);
-               if (time && endptr[0]) {
-                       if (endptr[0] == 's')
-                               time *= 1;
-                       else if (endptr[0] == 'm')
-                               time *= 60;
-                       else if (endptr[0] == 'h')
-                               time *= 3600;
-                       else if (endptr[0] == 'd')
-                               time *= 24 * 3600;
-                       else if (endptr[0] == 'w')
-                               time *= 7 * 24 * 3600;
-                       else
-                               goto err;
-               }
+               double time = parse_leasetime(c);
+               if (time < 0)
+                       goto err;
 
                if (time >= 60)
                        iface->dhcpv4_leasetime = time;
index a657e13d325f9e4de34a8ec0e5c904d44967274d..f277a67a23f8a49e4da48bc6b88fb170155fc2d7 100644 (file)
@@ -176,6 +176,8 @@ int setup_dhcpv4_interface(struct interface *iface, bool enable)
                                        iface->ifname);
                                return -1;
                        }
+                       if (lease->dhcpv4_leasetime >= 60)
+                               a->leasetime = lease->dhcpv4_leasetime;
                        a->addr = ntohl(lease->ipaddr.s_addr);
                        memcpy(a->hwaddr, lease->mac.ether_addr_octet, sizeof(a->hwaddr));
                        memcpy(a->hostname, lease->hostname, hostlen);
@@ -382,13 +384,22 @@ static void handle_dhcpv4(void *addr, void *data, size_t len,
        if (lease) {
                reply.yiaddr.s_addr = htonl(lease->addr);
 
-               uint32_t val = htonl(iface->dhcpv4_leasetime);
+               uint32_t val;
+               uint32_t leasetime;
+
+               if (lease->leasetime >= 60) {
+                       leasetime = lease->leasetime;
+               } else {
+                       leasetime = iface->dhcpv4_leasetime;
+               }
+
+               val = htonl(leasetime);
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_LEASETIME, 4, &val);
 
-               val = htonl(500 * iface->dhcpv4_leasetime / 1000);
+               val = htonl(500 * leasetime / 1000);
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_RENEW, 4, &val);
 
-               val = htonl(875 * iface->dhcpv4_leasetime / 1000);
+               val = htonl(875 * leasetime / 1000);
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_REBIND, 4, &val);
 
                dhcpv4_put(&reply, &cookie, DHCPV4_OPT_NETMASK, 4, &ifnetmask.sin_addr);
@@ -622,10 +633,17 @@ static struct dhcpv4_assignment* dhcpv4_lease(struct interface *iface,
                        a->head.prev->next = &a->head;
                }
 
+               uint32_t leasetime;
+               if (a->leasetime) {
+                       leasetime = a->leasetime;
+               } else {
+                       leasetime = iface->dhcpv4_leasetime;
+               }
+
                // Was only a solicitation: mark binding for removal
                if (assigned && a->valid_until < now) {
                        a->valid_until = (msg == DHCPV4_MSG_DISCOVER) ? 0 :
-                                       (now + iface->dhcpv4_leasetime);
+                                       (now + leasetime);
                } else if (!assigned && a) { // Cleanup failed assignment
                        free(a);
                        a = NULL;
index 6c72441a3718f0b1f055966818990fcf2fc5fefd..6c4dd50e08b3879a26f90949e2d1958bd8314796 100644 (file)
@@ -78,6 +78,7 @@ struct dhcpv4_assignment {
        uint32_t addr;
        time_t valid_until;
        uint8_t hwaddr[6];
+       uint32_t leasetime;
        char hostname[];
 };
 
index 9b8c47927b32b6a7ab733d0392073325a60a8708..4c321ac0da44a7a78561426f0d1404a92a72dbdd 100644 (file)
@@ -100,6 +100,9 @@ int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
                                return -1;
                        }
 
+                       if (lease->dhcpv4_leasetime > 0)
+                               a->leasetime = lease->dhcpv4_leasetime;
+
                        a->clid_len = duid_len;
                        a->length = 128;
                        if (lease->hostid) {
@@ -675,7 +678,12 @@ static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
                datalen += sizeof(stat);
        } else {
                if (a) {
-                       uint32_t leasetime = iface->dhcpv4_leasetime;
+                       uint32_t leasetime;
+                       if (a->leasetime > 0) {
+                               leasetime = a->leasetime;
+                       } else {
+                               leasetime = iface->dhcpv4_leasetime;
+                       }
                        if (leasetime == 0)
                                leasetime = 3600;
                        else if (leasetime < 60)
index d9f42c816b88b23a346bc8f08d282b5c0df90226..09ab46f9e7a90073657000871d803ac09702092f 100644 (file)
@@ -155,6 +155,8 @@ struct dhcpv6_assignment {
        ssize_t managed_size;
        struct ustream_fd managed_sock;
 
+       uint32_t leasetime;
+
        uint8_t clid_len;
        uint8_t clid_data[];
 };
index fd43dfe3ba206762beaa7e48a7dd3e279cf578da..3e8fc3ea6032c1ddab5047aaf88c4d60626f4ddc 100644 (file)
@@ -96,6 +96,7 @@ struct lease {
        struct ether_addr mac;
        uint16_t duid_len;
        uint8_t *duid;
+       uint32_t dhcpv4_leasetime;
        char hostname[];
 };