2 * iwinfo - Wireless Information Library - Shared utility routines
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
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.
23 #include "iwinfo/utils.h"
26 static int ioctl_socket
= -1;
27 struct uci_context
*uci_ctx
= NULL
;
29 static int iwinfo_ioctl_socket(void)
32 if (ioctl_socket
== -1)
34 ioctl_socket
= socket(AF_INET
, SOCK_DGRAM
, 0);
35 fcntl(ioctl_socket
, F_SETFD
, fcntl(ioctl_socket
, F_GETFD
) | FD_CLOEXEC
);
41 int iwinfo_ioctl(int cmd
, void *ifr
)
43 int s
= iwinfo_ioctl_socket();
44 return ioctl(s
, cmd
, ifr
);
47 int iwinfo_dbm2mw(int in
)
54 for(k
= 0; k
< ip
; k
++) res
*= 10;
55 for(k
= 0; k
< fp
; k
++) res
*= LOG10_MAGIC
;
60 int iwinfo_mw2dbm(int in
)
62 double fin
= (double) in
;
80 static int iwinfo_bit(int value
, int max
)
84 if (max
> 31 || !(value
& ((1 << max
) - 1)))
87 for (i
= 0; i
< max
; i
++)
98 static const char * const iwinfo_name(int mask
, int max
, const char * const names
[])
100 int index
= iwinfo_bit(mask
, max
);
108 const char * const iwinfo_band_name(int mask
)
110 return iwinfo_name(mask
, IWINFO_BAND_COUNT
, IWINFO_BAND_NAMES
);
113 const char * const iwinfo_htmode_name(int mask
)
115 return iwinfo_name(mask
, IWINFO_HTMODE_COUNT
, IWINFO_HTMODE_NAMES
);
118 uint32_t iwinfo_band2ghz(uint8_t band
)
135 uint8_t iwinfo_ghz2band(uint32_t ghz
)
140 return IWINFO_BAND_24
;
142 return IWINFO_BAND_5
;
144 return IWINFO_BAND_6
;
146 return IWINFO_BAND_60
;
152 size_t iwinfo_format_hwmodes(int modes
, char *buf
, size_t len
)
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 };
161 if (!(modes
& ((1 << IWINFO_80211_COUNT
) - 1)))
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
]]);
177 int iwinfo_htmode_is_ht(int htmode
)
181 case IWINFO_HTMODE_HT20
:
182 case IWINFO_HTMODE_HT40
:
189 int iwinfo_htmode_is_vht(int htmode
)
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
:
204 int iwinfo_htmode_is_he(int htmode
)
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
:
219 int iwinfo_ifup(const char *ifname
)
223 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
225 if (iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
))
228 ifr
.ifr_flags
|= (IFF_UP
| IFF_RUNNING
);
230 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
233 int iwinfo_ifdown(const char *ifname
)
237 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
239 if (iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
))
242 ifr
.ifr_flags
&= ~(IFF_UP
| IFF_RUNNING
);
244 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
247 int iwinfo_ifmac(const char *ifname
)
251 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
253 if (iwinfo_ioctl(SIOCGIFHWADDR
, &ifr
))
256 ifr
.ifr_hwaddr
.sa_data
[0] |= 0x02;
257 ifr
.ifr_hwaddr
.sa_data
[1]++;
258 ifr
.ifr_hwaddr
.sa_data
[2]++;
260 return !iwinfo_ioctl(SIOCSIFHWADDR
, &ifr
);
263 void iwinfo_close(void)
265 if (ioctl_socket
> -1)
271 struct iwinfo_hardware_entry
* iwinfo_hardware(struct iwinfo_hardware_id
*id
)
274 char buf
[256] = { 0 };
275 static struct iwinfo_hardware_entry e
;
276 struct iwinfo_hardware_entry
*rv
= NULL
;
278 if (!(db
= fopen(IWINFO_HARDWARE_FILE
, "r")))
281 while (fgets(buf
, sizeof(buf
) - 1, db
) != NULL
)
283 memset(&e
, 0, sizeof(e
));
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 sscanf(buf
, "\"%127[^\"]\" %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
291 e
.compatible
, &e
.txpower_offset
, &e
.frequency_offset
,
292 e
.vendor_name
, e
.device_name
) != 5)
295 if ((e
.vendor_id
!= 0xffff) && (e
.vendor_id
!= id
->vendor_id
))
298 if ((e
.device_id
!= 0xffff) && (e
.device_id
!= id
->device_id
))
301 if ((e
.subsystem_vendor_id
!= 0xffff) &&
302 (e
.subsystem_vendor_id
!= id
->subsystem_vendor_id
))
305 if ((e
.subsystem_device_id
!= 0xffff) &&
306 (e
.subsystem_device_id
!= id
->subsystem_device_id
))
309 if (strcmp(e
.compatible
, id
->compatible
))
320 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id
*id
)
329 if (!(mtd
= fopen("/proc/mtd", "r")))
332 while (fgets(buf
, sizeof(buf
), mtd
) != NULL
)
334 if (fscanf(mtd
, "mtd%d: %x %*x %127s", &off
, &len
, buf
) < 3 ||
335 (strcmp(buf
, "\"boardconfig\"") && strcmp(buf
, "\"EEPROM\"") &&
336 strcmp(buf
, "\"factory\"")))
350 snprintf(buf
, sizeof(buf
), "/dev/mtdblock%d", off
);
352 if ((fd
= open(buf
, O_RDONLY
)) < 0)
355 bc
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
|MAP_LOCKED
, fd
, 0);
357 if ((void *)bc
!= MAP_FAILED
)
362 for (off
= len
/ 2 - 0x800; off
>= 0; off
-= 0x800)
364 /* AR531X board data magic */
365 if ((bc
[off
] == 0x3533) && (bc
[off
+ 1] == 0x3131))
367 id
->vendor_id
= bc
[off
+ 0x7d];
368 id
->device_id
= bc
[off
+ 0x7c];
369 id
->subsystem_vendor_id
= bc
[off
+ 0x84];
370 id
->subsystem_device_id
= bc
[off
+ 0x83];
374 /* AR5416 EEPROM magic */
375 else if ((bc
[off
] == 0xA55A) || (bc
[off
] == 0x5AA5))
377 id
->vendor_id
= bc
[off
+ 0x0D];
378 id
->device_id
= bc
[off
+ 0x0E];
379 id
->subsystem_vendor_id
= bc
[off
+ 0x13];
380 id
->subsystem_device_id
= bc
[off
+ 0x14];
385 else if ((bc
[off
] == 0x3050) || (bc
[off
] == 0x5030) ||
386 (bc
[off
] == 0x3051) || (bc
[off
] == 0x5130) ||
387 (bc
[off
] == 0x3052) || (bc
[off
] == 0x5230) ||
388 (bc
[off
] == 0x3350) || (bc
[off
] == 0x5033) ||
389 (bc
[off
] == 0x3352) || (bc
[off
] == 0x5233) ||
390 (bc
[off
] == 0x3662) || (bc
[off
] == 0x6236) ||
391 (bc
[off
] == 0x3883) || (bc
[off
] == 0x8338) ||
392 (bc
[off
] == 0x5350) || (bc
[off
] == 0x5053))
395 id
->vendor_id
= 0x1814;
396 id
->subsystem_vendor_id
= 0x1814;
399 if (((bc
[off
] & 0xf0) == 0x30) ||
400 ((bc
[off
] & 0xff) == 0x53))
401 id
->device_id
= (bc
[off
] >> 8) | (bc
[off
] & 0x00ff) << 8;
403 id
->device_id
= bc
[off
];
405 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
406 id
->subsystem_device_id
= (bc
[off
+ 0x1a] & 0x0f00) >> 8;
407 } else if ((bc
[off
] == 0x7620) || (bc
[off
] == 0x2076) ||
408 (bc
[off
] == 0x7628) || (bc
[off
] == 0x2876) ||
409 (bc
[off
] == 0x7688) || (bc
[off
] == 0x8876)) {
410 /* vendor: MediaTek */
411 id
->vendor_id
= 0x14c3;
412 id
->subsystem_vendor_id
= 0x14c3;
415 if ((bc
[off
] & 0xff) == 0x76)
416 id
->device_id
= (bc
[off
] >> 8) | (bc
[off
] & 0x00ff) << 8;
418 id
->device_id
= bc
[off
];
420 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
421 id
->subsystem_device_id
= (bc
[off
+ 0x1a] & 0x0f00) >> 8;
430 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
433 static void iwinfo_parse_rsn_cipher(uint8_t idx
, uint16_t *ciphers
)
438 *ciphers
|= IWINFO_CIPHER_NONE
;
442 *ciphers
|= IWINFO_CIPHER_WEP40
;
446 *ciphers
|= IWINFO_CIPHER_TKIP
;
453 *ciphers
|= IWINFO_CIPHER_CCMP
;
457 *ciphers
|= IWINFO_CIPHER_WEP104
;
461 *ciphers
|= IWINFO_CIPHER_GCMP
;
465 *ciphers
|= IWINFO_CIPHER_GCMP256
;
469 *ciphers
|= IWINFO_CIPHER_CCMP256
;
472 case 6: /* AES-128-CMAC */
473 case 7: /* No group addressed */
474 case 11: /* BIP-GMAC-128 */
475 case 12: /* BIP-GMAC-256 */
476 case 13: /* BIP-CMAC-256 */
481 void iwinfo_parse_rsn(struct iwinfo_crypto_entry
*c
, uint8_t *data
, uint8_t len
,
482 uint16_t defcipher
, uint8_t defauth
)
485 uint8_t wpa_version
= 0;
487 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
488 static unsigned char ieee80211_oui
[3] = { 0x00, 0x0f, 0xac };
493 if (!memcmp(data
, ms_oui
, 3))
495 else if (!memcmp(data
, ieee80211_oui
, 3))
500 c
->group_ciphers
|= defcipher
;
501 c
->pair_ciphers
|= defcipher
;
502 c
->auth_suites
|= defauth
;
506 if (!memcmp(data
, ms_oui
, 3) || !memcmp(data
, ieee80211_oui
, 3))
507 iwinfo_parse_rsn_cipher(data
[3], &c
->group_ciphers
);
514 c
->pair_ciphers
|= defcipher
;
515 c
->auth_suites
|= defauth
;
519 count
= data
[0] | (data
[1] << 8);
520 if (2 + (count
* 4) > len
)
523 for (i
= 0; i
< count
; i
++)
524 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
525 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
526 iwinfo_parse_rsn_cipher(data
[2 + (i
* 4) + 3], &c
->pair_ciphers
);
528 data
+= 2 + (count
* 4);
529 len
-= 2 + (count
* 4);
533 c
->auth_suites
|= defauth
;
537 count
= data
[0] | (data
[1] << 8);
538 if (2 + (count
* 4) > len
)
541 for (i
= 0; i
< count
; i
++)
543 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
544 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
546 switch (data
[2 + (i
* 4) + 3])
548 case 1: /* IEEE 802.1x */
549 c
->wpa_version
|= wpa_version
;
550 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
554 c
->wpa_version
|= wpa_version
;
555 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
558 case 3: /* FT/IEEE 802.1X */
560 case 5: /* IEEE 802.1X/SHA-256 */
561 case 6: /* PSK/SHA-256 */
562 case 7: /* TPK Handshake */
567 c
->auth_suites
|= IWINFO_KMGMT_SAE
;
571 case 10: /* undefined */
574 case 11: /* 802.1x Suite-B */
575 case 12: /* 802.1x Suite-B-192 */
576 case 13: /* FT/802.1x SHA-384 */
578 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
581 case 14: /* FILS SHA-256 */
582 case 15: /* FILS SHA-384 */
583 case 16: /* FT/FILS SHA-256 */
584 case 17: /* FT/FILS SHA-384 */
589 c
->auth_suites
|= IWINFO_KMGMT_OWE
;
595 data
+= 2 + (count
* 4);
596 len
-= 2 + (count
* 4);
599 struct uci_section
*iwinfo_uci_get_radio(const char *name
, const char *type
)
601 struct uci_ptr ptr
= {
602 .package
= "wireless",
604 .flags
= (name
&& *name
== '@') ? UCI_LOOKUP_EXTENDED
: 0,
609 uci_ctx
= uci_alloc_context();
614 if (uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, true))
617 if (!ptr
.s
|| strcmp(ptr
.s
->type
, "wifi-device") != 0)
620 opt
= uci_lookup_option_string(uci_ctx
, ptr
.s
, "type");
621 if (!opt
|| strcmp(opt
, type
) != 0)
627 void iwinfo_uci_free(void)
632 uci_free_context(uci_ctx
);
637 struct iwinfo_ubus_query_state
{
644 static void iwinfo_ubus_query_cb(struct ubus_request
*req
, int type
,
645 struct blob_attr
*msg
)
647 struct iwinfo_ubus_query_state
*st
= req
->priv
;
649 struct blobmsg_policy pol1
[2] = {
650 { "ifname", BLOBMSG_TYPE_STRING
},
651 { "config", BLOBMSG_TYPE_TABLE
}
654 struct blobmsg_policy pol2
= { st
->field
, BLOBMSG_TYPE_STRING
};
655 struct blob_attr
*cur
, *cur2
, *cur3
, *cfg
[2], *res
;
658 blobmsg_for_each_attr(cur
, msg
, rem
) {
659 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
)
662 blobmsg_for_each_attr(cur2
, cur
, rem2
) {
663 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_ARRAY
)
666 if (strcmp(blobmsg_name(cur2
), "interfaces"))
669 blobmsg_for_each_attr(cur3
, cur2
, rem3
) {
670 blobmsg_parse(pol1
, sizeof(pol1
) / sizeof(pol1
[0]), cfg
,
671 blobmsg_data(cur3
), blobmsg_len(cur3
));
673 if (!cfg
[0] || !cfg
[1] ||
674 strcmp(blobmsg_get_string(cfg
[0]), st
->ifname
))
677 blobmsg_parse(&pol2
, 1, &res
,
678 blobmsg_data(cfg
[1]), blobmsg_len(cfg
[1]));
683 strncpy(st
->buf
, blobmsg_get_string(res
), st
->len
);
690 int iwinfo_ubus_query(const char *ifname
, const char *field
,
691 char *buf
, size_t len
)
693 struct iwinfo_ubus_query_state st
= {
700 struct ubus_context
*ctx
= NULL
;
701 struct blob_buf b
= { };
705 blob_buf_init(&b
, 0);
707 ctx
= ubus_connect(NULL
);
712 if (ubus_lookup_id(ctx
, "network.wireless", &id
))
715 if (ubus_invoke(ctx
, id
, "status", b
.head
, iwinfo_ubus_query_cb
, &st
, 250))