37f4de14aceb1f52af26e5f625707bc21138ee44
[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_bitrate(const char *ifname, int *buf)
132 {
133         int ret = -1;
134         int rate = 0;
135
136         if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
137                 *buf = rate / 2;
138
139         return ret;
140 }
141
142 int wl_get_signal(const char *ifname, int *buf)
143 {
144         unsigned int ap, rssi, i, rssi_count;
145         int ioctl_req_version = 0x2000;
146         char tmp[WLC_IOCTL_MAXLEN];
147         struct wl_maclist *macs = NULL;
148         wl_sta_rssi_t starssi;
149
150         memset(tmp, 0, WLC_IOCTL_MAXLEN);
151         memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
152
153         wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
154
155         rssi = 0;
156         rssi_count = 0;
157
158         if( !wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap )
159         {
160                 rssi = tmp[WL_BSS_RSSI_OFFSET];
161                 rssi_count = 1;
162         }
163         else
164         {
165                 /* Calculate average rssi from conntected stations */
166                 if( (macs = wl_read_assoclist(ifname)) != NULL )
167                 {
168                         for( i = 0; i < macs->count; i++ )
169                         {
170                                 memcpy(starssi.mac, &macs->ea[i], 6);
171
172                                 if( !wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12) )
173                                 {
174                                         rssi += starssi.rssi;
175                                         rssi_count++;
176                                 }
177                         }
178
179                         free(macs);
180                 }
181         }
182
183         *buf = (rssi == 0 || rssi_count == 0) ? 1 : (rssi / rssi_count);
184
185         return 0;
186 }
187
188 int wl_get_noise(const char *ifname, int *buf)
189 {
190         unsigned int ap, noise;
191         int ioctl_req_version = 0x2000;
192         char tmp[WLC_IOCTL_MAXLEN];
193
194         memset(tmp, 0, WLC_IOCTL_MAXLEN);
195         memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
196
197         wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
198
199         if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
200         {
201                 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
202                         noise = 0;
203         }
204         else
205         {
206                 noise = tmp[WL_BSS_NOISE_OFFSET];
207         }
208
209         *buf = noise;
210
211         return 0;
212 }
213
214 int wl_get_quality(const char *ifname, int *buf)
215 {
216         return wext_get_quality(ifname, buf);
217 }
218
219 int wl_get_quality_max(const char *ifname, int *buf)
220 {
221         return wext_get_quality_max(ifname, buf);
222 }
223
224 int wl_get_enctype(const char *ifname, char *buf)
225 {
226         uint32_t wsec, wpa;
227         char algo[9];
228
229         if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
230             wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
231                         return -1;
232
233         switch(wsec)
234         {
235                 case 2:
236                         sprintf(algo, "TKIP");
237                         break;
238
239                 case 4:
240                         sprintf(algo, "CCMP");
241                         break;
242
243                 case 6:
244                         sprintf(algo, "TKIP, CCMP");
245                         break;
246         }
247
248         switch(wpa)
249         {
250                 case 0:
251                         sprintf(buf, "%s", wsec ? "WEP" : "None");
252                         break;
253
254                 case 2:
255                         sprintf(buf, "WPA 802.1X (%s)", algo);
256                         break;
257
258                 case 4:
259                         sprintf(buf, "WPA PSK (%s)", algo);
260                         break;
261
262                 case 32:
263                         sprintf(buf, "802.1X (%s)", algo);
264                         break;
265
266                 case 64:
267                         sprintf(buf, "WPA2 802.1X (%s)", algo);
268                         break;
269
270                 case 66:
271                         sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
272                         break;
273
274                 case 128:
275                         sprintf(buf, "WPA2 PSK (%s)", algo);
276                         break;
277
278                 case 132:
279                         sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
280                         break;
281
282                 default:
283                         sprintf(buf, "Unkown");
284         }
285
286         return 0;
287 }
288
289 int wl_get_assoclist(const char *ifname, char *buf, int *len)
290 {
291         int i, j, noise;
292         int ap, infra, passive;
293         char line[128];
294         char macstr[18];
295         char devstr[IFNAMSIZ];
296         struct wl_maclist *macs;
297         struct wl_sta_rssi rssi;
298         struct iwinfo_assoclist_entry entry;
299         FILE *arp;
300
301         ap = infra = passive = 0;
302
303         wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
304         wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
305         wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
306
307         if( wl_get_noise(ifname, &noise) )
308                 noise = 0;
309
310         if( (ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL) )
311         {
312                 for( i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry) )
313                 {
314                         memcpy(rssi.mac, &macs->ea[i], 6);
315
316                         if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
317                                 entry.signal = (rssi.rssi - 0x100);
318                         else
319                                 entry.signal = 0;
320
321                         entry.noise = noise;
322                         memcpy(entry.mac, &macs->ea[i], 6);
323                         memcpy(&buf[j], &entry, sizeof(entry));
324                 }
325
326                 *len = j;
327                 free(macs);
328                 return 0;
329         }
330         else if( (arp = fopen("/proc/net/arp", "r")) != NULL )
331         {
332                 j = 0;
333
334                 while( fgets(line, sizeof(line), arp) != NULL )
335                 {
336                         if( sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname) )
337                         {
338                                 rssi.mac[0] = strtol(&macstr[0],  NULL, 16);
339                                 rssi.mac[1] = strtol(&macstr[3],  NULL, 16);
340                                 rssi.mac[2] = strtol(&macstr[6],  NULL, 16);
341                                 rssi.mac[3] = strtol(&macstr[9],  NULL, 16);
342                                 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
343                                 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
344
345                                 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
346                                         entry.signal = (rssi.rssi - 0x100);
347                                 else
348                                         entry.signal = 0;
349
350                                 entry.noise = noise;
351                                 memcpy(entry.mac, rssi.mac, 6);
352                                 memcpy(&buf[j], &entry, sizeof(entry));
353
354                                 j += sizeof(entry);
355                         }
356                 }
357
358                 *len = j;
359                 (void) fclose(arp);
360                 return 0;
361         }
362
363         return -1;
364 }
365