]> git.ipfire.org Git - thirdparty/iw.git/blob - util.c
mesh: add support for NL80211_MESHCONF_ELEMENT_TTL
[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 unsigned 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
88 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
89 "unspecified",
90 "IBSS",
91 "managed",
92 "AP",
93 "AP/VLAN",
94 "WDS",
95 "monitor",
96 "mesh point",
97 "P2P-client",
98 "P2P-GO",
99 };
100
101 static char modebuf[100];
102
103 const 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 }
110
111 static const char *commands[NL80211_CMD_MAX + 1] = {
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",
170 [NL80211_CMD_SET_CHANNEL] = "set_channel",
171 };
172
173 static char cmdbuf[100];
174
175 const char *command_name(enum nl80211_commands cmd)
176 {
177 if (cmd <= NL80211_CMD_MAX && commands[cmd])
178 return commands[cmd];
179 sprintf(cmdbuf, "Unknown command (%d)", cmd);
180 return cmdbuf;
181 }
182
183 int ieee80211_channel_to_frequency(int chan)
184 {
185 if (chan < 14)
186 return 2407 + chan * 5;
187
188 if (chan == 14)
189 return 2484;
190
191 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
192 return (chan + 1000) * 5;
193 }
194
195 int ieee80211_frequency_to_channel(int freq)
196 {
197 if (freq == 2484)
198 return 14;
199
200 if (freq < 2484)
201 return (freq - 2407) / 5;
202
203 /* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
204 return freq/5 - 1000;
205 }
206
207 void print_ssid_escaped(const uint8_t len, const uint8_t *data)
208 {
209 int i;
210
211 for (i = 0; i < len; i++) {
212 if (isprint(data[i]))
213 printf("%c", data[i]);
214 else
215 printf("\\x%.2x", data[i]);
216 }
217 }
218
219 static int hex2num(char digit)
220 {
221 if (!isxdigit(digit))
222 return -1;
223 if (isdigit(digit))
224 return digit - '0';
225 return tolower(digit) - 'a' + 10;
226 }
227
228 static int hex2byte(char *hex)
229 {
230 int d1, d2;
231
232 d1 = hex2num(hex[0]);
233 if (d1 < 0)
234 return -1;
235 d2 = hex2num(hex[1]);
236 if (d2 < 0)
237 return -1;
238 return (d1 << 4) | d2;
239 }
240
241 static char *hex2bin(char *hex, char *buf)
242 {
243 char *result = buf;
244 int d;
245
246 while (hex[0]) {
247 d = hex2byte(hex);
248 if (d < 0)
249 return NULL;
250 buf[0] = d;
251 buf++;
252 hex += 2;
253 }
254
255 return result;
256 }
257
258 int parse_keys(struct nl_msg *msg, char **argv, int argc)
259 {
260 struct nlattr *keys;
261 int i = 0;
262 bool have_default = false;
263 char keybuf[13];
264
265 if (!argc)
266 return 1;
267
268 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
269
270 keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
271 if (!keys)
272 return -ENOBUFS;
273
274 do {
275 char *arg = *argv;
276 int pos = 0, keylen;
277 struct nlattr *key = nla_nest_start(msg, ++i);
278 char *keydata;
279
280 if (!key)
281 return -ENOBUFS;
282
283 if (arg[pos] == 'd') {
284 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
285 pos++;
286 if (arg[pos] == ':')
287 pos++;
288 have_default = true;
289 }
290
291 if (!isdigit(arg[pos]))
292 goto explain;
293 NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
294 if (arg[pos++] != ':')
295 goto explain;
296 keydata = arg + pos;
297 switch (strlen(keydata)) {
298 case 10:
299 keydata = hex2bin(keydata, keybuf);
300 case 5:
301 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
302 keylen = 5;
303 break;
304 case 26:
305 keydata = hex2bin(keydata, keybuf);
306 case 13:
307 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
308 keylen = 13;
309 break;
310 default:
311 goto explain;
312 }
313
314 if (!keydata)
315 goto explain;
316
317 NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
318
319 argv++;
320 argc--;
321
322 /* one key should be TX key */
323 if (!have_default && !argc)
324 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
325
326 nla_nest_end(msg, key);
327 } while (argc);
328
329 nla_nest_end(msg, keys);
330
331 return 0;
332 nla_put_failure:
333 return -ENOBUFS;
334 explain:
335 fprintf(stderr, "key must be [d:]index:data where\n"
336 " 'd:' means default (transmit) key\n"
337 " 'index:' is a single digit (0-3)\n"
338 " 'data' must be 5 or 13 ascii chars\n"
339 " or 10 or 26 hex digits\n"
340 "for example: d:2:6162636465 is the same as d:2:abcde\n");
341 return 2;
342 }
343
344 static void print_mcs_index(const __u8 *mcs)
345 {
346 unsigned int mcs_bit, prev_bit = -2, prev_cont = 0;
347
348 for (mcs_bit = 0; mcs_bit <= 76; mcs_bit++) {
349 unsigned int mcs_octet = mcs_bit/8;
350 unsigned int MCS_RATE_BIT = 1 << mcs_bit % 8;
351 bool mcs_rate_idx_set;
352
353 mcs_rate_idx_set = !!(mcs[mcs_octet] & MCS_RATE_BIT);
354
355 if (!mcs_rate_idx_set)
356 continue;
357
358 if (prev_bit != mcs_bit - 1) {
359 if (prev_bit != -2)
360 printf("%d, ", prev_bit);
361 else
362 printf(" ");
363 printf("%d", mcs_bit);
364 prev_cont = 0;
365 } else if (!prev_cont) {
366 printf("-");
367 prev_cont = 1;
368 }
369
370 prev_bit = mcs_bit;
371 }
372
373 if (prev_cont)
374 printf("%d", prev_bit);
375 printf("\n");
376 }
377
378 /*
379 * There are only 4 possible values, we just use a case instead of computing it,
380 * but technically this can also be computed through the formula:
381 *
382 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
383 */
384 static __u32 compute_ampdu_length(__u8 exponent)
385 {
386 switch (exponent) {
387 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
388 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
389 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
390 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
391 default: return 0;
392 }
393 }
394
395 static const char *print_ampdu_space(__u8 space)
396 {
397 switch (space) {
398 case 0: return "No restriction";
399 case 1: return "1/4 usec";
400 case 2: return "1/2 usec";
401 case 3: return "1 usec";
402 case 4: return "2 usec";
403 case 5: return "4 usec";
404 case 6: return "8 usec";
405 case 7: return "16 usec";
406 default:
407 return "BUG (spacing more than 3 bits!)";
408 }
409 }
410
411 void print_ampdu_length(__u8 exponent)
412 {
413 __u32 max_ampdu_length;
414
415 max_ampdu_length = compute_ampdu_length(exponent);
416
417 if (max_ampdu_length) {
418 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
419 max_ampdu_length, exponent);
420 } else {
421 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
422 "(exponent: %d)\n", exponent);
423 }
424 }
425
426 void print_ampdu_spacing(__u8 spacing)
427 {
428 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
429 print_ampdu_space(spacing), spacing);
430 }
431
432 void print_ht_capability(__u16 cap)
433 {
434 #define PRINT_HT_CAP(_cond, _str) \
435 do { \
436 if (_cond) \
437 printf("\t\t\t" _str "\n"); \
438 } while (0)
439
440 printf("\t\tCapabilities: 0x%02x\n", cap);
441
442 PRINT_HT_CAP((cap & BIT(0)), "RX LDPC");
443 PRINT_HT_CAP((cap & BIT(1)), "HT20/HT40");
444 PRINT_HT_CAP(!(cap & BIT(1)), "HT20");
445
446 PRINT_HT_CAP(((cap >> 2) & 0x3) == 0, "Static SM Power Save");
447 PRINT_HT_CAP(((cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
448 PRINT_HT_CAP(((cap >> 2) & 0x3) == 3, "SM Power Save disabled");
449
450 PRINT_HT_CAP((cap & BIT(4)), "RX Greenfield");
451 PRINT_HT_CAP((cap & BIT(5)), "RX HT20 SGI");
452 PRINT_HT_CAP((cap & BIT(6)), "RX HT40 SGI");
453 PRINT_HT_CAP((cap & BIT(7)), "TX STBC");
454
455 PRINT_HT_CAP(((cap >> 8) & 0x3) == 0, "No RX STBC");
456 PRINT_HT_CAP(((cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
457 PRINT_HT_CAP(((cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
458 PRINT_HT_CAP(((cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
459
460 PRINT_HT_CAP((cap & BIT(10)), "HT Delayed Block Ack");
461
462 PRINT_HT_CAP(!(cap & BIT(11)), "Max AMSDU length: 3839 bytes");
463 PRINT_HT_CAP((cap & BIT(11)), "Max AMSDU length: 7935 bytes");
464
465 /*
466 * For beacons and probe response this would mean the BSS
467 * does or does not allow the usage of DSSS/CCK HT40.
468 * Otherwise it means the STA does or does not use
469 * DSSS/CCK HT40.
470 */
471 PRINT_HT_CAP((cap & BIT(12)), "DSSS/CCK HT40");
472 PRINT_HT_CAP(!(cap & BIT(12)), "No DSSS/CCK HT40");
473
474 /* BIT(13) is reserved */
475
476 PRINT_HT_CAP((cap & BIT(14)), "40 MHz Intolerant");
477
478 PRINT_HT_CAP((cap & BIT(15)), "L-SIG TXOP protection");
479 #undef PRINT_HT_CAP
480 }
481
482 void print_ht_mcs(const __u8 *mcs)
483 {
484 /* As defined in 7.3.2.57.4 Supported MCS Set field */
485 unsigned int tx_max_num_spatial_streams, max_rx_supp_data_rate;
486 bool tx_mcs_set_defined, tx_mcs_set_equal, tx_unequal_modulation;
487
488 max_rx_supp_data_rate = ((mcs[10] >> 8) & ((mcs[11] & 0x3) << 8));
489 tx_mcs_set_defined = !!(mcs[12] & (1 << 0));
490 tx_mcs_set_equal = !(mcs[12] & (1 << 1));
491 tx_max_num_spatial_streams = ((mcs[12] >> 2) & 3) + 1;
492 tx_unequal_modulation = !!(mcs[12] & (1 << 4));
493
494 if (max_rx_supp_data_rate)
495 printf("\t\tHT Max RX data rate: %d Mbps\n", max_rx_supp_data_rate);
496 /* XXX: else see 9.6.0e.5.3 how to get this I think */
497
498 if (tx_mcs_set_defined) {
499 if (tx_mcs_set_equal) {
500 printf("\t\tHT TX/RX MCS rate indexes supported:");
501 print_mcs_index(mcs);
502 } else {
503 printf("\t\tHT RX MCS rate indexes supported:");
504 print_mcs_index(mcs);
505
506 if (tx_unequal_modulation)
507 printf("\t\tTX unequal modulation supported\n");
508 else
509 printf("\t\tTX unequal modulation not supported\n");
510
511 printf("\t\tHT TX Max spatial streams: %d\n",
512 tx_max_num_spatial_streams);
513
514 printf("\t\tHT TX MCS rate indexes supported may differ\n");
515 }
516 } else {
517 printf("\t\tHT RX MCS rate indexes supported:");
518 print_mcs_index(mcs);
519 printf("\t\tHT TX MCS rate indexes are undefined\n");
520 }
521 }