utils: add helper functions to get names by values
[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 static int iwinfo_bit(int value, int max)
81 {
82 int i;
83
84 if (max > 31 || !(value & ((1 << max) - 1)))
85 return -1;
86
87 for (i = 0; i < max; i++)
88 {
89 if (value & 1)
90 break;
91
92 value >>= 1;
93 }
94
95 return i;
96 }
97
98 static const char * const iwinfo_name(int mask, int max, const char * const names[])
99 {
100 int index = iwinfo_bit(mask, max);
101
102 if (index < 0)
103 return NULL;
104
105 return names[index];
106 }
107
108 const char * const iwinfo_band_name(int mask)
109 {
110 return iwinfo_name(mask, IWINFO_BAND_COUNT, IWINFO_BAND_NAMES);
111 }
112
113 const char * const iwinfo_htmode_name(int mask)
114 {
115 return iwinfo_name(mask, IWINFO_HTMODE_COUNT, IWINFO_HTMODE_NAMES);
116 }
117
118 size_t iwinfo_format_hwmodes(int modes, char *buf, size_t len)
119 {
120 // bit numbers as per IWINFO_80211_*: ad ac ax a b g n
121 const int order[IWINFO_80211_COUNT] = { 5, 4, 6, 0, 1, 2, 3 };
122 size_t res = 0;
123 int i;
124
125 *buf = 0;
126
127 if (!(modes & ((1 << IWINFO_80211_COUNT) - 1)))
128 return 0;
129
130 for (i = 0; i < IWINFO_80211_COUNT; i++)
131 if (modes & 1 << order[i])
132 res += snprintf(buf + res, len - res, "%s/", IWINFO_80211_NAMES[order[i]]);
133
134 if (res > 0)
135 {
136 res--;
137 buf[res] = 0;
138 }
139
140 return res;
141 }
142
143 int iwinfo_htmode_is_ht(int htmode)
144 {
145 switch (htmode)
146 {
147 case IWINFO_HTMODE_HT20:
148 case IWINFO_HTMODE_HT40:
149 return 1;
150 }
151
152 return 0;
153 }
154
155 int iwinfo_htmode_is_vht(int htmode)
156 {
157 switch (htmode)
158 {
159 case IWINFO_HTMODE_VHT20:
160 case IWINFO_HTMODE_VHT40:
161 case IWINFO_HTMODE_VHT80:
162 case IWINFO_HTMODE_VHT80_80:
163 case IWINFO_HTMODE_VHT160:
164 return 1;
165 }
166
167 return 0;
168 }
169
170 int iwinfo_htmode_is_he(int htmode)
171 {
172 switch (htmode)
173 {
174 case IWINFO_HTMODE_HE20:
175 case IWINFO_HTMODE_HE40:
176 case IWINFO_HTMODE_HE80:
177 case IWINFO_HTMODE_HE80_80:
178 case IWINFO_HTMODE_HE160:
179 return 1;
180 }
181
182 return 0;
183 }
184
185 int iwinfo_ifup(const char *ifname)
186 {
187 struct ifreq ifr;
188
189 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
190
191 if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
192 return 0;
193
194 ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
195
196 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
197 }
198
199 int iwinfo_ifdown(const char *ifname)
200 {
201 struct ifreq ifr;
202
203 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
204
205 if (iwinfo_ioctl(SIOCGIFFLAGS, &ifr))
206 return 0;
207
208 ifr.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
209
210 return !iwinfo_ioctl(SIOCSIFFLAGS, &ifr);
211 }
212
213 int iwinfo_ifmac(const char *ifname)
214 {
215 struct ifreq ifr;
216
217 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
218
219 if (iwinfo_ioctl(SIOCGIFHWADDR, &ifr))
220 return 0;
221
222 ifr.ifr_hwaddr.sa_data[0] |= 0x02;
223 ifr.ifr_hwaddr.sa_data[1]++;
224 ifr.ifr_hwaddr.sa_data[2]++;
225
226 return !iwinfo_ioctl(SIOCSIFHWADDR, &ifr);
227 }
228
229 void iwinfo_close(void)
230 {
231 if (ioctl_socket > -1)
232 close(ioctl_socket);
233
234 ioctl_socket = -1;
235 }
236
237 struct iwinfo_hardware_entry * iwinfo_hardware(struct iwinfo_hardware_id *id)
238 {
239 FILE *db;
240 char buf[256] = { 0 };
241 static struct iwinfo_hardware_entry e;
242 struct iwinfo_hardware_entry *rv = NULL;
243
244 if (!(db = fopen(IWINFO_HARDWARE_FILE, "r")))
245 return NULL;
246
247 while (fgets(buf, sizeof(buf) - 1, db) != NULL)
248 {
249 memset(&e, 0, sizeof(e));
250
251 if (sscanf(buf, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
252 &e.vendor_id, &e.device_id,
253 &e.subsystem_vendor_id, &e.subsystem_device_id,
254 &e.txpower_offset, &e.frequency_offset,
255 e.vendor_name, e.device_name) < 8)
256 continue;
257
258 if ((e.vendor_id != 0xffff) && (e.vendor_id != id->vendor_id))
259 continue;
260
261 if ((e.device_id != 0xffff) && (e.device_id != id->device_id))
262 continue;
263
264 if ((e.subsystem_vendor_id != 0xffff) &&
265 (e.subsystem_vendor_id != id->subsystem_vendor_id))
266 continue;
267
268 if ((e.subsystem_device_id != 0xffff) &&
269 (e.subsystem_device_id != id->subsystem_device_id))
270 continue;
271
272 rv = &e;
273 break;
274 }
275
276 fclose(db);
277 return rv;
278 }
279
280 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
281 {
282 FILE *mtd;
283 uint16_t *bc;
284
285 int fd, off;
286 unsigned int len;
287 char buf[128];
288
289 if (!(mtd = fopen("/proc/mtd", "r")))
290 return -1;
291
292 while (fgets(buf, sizeof(buf), mtd) != NULL)
293 {
294 if (fscanf(mtd, "mtd%d: %x %*x %127s", &off, &len, buf) < 3 ||
295 (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"") &&
296 strcmp(buf, "\"factory\"")))
297 {
298 off = -1;
299 continue;
300 }
301
302 break;
303 }
304
305 fclose(mtd);
306
307 if (off < 0)
308 return -1;
309
310 snprintf(buf, sizeof(buf), "/dev/mtdblock%d", off);
311
312 if ((fd = open(buf, O_RDONLY)) < 0)
313 return -1;
314
315 bc = mmap(NULL, len, PROT_READ, MAP_PRIVATE|MAP_LOCKED, fd, 0);
316
317 if ((void *)bc != MAP_FAILED)
318 {
319 id->vendor_id = 0;
320 id->device_id = 0;
321
322 for (off = len / 2 - 0x800; off >= 0; off -= 0x800)
323 {
324 /* AR531X board data magic */
325 if ((bc[off] == 0x3533) && (bc[off + 1] == 0x3131))
326 {
327 id->vendor_id = bc[off + 0x7d];
328 id->device_id = bc[off + 0x7c];
329 id->subsystem_vendor_id = bc[off + 0x84];
330 id->subsystem_device_id = bc[off + 0x83];
331 break;
332 }
333
334 /* AR5416 EEPROM magic */
335 else if ((bc[off] == 0xA55A) || (bc[off] == 0x5AA5))
336 {
337 id->vendor_id = bc[off + 0x0D];
338 id->device_id = bc[off + 0x0E];
339 id->subsystem_vendor_id = bc[off + 0x13];
340 id->subsystem_device_id = bc[off + 0x14];
341 break;
342 }
343
344 /* Rt3xxx SoC */
345 else if ((bc[off] == 0x3050) || (bc[off] == 0x5030) ||
346 (bc[off] == 0x3051) || (bc[off] == 0x5130) ||
347 (bc[off] == 0x3052) || (bc[off] == 0x5230) ||
348 (bc[off] == 0x3350) || (bc[off] == 0x5033) ||
349 (bc[off] == 0x3352) || (bc[off] == 0x5233) ||
350 (bc[off] == 0x3662) || (bc[off] == 0x6236) ||
351 (bc[off] == 0x3883) || (bc[off] == 0x8338) ||
352 (bc[off] == 0x5350) || (bc[off] == 0x5053))
353 {
354 /* vendor: RaLink */
355 id->vendor_id = 0x1814;
356 id->subsystem_vendor_id = 0x1814;
357
358 /* device */
359 if (((bc[off] & 0xf0) == 0x30) ||
360 ((bc[off] & 0xff) == 0x53))
361 id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
362 else
363 id->device_id = bc[off];
364
365 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
366 id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
367 } else if ((bc[off] == 0x7620) || (bc[off] == 0x2076) ||
368 (bc[off] == 0x7628) || (bc[off] == 0x2876) ||
369 (bc[off] == 0x7688) || (bc[off] == 0x8876)) {
370 /* vendor: MediaTek */
371 id->vendor_id = 0x14c3;
372 id->subsystem_vendor_id = 0x14c3;
373
374 /* device */
375 if ((bc[off] & 0xff) == 0x76)
376 id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
377 else
378 id->device_id = bc[off];
379
380 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
381 id->subsystem_device_id = (bc[off + 0x1a] & 0x0f00) >> 8;
382 }
383 }
384
385 munmap(bc, len);
386 }
387
388 close(fd);
389
390 return (id->vendor_id && id->device_id) ? 0 : -1;
391 }
392
393 static void iwinfo_parse_rsn_cipher(uint8_t idx, uint16_t *ciphers)
394 {
395 switch (idx)
396 {
397 case 0:
398 *ciphers |= IWINFO_CIPHER_NONE;
399 break;
400
401 case 1:
402 *ciphers |= IWINFO_CIPHER_WEP40;
403 break;
404
405 case 2:
406 *ciphers |= IWINFO_CIPHER_TKIP;
407 break;
408
409 case 3: /* WRAP */
410 break;
411
412 case 4:
413 *ciphers |= IWINFO_CIPHER_CCMP;
414 break;
415
416 case 5:
417 *ciphers |= IWINFO_CIPHER_WEP104;
418 break;
419
420 case 8:
421 *ciphers |= IWINFO_CIPHER_GCMP;
422 break;
423
424 case 9:
425 *ciphers |= IWINFO_CIPHER_GCMP256;
426 break;
427
428 case 10:
429 *ciphers |= IWINFO_CIPHER_CCMP256;
430 break;
431
432 case 6: /* AES-128-CMAC */
433 case 7: /* No group addressed */
434 case 11: /* BIP-GMAC-128 */
435 case 12: /* BIP-GMAC-256 */
436 case 13: /* BIP-CMAC-256 */
437 break;
438 }
439 }
440
441 void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len,
442 uint16_t defcipher, uint8_t defauth)
443 {
444 uint16_t i, count;
445 uint8_t wpa_version = 0;
446
447 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
448 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
449
450 data += 2;
451 len -= 2;
452
453 if (!memcmp(data, ms_oui, 3))
454 wpa_version |= 1;
455 else if (!memcmp(data, ieee80211_oui, 3))
456 wpa_version |= 2;
457
458 if (len < 4)
459 {
460 c->group_ciphers |= defcipher;
461 c->pair_ciphers |= defcipher;
462 c->auth_suites |= defauth;
463 return;
464 }
465
466 if (!memcmp(data, ms_oui, 3) || !memcmp(data, ieee80211_oui, 3))
467 iwinfo_parse_rsn_cipher(data[3], &c->group_ciphers);
468
469 data += 4;
470 len -= 4;
471
472 if (len < 2)
473 {
474 c->pair_ciphers |= defcipher;
475 c->auth_suites |= defauth;
476 return;
477 }
478
479 count = data[0] | (data[1] << 8);
480 if (2 + (count * 4) > len)
481 return;
482
483 for (i = 0; i < count; i++)
484 if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
485 !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
486 iwinfo_parse_rsn_cipher(data[2 + (i * 4) + 3], &c->pair_ciphers);
487
488 data += 2 + (count * 4);
489 len -= 2 + (count * 4);
490
491 if (len < 2)
492 {
493 c->auth_suites |= defauth;
494 return;
495 }
496
497 count = data[0] | (data[1] << 8);
498 if (2 + (count * 4) > len)
499 return;
500
501 for (i = 0; i < count; i++)
502 {
503 if (!memcmp(data + 2 + (i * 4), ms_oui, 3) ||
504 !memcmp(data + 2 + (i * 4), ieee80211_oui, 3))
505 {
506 switch (data[2 + (i * 4) + 3])
507 {
508 case 1: /* IEEE 802.1x */
509 c->wpa_version |= wpa_version;
510 c->auth_suites |= IWINFO_KMGMT_8021x;
511 break;
512
513 case 2: /* PSK */
514 c->wpa_version |= wpa_version;
515 c->auth_suites |= IWINFO_KMGMT_PSK;
516 break;
517
518 case 3: /* FT/IEEE 802.1X */
519 case 4: /* FT/PSK */
520 case 5: /* IEEE 802.1X/SHA-256 */
521 case 6: /* PSK/SHA-256 */
522 case 7: /* TPK Handshake */
523 break;
524
525 case 8: /* SAE */
526 c->wpa_version |= 4;
527 c->auth_suites |= IWINFO_KMGMT_SAE;
528 break;
529
530 case 9: /* FT/SAE */
531 case 10: /* undefined */
532 break;
533
534 case 11: /* 802.1x Suite-B */
535 case 12: /* 802.1x Suite-B-192 */
536 case 13: /* FT/802.1x SHA-384 */
537 c->wpa_version |= 4;
538 c->auth_suites |= IWINFO_KMGMT_8021x;
539 break;
540
541 case 14: /* FILS SHA-256 */
542 case 15: /* FILS SHA-384 */
543 case 16: /* FT/FILS SHA-256 */
544 case 17: /* FT/FILS SHA-384 */
545 break;
546
547 case 18: /* OWE */
548 c->wpa_version |= 4;
549 c->auth_suites |= IWINFO_KMGMT_OWE;
550 break;
551 }
552 }
553 }
554
555 data += 2 + (count * 4);
556 len -= 2 + (count * 4);
557 }
558
559 struct uci_section *iwinfo_uci_get_radio(const char *name, const char *type)
560 {
561 struct uci_ptr ptr = {
562 .package = "wireless",
563 .section = name,
564 .flags = (name && *name == '@') ? UCI_LOOKUP_EXTENDED : 0,
565 };
566 const char *opt;
567
568 if (!uci_ctx) {
569 uci_ctx = uci_alloc_context();
570 if (!uci_ctx)
571 return NULL;
572 }
573
574 if (uci_lookup_ptr(uci_ctx, &ptr, NULL, true))
575 return NULL;
576
577 if (!ptr.s || strcmp(ptr.s->type, "wifi-device") != 0)
578 return NULL;
579
580 opt = uci_lookup_option_string(uci_ctx, ptr.s, "type");
581 if (!opt || strcmp(opt, type) != 0)
582 return NULL;
583
584 return ptr.s;
585 }
586
587 void iwinfo_uci_free(void)
588 {
589 if (!uci_ctx)
590 return;
591
592 uci_free_context(uci_ctx);
593 uci_ctx = NULL;
594 }
595
596
597 struct iwinfo_ubus_query_state {
598 const char *ifname;
599 const char *field;
600 size_t len;
601 char *buf;
602 };
603
604 static void iwinfo_ubus_query_cb(struct ubus_request *req, int type,
605 struct blob_attr *msg)
606 {
607 struct iwinfo_ubus_query_state *st = req->priv;
608
609 struct blobmsg_policy pol1[2] = {
610 { "ifname", BLOBMSG_TYPE_STRING },
611 { "config", BLOBMSG_TYPE_TABLE }
612 };
613
614 struct blobmsg_policy pol2 = { st->field, BLOBMSG_TYPE_STRING };
615 struct blob_attr *cur, *cur2, *cur3, *cfg[2], *res;
616 int rem, rem2, rem3;
617
618 blobmsg_for_each_attr(cur, msg, rem) {
619 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE)
620 continue;
621
622 blobmsg_for_each_attr(cur2, cur, rem2) {
623 if (blobmsg_type(cur2) != BLOBMSG_TYPE_ARRAY)
624 continue;
625
626 if (strcmp(blobmsg_name(cur2), "interfaces"))
627 continue;
628
629 blobmsg_for_each_attr(cur3, cur2, rem3) {
630 blobmsg_parse(pol1, sizeof(pol1) / sizeof(pol1[0]), cfg,
631 blobmsg_data(cur3), blobmsg_len(cur3));
632
633 if (!cfg[0] || !cfg[1] ||
634 strcmp(blobmsg_get_string(cfg[0]), st->ifname))
635 continue;
636
637 blobmsg_parse(&pol2, 1, &res,
638 blobmsg_data(cfg[1]), blobmsg_len(cfg[1]));
639
640 if (!res)
641 continue;
642
643 strncpy(st->buf, blobmsg_get_string(res), st->len);
644 return;
645 }
646 }
647 }
648 }
649
650 int iwinfo_ubus_query(const char *ifname, const char *field,
651 char *buf, size_t len)
652 {
653 struct iwinfo_ubus_query_state st = {
654 .ifname = ifname,
655 .field = field,
656 .buf = buf,
657 .len = len
658 };
659
660 struct ubus_context *ctx = NULL;
661 struct blob_buf b = { };
662 int rv = -1;
663 uint32_t id;
664
665 blob_buf_init(&b, 0);
666
667 ctx = ubus_connect(NULL);
668
669 if (!ctx)
670 goto out;
671
672 if (ubus_lookup_id(ctx, "network.wireless", &id))
673 goto out;
674
675 if (ubus_invoke(ctx, id, "status", b.head, iwinfo_ubus_query_cb, &st, 250))
676 goto out;
677
678 rv = 0;
679
680 out:
681 if (ctx)
682 ubus_free(ctx);
683
684 blob_buf_free(&b);
685
686 return rv;
687 }