[PATCH 3/3] netifd: ubus: move deprecated IPs to the dump output end

Jonas Lochmann openwrt at jonaslochmann.de
Sat Dec 27 11:32:01 PST 2025


The commonly used function network_get_ipaddr6 returns the first IP.
This modification avoids that it returns a deprecated IP if there is
any other IP available.

Signed-off-by: Jonas Lochmann <openwrt at jonaslochmann.de>
---
 ubus.c | 41 ++++++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 9 deletions(-)

diff --git a/ubus.c b/ubus.c
index 778fb10..5c68fba 100644
--- a/ubus.c
+++ b/ubus.c
@@ -514,14 +514,24 @@ netifd_add_interface_errors(struct blob_buf *b, struct interface *iface)
 	blobmsg_close_array(b, e);
 }
 
+enum addr_type {
+	ADDR_IPv4 = 1,
+	ADDR_IPv6_Active = 2,
+	ADDR_IPv6_Deprecated = 4,
+	ADDR_IPv6 = 5,
+};
+
 static void
-interface_ip_dump_address_list(struct interface_ip_settings *ip, time_t now, bool v6, bool enabled)
+interface_ip_dump_address_list(struct interface_ip_settings *ip,
+			       time_t now, enum addr_type addr_type,
+			       bool enabled)
 {
 	struct device_addr *addr;
 	char *buf;
 	void *a;
 	int buflen = 128;
 	int af;
+	bool v6 = addr_type != ADDR_IPv4;
 
 	vlist_for_each_element(&ip->addr, addr, node) {
 		if (addr->enabled != enabled)
@@ -535,6 +545,17 @@ interface_ip_dump_address_list(struct interface_ip_settings *ip, time_t now, boo
 		if (af != (v6 ? AF_INET6 : AF_INET))
 			continue;
 
+		if (v6) {
+			bool deprecated = addr->preferred_until &&
+				addr->preferred_until <= now;
+
+			enum addr_type flag = deprecated ?
+				ADDR_IPv6_Deprecated : ADDR_IPv6_Active;
+
+			if (!(addr_type & flag))
+				continue;
+		}
+
 		a = blobmsg_open_table(&b, NULL);
 
 		buf = blobmsg_alloc_string_buffer(&b, "address", buflen);
@@ -862,12 +883,13 @@ netifd_dump_status(struct interface *iface)
 		if (iface->assignment_weight)
 			blobmsg_add_u32(&b, "ip6weight", iface->assignment_weight);
 		a = blobmsg_open_array(&b, "ipv4-address");
-		interface_ip_dump_address_list(&iface->config_ip, now, false, true);
-		interface_ip_dump_address_list(&iface->proto_ip, now, false, true);
+		interface_ip_dump_address_list(&iface->config_ip, now, ADDR_IPv4, true);
+		interface_ip_dump_address_list(&iface->proto_ip, now, ADDR_IPv4, true);
 		blobmsg_close_array(&b, a);
 		a = blobmsg_open_array(&b, "ipv6-address");
-		interface_ip_dump_address_list(&iface->config_ip, now, true, true);
-		interface_ip_dump_address_list(&iface->proto_ip, now, true, true);
+		interface_ip_dump_address_list(&iface->config_ip, now, ADDR_IPv6, true);
+		interface_ip_dump_address_list(&iface->proto_ip, now, ADDR_IPv6_Active, true);
+		interface_ip_dump_address_list(&iface->proto_ip, now, ADDR_IPv6_Deprecated, true);
 		blobmsg_close_array(&b, a);
 		a = blobmsg_open_array(&b, "ipv6-prefix");
 		interface_ip_dump_prefix_list(&iface->config_ip, now);
@@ -895,12 +917,13 @@ netifd_dump_status(struct interface *iface)
 
 		inactive = blobmsg_open_table(&b, "inactive");
 		a = blobmsg_open_array(&b, "ipv4-address");
-		interface_ip_dump_address_list(&iface->config_ip, now, false, false);
-		interface_ip_dump_address_list(&iface->proto_ip, now, false, false);
+		interface_ip_dump_address_list(&iface->config_ip, now, ADDR_IPv4, false);
+		interface_ip_dump_address_list(&iface->proto_ip, now, ADDR_IPv4, false);
 		blobmsg_close_array(&b, a);
 		a = blobmsg_open_array(&b, "ipv6-address");
-		interface_ip_dump_address_list(&iface->config_ip, now, true, false);
-		interface_ip_dump_address_list(&iface->proto_ip, now, true, false);
+		interface_ip_dump_address_list(&iface->config_ip, now, ADDR_IPv6, false);
+		interface_ip_dump_address_list(&iface->proto_ip, now, ADDR_IPv6_Active, false);
+		interface_ip_dump_address_list(&iface->proto_ip, now, ADDR_IPv6_Deprecated, false);
 		blobmsg_close_array(&b, a);
 		a = blobmsg_open_array(&b, "route");
 		interface_ip_dump_route_list(&iface->config_ip, now, false);
-- 
2.47.3




More information about the openwrt-devel mailing list