From 4246bbda56d620539b3a39318eef8f2614dd3af6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Mon, 17 Nov 2025 22:51:34 +0100 Subject: [PATCH] dhcpv4: improve pool var naming MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Link: https://github.com/openwrt/odhcpd/pull/320 Signed-off-by: Álvaro Fernández Rojas --- src/config.c | 37 ++++++++++++++++++++++--------------- src/dhcpv4.c | 14 +++----------- src/odhcpd.h | 4 ++-- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/config.c b/src/config.c index 01bd375..747f6fa 100644 --- a/src/config.c +++ b/src/config.c @@ -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); diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 928c405..4178b58 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -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; diff --git a/src/odhcpd.h b/src/odhcpd.h index 28478ab..0bf9196 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -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; -- 2.30.2