• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,const unsigned char * arg)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 
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 	"NAN",
138 };
139 
140 static char modebuf[100];
141 
iftype_name(enum nl80211_iftype iftype)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 /*
152  * sed 's/^\tNL80211_CMD_//;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h
153  */
154 	[NL80211_CMD_UNSPEC] = "unspec",
155 	[NL80211_CMD_GET_WIPHY] = "get_wiphy",
156 	[NL80211_CMD_SET_WIPHY] = "set_wiphy",
157 	[NL80211_CMD_NEW_WIPHY] = "new_wiphy",
158 	[NL80211_CMD_DEL_WIPHY] = "del_wiphy",
159 	[NL80211_CMD_GET_INTERFACE] = "get_interface",
160 	[NL80211_CMD_SET_INTERFACE] = "set_interface",
161 	[NL80211_CMD_NEW_INTERFACE] = "new_interface",
162 	[NL80211_CMD_DEL_INTERFACE] = "del_interface",
163 	[NL80211_CMD_GET_KEY] = "get_key",
164 	[NL80211_CMD_SET_KEY] = "set_key",
165 	[NL80211_CMD_NEW_KEY] = "new_key",
166 	[NL80211_CMD_DEL_KEY] = "del_key",
167 	[NL80211_CMD_GET_BEACON] = "get_beacon",
168 	[NL80211_CMD_SET_BEACON] = "set_beacon",
169 	[NL80211_CMD_START_AP] = "start_ap",
170 	[NL80211_CMD_STOP_AP] = "stop_ap",
171 	[NL80211_CMD_GET_STATION] = "get_station",
172 	[NL80211_CMD_SET_STATION] = "set_station",
173 	[NL80211_CMD_NEW_STATION] = "new_station",
174 	[NL80211_CMD_DEL_STATION] = "del_station",
175 	[NL80211_CMD_GET_MPATH] = "get_mpath",
176 	[NL80211_CMD_SET_MPATH] = "set_mpath",
177 	[NL80211_CMD_NEW_MPATH] = "new_mpath",
178 	[NL80211_CMD_DEL_MPATH] = "del_mpath",
179 	[NL80211_CMD_SET_BSS] = "set_bss",
180 	[NL80211_CMD_SET_REG] = "set_reg",
181 	[NL80211_CMD_REQ_SET_REG] = "req_set_reg",
182 	[NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config",
183 	[NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config",
184 	[NL80211_CMD_SET_MGMT_EXTRA_IE /* reserved; not used */] = "set_mgmt_extra_ie /* reserved; not used */",
185 	[NL80211_CMD_GET_REG] = "get_reg",
186 	[NL80211_CMD_GET_SCAN] = "get_scan",
187 	[NL80211_CMD_TRIGGER_SCAN] = "trigger_scan",
188 	[NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results",
189 	[NL80211_CMD_SCAN_ABORTED] = "scan_aborted",
190 	[NL80211_CMD_REG_CHANGE] = "reg_change",
191 	[NL80211_CMD_AUTHENTICATE] = "authenticate",
192 	[NL80211_CMD_ASSOCIATE] = "associate",
193 	[NL80211_CMD_DEAUTHENTICATE] = "deauthenticate",
194 	[NL80211_CMD_DISASSOCIATE] = "disassociate",
195 	[NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure",
196 	[NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint",
197 	[NL80211_CMD_JOIN_IBSS] = "join_ibss",
198 	[NL80211_CMD_LEAVE_IBSS] = "leave_ibss",
199 	[NL80211_CMD_TESTMODE] = "testmode",
200 	[NL80211_CMD_CONNECT] = "connect",
201 	[NL80211_CMD_ROAM] = "roam",
202 	[NL80211_CMD_DISCONNECT] = "disconnect",
203 	[NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns",
204 	[NL80211_CMD_GET_SURVEY] = "get_survey",
205 	[NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results",
206 	[NL80211_CMD_SET_PMKSA] = "set_pmksa",
207 	[NL80211_CMD_DEL_PMKSA] = "del_pmksa",
208 	[NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa",
209 	[NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel",
210 	[NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel",
211 	[NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask",
212 	[NL80211_CMD_REGISTER_FRAME] = "register_frame",
213 	[NL80211_CMD_FRAME] = "frame",
214 	[NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status",
215 	[NL80211_CMD_SET_POWER_SAVE] = "set_power_save",
216 	[NL80211_CMD_GET_POWER_SAVE] = "get_power_save",
217 	[NL80211_CMD_SET_CQM] = "set_cqm",
218 	[NL80211_CMD_NOTIFY_CQM] = "notify_cqm",
219 	[NL80211_CMD_SET_CHANNEL] = "set_channel",
220 	[NL80211_CMD_SET_WDS_PEER] = "set_wds_peer",
221 	[NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel",
222 	[NL80211_CMD_JOIN_MESH] = "join_mesh",
223 	[NL80211_CMD_LEAVE_MESH] = "leave_mesh",
224 	[NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate",
225 	[NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate",
226 	[NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate",
227 	[NL80211_CMD_GET_WOWLAN] = "get_wowlan",
228 	[NL80211_CMD_SET_WOWLAN] = "set_wowlan",
229 	[NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan",
230 	[NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan",
231 	[NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results",
232 	[NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped",
233 	[NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload",
234 	[NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate",
235 	[NL80211_CMD_TDLS_OPER] = "tdls_oper",
236 	[NL80211_CMD_TDLS_MGMT] = "tdls_mgmt",
237 	[NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame",
238 	[NL80211_CMD_PROBE_CLIENT] = "probe_client",
239 	[NL80211_CMD_REGISTER_BEACONS] = "register_beacons",
240 	[NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame",
241 	[NL80211_CMD_SET_NOACK_MAP] = "set_noack_map",
242 	[NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify",
243 	[NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device",
244 	[NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device",
245 	[NL80211_CMD_CONN_FAILED] = "conn_failed",
246 	[NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate",
247 	[NL80211_CMD_SET_MAC_ACL] = "set_mac_acl",
248 	[NL80211_CMD_RADAR_DETECT] = "radar_detect",
249 	[NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features",
250 	[NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies",
251 	[NL80211_CMD_FT_EVENT] = "ft_event",
252 	[NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start",
253 	[NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop",
254 	[NL80211_CMD_GET_COALESCE] = "get_coalesce",
255 	[NL80211_CMD_SET_COALESCE] = "set_coalesce",
256 	[NL80211_CMD_CHANNEL_SWITCH] = "channel_switch",
257 	[NL80211_CMD_VENDOR] = "vendor",
258 	[NL80211_CMD_SET_QOS_MAP] = "set_qos_map",
259 	[NL80211_CMD_ADD_TX_TS] = "add_tx_ts",
260 	[NL80211_CMD_DEL_TX_TS] = "del_tx_ts",
261 	[NL80211_CMD_GET_MPP] = "get_mpp",
262 	[NL80211_CMD_JOIN_OCB] = "join_ocb",
263 	[NL80211_CMD_LEAVE_OCB] = "leave_ocb",
264 	[NL80211_CMD_CH_SWITCH_STARTED_NOTIFY] = "ch_switch_started_notify",
265 	[NL80211_CMD_TDLS_CHANNEL_SWITCH] = "tdls_channel_switch",
266 	[NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH] = "tdls_cancel_channel_switch",
267 	[NL80211_CMD_WIPHY_REG_CHANGE] = "wiphy_reg_change",
268 	[NL80211_CMD_ABORT_SCAN] = "abort_scan",
269 	[NL80211_CMD_START_NAN] = "start_nan",
270 	[NL80211_CMD_STOP_NAN] = "stop_nan",
271 	[NL80211_CMD_ADD_NAN_FUNCTION] = "add_nan_function",
272 	[NL80211_CMD_DEL_NAN_FUNCTION] = "del_nan_function",
273 	[NL80211_CMD_CHANGE_NAN_CONFIG] = "change_nan_config",
274 	[NL80211_CMD_NAN_MATCH] = "nan_match",
275 	[NL80211_CMD_SET_MULTICAST_TO_UNICAST] = "set_multicast_to_unicast",
276 	[NL80211_CMD_UPDATE_CONNECT_PARAMS] = "update_connect_params",
277 	[NL80211_CMD_SET_PMK] = "set_pmk",
278 	[NL80211_CMD_DEL_PMK] = "del_pmk",
279 	[NL80211_CMD_PORT_AUTHORIZED] = "port_authorized",
280 	[NL80211_CMD_RELOAD_REGDB] = "reload_regdb",
281 	[NL80211_CMD_EXTERNAL_AUTH] = "external_auth",
282 	[NL80211_CMD_STA_OPMODE_CHANGED] = "sta_opmode_changed",
283 	[NL80211_CMD_CONTROL_PORT_FRAME] = "control_port_frame",
284 	[NL80211_CMD_GET_FTM_RESPONDER_STATS] = "get_ftm_responder_stats",
285 	[NL80211_CMD_PEER_MEASUREMENT_START] = "peer_measurement_start",
286 	[NL80211_CMD_PEER_MEASUREMENT_RESULT] = "peer_measurement_result",
287 	[NL80211_CMD_PEER_MEASUREMENT_COMPLETE] = "peer_measurement_complete",
288 	[NL80211_CMD_NOTIFY_RADAR] = "notify_radar",
289 	[NL80211_CMD_UPDATE_OWE_INFO] = "update_owe_info",
290 	[NL80211_CMD_PROBE_MESH_LINK] = "probe_mesh_link",
291 	[NL80211_CMD_SET_TID_CONFIG] = "set_tid_config",
292 	[NL80211_CMD_UNPROT_BEACON] = "unprot_beacon",
293 	[NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS] = "control_port_frame_tx_status",
294 	[NL80211_CMD_SET_SAR_SPECS] = "set_sar_specs",
295 	[NL80211_CMD_OBSS_COLOR_COLLISION] = "obss_color_collision",
296 	[NL80211_CMD_COLOR_CHANGE_REQUEST] = "color_change_request",
297 	[NL80211_CMD_COLOR_CHANGE_STARTED] = "color_change_started",
298 	[NL80211_CMD_COLOR_CHANGE_ABORTED] = "color_change_aborted",
299 	[NL80211_CMD_COLOR_CHANGE_COMPLETED] = "color_change_completed",
300 	[NL80211_CMD_SET_FILS_AAD] = "set_fils_aad",
301 	[NL80211_CMD_ASSOC_COMEBACK] = "assoc_comeback",
302 };
303 
304 static char cmdbuf[100];
305 
command_name(enum nl80211_commands cmd)306 const char *command_name(enum nl80211_commands cmd)
307 {
308 	if (cmd <= NL80211_CMD_MAX && commands[cmd])
309 		return commands[cmd];
310 	sprintf(cmdbuf, "Unknown command (%d)", cmd);
311 	return cmdbuf;
312 }
313 
ieee80211_channel_to_frequency(int chan,enum nl80211_band band)314 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
315 {
316 	/* see 802.11 17.3.8.3.2 and Annex J
317 	 * there are overlapping channel numbers in 5GHz and 2GHz bands */
318 	if (chan <= 0)
319 		return 0; /* not supported */
320 	switch (band) {
321 	case NL80211_BAND_2GHZ:
322 		if (chan == 14)
323 			return 2484;
324 		else if (chan < 14)
325 			return 2407 + chan * 5;
326 		break;
327 	case NL80211_BAND_5GHZ:
328 		if (chan >= 182 && chan <= 196)
329 			return 4000 + chan * 5;
330 		else
331 			return 5000 + chan * 5;
332 		break;
333 	case NL80211_BAND_6GHZ:
334 		/* see 802.11ax D6.1 27.3.23.2 */
335 		if (chan == 2)
336 			return 5935;
337 		if (chan <= 253)
338 			return 5950 + chan * 5;
339 		break;
340 	case NL80211_BAND_60GHZ:
341 		if (chan < 7)
342 			return 56160 + chan * 2160;
343 		break;
344 	default:
345 		;
346 	}
347 	return 0; /* not supported */
348 }
349 
ieee80211_frequency_to_channel(int freq)350 int ieee80211_frequency_to_channel(int freq)
351 {
352 	/* see 802.11-2007 17.3.8.3.2 and Annex J */
353 	if (freq == 2484)
354 		return 14;
355 	/* see 802.11ax D6.1 27.3.23.2 and Annex E */
356 	else if (freq == 5935)
357 		return 2;
358 	else if (freq < 2484)
359 		return (freq - 2407) / 5;
360 	else if (freq >= 4910 && freq <= 4980)
361 		return (freq - 4000) / 5;
362 	else if (freq < 5950)
363 		return (freq - 5000) / 5;
364 	else if (freq <= 45000) /* DMG band lower limit */
365 		/* see 802.11ax D6.1 27.3.23.2 */
366 		return (freq - 5950) / 5;
367 	else if (freq >= 58320 && freq <= 70200)
368 		return (freq - 56160) / 2160;
369 	else
370 		return 0;
371 }
372 
print_ssid_escaped(const uint8_t len,const uint8_t * data)373 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
374 {
375 	int i;
376 
377 	for (i = 0; i < len; i++) {
378 		if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
379 			printf("%c", data[i]);
380 		else if (data[i] == ' ' &&
381 			 (i != 0 && i != len -1))
382 			printf(" ");
383 		else
384 			printf("\\x%.2x", data[i]);
385 	}
386 }
387 
hex2num(char digit)388 static int hex2num(char digit)
389 {
390 	if (!isxdigit(digit))
391 		return -1;
392 	if (isdigit(digit))
393 		return digit - '0';
394 	return tolower(digit) - 'a' + 10;
395 }
396 
hex2byte(const char * hex)397 static int hex2byte(const char *hex)
398 {
399 	int d1, d2;
400 
401 	d1 = hex2num(hex[0]);
402 	if (d1 < 0)
403 		return -1;
404 	d2 = hex2num(hex[1]);
405 	if (d2 < 0)
406 		return -1;
407 	return (d1 << 4) | d2;
408 }
409 
hex2bin(const char * hex,char * buf)410 char *hex2bin(const char *hex, char *buf)
411 {
412 	char *result = buf;
413 	int d;
414 
415 	while (hex[0]) {
416 		d = hex2byte(hex);
417 		if (d < 0)
418 			return NULL;
419 		buf[0] = d;
420 		buf++;
421 		hex += 2;
422 	}
423 
424 	return result;
425 }
426 
parse_akm_suite(const char * cipher_str)427 static int parse_akm_suite(const char *cipher_str)
428 {
429 
430 	if (!strcmp(cipher_str, "PSK"))
431 		return 0x000FAC02;
432 	if (!strcmp(cipher_str, "FT/PSK"))
433 		return 0x000FAC03;
434 	if (!strcmp(cipher_str, "PSK/SHA-256"))
435 		return 0x000FAC06;
436 	return -EINVAL;
437 }
438 
parse_cipher_suite(const char * cipher_str)439 static int parse_cipher_suite(const char *cipher_str)
440 {
441 
442 	if (!strcmp(cipher_str, "TKIP"))
443 		return WLAN_CIPHER_SUITE_TKIP;
444 	if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128"))
445 		return WLAN_CIPHER_SUITE_CCMP;
446 	if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128"))
447 		return WLAN_CIPHER_SUITE_GCMP;
448 	if (!strcmp(cipher_str, "GCMP-256"))
449 		return WLAN_CIPHER_SUITE_GCMP_256;
450 	if (!strcmp(cipher_str, "CCMP-256"))
451 		return WLAN_CIPHER_SUITE_CCMP_256;
452 	return -EINVAL;
453 }
454 
parse_keys(struct nl_msg * msg,char ** argv[],int * argc)455 int parse_keys(struct nl_msg *msg, char **argv[], int *argc)
456 {
457 	struct nlattr *keys;
458 	int i = 0;
459 	bool have_default = false;
460 	char *arg = **argv;
461 	char keybuf[13];
462 	int pos = 0;
463 
464 	if (!*argc)
465 		return 1;
466 
467 	if (!memcmp(&arg[pos], "psk", 3)) {
468 		char psk_keybuf[32];
469 		int cipher_suite, akm_suite;
470 
471 		if (*argc < 4)
472 			goto explain;
473 
474 		pos+=3;
475 		if (arg[pos] != ':')
476 			goto explain;
477 		pos++;
478 
479 		NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, NL80211_WPA_VERSION_2);
480 
481 		if (strlen(&arg[pos]) != (sizeof(psk_keybuf) * 2) || !hex2bin(&arg[pos], psk_keybuf)) {
482 			printf("Bad PSK\n");
483 			return -EINVAL;
484 		}
485 
486 		NLA_PUT(msg, NL80211_ATTR_PMK, 32, psk_keybuf);
487 		NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM);
488 
489 		*argv += 1;
490 		*argc -= 1;
491 		arg = **argv;
492 
493 		akm_suite = parse_akm_suite(arg);
494 		if (akm_suite < 0)
495 			goto explain;
496 
497 		NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, akm_suite);
498 
499 		*argv += 1;
500 		*argc -= 1;
501 		arg = **argv;
502 
503 		cipher_suite = parse_cipher_suite(arg);
504 		if (cipher_suite < 0)
505 			goto explain;
506 
507 		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher_suite);
508 
509 		*argv += 1;
510 		*argc -= 1;
511 		arg = **argv;
512 
513 		cipher_suite = parse_cipher_suite(arg);
514 		if (cipher_suite < 0)
515 			goto explain;
516 
517 		NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher_suite);
518 
519 		*argv += 1;
520 		*argc -= 1;
521 		return 0;
522 	}
523 
524 	NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
525 
526 	keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
527 	if (!keys)
528 		return -ENOBUFS;
529 
530 	do {
531 		int keylen;
532 		struct nlattr *key = nla_nest_start(msg, ++i);
533 		char *keydata;
534 
535 		arg = **argv;
536 		pos = 0;
537 
538 		if (!key)
539 			return -ENOBUFS;
540 
541 		if (arg[pos] == 'd') {
542 			NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
543 			pos++;
544 			if (arg[pos] == ':')
545 				pos++;
546 			have_default = true;
547 		}
548 
549 		if (!isdigit(arg[pos]))
550 			goto explain;
551 		NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
552 		if (arg[pos++] != ':')
553 			goto explain;
554 		keydata = arg + pos;
555 		switch (strlen(keydata)) {
556 		case 10:
557 			keydata = hex2bin(keydata, keybuf);
558 			/* fall through */
559 		case 5:
560 			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
561 				    WLAN_CIPHER_SUITE_WEP40);
562 			keylen = 5;
563 			break;
564 		case 26:
565 			keydata = hex2bin(keydata, keybuf);
566 			/* fall through */
567 		case 13:
568 			NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
569 				    WLAN_CIPHER_SUITE_WEP104);
570 			keylen = 13;
571 			break;
572 		default:
573 			goto explain;
574 		}
575 
576 		if (!keydata)
577 			goto explain;
578 
579 		NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
580 
581 		*argv += 1;
582 		*argc -= 1;
583 
584 		/* one key should be TX key */
585 		if (!have_default && !*argc)
586 			NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
587 
588 		nla_nest_end(msg, key);
589 	} while (*argc);
590 
591 	nla_nest_end(msg, keys);
592 
593 	return 0;
594  nla_put_failure:
595 	return -ENOBUFS;
596  explain:
597 	fprintf(stderr, "key must be [d:]index:data where\n"
598 			"  'd:'     means default (transmit) key\n"
599 			"  'index:' is a single digit (0-3)\n"
600 			"  'data'   must be 5 or 13 ascii chars\n"
601 			"           or 10 or 26 hex digits\n"
602 			"for example: d:2:6162636465 is the same as d:2:abcde\n"
603 			"or psk:data <AKM Suite> <pairwise CIPHER> <groupwise CIPHER> where\n"
604 			"  'data' is the PSK (output of wpa_passphrase and the CIPHER can be CCMP or GCMP\n"
605 			"for example: psk:0123456789abcdef PSK CCMP CCMP\n"
606 			"The allowed AKM suites are PSK, FT/PSK, PSK/SHA-256\n"
607 			"The allowed Cipher suites are TKIP, CCMP, GCMP, GCMP-256, CCMP-256\n");
608 	return 2;
609 }
610 
str_to_bw(const char * str)611 enum nl80211_chan_width str_to_bw(const char *str)
612 {
613 	static const struct {
614 		const char *name;
615 		unsigned int val;
616 	} bwmap[] = {
617 		{ .name = "5", .val = NL80211_CHAN_WIDTH_5, },
618 		{ .name = "10", .val = NL80211_CHAN_WIDTH_10, },
619 		{ .name = "20", .val = NL80211_CHAN_WIDTH_20, },
620 		{ .name = "40", .val = NL80211_CHAN_WIDTH_40, },
621 		{ .name = "80", .val = NL80211_CHAN_WIDTH_80, },
622 		{ .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, },
623 		{ .name = "160", .val = NL80211_CHAN_WIDTH_160, },
624 	};
625 	unsigned int i;
626 
627 	for (i = 0; i < ARRAY_SIZE(bwmap); i++) {
628 		if (strcasecmp(bwmap[i].name, str) == 0)
629 			return bwmap[i].val;
630 	}
631 
632 	return NL80211_CHAN_WIDTH_20_NOHT;
633 }
634 
parse_freqs(struct chandef * chandef,int argc,char ** argv,int * parsed)635 static int parse_freqs(struct chandef *chandef, int argc, char **argv,
636 		       int *parsed)
637 {
638 	uint32_t freq;
639 	char *end;
640 	bool need_cf1 = false, need_cf2 = false;
641 
642 	if (argc < 1)
643 		return 0;
644 
645 	chandef->width = str_to_bw(argv[0]);
646 
647 	switch (chandef->width) {
648 	case NL80211_CHAN_WIDTH_20_NOHT:
649 		/* First argument was not understood, give up gracefully. */
650 		return 0;
651 	case NL80211_CHAN_WIDTH_20:
652 	case NL80211_CHAN_WIDTH_5:
653 	case NL80211_CHAN_WIDTH_10:
654 		break;
655 	case NL80211_CHAN_WIDTH_80P80:
656 		need_cf2 = true;
657 		/* fall through */
658 	case NL80211_CHAN_WIDTH_40:
659 	case NL80211_CHAN_WIDTH_80:
660 	case NL80211_CHAN_WIDTH_160:
661 	case NL80211_CHAN_WIDTH_320:
662 		need_cf1 = true;
663 		break;
664 	case NL80211_CHAN_WIDTH_1:
665 	case NL80211_CHAN_WIDTH_2:
666 	case NL80211_CHAN_WIDTH_4:
667 	case NL80211_CHAN_WIDTH_8:
668 	case NL80211_CHAN_WIDTH_16:
669 		/* can't happen yet */
670 		break;
671 	}
672 
673 	*parsed += 1;
674 
675 	if (!need_cf1)
676 		return 0;
677 
678 	if (argc < 2)
679 		return 1;
680 
681 	/* center freq 1 */
682 	if (!*argv[1])
683 		return 1;
684 	freq = strtoul(argv[1], &end, 10);
685 	if (*end)
686 		return 1;
687 	*parsed += 1;
688 
689 	chandef->center_freq1 = freq;
690 
691 	if (!need_cf2)
692 		return 0;
693 
694 	if (argc < 3)
695 		return 1;
696 
697 	/* center freq 2 */
698 	if (!*argv[2])
699 		return 1;
700 	freq = strtoul(argv[2], &end, 10);
701 	if (*end)
702 		return 1;
703 	chandef->center_freq2 = freq;
704 
705 	*parsed += 1;
706 
707 	return 0;
708 }
709 
710 
711 /**
712  * parse_freqchan - Parse frequency or channel definition
713  *
714  * @chandef: chandef structure to be filled in
715  * @chan: Boolean whether to parse a channel or frequency based specifier
716  * @argc: Number of arguments
717  * @argv: Array of string arguments
718  * @parsed: Pointer to return the number of used arguments, or NULL to error
719  *          out if any argument is left unused.
720  *
721  * The given chandef structure will be filled in from the command line
722  * arguments. argc/argv will be updated so that further arguments from the
723  * command line can be parsed.
724  *
725  * Note that despite the fact that the function knows how many center freqs
726  * are needed, there's an ambiguity if the next argument after this is an
727  * integer argument, since the valid channel width values are interpreted
728  * as such, rather than a following argument. This can be avoided by the
729  * user by giving "NOHT" instead.
730  *
731  * The working specifier if chan is set are:
732  *   <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
733  *
734  * And if frequency is set:
735  *   <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
736  *   <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
737  *
738  * If the mode/channel width is not given the NOHT is assumed.
739  *
740  * Return: Number of used arguments, zero or negative error number otherwise
741  */
parse_freqchan(struct chandef * chandef,bool chan,int argc,char ** argv,int * parsed)742 int parse_freqchan(struct chandef *chandef, bool chan, int argc, char **argv,
743 		   int *parsed)
744 {
745 	char *end;
746 	static const struct chanmode chanmode[] = {
747 		{ .name = "HT20",
748 		  .width = NL80211_CHAN_WIDTH_20,
749 		  .freq1_diff = 0,
750 		  .chantype = NL80211_CHAN_HT20 },
751 		{ .name = "HT40+",
752 		  .width = NL80211_CHAN_WIDTH_40,
753 		  .freq1_diff = 10,
754 		  .chantype = NL80211_CHAN_HT40PLUS },
755 		{ .name = "HT40-",
756 		  .width = NL80211_CHAN_WIDTH_40,
757 		  .freq1_diff = -10,
758 		  .chantype = NL80211_CHAN_HT40MINUS },
759 		{ .name = "NOHT",
760 		  .width = NL80211_CHAN_WIDTH_20_NOHT,
761 		  .freq1_diff = 0,
762 		  .chantype = NL80211_CHAN_NO_HT },
763 		{ .name = "5MHz",
764 		  .width = NL80211_CHAN_WIDTH_5,
765 		  .freq1_diff = 0,
766 		  .chantype = -1 },
767 		{ .name = "10MHz",
768 		  .width = NL80211_CHAN_WIDTH_10,
769 		  .freq1_diff = 0,
770 		  .chantype = -1 },
771 		{ .name = "80MHz",
772 		  .width = NL80211_CHAN_WIDTH_80,
773 		  .freq1_diff = 0,
774 		  .chantype = -1 },
775 		{ .name = "160MHz",
776 		  .width = NL80211_CHAN_WIDTH_160,
777 		  .freq1_diff = 0,
778 		  .chantype = -1 },
779 		{ .name = "320MHz",
780 		  .width = NL80211_CHAN_WIDTH_320,
781 		  .freq1_diff = 0,
782 		  .chantype = -1 },
783 	};
784 	const struct chanmode *chanmode_selected = NULL;
785 	unsigned int freq;
786 	unsigned int i;
787 	int _parsed = 0;
788 	int res = 0;
789 
790 	if (argc < 1)
791 		return 1;
792 
793 	if (!argv[0])
794 		goto out;
795 	freq = strtoul(argv[0], &end, 10);
796 	if (*end) {
797 		res = 1;
798 		goto out;
799 	}
800 
801 	_parsed += 1;
802 
803 	memset(chandef, 0, sizeof(struct chandef));
804 
805 	if (chan) {
806 		enum nl80211_band band;
807 
808 		band = freq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
809 		freq = ieee80211_channel_to_frequency(freq, band);
810 	}
811 	chandef->control_freq = freq;
812 	/* Assume 20MHz NOHT channel for now. */
813 	chandef->center_freq1 = freq;
814 
815 	/* Try to parse HT mode definitions */
816 	if (argc > 1) {
817 		for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
818 			if (strcasecmp(chanmode[i].name, argv[1]) == 0) {
819 				chanmode_selected = &chanmode[i];
820 				_parsed += 1;
821 				break;
822 			}
823 		}
824 	}
825 
826 	/* channel mode given, use it and return. */
827 	if (chanmode_selected) {
828 		chandef->center_freq1 = get_cf1(chanmode_selected, freq);
829 		chandef->width = chanmode_selected->width;
830 		goto out;
831 	}
832 
833 	/* This was a only a channel definition, nothing further may follow. */
834 	if (chan)
835 		goto out;
836 
837 	res = parse_freqs(chandef, argc - 1, argv + 1, &_parsed);
838 
839  out:
840 	/* Error out if parsed is NULL. */
841 	if (!parsed && _parsed != argc)
842 		return 1;
843 
844 	if (parsed)
845 		*parsed = _parsed;
846 
847 	return res;
848 }
849 
put_chandef(struct nl_msg * msg,struct chandef * chandef)850 int put_chandef(struct nl_msg *msg, struct chandef *chandef)
851 {
852 	NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chandef->control_freq);
853 	NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width);
854 
855 	switch (chandef->width) {
856 	case NL80211_CHAN_WIDTH_20_NOHT:
857 		NLA_PUT_U32(msg,
858 			    NL80211_ATTR_WIPHY_CHANNEL_TYPE,
859 			    NL80211_CHAN_NO_HT);
860 		break;
861 	case NL80211_CHAN_WIDTH_20:
862 		NLA_PUT_U32(msg,
863 			    NL80211_ATTR_WIPHY_CHANNEL_TYPE,
864 			    NL80211_CHAN_HT20);
865 		break;
866 	case NL80211_CHAN_WIDTH_40:
867 		if (chandef->control_freq > chandef->center_freq1)
868 			NLA_PUT_U32(msg,
869 				    NL80211_ATTR_WIPHY_CHANNEL_TYPE,
870 				    NL80211_CHAN_HT40MINUS);
871 		else
872 			NLA_PUT_U32(msg,
873 				    NL80211_ATTR_WIPHY_CHANNEL_TYPE,
874 				    NL80211_CHAN_HT40PLUS);
875 		break;
876 	default:
877 		break;
878 	}
879 
880 	if (chandef->center_freq1)
881 		NLA_PUT_U32(msg,
882 			    NL80211_ATTR_CENTER_FREQ1,
883 			    chandef->center_freq1);
884 
885 	if (chandef->center_freq2)
886 		NLA_PUT_U32(msg,
887 			    NL80211_ATTR_CENTER_FREQ2,
888 			    chandef->center_freq2);
889 
890 	return 0;
891 
892  nla_put_failure:
893 	return -ENOBUFS;
894 }
895 
print_mcs_index(const __u8 * mcs)896 static void print_mcs_index(const __u8 *mcs)
897 {
898 	int mcs_bit, prev_bit = -2, prev_cont = 0;
899 
900 	for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
901 		unsigned int mcs_octet = mcs_bit/8;
902 		unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
903 		bool mcs_rate_idx_set;
904 
905 		mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
906 
907 		if (!mcs_rate_idx_set)
908 			continue;
909 
910 		if (prev_bit != mcs_bit - 1) {
911 			if (prev_bit != -2)
912 				printf("%d, ", prev_bit);
913 			else
914 				printf(" ");
915 			printf("%d", mcs_bit);
916 			prev_cont = 0;
917 		} else if (!prev_cont) {
918 			printf("-");
919 			prev_cont = 1;
920 		}
921 
922 		prev_bit = mcs_bit;
923 	}
924 
925 	if (prev_cont)
926 		printf("%d", prev_bit);
927 	printf("\n");
928 }
929 
930 /*
931  * There are only 4 possible values, we just use a case instead of computing it,
932  * but technically this can also be computed through the formula:
933  *
934  * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
935  */
compute_ampdu_length(__u8 exponent)936 static __u32 compute_ampdu_length(__u8 exponent)
937 {
938 	switch (exponent) {
939 	case 0: return 8191;  /* (2 ^(13 + 0)) -1 */
940 	case 1: return 16383; /* (2 ^(13 + 1)) -1 */
941 	case 2: return 32767; /* (2 ^(13 + 2)) -1 */
942 	case 3: return 65535; /* (2 ^(13 + 3)) -1 */
943 	default: return 0;
944 	}
945 }
946 
print_ampdu_space(__u8 space)947 static const char *print_ampdu_space(__u8 space)
948 {
949 	switch (space) {
950 	case 0: return "No restriction";
951 	case 1: return "1/4 usec";
952 	case 2: return "1/2 usec";
953 	case 3: return "1 usec";
954 	case 4: return "2 usec";
955 	case 5: return "4 usec";
956 	case 6: return "8 usec";
957 	case 7: return "16 usec";
958 	default:
959 		return "BUG (spacing more than 3 bits!)";
960 	}
961 }
962 
print_ampdu_length(__u8 exponent)963 void print_ampdu_length(__u8 exponent)
964 {
965 	__u32 max_ampdu_length;
966 
967 	max_ampdu_length = compute_ampdu_length(exponent);
968 
969 	if (max_ampdu_length) {
970 		printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
971 		       max_ampdu_length, exponent);
972 	} else {
973 		printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
974 		       "(exponent: %d)\n", exponent);
975 	}
976 }
977 
print_ampdu_spacing(__u8 spacing)978 void print_ampdu_spacing(__u8 spacing)
979 {
980 	printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
981 	       print_ampdu_space(spacing), spacing);
982 }
983 
print_ht_capability(__u16 cap)984 void print_ht_capability(__u16 cap)
985 {
986 #define PRINT_HT_CAP(_cond, _str) \
987 	do { \
988 		if (_cond) \
989 			printf("\t\t\t" _str "\n"); \
990 	} while (0)
991 
992 	printf("\t\tCapabilities: 0x%02x\n", cap);
993 
994 	PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
995 	PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
996 	PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
997 
998 	PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
999 	PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
1000 	PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
1001 
1002 	PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
1003 	PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
1004 	PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
1005 	PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
1006 
1007 	PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
1008 	PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
1009 	PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
1010 	PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
1011 
1012 	PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
1013 
1014 	PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
1015 	PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
1016 
1017 	/*
1018 	 * For beacons and probe response this would mean the BSS
1019 	 * does or does not allow the usage of DSSS/CCK HT40.
1020 	 * Otherwise it means the STA does or does not use
1021 	 * DSSS/CCK HT40.
1022 	 */
1023 	PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
1024 	PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
1025 
1026 	/* BIT(13) is reserved */
1027 
1028 	PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
1029 
1030 	PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
1031 #undef PRINT_HT_CAP
1032 }
1033 
print_ht_mcs(const __u8 * mcs)1034 void print_ht_mcs(const __u8 *mcs)
1035 {
1036 	/* As defined in 7.3.2.57.4 Supported MCS Set field */
1037 	unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
1038 	bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
1039 
1040 	max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
1041 	tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
1042 	tx_mcs_set_equal = !(mcs[12] & (1 << 1));
1043 	tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
1044 	tx_unequal_modulation = !!(mcs[12] & (1 << 4));
1045 
1046 	if (max_rx_supp_data_rate)
1047 		printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
1048 	/* XXX: else see 9.6.0e.5.3 how to get this I think */
1049 
1050 	if (tx_mcs_set_defined) {
1051 		if (tx_mcs_set_equal) {
1052 			printf("\t\tHT TX/RX MCS rate indexes supported:");
1053 			print_mcs_index(mcs);
1054 		} else {
1055 			printf("\t\tHT RX MCS rate indexes supported:");
1056 			print_mcs_index(mcs);
1057 
1058 			if (tx_unequal_modulation)
1059 				printf("\t\tTX unequal modulation supported\n");
1060 			else
1061 				printf("\t\tTX unequal modulation not supported\n");
1062 
1063 			printf("\t\tHT TX Max spatial streams: %d\n",
1064 				tx_max_num_spatial_streams);
1065 
1066 			printf("\t\tHT TX MCS rate indexes supported may differ\n");
1067 		}
1068 	} else {
1069 		printf("\t\tHT RX MCS rate indexes supported:");
1070 		print_mcs_index(mcs);
1071 		printf("\t\tHT TX MCS rate indexes are undefined\n");
1072 	}
1073 }
1074 
print_vht_info(__u32 capa,const __u8 * mcs)1075 void print_vht_info(__u32 capa, const __u8 *mcs)
1076 {
1077 	__u16 tmp;
1078 	int i;
1079 
1080 	printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
1081 
1082 #define PRINT_VHT_CAPA(_bit, _str) \
1083 	do { \
1084 		if (capa & BIT(_bit)) \
1085 			printf("\t\t\t" _str "\n"); \
1086 	} while (0)
1087 
1088 	printf("\t\t\tMax MPDU length: ");
1089 	switch (capa & 3) {
1090 	case 0: printf("3895\n"); break;
1091 	case 1: printf("7991\n"); break;
1092 	case 2: printf("11454\n"); break;
1093 	case 3: printf("(reserved)\n");
1094 	}
1095 	printf("\t\t\tSupported Channel Width: ");
1096 	switch ((capa >> 2) & 3) {
1097 	case 0: printf("neither 160 nor 80+80\n"); break;
1098 	case 1: printf("160 MHz\n"); break;
1099 	case 2: printf("160 MHz, 80+80 MHz\n"); break;
1100 	case 3: printf("(reserved)\n");
1101 	}
1102 	PRINT_VHT_CAPA(4, "RX LDPC");
1103 	PRINT_VHT_CAPA(5, "short GI (80 MHz)");
1104 	PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
1105 	PRINT_VHT_CAPA(7, "TX STBC");
1106 	/* RX STBC */
1107 	PRINT_VHT_CAPA(11, "SU Beamformer");
1108 	PRINT_VHT_CAPA(12, "SU Beamformee");
1109 	/* compressed steering */
1110 	/* # of sounding dimensions */
1111 	PRINT_VHT_CAPA(19, "MU Beamformer");
1112 	PRINT_VHT_CAPA(20, "MU Beamformee");
1113 	PRINT_VHT_CAPA(21, "VHT TXOP PS");
1114 	PRINT_VHT_CAPA(22, "+HTC-VHT");
1115 	/* max A-MPDU */
1116 	/* VHT link adaptation */
1117 	PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
1118 	PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
1119 
1120 	printf("\t\tVHT RX MCS set:\n");
1121 	tmp = mcs[0] | (mcs[1] << 8);
1122 	for (i = 1; i <= 8; i++) {
1123 		printf("\t\t\t%d streams: ", i);
1124 		switch ((tmp >> ((i-1)*2) ) & 3) {
1125 		case 0: printf("MCS 0-7\n"); break;
1126 		case 1: printf("MCS 0-8\n"); break;
1127 		case 2: printf("MCS 0-9\n"); break;
1128 		case 3: printf("not supported\n"); break;
1129 		}
1130 	}
1131 	tmp = mcs[2] | (mcs[3] << 8);
1132 	printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
1133 
1134 	printf("\t\tVHT TX MCS set:\n");
1135 	tmp = mcs[4] | (mcs[5] << 8);
1136 	for (i = 1; i <= 8; i++) {
1137 		printf("\t\t\t%d streams: ", i);
1138 		switch ((tmp >> ((i-1)*2) ) & 3) {
1139 		case 0: printf("MCS 0-7\n"); break;
1140 		case 1: printf("MCS 0-8\n"); break;
1141 		case 2: printf("MCS 0-9\n"); break;
1142 		case 3: printf("not supported\n"); break;
1143 		}
1144 	}
1145 	tmp = mcs[6] | (mcs[7] << 8);
1146 	printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
1147 }
1148 
__print_he_capa(const __u16 * mac_cap,const __u16 * phy_cap,const __u16 * mcs_set,size_t mcs_len,const __u8 * ppet,int ppet_len,bool indent)1149 static void __print_he_capa(const __u16 *mac_cap,
1150 			    const __u16 *phy_cap,
1151 			    const __u16 *mcs_set, size_t mcs_len,
1152 			    const __u8 *ppet, int ppet_len,
1153 			    bool indent)
1154 {
1155 	size_t mcs_used;
1156 	int i;
1157 	const char *pre = indent ? "\t" : "";
1158 
1159 	#define PRINT_HE_CAP(_var, _idx, _bit, _str) \
1160 	do { \
1161 		if (_var[_idx] & BIT(_bit)) \
1162 			printf("%s\t\t\t" _str "\n", pre); \
1163 	} while (0)
1164 
1165 	#define PRINT_HE_CAP_MASK(_var, _idx, _shift, _mask, _str) \
1166 	do { \
1167 		if ((_var[_idx] >> _shift) & _mask) \
1168 			printf("%s\t\t\t" _str ": %d\n", pre, (_var[_idx] >> _shift) & _mask); \
1169 	} while (0)
1170 
1171 	#define PRINT_HE_MAC_CAP(...) PRINT_HE_CAP(mac_cap, __VA_ARGS__)
1172 	#define PRINT_HE_MAC_CAP_MASK(...) PRINT_HE_CAP_MASK(mac_cap, __VA_ARGS__)
1173 	#define PRINT_HE_PHY_CAP(...) PRINT_HE_CAP(phy_cap, __VA_ARGS__)
1174 	#define PRINT_HE_PHY_CAP0(_idx, _bit, ...) PRINT_HE_CAP(phy_cap, _idx, _bit + 8, __VA_ARGS__)
1175 	#define PRINT_HE_PHY_CAP_MASK(...) PRINT_HE_CAP_MASK(phy_cap, __VA_ARGS__)
1176 
1177 	printf("%s\t\tHE MAC Capabilities (0x", pre);
1178 	for (i = 0; i < 3; i++)
1179 		printf("%04x", mac_cap[i]);
1180 	printf("):\n");
1181 
1182 	PRINT_HE_MAC_CAP(0, 0, "+HTC HE Supported");
1183 	PRINT_HE_MAC_CAP(0, 1, "TWT Requester");
1184 	PRINT_HE_MAC_CAP(0, 2, "TWT Responder");
1185 	PRINT_HE_MAC_CAP_MASK(0, 3, 0x3, "Dynamic BA Fragementation Level");
1186 	PRINT_HE_MAC_CAP_MASK(0, 5, 0x7, "Maximum number of MSDUS Fragments");
1187 	PRINT_HE_MAC_CAP_MASK(0, 8, 0x3, "Minimum Payload size of 128 bytes");
1188 	PRINT_HE_MAC_CAP_MASK(0, 10, 0x3, "Trigger Frame MAC Padding Duration");
1189 	PRINT_HE_MAC_CAP_MASK(0, 12, 0x7, "Multi-TID Aggregation Support");
1190 
1191 	PRINT_HE_MAC_CAP(1, 1, "All Ack");
1192 	PRINT_HE_MAC_CAP(1, 2, "TRS");
1193 	PRINT_HE_MAC_CAP(1, 3, "BSR");
1194 	PRINT_HE_MAC_CAP(1, 4, "Broadcast TWT");
1195 	PRINT_HE_MAC_CAP(1, 5, "32-bit BA Bitmap");
1196 	PRINT_HE_MAC_CAP(1, 6, "MU Cascading");
1197 	PRINT_HE_MAC_CAP(1, 7, "Ack-Enabled Aggregation");
1198 	PRINT_HE_MAC_CAP(1, 9, "OM Control");
1199 	PRINT_HE_MAC_CAP(1, 10, "OFDMA RA");
1200 	PRINT_HE_MAC_CAP_MASK(1, 11, 0x3, "Maximum A-MPDU Length Exponent");
1201 	PRINT_HE_MAC_CAP(1, 13, "A-MSDU Fragmentation");
1202 	PRINT_HE_MAC_CAP(1, 14, "Flexible TWT Scheduling");
1203 	PRINT_HE_MAC_CAP(1, 15, "RX Control Frame to MultiBSS");
1204 
1205 	PRINT_HE_MAC_CAP(2, 0, "BSRP BQRP A-MPDU Aggregation");
1206 	PRINT_HE_MAC_CAP(2, 1, "QTP");
1207 	PRINT_HE_MAC_CAP(2, 2, "BQR");
1208 	PRINT_HE_MAC_CAP(2, 3, "SRP Responder Role");
1209 	PRINT_HE_MAC_CAP(2, 4, "NDP Feedback Report");
1210 	PRINT_HE_MAC_CAP(2, 5, "OPS");
1211 	PRINT_HE_MAC_CAP(2, 6, "A-MSDU in A-MPDU");
1212 	PRINT_HE_MAC_CAP_MASK(2, 7, 7, "Multi-TID Aggregation TX");
1213 	PRINT_HE_MAC_CAP(2, 10, "HE Subchannel Selective Transmission");
1214 	PRINT_HE_MAC_CAP(2, 11, "UL 2x996-Tone RU");
1215 	PRINT_HE_MAC_CAP(2, 12, "OM Control UL MU Data Disable RX");
1216 
1217 	printf("%s\t\tHE PHY Capabilities: (0x", pre);
1218 	for (i = 0; i < 11; i++)
1219 		printf("%02x", ((__u8 *)phy_cap)[i + 1]);
1220 	printf("):\n");
1221 
1222 	PRINT_HE_PHY_CAP0(0, 1, "HE40/2.4GHz");
1223 	PRINT_HE_PHY_CAP0(0, 2, "HE40/HE80/5GHz");
1224 	PRINT_HE_PHY_CAP0(0, 3, "HE160/5GHz");
1225 	PRINT_HE_PHY_CAP0(0, 4, "HE160/HE80+80/5GHz");
1226 	PRINT_HE_PHY_CAP0(0, 5, "242 tone RUs/2.4GHz");
1227 	PRINT_HE_PHY_CAP0(0, 6, "242 tone RUs/5GHz");
1228 
1229 	PRINT_HE_PHY_CAP_MASK(1, 0, 0xf, "Punctured Preamble RX");
1230 	PRINT_HE_PHY_CAP_MASK(1, 4, 0x1, "Device Class");
1231 	PRINT_HE_PHY_CAP(1, 5, "LDPC Coding in Payload");
1232 	PRINT_HE_PHY_CAP(1, 6, "HE SU PPDU with 1x HE-LTF and 0.8us GI");
1233 	PRINT_HE_PHY_CAP_MASK(1, 7, 0x3, "Midamble Rx Max NSTS");
1234 	PRINT_HE_PHY_CAP(1, 9, "NDP with 4x HE-LTF and 3.2us GI");
1235 	PRINT_HE_PHY_CAP(1, 10, "STBC Tx <= 80MHz");
1236 	PRINT_HE_PHY_CAP(1, 11, "STBC Rx <= 80MHz");
1237 	PRINT_HE_PHY_CAP(1, 12, "Doppler Tx");
1238 	PRINT_HE_PHY_CAP(1, 13, "Doppler Rx");
1239 	PRINT_HE_PHY_CAP(1, 14, "Full Bandwidth UL MU-MIMO");
1240 	PRINT_HE_PHY_CAP(1, 15, "Partial Bandwidth UL MU-MIMO");
1241 
1242 	PRINT_HE_PHY_CAP_MASK(2, 0, 0x3, "DCM Max Constellation");
1243 	PRINT_HE_PHY_CAP_MASK(2, 2, 0x1, "DCM Max NSS Tx");
1244 	PRINT_HE_PHY_CAP_MASK(2, 3, 0x3, "DCM Max Constellation Rx");
1245 	PRINT_HE_PHY_CAP_MASK(2, 5, 0x1, "DCM Max NSS Rx");
1246 	PRINT_HE_PHY_CAP(2, 6, "Rx HE MU PPDU from Non-AP STA");
1247 	PRINT_HE_PHY_CAP(2, 7, "SU Beamformer");
1248 	PRINT_HE_PHY_CAP(2, 8, "SU Beamformee");
1249 	PRINT_HE_PHY_CAP(2, 9, "MU Beamformer");
1250 	PRINT_HE_PHY_CAP_MASK(2, 10, 0x7, "Beamformee STS <= 80Mhz");
1251 	PRINT_HE_PHY_CAP_MASK(2, 13, 0x7, "Beamformee STS > 80Mhz");
1252 
1253 	PRINT_HE_PHY_CAP_MASK(3, 0, 0x7, "Sounding Dimensions <= 80Mhz");
1254 	PRINT_HE_PHY_CAP_MASK(3, 3, 0x7, "Sounding Dimensions > 80Mhz");
1255 	PRINT_HE_PHY_CAP(3, 6, "Ng = 16 SU Feedback");
1256 	PRINT_HE_PHY_CAP(3, 7, "Ng = 16 MU Feedback");
1257 	PRINT_HE_PHY_CAP(3, 8, "Codebook Size SU Feedback");
1258 	PRINT_HE_PHY_CAP(3, 9, "Codebook Size MU Feedback");
1259 	PRINT_HE_PHY_CAP(3, 10, "Triggered SU Beamforming Feedback");
1260 	PRINT_HE_PHY_CAP(3, 11, "Triggered MU Beamforming Feedback");
1261 	PRINT_HE_PHY_CAP(3, 12, "Triggered CQI Feedback");
1262 	PRINT_HE_PHY_CAP(3, 13, "Partial Bandwidth Extended Range");
1263 	PRINT_HE_PHY_CAP(3, 14, "Partial Bandwidth DL MU-MIMO");
1264 	PRINT_HE_PHY_CAP(3, 15, "PPE Threshold Present");
1265 
1266 	PRINT_HE_PHY_CAP(4, 0, "SRP-based SR");
1267 	PRINT_HE_PHY_CAP(4, 1, "Power Boost Factor ar");
1268 	PRINT_HE_PHY_CAP(4, 2, "HE SU PPDU & HE PPDU 4x HE-LTF 0.8us GI");
1269 	PRINT_HE_PHY_CAP_MASK(4, 3, 0x7, "Max NC");
1270 	PRINT_HE_PHY_CAP(4, 6, "STBC Tx > 80MHz");
1271 	PRINT_HE_PHY_CAP(4, 7, "STBC Rx > 80MHz");
1272 	PRINT_HE_PHY_CAP(4, 8, "HE ER SU PPDU 4x HE-LTF 0.8us GI");
1273 	PRINT_HE_PHY_CAP(4, 9, "20MHz in 40MHz HE PPDU 2.4GHz");
1274 	PRINT_HE_PHY_CAP(4, 10, "20MHz in 160/80+80MHz HE PPDU");
1275 	PRINT_HE_PHY_CAP(4, 11, "80MHz in 160/80+80MHz HE PPDU");
1276 	PRINT_HE_PHY_CAP(4, 12, "HE ER SU PPDU 1x HE-LTF 0.8us GI");
1277 	PRINT_HE_PHY_CAP(4, 13, "Midamble Rx 2x & 1x HE-LTF");
1278 	PRINT_HE_PHY_CAP_MASK(4, 14, 0x3, "DCM Max BW");
1279 
1280 	PRINT_HE_PHY_CAP(5, 0, "Longer Than 16HE SIG-B OFDM Symbols");
1281 	PRINT_HE_PHY_CAP(5, 1, "Non-Triggered CQI Feedback");
1282 	PRINT_HE_PHY_CAP(5, 2, "TX 1024-QAM");
1283 	PRINT_HE_PHY_CAP(5, 3, "RX 1024-QAM");
1284 	PRINT_HE_PHY_CAP(5, 4, "RX Full BW SU Using HE MU PPDU with Compression SIGB");
1285 	PRINT_HE_PHY_CAP(5, 5, "RX Full BW SU Using HE MU PPDU with Non-Compression SIGB");
1286 
1287 	mcs_used = 0;
1288 	for (i = 0; i < 3; i++) {
1289 		__u8 phy_cap_support[] = { BIT(1) | BIT(2), BIT(3), BIT(4) };
1290 		char *bw[] = { "<= 80", "160", "80+80" };
1291 		int j;
1292 
1293 		if ((phy_cap[0] & (phy_cap_support[i] << 8)) == 0)
1294 			continue;
1295 
1296 		/* Supports more, but overflow? Abort. */
1297 		if ((i * 2 + 2) * sizeof(mcs_set[0]) >= mcs_len)
1298 			return;
1299 
1300 		for (j = 0; j < 2; j++) {
1301 			int k;
1302 			printf("%s\t\tHE %s MCS and NSS set %s MHz\n", pre, j ? "TX" : "RX", bw[i]);
1303 			for (k = 0; k < 8; k++) {
1304 				__u16 mcs = mcs_set[(i * 2) + j];
1305 				mcs >>= k * 2;
1306 				mcs &= 0x3;
1307 				printf("%s\t\t\t%d streams: ", pre, k + 1);
1308 				if (mcs == 3)
1309 					printf("not supported\n");
1310 				else
1311 					printf("MCS 0-%d\n", 7 + (mcs * 2));
1312 			}
1313 
1314 		}
1315 		mcs_used += 2 * sizeof(mcs_set[0]);
1316 	}
1317 
1318 	/* Caller didn't provide ppet; infer it, if there's trailing space. */
1319 	if (!ppet) {
1320 		ppet = (const void *)((const __u8 *)mcs_set + mcs_used);
1321 		if (mcs_used < mcs_len)
1322 			ppet_len = mcs_len - mcs_used;
1323 		else
1324 			ppet_len = 0;
1325 	}
1326 
1327 	if (ppet_len && (phy_cap[3] & BIT(15))) {
1328 		printf("%s\t\tPPE Threshold ", pre);
1329 		for (i = 0; i < ppet_len; i++)
1330 			if (ppet[i])
1331 				printf("0x%02x ", ppet[i]);
1332 		printf("\n");
1333 	}
1334 }
1335 
print_iftype_list(const char * name,const char * pfx,struct nlattr * attr)1336 void print_iftype_list(const char *name, const char *pfx, struct nlattr *attr)
1337 {
1338 	struct nlattr *ift;
1339 	int rem;
1340 
1341 	printf("%s:\n", name);
1342 	nla_for_each_nested(ift, attr, rem)
1343 		printf("%s * %s\n", pfx, iftype_name(nla_type(ift)));
1344 }
1345 
print_iftype_line(struct nlattr * attr)1346 void print_iftype_line(struct nlattr *attr)
1347 {
1348 	struct nlattr *ift;
1349 	bool first = true;
1350 	int rem;
1351 
1352 	nla_for_each_nested(ift, attr, rem) {
1353 		if (first)
1354 			first = false;
1355 		else
1356 			printf(", ");
1357 		printf("%s", iftype_name(nla_type(ift)));
1358 	}
1359 }
1360 
print_he_info(struct nlattr * nl_iftype)1361 void print_he_info(struct nlattr *nl_iftype)
1362 {
1363 	struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
1364 	__u16 mac_cap[3] = { 0 };
1365 	__u16 phy_cap[6] = { 0 };
1366 	__u16 mcs_set[6] = { 0 };
1367 	__u8 ppet[25] = { 0 };
1368 	size_t len;
1369 	int mcs_len = 0, ppet_len = 0;
1370 
1371 	nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
1372 		  nla_data(nl_iftype), nla_len(nl_iftype), NULL);
1373 
1374 	if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
1375 		return;
1376 
1377 	printf("\t\tHE Iftypes: ");
1378 	print_iftype_line(tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES]);
1379 	printf("\n");
1380 
1381 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]) {
1382 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]);
1383 		if (len > sizeof(mac_cap))
1384 			len = sizeof(mac_cap);
1385 		memcpy(mac_cap,
1386 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]),
1387 		       len);
1388 	}
1389 
1390 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1391 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1392 
1393 		if (len > sizeof(phy_cap) - 1)
1394 			len = sizeof(phy_cap) - 1;
1395 		memcpy(&((__u8 *)phy_cap)[1],
1396 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1397 		       len);
1398 	}
1399 
1400 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]) {
1401 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]);
1402 		if (len > sizeof(mcs_set))
1403 			len = sizeof(mcs_set);
1404 		memcpy(mcs_set,
1405 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]),
1406 		       len);
1407 		mcs_len = len;
1408 	}
1409 
1410 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]) {
1411 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]);
1412 		if (len > sizeof(ppet))
1413 			len = sizeof(ppet);
1414 		memcpy(ppet,
1415 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]),
1416 		       len);
1417 		ppet_len = len;
1418 	}
1419 
1420 	__print_he_capa(mac_cap, phy_cap, mcs_set, mcs_len, ppet, ppet_len,
1421 			true);
1422 }
1423 
__print_eht_capa(int band,const __u8 * mac_cap,const __u32 * phy_cap,const __u8 * mcs_set,size_t mcs_len,const __u8 * ppet,size_t ppet_len,const __u16 * he_phy_cap,bool indent)1424 static void __print_eht_capa(int band,
1425 			     const __u8 *mac_cap,
1426 			     const __u32 *phy_cap,
1427 			     const __u8 *mcs_set, size_t mcs_len,
1428 			     const __u8 *ppet, size_t ppet_len,
1429 			     const __u16 *he_phy_cap,
1430 			     bool indent)
1431 {
1432 	unsigned int i;
1433 	const char *pre = indent ? "\t" : "";
1434 	const char *mcs[] = { "0-7", "8-9", "10-11", "12-13"};
1435 
1436 	#define PRINT_EHT_CAP(_var, _idx, _bit, _str) \
1437 	do { \
1438 		if (_var[_idx] & BIT(_bit)) \
1439 			printf("%s\t\t\t" _str "\n", pre); \
1440 	} while (0)
1441 
1442 	#define PRINT_EHT_CAP_MASK(_var, _idx, _shift, _mask, _str) \
1443 	do { \
1444 		if ((_var[_idx] >> _shift) & _mask) \
1445 			printf("%s\t\t\t" _str ": %d\n", pre, (_var[_idx] >> _shift) & _mask); \
1446 	} while (0)
1447 
1448 	#define PRINT_EHT_MAC_CAP(...) PRINT_EHT_CAP(mac_cap, __VA_ARGS__)
1449 	#define PRINT_EHT_PHY_CAP(...) PRINT_EHT_CAP(phy_cap, __VA_ARGS__)
1450 	#define PRINT_EHT_PHY_CAP_MASK(...) PRINT_EHT_CAP_MASK(phy_cap, __VA_ARGS__)
1451 
1452 	printf("%s\t\tEHT MAC Capabilities (0x", pre);
1453 	for (i = 0; i < 2; i++)
1454 		printf("%02x", mac_cap[i]);
1455 	printf("):\n");
1456 
1457 	PRINT_EHT_MAC_CAP(0, 0, "NSEP priority access Supported");
1458 	PRINT_EHT_MAC_CAP(0, 1, "EHT OM Control Supported");
1459 	PRINT_EHT_MAC_CAP(0, 2, "Triggered TXOP Sharing Supported");
1460 	PRINT_EHT_MAC_CAP(0, 3, "ARR Supported");
1461 
1462 	printf("%s\t\tEHT PHY Capabilities: (0x", pre);
1463 	for (i = 0; i < 8; i++)
1464 		printf("%02x", ((__u8 *)phy_cap)[i]);
1465 	printf("):\n");
1466 
1467 	PRINT_EHT_PHY_CAP(0, 1, "320MHz in 6GHz Supported");
1468 	PRINT_EHT_PHY_CAP(0, 2, "242-tone RU in BW wider than 20MHz Supported");
1469 	PRINT_EHT_PHY_CAP(0, 3, "NDP With  EHT-LTF And 3.2 µs GI");
1470 	PRINT_EHT_PHY_CAP(0, 4, "Partial Bandwidth UL MU-MIMO");
1471 	PRINT_EHT_PHY_CAP(0, 5, "SU Beamformer");
1472 	PRINT_EHT_PHY_CAP(0, 6, "SU Beamformee");
1473 	PRINT_EHT_PHY_CAP_MASK(0, 7, 0x7, "Beamformee SS (80MHz)");
1474 	PRINT_EHT_PHY_CAP_MASK(0, 10, 0x7, "Beamformee SS (160MHz)");
1475 	PRINT_EHT_PHY_CAP_MASK(0, 13, 0x7, "Beamformee SS (320MHz)");
1476 
1477 	PRINT_EHT_PHY_CAP_MASK(0, 16, 0x7, "Number Of Sounding Dimensions (80MHz)");
1478 	PRINT_EHT_PHY_CAP_MASK(0, 19, 0x7, "Number Of Sounding Dimensions (160MHz)");
1479 	PRINT_EHT_PHY_CAP_MASK(0, 22, 0x7, "Number Of Sounding Dimensions (320MHz)");
1480 	PRINT_EHT_PHY_CAP(0, 25, "Ng = 16 SU Feedback");
1481 	PRINT_EHT_PHY_CAP(0, 26, "Ng = 16 MU Feedback");
1482 	PRINT_EHT_PHY_CAP(0, 27, "Codebook size (4, 2) SU Feedback");
1483 	PRINT_EHT_PHY_CAP(0, 28, "Codebook size (7, 5) MU Feedback");
1484 	PRINT_EHT_PHY_CAP(0, 29, "Triggered SU Beamforming Feedback");
1485 	PRINT_EHT_PHY_CAP(0, 30, "Triggered MU Beamforming Partial BW Feedback");
1486 	PRINT_EHT_PHY_CAP(0, 31, "Triggered CQI Feedback");
1487 
1488 	PRINT_EHT_PHY_CAP(1, 0, "Partial Bandwidth DL MU-MIMO");
1489 	PRINT_EHT_PHY_CAP(1, 1, "PSR-Based SR Support");
1490 	PRINT_EHT_PHY_CAP(1, 2, "Power Boost Factor Support");
1491 	PRINT_EHT_PHY_CAP(1, 3, "EHT MU PPDU With 4 EHT-LTF And 0.8 µs GI");
1492 	PRINT_EHT_PHY_CAP_MASK(1, 4, 0xf, "Max Nc");
1493 	PRINT_EHT_PHY_CAP(1, 8, "Non-Triggered CQI Feedback");
1494 
1495 	PRINT_EHT_PHY_CAP(1, 9, "Tx 1024-QAM And 4096-QAM < 242-tone RU");
1496 	PRINT_EHT_PHY_CAP(1, 10, "Rx 1024-QAM And 4096-QAM < 242-tone RU");
1497 	PRINT_EHT_PHY_CAP(1, 11, "PPE Thresholds Present");
1498 	PRINT_EHT_PHY_CAP_MASK(1, 12, 0x3, "Common Nominal Packet Padding");
1499 	PRINT_EHT_PHY_CAP_MASK(1, 14, 0x1f, "Maximum Number Of Supported EHT-LTFs");
1500 	PRINT_EHT_PHY_CAP_MASK(1, 19, 0xf, "Support of MCS 15");
1501 	PRINT_EHT_PHY_CAP(1, 23, "Support Of EHT DUP In 6 GHz");
1502 	PRINT_EHT_PHY_CAP(1, 24, "Support For 20MHz Rx NDP With Wider Bandwidth");
1503 	PRINT_EHT_PHY_CAP(1, 25, "Non-OFDMA UL MU-MIMO (80MHz)");
1504 	PRINT_EHT_PHY_CAP(1, 26, "Non-OFDMA UL MU-MIMO (160MHz)");
1505 	PRINT_EHT_PHY_CAP(1, 27, "Non-OFDMA UL MU-MIMO (320MHz)");
1506 	PRINT_EHT_PHY_CAP(1, 28, "MU Beamformer (80MHz)");
1507 	PRINT_EHT_PHY_CAP(1, 29, "MU Beamformer (160MHz)");
1508 	PRINT_EHT_PHY_CAP(1, 30, "MU Beamformer (320MHz)");
1509 
1510 	printf("%s\t\tEHT MCS/NSS: (0x", pre);
1511 	for (i = 0; i < mcs_len; i++)
1512 		printf("%02x", ((__u8 *)mcs_set)[i]);
1513 	printf("):\n");
1514 
1515 	if (!(he_phy_cap[0] & ((BIT(2) | BIT(3) | BIT(4)) << 8))){
1516 		for (i = 0; i < 4; i++)
1517 			printf("%s\t\tEHT bw=20 MHz, max NSS for MCS %s: Rx=%u, Tx=%u\n",
1518 			       pre, mcs[i],
1519 			       mcs_set[i] & 0xf, mcs_set[i] >> 4);
1520 	}
1521 
1522 	mcs_set += 4;
1523 	if (he_phy_cap[0] & (BIT(2) << 8)) {
1524 		for (i = 0; i < 3; i++)
1525 			printf("%s\t\tEHT bw <= 80 MHz, max NSS for MCS %s: Rx=%u, Tx=%u\n",
1526 			       pre, mcs[i + 1],
1527 			       mcs_set[i] & 0xf, mcs_set[i] >> 4);
1528 
1529 	}
1530 
1531 	mcs_set += 3;
1532 	if (he_phy_cap[0] & (BIT(3) << 8)) {
1533 		for (i = 0; i < 3; i++)
1534 			printf("%s\t\tEHT bw=160 MHz, max NSS for MCS %s: Rx=%u, Tx=%u\n",
1535 			       pre, mcs[i + 1],
1536 			       mcs_set[i] & 0xf, mcs_set[i] >> 4);
1537 
1538 	}
1539 
1540 	mcs_set += 3;
1541 	if (band == NL80211_BAND_6GHZ && (phy_cap[0] & BIT(1))) {
1542 		for (i = 0; i < 3; i++)
1543 			printf("%s\t\tEHT bw=320 MHz, max NSS for MCS %s: Rx=%u, Tx=%u\n",
1544 			       pre, mcs[i + 1],
1545 			       mcs_set[i] & 0xf, mcs_set[i] >> 4);
1546 
1547 	}
1548 
1549 	if (ppet && ppet_len && (phy_cap[1] & BIT(11))) {
1550 		printf("%s\t\tEHT PPE Thresholds ", pre);
1551 		for (i = 0; i < ppet_len; i++)
1552 			if (ppet[i])
1553 				printf("0x%02x ", ppet[i]);
1554 		printf("\n");
1555 	}
1556 }
1557 
print_eht_info(struct nlattr * nl_iftype,int band)1558 void print_eht_info(struct nlattr *nl_iftype, int band)
1559 {
1560 	struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
1561 	__u8 mac_cap[2] = { 0 };
1562 	__u32 phy_cap[2] = { 0 };
1563 	__u8 mcs_set[13] = { 0 };
1564 	__u8 ppet[31] = { 0 };
1565 	__u16 he_phy_cap[6] = { 0 };
1566 	size_t len, mcs_len = 0, ppet_len = 0;
1567 
1568 	nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
1569 		  nla_data(nl_iftype), nla_len(nl_iftype), NULL);
1570 
1571 	if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
1572 		return;
1573 
1574 	printf("\t\tEHT Iftypes: ");
1575 	print_iftype_line(tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES]);
1576 	printf("\n");
1577 
1578 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC]) {
1579 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC]);
1580 		if (len > sizeof(mac_cap))
1581 			len = sizeof(mac_cap);
1582 		memcpy(mac_cap,
1583 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC]),
1584 		       len);
1585 	}
1586 
1587 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]) {
1588 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]);
1589 
1590 		if (len > sizeof(phy_cap))
1591 			len = sizeof(phy_cap);
1592 
1593 		memcpy(phy_cap,
1594 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY]),
1595 		       len);
1596 	}
1597 
1598 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET]) {
1599 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET]);
1600 		if (len > sizeof(mcs_set))
1601 			len = sizeof(mcs_set);
1602 		memcpy(mcs_set,
1603 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET]),
1604 		       len);
1605 
1606 		// Assume that all parts of the MCS set are present
1607 		mcs_len = sizeof(mcs_set);
1608 	}
1609 
1610 	if (tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE]) {
1611 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE]);
1612 		if (len > sizeof(ppet))
1613 			len = sizeof(ppet);
1614 		memcpy(ppet,
1615 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE]),
1616 		       len);
1617 		ppet_len = len;
1618 	}
1619 
1620 	if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1621 		len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1622 
1623 		if (len > sizeof(he_phy_cap) - 1)
1624 			len = sizeof(he_phy_cap) - 1;
1625 		memcpy(&((__u8 *)he_phy_cap)[1],
1626 		       nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1627 		       len);
1628 	}
1629 
1630 	__print_eht_capa(band, mac_cap, phy_cap, mcs_set, mcs_len, ppet, ppet_len,
1631 			 he_phy_cap, true);
1632 }
1633 
print_he_capability(const uint8_t * ie,int len)1634 void print_he_capability(const uint8_t *ie, int len)
1635 {
1636 	const void *mac_cap, *phy_cap, *mcs_set;
1637 	int mcs_len;
1638 	int i = 0;
1639 
1640 	mac_cap = &ie[i];
1641 	i += 6;
1642 
1643 	phy_cap = &ie[i];
1644 	i += 11;
1645 
1646 	mcs_set = &ie[i];
1647 	mcs_len = len - i;
1648 
1649 	__print_he_capa(mac_cap, (const void *)((const __u8 *)phy_cap - 1),
1650 			mcs_set, mcs_len, NULL, 0, false);
1651 }
1652 
iw_hexdump(const char * prefix,const __u8 * buf,size_t size)1653 void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
1654 {
1655 	size_t i;
1656 
1657 	printf("%s: ", prefix);
1658 	for (i = 0; i < size; i++) {
1659 		if (i && i % 16 == 0)
1660 			printf("\n%s: ", prefix);
1661 		printf("%02x ", buf[i]);
1662 	}
1663 	printf("\n\n");
1664 }
1665 
get_cf1(const struct chanmode * chanmode,unsigned long freq)1666 int get_cf1(const struct chanmode *chanmode, unsigned long freq)
1667 {
1668 	unsigned int cf1 = freq, j;
1669 	unsigned int bw80[] = { 5180, 5260, 5500, 5580, 5660, 5745,
1670 				5955, 6035, 6115, 6195, 6275, 6355,
1671 				6435, 6515, 6595, 6675, 6755, 6835,
1672 				6195, 6995 };
1673 	unsigned int bw160[] = { 5180, 5500, 5955, 6115, 6275, 6435,
1674 				  6595, 6755, 6915 };
1675 
1676 	switch (chanmode->width) {
1677 	case NL80211_CHAN_WIDTH_80:
1678 	        /* setup center_freq1 */
1679 		for (j = 0; j < ARRAY_SIZE(bw80); j++) {
1680 			if (freq >= bw80[j] && freq < bw80[j] + 80)
1681 				break;
1682 		}
1683 
1684 		if (j == ARRAY_SIZE(bw80))
1685 			break;
1686 
1687 		cf1 = bw80[j] + 30;
1688 		break;
1689 	case NL80211_CHAN_WIDTH_160:
1690 		/* setup center_freq1 */
1691 		for (j = 0; j < ARRAY_SIZE(bw160); j++) {
1692 			if (freq >= bw160[j] && freq < bw160[j] + 160)
1693 				break;
1694 		}
1695 
1696 		if (j == ARRAY_SIZE(bw160))
1697 			break;
1698 
1699 		cf1 = bw160[j] + 70;
1700 		break;
1701 	default:
1702 		cf1 = freq + chanmode->freq1_diff;
1703 		break;
1704 	}
1705 
1706 	return cf1;
1707 }
1708 
parse_random_mac_addr(struct nl_msg * msg,char * addrs)1709 int parse_random_mac_addr(struct nl_msg *msg, char *addrs)
1710 {
1711 	char *a_addr, *a_mask, *sep;
1712 	unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
1713 
1714 	if (!*addrs) {
1715 		/* randomise all but the multicast bit */
1716 		NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN,
1717 			"\x00\x00\x00\x00\x00\x00");
1718 		NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
1719 			"\x01\x00\x00\x00\x00\x00");
1720 		return 0;
1721 	}
1722 
1723 	if (*addrs != '=')
1724 		return 1;
1725 
1726 	addrs++;
1727 	sep = strchr(addrs, '/');
1728 	a_addr = addrs;
1729 
1730 	if (!sep)
1731 		return 1;
1732 
1733 	*sep = 0;
1734 	a_mask = sep + 1;
1735 	if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
1736 		return 1;
1737 
1738 	NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1739 	NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
1740 
1741 	return 0;
1742  nla_put_failure:
1743 	return -ENOBUFS;
1744 }
1745