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