[package] iwinfo: replace internal constant mode strings with enums
[openwrt/svn-archive/archive.git] / package / iwinfo / src / iwinfo_madwifi.c
1 /*
2 * iwinfo - Wireless Information Library - Madwifi Backend
3 *
4 * Copyright (C) 2009-2010 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 madwifi_wrq(struct iwreq *wrq, const char *ifname, int cmd, void *data, size_t len)
200 {
201 strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
202
203 if( data != NULL )
204 {
205 if( len < IFNAMSIZ )
206 {
207 memcpy(wrq->u.name, data, len);
208 }
209 else
210 {
211 wrq->u.data.pointer = data;
212 wrq->u.data.length = len;
213 }
214 }
215
216 return iwinfo_ioctl(cmd, wrq);
217 }
218
219 static int get80211priv(const char *ifname, int op, void *data, size_t len)
220 {
221 struct iwreq iwr;
222
223 if( madwifi_wrq(&iwr, ifname, op, data, len) < 0 )
224 return -1;
225
226 return iwr.u.data.length;
227 }
228
229 static char * madwifi_isvap(const char *ifname, const char *wifiname)
230 {
231 int fd, ln;
232 char path[32];
233 char *ret = NULL;
234 static char name[IFNAMSIZ];
235
236 if( strlen(ifname) <= 9 )
237 {
238 sprintf(path, "/proc/sys/net/%s/%%parent", ifname);
239
240 if( (fd = open(path, O_RDONLY)) > -1 )
241 {
242 if( wifiname != NULL )
243 {
244 if( read(fd, name, strlen(wifiname)) == strlen(wifiname) )
245 ret = strncmp(name, wifiname, strlen(wifiname))
246 ? NULL : name;
247 }
248 else if( (ln = read(fd, name, IFNAMSIZ)) >= 4 )
249 {
250 name[ln-1] = 0;
251 ret = name;
252 }
253
254 (void) close(fd);
255 }
256 }
257
258 return ret;
259 }
260
261 static int madwifi_iswifi(const char *ifname)
262 {
263 int ret;
264 char path[32];
265 struct stat s;
266
267 ret = 0;
268
269 if( strlen(ifname) <= 7 )
270 {
271 sprintf(path, "/proc/sys/dev/%s/diversity", ifname);
272
273 if( ! stat(path, &s) )
274 ret = (s.st_mode & S_IFREG);
275 }
276
277 return ret;
278 }
279
280 static char * madwifi_ifadd(const char *ifname)
281 {
282 char *wifidev = NULL;
283 struct ifreq ifr = { 0 };
284 struct ieee80211_clone_params cp = { 0 };
285 static char nif[IFNAMSIZ] = { 0 };
286
287 if( !(wifidev = madwifi_isvap(ifname, NULL)) && madwifi_iswifi(ifname) )
288 wifidev = (char *)ifname;
289
290 if( wifidev )
291 {
292 snprintf(nif, sizeof(nif), "tmp.%s", ifname);
293
294 strncpy(cp.icp_name, nif, IFNAMSIZ);
295 cp.icp_opmode = IEEE80211_M_STA;
296 cp.icp_flags = IEEE80211_CLONE_BSSID;
297
298 strncpy(ifr.ifr_name, wifidev, IFNAMSIZ);
299 ifr.ifr_data = (void *)&cp;
300
301 if( !iwinfo_ioctl(SIOC80211IFCREATE, &ifr) )
302 {
303 return nif;
304 }
305 else
306 {
307 cp.icp_opmode = IEEE80211_M_MONITOR;
308
309 if( !iwinfo_ioctl(SIOC80211IFCREATE, &ifr) )
310 return nif;
311 }
312 }
313
314 return NULL;
315 }
316
317 static void madwifi_ifdel(const char *ifname)
318 {
319 struct ifreq ifr = { 0 };
320
321 strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
322 iwinfo_ioctl(SIOC80211IFDESTROY, &ifr);
323 }
324
325
326 int madwifi_probe(const char *ifname)
327 {
328 return ( !!madwifi_isvap(ifname, NULL) || madwifi_iswifi(ifname) );
329 }
330
331 void madwifi_close(void)
332 {
333 /* Nop */
334 }
335
336 int madwifi_get_mode(const char *ifname, int *buf)
337 {
338 return wext_get_mode(ifname, buf);
339 }
340
341 int madwifi_get_ssid(const char *ifname, char *buf)
342 {
343 return wext_get_ssid(ifname, buf);
344 }
345
346 int madwifi_get_bssid(const char *ifname, char *buf)
347 {
348 return wext_get_bssid(ifname, buf);
349 }
350
351 int madwifi_get_channel(const char *ifname, int *buf)
352 {
353 int i;
354 uint16_t freq;
355 struct iwreq wrq;
356 struct ieee80211req_chaninfo chans;
357
358 if( madwifi_wrq(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
359 {
360 /* Madwifi returns a Hz frequency, get it's freq list to find channel index */
361 freq = (uint16_t)(wrq.u.freq.m / 100000);
362
363 if( get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO, &chans, sizeof(chans)) >= 0 )
364 {
365 *buf = 0;
366
367 for( i = 0; i < chans.ic_nchans; i++ )
368 {
369 if( freq == chans.ic_chans[i].ic_freq )
370 {
371 *buf = chans.ic_chans[i].ic_ieee;
372 break;
373 }
374 }
375
376 return 0;
377 }
378 }
379
380 return -1;
381 }
382
383 int madwifi_get_frequency(const char *ifname, int *buf)
384 {
385 struct iwreq wrq;
386
387 if( madwifi_wrq(&wrq, ifname, SIOCGIWFREQ, NULL, 0) >= 0 )
388 {
389 *buf = (uint16_t)(wrq.u.freq.m / 100000);
390 return 0;
391 }
392
393 return -1;
394 }
395
396 int madwifi_get_txpower(const char *ifname, int *buf)
397 {
398 return wext_get_txpower(ifname, buf);
399 }
400
401 int madwifi_get_bitrate(const char *ifname, int *buf)
402 {
403 unsigned int mode, len, rate, rate_count;
404 uint8_t tmp[24*1024];
405 uint8_t *cp;
406 struct iwreq wrq;
407 struct ieee80211req_sta_info *si;
408
409 if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
410 {
411 mode = wrq.u.mode;
412
413 /* Calculate bitrate average from associated stations in ad-hoc mode */
414 if( mode == 1 )
415 {
416 rate = rate_count = 0;
417
418 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
419 {
420 cp = tmp;
421
422 do {
423 si = (struct ieee80211req_sta_info *) cp;
424
425 if( si->isi_rssi > 0 )
426 {
427 rate_count++;
428 rate += ((si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) / 2);
429 }
430
431 cp += si->isi_len;
432 len -= si->isi_len;
433 } while (len >= sizeof(struct ieee80211req_sta_info));
434 }
435
436 *buf = (rate == 0 || rate_count == 0) ? 0 : (rate / rate_count) * 1000;
437 return 0;
438 }
439
440 /* Return whatever wext tells us ... */
441 return wext_get_bitrate(ifname, buf);
442 }
443
444 return -1;
445 }
446
447 int madwifi_get_signal(const char *ifname, int *buf)
448 {
449 unsigned int mode, len, rssi, rssi_count;
450 uint8_t tmp[24*1024];
451 uint8_t *cp;
452 struct iwreq wrq;
453 struct ieee80211req_sta_info *si;
454
455 if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
456 {
457 mode = wrq.u.mode;
458
459 /* Calculate signal average from associated stations in ap or ad-hoc mode */
460 if( mode == 1 )
461 {
462 rssi = rssi_count = 0;
463
464 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
465 {
466 cp = tmp;
467
468 do {
469 si = (struct ieee80211req_sta_info *) cp;
470
471 if( si->isi_rssi > 0 )
472 {
473 rssi_count++;
474 rssi -= (si->isi_rssi - 95);
475 }
476
477 cp += si->isi_len;
478 len -= si->isi_len;
479 } while (len >= sizeof(struct ieee80211req_sta_info));
480 }
481
482 *buf = (rssi == 0 || rssi_count == 0) ? 1 : -(rssi / rssi_count);
483 return 0;
484 }
485
486 /* Return whatever wext tells us ... */
487 return wext_get_signal(ifname, buf);
488 }
489
490 return -1;
491 }
492
493 int madwifi_get_noise(const char *ifname, int *buf)
494 {
495 return wext_get_noise(ifname, buf);
496 }
497
498 int madwifi_get_quality(const char *ifname, int *buf)
499 {
500 unsigned int mode, len, quality, quality_count;
501 uint8_t tmp[24*1024];
502 uint8_t *cp;
503 struct iwreq wrq;
504 struct ieee80211req_sta_info *si;
505
506 if( madwifi_wrq(&wrq, ifname, SIOCGIWMODE, NULL, 0) >= 0 )
507 {
508 mode = wrq.u.mode;
509
510 /* Calculate signal average from associated stations in ad-hoc mode */
511 if( mode == 1 )
512 {
513 quality = quality_count = 0;
514
515 if( (len = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
516 {
517 cp = tmp;
518
519 do {
520 si = (struct ieee80211req_sta_info *) cp;
521
522 if( si->isi_rssi > 0 )
523 {
524 quality_count++;
525 quality += si->isi_rssi;
526 }
527
528 cp += si->isi_len;
529 len -= si->isi_len;
530 } while (len >= sizeof(struct ieee80211req_sta_info));
531 }
532
533 *buf = (quality == 0 || quality_count == 0) ? 0 : (quality / quality_count);
534 return 0;
535 }
536
537 /* Return whatever wext tells us ... */
538 return wext_get_quality(ifname, buf);
539 }
540
541 return -1;
542 }
543
544 int madwifi_get_quality_max(const char *ifname, int *buf)
545 {
546 return wext_get_quality_max(ifname, buf);
547 }
548
549 int madwifi_get_encryption(const char *ifname, char *buf)
550 {
551 int ciphers = 0, key_len = 0;
552 char keybuf[IW_ENCODING_TOKEN_MAX];
553 struct iwinfo_crypto_entry *c = (struct iwinfo_crypto_entry *)buf;
554 struct iwreq wrq;
555 struct ieee80211req_key wk;
556
557 memset(&wrq, 0, sizeof(wrq));
558
559 /* Obtain key info */
560 if( madwifi_wrq(&wrq, ifname, SIOCGIWENCODE, keybuf, sizeof(keybuf)) < 0 )
561 return -1;
562
563 #if 0
564 /* Have any encryption? */
565 if( (wrq.u.data.flags & IW_ENCODE_DISABLED) || (wrq.u.data.length == 0) )
566 return 0;
567 #endif
568
569 /* Save key len */
570 key_len = wrq.u.data.length;
571
572 /* Get wpa protocol version */
573 wrq.u.mode = IEEE80211_PARAM_WPA;
574 if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
575 c->wpa_version = wrq.u.mode;
576
577 /* Get authentication suites */
578 wrq.u.mode = IEEE80211_PARAM_AUTHMODE;
579 if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
580 {
581 switch(wrq.u.mode) {
582 case IEEE80211_AUTH_8021X:
583 c->auth_suites |= IWINFO_KMGMT_8021x;
584 break;
585
586 case IEEE80211_AUTH_WPA:
587 c->auth_suites |= IWINFO_KMGMT_PSK;
588 break;
589
590 case IEEE80211_AUTH_OPEN:
591 c->auth_algs |= IWINFO_AUTH_OPEN;
592 break;
593
594 case IEEE80211_AUTH_SHARED:
595 c->auth_algs |= IWINFO_AUTH_SHARED;
596 break;
597
598 default:
599 c->auth_suites |= IWINFO_KMGMT_NONE;
600 break;
601 }
602 }
603
604 memset(&wk, 0, sizeof(wk));
605 memset(wk.ik_macaddr, 0xff, IEEE80211_ADDR_LEN);
606
607 /* Get key information */
608 if( get80211priv(ifname, IEEE80211_IOCTL_GETKEY, &wk, sizeof(wk)) >= 0 )
609 {
610 /* Type 0 == WEP */
611 if( (wk.ik_type == 0) && (c->auth_algs == 0) )
612 c->auth_algs = (IWINFO_AUTH_OPEN | IWINFO_AUTH_SHARED);
613 }
614
615 /* Get used pairwise ciphers */
616 wrq.u.mode = IEEE80211_PARAM_UCASTCIPHERS;
617 if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
618 {
619 ciphers = wrq.u.mode;
620
621 if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_TKIP) )
622 c->pair_ciphers |= IWINFO_CIPHER_TKIP;
623
624 if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_AES_CCM) )
625 c->pair_ciphers |= IWINFO_CIPHER_CCMP;
626
627 if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_AES_OCB) )
628 c->pair_ciphers |= IWINFO_CIPHER_AESOCB;
629
630 if( c->wpa_version && ciphers & (1 << IEEE80211_CIPHER_CKIP) )
631 c->pair_ciphers |= IWINFO_CIPHER_CKIP;
632
633 if( !c->pair_ciphers && ciphers & (1 << IEEE80211_CIPHER_WEP) )
634 {
635 switch(key_len) {
636 case 13:
637 c->pair_ciphers |= IWINFO_CIPHER_WEP104;
638 break;
639
640 case 5:
641 c->pair_ciphers |= IWINFO_CIPHER_WEP40;
642 break;
643
644 case 0:
645 break;
646
647 default:
648 c->pair_ciphers = IWINFO_CIPHER_WEP40 |
649 IWINFO_CIPHER_WEP104;
650 break;
651 }
652 }
653
654 if( ciphers & (1 << IEEE80211_CIPHER_NONE) )
655 c->pair_ciphers |= IWINFO_CIPHER_NONE;
656 }
657
658 /* Get used group cipher */
659 wrq.u.mode = IEEE80211_PARAM_MCASTCIPHER;
660 if( madwifi_wrq(&wrq, ifname, IEEE80211_IOCTL_GETPARAM, NULL, 0) >= 0 )
661 {
662 ciphers = c->wpa_version ? wrq.u.mode : IEEE80211_CIPHER_WEP;
663
664 switch(ciphers) {
665 case IEEE80211_CIPHER_TKIP:
666 c->group_ciphers |= IWINFO_CIPHER_TKIP;
667 break;
668
669 case IEEE80211_CIPHER_AES_CCM:
670 c->group_ciphers |= IWINFO_CIPHER_CCMP;
671 break;
672
673 case IEEE80211_CIPHER_AES_OCB:
674 c->group_ciphers |= IWINFO_CIPHER_AESOCB;
675 break;
676
677 case IEEE80211_CIPHER_CKIP:
678 c->group_ciphers |= IWINFO_CIPHER_CKIP;
679 break;
680
681 case IEEE80211_CIPHER_WEP:
682 switch(key_len) {
683 case 13:
684 c->group_ciphers |= IWINFO_CIPHER_WEP104;
685 break;
686
687 case 5:
688 c->group_ciphers |= IWINFO_CIPHER_WEP40;
689 break;
690
691 default:
692 break;
693 }
694 break;
695
696 case IEEE80211_CIPHER_NONE:
697 c->group_ciphers |= IWINFO_CIPHER_NONE;
698 break;
699
700 default:
701 break;
702 }
703 }
704
705 c->enabled = (c->wpa_version || (c->auth_algs && c->pair_ciphers)) ? 1 : 0;
706
707 return 0;
708 }
709
710 int madwifi_get_assoclist(const char *ifname, char *buf, int *len)
711 {
712 int bl, tl, noise;
713 uint8_t *cp;
714 uint8_t tmp[24*1024];
715 struct ieee80211req_sta_info *si;
716 struct iwinfo_assoclist_entry entry;
717
718 if( (tl = get80211priv(ifname, IEEE80211_IOCTL_STA_INFO, tmp, 24*1024)) > 0 )
719 {
720 cp = tmp;
721 bl = 0;
722
723 if( madwifi_get_noise(ifname, &noise) )
724 noise = 0;
725
726 do {
727 si = (struct ieee80211req_sta_info *) cp;
728
729 memset(&entry, 0, sizeof(entry));
730
731 entry.signal = (si->isi_rssi - 95);
732 entry.noise = noise;
733 memcpy(entry.mac, &si->isi_macaddr, 6);
734
735 entry.inactive = si->isi_inact * 1000;
736
737 entry.tx_packets = (si->isi_txseqs[0] & IEEE80211_SEQ_SEQ_MASK)
738 >> IEEE80211_SEQ_SEQ_SHIFT;
739
740 entry.rx_packets = (si->isi_rxseqs[0] & IEEE80211_SEQ_SEQ_MASK)
741 >> IEEE80211_SEQ_SEQ_SHIFT;
742
743 entry.tx_rate.rate =
744 (si->isi_rates[si->isi_txrate] & IEEE80211_RATE_VAL) * 500;
745
746 /* XXX: this is just a guess */
747 entry.rx_rate.rate = entry.tx_rate.rate;
748
749 entry.rx_rate.mcs = -1;
750 entry.tx_rate.mcs = -1;
751
752 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_assoclist_entry));
753
754 bl += sizeof(struct iwinfo_assoclist_entry);
755 cp += si->isi_len;
756 tl -= si->isi_len;
757 } while (tl >= sizeof(struct ieee80211req_sta_info));
758
759 *len = bl;
760 return 0;
761 }
762
763 return -1;
764 }
765
766 int madwifi_get_txpwrlist(const char *ifname, char *buf, int *len)
767 {
768 int rc = -1;
769 char *res;
770
771 /* A wifiX device? */
772 if( madwifi_iswifi(ifname) )
773 {
774 if( (res = madwifi_ifadd(ifname)) != NULL )
775 {
776 rc = wext_get_txpwrlist(res, buf, len);
777 madwifi_ifdel(res);
778 }
779 }
780
781 /* Its an athX ... */
782 else if( !!madwifi_isvap(ifname, NULL) )
783 {
784 rc = wext_get_txpwrlist(ifname, buf, len);
785 }
786
787 return rc;
788 }
789
790 int madwifi_get_scanlist(const char *ifname, char *buf, int *len)
791 {
792 int ret;
793 char *res;
794 DIR *proc;
795 struct dirent *e;
796
797 ret = -1;
798
799 /* We got a wifiX device passed, try to lookup a vap on it */
800 if( madwifi_iswifi(ifname) )
801 {
802 if( (proc = opendir("/proc/sys/net/")) != NULL )
803 {
804 while( (e = readdir(proc)) != NULL )
805 {
806 if( !!madwifi_isvap(e->d_name, ifname) )
807 {
808 if( iwinfo_ifup(e->d_name) )
809 {
810 ret = wext_get_scanlist(e->d_name, buf, len);
811 break;
812 }
813 }
814 }
815
816 closedir(proc);
817 }
818
819 /* Still nothing found, try to create a vap */
820 if( ret == -1 )
821 {
822 if( (res = madwifi_ifadd(ifname)) != NULL )
823 {
824 if( iwinfo_ifup(res) )
825 {
826 wext_get_scanlist(res, buf, len);
827 sleep(1);
828
829 wext_get_scanlist(res, buf, len);
830 sleep(1);
831
832 ret = wext_get_scanlist(res, buf, len);
833 }
834
835 iwinfo_ifdown(res);
836 madwifi_ifdel(res);
837 }
838 }
839 }
840
841 /* Got athX device? */
842 else if( !!madwifi_isvap(ifname, NULL) )
843 {
844 ret = wext_get_scanlist(ifname, buf, len);
845 }
846
847 return ret;
848 }
849
850 int madwifi_get_freqlist(const char *ifname, char *buf, int *len)
851 {
852 int i, bl;
853 int rc = -1;
854 char *res;
855 struct ieee80211req_chaninfo chans;
856 struct iwinfo_freqlist_entry entry;
857
858 /* A wifiX device? */
859 if( madwifi_iswifi(ifname) )
860 {
861 if( (res = madwifi_ifadd(ifname)) != NULL )
862 {
863 rc = get80211priv(res, IEEE80211_IOCTL_GETCHANINFO,
864 &chans, sizeof(chans));
865
866 madwifi_ifdel(res);
867 }
868 }
869
870 /* Its an athX ... */
871 else if( !!madwifi_isvap(ifname, NULL) )
872 {
873 rc = get80211priv(ifname, IEEE80211_IOCTL_GETCHANINFO,
874 &chans, sizeof(chans));
875 }
876
877
878 /* Got chaninfo? */
879 if( rc >= 0 )
880 {
881 bl = 0;
882
883 for( i = 0; i < chans.ic_nchans; i++ )
884 {
885 entry.mhz = chans.ic_chans[i].ic_freq;
886 entry.channel = chans.ic_chans[i].ic_ieee;
887 entry.restricted = 0;
888
889 memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
890 bl += sizeof(struct iwinfo_freqlist_entry);
891 }
892
893 *len = bl;
894 return 0;
895 }
896
897 return -1;
898 }
899
900 int madwifi_get_country(const char *ifname, char *buf)
901 {
902 int i, fd, ccode = -1;
903 char buffer[34];
904 char *wifi = madwifi_iswifi(ifname)
905 ? (char *)ifname : madwifi_isvap(ifname, NULL);
906
907 struct ISO3166_to_CCode *e;
908
909 if( wifi )
910 {
911 snprintf(buffer, sizeof(buffer), "/proc/sys/dev/%s/countrycode", wifi);
912
913 if( (fd = open(buffer, O_RDONLY)) > -1 )
914 {
915 memset(buffer, 0, sizeof(buffer));
916
917 if( read(fd, buffer, sizeof(buffer)-1) > 0 )
918 ccode = atoi(buffer);
919
920 close(fd);
921 }
922 }
923
924 for( i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
925 {
926 e = &CountryCodes[i];
927
928 if( e->ccode == ccode )
929 {
930 sprintf(buf, "%c%c", e->iso3166 / 256, e->iso3166 % 256);
931 return 0;
932 }
933 }
934
935 return -1;
936 }
937
938 int madwifi_get_countrylist(const char *ifname, char *buf, int *len)
939 {
940 int i, count;
941 struct ISO3166_to_CCode *e, *p = NULL;
942 struct iwinfo_country_entry *c = (struct iwinfo_country_entry *)buf;
943
944 count = 0;
945
946 for( int i = 0; i < (sizeof(CountryCodes)/sizeof(CountryCodes[0])); i++ )
947 {
948 e = &CountryCodes[i];
949
950 if( !p || (e->iso3166 != p->iso3166) )
951 {
952 c->iso3166 = e->iso3166;
953 snprintf(c->ccode, sizeof(c->ccode), "%i", e->ccode);
954
955 c++;
956 count++;
957 }
958
959 p = e;
960 }
961
962 *len = (count * sizeof(struct iwinfo_country_entry));
963 return 0;
964 }
965
966 int madwifi_get_hwmodelist(const char *ifname, int *buf)
967 {
968 char chans[IWINFO_BUFSIZE] = { 0 };
969 struct iwinfo_freqlist_entry *e = NULL;
970 int len = 0;
971
972 if( !madwifi_get_freqlist(ifname, chans, &len) )
973 {
974 for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
975 {
976 if( e->channel <= 14 )
977 {
978 *buf |= IWINFO_80211_B;
979 *buf |= IWINFO_80211_G;
980 }
981 else
982 {
983 *buf |= IWINFO_80211_A;
984 }
985 }
986
987 return 0;
988 }
989
990 return -1;
991 }
992
993 int madwifi_get_mbssid_support(const char *ifname, int *buf)
994 {
995 /* Test whether we can create another interface */
996 char *nif = madwifi_ifadd(ifname);
997
998 if( nif )
999 {
1000 *buf = iwinfo_ifup(nif);
1001
1002 iwinfo_ifdown(nif);
1003 madwifi_ifdel(nif);
1004
1005 return 0;
1006 }
1007
1008 return -1;
1009 }
1010
1011 int madwifi_get_hardware_id(const char *ifname, char *buf)
1012 {
1013 char vendor[64];
1014 char device[64];
1015 struct iwinfo_hardware_id *ids;
1016 struct iwinfo_hardware_entry *e;
1017
1018 if (wext_get_hardware_id(ifname, buf))
1019 return iwinfo_hardware_id_from_mtd((struct iwinfo_hardware_id *)buf);
1020
1021 return 0;
1022 }
1023
1024 static const struct iwinfo_hardware_entry *
1025 madwifi_get_hardware_entry(const char *ifname)
1026 {
1027 struct iwinfo_hardware_id id;
1028
1029 if (madwifi_get_hardware_id(ifname, (char *)&id))
1030 return NULL;
1031
1032 return iwinfo_hardware(&id);
1033 }
1034
1035 int madwifi_get_hardware_name(const char *ifname, char *buf)
1036 {
1037 const struct iwinfo_hardware_entry *hw;
1038
1039 if (!(hw = madwifi_get_hardware_entry(ifname)))
1040 sprintf(buf, "Generic Atheros");
1041 else
1042 sprintf(buf, "%s %s", hw->vendor_name, hw->device_name);
1043
1044 return 0;
1045 }
1046
1047 int madwifi_get_txpower_offset(const char *ifname, int *buf)
1048 {
1049 const struct iwinfo_hardware_entry *hw;
1050
1051 if (!(hw = madwifi_get_hardware_entry(ifname)))
1052 return -1;
1053
1054 *buf = hw->txpower_offset;
1055 return 0;
1056 }
1057
1058 int madwifi_get_frequency_offset(const char *ifname, int *buf)
1059 {
1060 const struct iwinfo_hardware_entry *hw;
1061
1062 if (!(hw = madwifi_get_hardware_entry(ifname)))
1063 return -1;
1064
1065 *buf = hw->frequency_offset;
1066 return 0;
1067 }