ipaddress: Fix and make consistent label match handling
Subsystems (2)
PATCH 1/1 ipaddress: Fix and make consistent label match handling
Since commit 9516823051ce ("ipaddress: Improve print_linkinfo()") we return -1 instead of 0 when ip-address(8) label does not match network device name as we did before change. This causes regression when trying to output ip address matching label:
# ip addr add 192.168.192.1/24 dev lo label lo:1
# ip addr show label lo:1
<no output>This is special case and return 0 from print_linkinfo() earlier to match only filter.ifindex and filter.up if given, but not rest fields in @filter. Then call print_selected_addrinfo() without calling print_link_stats() in ipaddr_list_flush_or_save().
Later print_selected_addrinfo() calls print_addrinfo() that finally matches IFA_LABEL attribute in netlink buffer with filter.label using ifa_label_match_rta().
On the other hand there is three conditions checked in print_linkinfo() to determine label special case:
1) filter.label != NULL
2) filter.family == AF_UNSPEC || filter.family == AF_PACKET
3) fnmatch(filter.label, name, 0)
With 1) it is ok to check if filtering by label is on by given pattern in @filter.label.
Since label is IPv4 specific and AF_PACKET is for printing ip-link(8) information (see ipaddr_link_list()::ipaddress.c as example) checking for AF_PACKET in 2) doesn't take much sense: better to defer these checks to print_addrinfo() determine valid combinations before calling ifa_label_match_rta() to finally match IFA_LABEL to pattern in filter.label.
For 3) we have following call for test case:
fnmatch(pattern, string, flags) ->
fnmatch(filter.label, name, 0) ->
fnmatch("lo:1", "lo", 0) == FNM_NOMATCH (1) or non-zero on errorTo support special case in print_linkinfo() for filtering by label we only need to check if label pattern is given in filter.label and return 0 to skip print_link_stats() in ipaddr_list_flush_or_save(): actual filtering will be done in print_addrinfo().
Before commit 9516823051ce ("ipaddress: Improve print_linkinfo()"):
-------------------------------------------------------------------
$ ip addr sh label lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN \
group default qlen 1000
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
fnmatch("lo", "lo", 0) == 0
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
$ ip addr show label 'lo:*'
inet 192.168.192.1/24 scope global lo:1
valid_lft forever preferred_lft forever
$ ip addr sh label lo:1
inet 192.168.192.1/24 scope global lo:1
valid_lft forever preferred_lft forever
$ ip -4 addr sh label lo:1
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN \
group default qlen 1000
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
filter.family == AF_INET
inet 192.168.192.1/24 scope global lo:1
valid_lft forever preferred_lft foreverAfter this change applied:
--------------------------
$ ip/ip addr show label lo
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
$ ip/ip addr show label 'lo:*'
inet 192.168.192.1/24 scope global lo:1
valid_lft forever preferred_lft forever
$ ip/ip addr show label lo:1
inet 192.168.192.1/24 scope global lo:1
valid_lft forever preferred_lft forever
$ ip/ip -4 addr show label lo:1
inet 192.168.192.1/24 scope global lo:1
valid_lft forever preferred_lft foreverNote that we no longer show link information as we did previously:
we are filtering by "label" pattern, not showing by "dev".
--- ip/ipaddress.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
ip/ipaddress.c +2 -4
@@ -837,10 +837,8 @@ int print_linkinfo(const struct sockaddr_nl *who,if (!name)return -1 ; -if (filter.label && - (!filter.family || filter.family ==AF_PACKET ) && -fnmatch (filter.label, name,0 )) -return -1 ; +if (filter.label) +return 0 ;if (tb[IFLA_GROUP ]) {int group =rta_getattr_u32 (tb[IFLA_GROUP ]);
Signature · 1 line
1.7.10.4
Quoted 91 lines · On Sun, 15 Jul 2018 00:36:34 +0300 Serhey Popovych <[email protected]>…
Since commit
9516823051ce("ipaddress: Improveprint_linkinfo()") we return -1 instead of 0 when ip-address(8) label does not match network device name as we did before change. This causes regression when trying to output ip address matching label:# ip addr add 192.168.192.1/24 dev lo label lo:1 # ip addr show label lo:1 <no output>This is special case and return 0 from
print_linkinfo()earlier to match only filter.ifindex and filter.up if given, but not rest fields in @filter. Then callprint_selected_addrinfo()without callingprint_link_stats()inipaddr_list_flush_or_save().Later
print_selected_addrinfo()callsprint_addrinfo()that finally matchesIFA_LABELattribute in netlink buffer with filter.label usingifa_label_match_rta().On the other hand there is three conditions checked in
print_linkinfo()to determine label special case:1) filter.label != NULL
2) filter.family ==AF_UNSPEC|| filter.family ==AF_PACKET
3)fnmatch(filter.label, name, 0)With 1) it is ok to check if filtering by label is on by given pattern in @filter.label.
Since label is IPv4 specific and
AF_PACKETis for printing ip-link(8) information (see ipaddr_link_list()::ipaddress.c as example) checking forAF_PACKETin 2) doesn't take much sense: better to defer these checks toprint_addrinfo()determine valid combinations before callingifa_label_match_rta()to finally matchIFA_LABELto pattern in filter.label.For 3) we have following call for test case:
fnmatch(pattern, string, flags) -> fnmatch(filter.label, name, 0) -> fnmatch("lo:1", "lo", 0) == FNM_NOMATCH (1) or non-zero on errorTo support special case in
print_linkinfo()for filtering by label we only need to check if label pattern is given in filter.label and return 0 to skipprint_link_stats()inipaddr_list_flush_or_save(): actual filtering will be done inprint_addrinfo().Before commit
9516823051ce("ipaddress: Improveprint_linkinfo()"):
-------------------------------------------------------------------$ ip addr sh label lo 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN \ group default qlen 1000 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ fnmatch("lo", "lo", 0) == 0 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever $ ip addr show label 'lo:*' inet 192.168.192.1/24 scope global lo:1 valid_lft forever preferred_lft forever $ ip addr sh label lo:1 inet 192.168.192.1/24 scope global lo:1 valid_lft forever preferred_lft forever $ ip -4 addr sh label lo:1 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN \ group default qlen 1000 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ filter.family == AF_INET inet 192.168.192.1/24 scope global lo:1 valid_lft forever preferred_lft foreverAfter this change applied:
--------------------------$ ip/ip addr show label lo inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever $ ip/ip addr show label 'lo:*' inet 192.168.192.1/24 scope global lo:1 valid_lft forever preferred_lft forever $ ip/ip addr show label lo:1 inet 192.168.192.1/24 scope global lo:1 valid_lft forever preferred_lft forever $ ip/ip -4 addr show label lo:1 inet 192.168.192.1/24 scope global lo:1 valid_lft forever preferred_lft forever
Quoted 6 lines
Note that we no longer show link information as we did previously:
we are filtering by "label" pattern, not showing by "dev".Fixes commit 9516823051ce ("ipaddress: Improve print_linkinfo()")Reported-by Vincent Bernat <[email protected]>Signed-off-by Serhey Popovych <[email protected]>
Makes sense applied. Thanks for following through on this.