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