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