Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Stephen Hemminger
Mon, 16 Nov 2026 18:17
[PATCH iproute2] ip address: don't emit empty JSON objects for filtered addresses
Author: Stephen Hemminger
Date: Mon, 16 Nov 2026 18:17
Date: Mon, 16 Nov 2026 18:17
122 lines
14436 bytes
14436 bytes
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
Re: [PATCH iproute2] ip address: don't emit empty JSON objects for filtered addresses
Author: patchwork-bot+netdevbpf
Date: Wed, 18 Nov 2026 21:50
Date: Wed, 18 Nov 2026 21:50
24 lines
8554 bytes
8554 bytes
Hello:
This patch was applied to iproute2/iproute2.git (main)
by Stephen Hemminger <stephen@networkplumber.org>:
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads