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