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 int iwinfo_ifup(const char *ifname
)
84 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
86 if (iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
))
89 ifr
.ifr_flags
|= (IFF_UP
| IFF_RUNNING
);
91 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
94 int iwinfo_ifdown(const char *ifname
)
98 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
100 if (iwinfo_ioctl(SIOCGIFFLAGS
, &ifr
))
103 ifr
.ifr_flags
&= ~(IFF_UP
| IFF_RUNNING
);
105 return !iwinfo_ioctl(SIOCSIFFLAGS
, &ifr
);
108 int iwinfo_ifmac(const char *ifname
)
112 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
- 1);
114 if (iwinfo_ioctl(SIOCGIFHWADDR
, &ifr
))
117 ifr
.ifr_hwaddr
.sa_data
[0] |= 0x02;
118 ifr
.ifr_hwaddr
.sa_data
[1]++;
119 ifr
.ifr_hwaddr
.sa_data
[2]++;
121 return !iwinfo_ioctl(SIOCSIFHWADDR
, &ifr
);
124 void iwinfo_close(void)
126 if (ioctl_socket
> -1)
132 struct iwinfo_hardware_entry
* iwinfo_hardware(struct iwinfo_hardware_id
*id
)
135 char buf
[256] = { 0 };
136 static struct iwinfo_hardware_entry e
;
137 struct iwinfo_hardware_entry
*rv
= NULL
;
139 if (!(db
= fopen(IWINFO_HARDWARE_FILE
, "r")))
142 while (fgets(buf
, sizeof(buf
) - 1, db
) != NULL
)
144 memset(&e
, 0, sizeof(e
));
146 if (sscanf(buf
, "%hx %hx %hx %hx %hd %hd \"%63[^\"]\" \"%63[^\"]\"",
147 &e
.vendor_id
, &e
.device_id
,
148 &e
.subsystem_vendor_id
, &e
.subsystem_device_id
,
149 &e
.txpower_offset
, &e
.frequency_offset
,
150 e
.vendor_name
, e
.device_name
) < 8)
153 if ((e
.vendor_id
!= 0xffff) && (e
.vendor_id
!= id
->vendor_id
))
156 if ((e
.device_id
!= 0xffff) && (e
.device_id
!= id
->device_id
))
159 if ((e
.subsystem_vendor_id
!= 0xffff) &&
160 (e
.subsystem_vendor_id
!= id
->subsystem_vendor_id
))
163 if ((e
.subsystem_device_id
!= 0xffff) &&
164 (e
.subsystem_device_id
!= id
->subsystem_device_id
))
175 int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id
*id
)
184 if (!(mtd
= fopen("/proc/mtd", "r")))
187 while (fgets(buf
, sizeof(buf
), mtd
) != NULL
)
189 if (fscanf(mtd
, "mtd%d: %x %*x %127s", &off
, &len
, buf
) < 3 ||
190 (strcmp(buf
, "\"boardconfig\"") && strcmp(buf
, "\"EEPROM\"") &&
191 strcmp(buf
, "\"factory\"")))
205 snprintf(buf
, sizeof(buf
), "/dev/mtdblock%d", off
);
207 if ((fd
= open(buf
, O_RDONLY
)) < 0)
210 bc
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
|MAP_LOCKED
, fd
, 0);
212 if ((void *)bc
!= MAP_FAILED
)
217 for (off
= len
/ 2 - 0x800; off
>= 0; off
-= 0x800)
219 /* AR531X board data magic */
220 if ((bc
[off
] == 0x3533) && (bc
[off
+ 1] == 0x3131))
222 id
->vendor_id
= bc
[off
+ 0x7d];
223 id
->device_id
= bc
[off
+ 0x7c];
224 id
->subsystem_vendor_id
= bc
[off
+ 0x84];
225 id
->subsystem_device_id
= bc
[off
+ 0x83];
229 /* AR5416 EEPROM magic */
230 else if ((bc
[off
] == 0xA55A) || (bc
[off
] == 0x5AA5))
232 id
->vendor_id
= bc
[off
+ 0x0D];
233 id
->device_id
= bc
[off
+ 0x0E];
234 id
->subsystem_vendor_id
= bc
[off
+ 0x13];
235 id
->subsystem_device_id
= bc
[off
+ 0x14];
240 else if ((bc
[off
] == 0x3352) || (bc
[off
] == 0x5233) ||
241 (bc
[off
] == 0x3350) || (bc
[off
] == 0x5033) ||
242 (bc
[off
] == 0x3050) || (bc
[off
] == 0x5030) ||
243 (bc
[off
] == 0x3052) || (bc
[off
] == 0x5230))
246 id
->vendor_id
= 0x1814;
247 id
->subsystem_vendor_id
= 0x1814;
250 if ((bc
[off
] & 0xf0) == 0x30)
251 id
->device_id
= (bc
[off
] >> 8) | (bc
[off
] & 0x00ff) << 8;
253 id
->device_id
= bc
[off
];
255 /* subsystem from EEPROM_NIC_CONF0_RF_TYPE */
256 id
->subsystem_device_id
= (bc
[off
+ 0x1a] & 0x0f00) >> 8;
265 return (id
->vendor_id
&& id
->device_id
) ? 0 : -1;
268 void iwinfo_parse_rsn(struct iwinfo_crypto_entry
*c
, uint8_t *data
, uint8_t len
,
269 uint8_t defcipher
, uint8_t defauth
)
273 static unsigned char ms_oui
[3] = { 0x00, 0x50, 0xf2 };
274 static unsigned char ieee80211_oui
[3] = { 0x00, 0x0f, 0xac };
279 if (!memcmp(data
, ms_oui
, 3))
281 else if (!memcmp(data
, ieee80211_oui
, 3))
286 c
->group_ciphers
|= defcipher
;
287 c
->pair_ciphers
|= defcipher
;
288 c
->auth_suites
|= defauth
;
292 if (!memcmp(data
, ms_oui
, 3) || !memcmp(data
, ieee80211_oui
, 3))
296 case 1: c
->group_ciphers
|= IWINFO_CIPHER_WEP40
; break;
297 case 2: c
->group_ciphers
|= IWINFO_CIPHER_TKIP
; break;
298 case 4: c
->group_ciphers
|= IWINFO_CIPHER_CCMP
; break;
299 case 5: c
->group_ciphers
|= IWINFO_CIPHER_WEP104
; break;
300 case 6: /* AES-128-CMAC */ break;
301 default: /* proprietary */ break;
310 c
->pair_ciphers
|= defcipher
;
311 c
->auth_suites
|= defauth
;
315 count
= data
[0] | (data
[1] << 8);
316 if (2 + (count
* 4) > len
)
319 for (i
= 0; i
< count
; i
++)
321 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
322 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
324 switch (data
[2 + (i
* 4) + 3])
326 case 1: c
->pair_ciphers
|= IWINFO_CIPHER_WEP40
; break;
327 case 2: c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
; break;
328 case 4: c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
; break;
329 case 5: c
->pair_ciphers
|= IWINFO_CIPHER_WEP104
; break;
330 case 6: /* AES-128-CMAC */ break;
331 default: /* proprietary */ break;
336 data
+= 2 + (count
* 4);
337 len
-= 2 + (count
* 4);
341 c
->auth_suites
|= defauth
;
345 count
= data
[0] | (data
[1] << 8);
346 if (2 + (count
* 4) > len
)
349 for (i
= 0; i
< count
; i
++)
351 if (!memcmp(data
+ 2 + (i
* 4), ms_oui
, 3) ||
352 !memcmp(data
+ 2 + (i
* 4), ieee80211_oui
, 3))
354 switch (data
[2 + (i
* 4) + 3])
356 case 1: c
->auth_suites
|= IWINFO_KMGMT_8021x
; break;
357 case 2: c
->auth_suites
|= IWINFO_KMGMT_PSK
; break;
358 case 3: /* FT/IEEE 802.1X */ break;
359 case 4: /* FT/PSK */ break;
360 case 5: /* IEEE 802.1X/SHA-256 */ break;
361 case 6: /* PSK/SHA-256 */ break;
362 default: /* proprietary */ break;
367 data
+= 2 + (count
* 4);
368 len
-= 2 + (count
* 4);
371 struct uci_section
*iwinfo_uci_get_radio(const char *name
, const char *type
)
373 struct uci_ptr ptr
= {
374 .package
= "wireless",
376 .flags
= (name
&& *name
== '@') ? UCI_LOOKUP_EXTENDED
: 0,
381 uci_ctx
= uci_alloc_context();
386 if (uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, true))
389 if (!ptr
.s
|| strcmp(ptr
.s
->type
, "wifi-device") != 0)
392 opt
= uci_lookup_option_string(uci_ctx
, ptr
.s
, "type");
393 if (!opt
|| strcmp(opt
, type
) != 0)
399 void iwinfo_uci_free(void)
404 uci_free_context(uci_ctx
);
409 struct iwinfo_ubus_query_state
{
416 static void iwinfo_ubus_query_cb(struct ubus_request
*req
, int type
,
417 struct blob_attr
*msg
)
419 struct iwinfo_ubus_query_state
*st
= req
->priv
;
421 struct blobmsg_policy pol1
[2] = {
422 { "ifname", BLOBMSG_TYPE_STRING
},
423 { "config", BLOBMSG_TYPE_TABLE
}
426 struct blobmsg_policy pol2
= { st
->field
, BLOBMSG_TYPE_STRING
};
427 struct blob_attr
*cur
, *cur2
, *cur3
, *cfg
[2], *res
;
430 blobmsg_for_each_attr(cur
, msg
, rem
) {
431 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
)
434 blobmsg_for_each_attr(cur2
, cur
, rem2
) {
435 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_ARRAY
)
438 if (strcmp(blobmsg_name(cur2
), "interfaces"))
441 blobmsg_for_each_attr(cur3
, cur2
, rem3
) {
442 blobmsg_parse(pol1
, sizeof(pol1
) / sizeof(pol1
[0]), cfg
,
443 blobmsg_data(cur3
), blobmsg_len(cur3
));
445 if (!cfg
[0] || !cfg
[1] ||
446 strcmp(blobmsg_get_string(cfg
[0]), st
->ifname
))
449 blobmsg_parse(&pol2
, 1, &res
,
450 blobmsg_data(cfg
[1]), blobmsg_len(cfg
[1]));
455 strncpy(st
->buf
, blobmsg_get_string(res
), st
->len
);
462 int iwinfo_ubus_query(const char *ifname
, const char *field
,
463 char *buf
, size_t len
)
465 struct iwinfo_ubus_query_state st
= {
472 struct ubus_context
*ctx
= NULL
;
473 struct blob_buf b
= { };
477 blob_buf_init(&b
, 0);
479 ctx
= ubus_connect(NULL
);
484 if (ubus_lookup_id(ctx
, "network.wireless", &id
))
487 if (ubus_invoke(ctx
, id
, "status", b
.head
, iwinfo_ubus_query_cb
, &st
, 250))