Skip to contentSkip to conversation

ip address: don't emit empty JSON objects for filtered addresses

PATCH Applied Stephen Hemminger 2 participants 2 messages Nov 16 – Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ip address: don't emit empty JSON objects for filtered addresses

print_selected_addrinfo() opens a JSON object before print_addrinfo() decides whether the address passes the filter, so every rejected address leaves an empty object in the addr_info array:

    # ip -j address show proto kernel_lo
    [{...,"addr_info":[{},{"family":"inet6","local":"::1",...}]}]

print_addrinfo() cannot report the rejection to its caller; it is also the rtnl_dump_filter() callback, where a non-zero return aborts the dump.

Move the per-address filter predicates into ipaddr_filtered() and check them before opening the object.

---
 ip/ipaddress.c | 68 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 26 deletions(-)
ip/ipaddress.c +42 -26
index 4a4a87d9..fb9c05ee 100644
@@ -1497,6 +1497,41 @@ static int ifa_label_match_rta(int ifindex, const struct rtattr *rta)
 	return fnmatch(filter.label, label, 0);
 }
 
+/* Aliases IFA_LOCAL and IFA_ADDRESS in the caller's table. */
+static bool ipaddr_filtered(const struct ifaddrmsg *ifa, struct rtattr **rta_tb,
+			    unsigned int ifa_flags)
+{
+	if (!rta_tb[IFA_LOCAL])
+		rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
+	if (!rta_tb[IFA_ADDRESS])
+		rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
+
+	if (filter.ifindex && filter.ifindex != ifa->ifa_index)
+		return true;
+	if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
+		return true;
+	if ((filter.flags ^ ifa_flags) & filter.flagmask)
+		return true;
+
+	if (filter.family && filter.family != ifa->ifa_family)
+		return true;
+	if (filter.have_proto) {
+		__u8 proto = rta_tb[IFA_PROTO] ?
+			rta_getattr_u8(rta_tb[IFA_PROTO]) : IFAPROT_UNSPEC;
+
+		if (filter.proto != proto)
+			return true;
+	}
+
+	if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL]))
+		return true;
+
+	if (inet_addr_match_rta(&filter.pfx, rta_tb[IFA_LOCAL]))
+		return true;
+
+	return false;
+}
+
 int print_addrinfo(struct nlmsghdr *n, void *arg)
 {
 	FILE *fp = arg;
@@ -1528,32 +1563,7 @@ int print_addrinfo(struct nlmsghdr *n, void *arg)
 
 	ifa_flags = get_ifa_flags(ifa, rta_tb[IFA_FLAGS]);
 
-	if (!rta_tb[IFA_LOCAL])
-		rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
-	if (!rta_tb[IFA_ADDRESS])
-		rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
-
-	if (filter.ifindex && filter.ifindex != ifa->ifa_index)
-		return 0;
-	if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
-		return 0;
-	if ((filter.flags ^ ifa_flags) & filter.flagmask)
-		return 0;
-
-	if (filter.family && filter.family != ifa->ifa_family)
-		return 0;
-	if (filter.have_proto) {
-		__u8 proto = rta_tb[IFA_PROTO] ?
-			rta_getattr_u8(rta_tb[IFA_PROTO]) : IFAPROT_UNSPEC;
-
-		if (filter.proto != proto)
-			return 0;
-	}
-
-	if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL]))
-		return 0;
-
-	if (inet_addr_match_rta(&filter.pfx, rta_tb[IFA_LOCAL]))
+	if (ipaddr_filtered(ifa, rta_tb, ifa_flags))
 		return 0;
 
 	if (filter.flushb) {
@@ -1731,6 +1741,7 @@ static int print_selected_addrinfo(struct ifinfomsg *ifi,
 	for ( ; ainfo ;  ainfo = ainfo->next) {
 		struct nlmsghdr *n = &ainfo->h;
 		struct ifaddrmsg *ifa = NLMSG_DATA(n);
+		struct rtattr *rta_tb[IFA_MAX+1];
 
 		if (n->nlmsg_type != RTM_NEWADDR)
 			continue;
@@ -1748,6 +1759,11 @@ static int print_selected_addrinfo(struct ifinfomsg *ifi,
 		if (filter.down && ifi->ifi_flags&IFF_UP)
 			continue;
 
+		parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
+		if (ipaddr_filtered(ifa, rta_tb,
+				    get_ifa_flags(ifa, rta_tb[IFA_FLAGS])))
+			continue;
+
 		open_json_object(NULL);
 		print_addrinfo(n, fp);
 		close_json_object();
Signature · 1 line
2.53.0
#1 lore raw
This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 8 lines · On Mon, 16 Nov 2026 10:17:11 -0800 you wrote:

print_selected_addrinfo() opens a JSON object before print_addrinfo() decides whether the address passes the filter, so every rejected address leaves an empty object in the addr_info array:

    # ip -j address show proto kernel_lo
    [{...,"addr_info":[{},{"family":"inet6","local":"::1",...}]}]

[...]

Here is the summary with links:
- [iproute2] ip address: don't emit empty JSON objects for filtered addresses

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=87cb3f419850

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html