]> git.ipfire.org Git - thirdparty/iw.git/blobdiff - scan.c
iw: scan: fix buffer over-read in print_p2p
[thirdparty/iw.git] / scan.c
diff --git a/scan.c b/scan.c
index d9681f3ab203f91dda8468113cdef649664119ee..fd49038e62f4c43ce13b4449da1e7cc89563a382 100644 (file)
--- a/scan.c
+++ b/scan.c
@@ -69,35 +69,6 @@ union ieee80211_country_ie_triplet {
        } __attribute__ ((packed)) ext;
 } __attribute__ ((packed));
 
-static int parse_random_mac_addr(struct nl_msg *msg, char *arg)
-{
-       char *a_addr, *a_mask, *sep;
-       unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
-       char *addrs = arg + 9;
-
-       if (*addrs != '=')
-               return 0;
-
-       addrs++;
-       sep = strchr(addrs, '/');
-       a_addr = addrs;
-
-       if (!sep)
-               return 1;
-
-       *sep = 0;
-       a_mask = sep + 1;
-       if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
-               return 1;
-
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-       NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
-
-       return 0;
- nla_put_failure:
-       return -ENOBUFS;
-}
-
 int parse_sched_scan(struct nl_msg *msg, int *argc, char ***argv)
 {
        struct nl_msg *matchset = NULL, *freqs = NULL, *ssids = NULL;
@@ -228,11 +199,9 @@ int parse_sched_scan(struct nl_msg *msg, int *argc, char ***argv)
                        } else if (!strncmp(v[0], "randomise", 9) ||
                                   !strncmp(v[0], "randomize", 9)) {
                                flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
-                               if (c > 0) {
-                                       err = parse_random_mac_addr(msg, v[0]);
-                                       if (err)
-                                               goto nla_put_failure;
-                               }
+                               err = parse_random_mac_addr(msg, v[0] + 9);
+                               if (err)
+                                       goto nla_put_failure;
                        } else {
                                /* this element is not for us, so
                                 * return to continue parsing.
@@ -386,11 +355,12 @@ int parse_sched_scan(struct nl_msg *msg, int *argc, char ***argv)
 nla_put_failure:
        if (match)
                nla_nest_end(msg, match);
+out:
        nlmsg_free(freqs);
        nlmsg_free(matchset);
        nlmsg_free(scan_plans);
+       nlmsg_free(ssids);
 
-out:
        *argc = c;
        *argv = v;
        return err;
@@ -419,7 +389,7 @@ static int handle_scan(struct nl80211_state *state,
        bool passive = false, have_ssids = false, have_freqs = false;
        bool duration_mandatory = false;
        size_t ies_len = 0, meshid_len = 0;
-       unsigned char *ies = NULL, *meshid = NULL, *tmpies;
+       unsigned char *ies = NULL, *meshid = NULL, *tmpies = NULL;
        unsigned int flags = 0;
 
        ssids = nlmsg_alloc();
@@ -457,7 +427,7 @@ static int handle_scan(struct nl80211_state *state,
                        } else if (strncmp(argv[i], "randomise", 9) == 0 ||
                                   strncmp(argv[i], "randomize", 9) == 0) {
                                flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
-                               err = parse_random_mac_addr(msg, argv[i]);
+                               err = parse_random_mac_addr(msg, argv[i] + 9);
                                if (err)
                                        goto nla_put_failure;
                                break;
@@ -478,9 +448,8 @@ static int handle_scan(struct nl80211_state *state,
                        }
                        /* fall through - this is an error */
                case DONE:
-                       nlmsg_free(ssids);
-                       nlmsg_free(freqs);
-                       return 1;
+                       err = 1;
+                       goto nla_put_failure;
                case FREQ:
                        freq = strtoul(argv[i], &eptr, 10);
                        if (eptr != argv[i] + strlen(argv[i])) {
@@ -492,6 +461,8 @@ static int handle_scan(struct nl80211_state *state,
                        NLA_PUT_U32(freqs, i, freq);
                        break;
                case IES:
+                       if (ies)
+                               free(ies);
                        ies = parse_hex(argv[i], &ies_len);
                        if (!ies)
                                goto nla_put_failure;
@@ -520,24 +491,14 @@ static int handle_scan(struct nl80211_state *state,
 
        if (ies || meshid) {
                tmpies = (unsigned char *) malloc(ies_len + meshid_len);
-               if (!tmpies) {
-                       free(ies);
-                       free(meshid);
+               if (!tmpies)
                        goto nla_put_failure;
-               }
-               if (ies) {
+               if (ies)
                        memcpy(tmpies, ies, ies_len);
-                       free(ies);
-               }
-               if (meshid) {
+               if (meshid)
                        memcpy(&tmpies[ies_len], meshid, meshid_len);
-                       free(meshid);
-               }
-               if (nla_put(msg, NL80211_ATTR_IE, ies_len + meshid_len, tmpies) < 0) {
-                       free(tmpies);
+               if (nla_put(msg, NL80211_ATTR_IE, ies_len + meshid_len, tmpies) < 0)
                        goto nla_put_failure;
-               }
-               free(tmpies);
        }
 
        if (!have_ssids)
@@ -565,6 +526,12 @@ static int handle_scan(struct nl80211_state *state,
  nla_put_failure:
        nlmsg_free(ssids);
        nlmsg_free(freqs);
+       if (meshid)
+               free(meshid);
+       if (ies)
+               free(ies);
+       if (tmpies)
+               free(tmpies);
        return err;
 }
 
@@ -615,6 +582,61 @@ static void print_supprates(const uint8_t type, uint8_t len,
        printf("\n");
 }
 
+static void print_rm_enabled_capabilities(const uint8_t type, uint8_t len,
+                           const uint8_t *data,
+                           const struct print_ies_data *ie_buffer)
+{
+       __u64 capa = data[0] |
+                    data[1] << 8 |
+                    data[2] << 16 |
+                    data[3] << 24 |
+                    ((__u64) data[4]) << 32;
+
+       printf("\n");
+       printf("\t\tCapabilities: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
+                                                            data[0], data[1],
+                                                            data[2], data[3],
+                                                            data[4]);
+
+#define PRINT_RM_CAPA(_bit, _str) \
+       do { \
+               if (capa & BIT(_bit)) \
+                       printf("\t\t\t" _str "\n"); \
+       } while (0)
+
+       PRINT_RM_CAPA(0, "Link Measurement");
+       PRINT_RM_CAPA(1, "Neighbor Report");
+       PRINT_RM_CAPA(2, "Parallel Measurements");
+       PRINT_RM_CAPA(3, "Repeated Measurements");
+       PRINT_RM_CAPA(4, "Beacon Passive Measurement");
+       PRINT_RM_CAPA(5, "Beacon Active Measurement");
+       PRINT_RM_CAPA(6, "Beacon Table Measurement");
+       PRINT_RM_CAPA(7, "Beacon Measurement Reporting Conditions");
+       PRINT_RM_CAPA(8, "Frame Measurement");
+       PRINT_RM_CAPA(9, "Channel Load");
+       PRINT_RM_CAPA(10, "Noise Histogram Measurement");
+       PRINT_RM_CAPA(11, "Statistics Measurement");
+       PRINT_RM_CAPA(12, "LCI Measurement");
+       PRINT_RM_CAPA(13, "LCI Azimuth");
+       PRINT_RM_CAPA(14, "Transmit Stream/Category Measurement");
+       PRINT_RM_CAPA(15, "Triggered Transmit Stream/Category");
+       PRINT_RM_CAPA(16, "AP Channel Report");
+       PRINT_RM_CAPA(17, "RM MIB Capability");
+
+       PRINT_RM_CAPA(27, "Measurement Pilot Transmission Information");
+       PRINT_RM_CAPA(28, "Neighbor Report TSF Offset");
+       PRINT_RM_CAPA(29, "RCPI Measurement");
+       PRINT_RM_CAPA(30, "RSNI Measurement");
+       PRINT_RM_CAPA(31, "BSS Average Access Delay");
+       PRINT_RM_CAPA(32, "BSS Available Admission");
+       PRINT_RM_CAPA(33, "Antenna");
+       PRINT_RM_CAPA(34, "FTM Range Report");
+       PRINT_RM_CAPA(35, "Civic Location Measurement");
+
+       printf("\t\tNonoperating Channel Max Measurement Duration: %i\n", data[3] >> 5);
+       printf("\t\tMeasurement Pilot Capability: %i\n", data[4] & 7);
+}
+
 static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data,
                     const struct print_ies_data *ie_buffer)
 {
@@ -808,6 +830,36 @@ static void print_auth(const uint8_t *data)
                case 7:
                        printf("TDLS/TPK");
                        break;
+               case 8:
+                       printf("SAE");
+                       break;
+               case 9:
+                       printf("FT/SAE");
+                       break;
+               case 11:
+                       printf("IEEE 802.1X/SUITE-B");
+                       break;
+               case 12:
+                       printf("IEEE 802.1X/SUITE-B-192");
+                       break;
+               case 13:
+                       printf("FT/IEEE 802.1X/SHA-384");
+                       break;
+               case 14:
+                       printf("FILS/SHA-256");
+                       break;
+               case 15:
+                       printf("FILS/SHA-384");
+                       break;
+               case 16:
+                       printf("FT/FILS/SHA-256");
+                       break;
+               case 17:
+                       printf("FT/FILS/SHA-384");
+                       break;
+               case 18:
+                       printf("OWE");
+                       break;
                default:
                        printf("%.02x-%.02x-%.02x:%d",
                                data[0], data[1] ,data[2], data[3]);
@@ -818,6 +870,9 @@ static void print_auth(const uint8_t *data)
                case 1:
                        printf("OSEN");
                        break;
+               case 2:
+                       printf("DPP");
+                       break;
                default:
                        printf("%.02x-%.02x-%.02x:%d",
                                data[0], data[1] ,data[2], data[3]);
@@ -1169,6 +1224,36 @@ static void print_11u_rcon(const uint8_t type, uint8_t len, const uint8_t *data,
        }
 }
 
+static void print_tx_power_envelope(const uint8_t type, uint8_t len,
+                                   const uint8_t *data,
+                                   const struct print_ies_data *ie_buffer)
+{
+       const uint8_t local_max_tx_power_count = data[0] & 7;
+       const uint8_t local_max_tx_power_unit_interp = (data[0] >> 3) & 7;
+       int i;
+       static const char *power_names[] = {
+               "Local Maximum Transmit Power For 20 MHz",
+               "Local Maximum Transmit Power For 40 MHz",
+               "Local Maximum Transmit Power For 80 MHz",
+               "Local Maximum Transmit Power For 160/80+80 MHz",
+       };
+
+       printf("\n");
+
+       if (local_max_tx_power_count + 2 != len)
+               return;
+       if (local_max_tx_power_unit_interp != 0)
+               return;
+       for (i = 0; i < local_max_tx_power_count + 1; ++i) {
+               int8_t power_val = ((int8_t)data[1 + i]) >> 1;
+               int8_t point5 = data[1 + i] & 1;
+               if (point5)
+                       printf("\t\t * %s: %i.5 dBm\n", power_names[i], power_val);
+               else
+                       printf("\t\t * %s: %i dBm\n", power_names[i], power_val);
+       }
+}
+
 static const char *ht_secondary_offset[4] = {
        "no secondary",
        "above",
@@ -1382,7 +1467,7 @@ static void print_tim(const uint8_t type, uint8_t len, const uint8_t *data,
 static void print_ibssatim(const uint8_t type, uint8_t len, const uint8_t *data,
                           const struct print_ies_data *ie_buffer)
 {
-       printf(" %d TUs", (data[1] << 8) + data[0]);
+       printf(" %d TUs\n", (data[1] << 8) + data[0]);
 }
 
 static void print_vht_capa(const uint8_t type, uint8_t len, const uint8_t *data,
@@ -1412,6 +1497,89 @@ static void print_vht_oper(const uint8_t type, uint8_t len, const uint8_t *data,
        printf("\t\t * VHT basic MCS set: 0x%.2x%.2x\n", data[4], data[3]);
 }
 
+static void print_supp_op_classes(const uint8_t type, uint8_t len,
+                                 const uint8_t *data,
+                                 const struct print_ies_data *ie_buffer)
+{
+       uint8_t *p = (uint8_t*) data;
+       const uint8_t *next_data = p + len;
+       int zero_delimiter = 0;
+       int one_hundred_thirty_delimiter = 0;
+
+       printf("\n");
+       printf("\t\t * current operating class: %d\n", *p);
+       while (++p < next_data) {
+               if (*p == 130) {
+                       one_hundred_thirty_delimiter = 1;
+                       break;
+               }
+               if (*p == 0) {
+                       zero_delimiter = 0;
+                       break;
+               }
+               printf("\t\t * operating class: %d\n", *p);
+       }
+       if (one_hundred_thirty_delimiter)
+               while (++p < next_data) {
+                       printf("\t\t * current operating class extension: %d\n", *p);
+               }
+       if (zero_delimiter)
+               while (++p < next_data - 1) {
+                       printf("\t\t * operating class tuple: %d %d\n", p[0], p[1]);
+                       if (*p == 0)
+                               break;
+               }
+}
+
+static void print_measurement_pilot_tx(const uint8_t type, uint8_t len,
+                                      const uint8_t *data,
+                                      const struct print_ies_data *ie_buffer)
+{
+       uint8_t *p, len_remaining;
+
+       printf("\n");
+       printf("\t\t * interval: %d TUs\n", data[0]);
+
+       if(len <= 1)
+               return;
+
+       p = (uint8_t *) data + 1;
+       len_remaining = len - 1;
+
+       while (len_remaining >=5) {
+               uint8_t subelement_id = *p, len, *end;
+
+               p++;
+               len = *p;
+               p++;
+               end = p + len;
+
+               len_remaining -= 2;
+
+               /* 802.11-2016 only allows vendor specific elements */
+               if (subelement_id != 221) {
+                       printf("\t\t * <Invalid subelement ID %d>\n", subelement_id);
+                       return;
+               }
+
+               if (len < 3 || len > len_remaining) {
+                       printf(" <Parse error, element too short>\n");
+                       return;
+               }
+
+               printf("\t\t * vendor specific: OUI %.2x:%.2x:%.2x, data:",
+                       p[0], p[1], p[2]);
+               /* add only two here and use ++p in while loop */
+               p += 2;
+
+               while (++p < end)
+                       printf(" %.2x", *p);
+               printf("\n");
+
+               len_remaining -= len;
+       }
+}
+
 static void print_obss_scan_params(const uint8_t type, uint8_t len,
                                   const uint8_t *data,
                                   const struct print_ies_data *ie_buffer)
@@ -1533,6 +1701,8 @@ static const struct ie_print ieprinters[] = {
        [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), },
        [45] = { "HT capabilities", print_ht_capa, 26, 26, BIT(PRINT_SCAN), },
        [47] = { "ERP D4.0", print_erp, 1, 255, BIT(PRINT_SCAN), },
+       [59] = { "Supported operating classes", print_supp_op_classes, 1, 255, BIT(PRINT_SCAN), },
+       [66] = { "Measurement Pilot Transmission", print_measurement_pilot_tx, 1, 255, BIT(PRINT_SCAN), },
        [74] = { "Overlapping BSS scan params", print_obss_scan_params, 14, 255, BIT(PRINT_SCAN), },
        [61] = { "HT operation", print_ht_op, 22, 22, BIT(PRINT_SCAN), },
        [62] = { "Secondary Channel Offset", print_secchan_offs, 1, 1, BIT(PRINT_SCAN), },
@@ -1540,12 +1710,14 @@ static const struct ie_print ieprinters[] = {
        [192] = { "VHT operation", print_vht_oper, 5, 255, BIT(PRINT_SCAN), },
        [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), },
        [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
+       [70] = { "RM enabled capabilities", print_rm_enabled_capabilities, 5, 5, BIT(PRINT_SCAN), },
        [113] = { "MESH Configuration", print_mesh_conf, 7, 7, BIT(PRINT_SCAN), },
        [114] = { "MESH ID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), },
        [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), },
        [107] = { "802.11u Interworking", print_interworking, 0, 255, BIT(PRINT_SCAN), },
        [108] = { "802.11u Advertisement", print_11u_advert, 0, 255, BIT(PRINT_SCAN), },
-       [111] = { "802.11u Roaming Consortium", print_11u_rcon, 0, 255, BIT(PRINT_SCAN), },
+       [111] = { "802.11u Roaming Consortium", print_11u_rcon, 2, 255, BIT(PRINT_SCAN), },
+       [195] = { "Transmit Power Envelope", print_tx_power_envelope, 2, 5, BIT(PRINT_SCAN), },
 };
 
 static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data,
@@ -1655,7 +1827,7 @@ static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data,
        while (len >= 4) {
                subtype = (data[0] << 8) + data[1];
                sublen = (data[2] << 8) + data[3];
-               if (sublen > len)
+               if (sublen > len - 4)
                        break;
 
                switch (subtype) {
@@ -1853,8 +2025,7 @@ static inline void print_p2p(const uint8_t type, uint8_t len,
                                printf("\t * malformed device info\n");
                                break;
                        }
-
-                       /* fall through for now */
+                       /* fall through */
                case 0x00: /* status */
                case 0x01: /* minor reason */
                case 0x03: /* device ID */
@@ -1874,7 +2045,7 @@ static inline void print_p2p(const uint8_t type, uint8_t len,
                case 0x12: /* invitation flags */
                case 0xdd: /* vendor specific */
                default: {
-                       const __u8 *subdata = data + 4;
+                       const __u8 *subdata = data + 3;
                        __u16 tmplen = sublen;
 
                        tab_on_first(&first);
@@ -1918,10 +2089,39 @@ static inline void print_hs20_ind(const uint8_t type, uint8_t len,
                printf("\t\tUnexpected length: %i\n", len);
 }
 
+static void print_wifi_owe_tarns(const uint8_t type, uint8_t len,
+                                const uint8_t *data,
+                                const struct print_ies_data *ie_buffer)
+{
+       char mac_addr[20];
+       int ssid_len;
+
+       printf("\n");
+       if (len < 7)
+               return;
+
+       mac_addr_n2a(mac_addr, data);
+       printf("\t\tBSSID: %s\n", mac_addr);
+
+       ssid_len = data[6];
+       if (ssid_len > len - 7)
+               return;
+       printf("\t\tSSID: ");
+       print_ssid_escaped(ssid_len, data + 7);
+       printf("\n");
+
+       /* optional elements */
+       if (len >= ssid_len + 9) {
+               printf("\t\tBand Info: %u\n", data[ssid_len + 7]);
+               printf("\t\tChannel Info: %u\n", data[ssid_len + 8]);
+       }
+}
+
 static const struct ie_print wfa_printers[] = {
        [9] = { "P2P", print_p2p, 2, 255, BIT(PRINT_SCAN), },
        [16] = { "HotSpot 2.0 Indication", print_hs20_ind, 1, 255, BIT(PRINT_SCAN), },
        [18] = { "HotSpot 2.0 OSEN", print_wifi_osen, 1, 255, BIT(PRINT_SCAN), },
+       [28] = { "OWE Transition Mode", print_wifi_owe_tarns, 7, 255, BIT(PRINT_SCAN), },
 };
 
 static void print_vendor(unsigned char len, unsigned char *data,
@@ -1942,7 +2142,8 @@ static void print_vendor(unsigned char len, unsigned char *data,
                    wifiprinters[data[3]].name &&
                    wifiprinters[data[3]].flags & BIT(ptype)) {
                        print_ie(&wifiprinters[data[3]],
-                                data[3], len - 4, data + 4, 0);
+                                data[3], len - 4, data + 4,
+                                NULL);
                        return;
                }
                if (!unknown)
@@ -1959,7 +2160,8 @@ static void print_vendor(unsigned char len, unsigned char *data,
                    wfa_printers[data[3]].name &&
                    wfa_printers[data[3]].flags & BIT(ptype)) {
                        print_ie(&wfa_printers[data[3]],
-                                data[3], len - 4, data + 4, 0);
+                                data[3], len - 4, data + 4,
+                                NULL);
                        return;
                }
                if (!unknown)
@@ -1988,7 +2190,10 @@ void print_ies(unsigned char *ie, int ielen, bool unknown,
                .ie = ie,
                .ielen = ielen };
 
-       while (ielen >= 2 && ielen >= ie[1]) {
+       if (ie == NULL || ielen < 0)
+               return;
+
+       while (ielen >= 2 && ielen - 2 >= ie[1]) {
                if (ie[0] < ARRAY_SIZE(ieprinters) &&
                    ieprinters[ie[0]].name &&
                    ieprinters[ie[0]].flags & BIT(ptype)) {
@@ -2189,7 +2394,8 @@ static int print_bss_handler(struct nl_msg *msg, void *arg)
 
                if (bss[NL80211_BSS_PRESP_DATA] ||
                    (bcnies && (nla_len(ies) != nla_len(bcnies) ||
-                   memcmp(nla_data(ies), nla_data(bcnies), nla_len(ies)))))
+                               memcmp(nla_data(ies), nla_data(bcnies),
+                                      nla_len(ies)))))
                        printf("\tInformation elements from Probe Response "
                               "frame:\n");
                print_ies(nla_data(ies), nla_len(ies),