]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - station.c
iw: scan: fix double-free in error paths
[thirdparty/iw.git] / station.c
index 4885dc07d7c6eda064c16820630bae8173fc6ac3..1be3974042df3fc6085ad5103c542d19cfc39a5c 100644 (file)
--- a/station.c
+++ b/station.c
@@ -7,6 +7,7 @@
 #include <netlink/genl/ctrl.h>
 #include <netlink/msg.h>
 #include <netlink/attr.h>
+#include <time.h>
 
 #include "nl80211.h"
 #include "iw.h"
@@ -43,7 +44,64 @@ static void print_power_mode(struct nlattr *a)
        }
 }
 
-void parse_tid_stats(struct nlattr *tid_stats_attr)
+int parse_txq_stats(char *buf, int buflen, struct nlattr *tid_stats_attr, int header,
+                   int tid, const char *indent)
+{
+       struct nlattr *txqstats_info[NL80211_TXQ_STATS_MAX + 1], *txqinfo;
+       static struct nla_policy txqstats_policy[NL80211_TXQ_STATS_MAX + 1] = {
+               [NL80211_TXQ_STATS_BACKLOG_BYTES] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_BACKLOG_PACKETS] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_FLOWS] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_DROPS] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_ECN_MARKS] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_OVERLIMIT] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_COLLISIONS] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_TX_BYTES] = { .type = NLA_U32 },
+               [NL80211_TXQ_STATS_TX_PACKETS] = { .type = NLA_U32 },
+       };
+       char *pos = buf;
+       if (nla_parse_nested(txqstats_info, NL80211_TXQ_STATS_MAX, tid_stats_attr,
+                            txqstats_policy)) {
+               printf("failed to parse nested TXQ stats attributes!");
+               return 0;
+       }
+
+       if (header)
+               pos += snprintf(buf, buflen, "\n%s\t%s\tqsz-byt\t"
+                               "qsz-pkt\tflows\tdrops\tmarks\toverlmt\t"
+                               "hashcol\ttx-bytes\ttx-packets", indent,
+                               tid >= 0 ? "TID" : "");
+
+       pos += snprintf(pos, buflen - (pos - buf), "\n%s\t", indent);
+       if (tid >= 0)
+               pos += snprintf(pos, buflen - (pos - buf), "%d", tid);
+
+#define PRINT_STAT(key, spacer) do {                                    \
+               txqinfo = txqstats_info[NL80211_TXQ_STATS_ ## key];      \
+               pos += snprintf(pos, buflen - (pos - buf), spacer);      \
+               if (txqinfo)                                             \
+                       pos += snprintf(pos, buflen - (pos - buf), "%u", \
+                                       nla_get_u32(txqinfo));           \
+       } while (0)
+
+
+       PRINT_STAT(BACKLOG_BYTES, "\t");
+       PRINT_STAT(BACKLOG_PACKETS, "\t");
+       PRINT_STAT(FLOWS, "\t");
+       PRINT_STAT(DROPS, "\t");
+       PRINT_STAT(ECN_MARKS, "\t");
+       PRINT_STAT(OVERLIMIT, "\t");
+       PRINT_STAT(COLLISIONS, "\t");
+       PRINT_STAT(TX_BYTES, "\t");
+       PRINT_STAT(TX_PACKETS, "\t\t");
+
+#undef PRINT_STAT
+
+       return pos - buf;
+
+}
+
+static void parse_tid_stats(struct nlattr *tid_stats_attr)
 {
        struct nlattr *stats_info[NL80211_TID_STATS_MAX + 1], *tidattr, *info;
        static struct nla_policy stats_policy[NL80211_TID_STATS_MAX + 1] = {
@@ -51,8 +109,11 @@ void parse_tid_stats(struct nlattr *tid_stats_attr)
                [NL80211_TID_STATS_TX_MSDU] = { .type = NLA_U64 },
                [NL80211_TID_STATS_TX_MSDU_RETRIES] = { .type = NLA_U64 },
                [NL80211_TID_STATS_TX_MSDU_FAILED] = { .type = NLA_U64 },
+               [NL80211_TID_STATS_TXQ_STATS] = { .type = NLA_NESTED },
        };
        int rem, i = 0;
+       char txqbuf[2000] = {}, *pos = txqbuf;
+       int buflen = sizeof(txqbuf), foundtxq = 0;
 
        printf("\n\tMSDU:\n\t\tTID\trx\ttx\ttx retries\ttx failed");
        nla_for_each_nested(tidattr, tid_stats_attr, rem) {
@@ -61,7 +122,7 @@ void parse_tid_stats(struct nlattr *tid_stats_attr)
                        printf("failed to parse nested stats attributes!");
                        return;
                }
-               printf("\n\t\t%d", i++);
+               printf("\n\t\t%d", i);
                info = stats_info[NL80211_TID_STATS_RX_MSDU];
                if (info)
                        printf("\t%llu", (unsigned long long)nla_get_u64(info));
@@ -74,10 +135,20 @@ void parse_tid_stats(struct nlattr *tid_stats_attr)
                info = stats_info[NL80211_TID_STATS_TX_MSDU_FAILED];
                if (info)
                        printf("\t\t%llu", (unsigned long long)nla_get_u64(info));
+               info = stats_info[NL80211_TID_STATS_TXQ_STATS];
+               if (info) {
+                       pos += parse_txq_stats(pos, buflen - (pos - txqbuf), info, !foundtxq, i, "\t");
+                       foundtxq = 1;
+               }
+
+               i++;
        }
+
+       if (foundtxq)
+               printf("\n\tTXQs:%s", txqbuf);
 }
 
-void parse_bss_param(struct nlattr *bss_param_attr)
+static void parse_bss_param(struct nlattr *bss_param_attr)
 {
        struct nlattr *bss_param_info[NL80211_STA_BSS_PARAM_MAX + 1], *info;
        static struct nla_policy bss_poilcy[NL80211_STA_BSS_PARAM_MAX + 1] = {
@@ -173,6 +244,18 @@ void parse_bitrate(struct nlattr *bitrate_attr, char *buf, int buflen)
        if (rinfo[NL80211_RATE_INFO_VHT_NSS])
                pos += snprintf(pos, buflen - (pos - buf),
                                " VHT-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_VHT_NSS]));
+       if (rinfo[NL80211_RATE_INFO_HE_MCS])
+               pos += snprintf(pos, buflen - (pos - buf),
+                               " HE-MCS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_MCS]));
+       if (rinfo[NL80211_RATE_INFO_HE_NSS])
+               pos += snprintf(pos, buflen - (pos - buf),
+                               " HE-NSS %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_NSS]));
+       if (rinfo[NL80211_RATE_INFO_HE_GI])
+               pos += snprintf(pos, buflen - (pos - buf),
+                               " HE-GI %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_GI]));
+       if (rinfo[NL80211_RATE_INFO_HE_DCM])
+               pos += snprintf(pos, buflen - (pos - buf),
+                               " HE-DCM %d", nla_get_u8(rinfo[NL80211_RATE_INFO_HE_DCM]));
 }
 
 static char *get_chain_signal(struct nlattr *attr_list)
@@ -239,8 +322,17 @@ static int print_sta_handler(struct nl_msg *msg, void *arg)
                [NL80211_STA_INFO_TID_STATS] = { .type = NLA_NESTED },
                [NL80211_STA_INFO_BSS_PARAM] = { .type = NLA_NESTED },
                [NL80211_STA_INFO_RX_DURATION] = { .type = NLA_U64 },
+               [NL80211_STA_INFO_TX_DURATION] = { .type = NLA_U64 },
+               [NL80211_STA_INFO_ACK_SIGNAL] = {.type = NLA_U8 },
+               [NL80211_STA_INFO_ACK_SIGNAL_AVG] = { .type = NLA_U8 },
        };
        char *chain;
+       struct timeval now;
+       unsigned long long now_ms;
+
+       gettimeofday(&now, NULL);
+       now_ms = now.tv_sec * 1000;
+       now_ms += (now.tv_usec / 1000);
 
        nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
                  genlmsg_attrlen(gnlh, 0), NULL);
@@ -317,7 +409,7 @@ static int print_sta_handler(struct nl_msg *msg, void *arg)
 
        if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
                printf("\n\tbeacon signal avg:\t%d dBm",
-                      nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]));
+                      (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]));
        if (sinfo[NL80211_STA_INFO_T_OFFSET])
                printf("\n\tToffset:\t%llu us",
                       (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_T_OFFSET]));
@@ -329,6 +421,10 @@ static int print_sta_handler(struct nl_msg *msg, void *arg)
                printf("\n\ttx bitrate:\t%s", buf);
        }
 
+       if (sinfo[NL80211_STA_INFO_TX_DURATION])
+               printf("\n\ttx duration:\t%lld us",
+                      (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_TX_DURATION]));
+
        if (sinfo[NL80211_STA_INFO_RX_BITRATE]) {
                char buf[100];
 
@@ -340,6 +436,18 @@ static int print_sta_handler(struct nl_msg *msg, void *arg)
                printf("\n\trx duration:\t%lld us",
                       (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_RX_DURATION]));
 
+       if (sinfo[NL80211_STA_INFO_ACK_SIGNAL])
+               printf("\n\tlast ack signal:%d dBm",
+                       (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_ACK_SIGNAL]));
+
+       if (sinfo[NL80211_STA_INFO_ACK_SIGNAL_AVG])
+               printf("\n\tavg ack signal:\t%d dBm",
+                       (int8_t)nla_get_u8(sinfo[NL80211_STA_INFO_ACK_SIGNAL_AVG]));
+
+       if (sinfo[NL80211_STA_INFO_AIRTIME_WEIGHT]) {
+               printf("\n\tairtime weight: %d", nla_get_u16(sinfo[NL80211_STA_INFO_AIRTIME_WEIGHT]));
+       }
+
        if (sinfo[NL80211_STA_INFO_EXPECTED_THROUGHPUT]) {
                uint32_t thr;
 
@@ -468,8 +576,24 @@ static int print_sta_handler(struct nl_msg *msg, void *arg)
        if (sinfo[NL80211_STA_INFO_CONNECTED_TIME])
                printf("\n\tconnected time:\t%u seconds",
                        nla_get_u32(sinfo[NL80211_STA_INFO_CONNECTED_TIME]));
+       if (sinfo[NL80211_STA_INFO_ASSOC_AT_BOOTTIME]) {
+               unsigned long long bt;
+               struct timespec now_ts;
+               unsigned long long boot_ns;
+               unsigned long long assoc_at_ms;
+
+               clock_gettime(CLOCK_BOOTTIME, &now_ts);
+               boot_ns = now_ts.tv_sec * 1000000000;
+               boot_ns += now_ts.tv_nsec;
+
+               bt = (unsigned long long)nla_get_u64(sinfo[NL80211_STA_INFO_ASSOC_AT_BOOTTIME]);
+               printf("\n\tassociated at [boottime]:\t%llu.%.3llus",
+                      bt/1000000000, (bt%1000000000)/1000000);
+               assoc_at_ms = now_ms - ((boot_ns - bt) / 1000000);
+               printf("\n\tassociated at:\t%llu ms", assoc_at_ms);
+       }
 
-       printf("\n");
+       printf("\n\tcurrent time:\t%llu ms\n", now_ms);
        return NL_SKIP;
 }
 
@@ -571,6 +695,8 @@ COMMAND(station, del, "<MAC address> [subtype <subtype>] [reason-code <code>]",
 static const struct cmd *station_set_plink;
 static const struct cmd *station_set_vlan;
 static const struct cmd *station_set_mesh_power_mode;
+static const struct cmd *station_set_airtime_weight;
+static const struct cmd *station_set_txpwr;
 
 static const struct cmd *select_station_cmd(int argc, char **argv)
 {
@@ -582,6 +708,10 @@ static const struct cmd *select_station_cmd(int argc, char **argv)
                return station_set_vlan;
        if (strcmp(argv[1], "mesh_power_mode") == 0)
                return station_set_mesh_power_mode;
+       if (strcmp(argv[1], "airtime_weight") == 0)
+               return station_set_airtime_weight;
+       if (strcmp(argv[1], "txpwr") == 0)
+               return station_set_txpwr;
        return NULL;
 }
 
@@ -733,6 +863,120 @@ COMMAND_ALIAS(station, set, "<MAC address> mesh_power_mode "
        "Set link-specific mesh power mode for this station",
        select_station_cmd, station_set_mesh_power_mode);
 
+static int handle_station_set_airtime_weight(struct nl80211_state *state,
+                                            struct nl_msg *msg,
+                                            int argc, char **argv,
+                                            enum id_input id)
+{
+       unsigned char mac_addr[ETH_ALEN];
+       unsigned long airtime_weight = 0;
+       char *err = NULL;
+
+       if (argc < 3)
+               return 1;
+
+       if (mac_addr_a2n(mac_addr, argv[0])) {
+               fprintf(stderr, "invalid mac address\n");
+               return 2;
+       }
+       argc--;
+       argv++;
+
+       if (strcmp("airtime_weight", argv[0]) != 0)
+               return 1;
+       argc--;
+       argv++;
+
+       airtime_weight = strtoul(argv[0], &err, 0);
+       if (err && *err) {
+               fprintf(stderr, "invalid airtime weight\n");
+               return 2;
+       }
+       argc--;
+       argv++;
+
+       if (argc)
+               return 1;
+
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
+       NLA_PUT_U16(msg, NL80211_ATTR_AIRTIME_WEIGHT, airtime_weight);
+
+       return 0;
+ nla_put_failure:
+       return -ENOBUFS;
+
+}
+COMMAND_ALIAS(station, set, "<MAC address> airtime_weight <weight>",
+       NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_airtime_weight,
+       "Set airtime weight for this station.",
+       select_station_cmd, station_set_airtime_weight);
+
+static int handle_station_set_txpwr(struct nl80211_state *state,
+                                   struct nl_msg *msg,
+                                   int argc, char **argv,
+                                   enum id_input id)
+{
+       enum nl80211_tx_power_setting type;
+       unsigned char mac_addr[ETH_ALEN];
+       int sta_txpwr = 0;
+       char *err = NULL;
+
+       if (argc != 3 && argc != 4)
+               return 1;
+
+       if (mac_addr_a2n(mac_addr, argv[0])) {
+               fprintf(stderr, "invalid mac address\n");
+               return 2;
+       }
+
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
+       argc--;
+       argv++;
+
+       if (strcmp("txpwr", argv[0]) != 0)
+               return 1;
+       argc--;
+       argv++;
+
+       if (!strcmp(argv[0], "auto"))
+               type = NL80211_TX_POWER_AUTOMATIC;
+       else if (!strcmp(argv[0], "limit"))
+               type = NL80211_TX_POWER_LIMITED;
+       else {
+               printf("Invalid parameter: %s\n", argv[0]);
+               return 2;
+       }
+
+       NLA_PUT_U8(msg, NL80211_ATTR_STA_TX_POWER_SETTING, type);
+
+       if (type != NL80211_TX_POWER_AUTOMATIC) {
+               if (argc != 2) {
+                       printf("Missing TX power level argument.\n");
+                       return 2;
+               }
+
+               argc--;
+               argv++;
+
+               sta_txpwr = strtoul(argv[0], &err, 0);
+               NLA_PUT_U16(msg, NL80211_ATTR_STA_TX_POWER, sta_txpwr);
+       }
+
+       argc--;
+       argv++;
+
+       if (argc)
+               return 1;
+
+       return 0;
+ nla_put_failure:
+       return -ENOBUFS;
+}
+COMMAND_ALIAS(station, set, "<MAC address> txpwr <auto|limit> [<tx power dBm>]",
+       NL80211_CMD_SET_STATION, 0, CIB_NETDEV, handle_station_set_txpwr,
+       "Set Tx power for this station.",
+       select_station_cmd, station_set_txpwr);
+
 static int handle_station_dump(struct nl80211_state *state,
                               struct nl_msg *msg,
                               int argc, char **argv,