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 <sys/types.h>
25 #include "api/broadcom.h"
27 static int wl_ioctl(const char *name
, int cmd
, void *buf
, int len
)
37 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
38 ifr
.ifr_data
= (caddr_t
) &ioc
;
40 return iwinfo_ioctl(SIOCDEVPRIVATE
, &ifr
);
43 static int wl_iovar(const char *name
, const char *cmd
, const char *arg
,
44 int arglen
, void *buf
, int buflen
)
46 int cmdlen
= strlen(cmd
) + 1;
48 memcpy(buf
, cmd
, cmdlen
);
50 if (arg
&& arglen
> 0)
51 memcpy(buf
+ cmdlen
, arg
, arglen
);
53 return wl_ioctl(name
, WLC_GET_VAR
, buf
, buflen
);
56 static struct wl_maclist
* wl_read_assoclist(const char *ifname
)
58 struct wl_maclist
*macs
;
59 int maclen
= 4 + WL_MAX_STA_COUNT
* 6;
61 if (strstr(ifname
, "wds"))
64 if ((macs
= (struct wl_maclist
*) malloc(maclen
)) != NULL
)
66 memset(macs
, 0, maclen
);
67 macs
->count
= WL_MAX_STA_COUNT
;
69 if (!wl_ioctl(ifname
, WLC_GET_ASSOCLIST
, macs
, maclen
))
79 static int wl_probe(const char *ifname
)
82 return (!wl_ioctl(ifname
, WLC_GET_MAGIC
, &magic
, sizeof(magic
)) &&
83 (magic
== WLC_IOCTL_MAGIC
));
86 static void wl_close(void)
91 static int wl_get_mode(const char *ifname
, int *buf
)
94 int ap
, infra
, passive
;
96 if ((ret
= wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
))))
99 if ((ret
= wl_ioctl(ifname
, WLC_GET_INFRA
, &infra
, sizeof(infra
))))
102 if ((ret
= wl_ioctl(ifname
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
))))
106 *buf
= IWINFO_OPMODE_MONITOR
;
108 *buf
= IWINFO_OPMODE_ADHOC
;
110 *buf
= IWINFO_OPMODE_MASTER
;
112 *buf
= IWINFO_OPMODE_CLIENT
;
117 static int wl_get_ssid(const char *ifname
, char *buf
)
122 if (!(ret
= wl_ioctl(ifname
, WLC_GET_SSID
, &ssid
, sizeof(ssid
))))
123 memcpy(buf
, ssid
.ssid
, ssid
.ssid_len
);
128 static int wl_get_bssid(const char *ifname
, char *buf
)
133 if (!(ret
= wl_ioctl(ifname
, WLC_GET_BSSID
, bssid
, 6)))
134 sprintf(buf
, "%02X:%02X:%02X:%02X:%02X:%02X",
135 (uint8_t)bssid
[0], (uint8_t)bssid
[1], (uint8_t)bssid
[2],
136 (uint8_t)bssid
[3], (uint8_t)bssid
[4], (uint8_t)bssid
[5]
142 static int wl_get_channel(const char *ifname
, int *buf
)
144 return wl_ioctl(ifname
, WLC_GET_CHANNEL
, buf
, sizeof(buf
));
147 static int wl_get_frequency(const char *ifname
, int *buf
)
149 return wext_ops
.frequency(ifname
, buf
);
152 static int wl_get_txpower(const char *ifname
, int *buf
)
154 /* WLC_GET_VAR "qtxpower" */
155 return wext_ops
.txpower(ifname
, buf
);
158 static int wl_get_bitrate(const char *ifname
, int *buf
)
163 if( !(ret
= wl_ioctl(ifname
, WLC_GET_RATE
, &rate
, sizeof(rate
))) && (rate
> 0))
164 *buf
= ((rate
/ 2) * 1000) + ((rate
& 1) ? 500 : 0);
169 static int wl_get_signal(const char *ifname
, int *buf
)
171 unsigned int ap
, rssi
, i
, rssi_count
;
172 int ioctl_req_version
= 0x2000;
173 char tmp
[WLC_IOCTL_MAXLEN
];
174 struct wl_maclist
*macs
= NULL
;
175 wl_sta_rssi_t starssi
;
177 memset(tmp
, 0, WLC_IOCTL_MAXLEN
);
178 memcpy(tmp
, &ioctl_req_version
, sizeof(ioctl_req_version
));
180 wl_ioctl(ifname
, WLC_GET_BSS_INFO
, tmp
, WLC_IOCTL_MAXLEN
);
182 if (!wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
)) && !ap
)
184 *buf
= tmp
[WL_BSS_RSSI_OFFSET
];
188 rssi
= rssi_count
= 0;
190 /* Calculate average rssi from conntected stations */
191 if ((macs
= wl_read_assoclist(ifname
)) != NULL
)
193 for (i
= 0; i
< macs
->count
; i
++)
195 memcpy(starssi
.mac
, &macs
->ea
[i
], 6);
197 if (!wl_ioctl(ifname
, WLC_GET_RSSI
, &starssi
, 12))
199 rssi
-= starssi
.rssi
;
207 *buf
= (rssi
== 0 || rssi_count
== 0) ? 1 : -(rssi
/ rssi_count
);
213 static int wl_get_noise(const char *ifname
, int *buf
)
215 unsigned int ap
, noise
;
216 int ioctl_req_version
= 0x2000;
217 char tmp
[WLC_IOCTL_MAXLEN
];
219 memset(tmp
, 0, WLC_IOCTL_MAXLEN
);
220 memcpy(tmp
, &ioctl_req_version
, sizeof(ioctl_req_version
));
222 wl_ioctl(ifname
, WLC_GET_BSS_INFO
, tmp
, WLC_IOCTL_MAXLEN
);
224 if ((wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
)) < 0) || ap
)
226 if (wl_ioctl(ifname
, WLC_GET_PHY_NOISE
, &noise
, sizeof(noise
)) < 0)
231 noise
= tmp
[WL_BSS_NOISE_OFFSET
];
239 static int wl_get_quality(const char *ifname
, int *buf
)
241 return wext_ops
.quality(ifname
, buf
);
244 static int wl_get_quality_max(const char *ifname
, int *buf
)
246 return wext_ops
.quality_max(ifname
, buf
);
249 static int wl_get_encryption(const char *ifname
, char *buf
)
251 uint32_t wsec
, wauth
, wpa
;
252 struct iwinfo_crypto_entry
*c
= (struct iwinfo_crypto_entry
*)buf
;
254 if( wl_ioctl(ifname
, WLC_GET_WPA_AUTH
, &wpa
, sizeof(uint32_t)) ||
255 wl_ioctl(ifname
, WLC_GET_WSEC
, &wsec
, sizeof(uint32_t)) ||
256 wl_ioctl(ifname
, WLC_GET_AUTH
, &wauth
, sizeof(uint32_t)) )
262 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
266 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
270 c
->pair_ciphers
|= IWINFO_CIPHER_TKIP
;
271 c
->pair_ciphers
|= IWINFO_CIPHER_CCMP
;
279 c
->auth_algs
|= IWINFO_AUTH_OPEN
;
281 else if (wsec
&& wauth
)
282 c
->auth_algs
|= IWINFO_AUTH_SHARED
;
284 /* ToDo: evaluate WEP key lengths */
285 c
->pair_ciphers
= IWINFO_CIPHER_WEP40
| IWINFO_CIPHER_WEP104
;
286 c
->auth_suites
|= IWINFO_KMGMT_NONE
;
291 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
296 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
302 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
307 c
->auth_suites
|= IWINFO_KMGMT_8021x
;
312 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
317 c
->auth_suites
|= IWINFO_KMGMT_PSK
;
324 c
->enabled
= (c
->wpa_version
|| c
->auth_algs
) ? 1 : 0;
325 c
->group_ciphers
= c
->pair_ciphers
;
330 static int wl_get_phyname(const char *ifname
, char *buf
)
336 if ((p
= strchr(buf
, '.')) != NULL
)
342 static int wl_get_enctype(const char *ifname
, char *buf
)
347 if( wl_ioctl(ifname
, WLC_GET_WPA_AUTH
, &wpa
, sizeof(uint32_t)) ||
348 wl_ioctl(ifname
, WLC_GET_WSEC
, &wsec
, sizeof(uint32_t)) )
354 sprintf(algo
, "TKIP");
358 sprintf(algo
, "CCMP");
362 sprintf(algo
, "TKIP, CCMP");
369 sprintf(buf
, "%s", wsec
? "WEP" : "None");
373 sprintf(buf
, "WPA 802.1X (%s)", algo
);
377 sprintf(buf
, "WPA PSK (%s)", algo
);
381 sprintf(buf
, "802.1X (%s)", algo
);
385 sprintf(buf
, "WPA2 802.1X (%s)", algo
);
389 sprintf(buf
, "mixed WPA/WPA2 802.1X (%s)", algo
);
393 sprintf(buf
, "WPA2 PSK (%s)", algo
);
397 sprintf(buf
, "mixed WPA/WPA2 PSK (%s)", algo
);
401 sprintf(buf
, "Unknown");
407 static void wl_get_assoclist_cb(const char *ifname
,
408 struct iwinfo_assoclist_entry
*e
)
410 wl_sta_info_t sta
= { 0 };
412 if (!wl_iovar(ifname
, "sta_info", e
->mac
, 6, &sta
, sizeof(sta
)) &&
415 e
->inactive
= sta
.idle
* 1000;
416 e
->rx_packets
= sta
.rx_ucast_pkts
;
417 e
->tx_packets
= sta
.tx_pkts
;
418 e
->rx_rate
.rate
= sta
.rx_rate
;
419 e
->tx_rate
.rate
= sta
.tx_rate
;
427 static int wl_get_assoclist(const char *ifname
, char *buf
, int *len
)
430 int ap
, infra
, passive
;
433 char devstr
[IFNAMSIZ
];
434 struct wl_maclist
*macs
;
435 struct wl_sta_rssi rssi
;
436 struct iwinfo_assoclist_entry entry
;
439 ap
= infra
= passive
= 0;
441 wl_ioctl(ifname
, WLC_GET_AP
, &ap
, sizeof(ap
));
442 wl_ioctl(ifname
, WLC_GET_INFRA
, &infra
, sizeof(infra
));
443 wl_ioctl(ifname
, WLC_GET_PASSIVE
, &passive
, sizeof(passive
));
445 if (wl_get_noise(ifname
, &noise
))
448 if ((ap
|| infra
|| passive
) && ((macs
= wl_read_assoclist(ifname
)) != NULL
))
450 for (i
= 0, j
= 0; i
< macs
->count
; i
++, j
+= sizeof(struct iwinfo_assoclist_entry
))
452 memset(&entry
, 0, sizeof(entry
));
453 memcpy(rssi
.mac
, &macs
->ea
[i
], 6);
455 if (!wl_ioctl(ifname
, WLC_GET_RSSI
, &rssi
, sizeof(struct wl_sta_rssi
)))
456 entry
.signal
= (rssi
.rssi
- 0x100);
461 memcpy(entry
.mac
, &macs
->ea
[i
], 6);
462 wl_get_assoclist_cb(ifname
, &entry
);
464 memcpy(&buf
[j
], &entry
, sizeof(entry
));
471 else if ((arp
= fopen("/proc/net/arp", "r")) != NULL
)
475 while (fgets(line
, sizeof(line
), arp
) != NULL
)
477 if (sscanf(line
, "%*s 0x%*d 0x%*d %17s %*s %s", macstr
, devstr
) && !strcmp(devstr
, ifname
))
479 rssi
.mac
[0] = strtol(&macstr
[0], NULL
, 16);
480 rssi
.mac
[1] = strtol(&macstr
[3], NULL
, 16);
481 rssi
.mac
[2] = strtol(&macstr
[6], NULL
, 16);
482 rssi
.mac
[3] = strtol(&macstr
[9], NULL
, 16);
483 rssi
.mac
[4] = strtol(&macstr
[12], NULL
, 16);
484 rssi
.mac
[5] = strtol(&macstr
[15], NULL
, 16);
486 if (!wl_ioctl(ifname
, WLC_GET_RSSI
, &rssi
, sizeof(struct wl_sta_rssi
)))
487 entry
.signal
= (rssi
.rssi
- 0x100);
492 memcpy(entry
.mac
, rssi
.mac
, 6);
493 memcpy(&buf
[j
], &entry
, sizeof(entry
));
507 static int wl_get_txpwrlist(const char *ifname
, char *buf
, int *len
)
509 struct iwinfo_txpwrlist_entry entry
;
510 uint8_t dbm
[11] = { 0, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 };
511 uint8_t mw
[11] = { 1, 3, 6, 10, 15, 25, 39, 63, 100, 158, 251 };
514 for (i
= 0; i
< 11; i
++)
518 memcpy(&buf
[i
*sizeof(entry
)], &entry
, sizeof(entry
));
521 *len
= 11 * sizeof(entry
);
525 static int wl_get_scanlist(const char *ifname
, char *buf
, int *len
)
527 return wext_ops
.scanlist(ifname
, buf
, len
);
530 static int wl_get_freqlist(const char *ifname
, char *buf
, int *len
)
532 return wext_ops
.freqlist(ifname
, buf
, len
);
535 static int wl_get_country(const char *ifname
, char *buf
)
537 char ccode
[WLC_CNTRY_BUF_SZ
];
539 if (!wl_ioctl(ifname
, WLC_GET_COUNTRY
, ccode
, WLC_CNTRY_BUF_SZ
))
542 if (!strcmp(ccode
, "IL0"))
546 else if (!strcmp(ccode
, "YU"))
550 memcpy(buf
, ccode
, 2);
558 static int wl_get_countrylist(const char *ifname
, char *buf
, int *len
)
561 char cdata
[WLC_IOCTL_MAXLEN
];
562 struct iwinfo_country_entry
*c
= (struct iwinfo_country_entry
*)buf
;
563 wl_country_list_t
*cl
= (wl_country_list_t
*)cdata
;
565 cl
->buflen
= sizeof(cdata
);
567 if (!wl_ioctl(ifname
, WLC_GET_COUNTRY_LIST
, cl
, cl
->buflen
))
569 for (i
= 0, count
= 0; i
< cl
->count
; i
++, c
++)
571 snprintf(c
->ccode
, sizeof(c
->ccode
), "%s", &cl
->country_abbrev
[i
* WLC_CNTRY_BUF_SZ
]);
572 c
->iso3166
= c
->ccode
[0] * 256 + c
->ccode
[1];
575 if (!strcmp(c
->ccode
, "IL0"))
579 else if (!strcmp(c
->ccode
, "YU"))
583 *len
= (i
* sizeof(struct iwinfo_country_entry
));
590 static int wl_get_hwmodelist(const char *ifname
, int *buf
)
593 uint i
, band
[WLC_BAND_ALL
], bands
;
595 if (!wl_ioctl(ifname
, WLC_GET_PHYTYPE
, &phytype
, sizeof(phytype
)) &&
596 !wl_ioctl(ifname
, WLC_GET_BANDLIST
, band
, sizeof(band
)))
602 *buf
= IWINFO_80211_A
;
605 *buf
= IWINFO_80211_B
;
607 case WLC_PHY_TYPE_AC
:
608 *buf
|= IWINFO_80211_AC
;
609 case WLC_PHY_TYPE_HT
:
611 *buf
|= IWINFO_80211_N
;
612 case WLC_PHY_TYPE_LP
:
615 for (i
= 1; i
<= band
[0]; i
++)
619 if (bands
& WLC_BAND_5G
)
620 *buf
|= IWINFO_80211_A
;
621 if (bands
& WLC_BAND_2G
)
623 *buf
|= IWINFO_80211_B
;
624 *buf
|= IWINFO_80211_G
;
636 static int wl_get_htmodelist(const char *ifname
, int *buf
)
640 if (!wl_get_hwmodelist(ifname
, &modes
))
644 /* FIXME: determine real capabilities */
646 if (modes
& IWINFO_80211_N
)
647 *buf
|= IWINFO_HTMODE_HT20
| IWINFO_HTMODE_HT40
;
649 if (modes
& IWINFO_80211_AC
)
650 *buf
|= IWINFO_HTMODE_VHT20
| IWINFO_HTMODE_VHT40
|
659 static int wl_get_mbssid_support(const char *ifname
, int *buf
)
661 wlc_rev_info_t revinfo
;
663 /* Multi bssid support only works on corerev >= 9 */
664 if (!wl_ioctl(ifname
, WLC_GET_REVINFO
, &revinfo
, sizeof(revinfo
)))
666 if (revinfo
.corerev
>= 9)
676 static int wl_get_hardware_id(const char *ifname
, char *buf
)
678 wlc_rev_info_t revinfo
;
679 struct iwinfo_hardware_id
*ids
= (struct iwinfo_hardware_id
*)buf
;
681 if (wl_ioctl(ifname
, WLC_GET_REVINFO
, &revinfo
, sizeof(revinfo
)))
684 ids
->vendor_id
= revinfo
.vendorid
;
685 ids
->device_id
= revinfo
.deviceid
;
686 ids
->subsystem_vendor_id
= revinfo
.boardvendor
;
687 ids
->subsystem_device_id
= revinfo
.boardid
;
692 static int wl_get_hardware_name(const char *ifname
, char *buf
)
694 struct iwinfo_hardware_id ids
;
696 if (wl_get_hardware_id(ifname
, (char *)&ids
))
699 sprintf(buf
, "Broadcom BCM%04X", ids
.device_id
);
704 static int wl_get_txpower_offset(const char *ifname
, int *buf
)
712 if (!stat("/usr/sbin/nvram", &s
) && (s
.st_mode
& S_IXUSR
))
714 if ((p
= popen("/usr/sbin/nvram get opo", "r")) != NULL
)
716 if (fread(off
, 1, sizeof(off
), p
))
717 *buf
= strtoul(off
, NULL
, 16);
726 static int wl_get_frequency_offset(const char *ifname
, int *buf
)
733 const struct iwinfo_ops wl_ops
= {
736 .channel
= wl_get_channel
,
737 .frequency
= wl_get_frequency
,
738 .frequency_offset
= wl_get_frequency_offset
,
739 .txpower
= wl_get_txpower
,
740 .txpower_offset
= wl_get_txpower_offset
,
741 .bitrate
= wl_get_bitrate
,
742 .signal
= wl_get_signal
,
743 .noise
= wl_get_noise
,
744 .quality
= wl_get_quality
,
745 .quality_max
= wl_get_quality_max
,
746 .mbssid_support
= wl_get_mbssid_support
,
747 .hwmodelist
= wl_get_hwmodelist
,
748 .htmodelist
= wl_get_htmodelist
,
751 .bssid
= wl_get_bssid
,
752 .country
= wl_get_country
,
753 .hardware_id
= wl_get_hardware_id
,
754 .hardware_name
= wl_get_hardware_name
,
755 .encryption
= wl_get_encryption
,
756 .phyname
= wl_get_phyname
,
757 .assoclist
= wl_get_assoclist
,
758 .txpwrlist
= wl_get_txpwrlist
,
759 .scanlist
= wl_get_scanlist
,
760 .freqlist
= wl_get_freqlist
,
761 .countrylist
= wl_get_countrylist
,