]> git.ipfire.org Git - thirdparty/iw.git/blame - util.c
iw: fix android build
[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
febeb0c0 8void mac_addr_n2a(char *mac_addr, 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",
541ef425
JB
136};
137
138static char modebuf[100];
139
140const char *iftype_name(enum nl80211_iftype iftype)
141{
a66b3a35 142 if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
541ef425
JB
143 return ifmodes[iftype];
144 sprintf(modebuf, "Unknown mode (%d)", iftype);
145 return modebuf;
146}
379f8397 147
9990c1e9 148static const char *commands[NL80211_CMD_MAX + 1] = {
02aef469
JB
149/*
150 * sed 's/^\tNL80211_CMD_//;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h
151 */
152 [NL80211_CMD_UNSPEC] = "unspec",
06339499
JB
153 [NL80211_CMD_GET_WIPHY] = "get_wiphy",
154 [NL80211_CMD_SET_WIPHY] = "set_wiphy",
155 [NL80211_CMD_NEW_WIPHY] = "new_wiphy",
156 [NL80211_CMD_DEL_WIPHY] = "del_wiphy",
157 [NL80211_CMD_GET_INTERFACE] = "get_interface",
158 [NL80211_CMD_SET_INTERFACE] = "set_interface",
159 [NL80211_CMD_NEW_INTERFACE] = "new_interface",
160 [NL80211_CMD_DEL_INTERFACE] = "del_interface",
161 [NL80211_CMD_GET_KEY] = "get_key",
162 [NL80211_CMD_SET_KEY] = "set_key",
163 [NL80211_CMD_NEW_KEY] = "new_key",
164 [NL80211_CMD_DEL_KEY] = "del_key",
165 [NL80211_CMD_GET_BEACON] = "get_beacon",
166 [NL80211_CMD_SET_BEACON] = "set_beacon",
02aef469
JB
167 [NL80211_CMD_START_AP] = "start_ap",
168 [NL80211_CMD_STOP_AP] = "stop_ap",
06339499
JB
169 [NL80211_CMD_GET_STATION] = "get_station",
170 [NL80211_CMD_SET_STATION] = "set_station",
171 [NL80211_CMD_NEW_STATION] = "new_station",
172 [NL80211_CMD_DEL_STATION] = "del_station",
173 [NL80211_CMD_GET_MPATH] = "get_mpath",
174 [NL80211_CMD_SET_MPATH] = "set_mpath",
175 [NL80211_CMD_NEW_MPATH] = "new_mpath",
176 [NL80211_CMD_DEL_MPATH] = "del_mpath",
177 [NL80211_CMD_SET_BSS] = "set_bss",
178 [NL80211_CMD_SET_REG] = "set_reg",
02aef469
JB
179 [NL80211_CMD_REQ_SET_REG] = "req_set_reg",
180 [NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config",
181 [NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config",
06339499
JB
182 [NL80211_CMD_GET_REG] = "get_reg",
183 [NL80211_CMD_GET_SCAN] = "get_scan",
184 [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan",
185 [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results",
186 [NL80211_CMD_SCAN_ABORTED] = "scan_aborted",
187 [NL80211_CMD_REG_CHANGE] = "reg_change",
188 [NL80211_CMD_AUTHENTICATE] = "authenticate",
189 [NL80211_CMD_ASSOCIATE] = "associate",
190 [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate",
191 [NL80211_CMD_DISASSOCIATE] = "disassociate",
192 [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure",
193 [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint",
194 [NL80211_CMD_JOIN_IBSS] = "join_ibss",
195 [NL80211_CMD_LEAVE_IBSS] = "leave_ibss",
196 [NL80211_CMD_TESTMODE] = "testmode",
197 [NL80211_CMD_CONNECT] = "connect",
198 [NL80211_CMD_ROAM] = "roam",
199 [NL80211_CMD_DISCONNECT] = "disconnect",
200 [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns",
201 [NL80211_CMD_GET_SURVEY] = "get_survey",
02aef469 202 [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results",
06339499
JB
203 [NL80211_CMD_SET_PMKSA] = "set_pmksa",
204 [NL80211_CMD_DEL_PMKSA] = "del_pmksa",
205 [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa",
206 [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel",
207 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel",
208 [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask",
02aef469
JB
209 [NL80211_CMD_REGISTER_FRAME] = "register_frame",
210 [NL80211_CMD_FRAME] = "frame",
211 [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status",
212 [NL80211_CMD_SET_POWER_SAVE] = "set_power_save",
213 [NL80211_CMD_GET_POWER_SAVE] = "get_power_save",
214 [NL80211_CMD_SET_CQM] = "set_cqm",
215 [NL80211_CMD_NOTIFY_CQM] = "notify_cqm",
7b96e9e2 216 [NL80211_CMD_SET_CHANNEL] = "set_channel",
dcf57038
JB
217 [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer",
218 [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel",
219 [NL80211_CMD_JOIN_MESH] = "join_mesh",
220 [NL80211_CMD_LEAVE_MESH] = "leave_mesh",
02aef469
JB
221 [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate",
222 [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate",
223 [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate",
224 [NL80211_CMD_GET_WOWLAN] = "get_wowlan",
225 [NL80211_CMD_SET_WOWLAN] = "set_wowlan",
226 [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan",
227 [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan",
228 [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results",
229 [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped",
850ab15d 230 [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload",
02aef469
JB
231 [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate",
232 [NL80211_CMD_TDLS_OPER] = "tdls_oper",
233 [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt",
234 [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame",
235 [NL80211_CMD_PROBE_CLIENT] = "probe_client",
236 [NL80211_CMD_REGISTER_BEACONS] = "register_beacons",
237 [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame",
238 [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map",
239 [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify",
65d07957
JB
240 [NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device",
241 [NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device",
242 [NL80211_CMD_CONN_FAILED] = "conn_failed",
240aa0ef
JB
243 [NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate",
244 [NL80211_CMD_SET_MAC_ACL] = "set_mac_acl",
245 [NL80211_CMD_RADAR_DETECT] = "radar_detect",
246 [NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features",
247 [NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies",
248 [NL80211_CMD_FT_EVENT] = "ft_event",
249 [NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start",
250 [NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop",
0624c45d
JB
251 [NL80211_CMD_GET_COALESCE] = "get_coalesce",
252 [NL80211_CMD_SET_COALESCE] = "set_coalesce",
253 [NL80211_CMD_CHANNEL_SWITCH] = "channel_switch",
254 [NL80211_CMD_VENDOR] = "vendor",
b8820d2b
JB
255 [NL80211_CMD_SET_QOS_MAP] = "set_qos_map",
256 [NL80211_CMD_ADD_TX_TS] = "add_tx_ts",
257 [NL80211_CMD_DEL_TX_TS] = "del_tx_ts",
258 [NL80211_CMD_GET_MPP] = "get_mpp",
259 [NL80211_CMD_JOIN_OCB] = "join_ocb",
260 [NL80211_CMD_LEAVE_OCB] = "leave_ocb",
261 [NL80211_CMD_CH_SWITCH_STARTED_NOTIFY] = "ch_switch_started_notify",
9990c1e9
MH
262};
263
264static char cmdbuf[100];
265
266const char *command_name(enum nl80211_commands cmd)
267{
73780397 268 if (cmd <= NL80211_CMD_MAX && commands[cmd])
9990c1e9
MH
269 return commands[cmd];
270 sprintf(cmdbuf, "Unknown command (%d)", cmd);
271 return cmdbuf;
272}
273
58b46da2 274int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
379f8397 275{
58b46da2
BR
276 /* see 802.11 17.3.8.3.2 and Annex J
277 * there are overlapping channel numbers in 5GHz and 2GHz bands */
278 if (chan <= 0)
279 return 0; /* not supported */
280 switch (band) {
281 case NL80211_BAND_2GHZ:
282 if (chan == 14)
283 return 2484;
284 else if (chan < 14)
285 return 2407 + chan * 5;
286 break;
287 case NL80211_BAND_5GHZ:
288 if (chan >= 182 && chan <= 196)
289 return 4000 + chan * 5;
290 else
291 return 5000 + chan * 5;
292 break;
293 case NL80211_BAND_60GHZ:
294 if (chan < 5)
295 return 56160 + chan * 2160;
296 break;
297 default:
298 ;
299 }
300 return 0; /* not supported */
379f8397
JB
301}
302
303int ieee80211_frequency_to_channel(int freq)
304{
58b46da2 305 /* see 802.11-2007 17.3.8.3.2 and Annex J */
379f8397
JB
306 if (freq == 2484)
307 return 14;
58b46da2 308 else if (freq < 2484)
379f8397 309 return (freq - 2407) / 5;
58b46da2
BR
310 else if (freq >= 4910 && freq <= 4980)
311 return (freq - 4000) / 5;
312 else if (freq <= 45000) /* DMG band lower limit */
313 return (freq - 5000) / 5;
314 else if (freq >= 58320 && freq <= 64800)
d56e86bc 315 return (freq - 56160) / 2160;
58b46da2
BR
316 else
317 return 0;
379f8397 318}
748f8489
JB
319
320void print_ssid_escaped(const uint8_t len, const uint8_t *data)
321{
322 int i;
323
324 for (i = 0; i < len; i++) {
3f612733 325 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
748f8489 326 printf("%c", data[i]);
3f612733
JB
327 else if (data[i] == ' ' &&
328 (i != 0 && i != len -1))
329 printf(" ");
748f8489
JB
330 else
331 printf("\\x%.2x", data[i]);
332 }
333}
51e9bd80
JB
334
335static int hex2num(char digit)
336{
337 if (!isxdigit(digit))
338 return -1;
339 if (isdigit(digit))
340 return digit - '0';
341 return tolower(digit) - 'a' + 10;
342}
343
344static int hex2byte(char *hex)
345{
346 int d1, d2;
347
348 d1 = hex2num(hex[0]);
349 if (d1 < 0)
350 return -1;
351 d2 = hex2num(hex[1]);
352 if (d2 < 0)
353 return -1;
354 return (d1 << 4) | d2;
355}
356
357static char *hex2bin(char *hex, char *buf)
358{
359 char *result = buf;
360 int d;
361
362 while (hex[0]) {
363 d = hex2byte(hex);
364 if (d < 0)
365 return NULL;
366 buf[0] = d;
367 buf++;
368 hex += 2;
369 }
370
371 return result;
372}
373
374int parse_keys(struct nl_msg *msg, char **argv, int argc)
375{
376 struct nlattr *keys;
377 int i = 0;
041581ce 378 bool have_default = false;
51e9bd80
JB
379 char keybuf[13];
380
381 if (!argc)
382 return 1;
383
384 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
385
386 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
387 if (!keys)
388 return -ENOBUFS;
389
390 do {
391 char *arg = *argv;
392 int pos = 0, keylen;
393 struct nlattr *key = nla_nest_start(msg, ++i);
394 char *keydata;
395
396 if (!key)
397 return -ENOBUFS;
398
399 if (arg[pos] == 'd') {
400 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
401 pos++;
402 if (arg[pos] == ':')
403 pos++;
041581ce 404 have_default = true;
51e9bd80
JB
405 }
406
407 if (!isdigit(arg[pos]))
408 goto explain;
409 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
410 if (arg[pos++] != ':')
411 goto explain;
412 keydata = arg + pos;
413 switch (strlen(keydata)) {
414 case 10:
415 keydata = hex2bin(keydata, keybuf);
416 case 5:
417 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
418 keylen = 5;
419 break;
420 case 26:
421 keydata = hex2bin(keydata, keybuf);
422 case 13:
423 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
424 keylen = 13;
425 break;
426 default:
427 goto explain;
428 }
429
430 if (!keydata)
431 goto explain;
432
433 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
434
51e9bd80
JB
435 argv++;
436 argc--;
041581ce
JB
437
438 /* one key should be TX key */
439 if (!have_default && !argc)
440 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
441
442 nla_nest_end(msg, key);
51e9bd80
JB
443 } while (argc);
444
445 nla_nest_end(msg, keys);
446
447 return 0;
448 nla_put_failure:
449 return -ENOBUFS;
450 explain:
451 fprintf(stderr, "key must be [d:]index:data where\n"
452 " 'd:' means default (transmit) key\n"
453 " 'index:' is a single digit (0-3)\n"
454 " 'data' must be 5 or 13 ascii chars\n"
455 " or 10 or 26 hex digits\n"
456 "for example: d:2:6162636465 is the same as d:2:abcde\n");
457 return 2;
458}
deb3501c 459
7ddfb679 460static void print_mcs_index(const __u8 *mcs)
deb3501c 461{
9fea9777 462 int mcs_bit, prev_bit = -2, prev_cont = 0;
04953e90
JB
463
464 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
465 unsigned int mcs_octet = mcs_bit/8;
466 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
467 bool mcs_rate_idx_set;
468
469 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
470
471 if (!mcs_rate_idx_set)
472 continue;
473
474 if (prev_bit != mcs_bit - 1) {
475 if (prev_bit != -2)
476 printf("%d, ", prev_bit);
477 else
478 printf(" ");
479 printf("%d", mcs_bit);
480 prev_cont = 0;
481 } else if (!prev_cont) {
482 printf("-");
483 prev_cont = 1;
484 }
485
486 prev_bit = mcs_bit;
487 }
deb3501c 488
04953e90
JB
489 if (prev_cont)
490 printf("%d", prev_bit);
491 printf("\n");
deb3501c 492}
0950993f
LR
493
494/*
495 * There are only 4 possible values, we just use a case instead of computing it,
496 * but technically this can also be computed through the formula:
497 *
498 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
499 */
500static __u32 compute_ampdu_length(__u8 exponent)
501{
502 switch (exponent) {
503 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
504 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
505 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
506 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
507 default: return 0;
508 }
509}
510
511static const char *print_ampdu_space(__u8 space)
512{
513 switch (space) {
514 case 0: return "No restriction";
515 case 1: return "1/4 usec";
516 case 2: return "1/2 usec";
517 case 3: return "1 usec";
518 case 4: return "2 usec";
519 case 5: return "4 usec";
520 case 6: return "8 usec";
521 case 7: return "16 usec";
522 default:
7ae93cd5 523 return "BUG (spacing more than 3 bits!)";
0950993f
LR
524 }
525}
526
527void print_ampdu_length(__u8 exponent)
528{
04953e90 529 __u32 max_ampdu_length;
0950993f
LR
530
531 max_ampdu_length = compute_ampdu_length(exponent);
532
533 if (max_ampdu_length) {
534 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
535 max_ampdu_length, exponent);
3f362f8b 536 } else {
0950993f
LR
537 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
538 "(exponent: %d)\n", exponent);
539 }
540}
541
542void print_ampdu_spacing(__u8 spacing)
543{
3f362f8b
NB
544 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
545 print_ampdu_space(spacing), spacing);
0950993f 546}
357c1a5d
LR
547
548void print_ht_capability(__u16 cap)
549{
550#define PRINT_HT_CAP(_cond, _str) \
551 do { \
552 if (_cond) \
553 printf("\t\t\t" _str "\n"); \
554 } while (0)
555
556 printf("\t\tCapabilities: 0x%02x\n", cap);
557
028c0de5 558 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
357c1a5d
LR
559 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
560 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
561
562 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
563 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
564 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
565
566 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
567 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
568 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
569 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
570
571 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
572 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
573 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
574 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
575
576 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
577
c79c7464
CL
578 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
579 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
357c1a5d
LR
580
581 /*
582 * For beacons and probe response this would mean the BSS
583 * does or does not allow the usage of DSSS/CCK HT40.
584 * Otherwise it means the STA does or does not use
585 * DSSS/CCK HT40.
586 */
587 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
588 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
589
590 /* BIT(13) is reserved */
591
592 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
593
594 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
595#undef PRINT_HT_CAP
596}
7ddfb679
JB
597
598void print_ht_mcs(const __u8 *mcs)
599{
600 /* As defined in 7.3.2.57.4 Supported MCS Set field */
601 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
602 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
603
5ba6a62b 604 max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
7ddfb679
JB
605 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
606 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
607 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
608 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
609
610 if (max_rx_supp_data_rate)
611 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
612 /* XXX: else see 9.6.0e.5.3 how to get this I think */
613
614 if (tx_mcs_set_defined) {
615 if (tx_mcs_set_equal) {
2a79feb0 616 printf("\t\tHT TX/RX MCS rate indexes supported:");
7ddfb679
JB
617 print_mcs_index(mcs);
618 } else {
619 printf("\t\tHT RX MCS rate indexes supported:");
620 print_mcs_index(mcs);
621
622 if (tx_unequal_modulation)
623 printf("\t\tTX unequal modulation supported\n");
624 else
625 printf("\t\tTX unequal modulation not supported\n");
626
627 printf("\t\tHT TX Max spatial streams: %d\n",
628 tx_max_num_spatial_streams);
629
630 printf("\t\tHT TX MCS rate indexes supported may differ\n");
631 }
632 } else {
633 printf("\t\tHT RX MCS rate indexes supported:");
634 print_mcs_index(mcs);
089bb35d 635 printf("\t\tHT TX MCS rate indexes are undefined\n");
7ddfb679
JB
636 }
637}
54eb1613
JB
638
639void print_vht_info(__u32 capa, const __u8 *mcs)
640{
641 __u16 tmp;
642 int i;
643
644 printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
645
646#define PRINT_VHT_CAPA(_bit, _str) \
647 do { \
648 if (capa & BIT(_bit)) \
649 printf("\t\t\t" _str "\n"); \
650 } while (0)
651
652 printf("\t\t\tMax MPDU length: ");
653 switch (capa & 3) {
654 case 0: printf("3895\n"); break;
655 case 1: printf("7991\n"); break;
656 case 2: printf("11454\n"); break;
657 case 3: printf("(reserved)\n");
658 }
659 printf("\t\t\tSupported Channel Width: ");
660 switch ((capa >> 2) & 3) {
661 case 0: printf("neither 160 nor 80+80\n"); break;
662 case 1: printf("160 MHz\n"); break;
663 case 2: printf("160 MHz, 80+80 MHz\n"); break;
664 case 3: printf("(reserved)\n");
665 }
666 PRINT_VHT_CAPA(4, "RX LDPC");
667 PRINT_VHT_CAPA(5, "short GI (80 MHz)");
668 PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
669 PRINT_VHT_CAPA(7, "TX STBC");
670 /* RX STBC */
671 PRINT_VHT_CAPA(11, "SU Beamformer");
672 PRINT_VHT_CAPA(12, "SU Beamformee");
673 /* compressed steering */
674 /* # of sounding dimensions */
675 PRINT_VHT_CAPA(19, "MU Beamformer");
676 PRINT_VHT_CAPA(20, "MU Beamformee");
677 PRINT_VHT_CAPA(21, "VHT TXOP PS");
678 PRINT_VHT_CAPA(22, "+HTC-VHT");
679 /* max A-MPDU */
680 /* VHT link adaptation */
75271051
MB
681 PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
682 PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
54eb1613
JB
683
684 printf("\t\tVHT RX MCS set:\n");
685 tmp = mcs[0] | (mcs[1] << 8);
686 for (i = 1; i <= 8; i++) {
687 printf("\t\t\t%d streams: ", i);
688 switch ((tmp >> ((i-1)*2) ) & 3) {
689 case 0: printf("MCS 0-7\n"); break;
690 case 1: printf("MCS 0-8\n"); break;
691 case 2: printf("MCS 0-9\n"); break;
692 case 3: printf("not supported\n"); break;
693 }
694 }
695 tmp = mcs[2] | (mcs[3] << 8);
696 printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
697
698 printf("\t\tVHT TX MCS set:\n");
699 tmp = mcs[4] | (mcs[5] << 8);
700 for (i = 1; i <= 8; i++) {
701 printf("\t\t\t%d streams: ", i);
702 switch ((tmp >> ((i-1)*2) ) & 3) {
703 case 0: printf("MCS 0-7\n"); break;
704 case 1: printf("MCS 0-8\n"); break;
705 case 2: printf("MCS 0-9\n"); break;
706 case 3: printf("not supported\n"); break;
707 }
708 }
709 tmp = mcs[6] | (mcs[7] << 8);
710 printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
711}
492354de
JD
712
713void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
714{
715 int i;
716
717 printf("%s: ", prefix);
718 for (i = 0; i < size; i++) {
719 if (i && i % 16 == 0)
720 printf("\n%s: ", prefix);
721 printf("%02x ", buf[i]);
722 }
723 printf("\n\n");
724}