libs: introduce iwinfo - wireless information abstraction for proprietary broadcom...
[project/luci.git] / libs / iwinfo / src / iwinfo_madwifi.c
1 /*
2  * iwinfo - Wireless Information Library - Madwifi Backend
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
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.
9  *
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.
14  *
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/.
17  *
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.
21  */
22
23 #include "iwinfo_madwifi.h"
24 #include "iwinfo_wext.h"
25
26 static int ioctl_socket = -1;
27
28 static int madwifi_ioctl(struct iwreq *wrq, const char *ifname, int cmd, void *data, size_t len)
29 {
30         /* prepare socket */
31         if( ioctl_socket == -1 )
32                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
33
34         strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
35
36         if( data != NULL )
37         {
38                 if( len < IFNAMSIZ )
39                 {
40                         memcpy(wrq->u.name, data, len);
41                 }
42                 else
43                 {
44                         wrq->u.data.pointer = data;
45                         wrq->u.data.length = len;
46                 }
47         }
48
49         return ioctl(ioctl_socket, cmd, wrq);
50 }
51
52 static int get80211priv(const char *ifname, int op, void *data, size_t len)
53 {
54         struct iwreq iwr;
55
56         if( madwifi_ioctl(&iwr, ifname, op, data, len) < 0 )
57                 return -1;
58
59         return iwr.u.data.length;
60 }
61
62
63 int madwifi_probe(const char *ifname)
64 {
65         int fd, ret;
66         char path[32];
67         char name[5];
68
69         sprintf(path, "/proc/sys/net/%s/%%parent", ifname);
70         ret = 0;
71
72         if( (fd = open(path, O_RDONLY)) > -1 )
73         {
74                 if( read(fd, name, 4) == 4 )
75                         ret = strncmp(name, "wifi", 4) ? 0 : 1;
76
77                 (void) close(fd);
78         }
79
80         return ret;
81 }
82
83 int madwifi_get_mode(const char *ifname, char *buf)
84 {
85         return wext_get_mode(ifname, buf);
86 }
87
88 int madwifi_get_ssid(const char *ifname, char *buf)
89 {
90         return wext_get_ssid(ifname, buf);
91 }
92
93 int madwifi_get_bssid(const char *ifname, char *buf)
94 {
95         return wext_get_bssid(ifname, buf);
96 }
97
98 int madwifi_get_channel(const char *ifname, int *buf)
99 {
100         int i;
101         uint16_t freq;
102         struct iwreq wrq;
103         struct ieee80211req_chaninfo chans;
104
105         if( madwifi_ioctl(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
106         {
107                 /* Madwifi returns a Hz frequency, get it's freq list to find channel index */
108                 freq = (uint16_t)(wrq.u.freq.m / 100000);
109
110                 if( get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans)) >= 0 )
111                 {
112                         *buf = 0;
113
114                         for( i = 0; i < chans.ic_nchans; i++ )
115                         {
116                                 if( freq == chans.ic_chans[i].ic_freq )
117                                 {
118                                         *buf = chans.ic_chans[i].ic_ieee;
119                                         break;
120                                 }
121                         }
122
123                         return 0;
124                 }
125         }
126
127         return -1;
128 }
129
130 int madwifi_get_bitrate(const char *ifname, int *buf)
131 {
132         unsigned int mode, len, rate, rate_count;
133         uint8_t tmp[24*1024];
134         uint8_t *cp;
135         struct iwreq wrq;
136         struct ieee80211req_sta_info *si;
137
138         if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
139         {
140                 mode = wrq.u.mode;
141
142                 /* Calculate bitrate average from associated stations in ad-hoc mode */
143                 if( mode == 1 )
144                 {
145                         rate = rate_count = 0;
146
147                         if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
148                         {
149                                 cp = tmp;
150
151                                 do {
152                                         si = (struct ieee80211req_sta_info *) cp;
153
154                                         if( si->isi_rssi > 0 )
155                                         {
156                                                 rate_count++;
157                                                 rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
158                                         }
159
160                                         cp   += si->isi_len;
161                                         len  -= si->isi_len;
162                                 } while (len >= sizeof(struct ieee80211req_sta_info));
163                         }
164
165                         *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count);
166                         return 0;
167                 }
168
169                 /* Return whatever wext tells us ... */
170                 return wext_get_bitrate(ifname, buf);
171         }
172
173         return -1;
174 }
175
176 int madwifi_get_signal(const char *ifname, int *buf)
177 {
178         unsigned int mode, len, rssi, rssi_count;
179         uint8_t tmp[24*1024];
180         uint8_t *cp;
181         struct iwreq wrq;
182         struct ieee80211req_sta_info *si;
183
184         if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
185         {
186                 mode = wrq.u.mode;
187
188                 /* Calculate signal average from associated stations in ap or ad-hoc mode */
189                 if( mode == 1 )
190                 {
191                         rssi = rssi_count = 0;
192
193                         if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
194                         {
195                                 cp = tmp;
196
197                                 do {
198                                         si = (struct ieee80211req_sta_info *) cp;
199
200                                         if( si->isi_rssi > 0 )
201                                         {
202                                                 rssi_count++;
203                                                 rssi -= (si->isi_rssi - 95);
204                                         }
205
206                                         cp   += si->isi_len;
207                                         len  -= si->isi_len;
208                                 } while (len >= sizeof(struct ieee80211req_sta_info));
209                         }
210
211                         *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
212                         return 0;
213                 }
214
215                 /* Return whatever wext tells us ... */
216                 return wext_get_signal(ifname, buf);
217         }
218
219         return -1;
220 }
221
222 int madwifi_get_noise(const char *ifname, int *buf)
223 {
224         return wext_get_noise(ifname, buf);
225 }
226
227 int madwifi_get_quality(const char *ifname, int *buf)
228 {
229         unsigned int mode, len, quality, quality_count;
230         uint8_t tmp[24*1024];
231         uint8_t *cp;
232         struct iwreq wrq;
233         struct ieee80211req_sta_info *si;
234
235         if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
236         {
237                 mode = wrq.u.mode;
238
239                 /* Calculate signal average from associated stations in ad-hoc mode */
240                 if( mode == 1 )
241                 {
242                         quality = quality_count = 0;
243
244                         if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
245                         {
246                                 cp = tmp;
247
248                                 do {
249                                         si = (struct ieee80211req_sta_info *) cp;
250
251                                         if( si->isi_rssi > 0 )
252                                         {
253                                                 quality_count++;
254                                                 quality += si->isi_rssi;
255                                         }
256
257                                         cp   += si->isi_len;
258                                         len  -= si->isi_len;
259                                 } while (len >= sizeof(struct ieee80211req_sta_info));
260                         }
261
262                         *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
263                         return 0;
264                 }
265
266                 /* Return whatever wext tells us ... */
267                 return wext_get_quality(ifname, buf);
268         }
269
270         return -1;
271 }
272
273 int madwifi_get_quality_max(const char *ifname, int *buf)
274 {
275         return wext_get_quality_max(ifname, buf);
276 }
277
278 int madwifi_get_enctype(const char *ifname, char *buf)
279 {
280         struct iwreq wrq;
281         struct ieee80211req_key wk;
282         int wpa_version = 0, ciphers = 0, key_type = 0;
283         char cipher_string[32];
284
285         sprintf(buf, "Unknown");
286
287         memset(&wrq, 0, sizeof(wrq));
288         memset(&wk, 0, sizeof(wk));
289         memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
290
291         /* Get key information */
292         if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
293                 key_type = wk.ik_type;
294
295         /* Get wpa protocol version */
296         wrq.u.mode = IEEE80211_PARAM_WPA;
297         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
298                 wpa_version = wrq.u.mode;
299
300         /* Get used pairwise ciphers */
301         wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
302         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
303         {
304                 ciphers = wrq.u.mode;
305
306                 if( wpa_version > 0 )
307                 {
308                         memset(cipher_string, 0, sizeof(cipher_string));
309
310                         /* Looks like mixed wpa/wpa2 ? */
311                         if( (ciphers & (1<<IEEE80211_CIPHER_TKIP)) && (ciphers & (1<<IEEE80211_CIPHER_AES_CCM)) )
312                                 wpa_version = 3;
313
314
315                         if( (ciphers & (1<<IEEE80211_CIPHER_TKIP)) )
316                                 strcat(cipher_string, "TKIP, ");
317
318                         if( (ciphers & (1<<IEEE80211_CIPHER_AES_CCM)) )
319                                 strcat(cipher_string, "CCMP, ");
320
321                         if( (ciphers & (1<<IEEE80211_CIPHER_AES_OCB)) )
322                                 strcat(cipher_string, "AES-OCB, ");
323
324                         if( (ciphers & (1<<IEEE80211_CIPHER_CKIP)) )
325                                 strcat(cipher_string, "CKIP, ");
326
327                         cipher_string[strlen(cipher_string)-2] = 0;
328                 }
329
330                 switch(wpa_version)
331                 {
332                         case 3:
333                                 sprintf(buf, "mixed WPA/WPA2 (%s)", cipher_string);
334                                 break;
335
336                         case 2:
337                                 sprintf(buf, "WPA2 (%s)", cipher_string);
338                                 break;
339
340                         case 1:
341                                 sprintf(buf, "WPA (%s)", cipher_string);
342                                 break;
343                         
344                         default:
345                                 sprintf(buf, "%s", (key_type == 0) ? "WEP" : "None");
346                 }
347         }
348
349         return 0;
350 }
351
352 int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
353 {
354         int bl, tl, noise;
355         uint8_t *cp;
356         uint8_t tmp[24*1024];
357         struct ieee80211req_sta_info *si;
358         struct iwinfo_assoclist_entry entry;
359
360         if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
361         {
362                 cp = tmp;
363                 bl = 0;
364
365                 if( madwifi_get_noise(ifname, &noise) )
366                         noise = 0;
367
368                 do {
369                         si = (struct ieee80211req_sta_info *) cp;
370
371                         entry.signal = (si->isi_rssi - 95);
372                         entry.noise  = noise;
373                         memcpy(entry.mac, &si->isi_macaddr, 6);
374                         memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
375
376                         bl += sizeof(struct iwinfo_assoclist_entry);
377                         cp += si->isi_len;
378                         tl -= si->isi_len;
379                 } while (tl >= sizeof(struct ieee80211req_sta_info));
380
381                 *len = bl;
382                 return 0;
383         }
384
385         return -1;
386 }
387
388