]> git.ipfire.org Git - thirdparty/iw.git/blame - util.c
iw: scan: parse OWE Transition Mode element
[thirdparty/iw.git] / util.c
CommitLineData
748f8489 1#include <ctype.h>
51e9bd80
JB
2#include <netlink/attr.h>
3#include <errno.h>
4#include <stdbool.h>
3d1e8704 5#include "iw.h"
f5f7b1d0 6#include "nl80211.h"
3d1e8704 7
7f87d3cf 8void mac_addr_n2a(char *mac_addr, const unsigned char *arg)
3d1e8704 9{
53e5ce7a 10 int i, l;
3d1e8704
LCC
11
12 l = 0;
13 for (i = 0; i < ETH_ALEN ; i++) {
14 if (i == 0) {
53e5ce7a 15 sprintf(mac_addr+l, "%02x", arg[i]);
3d1e8704
LCC
16 l += 2;
17 } else {
53e5ce7a 18 sprintf(mac_addr+l, ":%02x", arg[i]);
3d1e8704
LCC
19 l += 3;
20 }
21 }
3d1e8704
LCC
22}
23
24int mac_addr_a2n(unsigned char *mac_addr, char *arg)
25{
26 int i;
27
28 for (i = 0; i < ETH_ALEN ; i++) {
29 int temp;
30 char *cp = strchr(arg, ':');
31 if (cp) {
32 *cp = 0;
33 cp++;
34 }
35 if (sscanf(arg, "%x", &temp) != 1)
36 return -1;
37 if (temp < 0 || temp > 255)
38 return -1;
39
40 mac_addr[i] = temp;
41 if (!cp)
42 break;
43 arg = cp;
44 }
45 if (i < ETH_ALEN - 1)
46 return -1;
47
48 return 0;
49}
541ef425 50
3ff24563
JB
51int parse_hex_mask(char *hexmask, unsigned char **result, size_t *result_len,
52 unsigned char **mask)
236d4191 53{
3ff24563
JB
54 size_t len = strlen(hexmask) / 2;
55 unsigned char *result_val;
56 unsigned char *result_mask = NULL;
57
236d4191
JB
58 int pos = 0;
59
3ff24563 60 *result_len = 0;
236d4191 61
3ff24563
JB
62 result_val = calloc(len + 2, 1);
63 if (!result_val)
64 goto error;
65 *result = result_val;
66 if (mask) {
67 result_mask = calloc(DIV_ROUND_UP(len, 8) + 2, 1);
68 if (!result_mask)
69 goto error;
70 *mask = result_mask;
71 }
236d4191
JB
72
73 while (1) {
3ff24563 74 char *cp = strchr(hexmask, ':');
236d4191
JB
75 if (cp) {
76 *cp = 0;
77 cp++;
78 }
236d4191 79
3ff24563
JB
80 if (result_mask && (strcmp(hexmask, "-") == 0 ||
81 strcmp(hexmask, "xx") == 0 ||
82 strcmp(hexmask, "--") == 0)) {
83 /* skip this byte and leave mask bit unset */
84 } else {
85 int temp, mask_pos;
86 char *end;
87
88 temp = strtoul(hexmask, &end, 16);
89 if (*end)
90 goto error;
91 if (temp < 0 || temp > 255)
92 goto error;
93 result_val[pos] = temp;
94
95 mask_pos = pos / 8;
96 if (result_mask)
97 result_mask[mask_pos] |= 1 << (pos % 8);
98 }
99
100 (*result_len)++;
101 pos++;
236d4191 102
236d4191
JB
103 if (!cp)
104 break;
3ff24563 105 hexmask = cp;
236d4191
JB
106 }
107
3ff24563 108 return 0;
236d4191 109 error:
3ff24563
JB
110 free(result_val);
111 free(result_mask);
112 return -1;
113}
114
115unsigned char *parse_hex(char *hex, size_t *outlen)
116{
117 unsigned char *result;
118
119 if (parse_hex_mask(hex, &result, outlen, NULL))
120 return NULL;
121 return result;
236d4191
JB
122}
123
541ef425
JB
124static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
125 "unspecified",
126 "IBSS",
34e78ed0 127 "managed",
541ef425 128 "AP",
34e78ed0 129 "AP/VLAN",
541ef425 130 "WDS",
34e78ed0 131 "monitor",
a4464243
JB
132 "mesh point",
133 "P2P-client",
134 "P2P-GO",
add40bbd 135 "P2P-device",
3955e524 136 "outside context of a BSS",
ed9b77ec 137 "NAN",
541ef425
JB
138};
139
140static char modebuf[100];
141
142const char *iftype_name(enum nl80211_iftype iftype)
143{
a66b3a35 144 if (iftype <= NL80211_IFTYPE_MAX && ifmodes[iftype])
541ef425
JB
145 return ifmodes[iftype];
146 sprintf(modebuf, "Unknown mode (%d)", iftype);
147 return modebuf;
148}
379f8397 149
9990c1e9 150static const char *commands[NL80211_CMD_MAX + 1] = {
02aef469 151/*
f45d4583 152 * sed 's%^\tNL80211_CMD_%%;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h | grep -v "reserved"
02aef469
JB
153 */
154 [NL80211_CMD_UNSPEC] = "unspec",
06339499
JB
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",
02aef469
JB
169 [NL80211_CMD_START_AP] = "start_ap",
170 [NL80211_CMD_STOP_AP] = "stop_ap",
06339499
JB
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",
02aef469
JB
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",
06339499
JB
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",
02aef469 204 [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results",
06339499
JB
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",
02aef469
JB
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",
7b96e9e2 218 [NL80211_CMD_SET_CHANNEL] = "set_channel",
dcf57038
JB
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",
02aef469
JB
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",
850ab15d 232 [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload",
02aef469
JB
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",
65d07957
JB
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",
240aa0ef
JB
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",
0624c45d
JB
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",
b8820d2b
JB
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",
8823f67b
JB
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",
478438c0 267 [NL80211_CMD_ABORT_SCAN] = "abort_scan",
2781d641
JB
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",
8a9e0b30
JB
274 [NL80211_CMD_SET_MULTICAST_TO_UNICAST] = "set_multicast_to_unicast",
275 [NL80211_CMD_UPDATE_CONNECT_PARAMS] = "update_connect_params",
378286c9
JB
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",
9990c1e9
MH
280};
281
282static char cmdbuf[100];
283
284const char *command_name(enum nl80211_commands cmd)
285{
73780397 286 if (cmd <= NL80211_CMD_MAX && commands[cmd])
9990c1e9
MH
287 return commands[cmd];
288 sprintf(cmdbuf, "Unknown command (%d)", cmd);
289 return cmdbuf;
290}
291
58b46da2 292int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
379f8397 293{
58b46da2
BR
294 /* see 802.11 17.3.8.3.2 and Annex J
295 * there are overlapping channel numbers in 5GHz and 2GHz bands */
296 if (chan <= 0)
297 return 0; /* not supported */
298 switch (band) {
299 case NL80211_BAND_2GHZ:
300 if (chan == 14)
301 return 2484;
302 else if (chan < 14)
303 return 2407 + chan * 5;
304 break;
305 case NL80211_BAND_5GHZ:
306 if (chan >= 182 && chan <= 196)
307 return 4000 + chan * 5;
308 else
309 return 5000 + chan * 5;
310 break;
311 case NL80211_BAND_60GHZ:
312 if (chan < 5)
313 return 56160 + chan * 2160;
314 break;
315 default:
316 ;
317 }
318 return 0; /* not supported */
379f8397
JB
319}
320
321int ieee80211_frequency_to_channel(int freq)
322{
58b46da2 323 /* see 802.11-2007 17.3.8.3.2 and Annex J */
379f8397
JB
324 if (freq == 2484)
325 return 14;
58b46da2 326 else if (freq < 2484)
379f8397 327 return (freq - 2407) / 5;
58b46da2
BR
328 else if (freq >= 4910 && freq <= 4980)
329 return (freq - 4000) / 5;
330 else if (freq <= 45000) /* DMG band lower limit */
331 return (freq - 5000) / 5;
332 else if (freq >= 58320 && freq <= 64800)
d56e86bc 333 return (freq - 56160) / 2160;
58b46da2
BR
334 else
335 return 0;
379f8397 336}
748f8489
JB
337
338void print_ssid_escaped(const uint8_t len, const uint8_t *data)
339{
340 int i;
341
342 for (i = 0; i < len; i++) {
3f612733 343 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
748f8489 344 printf("%c", data[i]);
3f612733
JB
345 else if (data[i] == ' ' &&
346 (i != 0 && i != len -1))
347 printf(" ");
748f8489
JB
348 else
349 printf("\\x%.2x", data[i]);
350 }
351}
51e9bd80
JB
352
353static int hex2num(char digit)
354{
355 if (!isxdigit(digit))
356 return -1;
357 if (isdigit(digit))
358 return digit - '0';
359 return tolower(digit) - 'a' + 10;
360}
361
9ad3cc24 362static int hex2byte(const char *hex)
51e9bd80
JB
363{
364 int d1, d2;
365
366 d1 = hex2num(hex[0]);
367 if (d1 < 0)
368 return -1;
369 d2 = hex2num(hex[1]);
370 if (d2 < 0)
371 return -1;
372 return (d1 << 4) | d2;
373}
374
60b6c638 375char *hex2bin(const char *hex, char *buf)
51e9bd80
JB
376{
377 char *result = buf;
378 int d;
379
380 while (hex[0]) {
381 d = hex2byte(hex);
382 if (d < 0)
383 return NULL;
384 buf[0] = d;
385 buf++;
386 hex += 2;
387 }
388
389 return result;
390}
391
6c2a0121
EG
392static int parse_akm_suite(const char *cipher_str)
393{
394
395 if (!strcmp(cipher_str, "PSK"))
396 return 0x000FAC02;
397 if (!strcmp(cipher_str, "FT/PSK"))
398 return 0x000FAC03;
399 if (!strcmp(cipher_str, "PSK/SHA-256"))
400 return 0x000FAC06;
401 return -EINVAL;
402}
403
404static int parse_cipher_suite(const char *cipher_str)
405{
406
407 if (!strcmp(cipher_str, "TKIP"))
408 return 0x000FAC02;
d7924705 409 if (!strcmp(cipher_str, "CCMP") || !strcmp(cipher_str, "CCMP-128"))
6c2a0121 410 return 0x000FAC04;
d7924705 411 if (!strcmp(cipher_str, "GCMP") || !strcmp(cipher_str, "GCMP-128"))
6c2a0121
EG
412 return 0x000FAC08;
413 if (!strcmp(cipher_str, "GCMP-256"))
414 return 0x000FAC09;
415 if (!strcmp(cipher_str, "CCMP-256"))
416 return 0x000FAC0A;
417 return -EINVAL;
418}
419
51e9bd80
JB
420int parse_keys(struct nl_msg *msg, char **argv, int argc)
421{
422 struct nlattr *keys;
423 int i = 0;
041581ce 424 bool have_default = false;
6c2a0121 425 char *arg = *argv;
51e9bd80 426 char keybuf[13];
6c2a0121 427 int pos = 0;
51e9bd80
JB
428
429 if (!argc)
430 return 1;
431
6c2a0121
EG
432 if (!memcmp(&arg[pos], "psk", 3)) {
433 char psk_keybuf[32];
434 int cipher_suite, akm_suite;
435
86dbcee9 436 if (argc < 4)
6c2a0121
EG
437 goto explain;
438
439 pos+=3;
440 if (arg[pos] != ':')
441 goto explain;
442 pos++;
443
444 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, NL80211_WPA_VERSION_2);
445
446 if (strlen(&arg[pos]) != (sizeof(psk_keybuf) * 2) || !hex2bin(&arg[pos], psk_keybuf)) {
447 printf("Bad PSK\n");
448 return -EINVAL;
449 }
450
451 NLA_PUT(msg, NL80211_ATTR_PMK, 32, psk_keybuf);
452 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, NL80211_AUTHTYPE_OPEN_SYSTEM);
453
454 argv++;
455 argc--;
456 arg = *argv;
457
458 akm_suite = parse_akm_suite(arg);
459 if (akm_suite < 0)
460 goto explain;
461
462 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, akm_suite);
463
464 argv++;
465 argc--;
466 arg = *argv;
467
468 cipher_suite = parse_cipher_suite(arg);
469 if (cipher_suite < 0)
470 goto explain;
471
472 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher_suite);
473
474 argv++;
475 argc--;
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_SUITE_GROUP, cipher_suite);
483
484 return 0;
485 }
486
51e9bd80
JB
487 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
488
489 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
490 if (!keys)
491 return -ENOBUFS;
492
493 do {
6c2a0121 494 int keylen;
51e9bd80
JB
495 struct nlattr *key = nla_nest_start(msg, ++i);
496 char *keydata;
497
6c2a0121
EG
498 arg = *argv;
499 pos = 0;
500
51e9bd80
JB
501 if (!key)
502 return -ENOBUFS;
503
504 if (arg[pos] == 'd') {
505 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
506 pos++;
507 if (arg[pos] == ':')
508 pos++;
041581ce 509 have_default = true;
51e9bd80
JB
510 }
511
512 if (!isdigit(arg[pos]))
513 goto explain;
514 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
515 if (arg[pos++] != ':')
516 goto explain;
517 keydata = arg + pos;
518 switch (strlen(keydata)) {
519 case 10:
520 keydata = hex2bin(keydata, keybuf);
6ab936f0 521 /* fall through */
51e9bd80
JB
522 case 5:
523 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
524 keylen = 5;
525 break;
526 case 26:
527 keydata = hex2bin(keydata, keybuf);
6ab936f0 528 /* fall through */
51e9bd80
JB
529 case 13:
530 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
531 keylen = 13;
532 break;
533 default:
534 goto explain;
535 }
536
537 if (!keydata)
538 goto explain;
539
540 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
541
51e9bd80
JB
542 argv++;
543 argc--;
041581ce
JB
544
545 /* one key should be TX key */
546 if (!have_default && !argc)
547 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
548
549 nla_nest_end(msg, key);
51e9bd80
JB
550 } while (argc);
551
552 nla_nest_end(msg, keys);
553
554 return 0;
555 nla_put_failure:
556 return -ENOBUFS;
557 explain:
558 fprintf(stderr, "key must be [d:]index:data where\n"
559 " 'd:' means default (transmit) key\n"
560 " 'index:' is a single digit (0-3)\n"
561 " 'data' must be 5 or 13 ascii chars\n"
562 " or 10 or 26 hex digits\n"
6c2a0121
EG
563 "for example: d:2:6162636465 is the same as d:2:abcde\n"
564 "or psk:data <AKM Suite> <pairwise CIPHER> <groupwise CIPHER> where\n"
565 " 'data' is the PSK (output of wpa_passphrase and the CIPHER can be CCMP or GCMP\n"
566 "for example: psk:0123456789abcdef PSK CCMP CCMP\n"
567 "The allowed AKM suites are PSK, FT/PSK, PSK/SHA-256\n"
568 "The allowed Cipher suites are TKIP, CCMP, GCMP, GCMP-256, CCMP-256\n");
51e9bd80
JB
569 return 2;
570}
deb3501c 571
c37f6c64 572enum nl80211_chan_width str_to_bw(const char *str)
997c60fd
BB
573{
574 static const struct {
575 const char *name;
576 unsigned int val;
577 } bwmap[] = {
578 { .name = "5", .val = NL80211_CHAN_WIDTH_5, },
579 { .name = "10", .val = NL80211_CHAN_WIDTH_10, },
580 { .name = "20", .val = NL80211_CHAN_WIDTH_20, },
581 { .name = "40", .val = NL80211_CHAN_WIDTH_40, },
582 { .name = "80", .val = NL80211_CHAN_WIDTH_80, },
583 { .name = "80+80", .val = NL80211_CHAN_WIDTH_80P80, },
584 { .name = "160", .val = NL80211_CHAN_WIDTH_160, },
585 };
c37f6c64
JB
586 unsigned int i;
587
588 for (i = 0; i < ARRAY_SIZE(bwmap); i++) {
589 if (strcasecmp(bwmap[i].name, str) == 0)
590 return bwmap[i].val;
591 }
592
593 return NL80211_CHAN_WIDTH_20_NOHT;
594}
595
596static int parse_freqs(struct chandef *chandef, int argc, char **argv,
597 int *parsed)
598{
997c60fd 599 uint32_t freq;
997c60fd 600 char *end;
4871fcf5 601 bool need_cf1 = false, need_cf2 = false;
997c60fd
BB
602
603 if (argc < 1)
604 return 0;
605
c37f6c64 606 chandef->width = str_to_bw(argv[0]);
997c60fd 607
4871fcf5
JB
608 switch (chandef->width) {
609 case NL80211_CHAN_WIDTH_20_NOHT:
610 /* First argument was not understood, give up gracefully. */
997c60fd 611 return 0;
4871fcf5
JB
612 case NL80211_CHAN_WIDTH_20:
613 case NL80211_CHAN_WIDTH_5:
614 case NL80211_CHAN_WIDTH_10:
615 break;
616 case NL80211_CHAN_WIDTH_80P80:
617 need_cf2 = true;
618 /* fall through */
619 case NL80211_CHAN_WIDTH_40:
620 case NL80211_CHAN_WIDTH_80:
621 case NL80211_CHAN_WIDTH_160:
622 need_cf1 = true;
623 break;
624 }
997c60fd 625
c37f6c64
JB
626 *parsed += 1;
627
4871fcf5 628 if (!need_cf1)
997c60fd
BB
629 return 0;
630
4871fcf5
JB
631 if (argc < 2)
632 return 1;
633
997c60fd
BB
634 /* center freq 1 */
635 if (!*argv[1])
4871fcf5 636 return 1;
997c60fd
BB
637 freq = strtoul(argv[1], &end, 10);
638 if (*end)
4871fcf5 639 return 1;
997c60fd
BB
640 *parsed += 1;
641
642 chandef->center_freq1 = freq;
643
4871fcf5 644 if (!need_cf2)
997c60fd
BB
645 return 0;
646
4871fcf5
JB
647 if (argc < 3)
648 return 1;
649
997c60fd
BB
650 /* center freq 2 */
651 if (!*argv[2])
4871fcf5 652 return 1;
997c60fd
BB
653 freq = strtoul(argv[2], &end, 10);
654 if (*end)
4871fcf5 655 return 1;
997c60fd
BB
656 chandef->center_freq2 = freq;
657
658 *parsed += 1;
659
660 return 0;
661}
662
663
664/**
665 * parse_freqchan - Parse frequency or channel definition
666 *
667 * @chandef: chandef structure to be filled in
668 * @chan: Boolean whether to parse a channel or frequency based specifier
669 * @argc: Number of arguments
670 * @argv: Array of string arguments
671 * @parsed: Pointer to return the number of used arguments, or NULL to error
672 * out if any argument is left unused.
673 *
674 * The given chandef structure will be filled in from the command line
675 * arguments. argc/argv will be updated so that further arguments from the
676 * command line can be parsed.
677 *
4871fcf5
JB
678 * Note that despite the fact that the function knows how many center freqs
679 * are needed, there's an ambiguity if the next argument after this is an
680 * integer argument, since the valid channel width values are interpreted
681 * as such, rather than a following argument. This can be avoided by the
682 * user by giving "NOHT" instead.
997c60fd
BB
683 *
684 * The working specifier if chan is set are:
685 * <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
686 *
687 * And if frequency is set:
688 * <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
689 * <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
690 *
691 * If the mode/channel width is not given the NOHT is assumed.
692 *
693 * Return: Number of used arguments, zero or negative error number otherwise
694 */
695int parse_freqchan(struct chandef *chandef, bool chan, int argc, char **argv,
696 int *parsed)
697{
698 char *end;
699 static const struct chanmode chanmode[] = {
700 { .name = "HT20",
701 .width = NL80211_CHAN_WIDTH_20,
702 .freq1_diff = 0,
703 .chantype = NL80211_CHAN_HT20 },
704 { .name = "HT40+",
705 .width = NL80211_CHAN_WIDTH_40,
706 .freq1_diff = 10,
707 .chantype = NL80211_CHAN_HT40PLUS },
708 { .name = "HT40-",
709 .width = NL80211_CHAN_WIDTH_40,
710 .freq1_diff = -10,
711 .chantype = NL80211_CHAN_HT40MINUS },
712 { .name = "NOHT",
713 .width = NL80211_CHAN_WIDTH_20_NOHT,
714 .freq1_diff = 0,
715 .chantype = NL80211_CHAN_NO_HT },
716 { .name = "5MHz",
717 .width = NL80211_CHAN_WIDTH_5,
718 .freq1_diff = 0,
719 .chantype = -1 },
720 { .name = "10MHz",
721 .width = NL80211_CHAN_WIDTH_10,
722 .freq1_diff = 0,
723 .chantype = -1 },
724 { .name = "80MHz",
725 .width = NL80211_CHAN_WIDTH_80,
726 .freq1_diff = 0,
727 .chantype = -1 },
728 };
729 const struct chanmode *chanmode_selected = NULL;
730 unsigned int freq;
731 unsigned int i;
732 int _parsed = 0;
733 int res = 0;
734
735 if (argc < 1)
736 return 1;
737
738 if (!argv[0])
739 goto out;
740 freq = strtoul(argv[0], &end, 10);
741 if (*end) {
742 res = 1;
743 goto out;
744 }
745
746 _parsed += 1;
747
748 memset(chandef, 0, sizeof(struct chandef));
749
750 if (chan) {
751 enum nl80211_band band;
752
753 band = freq <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
754 freq = ieee80211_channel_to_frequency(freq, band);
755 }
756 chandef->control_freq = freq;
757 /* Assume 20MHz NOHT channel for now. */
758 chandef->center_freq1 = freq;
759
760 /* Try to parse HT mode definitions */
761 if (argc > 1) {
762 for (i = 0; i < ARRAY_SIZE(chanmode); i++) {
763 if (strcasecmp(chanmode[i].name, argv[1]) == 0) {
764 chanmode_selected = &chanmode[i];
765 _parsed += 1;
766 break;
767 }
768 }
769 }
770
771 /* channel mode given, use it and return. */
772 if (chanmode_selected) {
773 chandef->center_freq1 = get_cf1(chanmode_selected, freq);
774 chandef->width = chanmode_selected->width;
775 goto out;
776 }
777
778 /* This was a only a channel definition, nothing further may follow. */
779 if (chan)
780 goto out;
781
782 res = parse_freqs(chandef, argc - 1, argv + 1, &_parsed);
783
784 out:
785 /* Error out if parsed is NULL. */
786 if (!parsed && _parsed != argc)
787 return 1;
788
789 if (parsed)
790 *parsed = _parsed;
791
792 return res;
793}
794
795int put_chandef(struct nl_msg *msg, struct chandef *chandef)
796{
797 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chandef->control_freq);
798 NLA_PUT_U32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width);
799
800 switch (chandef->width) {
801 case NL80211_CHAN_WIDTH_20_NOHT:
802 NLA_PUT_U32(msg,
803 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
804 NL80211_CHAN_NO_HT);
805 break;
806 case NL80211_CHAN_WIDTH_20:
807 NLA_PUT_U32(msg,
808 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
809 NL80211_CHAN_HT20);
810 break;
811 case NL80211_CHAN_WIDTH_40:
812 if (chandef->control_freq > chandef->center_freq1)
813 NLA_PUT_U32(msg,
814 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
815 NL80211_CHAN_HT40MINUS);
816 else
817 NLA_PUT_U32(msg,
818 NL80211_ATTR_WIPHY_CHANNEL_TYPE,
819 NL80211_CHAN_HT40PLUS);
820 break;
821 default:
822 break;
823 }
824
825 if (chandef->center_freq1)
826 NLA_PUT_U32(msg,
827 NL80211_ATTR_CENTER_FREQ1,
828 chandef->center_freq1);
829
830 if (chandef->center_freq2)
831 NLA_PUT_U32(msg,
832 NL80211_ATTR_CENTER_FREQ2,
833 chandef->center_freq2);
834
835 return 0;
836
837 nla_put_failure:
838 return -ENOBUFS;
839}
840
7ddfb679 841static void print_mcs_index(const __u8 *mcs)
deb3501c 842{
9fea9777 843 int mcs_bit, prev_bit = -2, prev_cont = 0;
04953e90
JB
844
845 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
846 unsigned int mcs_octet = mcs_bit/8;
847 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
848 bool mcs_rate_idx_set;
849
850 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
851
852 if (!mcs_rate_idx_set)
853 continue;
854
855 if (prev_bit != mcs_bit - 1) {
856 if (prev_bit != -2)
857 printf("%d, ", prev_bit);
858 else
859 printf(" ");
860 printf("%d", mcs_bit);
861 prev_cont = 0;
862 } else if (!prev_cont) {
863 printf("-");
864 prev_cont = 1;
865 }
866
867 prev_bit = mcs_bit;
868 }
deb3501c 869
04953e90
JB
870 if (prev_cont)
871 printf("%d", prev_bit);
872 printf("\n");
deb3501c 873}
0950993f
LR
874
875/*
876 * There are only 4 possible values, we just use a case instead of computing it,
877 * but technically this can also be computed through the formula:
878 *
879 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
880 */
881static __u32 compute_ampdu_length(__u8 exponent)
882{
883 switch (exponent) {
884 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
885 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
886 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
887 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
888 default: return 0;
889 }
890}
891
892static const char *print_ampdu_space(__u8 space)
893{
894 switch (space) {
895 case 0: return "No restriction";
896 case 1: return "1/4 usec";
897 case 2: return "1/2 usec";
898 case 3: return "1 usec";
899 case 4: return "2 usec";
900 case 5: return "4 usec";
901 case 6: return "8 usec";
902 case 7: return "16 usec";
903 default:
7ae93cd5 904 return "BUG (spacing more than 3 bits!)";
0950993f
LR
905 }
906}
907
908void print_ampdu_length(__u8 exponent)
909{
04953e90 910 __u32 max_ampdu_length;
0950993f
LR
911
912 max_ampdu_length = compute_ampdu_length(exponent);
913
914 if (max_ampdu_length) {
915 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
916 max_ampdu_length, exponent);
3f362f8b 917 } else {
0950993f
LR
918 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
919 "(exponent: %d)\n", exponent);
920 }
921}
922
923void print_ampdu_spacing(__u8 spacing)
924{
3f362f8b
NB
925 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
926 print_ampdu_space(spacing), spacing);
0950993f 927}
357c1a5d
LR
928
929void print_ht_capability(__u16 cap)
930{
931#define PRINT_HT_CAP(_cond, _str) \
932 do { \
933 if (_cond) \
934 printf("\t\t\t" _str "\n"); \
935 } while (0)
936
937 printf("\t\tCapabilities: 0x%02x\n", cap);
938
028c0de5 939 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
357c1a5d
LR
940 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
941 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
942
943 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
944 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
945 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
946
947 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
948 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
949 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
950 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
951
952 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
953 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
954 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
955 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
956
957 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
958
c79c7464
CL
959 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
960 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
357c1a5d
LR
961
962 /*
963 * For beacons and probe response this would mean the BSS
964 * does or does not allow the usage of DSSS/CCK HT40.
965 * Otherwise it means the STA does or does not use
966 * DSSS/CCK HT40.
967 */
968 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
969 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
970
971 /* BIT(13) is reserved */
972
973 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
974
975 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
976#undef PRINT_HT_CAP
977}
7ddfb679
JB
978
979void print_ht_mcs(const __u8 *mcs)
980{
981 /* As defined in 7.3.2.57.4 Supported MCS Set field */
982 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
983 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
984
5ba6a62b 985 max_rx_supp_data_rate = (mcs[10] | ((mcs[11] & 0x3) << 8));
7ddfb679
JB
986 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
987 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
988 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
989 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
990
991 if (max_rx_supp_data_rate)
992 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
993 /* XXX: else see 9.6.0e.5.3 how to get this I think */
994
995 if (tx_mcs_set_defined) {
996 if (tx_mcs_set_equal) {
2a79feb0 997 printf("\t\tHT TX/RX MCS rate indexes supported:");
7ddfb679
JB
998 print_mcs_index(mcs);
999 } else {
1000 printf("\t\tHT RX MCS rate indexes supported:");
1001 print_mcs_index(mcs);
1002
1003 if (tx_unequal_modulation)
1004 printf("\t\tTX unequal modulation supported\n");
1005 else
1006 printf("\t\tTX unequal modulation not supported\n");
1007
1008 printf("\t\tHT TX Max spatial streams: %d\n",
1009 tx_max_num_spatial_streams);
1010
1011 printf("\t\tHT TX MCS rate indexes supported may differ\n");
1012 }
1013 } else {
1014 printf("\t\tHT RX MCS rate indexes supported:");
1015 print_mcs_index(mcs);
089bb35d 1016 printf("\t\tHT TX MCS rate indexes are undefined\n");
7ddfb679
JB
1017 }
1018}
54eb1613
JB
1019
1020void print_vht_info(__u32 capa, const __u8 *mcs)
1021{
1022 __u16 tmp;
1023 int i;
1024
1025 printf("\t\tVHT Capabilities (0x%.8x):\n", capa);
1026
1027#define PRINT_VHT_CAPA(_bit, _str) \
1028 do { \
1029 if (capa & BIT(_bit)) \
1030 printf("\t\t\t" _str "\n"); \
1031 } while (0)
1032
1033 printf("\t\t\tMax MPDU length: ");
1034 switch (capa & 3) {
1035 case 0: printf("3895\n"); break;
1036 case 1: printf("7991\n"); break;
1037 case 2: printf("11454\n"); break;
1038 case 3: printf("(reserved)\n");
1039 }
1040 printf("\t\t\tSupported Channel Width: ");
1041 switch ((capa >> 2) & 3) {
1042 case 0: printf("neither 160 nor 80+80\n"); break;
1043 case 1: printf("160 MHz\n"); break;
1044 case 2: printf("160 MHz, 80+80 MHz\n"); break;
1045 case 3: printf("(reserved)\n");
1046 }
1047 PRINT_VHT_CAPA(4, "RX LDPC");
1048 PRINT_VHT_CAPA(5, "short GI (80 MHz)");
1049 PRINT_VHT_CAPA(6, "short GI (160/80+80 MHz)");
1050 PRINT_VHT_CAPA(7, "TX STBC");
1051 /* RX STBC */
1052 PRINT_VHT_CAPA(11, "SU Beamformer");
1053 PRINT_VHT_CAPA(12, "SU Beamformee");
1054 /* compressed steering */
1055 /* # of sounding dimensions */
1056 PRINT_VHT_CAPA(19, "MU Beamformer");
1057 PRINT_VHT_CAPA(20, "MU Beamformee");
1058 PRINT_VHT_CAPA(21, "VHT TXOP PS");
1059 PRINT_VHT_CAPA(22, "+HTC-VHT");
1060 /* max A-MPDU */
1061 /* VHT link adaptation */
75271051
MB
1062 PRINT_VHT_CAPA(28, "RX antenna pattern consistency");
1063 PRINT_VHT_CAPA(29, "TX antenna pattern consistency");
54eb1613
JB
1064
1065 printf("\t\tVHT RX MCS set:\n");
1066 tmp = mcs[0] | (mcs[1] << 8);
1067 for (i = 1; i <= 8; i++) {
1068 printf("\t\t\t%d streams: ", i);
1069 switch ((tmp >> ((i-1)*2) ) & 3) {
1070 case 0: printf("MCS 0-7\n"); break;
1071 case 1: printf("MCS 0-8\n"); break;
1072 case 2: printf("MCS 0-9\n"); break;
1073 case 3: printf("not supported\n"); break;
1074 }
1075 }
1076 tmp = mcs[2] | (mcs[3] << 8);
1077 printf("\t\tVHT RX highest supported: %d Mbps\n", tmp & 0x1fff);
1078
1079 printf("\t\tVHT TX MCS set:\n");
1080 tmp = mcs[4] | (mcs[5] << 8);
1081 for (i = 1; i <= 8; i++) {
1082 printf("\t\t\t%d streams: ", i);
1083 switch ((tmp >> ((i-1)*2) ) & 3) {
1084 case 0: printf("MCS 0-7\n"); break;
1085 case 1: printf("MCS 0-8\n"); break;
1086 case 2: printf("MCS 0-9\n"); break;
1087 case 3: printf("not supported\n"); break;
1088 }
1089 }
1090 tmp = mcs[6] | (mcs[7] << 8);
1091 printf("\t\tVHT TX highest supported: %d Mbps\n", tmp & 0x1fff);
1092}
492354de
JD
1093
1094void iw_hexdump(const char *prefix, const __u8 *buf, size_t size)
1095{
0ee571d5 1096 size_t i;
492354de
JD
1097
1098 printf("%s: ", prefix);
1099 for (i = 0; i < size; i++) {
1100 if (i && i % 16 == 0)
1101 printf("\n%s: ", prefix);
1102 printf("%02x ", buf[i]);
1103 }
1104 printf("\n\n");
1105}
c1b2b633
SE
1106
1107int get_cf1(const struct chanmode *chanmode, unsigned long freq)
1108{
1109 unsigned int cf1 = freq, j;
1110 unsigned int vht80[] = { 5180, 5260, 5500, 5580, 5660, 5745 };
1111
1112 switch (chanmode->width) {
1113 case NL80211_CHAN_WIDTH_80:
1114 /* setup center_freq1 */
1115 for (j = 0; j < ARRAY_SIZE(vht80); j++) {
1116 if (freq >= vht80[j] && freq < vht80[j] + 80)
1117 break;
1118 }
1119
1120 if (j == ARRAY_SIZE(vht80))
1121 break;
1122
1123 cf1 = vht80[j] + 30;
1124 break;
1125 default:
1126 cf1 = freq + chanmode->freq1_diff;
1127 break;
1128 }
1129
1130 return cf1;
1131}