]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
iw: nl80211: add NLA_F_NESTED to nla_nest_start() with older libnl versions
[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 WLAN_CIPHER_SUITE_TKIP;
294 if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128"))
295 return WLAN_CIPHER_SUITE_CCMP;
296 if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128"))
297 return WLAN_CIPHER_SUITE_GCMP;
298 if (!strcmp(cipher_str, "GCMP-256"))
299 return WLAN_CIPHER_SUITE_GCMP_256;
300 if (!strcmp(cipher_str, "CCMP-256"))
301 return WLAN_CIPHER_SUITE_CCMP_256;
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,
411 WLAN_CIPHER_SUITE_WEP40);
412 keylen = 5;
413 break;
414 case 26:
415 keydata = hex2bin(keydata, keybuf);
416 /* fall through */
417 case 13:
418 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
419 WLAN_CIPHER_SUITE_WEP104);
420 keylen = 13;
421 break;
422 default:
423 goto explain;
424 }
425
426 if (!keydata)
427 goto explain;
428
429 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
430
431 *argv += 1;
432 *argc -= 1;
433
434 /* one key should be TX key */
435 if (!have_default && !*argc)
436 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
437
438 nla_nest_end(msg, key);
439 } while (*argc);
440
441 nla_nest_end(msg, keys);
442
443 return 0;
444 nla_put_failure:
445 return -ENOBUFS;
446 explain:
447 fprintf(stderr, "key must be [d:]index:data where\n"
448 " 'd:' means default (transmit) key\n"
449 " 'index:' is a single digit (0-3)\n"
450 " 'data' must be 5 or 13 ascii chars\n"
451 " or 10 or 26 hex digits\n"
452 "for example: d:2:6162636465 is the same as d:2:abcde\n"
453 "or psk:data <AKM Suite> <pairwise CIPHER> <groupwise CIPHER> where\n"
454 " 'data' is the PSK (output of wpa_passphrase and the CIPHER can be CCMP or GCMP\n"
455 "for example: psk:0123456789abcdef PSK CCMP CCMP\n"
456 "The allowed AKM suites are PSK, FT/PSK, PSK/SHA-256\n"
457 "The allowed Cipher suites are TKIP, CCMP, GCMP, GCMP-256, CCMP-256\n");
458 return 2;
459 }
460
461 enum nl80211_chan_width str_to_bw(const char *str)
462 {
463 static const struct {
464 const char *name;
465 unsigned int val;
466 } bwmap[] = {
467 { .name = "5", .val = NL80211_CHAN_WIDTH_5, },
468 { .name = "10", .val = NL80211_CHAN_WIDTH_10, },
469 { .name = "20", .val = NL80211_CHAN_WIDTH_20, },
470 { .name = "40", .val = NL80211_CHAN_WIDTH_40, },
471 { .name = "80", .val = NL80211_CHAN_WIDTH_80, },
472 { .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, },
473 { .name = "160", .val = NL80211_CHAN_WIDTH_160, },
474 };
475 unsigned int i;
476
477 for (i = 0; i < ARRAY_SIZE(bwmap); i++) {
478 if (strcasecmp(bwmap[i].name, str) == 0)
479 return bwmap[i].val;
480 }
481
482 return NL80211_CHAN_WIDTH_20_NOHT;
483 }
484
485 static int parse_freqs(struct chandef *chandef, int argc, char **argv,
486 int *parsed)
487 {
488 uint32_t freq;
489 char *end;
490 bool need_cf1 = false, need_cf2 = false;
491
492 if (argc < 1)
493 return 0;
494
495 chandef->width = str_to_bw(argv[0]);
496
497 switch (chandef->width) {
498 case NL80211_CHAN_WIDTH_20_NOHT:
499 /* First argument was not understood, give up gracefully. */
500 return 0;
501 case NL80211_CHAN_WIDTH_20:
502 case NL80211_CHAN_WIDTH_5:
503 case NL80211_CHAN_WIDTH_10:
504 break;
505 case NL80211_CHAN_WIDTH_80P80:
506 need_cf2 = true;
507 /* fall through */
508 case NL80211_CHAN_WIDTH_40:
509 case NL80211_CHAN_WIDTH_80:
510 case NL80211_CHAN_WIDTH_160:
511 need_cf1 = true;
512 break;
513 case NL80211_CHAN_WIDTH_1:
514 case NL80211_CHAN_WIDTH_2:
515 case NL80211_CHAN_WIDTH_4:
516 case NL80211_CHAN_WIDTH_8:
517 case NL80211_CHAN_WIDTH_16:
518 /* can't happen yet */
519 break;
520 }
521
522 *parsed += 1;
523
524 if (!need_cf1)
525 return 0;
526
527 if (argc < 2)
528 return 1;
529
530 /* center freq 1 */
531 if (!*argv[1])
532 return 1;
533 freq = strtoul(argv[1], &end, 10);
534 if (*end)
535 return 1;
536 *parsed += 1;
537
538 chandef->center_freq1 = freq;
539
540 if (!need_cf2)
541 return 0;
542
543 if (argc < 3)
544 return 1;
545
546 /* center freq 2 */
547 if (!*argv[2])
548 return 1;
549 freq = strtoul(argv[2], &end, 10);
550 if (*end)
551 return 1;
552 chandef->center_freq2 = freq;
553
554 *parsed += 1;
555
556 return 0;
557 }
558
559
560 /**
561 * parse_freqchan - Parse frequency or channel definition
562 *
563 * @chandef: chandef structure to be filled in
564 * @chan: Boolean whether to parse a channel or frequency based specifier
565 * @argc: Number of arguments
566 * @argv: Array of string arguments
567 * @parsed: Pointer to return the number of used arguments, or NULL to error
568 * out if any argument is left unused.
569 *
570 * The given chandef structure will be filled in from the command line
571 * arguments. argc/argv will be updated so that further arguments from the
572 * command line can be parsed.
573 *
574 * Note that despite the fact that the function knows how many center freqs
575 * are needed, there's an ambiguity if the next argument after this is an
576 * integer argument, since the valid channel width values are interpreted
577 * as such, rather than a following argument. This can be avoided by the
578 * user by giving "NOHT" instead.
579 *
580 * The working specifier if chan is set are:
581 * <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
582 *
583 * And if frequency is set:
584 * <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
585 * <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
586 *
587 * If the mode/channel width is not given the NOHT is assumed.
588 *
589 * Return: Number of used arguments, zero or negative error number otherwise
590 */
591 int parse_freqchan(struct chandef *chandef, bool chan, int argc, char **argv,
592 int *parsed)
593 {
594 char *end;
595 static const struct chanmode chanmode[] = {
596 { .name = "HT20",
597 .width = NL80211_CHAN_WIDTH_20,
598 .freq1_diff = 0,
599 .chantype = NL80211_CHAN_HT20 },
600 { .name = "HT40+",
601 .width = NL80211_CHAN_WIDTH_40,
602 .freq1_diff = 10,
603 .chantype = NL80211_CHAN_HT40PLUS },
604 { .name = "HT40-",
605 .width = NL80211_CHAN_WIDTH_40,
606 .freq1_diff = -10,
607 .chantype = NL80211_CHAN_HT40MINUS },
608 { .name = "NOHT",
609 .width = NL80211_CHAN_WIDTH_20_NOHT,
610 .freq1_diff = 0,
611 .chantype = NL80211_CHAN_NO_HT },
612 { .name = "5MHz",
613 .width = NL80211_CHAN_WIDTH_5,
614 .freq1_diff = 0,
615 .chantype = -1 },
616 { .name = "10MHz",
617 .width = NL80211_CHAN_WIDTH_10,
618 .freq1_diff = 0,
619 .chantype = -1 },
620 { .name = "80MHz",
621 .width = NL80211_CHAN_WIDTH_80,
622 .freq1_diff = 0,
623 .chantype = -1 },
624 { .name = "160MHz",
625 .width = NL80211_CHAN_WIDTH_160,
626 .freq1_diff = 0,
627 .chantype = -1 },
628 };
629 const struct chanmode *chanmode_selected = NULL;
630 unsigned int freq;
631 unsigned int i;
632 int _parsed = 0;
633 int res = 0;
634
635 if (argc < 1)
636 return 1;
637
638 if (!argv[0])
639 goto out;
640 freq = strtoul(argv[0], &end, 10);
641 if (*end) {
642 res = 1;
643 goto out;
644 }
645
646 _parsed += 1;
647
648 memset(chandef, 0, sizeof(struct chandef));
649
650 if (chan) {
651 enum nl80211_band band;
652
653 band = freq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
654 freq = ieee80211_channel_to_frequency(freq, band);
655 }
656 chandef->control_freq = freq;
657 /* Assume 20MHz NOHT channel for now. */
658 chandef->center_freq1 = freq;
659
660 /* Try to parse HT mode definitions */
661 if (argc > 1) {
662 for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
663 if (strcasecmp(chanmode[i].name, argv[1]) == 0) {
664 chanmode_selected = &chanmode[i];
665 _parsed += 1;
666 break;
667 }
668 }
669 }
670
671 /* channel mode given, use it and return. */
672 if (chanmode_selected) {
673 chandef->center_freq1 = get_cf1(chanmode_selected, freq);
674 chandef->width = chanmode_selected->width;
675 goto out;
676 }
677
678 /* This was a only a channel definition, nothing further may follow. */
679 if (chan)
680 goto out;
681
682 res = parse_freqs(chandef, argc - 1, argv + 1, &_parsed);
683
684 out:
685 /* Error out if parsed is NULL. */
686 if (!parsed && _parsed != argc)
687 return 1;
688
689 if (parsed)
690 *parsed = _parsed;
691
692 return res;
693 }
694
695 int put_chandef(struct nl_msg *msg, struct chandef *chandef)
696 {
697 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chandef->control_freq);
698 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width);
699
700 switch (chandef->width) {
701 case NL80211_CHAN_WIDTH_20_NOHT:
702 NLA_PUT_U32(msg,
703 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
704 NL80211_CHAN_NO_HT);
705 break;
706 case NL80211_CHAN_WIDTH_20:
707 NLA_PUT_U32(msg,
708 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
709 NL80211_CHAN_HT20);
710 break;
711 case NL80211_CHAN_WIDTH_40:
712 if (chandef->control_freq > chandef->center_freq1)
713 NLA_PUT_U32(msg,
714 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
715 NL80211_CHAN_HT40MINUS);
716 else
717 NLA_PUT_U32(msg,
718 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
719 NL80211_CHAN_HT40PLUS);
720 break;
721 default:
722 break;
723 }
724
725 if (chandef->center_freq1)
726 NLA_PUT_U32(msg,
727 NL80211_ATTR_CENTER_FREQ1,
728 chandef->center_freq1);
729
730 if (chandef->center_freq2)
731 NLA_PUT_U32(msg,
732 NL80211_ATTR_CENTER_FREQ2,
733 chandef->center_freq2);
734
735 return 0;
736
737 nla_put_failure:
738 return -ENOBUFS;
739 }
740
741 static void print_mcs_index(const __u8 *mcs)
742 {
743 int mcs_bit, prev_bit = -2, prev_cont = 0;
744
745 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
746 unsigned int mcs_octet = mcs_bit/8;
747 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
748 bool mcs_rate_idx_set;
749
750 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
751
752 if (!mcs_rate_idx_set)
753 continue;
754
755 if (prev_bit != mcs_bit - 1) {
756 if (prev_bit != -2)
757 printf("%d, ", prev_bit);
758 else
759 printf(" ");
760 printf("%d", mcs_bit);
761 prev_cont = 0;
762 } else if (!prev_cont) {
763 printf("-");
764 prev_cont = 1;
765 }
766
767 prev_bit = mcs_bit;
768 }
769
770 if (prev_cont)
771 printf("%d", prev_bit);
772 printf("\n");
773 }
774
775 /*
776 * There are only 4 possible values, we just use a case instead of computing it,
777 * but technically this can also be computed through the formula:
778 *
779 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
780 */
781 static __u32 compute_ampdu_length(__u8 exponent)
782 {
783 switch (exponent) {
784 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
785 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
786 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
787 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
788 default: return 0;
789 }
790 }
791
792 static const char *print_ampdu_space(__u8 space)
793 {
794 switch (space) {
795 case 0: return "No restriction";
796 case 1: return "1/4 usec";
797 case 2: return "1/2 usec";
798 case 3: return "1 usec";
799 case 4: return "2 usec";
800 case 5: return "4 usec";
801 case 6: return "8 usec";
802 case 7: return "16 usec";
803 default:
804 return "BUG (spacing more than 3 bits!)";
805 }
806 }
807
808 void print_ampdu_length(__u8 exponent)
809 {
810 __u32 max_ampdu_length;
811
812 max_ampdu_length = compute_ampdu_length(exponent);
813
814 if (max_ampdu_length) {
815 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
816 max_ampdu_length, exponent);
817 } else {
818 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
819 "(exponent: %d)\n", exponent);
820 }
821 }
822
823 void print_ampdu_spacing(__u8 spacing)
824 {
825 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
826 print_ampdu_space(spacing), spacing);
827 }
828
829 void print_ht_capability(__u16 cap)
830 {
831 #define PRINT_HT_CAP(_cond, _str) \
832 do { \
833 if (_cond) \
834 printf("\t\t\t" _str "\n"); \
835 } while (0)
836
837 printf("\t\tCapabilities: 0x%02x\n", cap);
838
839 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
840 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
841 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
842
843 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
844 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
845 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
846
847 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
848 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
849 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
850 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
851
852 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
853 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
854 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
855 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
856
857 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
858
859 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
860 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
861
862 /*
863 * For beacons and probe response this would mean the BSS
864 * does or does not allow the usage of DSSS/CCK HT40.
865 * Otherwise it means the STA does or does not use
866 * DSSS/CCK HT40.
867 */
868 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
869 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
870
871 /* BIT(13) is reserved */
872
873 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
874
875 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
876 #undef PRINT_HT_CAP
877 }
878
879 void print_ht_mcs(const __u8 *mcs)
880 {
881 /* As defined in 7.3.2.57.4 Supported MCS Set field */
882 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
883 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
884
885 max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
886 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
887 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
888 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
889 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
890
891 if (max_rx_supp_data_rate)
892 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
893 /* XXX: else see 9.6.0e.5.3 how to get this I think */
894
895 if (tx_mcs_set_defined) {
896 if (tx_mcs_set_equal) {
897 printf("\t\tHT TX/RX MCS rate indexes supported:");
898 print_mcs_index(mcs);
899 } else {
900 printf("\t\tHT RX MCS rate indexes supported:");
901 print_mcs_index(mcs);
902
903 if (tx_unequal_modulation)
904 printf("\t\tTX unequal modulation supported\n");
905 else
906 printf("\t\tTX unequal modulation not supported\n");
907
908 printf("\t\tHT TX Max spatial streams: %d\n",
909 tx_max_num_spatial_streams);
910
911 printf("\t\tHT TX MCS rate indexes supported may differ\n");
912 }
913 } else {
914 printf("\t\tHT RX MCS rate indexes supported:");
915 print_mcs_index(mcs);
916 printf("\t\tHT TX MCS rate indexes are undefined\n");
917 }
918 }
919
920 void print_vht_info(__u32 capa, const __u8 *mcs)
921 {
922 __u16 tmp;
923 int i;
924
925 printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
926
927 #define PRINT_VHT_CAPA(_bit, _str) \
928 do { \
929 if (capa & BIT(_bit)) \
930 printf("\t\t\t" _str "\n"); \
931 } while (0)
932
933 printf("\t\t\tMax MPDU length: ");
934 switch (capa & 3) {
935 case 0: printf("3895\n"); break;
936 case 1: printf("7991\n"); break;
937 case 2: printf("11454\n"); break;
938 case 3: printf("(reserved)\n");
939 }
940 printf("\t\t\tSupported Channel Width: ");
941 switch ((capa >> 2) & 3) {
942 case 0: printf("neither 160 nor 80+80\n"); break;
943 case 1: printf("160 MHz\n"); break;
944 case 2: printf("160 MHz, 80+80 MHz\n"); break;
945 case 3: printf("(reserved)\n");
946 }
947 PRINT_VHT_CAPA(4, "RX LDPC");
948 PRINT_VHT_CAPA(5, "short GI (80 MHz)");
949 PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
950 PRINT_VHT_CAPA(7, "TX STBC");
951 /* RX STBC */
952 PRINT_VHT_CAPA(11, "SU Beamformer");
953 PRINT_VHT_CAPA(12, "SU Beamformee");
954 /* compressed steering */
955 /* # of sounding dimensions */
956 PRINT_VHT_CAPA(19, "MU Beamformer");
957 PRINT_VHT_CAPA(20, "MU Beamformee");
958 PRINT_VHT_CAPA(21, "VHT TXOP PS");
959 PRINT_VHT_CAPA(22, "+HTC-VHT");
960 /* max A-MPDU */
961 /* VHT link adaptation */
962 PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
963 PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
964
965 printf("\t\tVHT RX MCS set:\n");
966 tmp = mcs[0] | (mcs[1] << 8);
967 for (i = 1; i <= 8; i++) {
968 printf("\t\t\t%d streams: ", i);
969 switch ((tmp >> ((i-1)*2) ) & 3) {
970 case 0: printf("MCS 0-7\n"); break;
971 case 1: printf("MCS 0-8\n"); break;
972 case 2: printf("MCS 0-9\n"); break;
973 case 3: printf("not supported\n"); break;
974 }
975 }
976 tmp = mcs[2] | (mcs[3] << 8);
977 printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
978
979 printf("\t\tVHT TX MCS set:\n");
980 tmp = mcs[4] | (mcs[5] << 8);
981 for (i = 1; i <= 8; i++) {
982 printf("\t\t\t%d streams: ", i);
983 switch ((tmp >> ((i-1)*2) ) & 3) {
984 case 0: printf("MCS 0-7\n"); break;
985 case 1: printf("MCS 0-8\n"); break;
986 case 2: printf("MCS 0-9\n"); break;
987 case 3: printf("not supported\n"); break;
988 }
989 }
990 tmp = mcs[6] | (mcs[7] << 8);
991 printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
992 }
993
994 static void __print_he_capa(const __u16 *mac_cap,
995 const __u16 *phy_cap,
996 const __u16 *mcs_set, size_t mcs_len,
997 const __u8 *ppet, int ppet_len,
998 bool indent)
999 {
1000 size_t mcs_used;
1001 int i;
1002 const char *pre = indent ? "\t" : "";
1003
1004 #define PRINT_HE_CAP(_var, _idx, _bit, _str) \
1005 do { \
1006 if (_var[_idx] & BIT(_bit)) \
1007 printf("%s\t\t\t" _str "\n", pre); \
1008 } while (0)
1009
1010 #define PRINT_HE_CAP_MASK(_var, _idx, _shift, _mask, _str) \
1011 do { \
1012 if ((_var[_idx] >> _shift) & _mask) \
1013 printf("%s\t\t\t" _str ": %d\n", pre, (_var[_idx] >> _shift) & _mask); \
1014 } while (0)
1015
1016 #define PRINT_HE_MAC_CAP(...) PRINT_HE_CAP(mac_cap, __VA_ARGS__)
1017 #define PRINT_HE_MAC_CAP_MASK(...) PRINT_HE_CAP_MASK(mac_cap, __VA_ARGS__)
1018 #define PRINT_HE_PHY_CAP(...) PRINT_HE_CAP(phy_cap, __VA_ARGS__)
1019 #define PRINT_HE_PHY_CAP0(_idx, _bit, ...) PRINT_HE_CAP(phy_cap, _idx, _bit + 8, __VA_ARGS__)
1020 #define PRINT_HE_PHY_CAP_MASK(...) PRINT_HE_CAP_MASK(phy_cap, __VA_ARGS__)
1021
1022 printf("%s\t\tHE MAC Capabilities (0x", pre);
1023 for (i = 0; i < 3; i++)
1024 printf("%04x", mac_cap[i]);
1025 printf("):\n");
1026
1027 PRINT_HE_MAC_CAP(0, 0, "+HTC HE Supported");
1028 PRINT_HE_MAC_CAP(0, 1, "TWT Requester");
1029 PRINT_HE_MAC_CAP(0, 2, "TWT Responder");
1030 PRINT_HE_MAC_CAP_MASK(0, 3, 0x3, "Dynamic BA Fragementation Level");
1031 PRINT_HE_MAC_CAP_MASK(0, 5, 0x7, "Maximum number of MSDUS Fragments");
1032 PRINT_HE_MAC_CAP_MASK(0, 8, 0x3, "Minimum Payload size of 128 bytes");
1033 PRINT_HE_MAC_CAP_MASK(0, 10, 0x3, "Trigger Frame MAC Padding Duration");
1034 PRINT_HE_MAC_CAP_MASK(0, 12, 0x7, "Multi-TID Aggregation Support");
1035
1036 PRINT_HE_MAC_CAP(1, 1, "All Ack");
1037 PRINT_HE_MAC_CAP(1, 2, "TRS");
1038 PRINT_HE_MAC_CAP(1, 3, "BSR");
1039 PRINT_HE_MAC_CAP(1, 4, "Broadcast TWT");
1040 PRINT_HE_MAC_CAP(1, 5, "32-bit BA Bitmap");
1041 PRINT_HE_MAC_CAP(1, 6, "MU Cascading");
1042 PRINT_HE_MAC_CAP(1, 7, "Ack-Enabled Aggregation");
1043 PRINT_HE_MAC_CAP(1, 9, "OM Control");
1044 PRINT_HE_MAC_CAP(1, 10, "OFDMA RA");
1045 PRINT_HE_MAC_CAP_MASK(1, 11, 0x3, "Maximum A-MPDU Length Exponent");
1046 PRINT_HE_MAC_CAP(1, 13, "A-MSDU Fragmentation");
1047 PRINT_HE_MAC_CAP(1, 14, "Flexible TWT Scheduling");
1048 PRINT_HE_MAC_CAP(1, 15, "RX Control Frame to MultiBSS");
1049
1050 PRINT_HE_MAC_CAP(2, 0, "BSRP BQRP A-MPDU Aggregation");
1051 PRINT_HE_MAC_CAP(2, 1, "QTP");
1052 PRINT_HE_MAC_CAP(2, 2, "BQR");
1053 PRINT_HE_MAC_CAP(2, 3, "SRP Responder Role");
1054 PRINT_HE_MAC_CAP(2, 4, "NDP Feedback Report");
1055 PRINT_HE_MAC_CAP(2, 5, "OPS");
1056 PRINT_HE_MAC_CAP(2, 6, "A-MSDU in A-MPDU");
1057 PRINT_HE_MAC_CAP_MASK(2, 7, 7, "Multi-TID Aggregation TX");
1058 PRINT_HE_MAC_CAP(2, 10, "HE Subchannel Selective Transmission");
1059 PRINT_HE_MAC_CAP(2, 11, "UL 2x996-Tone RU");
1060 PRINT_HE_MAC_CAP(2, 12, "OM Control UL MU Data Disable RX");
1061
1062 printf("%s\t\tHE PHY Capabilities: (0x", pre);
1063 for (i = 0; i < 11; i++)
1064 printf("%02x", ((__u8 *)phy_cap)[i + 1]);
1065 printf("):\n");
1066
1067 PRINT_HE_PHY_CAP0(0, 1, "HE40/2.4GHz");
1068 PRINT_HE_PHY_CAP0(0, 2, "HE40/HE80/5GHz");
1069 PRINT_HE_PHY_CAP0(0, 3, "HE160/5GHz");
1070 PRINT_HE_PHY_CAP0(0, 4, "HE160/HE80+80/5GHz");
1071 PRINT_HE_PHY_CAP0(0, 5, "242 tone RUs/2.4GHz");
1072 PRINT_HE_PHY_CAP0(0, 6, "242 tone RUs/5GHz");
1073
1074 PRINT_HE_PHY_CAP_MASK(1, 0, 0xf, "Punctured Preamble RX");
1075 PRINT_HE_PHY_CAP_MASK(1, 4, 0x1, "Device Class");
1076 PRINT_HE_PHY_CAP(1, 5, "LDPC Coding in Payload");
1077 PRINT_HE_PHY_CAP(1, 6, "HE SU PPDU with 1x HE-LTF and 0.8us GI");
1078 PRINT_HE_PHY_CAP_MASK(1, 7, 0x3, "Midamble Rx Max NSTS");
1079 PRINT_HE_PHY_CAP(1, 9, "NDP with 4x HE-LTF and 3.2us GI");
1080 PRINT_HE_PHY_CAP(1, 10, "STBC Tx <= 80MHz");
1081 PRINT_HE_PHY_CAP(1, 11, "STBC Rx <= 80MHz");
1082 PRINT_HE_PHY_CAP(1, 12, "Doppler Tx");
1083 PRINT_HE_PHY_CAP(1, 13, "Doppler Rx");
1084 PRINT_HE_PHY_CAP(1, 14, "Full Bandwidth UL MU-MIMO");
1085 PRINT_HE_PHY_CAP(1, 15, "Partial Bandwidth UL MU-MIMO");
1086
1087 PRINT_HE_PHY_CAP_MASK(2, 0, 0x3, "DCM Max Constellation");
1088 PRINT_HE_PHY_CAP_MASK(2, 2, 0x1, "DCM Max NSS Tx");
1089 PRINT_HE_PHY_CAP_MASK(2, 3, 0x3, "DCM Max Constellation Rx");
1090 PRINT_HE_PHY_CAP_MASK(2, 5, 0x1, "DCM Max NSS Rx");
1091 PRINT_HE_PHY_CAP(2, 6, "Rx HE MU PPDU from Non-AP STA");
1092 PRINT_HE_PHY_CAP(2, 7, "SU Beamformer");
1093 PRINT_HE_PHY_CAP(2, 8, "SU Beamformee");
1094 PRINT_HE_PHY_CAP(2, 9, "MU Beamformer");
1095 PRINT_HE_PHY_CAP_MASK(2, 10, 0x7, "Beamformee STS <= 80Mhz");
1096 PRINT_HE_PHY_CAP_MASK(2, 13, 0x7, "Beamformee STS > 80Mhz");
1097
1098 PRINT_HE_PHY_CAP_MASK(3, 0, 0x7, "Sounding Dimensions <= 80Mhz");
1099 PRINT_HE_PHY_CAP_MASK(3, 3, 0x7, "Sounding Dimensions > 80Mhz");
1100 PRINT_HE_PHY_CAP(3, 6, "Ng = 16 SU Feedback");
1101 PRINT_HE_PHY_CAP(3, 7, "Ng = 16 MU Feedback");
1102 PRINT_HE_PHY_CAP(3, 8, "Codebook Size SU Feedback");
1103 PRINT_HE_PHY_CAP(3, 9, "Codebook Size MU Feedback");
1104 PRINT_HE_PHY_CAP(3, 10, "Triggered SU Beamforming Feedback");
1105 PRINT_HE_PHY_CAP(3, 11, "Triggered MU Beamforming Feedback");
1106 PRINT_HE_PHY_CAP(3, 12, "Triggered CQI Feedback");
1107 PRINT_HE_PHY_CAP(3, 13, "Partial Bandwidth Extended Range");
1108 PRINT_HE_PHY_CAP(3, 14, "Partial Bandwidth DL MU-MIMO");
1109 PRINT_HE_PHY_CAP(3, 15, "PPE Threshold Present");
1110
1111 PRINT_HE_PHY_CAP(4, 0, "SRP-based SR");
1112 PRINT_HE_PHY_CAP(4, 1, "Power Boost Factor ar");
1113 PRINT_HE_PHY_CAP(4, 2, "HE SU PPDU & HE PPDU 4x HE-LTF 0.8us GI");
1114 PRINT_HE_PHY_CAP_MASK(4, 3, 0x7, "Max NC");
1115 PRINT_HE_PHY_CAP(4, 6, "STBC Tx > 80MHz");
1116 PRINT_HE_PHY_CAP(4, 7, "STBC Rx > 80MHz");
1117 PRINT_HE_PHY_CAP(4, 8, "HE ER SU PPDU 4x HE-LTF 0.8us GI");
1118 PRINT_HE_PHY_CAP(4, 9, "20MHz in 40MHz HE PPDU 2.4GHz");
1119 PRINT_HE_PHY_CAP(4, 10, "20MHz in 160/80+80MHz HE PPDU");
1120 PRINT_HE_PHY_CAP(4, 11, "80MHz in 160/80+80MHz HE PPDU");
1121 PRINT_HE_PHY_CAP(4, 12, "HE ER SU PPDU 1x HE-LTF 0.8us GI");
1122 PRINT_HE_PHY_CAP(4, 13, "Midamble Rx 2x & 1x HE-LTF");
1123 PRINT_HE_PHY_CAP_MASK(4, 14, 0x3, "DCM Max BW");
1124
1125 PRINT_HE_PHY_CAP(5, 0, "Longer Than 16HE SIG-B OFDM Symbols");
1126 PRINT_HE_PHY_CAP(5, 1, "Non-Triggered CQI Feedback");
1127 PRINT_HE_PHY_CAP(5, 2, "TX 1024-QAM");
1128 PRINT_HE_PHY_CAP(5, 3, "RX 1024-QAM");
1129 PRINT_HE_PHY_CAP(5, 4, "RX Full BW SU Using HE MU PPDU with Compression SIGB");
1130 PRINT_HE_PHY_CAP(5, 5, "RX Full BW SU Using HE MU PPDU with Non-Compression SIGB");
1131
1132 mcs_used = 0;
1133 for (i = 0; i < 3; i++) {
1134 __u8 phy_cap_support[] = { BIT(1) | BIT(2), BIT(3), BIT(4) };
1135 char *bw[] = { "<= 80", "160", "80+80" };
1136 int j;
1137
1138 if ((phy_cap[0] & (phy_cap_support[i] << 8)) == 0)
1139 continue;
1140
1141 /* Supports more, but overflow? Abort. */
1142 if ((i * 2 + 2) * sizeof(mcs_set[0]) >= mcs_len)
1143 return;
1144
1145 for (j = 0; j < 2; j++) {
1146 int k;
1147 printf("%s\t\tHE %s MCS and NSS set %s MHz\n", pre, j ? "TX" : "RX", bw[i]);
1148 for (k = 0; k < 8; k++) {
1149 __u16 mcs = mcs_set[(i * 2) + j];
1150 mcs >>= k * 2;
1151 mcs &= 0x3;
1152 printf("%s\t\t\t%d streams: ", pre, k + 1);
1153 if (mcs == 3)
1154 printf("not supported\n");
1155 else
1156 printf("MCS 0-%d\n", 7 + (mcs * 2));
1157 }
1158
1159 }
1160 mcs_used += 2 * sizeof(mcs_set[0]);
1161 }
1162
1163 /* Caller didn't provide ppet; infer it, if there's trailing space. */
1164 if (!ppet) {
1165 ppet = (const void *)((const __u8 *)mcs_set + mcs_used);
1166 if (mcs_used < mcs_len)
1167 ppet_len = mcs_len - mcs_used;
1168 else
1169 ppet_len = 0;
1170 }
1171
1172 if (ppet_len && (phy_cap[3] & BIT(15))) {
1173 printf("%s\t\tPPE Threshold ", pre);
1174 for (i = 0; i < ppet_len; i++)
1175 if (ppet[i])
1176 printf("0x%02x ", ppet[i]);
1177 printf("\n");
1178 }
1179 }
1180
1181 void print_iftype_list(const char *name, const char *pfx, struct nlattr *attr)
1182 {
1183 struct nlattr *ift;
1184 int rem;
1185
1186 printf("%s:\n", name);
1187 nla_for_each_nested(ift, attr, rem)
1188 printf("%s * %s\n", pfx, iftype_name(nla_type(ift)));
1189 }
1190
1191 void print_iftype_line(struct nlattr *attr)
1192 {
1193 struct nlattr *ift;
1194 bool first = true;
1195 int rem;
1196
1197 nla_for_each_nested(ift, attr, rem) {
1198 if (first)
1199 first = false;
1200 else
1201 printf(", ");
1202 printf("%s", iftype_name(nla_type(ift)));
1203 }
1204 }
1205
1206 void print_he_info(struct nlattr *nl_iftype)
1207 {
1208 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
1209 __u16 mac_cap[3] = { 0 };
1210 __u16 phy_cap[6] = { 0 };
1211 __u16 mcs_set[6] = { 0 };
1212 __u8 ppet[25] = { 0 };
1213 size_t len;
1214 int mcs_len = 0, ppet_len = 0;
1215
1216 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
1217 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
1218
1219 if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
1220 return;
1221
1222 printf("\t\tHE Iftypes: ");
1223 print_iftype_line(tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES]);
1224 printf("\n");
1225
1226 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]) {
1227 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]);
1228 if (len > sizeof(mac_cap))
1229 len = sizeof(mac_cap);
1230 memcpy(mac_cap,
1231 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]),
1232 len);
1233 }
1234
1235 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1236 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1237
1238 if (len > sizeof(phy_cap) - 1)
1239 len = sizeof(phy_cap) - 1;
1240 memcpy(&((__u8 *)phy_cap)[1],
1241 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1242 len);
1243 }
1244
1245 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]) {
1246 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]);
1247 if (len > sizeof(mcs_set))
1248 len = sizeof(mcs_set);
1249 memcpy(mcs_set,
1250 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]),
1251 len);
1252 mcs_len = len;
1253 }
1254
1255 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]) {
1256 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]);
1257 if (len > sizeof(ppet))
1258 len = sizeof(ppet);
1259 memcpy(ppet,
1260 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]),
1261 len);
1262 ppet_len = len;
1263 }
1264
1265 __print_he_capa(mac_cap, phy_cap, mcs_set, mcs_len, ppet, ppet_len,
1266 true);
1267 }
1268
1269 void print_he_capability(const uint8_t *ie, int len)
1270 {
1271 const void *mac_cap, *phy_cap, *mcs_set;
1272 int mcs_len;
1273 int i = 0;
1274
1275 mac_cap = &ie[i];
1276 i += 6;
1277
1278 phy_cap = &ie[i];
1279 i += 11;
1280
1281 mcs_set = &ie[i];
1282 mcs_len = len - i;
1283
1284 __print_he_capa(mac_cap, phy_cap - 1, mcs_set, mcs_len, NULL, 0, false);
1285 }
1286
1287 void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
1288 {
1289 size_t i;
1290
1291 printf("%s: ", prefix);
1292 for (i = 0; i < size; i++) {
1293 if (i && i % 16 == 0)
1294 printf("\n%s: ", prefix);
1295 printf("%02x ", buf[i]);
1296 }
1297 printf("\n\n");
1298 }
1299
1300 int get_cf1(const struct chanmode *chanmode, unsigned long freq)
1301 {
1302 unsigned int cf1 = freq, j;
1303 unsigned int bw80[] = { 5180, 5260, 5500, 5580, 5660, 5745,
1304 5955, 6035, 6115, 6195, 6275, 6355,
1305 6435, 6515, 6595, 6675, 6755, 6835,
1306 6195, 6995 };
1307 unsigned int vht160[] = { 5180, 5500 };
1308
1309 switch (chanmode->width) {
1310 case NL80211_CHAN_WIDTH_80:
1311 /* setup center_freq1 */
1312 for (j = 0; j < ARRAY_SIZE(bw80); j++) {
1313 if (freq >= bw80[j] && freq < bw80[j] + 80)
1314 break;
1315 }
1316
1317 if (j == ARRAY_SIZE(bw80))
1318 break;
1319
1320 cf1 = bw80[j] + 30;
1321 break;
1322 case NL80211_CHAN_WIDTH_160:
1323 /* setup center_freq1 */
1324 for (j = 0; j < ARRAY_SIZE(vht160); j++) {
1325 if (freq >= vht160[j] && freq < vht160[j] + 160)
1326 break;
1327 }
1328
1329 if (j == ARRAY_SIZE(vht160))
1330 break;
1331
1332 cf1 = vht160[j] + 70;
1333 break;
1334 default:
1335 cf1 = freq + chanmode->freq1_diff;
1336 break;
1337 }
1338
1339 return cf1;
1340 }
1341
1342 int parse_random_mac_addr(struct nl_msg *msg, char *addrs)
1343 {
1344 char *a_addr, *a_mask, *sep;
1345 unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
1346
1347 if (!*addrs) {
1348 /* randomise all but the multicast bit */
1349 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN,
1350 "\x00\x00\x00\x00\x00\x00");
1351 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
1352 "\x01\x00\x00\x00\x00\x00");
1353 return 0;
1354 }
1355
1356 if (*addrs != '=')
1357 return 1;
1358
1359 addrs++;
1360 sep = strchr(addrs, '/');
1361 a_addr = addrs;
1362
1363 if (!sep)
1364 return 1;
1365
1366 *sep = 0;
1367 a_mask = sep + 1;
1368 if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
1369 return 1;
1370
1371 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1372 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
1373
1374 return 0;
1375 nla_put_failure:
1376 return -ENOBUFS;
1377 }