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 wl_ioctl(const char *name
, int cmd
, void *buf
, int len
)
34 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
35 ifr
.ifr_data
= (caddr_t
) &ioc
;
37 return iwinfo_ioctl(SIOCDEVPRIVATE
, &ifr
);
40 static struct wl_maclist
* wl_read_assoclist(const char *ifname
)
42 struct wl_maclist
*macs
;
43 int maclen
= 4 + WL_MAX_STA_COUNT
* 6;
45 if( (macs
= (struct wl_maclist
*) malloc(maclen
)) != NULL
)
47 memset(macs
, 0, maclen
);
48 macs
->count
= WL_MAX_STA_COUNT
;
50 if( !wl_ioctl(ifname
, WLC_GET_ASSOCLIST
, macs
, maclen
) )
60 int wl_probe(const char *ifname
)
64 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 /* WLC_GET_VAR "qtxpower" */
139 return wext_get_txpower(ifname
, buf
);
142 int wl_get_bitrate(const char *ifname
, int *buf
)
147 if( !(ret
= wl_ioctl(ifname
, WLC_GET_RATE
, &rate
, sizeof(rate
))) && (rate
> 0))
148 *buf
= ((rate
/ 2) * 1000) + ((rate
& 1) ? 500 : 0);
153 int wl_get_signal(const char *ifname
, int *buf
)
155 unsigned int ap
, rssi
, i
, rssi_count
;
156 int ioctl_req_version
= 0x2000;
157 char tmp
[WLC_IOCTL_MAXLEN
];
158 struct wl_maclist
*macs
= NULL
;
159 wl_sta_rssi_t starssi
;
161 memset(tmp
, 0, WLC_IOCTL_MAXLEN
);
162 memcpy(tmp
, &ioctl_req_version
, sizeof(ioctl_req_version
));
164 wl_ioctl(ifname
, WLC_GET_BSS_INFO
, tmp
, WLC_IOCTL_MAXLEN
);
166 if( !wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
)) && !ap
)
168 *buf
= tmp
[WL_BSS_RSSI_OFFSET
];
172 rssi
= rssi_count
= 0;
174 /* Calculate average rssi from conntected stations */
175 if( (macs
= wl_read_assoclist(ifname
)) != NULL
)
177 for( i
= 0; i
< macs
->count
; i
++ )
179 memcpy(starssi
.mac
, &macs
->ea
[i
], 6);
181 if( !wl_ioctl(ifname
, WLC_GET_RSSI
, &starssi
, 12) )
183 rssi
-= starssi
.rssi
;
191 *buf
= (rssi
== 0 || rssi_count
== 0) ? 1 : -(rssi
/ rssi_count
);
197 int wl_get_noise(const char *ifname
, int *buf
)
199 unsigned int ap
, noise
;
200 int ioctl_req_version
= 0x2000;
201 char tmp
[WLC_IOCTL_MAXLEN
];
203 memset(tmp
, 0, WLC_IOCTL_MAXLEN
);
204 memcpy(tmp
, &ioctl_req_version
, sizeof(ioctl_req_version
));
206 wl_ioctl(ifname
, WLC_GET_BSS_INFO
, tmp
, WLC_IOCTL_MAXLEN
);
208 if ((wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0) || ap
)
210 if (wl_ioctl(ifname
, WLC_GET_PHY_NOISE
, &noise
, sizeof(noise
)) < 0)
215 noise
= tmp
[WL_BSS_NOISE_OFFSET
];
223 int wl_get_quality(const char *ifname
, int *buf
)
225 return wext_get_quality(ifname
, buf
);
228 int wl_get_quality_max(const char *ifname
, int *buf
)
230 return wext_get_quality_max(ifname
, buf
);
233 int wl_get_encryption(const char *ifname
, char *buf
)
235 uint32_t wsec
, wauth
, wpa
;
236 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
238 if( wl_ioctl(ifname
, WLC_GET_WPA_AUTH
, &wpa
, sizeof(uint32_t)) ||
239 wl_ioctl(ifname
, WLC_GET_WSEC
, &wsec
, sizeof(uint32_t)) ||
240 wl_ioctl(ifname
, WLC_GET_AUTH
, &wauth
, sizeof(uint32_t)) )
246 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
250 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
254 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
255 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
263 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
265 else if( wsec
&& wauth
)
266 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
268 /* ToDo: evaluate WEP key lengths */
269 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
270 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
275 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
280 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
286 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
291 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
296 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
301 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
308 c
->enabled
= (c
->wpa_version
|| c
->auth_algs
) ? 1 : 0;
309 c
->group_ciphers
= c
->pair_ciphers
;
314 int wl_get_enctype(const char *ifname
, char *buf
)
319 if( wl_ioctl(ifname
, WLC_GET_WPA_AUTH
, &wpa
, sizeof(uint32_t)) ||
320 wl_ioctl(ifname
, WLC_GET_WSEC
, &wsec
, sizeof(uint32_t)) )
326 sprintf(algo
, "TKIP");
330 sprintf(algo
, "CCMP");
334 sprintf(algo
, "TKIP, CCMP");
341 sprintf(buf
, "%s", wsec
? "WEP" : "None");
345 sprintf(buf
, "WPA 802.1X (%s)", algo
);
349 sprintf(buf
, "WPA PSK (%s)", algo
);
353 sprintf(buf
, "802.1X (%s)", algo
);
357 sprintf(buf
, "WPA2 802.1X (%s)", algo
);
361 sprintf(buf
, "mixed WPA/WPA2 802.1X (%s)", algo
);
365 sprintf(buf
, "WPA2 PSK (%s)", algo
);
369 sprintf(buf
, "mixed WPA/WPA2 PSK (%s)", algo
);
373 sprintf(buf
, "Unknown");
379 int wl_get_assoclist(const char *ifname
, char *buf
, int *len
)
382 int ap
, infra
, passive
;
385 char devstr
[IFNAMSIZ
];
386 struct wl_maclist
*macs
;
387 struct wl_sta_rssi rssi
;
388 struct iwinfo_assoclist_entry entry
;
391 ap
= infra
= passive
= 0;
393 wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
));
394 wl_ioctl(ifname
, WLC_GET_INFRA
, &infra
, sizeof(infra
));
395 wl_ioctl(ifname
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
));
397 if( wl_get_noise(ifname
, &noise
) )
400 if( (ap
|| infra
|| passive
) && ((macs
= wl_read_assoclist(ifname
)) != NULL
) )
402 for( i
= 0, j
= 0; i
< macs
->count
; i
++, j
+= sizeof(struct iwinfo_assoclist_entry
) )
404 memcpy(rssi
.mac
, &macs
->ea
[i
], 6);
406 if( !wl_ioctl(ifname
, WLC_GET_RSSI
, &rssi
, sizeof(struct wl_sta_rssi
)) )
407 entry
.signal
= (rssi
.rssi
- 0x100);
412 memcpy(entry
.mac
, &macs
->ea
[i
], 6);
413 memcpy(&buf
[j
], &entry
, sizeof(entry
));
420 else if( (arp
= fopen("/proc/net/arp", "r")) != NULL
)
424 while( fgets(line
, sizeof(line
), arp
) != NULL
)
426 if( sscanf(line
, "%*s 0x%*d 0x%*d %17s %*s %s", macstr
, devstr
) && !strcmp(devstr
, ifname
) )
428 rssi
.mac
[0] = strtol(&macstr
[0], NULL
, 16);
429 rssi
.mac
[1] = strtol(&macstr
[3], NULL
, 16);
430 rssi
.mac
[2] = strtol(&macstr
[6], NULL
, 16);
431 rssi
.mac
[3] = strtol(&macstr
[9], NULL
, 16);
432 rssi
.mac
[4] = strtol(&macstr
[12], NULL
, 16);
433 rssi
.mac
[5] = strtol(&macstr
[15], NULL
, 16);
435 if( !wl_ioctl(ifname
, WLC_GET_RSSI
, &rssi
, sizeof(struct wl_sta_rssi
)) )
436 entry
.signal
= (rssi
.rssi
- 0x100);
441 memcpy(entry
.mac
, rssi
.mac
, 6);
442 memcpy(&buf
[j
], &entry
, sizeof(entry
));
456 int wl_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
458 struct iwinfo_txpwrlist_entry entry
;
459 uint8_t dbm
[8] = { 0, 6, 8, 10, 12, 14, 16, 18 };
460 uint8_t mw
[8] = { 1, 3, 6, 10, 15, 25, 39, 63 };
463 for( i
= 0; i
< 8; i
++ )
467 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
470 *len
= 8 * sizeof(entry
);
474 int wl_get_scanlist(const char *ifname
, char *buf
, int *len
)
476 return wext_get_scanlist(ifname
, buf
, len
);
479 int wl_get_freqlist(const char *ifname
, char *buf
, int *len
)
481 return wext_get_freqlist(ifname
, buf
, len
);
484 int wl_get_country(const char *ifname
, char *buf
)
486 char ccode
[WLC_CNTRY_BUF_SZ
];
488 if( !wl_ioctl(ifname
, WLC_GET_COUNTRY
, ccode
, WLC_CNTRY_BUF_SZ
) )
491 if( !strcmp(ccode
, "IL0") )
495 else if( !strcmp(ccode
, "YU") )
499 memcpy(buf
, ccode
, 2);
507 int wl_get_countrylist(const char *ifname
, char *buf
, int *len
)
510 char cdata
[WLC_IOCTL_MAXLEN
];
511 struct iwinfo_country_entry
*c
= (struct iwinfo_country_entry
*)buf
;
512 wl_country_list_t
*cl
= (wl_country_list_t
*)cdata
;
514 cl
->buflen
= sizeof(cdata
);
516 if( !wl_ioctl(ifname
, WLC_GET_COUNTRY_LIST
, cl
, cl
->buflen
) )
518 for( i
= 0, count
= 0; i
< cl
->count
; i
++, c
++ )
520 sprintf(c
->ccode
, &cl
->country_abbrev
[i
* WLC_CNTRY_BUF_SZ
]);
521 c
->iso3166
= c
->ccode
[0] * 256 + c
->ccode
[1];
524 if( !strcmp(c
->ccode
, "IL0") )
528 else if( !strcmp(c
->ccode
, "YU") )
532 *len
= (i
* sizeof(struct iwinfo_country_entry
));
539 int wl_get_hwmodelist(const char *ifname
, int *buf
)
541 return wext_get_hwmodelist(ifname
, buf
);
544 int wl_get_mbssid_support(const char *ifname
, int *buf
)
546 wlc_rev_info_t revinfo
;
548 /* Multi bssid support only works on corerev >= 9 */
549 if( !wl_ioctl(ifname
, WLC_GET_REVINFO
, &revinfo
, sizeof(revinfo
)) )
551 if( revinfo
.corerev
>= 9 )
561 int wl_get_hardware_id(const char *ifname
, char *buf
)
563 wlc_rev_info_t revinfo
;
564 struct iwinfo_hardware_id
*ids
= (struct iwinfo_hardware_id
*)buf
;
566 if (wl_ioctl(ifname
, WLC_GET_REVINFO
, &revinfo
, sizeof(revinfo
)))
569 ids
->vendor_id
= revinfo
.vendorid
;
570 ids
->device_id
= revinfo
.deviceid
;
571 ids
->subsystem_vendor_id
= revinfo
.boardvendor
;
572 ids
->subsystem_device_id
= revinfo
.boardid
;
577 int wl_get_hardware_name(const char *ifname
, char *buf
)
579 struct iwinfo_hardware_id ids
;
581 if (wl_get_hardware_id(ifname
, (char *)&ids
))
584 sprintf(buf
, "Broadcom BCM%04X", ids
.device_id
);
589 int wl_get_txpower_offset(const char *ifname
, int *buf
)
596 if ((p
= popen("/usr/sbin/nvram get opo", "r")) != NULL
)
598 if (fread(off
, 1, sizeof(off
), p
))
599 *buf
= strtoul(off
, NULL
, 16);
607 int wl_get_frequency_offset(const char *ifname
, int *buf
)