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_bitrate(const char *ifname, int *buf)
180 unsigned int mode, len, rate, rate_count;
181 uint8_t tmp[24*1024];
184 struct ieee80211req_sta_info *si;
186 if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
190 /* Calculate bitrate average from associated stations in ad-hoc mode */
193 rate = rate_count = 0;
195 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
200 si = (struct ieee80211req_sta_info *) cp;
202 if( si->isi_rssi > 0 )
205 rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
210 } while (len >= sizeof(struct ieee80211req_sta_info));
213 *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count) * 1000;
217 /* Return whatever wext tells us ... */
218 return wext_get_bitrate(ifname, buf);
224 int madwifi_get_signal(const char *ifname, int *buf)
226 unsigned int mode, len, rssi, rssi_count;
227 uint8_t tmp[24*1024];
230 struct ieee80211req_sta_info *si;
232 if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
236 /* Calculate signal average from associated stations in ap or ad-hoc mode */
239 rssi = rssi_count = 0;
241 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
246 si = (struct ieee80211req_sta_info *) cp;
248 if( si->isi_rssi > 0 )
251 rssi -= (si->isi_rssi - 95);
256 } while (len >= sizeof(struct ieee80211req_sta_info));
259 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
263 /* Return whatever wext tells us ... */
264 return wext_get_signal(ifname, buf);
270 int madwifi_get_noise(const char *ifname, int *buf)
272 return wext_get_noise(ifname, buf);
275 int madwifi_get_quality(const char *ifname, int *buf)
277 unsigned int mode, len, quality, quality_count;
278 uint8_t tmp[24*1024];
281 struct ieee80211req_sta_info *si;
283 if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
287 /* Calculate signal average from associated stations in ad-hoc mode */
290 quality = quality_count = 0;
292 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
297 si = (struct ieee80211req_sta_info *) cp;
299 if( si->isi_rssi > 0 )
302 quality += si->isi_rssi;
307 } while (len >= sizeof(struct ieee80211req_sta_info));
310 *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
314 /* Return whatever wext tells us ... */
315 return wext_get_quality(ifname, buf);
321 int madwifi_get_quality_max(const char *ifname, int *buf)
323 return wext_get_quality_max(ifname, buf);
326 int madwifi_get_enctype(const char *ifname, char *buf)
329 struct ieee80211req_key wk;
330 int wpa_version = 0, ciphers = 0, key_type = 0;
331 char cipher_string[32];
333 sprintf(buf, "Unknown");
335 memset(&wrq, 0, sizeof(wrq));
336 memset(&wk, 0, sizeof(wk));
337 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
339 /* Get key information */
340 if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
341 key_type = wk.ik_type;
343 /* Get wpa protocol version */
344 wrq.u.mode = IEEE80211_PARAM_WPA;
345 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
346 wpa_version = wrq.u.mode;
348 /* Get used pairwise ciphers */
349 wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
350 if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
352 ciphers = wrq.u.mode;
354 if( wpa_version > 0 )
356 memset(cipher_string, 0, sizeof(cipher_string));
358 /* Looks like mixed wpa/wpa2 ? */
359 if( (ciphers & (1<<IEEE80211_CIPHER_TKIP)) && (ciphers & (1<<IEEE80211_CIPHER_AES_CCM)) )
363 if( (ciphers & (1<<IEEE80211_CIPHER_TKIP)) )
364 strcat(cipher_string, "TKIP, ");
366 if( (ciphers & (1<<IEEE80211_CIPHER_AES_CCM)) )
367 strcat(cipher_string, "CCMP, ");
369 if( (ciphers & (1<<IEEE80211_CIPHER_AES_OCB)) )
370 strcat(cipher_string, "AES-OCB, ");
372 if( (ciphers & (1<<IEEE80211_CIPHER_CKIP)) )
373 strcat(cipher_string, "CKIP, ");
375 cipher_string[strlen(cipher_string)-2] = 0;
381 sprintf(buf, "mixed WPA/WPA2 (%s)", cipher_string);
385 sprintf(buf, "WPA2 (%s)", cipher_string);
389 sprintf(buf, "WPA (%s)", cipher_string);
393 sprintf(buf, "%s", (key_type == 0) ? "WEP" : "None");
400 int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
404 uint8_t tmp[24*1024];
405 struct ieee80211req_sta_info *si;
406 struct iwinfo_assoclist_entry entry;
408 if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
413 if( madwifi_get_noise(ifname, &noise) )
417 si = (struct ieee80211req_sta_info *) cp;
419 entry.signal = (si->isi_rssi - 95);
421 memcpy(entry.mac, &si->isi_macaddr, 6);
422 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
424 bl += sizeof(struct iwinfo_assoclist_entry);
427 } while (tl >= sizeof(struct ieee80211req_sta_info));
436 int madwifi_get_txpwrlist(const char *ifname, char *buf, int *len)
438 return wext_get_txpwrlist(ifname, buf, len);
441 int madwifi_get_scanlist(const char *ifname, char *buf, int *len)
450 /* We got a wifiX device passed, try to lookup a vap on it */
451 if( madwifi_iswifi(ifname) )
453 if( (proc = opendir("/proc/sys/net/")) != NULL )
455 while( (e = readdir(proc)) != NULL )
457 if( madwifi_isvap(e->d_name, ifname) )
459 sprintf(cmd, "ifconfig %s up", e->d_name);
461 if( ! WEXITSTATUS(system(cmd)) )
463 ret = wext_get_scanlist(e->d_name, buf, len);
472 /* Still nothing found, try to create a vap */
475 sprintf(cmd, "wlanconfig ath-scan create nounit "
476 "wlandev %s wlanmode sta >/dev/null", ifname);
478 if( ! WEXITSTATUS(system(cmd)) && ! WEXITSTATUS(system("ifconfig ath-scan up")) )
480 ret = wext_get_scanlist("ath-scan", buf, len);
482 (void) WEXITSTATUS(system("ifconfig ath-scan down"));
483 (void) WEXITSTATUS(system("wlanconfig ath-scan destroy"));
488 /* Got athX device? */
489 else if( madwifi_isvap(ifname, NULL) )
491 ret = wext_get_scanlist(ifname, buf, len);
497 int madwifi_get_freqlist(const char *ifname, char *buf, int *len)
500 struct ieee80211req_chaninfo chans;
501 struct iwinfo_freqlist_entry entry;
503 if( get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans)) >= 0 )
507 for( i = 0; i < chans.ic_nchans; i++ )
509 entry.mhz = (int)(chans.ic_chans[i].ic_freq / 1000);
510 entry.channel = chans.ic_chans[i].ic_ieee;
512 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
513 bl += sizeof(struct iwinfo_freqlist_entry);
523 int madwifi_get_mbssid_support(const char *ifname, int *buf)
525 /* We assume that multi bssid is always possible */