Netdev List
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: dsahern@kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute2] ip address: don't emit empty JSON objects for filtered addresses
Date: Mon, 16 Nov 2026 10:17:11 -0800	[thread overview]
Message-ID: <20261116181711.292634-1-stephen@networkplumber.org> (raw)

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.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 ip/ipaddress.c | 68 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 42 insertions(+), 26 deletions(-)

diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index 4a4a87d9..fb9c05ee 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -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();
-- 
2.53.0


                 reply	other threads:[~2026-11-16 18:17 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116181711.292634-1-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dsahern@kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox