]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - util.c
iw: add support for 5 and 10 MHz channels in IBSS mode
[thirdparty/iw.git] / util.c
diff --git a/util.c b/util.c
index 9fc08441a7bd79812dcd47a1b11d75ab7f938118..4542749a18bdd66d67fd79c08b26b57ca0c85f7d 100644 (file)
--- a/util.c
+++ b/util.c
@@ -48,41 +48,77 @@ int mac_addr_a2n(unsigned char *mac_addr, char *arg)
        return 0;
 }
 
-unsigned char *parse_hex(char *hex, size_t *outlen)
+int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len,
+                  unsigned char **mask)
 {
-       size_t len = strlen(hex);
-       unsigned char *result = calloc(len/2 + 2, 1);
+       size_t len = strlen(hexmask) / 2;
+       unsigned char *result_val;
+       unsigned char *result_mask = NULL;
+
        int pos = 0;
 
-       if (!result)
-               return NULL;
+       *result_len = 0;
 
-       *outlen = 0;
+       result_val = calloc(len + 2, 1);
+       if (!result_val)
+               goto error;
+       *result = result_val;
+       if (mask) {
+               result_mask = calloc(DIV_ROUND_UP(len, 8) + 2, 1);
+               if (!result_mask)
+                       goto error;
+               *mask = result_mask;
+       }
 
        while (1) {
-               int temp;
-               char *cp = strchr(hex, ':');
+               char *cp = strchr(hexmask, ':');
                if (cp) {
                        *cp = 0;
                        cp++;
                }
-               if (sscanf(hex, "%x", &temp) != 1)
-                       goto error;
-               if (temp < 0 || temp > 255)
-                       goto error;
 
-               (*outlen)++;
+               if (result_mask && (strcmp(hexmask, "-") == 0 ||
+                                   strcmp(hexmask, "xx") == 0 ||
+                                   strcmp(hexmask, "--") == 0)) {
+                       /* skip this byte and leave mask bit unset */
+               } else {
+                       int temp, mask_pos;
+                       char *end;
+
+                       temp = strtoul(hexmask, &end, 16);
+                       if (*end)
+                               goto error;
+                       if (temp < 0 || temp > 255)
+                               goto error;
+                       result_val[pos] = temp;
+
+                       mask_pos = pos / 8;
+                       if (result_mask)
+                               result_mask[mask_pos] |= 1 << (pos % 8);
+               }
+
+               (*result_len)++;
+               pos++;
 
-               result[pos++] = temp;
                if (!cp)
                        break;
-               hex = cp;
+               hexmask = cp;
        }
 
-       return result;
+       return 0;
  error:
-       free(result);
-       return NULL;
+       free(result_val);
+       free(result_mask);
+       return -1;
+}
+
+unsigned char *parse_hex(char *hex, size_t *outlen)
+{
+       unsigned char *result;
+
+       if (parse_hex_mask(hex, &result, outlen, NULL))
+               return NULL;
+       return result;
 }
 
 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
@@ -93,20 +129,27 @@ static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
        "AP/VLAN",
        "WDS",
        "monitor",
-       "mesh point"
+       "mesh point",
+       "P2P-client",
+       "P2P-GO",
+       "P2P-device",
 };
 
 static char modebuf[100];
 
 const char *iftype_name(enum nl80211_iftype iftype)
 {
-       if (iftype <= NL80211_IFTYPE_MAX)
+       if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
                return ifmodes[iftype];
        sprintf(modebuf, "Unknown mode (%d)", iftype);
        return modebuf;
 }
 
 static const char *commands[NL80211_CMD_MAX + 1] = {
+/*
+ * sed 's/^\tNL80211_CMD_//;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h
+ */
+       [NL80211_CMD_UNSPEC] = "unspec",
        [NL80211_CMD_GET_WIPHY] = "get_wiphy",
        [NL80211_CMD_SET_WIPHY] = "set_wiphy",
        [NL80211_CMD_NEW_WIPHY] = "new_wiphy",
@@ -121,8 +164,8 @@ static const char *commands[NL80211_CMD_MAX + 1] = {
        [NL80211_CMD_DEL_KEY] = "del_key",
        [NL80211_CMD_GET_BEACON] = "get_beacon",
        [NL80211_CMD_SET_BEACON] = "set_beacon",
-       [NL80211_CMD_NEW_BEACON] = "new_beacon",
-       [NL80211_CMD_DEL_BEACON] = "del_beacon",
+       [NL80211_CMD_START_AP] = "start_ap",
+       [NL80211_CMD_STOP_AP] = "stop_ap",
        [NL80211_CMD_GET_STATION] = "get_station",
        [NL80211_CMD_SET_STATION] = "set_station",
        [NL80211_CMD_NEW_STATION] = "new_station",
@@ -133,10 +176,9 @@ static const char *commands[NL80211_CMD_MAX + 1] = {
        [NL80211_CMD_DEL_MPATH] = "del_mpath",
        [NL80211_CMD_SET_BSS] = "set_bss",
        [NL80211_CMD_SET_REG] = "set_reg",
-       [NL80211_CMD_REQ_SET_REG] = "reg_set_reg",
-       [NL80211_CMD_GET_MESH_PARAMS] = "get_mesh_params",
-       [NL80211_CMD_SET_MESH_PARAMS] = "set_mesh_params",
-       [NL80211_CMD_SET_MGMT_EXTRA_IE] = "set_mgmt_extra_ie",
+       [NL80211_CMD_REQ_SET_REG] = "req_set_reg",
+       [NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config",
+       [NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config",
        [NL80211_CMD_GET_REG] = "get_reg",
        [NL80211_CMD_GET_SCAN] = "get_scan",
        [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan",
@@ -157,14 +199,55 @@ static const char *commands[NL80211_CMD_MAX + 1] = {
        [NL80211_CMD_DISCONNECT] = "disconnect",
        [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns",
        [NL80211_CMD_GET_SURVEY] = "get_survey",
+       [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results",
        [NL80211_CMD_SET_PMKSA] = "set_pmksa",
        [NL80211_CMD_DEL_PMKSA] = "del_pmksa",
        [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa",
        [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel",
        [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel",
        [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask",
-       [NL80211_CMD_REGISTER_ACTION] = "register_action",
-       [NL80211_CMD_ACTION] = "action",
+       [NL80211_CMD_REGISTER_FRAME] = "register_frame",
+       [NL80211_CMD_FRAME] = "frame",
+       [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status",
+       [NL80211_CMD_SET_POWER_SAVE] = "set_power_save",
+       [NL80211_CMD_GET_POWER_SAVE] = "get_power_save",
+       [NL80211_CMD_SET_CQM] = "set_cqm",
+       [NL80211_CMD_NOTIFY_CQM] = "notify_cqm",
+       [NL80211_CMD_SET_CHANNEL] = "set_channel",
+       [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer",
+       [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel",
+       [NL80211_CMD_JOIN_MESH] = "join_mesh",
+       [NL80211_CMD_LEAVE_MESH] = "leave_mesh",
+       [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate",
+       [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate",
+       [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate",
+       [NL80211_CMD_GET_WOWLAN] = "get_wowlan",
+       [NL80211_CMD_SET_WOWLAN] = "set_wowlan",
+       [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan",
+       [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan",
+       [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results",
+       [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped",
+       [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload",
+       [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate",
+       [NL80211_CMD_TDLS_OPER] = "tdls_oper",
+       [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt",
+       [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame",
+       [NL80211_CMD_PROBE_CLIENT] = "probe_client",
+       [NL80211_CMD_REGISTER_BEACONS] = "register_beacons",
+       [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame",
+       [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map",
+       [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify",
+       [NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device",
+       [NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device",
+       [NL80211_CMD_CONN_FAILED] = "conn_failed",
+       [NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate",
+       [NL80211_CMD_SET_MAC_ACL] = "set_mac_acl",
+       [NL80211_CMD_RADAR_DETECT] = "radar_detect",
+       [NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features",
+       [NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies",
+       [NL80211_CMD_FT_EVENT] = "ft_event",
+       [NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start",
+       [NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop",
 };
 
 static char cmdbuf[100];
@@ -177,28 +260,50 @@ const char *command_name(enum nl80211_commands cmd)
        return cmdbuf;
 }
 
-int ieee80211_channel_to_frequency(int chan)
+int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
 {
-       if (chan < 14)
-               return 2407 + chan * 5;
-
-       if (chan == 14)
-               return 2484;
-
-       /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
-       return (chan + 1000) * 5;
+       /* see 802.11 17.3.8.3.2 and Annex J
+        * there are overlapping channel numbers in 5GHz and 2GHz bands */
+       if (chan <= 0)
+               return 0; /* not supported */
+       switch (band) {
+       case NL80211_BAND_2GHZ:
+               if (chan == 14)
+                       return 2484;
+               else if (chan < 14)
+                       return 2407 + chan * 5;
+               break;
+       case NL80211_BAND_5GHZ:
+               if (chan >= 182 && chan <= 196)
+                       return 4000 + chan * 5;
+               else
+                       return 5000 + chan * 5;
+               break;
+       case NL80211_BAND_60GHZ:
+               if (chan < 5)
+                       return 56160 + chan * 2160;
+               break;
+       default:
+               ;
+       }
+       return 0; /* not supported */
 }
 
 int ieee80211_frequency_to_channel(int freq)
 {
+       /* see 802.11-2007 17.3.8.3.2 and Annex J */
        if (freq == 2484)
                return 14;
-
-       if (freq < 2484)
+       else if (freq < 2484)
                return (freq - 2407) / 5;
-
-       /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
-       return freq/5 - 1000;
+       else if (freq >= 4910 && freq <= 4980)
+               return (freq - 4000) / 5;
+       else if (freq <= 45000) /* DMG band lower limit */
+               return (freq - 5000) / 5;
+       else if (freq >= 58320 && freq <= 64800)
+               return (freq - 56160) / 2160;
+       else
+               return 0;
 }
 
 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
@@ -206,8 +311,11 @@ void print_ssid_escaped(const uint8_t len, const uint8_t *data)
        int i;
 
        for (i = 0; i < len; i++) {
-               if (isprint(data[i]))
+               if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
                        printf("%c", data[i]);
+               else if (data[i] == ' ' &&
+                        (i != 0 && i != len -1))
+                       printf(" ");
                else
                        printf("\\x%.2x", data[i]);
        }
@@ -340,7 +448,7 @@ int parse_keys(struct nl_msg *msg, char **argv, int argc)
 
 static void print_mcs_index(const __u8 *mcs)
 {
-       unsigned int mcs_bit, prev_bit = -2, prev_cont = 0;
+       int mcs_bit, prev_bit = -2, prev_cont = 0;
 
        for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
                unsigned int mcs_octet = mcs_bit/8;
@@ -414,7 +522,7 @@ void print_ampdu_length(__u8 exponent)
        if (max_ampdu_length) {
                printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
                       max_ampdu_length, exponent);
-        } else {
+       } else {
                printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
                       "(exponent: %d)\n", exponent);
        }
@@ -422,8 +530,8 @@ void print_ampdu_length(__u8 exponent)
 
 void print_ampdu_spacing(__u8 spacing)
 {
-        printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
-               print_ampdu_space(spacing), spacing);
+       printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
+              print_ampdu_space(spacing), spacing);
 }
 
 void print_ht_capability(__u16 cap)
@@ -436,7 +544,7 @@ void print_ht_capability(__u16 cap)
 
        printf("\t\tCapabilities: 0x%02x\n", cap);
 
-       PRINT_HT_CAP((cap & BIT(0)), "RX LDCP");
+       PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
        PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
        PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
 
@@ -456,8 +564,8 @@ void print_ht_capability(__u16 cap)
 
        PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
 
-       PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 3839 bytes");
-        PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 7935 bytes");
+       PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
+       PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
 
        /*
         * For beacons and probe response this would mean the BSS
@@ -482,7 +590,7 @@ void print_ht_mcs(const __u8 *mcs)
        unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
        bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
 
-       max_rx_supp_data_rate = ((mcs[10] >> 8) & ((mcs[11] & 0x3) << 8));
+       max_rx_supp_data_rate = (mcs[10] & ((mcs[11] & 0x3) << 8));
        tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
        tx_mcs_set_equal = !(mcs[12] & (1 << 1));
        tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
@@ -516,3 +624,77 @@ void print_ht_mcs(const __u8 *mcs)
                printf("\t\tHT TX MCS rate indexes are undefined\n");
        }
 }
+
+void print_vht_info(__u32 capa, const __u8 *mcs)
+{
+       __u16 tmp;
+       int i;
+
+       printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
+
+#define PRINT_VHT_CAPA(_bit, _str) \
+       do { \
+               if (capa & BIT(_bit)) \
+                       printf("\t\t\t" _str "\n"); \
+       } while (0)
+
+       printf("\t\t\tMax MPDU length: ");
+       switch (capa & 3) {
+       case 0: printf("3895\n"); break;
+       case 1: printf("7991\n"); break;
+       case 2: printf("11454\n"); break;
+       case 3: printf("(reserved)\n");
+       }
+       printf("\t\t\tSupported Channel Width: ");
+       switch ((capa >> 2) & 3) {
+       case 0: printf("neither 160 nor 80+80\n"); break;
+       case 1: printf("160 MHz\n"); break;
+       case 2: printf("160 MHz, 80+80 MHz\n"); break;
+       case 3: printf("(reserved)\n");
+       }
+       PRINT_VHT_CAPA(4, "RX LDPC");
+       PRINT_VHT_CAPA(5, "short GI (80 MHz)");
+       PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
+       PRINT_VHT_CAPA(7, "TX STBC");
+       /* RX STBC */
+       PRINT_VHT_CAPA(11, "SU Beamformer");
+       PRINT_VHT_CAPA(12, "SU Beamformee");
+       /* compressed steering */
+       /* # of sounding dimensions */
+       PRINT_VHT_CAPA(19, "MU Beamformer");
+       PRINT_VHT_CAPA(20, "MU Beamformee");
+       PRINT_VHT_CAPA(21, "VHT TXOP PS");
+       PRINT_VHT_CAPA(22, "+HTC-VHT");
+       /* max A-MPDU */
+       /* VHT link adaptation */
+       PRINT_VHT_CAPA(29, "RX antenna pattern consistency");
+       PRINT_VHT_CAPA(30, "TX antenna pattern consistency");
+
+       printf("\t\tVHT RX MCS set:\n");
+       tmp = mcs[0] | (mcs[1] << 8);
+       for (i = 1; i <= 8; i++) {
+               printf("\t\t\t%d streams: ", i);
+               switch ((tmp >> ((i-1)*2) ) & 3) {
+               case 0: printf("MCS 0-7\n"); break;
+               case 1: printf("MCS 0-8\n"); break;
+               case 2: printf("MCS 0-9\n"); break;
+               case 3: printf("not supported\n"); break;
+               }
+       }
+       tmp = mcs[2] | (mcs[3] << 8);
+       printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
+
+       printf("\t\tVHT TX MCS set:\n");
+       tmp = mcs[4] | (mcs[5] << 8);
+       for (i = 1; i <= 8; i++) {
+               printf("\t\t\t%d streams: ", i);
+               switch ((tmp >> ((i-1)*2) ) & 3) {
+               case 0: printf("MCS 0-7\n"); break;
+               case 1: printf("MCS 0-8\n"); break;
+               case 2: printf("MCS 0-9\n"); break;
+               case 3: printf("not supported\n"); break;
+               }
+       }
+       tmp = mcs[6] | (mcs[7] << 8);
+       printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
+}