]> git.ipfire.org Git - thirdparty/iw.git/blame - util.c
iw: scan: add extension tag parsing
[thirdparty/iw.git] / util.c
CommitLineData
748f8489 1#include <ctype.h>
51e9bd80
JB
2#include <netlink/attr.h>
3#include <errno.h>
4#include <stdbool.h>
3d1e8704 5#include "iw.h"
f5f7b1d0 6#include "nl80211.h"
3d1e8704 7
7f87d3cf 8void mac_addr_n2a(char *mac_addr, const unsigned char *arg)
3d1e8704 9{
53e5ce7a 10 int i, l;
3d1e8704
LCC
11
12 l = 0;
13 for (i = 0; i < ETH_ALEN ; i++) {
14 if (i == 0) {
53e5ce7a 15 sprintf(mac_addr+l, "%02x", arg[i]);
3d1e8704
LCC
16 l += 2;
17 } else {
53e5ce7a 18 sprintf(mac_addr+l, ":%02x", arg[i]);
3d1e8704
LCC
19 l += 3;
20 }
21 }
3d1e8704
LCC
22}
23
24int mac_addr_a2n(unsigned char *mac_addr, char *arg)
25{
26 int i;
27
28 for (i = 0; i < ETH_ALEN ; i++) {
29 int temp;
30 char *cp = strchr(arg, ':');
31 if (cp) {
32 *cp = 0;
33 cp++;
34 }
35 if (sscanf(arg, "%x", &temp) != 1)
36 return -1;
37 if (temp < 0 || temp > 255)
38 return -1;
39
40 mac_addr[i] = temp;
41 if (!cp)
42 break;
43 arg = cp;
44 }
45 if (i < ETH_ALEN - 1)
46 return -1;
47
48 return 0;
49}
541ef425 50
3ff24563
JB
51int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len,
52 unsigned char **mask)
236d4191 53{
3ff24563
JB
54 size_t len = strlen(hexmask) / 2;
55 unsigned char *result_val;
56 unsigned char *result_mask = NULL;
57
236d4191
JB
58 int pos = 0;
59
3ff24563 60 *result_len = 0;
236d4191 61
3ff24563
JB
62 result_val = calloc(len + 2, 1);
63 if (!result_val)
64 goto error;
65 *result = result_val;
66 if (mask) {
67 result_mask = calloc(DIV_ROUND_UP(len, 8) + 2, 1);
68 if (!result_mask)
69 goto error;
70 *mask = result_mask;
71 }
236d4191
JB
72
73 while (1) {
3ff24563 74 char *cp = strchr(hexmask, ':');
236d4191
JB
75 if (cp) {
76 *cp = 0;
77 cp++;
78 }
236d4191 79
3ff24563
JB
80 if (result_mask && (strcmp(hexmask, "-") == 0 ||
81 strcmp(hexmask, "xx") == 0 ||
82 strcmp(hexmask, "--") == 0)) {
83 /* skip this byte and leave mask bit unset */
84 } else {
85 int temp, mask_pos;
86 char *end;
87
88 temp = strtoul(hexmask, &end, 16);
89 if (*end)
90 goto error;
91 if (temp < 0 || temp > 255)
92 goto error;
93 result_val[pos] = temp;
94
95 mask_pos = pos / 8;
96 if (result_mask)
97 result_mask[mask_pos] |= 1 << (pos % 8);
98 }
99
100 (*result_len)++;
101 pos++;
236d4191 102
236d4191
JB
103 if (!cp)
104 break;
3ff24563 105 hexmask = cp;
236d4191
JB
106 }
107
3ff24563 108 return 0;
236d4191 109 error:
3ff24563
JB
110 free(result_val);
111 free(result_mask);
112 return -1;
113}
114
115unsigned char *parse_hex(char *hex, size_t *outlen)
116{
117 unsigned char *result;
118
119 if (parse_hex_mask(hex, &result, outlen, NULL))
120 return NULL;
121 return result;
236d4191
JB
122}
123
541ef425
JB
124static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
125 "unspecified",
126 "IBSS",
34e78ed0 127 "managed",
541ef425 128 "AP",
34e78ed0 129 "AP/VLAN",
541ef425 130 "WDS",
34e78ed0 131 "monitor",
a4464243
JB
132 "mesh point",
133 "P2P-client",
134 "P2P-GO",
add40bbd 135 "P2P-device",
3955e524 136 "outside context of a BSS",
ed9b77ec 137 "NAN",
541ef425
JB
138};
139
140static char modebuf[100];
141
142const char *iftype_name(enum nl80211_iftype iftype)
143{
a66b3a35 144 if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
541ef425
JB
145 return ifmodes[iftype];
146 sprintf(modebuf, "Unknown mode (%d)", iftype);
147 return modebuf;
148}
379f8397 149
9990c1e9 150static const char *commands[NL80211_CMD_MAX + 1] = {
dc1f3fe8 151#include "nl80211-commands.inc"
9990c1e9
MH
152};
153
154static char cmdbuf[100];
155
156const char *command_name(enum nl80211_commands cmd)
157{
73780397 158 if (cmd <= NL80211_CMD_MAX && commands[cmd])
9990c1e9
MH
159 return commands[cmd];
160 sprintf(cmdbuf, "Unknown command (%d)", cmd);
161 return cmdbuf;
162}
163
58b46da2 164int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
379f8397 165{
58b46da2
BR
166 /* see 802.11 17.3.8.3.2 and Annex J
167 * there are overlapping channel numbers in 5GHz and 2GHz bands */
168 if (chan <= 0)
169 return 0; /* not supported */
170 switch (band) {
171 case NL80211_BAND_2GHZ:
172 if (chan == 14)
173 return 2484;
174 else if (chan < 14)
175 return 2407 + chan * 5;
176 break;
177 case NL80211_BAND_5GHZ:
178 if (chan >= 182 && chan <= 196)
179 return 4000 + chan * 5;
180 else
181 return 5000 + chan * 5;
182 break;
43789196 183 case NL80211_BAND_6GHZ:
b12fc8a8
PKC
184 /* see 802.11ax D6.1 27.3.23.2 */
185 if (chan == 2)
186 return 5935;
43789196 187 if (chan <= 253)
b12fc8a8 188 return 5950 + chan * 5;
43789196 189 break;
58b46da2 190 case NL80211_BAND_60GHZ:
b12fc8a8 191 if (chan < 7)
58b46da2
BR
192 return 56160 + chan * 2160;
193 break;
194 default:
195 ;
196 }
197 return 0; /* not supported */
379f8397
JB
198}
199
200int ieee80211_frequency_to_channel(int freq)
201{
58b46da2 202 /* see 802.11-2007 17.3.8.3.2 and Annex J */
379f8397
JB
203 if (freq == 2484)
204 return 14;
b12fc8a8
PKC
205 /* see 802.11ax D6.1 27.3.23.2 and Annex E */
206 else if (freq == 5935)
207 return 2;
58b46da2 208 else if (freq < 2484)
379f8397 209 return (freq - 2407) / 5;
58b46da2
BR
210 else if (freq >= 4910 && freq <= 4980)
211 return (freq - 4000) / 5;
b12fc8a8 212 else if (freq < 5950)
58b46da2 213 return (freq - 5000) / 5;
43789196 214 else if (freq <= 45000) /* DMG band lower limit */
b12fc8a8
PKC
215 /* see 802.11ax D6.1 27.3.23.2 */
216 return (freq - 5950) / 5;
43789196 217 else if (freq >= 58320 && freq <= 70200)
d56e86bc 218 return (freq - 56160) / 2160;
58b46da2
BR
219 else
220 return 0;
379f8397 221}
748f8489
JB
222
223void print_ssid_escaped(const uint8_t len, const uint8_t *data)
224{
225 int i;
226
227 for (i = 0; i < len; i++) {
3f612733 228 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
748f8489 229 printf("%c", data[i]);
3f612733
JB
230 else if (data[i] == ' ' &&
231 (i != 0 && i != len -1))
232 printf(" ");
748f8489
JB
233 else
234 printf("\\x%.2x", data[i]);
235 }
236}
51e9bd80
JB
237
238static int hex2num(char digit)
239{
240 if (!isxdigit(digit))
241 return -1;
242 if (isdigit(digit))
243 return digit - '0';
244 return tolower(digit) - 'a' + 10;
245}
246
9ad3cc24 247static int hex2byte(const char *hex)
51e9bd80
JB
248{
249 int d1, d2;
250
251 d1 = hex2num(hex[0]);
252 if (d1 < 0)
253 return -1;
254 d2 = hex2num(hex[1]);
255 if (d2 < 0)
256 return -1;
257 return (d1 << 4) | d2;
258}
259
60b6c638 260char *hex2bin(const char *hex, char *buf)
51e9bd80
JB
261{
262 char *result = buf;
263 int d;
264
265 while (hex[0]) {
266 d = hex2byte(hex);
267 if (d < 0)
268 return NULL;
269 buf[0] = d;
270 buf++;
271 hex += 2;
272 }
273
274 return result;
275}
276
6c2a0121
EG
277static int parse_akm_suite(const char *cipher_str)
278{
279
280 if (!strcmp(cipher_str, "PSK"))
281 return 0x000FAC02;
282 if (!strcmp(cipher_str, "FT/PSK"))
283 return 0x000FAC03;
284 if (!strcmp(cipher_str, "PSK/SHA-256"))
285 return 0x000FAC06;
286 return -EINVAL;
287}
288
289static int parse_cipher_suite(const char *cipher_str)
290{
291
292 if (!strcmp(cipher_str, "TKIP"))
293 return 0x000FAC02;
d7924705 294 if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128"))
6c2a0121 295 return 0x000FAC04;
d7924705 296 if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128"))
6c2a0121
EG
297 return 0x000FAC08;
298 if (!strcmp(cipher_str, "GCMP-256"))
299 return 0x000FAC09;
300 if (!strcmp(cipher_str, "CCMP-256"))
301 return 0x000FAC0A;
302 return -EINVAL;
303}
304
0e39f109 305int parse_keys(struct nl_msg *msg, char **argv[], int *argc)
51e9bd80
JB
306{
307 struct nlattr *keys;
308 int i = 0;
041581ce 309 bool have_default = false;
0e39f109 310 char *arg = **argv;
51e9bd80 311 char keybuf[13];
6c2a0121 312 int pos = 0;
51e9bd80 313
0e39f109 314 if (!*argc)
51e9bd80
JB
315 return 1;
316
6c2a0121
EG
317 if (!memcmp(&arg[pos], "psk", 3)) {
318 char psk_keybuf[32];
319 int cipher_suite, akm_suite;
320
0e39f109 321 if (*argc < 4)
6c2a0121
EG
322 goto explain;
323
324 pos+=3;
325 if (arg[pos] != ':')
326 goto explain;
327 pos++;
328
329 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, NL80211_WPA_VERSION_2);
330
331 if (strlen(&arg[pos]) != (sizeof(psk_keybuf) * 2) || !hex2bin(&arg[pos], psk_keybuf)) {
332 printf("Bad PSK\n");
333 return -EINVAL;
334 }
335
336 NLA_PUT(msg, NL80211_ATTR_PMK, 32, psk_keybuf);
337 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM);
338
0e39f109
EG
339 *argv += 1;
340 *argc -= 1;
341 arg = **argv;
6c2a0121
EG
342
343 akm_suite = parse_akm_suite(arg);
344 if (akm_suite < 0)
345 goto explain;
346
347 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, akm_suite);
348
0e39f109
EG
349 *argv += 1;
350 *argc -= 1;
351 arg = **argv;
6c2a0121
EG
352
353 cipher_suite = parse_cipher_suite(arg);
354 if (cipher_suite < 0)
355 goto explain;
356
357 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher_suite);
358
0e39f109
EG
359 *argv += 1;
360 *argc -= 1;
361 arg = **argv;
6c2a0121
EG
362
363 cipher_suite = parse_cipher_suite(arg);
364 if (cipher_suite < 0)
365 goto explain;
366
367 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher_suite);
368
d4f1ea11
IP
369 *argv += 1;
370 *argc -= 1;
6c2a0121
EG
371 return 0;
372 }
373
51e9bd80
JB
374 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
375
376 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
377 if (!keys)
378 return -ENOBUFS;
379
380 do {
6c2a0121 381 int keylen;
51e9bd80
JB
382 struct nlattr *key = nla_nest_start(msg, ++i);
383 char *keydata;
384
0e39f109 385 arg = **argv;
6c2a0121
EG
386 pos = 0;
387
51e9bd80
JB
388 if (!key)
389 return -ENOBUFS;
390
391 if (arg[pos] == 'd') {
392 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
393 pos++;
394 if (arg[pos] == ':')
395 pos++;
041581ce 396 have_default = true;
51e9bd80
JB
397 }
398
399 if (!isdigit(arg[pos]))
400 goto explain;
401 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
402 if (arg[pos++] != ':')
403 goto explain;
404 keydata = arg + pos;
405 switch (strlen(keydata)) {
406 case 10:
407 keydata = hex2bin(keydata, keybuf);
6ab936f0 408 /* fall through */
51e9bd80
JB
409 case 5:
410 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
411 keylen = 5;
412 break;
413 case 26:
414 keydata = hex2bin(keydata, keybuf);
6ab936f0 415 /* fall through */
51e9bd80
JB
416 case 13:
417 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
418 keylen = 13;
419 break;
420 default:
421 goto explain;
422 }
423
424 if (!keydata)
425 goto explain;
426
427 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
428
0e39f109
EG
429 *argv += 1;
430 *argc -= 1;
041581ce
JB
431
432 /* one key should be TX key */
0e39f109 433 if (!have_default && !*argc)
041581ce
JB
434 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
435
436 nla_nest_end(msg, key);
0e39f109 437 } while (*argc);
51e9bd80
JB
438
439 nla_nest_end(msg, keys);
440
441 return 0;
442 nla_put_failure:
443 return -ENOBUFS;
444 explain:
445 fprintf(stderr, "key must be [d:]index:data where\n"
446 " 'd:' means default (transmit) key\n"
447 " 'index:' is a single digit (0-3)\n"
448 " 'data' must be 5 or 13 ascii chars\n"
449 " or 10 or 26 hex digits\n"
6c2a0121
EG
450 "for example: d:2:6162636465 is the same as d:2:abcde\n"
451 "or psk:data <AKM Suite> <pairwise CIPHER> <groupwise CIPHER> where\n"
452 " 'data' is the PSK (output of wpa_passphrase and the CIPHER can be CCMP or GCMP\n"
453 "for example: psk:0123456789abcdef PSK CCMP CCMP\n"
454 "The allowed AKM suites are PSK, FT/PSK, PSK/SHA-256\n"
455 "The allowed Cipher suites are TKIP, CCMP, GCMP, GCMP-256, CCMP-256\n");
51e9bd80
JB
456 return 2;
457}
deb3501c 458
c37f6c64 459enum nl80211_chan_width str_to_bw(const char *str)
997c60fd
BB
460{
461 static const struct {
462 const char *name;
463 unsigned int val;
464 } bwmap[] = {
465 { .name = "5", .val = NL80211_CHAN_WIDTH_5, },
466 { .name = "10", .val = NL80211_CHAN_WIDTH_10, },
467 { .name = "20", .val = NL80211_CHAN_WIDTH_20, },
468 { .name = "40", .val = NL80211_CHAN_WIDTH_40, },
469 { .name = "80", .val = NL80211_CHAN_WIDTH_80, },
470 { .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, },
471 { .name = "160", .val = NL80211_CHAN_WIDTH_160, },
472 };
c37f6c64
JB
473 unsigned int i;
474
475 for (i = 0; i < ARRAY_SIZE(bwmap); i++) {
476 if (strcasecmp(bwmap[i].name, str) == 0)
477 return bwmap[i].val;
478 }
479
480 return NL80211_CHAN_WIDTH_20_NOHT;
481}
482
483static int parse_freqs(struct chandef *chandef, int argc, char **argv,
484 int *parsed)
485{
997c60fd 486 uint32_t freq;
997c60fd 487 char *end;
4871fcf5 488 bool need_cf1 = false, need_cf2 = false;
997c60fd
BB
489
490 if (argc < 1)
491 return 0;
492
c37f6c64 493 chandef->width = str_to_bw(argv[0]);
997c60fd 494
4871fcf5
JB
495 switch (chandef->width) {
496 case NL80211_CHAN_WIDTH_20_NOHT:
497 /* First argument was not understood, give up gracefully. */
997c60fd 498 return 0;
4871fcf5
JB
499 case NL80211_CHAN_WIDTH_20:
500 case NL80211_CHAN_WIDTH_5:
501 case NL80211_CHAN_WIDTH_10:
502 break;
503 case NL80211_CHAN_WIDTH_80P80:
504 need_cf2 = true;
505 /* fall through */
506 case NL80211_CHAN_WIDTH_40:
507 case NL80211_CHAN_WIDTH_80:
508 case NL80211_CHAN_WIDTH_160:
509 need_cf1 = true;
510 break;
f718f11d
JB
511 case NL80211_CHAN_WIDTH_1:
512 case NL80211_CHAN_WIDTH_2:
513 case NL80211_CHAN_WIDTH_4:
514 case NL80211_CHAN_WIDTH_8:
515 case NL80211_CHAN_WIDTH_16:
516 /* can't happen yet */
517 break;
4871fcf5 518 }
997c60fd 519
c37f6c64
JB
520 *parsed += 1;
521
4871fcf5 522 if (!need_cf1)
997c60fd
BB
523 return 0;
524
4871fcf5
JB
525 if (argc < 2)
526 return 1;
527
997c60fd
BB
528 /* center freq 1 */
529 if (!*argv[1])
4871fcf5 530 return 1;
997c60fd
BB
531 freq = strtoul(argv[1], &end, 10);
532 if (*end)
4871fcf5 533 return 1;
997c60fd
BB
534 *parsed += 1;
535
536 chandef->center_freq1 = freq;
537
4871fcf5 538 if (!need_cf2)
997c60fd
BB
539 return 0;
540
4871fcf5
JB
541 if (argc < 3)
542 return 1;
543
997c60fd
BB
544 /* center freq 2 */
545 if (!*argv[2])
4871fcf5 546 return 1;
997c60fd
BB
547 freq = strtoul(argv[2], &end, 10);
548 if (*end)
4871fcf5 549 return 1;
997c60fd
BB
550 chandef->center_freq2 = freq;
551
552 *parsed += 1;
553
554 return 0;
555}
556
557
558/**
559 * parse_freqchan - Parse frequency or channel definition
560 *
561 * @chandef: chandef structure to be filled in
562 * @chan: Boolean whether to parse a channel or frequency based specifier
563 * @argc: Number of arguments
564 * @argv: Array of string arguments
565 * @parsed: Pointer to return the number of used arguments, or NULL to error
566 * out if any argument is left unused.
567 *
568 * The given chandef structure will be filled in from the command line
569 * arguments. argc/argv will be updated so that further arguments from the
570 * command line can be parsed.
571 *
4871fcf5
JB
572 * Note that despite the fact that the function knows how many center freqs
573 * are needed, there's an ambiguity if the next argument after this is an
574 * integer argument, since the valid channel width values are interpreted
575 * as such, rather than a following argument. This can be avoided by the
576 * user by giving "NOHT" instead.
997c60fd
BB
577 *
578 * The working specifier if chan is set are:
b6f2dac4 579 * <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
997c60fd
BB
580 *
581 * And if frequency is set:
b6f2dac4 582 * <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
997c60fd
BB
583 * <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
584 *
585 * If the mode/channel width is not given the NOHT is assumed.
586 *
587 * Return: Number of used arguments, zero or negative error number otherwise
588 */
589int parse_freqchan(struct chandef *chandef, bool chan, int argc, char **argv,
590 int *parsed)
591{
592 char *end;
593 static const struct chanmode chanmode[] = {
594 { .name = "HT20",
595 .width = NL80211_CHAN_WIDTH_20,
596 .freq1_diff = 0,
597 .chantype = NL80211_CHAN_HT20 },
598 { .name = "HT40+",
599 .width = NL80211_CHAN_WIDTH_40,
600 .freq1_diff = 10,
601 .chantype = NL80211_CHAN_HT40PLUS },
602 { .name = "HT40-",
603 .width = NL80211_CHAN_WIDTH_40,
604 .freq1_diff = -10,
605 .chantype = NL80211_CHAN_HT40MINUS },
606 { .name = "NOHT",
607 .width = NL80211_CHAN_WIDTH_20_NOHT,
608 .freq1_diff = 0,
609 .chantype = NL80211_CHAN_NO_HT },
610 { .name = "5MHz",
611 .width = NL80211_CHAN_WIDTH_5,
612 .freq1_diff = 0,
613 .chantype = -1 },
614 { .name = "10MHz",
615 .width = NL80211_CHAN_WIDTH_10,
616 .freq1_diff = 0,
617 .chantype = -1 },
618 { .name = "80MHz",
619 .width = NL80211_CHAN_WIDTH_80,
620 .freq1_diff = 0,
621 .chantype = -1 },
b6f2dac4
T
622 { .name = "160MHz",
623 .width = NL80211_CHAN_WIDTH_160,
624 .freq1_diff = 0,
625 .chantype = -1 },
997c60fd
BB
626 };
627 const struct chanmode *chanmode_selected = NULL;
628 unsigned int freq;
629 unsigned int i;
630 int _parsed = 0;
631 int res = 0;
632
633 if (argc < 1)
634 return 1;
635
636 if (!argv[0])
637 goto out;
638 freq = strtoul(argv[0], &end, 10);
639 if (*end) {
640 res = 1;
641 goto out;
642 }
643
644 _parsed += 1;
645
646 memset(chandef, 0, sizeof(struct chandef));
647
648 if (chan) {
649 enum nl80211_band band;
650
651 band = freq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
652 freq = ieee80211_channel_to_frequency(freq, band);
653 }
654 chandef->control_freq = freq;
655 /* Assume 20MHz NOHT channel for now. */
656 chandef->center_freq1 = freq;
657
658 /* Try to parse HT mode definitions */
659 if (argc > 1) {
660 for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
661 if (strcasecmp(chanmode[i].name, argv[1]) == 0) {
662 chanmode_selected = &chanmode[i];
663 _parsed += 1;
664 break;
665 }
666 }
667 }
668
669 /* channel mode given, use it and return. */
670 if (chanmode_selected) {
671 chandef->center_freq1 = get_cf1(chanmode_selected, freq);
672 chandef->width = chanmode_selected->width;
673 goto out;
674 }
675
676 /* This was a only a channel definition, nothing further may follow. */
677 if (chan)
678 goto out;
679
680 res = parse_freqs(chandef, argc - 1, argv + 1, &_parsed);
681
682 out:
683 /* Error out if parsed is NULL. */
684 if (!parsed && _parsed != argc)
685 return 1;
686
687 if (parsed)
688 *parsed = _parsed;
689
690 return res;
691}
692
693int put_chandef(struct nl_msg *msg, struct chandef *chandef)
694{
695 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chandef->control_freq);
696 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width);
697
698 switch (chandef->width) {
699 case NL80211_CHAN_WIDTH_20_NOHT:
700 NLA_PUT_U32(msg,
701 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
702 NL80211_CHAN_NO_HT);
703 break;
704 case NL80211_CHAN_WIDTH_20:
705 NLA_PUT_U32(msg,
706 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
707 NL80211_CHAN_HT20);
708 break;
709 case NL80211_CHAN_WIDTH_40:
710 if (chandef->control_freq > chandef->center_freq1)
711 NLA_PUT_U32(msg,
712 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
713 NL80211_CHAN_HT40MINUS);
714 else
715 NLA_PUT_U32(msg,
716 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
717 NL80211_CHAN_HT40PLUS);
718 break;
719 default:
720 break;
721 }
722
723 if (chandef->center_freq1)
724 NLA_PUT_U32(msg,
725 NL80211_ATTR_CENTER_FREQ1,
726 chandef->center_freq1);
727
728 if (chandef->center_freq2)
729 NLA_PUT_U32(msg,
730 NL80211_ATTR_CENTER_FREQ2,
731 chandef->center_freq2);
732
733 return 0;
734
735 nla_put_failure:
736 return -ENOBUFS;
737}
738
7ddfb679 739static void print_mcs_index(const __u8 *mcs)
deb3501c 740{
9fea9777 741 int mcs_bit, prev_bit = -2, prev_cont = 0;
04953e90
JB
742
743 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
744 unsigned int mcs_octet = mcs_bit/8;
745 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
746 bool mcs_rate_idx_set;
747
748 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
749
750 if (!mcs_rate_idx_set)
751 continue;
752
753 if (prev_bit != mcs_bit - 1) {
754 if (prev_bit != -2)
755 printf("%d, ", prev_bit);
756 else
757 printf(" ");
758 printf("%d", mcs_bit);
759 prev_cont = 0;
760 } else if (!prev_cont) {
761 printf("-");
762 prev_cont = 1;
763 }
764
765 prev_bit = mcs_bit;
766 }
deb3501c 767
04953e90
JB
768 if (prev_cont)
769 printf("%d", prev_bit);
770 printf("\n");
deb3501c 771}
0950993f
LR
772
773/*
774 * There are only 4 possible values, we just use a case instead of computing it,
775 * but technically this can also be computed through the formula:
776 *
777 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
778 */
779static __u32 compute_ampdu_length(__u8 exponent)
780{
781 switch (exponent) {
782 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
783 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
784 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
785 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
786 default: return 0;
787 }
788}
789
790static const char *print_ampdu_space(__u8 space)
791{
792 switch (space) {
793 case 0: return "No restriction";
794 case 1: return "1/4 usec";
795 case 2: return "1/2 usec";
796 case 3: return "1 usec";
797 case 4: return "2 usec";
798 case 5: return "4 usec";
799 case 6: return "8 usec";
800 case 7: return "16 usec";
801 default:
7ae93cd5 802 return "BUG (spacing more than 3 bits!)";
0950993f
LR
803 }
804}
805
806void print_ampdu_length(__u8 exponent)
807{
04953e90 808 __u32 max_ampdu_length;
0950993f
LR
809
810 max_ampdu_length = compute_ampdu_length(exponent);
811
812 if (max_ampdu_length) {
813 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
814 max_ampdu_length, exponent);
3f362f8b 815 } else {
0950993f
LR
816 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
817 "(exponent: %d)\n", exponent);
818 }
819}
820
821void print_ampdu_spacing(__u8 spacing)
822{
3f362f8b
NB
823 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
824 print_ampdu_space(spacing), spacing);
0950993f 825}
357c1a5d
LR
826
827void print_ht_capability(__u16 cap)
828{
829#define PRINT_HT_CAP(_cond, _str) \
830 do { \
831 if (_cond) \
832 printf("\t\t\t" _str "\n"); \
833 } while (0)
834
835 printf("\t\tCapabilities: 0x%02x\n", cap);
836
028c0de5 837 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
357c1a5d
LR
838 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
839 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
840
841 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
842 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
843 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
844
845 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
846 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
847 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
848 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
849
850 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
851 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
852 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
853 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
854
855 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
856
c79c7464
CL
857 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
858 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
357c1a5d
LR
859
860 /*
861 * For beacons and probe response this would mean the BSS
862 * does or does not allow the usage of DSSS/CCK HT40.
863 * Otherwise it means the STA does or does not use
864 * DSSS/CCK HT40.
865 */
866 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
867 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
868
869 /* BIT(13) is reserved */
870
871 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
872
873 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
874#undef PRINT_HT_CAP
875}
7ddfb679
JB
876
877void print_ht_mcs(const __u8 *mcs)
878{
879 /* As defined in 7.3.2.57.4 Supported MCS Set field */
880 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
881 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
882
5ba6a62b 883 max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
7ddfb679
JB
884 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
885 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
886 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
887 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
888
889 if (max_rx_supp_data_rate)
890 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
891 /* XXX: else see 9.6.0e.5.3 how to get this I think */
892
893 if (tx_mcs_set_defined) {
894 if (tx_mcs_set_equal) {
2a79feb0 895 printf("\t\tHT TX/RX MCS rate indexes supported:");
7ddfb679
JB
896 print_mcs_index(mcs);
897 } else {
898 printf("\t\tHT RX MCS rate indexes supported:");
899 print_mcs_index(mcs);
900
901 if (tx_unequal_modulation)
902 printf("\t\tTX unequal modulation supported\n");
903 else
904 printf("\t\tTX unequal modulation not supported\n");
905
906 printf("\t\tHT TX Max spatial streams: %d\n",
907 tx_max_num_spatial_streams);
908
909 printf("\t\tHT TX MCS rate indexes supported may differ\n");
910 }
911 } else {
912 printf("\t\tHT RX MCS rate indexes supported:");
913 print_mcs_index(mcs);
089bb35d 914 printf("\t\tHT TX MCS rate indexes are undefined\n");
7ddfb679
JB
915 }
916}
54eb1613
JB
917
918void print_vht_info(__u32 capa, const __u8 *mcs)
919{
920 __u16 tmp;
921 int i;
922
923 printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
924
925#define PRINT_VHT_CAPA(_bit, _str) \
926 do { \
927 if (capa & BIT(_bit)) \
928 printf("\t\t\t" _str "\n"); \
929 } while (0)
930
931 printf("\t\t\tMax MPDU length: ");
932 switch (capa & 3) {
933 case 0: printf("3895\n"); break;
934 case 1: printf("7991\n"); break;
935 case 2: printf("11454\n"); break;
936 case 3: printf("(reserved)\n");
937 }
938 printf("\t\t\tSupported Channel Width: ");
939 switch ((capa >> 2) & 3) {
940 case 0: printf("neither 160 nor 80+80\n"); break;
941 case 1: printf("160 MHz\n"); break;
942 case 2: printf("160 MHz, 80+80 MHz\n"); break;
943 case 3: printf("(reserved)\n");
944 }
945 PRINT_VHT_CAPA(4, "RX LDPC");
946 PRINT_VHT_CAPA(5, "short GI (80 MHz)");
947 PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
948 PRINT_VHT_CAPA(7, "TX STBC");
949 /* RX STBC */
950 PRINT_VHT_CAPA(11, "SU Beamformer");
951 PRINT_VHT_CAPA(12, "SU Beamformee");
952 /* compressed steering */
953 /* # of sounding dimensions */
954 PRINT_VHT_CAPA(19, "MU Beamformer");
955 PRINT_VHT_CAPA(20, "MU Beamformee");
956 PRINT_VHT_CAPA(21, "VHT TXOP PS");
957 PRINT_VHT_CAPA(22, "+HTC-VHT");
958 /* max A-MPDU */
959 /* VHT link adaptation */
75271051
MB
960 PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
961 PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
54eb1613
JB
962
963 printf("\t\tVHT RX MCS set:\n");
964 tmp = mcs[0] | (mcs[1] << 8);
965 for (i = 1; i <= 8; i++) {
966 printf("\t\t\t%d streams: ", i);
967 switch ((tmp >> ((i-1)*2) ) & 3) {
968 case 0: printf("MCS 0-7\n"); break;
969 case 1: printf("MCS 0-8\n"); break;
970 case 2: printf("MCS 0-9\n"); break;
971 case 3: printf("not supported\n"); break;
972 }
973 }
974 tmp = mcs[2] | (mcs[3] << 8);
975 printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
976
977 printf("\t\tVHT TX MCS set:\n");
978 tmp = mcs[4] | (mcs[5] << 8);
979 for (i = 1; i <= 8; i++) {
980 printf("\t\t\t%d streams: ", i);
981 switch ((tmp >> ((i-1)*2) ) & 3) {
982 case 0: printf("MCS 0-7\n"); break;
983 case 1: printf("MCS 0-8\n"); break;
984 case 2: printf("MCS 0-9\n"); break;
985 case 3: printf("not supported\n"); break;
986 }
987 }
988 tmp = mcs[6] | (mcs[7] << 8);
989 printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
990}
492354de 991
c741be9f
JC
992void print_he_info(struct nlattr *nl_iftype)
993{
994 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
995 struct nlattr *tb_flags[NL80211_IFTYPE_MAX + 1];
996 char *iftypes[NUM_NL80211_IFTYPES] = {
997 "Unspec", "Adhoc", "Station", "AP", "AP/VLAN", "WDS", "Monitor",
998 "Mesh", "P2P/Client", "P2P/Go", "P2P/Device", "OCB", "NAN",
999 };
1000 __u16 mac_cap[3] = { 0 };
1001 __u16 phy_cap[6] = { 0 };
1002 __u16 mcs_set[6] = { 0 };
1003 __u8 ppet[25] = { 0 };
1004 size_t len;
1005 int i;
1006
1007 #define PRINT_HE_CAP(_var, _idx, _bit, _str) \
1008 do { \
1009 if (_var[_idx] & BIT(_bit)) \
1010 printf("\t\t\t\t" _str "\n"); \
1011 } while (0)
1012
1013 #define PRINT_HE_CAP_MASK(_var, _idx, _shift, _mask, _str) \
1014 do { \
1015 if ((_var[_idx] >> _shift) & _mask) \
1016 printf("\t\t\t\t" _str ": %d\n", (_var[_idx] >> _shift) & _mask); \
1017 } while (0)
1018
1019 #define PRINT_HE_MAC_CAP(...) PRINT_HE_CAP(mac_cap, __VA_ARGS__)
1020 #define PRINT_HE_MAC_CAP_MASK(...) PRINT_HE_CAP_MASK(mac_cap, __VA_ARGS__)
1021 #define PRINT_HE_PHY_CAP(...) PRINT_HE_CAP(phy_cap, __VA_ARGS__)
1022 #define PRINT_HE_PHY_CAP0(_idx, _bit, ...) PRINT_HE_CAP(phy_cap, _idx, _bit + 8, __VA_ARGS__)
1023 #define PRINT_HE_PHY_CAP_MASK(...) PRINT_HE_CAP_MASK(phy_cap, __VA_ARGS__)
1024
1025 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
1026 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
1027
1028 if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
1029 return;
1030
1031 if (nla_parse_nested(tb_flags, NL80211_IFTYPE_MAX,
1032 tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES], NULL))
1033 return;
1034
1035 printf("\t\tHE Iftypes:");
1036 for (i = 0; i < NUM_NL80211_IFTYPES; i++)
1037 if (nla_get_flag(tb_flags[i]) && iftypes[i])
1038 printf(" %s", iftypes[i]);
1039 printf("\n");
1040
1041 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]) {
1042 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]);
1043 if (len > sizeof(mac_cap))
1044 len = sizeof(mac_cap);
1045 memcpy(mac_cap,
1046 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]),
1047 len);
1048 }
1049 printf("\t\t\tHE MAC Capabilities (0x");
1050 for (i = 0; i < 3; i++)
1051 printf("%04x", mac_cap[i]);
1052 printf("):\n");
1053
1054 PRINT_HE_MAC_CAP(0, 0, "+HTC HE Supported");
1055 PRINT_HE_MAC_CAP(0, 1, "TWT Requester");
1056 PRINT_HE_MAC_CAP(0, 2, "TWT Responder");
1057 PRINT_HE_MAC_CAP_MASK(0, 3, 0x3, "Dynamic BA Fragementation Level");
1058 PRINT_HE_MAC_CAP_MASK(0, 5, 0x7, "Maximum number of MSDUS Fragments");
1059 PRINT_HE_MAC_CAP_MASK(0, 8, 0x3, "Minimum Payload size of 128 bytes");
1060 PRINT_HE_MAC_CAP_MASK(0, 10, 0x3, "Trigger Frame MAC Padding Duration");
1061 PRINT_HE_MAC_CAP_MASK(0, 12, 0x7, "Multi-TID Aggregation Support");
1062
1063 PRINT_HE_MAC_CAP(1, 1, "All Ack");
1064 PRINT_HE_MAC_CAP(1, 2, "TRS");
1065 PRINT_HE_MAC_CAP(1, 3, "BSR");
1066 PRINT_HE_MAC_CAP(1, 4, "Broadcast TWT");
1067 PRINT_HE_MAC_CAP(1, 5, "32-bit BA Bitmap");
1068 PRINT_HE_MAC_CAP(1, 6, "MU Cascading");
1069 PRINT_HE_MAC_CAP(1, 7, "Ack-Enabled Aggregation");
1070 PRINT_HE_MAC_CAP(1, 9, "OM Control");
1071 PRINT_HE_MAC_CAP(1, 10, "OFDMA RA");
1072 PRINT_HE_MAC_CAP_MASK(1, 11, 0x3, "Maximum A-MPDU Length Exponent");
1073 PRINT_HE_MAC_CAP(1, 13, "A-MSDU Fragmentation");
1074 PRINT_HE_MAC_CAP(1, 14, "Flexible TWT Scheduling");
1075 PRINT_HE_MAC_CAP(1, 15, "RX Control Frame to MultiBSS");
1076
1077 PRINT_HE_MAC_CAP(2, 0, "BSRP BQRP A-MPDU Aggregation");
1078 PRINT_HE_MAC_CAP(2, 1, "QTP");
1079 PRINT_HE_MAC_CAP(2, 2, "BQR");
1080 PRINT_HE_MAC_CAP(2, 3, "SRP Responder Role");
1081 PRINT_HE_MAC_CAP(2, 4, "NDP Feedback Report");
1082 PRINT_HE_MAC_CAP(2, 5, "OPS");
1083 PRINT_HE_MAC_CAP(2, 6, "A-MSDU in A-MPDU");
1084 PRINT_HE_MAC_CAP_MASK(2, 7, 7, "Multi-TID Aggregation TX");
1085 PRINT_HE_MAC_CAP(2, 10, "HE Subchannel Selective Transmission");
1086 PRINT_HE_MAC_CAP(2, 11, "UL 2x996-Tone RU");
1087 PRINT_HE_MAC_CAP(2, 12, "OM Control UL MU Data Disable RX");
1088
1089 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1090 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1091
1092 if (len > sizeof(phy_cap) - 1)
1093 len = sizeof(phy_cap) - 1;
1094 memcpy(&((__u8 *)phy_cap)[1],
1095 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1096 len);
1097 }
1098 printf("\t\t\tHE PHY Capabilities: (0x");
1099 for (i = 0; i < 11; i++)
1100 printf("%02x", ((__u8 *)phy_cap)[i + 1]);
1101 printf("):\n");
1102
1103 PRINT_HE_PHY_CAP0(0, 1, "HE40/2.4GHz");
1104 PRINT_HE_PHY_CAP0(0, 2, "HE40/HE80/5GHz");
1105 PRINT_HE_PHY_CAP0(0, 3, "HE160/5GHz");
1106 PRINT_HE_PHY_CAP0(0, 4, "HE160/HE80+80/5GHz");
1107 PRINT_HE_PHY_CAP0(0, 5, "242 tone RUs/2.4GHz");
1108 PRINT_HE_PHY_CAP0(0, 6, "242 tone RUs/5GHz");
1109
1110 PRINT_HE_PHY_CAP_MASK(1, 0, 0xf, "Punctured Preamble RX");
1111 PRINT_HE_PHY_CAP_MASK(1, 4, 0x1, "Device Class");
1112 PRINT_HE_PHY_CAP(1, 5, "LDPC Coding in Payload");
1113 PRINT_HE_PHY_CAP(1, 6, "HE SU PPDU with 1x HE-LTF and 0.8us GI");
1114 PRINT_HE_PHY_CAP_MASK(1, 7, 0x3, "Midamble Rx Max NSTS");
1115 PRINT_HE_PHY_CAP(1, 9, "NDP with 4x HE-LTF and 3.2us GI");
1116 PRINT_HE_PHY_CAP(1, 10, "STBC Tx <= 80MHz");
1117 PRINT_HE_PHY_CAP(1, 11, "STBC Rx <= 80MHz");
1118 PRINT_HE_PHY_CAP(1, 12, "Doppler Tx");
1119 PRINT_HE_PHY_CAP(1, 13, "Doppler Rx");
1120 PRINT_HE_PHY_CAP(1, 14, "Full Bandwidth UL MU-MIMO");
1121 PRINT_HE_PHY_CAP(1, 15, "Partial Bandwidth UL MU-MIMO");
1122
1123 PRINT_HE_PHY_CAP_MASK(2, 0, 0x3, "DCM Max Constellation");
1124 PRINT_HE_PHY_CAP_MASK(2, 2, 0x1, "DCM Max NSS Tx");
1125 PRINT_HE_PHY_CAP_MASK(2, 3, 0x3, "DCM Max Constellation Rx");
1126 PRINT_HE_PHY_CAP_MASK(2, 5, 0x1, "DCM Max NSS Rx");
1127 PRINT_HE_PHY_CAP(2, 6, "Rx HE MU PPDU from Non-AP STA");
1128 PRINT_HE_PHY_CAP(2, 7, "SU Beamformer");
1129 PRINT_HE_PHY_CAP(2, 8, "SU Beamformee");
1130 PRINT_HE_PHY_CAP(2, 9, "MU Beamformer");
1131 PRINT_HE_PHY_CAP_MASK(2, 10, 0x7, "Beamformee STS <= 80Mhz");
1132 PRINT_HE_PHY_CAP_MASK(2, 13, 0x7, "Beamformee STS > 80Mhz");
1133
1134 PRINT_HE_PHY_CAP_MASK(3, 0, 0x7, "Sounding Dimensions <= 80Mhz");
1135 PRINT_HE_PHY_CAP_MASK(3, 3, 0x7, "Sounding Dimensions > 80Mhz");
1136 PRINT_HE_PHY_CAP(3, 6, "Ng = 16 SU Feedback");
1137 PRINT_HE_PHY_CAP(3, 7, "Ng = 16 MU Feedback");
1138 PRINT_HE_PHY_CAP(3, 8, "Codebook Size SU Feedback");
1139 PRINT_HE_PHY_CAP(3, 9, "Codebook Size MU Feedback");
1140 PRINT_HE_PHY_CAP(3, 10, "Triggered SU Beamforming Feedback");
1141 PRINT_HE_PHY_CAP(3, 11, "Triggered MU Beamforming Feedback");
1142 PRINT_HE_PHY_CAP(3, 12, "Triggered CQI Feedback");
1143 PRINT_HE_PHY_CAP(3, 13, "Partial Bandwidth Extended Range");
1144 PRINT_HE_PHY_CAP(3, 14, "Partial Bandwidth DL MU-MIMO");
1145 PRINT_HE_PHY_CAP(3, 15, "PPE Threshold Present");
1146
1147 PRINT_HE_PHY_CAP(4, 0, "SRP-based SR");
1148 PRINT_HE_PHY_CAP(4, 1, "Power Boost Factor ar");
1149 PRINT_HE_PHY_CAP(4, 2, "HE SU PPDU & HE PPDU 4x HE-LTF 0.8us GI");
1150 PRINT_HE_PHY_CAP_MASK(4, 3, 0x7, "Max NC");
1151 PRINT_HE_PHY_CAP(4, 6, "STBC Tx > 80MHz");
1152 PRINT_HE_PHY_CAP(4, 7, "STBC Rx > 80MHz");
1153 PRINT_HE_PHY_CAP(4, 8, "HE ER SU PPDU 4x HE-LTF 0.8us GI");
1154 PRINT_HE_PHY_CAP(4, 9, "20MHz in 40MHz HE PPDU 2.4GHz");
1155 PRINT_HE_PHY_CAP(4, 10, "20MHz in 160/80+80MHz HE PPDU");
1156 PRINT_HE_PHY_CAP(4, 11, "80MHz in 160/80+80MHz HE PPDU");
1157 PRINT_HE_PHY_CAP(4, 12, "HE ER SU PPDU 1x HE-LTF 0.8us GI");
1158 PRINT_HE_PHY_CAP(4, 13, "Midamble Rx 2x & 1x HE-LTF");
1159 PRINT_HE_PHY_CAP_MASK(4, 14, 0x3, "DCM Max BW");
1160
1161 PRINT_HE_PHY_CAP(5, 0, "Longer Than 16HE SIG-B OFDM Symbols");
1162 PRINT_HE_PHY_CAP(5, 1, "Non-Triggered CQI Feedback");
1163 PRINT_HE_PHY_CAP(5, 2, "TX 1024-QAM");
1164 PRINT_HE_PHY_CAP(5, 3, "RX 1024-QAM");
1165 PRINT_HE_PHY_CAP(5, 4, "RX Full BW SU Using HE MU PPDU with Compression SIGB");
1166 PRINT_HE_PHY_CAP(5, 5, "RX Full BW SU Using HE MU PPDU with Non-Compression SIGB");
1167
1168 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]) {
1169 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]);
1170 if (len > sizeof(mcs_set))
1171 len = sizeof(mcs_set);
1172 memcpy(mcs_set,
1173 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]),
1174 len);
1175 }
1176
1177 for (i = 0; i < 3; i++) {
1178 __u8 phy_cap_support[] = { BIT(1) | BIT(2), BIT(3), BIT(4) };
1179 char *bw[] = { "<= 80", "160", "80+80" };
1180 int j;
1181
1182 if ((phy_cap[0] & (phy_cap_support[i] << 8)) == 0)
1183 continue;
1184
1185 for (j = 0; j < 2; j++) {
1186 int k;
1187 printf("\t\t\tHE %s MCS and NSS set %s MHz\n", j ? "TX" : "RX", bw[i]);
1188 for (k = 0; k < 8; k++) {
1189 __u16 mcs = mcs_set[(i * 2) + j];
1190 mcs >>= k * 2;
1191 mcs &= 0x3;
1192 printf("\t\t\t\t\t %d streams: ", k + 1);
1193 if (mcs == 3)
1194 printf("not supported\n");
1195 else
1196 printf("MCS 0-%d\n", 7 + (mcs * 2));
1197 }
1198
1199 }
1200 }
1201
1202 len = 0;
1203 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]) {
1204 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]);
1205 if (len > sizeof(ppet))
1206 len = sizeof(ppet);
1207 memcpy(ppet,
1208 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]),
1209 len);
1210 }
1211
1212 if (len && (phy_cap[3] & BIT(15))) {
1213 size_t i;
1214
1215 printf("\t\t\tPPE Threshold ");
1216 for (i = 0; i < len; i++)
1217 if (ppet[i])
1218 printf("0x%02x ", ppet[i]);
1219 printf("\n");
1220 }
1221}
1222
492354de
JD
1223void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
1224{
0ee571d5 1225 size_t i;
492354de
JD
1226
1227 printf("%s: ", prefix);
1228 for (i = 0; i < size; i++) {
1229 if (i && i % 16 == 0)
1230 printf("\n%s: ", prefix);
1231 printf("%02x ", buf[i]);
1232 }
1233 printf("\n\n");
1234}
c1b2b633
SE
1235
1236int get_cf1(const struct chanmode *chanmode, unsigned long freq)
1237{
1238 unsigned int cf1 = freq, j;
c56036a4
PKC
1239 unsigned int bw80[] = { 5180, 5260, 5500, 5580, 5660, 5745,
1240 5955, 6035, 6115, 6195, 6275, 6355,
1241 6435, 6515, 6595, 6675, 6755, 6835,
1242 6195, 6995 };
b6f2dac4 1243 unsigned int vht160[] = { 5180, 5500 };
c1b2b633
SE
1244
1245 switch (chanmode->width) {
1246 case NL80211_CHAN_WIDTH_80:
1247 /* setup center_freq1 */
c56036a4
PKC
1248 for (j = 0; j < ARRAY_SIZE(bw80); j++) {
1249 if (freq >= bw80[j] && freq < bw80[j] + 80)
c1b2b633
SE
1250 break;
1251 }
1252
c56036a4 1253 if (j == ARRAY_SIZE(bw80))
c1b2b633
SE
1254 break;
1255
c56036a4 1256 cf1 = bw80[j] + 30;
c1b2b633 1257 break;
b6f2dac4
T
1258 case NL80211_CHAN_WIDTH_160:
1259 /* setup center_freq1 */
1260 for (j = 0; j < ARRAY_SIZE(vht160); j++) {
1261 if (freq >= vht160[j] && freq < vht160[j] + 160)
1262 break;
1263 }
1264
1265 if (j == ARRAY_SIZE(vht160))
1266 break;
1267
1268 cf1 = vht160[j] + 70;
1269 break;
c1b2b633
SE
1270 default:
1271 cf1 = freq + chanmode->freq1_diff;
1272 break;
1273 }
1274
1275 return cf1;
1276}
3c0117c1
JB
1277
1278int parse_random_mac_addr(struct nl_msg *msg, char *addrs)
1279{
1280 char *a_addr, *a_mask, *sep;
1281 unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
1282
1283 if (!*addrs) {
1284 /* randomise all but the multicast bit */
1285 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN,
1286 "\x00\x00\x00\x00\x00\x00");
1287 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
1288 "\x01\x00\x00\x00\x00\x00");
1289 return 0;
1290 }
1291
1292 if (*addrs != '=')
1293 return 1;
1294
1295 addrs++;
1296 sep = strchr(addrs, '/');
1297 a_addr = addrs;
1298
1299 if (!sep)
1300 return 1;
1301
1302 *sep = 0;
1303 a_mask = sep + 1;
1304 if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
1305 return 1;
1306
1307 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1308 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
1309
1310 return 0;
1311 nla_put_failure:
1312 return -ENOBUFS;
1313}