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