nl80211: look up the phy name instead of assuming name == phy<idx>
authorFelix Fietkau <nbd@nbd.name>
Thu, 22 Sep 2022 12:39:43 +0000 (14:39 +0200)
committerFelix Fietkau <nbd@nbd.name>
Thu, 22 Sep 2022 12:42:45 +0000 (14:42 +0200)
Signed-off-by: Felix Fietkau <nbd@nbd.name>
iwinfo_nl80211.c

index a708aec5bd21c0d10cac68c511d12e2d17cf3447..5d224b331c8de29cbec372ddd952948a57eb545d 100644 (file)
@@ -679,7 +679,7 @@ static int nl80211_channel2freq(int channel, const char *band)
        return 0;
 }
 
-static int nl80211_ifname2phy_cb(struct nl_msg *msg, void *arg)
+static int nl80211_phyname_cb(struct nl_msg *msg, void *arg)
 {
        char *buf = arg;
        struct nlattr **attr = nl80211_parse(msg);
@@ -700,11 +700,34 @@ static char * nl80211_ifname2phy(const char *ifname)
        memset(phy, 0, sizeof(phy));
 
        nl80211_request(ifname, NL80211_CMD_GET_WIPHY, 0,
-                       nl80211_ifname2phy_cb, phy);
+                       nl80211_phyname_cb, phy);
 
        return phy[0] ? phy : NULL;
 }
 
+static char * nl80211_phyidx2name(unsigned int idx)
+{
+       struct nl80211_msg_conveyor *cv;
+       static char phy[32] = { 0 };
+
+       if (nl80211_init() < 0)
+               return NULL;
+
+       cv = nl80211_new(nls->nl80211, NL80211_CMD_GET_WIPHY, 0);
+       if (!cv)
+               return NULL;
+
+       NLA_PUT_U32(cv->msg, NL80211_ATTR_WIPHY, idx);
+
+       memset(phy, 0, sizeof(phy));
+       nl80211_send(cv, nl80211_phyname_cb, phy);
+
+       return phy[0] ? phy : NULL;
+
+nla_put_failure:
+       return NULL;
+}
+
 static char * nl80211_phy2ifname(const char *ifname)
 {
        int ifidx = -1, cifidx = -1, phyidx = -1;
@@ -3507,6 +3530,7 @@ static int nl80211_get_frequency_offset(const char *ifname, int *buf)
 
 static int nl80211_lookup_phyname(const char *section, char *buf)
 {
+       const char *name;
        int idx;
 
        if (!strncmp(section, "path=", 5))
@@ -3519,7 +3543,11 @@ static int nl80211_lookup_phyname(const char *section, char *buf)
        if (idx < 0)
                return -1;
 
-       sprintf(buf, "phy%d", idx);
+       name = nl80211_phyidx2name(idx);
+       if (!name)
+               return -1;
+
+       strcpy(buf, name);
        return 0;
 }