nl80211: fix possible fd leak in nl80211_hostapd_hup()
[project/iwinfo.git] / iwinfo_nl80211.c
index 900eef2..a08d580 100644 (file)
@@ -31,6 +31,8 @@
 
 #define min(x, y) ((x) < (y)) ? (x) : (y)
 
+#define BIT(x) (1ULL<<(x))
+
 static struct nl80211_state *nls = NULL;
 
 static void nl80211_close(void)
@@ -949,7 +951,7 @@ static void nl80211_hostapd_hup(const char *ifname)
        if (phy)
        {
                snprintf(buf, sizeof(buf), "/var/run/wifi-%s.pid", phy);
-               if ((fd = open(buf, O_RDONLY)) > 0)
+               if ((fd = open(buf, O_RDONLY)) >= 0)
                {
                        if (read(fd, buf, sizeof(buf)) > 0)
                                pid = atoi(buf);
@@ -1186,22 +1188,35 @@ static int nl80211_get_channel(const char *ifname, int *buf)
        return -1;
 }
 
+static int nl80211_get_txpower_cb(struct nl_msg *msg, void *arg)
+{
+       int *buf = arg;
+       struct nlattr **tb = nl80211_parse(msg);
+
+       if (tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL])
+               *buf = iwinfo_mbm2dbm(nla_get_u32(tb[NL80211_ATTR_WIPHY_TX_POWER_LEVEL]));
+
+       return NL_SKIP;
+}
 
 static int nl80211_get_txpower(const char *ifname, int *buf)
 {
-#if 0
        char *res;
-       char path[PATH_MAX];
+       struct nl80211_msg_conveyor *req;
 
-       res = nl80211_ifname2phy(ifname);
-       snprintf(path, sizeof(path), "/sys/kernel/debug/ieee80211/%s/power",
-                res ? res : ifname);
+       res = nl80211_phy2ifname(ifname);
+       req = nl80211_msg(res ? res : ifname, NL80211_CMD_GET_INTERFACE, 0);
 
-       if ((*buf = nl80211_readint(path)) > -1)
-               return 0;
-#endif
+       if (req)
+       {
+               *buf = 0;
+               nl80211_send(req, nl80211_get_txpower_cb, buf);
+               nl80211_free(req);
+               if (*buf)
+                       return 0;
+       }
 
-       return wext_ops.txpower(ifname, buf);
+       return -1;
 }
 
 
@@ -1620,6 +1635,7 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
        struct nlattr **attr = nl80211_parse(msg);
        struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
        struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
+       struct nl80211_sta_flag_update *sta_flags;
 
        static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
                [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32    },
@@ -1628,6 +1644,13 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_RX_BITRATE]    = { .type = NLA_NESTED },
                [NL80211_STA_INFO_TX_BITRATE]    = { .type = NLA_NESTED },
                [NL80211_STA_INFO_SIGNAL]        = { .type = NLA_U8     },
+               [NL80211_STA_INFO_RX_BYTES]      = { .type = NLA_U32    },
+               [NL80211_STA_INFO_TX_BYTES]      = { .type = NLA_U32    },
+               [NL80211_STA_INFO_TX_RETRIES]    = { .type = NLA_U32    },
+               [NL80211_STA_INFO_TX_FAILED]     = { .type = NLA_U32    },
+               [NL80211_STA_INFO_T_OFFSET]      = { .type = NLA_U64    },
+               [NL80211_STA_INFO_STA_FLAGS] =
+                       { .minlen = sizeof(struct nl80211_sta_flag_update) },
        };
 
        static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
@@ -1695,6 +1718,52 @@ static int nl80211_get_assoclist_cb(struct nl_msg *msg, void *arg)
                        if (rinfo[NL80211_RATE_INFO_SHORT_GI])
                                e->tx_rate.is_short_gi = 1;
                }
+
+               if (sinfo[NL80211_STA_INFO_RX_BYTES])
+                       e->rx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_RX_BYTES]);
+
+               if (sinfo[NL80211_STA_INFO_TX_BYTES])
+                       e->tx_bytes = nla_get_u32(sinfo[NL80211_STA_INFO_TX_BYTES]);
+
+               if (sinfo[NL80211_STA_INFO_TX_RETRIES])
+                       e->tx_retries = nla_get_u32(sinfo[NL80211_STA_INFO_TX_RETRIES]);
+
+               if (sinfo[NL80211_STA_INFO_TX_FAILED])
+                       e->tx_failed = nla_get_u32(sinfo[NL80211_STA_INFO_TX_FAILED]);
+
+               if (sinfo[NL80211_STA_INFO_T_OFFSET])
+                       e->t_offset = nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]);
+
+               /* Station flags */
+               if (sinfo[NL80211_STA_INFO_STA_FLAGS])
+               {
+                       sta_flags = (struct nl80211_sta_flag_update *)
+                               nla_data(sinfo[NL80211_STA_INFO_STA_FLAGS]);
+
+                       if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
+                           sta_flags->set & BIT(NL80211_STA_FLAG_AUTHORIZED))
+                               e->is_authorized = 1;
+
+                       if (sta_flags->mask & BIT(NL80211_STA_FLAG_AUTHENTICATED) &&
+                           sta_flags->set & BIT(NL80211_STA_FLAG_AUTHENTICATED))
+                               e->is_authenticated = 1;
+
+                       if (sta_flags->mask & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) &&
+                           sta_flags->set & BIT(NL80211_STA_FLAG_SHORT_PREAMBLE))
+                               e->is_preamble_short = 1;
+
+                       if (sta_flags->mask & BIT(NL80211_STA_FLAG_WME) &&
+                           sta_flags->set & BIT(NL80211_STA_FLAG_WME))
+                               e->is_wme = 1;
+
+                       if (sta_flags->mask & BIT(NL80211_STA_FLAG_MFP) &&
+                           sta_flags->set & BIT(NL80211_STA_FLAG_MFP))
+                               e->is_mfp = 1;
+
+                       if (sta_flags->mask & BIT(NL80211_STA_FLAG_TDLS_PEER) &&
+                           sta_flags->set & BIT(NL80211_STA_FLAG_TDLS_PEER))
+                               e->is_tdls = 1;
+               }
        }
 
        e->noise = 0; /* filled in by caller */
@@ -2424,6 +2493,7 @@ static int nl80211_get_countrylist(const char *ifname, char *buf, int *len)
                e->iso3166 = l->iso3166;
                e->ccode[0] = (l->iso3166 / 256);
                e->ccode[1] = (l->iso3166 % 256);
+               e->ccode[2] = 0;
        }
 
        *len = (count * sizeof(struct iwinfo_country_entry));
@@ -2628,7 +2698,7 @@ static int nl80211_get_mbssid_support(const char *ifname, int *buf)
 
 static int nl80211_get_hardware_id(const char *ifname, char *buf)
 {
-       int rv;
+       int rv = -1;
        char *res;
 
        /* Got a radioX pseudo interface, find some interface on it or create one */