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