From a05cc991716c7cbd292ab7c9775327b7f7d0922e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20H=C3=A4rdeman?= Date: Sat, 22 Nov 2025 10:13:02 +0100 Subject: [PATCH] dhcpv4: iface->dhcpv4_router -> iface->dhcpv4_routers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This makes it clearer that the variable stores multiple addresses, also fix a potential realloc memleak. Signed-off-by: David Härdeman Link: https://github.com/openwrt/odhcpd/pull/320 Signed-off-by: Álvaro Fernández Rojas --- README.md | 2 +- src/config.c | 16 +++++++++------- src/dhcpv4.c | 6 +++--- src/odhcpd.h | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2d8c3f2..663986a 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ and may also receive information from ubus | dhcpv6_pd |bool | 1 | DHCPv6 stateful addressing hands out IA_PD - Internet Address - Prefix Delegation (PD) | | dhcpv6_pd_preferred |bool | 0 | Set the DHCPv6-PD Preferred (P) flag in outgoing ICMPv6 RA message PIOs (RFC9762); requires `dhcpv6` and `dhcpv6_pd`. | | dhcpv6_pd_min_len |integer| - | Minimum prefix length to delegate with IA_PD (value is adjusted if needed to be greater than the interface prefix length). Range [1,62] | -| router |list |``| Routers to announce, accepts IPv4 only | +| router |list |``| IPv4 addresses of routers on a given subnet (provided via DHCPv4, should be in order of preference) | | dns |list |``| DNS servers to announce, accepts IPv4 and IPv6 | | dnr |list |disabled| Encrypted DNS servers to announce, ` [ ...]` | | dns_service |bool | 1 | Announce the address of interface as DNS service if the list of dns is empty | diff --git a/src/config.c b/src/config.c index 30609a6..68ef3ab 100644 --- a/src/config.c +++ b/src/config.c @@ -346,7 +346,7 @@ static void clean_interface(struct interface *iface) free(iface->dns_addrs6); free(iface->dns_search); free(iface->upstream); - free(iface->dhcpv4_router); + free(iface->dhcpv4_routers); free(iface->dhcpv6_raw); free(iface->dhcpv4_ntp); free(iface->dhcpv6_ntp); @@ -1291,21 +1291,23 @@ int config_parse_interface(void *data, size_t len, const char *name, bool overwr unsigned rem; blobmsg_for_each_attr(cur, c, rem) { - struct in_addr addr4; + struct in_addr addr4, *tmp; if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false)) continue; if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) { - iface->dhcpv4_router = realloc(iface->dhcpv4_router, - (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router)); - if (!iface->dhcpv4_router) + tmp = realloc(iface->dhcpv4_routers, + (iface->dhcpv4_routers_cnt + 1) * sizeof(*iface->dhcpv4_routers)); + if (!tmp) goto err; - iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4; - } else + iface->dhcpv4_routers = tmp; + iface->dhcpv4_routers[iface->dhcpv4_routers_cnt++] = addr4; + } else { error("Invalid %s value configured for interface '%s'", iface_attrs[IFACE_ATTR_ROUTER].name, iface->name); + } } } diff --git a/src/dhcpv4.c b/src/dhcpv4.c index 545c814..97c1ec2 100644 --- a/src/dhcpv4.c +++ b/src/dhcpv4.c @@ -1094,9 +1094,9 @@ void dhcpv4_handle_msg(void *src_addr, void *data, size_t len, case DHCPV4_OPT_ROUTER: iov[IOV_ROUTER].iov_len = sizeof(reply_router); - if (iface->dhcpv4_router_cnt) { - reply_router.len = iface->dhcpv4_router_cnt * sizeof(*iface->dhcpv4_router); - iov[IOV_ROUTER_ADDR].iov_base = iface->dhcpv4_router; + if (iface->dhcpv4_routers_cnt) { + reply_router.len = iface->dhcpv4_routers_cnt * sizeof(*iface->dhcpv4_routers); + iov[IOV_ROUTER_ADDR].iov_base = iface->dhcpv4_routers; } else { reply_router.len = sizeof(iface->dhcpv4_own_ip.addr.in); iov[IOV_ROUTER_ADDR].iov_base = &iface->dhcpv4_own_ip.addr.in; diff --git a/src/odhcpd.h b/src/odhcpd.h index a74c295..a5ba496 100644 --- a/src/odhcpd.h +++ b/src/odhcpd.h @@ -447,8 +447,8 @@ struct interface { struct in_addr dhcpv4_start_ip; struct in_addr dhcpv4_end_ip; struct odhcpd_ipaddr dhcpv4_own_ip; - struct in_addr *dhcpv4_router; - size_t dhcpv4_router_cnt; + struct in_addr *dhcpv4_routers; // IPv4 addresses for routers on this subnet + size_t dhcpv4_routers_cnt; // Count of router addresses bool dhcpv4_forcereconf; // DNS -- 2.30.2