dhcpv4: improve pool var naming
authorDavid Härdeman <[email protected]>
Mon, 17 Nov 2025 21:51:34 +0000 (22:51 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Thu, 27 Nov 2025 07:23:28 +0000 (08:23 +0100)
The uci cfg options might have somewhat confusing names, but
that doesn't mean we have to perpetuate those names throughout the
source.

Also, move the checking of whether the pool parameters from when
we are setting up a pool to where the cfg variables are actually set
from config.

Finally, there's no need to store the pool start and end as "struct
in_addr" in struct interface, because the start and end actually aren't
IPv4 addresses.

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/320
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/config.c
src/dhcpv4.c
src/odhcpd.h

index 01bd375b2e5fc84d69d7c2105cffae2e42873542..747f6fad5e80c744a35ad5e0c740b813e2f14b65 100644 (file)
@@ -63,8 +63,9 @@ struct sys_conf sys_conf = {
        .tzdb_tz_len = 0,
 };
 
-#define START_DEFAULT  100
-#define LIMIT_DEFAULT  150
+#define DHCPV4_POOL_START_DEFAULT      100
+#define DHCPV4_POOL_LIMIT_DEFAULT      150
+#define DHCPV4_POOL_END_DEFAULT                (DHCPV4_POOL_START_DEFAULT + DHCPV4_POOL_LIMIT_DEFAULT - 1)
 
 #define HOSTID_LEN_MIN 12
 #define HOSTID_LEN_MAX 64
@@ -96,8 +97,8 @@ enum {
        IFACE_ATTR_NETWORKID,
        IFACE_ATTR_DYNAMICDHCP,
        IFACE_ATTR_LEASETIME,
-       IFACE_ATTR_LIMIT,
-       IFACE_ATTR_START,
+       IFACE_ATTR_DHCPV4_POOL_START,
+       IFACE_ATTR_DHCPV4_POOL_LIMIT,
        IFACE_ATTR_MASTER,
        IFACE_ATTR_UPSTREAM,
        IFACE_ATTR_RA,
@@ -149,8 +150,8 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
-       [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
-       [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
+       [IFACE_ATTR_DHCPV4_POOL_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
+       [IFACE_ATTR_DHCPV4_POOL_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
        [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
        [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
@@ -318,8 +319,8 @@ static void set_interface_defaults(struct interface *iface)
        iface->max_preferred_lifetime = ND_PREFERRED_LIMIT;
        iface->max_valid_lifetime = ND_VALID_LIMIT;
        iface->captive_portal_uri = NULL;
-       iface->dhcpv4_start.s_addr = htonl(START_DEFAULT);
-       iface->dhcpv4_end.s_addr = htonl(START_DEFAULT + LIMIT_DEFAULT - 1);
+       iface->dhcpv4_pool_start = DHCPV4_POOL_START_DEFAULT;
+       iface->dhcpv4_pool_end = DHCPV4_POOL_END_DEFAULT;
        iface->dhcpv6_assignall = true;
        iface->dhcpv6_pd = true;
        iface->dhcpv6_pd_preferred = false;
@@ -1207,15 +1208,21 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr
                              iface_attrs[IFACE_ATTR_MAX_VALID_LIFETIME].name, iface->name);
        }
 
-       if ((c = tb[IFACE_ATTR_START])) {
-               iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
-               iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
-                                                       LIMIT_DEFAULT - 1);
+       if ((c = tb[IFACE_ATTR_DHCPV4_POOL_START])) {
+               iface->dhcpv4_pool_start = blobmsg_get_u32(c);
+               iface->dhcpv4_pool_end = iface->dhcpv4_pool_start + DHCPV4_POOL_LIMIT_DEFAULT - 1;
        }
 
-       if ((c = tb[IFACE_ATTR_LIMIT]))
-               iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
-                                                       blobmsg_get_u32(c) - 1);
+       if ((c = tb[IFACE_ATTR_DHCPV4_POOL_LIMIT]))
+               iface->dhcpv4_pool_end = iface->dhcpv4_pool_start + blobmsg_get_u32(c) - 1;
+
+       if (iface->dhcpv4_pool_start == 0 ||
+           iface->dhcpv4_pool_start > UINT16_MAX ||
+           iface->dhcpv4_pool_end > UINT16_MAX ||
+           iface->dhcpv4_pool_start > iface->dhcpv4_pool_end) {
+               warn("Invalid DHCPv4 pool range for %s, disabling dynamic leases", iface->name);
+               iface->no_dynamic_dhcp = true;
+       }
 
        if ((c = tb[IFACE_ATTR_MASTER]))
                iface->master = blobmsg_get_bool(c);
index 928c4055549dbad16d14bfde21fd07ff76f957a2..4178b58ef8aaa15a66fe21bbf0a726c8c3e9b9cb 100644 (file)
@@ -1352,28 +1352,20 @@ static void dhcpv4_handle_dgram(void *addr, void *data, size_t len,
 
 static int dhcpv4_setup_addresses(struct interface *iface)
 {
+       uint32_t start = iface->dhcpv4_pool_start;
+       uint32_t end = iface->dhcpv4_pool_end;
+
        iface->dhcpv4_start_ip.s_addr = INADDR_ANY;
        iface->dhcpv4_end_ip.s_addr = INADDR_ANY;
        iface->dhcpv4_local.s_addr = INADDR_ANY;
        iface->dhcpv4_bcast.s_addr = INADDR_ANY;
        iface->dhcpv4_mask.s_addr = INADDR_ANY;
 
-       /* Sanity checks */
-       if (iface->dhcpv4_start.s_addr & htonl(0xffff0000) ||
-           iface->dhcpv4_end.s_addr & htonl(0xffff0000) ||
-           ntohl(iface->dhcpv4_start.s_addr) > ntohl(iface->dhcpv4_end.s_addr)) {
-               warn("Invalid DHCP range for %s", iface->name);
-               return -1;
-       }
-
        if (!iface->addr4_len) {
                warn("No network(s) available on %s", iface->name);
                return -1;
        }
 
-       uint32_t start = ntohl(iface->dhcpv4_start.s_addr);
-       uint32_t end = ntohl(iface->dhcpv4_end.s_addr);
-
        for (size_t i = 0; i < iface->addr4_len && start && end; i++) {
                struct in_addr *addr = &iface->addr4[i].addr.in;
                struct in_addr mask;
index 28478abc0cc2ee73110f871b5e310c16415b9dd9..0bf919631650e8736e7282f39d06f3645ce37e73 100644 (file)
@@ -439,8 +439,8 @@ struct interface {
        uint32_t dhcp_leasetime;
 
        // DHCPv4
-       struct in_addr dhcpv4_start;
-       struct in_addr dhcpv4_end;
+       uint32_t dhcpv4_pool_start;     // Offset to first dynamic address
+       uint32_t dhcpv4_pool_end;       // Offset to last dynamic address
        struct in_addr dhcpv4_start_ip;
        struct in_addr dhcpv4_end_ip;
        struct in_addr dhcpv4_local;