2 * iwinfo - Wireless Information Library - Madwifi 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 * The signal handling code is derived from the official madwifi tools,
19 * wlanconfig.c in particular. The encryption property handling was
20 * inspired by the hostapd madwifi driver.
23 #include "iwinfo_madwifi.h"
24 #include "iwinfo_wext.h"
26 static int ioctl_socket = -1;
28 static int madwifi_ioctl(struct iwreq *wrq, const char *ifname, int cmd, void *data, size_t len)
31 if( ioctl_socket == -1 )
32 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
34 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
40 memcpy(wrq->u.name, data, len);
44 wrq->u.data.pointer = data;
45 wrq->u.data.length = len;
49 return ioctl(ioctl_socket, cmd, wrq);
52 static int get80211priv(const char *ifname, int op, void *data, size_t len)
56 if( madwifi_ioctl(&iwr, ifname, op, data, len) < 0 )
59 return iwr.u.data.length;
62 static int madwifi_isvap(const char *ifname, const char *wifiname)
70 if( strlen(ifname) <= 9 )
72 sprintf(path, "/proc/sys/net/%s/%%parent", ifname);
74 if( (fd = open(path, O_RDONLY)) > -1 )
76 if( wifiname != NULL )
78 if( read(fd, name, strlen(wifiname)) == strlen(wifiname) )
79 ret = strncmp(name, wifiname, strlen(wifiname)) ? 0 : 1;
81 else if( read(fd, name, 4) == 4 )
83 ret = strncmp(name, "wifi", 4) ? 0 : 1;
93 static int madwifi_iswifi(const char *ifname)
101 if( strlen(ifname) <= 7 )
103 sprintf(path, "/proc/sys/dev/%s/diversity", ifname);
105 if( ! stat(path, &s) )
106 ret = (s.st_mode & S_IFREG);
113 int madwifi_probe(const char *ifname)
115 return ( madwifi_isvap(ifname, NULL) || madwifi_iswifi(ifname) );
118 int madwifi_get_mode(const char *ifname, char *buf)
120 return wext_get_mode(ifname, buf);
123 int madwifi_get_ssid(const char *ifname, char *buf)
125 return wext_get_ssid(ifname, buf);
128 int madwifi_get_bssid(const char *ifname, char *buf)
130 return wext_get_bssid(ifname, buf);
133 int madwifi_get_channel(const char *ifname, int *buf)
138 struct ieee80211req_chaninfo chans;
140 if( madwifi_ioctl(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
142 /* Madwifi returns a Hz frequency, get it's freq list to find channel index */
143 freq = (uint16_t)(wrq.u.freq.m / 100000);
145 if( get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans)) >= 0 )
149 for( i = 0; i < chans.ic_nchans; i++ )
151 if( freq == chans.ic_chans[i].ic_freq )
153 *buf = chans.ic_chans[i].ic_ieee;
165 int madwifi_get_frequency(const char *ifname, int *buf)
169 if( madwifi_ioctl(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
171 *buf = (uint16_t)(wrq.u.freq.m / 100000);
178 int madwifi_get_txpower(const char *ifname, int *buf)
180 return wext_get_txpower(ifname, buf);
183 int madwifi_get_bitrate(const char *ifname, int *buf)
185 unsigned int mode, len, rate, rate_count;
186 uint8_t tmp[24*1024];
189 struct ieee80211req_sta_info *si;
191 if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
195 /* Calculate bitrate average from associated stations in ad-hoc mode */
198 rate = rate_count = 0;
200 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
205 si = (struct ieee80211req_sta_info *) cp;
207 if( si->isi_rssi > 0 )
210 rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
215 } while (len >= sizeof(struct ieee80211req_sta_info));
218 *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count) * 1000;
222 /* Return whatever wext tells us ... */
223 return wext_get_bitrate(ifname, buf);
229 int madwifi_get_signal(const char *ifname, int *buf)
231 unsigned int mode, len, rssi, rssi_count;
232 uint8_t tmp[24*1024];
235 struct ieee80211req_sta_info *si;
237 if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
241 /* Calculate signal average from associated stations in ap or ad-hoc mode */
244 rssi = rssi_count = 0;
246 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
251 si = (struct ieee80211req_sta_info *) cp;
253 if( si->isi_rssi > 0 )
256 rssi -= (si->isi_rssi - 95);
261 } while (len >= sizeof(struct ieee80211req_sta_info));
264 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
268 /* Return whatever wext tells us ... */
269 return wext_get_signal(ifname, buf);
275 int madwifi_get_noise(const char *ifname, int *buf)
277 return wext_get_noise(ifname, buf);
280 int madwifi_get_quality(const char *ifname, int *buf)
282 unsigned int mode, len, quality, quality_count;
283 uint8_t tmp[24*1024];
286 struct ieee80211req_sta_info *si;
288 if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
292 /* Calculate signal average from associated stations in ad-hoc mode */
295 quality = quality_count = 0;
297 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
302 si = (struct ieee80211req_sta_info *) cp;
304 if( si->isi_rssi > 0 )
307 quality += si->isi_rssi;
312 } while (len >= sizeof(struct ieee80211req_sta_info));
315 *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
319 /* Return whatever wext tells us ... */
320 return wext_get_quality(ifname, buf);
326 int madwifi_get_quality_max(const char *ifname, int *buf)
328 return wext_get_quality_max(ifname, buf);
331 int madwifi_get_encryption(const char *ifname, char *buf)
333 int ciphers = 0, key_type = 0, key_len = 0;
334 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
336 struct ieee80211req_key wk;
338 memset(&wrq, 0, sizeof(wrq));
339 memset(&wk, 0, sizeof(wk));
340 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
342 /* Get key information */
343 if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
345 key_type = wk.ik_type;
349 c->auth_algs = (IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED);
352 /* Get wpa protocol version */
353 wrq.u.mode = IEEE80211_PARAM_WPA;
354 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
355 c->wpa_version = wrq.u.mode;
357 /* Get authentication suites */
358 wrq.u.mode = IEEE80211_PARAM_AUTHMODE;
359 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
362 case IEEE80211_AUTH_8021X:
363 c->auth_suites |= IWINFO_KMGMT_8021x;
366 case IEEE80211_AUTH_WPA:
367 c->auth_suites |= IWINFO_KMGMT_PSK;
371 c->auth_suites |= IWINFO_KMGMT_NONE;
376 /* Get group key length */
377 wrq.u.mode = IEEE80211_PARAM_MCASTKEYLEN;
378 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
379 key_len = wrq.u.mode;
381 /* Get used pairwise ciphers */
382 wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
383 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
385 ciphers = wrq.u.mode;
387 if( ciphers & (1 << IEEE80211_CIPHER_TKIP) )
388 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
390 if( ciphers & (1 << IEEE80211_CIPHER_AES_CCM) )
391 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
393 if( ciphers & (1 << IEEE80211_CIPHER_AES_OCB) )
394 c->pair_ciphers |= IWINFO_CIPHER_AESOCB;
396 if( ciphers & (1 << IEEE80211_CIPHER_CKIP) )
397 c->pair_ciphers |= IWINFO_CIPHER_CKIP;
399 if( ciphers & (1 << IEEE80211_CIPHER_WEP) )
403 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
407 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
415 if( ciphers & (1 << IEEE80211_CIPHER_NONE) )
416 c->pair_ciphers |= IWINFO_CIPHER_NONE;
419 /* Get used group cipher */
420 wrq.u.mode = IEEE80211_PARAM_MCASTCIPHER;
421 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
423 ciphers = wrq.u.mode;
426 case IEEE80211_CIPHER_TKIP:
427 c->group_ciphers |= IWINFO_CIPHER_TKIP;
430 case IEEE80211_CIPHER_AES_CCM:
431 c->group_ciphers |= IWINFO_CIPHER_CCMP;
434 case IEEE80211_CIPHER_AES_OCB:
435 c->group_ciphers |= IWINFO_CIPHER_AESOCB;
438 case IEEE80211_CIPHER_CKIP:
439 c->group_ciphers |= IWINFO_CIPHER_CKIP;
442 case IEEE80211_CIPHER_WEP:
445 c->group_ciphers |= IWINFO_CIPHER_WEP104;
449 c->group_ciphers |= IWINFO_CIPHER_WEP40;
457 case IEEE80211_CIPHER_NONE:
458 c->group_ciphers |= IWINFO_CIPHER_NONE;
466 c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
471 int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
475 uint8_t tmp[24*1024];
476 struct ieee80211req_sta_info *si;
477 struct iwinfo_assoclist_entry entry;
479 if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
484 if( madwifi_get_noise(ifname, &noise) )
488 si = (struct ieee80211req_sta_info *) cp;
490 entry.signal = (si->isi_rssi - 95);
492 memcpy(entry.mac, &si->isi_macaddr, 6);
493 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
495 bl += sizeof(struct iwinfo_assoclist_entry);
498 } while (tl >= sizeof(struct ieee80211req_sta_info));
507 int madwifi_get_txpwrlist(const char *ifname, char *buf, int *len)
512 /* A wifiX device? */
513 if( madwifi_iswifi(ifname) )
515 sprintf(cmd, "wlanconfig ath-txpwr create nounit "
516 "wlandev %s wlanmode ap >/dev/null", ifname);
518 if( ! WEXITSTATUS(system(cmd)) )
520 rc = wext_get_txpwrlist("ath-txpwr", buf, len);
521 (void) WEXITSTATUS(system("wlanconfig ath-txpwr destroy"));
525 /* Its an athX ... */
526 else if( madwifi_isvap(ifname, NULL) )
528 rc = wext_get_txpwrlist(ifname, buf, len);
534 int madwifi_get_scanlist(const char *ifname, char *buf, int *len)
543 /* We got a wifiX device passed, try to lookup a vap on it */
544 if( madwifi_iswifi(ifname) )
546 if( (proc = opendir("/proc/sys/net/")) != NULL )
548 while( (e = readdir(proc)) != NULL )
550 if( madwifi_isvap(e->d_name, ifname) )
552 sprintf(cmd, "ifconfig %s up", e->d_name);
554 if( ! WEXITSTATUS(system(cmd)) )
556 ret = wext_get_scanlist(e->d_name, buf, len);
565 /* Still nothing found, try to create a vap */
568 sprintf(cmd, "wlanconfig ath-scan create nounit "
569 "wlandev %s wlanmode sta >/dev/null", ifname);
571 if( ! WEXITSTATUS(system(cmd)) && ! WEXITSTATUS(system("ifconfig ath-scan up")) )
573 ret = wext_get_scanlist("ath-scan", buf, len);
575 (void) WEXITSTATUS(system("ifconfig ath-scan down"));
576 (void) WEXITSTATUS(system("wlanconfig ath-scan destroy"));
581 /* Got athX device? */
582 else if( madwifi_isvap(ifname, NULL) )
584 ret = wext_get_scanlist(ifname, buf, len);
590 int madwifi_get_freqlist(const char *ifname, char *buf, int *len)
595 struct ieee80211req_chaninfo chans;
596 struct iwinfo_freqlist_entry entry;
598 /* A wifiX device? */
599 if( madwifi_iswifi(ifname) )
601 sprintf(cmd, "wlanconfig ath-channels create nounit "
602 "wlandev %s wlanmode ap >/dev/null", ifname);
604 if( ! WEXITSTATUS(system(cmd)) )
606 rc = get80211priv("ath-channels", IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans));
607 (void) WEXITSTATUS(system("wlanconfig ath-channels destroy"));
611 /* Its an athX ... */
612 else if( madwifi_isvap(ifname, NULL) )
614 rc = get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans));
623 for( i = 0; i < chans.ic_nchans; i++ )
625 entry.mhz = chans.ic_chans[i].ic_freq;
626 entry.channel = chans.ic_chans[i].ic_ieee;
628 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
629 bl += sizeof(struct iwinfo_freqlist_entry);
639 int madwifi_get_mbssid_support(const char *ifname, int *buf)
641 /* We assume that multi bssid is always possible */