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 size_t iwinfo_format_hwmodes(int modes
, char *buf
, size_t len
)
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 };
89 if (!(modes
& ((1 << IWINFO_80211_COUNT
) - 1)))
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
]]);
105 int iwinfo_htmode_is_ht(int htmode
)
109 case IWINFO_HTMODE_HT20
:
110 case IWINFO_HTMODE_HT40
:
117 int iwinfo_htmode_is_vht(int htmode
)
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
:
132 int iwinfo_htmode_is_he(int htmode
)
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
:
147 int iwinfo_ifup(const char *ifname
)
151 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
153 if (iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
))
156 ifr
.ifr_flags
|= (IFF_UP
| IFF_RUNNING
);
158 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
161 int iwinfo_ifdown(const char *ifname
)
165 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
167 if (iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
))
170 ifr
.ifr_flags
&= ~(IFF_UP
| IFF_RUNNING
);
172 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
175 int iwinfo_ifmac(const char *ifname
)
179 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
181 if (iwinfo_ioctl(SIOCGIFHWADDR
, &ifr
))
184 ifr
.ifr_hwaddr
.sa_data
[0] |= 0x02;
185 ifr
.ifr_hwaddr
.sa_data
[1]++;
186 ifr
.ifr_hwaddr
.sa_data
[2]++;
188 return !iwinfo_ioctl(SIOCSIFHWADDR
, &ifr
);
191 void iwinfo_close(void)
193 if (ioctl_socket
> -1)
199 struct iwinfo_hardware_entry
* iwinfo_hardware(struct iwinfo_hardware_id
*id
)
202 char buf
[256] = { 0 };
203 static struct iwinfo_hardware_entry e
;
204 struct iwinfo_hardware_entry
*rv
= NULL
;
206 if (!(db
= fopen(IWINFO_HARDWARE_FILE
, "r")))
209 while (fgets(buf
, sizeof(buf
) - 1, db
) != NULL
)
211 memset(&e
, 0, sizeof(e
));
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)
220 if ((e
.vendor_id
!= 0xffff) && (e
.vendor_id
!= id
->vendor_id
))
223 if ((e
.device_id
!= 0xffff) && (e
.device_id
!= id
->device_id
))
226 if ((e
.subsystem_vendor_id
!= 0xffff) &&
227 (e
.subsystem_vendor_id
!= id
->subsystem_vendor_id
))
230 if ((e
.subsystem_device_id
!= 0xffff) &&
231 (e
.subsystem_device_id
!= id
->subsystem_device_id
))
242 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id
*id
)
251 if (!(mtd
= fopen("/proc/mtd", "r")))
254 while (fgets(buf
, sizeof(buf
), mtd
) != NULL
)
256 if (fscanf(mtd
, "mtd%d: %x %*x %127s", &off
, &len
, buf
) < 3 ||
257 (strcmp(buf
, "\"boardconfig\"") && strcmp(buf
, "\"EEPROM\"") &&
258 strcmp(buf
, "\"factory\"")))
272 snprintf(buf
, sizeof(buf
), "/dev/mtdblock%d", off
);
274 if ((fd
= open(buf
, O_RDONLY
)) < 0)
277 bc
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
|MAP_LOCKED
, fd
, 0);
279 if ((void *)bc
!= MAP_FAILED
)
284 for (off
= len
/ 2 - 0x800; off
>= 0; off
-= 0x800)
286 /* AR531X board data magic */
287 if ((bc
[off
] == 0x3533) && (bc
[off
+ 1] == 0x3131))
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];
296 /* AR5416 EEPROM magic */
297 else if ((bc
[off
] == 0xA55A) || (bc
[off
] == 0x5AA5))
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];
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))
317 id
->vendor_id
= 0x1814;
318 id
->subsystem_vendor_id
= 0x1814;
321 if (((bc
[off
] & 0xf0) == 0x30) ||
322 ((bc
[off
] & 0xff) == 0x53))
323 id
->device_id
= (bc
[off
] >> 8) | (bc
[off
] & 0x00ff) << 8;
325 id
->device_id
= bc
[off
];
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;
337 if ((bc
[off
] & 0xff) == 0x76)
338 id
->device_id
= (bc
[off
] >> 8) | (bc
[off
] & 0x00ff) << 8;
340 id
->device_id
= bc
[off
];
342 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
343 id
->subsystem_device_id
= (bc
[off
+ 0x1a] & 0x0f00) >> 8;
352 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
355 static void iwinfo_parse_rsn_cipher(uint8_t idx
, uint16_t *ciphers
)
360 *ciphers
|= IWINFO_CIPHER_NONE
;
364 *ciphers
|= IWINFO_CIPHER_WEP40
;
368 *ciphers
|= IWINFO_CIPHER_TKIP
;
375 *ciphers
|= IWINFO_CIPHER_CCMP
;
379 *ciphers
|= IWINFO_CIPHER_WEP104
;
383 *ciphers
|= IWINFO_CIPHER_GCMP
;
387 *ciphers
|= IWINFO_CIPHER_GCMP256
;
391 *ciphers
|= IWINFO_CIPHER_CCMP256
;
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 */
403 void iwinfo_parse_rsn(struct iwinfo_crypto_entry
*c
, uint8_t *data
, uint8_t len
,
404 uint16_t defcipher
, uint8_t defauth
)
407 uint8_t wpa_version
= 0;
409 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
410 static unsigned char ieee80211_oui
[3] = { 0x00, 0x0f, 0xac };
415 if (!memcmp(data
, ms_oui
, 3))
417 else if (!memcmp(data
, ieee80211_oui
, 3))
422 c
->group_ciphers
|= defcipher
;
423 c
->pair_ciphers
|= defcipher
;
424 c
->auth_suites
|= defauth
;
428 if (!memcmp(data
, ms_oui
, 3) || !memcmp(data
, ieee80211_oui
, 3))
429 iwinfo_parse_rsn_cipher(data
[3], &c
->group_ciphers
);
436 c
->pair_ciphers
|= defcipher
;
437 c
->auth_suites
|= defauth
;
441 count
= data
[0] | (data
[1] << 8);
442 if (2 + (count
* 4) > len
)
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
);
450 data
+= 2 + (count
* 4);
451 len
-= 2 + (count
* 4);
455 c
->auth_suites
|= defauth
;
459 count
= data
[0] | (data
[1] << 8);
460 if (2 + (count
* 4) > len
)
463 for (i
= 0; i
< count
; i
++)
465 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
466 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
468 switch (data
[2 + (i
* 4) + 3])
470 case 1: /* IEEE 802.1x */
471 c
->wpa_version
|= wpa_version
;
472 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
476 c
->wpa_version
|= wpa_version
;
477 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
480 case 3: /* FT/IEEE 802.1X */
482 case 5: /* IEEE 802.1X/SHA-256 */
483 case 6: /* PSK/SHA-256 */
484 case 7: /* TPK Handshake */
489 c
->auth_suites
|= IWINFO_KMGMT_SAE
;
493 case 10: /* undefined */
496 case 11: /* 802.1x Suite-B */
497 case 12: /* 802.1x Suite-B-192 */
498 case 13: /* FT/802.1x SHA-384 */
500 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
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 */
511 c
->auth_suites
|= IWINFO_KMGMT_OWE
;
517 data
+= 2 + (count
* 4);
518 len
-= 2 + (count
* 4);
521 struct uci_section
*iwinfo_uci_get_radio(const char *name
, const char *type
)
523 struct uci_ptr ptr
= {
524 .package
= "wireless",
526 .flags
= (name
&& *name
== '@') ? UCI_LOOKUP_EXTENDED
: 0,
531 uci_ctx
= uci_alloc_context();
536 if (uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, true))
539 if (!ptr
.s
|| strcmp(ptr
.s
->type
, "wifi-device") != 0)
542 opt
= uci_lookup_option_string(uci_ctx
, ptr
.s
, "type");
543 if (!opt
|| strcmp(opt
, type
) != 0)
549 void iwinfo_uci_free(void)
554 uci_free_context(uci_ctx
);
559 struct iwinfo_ubus_query_state
{
566 static void iwinfo_ubus_query_cb(struct ubus_request
*req
, int type
,
567 struct blob_attr
*msg
)
569 struct iwinfo_ubus_query_state
*st
= req
->priv
;
571 struct blobmsg_policy pol1
[2] = {
572 { "ifname", BLOBMSG_TYPE_STRING
},
573 { "config", BLOBMSG_TYPE_TABLE
}
576 struct blobmsg_policy pol2
= { st
->field
, BLOBMSG_TYPE_STRING
};
577 struct blob_attr
*cur
, *cur2
, *cur3
, *cfg
[2], *res
;
580 blobmsg_for_each_attr(cur
, msg
, rem
) {
581 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
)
584 blobmsg_for_each_attr(cur2
, cur
, rem2
) {
585 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_ARRAY
)
588 if (strcmp(blobmsg_name(cur2
), "interfaces"))
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
));
595 if (!cfg
[0] || !cfg
[1] ||
596 strcmp(blobmsg_get_string(cfg
[0]), st
->ifname
))
599 blobmsg_parse(&pol2
, 1, &res
,
600 blobmsg_data(cfg
[1]), blobmsg_len(cfg
[1]));
605 strncpy(st
->buf
, blobmsg_get_string(res
), st
->len
);
612 int iwinfo_ubus_query(const char *ifname
, const char *field
,
613 char *buf
, size_t len
)
615 struct iwinfo_ubus_query_state st
= {
622 struct ubus_context
*ctx
= NULL
;
623 struct blob_buf b
= { };
627 blob_buf_init(&b
, 0);
629 ctx
= ubus_connect(NULL
);
634 if (ubus_lookup_id(ctx
, "network.wireless", &id
))
637 if (ubus_invoke(ctx
, id
, "status", b
.head
, iwinfo_ubus_query_cb
, &st
, 250))