libiwinfo: add restricted flag for freqlist
[project/luci.git] / contrib / package / 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
27 /*
28  * Madwifi ISO 3166 to Country/Region Code mapping.
29  */
30
31 static struct ISO3166_to_CCode
32 {
33         u_int16_t iso3166;
34         u_int16_t ccode;
35 } CountryCodes[] = {
36         { 0x3030 /* 00 */,   0 }, /* World */
37         { 0x4145 /* AE */, 784 }, /* U.A.E. */
38         { 0x414C /* AL */,   8 }, /* Albania */
39         { 0x414D /* AM */,  51 }, /* Armenia */
40         { 0x4152 /* AR */,  32 }, /* Argentina */
41         { 0x4154 /* AT */,  40 }, /* Austria */
42         { 0x4155 /* AU */,  36 }, /* Australia */
43         { 0x415A /* AZ */,  31 }, /* Azerbaijan */
44         { 0x4245 /* BE */,  56 }, /* Belgium */
45         { 0x4247 /* BG */, 100 }, /* Bulgaria */
46         { 0x4248 /* BH */,  48 }, /* Bahrain */
47         { 0x424E /* BN */,  96 }, /* Brunei Darussalam */
48         { 0x424F /* BO */,  68 }, /* Bolivia */
49         { 0x4252 /* BR */,  76 }, /* Brazil */
50         { 0x4259 /* BY */, 112 }, /* Belarus */
51         { 0x425A /* BZ */,  84 }, /* Belize */
52         { 0x4341 /* CA */, 124 }, /* Canada */
53         { 0x4348 /* CH */, 756 }, /* Switzerland */
54         { 0x434C /* CL */, 152 }, /* Chile */
55         { 0x434E /* CN */, 156 }, /* People's Republic of China */
56         { 0x434F /* CO */, 170 }, /* Colombia */
57         { 0x4352 /* CR */, 188 }, /* Costa Rica */
58         { 0x4359 /* CY */, 196 }, /* Cyprus */
59         { 0x435A /* CZ */, 203 }, /* Czech Republic */
60         { 0x4445 /* DE */, 276 }, /* Germany */
61         { 0x444B /* DK */, 208 }, /* Denmark */
62         { 0x444F /* DO */, 214 }, /* Dominican Republic */
63         { 0x445A /* DZ */,  12 }, /* Algeria */
64         { 0x4543 /* EC */, 218 }, /* Ecuador */
65         { 0x4545 /* EE */, 233 }, /* Estonia */
66         { 0x4547 /* EG */, 818 }, /* Egypt */
67         { 0x4553 /* ES */, 724 }, /* Spain */
68         { 0x4649 /* FI */, 246 }, /* Finland */
69         { 0x464F /* FO */, 234 }, /* Faeroe Islands */
70         { 0x4652 /* FR */, 250 }, /* France */
71         { 0x4652 /* FR */, 255 }, /* France2 */
72         { 0x4742 /* GB */, 826 }, /* United Kingdom */
73         { 0x4745 /* GE */, 268 }, /* Georgia */
74         { 0x4752 /* GR */, 300 }, /* Greece */
75         { 0x4754 /* GT */, 320 }, /* Guatemala */
76         { 0x484B /* HK */, 344 }, /* Hong Kong S.A.R., P.R.C. */
77         { 0x484E /* HN */, 340 }, /* Honduras */
78         { 0x4852 /* HR */, 191 }, /* Croatia */
79         { 0x4855 /* HU */, 348 }, /* Hungary */
80         { 0x4944 /* ID */, 360 }, /* Indonesia */
81         { 0x4945 /* IE */, 372 }, /* Ireland */
82         { 0x494C /* IL */, 376 }, /* Israel */
83         { 0x494E /* IN */, 356 }, /* India */
84         { 0x4951 /* IQ */, 368 }, /* Iraq */
85         { 0x4952 /* IR */, 364 }, /* Iran */
86         { 0x4953 /* IS */, 352 }, /* Iceland */
87         { 0x4954 /* IT */, 380 }, /* Italy */
88         { 0x4A4D /* JM */, 388 }, /* Jamaica */
89         { 0x4A4F /* JO */, 400 }, /* Jordan */
90         { 0x4A50 /* JP */, 392 }, /* Japan */
91         { 0x4A50 /* JP */, 393 }, /* Japan (JP1) */
92         { 0x4A50 /* JP */, 394 }, /* Japan (JP0) */
93         { 0x4A50 /* JP */, 395 }, /* Japan (JP1-1) */
94         { 0x4A50 /* JP */, 396 }, /* Japan (JE1) */
95         { 0x4A50 /* JP */, 397 }, /* Japan (JE2) */
96         { 0x4A50 /* JP */, 399 }, /* Japan (JP6) */
97         { 0x4A50 /* JP */, 900 }, /* Japan */
98         { 0x4A50 /* JP */, 901 }, /* Japan */
99         { 0x4A50 /* JP */, 902 }, /* Japan */
100         { 0x4A50 /* JP */, 903 }, /* Japan */
101         { 0x4A50 /* JP */, 904 }, /* Japan */
102         { 0x4A50 /* JP */, 905 }, /* Japan */
103         { 0x4A50 /* JP */, 906 }, /* Japan */
104         { 0x4A50 /* JP */, 907 }, /* Japan */
105         { 0x4A50 /* JP */, 908 }, /* Japan */
106         { 0x4A50 /* JP */, 909 }, /* Japan */
107         { 0x4A50 /* JP */, 910 }, /* Japan */
108         { 0x4A50 /* JP */, 911 }, /* Japan */
109         { 0x4A50 /* JP */, 912 }, /* Japan */
110         { 0x4A50 /* JP */, 913 }, /* Japan */
111         { 0x4A50 /* JP */, 914 }, /* Japan */
112         { 0x4A50 /* JP */, 915 }, /* Japan */
113         { 0x4A50 /* JP */, 916 }, /* Japan */
114         { 0x4A50 /* JP */, 917 }, /* Japan */
115         { 0x4A50 /* JP */, 918 }, /* Japan */
116         { 0x4A50 /* JP */, 919 }, /* Japan */
117         { 0x4A50 /* JP */, 920 }, /* Japan */
118         { 0x4A50 /* JP */, 921 }, /* Japan */
119         { 0x4A50 /* JP */, 922 }, /* Japan */
120         { 0x4A50 /* JP */, 923 }, /* Japan */
121         { 0x4A50 /* JP */, 924 }, /* Japan */
122         { 0x4A50 /* JP */, 925 }, /* Japan */
123         { 0x4A50 /* JP */, 926 }, /* Japan */
124         { 0x4A50 /* JP */, 927 }, /* Japan */
125         { 0x4A50 /* JP */, 928 }, /* Japan */
126         { 0x4A50 /* JP */, 929 }, /* Japan */
127         { 0x4A50 /* JP */, 930 }, /* Japan */
128         { 0x4A50 /* JP */, 931 }, /* Japan */
129         { 0x4A50 /* JP */, 932 }, /* Japan */
130         { 0x4A50 /* JP */, 933 }, /* Japan */
131         { 0x4A50 /* JP */, 934 }, /* Japan */
132         { 0x4A50 /* JP */, 935 }, /* Japan */
133         { 0x4A50 /* JP */, 936 }, /* Japan */
134         { 0x4A50 /* JP */, 937 }, /* Japan */
135         { 0x4A50 /* JP */, 938 }, /* Japan */
136         { 0x4A50 /* JP */, 939 }, /* Japan */
137         { 0x4A50 /* JP */, 940 }, /* Japan */
138         { 0x4A50 /* JP */, 941 }, /* Japan */
139         { 0x4B45 /* KE */, 404 }, /* Kenya */
140         { 0x4B50 /* KP */, 408 }, /* North Korea */
141         { 0x4B52 /* KR */, 410 }, /* South Korea */
142         { 0x4B52 /* KR */, 411 }, /* South Korea */
143         { 0x4B57 /* KW */, 414 }, /* Kuwait */
144         { 0x4B5A /* KZ */, 398 }, /* Kazakhstan */
145         { 0x4C42 /* LB */, 422 }, /* Lebanon */
146         { 0x4C49 /* LI */, 438 }, /* Liechtenstein */
147         { 0x4C54 /* LT */, 440 }, /* Lithuania */
148         { 0x4C55 /* LU */, 442 }, /* Luxembourg */
149         { 0x4C56 /* LV */, 428 }, /* Latvia */
150         { 0x4C59 /* LY */, 434 }, /* Libya */
151         { 0x4D41 /* MA */, 504 }, /* Morocco */
152         { 0x4D43 /* MC */, 492 }, /* Principality of Monaco */
153         { 0x4D4B /* MK */, 807 }, /* the Former Yugoslav Republic of Macedonia */
154         { 0x4D4F /* MO */, 446 }, /* Macau */
155         { 0x4D58 /* MX */, 484 }, /* Mexico */
156         { 0x4D59 /* MY */, 458 }, /* Malaysia */
157         { 0x4E49 /* NI */, 558 }, /* Nicaragua */
158         { 0x4E4C /* NL */, 528 }, /* Netherlands */
159         { 0x4E4F /* NO */, 578 }, /* Norway */
160         { 0x4E5A /* NZ */, 554 }, /* New Zealand */
161         { 0x4F4D /* OM */, 512 }, /* Oman */
162         { 0x5041 /* PA */, 591 }, /* Panama */
163         { 0x5045 /* PE */, 604 }, /* Peru */
164         { 0x5048 /* PH */, 608 }, /* Republic of the Philippines */
165         { 0x504B /* PK */, 586 }, /* Islamic Republic of Pakistan */
166         { 0x504C /* PL */, 616 }, /* Poland */
167         { 0x5052 /* PR */, 630 }, /* Puerto Rico */
168         { 0x5054 /* PT */, 620 }, /* Portugal */
169         { 0x5059 /* PY */, 600 }, /* Paraguay */
170         { 0x5141 /* QA */, 634 }, /* Qatar */
171         { 0x524F /* RO */, 642 }, /* Romania */
172         { 0x5255 /* RU */, 643 }, /* Russia */
173         { 0x5341 /* SA */, 682 }, /* Saudi Arabia */
174         { 0x5345 /* SE */, 752 }, /* Sweden */
175         { 0x5347 /* SG */, 702 }, /* Singapore */
176         { 0x5349 /* SI */, 705 }, /* Slovenia */
177         { 0x534B /* SK */, 703 }, /* Slovak Republic */
178         { 0x5356 /* SV */, 222 }, /* El Salvador */
179         { 0x5359 /* SY */, 760 }, /* Syria */
180         { 0x5448 /* TH */, 764 }, /* Thailand */
181         { 0x544E /* TN */, 788 }, /* Tunisia */
182         { 0x5452 /* TR */, 792 }, /* Turkey */
183         { 0x5454 /* TT */, 780 }, /* Trinidad y Tobago */
184         { 0x5457 /* TW */, 158 }, /* Taiwan */
185         { 0x5541 /* UA */, 804 }, /* Ukraine */
186         { 0x554B /* UK */, 826 }, /* United Kingdom */
187         { 0x5553 /* US */, 840 }, /* United States */
188         { 0x5553 /* US */, 842 }, /* United States (Public Safety)*/
189         { 0x5559 /* UY */, 858 }, /* Uruguay */
190         { 0x555A /* UZ */, 860 }, /* Uzbekistan */
191         { 0x5645 /* VE */, 862 }, /* Venezuela */
192         { 0x564E /* VN */, 704 }, /* Viet Nam */
193         { 0x5945 /* YE */, 887 }, /* Yemen */
194         { 0x5A41 /* ZA */, 710 }, /* South Africa */
195         { 0x5A57 /* ZW */, 716 }, /* Zimbabwe */
196 };
197
198
199 static int ioctl_socket = -1;
200
201 static int madwifi_ioctl(struct iwreq *wrq, const char *ifname, int cmd, void *data, size_t len)
202 {
203         /* prepare socket */
204         if( ioctl_socket == -1 )
205         {
206                 ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
207                 fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
208         }
209
210         strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
211
212         if( data != NULL )
213         {
214                 if( len < IFNAMSIZ )
215                 {
216                         memcpy(wrq->u.name, data, len);
217                 }
218                 else
219                 {
220                         wrq->u.data.pointer = data;
221                         wrq->u.data.length = len;
222                 }
223         }
224
225         return ioctl(ioctl_socket, cmd, wrq);
226 }
227
228 static int get80211priv(const char *ifname, int op, void *data, size_t len)
229 {
230         struct iwreq iwr;
231
232         if( madwifi_ioctl(&iwr, ifname, op, data, len) < 0 )
233                 return -1;
234
235         return iwr.u.data.length;
236 }
237
238 static char * madwifi_isvap(const char *ifname, const char *wifiname)
239 {
240         int fd, ln;
241         char path[32];
242         char *ret = NULL;
243         static char name[IFNAMSIZ];
244
245         if( strlen(ifname) <= 9 )
246         {
247                 sprintf(path, "/proc/sys/net/%s/%%parent", ifname);
248
249                 if( (fd = open(path, O_RDONLY)) > -1 )
250                 {
251                         if( wifiname != NULL )
252                         {
253                                 if( read(fd, name, strlen(wifiname)) == strlen(wifiname) )
254                                         ret = strncmp(name, wifiname, strlen(wifiname))
255                                                 ? NULL : name;
256                         }
257                         else if( (ln = read(fd, name, IFNAMSIZ)) >= 4 )
258                         {
259                                 name[ln-1] = 0;
260                                 ret = name;
261                         }
262
263                         (void) close(fd);
264                 }
265         }
266
267         return ret;
268 }
269
270 static int madwifi_iswifi(const char *ifname)
271 {
272         int ret;
273         char path[32];
274         struct stat s;
275
276         ret = 0;
277
278         if( strlen(ifname) <= 7 )
279         {
280                 sprintf(path, "/proc/sys/dev/%s/diversity", ifname);
281
282                 if( ! stat(path, &s) )
283                         ret = (s.st_mode & S_IFREG);
284         }
285
286         return ret;
287 }
288
289
290 int madwifi_probe(const char *ifname)
291 {
292         return ( !!madwifi_isvap(ifname, NULL) || madwifi_iswifi(ifname) );
293 }
294
295 void madwifi_close(void)
296 {
297         if( ioctl_socket > -1 )
298                 close(ioctl_socket);
299 }
300
301 int madwifi_get_mode(const char *ifname, char *buf)
302 {
303         return wext_get_mode(ifname, buf);
304 }
305
306 int madwifi_get_ssid(const char *ifname, char *buf)
307 {
308         return wext_get_ssid(ifname, buf);
309 }
310
311 int madwifi_get_bssid(const char *ifname, char *buf)
312 {
313         return wext_get_bssid(ifname, buf);
314 }
315
316 int madwifi_get_channel(const char *ifname, int *buf)
317 {
318         int i;
319         uint16_t freq;
320         struct iwreq wrq;
321         struct ieee80211req_chaninfo chans;
322
323         if( madwifi_ioctl(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
324         {
325                 /* Madwifi returns a Hz frequency, get it's freq list to find channel index */
326                 freq = (uint16_t)(wrq.u.freq.m / 100000);
327
328                 if( get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans)) >= 0 )
329                 {
330                         *buf = 0;
331
332                         for( i = 0; i < chans.ic_nchans; i++ )
333                         {
334                                 if( freq == chans.ic_chans[i].ic_freq )
335                                 {
336                                         *buf = chans.ic_chans[i].ic_ieee;
337                                         break;
338                                 }
339                         }
340
341                         return 0;
342                 }
343         }
344
345         return -1;
346 }
347
348 int madwifi_get_frequency(const char *ifname, int *buf)
349 {
350         struct iwreq wrq;
351
352         if( madwifi_ioctl(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
353         {
354                 *buf = (uint16_t)(wrq.u.freq.m / 100000);
355                 return 0;
356         }
357
358         return -1;
359 }
360
361 int madwifi_get_txpower(const char *ifname, int *buf)
362 {
363         return wext_get_txpower(ifname, buf);
364 }
365
366 int madwifi_get_bitrate(const char *ifname, int *buf)
367 {
368         unsigned int mode, len, rate, rate_count;
369         uint8_t tmp[24*1024];
370         uint8_t *cp;
371         struct iwreq wrq;
372         struct ieee80211req_sta_info *si;
373
374         if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
375         {
376                 mode = wrq.u.mode;
377
378                 /* Calculate bitrate average from associated stations in ad-hoc mode */
379                 if( mode == 1 )
380                 {
381                         rate = rate_count = 0;
382
383                         if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
384                         {
385                                 cp = tmp;
386
387                                 do {
388                                         si = (struct ieee80211req_sta_info *) cp;
389
390                                         if( si->isi_rssi > 0 )
391                                         {
392                                                 rate_count++;
393                                                 rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
394                                         }
395
396                                         cp   += si->isi_len;
397                                         len  -= si->isi_len;
398                                 } while (len >= sizeof(struct ieee80211req_sta_info));
399                         }
400
401                         *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count) * 1000;
402                         return 0;
403                 }
404
405                 /* Return whatever wext tells us ... */
406                 return wext_get_bitrate(ifname, buf);
407         }
408
409         return -1;
410 }
411
412 int madwifi_get_signal(const char *ifname, int *buf)
413 {
414         unsigned int mode, len, rssi, rssi_count;
415         uint8_t tmp[24*1024];
416         uint8_t *cp;
417         struct iwreq wrq;
418         struct ieee80211req_sta_info *si;
419
420         if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
421         {
422                 mode = wrq.u.mode;
423
424                 /* Calculate signal average from associated stations in ap or ad-hoc mode */
425                 if( mode == 1 )
426                 {
427                         rssi = rssi_count = 0;
428
429                         if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
430                         {
431                                 cp = tmp;
432
433                                 do {
434                                         si = (struct ieee80211req_sta_info *) cp;
435
436                                         if( si->isi_rssi > 0 )
437                                         {
438                                                 rssi_count++;
439                                                 rssi -= (si->isi_rssi - 95);
440                                         }
441
442                                         cp   += si->isi_len;
443                                         len  -= si->isi_len;
444                                 } while (len >= sizeof(struct ieee80211req_sta_info));
445                         }
446
447                         *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
448                         return 0;
449                 }
450
451                 /* Return whatever wext tells us ... */
452                 return wext_get_signal(ifname, buf);
453         }
454
455         return -1;
456 }
457
458 int madwifi_get_noise(const char *ifname, int *buf)
459 {
460         return wext_get_noise(ifname, buf);
461 }
462
463 int madwifi_get_quality(const char *ifname, int *buf)
464 {
465         unsigned int mode, len, quality, quality_count;
466         uint8_t tmp[24*1024];
467         uint8_t *cp;
468         struct iwreq wrq;
469         struct ieee80211req_sta_info *si;
470
471         if( madwifi_ioctl(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
472         {
473                 mode = wrq.u.mode;
474
475                 /* Calculate signal average from associated stations in ad-hoc mode */
476                 if( mode == 1 )
477                 {
478                         quality = quality_count = 0;
479
480                         if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
481                         {
482                                 cp = tmp;
483
484                                 do {
485                                         si = (struct ieee80211req_sta_info *) cp;
486
487                                         if( si->isi_rssi > 0 )
488                                         {
489                                                 quality_count++;
490                                                 quality += si->isi_rssi;
491                                         }
492
493                                         cp   += si->isi_len;
494                                         len  -= si->isi_len;
495                                 } while (len >= sizeof(struct ieee80211req_sta_info));
496                         }
497
498                         *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
499                         return 0;
500                 }
501
502                 /* Return whatever wext tells us ... */
503                 return wext_get_quality(ifname, buf);
504         }
505
506         return -1;
507 }
508
509 int madwifi_get_quality_max(const char *ifname, int *buf)
510 {
511         return wext_get_quality_max(ifname, buf);
512 }
513
514 int madwifi_get_encryption(const char *ifname, char *buf)
515 {
516         int ciphers = 0, key_type = 0, key_len = 0;
517         struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
518         struct iwreq wrq;
519         struct ieee80211req_key wk;
520
521         memset(&wrq, 0, sizeof(wrq));
522         memset(&wk, 0, sizeof(wk));
523         memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
524
525         /* Get key information */
526         if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
527         {
528                 key_type = wk.ik_type;
529
530                 /* Type 0 == WEP */
531                 if( key_type == 0 )
532                         c->auth_algs = (IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED);
533         }
534
535         /* Get wpa protocol version */
536         wrq.u.mode = IEEE80211_PARAM_WPA;
537         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
538                 c->wpa_version = wrq.u.mode;
539
540         /* Get authentication suites */
541         wrq.u.mode = IEEE80211_PARAM_AUTHMODE;
542         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
543         {
544                 switch(wrq.u.mode) {
545                         case IEEE80211_AUTH_8021X:
546                                 c->auth_suites |= IWINFO_KMGMT_8021x;
547                                 break;
548
549                         case IEEE80211_AUTH_WPA:
550                                 c->auth_suites |= IWINFO_KMGMT_PSK;
551                                 break;
552
553                         default:
554                                 c->auth_suites |= IWINFO_KMGMT_NONE;
555                                 break;
556                 }
557         }
558
559         /* Get group key length */
560         wrq.u.mode = IEEE80211_PARAM_MCASTKEYLEN;
561         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
562                 key_len = wrq.u.mode;
563
564         /* Get used pairwise ciphers */
565         wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
566         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
567         {
568                 ciphers = wrq.u.mode;
569
570                 if( ciphers & (1 << IEEE80211_CIPHER_TKIP) )
571                         c->pair_ciphers |= IWINFO_CIPHER_TKIP;
572
573                 if( ciphers & (1 << IEEE80211_CIPHER_AES_CCM) )
574                         c->pair_ciphers |= IWINFO_CIPHER_CCMP;
575
576                 if( ciphers & (1 << IEEE80211_CIPHER_AES_OCB) )
577                         c->pair_ciphers |= IWINFO_CIPHER_AESOCB;
578
579                 if( ciphers & (1 << IEEE80211_CIPHER_CKIP) )
580                         c->pair_ciphers |= IWINFO_CIPHER_CKIP;
581
582                 if( ciphers & (1 << IEEE80211_CIPHER_WEP) )
583                 {
584                         switch(key_len) {
585                                 case 13:
586                                         c->pair_ciphers |= IWINFO_CIPHER_WEP104;
587                                         break;
588
589                                 case 5:
590                                         c->pair_ciphers |= IWINFO_CIPHER_WEP40;
591                                         break;
592
593                                 default:
594                                         break;
595                         }
596                 }
597
598                 if( ciphers & (1 << IEEE80211_CIPHER_NONE) )
599                         c->pair_ciphers |= IWINFO_CIPHER_NONE;
600         }
601
602         /* Get used group cipher */
603         wrq.u.mode = IEEE80211_PARAM_MCASTCIPHER;
604         if( madwifi_ioctl(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
605         {
606                 ciphers = wrq.u.mode;
607
608                 switch(wrq.u.mode) {
609                         case IEEE80211_CIPHER_TKIP:
610                                 c->group_ciphers |= IWINFO_CIPHER_TKIP;
611                                 break;
612
613                         case IEEE80211_CIPHER_AES_CCM:
614                                 c->group_ciphers |= IWINFO_CIPHER_CCMP;
615                                 break;
616
617                         case IEEE80211_CIPHER_AES_OCB:
618                                 c->group_ciphers |= IWINFO_CIPHER_AESOCB;
619                                 break;
620
621                         case IEEE80211_CIPHER_CKIP:
622                                 c->group_ciphers |= IWINFO_CIPHER_CKIP;
623                                 break;
624
625                         case IEEE80211_CIPHER_WEP:
626                                 switch(key_len) {
627                                         case 13:
628                                                 c->group_ciphers |= IWINFO_CIPHER_WEP104;
629                                                 break;
630
631                                         case 5:
632                                                 c->group_ciphers |= IWINFO_CIPHER_WEP40;
633                                                 break;
634
635                                         default:
636                                                 break;
637                                 }
638                                 break;
639
640                         case IEEE80211_CIPHER_NONE:
641                                 c->group_ciphers |= IWINFO_CIPHER_NONE;
642                                 break;
643
644                         default:
645                                 break;
646                 }
647         }
648
649         c->enabled = (c->wpa_version || c->auth_algs) ? 1 : 0;
650
651         return 0;
652 }
653
654 int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
655 {
656         int bl, tl, noise;
657         uint8_t *cp;
658         uint8_t tmp[24*1024];
659         struct ieee80211req_sta_info *si;
660         struct iwinfo_assoclist_entry entry;
661
662         if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
663         {
664                 cp = tmp;
665                 bl = 0;
666
667                 if( madwifi_get_noise(ifname, &noise) )
668                         noise = 0;
669
670                 do {
671                         si = (struct ieee80211req_sta_info *) cp;
672
673                         entry.signal = (si->isi_rssi - 95);
674                         entry.noise  = noise;
675                         memcpy(entry.mac, &si->isi_macaddr, 6);
676                         memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
677
678                         bl += sizeof(struct iwinfo_assoclist_entry);
679                         cp += si->isi_len;
680                         tl -= si->isi_len;
681                 } while (tl >= sizeof(struct ieee80211req_sta_info));
682
683                 *len = bl;
684                 return 0;
685         }
686
687         return -1;
688 }
689
690 int madwifi_get_txpwrlist(const char *ifname, char *buf, int *len)
691 {
692         int rc = -1;
693         char cmd[256];
694
695         /* A wifiX device? */
696         if( madwifi_iswifi(ifname) )
697         {
698                 sprintf(cmd, "wlanconfig ath-txpwr create nounit "
699                         "wlandev %s wlanmode ap >/dev/null", ifname);
700
701                 if( ! WEXITSTATUS(system(cmd)) )
702                 {
703                         rc = wext_get_txpwrlist("ath-txpwr", buf, len);
704                         (void) WEXITSTATUS(system("wlanconfig ath-txpwr destroy"));
705                 }
706         }
707
708         /* Its an athX ... */
709         else if( !!madwifi_isvap(ifname, NULL) )
710         {
711                 rc = wext_get_txpwrlist(ifname, buf, len);
712         }
713
714         return rc;
715 }
716
717 int madwifi_get_scanlist(const char *ifname, char *buf, int *len)
718 {
719         int ret;
720         char cmd[256];
721         DIR *proc;
722         struct dirent *e;
723
724         ret = -1;
725
726         /* We got a wifiX device passed, try to lookup a vap on it */
727         if( madwifi_iswifi(ifname) )
728         {
729                 if( (proc = opendir("/proc/sys/net/")) != NULL )
730                 {
731                         while( (e = readdir(proc)) != NULL )
732                         {
733                                 if( !!madwifi_isvap(e->d_name, ifname) )
734                                 {
735                                         sprintf(cmd, "ifconfig %s up", e->d_name);
736
737                                         if( ! WEXITSTATUS(system(cmd)) )
738                                         {
739                                                 ret = wext_get_scanlist(e->d_name, buf, len);
740                                                 break;
741                                         }
742                                 }
743                         }
744
745                         closedir(proc);
746                 }
747
748                 /* Still nothing found, try to create a vap */
749                 if( ret == -1 )
750                 {
751                         sprintf(cmd, "wlanconfig ath-scan create nounit "
752                                 "wlandev %s wlanmode sta >/dev/null", ifname);
753
754                         if( ! WEXITSTATUS(system(cmd)) && ! WEXITSTATUS(system("ifconfig ath-scan up")) )
755                         {
756                                 ret = wext_get_scanlist("ath-scan", buf, len);
757
758                                 (void) WEXITSTATUS(system("ifconfig ath-scan down"));
759                                 (void) WEXITSTATUS(system("wlanconfig ath-scan destroy"));
760                         }
761                 }
762         }
763
764         /* Got athX device? */
765         else if( !!madwifi_isvap(ifname, NULL) )
766         {
767                 ret = wext_get_scanlist(ifname, buf, len);
768         }
769
770         return ret;
771 }
772
773 int madwifi_get_freqlist(const char *ifname, char *buf, int *len)
774 {
775         int i, bl;
776         int rc = -1;
777         char cmd[256];
778         struct ieee80211req_chaninfo chans;
779         struct iwinfo_freqlist_entry entry;
780
781         /* A wifiX device? */
782         if( madwifi_iswifi(ifname) )
783         {
784                 sprintf(cmd, "wlanconfig ath-channels create nounit "
785                         "wlandev %s wlanmode ap >/dev/null", ifname);
786
787                 if( ! WEXITSTATUS(system(cmd)) )
788                 {
789                         rc = get80211priv("ath-channels", IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans));
790                         (void) WEXITSTATUS(system("wlanconfig ath-channels destroy"));
791                 }
792         }
793
794         /* Its an athX ... */
795         else if( !!madwifi_isvap(ifname, NULL) )
796         {
797                 rc = get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans));
798         }
799
800
801         /* Got chaninfo? */
802         if( rc >= 0 )
803         {
804                 bl = 0;
805
806                 for( i = 0; i < chans.ic_nchans; i++ )
807                 {
808                         entry.mhz        = chans.ic_chans[i].ic_freq;
809                         entry.channel    = chans.ic_chans[i].ic_ieee;
810                         entry.restricted = 0;
811
812                         memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
813                         bl += sizeof(struct iwinfo_freqlist_entry);
814                 }
815
816                 *len = bl;
817                 return 0;
818         }
819
820         return -1;
821 }
822
823 int madwifi_get_country(const char *ifname, char *buf)
824 {
825         int i, fd, ccode = -1;
826         char buffer[34];
827         char *wifi = madwifi_iswifi(ifname)
828                 ? (char *)ifname : madwifi_isvap(ifname, NULL);
829
830         struct ISO3166_to_CCode *e;
831
832         if( wifi )
833         {
834                 snprintf(buffer, sizeof(buffer), "/proc/sys/dev/%s/countrycode", wifi);
835
836                 if( (fd = open(buffer, O_RDONLY)) > -1 )
837                 {
838                         memset(buffer, 0, sizeof(buffer));
839
840                         if( read(fd, buffer, sizeof(buffer)-1) > 0 )
841                                 ccode = atoi(buffer);
842
843                         close(fd);
844                 }
845         }
846
847         for( i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
848         {
849                 e = &CountryCodes[i];
850
851                 if( e->ccode == ccode )
852                 {
853                         sprintf(buf, "%c%c", e->iso3166 / 256, e->iso3166 % 256);
854                         return 0;
855                 }
856         }
857
858         return -1;
859 }
860
861 int madwifi_get_countrylist(const char *ifname, char *buf, int *len)
862 {
863         int i, count;
864         struct ISO3166_to_CCode *e, *p = NULL;
865         struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
866
867         count = 0;
868
869         for( int i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
870         {
871                 e = &CountryCodes[i];
872
873                 if( !p || (e->iso3166 != p->iso3166) )
874                 {
875                         c->iso3166 = e->iso3166;
876                         snprintf(c->ccode, sizeof(c->ccode), "%i", e->ccode);
877
878                         c++;
879                         count++;
880                 }
881
882                 p = e;
883         }
884
885         *len = (count * sizeof(struct iwinfo_country_entry));
886         return 0;
887 }
888
889 int madwifi_get_mbssid_support(const char *ifname, int *buf)
890 {
891         /* We assume that multi bssid is always possible */
892         *buf = 1;
893         return 0;
894 }