81df81faf4a5488456b45250096dd6caa0bf79e3
[project/luci.git] / contrib / package / 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         {
34                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
35                 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
36         }
37
38         /* do it */
39         ioc.cmd = cmd;
40         ioc.buf = buf;
41         ioc.len = len;
42         strncpy(ifr.ifr_name, name, IFNAMSIZ);
43         ifr.ifr_data = (caddr_t) &ioc;
44
45         return ioctl(ioctl_socket, SIOCDEVPRIVATE, &ifr);
46 }
47
48 static struct wl_maclist * wl_read_assoclist(const char *ifname)
49 {
50         struct wl_maclist *macs;
51         int maclen = 4 + WL_MAX_STA_COUNT * 6;
52
53         if( (macs = (struct wl_maclist *) malloc(maclen)) != NULL )
54         {
55                 memset(macs, 0, maclen);
56                 macs->count = WL_MAX_STA_COUNT;
57
58                 if( !wl_ioctl(ifname, WLC_GET_ASSOCLIST, macs, maclen) )
59                         return macs;
60
61                 free(macs);
62         }
63
64         return NULL;
65 }
66
67
68 int wl_probe(const char *ifname)
69 {
70         int magic;
71
72         if( !wl_ioctl(ifname, WLC_GET_MAGIC, &magic, sizeof(magic)) && (magic == WLC_IOCTL_MAGIC))
73                 return 1;
74
75         return 0;
76 }
77
78 int wl_get_mode(const char *ifname, char *buf)
79 {
80         int ret = -1;
81         int ap, infra, passive;
82
83         if( (ret = wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap))) )
84                 return ret;
85
86         if( (ret = wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra))) )
87                 return ret;
88
89         if( (ret = wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive))) )
90                 return ret;
91
92         if( passive )
93                 sprintf(buf, "Monitor");
94         else if( !infra )
95                 sprintf(buf, "Ad-Hoc");
96         else if( ap )
97                 sprintf(buf, "Master");
98         else
99                 sprintf(buf, "Client");
100
101         return 0;
102 }
103
104 int wl_get_ssid(const char *ifname, char *buf)
105 {
106         int ret = -1;
107         wlc_ssid_t ssid;
108
109         if( !(ret = wl_ioctl(ifname, WLC_GET_SSID, &ssid, sizeof(ssid))) )
110                 memcpy(buf, ssid.ssid, ssid.ssid_len);
111
112         return ret;
113 }
114
115 int wl_get_bssid(const char *ifname, char *buf)
116 {
117         int ret = -1;
118         char bssid[6];
119
120         if( !(ret = wl_ioctl(ifname, WLC_GET_BSSID, bssid, 6)) )
121                 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
122                         (uint8_t)bssid[0], (uint8_t)bssid[1], (uint8_t)bssid[2],
123                         (uint8_t)bssid[3], (uint8_t)bssid[4], (uint8_t)bssid[5]
124                 );
125
126         return ret;
127 }
128
129 int wl_get_channel(const char *ifname, int *buf)
130 {
131         return wl_ioctl(ifname, WLC_GET_CHANNEL, buf, sizeof(buf));
132 }
133
134 int wl_get_frequency(const char *ifname, int *buf)
135 {
136         return wext_get_frequency(ifname, buf);
137 }
138
139 int wl_get_txpower(const char *ifname, int *buf)
140 {
141         return wext_get_txpower(ifname, buf);
142 }
143
144 int wl_get_bitrate(const char *ifname, int *buf)
145 {
146         int ret = -1;
147         int rate = 0;
148
149         if( !(ret = wl_ioctl(ifname, WLC_GET_RATE, &rate, sizeof(rate))) && (rate > 0))
150                 *buf = ((rate / 2) * 1000) + ((rate & 1) ? 500 : 0);
151
152         return ret;
153 }
154
155 int wl_get_signal(const char *ifname, int *buf)
156 {
157         unsigned int ap, rssi, i, rssi_count;
158         int ioctl_req_version = 0x2000;
159         char tmp[WLC_IOCTL_MAXLEN];
160         struct wl_maclist *macs = NULL;
161         wl_sta_rssi_t starssi;
162
163         memset(tmp, 0, WLC_IOCTL_MAXLEN);
164         memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
165
166         wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
167
168         if( !wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) && !ap )
169         {
170                 *buf = tmp[WL_BSS_RSSI_OFFSET];
171         }
172         else
173         {
174                 rssi = rssi_count = 0;
175
176                 /* Calculate average rssi from conntected stations */
177                 if( (macs = wl_read_assoclist(ifname)) != NULL )
178                 {
179                         for( i = 0; i < macs->count; i++ )
180                         {
181                                 memcpy(starssi.mac, &macs->ea[i], 6);
182
183                                 if( !wl_ioctl(ifname, WLC_GET_RSSI, &starssi, 12) )
184                                 {
185                                         rssi -= starssi.rssi;
186                                         rssi_count++;
187                                 }
188                         }
189
190                         free(macs);
191                 }
192
193                 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
194         }
195
196         return 0;
197 }
198
199 int wl_get_noise(const char *ifname, int *buf)
200 {
201         unsigned int ap, noise;
202         int ioctl_req_version = 0x2000;
203         char tmp[WLC_IOCTL_MAXLEN];
204
205         memset(tmp, 0, WLC_IOCTL_MAXLEN);
206         memcpy(tmp, &ioctl_req_version, sizeof(ioctl_req_version));
207
208         wl_ioctl(ifname, WLC_GET_BSS_INFO, tmp, WLC_IOCTL_MAXLEN);
209
210         if ((wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap)) < 0) || ap)
211         {
212                 if (wl_ioctl(ifname, WLC_GET_PHY_NOISE, &noise, sizeof(noise)) < 0)
213                         noise = 0;
214         }
215         else
216         {
217                 noise = tmp[WL_BSS_NOISE_OFFSET];
218         }
219
220         *buf = noise;
221
222         return 0;
223 }
224
225 int wl_get_quality(const char *ifname, int *buf)
226 {
227         return wext_get_quality(ifname, buf);
228 }
229
230 int wl_get_quality_max(const char *ifname, int *buf)
231 {
232         return wext_get_quality_max(ifname, buf);
233 }
234
235 int wl_get_encryption(const char *ifname, char *buf)
236 {
237         uint32_t wsec, wauth, wpa;
238         struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
239
240         if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa,   sizeof(uint32_t)) ||
241             wl_ioctl(ifname, WLC_GET_WSEC,     &wsec,  sizeof(uint32_t)) ||
242                 wl_ioctl(ifname, WLC_GET_AUTH,     &wauth, sizeof(uint32_t)) )
243                         return -1;
244
245         switch(wsec)
246         {
247                 case 2:
248                         c->pair_ciphers |= IWINFO_CIPHER_TKIP;
249                         break;
250
251                 case 4:
252                         c->pair_ciphers |= IWINFO_CIPHER_CCMP;
253                         break;
254
255                 case 6:
256                         c->pair_ciphers |= IWINFO_CIPHER_TKIP;
257                         c->pair_ciphers |= IWINFO_CIPHER_CCMP;
258                         break;
259         }
260
261         switch(wpa)
262         {
263                 case 0:
264                         if( wsec && !wauth )
265                                 c->auth_algs |= IWINFO_AUTH_OPEN;
266
267                         else if( wsec && wauth )
268                                 c->auth_algs |= IWINFO_AUTH_SHARED;
269
270                         /* ToDo: evaluate WEP key lengths */
271                         c->pair_ciphers = IWINFO_CIPHER_WEP40 | IWINFO_CIPHER_WEP104;
272                         c->auth_suites |= IWINFO_KMGMT_NONE;
273                         break;
274
275                 case 2:
276                         c->wpa_version = 1;
277                         c->auth_suites |= IWINFO_KMGMT_8021x;
278                         break;
279
280                 case 4:
281                         c->wpa_version = 1;
282                         c->auth_suites |= IWINFO_KMGMT_PSK;
283                         break;
284
285                 case 32:
286                 case 64:
287                         c->wpa_version = 2;
288                         c->auth_suites |= IWINFO_KMGMT_8021x;
289                         break;
290
291                 case 66:
292                         c->wpa_version = 3;
293                         c->auth_suites |= IWINFO_KMGMT_8021x;
294                         break;
295
296                 case 128:
297                         c->wpa_version = 2;
298                         c->auth_suites |= IWINFO_KMGMT_PSK;
299                         break;
300
301                 case 132:
302                         c->wpa_version = 3;
303                         c->auth_suites |= IWINFO_KMGMT_PSK;
304                         break;
305
306                 default:
307                         break;
308         }
309
310         c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
311         c->group_ciphers = c->pair_ciphers;
312
313         return 0;
314 }
315
316 int wl_get_enctype(const char *ifname, char *buf)
317 {
318         uint32_t wsec, wpa;
319         char algo[11];
320
321         if( wl_ioctl(ifname, WLC_GET_WPA_AUTH, &wpa, sizeof(uint32_t)) ||
322             wl_ioctl(ifname, WLC_GET_WSEC, &wsec, sizeof(uint32_t)) )
323                         return -1;
324
325         switch(wsec)
326         {
327                 case 2:
328                         sprintf(algo, "TKIP");
329                         break;
330
331                 case 4:
332                         sprintf(algo, "CCMP");
333                         break;
334
335                 case 6:
336                         sprintf(algo, "TKIP, CCMP");
337                         break;
338         }
339
340         switch(wpa)
341         {
342                 case 0:
343                         sprintf(buf, "%s", wsec ? "WEP" : "None");
344                         break;
345
346                 case 2:
347                         sprintf(buf, "WPA 802.1X (%s)", algo);
348                         break;
349
350                 case 4:
351                         sprintf(buf, "WPA PSK (%s)", algo);
352                         break;
353
354                 case 32:
355                         sprintf(buf, "802.1X (%s)", algo);
356                         break;
357
358                 case 64:
359                         sprintf(buf, "WPA2 802.1X (%s)", algo);
360                         break;
361
362                 case 66:
363                         sprintf(buf, "mixed WPA/WPA2 802.1X (%s)", algo);
364                         break;
365
366                 case 128:
367                         sprintf(buf, "WPA2 PSK (%s)", algo);
368                         break;
369
370                 case 132:
371                         sprintf(buf, "mixed WPA/WPA2 PSK (%s)", algo);
372                         break;
373
374                 default:
375                         sprintf(buf, "Unknown");
376         }
377
378         return 0;
379 }
380
381 int wl_get_assoclist(const char *ifname, char *buf, int *len)
382 {
383         int i, j, noise;
384         int ap, infra, passive;
385         char line[128];
386         char macstr[18];
387         char devstr[IFNAMSIZ];
388         struct wl_maclist *macs;
389         struct wl_sta_rssi rssi;
390         struct iwinfo_assoclist_entry entry;
391         FILE *arp;
392
393         ap = infra = passive = 0;
394
395         wl_ioctl(ifname, WLC_GET_AP, &ap, sizeof(ap));
396         wl_ioctl(ifname, WLC_GET_INFRA, &infra, sizeof(infra));
397         wl_ioctl(ifname, WLC_GET_PASSIVE, &passive, sizeof(passive));
398
399         if( wl_get_noise(ifname, &noise) )
400                 noise = 0;
401
402         if( (ap || infra || passive) && ((macs = wl_read_assoclist(ifname)) != NULL) )
403         {
404                 for( i = 0, j = 0; i < macs->count; i++, j += sizeof(struct iwinfo_assoclist_entry) )
405                 {
406                         memcpy(rssi.mac, &macs->ea[i], 6);
407
408                         if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
409                                 entry.signal = (rssi.rssi - 0x100);
410                         else
411                                 entry.signal = 0;
412
413                         entry.noise = noise;
414                         memcpy(entry.mac, &macs->ea[i], 6);
415                         memcpy(&buf[j], &entry, sizeof(entry));
416                 }
417
418                 *len = j;
419                 free(macs);
420                 return 0;
421         }
422         else if( (arp = fopen("/proc/net/arp", "r")) != NULL )
423         {
424                 j = 0;
425
426                 while( fgets(line, sizeof(line), arp) != NULL )
427                 {
428                         if( sscanf(line, "%*s 0x%*d 0x%*d %17s %*s %s", macstr, devstr) && !strcmp(devstr, ifname) )
429                         {
430                                 rssi.mac[0] = strtol(&macstr[0],  NULL, 16);
431                                 rssi.mac[1] = strtol(&macstr[3],  NULL, 16);
432                                 rssi.mac[2] = strtol(&macstr[6],  NULL, 16);
433                                 rssi.mac[3] = strtol(&macstr[9],  NULL, 16);
434                                 rssi.mac[4] = strtol(&macstr[12], NULL, 16);
435                                 rssi.mac[5] = strtol(&macstr[15], NULL, 16);
436
437                                 if( !wl_ioctl(ifname, WLC_GET_RSSI, &rssi, sizeof(struct wl_sta_rssi)) )
438                                         entry.signal = (rssi.rssi - 0x100);
439                                 else
440                                         entry.signal = 0;
441
442                                 entry.noise = noise;
443                                 memcpy(entry.mac, rssi.mac, 6);
444                                 memcpy(&buf[j], &entry, sizeof(entry));
445
446                                 j += sizeof(entry);
447                         }
448                 }
449
450                 *len = j;
451                 (void) fclose(arp);
452                 return 0;
453         }
454
455         return -1;
456 }
457
458 int wl_get_txpwrlist(const char *ifname, char *buf, int *len)
459 {
460         struct iwinfo_txpwrlist_entry entry;
461         uint8_t dbm[8] = { 0, 6, 8, 10, 12, 14, 16, 18 };
462         uint8_t mw[8]  = { 1, 3, 6, 10, 15, 25, 39, 63 };
463         int i;
464
465         for( i = 0; i < 8; i++ )
466         {
467                 entry.dbm = dbm[i];
468                 entry.mw  = mw[i];
469                 memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
470         }
471
472         *len = 8 * sizeof(entry);
473         return 0;
474 }
475
476 int wl_get_scanlist(const char *ifname, char *buf, int *len)
477 {
478         return wext_get_scanlist(ifname, buf, len);
479 }
480
481 int wl_get_freqlist(const char *ifname, char *buf, int *len)
482 {
483         return wext_get_freqlist(ifname, buf, len);
484 }
485
486 int wl_get_country(const char *ifname, char *buf)
487 {
488         char ccode[WLC_CNTRY_BUF_SZ];
489
490         if( !wl_ioctl(ifname, WLC_GET_COUNTRY, ccode, WLC_CNTRY_BUF_SZ) )
491         {
492                 /* IL0 -> World */
493                 if( !strcmp(ccode, "IL0") )
494                         sprintf(buf, "00");
495
496                 /* YU -> RS */
497                 else if( !strcmp(ccode, "YU") )
498                         sprintf(buf, "RS");
499
500                 else
501                         memcpy(buf, ccode, 2);
502
503                 return 0;
504         }
505
506         return -1;
507 }
508
509 int wl_get_countrylist(const char *ifname, char *buf, int *len)
510 {
511         int i, count;
512         char cdata[WLC_IOCTL_MAXLEN];
513         struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
514         wl_country_list_t *cl = (wl_country_list_t *)cdata;
515
516         cl->buflen = sizeof(cdata);
517
518         if( !wl_ioctl(ifname, WLC_GET_COUNTRY_LIST, cl, cl->buflen) )
519         {
520                 for( i = 0, count = 0; i < cl->count; i++, c++ )
521                 {
522                         sprintf(c->ccode, &cl->country_abbrev[i * WLC_CNTRY_BUF_SZ]);
523                         c->iso3166 = c->ccode[0] * 256 + c->ccode[1];
524
525                         /* IL0 -> World */
526                         if( !strcmp(c->ccode, "IL0") )
527                                 c->iso3166 = 0x3030;
528
529                         /* YU -> RS */
530                         else if( !strcmp(c->ccode, "YU") )
531                                 c->iso3166 = 0x5253;
532                 }
533
534                 *len = (i * sizeof(struct iwinfo_country_entry));
535                 return 0;
536         }
537
538         return -1;
539 }
540
541 int wl_get_mbssid_support(const char *ifname, int *buf)
542 {
543         wlc_rev_info_t revinfo;
544
545         /* Multi bssid support only works on corerev >= 9 */
546         if( !wl_ioctl(ifname, WLC_GET_REVINFO, &revinfo, sizeof(revinfo)) )
547         {
548                 if( revinfo.corerev >= 9 )
549                 {
550                         *buf = 1;
551                         return 0;
552                 }
553         }
554
555         return -1;
556 }