]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
print wdev id in event info
[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 };
136
137 static char modebuf[100];
138
139 const char *iftype_name(enum nl80211_iftype iftype)
140 {
141 if (iftype <= NL80211_IFTYPE_MAX)
142 return ifmodes[iftype];
143 sprintf(modebuf, "Unknown mode (%d)", iftype);
144 return modebuf;
145 }
146
147 static const char *commands[NL80211_CMD_MAX + 1] = {
148 /*
149 * sed 's/^\tNL80211_CMD_//;t n;d;:n s%^\([^=]*\),.*%\t[NL80211_CMD_\1] = \"\L\1\",%;t;d' nl80211.h
150 */
151 [NL80211_CMD_UNSPEC] = "unspec",
152 [NL80211_CMD_GET_WIPHY] = "get_wiphy",
153 [NL80211_CMD_SET_WIPHY] = "set_wiphy",
154 [NL80211_CMD_NEW_WIPHY] = "new_wiphy",
155 [NL80211_CMD_DEL_WIPHY] = "del_wiphy",
156 [NL80211_CMD_GET_INTERFACE] = "get_interface",
157 [NL80211_CMD_SET_INTERFACE] = "set_interface",
158 [NL80211_CMD_NEW_INTERFACE] = "new_interface",
159 [NL80211_CMD_DEL_INTERFACE] = "del_interface",
160 [NL80211_CMD_GET_KEY] = "get_key",
161 [NL80211_CMD_SET_KEY] = "set_key",
162 [NL80211_CMD_NEW_KEY] = "new_key",
163 [NL80211_CMD_DEL_KEY] = "del_key",
164 [NL80211_CMD_GET_BEACON] = "get_beacon",
165 [NL80211_CMD_SET_BEACON] = "set_beacon",
166 [NL80211_CMD_START_AP] = "start_ap",
167 [NL80211_CMD_STOP_AP] = "stop_ap",
168 [NL80211_CMD_GET_STATION] = "get_station",
169 [NL80211_CMD_SET_STATION] = "set_station",
170 [NL80211_CMD_NEW_STATION] = "new_station",
171 [NL80211_CMD_DEL_STATION] = "del_station",
172 [NL80211_CMD_GET_MPATH] = "get_mpath",
173 [NL80211_CMD_SET_MPATH] = "set_mpath",
174 [NL80211_CMD_NEW_MPATH] = "new_mpath",
175 [NL80211_CMD_DEL_MPATH] = "del_mpath",
176 [NL80211_CMD_SET_BSS] = "set_bss",
177 [NL80211_CMD_SET_REG] = "set_reg",
178 [NL80211_CMD_REQ_SET_REG] = "req_set_reg",
179 [NL80211_CMD_GET_MESH_CONFIG] = "get_mesh_config",
180 [NL80211_CMD_SET_MESH_CONFIG] = "set_mesh_config",
181 [NL80211_CMD_GET_REG] = "get_reg",
182 [NL80211_CMD_GET_SCAN] = "get_scan",
183 [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan",
184 [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results",
185 [NL80211_CMD_SCAN_ABORTED] = "scan_aborted",
186 [NL80211_CMD_REG_CHANGE] = "reg_change",
187 [NL80211_CMD_AUTHENTICATE] = "authenticate",
188 [NL80211_CMD_ASSOCIATE] = "associate",
189 [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate",
190 [NL80211_CMD_DISASSOCIATE] = "disassociate",
191 [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure",
192 [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint",
193 [NL80211_CMD_JOIN_IBSS] = "join_ibss",
194 [NL80211_CMD_LEAVE_IBSS] = "leave_ibss",
195 [NL80211_CMD_TESTMODE] = "testmode",
196 [NL80211_CMD_CONNECT] = "connect",
197 [NL80211_CMD_ROAM] = "roam",
198 [NL80211_CMD_DISCONNECT] = "disconnect",
199 [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns",
200 [NL80211_CMD_GET_SURVEY] = "get_survey",
201 [NL80211_CMD_NEW_SURVEY_RESULTS] = "new_survey_results",
202 [NL80211_CMD_SET_PMKSA] = "set_pmksa",
203 [NL80211_CMD_DEL_PMKSA] = "del_pmksa",
204 [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa",
205 [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel",
206 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel",
207 [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask",
208 [NL80211_CMD_REGISTER_FRAME] = "register_frame",
209 [NL80211_CMD_FRAME] = "frame",
210 [NL80211_CMD_FRAME_TX_STATUS] = "frame_tx_status",
211 [NL80211_CMD_SET_POWER_SAVE] = "set_power_save",
212 [NL80211_CMD_GET_POWER_SAVE] = "get_power_save",
213 [NL80211_CMD_SET_CQM] = "set_cqm",
214 [NL80211_CMD_NOTIFY_CQM] = "notify_cqm",
215 [NL80211_CMD_SET_CHANNEL] = "set_channel",
216 [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer",
217 [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel",
218 [NL80211_CMD_JOIN_MESH] = "join_mesh",
219 [NL80211_CMD_LEAVE_MESH] = "leave_mesh",
220 [NL80211_CMD_UNPROT_DEAUTHENTICATE] = "unprot_deauthenticate",
221 [NL80211_CMD_UNPROT_DISASSOCIATE] = "unprot_disassociate",
222 [NL80211_CMD_NEW_PEER_CANDIDATE] = "new_peer_candidate",
223 [NL80211_CMD_GET_WOWLAN] = "get_wowlan",
224 [NL80211_CMD_SET_WOWLAN] = "set_wowlan",
225 [NL80211_CMD_START_SCHED_SCAN] = "start_sched_scan",
226 [NL80211_CMD_STOP_SCHED_SCAN] = "stop_sched_scan",
227 [NL80211_CMD_SCHED_SCAN_RESULTS] = "sched_scan_results",
228 [NL80211_CMD_SCHED_SCAN_STOPPED] = "sched_scan_stopped",
229 [NL80211_CMD_SET_REKEY_OFFLOAD] = "set_rekey_offload",
230 [NL80211_CMD_PMKSA_CANDIDATE] = "pmksa_candidate",
231 [NL80211_CMD_TDLS_OPER] = "tdls_oper",
232 [NL80211_CMD_TDLS_MGMT] = "tdls_mgmt",
233 [NL80211_CMD_UNEXPECTED_FRAME] = "unexpected_frame",
234 [NL80211_CMD_PROBE_CLIENT] = "probe_client",
235 [NL80211_CMD_REGISTER_BEACONS] = "register_beacons",
236 [NL80211_CMD_UNEXPECTED_4ADDR_FRAME] = "unexpected_4addr_frame",
237 [NL80211_CMD_SET_NOACK_MAP] = "set_noack_map",
238 [NL80211_CMD_CH_SWITCH_NOTIFY] = "ch_switch_notify",
239 };
240
241 static char cmdbuf[100];
242
243 const char *command_name(enum nl80211_commands cmd)
244 {
245 if (cmd <= NL80211_CMD_MAX && commands[cmd])
246 return commands[cmd];
247 sprintf(cmdbuf, "Unknown command (%d)", cmd);
248 return cmdbuf;
249 }
250
251 int ieee80211_channel_to_frequency(int chan)
252 {
253 if (chan < 14)
254 return 2407 + chan * 5;
255
256 if (chan == 14)
257 return 2484;
258
259 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
260 return (chan + 1000) * 5;
261 }
262
263 int ieee80211_frequency_to_channel(int freq)
264 {
265 if (freq == 2484)
266 return 14;
267
268 if (freq < 2484)
269 return (freq - 2407) / 5;
270
271 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
272 if (freq < 45000)
273 return freq/5 - 1000;
274
275 if (freq >= 58320 && freq <= 64800)
276 return (freq - 56160) / 2160;
277
278 return 0;
279 }
280
281 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
282 {
283 int i;
284
285 for (i = 0; i < len; i++) {
286 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
287 printf("%c", data[i]);
288 else if (data[i] == ' ' &&
289 (i != 0 && i != len -1))
290 printf(" ");
291 else
292 printf("\\x%.2x", data[i]);
293 }
294 }
295
296 static int hex2num(char digit)
297 {
298 if (!isxdigit(digit))
299 return -1;
300 if (isdigit(digit))
301 return digit - '0';
302 return tolower(digit) - 'a' + 10;
303 }
304
305 static int hex2byte(char *hex)
306 {
307 int d1, d2;
308
309 d1 = hex2num(hex[0]);
310 if (d1 < 0)
311 return -1;
312 d2 = hex2num(hex[1]);
313 if (d2 < 0)
314 return -1;
315 return (d1 << 4) | d2;
316 }
317
318 static char *hex2bin(char *hex, char *buf)
319 {
320 char *result = buf;
321 int d;
322
323 while (hex[0]) {
324 d = hex2byte(hex);
325 if (d < 0)
326 return NULL;
327 buf[0] = d;
328 buf++;
329 hex += 2;
330 }
331
332 return result;
333 }
334
335 int parse_keys(struct nl_msg *msg, char **argv, int argc)
336 {
337 struct nlattr *keys;
338 int i = 0;
339 bool have_default = false;
340 char keybuf[13];
341
342 if (!argc)
343 return 1;
344
345 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
346
347 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
348 if (!keys)
349 return -ENOBUFS;
350
351 do {
352 char *arg = *argv;
353 int pos = 0, keylen;
354 struct nlattr *key = nla_nest_start(msg, ++i);
355 char *keydata;
356
357 if (!key)
358 return -ENOBUFS;
359
360 if (arg[pos] == 'd') {
361 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
362 pos++;
363 if (arg[pos] == ':')
364 pos++;
365 have_default = true;
366 }
367
368 if (!isdigit(arg[pos]))
369 goto explain;
370 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
371 if (arg[pos++] != ':')
372 goto explain;
373 keydata = arg + pos;
374 switch (strlen(keydata)) {
375 case 10:
376 keydata = hex2bin(keydata, keybuf);
377 case 5:
378 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
379 keylen = 5;
380 break;
381 case 26:
382 keydata = hex2bin(keydata, keybuf);
383 case 13:
384 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
385 keylen = 13;
386 break;
387 default:
388 goto explain;
389 }
390
391 if (!keydata)
392 goto explain;
393
394 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
395
396 argv++;
397 argc--;
398
399 /* one key should be TX key */
400 if (!have_default && !argc)
401 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
402
403 nla_nest_end(msg, key);
404 } while (argc);
405
406 nla_nest_end(msg, keys);
407
408 return 0;
409 nla_put_failure:
410 return -ENOBUFS;
411 explain:
412 fprintf(stderr, "key must be [d:]index:data where\n"
413 " 'd:' means default (transmit) key\n"
414 " 'index:' is a single digit (0-3)\n"
415 " 'data' must be 5 or 13 ascii chars\n"
416 " or 10 or 26 hex digits\n"
417 "for example: d:2:6162636465 is the same as d:2:abcde\n");
418 return 2;
419 }
420
421 static void print_mcs_index(const __u8 *mcs)
422 {
423 int mcs_bit, prev_bit = -2, prev_cont = 0;
424
425 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
426 unsigned int mcs_octet = mcs_bit/8;
427 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
428 bool mcs_rate_idx_set;
429
430 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
431
432 if (!mcs_rate_idx_set)
433 continue;
434
435 if (prev_bit != mcs_bit - 1) {
436 if (prev_bit != -2)
437 printf("%d, ", prev_bit);
438 else
439 printf(" ");
440 printf("%d", mcs_bit);
441 prev_cont = 0;
442 } else if (!prev_cont) {
443 printf("-");
444 prev_cont = 1;
445 }
446
447 prev_bit = mcs_bit;
448 }
449
450 if (prev_cont)
451 printf("%d", prev_bit);
452 printf("\n");
453 }
454
455 /*
456 * There are only 4 possible values, we just use a case instead of computing it,
457 * but technically this can also be computed through the formula:
458 *
459 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
460 */
461 static __u32 compute_ampdu_length(__u8 exponent)
462 {
463 switch (exponent) {
464 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
465 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
466 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
467 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
468 default: return 0;
469 }
470 }
471
472 static const char *print_ampdu_space(__u8 space)
473 {
474 switch (space) {
475 case 0: return "No restriction";
476 case 1: return "1/4 usec";
477 case 2: return "1/2 usec";
478 case 3: return "1 usec";
479 case 4: return "2 usec";
480 case 5: return "4 usec";
481 case 6: return "8 usec";
482 case 7: return "16 usec";
483 default:
484 return "BUG (spacing more than 3 bits!)";
485 }
486 }
487
488 void print_ampdu_length(__u8 exponent)
489 {
490 __u32 max_ampdu_length;
491
492 max_ampdu_length = compute_ampdu_length(exponent);
493
494 if (max_ampdu_length) {
495 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
496 max_ampdu_length, exponent);
497 } else {
498 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
499 "(exponent: %d)\n", exponent);
500 }
501 }
502
503 void print_ampdu_spacing(__u8 spacing)
504 {
505 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
506 print_ampdu_space(spacing), spacing);
507 }
508
509 void print_ht_capability(__u16 cap)
510 {
511 #define PRINT_HT_CAP(_cond, _str) \
512 do { \
513 if (_cond) \
514 printf("\t\t\t" _str "\n"); \
515 } while (0)
516
517 printf("\t\tCapabilities: 0x%02x\n", cap);
518
519 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
520 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
521 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
522
523 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
524 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
525 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
526
527 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
528 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
529 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
530 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
531
532 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
533 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
534 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
535 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
536
537 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
538
539 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
540 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
541
542 /*
543 * For beacons and probe response this would mean the BSS
544 * does or does not allow the usage of DSSS/CCK HT40.
545 * Otherwise it means the STA does or does not use
546 * DSSS/CCK HT40.
547 */
548 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
549 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
550
551 /* BIT(13) is reserved */
552
553 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
554
555 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
556 #undef PRINT_HT_CAP
557 }
558
559 void print_ht_mcs(const __u8 *mcs)
560 {
561 /* As defined in 7.3.2.57.4 Supported MCS Set field */
562 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
563 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
564
565 max_rx_supp_data_rate = ((mcs[10] >> 8) & ((mcs[11] & 0x3) << 8));
566 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
567 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
568 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
569 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
570
571 if (max_rx_supp_data_rate)
572 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
573 /* XXX: else see 9.6.0e.5.3 how to get this I think */
574
575 if (tx_mcs_set_defined) {
576 if (tx_mcs_set_equal) {
577 printf("\t\tHT TX/RX MCS rate indexes supported:");
578 print_mcs_index(mcs);
579 } else {
580 printf("\t\tHT RX MCS rate indexes supported:");
581 print_mcs_index(mcs);
582
583 if (tx_unequal_modulation)
584 printf("\t\tTX unequal modulation supported\n");
585 else
586 printf("\t\tTX unequal modulation not supported\n");
587
588 printf("\t\tHT TX Max spatial streams: %d\n",
589 tx_max_num_spatial_streams);
590
591 printf("\t\tHT TX MCS rate indexes supported may differ\n");
592 }
593 } else {
594 printf("\t\tHT RX MCS rate indexes supported:");
595 print_mcs_index(mcs);
596 printf("\t\tHT TX MCS rate indexes are undefined\n");
597 }
598 }