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 )
34 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
35 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
42 strncpy(ifr.ifr_name, name, IFNAMSIZ);
43 ifr.ifr_data = (caddr_t) &ioc;
45 return ioctl(ioctl_socket, SIOCDEVPRIVATE, &ifr);
48 static struct wl_maclist * wl_read_assoclist(const char *ifname)
50 struct wl_maclist *macs;
51 int maclen = 4 + WL_MAX_STA_COUNT * 6;
53 if( (macs = (struct wl_maclist *) malloc(maclen)) != NULL )
55 memset(macs, 0, maclen);
56 macs->count = WL_MAX_STA_COUNT;
58 if( !wl_ioctl(ifname, WLC_GET_ASSOCLIST, macs, maclen) )
68 int wl_probe(const char *ifname)
72 if( !wl_ioctl(ifname, WLC_GET_MAGIC, &magic, sizeof(magic)) && (magic == WLC_IOCTL_MAGIC))
80 if( ioctl_socket > -1 )
84 int wl_get_mode(const char *ifname, char *buf)
87 int ap, infra, passive;
89 if( (ret = wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap))) )
92 if( (ret = wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra))) )
95 if( (ret = wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive))) )
99 sprintf(buf, "Monitor");
101 sprintf(buf, "Ad-Hoc");
103 sprintf(buf, "Master");
105 sprintf(buf, "Client");
110 int wl_get_ssid(const char *ifname, char *buf)
115 if( !(ret = wl_ioctl(ifname, WLC_GET_SSID, &ssid, sizeof(ssid))) )
116 memcpy(buf, ssid.ssid, ssid.ssid_len);
121 int wl_get_bssid(const char *ifname, char *buf)
126 if( !(ret = wl_ioctl(ifname, WLC_GET_BSSID, bssid, 6)) )
127 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
128 (uint8_t)bssid[0], (uint8_t)bssid[1], (uint8_t)bssid[2],
129 (uint8_t)bssid[3], (uint8_t)bssid[4], (uint8_t)bssid[5]
135 int wl_get_channel(const char *ifname, int *buf)
137 return wl_ioctl(ifname, WLC_GET_CHANNEL, buf, sizeof(buf));
140 int wl_get_frequency(const char *ifname, int *buf)
142 return wext_get_frequency(ifname, buf);
145 int wl_get_txpower(const char *ifname, int *buf)
147 return wext_get_txpower(ifname, buf);
150 int wl_get_bitrate(const char *ifname, int *buf)
155 if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
156 *buf = ((rate / 2) * 1000) + ((rate & 1) ? 500 : 0);
161 int wl_get_signal(const char *ifname, int *buf)
163 unsigned int ap, rssi, i, rssi_count;
164 int ioctl_req_version = 0x2000;
165 char tmp[WLC_IOCTL_MAXLEN];
166 struct wl_maclist *macs = NULL;
167 wl_sta_rssi_t starssi;
169 memset(tmp, 0, WLC_IOCTL_MAXLEN);
170 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
172 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
174 if( !wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap )
176 *buf = tmp[WL_BSS_RSSI_OFFSET];
180 rssi = rssi_count = 0;
182 /* Calculate average rssi from conntected stations */
183 if( (macs = wl_read_assoclist(ifname)) != NULL )
185 for( i = 0; i < macs->count; i++ )
187 memcpy(starssi.mac, &macs->ea[i], 6);
189 if( !wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12) )
191 rssi -= starssi.rssi;
199 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
205 int wl_get_noise(const char *ifname, int *buf)
207 unsigned int ap, noise;
208 int ioctl_req_version = 0x2000;
209 char tmp[WLC_IOCTL_MAXLEN];
211 memset(tmp, 0, WLC_IOCTL_MAXLEN);
212 memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
214 wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
216 if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
218 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
223 noise = tmp[WL_BSS_NOISE_OFFSET];
231 int wl_get_quality(const char *ifname, int *buf)
233 return wext_get_quality(ifname, buf);
236 int wl_get_quality_max(const char *ifname, int *buf)
238 return wext_get_quality_max(ifname, buf);
241 int wl_get_encryption(const char *ifname, char *buf)
243 uint32_t wsec, wauth, wpa;
244 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
246 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
247 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) ||
248 wl_ioctl(ifname, WLC_GET_AUTH, &wauth, sizeof(uint32_t)) )
254 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
258 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
262 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
263 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
271 c->auth_algs |= IWINFO_AUTH_OPEN;
273 else if( wsec && wauth )
274 c->auth_algs |= IWINFO_AUTH_SHARED;
276 /* ToDo: evaluate WEP key lengths */
277 c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
278 c->auth_suites |= IWINFO_KMGMT_NONE;
283 c->auth_suites |= IWINFO_KMGMT_8021x;
288 c->auth_suites |= IWINFO_KMGMT_PSK;
294 c->auth_suites |= IWINFO_KMGMT_8021x;
299 c->auth_suites |= IWINFO_KMGMT_8021x;
304 c->auth_suites |= IWINFO_KMGMT_PSK;
309 c->auth_suites |= IWINFO_KMGMT_PSK;
316 c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
317 c->group_ciphers = c->pair_ciphers;
322 int wl_get_enctype(const char *ifname, char *buf)
327 if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
328 wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
334 sprintf(algo, "TKIP");
338 sprintf(algo, "CCMP");
342 sprintf(algo, "TKIP, CCMP");
349 sprintf(buf, "%s", wsec ? "WEP" : "None");
353 sprintf(buf, "WPA 802.1X (%s)", algo);
357 sprintf(buf, "WPA PSK (%s)", algo);
361 sprintf(buf, "802.1X (%s)", algo);
365 sprintf(buf, "WPA2 802.1X (%s)", algo);
369 sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
373 sprintf(buf, "WPA2 PSK (%s)", algo);
377 sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
381 sprintf(buf, "Unknown");
387 int wl_get_assoclist(const char *ifname, char *buf, int *len)
390 int ap, infra, passive;
393 char devstr[IFNAMSIZ];
394 struct wl_maclist *macs;
395 struct wl_sta_rssi rssi;
396 struct iwinfo_assoclist_entry entry;
399 ap = infra = passive = 0;
401 wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
402 wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
403 wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
405 if( wl_get_noise(ifname, &noise) )
408 if( (ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL) )
410 for( i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry) )
412 memcpy(rssi.mac, &macs->ea[i], 6);
414 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
415 entry.signal = (rssi.rssi - 0x100);
420 memcpy(entry.mac, &macs->ea[i], 6);
421 memcpy(&buf[j], &entry, sizeof(entry));
428 else if( (arp = fopen("/proc/net/arp", "r")) != NULL )
432 while( fgets(line, sizeof(line), arp) != NULL )
434 if( sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname) )
436 rssi.mac[0] = strtol(&macstr[0], NULL, 16);
437 rssi.mac[1] = strtol(&macstr[3], NULL, 16);
438 rssi.mac[2] = strtol(&macstr[6], NULL, 16);
439 rssi.mac[3] = strtol(&macstr[9], NULL, 16);
440 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
441 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
443 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
444 entry.signal = (rssi.rssi - 0x100);
449 memcpy(entry.mac, rssi.mac, 6);
450 memcpy(&buf[j], &entry, sizeof(entry));
464 int wl_get_txpwrlist(const char *ifname, char *buf, int *len)
466 struct iwinfo_txpwrlist_entry entry;
467 uint8_t dbm[8] = { 0, 6, 8, 10, 12, 14, 16, 18 };
468 uint8_t mw[8] = { 1, 3, 6, 10, 15, 25, 39, 63 };
471 for( i = 0; i < 8; i++ )
475 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
478 *len = 8 * sizeof(entry);
482 int wl_get_scanlist(const char *ifname, char *buf, int *len)
484 return wext_get_scanlist(ifname, buf, len);
487 int wl_get_freqlist(const char *ifname, char *buf, int *len)
489 return wext_get_freqlist(ifname, buf, len);
492 int wl_get_country(const char *ifname, char *buf)
494 char ccode[WLC_CNTRY_BUF_SZ];
496 if( !wl_ioctl(ifname, WLC_GET_COUNTRY, ccode, WLC_CNTRY_BUF_SZ) )
499 if( !strcmp(ccode, "IL0") )
503 else if( !strcmp(ccode, "YU") )
507 memcpy(buf, ccode, 2);
515 int wl_get_countrylist(const char *ifname, char *buf, int *len)
518 char cdata[WLC_IOCTL_MAXLEN];
519 struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
520 wl_country_list_t *cl = (wl_country_list_t *)cdata;
522 cl->buflen = sizeof(cdata);
524 if( !wl_ioctl(ifname, WLC_GET_COUNTRY_LIST, cl, cl->buflen) )
526 for( i = 0, count = 0; i < cl->count; i++, c++ )
528 sprintf(c->ccode, &cl->country_abbrev[i * WLC_CNTRY_BUF_SZ]);
529 c->iso3166 = c->ccode[0] * 256 + c->ccode[1];
532 if( !strcmp(c->ccode, "IL0") )
536 else if( !strcmp(c->ccode, "YU") )
540 *len = (i * sizeof(struct iwinfo_country_entry));
547 int wl_get_hwmodelist(const char *ifname, int *buf)
549 return wext_get_hwmodelist(ifname, buf);
552 int wl_get_mbssid_support(const char *ifname, int *buf)
554 wlc_rev_info_t revinfo;
556 /* Multi bssid support only works on corerev >= 9 */
557 if( !wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)) )
559 if( revinfo.corerev >= 9 )