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