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