nl80211: fix phy/netdev index lookup
authorFelix Fietkau <nbd@nbd.name>
Thu, 22 Sep 2022 12:22:57 +0000 (14:22 +0200)
committerFelix Fietkau <nbd@nbd.name>
Thu, 22 Sep 2022 12:42:45 +0000 (14:42 +0200)
Don't assume a fixed naming pattern. Check all options in this order:

- netdev
- phy name
- wifi device in uci

Use the first one that matches

Signed-off-by: Felix Fietkau <nbd@nbd.name>
iwinfo_nl80211.c

index 15831c95087d70e50667ba59bb5191c77875bb5f..a708aec5bd21c0d10cac68c511d12e2d17cf3447 100644 (file)
@@ -409,7 +409,8 @@ out:
 static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
                                                  int cmd, int flags)
 {
-       int ifidx = -1, phyidx = -1;
+       unsigned int ifidx = 0;
+       int phyidx = -1;
        struct nl80211_msg_conveyor *cv;
 
        if (ifname == NULL)
@@ -418,16 +419,17 @@ static struct nl80211_msg_conveyor * nl80211_msg(const char *ifname,
        if (nl80211_init() < 0)
                return NULL;
 
-       if (!strncmp(ifname, "phy", 3))
-               phyidx = atoi(&ifname[3]);
-       else if (!strncmp(ifname, "radio", 5))
-               phyidx = nl80211_phy_idx_from_uci(ifname);
-
        if (!strncmp(ifname, "mon.", 4))
                ifidx = if_nametoindex(&ifname[4]);
        else
                ifidx = if_nametoindex(ifname);
 
+       if (!ifidx) {
+               phyidx = nl80211_phy_idx_from_phy(ifname);
+               if (phyidx < 0)
+                       phyidx = nl80211_phy_idx_from_uci(ifname);
+       }
+
        /* Valid ifidx must be greater than 0 */
        if ((ifidx <= 0) && (phyidx < 0))
                return NULL;
@@ -715,11 +717,11 @@ static char * nl80211_phy2ifname(const char *ifname)
        /* Only accept phy name of the form phy%d or radio%d */
        if (!ifname)
                return NULL;
-       else if (!strncmp(ifname, "phy", 3))
-               phyidx = atoi(&ifname[3]);
-       else if (!strncmp(ifname, "radio", 5))
-               phyidx = nl80211_phy_idx_from_uci(ifname);
-       else
+
+       phyidx = nl80211_phy_idx_from_phy(ifname);
+       if (phyidx < 0)
+               phyidx = nl80211_phy_idx_from_uci(ifname);;
+       if (phyidx < 0)
                return NULL;
 
        memset(nif, 0, sizeof(nif));