libs/iwinfo: return bitrate in kilobits to properly handle .5 rates
[project/luci.git] / libs / iwinfo / src / iwinfo_wl.c
1 /*
2  * iwinfo - Wireless Information Library - Broadcom wl.o 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  * This code is based on the wlc.c utility published by OpenWrt.org . 
19  */
20
21 #include "iwinfo_wl.h"
22 #include "iwinfo_wext.h"
23
24 static int ioctl_socket = -1;
25
26 static int wl_ioctl(const char *name, int cmd, void *buf, int len)
27 {
28         struct ifreq ifr;
29         wl_ioctl_t ioc;
30
31         /* prepare socket */
32         if( ioctl_socket == -1 )
33                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
34
35         /* do it */
36         ioc.cmd = cmd;
37         ioc.buf = buf;
38         ioc.len = len;
39         strncpy(ifr.ifr_name, name, IFNAMSIZ);
40         ifr.ifr_data = (caddr_t) &ioc;
41
42         return ioctl(ioctl_socket, SIOCDEVPRIVATE, &ifr);
43 }
44
45 static struct wl_maclist * wl_read_assoclist(const char *ifname)
46 {
47         struct wl_maclist *macs;
48         int maclen = 4 + WL_MAX_STA_COUNT * 6;
49
50         if( (macs = (struct wl_maclist *) malloc(maclen)) != NULL )
51         {
52                 memset(macs, 0, maclen);
53                 macs->count = WL_MAX_STA_COUNT;
54
55                 if( !wl_ioctl(ifname, WLC_GET_ASSOCLIST, macs, maclen) )
56                         return macs;
57
58                 free(macs);
59         }
60
61         return NULL;
62 }
63
64
65 int wl_probe(const char *ifname)
66 {
67         int magic;
68
69         if( !wl_ioctl(ifname, WLC_GET_MAGIC, &magic, sizeof(magic)) && (magic == WLC_IOCTL_MAGIC))
70                 return 1;
71
72         return 0;
73 }
74
75 int wl_get_mode(const char *ifname, char *buf)
76 {
77         int ret = -1;
78         int ap, infra, passive;
79
80         if( (ret = wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap))) )
81                 return ret;
82
83         if( (ret = wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra))) )
84                 return ret;
85
86         if( (ret = wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive))) )
87                 return ret;
88
89         if( passive )
90                 sprintf(buf, "Monitor");
91         else if( !infra )
92                 sprintf(buf, "Ad-Hoc");
93         else if( ap )
94                 sprintf(buf, "Master");
95         else
96                 sprintf(buf, "Client");
97
98         return 0;
99 }
100
101 int wl_get_ssid(const char *ifname, char *buf)
102 {
103         int ret = -1;
104         wlc_ssid_t ssid;
105
106         if( !(ret = wl_ioctl(ifname, WLC_GET_SSID, &ssid, sizeof(ssid))) )
107                 memcpy(buf, ssid.ssid, ssid.ssid_len);
108
109         return ret;
110 }
111
112 int wl_get_bssid(const char *ifname, char *buf)
113 {
114         int ret = -1;
115         char bssid[6];
116
117         if( !(ret = wl_ioctl(ifname, WLC_GET_BSSID, bssid, 6)) )
118                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
119                         (uint8_t)bssid[0], (uint8_t)bssid[1], (uint8_t)bssid[2],
120                         (uint8_t)bssid[3], (uint8_t)bssid[4], (uint8_t)bssid[5]
121                 );
122
123         return ret;
124 }
125
126 int wl_get_channel(const char *ifname, int *buf)
127 {
128         return wl_ioctl(ifname, WLC_GET_CHANNEL, buf, sizeof(buf));
129 }
130
131 int wl_get_frequency(const char *ifname, int *buf)
132 {
133         return wext_get_frequency(ifname, buf);
134 }
135
136 int wl_get_bitrate(const char *ifname, int *buf)
137 {
138         int ret = -1;
139         int rate = 0;
140
141         if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
142                 *buf = ((rate / 2) * 1000) + ((rate & 1) ? 500 : 0);
143
144         return ret;
145 }
146
147 int wl_get_signal(const char *ifname, int *buf)
148 {
149         unsigned int ap, rssi, i, rssi_count;
150         int ioctl_req_version = 0x2000;
151         char tmp[WLC_IOCTL_MAXLEN];
152         struct wl_maclist *macs = NULL;
153         wl_sta_rssi_t starssi;
154
155         memset(tmp, 0, WLC_IOCTL_MAXLEN);
156         memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
157
158         wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
159
160         if( !wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap )
161         {
162                 *buf = tmp[WL_BSS_RSSI_OFFSET];
163         }
164         else
165         {
166                 rssi = rssi_count = 0;
167
168                 /* Calculate average rssi from conntected stations */
169                 if( (macs = wl_read_assoclist(ifname)) != NULL )
170                 {
171                         for( i = 0; i < macs->count; i++ )
172                         {
173                                 memcpy(starssi.mac, &macs->ea[i], 6);
174
175                                 if( !wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12) )
176                                 {
177                                         rssi -= starssi.rssi;
178                                         rssi_count++;
179                                 }
180                         }
181
182                         free(macs);
183                 }
184
185                 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
186         }
187
188         return 0;
189 }
190
191 int wl_get_noise(const char *ifname, int *buf)
192 {
193         unsigned int ap, noise;
194         int ioctl_req_version = 0x2000;
195         char tmp[WLC_IOCTL_MAXLEN];
196
197         memset(tmp, 0, WLC_IOCTL_MAXLEN);
198         memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
199
200         wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
201
202         if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
203         {
204                 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
205                         noise = 0;
206         }
207         else
208         {
209                 noise = tmp[WL_BSS_NOISE_OFFSET];
210         }
211
212         *buf = noise;
213
214         return 0;
215 }
216
217 int wl_get_quality(const char *ifname, int *buf)
218 {
219         return wext_get_quality(ifname, buf);
220 }
221
222 int wl_get_quality_max(const char *ifname, int *buf)
223 {
224         return wext_get_quality_max(ifname, buf);
225 }
226
227 int wl_get_enctype(const char *ifname, char *buf)
228 {
229         uint32_t wsec, wpa;
230         char algo[9];
231
232         if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
233             wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
234                         return -1;
235
236         switch(wsec)
237         {
238                 case 2:
239                         sprintf(algo, "TKIP");
240                         break;
241
242                 case 4:
243                         sprintf(algo, "CCMP");
244                         break;
245
246                 case 6:
247                         sprintf(algo, "TKIP, CCMP");
248                         break;
249         }
250
251         switch(wpa)
252         {
253                 case 0:
254                         sprintf(buf, "%s", wsec ? "WEP" : "None");
255                         break;
256
257                 case 2:
258                         sprintf(buf, "WPA 802.1X (%s)", algo);
259                         break;
260
261                 case 4:
262                         sprintf(buf, "WPA PSK (%s)", algo);
263                         break;
264
265                 case 32:
266                         sprintf(buf, "802.1X (%s)", algo);
267                         break;
268
269                 case 64:
270                         sprintf(buf, "WPA2 802.1X (%s)", algo);
271                         break;
272
273                 case 66:
274                         sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
275                         break;
276
277                 case 128:
278                         sprintf(buf, "WPA2 PSK (%s)", algo);
279                         break;
280
281                 case 132:
282                         sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
283                         break;
284
285                 default:
286                         sprintf(buf, "Unkown");
287         }
288
289         return 0;
290 }
291
292 int wl_get_assoclist(const char *ifname, char *buf, int *len)
293 {
294         int i, j, noise;
295         int ap, infra, passive;
296         char line[128];
297         char macstr[18];
298         char devstr[IFNAMSIZ];
299         struct wl_maclist *macs;
300         struct wl_sta_rssi rssi;
301         struct iwinfo_assoclist_entry entry;
302         FILE *arp;
303
304         ap = infra = passive = 0;
305
306         wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
307         wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
308         wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
309
310         if( wl_get_noise(ifname, &noise) )
311                 noise = 0;
312
313         if( (ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL) )
314         {
315                 for( i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry) )
316                 {
317                         memcpy(rssi.mac, &macs->ea[i], 6);
318
319                         if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
320                                 entry.signal = (rssi.rssi - 0x100);
321                         else
322                                 entry.signal = 0;
323
324                         entry.noise = noise;
325                         memcpy(entry.mac, &macs->ea[i], 6);
326                         memcpy(&buf[j], &entry, sizeof(entry));
327                 }
328
329                 *len = j;
330                 free(macs);
331                 return 0;
332         }
333         else if( (arp = fopen("/proc/net/arp", "r")) != NULL )
334         {
335                 j = 0;
336
337                 while( fgets(line, sizeof(line), arp) != NULL )
338                 {
339                         if( sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname) )
340                         {
341                                 rssi.mac[0] = strtol(&macstr[0],  NULL, 16);
342                                 rssi.mac[1] = strtol(&macstr[3],  NULL, 16);
343                                 rssi.mac[2] = strtol(&macstr[6],  NULL, 16);
344                                 rssi.mac[3] = strtol(&macstr[9],  NULL, 16);
345                                 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
346                                 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
347
348                                 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
349                                         entry.signal = (rssi.rssi - 0x100);
350                                 else
351                                         entry.signal = 0;
352
353                                 entry.noise = noise;
354                                 memcpy(entry.mac, rssi.mac, 6);
355                                 memcpy(&buf[j], &entry, sizeof(entry));
356
357                                 j += sizeof(entry);
358                         }
359                 }
360
361                 *len = j;
362                 (void) fclose(arp);
363                 return 0;
364         }
365
366         return -1;
367 }
368
369 int wl_get_txpwrlist(const char *ifname, char *buf, int *len)
370 {
371         struct iwinfo_txpwrlist_entry entry;
372         uint8_t dbm[8] = { 0, 6, 8, 10, 12, 14, 16, 18 };
373         uint8_t mw[8]  = { 1, 3, 6, 10, 15, 25, 39, 63 };
374         int i;
375
376         for( i = 0; i < 8; i++ )
377         {
378                 entry.dbm = dbm[i];
379                 entry.mw  = mw[i];
380                 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
381         }
382
383         *len = 8 * sizeof(entry);
384         return 0;
385 }
386
387 int wl_get_scanlist(const char *ifname, char *buf, int *len)
388 {
389         return wext_get_scanlist(ifname, buf, len);
390 }
391
392 int wl_get_mbssid_support(const char *ifname, int *buf)
393 {
394         wlc_rev_info_t revinfo;
395
396         /* Multi bssid support only works on corerev >= 9 */
397         if( !wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)) )
398         {
399                 if( revinfo.corerev >= 9 )
400                 {
401                         *buf = 1;
402                         return 0;
403                 }
404         }
405
406         return -1;
407 }
408