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