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