2 * iwinfo - Wireless Information Library - Broadcom wl.o Backend
4 * Copyright (C) 2009 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 * This code is based on the wlc.c utility published by OpenWrt.org .
21 #include "iwinfo_wl.h"
22 #include "iwinfo_wext.h"
24 static int ioctl_socket = -1;
26 static int wl_ioctl(const char *name, int cmd, void *buf, int len)
32 if( ioctl_socket == -1 )
33 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
39 strncpy(ifr.ifr_name, name, IFNAMSIZ);
40 ifr.ifr_data = (caddr_t) &ioc;
42 return ioctl(ioctl_socket, SIOCDEVPRIVATE, &ifr);
45 static struct wl_maclist * wl_read_assoclist(const char *ifname)
47 struct wl_maclist *macs;
48 int maclen = 4 + WL_MAX_STA_COUNT * 6;
50 if( (macs = (struct wl_maclist *) malloc(maclen)) != NULL )
52 memset(macs, 0, maclen);
53 macs->count = WL_MAX_STA_COUNT;
55 if( !wl_ioctl(ifname, WLC_GET_ASSOCLIST, macs, maclen) )
65 int wl_probe(const char *ifname)
69 if( !wl_ioctl(ifname, WLC_GET_MAGIC, &magic, sizeof(magic)) && (magic == WLC_IOCTL_MAGIC))
75 int wl_get_mode(const char *ifname, char *buf)
78 int ap, infra, passive;
80 if( (ret = wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap))) )
83 if( (ret = wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra))) )
86 if( (ret = wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive))) )
90 sprintf(buf, "Monitor");
92 sprintf(buf, "Ad-Hoc");
94 sprintf(buf, "Master");
96 sprintf(buf, "Client");
101 int wl_get_ssid(const char *ifname, char *buf)
106 if( !(ret = wl_ioctl(ifname, WLC_GET_SSID, &ssid, sizeof(ssid))) )
107 memcpy(buf, ssid.ssid, ssid.ssid_len);
112 int wl_get_bssid(const char *ifname, char *buf)
117 if( !(ret = wl_ioctl(ifname, WLC_GET_BSSID, bssid, 6)) )
118 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
119 (uint8_t)bssid[0], (uint8_t)bssid[1], (uint8_t)bssid[2],
120 (uint8_t)bssid[3], (uint8_t)bssid[4], (uint8_t)bssid[5]
126 int wl_get_channel(const char *ifname, int *buf)
128 return wl_ioctl(ifname, WLC_GET_CHANNEL, buf, sizeof(buf));
131 int wl_get_frequency(const char *ifname, int *buf)
133 return wext_get_frequency(ifname, buf);
136 int wl_get_txpower(const char *ifname, int *buf)
138 return wext_get_txpower(ifname, buf);
141 int wl_get_bitrate(const char *ifname, int *buf)
146 if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
147 *buf = ((rate / 2) * 1000) + ((rate & 1) ? 500 : 0);
152 int wl_get_signal(const char *ifname, int *buf)
154 unsigned int ap, rssi, i, rssi_count;
155 int ioctl_req_version = 0x2000;
156 char tmp[WLC_IOCTL_MAXLEN];
157 struct wl_maclist *macs = NULL;
158 wl_sta_rssi_t starssi;
160 memset(tmp, 0, WLC_IOCTL_MAXLEN);
161 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
163 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
165 if( !wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap )
167 *buf = tmp[WL_BSS_RSSI_OFFSET];
171 rssi = rssi_count = 0;
173 /* Calculate average rssi from conntected stations */
174 if( (macs = wl_read_assoclist(ifname)) != NULL )
176 for( i = 0; i < macs->count; i++ )
178 memcpy(starssi.mac, &macs->ea[i], 6);
180 if( !wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12) )
182 rssi -= starssi.rssi;
190 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
196 int wl_get_noise(const char *ifname, int *buf)
198 unsigned int ap, noise;
199 int ioctl_req_version = 0x2000;
200 char tmp[WLC_IOCTL_MAXLEN];
202 memset(tmp, 0, WLC_IOCTL_MAXLEN);
203 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
205 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
207 if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
209 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
214 noise = tmp[WL_BSS_NOISE_OFFSET];
222 int wl_get_quality(const char *ifname, int *buf)
224 return wext_get_quality(ifname, buf);
227 int wl_get_quality_max(const char *ifname, int *buf)
229 return wext_get_quality_max(ifname, buf);
232 int wl_get_encryption(const char *ifname, char *buf)
234 uint32_t wsec, wauth, wpa;
235 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
237 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
238 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) ||
239 wl_ioctl(ifname, WLC_GET_AUTH, &wauth, sizeof(uint32_t)) )
245 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
249 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
253 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
254 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
262 c->auth_algs |= IWINFO_AUTH_OPEN;
264 else if( wsec && wauth )
265 c->auth_algs |= IWINFO_AUTH_SHARED;
267 /* ToDo: evaluate WEP key lengths */
268 c->auth_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
269 c->auth_suites |= IWINFO_KMGMT_NONE;
274 c->auth_suites |= IWINFO_KMGMT_8021x;
279 c->auth_suites |= IWINFO_KMGMT_PSK;
285 c->auth_suites |= IWINFO_KMGMT_8021x;
290 c->auth_suites |= IWINFO_KMGMT_8021x;
295 c->auth_suites |= IWINFO_KMGMT_PSK;
300 c->auth_suites |= IWINFO_KMGMT_PSK;
307 c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
308 c->group_ciphers = c->pair_ciphers;
313 int wl_get_enctype(const char *ifname, char *buf)
318 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
319 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
325 sprintf(algo, "TKIP");
329 sprintf(algo, "CCMP");
333 sprintf(algo, "TKIP, CCMP");
340 sprintf(buf, "%s", wsec ? "WEP" : "None");
344 sprintf(buf, "WPA 802.1X (%s)", algo);
348 sprintf(buf, "WPA PSK (%s)", algo);
352 sprintf(buf, "802.1X (%s)", algo);
356 sprintf(buf, "WPA2 802.1X (%s)", algo);
360 sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
364 sprintf(buf, "WPA2 PSK (%s)", algo);
368 sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
372 sprintf(buf, "Unknown");
378 int wl_get_assoclist(const char *ifname, char *buf, int *len)
381 int ap, infra, passive;
384 char devstr[IFNAMSIZ];
385 struct wl_maclist *macs;
386 struct wl_sta_rssi rssi;
387 struct iwinfo_assoclist_entry entry;
390 ap = infra = passive = 0;
392 wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
393 wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
394 wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
396 if( wl_get_noise(ifname, &noise) )
399 if( (ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL) )
401 for( i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry) )
403 memcpy(rssi.mac, &macs->ea[i], 6);
405 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
406 entry.signal = (rssi.rssi - 0x100);
411 memcpy(entry.mac, &macs->ea[i], 6);
412 memcpy(&buf[j], &entry, sizeof(entry));
419 else if( (arp = fopen("/proc/net/arp", "r")) != NULL )
423 while( fgets(line, sizeof(line), arp) != NULL )
425 if( sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname) )
427 rssi.mac[0] = strtol(&macstr[0], NULL, 16);
428 rssi.mac[1] = strtol(&macstr[3], NULL, 16);
429 rssi.mac[2] = strtol(&macstr[6], NULL, 16);
430 rssi.mac[3] = strtol(&macstr[9], NULL, 16);
431 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
432 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
434 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
435 entry.signal = (rssi.rssi - 0x100);
440 memcpy(entry.mac, rssi.mac, 6);
441 memcpy(&buf[j], &entry, sizeof(entry));
455 int wl_get_txpwrlist(const char *ifname, char *buf, int *len)
457 struct iwinfo_txpwrlist_entry entry;
458 uint8_t dbm[8] = { 0, 6, 8, 10, 12, 14, 16, 18 };
459 uint8_t mw[8] = { 1, 3, 6, 10, 15, 25, 39, 63 };
462 for( i = 0; i < 8; i++ )
466 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
469 *len = 8 * sizeof(entry);
473 int wl_get_scanlist(const char *ifname, char *buf, int *len)
475 return wext_get_scanlist(ifname, buf, len);
478 int wl_get_freqlist(const char *ifname, char *buf, int *len)
480 return wext_get_freqlist(ifname, buf, len);
483 int wl_get_mbssid_support(const char *ifname, int *buf)
485 wlc_rev_info_t revinfo;
487 /* Multi bssid support only works on corerev >= 9 */
488 if( !wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)) )
490 if( revinfo.corerev >= 9 )