]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
info: macro-ify ext_feat_print()
[thirdparty/iw.git] / util.c
1 #include <ctype.h>
2 #include <netlink/attr.h>
3 #include <errno.h>
4 #include <stdbool.h>
5 #include "iw.h"
6 #include "nl80211.h"
7
8 void mac_addr_n2a(char *mac_addr, const unsigned char *arg)
9 {
10 int i, l;
11
12 l = 0;
13 for (i = 0; i < ETH_ALEN ; i++) {
14 if (i == 0) {
15 sprintf(mac_addr+l, "%02x", arg[i]);
16 l += 2;
17 } else {
18 sprintf(mac_addr+l, ":%02x", arg[i]);
19 l += 3;
20 }
21 }
22 }
23
24 int mac_addr_a2n(unsigned char *mac_addr, char *arg)
25 {
26 int i;
27
28 for (i = 0; i < ETH_ALEN ; i++) {
29 int temp;
30 char *cp = strchr(arg, ':');
31 if (cp) {
32 *cp = 0;
33 cp++;
34 }
35 if (sscanf(arg, "%x", &temp) != 1)
36 return -1;
37 if (temp < 0 || temp > 255)
38 return -1;
39
40 mac_addr[i] = temp;
41 if (!cp)
42 break;
43 arg = cp;
44 }
45 if (i < ETH_ALEN - 1)
46 return -1;
47
48 return 0;
49 }
50
51 int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len,
52 unsigned char **mask)
53 {
54 size_t len = strlen(hexmask) / 2;
55 unsigned char *result_val;
56 unsigned char *result_mask = NULL;
57
58 int pos = 0;
59
60 *result_len = 0;
61
62 result_val = calloc(len + 2, 1);
63 if (!result_val)
64 goto error;
65 *result = result_val;
66 if (mask) {
67 result_mask = calloc(DIV_ROUND_UP(len, 8) + 2, 1);
68 if (!result_mask)
69 goto error;
70 *mask = result_mask;
71 }
72
73 while (1) {
74 char *cp = strchr(hexmask, ':');
75 if (cp) {
76 *cp = 0;
77 cp++;
78 }
79
80 if (result_mask && (strcmp(hexmask, "-") == 0 ||
81 strcmp(hexmask, "xx") == 0 ||
82 strcmp(hexmask, "--") == 0)) {
83 /* skip this byte and leave mask bit unset */
84 } else {
85 int temp, mask_pos;
86 char *end;
87
88 temp = strtoul(hexmask, &end, 16);
89 if (*end)
90 goto error;
91 if (temp < 0 || temp > 255)
92 goto error;
93 result_val[pos] = temp;
94
95 mask_pos = pos / 8;
96 if (result_mask)
97 result_mask[mask_pos] |= 1 << (pos % 8);
98 }
99
100 (*result_len)++;
101 pos++;
102
103 if (!cp)
104 break;
105 hexmask = cp;
106 }
107
108 return 0;
109 error:
110 free(result_val);
111 free(result_mask);
112 return -1;
113 }
114
115 unsigned char *parse_hex(char *hex, size_t *outlen)
116 {
117 unsigned char *result;
118
119 if (parse_hex_mask(hex, &result, outlen, NULL))
120 return NULL;
121 return result;
122 }
123
124 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
125 "unspecified",
126 "IBSS",
127 "managed",
128 "AP",
129 "AP/VLAN",
130 "WDS",
131 "monitor",
132 "mesh point",
133 "P2P-client",
134 "P2P-GO",
135 "P2P-device",
136 "outside context of a BSS",
137 "NAN",
138 };
139
140 static char modebuf[100];
141
142 const char *iftype_name(enum nl80211_iftype iftype)
143 {
144 if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
145 return ifmodes[iftype];
146 sprintf(modebuf, "Unknown mode (%d)", iftype);
147 return modebuf;
148 }
149
150 static const char *commands[NL80211_CMD_MAX + 1] = {
151 /*
152 * sed 's%^\tNL80211_CMD_%%;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h | grep -v "reserved"
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_GET_REG] = "get_reg",
185 [NL80211_CMD_GET_SCAN] = "get_scan",
186 [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan",
187 [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results",
188 [NL80211_CMD_SCAN_ABORTED] = "scan_aborted",
189 [NL80211_CMD_REG_CHANGE] = "reg_change",
190 [NL80211_CMD_AUTHENTICATE] = "authenticate",
191 [NL80211_CMD_ASSOCIATE] = "associate",
192 [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate",
193 [NL80211_CMD_DISASSOCIATE] = "disassociate",
194 [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure",
195 [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint",
196 [NL80211_CMD_JOIN_IBSS] = "join_ibss",
197 [NL80211_CMD_LEAVE_IBSS] = "leave_ibss",
198 [NL80211_CMD_TESTMODE] = "testmode",
199 [NL80211_CMD_CONNECT] = "connect",
200 [NL80211_CMD_ROAM] = "roam",
201 [NL80211_CMD_DISCONNECT] = "disconnect",
202 [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns",
203 [NL80211_CMD_GET_SURVEY] = "get_survey",
204 [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results",
205 [NL80211_CMD_SET_PMKSA] = "set_pmksa",
206 [NL80211_CMD_DEL_PMKSA] = "del_pmksa",
207 [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa",
208 [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel",
209 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel",
210 [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask",
211 [NL80211_CMD_REGISTER_FRAME] = "register_frame",
212 [NL80211_CMD_FRAME] = "frame",
213 [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status",
214 [NL80211_CMD_SET_POWER_SAVE] = "set_power_save",
215 [NL80211_CMD_GET_POWER_SAVE] = "get_power_save",
216 [NL80211_CMD_SET_CQM] = "set_cqm",
217 [NL80211_CMD_NOTIFY_CQM] = "notify_cqm",
218 [NL80211_CMD_SET_CHANNEL] = "set_channel",
219 [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer",
220 [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel",
221 [NL80211_CMD_JOIN_MESH] = "join_mesh",
222 [NL80211_CMD_LEAVE_MESH] = "leave_mesh",
223 [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate",
224 [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate",
225 [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate",
226 [NL80211_CMD_GET_WOWLAN] = "get_wowlan",
227 [NL80211_CMD_SET_WOWLAN] = "set_wowlan",
228 [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan",
229 [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan",
230 [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results",
231 [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped",
232 [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload",
233 [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate",
234 [NL80211_CMD_TDLS_OPER] = "tdls_oper",
235 [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt",
236 [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame",
237 [NL80211_CMD_PROBE_CLIENT] = "probe_client",
238 [NL80211_CMD_REGISTER_BEACONS] = "register_beacons",
239 [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame",
240 [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map",
241 [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify",
242 [NL80211_CMD_START_P2P_DEVICE] = "start_p2p_device",
243 [NL80211_CMD_STOP_P2P_DEVICE] = "stop_p2p_device",
244 [NL80211_CMD_CONN_FAILED] = "conn_failed",
245 [NL80211_CMD_SET_MCAST_RATE] = "set_mcast_rate",
246 [NL80211_CMD_SET_MAC_ACL] = "set_mac_acl",
247 [NL80211_CMD_RADAR_DETECT] = "radar_detect",
248 [NL80211_CMD_GET_PROTOCOL_FEATURES] = "get_protocol_features",
249 [NL80211_CMD_UPDATE_FT_IES] = "update_ft_ies",
250 [NL80211_CMD_FT_EVENT] = "ft_event",
251 [NL80211_CMD_CRIT_PROTOCOL_START] = "crit_protocol_start",
252 [NL80211_CMD_CRIT_PROTOCOL_STOP] = "crit_protocol_stop",
253 [NL80211_CMD_GET_COALESCE] = "get_coalesce",
254 [NL80211_CMD_SET_COALESCE] = "set_coalesce",
255 [NL80211_CMD_CHANNEL_SWITCH] = "channel_switch",
256 [NL80211_CMD_VENDOR] = "vendor",
257 [NL80211_CMD_SET_QOS_MAP] = "set_qos_map",
258 [NL80211_CMD_ADD_TX_TS] = "add_tx_ts",
259 [NL80211_CMD_DEL_TX_TS] = "del_tx_ts",
260 [NL80211_CMD_GET_MPP] = "get_mpp",
261 [NL80211_CMD_JOIN_OCB] = "join_ocb",
262 [NL80211_CMD_LEAVE_OCB] = "leave_ocb",
263 [NL80211_CMD_CH_SWITCH_STARTED_NOTIFY] = "ch_switch_started_notify",
264 [NL80211_CMD_TDLS_CHANNEL_SWITCH] = "tdls_channel_switch",
265 [NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH] = "tdls_cancel_channel_switch",
266 [NL80211_CMD_WIPHY_REG_CHANGE] = "wiphy_reg_change",
267 [NL80211_CMD_ABORT_SCAN] = "abort_scan",
268 [NL80211_CMD_START_NAN] = "start_nan",
269 [NL80211_CMD_STOP_NAN] = "stop_nan",
270 [NL80211_CMD_ADD_NAN_FUNCTION] = "add_nan_function",
271 [NL80211_CMD_DEL_NAN_FUNCTION] = "del_nan_function",
272 [NL80211_CMD_CHANGE_NAN_CONFIG] = "change_nan_config",
273 [NL80211_CMD_NAN_MATCH] = "nan_match",
274 [NL80211_CMD_SET_MULTICAST_TO_UNICAST] = "set_multicast_to_unicast",
275 [NL80211_CMD_UPDATE_CONNECT_PARAMS] = "update_connect_params",
276 [NL80211_CMD_SET_PMK] = "set_pmk",
277 [NL80211_CMD_DEL_PMK] = "del_pmk",
278 [NL80211_CMD_PORT_AUTHORIZED] = "port_authorized",
279 [NL80211_CMD_RELOAD_REGDB] = "reload_regdb",
280 [NL80211_CMD_EXTERNAL_AUTH] = "external_auth",
281 [NL80211_CMD_STA_OPMODE_CHANGED] = "sta_opmode_changed",
282 [NL80211_CMD_CONTROL_PORT_FRAME] = "control_port_frame",
283 [NL80211_CMD_GET_FTM_RESPONDER_STATS] = "get_ftm_responder_stats",
284 [NL80211_CMD_PEER_MEASUREMENT_START] = "peer_measurement_start",
285 [NL80211_CMD_PEER_MEASUREMENT_RESULT] = "peer_measurement_result",
286 [NL80211_CMD_PEER_MEASUREMENT_COMPLETE] = "peer_measurement_complete",
287 [NL80211_CMD_NOTIFY_RADAR] = "notify_radar",
288 [NL80211_CMD_UPDATE_OWE_INFO] = "update_owe_info",
289 [NL80211_CMD_PROBE_MESH_LINK] = "probe_mesh_link",
290 };
291
292 static char cmdbuf[100];
293
294 const char *command_name(enum nl80211_commands cmd)
295 {
296 if (cmd <= NL80211_CMD_MAX && commands[cmd])
297 return commands[cmd];
298 sprintf(cmdbuf, "Unknown command (%d)", cmd);
299 return cmdbuf;
300 }
301
302 int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
303 {
304 /* see 802.11 17.3.8.3.2 and Annex J
305 * there are overlapping channel numbers in 5GHz and 2GHz bands */
306 if (chan <= 0)
307 return 0; /* not supported */
308 switch (band) {
309 case NL80211_BAND_2GHZ:
310 if (chan == 14)
311 return 2484;
312 else if (chan < 14)
313 return 2407 + chan * 5;
314 break;
315 case NL80211_BAND_5GHZ:
316 if (chan >= 182 && chan <= 196)
317 return 4000 + chan * 5;
318 else
319 return 5000 + chan * 5;
320 break;
321 case NL80211_BAND_60GHZ:
322 if (chan < 5)
323 return 56160 + chan * 2160;
324 break;
325 default:
326 ;
327 }
328 return 0; /* not supported */
329 }
330
331 int ieee80211_frequency_to_channel(int freq)
332 {
333 /* see 802.11-2007 17.3.8.3.2 and Annex J */
334 if (freq == 2484)
335 return 14;
336 else if (freq < 2484)
337 return (freq - 2407) / 5;
338 else if (freq >= 4910 && freq <= 4980)
339 return (freq - 4000) / 5;
340 else if (freq <= 45000) /* DMG band lower limit */
341 return (freq - 5000) / 5;
342 else if (freq >= 58320 && freq <= 64800)
343 return (freq - 56160) / 2160;
344 else
345 return 0;
346 }
347
348 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
349 {
350 int i;
351
352 for (i = 0; i < len; i++) {
353 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
354 printf("%c", data[i]);
355 else if (data[i] == ' ' &&
356 (i != 0 && i != len -1))
357 printf(" ");
358 else
359 printf("\\x%.2x", data[i]);
360 }
361 }
362
363 static int hex2num(char digit)
364 {
365 if (!isxdigit(digit))
366 return -1;
367 if (isdigit(digit))
368 return digit - '0';
369 return tolower(digit) - 'a' + 10;
370 }
371
372 static int hex2byte(const char *hex)
373 {
374 int d1, d2;
375
376 d1 = hex2num(hex[0]);
377 if (d1 < 0)
378 return -1;
379 d2 = hex2num(hex[1]);
380 if (d2 < 0)
381 return -1;
382 return (d1 << 4) | d2;
383 }
384
385 char *hex2bin(const char *hex, char *buf)
386 {
387 char *result = buf;
388 int d;
389
390 while (hex[0]) {
391 d = hex2byte(hex);
392 if (d < 0)
393 return NULL;
394 buf[0] = d;
395 buf++;
396 hex += 2;
397 }
398
399 return result;
400 }
401
402 static int parse_akm_suite(const char *cipher_str)
403 {
404
405 if (!strcmp(cipher_str, "PSK"))
406 return 0x000FAC02;
407 if (!strcmp(cipher_str, "FT/PSK"))
408 return 0x000FAC03;
409 if (!strcmp(cipher_str, "PSK/SHA-256"))
410 return 0x000FAC06;
411 return -EINVAL;
412 }
413
414 static int parse_cipher_suite(const char *cipher_str)
415 {
416
417 if (!strcmp(cipher_str, "TKIP"))
418 return 0x000FAC02;
419 if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128"))
420 return 0x000FAC04;
421 if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128"))
422 return 0x000FAC08;
423 if (!strcmp(cipher_str, "GCMP-256"))
424 return 0x000FAC09;
425 if (!strcmp(cipher_str, "CCMP-256"))
426 return 0x000FAC0A;
427 return -EINVAL;
428 }
429
430 int parse_keys(struct nl_msg *msg, char **argv[], int *argc)
431 {
432 struct nlattr *keys;
433 int i = 0;
434 bool have_default = false;
435 char *arg = **argv;
436 char keybuf[13];
437 int pos = 0;
438
439 if (!*argc)
440 return 1;
441
442 if (!memcmp(&arg[pos], "psk", 3)) {
443 char psk_keybuf[32];
444 int cipher_suite, akm_suite;
445
446 if (*argc < 4)
447 goto explain;
448
449 pos+=3;
450 if (arg[pos] != ':')
451 goto explain;
452 pos++;
453
454 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, NL80211_WPA_VERSION_2);
455
456 if (strlen(&arg[pos]) != (sizeof(psk_keybuf) * 2) || !hex2bin(&arg[pos], psk_keybuf)) {
457 printf("Bad PSK\n");
458 return -EINVAL;
459 }
460
461 NLA_PUT(msg, NL80211_ATTR_PMK, 32, psk_keybuf);
462 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM);
463
464 *argv += 1;
465 *argc -= 1;
466 arg = **argv;
467
468 akm_suite = parse_akm_suite(arg);
469 if (akm_suite < 0)
470 goto explain;
471
472 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, akm_suite);
473
474 *argv += 1;
475 *argc -= 1;
476 arg = **argv;
477
478 cipher_suite = parse_cipher_suite(arg);
479 if (cipher_suite < 0)
480 goto explain;
481
482 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher_suite);
483
484 *argv += 1;
485 *argc -= 1;
486 arg = **argv;
487
488 cipher_suite = parse_cipher_suite(arg);
489 if (cipher_suite < 0)
490 goto explain;
491
492 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher_suite);
493
494 *argv += 1;
495 *argc -= 1;
496 return 0;
497 }
498
499 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
500
501 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
502 if (!keys)
503 return -ENOBUFS;
504
505 do {
506 int keylen;
507 struct nlattr *key = nla_nest_start(msg, ++i);
508 char *keydata;
509
510 arg = **argv;
511 pos = 0;
512
513 if (!key)
514 return -ENOBUFS;
515
516 if (arg[pos] == 'd') {
517 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
518 pos++;
519 if (arg[pos] == ':')
520 pos++;
521 have_default = true;
522 }
523
524 if (!isdigit(arg[pos]))
525 goto explain;
526 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
527 if (arg[pos++] != ':')
528 goto explain;
529 keydata = arg + pos;
530 switch (strlen(keydata)) {
531 case 10:
532 keydata = hex2bin(keydata, keybuf);
533 /* fall through */
534 case 5:
535 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
536 keylen = 5;
537 break;
538 case 26:
539 keydata = hex2bin(keydata, keybuf);
540 /* fall through */
541 case 13:
542 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
543 keylen = 13;
544 break;
545 default:
546 goto explain;
547 }
548
549 if (!keydata)
550 goto explain;
551
552 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
553
554 *argv += 1;
555 *argc -= 1;
556
557 /* one key should be TX key */
558 if (!have_default && !*argc)
559 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
560
561 nla_nest_end(msg, key);
562 } while (*argc);
563
564 nla_nest_end(msg, keys);
565
566 return 0;
567 nla_put_failure:
568 return -ENOBUFS;
569 explain:
570 fprintf(stderr, "key must be [d:]index:data where\n"
571 " 'd:' means default (transmit) key\n"
572 " 'index:' is a single digit (0-3)\n"
573 " 'data' must be 5 or 13 ascii chars\n"
574 " or 10 or 26 hex digits\n"
575 "for example: d:2:6162636465 is the same as d:2:abcde\n"
576 "or psk:data <AKM Suite> <pairwise CIPHER> <groupwise CIPHER> where\n"
577 " 'data' is the PSK (output of wpa_passphrase and the CIPHER can be CCMP or GCMP\n"
578 "for example: psk:0123456789abcdef PSK CCMP CCMP\n"
579 "The allowed AKM suites are PSK, FT/PSK, PSK/SHA-256\n"
580 "The allowed Cipher suites are TKIP, CCMP, GCMP, GCMP-256, CCMP-256\n");
581 return 2;
582 }
583
584 enum nl80211_chan_width str_to_bw(const char *str)
585 {
586 static const struct {
587 const char *name;
588 unsigned int val;
589 } bwmap[] = {
590 { .name = "5", .val = NL80211_CHAN_WIDTH_5, },
591 { .name = "10", .val = NL80211_CHAN_WIDTH_10, },
592 { .name = "20", .val = NL80211_CHAN_WIDTH_20, },
593 { .name = "40", .val = NL80211_CHAN_WIDTH_40, },
594 { .name = "80", .val = NL80211_CHAN_WIDTH_80, },
595 { .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, },
596 { .name = "160", .val = NL80211_CHAN_WIDTH_160, },
597 };
598 unsigned int i;
599
600 for (i = 0; i < ARRAY_SIZE(bwmap); i++) {
601 if (strcasecmp(bwmap[i].name, str) == 0)
602 return bwmap[i].val;
603 }
604
605 return NL80211_CHAN_WIDTH_20_NOHT;
606 }
607
608 static int parse_freqs(struct chandef *chandef, int argc, char **argv,
609 int *parsed)
610 {
611 uint32_t freq;
612 char *end;
613 bool need_cf1 = false, need_cf2 = false;
614
615 if (argc < 1)
616 return 0;
617
618 chandef->width = str_to_bw(argv[0]);
619
620 switch (chandef->width) {
621 case NL80211_CHAN_WIDTH_20_NOHT:
622 /* First argument was not understood, give up gracefully. */
623 return 0;
624 case NL80211_CHAN_WIDTH_20:
625 case NL80211_CHAN_WIDTH_5:
626 case NL80211_CHAN_WIDTH_10:
627 break;
628 case NL80211_CHAN_WIDTH_80P80:
629 need_cf2 = true;
630 /* fall through */
631 case NL80211_CHAN_WIDTH_40:
632 case NL80211_CHAN_WIDTH_80:
633 case NL80211_CHAN_WIDTH_160:
634 need_cf1 = true;
635 break;
636 }
637
638 *parsed += 1;
639
640 if (!need_cf1)
641 return 0;
642
643 if (argc < 2)
644 return 1;
645
646 /* center freq 1 */
647 if (!*argv[1])
648 return 1;
649 freq = strtoul(argv[1], &end, 10);
650 if (*end)
651 return 1;
652 *parsed += 1;
653
654 chandef->center_freq1 = freq;
655
656 if (!need_cf2)
657 return 0;
658
659 if (argc < 3)
660 return 1;
661
662 /* center freq 2 */
663 if (!*argv[2])
664 return 1;
665 freq = strtoul(argv[2], &end, 10);
666 if (*end)
667 return 1;
668 chandef->center_freq2 = freq;
669
670 *parsed += 1;
671
672 return 0;
673 }
674
675
676 /**
677 * parse_freqchan - Parse frequency or channel definition
678 *
679 * @chandef: chandef structure to be filled in
680 * @chan: Boolean whether to parse a channel or frequency based specifier
681 * @argc: Number of arguments
682 * @argv: Array of string arguments
683 * @parsed: Pointer to return the number of used arguments, or NULL to error
684 * out if any argument is left unused.
685 *
686 * The given chandef structure will be filled in from the command line
687 * arguments. argc/argv will be updated so that further arguments from the
688 * command line can be parsed.
689 *
690 * Note that despite the fact that the function knows how many center freqs
691 * are needed, there's an ambiguity if the next argument after this is an
692 * integer argument, since the valid channel width values are interpreted
693 * as such, rather than a following argument. This can be avoided by the
694 * user by giving "NOHT" instead.
695 *
696 * The working specifier if chan is set are:
697 * <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
698 *
699 * And if frequency is set:
700 * <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
701 * <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
702 *
703 * If the mode/channel width is not given the NOHT is assumed.
704 *
705 * Return: Number of used arguments, zero or negative error number otherwise
706 */
707 int parse_freqchan(struct chandef *chandef, bool chan, int argc, char **argv,
708 int *parsed)
709 {
710 char *end;
711 static const struct chanmode chanmode[] = {
712 { .name = "HT20",
713 .width = NL80211_CHAN_WIDTH_20,
714 .freq1_diff = 0,
715 .chantype = NL80211_CHAN_HT20 },
716 { .name = "HT40+",
717 .width = NL80211_CHAN_WIDTH_40,
718 .freq1_diff = 10,
719 .chantype = NL80211_CHAN_HT40PLUS },
720 { .name = "HT40-",
721 .width = NL80211_CHAN_WIDTH_40,
722 .freq1_diff = -10,
723 .chantype = NL80211_CHAN_HT40MINUS },
724 { .name = "NOHT",
725 .width = NL80211_CHAN_WIDTH_20_NOHT,
726 .freq1_diff = 0,
727 .chantype = NL80211_CHAN_NO_HT },
728 { .name = "5MHz",
729 .width = NL80211_CHAN_WIDTH_5,
730 .freq1_diff = 0,
731 .chantype = -1 },
732 { .name = "10MHz",
733 .width = NL80211_CHAN_WIDTH_10,
734 .freq1_diff = 0,
735 .chantype = -1 },
736 { .name = "80MHz",
737 .width = NL80211_CHAN_WIDTH_80,
738 .freq1_diff = 0,
739 .chantype = -1 },
740 };
741 const struct chanmode *chanmode_selected = NULL;
742 unsigned int freq;
743 unsigned int i;
744 int _parsed = 0;
745 int res = 0;
746
747 if (argc < 1)
748 return 1;
749
750 if (!argv[0])
751 goto out;
752 freq = strtoul(argv[0], &end, 10);
753 if (*end) {
754 res = 1;
755 goto out;
756 }
757
758 _parsed += 1;
759
760 memset(chandef, 0, sizeof(struct chandef));
761
762 if (chan) {
763 enum nl80211_band band;
764
765 band = freq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
766 freq = ieee80211_channel_to_frequency(freq, band);
767 }
768 chandef->control_freq = freq;
769 /* Assume 20MHz NOHT channel for now. */
770 chandef->center_freq1 = freq;
771
772 /* Try to parse HT mode definitions */
773 if (argc > 1) {
774 for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
775 if (strcasecmp(chanmode[i].name, argv[1]) == 0) {
776 chanmode_selected = &chanmode[i];
777 _parsed += 1;
778 break;
779 }
780 }
781 }
782
783 /* channel mode given, use it and return. */
784 if (chanmode_selected) {
785 chandef->center_freq1 = get_cf1(chanmode_selected, freq);
786 chandef->width = chanmode_selected->width;
787 goto out;
788 }
789
790 /* This was a only a channel definition, nothing further may follow. */
791 if (chan)
792 goto out;
793
794 res = parse_freqs(chandef, argc - 1, argv + 1, &_parsed);
795
796 out:
797 /* Error out if parsed is NULL. */
798 if (!parsed && _parsed != argc)
799 return 1;
800
801 if (parsed)
802 *parsed = _parsed;
803
804 return res;
805 }
806
807 int put_chandef(struct nl_msg *msg, struct chandef *chandef)
808 {
809 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chandef->control_freq);
810 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width);
811
812 switch (chandef->width) {
813 case NL80211_CHAN_WIDTH_20_NOHT:
814 NLA_PUT_U32(msg,
815 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
816 NL80211_CHAN_NO_HT);
817 break;
818 case NL80211_CHAN_WIDTH_20:
819 NLA_PUT_U32(msg,
820 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
821 NL80211_CHAN_HT20);
822 break;
823 case NL80211_CHAN_WIDTH_40:
824 if (chandef->control_freq > chandef->center_freq1)
825 NLA_PUT_U32(msg,
826 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
827 NL80211_CHAN_HT40MINUS);
828 else
829 NLA_PUT_U32(msg,
830 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
831 NL80211_CHAN_HT40PLUS);
832 break;
833 default:
834 break;
835 }
836
837 if (chandef->center_freq1)
838 NLA_PUT_U32(msg,
839 NL80211_ATTR_CENTER_FREQ1,
840 chandef->center_freq1);
841
842 if (chandef->center_freq2)
843 NLA_PUT_U32(msg,
844 NL80211_ATTR_CENTER_FREQ2,
845 chandef->center_freq2);
846
847 return 0;
848
849 nla_put_failure:
850 return -ENOBUFS;
851 }
852
853 static void print_mcs_index(const __u8 *mcs)
854 {
855 int mcs_bit, prev_bit = -2, prev_cont = 0;
856
857 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
858 unsigned int mcs_octet = mcs_bit/8;
859 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
860 bool mcs_rate_idx_set;
861
862 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
863
864 if (!mcs_rate_idx_set)
865 continue;
866
867 if (prev_bit != mcs_bit - 1) {
868 if (prev_bit != -2)
869 printf("%d, ", prev_bit);
870 else
871 printf(" ");
872 printf("%d", mcs_bit);
873 prev_cont = 0;
874 } else if (!prev_cont) {
875 printf("-");
876 prev_cont = 1;
877 }
878
879 prev_bit = mcs_bit;
880 }
881
882 if (prev_cont)
883 printf("%d", prev_bit);
884 printf("\n");
885 }
886
887 /*
888 * There are only 4 possible values, we just use a case instead of computing it,
889 * but technically this can also be computed through the formula:
890 *
891 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
892 */
893 static __u32 compute_ampdu_length(__u8 exponent)
894 {
895 switch (exponent) {
896 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
897 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
898 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
899 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
900 default: return 0;
901 }
902 }
903
904 static const char *print_ampdu_space(__u8 space)
905 {
906 switch (space) {
907 case 0: return "No restriction";
908 case 1: return "1/4 usec";
909 case 2: return "1/2 usec";
910 case 3: return "1 usec";
911 case 4: return "2 usec";
912 case 5: return "4 usec";
913 case 6: return "8 usec";
914 case 7: return "16 usec";
915 default:
916 return "BUG (spacing more than 3 bits!)";
917 }
918 }
919
920 void print_ampdu_length(__u8 exponent)
921 {
922 __u32 max_ampdu_length;
923
924 max_ampdu_length = compute_ampdu_length(exponent);
925
926 if (max_ampdu_length) {
927 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
928 max_ampdu_length, exponent);
929 } else {
930 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
931 "(exponent: %d)\n", exponent);
932 }
933 }
934
935 void print_ampdu_spacing(__u8 spacing)
936 {
937 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
938 print_ampdu_space(spacing), spacing);
939 }
940
941 void print_ht_capability(__u16 cap)
942 {
943 #define PRINT_HT_CAP(_cond, _str) \
944 do { \
945 if (_cond) \
946 printf("\t\t\t" _str "\n"); \
947 } while (0)
948
949 printf("\t\tCapabilities: 0x%02x\n", cap);
950
951 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
952 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
953 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
954
955 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
956 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
957 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
958
959 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
960 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
961 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
962 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
963
964 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
965 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
966 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
967 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
968
969 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
970
971 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
972 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
973
974 /*
975 * For beacons and probe response this would mean the BSS
976 * does or does not allow the usage of DSSS/CCK HT40.
977 * Otherwise it means the STA does or does not use
978 * DSSS/CCK HT40.
979 */
980 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
981 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
982
983 /* BIT(13) is reserved */
984
985 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
986
987 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
988 #undef PRINT_HT_CAP
989 }
990
991 void print_ht_mcs(const __u8 *mcs)
992 {
993 /* As defined in 7.3.2.57.4 Supported MCS Set field */
994 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
995 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
996
997 max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
998 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
999 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
1000 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
1001 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
1002
1003 if (max_rx_supp_data_rate)
1004 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
1005 /* XXX: else see 9.6.0e.5.3 how to get this I think */
1006
1007 if (tx_mcs_set_defined) {
1008 if (tx_mcs_set_equal) {
1009 printf("\t\tHT TX/RX MCS rate indexes supported:");
1010 print_mcs_index(mcs);
1011 } else {
1012 printf("\t\tHT RX MCS rate indexes supported:");
1013 print_mcs_index(mcs);
1014
1015 if (tx_unequal_modulation)
1016 printf("\t\tTX unequal modulation supported\n");
1017 else
1018 printf("\t\tTX unequal modulation not supported\n");
1019
1020 printf("\t\tHT TX Max spatial streams: %d\n",
1021 tx_max_num_spatial_streams);
1022
1023 printf("\t\tHT TX MCS rate indexes supported may differ\n");
1024 }
1025 } else {
1026 printf("\t\tHT RX MCS rate indexes supported:");
1027 print_mcs_index(mcs);
1028 printf("\t\tHT TX MCS rate indexes are undefined\n");
1029 }
1030 }
1031
1032 void print_vht_info(__u32 capa, const __u8 *mcs)
1033 {
1034 __u16 tmp;
1035 int i;
1036
1037 printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
1038
1039 #define PRINT_VHT_CAPA(_bit, _str) \
1040 do { \
1041 if (capa & BIT(_bit)) \
1042 printf("\t\t\t" _str "\n"); \
1043 } while (0)
1044
1045 printf("\t\t\tMax MPDU length: ");
1046 switch (capa & 3) {
1047 case 0: printf("3895\n"); break;
1048 case 1: printf("7991\n"); break;
1049 case 2: printf("11454\n"); break;
1050 case 3: printf("(reserved)\n");
1051 }
1052 printf("\t\t\tSupported Channel Width: ");
1053 switch ((capa >> 2) & 3) {
1054 case 0: printf("neither 160 nor 80+80\n"); break;
1055 case 1: printf("160 MHz\n"); break;
1056 case 2: printf("160 MHz, 80+80 MHz\n"); break;
1057 case 3: printf("(reserved)\n");
1058 }
1059 PRINT_VHT_CAPA(4, "RX LDPC");
1060 PRINT_VHT_CAPA(5, "short GI (80 MHz)");
1061 PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
1062 PRINT_VHT_CAPA(7, "TX STBC");
1063 /* RX STBC */
1064 PRINT_VHT_CAPA(11, "SU Beamformer");
1065 PRINT_VHT_CAPA(12, "SU Beamformee");
1066 /* compressed steering */
1067 /* # of sounding dimensions */
1068 PRINT_VHT_CAPA(19, "MU Beamformer");
1069 PRINT_VHT_CAPA(20, "MU Beamformee");
1070 PRINT_VHT_CAPA(21, "VHT TXOP PS");
1071 PRINT_VHT_CAPA(22, "+HTC-VHT");
1072 /* max A-MPDU */
1073 /* VHT link adaptation */
1074 PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
1075 PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
1076
1077 printf("\t\tVHT RX MCS set:\n");
1078 tmp = mcs[0] | (mcs[1] << 8);
1079 for (i = 1; i <= 8; i++) {
1080 printf("\t\t\t%d streams: ", i);
1081 switch ((tmp >> ((i-1)*2) ) & 3) {
1082 case 0: printf("MCS 0-7\n"); break;
1083 case 1: printf("MCS 0-8\n"); break;
1084 case 2: printf("MCS 0-9\n"); break;
1085 case 3: printf("not supported\n"); break;
1086 }
1087 }
1088 tmp = mcs[2] | (mcs[3] << 8);
1089 printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
1090
1091 printf("\t\tVHT TX MCS set:\n");
1092 tmp = mcs[4] | (mcs[5] << 8);
1093 for (i = 1; i <= 8; i++) {
1094 printf("\t\t\t%d streams: ", i);
1095 switch ((tmp >> ((i-1)*2) ) & 3) {
1096 case 0: printf("MCS 0-7\n"); break;
1097 case 1: printf("MCS 0-8\n"); break;
1098 case 2: printf("MCS 0-9\n"); break;
1099 case 3: printf("not supported\n"); break;
1100 }
1101 }
1102 tmp = mcs[6] | (mcs[7] << 8);
1103 printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
1104 }
1105
1106 void print_he_info(struct nlattr *nl_iftype)
1107 {
1108 struct nlattr *tb[NL80211_BAND_IFTYPE_ATTR_MAX + 1];
1109 struct nlattr *tb_flags[NL80211_IFTYPE_MAX + 1];
1110 char *iftypes[NUM_NL80211_IFTYPES] = {
1111 "Unspec", "Adhoc", "Station", "AP", "AP/VLAN", "WDS", "Monitor",
1112 "Mesh", "P2P/Client", "P2P/Go", "P2P/Device", "OCB", "NAN",
1113 };
1114 __u16 mac_cap[3] = { 0 };
1115 __u16 phy_cap[6] = { 0 };
1116 __u16 mcs_set[6] = { 0 };
1117 __u8 ppet[25] = { 0 };
1118 size_t len;
1119 int i;
1120
1121 #define PRINT_HE_CAP(_var, _idx, _bit, _str) \
1122 do { \
1123 if (_var[_idx] & BIT(_bit)) \
1124 printf("\t\t\t\t" _str "\n"); \
1125 } while (0)
1126
1127 #define PRINT_HE_CAP_MASK(_var, _idx, _shift, _mask, _str) \
1128 do { \
1129 if ((_var[_idx] >> _shift) & _mask) \
1130 printf("\t\t\t\t" _str ": %d\n", (_var[_idx] >> _shift) & _mask); \
1131 } while (0)
1132
1133 #define PRINT_HE_MAC_CAP(...) PRINT_HE_CAP(mac_cap, __VA_ARGS__)
1134 #define PRINT_HE_MAC_CAP_MASK(...) PRINT_HE_CAP_MASK(mac_cap, __VA_ARGS__)
1135 #define PRINT_HE_PHY_CAP(...) PRINT_HE_CAP(phy_cap, __VA_ARGS__)
1136 #define PRINT_HE_PHY_CAP0(_idx, _bit, ...) PRINT_HE_CAP(phy_cap, _idx, _bit + 8, __VA_ARGS__)
1137 #define PRINT_HE_PHY_CAP_MASK(...) PRINT_HE_CAP_MASK(phy_cap, __VA_ARGS__)
1138
1139 nla_parse(tb, NL80211_BAND_IFTYPE_ATTR_MAX,
1140 nla_data(nl_iftype), nla_len(nl_iftype), NULL);
1141
1142 if (!tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES])
1143 return;
1144
1145 if (nla_parse_nested(tb_flags, NL80211_IFTYPE_MAX,
1146 tb[NL80211_BAND_IFTYPE_ATTR_IFTYPES], NULL))
1147 return;
1148
1149 printf("\t\tHE Iftypes:");
1150 for (i = 0; i < NUM_NL80211_IFTYPES; i++)
1151 if (nla_get_flag(tb_flags[i]) && iftypes[i])
1152 printf(" %s", iftypes[i]);
1153 printf("\n");
1154
1155 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]) {
1156 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]);
1157 if (len > sizeof(mac_cap))
1158 len = sizeof(mac_cap);
1159 memcpy(mac_cap,
1160 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC]),
1161 len);
1162 }
1163 printf("\t\t\tHE MAC Capabilities (0x");
1164 for (i = 0; i < 3; i++)
1165 printf("%04x", mac_cap[i]);
1166 printf("):\n");
1167
1168 PRINT_HE_MAC_CAP(0, 0, "+HTC HE Supported");
1169 PRINT_HE_MAC_CAP(0, 1, "TWT Requester");
1170 PRINT_HE_MAC_CAP(0, 2, "TWT Responder");
1171 PRINT_HE_MAC_CAP_MASK(0, 3, 0x3, "Dynamic BA Fragementation Level");
1172 PRINT_HE_MAC_CAP_MASK(0, 5, 0x7, "Maximum number of MSDUS Fragments");
1173 PRINT_HE_MAC_CAP_MASK(0, 8, 0x3, "Minimum Payload size of 128 bytes");
1174 PRINT_HE_MAC_CAP_MASK(0, 10, 0x3, "Trigger Frame MAC Padding Duration");
1175 PRINT_HE_MAC_CAP_MASK(0, 12, 0x7, "Multi-TID Aggregation Support");
1176
1177 PRINT_HE_MAC_CAP(1, 1, "All Ack");
1178 PRINT_HE_MAC_CAP(1, 2, "TRS");
1179 PRINT_HE_MAC_CAP(1, 3, "BSR");
1180 PRINT_HE_MAC_CAP(1, 4, "Broadcast TWT");
1181 PRINT_HE_MAC_CAP(1, 5, "32-bit BA Bitmap");
1182 PRINT_HE_MAC_CAP(1, 6, "MU Cascading");
1183 PRINT_HE_MAC_CAP(1, 7, "Ack-Enabled Aggregation");
1184 PRINT_HE_MAC_CAP(1, 9, "OM Control");
1185 PRINT_HE_MAC_CAP(1, 10, "OFDMA RA");
1186 PRINT_HE_MAC_CAP_MASK(1, 11, 0x3, "Maximum A-MPDU Length Exponent");
1187 PRINT_HE_MAC_CAP(1, 13, "A-MSDU Fragmentation");
1188 PRINT_HE_MAC_CAP(1, 14, "Flexible TWT Scheduling");
1189 PRINT_HE_MAC_CAP(1, 15, "RX Control Frame to MultiBSS");
1190
1191 PRINT_HE_MAC_CAP(2, 0, "BSRP BQRP A-MPDU Aggregation");
1192 PRINT_HE_MAC_CAP(2, 1, "QTP");
1193 PRINT_HE_MAC_CAP(2, 2, "BQR");
1194 PRINT_HE_MAC_CAP(2, 3, "SRP Responder Role");
1195 PRINT_HE_MAC_CAP(2, 4, "NDP Feedback Report");
1196 PRINT_HE_MAC_CAP(2, 5, "OPS");
1197 PRINT_HE_MAC_CAP(2, 6, "A-MSDU in A-MPDU");
1198 PRINT_HE_MAC_CAP_MASK(2, 7, 7, "Multi-TID Aggregation TX");
1199 PRINT_HE_MAC_CAP(2, 10, "HE Subchannel Selective Transmission");
1200 PRINT_HE_MAC_CAP(2, 11, "UL 2x996-Tone RU");
1201 PRINT_HE_MAC_CAP(2, 12, "OM Control UL MU Data Disable RX");
1202
1203 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]) {
1204 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]);
1205
1206 if (len > sizeof(phy_cap) - 1)
1207 len = sizeof(phy_cap) - 1;
1208 memcpy(&((__u8 *)phy_cap)[1],
1209 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY]),
1210 len);
1211 }
1212 printf("\t\t\tHE PHY Capabilities: (0x");
1213 for (i = 0; i < 11; i++)
1214 printf("%02x", ((__u8 *)phy_cap)[i + 1]);
1215 printf("):\n");
1216
1217 PRINT_HE_PHY_CAP0(0, 1, "HE40/2.4GHz");
1218 PRINT_HE_PHY_CAP0(0, 2, "HE40/HE80/5GHz");
1219 PRINT_HE_PHY_CAP0(0, 3, "HE160/5GHz");
1220 PRINT_HE_PHY_CAP0(0, 4, "HE160/HE80+80/5GHz");
1221 PRINT_HE_PHY_CAP0(0, 5, "242 tone RUs/2.4GHz");
1222 PRINT_HE_PHY_CAP0(0, 6, "242 tone RUs/5GHz");
1223
1224 PRINT_HE_PHY_CAP_MASK(1, 0, 0xf, "Punctured Preamble RX");
1225 PRINT_HE_PHY_CAP_MASK(1, 4, 0x1, "Device Class");
1226 PRINT_HE_PHY_CAP(1, 5, "LDPC Coding in Payload");
1227 PRINT_HE_PHY_CAP(1, 6, "HE SU PPDU with 1x HE-LTF and 0.8us GI");
1228 PRINT_HE_PHY_CAP_MASK(1, 7, 0x3, "Midamble Rx Max NSTS");
1229 PRINT_HE_PHY_CAP(1, 9, "NDP with 4x HE-LTF and 3.2us GI");
1230 PRINT_HE_PHY_CAP(1, 10, "STBC Tx <= 80MHz");
1231 PRINT_HE_PHY_CAP(1, 11, "STBC Rx <= 80MHz");
1232 PRINT_HE_PHY_CAP(1, 12, "Doppler Tx");
1233 PRINT_HE_PHY_CAP(1, 13, "Doppler Rx");
1234 PRINT_HE_PHY_CAP(1, 14, "Full Bandwidth UL MU-MIMO");
1235 PRINT_HE_PHY_CAP(1, 15, "Partial Bandwidth UL MU-MIMO");
1236
1237 PRINT_HE_PHY_CAP_MASK(2, 0, 0x3, "DCM Max Constellation");
1238 PRINT_HE_PHY_CAP_MASK(2, 2, 0x1, "DCM Max NSS Tx");
1239 PRINT_HE_PHY_CAP_MASK(2, 3, 0x3, "DCM Max Constellation Rx");
1240 PRINT_HE_PHY_CAP_MASK(2, 5, 0x1, "DCM Max NSS Rx");
1241 PRINT_HE_PHY_CAP(2, 6, "Rx HE MU PPDU from Non-AP STA");
1242 PRINT_HE_PHY_CAP(2, 7, "SU Beamformer");
1243 PRINT_HE_PHY_CAP(2, 8, "SU Beamformee");
1244 PRINT_HE_PHY_CAP(2, 9, "MU Beamformer");
1245 PRINT_HE_PHY_CAP_MASK(2, 10, 0x7, "Beamformee STS <= 80Mhz");
1246 PRINT_HE_PHY_CAP_MASK(2, 13, 0x7, "Beamformee STS > 80Mhz");
1247
1248 PRINT_HE_PHY_CAP_MASK(3, 0, 0x7, "Sounding Dimensions <= 80Mhz");
1249 PRINT_HE_PHY_CAP_MASK(3, 3, 0x7, "Sounding Dimensions > 80Mhz");
1250 PRINT_HE_PHY_CAP(3, 6, "Ng = 16 SU Feedback");
1251 PRINT_HE_PHY_CAP(3, 7, "Ng = 16 MU Feedback");
1252 PRINT_HE_PHY_CAP(3, 8, "Codebook Size SU Feedback");
1253 PRINT_HE_PHY_CAP(3, 9, "Codebook Size MU Feedback");
1254 PRINT_HE_PHY_CAP(3, 10, "Triggered SU Beamforming Feedback");
1255 PRINT_HE_PHY_CAP(3, 11, "Triggered MU Beamforming Feedback");
1256 PRINT_HE_PHY_CAP(3, 12, "Triggered CQI Feedback");
1257 PRINT_HE_PHY_CAP(3, 13, "Partial Bandwidth Extended Range");
1258 PRINT_HE_PHY_CAP(3, 14, "Partial Bandwidth DL MU-MIMO");
1259 PRINT_HE_PHY_CAP(3, 15, "PPE Threshold Present");
1260
1261 PRINT_HE_PHY_CAP(4, 0, "SRP-based SR");
1262 PRINT_HE_PHY_CAP(4, 1, "Power Boost Factor ar");
1263 PRINT_HE_PHY_CAP(4, 2, "HE SU PPDU & HE PPDU 4x HE-LTF 0.8us GI");
1264 PRINT_HE_PHY_CAP_MASK(4, 3, 0x7, "Max NC");
1265 PRINT_HE_PHY_CAP(4, 6, "STBC Tx > 80MHz");
1266 PRINT_HE_PHY_CAP(4, 7, "STBC Rx > 80MHz");
1267 PRINT_HE_PHY_CAP(4, 8, "HE ER SU PPDU 4x HE-LTF 0.8us GI");
1268 PRINT_HE_PHY_CAP(4, 9, "20MHz in 40MHz HE PPDU 2.4GHz");
1269 PRINT_HE_PHY_CAP(4, 10, "20MHz in 160/80+80MHz HE PPDU");
1270 PRINT_HE_PHY_CAP(4, 11, "80MHz in 160/80+80MHz HE PPDU");
1271 PRINT_HE_PHY_CAP(4, 12, "HE ER SU PPDU 1x HE-LTF 0.8us GI");
1272 PRINT_HE_PHY_CAP(4, 13, "Midamble Rx 2x & 1x HE-LTF");
1273 PRINT_HE_PHY_CAP_MASK(4, 14, 0x3, "DCM Max BW");
1274
1275 PRINT_HE_PHY_CAP(5, 0, "Longer Than 16HE SIG-B OFDM Symbols");
1276 PRINT_HE_PHY_CAP(5, 1, "Non-Triggered CQI Feedback");
1277 PRINT_HE_PHY_CAP(5, 2, "TX 1024-QAM");
1278 PRINT_HE_PHY_CAP(5, 3, "RX 1024-QAM");
1279 PRINT_HE_PHY_CAP(5, 4, "RX Full BW SU Using HE MU PPDU with Compression SIGB");
1280 PRINT_HE_PHY_CAP(5, 5, "RX Full BW SU Using HE MU PPDU with Non-Compression SIGB");
1281
1282 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]) {
1283 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]);
1284 if (len > sizeof(mcs_set))
1285 len = sizeof(mcs_set);
1286 memcpy(mcs_set,
1287 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET]),
1288 len);
1289 }
1290
1291 for (i = 0; i < 3; i++) {
1292 __u8 phy_cap_support[] = { BIT(1) | BIT(2), BIT(3), BIT(4) };
1293 char *bw[] = { "<= 80", "160", "80+80" };
1294 int j;
1295
1296 if ((phy_cap[0] & (phy_cap_support[i] << 8)) == 0)
1297 continue;
1298
1299 for (j = 0; j < 2; j++) {
1300 int k;
1301 printf("\t\t\tHE %s MCS and NSS set %s MHz\n", j ? "TX" : "RX", bw[i]);
1302 for (k = 0; k < 8; k++) {
1303 __u16 mcs = mcs_set[(i * 2) + j];
1304 mcs >>= k * 2;
1305 mcs &= 0x3;
1306 printf("\t\t\t\t\t %d streams: ", k + 1);
1307 if (mcs == 3)
1308 printf("not supported\n");
1309 else
1310 printf("MCS 0-%d\n", 7 + (mcs * 2));
1311 }
1312
1313 }
1314 }
1315
1316 len = 0;
1317 if (tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]) {
1318 len = nla_len(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]);
1319 if (len > sizeof(ppet))
1320 len = sizeof(ppet);
1321 memcpy(ppet,
1322 nla_data(tb[NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE]),
1323 len);
1324 }
1325
1326 if (len && (phy_cap[3] & BIT(15))) {
1327 size_t i;
1328
1329 printf("\t\t\tPPE Threshold ");
1330 for (i = 0; i < len; i++)
1331 if (ppet[i])
1332 printf("0x%02x ", ppet[i]);
1333 printf("\n");
1334 }
1335 }
1336
1337 void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
1338 {
1339 size_t i;
1340
1341 printf("%s: ", prefix);
1342 for (i = 0; i < size; i++) {
1343 if (i && i % 16 == 0)
1344 printf("\n%s: ", prefix);
1345 printf("%02x ", buf[i]);
1346 }
1347 printf("\n\n");
1348 }
1349
1350 int get_cf1(const struct chanmode *chanmode, unsigned long freq)
1351 {
1352 unsigned int cf1 = freq, j;
1353 unsigned int vht80[] = { 5180, 5260, 5500, 5580, 5660, 5745 };
1354
1355 switch (chanmode->width) {
1356 case NL80211_CHAN_WIDTH_80:
1357 /* setup center_freq1 */
1358 for (j = 0; j < ARRAY_SIZE(vht80); j++) {
1359 if (freq >= vht80[j] && freq < vht80[j] + 80)
1360 break;
1361 }
1362
1363 if (j == ARRAY_SIZE(vht80))
1364 break;
1365
1366 cf1 = vht80[j] + 30;
1367 break;
1368 default:
1369 cf1 = freq + chanmode->freq1_diff;
1370 break;
1371 }
1372
1373 return cf1;
1374 }
1375
1376 int parse_random_mac_addr(struct nl_msg *msg, char *addrs)
1377 {
1378 char *a_addr, *a_mask, *sep;
1379 unsigned char addr[ETH_ALEN], mask[ETH_ALEN];
1380
1381 if (!*addrs) {
1382 /* randomise all but the multicast bit */
1383 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN,
1384 "\x00\x00\x00\x00\x00\x00");
1385 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
1386 "\x01\x00\x00\x00\x00\x00");
1387 return 0;
1388 }
1389
1390 if (*addrs != '=')
1391 return 1;
1392
1393 addrs++;
1394 sep = strchr(addrs, '/');
1395 a_addr = addrs;
1396
1397 if (!sep)
1398 return 1;
1399
1400 *sep = 0;
1401 a_mask = sep + 1;
1402 if (mac_addr_a2n(addr, a_addr) || mac_addr_a2n(mask, a_mask))
1403 return 1;
1404
1405 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1406 NLA_PUT(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN, mask);
1407
1408 return 0;
1409 nla_put_failure:
1410 return -ENOBUFS;
1411 }