Subject: Aoe-sancheck: Refine interface probing Origin: aoetools-36-11-g4a3ee18 Upstream-Author: Christoph Biedl Date: Sat May 22 14:37:25 2021 +0200 There are two issues in the ethlist function in aoe-sancheck.c: First it skips all network interfaces that do not start with "eth". This is a problem these days where people use different naming schemes like the so-called predictable interface names as mandated by udev/systemd. Second it only probes for interfaces with an interface number of up to 15. At least in current Linux, that number can take huge values and those interfaces will be ignored completely. About the first: Probe all interfaces (but lo) that support the ARP protocol, assuming that is a fair indicator for ethernet support. About the second, use the if_nameindex function to get a list of all interfaces currently known to the kernel and operate on that one. --- a/aoe-sancheck.c +++ b/aoe-sancheck.c @@ -501,23 +501,38 @@ int ethlist(char **ifs, int nifs) { - int i, s, n; + int s, n; struct ifreq ifr; + struct if_nameindex *if_ni, *i; s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) return 0; + + if_ni = if_nameindex(); + if (if_ni == NULL) + return 0; + n = 0; - for (i=0; iif_index == 0 && i->if_name == NULL); i++) { memset(&ifr, 0, sizeof ifr); - ifr.ifr_ifindex = i; - if (ioctl(s, SIOCGIFNAME, &ifr) < 0) + ifr.ifr_ifindex = i->if_index; + strcpy(ifr.ifr_name, i->if_name); + // get interface flags + if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) + continue; + // only use interfaces that use arp protocol + if (ifr.ifr_flags & IFF_NOARP) continue; - if (strncmp(ifr.ifr_name, "eth", 3)) + // skip loopback interfaces + if (ifr.ifr_flags & IFF_LOOPBACK) continue; + if (n == nifs) + break; inserteth(ifs, nifs, ifr.ifr_name); n++; } + if_freenameindex(if_ni); close(s); return n; }