utils: add and use iwinfo_format_hwmodes()
[project/iwinfo.git] / iwinfo_utils.c
1 /*
2 * iwinfo - Wireless Information Library - Shared utility routines
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * The iwinfo library is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * The iwinfo library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17 *
18 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
21 */
22
23 #include "iwinfo/utils.h"
24
25
26 static int ioctl_socket = -1;
27 struct uci_context *uci_ctx = NULL;
28
29 static int iwinfo_ioctl_socket(void)
30 {
31 /* Prepare socket */
32 if (ioctl_socket == -1)
33 {
34 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
35 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
36 }
37
38 return ioctl_socket;
39 }
40
41 int iwinfo_ioctl(int cmd, void *ifr)
42 {
43 int s = iwinfo_ioctl_socket();
44 return ioctl(s, cmd, ifr);
45 }
46
47 int iwinfo_dbm2mw(int in)
48 {
49 double res = 1.0;
50 int ip = in / 10;
51 int fp = in % 10;
52 int k;
53
54 for(k = 0; k < ip; k++) res *= 10;
55 for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
56
57 return (int)res;
58 }
59
60 int iwinfo_mw2dbm(int in)
61 {
62 double fin = (double) in;
63 int res = 0;
64
65 while(fin > 10.0)
66 {
67 res += 10;
68 fin /= 10.0;
69 }
70
71 while(fin > 1.000001)
72 {
73 res += 1;
74 fin /= LOG10_MAGIC;
75 }
76
77 return (int)res;
78 }
79
80 size_t iwinfo_format_hwmodes(int modes, char *buf, size_t len)
81 {
82 // bit numbers as per IWINFO_80211_*: ad ac ax a b g n
83 const int order[IWINFO_80211_COUNT] = { 5, 4, 6, 0, 1, 2, 3 };
84 size_t res = 0;
85 int i;
86
87 *buf = 0;
88
89 if (!(modes & ((1 << IWINFO_80211_COUNT) - 1)))
90 return 0;
91
92 for (i = 0; i < IWINFO_80211_COUNT; i++)
93 if (modes & 1 << order[i])
94 res += snprintf(buf + res, len - res, "%s/", IWINFO_80211_NAMES[order[i]]);
95
96 if (res > 0)
97 {
98 res--;
99 buf[res] = 0;
100 }
101
102 return res;
103 }
104
105 int iwinfo_ifup(const char *ifname)
106 {
107 struct ifreq ifr;
108
109 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
110
111 if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
112 return 0;
113
114 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
115
116 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
117 }
118
119 int iwinfo_ifdown(const char *ifname)
120 {
121 struct ifreq ifr;
122
123 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
124
125 if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
126 return 0;
127
128 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
129
130 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
131 }
132
133 int iwinfo_ifmac(const char *ifname)
134 {
135 struct ifreq ifr;
136
137 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
138
139 if (iwinfo_ioctl(SIOCGIFHWADDR, &ifr))
140 return 0;
141
142 ifr.ifr_hwaddr.sa_data[0] |= 0x02;
143 ifr.ifr_hwaddr.sa_data[1]++;
144 ifr.ifr_hwaddr.sa_data[2]++;
145
146 return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
147 }
148
149 void iwinfo_close(void)
150 {
151 if (ioctl_socket > -1)
152 close(ioctl_socket);
153
154 ioctl_socket = -1;
155 }
156
157 struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
158 {
159 FILE *db;
160 char buf[256] = { 0 };
161 static struct iwinfo_hardware_entry e;
162 struct iwinfo_hardware_entry *rv = NULL;
163
164 if (!(db = fopen(IWINFO_HARDWARE_FILE, "r")))
165 return NULL;
166
167 while (fgets(buf, sizeof(buf) - 1, db) != NULL)
168 {
169 memset(&e, 0, sizeof(e));
170
171 if (sscanf(buf, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
172 &e.vendor_id, &e.device_id,
173 &e.subsystem_vendor_id, &e.subsystem_device_id,
174 &e.txpower_offset, &e.frequency_offset,
175 e.vendor_name, e.device_name) < 8)
176 continue;
177
178 if ((e.vendor_id != 0xffff) && (e.vendor_id != id->vendor_id))
179 continue;
180
181 if ((e.device_id != 0xffff) && (e.device_id != id->device_id))
182 continue;
183
184 if ((e.subsystem_vendor_id != 0xffff) &&
185 (e.subsystem_vendor_id != id->subsystem_vendor_id))
186 continue;
187
188 if ((e.subsystem_device_id != 0xffff) &&
189 (e.subsystem_device_id != id->subsystem_device_id))
190 continue;
191
192 rv = &e;
193 break;
194 }
195
196 fclose(db);
197 return rv;
198 }
199
200 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
201 {
202 FILE *mtd;
203 uint16_t *bc;
204
205 int fd, off;
206 unsigned int len;
207 char buf[128];
208
209 if (!(mtd = fopen("/proc/mtd", "r")))
210 return -1;
211
212 while (fgets(buf, sizeof(buf), mtd) != NULL)
213 {
214 if (fscanf(mtd, "mtd%d: %x %*x %127s", &off, &len, buf) < 3 ||
215 (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"") &&
216 strcmp(buf, "\"factory\"")))
217 {
218 off = -1;
219 continue;
220 }
221
222 break;
223 }
224
225 fclose(mtd);
226
227 if (off < 0)
228 return -1;
229
230 snprintf(buf, sizeof(buf), "/dev/mtdblock%d", off);
231
232 if ((fd = open(buf, O_RDONLY)) < 0)
233 return -1;
234
235 bc = mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_LOCKED, fd, 0);
236
237 if ((void *)bc != MAP_FAILED)
238 {
239 id->vendor_id = 0;
240 id->device_id = 0;
241
242 for (off = len / 2 - 0x800; off >= 0; off -= 0x800)
243 {
244 /* AR531X board data magic */
245 if ((bc[off] == 0x3533) && (bc[off + 1] == 0x3131))
246 {
247 id->vendor_id = bc[off + 0x7d];
248 id->device_id = bc[off + 0x7c];
249 id->subsystem_vendor_id = bc[off + 0x84];
250 id->subsystem_device_id = bc[off + 0x83];
251 break;
252 }
253
254 /* AR5416 EEPROM magic */
255 else if ((bc[off] == 0xA55A) || (bc[off] == 0x5AA5))
256 {
257 id->vendor_id = bc[off + 0x0D];
258 id->device_id = bc[off + 0x0E];
259 id->subsystem_vendor_id = bc[off + 0x13];
260 id->subsystem_device_id = bc[off + 0x14];
261 break;
262 }
263
264 /* Rt3xxx SoC */
265 else if ((bc[off] == 0x3050) || (bc[off] == 0x5030) ||
266 (bc[off] == 0x3051) || (bc[off] == 0x5130) ||
267 (bc[off] == 0x3052) || (bc[off] == 0x5230) ||
268 (bc[off] == 0x3350) || (bc[off] == 0x5033) ||
269 (bc[off] == 0x3352) || (bc[off] == 0x5233) ||
270 (bc[off] == 0x3662) || (bc[off] == 0x6236) ||
271 (bc[off] == 0x3883) || (bc[off] == 0x8338) ||
272 (bc[off] == 0x5350) || (bc[off] == 0x5053))
273 {
274 /* vendor: RaLink */
275 id->vendor_id = 0x1814;
276 id->subsystem_vendor_id = 0x1814;
277
278 /* device */
279 if (((bc[off] & 0xf0) == 0x30) ||
280 ((bc[off] & 0xff) == 0x53))
281 id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
282 else
283 id->device_id = bc[off];
284
285 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
286 id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
287 } else if ((bc[off] == 0x7620) || (bc[off] == 0x2076) ||
288 (bc[off] == 0x7628) || (bc[off] == 0x2876) ||
289 (bc[off] == 0x7688) || (bc[off] == 0x8876)) {
290 /* vendor: MediaTek */
291 id->vendor_id = 0x14c3;
292 id->subsystem_vendor_id = 0x14c3;
293
294 /* device */
295 if ((bc[off] & 0xff) == 0x76)
296 id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
297 else
298 id->device_id = bc[off];
299
300 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
301 id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
302 }
303 }
304
305 munmap(bc, len);
306 }
307
308 close(fd);
309
310 return (id->vendor_id && id->device_id) ? 0 : -1;
311 }
312
313 static void iwinfo_parse_rsn_cipher(uint8_t idx, uint16_t *ciphers)
314 {
315 switch (idx)
316 {
317 case 0:
318 *ciphers |= IWINFO_CIPHER_NONE;
319 break;
320
321 case 1:
322 *ciphers |= IWINFO_CIPHER_WEP40;
323 break;
324
325 case 2:
326 *ciphers |= IWINFO_CIPHER_TKIP;
327 break;
328
329 case 3: /* WRAP */
330 break;
331
332 case 4:
333 *ciphers |= IWINFO_CIPHER_CCMP;
334 break;
335
336 case 5:
337 *ciphers |= IWINFO_CIPHER_WEP104;
338 break;
339
340 case 8:
341 *ciphers |= IWINFO_CIPHER_GCMP;
342 break;
343
344 case 9:
345 *ciphers |= IWINFO_CIPHER_GCMP256;
346 break;
347
348 case 10:
349 *ciphers |= IWINFO_CIPHER_CCMP256;
350 break;
351
352 case 6: /* AES-128-CMAC */
353 case 7: /* No group addressed */
354 case 11: /* BIP-GMAC-128 */
355 case 12: /* BIP-GMAC-256 */
356 case 13: /* BIP-CMAC-256 */
357 break;
358 }
359 }
360
361 void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len,
362 uint16_t defcipher, uint8_t defauth)
363 {
364 uint16_t i, count;
365 uint8_t wpa_version = 0;
366
367 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
368 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
369
370 data += 2;
371 len -= 2;
372
373 if (!memcmp(data, ms_oui, 3))
374 wpa_version |= 1;
375 else if (!memcmp(data, ieee80211_oui, 3))
376 wpa_version |= 2;
377
378 if (len < 4)
379 {
380 c->group_ciphers |= defcipher;
381 c->pair_ciphers |= defcipher;
382 c->auth_suites |= defauth;
383 return;
384 }
385
386 if (!memcmp(data, ms_oui, 3) || !memcmp(data, ieee80211_oui, 3))
387 iwinfo_parse_rsn_cipher(data[3], &c->group_ciphers);
388
389 data += 4;
390 len -= 4;
391
392 if (len < 2)
393 {
394 c->pair_ciphers |= defcipher;
395 c->auth_suites |= defauth;
396 return;
397 }
398
399 count = data[0] | (data[1] << 8);
400 if (2 + (count * 4) > len)
401 return;
402
403 for (i = 0; i < count; i++)
404 if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
405 !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
406 iwinfo_parse_rsn_cipher(data[2 + (i * 4) + 3], &c->pair_ciphers);
407
408 data += 2 + (count * 4);
409 len -= 2 + (count * 4);
410
411 if (len < 2)
412 {
413 c->auth_suites |= defauth;
414 return;
415 }
416
417 count = data[0] | (data[1] << 8);
418 if (2 + (count * 4) > len)
419 return;
420
421 for (i = 0; i < count; i++)
422 {
423 if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
424 !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
425 {
426 switch (data[2 + (i * 4) + 3])
427 {
428 case 1: /* IEEE 802.1x */
429 c->wpa_version |= wpa_version;
430 c->auth_suites |= IWINFO_KMGMT_8021x;
431 break;
432
433 case 2: /* PSK */
434 c->wpa_version |= wpa_version;
435 c->auth_suites |= IWINFO_KMGMT_PSK;
436 break;
437
438 case 3: /* FT/IEEE 802.1X */
439 case 4: /* FT/PSK */
440 case 5: /* IEEE 802.1X/SHA-256 */
441 case 6: /* PSK/SHA-256 */
442 case 7: /* TPK Handshake */
443 break;
444
445 case 8: /* SAE */
446 c->wpa_version |= 4;
447 c->auth_suites |= IWINFO_KMGMT_SAE;
448 break;
449
450 case 9: /* FT/SAE */
451 case 10: /* undefined */
452 break;
453
454 case 11: /* 802.1x Suite-B */
455 case 12: /* 802.1x Suite-B-192 */
456 case 13: /* FT/802.1x SHA-384 */
457 c->wpa_version |= 4;
458 c->auth_suites |= IWINFO_KMGMT_8021x;
459 break;
460
461 case 14: /* FILS SHA-256 */
462 case 15: /* FILS SHA-384 */
463 case 16: /* FT/FILS SHA-256 */
464 case 17: /* FT/FILS SHA-384 */
465 break;
466
467 case 18: /* OWE */
468 c->wpa_version |= 4;
469 c->auth_suites |= IWINFO_KMGMT_OWE;
470 break;
471 }
472 }
473 }
474
475 data += 2 + (count * 4);
476 len -= 2 + (count * 4);
477 }
478
479 struct uci_section *iwinfo_uci_get_radio(const char *name, const char *type)
480 {
481 struct uci_ptr ptr = {
482 .package = "wireless",
483 .section = name,
484 .flags = (name && *name == '@') ? UCI_LOOKUP_EXTENDED : 0,
485 };
486 const char *opt;
487
488 if (!uci_ctx) {
489 uci_ctx = uci_alloc_context();
490 if (!uci_ctx)
491 return NULL;
492 }
493
494 if (uci_lookup_ptr(uci_ctx, &ptr, NULL, true))
495 return NULL;
496
497 if (!ptr.s || strcmp(ptr.s->type, "wifi-device") != 0)
498 return NULL;
499
500 opt = uci_lookup_option_string(uci_ctx, ptr.s, "type");
501 if (!opt || strcmp(opt, type) != 0)
502 return NULL;
503
504 return ptr.s;
505 }
506
507 void iwinfo_uci_free(void)
508 {
509 if (!uci_ctx)
510 return;
511
512 uci_free_context(uci_ctx);
513 uci_ctx = NULL;
514 }
515
516
517 struct iwinfo_ubus_query_state {
518 const char *ifname;
519 const char *field;
520 size_t len;
521 char *buf;
522 };
523
524 static void iwinfo_ubus_query_cb(struct ubus_request *req, int type,
525 struct blob_attr *msg)
526 {
527 struct iwinfo_ubus_query_state *st = req->priv;
528
529 struct blobmsg_policy pol1[2] = {
530 { "ifname", BLOBMSG_TYPE_STRING },
531 { "config", BLOBMSG_TYPE_TABLE }
532 };
533
534 struct blobmsg_policy pol2 = { st->field, BLOBMSG_TYPE_STRING };
535 struct blob_attr *cur, *cur2, *cur3, *cfg[2], *res;
536 int rem, rem2, rem3;
537
538 blobmsg_for_each_attr(cur, msg, rem) {
539 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
540 continue;
541
542 blobmsg_for_each_attr(cur2, cur, rem2) {
543 if (blobmsg_type(cur2) != BLOBMSG_TYPE_ARRAY)
544 continue;
545
546 if (strcmp(blobmsg_name(cur2), "interfaces"))
547 continue;
548
549 blobmsg_for_each_attr(cur3, cur2, rem3) {
550 blobmsg_parse(pol1, sizeof(pol1) / sizeof(pol1[0]), cfg,
551 blobmsg_data(cur3), blobmsg_len(cur3));
552
553 if (!cfg[0] || !cfg[1] ||
554 strcmp(blobmsg_get_string(cfg[0]), st->ifname))
555 continue;
556
557 blobmsg_parse(&pol2, 1, &res,
558 blobmsg_data(cfg[1]), blobmsg_len(cfg[1]));
559
560 if (!res)
561 continue;
562
563 strncpy(st->buf, blobmsg_get_string(res), st->len);
564 return;
565 }
566 }
567 }
568 }
569
570 int iwinfo_ubus_query(const char *ifname, const char *field,
571 char *buf, size_t len)
572 {
573 struct iwinfo_ubus_query_state st = {
574 .ifname = ifname,
575 .field = field,
576 .buf = buf,
577 .len = len
578 };
579
580 struct ubus_context *ctx = NULL;
581 struct blob_buf b = { };
582 int rv = -1;
583 uint32_t id;
584
585 blob_buf_init(&b, 0);
586
587 ctx = ubus_connect(NULL);
588
589 if (!ctx)
590 goto out;
591
592 if (ubus_lookup_id(ctx, "network.wireless", &id))
593 goto out;
594
595 if (ubus_invoke(ctx, id, "status", b.head, iwinfo_ubus_query_cb, &st, 250))
596 goto out;
597
598 rv = 0;
599
600 out:
601 if (ctx)
602 ubus_free(ctx);
603
604 blob_buf_free(&b);
605
606 return rv;
607 }