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 int wl_iovar(const char *name
, const char *cmd
, const char *arg
,
41 int arglen
, void *buf
, int buflen
)
43 int cmdlen
= strlen(cmd
) + 1;
45 memcpy(buf
, cmd
, cmdlen
);
47 if (arg
&& arglen
> 0)
48 memcpy(buf
+ cmdlen
, arg
, arglen
);
50 return wl_ioctl(name
, WLC_GET_VAR
, buf
, buflen
);
53 static struct wl_maclist
* wl_read_assoclist(const char *ifname
)
55 struct wl_maclist
*macs
;
56 int maclen
= 4 + WL_MAX_STA_COUNT
* 6;
58 if (strstr(ifname
, "wds"))
61 if ((macs
= (struct wl_maclist
*) malloc(maclen
)) != NULL
)
63 memset(macs
, 0, maclen
);
64 macs
->count
= WL_MAX_STA_COUNT
;
66 if (!wl_ioctl(ifname
, WLC_GET_ASSOCLIST
, macs
, maclen
))
76 int wl_probe(const char *ifname
)
79 return (!wl_ioctl(ifname
, WLC_GET_MAGIC
, &magic
, sizeof(magic
)) &&
80 (magic
== WLC_IOCTL_MAGIC
));
88 int wl_get_mode(const char *ifname
, int *buf
)
91 int ap
, infra
, passive
;
93 if ((ret
= wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
))))
96 if ((ret
= wl_ioctl(ifname
, WLC_GET_INFRA
, &infra
, sizeof(infra
))))
99 if ((ret
= wl_ioctl(ifname
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
))))
103 *buf
= IWINFO_OPMODE_MONITOR
;
105 *buf
= IWINFO_OPMODE_ADHOC
;
107 *buf
= IWINFO_OPMODE_MASTER
;
109 *buf
= IWINFO_OPMODE_CLIENT
;
114 int wl_get_ssid(const char *ifname
, char *buf
)
119 if (!(ret
= wl_ioctl(ifname
, WLC_GET_SSID
, &ssid
, sizeof(ssid
))))
120 memcpy(buf
, ssid
.ssid
, ssid
.ssid_len
);
125 int wl_get_bssid(const char *ifname
, char *buf
)
130 if (!(ret
= wl_ioctl(ifname
, WLC_GET_BSSID
, bssid
, 6)))
131 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
132 (uint8_t)bssid
[0], (uint8_t)bssid
[1], (uint8_t)bssid
[2],
133 (uint8_t)bssid
[3], (uint8_t)bssid
[4], (uint8_t)bssid
[5]
139 int wl_get_channel(const char *ifname
, int *buf
)
141 return wl_ioctl(ifname
, WLC_GET_CHANNEL
, buf
, sizeof(buf
));
144 int wl_get_frequency(const char *ifname
, int *buf
)
146 return wext_get_frequency(ifname
, buf
);
149 int wl_get_txpower(const char *ifname
, int *buf
)
151 /* WLC_GET_VAR "qtxpower" */
152 return wext_get_txpower(ifname
, buf
);
155 int wl_get_bitrate(const char *ifname
, int *buf
)
160 if( !(ret
= wl_ioctl(ifname
, WLC_GET_RATE
, &rate
, sizeof(rate
))) && (rate
> 0))
161 *buf
= ((rate
/ 2) * 1000) + ((rate
& 1) ? 500 : 0);
166 int wl_get_signal(const char *ifname
, int *buf
)
168 unsigned int ap
, rssi
, i
, rssi_count
;
169 int ioctl_req_version
= 0x2000;
170 char tmp
[WLC_IOCTL_MAXLEN
];
171 struct wl_maclist
*macs
= NULL
;
172 wl_sta_rssi_t starssi
;
174 memset(tmp
, 0, WLC_IOCTL_MAXLEN
);
175 memcpy(tmp
, &ioctl_req_version
, sizeof(ioctl_req_version
));
177 wl_ioctl(ifname
, WLC_GET_BSS_INFO
, tmp
, WLC_IOCTL_MAXLEN
);
179 if (!wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
)) && !ap
)
181 *buf
= tmp
[WL_BSS_RSSI_OFFSET
];
185 rssi
= rssi_count
= 0;
187 /* Calculate average rssi from conntected stations */
188 if ((macs
= wl_read_assoclist(ifname
)) != NULL
)
190 for (i
= 0; i
< macs
->count
; i
++)
192 memcpy(starssi
.mac
, &macs
->ea
[i
], 6);
194 if (!wl_ioctl(ifname
, WLC_GET_RSSI
, &starssi
, 12))
196 rssi
-= starssi
.rssi
;
204 *buf
= (rssi
== 0 || rssi_count
== 0) ? 1 : -(rssi
/ rssi_count
);
210 int wl_get_noise(const char *ifname
, int *buf
)
212 unsigned int ap
, noise
;
213 int ioctl_req_version
= 0x2000;
214 char tmp
[WLC_IOCTL_MAXLEN
];
216 memset(tmp
, 0, WLC_IOCTL_MAXLEN
);
217 memcpy(tmp
, &ioctl_req_version
, sizeof(ioctl_req_version
));
219 wl_ioctl(ifname
, WLC_GET_BSS_INFO
, tmp
, WLC_IOCTL_MAXLEN
);
221 if ((wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0) || ap
)
223 if (wl_ioctl(ifname
, WLC_GET_PHY_NOISE
, &noise
, sizeof(noise
)) < 0)
228 noise
= tmp
[WL_BSS_NOISE_OFFSET
];
236 int wl_get_quality(const char *ifname
, int *buf
)
238 return wext_get_quality(ifname
, buf
);
241 int wl_get_quality_max(const char *ifname
, int *buf
)
243 return wext_get_quality_max(ifname
, buf
);
246 int wl_get_encryption(const char *ifname
, char *buf
)
248 uint32_t wsec
, wauth
, wpa
;
249 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
251 if( wl_ioctl(ifname
, WLC_GET_WPA_AUTH
, &wpa
, sizeof(uint32_t)) ||
252 wl_ioctl(ifname
, WLC_GET_WSEC
, &wsec
, sizeof(uint32_t)) ||
253 wl_ioctl(ifname
, WLC_GET_AUTH
, &wauth
, sizeof(uint32_t)) )
259 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
263 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
267 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
268 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
276 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
278 else if (wsec
&& wauth
)
279 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
281 /* ToDo: evaluate WEP key lengths */
282 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
283 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
288 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
293 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
299 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
304 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
309 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
314 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
321 c
->enabled
= (c
->wpa_version
|| c
->auth_algs
) ? 1 : 0;
322 c
->group_ciphers
= c
->pair_ciphers
;
327 int wl_get_phyname(const char *ifname
, char *buf
)
333 if ((p
= strchr(buf
, '.')) != NULL
)
339 int wl_get_enctype(const char *ifname
, char *buf
)
344 if( wl_ioctl(ifname
, WLC_GET_WPA_AUTH
, &wpa
, sizeof(uint32_t)) ||
345 wl_ioctl(ifname
, WLC_GET_WSEC
, &wsec
, sizeof(uint32_t)) )
351 sprintf(algo
, "TKIP");
355 sprintf(algo
, "CCMP");
359 sprintf(algo
, "TKIP, CCMP");
366 sprintf(buf
, "%s", wsec
? "WEP" : "None");
370 sprintf(buf
, "WPA 802.1X (%s)", algo
);
374 sprintf(buf
, "WPA PSK (%s)", algo
);
378 sprintf(buf
, "802.1X (%s)", algo
);
382 sprintf(buf
, "WPA2 802.1X (%s)", algo
);
386 sprintf(buf
, "mixed WPA/WPA2 802.1X (%s)", algo
);
390 sprintf(buf
, "WPA2 PSK (%s)", algo
);
394 sprintf(buf
, "mixed WPA/WPA2 PSK (%s)", algo
);
398 sprintf(buf
, "Unknown");
404 static void wl_get_assoclist_cb(const char *ifname
,
405 struct iwinfo_assoclist_entry
*e
)
407 wl_sta_info_t sta
= { 0 };
409 if (!wl_iovar(ifname
, "sta_info", e
->mac
, 6, &sta
, sizeof(sta
)) &&
412 e
->inactive
= sta
.idle
* 1000;
413 e
->rx_packets
= sta
.rx_ucast_pkts
;
414 e
->tx_packets
= sta
.tx_pkts
;
415 e
->rx_rate
.rate
= sta
.rx_rate
;
416 e
->tx_rate
.rate
= sta
.tx_rate
;
424 int wl_get_assoclist(const char *ifname
, char *buf
, int *len
)
427 int ap
, infra
, passive
;
430 char devstr
[IFNAMSIZ
];
431 struct wl_maclist
*macs
;
432 struct wl_sta_rssi rssi
;
433 struct iwinfo_assoclist_entry entry
;
436 ap
= infra
= passive
= 0;
438 wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
));
439 wl_ioctl(ifname
, WLC_GET_INFRA
, &infra
, sizeof(infra
));
440 wl_ioctl(ifname
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
));
442 if (wl_get_noise(ifname
, &noise
))
445 if ((ap
|| infra
|| passive
) && ((macs
= wl_read_assoclist(ifname
)) != NULL
))
447 for (i
= 0, j
= 0; i
< macs
->count
; i
++, j
+= sizeof(struct iwinfo_assoclist_entry
))
449 memset(&entry
, 0, sizeof(entry
));
450 memcpy(rssi
.mac
, &macs
->ea
[i
], 6);
452 if (!wl_ioctl(ifname
, WLC_GET_RSSI
, &rssi
, sizeof(struct wl_sta_rssi
)))
453 entry
.signal
= (rssi
.rssi
- 0x100);
458 memcpy(entry
.mac
, &macs
->ea
[i
], 6);
459 wl_get_assoclist_cb(ifname
, &entry
);
461 memcpy(&buf
[j
], &entry
, sizeof(entry
));
468 else if ((arp
= fopen("/proc/net/arp", "r")) != NULL
)
472 while (fgets(line
, sizeof(line
), arp
) != NULL
)
474 if (sscanf(line
, "%*s 0x%*d 0x%*d %17s %*s %s", macstr
, devstr
) && !strcmp(devstr
, ifname
))
476 rssi
.mac
[0] = strtol(&macstr
[0], NULL
, 16);
477 rssi
.mac
[1] = strtol(&macstr
[3], NULL
, 16);
478 rssi
.mac
[2] = strtol(&macstr
[6], NULL
, 16);
479 rssi
.mac
[3] = strtol(&macstr
[9], NULL
, 16);
480 rssi
.mac
[4] = strtol(&macstr
[12], NULL
, 16);
481 rssi
.mac
[5] = strtol(&macstr
[15], NULL
, 16);
483 if (!wl_ioctl(ifname
, WLC_GET_RSSI
, &rssi
, sizeof(struct wl_sta_rssi
)))
484 entry
.signal
= (rssi
.rssi
- 0x100);
489 memcpy(entry
.mac
, rssi
.mac
, 6);
490 memcpy(&buf
[j
], &entry
, sizeof(entry
));
504 int wl_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
506 struct iwinfo_txpwrlist_entry entry
;
507 uint8_t dbm
[11] = { 0, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };
508 uint8_t mw
[11] = { 1, 3, 6, 10, 15, 25, 39, 63, 100, 158, 251 };
511 for (i
= 0; i
< 11; i
++)
515 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
518 *len
= 11 * sizeof(entry
);
522 int wl_get_scanlist(const char *ifname
, char *buf
, int *len
)
524 return wext_get_scanlist(ifname
, buf
, len
);
527 int wl_get_freqlist(const char *ifname
, char *buf
, int *len
)
529 return wext_get_freqlist(ifname
, buf
, len
);
532 int wl_get_country(const char *ifname
, char *buf
)
534 char ccode
[WLC_CNTRY_BUF_SZ
];
536 if (!wl_ioctl(ifname
, WLC_GET_COUNTRY
, ccode
, WLC_CNTRY_BUF_SZ
))
539 if (!strcmp(ccode
, "IL0"))
543 else if (!strcmp(ccode
, "YU"))
547 memcpy(buf
, ccode
, 2);
555 int wl_get_countrylist(const char *ifname
, char *buf
, int *len
)
558 char cdata
[WLC_IOCTL_MAXLEN
];
559 struct iwinfo_country_entry
*c
= (struct iwinfo_country_entry
*)buf
;
560 wl_country_list_t
*cl
= (wl_country_list_t
*)cdata
;
562 cl
->buflen
= sizeof(cdata
);
564 if (!wl_ioctl(ifname
, WLC_GET_COUNTRY_LIST
, cl
, cl
->buflen
))
566 for (i
= 0, count
= 0; i
< cl
->count
; i
++, c
++)
568 sprintf(c
->ccode
, &cl
->country_abbrev
[i
* WLC_CNTRY_BUF_SZ
]);
569 c
->iso3166
= c
->ccode
[0] * 256 + c
->ccode
[1];
572 if (!strcmp(c
->ccode
, "IL0"))
576 else if (!strcmp(c
->ccode
, "YU"))
580 *len
= (i
* sizeof(struct iwinfo_country_entry
));
587 int wl_get_hwmodelist(const char *ifname
, int *buf
)
590 uint i
, band
[WLC_BAND_ALL
], bands
;
592 if (!wl_ioctl(ifname
, WLC_GET_PHYTYPE
, &phytype
, sizeof(phytype
)) &&
593 !wl_ioctl(ifname
, WLC_GET_BANDLIST
, band
, sizeof(band
)))
598 *buf
= IWINFO_80211_A
;
601 *buf
= IWINFO_80211_B
;
603 case WLC_PHY_TYPE_LP
:
607 for (i
= 1; i
<= band
[0]; i
++)
612 if (bands
& WLC_BAND_5G
)
613 *buf
|= IWINFO_80211_A
;
614 if (bands
& WLC_BAND_2G
)
616 *buf
|= IWINFO_80211_B
;
617 *buf
|= IWINFO_80211_G
;
619 if (phytype
== WLC_PHY_TYPE_N
)
620 *buf
|= IWINFO_80211_N
;
631 int wl_get_mbssid_support(const char *ifname
, int *buf
)
633 wlc_rev_info_t revinfo
;
635 /* Multi bssid support only works on corerev >= 9 */
636 if (!wl_ioctl(ifname
, WLC_GET_REVINFO
, &revinfo
, sizeof(revinfo
)))
638 if (revinfo
.corerev
>= 9)
648 int wl_get_hardware_id(const char *ifname
, char *buf
)
650 wlc_rev_info_t revinfo
;
651 struct iwinfo_hardware_id
*ids
= (struct iwinfo_hardware_id
*)buf
;
653 if (wl_ioctl(ifname
, WLC_GET_REVINFO
, &revinfo
, sizeof(revinfo
)))
656 ids
->vendor_id
= revinfo
.vendorid
;
657 ids
->device_id
= revinfo
.deviceid
;
658 ids
->subsystem_vendor_id
= revinfo
.boardvendor
;
659 ids
->subsystem_device_id
= revinfo
.boardid
;
664 int wl_get_hardware_name(const char *ifname
, char *buf
)
666 struct iwinfo_hardware_id ids
;
668 if (wl_get_hardware_id(ifname
, (char *)&ids
))
671 sprintf(buf
, "Broadcom BCM%04X", ids
.device_id
);
676 int wl_get_txpower_offset(const char *ifname
, int *buf
)
683 if ((p
= popen("/usr/sbin/nvram get opo", "r")) != NULL
)
685 if (fread(off
, 1, sizeof(off
), p
))
686 *buf
= strtoul(off
, NULL
, 16);
694 int wl_get_frequency_offset(const char *ifname
, int *buf
)