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