]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
iw: unify interface type list printing
[thirdparty/iw.git] / util.c
1 #include <ctype.h>
2 #include <netlink/attr.h>
3 #include <errno.h>
4 #include <stdbool.h>
5 #include "iw.h"
6 #include "nl80211.h"
7
8 void mac_addr_n2a(char *mac_addr, const unsigned char *arg)
9 {
10 int i, l;
11
12 l = 0;
13 for (i = 0; i < ETH_ALEN ; i++) {
14 if (i == 0) {
15 sprintf(mac_addr+l, "%02x", arg[i]);
16 l += 2;
17 } else {
18 sprintf(mac_addr+l, ":%02x", arg[i]);
19 l += 3;
20 }
21 }
22 }
23
24 int 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 }
50
51 int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len,
52 unsigned char **mask)
53 {
54 size_t len = strlen(hexmask) / 2;
55 unsigned char *result_val;
56 unsigned char *result_mask = NULL;
57
58 int pos = 0;
59
60 *result_len = 0;
61
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 }
72
73 while (1) {
74 char *cp = strchr(hexmask, ':');
75 if (cp) {
76 *cp = 0;
77 cp++;
78 }
79
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++;
102
103 if (!cp)
104 break;
105 hexmask = cp;
106 }
107
108 return 0;
109 error:
110 free(result_val);
111 free(result_mask);
112 return -1;
113 }
114
115 unsigned 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;
122 }
123
124 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
125 "unspecified",
126 "IBSS",
127 "managed",
128 "AP",
129 "AP/VLAN",
130 "WDS",
131 "monitor",
132 "mesh point",
133 "P2P-client",
134 "P2P-GO",
135 "P2P-device",
136 "outside context of a BSS",
137 "NAN",
138 };
139
140 static char modebuf[100];
141
142 const char *iftype_name(enum nl80211_iftype iftype)
143 {
144 if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
145 return ifmodes[iftype];
146 sprintf(modebuf, "Unknown mode (%d)", iftype);
147 return modebuf;
148 }
149
150 static const char *commands[NL80211_CMD_MAX + 1] = {
151 #include "nl80211-commands.inc"
152 };
153
154 static char cmdbuf[100];
155
156 const char *command_name(enum nl80211_commands cmd)
157 {
158 if (cmd <= NL80211_CMD_MAX && commands[cmd])
159 return commands[cmd];
160 sprintf(cmdbuf, "Unknown command (%d)", cmd);
161 return cmdbuf;
162 }
163
164 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
165 {
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;
183 case NL80211_BAND_6GHZ:
184 /* see 802.11ax D6.1 27.3.23.2 */
185 if (chan == 2)
186 return 5935;
187 if (chan <= 253)
188 return 5950 + chan * 5;
189 break;
190 case NL80211_BAND_60GHZ:
191 if (chan < 7)
192 return 56160 + chan * 2160;
193 break;
194 default:
195 ;
196 }
197 return 0; /* not supported */
198 }
199
200 int ieee80211_frequency_to_channel(int freq)
201 {
202 /* see 802.11-2007 17.3.8.3.2 and Annex J */
203 if (freq == 2484)
204 return 14;
205 /* see 802.11ax D6.1 27.3.23.2 and Annex E */
206 else if (freq == 5935)
207 return 2;
208 else if (freq < 2484)
209 return (freq - 2407) / 5;
210 else if (freq >= 4910 && freq <= 4980)
211 return (freq - 4000) / 5;
212 else if (freq < 5950)
213 return (freq - 5000) / 5;
214 else if (freq <= 45000) /* DMG band lower limit */
215 /* see 802.11ax D6.1 27.3.23.2 */
216 return (freq - 5950) / 5;
217 else if (freq >= 58320 && freq <= 70200)
218 return (freq - 56160) / 2160;
219 else
220 return 0;
221 }
222
223 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
224 {
225 int i;
226
227 for (i = 0; i < len; i++) {
228 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
229 printf("%c", data[i]);
230 else if (data[i] == ' ' &&
231 (i != 0 && i != len -1))
232 printf(" ");
233 else
234 printf("\\x%.2x", data[i]);
235 }
236 }
237
238 static 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
247 static int hex2byte(const char *hex)
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
260 char *hex2bin(const char *hex, char *buf)
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
277 static 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
289 static int parse_cipher_suite(const char *cipher_str)
290 {
291
292 if (!strcmp(cipher_str, "TKIP"))
293 return 0x000FAC02;
294 if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128"))
295 return 0x000FAC04;
296 if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128"))
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
305 int parse_keys(struct nl_msg *msg, char **argv[], int *argc)
306 {
307 struct nlattr *keys;
308 int i = 0;
309 bool have_default = false;
310 char *arg = **argv;
311 char keybuf[13];
312 int pos = 0;
313
314 if (!*argc)
315 return 1;
316
317 if (!memcmp(&arg[pos], "psk", 3)) {
318 char psk_keybuf[32];
319 int cipher_suite, akm_suite;
320
321 if (*argc < 4)
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
339 *argv += 1;
340 *argc -= 1;
341 arg = **argv;
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
349 *argv += 1;
350 *argc -= 1;
351 arg = **argv;
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
359 *argv += 1;
360 *argc -= 1;
361 arg = **argv;
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
369 *argv += 1;
370 *argc -= 1;
371 return 0;
372 }
373
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 {
381 int keylen;
382 struct nlattr *key = nla_nest_start(msg, ++i);
383 char *keydata;
384
385 arg = **argv;
386 pos = 0;
387
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++;
396 have_default = true;
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);
408 /* fall through */
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);
415 /* fall through */
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
429 *argv += 1;
430 *argc -= 1;
431
432 /* one key should be TX key */
433 if (!have_default && !*argc)
434 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
435
436 nla_nest_end(msg, key);
437 } while (*argc);
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"
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");
456 return 2;
457 }
458
459 enum nl80211_chan_width str_to_bw(const char *str)
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 };
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
483 static int parse_freqs(struct chandef *chandef, int argc, char **argv,
484 int *parsed)
485 {
486 uint32_t freq;
487 char *end;
488 bool need_cf1 = false, need_cf2 = false;
489
490 if (argc < 1)
491 return 0;
492
493 chandef->width = str_to_bw(argv[0]);
494
495 switch (chandef->width) {
496 case NL80211_CHAN_WIDTH_20_NOHT:
497 /* First argument was not understood, give up gracefully. */
498 return 0;
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;
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;
518 }
519
520 *parsed += 1;
521
522 if (!need_cf1)
523 return 0;
524
525 if (argc < 2)
526 return 1;
527
528 /* center freq 1 */
529 if (!*argv[1])
530 return 1;
531 freq = strtoul(argv[1], &end, 10);
532 if (*end)
533 return 1;
534 *parsed += 1;
535
536 chandef->center_freq1 = freq;
537
538 if (!need_cf2)
539 return 0;
540
541 if (argc < 3)
542 return 1;
543
544 /* center freq 2 */
545 if (!*argv[2])
546 return 1;
547 freq = strtoul(argv[2], &end, 10);
548 if (*end)
549 return 1;
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 *
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.
577 *
578 * The working specifier if chan is set are:
579 * <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
580 *
581 * And if frequency is set:
582 * <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
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 */
589 int 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 },
622 { .name = "160MHz",
623 .width = NL80211_CHAN_WIDTH_160,
624 .freq1_diff = 0,
625 .chantype = -1 },
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
693 int 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
739 static void print_mcs_index(const __u8 *mcs)
740 {
741 int mcs_bit, prev_bit = -2, prev_cont = 0;
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 }
767
768 if (prev_cont)
769 printf("%d", prev_bit);
770 printf("\n");
771 }
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 */
779 static __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
790 static 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:
802 return "BUG (spacing more than 3 bits!)";
803 }
804 }
805
806 void print_ampdu_length(__u8 exponent)
807 {
808 __u32 max_ampdu_length;
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);
815 } else {
816 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
817 "(exponent: %d)\n", exponent);
818 }
819 }
820
821 void print_ampdu_spacing(__u8 spacing)
822 {
823 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
824 print_ampdu_space(spacing), spacing);
825 }
826
827 void 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
837 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
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
857 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
858 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
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 }
876
877 void 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
883 max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
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) {
895 printf("\t\tHT TX/RX MCS rate indexes supported:");
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);
914 printf("\t\tHT TX MCS rate indexes are undefined\n");
915 }
916 }
917
918 void 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 */
960 PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
961 PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
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 }
991
992 static void __print_he_capa(const __u16 *mac_cap,
993 const __u16 *phy_cap,
994 const __u16 *mcs_set, size_t mcs_len,
995 const __u8 *ppet, int ppet_len,
996 bool indent)
997 {
998 size_t mcs_used;
999 int i;
1000 const char *pre = indent ? "\t" : "";
1001
1002 #define PRINT_HE_CAP(_var, _idx, _bit, _str) \
1003 do { \
1004 if (_var[_idx] & BIT(_bit)) \
1005 printf("%s\t\t\t" _str "\n", pre); \
1006 } while (0)
1007
1008 #define PRINT_HE_CAP_MASK(_var, _idx, _shift, _mask, _str) \
1009 do { \
1010 if ((_var[_idx] >> _shift) & _mask) \
1011 printf("%s\t\t\t" _str ": %d\n", pre, (_var[_idx] >> _shift) & _mask); \
1012 } while (0)
1013
1014 #define PRINT_HE_MAC_CAP(...) PRINT_HE_CAP(mac_cap, __VA_ARGS__)
1015 #define PRINT_HE_MAC_CAP_MASK(...) PRINT_HE_CAP_MASK(mac_cap, __VA_ARGS__)
1016 #define PRINT_HE_PHY_CAP(...) PRINT_HE_CAP(phy_cap, __VA_ARGS__)
1017 #define PRINT_HE_PHY_CAP0(_idx, _bit, ...) PRINT_HE_CAP(phy_cap, _idx, _bit + 8, __VA_ARGS__)
1018 #define PRINT_HE_PHY_CAP_MASK(...) PRINT_HE_CAP_MASK(phy_cap, __VA_ARGS__)
1019
1020 printf("%s\t\tHE MAC Capabilities (0x", pre);
1021 for (i = 0; i < 3; i++)
1022 printf("%04x", mac_cap[i]);
1023 printf("):\n");
1024
1025 PRINT_HE_MAC_CAP(0, 0, "+HTC HE Supported");
1026 PRINT_HE_MAC_CAP(0, 1, "TWT Requester");
1027 PRINT_HE_MAC_CAP(0, 2, "TWT Responder");
1028 PRINT_HE_MAC_CAP_MASK(0, 3, 0x3, "Dynamic BA Fragementation Level");
1029 PRINT_HE_MAC_CAP_MASK(0, 5, 0x7, "Maximum number of MSDUS Fragments");
1030 PRINT_HE_MAC_CAP_MASK(0, 8, 0x3, "Minimum Payload size of 128 bytes");
1031 PRINT_HE_MAC_CAP_MASK(0, 10, 0x3, "Trigger Frame MAC Padding Duration");
1032 PRINT_HE_MAC_CAP_MASK(0, 12, 0x7, "Multi-TID Aggregation Support");
1033
1034 PRINT_HE_MAC_CAP(1, 1, "All Ack");
1035 PRINT_HE_MAC_CAP(1, 2, "TRS");
1036 PRINT_HE_MAC_CAP(1, 3, "BSR");
1037 PRINT_HE_MAC_CAP(1, 4, "Broadcast TWT");
1038 PRINT_HE_MAC_CAP(1, 5, "32-bit BA Bitmap");
1039 PRINT_HE_MAC_CAP(1, 6, "MU Cascading");
1040 PRINT_HE_MAC_CAP(1, 7, "Ack-Enabled Aggregation");
1041 PRINT_HE_MAC_CAP(1, 9, "OM Control");
1042 PRINT_HE_MAC_CAP(1, 10, "OFDMA RA");
1043 PRINT_HE_MAC_CAP_MASK(1, 11, 0x3, "Maximum A-MPDU Length Exponent");
1044 PRINT_HE_MAC_CAP(1, 13, "A-MSDU Fragmentation");
1045 PRINT_HE_MAC_CAP(1, 14, "Flexible TWT Scheduling");
1046 PRINT_HE_MAC_CAP(1, 15, "RX Control Frame to MultiBSS");
1047
1048 PRINT_HE_MAC_CAP(2, 0, "BSRP BQRP A-MPDU Aggregation");
1049 PRINT_HE_MAC_CAP(2, 1, "QTP");
1050 PRINT_HE_MAC_CAP(2, 2, "BQR");
1051 PRINT_HE_MAC_CAP(2, 3, "SRP Responder Role");
1052 PRINT_HE_MAC_CAP(2, 4, "NDP Feedback Report");
1053 PRINT_HE_MAC_CAP(2, 5, "OPS");
1054 PRINT_HE_MAC_CAP(2, 6, "A-MSDU in A-MPDU");
1055 PRINT_HE_MAC_CAP_MASK(2, 7, 7, "Multi-TID Aggregation TX");
1056 PRINT_HE_MAC_CAP(2, 10, "HE Subchannel Selective Transmission");
1057 PRINT_HE_MAC_CAP(2, 11, "UL 2x996-Tone RU");
1058 PRINT_HE_MAC_CAP(2, 12, "OM Control UL MU Data Disable RX");
1059
1060 printf("%s\t\tHE PHY Capabilities: (0x", pre);
1061 for (i = 0; i < 11; i++)
1062 printf("%02x", ((__u8 *)phy_cap)[i + 1]);
1063 printf("):\n");
1064
1065 PRINT_HE_PHY_CAP0(0, 1, "HE40/2.4GHz");
1066 PRINT_HE_PHY_CAP0(0, 2, "HE40/HE80/5GHz");
1067 PRINT_HE_PHY_CAP0(0, 3, "HE160/5GHz");
1068 PRINT_HE_PHY_CAP0(0, 4, "HE160/HE80+80/5GHz");
1069 PRINT_HE_PHY_CAP0(0, 5, "242 tone RUs/2.4GHz");
1070 PRINT_HE_PHY_CAP0(0, 6, "242 tone RUs/5GHz");
1071
1072 PRINT_HE_PHY_CAP_MASK(1, 0, 0xf, "Punctured Preamble RX");
1073 PRINT_HE_PHY_CAP_MASK(1, 4, 0x1, "Device Class");
1074 PRINT_HE_PHY_CAP(1, 5, "LDPC Coding in Payload");
1075 PRINT_HE_PHY_CAP(1, 6, "HE SU PPDU with 1x HE-LTF and 0.8us GI");
1076 PRINT_HE_PHY_CAP_MASK(1, 7, 0x3, "Midamble Rx Max NSTS");
1077 PRINT_HE_PHY_CAP(1, 9, "NDP with 4x HE-LTF and 3.2us GI");
1078 PRINT_HE_PHY_CAP(1, 10, "STBC Tx <= 80MHz");
1079 PRINT_HE_PHY_CAP(1, 11, "STBC Rx <= 80MHz");
1080 PRINT_HE_PHY_CAP(1, 12, "Doppler Tx");
1081 PRINT_HE_PHY_CAP(1, 13, "Doppler Rx");
1082 PRINT_HE_PHY_CAP(1, 14, "Full Bandwidth UL MU-MIMO");
1083 PRINT_HE_PHY_CAP(1, 15, "Partial Bandwidth UL MU-MIMO");
1084
1085 PRINT_HE_PHY_CAP_MASK(2, 0, 0x3, "DCM Max Constellation");
1086 PRINT_HE_PHY_CAP_MASK(2, 2, 0x1, "DCM Max NSS Tx");
1087 PRINT_HE_PHY_CAP_MASK(2, 3, 0x3, "DCM Max Constellation Rx");
1088 PRINT_HE_PHY_CAP_MASK(2, 5, 0x1, "DCM Max NSS Rx");
1089 PRINT_HE_PHY_CAP(2, 6, "Rx HE MU PPDU from Non-AP STA");
1090 PRINT_HE_PHY_CAP(2, 7, "SU Beamformer");
1091 PRINT_HE_PHY_CAP(2, 8, "SU Beamformee");
1092 PRINT_HE_PHY_CAP(2, 9, "MU Beamformer");
1093 PRINT_HE_PHY_CAP_MASK(2, 10, 0x7, "Beamformee STS <= 80Mhz");
1094 PRINT_HE_PHY_CAP_MASK(2, 13, 0x7, "Beamformee STS > 80Mhz");
1095
1096 PRINT_HE_PHY_CAP_MASK(3, 0, 0x7, "Sounding Dimensions <= 80Mhz");
1097 PRINT_HE_PHY_CAP_MASK(3, 3, 0x7, "Sounding Dimensions > 80Mhz");
1098 PRINT_HE_PHY_CAP(3, 6, "Ng = 16 SU Feedback");
1099 PRINT_HE_PHY_CAP(3, 7, "Ng = 16 MU Feedback");
1100 PRINT_HE_PHY_CAP(3, 8, "Codebook Size SU Feedback");
1101 PRINT_HE_PHY_CAP(3, 9, "Codebook Size MU Feedback");
1102 PRINT_HE_PHY_CAP(3, 10, "Triggered SU Beamforming Feedback");
1103 PRINT_HE_PHY_CAP(3, 11, "Triggered MU Beamforming Feedback");
1104 PRINT_HE_PHY_CAP(3, 12, "Triggered CQI Feedback");
1105 PRINT_HE_PHY_CAP(3, 13, "Partial Bandwidth Extended Range");
1106 PRINT_HE_PHY_CAP(3, 14, "Partial Bandwidth DL MU-MIMO");
1107 PRINT_HE_PHY_CAP(3, 15, "PPE Threshold Present");
1108
1109 PRINT_HE_PHY_CAP(4, 0, "SRP-based SR");
1110 PRINT_HE_PHY_CAP(4, 1, "Power Boost Factor ar");
1111 PRINT_HE_PHY_CAP(4, 2, "HE SU PPDU & HE PPDU 4x HE-LTF 0.8us GI");
1112 PRINT_HE_PHY_CAP_MASK(4, 3, 0x7, "Max NC");
1113 PRINT_HE_PHY_CAP(4, 6, "STBC Tx > 80MHz");
1114 PRINT_HE_PHY_CAP(4, 7, "STBC Rx > 80MHz");
1115 PRINT_HE_PHY_CAP(4, 8, "HE ER SU PPDU 4x HE-LTF 0.8us GI");
1116 PRINT_HE_PHY_CAP(4, 9, "20MHz in 40MHz HE PPDU 2.4GHz");
1117 PRINT_HE_PHY_CAP(4, 10, "20MHz in 160/80+80MHz HE PPDU");
1118 PRINT_HE_PHY_CAP(4, 11, "80MHz in 160/80+80MHz HE PPDU");
1119 PRINT_HE_PHY_CAP(4, 12, "HE ER SU PPDU 1x HE-LTF 0.8us GI");
1120 PRINT_HE_PHY_CAP(4, 13, "Midamble Rx 2x & 1x HE-LTF");
1121 PRINT_HE_PHY_CAP_MASK(4, 14, 0x3, "DCM Max BW");
1122
1123 PRINT_HE_PHY_CAP(5, 0, "Longer Than 16HE SIG-B OFDM Symbols");
1124 PRINT_HE_PHY_CAP(5, 1, "Non-Triggered CQI Feedback");
1125 PRINT_HE_PHY_CAP(5, 2, "TX 1024-QAM");
1126 PRINT_HE_PHY_CAP(5, 3, "RX 1024-QAM");
1127 PRINT_HE_PHY_CAP(5, 4, "RX Full BW SU Using HE MU PPDU with Compression SIGB");
1128 PRINT_HE_PHY_CAP(5, 5, "RX Full BW SU Using HE MU PPDU with Non-Compression SIGB");
1129
1130 mcs_used = 0;
1131 for (i = 0; i < 3; i++) {
1132 __u8 phy_cap_support[] = { BIT(1) | BIT(2), BIT(3), BIT(4) };
1133 char *bw[] = { "<= 80", "160", "80+80" };
1134 int j;
1135
1136 if ((phy_cap[0] & (phy_cap_support[i] << 8)) == 0)
1137 continue;
1138
1139 /* Supports more, but overflow? Abort. */
1140 if ((i * 2 + 2) * sizeof(mcs_set[0]) >= mcs_len)
1141 return;
1142
1143 for (j = 0; j < 2; j++) {
1144 int k;
1145 printf("%s\t\tHE %s MCS and NSS set %s MHz\n", pre, j ? "TX" : "RX", bw[i]);
1146 for (k = 0; k < 8; k++) {
1147 __u16 mcs = mcs_set[(i * 2) + j];
1148 mcs >>= k * 2;
1149 mcs &= 0x3;
1150 printf("%s\t\t\t%d streams: ", pre, k + 1);
1151 if (mcs == 3)
1152 printf("not supported\n");
1153 else
1154 printf("MCS 0-%d\n", 7 + (mcs * 2));
1155 }
1156
1157 }
1158 mcs_used += 2 * sizeof(mcs_set[0]);
1159 }
1160
1161 /* Caller didn't provide ppet; infer it, if there's trailing space. */
1162 if (!ppet) {
1163 ppet = (const void *)((const __u8 *)mcs_set + mcs_used);
1164 if (mcs_used < mcs_len)
1165 ppet_len = mcs_len - mcs_used;
1166 else
1167 ppet_len = 0;
1168 }
1169
1170 if (ppet_len && (phy_cap[3] & BIT(15))) {
1171 printf("%s\t\tPPE Threshold ", pre);
1172 for (i = 0; i < ppet_len; i++)
1173 if (ppet[i])
1174 printf("0x%02x ", ppet[i]);
1175 printf("\n");
1176 }
1177 }
1178
1179 void print_iftype_list(const char *name, const char *pfx, struct nlattr *attr)
1180 {
1181 struct nlattr *ift;
1182 int rem;
1183
1184 printf("%s:\n", name);
1185 nla_for_each_nested(ift, attr, rem)
1186 printf("%s * %s\n", pfx, iftype_name(nla_type(ift)));
1187 }
1188
1189 void print_iftype_line(struct nlattr *attr)
1190 {
1191 struct nlattr *ift;
1192 bool first = true;
1193 int rem;
1194
1195 nla_for_each_nested(ift, attr, rem) {
1196 if (first)
1197 first = false;
1198 else
1199 printf(", ");
1200 printf("%s", iftype_name(nla_type(ift)));
1201 }
1202 }
1203
1204 void print_he_info(struct nlattr *nl_iftype)
1205 {
1206 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
1207 __u16 mac_cap[3] = { 0 };
1208 __u16 phy_cap[6] = { 0 };
1209 __u16 mcs_set[6] = { 0 };
1210 __u8 ppet[25] = { 0 };
1211 size_t len;
1212 int mcs_len = 0, ppet_len = 0;
1213
1214 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
1215 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
1216
1217 if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
1218 return;
1219
1220 printf("\t\tHE Iftypes: ");
1221 print_iftype_line(tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES]);
1222 printf("\n");
1223
1224 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]) {
1225 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]);
1226 if (len > sizeof(mac_cap))
1227 len = sizeof(mac_cap);
1228 memcpy(mac_cap,
1229 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]),
1230 len);
1231 }
1232
1233 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1234 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1235
1236 if (len > sizeof(phy_cap) - 1)
1237 len = sizeof(phy_cap) - 1;
1238 memcpy(&((__u8 *)phy_cap)[1],
1239 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1240 len);
1241 }
1242
1243 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]) {
1244 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]);
1245 if (len > sizeof(mcs_set))
1246 len = sizeof(mcs_set);
1247 memcpy(mcs_set,
1248 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]),
1249 len);
1250 mcs_len = len;
1251 }
1252
1253 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]) {
1254 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]);
1255 if (len > sizeof(ppet))
1256 len = sizeof(ppet);
1257 memcpy(ppet,
1258 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]),
1259 len);
1260 ppet_len = len;
1261 }
1262
1263 __print_he_capa(mac_cap, phy_cap, mcs_set, mcs_len, ppet, ppet_len,
1264 true);
1265 }
1266
1267 void print_he_capability(const uint8_t *ie, int len)
1268 {
1269 const void *mac_cap, *phy_cap, *mcs_set;
1270 int mcs_len;
1271 int i = 0;
1272
1273 mac_cap = &ie[i];
1274 i += 6;
1275
1276 phy_cap = &ie[i];
1277 i += 11;
1278
1279 mcs_set = &ie[i];
1280 mcs_len = len - i;
1281
1282 __print_he_capa(mac_cap, phy_cap - 1, mcs_set, mcs_len, NULL, 0, false);
1283 }
1284
1285 void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
1286 {
1287 size_t i;
1288
1289 printf("%s: ", prefix);
1290 for (i = 0; i < size; i++) {
1291 if (i && i % 16 == 0)
1292 printf("\n%s: ", prefix);
1293 printf("%02x ", buf[i]);
1294 }
1295 printf("\n\n");
1296 }
1297
1298 int get_cf1(const struct chanmode *chanmode, unsigned long freq)
1299 {
1300 unsigned int cf1 = freq, j;
1301 unsigned int bw80[] = { 5180, 5260, 5500, 5580, 5660, 5745,
1302 5955, 6035, 6115, 6195, 6275, 6355,
1303 6435, 6515, 6595, 6675, 6755, 6835,
1304 6195, 6995 };
1305 unsigned int vht160[] = { 5180, 5500 };
1306
1307 switch (chanmode->width) {
1308 case NL80211_CHAN_WIDTH_80:
1309 /* setup center_freq1 */
1310 for (j = 0; j < ARRAY_SIZE(bw80); j++) {
1311 if (freq >= bw80[j] && freq < bw80[j] + 80)
1312 break;
1313 }
1314
1315 if (j == ARRAY_SIZE(bw80))
1316 break;
1317
1318 cf1 = bw80[j] + 30;
1319 break;
1320 case NL80211_CHAN_WIDTH_160:
1321 /* setup center_freq1 */
1322 for (j = 0; j < ARRAY_SIZE(vht160); j++) {
1323 if (freq >= vht160[j] && freq < vht160[j] + 160)
1324 break;
1325 }
1326
1327 if (j == ARRAY_SIZE(vht160))
1328 break;
1329
1330 cf1 = vht160[j] + 70;
1331 break;
1332 default:
1333 cf1 = freq + chanmode->freq1_diff;
1334 break;
1335 }
1336
1337 return cf1;
1338 }
1339
1340 int parse_random_mac_addr(struct nl_msg *msg, char *addrs)
1341 {
1342 char *a_addr, *a_mask, *sep;
1343 unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
1344
1345 if (!*addrs) {
1346 /* randomise all but the multicast bit */
1347 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN,
1348 "\x00\x00\x00\x00\x00\x00");
1349 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
1350 "\x01\x00\x00\x00\x00\x00");
1351 return 0;
1352 }
1353
1354 if (*addrs != '=')
1355 return 1;
1356
1357 addrs++;
1358 sep = strchr(addrs, '/');
1359 a_addr = addrs;
1360
1361 if (!sep)
1362 return 1;
1363
1364 *sep = 0;
1365 a_mask = sep + 1;
1366 if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
1367 return 1;
1368
1369 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1370 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
1371
1372 return 0;
1373 nla_put_failure:
1374 return -ENOBUFS;
1375 }