]> git.ipfire.org Git - thirdparty/iw.git/blame - util.c
update nl80211.h
[thirdparty/iw.git] / util.c
CommitLineData
748f8489 1#include <ctype.h>
51e9bd80
JB
2#include <netlink/attr.h>
3#include <errno.h>
4#include <stdbool.h>
3d1e8704 5#include "iw.h"
f5f7b1d0 6#include "nl80211.h"
3d1e8704 7
febeb0c0 8void mac_addr_n2a(char *mac_addr, unsigned char *arg)
3d1e8704 9{
53e5ce7a 10 int i, l;
3d1e8704
LCC
11
12 l = 0;
13 for (i = 0; i < ETH_ALEN ; i++) {
14 if (i == 0) {
53e5ce7a 15 sprintf(mac_addr+l, "%02x", arg[i]);
3d1e8704
LCC
16 l += 2;
17 } else {
53e5ce7a 18 sprintf(mac_addr+l, ":%02x", arg[i]);
3d1e8704
LCC
19 l += 3;
20 }
21 }
3d1e8704
LCC
22}
23
24int 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}
541ef425 50
236d4191
JB
51unsigned char *parse_hex(char *hex, size_t *outlen)
52{
53 size_t len = strlen(hex);
54 unsigned char *result = calloc(len/2 + 2, 1);
55 int pos = 0;
56
57 if (!result)
58 return NULL;
59
60 *outlen = 0;
61
62 while (1) {
63 int temp;
64 char *cp = strchr(hex, ':');
65 if (cp) {
66 *cp = 0;
67 cp++;
68 }
69 if (sscanf(hex, "%x", &temp) != 1)
70 goto error;
71 if (temp < 0 || temp > 255)
72 goto error;
73
74 (*outlen)++;
75
76 result[pos++] = temp;
77 if (!cp)
78 break;
79 hex = cp;
80 }
81
82 return result;
83 error:
84 free(result);
85 return NULL;
86}
87
541ef425
JB
88static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
89 "unspecified",
90 "IBSS",
34e78ed0 91 "managed",
541ef425 92 "AP",
34e78ed0 93 "AP/VLAN",
541ef425 94 "WDS",
34e78ed0 95 "monitor",
a4464243
JB
96 "mesh point",
97 "P2P-client",
98 "P2P-GO",
541ef425
JB
99};
100
101static char modebuf[100];
102
103const char *iftype_name(enum nl80211_iftype iftype)
104{
105 if (iftype <= NL80211_IFTYPE_MAX)
106 return ifmodes[iftype];
107 sprintf(modebuf, "Unknown mode (%d)", iftype);
108 return modebuf;
109}
379f8397 110
9990c1e9 111static const char *commands[NL80211_CMD_MAX + 1] = {
06339499
JB
112 [NL80211_CMD_GET_WIPHY] = "get_wiphy",
113 [NL80211_CMD_SET_WIPHY] = "set_wiphy",
114 [NL80211_CMD_NEW_WIPHY] = "new_wiphy",
115 [NL80211_CMD_DEL_WIPHY] = "del_wiphy",
116 [NL80211_CMD_GET_INTERFACE] = "get_interface",
117 [NL80211_CMD_SET_INTERFACE] = "set_interface",
118 [NL80211_CMD_NEW_INTERFACE] = "new_interface",
119 [NL80211_CMD_DEL_INTERFACE] = "del_interface",
120 [NL80211_CMD_GET_KEY] = "get_key",
121 [NL80211_CMD_SET_KEY] = "set_key",
122 [NL80211_CMD_NEW_KEY] = "new_key",
123 [NL80211_CMD_DEL_KEY] = "del_key",
124 [NL80211_CMD_GET_BEACON] = "get_beacon",
125 [NL80211_CMD_SET_BEACON] = "set_beacon",
126 [NL80211_CMD_NEW_BEACON] = "new_beacon",
127 [NL80211_CMD_DEL_BEACON] = "del_beacon",
128 [NL80211_CMD_GET_STATION] = "get_station",
129 [NL80211_CMD_SET_STATION] = "set_station",
130 [NL80211_CMD_NEW_STATION] = "new_station",
131 [NL80211_CMD_DEL_STATION] = "del_station",
132 [NL80211_CMD_GET_MPATH] = "get_mpath",
133 [NL80211_CMD_SET_MPATH] = "set_mpath",
134 [NL80211_CMD_NEW_MPATH] = "new_mpath",
135 [NL80211_CMD_DEL_MPATH] = "del_mpath",
136 [NL80211_CMD_SET_BSS] = "set_bss",
137 [NL80211_CMD_SET_REG] = "set_reg",
138 [NL80211_CMD_REQ_SET_REG] = "reg_set_reg",
139 [NL80211_CMD_GET_MESH_PARAMS] = "get_mesh_params",
140 [NL80211_CMD_SET_MESH_PARAMS] = "set_mesh_params",
141 [NL80211_CMD_SET_MGMT_EXTRA_IE] = "set_mgmt_extra_ie",
142 [NL80211_CMD_GET_REG] = "get_reg",
143 [NL80211_CMD_GET_SCAN] = "get_scan",
144 [NL80211_CMD_TRIGGER_SCAN] = "trigger_scan",
145 [NL80211_CMD_NEW_SCAN_RESULTS] = "new_scan_results",
146 [NL80211_CMD_SCAN_ABORTED] = "scan_aborted",
147 [NL80211_CMD_REG_CHANGE] = "reg_change",
148 [NL80211_CMD_AUTHENTICATE] = "authenticate",
149 [NL80211_CMD_ASSOCIATE] = "associate",
150 [NL80211_CMD_DEAUTHENTICATE] = "deauthenticate",
151 [NL80211_CMD_DISASSOCIATE] = "disassociate",
152 [NL80211_CMD_MICHAEL_MIC_FAILURE] = "michael_mic_failure",
153 [NL80211_CMD_REG_BEACON_HINT] = "reg_beacon_hint",
154 [NL80211_CMD_JOIN_IBSS] = "join_ibss",
155 [NL80211_CMD_LEAVE_IBSS] = "leave_ibss",
156 [NL80211_CMD_TESTMODE] = "testmode",
157 [NL80211_CMD_CONNECT] = "connect",
158 [NL80211_CMD_ROAM] = "roam",
159 [NL80211_CMD_DISCONNECT] = "disconnect",
160 [NL80211_CMD_SET_WIPHY_NETNS] = "set_wiphy_netns",
161 [NL80211_CMD_GET_SURVEY] = "get_survey",
162 [NL80211_CMD_SET_PMKSA] = "set_pmksa",
163 [NL80211_CMD_DEL_PMKSA] = "del_pmksa",
164 [NL80211_CMD_FLUSH_PMKSA] = "flush_pmksa",
165 [NL80211_CMD_REMAIN_ON_CHANNEL] = "remain_on_channel",
166 [NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL] = "cancel_remain_on_channel",
167 [NL80211_CMD_SET_TX_BITRATE_MASK] = "set_tx_bitrate_mask",
168 [NL80211_CMD_REGISTER_ACTION] = "register_action",
169 [NL80211_CMD_ACTION] = "action",
7b96e9e2 170 [NL80211_CMD_SET_CHANNEL] = "set_channel",
dcf57038
JB
171 [NL80211_CMD_SET_WDS_PEER] = "set_wds_peer",
172 [NL80211_CMD_FRAME_WAIT_CANCEL] = "frame_wait_cancel",
173 [NL80211_CMD_JOIN_MESH] = "join_mesh",
174 [NL80211_CMD_LEAVE_MESH] = "leave_mesh",
9990c1e9
MH
175};
176
177static char cmdbuf[100];
178
179const char *command_name(enum nl80211_commands cmd)
180{
73780397 181 if (cmd <= NL80211_CMD_MAX && commands[cmd])
9990c1e9
MH
182 return commands[cmd];
183 sprintf(cmdbuf, "Unknown command (%d)", cmd);
184 return cmdbuf;
185}
186
379f8397
JB
187int ieee80211_channel_to_frequency(int chan)
188{
189 if (chan < 14)
190 return 2407 + chan * 5;
191
192 if (chan == 14)
193 return 2484;
194
195 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
196 return (chan + 1000) * 5;
197}
198
199int ieee80211_frequency_to_channel(int freq)
200{
201 if (freq == 2484)
202 return 14;
203
204 if (freq < 2484)
205 return (freq - 2407) / 5;
206
207 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
208 return freq/5 - 1000;
209}
748f8489
JB
210
211void print_ssid_escaped(const uint8_t len, const uint8_t *data)
212{
213 int i;
214
215 for (i = 0; i < len; i++) {
216 if (isprint(data[i]))
217 printf("%c", data[i]);
218 else
219 printf("\\x%.2x", data[i]);
220 }
221}
51e9bd80
JB
222
223static int hex2num(char digit)
224{
225 if (!isxdigit(digit))
226 return -1;
227 if (isdigit(digit))
228 return digit - '0';
229 return tolower(digit) - 'a' + 10;
230}
231
232static int hex2byte(char *hex)
233{
234 int d1, d2;
235
236 d1 = hex2num(hex[0]);
237 if (d1 < 0)
238 return -1;
239 d2 = hex2num(hex[1]);
240 if (d2 < 0)
241 return -1;
242 return (d1 << 4) | d2;
243}
244
245static char *hex2bin(char *hex, char *buf)
246{
247 char *result = buf;
248 int d;
249
250 while (hex[0]) {
251 d = hex2byte(hex);
252 if (d < 0)
253 return NULL;
254 buf[0] = d;
255 buf++;
256 hex += 2;
257 }
258
259 return result;
260}
261
262int parse_keys(struct nl_msg *msg, char **argv, int argc)
263{
264 struct nlattr *keys;
265 int i = 0;
041581ce 266 bool have_default = false;
51e9bd80
JB
267 char keybuf[13];
268
269 if (!argc)
270 return 1;
271
272 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
273
274 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
275 if (!keys)
276 return -ENOBUFS;
277
278 do {
279 char *arg = *argv;
280 int pos = 0, keylen;
281 struct nlattr *key = nla_nest_start(msg, ++i);
282 char *keydata;
283
284 if (!key)
285 return -ENOBUFS;
286
287 if (arg[pos] == 'd') {
288 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
289 pos++;
290 if (arg[pos] == ':')
291 pos++;
041581ce 292 have_default = true;
51e9bd80
JB
293 }
294
295 if (!isdigit(arg[pos]))
296 goto explain;
297 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
298 if (arg[pos++] != ':')
299 goto explain;
300 keydata = arg + pos;
301 switch (strlen(keydata)) {
302 case 10:
303 keydata = hex2bin(keydata, keybuf);
304 case 5:
305 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
306 keylen = 5;
307 break;
308 case 26:
309 keydata = hex2bin(keydata, keybuf);
310 case 13:
311 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
312 keylen = 13;
313 break;
314 default:
315 goto explain;
316 }
317
318 if (!keydata)
319 goto explain;
320
321 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
322
51e9bd80
JB
323 argv++;
324 argc--;
041581ce
JB
325
326 /* one key should be TX key */
327 if (!have_default && !argc)
328 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
329
330 nla_nest_end(msg, key);
51e9bd80
JB
331 } while (argc);
332
333 nla_nest_end(msg, keys);
334
335 return 0;
336 nla_put_failure:
337 return -ENOBUFS;
338 explain:
339 fprintf(stderr, "key must be [d:]index:data where\n"
340 " 'd:' means default (transmit) key\n"
341 " 'index:' is a single digit (0-3)\n"
342 " 'data' must be 5 or 13 ascii chars\n"
343 " or 10 or 26 hex digits\n"
344 "for example: d:2:6162636465 is the same as d:2:abcde\n");
345 return 2;
346}
deb3501c 347
7ddfb679 348static void print_mcs_index(const __u8 *mcs)
deb3501c 349{
04953e90
JB
350 unsigned int mcs_bit, prev_bit = -2, prev_cont = 0;
351
352 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
353 unsigned int mcs_octet = mcs_bit/8;
354 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
355 bool mcs_rate_idx_set;
356
357 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
358
359 if (!mcs_rate_idx_set)
360 continue;
361
362 if (prev_bit != mcs_bit - 1) {
363 if (prev_bit != -2)
364 printf("%d, ", prev_bit);
365 else
366 printf(" ");
367 printf("%d", mcs_bit);
368 prev_cont = 0;
369 } else if (!prev_cont) {
370 printf("-");
371 prev_cont = 1;
372 }
373
374 prev_bit = mcs_bit;
375 }
deb3501c 376
04953e90
JB
377 if (prev_cont)
378 printf("%d", prev_bit);
379 printf("\n");
deb3501c 380}
0950993f
LR
381
382/*
383 * There are only 4 possible values, we just use a case instead of computing it,
384 * but technically this can also be computed through the formula:
385 *
386 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
387 */
388static __u32 compute_ampdu_length(__u8 exponent)
389{
390 switch (exponent) {
391 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
392 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
393 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
394 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
395 default: return 0;
396 }
397}
398
399static const char *print_ampdu_space(__u8 space)
400{
401 switch (space) {
402 case 0: return "No restriction";
403 case 1: return "1/4 usec";
404 case 2: return "1/2 usec";
405 case 3: return "1 usec";
406 case 4: return "2 usec";
407 case 5: return "4 usec";
408 case 6: return "8 usec";
409 case 7: return "16 usec";
410 default:
7ae93cd5 411 return "BUG (spacing more than 3 bits!)";
0950993f
LR
412 }
413}
414
415void print_ampdu_length(__u8 exponent)
416{
04953e90 417 __u32 max_ampdu_length;
0950993f
LR
418
419 max_ampdu_length = compute_ampdu_length(exponent);
420
421 if (max_ampdu_length) {
422 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
423 max_ampdu_length, exponent);
424 } else {
425 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
426 "(exponent: %d)\n", exponent);
427 }
428}
429
430void print_ampdu_spacing(__u8 spacing)
431{
432 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
433 print_ampdu_space(spacing), spacing);
434}
357c1a5d
LR
435
436void print_ht_capability(__u16 cap)
437{
438#define PRINT_HT_CAP(_cond, _str) \
439 do { \
440 if (_cond) \
441 printf("\t\t\t" _str "\n"); \
442 } while (0)
443
444 printf("\t\tCapabilities: 0x%02x\n", cap);
445
028c0de5 446 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
357c1a5d
LR
447 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
448 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
449
450 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
451 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
452 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
453
454 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
455 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
456 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
457 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
458
459 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
460 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
461 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
462 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
463
464 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
465
c79c7464
CL
466 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
467 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
357c1a5d
LR
468
469 /*
470 * For beacons and probe response this would mean the BSS
471 * does or does not allow the usage of DSSS/CCK HT40.
472 * Otherwise it means the STA does or does not use
473 * DSSS/CCK HT40.
474 */
475 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
476 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
477
478 /* BIT(13) is reserved */
479
480 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
481
482 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
483#undef PRINT_HT_CAP
484}
7ddfb679
JB
485
486void print_ht_mcs(const __u8 *mcs)
487{
488 /* As defined in 7.3.2.57.4 Supported MCS Set field */
489 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
490 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
491
492 max_rx_supp_data_rate = ((mcs[10] >> 8) & ((mcs[11] & 0x3) << 8));
493 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
494 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
495 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
496 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
497
498 if (max_rx_supp_data_rate)
499 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
500 /* XXX: else see 9.6.0e.5.3 how to get this I think */
501
502 if (tx_mcs_set_defined) {
503 if (tx_mcs_set_equal) {
2a79feb0 504 printf("\t\tHT TX/RX MCS rate indexes supported:");
7ddfb679
JB
505 print_mcs_index(mcs);
506 } else {
507 printf("\t\tHT RX MCS rate indexes supported:");
508 print_mcs_index(mcs);
509
510 if (tx_unequal_modulation)
511 printf("\t\tTX unequal modulation supported\n");
512 else
513 printf("\t\tTX unequal modulation not supported\n");
514
515 printf("\t\tHT TX Max spatial streams: %d\n",
516 tx_max_num_spatial_streams);
517
518 printf("\t\tHT TX MCS rate indexes supported may differ\n");
519 }
520 } else {
521 printf("\t\tHT RX MCS rate indexes supported:");
522 print_mcs_index(mcs);
089bb35d 523 printf("\t\tHT TX MCS rate indexes are undefined\n");
7ddfb679
JB
524 }
525}