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 --- 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